]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/leds/leds-lp55xx-common.c
leds-lp55xx: use lp55xx_init_led() common function
[karo-tx-linux.git] / drivers / leds / leds-lp55xx-common.c
1 /*
2  * LP5521/LP5523/LP55231 Common Driver
3  *
4  * Copyright 2012 Texas Instruments
5  *
6  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7  *
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.
11  *
12  * Derived from leds-lp5521.c, leds-lp5523.c
13  */
14
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>
20
21 #include "leds-lp55xx-common.h"
22
23 static void lp55xx_reset_device(struct lp55xx_chip *chip)
24 {
25         struct lp55xx_device_config *cfg = chip->cfg;
26         u8 addr = cfg->reset.addr;
27         u8 val  = cfg->reset.val;
28
29         /* no error checking here because no ACK from the device after reset */
30         lp55xx_write(chip, addr, val);
31 }
32
33 static int lp55xx_detect_device(struct lp55xx_chip *chip)
34 {
35         struct lp55xx_device_config *cfg = chip->cfg;
36         u8 addr = cfg->enable.addr;
37         u8 val  = cfg->enable.val;
38         int ret;
39
40         ret = lp55xx_write(chip, addr, val);
41         if (ret)
42                 return ret;
43
44         usleep_range(1000, 2000);
45
46         ret = lp55xx_read(chip, addr, &val);
47         if (ret)
48                 return ret;
49
50         if (val != cfg->enable.val)
51                 return -ENODEV;
52
53         return 0;
54 }
55
56 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
57 {
58         struct lp55xx_device_config *cfg = chip->cfg;
59
60         if (!cfg->post_init_device)
61                 return 0;
62
63         return cfg->post_init_device(chip);
64 }
65
66 static struct attribute *lp55xx_led_attributes[] = {
67         NULL,
68 };
69
70 static struct attribute_group lp55xx_led_attr_group = {
71         .attrs = lp55xx_led_attributes
72 };
73
74 static void lp55xx_set_brightness(struct led_classdev *cdev,
75                              enum led_brightness brightness)
76 {
77 }
78
79 static int lp55xx_init_led(struct lp55xx_led *led,
80                         struct lp55xx_chip *chip, int chan)
81 {
82         struct lp55xx_platform_data *pdata = chip->pdata;
83         struct lp55xx_device_config *cfg = chip->cfg;
84         struct device *dev = &chip->cl->dev;
85         char name[32];
86         int ret;
87         int max_channel = cfg->max_channel;
88
89         if (chan >= max_channel) {
90                 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
91                 return -EINVAL;
92         }
93
94         if (pdata->led_config[chan].led_current == 0)
95                 return 0;
96
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;
100
101         if (led->chan_nr >= max_channel) {
102                 dev_err(dev, "Use channel numbers between 0 and %d\n",
103                         max_channel - 1);
104                 return -EINVAL;
105         }
106
107         led->cdev.brightness_set = lp55xx_set_brightness;
108
109         if (pdata->led_config[chan].name) {
110                 led->cdev.name = pdata->led_config[chan].name;
111         } else {
112                 snprintf(name, sizeof(name), "%s:channel%d",
113                         pdata->label ? : chip->cl->name, chan);
114                 led->cdev.name = name;
115         }
116
117         /*
118          * register led class device for each channel and
119          * add device attributes
120          */
121
122         ret = led_classdev_register(dev, &led->cdev);
123         if (ret) {
124                 dev_err(dev, "led register err: %d\n", ret);
125                 return ret;
126         }
127
128         ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
129         if (ret) {
130                 dev_err(dev, "led sysfs err: %d\n", ret);
131                 led_classdev_unregister(&led->cdev);
132                 return ret;
133         }
134
135         return 0;
136 }
137
138 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
139 {
140         return i2c_smbus_write_byte_data(chip->cl, reg, val);
141 }
142 EXPORT_SYMBOL_GPL(lp55xx_write);
143
144 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
145 {
146         s32 ret;
147
148         ret = i2c_smbus_read_byte_data(chip->cl, reg);
149         if (ret < 0)
150                 return ret;
151
152         *val = ret;
153         return 0;
154 }
155 EXPORT_SYMBOL_GPL(lp55xx_read);
156
157 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
158 {
159         int ret;
160         u8 tmp;
161
162         ret = lp55xx_read(chip, reg, &tmp);
163         if (ret)
164                 return ret;
165
166         tmp &= ~mask;
167         tmp |= val & mask;
168
169         return lp55xx_write(chip, reg, tmp);
170 }
171 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
172
173 int lp55xx_init_device(struct lp55xx_chip *chip)
174 {
175         struct lp55xx_platform_data *pdata;
176         struct lp55xx_device_config *cfg;
177         struct device *dev = &chip->cl->dev;
178         int ret = 0;
179
180         WARN_ON(!chip);
181
182         pdata = chip->pdata;
183         cfg = chip->cfg;
184
185         if (!pdata || !cfg)
186                 return -EINVAL;
187
188         if (pdata->setup_resources) {
189                 ret = pdata->setup_resources();
190                 if (ret < 0) {
191                         dev_err(dev, "setup resoure err: %d\n", ret);
192                         goto err;
193                 }
194         }
195
196         if (pdata->enable) {
197                 pdata->enable(0);
198                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
199                 pdata->enable(1);
200                 usleep_range(1000, 2000); /* 500us abs min. */
201         }
202
203         lp55xx_reset_device(chip);
204
205         /*
206          * Exact value is not available. 10 - 20ms
207          * appears to be enough for reset.
208          */
209         usleep_range(10000, 20000);
210
211         ret = lp55xx_detect_device(chip);
212         if (ret) {
213                 dev_err(dev, "device detection err: %d\n", ret);
214                 goto err;
215         }
216
217         /* chip specific initialization */
218         ret = lp55xx_post_init_device(chip);
219         if (ret) {
220                 dev_err(dev, "post init device err: %d\n", ret);
221                 goto err_post_init;
222         }
223
224         return 0;
225
226 err_post_init:
227         lp55xx_deinit_device(chip);
228 err:
229         return ret;
230 }
231 EXPORT_SYMBOL_GPL(lp55xx_init_device);
232
233 void lp55xx_deinit_device(struct lp55xx_chip *chip)
234 {
235         struct lp55xx_platform_data *pdata = chip->pdata;
236
237         if (pdata->enable)
238                 pdata->enable(0);
239
240         if (pdata->release_resources)
241                 pdata->release_resources();
242 }
243 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
244
245 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
246 {
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;
251         u8 led_current;
252         int ret;
253         int i;
254
255         if (!cfg->brightness_work_fn) {
256                 dev_err(&chip->cl->dev, "empty brightness configuration\n");
257                 return -EINVAL;
258         }
259
260         for (i = 0; i < num_channels; i++) {
261
262                 /* do not initialize channels that are not connected */
263                 if (pdata->led_config[i].led_current == 0)
264                         continue;
265
266                 led_current = pdata->led_config[i].led_current;
267                 each = led + i;
268                 ret = lp55xx_init_led(each, chip, i);
269                 if (ret)
270                         goto err_init_led;
271
272                 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
273
274                 chip->num_leds++;
275                 each->chip = chip;
276
277                 /* setting led current at each channel */
278                 if (cfg->set_led_current)
279                         cfg->set_led_current(each, led_current);
280         }
281
282         return 0;
283
284 err_init_led:
285         for (i = 0; i < chip->num_leds; i++) {
286                 each = led + i;
287                 led_classdev_unregister(&each->cdev);
288                 flush_work(&each->brightness_work);
289         }
290         return ret;
291 }
292 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
293
294 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
295 MODULE_DESCRIPTION("LP55xx Common Driver");
296 MODULE_LICENSE("GPL");