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 ADP8860 Backlight Devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009-2010 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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/workqueue.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/adp8860.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define ADP8860_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define ADP8860_USE_LEDS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define ADP8860_MFDVID 0x00 /* Manufacturer and device ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define ADP8860_MDCR 0x01 /* Device mode and status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define ADP8860_MDCR2 0x02 /* Device mode and Status Register 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define ADP8860_INTR_EN 0x03 /* Interrupts enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define ADP8860_CFGR 0x04 /* Configuration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define ADP8860_BLSEN 0x05 /* Sink enable backlight or independent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define ADP8860_BLOFF 0x06 /* Backlight off timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define ADP8860_BLDIM 0x07 /* Backlight dim timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define ADP8860_BLFR 0x08 /* Backlight fade in and out rates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define ADP8860_BLMX1 0x09 /* Backlight (Brightness Level 1-daylight) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define ADP8860_BLDM1 0x0A /* Backlight (Brightness Level 1-daylight) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define ADP8860_BLMX2 0x0B /* Backlight (Brightness Level 2-office) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define ADP8860_BLDM2 0x0C /* Backlight (Brightness Level 2-office) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define ADP8860_BLMX3 0x0D /* Backlight (Brightness Level 3-dark) maximum current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ADP8860_BLDM3 0x0E /* Backlight (Brightness Level 3-dark) dim current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ADP8860_ISCFR 0x0F /* Independent sink current fade control register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define ADP8860_ISCC 0x10 /* Independent sink current control register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ADP8860_ISCT1 0x11 /* Independent Sink Current Timer Register LED[7:5] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ADP8860_ISCT2 0x12 /* Independent Sink Current Timer Register LED[4:1] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define ADP8860_ISCF 0x13 /* Independent sink current fade register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define ADP8860_ISC7 0x14 /* Independent Sink Current LED7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define ADP8860_ISC6 0x15 /* Independent Sink Current LED6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define ADP8860_ISC5 0x16 /* Independent Sink Current LED5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define ADP8860_ISC4 0x17 /* Independent Sink Current LED4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define ADP8860_ISC3 0x18 /* Independent Sink Current LED3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define ADP8860_ISC2 0x19 /* Independent Sink Current LED2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define ADP8860_ISC1 0x1A /* Independent Sink Current LED1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define ADP8860_CCFG 0x1B /* Comparator configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define ADP8860_CCFG2 0x1C /* Second comparator configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define ADP8860_L2_TRP 0x1D /* L2 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define ADP8860_L2_HYS 0x1E /* L2 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define ADP8860_L3_TRP 0x1F /* L3 comparator reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define ADP8860_L3_HYS 0x20 /* L3 hysteresis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define ADP8860_PH1LEVL 0x21 /* First phototransistor ambient light level-low byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define ADP8860_PH1LEVH 0x22 /* First phototransistor ambient light level-high byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define ADP8860_PH2LEVL 0x23 /* Second phototransistor ambient light level-low byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define ADP8860_PH2LEVH 0x24 /* Second phototransistor ambient light level-high byte register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define ADP8860_MANUFID		0x0  /* Analog Devices ADP8860 Manufacturer ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define ADP8861_MANUFID		0x4  /* Analog Devices ADP8861 Manufacturer ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define ADP8863_MANUFID		0x2  /* Analog Devices ADP8863 Manufacturer ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define ADP8860_DEVID(x)	((x) & 0xF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define ADP8860_MANID(x)	((x) >> 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /* MDCR Device mode and status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define INT_CFG			(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define NSTBY			(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define DIM_EN			(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define GDWN_DIS		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define SIS_EN			(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define CMP_AUTOEN		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define BLEN			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) /* ADP8860_CCFG Main ALS comparator level enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define L3_EN			(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define L2_EN			(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define CFGR_BLV_SHIFT		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define CFGR_BLV_MASK		0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define ADP8860_FLAG_LED_MASK	0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define FADE_VAL(in, out)	((0xF & (in)) | ((0xF & (out)) << 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #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  88) #define ALS_CCFG_VAL(filt)	((0x7 & filt) << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	adp8860,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	adp8861,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	adp8863
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) struct adp8860_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct led_classdev	cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct work_struct	work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	enum led_brightness	new_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int			id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int			flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct adp8860_bl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct adp8860_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct adp8860_backlight_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned long cached_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int revid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int current_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned en_ambl_sens:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	unsigned gdwn_dis:1;
^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) static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ret = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	*val = (uint8_t)ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return i2c_smbus_write_byte_data(client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct adp8860_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ret = adp8860_read(client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!ret && ((reg_val & bit_mask) != bit_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		reg_val |= bit_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ret = adp8860_write(client, reg, reg_val);
^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) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct adp8860_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	ret = adp8860_read(client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!ret && (reg_val & bit_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		reg_val &= ~bit_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		ret = adp8860_write(client, reg, reg_val);
^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) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * Independent sink / LED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) #if defined(ADP8860_USE_LEDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void adp8860_led_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct adp8860_led *led = container_of(work, struct adp8860_led, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	adp8860_write(led->client, ADP8860_ISC1 - led->id + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			 led->new_brightness >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static void adp8860_led_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			   enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct adp8860_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	led = container_of(led_cdev, struct adp8860_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	led->new_brightness = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	schedule_work(&led->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int adp8860_led_setup(struct adp8860_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct i2c_client *client = led->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (led->id > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ret |= adp8860_set_bits(client, ADP8860_ISCT1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				(led->flags & 0x3) << ((led->id - 5) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		ret |= adp8860_set_bits(client, ADP8860_ISCT2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				(led->flags & 0x3) << ((led->id - 1) * 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	return ret;
^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 adp8860_led_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct adp8860_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct adp8860_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct adp8860_led *led, *led_dat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct led_info *cur_led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (led == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ret = adp8860_write(client, ADP8860_ISCT1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			(pdata->led_on_time & 0x3) << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ret |= adp8860_write(client, ADP8860_ISCF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		dev_err(&client->dev, "failed to write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return ret;
^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) 	for (i = 0; i < pdata->num_leds; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		cur_led = &pdata->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		led_dat = &led[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (led_dat->id > 7 || led_dat->id < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			dev_err(&client->dev, "Invalid LED ID %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			dev_err(&client->dev, "LED %d used by Backlight\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		led_dat->cdev.name = cur_led->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		led_dat->cdev.default_trigger = cur_led->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		led_dat->cdev.brightness_set = adp8860_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		led_dat->cdev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		led_dat->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		led_dat->new_brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		INIT_WORK(&led_dat->work, adp8860_led_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		ret = led_classdev_register(&client->dev, &led_dat->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			dev_err(&client->dev, "failed to register LED %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 				led_dat->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		ret = adp8860_led_setup(led_dat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			dev_err(&client->dev, "failed to write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	data->led = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	for (i = i - 1; i >= 0; --i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		led_classdev_unregister(&led[i].cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		cancel_work_sync(&led[i].work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int adp8860_led_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	struct adp8860_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct adp8860_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	for (i = 0; i < pdata->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		led_classdev_unregister(&data->led[i].cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		cancel_work_sync(&data->led[i].work);
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int adp8860_led_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int adp8860_led_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int adp8860_bl_set(struct backlight_device *bl, int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct adp8860_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (data->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			/* Disable Ambient Light auto adjust */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			ret |= adp8860_clr_bits(client, ADP8860_MDCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 					CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			 * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			 * restore daylight l1 sysfs brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			ret |= adp8860_write(client, ADP8860_BLMX1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 					 data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			ret |= adp8860_set_bits(client, ADP8860_MDCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 					 CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (data->current_brightness && brightness == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		ret |= adp8860_set_bits(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				ADP8860_MDCR, DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	else if (data->current_brightness == 0 && brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		ret |= adp8860_clr_bits(client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 				ADP8860_MDCR, DIM_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		data->current_brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int adp8860_bl_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return adp8860_bl_set(bl, backlight_get_brightness(bl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int adp8860_bl_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct adp8860_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return data->current_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static const struct backlight_ops adp8860_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	.update_status	= adp8860_bl_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.get_brightness	= adp8860_bl_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int adp8860_bl_setup(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct adp8860_bl *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	struct adp8860_backlight_platform_data *pdata = data->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (data->en_ambl_sens) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		data->cached_daylight_max = pdata->l1_daylight_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ret |= adp8860_write(client, ADP8860_BLMX2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 						pdata->l2_office_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		ret |= adp8860_write(client, ADP8860_BLDM2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 						pdata->l2_office_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		ret |= adp8860_write(client, ADP8860_BLMX3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 						pdata->l3_dark_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		ret |= adp8860_write(client, ADP8860_BLDM3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 						pdata->l3_dark_dim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 						ALS_CCFG_VAL(pdata->abml_filt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	ret |= adp8860_write(client, ADP8860_CFGR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			BL_CFGR_VAL(pdata->bl_fade_law, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			pdata->bl_fade_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			(data->gdwn_dis ? GDWN_DIS : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	error = adp8860_read(data->client, reg, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	return sprintf(buf, "%u\n", reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static ssize_t adp8860_store(struct device *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			 size_t count, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	adp8860_write(data->client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return adp8860_show(dev, buf, ADP8860_BLMX3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return adp8860_store(dev, buf, count, ADP8860_BLMX3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			adp8860_bl_l3_dark_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	return adp8860_show(dev, buf, ADP8860_BLMX2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	return adp8860_store(dev, buf, count, ADP8860_BLMX2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			adp8860_bl_l2_office_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	return adp8860_show(dev, buf, ADP8860_BLMX1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	return adp8860_store(dev, buf, count, ADP8860_BLMX1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			adp8860_bl_l1_daylight_max_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	return adp8860_show(dev, buf, ADP8860_BLDM3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	return adp8860_store(dev, buf, count, ADP8860_BLDM3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			adp8860_bl_l3_dark_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return adp8860_show(dev, buf, ADP8860_BLDM2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	return adp8860_store(dev, buf, count, ADP8860_BLDM2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			adp8860_bl_l2_office_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	return adp8860_show(dev, buf, ADP8860_BLDM1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	return adp8860_store(dev, buf, count, ADP8860_BLDM1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			adp8860_bl_l1_daylight_dim_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #ifdef ADP8860_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	uint16_t ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		ret_val = reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		error = adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	/* Return 13-bit conversion value for the first light sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	ret_val += (reg_val & 0x1F) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	return sprintf(buf, "%u\n", ret_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static DEVICE_ATTR(ambient_light_level, 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		adp8860_bl_ambient_light_level_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 				     struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	return sprintf(buf, "%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	struct adp8860_bl *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	ret = kstrtoul(buf, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		/* Enable automatic ambient light sensing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	} else if ((val > 0) && (val <= 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		/* Disable automatic ambient light sensing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		/* Set user supplied ambient light zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		ret = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			reg_val |= (val - 1) << CFGR_BLV_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 			adp8860_write(data->client, ADP8860_CFGR, reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static DEVICE_ATTR(ambient_light_zone, 0664,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		adp8860_bl_ambient_light_zone_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		adp8860_bl_ambient_light_zone_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static struct attribute *adp8860_bl_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	&dev_attr_l3_dark_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	&dev_attr_l3_dark_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	&dev_attr_l2_office_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	&dev_attr_l2_office_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	&dev_attr_l1_daylight_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	&dev_attr_l1_daylight_dim.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) #ifdef ADP8860_EXT_FEATURES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	&dev_attr_ambient_light_level.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	&dev_attr_ambient_light_zone.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static const struct attribute_group adp8860_bl_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	.attrs = adp8860_bl_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static int adp8860_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 					const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	struct adp8860_bl *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	struct adp8860_backlight_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 					I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		dev_err(&client->dev, "no platform data?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	switch (ADP8860_MANID(reg_val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	case ADP8863_MANUFID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		data->gdwn_dis = !!pdata->gdwn_dis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	case ADP8860_MANUFID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		data->en_ambl_sens = !!pdata->en_ambl_sens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	case ADP8861_MANUFID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		data->gdwn_dis = !!pdata->gdwn_dis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		dev_err(&client->dev, "failed to probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	/* It's confirmed that the DEVID field is actually a REVID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	data->revid = ADP8860_DEVID(reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	data->pdata = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	data->id = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	data->current_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	memset(&props, 0, sizeof(props));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	props.max_brightness = ADP8860_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	bl = devm_backlight_device_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 				dev_driver_string(&client->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 				&client->dev, data, &adp8860_bl_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	if (IS_ERR(bl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		dev_err(&client->dev, "failed to register backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		return PTR_ERR(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	data->bl = bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	if (data->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		ret = sysfs_create_group(&bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			&adp8860_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		dev_err(&client->dev, "failed to register sysfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	ret = adp8860_bl_setup(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	dev_info(&client->dev, "%s Rev.%d Backlight\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		client->name, data->revid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	if (pdata->num_leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		adp8860_led_probe(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	if (data->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		sysfs_remove_group(&data->bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 			&adp8860_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static int adp8860_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	struct adp8860_bl *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	if (data->led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		adp8860_led_remove(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	if (data->en_ambl_sens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		sysfs_remove_group(&data->bl->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			&adp8860_bl_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) static int adp8860_i2c_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) static int adp8860_i2c_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	adp8860_set_bits(client, ADP8860_MDCR, NSTBY | BLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static SIMPLE_DEV_PM_OPS(adp8860_i2c_pm_ops, adp8860_i2c_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 			adp8860_i2c_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) static const struct i2c_device_id adp8860_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	{ "adp8860", adp8860 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	{ "adp8861", adp8861 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	{ "adp8863", adp8863 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) MODULE_DEVICE_TABLE(i2c, adp8860_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static struct i2c_driver adp8860_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		.name	= KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		.pm	= &adp8860_i2c_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	.probe    = adp8860_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	.remove   = adp8860_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	.id_table = adp8860_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) module_i2c_driver(adp8860_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) MODULE_DESCRIPTION("ADP8860 Backlight driver");