xref: /openbmc/linux/drivers/leds/leds-ns2.c (revision a7ad53cb)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
211efe71fSSimon Guinot /*
311efe71fSSimon Guinot  * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
411efe71fSSimon Guinot  *
511efe71fSSimon Guinot  * Copyright (C) 2010 LaCie
611efe71fSSimon Guinot  *
711efe71fSSimon Guinot  * Author: Simon Guinot <sguinot@lacie.com>
811efe71fSSimon Guinot  *
911efe71fSSimon Guinot  * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
1011efe71fSSimon Guinot  */
1111efe71fSSimon Guinot 
1211efe71fSSimon Guinot #include <linux/kernel.h>
1311efe71fSSimon Guinot #include <linux/platform_device.h>
1411efe71fSSimon Guinot #include <linux/slab.h>
15c7896490SLinus Walleij #include <linux/gpio/consumer.h>
1611efe71fSSimon Guinot #include <linux/leds.h>
1754f4dedbSPaul Gortmaker #include <linux/module.h>
18c68f46ddSSachin Kamat #include <linux/of.h>
194b90432dSSimon Guinot #include "leds.h"
2011efe71fSSimon Guinot 
21c7896490SLinus Walleij enum ns2_led_modes {
22c7896490SLinus Walleij 	NS_V2_LED_OFF,
23c7896490SLinus Walleij 	NS_V2_LED_ON,
24c7896490SLinus Walleij 	NS_V2_LED_SATA,
25c7896490SLinus Walleij };
26c7896490SLinus Walleij 
27c7896490SLinus Walleij struct ns2_led_modval {
28c7896490SLinus Walleij 	enum ns2_led_modes	mode;
29c7896490SLinus Walleij 	int			cmd_level;
30c7896490SLinus Walleij 	int			slow_level;
31c7896490SLinus Walleij };
32c7896490SLinus Walleij 
33c7896490SLinus Walleij struct ns2_led {
34c7896490SLinus Walleij 	const char	*name;
35c7896490SLinus Walleij 	const char	*default_trigger;
36ccbbb117SLinus Walleij 	struct gpio_desc *cmd;
37ccbbb117SLinus Walleij 	struct gpio_desc *slow;
38c7896490SLinus Walleij 	int		num_modes;
39c7896490SLinus Walleij 	struct ns2_led_modval *modval;
40c7896490SLinus Walleij };
41c7896490SLinus Walleij 
42c7896490SLinus Walleij struct ns2_led_platform_data {
43c7896490SLinus Walleij 	int		num_leds;
44c7896490SLinus Walleij 	struct ns2_led	*leds;
45c7896490SLinus Walleij };
46c7896490SLinus Walleij 
4711efe71fSSimon Guinot /*
48f7fafd08SVincent Donnefort  * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
49f7fafd08SVincent Donnefort  * modes are available: off, on and SATA activity blinking. The LED modes are
50f7fafd08SVincent Donnefort  * controlled through two GPIOs (command and slow): each combination of values
51f7fafd08SVincent Donnefort  * for the command/slow GPIOs corresponds to a LED mode.
5211efe71fSSimon Guinot  */
5311efe71fSSimon Guinot 
5411efe71fSSimon Guinot struct ns2_led_data {
5511efe71fSSimon Guinot 	struct led_classdev	cdev;
56ccbbb117SLinus Walleij 	struct gpio_desc	*cmd;
57ccbbb117SLinus Walleij 	struct gpio_desc	*slow;
584b90432dSSimon Guinot 	bool			can_sleep;
5911efe71fSSimon Guinot 	unsigned char		sata; /* True when SATA mode active. */
6011efe71fSSimon Guinot 	rwlock_t		rw_lock; /* Lock GPIOs. */
61f7fafd08SVincent Donnefort 	int			num_modes;
62f7fafd08SVincent Donnefort 	struct ns2_led_modval	*modval;
6311efe71fSSimon Guinot };
6411efe71fSSimon Guinot 
6511efe71fSSimon Guinot static int ns2_led_get_mode(struct ns2_led_data *led_dat,
6611efe71fSSimon Guinot 			    enum ns2_led_modes *mode)
6711efe71fSSimon Guinot {
6811efe71fSSimon Guinot 	int i;
6911efe71fSSimon Guinot 	int ret = -EINVAL;
7011efe71fSSimon Guinot 	int cmd_level;
7111efe71fSSimon Guinot 	int slow_level;
7211efe71fSSimon Guinot 
73ccbbb117SLinus Walleij 	cmd_level = gpiod_get_value_cansleep(led_dat->cmd);
74ccbbb117SLinus Walleij 	slow_level = gpiod_get_value_cansleep(led_dat->slow);
7511efe71fSSimon Guinot 
76f7fafd08SVincent Donnefort 	for (i = 0; i < led_dat->num_modes; i++) {
77f7fafd08SVincent Donnefort 		if (cmd_level == led_dat->modval[i].cmd_level &&
78f7fafd08SVincent Donnefort 		    slow_level == led_dat->modval[i].slow_level) {
79f7fafd08SVincent Donnefort 			*mode = led_dat->modval[i].mode;
8011efe71fSSimon Guinot 			ret = 0;
8111efe71fSSimon Guinot 			break;
8211efe71fSSimon Guinot 		}
8311efe71fSSimon Guinot 	}
8411efe71fSSimon Guinot 
8511efe71fSSimon Guinot 	return ret;
8611efe71fSSimon Guinot }
8711efe71fSSimon Guinot 
8811efe71fSSimon Guinot static void ns2_led_set_mode(struct ns2_led_data *led_dat,
8911efe71fSSimon Guinot 			     enum ns2_led_modes mode)
9011efe71fSSimon Guinot {
9111efe71fSSimon Guinot 	int i;
924b90432dSSimon Guinot 	bool found = false;
93f539dfedSSimon Guinot 	unsigned long flags;
9411efe71fSSimon Guinot 
954b90432dSSimon Guinot 	for (i = 0; i < led_dat->num_modes; i++)
964b90432dSSimon Guinot 		if (mode == led_dat->modval[i].mode) {
974b90432dSSimon Guinot 			found = true;
984b90432dSSimon Guinot 			break;
994b90432dSSimon Guinot 		}
1004b90432dSSimon Guinot 
1014b90432dSSimon Guinot 	if (!found)
1024b90432dSSimon Guinot 		return;
1034b90432dSSimon Guinot 
104f539dfedSSimon Guinot 	write_lock_irqsave(&led_dat->rw_lock, flags);
10511efe71fSSimon Guinot 
1064b90432dSSimon Guinot 	if (!led_dat->can_sleep) {
107ccbbb117SLinus Walleij 		gpiod_set_value(led_dat->cmd,
108f7fafd08SVincent Donnefort 				led_dat->modval[i].cmd_level);
109ccbbb117SLinus Walleij 		gpiod_set_value(led_dat->slow,
110f7fafd08SVincent Donnefort 				led_dat->modval[i].slow_level);
1114b90432dSSimon Guinot 		goto exit_unlock;
11211efe71fSSimon Guinot 	}
11311efe71fSSimon Guinot 
114ccbbb117SLinus Walleij 	gpiod_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
115ccbbb117SLinus Walleij 	gpiod_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
1164b90432dSSimon Guinot 
1174b90432dSSimon Guinot exit_unlock:
118f539dfedSSimon Guinot 	write_unlock_irqrestore(&led_dat->rw_lock, flags);
11911efe71fSSimon Guinot }
12011efe71fSSimon Guinot 
12111efe71fSSimon Guinot static void ns2_led_set(struct led_classdev *led_cdev,
12211efe71fSSimon Guinot 			enum led_brightness value)
12311efe71fSSimon Guinot {
12411efe71fSSimon Guinot 	struct ns2_led_data *led_dat =
12511efe71fSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
12611efe71fSSimon Guinot 	enum ns2_led_modes mode;
12711efe71fSSimon Guinot 
12811efe71fSSimon Guinot 	if (value == LED_OFF)
12911efe71fSSimon Guinot 		mode = NS_V2_LED_OFF;
13011efe71fSSimon Guinot 	else if (led_dat->sata)
13111efe71fSSimon Guinot 		mode = NS_V2_LED_SATA;
13211efe71fSSimon Guinot 	else
13311efe71fSSimon Guinot 		mode = NS_V2_LED_ON;
13411efe71fSSimon Guinot 
13511efe71fSSimon Guinot 	ns2_led_set_mode(led_dat, mode);
13611efe71fSSimon Guinot }
13711efe71fSSimon Guinot 
138c29e650bSJacek Anaszewski static int ns2_led_set_blocking(struct led_classdev *led_cdev,
139c29e650bSJacek Anaszewski 			enum led_brightness value)
140c29e650bSJacek Anaszewski {
141c29e650bSJacek Anaszewski 	ns2_led_set(led_cdev, value);
142c29e650bSJacek Anaszewski 	return 0;
143c29e650bSJacek Anaszewski }
144c29e650bSJacek Anaszewski 
14511efe71fSSimon Guinot static ssize_t ns2_led_sata_store(struct device *dev,
14611efe71fSSimon Guinot 				  struct device_attribute *attr,
14711efe71fSSimon Guinot 				  const char *buff, size_t count)
14811efe71fSSimon Guinot {
149e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
150e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
151e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
15211efe71fSSimon Guinot 	int ret;
15311efe71fSSimon Guinot 	unsigned long enable;
15411efe71fSSimon Guinot 
1553874350cSJingoo Han 	ret = kstrtoul(buff, 10, &enable);
15611efe71fSSimon Guinot 	if (ret < 0)
15711efe71fSSimon Guinot 		return ret;
15811efe71fSSimon Guinot 
15911efe71fSSimon Guinot 	enable = !!enable;
16011efe71fSSimon Guinot 
16111efe71fSSimon Guinot 	if (led_dat->sata == enable)
1624b90432dSSimon Guinot 		goto exit;
16311efe71fSSimon Guinot 
16411efe71fSSimon Guinot 	led_dat->sata = enable;
16511efe71fSSimon Guinot 
1664b90432dSSimon Guinot 	if (!led_get_brightness(led_cdev))
1674b90432dSSimon Guinot 		goto exit;
1684b90432dSSimon Guinot 
1694b90432dSSimon Guinot 	if (enable)
1704b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
1714b90432dSSimon Guinot 	else
1724b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_ON);
1734b90432dSSimon Guinot 
1744b90432dSSimon Guinot exit:
17511efe71fSSimon Guinot 	return count;
17611efe71fSSimon Guinot }
17711efe71fSSimon Guinot 
17811efe71fSSimon Guinot static ssize_t ns2_led_sata_show(struct device *dev,
17911efe71fSSimon Guinot 				 struct device_attribute *attr, char *buf)
18011efe71fSSimon Guinot {
181e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
182e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
183e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
18411efe71fSSimon Guinot 
18511efe71fSSimon Guinot 	return sprintf(buf, "%d\n", led_dat->sata);
18611efe71fSSimon Guinot }
18711efe71fSSimon Guinot 
18811efe71fSSimon Guinot static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
18911efe71fSSimon Guinot 
190475f8548SJohan Hovold static struct attribute *ns2_led_attrs[] = {
191475f8548SJohan Hovold 	&dev_attr_sata.attr,
192475f8548SJohan Hovold 	NULL
193475f8548SJohan Hovold };
194475f8548SJohan Hovold ATTRIBUTE_GROUPS(ns2_led);
195475f8548SJohan Hovold 
19698ea1ea2SBill Pemberton static int
19711efe71fSSimon Guinot create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
19811efe71fSSimon Guinot 	       const struct ns2_led *template)
19911efe71fSSimon Guinot {
20011efe71fSSimon Guinot 	int ret;
20111efe71fSSimon Guinot 	enum ns2_led_modes mode;
20211efe71fSSimon Guinot 
20311efe71fSSimon Guinot 	rwlock_init(&led_dat->rw_lock);
20411efe71fSSimon Guinot 
20511efe71fSSimon Guinot 	led_dat->cdev.name = template->name;
20611efe71fSSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
20711efe71fSSimon Guinot 	led_dat->cdev.blink_set = NULL;
20811efe71fSSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
209475f8548SJohan Hovold 	led_dat->cdev.groups = ns2_led_groups;
21011efe71fSSimon Guinot 	led_dat->cmd = template->cmd;
21111efe71fSSimon Guinot 	led_dat->slow = template->slow;
212ccbbb117SLinus Walleij 	led_dat->can_sleep = gpiod_cansleep(led_dat->cmd) |
213ccbbb117SLinus Walleij 				gpiod_cansleep(led_dat->slow);
214c29e650bSJacek Anaszewski 	if (led_dat->can_sleep)
215c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking;
216c29e650bSJacek Anaszewski 	else
217c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set = ns2_led_set;
218f7fafd08SVincent Donnefort 	led_dat->modval = template->modval;
219f7fafd08SVincent Donnefort 	led_dat->num_modes = template->num_modes;
22011efe71fSSimon Guinot 
22111efe71fSSimon Guinot 	ret = ns2_led_get_mode(led_dat, &mode);
22211efe71fSSimon Guinot 	if (ret < 0)
22304195823SSachin Kamat 		return ret;
22411efe71fSSimon Guinot 
22511efe71fSSimon Guinot 	/* Set LED initial state. */
22611efe71fSSimon Guinot 	led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
22711efe71fSSimon Guinot 	led_dat->cdev.brightness =
22811efe71fSSimon Guinot 		(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
22911efe71fSSimon Guinot 
23011efe71fSSimon Guinot 	ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
23111efe71fSSimon Guinot 	if (ret < 0)
23204195823SSachin Kamat 		return ret;
23311efe71fSSimon Guinot 
23411efe71fSSimon Guinot 	return 0;
23511efe71fSSimon Guinot }
23611efe71fSSimon Guinot 
237b8cd742aSArnd Bergmann static void delete_ns2_led(struct ns2_led_data *led_dat)
23811efe71fSSimon Guinot {
23911efe71fSSimon Guinot 	led_classdev_unregister(&led_dat->cdev);
24011efe71fSSimon Guinot }
24111efe71fSSimon Guinot 
24272052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
24372052fccSSimon Guinot /*
24472052fccSSimon Guinot  * Translate OpenFirmware node properties into platform_data.
24572052fccSSimon Guinot  */
246cf4af012SLinus Torvalds static int
24772052fccSSimon Guinot ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
24872052fccSSimon Guinot {
24972052fccSSimon Guinot 	struct device_node *np = dev->of_node;
25072052fccSSimon Guinot 	struct device_node *child;
251f7fafd08SVincent Donnefort 	struct ns2_led *led, *leds;
25279937a4bSNishka Dasgupta 	int ret, num_leds = 0;
25372052fccSSimon Guinot 
25472052fccSSimon Guinot 	num_leds = of_get_child_count(np);
25572052fccSSimon Guinot 	if (!num_leds)
25672052fccSSimon Guinot 		return -ENODEV;
25772052fccSSimon Guinot 
258a86854d0SKees Cook 	leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led),
25972052fccSSimon Guinot 			    GFP_KERNEL);
26072052fccSSimon Guinot 	if (!leds)
26172052fccSSimon Guinot 		return -ENOMEM;
26272052fccSSimon Guinot 
263f7fafd08SVincent Donnefort 	led = leds;
26472052fccSSimon Guinot 	for_each_child_of_node(np, child) {
26572052fccSSimon Guinot 		const char *string;
26679937a4bSNishka Dasgupta 		int i, num_modes;
267f7fafd08SVincent Donnefort 		struct ns2_led_modval *modval;
268ccbbb117SLinus Walleij 		struct gpio_desc *gd;
26972052fccSSimon Guinot 
27072052fccSSimon Guinot 		ret = of_property_read_string(child, "label", &string);
271f7fafd08SVincent Donnefort 		led->name = (ret == 0) ? string : child->name;
272ccbbb117SLinus Walleij 
273ccbbb117SLinus Walleij 		gd = gpiod_get_from_of_node(child, "cmd-gpio", 0,
274ccbbb117SLinus Walleij 					    GPIOD_ASIS, led->name);
275ccbbb117SLinus Walleij 		if (IS_ERR(gd)) {
276ccbbb117SLinus Walleij 			ret = PTR_ERR(gd);
277ccbbb117SLinus Walleij 			goto err_node_put;
278ccbbb117SLinus Walleij 		}
279ccbbb117SLinus Walleij 		led->cmd = gd;
280ccbbb117SLinus Walleij 		gd = gpiod_get_from_of_node(child, "slow-gpio", 0,
281ccbbb117SLinus Walleij 					    GPIOD_ASIS, led->name);
282ccbbb117SLinus Walleij 		if (IS_ERR(gd)) {
283ccbbb117SLinus Walleij 			ret = PTR_ERR(gd);
284ccbbb117SLinus Walleij 			goto err_node_put;
285ccbbb117SLinus Walleij 		}
286ccbbb117SLinus Walleij 		led->slow = gd;
287ccbbb117SLinus Walleij 
28872052fccSSimon Guinot 		ret = of_property_read_string(child, "linux,default-trigger",
28972052fccSSimon Guinot 					      &string);
29072052fccSSimon Guinot 		if (ret == 0)
291f7fafd08SVincent Donnefort 			led->default_trigger = string;
29272052fccSSimon Guinot 
293f7fafd08SVincent Donnefort 		ret = of_property_count_u32_elems(child, "modes-map");
294f7fafd08SVincent Donnefort 		if (ret < 0 || ret % 3) {
295f7fafd08SVincent Donnefort 			dev_err(dev,
296f7fafd08SVincent Donnefort 				"Missing or malformed modes-map property\n");
29779937a4bSNishka Dasgupta 			ret = -EINVAL;
29879937a4bSNishka Dasgupta 			goto err_node_put;
299f7fafd08SVincent Donnefort 		}
300f7fafd08SVincent Donnefort 
301f7fafd08SVincent Donnefort 		num_modes = ret / 3;
302a86854d0SKees Cook 		modval = devm_kcalloc(dev,
303a86854d0SKees Cook 				      num_modes,
304a86854d0SKees Cook 				      sizeof(struct ns2_led_modval),
305f7fafd08SVincent Donnefort 				      GFP_KERNEL);
30679937a4bSNishka Dasgupta 		if (!modval) {
30779937a4bSNishka Dasgupta 			ret = -ENOMEM;
30879937a4bSNishka Dasgupta 			goto err_node_put;
30979937a4bSNishka Dasgupta 		}
310f7fafd08SVincent Donnefort 
311f7fafd08SVincent Donnefort 		for (i = 0; i < num_modes; i++) {
312f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
313f7fafd08SVincent Donnefort 						"modes-map", 3 * i,
314f7fafd08SVincent Donnefort 						(u32 *) &modval[i].mode);
315f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
316f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 1,
317f7fafd08SVincent Donnefort 						(u32 *) &modval[i].cmd_level);
318f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
319f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 2,
320f7fafd08SVincent Donnefort 						(u32 *) &modval[i].slow_level);
321f7fafd08SVincent Donnefort 		}
322f7fafd08SVincent Donnefort 
323f7fafd08SVincent Donnefort 		led->num_modes = num_modes;
324f7fafd08SVincent Donnefort 		led->modval = modval;
325f7fafd08SVincent Donnefort 
326f7fafd08SVincent Donnefort 		led++;
32772052fccSSimon Guinot 	}
32872052fccSSimon Guinot 
32972052fccSSimon Guinot 	pdata->leds = leds;
33072052fccSSimon Guinot 	pdata->num_leds = num_leds;
33172052fccSSimon Guinot 
33272052fccSSimon Guinot 	return 0;
33379937a4bSNishka Dasgupta 
33479937a4bSNishka Dasgupta err_node_put:
33579937a4bSNishka Dasgupta 	of_node_put(child);
33679937a4bSNishka Dasgupta 	return ret;
33772052fccSSimon Guinot }
33872052fccSSimon Guinot 
33972052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = {
34072052fccSSimon Guinot 	{ .compatible = "lacie,ns2-leds", },
34172052fccSSimon Guinot 	{},
34272052fccSSimon Guinot };
34398f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
34472052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
34572052fccSSimon Guinot 
3463de1929bSSimon Guinot struct ns2_led_priv {
3473de1929bSSimon Guinot 	int num_leds;
3483de1929bSSimon Guinot 	struct ns2_led_data leds_data[];
3493de1929bSSimon Guinot };
3503de1929bSSimon Guinot 
35198ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev)
35211efe71fSSimon Guinot {
35387aae1eaSJingoo Han 	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
3543de1929bSSimon Guinot 	struct ns2_led_priv *priv;
35511efe71fSSimon Guinot 	int i;
35611efe71fSSimon Guinot 	int ret;
35711efe71fSSimon Guinot 
35872052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
35972052fccSSimon Guinot 	if (!pdata) {
36072052fccSSimon Guinot 		pdata = devm_kzalloc(&pdev->dev,
36172052fccSSimon Guinot 				     sizeof(struct ns2_led_platform_data),
36272052fccSSimon Guinot 				     GFP_KERNEL);
36372052fccSSimon Guinot 		if (!pdata)
36472052fccSSimon Guinot 			return -ENOMEM;
36572052fccSSimon Guinot 
36672052fccSSimon Guinot 		ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
36772052fccSSimon Guinot 		if (ret)
36872052fccSSimon Guinot 			return ret;
36972052fccSSimon Guinot 	}
37072052fccSSimon Guinot #else
37111efe71fSSimon Guinot 	if (!pdata)
37211efe71fSSimon Guinot 		return -EINVAL;
37372052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
37411efe71fSSimon Guinot 
375a7ad53cbSGustavo A. R. Silva 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds_data, pdata->num_leds), GFP_KERNEL);
3763de1929bSSimon Guinot 	if (!priv)
37711efe71fSSimon Guinot 		return -ENOMEM;
3783de1929bSSimon Guinot 	priv->num_leds = pdata->num_leds;
37911efe71fSSimon Guinot 
3803de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++) {
3813de1929bSSimon Guinot 		ret = create_ns2_led(pdev, &priv->leds_data[i],
3823de1929bSSimon Guinot 				     &pdata->leds[i]);
383a209f766SBryan Wu 		if (ret < 0) {
384a209f766SBryan Wu 			for (i = i - 1; i >= 0; i--)
3853de1929bSSimon Guinot 				delete_ns2_led(&priv->leds_data[i]);
386a209f766SBryan Wu 			return ret;
387a209f766SBryan Wu 		}
38811efe71fSSimon Guinot 	}
38911efe71fSSimon Guinot 
3903de1929bSSimon Guinot 	platform_set_drvdata(pdev, priv);
39111efe71fSSimon Guinot 
39211efe71fSSimon Guinot 	return 0;
39311efe71fSSimon Guinot }
39411efe71fSSimon Guinot 
395678e8a6bSBill Pemberton static int ns2_led_remove(struct platform_device *pdev)
39611efe71fSSimon Guinot {
39711efe71fSSimon Guinot 	int i;
3983de1929bSSimon Guinot 	struct ns2_led_priv *priv;
39911efe71fSSimon Guinot 
4003de1929bSSimon Guinot 	priv = platform_get_drvdata(pdev);
40111efe71fSSimon Guinot 
4023de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++)
4033de1929bSSimon Guinot 		delete_ns2_led(&priv->leds_data[i]);
40411efe71fSSimon Guinot 
40511efe71fSSimon Guinot 	return 0;
40611efe71fSSimon Guinot }
40711efe71fSSimon Guinot 
40811efe71fSSimon Guinot static struct platform_driver ns2_led_driver = {
40911efe71fSSimon Guinot 	.probe		= ns2_led_probe,
410df07cf81SBill Pemberton 	.remove		= ns2_led_remove,
41111efe71fSSimon Guinot 	.driver		= {
41211efe71fSSimon Guinot 		.name		= "leds-ns2",
41372052fccSSimon Guinot 		.of_match_table	= of_match_ptr(of_ns2_leds_match),
41411efe71fSSimon Guinot 	},
41511efe71fSSimon Guinot };
41611efe71fSSimon Guinot 
417892a8843SAxel Lin module_platform_driver(ns2_led_driver);
41811efe71fSSimon Guinot 
41911efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
42011efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver");
42111efe71fSSimon Guinot MODULE_LICENSE("GPL");
422892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2");
423