xref: /linux/drivers/net/pse-pd/pse_core.c (revision c394e757dedd9cf947f9ac470d615d28fd2b07d1)
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/ethtool_netlink.h>
11 #include <linux/of.h>
12 #include <linux/phy.h>
13 #include <linux/pse-pd/pse.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/rtnetlink.h>
17 #include <net/net_trackers.h>
18 
19 #define PSE_PW_D_LIMIT INT_MAX
20 
21 static DEFINE_MUTEX(pse_list_mutex);
22 static LIST_HEAD(pse_controller_list);
23 static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
24 static DEFINE_MUTEX(pse_pw_d_mutex);
25 
26 /**
27  * struct pse_control - a PSE control
28  * @pcdev: a pointer to the PSE controller device
29  *         this PSE control belongs to
30  * @ps: PSE PI supply of the PSE control
31  * @list: list entry for the pcdev's PSE controller list
32  * @id: ID of the PSE line in the PSE controller device
33  * @refcnt: Number of gets of this pse_control
34  * @attached_phydev: PHY device pointer attached by the PSE control
35  */
36 struct pse_control {
37 	struct pse_controller_dev *pcdev;
38 	struct regulator *ps;
39 	struct list_head list;
40 	unsigned int id;
41 	struct kref refcnt;
42 	struct phy_device *attached_phydev;
43 };
44 
45 /**
46  * struct pse_power_domain - a PSE power domain
47  * @id: ID of the power domain
48  * @supply: Power supply the Power Domain
49  * @refcnt: Number of gets of this pse_power_domain
50  */
51 struct pse_power_domain {
52 	int id;
53 	struct regulator *supply;
54 	struct kref refcnt;
55 };
56 
57 static int of_load_single_pse_pi_pairset(struct device_node *node,
58 					 struct pse_pi *pi,
59 					 int pairset_num)
60 {
61 	struct device_node *pairset_np;
62 	const char *name;
63 	int ret;
64 
65 	ret = of_property_read_string_index(node, "pairset-names",
66 					    pairset_num, &name);
67 	if (ret)
68 		return ret;
69 
70 	if (!strcmp(name, "alternative-a")) {
71 		pi->pairset[pairset_num].pinout = ALTERNATIVE_A;
72 	} else if (!strcmp(name, "alternative-b")) {
73 		pi->pairset[pairset_num].pinout = ALTERNATIVE_B;
74 	} else {
75 		pr_err("pse: wrong pairset-names value %s (%pOF)\n",
76 		       name, node);
77 		return -EINVAL;
78 	}
79 
80 	pairset_np = of_parse_phandle(node, "pairsets", pairset_num);
81 	if (!pairset_np)
82 		return -ENODEV;
83 
84 	pi->pairset[pairset_num].np = pairset_np;
85 
86 	return 0;
87 }
88 
89 /**
90  * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity
91  * @node: a pointer of the device node
92  * @pi: a pointer of the PSE PI to fill
93  * @npairsets: the number of pairsets (1 or 2) used by the PI
94  *
95  * Return: 0 on success and failure value on error
96  */
97 static int of_load_pse_pi_pairsets(struct device_node *node,
98 				   struct pse_pi *pi,
99 				   int npairsets)
100 {
101 	int i, ret;
102 
103 	ret = of_property_count_strings(node, "pairset-names");
104 	if (ret != npairsets) {
105 		pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n",
106 		       npairsets, ret, node);
107 		return -EINVAL;
108 	}
109 
110 	for (i = 0; i < npairsets; i++) {
111 		ret = of_load_single_pse_pi_pairset(node, pi, i);
112 		if (ret)
113 			goto out;
114 	}
115 
116 	if (npairsets == 2 &&
117 	    pi->pairset[0].pinout == pi->pairset[1].pinout) {
118 		pr_err("pse: two PI pairsets can not have identical pinout (%pOF)",
119 		       node);
120 		ret = -EINVAL;
121 	}
122 
123 out:
124 	/* If an error appears, release all the pairset device node kref */
125 	if (ret) {
126 		of_node_put(pi->pairset[0].np);
127 		pi->pairset[0].np = NULL;
128 		of_node_put(pi->pairset[1].np);
129 		pi->pairset[1].np = NULL;
130 	}
131 
132 	return ret;
133 }
134 
135 static void pse_release_pis(struct pse_controller_dev *pcdev)
136 {
137 	int i;
138 
139 	for (i = 0; i < pcdev->nr_lines; i++) {
140 		of_node_put(pcdev->pi[i].pairset[0].np);
141 		of_node_put(pcdev->pi[i].pairset[1].np);
142 		of_node_put(pcdev->pi[i].np);
143 	}
144 	kfree(pcdev->pi);
145 }
146 
147 /**
148  * of_load_pse_pis - load all the PSE PIs
149  * @pcdev: a pointer to the PSE controller device
150  *
151  * Return: 0 on success and failure value on error
152  */
153 static int of_load_pse_pis(struct pse_controller_dev *pcdev)
154 {
155 	struct device_node *np = pcdev->dev->of_node;
156 	struct device_node *node, *pis;
157 	int ret;
158 
159 	if (!np)
160 		return -ENODEV;
161 
162 	pcdev->pi = kcalloc(pcdev->nr_lines, sizeof(*pcdev->pi), GFP_KERNEL);
163 	if (!pcdev->pi)
164 		return -ENOMEM;
165 
166 	pis = of_get_child_by_name(np, "pse-pis");
167 	if (!pis) {
168 		/* no description of PSE PIs */
169 		pcdev->no_of_pse_pi = true;
170 		return 0;
171 	}
172 
173 	for_each_child_of_node(pis, node) {
174 		struct pse_pi pi = {0};
175 		u32 id;
176 
177 		if (!of_node_name_eq(node, "pse-pi"))
178 			continue;
179 
180 		ret = of_property_read_u32(node, "reg", &id);
181 		if (ret) {
182 			dev_err(pcdev->dev,
183 				"can't get reg property for node '%pOF'",
184 				node);
185 			goto out;
186 		}
187 
188 		if (id >= pcdev->nr_lines) {
189 			dev_err(pcdev->dev,
190 				"reg value (%u) is out of range (%u) (%pOF)\n",
191 				id, pcdev->nr_lines, node);
192 			ret = -EINVAL;
193 			goto out;
194 		}
195 
196 		if (pcdev->pi[id].np) {
197 			dev_err(pcdev->dev,
198 				"other node with same reg value was already registered. %pOF : %pOF\n",
199 				pcdev->pi[id].np, node);
200 			ret = -EINVAL;
201 			goto out;
202 		}
203 
204 		ret = of_count_phandle_with_args(node, "pairsets", NULL);
205 		/* npairsets is limited to value one or two */
206 		if (ret == 1 || ret == 2) {
207 			ret = of_load_pse_pi_pairsets(node, &pi, ret);
208 			if (ret)
209 				goto out;
210 		} else if (ret != ENOENT) {
211 			dev_err(pcdev->dev,
212 				"error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n",
213 				ret, node);
214 			ret = -EINVAL;
215 			goto out;
216 		}
217 
218 		of_node_get(node);
219 		pi.np = node;
220 		memcpy(&pcdev->pi[id], &pi, sizeof(pi));
221 	}
222 
223 	of_node_put(pis);
224 	return 0;
225 
226 out:
227 	pse_release_pis(pcdev);
228 	of_node_put(node);
229 	of_node_put(pis);
230 	return ret;
231 }
232 
233 /**
234  * pse_control_find_net_by_id - Find net attached to the pse control id
235  * @pcdev: a pointer to the PSE
236  * @id: index of the PSE control
237  *
238  * Return: pse_control pointer or NULL. The device returned has had a
239  *	   reference added and the pointer is safe until the user calls
240  *	   pse_control_put() to indicate they have finished with it.
241  */
242 static struct pse_control *
243 pse_control_find_by_id(struct pse_controller_dev *pcdev, int id)
244 {
245 	struct pse_control *psec;
246 
247 	mutex_lock(&pse_list_mutex);
248 	list_for_each_entry(psec, &pcdev->pse_control_head, list) {
249 		if (psec->id == id) {
250 			kref_get(&psec->refcnt);
251 			mutex_unlock(&pse_list_mutex);
252 			return psec;
253 		}
254 	}
255 	mutex_unlock(&pse_list_mutex);
256 	return NULL;
257 }
258 
259 /**
260  * pse_control_get_netdev - Return netdev associated to a PSE control
261  * @psec: PSE control pointer
262  *
263  * Return: netdev pointer or NULL
264  */
265 static struct net_device *pse_control_get_netdev(struct pse_control *psec)
266 {
267 	ASSERT_RTNL();
268 
269 	if (!psec || !psec->attached_phydev)
270 		return NULL;
271 
272 	return psec->attached_phydev->attached_dev;
273 }
274 
275 /**
276  * pse_pi_is_hw_enabled - Is PI enabled at the hardware level
277  * @pcdev: a pointer to the PSE controller device
278  * @id: Index of the PI
279  *
280  * Return: 1 if the PI is enabled at the hardware level, 0 if not, and
281  *	   a failure value on error
282  */
283 static int pse_pi_is_hw_enabled(struct pse_controller_dev *pcdev, int id)
284 {
285 	struct pse_admin_state admin_state = {0};
286 	int ret;
287 
288 	ret = pcdev->ops->pi_get_admin_state(pcdev, id, &admin_state);
289 	if (ret < 0)
290 		return ret;
291 
292 	/* PI is well enabled at the hardware level */
293 	if (admin_state.podl_admin_state == ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED ||
294 	    admin_state.c33_admin_state == ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED)
295 		return 1;
296 
297 	return 0;
298 }
299 
300 static int pse_pi_is_enabled(struct regulator_dev *rdev)
301 {
302 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
303 	const struct pse_controller_ops *ops;
304 	int id, ret;
305 
306 	ops = pcdev->ops;
307 	if (!ops->pi_get_admin_state)
308 		return -EOPNOTSUPP;
309 
310 	id = rdev_get_id(rdev);
311 	mutex_lock(&pcdev->lock);
312 	ret = pse_pi_is_hw_enabled(pcdev, id);
313 	mutex_unlock(&pcdev->lock);
314 
315 	return ret;
316 }
317 
318 static int pse_pi_enable(struct regulator_dev *rdev)
319 {
320 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
321 	const struct pse_controller_ops *ops;
322 	int id, ret;
323 
324 	ops = pcdev->ops;
325 	if (!ops->pi_enable)
326 		return -EOPNOTSUPP;
327 
328 	id = rdev_get_id(rdev);
329 	mutex_lock(&pcdev->lock);
330 	ret = ops->pi_enable(pcdev, id);
331 	if (!ret)
332 		pcdev->pi[id].admin_state_enabled = 1;
333 	mutex_unlock(&pcdev->lock);
334 
335 	return ret;
336 }
337 
338 static int pse_pi_disable(struct regulator_dev *rdev)
339 {
340 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
341 	const struct pse_controller_ops *ops;
342 	int id, ret;
343 
344 	ops = pcdev->ops;
345 	if (!ops->pi_disable)
346 		return -EOPNOTSUPP;
347 
348 	id = rdev_get_id(rdev);
349 	mutex_lock(&pcdev->lock);
350 	ret = ops->pi_disable(pcdev, id);
351 	if (!ret)
352 		pcdev->pi[id].admin_state_enabled = 0;
353 	mutex_unlock(&pcdev->lock);
354 
355 	return ret;
356 }
357 
358 static int _pse_pi_get_voltage(struct regulator_dev *rdev)
359 {
360 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
361 	const struct pse_controller_ops *ops;
362 	int id;
363 
364 	ops = pcdev->ops;
365 	if (!ops->pi_get_voltage)
366 		return -EOPNOTSUPP;
367 
368 	id = rdev_get_id(rdev);
369 	return ops->pi_get_voltage(pcdev, id);
370 }
371 
372 static int pse_pi_get_voltage(struct regulator_dev *rdev)
373 {
374 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
375 	int ret;
376 
377 	mutex_lock(&pcdev->lock);
378 	ret = _pse_pi_get_voltage(rdev);
379 	mutex_unlock(&pcdev->lock);
380 
381 	return ret;
382 }
383 
384 static int pse_pi_get_current_limit(struct regulator_dev *rdev)
385 {
386 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
387 	const struct pse_controller_ops *ops;
388 	int id, uV, mW, ret;
389 	s64 tmp_64;
390 
391 	ops = pcdev->ops;
392 	id = rdev_get_id(rdev);
393 	if (!ops->pi_get_pw_limit || !ops->pi_get_voltage)
394 		return -EOPNOTSUPP;
395 
396 	mutex_lock(&pcdev->lock);
397 	ret = ops->pi_get_pw_limit(pcdev, id);
398 	if (ret < 0)
399 		goto out;
400 	mW = ret;
401 
402 	ret = _pse_pi_get_voltage(rdev);
403 	if (!ret) {
404 		dev_err(pcdev->dev, "Voltage null\n");
405 		ret = -ERANGE;
406 		goto out;
407 	}
408 	if (ret < 0)
409 		goto out;
410 	uV = ret;
411 
412 	tmp_64 = mW;
413 	tmp_64 *= 1000000000ull;
414 	/* uA = mW * 1000000000 / uV */
415 	ret = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
416 
417 out:
418 	mutex_unlock(&pcdev->lock);
419 	return ret;
420 }
421 
422 static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA,
423 				    int max_uA)
424 {
425 	struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
426 	const struct pse_controller_ops *ops;
427 	int id, mW, ret;
428 	s64 tmp_64;
429 
430 	ops = pcdev->ops;
431 	if (!ops->pi_set_pw_limit || !ops->pi_get_voltage)
432 		return -EOPNOTSUPP;
433 
434 	if (max_uA > MAX_PI_CURRENT)
435 		return -ERANGE;
436 
437 	id = rdev_get_id(rdev);
438 	mutex_lock(&pcdev->lock);
439 	ret = _pse_pi_get_voltage(rdev);
440 	if (!ret) {
441 		dev_err(pcdev->dev, "Voltage null\n");
442 		ret = -ERANGE;
443 		goto out;
444 	}
445 	if (ret < 0)
446 		goto out;
447 
448 	tmp_64 = ret;
449 	tmp_64 *= max_uA;
450 	/* mW = uA * uV / 1000000000 */
451 	mW = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
452 	ret = ops->pi_set_pw_limit(pcdev, id, mW);
453 out:
454 	mutex_unlock(&pcdev->lock);
455 
456 	return ret;
457 }
458 
459 static const struct regulator_ops pse_pi_ops = {
460 	.is_enabled = pse_pi_is_enabled,
461 	.enable = pse_pi_enable,
462 	.disable = pse_pi_disable,
463 	.get_voltage = pse_pi_get_voltage,
464 	.get_current_limit = pse_pi_get_current_limit,
465 	.set_current_limit = pse_pi_set_current_limit,
466 };
467 
468 static int
469 devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
470 			       char *name, int id)
471 {
472 	struct regulator_init_data *rinit_data;
473 	struct regulator_config rconfig = {0};
474 	struct regulator_desc *rdesc;
475 	struct regulator_dev *rdev;
476 
477 	rinit_data = devm_kzalloc(pcdev->dev, sizeof(*rinit_data),
478 				  GFP_KERNEL);
479 	if (!rinit_data)
480 		return -ENOMEM;
481 
482 	rdesc = devm_kzalloc(pcdev->dev, sizeof(*rdesc), GFP_KERNEL);
483 	if (!rdesc)
484 		return -ENOMEM;
485 
486 	/* Regulator descriptor id have to be the same as its associated
487 	 * PSE PI id for the well functioning of the PSE controls.
488 	 */
489 	rdesc->id = id;
490 	rdesc->name = name;
491 	rdesc->type = REGULATOR_VOLTAGE;
492 	rdesc->ops = &pse_pi_ops;
493 	rdesc->owner = pcdev->owner;
494 
495 	rinit_data->constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
496 
497 	if (pcdev->ops->pi_set_pw_limit)
498 		rinit_data->constraints.valid_ops_mask |=
499 			REGULATOR_CHANGE_CURRENT;
500 
501 	rinit_data->supply_regulator = "vpwr";
502 
503 	rconfig.dev = pcdev->dev;
504 	rconfig.driver_data = pcdev;
505 	rconfig.init_data = rinit_data;
506 	rconfig.of_node = pcdev->pi[id].np;
507 
508 	rdev = devm_regulator_register(pcdev->dev, rdesc, &rconfig);
509 	if (IS_ERR(rdev)) {
510 		dev_err_probe(pcdev->dev, PTR_ERR(rdev),
511 			      "Failed to register regulator\n");
512 		return PTR_ERR(rdev);
513 	}
514 
515 	pcdev->pi[id].rdev = rdev;
516 
517 	return 0;
518 }
519 
520 static void __pse_pw_d_release(struct kref *kref)
521 {
522 	struct pse_power_domain *pw_d = container_of(kref,
523 						     struct pse_power_domain,
524 						     refcnt);
525 
526 	regulator_put(pw_d->supply);
527 	xa_erase(&pse_pw_d_map, pw_d->id);
528 	mutex_unlock(&pse_pw_d_mutex);
529 }
530 
531 /**
532  * pse_flush_pw_ds - flush all PSE power domains of a PSE
533  * @pcdev: a pointer to the initialized PSE controller device
534  */
535 static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
536 {
537 	struct pse_power_domain *pw_d;
538 	int i;
539 
540 	for (i = 0; i < pcdev->nr_lines; i++) {
541 		if (!pcdev->pi[i].pw_d)
542 			continue;
543 
544 		pw_d = xa_load(&pse_pw_d_map, pcdev->pi[i].pw_d->id);
545 		if (!pw_d)
546 			continue;
547 
548 		kref_put_mutex(&pw_d->refcnt, __pse_pw_d_release,
549 			       &pse_pw_d_mutex);
550 	}
551 }
552 
553 /**
554  * devm_pse_alloc_pw_d - allocate a new PSE power domain for a device
555  * @dev: device that is registering this PSE power domain
556  *
557  * Return: Pointer to the newly allocated PSE power domain or error pointers
558  */
559 static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev)
560 {
561 	struct pse_power_domain *pw_d;
562 	int index, ret;
563 
564 	pw_d = devm_kzalloc(dev, sizeof(*pw_d), GFP_KERNEL);
565 	if (!pw_d)
566 		return ERR_PTR(-ENOMEM);
567 
568 	ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1, PSE_PW_D_LIMIT),
569 		       GFP_KERNEL);
570 	if (ret)
571 		return ERR_PTR(ret);
572 
573 	kref_init(&pw_d->refcnt);
574 	pw_d->id = index;
575 	return pw_d;
576 }
577 
578 /**
579  * pse_register_pw_ds - register the PSE power domains for a PSE
580  * @pcdev: a pointer to the PSE controller device
581  *
582  * Return: 0 on success and failure value on error
583  */
584 static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
585 {
586 	int i, ret = 0;
587 
588 	mutex_lock(&pse_pw_d_mutex);
589 	for (i = 0; i < pcdev->nr_lines; i++) {
590 		struct regulator_dev *rdev = pcdev->pi[i].rdev;
591 		struct pse_power_domain *pw_d;
592 		struct regulator *supply;
593 		bool present = false;
594 		unsigned long index;
595 
596 		/* No regulator or regulator parent supply registered.
597 		 * We need a regulator parent to register a PSE power domain
598 		 */
599 		if (!rdev || !rdev->supply)
600 			continue;
601 
602 		xa_for_each(&pse_pw_d_map, index, pw_d) {
603 			/* Power supply already registered as a PSE power
604 			 * domain.
605 			 */
606 			if (regulator_is_equal(pw_d->supply, rdev->supply)) {
607 				present = true;
608 				pcdev->pi[i].pw_d = pw_d;
609 				break;
610 			}
611 		}
612 		if (present) {
613 			kref_get(&pw_d->refcnt);
614 			continue;
615 		}
616 
617 		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
618 		if (IS_ERR(pw_d)) {
619 			ret = PTR_ERR(pw_d);
620 			goto out;
621 		}
622 
623 		supply = regulator_get(&rdev->dev, rdev->supply_name);
624 		if (IS_ERR(supply)) {
625 			xa_erase(&pse_pw_d_map, pw_d->id);
626 			ret = PTR_ERR(supply);
627 			goto out;
628 		}
629 
630 		pw_d->supply = supply;
631 		pcdev->pi[i].pw_d = pw_d;
632 	}
633 
634 out:
635 	mutex_unlock(&pse_pw_d_mutex);
636 	return ret;
637 }
638 
639 /**
640  * pse_controller_register - register a PSE controller device
641  * @pcdev: a pointer to the initialized PSE controller device
642  *
643  * Return: 0 on success and failure value on error
644  */
645 int pse_controller_register(struct pse_controller_dev *pcdev)
646 {
647 	size_t reg_name_len;
648 	int ret, i;
649 
650 	mutex_init(&pcdev->lock);
651 	INIT_LIST_HEAD(&pcdev->pse_control_head);
652 
653 	if (!pcdev->nr_lines)
654 		pcdev->nr_lines = 1;
655 
656 	if (!pcdev->ops->pi_get_admin_state ||
657 	    !pcdev->ops->pi_get_pw_status) {
658 		dev_err(pcdev->dev,
659 			"Mandatory status report callbacks are missing");
660 		return -EINVAL;
661 	}
662 
663 	ret = of_load_pse_pis(pcdev);
664 	if (ret)
665 		return ret;
666 
667 	if (pcdev->ops->setup_pi_matrix) {
668 		ret = pcdev->ops->setup_pi_matrix(pcdev);
669 		if (ret)
670 			return ret;
671 	}
672 
673 	/* Each regulator name len is pcdev dev name + 7 char +
674 	 * int max digit number (10) + 1
675 	 */
676 	reg_name_len = strlen(dev_name(pcdev->dev)) + 18;
677 
678 	/* Register PI regulators */
679 	for (i = 0; i < pcdev->nr_lines; i++) {
680 		char *reg_name;
681 
682 		/* Do not register regulator for PIs not described */
683 		if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np)
684 			continue;
685 
686 		reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL);
687 		if (!reg_name)
688 			return -ENOMEM;
689 
690 		snprintf(reg_name, reg_name_len, "pse-%s_pi%d",
691 			 dev_name(pcdev->dev), i);
692 
693 		ret = devm_pse_pi_regulator_register(pcdev, reg_name, i);
694 		if (ret)
695 			return ret;
696 	}
697 
698 	ret = pse_register_pw_ds(pcdev);
699 	if (ret)
700 		return ret;
701 
702 	mutex_lock(&pse_list_mutex);
703 	list_add(&pcdev->list, &pse_controller_list);
704 	mutex_unlock(&pse_list_mutex);
705 
706 	return 0;
707 }
708 EXPORT_SYMBOL_GPL(pse_controller_register);
709 
710 /**
711  * pse_controller_unregister - unregister a PSE controller device
712  * @pcdev: a pointer to the PSE controller device
713  */
714 void pse_controller_unregister(struct pse_controller_dev *pcdev)
715 {
716 	pse_flush_pw_ds(pcdev);
717 	pse_release_pis(pcdev);
718 	mutex_lock(&pse_list_mutex);
719 	list_del(&pcdev->list);
720 	mutex_unlock(&pse_list_mutex);
721 }
722 EXPORT_SYMBOL_GPL(pse_controller_unregister);
723 
724 static void devm_pse_controller_release(struct device *dev, void *res)
725 {
726 	pse_controller_unregister(*(struct pse_controller_dev **)res);
727 }
728 
729 /**
730  * devm_pse_controller_register - resource managed pse_controller_register()
731  * @dev: device that is registering this PSE controller
732  * @pcdev: a pointer to the initialized PSE controller device
733  *
734  * Managed pse_controller_register(). For PSE controllers registered by
735  * this function, pse_controller_unregister() is automatically called on
736  * driver detach. See pse_controller_register() for more information.
737  *
738  * Return: 0 on success and failure value on error
739  */
740 int devm_pse_controller_register(struct device *dev,
741 				 struct pse_controller_dev *pcdev)
742 {
743 	struct pse_controller_dev **pcdevp;
744 	int ret;
745 
746 	pcdevp = devres_alloc(devm_pse_controller_release, sizeof(*pcdevp),
747 			      GFP_KERNEL);
748 	if (!pcdevp)
749 		return -ENOMEM;
750 
751 	ret = pse_controller_register(pcdev);
752 	if (ret) {
753 		devres_free(pcdevp);
754 		return ret;
755 	}
756 
757 	*pcdevp = pcdev;
758 	devres_add(dev, pcdevp);
759 
760 	return 0;
761 }
762 EXPORT_SYMBOL_GPL(devm_pse_controller_register);
763 
764 struct pse_irq {
765 	struct pse_controller_dev *pcdev;
766 	struct pse_irq_desc desc;
767 	unsigned long *notifs;
768 };
769 
770 /**
771  * pse_to_regulator_notifs - Convert PSE notifications to Regulator
772  *			     notifications
773  * @notifs: PSE notifications
774  *
775  * Return: Regulator notifications
776  */
777 static unsigned long pse_to_regulator_notifs(unsigned long notifs)
778 {
779 	unsigned long rnotifs = 0;
780 
781 	if (notifs & ETHTOOL_PSE_EVENT_OVER_CURRENT)
782 		rnotifs |= REGULATOR_EVENT_OVER_CURRENT;
783 	if (notifs & ETHTOOL_PSE_EVENT_OVER_TEMP)
784 		rnotifs |= REGULATOR_EVENT_OVER_TEMP;
785 
786 	return rnotifs;
787 }
788 
789 /**
790  * pse_isr - IRQ handler for PSE
791  * @irq: irq number
792  * @data: pointer to user interrupt structure
793  *
794  * Return: irqreturn_t - status of IRQ
795  */
796 static irqreturn_t pse_isr(int irq, void *data)
797 {
798 	struct pse_controller_dev *pcdev;
799 	unsigned long notifs_mask = 0;
800 	struct pse_irq_desc *desc;
801 	struct pse_irq *h = data;
802 	int ret, i;
803 
804 	desc = &h->desc;
805 	pcdev = h->pcdev;
806 
807 	/* Clear notifs mask */
808 	memset(h->notifs, 0, pcdev->nr_lines * sizeof(*h->notifs));
809 	mutex_lock(&pcdev->lock);
810 	ret = desc->map_event(irq, pcdev, h->notifs, &notifs_mask);
811 	mutex_unlock(&pcdev->lock);
812 	if (ret || !notifs_mask)
813 		return IRQ_NONE;
814 
815 	for_each_set_bit(i, &notifs_mask, pcdev->nr_lines) {
816 		unsigned long notifs, rnotifs;
817 		struct net_device *netdev;
818 		struct pse_control *psec;
819 
820 		/* Do nothing PI not described */
821 		if (!pcdev->pi[i].rdev)
822 			continue;
823 
824 		notifs = h->notifs[i];
825 		dev_dbg(h->pcdev->dev,
826 			"Sending PSE notification EVT 0x%lx\n", notifs);
827 
828 		psec = pse_control_find_by_id(pcdev, i);
829 		rtnl_lock();
830 		netdev = pse_control_get_netdev(psec);
831 		if (netdev)
832 			ethnl_pse_send_ntf(netdev, notifs);
833 		rtnl_unlock();
834 		pse_control_put(psec);
835 
836 		rnotifs = pse_to_regulator_notifs(notifs);
837 		regulator_notifier_call_chain(pcdev->pi[i].rdev, rnotifs,
838 					      NULL);
839 	}
840 
841 	return IRQ_HANDLED;
842 }
843 
844 /**
845  * devm_pse_irq_helper - Register IRQ based PSE event notifier
846  * @pcdev: a pointer to the PSE
847  * @irq: the irq value to be passed to request_irq
848  * @irq_flags: the flags to be passed to request_irq
849  * @d: PSE interrupt description
850  *
851  * Return: 0 on success and errno on failure
852  */
853 int devm_pse_irq_helper(struct pse_controller_dev *pcdev, int irq,
854 			int irq_flags, const struct pse_irq_desc *d)
855 {
856 	struct device *dev = pcdev->dev;
857 	size_t irq_name_len;
858 	struct pse_irq *h;
859 	char *irq_name;
860 	int ret;
861 
862 	if (!d || !d->map_event || !d->name)
863 		return -EINVAL;
864 
865 	h = devm_kzalloc(dev, sizeof(*h), GFP_KERNEL);
866 	if (!h)
867 		return -ENOMEM;
868 
869 	h->pcdev = pcdev;
870 	h->desc = *d;
871 
872 	/* IRQ name len is pcdev dev name + 5 char + irq desc name + 1 */
873 	irq_name_len = strlen(dev_name(pcdev->dev)) + 5 + strlen(d->name) + 1;
874 	irq_name = devm_kzalloc(dev, irq_name_len, GFP_KERNEL);
875 	if (!irq_name)
876 		return -ENOMEM;
877 
878 	snprintf(irq_name, irq_name_len, "pse-%s:%s", dev_name(pcdev->dev),
879 		 d->name);
880 
881 	h->notifs = devm_kcalloc(dev, pcdev->nr_lines,
882 				 sizeof(*h->notifs), GFP_KERNEL);
883 	if (!h->notifs)
884 		return -ENOMEM;
885 
886 	ret = devm_request_threaded_irq(dev, irq, NULL, pse_isr,
887 					IRQF_ONESHOT | irq_flags,
888 					irq_name, h);
889 	if (ret)
890 		dev_err(pcdev->dev, "Failed to request IRQ %d\n", irq);
891 
892 	pcdev->irq = irq;
893 	return ret;
894 }
895 EXPORT_SYMBOL_GPL(devm_pse_irq_helper);
896 
897 /* PSE control section */
898 
899 static void __pse_control_release(struct kref *kref)
900 {
901 	struct pse_control *psec = container_of(kref, struct pse_control,
902 						  refcnt);
903 
904 	lockdep_assert_held(&pse_list_mutex);
905 
906 	if (psec->pcdev->pi[psec->id].admin_state_enabled)
907 		regulator_disable(psec->ps);
908 	devm_regulator_put(psec->ps);
909 
910 	module_put(psec->pcdev->owner);
911 
912 	list_del(&psec->list);
913 	kfree(psec);
914 }
915 
916 static void __pse_control_put_internal(struct pse_control *psec)
917 {
918 	lockdep_assert_held(&pse_list_mutex);
919 
920 	kref_put(&psec->refcnt, __pse_control_release);
921 }
922 
923 /**
924  * pse_control_put - free the PSE control
925  * @psec: PSE control pointer
926  */
927 void pse_control_put(struct pse_control *psec)
928 {
929 	if (IS_ERR_OR_NULL(psec))
930 		return;
931 
932 	mutex_lock(&pse_list_mutex);
933 	__pse_control_put_internal(psec);
934 	mutex_unlock(&pse_list_mutex);
935 }
936 EXPORT_SYMBOL_GPL(pse_control_put);
937 
938 static struct pse_control *
939 pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index,
940 			 struct phy_device *phydev)
941 {
942 	struct pse_control *psec;
943 	int ret;
944 
945 	lockdep_assert_held(&pse_list_mutex);
946 
947 	list_for_each_entry(psec, &pcdev->pse_control_head, list) {
948 		if (psec->id == index) {
949 			kref_get(&psec->refcnt);
950 			return psec;
951 		}
952 	}
953 
954 	psec = kzalloc(sizeof(*psec), GFP_KERNEL);
955 	if (!psec)
956 		return ERR_PTR(-ENOMEM);
957 
958 	if (!try_module_get(pcdev->owner)) {
959 		ret = -ENODEV;
960 		goto free_psec;
961 	}
962 
963 	psec->ps = devm_regulator_get_exclusive(pcdev->dev,
964 						rdev_get_name(pcdev->pi[index].rdev));
965 	if (IS_ERR(psec->ps)) {
966 		ret = PTR_ERR(psec->ps);
967 		goto put_module;
968 	}
969 
970 	ret = regulator_is_enabled(psec->ps);
971 	if (ret < 0)
972 		goto regulator_put;
973 
974 	pcdev->pi[index].admin_state_enabled = ret;
975 
976 	psec->pcdev = pcdev;
977 	list_add(&psec->list, &pcdev->pse_control_head);
978 	psec->id = index;
979 	psec->attached_phydev = phydev;
980 	kref_init(&psec->refcnt);
981 
982 	return psec;
983 
984 regulator_put:
985 	devm_regulator_put(psec->ps);
986 put_module:
987 	module_put(pcdev->owner);
988 free_psec:
989 	kfree(psec);
990 
991 	return ERR_PTR(ret);
992 }
993 
994 /**
995  * of_pse_match_pi - Find the PSE PI id matching the device node phandle
996  * @pcdev: a pointer to the PSE controller device
997  * @np: a pointer to the device node
998  *
999  * Return: id of the PSE PI, -EINVAL if not found
1000  */
1001 static int of_pse_match_pi(struct pse_controller_dev *pcdev,
1002 			   struct device_node *np)
1003 {
1004 	int i;
1005 
1006 	for (i = 0; i < pcdev->nr_lines; i++) {
1007 		if (pcdev->pi[i].np == np)
1008 			return i;
1009 	}
1010 
1011 	return -EINVAL;
1012 }
1013 
1014 /**
1015  * psec_id_xlate - translate pse_spec to the PSE line number according
1016  *		   to the number of pse-cells in case of no pse_pi node
1017  * @pcdev: a pointer to the PSE controller device
1018  * @pse_spec: PSE line specifier as found in the device tree
1019  *
1020  * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise.
1021  */
1022 static int psec_id_xlate(struct pse_controller_dev *pcdev,
1023 			 const struct of_phandle_args *pse_spec)
1024 {
1025 	if (!pcdev->of_pse_n_cells)
1026 		return 0;
1027 
1028 	if (pcdev->of_pse_n_cells > 1 ||
1029 	    pse_spec->args[0] >= pcdev->nr_lines)
1030 		return -EINVAL;
1031 
1032 	return pse_spec->args[0];
1033 }
1034 
1035 struct pse_control *of_pse_control_get(struct device_node *node,
1036 				       struct phy_device *phydev)
1037 {
1038 	struct pse_controller_dev *r, *pcdev;
1039 	struct of_phandle_args args;
1040 	struct pse_control *psec;
1041 	int psec_id;
1042 	int ret;
1043 
1044 	if (!node)
1045 		return ERR_PTR(-EINVAL);
1046 
1047 	ret = of_parse_phandle_with_args(node, "pses", "#pse-cells", 0, &args);
1048 	if (ret)
1049 		return ERR_PTR(ret);
1050 
1051 	mutex_lock(&pse_list_mutex);
1052 	pcdev = NULL;
1053 	list_for_each_entry(r, &pse_controller_list, list) {
1054 		if (!r->no_of_pse_pi) {
1055 			ret = of_pse_match_pi(r, args.np);
1056 			if (ret >= 0) {
1057 				pcdev = r;
1058 				psec_id = ret;
1059 				break;
1060 			}
1061 		} else if (args.np == r->dev->of_node) {
1062 			pcdev = r;
1063 			break;
1064 		}
1065 	}
1066 
1067 	if (!pcdev) {
1068 		psec = ERR_PTR(-EPROBE_DEFER);
1069 		goto out;
1070 	}
1071 
1072 	if (WARN_ON(args.args_count != pcdev->of_pse_n_cells)) {
1073 		psec = ERR_PTR(-EINVAL);
1074 		goto out;
1075 	}
1076 
1077 	if (pcdev->no_of_pse_pi) {
1078 		psec_id = psec_id_xlate(pcdev, &args);
1079 		if (psec_id < 0) {
1080 			psec = ERR_PTR(psec_id);
1081 			goto out;
1082 		}
1083 	}
1084 
1085 	/* pse_list_mutex also protects the pcdev's pse_control list */
1086 	psec = pse_control_get_internal(pcdev, psec_id, phydev);
1087 
1088 out:
1089 	mutex_unlock(&pse_list_mutex);
1090 	of_node_put(args.np);
1091 
1092 	return psec;
1093 }
1094 EXPORT_SYMBOL_GPL(of_pse_control_get);
1095 
1096 /**
1097  * pse_ethtool_get_status - get status of PSE control
1098  * @psec: PSE control pointer
1099  * @extack: extack for reporting useful error messages
1100  * @status: struct to store PSE status
1101  *
1102  * Return: 0 on success and failure value on error
1103  */
1104 int pse_ethtool_get_status(struct pse_control *psec,
1105 			   struct netlink_ext_ack *extack,
1106 			   struct ethtool_pse_control_status *status)
1107 {
1108 	struct pse_admin_state admin_state = {0};
1109 	struct pse_pw_status pw_status = {0};
1110 	const struct pse_controller_ops *ops;
1111 	struct pse_controller_dev *pcdev;
1112 	int ret;
1113 
1114 	pcdev = psec->pcdev;
1115 	ops = pcdev->ops;
1116 	mutex_lock(&pcdev->lock);
1117 	if (pcdev->pi[psec->id].pw_d)
1118 		status->pw_d_id = pcdev->pi[psec->id].pw_d->id;
1119 
1120 	ret = ops->pi_get_admin_state(pcdev, psec->id, &admin_state);
1121 	if (ret)
1122 		goto out;
1123 	status->podl_admin_state = admin_state.podl_admin_state;
1124 	status->c33_admin_state = admin_state.c33_admin_state;
1125 
1126 	ret = ops->pi_get_pw_status(pcdev, psec->id, &pw_status);
1127 	if (ret)
1128 		goto out;
1129 	status->podl_pw_status = pw_status.podl_pw_status;
1130 	status->c33_pw_status = pw_status.c33_pw_status;
1131 
1132 	if (ops->pi_get_ext_state) {
1133 		struct pse_ext_state_info ext_state_info = {0};
1134 
1135 		ret = ops->pi_get_ext_state(pcdev, psec->id,
1136 					    &ext_state_info);
1137 		if (ret)
1138 			goto out;
1139 
1140 		memcpy(&status->c33_ext_state_info,
1141 		       &ext_state_info.c33_ext_state_info,
1142 		       sizeof(status->c33_ext_state_info));
1143 	}
1144 
1145 	if (ops->pi_get_pw_class) {
1146 		ret = ops->pi_get_pw_class(pcdev, psec->id);
1147 		if (ret < 0)
1148 			goto out;
1149 
1150 		status->c33_pw_class = ret;
1151 	}
1152 
1153 	if (ops->pi_get_actual_pw) {
1154 		ret = ops->pi_get_actual_pw(pcdev, psec->id);
1155 		if (ret < 0)
1156 			goto out;
1157 
1158 		status->c33_actual_pw = ret;
1159 	}
1160 
1161 	if (ops->pi_get_pw_limit) {
1162 		ret = ops->pi_get_pw_limit(pcdev, psec->id);
1163 		if (ret < 0)
1164 			goto out;
1165 
1166 		status->c33_avail_pw_limit = ret;
1167 	}
1168 
1169 	if (ops->pi_get_pw_limit_ranges) {
1170 		struct pse_pw_limit_ranges pw_limit_ranges = {0};
1171 
1172 		ret = ops->pi_get_pw_limit_ranges(pcdev, psec->id,
1173 						  &pw_limit_ranges);
1174 		if (ret < 0)
1175 			goto out;
1176 
1177 		status->c33_pw_limit_ranges =
1178 			pw_limit_ranges.c33_pw_limit_ranges;
1179 		status->c33_pw_limit_nb_ranges = ret;
1180 	}
1181 out:
1182 	mutex_unlock(&psec->pcdev->lock);
1183 	return ret;
1184 }
1185 EXPORT_SYMBOL_GPL(pse_ethtool_get_status);
1186 
1187 static int pse_ethtool_c33_set_config(struct pse_control *psec,
1188 				      const struct pse_control_config *config)
1189 {
1190 	int err = 0;
1191 
1192 	/* Look at admin_state_enabled status to not call regulator_enable
1193 	 * or regulator_disable twice creating a regulator counter mismatch
1194 	 */
1195 	switch (config->c33_admin_control) {
1196 	case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED:
1197 		/* We could have mismatch between admin_state_enabled and
1198 		 * state reported by regulator_is_enabled. This can occur when
1199 		 * the PI is forcibly turn off by the controller. Call
1200 		 * regulator_disable on that case to fix the counters state.
1201 		 */
1202 		if (psec->pcdev->pi[psec->id].admin_state_enabled &&
1203 		    !regulator_is_enabled(psec->ps)) {
1204 			err = regulator_disable(psec->ps);
1205 			if (err)
1206 				break;
1207 		}
1208 		if (!psec->pcdev->pi[psec->id].admin_state_enabled)
1209 			err = regulator_enable(psec->ps);
1210 		break;
1211 	case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED:
1212 		if (psec->pcdev->pi[psec->id].admin_state_enabled)
1213 			err = regulator_disable(psec->ps);
1214 		break;
1215 	default:
1216 		err = -EOPNOTSUPP;
1217 	}
1218 
1219 	return err;
1220 }
1221 
1222 static int pse_ethtool_podl_set_config(struct pse_control *psec,
1223 				       const struct pse_control_config *config)
1224 {
1225 	int err = 0;
1226 
1227 	/* Look at admin_state_enabled status to not call regulator_enable
1228 	 * or regulator_disable twice creating a regulator counter mismatch
1229 	 */
1230 	switch (config->podl_admin_control) {
1231 	case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
1232 		if (!psec->pcdev->pi[psec->id].admin_state_enabled)
1233 			err = regulator_enable(psec->ps);
1234 		break;
1235 	case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
1236 		if (psec->pcdev->pi[psec->id].admin_state_enabled)
1237 			err = regulator_disable(psec->ps);
1238 		break;
1239 	default:
1240 		err = -EOPNOTSUPP;
1241 	}
1242 
1243 	return err;
1244 }
1245 
1246 /**
1247  * pse_ethtool_set_config - set PSE control configuration
1248  * @psec: PSE control pointer
1249  * @extack: extack for reporting useful error messages
1250  * @config: Configuration of the test to run
1251  *
1252  * Return: 0 on success and failure value on error
1253  */
1254 int pse_ethtool_set_config(struct pse_control *psec,
1255 			   struct netlink_ext_ack *extack,
1256 			   const struct pse_control_config *config)
1257 {
1258 	int err = 0;
1259 
1260 	if (pse_has_c33(psec) && config->c33_admin_control) {
1261 		err = pse_ethtool_c33_set_config(psec, config);
1262 		if (err)
1263 			return err;
1264 	}
1265 
1266 	if (pse_has_podl(psec) && config->podl_admin_control)
1267 		err = pse_ethtool_podl_set_config(psec, config);
1268 
1269 	return err;
1270 }
1271 EXPORT_SYMBOL_GPL(pse_ethtool_set_config);
1272 
1273 /**
1274  * pse_ethtool_set_pw_limit - set PSE control power limit
1275  * @psec: PSE control pointer
1276  * @extack: extack for reporting useful error messages
1277  * @pw_limit: power limit value in mW
1278  *
1279  * Return: 0 on success and failure value on error
1280  */
1281 int pse_ethtool_set_pw_limit(struct pse_control *psec,
1282 			     struct netlink_ext_ack *extack,
1283 			     const unsigned int pw_limit)
1284 {
1285 	int uV, uA, ret;
1286 	s64 tmp_64;
1287 
1288 	if (pw_limit > MAX_PI_PW)
1289 		return -ERANGE;
1290 
1291 	ret = regulator_get_voltage(psec->ps);
1292 	if (!ret) {
1293 		NL_SET_ERR_MSG(extack,
1294 			       "Can't calculate the current, PSE voltage read is 0");
1295 		return -ERANGE;
1296 	}
1297 	if (ret < 0) {
1298 		NL_SET_ERR_MSG(extack,
1299 			       "Error reading PSE voltage");
1300 		return ret;
1301 	}
1302 	uV = ret;
1303 
1304 	tmp_64 = pw_limit;
1305 	tmp_64 *= 1000000000ull;
1306 	/* uA = mW * 1000000000 / uV */
1307 	uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
1308 
1309 	return regulator_set_current_limit(psec->ps, 0, uA);
1310 }
1311 EXPORT_SYMBOL_GPL(pse_ethtool_set_pw_limit);
1312 
1313 bool pse_has_podl(struct pse_control *psec)
1314 {
1315 	return psec->pcdev->types & ETHTOOL_PSE_PODL;
1316 }
1317 EXPORT_SYMBOL_GPL(pse_has_podl);
1318 
1319 bool pse_has_c33(struct pse_control *psec)
1320 {
1321 	return psec->pcdev->types & ETHTOOL_PSE_C33;
1322 }
1323 EXPORT_SYMBOL_GPL(pse_has_c33);
1324