blob: c7a4230233ea991e660dabf6f55ceedff9c5c381 [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>
Sachin Kamatc68f46d2013-09-28 04:38:30 -070033#include <linux/of.h>
Simon Guinot72052fc2012-10-17 12:09:03 +020034#include <linux/of_gpio.h>
Simon Guinot11efe712010-07-06 16:08:46 +020035
36/*
37 * The Network Space v2 dual-GPIO LED is wired to a CPLD and can blink in
38 * relation with the SATA activity. This capability is exposed through the
39 * "sata" sysfs attribute.
40 *
41 * The following array detail the different LED registers and the combination
42 * of their possible values:
43 *
44 * cmd_led | slow_led | /SATA active | LED state
45 * | | |
46 * 1 | 0 | x | off
47 * - | 1 | x | on
48 * 0 | 0 | 1 | on
49 * 0 | 0 | 0 | blink (rate 300ms)
50 */
51
52enum ns2_led_modes {
53 NS_V2_LED_OFF,
54 NS_V2_LED_ON,
55 NS_V2_LED_SATA,
56};
57
58struct ns2_led_mode_value {
59 enum ns2_led_modes mode;
60 int cmd_level;
61 int slow_level;
62};
63
64static struct ns2_led_mode_value ns2_led_modval[] = {
65 { NS_V2_LED_OFF , 1, 0 },
66 { NS_V2_LED_ON , 0, 1 },
67 { NS_V2_LED_ON , 1, 1 },
68 { NS_V2_LED_SATA, 0, 0 },
69};
70
71struct ns2_led_data {
72 struct led_classdev cdev;
73 unsigned cmd;
74 unsigned slow;
75 unsigned char sata; /* True when SATA mode active. */
76 rwlock_t rw_lock; /* Lock GPIOs. */
77};
78
79static int ns2_led_get_mode(struct ns2_led_data *led_dat,
80 enum ns2_led_modes *mode)
81{
82 int i;
83 int ret = -EINVAL;
84 int cmd_level;
85 int slow_level;
86
Simon Guinotf539dfe2010-09-19 15:30:59 +020087 read_lock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +020088
89 cmd_level = gpio_get_value(led_dat->cmd);
90 slow_level = gpio_get_value(led_dat->slow);
91
92 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
93 if (cmd_level == ns2_led_modval[i].cmd_level &&
94 slow_level == ns2_led_modval[i].slow_level) {
95 *mode = ns2_led_modval[i].mode;
96 ret = 0;
97 break;
98 }
99 }
100
Simon Guinotf539dfe2010-09-19 15:30:59 +0200101 read_unlock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +0200102
103 return ret;
104}
105
106static void ns2_led_set_mode(struct ns2_led_data *led_dat,
107 enum ns2_led_modes mode)
108{
109 int i;
Simon Guinotf539dfe2010-09-19 15:30:59 +0200110 unsigned long flags;
Simon Guinot11efe712010-07-06 16:08:46 +0200111
Simon Guinotf539dfe2010-09-19 15:30:59 +0200112 write_lock_irqsave(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200113
114 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
115 if (mode == ns2_led_modval[i].mode) {
116 gpio_set_value(led_dat->cmd,
117 ns2_led_modval[i].cmd_level);
118 gpio_set_value(led_dat->slow,
119 ns2_led_modval[i].slow_level);
120 }
121 }
122
Simon Guinotf539dfe2010-09-19 15:30:59 +0200123 write_unlock_irqrestore(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200124}
125
126static void ns2_led_set(struct led_classdev *led_cdev,
127 enum led_brightness value)
128{
129 struct ns2_led_data *led_dat =
130 container_of(led_cdev, struct ns2_led_data, cdev);
131 enum ns2_led_modes mode;
132
133 if (value == LED_OFF)
134 mode = NS_V2_LED_OFF;
135 else if (led_dat->sata)
136 mode = NS_V2_LED_SATA;
137 else
138 mode = NS_V2_LED_ON;
139
140 ns2_led_set_mode(led_dat, mode);
141}
142
143static ssize_t ns2_led_sata_store(struct device *dev,
144 struct device_attribute *attr,
145 const char *buff, size_t count)
146{
Simon Guinote5971bb2010-10-07 16:35:40 +0200147 struct led_classdev *led_cdev = dev_get_drvdata(dev);
148 struct ns2_led_data *led_dat =
149 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200150 int ret;
151 unsigned long enable;
152 enum ns2_led_modes mode;
Simon Guinot11efe712010-07-06 16:08:46 +0200153
Jingoo Han38743502012-10-23 05:25:35 -0700154 ret = kstrtoul(buff, 10, &enable);
Simon Guinot11efe712010-07-06 16:08:46 +0200155 if (ret < 0)
156 return ret;
157
158 enable = !!enable;
159
160 if (led_dat->sata == enable)
161 return count;
162
163 ret = ns2_led_get_mode(led_dat, &mode);
164 if (ret < 0)
165 return ret;
166
167 if (enable && mode == NS_V2_LED_ON)
168 ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
169 if (!enable && mode == NS_V2_LED_SATA)
170 ns2_led_set_mode(led_dat, NS_V2_LED_ON);
171
172 led_dat->sata = enable;
173
174 return count;
175}
176
177static ssize_t ns2_led_sata_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
Simon Guinote5971bb2010-10-07 16:35:40 +0200180 struct led_classdev *led_cdev = dev_get_drvdata(dev);
181 struct ns2_led_data *led_dat =
182 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200183
184 return sprintf(buf, "%d\n", led_dat->sata);
185}
186
187static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
188
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500189static int
Simon Guinot11efe712010-07-06 16:08:46 +0200190create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
191 const struct ns2_led *template)
192{
193 int ret;
194 enum ns2_led_modes mode;
195
Sachin Kamat04195822012-11-25 10:28:10 +0530196 ret = devm_gpio_request_one(&pdev->dev, template->cmd,
Jingoo Han9d04cba2013-03-07 18:38:26 -0800197 gpio_get_value(template->cmd) ?
198 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700199 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200200 if (ret) {
201 dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
202 template->name);
Jingoo Han31c3dc72012-10-23 05:18:21 -0700203 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200204 }
205
Sachin Kamat04195822012-11-25 10:28:10 +0530206 ret = devm_gpio_request_one(&pdev->dev, template->slow,
Jingoo Han9d04cba2013-03-07 18:38:26 -0800207 gpio_get_value(template->slow) ?
208 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700209 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200210 if (ret) {
211 dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
212 template->name);
Sachin Kamat04195822012-11-25 10:28:10 +0530213 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200214 }
215
216 rwlock_init(&led_dat->rw_lock);
217
218 led_dat->cdev.name = template->name;
219 led_dat->cdev.default_trigger = template->default_trigger;
220 led_dat->cdev.blink_set = NULL;
221 led_dat->cdev.brightness_set = ns2_led_set;
222 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
223 led_dat->cmd = template->cmd;
224 led_dat->slow = template->slow;
225
226 ret = ns2_led_get_mode(led_dat, &mode);
227 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530228 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200229
230 /* Set LED initial state. */
231 led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
232 led_dat->cdev.brightness =
233 (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
234
235 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
236 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530237 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200238
Simon Guinot11efe712010-07-06 16:08:46 +0200239 ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
240 if (ret < 0)
241 goto err_free_cdev;
242
243 return 0;
244
245err_free_cdev:
246 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200247 return ret;
248}
249
Arnd Bergmannb8cd7422012-05-10 13:01:46 -0700250static void delete_ns2_led(struct ns2_led_data *led_dat)
Simon Guinot11efe712010-07-06 16:08:46 +0200251{
252 device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
253 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200254}
255
Simon Guinot72052fc2012-10-17 12:09:03 +0200256#ifdef CONFIG_OF_GPIO
257/*
258 * Translate OpenFirmware node properties into platform_data.
259 */
Linus Torvaldscf4af012012-12-12 12:14:06 -0800260static int
Simon Guinot72052fc2012-10-17 12:09:03 +0200261ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
262{
263 struct device_node *np = dev->of_node;
264 struct device_node *child;
265 struct ns2_led *leds;
266 int num_leds = 0;
267 int i = 0;
268
269 num_leds = of_get_child_count(np);
270 if (!num_leds)
271 return -ENODEV;
272
273 leds = devm_kzalloc(dev, num_leds * sizeof(struct ns2_led),
274 GFP_KERNEL);
275 if (!leds)
276 return -ENOMEM;
277
278 for_each_child_of_node(np, child) {
279 const char *string;
280 int ret;
281
282 ret = of_get_named_gpio(child, "cmd-gpio", 0);
283 if (ret < 0)
284 return ret;
285 leds[i].cmd = ret;
286 ret = of_get_named_gpio(child, "slow-gpio", 0);
287 if (ret < 0)
288 return ret;
289 leds[i].slow = ret;
290 ret = of_property_read_string(child, "label", &string);
291 leds[i].name = (ret == 0) ? string : child->name;
292 ret = of_property_read_string(child, "linux,default-trigger",
293 &string);
294 if (ret == 0)
295 leds[i].default_trigger = string;
296
297 i++;
298 }
299
300 pdata->leds = leds;
301 pdata->num_leds = num_leds;
302
303 return 0;
304}
305
306static const struct of_device_id of_ns2_leds_match[] = {
307 { .compatible = "lacie,ns2-leds", },
308 {},
309};
310#endif /* CONFIG_OF_GPIO */
311
Simon Guinot3de19292013-03-19 11:07:29 -0700312struct ns2_led_priv {
313 int num_leds;
314 struct ns2_led_data leds_data[];
315};
316
317static inline int sizeof_ns2_led_priv(int num_leds)
318{
319 return sizeof(struct ns2_led_priv) +
320 (sizeof(struct ns2_led_data) * num_leds);
321}
322
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500323static int ns2_led_probe(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200324{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700325 struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Simon Guinot3de19292013-03-19 11:07:29 -0700326 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200327 int i;
328 int ret;
329
Simon Guinot72052fc2012-10-17 12:09:03 +0200330#ifdef CONFIG_OF_GPIO
331 if (!pdata) {
332 pdata = devm_kzalloc(&pdev->dev,
333 sizeof(struct ns2_led_platform_data),
334 GFP_KERNEL);
335 if (!pdata)
336 return -ENOMEM;
337
338 ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
339 if (ret)
340 return ret;
341 }
342#else
Simon Guinot11efe712010-07-06 16:08:46 +0200343 if (!pdata)
344 return -EINVAL;
Simon Guinot72052fc2012-10-17 12:09:03 +0200345#endif /* CONFIG_OF_GPIO */
Simon Guinot11efe712010-07-06 16:08:46 +0200346
Simon Guinot3de19292013-03-19 11:07:29 -0700347 priv = devm_kzalloc(&pdev->dev,
348 sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
349 if (!priv)
Simon Guinot11efe712010-07-06 16:08:46 +0200350 return -ENOMEM;
Simon Guinot3de19292013-03-19 11:07:29 -0700351 priv->num_leds = pdata->num_leds;
Simon Guinot11efe712010-07-06 16:08:46 +0200352
Simon Guinot3de19292013-03-19 11:07:29 -0700353 for (i = 0; i < priv->num_leds; i++) {
354 ret = create_ns2_led(pdev, &priv->leds_data[i],
355 &pdata->leds[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800356 if (ret < 0) {
357 for (i = i - 1; i >= 0; i--)
Simon Guinot3de19292013-03-19 11:07:29 -0700358 delete_ns2_led(&priv->leds_data[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800359 return ret;
360 }
Simon Guinot11efe712010-07-06 16:08:46 +0200361 }
362
Simon Guinot3de19292013-03-19 11:07:29 -0700363 platform_set_drvdata(pdev, priv);
Simon Guinot11efe712010-07-06 16:08:46 +0200364
365 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200366}
367
Bill Pemberton678e8a62012-11-19 13:26:00 -0500368static int ns2_led_remove(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200369{
370 int i;
Simon Guinot3de19292013-03-19 11:07:29 -0700371 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200372
Simon Guinot3de19292013-03-19 11:07:29 -0700373 priv = platform_get_drvdata(pdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200374
Simon Guinot3de19292013-03-19 11:07:29 -0700375 for (i = 0; i < priv->num_leds; i++)
376 delete_ns2_led(&priv->leds_data[i]);
Simon Guinot11efe712010-07-06 16:08:46 +0200377
Simon Guinot11efe712010-07-06 16:08:46 +0200378 return 0;
379}
380
381static struct platform_driver ns2_led_driver = {
382 .probe = ns2_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500383 .remove = ns2_led_remove,
Simon Guinot11efe712010-07-06 16:08:46 +0200384 .driver = {
Simon Guinot72052fc2012-10-17 12:09:03 +0200385 .name = "leds-ns2",
386 .owner = THIS_MODULE,
387 .of_match_table = of_match_ptr(of_ns2_leds_match),
Simon Guinot11efe712010-07-06 16:08:46 +0200388 },
389};
Simon Guinot11efe712010-07-06 16:08:46 +0200390
Axel Lin892a8842012-01-10 15:09:24 -0800391module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200392
393MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
394MODULE_DESCRIPTION("Network Space v2 LED driver");
395MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800396MODULE_ALIAS("platform:leds-ns2");