1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2011 bct electronic GmbH
4 * Copyright 2013 Qtechnology/AS
6 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
7 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
9 * Based on leds-pca955x.c
11 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
12 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
14 * Note that hardware blinking violates the leds infrastructure driver
15 * interface since the hardware only supports blinking all LEDs with the
16 * same delay_on/delay_off rates. That is, only the LEDs that are set to
17 * blink will actually blink but all LEDs that are set to blink will blink
18 * in identical fashion. The delay_on/delay_off values of the last LED
19 * that is set to blink will be used for all of the blinking LEDs.
20 * Hardware blinking is disabled by default but can be enabled by setting
21 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
22 * or by adding the 'nxp,hw-blink' property to the DTS.
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <linux/leds.h>
30 #include <linux/err.h>
31 #include <linux/i2c.h>
32 #include <linux/property.h>
33 #include <linux/slab.h>
35 #include <linux/platform_data/leds-pca963x.h>
37 /* LED select registers determine the source that drives LED outputs */
38 #define PCA963X_LED_OFF 0x0 /* LED driver off */
39 #define PCA963X_LED_ON 0x1 /* LED driver on */
40 #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
41 #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
43 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
45 #define PCA963X_MODE1 0x00
46 #define PCA963X_MODE2 0x01
47 #define PCA963X_PWM_BASE 0x02
55 struct pca963x_chipdef
{
63 static struct pca963x_chipdef pca963x_chipdefs
[] = {
84 /* Total blink period in milliseconds */
85 #define PCA963X_BLINK_PERIOD_MIN 42
86 #define PCA963X_BLINK_PERIOD_MAX 10667
88 static const struct i2c_device_id pca963x_id
[] = {
89 { "pca9632", pca9633
},
90 { "pca9633", pca9633
},
91 { "pca9634", pca9634
},
92 { "pca9635", pca9635
},
95 MODULE_DEVICE_TABLE(i2c
, pca963x_id
);
100 struct pca963x_chipdef
*chipdef
;
102 struct i2c_client
*client
;
103 struct pca963x_led
*leds
;
104 unsigned long leds_on
;
108 struct pca963x
*chip
;
109 struct led_classdev led_cdev
;
110 int led_num
; /* 0 .. 15 potentially */
116 static int pca963x_brightness(struct pca963x_led
*pca963x
,
117 enum led_brightness brightness
)
119 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
120 + (pca963x
->led_num
/ 4);
122 int shift
= 2 * (pca963x
->led_num
% 4);
123 u8 mask
= 0x3 << shift
;
126 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
127 switch (brightness
) {
129 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
131 (ledout
& ~mask
) | (PCA963X_LED_ON
<< shift
));
134 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
135 ledout_addr
, ledout
& ~mask
);
138 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
139 PCA963X_PWM_BASE
+ pca963x
->led_num
,
143 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
145 (ledout
& ~mask
) | (PCA963X_LED_PWM
<< shift
));
152 static void pca963x_blink(struct pca963x_led
*pca963x
)
154 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
+
155 (pca963x
->led_num
/ 4);
157 u8 mode2
= i2c_smbus_read_byte_data(pca963x
->chip
->client
,
159 int shift
= 2 * (pca963x
->led_num
% 4);
160 u8 mask
= 0x3 << shift
;
162 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
163 pca963x
->chip
->chipdef
->grppwm
, pca963x
->gdc
);
165 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
166 pca963x
->chip
->chipdef
->grpfreq
, pca963x
->gfrq
);
168 if (!(mode2
& PCA963X_MODE2_DMBLNK
))
169 i2c_smbus_write_byte_data(pca963x
->chip
->client
, PCA963X_MODE2
,
170 mode2
| PCA963X_MODE2_DMBLNK
);
172 mutex_lock(&pca963x
->chip
->mutex
);
173 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
174 if ((ledout
& mask
) != (PCA963X_LED_GRP_PWM
<< shift
))
175 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
176 (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
));
177 mutex_unlock(&pca963x
->chip
->mutex
);
180 static int pca963x_power_state(struct pca963x_led
*pca963x
)
182 unsigned long *leds_on
= &pca963x
->chip
->leds_on
;
183 unsigned long cached_leds
= pca963x
->chip
->leds_on
;
185 if (pca963x
->led_cdev
.brightness
)
186 set_bit(pca963x
->led_num
, leds_on
);
188 clear_bit(pca963x
->led_num
, leds_on
);
190 if (!(*leds_on
) != !cached_leds
)
191 return i2c_smbus_write_byte_data(pca963x
->chip
->client
,
192 PCA963X_MODE1
, *leds_on
? 0 : BIT(4));
197 static int pca963x_led_set(struct led_classdev
*led_cdev
,
198 enum led_brightness value
)
200 struct pca963x_led
*pca963x
;
203 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
205 mutex_lock(&pca963x
->chip
->mutex
);
207 ret
= pca963x_brightness(pca963x
, value
);
210 ret
= pca963x_power_state(pca963x
);
213 mutex_unlock(&pca963x
->chip
->mutex
);
217 static unsigned int pca963x_period_scale(struct pca963x_led
*pca963x
,
220 unsigned int scaling
= pca963x
->chip
->chipdef
->scaling
;
222 return scaling
? DIV_ROUND_CLOSEST(val
* scaling
, 1000) : val
;
225 static int pca963x_blink_set(struct led_classdev
*led_cdev
,
226 unsigned long *delay_on
, unsigned long *delay_off
)
228 struct pca963x_led
*pca963x
;
229 unsigned long time_on
, time_off
, period
;
232 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
235 time_off
= *delay_off
;
237 /* If both zero, pick reasonable defaults of 500ms each */
238 if (!time_on
&& !time_off
) {
243 period
= pca963x_period_scale(pca963x
, time_on
+ time_off
);
245 /* If period not supported by hardware, default to someting sane. */
246 if ((period
< PCA963X_BLINK_PERIOD_MIN
) ||
247 (period
> PCA963X_BLINK_PERIOD_MAX
)) {
250 period
= pca963x_period_scale(pca963x
, 1000);
254 * From manual: duty cycle = (GDC / 256) ->
255 * (time_on / period) = (GDC / 256) ->
256 * GDC = ((time_on * 256) / period)
258 gdc
= (pca963x_period_scale(pca963x
, time_on
) * 256) / period
;
261 * From manual: period = ((GFRQ + 1) / 24) in seconds.
262 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
263 * GFRQ = ((period * 24 / 1000) - 1)
265 gfrq
= (period
* 24 / 1000) - 1;
268 pca963x
->gfrq
= gfrq
;
270 pca963x_blink(pca963x
);
273 *delay_off
= time_off
;
278 static struct pca963x_platform_data
*
279 pca963x_get_pdata(struct i2c_client
*client
, struct pca963x_chipdef
*chip
)
281 struct pca963x_platform_data
*pdata
;
282 struct led_info
*pca963x_leds
;
283 struct fwnode_handle
*child
;
286 count
= device_get_child_node_count(&client
->dev
);
287 if (!count
|| count
> chip
->n_leds
)
288 return ERR_PTR(-ENODEV
);
290 pca963x_leds
= devm_kcalloc(&client
->dev
,
291 chip
->n_leds
, sizeof(struct led_info
), GFP_KERNEL
);
293 return ERR_PTR(-ENOMEM
);
295 device_for_each_child_node(&client
->dev
, child
) {
296 struct led_info led
= {};
300 res
= fwnode_property_read_u32(child
, "reg", ®
);
301 if ((res
!= 0) || (reg
>= chip
->n_leds
))
304 res
= fwnode_property_read_string(child
, "label", &led
.name
);
305 if ((res
!= 0) && is_of_node(child
))
306 led
.name
= to_of_node(child
)->name
;
308 fwnode_property_read_string(child
, "linux,default-trigger",
309 &led
.default_trigger
);
311 pca963x_leds
[reg
] = led
;
313 pdata
= devm_kzalloc(&client
->dev
,
314 sizeof(struct pca963x_platform_data
), GFP_KERNEL
);
316 return ERR_PTR(-ENOMEM
);
318 pdata
->leds
.leds
= pca963x_leds
;
319 pdata
->leds
.num_leds
= chip
->n_leds
;
321 /* default to open-drain unless totem pole (push-pull) is specified */
322 if (device_property_read_bool(&client
->dev
, "nxp,totem-pole"))
323 pdata
->outdrv
= PCA963X_TOTEM_POLE
;
325 pdata
->outdrv
= PCA963X_OPEN_DRAIN
;
327 /* default to software blinking unless hardware blinking is specified */
328 if (device_property_read_bool(&client
->dev
, "nxp,hw-blink"))
329 pdata
->blink_type
= PCA963X_HW_BLINK
;
331 pdata
->blink_type
= PCA963X_SW_BLINK
;
333 if (device_property_read_u32(&client
->dev
, "nxp,period-scale",
335 chip
->scaling
= 1000;
337 /* default to non-inverted output, unless inverted is specified */
338 if (device_property_read_bool(&client
->dev
, "nxp,inverted-out"))
339 pdata
->dir
= PCA963X_INVERTED
;
341 pdata
->dir
= PCA963X_NORMAL
;
346 static const struct of_device_id of_pca963x_match
[] = {
347 { .compatible
= "nxp,pca9632", },
348 { .compatible
= "nxp,pca9633", },
349 { .compatible
= "nxp,pca9634", },
350 { .compatible
= "nxp,pca9635", },
353 MODULE_DEVICE_TABLE(of
, of_pca963x_match
);
355 static int pca963x_probe(struct i2c_client
*client
,
356 const struct i2c_device_id
*id
)
358 struct pca963x
*pca963x_chip
;
359 struct pca963x_led
*pca963x
;
360 struct pca963x_platform_data
*pdata
;
361 struct pca963x_chipdef
*chip
;
364 chip
= &pca963x_chipdefs
[id
->driver_data
];
365 pdata
= dev_get_platdata(&client
->dev
);
368 pdata
= pca963x_get_pdata(client
, chip
);
370 dev_warn(&client
->dev
, "could not parse configuration\n");
375 if (pdata
&& (pdata
->leds
.num_leds
< 1 ||
376 pdata
->leds
.num_leds
> chip
->n_leds
)) {
377 dev_err(&client
->dev
, "board info must claim 1-%d LEDs",
382 pca963x_chip
= devm_kzalloc(&client
->dev
, sizeof(*pca963x_chip
),
386 pca963x
= devm_kcalloc(&client
->dev
, chip
->n_leds
, sizeof(*pca963x
),
391 i2c_set_clientdata(client
, pca963x_chip
);
393 mutex_init(&pca963x_chip
->mutex
);
394 pca963x_chip
->chipdef
= chip
;
395 pca963x_chip
->client
= client
;
396 pca963x_chip
->leds
= pca963x
;
398 /* Turn off LEDs by default*/
399 for (i
= 0; i
< chip
->n_leds
/ 4; i
++)
400 i2c_smbus_write_byte_data(client
, chip
->ledout_base
+ i
, 0x00);
402 for (i
= 0; i
< chip
->n_leds
; i
++) {
403 pca963x
[i
].led_num
= i
;
404 pca963x
[i
].chip
= pca963x_chip
;
406 /* Platform data can specify LED names and default triggers */
407 if (pdata
&& i
< pdata
->leds
.num_leds
) {
408 if (pdata
->leds
.leds
[i
].name
)
409 snprintf(pca963x
[i
].name
,
410 sizeof(pca963x
[i
].name
), "pca963x:%s",
411 pdata
->leds
.leds
[i
].name
);
412 if (pdata
->leds
.leds
[i
].default_trigger
)
413 pca963x
[i
].led_cdev
.default_trigger
=
414 pdata
->leds
.leds
[i
].default_trigger
;
416 if (!pdata
|| i
>= pdata
->leds
.num_leds
||
417 !pdata
->leds
.leds
[i
].name
)
418 snprintf(pca963x
[i
].name
, sizeof(pca963x
[i
].name
),
419 "pca963x:%d:%.2x:%d", client
->adapter
->nr
,
422 pca963x
[i
].led_cdev
.name
= pca963x
[i
].name
;
423 pca963x
[i
].led_cdev
.brightness_set_blocking
= pca963x_led_set
;
425 if (pdata
&& pdata
->blink_type
== PCA963X_HW_BLINK
)
426 pca963x
[i
].led_cdev
.blink_set
= pca963x_blink_set
;
428 err
= led_classdev_register(&client
->dev
, &pca963x
[i
].led_cdev
);
433 /* Disable LED all-call address, and power down initially */
434 i2c_smbus_write_byte_data(client
, PCA963X_MODE1
, BIT(4));
437 u8 mode2
= i2c_smbus_read_byte_data(pca963x
->chip
->client
,
439 /* Configure output: open-drain or totem pole (push-pull) */
440 if (pdata
->outdrv
== PCA963X_OPEN_DRAIN
)
444 /* Configure direction: normal or inverted */
445 if (pdata
->dir
== PCA963X_INVERTED
)
447 i2c_smbus_write_byte_data(pca963x
->chip
->client
, PCA963X_MODE2
,
455 led_classdev_unregister(&pca963x
[i
].led_cdev
);
460 static int pca963x_remove(struct i2c_client
*client
)
462 struct pca963x
*pca963x
= i2c_get_clientdata(client
);
465 for (i
= 0; i
< pca963x
->chipdef
->n_leds
; i
++)
466 led_classdev_unregister(&pca963x
->leds
[i
].led_cdev
);
471 static struct i2c_driver pca963x_driver
= {
473 .name
= "leds-pca963x",
474 .of_match_table
= of_pca963x_match
,
476 .probe
= pca963x_probe
,
477 .remove
= pca963x_remove
,
478 .id_table
= pca963x_id
,
481 module_i2c_driver(pca963x_driver
);
483 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
484 MODULE_DESCRIPTION("PCA963X LED driver");
485 MODULE_LICENSE("GPL v2");