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 21a59ec1e7SMichael Hennerich #include <linux/i2c/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); 147a59ec1e7SMichael Hennerich if (ret) 148a59ec1e7SMichael Hennerich dev_err(&client->dev, "failed to write\n"); 149a59ec1e7SMichael Hennerich 150a59ec1e7SMichael Hennerich return ret; 151a59ec1e7SMichael Hennerich } 152a59ec1e7SMichael Hennerich 153a59ec1e7SMichael Hennerich static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask) 154a59ec1e7SMichael Hennerich { 155a59ec1e7SMichael Hennerich struct adp8870_bl *data = i2c_get_clientdata(client); 156a59ec1e7SMichael Hennerich uint8_t reg_val; 157a59ec1e7SMichael Hennerich int ret; 158a59ec1e7SMichael Hennerich 159a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 160a59ec1e7SMichael Hennerich 161a59ec1e7SMichael Hennerich ret = adp8870_read(client, reg, ®_val); 162a59ec1e7SMichael Hennerich 16336c3e759SAxel Lin if (!ret && ((reg_val & bit_mask) != bit_mask)) { 164a59ec1e7SMichael Hennerich reg_val |= bit_mask; 165a59ec1e7SMichael Hennerich ret = adp8870_write(client, reg, reg_val); 166a59ec1e7SMichael Hennerich } 167a59ec1e7SMichael Hennerich 168a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 169a59ec1e7SMichael Hennerich return ret; 170a59ec1e7SMichael Hennerich } 171a59ec1e7SMichael Hennerich 172a59ec1e7SMichael Hennerich static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask) 173a59ec1e7SMichael Hennerich { 174a59ec1e7SMichael Hennerich struct adp8870_bl *data = i2c_get_clientdata(client); 175a59ec1e7SMichael Hennerich uint8_t reg_val; 176a59ec1e7SMichael Hennerich int ret; 177a59ec1e7SMichael Hennerich 178a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 179a59ec1e7SMichael Hennerich 180a59ec1e7SMichael Hennerich ret = adp8870_read(client, reg, ®_val); 181a59ec1e7SMichael Hennerich 182a59ec1e7SMichael Hennerich if (!ret && (reg_val & bit_mask)) { 183a59ec1e7SMichael Hennerich reg_val &= ~bit_mask; 184a59ec1e7SMichael Hennerich ret = adp8870_write(client, reg, reg_val); 185a59ec1e7SMichael Hennerich } 186a59ec1e7SMichael Hennerich 187a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 188a59ec1e7SMichael Hennerich return ret; 189a59ec1e7SMichael Hennerich } 190a59ec1e7SMichael Hennerich 191a59ec1e7SMichael Hennerich /* 192a59ec1e7SMichael Hennerich * Independent sink / LED 193a59ec1e7SMichael Hennerich */ 194a59ec1e7SMichael Hennerich #if defined(ADP8870_USE_LEDS) 195a59ec1e7SMichael Hennerich static void adp8870_led_work(struct work_struct *work) 196a59ec1e7SMichael Hennerich { 197a59ec1e7SMichael Hennerich struct adp8870_led *led = container_of(work, struct adp8870_led, work); 198a59ec1e7SMichael Hennerich adp8870_write(led->client, ADP8870_ISC1 + led->id - 1, 199a59ec1e7SMichael Hennerich led->new_brightness >> 1); 200a59ec1e7SMichael Hennerich } 201a59ec1e7SMichael Hennerich 202a59ec1e7SMichael Hennerich static void adp8870_led_set(struct led_classdev *led_cdev, 203a59ec1e7SMichael Hennerich enum led_brightness value) 204a59ec1e7SMichael Hennerich { 205a59ec1e7SMichael Hennerich struct adp8870_led *led; 206a59ec1e7SMichael Hennerich 207a59ec1e7SMichael Hennerich led = container_of(led_cdev, struct adp8870_led, cdev); 208a59ec1e7SMichael Hennerich led->new_brightness = value; 209a59ec1e7SMichael Hennerich /* 210a59ec1e7SMichael Hennerich * Use workqueue for IO since I2C operations can sleep. 211a59ec1e7SMichael Hennerich */ 212a59ec1e7SMichael Hennerich schedule_work(&led->work); 213a59ec1e7SMichael Hennerich } 214a59ec1e7SMichael Hennerich 215a59ec1e7SMichael Hennerich static int adp8870_led_setup(struct adp8870_led *led) 216a59ec1e7SMichael Hennerich { 217a59ec1e7SMichael Hennerich struct i2c_client *client = led->client; 218a59ec1e7SMichael Hennerich int ret = 0; 219a59ec1e7SMichael Hennerich 220a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0); 221a59ec1e7SMichael Hennerich if (ret) 222a59ec1e7SMichael Hennerich return ret; 223a59ec1e7SMichael Hennerich 224a59ec1e7SMichael Hennerich ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1)); 225a59ec1e7SMichael Hennerich if (ret) 226a59ec1e7SMichael Hennerich return ret; 227a59ec1e7SMichael Hennerich 228a59ec1e7SMichael Hennerich if (led->id > 4) 229a59ec1e7SMichael Hennerich ret = adp8870_set_bits(client, ADP8870_ISCT1, 230a59ec1e7SMichael Hennerich (led->flags & 0x3) << ((led->id - 5) * 2)); 231a59ec1e7SMichael Hennerich else 232a59ec1e7SMichael Hennerich ret = adp8870_set_bits(client, ADP8870_ISCT2, 233a59ec1e7SMichael Hennerich (led->flags & 0x3) << ((led->id - 1) * 2)); 234a59ec1e7SMichael Hennerich 235a59ec1e7SMichael Hennerich return ret; 236a59ec1e7SMichael Hennerich } 237a59ec1e7SMichael Hennerich 2381b9e450dSBill Pemberton static int adp8870_led_probe(struct i2c_client *client) 239a59ec1e7SMichael Hennerich { 240a59ec1e7SMichael Hennerich struct adp8870_backlight_platform_data *pdata = 241c512794cSJingoo Han dev_get_platdata(&client->dev); 242a59ec1e7SMichael Hennerich struct adp8870_bl *data = i2c_get_clientdata(client); 243a59ec1e7SMichael Hennerich struct adp8870_led *led, *led_dat; 244a59ec1e7SMichael Hennerich struct led_info *cur_led; 245a59ec1e7SMichael Hennerich int ret, i; 246a59ec1e7SMichael Hennerich 24791cdb239SJingoo Han led = devm_kzalloc(&client->dev, pdata->num_leds * sizeof(*led), 24891cdb239SJingoo Han GFP_KERNEL); 249a59ec1e7SMichael Hennerich if (led == NULL) { 250a59ec1e7SMichael Hennerich dev_err(&client->dev, "failed to alloc memory\n"); 251a59ec1e7SMichael Hennerich return -ENOMEM; 252a59ec1e7SMichael Hennerich } 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; 404a59ec1e7SMichael Hennerich if (bl->props.power != FB_BLANK_UNBLANK) 405a59ec1e7SMichael Hennerich brightness = 0; 406a59ec1e7SMichael Hennerich 407a59ec1e7SMichael Hennerich if (bl->props.fb_blank != FB_BLANK_UNBLANK) 408a59ec1e7SMichael Hennerich brightness = 0; 409a59ec1e7SMichael Hennerich 410a59ec1e7SMichael Hennerich return adp8870_bl_set(bl, brightness); 411a59ec1e7SMichael Hennerich } 412a59ec1e7SMichael Hennerich 413a59ec1e7SMichael Hennerich static int adp8870_bl_get_brightness(struct backlight_device *bl) 414a59ec1e7SMichael Hennerich { 415a59ec1e7SMichael Hennerich struct adp8870_bl *data = bl_get_data(bl); 416a59ec1e7SMichael Hennerich 417a59ec1e7SMichael Hennerich return data->current_brightness; 418a59ec1e7SMichael Hennerich } 419a59ec1e7SMichael Hennerich 420a59ec1e7SMichael Hennerich static const struct backlight_ops adp8870_bl_ops = { 421a59ec1e7SMichael Hennerich .update_status = adp8870_bl_update_status, 422a59ec1e7SMichael Hennerich .get_brightness = adp8870_bl_get_brightness, 423a59ec1e7SMichael Hennerich }; 424a59ec1e7SMichael Hennerich 425a59ec1e7SMichael Hennerich static int adp8870_bl_setup(struct backlight_device *bl) 426a59ec1e7SMichael Hennerich { 427a59ec1e7SMichael Hennerich struct adp8870_bl *data = bl_get_data(bl); 428a59ec1e7SMichael Hennerich struct i2c_client *client = data->client; 429a59ec1e7SMichael Hennerich struct adp8870_backlight_platform_data *pdata = data->pdata; 430a59ec1e7SMichael Hennerich int ret = 0; 431a59ec1e7SMichael Hennerich 432a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign); 433a59ec1e7SMichael Hennerich if (ret) 434a59ec1e7SMichael Hennerich return ret; 435a59ec1e7SMichael Hennerich 436a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign); 437a59ec1e7SMichael Hennerich if (ret) 438a59ec1e7SMichael Hennerich return ret; 439a59ec1e7SMichael Hennerich 440a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max); 441a59ec1e7SMichael Hennerich if (ret) 442a59ec1e7SMichael Hennerich return ret; 443a59ec1e7SMichael Hennerich 444a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim); 445a59ec1e7SMichael Hennerich if (ret) 446a59ec1e7SMichael Hennerich return ret; 447a59ec1e7SMichael Hennerich 448a59ec1e7SMichael Hennerich if (pdata->en_ambl_sens) { 449a59ec1e7SMichael Hennerich data->cached_daylight_max = pdata->l1_daylight_max; 450a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLMX2, 451a59ec1e7SMichael Hennerich pdata->l2_bright_max); 452a59ec1e7SMichael Hennerich if (ret) 453a59ec1e7SMichael Hennerich return ret; 454a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLDM2, 455a59ec1e7SMichael Hennerich pdata->l2_bright_dim); 456a59ec1e7SMichael Hennerich if (ret) 457a59ec1e7SMichael Hennerich return ret; 458a59ec1e7SMichael Hennerich 459a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLMX3, 460a59ec1e7SMichael Hennerich pdata->l3_office_max); 461a59ec1e7SMichael Hennerich if (ret) 462a59ec1e7SMichael Hennerich return ret; 463a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLDM3, 464a59ec1e7SMichael Hennerich pdata->l3_office_dim); 465a59ec1e7SMichael Hennerich if (ret) 466a59ec1e7SMichael Hennerich return ret; 467a59ec1e7SMichael Hennerich 468a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLMX4, 469a59ec1e7SMichael Hennerich pdata->l4_indoor_max); 470a59ec1e7SMichael Hennerich if (ret) 471a59ec1e7SMichael Hennerich return ret; 472a59ec1e7SMichael Hennerich 473a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLDM4, 474a59ec1e7SMichael Hennerich pdata->l4_indor_dim); 475a59ec1e7SMichael Hennerich if (ret) 476a59ec1e7SMichael Hennerich return ret; 477a59ec1e7SMichael Hennerich 478a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLMX5, 479a59ec1e7SMichael Hennerich pdata->l5_dark_max); 480a59ec1e7SMichael Hennerich if (ret) 481a59ec1e7SMichael Hennerich return ret; 482a59ec1e7SMichael Hennerich 483a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLDM5, 484a59ec1e7SMichael Hennerich pdata->l5_dark_dim); 485a59ec1e7SMichael Hennerich if (ret) 486a59ec1e7SMichael Hennerich return ret; 487a59ec1e7SMichael Hennerich 488a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip); 489a59ec1e7SMichael Hennerich if (ret) 490a59ec1e7SMichael Hennerich return ret; 491a59ec1e7SMichael Hennerich 492a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst); 493a59ec1e7SMichael Hennerich if (ret) 494a59ec1e7SMichael Hennerich return ret; 495a59ec1e7SMichael Hennerich 496a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip); 497a59ec1e7SMichael Hennerich if (ret) 498a59ec1e7SMichael Hennerich return ret; 499a59ec1e7SMichael Hennerich 500a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst); 501a59ec1e7SMichael Hennerich if (ret) 502a59ec1e7SMichael Hennerich return ret; 503a59ec1e7SMichael Hennerich 504a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip); 505a59ec1e7SMichael Hennerich if (ret) 506a59ec1e7SMichael Hennerich return ret; 507a59ec1e7SMichael Hennerich 508a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst); 509a59ec1e7SMichael Hennerich if (ret) 510a59ec1e7SMichael Hennerich return ret; 511a59ec1e7SMichael Hennerich 512a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip); 513a59ec1e7SMichael Hennerich if (ret) 514a59ec1e7SMichael Hennerich return ret; 515a59ec1e7SMichael Hennerich 516a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst); 517a59ec1e7SMichael Hennerich if (ret) 518a59ec1e7SMichael Hennerich return ret; 519a59ec1e7SMichael Hennerich 520a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN | 521a59ec1e7SMichael Hennerich L3_EN | L2_EN); 522a59ec1e7SMichael Hennerich if (ret) 523a59ec1e7SMichael Hennerich return ret; 524a59ec1e7SMichael Hennerich 525a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_CMP_CTL, 526a59ec1e7SMichael Hennerich ALS_CMPR_CFG_VAL(pdata->abml_filt)); 527a59ec1e7SMichael Hennerich if (ret) 528a59ec1e7SMichael Hennerich return ret; 529a59ec1e7SMichael Hennerich } 530a59ec1e7SMichael Hennerich 531a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_CFGR, 532a59ec1e7SMichael Hennerich BL_CFGR_VAL(pdata->bl_fade_law, 0)); 533a59ec1e7SMichael Hennerich if (ret) 534a59ec1e7SMichael Hennerich return ret; 535a59ec1e7SMichael Hennerich 536a59ec1e7SMichael Hennerich ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in, 537a59ec1e7SMichael Hennerich pdata->bl_fade_out)); 538a59ec1e7SMichael Hennerich if (ret) 539a59ec1e7SMichael Hennerich return ret; 540a59ec1e7SMichael Hennerich /* 541a59ec1e7SMichael Hennerich * ADP8870 Rev0 requires GDWN_DIS bit set 542a59ec1e7SMichael Hennerich */ 543a59ec1e7SMichael Hennerich 544a59ec1e7SMichael Hennerich ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY | 545a59ec1e7SMichael Hennerich (data->revid == 0 ? GDWN_DIS : 0)); 546a59ec1e7SMichael Hennerich 547a59ec1e7SMichael Hennerich return ret; 548a59ec1e7SMichael Hennerich } 549a59ec1e7SMichael Hennerich 550a59ec1e7SMichael Hennerich static ssize_t adp8870_show(struct device *dev, char *buf, int reg) 551a59ec1e7SMichael Hennerich { 552a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 553a59ec1e7SMichael Hennerich int error; 554a59ec1e7SMichael Hennerich uint8_t reg_val; 555a59ec1e7SMichael Hennerich 556a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 557a59ec1e7SMichael Hennerich error = adp8870_read(data->client, reg, ®_val); 558a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 559a59ec1e7SMichael Hennerich 560a59ec1e7SMichael Hennerich if (error < 0) 561a59ec1e7SMichael Hennerich return error; 562a59ec1e7SMichael Hennerich 563a59ec1e7SMichael Hennerich return sprintf(buf, "%u\n", reg_val); 564a59ec1e7SMichael Hennerich } 565a59ec1e7SMichael Hennerich 566a59ec1e7SMichael Hennerich static ssize_t adp8870_store(struct device *dev, const char *buf, 567a59ec1e7SMichael Hennerich size_t count, int reg) 568a59ec1e7SMichael Hennerich { 569a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 570a59ec1e7SMichael Hennerich unsigned long val; 571a59ec1e7SMichael Hennerich int ret; 572a59ec1e7SMichael Hennerich 573dc406f56SJingoo Han ret = kstrtoul(buf, 10, &val); 574a59ec1e7SMichael Hennerich if (ret) 575a59ec1e7SMichael Hennerich return ret; 576a59ec1e7SMichael Hennerich 577a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 578a59ec1e7SMichael Hennerich adp8870_write(data->client, reg, val); 579a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 580a59ec1e7SMichael Hennerich 581a59ec1e7SMichael Hennerich return count; 582a59ec1e7SMichael Hennerich } 583a59ec1e7SMichael Hennerich 584a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev, 585a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 586a59ec1e7SMichael Hennerich { 587a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLMX5); 588a59ec1e7SMichael Hennerich } 589a59ec1e7SMichael Hennerich 590a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev, 591a59ec1e7SMichael Hennerich struct device_attribute *attr, const char *buf, size_t count) 592a59ec1e7SMichael Hennerich { 593a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLMX5); 594a59ec1e7SMichael Hennerich } 595a59ec1e7SMichael Hennerich static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show, 596a59ec1e7SMichael Hennerich adp8870_bl_l5_dark_max_store); 597a59ec1e7SMichael Hennerich 598a59ec1e7SMichael Hennerich 599a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev, 600a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 601a59ec1e7SMichael Hennerich { 602a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLMX4); 603a59ec1e7SMichael Hennerich } 604a59ec1e7SMichael Hennerich 605a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev, 606a59ec1e7SMichael Hennerich struct device_attribute *attr, const char *buf, size_t count) 607a59ec1e7SMichael Hennerich { 608a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLMX4); 609a59ec1e7SMichael Hennerich } 610a59ec1e7SMichael Hennerich static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show, 611a59ec1e7SMichael Hennerich adp8870_bl_l4_indoor_max_store); 612a59ec1e7SMichael Hennerich 613a59ec1e7SMichael Hennerich 614a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l3_office_max_show(struct device *dev, 615a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 616a59ec1e7SMichael Hennerich { 617a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLMX3); 618a59ec1e7SMichael Hennerich } 619a59ec1e7SMichael Hennerich 620a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l3_office_max_store(struct device *dev, 621a59ec1e7SMichael Hennerich struct device_attribute *attr, const char *buf, size_t count) 622a59ec1e7SMichael Hennerich { 623a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLMX3); 624a59ec1e7SMichael Hennerich } 625a59ec1e7SMichael Hennerich 626a59ec1e7SMichael Hennerich static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show, 627a59ec1e7SMichael Hennerich adp8870_bl_l3_office_max_store); 628a59ec1e7SMichael Hennerich 629a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev, 630a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 631a59ec1e7SMichael Hennerich { 632a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLMX2); 633a59ec1e7SMichael Hennerich } 634a59ec1e7SMichael Hennerich 635a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev, 636a59ec1e7SMichael Hennerich struct device_attribute *attr, const char *buf, size_t count) 637a59ec1e7SMichael Hennerich { 638a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLMX2); 639a59ec1e7SMichael Hennerich } 640a59ec1e7SMichael Hennerich static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show, 641a59ec1e7SMichael Hennerich adp8870_bl_l2_bright_max_store); 642a59ec1e7SMichael Hennerich 643a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev, 644a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 645a59ec1e7SMichael Hennerich { 646a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLMX1); 647a59ec1e7SMichael Hennerich } 648a59ec1e7SMichael Hennerich 649a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev, 650a59ec1e7SMichael Hennerich struct device_attribute *attr, const char *buf, size_t count) 651a59ec1e7SMichael Hennerich { 652a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 653dc406f56SJingoo Han int ret = kstrtoul(buf, 10, &data->cached_daylight_max); 654a59ec1e7SMichael Hennerich if (ret) 655a59ec1e7SMichael Hennerich return ret; 656a59ec1e7SMichael Hennerich 657a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLMX1); 658a59ec1e7SMichael Hennerich } 659a59ec1e7SMichael Hennerich static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show, 660a59ec1e7SMichael Hennerich adp8870_bl_l1_daylight_max_store); 661a59ec1e7SMichael Hennerich 662a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev, 663a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 664a59ec1e7SMichael Hennerich { 665a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLDM5); 666a59ec1e7SMichael Hennerich } 667a59ec1e7SMichael Hennerich 668a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev, 669a59ec1e7SMichael Hennerich struct device_attribute *attr, 670a59ec1e7SMichael Hennerich const char *buf, size_t count) 671a59ec1e7SMichael Hennerich { 672a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLDM5); 673a59ec1e7SMichael Hennerich } 674a59ec1e7SMichael Hennerich static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show, 675a59ec1e7SMichael Hennerich adp8870_bl_l5_dark_dim_store); 676a59ec1e7SMichael Hennerich 677a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev, 678a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 679a59ec1e7SMichael Hennerich { 680a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLDM4); 681a59ec1e7SMichael Hennerich } 682a59ec1e7SMichael Hennerich 683a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev, 684a59ec1e7SMichael Hennerich struct device_attribute *attr, 685a59ec1e7SMichael Hennerich const char *buf, size_t count) 686a59ec1e7SMichael Hennerich { 687a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLDM4); 688a59ec1e7SMichael Hennerich } 689a59ec1e7SMichael Hennerich static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show, 690a59ec1e7SMichael Hennerich adp8870_bl_l4_indoor_dim_store); 691a59ec1e7SMichael Hennerich 692a59ec1e7SMichael Hennerich 693a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev, 694a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 695a59ec1e7SMichael Hennerich { 696a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLDM3); 697a59ec1e7SMichael Hennerich } 698a59ec1e7SMichael Hennerich 699a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev, 700a59ec1e7SMichael Hennerich struct device_attribute *attr, 701a59ec1e7SMichael Hennerich const char *buf, size_t count) 702a59ec1e7SMichael Hennerich { 703a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLDM3); 704a59ec1e7SMichael Hennerich } 705a59ec1e7SMichael Hennerich static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show, 706a59ec1e7SMichael Hennerich adp8870_bl_l3_office_dim_store); 707a59ec1e7SMichael Hennerich 708a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev, 709a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 710a59ec1e7SMichael Hennerich { 711a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLDM2); 712a59ec1e7SMichael Hennerich } 713a59ec1e7SMichael Hennerich 714a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev, 715a59ec1e7SMichael Hennerich struct device_attribute *attr, 716a59ec1e7SMichael Hennerich const char *buf, size_t count) 717a59ec1e7SMichael Hennerich { 718a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLDM2); 719a59ec1e7SMichael Hennerich } 720a59ec1e7SMichael Hennerich static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show, 721a59ec1e7SMichael Hennerich adp8870_bl_l2_bright_dim_store); 722a59ec1e7SMichael Hennerich 723a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev, 724a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 725a59ec1e7SMichael Hennerich { 726a59ec1e7SMichael Hennerich return adp8870_show(dev, buf, ADP8870_BLDM1); 727a59ec1e7SMichael Hennerich } 728a59ec1e7SMichael Hennerich 729a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev, 730a59ec1e7SMichael Hennerich struct device_attribute *attr, 731a59ec1e7SMichael Hennerich const char *buf, size_t count) 732a59ec1e7SMichael Hennerich { 733a59ec1e7SMichael Hennerich return adp8870_store(dev, buf, count, ADP8870_BLDM1); 734a59ec1e7SMichael Hennerich } 735a59ec1e7SMichael Hennerich static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show, 736a59ec1e7SMichael Hennerich adp8870_bl_l1_daylight_dim_store); 737a59ec1e7SMichael Hennerich 738a59ec1e7SMichael Hennerich #ifdef ADP8870_EXT_FEATURES 739a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev, 740a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 741a59ec1e7SMichael Hennerich { 742a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 743a59ec1e7SMichael Hennerich int error; 744a59ec1e7SMichael Hennerich uint8_t reg_val; 745a59ec1e7SMichael Hennerich uint16_t ret_val; 746a59ec1e7SMichael Hennerich 747a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 748a59ec1e7SMichael Hennerich error = adp8870_read(data->client, ADP8870_PH1LEVL, ®_val); 749a59ec1e7SMichael Hennerich if (error < 0) { 750a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 751a59ec1e7SMichael Hennerich return error; 752a59ec1e7SMichael Hennerich } 753a59ec1e7SMichael Hennerich ret_val = reg_val; 754a59ec1e7SMichael Hennerich error = adp8870_read(data->client, ADP8870_PH1LEVH, ®_val); 755a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 756a59ec1e7SMichael Hennerich 757a59ec1e7SMichael Hennerich if (error < 0) 758a59ec1e7SMichael Hennerich return error; 759a59ec1e7SMichael Hennerich 760a59ec1e7SMichael Hennerich /* Return 13-bit conversion value for the first light sensor */ 761a59ec1e7SMichael Hennerich ret_val += (reg_val & 0x1F) << 8; 762a59ec1e7SMichael Hennerich 763a59ec1e7SMichael Hennerich return sprintf(buf, "%u\n", ret_val); 764a59ec1e7SMichael Hennerich } 765a59ec1e7SMichael Hennerich static DEVICE_ATTR(ambient_light_level, 0444, 766a59ec1e7SMichael Hennerich adp8870_bl_ambient_light_level_show, NULL); 767a59ec1e7SMichael Hennerich 768a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev, 769a59ec1e7SMichael Hennerich struct device_attribute *attr, char *buf) 770a59ec1e7SMichael Hennerich { 771a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 772a59ec1e7SMichael Hennerich int error; 773a59ec1e7SMichael Hennerich uint8_t reg_val; 774a59ec1e7SMichael Hennerich 775a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 776a59ec1e7SMichael Hennerich error = adp8870_read(data->client, ADP8870_CFGR, ®_val); 777a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 778a59ec1e7SMichael Hennerich 779a59ec1e7SMichael Hennerich if (error < 0) 780a59ec1e7SMichael Hennerich return error; 781a59ec1e7SMichael Hennerich 782a59ec1e7SMichael Hennerich return sprintf(buf, "%u\n", 783a59ec1e7SMichael Hennerich ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1); 784a59ec1e7SMichael Hennerich } 785a59ec1e7SMichael Hennerich 786a59ec1e7SMichael Hennerich static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev, 787a59ec1e7SMichael Hennerich struct device_attribute *attr, 788a59ec1e7SMichael Hennerich const char *buf, size_t count) 789a59ec1e7SMichael Hennerich { 790a59ec1e7SMichael Hennerich struct adp8870_bl *data = dev_get_drvdata(dev); 791a59ec1e7SMichael Hennerich unsigned long val; 792a59ec1e7SMichael Hennerich uint8_t reg_val; 793a59ec1e7SMichael Hennerich int ret; 794a59ec1e7SMichael Hennerich 795dc406f56SJingoo Han ret = kstrtoul(buf, 10, &val); 796a59ec1e7SMichael Hennerich if (ret) 797a59ec1e7SMichael Hennerich return ret; 798a59ec1e7SMichael Hennerich 799a59ec1e7SMichael Hennerich if (val == 0) { 800a59ec1e7SMichael Hennerich /* Enable automatic ambient light sensing */ 801a59ec1e7SMichael Hennerich adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN); 802a59ec1e7SMichael Hennerich } else if ((val > 0) && (val < 6)) { 803a59ec1e7SMichael Hennerich /* Disable automatic ambient light sensing */ 804a59ec1e7SMichael Hennerich adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN); 805a59ec1e7SMichael Hennerich 806a59ec1e7SMichael Hennerich /* Set user supplied ambient light zone */ 807a59ec1e7SMichael Hennerich mutex_lock(&data->lock); 808a59ec1e7SMichael Hennerich adp8870_read(data->client, ADP8870_CFGR, ®_val); 809a59ec1e7SMichael Hennerich reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT); 810a59ec1e7SMichael Hennerich reg_val |= (val - 1) << CFGR_BLV_SHIFT; 811a59ec1e7SMichael Hennerich adp8870_write(data->client, ADP8870_CFGR, reg_val); 812a59ec1e7SMichael Hennerich mutex_unlock(&data->lock); 813a59ec1e7SMichael Hennerich } 814a59ec1e7SMichael Hennerich 815a59ec1e7SMichael Hennerich return count; 816a59ec1e7SMichael Hennerich } 817a59ec1e7SMichael Hennerich static DEVICE_ATTR(ambient_light_zone, 0664, 818a59ec1e7SMichael Hennerich adp8870_bl_ambient_light_zone_show, 819a59ec1e7SMichael Hennerich adp8870_bl_ambient_light_zone_store); 820a59ec1e7SMichael Hennerich #endif 821a59ec1e7SMichael Hennerich 822a59ec1e7SMichael Hennerich static struct attribute *adp8870_bl_attributes[] = { 823a59ec1e7SMichael Hennerich &dev_attr_l5_dark_max.attr, 824a59ec1e7SMichael Hennerich &dev_attr_l5_dark_dim.attr, 825a59ec1e7SMichael Hennerich &dev_attr_l4_indoor_max.attr, 826a59ec1e7SMichael Hennerich &dev_attr_l4_indoor_dim.attr, 827a59ec1e7SMichael Hennerich &dev_attr_l3_office_max.attr, 828a59ec1e7SMichael Hennerich &dev_attr_l3_office_dim.attr, 829a59ec1e7SMichael Hennerich &dev_attr_l2_bright_max.attr, 830a59ec1e7SMichael Hennerich &dev_attr_l2_bright_dim.attr, 831a59ec1e7SMichael Hennerich &dev_attr_l1_daylight_max.attr, 832a59ec1e7SMichael Hennerich &dev_attr_l1_daylight_dim.attr, 833a59ec1e7SMichael Hennerich #ifdef ADP8870_EXT_FEATURES 834a59ec1e7SMichael Hennerich &dev_attr_ambient_light_level.attr, 835a59ec1e7SMichael Hennerich &dev_attr_ambient_light_zone.attr, 836a59ec1e7SMichael Hennerich #endif 837a59ec1e7SMichael Hennerich NULL 838a59ec1e7SMichael Hennerich }; 839a59ec1e7SMichael Hennerich 840a59ec1e7SMichael Hennerich static const struct attribute_group adp8870_bl_attr_group = { 841a59ec1e7SMichael Hennerich .attrs = adp8870_bl_attributes, 842a59ec1e7SMichael Hennerich }; 843a59ec1e7SMichael Hennerich 8441b9e450dSBill Pemberton static int adp8870_probe(struct i2c_client *client, 845a59ec1e7SMichael Hennerich const struct i2c_device_id *id) 846a59ec1e7SMichael Hennerich { 847a59ec1e7SMichael Hennerich struct backlight_properties props; 848a59ec1e7SMichael Hennerich struct backlight_device *bl; 849a59ec1e7SMichael Hennerich struct adp8870_bl *data; 850a59ec1e7SMichael Hennerich struct adp8870_backlight_platform_data *pdata = 851c512794cSJingoo Han dev_get_platdata(&client->dev); 852a59ec1e7SMichael Hennerich uint8_t reg_val; 853a59ec1e7SMichael Hennerich int ret; 854a59ec1e7SMichael Hennerich 855a59ec1e7SMichael Hennerich if (!i2c_check_functionality(client->adapter, 856a59ec1e7SMichael Hennerich I2C_FUNC_SMBUS_BYTE_DATA)) { 857a59ec1e7SMichael Hennerich dev_err(&client->dev, "SMBUS Byte Data not Supported\n"); 858a59ec1e7SMichael Hennerich return -EIO; 859a59ec1e7SMichael Hennerich } 860a59ec1e7SMichael Hennerich 861a59ec1e7SMichael Hennerich if (!pdata) { 862a59ec1e7SMichael Hennerich dev_err(&client->dev, "no platform data?\n"); 863a59ec1e7SMichael Hennerich return -EINVAL; 864a59ec1e7SMichael Hennerich } 865a59ec1e7SMichael Hennerich 866a59ec1e7SMichael Hennerich ret = adp8870_read(client, ADP8870_MFDVID, ®_val); 867a59ec1e7SMichael Hennerich if (ret < 0) 868a59ec1e7SMichael Hennerich return -EIO; 869a59ec1e7SMichael Hennerich 870a59ec1e7SMichael Hennerich if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) { 871a59ec1e7SMichael Hennerich dev_err(&client->dev, "failed to probe\n"); 872a59ec1e7SMichael Hennerich return -ENODEV; 873a59ec1e7SMichael Hennerich } 874a59ec1e7SMichael Hennerich 87591cdb239SJingoo Han data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 876a59ec1e7SMichael Hennerich if (data == NULL) 877a59ec1e7SMichael Hennerich return -ENOMEM; 878a59ec1e7SMichael Hennerich 879a59ec1e7SMichael Hennerich data->revid = ADP8870_DEVID(reg_val); 880a59ec1e7SMichael Hennerich data->client = client; 881a59ec1e7SMichael Hennerich data->pdata = pdata; 882a59ec1e7SMichael Hennerich data->id = id->driver_data; 883a59ec1e7SMichael Hennerich data->current_brightness = 0; 884a59ec1e7SMichael Hennerich i2c_set_clientdata(client, data); 885a59ec1e7SMichael Hennerich 886a59ec1e7SMichael Hennerich mutex_init(&data->lock); 887a59ec1e7SMichael Hennerich 888a59ec1e7SMichael Hennerich memset(&props, 0, sizeof(props)); 889e1bbd19bSAndrew Morton props.type = BACKLIGHT_RAW; 890a59ec1e7SMichael Hennerich props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS; 891*568ee6c3SJingoo Han bl = devm_backlight_device_register(&client->dev, 892*568ee6c3SJingoo Han dev_driver_string(&client->dev), 893a59ec1e7SMichael Hennerich &client->dev, data, &adp8870_bl_ops, &props); 894a59ec1e7SMichael Hennerich if (IS_ERR(bl)) { 895a59ec1e7SMichael Hennerich dev_err(&client->dev, "failed to register backlight\n"); 89691cdb239SJingoo Han return PTR_ERR(bl); 897a59ec1e7SMichael Hennerich } 898a59ec1e7SMichael Hennerich 899a59ec1e7SMichael Hennerich data->bl = bl; 900a59ec1e7SMichael Hennerich 9018541b827SJingoo Han if (pdata->en_ambl_sens) { 902a59ec1e7SMichael Hennerich ret = sysfs_create_group(&bl->dev.kobj, 903a59ec1e7SMichael Hennerich &adp8870_bl_attr_group); 904a59ec1e7SMichael Hennerich if (ret) { 905a59ec1e7SMichael Hennerich dev_err(&client->dev, "failed to register sysfs\n"); 906*568ee6c3SJingoo Han return ret; 907a59ec1e7SMichael Hennerich } 9088541b827SJingoo Han } 909a59ec1e7SMichael Hennerich 910a59ec1e7SMichael Hennerich ret = adp8870_bl_setup(bl); 911a59ec1e7SMichael Hennerich if (ret) { 912a59ec1e7SMichael Hennerich ret = -EIO; 913a59ec1e7SMichael Hennerich goto out; 914a59ec1e7SMichael Hennerich } 915a59ec1e7SMichael Hennerich 916a59ec1e7SMichael Hennerich backlight_update_status(bl); 917a59ec1e7SMichael Hennerich 918a59ec1e7SMichael Hennerich dev_info(&client->dev, "Rev.%d Backlight\n", data->revid); 919a59ec1e7SMichael Hennerich 920a59ec1e7SMichael Hennerich if (pdata->num_leds) 921a59ec1e7SMichael Hennerich adp8870_led_probe(client); 922a59ec1e7SMichael Hennerich 923a59ec1e7SMichael Hennerich return 0; 924a59ec1e7SMichael Hennerich 925a59ec1e7SMichael Hennerich out: 926a59ec1e7SMichael Hennerich if (data->pdata->en_ambl_sens) 927a59ec1e7SMichael Hennerich sysfs_remove_group(&data->bl->dev.kobj, 928a59ec1e7SMichael Hennerich &adp8870_bl_attr_group); 929a59ec1e7SMichael Hennerich 930a59ec1e7SMichael Hennerich return ret; 931a59ec1e7SMichael Hennerich } 932a59ec1e7SMichael Hennerich 9337e4b9d0bSBill Pemberton static int adp8870_remove(struct i2c_client *client) 934a59ec1e7SMichael Hennerich { 935a59ec1e7SMichael Hennerich struct adp8870_bl *data = i2c_get_clientdata(client); 936a59ec1e7SMichael Hennerich 937a59ec1e7SMichael Hennerich adp8870_clr_bits(client, ADP8870_MDCR, NSTBY); 938a59ec1e7SMichael Hennerich 939a59ec1e7SMichael Hennerich if (data->led) 940a59ec1e7SMichael Hennerich adp8870_led_remove(client); 941a59ec1e7SMichael Hennerich 942a59ec1e7SMichael Hennerich if (data->pdata->en_ambl_sens) 943a59ec1e7SMichael Hennerich sysfs_remove_group(&data->bl->dev.kobj, 944a59ec1e7SMichael Hennerich &adp8870_bl_attr_group); 945a59ec1e7SMichael Hennerich 946a59ec1e7SMichael Hennerich return 0; 947a59ec1e7SMichael Hennerich } 948a59ec1e7SMichael Hennerich 9493cd4291aSJingoo Han #ifdef CONFIG_PM_SLEEP 9503cd4291aSJingoo Han static int adp8870_i2c_suspend(struct device *dev) 951a59ec1e7SMichael Hennerich { 9523cd4291aSJingoo Han struct i2c_client *client = to_i2c_client(dev); 9533cd4291aSJingoo Han 954a59ec1e7SMichael Hennerich adp8870_clr_bits(client, ADP8870_MDCR, NSTBY); 955a59ec1e7SMichael Hennerich 956a59ec1e7SMichael Hennerich return 0; 957a59ec1e7SMichael Hennerich } 958a59ec1e7SMichael Hennerich 9593cd4291aSJingoo Han static int adp8870_i2c_resume(struct device *dev) 960a59ec1e7SMichael Hennerich { 9613cd4291aSJingoo Han struct i2c_client *client = to_i2c_client(dev); 9623cd4291aSJingoo Han 9635eb02c01SLars-Peter Clausen adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN); 964a59ec1e7SMichael Hennerich 965a59ec1e7SMichael Hennerich return 0; 966a59ec1e7SMichael Hennerich } 967a59ec1e7SMichael Hennerich #endif 968a59ec1e7SMichael Hennerich 9693cd4291aSJingoo Han static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend, 9703cd4291aSJingoo Han adp8870_i2c_resume); 9713cd4291aSJingoo Han 972a59ec1e7SMichael Hennerich static const struct i2c_device_id adp8870_id[] = { 973a59ec1e7SMichael Hennerich { "adp8870", 0 }, 974a59ec1e7SMichael Hennerich { } 975a59ec1e7SMichael Hennerich }; 976a59ec1e7SMichael Hennerich MODULE_DEVICE_TABLE(i2c, adp8870_id); 977a59ec1e7SMichael Hennerich 978a59ec1e7SMichael Hennerich static struct i2c_driver adp8870_driver = { 979a59ec1e7SMichael Hennerich .driver = { 980a59ec1e7SMichael Hennerich .name = KBUILD_MODNAME, 9813cd4291aSJingoo Han .pm = &adp8870_i2c_pm_ops, 982a59ec1e7SMichael Hennerich }, 983a59ec1e7SMichael Hennerich .probe = adp8870_probe, 984d1723fa2SBill Pemberton .remove = adp8870_remove, 985a59ec1e7SMichael Hennerich .id_table = adp8870_id, 986a59ec1e7SMichael Hennerich }; 987a59ec1e7SMichael Hennerich 98881ce6864SAxel Lin module_i2c_driver(adp8870_driver); 989a59ec1e7SMichael Hennerich 990a59ec1e7SMichael Hennerich MODULE_LICENSE("GPL v2"); 991a59ec1e7SMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 992a59ec1e7SMichael Hennerich MODULE_DESCRIPTION("ADP8870 Backlight driver"); 993c53252b7SAxel Lin MODULE_ALIAS("i2c:adp8870-backlight"); 994