0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/init.h>
0010 #include <linux/errno.h>
0011 #include <linux/pm.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/i2c.h>
0014 #include <linux/fb.h>
0015 #include <linux/backlight.h>
0016 #include <linux/leds.h>
0017 #include <linux/workqueue.h>
0018 #include <linux/slab.h>
0019
0020 #include <linux/platform_data/adp8870.h>
0021 #define ADP8870_EXT_FEATURES
0022 #define ADP8870_USE_LEDS
0023
0024
0025 #define ADP8870_MFDVID 0x00
0026 #define ADP8870_MDCR 0x01
0027 #define ADP8870_INT_STAT 0x02
0028 #define ADP8870_INT_EN 0x03
0029 #define ADP8870_CFGR 0x04
0030 #define ADP8870_BLSEL 0x05
0031 #define ADP8870_PWMLED 0x06
0032 #define ADP8870_BLOFF 0x07
0033 #define ADP8870_BLDIM 0x08
0034 #define ADP8870_BLFR 0x09
0035 #define ADP8870_BLMX1 0x0A
0036 #define ADP8870_BLDM1 0x0B
0037 #define ADP8870_BLMX2 0x0C
0038 #define ADP8870_BLDM2 0x0D
0039 #define ADP8870_BLMX3 0x0E
0040 #define ADP8870_BLDM3 0x0F
0041 #define ADP8870_BLMX4 0x10
0042 #define ADP8870_BLDM4 0x11
0043 #define ADP8870_BLMX5 0x12
0044 #define ADP8870_BLDM5 0x13
0045 #define ADP8870_ISCLAW 0x1A
0046 #define ADP8870_ISCC 0x1B
0047 #define ADP8870_ISCT1 0x1C
0048 #define ADP8870_ISCT2 0x1D
0049 #define ADP8870_ISCF 0x1E
0050 #define ADP8870_ISC1 0x1F
0051 #define ADP8870_ISC2 0x20
0052 #define ADP8870_ISC3 0x21
0053 #define ADP8870_ISC4 0x22
0054 #define ADP8870_ISC5 0x23
0055 #define ADP8870_ISC6 0x24
0056 #define ADP8870_ISC7 0x25
0057 #define ADP8870_ISC7_L2 0x26
0058 #define ADP8870_ISC7_L3 0x27
0059 #define ADP8870_ISC7_L4 0x28
0060 #define ADP8870_ISC7_L5 0x29
0061 #define ADP8870_CMP_CTL 0x2D
0062 #define ADP8870_ALS1_EN 0x2E
0063 #define ADP8870_ALS2_EN 0x2F
0064 #define ADP8870_ALS1_STAT 0x30
0065 #define ADP8870_ALS2_STAT 0x31
0066 #define ADP8870_L2TRP 0x32
0067 #define ADP8870_L2HYS 0x33
0068 #define ADP8870_L3TRP 0x34
0069 #define ADP8870_L3HYS 0x35
0070 #define ADP8870_L4TRP 0x36
0071 #define ADP8870_L4HYS 0x37
0072 #define ADP8870_L5TRP 0x38
0073 #define ADP8870_L5HYS 0x39
0074 #define ADP8870_PH1LEVL 0x40
0075 #define ADP8870_PH1LEVH 0x41
0076 #define ADP8870_PH2LEVL 0x42
0077 #define ADP8870_PH2LEVH 0x43
0078
0079 #define ADP8870_MANUFID 0x3
0080 #define ADP8870_DEVID(x) ((x) & 0xF)
0081 #define ADP8870_MANID(x) ((x) >> 4)
0082
0083
0084 #define D7ALSEN (1 << 7)
0085 #define INT_CFG (1 << 6)
0086 #define NSTBY (1 << 5)
0087 #define DIM_EN (1 << 4)
0088 #define GDWN_DIS (1 << 3)
0089 #define SIS_EN (1 << 2)
0090 #define CMP_AUTOEN (1 << 1)
0091 #define BLEN (1 << 0)
0092
0093
0094 #define L5_EN (1 << 3)
0095 #define L4_EN (1 << 2)
0096 #define L3_EN (1 << 1)
0097 #define L2_EN (1 << 0)
0098
0099 #define CFGR_BLV_SHIFT 3
0100 #define CFGR_BLV_MASK 0x7
0101 #define ADP8870_FLAG_LED_MASK 0xFF
0102
0103 #define FADE_VAL(in, out) ((0xF & (in)) | ((0xF & (out)) << 4))
0104 #define BL_CFGR_VAL(law, blv) ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
0105 #define ALS_CMPR_CFG_VAL(filt) ((0x7 & (filt)) << 1)
0106
0107 struct adp8870_bl {
0108 struct i2c_client *client;
0109 struct backlight_device *bl;
0110 struct adp8870_led *led;
0111 struct adp8870_backlight_platform_data *pdata;
0112 struct mutex lock;
0113 unsigned long cached_daylight_max;
0114 int id;
0115 int revid;
0116 int current_brightness;
0117 };
0118
0119 struct adp8870_led {
0120 struct led_classdev cdev;
0121 struct work_struct work;
0122 struct i2c_client *client;
0123 enum led_brightness new_brightness;
0124 int id;
0125 int flags;
0126 };
0127
0128 static int adp8870_read(struct i2c_client *client, int reg, uint8_t *val)
0129 {
0130 int ret;
0131
0132 ret = i2c_smbus_read_byte_data(client, reg);
0133 if (ret < 0) {
0134 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
0135 return ret;
0136 }
0137
0138 *val = ret;
0139 return 0;
0140 }
0141
0142
0143 static int adp8870_write(struct i2c_client *client, u8 reg, u8 val)
0144 {
0145 int ret = i2c_smbus_write_byte_data(client, reg, val);
0146
0147 if (ret)
0148 dev_err(&client->dev, "failed to write\n");
0149
0150 return ret;
0151 }
0152
0153 static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
0154 {
0155 struct adp8870_bl *data = i2c_get_clientdata(client);
0156 uint8_t reg_val;
0157 int ret;
0158
0159 mutex_lock(&data->lock);
0160
0161 ret = adp8870_read(client, reg, ®_val);
0162
0163 if (!ret && ((reg_val & bit_mask) != bit_mask)) {
0164 reg_val |= bit_mask;
0165 ret = adp8870_write(client, reg, reg_val);
0166 }
0167
0168 mutex_unlock(&data->lock);
0169 return ret;
0170 }
0171
0172 static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
0173 {
0174 struct adp8870_bl *data = i2c_get_clientdata(client);
0175 uint8_t reg_val;
0176 int ret;
0177
0178 mutex_lock(&data->lock);
0179
0180 ret = adp8870_read(client, reg, ®_val);
0181
0182 if (!ret && (reg_val & bit_mask)) {
0183 reg_val &= ~bit_mask;
0184 ret = adp8870_write(client, reg, reg_val);
0185 }
0186
0187 mutex_unlock(&data->lock);
0188 return ret;
0189 }
0190
0191
0192
0193
0194 #if defined(ADP8870_USE_LEDS)
0195 static void adp8870_led_work(struct work_struct *work)
0196 {
0197 struct adp8870_led *led = container_of(work, struct adp8870_led, work);
0198
0199 adp8870_write(led->client, ADP8870_ISC1 + led->id - 1,
0200 led->new_brightness >> 1);
0201 }
0202
0203 static void adp8870_led_set(struct led_classdev *led_cdev,
0204 enum led_brightness value)
0205 {
0206 struct adp8870_led *led;
0207
0208 led = container_of(led_cdev, struct adp8870_led, cdev);
0209 led->new_brightness = value;
0210
0211
0212
0213 schedule_work(&led->work);
0214 }
0215
0216 static int adp8870_led_setup(struct adp8870_led *led)
0217 {
0218 struct i2c_client *client = led->client;
0219 int ret = 0;
0220
0221 ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0);
0222 if (ret)
0223 return ret;
0224
0225 ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1));
0226 if (ret)
0227 return ret;
0228
0229 if (led->id > 4)
0230 ret = adp8870_set_bits(client, ADP8870_ISCT1,
0231 (led->flags & 0x3) << ((led->id - 5) * 2));
0232 else
0233 ret = adp8870_set_bits(client, ADP8870_ISCT2,
0234 (led->flags & 0x3) << ((led->id - 1) * 2));
0235
0236 return ret;
0237 }
0238
0239 static int adp8870_led_probe(struct i2c_client *client)
0240 {
0241 struct adp8870_backlight_platform_data *pdata =
0242 dev_get_platdata(&client->dev);
0243 struct adp8870_bl *data = i2c_get_clientdata(client);
0244 struct adp8870_led *led, *led_dat;
0245 struct led_info *cur_led;
0246 int ret, i;
0247
0248 led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
0249 GFP_KERNEL);
0250 if (led == NULL)
0251 return -ENOMEM;
0252
0253 ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law);
0254 if (ret)
0255 return ret;
0256
0257 ret = adp8870_write(client, ADP8870_ISCT1,
0258 (pdata->led_on_time & 0x3) << 6);
0259 if (ret)
0260 return ret;
0261
0262 ret = adp8870_write(client, ADP8870_ISCF,
0263 FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
0264 if (ret)
0265 return ret;
0266
0267 for (i = 0; i < pdata->num_leds; ++i) {
0268 cur_led = &pdata->leds[i];
0269 led_dat = &led[i];
0270
0271 led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK;
0272
0273 if (led_dat->id > 7 || led_dat->id < 1) {
0274 dev_err(&client->dev, "Invalid LED ID %d\n",
0275 led_dat->id);
0276 ret = -EINVAL;
0277 goto err;
0278 }
0279
0280 if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
0281 dev_err(&client->dev, "LED %d used by Backlight\n",
0282 led_dat->id);
0283 ret = -EBUSY;
0284 goto err;
0285 }
0286
0287 led_dat->cdev.name = cur_led->name;
0288 led_dat->cdev.default_trigger = cur_led->default_trigger;
0289 led_dat->cdev.brightness_set = adp8870_led_set;
0290 led_dat->cdev.brightness = LED_OFF;
0291 led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
0292 led_dat->client = client;
0293 led_dat->new_brightness = LED_OFF;
0294 INIT_WORK(&led_dat->work, adp8870_led_work);
0295
0296 ret = led_classdev_register(&client->dev, &led_dat->cdev);
0297 if (ret) {
0298 dev_err(&client->dev, "failed to register LED %d\n",
0299 led_dat->id);
0300 goto err;
0301 }
0302
0303 ret = adp8870_led_setup(led_dat);
0304 if (ret) {
0305 dev_err(&client->dev, "failed to write\n");
0306 i++;
0307 goto err;
0308 }
0309 }
0310
0311 data->led = led;
0312
0313 return 0;
0314
0315 err:
0316 for (i = i - 1; i >= 0; --i) {
0317 led_classdev_unregister(&led[i].cdev);
0318 cancel_work_sync(&led[i].work);
0319 }
0320
0321 return ret;
0322 }
0323
0324 static int adp8870_led_remove(struct i2c_client *client)
0325 {
0326 struct adp8870_backlight_platform_data *pdata =
0327 dev_get_platdata(&client->dev);
0328 struct adp8870_bl *data = i2c_get_clientdata(client);
0329 int i;
0330
0331 for (i = 0; i < pdata->num_leds; i++) {
0332 led_classdev_unregister(&data->led[i].cdev);
0333 cancel_work_sync(&data->led[i].work);
0334 }
0335
0336 return 0;
0337 }
0338 #else
0339 static int adp8870_led_probe(struct i2c_client *client)
0340 {
0341 return 0;
0342 }
0343
0344 static int adp8870_led_remove(struct i2c_client *client)
0345 {
0346 return 0;
0347 }
0348 #endif
0349
0350 static int adp8870_bl_set(struct backlight_device *bl, int brightness)
0351 {
0352 struct adp8870_bl *data = bl_get_data(bl);
0353 struct i2c_client *client = data->client;
0354 int ret = 0;
0355
0356 if (data->pdata->en_ambl_sens) {
0357 if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) {
0358
0359 ret = adp8870_clr_bits(client, ADP8870_MDCR,
0360 CMP_AUTOEN);
0361 if (ret)
0362 return ret;
0363 ret = adp8870_write(client, ADP8870_BLMX1, brightness);
0364 if (ret)
0365 return ret;
0366 } else {
0367
0368
0369
0370
0371 ret = adp8870_write(client, ADP8870_BLMX1,
0372 data->cached_daylight_max);
0373 if (ret)
0374 return ret;
0375
0376 ret = adp8870_set_bits(client, ADP8870_MDCR,
0377 CMP_AUTOEN);
0378 if (ret)
0379 return ret;
0380 }
0381 } else {
0382 ret = adp8870_write(client, ADP8870_BLMX1, brightness);
0383 if (ret)
0384 return ret;
0385 }
0386
0387 if (data->current_brightness && brightness == 0)
0388 ret = adp8870_set_bits(client,
0389 ADP8870_MDCR, DIM_EN);
0390 else if (data->current_brightness == 0 && brightness)
0391 ret = adp8870_clr_bits(client,
0392 ADP8870_MDCR, DIM_EN);
0393
0394 if (!ret)
0395 data->current_brightness = brightness;
0396
0397 return ret;
0398 }
0399
0400 static int adp8870_bl_update_status(struct backlight_device *bl)
0401 {
0402 return adp8870_bl_set(bl, backlight_get_brightness(bl));
0403 }
0404
0405 static int adp8870_bl_get_brightness(struct backlight_device *bl)
0406 {
0407 struct adp8870_bl *data = bl_get_data(bl);
0408
0409 return data->current_brightness;
0410 }
0411
0412 static const struct backlight_ops adp8870_bl_ops = {
0413 .update_status = adp8870_bl_update_status,
0414 .get_brightness = adp8870_bl_get_brightness,
0415 };
0416
0417 static int adp8870_bl_setup(struct backlight_device *bl)
0418 {
0419 struct adp8870_bl *data = bl_get_data(bl);
0420 struct i2c_client *client = data->client;
0421 struct adp8870_backlight_platform_data *pdata = data->pdata;
0422 int ret = 0;
0423
0424 ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign);
0425 if (ret)
0426 return ret;
0427
0428 ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign);
0429 if (ret)
0430 return ret;
0431
0432 ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max);
0433 if (ret)
0434 return ret;
0435
0436 ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim);
0437 if (ret)
0438 return ret;
0439
0440 if (pdata->en_ambl_sens) {
0441 data->cached_daylight_max = pdata->l1_daylight_max;
0442 ret = adp8870_write(client, ADP8870_BLMX2,
0443 pdata->l2_bright_max);
0444 if (ret)
0445 return ret;
0446 ret = adp8870_write(client, ADP8870_BLDM2,
0447 pdata->l2_bright_dim);
0448 if (ret)
0449 return ret;
0450
0451 ret = adp8870_write(client, ADP8870_BLMX3,
0452 pdata->l3_office_max);
0453 if (ret)
0454 return ret;
0455 ret = adp8870_write(client, ADP8870_BLDM3,
0456 pdata->l3_office_dim);
0457 if (ret)
0458 return ret;
0459
0460 ret = adp8870_write(client, ADP8870_BLMX4,
0461 pdata->l4_indoor_max);
0462 if (ret)
0463 return ret;
0464
0465 ret = adp8870_write(client, ADP8870_BLDM4,
0466 pdata->l4_indor_dim);
0467 if (ret)
0468 return ret;
0469
0470 ret = adp8870_write(client, ADP8870_BLMX5,
0471 pdata->l5_dark_max);
0472 if (ret)
0473 return ret;
0474
0475 ret = adp8870_write(client, ADP8870_BLDM5,
0476 pdata->l5_dark_dim);
0477 if (ret)
0478 return ret;
0479
0480 ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip);
0481 if (ret)
0482 return ret;
0483
0484 ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst);
0485 if (ret)
0486 return ret;
0487
0488 ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip);
0489 if (ret)
0490 return ret;
0491
0492 ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst);
0493 if (ret)
0494 return ret;
0495
0496 ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip);
0497 if (ret)
0498 return ret;
0499
0500 ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst);
0501 if (ret)
0502 return ret;
0503
0504 ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip);
0505 if (ret)
0506 return ret;
0507
0508 ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst);
0509 if (ret)
0510 return ret;
0511
0512 ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN |
0513 L3_EN | L2_EN);
0514 if (ret)
0515 return ret;
0516
0517 ret = adp8870_write(client, ADP8870_CMP_CTL,
0518 ALS_CMPR_CFG_VAL(pdata->abml_filt));
0519 if (ret)
0520 return ret;
0521 }
0522
0523 ret = adp8870_write(client, ADP8870_CFGR,
0524 BL_CFGR_VAL(pdata->bl_fade_law, 0));
0525 if (ret)
0526 return ret;
0527
0528 ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in,
0529 pdata->bl_fade_out));
0530 if (ret)
0531 return ret;
0532
0533
0534
0535
0536 ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY |
0537 (data->revid == 0 ? GDWN_DIS : 0));
0538
0539 return ret;
0540 }
0541
0542 static ssize_t adp8870_show(struct device *dev, char *buf, int reg)
0543 {
0544 struct adp8870_bl *data = dev_get_drvdata(dev);
0545 int error;
0546 uint8_t reg_val;
0547
0548 mutex_lock(&data->lock);
0549 error = adp8870_read(data->client, reg, ®_val);
0550 mutex_unlock(&data->lock);
0551
0552 if (error < 0)
0553 return error;
0554
0555 return sprintf(buf, "%u\n", reg_val);
0556 }
0557
0558 static ssize_t adp8870_store(struct device *dev, const char *buf,
0559 size_t count, int reg)
0560 {
0561 struct adp8870_bl *data = dev_get_drvdata(dev);
0562 unsigned long val;
0563 int ret;
0564
0565 ret = kstrtoul(buf, 10, &val);
0566 if (ret)
0567 return ret;
0568
0569 mutex_lock(&data->lock);
0570 adp8870_write(data->client, reg, val);
0571 mutex_unlock(&data->lock);
0572
0573 return count;
0574 }
0575
0576 static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev,
0577 struct device_attribute *attr, char *buf)
0578 {
0579 return adp8870_show(dev, buf, ADP8870_BLMX5);
0580 }
0581
0582 static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev,
0583 struct device_attribute *attr, const char *buf, size_t count)
0584 {
0585 return adp8870_store(dev, buf, count, ADP8870_BLMX5);
0586 }
0587 static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show,
0588 adp8870_bl_l5_dark_max_store);
0589
0590
0591 static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev,
0592 struct device_attribute *attr, char *buf)
0593 {
0594 return adp8870_show(dev, buf, ADP8870_BLMX4);
0595 }
0596
0597 static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev,
0598 struct device_attribute *attr, const char *buf, size_t count)
0599 {
0600 return adp8870_store(dev, buf, count, ADP8870_BLMX4);
0601 }
0602 static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show,
0603 adp8870_bl_l4_indoor_max_store);
0604
0605
0606 static ssize_t adp8870_bl_l3_office_max_show(struct device *dev,
0607 struct device_attribute *attr, char *buf)
0608 {
0609 return adp8870_show(dev, buf, ADP8870_BLMX3);
0610 }
0611
0612 static ssize_t adp8870_bl_l3_office_max_store(struct device *dev,
0613 struct device_attribute *attr, const char *buf, size_t count)
0614 {
0615 return adp8870_store(dev, buf, count, ADP8870_BLMX3);
0616 }
0617
0618 static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show,
0619 adp8870_bl_l3_office_max_store);
0620
0621 static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev,
0622 struct device_attribute *attr, char *buf)
0623 {
0624 return adp8870_show(dev, buf, ADP8870_BLMX2);
0625 }
0626
0627 static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev,
0628 struct device_attribute *attr, const char *buf, size_t count)
0629 {
0630 return adp8870_store(dev, buf, count, ADP8870_BLMX2);
0631 }
0632 static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show,
0633 adp8870_bl_l2_bright_max_store);
0634
0635 static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev,
0636 struct device_attribute *attr, char *buf)
0637 {
0638 return adp8870_show(dev, buf, ADP8870_BLMX1);
0639 }
0640
0641 static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev,
0642 struct device_attribute *attr, const char *buf, size_t count)
0643 {
0644 struct adp8870_bl *data = dev_get_drvdata(dev);
0645 int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
0646
0647 if (ret)
0648 return ret;
0649
0650 return adp8870_store(dev, buf, count, ADP8870_BLMX1);
0651 }
0652 static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show,
0653 adp8870_bl_l1_daylight_max_store);
0654
0655 static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev,
0656 struct device_attribute *attr, char *buf)
0657 {
0658 return adp8870_show(dev, buf, ADP8870_BLDM5);
0659 }
0660
0661 static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev,
0662 struct device_attribute *attr,
0663 const char *buf, size_t count)
0664 {
0665 return adp8870_store(dev, buf, count, ADP8870_BLDM5);
0666 }
0667 static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show,
0668 adp8870_bl_l5_dark_dim_store);
0669
0670 static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev,
0671 struct device_attribute *attr, char *buf)
0672 {
0673 return adp8870_show(dev, buf, ADP8870_BLDM4);
0674 }
0675
0676 static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev,
0677 struct device_attribute *attr,
0678 const char *buf, size_t count)
0679 {
0680 return adp8870_store(dev, buf, count, ADP8870_BLDM4);
0681 }
0682 static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show,
0683 adp8870_bl_l4_indoor_dim_store);
0684
0685
0686 static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev,
0687 struct device_attribute *attr, char *buf)
0688 {
0689 return adp8870_show(dev, buf, ADP8870_BLDM3);
0690 }
0691
0692 static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev,
0693 struct device_attribute *attr,
0694 const char *buf, size_t count)
0695 {
0696 return adp8870_store(dev, buf, count, ADP8870_BLDM3);
0697 }
0698 static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show,
0699 adp8870_bl_l3_office_dim_store);
0700
0701 static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev,
0702 struct device_attribute *attr, char *buf)
0703 {
0704 return adp8870_show(dev, buf, ADP8870_BLDM2);
0705 }
0706
0707 static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev,
0708 struct device_attribute *attr,
0709 const char *buf, size_t count)
0710 {
0711 return adp8870_store(dev, buf, count, ADP8870_BLDM2);
0712 }
0713 static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show,
0714 adp8870_bl_l2_bright_dim_store);
0715
0716 static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev,
0717 struct device_attribute *attr, char *buf)
0718 {
0719 return adp8870_show(dev, buf, ADP8870_BLDM1);
0720 }
0721
0722 static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev,
0723 struct device_attribute *attr,
0724 const char *buf, size_t count)
0725 {
0726 return adp8870_store(dev, buf, count, ADP8870_BLDM1);
0727 }
0728 static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show,
0729 adp8870_bl_l1_daylight_dim_store);
0730
0731 #ifdef ADP8870_EXT_FEATURES
0732 static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev,
0733 struct device_attribute *attr, char *buf)
0734 {
0735 struct adp8870_bl *data = dev_get_drvdata(dev);
0736 int error;
0737 uint8_t reg_val;
0738 uint16_t ret_val;
0739
0740 mutex_lock(&data->lock);
0741 error = adp8870_read(data->client, ADP8870_PH1LEVL, ®_val);
0742 if (error < 0) {
0743 mutex_unlock(&data->lock);
0744 return error;
0745 }
0746 ret_val = reg_val;
0747 error = adp8870_read(data->client, ADP8870_PH1LEVH, ®_val);
0748 mutex_unlock(&data->lock);
0749
0750 if (error < 0)
0751 return error;
0752
0753
0754 ret_val += (reg_val & 0x1F) << 8;
0755
0756 return sprintf(buf, "%u\n", ret_val);
0757 }
0758 static DEVICE_ATTR(ambient_light_level, 0444,
0759 adp8870_bl_ambient_light_level_show, NULL);
0760
0761 static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev,
0762 struct device_attribute *attr, char *buf)
0763 {
0764 struct adp8870_bl *data = dev_get_drvdata(dev);
0765 int error;
0766 uint8_t reg_val;
0767
0768 mutex_lock(&data->lock);
0769 error = adp8870_read(data->client, ADP8870_CFGR, ®_val);
0770 mutex_unlock(&data->lock);
0771
0772 if (error < 0)
0773 return error;
0774
0775 return sprintf(buf, "%u\n",
0776 ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
0777 }
0778
0779 static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev,
0780 struct device_attribute *attr,
0781 const char *buf, size_t count)
0782 {
0783 struct adp8870_bl *data = dev_get_drvdata(dev);
0784 unsigned long val;
0785 uint8_t reg_val;
0786 int ret;
0787
0788 ret = kstrtoul(buf, 10, &val);
0789 if (ret)
0790 return ret;
0791
0792 if (val == 0) {
0793
0794 adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
0795 } else if ((val > 0) && (val < 6)) {
0796
0797 adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
0798
0799
0800 mutex_lock(&data->lock);
0801 ret = adp8870_read(data->client, ADP8870_CFGR, ®_val);
0802 if (!ret) {
0803 reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
0804 reg_val |= (val - 1) << CFGR_BLV_SHIFT;
0805 adp8870_write(data->client, ADP8870_CFGR, reg_val);
0806 }
0807 mutex_unlock(&data->lock);
0808 }
0809
0810 return count;
0811 }
0812 static DEVICE_ATTR(ambient_light_zone, 0664,
0813 adp8870_bl_ambient_light_zone_show,
0814 adp8870_bl_ambient_light_zone_store);
0815 #endif
0816
0817 static struct attribute *adp8870_bl_attributes[] = {
0818 &dev_attr_l5_dark_max.attr,
0819 &dev_attr_l5_dark_dim.attr,
0820 &dev_attr_l4_indoor_max.attr,
0821 &dev_attr_l4_indoor_dim.attr,
0822 &dev_attr_l3_office_max.attr,
0823 &dev_attr_l3_office_dim.attr,
0824 &dev_attr_l2_bright_max.attr,
0825 &dev_attr_l2_bright_dim.attr,
0826 &dev_attr_l1_daylight_max.attr,
0827 &dev_attr_l1_daylight_dim.attr,
0828 #ifdef ADP8870_EXT_FEATURES
0829 &dev_attr_ambient_light_level.attr,
0830 &dev_attr_ambient_light_zone.attr,
0831 #endif
0832 NULL
0833 };
0834
0835 static const struct attribute_group adp8870_bl_attr_group = {
0836 .attrs = adp8870_bl_attributes,
0837 };
0838
0839 static int adp8870_probe(struct i2c_client *client,
0840 const struct i2c_device_id *id)
0841 {
0842 struct backlight_properties props;
0843 struct backlight_device *bl;
0844 struct adp8870_bl *data;
0845 struct adp8870_backlight_platform_data *pdata =
0846 dev_get_platdata(&client->dev);
0847 uint8_t reg_val;
0848 int ret;
0849
0850 if (!i2c_check_functionality(client->adapter,
0851 I2C_FUNC_SMBUS_BYTE_DATA)) {
0852 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
0853 return -EIO;
0854 }
0855
0856 if (!pdata) {
0857 dev_err(&client->dev, "no platform data?\n");
0858 return -EINVAL;
0859 }
0860
0861 ret = adp8870_read(client, ADP8870_MFDVID, ®_val);
0862 if (ret < 0)
0863 return -EIO;
0864
0865 if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) {
0866 dev_err(&client->dev, "failed to probe\n");
0867 return -ENODEV;
0868 }
0869
0870 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
0871 if (data == NULL)
0872 return -ENOMEM;
0873
0874 data->revid = ADP8870_DEVID(reg_val);
0875 data->client = client;
0876 data->pdata = pdata;
0877 data->id = id->driver_data;
0878 data->current_brightness = 0;
0879 i2c_set_clientdata(client, data);
0880
0881 mutex_init(&data->lock);
0882
0883 memset(&props, 0, sizeof(props));
0884 props.type = BACKLIGHT_RAW;
0885 props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS;
0886 bl = devm_backlight_device_register(&client->dev,
0887 dev_driver_string(&client->dev),
0888 &client->dev, data, &adp8870_bl_ops, &props);
0889 if (IS_ERR(bl)) {
0890 dev_err(&client->dev, "failed to register backlight\n");
0891 return PTR_ERR(bl);
0892 }
0893
0894 data->bl = bl;
0895
0896 if (pdata->en_ambl_sens) {
0897 ret = sysfs_create_group(&bl->dev.kobj,
0898 &adp8870_bl_attr_group);
0899 if (ret) {
0900 dev_err(&client->dev, "failed to register sysfs\n");
0901 return ret;
0902 }
0903 }
0904
0905 ret = adp8870_bl_setup(bl);
0906 if (ret) {
0907 ret = -EIO;
0908 goto out;
0909 }
0910
0911 backlight_update_status(bl);
0912
0913 dev_info(&client->dev, "Rev.%d Backlight\n", data->revid);
0914
0915 if (pdata->num_leds)
0916 adp8870_led_probe(client);
0917
0918 return 0;
0919
0920 out:
0921 if (data->pdata->en_ambl_sens)
0922 sysfs_remove_group(&data->bl->dev.kobj,
0923 &adp8870_bl_attr_group);
0924
0925 return ret;
0926 }
0927
0928 static int adp8870_remove(struct i2c_client *client)
0929 {
0930 struct adp8870_bl *data = i2c_get_clientdata(client);
0931
0932 adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
0933
0934 if (data->led)
0935 adp8870_led_remove(client);
0936
0937 if (data->pdata->en_ambl_sens)
0938 sysfs_remove_group(&data->bl->dev.kobj,
0939 &adp8870_bl_attr_group);
0940
0941 return 0;
0942 }
0943
0944 #ifdef CONFIG_PM_SLEEP
0945 static int adp8870_i2c_suspend(struct device *dev)
0946 {
0947 struct i2c_client *client = to_i2c_client(dev);
0948
0949 adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
0950
0951 return 0;
0952 }
0953
0954 static int adp8870_i2c_resume(struct device *dev)
0955 {
0956 struct i2c_client *client = to_i2c_client(dev);
0957
0958 adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN);
0959
0960 return 0;
0961 }
0962 #endif
0963
0964 static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend,
0965 adp8870_i2c_resume);
0966
0967 static const struct i2c_device_id adp8870_id[] = {
0968 { "adp8870", 0 },
0969 { }
0970 };
0971 MODULE_DEVICE_TABLE(i2c, adp8870_id);
0972
0973 static struct i2c_driver adp8870_driver = {
0974 .driver = {
0975 .name = KBUILD_MODNAME,
0976 .pm = &adp8870_i2c_pm_ops,
0977 },
0978 .probe = adp8870_probe,
0979 .remove = adp8870_remove,
0980 .id_table = adp8870_id,
0981 };
0982
0983 module_i2c_driver(adp8870_driver);
0984
0985 MODULE_LICENSE("GPL v2");
0986 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
0987 MODULE_DESCRIPTION("ADP8870 Backlight driver");