3232#include <linux/property.h>
3333#include <linux/slab.h>
3434#include <linux/of.h>
35- #include <linux/platform_data/leds-pca963x.h>
3635
3736/* LED select registers determine the source that drives LED outputs */
3837#define PCA963X_LED_OFF 0x0 /* LED driver off */
@@ -102,7 +101,6 @@ struct pca963x_led {
102101 struct pca963x * chip ;
103102 struct led_classdev led_cdev ;
104103 int led_num ; /* 0 .. 15 potentially */
105- char name [32 ];
106104 u8 gdc ;
107105 u8 gfrq ;
108106};
@@ -284,72 +282,85 @@ static int pca963x_blink_set(struct led_classdev *led_cdev,
284282 return 0 ;
285283}
286284
287- static struct pca963x_platform_data *
288- pca963x_get_pdata ( struct device * dev , struct pca963x_chipdef * chipdef )
285+ static int pca963x_register_leds ( struct i2c_client * client ,
286+ struct pca963x * chip )
289287{
290- struct pca963x_platform_data * pdata ;
291- struct led_info * pca963x_leds ;
288+ struct pca963x_chipdef * chipdef = chip -> chipdef ;
289+ struct pca963x_led * led = chip -> leds ;
290+ struct device * dev = & client -> dev ;
292291 struct fwnode_handle * child ;
293- int count ;
292+ const char * name ;
293+ char label [64 ];
294+ bool hw_blink ;
295+ s32 mode2 ;
296+ u32 reg ;
297+ int ret ;
294298
295- count = device_get_child_node_count ( dev );
296- if (! count || count > chipdef -> n_leds )
297- return ERR_PTR ( - ENODEV ) ;
299+ if ( device_property_read_u32 ( dev , "nxp,period-scale" ,
300+ & chipdef -> scaling ) )
301+ chipdef -> scaling = 1000 ;
298302
299- pca963x_leds = devm_kcalloc (dev , chipdef -> n_leds ,
300- sizeof (struct led_info ), GFP_KERNEL );
301- if (!pca963x_leds )
302- return ERR_PTR (- ENOMEM );
303+ hw_blink = device_property_read_bool (dev , "nxp,hw-blink" );
303304
304- device_for_each_child_node (dev , child ) {
305- struct led_info led = {};
306- u32 reg ;
307- int res ;
305+ mode2 = i2c_smbus_read_byte_data (client , PCA963X_MODE2 );
306+ if (mode2 < 0 )
307+ return mode2 ;
308308
309- res = fwnode_property_read_u32 (child , "reg" , & reg );
310- if ((res != 0 ) || (reg >= chipdef -> n_leds ))
311- continue ;
309+ /* default to open-drain unless totem pole (push-pull) is specified */
310+ if (device_property_read_bool (dev , "nxp,totem-pole" ))
311+ mode2 |= PCA963X_MODE2_OUTDRV ;
312+ else
313+ mode2 &= ~PCA963X_MODE2_OUTDRV ;
312314
313- res = fwnode_property_read_string (child , "label" , & led .name );
314- if ((res != 0 ) && is_of_node (child ))
315- led .name = to_of_node (child )-> name ;
315+ /* default to non-inverted output, unless inverted is specified */
316+ if (device_property_read_bool (dev , "nxp,inverted-out" ))
317+ mode2 |= PCA963X_MODE2_INVRT ;
318+ else
319+ mode2 &= ~PCA963X_MODE2_INVRT ;
316320
317- fwnode_property_read_string (child , "linux,default-trigger" ,
318- & led .default_trigger );
321+ ret = i2c_smbus_write_byte_data (client , PCA963X_MODE2 , mode2 );
322+ if (ret < 0 )
323+ return ret ;
319324
320- pca963x_leds [reg ] = led ;
321- }
322- pdata = devm_kzalloc (dev , sizeof (struct pca963x_platform_data ),
323- GFP_KERNEL );
324- if (!pdata )
325- return ERR_PTR (- ENOMEM );
325+ device_for_each_child_node (dev , child ) {
326+ ret = fwnode_property_read_u32 (child , "reg" , & reg );
327+ if (ret || reg >= chipdef -> n_leds ) {
328+ dev_err (dev , "Invalid 'reg' property for node %pfw\n" ,
329+ child );
330+ ret = - EINVAL ;
331+ goto err ;
332+ }
326333
327- pdata -> leds .leds = pca963x_leds ;
328- pdata -> leds .num_leds = chipdef -> n_leds ;
334+ ret = fwnode_property_read_string (child , "label" , & name );
335+ if (!fwnode_property_read_string (child , "label" , & name ))
336+ snprintf (label , sizeof (label ), "pca963x:%s" , name );
337+ else
338+ snprintf (label , sizeof (label ), "pca963x::" );
329339
330- /* default to open-drain unless totem pole (push-pull) is specified */
331- if (device_property_read_bool (dev , "nxp,totem-pole" ))
332- pdata -> outdrv = PCA963X_TOTEM_POLE ;
333- else
334- pdata -> outdrv = PCA963X_OPEN_DRAIN ;
340+ fwnode_property_read_string (child , "linux,default-trigger" ,
341+ & led -> led_cdev .default_trigger );
335342
336- /* default to software blinking unless hardware blinking is specified */
337- if (device_property_read_bool (dev , "nxp,hw-blink" ))
338- pdata -> blink_type = PCA963X_HW_BLINK ;
339- else
340- pdata -> blink_type = PCA963X_SW_BLINK ;
343+ led -> led_num = reg ;
344+ led -> chip = chip ;
345+ led -> led_cdev .name = label ;
346+ led -> led_cdev .brightness_set_blocking = pca963x_led_set ;
347+ if (hw_blink )
348+ led -> led_cdev .blink_set = pca963x_blink_set ;
341349
342- if (device_property_read_u32 (dev , "nxp,period-scale" ,
343- & chipdef -> scaling ))
344- chipdef -> scaling = 1000 ;
350+ ret = devm_led_classdev_register (dev , & led -> led_cdev );
351+ if (ret ) {
352+ dev_err (dev , "Failed to register LED for node %pfw\n" ,
353+ child );
354+ goto err ;
355+ }
345356
346- /* default to non-inverted output, unless inverted is specified */
347- if (device_property_read_bool (dev , "nxp,inverted-out" ))
348- pdata -> dir = PCA963X_INVERTED ;
349- else
350- pdata -> dir = PCA963X_NORMAL ;
357+ ++ led ;
358+ }
351359
352- return pdata ;
360+ return 0 ;
361+ err :
362+ fwnode_handle_put (child );
363+ return ret ;
353364}
354365
355366static const struct of_device_id of_pca963x_match [] = {
@@ -366,30 +377,19 @@ static int pca963x_probe(struct i2c_client *client,
366377{
367378 struct device * dev = & client -> dev ;
368379 struct pca963x_chipdef * chipdef ;
369- struct pca963x_platform_data * pdata ;
370380 struct pca963x * chip ;
371- int i , err ;
381+ int i , count ;
372382
373383 chipdef = & pca963x_chipdefs [id -> driver_data ];
374- pdata = dev_get_platdata (dev );
375384
376- if (!pdata ) {
377- pdata = pca963x_get_pdata (dev , chipdef );
378- if (IS_ERR (pdata )) {
379- dev_warn (dev , "could not parse configuration\n" );
380- pdata = NULL ;
381- }
382- }
383-
384- if (pdata && (pdata -> leds .num_leds < 1 ||
385- pdata -> leds .num_leds > chipdef -> n_leds )) {
386- dev_err (dev , "board info must claim 1-%d LEDs" ,
387- chipdef -> n_leds );
385+ count = device_get_child_node_count (dev );
386+ if (!count || count > chipdef -> n_leds ) {
387+ dev_err (dev , "Node %pfw must define between 1 and %d LEDs\n" ,
388+ dev_fwnode (dev ), chipdef -> n_leds );
388389 return - EINVAL ;
389390 }
390391
391- chip = devm_kzalloc (dev , struct_size (chip , leds , chipdef -> n_leds ),
392- GFP_KERNEL );
392+ chip = devm_kzalloc (dev , struct_size (chip , leds , count ), GFP_KERNEL );
393393 if (!chip )
394394 return - ENOMEM ;
395395
@@ -403,56 +403,10 @@ static int pca963x_probe(struct i2c_client *client,
403403 for (i = 0 ; i < chipdef -> n_leds / 4 ; i ++ )
404404 i2c_smbus_write_byte_data (client , chipdef -> ledout_base + i , 0x00 );
405405
406- for (i = 0 ; i < chipdef -> n_leds ; i ++ ) {
407- struct pca963x_led * led = & chip -> leds [i ];
408-
409- led -> led_num = i ;
410- led -> chip = chip ;
411-
412- /* Platform data can specify LED names and default triggers */
413- if (pdata && i < pdata -> leds .num_leds ) {
414- if (pdata -> leds .leds [i ].name )
415- snprintf (led -> name ,
416- sizeof (led -> name ), "pca963x:%s" ,
417- pdata -> leds .leds [i ].name );
418- if (pdata -> leds .leds [i ].default_trigger )
419- led -> led_cdev .default_trigger =
420- pdata -> leds .leds [i ].default_trigger ;
421- }
422- if (!pdata || i >= pdata -> leds .num_leds ||
423- !pdata -> leds .leds [i ].name )
424- snprintf (led -> name , sizeof (led -> name ),
425- "pca963x:%d:%.2x:%d" , client -> adapter -> nr ,
426- client -> addr , i );
427-
428- led -> led_cdev .name = led -> name ;
429- led -> led_cdev .brightness_set_blocking = pca963x_led_set ;
430-
431- if (pdata && pdata -> blink_type == PCA963X_HW_BLINK )
432- led -> led_cdev .blink_set = pca963x_blink_set ;
433-
434- err = devm_led_classdev_register (dev , & led -> led_cdev );
435- if (err < 0 )
436- return err ;
437- }
438-
439406 /* Disable LED all-call address, and power down initially */
440407 i2c_smbus_write_byte_data (client , PCA963X_MODE1 , BIT (4 ));
441408
442- if (pdata ) {
443- u8 mode2 = i2c_smbus_read_byte_data (client , PCA963X_MODE2 );
444- /* Configure output: open-drain or totem pole (push-pull) */
445- if (pdata -> outdrv == PCA963X_OPEN_DRAIN )
446- mode2 &= ~PCA963X_MODE2_OUTDRV ;
447- else
448- mode2 |= PCA963X_MODE2_OUTDRV ;
449- /* Configure direction: normal or inverted */
450- if (pdata -> dir == PCA963X_INVERTED )
451- mode2 |= PCA963X_MODE2_INVRT ;
452- i2c_smbus_write_byte_data (client , PCA963X_MODE2 , mode2 );
453- }
454-
455- return 0 ;
409+ return pca963x_register_leds (client , chip );
456410}
457411
458412static struct i2c_driver pca963x_driver = {
0 commit comments