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 #define ISL29018_INT_TIME_AVAIL "0.090000 0.005630 0.000351 0.000021"
66 #define ISL29023_INT_TIME_AVAIL "0.090000 0.005600 0.000352 0.000022"
67 #define ISL29035_INT_TIME_AVAIL "0.105000 0.006500 0.000410 0.000025"
69 static const char * const int_time_avail[] = {
70 ISL29018_INT_TIME_AVAIL,
71 ISL29023_INT_TIME_AVAIL,
72 ISL29035_INT_TIME_AVAIL,
75 enum isl29018_int_time {
82 static const unsigned int isl29018_int_utimes[3][4] = {
83 {90000, 5630, 351, 21},
84 {90000, 5600, 352, 22},
85 {105000, 6500, 410, 25},
88 static const struct isl29018_scale {
91 } isl29018_scales[4][4] = {
92 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
93 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
94 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
95 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
98 struct isl29018_chip {
99 struct regmap *regmap;
102 unsigned int calibscale;
103 unsigned int ucalibscale;
104 unsigned int int_time;
105 struct isl29018_scale scale;
110 static int isl29018_set_integration_time(struct isl29018_chip *chip,
114 unsigned int int_time, new_int_time;
116 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
117 if (utime == isl29018_int_utimes[chip->type][i]) {
123 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
126 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
127 ISL29018_CMD2_RESOLUTION_MASK,
128 i << ISL29018_CMD2_RESOLUTION_SHIFT);
132 /* Keep the same range when integration time changes */
133 int_time = chip->int_time;
134 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
135 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
136 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
137 chip->scale = isl29018_scales[new_int_time][i];
141 chip->int_time = new_int_time;
146 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
149 struct isl29018_scale new_scale;
151 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
152 if (scale == isl29018_scales[chip->int_time][i].scale &&
153 uscale == isl29018_scales[chip->int_time][i].uscale) {
154 new_scale = isl29018_scales[chip->int_time][i];
159 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
162 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
163 ISL29018_CMD2_RANGE_MASK,
164 i << ISL29018_CMD2_RANGE_SHIFT);
168 chip->scale = new_scale;
173 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
178 struct device *dev = regmap_get_device(chip->regmap);
181 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
182 mode << ISL29018_CMD1_OPMODE_SHIFT);
185 "Error in setting operating mode err %d\n", status);
188 msleep(ISL29018_CONV_TIME_MS);
189 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
192 "Error in reading LSB DATA with err %d\n", status);
196 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
199 "Error in reading MSB DATA with error %d\n", status);
202 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
204 return (msb << 8) | lsb;
207 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
210 unsigned int data_x_range;
212 lux_data = isl29018_read_sensor_input(chip,
213 ISL29018_CMD1_OPMODE_ALS_ONCE);
217 data_x_range = lux_data * chip->scale.scale +
218 lux_data * chip->scale.uscale / 1000000;
219 *lux = data_x_range * chip->calibscale +
220 data_x_range * chip->ucalibscale / 1000000;
225 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
229 ir_data = isl29018_read_sensor_input(chip,
230 ISL29018_CMD1_OPMODE_IR_ONCE);
239 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
245 struct device *dev = regmap_get_device(chip->regmap);
247 /* Do proximity sensing with required scheme */
248 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
249 ISL29018_CMD2_SCHEME_MASK,
250 scheme << ISL29018_CMD2_SCHEME_SHIFT);
252 dev_err(dev, "Error in setting operating mode\n");
256 prox_data = isl29018_read_sensor_input(chip,
257 ISL29018_CMD1_OPMODE_PROX_ONCE);
262 *near_ir = prox_data;
266 ir_data = isl29018_read_sensor_input(chip,
267 ISL29018_CMD1_OPMODE_IR_ONCE);
271 if (prox_data >= ir_data)
272 *near_ir = prox_data - ir_data;
279 static ssize_t isl29018_show_scale_available(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
283 struct isl29018_chip *chip = iio_priv(indio_dev);
286 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
287 len += sprintf(buf + len, "%d.%06d ",
288 isl29018_scales[chip->int_time][i].scale,
289 isl29018_scales[chip->int_time][i].uscale);
296 static ssize_t isl29018_show_int_time_available(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
300 struct isl29018_chip *chip = iio_priv(indio_dev);
303 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
304 len += sprintf(buf + len, "0.%06d ",
305 isl29018_int_utimes[chip->type][i]);
312 static ssize_t isl29018_show_prox_infrared_suppression(struct device *dev,
313 struct device_attribute *attr,
316 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
317 struct isl29018_chip *chip = iio_priv(indio_dev);
320 * Return the "proximity scheme" i.e. if the chip does on chip
321 * infrared suppression (1 means perform on chip suppression)
323 return sprintf(buf, "%d\n", chip->prox_scheme);
326 static ssize_t isl29018_store_prox_infrared_suppression(struct device *dev,
327 struct device_attribute *attr,
328 const char *buf, size_t count)
330 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
331 struct isl29018_chip *chip = iio_priv(indio_dev);
334 if (kstrtoint(buf, 10, &val))
336 if (!(val == 0 || val == 1))
340 * Get the "proximity scheme" i.e. if the chip does on chip
341 * infrared suppression (1 means perform on chip suppression)
343 mutex_lock(&chip->lock);
344 chip->prox_scheme = val;
345 mutex_unlock(&chip->lock);
350 static int isl29018_write_raw(struct iio_dev *indio_dev,
351 struct iio_chan_spec const *chan,
356 struct isl29018_chip *chip = iio_priv(indio_dev);
359 mutex_lock(&chip->lock);
361 case IIO_CHAN_INFO_CALIBSCALE:
362 if (chan->type == IIO_LIGHT) {
363 chip->calibscale = val;
364 chip->ucalibscale = val2;
368 case IIO_CHAN_INFO_INT_TIME:
369 if (chan->type == IIO_LIGHT) {
371 mutex_unlock(&chip->lock);
374 ret = isl29018_set_integration_time(chip, val2);
377 case IIO_CHAN_INFO_SCALE:
378 if (chan->type == IIO_LIGHT)
379 ret = isl29018_set_scale(chip, val, val2);
384 mutex_unlock(&chip->lock);
389 static int isl29018_read_raw(struct iio_dev *indio_dev,
390 struct iio_chan_spec const *chan,
396 struct isl29018_chip *chip = iio_priv(indio_dev);
398 mutex_lock(&chip->lock);
399 if (chip->suspended) {
400 mutex_unlock(&chip->lock);
404 case IIO_CHAN_INFO_RAW:
405 case IIO_CHAN_INFO_PROCESSED:
406 switch (chan->type) {
408 ret = isl29018_read_lux(chip, val);
411 ret = isl29018_read_ir(chip, val);
414 ret = isl29018_read_proximity_ir(chip,
424 case IIO_CHAN_INFO_INT_TIME:
425 if (chan->type == IIO_LIGHT) {
427 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
428 ret = IIO_VAL_INT_PLUS_MICRO;
431 case IIO_CHAN_INFO_SCALE:
432 if (chan->type == IIO_LIGHT) {
433 *val = chip->scale.scale;
434 *val2 = chip->scale.uscale;
435 ret = IIO_VAL_INT_PLUS_MICRO;
438 case IIO_CHAN_INFO_CALIBSCALE:
439 if (chan->type == IIO_LIGHT) {
440 *val = chip->calibscale;
441 *val2 = chip->ucalibscale;
442 ret = IIO_VAL_INT_PLUS_MICRO;
448 mutex_unlock(&chip->lock);
452 #define ISL29018_LIGHT_CHANNEL { \
456 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
457 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
458 BIT(IIO_CHAN_INFO_SCALE) | \
459 BIT(IIO_CHAN_INFO_INT_TIME), \
462 #define ISL29018_IR_CHANNEL { \
463 .type = IIO_INTENSITY, \
465 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
466 .channel2 = IIO_MOD_LIGHT_IR, \
469 #define ISL29018_PROXIMITY_CHANNEL { \
470 .type = IIO_PROXIMITY, \
471 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
474 static const struct iio_chan_spec isl29018_channels[] = {
475 ISL29018_LIGHT_CHANNEL,
477 ISL29018_PROXIMITY_CHANNEL,
480 static const struct iio_chan_spec isl29023_channels[] = {
481 ISL29018_LIGHT_CHANNEL,
485 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
486 isl29018_show_int_time_available, NULL, 0);
487 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
488 isl29018_show_scale_available, NULL, 0);
489 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
491 isl29018_show_prox_infrared_suppression,
492 isl29018_store_prox_infrared_suppression, 0);
494 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
496 static struct attribute *isl29018_attributes[] = {
497 ISL29018_DEV_ATTR(in_illuminance_scale_available),
498 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
499 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
503 static struct attribute *isl29023_attributes[] = {
504 ISL29018_DEV_ATTR(in_illuminance_scale_available),
505 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
509 static const struct attribute_group isl29018_group = {
510 .attrs = isl29018_attributes,
513 static const struct attribute_group isl29023_group = {
514 .attrs = isl29023_attributes,
517 static int isl29035_detect(struct isl29018_chip *chip)
521 struct device *dev = regmap_get_device(chip->regmap);
523 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
526 "Error reading ID register with error %d\n",
531 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
533 if (id != ISL29035_DEVICE_ID)
536 /* Clear brownout bit */
537 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
538 ISL29035_BOUT_MASK, 0);
547 static int isl29018_chip_init(struct isl29018_chip *chip)
550 struct device *dev = regmap_get_device(chip->regmap);
552 if (chip->type == isl29035) {
553 status = isl29035_detect(chip);
558 /* Code added per Intersil Application Note 1534:
559 * When VDD sinks to approximately 1.8V or below, some of
560 * the part's registers may change their state. When VDD
561 * recovers to 2.25V (or greater), the part may thus be in an
562 * unknown mode of operation. The user can return the part to
563 * a known mode of operation either by (a) setting VDD = 0V for
564 * 1 second or more and then powering back up with a slew rate
565 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
566 * conversions, clear the test registers, and then rewrite all
567 * registers to the desired values.
569 * For ISL29011, ISL29018, ISL29021, ISL29023
570 * 1. Write 0x00 to register 0x08 (TEST)
571 * 2. Write 0x00 to register 0x00 (CMD1)
572 * 3. Rewrite all registers to the desired values
574 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
575 * the same thing EXCEPT the data sheet asks for a 1ms delay after
576 * writing the CMD1 register.
578 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
580 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
585 /* See Intersil AN1534 comments above.
586 * "Operating Mode" (COMMAND1) register is reprogrammed when
587 * data is read from the device.
589 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
591 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
596 usleep_range(1000, 2000); /* per data sheet, page 10 */
599 status = isl29018_set_scale(chip, chip->scale.scale,
602 dev_err(dev, "Init of isl29018 fails\n");
606 status = isl29018_set_integration_time(chip,
607 isl29018_int_utimes[chip->type][chip->int_time]);
609 dev_err(dev, "Init of isl29018 fails\n");
616 static const struct iio_info isl29018_info = {
617 .attrs = &isl29018_group,
618 .driver_module = THIS_MODULE,
619 .read_raw = isl29018_read_raw,
620 .write_raw = isl29018_write_raw,
623 static const struct iio_info isl29023_info = {
624 .attrs = &isl29023_group,
625 .driver_module = THIS_MODULE,
626 .read_raw = isl29018_read_raw,
627 .write_raw = isl29018_write_raw,
630 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
633 case ISL29018_REG_ADD_DATA_LSB:
634 case ISL29018_REG_ADD_DATA_MSB:
635 case ISL29018_REG_ADD_COMMAND1:
636 case ISL29018_REG_TEST:
637 case ISL29035_REG_DEVICE_ID:
644 static const struct regmap_config isl29018_regmap_config = {
647 .volatile_reg = isl29018_is_volatile_reg,
648 .max_register = ISL29018_REG_TEST,
649 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
650 .cache_type = REGCACHE_RBTREE,
653 static const struct regmap_config isl29035_regmap_config = {
656 .volatile_reg = isl29018_is_volatile_reg,
657 .max_register = ISL29035_REG_DEVICE_ID,
658 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
659 .cache_type = REGCACHE_RBTREE,
662 struct isl29018_chip_info {
663 const struct iio_chan_spec *channels;
665 const struct iio_info *indio_info;
666 const struct regmap_config *regmap_cfg;
669 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
671 .channels = isl29018_channels,
672 .num_channels = ARRAY_SIZE(isl29018_channels),
673 .indio_info = &isl29018_info,
674 .regmap_cfg = &isl29018_regmap_config,
677 .channels = isl29023_channels,
678 .num_channels = ARRAY_SIZE(isl29023_channels),
679 .indio_info = &isl29023_info,
680 .regmap_cfg = &isl29018_regmap_config,
683 .channels = isl29023_channels,
684 .num_channels = ARRAY_SIZE(isl29023_channels),
685 .indio_info = &isl29023_info,
686 .regmap_cfg = &isl29035_regmap_config,
690 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
692 const struct acpi_device_id *id;
694 id = acpi_match_device(dev->driver->acpi_match_table, dev);
699 *data = (int)id->driver_data;
701 return dev_name(dev);
704 static int isl29018_probe(struct i2c_client *client,
705 const struct i2c_device_id *id)
707 struct isl29018_chip *chip;
708 struct iio_dev *indio_dev;
710 const char *name = NULL;
713 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
716 chip = iio_priv(indio_dev);
718 i2c_set_clientdata(client, indio_dev);
722 dev_id = id->driver_data;
725 if (ACPI_HANDLE(&client->dev))
726 name = isl29018_match_acpi_device(&client->dev, &dev_id);
728 mutex_init(&chip->lock);
731 chip->calibscale = 1;
732 chip->ucalibscale = 0;
733 chip->int_time = ISL29018_INT_TIME_16;
734 chip->scale = isl29018_scales[chip->int_time][0];
735 chip->suspended = false;
737 chip->regmap = devm_regmap_init_i2c(client,
738 isl29018_chip_info_tbl[dev_id].regmap_cfg);
739 if (IS_ERR(chip->regmap)) {
740 err = PTR_ERR(chip->regmap);
741 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
745 err = isl29018_chip_init(chip);
749 indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
750 indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
751 indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
752 indio_dev->name = name;
753 indio_dev->dev.parent = &client->dev;
754 indio_dev->modes = INDIO_DIRECT_MODE;
755 return devm_iio_device_register(&client->dev, indio_dev);
758 #ifdef CONFIG_PM_SLEEP
759 static int isl29018_suspend(struct device *dev)
761 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
763 mutex_lock(&chip->lock);
765 /* Since this driver uses only polling commands, we are by default in
766 * auto shutdown (ie, power-down) mode.
767 * So we do not have much to do here.
769 chip->suspended = true;
771 mutex_unlock(&chip->lock);
775 static int isl29018_resume(struct device *dev)
777 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
780 mutex_lock(&chip->lock);
782 err = isl29018_chip_init(chip);
784 chip->suspended = false;
786 mutex_unlock(&chip->lock);
790 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
791 #define ISL29018_PM_OPS (&isl29018_pm_ops)
793 #define ISL29018_PM_OPS NULL
796 static const struct acpi_device_id isl29018_acpi_match[] = {
797 {"ISL29018", isl29018},
798 {"ISL29023", isl29023},
799 {"ISL29035", isl29035},
802 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
804 static const struct i2c_device_id isl29018_id[] = {
805 {"isl29018", isl29018},
806 {"isl29023", isl29023},
807 {"isl29035", isl29035},
811 MODULE_DEVICE_TABLE(i2c, isl29018_id);
813 static const struct of_device_id isl29018_of_match[] = {
814 { .compatible = "isil,isl29018", },
815 { .compatible = "isil,isl29023", },
816 { .compatible = "isil,isl29035", },
819 MODULE_DEVICE_TABLE(of, isl29018_of_match);
821 static struct i2c_driver isl29018_driver = {
824 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
825 .pm = ISL29018_PM_OPS,
826 .of_match_table = isl29018_of_match,
828 .probe = isl29018_probe,
829 .id_table = isl29018_id,
831 module_i2c_driver(isl29018_driver);
833 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
834 MODULE_LICENSE("GPL");