blob: a95a61220169ddc8099ba127d01cdb0db3a5adcb [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>
Simon Guinot11efe712010-07-06 16:08:46 +020026#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/gpio.h>
29#include <linux/leds.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040030#include <linux/module.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020031#include <linux/platform_data/leds-kirkwood-ns2.h>
Sachin Kamatc68f46d2013-09-28 04:38:30 -070032#include <linux/of.h>
Simon Guinot72052fc2012-10-17 12:09:03 +020033#include <linux/of_gpio.h>
Simon Guinot4b904322015-07-02 19:56:42 +020034#include "leds.h"
Simon Guinot11efe712010-07-06 16:08:46 +020035
36/*
Vincent Donnefortf7fafd02015-07-02 19:56:40 +020037 * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
38 * modes are available: off, on and SATA activity blinking. The LED modes are
39 * controlled through two GPIOs (command and slow): each combination of values
40 * for the command/slow GPIOs corresponds to a LED mode.
Simon Guinot11efe712010-07-06 16:08:46 +020041 */
42
Simon Guinot11efe712010-07-06 16:08:46 +020043struct ns2_led_data {
44 struct led_classdev cdev;
45 unsigned cmd;
46 unsigned slow;
Simon Guinot4b904322015-07-02 19:56:42 +020047 bool can_sleep;
48 int mode_index;
Simon Guinot11efe712010-07-06 16:08:46 +020049 unsigned char sata; /* True when SATA mode active. */
50 rwlock_t rw_lock; /* Lock GPIOs. */
Simon Guinot4b904322015-07-02 19:56:42 +020051 struct work_struct work;
Vincent Donnefortf7fafd02015-07-02 19:56:40 +020052 int num_modes;
53 struct ns2_led_modval *modval;
Simon Guinot11efe712010-07-06 16:08:46 +020054};
55
Simon Guinot4b904322015-07-02 19:56:42 +020056static void ns2_led_work(struct work_struct *work)
57{
58 struct ns2_led_data *led_dat =
59 container_of(work, struct ns2_led_data, work);
60 int i = led_dat->mode_index;
61
62 gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
63 gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
64}
65
Simon Guinot11efe712010-07-06 16:08:46 +020066static int ns2_led_get_mode(struct ns2_led_data *led_dat,
67 enum ns2_led_modes *mode)
68{
69 int i;
70 int ret = -EINVAL;
71 int cmd_level;
72 int slow_level;
73
Simon Guinot4b904322015-07-02 19:56:42 +020074 cmd_level = gpio_get_value_cansleep(led_dat->cmd);
75 slow_level = gpio_get_value_cansleep(led_dat->slow);
Simon Guinot11efe712010-07-06 16:08:46 +020076
Vincent Donnefortf7fafd02015-07-02 19:56:40 +020077 for (i = 0; i < led_dat->num_modes; i++) {
78 if (cmd_level == led_dat->modval[i].cmd_level &&
79 slow_level == led_dat->modval[i].slow_level) {
80 *mode = led_dat->modval[i].mode;
Simon Guinot11efe712010-07-06 16:08:46 +020081 ret = 0;
82 break;
83 }
84 }
85
Simon Guinot11efe712010-07-06 16:08:46 +020086 return ret;
87}
88
89static void ns2_led_set_mode(struct ns2_led_data *led_dat,
90 enum ns2_led_modes mode)
91{
92 int i;
Simon Guinot4b904322015-07-02 19:56:42 +020093 bool found = false;
Simon Guinotf539dfe2010-09-19 15:30:59 +020094 unsigned long flags;
Simon Guinot11efe712010-07-06 16:08:46 +020095
Simon Guinot4b904322015-07-02 19:56:42 +020096 for (i = 0; i < led_dat->num_modes; i++)
97 if (mode == led_dat->modval[i].mode) {
98 found = true;
99 break;
100 }
101
102 if (!found)
103 return;
104
Simon Guinotf539dfe2010-09-19 15:30:59 +0200105 write_lock_irqsave(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200106
Simon Guinot4b904322015-07-02 19:56:42 +0200107 if (!led_dat->can_sleep) {
108 gpio_set_value(led_dat->cmd,
109 led_dat->modval[i].cmd_level);
110 gpio_set_value(led_dat->slow,
111 led_dat->modval[i].slow_level);
112 goto exit_unlock;
Simon Guinot11efe712010-07-06 16:08:46 +0200113 }
114
Simon Guinot4b904322015-07-02 19:56:42 +0200115 led_dat->mode_index = i;
116 schedule_work(&led_dat->work);
117
118exit_unlock:
Simon Guinotf539dfe2010-09-19 15:30:59 +0200119 write_unlock_irqrestore(&led_dat->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200120}
121
122static void ns2_led_set(struct led_classdev *led_cdev,
123 enum led_brightness value)
124{
125 struct ns2_led_data *led_dat =
126 container_of(led_cdev, struct ns2_led_data, cdev);
127 enum ns2_led_modes mode;
128
129 if (value == LED_OFF)
130 mode = NS_V2_LED_OFF;
131 else if (led_dat->sata)
132 mode = NS_V2_LED_SATA;
133 else
134 mode = NS_V2_LED_ON;
135
136 ns2_led_set_mode(led_dat, mode);
137}
138
139static ssize_t ns2_led_sata_store(struct device *dev,
140 struct device_attribute *attr,
141 const char *buff, size_t count)
142{
Simon Guinote5971bb2010-10-07 16:35:40 +0200143 struct led_classdev *led_cdev = dev_get_drvdata(dev);
144 struct ns2_led_data *led_dat =
145 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200146 int ret;
147 unsigned long enable;
Simon Guinot11efe712010-07-06 16:08:46 +0200148
Jingoo Han38743502012-10-23 05:25:35 -0700149 ret = kstrtoul(buff, 10, &enable);
Simon Guinot11efe712010-07-06 16:08:46 +0200150 if (ret < 0)
151 return ret;
152
153 enable = !!enable;
154
155 if (led_dat->sata == enable)
Simon Guinot4b904322015-07-02 19:56:42 +0200156 goto exit;
Simon Guinot11efe712010-07-06 16:08:46 +0200157
158 led_dat->sata = enable;
159
Simon Guinot4b904322015-07-02 19:56:42 +0200160 if (!led_get_brightness(led_cdev))
161 goto exit;
162
163 if (enable)
164 ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
165 else
166 ns2_led_set_mode(led_dat, NS_V2_LED_ON);
167
168exit:
Simon Guinot11efe712010-07-06 16:08:46 +0200169 return count;
170}
171
172static ssize_t ns2_led_sata_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
Simon Guinote5971bb2010-10-07 16:35:40 +0200175 struct led_classdev *led_cdev = dev_get_drvdata(dev);
176 struct ns2_led_data *led_dat =
177 container_of(led_cdev, struct ns2_led_data, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200178
179 return sprintf(buf, "%d\n", led_dat->sata);
180}
181
182static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
183
Johan Hovold475f8542014-06-25 10:08:51 -0700184static struct attribute *ns2_led_attrs[] = {
185 &dev_attr_sata.attr,
186 NULL
187};
188ATTRIBUTE_GROUPS(ns2_led);
189
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500190static int
Simon Guinot11efe712010-07-06 16:08:46 +0200191create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
192 const struct ns2_led *template)
193{
194 int ret;
195 enum ns2_led_modes mode;
196
Sachin Kamat04195822012-11-25 10:28:10 +0530197 ret = devm_gpio_request_one(&pdev->dev, template->cmd,
Simon Guinot4b904322015-07-02 19:56:42 +0200198 gpio_get_value_cansleep(template->cmd) ?
Jingoo Han9d04cba2013-03-07 18:38:26 -0800199 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700200 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200201 if (ret) {
202 dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
203 template->name);
Jingoo Han31c3dc72012-10-23 05:18:21 -0700204 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200205 }
206
Sachin Kamat04195822012-11-25 10:28:10 +0530207 ret = devm_gpio_request_one(&pdev->dev, template->slow,
Simon Guinot4b904322015-07-02 19:56:42 +0200208 gpio_get_value_cansleep(template->slow) ?
Jingoo Han9d04cba2013-03-07 18:38:26 -0800209 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
Jingoo Han31c3dc72012-10-23 05:18:21 -0700210 template->name);
Simon Guinot11efe712010-07-06 16:08:46 +0200211 if (ret) {
212 dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
213 template->name);
Sachin Kamat04195822012-11-25 10:28:10 +0530214 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200215 }
216
217 rwlock_init(&led_dat->rw_lock);
218
219 led_dat->cdev.name = template->name;
220 led_dat->cdev.default_trigger = template->default_trigger;
221 led_dat->cdev.blink_set = NULL;
222 led_dat->cdev.brightness_set = ns2_led_set;
223 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
Johan Hovold475f8542014-06-25 10:08:51 -0700224 led_dat->cdev.groups = ns2_led_groups;
Simon Guinot11efe712010-07-06 16:08:46 +0200225 led_dat->cmd = template->cmd;
226 led_dat->slow = template->slow;
Simon Guinot4b904322015-07-02 19:56:42 +0200227 led_dat->can_sleep = gpio_cansleep(led_dat->cmd) |
228 gpio_cansleep(led_dat->slow);
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200229 led_dat->modval = template->modval;
230 led_dat->num_modes = template->num_modes;
Simon Guinot11efe712010-07-06 16:08:46 +0200231
232 ret = ns2_led_get_mode(led_dat, &mode);
233 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530234 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200235
236 /* Set LED initial state. */
237 led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
238 led_dat->cdev.brightness =
239 (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
240
Simon Guinot4b904322015-07-02 19:56:42 +0200241 INIT_WORK(&led_dat->work, ns2_led_work);
242
Simon Guinot11efe712010-07-06 16:08:46 +0200243 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
244 if (ret < 0)
Sachin Kamat04195822012-11-25 10:28:10 +0530245 return ret;
Simon Guinot11efe712010-07-06 16:08:46 +0200246
Simon Guinot11efe712010-07-06 16:08:46 +0200247 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200248}
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{
Simon Guinot11efe712010-07-06 16:08:46 +0200252 led_classdev_unregister(&led_dat->cdev);
Simon Guinot4b904322015-07-02 19:56:42 +0200253 cancel_work_sync(&led_dat->work);
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;
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200265 struct ns2_led *led, *leds;
Simon Guinot72052fc2012-10-17 12:09:03 +0200266 int num_leds = 0;
Simon Guinot72052fc2012-10-17 12:09:03 +0200267
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
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200277 led = leds;
Simon Guinot72052fc2012-10-17 12:09:03 +0200278 for_each_child_of_node(np, child) {
279 const char *string;
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200280 int ret, i, num_modes;
281 struct ns2_led_modval *modval;
Simon Guinot72052fc2012-10-17 12:09:03 +0200282
283 ret = of_get_named_gpio(child, "cmd-gpio", 0);
284 if (ret < 0)
285 return ret;
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200286 led->cmd = ret;
Simon Guinot72052fc2012-10-17 12:09:03 +0200287 ret = of_get_named_gpio(child, "slow-gpio", 0);
288 if (ret < 0)
289 return ret;
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200290 led->slow = ret;
Simon Guinot72052fc2012-10-17 12:09:03 +0200291 ret = of_property_read_string(child, "label", &string);
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200292 led->name = (ret == 0) ? string : child->name;
Simon Guinot72052fc2012-10-17 12:09:03 +0200293 ret = of_property_read_string(child, "linux,default-trigger",
294 &string);
295 if (ret == 0)
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200296 led->default_trigger = string;
Simon Guinot72052fc2012-10-17 12:09:03 +0200297
Vincent Donnefortf7fafd02015-07-02 19:56:40 +0200298 ret = of_property_count_u32_elems(child, "modes-map");
299 if (ret < 0 || ret % 3) {
300 dev_err(dev,
301 "Missing or malformed modes-map property\n");
302 return -EINVAL;
303 }
304
305 num_modes = ret / 3;
306 modval = devm_kzalloc(dev,
307 num_modes * sizeof(struct ns2_led_modval),
308 GFP_KERNEL);
309 if (!modval)
310 return -ENOMEM;
311
312 for (i = 0; i < num_modes; i++) {
313 of_property_read_u32_index(child,
314 "modes-map", 3 * i,
315 (u32 *) &modval[i].mode);
316 of_property_read_u32_index(child,
317 "modes-map", 3 * i + 1,
318 (u32 *) &modval[i].cmd_level);
319 of_property_read_u32_index(child,
320 "modes-map", 3 * i + 2,
321 (u32 *) &modval[i].slow_level);
322 }
323
324 led->num_modes = num_modes;
325 led->modval = modval;
326
327 led++;
Simon Guinot72052fc2012-10-17 12:09:03 +0200328 }
329
330 pdata->leds = leds;
331 pdata->num_leds = num_leds;
332
333 return 0;
334}
335
336static const struct of_device_id of_ns2_leds_match[] = {
337 { .compatible = "lacie,ns2-leds", },
338 {},
339};
Luis de Bethencourt98f9cc72015-09-01 23:36:59 +0200340MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
Simon Guinot72052fc2012-10-17 12:09:03 +0200341#endif /* CONFIG_OF_GPIO */
342
Simon Guinot3de19292013-03-19 11:07:29 -0700343struct ns2_led_priv {
344 int num_leds;
345 struct ns2_led_data leds_data[];
346};
347
348static inline int sizeof_ns2_led_priv(int num_leds)
349{
350 return sizeof(struct ns2_led_priv) +
351 (sizeof(struct ns2_led_data) * num_leds);
352}
353
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500354static int ns2_led_probe(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200355{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700356 struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Simon Guinot3de19292013-03-19 11:07:29 -0700357 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200358 int i;
359 int ret;
360
Simon Guinot72052fc2012-10-17 12:09:03 +0200361#ifdef CONFIG_OF_GPIO
362 if (!pdata) {
363 pdata = devm_kzalloc(&pdev->dev,
364 sizeof(struct ns2_led_platform_data),
365 GFP_KERNEL);
366 if (!pdata)
367 return -ENOMEM;
368
369 ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
370 if (ret)
371 return ret;
372 }
373#else
Simon Guinot11efe712010-07-06 16:08:46 +0200374 if (!pdata)
375 return -EINVAL;
Simon Guinot72052fc2012-10-17 12:09:03 +0200376#endif /* CONFIG_OF_GPIO */
Simon Guinot11efe712010-07-06 16:08:46 +0200377
Simon Guinot3de19292013-03-19 11:07:29 -0700378 priv = devm_kzalloc(&pdev->dev,
379 sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
380 if (!priv)
Simon Guinot11efe712010-07-06 16:08:46 +0200381 return -ENOMEM;
Simon Guinot3de19292013-03-19 11:07:29 -0700382 priv->num_leds = pdata->num_leds;
Simon Guinot11efe712010-07-06 16:08:46 +0200383
Simon Guinot3de19292013-03-19 11:07:29 -0700384 for (i = 0; i < priv->num_leds; i++) {
385 ret = create_ns2_led(pdev, &priv->leds_data[i],
386 &pdata->leds[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800387 if (ret < 0) {
388 for (i = i - 1; i >= 0; i--)
Simon Guinot3de19292013-03-19 11:07:29 -0700389 delete_ns2_led(&priv->leds_data[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800390 return ret;
391 }
Simon Guinot11efe712010-07-06 16:08:46 +0200392 }
393
Simon Guinot3de19292013-03-19 11:07:29 -0700394 platform_set_drvdata(pdev, priv);
Simon Guinot11efe712010-07-06 16:08:46 +0200395
396 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200397}
398
Bill Pemberton678e8a62012-11-19 13:26:00 -0500399static int ns2_led_remove(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200400{
401 int i;
Simon Guinot3de19292013-03-19 11:07:29 -0700402 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200403
Simon Guinot3de19292013-03-19 11:07:29 -0700404 priv = platform_get_drvdata(pdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200405
Simon Guinot3de19292013-03-19 11:07:29 -0700406 for (i = 0; i < priv->num_leds; i++)
407 delete_ns2_led(&priv->leds_data[i]);
Simon Guinot11efe712010-07-06 16:08:46 +0200408
Simon Guinot11efe712010-07-06 16:08:46 +0200409 return 0;
410}
411
412static struct platform_driver ns2_led_driver = {
413 .probe = ns2_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500414 .remove = ns2_led_remove,
Simon Guinot11efe712010-07-06 16:08:46 +0200415 .driver = {
Simon Guinot72052fc2012-10-17 12:09:03 +0200416 .name = "leds-ns2",
Simon Guinot72052fc2012-10-17 12:09:03 +0200417 .of_match_table = of_match_ptr(of_ns2_leds_match),
Simon Guinot11efe712010-07-06 16:08:46 +0200418 },
419};
Simon Guinot11efe712010-07-06 16:08:46 +0200420
Axel Lin892a8842012-01-10 15:09:24 -0800421module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200422
423MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
424MODULE_DESCRIPTION("Network Space v2 LED driver");
425MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800426MODULE_ALIAS("platform:leds-ns2");