xref: /linux/drivers/net/pse-pd/pse_core.c (revision fa2f0454174c2f33005f5a6e6f70c7160a15b2a1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Framework for Ethernet Power Sourcing Equipment
4 //
5 // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
6 //
7 
8 #include <linux/device.h>
9 #include <linux/ethtool.h>
10 #include <linux/of.h>
11 #include <linux/pse-pd/pse.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 
15 static DEFINE_MUTEX(pse_list_mutex);
16 static LIST_HEAD(pse_controller_list);
17 
18 /**
19  * struct pse_control - a PSE control
20  * @pcdev: a pointer to the PSE controller device
21  *         this PSE control belongs to
22  * @ps: PSE PI supply of the PSE control
23  * @list: list entry for the pcdev's PSE controller list
24  * @id: ID of the PSE line in the PSE controller device
25  * @refcnt: Number of gets of this pse_control
26  * @attached_phydev: PHY device pointer attached by the PSE control
27  */
28 struct pse_control {
29 	struct pse_controller_dev *pcdev;
30 	struct regulator *ps;
31 	struct list_head list;
32 	unsigned int id;
33 	struct kref refcnt;
34 	struct phy_device *attached_phydev;
35 };
36 
37 static int of_load_single_pse_pi_pairset(struct device_node *node,
38 					 struct pse_pi *pi,
39 					 int pairset_num)
40 {
41 	struct device_node *pairset_np;
42 	const char *name;
43 	int ret;
44 
45 	ret = of_property_read_string_index(node, "pairset-names",
46 					    pairset_num, &name);
47 	if (ret)
48 		return ret;
49 
50 	if (!strcmp(name, "alternative-a")) {
51 		pi->pairset[pairset_num].pinout = ALTERNATIVE_A;
52 	} else if (!strcmp(name, "alternative-b")) {
53 		pi->pairset[pairset_num].pinout = ALTERNATIVE_B;
54 	} else {
55 		pr_err("pse: wrong pairset-names value %s (%pOF)\n",
56 		       name, node);
57 		return -EINVAL;
58 	}
59 
60 	pairset_np = of_parse_phandle(node, "pairsets", pairset_num);
61 	if (!pairset_np)
62 		return -ENODEV;
63 
64 	pi->pairset[pairset_num].np = pairset_np;
65 
66 	return 0;
67 }
68 
69 /**
70  * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity
71  * @node: a pointer of the device node
72  * @pi: a pointer of the PSE PI to fill
73  * @npairsets: the number of pairsets (1 or 2) used by the PI
74  *
75  * Return: 0 on success and failure value on error
76  */
77 static int of_load_pse_pi_pairsets(struct device_node *node,
78 				   struct pse_pi *pi,
79 				   int npairsets)
80 {
81 	int i, ret;
82 
83 	ret = of_property_count_strings(node, "pairset-names");
84 	if (ret != npairsets) {
85 		pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n",
86 		       npairsets, ret, node);
87 		return -EINVAL;
88 	}
89 
90 	for (i = 0; i < npairsets; i++) {
91 		ret = of_load_single_pse_pi_pairset(node, pi, i);
92 		if (ret)
93 			goto out;
94 	}
95 
96 	if (npairsets == 2 &&
97 	    pi->pairset[0].pinout == pi->pairset[1].pinout) {
98 		pr_err("pse: two PI pairsets can not have identical pinout (%pOF)",
99 		       node);
100 		ret = -EINVAL;
101 	}
102 
103 out:
104 	/* If an error appears, release all the pairset device node kref */
105 	if (ret) {
106 		of_node_put(pi->pairset[0].np);
107 		pi->pairset[0].np = NULL;
108 		of_node_put(pi->pairset[1].np);
109 		pi->pairset[1].np = NULL;
110 	}
111 
112 	return ret;
113 }
114 
115 static void pse_release_pis(struct pse_controller_dev *pcdev)
116 {
117 	int i;
118 
119 	for (i = 0; i < pcdev->nr_lines; i++) {
120 		of_node_put(pcdev->pi[i].pairset[0].np);
121 		of_node_put(pcdev->pi[i].pairset[1].np);
122 		of_node_put(pcdev->pi[i].np);
123 	}
124 	kfree(pcdev->pi);
125 }
126 
127 /**
128  * of_load_pse_pis - load all the PSE PIs
129  * @pcdev: a pointer to the PSE controller device
130  *
131  * Return: 0 on success and failure value on error
132  */
133 static int of_load_pse_pis(struct pse_controller_dev *pcdev)
134 {
135 	struct device_node *np = pcdev->dev->of_node;
136 	struct device_node *node, *pis;
137 	int ret;
138 
139 	if (!np)
140 		return -ENODEV;
141 
142 	pcdev->pi = kcalloc(pcdev->nr_lines, sizeof(*pcdev->pi), GFP_KERNEL);
143 	if (!pcdev->pi)
144 		return -ENOMEM;
145 
146 	pis = of_get_child_by_name(np, "pse-pis");
147 	if (!pis) {
148 		/* no description of PSE PIs */
149 		pcdev->no_of_pse_pi = true;
150 		return 0;
151 	}
152 
153 	for_each_child_of_node(pis, node) {
154 		struct pse_pi pi = {0};
155 		u32 id;
156 
157 		if (!of_node_name_eq(node, "pse-pi"))
158 			continue;
159 
160 		ret = of_property_read_u32(node, "reg", &id);
161 		if (ret) {
162 			dev_err(pcdev->dev,
163 				"can't get reg property for node '%pOF'",
164 				node);
165 			goto out;
166 		}
167 
168 		if (id >= pcdev->nr_lines) {
169 			dev_err(pcdev->dev,
170 				"reg value (%u) is out of range (%u) (%pOF)\n",
171 				id, pcdev->nr_lines, node);
172 			ret = -EINVAL;
173 			goto out;
174 		}
175 
176 		if (pcdev->pi[id].np) {
177 			dev_err(pcdev->dev,
178 				"other node with same reg value was already registered. %pOF : %pOF\n",
179 				pcdev->pi[id].np, node);
180 			ret = -EINVAL;
181 			goto out;
182 		}
183 
184 		ret = of_count_phandle_with_args(node, "pairsets", NULL);
185 		/* npairsets is limited to value one or two */
186 		if (ret == 1 || ret == 2) {
187 			ret = of_load_pse_pi_pairsets(node, &pi, ret);
188 			if (ret)
189 				goto out;
190 		} else if (ret != ENOENT) {
191 			dev_err(pcdev->dev,
192 				"error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n",
193 				ret, node);
194 			ret = -EINVAL;
195 			goto out;
196 		}
197 
198 		of_node_get(node);
199 		pi.np = node;
200 		memcpy(&pcdev->pi[id], &pi, sizeof(pi));
201 	}
202 
203 	of_node_put(pis);
204 	return 0;
205 
206 out:
207 	pse_release_pis(pcdev);
208 	of_node_put(node);
209 	of_node_put(pis);
210 	return ret;
211 }
212 
213 static int pse_pi_is_enabled(struct regulator_dev *rdev)
214 {
215 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
216 	struct pse_admin_state admin_state = {0};
217 	const struct pse_controller_ops *ops;
218 	int id, ret;
219 
220 	ops = pcdev->ops;
221 	if (!ops->pi_get_admin_state)
222 		return -EOPNOTSUPP;
223 
224 	id = rdev_get_id(rdev);
225 	mutex_lock(&pcdev->lock);
226 	ret = ops->pi_get_admin_state(pcdev, id, &admin_state);
227 	if (ret)
228 		goto out;
229 
230 	if (admin_state.podl_admin_state == ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED ||
231 	    admin_state.c33_admin_state == ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED)
232 		ret = 1;
233 
234 out:
235 	mutex_unlock(&pcdev->lock);
236 
237 	return ret;
238 }
239 
240 static int pse_pi_enable(struct regulator_dev *rdev)
241 {
242 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
243 	const struct pse_controller_ops *ops;
244 	int id, ret;
245 
246 	ops = pcdev->ops;
247 	if (!ops->pi_enable)
248 		return -EOPNOTSUPP;
249 
250 	id = rdev_get_id(rdev);
251 	mutex_lock(&pcdev->lock);
252 	ret = ops->pi_enable(pcdev, id);
253 	if (!ret)
254 		pcdev->pi[id].admin_state_enabled = 1;
255 	mutex_unlock(&pcdev->lock);
256 
257 	return ret;
258 }
259 
260 static int pse_pi_disable(struct regulator_dev *rdev)
261 {
262 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
263 	const struct pse_controller_ops *ops;
264 	int id, ret;
265 
266 	ops = pcdev->ops;
267 	if (!ops->pi_disable)
268 		return -EOPNOTSUPP;
269 
270 	id = rdev_get_id(rdev);
271 	mutex_lock(&pcdev->lock);
272 	ret = ops->pi_disable(pcdev, id);
273 	if (!ret)
274 		pcdev->pi[id].admin_state_enabled = 0;
275 	mutex_unlock(&pcdev->lock);
276 
277 	return ret;
278 }
279 
280 static int _pse_pi_get_voltage(struct regulator_dev *rdev)
281 {
282 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
283 	const struct pse_controller_ops *ops;
284 	int id;
285 
286 	ops = pcdev->ops;
287 	if (!ops->pi_get_voltage)
288 		return -EOPNOTSUPP;
289 
290 	id = rdev_get_id(rdev);
291 	return ops->pi_get_voltage(pcdev, id);
292 }
293 
294 static int pse_pi_get_voltage(struct regulator_dev *rdev)
295 {
296 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
297 	int ret;
298 
299 	mutex_lock(&pcdev->lock);
300 	ret = _pse_pi_get_voltage(rdev);
301 	mutex_unlock(&pcdev->lock);
302 
303 	return ret;
304 }
305 
306 static int pse_pi_get_current_limit(struct regulator_dev *rdev)
307 {
308 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
309 	const struct pse_controller_ops *ops;
310 	int id, uV, mW, ret;
311 	s64 tmp_64;
312 
313 	ops = pcdev->ops;
314 	id = rdev_get_id(rdev);
315 	if (!ops->pi_get_pw_limit || !ops->pi_get_voltage)
316 		return -EOPNOTSUPP;
317 
318 	mutex_lock(&pcdev->lock);
319 	ret = ops->pi_get_pw_limit(pcdev, id);
320 	if (ret < 0)
321 		goto out;
322 	mW = ret;
323 
324 	ret = _pse_pi_get_voltage(rdev);
325 	if (!ret) {
326 		dev_err(pcdev->dev, "Voltage null\n");
327 		ret = -ERANGE;
328 		goto out;
329 	}
330 	if (ret < 0)
331 		goto out;
332 	uV = ret;
333 
334 	tmp_64 = mW;
335 	tmp_64 *= 1000000000ull;
336 	/* uA = mW * 1000000000 / uV */
337 	ret = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
338 
339 out:
340 	mutex_unlock(&pcdev->lock);
341 	return ret;
342 }
343 
344 static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA,
345 				    int max_uA)
346 {
347 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
348 	const struct pse_controller_ops *ops;
349 	int id, mW, ret;
350 	s64 tmp_64;
351 
352 	ops = pcdev->ops;
353 	if (!ops->pi_set_pw_limit || !ops->pi_get_voltage)
354 		return -EOPNOTSUPP;
355 
356 	if (max_uA > MAX_PI_CURRENT)
357 		return -ERANGE;
358 
359 	id = rdev_get_id(rdev);
360 	mutex_lock(&pcdev->lock);
361 	ret = _pse_pi_get_voltage(rdev);
362 	if (!ret) {
363 		dev_err(pcdev->dev, "Voltage null\n");
364 		ret = -ERANGE;
365 		goto out;
366 	}
367 	if (ret < 0)
368 		goto out;
369 
370 	tmp_64 = ret;
371 	tmp_64 *= max_uA;
372 	/* mW = uA * uV / 1000000000 */
373 	mW = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
374 	ret = ops->pi_set_pw_limit(pcdev, id, mW);
375 out:
376 	mutex_unlock(&pcdev->lock);
377 
378 	return ret;
379 }
380 
381 static const struct regulator_ops pse_pi_ops = {
382 	.is_enabled = pse_pi_is_enabled,
383 	.enable = pse_pi_enable,
384 	.disable = pse_pi_disable,
385 	.get_voltage = pse_pi_get_voltage,
386 	.get_current_limit = pse_pi_get_current_limit,
387 	.set_current_limit = pse_pi_set_current_limit,
388 };
389 
390 static int
391 devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
392 			       char *name, int id)
393 {
394 	struct regulator_init_data *rinit_data;
395 	struct regulator_config rconfig = {0};
396 	struct regulator_desc *rdesc;
397 	struct regulator_dev *rdev;
398 
399 	rinit_data = devm_kzalloc(pcdev->dev, sizeof(*rinit_data),
400 				  GFP_KERNEL);
401 	if (!rinit_data)
402 		return -ENOMEM;
403 
404 	rdesc = devm_kzalloc(pcdev->dev, sizeof(*rdesc), GFP_KERNEL);
405 	if (!rdesc)
406 		return -ENOMEM;
407 
408 	/* Regulator descriptor id have to be the same as its associated
409 	 * PSE PI id for the well functioning of the PSE controls.
410 	 */
411 	rdesc->id = id;
412 	rdesc->name = name;
413 	rdesc->type = REGULATOR_VOLTAGE;
414 	rdesc->ops = &pse_pi_ops;
415 	rdesc->owner = pcdev->owner;
416 
417 	rinit_data->constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
418 
419 	if (pcdev->ops->pi_set_pw_limit)
420 		rinit_data->constraints.valid_ops_mask |=
421 			REGULATOR_CHANGE_CURRENT;
422 
423 	rinit_data->supply_regulator = "vpwr";
424 
425 	rconfig.dev = pcdev->dev;
426 	rconfig.driver_data = pcdev;
427 	rconfig.init_data = rinit_data;
428 	rconfig.of_node = pcdev->pi[id].np;
429 
430 	rdev = devm_regulator_register(pcdev->dev, rdesc, &rconfig);
431 	if (IS_ERR(rdev)) {
432 		dev_err_probe(pcdev->dev, PTR_ERR(rdev),
433 			      "Failed to register regulator\n");
434 		return PTR_ERR(rdev);
435 	}
436 
437 	pcdev->pi[id].rdev = rdev;
438 
439 	return 0;
440 }
441 
442 /**
443  * pse_controller_register - register a PSE controller device
444  * @pcdev: a pointer to the initialized PSE controller device
445  *
446  * Return: 0 on success and failure value on error
447  */
448 int pse_controller_register(struct pse_controller_dev *pcdev)
449 {
450 	size_t reg_name_len;
451 	int ret, i;
452 
453 	mutex_init(&pcdev->lock);
454 	INIT_LIST_HEAD(&pcdev->pse_control_head);
455 
456 	if (!pcdev->nr_lines)
457 		pcdev->nr_lines = 1;
458 
459 	if (!pcdev->ops->pi_get_admin_state ||
460 	    !pcdev->ops->pi_get_pw_status) {
461 		dev_err(pcdev->dev,
462 			"Mandatory status report callbacks are missing");
463 		return -EINVAL;
464 	}
465 
466 	ret = of_load_pse_pis(pcdev);
467 	if (ret)
468 		return ret;
469 
470 	if (pcdev->ops->setup_pi_matrix) {
471 		ret = pcdev->ops->setup_pi_matrix(pcdev);
472 		if (ret)
473 			return ret;
474 	}
475 
476 	/* Each regulator name len is pcdev dev name + 7 char +
477 	 * int max digit number (10) + 1
478 	 */
479 	reg_name_len = strlen(dev_name(pcdev->dev)) + 18;
480 
481 	/* Register PI regulators */
482 	for (i = 0; i < pcdev->nr_lines; i++) {
483 		char *reg_name;
484 
485 		/* Do not register regulator for PIs not described */
486 		if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np)
487 			continue;
488 
489 		reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL);
490 		if (!reg_name)
491 			return -ENOMEM;
492 
493 		snprintf(reg_name, reg_name_len, "pse-%s_pi%d",
494 			 dev_name(pcdev->dev), i);
495 
496 		ret = devm_pse_pi_regulator_register(pcdev, reg_name, i);
497 		if (ret)
498 			return ret;
499 	}
500 
501 	mutex_lock(&pse_list_mutex);
502 	list_add(&pcdev->list, &pse_controller_list);
503 	mutex_unlock(&pse_list_mutex);
504 
505 	return 0;
506 }
507 EXPORT_SYMBOL_GPL(pse_controller_register);
508 
509 /**
510  * pse_controller_unregister - unregister a PSE controller device
511  * @pcdev: a pointer to the PSE controller device
512  */
513 void pse_controller_unregister(struct pse_controller_dev *pcdev)
514 {
515 	pse_release_pis(pcdev);
516 	mutex_lock(&pse_list_mutex);
517 	list_del(&pcdev->list);
518 	mutex_unlock(&pse_list_mutex);
519 }
520 EXPORT_SYMBOL_GPL(pse_controller_unregister);
521 
522 static void devm_pse_controller_release(struct device *dev, void *res)
523 {
524 	pse_controller_unregister(*(struct pse_controller_dev **)res);
525 }
526 
527 /**
528  * devm_pse_controller_register - resource managed pse_controller_register()
529  * @dev: device that is registering this PSE controller
530  * @pcdev: a pointer to the initialized PSE controller device
531  *
532  * Managed pse_controller_register(). For PSE controllers registered by
533  * this function, pse_controller_unregister() is automatically called on
534  * driver detach. See pse_controller_register() for more information.
535  *
536  * Return: 0 on success and failure value on error
537  */
538 int devm_pse_controller_register(struct device *dev,
539 				 struct pse_controller_dev *pcdev)
540 {
541 	struct pse_controller_dev **pcdevp;
542 	int ret;
543 
544 	pcdevp = devres_alloc(devm_pse_controller_release, sizeof(*pcdevp),
545 			      GFP_KERNEL);
546 	if (!pcdevp)
547 		return -ENOMEM;
548 
549 	ret = pse_controller_register(pcdev);
550 	if (ret) {
551 		devres_free(pcdevp);
552 		return ret;
553 	}
554 
555 	*pcdevp = pcdev;
556 	devres_add(dev, pcdevp);
557 
558 	return 0;
559 }
560 EXPORT_SYMBOL_GPL(devm_pse_controller_register);
561 
562 /* PSE control section */
563 
564 static void __pse_control_release(struct kref *kref)
565 {
566 	struct pse_control *psec = container_of(kref, struct pse_control,
567 						  refcnt);
568 
569 	lockdep_assert_held(&pse_list_mutex);
570 
571 	if (psec->pcdev->pi[psec->id].admin_state_enabled)
572 		regulator_disable(psec->ps);
573 	devm_regulator_put(psec->ps);
574 
575 	module_put(psec->pcdev->owner);
576 
577 	list_del(&psec->list);
578 	kfree(psec);
579 }
580 
581 static void __pse_control_put_internal(struct pse_control *psec)
582 {
583 	lockdep_assert_held(&pse_list_mutex);
584 
585 	kref_put(&psec->refcnt, __pse_control_release);
586 }
587 
588 /**
589  * pse_control_put - free the PSE control
590  * @psec: PSE control pointer
591  */
592 void pse_control_put(struct pse_control *psec)
593 {
594 	if (IS_ERR_OR_NULL(psec))
595 		return;
596 
597 	mutex_lock(&pse_list_mutex);
598 	__pse_control_put_internal(psec);
599 	mutex_unlock(&pse_list_mutex);
600 }
601 EXPORT_SYMBOL_GPL(pse_control_put);
602 
603 static struct pse_control *
604 pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index,
605 			 struct phy_device *phydev)
606 {
607 	struct pse_control *psec;
608 	int ret;
609 
610 	lockdep_assert_held(&pse_list_mutex);
611 
612 	list_for_each_entry(psec, &pcdev->pse_control_head, list) {
613 		if (psec->id == index) {
614 			kref_get(&psec->refcnt);
615 			return psec;
616 		}
617 	}
618 
619 	psec = kzalloc(sizeof(*psec), GFP_KERNEL);
620 	if (!psec)
621 		return ERR_PTR(-ENOMEM);
622 
623 	if (!try_module_get(pcdev->owner)) {
624 		ret = -ENODEV;
625 		goto free_psec;
626 	}
627 
628 	psec->ps = devm_regulator_get_exclusive(pcdev->dev,
629 						rdev_get_name(pcdev->pi[index].rdev));
630 	if (IS_ERR(psec->ps)) {
631 		ret = PTR_ERR(psec->ps);
632 		goto put_module;
633 	}
634 
635 	ret = regulator_is_enabled(psec->ps);
636 	if (ret < 0)
637 		goto regulator_put;
638 
639 	pcdev->pi[index].admin_state_enabled = ret;
640 
641 	psec->pcdev = pcdev;
642 	list_add(&psec->list, &pcdev->pse_control_head);
643 	psec->id = index;
644 	psec->attached_phydev = phydev;
645 	kref_init(&psec->refcnt);
646 
647 	return psec;
648 
649 regulator_put:
650 	devm_regulator_put(psec->ps);
651 put_module:
652 	module_put(pcdev->owner);
653 free_psec:
654 	kfree(psec);
655 
656 	return ERR_PTR(ret);
657 }
658 
659 /**
660  * of_pse_match_pi - Find the PSE PI id matching the device node phandle
661  * @pcdev: a pointer to the PSE controller device
662  * @np: a pointer to the device node
663  *
664  * Return: id of the PSE PI, -EINVAL if not found
665  */
666 static int of_pse_match_pi(struct pse_controller_dev *pcdev,
667 			   struct device_node *np)
668 {
669 	int i;
670 
671 	for (i = 0; i < pcdev->nr_lines; i++) {
672 		if (pcdev->pi[i].np == np)
673 			return i;
674 	}
675 
676 	return -EINVAL;
677 }
678 
679 /**
680  * psec_id_xlate - translate pse_spec to the PSE line number according
681  *		   to the number of pse-cells in case of no pse_pi node
682  * @pcdev: a pointer to the PSE controller device
683  * @pse_spec: PSE line specifier as found in the device tree
684  *
685  * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise.
686  */
687 static int psec_id_xlate(struct pse_controller_dev *pcdev,
688 			 const struct of_phandle_args *pse_spec)
689 {
690 	if (!pcdev->of_pse_n_cells)
691 		return 0;
692 
693 	if (pcdev->of_pse_n_cells > 1 ||
694 	    pse_spec->args[0] >= pcdev->nr_lines)
695 		return -EINVAL;
696 
697 	return pse_spec->args[0];
698 }
699 
700 struct pse_control *of_pse_control_get(struct device_node *node,
701 				       struct phy_device *phydev)
702 {
703 	struct pse_controller_dev *r, *pcdev;
704 	struct of_phandle_args args;
705 	struct pse_control *psec;
706 	int psec_id;
707 	int ret;
708 
709 	if (!node)
710 		return ERR_PTR(-EINVAL);
711 
712 	ret = of_parse_phandle_with_args(node, "pses", "#pse-cells", 0, &args);
713 	if (ret)
714 		return ERR_PTR(ret);
715 
716 	mutex_lock(&pse_list_mutex);
717 	pcdev = NULL;
718 	list_for_each_entry(r, &pse_controller_list, list) {
719 		if (!r->no_of_pse_pi) {
720 			ret = of_pse_match_pi(r, args.np);
721 			if (ret >= 0) {
722 				pcdev = r;
723 				psec_id = ret;
724 				break;
725 			}
726 		} else if (args.np == r->dev->of_node) {
727 			pcdev = r;
728 			break;
729 		}
730 	}
731 
732 	if (!pcdev) {
733 		psec = ERR_PTR(-EPROBE_DEFER);
734 		goto out;
735 	}
736 
737 	if (WARN_ON(args.args_count != pcdev->of_pse_n_cells)) {
738 		psec = ERR_PTR(-EINVAL);
739 		goto out;
740 	}
741 
742 	if (pcdev->no_of_pse_pi) {
743 		psec_id = psec_id_xlate(pcdev, &args);
744 		if (psec_id < 0) {
745 			psec = ERR_PTR(psec_id);
746 			goto out;
747 		}
748 	}
749 
750 	/* pse_list_mutex also protects the pcdev's pse_control list */
751 	psec = pse_control_get_internal(pcdev, psec_id, phydev);
752 
753 out:
754 	mutex_unlock(&pse_list_mutex);
755 	of_node_put(args.np);
756 
757 	return psec;
758 }
759 EXPORT_SYMBOL_GPL(of_pse_control_get);
760 
761 /**
762  * pse_ethtool_get_status - get status of PSE control
763  * @psec: PSE control pointer
764  * @extack: extack for reporting useful error messages
765  * @status: struct to store PSE status
766  *
767  * Return: 0 on success and failure value on error
768  */
769 int pse_ethtool_get_status(struct pse_control *psec,
770 			   struct netlink_ext_ack *extack,
771 			   struct ethtool_pse_control_status *status)
772 {
773 	struct pse_admin_state admin_state = {0};
774 	struct pse_pw_status pw_status = {0};
775 	const struct pse_controller_ops *ops;
776 	struct pse_controller_dev *pcdev;
777 	int ret;
778 
779 	pcdev = psec->pcdev;
780 	ops = pcdev->ops;
781 	mutex_lock(&pcdev->lock);
782 	ret = ops->pi_get_admin_state(pcdev, psec->id, &admin_state);
783 	if (ret)
784 		goto out;
785 	status->podl_admin_state = admin_state.podl_admin_state;
786 	status->c33_admin_state = admin_state.c33_admin_state;
787 
788 	ret = ops->pi_get_pw_status(pcdev, psec->id, &pw_status);
789 	if (ret)
790 		goto out;
791 	status->podl_pw_status = pw_status.podl_pw_status;
792 	status->c33_pw_status = pw_status.c33_pw_status;
793 
794 	if (ops->pi_get_ext_state) {
795 		struct pse_ext_state_info ext_state_info = {0};
796 
797 		ret = ops->pi_get_ext_state(pcdev, psec->id,
798 					    &ext_state_info);
799 		if (ret)
800 			goto out;
801 
802 		memcpy(&status->c33_ext_state_info,
803 		       &ext_state_info.c33_ext_state_info,
804 		       sizeof(status->c33_ext_state_info));
805 	}
806 
807 	if (ops->pi_get_pw_class) {
808 		ret = ops->pi_get_pw_class(pcdev, psec->id);
809 		if (ret < 0)
810 			goto out;
811 
812 		status->c33_pw_class = ret;
813 	}
814 
815 	if (ops->pi_get_actual_pw) {
816 		ret = ops->pi_get_actual_pw(pcdev, psec->id);
817 		if (ret < 0)
818 			goto out;
819 
820 		status->c33_actual_pw = ret;
821 	}
822 
823 	if (ops->pi_get_pw_limit) {
824 		ret = ops->pi_get_pw_limit(pcdev, psec->id);
825 		if (ret < 0)
826 			goto out;
827 
828 		status->c33_avail_pw_limit = ret;
829 	}
830 
831 	if (ops->pi_get_pw_limit_ranges) {
832 		struct pse_pw_limit_ranges pw_limit_ranges = {0};
833 
834 		ret = ops->pi_get_pw_limit_ranges(pcdev, psec->id,
835 						  &pw_limit_ranges);
836 		if (ret < 0)
837 			goto out;
838 
839 		status->c33_pw_limit_ranges =
840 			pw_limit_ranges.c33_pw_limit_ranges;
841 		status->c33_pw_limit_nb_ranges = ret;
842 	}
843 out:
844 	mutex_unlock(&psec->pcdev->lock);
845 	return ret;
846 }
847 EXPORT_SYMBOL_GPL(pse_ethtool_get_status);
848 
849 static int pse_ethtool_c33_set_config(struct pse_control *psec,
850 				      const struct pse_control_config *config)
851 {
852 	int err = 0;
853 
854 	/* Look at admin_state_enabled status to not call regulator_enable
855 	 * or regulator_disable twice creating a regulator counter mismatch
856 	 */
857 	switch (config->c33_admin_control) {
858 	case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED:
859 		/* We could have mismatch between admin_state_enabled and
860 		 * state reported by regulator_is_enabled. This can occur when
861 		 * the PI is forcibly turn off by the controller. Call
862 		 * regulator_disable on that case to fix the counters state.
863 		 */
864 		if (psec->pcdev->pi[psec->id].admin_state_enabled &&
865 		    !regulator_is_enabled(psec->ps)) {
866 			err = regulator_disable(psec->ps);
867 			if (err)
868 				break;
869 		}
870 		if (!psec->pcdev->pi[psec->id].admin_state_enabled)
871 			err = regulator_enable(psec->ps);
872 		break;
873 	case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED:
874 		if (psec->pcdev->pi[psec->id].admin_state_enabled)
875 			err = regulator_disable(psec->ps);
876 		break;
877 	default:
878 		err = -EOPNOTSUPP;
879 	}
880 
881 	return err;
882 }
883 
884 static int pse_ethtool_podl_set_config(struct pse_control *psec,
885 				       const struct pse_control_config *config)
886 {
887 	int err = 0;
888 
889 	/* Look at admin_state_enabled status to not call regulator_enable
890 	 * or regulator_disable twice creating a regulator counter mismatch
891 	 */
892 	switch (config->podl_admin_control) {
893 	case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
894 		if (!psec->pcdev->pi[psec->id].admin_state_enabled)
895 			err = regulator_enable(psec->ps);
896 		break;
897 	case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
898 		if (psec->pcdev->pi[psec->id].admin_state_enabled)
899 			err = regulator_disable(psec->ps);
900 		break;
901 	default:
902 		err = -EOPNOTSUPP;
903 	}
904 
905 	return err;
906 }
907 
908 /**
909  * pse_ethtool_set_config - set PSE control configuration
910  * @psec: PSE control pointer
911  * @extack: extack for reporting useful error messages
912  * @config: Configuration of the test to run
913  *
914  * Return: 0 on success and failure value on error
915  */
916 int pse_ethtool_set_config(struct pse_control *psec,
917 			   struct netlink_ext_ack *extack,
918 			   const struct pse_control_config *config)
919 {
920 	int err = 0;
921 
922 	if (pse_has_c33(psec) && config->c33_admin_control) {
923 		err = pse_ethtool_c33_set_config(psec, config);
924 		if (err)
925 			return err;
926 	}
927 
928 	if (pse_has_podl(psec) && config->podl_admin_control)
929 		err = pse_ethtool_podl_set_config(psec, config);
930 
931 	return err;
932 }
933 EXPORT_SYMBOL_GPL(pse_ethtool_set_config);
934 
935 /**
936  * pse_ethtool_set_pw_limit - set PSE control power limit
937  * @psec: PSE control pointer
938  * @extack: extack for reporting useful error messages
939  * @pw_limit: power limit value in mW
940  *
941  * Return: 0 on success and failure value on error
942  */
943 int pse_ethtool_set_pw_limit(struct pse_control *psec,
944 			     struct netlink_ext_ack *extack,
945 			     const unsigned int pw_limit)
946 {
947 	int uV, uA, ret;
948 	s64 tmp_64;
949 
950 	if (pw_limit > MAX_PI_PW)
951 		return -ERANGE;
952 
953 	ret = regulator_get_voltage(psec->ps);
954 	if (!ret) {
955 		NL_SET_ERR_MSG(extack,
956 			       "Can't calculate the current, PSE voltage read is 0");
957 		return -ERANGE;
958 	}
959 	if (ret < 0) {
960 		NL_SET_ERR_MSG(extack,
961 			       "Error reading PSE voltage");
962 		return ret;
963 	}
964 	uV = ret;
965 
966 	tmp_64 = pw_limit;
967 	tmp_64 *= 1000000000ull;
968 	/* uA = mW * 1000000000 / uV */
969 	uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
970 
971 	return regulator_set_current_limit(psec->ps, 0, uA);
972 }
973 EXPORT_SYMBOL_GPL(pse_ethtool_set_pw_limit);
974 
975 bool pse_has_podl(struct pse_control *psec)
976 {
977 	return psec->pcdev->types & ETHTOOL_PSE_PODL;
978 }
979 EXPORT_SYMBOL_GPL(pse_has_podl);
980 
981 bool pse_has_c33(struct pse_control *psec)
982 {
983 	return psec->pcdev->types & ETHTOOL_PSE_C33;
984 }
985 EXPORT_SYMBOL_GPL(pse_has_c33);
986