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