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