Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 LaCie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Simon Guinot <sguinot@lacie.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct netxbig_gpio_ext {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct gpio_desc **addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	int		num_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct gpio_desc **data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int		num_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct gpio_desc *enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) enum netxbig_led_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	NETXBIG_LED_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	NETXBIG_LED_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	NETXBIG_LED_SATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	NETXBIG_LED_TIMER1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	NETXBIG_LED_TIMER2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	NETXBIG_LED_MODE_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define NETXBIG_LED_INVALID_MODE NETXBIG_LED_MODE_NUM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct netxbig_led_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned long		delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned long		delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	enum netxbig_led_mode	mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct netxbig_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	const char	*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	const char	*default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int		mode_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int		*mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int		bright_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int		bright_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct netxbig_led_platform_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct netxbig_gpio_ext	*gpio_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct netxbig_led_timer *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int			num_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct netxbig_led	*leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int			num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * GPIO extension bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static DEFINE_SPINLOCK(gpio_ext_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	for (pin = 0; pin < gpio_ext->num_addr; pin++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		gpiod_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	for (pin = 0; pin < gpio_ext->num_data; pin++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		gpiod_set_value(gpio_ext->data[pin], (data >> pin) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	/* Enable select is done on the raising edge. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	gpiod_set_value(gpio_ext->enable, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	gpiod_set_value(gpio_ext->enable, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			       int addr, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	spin_lock_irqsave(&gpio_ext_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	gpio_ext_set_addr(gpio_ext, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	gpio_ext_set_data(gpio_ext, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	gpio_ext_enable_select(gpio_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	spin_unlock_irqrestore(&gpio_ext_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * Class LED driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct netxbig_led_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct netxbig_gpio_ext	*gpio_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct led_classdev	cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int			mode_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int			*mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int			bright_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct			netxbig_led_timer *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int			num_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	enum netxbig_led_mode	mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int			sata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				      unsigned long delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				      unsigned long delay_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 				      struct netxbig_led_timer *timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 				      int num_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	for (i = 0; i < num_timer; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (timer[i].delay_on == delay_on &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		    timer[i].delay_off == delay_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			*mode = timer[i].mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int netxbig_led_blink_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 				 unsigned long *delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				 unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct netxbig_led_data *led_dat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		container_of(led_cdev, struct netxbig_led_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	enum netxbig_led_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Look for a LED mode with the requested timer frequency. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 					 led_dat->timer, led_dat->num_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	mode_val = led_dat->mode_val[mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (mode_val == NETXBIG_LED_INVALID_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	spin_lock_irq(&led_dat->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	led_dat->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	spin_unlock_irq(&led_dat->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void netxbig_led_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			    enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct netxbig_led_data *led_dat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		container_of(led_cdev, struct netxbig_led_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	enum netxbig_led_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int set_brightness = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	spin_lock_irqsave(&led_dat->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (value == LED_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		mode = NETXBIG_LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		set_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (led_dat->sata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			mode = NETXBIG_LED_SATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		else if (led_dat->mode == NETXBIG_LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			mode = NETXBIG_LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		else /* Keep 'timer' mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			mode = led_dat->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	mode_val = led_dat->mode_val[mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	led_dat->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 * Note that the brightness register is shared between all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * SATA LEDs. So, change the brightness setting for a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * SATA LED will affect all the others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (set_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		gpio_ext_set_value(led_dat->gpio_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				   led_dat->bright_addr, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	spin_unlock_irqrestore(&led_dat->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static ssize_t netxbig_led_sata_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				      const char *buff, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct netxbig_led_data *led_dat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		container_of(led_cdev, struct netxbig_led_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	unsigned long enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	enum netxbig_led_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	int mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	ret = kstrtoul(buff, 10, &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	enable = !!enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	spin_lock_irq(&led_dat->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (led_dat->sata == enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (led_dat->mode != NETXBIG_LED_ON &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	    led_dat->mode != NETXBIG_LED_SATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	else if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		mode = NETXBIG_LED_SATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		mode = NETXBIG_LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	mode_val = led_dat->mode_val[mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (mode_val == NETXBIG_LED_INVALID_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		goto exit_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	led_dat->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	led_dat->sata = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) exit_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	spin_unlock_irq(&led_dat->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static ssize_t netxbig_led_sata_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct netxbig_led_data *led_dat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		container_of(led_cdev, struct netxbig_led_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return sprintf(buf, "%d\n", led_dat->sata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static struct attribute *netxbig_led_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	&dev_attr_sata.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ATTRIBUTE_GROUPS(netxbig_led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int create_netxbig_led(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			      struct netxbig_led_platform_data *pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			      struct netxbig_led_data *led_dat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			      const struct netxbig_led *template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	spin_lock_init(&led_dat->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	led_dat->gpio_ext = pdata->gpio_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	led_dat->cdev.name = template->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	led_dat->cdev.default_trigger = template->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	led_dat->cdev.blink_set = netxbig_led_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	led_dat->cdev.brightness_set = netxbig_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * Because the GPIO extension bus don't allow to read registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * value, there is no way to probe the LED initial state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * So, the initial sysfs LED value for the "brightness" and "sata"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * attributes are inconsistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * Note that the initial LED state can't be reconfigured.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 * The reason is that the LED behaviour must stay uniform during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 * the whole boot process (bootloader+linux).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	led_dat->sata = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	led_dat->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	led_dat->cdev.max_brightness = template->bright_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	led_dat->mode_addr = template->mode_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	led_dat->mode_val = template->mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	led_dat->bright_addr = template->bright_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	led_dat->timer = pdata->timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	led_dat->num_timer = pdata->num_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	 * If available, expose the SATA activity blink capability through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	 * a "sata" sysfs attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		led_dat->cdev.groups = netxbig_led_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * netxbig_gpio_ext_remove() - Clean up GPIO extension data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * @data: managed resource data to clean up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * Since we pick GPIO descriptors from another device than the device our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * driver is probing to, we need to register a specific callback to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * these up using managed resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void netxbig_gpio_ext_remove(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct netxbig_gpio_ext *gpio_ext = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	for (i = 0; i < gpio_ext->num_addr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		gpiod_put(gpio_ext->addr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	for (i = 0; i < gpio_ext->num_data; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		gpiod_put(gpio_ext->data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	gpiod_put(gpio_ext->enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * netxbig_gpio_ext_get() - Obtain GPIO extension device data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * @dev: main LED device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * @gpio_ext_dev: the GPIO extension device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * @gpio_ext: the data structure holding the GPIO extension data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * This function walks the subdevice that only contain GPIO line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * handles in the device tree and obtains the GPIO descriptors from that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static int netxbig_gpio_ext_get(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				struct device *gpio_ext_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				struct netxbig_gpio_ext *gpio_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct gpio_desc **addr, **data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int num_addr, num_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	ret = gpiod_count(gpio_ext_dev, "addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			"Failed to count GPIOs in DT property addr-gpios\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	num_addr = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	addr = devm_kcalloc(dev, num_addr, sizeof(*addr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 * We cannot use devm_ managed resources with these GPIO descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * since they are associated with the "GPIO extension device" which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 * does not probe any driver. The device tree parser will however
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 * populate a platform device for it so we can anyway obtain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	 * GPIO descriptors from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	for (i = 0; i < num_addr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		gpiod = gpiod_get_index(gpio_ext_dev, "addr", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		if (IS_ERR(gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			return PTR_ERR(gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		gpiod_set_consumer_name(gpiod, "GPIO extension addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		addr[i] = gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	gpio_ext->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	gpio_ext->num_addr = num_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	ret = gpiod_count(gpio_ext_dev, "data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			"Failed to count GPIOs in DT property data-gpios\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	num_data = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	data = devm_kcalloc(dev, num_data, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	for (i = 0; i < num_data; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		gpiod = gpiod_get_index(gpio_ext_dev, "data", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 					GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		if (IS_ERR(gpiod))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			return PTR_ERR(gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		gpiod_set_consumer_name(gpiod, "GPIO extension data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		data[i] = gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	gpio_ext->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	gpio_ext->num_data = num_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	gpiod = gpiod_get(gpio_ext_dev, "enable", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (IS_ERR(gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			"Failed to get GPIO from DT property enable-gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return PTR_ERR(gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	gpiod_set_consumer_name(gpiod, "GPIO extension enable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	gpio_ext->enable = gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return devm_add_action_or_reset(dev, netxbig_gpio_ext_remove, gpio_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int netxbig_leds_get_of_pdata(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				     struct netxbig_led_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct device_node *np = dev_of_node(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct device_node *gpio_ext_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct platform_device *gpio_ext_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct device *gpio_ext_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct netxbig_gpio_ext *gpio_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	struct netxbig_led_timer *timers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct netxbig_led *leds, *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	int num_timers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	int num_leds = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	/* GPIO extension */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (!gpio_ext_np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		dev_err(dev, "Failed to get DT handle gpio-ext\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	gpio_ext_pdev = of_find_device_by_node(gpio_ext_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (!gpio_ext_pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		dev_err(dev, "Failed to find platform device for gpio-ext\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	gpio_ext_dev = &gpio_ext_pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (!gpio_ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		of_node_put(gpio_ext_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	ret = netxbig_gpio_ext_get(dev, gpio_ext_dev, gpio_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	of_node_put(gpio_ext_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	pdata->gpio_ext = gpio_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/* Timers (optional) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	ret = of_property_count_u32_elems(np, "timers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		if (ret % 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		num_timers = ret / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		timers = devm_kcalloc(dev, num_timers, sizeof(*timers),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		if (!timers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		for (i = 0; i < num_timers; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			of_property_read_u32_index(np, "timers", 3 * i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 						   &timers[i].mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			if (timers[i].mode >= NETXBIG_LED_MODE_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 				goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			of_property_read_u32_index(np, "timers",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 						   3 * i + 1, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			timers[i].delay_on = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			of_property_read_u32_index(np, "timers",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 						   3 * i + 2, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			timers[i].delay_off = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		pdata->timer = timers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		pdata->num_timer = num_timers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	/* LEDs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	num_leds = of_get_available_child_count(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if (!num_leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		dev_err(dev, "No LED subnodes found in DT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	leds = devm_kcalloc(dev, num_leds, sizeof(*leds), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (!leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		goto put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	led = leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		const char *string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		int *mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		int num_modes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		ret = of_property_read_u32(child, "mode-addr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 					   &led->mode_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		ret = of_property_read_u32(child, "bright-addr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 					   &led->bright_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		ret = of_property_read_u32(child, "max-brightness",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 					   &led->bright_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		mode_val =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 				     NETXBIG_LED_MODE_NUM, sizeof(*mode_val),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		if (!mode_val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		for (i = 0; i < NETXBIG_LED_MODE_NUM; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			mode_val[i] = NETXBIG_LED_INVALID_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		ret = of_property_count_u32_elems(child, "mode-val");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		if (ret < 0 || ret % 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		num_modes = ret / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		if (num_modes > NETXBIG_LED_MODE_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		for (i = 0; i < num_modes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			of_property_read_u32_index(child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 						   "mode-val", 2 * i, &mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			of_property_read_u32_index(child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 						   "mode-val", 2 * i + 1, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			if (mode >= NETXBIG_LED_MODE_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 				goto err_node_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			mode_val[mode] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		led->mode_val = mode_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		if (!of_property_read_string(child, "label", &string))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			led->name = string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			led->name = child->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		if (!of_property_read_string(child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 					     "linux,default-trigger", &string))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			led->default_trigger = string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		led++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	pdata->leds = leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	pdata->num_leds = num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err_node_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) put_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	put_device(gpio_ext_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static const struct of_device_id of_netxbig_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	{ .compatible = "lacie,netxbig-leds", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) MODULE_DEVICE_TABLE(of, of_netxbig_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int netxbig_led_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct netxbig_led_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct netxbig_led_data *leds_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	leds_data = devm_kcalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 				 pdata->num_leds, sizeof(*leds_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 				 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	if (!leds_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	for (i = 0; i < pdata->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		ret = create_netxbig_led(pdev, pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 					 &leds_data[i], &pdata->leds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static struct platform_driver netxbig_led_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	.probe		= netxbig_led_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		.name		= "leds-netxbig",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		.of_match_table	= of_netxbig_leds_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) module_platform_driver(netxbig_led_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) MODULE_ALIAS("platform:leds-netxbig");