2 * Copyright 2011 bct electronic GmbH
3 * Copyright 2013 Qtechnology/AS
5 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
6 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
8 * Based on leds-pca955x.c
10 * This file is subject to the terms and conditions of version 2 of
11 * the GNU General Public License. See the file COPYING in the main
12 * directory of this archive for more details.
14 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
15 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
17 * Note that hardware blinking violates the leds infrastructure driver
18 * interface since the hardware only supports blinking all LEDs with the
19 * same delay_on/delay_off rates. That is, only the LEDs that are set to
20 * blink will actually blink but all LEDs that are set to blink will blink
21 * in identical fashion. The delay_on/delay_off values of the last LED
22 * that is set to blink will be used for all of the blinking LEDs.
23 * Hardware blinking is disabled by default but can be enabled by setting
24 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
25 * or by adding the 'nxp,hw-blink' property to the DTS.
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/string.h>
31 #include <linux/ctype.h>
32 #include <linux/leds.h>
33 #include <linux/err.h>
34 #include <linux/i2c.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
38 #include <linux/platform_data/leds-pca963x.h>
40 /* LED select registers determine the source that drives LED outputs */
41 #define PCA963X_LED_OFF 0x0 /* LED driver off */
42 #define PCA963X_LED_ON 0x1 /* LED driver on */
43 #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
44 #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
46 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
48 #define PCA963X_MODE1 0x00
49 #define PCA963X_MODE2 0x01
50 #define PCA963X_PWM_BASE 0x02
58 struct pca963x_chipdef
{
65 static struct pca963x_chipdef pca963x_chipdefs
[] = {
86 /* Total blink period in milliseconds */
87 #define PCA963X_BLINK_PERIOD_MIN 42
88 #define PCA963X_BLINK_PERIOD_MAX 10667
90 static const struct i2c_device_id pca963x_id
[] = {
91 { "pca9632", pca9633
},
92 { "pca9633", pca9633
},
93 { "pca9634", pca9634
},
94 { "pca9635", pca9635
},
97 MODULE_DEVICE_TABLE(i2c
, pca963x_id
);
107 struct pca963x_chipdef
*chipdef
;
109 struct i2c_client
*client
;
110 struct pca963x_led
*leds
;
114 struct pca963x
*chip
;
115 struct work_struct work
;
116 enum led_brightness brightness
;
117 struct led_classdev led_cdev
;
118 int led_num
; /* 0 .. 15 potentially */
119 enum pca963x_cmd cmd
;
125 static void pca963x_brightness_work(struct pca963x_led
*pca963x
)
127 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
128 + (pca963x
->led_num
/ 4);
130 int shift
= 2 * (pca963x
->led_num
% 4);
131 u8 mask
= 0x3 << shift
;
133 mutex_lock(&pca963x
->chip
->mutex
);
134 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
135 switch (pca963x
->brightness
) {
137 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
138 (ledout
& ~mask
) | (PCA963X_LED_ON
<< shift
));
141 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
145 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
146 PCA963X_PWM_BASE
+ pca963x
->led_num
,
147 pca963x
->brightness
);
148 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
149 (ledout
& ~mask
) | (PCA963X_LED_PWM
<< shift
));
152 mutex_unlock(&pca963x
->chip
->mutex
);
155 static void pca963x_blink_work(struct pca963x_led
*pca963x
)
157 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
+
158 (pca963x
->led_num
/ 4);
160 u8 mode2
= i2c_smbus_read_byte_data(pca963x
->chip
->client
,
162 int shift
= 2 * (pca963x
->led_num
% 4);
163 u8 mask
= 0x3 << shift
;
165 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
166 pca963x
->chip
->chipdef
->grppwm
, pca963x
->gdc
);
168 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
169 pca963x
->chip
->chipdef
->grpfreq
, pca963x
->gfrq
);
171 if (!(mode2
& PCA963X_MODE2_DMBLNK
))
172 i2c_smbus_write_byte_data(pca963x
->chip
->client
, PCA963X_MODE2
,
173 mode2
| PCA963X_MODE2_DMBLNK
);
175 mutex_lock(&pca963x
->chip
->mutex
);
176 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
177 if ((ledout
& mask
) != (PCA963X_LED_GRP_PWM
<< shift
))
178 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
179 (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
));
180 mutex_unlock(&pca963x
->chip
->mutex
);
183 static void pca963x_work(struct work_struct
*work
)
185 struct pca963x_led
*pca963x
= container_of(work
,
186 struct pca963x_led
, work
);
188 switch (pca963x
->cmd
) {
190 pca963x_brightness_work(pca963x
);
193 pca963x_blink_work(pca963x
);
198 static void pca963x_led_set(struct led_classdev
*led_cdev
,
199 enum led_brightness value
)
201 struct pca963x_led
*pca963x
;
203 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
205 pca963x
->cmd
= BRIGHTNESS_SET
;
206 pca963x
->brightness
= value
;
209 * Must use workqueue for the actual I/O since I2C operations
212 schedule_work(&pca963x
->work
);
215 static int pca963x_blink_set(struct led_classdev
*led_cdev
,
216 unsigned long *delay_on
, unsigned long *delay_off
)
218 struct pca963x_led
*pca963x
;
219 unsigned long time_on
, time_off
, period
;
222 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
225 time_off
= *delay_off
;
227 /* If both zero, pick reasonable defaults of 500ms each */
228 if (!time_on
&& !time_off
) {
233 period
= time_on
+ time_off
;
235 /* If period not supported by hardware, default to someting sane. */
236 if ((period
< PCA963X_BLINK_PERIOD_MIN
) ||
237 (period
> PCA963X_BLINK_PERIOD_MAX
)) {
240 period
= time_on
+ time_off
;
244 * From manual: duty cycle = (GDC / 256) ->
245 * (time_on / period) = (GDC / 256) ->
246 * GDC = ((time_on * 256) / period)
248 gdc
= (time_on
* 256) / period
;
251 * From manual: period = ((GFRQ + 1) / 24) in seconds.
252 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
253 * GFRQ = ((period * 24 / 1000) - 1)
255 gfrq
= (period
* 24 / 1000) - 1;
257 pca963x
->cmd
= BLINK_SET
;
259 pca963x
->gfrq
= gfrq
;
262 * Must use workqueue for the actual I/O since I2C operations
265 schedule_work(&pca963x
->work
);
268 *delay_off
= time_off
;
273 #if IS_ENABLED(CONFIG_OF)
274 static struct pca963x_platform_data
*
275 pca963x_dt_init(struct i2c_client
*client
, struct pca963x_chipdef
*chip
)
277 struct device_node
*np
= client
->dev
.of_node
, *child
;
278 struct pca963x_platform_data
*pdata
;
279 struct led_info
*pca963x_leds
;
282 count
= of_get_child_count(np
);
283 if (!count
|| count
> chip
->n_leds
)
284 return ERR_PTR(-ENODEV
);
286 pca963x_leds
= devm_kzalloc(&client
->dev
,
287 sizeof(struct led_info
) * chip
->n_leds
, GFP_KERNEL
);
289 return ERR_PTR(-ENOMEM
);
291 for_each_child_of_node(np
, child
) {
292 struct led_info led
= {};
296 res
= of_property_read_u32(child
, "reg", ®
);
297 if ((res
!= 0) || (reg
>= chip
->n_leds
))
300 of_get_property(child
, "label", NULL
) ? : child
->name
;
301 led
.default_trigger
=
302 of_get_property(child
, "linux,default-trigger", NULL
);
303 pca963x_leds
[reg
] = led
;
305 pdata
= devm_kzalloc(&client
->dev
,
306 sizeof(struct pca963x_platform_data
), GFP_KERNEL
);
308 return ERR_PTR(-ENOMEM
);
310 pdata
->leds
.leds
= pca963x_leds
;
311 pdata
->leds
.num_leds
= chip
->n_leds
;
313 /* default to open-drain unless totem pole (push-pull) is specified */
314 if (of_property_read_bool(np
, "nxp,totem-pole"))
315 pdata
->outdrv
= PCA963X_TOTEM_POLE
;
317 pdata
->outdrv
= PCA963X_OPEN_DRAIN
;
319 /* default to software blinking unless hardware blinking is specified */
320 if (of_property_read_bool(np
, "nxp,hw-blink"))
321 pdata
->blink_type
= PCA963X_HW_BLINK
;
323 pdata
->blink_type
= PCA963X_SW_BLINK
;
328 static const struct of_device_id of_pca963x_match
[] = {
329 { .compatible
= "nxp,pca9632", },
330 { .compatible
= "nxp,pca9633", },
331 { .compatible
= "nxp,pca9634", },
332 { .compatible
= "nxp,pca9635", },
335 MODULE_DEVICE_TABLE(of
, of_pca963x_match
);
337 static struct pca963x_platform_data
*
338 pca963x_dt_init(struct i2c_client
*client
, struct pca963x_chipdef
*chip
)
340 return ERR_PTR(-ENODEV
);
344 static int pca963x_probe(struct i2c_client
*client
,
345 const struct i2c_device_id
*id
)
347 struct pca963x
*pca963x_chip
;
348 struct pca963x_led
*pca963x
;
349 struct pca963x_platform_data
*pdata
;
350 struct pca963x_chipdef
*chip
;
353 chip
= &pca963x_chipdefs
[id
->driver_data
];
354 pdata
= dev_get_platdata(&client
->dev
);
357 pdata
= pca963x_dt_init(client
, chip
);
359 dev_warn(&client
->dev
, "could not parse configuration\n");
364 if (pdata
&& (pdata
->leds
.num_leds
< 1 ||
365 pdata
->leds
.num_leds
> chip
->n_leds
)) {
366 dev_err(&client
->dev
, "board info must claim 1-%d LEDs",
371 pca963x_chip
= devm_kzalloc(&client
->dev
, sizeof(*pca963x_chip
),
375 pca963x
= devm_kzalloc(&client
->dev
, chip
->n_leds
* sizeof(*pca963x
),
380 i2c_set_clientdata(client
, pca963x_chip
);
382 mutex_init(&pca963x_chip
->mutex
);
383 pca963x_chip
->chipdef
= chip
;
384 pca963x_chip
->client
= client
;
385 pca963x_chip
->leds
= pca963x
;
387 /* Turn off LEDs by default*/
388 for (i
= 0; i
< chip
->n_leds
/ 4; i
++)
389 i2c_smbus_write_byte_data(client
, chip
->ledout_base
+ i
, 0x00);
391 for (i
= 0; i
< chip
->n_leds
; i
++) {
392 pca963x
[i
].led_num
= i
;
393 pca963x
[i
].chip
= pca963x_chip
;
395 /* Platform data can specify LED names and default triggers */
396 if (pdata
&& i
< pdata
->leds
.num_leds
) {
397 if (pdata
->leds
.leds
[i
].name
)
398 snprintf(pca963x
[i
].name
,
399 sizeof(pca963x
[i
].name
), "pca963x:%s",
400 pdata
->leds
.leds
[i
].name
);
401 if (pdata
->leds
.leds
[i
].default_trigger
)
402 pca963x
[i
].led_cdev
.default_trigger
=
403 pdata
->leds
.leds
[i
].default_trigger
;
405 if (!pdata
|| i
>= pdata
->leds
.num_leds
||
406 !pdata
->leds
.leds
[i
].name
)
407 snprintf(pca963x
[i
].name
, sizeof(pca963x
[i
].name
),
408 "pca963x:%d:%.2x:%d", client
->adapter
->nr
,
411 pca963x
[i
].led_cdev
.name
= pca963x
[i
].name
;
412 pca963x
[i
].led_cdev
.brightness_set
= pca963x_led_set
;
414 if (pdata
&& pdata
->blink_type
== PCA963X_HW_BLINK
)
415 pca963x
[i
].led_cdev
.blink_set
= pca963x_blink_set
;
417 INIT_WORK(&pca963x
[i
].work
, pca963x_work
);
419 err
= led_classdev_register(&client
->dev
, &pca963x
[i
].led_cdev
);
424 /* Disable LED all-call address and set normal mode */
425 i2c_smbus_write_byte_data(client
, PCA963X_MODE1
, 0x00);
428 /* Configure output: open-drain or totem pole (push-pull) */
429 if (pdata
->outdrv
== PCA963X_OPEN_DRAIN
)
430 i2c_smbus_write_byte_data(client
, PCA963X_MODE2
, 0x01);
432 i2c_smbus_write_byte_data(client
, PCA963X_MODE2
, 0x05);
439 led_classdev_unregister(&pca963x
[i
].led_cdev
);
440 cancel_work_sync(&pca963x
[i
].work
);
446 static int pca963x_remove(struct i2c_client
*client
)
448 struct pca963x
*pca963x
= i2c_get_clientdata(client
);
451 for (i
= 0; i
< pca963x
->chipdef
->n_leds
; i
++) {
452 led_classdev_unregister(&pca963x
->leds
[i
].led_cdev
);
453 cancel_work_sync(&pca963x
->leds
[i
].work
);
459 static struct i2c_driver pca963x_driver
= {
461 .name
= "leds-pca963x",
462 .of_match_table
= of_match_ptr(of_pca963x_match
),
464 .probe
= pca963x_probe
,
465 .remove
= pca963x_remove
,
466 .id_table
= pca963x_id
,
469 module_i2c_driver(pca963x_driver
);
471 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
472 MODULE_DESCRIPTION("PCA963X LED driver");
473 MODULE_LICENSE("GPL v2");