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> 1524467832SSimon Guinot #include <linux/gpio.h> 162976b179SSimon Guinot #include <linux/of_gpio.h> 1724467832SSimon Guinot #include <linux/leds.h> 18156189a6SMasahiro Yamada 19156189a6SMasahiro Yamada struct netxbig_gpio_ext { 20156189a6SMasahiro Yamada unsigned int *addr; 21156189a6SMasahiro Yamada int num_addr; 22156189a6SMasahiro Yamada unsigned int *data; 23156189a6SMasahiro Yamada int num_data; 24156189a6SMasahiro Yamada unsigned int enable; 25156189a6SMasahiro Yamada }; 26156189a6SMasahiro Yamada 27156189a6SMasahiro Yamada enum netxbig_led_mode { 28156189a6SMasahiro Yamada NETXBIG_LED_OFF, 29156189a6SMasahiro Yamada NETXBIG_LED_ON, 30156189a6SMasahiro Yamada NETXBIG_LED_SATA, 31156189a6SMasahiro Yamada NETXBIG_LED_TIMER1, 32156189a6SMasahiro Yamada NETXBIG_LED_TIMER2, 33156189a6SMasahiro Yamada NETXBIG_LED_MODE_NUM, 34156189a6SMasahiro Yamada }; 35156189a6SMasahiro Yamada 36156189a6SMasahiro Yamada #define NETXBIG_LED_INVALID_MODE NETXBIG_LED_MODE_NUM 37156189a6SMasahiro Yamada 38156189a6SMasahiro Yamada struct netxbig_led_timer { 39156189a6SMasahiro Yamada unsigned long delay_on; 40156189a6SMasahiro Yamada unsigned long delay_off; 41156189a6SMasahiro Yamada enum netxbig_led_mode mode; 42156189a6SMasahiro Yamada }; 43156189a6SMasahiro Yamada 44156189a6SMasahiro Yamada struct netxbig_led { 45156189a6SMasahiro Yamada const char *name; 46156189a6SMasahiro Yamada const char *default_trigger; 47156189a6SMasahiro Yamada int mode_addr; 48156189a6SMasahiro Yamada int *mode_val; 49156189a6SMasahiro Yamada int bright_addr; 50156189a6SMasahiro Yamada int bright_max; 51156189a6SMasahiro Yamada }; 52156189a6SMasahiro Yamada 53156189a6SMasahiro Yamada struct netxbig_led_platform_data { 54156189a6SMasahiro Yamada struct netxbig_gpio_ext *gpio_ext; 55156189a6SMasahiro Yamada struct netxbig_led_timer *timer; 56156189a6SMasahiro Yamada int num_timer; 57156189a6SMasahiro Yamada struct netxbig_led *leds; 58156189a6SMasahiro Yamada int num_leds; 59156189a6SMasahiro Yamada }; 6024467832SSimon Guinot 6124467832SSimon Guinot /* 6224467832SSimon Guinot * GPIO extension bus. 6324467832SSimon Guinot */ 6424467832SSimon Guinot 6524467832SSimon Guinot static DEFINE_SPINLOCK(gpio_ext_lock); 6624467832SSimon Guinot 6724467832SSimon Guinot static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr) 6824467832SSimon Guinot { 6924467832SSimon Guinot int pin; 7024467832SSimon Guinot 7124467832SSimon Guinot for (pin = 0; pin < gpio_ext->num_addr; pin++) 7224467832SSimon Guinot gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1); 7324467832SSimon Guinot } 7424467832SSimon Guinot 7524467832SSimon Guinot static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data) 7624467832SSimon Guinot { 7724467832SSimon Guinot int pin; 7824467832SSimon Guinot 7924467832SSimon Guinot for (pin = 0; pin < gpio_ext->num_data; pin++) 8024467832SSimon Guinot gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1); 8124467832SSimon Guinot } 8224467832SSimon Guinot 8324467832SSimon Guinot static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext) 8424467832SSimon Guinot { 8524467832SSimon Guinot /* Enable select is done on the raising edge. */ 8624467832SSimon Guinot gpio_set_value(gpio_ext->enable, 0); 8724467832SSimon Guinot gpio_set_value(gpio_ext->enable, 1); 8824467832SSimon Guinot } 8924467832SSimon Guinot 9024467832SSimon Guinot static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext, 9124467832SSimon Guinot int addr, int value) 9224467832SSimon Guinot { 9324467832SSimon Guinot unsigned long flags; 9424467832SSimon Guinot 9524467832SSimon Guinot spin_lock_irqsave(&gpio_ext_lock, flags); 9624467832SSimon Guinot gpio_ext_set_addr(gpio_ext, addr); 9724467832SSimon Guinot gpio_ext_set_data(gpio_ext, value); 9824467832SSimon Guinot gpio_ext_enable_select(gpio_ext); 9924467832SSimon Guinot spin_unlock_irqrestore(&gpio_ext_lock, flags); 10024467832SSimon Guinot } 10124467832SSimon Guinot 102cd010955SSimon Guinot static int gpio_ext_init(struct platform_device *pdev, 103cd010955SSimon Guinot struct netxbig_gpio_ext *gpio_ext) 10424467832SSimon Guinot { 10524467832SSimon Guinot int err; 10624467832SSimon Guinot int i; 10724467832SSimon Guinot 10824467832SSimon Guinot if (unlikely(!gpio_ext)) 10924467832SSimon Guinot return -EINVAL; 11024467832SSimon Guinot 11124467832SSimon Guinot /* Configure address GPIOs. */ 11224467832SSimon Guinot for (i = 0; i < gpio_ext->num_addr; i++) { 113cd010955SSimon Guinot err = devm_gpio_request_one(&pdev->dev, gpio_ext->addr[i], 114cd010955SSimon Guinot GPIOF_OUT_INIT_LOW, 115b96a573fSAxel Lin "GPIO extension addr"); 11624467832SSimon Guinot if (err) 117cd010955SSimon Guinot return err; 11824467832SSimon Guinot } 11924467832SSimon Guinot /* Configure data GPIOs. */ 12024467832SSimon Guinot for (i = 0; i < gpio_ext->num_data; i++) { 121cd010955SSimon Guinot err = devm_gpio_request_one(&pdev->dev, gpio_ext->data[i], 122cd010955SSimon Guinot GPIOF_OUT_INIT_LOW, 123b96a573fSAxel Lin "GPIO extension data"); 12424467832SSimon Guinot if (err) 12524467832SSimon Guinot return err; 12624467832SSimon Guinot } 127cd010955SSimon Guinot /* Configure "enable select" GPIO. */ 128cd010955SSimon Guinot err = devm_gpio_request_one(&pdev->dev, gpio_ext->enable, 129cd010955SSimon Guinot GPIOF_OUT_INIT_LOW, 130cd010955SSimon Guinot "GPIO extension enable"); 131cd010955SSimon Guinot if (err) 132cd010955SSimon Guinot return err; 13324467832SSimon Guinot 134cd010955SSimon Guinot return 0; 13524467832SSimon Guinot } 13624467832SSimon Guinot 13724467832SSimon Guinot /* 13824467832SSimon Guinot * Class LED driver. 13924467832SSimon Guinot */ 14024467832SSimon Guinot 14124467832SSimon Guinot struct netxbig_led_data { 14224467832SSimon Guinot struct netxbig_gpio_ext *gpio_ext; 14324467832SSimon Guinot struct led_classdev cdev; 14424467832SSimon Guinot int mode_addr; 14524467832SSimon Guinot int *mode_val; 14624467832SSimon Guinot int bright_addr; 14724467832SSimon Guinot struct netxbig_led_timer *timer; 14824467832SSimon Guinot int num_timer; 14924467832SSimon Guinot enum netxbig_led_mode mode; 15024467832SSimon Guinot int sata; 15124467832SSimon Guinot spinlock_t lock; 15224467832SSimon Guinot }; 15324467832SSimon Guinot 15424467832SSimon Guinot static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode, 15524467832SSimon Guinot unsigned long delay_on, 15624467832SSimon Guinot unsigned long delay_off, 15724467832SSimon Guinot struct netxbig_led_timer *timer, 15824467832SSimon Guinot int num_timer) 15924467832SSimon Guinot { 16024467832SSimon Guinot int i; 16124467832SSimon Guinot 16224467832SSimon Guinot for (i = 0; i < num_timer; i++) { 16324467832SSimon Guinot if (timer[i].delay_on == delay_on && 16424467832SSimon Guinot timer[i].delay_off == delay_off) { 16524467832SSimon Guinot *mode = timer[i].mode; 16624467832SSimon Guinot return 0; 16724467832SSimon Guinot } 16824467832SSimon Guinot } 16924467832SSimon Guinot return -EINVAL; 17024467832SSimon Guinot } 17124467832SSimon Guinot 17224467832SSimon Guinot static int netxbig_led_blink_set(struct led_classdev *led_cdev, 17324467832SSimon Guinot unsigned long *delay_on, 17424467832SSimon Guinot unsigned long *delay_off) 17524467832SSimon Guinot { 17624467832SSimon Guinot struct netxbig_led_data *led_dat = 17724467832SSimon Guinot container_of(led_cdev, struct netxbig_led_data, cdev); 17824467832SSimon Guinot enum netxbig_led_mode mode; 17924467832SSimon Guinot int mode_val; 18024467832SSimon Guinot int ret; 18124467832SSimon Guinot 18224467832SSimon Guinot /* Look for a LED mode with the requested timer frequency. */ 18324467832SSimon Guinot ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off, 18424467832SSimon Guinot led_dat->timer, led_dat->num_timer); 18524467832SSimon Guinot if (ret < 0) 18624467832SSimon Guinot return ret; 18724467832SSimon Guinot 18824467832SSimon Guinot mode_val = led_dat->mode_val[mode]; 18924467832SSimon Guinot if (mode_val == NETXBIG_LED_INVALID_MODE) 19024467832SSimon Guinot return -EINVAL; 19124467832SSimon Guinot 19224467832SSimon Guinot spin_lock_irq(&led_dat->lock); 19324467832SSimon Guinot 19424467832SSimon Guinot gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val); 19524467832SSimon Guinot led_dat->mode = mode; 19624467832SSimon Guinot 19724467832SSimon Guinot spin_unlock_irq(&led_dat->lock); 19824467832SSimon Guinot 19924467832SSimon Guinot return 0; 20024467832SSimon Guinot } 20124467832SSimon Guinot 20224467832SSimon Guinot static void netxbig_led_set(struct led_classdev *led_cdev, 20324467832SSimon Guinot enum led_brightness value) 20424467832SSimon Guinot { 20524467832SSimon Guinot struct netxbig_led_data *led_dat = 20624467832SSimon Guinot container_of(led_cdev, struct netxbig_led_data, cdev); 20724467832SSimon Guinot enum netxbig_led_mode mode; 2087b9d9d88SSimon Guinot int mode_val; 20924467832SSimon Guinot int set_brightness = 1; 21024467832SSimon Guinot unsigned long flags; 21124467832SSimon Guinot 21224467832SSimon Guinot spin_lock_irqsave(&led_dat->lock, flags); 21324467832SSimon Guinot 21424467832SSimon Guinot if (value == LED_OFF) { 21524467832SSimon Guinot mode = NETXBIG_LED_OFF; 21624467832SSimon Guinot set_brightness = 0; 21724467832SSimon Guinot } else { 21824467832SSimon Guinot if (led_dat->sata) 21924467832SSimon Guinot mode = NETXBIG_LED_SATA; 22024467832SSimon Guinot else if (led_dat->mode == NETXBIG_LED_OFF) 22124467832SSimon Guinot mode = NETXBIG_LED_ON; 22224467832SSimon Guinot else /* Keep 'timer' mode. */ 22324467832SSimon Guinot mode = led_dat->mode; 22424467832SSimon Guinot } 22524467832SSimon Guinot mode_val = led_dat->mode_val[mode]; 22624467832SSimon Guinot 22724467832SSimon Guinot gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val); 22824467832SSimon Guinot led_dat->mode = mode; 22924467832SSimon Guinot /* 23024467832SSimon Guinot * Note that the brightness register is shared between all the 23124467832SSimon Guinot * SATA LEDs. So, change the brightness setting for a single 23224467832SSimon Guinot * SATA LED will affect all the others. 23324467832SSimon Guinot */ 2347b9d9d88SSimon Guinot if (set_brightness) 23524467832SSimon Guinot gpio_ext_set_value(led_dat->gpio_ext, 2367b9d9d88SSimon Guinot led_dat->bright_addr, value); 23724467832SSimon Guinot 23824467832SSimon Guinot spin_unlock_irqrestore(&led_dat->lock, flags); 23924467832SSimon Guinot } 24024467832SSimon Guinot 24124467832SSimon Guinot static ssize_t netxbig_led_sata_store(struct device *dev, 24224467832SSimon Guinot struct device_attribute *attr, 24324467832SSimon Guinot const char *buff, size_t count) 24424467832SSimon Guinot { 24524467832SSimon Guinot struct led_classdev *led_cdev = dev_get_drvdata(dev); 24624467832SSimon Guinot struct netxbig_led_data *led_dat = 24724467832SSimon Guinot container_of(led_cdev, struct netxbig_led_data, cdev); 24824467832SSimon Guinot unsigned long enable; 24924467832SSimon Guinot enum netxbig_led_mode mode; 25024467832SSimon Guinot int mode_val; 25124467832SSimon Guinot int ret; 25224467832SSimon Guinot 2537517611aSJingoo Han ret = kstrtoul(buff, 10, &enable); 25424467832SSimon Guinot if (ret < 0) 25524467832SSimon Guinot return ret; 25624467832SSimon Guinot 25724467832SSimon Guinot enable = !!enable; 25824467832SSimon Guinot 25924467832SSimon Guinot spin_lock_irq(&led_dat->lock); 26024467832SSimon Guinot 26124467832SSimon Guinot if (led_dat->sata == enable) { 26224467832SSimon Guinot ret = count; 26324467832SSimon Guinot goto exit_unlock; 26424467832SSimon Guinot } 26524467832SSimon Guinot 26624467832SSimon Guinot if (led_dat->mode != NETXBIG_LED_ON && 26724467832SSimon Guinot led_dat->mode != NETXBIG_LED_SATA) 26824467832SSimon Guinot mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */ 26924467832SSimon Guinot else if (enable) 27024467832SSimon Guinot mode = NETXBIG_LED_SATA; 27124467832SSimon Guinot else 27224467832SSimon Guinot mode = NETXBIG_LED_ON; 27324467832SSimon Guinot 27424467832SSimon Guinot mode_val = led_dat->mode_val[mode]; 27524467832SSimon Guinot if (mode_val == NETXBIG_LED_INVALID_MODE) { 27624467832SSimon Guinot ret = -EINVAL; 27724467832SSimon Guinot goto exit_unlock; 27824467832SSimon Guinot } 27924467832SSimon Guinot 28024467832SSimon Guinot gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val); 28124467832SSimon Guinot led_dat->mode = mode; 28224467832SSimon Guinot led_dat->sata = enable; 28324467832SSimon Guinot 28424467832SSimon Guinot ret = count; 28524467832SSimon Guinot 28624467832SSimon Guinot exit_unlock: 28724467832SSimon Guinot spin_unlock_irq(&led_dat->lock); 28824467832SSimon Guinot 28924467832SSimon Guinot return ret; 29024467832SSimon Guinot } 29124467832SSimon Guinot 29224467832SSimon Guinot static ssize_t netxbig_led_sata_show(struct device *dev, 29324467832SSimon Guinot struct device_attribute *attr, char *buf) 29424467832SSimon Guinot { 29524467832SSimon Guinot struct led_classdev *led_cdev = dev_get_drvdata(dev); 29624467832SSimon Guinot struct netxbig_led_data *led_dat = 29724467832SSimon Guinot container_of(led_cdev, struct netxbig_led_data, cdev); 29824467832SSimon Guinot 29924467832SSimon Guinot return sprintf(buf, "%d\n", led_dat->sata); 30024467832SSimon Guinot } 30124467832SSimon Guinot 30224467832SSimon Guinot static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store); 30324467832SSimon Guinot 304588a6a99SJohan Hovold static struct attribute *netxbig_led_attrs[] = { 305588a6a99SJohan Hovold &dev_attr_sata.attr, 306588a6a99SJohan Hovold NULL 307588a6a99SJohan Hovold }; 308588a6a99SJohan Hovold ATTRIBUTE_GROUPS(netxbig_led); 309588a6a99SJohan Hovold 310cd010955SSimon Guinot static int create_netxbig_led(struct platform_device *pdev, 311cd010955SSimon Guinot struct netxbig_led_platform_data *pdata, 312cd010955SSimon Guinot struct netxbig_led_data *led_dat, 313cd010955SSimon Guinot const struct netxbig_led *template) 31424467832SSimon Guinot { 31524467832SSimon Guinot spin_lock_init(&led_dat->lock); 31624467832SSimon Guinot led_dat->gpio_ext = pdata->gpio_ext; 31724467832SSimon Guinot led_dat->cdev.name = template->name; 31824467832SSimon Guinot led_dat->cdev.default_trigger = template->default_trigger; 31924467832SSimon Guinot led_dat->cdev.blink_set = netxbig_led_blink_set; 32024467832SSimon Guinot led_dat->cdev.brightness_set = netxbig_led_set; 32124467832SSimon Guinot /* 32224467832SSimon Guinot * Because the GPIO extension bus don't allow to read registers 32324467832SSimon Guinot * value, there is no way to probe the LED initial state. 32424467832SSimon Guinot * So, the initial sysfs LED value for the "brightness" and "sata" 32524467832SSimon Guinot * attributes are inconsistent. 32624467832SSimon Guinot * 32724467832SSimon Guinot * Note that the initial LED state can't be reconfigured. 32824467832SSimon Guinot * The reason is that the LED behaviour must stay uniform during 32924467832SSimon Guinot * the whole boot process (bootloader+linux). 33024467832SSimon Guinot */ 33124467832SSimon Guinot led_dat->sata = 0; 33224467832SSimon Guinot led_dat->cdev.brightness = LED_OFF; 3337b9d9d88SSimon Guinot led_dat->cdev.max_brightness = template->bright_max; 33424467832SSimon Guinot led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME; 33524467832SSimon Guinot led_dat->mode_addr = template->mode_addr; 33624467832SSimon Guinot led_dat->mode_val = template->mode_val; 33724467832SSimon Guinot led_dat->bright_addr = template->bright_addr; 33824467832SSimon Guinot led_dat->timer = pdata->timer; 33924467832SSimon Guinot led_dat->num_timer = pdata->num_timer; 3400c86ac2cSSimon Guinot /* 3410c86ac2cSSimon Guinot * If available, expose the SATA activity blink capability through 3420c86ac2cSSimon Guinot * a "sata" sysfs attribute. 3430c86ac2cSSimon Guinot */ 3440c86ac2cSSimon Guinot if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE) 3450c86ac2cSSimon Guinot led_dat->cdev.groups = netxbig_led_groups; 34624467832SSimon Guinot 347cd010955SSimon Guinot return devm_led_classdev_register(&pdev->dev, &led_dat->cdev); 34824467832SSimon Guinot } 34924467832SSimon Guinot 3502976b179SSimon Guinot static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np, 3512976b179SSimon Guinot struct netxbig_gpio_ext *gpio_ext) 3522976b179SSimon Guinot { 3532976b179SSimon Guinot int *addr, *data; 3542976b179SSimon Guinot int num_addr, num_data; 3552976b179SSimon Guinot int ret; 3562976b179SSimon Guinot int i; 3572976b179SSimon Guinot 3582976b179SSimon Guinot ret = of_gpio_named_count(np, "addr-gpios"); 3592976b179SSimon Guinot if (ret < 0) { 3602976b179SSimon Guinot dev_err(dev, 3612976b179SSimon Guinot "Failed to count GPIOs in DT property addr-gpios\n"); 3622976b179SSimon Guinot return ret; 3632976b179SSimon Guinot } 3642976b179SSimon Guinot num_addr = ret; 365a86854d0SKees Cook addr = devm_kcalloc(dev, num_addr, sizeof(*addr), GFP_KERNEL); 3662976b179SSimon Guinot if (!addr) 3672976b179SSimon Guinot return -ENOMEM; 3682976b179SSimon Guinot 3692976b179SSimon Guinot for (i = 0; i < num_addr; i++) { 3702976b179SSimon Guinot ret = of_get_named_gpio(np, "addr-gpios", i); 3712976b179SSimon Guinot if (ret < 0) 3722976b179SSimon Guinot return ret; 3732976b179SSimon Guinot addr[i] = ret; 3742976b179SSimon Guinot } 3752976b179SSimon Guinot gpio_ext->addr = addr; 3762976b179SSimon Guinot gpio_ext->num_addr = num_addr; 3772976b179SSimon Guinot 3782976b179SSimon Guinot ret = of_gpio_named_count(np, "data-gpios"); 3792976b179SSimon Guinot if (ret < 0) { 3802976b179SSimon Guinot dev_err(dev, 3812976b179SSimon Guinot "Failed to count GPIOs in DT property data-gpios\n"); 3822976b179SSimon Guinot return ret; 3832976b179SSimon Guinot } 3842976b179SSimon Guinot num_data = ret; 385a86854d0SKees Cook data = devm_kcalloc(dev, num_data, sizeof(*data), GFP_KERNEL); 3862976b179SSimon Guinot if (!data) 3872976b179SSimon Guinot return -ENOMEM; 3882976b179SSimon Guinot 3892976b179SSimon Guinot for (i = 0; i < num_data; i++) { 3902976b179SSimon Guinot ret = of_get_named_gpio(np, "data-gpios", i); 3912976b179SSimon Guinot if (ret < 0) 3922976b179SSimon Guinot return ret; 3932976b179SSimon Guinot data[i] = ret; 3942976b179SSimon Guinot } 3952976b179SSimon Guinot gpio_ext->data = data; 3962976b179SSimon Guinot gpio_ext->num_data = num_data; 3972976b179SSimon Guinot 3982976b179SSimon Guinot ret = of_get_named_gpio(np, "enable-gpio", 0); 3992976b179SSimon Guinot if (ret < 0) { 4002976b179SSimon Guinot dev_err(dev, 4012976b179SSimon Guinot "Failed to get GPIO from DT property enable-gpio\n"); 4022976b179SSimon Guinot return ret; 4032976b179SSimon Guinot } 4042976b179SSimon Guinot gpio_ext->enable = ret; 4052976b179SSimon Guinot 4062976b179SSimon Guinot return 0; 4072976b179SSimon Guinot } 4082976b179SSimon Guinot 4092976b179SSimon Guinot static int netxbig_leds_get_of_pdata(struct device *dev, 4102976b179SSimon Guinot struct netxbig_led_platform_data *pdata) 4112976b179SSimon Guinot { 4122976b179SSimon Guinot struct device_node *np = dev->of_node; 4132976b179SSimon Guinot struct device_node *gpio_ext_np; 4142976b179SSimon Guinot struct device_node *child; 4152976b179SSimon Guinot struct netxbig_gpio_ext *gpio_ext; 4162976b179SSimon Guinot struct netxbig_led_timer *timers; 4172976b179SSimon Guinot struct netxbig_led *leds, *led; 4182976b179SSimon Guinot int num_timers; 4192976b179SSimon Guinot int num_leds = 0; 4202976b179SSimon Guinot int ret; 4212976b179SSimon Guinot int i; 4222976b179SSimon Guinot 4232976b179SSimon Guinot /* GPIO extension */ 4242976b179SSimon Guinot gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0); 4252976b179SSimon Guinot if (!gpio_ext_np) { 4262976b179SSimon Guinot dev_err(dev, "Failed to get DT handle gpio-ext\n"); 4272976b179SSimon Guinot return -EINVAL; 4282976b179SSimon Guinot } 4292976b179SSimon Guinot 4302976b179SSimon Guinot gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL); 431af7b6505SNishka Dasgupta if (!gpio_ext) { 432af7b6505SNishka Dasgupta of_node_put(gpio_ext_np); 4332976b179SSimon Guinot return -ENOMEM; 434af7b6505SNishka Dasgupta } 4352976b179SSimon Guinot ret = gpio_ext_get_of_pdata(dev, gpio_ext_np, gpio_ext); 436af7b6505SNishka Dasgupta of_node_put(gpio_ext_np); 4372976b179SSimon Guinot if (ret) 4382976b179SSimon Guinot return ret; 4392976b179SSimon Guinot pdata->gpio_ext = gpio_ext; 4402976b179SSimon Guinot 4412976b179SSimon Guinot /* Timers (optional) */ 4422976b179SSimon Guinot ret = of_property_count_u32_elems(np, "timers"); 4432976b179SSimon Guinot if (ret > 0) { 4442976b179SSimon Guinot if (ret % 3) 4452976b179SSimon Guinot return -EINVAL; 4462976b179SSimon Guinot num_timers = ret / 3; 447a86854d0SKees Cook timers = devm_kcalloc(dev, num_timers, sizeof(*timers), 4482976b179SSimon Guinot GFP_KERNEL); 4492976b179SSimon Guinot if (!timers) 4502976b179SSimon Guinot return -ENOMEM; 4512976b179SSimon Guinot for (i = 0; i < num_timers; i++) { 4522976b179SSimon Guinot u32 tmp; 4532976b179SSimon Guinot 4542976b179SSimon Guinot of_property_read_u32_index(np, "timers", 3 * i, 4552976b179SSimon Guinot &timers[i].mode); 4562976b179SSimon Guinot if (timers[i].mode >= NETXBIG_LED_MODE_NUM) 4572976b179SSimon Guinot return -EINVAL; 4582976b179SSimon Guinot of_property_read_u32_index(np, "timers", 4592976b179SSimon Guinot 3 * i + 1, &tmp); 4602976b179SSimon Guinot timers[i].delay_on = tmp; 4612976b179SSimon Guinot of_property_read_u32_index(np, "timers", 4622976b179SSimon Guinot 3 * i + 2, &tmp); 4632976b179SSimon Guinot timers[i].delay_off = tmp; 4642976b179SSimon Guinot } 4652976b179SSimon Guinot pdata->timer = timers; 4662976b179SSimon Guinot pdata->num_timer = num_timers; 4672976b179SSimon Guinot } 4682976b179SSimon Guinot 4692976b179SSimon Guinot /* LEDs */ 4702976b179SSimon Guinot num_leds = of_get_child_count(np); 4712976b179SSimon Guinot if (!num_leds) { 4722976b179SSimon Guinot dev_err(dev, "No LED subnodes found in DT\n"); 4732976b179SSimon Guinot return -ENODEV; 4742976b179SSimon Guinot } 4752976b179SSimon Guinot 476a86854d0SKees Cook leds = devm_kcalloc(dev, num_leds, sizeof(*leds), GFP_KERNEL); 4772976b179SSimon Guinot if (!leds) 4782976b179SSimon Guinot return -ENOMEM; 4792976b179SSimon Guinot 4802976b179SSimon Guinot led = leds; 4812976b179SSimon Guinot for_each_child_of_node(np, child) { 4822976b179SSimon Guinot const char *string; 4832976b179SSimon Guinot int *mode_val; 4842976b179SSimon Guinot int num_modes; 4852976b179SSimon Guinot 4862976b179SSimon Guinot ret = of_property_read_u32(child, "mode-addr", 4872976b179SSimon Guinot &led->mode_addr); 4882976b179SSimon Guinot if (ret) 4892976b179SSimon Guinot goto err_node_put; 4902976b179SSimon Guinot 4912976b179SSimon Guinot ret = of_property_read_u32(child, "bright-addr", 4922976b179SSimon Guinot &led->bright_addr); 4932976b179SSimon Guinot if (ret) 4942976b179SSimon Guinot goto err_node_put; 4952976b179SSimon Guinot 4962976b179SSimon Guinot ret = of_property_read_u32(child, "max-brightness", 4972976b179SSimon Guinot &led->bright_max); 4982976b179SSimon Guinot if (ret) 4992976b179SSimon Guinot goto err_node_put; 5002976b179SSimon Guinot 5012976b179SSimon Guinot mode_val = 502a86854d0SKees Cook devm_kcalloc(dev, 503a86854d0SKees Cook NETXBIG_LED_MODE_NUM, sizeof(*mode_val), 5042976b179SSimon Guinot GFP_KERNEL); 5052976b179SSimon Guinot if (!mode_val) { 5062976b179SSimon Guinot ret = -ENOMEM; 5072976b179SSimon Guinot goto err_node_put; 5082976b179SSimon Guinot } 5092976b179SSimon Guinot 5102976b179SSimon Guinot for (i = 0; i < NETXBIG_LED_MODE_NUM; i++) 5112976b179SSimon Guinot mode_val[i] = NETXBIG_LED_INVALID_MODE; 5122976b179SSimon Guinot 5132976b179SSimon Guinot ret = of_property_count_u32_elems(child, "mode-val"); 5142976b179SSimon Guinot if (ret < 0 || ret % 2) { 5152976b179SSimon Guinot ret = -EINVAL; 5162976b179SSimon Guinot goto err_node_put; 5172976b179SSimon Guinot } 5182976b179SSimon Guinot num_modes = ret / 2; 5192976b179SSimon Guinot if (num_modes > NETXBIG_LED_MODE_NUM) { 5202976b179SSimon Guinot ret = -EINVAL; 5212976b179SSimon Guinot goto err_node_put; 5222976b179SSimon Guinot } 5232976b179SSimon Guinot 5242976b179SSimon Guinot for (i = 0; i < num_modes; i++) { 5252976b179SSimon Guinot int mode; 5262976b179SSimon Guinot int val; 5272976b179SSimon Guinot 5282976b179SSimon Guinot of_property_read_u32_index(child, 5292976b179SSimon Guinot "mode-val", 2 * i, &mode); 5302976b179SSimon Guinot of_property_read_u32_index(child, 5312976b179SSimon Guinot "mode-val", 2 * i + 1, &val); 5322976b179SSimon Guinot if (mode >= NETXBIG_LED_MODE_NUM) { 5332976b179SSimon Guinot ret = -EINVAL; 5342976b179SSimon Guinot goto err_node_put; 5352976b179SSimon Guinot } 5362976b179SSimon Guinot mode_val[mode] = val; 5372976b179SSimon Guinot } 5382976b179SSimon Guinot led->mode_val = mode_val; 5392976b179SSimon Guinot 5402976b179SSimon Guinot if (!of_property_read_string(child, "label", &string)) 5412976b179SSimon Guinot led->name = string; 5422976b179SSimon Guinot else 5432976b179SSimon Guinot led->name = child->name; 5442976b179SSimon Guinot 5452976b179SSimon Guinot if (!of_property_read_string(child, 5462976b179SSimon Guinot "linux,default-trigger", &string)) 5472976b179SSimon Guinot led->default_trigger = string; 5482976b179SSimon Guinot 5492976b179SSimon Guinot led++; 5502976b179SSimon Guinot } 5512976b179SSimon Guinot 5522976b179SSimon Guinot pdata->leds = leds; 5532976b179SSimon Guinot pdata->num_leds = num_leds; 5542976b179SSimon Guinot 5552976b179SSimon Guinot return 0; 5562976b179SSimon Guinot 5572976b179SSimon Guinot err_node_put: 5582976b179SSimon Guinot of_node_put(child); 5592976b179SSimon Guinot return ret; 5602976b179SSimon Guinot } 5612976b179SSimon Guinot 5622976b179SSimon Guinot static const struct of_device_id of_netxbig_leds_match[] = { 5632976b179SSimon Guinot { .compatible = "lacie,netxbig-leds", }, 5642976b179SSimon Guinot {}, 5652976b179SSimon Guinot }; 566825fe38aSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, of_netxbig_leds_match); 5672976b179SSimon Guinot 56898ea1ea2SBill Pemberton static int netxbig_led_probe(struct platform_device *pdev) 56924467832SSimon Guinot { 570156189a6SMasahiro Yamada struct netxbig_led_platform_data *pdata; 571cd010955SSimon Guinot struct netxbig_led_data *leds_data; 57224467832SSimon Guinot int i; 57324467832SSimon Guinot int ret; 57424467832SSimon Guinot 5752976b179SSimon Guinot pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 57624467832SSimon Guinot if (!pdata) 57724467832SSimon Guinot return -ENOMEM; 5782976b179SSimon Guinot ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata); 5792976b179SSimon Guinot if (ret) 5802976b179SSimon Guinot return ret; 5812976b179SSimon Guinot 582a86854d0SKees Cook leds_data = devm_kcalloc(&pdev->dev, 583a86854d0SKees Cook pdata->num_leds, sizeof(*leds_data), 5842976b179SSimon Guinot GFP_KERNEL); 585cd010955SSimon Guinot if (!leds_data) 5862976b179SSimon Guinot return -ENOMEM; 58724467832SSimon Guinot 588cd010955SSimon Guinot ret = gpio_ext_init(pdev, pdata->gpio_ext); 58924467832SSimon Guinot if (ret < 0) 5908095c385SBryan Wu return ret; 59124467832SSimon Guinot 59224467832SSimon Guinot for (i = 0; i < pdata->num_leds; i++) { 593cd010955SSimon Guinot ret = create_netxbig_led(pdev, pdata, 594cd010955SSimon Guinot &leds_data[i], &pdata->leds[i]); 59524467832SSimon Guinot if (ret < 0) 59624467832SSimon Guinot return ret; 59724467832SSimon Guinot } 59824467832SSimon Guinot 59924467832SSimon Guinot return 0; 60024467832SSimon Guinot } 60124467832SSimon Guinot 60224467832SSimon Guinot static struct platform_driver netxbig_led_driver = { 60324467832SSimon Guinot .probe = netxbig_led_probe, 60424467832SSimon Guinot .driver = { 60524467832SSimon Guinot .name = "leds-netxbig", 606156189a6SMasahiro Yamada .of_match_table = of_netxbig_leds_match, 60724467832SSimon Guinot }, 60824467832SSimon Guinot }; 60924467832SSimon Guinot 610892a8843SAxel Lin module_platform_driver(netxbig_led_driver); 61124467832SSimon Guinot 61224467832SSimon Guinot MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>"); 61324467832SSimon Guinot MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards"); 61424467832SSimon Guinot MODULE_LICENSE("GPL"); 615892a8843SAxel Lin MODULE_ALIAS("platform:leds-netxbig"); 616