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/property.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 {
66 static struct pca963x_chipdef pca963x_chipdefs[] = {
87 /* Total blink period in milliseconds */
88 #define PCA963X_BLINK_PERIOD_MIN 42
89 #define PCA963X_BLINK_PERIOD_MAX 10667
91 static const struct i2c_device_id pca963x_id[] = {
92 { "pca9632", pca9633 },
93 { "pca9633", pca9633 },
94 { "pca9634", pca9634 },
95 { "pca9635", pca9635 },
98 MODULE_DEVICE_TABLE(i2c, pca963x_id);
103 struct pca963x_chipdef *chipdef;
105 struct i2c_client *client;
106 struct pca963x_led *leds;
107 unsigned long leds_on;
111 struct pca963x *chip;
112 struct led_classdev led_cdev;
113 int led_num; /* 0 .. 15 potentially */
119 static int pca963x_brightness(struct pca963x_led *pca963x,
120 enum led_brightness brightness)
122 u8 ledout_addr = pca963x->chip->chipdef->ledout_base
123 + (pca963x->led_num / 4);
125 int shift = 2 * (pca963x->led_num % 4);
126 u8 mask = 0x3 << shift;
129 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
130 switch (brightness) {
132 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
134 (ledout & ~mask) | (PCA963X_LED_ON << shift));
137 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
138 ledout_addr, ledout & ~mask);
141 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
142 PCA963X_PWM_BASE + pca963x->led_num,
146 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
148 (ledout & ~mask) | (PCA963X_LED_PWM << shift));
155 static void pca963x_blink(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 int pca963x_power_state(struct pca963x_led *pca963x)
185 unsigned long *leds_on = &pca963x->chip->leds_on;
186 unsigned long cached_leds = pca963x->chip->leds_on;
188 if (pca963x->led_cdev.brightness)
189 set_bit(pca963x->led_num, leds_on);
191 clear_bit(pca963x->led_num, leds_on);
193 if (!(*leds_on) != !cached_leds)
194 return i2c_smbus_write_byte_data(pca963x->chip->client,
195 PCA963X_MODE1, *leds_on ? 0 : BIT(4));
200 static int pca963x_led_set(struct led_classdev *led_cdev,
201 enum led_brightness value)
203 struct pca963x_led *pca963x;
206 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
208 mutex_lock(&pca963x->chip->mutex);
210 ret = pca963x_brightness(pca963x, value);
213 ret = pca963x_power_state(pca963x);
216 mutex_unlock(&pca963x->chip->mutex);
220 static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
223 unsigned int scaling = pca963x->chip->chipdef->scaling;
225 return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
228 static int pca963x_blink_set(struct led_classdev *led_cdev,
229 unsigned long *delay_on, unsigned long *delay_off)
231 struct pca963x_led *pca963x;
232 unsigned long time_on, time_off, period;
235 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
238 time_off = *delay_off;
240 /* If both zero, pick reasonable defaults of 500ms each */
241 if (!time_on && !time_off) {
246 period = pca963x_period_scale(pca963x, time_on + time_off);
248 /* If period not supported by hardware, default to someting sane. */
249 if ((period < PCA963X_BLINK_PERIOD_MIN) ||
250 (period > PCA963X_BLINK_PERIOD_MAX)) {
253 period = pca963x_period_scale(pca963x, 1000);
257 * From manual: duty cycle = (GDC / 256) ->
258 * (time_on / period) = (GDC / 256) ->
259 * GDC = ((time_on * 256) / period)
261 gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
264 * From manual: period = ((GFRQ + 1) / 24) in seconds.
265 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
266 * GFRQ = ((period * 24 / 1000) - 1)
268 gfrq = (period * 24 / 1000) - 1;
271 pca963x->gfrq = gfrq;
273 pca963x_blink(pca963x);
276 *delay_off = time_off;
281 static struct pca963x_platform_data *
282 pca963x_get_pdata(struct i2c_client *client, struct pca963x_chipdef *chip)
284 struct pca963x_platform_data *pdata;
285 struct led_info *pca963x_leds;
286 struct fwnode_handle *child;
289 count = device_get_child_node_count(&client->dev);
290 if (!count || count > chip->n_leds)
291 return ERR_PTR(-ENODEV);
293 pca963x_leds = devm_kcalloc(&client->dev,
294 chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
296 return ERR_PTR(-ENOMEM);
298 device_for_each_child_node(&client->dev, child) {
299 struct led_info led = {};
303 res = fwnode_property_read_u32(child, "reg", ®);
304 if ((res != 0) || (reg >= chip->n_leds))
307 res = fwnode_property_read_string(child, "label", &led.name);
308 if ((res != 0) && is_of_node(child))
309 led.name = to_of_node(child)->name;
311 fwnode_property_read_string(child, "linux,default-trigger",
312 &led.default_trigger);
314 pca963x_leds[reg] = led;
316 pdata = devm_kzalloc(&client->dev,
317 sizeof(struct pca963x_platform_data), GFP_KERNEL);
319 return ERR_PTR(-ENOMEM);
321 pdata->leds.leds = pca963x_leds;
322 pdata->leds.num_leds = chip->n_leds;
324 /* default to open-drain unless totem pole (push-pull) is specified */
325 if (device_property_read_bool(&client->dev, "nxp,totem-pole"))
326 pdata->outdrv = PCA963X_TOTEM_POLE;
328 pdata->outdrv = PCA963X_OPEN_DRAIN;
330 /* default to software blinking unless hardware blinking is specified */
331 if (device_property_read_bool(&client->dev, "nxp,hw-blink"))
332 pdata->blink_type = PCA963X_HW_BLINK;
334 pdata->blink_type = PCA963X_SW_BLINK;
336 if (device_property_read_u32(&client->dev, "nxp,period-scale",
338 chip->scaling = 1000;
340 /* default to non-inverted output, unless inverted is specified */
341 if (device_property_read_bool(&client->dev, "nxp,inverted-out"))
342 pdata->dir = PCA963X_INVERTED;
344 pdata->dir = PCA963X_NORMAL;
349 static const struct of_device_id of_pca963x_match[] = {
350 { .compatible = "nxp,pca9632", },
351 { .compatible = "nxp,pca9633", },
352 { .compatible = "nxp,pca9634", },
353 { .compatible = "nxp,pca9635", },
356 MODULE_DEVICE_TABLE(of, of_pca963x_match);
358 static int pca963x_probe(struct i2c_client *client,
359 const struct i2c_device_id *id)
361 struct pca963x *pca963x_chip;
362 struct pca963x_led *pca963x;
363 struct pca963x_platform_data *pdata;
364 struct pca963x_chipdef *chip;
367 chip = &pca963x_chipdefs[id->driver_data];
368 pdata = dev_get_platdata(&client->dev);
371 pdata = pca963x_get_pdata(client, chip);
373 dev_warn(&client->dev, "could not parse configuration\n");
378 if (pdata && (pdata->leds.num_leds < 1 ||
379 pdata->leds.num_leds > chip->n_leds)) {
380 dev_err(&client->dev, "board info must claim 1-%d LEDs",
385 pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
389 pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
394 i2c_set_clientdata(client, pca963x_chip);
396 mutex_init(&pca963x_chip->mutex);
397 pca963x_chip->chipdef = chip;
398 pca963x_chip->client = client;
399 pca963x_chip->leds = pca963x;
401 /* Turn off LEDs by default*/
402 for (i = 0; i < chip->n_leds / 4; i++)
403 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
405 for (i = 0; i < chip->n_leds; i++) {
406 pca963x[i].led_num = i;
407 pca963x[i].chip = pca963x_chip;
409 /* Platform data can specify LED names and default triggers */
410 if (pdata && i < pdata->leds.num_leds) {
411 if (pdata->leds.leds[i].name)
412 snprintf(pca963x[i].name,
413 sizeof(pca963x[i].name), "pca963x:%s",
414 pdata->leds.leds[i].name);
415 if (pdata->leds.leds[i].default_trigger)
416 pca963x[i].led_cdev.default_trigger =
417 pdata->leds.leds[i].default_trigger;
419 if (!pdata || i >= pdata->leds.num_leds ||
420 !pdata->leds.leds[i].name)
421 snprintf(pca963x[i].name, sizeof(pca963x[i].name),
422 "pca963x:%d:%.2x:%d", client->adapter->nr,
425 pca963x[i].led_cdev.name = pca963x[i].name;
426 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
428 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
429 pca963x[i].led_cdev.blink_set = pca963x_blink_set;
431 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
436 /* Disable LED all-call address, and power down initially */
437 i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
440 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
442 /* Configure output: open-drain or totem pole (push-pull) */
443 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
447 /* Configure direction: normal or inverted */
448 if (pdata->dir == PCA963X_INVERTED)
450 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
458 led_classdev_unregister(&pca963x[i].led_cdev);
463 static int pca963x_remove(struct i2c_client *client)
465 struct pca963x *pca963x = i2c_get_clientdata(client);
468 for (i = 0; i < pca963x->chipdef->n_leds; i++)
469 led_classdev_unregister(&pca963x->leds[i].led_cdev);
474 static struct i2c_driver pca963x_driver = {
476 .name = "leds-pca963x",
477 .of_match_table = of_pca963x_match,
479 .probe = pca963x_probe,
480 .remove = pca963x_remove,
481 .id_table = pca963x_id,
484 module_i2c_driver(pca963x_driver);
486 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
487 MODULE_DESCRIPTION("PCA963X LED driver");
488 MODULE_LICENSE("GPL v2");