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 void lp55xx_reset_device(struct lp55xx_chip *chip)
25 struct lp55xx_device_config *cfg = chip->cfg;
26 u8 addr = cfg->reset.addr;
27 u8 val = cfg->reset.val;
29 /* no error checking here because no ACK from the device after reset */
30 lp55xx_write(chip, addr, val);
33 static int lp55xx_detect_device(struct lp55xx_chip *chip)
35 struct lp55xx_device_config *cfg = chip->cfg;
36 u8 addr = cfg->enable.addr;
37 u8 val = cfg->enable.val;
40 ret = lp55xx_write(chip, addr, val);
44 usleep_range(1000, 2000);
46 ret = lp55xx_read(chip, addr, &val);
50 if (val != cfg->enable.val)
56 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
58 struct lp55xx_device_config *cfg = chip->cfg;
60 if (!cfg->post_init_device)
63 return cfg->post_init_device(chip);
66 static struct attribute *lp55xx_led_attributes[] = {
70 static struct attribute_group lp55xx_led_attr_group = {
71 .attrs = lp55xx_led_attributes
74 static void lp55xx_set_brightness(struct led_classdev *cdev,
75 enum led_brightness brightness)
79 static int lp55xx_init_led(struct lp55xx_led *led,
80 struct lp55xx_chip *chip, int chan)
82 struct lp55xx_platform_data *pdata = chip->pdata;
83 struct lp55xx_device_config *cfg = chip->cfg;
84 struct device *dev = &chip->cl->dev;
87 int max_channel = cfg->max_channel;
89 if (chan >= max_channel) {
90 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
94 if (pdata->led_config[chan].led_current == 0)
97 led->led_current = pdata->led_config[chan].led_current;
98 led->max_current = pdata->led_config[chan].max_current;
99 led->chan_nr = pdata->led_config[chan].chan_nr;
101 if (led->chan_nr >= max_channel) {
102 dev_err(dev, "Use channel numbers between 0 and %d\n",
107 led->cdev.brightness_set = lp55xx_set_brightness;
109 if (pdata->led_config[chan].name) {
110 led->cdev.name = pdata->led_config[chan].name;
112 snprintf(name, sizeof(name), "%s:channel%d",
113 pdata->label ? : chip->cl->name, chan);
114 led->cdev.name = name;
118 * register led class device for each channel and
119 * add device attributes
122 ret = led_classdev_register(dev, &led->cdev);
124 dev_err(dev, "led register err: %d\n", ret);
128 ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
130 dev_err(dev, "led sysfs err: %d\n", ret);
131 led_classdev_unregister(&led->cdev);
138 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
140 return i2c_smbus_write_byte_data(chip->cl, reg, val);
142 EXPORT_SYMBOL_GPL(lp55xx_write);
144 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
148 ret = i2c_smbus_read_byte_data(chip->cl, reg);
155 EXPORT_SYMBOL_GPL(lp55xx_read);
157 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
162 ret = lp55xx_read(chip, reg, &tmp);
169 return lp55xx_write(chip, reg, tmp);
171 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
173 int lp55xx_init_device(struct lp55xx_chip *chip)
175 struct lp55xx_platform_data *pdata;
176 struct lp55xx_device_config *cfg;
177 struct device *dev = &chip->cl->dev;
188 if (pdata->setup_resources) {
189 ret = pdata->setup_resources();
191 dev_err(dev, "setup resoure err: %d\n", ret);
198 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
200 usleep_range(1000, 2000); /* 500us abs min. */
203 lp55xx_reset_device(chip);
206 * Exact value is not available. 10 - 20ms
207 * appears to be enough for reset.
209 usleep_range(10000, 20000);
211 ret = lp55xx_detect_device(chip);
213 dev_err(dev, "device detection err: %d\n", ret);
217 /* chip specific initialization */
218 ret = lp55xx_post_init_device(chip);
220 dev_err(dev, "post init device err: %d\n", ret);
227 lp55xx_deinit_device(chip);
231 EXPORT_SYMBOL_GPL(lp55xx_init_device);
233 void lp55xx_deinit_device(struct lp55xx_chip *chip)
235 struct lp55xx_platform_data *pdata = chip->pdata;
240 if (pdata->release_resources)
241 pdata->release_resources();
243 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
245 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
247 struct lp55xx_platform_data *pdata = chip->pdata;
248 struct lp55xx_device_config *cfg = chip->cfg;
249 int num_channels = pdata->num_channels;
250 struct lp55xx_led *each;
255 if (!cfg->brightness_work_fn) {
256 dev_err(&chip->cl->dev, "empty brightness configuration\n");
260 for (i = 0; i < num_channels; i++) {
262 /* do not initialize channels that are not connected */
263 if (pdata->led_config[i].led_current == 0)
266 led_current = pdata->led_config[i].led_current;
268 ret = lp55xx_init_led(each, chip, i);
272 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
277 /* setting led current at each channel */
278 if (cfg->set_led_current)
279 cfg->set_led_current(each, led_current);
285 for (i = 0; i < chip->num_leds; i++) {
287 led_classdev_unregister(&each->cdev);
288 flush_work(&each->brightness_work);
292 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
294 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
295 MODULE_DESCRIPTION("LP55xx Common Driver");
296 MODULE_LICENSE("GPL");