1 // SPDX-License-Identifier: GPL-2.0-only
3 // Framework for Ethernet Power Sourcing Equipment
5 // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
8 #include <linux/device.h>
9 #include <linux/ethtool.h>
11 #include <linux/pse-pd/pse.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
15 static DEFINE_MUTEX(pse_list_mutex
);
16 static LIST_HEAD(pse_controller_list
);
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
28 struct pse_controller_dev
*pcdev
;
30 struct list_head list
;
35 static int of_load_single_pse_pi_pairset(struct device_node
*node
,
39 struct device_node
*pairset_np
;
43 ret
= of_property_read_string_index(node
, "pairset-names",
48 if (!strcmp(name
, "alternative-a")) {
49 pi
->pairset
[pairset_num
].pinout
= ALTERNATIVE_A
;
50 } else if (!strcmp(name
, "alternative-b")) {
51 pi
->pairset
[pairset_num
].pinout
= ALTERNATIVE_B
;
53 pr_err("pse: wrong pairset-names value %s (%pOF)\n",
58 pairset_np
= of_parse_phandle(node
, "pairsets", pairset_num
);
62 pi
->pairset
[pairset_num
].np
= pairset_np
;
68 * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity
69 * @node: a pointer of the device node
70 * @pi: a pointer of the PSE PI to fill
71 * @npairsets: the number of pairsets (1 or 2) used by the PI
73 * Return: 0 on success and failure value on error
75 static int of_load_pse_pi_pairsets(struct device_node
*node
,
81 ret
= of_property_count_strings(node
, "pairset-names");
82 if (ret
!= npairsets
) {
83 pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n",
84 npairsets
, ret
, node
);
88 for (i
= 0; i
< npairsets
; i
++) {
89 ret
= of_load_single_pse_pi_pairset(node
, pi
, i
);
95 pi
->pairset
[0].pinout
== pi
->pairset
[1].pinout
) {
96 pr_err("pse: two PI pairsets can not have identical pinout (%pOF)",
102 /* If an error appears, release all the pairset device node kref */
104 of_node_put(pi
->pairset
[0].np
);
105 pi
->pairset
[0].np
= NULL
;
106 of_node_put(pi
->pairset
[1].np
);
107 pi
->pairset
[1].np
= NULL
;
113 static void pse_release_pis(struct pse_controller_dev
*pcdev
)
117 for (i
= 0; i
< pcdev
->nr_lines
; i
++) {
118 of_node_put(pcdev
->pi
[i
].pairset
[0].np
);
119 of_node_put(pcdev
->pi
[i
].pairset
[1].np
);
120 of_node_put(pcdev
->pi
[i
].np
);
126 * of_load_pse_pis - load all the PSE PIs
127 * @pcdev: a pointer to the PSE controller device
129 * Return: 0 on success and failure value on error
131 static int of_load_pse_pis(struct pse_controller_dev
*pcdev
)
133 struct device_node
*np
= pcdev
->dev
->of_node
;
134 struct device_node
*node
, *pis
;
140 pcdev
->pi
= kcalloc(pcdev
->nr_lines
, sizeof(*pcdev
->pi
), GFP_KERNEL
);
144 pis
= of_get_child_by_name(np
, "pse-pis");
146 /* no description of PSE PIs */
147 pcdev
->no_of_pse_pi
= true;
151 for_each_child_of_node(pis
, node
) {
152 struct pse_pi pi
= {0};
155 if (!of_node_name_eq(node
, "pse-pi"))
158 ret
= of_property_read_u32(node
, "reg", &id
);
161 "can't get reg property for node '%pOF'",
166 if (id
>= pcdev
->nr_lines
) {
168 "reg value (%u) is out of range (%u) (%pOF)\n",
169 id
, pcdev
->nr_lines
, node
);
174 if (pcdev
->pi
[id
].np
) {
176 "other node with same reg value was already registered. %pOF : %pOF\n",
177 pcdev
->pi
[id
].np
, node
);
182 ret
= of_count_phandle_with_args(node
, "pairsets", NULL
);
183 /* npairsets is limited to value one or two */
184 if (ret
== 1 || ret
== 2) {
185 ret
= of_load_pse_pi_pairsets(node
, &pi
, ret
);
188 } else if (ret
!= ENOENT
) {
190 "error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n",
198 memcpy(&pcdev
->pi
[id
], &pi
, sizeof(pi
));
205 pse_release_pis(pcdev
);
211 static int pse_pi_is_enabled(struct regulator_dev
*rdev
)
213 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
214 struct pse_admin_state admin_state
= {0};
215 const struct pse_controller_ops
*ops
;
219 if (!ops
->pi_get_admin_state
)
222 id
= rdev_get_id(rdev
);
223 mutex_lock(&pcdev
->lock
);
224 ret
= ops
->pi_get_admin_state(pcdev
, id
, &admin_state
);
228 if (admin_state
.podl_admin_state
== ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED
||
229 admin_state
.c33_admin_state
== ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED
)
233 mutex_unlock(&pcdev
->lock
);
238 static int pse_pi_enable(struct regulator_dev
*rdev
)
240 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
241 const struct pse_controller_ops
*ops
;
248 id
= rdev_get_id(rdev
);
249 mutex_lock(&pcdev
->lock
);
250 ret
= ops
->pi_enable(pcdev
, id
);
252 pcdev
->pi
[id
].admin_state_enabled
= 1;
253 mutex_unlock(&pcdev
->lock
);
258 static int pse_pi_disable(struct regulator_dev
*rdev
)
260 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
261 const struct pse_controller_ops
*ops
;
265 if (!ops
->pi_disable
)
268 id
= rdev_get_id(rdev
);
269 mutex_lock(&pcdev
->lock
);
270 ret
= ops
->pi_disable(pcdev
, id
);
272 pcdev
->pi
[id
].admin_state_enabled
= 0;
273 mutex_unlock(&pcdev
->lock
);
278 static int _pse_pi_get_voltage(struct regulator_dev
*rdev
)
280 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
281 const struct pse_controller_ops
*ops
;
285 if (!ops
->pi_get_voltage
)
288 id
= rdev_get_id(rdev
);
289 return ops
->pi_get_voltage(pcdev
, id
);
292 static int pse_pi_get_voltage(struct regulator_dev
*rdev
)
294 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
297 mutex_lock(&pcdev
->lock
);
298 ret
= _pse_pi_get_voltage(rdev
);
299 mutex_unlock(&pcdev
->lock
);
304 static int pse_pi_get_current_limit(struct regulator_dev
*rdev
)
306 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
307 const struct pse_controller_ops
*ops
;
312 id
= rdev_get_id(rdev
);
313 if (!ops
->pi_get_pw_limit
|| !ops
->pi_get_voltage
)
316 mutex_lock(&pcdev
->lock
);
317 ret
= ops
->pi_get_pw_limit(pcdev
, id
);
322 ret
= pse_pi_get_voltage(rdev
);
324 dev_err(pcdev
->dev
, "Voltage null\n");
333 tmp_64
*= 1000000000ull;
334 /* uA = mW * 1000000000 / uV */
335 ret
= DIV_ROUND_CLOSEST_ULL(tmp_64
, uV
);
338 mutex_unlock(&pcdev
->lock
);
342 static int pse_pi_set_current_limit(struct regulator_dev
*rdev
, int min_uA
,
345 struct pse_controller_dev
*pcdev
= rdev_get_drvdata(rdev
);
346 const struct pse_controller_ops
*ops
;
351 if (!ops
->pi_set_pw_limit
|| !ops
->pi_get_voltage
)
354 if (max_uA
> MAX_PI_CURRENT
)
357 id
= rdev_get_id(rdev
);
358 mutex_lock(&pcdev
->lock
);
359 ret
= pse_pi_get_voltage(rdev
);
361 dev_err(pcdev
->dev
, "Voltage null\n");
370 /* mW = uA * uV / 1000000000 */
371 mW
= DIV_ROUND_CLOSEST_ULL(tmp_64
, 1000000000);
372 ret
= ops
->pi_set_pw_limit(pcdev
, id
, mW
);
374 mutex_unlock(&pcdev
->lock
);
379 static const struct regulator_ops pse_pi_ops
= {
380 .is_enabled
= pse_pi_is_enabled
,
381 .enable
= pse_pi_enable
,
382 .disable
= pse_pi_disable
,
383 .get_voltage
= pse_pi_get_voltage
,
384 .get_current_limit
= pse_pi_get_current_limit
,
385 .set_current_limit
= pse_pi_set_current_limit
,
389 devm_pse_pi_regulator_register(struct pse_controller_dev
*pcdev
,
392 struct regulator_init_data
*rinit_data
;
393 struct regulator_config rconfig
= {0};
394 struct regulator_desc
*rdesc
;
395 struct regulator_dev
*rdev
;
397 rinit_data
= devm_kzalloc(pcdev
->dev
, sizeof(*rinit_data
),
402 rdesc
= devm_kzalloc(pcdev
->dev
, sizeof(*rdesc
), GFP_KERNEL
);
406 /* Regulator descriptor id have to be the same as its associated
407 * PSE PI id for the well functioning of the PSE controls.
411 rdesc
->type
= REGULATOR_VOLTAGE
;
412 rdesc
->ops
= &pse_pi_ops
;
413 rdesc
->owner
= pcdev
->owner
;
415 rinit_data
->constraints
.valid_ops_mask
= REGULATOR_CHANGE_STATUS
;
417 if (pcdev
->ops
->pi_set_pw_limit
)
418 rinit_data
->constraints
.valid_ops_mask
|=
419 REGULATOR_CHANGE_CURRENT
;
421 rinit_data
->supply_regulator
= "vpwr";
423 rconfig
.dev
= pcdev
->dev
;
424 rconfig
.driver_data
= pcdev
;
425 rconfig
.init_data
= rinit_data
;
426 rconfig
.of_node
= pcdev
->pi
[id
].np
;
428 rdev
= devm_regulator_register(pcdev
->dev
, rdesc
, &rconfig
);
430 dev_err_probe(pcdev
->dev
, PTR_ERR(rdev
),
431 "Failed to register regulator\n");
432 return PTR_ERR(rdev
);
435 pcdev
->pi
[id
].rdev
= rdev
;
441 * pse_controller_register - register a PSE controller device
442 * @pcdev: a pointer to the initialized PSE controller device
444 * Return: 0 on success and failure value on error
446 int pse_controller_register(struct pse_controller_dev
*pcdev
)
451 mutex_init(&pcdev
->lock
);
452 INIT_LIST_HEAD(&pcdev
->pse_control_head
);
454 if (!pcdev
->nr_lines
)
457 if (!pcdev
->ops
->pi_get_admin_state
||
458 !pcdev
->ops
->pi_get_pw_status
) {
460 "Mandatory status report callbacks are missing");
464 ret
= of_load_pse_pis(pcdev
);
468 if (pcdev
->ops
->setup_pi_matrix
) {
469 ret
= pcdev
->ops
->setup_pi_matrix(pcdev
);
474 /* Each regulator name len is pcdev dev name + 7 char +
475 * int max digit number (10) + 1
477 reg_name_len
= strlen(dev_name(pcdev
->dev
)) + 18;
479 /* Register PI regulators */
480 for (i
= 0; i
< pcdev
->nr_lines
; i
++) {
483 /* Do not register regulator for PIs not described */
484 if (!pcdev
->no_of_pse_pi
&& !pcdev
->pi
[i
].np
)
487 reg_name
= devm_kzalloc(pcdev
->dev
, reg_name_len
, GFP_KERNEL
);
491 snprintf(reg_name
, reg_name_len
, "pse-%s_pi%d",
492 dev_name(pcdev
->dev
), i
);
494 ret
= devm_pse_pi_regulator_register(pcdev
, reg_name
, i
);
499 mutex_lock(&pse_list_mutex
);
500 list_add(&pcdev
->list
, &pse_controller_list
);
501 mutex_unlock(&pse_list_mutex
);
505 EXPORT_SYMBOL_GPL(pse_controller_register
);
508 * pse_controller_unregister - unregister a PSE controller device
509 * @pcdev: a pointer to the PSE controller device
511 void pse_controller_unregister(struct pse_controller_dev
*pcdev
)
513 pse_release_pis(pcdev
);
514 mutex_lock(&pse_list_mutex
);
515 list_del(&pcdev
->list
);
516 mutex_unlock(&pse_list_mutex
);
518 EXPORT_SYMBOL_GPL(pse_controller_unregister
);
520 static void devm_pse_controller_release(struct device
*dev
, void *res
)
522 pse_controller_unregister(*(struct pse_controller_dev
**)res
);
526 * devm_pse_controller_register - resource managed pse_controller_register()
527 * @dev: device that is registering this PSE controller
528 * @pcdev: a pointer to the initialized PSE controller device
530 * Managed pse_controller_register(). For PSE controllers registered by
531 * this function, pse_controller_unregister() is automatically called on
532 * driver detach. See pse_controller_register() for more information.
534 * Return: 0 on success and failure value on error
536 int devm_pse_controller_register(struct device
*dev
,
537 struct pse_controller_dev
*pcdev
)
539 struct pse_controller_dev
**pcdevp
;
542 pcdevp
= devres_alloc(devm_pse_controller_release
, sizeof(*pcdevp
),
547 ret
= pse_controller_register(pcdev
);
554 devres_add(dev
, pcdevp
);
558 EXPORT_SYMBOL_GPL(devm_pse_controller_register
);
560 /* PSE control section */
562 static void __pse_control_release(struct kref
*kref
)
564 struct pse_control
*psec
= container_of(kref
, struct pse_control
,
567 lockdep_assert_held(&pse_list_mutex
);
569 if (psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
)
570 regulator_disable(psec
->ps
);
571 devm_regulator_put(psec
->ps
);
573 module_put(psec
->pcdev
->owner
);
575 list_del(&psec
->list
);
579 static void __pse_control_put_internal(struct pse_control
*psec
)
581 lockdep_assert_held(&pse_list_mutex
);
583 kref_put(&psec
->refcnt
, __pse_control_release
);
587 * pse_control_put - free the PSE control
588 * @psec: PSE control pointer
590 void pse_control_put(struct pse_control
*psec
)
592 if (IS_ERR_OR_NULL(psec
))
595 mutex_lock(&pse_list_mutex
);
596 __pse_control_put_internal(psec
);
597 mutex_unlock(&pse_list_mutex
);
599 EXPORT_SYMBOL_GPL(pse_control_put
);
601 static struct pse_control
*
602 pse_control_get_internal(struct pse_controller_dev
*pcdev
, unsigned int index
)
604 struct pse_control
*psec
;
607 lockdep_assert_held(&pse_list_mutex
);
609 list_for_each_entry(psec
, &pcdev
->pse_control_head
, list
) {
610 if (psec
->id
== index
) {
611 kref_get(&psec
->refcnt
);
616 psec
= kzalloc(sizeof(*psec
), GFP_KERNEL
);
618 return ERR_PTR(-ENOMEM
);
620 if (!try_module_get(pcdev
->owner
)) {
625 psec
->ps
= devm_regulator_get_exclusive(pcdev
->dev
,
626 rdev_get_name(pcdev
->pi
[index
].rdev
));
627 if (IS_ERR(psec
->ps
)) {
628 ret
= PTR_ERR(psec
->ps
);
632 ret
= regulator_is_enabled(psec
->ps
);
636 pcdev
->pi
[index
].admin_state_enabled
= ret
;
639 list_add(&psec
->list
, &pcdev
->pse_control_head
);
641 kref_init(&psec
->refcnt
);
646 devm_regulator_put(psec
->ps
);
648 module_put(pcdev
->owner
);
656 * of_pse_match_pi - Find the PSE PI id matching the device node phandle
657 * @pcdev: a pointer to the PSE controller device
658 * @np: a pointer to the device node
660 * Return: id of the PSE PI, -EINVAL if not found
662 static int of_pse_match_pi(struct pse_controller_dev
*pcdev
,
663 struct device_node
*np
)
667 for (i
= 0; i
< pcdev
->nr_lines
; i
++) {
668 if (pcdev
->pi
[i
].np
== np
)
676 * psec_id_xlate - translate pse_spec to the PSE line number according
677 * to the number of pse-cells in case of no pse_pi node
678 * @pcdev: a pointer to the PSE controller device
679 * @pse_spec: PSE line specifier as found in the device tree
681 * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise.
683 static int psec_id_xlate(struct pse_controller_dev
*pcdev
,
684 const struct of_phandle_args
*pse_spec
)
686 if (!pcdev
->of_pse_n_cells
)
689 if (pcdev
->of_pse_n_cells
> 1 ||
690 pse_spec
->args
[0] >= pcdev
->nr_lines
)
693 return pse_spec
->args
[0];
696 struct pse_control
*of_pse_control_get(struct device_node
*node
)
698 struct pse_controller_dev
*r
, *pcdev
;
699 struct of_phandle_args args
;
700 struct pse_control
*psec
;
705 return ERR_PTR(-EINVAL
);
707 ret
= of_parse_phandle_with_args(node
, "pses", "#pse-cells", 0, &args
);
711 mutex_lock(&pse_list_mutex
);
713 list_for_each_entry(r
, &pse_controller_list
, list
) {
714 if (!r
->no_of_pse_pi
) {
715 ret
= of_pse_match_pi(r
, args
.np
);
721 } else if (args
.np
== r
->dev
->of_node
) {
728 psec
= ERR_PTR(-EPROBE_DEFER
);
732 if (WARN_ON(args
.args_count
!= pcdev
->of_pse_n_cells
)) {
733 psec
= ERR_PTR(-EINVAL
);
737 if (pcdev
->no_of_pse_pi
) {
738 psec_id
= psec_id_xlate(pcdev
, &args
);
740 psec
= ERR_PTR(psec_id
);
745 /* pse_list_mutex also protects the pcdev's pse_control list */
746 psec
= pse_control_get_internal(pcdev
, psec_id
);
749 mutex_unlock(&pse_list_mutex
);
750 of_node_put(args
.np
);
754 EXPORT_SYMBOL_GPL(of_pse_control_get
);
757 * pse_ethtool_get_status - get status of PSE control
758 * @psec: PSE control pointer
759 * @extack: extack for reporting useful error messages
760 * @status: struct to store PSE status
762 * Return: 0 on success and failure value on error
764 int pse_ethtool_get_status(struct pse_control
*psec
,
765 struct netlink_ext_ack
*extack
,
766 struct ethtool_pse_control_status
*status
)
768 struct pse_admin_state admin_state
= {0};
769 struct pse_pw_status pw_status
= {0};
770 const struct pse_controller_ops
*ops
;
771 struct pse_controller_dev
*pcdev
;
776 mutex_lock(&pcdev
->lock
);
777 ret
= ops
->pi_get_admin_state(pcdev
, psec
->id
, &admin_state
);
780 status
->podl_admin_state
= admin_state
.podl_admin_state
;
781 status
->c33_admin_state
= admin_state
.c33_admin_state
;
783 ret
= ops
->pi_get_pw_status(pcdev
, psec
->id
, &pw_status
);
786 status
->podl_pw_status
= pw_status
.podl_pw_status
;
787 status
->c33_pw_status
= pw_status
.c33_pw_status
;
789 if (ops
->pi_get_ext_state
) {
790 struct pse_ext_state_info ext_state_info
= {0};
792 ret
= ops
->pi_get_ext_state(pcdev
, psec
->id
,
797 memcpy(&status
->c33_ext_state_info
,
798 &ext_state_info
.c33_ext_state_info
,
799 sizeof(status
->c33_ext_state_info
));
802 if (ops
->pi_get_pw_class
) {
803 ret
= ops
->pi_get_pw_class(pcdev
, psec
->id
);
807 status
->c33_pw_class
= ret
;
810 if (ops
->pi_get_actual_pw
) {
811 ret
= ops
->pi_get_actual_pw(pcdev
, psec
->id
);
815 status
->c33_actual_pw
= ret
;
818 if (ops
->pi_get_pw_limit
) {
819 ret
= ops
->pi_get_pw_limit(pcdev
, psec
->id
);
823 status
->c33_avail_pw_limit
= ret
;
826 if (ops
->pi_get_pw_limit_ranges
) {
827 struct pse_pw_limit_ranges pw_limit_ranges
= {0};
829 ret
= ops
->pi_get_pw_limit_ranges(pcdev
, psec
->id
,
834 status
->c33_pw_limit_ranges
=
835 pw_limit_ranges
.c33_pw_limit_ranges
;
836 status
->c33_pw_limit_nb_ranges
= ret
;
839 mutex_unlock(&psec
->pcdev
->lock
);
842 EXPORT_SYMBOL_GPL(pse_ethtool_get_status
);
844 static int pse_ethtool_c33_set_config(struct pse_control
*psec
,
845 const struct pse_control_config
*config
)
849 /* Look at admin_state_enabled status to not call regulator_enable
850 * or regulator_disable twice creating a regulator counter mismatch
852 switch (config
->c33_admin_control
) {
853 case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED
:
854 /* We could have mismatch between admin_state_enabled and
855 * state reported by regulator_is_enabled. This can occur when
856 * the PI is forcibly turn off by the controller. Call
857 * regulator_disable on that case to fix the counters state.
859 if (psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
&&
860 !regulator_is_enabled(psec
->ps
)) {
861 err
= regulator_disable(psec
->ps
);
865 if (!psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
)
866 err
= regulator_enable(psec
->ps
);
868 case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED
:
869 if (psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
)
870 err
= regulator_disable(psec
->ps
);
879 static int pse_ethtool_podl_set_config(struct pse_control
*psec
,
880 const struct pse_control_config
*config
)
884 /* Look at admin_state_enabled status to not call regulator_enable
885 * or regulator_disable twice creating a regulator counter mismatch
887 switch (config
->podl_admin_control
) {
888 case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED
:
889 if (!psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
)
890 err
= regulator_enable(psec
->ps
);
892 case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED
:
893 if (psec
->pcdev
->pi
[psec
->id
].admin_state_enabled
)
894 err
= regulator_disable(psec
->ps
);
904 * pse_ethtool_set_config - set PSE control configuration
905 * @psec: PSE control pointer
906 * @extack: extack for reporting useful error messages
907 * @config: Configuration of the test to run
909 * Return: 0 on success and failure value on error
911 int pse_ethtool_set_config(struct pse_control
*psec
,
912 struct netlink_ext_ack
*extack
,
913 const struct pse_control_config
*config
)
917 if (pse_has_c33(psec
) && config
->c33_admin_control
) {
918 err
= pse_ethtool_c33_set_config(psec
, config
);
923 if (pse_has_podl(psec
) && config
->podl_admin_control
)
924 err
= pse_ethtool_podl_set_config(psec
, config
);
928 EXPORT_SYMBOL_GPL(pse_ethtool_set_config
);
931 * pse_ethtool_set_pw_limit - set PSE control power limit
932 * @psec: PSE control pointer
933 * @extack: extack for reporting useful error messages
934 * @pw_limit: power limit value in mW
936 * Return: 0 on success and failure value on error
938 int pse_ethtool_set_pw_limit(struct pse_control
*psec
,
939 struct netlink_ext_ack
*extack
,
940 const unsigned int pw_limit
)
945 if (pw_limit
> MAX_PI_PW
)
948 ret
= regulator_get_voltage(psec
->ps
);
950 NL_SET_ERR_MSG(extack
,
951 "Can't calculate the current, PSE voltage read is 0");
955 NL_SET_ERR_MSG(extack
,
956 "Error reading PSE voltage");
962 tmp_64
*= 1000000000ull;
963 /* uA = mW * 1000000000 / uV */
964 uA
= DIV_ROUND_CLOSEST_ULL(tmp_64
, uV
);
966 return regulator_set_current_limit(psec
->ps
, 0, uA
);
968 EXPORT_SYMBOL_GPL(pse_ethtool_set_pw_limit
);
970 bool pse_has_podl(struct pse_control
*psec
)
972 return psec
->pcdev
->types
& ETHTOOL_PSE_PODL
;
974 EXPORT_SYMBOL_GPL(pse_has_podl
);
976 bool pse_has_c33(struct pse_control
*psec
)
978 return psec
->pcdev
->types
& ETHTOOL_PSE_C33
;
980 EXPORT_SYMBOL_GPL(pse_has_c33
);