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>
23 #include <linux/gpio.h>
24 #include <linux/of_gpio.h>
26 #include "leds-lp55xx-common.h"
28 /* External clock rate */
29 #define LP55XX_CLK_32K 32768
31 static struct lp55xx_led
*cdev_to_lp55xx_led(struct led_classdev
*cdev
)
33 return container_of(cdev
, struct lp55xx_led
, cdev
);
36 static struct lp55xx_led
*dev_to_lp55xx_led(struct device
*dev
)
38 return cdev_to_lp55xx_led(dev_get_drvdata(dev
));
41 static void lp55xx_reset_device(struct lp55xx_chip
*chip
)
43 struct lp55xx_device_config
*cfg
= chip
->cfg
;
44 u8 addr
= cfg
->reset
.addr
;
45 u8 val
= cfg
->reset
.val
;
47 /* no error checking here because no ACK from the device after reset */
48 lp55xx_write(chip
, addr
, val
);
51 static int lp55xx_detect_device(struct lp55xx_chip
*chip
)
53 struct lp55xx_device_config
*cfg
= chip
->cfg
;
54 u8 addr
= cfg
->enable
.addr
;
55 u8 val
= cfg
->enable
.val
;
58 ret
= lp55xx_write(chip
, addr
, val
);
62 usleep_range(1000, 2000);
64 ret
= lp55xx_read(chip
, addr
, &val
);
68 if (val
!= cfg
->enable
.val
)
74 static int lp55xx_post_init_device(struct lp55xx_chip
*chip
)
76 struct lp55xx_device_config
*cfg
= chip
->cfg
;
78 if (!cfg
->post_init_device
)
81 return cfg
->post_init_device(chip
);
84 static ssize_t
lp55xx_show_current(struct device
*dev
,
85 struct device_attribute
*attr
,
88 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
90 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->led_current
);
93 static ssize_t
lp55xx_store_current(struct device
*dev
,
94 struct device_attribute
*attr
,
95 const char *buf
, size_t len
)
97 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
98 struct lp55xx_chip
*chip
= led
->chip
;
101 if (kstrtoul(buf
, 0, &curr
))
104 if (curr
> led
->max_current
)
107 if (!chip
->cfg
->set_led_current
)
110 mutex_lock(&chip
->lock
);
111 chip
->cfg
->set_led_current(led
, (u8
)curr
);
112 mutex_unlock(&chip
->lock
);
117 static ssize_t
lp55xx_show_max_current(struct device
*dev
,
118 struct device_attribute
*attr
,
121 struct lp55xx_led
*led
= dev_to_lp55xx_led(dev
);
123 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->max_current
);
126 static DEVICE_ATTR(led_current
, S_IRUGO
| S_IWUSR
, lp55xx_show_current
,
127 lp55xx_store_current
);
128 static DEVICE_ATTR(max_current
, S_IRUGO
, lp55xx_show_max_current
, NULL
);
130 static struct attribute
*lp55xx_led_attrs
[] = {
131 &dev_attr_led_current
.attr
,
132 &dev_attr_max_current
.attr
,
135 ATTRIBUTE_GROUPS(lp55xx_led
);
137 static int lp55xx_set_brightness(struct led_classdev
*cdev
,
138 enum led_brightness brightness
)
140 struct lp55xx_led
*led
= cdev_to_lp55xx_led(cdev
);
141 struct lp55xx_device_config
*cfg
= led
->chip
->cfg
;
143 led
->brightness
= (u8
)brightness
;
144 return cfg
->brightness_fn(led
);
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
;
168 led
->cdev
.default_trigger
= pdata
->led_config
[chan
].default_trigger
;
170 if (led
->chan_nr
>= max_channel
) {
171 dev_err(dev
, "Use channel numbers between 0 and %d\n",
176 led
->cdev
.brightness_set_blocking
= lp55xx_set_brightness
;
177 led
->cdev
.groups
= lp55xx_led_groups
;
179 if (pdata
->led_config
[chan
].name
) {
180 led
->cdev
.name
= pdata
->led_config
[chan
].name
;
182 snprintf(name
, sizeof(name
), "%s:channel%d",
183 pdata
->label
? : chip
->cl
->name
, chan
);
184 led
->cdev
.name
= name
;
187 ret
= led_classdev_register(dev
, &led
->cdev
);
189 dev_err(dev
, "led register err: %d\n", ret
);
196 static void lp55xx_firmware_loaded(const struct firmware
*fw
, void *context
)
198 struct lp55xx_chip
*chip
= context
;
199 struct device
*dev
= &chip
->cl
->dev
;
200 enum lp55xx_engine_index idx
= chip
->engine_idx
;
203 dev_err(dev
, "firmware request failed\n");
207 /* handling firmware data is chip dependent */
208 mutex_lock(&chip
->lock
);
210 chip
->engines
[idx
- 1].mode
= LP55XX_ENGINE_LOAD
;
212 if (chip
->cfg
->firmware_cb
)
213 chip
->cfg
->firmware_cb(chip
);
215 mutex_unlock(&chip
->lock
);
218 /* firmware should be released for other channel use */
219 release_firmware(chip
->fw
);
222 static int lp55xx_request_firmware(struct lp55xx_chip
*chip
)
224 const char *name
= chip
->cl
->name
;
225 struct device
*dev
= &chip
->cl
->dev
;
227 return request_firmware_nowait(THIS_MODULE
, false, name
, dev
,
228 GFP_KERNEL
, chip
, lp55xx_firmware_loaded
);
231 static ssize_t
lp55xx_show_engine_select(struct device
*dev
,
232 struct device_attribute
*attr
,
235 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
236 struct lp55xx_chip
*chip
= led
->chip
;
238 return sprintf(buf
, "%d\n", chip
->engine_idx
);
241 static ssize_t
lp55xx_store_engine_select(struct device
*dev
,
242 struct device_attribute
*attr
,
243 const char *buf
, size_t len
)
245 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
246 struct lp55xx_chip
*chip
= led
->chip
;
250 if (kstrtoul(buf
, 0, &val
))
253 /* select the engine to be run */
256 case LP55XX_ENGINE_1
:
257 case LP55XX_ENGINE_2
:
258 case LP55XX_ENGINE_3
:
259 mutex_lock(&chip
->lock
);
260 chip
->engine_idx
= val
;
261 ret
= lp55xx_request_firmware(chip
);
262 mutex_unlock(&chip
->lock
);
265 dev_err(dev
, "%lu: invalid engine index. (1, 2, 3)\n", val
);
270 dev_err(dev
, "request firmware err: %d\n", ret
);
277 static inline void lp55xx_run_engine(struct lp55xx_chip
*chip
, bool start
)
279 if (chip
->cfg
->run_engine
)
280 chip
->cfg
->run_engine(chip
, start
);
283 static ssize_t
lp55xx_store_engine_run(struct device
*dev
,
284 struct device_attribute
*attr
,
285 const char *buf
, size_t len
)
287 struct lp55xx_led
*led
= i2c_get_clientdata(to_i2c_client(dev
));
288 struct lp55xx_chip
*chip
= led
->chip
;
291 if (kstrtoul(buf
, 0, &val
))
294 /* run or stop the selected engine */
297 lp55xx_run_engine(chip
, false);
301 mutex_lock(&chip
->lock
);
302 lp55xx_run_engine(chip
, true);
303 mutex_unlock(&chip
->lock
);
308 static DEVICE_ATTR(select_engine
, S_IRUGO
| S_IWUSR
,
309 lp55xx_show_engine_select
, lp55xx_store_engine_select
);
310 static DEVICE_ATTR(run_engine
, S_IWUSR
, NULL
, lp55xx_store_engine_run
);
312 static struct attribute
*lp55xx_engine_attributes
[] = {
313 &dev_attr_select_engine
.attr
,
314 &dev_attr_run_engine
.attr
,
318 static const struct attribute_group lp55xx_engine_attr_group
= {
319 .attrs
= lp55xx_engine_attributes
,
322 int lp55xx_write(struct lp55xx_chip
*chip
, u8 reg
, u8 val
)
324 return i2c_smbus_write_byte_data(chip
->cl
, reg
, val
);
326 EXPORT_SYMBOL_GPL(lp55xx_write
);
328 int lp55xx_read(struct lp55xx_chip
*chip
, u8 reg
, u8
*val
)
332 ret
= i2c_smbus_read_byte_data(chip
->cl
, reg
);
339 EXPORT_SYMBOL_GPL(lp55xx_read
);
341 int lp55xx_update_bits(struct lp55xx_chip
*chip
, u8 reg
, u8 mask
, u8 val
)
346 ret
= lp55xx_read(chip
, reg
, &tmp
);
353 return lp55xx_write(chip
, reg
, tmp
);
355 EXPORT_SYMBOL_GPL(lp55xx_update_bits
);
357 bool lp55xx_is_extclk_used(struct lp55xx_chip
*chip
)
362 clk
= devm_clk_get(&chip
->cl
->dev
, "32k_clk");
364 goto use_internal_clk
;
366 err
= clk_prepare_enable(clk
);
368 goto use_internal_clk
;
370 if (clk_get_rate(clk
) != LP55XX_CLK_32K
) {
371 clk_disable_unprepare(clk
);
372 goto use_internal_clk
;
375 dev_info(&chip
->cl
->dev
, "%dHz external clock used\n", LP55XX_CLK_32K
);
381 dev_info(&chip
->cl
->dev
, "internal clock used\n");
384 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used
);
386 int lp55xx_init_device(struct lp55xx_chip
*chip
)
388 struct lp55xx_platform_data
*pdata
;
389 struct lp55xx_device_config
*cfg
;
390 struct device
*dev
= &chip
->cl
->dev
;
401 if (gpio_is_valid(pdata
->enable_gpio
)) {
402 ret
= devm_gpio_request_one(dev
, pdata
->enable_gpio
,
403 GPIOF_DIR_OUT
, "lp5523_enable");
405 dev_err(dev
, "could not acquire enable gpio (err=%d)\n",
410 gpio_set_value(pdata
->enable_gpio
, 0);
411 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
412 gpio_set_value(pdata
->enable_gpio
, 1);
413 usleep_range(1000, 2000); /* 500us abs min. */
416 lp55xx_reset_device(chip
);
419 * Exact value is not available. 10 - 20ms
420 * appears to be enough for reset.
422 usleep_range(10000, 20000);
424 ret
= lp55xx_detect_device(chip
);
426 dev_err(dev
, "device detection err: %d\n", ret
);
430 /* chip specific initialization */
431 ret
= lp55xx_post_init_device(chip
);
433 dev_err(dev
, "post init device err: %d\n", ret
);
440 lp55xx_deinit_device(chip
);
444 EXPORT_SYMBOL_GPL(lp55xx_init_device
);
446 void lp55xx_deinit_device(struct lp55xx_chip
*chip
)
448 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
451 clk_disable_unprepare(chip
->clk
);
453 if (gpio_is_valid(pdata
->enable_gpio
))
454 gpio_set_value(pdata
->enable_gpio
, 0);
456 EXPORT_SYMBOL_GPL(lp55xx_deinit_device
);
458 int lp55xx_register_leds(struct lp55xx_led
*led
, struct lp55xx_chip
*chip
)
460 struct lp55xx_platform_data
*pdata
= chip
->pdata
;
461 struct lp55xx_device_config
*cfg
= chip
->cfg
;
462 int num_channels
= pdata
->num_channels
;
463 struct lp55xx_led
*each
;
468 if (!cfg
->brightness_fn
) {
469 dev_err(&chip
->cl
->dev
, "empty brightness configuration\n");
473 for (i
= 0; i
< num_channels
; i
++) {
475 /* do not initialize channels that are not connected */
476 if (pdata
->led_config
[i
].led_current
== 0)
479 led_current
= pdata
->led_config
[i
].led_current
;
481 ret
= lp55xx_init_led(each
, chip
, i
);
488 /* setting led current at each channel */
489 if (cfg
->set_led_current
)
490 cfg
->set_led_current(each
, led_current
);
496 lp55xx_unregister_leds(led
, chip
);
499 EXPORT_SYMBOL_GPL(lp55xx_register_leds
);
501 void lp55xx_unregister_leds(struct lp55xx_led
*led
, struct lp55xx_chip
*chip
)
504 struct lp55xx_led
*each
;
506 for (i
= 0; i
< chip
->num_leds
; i
++) {
508 led_classdev_unregister(&each
->cdev
);
511 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds
);
513 int lp55xx_register_sysfs(struct lp55xx_chip
*chip
)
515 struct device
*dev
= &chip
->cl
->dev
;
516 struct lp55xx_device_config
*cfg
= chip
->cfg
;
519 if (!cfg
->run_engine
|| !cfg
->firmware_cb
)
520 goto dev_specific_attrs
;
522 ret
= sysfs_create_group(&dev
->kobj
, &lp55xx_engine_attr_group
);
527 return cfg
->dev_attr_group
?
528 sysfs_create_group(&dev
->kobj
, cfg
->dev_attr_group
) : 0;
530 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs
);
532 void lp55xx_unregister_sysfs(struct lp55xx_chip
*chip
)
534 struct device
*dev
= &chip
->cl
->dev
;
535 struct lp55xx_device_config
*cfg
= chip
->cfg
;
537 if (cfg
->dev_attr_group
)
538 sysfs_remove_group(&dev
->kobj
, cfg
->dev_attr_group
);
540 sysfs_remove_group(&dev
->kobj
, &lp55xx_engine_attr_group
);
542 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs
);
544 struct lp55xx_platform_data
*lp55xx_of_populate_pdata(struct device
*dev
,
545 struct device_node
*np
)
547 struct device_node
*child
;
548 struct lp55xx_platform_data
*pdata
;
549 struct lp55xx_led_config
*cfg
;
553 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
555 return ERR_PTR(-ENOMEM
);
557 num_channels
= of_get_child_count(np
);
558 if (num_channels
== 0) {
559 dev_err(dev
, "no LED channels\n");
560 return ERR_PTR(-EINVAL
);
563 cfg
= devm_kzalloc(dev
, sizeof(*cfg
) * num_channels
, GFP_KERNEL
);
565 return ERR_PTR(-ENOMEM
);
567 pdata
->led_config
= &cfg
[0];
568 pdata
->num_channels
= num_channels
;
570 for_each_child_of_node(np
, child
) {
573 of_property_read_string(child
, "chan-name", &cfg
[i
].name
);
574 of_property_read_u8(child
, "led-cur", &cfg
[i
].led_current
);
575 of_property_read_u8(child
, "max-cur", &cfg
[i
].max_current
);
576 cfg
[i
].default_trigger
=
577 of_get_property(child
, "linux,default-trigger", NULL
);
582 of_property_read_string(np
, "label", &pdata
->label
);
583 of_property_read_u8(np
, "clock-mode", &pdata
->clock_mode
);
585 pdata
->enable_gpio
= of_get_named_gpio(np
, "enable-gpio", 0);
587 /* LP8501 specific */
588 of_property_read_u8(np
, "pwr-sel", (u8
*)&pdata
->pwr_sel
);
592 EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata
);
594 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
595 MODULE_DESCRIPTION("LP55xx Common Driver");
596 MODULE_LICENSE("GPL");