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;
120 struct isl29018_scale new_scale;
122 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
123 if (utime == isl29018_int_utimes[chip->type][i]) {
125 new_scale = isl29018_scales[new_int_time][0];
130 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
133 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
134 COMMANDII_RESOLUTION_MASK,
135 i << COMMANDII_RESOLUTION_SHIFT);
139 /* keep the same range when integration time changes */
140 int_time = chip->int_time;
141 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
142 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
143 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
144 chip->scale = isl29018_scales[new_int_time][i];
148 chip->int_time = new_int_time;
153 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
156 struct isl29018_scale new_scale;
158 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
159 if (scale == isl29018_scales[chip->int_time][i].scale &&
160 uscale == isl29018_scales[chip->int_time][i].uscale) {
161 new_scale = isl29018_scales[chip->int_time][i];
166 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
169 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
170 COMMANDII_RANGE_MASK,
171 i << COMMANDII_RANGE_SHIFT);
175 chip->scale = new_scale;
180 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
187 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
188 mode << COMMMAND1_OPMODE_SHIFT);
191 "Error in setting operating mode err %d\n", status);
194 msleep(CONVERSION_TIME_MS);
195 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
198 "Error in reading LSB DATA with err %d\n", status);
202 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
205 "Error in reading MSB DATA with error %d\n", status);
208 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
210 return (msb << 8) | lsb;
213 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
216 unsigned int data_x_range;
218 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
223 data_x_range = lux_data * chip->scale.scale +
224 lux_data * chip->scale.uscale / 1000000;
225 *lux = data_x_range * chip->calibscale +
226 data_x_range * chip->ucalibscale / 1000000;
231 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
235 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
245 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
252 /* Do proximity sensing with required scheme */
253 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
254 COMMANDII_SCHEME_MASK,
255 scheme << COMMANDII_SCHEME_SHIFT);
257 dev_err(chip->dev, "Error in setting operating mode\n");
261 prox_data = isl29018_read_sensor_input(chip,
262 COMMMAND1_OPMODE_PROX_ONCE);
267 *near_ir = prox_data;
271 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
276 if (prox_data >= ir_data)
277 *near_ir = prox_data - ir_data;
284 static ssize_t show_scale_available(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
288 struct isl29018_chip *chip = iio_priv(indio_dev);
291 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
292 len += sprintf(buf + len, "%d.%06d ",
293 isl29018_scales[chip->int_time][i].scale,
294 isl29018_scales[chip->int_time][i].uscale);
301 static ssize_t show_int_time_available(struct device *dev,
302 struct device_attribute *attr, char *buf)
304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
305 struct isl29018_chip *chip = iio_priv(indio_dev);
308 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
309 len += sprintf(buf + len, "0.%06d ",
310 isl29018_int_utimes[chip->type][i]);
317 /* proximity scheme */
318 static ssize_t show_prox_infrared_suppression(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
322 struct isl29018_chip *chip = iio_priv(indio_dev);
324 /* return the "proximity scheme" i.e. if the chip does on chip
325 infrared suppression (1 means perform on chip suppression) */
326 return sprintf(buf, "%d\n", chip->prox_scheme);
329 static ssize_t store_prox_infrared_suppression(struct device *dev,
330 struct device_attribute *attr, const char *buf, size_t count)
332 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
333 struct isl29018_chip *chip = iio_priv(indio_dev);
336 if (kstrtoint(buf, 10, &val))
338 if (!(val == 0 || val == 1)) {
339 dev_err(dev, "The mode is not supported\n");
343 /* get the "proximity scheme" i.e. if the chip does on chip
344 infrared suppression (1 means perform on chip suppression) */
345 mutex_lock(&chip->lock);
346 chip->prox_scheme = val;
347 mutex_unlock(&chip->lock);
353 static int isl29018_write_raw(struct iio_dev *indio_dev,
354 struct iio_chan_spec const *chan,
359 struct isl29018_chip *chip = iio_priv(indio_dev);
362 mutex_lock(&chip->lock);
364 case IIO_CHAN_INFO_CALIBSCALE:
365 if (chan->type == IIO_LIGHT) {
366 chip->calibscale = val;
367 chip->ucalibscale = val2;
371 case IIO_CHAN_INFO_INT_TIME:
372 if (chan->type == IIO_LIGHT) {
374 mutex_unlock(&chip->lock);
377 ret = isl29018_set_integration_time(chip, val2);
380 case IIO_CHAN_INFO_SCALE:
381 if (chan->type == IIO_LIGHT)
382 ret = isl29018_set_scale(chip, val, val2);
387 mutex_unlock(&chip->lock);
392 static int isl29018_read_raw(struct iio_dev *indio_dev,
393 struct iio_chan_spec const *chan,
399 struct isl29018_chip *chip = iio_priv(indio_dev);
401 mutex_lock(&chip->lock);
402 if (chip->suspended) {
403 mutex_unlock(&chip->lock);
407 case IIO_CHAN_INFO_RAW:
408 case IIO_CHAN_INFO_PROCESSED:
409 switch (chan->type) {
411 ret = isl29018_read_lux(chip, val);
414 ret = isl29018_read_ir(chip, val);
417 ret = isl29018_read_proximity_ir(chip,
418 chip->prox_scheme, val);
426 case IIO_CHAN_INFO_INT_TIME:
427 if (chan->type == IIO_LIGHT) {
429 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
430 ret = IIO_VAL_INT_PLUS_MICRO;
433 case IIO_CHAN_INFO_SCALE:
434 if (chan->type == IIO_LIGHT) {
435 *val = chip->scale.scale;
436 *val2 = chip->scale.uscale;
437 ret = IIO_VAL_INT_PLUS_MICRO;
440 case IIO_CHAN_INFO_CALIBSCALE:
441 if (chan->type == IIO_LIGHT) {
442 *val = chip->calibscale;
443 *val2 = chip->ucalibscale;
444 ret = IIO_VAL_INT_PLUS_MICRO;
450 mutex_unlock(&chip->lock);
454 #define ISL29018_LIGHT_CHANNEL { \
458 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
459 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
460 BIT(IIO_CHAN_INFO_SCALE) | \
461 BIT(IIO_CHAN_INFO_INT_TIME), \
464 #define ISL29018_IR_CHANNEL { \
465 .type = IIO_INTENSITY, \
467 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
468 .channel2 = IIO_MOD_LIGHT_IR, \
471 #define ISL29018_PROXIMITY_CHANNEL { \
472 .type = IIO_PROXIMITY, \
473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
476 static const struct iio_chan_spec isl29018_channels[] = {
477 ISL29018_LIGHT_CHANNEL,
479 ISL29018_PROXIMITY_CHANNEL,
482 static const struct iio_chan_spec isl29023_channels[] = {
483 ISL29018_LIGHT_CHANNEL,
487 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
488 show_int_time_available, NULL, 0);
489 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
490 show_scale_available, NULL, 0);
491 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
493 show_prox_infrared_suppression,
494 store_prox_infrared_suppression, 0);
496 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
498 static struct attribute *isl29018_attributes[] = {
499 ISL29018_DEV_ATTR(in_illuminance_scale_available),
500 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
501 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
505 static struct attribute *isl29023_attributes[] = {
506 ISL29018_DEV_ATTR(in_illuminance_scale_available),
507 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
511 static const struct attribute_group isl29018_group = {
512 .attrs = isl29018_attributes,
515 static const struct attribute_group isl29023_group = {
516 .attrs = isl29023_attributes,
519 static int isl29035_detect(struct isl29018_chip *chip)
524 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
527 "Error reading ID register with error %d\n",
532 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
534 if (id != ISL29035_DEVICE_ID)
537 /* clear out brownout bit */
538 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
539 ISL29035_BOUT_MASK, 0);
548 static int isl29018_chip_init(struct isl29018_chip *chip)
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(chip->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(chip->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(chip->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(chip->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 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:
645 * isl29018_regmap_config: regmap configuration.
646 * Use RBTREE mechanism for caching.
648 static const struct regmap_config isl29018_regmap_config = {
651 .volatile_reg = is_volatile_reg,
652 .max_register = ISL29018_REG_TEST,
653 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
654 .cache_type = REGCACHE_RBTREE,
657 /* isl29035_regmap_config: regmap configuration for ISL29035 */
658 static const struct regmap_config isl29035_regmap_config = {
661 .volatile_reg = is_volatile_reg,
662 .max_register = ISL29035_REG_DEVICE_ID,
663 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
664 .cache_type = REGCACHE_RBTREE,
668 const struct iio_chan_spec *channels;
670 const struct iio_info *indio_info;
671 const struct regmap_config *regmap_cfg;
674 static const struct chip_info chip_info_tbl[] = {
676 .channels = isl29018_channels,
677 .num_channels = ARRAY_SIZE(isl29018_channels),
678 .indio_info = &isl29018_info,
679 .regmap_cfg = &isl29018_regmap_config,
682 .channels = isl29023_channels,
683 .num_channels = ARRAY_SIZE(isl29023_channels),
684 .indio_info = &isl29023_info,
685 .regmap_cfg = &isl29018_regmap_config,
688 .channels = isl29023_channels,
689 .num_channels = ARRAY_SIZE(isl29023_channels),
690 .indio_info = &isl29023_info,
691 .regmap_cfg = &isl29035_regmap_config,
695 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
697 const struct acpi_device_id *id;
699 id = acpi_match_device(dev->driver->acpi_match_table, dev);
704 *data = (int) id->driver_data;
706 return dev_name(dev);
709 static int isl29018_probe(struct i2c_client *client,
710 const struct i2c_device_id *id)
712 struct isl29018_chip *chip;
713 struct iio_dev *indio_dev;
715 const char *name = NULL;
718 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
719 if (indio_dev == NULL) {
720 dev_err(&client->dev, "iio allocation fails\n");
723 chip = iio_priv(indio_dev);
725 i2c_set_clientdata(client, indio_dev);
726 chip->dev = &client->dev;
730 dev_id = id->driver_data;
733 if (ACPI_HANDLE(&client->dev))
734 name = isl29018_match_acpi_device(&client->dev, &dev_id);
736 mutex_init(&chip->lock);
739 chip->calibscale = 1;
740 chip->ucalibscale = 0;
741 chip->int_time = ISL29018_INT_TIME_16;
742 chip->scale = isl29018_scales[chip->int_time][0];
743 chip->suspended = false;
745 chip->regmap = devm_regmap_init_i2c(client,
746 chip_info_tbl[dev_id].regmap_cfg);
747 if (IS_ERR(chip->regmap)) {
748 err = PTR_ERR(chip->regmap);
749 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
753 err = isl29018_chip_init(chip);
757 indio_dev->info = chip_info_tbl[dev_id].indio_info;
758 indio_dev->channels = chip_info_tbl[dev_id].channels;
759 indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
760 indio_dev->name = name;
761 indio_dev->dev.parent = &client->dev;
762 indio_dev->modes = INDIO_DIRECT_MODE;
763 err = devm_iio_device_register(&client->dev, indio_dev);
765 dev_err(&client->dev, "iio registration fails\n");
772 #ifdef CONFIG_PM_SLEEP
773 static int isl29018_suspend(struct device *dev)
775 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
777 mutex_lock(&chip->lock);
779 /* Since this driver uses only polling commands, we are by default in
780 * auto shutdown (ie, power-down) mode.
781 * So we do not have much to do here.
783 chip->suspended = true;
785 mutex_unlock(&chip->lock);
789 static int isl29018_resume(struct device *dev)
791 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
794 mutex_lock(&chip->lock);
796 err = isl29018_chip_init(chip);
798 chip->suspended = false;
800 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
810 static const struct acpi_device_id isl29018_acpi_match[] = {
811 {"ISL29018", isl29018},
812 {"ISL29023", isl29023},
813 {"ISL29035", isl29035},
816 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
818 static const struct i2c_device_id isl29018_id[] = {
819 {"isl29018", isl29018},
820 {"isl29023", isl29023},
821 {"isl29035", isl29035},
825 MODULE_DEVICE_TABLE(i2c, isl29018_id);
827 static const struct of_device_id isl29018_of_match[] = {
828 { .compatible = "isil,isl29018", },
829 { .compatible = "isil,isl29023", },
830 { .compatible = "isil,isl29035", },
833 MODULE_DEVICE_TABLE(of, isl29018_of_match);
835 static struct i2c_driver isl29018_driver = {
836 .class = I2C_CLASS_HWMON,
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");