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>
34 #define CONVERSION_TIME_MS 100
36 #define ISL29018_REG_ADD_COMMAND1 0x00
37 #define COMMMAND1_OPMODE_SHIFT 5
38 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
39 #define COMMMAND1_OPMODE_POWER_DOWN 0
40 #define COMMMAND1_OPMODE_ALS_ONCE 1
41 #define COMMMAND1_OPMODE_IR_ONCE 2
42 #define COMMMAND1_OPMODE_PROX_ONCE 3
44 #define ISL29018_REG_ADD_COMMANDII 0x01
45 #define COMMANDII_RESOLUTION_SHIFT 2
46 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
48 #define COMMANDII_RANGE_SHIFT 0
49 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
51 #define COMMANDII_SCHEME_SHIFT 7
52 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
54 #define ISL29018_REG_ADD_DATA_LSB 0x02
55 #define ISL29018_REG_ADD_DATA_MSB 0x03
57 #define ISL29018_REG_TEST 0x08
58 #define ISL29018_TEST_SHIFT 0
59 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
61 #define ISL29035_REG_DEVICE_ID 0x0F
62 #define ISL29035_DEVICE_ID_SHIFT 0x03
63 #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
64 #define ISL29035_DEVICE_ID 0x5
65 #define ISL29035_BOUT_SHIFT 0x07
66 #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
68 struct isl29018_chip {
70 struct regmap *regmap;
73 unsigned int lux_scale;
74 unsigned int lux_uscale;
81 static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
82 unsigned int *new_range)
84 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
87 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
88 if (range <= supp_ranges[i]) {
89 *new_range = (unsigned int)supp_ranges[i];
94 if (i >= ARRAY_SIZE(supp_ranges))
97 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
98 COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
101 static int isl29018_set_resolution(struct isl29018_chip *chip,
102 unsigned long adcbit, unsigned int *conf_adc_bit)
104 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
107 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
108 if (adcbit >= supp_adcbit[i]) {
109 *conf_adc_bit = (unsigned int)supp_adcbit[i];
114 if (i >= ARRAY_SIZE(supp_adcbit))
117 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
118 COMMANDII_RESOLUTION_MASK,
119 i << COMMANDII_RESOLUTION_SHIFT);
122 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
129 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
130 mode << COMMMAND1_OPMODE_SHIFT);
133 "Error in setting operating mode err %d\n", status);
136 msleep(CONVERSION_TIME_MS);
137 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
140 "Error in reading LSB DATA with err %d\n", status);
144 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
147 "Error in reading MSB DATA with error %d\n", status);
150 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
152 return (msb << 8) | lsb;
155 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
158 unsigned int data_x_range, lux_unshifted;
160 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
165 /* To support fractional scaling, separate the unshifted lux
166 * into two calculations: int scaling and micro-scaling.
167 * lux_uscale ranges from 0-999999, so about 20 bits. Split
168 * the /1,000,000 in two to reduce the risk of over/underflow.
170 data_x_range = lux_data * chip->range;
171 lux_unshifted = data_x_range * chip->lux_scale;
172 lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
173 *lux = lux_unshifted >> chip->adc_bit;
178 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
182 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
192 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
199 /* Do proximity sensing with required scheme */
200 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
201 COMMANDII_SCHEME_MASK,
202 scheme << COMMANDII_SCHEME_SHIFT);
204 dev_err(chip->dev, "Error in setting operating mode\n");
208 prox_data = isl29018_read_sensor_input(chip,
209 COMMMAND1_OPMODE_PROX_ONCE);
214 *near_ir = prox_data;
218 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
223 if (prox_data >= ir_data)
224 *near_ir = prox_data - ir_data;
231 /* Sysfs interface */
233 static ssize_t show_range(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
237 struct isl29018_chip *chip = iio_priv(indio_dev);
239 return sprintf(buf, "%u\n", chip->range);
242 static ssize_t store_range(struct device *dev,
243 struct device_attribute *attr, const char *buf, size_t count)
245 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
246 struct isl29018_chip *chip = iio_priv(indio_dev);
249 unsigned int new_range;
251 if (kstrtoul(buf, 10, &lval))
254 if (!(lval == 1000UL || lval == 4000UL ||
255 lval == 16000UL || lval == 64000UL)) {
256 dev_err(dev, "The range is not supported\n");
260 mutex_lock(&chip->lock);
261 status = isl29018_set_range(chip, lval, &new_range);
263 mutex_unlock(&chip->lock);
265 "Error in setting max range with err %d\n", status);
268 chip->range = new_range;
269 mutex_unlock(&chip->lock);
275 static ssize_t show_resolution(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
279 struct isl29018_chip *chip = iio_priv(indio_dev);
281 return sprintf(buf, "%u\n", chip->adc_bit);
284 static ssize_t store_resolution(struct device *dev,
285 struct device_attribute *attr, const char *buf, size_t count)
287 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
288 struct isl29018_chip *chip = iio_priv(indio_dev);
291 unsigned int new_adc_bit;
293 if (kstrtouint(buf, 10, &val))
295 if (!(val == 4 || val == 8 || val == 12 || val == 16)) {
296 dev_err(dev, "The resolution is not supported\n");
300 mutex_lock(&chip->lock);
301 status = isl29018_set_resolution(chip, val, &new_adc_bit);
303 mutex_unlock(&chip->lock);
304 dev_err(dev, "Error in setting resolution\n");
307 chip->adc_bit = new_adc_bit;
308 mutex_unlock(&chip->lock);
313 /* proximity scheme */
314 static ssize_t show_prox_infrared_suppression(struct device *dev,
315 struct device_attribute *attr, char *buf)
317 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
318 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) */
322 return sprintf(buf, "%d\n", chip->prox_scheme);
325 static ssize_t store_prox_infrared_suppression(struct device *dev,
326 struct device_attribute *attr, const char *buf, size_t count)
328 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
329 struct isl29018_chip *chip = iio_priv(indio_dev);
332 if (kstrtoint(buf, 10, &val))
334 if (!(val == 0 || val == 1)) {
335 dev_err(dev, "The mode is not supported\n");
339 /* get the "proximity scheme" i.e. if the chip does on chip
340 infrared suppression (1 means perform on chip suppression) */
341 mutex_lock(&chip->lock);
342 chip->prox_scheme = val;
343 mutex_unlock(&chip->lock);
349 static int isl29018_write_raw(struct iio_dev *indio_dev,
350 struct iio_chan_spec const *chan,
355 struct isl29018_chip *chip = iio_priv(indio_dev);
358 mutex_lock(&chip->lock);
359 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
360 chip->lux_scale = val;
361 /* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
362 chip->lux_uscale = val2;
365 mutex_unlock(&chip->lock);
370 static int isl29018_read_raw(struct iio_dev *indio_dev,
371 struct iio_chan_spec const *chan,
377 struct isl29018_chip *chip = iio_priv(indio_dev);
379 mutex_lock(&chip->lock);
380 if (chip->suspended) {
381 mutex_unlock(&chip->lock);
385 case IIO_CHAN_INFO_RAW:
386 case IIO_CHAN_INFO_PROCESSED:
387 switch (chan->type) {
389 ret = isl29018_read_lux(chip, val);
392 ret = isl29018_read_ir(chip, val);
395 ret = isl29018_read_proximity_ir(chip,
396 chip->prox_scheme, val);
404 case IIO_CHAN_INFO_CALIBSCALE:
405 if (chan->type == IIO_LIGHT) {
406 *val = chip->lux_scale;
407 *val2 = chip->lux_uscale;
408 ret = IIO_VAL_INT_PLUS_MICRO;
414 mutex_unlock(&chip->lock);
418 #define ISL29018_LIGHT_CHANNEL { \
422 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
423 BIT(IIO_CHAN_INFO_CALIBSCALE), \
426 #define ISL29018_IR_CHANNEL { \
427 .type = IIO_INTENSITY, \
429 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
430 .channel2 = IIO_MOD_LIGHT_IR, \
433 #define ISL29018_PROXIMITY_CHANNEL { \
434 .type = IIO_PROXIMITY, \
435 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
438 static const struct iio_chan_spec isl29018_channels[] = {
439 ISL29018_LIGHT_CHANNEL,
441 ISL29018_PROXIMITY_CHANNEL,
444 static const struct iio_chan_spec isl29023_channels[] = {
445 ISL29018_LIGHT_CHANNEL,
449 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
450 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
451 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
452 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
453 show_resolution, store_resolution, 0);
454 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
456 show_prox_infrared_suppression,
457 store_prox_infrared_suppression, 0);
459 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
460 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
461 static struct attribute *isl29018_attributes[] = {
462 ISL29018_DEV_ATTR(range),
463 ISL29018_CONST_ATTR(range_available),
464 ISL29018_DEV_ATTR(adc_resolution),
465 ISL29018_CONST_ATTR(adc_resolution_available),
466 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
470 static struct attribute *isl29023_attributes[] = {
471 ISL29018_DEV_ATTR(range),
472 ISL29018_CONST_ATTR(range_available),
473 ISL29018_DEV_ATTR(adc_resolution),
474 ISL29018_CONST_ATTR(adc_resolution_available),
478 static const struct attribute_group isl29018_group = {
479 .attrs = isl29018_attributes,
482 static const struct attribute_group isl29023_group = {
483 .attrs = isl29023_attributes,
486 static int isl29035_detect(struct isl29018_chip *chip)
491 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
494 "Error reading ID register with error %d\n",
499 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
501 if (id != ISL29035_DEVICE_ID)
504 /* clear out brownout bit */
505 return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
506 ISL29035_BOUT_MASK, 0);
515 static int isl29018_chip_init(struct isl29018_chip *chip)
518 unsigned int new_adc_bit;
519 unsigned int new_range;
521 if (chip->type == isl29035) {
522 status = isl29035_detect(chip);
527 /* Code added per Intersil Application Note 1534:
528 * When VDD sinks to approximately 1.8V or below, some of
529 * the part's registers may change their state. When VDD
530 * recovers to 2.25V (or greater), the part may thus be in an
531 * unknown mode of operation. The user can return the part to
532 * a known mode of operation either by (a) setting VDD = 0V for
533 * 1 second or more and then powering back up with a slew rate
534 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
535 * conversions, clear the test registers, and then rewrite all
536 * registers to the desired values.
538 * FOR ISL29011, ISL29018, ISL29021, ISL29023
539 * 1. Write 0x00 to register 0x08 (TEST)
540 * 2. Write 0x00 to register 0x00 (CMD1)
541 * 3. Rewrite all registers to the desired values
543 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
544 * the same thing EXCEPT the data sheet asks for a 1ms delay after
545 * writing the CMD1 register.
547 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
549 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
554 /* See Intersil AN1534 comments above.
555 * "Operating Mode" (COMMAND1) register is reprogrammed when
556 * data is read from the device.
558 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
560 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
565 msleep(1); /* per data sheet, page 10 */
568 status = isl29018_set_range(chip, chip->range, &new_range);
570 dev_err(chip->dev, "Init of isl29018 fails\n");
574 status = isl29018_set_resolution(chip, chip->adc_bit,
580 static const struct iio_info isl29018_info = {
581 .attrs = &isl29018_group,
582 .driver_module = THIS_MODULE,
583 .read_raw = &isl29018_read_raw,
584 .write_raw = &isl29018_write_raw,
587 static const struct iio_info isl29023_info = {
588 .attrs = &isl29023_group,
589 .driver_module = THIS_MODULE,
590 .read_raw = &isl29018_read_raw,
591 .write_raw = &isl29018_write_raw,
594 static bool is_volatile_reg(struct device *dev, unsigned int reg)
597 case ISL29018_REG_ADD_DATA_LSB:
598 case ISL29018_REG_ADD_DATA_MSB:
599 case ISL29018_REG_ADD_COMMAND1:
600 case ISL29018_REG_TEST:
601 case ISL29035_REG_DEVICE_ID:
609 * isl29018_regmap_config: regmap configuration.
610 * Use RBTREE mechanism for caching.
612 static const struct regmap_config isl29018_regmap_config = {
615 .volatile_reg = is_volatile_reg,
616 .max_register = ISL29018_REG_TEST,
617 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
618 .cache_type = REGCACHE_RBTREE,
621 /* isl29035_regmap_config: regmap configuration for ISL29035 */
622 static const struct regmap_config isl29035_regmap_config = {
625 .volatile_reg = is_volatile_reg,
626 .max_register = ISL29035_REG_DEVICE_ID,
627 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
628 .cache_type = REGCACHE_RBTREE,
632 const struct iio_chan_spec *channels;
634 const struct iio_info *indio_info;
635 const struct regmap_config *regmap_cfg;
638 static const struct chip_info chip_info_tbl[] = {
640 .channels = isl29018_channels,
641 .num_channels = ARRAY_SIZE(isl29018_channels),
642 .indio_info = &isl29018_info,
643 .regmap_cfg = &isl29018_regmap_config,
646 .channels = isl29023_channels,
647 .num_channels = ARRAY_SIZE(isl29023_channels),
648 .indio_info = &isl29023_info,
649 .regmap_cfg = &isl29018_regmap_config,
652 .channels = isl29023_channels,
653 .num_channels = ARRAY_SIZE(isl29023_channels),
654 .indio_info = &isl29023_info,
655 .regmap_cfg = &isl29035_regmap_config,
659 static int isl29018_probe(struct i2c_client *client,
660 const struct i2c_device_id *id)
662 struct isl29018_chip *chip;
663 struct iio_dev *indio_dev;
666 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
667 if (indio_dev == NULL) {
668 dev_err(&client->dev, "iio allocation fails\n");
671 chip = iio_priv(indio_dev);
673 i2c_set_clientdata(client, indio_dev);
674 chip->dev = &client->dev;
676 mutex_init(&chip->lock);
678 chip->type = id->driver_data;
680 chip->lux_uscale = 0;
683 chip->suspended = false;
685 chip->regmap = devm_regmap_init_i2c(client,
686 chip_info_tbl[id->driver_data].regmap_cfg);
687 if (IS_ERR(chip->regmap)) {
688 err = PTR_ERR(chip->regmap);
689 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
693 err = isl29018_chip_init(chip);
697 indio_dev->info = chip_info_tbl[id->driver_data].indio_info;
698 indio_dev->channels = chip_info_tbl[id->driver_data].channels;
699 indio_dev->num_channels = chip_info_tbl[id->driver_data].num_channels;
700 indio_dev->name = id->name;
701 indio_dev->dev.parent = &client->dev;
702 indio_dev->modes = INDIO_DIRECT_MODE;
703 err = devm_iio_device_register(&client->dev, indio_dev);
705 dev_err(&client->dev, "iio registration fails\n");
712 #ifdef CONFIG_PM_SLEEP
713 static int isl29018_suspend(struct device *dev)
715 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
717 mutex_lock(&chip->lock);
719 /* Since this driver uses only polling commands, we are by default in
720 * auto shutdown (ie, power-down) mode.
721 * So we do not have much to do here.
723 chip->suspended = true;
725 mutex_unlock(&chip->lock);
729 static int isl29018_resume(struct device *dev)
731 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
734 mutex_lock(&chip->lock);
736 err = isl29018_chip_init(chip);
738 chip->suspended = false;
740 mutex_unlock(&chip->lock);
744 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
745 #define ISL29018_PM_OPS (&isl29018_pm_ops)
747 #define ISL29018_PM_OPS NULL
750 static const struct i2c_device_id isl29018_id[] = {
751 {"isl29018", isl29018},
752 {"isl29023", isl29023},
753 {"isl29035", isl29035},
757 MODULE_DEVICE_TABLE(i2c, isl29018_id);
759 static const struct of_device_id isl29018_of_match[] = {
760 { .compatible = "isil,isl29018", },
761 { .compatible = "isil,isl29023", },
762 { .compatible = "isil,isl29035", },
765 MODULE_DEVICE_TABLE(of, isl29018_of_match);
767 static struct i2c_driver isl29018_driver = {
768 .class = I2C_CLASS_HWMON,
771 .pm = ISL29018_PM_OPS,
772 .owner = THIS_MODULE,
773 .of_match_table = isl29018_of_match,
775 .probe = isl29018_probe,
776 .id_table = isl29018_id,
778 module_i2c_driver(isl29018_driver);
780 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
781 MODULE_LICENSE("GPL");