2 * A iio driver for the light sensor ISL 29018/29023/29035.
4 * IIO driver for monitoring ambient light intensity in luxi, proximity
5 * sensing and infrared sensing.
7 * Copyright (c) 2010, NVIDIA Corporation.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/delay.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/acpi.h>
31 #define ISL29018_CONV_TIME_MS 100
33 #define ISL29018_REG_ADD_COMMAND1 0x00
34 #define ISL29018_CMD1_OPMODE_SHIFT 5
35 #define ISL29018_CMD1_OPMODE_MASK (7 << ISL29018_CMD1_OPMODE_SHIFT)
36 #define ISL29018_CMD1_OPMODE_POWER_DOWN 0
37 #define ISL29018_CMD1_OPMODE_ALS_ONCE 1
38 #define ISL29018_CMD1_OPMODE_IR_ONCE 2
39 #define ISL29018_CMD1_OPMODE_PROX_ONCE 3
41 #define ISL29018_REG_ADD_COMMAND2 0x01
42 #define ISL29018_CMD2_RESOLUTION_SHIFT 2
43 #define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
45 #define ISL29018_CMD2_RANGE_SHIFT 0
46 #define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
48 #define ISL29018_CMD2_SCHEME_SHIFT 7
49 #define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
51 #define ISL29018_REG_ADD_DATA_LSB 0x02
52 #define ISL29018_REG_ADD_DATA_MSB 0x03
54 #define ISL29018_REG_TEST 0x08
55 #define ISL29018_TEST_SHIFT 0
56 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
58 #define ISL29035_REG_DEVICE_ID 0x0F
59 #define ISL29035_DEVICE_ID_SHIFT 0x03
60 #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
61 #define ISL29035_DEVICE_ID 0x5
62 #define ISL29035_BOUT_SHIFT 0x07
63 #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
65 enum isl29018_int_time {
72 static const unsigned int isl29018_int_utimes[3][4] = {
73 {90000, 5630, 351, 21},
74 {90000, 5600, 352, 22},
75 {105000, 6500, 410, 25},
78 static const struct isl29018_scale {
81 } isl29018_scales[4][4] = {
82 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
83 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
84 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
85 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
88 struct isl29018_chip {
89 struct regmap *regmap;
92 unsigned int calibscale;
93 unsigned int ucalibscale;
94 unsigned int int_time;
95 struct isl29018_scale scale;
100 static int isl29018_set_integration_time(struct isl29018_chip *chip,
105 unsigned int int_time, new_int_time;
107 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
108 if (utime == isl29018_int_utimes[chip->type][i]) {
114 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
117 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
118 ISL29018_CMD2_RESOLUTION_MASK,
119 i << ISL29018_CMD2_RESOLUTION_SHIFT);
123 /* Keep the same range when integration time changes */
124 int_time = chip->int_time;
125 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
126 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
127 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
128 chip->scale = isl29018_scales[new_int_time][i];
132 chip->int_time = new_int_time;
137 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
141 struct isl29018_scale new_scale;
143 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
144 if (scale == isl29018_scales[chip->int_time][i].scale &&
145 uscale == isl29018_scales[chip->int_time][i].uscale) {
146 new_scale = isl29018_scales[chip->int_time][i];
151 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
154 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
155 ISL29018_CMD2_RANGE_MASK,
156 i << ISL29018_CMD2_RANGE_SHIFT);
160 chip->scale = new_scale;
165 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
170 struct device *dev = regmap_get_device(chip->regmap);
173 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
174 mode << ISL29018_CMD1_OPMODE_SHIFT);
177 "Error in setting operating mode err %d\n", status);
180 msleep(ISL29018_CONV_TIME_MS);
181 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
184 "Error in reading LSB DATA with err %d\n", status);
188 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
191 "Error in reading MSB DATA with error %d\n", status);
194 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
196 return (msb << 8) | lsb;
199 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
202 unsigned int data_x_range;
204 lux_data = isl29018_read_sensor_input(chip,
205 ISL29018_CMD1_OPMODE_ALS_ONCE);
209 data_x_range = lux_data * chip->scale.scale +
210 lux_data * chip->scale.uscale / 1000000;
211 *lux = data_x_range * chip->calibscale +
212 data_x_range * chip->ucalibscale / 1000000;
217 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
221 ir_data = isl29018_read_sensor_input(chip,
222 ISL29018_CMD1_OPMODE_IR_ONCE);
231 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
237 struct device *dev = regmap_get_device(chip->regmap);
239 /* Do proximity sensing with required scheme */
240 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
241 ISL29018_CMD2_SCHEME_MASK,
242 scheme << ISL29018_CMD2_SCHEME_SHIFT);
244 dev_err(dev, "Error in setting operating mode\n");
248 prox_data = isl29018_read_sensor_input(chip,
249 ISL29018_CMD1_OPMODE_PROX_ONCE);
254 *near_ir = prox_data;
258 ir_data = isl29018_read_sensor_input(chip,
259 ISL29018_CMD1_OPMODE_IR_ONCE);
263 if (prox_data >= ir_data)
264 *near_ir = prox_data - ir_data;
271 static ssize_t in_illuminance_scale_available_show
272 (struct device *dev, struct device_attribute *attr,
275 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
276 struct isl29018_chip *chip = iio_priv(indio_dev);
280 mutex_lock(&chip->lock);
281 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
282 len += sprintf(buf + len, "%d.%06d ",
283 isl29018_scales[chip->int_time][i].scale,
284 isl29018_scales[chip->int_time][i].uscale);
285 mutex_unlock(&chip->lock);
292 static ssize_t in_illuminance_integration_time_available_show
293 (struct device *dev, struct device_attribute *attr,
296 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
297 struct isl29018_chip *chip = iio_priv(indio_dev);
301 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
302 len += sprintf(buf + len, "0.%06d ",
303 isl29018_int_utimes[chip->type][i]);
311 * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
312 * infrared suppression:
314 * Proximity Sensing Scheme: Bit 7. This bit programs the function
315 * of the proximity detection. Logic 0 of this bit, Scheme 0, makes
316 * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range
317 * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit,
318 * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary)
319 * proximity_less_ambient detection. The range of Scheme 1
320 * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended
321 * for resolutions less than 16. While Scheme 0 has wider dynamic
322 * range, Scheme 1 proximity detection is less affected by the
323 * ambient IR noise variation.
325 * 0 Sensing IR from LED and ambient
326 * 1 Sensing IR from LED with ambient IR rejection
328 static ssize_t proximity_on_chip_ambient_infrared_suppression_show
329 (struct device *dev, struct device_attribute *attr,
332 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
333 struct isl29018_chip *chip = iio_priv(indio_dev);
336 * Return the "proximity scheme" i.e. if the chip does on chip
337 * infrared suppression (1 means perform on chip suppression)
339 return sprintf(buf, "%d\n", chip->prox_scheme);
342 static ssize_t proximity_on_chip_ambient_infrared_suppression_store
343 (struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
346 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
347 struct isl29018_chip *chip = iio_priv(indio_dev);
350 if (kstrtoint(buf, 10, &val))
352 if (!(val == 0 || val == 1))
356 * Get the "proximity scheme" i.e. if the chip does on chip
357 * infrared suppression (1 means perform on chip suppression)
359 mutex_lock(&chip->lock);
360 chip->prox_scheme = val;
361 mutex_unlock(&chip->lock);
366 static int isl29018_write_raw(struct iio_dev *indio_dev,
367 struct iio_chan_spec const *chan,
372 struct isl29018_chip *chip = iio_priv(indio_dev);
375 mutex_lock(&chip->lock);
376 if (chip->suspended) {
381 case IIO_CHAN_INFO_CALIBSCALE:
382 if (chan->type == IIO_LIGHT) {
383 chip->calibscale = val;
384 chip->ucalibscale = val2;
388 case IIO_CHAN_INFO_INT_TIME:
389 if (chan->type == IIO_LIGHT && !val)
390 ret = isl29018_set_integration_time(chip, val2);
392 case IIO_CHAN_INFO_SCALE:
393 if (chan->type == IIO_LIGHT)
394 ret = isl29018_set_scale(chip, val, val2);
401 mutex_unlock(&chip->lock);
406 static int isl29018_read_raw(struct iio_dev *indio_dev,
407 struct iio_chan_spec const *chan,
413 struct isl29018_chip *chip = iio_priv(indio_dev);
415 mutex_lock(&chip->lock);
416 if (chip->suspended) {
421 case IIO_CHAN_INFO_RAW:
422 case IIO_CHAN_INFO_PROCESSED:
423 switch (chan->type) {
425 ret = isl29018_read_lux(chip, val);
428 ret = isl29018_read_ir(chip, val);
431 ret = isl29018_read_proximity_ir(chip,
441 case IIO_CHAN_INFO_INT_TIME:
442 if (chan->type == IIO_LIGHT) {
444 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
445 ret = IIO_VAL_INT_PLUS_MICRO;
448 case IIO_CHAN_INFO_SCALE:
449 if (chan->type == IIO_LIGHT) {
450 *val = chip->scale.scale;
451 *val2 = chip->scale.uscale;
452 ret = IIO_VAL_INT_PLUS_MICRO;
455 case IIO_CHAN_INFO_CALIBSCALE:
456 if (chan->type == IIO_LIGHT) {
457 *val = chip->calibscale;
458 *val2 = chip->ucalibscale;
459 ret = IIO_VAL_INT_PLUS_MICRO;
467 mutex_unlock(&chip->lock);
472 #define ISL29018_LIGHT_CHANNEL { \
476 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
477 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
478 BIT(IIO_CHAN_INFO_SCALE) | \
479 BIT(IIO_CHAN_INFO_INT_TIME), \
482 #define ISL29018_IR_CHANNEL { \
483 .type = IIO_INTENSITY, \
485 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
486 .channel2 = IIO_MOD_LIGHT_IR, \
489 #define ISL29018_PROXIMITY_CHANNEL { \
490 .type = IIO_PROXIMITY, \
491 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
494 static const struct iio_chan_spec isl29018_channels[] = {
495 ISL29018_LIGHT_CHANNEL,
497 ISL29018_PROXIMITY_CHANNEL,
500 static const struct iio_chan_spec isl29023_channels[] = {
501 ISL29018_LIGHT_CHANNEL,
505 static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
506 static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
507 static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
509 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
511 static struct attribute *isl29018_attributes[] = {
512 ISL29018_DEV_ATTR(in_illuminance_scale_available),
513 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
514 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
518 static struct attribute *isl29023_attributes[] = {
519 ISL29018_DEV_ATTR(in_illuminance_scale_available),
520 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
524 static const struct attribute_group isl29018_group = {
525 .attrs = isl29018_attributes,
528 static const struct attribute_group isl29023_group = {
529 .attrs = isl29023_attributes,
538 static int isl29018_chip_init(struct isl29018_chip *chip)
541 struct device *dev = regmap_get_device(chip->regmap);
543 if (chip->type == isl29035) {
546 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
549 "Error reading ID register with error %d\n",
554 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
556 if (id != ISL29035_DEVICE_ID)
559 /* Clear brownout bit */
560 status = regmap_update_bits(chip->regmap,
561 ISL29035_REG_DEVICE_ID,
562 ISL29035_BOUT_MASK, 0);
568 * Code added per Intersil Application Note 1534:
569 * When VDD sinks to approximately 1.8V or below, some of
570 * the part's registers may change their state. When VDD
571 * recovers to 2.25V (or greater), the part may thus be in an
572 * unknown mode of operation. The user can return the part to
573 * a known mode of operation either by (a) setting VDD = 0V for
574 * 1 second or more and then powering back up with a slew rate
575 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
576 * conversions, clear the test registers, and then rewrite all
577 * registers to the desired values.
579 * For ISL29011, ISL29018, ISL29021, ISL29023
580 * 1. Write 0x00 to register 0x08 (TEST)
581 * 2. Write 0x00 to register 0x00 (CMD1)
582 * 3. Rewrite all registers to the desired values
584 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
585 * the same thing EXCEPT the data sheet asks for a 1ms delay after
586 * writing the CMD1 register.
588 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
590 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
596 * See Intersil AN1534 comments above.
597 * "Operating Mode" (COMMAND1) register is reprogrammed when
598 * data is read from the device.
600 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
602 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
607 usleep_range(1000, 2000); /* per data sheet, page 10 */
610 status = isl29018_set_scale(chip, chip->scale.scale,
613 dev_err(dev, "Init of isl29018 fails\n");
617 status = isl29018_set_integration_time(chip,
618 isl29018_int_utimes[chip->type][chip->int_time]);
620 dev_err(dev, "Init of isl29018 fails\n");
625 static const struct iio_info isl29018_info = {
626 .attrs = &isl29018_group,
627 .driver_module = THIS_MODULE,
628 .read_raw = isl29018_read_raw,
629 .write_raw = isl29018_write_raw,
632 static const struct iio_info isl29023_info = {
633 .attrs = &isl29023_group,
634 .driver_module = THIS_MODULE,
635 .read_raw = isl29018_read_raw,
636 .write_raw = isl29018_write_raw,
639 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
642 case ISL29018_REG_ADD_DATA_LSB:
643 case ISL29018_REG_ADD_DATA_MSB:
644 case ISL29018_REG_ADD_COMMAND1:
645 case ISL29018_REG_TEST:
646 case ISL29035_REG_DEVICE_ID:
653 static const struct regmap_config isl29018_regmap_config = {
656 .volatile_reg = isl29018_is_volatile_reg,
657 .max_register = ISL29018_REG_TEST,
658 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
659 .cache_type = REGCACHE_RBTREE,
662 static const struct regmap_config isl29035_regmap_config = {
665 .volatile_reg = isl29018_is_volatile_reg,
666 .max_register = ISL29035_REG_DEVICE_ID,
667 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
668 .cache_type = REGCACHE_RBTREE,
671 struct isl29018_chip_info {
672 const struct iio_chan_spec *channels;
674 const struct iio_info *indio_info;
675 const struct regmap_config *regmap_cfg;
678 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
680 .channels = isl29018_channels,
681 .num_channels = ARRAY_SIZE(isl29018_channels),
682 .indio_info = &isl29018_info,
683 .regmap_cfg = &isl29018_regmap_config,
686 .channels = isl29023_channels,
687 .num_channels = ARRAY_SIZE(isl29023_channels),
688 .indio_info = &isl29023_info,
689 .regmap_cfg = &isl29018_regmap_config,
692 .channels = isl29023_channels,
693 .num_channels = ARRAY_SIZE(isl29023_channels),
694 .indio_info = &isl29023_info,
695 .regmap_cfg = &isl29035_regmap_config,
699 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
701 const struct acpi_device_id *id;
703 id = acpi_match_device(dev->driver->acpi_match_table, dev);
708 *data = (int)id->driver_data;
710 return dev_name(dev);
713 static int isl29018_probe(struct i2c_client *client,
714 const struct i2c_device_id *id)
716 struct isl29018_chip *chip;
717 struct iio_dev *indio_dev;
719 const char *name = NULL;
722 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
726 chip = iio_priv(indio_dev);
728 i2c_set_clientdata(client, indio_dev);
732 dev_id = id->driver_data;
735 if (ACPI_HANDLE(&client->dev))
736 name = isl29018_match_acpi_device(&client->dev, &dev_id);
738 mutex_init(&chip->lock);
741 chip->calibscale = 1;
742 chip->ucalibscale = 0;
743 chip->int_time = ISL29018_INT_TIME_16;
744 chip->scale = isl29018_scales[chip->int_time][0];
745 chip->suspended = false;
747 chip->regmap = devm_regmap_init_i2c(client,
748 isl29018_chip_info_tbl[dev_id].regmap_cfg);
749 if (IS_ERR(chip->regmap)) {
750 err = PTR_ERR(chip->regmap);
751 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
755 err = isl29018_chip_init(chip);
759 indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
760 indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
761 indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
762 indio_dev->name = name;
763 indio_dev->dev.parent = &client->dev;
764 indio_dev->modes = INDIO_DIRECT_MODE;
766 return devm_iio_device_register(&client->dev, indio_dev);
769 #ifdef CONFIG_PM_SLEEP
770 static int isl29018_suspend(struct device *dev)
772 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
774 mutex_lock(&chip->lock);
777 * Since this driver uses only polling commands, we are by default in
778 * auto shutdown (ie, power-down) mode.
779 * So we do not have much to do here.
781 chip->suspended = true;
783 mutex_unlock(&chip->lock);
788 static int isl29018_resume(struct device *dev)
790 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
793 mutex_lock(&chip->lock);
795 err = isl29018_chip_init(chip);
797 chip->suspended = false;
799 mutex_unlock(&chip->lock);
804 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
805 #define ISL29018_PM_OPS (&isl29018_pm_ops)
807 #define ISL29018_PM_OPS NULL
811 static const struct acpi_device_id isl29018_acpi_match[] = {
812 {"ISL29018", isl29018},
813 {"ISL29023", isl29023},
814 {"ISL29035", isl29035},
817 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
820 static const struct i2c_device_id isl29018_id[] = {
821 {"isl29018", isl29018},
822 {"isl29023", isl29023},
823 {"isl29035", isl29035},
826 MODULE_DEVICE_TABLE(i2c, isl29018_id);
828 static const struct of_device_id isl29018_of_match[] = {
829 { .compatible = "isil,isl29018", },
830 { .compatible = "isil,isl29023", },
831 { .compatible = "isil,isl29035", },
834 MODULE_DEVICE_TABLE(of, isl29018_of_match);
836 static struct i2c_driver isl29018_driver = {
839 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
840 .pm = ISL29018_PM_OPS,
841 .of_match_table = isl29018_of_match,
843 .probe = isl29018_probe,
844 .id_table = isl29018_id,
846 module_i2c_driver(isl29018_driver);
848 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
849 MODULE_LICENSE("GPL");