0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/module.h>
0015 #include <linux/i2c.h>
0016 #include <linux/err.h>
0017 #include <linux/mutex.h>
0018 #include <linux/delay.h>
0019 #include <linux/slab.h>
0020 #include <linux/regmap.h>
0021 #include <linux/iio/iio.h>
0022 #include <linux/iio/sysfs.h>
0023 #include <linux/pm_runtime.h>
0024
0025 #define ISL29028_CONV_TIME_MS 100
0026
0027 #define ISL29028_REG_CONFIGURE 0x01
0028
0029 #define ISL29028_CONF_ALS_IR_MODE_ALS 0
0030 #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
0031 #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
0032
0033 #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
0034 #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
0035 #define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
0036
0037 #define ISL29028_CONF_ALS_DIS 0
0038 #define ISL29028_CONF_ALS_EN BIT(2)
0039 #define ISL29028_CONF_ALS_EN_MASK BIT(2)
0040
0041 #define ISL29028_CONF_PROX_SLP_SH 4
0042 #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
0043
0044 #define ISL29028_CONF_PROX_EN BIT(7)
0045 #define ISL29028_CONF_PROX_EN_MASK BIT(7)
0046
0047 #define ISL29028_REG_INTERRUPT 0x02
0048
0049 #define ISL29028_REG_PROX_DATA 0x08
0050 #define ISL29028_REG_ALSIR_L 0x09
0051 #define ISL29028_REG_ALSIR_U 0x0A
0052
0053 #define ISL29028_REG_TEST1_MODE 0x0E
0054 #define ISL29028_REG_TEST2_MODE 0x0F
0055
0056 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
0057
0058 #define ISL29028_POWER_OFF_DELAY_MS 2000
0059
0060 struct isl29028_prox_data {
0061 int sampling_int;
0062 int sampling_fract;
0063 int sleep_time;
0064 };
0065
0066 static const struct isl29028_prox_data isl29028_prox_data[] = {
0067 { 1, 250000, 800 },
0068 { 2, 500000, 400 },
0069 { 5, 0, 200 },
0070 { 10, 0, 100 },
0071 { 13, 300000, 75 },
0072 { 20, 0, 50 },
0073 { 80, 0, 13 },
0074
0075
0076
0077 { 100, 0, 0 }
0078 };
0079
0080 enum isl29028_als_ir_mode {
0081 ISL29028_MODE_NONE = 0,
0082 ISL29028_MODE_ALS,
0083 ISL29028_MODE_IR,
0084 };
0085
0086 struct isl29028_chip {
0087 struct mutex lock;
0088 struct regmap *regmap;
0089 int prox_sampling_int;
0090 int prox_sampling_frac;
0091 bool enable_prox;
0092 int lux_scale;
0093 enum isl29028_als_ir_mode als_ir_mode;
0094 };
0095
0096 static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
0097 {
0098 int i;
0099
0100 for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
0101 if (isl29028_prox_data[i].sampling_int == sampling_int &&
0102 isl29028_prox_data[i].sampling_fract == sampling_fract)
0103 return i;
0104 }
0105
0106 return -EINVAL;
0107 }
0108
0109 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
0110 int sampling_int, int sampling_fract)
0111 {
0112 struct device *dev = regmap_get_device(chip->regmap);
0113 int sleep_index, ret;
0114
0115 sleep_index = isl29028_find_prox_sleep_index(sampling_int,
0116 sampling_fract);
0117 if (sleep_index < 0)
0118 return sleep_index;
0119
0120 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0121 ISL29028_CONF_PROX_SLP_MASK,
0122 sleep_index << ISL29028_CONF_PROX_SLP_SH);
0123
0124 if (ret < 0) {
0125 dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
0126 __func__, ret);
0127 return ret;
0128 }
0129
0130 chip->prox_sampling_int = sampling_int;
0131 chip->prox_sampling_frac = sampling_fract;
0132
0133 return ret;
0134 }
0135
0136 static int isl29028_enable_proximity(struct isl29028_chip *chip)
0137 {
0138 int prox_index, ret;
0139
0140 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
0141 chip->prox_sampling_frac);
0142 if (ret < 0)
0143 return ret;
0144
0145 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0146 ISL29028_CONF_PROX_EN_MASK,
0147 ISL29028_CONF_PROX_EN);
0148 if (ret < 0)
0149 return ret;
0150
0151
0152 prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
0153 chip->prox_sampling_frac);
0154 if (prox_index < 0)
0155 return prox_index;
0156
0157 msleep(isl29028_prox_data[prox_index].sleep_time);
0158
0159 return 0;
0160 }
0161
0162 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
0163 {
0164 struct device *dev = regmap_get_device(chip->regmap);
0165 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
0166 ISL29028_CONF_ALS_RANGE_LOW_LUX;
0167 int ret;
0168
0169 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0170 ISL29028_CONF_ALS_RANGE_MASK, val);
0171 if (ret < 0) {
0172 dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
0173 ret);
0174 return ret;
0175 }
0176
0177 chip->lux_scale = lux_scale;
0178
0179 return ret;
0180 }
0181
0182 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
0183 enum isl29028_als_ir_mode mode)
0184 {
0185 int ret;
0186
0187 if (chip->als_ir_mode == mode)
0188 return 0;
0189
0190 ret = isl29028_set_als_scale(chip, chip->lux_scale);
0191 if (ret < 0)
0192 return ret;
0193
0194 switch (mode) {
0195 case ISL29028_MODE_ALS:
0196 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0197 ISL29028_CONF_ALS_IR_MODE_MASK,
0198 ISL29028_CONF_ALS_IR_MODE_ALS);
0199 if (ret < 0)
0200 return ret;
0201
0202 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0203 ISL29028_CONF_ALS_RANGE_MASK,
0204 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
0205 break;
0206 case ISL29028_MODE_IR:
0207 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0208 ISL29028_CONF_ALS_IR_MODE_MASK,
0209 ISL29028_CONF_ALS_IR_MODE_IR);
0210 break;
0211 case ISL29028_MODE_NONE:
0212 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0213 ISL29028_CONF_ALS_EN_MASK,
0214 ISL29028_CONF_ALS_DIS);
0215 }
0216
0217 if (ret < 0)
0218 return ret;
0219
0220
0221 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
0222 ISL29028_CONF_ALS_EN_MASK,
0223 ISL29028_CONF_ALS_EN);
0224 if (ret < 0)
0225 return ret;
0226
0227
0228 msleep(ISL29028_CONV_TIME_MS);
0229
0230 chip->als_ir_mode = mode;
0231
0232 return 0;
0233 }
0234
0235 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
0236 {
0237 struct device *dev = regmap_get_device(chip->regmap);
0238 unsigned int lsb;
0239 unsigned int msb;
0240 int ret;
0241
0242 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
0243 if (ret < 0) {
0244 dev_err(dev,
0245 "%s(): Error %d reading register ALSIR_L\n",
0246 __func__, ret);
0247 return ret;
0248 }
0249
0250 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
0251 if (ret < 0) {
0252 dev_err(dev,
0253 "%s(): Error %d reading register ALSIR_U\n",
0254 __func__, ret);
0255 return ret;
0256 }
0257
0258 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
0259
0260 return 0;
0261 }
0262
0263 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
0264 {
0265 struct device *dev = regmap_get_device(chip->regmap);
0266 unsigned int data;
0267 int ret;
0268
0269 if (!chip->enable_prox) {
0270 ret = isl29028_enable_proximity(chip);
0271 if (ret < 0)
0272 return ret;
0273
0274 chip->enable_prox = true;
0275 }
0276
0277 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
0278 if (ret < 0) {
0279 dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
0280 __func__, ret);
0281 return ret;
0282 }
0283
0284 *prox = data;
0285
0286 return 0;
0287 }
0288
0289 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
0290 {
0291 struct device *dev = regmap_get_device(chip->regmap);
0292 int ret;
0293 int als_ir_data;
0294
0295 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
0296 if (ret < 0) {
0297 dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
0298 ret);
0299 return ret;
0300 }
0301
0302 ret = isl29028_read_als_ir(chip, &als_ir_data);
0303 if (ret < 0)
0304 return ret;
0305
0306
0307
0308
0309
0310
0311 if (chip->lux_scale == 125)
0312 als_ir_data = (als_ir_data * 31) / 1000;
0313 else
0314 als_ir_data = (als_ir_data * 49) / 100;
0315
0316 *als_data = als_ir_data;
0317
0318 return 0;
0319 }
0320
0321 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
0322 {
0323 struct device *dev = regmap_get_device(chip->regmap);
0324 int ret;
0325
0326 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
0327 if (ret < 0) {
0328 dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
0329 ret);
0330 return ret;
0331 }
0332
0333 return isl29028_read_als_ir(chip, ir_data);
0334 }
0335
0336 static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
0337 {
0338 struct device *dev = regmap_get_device(chip->regmap);
0339 int ret;
0340
0341 if (on) {
0342 ret = pm_runtime_resume_and_get(dev);
0343 } else {
0344 pm_runtime_mark_last_busy(dev);
0345 ret = pm_runtime_put_autosuspend(dev);
0346 }
0347
0348 return ret;
0349 }
0350
0351
0352 static int isl29028_write_raw(struct iio_dev *indio_dev,
0353 struct iio_chan_spec const *chan,
0354 int val, int val2, long mask)
0355 {
0356 struct isl29028_chip *chip = iio_priv(indio_dev);
0357 struct device *dev = regmap_get_device(chip->regmap);
0358 int ret;
0359
0360 ret = isl29028_set_pm_runtime_busy(chip, true);
0361 if (ret < 0)
0362 return ret;
0363
0364 mutex_lock(&chip->lock);
0365
0366 ret = -EINVAL;
0367 switch (chan->type) {
0368 case IIO_PROXIMITY:
0369 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
0370 dev_err(dev,
0371 "%s(): proximity: Mask value 0x%08lx is not supported\n",
0372 __func__, mask);
0373 break;
0374 }
0375
0376 if (val < 1 || val > 100) {
0377 dev_err(dev,
0378 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
0379 __func__, val);
0380 break;
0381 }
0382
0383 ret = isl29028_set_proxim_sampling(chip, val, val2);
0384 break;
0385 case IIO_LIGHT:
0386 if (mask != IIO_CHAN_INFO_SCALE) {
0387 dev_err(dev,
0388 "%s(): light: Mask value 0x%08lx is not supported\n",
0389 __func__, mask);
0390 break;
0391 }
0392
0393 if (val != 125 && val != 2000) {
0394 dev_err(dev,
0395 "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
0396 __func__, val);
0397 break;
0398 }
0399
0400 ret = isl29028_set_als_scale(chip, val);
0401 break;
0402 default:
0403 dev_err(dev, "%s(): Unsupported channel type %x\n",
0404 __func__, chan->type);
0405 break;
0406 }
0407
0408 mutex_unlock(&chip->lock);
0409
0410 if (ret < 0)
0411 return ret;
0412
0413 ret = isl29028_set_pm_runtime_busy(chip, false);
0414 if (ret < 0)
0415 return ret;
0416
0417 return ret;
0418 }
0419
0420 static int isl29028_read_raw(struct iio_dev *indio_dev,
0421 struct iio_chan_spec const *chan,
0422 int *val, int *val2, long mask)
0423 {
0424 struct isl29028_chip *chip = iio_priv(indio_dev);
0425 struct device *dev = regmap_get_device(chip->regmap);
0426 int ret, pm_ret;
0427
0428 ret = isl29028_set_pm_runtime_busy(chip, true);
0429 if (ret < 0)
0430 return ret;
0431
0432 mutex_lock(&chip->lock);
0433
0434 ret = -EINVAL;
0435 switch (mask) {
0436 case IIO_CHAN_INFO_RAW:
0437 case IIO_CHAN_INFO_PROCESSED:
0438 switch (chan->type) {
0439 case IIO_LIGHT:
0440 ret = isl29028_als_get(chip, val);
0441 break;
0442 case IIO_INTENSITY:
0443 ret = isl29028_ir_get(chip, val);
0444 break;
0445 case IIO_PROXIMITY:
0446 ret = isl29028_read_proxim(chip, val);
0447 break;
0448 default:
0449 break;
0450 }
0451
0452 if (ret < 0)
0453 break;
0454
0455 ret = IIO_VAL_INT;
0456 break;
0457 case IIO_CHAN_INFO_SAMP_FREQ:
0458 if (chan->type != IIO_PROXIMITY)
0459 break;
0460
0461 *val = chip->prox_sampling_int;
0462 *val2 = chip->prox_sampling_frac;
0463 ret = IIO_VAL_INT;
0464 break;
0465 case IIO_CHAN_INFO_SCALE:
0466 if (chan->type != IIO_LIGHT)
0467 break;
0468 *val = chip->lux_scale;
0469 ret = IIO_VAL_INT;
0470 break;
0471 default:
0472 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
0473 __func__, mask);
0474 break;
0475 }
0476
0477 mutex_unlock(&chip->lock);
0478
0479 if (ret < 0)
0480 return ret;
0481
0482
0483
0484
0485
0486
0487 pm_ret = isl29028_set_pm_runtime_busy(chip, false);
0488 if (pm_ret < 0)
0489 return pm_ret;
0490
0491 return ret;
0492 }
0493
0494 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
0495 "1.25 2.5 5 10 13.3 20 80 100");
0496 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
0497
0498 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
0499 static struct attribute *isl29028_attributes[] = {
0500 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
0501 ISL29028_CONST_ATTR(in_illuminance_scale_available),
0502 NULL,
0503 };
0504
0505 static const struct attribute_group isl29108_group = {
0506 .attrs = isl29028_attributes,
0507 };
0508
0509 static const struct iio_chan_spec isl29028_channels[] = {
0510 {
0511 .type = IIO_LIGHT,
0512 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
0513 BIT(IIO_CHAN_INFO_SCALE),
0514 }, {
0515 .type = IIO_INTENSITY,
0516 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0517 }, {
0518 .type = IIO_PROXIMITY,
0519 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0520 BIT(IIO_CHAN_INFO_SAMP_FREQ),
0521 }
0522 };
0523
0524 static const struct iio_info isl29028_info = {
0525 .attrs = &isl29108_group,
0526 .read_raw = isl29028_read_raw,
0527 .write_raw = isl29028_write_raw,
0528 };
0529
0530 static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
0531 {
0532 struct device *dev = regmap_get_device(chip->regmap);
0533 int ret;
0534
0535 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
0536 if (ret < 0)
0537 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
0538 __func__, ret);
0539
0540 chip->als_ir_mode = ISL29028_MODE_NONE;
0541 chip->enable_prox = false;
0542
0543 return ret;
0544 }
0545
0546 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
0547 {
0548 switch (reg) {
0549 case ISL29028_REG_INTERRUPT:
0550 case ISL29028_REG_PROX_DATA:
0551 case ISL29028_REG_ALSIR_L:
0552 case ISL29028_REG_ALSIR_U:
0553 return true;
0554 default:
0555 return false;
0556 }
0557 }
0558
0559 static const struct regmap_config isl29028_regmap_config = {
0560 .reg_bits = 8,
0561 .val_bits = 8,
0562 .volatile_reg = isl29028_is_volatile_reg,
0563 .max_register = ISL29028_NUM_REGS - 1,
0564 .num_reg_defaults_raw = ISL29028_NUM_REGS,
0565 .cache_type = REGCACHE_RBTREE,
0566 };
0567
0568 static int isl29028_probe(struct i2c_client *client,
0569 const struct i2c_device_id *id)
0570 {
0571 struct isl29028_chip *chip;
0572 struct iio_dev *indio_dev;
0573 int ret;
0574
0575 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
0576 if (!indio_dev)
0577 return -ENOMEM;
0578
0579 chip = iio_priv(indio_dev);
0580
0581 i2c_set_clientdata(client, indio_dev);
0582 mutex_init(&chip->lock);
0583
0584 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
0585 if (IS_ERR(chip->regmap)) {
0586 ret = PTR_ERR(chip->regmap);
0587 dev_err(&client->dev, "%s: Error %d initializing regmap\n",
0588 __func__, ret);
0589 return ret;
0590 }
0591
0592 chip->enable_prox = false;
0593 chip->prox_sampling_int = 20;
0594 chip->prox_sampling_frac = 0;
0595 chip->lux_scale = 2000;
0596
0597 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
0598 if (ret < 0) {
0599 dev_err(&client->dev,
0600 "%s(): Error %d writing to TEST1_MODE register\n",
0601 __func__, ret);
0602 return ret;
0603 }
0604
0605 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
0606 if (ret < 0) {
0607 dev_err(&client->dev,
0608 "%s(): Error %d writing to TEST2_MODE register\n",
0609 __func__, ret);
0610 return ret;
0611 }
0612
0613 ret = isl29028_clear_configure_reg(chip);
0614 if (ret < 0)
0615 return ret;
0616
0617 indio_dev->info = &isl29028_info;
0618 indio_dev->channels = isl29028_channels;
0619 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
0620 indio_dev->name = id->name;
0621 indio_dev->modes = INDIO_DIRECT_MODE;
0622
0623 pm_runtime_enable(&client->dev);
0624 pm_runtime_set_autosuspend_delay(&client->dev,
0625 ISL29028_POWER_OFF_DELAY_MS);
0626 pm_runtime_use_autosuspend(&client->dev);
0627
0628 ret = iio_device_register(indio_dev);
0629 if (ret < 0) {
0630 dev_err(&client->dev,
0631 "%s(): iio registration failed with error %d\n",
0632 __func__, ret);
0633 return ret;
0634 }
0635
0636 return 0;
0637 }
0638
0639 static int isl29028_remove(struct i2c_client *client)
0640 {
0641 struct iio_dev *indio_dev = i2c_get_clientdata(client);
0642 struct isl29028_chip *chip = iio_priv(indio_dev);
0643
0644 iio_device_unregister(indio_dev);
0645
0646 pm_runtime_disable(&client->dev);
0647 pm_runtime_set_suspended(&client->dev);
0648
0649 isl29028_clear_configure_reg(chip);
0650
0651 return 0;
0652 }
0653
0654 static int isl29028_suspend(struct device *dev)
0655 {
0656 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
0657 struct isl29028_chip *chip = iio_priv(indio_dev);
0658 int ret;
0659
0660 mutex_lock(&chip->lock);
0661
0662 ret = isl29028_clear_configure_reg(chip);
0663
0664 mutex_unlock(&chip->lock);
0665
0666 return ret;
0667 }
0668
0669 static int isl29028_resume(struct device *dev)
0670 {
0671
0672
0673
0674
0675
0676 return 0;
0677 }
0678
0679 static DEFINE_RUNTIME_DEV_PM_OPS(isl29028_pm_ops, isl29028_suspend,
0680 isl29028_resume, NULL);
0681
0682 static const struct i2c_device_id isl29028_id[] = {
0683 {"isl29028", 0},
0684 {"isl29030", 0},
0685 {}
0686 };
0687 MODULE_DEVICE_TABLE(i2c, isl29028_id);
0688
0689 static const struct of_device_id isl29028_of_match[] = {
0690 { .compatible = "isl,isl29028", },
0691 { .compatible = "isil,isl29028", },
0692 { .compatible = "isil,isl29030", },
0693 { },
0694 };
0695 MODULE_DEVICE_TABLE(of, isl29028_of_match);
0696
0697 static struct i2c_driver isl29028_driver = {
0698 .driver = {
0699 .name = "isl29028",
0700 .pm = pm_ptr(&isl29028_pm_ops),
0701 .of_match_table = isl29028_of_match,
0702 },
0703 .probe = isl29028_probe,
0704 .remove = isl29028_remove,
0705 .id_table = isl29028_id,
0706 };
0707
0708 module_i2c_driver(isl29028_driver);
0709
0710 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
0711 MODULE_LICENSE("GPL v2");
0712 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");