]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/leds/leds-pca963x.c
leds:lp55xx: use the private data instead of updating I2C device platform data
[mirror_ubuntu-artful-kernel.git] / drivers / leds / leds-pca963x.c
CommitLineData
75cb2e1d
PM
1/*
2 * Copyright 2011 bct electronic GmbH
af67384f 3 * Copyright 2013 Qtechnology/AS
75cb2e1d
PM
4 *
5 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
af67384f 6 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
75cb2e1d
PM
7 *
8 * Based on leds-pca955x.c
9 *
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.
13 *
14 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
3dfedb9d 15 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
75cb2e1d 16 *
8465b018
MG
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
56a1740c 24 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
8465b018 25 * or by adding the 'nxp,hw-blink' property to the DTS.
75cb2e1d
PM
26 */
27
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>
81d22878 37#include <linux/of.h>
56a1740c 38#include <linux/platform_data/leds-pca963x.h>
75cb2e1d
PM
39
40/* LED select registers determine the source that drives LED outputs */
56a1740c
RRD
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 */
75cb2e1d 45
56a1740c 46#define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
8465b018 47
56a1740c
RRD
48#define PCA963X_MODE1 0x00
49#define PCA963X_MODE2 0x01
50#define PCA963X_PWM_BASE 0x02
af67384f 51
56a1740c 52enum pca963x_type {
af67384f
RRD
53 pca9633,
54 pca9634,
3dfedb9d 55 pca9635,
af67384f
RRD
56};
57
56a1740c 58struct pca963x_chipdef {
af67384f
RRD
59 u8 grppwm;
60 u8 grpfreq;
61 u8 ledout_base;
62 int n_leds;
63};
64
56a1740c 65static struct pca963x_chipdef pca963x_chipdefs[] = {
af67384f
RRD
66 [pca9633] = {
67 .grppwm = 0x6,
68 .grpfreq = 0x7,
69 .ledout_base = 0x8,
70 .n_leds = 4,
71 },
72 [pca9634] = {
73 .grppwm = 0xa,
74 .grpfreq = 0xb,
75 .ledout_base = 0xc,
76 .n_leds = 8,
77 },
3dfedb9d
PM
78 [pca9635] = {
79 .grppwm = 0x12,
80 .grpfreq = 0x13,
81 .ledout_base = 0x14,
82 .n_leds = 16,
83 },
af67384f 84};
75cb2e1d 85
8465b018 86/* Total blink period in milliseconds */
56a1740c
RRD
87#define PCA963X_BLINK_PERIOD_MIN 42
88#define PCA963X_BLINK_PERIOD_MAX 10667
8465b018 89
56a1740c 90static const struct i2c_device_id pca963x_id[] = {
af67384f
RRD
91 { "pca9632", pca9633 },
92 { "pca9633", pca9633 },
93 { "pca9634", pca9634 },
3dfedb9d 94 { "pca9635", pca9635 },
75cb2e1d
PM
95 { }
96};
56a1740c 97MODULE_DEVICE_TABLE(i2c, pca963x_id);
75cb2e1d 98
56a1740c 99enum pca963x_cmd {
8465b018
MG
100 BRIGHTNESS_SET,
101 BLINK_SET,
102};
103
56a1740c 104struct pca963x_led;
a7d0e988 105
56a1740c
RRD
106struct pca963x {
107 struct pca963x_chipdef *chipdef;
a7d0e988
RRD
108 struct mutex mutex;
109 struct i2c_client *client;
56a1740c 110 struct pca963x_led *leds;
a7d0e988
RRD
111};
112
56a1740c
RRD
113struct pca963x_led {
114 struct pca963x *chip;
75cb2e1d
PM
115 struct work_struct work;
116 enum led_brightness brightness;
117 struct led_classdev led_cdev;
3dfedb9d 118 int led_num; /* 0 .. 15 potentially */
56a1740c 119 enum pca963x_cmd cmd;
75cb2e1d 120 char name[32];
8465b018
MG
121 u8 gdc;
122 u8 gfrq;
75cb2e1d
PM
123};
124
56a1740c 125static void pca963x_brightness_work(struct pca963x_led *pca963x)
75cb2e1d 126{
56a1740c
RRD
127 u8 ledout_addr = pca963x->chip->chipdef->ledout_base
128 + (pca963x->led_num / 4);
af67384f 129 u8 ledout;
56a1740c 130 int shift = 2 * (pca963x->led_num % 4);
75cb2e1d
PM
131 u8 mask = 0x3 << shift;
132
56a1740c
RRD
133 mutex_lock(&pca963x->chip->mutex);
134 ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
135 switch (pca963x->brightness) {
75cb2e1d 136 case LED_FULL:
56a1740c
RRD
137 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
138 (ledout & ~mask) | (PCA963X_LED_ON << shift));
75cb2e1d
PM
139 break;
140 case LED_OFF:
56a1740c 141 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
75cb2e1d
PM
142 ledout & ~mask);
143 break;
144 default:
56a1740c
RRD
145 i2c_smbus_write_byte_data(pca963x->chip->client,
146 PCA963X_PWM_BASE + pca963x->led_num,
147 pca963x->brightness);
148 i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
149 (ledout & ~mask) | (PCA963X_LED_PWM << shift));
75cb2e1d
PM
150 break;
151 }
56a1740c 152 mutex_unlock(&pca963x->chip->mutex);
75cb2e1d
PM
153}
154
56a1740c 155static void pca963x_blink_work(struct pca963x_led *pca963x)
8465b018 156{
56a1740c
RRD
157 u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
158 (pca963x->led_num / 4);
a7d0e988 159 u8 ledout;
56a1740c
RRD
160 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
161 PCA963X_MODE2);
162 int shift = 2 * (pca963x->led_num % 4);
8465b018
MG
163 u8 mask = 0x3 << shift;
164
56a1740c
RRD
165 i2c_smbus_write_byte_data(pca963x->chip->client,
166 pca963x->chip->chipdef->grppwm, pca963x->gdc);
8465b018 167
56a1740c
RRD
168 i2c_smbus_write_byte_data(pca963x->chip->client,
169 pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
8465b018 170
56a1740c
RRD
171 if (!(mode2 & PCA963X_MODE2_DMBLNK))
172 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
173 mode2 | PCA963X_MODE2_DMBLNK);
8465b018 174
56a1740c
RRD
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);
8465b018
MG
181}
182
56a1740c 183static void pca963x_work(struct work_struct *work)
8465b018 184{
56a1740c
RRD
185 struct pca963x_led *pca963x = container_of(work,
186 struct pca963x_led, work);
8465b018 187
56a1740c 188 switch (pca963x->cmd) {
8465b018 189 case BRIGHTNESS_SET:
56a1740c 190 pca963x_brightness_work(pca963x);
8465b018
MG
191 break;
192 case BLINK_SET:
56a1740c 193 pca963x_blink_work(pca963x);
8465b018
MG
194 break;
195 }
196}
197
56a1740c 198static void pca963x_led_set(struct led_classdev *led_cdev,
75cb2e1d
PM
199 enum led_brightness value)
200{
56a1740c 201 struct pca963x_led *pca963x;
75cb2e1d 202
56a1740c 203 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
75cb2e1d 204
56a1740c
RRD
205 pca963x->cmd = BRIGHTNESS_SET;
206 pca963x->brightness = value;
75cb2e1d
PM
207
208 /*
209 * Must use workqueue for the actual I/O since I2C operations
210 * can sleep.
211 */
56a1740c 212 schedule_work(&pca963x->work);
75cb2e1d
PM
213}
214
56a1740c 215static int pca963x_blink_set(struct led_classdev *led_cdev,
8465b018
MG
216 unsigned long *delay_on, unsigned long *delay_off)
217{
56a1740c 218 struct pca963x_led *pca963x;
8465b018
MG
219 unsigned long time_on, time_off, period;
220 u8 gdc, gfrq;
221
56a1740c 222 pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
8465b018
MG
223
224 time_on = *delay_on;
225 time_off = *delay_off;
226
227 /* If both zero, pick reasonable defaults of 500ms each */
228 if (!time_on && !time_off) {
229 time_on = 500;
230 time_off = 500;
231 }
232
233 period = time_on + time_off;
234
235 /* If period not supported by hardware, default to someting sane. */
56a1740c
RRD
236 if ((period < PCA963X_BLINK_PERIOD_MIN) ||
237 (period > PCA963X_BLINK_PERIOD_MAX)) {
8465b018
MG
238 time_on = 500;
239 time_off = 500;
240 period = time_on + time_off;
241 }
242
243 /*
244 * From manual: duty cycle = (GDC / 256) ->
245 * (time_on / period) = (GDC / 256) ->
246 * GDC = ((time_on * 256) / period)
247 */
248 gdc = (time_on * 256) / period;
249
250 /*
251 * From manual: period = ((GFRQ + 1) / 24) in seconds.
252 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
253 * GFRQ = ((period * 24 / 1000) - 1)
254 */
255 gfrq = (period * 24 / 1000) - 1;
256
56a1740c
RRD
257 pca963x->cmd = BLINK_SET;
258 pca963x->gdc = gdc;
259 pca963x->gfrq = gfrq;
8465b018
MG
260
261 /*
262 * Must use workqueue for the actual I/O since I2C operations
263 * can sleep.
264 */
56a1740c 265 schedule_work(&pca963x->work);
8465b018
MG
266
267 *delay_on = time_on;
268 *delay_off = time_off;
269
270 return 0;
271}
272
81d22878 273#if IS_ENABLED(CONFIG_OF)
56a1740c
RRD
274static struct pca963x_platform_data *
275pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
81d22878
TL
276{
277 struct device_node *np = client->dev.of_node, *child;
56a1740c
RRD
278 struct pca963x_platform_data *pdata;
279 struct led_info *pca963x_leds;
81d22878
TL
280 int count;
281
282 count = of_get_child_count(np);
af67384f 283 if (!count || count > chip->n_leds)
81d22878
TL
284 return ERR_PTR(-ENODEV);
285
56a1740c 286 pca963x_leds = devm_kzalloc(&client->dev,
af67384f 287 sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
56a1740c 288 if (!pca963x_leds)
81d22878
TL
289 return ERR_PTR(-ENOMEM);
290
291 for_each_child_of_node(np, child) {
a44b0f5e 292 struct led_info led = {};
81d22878
TL
293 u32 reg;
294 int res;
295
8a6acd64
RRD
296 res = of_property_read_u32(child, "reg", &reg);
297 if ((res != 0) || (reg >= chip->n_leds))
298 continue;
81d22878
TL
299 led.name =
300 of_get_property(child, "label", NULL) ? : child->name;
301 led.default_trigger =
302 of_get_property(child, "linux,default-trigger", NULL);
56a1740c 303 pca963x_leds[reg] = led;
81d22878
TL
304 }
305 pdata = devm_kzalloc(&client->dev,
56a1740c 306 sizeof(struct pca963x_platform_data), GFP_KERNEL);
81d22878
TL
307 if (!pdata)
308 return ERR_PTR(-ENOMEM);
309
56a1740c 310 pdata->leds.leds = pca963x_leds;
8a6acd64 311 pdata->leds.num_leds = chip->n_leds;
81d22878
TL
312
313 /* default to open-drain unless totem pole (push-pull) is specified */
314 if (of_property_read_bool(np, "nxp,totem-pole"))
56a1740c 315 pdata->outdrv = PCA963X_TOTEM_POLE;
81d22878 316 else
56a1740c 317 pdata->outdrv = PCA963X_OPEN_DRAIN;
81d22878 318
8465b018
MG
319 /* default to software blinking unless hardware blinking is specified */
320 if (of_property_read_bool(np, "nxp,hw-blink"))
56a1740c 321 pdata->blink_type = PCA963X_HW_BLINK;
8465b018 322 else
56a1740c 323 pdata->blink_type = PCA963X_SW_BLINK;
8465b018 324
81d22878
TL
325 return pdata;
326}
327
56a1740c 328static const struct of_device_id of_pca963x_match[] = {
af67384f
RRD
329 { .compatible = "nxp,pca9632", },
330 { .compatible = "nxp,pca9633", },
331 { .compatible = "nxp,pca9634", },
3dfedb9d 332 { .compatible = "nxp,pca9635", },
81d22878
TL
333 {},
334};
335#else
56a1740c
RRD
336static struct pca963x_platform_data *
337pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
81d22878
TL
338{
339 return ERR_PTR(-ENODEV);
340}
341#endif
342
56a1740c 343static int pca963x_probe(struct i2c_client *client,
75cb2e1d
PM
344 const struct i2c_device_id *id)
345{
56a1740c
RRD
346 struct pca963x *pca963x_chip;
347 struct pca963x_led *pca963x;
348 struct pca963x_platform_data *pdata;
349 struct pca963x_chipdef *chip;
75cb2e1d
PM
350 int i, err;
351
56a1740c 352 chip = &pca963x_chipdefs[id->driver_data];
87aae1ea 353 pdata = dev_get_platdata(&client->dev);
75cb2e1d 354
81d22878 355 if (!pdata) {
56a1740c 356 pdata = pca963x_dt_init(client, chip);
81d22878
TL
357 if (IS_ERR(pdata)) {
358 dev_warn(&client->dev, "could not parse configuration\n");
359 pdata = NULL;
360 }
361 }
362
af67384f
RRD
363 if (pdata && (pdata->leds.num_leds < 1 ||
364 pdata->leds.num_leds > chip->n_leds)) {
365 dev_err(&client->dev, "board info must claim 1-%d LEDs",
366 chip->n_leds);
367 return -EINVAL;
75cb2e1d
PM
368 }
369
56a1740c 370 pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
a7d0e988 371 GFP_KERNEL);
56a1740c 372 if (!pca963x_chip)
a7d0e988 373 return -ENOMEM;
56a1740c 374 pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x),
af67384f 375 GFP_KERNEL);
56a1740c 376 if (!pca963x)
75cb2e1d
PM
377 return -ENOMEM;
378
56a1740c 379 i2c_set_clientdata(client, pca963x_chip);
a7d0e988 380
56a1740c
RRD
381 mutex_init(&pca963x_chip->mutex);
382 pca963x_chip->chipdef = chip;
383 pca963x_chip->client = client;
384 pca963x_chip->leds = pca963x;
a7d0e988
RRD
385
386 /* Turn off LEDs by default*/
3dfedb9d
PM
387 for (i = 0; i < chip->n_leds / 4; i++)
388 i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
75cb2e1d 389
af67384f 390 for (i = 0; i < chip->n_leds; i++) {
56a1740c
RRD
391 pca963x[i].led_num = i;
392 pca963x[i].chip = pca963x_chip;
75cb2e1d
PM
393
394 /* Platform data can specify LED names and default triggers */
2f73c392
PM
395 if (pdata && i < pdata->leds.num_leds) {
396 if (pdata->leds.leds[i].name)
56a1740c
RRD
397 snprintf(pca963x[i].name,
398 sizeof(pca963x[i].name), "pca963x:%s",
2f73c392
PM
399 pdata->leds.leds[i].name);
400 if (pdata->leds.leds[i].default_trigger)
56a1740c 401 pca963x[i].led_cdev.default_trigger =
2f73c392 402 pdata->leds.leds[i].default_trigger;
75cb2e1d 403 }
a5cd98b7
RRD
404 if (!pdata || i >= pdata->leds.num_leds ||
405 !pdata->leds.leds[i].name)
56a1740c
RRD
406 snprintf(pca963x[i].name, sizeof(pca963x[i].name),
407 "pca963x:%d:%.2x:%d", client->adapter->nr,
a5cd98b7 408 client->addr, i);
75cb2e1d 409
56a1740c
RRD
410 pca963x[i].led_cdev.name = pca963x[i].name;
411 pca963x[i].led_cdev.brightness_set = pca963x_led_set;
75cb2e1d 412
56a1740c
RRD
413 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
414 pca963x[i].led_cdev.blink_set = pca963x_blink_set;
8465b018 415
56a1740c 416 INIT_WORK(&pca963x[i].work, pca963x_work);
75cb2e1d 417
56a1740c 418 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
75cb2e1d
PM
419 if (err < 0)
420 goto exit;
421 }
422
423 /* Disable LED all-call address and set normal mode */
56a1740c 424 i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00);
75cb2e1d 425
0c62f42d
PM
426 if (pdata) {
427 /* Configure output: open-drain or totem pole (push-pull) */
428 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
429 i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01);
430 else
431 i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x05);
432 }
2f73c392 433
75cb2e1d
PM
434 return 0;
435
436exit:
437 while (i--) {
56a1740c
RRD
438 led_classdev_unregister(&pca963x[i].led_cdev);
439 cancel_work_sync(&pca963x[i].work);
75cb2e1d
PM
440 }
441
75cb2e1d
PM
442 return err;
443}
444
56a1740c 445static int pca963x_remove(struct i2c_client *client)
75cb2e1d 446{
56a1740c 447 struct pca963x *pca963x = i2c_get_clientdata(client);
75cb2e1d
PM
448 int i;
449
56a1740c
RRD
450 for (i = 0; i < pca963x->chipdef->n_leds; i++) {
451 led_classdev_unregister(&pca963x->leds[i].led_cdev);
452 cancel_work_sync(&pca963x->leds[i].work);
a7d0e988 453 }
75cb2e1d 454
75cb2e1d
PM
455 return 0;
456}
457
56a1740c 458static struct i2c_driver pca963x_driver = {
75cb2e1d 459 .driver = {
56a1740c 460 .name = "leds-pca963x",
56a1740c 461 .of_match_table = of_match_ptr(of_pca963x_match),
75cb2e1d 462 },
56a1740c
RRD
463 .probe = pca963x_probe,
464 .remove = pca963x_remove,
465 .id_table = pca963x_id,
75cb2e1d
PM
466};
467
56a1740c 468module_i2c_driver(pca963x_driver);
75cb2e1d
PM
469
470MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
56a1740c 471MODULE_DESCRIPTION("PCA963X LED driver");
75cb2e1d 472MODULE_LICENSE("GPL v2");