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+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Driver for Panasonic AN30259A 3-channel LED driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) // Copyright (c) 2018 Simon Shields <simon@lineageos.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) // Datasheet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) // https://www.alliedelec.com/m/d/a9d2b3ee87c2d1a535a41dd747b1c247.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define AN30259A_MAX_LEDS 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define AN30259A_REG_SRESET 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define AN30259A_LED_SRESET BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* LED power registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define AN30259A_REG_LED_ON 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define AN30259A_LED_EN(x) BIT((x) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define AN30259A_LED_SLOPE(x) BIT(((x) - 1) + 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define AN30259A_REG_LEDCC(x) (0x03 + ((x) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* slope control registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define AN30259A_REG_SLOPE(x) (0x06 + ((x) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define AN30259A_LED_SLOPETIME1(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define AN30259A_LED_SLOPETIME2(x) ((x) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define AN30259A_REG_LEDCNT1(x) (0x09 + (4 * ((x) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define AN30259A_LED_DUTYMAX(x) ((x) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define AN30259A_LED_DUTYMID(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define AN30259A_REG_LEDCNT2(x) (0x0A + (4 * ((x) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define AN30259A_LED_DELAY(x) ((x) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define AN30259A_LED_DUTYMIN(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /* detention time control (length of each slope step) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define AN30259A_REG_LEDCNT3(x) (0x0B + (4 * ((x) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define AN30259A_LED_DT1(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define AN30259A_LED_DT2(x) ((x) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define AN30259A_REG_LEDCNT4(x) (0x0C + (4 * ((x) - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define AN30259A_LED_DT3(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define AN30259A_LED_DT4(x) ((x) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define AN30259A_REG_MAX 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define AN30259A_BLINK_MAX_TIME 7500 /* ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define AN30259A_SLOPE_RESOLUTION 500 /* ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define AN30259A_NAME "an30259a"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define STATE_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define STATE_KEEP 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define STATE_ON 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) struct an30259a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) struct an30259a_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct an30259a *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct fwnode_handle *fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct led_classdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u32 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u32 default_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	bool sloping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) struct an30259a {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct mutex mutex; /* held when writing to registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct an30259a_led leds[AN30259A_MAX_LEDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int an30259a_brightness_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				   enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct an30259a_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned int led_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	led = container_of(cdev, struct an30259a_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	mutex_lock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	switch (brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	case LED_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		led_on &= ~AN30259A_LED_EN(led->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		led_on &= ~AN30259A_LED_SLOPE(led->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		led->sloping = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		led_on |= AN30259A_LED_EN(led->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		if (led->sloping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			led_on |= AN30259A_LED_SLOPE(led->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		ret = regmap_write(led->chip->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				   AN30259A_REG_LEDCNT1(led->num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				   AN30259A_LED_DUTYMAX(0xf) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				   AN30259A_LED_DUTYMID(0xf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCC(led->num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			   brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mutex_unlock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int an30259a_blink_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			      unsigned long *delay_off, unsigned long *delay_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct an30259a_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	int ret, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned int led_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned long off = *delay_off, on = *delay_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	led = container_of(cdev, struct an30259a_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	mutex_lock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	num = led->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* slope time can only be a multiple of 500ms. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (off % AN30259A_SLOPE_RESOLUTION || on % AN30259A_SLOPE_RESOLUTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	/* up to a maximum of 7500ms. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (off > AN30259A_BLINK_MAX_TIME || on > AN30259A_BLINK_MAX_TIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* if no blink specified, default to 1 Hz. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (!off && !on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		*delay_off = off = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		*delay_on = on = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	/* convert into values the HW will understand. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	off /= AN30259A_SLOPE_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	on /= AN30259A_SLOPE_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	/* duty min should be zero (=off), delay should be zero. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT2(num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			   AN30259A_LED_DELAY(0) | AN30259A_LED_DUTYMIN(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* reset detention time (no "breathing" effect). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT3(num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			   AN30259A_LED_DT1(0) | AN30259A_LED_DT2(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT4(num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			   AN30259A_LED_DT3(0) | AN30259A_LED_DT4(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* slope time controls on/off cycle length. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_SLOPE(num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			   AN30259A_LED_SLOPETIME1(off) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			   AN30259A_LED_SLOPETIME2(on));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	/* Finally, enable slope mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	led_on |= AN30259A_LED_SLOPE(num) | AN30259A_LED_EN(led->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		led->sloping = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	mutex_unlock(&led->chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int an30259a_dt_init(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			    struct an30259a *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct device_node *np = dev_of_node(&client->dev), *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int count, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct an30259a_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	count = of_get_available_child_count(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (!count || count > AN30259A_MAX_LEDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		u32 source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		ret = of_property_read_u32(child, "reg", &source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (ret != 0 || !source || source > AN30259A_MAX_LEDS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			dev_err(&client->dev, "Couldn't read LED address: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		led = &chip->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		led->num = source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		led->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		led->fwnode = of_fwnode_handle(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		if (!of_property_read_string(child, "default-state", &str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			if (!strcmp(str, "on"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				led->default_state = STATE_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			else if (!strcmp(str, "keep"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				led->default_state = STATE_KEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				led->default_state = STATE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	chip->num_leds = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static const struct regmap_config an30259a_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	.max_register = AN30259A_REG_MAX,
^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 void an30259a_init_default_state(struct an30259a_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct an30259a *chip = led->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int led_on, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	switch (led->default_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	case STATE_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		led->cdev.brightness = LED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	case STATE_KEEP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		err = regmap_read(chip->regmap, AN30259A_REG_LED_ON, &led_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		if (!(led_on & AN30259A_LED_EN(led->num))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			led->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		regmap_read(chip->regmap, AN30259A_REG_LEDCC(led->num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			    &led->cdev.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		led->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	an30259a_brightness_set(&led->cdev, led->cdev.brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int an30259a_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct an30259a *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	err = an30259a_dt_init(client, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	mutex_init(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	chip->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	i2c_set_clientdata(client, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	chip->regmap = devm_regmap_init_i2c(client, &an30259a_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (IS_ERR(chip->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		err = PTR_ERR(chip->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	for (i = 0; i < chip->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		an30259a_init_default_state(&chip->leds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		chip->leds[i].cdev.brightness_set_blocking =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			an30259a_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		chip->leds[i].cdev.blink_set = an30259a_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		init_data.fwnode = chip->leds[i].fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		init_data.devicename = AN30259A_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		init_data.default_label = ":";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		err = devm_led_classdev_register_ext(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 						 &chip->leds[i].cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 						 &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	mutex_destroy(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return err;
^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) static int an30259a_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct an30259a *chip = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	mutex_destroy(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static const struct of_device_id an30259a_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	{ .compatible = "panasonic,an30259a", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	{ /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) MODULE_DEVICE_TABLE(of, an30259a_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static const struct i2c_device_id an30259a_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	{ "an30259a", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	{ /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) MODULE_DEVICE_TABLE(i2c, an30259a_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static struct i2c_driver an30259a_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		.name = "leds-an30259a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.of_match_table = of_match_ptr(an30259a_match_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.probe_new = an30259a_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	.remove = an30259a_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	.id_table = an30259a_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) module_i2c_driver(an30259a_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) MODULE_AUTHOR("Simon Shields <simon@lineageos.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) MODULE_DESCRIPTION("AN30259A LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) MODULE_LICENSE("GPL v2");