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 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 'PCA9633_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/workqueue.h>
36 #include <linux/slab.h>
38 #include <linux/platform_data/leds-pca9633.h>
40 /* LED select registers determine the source that drives LED outputs */
41 #define PCA9633_LED_OFF 0x0 /* LED driver off */
42 #define PCA9633_LED_ON 0x1 /* LED driver on */
43 #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */
44 #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
46 #define PCA9633_MODE2_DMBLNK 0x20 /* Enable blinking */
48 #define PCA9633_MODE1 0x00
49 #define PCA9633_MODE2 0x01
50 #define PCA9633_PWM_BASE 0x02
57 struct pca9633_chipdef {
64 static struct pca9633_chipdef pca9633_chipdefs[] = {
79 /* Total blink period in milliseconds */
80 #define PCA9632_BLINK_PERIOD_MIN 42
81 #define PCA9632_BLINK_PERIOD_MAX 10667
83 static const struct i2c_device_id pca9633_id[] = {
84 { "pca9632", pca9633 },
85 { "pca9633", pca9633 },
86 { "pca9634", pca9634 },
89 MODULE_DEVICE_TABLE(i2c, pca9633_id);
97 struct i2c_client *client;
98 struct pca9633_chipdef *chipdef;
99 struct work_struct work;
100 enum led_brightness brightness;
101 struct led_classdev led_cdev;
102 int led_num; /* 0 .. 7 potentially */
103 enum pca9633_cmd cmd;
109 static void pca9633_brightness_work(struct pca9633_led *pca9633)
111 u8 ledout_addr = pca9633->chipdef->ledout_base + (pca9633->led_num / 4);
113 int shift = 2 * (pca9633->led_num % 4);
114 u8 mask = 0x3 << shift;
116 ledout = i2c_smbus_read_byte_data(pca9633->client, ledout_addr);
117 switch (pca9633->brightness) {
119 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
120 (ledout & ~mask) | (PCA9633_LED_ON << shift));
123 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
127 i2c_smbus_write_byte_data(pca9633->client,
128 PCA9633_PWM_BASE + pca9633->led_num,
129 pca9633->brightness);
130 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
131 (ledout & ~mask) | (PCA9633_LED_PWM << shift));
136 static void pca9633_blink_work(struct pca9633_led *pca9633)
138 u8 ledout_addr = pca9633->chipdef->ledout_base + (pca9633->led_num / 4);
139 u8 ledout = i2c_smbus_read_byte_data(pca9633->client, ledout_addr);
140 u8 mode2 = i2c_smbus_read_byte_data(pca9633->client, PCA9633_MODE2);
141 int shift = 2 * (pca9633->led_num % 4);
142 u8 mask = 0x3 << shift;
144 i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grppwm,
147 i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grpfreq,
150 if (!(mode2 & PCA9633_MODE2_DMBLNK))
151 i2c_smbus_write_byte_data(pca9633->client, PCA9633_MODE2,
152 mode2 | PCA9633_MODE2_DMBLNK);
154 if ((ledout & mask) != (PCA9633_LED_GRP_PWM << shift))
155 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
156 (ledout & ~mask) | (PCA9633_LED_GRP_PWM << shift));
159 static void pca9633_work(struct work_struct *work)
161 struct pca9633_led *pca9633 = container_of(work,
162 struct pca9633_led, work);
164 switch (pca9633->cmd) {
166 pca9633_brightness_work(pca9633);
169 pca9633_blink_work(pca9633);
174 static void pca9633_led_set(struct led_classdev *led_cdev,
175 enum led_brightness value)
177 struct pca9633_led *pca9633;
179 pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
181 pca9633->cmd = BRIGHTNESS_SET;
182 pca9633->brightness = value;
185 * Must use workqueue for the actual I/O since I2C operations
188 schedule_work(&pca9633->work);
191 static int pca9633_blink_set(struct led_classdev *led_cdev,
192 unsigned long *delay_on, unsigned long *delay_off)
194 struct pca9633_led *pca9633;
195 unsigned long time_on, time_off, period;
198 pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
201 time_off = *delay_off;
203 /* If both zero, pick reasonable defaults of 500ms each */
204 if (!time_on && !time_off) {
209 period = time_on + time_off;
211 /* If period not supported by hardware, default to someting sane. */
212 if ((period < PCA9632_BLINK_PERIOD_MIN) ||
213 (period > PCA9632_BLINK_PERIOD_MAX)) {
216 period = time_on + time_off;
220 * From manual: duty cycle = (GDC / 256) ->
221 * (time_on / period) = (GDC / 256) ->
222 * GDC = ((time_on * 256) / period)
224 gdc = (time_on * 256) / period;
227 * From manual: period = ((GFRQ + 1) / 24) in seconds.
228 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
229 * GFRQ = ((period * 24 / 1000) - 1)
231 gfrq = (period * 24 / 1000) - 1;
233 pca9633->cmd = BLINK_SET;
235 pca9633->gfrq = gfrq;
238 * Must use workqueue for the actual I/O since I2C operations
241 schedule_work(&pca9633->work);
244 *delay_off = time_off;
249 #if IS_ENABLED(CONFIG_OF)
250 static struct pca9633_platform_data *
251 pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip)
253 struct device_node *np = client->dev.of_node, *child;
254 struct pca9633_platform_data *pdata;
255 struct led_info *pca9633_leds;
258 count = of_get_child_count(np);
259 if (!count || count > chip->n_leds)
260 return ERR_PTR(-ENODEV);
262 pca9633_leds = devm_kzalloc(&client->dev,
263 sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
265 return ERR_PTR(-ENOMEM);
267 for_each_child_of_node(np, child) {
273 of_get_property(child, "label", NULL) ? : child->name;
274 led.default_trigger =
275 of_get_property(child, "linux,default-trigger", NULL);
276 res = of_property_read_u32(child, "reg", ®);
279 pca9633_leds[reg] = led;
281 pdata = devm_kzalloc(&client->dev,
282 sizeof(struct pca9633_platform_data), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 pdata->leds.leds = pca9633_leds;
287 pdata->leds.num_leds = count;
289 /* default to open-drain unless totem pole (push-pull) is specified */
290 if (of_property_read_bool(np, "nxp,totem-pole"))
291 pdata->outdrv = PCA9633_TOTEM_POLE;
293 pdata->outdrv = PCA9633_OPEN_DRAIN;
295 /* default to software blinking unless hardware blinking is specified */
296 if (of_property_read_bool(np, "nxp,hw-blink"))
297 pdata->blink_type = PCA9633_HW_BLINK;
299 pdata->blink_type = PCA9633_SW_BLINK;
304 static const struct of_device_id of_pca9633_match[] = {
305 { .compatible = "nxp,pca9632", },
306 { .compatible = "nxp,pca9633", },
307 { .compatible = "nxp,pca9634", },
311 static struct pca9633_platform_data *
312 pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip)
314 return ERR_PTR(-ENODEV);
318 static int pca9633_probe(struct i2c_client *client,
319 const struct i2c_device_id *id)
321 struct pca9633_led *pca9633;
322 struct pca9633_platform_data *pdata;
323 struct pca9633_chipdef *chip;
326 chip = &pca9633_chipdefs[id->driver_data];
327 pdata = dev_get_platdata(&client->dev);
330 pdata = pca9633_dt_init(client, chip);
332 dev_warn(&client->dev, "could not parse configuration\n");
337 if (pdata && (pdata->leds.num_leds < 1 ||
338 pdata->leds.num_leds > chip->n_leds)) {
339 dev_err(&client->dev, "board info must claim 1-%d LEDs",
344 pca9633 = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca9633),
349 i2c_set_clientdata(client, pca9633);
351 for (i = 0; i < chip->n_leds; i++) {
352 pca9633[i].client = client;
353 pca9633[i].led_num = i;
354 pca9633[i].chipdef = chip;
356 /* Platform data can specify LED names and default triggers */
357 if (pdata && i < pdata->leds.num_leds) {
358 if (pdata->leds.leds[i].name)
359 snprintf(pca9633[i].name,
360 sizeof(pca9633[i].name), "pca9633:%s",
361 pdata->leds.leds[i].name);
362 if (pdata->leds.leds[i].default_trigger)
363 pca9633[i].led_cdev.default_trigger =
364 pdata->leds.leds[i].default_trigger;
366 if (!pdata || i >= pdata->leds.num_leds ||
367 !pdata->leds.leds[i].name)
368 snprintf(pca9633[i].name, sizeof(pca9633[i].name),
369 "pca9633:%d:%.2x:%d", client->adapter->nr,
372 pca9633[i].led_cdev.name = pca9633[i].name;
373 pca9633[i].led_cdev.brightness_set = pca9633_led_set;
375 if (pdata && pdata->blink_type == PCA9633_HW_BLINK)
376 pca9633[i].led_cdev.blink_set = pca9633_blink_set;
378 INIT_WORK(&pca9633[i].work, pca9633_work);
380 err = led_classdev_register(&client->dev, &pca9633[i].led_cdev);
385 /* Disable LED all-call address and set normal mode */
386 i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00);
388 /* Configure output: open-drain or totem pole (push-pull) */
389 if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN)
390 i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01);
393 i2c_smbus_write_byte_data(client, chip->ledout_base, 0x00);
394 if (chip->n_leds > 4)
395 i2c_smbus_write_byte_data(client, chip->ledout_base + 1, 0x00);
401 led_classdev_unregister(&pca9633[i].led_cdev);
402 cancel_work_sync(&pca9633[i].work);
408 static int pca9633_remove(struct i2c_client *client)
410 struct pca9633_led *pca9633 = i2c_get_clientdata(client);
413 for (i = 0; i < pca9633->chipdef->n_leds; i++)
414 if (pca9633[i].client != NULL) {
415 led_classdev_unregister(&pca9633[i].led_cdev);
416 cancel_work_sync(&pca9633[i].work);
422 static struct i2c_driver pca9633_driver = {
424 .name = "leds-pca9633",
425 .owner = THIS_MODULE,
426 .of_match_table = of_match_ptr(of_pca9633_match),
428 .probe = pca9633_probe,
429 .remove = pca9633_remove,
430 .id_table = pca9633_id,
433 module_i2c_driver(pca9633_driver);
435 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
436 MODULE_DESCRIPTION("PCA9633 LED driver");
437 MODULE_LICENSE("GPL v2");