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)  * Backlight driver for Analog Devices ADP8870 Backlight Devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009-2011 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/backlight.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/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/platform_data/adp8870.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define ADP8870_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define ADP8870_USE_LEDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define ADP8870_MFDVID	0x00  /* Manufacturer and device ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define ADP8870_MDCR	0x01  /* Device mode and status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define ADP8870_INT_STAT 0x02  /* Interrupts status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define ADP8870_INT_EN	0x03  /* Interrupts enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define ADP8870_CFGR	0x04  /* Configuration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define ADP8870_BLSEL	0x05  /* Sink enable backlight or independent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define ADP8870_PWMLED	0x06  /* PWM Enable Selection Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define ADP8870_BLOFF	0x07  /* Backlight off timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define ADP8870_BLDIM	0x08  /* Backlight dim timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define ADP8870_BLFR	0x09  /* Backlight fade in and out rates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define ADP8870_BLMX1	0x0A  /* Backlight (Brightness Level 1-daylight) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define ADP8870_BLDM1	0x0B  /* Backlight (Brightness Level 1-daylight) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define ADP8870_BLMX2	0x0C  /* Backlight (Brightness Level 2-bright) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ADP8870_BLDM2	0x0D  /* Backlight (Brightness Level 2-bright) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ADP8870_BLMX3	0x0E  /* Backlight (Brightness Level 3-office) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define ADP8870_BLDM3	0x0F  /* Backlight (Brightness Level 3-office) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ADP8870_BLMX4	0x10  /* Backlight (Brightness Level 4-indoor) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ADP8870_BLDM4	0x11  /* Backlight (Brightness Level 4-indoor) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define ADP8870_BLMX5	0x12  /* Backlight (Brightness Level 5-dark) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define ADP8870_BLDM5	0x13  /* Backlight (Brightness Level 5-dark) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define ADP8870_ISCLAW	0x1A  /* Independent sink current fade law register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define ADP8870_ISCC	0x1B  /* Independent sink current control register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define ADP8870_ISCT1	0x1C  /* Independent Sink Current Timer Register LED[7:5] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define ADP8870_ISCT2	0x1D  /* Independent Sink Current Timer Register LED[4:1] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define ADP8870_ISCF	0x1E  /* Independent sink current fade register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define ADP8870_ISC1	0x1F  /* Independent Sink Current LED1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define ADP8870_ISC2	0x20  /* Independent Sink Current LED2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define ADP8870_ISC3	0x21  /* Independent Sink Current LED3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define ADP8870_ISC4	0x22  /* Independent Sink Current LED4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define ADP8870_ISC5	0x23  /* Independent Sink Current LED5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define ADP8870_ISC6	0x24  /* Independent Sink Current LED6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define ADP8870_ISC7	0x25  /* Independent Sink Current LED7 (Brightness Level 1-daylight) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define ADP8870_ISC7_L2	0x26  /* Independent Sink Current LED7 (Brightness Level 2-bright) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define ADP8870_ISC7_L3	0x27  /* Independent Sink Current LED7 (Brightness Level 3-office) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define ADP8870_ISC7_L4	0x28  /* Independent Sink Current LED7 (Brightness Level 4-indoor) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define ADP8870_ISC7_L5	0x29  /* Independent Sink Current LED7 (Brightness Level 5-dark) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define ADP8870_CMP_CTL	0x2D  /* ALS Comparator Control Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define ADP8870_ALS1_EN	0x2E  /* Main ALS comparator level enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define ADP8870_ALS2_EN	0x2F  /* Second ALS comparator level enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define ADP8870_ALS1_STAT 0x30  /* Main ALS Comparator Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define ADP8870_ALS2_STAT 0x31  /* Second ALS Comparator Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define ADP8870_L2TRP	0x32  /* L2 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define ADP8870_L2HYS	0x33  /* L2 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define ADP8870_L3TRP	0x34  /* L3 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define ADP8870_L3HYS	0x35  /* L3 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define ADP8870_L4TRP	0x36  /* L4 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define ADP8870_L4HYS	0x37  /* L4 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define ADP8870_L5TRP	0x38  /* L5 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define ADP8870_L5HYS	0x39  /* L5 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define ADP8870_PH1LEVL	0x40  /* First phototransistor ambient light level-low byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define ADP8870_PH1LEVH	0x41  /* First phototransistor ambient light level-high byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define ADP8870_PH2LEVL	0x42  /* Second phototransistor ambient light level-low byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define ADP8870_PH2LEVH	0x43  /* Second phototransistor ambient light level-high byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define ADP8870_MANUFID		0x3  /* Analog Devices AD8870 Manufacturer and device ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define ADP8870_DEVID(x)	((x) & 0xF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define ADP8870_MANID(x)	((x) >> 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* MDCR Device mode and status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define D7ALSEN			(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define INT_CFG			(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define NSTBY			(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define DIM_EN			(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define GDWN_DIS		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #define SIS_EN			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define CMP_AUTOEN		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define BLEN			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /* ADP8870_ALS1_EN Main ALS comparator level enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define L5_EN			(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define L4_EN			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define L3_EN			(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define L2_EN			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #define CFGR_BLV_SHIFT		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define CFGR_BLV_MASK		0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define ADP8870_FLAG_LED_MASK	0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define FADE_VAL(in, out)	((0xF & (in)) | ((0xF & (out)) << 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define BL_CFGR_VAL(law, blv)	((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define ALS_CMPR_CFG_VAL(filt)	((0x7 & (filt)) << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct adp8870_bl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct adp8870_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct adp8870_backlight_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	unsigned long cached_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int revid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int current_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct adp8870_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct led_classdev	cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct work_struct	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	enum led_brightness	new_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int			id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int adp8870_read(struct i2c_client *client, int reg, uint8_t *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return ret;
^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) 	*val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int adp8870_write(struct i2c_client *client, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int ret = i2c_smbus_write_byte_data(client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		dev_err(&client->dev, "failed to write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct adp8870_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = adp8870_read(client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (!ret && ((reg_val & bit_mask) != bit_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		reg_val |= bit_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		ret = adp8870_write(client, reg, reg_val);
^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) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct adp8870_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = adp8870_read(client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!ret && (reg_val & bit_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		reg_val &= ~bit_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		ret = adp8870_write(client, reg, reg_val);
^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) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return ret;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * Independent sink / LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #if defined(ADP8870_USE_LEDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static void adp8870_led_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct adp8870_led *led = container_of(work, struct adp8870_led, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	adp8870_write(led->client, ADP8870_ISC1 + led->id - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			 led->new_brightness >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void adp8870_led_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			   enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct adp8870_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	led = container_of(led_cdev, struct adp8870_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	led->new_brightness = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * Use workqueue for IO since I2C operations can sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	schedule_work(&led->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int adp8870_led_setup(struct adp8870_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct i2c_client *client = led->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (led->id > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		ret = adp8870_set_bits(client, ADP8870_ISCT1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				(led->flags & 0x3) << ((led->id - 5) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		ret = adp8870_set_bits(client, ADP8870_ISCT2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				(led->flags & 0x3) << ((led->id - 1) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int adp8870_led_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct adp8870_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct adp8870_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct adp8870_led *led, *led_dat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct led_info *cur_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (led == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (ret)
^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) 	ret = adp8870_write(client, ADP8870_ISCT1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			(pdata->led_on_time & 0x3) << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ret = adp8870_write(client, ADP8870_ISCF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	for (i = 0; i < pdata->num_leds; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		cur_led = &pdata->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		led_dat = &led[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if (led_dat->id > 7 || led_dat->id < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			dev_err(&client->dev, "Invalid LED ID %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			dev_err(&client->dev, "LED %d used by Backlight\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		led_dat->cdev.name = cur_led->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		led_dat->cdev.default_trigger = cur_led->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		led_dat->cdev.brightness_set = adp8870_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		led_dat->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		led_dat->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		led_dat->new_brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		INIT_WORK(&led_dat->work, adp8870_led_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		ret = led_classdev_register(&client->dev, &led_dat->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			dev_err(&client->dev, "failed to register LED %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		ret = adp8870_led_setup(led_dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			dev_err(&client->dev, "failed to write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	data->led = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	for (i = i - 1; i >= 0; --i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		led_classdev_unregister(&led[i].cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		cancel_work_sync(&led[i].work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int adp8870_led_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct adp8870_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct adp8870_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	for (i = 0; i < pdata->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		led_classdev_unregister(&data->led[i].cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		cancel_work_sync(&data->led[i].work);
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int adp8870_led_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return 0;
^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) static int adp8870_led_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int adp8870_bl_set(struct backlight_device *bl, int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct adp8870_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (data->pdata->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			/* Disable Ambient Light auto adjust */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			ret = adp8870_clr_bits(client, ADP8870_MDCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 					CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			ret = adp8870_write(client, ADP8870_BLMX1, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			 * restore daylight l1 sysfs brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			ret = adp8870_write(client, ADP8870_BLMX1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 					 data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			ret = adp8870_set_bits(client, ADP8870_MDCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 					 CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		ret = adp8870_write(client, ADP8870_BLMX1, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (data->current_brightness && brightness == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		ret = adp8870_set_bits(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				ADP8870_MDCR, DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	else if (data->current_brightness == 0 && brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		ret = adp8870_clr_bits(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				ADP8870_MDCR, DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		data->current_brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static int adp8870_bl_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return adp8870_bl_set(bl, backlight_get_brightness(bl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int adp8870_bl_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct adp8870_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return data->current_brightness;
^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) static const struct backlight_ops adp8870_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.update_status	= adp8870_bl_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.get_brightness	= adp8870_bl_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int adp8870_bl_setup(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	struct adp8870_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	struct adp8870_backlight_platform_data *pdata = data->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (pdata->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		data->cached_daylight_max = pdata->l1_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		ret = adp8870_write(client, ADP8870_BLMX2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 						pdata->l2_bright_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		ret = adp8870_write(client, ADP8870_BLDM2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 						pdata->l2_bright_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		ret = adp8870_write(client, ADP8870_BLMX3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 						pdata->l3_office_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		ret = adp8870_write(client, ADP8870_BLDM3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 						pdata->l3_office_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		ret = adp8870_write(client, ADP8870_BLMX4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 						pdata->l4_indoor_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		ret = adp8870_write(client, ADP8870_BLDM4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 						pdata->l4_indor_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		ret = adp8870_write(client, ADP8870_BLMX5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 						pdata->l5_dark_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		ret = adp8870_write(client, ADP8870_BLDM5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 						pdata->l5_dark_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 						L3_EN | L2_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		ret = adp8870_write(client, ADP8870_CMP_CTL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			ALS_CMPR_CFG_VAL(pdata->abml_filt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	ret = adp8870_write(client, ADP8870_CFGR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			BL_CFGR_VAL(pdata->bl_fade_law, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			pdata->bl_fade_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	 * ADP8870 Rev0 requires GDWN_DIS bit set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			(data->revid == 0 ? GDWN_DIS : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static ssize_t adp8870_show(struct device *dev, char *buf, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	error = adp8870_read(data->client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	return sprintf(buf, "%u\n", reg_val);
^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 ssize_t adp8870_store(struct device *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			 size_t count, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	adp8870_write(data->client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	return adp8870_show(dev, buf, ADP8870_BLMX5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	return adp8870_store(dev, buf, count, ADP8870_BLMX5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			adp8870_bl_l5_dark_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	return adp8870_show(dev, buf, ADP8870_BLMX4);
^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) static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	return adp8870_store(dev, buf, count, ADP8870_BLMX4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			adp8870_bl_l4_indoor_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static ssize_t adp8870_bl_l3_office_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	return adp8870_show(dev, buf, ADP8870_BLMX3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) static ssize_t adp8870_bl_l3_office_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return adp8870_store(dev, buf, count, ADP8870_BLMX3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			adp8870_bl_l3_office_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	return adp8870_show(dev, buf, ADP8870_BLMX2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	return adp8870_store(dev, buf, count, ADP8870_BLMX2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 			adp8870_bl_l2_bright_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	return adp8870_show(dev, buf, ADP8870_BLMX1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	return adp8870_store(dev, buf, count, ADP8870_BLMX1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			adp8870_bl_l1_daylight_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	return adp8870_show(dev, buf, ADP8870_BLDM5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	return adp8870_store(dev, buf, count, ADP8870_BLDM5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			adp8870_bl_l5_dark_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	return adp8870_show(dev, buf, ADP8870_BLDM4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	return adp8870_store(dev, buf, count, ADP8870_BLDM4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 			adp8870_bl_l4_indoor_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	return adp8870_show(dev, buf, ADP8870_BLDM3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	return adp8870_store(dev, buf, count, ADP8870_BLDM3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			adp8870_bl_l3_office_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	return adp8870_show(dev, buf, ADP8870_BLDM2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	return adp8870_store(dev, buf, count, ADP8870_BLDM2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 			adp8870_bl_l2_bright_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	return adp8870_show(dev, buf, ADP8870_BLDM1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	return adp8870_store(dev, buf, count, ADP8870_BLDM1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 			adp8870_bl_l1_daylight_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) #ifdef ADP8870_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	uint16_t ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	error = adp8870_read(data->client, ADP8870_PH1LEVL, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	ret_val = reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	error = adp8870_read(data->client, ADP8870_PH1LEVH, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	/* Return 13-bit conversion value for the first light sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	ret_val += (reg_val & 0x1F) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	return sprintf(buf, "%u\n", ret_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static DEVICE_ATTR(ambient_light_level, 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		adp8870_bl_ambient_light_level_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	error = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	return sprintf(buf, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	struct adp8870_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	if (val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 		/* Enable automatic ambient light sensing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 		adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	} else if ((val > 0) && (val < 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		/* Disable automatic ambient light sensing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 		/* Set user supplied ambient light zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 		mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		ret = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 			reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 			reg_val |= (val - 1) << CFGR_BLV_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 			adp8870_write(data->client, ADP8870_CFGR, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) static DEVICE_ATTR(ambient_light_zone, 0664,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		adp8870_bl_ambient_light_zone_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		adp8870_bl_ambient_light_zone_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) static struct attribute *adp8870_bl_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	&dev_attr_l5_dark_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	&dev_attr_l5_dark_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	&dev_attr_l4_indoor_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	&dev_attr_l4_indoor_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	&dev_attr_l3_office_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	&dev_attr_l3_office_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	&dev_attr_l2_bright_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	&dev_attr_l2_bright_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	&dev_attr_l1_daylight_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	&dev_attr_l1_daylight_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) #ifdef ADP8870_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	&dev_attr_ambient_light_level.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	&dev_attr_ambient_light_zone.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) static const struct attribute_group adp8870_bl_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	.attrs = adp8870_bl_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) static int adp8870_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 					const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	struct adp8870_bl *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	struct adp8870_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 					I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 		dev_err(&client->dev, "no platform data?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	ret = adp8870_read(client, ADP8870_MFDVID, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		dev_err(&client->dev, "failed to probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	data->revid = ADP8870_DEVID(reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	data->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	data->id = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	data->current_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	memset(&props, 0, sizeof(props));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	bl = devm_backlight_device_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 				dev_driver_string(&client->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 				&client->dev, data, &adp8870_bl_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	if (IS_ERR(bl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 		dev_err(&client->dev, "failed to register backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 		return PTR_ERR(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	data->bl = bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	if (pdata->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 		ret = sysfs_create_group(&bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 			&adp8870_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 			dev_err(&client->dev, "failed to register sysfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	ret = adp8870_bl_setup(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	dev_info(&client->dev, "Rev.%d Backlight\n", data->revid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	if (pdata->num_leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		adp8870_led_probe(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	if (data->pdata->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 		sysfs_remove_group(&data->bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 			&adp8870_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) static int adp8870_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	struct adp8870_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	if (data->led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 		adp8870_led_remove(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	if (data->pdata->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 		sysfs_remove_group(&data->bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 			&adp8870_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static int adp8870_i2c_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) static int adp8870_i2c_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 	adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) 			adp8870_i2c_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) static const struct i2c_device_id adp8870_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) 	{ "adp8870", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) MODULE_DEVICE_TABLE(i2c, adp8870_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) static struct i2c_driver adp8870_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) 		.name	= KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) 		.pm	= &adp8870_i2c_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) 	.probe    = adp8870_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) 	.remove   = adp8870_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) 	.id_table = adp8870_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) module_i2c_driver(adp8870_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) MODULE_DESCRIPTION("ADP8870 Backlight driver");