xref: /openbmc/linux/drivers/leds/leds-ns2.c (revision c7896490dd1a4e6b346e8a475e4a433356362770)
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>
15*c7896490SLinus 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 
21*c7896490SLinus Walleij enum ns2_led_modes {
22*c7896490SLinus Walleij 	NS_V2_LED_OFF,
23*c7896490SLinus Walleij 	NS_V2_LED_ON,
24*c7896490SLinus Walleij 	NS_V2_LED_SATA,
25*c7896490SLinus Walleij };
26*c7896490SLinus Walleij 
27*c7896490SLinus Walleij struct ns2_led_modval {
28*c7896490SLinus Walleij 	enum ns2_led_modes	mode;
29*c7896490SLinus Walleij 	int			cmd_level;
30*c7896490SLinus Walleij 	int			slow_level;
31*c7896490SLinus Walleij };
32*c7896490SLinus Walleij 
33*c7896490SLinus Walleij struct ns2_led {
34*c7896490SLinus Walleij 	const char	*name;
35*c7896490SLinus Walleij 	const char	*default_trigger;
36*c7896490SLinus Walleij 	unsigned	cmd;
37*c7896490SLinus Walleij 	unsigned	slow;
38*c7896490SLinus Walleij 	int		num_modes;
39*c7896490SLinus Walleij 	struct ns2_led_modval *modval;
40*c7896490SLinus Walleij };
41*c7896490SLinus Walleij 
42*c7896490SLinus Walleij struct ns2_led_platform_data {
43*c7896490SLinus Walleij 	int		num_leds;
44*c7896490SLinus Walleij 	struct ns2_led	*leds;
45*c7896490SLinus Walleij };
46*c7896490SLinus 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;
562224f2ffSKitone Elvis Peter 	unsigned int		cmd;
572224f2ffSKitone Elvis Peter 	unsigned int		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 
734b90432dSSimon Guinot 	cmd_level = gpio_get_value_cansleep(led_dat->cmd);
744b90432dSSimon Guinot 	slow_level = gpio_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) {
10711efe71fSSimon Guinot 		gpio_set_value(led_dat->cmd,
108f7fafd08SVincent Donnefort 			       led_dat->modval[i].cmd_level);
10911efe71fSSimon Guinot 		gpio_set_value(led_dat->slow,
110f7fafd08SVincent Donnefort 			       led_dat->modval[i].slow_level);
1114b90432dSSimon Guinot 		goto exit_unlock;
11211efe71fSSimon Guinot 	}
11311efe71fSSimon Guinot 
114c29e650bSJacek Anaszewski 	gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
115c29e650bSJacek Anaszewski 	gpio_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 
20304195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->cmd,
2044b90432dSSimon Guinot 			gpio_get_value_cansleep(template->cmd) ?
2059d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
20631c3dc74SJingoo Han 			template->name);
20711efe71fSSimon Guinot 	if (ret) {
20811efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
20911efe71fSSimon Guinot 			template->name);
21031c3dc74SJingoo Han 		return ret;
21111efe71fSSimon Guinot 	}
21211efe71fSSimon Guinot 
21304195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->slow,
2144b90432dSSimon Guinot 			gpio_get_value_cansleep(template->slow) ?
2159d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
21631c3dc74SJingoo Han 			template->name);
21711efe71fSSimon Guinot 	if (ret) {
21811efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
21911efe71fSSimon Guinot 			template->name);
22004195823SSachin Kamat 		return ret;
22111efe71fSSimon Guinot 	}
22211efe71fSSimon Guinot 
22311efe71fSSimon Guinot 	rwlock_init(&led_dat->rw_lock);
22411efe71fSSimon Guinot 
22511efe71fSSimon Guinot 	led_dat->cdev.name = template->name;
22611efe71fSSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
22711efe71fSSimon Guinot 	led_dat->cdev.blink_set = NULL;
22811efe71fSSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
229475f8548SJohan Hovold 	led_dat->cdev.groups = ns2_led_groups;
23011efe71fSSimon Guinot 	led_dat->cmd = template->cmd;
23111efe71fSSimon Guinot 	led_dat->slow = template->slow;
2324b90432dSSimon Guinot 	led_dat->can_sleep = gpio_cansleep(led_dat->cmd) |
2334b90432dSSimon Guinot 				gpio_cansleep(led_dat->slow);
234c29e650bSJacek Anaszewski 	if (led_dat->can_sleep)
235c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking;
236c29e650bSJacek Anaszewski 	else
237c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set = ns2_led_set;
238f7fafd08SVincent Donnefort 	led_dat->modval = template->modval;
239f7fafd08SVincent Donnefort 	led_dat->num_modes = template->num_modes;
24011efe71fSSimon Guinot 
24111efe71fSSimon Guinot 	ret = ns2_led_get_mode(led_dat, &mode);
24211efe71fSSimon Guinot 	if (ret < 0)
24304195823SSachin Kamat 		return ret;
24411efe71fSSimon Guinot 
24511efe71fSSimon Guinot 	/* Set LED initial state. */
24611efe71fSSimon Guinot 	led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
24711efe71fSSimon Guinot 	led_dat->cdev.brightness =
24811efe71fSSimon Guinot 		(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
24911efe71fSSimon Guinot 
25011efe71fSSimon Guinot 	ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
25111efe71fSSimon Guinot 	if (ret < 0)
25204195823SSachin Kamat 		return ret;
25311efe71fSSimon Guinot 
25411efe71fSSimon Guinot 	return 0;
25511efe71fSSimon Guinot }
25611efe71fSSimon Guinot 
257b8cd742aSArnd Bergmann static void delete_ns2_led(struct ns2_led_data *led_dat)
25811efe71fSSimon Guinot {
25911efe71fSSimon Guinot 	led_classdev_unregister(&led_dat->cdev);
26011efe71fSSimon Guinot }
26111efe71fSSimon Guinot 
26272052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
26372052fccSSimon Guinot /*
26472052fccSSimon Guinot  * Translate OpenFirmware node properties into platform_data.
26572052fccSSimon Guinot  */
266cf4af012SLinus Torvalds static int
26772052fccSSimon Guinot ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
26872052fccSSimon Guinot {
26972052fccSSimon Guinot 	struct device_node *np = dev->of_node;
27072052fccSSimon Guinot 	struct device_node *child;
271f7fafd08SVincent Donnefort 	struct ns2_led *led, *leds;
27279937a4bSNishka Dasgupta 	int ret, num_leds = 0;
27372052fccSSimon Guinot 
27472052fccSSimon Guinot 	num_leds = of_get_child_count(np);
27572052fccSSimon Guinot 	if (!num_leds)
27672052fccSSimon Guinot 		return -ENODEV;
27772052fccSSimon Guinot 
278a86854d0SKees Cook 	leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led),
27972052fccSSimon Guinot 			    GFP_KERNEL);
28072052fccSSimon Guinot 	if (!leds)
28172052fccSSimon Guinot 		return -ENOMEM;
28272052fccSSimon Guinot 
283f7fafd08SVincent Donnefort 	led = leds;
28472052fccSSimon Guinot 	for_each_child_of_node(np, child) {
28572052fccSSimon Guinot 		const char *string;
28679937a4bSNishka Dasgupta 		int i, num_modes;
287f7fafd08SVincent Donnefort 		struct ns2_led_modval *modval;
28872052fccSSimon Guinot 
28972052fccSSimon Guinot 		ret = of_get_named_gpio(child, "cmd-gpio", 0);
29072052fccSSimon Guinot 		if (ret < 0)
29179937a4bSNishka Dasgupta 			goto err_node_put;
292f7fafd08SVincent Donnefort 		led->cmd = ret;
29372052fccSSimon Guinot 		ret = of_get_named_gpio(child, "slow-gpio", 0);
29472052fccSSimon Guinot 		if (ret < 0)
29579937a4bSNishka Dasgupta 			goto err_node_put;
296f7fafd08SVincent Donnefort 		led->slow = ret;
29772052fccSSimon Guinot 		ret = of_property_read_string(child, "label", &string);
298f7fafd08SVincent Donnefort 		led->name = (ret == 0) ? string : child->name;
29972052fccSSimon Guinot 		ret = of_property_read_string(child, "linux,default-trigger",
30072052fccSSimon Guinot 					      &string);
30172052fccSSimon Guinot 		if (ret == 0)
302f7fafd08SVincent Donnefort 			led->default_trigger = string;
30372052fccSSimon Guinot 
304f7fafd08SVincent Donnefort 		ret = of_property_count_u32_elems(child, "modes-map");
305f7fafd08SVincent Donnefort 		if (ret < 0 || ret % 3) {
306f7fafd08SVincent Donnefort 			dev_err(dev,
307f7fafd08SVincent Donnefort 				"Missing or malformed modes-map property\n");
30879937a4bSNishka Dasgupta 			ret = -EINVAL;
30979937a4bSNishka Dasgupta 			goto err_node_put;
310f7fafd08SVincent Donnefort 		}
311f7fafd08SVincent Donnefort 
312f7fafd08SVincent Donnefort 		num_modes = ret / 3;
313a86854d0SKees Cook 		modval = devm_kcalloc(dev,
314a86854d0SKees Cook 				      num_modes,
315a86854d0SKees Cook 				      sizeof(struct ns2_led_modval),
316f7fafd08SVincent Donnefort 				      GFP_KERNEL);
31779937a4bSNishka Dasgupta 		if (!modval) {
31879937a4bSNishka Dasgupta 			ret = -ENOMEM;
31979937a4bSNishka Dasgupta 			goto err_node_put;
32079937a4bSNishka Dasgupta 		}
321f7fafd08SVincent Donnefort 
322f7fafd08SVincent Donnefort 		for (i = 0; i < num_modes; i++) {
323f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
324f7fafd08SVincent Donnefort 						"modes-map", 3 * i,
325f7fafd08SVincent Donnefort 						(u32 *) &modval[i].mode);
326f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
327f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 1,
328f7fafd08SVincent Donnefort 						(u32 *) &modval[i].cmd_level);
329f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
330f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 2,
331f7fafd08SVincent Donnefort 						(u32 *) &modval[i].slow_level);
332f7fafd08SVincent Donnefort 		}
333f7fafd08SVincent Donnefort 
334f7fafd08SVincent Donnefort 		led->num_modes = num_modes;
335f7fafd08SVincent Donnefort 		led->modval = modval;
336f7fafd08SVincent Donnefort 
337f7fafd08SVincent Donnefort 		led++;
33872052fccSSimon Guinot 	}
33972052fccSSimon Guinot 
34072052fccSSimon Guinot 	pdata->leds = leds;
34172052fccSSimon Guinot 	pdata->num_leds = num_leds;
34272052fccSSimon Guinot 
34372052fccSSimon Guinot 	return 0;
34479937a4bSNishka Dasgupta 
34579937a4bSNishka Dasgupta err_node_put:
34679937a4bSNishka Dasgupta 	of_node_put(child);
34779937a4bSNishka Dasgupta 	return ret;
34872052fccSSimon Guinot }
34972052fccSSimon Guinot 
35072052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = {
35172052fccSSimon Guinot 	{ .compatible = "lacie,ns2-leds", },
35272052fccSSimon Guinot 	{},
35372052fccSSimon Guinot };
35498f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
35572052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
35672052fccSSimon Guinot 
3573de1929bSSimon Guinot struct ns2_led_priv {
3583de1929bSSimon Guinot 	int num_leds;
3593de1929bSSimon Guinot 	struct ns2_led_data leds_data[];
3603de1929bSSimon Guinot };
3613de1929bSSimon Guinot 
3623de1929bSSimon Guinot static inline int sizeof_ns2_led_priv(int num_leds)
3633de1929bSSimon Guinot {
3643de1929bSSimon Guinot 	return sizeof(struct ns2_led_priv) +
3653de1929bSSimon Guinot 		      (sizeof(struct ns2_led_data) * num_leds);
3663de1929bSSimon Guinot }
3673de1929bSSimon Guinot 
36898ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev)
36911efe71fSSimon Guinot {
37087aae1eaSJingoo Han 	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
3713de1929bSSimon Guinot 	struct ns2_led_priv *priv;
37211efe71fSSimon Guinot 	int i;
37311efe71fSSimon Guinot 	int ret;
37411efe71fSSimon Guinot 
37572052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
37672052fccSSimon Guinot 	if (!pdata) {
37772052fccSSimon Guinot 		pdata = devm_kzalloc(&pdev->dev,
37872052fccSSimon Guinot 				     sizeof(struct ns2_led_platform_data),
37972052fccSSimon Guinot 				     GFP_KERNEL);
38072052fccSSimon Guinot 		if (!pdata)
38172052fccSSimon Guinot 			return -ENOMEM;
38272052fccSSimon Guinot 
38372052fccSSimon Guinot 		ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
38472052fccSSimon Guinot 		if (ret)
38572052fccSSimon Guinot 			return ret;
38672052fccSSimon Guinot 	}
38772052fccSSimon Guinot #else
38811efe71fSSimon Guinot 	if (!pdata)
38911efe71fSSimon Guinot 		return -EINVAL;
39072052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
39111efe71fSSimon Guinot 
3923de1929bSSimon Guinot 	priv = devm_kzalloc(&pdev->dev,
3933de1929bSSimon Guinot 			    sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
3943de1929bSSimon Guinot 	if (!priv)
39511efe71fSSimon Guinot 		return -ENOMEM;
3963de1929bSSimon Guinot 	priv->num_leds = pdata->num_leds;
39711efe71fSSimon Guinot 
3983de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++) {
3993de1929bSSimon Guinot 		ret = create_ns2_led(pdev, &priv->leds_data[i],
4003de1929bSSimon Guinot 				     &pdata->leds[i]);
401a209f766SBryan Wu 		if (ret < 0) {
402a209f766SBryan Wu 			for (i = i - 1; i >= 0; i--)
4033de1929bSSimon Guinot 				delete_ns2_led(&priv->leds_data[i]);
404a209f766SBryan Wu 			return ret;
405a209f766SBryan Wu 		}
40611efe71fSSimon Guinot 	}
40711efe71fSSimon Guinot 
4083de1929bSSimon Guinot 	platform_set_drvdata(pdev, priv);
40911efe71fSSimon Guinot 
41011efe71fSSimon Guinot 	return 0;
41111efe71fSSimon Guinot }
41211efe71fSSimon Guinot 
413678e8a6bSBill Pemberton static int ns2_led_remove(struct platform_device *pdev)
41411efe71fSSimon Guinot {
41511efe71fSSimon Guinot 	int i;
4163de1929bSSimon Guinot 	struct ns2_led_priv *priv;
41711efe71fSSimon Guinot 
4183de1929bSSimon Guinot 	priv = platform_get_drvdata(pdev);
41911efe71fSSimon Guinot 
4203de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++)
4213de1929bSSimon Guinot 		delete_ns2_led(&priv->leds_data[i]);
42211efe71fSSimon Guinot 
42311efe71fSSimon Guinot 	return 0;
42411efe71fSSimon Guinot }
42511efe71fSSimon Guinot 
42611efe71fSSimon Guinot static struct platform_driver ns2_led_driver = {
42711efe71fSSimon Guinot 	.probe		= ns2_led_probe,
428df07cf81SBill Pemberton 	.remove		= ns2_led_remove,
42911efe71fSSimon Guinot 	.driver		= {
43011efe71fSSimon Guinot 		.name		= "leds-ns2",
43172052fccSSimon Guinot 		.of_match_table	= of_match_ptr(of_ns2_leds_match),
43211efe71fSSimon Guinot 	},
43311efe71fSSimon Guinot };
43411efe71fSSimon Guinot 
435892a8843SAxel Lin module_platform_driver(ns2_led_driver);
43611efe71fSSimon Guinot 
43711efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
43811efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver");
43911efe71fSSimon Guinot MODULE_LICENSE("GPL");
440892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2");
441