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