7dd48f896d4e9d8cde342e0feb223383b2ce5dd8
[firefly-linux-kernel-4.4.55.git] / drivers / leds / leds-pca9633.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 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 'PCA9633_HW_BLINK'
25  * or by adding the 'nxp,hw-blink' property to the DTS.
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>
37 #include <linux/of.h>
38 #include <linux/platform_data/leds-pca9633.h>
39
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 */
45
46 #define PCA9633_MODE2_DMBLNK    0x20    /* Enable blinking */
47
48 #define PCA9633_MODE1           0x00
49 #define PCA9633_MODE2           0x01
50 #define PCA9633_PWM_BASE        0x02
51
52 enum pca9633_type {
53         pca9633,
54         pca9634,
55 };
56
57 struct pca9633_chipdef {
58         u8                      grppwm;
59         u8                      grpfreq;
60         u8                      ledout_base;
61         int                     n_leds;
62 };
63
64 static struct pca9633_chipdef pca9633_chipdefs[] = {
65         [pca9633] = {
66                 .grppwm         = 0x6,
67                 .grpfreq        = 0x7,
68                 .ledout_base    = 0x8,
69                 .n_leds         = 4,
70         },
71         [pca9634] = {
72                 .grppwm         = 0xa,
73                 .grpfreq        = 0xb,
74                 .ledout_base    = 0xc,
75                 .n_leds         = 8,
76         },
77 };
78
79 /* Total blink period in milliseconds */
80 #define PCA9632_BLINK_PERIOD_MIN        42
81 #define PCA9632_BLINK_PERIOD_MAX        10667
82
83 static const struct i2c_device_id pca9633_id[] = {
84         { "pca9632", pca9633 },
85         { "pca9633", pca9633 },
86         { "pca9634", pca9634 },
87         { }
88 };
89 MODULE_DEVICE_TABLE(i2c, pca9633_id);
90
91 enum pca9633_cmd {
92         BRIGHTNESS_SET,
93         BLINK_SET,
94 };
95
96 struct pca9633_led {
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;
104         char name[32];
105         u8 gdc;
106         u8 gfrq;
107 };
108
109 static void pca9633_brightness_work(struct pca9633_led *pca9633)
110 {
111         u8 ledout_addr = pca9633->chipdef->ledout_base + (pca9633->led_num / 4);
112         u8 ledout;
113         int shift = 2 * (pca9633->led_num % 4);
114         u8 mask = 0x3 << shift;
115
116         ledout = i2c_smbus_read_byte_data(pca9633->client, ledout_addr);
117         switch (pca9633->brightness) {
118         case LED_FULL:
119                 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
120                         (ledout & ~mask) | (PCA9633_LED_ON << shift));
121                 break;
122         case LED_OFF:
123                 i2c_smbus_write_byte_data(pca9633->client, ledout_addr,
124                         ledout & ~mask);
125                 break;
126         default:
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));
132                 break;
133         }
134 }
135
136 static void pca9633_blink_work(struct pca9633_led *pca9633)
137 {
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;
143
144         i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grppwm,
145                 pca9633->gdc);
146
147         i2c_smbus_write_byte_data(pca9633->client, pca9633->chipdef->grpfreq,
148                 pca9633->gfrq);
149
150         if (!(mode2 & PCA9633_MODE2_DMBLNK))
151                 i2c_smbus_write_byte_data(pca9633->client, PCA9633_MODE2,
152                         mode2 | PCA9633_MODE2_DMBLNK);
153
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));
157 }
158
159 static void pca9633_work(struct work_struct *work)
160 {
161         struct pca9633_led *pca9633 = container_of(work,
162                 struct pca9633_led, work);
163
164         switch (pca9633->cmd) {
165         case BRIGHTNESS_SET:
166                 pca9633_brightness_work(pca9633);
167                 break;
168         case BLINK_SET:
169                 pca9633_blink_work(pca9633);
170                 break;
171         }
172 }
173
174 static void pca9633_led_set(struct led_classdev *led_cdev,
175         enum led_brightness value)
176 {
177         struct pca9633_led *pca9633;
178
179         pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
180
181         pca9633->cmd = BRIGHTNESS_SET;
182         pca9633->brightness = value;
183
184         /*
185          * Must use workqueue for the actual I/O since I2C operations
186          * can sleep.
187          */
188         schedule_work(&pca9633->work);
189 }
190
191 static int pca9633_blink_set(struct led_classdev *led_cdev,
192                 unsigned long *delay_on, unsigned long *delay_off)
193 {
194         struct pca9633_led *pca9633;
195         unsigned long time_on, time_off, period;
196         u8 gdc, gfrq;
197
198         pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
199
200         time_on = *delay_on;
201         time_off = *delay_off;
202
203         /* If both zero, pick reasonable defaults of 500ms each */
204         if (!time_on && !time_off) {
205                 time_on = 500;
206                 time_off = 500;
207         }
208
209         period = time_on + time_off;
210
211         /* If period not supported by hardware, default to someting sane. */
212         if ((period < PCA9632_BLINK_PERIOD_MIN) ||
213             (period > PCA9632_BLINK_PERIOD_MAX)) {
214                 time_on = 500;
215                 time_off = 500;
216                 period = time_on + time_off;
217         }
218
219         /*
220          * From manual: duty cycle = (GDC / 256) ->
221          *      (time_on / period) = (GDC / 256) ->
222          *              GDC = ((time_on * 256) / period)
223          */
224         gdc = (time_on * 256) / period;
225
226         /*
227          * From manual: period = ((GFRQ + 1) / 24) in seconds.
228          * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
229          *              GFRQ = ((period * 24 / 1000) - 1)
230          */
231         gfrq = (period * 24 / 1000) - 1;
232
233         pca9633->cmd = BLINK_SET;
234         pca9633->gdc = gdc;
235         pca9633->gfrq = gfrq;
236
237         /*
238          * Must use workqueue for the actual I/O since I2C operations
239          * can sleep.
240          */
241         schedule_work(&pca9633->work);
242
243         *delay_on = time_on;
244         *delay_off = time_off;
245
246         return 0;
247 }
248
249 #if IS_ENABLED(CONFIG_OF)
250 static struct pca9633_platform_data *
251 pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip)
252 {
253         struct device_node *np = client->dev.of_node, *child;
254         struct pca9633_platform_data *pdata;
255         struct led_info *pca9633_leds;
256         int count;
257
258         count = of_get_child_count(np);
259         if (!count || count > chip->n_leds)
260                 return ERR_PTR(-ENODEV);
261
262         pca9633_leds = devm_kzalloc(&client->dev,
263                         sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
264         if (!pca9633_leds)
265                 return ERR_PTR(-ENOMEM);
266
267         for_each_child_of_node(np, child) {
268                 struct led_info led;
269                 u32 reg;
270                 int res;
271
272                 led.name =
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", &reg);
277                 if (res != 0)
278                         continue;
279                 pca9633_leds[reg] = led;
280         }
281         pdata = devm_kzalloc(&client->dev,
282                              sizeof(struct pca9633_platform_data), GFP_KERNEL);
283         if (!pdata)
284                 return ERR_PTR(-ENOMEM);
285
286         pdata->leds.leds = pca9633_leds;
287         pdata->leds.num_leds = count;
288
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;
292         else
293                 pdata->outdrv = PCA9633_OPEN_DRAIN;
294
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;
298         else
299                 pdata->blink_type = PCA9633_SW_BLINK;
300
301         return pdata;
302 }
303
304 static const struct of_device_id of_pca9633_match[] = {
305         { .compatible = "nxp,pca9632", },
306         { .compatible = "nxp,pca9633", },
307         { .compatible = "nxp,pca9634", },
308         {},
309 };
310 #else
311 static struct pca9633_platform_data *
312 pca9633_dt_init(struct i2c_client *client, struct pca9633_chipdef *chip)
313 {
314         return ERR_PTR(-ENODEV);
315 }
316 #endif
317
318 static int pca9633_probe(struct i2c_client *client,
319                                         const struct i2c_device_id *id)
320 {
321         struct pca9633_led *pca9633;
322         struct pca9633_platform_data *pdata;
323         struct pca9633_chipdef *chip;
324         int i, err;
325
326         chip = &pca9633_chipdefs[id->driver_data];
327         pdata = dev_get_platdata(&client->dev);
328
329         if (!pdata) {
330                 pdata = pca9633_dt_init(client, chip);
331                 if (IS_ERR(pdata)) {
332                         dev_warn(&client->dev, "could not parse configuration\n");
333                         pdata = NULL;
334                 }
335         }
336
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",
340                                                                 chip->n_leds);
341                 return -EINVAL;
342         }
343
344         pca9633 = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca9633),
345                                                                 GFP_KERNEL);
346         if (!pca9633)
347                 return -ENOMEM;
348
349         i2c_set_clientdata(client, pca9633);
350
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;
355
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;
365                 }
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,
370                                  client->addr, i);
371
372                 pca9633[i].led_cdev.name = pca9633[i].name;
373                 pca9633[i].led_cdev.brightness_set = pca9633_led_set;
374
375                 if (pdata && pdata->blink_type == PCA9633_HW_BLINK)
376                         pca9633[i].led_cdev.blink_set = pca9633_blink_set;
377
378                 INIT_WORK(&pca9633[i].work, pca9633_work);
379
380                 err = led_classdev_register(&client->dev, &pca9633[i].led_cdev);
381                 if (err < 0)
382                         goto exit;
383         }
384
385         /* Disable LED all-call address and set normal mode */
386         i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00);
387
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);
391
392         /* Turn off LEDs */
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);
396
397         return 0;
398
399 exit:
400         while (i--) {
401                 led_classdev_unregister(&pca9633[i].led_cdev);
402                 cancel_work_sync(&pca9633[i].work);
403         }
404
405         return err;
406 }
407
408 static int pca9633_remove(struct i2c_client *client)
409 {
410         struct pca9633_led *pca9633 = i2c_get_clientdata(client);
411         int i;
412
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);
417                 }
418
419         return 0;
420 }
421
422 static struct i2c_driver pca9633_driver = {
423         .driver = {
424                 .name   = "leds-pca9633",
425                 .owner  = THIS_MODULE,
426                 .of_match_table = of_match_ptr(of_pca9633_match),
427         },
428         .probe  = pca9633_probe,
429         .remove = pca9633_remove,
430         .id_table = pca9633_id,
431 };
432
433 module_i2c_driver(pca9633_driver);
434
435 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
436 MODULE_DESCRIPTION("PCA9633 LED driver");
437 MODULE_LICENSE("GPL v2");