xref: /openbmc/linux/drivers/leds/leds-ns2.c (revision 98f9cc7f)
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;
484b90432dSSimon Guinot 	int			mode_index;
4911efe71fSSimon Guinot 	unsigned char		sata; /* True when SATA mode active. */
5011efe71fSSimon Guinot 	rwlock_t		rw_lock; /* Lock GPIOs. */
514b90432dSSimon Guinot 	struct work_struct	work;
52f7fafd08SVincent Donnefort 	int			num_modes;
53f7fafd08SVincent Donnefort 	struct ns2_led_modval	*modval;
5411efe71fSSimon Guinot };
5511efe71fSSimon Guinot 
564b90432dSSimon Guinot static void ns2_led_work(struct work_struct *work)
574b90432dSSimon Guinot {
584b90432dSSimon Guinot 	struct ns2_led_data *led_dat =
594b90432dSSimon Guinot 		container_of(work, struct ns2_led_data, work);
604b90432dSSimon Guinot 	int i = led_dat->mode_index;
614b90432dSSimon Guinot 
624b90432dSSimon Guinot 	gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
634b90432dSSimon Guinot 	gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
644b90432dSSimon Guinot }
654b90432dSSimon Guinot 
6611efe71fSSimon Guinot static int ns2_led_get_mode(struct ns2_led_data *led_dat,
6711efe71fSSimon Guinot 			    enum ns2_led_modes *mode)
6811efe71fSSimon Guinot {
6911efe71fSSimon Guinot 	int i;
7011efe71fSSimon Guinot 	int ret = -EINVAL;
7111efe71fSSimon Guinot 	int cmd_level;
7211efe71fSSimon Guinot 	int slow_level;
7311efe71fSSimon Guinot 
744b90432dSSimon Guinot 	cmd_level = gpio_get_value_cansleep(led_dat->cmd);
754b90432dSSimon Guinot 	slow_level = gpio_get_value_cansleep(led_dat->slow);
7611efe71fSSimon Guinot 
77f7fafd08SVincent Donnefort 	for (i = 0; i < led_dat->num_modes; i++) {
78f7fafd08SVincent Donnefort 		if (cmd_level == led_dat->modval[i].cmd_level &&
79f7fafd08SVincent Donnefort 		    slow_level == led_dat->modval[i].slow_level) {
80f7fafd08SVincent Donnefort 			*mode = led_dat->modval[i].mode;
8111efe71fSSimon Guinot 			ret = 0;
8211efe71fSSimon Guinot 			break;
8311efe71fSSimon Guinot 		}
8411efe71fSSimon Guinot 	}
8511efe71fSSimon Guinot 
8611efe71fSSimon Guinot 	return ret;
8711efe71fSSimon Guinot }
8811efe71fSSimon Guinot 
8911efe71fSSimon Guinot static void ns2_led_set_mode(struct ns2_led_data *led_dat,
9011efe71fSSimon Guinot 			     enum ns2_led_modes mode)
9111efe71fSSimon Guinot {
9211efe71fSSimon Guinot 	int i;
934b90432dSSimon Guinot 	bool found = false;
94f539dfedSSimon Guinot 	unsigned long flags;
9511efe71fSSimon Guinot 
964b90432dSSimon Guinot 	for (i = 0; i < led_dat->num_modes; i++)
974b90432dSSimon Guinot 		if (mode == led_dat->modval[i].mode) {
984b90432dSSimon Guinot 			found = true;
994b90432dSSimon Guinot 			break;
1004b90432dSSimon Guinot 		}
1014b90432dSSimon Guinot 
1024b90432dSSimon Guinot 	if (!found)
1034b90432dSSimon Guinot 		return;
1044b90432dSSimon Guinot 
105f539dfedSSimon Guinot 	write_lock_irqsave(&led_dat->rw_lock, flags);
10611efe71fSSimon Guinot 
1074b90432dSSimon Guinot 	if (!led_dat->can_sleep) {
10811efe71fSSimon Guinot 		gpio_set_value(led_dat->cmd,
109f7fafd08SVincent Donnefort 			       led_dat->modval[i].cmd_level);
11011efe71fSSimon Guinot 		gpio_set_value(led_dat->slow,
111f7fafd08SVincent Donnefort 			       led_dat->modval[i].slow_level);
1124b90432dSSimon Guinot 		goto exit_unlock;
11311efe71fSSimon Guinot 	}
11411efe71fSSimon Guinot 
1154b90432dSSimon Guinot 	led_dat->mode_index = i;
1164b90432dSSimon Guinot 	schedule_work(&led_dat->work);
1174b90432dSSimon Guinot 
1184b90432dSSimon Guinot exit_unlock:
119f539dfedSSimon Guinot 	write_unlock_irqrestore(&led_dat->rw_lock, flags);
12011efe71fSSimon Guinot }
12111efe71fSSimon Guinot 
12211efe71fSSimon Guinot static void ns2_led_set(struct led_classdev *led_cdev,
12311efe71fSSimon Guinot 			enum led_brightness value)
12411efe71fSSimon Guinot {
12511efe71fSSimon Guinot 	struct ns2_led_data *led_dat =
12611efe71fSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
12711efe71fSSimon Guinot 	enum ns2_led_modes mode;
12811efe71fSSimon Guinot 
12911efe71fSSimon Guinot 	if (value == LED_OFF)
13011efe71fSSimon Guinot 		mode = NS_V2_LED_OFF;
13111efe71fSSimon Guinot 	else if (led_dat->sata)
13211efe71fSSimon Guinot 		mode = NS_V2_LED_SATA;
13311efe71fSSimon Guinot 	else
13411efe71fSSimon Guinot 		mode = NS_V2_LED_ON;
13511efe71fSSimon Guinot 
13611efe71fSSimon Guinot 	ns2_led_set_mode(led_dat, mode);
13711efe71fSSimon Guinot }
13811efe71fSSimon Guinot 
13911efe71fSSimon Guinot static ssize_t ns2_led_sata_store(struct device *dev,
14011efe71fSSimon Guinot 				  struct device_attribute *attr,
14111efe71fSSimon Guinot 				  const char *buff, size_t count)
14211efe71fSSimon Guinot {
143e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
144e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
145e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
14611efe71fSSimon Guinot 	int ret;
14711efe71fSSimon Guinot 	unsigned long enable;
14811efe71fSSimon Guinot 
1493874350cSJingoo Han 	ret = kstrtoul(buff, 10, &enable);
15011efe71fSSimon Guinot 	if (ret < 0)
15111efe71fSSimon Guinot 		return ret;
15211efe71fSSimon Guinot 
15311efe71fSSimon Guinot 	enable = !!enable;
15411efe71fSSimon Guinot 
15511efe71fSSimon Guinot 	if (led_dat->sata == enable)
1564b90432dSSimon Guinot 		goto exit;
15711efe71fSSimon Guinot 
15811efe71fSSimon Guinot 	led_dat->sata = enable;
15911efe71fSSimon Guinot 
1604b90432dSSimon Guinot 	if (!led_get_brightness(led_cdev))
1614b90432dSSimon Guinot 		goto exit;
1624b90432dSSimon Guinot 
1634b90432dSSimon Guinot 	if (enable)
1644b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
1654b90432dSSimon Guinot 	else
1664b90432dSSimon Guinot 		ns2_led_set_mode(led_dat, NS_V2_LED_ON);
1674b90432dSSimon Guinot 
1684b90432dSSimon Guinot exit:
16911efe71fSSimon Guinot 	return count;
17011efe71fSSimon Guinot }
17111efe71fSSimon Guinot 
17211efe71fSSimon Guinot static ssize_t ns2_led_sata_show(struct device *dev,
17311efe71fSSimon Guinot 				 struct device_attribute *attr, char *buf)
17411efe71fSSimon Guinot {
175e5971bbcSSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
176e5971bbcSSimon Guinot 	struct ns2_led_data *led_dat =
177e5971bbcSSimon Guinot 		container_of(led_cdev, struct ns2_led_data, cdev);
17811efe71fSSimon Guinot 
17911efe71fSSimon Guinot 	return sprintf(buf, "%d\n", led_dat->sata);
18011efe71fSSimon Guinot }
18111efe71fSSimon Guinot 
18211efe71fSSimon Guinot static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
18311efe71fSSimon Guinot 
184475f8548SJohan Hovold static struct attribute *ns2_led_attrs[] = {
185475f8548SJohan Hovold 	&dev_attr_sata.attr,
186475f8548SJohan Hovold 	NULL
187475f8548SJohan Hovold };
188475f8548SJohan Hovold ATTRIBUTE_GROUPS(ns2_led);
189475f8548SJohan Hovold 
19098ea1ea2SBill Pemberton static int
19111efe71fSSimon Guinot create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
19211efe71fSSimon Guinot 	       const struct ns2_led *template)
19311efe71fSSimon Guinot {
19411efe71fSSimon Guinot 	int ret;
19511efe71fSSimon Guinot 	enum ns2_led_modes mode;
19611efe71fSSimon Guinot 
19704195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->cmd,
1984b90432dSSimon Guinot 			gpio_get_value_cansleep(template->cmd) ?
1999d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
20031c3dc74SJingoo Han 			template->name);
20111efe71fSSimon Guinot 	if (ret) {
20211efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
20311efe71fSSimon Guinot 			template->name);
20431c3dc74SJingoo Han 		return ret;
20511efe71fSSimon Guinot 	}
20611efe71fSSimon Guinot 
20704195823SSachin Kamat 	ret = devm_gpio_request_one(&pdev->dev, template->slow,
2084b90432dSSimon Guinot 			gpio_get_value_cansleep(template->slow) ?
2099d04cbaaSJingoo Han 			GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
21031c3dc74SJingoo Han 			template->name);
21111efe71fSSimon Guinot 	if (ret) {
21211efe71fSSimon Guinot 		dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
21311efe71fSSimon Guinot 			template->name);
21404195823SSachin Kamat 		return ret;
21511efe71fSSimon Guinot 	}
21611efe71fSSimon Guinot 
21711efe71fSSimon Guinot 	rwlock_init(&led_dat->rw_lock);
21811efe71fSSimon Guinot 
21911efe71fSSimon Guinot 	led_dat->cdev.name = template->name;
22011efe71fSSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
22111efe71fSSimon Guinot 	led_dat->cdev.blink_set = NULL;
22211efe71fSSimon Guinot 	led_dat->cdev.brightness_set = ns2_led_set;
22311efe71fSSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
224475f8548SJohan Hovold 	led_dat->cdev.groups = ns2_led_groups;
22511efe71fSSimon Guinot 	led_dat->cmd = template->cmd;
22611efe71fSSimon Guinot 	led_dat->slow = template->slow;
2274b90432dSSimon Guinot 	led_dat->can_sleep = gpio_cansleep(led_dat->cmd) |
2284b90432dSSimon Guinot 				gpio_cansleep(led_dat->slow);
229f7fafd08SVincent Donnefort 	led_dat->modval = template->modval;
230f7fafd08SVincent Donnefort 	led_dat->num_modes = template->num_modes;
23111efe71fSSimon Guinot 
23211efe71fSSimon Guinot 	ret = ns2_led_get_mode(led_dat, &mode);
23311efe71fSSimon Guinot 	if (ret < 0)
23404195823SSachin Kamat 		return ret;
23511efe71fSSimon Guinot 
23611efe71fSSimon Guinot 	/* Set LED initial state. */
23711efe71fSSimon Guinot 	led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
23811efe71fSSimon Guinot 	led_dat->cdev.brightness =
23911efe71fSSimon Guinot 		(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
24011efe71fSSimon Guinot 
2414b90432dSSimon Guinot 	INIT_WORK(&led_dat->work, ns2_led_work);
2424b90432dSSimon Guinot 
24311efe71fSSimon Guinot 	ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
24411efe71fSSimon Guinot 	if (ret < 0)
24504195823SSachin Kamat 		return ret;
24611efe71fSSimon Guinot 
24711efe71fSSimon Guinot 	return 0;
24811efe71fSSimon Guinot }
24911efe71fSSimon Guinot 
250b8cd742aSArnd Bergmann static void delete_ns2_led(struct ns2_led_data *led_dat)
25111efe71fSSimon Guinot {
25211efe71fSSimon Guinot 	led_classdev_unregister(&led_dat->cdev);
2534b90432dSSimon Guinot 	cancel_work_sync(&led_dat->work);
25411efe71fSSimon Guinot }
25511efe71fSSimon Guinot 
25672052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
25772052fccSSimon Guinot /*
25872052fccSSimon Guinot  * Translate OpenFirmware node properties into platform_data.
25972052fccSSimon Guinot  */
260cf4af012SLinus Torvalds static int
26172052fccSSimon Guinot ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
26272052fccSSimon Guinot {
26372052fccSSimon Guinot 	struct device_node *np = dev->of_node;
26472052fccSSimon Guinot 	struct device_node *child;
265f7fafd08SVincent Donnefort 	struct ns2_led *led, *leds;
26672052fccSSimon Guinot 	int num_leds = 0;
26772052fccSSimon Guinot 
26872052fccSSimon Guinot 	num_leds = of_get_child_count(np);
26972052fccSSimon Guinot 	if (!num_leds)
27072052fccSSimon Guinot 		return -ENODEV;
27172052fccSSimon Guinot 
27272052fccSSimon Guinot 	leds = devm_kzalloc(dev, num_leds * sizeof(struct ns2_led),
27372052fccSSimon Guinot 			    GFP_KERNEL);
27472052fccSSimon Guinot 	if (!leds)
27572052fccSSimon Guinot 		return -ENOMEM;
27672052fccSSimon Guinot 
277f7fafd08SVincent Donnefort 	led = leds;
27872052fccSSimon Guinot 	for_each_child_of_node(np, child) {
27972052fccSSimon Guinot 		const char *string;
280f7fafd08SVincent Donnefort 		int ret, i, num_modes;
281f7fafd08SVincent Donnefort 		struct ns2_led_modval *modval;
28272052fccSSimon Guinot 
28372052fccSSimon Guinot 		ret = of_get_named_gpio(child, "cmd-gpio", 0);
28472052fccSSimon Guinot 		if (ret < 0)
28572052fccSSimon Guinot 			return ret;
286f7fafd08SVincent Donnefort 		led->cmd = ret;
28772052fccSSimon Guinot 		ret = of_get_named_gpio(child, "slow-gpio", 0);
28872052fccSSimon Guinot 		if (ret < 0)
28972052fccSSimon Guinot 			return ret;
290f7fafd08SVincent Donnefort 		led->slow = ret;
29172052fccSSimon Guinot 		ret = of_property_read_string(child, "label", &string);
292f7fafd08SVincent Donnefort 		led->name = (ret == 0) ? string : child->name;
29372052fccSSimon Guinot 		ret = of_property_read_string(child, "linux,default-trigger",
29472052fccSSimon Guinot 					      &string);
29572052fccSSimon Guinot 		if (ret == 0)
296f7fafd08SVincent Donnefort 			led->default_trigger = string;
29772052fccSSimon Guinot 
298f7fafd08SVincent Donnefort 		ret = of_property_count_u32_elems(child, "modes-map");
299f7fafd08SVincent Donnefort 		if (ret < 0 || ret % 3) {
300f7fafd08SVincent Donnefort 			dev_err(dev,
301f7fafd08SVincent Donnefort 				"Missing or malformed modes-map property\n");
302f7fafd08SVincent Donnefort 			return -EINVAL;
303f7fafd08SVincent Donnefort 		}
304f7fafd08SVincent Donnefort 
305f7fafd08SVincent Donnefort 		num_modes = ret / 3;
306f7fafd08SVincent Donnefort 		modval = devm_kzalloc(dev,
307f7fafd08SVincent Donnefort 				      num_modes * sizeof(struct ns2_led_modval),
308f7fafd08SVincent Donnefort 				      GFP_KERNEL);
309f7fafd08SVincent Donnefort 		if (!modval)
310f7fafd08SVincent Donnefort 			return -ENOMEM;
311f7fafd08SVincent Donnefort 
312f7fafd08SVincent Donnefort 		for (i = 0; i < num_modes; i++) {
313f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
314f7fafd08SVincent Donnefort 						"modes-map", 3 * i,
315f7fafd08SVincent Donnefort 						(u32 *) &modval[i].mode);
316f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
317f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 1,
318f7fafd08SVincent Donnefort 						(u32 *) &modval[i].cmd_level);
319f7fafd08SVincent Donnefort 			of_property_read_u32_index(child,
320f7fafd08SVincent Donnefort 						"modes-map", 3 * i + 2,
321f7fafd08SVincent Donnefort 						(u32 *) &modval[i].slow_level);
322f7fafd08SVincent Donnefort 		}
323f7fafd08SVincent Donnefort 
324f7fafd08SVincent Donnefort 		led->num_modes = num_modes;
325f7fafd08SVincent Donnefort 		led->modval = modval;
326f7fafd08SVincent Donnefort 
327f7fafd08SVincent Donnefort 		led++;
32872052fccSSimon Guinot 	}
32972052fccSSimon Guinot 
33072052fccSSimon Guinot 	pdata->leds = leds;
33172052fccSSimon Guinot 	pdata->num_leds = num_leds;
33272052fccSSimon Guinot 
33372052fccSSimon Guinot 	return 0;
33472052fccSSimon Guinot }
33572052fccSSimon Guinot 
33672052fccSSimon Guinot static const struct of_device_id of_ns2_leds_match[] = {
33772052fccSSimon Guinot 	{ .compatible = "lacie,ns2-leds", },
33872052fccSSimon Guinot 	{},
33972052fccSSimon Guinot };
34098f9cc7fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
34172052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
34272052fccSSimon Guinot 
3433de1929bSSimon Guinot struct ns2_led_priv {
3443de1929bSSimon Guinot 	int num_leds;
3453de1929bSSimon Guinot 	struct ns2_led_data leds_data[];
3463de1929bSSimon Guinot };
3473de1929bSSimon Guinot 
3483de1929bSSimon Guinot static inline int sizeof_ns2_led_priv(int num_leds)
3493de1929bSSimon Guinot {
3503de1929bSSimon Guinot 	return sizeof(struct ns2_led_priv) +
3513de1929bSSimon Guinot 		      (sizeof(struct ns2_led_data) * num_leds);
3523de1929bSSimon Guinot }
3533de1929bSSimon Guinot 
35498ea1ea2SBill Pemberton static int ns2_led_probe(struct platform_device *pdev)
35511efe71fSSimon Guinot {
35687aae1eaSJingoo Han 	struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
3573de1929bSSimon Guinot 	struct ns2_led_priv *priv;
35811efe71fSSimon Guinot 	int i;
35911efe71fSSimon Guinot 	int ret;
36011efe71fSSimon Guinot 
36172052fccSSimon Guinot #ifdef CONFIG_OF_GPIO
36272052fccSSimon Guinot 	if (!pdata) {
36372052fccSSimon Guinot 		pdata = devm_kzalloc(&pdev->dev,
36472052fccSSimon Guinot 				     sizeof(struct ns2_led_platform_data),
36572052fccSSimon Guinot 				     GFP_KERNEL);
36672052fccSSimon Guinot 		if (!pdata)
36772052fccSSimon Guinot 			return -ENOMEM;
36872052fccSSimon Guinot 
36972052fccSSimon Guinot 		ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
37072052fccSSimon Guinot 		if (ret)
37172052fccSSimon Guinot 			return ret;
37272052fccSSimon Guinot 	}
37372052fccSSimon Guinot #else
37411efe71fSSimon Guinot 	if (!pdata)
37511efe71fSSimon Guinot 		return -EINVAL;
37672052fccSSimon Guinot #endif /* CONFIG_OF_GPIO */
37711efe71fSSimon Guinot 
3783de1929bSSimon Guinot 	priv = devm_kzalloc(&pdev->dev,
3793de1929bSSimon Guinot 			    sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
3803de1929bSSimon Guinot 	if (!priv)
38111efe71fSSimon Guinot 		return -ENOMEM;
3823de1929bSSimon Guinot 	priv->num_leds = pdata->num_leds;
38311efe71fSSimon Guinot 
3843de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++) {
3853de1929bSSimon Guinot 		ret = create_ns2_led(pdev, &priv->leds_data[i],
3863de1929bSSimon Guinot 				     &pdata->leds[i]);
387a209f766SBryan Wu 		if (ret < 0) {
388a209f766SBryan Wu 			for (i = i - 1; i >= 0; i--)
3893de1929bSSimon Guinot 				delete_ns2_led(&priv->leds_data[i]);
390a209f766SBryan Wu 			return ret;
391a209f766SBryan Wu 		}
39211efe71fSSimon Guinot 	}
39311efe71fSSimon Guinot 
3943de1929bSSimon Guinot 	platform_set_drvdata(pdev, priv);
39511efe71fSSimon Guinot 
39611efe71fSSimon Guinot 	return 0;
39711efe71fSSimon Guinot }
39811efe71fSSimon Guinot 
399678e8a6bSBill Pemberton static int ns2_led_remove(struct platform_device *pdev)
40011efe71fSSimon Guinot {
40111efe71fSSimon Guinot 	int i;
4023de1929bSSimon Guinot 	struct ns2_led_priv *priv;
40311efe71fSSimon Guinot 
4043de1929bSSimon Guinot 	priv = platform_get_drvdata(pdev);
40511efe71fSSimon Guinot 
4063de1929bSSimon Guinot 	for (i = 0; i < priv->num_leds; i++)
4073de1929bSSimon Guinot 		delete_ns2_led(&priv->leds_data[i]);
40811efe71fSSimon Guinot 
40911efe71fSSimon Guinot 	return 0;
41011efe71fSSimon Guinot }
41111efe71fSSimon Guinot 
41211efe71fSSimon Guinot static struct platform_driver ns2_led_driver = {
41311efe71fSSimon Guinot 	.probe		= ns2_led_probe,
414df07cf81SBill Pemberton 	.remove		= ns2_led_remove,
41511efe71fSSimon Guinot 	.driver		= {
41611efe71fSSimon Guinot 		.name		= "leds-ns2",
41772052fccSSimon Guinot 		.of_match_table	= of_match_ptr(of_ns2_leds_match),
41811efe71fSSimon Guinot 	},
41911efe71fSSimon Guinot };
42011efe71fSSimon Guinot 
421892a8843SAxel Lin module_platform_driver(ns2_led_driver);
42211efe71fSSimon Guinot 
42311efe71fSSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
42411efe71fSSimon Guinot MODULE_DESCRIPTION("Network Space v2 LED driver");
42511efe71fSSimon Guinot MODULE_LICENSE("GPL");
426892a8843SAxel Lin MODULE_ALIAS("platform:leds-ns2");
427