2 * A iio driver for the light sensor ISL 29018.
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 struct isl29018_chip {
63 struct regmap *regmap;
65 unsigned int lux_scale;
66 unsigned int lux_uscale;
73 static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
74 unsigned int *new_range)
76 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
79 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
80 if (range <= supp_ranges[i]) {
81 *new_range = (unsigned int)supp_ranges[i];
86 if (i >= ARRAY_SIZE(supp_ranges))
89 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
90 COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
93 static int isl29018_set_resolution(struct isl29018_chip *chip,
94 unsigned long adcbit, unsigned int *conf_adc_bit)
96 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
99 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
100 if (adcbit >= supp_adcbit[i]) {
101 *conf_adc_bit = (unsigned int)supp_adcbit[i];
106 if (i >= ARRAY_SIZE(supp_adcbit))
109 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
110 COMMANDII_RESOLUTION_MASK,
111 i << COMMANDII_RESOLUTION_SHIFT);
114 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
121 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
122 mode << COMMMAND1_OPMODE_SHIFT);
125 "Error in setting operating mode err %d\n", status);
128 msleep(CONVERSION_TIME_MS);
129 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
132 "Error in reading LSB DATA with err %d\n", status);
136 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
139 "Error in reading MSB DATA with error %d\n", status);
142 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
144 return (msb << 8) | lsb;
147 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
150 unsigned int data_x_range, lux_unshifted;
152 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
157 /* To support fractional scaling, separate the unshifted lux
158 * into two calculations: int scaling and micro-scaling.
159 * lux_uscale ranges from 0-999999, so about 20 bits. Split
160 * the /1,000,000 in two to reduce the risk of over/underflow.
162 data_x_range = lux_data * chip->range;
163 lux_unshifted = data_x_range * chip->lux_scale;
164 lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
165 *lux = lux_unshifted >> chip->adc_bit;
170 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
174 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
184 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
191 /* Do proximity sensing with required scheme */
192 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
193 COMMANDII_SCHEME_MASK,
194 scheme << COMMANDII_SCHEME_SHIFT);
196 dev_err(chip->dev, "Error in setting operating mode\n");
200 prox_data = isl29018_read_sensor_input(chip,
201 COMMMAND1_OPMODE_PROX_ONCE);
206 *near_ir = prox_data;
210 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
215 if (prox_data >= ir_data)
216 *near_ir = prox_data - ir_data;
223 /* Sysfs interface */
225 static ssize_t show_range(struct device *dev,
226 struct device_attribute *attr, char *buf)
228 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
229 struct isl29018_chip *chip = iio_priv(indio_dev);
231 return sprintf(buf, "%u\n", chip->range);
234 static ssize_t store_range(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
237 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
238 struct isl29018_chip *chip = iio_priv(indio_dev);
241 unsigned int new_range;
243 if (kstrtoul(buf, 10, &lval))
246 if (!(lval == 1000UL || lval == 4000UL ||
247 lval == 16000UL || lval == 64000UL)) {
248 dev_err(dev, "The range is not supported\n");
252 mutex_lock(&chip->lock);
253 status = isl29018_set_range(chip, lval, &new_range);
255 mutex_unlock(&chip->lock);
257 "Error in setting max range with err %d\n", status);
260 chip->range = new_range;
261 mutex_unlock(&chip->lock);
267 static ssize_t show_resolution(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
271 struct isl29018_chip *chip = iio_priv(indio_dev);
273 return sprintf(buf, "%u\n", chip->adc_bit);
276 static ssize_t store_resolution(struct device *dev,
277 struct device_attribute *attr, const char *buf, size_t count)
279 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
280 struct isl29018_chip *chip = iio_priv(indio_dev);
283 unsigned int new_adc_bit;
285 if (kstrtouint(buf, 10, &val))
287 if (!(val == 4 || val == 8 || val == 12 || val == 16)) {
288 dev_err(dev, "The resolution is not supported\n");
292 mutex_lock(&chip->lock);
293 status = isl29018_set_resolution(chip, val, &new_adc_bit);
295 mutex_unlock(&chip->lock);
296 dev_err(dev, "Error in setting resolution\n");
299 chip->adc_bit = new_adc_bit;
300 mutex_unlock(&chip->lock);
305 /* proximity scheme */
306 static ssize_t show_prox_infrared_suppression(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
310 struct isl29018_chip *chip = iio_priv(indio_dev);
312 /* return the "proximity scheme" i.e. if the chip does on chip
313 infrared suppression (1 means perform on chip suppression) */
314 return sprintf(buf, "%d\n", chip->prox_scheme);
317 static ssize_t store_prox_infrared_suppression(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
320 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
321 struct isl29018_chip *chip = iio_priv(indio_dev);
324 if (kstrtoint(buf, 10, &val))
326 if (!(val == 0 || val == 1)) {
327 dev_err(dev, "The mode is not supported\n");
331 /* get the "proximity scheme" i.e. if the chip does on chip
332 infrared suppression (1 means perform on chip suppression) */
333 mutex_lock(&chip->lock);
334 chip->prox_scheme = val;
335 mutex_unlock(&chip->lock);
341 static int isl29018_write_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
347 struct isl29018_chip *chip = iio_priv(indio_dev);
350 mutex_lock(&chip->lock);
351 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
352 chip->lux_scale = val;
353 /* With no write_raw_get_fmt(), val2 is a MICRO fraction. */
354 chip->lux_uscale = val2;
357 mutex_unlock(&chip->lock);
362 static int isl29018_read_raw(struct iio_dev *indio_dev,
363 struct iio_chan_spec const *chan,
369 struct isl29018_chip *chip = iio_priv(indio_dev);
371 mutex_lock(&chip->lock);
372 if (chip->suspended) {
373 mutex_unlock(&chip->lock);
377 case IIO_CHAN_INFO_RAW:
378 case IIO_CHAN_INFO_PROCESSED:
379 switch (chan->type) {
381 ret = isl29018_read_lux(chip, val);
384 ret = isl29018_read_ir(chip, val);
387 ret = isl29018_read_proximity_ir(chip,
388 chip->prox_scheme, val);
396 case IIO_CHAN_INFO_CALIBSCALE:
397 if (chan->type == IIO_LIGHT) {
398 *val = chip->lux_scale;
399 *val2 = chip->lux_uscale;
400 ret = IIO_VAL_INT_PLUS_MICRO;
406 mutex_unlock(&chip->lock);
410 static const struct iio_chan_spec isl29018_channels[] = {
415 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
416 BIT(IIO_CHAN_INFO_CALIBSCALE),
418 .type = IIO_INTENSITY,
420 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
421 .channel2 = IIO_MOD_LIGHT_IR,
423 /* Unindexed in current ABI. But perhaps it should be. */
424 .type = IIO_PROXIMITY,
425 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
429 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
430 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
431 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
432 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
433 show_resolution, store_resolution, 0);
434 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
436 show_prox_infrared_suppression,
437 store_prox_infrared_suppression, 0);
439 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
440 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
441 static struct attribute *isl29018_attributes[] = {
442 ISL29018_DEV_ATTR(range),
443 ISL29018_CONST_ATTR(range_available),
444 ISL29018_DEV_ATTR(adc_resolution),
445 ISL29018_CONST_ATTR(adc_resolution_available),
446 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
450 static const struct attribute_group isl29108_group = {
451 .attrs = isl29018_attributes,
454 static int isl29018_chip_init(struct isl29018_chip *chip)
458 unsigned int new_range;
460 /* Code added per Intersil Application Note 1534:
461 * When VDD sinks to approximately 1.8V or below, some of
462 * the part's registers may change their state. When VDD
463 * recovers to 2.25V (or greater), the part may thus be in an
464 * unknown mode of operation. The user can return the part to
465 * a known mode of operation either by (a) setting VDD = 0V for
466 * 1 second or more and then powering back up with a slew rate
467 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
468 * conversions, clear the test registers, and then rewrite all
469 * registers to the desired values.
471 * FOR ISL29011, ISL29018, ISL29021, ISL29023
472 * 1. Write 0x00 to register 0x08 (TEST)
473 * 2. Write 0x00 to register 0x00 (CMD1)
474 * 3. Rewrite all registers to the desired values
476 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
477 * the same thing EXCEPT the data sheet asks for a 1ms delay after
478 * writing the CMD1 register.
480 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
482 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
487 /* See Intersil AN1534 comments above.
488 * "Operating Mode" (COMMAND1) register is reprogrammed when
489 * data is read from the device.
491 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
493 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
498 msleep(1); /* per data sheet, page 10 */
501 status = isl29018_set_range(chip, chip->range, &new_range);
503 dev_err(chip->dev, "Init of isl29018 fails\n");
507 status = isl29018_set_resolution(chip, chip->adc_bit,
513 static const struct iio_info isl29108_info = {
514 .attrs = &isl29108_group,
515 .driver_module = THIS_MODULE,
516 .read_raw = &isl29018_read_raw,
517 .write_raw = &isl29018_write_raw,
520 static bool is_volatile_reg(struct device *dev, unsigned int reg)
523 case ISL29018_REG_ADD_DATA_LSB:
524 case ISL29018_REG_ADD_DATA_MSB:
525 case ISL29018_REG_ADD_COMMAND1:
526 case ISL29018_REG_TEST:
534 * isl29018_regmap_config: regmap configuration.
535 * Use RBTREE mechanism for caching.
537 static const struct regmap_config isl29018_regmap_config = {
540 .volatile_reg = is_volatile_reg,
541 .max_register = ISL29018_REG_TEST,
542 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
543 .cache_type = REGCACHE_RBTREE,
546 static int isl29018_probe(struct i2c_client *client,
547 const struct i2c_device_id *id)
549 struct isl29018_chip *chip;
550 struct iio_dev *indio_dev;
553 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
554 if (indio_dev == NULL) {
555 dev_err(&client->dev, "iio allocation fails\n");
558 chip = iio_priv(indio_dev);
560 i2c_set_clientdata(client, indio_dev);
561 chip->dev = &client->dev;
563 mutex_init(&chip->lock);
566 chip->lux_uscale = 0;
569 chip->suspended = false;
571 chip->regmap = devm_regmap_init_i2c(client, &isl29018_regmap_config);
572 if (IS_ERR(chip->regmap)) {
573 err = PTR_ERR(chip->regmap);
574 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
578 err = isl29018_chip_init(chip);
582 indio_dev->info = &isl29108_info;
583 indio_dev->channels = isl29018_channels;
584 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
585 indio_dev->name = id->name;
586 indio_dev->dev.parent = &client->dev;
587 indio_dev->modes = INDIO_DIRECT_MODE;
588 err = iio_device_register(indio_dev);
590 dev_err(&client->dev, "iio registration fails\n");
597 static int isl29018_remove(struct i2c_client *client)
599 struct iio_dev *indio_dev = i2c_get_clientdata(client);
601 dev_dbg(&client->dev, "%s()\n", __func__);
602 iio_device_unregister(indio_dev);
607 #ifdef CONFIG_PM_SLEEP
608 static int isl29018_suspend(struct device *dev)
610 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
612 mutex_lock(&chip->lock);
614 /* Since this driver uses only polling commands, we are by default in
615 * auto shutdown (ie, power-down) mode.
616 * So we do not have much to do here.
618 chip->suspended = true;
620 mutex_unlock(&chip->lock);
624 static int isl29018_resume(struct device *dev)
626 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
629 mutex_lock(&chip->lock);
631 err = isl29018_chip_init(chip);
633 chip->suspended = false;
635 mutex_unlock(&chip->lock);
639 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
640 #define ISL29018_PM_OPS (&isl29018_pm_ops)
642 #define ISL29018_PM_OPS NULL
645 static const struct i2c_device_id isl29018_id[] = {
650 MODULE_DEVICE_TABLE(i2c, isl29018_id);
652 static const struct of_device_id isl29018_of_match[] = {
653 { .compatible = "isil,isl29018", },
656 MODULE_DEVICE_TABLE(of, isl29018_of_match);
658 static struct i2c_driver isl29018_driver = {
659 .class = I2C_CLASS_HWMON,
662 .pm = ISL29018_PM_OPS,
663 .owner = THIS_MODULE,
664 .of_match_table = isl29018_of_match,
666 .probe = isl29018_probe,
667 .remove = isl29018_remove,
668 .id_table = isl29018_id,
670 module_i2c_driver(isl29018_driver);
672 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
673 MODULE_LICENSE("GPL");