xref: /openbmc/linux/drivers/leds/leds-ns2.c (revision a86854d0)
111efe71fSSimon Guinot /*
211efe71fSSimon Guinot  * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
311efe71fSSimon Guinot  *
411efe71fSSimon Guinot  * Copyright (C) 2010 LaCie
511efe71fSSimon Guinot  *
611efe71fSSimon Guinot  * Author: Simon Guinot <sguinot@lacie.com>
711efe71fSSimon Guinot  *
811efe71fSSimon Guinot  * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
911efe71fSSimon Guinot  *
1011efe71fSSimon Guinot  * This program is free software; you can redistribute it and/or modify
1111efe71fSSimon Guinot  * it under the terms of the GNU General Public License as published by
1211efe71fSSimon Guinot  * the Free Software Foundation; either version 2 of the License, or
1311efe71fSSimon Guinot  * (at your option) any later version.
1411efe71fSSimon Guinot  *
1511efe71fSSimon Guinot  * This program is distributed in the hope that it will be useful,
1611efe71fSSimon Guinot  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1711efe71fSSimon Guinot  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1811efe71fSSimon Guinot  * GNU General Public License for more details.
1911efe71fSSimon Guinot  *
2011efe71fSSimon Guinot  * You should have received a copy of the GNU General Public License
2111efe71fSSimon Guinot  * along with this program; if not, write to the Free Software
2211efe71fSSimon Guinot  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2311efe71fSSimon Guinot  */
2411efe71fSSimon Guinot 
2511efe71fSSimon Guinot #include <linux/kernel.h>
2611efe71fSSimon Guinot #include <linux/platform_device.h>
2711efe71fSSimon Guinot #include <linux/slab.h>
2811efe71fSSimon Guinot #include <linux/gpio.h>
2911efe71fSSimon Guinot #include <linux/leds.h>
3054f4dedbSPaul Gortmaker #include <linux/module.h>
31c02cecb9SArnd Bergmann #include <linux/platform_data/leds-kirkwood-ns2.h>
32c68f46ddSSachin Kamat #include <linux/of.h>
3372052fccSSimon Guinot #include <linux/of_gpio.h>
344b90432dSSimon Guinot #include "leds.h"
3511efe71fSSimon Guinot 
3611efe71fSSimon Guinot /*
37f7fafd08SVincent Donnefort  * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
38f7fafd08SVincent Donnefort  * modes are available: off, on and SATA activity blinking. The LED modes are
39f7fafd08SVincent Donnefort  * controlled through two GPIOs (command and slow): each combination of values
40f7fafd08SVincent Donnefort  * for the command/slow GPIOs corresponds to a LED mode.
4111efe71fSSimon Guinot  */
4211efe71fSSimon Guinot 
4311efe71fSSimon Guinot struct ns2_led_data {
4411efe71fSSimon Guinot 	struct led_classdev	cdev;
4511efe71fSSimon Guinot 	unsigned		cmd;
4611efe71fSSimon Guinot 	unsigned		slow;
474b90432dSSimon Guinot 	bool			can_sleep;
4811efe71fSSimon Guinot 	unsigned char		sata; /* True when SATA mode active. */
4911efe71fSSimon Guinot 	rwlock_t		rw_lock; /* Lock GPIOs. */
50f7fafd08SVincent Donnefort 	int			num_modes;
51f7fafd08SVincent Donnefort 	struct ns2_led_modval	*modval;
5211efe71fSSimon Guinot };
5311efe71fSSimon Guinot 
5411efe71fSSimon Guinot static int ns2_led_get_mode(struct ns2_led_data *led_dat,
5511efe71fSSimon Guinot 			    enum ns2_led_modes *mode)
5611efe71fSSimon Guinot {
5711efe71fSSimon Guinot 	int i;
5811efe71fSSimon Guinot 	int ret = -EINVAL;
5911efe71fSSimon Guinot 	int cmd_level;
6011efe71fSSimon Guinot 	int slow_level;
6111efe71fSSimon Guinot 
624b90432dSSimon Guinot 	cmd_level = gpio_get_value_cansleep(led_dat->cmd);
634b90432dSSimon Guinot 	slow_level = gpio_get_value_cansleep(led_dat->slow);
6411efe71fSSimon Guinot 
65f7fafd08SVincent Donnefort 	for (i = 0; i < led_dat->num_modes; i++) {
66f7fafd08SVincent Donnefort 		if (cmd_level == led_dat->modval[i].cmd_level &&
67f7fafd08SVincent Donnefort 		    slow_level == led_dat->modval[i].slow_level) {
68f7fafd08SVincent Donnefort 			*mode = led_dat->modval[i].mode;
6911efe71fSSimon Guinot 			ret = 0;
7011efe71fSSimon Guinot 			break;
7111efe71fSSimon Guinot 		}
7211efe71fSSimon Guinot 	}
7311efe71fSSimon Guinot 
7411efe71fSSimon Guinot 	return ret;
7511efe71fSSimon Guinot }
7611efe71fSSimon Guinot 
7711efe71fSSimon Guinot static void ns2_led_set_mode(struct ns2_led_data *led_dat,
7811efe71fSSimon Guinot 			     enum ns2_led_modes mode)
7911efe71fSSimon Guinot {
8011efe71fSSimon Guinot 	int i;
814b90432dSSimon Guinot 	bool found = false;
82f539dfedSSimon Guinot 	unsigned long flags;
8311efe71fSSimon Guinot 
844b90432dSSimon Guinot 	for (i = 0; i < led_dat->num_modes; i++)
854b90432dSSimon Guinot 		if (mode == led_dat->modval[i].mode) {
864b90432dSSimon Guinot 			found = true;
874b90432dSSimon Guinot 			break;
884b90432dSSimon Guinot 		}
894b90432dSSimon Guinot 
904b90432dSSimon Guinot 	if (!found)
914b90432dSSimon Guinot 		return;
924b90432dSSimon Guinot 
93f539dfedSSimon Guinot 	write_lock_irqsave(&led_dat->rw_lock, flags);
9411efe71fSSimon Guinot 
954b90432dSSimon Guinot 	if (!led_dat->can_sleep) {
9611efe71fSSimon Guinot 		gpio_set_value(led_dat->cmd,
97f7fafd08SVincent Donnefort 			       led_dat->modval[i].cmd_level);
9811efe71fSSimon Guinot 		gpio_set_value(led_dat->slow,
99f7fafd08SVincent Donnefort 			       led_dat->modval[i].slow_level);
1004b90432dSSimon Guinot 		goto exit_unlock;
10111efe71fSSimon Guinot 	}
10211efe71fSSimon Guinot 
103c29e650bSJacek Anaszewski 	gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
104c29e650bSJacek Anaszewski 	gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
1054b90432dSSimon Guinot 
1064b90432dSSimon Guinot exit_unlock:
107f539dfedSSimon Guinot 	write_unlock_irqrestore(&led_dat->rw_lock, flags);
10811efe71fSSimon Guinot }
10911efe71fSSimon Guinot 
11011efe71fSSimon Guinot static void ns2_led_set(struct led_classdev *led_cdev,
11111efe71fSSimon Guinot 			enum led_brightness value)
11211efe71fSSimon Guinot {
11311efe71fSSimon Guinot 	struct ns2_led_data *led_dat =
11411efe71fSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
11511efe71fSSimon Guinot 	enum ns2_led_modes mode;
11611efe71fSSimon Guinot 
11711efe71fSSimon Guinot 	if (value == LED_OFF)
11811efe71fSSimon Guinot 		mode = NS_V2_LED_OFF;
11911efe71fSSimon Guinot 	else if (led_dat->sata)
12011efe71fSSimon Guinot 		mode = NS_V2_LED_SATA;
12111efe71fSSimon Guinot 	else
12211efe71fSSimon Guinot 		mode = NS_V2_LED_ON;
12311efe71fSSimon Guinot 
12411efe71fSSimon Guinot 	ns2_led_set_mode(led_dat, mode);
12511efe71fSSimon Guinot }
12611efe71fSSimon Guinot 
127c29e650bSJacek Anaszewski static int ns2_led_set_blocking(struct led_classdev *led_cdev,
128c29e650bSJacek Anaszewski 			enum led_brightness value)
129c29e650bSJacek Anaszewski {
130c29e650bSJacek Anaszewski 	ns2_led_set(led_cdev, value);
131c29e650bSJacek Anaszewski 	return 0;
132c29e650bSJacek Anaszewski }
133c29e650bSJacek Anaszewski 
13411efe71fSSimon Guinot static ssize_t ns2_led_sata_store(struct device *dev,
13511efe71fSSimon Guinot 				  struct device_attribute *attr,
13611efe71fSSimon Guinot 				  const char *buff, size_t count)
13711efe71fSSimon Guinot {
138e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
139e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
140e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
14111efe71fSSimon Guinot 	int ret;
14211efe71fSSimon Guinot 	unsigned long enable;
14311efe71fSSimon Guinot 
1443874350cSJingoo Han 	ret = kstrtoul(buff, 10, &enable);
14511efe71fSSimon Guinot 	if (ret < 0)
14611efe71fSSimon Guinot 		return ret;
14711efe71fSSimon Guinot 
14811efe71fSSimon Guinot 	enable = !!enable;
14911efe71fSSimon Guinot 
15011efe71fSSimon Guinot 	if (led_dat->sata == enable)
1514b90432dSSimon Guinot 		goto exit;
15211efe71fSSimon Guinot 
15311efe71fSSimon Guinot 	led_dat->sata = enable;
15411efe71fSSimon Guinot 
1554b90432dSSimon Guinot 	if (!led_get_brightness(led_cdev))
1564b90432dSSimon Guinot 		goto exit;
1574b90432dSSimon Guinot 
1584b90432dSSimon Guinot 	if (enable)
1594b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
1604b90432dSSimon Guinot 	else
1614b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_ON);
1624b90432dSSimon Guinot 
1634b90432dSSimon Guinot exit:
16411efe71fSSimon Guinot 	return count;
16511efe71fSSimon Guinot }
16611efe71fSSimon Guinot 
16711efe71fSSimon Guinot static ssize_t ns2_led_sata_show(struct device *dev,
16811efe71fSSimon Guinot 				 struct device_attribute *attr, char *buf)
16911efe71fSSimon Guinot {
170e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
171e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
172e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
17311efe71fSSimon Guinot 
17411efe71fSSimon Guinot 	return sprintf(buf, "%d\n", led_dat->sata);
17511efe71fSSimon Guinot }
17611efe71fSSimon Guinot 
17711efe71fSSimon Guinot static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
17811efe71fSSimon Guinot 
179475f8548SJohan Hovold static struct attribute *ns2_led_attrs[] = {
180475f8548SJohan Hovold 	&dev_attr_sata.attr,
181475f8548SJohan Hovold 	NULL
182475f8548SJohan Hovold };
183475f8548SJohan Hovold ATTRIBUTE_GROUPS(ns2_led);
184475f8548SJohan Hovold 
18598ea1ea2SBill Pemberton static int
18611efe71fSSimon Guinot create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
18711efe71fSSimon Guinot 	       const struct ns2_led *template)
18811efe71fSSimon Guinot {
18911efe71fSSimon Guinot 	int ret;
19011efe71fSSimon Guinot 	enum ns2_led_modes mode;
19111efe71fSSimon Guinot 
19204195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->cmd,
1934b90432dSSimon Guinot 			gpio_get_value_cansleep(template->cmd) ?
1949d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
19531c3dc74SJingoo Han 			template->name);
19611efe71fSSimon Guinot 	if (ret) {
19711efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
19811efe71fSSimon Guinot 			template->name);
19931c3dc74SJingoo Han 		return ret;
20011efe71fSSimon Guinot 	}
20111efe71fSSimon Guinot 
20204195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->slow,
2034b90432dSSimon Guinot 			gpio_get_value_cansleep(template->slow) ?
2049d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
20531c3dc74SJingoo Han 			template->name);
20611efe71fSSimon Guinot 	if (ret) {
20711efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
20811efe71fSSimon Guinot 			template->name);
20904195823SSachin Kamat 		return ret;
21011efe71fSSimon Guinot 	}
21111efe71fSSimon Guinot 
21211efe71fSSimon Guinot 	rwlock_init(&led_dat->rw_lock);
21311efe71fSSimon Guinot 
21411efe71fSSimon Guinot 	led_dat->cdev.name = template->name;
21511efe71fSSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
21611efe71fSSimon Guinot 	led_dat->cdev.blink_set = NULL;
21711efe71fSSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
218475f8548SJohan Hovold 	led_dat->cdev.groups = ns2_led_groups;
21911efe71fSSimon Guinot 	led_dat->cmd = template->cmd;
22011efe71fSSimon Guinot 	led_dat->slow = template->slow;
2214b90432dSSimon Guinot 	led_dat->can_sleep = gpio_cansleep(led_dat->cmd) |
2224b90432dSSimon Guinot 				gpio_cansleep(led_dat->slow);
223c29e650bSJacek Anaszewski 	if (led_dat->can_sleep)
224c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking;
225c29e650bSJacek Anaszewski 	else
226c29e650bSJacek Anaszewski 		led_dat->cdev.brightness_set = ns2_led_set;
227f7fafd08SVincent Donnefort 	led_dat->modval = template->modval;
228f7fafd08SVincent Donnefort 	led_dat->num_modes = template->num_modes;
22911efe71fSSimon Guinot 
23011efe71fSSimon Guinot 	ret = ns2_led_get_mode(led_dat, &mode);
23111efe71fSSimon Guinot 	if (ret < 0)
23204195823SSachin Kamat 		return ret;
23311efe71fSSimon Guinot 
23411efe71fSSimon Guinot 	/* Set LED initial state. */
23511efe71fSSimon Guinot 	led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
23611efe71fSSimon Guinot 	led_dat->cdev.brightness =
23711efe71fSSimon Guinot 		(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
23811efe71fSSimon Guinot 
23911efe71fSSimon Guinot 	ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
24011efe71fSSimon Guinot 	if (ret < 0)
24104195823SSachin Kamat 		return ret;
24211efe71fSSimon Guinot 
24311efe71fSSimon Guinot 	return 0;
24411efe71fSSimon Guinot }
24511efe71fSSimon Guinot 
246b8cd742aSArnd Bergmann static void delete_ns2_led(struct ns2_led_data *led_dat)
24711efe71fSSimon Guinot {
24811efe71fSSimon Guinot 	led_classdev_unregister(&led_dat->cdev);
24911efe71fSSimon Guinot }
25011efe71fSSimon Guinot 
25172052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
25272052fccSSimon Guinot /*
25372052fccSSimon Guinot  * Translate OpenFirmware node properties into platform_data.
25472052fccSSimon Guinot  */
255cf4af012SLinus Torvalds static int
25672052fccSSimon Guinot ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
25772052fccSSimon Guinot {
25872052fccSSimon Guinot 	struct device_node *np = dev->of_node;
25972052fccSSimon Guinot 	struct device_node *child;
260f7fafd08SVincent Donnefort 	struct ns2_led *led, *leds;
26172052fccSSimon Guinot 	int num_leds = 0;
26272052fccSSimon Guinot 
26372052fccSSimon Guinot 	num_leds = of_get_child_count(np);
26472052fccSSimon Guinot 	if (!num_leds)
26572052fccSSimon Guinot 		return -ENODEV;
26672052fccSSimon Guinot 
267a86854d0SKees Cook 	leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led),
26872052fccSSimon Guinot 			    GFP_KERNEL);
26972052fccSSimon Guinot 	if (!leds)
27072052fccSSimon Guinot 		return -ENOMEM;
27172052fccSSimon Guinot 
272f7fafd08SVincent Donnefort 	led = leds;
27372052fccSSimon Guinot 	for_each_child_of_node(np, child) {
27472052fccSSimon Guinot 		const char *string;
275f7fafd08SVincent Donnefort 		int ret, i, num_modes;
276f7fafd08SVincent Donnefort 		struct ns2_led_modval *modval;
27772052fccSSimon Guinot 
27872052fccSSimon Guinot 		ret = of_get_named_gpio(child, "cmd-gpio", 0);
27972052fccSSimon Guinot 		if (ret < 0)
28072052fccSSimon Guinot 			return ret;
281f7fafd08SVincent Donnefort 		led->cmd = ret;
28272052fccSSimon Guinot 		ret = of_get_named_gpio(child, "slow-gpio", 0);
28372052fccSSimon Guinot 		if (ret < 0)
28472052fccSSimon Guinot 			return ret;
285f7fafd08SVincent Donnefort 		led->slow = ret;
28672052fccSSimon Guinot 		ret = of_property_read_string(child, "label", &string);
287f7fafd08SVincent Donnefort 		led->name = (ret == 0) ? string : child->name;
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");
297f7fafd08SVincent Donnefort 			return -EINVAL;
298f7fafd08SVincent Donnefort 		}
299f7fafd08SVincent Donnefort 
300f7fafd08SVincent Donnefort 		num_modes = ret / 3;
301a86854d0SKees Cook 		modval = devm_kcalloc(dev,
302a86854d0SKees Cook 				      num_modes,
303a86854d0SKees Cook 				      sizeof(struct ns2_led_modval),
304f7fafd08SVincent Donnefort 				      GFP_KERNEL);
305f7fafd08SVincent Donnefort 		if (!modval)
306f7fafd08SVincent Donnefort 			return -ENOMEM;
307f7fafd08SVincent Donnefort 
308f7fafd08SVincent Donnefort 		for (i = 0; i < num_modes; i++) {
309f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
310f7fafd08SVincent Donnefort 						"modes-map", 3 * i,
311f7fafd08SVincent Donnefort 						(u32 *) &modval[i].mode);
312f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
313f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 1,
314f7fafd08SVincent Donnefort 						(u32 *) &modval[i].cmd_level);
315f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
316f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 2,
317f7fafd08SVincent Donnefort 						(u32 *) &modval[i].slow_level);
318f7fafd08SVincent Donnefort 		}
319f7fafd08SVincent Donnefort 
320f7fafd08SVincent Donnefort 		led->num_modes = num_modes;
321f7fafd08SVincent Donnefort 		led->modval = modval;
322f7fafd08SVincent Donnefort 
323f7fafd08SVincent Donnefort 		led++;
32472052fccSSimon Guinot 	}
32572052fccSSimon Guinot 
32672052fccSSimon Guinot 	pdata->leds = leds;
32772052fccSSimon Guinot 	pdata->num_leds = num_leds;
32872052fccSSimon Guinot 
32972052fccSSimon Guinot 	return 0;
33072052fccSSimon Guinot }
33172052fccSSimon Guinot 
33272052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = {
33372052fccSSimon Guinot 	{ .compatible = "lacie,ns2-leds", },
33472052fccSSimon Guinot 	{},
33572052fccSSimon Guinot };
33698f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
33772052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
33872052fccSSimon Guinot 
3393de1929bSSimon Guinot struct ns2_led_priv {
3403de1929bSSimon Guinot 	int num_leds;
3413de1929bSSimon Guinot 	struct ns2_led_data leds_data[];
3423de1929bSSimon Guinot };
3433de1929bSSimon Guinot 
3443de1929bSSimon Guinot static inline int sizeof_ns2_led_priv(int num_leds)
3453de1929bSSimon Guinot {
3463de1929bSSimon Guinot 	return sizeof(struct ns2_led_priv) +
3473de1929bSSimon Guinot 		      (sizeof(struct ns2_led_data) * num_leds);
3483de1929bSSimon Guinot }
3493de1929bSSimon Guinot 
35098ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev)
35111efe71fSSimon Guinot {
35287aae1eaSJingoo Han 	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
3533de1929bSSimon Guinot 	struct ns2_led_priv *priv;
35411efe71fSSimon Guinot 	int i;
35511efe71fSSimon Guinot 	int ret;
35611efe71fSSimon Guinot 
35772052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
35872052fccSSimon Guinot 	if (!pdata) {
35972052fccSSimon Guinot 		pdata = devm_kzalloc(&pdev->dev,
36072052fccSSimon Guinot 				     sizeof(struct ns2_led_platform_data),
36172052fccSSimon Guinot 				     GFP_KERNEL);
36272052fccSSimon Guinot 		if (!pdata)
36372052fccSSimon Guinot 			return -ENOMEM;
36472052fccSSimon Guinot 
36572052fccSSimon Guinot 		ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
36672052fccSSimon Guinot 		if (ret)
36772052fccSSimon Guinot 			return ret;
36872052fccSSimon Guinot 	}
36972052fccSSimon Guinot #else
37011efe71fSSimon Guinot 	if (!pdata)
37111efe71fSSimon Guinot 		return -EINVAL;
37272052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
37311efe71fSSimon Guinot 
3743de1929bSSimon Guinot 	priv = devm_kzalloc(&pdev->dev,
3753de1929bSSimon Guinot 			    sizeof_ns2_led_priv(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