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
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/acpi.h>
35 #define CONVERSION_TIME_MS 100
37 #define ISL29018_REG_ADD_COMMAND1 0x00
38 #define COMMMAND1_OPMODE_SHIFT 5
39 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
40 #define COMMMAND1_OPMODE_POWER_DOWN 0
41 #define COMMMAND1_OPMODE_ALS_ONCE 1
42 #define COMMMAND1_OPMODE_IR_ONCE 2
43 #define COMMMAND1_OPMODE_PROX_ONCE 3
45 #define ISL29018_REG_ADD_COMMANDII 0x01
46 #define COMMANDII_RESOLUTION_SHIFT 2
47 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
49 #define COMMANDII_RANGE_SHIFT 0
50 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
52 #define COMMANDII_SCHEME_SHIFT 7
53 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
55 #define ISL29018_REG_ADD_DATA_LSB 0x02
56 #define ISL29018_REG_ADD_DATA_MSB 0x03
58 #define ISL29018_REG_TEST 0x08
59 #define ISL29018_TEST_SHIFT 0
60 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
62 #define ISL29035_REG_DEVICE_ID 0x0F
63 #define ISL29035_DEVICE_ID_SHIFT 0x03
64 #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
65 #define ISL29035_DEVICE_ID 0x5
66 #define ISL29035_BOUT_SHIFT 0x07
67 #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
69 #define ISL29018_INT_TIME_AVAIL "0.090000 0.005630 0.000351 0.000021"
70 #define ISL29023_INT_TIME_AVAIL "0.090000 0.005600 0.000352 0.000022"
71 #define ISL29035_INT_TIME_AVAIL "0.105000 0.006500 0.000410 0.000025"
73 static const char * const int_time_avail[] = {
74 ISL29018_INT_TIME_AVAIL,
75 ISL29023_INT_TIME_AVAIL,
76 ISL29035_INT_TIME_AVAIL,
79 enum isl29018_int_time {
86 static const unsigned int isl29018_int_utimes[3][4] = {
87 {90000, 5630, 351, 21},
88 {90000, 5600, 352, 22},
89 {105000, 6500, 410, 25},
92 static const struct isl29018_scale {
95 } isl29018_scales[4][4] = {
96 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
97 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
98 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
99 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
102 struct isl29018_chip {
103 struct regmap *regmap;
106 unsigned int calibscale;
107 unsigned int ucalibscale;
108 unsigned int int_time;
109 struct isl29018_scale scale;
114 static int isl29018_set_integration_time(struct isl29018_chip *chip,
118 unsigned int int_time, new_int_time;
120 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
121 if (utime == isl29018_int_utimes[chip->type][i]) {
127 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
130 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
131 COMMANDII_RESOLUTION_MASK,
132 i << COMMANDII_RESOLUTION_SHIFT);
136 /* keep the same range when integration time changes */
137 int_time = chip->int_time;
138 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
139 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
140 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
141 chip->scale = isl29018_scales[new_int_time][i];
145 chip->int_time = new_int_time;
150 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
153 struct isl29018_scale new_scale;
155 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
156 if (scale == isl29018_scales[chip->int_time][i].scale &&
157 uscale == isl29018_scales[chip->int_time][i].uscale) {
158 new_scale = isl29018_scales[chip->int_time][i];
163 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
166 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
167 COMMANDII_RANGE_MASK,
168 i << COMMANDII_RANGE_SHIFT);
172 chip->scale = new_scale;
177 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
182 struct device *dev = regmap_get_device(chip->regmap);
185 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
186 mode << COMMMAND1_OPMODE_SHIFT);
189 "Error in setting operating mode err %d\n", status);
192 msleep(CONVERSION_TIME_MS);
193 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
196 "Error in reading LSB DATA with err %d\n", status);
200 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
203 "Error in reading MSB DATA with error %d\n", status);
206 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
208 return (msb << 8) | lsb;
211 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
214 unsigned int data_x_range;
216 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
221 data_x_range = lux_data * chip->scale.scale +
222 lux_data * chip->scale.uscale / 1000000;
223 *lux = data_x_range * chip->calibscale +
224 data_x_range * chip->ucalibscale / 1000000;
229 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
233 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
243 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
249 struct device *dev = regmap_get_device(chip->regmap);
251 /* Do proximity sensing with required scheme */
252 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
253 COMMANDII_SCHEME_MASK,
254 scheme << COMMANDII_SCHEME_SHIFT);
256 dev_err(dev, "Error in setting operating mode\n");
260 prox_data = isl29018_read_sensor_input(chip,
261 COMMMAND1_OPMODE_PROX_ONCE);
266 *near_ir = prox_data;
270 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
275 if (prox_data >= ir_data)
276 *near_ir = prox_data - ir_data;
283 static ssize_t show_scale_available(struct device *dev,
284 struct device_attribute *attr, char *buf)
286 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
287 struct isl29018_chip *chip = iio_priv(indio_dev);
290 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
291 len += sprintf(buf + len, "%d.%06d ",
292 isl29018_scales[chip->int_time][i].scale,
293 isl29018_scales[chip->int_time][i].uscale);
300 static ssize_t show_int_time_available(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
304 struct isl29018_chip *chip = iio_priv(indio_dev);
307 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
308 len += sprintf(buf + len, "0.%06d ",
309 isl29018_int_utimes[chip->type][i]);
316 /* proximity scheme */
317 static ssize_t show_prox_infrared_suppression(struct device *dev,
318 struct device_attribute *attr,
321 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
322 struct isl29018_chip *chip = iio_priv(indio_dev);
325 * return the "proximity scheme" i.e. if the chip does on chip
326 * infrared suppression (1 means perform on chip suppression)
328 return sprintf(buf, "%d\n", chip->prox_scheme);
331 static ssize_t store_prox_infrared_suppression(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf, size_t count)
335 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336 struct isl29018_chip *chip = iio_priv(indio_dev);
339 if (kstrtoint(buf, 10, &val))
341 if (!(val == 0 || val == 1)) {
342 dev_err(dev, "The mode is not supported\n");
347 * get the "proximity scheme" i.e. if the chip does on chip
348 * infrared suppression (1 means perform on chip suppression)
350 mutex_lock(&chip->lock);
351 chip->prox_scheme = val;
352 mutex_unlock(&chip->lock);
358 static int isl29018_write_raw(struct iio_dev *indio_dev,
359 struct iio_chan_spec const *chan,
364 struct isl29018_chip *chip = iio_priv(indio_dev);
367 mutex_lock(&chip->lock);
369 case IIO_CHAN_INFO_CALIBSCALE:
370 if (chan->type == IIO_LIGHT) {
371 chip->calibscale = val;
372 chip->ucalibscale = val2;
376 case IIO_CHAN_INFO_INT_TIME:
377 if (chan->type == IIO_LIGHT) {
379 mutex_unlock(&chip->lock);
382 ret = isl29018_set_integration_time(chip, val2);
385 case IIO_CHAN_INFO_SCALE:
386 if (chan->type == IIO_LIGHT)
387 ret = isl29018_set_scale(chip, val, val2);
392 mutex_unlock(&chip->lock);
397 static int isl29018_read_raw(struct iio_dev *indio_dev,
398 struct iio_chan_spec const *chan,
404 struct isl29018_chip *chip = iio_priv(indio_dev);
406 mutex_lock(&chip->lock);
407 if (chip->suspended) {
408 mutex_unlock(&chip->lock);
412 case IIO_CHAN_INFO_RAW:
413 case IIO_CHAN_INFO_PROCESSED:
414 switch (chan->type) {
416 ret = isl29018_read_lux(chip, val);
419 ret = isl29018_read_ir(chip, val);
422 ret = isl29018_read_proximity_ir(chip,
432 case IIO_CHAN_INFO_INT_TIME:
433 if (chan->type == IIO_LIGHT) {
435 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
436 ret = IIO_VAL_INT_PLUS_MICRO;
439 case IIO_CHAN_INFO_SCALE:
440 if (chan->type == IIO_LIGHT) {
441 *val = chip->scale.scale;
442 *val2 = chip->scale.uscale;
443 ret = IIO_VAL_INT_PLUS_MICRO;
446 case IIO_CHAN_INFO_CALIBSCALE:
447 if (chan->type == IIO_LIGHT) {
448 *val = chip->calibscale;
449 *val2 = chip->ucalibscale;
450 ret = IIO_VAL_INT_PLUS_MICRO;
456 mutex_unlock(&chip->lock);
460 #define ISL29018_LIGHT_CHANNEL { \
464 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
465 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
466 BIT(IIO_CHAN_INFO_SCALE) | \
467 BIT(IIO_CHAN_INFO_INT_TIME), \
470 #define ISL29018_IR_CHANNEL { \
471 .type = IIO_INTENSITY, \
473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
474 .channel2 = IIO_MOD_LIGHT_IR, \
477 #define ISL29018_PROXIMITY_CHANNEL { \
478 .type = IIO_PROXIMITY, \
479 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
482 static const struct iio_chan_spec isl29018_channels[] = {
483 ISL29018_LIGHT_CHANNEL,
485 ISL29018_PROXIMITY_CHANNEL,
488 static const struct iio_chan_spec isl29023_channels[] = {
489 ISL29018_LIGHT_CHANNEL,
493 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
494 show_int_time_available, NULL, 0);
495 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
496 show_scale_available, NULL, 0);
497 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
499 show_prox_infrared_suppression,
500 store_prox_infrared_suppression, 0);
502 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
504 static struct attribute *isl29018_attributes[] = {
505 ISL29018_DEV_ATTR(in_illuminance_scale_available),
506 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
507 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
511 static struct attribute *isl29023_attributes[] = {
512 ISL29018_DEV_ATTR(in_illuminance_scale_available),
513 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
517 static const struct attribute_group isl29018_group = {
518 .attrs = isl29018_attributes,
521 static const struct attribute_group isl29023_group = {
522 .attrs = isl29023_attributes,
525 static int isl29035_detect(struct isl29018_chip *chip)
529 struct device *dev = regmap_get_device(chip->regmap);
531 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
534 "Error reading ID register with error %d\n",
539 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
541 if (id != ISL29035_DEVICE_ID)
544 /* clear out brownout bit */
545 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
546 ISL29035_BOUT_MASK, 0);
555 static int isl29018_chip_init(struct isl29018_chip *chip)
558 struct device *dev = regmap_get_device(chip->regmap);
560 if (chip->type == isl29035) {
561 status = isl29035_detect(chip);
566 /* Code added per Intersil Application Note 1534:
567 * When VDD sinks to approximately 1.8V or below, some of
568 * the part's registers may change their state. When VDD
569 * recovers to 2.25V (or greater), the part may thus be in an
570 * unknown mode of operation. The user can return the part to
571 * a known mode of operation either by (a) setting VDD = 0V for
572 * 1 second or more and then powering back up with a slew rate
573 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
574 * conversions, clear the test registers, and then rewrite all
575 * registers to the desired values.
577 * FOR ISL29011, ISL29018, ISL29021, ISL29023
578 * 1. Write 0x00 to register 0x08 (TEST)
579 * 2. Write 0x00 to register 0x00 (CMD1)
580 * 3. Rewrite all registers to the desired values
582 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
583 * the same thing EXCEPT the data sheet asks for a 1ms delay after
584 * writing the CMD1 register.
586 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
588 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
593 /* See Intersil AN1534 comments above.
594 * "Operating Mode" (COMMAND1) register is reprogrammed when
595 * data is read from the device.
597 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
599 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
604 usleep_range(1000, 2000); /* per data sheet, page 10 */
607 status = isl29018_set_scale(chip, chip->scale.scale,
610 dev_err(dev, "Init of isl29018 fails\n");
614 status = isl29018_set_integration_time(chip,
615 isl29018_int_utimes[chip->type][chip->int_time]);
617 dev_err(dev, "Init of isl29018 fails\n");
624 static const struct iio_info isl29018_info = {
625 .attrs = &isl29018_group,
626 .driver_module = THIS_MODULE,
627 .read_raw = isl29018_read_raw,
628 .write_raw = isl29018_write_raw,
631 static const struct iio_info isl29023_info = {
632 .attrs = &isl29023_group,
633 .driver_module = THIS_MODULE,
634 .read_raw = isl29018_read_raw,
635 .write_raw = isl29018_write_raw,
638 static bool is_volatile_reg(struct device *dev, unsigned int reg)
641 case ISL29018_REG_ADD_DATA_LSB:
642 case ISL29018_REG_ADD_DATA_MSB:
643 case ISL29018_REG_ADD_COMMAND1:
644 case ISL29018_REG_TEST:
645 case ISL29035_REG_DEVICE_ID:
653 * isl29018_regmap_config: regmap configuration.
654 * Use RBTREE mechanism for caching.
656 static const struct regmap_config isl29018_regmap_config = {
659 .volatile_reg = is_volatile_reg,
660 .max_register = ISL29018_REG_TEST,
661 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
662 .cache_type = REGCACHE_RBTREE,
665 /* isl29035_regmap_config: regmap configuration for ISL29035 */
666 static const struct regmap_config isl29035_regmap_config = {
669 .volatile_reg = is_volatile_reg,
670 .max_register = ISL29035_REG_DEVICE_ID,
671 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
672 .cache_type = REGCACHE_RBTREE,
676 const struct iio_chan_spec *channels;
678 const struct iio_info *indio_info;
679 const struct regmap_config *regmap_cfg;
682 static const struct chip_info chip_info_tbl[] = {
684 .channels = isl29018_channels,
685 .num_channels = ARRAY_SIZE(isl29018_channels),
686 .indio_info = &isl29018_info,
687 .regmap_cfg = &isl29018_regmap_config,
690 .channels = isl29023_channels,
691 .num_channels = ARRAY_SIZE(isl29023_channels),
692 .indio_info = &isl29023_info,
693 .regmap_cfg = &isl29018_regmap_config,
696 .channels = isl29023_channels,
697 .num_channels = ARRAY_SIZE(isl29023_channels),
698 .indio_info = &isl29023_info,
699 .regmap_cfg = &isl29035_regmap_config,
703 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
705 const struct acpi_device_id *id;
707 id = acpi_match_device(dev->driver->acpi_match_table, dev);
712 *data = (int)id->driver_data;
714 return dev_name(dev);
717 static int isl29018_probe(struct i2c_client *client,
718 const struct i2c_device_id *id)
720 struct isl29018_chip *chip;
721 struct iio_dev *indio_dev;
723 const char *name = NULL;
726 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
728 dev_err(&client->dev, "iio allocation fails\n");
731 chip = iio_priv(indio_dev);
733 i2c_set_clientdata(client, indio_dev);
737 dev_id = id->driver_data;
740 if (ACPI_HANDLE(&client->dev))
741 name = isl29018_match_acpi_device(&client->dev, &dev_id);
743 mutex_init(&chip->lock);
746 chip->calibscale = 1;
747 chip->ucalibscale = 0;
748 chip->int_time = ISL29018_INT_TIME_16;
749 chip->scale = isl29018_scales[chip->int_time][0];
750 chip->suspended = false;
752 chip->regmap = devm_regmap_init_i2c(client,
753 chip_info_tbl[dev_id].regmap_cfg);
754 if (IS_ERR(chip->regmap)) {
755 err = PTR_ERR(chip->regmap);
756 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
760 err = isl29018_chip_init(chip);
764 indio_dev->info = chip_info_tbl[dev_id].indio_info;
765 indio_dev->channels = chip_info_tbl[dev_id].channels;
766 indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
767 indio_dev->name = name;
768 indio_dev->dev.parent = &client->dev;
769 indio_dev->modes = INDIO_DIRECT_MODE;
770 err = devm_iio_device_register(&client->dev, indio_dev);
772 dev_err(&client->dev, "iio registration fails\n");
779 #ifdef CONFIG_PM_SLEEP
780 static int isl29018_suspend(struct device *dev)
782 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
784 mutex_lock(&chip->lock);
786 /* Since this driver uses only polling commands, we are by default in
787 * auto shutdown (ie, power-down) mode.
788 * So we do not have much to do here.
790 chip->suspended = true;
792 mutex_unlock(&chip->lock);
796 static int isl29018_resume(struct device *dev)
798 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
801 mutex_lock(&chip->lock);
803 err = isl29018_chip_init(chip);
805 chip->suspended = false;
807 mutex_unlock(&chip->lock);
811 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
812 #define ISL29018_PM_OPS (&isl29018_pm_ops)
814 #define ISL29018_PM_OPS NULL
817 static const struct acpi_device_id isl29018_acpi_match[] = {
818 {"ISL29018", isl29018},
819 {"ISL29023", isl29023},
820 {"ISL29035", isl29035},
823 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
825 static const struct i2c_device_id isl29018_id[] = {
826 {"isl29018", isl29018},
827 {"isl29023", isl29023},
828 {"isl29035", isl29035},
832 MODULE_DEVICE_TABLE(i2c, isl29018_id);
834 static const struct of_device_id isl29018_of_match[] = {
835 { .compatible = "isil,isl29018", },
836 { .compatible = "isil,isl29023", },
837 { .compatible = "isil,isl29035", },
840 MODULE_DEVICE_TABLE(of, isl29018_of_match);
842 static struct i2c_driver isl29018_driver = {
843 .class = I2C_CLASS_HWMON,
846 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
847 .pm = ISL29018_PM_OPS,
848 .of_match_table = isl29018_of_match,
850 .probe = isl29018_probe,
851 .id_table = isl29018_id,
853 module_i2c_driver(isl29018_driver);
855 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
856 MODULE_LICENSE("GPL");