Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * A iio driver for the light sensor ISL 29018/29023/29035.
0004  *
0005  * IIO driver for monitoring ambient light intensity in luxi, proximity
0006  * sensing and infrared sensing.
0007  *
0008  * Copyright (c) 2010, NVIDIA Corporation.
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/i2c.h>
0013 #include <linux/err.h>
0014 #include <linux/mutex.h>
0015 #include <linux/delay.h>
0016 #include <linux/regmap.h>
0017 #include <linux/regulator/consumer.h>
0018 #include <linux/slab.h>
0019 #include <linux/iio/iio.h>
0020 #include <linux/iio/sysfs.h>
0021 #include <linux/acpi.h>
0022 
0023 #define ISL29018_CONV_TIME_MS       100
0024 
0025 #define ISL29018_REG_ADD_COMMAND1   0x00
0026 #define ISL29018_CMD1_OPMODE_SHIFT  5
0027 #define ISL29018_CMD1_OPMODE_MASK   (7 << ISL29018_CMD1_OPMODE_SHIFT)
0028 #define ISL29018_CMD1_OPMODE_POWER_DOWN 0
0029 #define ISL29018_CMD1_OPMODE_ALS_ONCE   1
0030 #define ISL29018_CMD1_OPMODE_IR_ONCE    2
0031 #define ISL29018_CMD1_OPMODE_PROX_ONCE  3
0032 
0033 #define ISL29018_REG_ADD_COMMAND2   0x01
0034 #define ISL29018_CMD2_RESOLUTION_SHIFT  2
0035 #define ISL29018_CMD2_RESOLUTION_MASK   (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
0036 
0037 #define ISL29018_CMD2_RANGE_SHIFT   0
0038 #define ISL29018_CMD2_RANGE_MASK    (0x3 << ISL29018_CMD2_RANGE_SHIFT)
0039 
0040 #define ISL29018_CMD2_SCHEME_SHIFT  7
0041 #define ISL29018_CMD2_SCHEME_MASK   (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
0042 
0043 #define ISL29018_REG_ADD_DATA_LSB   0x02
0044 #define ISL29018_REG_ADD_DATA_MSB   0x03
0045 
0046 #define ISL29018_REG_TEST       0x08
0047 #define ISL29018_TEST_SHIFT     0
0048 #define ISL29018_TEST_MASK      (0xFF << ISL29018_TEST_SHIFT)
0049 
0050 #define ISL29035_REG_DEVICE_ID      0x0F
0051 #define ISL29035_DEVICE_ID_SHIFT    0x03
0052 #define ISL29035_DEVICE_ID_MASK     (0x7 << ISL29035_DEVICE_ID_SHIFT)
0053 #define ISL29035_DEVICE_ID      0x5
0054 #define ISL29035_BOUT_SHIFT     0x07
0055 #define ISL29035_BOUT_MASK      (0x01 << ISL29035_BOUT_SHIFT)
0056 
0057 enum isl29018_int_time {
0058     ISL29018_INT_TIME_16,
0059     ISL29018_INT_TIME_12,
0060     ISL29018_INT_TIME_8,
0061     ISL29018_INT_TIME_4,
0062 };
0063 
0064 static const unsigned int isl29018_int_utimes[3][4] = {
0065     {90000, 5630, 351, 21},
0066     {90000, 5600, 352, 22},
0067     {105000, 6500, 410, 25},
0068 };
0069 
0070 static const struct isl29018_scale {
0071     unsigned int scale;
0072     unsigned int uscale;
0073 } isl29018_scales[4][4] = {
0074     { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
0075     { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
0076     { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
0077     { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
0078 };
0079 
0080 struct isl29018_chip {
0081     struct regmap       *regmap;
0082     struct mutex        lock;
0083     int         type;
0084     unsigned int        calibscale;
0085     unsigned int        ucalibscale;
0086     unsigned int        int_time;
0087     struct isl29018_scale   scale;
0088     int         prox_scheme;
0089     bool            suspended;
0090     struct regulator    *vcc_reg;
0091 };
0092 
0093 static int isl29018_set_integration_time(struct isl29018_chip *chip,
0094                      unsigned int utime)
0095 {
0096     unsigned int i;
0097     int ret;
0098     unsigned int int_time, new_int_time;
0099 
0100     for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
0101         if (utime == isl29018_int_utimes[chip->type][i]) {
0102             new_int_time = i;
0103             break;
0104         }
0105     }
0106 
0107     if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
0108         return -EINVAL;
0109 
0110     ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
0111                  ISL29018_CMD2_RESOLUTION_MASK,
0112                  i << ISL29018_CMD2_RESOLUTION_SHIFT);
0113     if (ret < 0)
0114         return ret;
0115 
0116     /* Keep the same range when integration time changes */
0117     int_time = chip->int_time;
0118     for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
0119         if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
0120             chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
0121             chip->scale = isl29018_scales[new_int_time][i];
0122             break;
0123         }
0124     }
0125     chip->int_time = new_int_time;
0126 
0127     return 0;
0128 }
0129 
0130 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
0131 {
0132     unsigned int i;
0133     int ret;
0134     struct isl29018_scale new_scale;
0135 
0136     for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
0137         if (scale == isl29018_scales[chip->int_time][i].scale &&
0138             uscale == isl29018_scales[chip->int_time][i].uscale) {
0139             new_scale = isl29018_scales[chip->int_time][i];
0140             break;
0141         }
0142     }
0143 
0144     if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
0145         return -EINVAL;
0146 
0147     ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
0148                  ISL29018_CMD2_RANGE_MASK,
0149                  i << ISL29018_CMD2_RANGE_SHIFT);
0150     if (ret < 0)
0151         return ret;
0152 
0153     chip->scale = new_scale;
0154 
0155     return 0;
0156 }
0157 
0158 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
0159 {
0160     int status;
0161     unsigned int lsb;
0162     unsigned int msb;
0163     struct device *dev = regmap_get_device(chip->regmap);
0164 
0165     /* Set mode */
0166     status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
0167                   mode << ISL29018_CMD1_OPMODE_SHIFT);
0168     if (status) {
0169         dev_err(dev,
0170             "Error in setting operating mode err %d\n", status);
0171         return status;
0172     }
0173     msleep(ISL29018_CONV_TIME_MS);
0174     status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
0175     if (status < 0) {
0176         dev_err(dev,
0177             "Error in reading LSB DATA with err %d\n", status);
0178         return status;
0179     }
0180 
0181     status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
0182     if (status < 0) {
0183         dev_err(dev,
0184             "Error in reading MSB DATA with error %d\n", status);
0185         return status;
0186     }
0187     dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
0188 
0189     return (msb << 8) | lsb;
0190 }
0191 
0192 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
0193 {
0194     int lux_data;
0195     unsigned int data_x_range;
0196 
0197     lux_data = isl29018_read_sensor_input(chip,
0198                           ISL29018_CMD1_OPMODE_ALS_ONCE);
0199     if (lux_data < 0)
0200         return lux_data;
0201 
0202     data_x_range = lux_data * chip->scale.scale +
0203                lux_data * chip->scale.uscale / 1000000;
0204     *lux = data_x_range * chip->calibscale +
0205            data_x_range * chip->ucalibscale / 1000000;
0206 
0207     return 0;
0208 }
0209 
0210 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
0211 {
0212     int ir_data;
0213 
0214     ir_data = isl29018_read_sensor_input(chip,
0215                          ISL29018_CMD1_OPMODE_IR_ONCE);
0216     if (ir_data < 0)
0217         return ir_data;
0218 
0219     *ir = ir_data;
0220 
0221     return 0;
0222 }
0223 
0224 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
0225                       int *near_ir)
0226 {
0227     int status;
0228     int prox_data = -1;
0229     int ir_data = -1;
0230     struct device *dev = regmap_get_device(chip->regmap);
0231 
0232     /* Do proximity sensing with required scheme */
0233     status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
0234                     ISL29018_CMD2_SCHEME_MASK,
0235                     scheme << ISL29018_CMD2_SCHEME_SHIFT);
0236     if (status) {
0237         dev_err(dev, "Error in setting operating mode\n");
0238         return status;
0239     }
0240 
0241     prox_data = isl29018_read_sensor_input(chip,
0242                            ISL29018_CMD1_OPMODE_PROX_ONCE);
0243     if (prox_data < 0)
0244         return prox_data;
0245 
0246     if (scheme == 1) {
0247         *near_ir = prox_data;
0248         return 0;
0249     }
0250 
0251     ir_data = isl29018_read_sensor_input(chip,
0252                          ISL29018_CMD1_OPMODE_IR_ONCE);
0253     if (ir_data < 0)
0254         return ir_data;
0255 
0256     if (prox_data >= ir_data)
0257         *near_ir = prox_data - ir_data;
0258     else
0259         *near_ir = 0;
0260 
0261     return 0;
0262 }
0263 
0264 static ssize_t in_illuminance_scale_available_show
0265             (struct device *dev, struct device_attribute *attr,
0266              char *buf)
0267 {
0268     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0269     struct isl29018_chip *chip = iio_priv(indio_dev);
0270     unsigned int i;
0271     int len = 0;
0272 
0273     mutex_lock(&chip->lock);
0274     for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
0275         len += sprintf(buf + len, "%d.%06d ",
0276                    isl29018_scales[chip->int_time][i].scale,
0277                    isl29018_scales[chip->int_time][i].uscale);
0278     mutex_unlock(&chip->lock);
0279 
0280     buf[len - 1] = '\n';
0281 
0282     return len;
0283 }
0284 
0285 static ssize_t in_illuminance_integration_time_available_show
0286             (struct device *dev, struct device_attribute *attr,
0287              char *buf)
0288 {
0289     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0290     struct isl29018_chip *chip = iio_priv(indio_dev);
0291     unsigned int i;
0292     int len = 0;
0293 
0294     for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
0295         len += sprintf(buf + len, "0.%06d ",
0296                    isl29018_int_utimes[chip->type][i]);
0297 
0298     buf[len - 1] = '\n';
0299 
0300     return len;
0301 }
0302 
0303 /*
0304  * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
0305  * infrared suppression:
0306  *
0307  *   Proximity Sensing Scheme: Bit 7. This bit programs the function
0308  * of the proximity detection. Logic 0 of this bit, Scheme 0, makes
0309  * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range
0310  * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit,
0311  * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary)
0312  * proximity_less_ambient detection. The range of Scheme 1
0313  * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended
0314  * for resolutions less than 16. While Scheme 0 has wider dynamic
0315  * range, Scheme 1 proximity detection is less affected by the
0316  * ambient IR noise variation.
0317  *
0318  * 0 Sensing IR from LED and ambient
0319  * 1 Sensing IR from LED with ambient IR rejection
0320  */
0321 static ssize_t proximity_on_chip_ambient_infrared_suppression_show
0322             (struct device *dev, struct device_attribute *attr,
0323              char *buf)
0324 {
0325     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0326     struct isl29018_chip *chip = iio_priv(indio_dev);
0327 
0328     /*
0329      * Return the "proximity scheme" i.e. if the chip does on chip
0330      * infrared suppression (1 means perform on chip suppression)
0331      */
0332     return sprintf(buf, "%d\n", chip->prox_scheme);
0333 }
0334 
0335 static ssize_t proximity_on_chip_ambient_infrared_suppression_store
0336             (struct device *dev, struct device_attribute *attr,
0337              const char *buf, size_t count)
0338 {
0339     struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0340     struct isl29018_chip *chip = iio_priv(indio_dev);
0341     int val;
0342 
0343     if (kstrtoint(buf, 10, &val))
0344         return -EINVAL;
0345     if (!(val == 0 || val == 1))
0346         return -EINVAL;
0347 
0348     /*
0349      * Get the "proximity scheme" i.e. if the chip does on chip
0350      * infrared suppression (1 means perform on chip suppression)
0351      */
0352     mutex_lock(&chip->lock);
0353     chip->prox_scheme = val;
0354     mutex_unlock(&chip->lock);
0355 
0356     return count;
0357 }
0358 
0359 static int isl29018_write_raw(struct iio_dev *indio_dev,
0360                   struct iio_chan_spec const *chan,
0361                   int val,
0362                   int val2,
0363                   long mask)
0364 {
0365     struct isl29018_chip *chip = iio_priv(indio_dev);
0366     int ret = -EINVAL;
0367 
0368     mutex_lock(&chip->lock);
0369     if (chip->suspended) {
0370         ret = -EBUSY;
0371         goto write_done;
0372     }
0373     switch (mask) {
0374     case IIO_CHAN_INFO_CALIBSCALE:
0375         if (chan->type == IIO_LIGHT) {
0376             chip->calibscale = val;
0377             chip->ucalibscale = val2;
0378             ret = 0;
0379         }
0380         break;
0381     case IIO_CHAN_INFO_INT_TIME:
0382         if (chan->type == IIO_LIGHT && !val)
0383             ret = isl29018_set_integration_time(chip, val2);
0384         break;
0385     case IIO_CHAN_INFO_SCALE:
0386         if (chan->type == IIO_LIGHT)
0387             ret = isl29018_set_scale(chip, val, val2);
0388         break;
0389     default:
0390         break;
0391     }
0392 
0393 write_done:
0394     mutex_unlock(&chip->lock);
0395 
0396     return ret;
0397 }
0398 
0399 static int isl29018_read_raw(struct iio_dev *indio_dev,
0400                  struct iio_chan_spec const *chan,
0401                  int *val,
0402                  int *val2,
0403                  long mask)
0404 {
0405     int ret = -EINVAL;
0406     struct isl29018_chip *chip = iio_priv(indio_dev);
0407 
0408     mutex_lock(&chip->lock);
0409     if (chip->suspended) {
0410         ret = -EBUSY;
0411         goto read_done;
0412     }
0413     switch (mask) {
0414     case IIO_CHAN_INFO_RAW:
0415     case IIO_CHAN_INFO_PROCESSED:
0416         switch (chan->type) {
0417         case IIO_LIGHT:
0418             ret = isl29018_read_lux(chip, val);
0419             break;
0420         case IIO_INTENSITY:
0421             ret = isl29018_read_ir(chip, val);
0422             break;
0423         case IIO_PROXIMITY:
0424             ret = isl29018_read_proximity_ir(chip,
0425                              chip->prox_scheme,
0426                              val);
0427             break;
0428         default:
0429             break;
0430         }
0431         if (!ret)
0432             ret = IIO_VAL_INT;
0433         break;
0434     case IIO_CHAN_INFO_INT_TIME:
0435         if (chan->type == IIO_LIGHT) {
0436             *val = 0;
0437             *val2 = isl29018_int_utimes[chip->type][chip->int_time];
0438             ret = IIO_VAL_INT_PLUS_MICRO;
0439         }
0440         break;
0441     case IIO_CHAN_INFO_SCALE:
0442         if (chan->type == IIO_LIGHT) {
0443             *val = chip->scale.scale;
0444             *val2 = chip->scale.uscale;
0445             ret = IIO_VAL_INT_PLUS_MICRO;
0446         }
0447         break;
0448     case IIO_CHAN_INFO_CALIBSCALE:
0449         if (chan->type == IIO_LIGHT) {
0450             *val = chip->calibscale;
0451             *val2 = chip->ucalibscale;
0452             ret = IIO_VAL_INT_PLUS_MICRO;
0453         }
0454         break;
0455     default:
0456         break;
0457     }
0458 
0459 read_done:
0460     mutex_unlock(&chip->lock);
0461 
0462     return ret;
0463 }
0464 
0465 #define ISL29018_LIGHT_CHANNEL {                    \
0466     .type = IIO_LIGHT,                      \
0467     .indexed = 1,                           \
0468     .channel = 0,                           \
0469     .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |        \
0470     BIT(IIO_CHAN_INFO_CALIBSCALE) |                 \
0471     BIT(IIO_CHAN_INFO_SCALE) |                  \
0472     BIT(IIO_CHAN_INFO_INT_TIME),                    \
0473 }
0474 
0475 #define ISL29018_IR_CHANNEL {                       \
0476     .type = IIO_INTENSITY,                      \
0477     .modified = 1,                          \
0478     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
0479     .channel2 = IIO_MOD_LIGHT_IR,                   \
0480 }
0481 
0482 #define ISL29018_PROXIMITY_CHANNEL {                    \
0483     .type = IIO_PROXIMITY,                      \
0484     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
0485 }
0486 
0487 static const struct iio_chan_spec isl29018_channels[] = {
0488     ISL29018_LIGHT_CHANNEL,
0489     ISL29018_IR_CHANNEL,
0490     ISL29018_PROXIMITY_CHANNEL,
0491 };
0492 
0493 static const struct iio_chan_spec isl29023_channels[] = {
0494     ISL29018_LIGHT_CHANNEL,
0495     ISL29018_IR_CHANNEL,
0496 };
0497 
0498 static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
0499 static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
0500 static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
0501 
0502 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
0503 
0504 static struct attribute *isl29018_attributes[] = {
0505     ISL29018_DEV_ATTR(in_illuminance_scale_available),
0506     ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
0507     ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
0508     NULL
0509 };
0510 
0511 static struct attribute *isl29023_attributes[] = {
0512     ISL29018_DEV_ATTR(in_illuminance_scale_available),
0513     ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
0514     NULL
0515 };
0516 
0517 static const struct attribute_group isl29018_group = {
0518     .attrs = isl29018_attributes,
0519 };
0520 
0521 static const struct attribute_group isl29023_group = {
0522     .attrs = isl29023_attributes,
0523 };
0524 
0525 enum {
0526     isl29018,
0527     isl29023,
0528     isl29035,
0529 };
0530 
0531 static int isl29018_chip_init(struct isl29018_chip *chip)
0532 {
0533     int status;
0534     struct device *dev = regmap_get_device(chip->regmap);
0535 
0536     if (chip->type == isl29035) {
0537         unsigned int id;
0538 
0539         status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
0540         if (status < 0) {
0541             dev_err(dev,
0542                 "Error reading ID register with error %d\n",
0543                 status);
0544             return status;
0545         }
0546 
0547         id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
0548 
0549         if (id != ISL29035_DEVICE_ID)
0550             return -ENODEV;
0551 
0552         /* Clear brownout bit */
0553         status = regmap_update_bits(chip->regmap,
0554                         ISL29035_REG_DEVICE_ID,
0555                         ISL29035_BOUT_MASK, 0);
0556         if (status < 0)
0557             return status;
0558     }
0559 
0560     /*
0561      * Code added per Intersil Application Note 1534:
0562      *     When VDD sinks to approximately 1.8V or below, some of
0563      * the part's registers may change their state. When VDD
0564      * recovers to 2.25V (or greater), the part may thus be in an
0565      * unknown mode of operation. The user can return the part to
0566      * a known mode of operation either by (a) setting VDD = 0V for
0567      * 1 second or more and then powering back up with a slew rate
0568      * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
0569      * conversions, clear the test registers, and then rewrite all
0570      * registers to the desired values.
0571      * ...
0572      * For ISL29011, ISL29018, ISL29021, ISL29023
0573      * 1. Write 0x00 to register 0x08 (TEST)
0574      * 2. Write 0x00 to register 0x00 (CMD1)
0575      * 3. Rewrite all registers to the desired values
0576      *
0577      * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
0578      * the same thing EXCEPT the data sheet asks for a 1ms delay after
0579      * writing the CMD1 register.
0580      */
0581     status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
0582     if (status < 0) {
0583         dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
0584             status);
0585         return status;
0586     }
0587 
0588     /*
0589      * See Intersil AN1534 comments above.
0590      * "Operating Mode" (COMMAND1) register is reprogrammed when
0591      * data is read from the device.
0592      */
0593     status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
0594     if (status < 0) {
0595         dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
0596             status);
0597         return status;
0598     }
0599 
0600     usleep_range(1000, 2000);   /* per data sheet, page 10 */
0601 
0602     /* Set defaults */
0603     status = isl29018_set_scale(chip, chip->scale.scale,
0604                     chip->scale.uscale);
0605     if (status < 0) {
0606         dev_err(dev, "Init of isl29018 fails\n");
0607         return status;
0608     }
0609 
0610     status = isl29018_set_integration_time(chip,
0611             isl29018_int_utimes[chip->type][chip->int_time]);
0612     if (status < 0)
0613         dev_err(dev, "Init of isl29018 fails\n");
0614 
0615     return status;
0616 }
0617 
0618 static const struct iio_info isl29018_info = {
0619     .attrs = &isl29018_group,
0620     .read_raw = isl29018_read_raw,
0621     .write_raw = isl29018_write_raw,
0622 };
0623 
0624 static const struct iio_info isl29023_info = {
0625     .attrs = &isl29023_group,
0626     .read_raw = isl29018_read_raw,
0627     .write_raw = isl29018_write_raw,
0628 };
0629 
0630 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
0631 {
0632     switch (reg) {
0633     case ISL29018_REG_ADD_DATA_LSB:
0634     case ISL29018_REG_ADD_DATA_MSB:
0635     case ISL29018_REG_ADD_COMMAND1:
0636     case ISL29018_REG_TEST:
0637     case ISL29035_REG_DEVICE_ID:
0638         return true;
0639     default:
0640         return false;
0641     }
0642 }
0643 
0644 static const struct regmap_config isl29018_regmap_config = {
0645     .reg_bits = 8,
0646     .val_bits = 8,
0647     .volatile_reg = isl29018_is_volatile_reg,
0648     .max_register = ISL29018_REG_TEST,
0649     .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
0650     .cache_type = REGCACHE_RBTREE,
0651 };
0652 
0653 static const struct regmap_config isl29035_regmap_config = {
0654     .reg_bits = 8,
0655     .val_bits = 8,
0656     .volatile_reg = isl29018_is_volatile_reg,
0657     .max_register = ISL29035_REG_DEVICE_ID,
0658     .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
0659     .cache_type = REGCACHE_RBTREE,
0660 };
0661 
0662 struct isl29018_chip_info {
0663     const struct iio_chan_spec *channels;
0664     int num_channels;
0665     const struct iio_info *indio_info;
0666     const struct regmap_config *regmap_cfg;
0667 };
0668 
0669 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
0670     [isl29018] = {
0671         .channels = isl29018_channels,
0672         .num_channels = ARRAY_SIZE(isl29018_channels),
0673         .indio_info = &isl29018_info,
0674         .regmap_cfg = &isl29018_regmap_config,
0675     },
0676     [isl29023] = {
0677         .channels = isl29023_channels,
0678         .num_channels = ARRAY_SIZE(isl29023_channels),
0679         .indio_info = &isl29023_info,
0680         .regmap_cfg = &isl29018_regmap_config,
0681     },
0682     [isl29035] = {
0683         .channels = isl29023_channels,
0684         .num_channels = ARRAY_SIZE(isl29023_channels),
0685         .indio_info = &isl29023_info,
0686         .regmap_cfg = &isl29035_regmap_config,
0687     },
0688 };
0689 
0690 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
0691 {
0692     const struct acpi_device_id *id;
0693 
0694     id = acpi_match_device(dev->driver->acpi_match_table, dev);
0695 
0696     if (!id)
0697         return NULL;
0698 
0699     *data = (int)id->driver_data;
0700 
0701     return dev_name(dev);
0702 }
0703 
0704 static void isl29018_disable_regulator_action(void *_data)
0705 {
0706     struct isl29018_chip *chip = _data;
0707     int err;
0708 
0709     err = regulator_disable(chip->vcc_reg);
0710     if (err)
0711         pr_err("failed to disable isl29018's VCC regulator!\n");
0712 }
0713 
0714 static int isl29018_probe(struct i2c_client *client,
0715               const struct i2c_device_id *id)
0716 {
0717     struct isl29018_chip *chip;
0718     struct iio_dev *indio_dev;
0719     int err;
0720     const char *name = NULL;
0721     int dev_id = 0;
0722 
0723     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
0724     if (!indio_dev)
0725         return -ENOMEM;
0726 
0727     chip = iio_priv(indio_dev);
0728 
0729     i2c_set_clientdata(client, indio_dev);
0730 
0731     if (id) {
0732         name = id->name;
0733         dev_id = id->driver_data;
0734     }
0735 
0736     if (ACPI_HANDLE(&client->dev))
0737         name = isl29018_match_acpi_device(&client->dev, &dev_id);
0738 
0739     mutex_init(&chip->lock);
0740 
0741     chip->type = dev_id;
0742     chip->calibscale = 1;
0743     chip->ucalibscale = 0;
0744     chip->int_time = ISL29018_INT_TIME_16;
0745     chip->scale = isl29018_scales[chip->int_time][0];
0746     chip->suspended = false;
0747 
0748     chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
0749     if (IS_ERR(chip->vcc_reg))
0750         return dev_err_probe(&client->dev, PTR_ERR(chip->vcc_reg),
0751                      "failed to get VCC regulator!\n");
0752 
0753     err = regulator_enable(chip->vcc_reg);
0754     if (err) {
0755         dev_err(&client->dev, "failed to enable VCC regulator!\n");
0756         return err;
0757     }
0758 
0759     err = devm_add_action_or_reset(&client->dev, isl29018_disable_regulator_action,
0760                  chip);
0761     if (err) {
0762         dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
0763         return err;
0764     }
0765 
0766     chip->regmap = devm_regmap_init_i2c(client,
0767                 isl29018_chip_info_tbl[dev_id].regmap_cfg);
0768     if (IS_ERR(chip->regmap)) {
0769         err = PTR_ERR(chip->regmap);
0770         dev_err(&client->dev, "regmap initialization fails: %d\n", err);
0771         return err;
0772     }
0773 
0774     err = isl29018_chip_init(chip);
0775     if (err)
0776         return err;
0777 
0778     indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
0779     indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
0780     indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
0781     indio_dev->name = name;
0782     indio_dev->modes = INDIO_DIRECT_MODE;
0783 
0784     return devm_iio_device_register(&client->dev, indio_dev);
0785 }
0786 
0787 static int isl29018_suspend(struct device *dev)
0788 {
0789     struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
0790     int ret;
0791 
0792     mutex_lock(&chip->lock);
0793 
0794     /*
0795      * Since this driver uses only polling commands, we are by default in
0796      * auto shutdown (ie, power-down) mode.
0797      * So we do not have much to do here.
0798      */
0799     chip->suspended = true;
0800     ret = regulator_disable(chip->vcc_reg);
0801     if (ret)
0802         dev_err(dev, "failed to disable VCC regulator\n");
0803 
0804     mutex_unlock(&chip->lock);
0805 
0806     return ret;
0807 }
0808 
0809 static int isl29018_resume(struct device *dev)
0810 {
0811     struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
0812     int err;
0813 
0814     mutex_lock(&chip->lock);
0815 
0816     err = regulator_enable(chip->vcc_reg);
0817     if (err) {
0818         dev_err(dev, "failed to enable VCC regulator\n");
0819         mutex_unlock(&chip->lock);
0820         return err;
0821     }
0822 
0823     err = isl29018_chip_init(chip);
0824     if (!err)
0825         chip->suspended = false;
0826 
0827     mutex_unlock(&chip->lock);
0828 
0829     return err;
0830 }
0831 
0832 static DEFINE_SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend,
0833                 isl29018_resume);
0834 
0835 #ifdef CONFIG_ACPI
0836 static const struct acpi_device_id isl29018_acpi_match[] = {
0837     {"ISL29018", isl29018},
0838     {"ISL29023", isl29023},
0839     {"ISL29035", isl29035},
0840     {},
0841 };
0842 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
0843 #endif
0844 
0845 static const struct i2c_device_id isl29018_id[] = {
0846     {"isl29018", isl29018},
0847     {"isl29023", isl29023},
0848     {"isl29035", isl29035},
0849     {}
0850 };
0851 MODULE_DEVICE_TABLE(i2c, isl29018_id);
0852 
0853 static const struct of_device_id isl29018_of_match[] = {
0854     { .compatible = "isil,isl29018", },
0855     { .compatible = "isil,isl29023", },
0856     { .compatible = "isil,isl29035", },
0857     { },
0858 };
0859 MODULE_DEVICE_TABLE(of, isl29018_of_match);
0860 
0861 static struct i2c_driver isl29018_driver = {
0862     .driver  = {
0863             .name = "isl29018",
0864             .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
0865             .pm = pm_sleep_ptr(&isl29018_pm_ops),
0866             .of_match_table = isl29018_of_match,
0867             },
0868     .probe   = isl29018_probe,
0869     .id_table = isl29018_id,
0870 };
0871 module_i2c_driver(isl29018_driver);
0872 
0873 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
0874 MODULE_LICENSE("GPL");