blob: 9505d862a2161dcfdcef05b389951e2c9b96179d [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 Guinot11efe712010-07-06 16:08:46 +020033
34/*
35 * The Network Space v2 dual-GPIO LED is wired to a CPLD and can blink in
36 * relation with the SATA activity. This capability is exposed through the
37 * "sata" sysfs attribute.
38 *
39 * The following array detail the different LED registers and the combination
40 * of their possible values:
41 *
42 * cmd_led | slow_led | /SATA active | LED state
43 * | | |
44 * 1 | 0 | x | off
45 * - | 1 | x | on
46 * 0 | 0 | 1 | on
47 * 0 | 0 | 0 | blink (rate 300ms)
48 */
49
50enum ns2_led_modes {
51 NS_V2_LED_OFF,
52 NS_V2_LED_ON,
53 NS_V2_LED_SATA,
54};
55
56struct ns2_led_mode_value {
57 enum ns2_led_modes mode;
58 int cmd_level;
59 int slow_level;
60};
61
62static struct ns2_led_mode_value ns2_led_modval[] = {
63 { NS_V2_LED_OFF , 1, 0 },
64 { NS_V2_LED_ON , 0, 1 },
65 { NS_V2_LED_ON , 1, 1 },
66 { NS_V2_LED_SATA, 0, 0 },
67};
68
69struct ns2_led_data {
70 struct led_classdev cdev;
71 unsigned cmd;
72 unsigned slow;
73 unsigned char sata; /* True when SATA mode active. */
74 rwlock_t rw_lock; /* Lock GPIOs. */
75};
76
77static int ns2_led_get_mode(struct ns2_led_data *led_dat,
78 enum ns2_led_modes *mode)
79{
80 int i;
81 int ret = -EINVAL;
82 int cmd_level;
83 int slow_level;
84
Simon Guinotf539dfe2010-09-19 15:30:59 +020085 read_lock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +020086
87 cmd_level = gpio_get_value(led_dat->cmd);
88 slow_level = gpio_get_value(led_dat->slow);
89
90 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
91 if (cmd_level == ns2_led_modval[i].cmd_level &&
92 slow_level == ns2_led_modval[i].slow_level) {
93 *mode = ns2_led_modval[i].mode;
94 ret = 0;
95 break;
96 }
97 }
98
Simon Guinotf539dfe2010-09-19 15:30:59 +020099 read_unlock_irq(&led_dat->rw_lock);
Simon Guinot11efe712010-07-06 16:08:46 +0200100
101 return ret;
102}
103
104static void ns2_led_set_mode(struct ns2_led_data *led_dat,
105 enum ns2_led_modes mode)
106{
107 int i;
Simon Guinotf539dfe2010-09-19 15:30:59 +0200108 unsigned long flags;
Simon Guinot11efe712010-07-06 16:08:46 +0200109
Simon Guinotf539dfe2010-09-19 15:30:59 +0200110 write_lock_irqsave(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200111
112 for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
113 if (mode == ns2_led_modval[i].mode) {
114 gpio_set_value(led_dat->cmd,
115 ns2_led_modval[i].cmd_level);
116 gpio_set_value(led_dat->slow,
117 ns2_led_modval[i].slow_level);
118 }
119 }
120
Simon Guinotf539dfe2010-09-19 15:30:59 +0200121 write_unlock_irqrestore(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200122}
123
124static void ns2_led_set(struct led_classdev *led_cdev,
125 enum led_brightness value)
126{
127 struct ns2_led_data *led_dat =
128 container_of(led_cdev, struct ns2_led_data, cdev);
129 enum ns2_led_modes mode;
130
131 if (value == LED_OFF)
132 mode = NS_V2_LED_OFF;
133 else if (led_dat->sata)
134 mode = NS_V2_LED_SATA;
135 else
136 mode = NS_V2_LED_ON;
137
138 ns2_led_set_mode(led_dat, mode);
139}
140
141static ssize_t ns2_led_sata_store(struct device *dev,
142 struct device_attribute *attr,
143 const char *buff, size_t count)
144{
Simon Guinote5971bb2010-10-07 16:35:40 +0200145 struct led_classdev *led_cdev = dev_get_drvdata(dev);
146 struct ns2_led_data *led_dat =
147 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200148 int ret;
149 unsigned long enable;
150 enum ns2_led_modes mode;
Simon Guinot11efe712010-07-06 16:08:46 +0200151
Jingoo Han38743502012-10-23 05:25:35 -0700152 ret = kstrtoul(buff, 10, &enable);
Simon Guinot11efe712010-07-06 16:08:46 +0200153 if (ret < 0)
154 return ret;
155
156 enable = !!enable;
157
158 if (led_dat->sata == enable)
159 return count;
160
161 ret = ns2_led_get_mode(led_dat, &mode);
162 if (ret < 0)
163 return ret;
164
165 if (enable && mode == NS_V2_LED_ON)
166 ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
167 if (!enable && mode == NS_V2_LED_SATA)
168 ns2_led_set_mode(led_dat, NS_V2_LED_ON);
169
170 led_dat->sata = enable;
171
172 return count;
173}
174
175static ssize_t ns2_led_sata_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
Simon Guinote5971bb2010-10-07 16:35:40 +0200178 struct led_classdev *led_cdev = dev_get_drvdata(dev);
179 struct ns2_led_data *led_dat =
180 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200181
182 return sprintf(buf, "%d\n", led_dat->sata);
183}
184
185static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
186
187static int __devinit
188create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
189 const struct ns2_led *template)
190{
191 int ret;
192 enum ns2_led_modes mode;
193
Sachin Kamat04195822012-11-25 10:28:10 +0530194 ret = devm_gpio_request_one(&pdev->dev, template->cmd,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700195 GPIOF_DIR_OUT | gpio_get_value(template->cmd),
196 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200197 if (ret) {
198 dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
199 template->name);
Jingoo Han31c3dc72012-10-23 05:18:21 -0700200 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200201 }
202
Sachin Kamat04195822012-11-25 10:28:10 +0530203 ret = devm_gpio_request_one(&pdev->dev, template->slow,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700204 GPIOF_DIR_OUT | gpio_get_value(template->slow),
205 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200206 if (ret) {
207 dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
208 template->name);
Sachin Kamat04195822012-11-25 10:28:10 +0530209 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200210 }
211
212 rwlock_init(&led_dat->rw_lock);
213
214 led_dat->cdev.name = template->name;
215 led_dat->cdev.default_trigger = template->default_trigger;
216 led_dat->cdev.blink_set = NULL;
217 led_dat->cdev.brightness_set = ns2_led_set;
218 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
219 led_dat->cmd = template->cmd;
220 led_dat->slow = template->slow;
221
222 ret = ns2_led_get_mode(led_dat, &mode);
223 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530224 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200225
226 /* Set LED initial state. */
227 led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
228 led_dat->cdev.brightness =
229 (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
230
231 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
232 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530233 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200234
Simon Guinot11efe712010-07-06 16:08:46 +0200235 ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
236 if (ret < 0)
237 goto err_free_cdev;
238
239 return 0;
240
241err_free_cdev:
242 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200243 return ret;
244}
245
Arnd Bergmannb8cd7422012-05-10 13:01:46 -0700246static void delete_ns2_led(struct ns2_led_data *led_dat)
Simon Guinot11efe712010-07-06 16:08:46 +0200247{
248 device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
249 led_classdev_unregister(&led_dat->cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200250}
251
252static int __devinit ns2_led_probe(struct platform_device *pdev)
253{
254 struct ns2_led_platform_data *pdata = pdev->dev.platform_data;
255 struct ns2_led_data *leds_data;
256 int i;
257 int ret;
258
259 if (!pdata)
260 return -EINVAL;
261
Bryan Wua209f762012-07-04 12:30:50 +0800262 leds_data = devm_kzalloc(&pdev->dev, sizeof(struct ns2_led_data) *
Simon Guinot11efe712010-07-06 16:08:46 +0200263 pdata->num_leds, GFP_KERNEL);
264 if (!leds_data)
265 return -ENOMEM;
266
267 for (i = 0; i < pdata->num_leds; i++) {
268 ret = create_ns2_led(pdev, &leds_data[i], &pdata->leds[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800269 if (ret < 0) {
270 for (i = i - 1; i >= 0; i--)
271 delete_ns2_led(&leds_data[i]);
272 return ret;
273 }
Simon Guinot11efe712010-07-06 16:08:46 +0200274 }
275
276 platform_set_drvdata(pdev, leds_data);
277
278 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200279}
280
281static int __devexit ns2_led_remove(struct platform_device *pdev)
282{
283 int i;
284 struct ns2_led_platform_data *pdata = pdev->dev.platform_data;
285 struct ns2_led_data *leds_data;
286
287 leds_data = platform_get_drvdata(pdev);
288
289 for (i = 0; i < pdata->num_leds; i++)
290 delete_ns2_led(&leds_data[i]);
291
Simon Guinot11efe712010-07-06 16:08:46 +0200292 platform_set_drvdata(pdev, NULL);
293
294 return 0;
295}
296
297static struct platform_driver ns2_led_driver = {
298 .probe = ns2_led_probe,
299 .remove = __devexit_p(ns2_led_remove),
300 .driver = {
301 .name = "leds-ns2",
302 .owner = THIS_MODULE,
303 },
304};
Simon Guinot11efe712010-07-06 16:08:46 +0200305
Axel Lin892a8842012-01-10 15:09:24 -0800306module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200307
308MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
309MODULE_DESCRIPTION("Network Space v2 LED driver");
310MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800311MODULE_ALIAS("platform:leds-ns2");