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 struct isl29018_chip {
71 struct regmap *regmap;
74 unsigned int lux_scale;
75 unsigned int lux_uscale;
82 static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
83 unsigned int *new_range)
85 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
88 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
89 if (range <= supp_ranges[i]) {
90 *new_range = (unsigned int)supp_ranges[i];
95 if (i >= ARRAY_SIZE(supp_ranges))
98 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
99 COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
102 static int isl29018_set_resolution(struct isl29018_chip *chip,
103 unsigned long adcbit, unsigned int *conf_adc_bit)
105 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
108 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
109 if (adcbit >= supp_adcbit[i]) {
110 *conf_adc_bit = (unsigned int)supp_adcbit[i];
115 if (i >= ARRAY_SIZE(supp_adcbit))
118 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
119 COMMANDII_RESOLUTION_MASK,
120 i << COMMANDII_RESOLUTION_SHIFT);
123 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
130 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
131 mode << COMMMAND1_OPMODE_SHIFT);
134 "Error in setting operating mode err %d\n", status);
137 msleep(CONVERSION_TIME_MS);
138 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
141 "Error in reading LSB DATA with err %d\n", status);
145 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
148 "Error in reading MSB DATA with error %d\n", status);
151 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
153 return (msb << 8) | lsb;
156 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
159 unsigned int data_x_range, lux_unshifted;
161 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
166 /* To support fractional scaling, separate the unshifted lux
167 * into two calculations: int scaling and micro-scaling.
168 * lux_uscale ranges from 0-999999, so about 20 bits. Split
169 * the /1,000,000 in two to reduce the risk of over/underflow.
171 data_x_range = lux_data * chip->range;
172 lux_unshifted = data_x_range * chip->lux_scale;
173 lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
174 *lux = lux_unshifted >> chip->adc_bit;
179 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
183 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
193 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
200 /* Do proximity sensing with required scheme */
201 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
202 COMMANDII_SCHEME_MASK,
203 scheme << COMMANDII_SCHEME_SHIFT);
205 dev_err(chip->dev, "Error in setting operating mode\n");
209 prox_data = isl29018_read_sensor_input(chip,
210 COMMMAND1_OPMODE_PROX_ONCE);
215 *near_ir = prox_data;
219 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
224 if (prox_data >= ir_data)
225 *near_ir = prox_data - ir_data;
232 /* Sysfs interface */
234 static ssize_t show_range(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
238 struct isl29018_chip *chip = iio_priv(indio_dev);
240 return sprintf(buf, "%u\n", chip->range);
243 static ssize_t store_range(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
246 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
247 struct isl29018_chip *chip = iio_priv(indio_dev);
250 unsigned int new_range;
252 if (kstrtoul(buf, 10, &lval))
255 if (!(lval == 1000UL || lval == 4000UL ||
256 lval == 16000UL || lval == 64000UL)) {
257 dev_err(dev, "The range is not supported\n");
261 mutex_lock(&chip->lock);
262 status = isl29018_set_range(chip, lval, &new_range);
264 mutex_unlock(&chip->lock);
266 "Error in setting max range with err %d\n", status);
269 chip->range = new_range;
270 mutex_unlock(&chip->lock);
276 static ssize_t show_resolution(struct device *dev,
277 struct device_attribute *attr, char *buf)
279 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
280 struct isl29018_chip *chip = iio_priv(indio_dev);
282 return sprintf(buf, "%u\n", chip->adc_bit);
285 static ssize_t store_resolution(struct device *dev,
286 struct device_attribute *attr, const char *buf, size_t count)
288 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
289 struct isl29018_chip *chip = iio_priv(indio_dev);
292 unsigned int new_adc_bit;
294 if (kstrtouint(buf, 10, &val))
296 if (!(val == 4 || val == 8 || val == 12 || val == 16)) {
297 dev_err(dev, "The resolution is not supported\n");
301 mutex_lock(&chip->lock);
302 status = isl29018_set_resolution(chip, val, &new_adc_bit);
304 mutex_unlock(&chip->lock);
305 dev_err(dev, "Error in setting resolution\n");
308 chip->adc_bit = new_adc_bit;
309 mutex_unlock(&chip->lock);
314 /* proximity scheme */
315 static ssize_t show_prox_infrared_suppression(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
319 struct isl29018_chip *chip = iio_priv(indio_dev);
321 /* return the "proximity scheme" i.e. if the chip does on chip
322 infrared suppression (1 means perform on chip suppression) */
323 return sprintf(buf, "%d\n", chip->prox_scheme);
326 static ssize_t store_prox_infrared_suppression(struct device *dev,
327 struct device_attribute *attr, const char *buf, size_t count)
329 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
330 struct isl29018_chip *chip = iio_priv(indio_dev);
333 if (kstrtoint(buf, 10, &val))
335 if (!(val == 0 || val == 1)) {
336 dev_err(dev, "The mode is not supported\n");
340 /* get the "proximity scheme" i.e. if the chip does on chip
341 infrared suppression (1 means perform on chip suppression) */
342 mutex_lock(&chip->lock);
343 chip->prox_scheme = val;
344 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);
360 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
361 chip->lux_scale = val;
362 /* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
363 chip->lux_uscale = val2;
366 mutex_unlock(&chip->lock);
371 static int isl29018_read_raw(struct iio_dev *indio_dev,
372 struct iio_chan_spec const *chan,
378 struct isl29018_chip *chip = iio_priv(indio_dev);
380 mutex_lock(&chip->lock);
381 if (chip->suspended) {
382 mutex_unlock(&chip->lock);
386 case IIO_CHAN_INFO_RAW:
387 case IIO_CHAN_INFO_PROCESSED:
388 switch (chan->type) {
390 ret = isl29018_read_lux(chip, val);
393 ret = isl29018_read_ir(chip, val);
396 ret = isl29018_read_proximity_ir(chip,
397 chip->prox_scheme, val);
405 case IIO_CHAN_INFO_CALIBSCALE:
406 if (chan->type == IIO_LIGHT) {
407 *val = chip->lux_scale;
408 *val2 = chip->lux_uscale;
409 ret = IIO_VAL_INT_PLUS_MICRO;
415 mutex_unlock(&chip->lock);
419 #define ISL29018_LIGHT_CHANNEL { \
423 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
424 BIT(IIO_CHAN_INFO_CALIBSCALE), \
427 #define ISL29018_IR_CHANNEL { \
428 .type = IIO_INTENSITY, \
430 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
431 .channel2 = IIO_MOD_LIGHT_IR, \
434 #define ISL29018_PROXIMITY_CHANNEL { \
435 .type = IIO_PROXIMITY, \
436 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
439 static const struct iio_chan_spec isl29018_channels[] = {
440 ISL29018_LIGHT_CHANNEL,
442 ISL29018_PROXIMITY_CHANNEL,
445 static const struct iio_chan_spec isl29023_channels[] = {
446 ISL29018_LIGHT_CHANNEL,
450 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
451 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
452 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
453 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
454 show_resolution, store_resolution, 0);
455 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
457 show_prox_infrared_suppression,
458 store_prox_infrared_suppression, 0);
460 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
461 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
462 static struct attribute *isl29018_attributes[] = {
463 ISL29018_DEV_ATTR(range),
464 ISL29018_CONST_ATTR(range_available),
465 ISL29018_DEV_ATTR(adc_resolution),
466 ISL29018_CONST_ATTR(adc_resolution_available),
467 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
471 static struct attribute *isl29023_attributes[] = {
472 ISL29018_DEV_ATTR(range),
473 ISL29018_CONST_ATTR(range_available),
474 ISL29018_DEV_ATTR(adc_resolution),
475 ISL29018_CONST_ATTR(adc_resolution_available),
479 static const struct attribute_group isl29018_group = {
480 .attrs = isl29018_attributes,
483 static const struct attribute_group isl29023_group = {
484 .attrs = isl29023_attributes,
487 static int isl29035_detect(struct isl29018_chip *chip)
492 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
495 "Error reading ID register with error %d\n",
500 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
502 if (id != ISL29035_DEVICE_ID)
505 /* clear out brownout bit */
506 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
507 ISL29035_BOUT_MASK, 0);
516 static int isl29018_chip_init(struct isl29018_chip *chip)
519 unsigned int new_adc_bit;
520 unsigned int new_range;
522 if (chip->type == isl29035) {
523 status = isl29035_detect(chip);
528 /* Code added per Intersil Application Note 1534:
529 * When VDD sinks to approximately 1.8V or below, some of
530 * the part's registers may change their state. When VDD
531 * recovers to 2.25V (or greater), the part may thus be in an
532 * unknown mode of operation. The user can return the part to
533 * a known mode of operation either by (a) setting VDD = 0V for
534 * 1 second or more and then powering back up with a slew rate
535 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
536 * conversions, clear the test registers, and then rewrite all
537 * registers to the desired values.
539 * FOR ISL29011, ISL29018, ISL29021, ISL29023
540 * 1. Write 0x00 to register 0x08 (TEST)
541 * 2. Write 0x00 to register 0x00 (CMD1)
542 * 3. Rewrite all registers to the desired values
544 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
545 * the same thing EXCEPT the data sheet asks for a 1ms delay after
546 * writing the CMD1 register.
548 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
550 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
555 /* See Intersil AN1534 comments above.
556 * "Operating Mode" (COMMAND1) register is reprogrammed when
557 * data is read from the device.
559 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
561 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
566 msleep(1); /* per data sheet, page 10 */
569 status = isl29018_set_range(chip, chip->range, &new_range);
571 dev_err(chip->dev, "Init of isl29018 fails\n");
575 status = isl29018_set_resolution(chip, chip->adc_bit,
581 static const struct iio_info isl29018_info = {
582 .attrs = &isl29018_group,
583 .driver_module = THIS_MODULE,
584 .read_raw = &isl29018_read_raw,
585 .write_raw = &isl29018_write_raw,
588 static const struct iio_info isl29023_info = {
589 .attrs = &isl29023_group,
590 .driver_module = THIS_MODULE,
591 .read_raw = &isl29018_read_raw,
592 .write_raw = &isl29018_write_raw,
595 static bool is_volatile_reg(struct device *dev, unsigned int reg)
598 case ISL29018_REG_ADD_DATA_LSB:
599 case ISL29018_REG_ADD_DATA_MSB:
600 case ISL29018_REG_ADD_COMMAND1:
601 case ISL29018_REG_TEST:
602 case ISL29035_REG_DEVICE_ID:
610 * isl29018_regmap_config: regmap configuration.
611 * Use RBTREE mechanism for caching.
613 static const struct regmap_config isl29018_regmap_config = {
616 .volatile_reg = is_volatile_reg,
617 .max_register = ISL29018_REG_TEST,
618 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
619 .cache_type = REGCACHE_RBTREE,
622 /* isl29035_regmap_config: regmap configuration for ISL29035 */
623 static const struct regmap_config isl29035_regmap_config = {
626 .volatile_reg = is_volatile_reg,
627 .max_register = ISL29035_REG_DEVICE_ID,
628 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
629 .cache_type = REGCACHE_RBTREE,
633 const struct iio_chan_spec *channels;
635 const struct iio_info *indio_info;
636 const struct regmap_config *regmap_cfg;
639 static const struct chip_info chip_info_tbl[] = {
641 .channels = isl29018_channels,
642 .num_channels = ARRAY_SIZE(isl29018_channels),
643 .indio_info = &isl29018_info,
644 .regmap_cfg = &isl29018_regmap_config,
647 .channels = isl29023_channels,
648 .num_channels = ARRAY_SIZE(isl29023_channels),
649 .indio_info = &isl29023_info,
650 .regmap_cfg = &isl29018_regmap_config,
653 .channels = isl29023_channels,
654 .num_channels = ARRAY_SIZE(isl29023_channels),
655 .indio_info = &isl29023_info,
656 .regmap_cfg = &isl29035_regmap_config,
660 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
662 const struct acpi_device_id *id;
664 id = acpi_match_device(dev->driver->acpi_match_table, dev);
669 *data = (int) id->driver_data;
671 return dev_name(dev);
674 static int isl29018_probe(struct i2c_client *client,
675 const struct i2c_device_id *id)
677 struct isl29018_chip *chip;
678 struct iio_dev *indio_dev;
680 const char *name = NULL;
683 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
684 if (indio_dev == NULL) {
685 dev_err(&client->dev, "iio allocation fails\n");
688 chip = iio_priv(indio_dev);
690 i2c_set_clientdata(client, indio_dev);
691 chip->dev = &client->dev;
695 dev_id = id->driver_data;
698 if (ACPI_HANDLE(&client->dev))
699 name = isl29018_match_acpi_device(&client->dev, &dev_id);
701 mutex_init(&chip->lock);
705 chip->lux_uscale = 0;
708 chip->suspended = false;
710 chip->regmap = devm_regmap_init_i2c(client,
711 chip_info_tbl[dev_id].regmap_cfg);
712 if (IS_ERR(chip->regmap)) {
713 err = PTR_ERR(chip->regmap);
714 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
718 err = isl29018_chip_init(chip);
722 indio_dev->info = chip_info_tbl[dev_id].indio_info;
723 indio_dev->channels = chip_info_tbl[dev_id].channels;
724 indio_dev->num_channels = chip_info_tbl[dev_id].num_channels;
725 indio_dev->name = name;
726 indio_dev->dev.parent = &client->dev;
727 indio_dev->modes = INDIO_DIRECT_MODE;
728 err = devm_iio_device_register(&client->dev, indio_dev);
730 dev_err(&client->dev, "iio registration fails\n");
737 #ifdef CONFIG_PM_SLEEP
738 static int isl29018_suspend(struct device *dev)
740 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
742 mutex_lock(&chip->lock);
744 /* Since this driver uses only polling commands, we are by default in
745 * auto shutdown (ie, power-down) mode.
746 * So we do not have much to do here.
748 chip->suspended = true;
750 mutex_unlock(&chip->lock);
754 static int isl29018_resume(struct device *dev)
756 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
759 mutex_lock(&chip->lock);
761 err = isl29018_chip_init(chip);
763 chip->suspended = false;
765 mutex_unlock(&chip->lock);
769 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
770 #define ISL29018_PM_OPS (&isl29018_pm_ops)
772 #define ISL29018_PM_OPS NULL
775 static const struct acpi_device_id isl29018_acpi_match[] = {
776 {"ISL29018", isl29018},
777 {"ISL29023", isl29023},
778 {"ISL29035", isl29035},
781 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
783 static const struct i2c_device_id isl29018_id[] = {
784 {"isl29018", isl29018},
785 {"isl29023", isl29023},
786 {"isl29035", isl29035},
790 MODULE_DEVICE_TABLE(i2c, isl29018_id);
792 static const struct of_device_id isl29018_of_match[] = {
793 { .compatible = "isil,isl29018", },
794 { .compatible = "isil,isl29023", },
795 { .compatible = "isil,isl29035", },
798 MODULE_DEVICE_TABLE(of, isl29018_of_match);
800 static struct i2c_driver isl29018_driver = {
801 .class = I2C_CLASS_HWMON,
804 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
805 .pm = ISL29018_PM_OPS,
806 .owner = THIS_MODULE,
807 .of_match_table = isl29018_of_match,
809 .probe = isl29018_probe,
810 .id_table = isl29018_id,
812 module_i2c_driver(isl29018_driver);
814 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
815 MODULE_LICENSE("GPL");