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>
36 /* LED select registers determine the source that drives LED outputs */
37 #define PCA963X_LED_OFF 0x0 /* LED driver off */
38 #define PCA963X_LED_ON 0x1 /* LED driver on */
39 #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
40 #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
42 #define PCA963X_MODE1_SLEEP 0x04 /* Normal mode or Low Power mode, oscillator off */
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
*chip
;
103 struct led_classdev led_cdev
;
104 int led_num
; /* 0 .. 15 potentially */
111 struct pca963x_chipdef
*chipdef
;
113 struct i2c_client
*client
;
114 unsigned long leds_on
;
115 struct pca963x_led leds
[];
118 static int pca963x_brightness(struct pca963x_led
*led
,
119 enum led_brightness brightness
)
121 struct i2c_client
*client
= led
->chip
->client
;
122 struct pca963x_chipdef
*chipdef
= led
->chip
->chipdef
;
123 u8 ledout_addr
, ledout
, mask
, val
;
127 ledout_addr
= chipdef
->ledout_base
+ (led
->led_num
/ 4);
128 shift
= 2 * (led
->led_num
% 4);
130 ledout
= i2c_smbus_read_byte_data(client
, ledout_addr
);
132 switch (brightness
) {
135 val
= (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
);
136 ret
= i2c_smbus_write_byte_data(client
,
141 val
= (ledout
& ~mask
) | (PCA963X_LED_ON
<< shift
);
143 ret
= i2c_smbus_write_byte_data(client
, ledout_addr
, val
);
146 val
= ledout
& ~mask
;
147 ret
= i2c_smbus_write_byte_data(client
, ledout_addr
, val
);
148 led
->blinking
= false;
151 ret
= i2c_smbus_write_byte_data(client
,
159 val
= (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
);
161 val
= (ledout
& ~mask
) | (PCA963X_LED_PWM
<< shift
);
163 ret
= i2c_smbus_write_byte_data(client
, ledout_addr
, val
);
170 static void pca963x_blink(struct pca963x_led
*led
)
172 struct i2c_client
*client
= led
->chip
->client
;
173 struct pca963x_chipdef
*chipdef
= led
->chip
->chipdef
;
174 u8 ledout_addr
, ledout
, mask
, val
, mode2
;
177 ledout_addr
= chipdef
->ledout_base
+ (led
->led_num
/ 4);
178 shift
= 2 * (led
->led_num
% 4);
180 mode2
= i2c_smbus_read_byte_data(client
, PCA963X_MODE2
);
182 i2c_smbus_write_byte_data(client
, chipdef
->grppwm
, led
->gdc
);
184 i2c_smbus_write_byte_data(client
, chipdef
->grpfreq
, led
->gfrq
);
186 if (!(mode2
& PCA963X_MODE2_DMBLNK
))
187 i2c_smbus_write_byte_data(client
, PCA963X_MODE2
,
188 mode2
| PCA963X_MODE2_DMBLNK
);
190 mutex_lock(&led
->chip
->mutex
);
192 ledout
= i2c_smbus_read_byte_data(client
, ledout_addr
);
193 if ((ledout
& mask
) != (PCA963X_LED_GRP_PWM
<< shift
)) {
194 val
= (ledout
& ~mask
) | (PCA963X_LED_GRP_PWM
<< shift
);
195 i2c_smbus_write_byte_data(client
, ledout_addr
, val
);
198 mutex_unlock(&led
->chip
->mutex
);
199 led
->blinking
= true;
202 static int pca963x_power_state(struct pca963x_led
*led
)
204 struct i2c_client
*client
= led
->chip
->client
;
205 unsigned long *leds_on
= &led
->chip
->leds_on
;
206 unsigned long cached_leds
= *leds_on
;
208 if (led
->led_cdev
.brightness
)
209 set_bit(led
->led_num
, leds_on
);
211 clear_bit(led
->led_num
, leds_on
);
213 if (!(*leds_on
) != !cached_leds
)
214 return i2c_smbus_write_byte_data(client
, PCA963X_MODE1
,
215 *leds_on
? 0 : BIT(4));
220 static int pca963x_led_set(struct led_classdev
*led_cdev
,
221 enum led_brightness value
)
223 struct pca963x_led
*led
;
226 led
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
228 mutex_lock(&led
->chip
->mutex
);
230 ret
= pca963x_brightness(led
, value
);
233 ret
= pca963x_power_state(led
);
236 mutex_unlock(&led
->chip
->mutex
);
240 static unsigned int pca963x_period_scale(struct pca963x_led
*led
,
243 unsigned int scaling
= led
->chip
->chipdef
->scaling
;
245 return scaling
? DIV_ROUND_CLOSEST(val
* scaling
, 1000) : val
;
248 static int pca963x_blink_set(struct led_classdev
*led_cdev
,
249 unsigned long *delay_on
, unsigned long *delay_off
)
251 unsigned long time_on
, time_off
, period
;
252 struct pca963x_led
*led
;
255 led
= container_of(led_cdev
, struct pca963x_led
, led_cdev
);
258 time_off
= *delay_off
;
260 /* If both zero, pick reasonable defaults of 500ms each */
261 if (!time_on
&& !time_off
) {
266 period
= pca963x_period_scale(led
, time_on
+ time_off
);
268 /* If period not supported by hardware, default to someting sane. */
269 if ((period
< PCA963X_BLINK_PERIOD_MIN
) ||
270 (period
> PCA963X_BLINK_PERIOD_MAX
)) {
273 period
= pca963x_period_scale(led
, 1000);
277 * From manual: duty cycle = (GDC / 256) ->
278 * (time_on / period) = (GDC / 256) ->
279 * GDC = ((time_on * 256) / period)
281 gdc
= (pca963x_period_scale(led
, time_on
) * 256) / period
;
284 * From manual: period = ((GFRQ + 1) / 24) in seconds.
285 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
286 * GFRQ = ((period * 24 / 1000) - 1)
288 gfrq
= (period
* 24 / 1000) - 1;
294 led
->led_cdev
.brightness
= LED_FULL
;
295 pca963x_led_set(led_cdev
, LED_FULL
);
298 *delay_off
= time_off
;
303 static int pca963x_register_leds(struct i2c_client
*client
,
304 struct pca963x
*chip
)
306 struct pca963x_chipdef
*chipdef
= chip
->chipdef
;
307 struct pca963x_led
*led
= chip
->leds
;
308 struct device
*dev
= &client
->dev
;
314 if (device_property_read_u32(dev
, "nxp,period-scale",
316 chipdef
->scaling
= 1000;
318 hw_blink
= device_property_read_bool(dev
, "nxp,hw-blink");
320 mode2
= i2c_smbus_read_byte_data(client
, PCA963X_MODE2
);
324 /* default to open-drain unless totem pole (push-pull) is specified */
325 if (device_property_read_bool(dev
, "nxp,totem-pole"))
326 mode2
|= PCA963X_MODE2_OUTDRV
;
328 mode2
&= ~PCA963X_MODE2_OUTDRV
;
330 /* default to non-inverted output, unless inverted is specified */
331 if (device_property_read_bool(dev
, "nxp,inverted-out"))
332 mode2
|= PCA963X_MODE2_INVRT
;
334 mode2
&= ~PCA963X_MODE2_INVRT
;
336 ret
= i2c_smbus_write_byte_data(client
, PCA963X_MODE2
, mode2
);
340 device_for_each_child_node_scoped(dev
, child
) {
341 struct led_init_data init_data
= {};
342 char default_label
[32];
344 ret
= fwnode_property_read_u32(child
, "reg", ®
);
345 if (ret
|| reg
>= chipdef
->n_leds
) {
346 dev_err(dev
, "Invalid 'reg' property for node %pfw\n",
353 led
->led_cdev
.brightness_set_blocking
= pca963x_led_set
;
355 led
->led_cdev
.blink_set
= pca963x_blink_set
;
356 led
->blinking
= false;
358 init_data
.fwnode
= child
;
359 /* for backwards compatibility */
360 init_data
.devicename
= "pca963x";
361 snprintf(default_label
, sizeof(default_label
), "%d:%.2x:%u",
362 client
->adapter
->nr
, client
->addr
, reg
);
363 init_data
.default_label
= default_label
;
365 ret
= devm_led_classdev_register_ext(dev
, &led
->led_cdev
,
368 dev_err(dev
, "Failed to register LED for node %pfw\n",
379 static int pca963x_suspend(struct device
*dev
)
381 struct pca963x
*chip
= dev_get_drvdata(dev
);
384 reg
= i2c_smbus_read_byte_data(chip
->client
, PCA963X_MODE1
);
385 reg
= reg
| BIT(PCA963X_MODE1_SLEEP
);
386 i2c_smbus_write_byte_data(chip
->client
, PCA963X_MODE1
, reg
);
391 static int pca963x_resume(struct device
*dev
)
393 struct pca963x
*chip
= dev_get_drvdata(dev
);
396 reg
= i2c_smbus_read_byte_data(chip
->client
, PCA963X_MODE1
);
397 reg
= reg
& ~BIT(PCA963X_MODE1_SLEEP
);
398 i2c_smbus_write_byte_data(chip
->client
, PCA963X_MODE1
, reg
);
403 static DEFINE_SIMPLE_DEV_PM_OPS(pca963x_pm
, pca963x_suspend
, pca963x_resume
);
405 static const struct of_device_id of_pca963x_match
[] = {
406 { .compatible
= "nxp,pca9632", },
407 { .compatible
= "nxp,pca9633", },
408 { .compatible
= "nxp,pca9634", },
409 { .compatible
= "nxp,pca9635", },
412 MODULE_DEVICE_TABLE(of
, of_pca963x_match
);
414 static int pca963x_probe(struct i2c_client
*client
)
416 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
417 struct device
*dev
= &client
->dev
;
418 struct pca963x_chipdef
*chipdef
;
419 struct pca963x
*chip
;
422 chipdef
= &pca963x_chipdefs
[id
->driver_data
];
424 count
= device_get_child_node_count(dev
);
425 if (!count
|| count
> chipdef
->n_leds
) {
426 dev_err(dev
, "Node %pfw must define between 1 and %d LEDs\n",
427 dev_fwnode(dev
), chipdef
->n_leds
);
431 chip
= devm_kzalloc(dev
, struct_size(chip
, leds
, count
), GFP_KERNEL
);
435 i2c_set_clientdata(client
, chip
);
437 mutex_init(&chip
->mutex
);
438 chip
->chipdef
= chipdef
;
439 chip
->client
= client
;
441 /* Turn off LEDs by default*/
442 for (i
= 0; i
< chipdef
->n_leds
/ 4; i
++)
443 i2c_smbus_write_byte_data(client
, chipdef
->ledout_base
+ i
, 0x00);
445 /* Disable LED all-call address, and power down initially */
446 i2c_smbus_write_byte_data(client
, PCA963X_MODE1
, BIT(4));
448 return pca963x_register_leds(client
, chip
);
451 static struct i2c_driver pca963x_driver
= {
453 .name
= "leds-pca963x",
454 .of_match_table
= of_pca963x_match
,
455 .pm
= pm_sleep_ptr(&pca963x_pm
)
457 .probe
= pca963x_probe
,
458 .id_table
= pca963x_id
,
461 module_i2c_driver(pca963x_driver
);
463 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
464 MODULE_DESCRIPTION("PCA963X LED driver");
465 MODULE_LICENSE("GPL v2");