blob: 70137b1eecf5bdebe130a9f3a079567bb53df9c3 [file] [log] [blame]
Simon Guinot11efe712010-07-06 16:08:46 +02001/*
2 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
3 *
4 * Copyright (C) 2010 LaCie
5 *
6 * Author: Simon Guinot <sguinot@lacie.com>
7 *
8 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29#include <linux/gpio.h>
30#include <linux/leds.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040031#include <linux/module.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020032#include <linux/platform_data/leds-kirkwood-ns2.h>
Simon Guinot72052fc2012-10-17 12:09:03 +020033#include <linux/of_gpio.h>
Simon Guinot11efe712010-07-06 16:08:46 +020034
35/*
36 * The Network Space v2 dual-GPIO LED is wired to a CPLD and can blink in
37 * relation with the SATA activity. This capability is exposed through the
38 * "sata" sysfs attribute.
39 *
40 * The following array detail the different LED registers and the combination
41 * of their possible values:
42 *
43 * cmd_led | slow_led | /SATA active | LED state
44 * | | |
45 * 1 | 0 | x | off
46 * - | 1 | x | on
47 * 0 | 0 | 1 | on
48 * 0 | 0 | 0 | blink (rate 300ms)
49 */
50
51enum ns2_led_modes {
52 NS_V2_LED_OFF,
53 NS_V2_LED_ON,
54 NS_V2_LED_SATA,
55};
56
57struct ns2_led_mode_value {
58 enum ns2_led_modes mode;
59 int cmd_level;
60 int slow_level;
61};
62
63static struct ns2_led_mode_value ns2_led_modval[] = {
64 { NS_V2_LED_OFF , 1, 0 },
65 { NS_V2_LED_ON , 0, 1 },
66 { NS_V2_LED_ON , 1, 1 },
67 { NS_V2_LED_SATA, 0, 0 },
68};
69
70struct ns2_led_data {
71 struct led_classdev cdev;
72 unsigned cmd;
73 unsigned slow;
74 unsigned char sata; /* True when SATA mode active. */
75 rwlock_t rw_lock; /* Lock GPIOs. */
76};
77
78static int ns2_led_get_mode(struct ns2_led_data *led_dat,
79 enum ns2_led_modes *mode)
80{
81 int i;
82 int ret = -EINVAL;
83 int cmd_level;
84 int slow_level;
85
Simon Guinotf539dfe2010-09-19 15:30:59 +020086 read_lock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +020087
88 cmd_level = gpio_get_value(led_dat->cmd);
89 slow_level = gpio_get_value(led_dat->slow);
90
91 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
92 if (cmd_level == ns2_led_modval[i].cmd_level &&
93 slow_level == ns2_led_modval[i].slow_level) {
94 *mode = ns2_led_modval[i].mode;
95 ret = 0;
96 break;
97 }
98 }
99
Simon Guinotf539dfe2010-09-19 15:30:59 +0200100 read_unlock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +0200101
102 return ret;
103}
104
105static void ns2_led_set_mode(struct ns2_led_data *led_dat,
106 enum ns2_led_modes mode)
107{
108 int i;
Simon Guinotf539dfe2010-09-19 15:30:59 +0200109 unsigned long flags;
Simon Guinot11efe712010-07-06 16:08:46 +0200110
Simon Guinotf539dfe2010-09-19 15:30:59 +0200111 write_lock_irqsave(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200112
113 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
114 if (mode == ns2_led_modval[i].mode) {
115 gpio_set_value(led_dat->cmd,
116 ns2_led_modval[i].cmd_level);
117 gpio_set_value(led_dat->slow,
118 ns2_led_modval[i].slow_level);
119 }
120 }
121
Simon Guinotf539dfe2010-09-19 15:30:59 +0200122 write_unlock_irqrestore(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200123}
124
125static void ns2_led_set(struct led_classdev *led_cdev,
126 enum led_brightness value)
127{
128 struct ns2_led_data *led_dat =
129 container_of(led_cdev, struct ns2_led_data, cdev);
130 enum ns2_led_modes mode;
131
132 if (value == LED_OFF)
133 mode = NS_V2_LED_OFF;
134 else if (led_dat->sata)
135 mode = NS_V2_LED_SATA;
136 else
137 mode = NS_V2_LED_ON;
138
139 ns2_led_set_mode(led_dat, mode);
140}
141
142static ssize_t ns2_led_sata_store(struct device *dev,
143 struct device_attribute *attr,
144 const char *buff, size_t count)
145{
Simon Guinote5971bb2010-10-07 16:35:40 +0200146 struct led_classdev *led_cdev = dev_get_drvdata(dev);
147 struct ns2_led_data *led_dat =
148 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200149 int ret;
150 unsigned long enable;
151 enum ns2_led_modes mode;
Simon Guinot11efe712010-07-06 16:08:46 +0200152
Jingoo Han38743502012-10-23 05:25:35 -0700153 ret = kstrtoul(buff, 10, &enable);
Simon Guinot11efe712010-07-06 16:08:46 +0200154 if (ret < 0)
155 return ret;
156
157 enable = !!enable;
158
159 if (led_dat->sata == enable)
160 return count;
161
162 ret = ns2_led_get_mode(led_dat, &mode);
163 if (ret < 0)
164 return ret;
165
166 if (enable && mode == NS_V2_LED_ON)
167 ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
168 if (!enable && mode == NS_V2_LED_SATA)
169 ns2_led_set_mode(led_dat, NS_V2_LED_ON);
170
171 led_dat->sata = enable;
172
173 return count;
174}
175
176static ssize_t ns2_led_sata_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
Simon Guinote5971bb2010-10-07 16:35:40 +0200179 struct led_classdev *led_cdev = dev_get_drvdata(dev);
180 struct ns2_led_data *led_dat =
181 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200182
183 return sprintf(buf, "%d\n", led_dat->sata);
184}
185
186static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
187
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500188static int
Simon Guinot11efe712010-07-06 16:08:46 +0200189create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
190 const struct ns2_led *template)
191{
192 int ret;
193 enum ns2_led_modes mode;
194
Sachin Kamat04195822012-11-25 10:28:10 +0530195 ret = devm_gpio_request_one(&pdev->dev, template->cmd,
Jingoo Han9d04cba2013-03-07 18:38:26 -0800196 gpio_get_value(template->cmd) ?
197 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700198 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200199 if (ret) {
200 dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
201 template->name);
Jingoo Han31c3dc72012-10-23 05:18:21 -0700202 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200203 }
204
Sachin Kamat04195822012-11-25 10:28:10 +0530205 ret = devm_gpio_request_one(&pdev->dev, template->slow,
Jingoo Han9d04cba2013-03-07 18:38:26 -0800206 gpio_get_value(template->slow) ?
207 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700208 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200209 if (ret) {
210 dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
211 template->name);
Sachin Kamat04195822012-11-25 10:28:10 +0530212 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200213 }
214
215 rwlock_init(&led_dat->rw_lock);
216
217 led_dat->cdev.name = template->name;
218 led_dat->cdev.default_trigger = template->default_trigger;
219 led_dat->cdev.blink_set = NULL;
220 led_dat->cdev.brightness_set = ns2_led_set;
221 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
222 led_dat->cmd = template->cmd;
223 led_dat->slow = template->slow;
224
225 ret = ns2_led_get_mode(led_dat, &mode);
226 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530227 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200228
229 /* Set LED initial state. */
230 led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
231 led_dat->cdev.brightness =
232 (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
233
234 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
235 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530236 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200237
Simon Guinot11efe712010-07-06 16:08:46 +0200238 ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
239 if (ret < 0)
240 goto err_free_cdev;
241
242 return 0;
243
244err_free_cdev:
245 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200246 return ret;
247}
248
Arnd Bergmannb8cd7422012-05-10 13:01:46 -0700249static void delete_ns2_led(struct ns2_led_data *led_dat)
Simon Guinot11efe712010-07-06 16:08:46 +0200250{
251 device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
252 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200253}
254
Simon Guinot72052fc2012-10-17 12:09:03 +0200255#ifdef CONFIG_OF_GPIO
256/*
257 * Translate OpenFirmware node properties into platform_data.
258 */
Linus Torvaldscf4af012012-12-12 12:14:06 -0800259static int
Simon Guinot72052fc2012-10-17 12:09:03 +0200260ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
261{
262 struct device_node *np = dev->of_node;
263 struct device_node *child;
264 struct ns2_led *leds;
265 int num_leds = 0;
266 int i = 0;
267
268 num_leds = of_get_child_count(np);
269 if (!num_leds)
270 return -ENODEV;
271
272 leds = devm_kzalloc(dev, num_leds * sizeof(struct ns2_led),
273 GFP_KERNEL);
274 if (!leds)
275 return -ENOMEM;
276
277 for_each_child_of_node(np, child) {
278 const char *string;
279 int ret;
280
281 ret = of_get_named_gpio(child, "cmd-gpio", 0);
282 if (ret < 0)
283 return ret;
284 leds[i].cmd = ret;
285 ret = of_get_named_gpio(child, "slow-gpio", 0);
286 if (ret < 0)
287 return ret;
288 leds[i].slow = ret;
289 ret = of_property_read_string(child, "label", &string);
290 leds[i].name = (ret == 0) ? string : child->name;
291 ret = of_property_read_string(child, "linux,default-trigger",
292 &string);
293 if (ret == 0)
294 leds[i].default_trigger = string;
295
296 i++;
297 }
298
299 pdata->leds = leds;
300 pdata->num_leds = num_leds;
301
302 return 0;
303}
304
305static const struct of_device_id of_ns2_leds_match[] = {
306 { .compatible = "lacie,ns2-leds", },
307 {},
308};
309#endif /* CONFIG_OF_GPIO */
310
Simon Guinot3de19292013-03-19 11:07:29 -0700311struct ns2_led_priv {
312 int num_leds;
313 struct ns2_led_data leds_data[];
314};
315
316static inline int sizeof_ns2_led_priv(int num_leds)
317{
318 return sizeof(struct ns2_led_priv) +
319 (sizeof(struct ns2_led_data) * num_leds);
320}
321
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500322static int ns2_led_probe(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200323{
324 struct ns2_led_platform_data *pdata = pdev->dev.platform_data;
Simon Guinot3de19292013-03-19 11:07:29 -0700325 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200326 int i;
327 int ret;
328
Simon Guinot72052fc2012-10-17 12:09:03 +0200329#ifdef CONFIG_OF_GPIO
330 if (!pdata) {
331 pdata = devm_kzalloc(&pdev->dev,
332 sizeof(struct ns2_led_platform_data),
333 GFP_KERNEL);
334 if (!pdata)
335 return -ENOMEM;
336
337 ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
338 if (ret)
339 return ret;
340 }
341#else
Simon Guinot11efe712010-07-06 16:08:46 +0200342 if (!pdata)
343 return -EINVAL;
Simon Guinot72052fc2012-10-17 12:09:03 +0200344#endif /* CONFIG_OF_GPIO */
Simon Guinot11efe712010-07-06 16:08:46 +0200345
Simon Guinot3de19292013-03-19 11:07:29 -0700346 priv = devm_kzalloc(&pdev->dev,
347 sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
348 if (!priv)
Simon Guinot11efe712010-07-06 16:08:46 +0200349 return -ENOMEM;
Simon Guinot3de19292013-03-19 11:07:29 -0700350 priv->num_leds = pdata->num_leds;
Simon Guinot11efe712010-07-06 16:08:46 +0200351
Simon Guinot3de19292013-03-19 11:07:29 -0700352 for (i = 0; i < priv->num_leds; i++) {
353 ret = create_ns2_led(pdev, &priv->leds_data[i],
354 &pdata->leds[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800355 if (ret < 0) {
356 for (i = i - 1; i >= 0; i--)
Simon Guinot3de19292013-03-19 11:07:29 -0700357 delete_ns2_led(&priv->leds_data[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800358 return ret;
359 }
Simon Guinot11efe712010-07-06 16:08:46 +0200360 }
361
Simon Guinot3de19292013-03-19 11:07:29 -0700362 platform_set_drvdata(pdev, priv);
Simon Guinot11efe712010-07-06 16:08:46 +0200363
364 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200365}
366
Bill Pemberton678e8a62012-11-19 13:26:00 -0500367static int ns2_led_remove(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200368{
369 int i;
Simon Guinot3de19292013-03-19 11:07:29 -0700370 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200371
Simon Guinot3de19292013-03-19 11:07:29 -0700372 priv = platform_get_drvdata(pdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200373
Simon Guinot3de19292013-03-19 11:07:29 -0700374 for (i = 0; i < priv->num_leds; i++)
375 delete_ns2_led(&priv->leds_data[i]);
Simon Guinot11efe712010-07-06 16:08:46 +0200376
Simon Guinot11efe712010-07-06 16:08:46 +0200377 platform_set_drvdata(pdev, NULL);
378
379 return 0;
380}
381
382static struct platform_driver ns2_led_driver = {
383 .probe = ns2_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500384 .remove = ns2_led_remove,
Simon Guinot11efe712010-07-06 16:08:46 +0200385 .driver = {
Simon Guinot72052fc2012-10-17 12:09:03 +0200386 .name = "leds-ns2",
387 .owner = THIS_MODULE,
388 .of_match_table = of_match_ptr(of_ns2_leds_match),
Simon Guinot11efe712010-07-06 16:08:46 +0200389 },
390};
Simon Guinot11efe712010-07-06 16:08:46 +0200391
Axel Lin892a8842012-01-10 15:09:24 -0800392module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200393
394MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
395MODULE_DESCRIPTION("Network Space v2 LED driver");
396MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800397MODULE_ALIAS("platform:leds-ns2");