ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / leds / leds-netxbig.c
blob26515c27ea8cca18a2e5f5ac24f82a6158194182
1 /*
2 * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
4 * Copyright (C) 2010 LaCie
6 * Author: Simon Guinot <sguinot@lacie.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 as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/leds.h>
30 #include <linux/platform_data/leds-kirkwood-netxbig.h>
33 * GPIO extension bus.
36 static DEFINE_SPINLOCK(gpio_ext_lock);
38 static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
40 int pin;
42 for (pin = 0; pin < gpio_ext->num_addr; pin++)
43 gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
46 static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
48 int pin;
50 for (pin = 0; pin < gpio_ext->num_data; pin++)
51 gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
54 static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
56 /* Enable select is done on the raising edge. */
57 gpio_set_value(gpio_ext->enable, 0);
58 gpio_set_value(gpio_ext->enable, 1);
61 static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
62 int addr, int value)
64 unsigned long flags;
66 spin_lock_irqsave(&gpio_ext_lock, flags);
67 gpio_ext_set_addr(gpio_ext, addr);
68 gpio_ext_set_data(gpio_ext, value);
69 gpio_ext_enable_select(gpio_ext);
70 spin_unlock_irqrestore(&gpio_ext_lock, flags);
73 static int gpio_ext_init(struct netxbig_gpio_ext *gpio_ext)
75 int err;
76 int i;
78 if (unlikely(!gpio_ext))
79 return -EINVAL;
81 /* Configure address GPIOs. */
82 for (i = 0; i < gpio_ext->num_addr; i++) {
83 err = gpio_request_one(gpio_ext->addr[i], GPIOF_OUT_INIT_LOW,
84 "GPIO extension addr");
85 if (err)
86 goto err_free_addr;
88 /* Configure data GPIOs. */
89 for (i = 0; i < gpio_ext->num_data; i++) {
90 err = gpio_request_one(gpio_ext->data[i], GPIOF_OUT_INIT_LOW,
91 "GPIO extension data");
92 if (err)
93 goto err_free_data;
95 /* Configure "enable select" GPIO. */
96 err = gpio_request_one(gpio_ext->enable, GPIOF_OUT_INIT_LOW,
97 "GPIO extension enable");
98 if (err)
99 goto err_free_data;
101 return 0;
103 err_free_data:
104 for (i = i - 1; i >= 0; i--)
105 gpio_free(gpio_ext->data[i]);
106 i = gpio_ext->num_addr;
107 err_free_addr:
108 for (i = i - 1; i >= 0; i--)
109 gpio_free(gpio_ext->addr[i]);
111 return err;
114 static void gpio_ext_free(struct netxbig_gpio_ext *gpio_ext)
116 int i;
118 gpio_free(gpio_ext->enable);
119 for (i = gpio_ext->num_addr - 1; i >= 0; i--)
120 gpio_free(gpio_ext->addr[i]);
121 for (i = gpio_ext->num_data - 1; i >= 0; i--)
122 gpio_free(gpio_ext->data[i]);
126 * Class LED driver.
129 struct netxbig_led_data {
130 struct netxbig_gpio_ext *gpio_ext;
131 struct led_classdev cdev;
132 int mode_addr;
133 int *mode_val;
134 int bright_addr;
135 int bright_max;
136 struct netxbig_led_timer *timer;
137 int num_timer;
138 enum netxbig_led_mode mode;
139 int sata;
140 spinlock_t lock;
143 static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
144 unsigned long delay_on,
145 unsigned long delay_off,
146 struct netxbig_led_timer *timer,
147 int num_timer)
149 int i;
151 for (i = 0; i < num_timer; i++) {
152 if (timer[i].delay_on == delay_on &&
153 timer[i].delay_off == delay_off) {
154 *mode = timer[i].mode;
155 return 0;
158 return -EINVAL;
161 static int netxbig_led_blink_set(struct led_classdev *led_cdev,
162 unsigned long *delay_on,
163 unsigned long *delay_off)
165 struct netxbig_led_data *led_dat =
166 container_of(led_cdev, struct netxbig_led_data, cdev);
167 enum netxbig_led_mode mode;
168 int mode_val;
169 int ret;
171 /* Look for a LED mode with the requested timer frequency. */
172 ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
173 led_dat->timer, led_dat->num_timer);
174 if (ret < 0)
175 return ret;
177 mode_val = led_dat->mode_val[mode];
178 if (mode_val == NETXBIG_LED_INVALID_MODE)
179 return -EINVAL;
181 spin_lock_irq(&led_dat->lock);
183 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
184 led_dat->mode = mode;
186 spin_unlock_irq(&led_dat->lock);
188 return 0;
191 static void netxbig_led_set(struct led_classdev *led_cdev,
192 enum led_brightness value)
194 struct netxbig_led_data *led_dat =
195 container_of(led_cdev, struct netxbig_led_data, cdev);
196 enum netxbig_led_mode mode;
197 int mode_val, bright_val;
198 int set_brightness = 1;
199 unsigned long flags;
201 spin_lock_irqsave(&led_dat->lock, flags);
203 if (value == LED_OFF) {
204 mode = NETXBIG_LED_OFF;
205 set_brightness = 0;
206 } else {
207 if (led_dat->sata)
208 mode = NETXBIG_LED_SATA;
209 else if (led_dat->mode == NETXBIG_LED_OFF)
210 mode = NETXBIG_LED_ON;
211 else /* Keep 'timer' mode. */
212 mode = led_dat->mode;
214 mode_val = led_dat->mode_val[mode];
216 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
217 led_dat->mode = mode;
219 * Note that the brightness register is shared between all the
220 * SATA LEDs. So, change the brightness setting for a single
221 * SATA LED will affect all the others.
223 if (set_brightness) {
224 bright_val = DIV_ROUND_UP(value * led_dat->bright_max,
225 LED_FULL);
226 gpio_ext_set_value(led_dat->gpio_ext,
227 led_dat->bright_addr, bright_val);
230 spin_unlock_irqrestore(&led_dat->lock, flags);
233 static ssize_t netxbig_led_sata_store(struct device *dev,
234 struct device_attribute *attr,
235 const char *buff, size_t count)
237 struct led_classdev *led_cdev = dev_get_drvdata(dev);
238 struct netxbig_led_data *led_dat =
239 container_of(led_cdev, struct netxbig_led_data, cdev);
240 unsigned long enable;
241 enum netxbig_led_mode mode;
242 int mode_val;
243 int ret;
245 ret = kstrtoul(buff, 10, &enable);
246 if (ret < 0)
247 return ret;
249 enable = !!enable;
251 spin_lock_irq(&led_dat->lock);
253 if (led_dat->sata == enable) {
254 ret = count;
255 goto exit_unlock;
258 if (led_dat->mode != NETXBIG_LED_ON &&
259 led_dat->mode != NETXBIG_LED_SATA)
260 mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
261 else if (enable)
262 mode = NETXBIG_LED_SATA;
263 else
264 mode = NETXBIG_LED_ON;
266 mode_val = led_dat->mode_val[mode];
267 if (mode_val == NETXBIG_LED_INVALID_MODE) {
268 ret = -EINVAL;
269 goto exit_unlock;
272 gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
273 led_dat->mode = mode;
274 led_dat->sata = enable;
276 ret = count;
278 exit_unlock:
279 spin_unlock_irq(&led_dat->lock);
281 return ret;
284 static ssize_t netxbig_led_sata_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct led_classdev *led_cdev = dev_get_drvdata(dev);
288 struct netxbig_led_data *led_dat =
289 container_of(led_cdev, struct netxbig_led_data, cdev);
291 return sprintf(buf, "%d\n", led_dat->sata);
294 static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
296 static struct attribute *netxbig_led_attrs[] = {
297 &dev_attr_sata.attr,
298 NULL
300 ATTRIBUTE_GROUPS(netxbig_led);
302 static void delete_netxbig_led(struct netxbig_led_data *led_dat)
304 led_classdev_unregister(&led_dat->cdev);
307 static int
308 create_netxbig_led(struct platform_device *pdev,
309 struct netxbig_led_data *led_dat,
310 const struct netxbig_led *template)
312 struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
314 spin_lock_init(&led_dat->lock);
315 led_dat->gpio_ext = pdata->gpio_ext;
316 led_dat->cdev.name = template->name;
317 led_dat->cdev.default_trigger = template->default_trigger;
318 led_dat->cdev.blink_set = netxbig_led_blink_set;
319 led_dat->cdev.brightness_set = netxbig_led_set;
321 * Because the GPIO extension bus don't allow to read registers
322 * value, there is no way to probe the LED initial state.
323 * So, the initial sysfs LED value for the "brightness" and "sata"
324 * attributes are inconsistent.
326 * Note that the initial LED state can't be reconfigured.
327 * The reason is that the LED behaviour must stay uniform during
328 * the whole boot process (bootloader+linux).
330 led_dat->sata = 0;
331 led_dat->cdev.brightness = LED_OFF;
332 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
334 * If available, expose the SATA activity blink capability through
335 * a "sata" sysfs attribute.
337 if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
338 led_dat->cdev.groups = netxbig_led_groups;
339 led_dat->mode_addr = template->mode_addr;
340 led_dat->mode_val = template->mode_val;
341 led_dat->bright_addr = template->bright_addr;
342 led_dat->bright_max = (1 << pdata->gpio_ext->num_data) - 1;
343 led_dat->timer = pdata->timer;
344 led_dat->num_timer = pdata->num_timer;
346 return led_classdev_register(&pdev->dev, &led_dat->cdev);
349 static int netxbig_led_probe(struct platform_device *pdev)
351 struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
352 struct netxbig_led_data *leds_data;
353 int i;
354 int ret;
356 if (!pdata)
357 return -EINVAL;
359 leds_data = devm_kzalloc(&pdev->dev,
360 sizeof(struct netxbig_led_data) * pdata->num_leds, GFP_KERNEL);
361 if (!leds_data)
362 return -ENOMEM;
364 ret = gpio_ext_init(pdata->gpio_ext);
365 if (ret < 0)
366 return ret;
368 for (i = 0; i < pdata->num_leds; i++) {
369 ret = create_netxbig_led(pdev, &leds_data[i], &pdata->leds[i]);
370 if (ret < 0)
371 goto err_free_leds;
374 platform_set_drvdata(pdev, leds_data);
376 return 0;
378 err_free_leds:
379 for (i = i - 1; i >= 0; i--)
380 delete_netxbig_led(&leds_data[i]);
382 gpio_ext_free(pdata->gpio_ext);
383 return ret;
386 static int netxbig_led_remove(struct platform_device *pdev)
388 struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
389 struct netxbig_led_data *leds_data;
390 int i;
392 leds_data = platform_get_drvdata(pdev);
394 for (i = 0; i < pdata->num_leds; i++)
395 delete_netxbig_led(&leds_data[i]);
397 gpio_ext_free(pdata->gpio_ext);
399 return 0;
402 static struct platform_driver netxbig_led_driver = {
403 .probe = netxbig_led_probe,
404 .remove = netxbig_led_remove,
405 .driver = {
406 .name = "leds-netxbig",
410 module_platform_driver(netxbig_led_driver);
412 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
413 MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
414 MODULE_LICENSE("GPL");
415 MODULE_ALIAS("platform:leds-netxbig");