2 * LP5521/LP5523/LP55231 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/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/platform_data/leds-lp55xx.h>
21 #include "leds-lp55xx-common.h"
23 static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
25 return container_of(cdev, struct lp55xx_led, cdev);
28 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
30 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
33 static void lp55xx_reset_device(struct lp55xx_chip *chip)
35 struct lp55xx_device_config *cfg = chip->cfg;
36 u8 addr = cfg->reset.addr;
37 u8 val = cfg->reset.val;
39 /* no error checking here because no ACK from the device after reset */
40 lp55xx_write(chip, addr, val);
43 static int lp55xx_detect_device(struct lp55xx_chip *chip)
45 struct lp55xx_device_config *cfg = chip->cfg;
46 u8 addr = cfg->enable.addr;
47 u8 val = cfg->enable.val;
50 ret = lp55xx_write(chip, addr, val);
54 usleep_range(1000, 2000);
56 ret = lp55xx_read(chip, addr, &val);
60 if (val != cfg->enable.val)
66 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
68 struct lp55xx_device_config *cfg = chip->cfg;
70 if (!cfg->post_init_device)
73 return cfg->post_init_device(chip);
76 static ssize_t lp55xx_show_current(struct device *dev,
77 struct device_attribute *attr,
80 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
82 return sprintf(buf, "%d\n", led->led_current);
85 static ssize_t lp55xx_store_current(struct device *dev,
86 struct device_attribute *attr,
87 const char *buf, size_t len)
89 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
90 struct lp55xx_chip *chip = led->chip;
93 if (kstrtoul(buf, 0, &curr))
96 if (curr > led->max_current)
99 if (!chip->cfg->set_led_current)
102 mutex_lock(&chip->lock);
103 chip->cfg->set_led_current(led, (u8)curr);
104 mutex_unlock(&chip->lock);
109 static ssize_t lp55xx_show_max_current(struct device *dev,
110 struct device_attribute *attr,
113 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
115 return sprintf(buf, "%d\n", led->max_current);
118 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
119 lp55xx_store_current);
120 static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
122 static struct attribute *lp55xx_led_attributes[] = {
123 &dev_attr_led_current.attr,
124 &dev_attr_max_current.attr,
128 static struct attribute_group lp55xx_led_attr_group = {
129 .attrs = lp55xx_led_attributes
132 static void lp55xx_set_brightness(struct led_classdev *cdev,
133 enum led_brightness brightness)
135 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
137 led->brightness = (u8)brightness;
138 schedule_work(&led->brightness_work);
141 static int lp55xx_init_led(struct lp55xx_led *led,
142 struct lp55xx_chip *chip, int chan)
144 struct lp55xx_platform_data *pdata = chip->pdata;
145 struct lp55xx_device_config *cfg = chip->cfg;
146 struct device *dev = &chip->cl->dev;
149 int max_channel = cfg->max_channel;
151 if (chan >= max_channel) {
152 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
156 if (pdata->led_config[chan].led_current == 0)
159 led->led_current = pdata->led_config[chan].led_current;
160 led->max_current = pdata->led_config[chan].max_current;
161 led->chan_nr = pdata->led_config[chan].chan_nr;
163 if (led->chan_nr >= max_channel) {
164 dev_err(dev, "Use channel numbers between 0 and %d\n",
169 led->cdev.brightness_set = lp55xx_set_brightness;
171 if (pdata->led_config[chan].name) {
172 led->cdev.name = pdata->led_config[chan].name;
174 snprintf(name, sizeof(name), "%s:channel%d",
175 pdata->label ? : chip->cl->name, chan);
176 led->cdev.name = name;
180 * register led class device for each channel and
181 * add device attributes
184 ret = led_classdev_register(dev, &led->cdev);
186 dev_err(dev, "led register err: %d\n", ret);
190 ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
192 dev_err(dev, "led sysfs err: %d\n", ret);
193 led_classdev_unregister(&led->cdev);
200 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
202 return i2c_smbus_write_byte_data(chip->cl, reg, val);
204 EXPORT_SYMBOL_GPL(lp55xx_write);
206 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
210 ret = i2c_smbus_read_byte_data(chip->cl, reg);
217 EXPORT_SYMBOL_GPL(lp55xx_read);
219 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
224 ret = lp55xx_read(chip, reg, &tmp);
231 return lp55xx_write(chip, reg, tmp);
233 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
235 int lp55xx_init_device(struct lp55xx_chip *chip)
237 struct lp55xx_platform_data *pdata;
238 struct lp55xx_device_config *cfg;
239 struct device *dev = &chip->cl->dev;
250 if (pdata->setup_resources) {
251 ret = pdata->setup_resources();
253 dev_err(dev, "setup resoure err: %d\n", ret);
260 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
262 usleep_range(1000, 2000); /* 500us abs min. */
265 lp55xx_reset_device(chip);
268 * Exact value is not available. 10 - 20ms
269 * appears to be enough for reset.
271 usleep_range(10000, 20000);
273 ret = lp55xx_detect_device(chip);
275 dev_err(dev, "device detection err: %d\n", ret);
279 /* chip specific initialization */
280 ret = lp55xx_post_init_device(chip);
282 dev_err(dev, "post init device err: %d\n", ret);
289 lp55xx_deinit_device(chip);
293 EXPORT_SYMBOL_GPL(lp55xx_init_device);
295 void lp55xx_deinit_device(struct lp55xx_chip *chip)
297 struct lp55xx_platform_data *pdata = chip->pdata;
302 if (pdata->release_resources)
303 pdata->release_resources();
305 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
307 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
309 struct lp55xx_platform_data *pdata = chip->pdata;
310 struct lp55xx_device_config *cfg = chip->cfg;
311 int num_channels = pdata->num_channels;
312 struct lp55xx_led *each;
317 if (!cfg->brightness_work_fn) {
318 dev_err(&chip->cl->dev, "empty brightness configuration\n");
322 for (i = 0; i < num_channels; i++) {
324 /* do not initialize channels that are not connected */
325 if (pdata->led_config[i].led_current == 0)
328 led_current = pdata->led_config[i].led_current;
330 ret = lp55xx_init_led(each, chip, i);
334 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
339 /* setting led current at each channel */
340 if (cfg->set_led_current)
341 cfg->set_led_current(each, led_current);
347 lp55xx_unregister_leds(led, chip);
350 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
352 void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
355 struct lp55xx_led *each;
356 struct kobject *kobj;
358 for (i = 0; i < chip->num_leds; i++) {
360 kobj = &led->cdev.dev->kobj;
361 sysfs_remove_group(kobj, &lp55xx_led_attr_group);
362 led_classdev_unregister(&each->cdev);
363 flush_work(&each->brightness_work);
366 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
368 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
369 MODULE_DESCRIPTION("LP55xx Common Driver");
370 MODULE_LICENSE("GPL");