2 * LP5521/LP5523/LP55231/LP5562 Common Driver
4 * Copyright 2012 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
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.
12 * Derived from leds-lp5521.c, leds-lp5523.c
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>
24 #include "leds-lp55xx-common.h"
26 /* External clock rate */
27 #define LP55XX_CLK_32K 32768
29 static struct lp55xx_led
*cdev_to_lp55xx_led(struct led_classdev
*cdev
)
31 return container_of(cdev
, struct lp55xx_led
, cdev
);
34 static struct lp55xx_led
*dev_to_lp55xx_led(struct device
*dev
)
36 return cdev_to_lp55xx_led(dev_get_drvdata(dev
));
39 static void lp55xx_reset_device(struct lp55xx_chip
*chip
)
41 struct lp55xx_device_config
*cfg
= chip
->cfg
;
42 u8 addr
= cfg
->reset
.addr
;
43 u8 val
= cfg
->reset
.val
;
45 /* no error checking here because no ACK from the device after reset */
46 lp55xx_write(chip
, addr
, val
);
49 static int lp55xx_detect_device(struct lp55xx_chip
*chip
)
51 struct lp55xx_device_config
*cfg
= chip
->cfg
;
52 u8 addr
= cfg
->enable
.addr
;
53 u8 val
= cfg
->enable
.val
;
56 ret
= lp55xx_write(chip
, addr
, val
);
60 usleep_range(1000, 2000);
62 ret
= lp55xx_read(chip
, addr
, &val
);
66 if (val
!= cfg
->enable
.val
)
72 static int lp55xx_post_init_device(struct lp55xx_chip
*chip
)
74 struct lp55xx_device_config
*cfg
= chip
->cfg
;
76 if (!cfg
->post_init_device
)
79 return cfg
->post_init_device(chip
);
82 static ssize_t
lp55xx_show_current(struct device
*dev
,
83 struct device_attribute
*attr
,
86 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
88 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->led_current
);
91 static ssize_t
lp55xx_store_current(struct device
*dev
,
92 struct device_attribute
*attr
,
93 const char *buf
, size_t len
)
95 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
96 struct lp55xx_chip
*chip
= led
->chip
;
99 if (kstrtoul(buf
, 0, &curr
))
102 if (curr
> led
->max_current
)
105 if (!chip
->cfg
->set_led_current
)
108 mutex_lock(&chip
->lock
);
109 chip
->cfg
->set_led_current(led
, (u8
)curr
);
110 mutex_unlock(&chip
->lock
);
115 static ssize_t
lp55xx_show_max_current(struct device
*dev
,
116 struct device_attribute
*attr
,
119 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
121 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->max_current
);
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
);
128 static struct attribute
*lp55xx_led_attributes
[] = {
129 &dev_attr_led_current
.attr
,
130 &dev_attr_max_current
.attr
,
134 static struct attribute_group lp55xx_led_attr_group
= {
135 .attrs
= lp55xx_led_attributes
138 static void lp55xx_set_brightness(struct led_classdev
*cdev
,
139 enum led_brightness brightness
)
141 struct lp55xx_led
*led
= cdev_to_lp55xx_led(cdev
);
143 led
->brightness
= (u8
)brightness
;
144 schedule_work(&led
->brightness_work
);
147 static int lp55xx_init_led(struct lp55xx_led
*led
,
148 struct lp55xx_chip
*chip
, int chan
)
150 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
151 struct lp55xx_device_config
*cfg
= chip
->cfg
;
152 struct device
*dev
= &chip
->cl
->dev
;
155 int max_channel
= cfg
->max_channel
;
157 if (chan
>= max_channel
) {
158 dev_err(dev
, "invalid channel: %d / %d\n", chan
, max_channel
);
162 if (pdata
->led_config
[chan
].led_current
== 0)
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
;
169 if (led
->chan_nr
>= max_channel
) {
170 dev_err(dev
, "Use channel numbers between 0 and %d\n",
175 led
->cdev
.brightness_set
= lp55xx_set_brightness
;
177 if (pdata
->led_config
[chan
].name
) {
178 led
->cdev
.name
= pdata
->led_config
[chan
].name
;
180 snprintf(name
, sizeof(name
), "%s:channel%d",
181 pdata
->label
? : chip
->cl
->name
, chan
);
182 led
->cdev
.name
= name
;
186 * register led class device for each channel and
187 * add device attributes
190 ret
= led_classdev_register(dev
, &led
->cdev
);
192 dev_err(dev
, "led register err: %d\n", ret
);
196 ret
= sysfs_create_group(&led
->cdev
.dev
->kobj
, &lp55xx_led_attr_group
);
198 dev_err(dev
, "led sysfs err: %d\n", ret
);
199 led_classdev_unregister(&led
->cdev
);
206 static void lp55xx_firmware_loaded(const struct firmware
*fw
, void *context
)
208 struct lp55xx_chip
*chip
= context
;
209 struct device
*dev
= &chip
->cl
->dev
;
212 dev_err(dev
, "firmware request failed\n");
216 /* handling firmware data is chip dependent */
217 mutex_lock(&chip
->lock
);
220 if (chip
->cfg
->firmware_cb
)
221 chip
->cfg
->firmware_cb(chip
);
223 mutex_unlock(&chip
->lock
);
226 /* firmware should be released for other channel use */
227 release_firmware(chip
->fw
);
230 static int lp55xx_request_firmware(struct lp55xx_chip
*chip
)
232 const char *name
= chip
->cl
->name
;
233 struct device
*dev
= &chip
->cl
->dev
;
235 return request_firmware_nowait(THIS_MODULE
, true, name
, dev
,
236 GFP_KERNEL
, chip
, lp55xx_firmware_loaded
);
239 static ssize_t
lp55xx_show_engine_select(struct device
*dev
,
240 struct device_attribute
*attr
,
243 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
244 struct lp55xx_chip
*chip
= led
->chip
;
246 return sprintf(buf
, "%d\n", chip
->engine_idx
);
249 static ssize_t
lp55xx_store_engine_select(struct device
*dev
,
250 struct device_attribute
*attr
,
251 const char *buf
, size_t len
)
253 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
254 struct lp55xx_chip
*chip
= led
->chip
;
258 if (kstrtoul(buf
, 0, &val
))
261 /* select the engine to be run */
264 case LP55XX_ENGINE_1
:
265 case LP55XX_ENGINE_2
:
266 case LP55XX_ENGINE_3
:
267 mutex_lock(&chip
->lock
);
268 chip
->engine_idx
= val
;
269 ret
= lp55xx_request_firmware(chip
);
270 mutex_unlock(&chip
->lock
);
273 dev_err(dev
, "%lu: invalid engine index. (1, 2, 3)\n", val
);
278 dev_err(dev
, "request firmware err: %d\n", ret
);
285 static inline void lp55xx_run_engine(struct lp55xx_chip
*chip
, bool start
)
287 if (chip
->cfg
->run_engine
)
288 chip
->cfg
->run_engine(chip
, start
);
291 static ssize_t
lp55xx_store_engine_run(struct device
*dev
,
292 struct device_attribute
*attr
,
293 const char *buf
, size_t len
)
295 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
296 struct lp55xx_chip
*chip
= led
->chip
;
299 if (kstrtoul(buf
, 0, &val
))
302 /* run or stop the selected engine */
305 lp55xx_run_engine(chip
, false);
309 mutex_lock(&chip
->lock
);
310 lp55xx_run_engine(chip
, true);
311 mutex_unlock(&chip
->lock
);
316 static DEVICE_ATTR(select_engine
, S_IRUGO
| S_IWUSR
,
317 lp55xx_show_engine_select
, lp55xx_store_engine_select
);
318 static DEVICE_ATTR(run_engine
, S_IWUSR
, NULL
, lp55xx_store_engine_run
);
320 static struct attribute
*lp55xx_engine_attributes
[] = {
321 &dev_attr_select_engine
.attr
,
322 &dev_attr_run_engine
.attr
,
326 static const struct attribute_group lp55xx_engine_attr_group
= {
327 .attrs
= lp55xx_engine_attributes
,
330 int lp55xx_write(struct lp55xx_chip
*chip
, u8 reg
, u8 val
)
332 return i2c_smbus_write_byte_data(chip
->cl
, reg
, val
);
334 EXPORT_SYMBOL_GPL(lp55xx_write
);
336 int lp55xx_read(struct lp55xx_chip
*chip
, u8 reg
, u8
*val
)
340 ret
= i2c_smbus_read_byte_data(chip
->cl
, reg
);
347 EXPORT_SYMBOL_GPL(lp55xx_read
);
349 int lp55xx_update_bits(struct lp55xx_chip
*chip
, u8 reg
, u8 mask
, u8 val
)
354 ret
= lp55xx_read(chip
, reg
, &tmp
);
361 return lp55xx_write(chip
, reg
, tmp
);
363 EXPORT_SYMBOL_GPL(lp55xx_update_bits
);
365 bool lp55xx_is_extclk_used(struct lp55xx_chip
*chip
)
370 clk
= devm_clk_get(&chip
->cl
->dev
, "32k_clk");
372 goto use_internal_clk
;
374 err
= clk_prepare_enable(clk
);
376 goto use_internal_clk
;
378 if (clk_get_rate(clk
) != LP55XX_CLK_32K
) {
379 clk_disable_unprepare(clk
);
380 goto use_internal_clk
;
383 dev_info(&chip
->cl
->dev
, "%dHz external clock used\n", LP55XX_CLK_32K
);
389 dev_info(&chip
->cl
->dev
, "internal clock used\n");
392 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used
);
394 int lp55xx_init_device(struct lp55xx_chip
*chip
)
396 struct lp55xx_platform_data
*pdata
;
397 struct lp55xx_device_config
*cfg
;
398 struct device
*dev
= &chip
->cl
->dev
;
409 if (pdata
->setup_resources
) {
410 ret
= pdata
->setup_resources();
412 dev_err(dev
, "setup resoure err: %d\n", ret
);
419 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
421 usleep_range(1000, 2000); /* 500us abs min. */
424 lp55xx_reset_device(chip
);
427 * Exact value is not available. 10 - 20ms
428 * appears to be enough for reset.
430 usleep_range(10000, 20000);
432 ret
= lp55xx_detect_device(chip
);
434 dev_err(dev
, "device detection err: %d\n", ret
);
438 /* chip specific initialization */
439 ret
= lp55xx_post_init_device(chip
);
441 dev_err(dev
, "post init device err: %d\n", ret
);
448 lp55xx_deinit_device(chip
);
452 EXPORT_SYMBOL_GPL(lp55xx_init_device
);
454 void lp55xx_deinit_device(struct lp55xx_chip
*chip
)
456 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
459 clk_disable_unprepare(chip
->clk
);
464 if (pdata
->release_resources
)
465 pdata
->release_resources();
467 EXPORT_SYMBOL_GPL(lp55xx_deinit_device
);
469 int lp55xx_register_leds(struct lp55xx_led
*led
, struct lp55xx_chip
*chip
)
471 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
472 struct lp55xx_device_config
*cfg
= chip
->cfg
;
473 int num_channels
= pdata
->num_channels
;
474 struct lp55xx_led
*each
;
479 if (!cfg
->brightness_work_fn
) {
480 dev_err(&chip
->cl
->dev
, "empty brightness configuration\n");
484 for (i
= 0; i
< num_channels
; i
++) {
486 /* do not initialize channels that are not connected */
487 if (pdata
->led_config
[i
].led_current
== 0)
490 led_current
= pdata
->led_config
[i
].led_current
;
492 ret
= lp55xx_init_led(each
, chip
, i
);
496 INIT_WORK(&each
->brightness_work
, cfg
->brightness_work_fn
);
501 /* setting led current at each channel */
502 if (cfg
->set_led_current
)
503 cfg
->set_led_current(each
, led_current
);
509 lp55xx_unregister_leds(led
, chip
);
512 EXPORT_SYMBOL_GPL(lp55xx_register_leds
);
514 void lp55xx_unregister_leds(struct lp55xx_led
*led
, struct lp55xx_chip
*chip
)
517 struct lp55xx_led
*each
;
519 for (i
= 0; i
< chip
->num_leds
; i
++) {
521 led_classdev_unregister(&each
->cdev
);
522 flush_work(&each
->brightness_work
);
525 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds
);
527 int lp55xx_register_sysfs(struct lp55xx_chip
*chip
)
529 struct device
*dev
= &chip
->cl
->dev
;
530 struct lp55xx_device_config
*cfg
= chip
->cfg
;
533 if (!cfg
->run_engine
|| !cfg
->firmware_cb
)
534 goto dev_specific_attrs
;
536 ret
= sysfs_create_group(&dev
->kobj
, &lp55xx_engine_attr_group
);
541 return cfg
->dev_attr_group
?
542 sysfs_create_group(&dev
->kobj
, cfg
->dev_attr_group
) : 0;
544 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs
);
546 void lp55xx_unregister_sysfs(struct lp55xx_chip
*chip
)
548 struct device
*dev
= &chip
->cl
->dev
;
549 struct lp55xx_device_config
*cfg
= chip
->cfg
;
551 if (cfg
->dev_attr_group
)
552 sysfs_remove_group(&dev
->kobj
, cfg
->dev_attr_group
);
554 sysfs_remove_group(&dev
->kobj
, &lp55xx_engine_attr_group
);
556 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs
);
558 int lp55xx_of_populate_pdata(struct device
*dev
, struct device_node
*np
)
560 struct device_node
*child
;
561 struct lp55xx_platform_data
*pdata
;
562 struct lp55xx_led_config
*cfg
;
566 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
570 num_channels
= of_get_child_count(np
);
571 if (num_channels
== 0) {
572 dev_err(dev
, "no LED channels\n");
576 cfg
= devm_kzalloc(dev
, sizeof(*cfg
) * num_channels
, GFP_KERNEL
);
580 pdata
->led_config
= &cfg
[0];
581 pdata
->num_channels
= num_channels
;
583 for_each_child_of_node(np
, child
) {
586 of_property_read_string(child
, "chan-name", &cfg
[i
].name
);
587 of_property_read_u8(child
, "led-cur", &cfg
[i
].led_current
);
588 of_property_read_u8(child
, "max-cur", &cfg
[i
].max_current
);
593 of_property_read_string(np
, "label", &pdata
->label
);
594 of_property_read_u8(np
, "clock-mode", &pdata
->clock_mode
);
596 dev
->platform_data
= pdata
;
600 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata
);
602 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
603 MODULE_DESCRIPTION("LP55xx Common Driver");
604 MODULE_LICENSE("GPL");