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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * leds-tca6507
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * The TCA6507 is a programmable LED controller that can drive 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * separate lines either by holding them low, or by pulsing them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * with modulated width.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * The modulation can be varied in a simple pattern to produce a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * blink or double-blink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * This driver can configure each line either as a 'GPIO' which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * out-only (pull-up resistor required) or as an LED with variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * brightness and hardware-assisted blinking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Apart from OFF and ON there are three programmable brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * levels which can be programmed from 0 to 15 and indicate how many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * 500usec intervals in each 8msec that the led is 'on'.  The levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * are named MASTER, BANK0 and BANK1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * There are two different blink rates that can be programmed, each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * with separate time for rise, on, fall, off and second-off.  Thus if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * 3 or more different non-trivial rates are required, software must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * be used for the extra rates. The two different blink rates must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * align with the two levels BANK0 and BANK1.  This driver does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * support double-blink so 'second-off' always matches 'off'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Only 16 different times can be programmed in a roughly logarithmic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * scale from 64ms to 16320ms.  To be precise the possible times are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *    0, 64, 128, 192, 256, 384, 512, 768,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *    1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Times that cannot be closely matched with these must be handled in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * software.  This driver allows 12.5% error in matching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * This driver does not allow rise/fall rates to be set explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * When trying to match a given 'on' or 'off' period, an appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * pair of 'change' and 'hold' times are chosen to get a close match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * If the target delay is even, the 'change' number will be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * smaller; if odd, the 'hold' number will be the smaller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Choosing pairs of delays with 12.5% errors allows us to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * delays in the ranges: 56-72, 112-144, 168-216, 224-27504,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * 28560-36720.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * 26% of the achievable sums can be matched by multiple pairings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * For example 1536 == 1536+0, 1024+512, or 768+768.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * This driver will always choose the pairing with the least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * maximum - 768+768 in this case.  Other pairings are not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * Access to the 3 levels and 2 blinks are on a first-come,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * first-served basis.  Access can be shared by multiple leds if they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * have the same level and either same blink rates, or some don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * blink.  When a led changes, it relinquishes access and tries again,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * so it might lose access to hardware blink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * If a blink engine cannot be allocated, software blink is used.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * the desired brightness cannot be allocated, the closest available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * non-zero brightness is used.  As 'full' is always available, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * worst case would be to have two different blink rates at '1', with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * Max at '2', then other leds will have to choose between '2' and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * '16'.  Hopefully this is not likely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * brightness and LEDs using the blink.  It can only be reprogrammed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * when the appropriate counter is zero.  The MASTER level has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * single usage count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * Each LED has programmable 'on' and 'off' time as milliseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * With each there is a flag saying if it was explicitly requested or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * defaulted.  Similarly the banks know if each time was explicit or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * default.  Defaults are permitted to be changed freely - they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * not recognised when matching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* LED select registers determine the source that drives LED outputs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define TCA6507_LS_LED_OFF	0x0	/* Output HI-Z (off) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define TCA6507_LS_LED_OFF1	0x1	/* Output HI-Z (off) - not used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define TCA6507_LS_LED_PWM0	0x2	/* Output LOW with Bank0 rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define TCA6507_LS_LED_PWM1	0x3	/* Output LOW with Bank1 rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define TCA6507_LS_LED_ON	0x4	/* Output LOW (on) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #define TCA6507_LS_LED_MIR	0x5	/* Output LOW with Master Intensity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define TCA6507_LS_BLINK0	0x6	/* Blink at Bank0 rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define TCA6507_LS_BLINK1	0x7	/* Blink at Bank1 rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct tca6507_platform_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct led_platform_data leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define	TCA6507_MAKE_GPIO 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	BANK0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	BANK1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	MASTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int bank_source[3] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	TCA6507_LS_LED_PWM0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	TCA6507_LS_LED_PWM1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	TCA6507_LS_LED_MIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int blink_source[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	TCA6507_LS_BLINK0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	TCA6507_LS_BLINK1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* PWM registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define	TCA6507_REG_CNT			11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * owns one bit in each register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define	TCA6507_FADE_ON			0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define	TCA6507_FULL_ON			0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define	TCA6507_FADE_OFF		0x05
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define	TCA6507_FIRST_OFF		0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define	TCA6507_SECOND_OFF		0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define	TCA6507_MAX_INTENSITY		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define	TCA6507_MASTER_INTENSITY	0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define	TCA6507_INITIALIZE		0x0A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define	INIT_CODE			0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define TIMECODES 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int time_codes[TIMECODES] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	0, 64, 128, 192, 256, 384, 512, 768,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static inline int TO_LEVEL(int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return brightness >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* ...and convert back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline int TO_BRIGHT(int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return (level << 4) | 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define NUM_LEDS 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct tca6507_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int			reg_set;	/* One bit per register where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 						 * a '1' means the register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 						 * should be written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	u8			reg_file[TCA6507_REG_CNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/* Bank 2 is Master Intensity and doesn't use times */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct bank {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		int ontime, offtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		int on_dflt, off_dflt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		int time_use, level_use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	} bank[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct work_struct	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct tca6507_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		struct tca6507_chip	*chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		struct led_classdev	led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		int			num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		int			ontime, offtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		int			on_dflt, off_dflt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		int			bank;	/* Bank used, or -1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		int			blink;	/* Set if hardware-blinking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	} leds[NUM_LEDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct gpio_chip		gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int				gpio_map[NUM_LEDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct i2c_device_id tca6507_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	{ "tca6507" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) MODULE_DEVICE_TABLE(i2c, tca6507_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int choose_times(int msec, int *c1p, int *c2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 * Choose two timecodes which add to 'msec' as near as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * possible.  The first returned is the 'on' or 'off' time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * The second is to be used as a 'fade-on' or 'fade-off' time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 * If 'msec' is even, the first will not be smaller than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	 * second.  If 'msec' is odd, the first will not be larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * than the second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * If we cannot get a sum within 1/8 of 'msec' fail with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * -EINVAL, otherwise return the sum that was achieved, plus 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * if the first is smaller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * If two possibilities are equally good (e.g. 512+0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 * 256+256), choose the first pair so there is more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * change-time visible (i.e. it is softer).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	int tmax = msec * 9 / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	int tmin = msec * 7 / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int diff = 65536;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	/* We start at '1' to ensure we never even think of choosing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * total time of '0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	for (c1 = 1; c1 < TIMECODES; c1++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		int t = time_codes[c1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (t*2 < tmin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		if (t > tmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		for (c2 = 0; c2 <= c1; c2++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			int tt = t + time_codes[c2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			int d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			if (tt < tmin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			if (tt > tmax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			/* This works! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			d = abs(msec - tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			if (d >= diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			/* Best yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			*c1p = c1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			*c2p = c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			diff = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			if (d == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 				return msec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (diff < 65536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (msec & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			c1 = *c2p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			*c2p = *c1p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			*c1p = c1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		actual = time_codes[*c1p] + time_codes[*c2p];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (*c1p < *c2p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			return actual + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			return actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* No close match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * Update the register file with the appropriate 3-bit state for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * given led.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void set_select(struct tca6507_chip *tca, int led, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	int mask = (1 << led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	for (bit = 0; bit < 3; bit++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		int n = tca->reg_file[bit] & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		if (val & (1 << bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			n |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		if (tca->reg_file[bit] != n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			tca->reg_file[bit] = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			tca->reg_set |= (1 << bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Update the register file with the appropriate 4-bit code for one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * bank or other.  This can be used for timers, for levels, or for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	int mask = 0xF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (bank) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		mask <<= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		new <<= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	n = tca->reg_file[reg] & ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	n |= new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (tca->reg_file[reg] != n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		tca->reg_file[reg] = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		tca->reg_set |= 1 << reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* Update brightness level. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static void set_level(struct tca6507_chip *tca, int bank, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	switch (bank) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	case BANK0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	case BANK1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	case MASTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	tca->bank[bank].level = level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Record all relevant time codes for a given bank */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static void set_times(struct tca6507_chip *tca, int bank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	int c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	result = choose_times(tca->bank[bank].ontime, &c1, &c2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	dev_dbg(&tca->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		"Chose on  times %d(%d) %d(%d) for %dms\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		c1, time_codes[c1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		c2, time_codes[c2], tca->bank[bank].ontime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	set_code(tca, TCA6507_FADE_ON, bank, c2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	set_code(tca, TCA6507_FULL_ON, bank, c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	tca->bank[bank].ontime = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	result = choose_times(tca->bank[bank].offtime, &c1, &c2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	dev_dbg(&tca->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		"Chose off times %d(%d) %d(%d) for %dms\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		c1, time_codes[c1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		c2, time_codes[c2], tca->bank[bank].offtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	set_code(tca, TCA6507_FADE_OFF, bank, c2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	set_code(tca, TCA6507_FIRST_OFF, bank, c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	set_code(tca, TCA6507_SECOND_OFF, bank, c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	tca->bank[bank].offtime = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /* Write all needed register of tca6507 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static void tca6507_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 						work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct i2c_client *cl = tca->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	u8 file[TCA6507_REG_CNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	spin_lock_irq(&tca->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	set = tca->reg_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	memcpy(file, tca->reg_file, TCA6507_REG_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	tca->reg_set = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	spin_unlock_irq(&tca->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	for (r = 0; r < TCA6507_REG_CNT; r++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		if (set & (1<<r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			i2c_smbus_write_byte_data(cl, r, file[r]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static void led_release(struct tca6507_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* If led owns any resource, release it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct tca6507_chip *tca = led->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (led->bank >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		struct bank *b = tca->bank + led->bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if (led->blink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			b->time_use--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		b->level_use--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	led->blink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	led->bank = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int led_prepare(struct tca6507_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	/* Assign this led to a bank, configuring that bank if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 * necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int level = TO_LEVEL(led->led_cdev.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	struct tca6507_chip *tca = led->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	int c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct bank *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	int need_init = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	led->led_cdev.brightness = TO_BRIGHT(level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (level == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		set_select(tca, led->num, TCA6507_LS_LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (led->ontime == 0 || led->offtime == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		 * Just set the brightness, choosing first usable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		 * bank.  If none perfect, choose best.  Count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		 * backwards so we check MASTER bank first to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		 * wasting a timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		int best = -1;/* full-on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		int diff = 15-level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (level == 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			set_select(tca, led->num, TCA6507_LS_LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		for (i = MASTER; i >= BANK0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			int d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			if (tca->bank[i].level == level ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			    tca->bank[i].level_use == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 				best = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			d = abs(level - tca->bank[i].level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			if (d < diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				diff = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 				best = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (best == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			/* Best brightness is full-on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			set_select(tca, led->num, TCA6507_LS_LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			led->led_cdev.brightness = LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		if (!tca->bank[best].level_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			set_level(tca, best, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		tca->bank[best].level_use++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		led->bank = best;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		set_select(tca, led->num, bank_source[best]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	 * We have on/off time so we need to try to allocate a timing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 * bank.  First check if times are compatible with hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * and give up if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (choose_times(led->ontime, &c1, &c2) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (choose_times(led->offtime, &c1, &c2) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	for (i = BANK0; i <= BANK1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (tca->bank[i].level_use == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			/* not in use - it is ours! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		if (tca->bank[i].level != level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			/* Incompatible level - skip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			/* FIX: if timer matches we maybe should consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			 * this anyway...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		if (tca->bank[i].time_use == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			/* Timer not in use, and level matches - use it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		if (!(tca->bank[i].on_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		      led->on_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		      tca->bank[i].ontime == led->ontime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			/* on time is incompatible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		if (!(tca->bank[i].off_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		      led->off_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		      tca->bank[i].offtime == led->offtime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			/* off time is incompatible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		/* looks like a suitable match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (i > BANK1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		/* Nothing matches - how sad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	b = &tca->bank[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (b->level_use == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		set_level(tca, i, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	b->level_use++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	led->bank = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (b->on_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	    !led->on_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	    b->time_use == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		b->ontime = led->ontime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		b->on_dflt = led->on_dflt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		need_init = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (b->off_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	    !led->off_dflt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	    b->time_use == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		b->offtime = led->offtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		b->off_dflt = led->off_dflt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		need_init = 1;
^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) 	if (need_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		set_times(tca, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	led->ontime = b->ontime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	led->offtime = b->offtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	b->time_use++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	led->blink = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	led->led_cdev.brightness = TO_BRIGHT(b->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	set_select(tca, led->num, blink_source[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static int led_assign(struct tca6507_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	struct tca6507_chip *tca = led->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	spin_lock_irqsave(&tca->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	led_release(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	err = led_prepare(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		 * Can only fail on timer setup.  In that case we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		 * to re-establish as steady level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		led->ontime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		led->offtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		led_prepare(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	spin_unlock_irqrestore(&tca->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (tca->reg_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		schedule_work(&tca->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static void tca6507_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 				   enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 					       led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	led->led_cdev.brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	led->ontime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	led->offtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	led_assign(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static int tca6507_blink_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			     unsigned long *delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			     unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 					       led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (*delay_on == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		led->on_dflt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	else if (delay_on != &led_cdev->blink_delay_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		led->on_dflt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	led->ontime = *delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (*delay_off == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		led->off_dflt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	else if (delay_off != &led_cdev->blink_delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		led->off_dflt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	led->offtime = *delay_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	if (led->ontime == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		led->ontime = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (led->offtime == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		led->offtime = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	if (led->led_cdev.brightness == LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		led->led_cdev.brightness = LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	if (led_assign(led) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		led->ontime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		led->offtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		led->led_cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	*delay_on = led->ontime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	*delay_off = led->offtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static void tca6507_gpio_set_value(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				   unsigned offset, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	struct tca6507_chip *tca = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	spin_lock_irqsave(&tca->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	 * 'OFF' is floating high, and 'ON' is pulled down, so it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	 * the inverse sense of 'val'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	set_select(tca, tca->gpio_map[offset],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		   val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	spin_unlock_irqrestore(&tca->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	if (tca->reg_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		schedule_work(&tca->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static int tca6507_gpio_direction_output(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 					  unsigned offset, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	tca6507_gpio_set_value(gc, offset, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static int tca6507_probe_gpios(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 			       struct tca6507_chip *tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 			       struct tca6507_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	int gpios = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	for (i = 0; i < NUM_LEDS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 			/* Configure as a gpio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			tca->gpio_map[gpios] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 			gpios++;
^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) 	if (!gpios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	tca->gpio.label = "gpio-tca6507";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	tca->gpio.ngpio = gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	tca->gpio.base = pdata->gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	tca->gpio.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	tca->gpio.direction_output = tca6507_gpio_direction_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	tca->gpio.set = tca6507_gpio_set_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	tca->gpio.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) #ifdef CONFIG_OF_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	tca->gpio.of_node = of_node_get(dev_of_node(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	err = gpiochip_add_data(&tca->gpio, tca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		tca->gpio.ngpio = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static void tca6507_remove_gpio(struct tca6507_chip *tca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	if (tca->gpio.ngpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		gpiochip_remove(&tca->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) #else /* CONFIG_GPIOLIB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static int tca6507_probe_gpios(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			       struct tca6507_chip *tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			       struct tca6507_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static void tca6507_remove_gpio(struct tca6507_chip *tca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) #endif /* CONFIG_GPIOLIB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) static struct tca6507_platform_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) tca6507_led_dt_init(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	struct tca6507_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	struct led_info *tca_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	count = device_get_child_node_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	if (!count || count > NUM_LEDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	if (!tca_leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	device_for_each_child_node(dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		struct led_info led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		if (fwnode_property_read_string(child, "label", &led.name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			led.name = fwnode_get_name(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		fwnode_property_read_string(child, "linux,default-trigger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 					    &led.default_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		led.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		if (fwnode_property_match_string(child, "compatible",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 						 "gpio") >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			led.flags |= TCA6507_MAKE_GPIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		ret = fwnode_property_read_u32(child, "reg", &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		if (ret || reg >= NUM_LEDS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			return ERR_PTR(ret ? : -EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		tca_leds[reg] = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 			     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	pdata->leds.leds = tca_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	pdata->leds.num_leds = NUM_LEDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) #ifdef CONFIG_GPIOLIB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	pdata->gpio_base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	return pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	{ .compatible = "ti,tca6507", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) static int tca6507_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	struct i2c_adapter *adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	struct tca6507_chip *tca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	struct tca6507_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	pdata = tca6507_led_dt_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	if (IS_ERR(pdata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		return PTR_ERR(pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	if (!tca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	tca->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	INIT_WORK(&tca->work, tca6507_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	spin_lock_init(&tca->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	i2c_set_clientdata(client, tca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	for (i = 0; i < NUM_LEDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		struct tca6507_led *l = tca->leds + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		l->chip = tca;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		l->num = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 			l->led_cdev.name = pdata->leds.leds[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 			l->led_cdev.default_trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 				= pdata->leds.leds[i].default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			l->led_cdev.brightness_set = tca6507_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 			l->led_cdev.blink_set = tca6507_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 			l->bank = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 			err = led_classdev_register(dev, &l->led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 				goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	err = tca6507_probe_gpios(dev, tca, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	/* set all registers to known state - zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	tca->reg_set = 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	schedule_work(&tca->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	while (i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 		if (tca->leds[i].led_cdev.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 			led_classdev_unregister(&tca->leds[i].led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) static int tca6507_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	struct tca6507_chip *tca = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	struct tca6507_led *tca_leds = tca->leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	for (i = 0; i < NUM_LEDS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		if (tca_leds[i].led_cdev.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 			led_classdev_unregister(&tca_leds[i].led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	tca6507_remove_gpio(tca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	cancel_work_sync(&tca->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) static struct i2c_driver tca6507_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	.driver   = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		.name    = "leds-tca6507",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 		.of_match_table = of_match_ptr(of_tca6507_leds_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	.probe    = tca6507_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	.remove   = tca6507_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	.id_table = tca6507_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) module_i2c_driver(tca6507_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) MODULE_LICENSE("GPL v2");