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 void lp55xx_reset_device(struct lp55xx_chip *chip)
30 struct lp55xx_device_config *cfg = chip->cfg;
31 u8 addr = cfg->reset.addr;
32 u8 val = cfg->reset.val;
34 /* no error checking here because no ACK from the device after reset */
35 lp55xx_write(chip, addr, val);
38 static int lp55xx_detect_device(struct lp55xx_chip *chip)
40 struct lp55xx_device_config *cfg = chip->cfg;
41 u8 addr = cfg->enable.addr;
42 u8 val = cfg->enable.val;
45 ret = lp55xx_write(chip, addr, val);
49 usleep_range(1000, 2000);
51 ret = lp55xx_read(chip, addr, &val);
55 if (val != cfg->enable.val)
61 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
63 struct lp55xx_device_config *cfg = chip->cfg;
65 if (!cfg->post_init_device)
68 return cfg->post_init_device(chip);
71 static struct attribute *lp55xx_led_attributes[] = {
75 static struct attribute_group lp55xx_led_attr_group = {
76 .attrs = lp55xx_led_attributes
79 static void lp55xx_set_brightness(struct led_classdev *cdev,
80 enum led_brightness brightness)
82 struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
84 led->brightness = (u8)brightness;
85 schedule_work(&led->brightness_work);
88 static int lp55xx_init_led(struct lp55xx_led *led,
89 struct lp55xx_chip *chip, int chan)
91 struct lp55xx_platform_data *pdata = chip->pdata;
92 struct lp55xx_device_config *cfg = chip->cfg;
93 struct device *dev = &chip->cl->dev;
96 int max_channel = cfg->max_channel;
98 if (chan >= max_channel) {
99 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
103 if (pdata->led_config[chan].led_current == 0)
106 led->led_current = pdata->led_config[chan].led_current;
107 led->max_current = pdata->led_config[chan].max_current;
108 led->chan_nr = pdata->led_config[chan].chan_nr;
110 if (led->chan_nr >= max_channel) {
111 dev_err(dev, "Use channel numbers between 0 and %d\n",
116 led->cdev.brightness_set = lp55xx_set_brightness;
118 if (pdata->led_config[chan].name) {
119 led->cdev.name = pdata->led_config[chan].name;
121 snprintf(name, sizeof(name), "%s:channel%d",
122 pdata->label ? : chip->cl->name, chan);
123 led->cdev.name = name;
127 * register led class device for each channel and
128 * add device attributes
131 ret = led_classdev_register(dev, &led->cdev);
133 dev_err(dev, "led register err: %d\n", ret);
137 ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
139 dev_err(dev, "led sysfs err: %d\n", ret);
140 led_classdev_unregister(&led->cdev);
147 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
149 return i2c_smbus_write_byte_data(chip->cl, reg, val);
151 EXPORT_SYMBOL_GPL(lp55xx_write);
153 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
157 ret = i2c_smbus_read_byte_data(chip->cl, reg);
164 EXPORT_SYMBOL_GPL(lp55xx_read);
166 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
171 ret = lp55xx_read(chip, reg, &tmp);
178 return lp55xx_write(chip, reg, tmp);
180 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
182 int lp55xx_init_device(struct lp55xx_chip *chip)
184 struct lp55xx_platform_data *pdata;
185 struct lp55xx_device_config *cfg;
186 struct device *dev = &chip->cl->dev;
197 if (pdata->setup_resources) {
198 ret = pdata->setup_resources();
200 dev_err(dev, "setup resoure err: %d\n", ret);
207 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
209 usleep_range(1000, 2000); /* 500us abs min. */
212 lp55xx_reset_device(chip);
215 * Exact value is not available. 10 - 20ms
216 * appears to be enough for reset.
218 usleep_range(10000, 20000);
220 ret = lp55xx_detect_device(chip);
222 dev_err(dev, "device detection err: %d\n", ret);
226 /* chip specific initialization */
227 ret = lp55xx_post_init_device(chip);
229 dev_err(dev, "post init device err: %d\n", ret);
236 lp55xx_deinit_device(chip);
240 EXPORT_SYMBOL_GPL(lp55xx_init_device);
242 void lp55xx_deinit_device(struct lp55xx_chip *chip)
244 struct lp55xx_platform_data *pdata = chip->pdata;
249 if (pdata->release_resources)
250 pdata->release_resources();
252 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
254 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
256 struct lp55xx_platform_data *pdata = chip->pdata;
257 struct lp55xx_device_config *cfg = chip->cfg;
258 int num_channels = pdata->num_channels;
259 struct lp55xx_led *each;
264 if (!cfg->brightness_work_fn) {
265 dev_err(&chip->cl->dev, "empty brightness configuration\n");
269 for (i = 0; i < num_channels; i++) {
271 /* do not initialize channels that are not connected */
272 if (pdata->led_config[i].led_current == 0)
275 led_current = pdata->led_config[i].led_current;
277 ret = lp55xx_init_led(each, chip, i);
281 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
286 /* setting led current at each channel */
287 if (cfg->set_led_current)
288 cfg->set_led_current(each, led_current);
294 for (i = 0; i < chip->num_leds; i++) {
296 led_classdev_unregister(&each->cdev);
297 flush_work(&each->brightness_work);
301 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
303 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
304 MODULE_DESCRIPTION("LP55xx Common Driver");
305 MODULE_LICENSE("GPL");