Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Backlight driver for Analog Devices ADP8860 Backlight Devices
0004  *
0005  * Copyright 2009-2010 Analog Devices Inc.
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/slab.h>
0018 #include <linux/workqueue.h>
0019 
0020 #include <linux/platform_data/adp8860.h>
0021 #define ADP8860_EXT_FEATURES
0022 #define ADP8860_USE_LEDS
0023 
0024 #define ADP8860_MFDVID 0x00 /* Manufacturer and device ID */
0025 #define ADP8860_MDCR 0x01 /* Device mode and status */
0026 #define ADP8860_MDCR2 0x02 /* Device mode and Status Register 2 */
0027 #define ADP8860_INTR_EN 0x03 /* Interrupts enable */
0028 #define ADP8860_CFGR 0x04 /* Configuration register */
0029 #define ADP8860_BLSEN 0x05 /* Sink enable backlight or independent */
0030 #define ADP8860_BLOFF 0x06 /* Backlight off timeout */
0031 #define ADP8860_BLDIM 0x07 /* Backlight dim timeout */
0032 #define ADP8860_BLFR 0x08 /* Backlight fade in and out rates */
0033 #define ADP8860_BLMX1 0x09 /* Backlight (Brightness Level 1-daylight) maximum current */
0034 #define ADP8860_BLDM1 0x0A /* Backlight (Brightness Level 1-daylight) dim current */
0035 #define ADP8860_BLMX2 0x0B /* Backlight (Brightness Level 2-office) maximum current */
0036 #define ADP8860_BLDM2 0x0C /* Backlight (Brightness Level 2-office) dim current */
0037 #define ADP8860_BLMX3 0x0D /* Backlight (Brightness Level 3-dark) maximum current */
0038 #define ADP8860_BLDM3 0x0E /* Backlight (Brightness Level 3-dark) dim current */
0039 #define ADP8860_ISCFR 0x0F /* Independent sink current fade control register */
0040 #define ADP8860_ISCC 0x10 /* Independent sink current control register */
0041 #define ADP8860_ISCT1 0x11 /* Independent Sink Current Timer Register LED[7:5] */
0042 #define ADP8860_ISCT2 0x12 /* Independent Sink Current Timer Register LED[4:1] */
0043 #define ADP8860_ISCF 0x13 /* Independent sink current fade register */
0044 #define ADP8860_ISC7 0x14 /* Independent Sink Current LED7 */
0045 #define ADP8860_ISC6 0x15 /* Independent Sink Current LED6 */
0046 #define ADP8860_ISC5 0x16 /* Independent Sink Current LED5 */
0047 #define ADP8860_ISC4 0x17 /* Independent Sink Current LED4 */
0048 #define ADP8860_ISC3 0x18 /* Independent Sink Current LED3 */
0049 #define ADP8860_ISC2 0x19 /* Independent Sink Current LED2 */
0050 #define ADP8860_ISC1 0x1A /* Independent Sink Current LED1 */
0051 #define ADP8860_CCFG 0x1B /* Comparator configuration */
0052 #define ADP8860_CCFG2 0x1C /* Second comparator configuration */
0053 #define ADP8860_L2_TRP 0x1D /* L2 comparator reference */
0054 #define ADP8860_L2_HYS 0x1E /* L2 hysteresis */
0055 #define ADP8860_L3_TRP 0x1F /* L3 comparator reference */
0056 #define ADP8860_L3_HYS 0x20 /* L3 hysteresis */
0057 #define ADP8860_PH1LEVL 0x21 /* First phototransistor ambient light level-low byte register */
0058 #define ADP8860_PH1LEVH 0x22 /* First phototransistor ambient light level-high byte register */
0059 #define ADP8860_PH2LEVL 0x23 /* Second phototransistor ambient light level-low byte register */
0060 #define ADP8860_PH2LEVH 0x24 /* Second phototransistor ambient light level-high byte register */
0061 
0062 #define ADP8860_MANUFID     0x0  /* Analog Devices ADP8860 Manufacturer ID */
0063 #define ADP8861_MANUFID     0x4  /* Analog Devices ADP8861 Manufacturer ID */
0064 #define ADP8863_MANUFID     0x2  /* Analog Devices ADP8863 Manufacturer ID */
0065 
0066 #define ADP8860_DEVID(x)    ((x) & 0xF)
0067 #define ADP8860_MANID(x)    ((x) >> 4)
0068 
0069 /* MDCR Device mode and status */
0070 #define INT_CFG         (1 << 6)
0071 #define NSTBY           (1 << 5)
0072 #define DIM_EN          (1 << 4)
0073 #define GDWN_DIS        (1 << 3)
0074 #define SIS_EN          (1 << 2)
0075 #define CMP_AUTOEN      (1 << 1)
0076 #define BLEN            (1 << 0)
0077 
0078 /* ADP8860_CCFG Main ALS comparator level enable */
0079 #define L3_EN           (1 << 1)
0080 #define L2_EN           (1 << 0)
0081 
0082 #define CFGR_BLV_SHIFT      3
0083 #define CFGR_BLV_MASK       0x3
0084 #define ADP8860_FLAG_LED_MASK   0xFF
0085 
0086 #define FADE_VAL(in, out)   ((0xF & (in)) | ((0xF & (out)) << 4))
0087 #define BL_CFGR_VAL(law, blv)   ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
0088 #define ALS_CCFG_VAL(filt)  ((0x7 & filt) << 5)
0089 
0090 enum {
0091     adp8860,
0092     adp8861,
0093     adp8863
0094 };
0095 
0096 struct adp8860_led {
0097     struct led_classdev cdev;
0098     struct work_struct  work;
0099     struct i2c_client   *client;
0100     enum led_brightness new_brightness;
0101     int         id;
0102     int         flags;
0103 };
0104 
0105 struct adp8860_bl {
0106     struct i2c_client *client;
0107     struct backlight_device *bl;
0108     struct adp8860_led *led;
0109     struct adp8860_backlight_platform_data *pdata;
0110     struct mutex lock;
0111     unsigned long cached_daylight_max;
0112     int id;
0113     int revid;
0114     int current_brightness;
0115     unsigned en_ambl_sens:1;
0116     unsigned gdwn_dis:1;
0117 };
0118 
0119 static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
0120 {
0121     int ret;
0122 
0123     ret = i2c_smbus_read_byte_data(client, reg);
0124     if (ret < 0) {
0125         dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
0126         return ret;
0127     }
0128 
0129     *val = (uint8_t)ret;
0130     return 0;
0131 }
0132 
0133 static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
0134 {
0135     return i2c_smbus_write_byte_data(client, reg, val);
0136 }
0137 
0138 static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
0139 {
0140     struct adp8860_bl *data = i2c_get_clientdata(client);
0141     uint8_t reg_val;
0142     int ret;
0143 
0144     mutex_lock(&data->lock);
0145 
0146     ret = adp8860_read(client, reg, &reg_val);
0147 
0148     if (!ret && ((reg_val & bit_mask) != bit_mask)) {
0149         reg_val |= bit_mask;
0150         ret = adp8860_write(client, reg, reg_val);
0151     }
0152 
0153     mutex_unlock(&data->lock);
0154     return ret;
0155 }
0156 
0157 static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
0158 {
0159     struct adp8860_bl *data = i2c_get_clientdata(client);
0160     uint8_t reg_val;
0161     int ret;
0162 
0163     mutex_lock(&data->lock);
0164 
0165     ret = adp8860_read(client, reg, &reg_val);
0166 
0167     if (!ret && (reg_val & bit_mask)) {
0168         reg_val &= ~bit_mask;
0169         ret = adp8860_write(client, reg, reg_val);
0170     }
0171 
0172     mutex_unlock(&data->lock);
0173     return ret;
0174 }
0175 
0176 /*
0177  * Independent sink / LED
0178  */
0179 #if defined(ADP8860_USE_LEDS)
0180 static void adp8860_led_work(struct work_struct *work)
0181 {
0182     struct adp8860_led *led = container_of(work, struct adp8860_led, work);
0183 
0184     adp8860_write(led->client, ADP8860_ISC1 - led->id + 1,
0185              led->new_brightness >> 1);
0186 }
0187 
0188 static void adp8860_led_set(struct led_classdev *led_cdev,
0189                enum led_brightness value)
0190 {
0191     struct adp8860_led *led;
0192 
0193     led = container_of(led_cdev, struct adp8860_led, cdev);
0194     led->new_brightness = value;
0195     schedule_work(&led->work);
0196 }
0197 
0198 static int adp8860_led_setup(struct adp8860_led *led)
0199 {
0200     struct i2c_client *client = led->client;
0201     int ret = 0;
0202 
0203     ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
0204     ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
0205 
0206     if (led->id > 4)
0207         ret |= adp8860_set_bits(client, ADP8860_ISCT1,
0208                 (led->flags & 0x3) << ((led->id - 5) * 2));
0209     else
0210         ret |= adp8860_set_bits(client, ADP8860_ISCT2,
0211                 (led->flags & 0x3) << ((led->id - 1) * 2));
0212 
0213     return ret;
0214 }
0215 
0216 static int adp8860_led_probe(struct i2c_client *client)
0217 {
0218     struct adp8860_backlight_platform_data *pdata =
0219         dev_get_platdata(&client->dev);
0220     struct adp8860_bl *data = i2c_get_clientdata(client);
0221     struct adp8860_led *led, *led_dat;
0222     struct led_info *cur_led;
0223     int ret, i;
0224 
0225     led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
0226                 GFP_KERNEL);
0227     if (led == NULL)
0228         return -ENOMEM;
0229 
0230     ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
0231     ret = adp8860_write(client, ADP8860_ISCT1,
0232             (pdata->led_on_time & 0x3) << 6);
0233     ret |= adp8860_write(client, ADP8860_ISCF,
0234             FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
0235 
0236     if (ret) {
0237         dev_err(&client->dev, "failed to write\n");
0238         return ret;
0239     }
0240 
0241     for (i = 0; i < pdata->num_leds; ++i) {
0242         cur_led = &pdata->leds[i];
0243         led_dat = &led[i];
0244 
0245         led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
0246 
0247         if (led_dat->id > 7 || led_dat->id < 1) {
0248             dev_err(&client->dev, "Invalid LED ID %d\n",
0249                 led_dat->id);
0250             ret = -EINVAL;
0251             goto err;
0252         }
0253 
0254         if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
0255             dev_err(&client->dev, "LED %d used by Backlight\n",
0256                 led_dat->id);
0257             ret = -EBUSY;
0258             goto err;
0259         }
0260 
0261         led_dat->cdev.name = cur_led->name;
0262         led_dat->cdev.default_trigger = cur_led->default_trigger;
0263         led_dat->cdev.brightness_set = adp8860_led_set;
0264         led_dat->cdev.brightness = LED_OFF;
0265         led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
0266         led_dat->client = client;
0267         led_dat->new_brightness = LED_OFF;
0268         INIT_WORK(&led_dat->work, adp8860_led_work);
0269 
0270         ret = led_classdev_register(&client->dev, &led_dat->cdev);
0271         if (ret) {
0272             dev_err(&client->dev, "failed to register LED %d\n",
0273                 led_dat->id);
0274             goto err;
0275         }
0276 
0277         ret = adp8860_led_setup(led_dat);
0278         if (ret) {
0279             dev_err(&client->dev, "failed to write\n");
0280             i++;
0281             goto err;
0282         }
0283     }
0284 
0285     data->led = led;
0286 
0287     return 0;
0288 
0289  err:
0290     for (i = i - 1; i >= 0; --i) {
0291         led_classdev_unregister(&led[i].cdev);
0292         cancel_work_sync(&led[i].work);
0293     }
0294 
0295     return ret;
0296 }
0297 
0298 static int adp8860_led_remove(struct i2c_client *client)
0299 {
0300     struct adp8860_backlight_platform_data *pdata =
0301         dev_get_platdata(&client->dev);
0302     struct adp8860_bl *data = i2c_get_clientdata(client);
0303     int i;
0304 
0305     for (i = 0; i < pdata->num_leds; i++) {
0306         led_classdev_unregister(&data->led[i].cdev);
0307         cancel_work_sync(&data->led[i].work);
0308     }
0309 
0310     return 0;
0311 }
0312 #else
0313 static int adp8860_led_probe(struct i2c_client *client)
0314 {
0315     return 0;
0316 }
0317 
0318 static int adp8860_led_remove(struct i2c_client *client)
0319 {
0320     return 0;
0321 }
0322 #endif
0323 
0324 static int adp8860_bl_set(struct backlight_device *bl, int brightness)
0325 {
0326     struct adp8860_bl *data = bl_get_data(bl);
0327     struct i2c_client *client = data->client;
0328     int ret = 0;
0329 
0330     if (data->en_ambl_sens) {
0331         if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
0332             /* Disable Ambient Light auto adjust */
0333             ret |= adp8860_clr_bits(client, ADP8860_MDCR,
0334                     CMP_AUTOEN);
0335             ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
0336         } else {
0337             /*
0338              * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
0339              * restore daylight l1 sysfs brightness
0340              */
0341             ret |= adp8860_write(client, ADP8860_BLMX1,
0342                      data->cached_daylight_max);
0343             ret |= adp8860_set_bits(client, ADP8860_MDCR,
0344                      CMP_AUTOEN);
0345         }
0346     } else
0347         ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
0348 
0349     if (data->current_brightness && brightness == 0)
0350         ret |= adp8860_set_bits(client,
0351                 ADP8860_MDCR, DIM_EN);
0352     else if (data->current_brightness == 0 && brightness)
0353         ret |= adp8860_clr_bits(client,
0354                 ADP8860_MDCR, DIM_EN);
0355 
0356     if (!ret)
0357         data->current_brightness = brightness;
0358 
0359     return ret;
0360 }
0361 
0362 static int adp8860_bl_update_status(struct backlight_device *bl)
0363 {
0364     return adp8860_bl_set(bl, backlight_get_brightness(bl));
0365 }
0366 
0367 static int adp8860_bl_get_brightness(struct backlight_device *bl)
0368 {
0369     struct adp8860_bl *data = bl_get_data(bl);
0370 
0371     return data->current_brightness;
0372 }
0373 
0374 static const struct backlight_ops adp8860_bl_ops = {
0375     .update_status  = adp8860_bl_update_status,
0376     .get_brightness = adp8860_bl_get_brightness,
0377 };
0378 
0379 static int adp8860_bl_setup(struct backlight_device *bl)
0380 {
0381     struct adp8860_bl *data = bl_get_data(bl);
0382     struct i2c_client *client = data->client;
0383     struct adp8860_backlight_platform_data *pdata = data->pdata;
0384     int ret = 0;
0385 
0386     ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
0387     ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
0388     ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
0389 
0390     if (data->en_ambl_sens) {
0391         data->cached_daylight_max = pdata->l1_daylight_max;
0392         ret |= adp8860_write(client, ADP8860_BLMX2,
0393                         pdata->l2_office_max);
0394         ret |= adp8860_write(client, ADP8860_BLDM2,
0395                         pdata->l2_office_dim);
0396         ret |= adp8860_write(client, ADP8860_BLMX3,
0397                         pdata->l3_dark_max);
0398         ret |= adp8860_write(client, ADP8860_BLDM3,
0399                         pdata->l3_dark_dim);
0400 
0401         ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
0402         ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
0403         ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
0404         ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
0405         ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
0406                         ALS_CCFG_VAL(pdata->abml_filt));
0407     }
0408 
0409     ret |= adp8860_write(client, ADP8860_CFGR,
0410             BL_CFGR_VAL(pdata->bl_fade_law, 0));
0411 
0412     ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
0413             pdata->bl_fade_out));
0414 
0415     ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
0416             (data->gdwn_dis ? GDWN_DIS : 0));
0417 
0418     return ret;
0419 }
0420 
0421 static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
0422 {
0423     struct adp8860_bl *data = dev_get_drvdata(dev);
0424     int error;
0425     uint8_t reg_val;
0426 
0427     mutex_lock(&data->lock);
0428     error = adp8860_read(data->client, reg, &reg_val);
0429     mutex_unlock(&data->lock);
0430 
0431     if (error < 0)
0432         return error;
0433 
0434     return sprintf(buf, "%u\n", reg_val);
0435 }
0436 
0437 static ssize_t adp8860_store(struct device *dev, const char *buf,
0438              size_t count, int reg)
0439 {
0440     struct adp8860_bl *data = dev_get_drvdata(dev);
0441     unsigned long val;
0442     int ret;
0443 
0444     ret = kstrtoul(buf, 10, &val);
0445     if (ret)
0446         return ret;
0447 
0448     mutex_lock(&data->lock);
0449     adp8860_write(data->client, reg, val);
0450     mutex_unlock(&data->lock);
0451 
0452     return count;
0453 }
0454 
0455 static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
0456                      struct device_attribute *attr, char *buf)
0457 {
0458     return adp8860_show(dev, buf, ADP8860_BLMX3);
0459 }
0460 
0461 static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
0462         struct device_attribute *attr, const char *buf, size_t count)
0463 {
0464     return adp8860_store(dev, buf, count, ADP8860_BLMX3);
0465 }
0466 
0467 static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
0468             adp8860_bl_l3_dark_max_store);
0469 
0470 static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
0471         struct device_attribute *attr, char *buf)
0472 {
0473     return adp8860_show(dev, buf, ADP8860_BLMX2);
0474 }
0475 
0476 static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
0477         struct device_attribute *attr, const char *buf, size_t count)
0478 {
0479     return adp8860_store(dev, buf, count, ADP8860_BLMX2);
0480 }
0481 static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
0482             adp8860_bl_l2_office_max_store);
0483 
0484 static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
0485             struct device_attribute *attr, char *buf)
0486 {
0487     return adp8860_show(dev, buf, ADP8860_BLMX1);
0488 }
0489 
0490 static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
0491         struct device_attribute *attr, const char *buf, size_t count)
0492 {
0493     struct adp8860_bl *data = dev_get_drvdata(dev);
0494     int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
0495 
0496     if (ret)
0497         return ret;
0498 
0499     return adp8860_store(dev, buf, count, ADP8860_BLMX1);
0500 }
0501 static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
0502             adp8860_bl_l1_daylight_max_store);
0503 
0504 static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
0505             struct device_attribute *attr, char *buf)
0506 {
0507     return adp8860_show(dev, buf, ADP8860_BLDM3);
0508 }
0509 
0510 static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
0511                      struct device_attribute *attr,
0512                      const char *buf, size_t count)
0513 {
0514     return adp8860_store(dev, buf, count, ADP8860_BLDM3);
0515 }
0516 static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
0517             adp8860_bl_l3_dark_dim_store);
0518 
0519 static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
0520             struct device_attribute *attr, char *buf)
0521 {
0522     return adp8860_show(dev, buf, ADP8860_BLDM2);
0523 }
0524 
0525 static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
0526                      struct device_attribute *attr,
0527                      const char *buf, size_t count)
0528 {
0529     return adp8860_store(dev, buf, count, ADP8860_BLDM2);
0530 }
0531 static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
0532             adp8860_bl_l2_office_dim_store);
0533 
0534 static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
0535                      struct device_attribute *attr, char *buf)
0536 {
0537     return adp8860_show(dev, buf, ADP8860_BLDM1);
0538 }
0539 
0540 static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
0541                      struct device_attribute *attr,
0542                      const char *buf, size_t count)
0543 {
0544     return adp8860_store(dev, buf, count, ADP8860_BLDM1);
0545 }
0546 static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
0547             adp8860_bl_l1_daylight_dim_store);
0548 
0549 #ifdef ADP8860_EXT_FEATURES
0550 static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
0551                      struct device_attribute *attr, char *buf)
0552 {
0553     struct adp8860_bl *data = dev_get_drvdata(dev);
0554     int error;
0555     uint8_t reg_val;
0556     uint16_t ret_val;
0557 
0558     mutex_lock(&data->lock);
0559     error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
0560     if (!error) {
0561         ret_val = reg_val;
0562         error = adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
0563     }
0564     mutex_unlock(&data->lock);
0565 
0566     if (error)
0567         return error;
0568 
0569     /* Return 13-bit conversion value for the first light sensor */
0570     ret_val += (reg_val & 0x1F) << 8;
0571 
0572     return sprintf(buf, "%u\n", ret_val);
0573 }
0574 static DEVICE_ATTR(ambient_light_level, 0444,
0575         adp8860_bl_ambient_light_level_show, NULL);
0576 
0577 static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
0578                      struct device_attribute *attr, char *buf)
0579 {
0580     struct adp8860_bl *data = dev_get_drvdata(dev);
0581     int error;
0582     uint8_t reg_val;
0583 
0584     mutex_lock(&data->lock);
0585     error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
0586     mutex_unlock(&data->lock);
0587 
0588     if (error < 0)
0589         return error;
0590 
0591     return sprintf(buf, "%u\n",
0592         ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
0593 }
0594 
0595 static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
0596                      struct device_attribute *attr,
0597                      const char *buf, size_t count)
0598 {
0599     struct adp8860_bl *data = dev_get_drvdata(dev);
0600     unsigned long val;
0601     uint8_t reg_val;
0602     int ret;
0603 
0604     ret = kstrtoul(buf, 10, &val);
0605     if (ret)
0606         return ret;
0607 
0608     if (val == 0) {
0609         /* Enable automatic ambient light sensing */
0610         adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
0611     } else if ((val > 0) && (val <= 3)) {
0612         /* Disable automatic ambient light sensing */
0613         adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
0614 
0615         /* Set user supplied ambient light zone */
0616         mutex_lock(&data->lock);
0617         ret = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
0618         if (!ret) {
0619             reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
0620             reg_val |= (val - 1) << CFGR_BLV_SHIFT;
0621             adp8860_write(data->client, ADP8860_CFGR, reg_val);
0622         }
0623         mutex_unlock(&data->lock);
0624     }
0625 
0626     return count;
0627 }
0628 static DEVICE_ATTR(ambient_light_zone, 0664,
0629         adp8860_bl_ambient_light_zone_show,
0630         adp8860_bl_ambient_light_zone_store);
0631 #endif
0632 
0633 static struct attribute *adp8860_bl_attributes[] = {
0634     &dev_attr_l3_dark_max.attr,
0635     &dev_attr_l3_dark_dim.attr,
0636     &dev_attr_l2_office_max.attr,
0637     &dev_attr_l2_office_dim.attr,
0638     &dev_attr_l1_daylight_max.attr,
0639     &dev_attr_l1_daylight_dim.attr,
0640 #ifdef ADP8860_EXT_FEATURES
0641     &dev_attr_ambient_light_level.attr,
0642     &dev_attr_ambient_light_zone.attr,
0643 #endif
0644     NULL
0645 };
0646 
0647 static const struct attribute_group adp8860_bl_attr_group = {
0648     .attrs = adp8860_bl_attributes,
0649 };
0650 
0651 static int adp8860_probe(struct i2c_client *client,
0652                     const struct i2c_device_id *id)
0653 {
0654     struct backlight_device *bl;
0655     struct adp8860_bl *data;
0656     struct adp8860_backlight_platform_data *pdata =
0657         dev_get_platdata(&client->dev);
0658     struct backlight_properties props;
0659     uint8_t reg_val;
0660     int ret;
0661 
0662     if (!i2c_check_functionality(client->adapter,
0663                     I2C_FUNC_SMBUS_BYTE_DATA)) {
0664         dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
0665         return -EIO;
0666     }
0667 
0668     if (!pdata) {
0669         dev_err(&client->dev, "no platform data?\n");
0670         return -EINVAL;
0671     }
0672 
0673     data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
0674     if (data == NULL)
0675         return -ENOMEM;
0676 
0677     ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
0678     if (ret < 0)
0679         return ret;
0680 
0681     switch (ADP8860_MANID(reg_val)) {
0682     case ADP8863_MANUFID:
0683         data->gdwn_dis = !!pdata->gdwn_dis;
0684         fallthrough;
0685     case ADP8860_MANUFID:
0686         data->en_ambl_sens = !!pdata->en_ambl_sens;
0687         break;
0688     case ADP8861_MANUFID:
0689         data->gdwn_dis = !!pdata->gdwn_dis;
0690         break;
0691     default:
0692         dev_err(&client->dev, "failed to probe\n");
0693         return -ENODEV;
0694     }
0695 
0696     /* It's confirmed that the DEVID field is actually a REVID */
0697 
0698     data->revid = ADP8860_DEVID(reg_val);
0699     data->client = client;
0700     data->pdata = pdata;
0701     data->id = id->driver_data;
0702     data->current_brightness = 0;
0703     i2c_set_clientdata(client, data);
0704 
0705     memset(&props, 0, sizeof(props));
0706     props.type = BACKLIGHT_RAW;
0707     props.max_brightness = ADP8860_MAX_BRIGHTNESS;
0708 
0709     mutex_init(&data->lock);
0710 
0711     bl = devm_backlight_device_register(&client->dev,
0712                 dev_driver_string(&client->dev),
0713                 &client->dev, data, &adp8860_bl_ops, &props);
0714     if (IS_ERR(bl)) {
0715         dev_err(&client->dev, "failed to register backlight\n");
0716         return PTR_ERR(bl);
0717     }
0718 
0719     bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
0720 
0721     data->bl = bl;
0722 
0723     if (data->en_ambl_sens)
0724         ret = sysfs_create_group(&bl->dev.kobj,
0725             &adp8860_bl_attr_group);
0726 
0727     if (ret) {
0728         dev_err(&client->dev, "failed to register sysfs\n");
0729         return ret;
0730     }
0731 
0732     ret = adp8860_bl_setup(bl);
0733     if (ret) {
0734         ret = -EIO;
0735         goto out;
0736     }
0737 
0738     backlight_update_status(bl);
0739 
0740     dev_info(&client->dev, "%s Rev.%d Backlight\n",
0741         client->name, data->revid);
0742 
0743     if (pdata->num_leds)
0744         adp8860_led_probe(client);
0745 
0746     return 0;
0747 
0748 out:
0749     if (data->en_ambl_sens)
0750         sysfs_remove_group(&data->bl->dev.kobj,
0751             &adp8860_bl_attr_group);
0752 
0753     return ret;
0754 }
0755 
0756 static int adp8860_remove(struct i2c_client *client)
0757 {
0758     struct adp8860_bl *data = i2c_get_clientdata(client);
0759 
0760     adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
0761 
0762     if (data->led)
0763         adp8860_led_remove(client);
0764 
0765     if (data->en_ambl_sens)
0766         sysfs_remove_group(&data->bl->dev.kobj,
0767             &adp8860_bl_attr_group);
0768 
0769     return 0;
0770 }
0771 
0772 #ifdef CONFIG_PM_SLEEP
0773 static int adp8860_i2c_suspend(struct device *dev)
0774 {
0775     struct i2c_client *client = to_i2c_client(dev);
0776 
0777     adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
0778 
0779     return 0;
0780 }
0781 
0782 static int adp8860_i2c_resume(struct device *dev)
0783 {
0784     struct i2c_client *client = to_i2c_client(dev);
0785 
0786     adp8860_set_bits(client, ADP8860_MDCR, NSTBY | BLEN);
0787 
0788     return 0;
0789 }
0790 #endif
0791 
0792 static SIMPLE_DEV_PM_OPS(adp8860_i2c_pm_ops, adp8860_i2c_suspend,
0793             adp8860_i2c_resume);
0794 
0795 static const struct i2c_device_id adp8860_id[] = {
0796     { "adp8860", adp8860 },
0797     { "adp8861", adp8861 },
0798     { "adp8863", adp8863 },
0799     { }
0800 };
0801 MODULE_DEVICE_TABLE(i2c, adp8860_id);
0802 
0803 static struct i2c_driver adp8860_driver = {
0804     .driver = {
0805         .name   = KBUILD_MODNAME,
0806         .pm = &adp8860_i2c_pm_ops,
0807     },
0808     .probe    = adp8860_probe,
0809     .remove   = adp8860_remove,
0810     .id_table = adp8860_id,
0811 };
0812 
0813 module_i2c_driver(adp8860_driver);
0814 
0815 MODULE_LICENSE("GPL v2");
0816 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
0817 MODULE_DESCRIPTION("ADP8860 Backlight driver");