21
21
#include <linux/workqueue.h>
22
22
#include <linux/leds-pca9532.h>
23
23
#include <linux/gpio.h>
24
+ #include <linux/of.h>
25
+ #include <linux/of_device.h>
24
26
25
27
/* m = num_leds*/
26
28
#define PCA9532_REG_INPUT (i ) ((i) >> 3)
@@ -86,9 +88,22 @@ static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
86
88
},
87
89
};
88
90
91
+ #ifdef CONFIG_OF
92
+ static const struct of_device_id of_pca9532_leds_match [] = {
93
+ { .compatible = "nxp,pca9530" , .data = (void * )pca9530 },
94
+ { .compatible = "nxp,pca9531" , .data = (void * )pca9531 },
95
+ { .compatible = "nxp,pca9532" , .data = (void * )pca9532 },
96
+ { .compatible = "nxp,pca9533" , .data = (void * )pca9533 },
97
+ {},
98
+ };
99
+
100
+ MODULE_DEVICE_TABLE (of , of_pca9532_leds_match );
101
+ #endif
102
+
89
103
static struct i2c_driver pca9532_driver = {
90
104
.driver = {
91
105
.name = "leds-pca953x" ,
106
+ .of_match_table = of_match_ptr (of_pca9532_leds_match ),
92
107
},
93
108
.probe = pca9532_probe ,
94
109
.remove = pca9532_remove ,
@@ -354,6 +369,7 @@ static int pca9532_configure(struct i2c_client *client,
354
369
led -> state = pled -> state ;
355
370
led -> name = pled -> name ;
356
371
led -> ldev .name = led -> name ;
372
+ led -> ldev .default_trigger = led -> default_trigger ;
357
373
led -> ldev .brightness = LED_OFF ;
358
374
led -> ldev .brightness_set_blocking =
359
375
pca9532_set_brightness ;
@@ -432,15 +448,66 @@ static int pca9532_configure(struct i2c_client *client,
432
448
return err ;
433
449
}
434
450
451
+ static struct pca9532_platform_data *
452
+ pca9532_of_populate_pdata (struct device * dev , struct device_node * np )
453
+ {
454
+ struct pca9532_platform_data * pdata ;
455
+ struct device_node * child ;
456
+ const struct of_device_id * match ;
457
+ int devid , maxleds ;
458
+ int i = 0 ;
459
+
460
+ match = of_match_device (of_pca9532_leds_match , dev );
461
+ if (!match )
462
+ return ERR_PTR (- ENODEV );
463
+
464
+ devid = (int )(uintptr_t )match -> data ;
465
+ maxleds = pca9532_chip_info_tbl [devid ].num_leds ;
466
+
467
+ pdata = devm_kzalloc (dev , sizeof (* pdata ), GFP_KERNEL );
468
+ if (!pdata )
469
+ return ERR_PTR (- ENOMEM );
470
+
471
+ for_each_child_of_node (np , child ) {
472
+ if (of_property_read_string (child , "label" ,
473
+ & pdata -> leds [i ].name ))
474
+ pdata -> leds [i ].name = child -> name ;
475
+ of_property_read_u32 (child , "type" , & pdata -> leds [i ].type );
476
+ of_property_read_string (child , "linux,default-trigger" ,
477
+ & pdata -> leds [i ].default_trigger );
478
+ if (++ i >= maxleds ) {
479
+ of_node_put (child );
480
+ break ;
481
+ }
482
+ }
483
+
484
+ return pdata ;
485
+ }
486
+
435
487
static int pca9532_probe (struct i2c_client * client ,
436
488
const struct i2c_device_id * id )
437
489
{
490
+ int devid ;
438
491
struct pca9532_data * data = i2c_get_clientdata (client );
439
492
struct pca9532_platform_data * pca9532_pdata =
440
493
dev_get_platdata (& client -> dev );
441
-
442
- if (!pca9532_pdata )
443
- return - EIO ;
494
+ struct device_node * np = client -> dev .of_node ;
495
+
496
+ if (!pca9532_pdata ) {
497
+ if (np ) {
498
+ pca9532_pdata =
499
+ pca9532_of_populate_pdata (& client -> dev , np );
500
+ if (IS_ERR (pca9532_pdata ))
501
+ return PTR_ERR (pca9532_pdata );
502
+ } else {
503
+ dev_err (& client -> dev , "no platform data\n" );
504
+ return - EINVAL ;
505
+ }
506
+ devid = (int )(uintptr_t )of_match_device (
507
+ of_pca9532_leds_match , & client -> dev )-> data ;
508
+ } else {
509
+ devid = id -> driver_data ;
510
+ }
444
511
445
512
if (!i2c_check_functionality (client -> adapter ,
446
513
I2C_FUNC_SMBUS_BYTE_DATA ))
@@ -450,7 +517,7 @@ static int pca9532_probe(struct i2c_client *client,
450
517
if (!data )
451
518
return - ENOMEM ;
452
519
453
- data -> chip_info = & pca9532_chip_info_tbl [id -> driver_data ];
520
+ data -> chip_info = & pca9532_chip_info_tbl [devid ];
454
521
455
522
dev_info (& client -> dev , "setting platform data\n" );
456
523
i2c_set_clientdata (client , data );
0 commit comments