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/acpi.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/string.h>
32 #include <linux/ctype.h>
33 #include <linux/leds.h>
34 #include <linux/err.h>
35 #include <linux/i2c.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_OUTDRV 0x04 /* Open-drain or totem pole */
47 #define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
48 #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
50 #define PCA963X_MODE1 0x00
51 #define PCA963X_MODE2 0x01
52 #define PCA963X_PWM_BASE 0x02
60 struct pca963x_chipdef {
68 static struct pca963x_chipdef pca963x_chipdefs[] = {
89 /* Total blink period in milliseconds */
90 #define PCA963X_BLINK_PERIOD_MIN 42
91 #define PCA963X_BLINK_PERIOD_MAX 10667
93 static const struct i2c_device_id pca963x_id[] = {
94 { "pca9632", pca9633 },
95 { "pca9633", pca9633 },
96 { "pca9634", pca9634 },
97 { "pca9635", pca9635 },
100 MODULE_DEVICE_TABLE(i2c, pca963x_id);
102 static const struct acpi_device_id pca963x_acpi_ids[] = {
103 { "PCA9632", pca9633 },
104 { "PCA9633", pca9633 },
105 { "PCA9634", pca9634 },
106 { "PCA9635", pca9635 },
109 MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids);
114 struct pca963x_chipdef *chipdef;
116 struct i2c_client *client;
117 struct pca963x_led *leds;
118 unsigned long leds_on;
122 struct pca963x *chip;
123 struct led_classdev led_cdev;
124 int led_num; /* 0 .. 15 potentially */
130 static int pca963x_brightness(struct pca963x_led *pca963x,
131 enum led_brightness brightness)
133 u8 ledout_addr = pca963x->chip->chipdef->ledout_base
134 + (pca963x->led_num / 4);
136 int shift = 2 * (pca963x->led_num % 4);
137 u8 mask = 0x3 << shift;
140 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
141 switch (brightness) {
143 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
145 (ledout & ~mask) | (PCA963X_LED_ON << shift));
148 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
149 ledout_addr, ledout & ~mask);
152 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
153 PCA963X_PWM_BASE + pca963x->led_num,
157 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
159 (ledout & ~mask) | (PCA963X_LED_PWM << shift));
166 static void pca963x_blink(struct pca963x_led *pca963x)
168 u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
169 (pca963x->led_num / 4);
171 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
173 int shift = 2 * (pca963x->led_num % 4);
174 u8 mask = 0x3 << shift;
176 i2c_smbus_write_byte_data(pca963x->chip->client,
177 pca963x->chip->chipdef->grppwm, pca963x->gdc);
179 i2c_smbus_write_byte_data(pca963x->chip->client,
180 pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
182 if (!(mode2 & PCA963X_MODE2_DMBLNK))
183 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
184 mode2 | PCA963X_MODE2_DMBLNK);
186 mutex_lock(&pca963x->chip->mutex);
187 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
188 if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
189 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
190 (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
191 mutex_unlock(&pca963x->chip->mutex);
194 static int pca963x_power_state(struct pca963x_led *pca963x)
196 unsigned long *leds_on = &pca963x->chip->leds_on;
197 unsigned long cached_leds = pca963x->chip->leds_on;
199 if (pca963x->led_cdev.brightness)
200 set_bit(pca963x->led_num, leds_on);
202 clear_bit(pca963x->led_num, leds_on);
204 if (!(*leds_on) != !cached_leds)
205 return i2c_smbus_write_byte_data(pca963x->chip->client,
206 PCA963X_MODE1, *leds_on ? 0 : BIT(4));
211 static int pca963x_led_set(struct led_classdev *led_cdev,
212 enum led_brightness value)
214 struct pca963x_led *pca963x;
217 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
219 mutex_lock(&pca963x->chip->mutex);
221 ret = pca963x_brightness(pca963x, value);
224 ret = pca963x_power_state(pca963x);
227 mutex_unlock(&pca963x->chip->mutex);
231 static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
234 unsigned int scaling = pca963x->chip->chipdef->scaling;
236 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
239 static int pca963x_blink_set(struct led_classdev *led_cdev,
240 unsigned long *delay_on, unsigned long *delay_off)
242 struct pca963x_led *pca963x;
243 unsigned long time_on, time_off, period;
246 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
249 time_off = *delay_off;
251 /* If both zero, pick reasonable defaults of 500ms each */
252 if (!time_on && !time_off) {
257 period = pca963x_period_scale(pca963x, time_on + time_off);
259 /* If period not supported by hardware, default to someting sane. */
260 if ((period < PCA963X_BLINK_PERIOD_MIN) ||
261 (period > PCA963X_BLINK_PERIOD_MAX)) {
264 period = pca963x_period_scale(pca963x, 1000);
268 * From manual: duty cycle = (GDC / 256) ->
269 * (time_on / period) = (GDC / 256) ->
270 * GDC = ((time_on * 256) / period)
272 gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
275 * From manual: period = ((GFRQ + 1) / 24) in seconds.
276 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
277 * GFRQ = ((period * 24 / 1000) - 1)
279 gfrq = (period * 24 / 1000) - 1;
282 pca963x->gfrq = gfrq;
284 pca963x_blink(pca963x);
287 *delay_off = time_off;
292 #if IS_ENABLED(CONFIG_OF)
293 static struct pca963x_platform_data *
294 pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
296 struct device_node *np = client->dev.of_node, *child;
297 struct pca963x_platform_data *pdata;
298 struct led_info *pca963x_leds;
301 count = of_get_child_count(np);
302 if (!count || count > chip->n_leds)
303 return ERR_PTR(-ENODEV);
305 pca963x_leds = devm_kzalloc(&client->dev,
306 sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
308 return ERR_PTR(-ENOMEM);
310 for_each_child_of_node(np, child) {
311 struct led_info led = {};
315 res = of_property_read_u32(child, "reg", ®);
316 if ((res != 0) || (reg >= chip->n_leds))
319 of_get_property(child, "label", NULL) ? : child->name;
320 led.default_trigger =
321 of_get_property(child, "linux,default-trigger", NULL);
322 pca963x_leds[reg] = led;
324 pdata = devm_kzalloc(&client->dev,
325 sizeof(struct pca963x_platform_data), GFP_KERNEL);
327 return ERR_PTR(-ENOMEM);
329 pdata->leds.leds = pca963x_leds;
330 pdata->leds.num_leds = chip->n_leds;
332 /* default to open-drain unless totem pole (push-pull) is specified */
333 if (of_property_read_bool(np, "nxp,totem-pole"))
334 pdata->outdrv = PCA963X_TOTEM_POLE;
336 pdata->outdrv = PCA963X_OPEN_DRAIN;
338 /* default to software blinking unless hardware blinking is specified */
339 if (of_property_read_bool(np, "nxp,hw-blink"))
340 pdata->blink_type = PCA963X_HW_BLINK;
342 pdata->blink_type = PCA963X_SW_BLINK;
344 if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling))
345 chip->scaling = 1000;
347 /* default to non-inverted output, unless inverted is specified */
348 if (of_property_read_bool(np, "nxp,inverted-out"))
349 pdata->dir = PCA963X_INVERTED;
351 pdata->dir = PCA963X_NORMAL;
356 static const struct of_device_id of_pca963x_match[] = {
357 { .compatible = "nxp,pca9632", },
358 { .compatible = "nxp,pca9633", },
359 { .compatible = "nxp,pca9634", },
360 { .compatible = "nxp,pca9635", },
363 MODULE_DEVICE_TABLE(of, of_pca963x_match);
365 static struct pca963x_platform_data *
366 pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
368 return ERR_PTR(-ENODEV);
372 static int pca963x_probe(struct i2c_client *client,
373 const struct i2c_device_id *id)
375 struct pca963x *pca963x_chip;
376 struct pca963x_led *pca963x;
377 struct pca963x_platform_data *pdata;
378 struct pca963x_chipdef *chip;
382 chip = &pca963x_chipdefs[id->driver_data];
384 const struct acpi_device_id *acpi_id;
386 acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev);
389 chip = &pca963x_chipdefs[acpi_id->driver_data];
391 pdata = dev_get_platdata(&client->dev);
394 pdata = pca963x_dt_init(client, chip);
396 dev_warn(&client->dev, "could not parse configuration\n");
401 if (pdata && (pdata->leds.num_leds < 1 ||
402 pdata->leds.num_leds > chip->n_leds)) {
403 dev_err(&client->dev, "board info must claim 1-%d LEDs",
408 pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
412 pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x),
417 i2c_set_clientdata(client, pca963x_chip);
419 mutex_init(&pca963x_chip->mutex);
420 pca963x_chip->chipdef = chip;
421 pca963x_chip->client = client;
422 pca963x_chip->leds = pca963x;
424 /* Turn off LEDs by default*/
425 for (i = 0; i < chip->n_leds / 4; i++)
426 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
428 for (i = 0; i < chip->n_leds; i++) {
429 pca963x[i].led_num = i;
430 pca963x[i].chip = pca963x_chip;
432 /* Platform data can specify LED names and default triggers */
433 if (pdata && i < pdata->leds.num_leds) {
434 if (pdata->leds.leds[i].name)
435 snprintf(pca963x[i].name,
436 sizeof(pca963x[i].name), "pca963x:%s",
437 pdata->leds.leds[i].name);
438 if (pdata->leds.leds[i].default_trigger)
439 pca963x[i].led_cdev.default_trigger =
440 pdata->leds.leds[i].default_trigger;
442 if (!pdata || i >= pdata->leds.num_leds ||
443 !pdata->leds.leds[i].name)
444 snprintf(pca963x[i].name, sizeof(pca963x[i].name),
445 "pca963x:%d:%.2x:%d", client->adapter->nr,
448 pca963x[i].led_cdev.name = pca963x[i].name;
449 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
451 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
452 pca963x[i].led_cdev.blink_set = pca963x_blink_set;
454 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
459 /* Disable LED all-call address, and power down initially */
460 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
463 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
465 /* Configure output: open-drain or totem pole (push-pull) */
466 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
467 mode2 &= ~PCA963X_MODE2_OUTDRV;
469 mode2 |= PCA963X_MODE2_OUTDRV;
470 /* Configure direction: normal or inverted */
471 if (pdata->dir == PCA963X_INVERTED)
472 mode2 |= PCA963X_MODE2_INVRT;
473 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
481 led_classdev_unregister(&pca963x[i].led_cdev);
486 static int pca963x_remove(struct i2c_client *client)
488 struct pca963x *pca963x = i2c_get_clientdata(client);
491 for (i = 0; i < pca963x->chipdef->n_leds; i++)
492 led_classdev_unregister(&pca963x->leds[i].led_cdev);
497 static struct i2c_driver pca963x_driver = {
499 .name = "leds-pca963x",
500 .of_match_table = of_match_ptr(of_pca963x_match),
501 .acpi_match_table = ACPI_PTR(pca963x_acpi_ids),
503 .probe = pca963x_probe,
504 .remove = pca963x_remove,
505 .id_table = pca963x_id,
508 module_i2c_driver(pca963x_driver);
510 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
511 MODULE_DESCRIPTION("PCA963X LED driver");
512 MODULE_LICENSE("GPL v2");