blob: 1677d66d8b0e349b79e5d8c4fbaaee855f62a3a1 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Simon Guinot11efe712010-07-06 16:08:46 +02002/*
3 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
4 *
5 * Copyright (C) 2010 LaCie
6 *
7 * Author: Simon Guinot <sguinot@lacie.com>
8 *
9 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
Simon Guinot11efe712010-07-06 16:08:46 +020010 */
11
12#include <linux/kernel.h>
Simon Guinot11efe712010-07-06 16:08:46 +020013#include <linux/platform_device.h>
14#include <linux/slab.h>
Linus Walleijc7896492020-01-07 15:10:28 +010015#include <linux/gpio/consumer.h>
Simon Guinot11efe712010-07-06 16:08:46 +020016#include <linux/leds.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040017#include <linux/module.h>
Sachin Kamatc68f46d2013-09-28 04:38:30 -070018#include <linux/of.h>
Simon Guinot4b904322015-07-02 19:56:42 +020019#include "leds.h"
Simon Guinot11efe712010-07-06 16:08:46 +020020
Linus Walleijc7896492020-01-07 15:10:28 +010021enum ns2_led_modes {
22 NS_V2_LED_OFF,
23 NS_V2_LED_ON,
24 NS_V2_LED_SATA,
25};
26
Marek Behún940cca12020-09-26 22:11:30 +020027/*
28 * If the size of this structure or types of its members is changed,
29 * the filling of array modval in function ns2_led_register must be changed
30 * accordingly.
31 */
Linus Walleijc7896492020-01-07 15:10:28 +010032struct ns2_led_modval {
Marek Behún940cca12020-09-26 22:11:30 +020033 u32 mode;
34 u32 cmd_level;
35 u32 slow_level;
36} __packed;
Linus Walleijc7896492020-01-07 15:10:28 +010037
Simon Guinot11efe712010-07-06 16:08:46 +020038/*
Vincent Donnefortf7fafd02015-07-02 19:56:40 +020039 * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
40 * modes are available: off, on and SATA activity blinking. The LED modes are
41 * controlled through two GPIOs (command and slow): each combination of values
42 * for the command/slow GPIOs corresponds to a LED mode.
Simon Guinot11efe712010-07-06 16:08:46 +020043 */
44
Marek Behún01026ce2020-09-18 00:33:29 +020045struct ns2_led {
Simon Guinot11efe712010-07-06 16:08:46 +020046 struct led_classdev cdev;
Linus Walleijccbbb112020-01-07 15:10:29 +010047 struct gpio_desc *cmd;
48 struct gpio_desc *slow;
Simon Guinot4b904322015-07-02 19:56:42 +020049 bool can_sleep;
Simon Guinot11efe712010-07-06 16:08:46 +020050 unsigned char sata; /* True when SATA mode active. */
51 rwlock_t rw_lock; /* Lock GPIOs. */
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
Marek Behúna78bd8f2020-09-18 00:33:30 +020056static int ns2_led_get_mode(struct ns2_led *led, enum ns2_led_modes *mode)
Simon Guinot11efe712010-07-06 16:08:46 +020057{
58 int i;
Simon Guinot11efe712010-07-06 16:08:46 +020059 int cmd_level;
60 int slow_level;
61
Marek Behúna78bd8f2020-09-18 00:33:30 +020062 cmd_level = gpiod_get_value_cansleep(led->cmd);
63 slow_level = gpiod_get_value_cansleep(led->slow);
Simon Guinot11efe712010-07-06 16:08:46 +020064
Marek Behúna78bd8f2020-09-18 00:33:30 +020065 for (i = 0; i < led->num_modes; i++) {
66 if (cmd_level == led->modval[i].cmd_level &&
67 slow_level == led->modval[i].slow_level) {
68 *mode = led->modval[i].mode;
Marek Behúna2fc7032020-09-18 00:33:31 +020069 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +020070 }
71 }
72
Marek Behúna2fc7032020-09-18 00:33:31 +020073 return -EINVAL;
Simon Guinot11efe712010-07-06 16:08:46 +020074}
75
Marek Behúna78bd8f2020-09-18 00:33:30 +020076static void ns2_led_set_mode(struct ns2_led *led, enum ns2_led_modes mode)
Simon Guinot11efe712010-07-06 16:08:46 +020077{
78 int i;
Simon Guinotf539dfe2010-09-19 15:30:59 +020079 unsigned long flags;
Simon Guinot11efe712010-07-06 16:08:46 +020080
Marek Behúna78bd8f2020-09-18 00:33:30 +020081 for (i = 0; i < led->num_modes; i++)
Marek Behún48b77cd2020-09-18 00:33:34 +020082 if (mode == led->modval[i].mode)
Simon Guinot4b904322015-07-02 19:56:42 +020083 break;
Simon Guinot4b904322015-07-02 19:56:42 +020084
Marek Behún48b77cd2020-09-18 00:33:34 +020085 if (i == led->num_modes)
Simon Guinot4b904322015-07-02 19:56:42 +020086 return;
87
Marek Behúna78bd8f2020-09-18 00:33:30 +020088 write_lock_irqsave(&led->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +020089
Marek Behúna78bd8f2020-09-18 00:33:30 +020090 if (!led->can_sleep) {
91 gpiod_set_value(led->cmd, led->modval[i].cmd_level);
92 gpiod_set_value(led->slow, led->modval[i].slow_level);
Simon Guinot4b904322015-07-02 19:56:42 +020093 goto exit_unlock;
Simon Guinot11efe712010-07-06 16:08:46 +020094 }
95
Marek Behúna78bd8f2020-09-18 00:33:30 +020096 gpiod_set_value_cansleep(led->cmd, led->modval[i].cmd_level);
97 gpiod_set_value_cansleep(led->slow, led->modval[i].slow_level);
Simon Guinot4b904322015-07-02 19:56:42 +020098
99exit_unlock:
Marek Behúna78bd8f2020-09-18 00:33:30 +0200100 write_unlock_irqrestore(&led->rw_lock, flags);
Simon Guinot11efe712010-07-06 16:08:46 +0200101}
102
103static void ns2_led_set(struct led_classdev *led_cdev,
104 enum led_brightness value)
105{
Marek Behúna78bd8f2020-09-18 00:33:30 +0200106 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200107 enum ns2_led_modes mode;
108
109 if (value == LED_OFF)
110 mode = NS_V2_LED_OFF;
Marek Behúna78bd8f2020-09-18 00:33:30 +0200111 else if (led->sata)
Simon Guinot11efe712010-07-06 16:08:46 +0200112 mode = NS_V2_LED_SATA;
113 else
114 mode = NS_V2_LED_ON;
115
Marek Behúna78bd8f2020-09-18 00:33:30 +0200116 ns2_led_set_mode(led, mode);
Simon Guinot11efe712010-07-06 16:08:46 +0200117}
118
Jacek Anaszewskic29e6502015-11-20 11:39:41 +0100119static int ns2_led_set_blocking(struct led_classdev *led_cdev,
120 enum led_brightness value)
121{
122 ns2_led_set(led_cdev, value);
123 return 0;
124}
125
Simon Guinot11efe712010-07-06 16:08:46 +0200126static ssize_t ns2_led_sata_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buff, size_t count)
129{
Simon Guinote5971bb2010-10-07 16:35:40 +0200130 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Marek Behúna78bd8f2020-09-18 00:33:30 +0200131 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200132 int ret;
133 unsigned long enable;
Simon Guinot11efe712010-07-06 16:08:46 +0200134
Jingoo Han38743502012-10-23 05:25:35 -0700135 ret = kstrtoul(buff, 10, &enable);
Simon Guinot11efe712010-07-06 16:08:46 +0200136 if (ret < 0)
137 return ret;
138
139 enable = !!enable;
140
Marek Behúna78bd8f2020-09-18 00:33:30 +0200141 if (led->sata == enable)
Simon Guinot4b904322015-07-02 19:56:42 +0200142 goto exit;
Simon Guinot11efe712010-07-06 16:08:46 +0200143
Marek Behúna78bd8f2020-09-18 00:33:30 +0200144 led->sata = enable;
Simon Guinot11efe712010-07-06 16:08:46 +0200145
Simon Guinot4b904322015-07-02 19:56:42 +0200146 if (!led_get_brightness(led_cdev))
147 goto exit;
148
149 if (enable)
Marek Behúna78bd8f2020-09-18 00:33:30 +0200150 ns2_led_set_mode(led, NS_V2_LED_SATA);
Simon Guinot4b904322015-07-02 19:56:42 +0200151 else
Marek Behúna78bd8f2020-09-18 00:33:30 +0200152 ns2_led_set_mode(led, NS_V2_LED_ON);
Simon Guinot4b904322015-07-02 19:56:42 +0200153
154exit:
Simon Guinot11efe712010-07-06 16:08:46 +0200155 return count;
156}
157
158static ssize_t ns2_led_sata_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
Simon Guinote5971bb2010-10-07 16:35:40 +0200161 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Marek Behúna78bd8f2020-09-18 00:33:30 +0200162 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
Simon Guinot11efe712010-07-06 16:08:46 +0200163
Marek Behúna78bd8f2020-09-18 00:33:30 +0200164 return sprintf(buf, "%d\n", led->sata);
Simon Guinot11efe712010-07-06 16:08:46 +0200165}
166
167static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
168
Johan Hovold475f8542014-06-25 10:08:51 -0700169static struct attribute *ns2_led_attrs[] = {
170 &dev_attr_sata.attr,
171 NULL
172};
173ATTRIBUTE_GROUPS(ns2_led);
174
Marek Behún940cca12020-09-26 22:11:30 +0200175static int ns2_led_register(struct device *dev, struct fwnode_handle *node,
Marek Behúna4a469b2020-09-18 00:33:33 +0200176 struct ns2_led *led)
Marek Behúnf72deb72020-09-18 00:33:27 +0200177{
Marek Behúnf847ef52020-09-18 00:33:37 +0200178 struct led_init_data init_data = {};
Marek Behúnf72deb72020-09-18 00:33:27 +0200179 struct ns2_led_modval *modval;
Marek Behúna4a469b2020-09-18 00:33:33 +0200180 enum ns2_led_modes mode;
Marek Behún940cca12020-09-26 22:11:30 +0200181 int nmodes, ret;
Marek Behúnf72deb72020-09-18 00:33:27 +0200182
Marek Behún940cca12020-09-26 22:11:30 +0200183 led->cmd = devm_fwnode_gpiod_get_index(dev, node, "cmd", 0, GPIOD_ASIS,
184 fwnode_get_name(node));
Marek Behúnf72deb72020-09-18 00:33:27 +0200185 if (IS_ERR(led->cmd))
186 return PTR_ERR(led->cmd);
187
Marek Behún940cca12020-09-26 22:11:30 +0200188 led->slow = devm_fwnode_gpiod_get_index(dev, node, "slow", 0,
189 GPIOD_ASIS,
190 fwnode_get_name(node));
Marek Behúnf72deb72020-09-18 00:33:27 +0200191 if (IS_ERR(led->slow))
192 return PTR_ERR(led->slow);
193
Marek Behún940cca12020-09-26 22:11:30 +0200194 ret = fwnode_property_count_u32(node, "modes-map");
Marek Behúnf72deb72020-09-18 00:33:27 +0200195 if (ret < 0 || ret % 3) {
Marek Behún940cca12020-09-26 22:11:30 +0200196 dev_err(dev, "Missing or malformed modes-map for %pfw\n", node);
Marek Behúnf72deb72020-09-18 00:33:27 +0200197 return -EINVAL;
198 }
199
200 nmodes = ret / 3;
201 modval = devm_kcalloc(dev, nmodes, sizeof(*modval), GFP_KERNEL);
202 if (!modval)
203 return -ENOMEM;
204
Marek Behún940cca12020-09-26 22:11:30 +0200205 fwnode_property_read_u32_array(node, "modes-map", (void *)modval,
206 nmodes * 3);
Marek Behúnf72deb72020-09-18 00:33:27 +0200207
Marek Behúna4a469b2020-09-18 00:33:33 +0200208 rwlock_init(&led->rw_lock);
209
210 led->cdev.blink_set = NULL;
211 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
212 led->cdev.groups = ns2_led_groups;
213 led->can_sleep = gpiod_cansleep(led->cmd) || gpiod_cansleep(led->slow);
214 if (led->can_sleep)
215 led->cdev.brightness_set_blocking = ns2_led_set_blocking;
216 else
217 led->cdev.brightness_set = ns2_led_set;
Marek Behúnf72deb72020-09-18 00:33:27 +0200218 led->num_modes = nmodes;
219 led->modval = modval;
220
Marek Behúna4a469b2020-09-18 00:33:33 +0200221 ret = ns2_led_get_mode(led, &mode);
222 if (ret < 0)
223 return ret;
Marek Behúnf72deb72020-09-18 00:33:27 +0200224
Marek Behúna4a469b2020-09-18 00:33:33 +0200225 /* Set LED initial state. */
226 led->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
227 led->cdev.brightness = (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
Simon Guinot72052fc2012-10-17 12:09:03 +0200228
Marek Behún940cca12020-09-26 22:11:30 +0200229 init_data.fwnode = node;
Marek Behúnf847ef52020-09-18 00:33:37 +0200230
231 ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
Marek Behúna4a469b2020-09-18 00:33:33 +0200232 if (ret)
Marek Behún940cca12020-09-26 22:11:30 +0200233 dev_err(dev, "Failed to register LED for node %pfw\n", node);
Simon Guinot72052fc2012-10-17 12:09:03 +0200234
Marek Behúna4a469b2020-09-18 00:33:33 +0200235 return ret;
Simon Guinot72052fc2012-10-17 12:09:03 +0200236}
237
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500238static int ns2_led_probe(struct platform_device *pdev)
Simon Guinot11efe712010-07-06 16:08:46 +0200239{
Marek Behúnb3f96922020-09-18 00:33:32 +0200240 struct device *dev = &pdev->dev;
Marek Behún940cca12020-09-26 22:11:30 +0200241 struct fwnode_handle *child;
Marek Behún01026ce2020-09-18 00:33:29 +0200242 struct ns2_led *leds;
Marek Behúna4a469b2020-09-18 00:33:33 +0200243 int count;
Simon Guinot11efe712010-07-06 16:08:46 +0200244 int ret;
245
Marek Behún940cca12020-09-26 22:11:30 +0200246 count = device_get_child_node_count(dev);
Marek Behúna4a469b2020-09-18 00:33:33 +0200247 if (!count)
248 return -ENODEV;
Simon Guinot72052fc2012-10-17 12:09:03 +0200249
Marek Behúna4a469b2020-09-18 00:33:33 +0200250 leds = devm_kzalloc(dev, array_size(sizeof(*leds), count), GFP_KERNEL);
Marek Behún19d4deb2020-09-18 00:33:25 +0200251 if (!leds)
Simon Guinot11efe712010-07-06 16:08:46 +0200252 return -ENOMEM;
253
Marek Behún940cca12020-09-26 22:11:30 +0200254 device_for_each_child_node(dev, child) {
Marek Behúna4a469b2020-09-18 00:33:33 +0200255 ret = ns2_led_register(dev, child, leds++);
256 if (ret) {
Marek Behún940cca12020-09-26 22:11:30 +0200257 fwnode_handle_put(child);
Bryan Wua209f762012-07-04 12:30:50 +0800258 return ret;
Marek Behúna4a469b2020-09-18 00:33:33 +0200259 }
Simon Guinot11efe712010-07-06 16:08:46 +0200260 }
261
Simon Guinot11efe712010-07-06 16:08:46 +0200262 return 0;
Simon Guinot11efe712010-07-06 16:08:46 +0200263}
264
Marek Behún8fd8f942020-09-26 22:11:31 +0200265static const struct of_device_id of_ns2_leds_match[] = {
266 { .compatible = "lacie,ns2-leds", },
267 {},
268};
269MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
270
Simon Guinot11efe712010-07-06 16:08:46 +0200271static struct platform_driver ns2_led_driver = {
272 .probe = ns2_led_probe,
Simon Guinot11efe712010-07-06 16:08:46 +0200273 .driver = {
Simon Guinot72052fc2012-10-17 12:09:03 +0200274 .name = "leds-ns2",
Marek Behún8fd8f942020-09-26 22:11:31 +0200275 .of_match_table = of_ns2_leds_match,
Simon Guinot11efe712010-07-06 16:08:46 +0200276 },
277};
Simon Guinot11efe712010-07-06 16:08:46 +0200278
Axel Lin892a8842012-01-10 15:09:24 -0800279module_platform_driver(ns2_led_driver);
Simon Guinot11efe712010-07-06 16:08:46 +0200280
281MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
282MODULE_DESCRIPTION("Network Space v2 LED driver");
283MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800284MODULE_ALIAS("platform:leds-ns2");