leds: lp55xx: enable setting default trigger
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / leds / leds-lp55xx-common.c
1 /*
2  * LP5521/LP5523/LP55231/LP5562 Common Driver
3  *
4  * Copyright 2012 Texas Instruments
5  *
6  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Derived from leds-lp5521.c, leds-lp5523.c
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/i2c.h>
19 #include <linux/leds.h>
20 #include <linux/module.h>
21 #include <linux/platform_data/leds-lp55xx.h>
22 #include <linux/slab.h>
23
24 #include "leds-lp55xx-common.h"
25
26 /* External clock rate */
27 #define LP55XX_CLK_32K                  32768
28
29 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
30 {
31         return container_of(cdev, struct lp55xx_led, cdev);
32 }
33
34 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
35 {
36         return cdev_to_lp55xx_led(dev_get_drvdata(dev));
37 }
38
39 static void lp55xx_reset_device(struct lp55xx_chip *chip)
40 {
41         struct lp55xx_device_config *cfg = chip->cfg;
42         u8 addr = cfg->reset.addr;
43         u8 val  = cfg->reset.val;
44
45         /* no error checking here because no ACK from the device after reset */
46         lp55xx_write(chip, addr, val);
47 }
48
49 static int lp55xx_detect_device(struct lp55xx_chip *chip)
50 {
51         struct lp55xx_device_config *cfg = chip->cfg;
52         u8 addr = cfg->enable.addr;
53         u8 val  = cfg->enable.val;
54         int ret;
55
56         ret = lp55xx_write(chip, addr, val);
57         if (ret)
58                 return ret;
59
60         usleep_range(1000, 2000);
61
62         ret = lp55xx_read(chip, addr, &val);
63         if (ret)
64                 return ret;
65
66         if (val != cfg->enable.val)
67                 return -ENODEV;
68
69         return 0;
70 }
71
72 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
73 {
74         struct lp55xx_device_config *cfg = chip->cfg;
75
76         if (!cfg->post_init_device)
77                 return 0;
78
79         return cfg->post_init_device(chip);
80 }
81
82 static ssize_t lp55xx_show_current(struct device *dev,
83                             struct device_attribute *attr,
84                             char *buf)
85 {
86         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
87
88         return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
89 }
90
91 static ssize_t lp55xx_store_current(struct device *dev,
92                              struct device_attribute *attr,
93                              const char *buf, size_t len)
94 {
95         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
96         struct lp55xx_chip *chip = led->chip;
97         unsigned long curr;
98
99         if (kstrtoul(buf, 0, &curr))
100                 return -EINVAL;
101
102         if (curr > led->max_current)
103                 return -EINVAL;
104
105         if (!chip->cfg->set_led_current)
106                 return len;
107
108         mutex_lock(&chip->lock);
109         chip->cfg->set_led_current(led, (u8)curr);
110         mutex_unlock(&chip->lock);
111
112         return len;
113 }
114
115 static ssize_t lp55xx_show_max_current(struct device *dev,
116                             struct device_attribute *attr,
117                             char *buf)
118 {
119         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
120
121         return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
122 }
123
124 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
125                 lp55xx_store_current);
126 static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
127
128 static struct attribute *lp55xx_led_attributes[] = {
129         &dev_attr_led_current.attr,
130         &dev_attr_max_current.attr,
131         NULL,
132 };
133
134 static struct attribute_group lp55xx_led_attr_group = {
135         .attrs = lp55xx_led_attributes
136 };
137
138 static void lp55xx_set_brightness(struct led_classdev *cdev,
139                              enum led_brightness brightness)
140 {
141         struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
142
143         led->brightness = (u8)brightness;
144         schedule_work(&led->brightness_work);
145 }
146
147 static int lp55xx_init_led(struct lp55xx_led *led,
148                         struct lp55xx_chip *chip, int chan)
149 {
150         struct lp55xx_platform_data *pdata = chip->pdata;
151         struct lp55xx_device_config *cfg = chip->cfg;
152         struct device *dev = &chip->cl->dev;
153         char name[32];
154         int ret;
155         int max_channel = cfg->max_channel;
156
157         if (chan >= max_channel) {
158                 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
159                 return -EINVAL;
160         }
161
162         if (pdata->led_config[chan].led_current == 0)
163                 return 0;
164
165         led->led_current = pdata->led_config[chan].led_current;
166         led->max_current = pdata->led_config[chan].max_current;
167         led->chan_nr = pdata->led_config[chan].chan_nr;
168         led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
169
170         if (led->chan_nr >= max_channel) {
171                 dev_err(dev, "Use channel numbers between 0 and %d\n",
172                         max_channel - 1);
173                 return -EINVAL;
174         }
175
176         led->cdev.brightness_set = lp55xx_set_brightness;
177
178         if (pdata->led_config[chan].name) {
179                 led->cdev.name = pdata->led_config[chan].name;
180         } else {
181                 snprintf(name, sizeof(name), "%s:channel%d",
182                         pdata->label ? : chip->cl->name, chan);
183                 led->cdev.name = name;
184         }
185
186         /*
187          * register led class device for each channel and
188          * add device attributes
189          */
190
191         ret = led_classdev_register(dev, &led->cdev);
192         if (ret) {
193                 dev_err(dev, "led register err: %d\n", ret);
194                 return ret;
195         }
196
197         ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
198         if (ret) {
199                 dev_err(dev, "led sysfs err: %d\n", ret);
200                 led_classdev_unregister(&led->cdev);
201                 return ret;
202         }
203
204         return 0;
205 }
206
207 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
208 {
209         struct lp55xx_chip *chip = context;
210         struct device *dev = &chip->cl->dev;
211
212         if (!fw) {
213                 dev_err(dev, "firmware request failed\n");
214                 goto out;
215         }
216
217         /* handling firmware data is chip dependent */
218         mutex_lock(&chip->lock);
219
220         chip->fw = fw;
221         if (chip->cfg->firmware_cb)
222                 chip->cfg->firmware_cb(chip);
223
224         mutex_unlock(&chip->lock);
225
226 out:
227         /* firmware should be released for other channel use */
228         release_firmware(chip->fw);
229 }
230
231 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
232 {
233         const char *name = chip->cl->name;
234         struct device *dev = &chip->cl->dev;
235
236         return request_firmware_nowait(THIS_MODULE, true, name, dev,
237                                 GFP_KERNEL, chip, lp55xx_firmware_loaded);
238 }
239
240 static ssize_t lp55xx_show_engine_select(struct device *dev,
241                             struct device_attribute *attr,
242                             char *buf)
243 {
244         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
245         struct lp55xx_chip *chip = led->chip;
246
247         return sprintf(buf, "%d\n", chip->engine_idx);
248 }
249
250 static ssize_t lp55xx_store_engine_select(struct device *dev,
251                              struct device_attribute *attr,
252                              const char *buf, size_t len)
253 {
254         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
255         struct lp55xx_chip *chip = led->chip;
256         unsigned long val;
257         int ret;
258
259         if (kstrtoul(buf, 0, &val))
260                 return -EINVAL;
261
262         /* select the engine to be run */
263
264         switch (val) {
265         case LP55XX_ENGINE_1:
266         case LP55XX_ENGINE_2:
267         case LP55XX_ENGINE_3:
268                 mutex_lock(&chip->lock);
269                 chip->engine_idx = val;
270                 ret = lp55xx_request_firmware(chip);
271                 mutex_unlock(&chip->lock);
272                 break;
273         default:
274                 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
275                 return -EINVAL;
276         }
277
278         if (ret) {
279                 dev_err(dev, "request firmware err: %d\n", ret);
280                 return ret;
281         }
282
283         return len;
284 }
285
286 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
287 {
288         if (chip->cfg->run_engine)
289                 chip->cfg->run_engine(chip, start);
290 }
291
292 static ssize_t lp55xx_store_engine_run(struct device *dev,
293                              struct device_attribute *attr,
294                              const char *buf, size_t len)
295 {
296         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
297         struct lp55xx_chip *chip = led->chip;
298         unsigned long val;
299
300         if (kstrtoul(buf, 0, &val))
301                 return -EINVAL;
302
303         /* run or stop the selected engine */
304
305         if (val <= 0) {
306                 lp55xx_run_engine(chip, false);
307                 return len;
308         }
309
310         mutex_lock(&chip->lock);
311         lp55xx_run_engine(chip, true);
312         mutex_unlock(&chip->lock);
313
314         return len;
315 }
316
317 static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
318                    lp55xx_show_engine_select, lp55xx_store_engine_select);
319 static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
320
321 static struct attribute *lp55xx_engine_attributes[] = {
322         &dev_attr_select_engine.attr,
323         &dev_attr_run_engine.attr,
324         NULL,
325 };
326
327 static const struct attribute_group lp55xx_engine_attr_group = {
328         .attrs = lp55xx_engine_attributes,
329 };
330
331 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
332 {
333         return i2c_smbus_write_byte_data(chip->cl, reg, val);
334 }
335 EXPORT_SYMBOL_GPL(lp55xx_write);
336
337 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
338 {
339         s32 ret;
340
341         ret = i2c_smbus_read_byte_data(chip->cl, reg);
342         if (ret < 0)
343                 return ret;
344
345         *val = ret;
346         return 0;
347 }
348 EXPORT_SYMBOL_GPL(lp55xx_read);
349
350 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
351 {
352         int ret;
353         u8 tmp;
354
355         ret = lp55xx_read(chip, reg, &tmp);
356         if (ret)
357                 return ret;
358
359         tmp &= ~mask;
360         tmp |= val & mask;
361
362         return lp55xx_write(chip, reg, tmp);
363 }
364 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
365
366 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
367 {
368         struct clk *clk;
369         int err;
370
371         clk = devm_clk_get(&chip->cl->dev, "32k_clk");
372         if (IS_ERR(clk))
373                 goto use_internal_clk;
374
375         err = clk_prepare_enable(clk);
376         if (err)
377                 goto use_internal_clk;
378
379         if (clk_get_rate(clk) != LP55XX_CLK_32K) {
380                 clk_disable_unprepare(clk);
381                 goto use_internal_clk;
382         }
383
384         dev_info(&chip->cl->dev, "%dHz external clock used\n",  LP55XX_CLK_32K);
385
386         chip->clk = clk;
387         return true;
388
389 use_internal_clk:
390         dev_info(&chip->cl->dev, "internal clock used\n");
391         return false;
392 }
393 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
394
395 int lp55xx_init_device(struct lp55xx_chip *chip)
396 {
397         struct lp55xx_platform_data *pdata;
398         struct lp55xx_device_config *cfg;
399         struct device *dev = &chip->cl->dev;
400         int ret = 0;
401
402         WARN_ON(!chip);
403
404         pdata = chip->pdata;
405         cfg = chip->cfg;
406
407         if (!pdata || !cfg)
408                 return -EINVAL;
409
410         if (pdata->setup_resources) {
411                 ret = pdata->setup_resources();
412                 if (ret < 0) {
413                         dev_err(dev, "setup resoure err: %d\n", ret);
414                         goto err;
415                 }
416         }
417
418         if (pdata->enable) {
419                 pdata->enable(0);
420                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
421                 pdata->enable(1);
422                 usleep_range(1000, 2000); /* 500us abs min. */
423         }
424
425         lp55xx_reset_device(chip);
426
427         /*
428          * Exact value is not available. 10 - 20ms
429          * appears to be enough for reset.
430          */
431         usleep_range(10000, 20000);
432
433         ret = lp55xx_detect_device(chip);
434         if (ret) {
435                 dev_err(dev, "device detection err: %d\n", ret);
436                 goto err;
437         }
438
439         /* chip specific initialization */
440         ret = lp55xx_post_init_device(chip);
441         if (ret) {
442                 dev_err(dev, "post init device err: %d\n", ret);
443                 goto err_post_init;
444         }
445
446         return 0;
447
448 err_post_init:
449         lp55xx_deinit_device(chip);
450 err:
451         return ret;
452 }
453 EXPORT_SYMBOL_GPL(lp55xx_init_device);
454
455 void lp55xx_deinit_device(struct lp55xx_chip *chip)
456 {
457         struct lp55xx_platform_data *pdata = chip->pdata;
458
459         if (chip->clk)
460                 clk_disable_unprepare(chip->clk);
461
462         if (pdata->enable)
463                 pdata->enable(0);
464
465         if (pdata->release_resources)
466                 pdata->release_resources();
467 }
468 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
469
470 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
471 {
472         struct lp55xx_platform_data *pdata = chip->pdata;
473         struct lp55xx_device_config *cfg = chip->cfg;
474         int num_channels = pdata->num_channels;
475         struct lp55xx_led *each;
476         u8 led_current;
477         int ret;
478         int i;
479
480         if (!cfg->brightness_work_fn) {
481                 dev_err(&chip->cl->dev, "empty brightness configuration\n");
482                 return -EINVAL;
483         }
484
485         for (i = 0; i < num_channels; i++) {
486
487                 /* do not initialize channels that are not connected */
488                 if (pdata->led_config[i].led_current == 0)
489                         continue;
490
491                 led_current = pdata->led_config[i].led_current;
492                 each = led + i;
493                 ret = lp55xx_init_led(each, chip, i);
494                 if (ret)
495                         goto err_init_led;
496
497                 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
498
499                 chip->num_leds++;
500                 each->chip = chip;
501
502                 /* setting led current at each channel */
503                 if (cfg->set_led_current)
504                         cfg->set_led_current(each, led_current);
505         }
506
507         return 0;
508
509 err_init_led:
510         lp55xx_unregister_leds(led, chip);
511         return ret;
512 }
513 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
514
515 void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
516 {
517         int i;
518         struct lp55xx_led *each;
519
520         for (i = 0; i < chip->num_leds; i++) {
521                 each = led + i;
522                 led_classdev_unregister(&each->cdev);
523                 flush_work(&each->brightness_work);
524         }
525 }
526 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
527
528 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
529 {
530         struct device *dev = &chip->cl->dev;
531         struct lp55xx_device_config *cfg = chip->cfg;
532         int ret;
533
534         if (!cfg->run_engine || !cfg->firmware_cb)
535                 goto dev_specific_attrs;
536
537         ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
538         if (ret)
539                 return ret;
540
541 dev_specific_attrs:
542         return cfg->dev_attr_group ?
543                 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
544 }
545 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
546
547 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
548 {
549         struct device *dev = &chip->cl->dev;
550         struct lp55xx_device_config *cfg = chip->cfg;
551
552         if (cfg->dev_attr_group)
553                 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
554
555         sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
556 }
557 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
558
559 int lp55xx_of_populate_pdata(struct device *dev, struct device_node *np)
560 {
561         struct device_node *child;
562         struct lp55xx_platform_data *pdata;
563         struct lp55xx_led_config *cfg;
564         int num_channels;
565         int i = 0;
566
567         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
568         if (!pdata)
569                 return -ENOMEM;
570
571         num_channels = of_get_child_count(np);
572         if (num_channels == 0) {
573                 dev_err(dev, "no LED channels\n");
574                 return -EINVAL;
575         }
576
577         cfg = devm_kzalloc(dev, sizeof(*cfg) * num_channels, GFP_KERNEL);
578         if (!cfg)
579                 return -ENOMEM;
580
581         pdata->led_config = &cfg[0];
582         pdata->num_channels = num_channels;
583
584         for_each_child_of_node(np, child) {
585                 cfg[i].chan_nr = i;
586
587                 of_property_read_string(child, "chan-name", &cfg[i].name);
588                 of_property_read_u8(child, "led-cur", &cfg[i].led_current);
589                 of_property_read_u8(child, "max-cur", &cfg[i].max_current);
590                 cfg[i].default_trigger =
591                         of_get_property(child, "linux,default-trigger", NULL);
592
593                 i++;
594         }
595
596         of_property_read_string(np, "label", &pdata->label);
597         of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
598
599         /* LP8501 specific */
600         of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
601
602         dev->platform_data = pdata;
603
604         return 0;
605 }
606 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata);
607
608 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
609 MODULE_DESCRIPTION("LP55xx Common Driver");
610 MODULE_LICENSE("GPL");