Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / leds-ns2.c
blob1677d66d8b0e349b79e5d8c4fbaaee855f62a3a1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
5 * Copyright (C) 2010 LaCie
7 * Author: Simon Guinot <sguinot@lacie.com>
9 * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include "leds.h"
21 enum ns2_led_modes {
22 NS_V2_LED_OFF,
23 NS_V2_LED_ON,
24 NS_V2_LED_SATA,
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.
32 struct ns2_led_modval {
33 u32 mode;
34 u32 cmd_level;
35 u32 slow_level;
36 } __packed;
39 * 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.
45 struct ns2_led {
46 struct led_classdev cdev;
47 struct gpio_desc *cmd;
48 struct gpio_desc *slow;
49 bool can_sleep;
50 unsigned char sata; /* True when SATA mode active. */
51 rwlock_t rw_lock; /* Lock GPIOs. */
52 int num_modes;
53 struct ns2_led_modval *modval;
56 static int ns2_led_get_mode(struct ns2_led *led, enum ns2_led_modes *mode)
58 int i;
59 int cmd_level;
60 int slow_level;
62 cmd_level = gpiod_get_value_cansleep(led->cmd);
63 slow_level = gpiod_get_value_cansleep(led->slow);
65 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;
69 return 0;
73 return -EINVAL;
76 static void ns2_led_set_mode(struct ns2_led *led, enum ns2_led_modes mode)
78 int i;
79 unsigned long flags;
81 for (i = 0; i < led->num_modes; i++)
82 if (mode == led->modval[i].mode)
83 break;
85 if (i == led->num_modes)
86 return;
88 write_lock_irqsave(&led->rw_lock, flags);
90 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);
93 goto exit_unlock;
96 gpiod_set_value_cansleep(led->cmd, led->modval[i].cmd_level);
97 gpiod_set_value_cansleep(led->slow, led->modval[i].slow_level);
99 exit_unlock:
100 write_unlock_irqrestore(&led->rw_lock, flags);
103 static void ns2_led_set(struct led_classdev *led_cdev,
104 enum led_brightness value)
106 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
107 enum ns2_led_modes mode;
109 if (value == LED_OFF)
110 mode = NS_V2_LED_OFF;
111 else if (led->sata)
112 mode = NS_V2_LED_SATA;
113 else
114 mode = NS_V2_LED_ON;
116 ns2_led_set_mode(led, mode);
119 static int ns2_led_set_blocking(struct led_classdev *led_cdev,
120 enum led_brightness value)
122 ns2_led_set(led_cdev, value);
123 return 0;
126 static ssize_t ns2_led_sata_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buff, size_t count)
130 struct led_classdev *led_cdev = dev_get_drvdata(dev);
131 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
132 int ret;
133 unsigned long enable;
135 ret = kstrtoul(buff, 10, &enable);
136 if (ret < 0)
137 return ret;
139 enable = !!enable;
141 if (led->sata == enable)
142 goto exit;
144 led->sata = enable;
146 if (!led_get_brightness(led_cdev))
147 goto exit;
149 if (enable)
150 ns2_led_set_mode(led, NS_V2_LED_SATA);
151 else
152 ns2_led_set_mode(led, NS_V2_LED_ON);
154 exit:
155 return count;
158 static ssize_t ns2_led_sata_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
161 struct led_classdev *led_cdev = dev_get_drvdata(dev);
162 struct ns2_led *led = container_of(led_cdev, struct ns2_led, cdev);
164 return sprintf(buf, "%d\n", led->sata);
167 static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
169 static struct attribute *ns2_led_attrs[] = {
170 &dev_attr_sata.attr,
171 NULL
173 ATTRIBUTE_GROUPS(ns2_led);
175 static int ns2_led_register(struct device *dev, struct fwnode_handle *node,
176 struct ns2_led *led)
178 struct led_init_data init_data = {};
179 struct ns2_led_modval *modval;
180 enum ns2_led_modes mode;
181 int nmodes, ret;
183 led->cmd = devm_fwnode_gpiod_get_index(dev, node, "cmd", 0, GPIOD_ASIS,
184 fwnode_get_name(node));
185 if (IS_ERR(led->cmd))
186 return PTR_ERR(led->cmd);
188 led->slow = devm_fwnode_gpiod_get_index(dev, node, "slow", 0,
189 GPIOD_ASIS,
190 fwnode_get_name(node));
191 if (IS_ERR(led->slow))
192 return PTR_ERR(led->slow);
194 ret = fwnode_property_count_u32(node, "modes-map");
195 if (ret < 0 || ret % 3) {
196 dev_err(dev, "Missing or malformed modes-map for %pfw\n", node);
197 return -EINVAL;
200 nmodes = ret / 3;
201 modval = devm_kcalloc(dev, nmodes, sizeof(*modval), GFP_KERNEL);
202 if (!modval)
203 return -ENOMEM;
205 fwnode_property_read_u32_array(node, "modes-map", (void *)modval,
206 nmodes * 3);
208 rwlock_init(&led->rw_lock);
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;
218 led->num_modes = nmodes;
219 led->modval = modval;
221 ret = ns2_led_get_mode(led, &mode);
222 if (ret < 0)
223 return ret;
225 /* 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;
229 init_data.fwnode = node;
231 ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
232 if (ret)
233 dev_err(dev, "Failed to register LED for node %pfw\n", node);
235 return ret;
238 static int ns2_led_probe(struct platform_device *pdev)
240 struct device *dev = &pdev->dev;
241 struct fwnode_handle *child;
242 struct ns2_led *leds;
243 int count;
244 int ret;
246 count = device_get_child_node_count(dev);
247 if (!count)
248 return -ENODEV;
250 leds = devm_kzalloc(dev, array_size(sizeof(*leds), count), GFP_KERNEL);
251 if (!leds)
252 return -ENOMEM;
254 device_for_each_child_node(dev, child) {
255 ret = ns2_led_register(dev, child, leds++);
256 if (ret) {
257 fwnode_handle_put(child);
258 return ret;
262 return 0;
265 static const struct of_device_id of_ns2_leds_match[] = {
266 { .compatible = "lacie,ns2-leds", },
269 MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
271 static struct platform_driver ns2_led_driver = {
272 .probe = ns2_led_probe,
273 .driver = {
274 .name = "leds-ns2",
275 .of_match_table = of_ns2_leds_match,
279 module_platform_driver(ns2_led_driver);
281 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
282 MODULE_DESCRIPTION("Network Space v2 LED driver");
283 MODULE_LICENSE("GPL");
284 MODULE_ALIAS("platform:leds-ns2");