1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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
32 struct ns2_led_modval
{
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.
46 struct led_classdev cdev
;
47 struct gpio_desc
*cmd
;
48 struct gpio_desc
*slow
;
50 unsigned char sata
; /* True when SATA mode active. */
51 rwlock_t rw_lock
; /* Lock GPIOs. */
53 struct ns2_led_modval
*modval
;
56 static int ns2_led_get_mode(struct ns2_led
*led
, enum ns2_led_modes
*mode
)
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
;
76 static void ns2_led_set_mode(struct ns2_led
*led
, enum ns2_led_modes mode
)
81 for (i
= 0; i
< led
->num_modes
; i
++)
82 if (mode
== led
->modval
[i
].mode
)
85 if (i
== led
->num_modes
)
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
);
96 gpiod_set_value_cansleep(led
->cmd
, led
->modval
[i
].cmd_level
);
97 gpiod_set_value_cansleep(led
->slow
, led
->modval
[i
].slow_level
);
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
;
112 mode
= NS_V2_LED_SATA
;
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
);
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
);
133 unsigned long enable
;
135 ret
= kstrtoul(buff
, 10, &enable
);
141 if (led
->sata
== enable
)
146 if (!led_get_brightness(led_cdev
))
150 ns2_led_set_mode(led
, NS_V2_LED_SATA
);
152 ns2_led_set_mode(led
, NS_V2_LED_ON
);
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
[] = {
173 ATTRIBUTE_GROUPS(ns2_led
);
175 static int ns2_led_register(struct device
*dev
, struct fwnode_handle
*node
,
178 struct led_init_data init_data
= {};
179 struct ns2_led_modval
*modval
;
180 enum ns2_led_modes mode
;
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,
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
);
201 modval
= devm_kcalloc(dev
, nmodes
, sizeof(*modval
), GFP_KERNEL
);
205 fwnode_property_read_u32_array(node
, "modes-map", (void *)modval
,
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
);
215 led
->cdev
.brightness_set_blocking
= ns2_led_set_blocking
;
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
);
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
);
233 dev_err(dev
, "Failed to register LED for node %pfw\n", node
);
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
;
246 count
= device_get_child_node_count(dev
);
250 leds
= devm_kzalloc(dev
, array_size(sizeof(*leds
), count
), GFP_KERNEL
);
254 device_for_each_child_node(dev
, child
) {
255 ret
= ns2_led_register(dev
, child
, leds
++);
257 fwnode_handle_put(child
);
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
,
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");