88#include <linux/device.h>
99#include <linux/of.h>
1010#include <linux/pse-pd/pse.h>
11+ #include <linux/regulator/driver.h>
12+ #include <linux/regulator/machine.h>
1113
1214static DEFINE_MUTEX (pse_list_mutex );
1315static LIST_HEAD (pse_controller_list );
@@ -16,12 +18,14 @@ static LIST_HEAD(pse_controller_list);
1618 * struct pse_control - a PSE control
1719 * @pcdev: a pointer to the PSE controller device
1820 * this PSE control belongs to
21+ * @ps: PSE PI supply of the PSE control
1922 * @list: list entry for the pcdev's PSE controller list
2023 * @id: ID of the PSE line in the PSE controller device
2124 * @refcnt: Number of gets of this pse_control
2225 */
2326struct pse_control {
2427 struct pse_controller_dev * pcdev ;
28+ struct regulator * ps ;
2529 struct list_head list ;
2630 unsigned int id ;
2731 struct kref refcnt ;
@@ -132,19 +136,17 @@ static int of_load_pse_pis(struct pse_controller_dev *pcdev)
132136 if (!np )
133137 return - ENODEV ;
134138
139+ pcdev -> pi = kcalloc (pcdev -> nr_lines , sizeof (* pcdev -> pi ), GFP_KERNEL );
140+ if (!pcdev -> pi )
141+ return - ENOMEM ;
142+
135143 pis = of_get_child_by_name (np , "pse-pis" );
136144 if (!pis ) {
137145 /* no description of PSE PIs */
138146 pcdev -> no_of_pse_pi = true;
139147 return 0 ;
140148 }
141149
142- pcdev -> pi = kcalloc (pcdev -> nr_lines , sizeof (* pcdev -> pi ), GFP_KERNEL );
143- if (!pcdev -> pi ) {
144- of_node_put (pis );
145- return - ENOMEM ;
146- }
147-
148150 for_each_child_of_node (pis , node ) {
149151 struct pse_pi pi = {0 };
150152 u32 id ;
@@ -205,13 +207,124 @@ static int of_load_pse_pis(struct pse_controller_dev *pcdev)
205207 return ret ;
206208}
207209
210+ static int pse_pi_is_enabled (struct regulator_dev * rdev )
211+ {
212+ struct pse_controller_dev * pcdev = rdev_get_drvdata (rdev );
213+ const struct pse_controller_ops * ops ;
214+ int id , ret ;
215+
216+ ops = pcdev -> ops ;
217+ if (!ops -> pi_is_enabled )
218+ return - EOPNOTSUPP ;
219+
220+ id = rdev_get_id (rdev );
221+ mutex_lock (& pcdev -> lock );
222+ ret = ops -> pi_is_enabled (pcdev , id );
223+ mutex_unlock (& pcdev -> lock );
224+
225+ return ret ;
226+ }
227+
228+ static int pse_pi_enable (struct regulator_dev * rdev )
229+ {
230+ struct pse_controller_dev * pcdev = rdev_get_drvdata (rdev );
231+ const struct pse_controller_ops * ops ;
232+ int id , ret ;
233+
234+ ops = pcdev -> ops ;
235+ if (!ops -> pi_enable )
236+ return - EOPNOTSUPP ;
237+
238+ id = rdev_get_id (rdev );
239+ mutex_lock (& pcdev -> lock );
240+ ret = ops -> pi_enable (pcdev , id );
241+ if (!ret )
242+ pcdev -> pi [id ].admin_state_enabled = 1 ;
243+ mutex_unlock (& pcdev -> lock );
244+
245+ return ret ;
246+ }
247+
248+ static int pse_pi_disable (struct regulator_dev * rdev )
249+ {
250+ struct pse_controller_dev * pcdev = rdev_get_drvdata (rdev );
251+ const struct pse_controller_ops * ops ;
252+ int id , ret ;
253+
254+ ops = pcdev -> ops ;
255+ if (!ops -> pi_disable )
256+ return - EOPNOTSUPP ;
257+
258+ id = rdev_get_id (rdev );
259+ mutex_lock (& pcdev -> lock );
260+ ret = ops -> pi_disable (pcdev , id );
261+ if (!ret )
262+ pcdev -> pi [id ].admin_state_enabled = 0 ;
263+ mutex_unlock (& pcdev -> lock );
264+
265+ return ret ;
266+ }
267+
268+ static const struct regulator_ops pse_pi_ops = {
269+ .is_enabled = pse_pi_is_enabled ,
270+ .enable = pse_pi_enable ,
271+ .disable = pse_pi_disable ,
272+ };
273+
274+ static int
275+ devm_pse_pi_regulator_register (struct pse_controller_dev * pcdev ,
276+ char * name , int id )
277+ {
278+ struct regulator_init_data * rinit_data ;
279+ struct regulator_config rconfig = {0 };
280+ struct regulator_desc * rdesc ;
281+ struct regulator_dev * rdev ;
282+
283+ rinit_data = devm_kzalloc (pcdev -> dev , sizeof (* rinit_data ),
284+ GFP_KERNEL );
285+ if (!rinit_data )
286+ return - ENOMEM ;
287+
288+ rdesc = devm_kzalloc (pcdev -> dev , sizeof (* rdesc ), GFP_KERNEL );
289+ if (!rdesc )
290+ return - ENOMEM ;
291+
292+ /* Regulator descriptor id have to be the same as its associated
293+ * PSE PI id for the well functioning of the PSE controls.
294+ */
295+ rdesc -> id = id ;
296+ rdesc -> name = name ;
297+ rdesc -> type = REGULATOR_CURRENT ;
298+ rdesc -> ops = & pse_pi_ops ;
299+ rdesc -> owner = pcdev -> owner ;
300+
301+ rinit_data -> constraints .valid_ops_mask = REGULATOR_CHANGE_STATUS ;
302+ rinit_data -> supply_regulator = "vpwr" ;
303+
304+ rconfig .dev = pcdev -> dev ;
305+ rconfig .driver_data = pcdev ;
306+ rconfig .init_data = rinit_data ;
307+
308+ rdev = devm_regulator_register (pcdev -> dev , rdesc , & rconfig );
309+ if (IS_ERR (rdev )) {
310+ dev_err_probe (pcdev -> dev , PTR_ERR (rdev ),
311+ "Failed to register regulator\n" );
312+ return PTR_ERR (rdev );
313+ }
314+
315+ pcdev -> pi [id ].rdev = rdev ;
316+
317+ return 0 ;
318+ }
319+
208320/**
209321 * pse_controller_register - register a PSE controller device
210322 * @pcdev: a pointer to the initialized PSE controller device
211323 */
212324int pse_controller_register (struct pse_controller_dev * pcdev )
213325{
214- int ret ;
326+ size_t reg_name_len ;
327+ int ret , i ;
215328
216329 mutex_init (& pcdev -> lock );
217330 INIT_LIST_HEAD (& pcdev -> pse_control_head );
@@ -229,6 +342,31 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
229342 return ret ;
230343 }
231344
345+ /* Each regulator name len is pcdev dev name + 7 char +
346+ * int max digit number (10) + 1
347+ */
348+ reg_name_len = strlen (dev_name (pcdev -> dev )) + 18 ;
349+
350+ /* Register PI regulators */
351+ for (i = 0 ; i < pcdev -> nr_lines ; i ++ ) {
352+ char * reg_name ;
353+
354+ /* Do not register regulator for PIs not described */
355+ if (!pcdev -> no_of_pse_pi && !pcdev -> pi [i ].np )
356+ continue ;
357+
358+ reg_name = devm_kzalloc (pcdev -> dev , reg_name_len , GFP_KERNEL );
359+ if (!reg_name )
360+ return - ENOMEM ;
361+
362+ snprintf (reg_name , reg_name_len , "pse-%s_pi%d" ,
363+ dev_name (pcdev -> dev ), i );
364+
365+ ret = devm_pse_pi_regulator_register (pcdev , reg_name , i );
366+ if (ret )
367+ return ret ;
368+ }
369+
232370 mutex_lock (& pse_list_mutex );
233371 list_add (& pcdev -> list , & pse_controller_list );
234372 mutex_unlock (& pse_list_mutex );
@@ -297,6 +435,10 @@ static void __pse_control_release(struct kref *kref)
297435
298436 lockdep_assert_held (& pse_list_mutex );
299437
438+ if (psec -> pcdev -> pi [psec -> id ].admin_state_enabled )
439+ regulator_disable (psec -> ps );
440+ devm_regulator_put (psec -> ps );
441+
300442 module_put (psec -> pcdev -> owner );
301443
302444 list_del (& psec -> list );
@@ -329,6 +471,7 @@ static struct pse_control *
329471pse_control_get_internal (struct pse_controller_dev * pcdev , unsigned int index )
330472{
331473 struct pse_control * psec ;
474+ int ret ;
332475
333476 lockdep_assert_held (& pse_list_mutex );
334477
@@ -344,16 +487,38 @@ pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index)
344487 return ERR_PTR (- ENOMEM );
345488
346489 if (!try_module_get (pcdev -> owner )) {
347- kfree ( psec ) ;
348- return ERR_PTR ( - ENODEV ) ;
490+ ret = - ENODEV ;
491+ goto free_psec ;
349492 }
350493
494+ psec -> ps = devm_regulator_get_exclusive (pcdev -> dev ,
495+ rdev_get_name (pcdev -> pi [index ].rdev ));
496+ if (IS_ERR (psec -> ps )) {
497+ ret = PTR_ERR (psec -> ps );
498+ goto put_module ;
499+ }
500+
501+ ret = regulator_is_enabled (psec -> ps );
502+ if (ret < 0 )
503+ goto regulator_put ;
504+
505+ pcdev -> pi [index ].admin_state_enabled = ret ;
506+
351507 psec -> pcdev = pcdev ;
352508 list_add (& psec -> list , & pcdev -> pse_control_head );
353509 psec -> id = index ;
354510 kref_init (& psec -> refcnt );
355511
356512 return psec ;
513+
514+ regulator_put :
515+ devm_regulator_put (psec -> ps );
516+ put_module :
517+ module_put (pcdev -> owner );
518+ free_psec :
519+ kfree (psec );
520+
521+ return ERR_PTR (ret );
357522}
358523
359524/**
@@ -486,6 +651,54 @@ int pse_ethtool_get_status(struct pse_control *psec,
486651}
487652EXPORT_SYMBOL_GPL (pse_ethtool_get_status );
488653
654+ static int pse_ethtool_c33_set_config (struct pse_control * psec ,
655+ const struct pse_control_config * config )
656+ {
657+ int err = 0 ;
658+
659+ /* Look at admin_state_enabled status to not call regulator_enable
660+ * or regulator_disable twice creating a regulator counter mismatch
661+ */
662+ switch (config -> c33_admin_control ) {
663+ case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED :
664+ if (!psec -> pcdev -> pi [psec -> id ].admin_state_enabled )
665+ err = regulator_enable (psec -> ps );
666+ break ;
667+ case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED :
668+ if (psec -> pcdev -> pi [psec -> id ].admin_state_enabled )
669+ err = regulator_disable (psec -> ps );
670+ break ;
671+ default :
672+ err = - EOPNOTSUPP ;
673+ }
674+
675+ return err ;
676+ }
677+
678+ static int pse_ethtool_podl_set_config (struct pse_control * psec ,
679+ const struct pse_control_config * config )
680+ {
681+ int err = 0 ;
682+
683+ /* Look at admin_state_enabled status to not call regulator_enable
684+ * or regulator_disable twice creating a regulator counter mismatch
685+ */
686+ switch (config -> podl_admin_control ) {
687+ case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED :
688+ if (!psec -> pcdev -> pi [psec -> id ].admin_state_enabled )
689+ err = regulator_enable (psec -> ps );
690+ break ;
691+ case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED :
692+ if (psec -> pcdev -> pi [psec -> id ].admin_state_enabled )
693+ err = regulator_disable (psec -> ps );
694+ break ;
695+ default :
696+ err = - EOPNOTSUPP ;
697+ }
698+
699+ return err ;
700+ }
701+
489702/**
490703 * pse_ethtool_set_config - set PSE control configuration
491704 * @psec: PSE control pointer
@@ -496,20 +709,16 @@ int pse_ethtool_set_config(struct pse_control *psec,
496709 struct netlink_ext_ack * extack ,
497710 const struct pse_control_config * config )
498711{
499- const struct pse_controller_ops * ops ;
500- int err ;
501-
502- ops = psec -> pcdev -> ops ;
712+ int err = 0 ;
503713
504- if (! ops -> ethtool_set_config ) {
505- NL_SET_ERR_MSG ( extack ,
506- "PSE driver does not configuration" );
507- return - EOPNOTSUPP ;
714+ if (pse_has_c33 ( psec ) ) {
715+ err = pse_ethtool_c33_set_config ( psec , config );
716+ if ( err )
717+ return err ;
508718 }
509719
510- mutex_lock (& psec -> pcdev -> lock );
511- err = ops -> ethtool_set_config (psec -> pcdev , psec -> id , extack , config );
512- mutex_unlock (& psec -> pcdev -> lock );
720+ if (pse_has_podl (psec ))
721+ err = pse_ethtool_podl_set_config (psec , config );
513722
514723 return err ;
515724}
0 commit comments