xref: /openbmc/linux/drivers/leds/leds-netxbig.c (revision 5ccfa39d)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
224467832SSimon Guinot /*
324467832SSimon Guinot  * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
424467832SSimon Guinot  *
524467832SSimon Guinot  * Copyright (C) 2010 LaCie
624467832SSimon Guinot  *
724467832SSimon Guinot  * Author: Simon Guinot <sguinot@lacie.com>
824467832SSimon Guinot  */
924467832SSimon Guinot 
1024467832SSimon Guinot #include <linux/module.h>
1124467832SSimon Guinot #include <linux/irq.h>
1224467832SSimon Guinot #include <linux/slab.h>
1324467832SSimon Guinot #include <linux/spinlock.h>
1424467832SSimon Guinot #include <linux/platform_device.h>
159af512e8SLinus Walleij #include <linux/gpio/consumer.h>
1624467832SSimon Guinot #include <linux/leds.h>
179af512e8SLinus Walleij #include <linux/of.h>
189af512e8SLinus Walleij #include <linux/of_platform.h>
19156189a6SMasahiro Yamada 
20156189a6SMasahiro Yamada struct netxbig_gpio_ext {
219af512e8SLinus Walleij 	struct gpio_desc **addr;
22156189a6SMasahiro Yamada 	int		num_addr;
239af512e8SLinus Walleij 	struct gpio_desc **data;
24156189a6SMasahiro Yamada 	int		num_data;
259af512e8SLinus Walleij 	struct gpio_desc *enable;
26156189a6SMasahiro Yamada };
27156189a6SMasahiro Yamada 
28156189a6SMasahiro Yamada enum netxbig_led_mode {
29156189a6SMasahiro Yamada 	NETXBIG_LED_OFF,
30156189a6SMasahiro Yamada 	NETXBIG_LED_ON,
31156189a6SMasahiro Yamada 	NETXBIG_LED_SATA,
32156189a6SMasahiro Yamada 	NETXBIG_LED_TIMER1,
33156189a6SMasahiro Yamada 	NETXBIG_LED_TIMER2,
34156189a6SMasahiro Yamada 	NETXBIG_LED_MODE_NUM,
35156189a6SMasahiro Yamada };
36156189a6SMasahiro Yamada 
37156189a6SMasahiro Yamada #define NETXBIG_LED_INVALID_MODE NETXBIG_LED_MODE_NUM
38156189a6SMasahiro Yamada 
39156189a6SMasahiro Yamada struct netxbig_led_timer {
40156189a6SMasahiro Yamada 	unsigned long		delay_on;
41156189a6SMasahiro Yamada 	unsigned long		delay_off;
42156189a6SMasahiro Yamada 	enum netxbig_led_mode	mode;
43156189a6SMasahiro Yamada };
44156189a6SMasahiro Yamada 
45156189a6SMasahiro Yamada struct netxbig_led {
46156189a6SMasahiro Yamada 	const char	*name;
47156189a6SMasahiro Yamada 	const char	*default_trigger;
48156189a6SMasahiro Yamada 	int		mode_addr;
49156189a6SMasahiro Yamada 	int		*mode_val;
50156189a6SMasahiro Yamada 	int		bright_addr;
51156189a6SMasahiro Yamada 	int		bright_max;
52156189a6SMasahiro Yamada };
53156189a6SMasahiro Yamada 
54156189a6SMasahiro Yamada struct netxbig_led_platform_data {
55156189a6SMasahiro Yamada 	struct netxbig_gpio_ext	*gpio_ext;
56156189a6SMasahiro Yamada 	struct netxbig_led_timer *timer;
57156189a6SMasahiro Yamada 	int			num_timer;
58156189a6SMasahiro Yamada 	struct netxbig_led	*leds;
59156189a6SMasahiro Yamada 	int			num_leds;
60156189a6SMasahiro Yamada };
6124467832SSimon Guinot 
6224467832SSimon Guinot /*
6324467832SSimon Guinot  * GPIO extension bus.
6424467832SSimon Guinot  */
6524467832SSimon Guinot 
6624467832SSimon Guinot static DEFINE_SPINLOCK(gpio_ext_lock);
6724467832SSimon Guinot 
gpio_ext_set_addr(struct netxbig_gpio_ext * gpio_ext,int addr)6824467832SSimon Guinot static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
6924467832SSimon Guinot {
7024467832SSimon Guinot 	int pin;
7124467832SSimon Guinot 
7224467832SSimon Guinot 	for (pin = 0; pin < gpio_ext->num_addr; pin++)
739af512e8SLinus Walleij 		gpiod_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
7424467832SSimon Guinot }
7524467832SSimon Guinot 
gpio_ext_set_data(struct netxbig_gpio_ext * gpio_ext,int data)7624467832SSimon Guinot static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
7724467832SSimon Guinot {
7824467832SSimon Guinot 	int pin;
7924467832SSimon Guinot 
8024467832SSimon Guinot 	for (pin = 0; pin < gpio_ext->num_data; pin++)
819af512e8SLinus Walleij 		gpiod_set_value(gpio_ext->data[pin], (data >> pin) & 1);
8224467832SSimon Guinot }
8324467832SSimon Guinot 
gpio_ext_enable_select(struct netxbig_gpio_ext * gpio_ext)8424467832SSimon Guinot static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
8524467832SSimon Guinot {
8624467832SSimon Guinot 	/* Enable select is done on the raising edge. */
879af512e8SLinus Walleij 	gpiod_set_value(gpio_ext->enable, 0);
889af512e8SLinus Walleij 	gpiod_set_value(gpio_ext->enable, 1);
8924467832SSimon Guinot }
9024467832SSimon Guinot 
gpio_ext_set_value(struct netxbig_gpio_ext * gpio_ext,int addr,int value)9124467832SSimon Guinot static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
9224467832SSimon Guinot 			       int addr, int value)
9324467832SSimon Guinot {
9424467832SSimon Guinot 	unsigned long flags;
9524467832SSimon Guinot 
9624467832SSimon Guinot 	spin_lock_irqsave(&gpio_ext_lock, flags);
9724467832SSimon Guinot 	gpio_ext_set_addr(gpio_ext, addr);
9824467832SSimon Guinot 	gpio_ext_set_data(gpio_ext, value);
9924467832SSimon Guinot 	gpio_ext_enable_select(gpio_ext);
10024467832SSimon Guinot 	spin_unlock_irqrestore(&gpio_ext_lock, flags);
10124467832SSimon Guinot }
10224467832SSimon Guinot 
10324467832SSimon Guinot /*
10424467832SSimon Guinot  * Class LED driver.
10524467832SSimon Guinot  */
10624467832SSimon Guinot 
10724467832SSimon Guinot struct netxbig_led_data {
10824467832SSimon Guinot 	struct netxbig_gpio_ext	*gpio_ext;
10924467832SSimon Guinot 	struct led_classdev	cdev;
11024467832SSimon Guinot 	int			mode_addr;
11124467832SSimon Guinot 	int			*mode_val;
11224467832SSimon Guinot 	int			bright_addr;
11324467832SSimon Guinot 	struct			netxbig_led_timer *timer;
11424467832SSimon Guinot 	int			num_timer;
11524467832SSimon Guinot 	enum netxbig_led_mode	mode;
11624467832SSimon Guinot 	int			sata;
11724467832SSimon Guinot 	spinlock_t		lock;
11824467832SSimon Guinot };
11924467832SSimon Guinot 
netxbig_led_get_timer_mode(enum netxbig_led_mode * mode,unsigned long delay_on,unsigned long delay_off,struct netxbig_led_timer * timer,int num_timer)12024467832SSimon Guinot static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
12124467832SSimon Guinot 				      unsigned long delay_on,
12224467832SSimon Guinot 				      unsigned long delay_off,
12324467832SSimon Guinot 				      struct netxbig_led_timer *timer,
12424467832SSimon Guinot 				      int num_timer)
12524467832SSimon Guinot {
12624467832SSimon Guinot 	int i;
12724467832SSimon Guinot 
12824467832SSimon Guinot 	for (i = 0; i < num_timer; i++) {
12924467832SSimon Guinot 		if (timer[i].delay_on == delay_on &&
13024467832SSimon Guinot 		    timer[i].delay_off == delay_off) {
13124467832SSimon Guinot 			*mode = timer[i].mode;
13224467832SSimon Guinot 			return 0;
13324467832SSimon Guinot 		}
13424467832SSimon Guinot 	}
13524467832SSimon Guinot 	return -EINVAL;
13624467832SSimon Guinot }
13724467832SSimon Guinot 
netxbig_led_blink_set(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)13824467832SSimon Guinot static int netxbig_led_blink_set(struct led_classdev *led_cdev,
13924467832SSimon Guinot 				 unsigned long *delay_on,
14024467832SSimon Guinot 				 unsigned long *delay_off)
14124467832SSimon Guinot {
14224467832SSimon Guinot 	struct netxbig_led_data *led_dat =
14324467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
14424467832SSimon Guinot 	enum netxbig_led_mode mode;
14524467832SSimon Guinot 	int mode_val;
14624467832SSimon Guinot 	int ret;
14724467832SSimon Guinot 
14824467832SSimon Guinot 	/* Look for a LED mode with the requested timer frequency. */
14924467832SSimon Guinot 	ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
15024467832SSimon Guinot 					 led_dat->timer, led_dat->num_timer);
15124467832SSimon Guinot 	if (ret < 0)
15224467832SSimon Guinot 		return ret;
15324467832SSimon Guinot 
15424467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
15524467832SSimon Guinot 	if (mode_val == NETXBIG_LED_INVALID_MODE)
15624467832SSimon Guinot 		return -EINVAL;
15724467832SSimon Guinot 
15824467832SSimon Guinot 	spin_lock_irq(&led_dat->lock);
15924467832SSimon Guinot 
16024467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
16124467832SSimon Guinot 	led_dat->mode = mode;
16224467832SSimon Guinot 
16324467832SSimon Guinot 	spin_unlock_irq(&led_dat->lock);
16424467832SSimon Guinot 
16524467832SSimon Guinot 	return 0;
16624467832SSimon Guinot }
16724467832SSimon Guinot 
netxbig_led_set(struct led_classdev * led_cdev,enum led_brightness value)16824467832SSimon Guinot static void netxbig_led_set(struct led_classdev *led_cdev,
16924467832SSimon Guinot 			    enum led_brightness value)
17024467832SSimon Guinot {
17124467832SSimon Guinot 	struct netxbig_led_data *led_dat =
17224467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
17324467832SSimon Guinot 	enum netxbig_led_mode mode;
1747b9d9d88SSimon Guinot 	int mode_val;
17524467832SSimon Guinot 	int set_brightness = 1;
17624467832SSimon Guinot 	unsigned long flags;
17724467832SSimon Guinot 
17824467832SSimon Guinot 	spin_lock_irqsave(&led_dat->lock, flags);
17924467832SSimon Guinot 
18024467832SSimon Guinot 	if (value == LED_OFF) {
18124467832SSimon Guinot 		mode = NETXBIG_LED_OFF;
18224467832SSimon Guinot 		set_brightness = 0;
18324467832SSimon Guinot 	} else {
18424467832SSimon Guinot 		if (led_dat->sata)
18524467832SSimon Guinot 			mode = NETXBIG_LED_SATA;
18624467832SSimon Guinot 		else if (led_dat->mode == NETXBIG_LED_OFF)
18724467832SSimon Guinot 			mode = NETXBIG_LED_ON;
18824467832SSimon Guinot 		else /* Keep 'timer' mode. */
18924467832SSimon Guinot 			mode = led_dat->mode;
19024467832SSimon Guinot 	}
19124467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
19224467832SSimon Guinot 
19324467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
19424467832SSimon Guinot 	led_dat->mode = mode;
19524467832SSimon Guinot 	/*
19624467832SSimon Guinot 	 * Note that the brightness register is shared between all the
19724467832SSimon Guinot 	 * SATA LEDs. So, change the brightness setting for a single
19824467832SSimon Guinot 	 * SATA LED will affect all the others.
19924467832SSimon Guinot 	 */
2007b9d9d88SSimon Guinot 	if (set_brightness)
20124467832SSimon Guinot 		gpio_ext_set_value(led_dat->gpio_ext,
2027b9d9d88SSimon Guinot 				   led_dat->bright_addr, value);
20324467832SSimon Guinot 
20424467832SSimon Guinot 	spin_unlock_irqrestore(&led_dat->lock, flags);
20524467832SSimon Guinot }
20624467832SSimon Guinot 
sata_store(struct device * dev,struct device_attribute * attr,const char * buff,size_t count)207*5ccfa39dSDwaipayan Ray static ssize_t sata_store(struct device *dev,
20824467832SSimon Guinot 			  struct device_attribute *attr,
20924467832SSimon Guinot 			  const char *buff, size_t count)
21024467832SSimon Guinot {
21124467832SSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
21224467832SSimon Guinot 	struct netxbig_led_data *led_dat =
21324467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
21424467832SSimon Guinot 	unsigned long enable;
21524467832SSimon Guinot 	enum netxbig_led_mode mode;
21624467832SSimon Guinot 	int mode_val;
21724467832SSimon Guinot 	int ret;
21824467832SSimon Guinot 
2197517611aSJingoo Han 	ret = kstrtoul(buff, 10, &enable);
22024467832SSimon Guinot 	if (ret < 0)
22124467832SSimon Guinot 		return ret;
22224467832SSimon Guinot 
22324467832SSimon Guinot 	enable = !!enable;
22424467832SSimon Guinot 
22524467832SSimon Guinot 	spin_lock_irq(&led_dat->lock);
22624467832SSimon Guinot 
22724467832SSimon Guinot 	if (led_dat->sata == enable) {
22824467832SSimon Guinot 		ret = count;
22924467832SSimon Guinot 		goto exit_unlock;
23024467832SSimon Guinot 	}
23124467832SSimon Guinot 
23224467832SSimon Guinot 	if (led_dat->mode != NETXBIG_LED_ON &&
23324467832SSimon Guinot 	    led_dat->mode != NETXBIG_LED_SATA)
23424467832SSimon Guinot 		mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
23524467832SSimon Guinot 	else if (enable)
23624467832SSimon Guinot 		mode = NETXBIG_LED_SATA;
23724467832SSimon Guinot 	else
23824467832SSimon Guinot 		mode = NETXBIG_LED_ON;
23924467832SSimon Guinot 
24024467832SSimon Guinot 	mode_val = led_dat->mode_val[mode];
24124467832SSimon Guinot 	if (mode_val == NETXBIG_LED_INVALID_MODE) {
24224467832SSimon Guinot 		ret = -EINVAL;
24324467832SSimon Guinot 		goto exit_unlock;
24424467832SSimon Guinot 	}
24524467832SSimon Guinot 
24624467832SSimon Guinot 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
24724467832SSimon Guinot 	led_dat->mode = mode;
24824467832SSimon Guinot 	led_dat->sata = enable;
24924467832SSimon Guinot 
25024467832SSimon Guinot 	ret = count;
25124467832SSimon Guinot 
25224467832SSimon Guinot exit_unlock:
25324467832SSimon Guinot 	spin_unlock_irq(&led_dat->lock);
25424467832SSimon Guinot 
25524467832SSimon Guinot 	return ret;
25624467832SSimon Guinot }
25724467832SSimon Guinot 
sata_show(struct device * dev,struct device_attribute * attr,char * buf)258*5ccfa39dSDwaipayan Ray static ssize_t sata_show(struct device *dev,
25924467832SSimon Guinot 			 struct device_attribute *attr, char *buf)
26024467832SSimon Guinot {
26124467832SSimon Guinot 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
26224467832SSimon Guinot 	struct netxbig_led_data *led_dat =
26324467832SSimon Guinot 		container_of(led_cdev, struct netxbig_led_data, cdev);
26424467832SSimon Guinot 
26524467832SSimon Guinot 	return sprintf(buf, "%d\n", led_dat->sata);
26624467832SSimon Guinot }
26724467832SSimon Guinot 
268*5ccfa39dSDwaipayan Ray static DEVICE_ATTR_RW(sata);
26924467832SSimon Guinot 
270588a6a99SJohan Hovold static struct attribute *netxbig_led_attrs[] = {
271588a6a99SJohan Hovold 	&dev_attr_sata.attr,
272588a6a99SJohan Hovold 	NULL
273588a6a99SJohan Hovold };
274588a6a99SJohan Hovold ATTRIBUTE_GROUPS(netxbig_led);
275588a6a99SJohan Hovold 
create_netxbig_led(struct platform_device * pdev,struct netxbig_led_platform_data * pdata,struct netxbig_led_data * led_dat,const struct netxbig_led * template)276cd010955SSimon Guinot static int create_netxbig_led(struct platform_device *pdev,
277cd010955SSimon Guinot 			      struct netxbig_led_platform_data *pdata,
278cd010955SSimon Guinot 			      struct netxbig_led_data *led_dat,
279cd010955SSimon Guinot 			      const struct netxbig_led *template)
28024467832SSimon Guinot {
28124467832SSimon Guinot 	spin_lock_init(&led_dat->lock);
28224467832SSimon Guinot 	led_dat->gpio_ext = pdata->gpio_ext;
28324467832SSimon Guinot 	led_dat->cdev.name = template->name;
28424467832SSimon Guinot 	led_dat->cdev.default_trigger = template->default_trigger;
28524467832SSimon Guinot 	led_dat->cdev.blink_set = netxbig_led_blink_set;
28624467832SSimon Guinot 	led_dat->cdev.brightness_set = netxbig_led_set;
28724467832SSimon Guinot 	/*
28824467832SSimon Guinot 	 * Because the GPIO extension bus don't allow to read registers
28924467832SSimon Guinot 	 * value, there is no way to probe the LED initial state.
29024467832SSimon Guinot 	 * So, the initial sysfs LED value for the "brightness" and "sata"
29124467832SSimon Guinot 	 * attributes are inconsistent.
29224467832SSimon Guinot 	 *
29324467832SSimon Guinot 	 * Note that the initial LED state can't be reconfigured.
29424467832SSimon Guinot 	 * The reason is that the LED behaviour must stay uniform during
29524467832SSimon Guinot 	 * the whole boot process (bootloader+linux).
29624467832SSimon Guinot 	 */
29724467832SSimon Guinot 	led_dat->sata = 0;
29824467832SSimon Guinot 	led_dat->cdev.brightness = LED_OFF;
2997b9d9d88SSimon Guinot 	led_dat->cdev.max_brightness = template->bright_max;
30024467832SSimon Guinot 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
30124467832SSimon Guinot 	led_dat->mode_addr = template->mode_addr;
30224467832SSimon Guinot 	led_dat->mode_val = template->mode_val;
30324467832SSimon Guinot 	led_dat->bright_addr = template->bright_addr;
30424467832SSimon Guinot 	led_dat->timer = pdata->timer;
30524467832SSimon Guinot 	led_dat->num_timer = pdata->num_timer;
3060c86ac2cSSimon Guinot 	/*
3070c86ac2cSSimon Guinot 	 * If available, expose the SATA activity blink capability through
3080c86ac2cSSimon Guinot 	 * a "sata" sysfs attribute.
3090c86ac2cSSimon Guinot 	 */
3100c86ac2cSSimon Guinot 	if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
3110c86ac2cSSimon Guinot 		led_dat->cdev.groups = netxbig_led_groups;
31224467832SSimon Guinot 
313cd010955SSimon Guinot 	return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
31424467832SSimon Guinot }
31524467832SSimon Guinot 
3169af512e8SLinus Walleij /**
3179af512e8SLinus Walleij  * netxbig_gpio_ext_remove() - Clean up GPIO extension data
3189af512e8SLinus Walleij  * @data: managed resource data to clean up
3199af512e8SLinus Walleij  *
3209af512e8SLinus Walleij  * Since we pick GPIO descriptors from another device than the device our
3219af512e8SLinus Walleij  * driver is probing to, we need to register a specific callback to free
3229af512e8SLinus Walleij  * these up using managed resources.
3239af512e8SLinus Walleij  */
netxbig_gpio_ext_remove(void * data)3249af512e8SLinus Walleij static void netxbig_gpio_ext_remove(void *data)
3259af512e8SLinus Walleij {
3269af512e8SLinus Walleij 	struct netxbig_gpio_ext *gpio_ext = data;
3279af512e8SLinus Walleij 	int i;
3289af512e8SLinus Walleij 
3299af512e8SLinus Walleij 	for (i = 0; i < gpio_ext->num_addr; i++)
3309af512e8SLinus Walleij 		gpiod_put(gpio_ext->addr[i]);
3319af512e8SLinus Walleij 	for (i = 0; i < gpio_ext->num_data; i++)
3329af512e8SLinus Walleij 		gpiod_put(gpio_ext->data[i]);
3339af512e8SLinus Walleij 	gpiod_put(gpio_ext->enable);
3349af512e8SLinus Walleij }
3359af512e8SLinus Walleij 
3369af512e8SLinus Walleij /**
3379af512e8SLinus Walleij  * netxbig_gpio_ext_get() - Obtain GPIO extension device data
3389af512e8SLinus Walleij  * @dev: main LED device
3399af512e8SLinus Walleij  * @gpio_ext_dev: the GPIO extension device
3409af512e8SLinus Walleij  * @gpio_ext: the data structure holding the GPIO extension data
3419af512e8SLinus Walleij  *
3429af512e8SLinus Walleij  * This function walks the subdevice that only contain GPIO line
3439af512e8SLinus Walleij  * handles in the device tree and obtains the GPIO descriptors from that
3449af512e8SLinus Walleij  * device.
3459af512e8SLinus Walleij  */
netxbig_gpio_ext_get(struct device * dev,struct device * gpio_ext_dev,struct netxbig_gpio_ext * gpio_ext)3469af512e8SLinus Walleij static int netxbig_gpio_ext_get(struct device *dev,
3479af512e8SLinus Walleij 				struct device *gpio_ext_dev,
3482976b179SSimon Guinot 				struct netxbig_gpio_ext *gpio_ext)
3492976b179SSimon Guinot {
3509af512e8SLinus Walleij 	struct gpio_desc **addr, **data;
3512976b179SSimon Guinot 	int num_addr, num_data;
3529af512e8SLinus Walleij 	struct gpio_desc *gpiod;
3532976b179SSimon Guinot 	int ret;
3542976b179SSimon Guinot 	int i;
3552976b179SSimon Guinot 
3569af512e8SLinus Walleij 	ret = gpiod_count(gpio_ext_dev, "addr");
3572976b179SSimon Guinot 	if (ret < 0) {
3582976b179SSimon Guinot 		dev_err(dev,
3592976b179SSimon Guinot 			"Failed to count GPIOs in DT property addr-gpios\n");
3602976b179SSimon Guinot 		return ret;
3612976b179SSimon Guinot 	}
3622976b179SSimon Guinot 	num_addr = ret;
363a86854d0SKees Cook 	addr = devm_kcalloc(dev, num_addr, sizeof(*addr), GFP_KERNEL);
3642976b179SSimon Guinot 	if (!addr)
3652976b179SSimon Guinot 		return -ENOMEM;
3662976b179SSimon Guinot 
3679af512e8SLinus Walleij 	/*
3689af512e8SLinus Walleij 	 * We cannot use devm_ managed resources with these GPIO descriptors
3699af512e8SLinus Walleij 	 * since they are associated with the "GPIO extension device" which
3709af512e8SLinus Walleij 	 * does not probe any driver. The device tree parser will however
3719af512e8SLinus Walleij 	 * populate a platform device for it so we can anyway obtain the
3729af512e8SLinus Walleij 	 * GPIO descriptors from the device.
3739af512e8SLinus Walleij 	 */
3742976b179SSimon Guinot 	for (i = 0; i < num_addr; i++) {
3759af512e8SLinus Walleij 		gpiod = gpiod_get_index(gpio_ext_dev, "addr", i,
3769af512e8SLinus Walleij 					GPIOD_OUT_LOW);
3779af512e8SLinus Walleij 		if (IS_ERR(gpiod))
3789af512e8SLinus Walleij 			return PTR_ERR(gpiod);
3799af512e8SLinus Walleij 		gpiod_set_consumer_name(gpiod, "GPIO extension addr");
3809af512e8SLinus Walleij 		addr[i] = gpiod;
3812976b179SSimon Guinot 	}
3822976b179SSimon Guinot 	gpio_ext->addr = addr;
3832976b179SSimon Guinot 	gpio_ext->num_addr = num_addr;
3842976b179SSimon Guinot 
3859af512e8SLinus Walleij 	ret = gpiod_count(gpio_ext_dev, "data");
3862976b179SSimon Guinot 	if (ret < 0) {
3872976b179SSimon Guinot 		dev_err(dev,
3882976b179SSimon Guinot 			"Failed to count GPIOs in DT property data-gpios\n");
3892976b179SSimon Guinot 		return ret;
3902976b179SSimon Guinot 	}
3912976b179SSimon Guinot 	num_data = ret;
392a86854d0SKees Cook 	data = devm_kcalloc(dev, num_data, sizeof(*data), GFP_KERNEL);
3932976b179SSimon Guinot 	if (!data)
3942976b179SSimon Guinot 		return -ENOMEM;
3952976b179SSimon Guinot 
3962976b179SSimon Guinot 	for (i = 0; i < num_data; i++) {
3979af512e8SLinus Walleij 		gpiod = gpiod_get_index(gpio_ext_dev, "data", i,
3989af512e8SLinus Walleij 					GPIOD_OUT_LOW);
3999af512e8SLinus Walleij 		if (IS_ERR(gpiod))
4009af512e8SLinus Walleij 			return PTR_ERR(gpiod);
4019af512e8SLinus Walleij 		gpiod_set_consumer_name(gpiod, "GPIO extension data");
4029af512e8SLinus Walleij 		data[i] = gpiod;
4032976b179SSimon Guinot 	}
4042976b179SSimon Guinot 	gpio_ext->data = data;
4052976b179SSimon Guinot 	gpio_ext->num_data = num_data;
4062976b179SSimon Guinot 
4079af512e8SLinus Walleij 	gpiod = gpiod_get(gpio_ext_dev, "enable", GPIOD_OUT_LOW);
4089af512e8SLinus Walleij 	if (IS_ERR(gpiod)) {
4092976b179SSimon Guinot 		dev_err(dev,
4102976b179SSimon Guinot 			"Failed to get GPIO from DT property enable-gpio\n");
4119af512e8SLinus Walleij 		return PTR_ERR(gpiod);
4122976b179SSimon Guinot 	}
4139af512e8SLinus Walleij 	gpiod_set_consumer_name(gpiod, "GPIO extension enable");
4149af512e8SLinus Walleij 	gpio_ext->enable = gpiod;
4152976b179SSimon Guinot 
4169af512e8SLinus Walleij 	return devm_add_action_or_reset(dev, netxbig_gpio_ext_remove, gpio_ext);
4172976b179SSimon Guinot }
4182976b179SSimon Guinot 
netxbig_leds_get_of_pdata(struct device * dev,struct netxbig_led_platform_data * pdata)4192976b179SSimon Guinot static int netxbig_leds_get_of_pdata(struct device *dev,
4202976b179SSimon Guinot 				     struct netxbig_led_platform_data *pdata)
4212976b179SSimon Guinot {
4228853c95eSMarek Behún 	struct device_node *np = dev_of_node(dev);
4232976b179SSimon Guinot 	struct device_node *gpio_ext_np;
4249af512e8SLinus Walleij 	struct platform_device *gpio_ext_pdev;
4259af512e8SLinus Walleij 	struct device *gpio_ext_dev;
4262976b179SSimon Guinot 	struct device_node *child;
4272976b179SSimon Guinot 	struct netxbig_gpio_ext *gpio_ext;
4282976b179SSimon Guinot 	struct netxbig_led_timer *timers;
4292976b179SSimon Guinot 	struct netxbig_led *leds, *led;
4302976b179SSimon Guinot 	int num_timers;
4312976b179SSimon Guinot 	int num_leds = 0;
4322976b179SSimon Guinot 	int ret;
4332976b179SSimon Guinot 	int i;
4342976b179SSimon Guinot 
4352976b179SSimon Guinot 	/* GPIO extension */
4362976b179SSimon Guinot 	gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0);
4372976b179SSimon Guinot 	if (!gpio_ext_np) {
4382976b179SSimon Guinot 		dev_err(dev, "Failed to get DT handle gpio-ext\n");
4392976b179SSimon Guinot 		return -EINVAL;
4402976b179SSimon Guinot 	}
4419af512e8SLinus Walleij 	gpio_ext_pdev = of_find_device_by_node(gpio_ext_np);
4429af512e8SLinus Walleij 	if (!gpio_ext_pdev) {
4439af512e8SLinus Walleij 		dev_err(dev, "Failed to find platform device for gpio-ext\n");
4449af512e8SLinus Walleij 		return -ENODEV;
4459af512e8SLinus Walleij 	}
4469af512e8SLinus Walleij 	gpio_ext_dev = &gpio_ext_pdev->dev;
4472976b179SSimon Guinot 
4482976b179SSimon Guinot 	gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
449af7b6505SNishka Dasgupta 	if (!gpio_ext) {
450af7b6505SNishka Dasgupta 		of_node_put(gpio_ext_np);
451311066aaSYu Kuai 		ret = -ENOMEM;
452311066aaSYu Kuai 		goto put_device;
453af7b6505SNishka Dasgupta 	}
4549af512e8SLinus Walleij 	ret = netxbig_gpio_ext_get(dev, gpio_ext_dev, gpio_ext);
455af7b6505SNishka Dasgupta 	of_node_put(gpio_ext_np);
4562976b179SSimon Guinot 	if (ret)
457311066aaSYu Kuai 		goto put_device;
4582976b179SSimon Guinot 	pdata->gpio_ext = gpio_ext;
4592976b179SSimon Guinot 
4602976b179SSimon Guinot 	/* Timers (optional) */
4612976b179SSimon Guinot 	ret = of_property_count_u32_elems(np, "timers");
4622976b179SSimon Guinot 	if (ret > 0) {
463311066aaSYu Kuai 		if (ret % 3) {
464311066aaSYu Kuai 			ret = -EINVAL;
465311066aaSYu Kuai 			goto put_device;
466311066aaSYu Kuai 		}
467311066aaSYu Kuai 
4682976b179SSimon Guinot 		num_timers = ret / 3;
469a86854d0SKees Cook 		timers = devm_kcalloc(dev, num_timers, sizeof(*timers),
4702976b179SSimon Guinot 				      GFP_KERNEL);
471311066aaSYu Kuai 		if (!timers) {
472311066aaSYu Kuai 			ret = -ENOMEM;
473311066aaSYu Kuai 			goto put_device;
474311066aaSYu Kuai 		}
4752976b179SSimon Guinot 		for (i = 0; i < num_timers; i++) {
4762976b179SSimon Guinot 			u32 tmp;
4772976b179SSimon Guinot 
4782976b179SSimon Guinot 			of_property_read_u32_index(np, "timers", 3 * i,
4792976b179SSimon Guinot 						   &timers[i].mode);
480311066aaSYu Kuai 			if (timers[i].mode >= NETXBIG_LED_MODE_NUM) {
481311066aaSYu Kuai 				ret = -EINVAL;
482311066aaSYu Kuai 				goto put_device;
483311066aaSYu Kuai 			}
4842976b179SSimon Guinot 			of_property_read_u32_index(np, "timers",
4852976b179SSimon Guinot 						   3 * i + 1, &tmp);
4862976b179SSimon Guinot 			timers[i].delay_on = tmp;
4872976b179SSimon Guinot 			of_property_read_u32_index(np, "timers",
4882976b179SSimon Guinot 						   3 * i + 2, &tmp);
4892976b179SSimon Guinot 			timers[i].delay_off = tmp;
4902976b179SSimon Guinot 		}
4912976b179SSimon Guinot 		pdata->timer = timers;
4922976b179SSimon Guinot 		pdata->num_timer = num_timers;
4932976b179SSimon Guinot 	}
4942976b179SSimon Guinot 
4952976b179SSimon Guinot 	/* LEDs */
49699a013c8SMarek Behún 	num_leds = of_get_available_child_count(np);
4972976b179SSimon Guinot 	if (!num_leds) {
4982976b179SSimon Guinot 		dev_err(dev, "No LED subnodes found in DT\n");
499311066aaSYu Kuai 		ret = -ENODEV;
500311066aaSYu Kuai 		goto put_device;
5012976b179SSimon Guinot 	}
5022976b179SSimon Guinot 
503a86854d0SKees Cook 	leds = devm_kcalloc(dev, num_leds, sizeof(*leds), GFP_KERNEL);
504311066aaSYu Kuai 	if (!leds) {
505311066aaSYu Kuai 		ret = -ENOMEM;
506311066aaSYu Kuai 		goto put_device;
507311066aaSYu Kuai 	}
5082976b179SSimon Guinot 
5092976b179SSimon Guinot 	led = leds;
51099a013c8SMarek Behún 	for_each_available_child_of_node(np, child) {
5112976b179SSimon Guinot 		const char *string;
5122976b179SSimon Guinot 		int *mode_val;
5132976b179SSimon Guinot 		int num_modes;
5142976b179SSimon Guinot 
5152976b179SSimon Guinot 		ret = of_property_read_u32(child, "mode-addr",
5162976b179SSimon Guinot 					   &led->mode_addr);
5172976b179SSimon Guinot 		if (ret)
5182976b179SSimon Guinot 			goto err_node_put;
5192976b179SSimon Guinot 
5202976b179SSimon Guinot 		ret = of_property_read_u32(child, "bright-addr",
5212976b179SSimon Guinot 					   &led->bright_addr);
5222976b179SSimon Guinot 		if (ret)
5232976b179SSimon Guinot 			goto err_node_put;
5242976b179SSimon Guinot 
5252976b179SSimon Guinot 		ret = of_property_read_u32(child, "max-brightness",
5262976b179SSimon Guinot 					   &led->bright_max);
5272976b179SSimon Guinot 		if (ret)
5282976b179SSimon Guinot 			goto err_node_put;
5292976b179SSimon Guinot 
5302976b179SSimon Guinot 		mode_val =
531a86854d0SKees Cook 			devm_kcalloc(dev,
532a86854d0SKees Cook 				     NETXBIG_LED_MODE_NUM, sizeof(*mode_val),
5332976b179SSimon Guinot 				     GFP_KERNEL);
5342976b179SSimon Guinot 		if (!mode_val) {
5352976b179SSimon Guinot 			ret = -ENOMEM;
5362976b179SSimon Guinot 			goto err_node_put;
5372976b179SSimon Guinot 		}
5382976b179SSimon Guinot 
5392976b179SSimon Guinot 		for (i = 0; i < NETXBIG_LED_MODE_NUM; i++)
5402976b179SSimon Guinot 			mode_val[i] = NETXBIG_LED_INVALID_MODE;
5412976b179SSimon Guinot 
5422976b179SSimon Guinot 		ret = of_property_count_u32_elems(child, "mode-val");
5432976b179SSimon Guinot 		if (ret < 0 || ret % 2) {
5442976b179SSimon Guinot 			ret = -EINVAL;
5452976b179SSimon Guinot 			goto err_node_put;
5462976b179SSimon Guinot 		}
5472976b179SSimon Guinot 		num_modes = ret / 2;
5482976b179SSimon Guinot 		if (num_modes > NETXBIG_LED_MODE_NUM) {
5492976b179SSimon Guinot 			ret = -EINVAL;
5502976b179SSimon Guinot 			goto err_node_put;
5512976b179SSimon Guinot 		}
5522976b179SSimon Guinot 
5532976b179SSimon Guinot 		for (i = 0; i < num_modes; i++) {
5542976b179SSimon Guinot 			int mode;
5552976b179SSimon Guinot 			int val;
5562976b179SSimon Guinot 
5572976b179SSimon Guinot 			of_property_read_u32_index(child,
5582976b179SSimon Guinot 						   "mode-val", 2 * i, &mode);
5592976b179SSimon Guinot 			of_property_read_u32_index(child,
5602976b179SSimon Guinot 						   "mode-val", 2 * i + 1, &val);
5612976b179SSimon Guinot 			if (mode >= NETXBIG_LED_MODE_NUM) {
5622976b179SSimon Guinot 				ret = -EINVAL;
5632976b179SSimon Guinot 				goto err_node_put;
5642976b179SSimon Guinot 			}
5652976b179SSimon Guinot 			mode_val[mode] = val;
5662976b179SSimon Guinot 		}
5672976b179SSimon Guinot 		led->mode_val = mode_val;
5682976b179SSimon Guinot 
5692976b179SSimon Guinot 		if (!of_property_read_string(child, "label", &string))
5702976b179SSimon Guinot 			led->name = string;
5712976b179SSimon Guinot 		else
5722976b179SSimon Guinot 			led->name = child->name;
5732976b179SSimon Guinot 
5742976b179SSimon Guinot 		if (!of_property_read_string(child,
5752976b179SSimon Guinot 					     "linux,default-trigger", &string))
5762976b179SSimon Guinot 			led->default_trigger = string;
5772976b179SSimon Guinot 
5782976b179SSimon Guinot 		led++;
5792976b179SSimon Guinot 	}
5802976b179SSimon Guinot 
5812976b179SSimon Guinot 	pdata->leds = leds;
5822976b179SSimon Guinot 	pdata->num_leds = num_leds;
5832976b179SSimon Guinot 
5842976b179SSimon Guinot 	return 0;
5852976b179SSimon Guinot 
5862976b179SSimon Guinot err_node_put:
5872976b179SSimon Guinot 	of_node_put(child);
588311066aaSYu Kuai put_device:
589311066aaSYu Kuai 	put_device(gpio_ext_dev);
5902976b179SSimon Guinot 	return ret;
5912976b179SSimon Guinot }
5922976b179SSimon Guinot 
5932976b179SSimon Guinot static const struct of_device_id of_netxbig_leds_match[] = {
5942976b179SSimon Guinot 	{ .compatible = "lacie,netxbig-leds", },
5952976b179SSimon Guinot 	{},
5962976b179SSimon Guinot };
597825fe38aSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, of_netxbig_leds_match);
5982976b179SSimon Guinot 
netxbig_led_probe(struct platform_device * pdev)59998ea1ea2SBill Pemberton static int netxbig_led_probe(struct platform_device *pdev)
60024467832SSimon Guinot {
601156189a6SMasahiro Yamada 	struct netxbig_led_platform_data *pdata;
602cd010955SSimon Guinot 	struct netxbig_led_data *leds_data;
60324467832SSimon Guinot 	int i;
60424467832SSimon Guinot 	int ret;
60524467832SSimon Guinot 
6062976b179SSimon Guinot 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
60724467832SSimon Guinot 	if (!pdata)
60824467832SSimon Guinot 		return -ENOMEM;
6092976b179SSimon Guinot 	ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata);
6102976b179SSimon Guinot 	if (ret)
6112976b179SSimon Guinot 		return ret;
6122976b179SSimon Guinot 
613a86854d0SKees Cook 	leds_data = devm_kcalloc(&pdev->dev,
614a86854d0SKees Cook 				 pdata->num_leds, sizeof(*leds_data),
6152976b179SSimon Guinot 				 GFP_KERNEL);
616cd010955SSimon Guinot 	if (!leds_data)
6172976b179SSimon Guinot 		return -ENOMEM;
61824467832SSimon Guinot 
61924467832SSimon Guinot 	for (i = 0; i < pdata->num_leds; i++) {
620cd010955SSimon Guinot 		ret = create_netxbig_led(pdev, pdata,
621cd010955SSimon Guinot 					 &leds_data[i], &pdata->leds[i]);
62224467832SSimon Guinot 		if (ret < 0)
62324467832SSimon Guinot 			return ret;
62424467832SSimon Guinot 	}
62524467832SSimon Guinot 
62624467832SSimon Guinot 	return 0;
62724467832SSimon Guinot }
62824467832SSimon Guinot 
62924467832SSimon Guinot static struct platform_driver netxbig_led_driver = {
63024467832SSimon Guinot 	.probe		= netxbig_led_probe,
63124467832SSimon Guinot 	.driver		= {
63224467832SSimon Guinot 		.name		= "leds-netxbig",
633156189a6SMasahiro Yamada 		.of_match_table	= of_netxbig_leds_match,
63424467832SSimon Guinot 	},
63524467832SSimon Guinot };
63624467832SSimon Guinot 
637892a8843SAxel Lin module_platform_driver(netxbig_led_driver);
63824467832SSimon Guinot 
63924467832SSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
64024467832SSimon Guinot MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
64124467832SSimon Guinot MODULE_LICENSE("GPL");
642892a8843SAxel Lin MODULE_ALIAS("platform:leds-netxbig");
643