xref: /linux/drivers/leds/leds-netxbig.c (revision 7b9d9d88158e99a6a17a292ec4ec47105a771c4f)
124467832SSimon Guinot /*
224467832SSimon Guinot  * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
324467832SSimon Guinot  *
424467832SSimon Guinot  * Copyright (C) 2010 LaCie
524467832SSimon Guinot  *
624467832SSimon Guinot  * Author: Simon Guinot <sguinot@lacie.com>
724467832SSimon Guinot  *
824467832SSimon Guinot  * This program is free software; you can redistribute it and/or modify
924467832SSimon Guinot  * it under the terms of the GNU General Public License as published by
1024467832SSimon Guinot  * the Free Software Foundation; either version 2 of the License, or
1124467832SSimon Guinot  * (at your option) any later version.
1224467832SSimon Guinot  *
1324467832SSimon Guinot  * This program is distributed in the hope that it will be useful,
1424467832SSimon Guinot  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1524467832SSimon Guinot  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1624467832SSimon Guinot  * GNU General Public License for more details.
1724467832SSimon Guinot  *
1824467832SSimon Guinot  * You should have received a copy of the GNU General Public License
1924467832SSimon Guinot  * along with this program; if not, write to the Free Software
2024467832SSimon Guinot  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2124467832SSimon Guinot  */
2224467832SSimon Guinot 
2324467832SSimon Guinot #include <linux/module.h>
2424467832SSimon Guinot #include <linux/irq.h>
2524467832SSimon Guinot #include <linux/slab.h>
2624467832SSimon Guinot #include <linux/spinlock.h>
2724467832SSimon Guinot #include <linux/platform_device.h>
2824467832SSimon Guinot #include <linux/gpio.h>
292976b179SSimon Guinot #include <linux/of_gpio.h>
3024467832SSimon Guinot #include <linux/leds.h>
31c02cecb9SArnd Bergmann #include <linux/platform_data/leds-kirkwood-netxbig.h>
3224467832SSimon Guinot 
3324467832SSimon Guinot /*
3424467832SSimon Guinot  * GPIO extension bus.
3524467832SSimon Guinot  */
3624467832SSimon Guinot 
3724467832SSimon Guinot static DEFINE_SPINLOCK(gpio_ext_lock);
3824467832SSimon Guinot 
3924467832SSimon Guinot static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
4024467832SSimon Guinot {
4124467832SSimon Guinot 	int pin;
4224467832SSimon Guinot 
4324467832SSimon Guinot 	for (pin = 0; pin < gpio_ext->num_addr; pin++)
4424467832SSimon Guinot 		gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
4524467832SSimon Guinot }
4624467832SSimon Guinot 
4724467832SSimon Guinot static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
4824467832SSimon Guinot {
4924467832SSimon Guinot 	int pin;
5024467832SSimon Guinot 
5124467832SSimon Guinot 	for (pin = 0; pin < gpio_ext->num_data; pin++)
5224467832SSimon Guinot 		gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
5324467832SSimon Guinot }
5424467832SSimon Guinot 
5524467832SSimon Guinot static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
5624467832SSimon Guinot {
5724467832SSimon Guinot 	/* Enable select is done on the raising edge. */
5824467832SSimon Guinot 	gpio_set_value(gpio_ext->enable, 0);
5924467832SSimon Guinot 	gpio_set_value(gpio_ext->enable, 1);
6024467832SSimon Guinot }
6124467832SSimon Guinot 
6224467832SSimon Guinot static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
6324467832SSimon Guinot 			       int addr, int value)
6424467832SSimon Guinot {
6524467832SSimon Guinot 	unsigned long flags;
6624467832SSimon Guinot 
6724467832SSimon Guinot 	spin_lock_irqsave(&gpio_ext_lock, flags);
6824467832SSimon Guinot 	gpio_ext_set_addr(gpio_ext, addr);
6924467832SSimon Guinot 	gpio_ext_set_data(gpio_ext, value);
7024467832SSimon Guinot 	gpio_ext_enable_select(gpio_ext);
7124467832SSimon Guinot 	spin_unlock_irqrestore(&gpio_ext_lock, flags);
7224467832SSimon Guinot }
7324467832SSimon Guinot 
74cd010955SSimon Guinot static int gpio_ext_init(struct platform_device *pdev,
75cd010955SSimon Guinot 			 struct netxbig_gpio_ext *gpio_ext)
7624467832SSimon Guinot {
7724467832SSimon Guinot 	int err;
7824467832SSimon Guinot 	int i;
7924467832SSimon Guinot 
8024467832SSimon Guinot 	if (unlikely(!gpio_ext))
8124467832SSimon Guinot 		return -EINVAL;
8224467832SSimon Guinot 
8324467832SSimon Guinot 	/* Configure address GPIOs. */
8424467832SSimon Guinot 	for (i = 0; i < gpio_ext->num_addr; i++) {
85cd010955SSimon Guinot 		err = devm_gpio_request_one(&pdev->dev, gpio_ext->addr[i],
86cd010955SSimon Guinot 					    GPIOF_OUT_INIT_LOW,
87b96a573fSAxel Lin 					    "GPIO extension addr");
8824467832SSimon Guinot 		if (err)
89cd010955SSimon Guinot 			return err;
9024467832SSimon Guinot 	}
9124467832SSimon Guinot 	/* Configure data GPIOs. */
9224467832SSimon Guinot 	for (i = 0; i < gpio_ext->num_data; i++) {
93cd010955SSimon Guinot 		err = devm_gpio_request_one(&pdev->dev, gpio_ext->data[i],
94cd010955SSimon Guinot 					    GPIOF_OUT_INIT_LOW,
95b96a573fSAxel Lin 					    "GPIO extension data");
9624467832SSimon Guinot 		if (err)
9724467832SSimon Guinot 			return err;
9824467832SSimon Guinot 	}
99cd010955SSimon Guinot 	/* Configure "enable select" GPIO. */
100cd010955SSimon Guinot 	err = devm_gpio_request_one(&pdev->dev, gpio_ext->enable,
101cd010955SSimon Guinot 				    GPIOF_OUT_INIT_LOW,
102cd010955SSimon Guinot 				    "GPIO extension enable");
103cd010955SSimon Guinot 	if (err)
104cd010955SSimon Guinot 		return err;
10524467832SSimon Guinot 
106cd010955SSimon Guinot 	return 0;
10724467832SSimon Guinot }
10824467832SSimon Guinot 
10924467832SSimon Guinot /*
11024467832SSimon Guinot  * Class LED driver.
11124467832SSimon Guinot  */
11224467832SSimon Guinot 
11324467832SSimon Guinot struct netxbig_led_data {
11424467832SSimon Guinot 	struct netxbig_gpio_ext	*gpio_ext;
11524467832SSimon Guinot 	struct led_classdev	cdev;
11624467832SSimon Guinot 	int			mode_addr;
11724467832SSimon Guinot 	int			*mode_val;
11824467832SSimon Guinot 	int			bright_addr;
11924467832SSimon Guinot 	struct			netxbig_led_timer *timer;
12024467832SSimon Guinot 	int			num_timer;
12124467832SSimon Guinot 	enum netxbig_led_mode	mode;
12224467832SSimon Guinot 	int			sata;
12324467832SSimon Guinot 	spinlock_t		lock;
12424467832SSimon Guinot };
12524467832SSimon Guinot 
12624467832SSimon Guinot static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
12724467832SSimon Guinot 				      unsigned long delay_on,
12824467832SSimon Guinot 				      unsigned long delay_off,
12924467832SSimon Guinot 				      struct netxbig_led_timer *timer,
13024467832SSimon Guinot 				      int num_timer)
13124467832SSimon Guinot {
13224467832SSimon Guinot 	int i;
13324467832SSimon Guinot 
13424467832SSimon Guinot 	for (i = 0; i < num_timer; i++) {
13524467832SSimon Guinot 		if (timer[i].delay_on == delay_on &&
13624467832SSimon Guinot 		    timer[i].delay_off == delay_off) {
13724467832SSimon Guinot 			*mode = timer[i].mode;
13824467832SSimon Guinot 			return 0;
13924467832SSimon Guinot 		}
14024467832SSimon Guinot 	}
14124467832SSimon Guinot 	return -EINVAL;
14224467832SSimon Guinot }
14324467832SSimon Guinot 
14424467832SSimon Guinot static int netxbig_led_blink_set(struct led_classdev *led_cdev,
14524467832SSimon Guinot 				 unsigned long *delay_on,
14624467832SSimon Guinot 				 unsigned long *delay_off)
14724467832SSimon Guinot {
14824467832SSimon Guinot 	struct netxbig_led_data *led_dat =
14924467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
15024467832SSimon Guinot 	enum netxbig_led_mode mode;
15124467832SSimon Guinot 	int mode_val;
15224467832SSimon Guinot 	int ret;
15324467832SSimon Guinot 
15424467832SSimon Guinot 	/* Look for a LED mode with the requested timer frequency. */
15524467832SSimon Guinot 	ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
15624467832SSimon Guinot 					 led_dat->timer, led_dat->num_timer);
15724467832SSimon Guinot 	if (ret < 0)
15824467832SSimon Guinot 		return ret;
15924467832SSimon Guinot 
16024467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
16124467832SSimon Guinot 	if (mode_val == NETXBIG_LED_INVALID_MODE)
16224467832SSimon Guinot 		return -EINVAL;
16324467832SSimon Guinot 
16424467832SSimon Guinot 	spin_lock_irq(&led_dat->lock);
16524467832SSimon Guinot 
16624467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
16724467832SSimon Guinot 	led_dat->mode = mode;
16824467832SSimon Guinot 
16924467832SSimon Guinot 	spin_unlock_irq(&led_dat->lock);
17024467832SSimon Guinot 
17124467832SSimon Guinot 	return 0;
17224467832SSimon Guinot }
17324467832SSimon Guinot 
17424467832SSimon Guinot static void netxbig_led_set(struct led_classdev *led_cdev,
17524467832SSimon Guinot 			    enum led_brightness value)
17624467832SSimon Guinot {
17724467832SSimon Guinot 	struct netxbig_led_data *led_dat =
17824467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
17924467832SSimon Guinot 	enum netxbig_led_mode mode;
180*7b9d9d88SSimon Guinot 	int mode_val;
18124467832SSimon Guinot 	int set_brightness = 1;
18224467832SSimon Guinot 	unsigned long flags;
18324467832SSimon Guinot 
18424467832SSimon Guinot 	spin_lock_irqsave(&led_dat->lock, flags);
18524467832SSimon Guinot 
18624467832SSimon Guinot 	if (value == LED_OFF) {
18724467832SSimon Guinot 		mode = NETXBIG_LED_OFF;
18824467832SSimon Guinot 		set_brightness = 0;
18924467832SSimon Guinot 	} else {
19024467832SSimon Guinot 		if (led_dat->sata)
19124467832SSimon Guinot 			mode = NETXBIG_LED_SATA;
19224467832SSimon Guinot 		else if (led_dat->mode == NETXBIG_LED_OFF)
19324467832SSimon Guinot 			mode = NETXBIG_LED_ON;
19424467832SSimon Guinot 		else /* Keep 'timer' mode. */
19524467832SSimon Guinot 			mode = led_dat->mode;
19624467832SSimon Guinot 	}
19724467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
19824467832SSimon Guinot 
19924467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
20024467832SSimon Guinot 	led_dat->mode = mode;
20124467832SSimon Guinot 	/*
20224467832SSimon Guinot 	 * Note that the brightness register is shared between all the
20324467832SSimon Guinot 	 * SATA LEDs. So, change the brightness setting for a single
20424467832SSimon Guinot 	 * SATA LED will affect all the others.
20524467832SSimon Guinot 	 */
206*7b9d9d88SSimon Guinot 	if (set_brightness)
20724467832SSimon Guinot 		gpio_ext_set_value(led_dat->gpio_ext,
208*7b9d9d88SSimon Guinot 				   led_dat->bright_addr, value);
20924467832SSimon Guinot 
21024467832SSimon Guinot 	spin_unlock_irqrestore(&led_dat->lock, flags);
21124467832SSimon Guinot }
21224467832SSimon Guinot 
21324467832SSimon Guinot static ssize_t netxbig_led_sata_store(struct device *dev,
21424467832SSimon Guinot 				      struct device_attribute *attr,
21524467832SSimon Guinot 				      const char *buff, size_t count)
21624467832SSimon Guinot {
21724467832SSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
21824467832SSimon Guinot 	struct netxbig_led_data *led_dat =
21924467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
22024467832SSimon Guinot 	unsigned long enable;
22124467832SSimon Guinot 	enum netxbig_led_mode mode;
22224467832SSimon Guinot 	int mode_val;
22324467832SSimon Guinot 	int ret;
22424467832SSimon Guinot 
2257517611aSJingoo Han 	ret = kstrtoul(buff, 10, &enable);
22624467832SSimon Guinot 	if (ret < 0)
22724467832SSimon Guinot 		return ret;
22824467832SSimon Guinot 
22924467832SSimon Guinot 	enable = !!enable;
23024467832SSimon Guinot 
23124467832SSimon Guinot 	spin_lock_irq(&led_dat->lock);
23224467832SSimon Guinot 
23324467832SSimon Guinot 	if (led_dat->sata == enable) {
23424467832SSimon Guinot 		ret = count;
23524467832SSimon Guinot 		goto exit_unlock;
23624467832SSimon Guinot 	}
23724467832SSimon Guinot 
23824467832SSimon Guinot 	if (led_dat->mode != NETXBIG_LED_ON &&
23924467832SSimon Guinot 	    led_dat->mode != NETXBIG_LED_SATA)
24024467832SSimon Guinot 		mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
24124467832SSimon Guinot 	else if (enable)
24224467832SSimon Guinot 		mode = NETXBIG_LED_SATA;
24324467832SSimon Guinot 	else
24424467832SSimon Guinot 		mode = NETXBIG_LED_ON;
24524467832SSimon Guinot 
24624467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
24724467832SSimon Guinot 	if (mode_val == NETXBIG_LED_INVALID_MODE) {
24824467832SSimon Guinot 		ret = -EINVAL;
24924467832SSimon Guinot 		goto exit_unlock;
25024467832SSimon Guinot 	}
25124467832SSimon Guinot 
25224467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
25324467832SSimon Guinot 	led_dat->mode = mode;
25424467832SSimon Guinot 	led_dat->sata = enable;
25524467832SSimon Guinot 
25624467832SSimon Guinot 	ret = count;
25724467832SSimon Guinot 
25824467832SSimon Guinot exit_unlock:
25924467832SSimon Guinot 	spin_unlock_irq(&led_dat->lock);
26024467832SSimon Guinot 
26124467832SSimon Guinot 	return ret;
26224467832SSimon Guinot }
26324467832SSimon Guinot 
26424467832SSimon Guinot static ssize_t netxbig_led_sata_show(struct device *dev,
26524467832SSimon Guinot 				     struct device_attribute *attr, char *buf)
26624467832SSimon Guinot {
26724467832SSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
26824467832SSimon Guinot 	struct netxbig_led_data *led_dat =
26924467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
27024467832SSimon Guinot 
27124467832SSimon Guinot 	return sprintf(buf, "%d\n", led_dat->sata);
27224467832SSimon Guinot }
27324467832SSimon Guinot 
27424467832SSimon Guinot static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
27524467832SSimon Guinot 
276588a6a99SJohan Hovold static struct attribute *netxbig_led_attrs[] = {
277588a6a99SJohan Hovold 	&dev_attr_sata.attr,
278588a6a99SJohan Hovold 	NULL
279588a6a99SJohan Hovold };
280588a6a99SJohan Hovold ATTRIBUTE_GROUPS(netxbig_led);
281588a6a99SJohan Hovold 
282cd010955SSimon Guinot static int create_netxbig_led(struct platform_device *pdev,
283cd010955SSimon Guinot 			      struct netxbig_led_platform_data *pdata,
284cd010955SSimon Guinot 			      struct netxbig_led_data *led_dat,
285cd010955SSimon Guinot 			      const struct netxbig_led *template)
28624467832SSimon Guinot {
28724467832SSimon Guinot 	spin_lock_init(&led_dat->lock);
28824467832SSimon Guinot 	led_dat->gpio_ext = pdata->gpio_ext;
28924467832SSimon Guinot 	led_dat->cdev.name = template->name;
29024467832SSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
29124467832SSimon Guinot 	led_dat->cdev.blink_set = netxbig_led_blink_set;
29224467832SSimon Guinot 	led_dat->cdev.brightness_set = netxbig_led_set;
29324467832SSimon Guinot 	/*
29424467832SSimon Guinot 	 * Because the GPIO extension bus don't allow to read registers
29524467832SSimon Guinot 	 * value, there is no way to probe the LED initial state.
29624467832SSimon Guinot 	 * So, the initial sysfs LED value for the "brightness" and "sata"
29724467832SSimon Guinot 	 * attributes are inconsistent.
29824467832SSimon Guinot 	 *
29924467832SSimon Guinot 	 * Note that the initial LED state can't be reconfigured.
30024467832SSimon Guinot 	 * The reason is that the LED behaviour must stay uniform during
30124467832SSimon Guinot 	 * the whole boot process (bootloader+linux).
30224467832SSimon Guinot 	 */
30324467832SSimon Guinot 	led_dat->sata = 0;
30424467832SSimon Guinot 	led_dat->cdev.brightness = LED_OFF;
305*7b9d9d88SSimon Guinot 	led_dat->cdev.max_brightness = template->bright_max;
30624467832SSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
30724467832SSimon Guinot 	led_dat->mode_addr = template->mode_addr;
30824467832SSimon Guinot 	led_dat->mode_val = template->mode_val;
30924467832SSimon Guinot 	led_dat->bright_addr = template->bright_addr;
31024467832SSimon Guinot 	led_dat->timer = pdata->timer;
31124467832SSimon Guinot 	led_dat->num_timer = pdata->num_timer;
3120c86ac2cSSimon Guinot 	/*
3130c86ac2cSSimon Guinot 	 * If available, expose the SATA activity blink capability through
3140c86ac2cSSimon Guinot 	 * a "sata" sysfs attribute.
3150c86ac2cSSimon Guinot 	 */
3160c86ac2cSSimon Guinot 	if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
3170c86ac2cSSimon Guinot 		led_dat->cdev.groups = netxbig_led_groups;
31824467832SSimon Guinot 
319cd010955SSimon Guinot 	return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
32024467832SSimon Guinot }
32124467832SSimon Guinot 
3222976b179SSimon Guinot #ifdef CONFIG_OF_GPIO
3232976b179SSimon Guinot static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
3242976b179SSimon Guinot 				 struct netxbig_gpio_ext *gpio_ext)
3252976b179SSimon Guinot {
3262976b179SSimon Guinot 	int *addr, *data;
3272976b179SSimon Guinot 	int num_addr, num_data;
3282976b179SSimon Guinot 	int ret;
3292976b179SSimon Guinot 	int i;
3302976b179SSimon Guinot 
3312976b179SSimon Guinot 	ret = of_gpio_named_count(np, "addr-gpios");
3322976b179SSimon Guinot 	if (ret < 0) {
3332976b179SSimon Guinot 		dev_err(dev,
3342976b179SSimon Guinot 			"Failed to count GPIOs in DT property addr-gpios\n");
3352976b179SSimon Guinot 		return ret;
3362976b179SSimon Guinot 	}
3372976b179SSimon Guinot 	num_addr = ret;
3382976b179SSimon Guinot 	addr = devm_kzalloc(dev, num_addr * sizeof(*addr), GFP_KERNEL);
3392976b179SSimon Guinot 	if (!addr)
3402976b179SSimon Guinot 		return -ENOMEM;
3412976b179SSimon Guinot 
3422976b179SSimon Guinot 	for (i = 0; i < num_addr; i++) {
3432976b179SSimon Guinot 		ret = of_get_named_gpio(np, "addr-gpios", i);
3442976b179SSimon Guinot 		if (ret < 0)
3452976b179SSimon Guinot 			return ret;
3462976b179SSimon Guinot 		addr[i] = ret;
3472976b179SSimon Guinot 	}
3482976b179SSimon Guinot 	gpio_ext->addr = addr;
3492976b179SSimon Guinot 	gpio_ext->num_addr = num_addr;
3502976b179SSimon Guinot 
3512976b179SSimon Guinot 	ret = of_gpio_named_count(np, "data-gpios");
3522976b179SSimon Guinot 	if (ret < 0) {
3532976b179SSimon Guinot 		dev_err(dev,
3542976b179SSimon Guinot 			"Failed to count GPIOs in DT property data-gpios\n");
3552976b179SSimon Guinot 		return ret;
3562976b179SSimon Guinot 	}
3572976b179SSimon Guinot 	num_data = ret;
3582976b179SSimon Guinot 	data = devm_kzalloc(dev, num_data * sizeof(*data), GFP_KERNEL);
3592976b179SSimon Guinot 	if (!data)
3602976b179SSimon Guinot 		return -ENOMEM;
3612976b179SSimon Guinot 
3622976b179SSimon Guinot 	for (i = 0; i < num_data; i++) {
3632976b179SSimon Guinot 		ret = of_get_named_gpio(np, "data-gpios", i);
3642976b179SSimon Guinot 		if (ret < 0)
3652976b179SSimon Guinot 			return ret;
3662976b179SSimon Guinot 		data[i] = ret;
3672976b179SSimon Guinot 	}
3682976b179SSimon Guinot 	gpio_ext->data = data;
3692976b179SSimon Guinot 	gpio_ext->num_data = num_data;
3702976b179SSimon Guinot 
3712976b179SSimon Guinot 	ret = of_get_named_gpio(np, "enable-gpio", 0);
3722976b179SSimon Guinot 	if (ret < 0) {
3732976b179SSimon Guinot 		dev_err(dev,
3742976b179SSimon Guinot 			"Failed to get GPIO from DT property enable-gpio\n");
3752976b179SSimon Guinot 		return ret;
3762976b179SSimon Guinot 	}
3772976b179SSimon Guinot 	gpio_ext->enable = ret;
3782976b179SSimon Guinot 
3792976b179SSimon Guinot 	return 0;
3802976b179SSimon Guinot }
3812976b179SSimon Guinot 
3822976b179SSimon Guinot static int netxbig_leds_get_of_pdata(struct device *dev,
3832976b179SSimon Guinot 				     struct netxbig_led_platform_data *pdata)
3842976b179SSimon Guinot {
3852976b179SSimon Guinot 	struct device_node *np = dev->of_node;
3862976b179SSimon Guinot 	struct device_node *gpio_ext_np;
3872976b179SSimon Guinot 	struct device_node *child;
3882976b179SSimon Guinot 	struct netxbig_gpio_ext *gpio_ext;
3892976b179SSimon Guinot 	struct netxbig_led_timer *timers;
3902976b179SSimon Guinot 	struct netxbig_led *leds, *led;
3912976b179SSimon Guinot 	int num_timers;
3922976b179SSimon Guinot 	int num_leds = 0;
3932976b179SSimon Guinot 	int ret;
3942976b179SSimon Guinot 	int i;
3952976b179SSimon Guinot 
3962976b179SSimon Guinot 	/* GPIO extension */
3972976b179SSimon Guinot 	gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0);
3982976b179SSimon Guinot 	if (!gpio_ext_np) {
3992976b179SSimon Guinot 		dev_err(dev, "Failed to get DT handle gpio-ext\n");
4002976b179SSimon Guinot 		return -EINVAL;
4012976b179SSimon Guinot 	}
4022976b179SSimon Guinot 
4032976b179SSimon Guinot 	gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
4042976b179SSimon Guinot 	if (!gpio_ext)
4052976b179SSimon Guinot 		return -ENOMEM;
4062976b179SSimon Guinot 	ret = gpio_ext_get_of_pdata(dev, gpio_ext_np, gpio_ext);
4072976b179SSimon Guinot 	if (ret)
4082976b179SSimon Guinot 		return ret;
4092976b179SSimon Guinot 	of_node_put(gpio_ext_np);
4102976b179SSimon Guinot 	pdata->gpio_ext = gpio_ext;
4112976b179SSimon Guinot 
4122976b179SSimon Guinot 	/* Timers (optional) */
4132976b179SSimon Guinot 	ret = of_property_count_u32_elems(np, "timers");
4142976b179SSimon Guinot 	if (ret > 0) {
4152976b179SSimon Guinot 		if (ret % 3)
4162976b179SSimon Guinot 			return -EINVAL;
4172976b179SSimon Guinot 		num_timers = ret / 3;
4182976b179SSimon Guinot 		timers = devm_kzalloc(dev, num_timers * sizeof(*timers),
4192976b179SSimon Guinot 				      GFP_KERNEL);
4202976b179SSimon Guinot 		if (!timers)
4212976b179SSimon Guinot 			return -ENOMEM;
4222976b179SSimon Guinot 		for (i = 0; i < num_timers; i++) {
4232976b179SSimon Guinot 			u32 tmp;
4242976b179SSimon Guinot 
4252976b179SSimon Guinot 			of_property_read_u32_index(np, "timers", 3 * i,
4262976b179SSimon Guinot 						   &timers[i].mode);
4272976b179SSimon Guinot 			if (timers[i].mode >= NETXBIG_LED_MODE_NUM)
4282976b179SSimon Guinot 				return -EINVAL;
4292976b179SSimon Guinot 			of_property_read_u32_index(np, "timers",
4302976b179SSimon Guinot 						   3 * i + 1, &tmp);
4312976b179SSimon Guinot 			timers[i].delay_on = tmp;
4322976b179SSimon Guinot 			of_property_read_u32_index(np, "timers",
4332976b179SSimon Guinot 						   3 * i + 2, &tmp);
4342976b179SSimon Guinot 			timers[i].delay_off = tmp;
4352976b179SSimon Guinot 		}
4362976b179SSimon Guinot 		pdata->timer = timers;
4372976b179SSimon Guinot 		pdata->num_timer = num_timers;
4382976b179SSimon Guinot 	}
4392976b179SSimon Guinot 
4402976b179SSimon Guinot 	/* LEDs */
4412976b179SSimon Guinot 	num_leds = of_get_child_count(np);
4422976b179SSimon Guinot 	if (!num_leds) {
4432976b179SSimon Guinot 		dev_err(dev, "No LED subnodes found in DT\n");
4442976b179SSimon Guinot 		return -ENODEV;
4452976b179SSimon Guinot 	}
4462976b179SSimon Guinot 
4472976b179SSimon Guinot 	leds = devm_kzalloc(dev, num_leds * sizeof(*leds), GFP_KERNEL);
4482976b179SSimon Guinot 	if (!leds)
4492976b179SSimon Guinot 		return -ENOMEM;
4502976b179SSimon Guinot 
4512976b179SSimon Guinot 	led = leds;
4522976b179SSimon Guinot 	for_each_child_of_node(np, child) {
4532976b179SSimon Guinot 		const char *string;
4542976b179SSimon Guinot 		int *mode_val;
4552976b179SSimon Guinot 		int num_modes;
4562976b179SSimon Guinot 
4572976b179SSimon Guinot 		ret = of_property_read_u32(child, "mode-addr",
4582976b179SSimon Guinot 					   &led->mode_addr);
4592976b179SSimon Guinot 		if (ret)
4602976b179SSimon Guinot 			goto err_node_put;
4612976b179SSimon Guinot 
4622976b179SSimon Guinot 		ret = of_property_read_u32(child, "bright-addr",
4632976b179SSimon Guinot 					   &led->bright_addr);
4642976b179SSimon Guinot 		if (ret)
4652976b179SSimon Guinot 			goto err_node_put;
4662976b179SSimon Guinot 
4672976b179SSimon Guinot 		ret = of_property_read_u32(child, "max-brightness",
4682976b179SSimon Guinot 					   &led->bright_max);
4692976b179SSimon Guinot 		if (ret)
4702976b179SSimon Guinot 			goto err_node_put;
4712976b179SSimon Guinot 
4722976b179SSimon Guinot 		mode_val =
4732976b179SSimon Guinot 			devm_kzalloc(dev,
4742976b179SSimon Guinot 				     NETXBIG_LED_MODE_NUM * sizeof(*mode_val),
4752976b179SSimon Guinot 				     GFP_KERNEL);
4762976b179SSimon Guinot 		if (!mode_val) {
4772976b179SSimon Guinot 			ret = -ENOMEM;
4782976b179SSimon Guinot 			goto err_node_put;
4792976b179SSimon Guinot 		}
4802976b179SSimon Guinot 
4812976b179SSimon Guinot 		for (i = 0; i < NETXBIG_LED_MODE_NUM; i++)
4822976b179SSimon Guinot 			mode_val[i] = NETXBIG_LED_INVALID_MODE;
4832976b179SSimon Guinot 
4842976b179SSimon Guinot 		ret = of_property_count_u32_elems(child, "mode-val");
4852976b179SSimon Guinot 		if (ret < 0 || ret % 2) {
4862976b179SSimon Guinot 			ret = -EINVAL;
4872976b179SSimon Guinot 			goto err_node_put;
4882976b179SSimon Guinot 		}
4892976b179SSimon Guinot 		num_modes = ret / 2;
4902976b179SSimon Guinot 		if (num_modes > NETXBIG_LED_MODE_NUM) {
4912976b179SSimon Guinot 			ret = -EINVAL;
4922976b179SSimon Guinot 			goto err_node_put;
4932976b179SSimon Guinot 		}
4942976b179SSimon Guinot 
4952976b179SSimon Guinot 		for (i = 0; i < num_modes; i++) {
4962976b179SSimon Guinot 			int mode;
4972976b179SSimon Guinot 			int val;
4982976b179SSimon Guinot 
4992976b179SSimon Guinot 			of_property_read_u32_index(child,
5002976b179SSimon Guinot 						   "mode-val", 2 * i, &mode);
5012976b179SSimon Guinot 			of_property_read_u32_index(child,
5022976b179SSimon Guinot 						   "mode-val", 2 * i + 1, &val);
5032976b179SSimon Guinot 			if (mode >= NETXBIG_LED_MODE_NUM) {
5042976b179SSimon Guinot 				ret = -EINVAL;
5052976b179SSimon Guinot 				goto err_node_put;
5062976b179SSimon Guinot 			}
5072976b179SSimon Guinot 			mode_val[mode] = val;
5082976b179SSimon Guinot 		}
5092976b179SSimon Guinot 		led->mode_val = mode_val;
5102976b179SSimon Guinot 
5112976b179SSimon Guinot 		if (!of_property_read_string(child, "label", &string))
5122976b179SSimon Guinot 			led->name = string;
5132976b179SSimon Guinot 		else
5142976b179SSimon Guinot 			led->name = child->name;
5152976b179SSimon Guinot 
5162976b179SSimon Guinot 		if (!of_property_read_string(child,
5172976b179SSimon Guinot 					     "linux,default-trigger", &string))
5182976b179SSimon Guinot 			led->default_trigger = string;
5192976b179SSimon Guinot 
5202976b179SSimon Guinot 		led++;
5212976b179SSimon Guinot 	}
5222976b179SSimon Guinot 
5232976b179SSimon Guinot 	pdata->leds = leds;
5242976b179SSimon Guinot 	pdata->num_leds = num_leds;
5252976b179SSimon Guinot 
5262976b179SSimon Guinot 	return 0;
5272976b179SSimon Guinot 
5282976b179SSimon Guinot err_node_put:
5292976b179SSimon Guinot 	of_node_put(child);
5302976b179SSimon Guinot 	return ret;
5312976b179SSimon Guinot }
5322976b179SSimon Guinot 
5332976b179SSimon Guinot static const struct of_device_id of_netxbig_leds_match[] = {
5342976b179SSimon Guinot 	{ .compatible = "lacie,netxbig-leds", },
5352976b179SSimon Guinot 	{},
5362976b179SSimon Guinot };
5372976b179SSimon Guinot #else
5382976b179SSimon Guinot static inline int
5392976b179SSimon Guinot netxbig_leds_get_of_pdata(struct device *dev,
5402976b179SSimon Guinot 			  struct netxbig_led_platform_data *pdata)
5412976b179SSimon Guinot {
5422976b179SSimon Guinot 	return -ENODEV;
5432976b179SSimon Guinot }
5442976b179SSimon Guinot #endif /* CONFIG_OF_GPIO */
5452976b179SSimon Guinot 
54698ea1ea2SBill Pemberton static int netxbig_led_probe(struct platform_device *pdev)
54724467832SSimon Guinot {
54887aae1eaSJingoo Han 	struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
549cd010955SSimon Guinot 	struct netxbig_led_data *leds_data;
55024467832SSimon Guinot 	int i;
55124467832SSimon Guinot 	int ret;
55224467832SSimon Guinot 
5532976b179SSimon Guinot 	if (!pdata) {
5542976b179SSimon Guinot 		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
55524467832SSimon Guinot 		if (!pdata)
55624467832SSimon Guinot 			return -ENOMEM;
5572976b179SSimon Guinot 		ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata);
5582976b179SSimon Guinot 		if (ret)
5592976b179SSimon Guinot 			return ret;
5602976b179SSimon Guinot 	}
5612976b179SSimon Guinot 
562cd010955SSimon Guinot 	leds_data = devm_kzalloc(&pdev->dev,
563cd010955SSimon Guinot 				 pdata->num_leds * sizeof(*leds_data),
5642976b179SSimon Guinot 				 GFP_KERNEL);
565cd010955SSimon Guinot 	if (!leds_data)
5662976b179SSimon Guinot 		return -ENOMEM;
56724467832SSimon Guinot 
568cd010955SSimon Guinot 	ret = gpio_ext_init(pdev, pdata->gpio_ext);
56924467832SSimon Guinot 	if (ret < 0)
5708095c385SBryan Wu 		return ret;
57124467832SSimon Guinot 
57224467832SSimon Guinot 	for (i = 0; i < pdata->num_leds; i++) {
573cd010955SSimon Guinot 		ret = create_netxbig_led(pdev, pdata,
574cd010955SSimon Guinot 					 &leds_data[i], &pdata->leds[i]);
57524467832SSimon Guinot 		if (ret < 0)
57624467832SSimon Guinot 			return ret;
57724467832SSimon Guinot 	}
57824467832SSimon Guinot 
57924467832SSimon Guinot 	return 0;
58024467832SSimon Guinot }
58124467832SSimon Guinot 
58224467832SSimon Guinot static struct platform_driver netxbig_led_driver = {
58324467832SSimon Guinot 	.probe		= netxbig_led_probe,
58424467832SSimon Guinot 	.driver		= {
58524467832SSimon Guinot 		.name		= "leds-netxbig",
5862976b179SSimon Guinot 		.of_match_table	= of_match_ptr(of_netxbig_leds_match),
58724467832SSimon Guinot 	},
58824467832SSimon Guinot };
58924467832SSimon Guinot 
590892a8843SAxel Lin module_platform_driver(netxbig_led_driver);
59124467832SSimon Guinot 
59224467832SSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
59324467832SSimon Guinot MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
59424467832SSimon Guinot MODULE_LICENSE("GPL");
595892a8843SAxel Lin MODULE_ALIAS("platform:leds-netxbig");
596