]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/leds/leds-lp55xx-common.c
leds-lp55xx: add new lp55xx_register_sysfs() for the firmware interface
[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 struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
24 {
25         return container_of(cdev, struct lp55xx_led, cdev);
26 }
27
28 static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
29 {
30         return cdev_to_lp55xx_led(dev_get_drvdata(dev));
31 }
32
33 static void lp55xx_reset_device(struct lp55xx_chip *chip)
34 {
35         struct lp55xx_device_config *cfg = chip->cfg;
36         u8 addr = cfg->reset.addr;
37         u8 val  = cfg->reset.val;
38
39         /* no error checking here because no ACK from the device after reset */
40         lp55xx_write(chip, addr, val);
41 }
42
43 static int lp55xx_detect_device(struct lp55xx_chip *chip)
44 {
45         struct lp55xx_device_config *cfg = chip->cfg;
46         u8 addr = cfg->enable.addr;
47         u8 val  = cfg->enable.val;
48         int ret;
49
50         ret = lp55xx_write(chip, addr, val);
51         if (ret)
52                 return ret;
53
54         usleep_range(1000, 2000);
55
56         ret = lp55xx_read(chip, addr, &val);
57         if (ret)
58                 return ret;
59
60         if (val != cfg->enable.val)
61                 return -ENODEV;
62
63         return 0;
64 }
65
66 static int lp55xx_post_init_device(struct lp55xx_chip *chip)
67 {
68         struct lp55xx_device_config *cfg = chip->cfg;
69
70         if (!cfg->post_init_device)
71                 return 0;
72
73         return cfg->post_init_device(chip);
74 }
75
76 static ssize_t lp55xx_show_current(struct device *dev,
77                             struct device_attribute *attr,
78                             char *buf)
79 {
80         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
81
82         return sprintf(buf, "%d\n", led->led_current);
83 }
84
85 static ssize_t lp55xx_store_current(struct device *dev,
86                              struct device_attribute *attr,
87                              const char *buf, size_t len)
88 {
89         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
90         struct lp55xx_chip *chip = led->chip;
91         unsigned long curr;
92
93         if (kstrtoul(buf, 0, &curr))
94                 return -EINVAL;
95
96         if (curr > led->max_current)
97                 return -EINVAL;
98
99         if (!chip->cfg->set_led_current)
100                 return len;
101
102         mutex_lock(&chip->lock);
103         chip->cfg->set_led_current(led, (u8)curr);
104         mutex_unlock(&chip->lock);
105
106         return len;
107 }
108
109 static ssize_t lp55xx_show_max_current(struct device *dev,
110                             struct device_attribute *attr,
111                             char *buf)
112 {
113         struct lp55xx_led *led = dev_to_lp55xx_led(dev);
114
115         return sprintf(buf, "%d\n", led->max_current);
116 }
117
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);
121
122 static struct attribute *lp55xx_led_attributes[] = {
123         &dev_attr_led_current.attr,
124         &dev_attr_max_current.attr,
125         NULL,
126 };
127
128 static struct attribute_group lp55xx_led_attr_group = {
129         .attrs = lp55xx_led_attributes
130 };
131
132 static void lp55xx_set_brightness(struct led_classdev *cdev,
133                              enum led_brightness brightness)
134 {
135         struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
136
137         led->brightness = (u8)brightness;
138         schedule_work(&led->brightness_work);
139 }
140
141 static int lp55xx_init_led(struct lp55xx_led *led,
142                         struct lp55xx_chip *chip, int chan)
143 {
144         struct lp55xx_platform_data *pdata = chip->pdata;
145         struct lp55xx_device_config *cfg = chip->cfg;
146         struct device *dev = &chip->cl->dev;
147         char name[32];
148         int ret;
149         int max_channel = cfg->max_channel;
150
151         if (chan >= max_channel) {
152                 dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
153                 return -EINVAL;
154         }
155
156         if (pdata->led_config[chan].led_current == 0)
157                 return 0;
158
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;
162
163         if (led->chan_nr >= max_channel) {
164                 dev_err(dev, "Use channel numbers between 0 and %d\n",
165                         max_channel - 1);
166                 return -EINVAL;
167         }
168
169         led->cdev.brightness_set = lp55xx_set_brightness;
170
171         if (pdata->led_config[chan].name) {
172                 led->cdev.name = pdata->led_config[chan].name;
173         } else {
174                 snprintf(name, sizeof(name), "%s:channel%d",
175                         pdata->label ? : chip->cl->name, chan);
176                 led->cdev.name = name;
177         }
178
179         /*
180          * register led class device for each channel and
181          * add device attributes
182          */
183
184         ret = led_classdev_register(dev, &led->cdev);
185         if (ret) {
186                 dev_err(dev, "led register err: %d\n", ret);
187                 return ret;
188         }
189
190         ret = sysfs_create_group(&led->cdev.dev->kobj, &lp55xx_led_attr_group);
191         if (ret) {
192                 dev_err(dev, "led sysfs err: %d\n", ret);
193                 led_classdev_unregister(&led->cdev);
194                 return ret;
195         }
196
197         return 0;
198 }
199
200 static struct attribute *lp55xx_engine_attributes[] = {
201         NULL,
202 };
203
204 static const struct attribute_group lp55xx_engine_attr_group = {
205         .attrs = lp55xx_engine_attributes,
206 };
207
208 int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
209 {
210         return i2c_smbus_write_byte_data(chip->cl, reg, val);
211 }
212 EXPORT_SYMBOL_GPL(lp55xx_write);
213
214 int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
215 {
216         s32 ret;
217
218         ret = i2c_smbus_read_byte_data(chip->cl, reg);
219         if (ret < 0)
220                 return ret;
221
222         *val = ret;
223         return 0;
224 }
225 EXPORT_SYMBOL_GPL(lp55xx_read);
226
227 int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
228 {
229         int ret;
230         u8 tmp;
231
232         ret = lp55xx_read(chip, reg, &tmp);
233         if (ret)
234                 return ret;
235
236         tmp &= ~mask;
237         tmp |= val & mask;
238
239         return lp55xx_write(chip, reg, tmp);
240 }
241 EXPORT_SYMBOL_GPL(lp55xx_update_bits);
242
243 int lp55xx_init_device(struct lp55xx_chip *chip)
244 {
245         struct lp55xx_platform_data *pdata;
246         struct lp55xx_device_config *cfg;
247         struct device *dev = &chip->cl->dev;
248         int ret = 0;
249
250         WARN_ON(!chip);
251
252         pdata = chip->pdata;
253         cfg = chip->cfg;
254
255         if (!pdata || !cfg)
256                 return -EINVAL;
257
258         if (pdata->setup_resources) {
259                 ret = pdata->setup_resources();
260                 if (ret < 0) {
261                         dev_err(dev, "setup resoure err: %d\n", ret);
262                         goto err;
263                 }
264         }
265
266         if (pdata->enable) {
267                 pdata->enable(0);
268                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
269                 pdata->enable(1);
270                 usleep_range(1000, 2000); /* 500us abs min. */
271         }
272
273         lp55xx_reset_device(chip);
274
275         /*
276          * Exact value is not available. 10 - 20ms
277          * appears to be enough for reset.
278          */
279         usleep_range(10000, 20000);
280
281         ret = lp55xx_detect_device(chip);
282         if (ret) {
283                 dev_err(dev, "device detection err: %d\n", ret);
284                 goto err;
285         }
286
287         /* chip specific initialization */
288         ret = lp55xx_post_init_device(chip);
289         if (ret) {
290                 dev_err(dev, "post init device err: %d\n", ret);
291                 goto err_post_init;
292         }
293
294         return 0;
295
296 err_post_init:
297         lp55xx_deinit_device(chip);
298 err:
299         return ret;
300 }
301 EXPORT_SYMBOL_GPL(lp55xx_init_device);
302
303 void lp55xx_deinit_device(struct lp55xx_chip *chip)
304 {
305         struct lp55xx_platform_data *pdata = chip->pdata;
306
307         if (pdata->enable)
308                 pdata->enable(0);
309
310         if (pdata->release_resources)
311                 pdata->release_resources();
312 }
313 EXPORT_SYMBOL_GPL(lp55xx_deinit_device);
314
315 int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
316 {
317         struct lp55xx_platform_data *pdata = chip->pdata;
318         struct lp55xx_device_config *cfg = chip->cfg;
319         int num_channels = pdata->num_channels;
320         struct lp55xx_led *each;
321         u8 led_current;
322         int ret;
323         int i;
324
325         if (!cfg->brightness_work_fn) {
326                 dev_err(&chip->cl->dev, "empty brightness configuration\n");
327                 return -EINVAL;
328         }
329
330         for (i = 0; i < num_channels; i++) {
331
332                 /* do not initialize channels that are not connected */
333                 if (pdata->led_config[i].led_current == 0)
334                         continue;
335
336                 led_current = pdata->led_config[i].led_current;
337                 each = led + i;
338                 ret = lp55xx_init_led(each, chip, i);
339                 if (ret)
340                         goto err_init_led;
341
342                 INIT_WORK(&each->brightness_work, cfg->brightness_work_fn);
343
344                 chip->num_leds++;
345                 each->chip = chip;
346
347                 /* setting led current at each channel */
348                 if (cfg->set_led_current)
349                         cfg->set_led_current(each, led_current);
350         }
351
352         return 0;
353
354 err_init_led:
355         lp55xx_unregister_leds(led, chip);
356         return ret;
357 }
358 EXPORT_SYMBOL_GPL(lp55xx_register_leds);
359
360 void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
361 {
362         int i;
363         struct lp55xx_led *each;
364         struct kobject *kobj;
365
366         for (i = 0; i < chip->num_leds; i++) {
367                 each = led + i;
368                 kobj = &led->cdev.dev->kobj;
369                 sysfs_remove_group(kobj, &lp55xx_led_attr_group);
370                 led_classdev_unregister(&each->cdev);
371                 flush_work(&each->brightness_work);
372         }
373 }
374 EXPORT_SYMBOL_GPL(lp55xx_unregister_leds);
375
376 int lp55xx_register_sysfs(struct lp55xx_chip *chip)
377 {
378         struct device *dev = &chip->cl->dev;
379
380         return sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
381 }
382 EXPORT_SYMBOL_GPL(lp55xx_register_sysfs);
383
384 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
385 MODULE_DESCRIPTION("LP55xx Common Driver");
386 MODULE_LICENSE("GPL");