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 {
104 struct regmap *regmap;
107 unsigned int calibscale;
108 unsigned int ucalibscale;
109 unsigned int int_time;
110 struct isl29018_scale scale;
115 static int isl29018_set_integration_time(struct isl29018_chip *chip,
119 unsigned int int_time, new_int_time;
121 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
122 if (utime == isl29018_int_utimes[chip->type][i]) {
128 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
131 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
132 COMMANDII_RESOLUTION_MASK,
133 i << COMMANDII_RESOLUTION_SHIFT);
137 /* keep the same range when integration time changes */
138 int_time = chip->int_time;
139 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
140 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
141 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
142 chip->scale = isl29018_scales[new_int_time][i];
146 chip->int_time = new_int_time;
151 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
154 struct isl29018_scale new_scale;
156 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
157 if (scale == isl29018_scales[chip->int_time][i].scale &&
158 uscale == isl29018_scales[chip->int_time][i].uscale) {
159 new_scale = isl29018_scales[chip->int_time][i];
164 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
167 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
168 COMMANDII_RANGE_MASK,
169 i << COMMANDII_RANGE_SHIFT);
173 chip->scale = new_scale;
178 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
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(chip->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,
250 /* Do proximity sensing with required scheme */
251 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
252 COMMANDII_SCHEME_MASK,
253 scheme << COMMANDII_SCHEME_SHIFT);
255 dev_err(chip->dev, "Error in setting operating mode\n");
259 prox_data = isl29018_read_sensor_input(chip,
260 COMMMAND1_OPMODE_PROX_ONCE);
265 *near_ir = prox_data;
269 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
274 if (prox_data >= ir_data)
275 *near_ir = prox_data - ir_data;
282 static ssize_t show_scale_available(struct device *dev,
283 struct device_attribute *attr, char *buf)
285 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
286 struct isl29018_chip *chip = iio_priv(indio_dev);
289 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
290 len += sprintf(buf + len, "%d.%06d ",
291 isl29018_scales[chip->int_time][i].scale,
292 isl29018_scales[chip->int_time][i].uscale);
299 static ssize_t show_int_time_available(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
303 struct isl29018_chip *chip = iio_priv(indio_dev);
306 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
307 len += sprintf(buf + len, "0.%06d ",
308 isl29018_int_utimes[chip->type][i]);
315 /* proximity scheme */
316 static ssize_t show_prox_infrared_suppression(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
320 struct isl29018_chip *chip = iio_priv(indio_dev);
322 /* return the "proximity scheme" i.e. if the chip does on chip
323 infrared suppression (1 means perform on chip suppression) */
324 return sprintf(buf, "%d\n", chip->prox_scheme);
327 static ssize_t store_prox_infrared_suppression(struct device *dev,
328 struct device_attribute *attr, 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)) {
337 dev_err(dev, "The mode is not supported\n");
341 /* get the "proximity scheme" i.e. if the chip does on chip
342 infrared suppression (1 means perform on chip suppression) */
343 mutex_lock(&chip->lock);
344 chip->prox_scheme = val;
345 mutex_unlock(&chip->lock);
351 static int isl29018_write_raw(struct iio_dev *indio_dev,
352 struct iio_chan_spec const *chan,
357 struct isl29018_chip *chip = iio_priv(indio_dev);
360 mutex_lock(&chip->lock);
362 case IIO_CHAN_INFO_CALIBSCALE:
363 if (chan->type == IIO_LIGHT) {
364 chip->calibscale = val;
365 chip->ucalibscale = val2;
369 case IIO_CHAN_INFO_INT_TIME:
370 if (chan->type == IIO_LIGHT) {
372 mutex_unlock(&chip->lock);
375 ret = isl29018_set_integration_time(chip, val2);
378 case IIO_CHAN_INFO_SCALE:
379 if (chan->type == IIO_LIGHT)
380 ret = isl29018_set_scale(chip, val, val2);
385 mutex_unlock(&chip->lock);
390 static int isl29018_read_raw(struct iio_dev *indio_dev,
391 struct iio_chan_spec const *chan,
397 struct isl29018_chip *chip = iio_priv(indio_dev);
399 mutex_lock(&chip->lock);
400 if (chip->suspended) {
401 mutex_unlock(&chip->lock);
405 case IIO_CHAN_INFO_RAW:
406 case IIO_CHAN_INFO_PROCESSED:
407 switch (chan->type) {
409 ret = isl29018_read_lux(chip, val);
412 ret = isl29018_read_ir(chip, val);
415 ret = isl29018_read_proximity_ir(chip,
416 chip->prox_scheme, val);
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 show_int_time_available, NULL, 0);
487 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
488 show_scale_available, NULL, 0);
489 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
491 show_prox_infrared_suppression,
492 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)
522 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
525 "Error reading ID register with error %d\n",
530 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
532 if (id != ISL29035_DEVICE_ID)
535 /* clear out brownout bit */
536 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
537 ISL29035_BOUT_MASK, 0);
546 static int isl29018_chip_init(struct isl29018_chip *chip)
550 if (chip->type == isl29035) {
551 status = isl29035_detect(chip);
556 /* Code added per Intersil Application Note 1534:
557 * When VDD sinks to approximately 1.8V or below, some of
558 * the part's registers may change their state. When VDD
559 * recovers to 2.25V (or greater), the part may thus be in an
560 * unknown mode of operation. The user can return the part to
561 * a known mode of operation either by (a) setting VDD = 0V for
562 * 1 second or more and then powering back up with a slew rate
563 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
564 * conversions, clear the test registers, and then rewrite all
565 * registers to the desired values.
567 * FOR ISL29011, ISL29018, ISL29021, ISL29023
568 * 1. Write 0x00 to register 0x08 (TEST)
569 * 2. Write 0x00 to register 0x00 (CMD1)
570 * 3. Rewrite all registers to the desired values
572 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
573 * the same thing EXCEPT the data sheet asks for a 1ms delay after
574 * writing the CMD1 register.
576 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
578 dev_err(chip->dev, "Failed to clear isl29018 TEST reg.(%d)\n",
583 /* See Intersil AN1534 comments above.
584 * "Operating Mode" (COMMAND1) register is reprogrammed when
585 * data is read from the device.
587 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
589 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
594 usleep_range(1000, 2000); /* per data sheet, page 10 */
597 status = isl29018_set_scale(chip, chip->scale.scale,
600 dev_err(chip->dev, "Init of isl29018 fails\n");
604 status = isl29018_set_integration_time(chip,
605 isl29018_int_utimes[chip->type][chip->int_time]);
607 dev_err(chip->dev, "Init of isl29018 fails\n");
614 static const struct iio_info isl29018_info = {
615 .attrs = &isl29018_group,
616 .driver_module = THIS_MODULE,
617 .read_raw = &isl29018_read_raw,
618 .write_raw = &isl29018_write_raw,
621 static const struct iio_info isl29023_info = {
622 .attrs = &isl29023_group,
623 .driver_module = THIS_MODULE,
624 .read_raw = &isl29018_read_raw,
625 .write_raw = &isl29018_write_raw,
628 static bool is_volatile_reg(struct device *dev, unsigned int reg)
631 case ISL29018_REG_ADD_DATA_LSB:
632 case ISL29018_REG_ADD_DATA_MSB:
633 case ISL29018_REG_ADD_COMMAND1:
634 case ISL29018_REG_TEST:
635 case ISL29035_REG_DEVICE_ID:
643 * isl29018_regmap_config: regmap configuration.
644 * Use RBTREE mechanism for caching.
646 static const struct regmap_config isl29018_regmap_config = {
649 .volatile_reg = is_volatile_reg,
650 .max_register = ISL29018_REG_TEST,
651 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
652 .cache_type = REGCACHE_RBTREE,
655 /* isl29035_regmap_config: regmap configuration for ISL29035 */
656 static const struct regmap_config isl29035_regmap_config = {
659 .volatile_reg = is_volatile_reg,
660 .max_register = ISL29035_REG_DEVICE_ID,
661 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
662 .cache_type = REGCACHE_RBTREE,
666 const struct iio_chan_spec *channels;
668 const struct iio_info *indio_info;
669 const struct regmap_config *regmap_cfg;
672 static const struct chip_info chip_info_tbl[] = {
674 .channels = isl29018_channels,
675 .num_channels = ARRAY_SIZE(isl29018_channels),
676 .indio_info = &isl29018_info,
677 .regmap_cfg = &isl29018_regmap_config,
680 .channels = isl29023_channels,
681 .num_channels = ARRAY_SIZE(isl29023_channels),
682 .indio_info = &isl29023_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 = &isl29035_regmap_config,
693 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
695 const struct acpi_device_id *id;
697 id = acpi_match_device(dev->driver->acpi_match_table, dev);
702 *data = (int) id->driver_data;
704 return dev_name(dev);
707 static int isl29018_probe(struct i2c_client *client,
708 const struct i2c_device_id *id)
710 struct isl29018_chip *chip;
711 struct iio_dev *indio_dev;
713 const char *name = NULL;
716 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
718 dev_err(&client->dev, "iio allocation fails\n");
721 chip = iio_priv(indio_dev);
723 i2c_set_clientdata(client, indio_dev);
724 chip->dev = &client->dev;
728 dev_id = id->driver_data;
731 if (ACPI_HANDLE(&client->dev))
732 name = isl29018_match_acpi_device(&client->dev, &dev_id);
734 mutex_init(&chip->lock);
737 chip->calibscale = 1;
738 chip->ucalibscale = 0;
739 chip->int_time = ISL29018_INT_TIME_16;
740 chip->scale = isl29018_scales[chip->int_time][0];
741 chip->suspended = false;
743 chip->regmap = devm_regmap_init_i2c(client,
744 chip_info_tbl[dev_id].regmap_cfg);
745 if (IS_ERR(chip->regmap)) {
746 err = PTR_ERR(chip->regmap);
747 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
751 err = isl29018_chip_init(chip);
755 indio_dev->info = chip_info_tbl[dev_id].indio_info;
756 indio_dev->channels = chip_info_tbl[dev_id].channels;
757 indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
758 indio_dev->name = name;
759 indio_dev->dev.parent = &client->dev;
760 indio_dev->modes = INDIO_DIRECT_MODE;
761 err = devm_iio_device_register(&client->dev, indio_dev);
763 dev_err(&client->dev, "iio registration fails\n");
770 #ifdef CONFIG_PM_SLEEP
771 static int isl29018_suspend(struct device *dev)
773 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
775 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);
787 static int isl29018_resume(struct device *dev)
789 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
792 mutex_lock(&chip->lock);
794 err = isl29018_chip_init(chip);
796 chip->suspended = false;
798 mutex_unlock(&chip->lock);
802 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
803 #define ISL29018_PM_OPS (&isl29018_pm_ops)
805 #define ISL29018_PM_OPS NULL
808 static const struct acpi_device_id isl29018_acpi_match[] = {
809 {"ISL29018", isl29018},
810 {"ISL29023", isl29023},
811 {"ISL29035", isl29035},
814 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
816 static const struct i2c_device_id isl29018_id[] = {
817 {"isl29018", isl29018},
818 {"isl29023", isl29023},
819 {"isl29035", isl29035},
823 MODULE_DEVICE_TABLE(i2c, isl29018_id);
825 static const struct of_device_id isl29018_of_match[] = {
826 { .compatible = "isil,isl29018", },
827 { .compatible = "isil,isl29023", },
828 { .compatible = "isil,isl29035", },
831 MODULE_DEVICE_TABLE(of, isl29018_of_match);
833 static struct i2c_driver isl29018_driver = {
834 .class = I2C_CLASS_HWMON,
837 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
838 .pm = ISL29018_PM_OPS,
839 .of_match_table = isl29018_of_match,
841 .probe = isl29018_probe,
842 .id_table = isl29018_id,
844 module_i2c_driver(isl29018_driver);
846 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
847 MODULE_LICENSE("GPL");