GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / leds / leds-pca963x.c
1 /*
2  * Copyright 2011 bct electronic GmbH
3  * Copyright 2013 Qtechnology/AS
4  *
5  * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
6  * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
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)
15  * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
16  *
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.
26  */
27
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>
37 #include <linux/of.h>
38 #include <linux/platform_data/leds-pca963x.h>
39
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 */
45
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 */
49
50 #define PCA963X_MODE1           0x00
51 #define PCA963X_MODE2           0x01
52 #define PCA963X_PWM_BASE        0x02
53
54 enum pca963x_type {
55         pca9633,
56         pca9634,
57         pca9635,
58 };
59
60 struct pca963x_chipdef {
61         u8                      grppwm;
62         u8                      grpfreq;
63         u8                      ledout_base;
64         int                     n_leds;
65         unsigned int            scaling;
66 };
67
68 static struct pca963x_chipdef pca963x_chipdefs[] = {
69         [pca9633] = {
70                 .grppwm         = 0x6,
71                 .grpfreq        = 0x7,
72                 .ledout_base    = 0x8,
73                 .n_leds         = 4,
74         },
75         [pca9634] = {
76                 .grppwm         = 0xa,
77                 .grpfreq        = 0xb,
78                 .ledout_base    = 0xc,
79                 .n_leds         = 8,
80         },
81         [pca9635] = {
82                 .grppwm         = 0x12,
83                 .grpfreq        = 0x13,
84                 .ledout_base    = 0x14,
85                 .n_leds         = 16,
86         },
87 };
88
89 /* Total blink period in milliseconds */
90 #define PCA963X_BLINK_PERIOD_MIN        42
91 #define PCA963X_BLINK_PERIOD_MAX        10667
92
93 static const struct i2c_device_id pca963x_id[] = {
94         { "pca9632", pca9633 },
95         { "pca9633", pca9633 },
96         { "pca9634", pca9634 },
97         { "pca9635", pca9635 },
98         { }
99 };
100 MODULE_DEVICE_TABLE(i2c, pca963x_id);
101
102 static const struct acpi_device_id pca963x_acpi_ids[] = {
103         { "PCA9632", pca9633 },
104         { "PCA9633", pca9633 },
105         { "PCA9634", pca9634 },
106         { "PCA9635", pca9635 },
107         { }
108 };
109 MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids);
110
111 struct pca963x_led;
112
113 struct pca963x {
114         struct pca963x_chipdef *chipdef;
115         struct mutex mutex;
116         struct i2c_client *client;
117         struct pca963x_led *leds;
118         unsigned long leds_on;
119 };
120
121 struct pca963x_led {
122         struct pca963x *chip;
123         struct led_classdev led_cdev;
124         int led_num; /* 0 .. 15 potentially */
125         char name[32];
126         u8 gdc;
127         u8 gfrq;
128 };
129
130 static int pca963x_brightness(struct pca963x_led *pca963x,
131                                enum led_brightness brightness)
132 {
133         u8 ledout_addr = pca963x->chip->chipdef->ledout_base
134                 + (pca963x->led_num / 4);
135         u8 ledout;
136         int shift = 2 * (pca963x->led_num % 4);
137         u8 mask = 0x3 << shift;
138         int ret;
139
140         ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
141         switch (brightness) {
142         case LED_FULL:
143                 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
144                         ledout_addr,
145                         (ledout & ~mask) | (PCA963X_LED_ON << shift));
146                 break;
147         case LED_OFF:
148                 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
149                         ledout_addr, ledout & ~mask);
150                 break;
151         default:
152                 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
153                         PCA963X_PWM_BASE + pca963x->led_num,
154                         brightness);
155                 if (ret < 0)
156                         return ret;
157                 ret = i2c_smbus_write_byte_data(pca963x->chip->client,
158                         ledout_addr,
159                         (ledout & ~mask) | (PCA963X_LED_PWM << shift));
160                 break;
161         }
162
163         return ret;
164 }
165
166 static void pca963x_blink(struct pca963x_led *pca963x)
167 {
168         u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
169                 (pca963x->led_num / 4);
170         u8 ledout;
171         u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
172                                                         PCA963X_MODE2);
173         int shift = 2 * (pca963x->led_num % 4);
174         u8 mask = 0x3 << shift;
175
176         i2c_smbus_write_byte_data(pca963x->chip->client,
177                         pca963x->chip->chipdef->grppwm, pca963x->gdc);
178
179         i2c_smbus_write_byte_data(pca963x->chip->client,
180                         pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
181
182         if (!(mode2 & PCA963X_MODE2_DMBLNK))
183                 i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
184                         mode2 | PCA963X_MODE2_DMBLNK);
185
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);
192 }
193
194 static int pca963x_power_state(struct pca963x_led *pca963x)
195 {
196         unsigned long *leds_on = &pca963x->chip->leds_on;
197         unsigned long cached_leds = pca963x->chip->leds_on;
198
199         if (pca963x->led_cdev.brightness)
200                 set_bit(pca963x->led_num, leds_on);
201         else
202                 clear_bit(pca963x->led_num, leds_on);
203
204         if (!(*leds_on) != !cached_leds)
205                 return i2c_smbus_write_byte_data(pca963x->chip->client,
206                         PCA963X_MODE1, *leds_on ? 0 : BIT(4));
207
208         return 0;
209 }
210
211 static int pca963x_led_set(struct led_classdev *led_cdev,
212         enum led_brightness value)
213 {
214         struct pca963x_led *pca963x;
215         int ret;
216
217         pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
218
219         mutex_lock(&pca963x->chip->mutex);
220
221         ret = pca963x_brightness(pca963x, value);
222         if (ret < 0)
223                 goto unlock;
224         ret = pca963x_power_state(pca963x);
225
226 unlock:
227         mutex_unlock(&pca963x->chip->mutex);
228         return ret;
229 }
230
231 static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
232         unsigned int val)
233 {
234         unsigned int scaling = pca963x->chip->chipdef->scaling;
235
236         return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
237 }
238
239 static int pca963x_blink_set(struct led_classdev *led_cdev,
240                 unsigned long *delay_on, unsigned long *delay_off)
241 {
242         struct pca963x_led *pca963x;
243         unsigned long time_on, time_off, period;
244         u8 gdc, gfrq;
245
246         pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
247
248         time_on = *delay_on;
249         time_off = *delay_off;
250
251         /* If both zero, pick reasonable defaults of 500ms each */
252         if (!time_on && !time_off) {
253                 time_on = 500;
254                 time_off = 500;
255         }
256
257         period = pca963x_period_scale(pca963x, time_on + time_off);
258
259         /* If period not supported by hardware, default to someting sane. */
260         if ((period < PCA963X_BLINK_PERIOD_MIN) ||
261             (period > PCA963X_BLINK_PERIOD_MAX)) {
262                 time_on = 500;
263                 time_off = 500;
264                 period = pca963x_period_scale(pca963x, 1000);
265         }
266
267         /*
268          * From manual: duty cycle = (GDC / 256) ->
269          *      (time_on / period) = (GDC / 256) ->
270          *              GDC = ((time_on * 256) / period)
271          */
272         gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
273
274         /*
275          * From manual: period = ((GFRQ + 1) / 24) in seconds.
276          * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
277          *              GFRQ = ((period * 24 / 1000) - 1)
278          */
279         gfrq = (period * 24 / 1000) - 1;
280
281         pca963x->gdc = gdc;
282         pca963x->gfrq = gfrq;
283
284         pca963x_blink(pca963x);
285
286         *delay_on = time_on;
287         *delay_off = time_off;
288
289         return 0;
290 }
291
292 #if IS_ENABLED(CONFIG_OF)
293 static struct pca963x_platform_data *
294 pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
295 {
296         struct device_node *np = client->dev.of_node, *child;
297         struct pca963x_platform_data *pdata;
298         struct led_info *pca963x_leds;
299         int count;
300
301         count = of_get_child_count(np);
302         if (!count || count > chip->n_leds)
303                 return ERR_PTR(-ENODEV);
304
305         pca963x_leds = devm_kcalloc(&client->dev,
306                         chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
307         if (!pca963x_leds)
308                 return ERR_PTR(-ENOMEM);
309
310         for_each_child_of_node(np, child) {
311                 struct led_info led = {};
312                 u32 reg;
313                 int res;
314
315                 res = of_property_read_u32(child, "reg", &reg);
316                 if ((res != 0) || (reg >= chip->n_leds))
317                         continue;
318                 led.name =
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;
323         }
324         pdata = devm_kzalloc(&client->dev,
325                              sizeof(struct pca963x_platform_data), GFP_KERNEL);
326         if (!pdata)
327                 return ERR_PTR(-ENOMEM);
328
329         pdata->leds.leds = pca963x_leds;
330         pdata->leds.num_leds = chip->n_leds;
331
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;
335         else
336                 pdata->outdrv = PCA963X_OPEN_DRAIN;
337
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;
341         else
342                 pdata->blink_type = PCA963X_SW_BLINK;
343
344         if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling))
345                 chip->scaling = 1000;
346
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;
350         else
351                 pdata->dir = PCA963X_NORMAL;
352
353         return pdata;
354 }
355
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", },
361         {},
362 };
363 MODULE_DEVICE_TABLE(of, of_pca963x_match);
364 #else
365 static struct pca963x_platform_data *
366 pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
367 {
368         return ERR_PTR(-ENODEV);
369 }
370 #endif
371
372 static int pca963x_probe(struct i2c_client *client,
373                                         const struct i2c_device_id *id)
374 {
375         struct pca963x *pca963x_chip;
376         struct pca963x_led *pca963x;
377         struct pca963x_platform_data *pdata;
378         struct pca963x_chipdef *chip;
379         int i, err;
380
381         if (id) {
382                 chip = &pca963x_chipdefs[id->driver_data];
383         } else {
384                 const struct acpi_device_id *acpi_id;
385
386                 acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev);
387                 if (!acpi_id)
388                         return -ENODEV;
389                 chip = &pca963x_chipdefs[acpi_id->driver_data];
390         }
391         pdata = dev_get_platdata(&client->dev);
392
393         if (!pdata) {
394                 pdata = pca963x_dt_init(client, chip);
395                 if (IS_ERR(pdata)) {
396                         dev_warn(&client->dev, "could not parse configuration\n");
397                         pdata = NULL;
398                 }
399         }
400
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",
404                                                                 chip->n_leds);
405                 return -EINVAL;
406         }
407
408         pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
409                                                                 GFP_KERNEL);
410         if (!pca963x_chip)
411                 return -ENOMEM;
412         pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
413                                                                 GFP_KERNEL);
414         if (!pca963x)
415                 return -ENOMEM;
416
417         i2c_set_clientdata(client, pca963x_chip);
418
419         mutex_init(&pca963x_chip->mutex);
420         pca963x_chip->chipdef = chip;
421         pca963x_chip->client = client;
422         pca963x_chip->leds = pca963x;
423
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);
427
428         for (i = 0; i < chip->n_leds; i++) {
429                 pca963x[i].led_num = i;
430                 pca963x[i].chip = pca963x_chip;
431
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;
441                 }
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,
446                                  client->addr, i);
447
448                 pca963x[i].led_cdev.name = pca963x[i].name;
449                 pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
450
451                 if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
452                         pca963x[i].led_cdev.blink_set = pca963x_blink_set;
453
454                 err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
455                 if (err < 0)
456                         goto exit;
457         }
458
459         /* Disable LED all-call address, and power down initially */
460         i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
461
462         if (pdata) {
463                 u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
464                                                     PCA963X_MODE2);
465                 /* Configure output: open-drain or totem pole (push-pull) */
466                 if (pdata->outdrv == PCA963X_OPEN_DRAIN)
467                         mode2 &= ~PCA963X_MODE2_OUTDRV;
468                 else
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,
474                                           mode2);
475         }
476
477         return 0;
478
479 exit:
480         while (i--)
481                 led_classdev_unregister(&pca963x[i].led_cdev);
482
483         return err;
484 }
485
486 static int pca963x_remove(struct i2c_client *client)
487 {
488         struct pca963x *pca963x = i2c_get_clientdata(client);
489         int i;
490
491         for (i = 0; i < pca963x->chipdef->n_leds; i++)
492                 led_classdev_unregister(&pca963x->leds[i].led_cdev);
493
494         return 0;
495 }
496
497 static struct i2c_driver pca963x_driver = {
498         .driver = {
499                 .name   = "leds-pca963x",
500                 .of_match_table = of_match_ptr(of_pca963x_match),
501                 .acpi_match_table = ACPI_PTR(pca963x_acpi_ids),
502         },
503         .probe  = pca963x_probe,
504         .remove = pca963x_remove,
505         .id_table = pca963x_id,
506 };
507
508 module_i2c_driver(pca963x_driver);
509
510 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
511 MODULE_DESCRIPTION("PCA963X LED driver");
512 MODULE_LICENSE("GPL v2");