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 <ribalda@kernel.org>
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_OUTDRV 0x04 /* Open-drain or totem pole */
44 #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
45 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
47 #define PCA963X_MODE1 0x00
48 #define PCA963X_MODE2 0x01
49 #define PCA963X_PWM_BASE 0x02
57 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
);
102 struct pca963x_chipdef
*chipdef
;
104 struct i2c_client
*client
;
105 struct pca963x_led
*leds
;
106 unsigned long leds_on
;
110 struct pca963x
*chip
;
111 struct led_classdev led_cdev
;
112 int led_num
; /* 0 .. 15 potentially */
118 static int pca963x_brightness(struct pca963x_led
*pca963x
,
119 enum led_brightness brightness
)
121 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
122 + (pca963x
->led_num
/ 4);
124 int shift
= 2 * (pca963x
->led_num
% 4);
125 u8 mask
= 0x3 << shift
;
128 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
129 switch (brightness
) {
131 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
133 (ledout
& ~mask
) | (PCA963X_LED_ON
<< shift
));
136 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
137 ledout_addr
, ledout
& ~mask
);
140 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
141 PCA963X_PWM_BASE
+ pca963x
->led_num
,
145 ret
= i2c_smbus_write_byte_data(pca963x
->chip
->client
,
147 (ledout
& ~mask
) | (PCA963X_LED_PWM
<< shift
));
154 static void pca963x_blink(struct pca963x_led
*pca963x
)
156 u8 ledout_addr
= pca963x
->chip
->chipdef
->ledout_base
+
157 (pca963x
->led_num
/ 4);
159 u8 mode2
= i2c_smbus_read_byte_data(pca963x
->chip
->client
,
161 int shift
= 2 * (pca963x
->led_num
% 4);
162 u8 mask
= 0x3 << shift
;
164 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
165 pca963x
->chip
->chipdef
->grppwm
, pca963x
->gdc
);
167 i2c_smbus_write_byte_data(pca963x
->chip
->client
,
168 pca963x
->chip
->chipdef
->grpfreq
, pca963x
->gfrq
);
170 if (!(mode2
& PCA963X_MODE2_DMBLNK
))
171 i2c_smbus_write_byte_data(pca963x
->chip
->client
, PCA963X_MODE2
,
172 mode2
| PCA963X_MODE2_DMBLNK
);
174 mutex_lock(&pca963x
->chip
->mutex
);
175 ledout
= i2c_smbus_read_byte_data(pca963x
->chip
->client
, ledout_addr
);
176 if ((ledout
& mask
) != (PCA963X_LED_GRP_PWM
<< shift
))
177 i2c_smbus_write_byte_data(pca963x
->chip
->client
, ledout_addr
,
178 (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
));
179 mutex_unlock(&pca963x
->chip
->mutex
);
182 static int pca963x_power_state(struct pca963x_led
*pca963x
)
184 unsigned long *leds_on
= &pca963x
->chip
->leds_on
;
185 unsigned long cached_leds
= pca963x
->chip
->leds_on
;
187 if (pca963x
->led_cdev
.brightness
)
188 set_bit(pca963x
->led_num
, leds_on
);
190 clear_bit(pca963x
->led_num
, leds_on
);
192 if (!(*leds_on
) != !cached_leds
)
193 return i2c_smbus_write_byte_data(pca963x
->chip
->client
,
194 PCA963X_MODE1
, *leds_on
? 0 : BIT(4));
199 static int pca963x_led_set(struct led_classdev
*led_cdev
,
200 enum led_brightness value
)
202 struct pca963x_led
*pca963x
;
205 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
207 mutex_lock(&pca963x
->chip
->mutex
);
209 ret
= pca963x_brightness(pca963x
, value
);
212 ret
= pca963x_power_state(pca963x
);
215 mutex_unlock(&pca963x
->chip
->mutex
);
219 static unsigned int pca963x_period_scale(struct pca963x_led
*pca963x
,
222 unsigned int scaling
= pca963x
->chip
->chipdef
->scaling
;
224 return scaling
? DIV_ROUND_CLOSEST(val
* scaling
, 1000) : val
;
227 static int pca963x_blink_set(struct led_classdev
*led_cdev
,
228 unsigned long *delay_on
, unsigned long *delay_off
)
230 struct pca963x_led
*pca963x
;
231 unsigned long time_on
, time_off
, period
;
234 pca963x
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
237 time_off
= *delay_off
;
239 /* If both zero, pick reasonable defaults of 500ms each */
240 if (!time_on
&& !time_off
) {
245 period
= pca963x_period_scale(pca963x
, time_on
+ time_off
);
247 /* If period not supported by hardware, default to someting sane. */
248 if ((period
< PCA963X_BLINK_PERIOD_MIN
) ||
249 (period
> PCA963X_BLINK_PERIOD_MAX
)) {
252 period
= pca963x_period_scale(pca963x
, 1000);
256 * From manual: duty cycle = (GDC / 256) ->
257 * (time_on / period) = (GDC / 256) ->
258 * GDC = ((time_on * 256) / period)
260 gdc
= (pca963x_period_scale(pca963x
, time_on
) * 256) / period
;
263 * From manual: period = ((GFRQ + 1) / 24) in seconds.
264 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
265 * GFRQ = ((period * 24 / 1000) - 1)
267 gfrq
= (period
* 24 / 1000) - 1;
270 pca963x
->gfrq
= gfrq
;
272 pca963x_blink(pca963x
);
275 *delay_off
= time_off
;
280 static struct pca963x_platform_data
*
281 pca963x_get_pdata(struct i2c_client
*client
, struct pca963x_chipdef
*chip
)
283 struct pca963x_platform_data
*pdata
;
284 struct led_info
*pca963x_leds
;
285 struct fwnode_handle
*child
;
288 count
= device_get_child_node_count(&client
->dev
);
289 if (!count
|| count
> chip
->n_leds
)
290 return ERR_PTR(-ENODEV
);
292 pca963x_leds
= devm_kcalloc(&client
->dev
,
293 chip
->n_leds
, sizeof(struct led_info
), GFP_KERNEL
);
295 return ERR_PTR(-ENOMEM
);
297 device_for_each_child_node(&client
->dev
, child
) {
298 struct led_info led
= {};
302 res
= fwnode_property_read_u32(child
, "reg", ®
);
303 if ((res
!= 0) || (reg
>= chip
->n_leds
))
306 res
= fwnode_property_read_string(child
, "label", &led
.name
);
307 if ((res
!= 0) && is_of_node(child
))
308 led
.name
= to_of_node(child
)->name
;
310 fwnode_property_read_string(child
, "linux,default-trigger",
311 &led
.default_trigger
);
313 pca963x_leds
[reg
] = led
;
315 pdata
= devm_kzalloc(&client
->dev
,
316 sizeof(struct pca963x_platform_data
), GFP_KERNEL
);
318 return ERR_PTR(-ENOMEM
);
320 pdata
->leds
.leds
= pca963x_leds
;
321 pdata
->leds
.num_leds
= chip
->n_leds
;
323 /* default to open-drain unless totem pole (push-pull) is specified */
324 if (device_property_read_bool(&client
->dev
, "nxp,totem-pole"))
325 pdata
->outdrv
= PCA963X_TOTEM_POLE
;
327 pdata
->outdrv
= PCA963X_OPEN_DRAIN
;
329 /* default to software blinking unless hardware blinking is specified */
330 if (device_property_read_bool(&client
->dev
, "nxp,hw-blink"))
331 pdata
->blink_type
= PCA963X_HW_BLINK
;
333 pdata
->blink_type
= PCA963X_SW_BLINK
;
335 if (device_property_read_u32(&client
->dev
, "nxp,period-scale",
337 chip
->scaling
= 1000;
339 /* default to non-inverted output, unless inverted is specified */
340 if (device_property_read_bool(&client
->dev
, "nxp,inverted-out"))
341 pdata
->dir
= PCA963X_INVERTED
;
343 pdata
->dir
= PCA963X_NORMAL
;
348 static const struct of_device_id of_pca963x_match
[] = {
349 { .compatible
= "nxp,pca9632", },
350 { .compatible
= "nxp,pca9633", },
351 { .compatible
= "nxp,pca9634", },
352 { .compatible
= "nxp,pca9635", },
355 MODULE_DEVICE_TABLE(of
, of_pca963x_match
);
357 static int pca963x_probe(struct i2c_client
*client
,
358 const struct i2c_device_id
*id
)
360 struct pca963x
*pca963x_chip
;
361 struct pca963x_led
*pca963x
;
362 struct pca963x_platform_data
*pdata
;
363 struct pca963x_chipdef
*chip
;
366 chip
= &pca963x_chipdefs
[id
->driver_data
];
367 pdata
= dev_get_platdata(&client
->dev
);
370 pdata
= pca963x_get_pdata(client
, chip
);
372 dev_warn(&client
->dev
, "could not parse configuration\n");
377 if (pdata
&& (pdata
->leds
.num_leds
< 1 ||
378 pdata
->leds
.num_leds
> chip
->n_leds
)) {
379 dev_err(&client
->dev
, "board info must claim 1-%d LEDs",
384 pca963x_chip
= devm_kzalloc(&client
->dev
, sizeof(*pca963x_chip
),
388 pca963x
= devm_kcalloc(&client
->dev
, chip
->n_leds
, sizeof(*pca963x
),
393 i2c_set_clientdata(client
, pca963x_chip
);
395 mutex_init(&pca963x_chip
->mutex
);
396 pca963x_chip
->chipdef
= chip
;
397 pca963x_chip
->client
= client
;
398 pca963x_chip
->leds
= pca963x
;
400 /* Turn off LEDs by default*/
401 for (i
= 0; i
< chip
->n_leds
/ 4; i
++)
402 i2c_smbus_write_byte_data(client
, chip
->ledout_base
+ i
, 0x00);
404 for (i
= 0; i
< chip
->n_leds
; i
++) {
405 pca963x
[i
].led_num
= i
;
406 pca963x
[i
].chip
= pca963x_chip
;
408 /* Platform data can specify LED names and default triggers */
409 if (pdata
&& i
< pdata
->leds
.num_leds
) {
410 if (pdata
->leds
.leds
[i
].name
)
411 snprintf(pca963x
[i
].name
,
412 sizeof(pca963x
[i
].name
), "pca963x:%s",
413 pdata
->leds
.leds
[i
].name
);
414 if (pdata
->leds
.leds
[i
].default_trigger
)
415 pca963x
[i
].led_cdev
.default_trigger
=
416 pdata
->leds
.leds
[i
].default_trigger
;
418 if (!pdata
|| i
>= pdata
->leds
.num_leds
||
419 !pdata
->leds
.leds
[i
].name
)
420 snprintf(pca963x
[i
].name
, sizeof(pca963x
[i
].name
),
421 "pca963x:%d:%.2x:%d", client
->adapter
->nr
,
424 pca963x
[i
].led_cdev
.name
= pca963x
[i
].name
;
425 pca963x
[i
].led_cdev
.brightness_set_blocking
= pca963x_led_set
;
427 if (pdata
&& pdata
->blink_type
== PCA963X_HW_BLINK
)
428 pca963x
[i
].led_cdev
.blink_set
= pca963x_blink_set
;
430 err
= led_classdev_register(&client
->dev
, &pca963x
[i
].led_cdev
);
435 /* Disable LED all-call address, and power down initially */
436 i2c_smbus_write_byte_data(client
, PCA963X_MODE1
, BIT(4));
439 u8 mode2
= i2c_smbus_read_byte_data(pca963x
->chip
->client
,
441 /* Configure output: open-drain or totem pole (push-pull) */
442 if (pdata
->outdrv
== PCA963X_OPEN_DRAIN
)
443 mode2
&= ~PCA963X_MODE2_OUTDRV
;
445 mode2
|= PCA963X_MODE2_OUTDRV
;
446 /* Configure direction: normal or inverted */
447 if (pdata
->dir
== PCA963X_INVERTED
)
448 mode2
|= PCA963X_MODE2_INVRT
;
449 i2c_smbus_write_byte_data(pca963x
->chip
->client
, PCA963X_MODE2
,
457 led_classdev_unregister(&pca963x
[i
].led_cdev
);
462 static int pca963x_remove(struct i2c_client
*client
)
464 struct pca963x
*pca963x
= i2c_get_clientdata(client
);
467 for (i
= 0; i
< pca963x
->chipdef
->n_leds
; i
++)
468 led_classdev_unregister(&pca963x
->leds
[i
].led_cdev
);
473 static struct i2c_driver pca963x_driver
= {
475 .name
= "leds-pca963x",
476 .of_match_table
= of_pca963x_match
,
478 .probe
= pca963x_probe
,
479 .remove
= pca963x_remove
,
480 .id_table
= pca963x_id
,
483 module_i2c_driver(pca963x_driver
);
485 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
486 MODULE_DESCRIPTION("PCA963X LED driver");
487 MODULE_LICENSE("GPL v2");