blob: b33514d9f427e279bf091abab4126f55e0d48182 [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};
340#endif /* CONFIG_OF_GPIO */
341
Simon Guinot3de19292013-03-19 11:07:29 -0700342struct ns2_led_priv {
343 int num_leds;
344 struct ns2_led_data leds_data[];
345};
346
347static inline int sizeof_ns2_led_priv(int num_leds)
348{
349 return sizeof(struct ns2_led_priv) +
350 (sizeof(struct ns2_led_data) * num_leds);
351}
352
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500353static int ns2_led_probe(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200354{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700355 struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Simon Guinot3de19292013-03-19 11:07:29 -0700356 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200357 int i;
358 int ret;
359
Simon Guinot72052fc2012-10-17 12:09:03 +0200360#ifdef CONFIG_OF_GPIO
361 if (!pdata) {
362 pdata = devm_kzalloc(&pdev->dev,
363 sizeof(struct ns2_led_platform_data),
364 GFP_KERNEL);
365 if (!pdata)
366 return -ENOMEM;
367
368 ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
369 if (ret)
370 return ret;
371 }
372#else
Simon Guinot11efe712010-07-06 16:08:46 +0200373 if (!pdata)
374 return -EINVAL;
Simon Guinot72052fc2012-10-17 12:09:03 +0200375#endif /* CONFIG_OF_GPIO */
Simon Guinot11efe712010-07-06 16:08:46 +0200376
Simon Guinot3de19292013-03-19 11:07:29 -0700377 priv = devm_kzalloc(&pdev->dev,
378 sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
379 if (!priv)
Simon Guinot11efe712010-07-06 16:08:46 +0200380 return -ENOMEM;
Simon Guinot3de19292013-03-19 11:07:29 -0700381 priv->num_leds = pdata->num_leds;
Simon Guinot11efe712010-07-06 16:08:46 +0200382
Simon Guinot3de19292013-03-19 11:07:29 -0700383 for (i = 0; i < priv->num_leds; i++) {
384 ret = create_ns2_led(pdev, &priv->leds_data[i],
385 &pdata->leds[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800386 if (ret < 0) {
387 for (i = i - 1; i >= 0; i--)
Simon Guinot3de19292013-03-19 11:07:29 -0700388 delete_ns2_led(&priv->leds_data[i]);
Bryan Wua209f762012-07-04 12:30:50 +0800389 return ret;
390 }
Simon Guinot11efe712010-07-06 16:08:46 +0200391 }
392
Simon Guinot3de19292013-03-19 11:07:29 -0700393 platform_set_drvdata(pdev, priv);
Simon Guinot11efe712010-07-06 16:08:46 +0200394
395 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200396}
397
Bill Pemberton678e8a62012-11-19 13:26:00 -0500398static int ns2_led_remove(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200399{
400 int i;
Simon Guinot3de19292013-03-19 11:07:29 -0700401 struct ns2_led_priv *priv;
Simon Guinot11efe712010-07-06 16:08:46 +0200402
Simon Guinot3de19292013-03-19 11:07:29 -0700403 priv = platform_get_drvdata(pdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200404
Simon Guinot3de19292013-03-19 11:07:29 -0700405 for (i = 0; i < priv->num_leds; i++)
406 delete_ns2_led(&priv->leds_data[i]);
Simon Guinot11efe712010-07-06 16:08:46 +0200407
Simon Guinot11efe712010-07-06 16:08:46 +0200408 return 0;
409}
410
411static struct platform_driver ns2_led_driver = {
412 .probe = ns2_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500413 .remove = ns2_led_remove,
Simon Guinot11efe712010-07-06 16:08:46 +0200414 .driver = {
Simon Guinot72052fc2012-10-17 12:09:03 +0200415 .name = "leds-ns2",
Simon Guinot72052fc2012-10-17 12:09:03 +0200416 .of_match_table = of_match_ptr(of_ns2_leds_match),
Simon Guinot11efe712010-07-06 16:08:46 +0200417 },
418};
Simon Guinot11efe712010-07-06 16:08:46 +0200419
Axel Lin892a8842012-01-10 15:09:24 -0800420module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200421
422MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
423MODULE_DESCRIPTION("Network Space v2 LED driver");
424MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800425MODULE_ALIAS("platform:leds-ns2");