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>
23 #include "leds-lp55xx-common.h"
25 /* External clock rate */
26 #define LP55XX_CLK_32K 32768
28 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
30 return container_of(cdev, struct lp55xx_led, cdev);
33 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
35 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
38 static void lp55xx_reset_device(struct lp55xx_chip *chip)
40 struct lp55xx_device_config *cfg = chip->cfg;
41 u8 addr = cfg->reset.addr;
42 u8 val = cfg->reset.val;
44 /* no error checking here because no ACK from the device after reset */
45 lp55xx_write(chip, addr, val);
48 static int lp55xx_detect_device(struct lp55xx_chip *chip)
50 struct lp55xx_device_config *cfg = chip->cfg;
51 u8 addr = cfg->enable.addr;
52 u8 val = cfg->enable.val;
55 ret = lp55xx_write(chip, addr, val);
59 usleep_range(1000, 2000);
61 ret = lp55xx_read(chip, addr, &val);
65 if (val != cfg->enable.val)
71 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
73 struct lp55xx_device_config *cfg = chip->cfg;
75 if (!cfg->post_init_device)
78 return cfg->post_init_device(chip);
81 static ssize_t lp55xx_show_current(struct device *dev,
82 struct device_attribute *attr,
85 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
87 return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current);
90 static ssize_t lp55xx_store_current(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t len)
94 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
95 struct lp55xx_chip *chip = led->chip;
98 if (kstrtoul(buf, 0, &curr))
101 if (curr > led->max_current)
104 if (!chip->cfg->set_led_current)
107 mutex_lock(&chip->lock);
108 chip->cfg->set_led_current(led, (u8)curr);
109 mutex_unlock(&chip->lock);
114 static ssize_t lp55xx_show_max_current(struct device *dev,
115 struct device_attribute *attr,
118 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
120 return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current);
123 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
124 lp55xx_store_current);
125 static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
127 static struct attribute *lp55xx_led_attributes[] = {
128 &dev_attr_led_current.attr,
129 &dev_attr_max_current.attr,
133 static struct attribute_group lp55xx_led_attr_group = {
134 .attrs = lp55xx_led_attributes
137 static void lp55xx_set_brightness(struct led_classdev *cdev,
138 enum led_brightness brightness)
140 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
142 led->brightness = (u8)brightness;
143 schedule_work(&led->brightness_work);
146 static int lp55xx_init_led(struct lp55xx_led *led,
147 struct lp55xx_chip *chip, int chan)
149 struct lp55xx_platform_data *pdata = chip->pdata;
150 struct lp55xx_device_config *cfg = chip->cfg;
151 struct device *dev = &chip->cl->dev;
154 int max_channel = cfg->max_channel;
156 if (chan >= max_channel) {
157 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
161 if (pdata->led_config[chan].led_current == 0)
164 led->led_current = pdata->led_config[chan].led_current;
165 led->max_current = pdata->led_config[chan].max_current;
166 led->chan_nr = pdata->led_config[chan].chan_nr;
168 if (led->chan_nr >= max_channel) {
169 dev_err(dev, "Use channel numbers between 0 and %d\n",
174 led->cdev.brightness_set = lp55xx_set_brightness;
176 if (pdata->led_config[chan].name) {
177 led->cdev.name = pdata->led_config[chan].name;
179 snprintf(name, sizeof(name), "%s:channel%d",
180 pdata->label ? : chip->cl->name, chan);
181 led->cdev.name = name;
185 * register led class device for each channel and
186 * add device attributes
189 ret = led_classdev_register(dev, &led->cdev);
191 dev_err(dev, "led register err: %d\n", ret);
195 ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
197 dev_err(dev, "led sysfs err: %d\n", ret);
198 led_classdev_unregister(&led->cdev);
205 static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
207 struct lp55xx_chip *chip = context;
208 struct device *dev = &chip->cl->dev;
211 dev_err(dev, "firmware request failed\n");
215 /* handling firmware data is chip dependent */
216 mutex_lock(&chip->lock);
219 if (chip->cfg->firmware_cb)
220 chip->cfg->firmware_cb(chip);
222 mutex_unlock(&chip->lock);
225 /* firmware should be released for other channel use */
226 release_firmware(chip->fw);
229 static int lp55xx_request_firmware(struct lp55xx_chip *chip)
231 const char *name = chip->cl->name;
232 struct device *dev = &chip->cl->dev;
234 return request_firmware_nowait(THIS_MODULE, true, name, dev,
235 GFP_KERNEL, chip, lp55xx_firmware_loaded);
238 static ssize_t lp55xx_show_engine_select(struct device *dev,
239 struct device_attribute *attr,
242 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
243 struct lp55xx_chip *chip = led->chip;
245 return sprintf(buf, "%d\n", chip->engine_idx);
248 static ssize_t lp55xx_store_engine_select(struct device *dev,
249 struct device_attribute *attr,
250 const char *buf, size_t len)
252 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
253 struct lp55xx_chip *chip = led->chip;
257 if (kstrtoul(buf, 0, &val))
260 /* select the engine to be run */
263 case LP55XX_ENGINE_1:
264 case LP55XX_ENGINE_2:
265 case LP55XX_ENGINE_3:
266 mutex_lock(&chip->lock);
267 chip->engine_idx = val;
268 ret = lp55xx_request_firmware(chip);
269 mutex_unlock(&chip->lock);
272 dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
277 dev_err(dev, "request firmware err: %d\n", ret);
284 static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
286 if (chip->cfg->run_engine)
287 chip->cfg->run_engine(chip, start);
290 static ssize_t lp55xx_store_engine_run(struct device *dev,
291 struct device_attribute *attr,
292 const char *buf, size_t len)
294 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
295 struct lp55xx_chip *chip = led->chip;
298 if (kstrtoul(buf, 0, &val))
301 /* run or stop the selected engine */
304 lp55xx_run_engine(chip, false);
308 mutex_lock(&chip->lock);
309 lp55xx_run_engine(chip, true);
310 mutex_unlock(&chip->lock);
315 static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR,
316 lp55xx_show_engine_select, lp55xx_store_engine_select);
317 static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run);
319 static struct attribute *lp55xx_engine_attributes[] = {
320 &dev_attr_select_engine.attr,
321 &dev_attr_run_engine.attr,
325 static const struct attribute_group lp55xx_engine_attr_group = {
326 .attrs = lp55xx_engine_attributes,
329 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
331 return i2c_smbus_write_byte_data(chip->cl, reg, val);
333 EXPORT_SYMBOL_GPL(lp55xx_write);
335 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
339 ret = i2c_smbus_read_byte_data(chip->cl, reg);
346 EXPORT_SYMBOL_GPL(lp55xx_read);
348 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
353 ret = lp55xx_read(chip, reg, &tmp);
360 return lp55xx_write(chip, reg, tmp);
362 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
364 bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
369 clk = devm_clk_get(&chip->cl->dev, "32k_clk");
371 goto use_internal_clk;
373 err = clk_prepare_enable(clk);
375 goto use_internal_clk;
377 if (clk_get_rate(clk) != LP55XX_CLK_32K) {
378 clk_disable_unprepare(clk);
379 goto use_internal_clk;
382 dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
388 dev_info(&chip->cl->dev, "internal clock used\n");
391 EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
393 int lp55xx_init_device(struct lp55xx_chip *chip)
395 struct lp55xx_platform_data *pdata;
396 struct lp55xx_device_config *cfg;
397 struct device *dev = &chip->cl->dev;
408 if (pdata->setup_resources) {
409 ret = pdata->setup_resources();
411 dev_err(dev, "setup resoure err: %d\n", ret);
418 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
420 usleep_range(1000, 2000); /* 500us abs min. */
423 lp55xx_reset_device(chip);
426 * Exact value is not available. 10 - 20ms
427 * appears to be enough for reset.
429 usleep_range(10000, 20000);
431 ret = lp55xx_detect_device(chip);
433 dev_err(dev, "device detection err: %d\n", ret);
437 /* chip specific initialization */
438 ret = lp55xx_post_init_device(chip);
440 dev_err(dev, "post init device err: %d\n", ret);
447 lp55xx_deinit_device(chip);
451 EXPORT_SYMBOL_GPL(lp55xx_init_device);
453 void lp55xx_deinit_device(struct lp55xx_chip *chip)
455 struct lp55xx_platform_data *pdata = chip->pdata;
458 clk_disable_unprepare(chip->clk);
463 if (pdata->release_resources)
464 pdata->release_resources();
466 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
468 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
470 struct lp55xx_platform_data *pdata = chip->pdata;
471 struct lp55xx_device_config *cfg = chip->cfg;
472 int num_channels = pdata->num_channels;
473 struct lp55xx_led *each;
478 if (!cfg->brightness_work_fn) {
479 dev_err(&chip->cl->dev, "empty brightness configuration\n");
483 for (i = 0; i < num_channels; i++) {
485 /* do not initialize channels that are not connected */
486 if (pdata->led_config[i].led_current == 0)
489 led_current = pdata->led_config[i].led_current;
491 ret = lp55xx_init_led(each, chip, i);
495 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
500 /* setting led current at each channel */
501 if (cfg->set_led_current)
502 cfg->set_led_current(each, led_current);
508 lp55xx_unregister_leds(led, chip);
511 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
513 void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
516 struct lp55xx_led *each;
518 for (i = 0; i < chip->num_leds; i++) {
520 led_classdev_unregister(&each->cdev);
521 flush_work(&each->brightness_work);
524 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
526 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
528 struct device *dev = &chip->cl->dev;
529 struct lp55xx_device_config *cfg = chip->cfg;
532 if (!cfg->run_engine || !cfg->firmware_cb)
533 goto dev_specific_attrs;
535 ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
540 return cfg->dev_attr_group ?
541 sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
543 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
545 void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
547 struct device *dev = &chip->cl->dev;
548 struct lp55xx_device_config *cfg = chip->cfg;
550 if (cfg->dev_attr_group)
551 sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
553 sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
555 EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
557 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
558 MODULE_DESCRIPTION("LP55xx Common Driver");
559 MODULE_LICENSE("GPL");