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/slab.h>
33 #define ISL29018_REG_ADD_COMMAND1 0x00
34 #define COMMMAND1_OPMODE_SHIFT 5
35 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
36 #define COMMMAND1_OPMODE_POWER_DOWN 0
37 #define COMMMAND1_OPMODE_ALS_ONCE 1
38 #define COMMMAND1_OPMODE_IR_ONCE 2
39 #define COMMMAND1_OPMODE_PROX_ONCE 3
41 #define ISL29018_REG_ADD_COMMANDII 0x01
42 #define COMMANDII_RESOLUTION_SHIFT 2
43 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
45 #define COMMANDII_RANGE_SHIFT 0
46 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
48 #define COMMANDII_SCHEME_SHIFT 7
49 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
51 #define ISL29018_REG_ADD_DATA_LSB 0x02
52 #define ISL29018_REG_ADD_DATA_MSB 0x03
53 #define ISL29018_MAX_REGS (ISL29018_REG_ADD_DATA_MSB+1)
55 #define ISL29018_REG_TEST 0x08
56 #define ISL29018_TEST_SHIFT 0
57 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
59 #define ISL29018_REG_TEST 0x08
60 #define ISL29018_TEST_SHIFT 0
61 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
63 struct isl29018_chip {
64 struct i2c_client *client;
66 unsigned int lux_scale;
70 u8 reg_cache[ISL29018_MAX_REGS];
73 static int isl29018_write_data(struct i2c_client *client, u8 reg,
74 u8 val, u8 mask, u8 shift)
78 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
80 /* don't cache or mask REG_TEST */
81 if (reg < ISL29018_MAX_REGS) {
82 regval = chip->reg_cache[reg];
84 regval |= val << shift;
87 ret = i2c_smbus_write_byte_data(client, reg, regval);
89 dev_err(&client->dev, "Write to device fails status %x\n", ret);
91 /* don't update cache on err */
92 if (reg < ISL29018_MAX_REGS)
93 chip->reg_cache[reg] = regval;
99 static int isl29018_set_range(struct i2c_client *client, unsigned long range,
100 unsigned int *new_range)
102 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
105 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
106 if (range <= supp_ranges[i]) {
107 *new_range = (unsigned int)supp_ranges[i];
112 if (i >= ARRAY_SIZE(supp_ranges))
115 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
116 i, COMMANDII_RANGE_MASK, COMMANDII_RANGE_SHIFT);
119 static int isl29018_set_resolution(struct i2c_client *client,
120 unsigned long adcbit, unsigned int *conf_adc_bit)
122 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
125 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
126 if (adcbit >= supp_adcbit[i]) {
127 *conf_adc_bit = (unsigned int)supp_adcbit[i];
132 if (i >= ARRAY_SIZE(supp_adcbit))
135 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
136 i, COMMANDII_RESOLUTION_MASK,
137 COMMANDII_RESOLUTION_SHIFT);
140 static void integration_wait(struct isl29018_chip *chip)
144 /* Integration and conversion time is described in the data sheet
145 * as 2^n x (constant), and for 16 bits it's 90ms. So calculate
148 * Since the time taken depends on a resistor value, leave some
149 * margin above the 90ms timeout, otherwise we risk reading a
152 wait_time = DIV_ROUND_UP(100, 1 << (16 - chip->adc_bit));
156 static int isl29018_read_sensor_input(struct i2c_client *client, int mode)
162 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1,
163 mode, COMMMAND1_OPMODE_MASK, COMMMAND1_OPMODE_SHIFT);
165 dev_err(&client->dev, "Error in setting operating mode\n");
168 integration_wait(iio_priv(i2c_get_clientdata(client)));
169 status = i2c_smbus_read_word_data(client, ISL29018_REG_ADD_DATA_LSB);
171 dev_err(&client->dev, "Error in reading Lux DATA\n");
175 lux = le16_to_cpu(status);
176 dev_vdbg(&client->dev, "lux = %u\n", lux);
181 static int isl29018_read_lux(struct i2c_client *client, int *lux)
184 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
186 lux_data = isl29018_read_sensor_input(client,
187 COMMMAND1_OPMODE_ALS_ONCE);
192 *lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
197 static int isl29018_read_ir(struct i2c_client *client, int *ir)
201 ir_data = isl29018_read_sensor_input(client, COMMMAND1_OPMODE_IR_ONCE);
211 static int isl29018_read_proximity_ir(struct i2c_client *client, int scheme,
218 /* Do proximity sensing with required scheme */
219 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
220 scheme, COMMANDII_SCHEME_MASK, COMMANDII_SCHEME_SHIFT);
222 dev_err(&client->dev, "Error in setting operating mode\n");
226 prox_data = isl29018_read_sensor_input(client,
227 COMMMAND1_OPMODE_PROX_ONCE);
232 *near_ir = prox_data;
236 ir_data = isl29018_read_sensor_input(client,
237 COMMMAND1_OPMODE_IR_ONCE);
242 if (prox_data >= ir_data)
243 *near_ir = prox_data - ir_data;
250 /* Sysfs interface */
252 static ssize_t show_lux_scale(struct device *dev,
253 struct device_attribute *attr, char *buf)
255 struct iio_dev *indio_dev = dev_get_drvdata(dev);
256 struct isl29018_chip *chip = iio_priv(indio_dev);
258 return sprintf(buf, "%d\n", chip->lux_scale);
261 static ssize_t store_lux_scale(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t count)
264 struct iio_dev *indio_dev = dev_get_drvdata(dev);
265 struct isl29018_chip *chip = iio_priv(indio_dev);
268 lval = simple_strtoul(buf, NULL, 10);
272 mutex_lock(&chip->lock);
273 chip->lux_scale = lval;
274 mutex_unlock(&chip->lock);
279 static ssize_t get_sensor_data(struct device *dev, char *buf, int mode)
281 struct iio_dev *indio_dev = dev_get_drvdata(dev);
282 struct isl29018_chip *chip = iio_priv(indio_dev);
283 struct i2c_client *client = chip->client;
287 mutex_lock(&chip->lock);
289 case COMMMAND1_OPMODE_PROX_ONCE:
290 status = isl29018_read_proximity_ir(client,
291 chip->prox_scheme, &value);
294 case COMMMAND1_OPMODE_ALS_ONCE:
295 status = isl29018_read_lux(client, &value);
298 case COMMMAND1_OPMODE_IR_ONCE:
299 status = isl29018_read_ir(client, &value);
303 dev_err(&client->dev, "Mode %d is not supported\n", mode);
304 mutex_unlock(&chip->lock);
309 dev_err(&client->dev, "Error in Reading data");
310 mutex_unlock(&chip->lock);
314 mutex_unlock(&chip->lock);
316 return sprintf(buf, "%d\n", value);
321 static ssize_t show_lux(struct device *dev,
322 struct device_attribute *devattr, char *buf)
324 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_ALS_ONCE);
328 static ssize_t show_ir(struct device *dev,
329 struct device_attribute *devattr, char *buf)
331 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_IR_ONCE);
334 /* Read nearest ir */
335 static ssize_t show_proxim_ir(struct device *dev,
336 struct device_attribute *devattr, char *buf)
338 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_PROX_ONCE);
342 static ssize_t show_range(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 struct iio_dev *indio_dev = dev_get_drvdata(dev);
346 struct isl29018_chip *chip = iio_priv(indio_dev);
348 return sprintf(buf, "%u\n", chip->range);
351 static ssize_t store_range(struct device *dev,
352 struct device_attribute *attr, const char *buf, size_t count)
354 struct iio_dev *indio_dev = dev_get_drvdata(dev);
355 struct isl29018_chip *chip = iio_priv(indio_dev);
356 struct i2c_client *client = chip->client;
359 unsigned int new_range;
361 if (strict_strtoul(buf, 10, &lval))
364 if (!(lval == 1000UL || lval == 4000UL ||
365 lval == 16000UL || lval == 64000UL)) {
366 dev_err(dev, "The range is not supported\n");
370 mutex_lock(&chip->lock);
371 status = isl29018_set_range(client, lval, &new_range);
373 mutex_unlock(&chip->lock);
374 dev_err(dev, "Error in setting max range\n");
377 chip->range = new_range;
378 mutex_unlock(&chip->lock);
384 static ssize_t show_resolution(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct iio_dev *indio_dev = dev_get_drvdata(dev);
388 struct isl29018_chip *chip = iio_priv(indio_dev);
390 return sprintf(buf, "%u\n", chip->adc_bit);
393 static ssize_t store_resolution(struct device *dev,
394 struct device_attribute *attr, const char *buf, size_t count)
396 struct iio_dev *indio_dev = dev_get_drvdata(dev);
397 struct isl29018_chip *chip = iio_priv(indio_dev);
398 struct i2c_client *client = chip->client;
401 unsigned int new_adc_bit;
403 if (strict_strtoul(buf, 10, &lval))
405 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
406 dev_err(dev, "The resolution is not supported\n");
410 mutex_lock(&chip->lock);
411 status = isl29018_set_resolution(client, lval, &new_adc_bit);
413 mutex_unlock(&chip->lock);
414 dev_err(dev, "Error in setting resolution\n");
417 chip->adc_bit = new_adc_bit;
418 mutex_unlock(&chip->lock);
423 /* proximity scheme */
424 static ssize_t show_prox_infrared_supression(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct iio_dev *indio_dev = dev_get_drvdata(dev);
428 struct isl29018_chip *chip = iio_priv(indio_dev);
430 /* return the "proximity scheme" i.e. if the chip does on chip
431 infrared supression (1 means perform on chip supression) */
432 return sprintf(buf, "%d\n", chip->prox_scheme);
435 static ssize_t store_prox_infrared_supression(struct device *dev,
436 struct device_attribute *attr, const char *buf, size_t count)
438 struct iio_dev *indio_dev = dev_get_drvdata(dev);
439 struct isl29018_chip *chip = iio_priv(indio_dev);
442 if (strict_strtoul(buf, 10, &lval))
444 if (!(lval == 0UL || lval == 1UL)) {
445 dev_err(dev, "The mode is not supported\n");
449 /* get the "proximity scheme" i.e. if the chip does on chip
450 infrared supression (1 means perform on chip supression) */
451 mutex_lock(&chip->lock);
452 chip->prox_scheme = (int)lval;
453 mutex_unlock(&chip->lock);
459 static int isl29018_write_raw(struct iio_dev *indio_dev,
460 struct iio_chan_spec const *chan,
465 struct isl29018_chip *chip = iio_priv(indio_dev);
468 mutex_lock(&chip->lock);
469 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
470 chip->lux_scale = val;
473 mutex_unlock(&chip->lock);
478 static int isl29018_read_raw(struct iio_dev *indio_dev,
479 struct iio_chan_spec const *chan,
485 struct isl29018_chip *chip = iio_priv(indio_dev);
486 struct i2c_client *client = chip->client;
488 mutex_lock(&chip->lock);
491 switch (chan->type) {
493 ret = isl29018_read_lux(client, val);
496 ret = isl29018_read_ir(client, val);
499 ret = isl29018_read_proximity_ir(client,
500 chip->prox_scheme, val);
508 case IIO_CHAN_INFO_CALIBSCALE:
509 if (chan->type == IIO_LIGHT) {
510 *val = chip->lux_scale;
517 mutex_unlock(&chip->lock);
521 static const struct iio_chan_spec isl29018_channels[] = {
526 .processed_val = IIO_PROCESSED,
527 .info_mask = IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
529 .type = IIO_INTENSITY,
531 .channel2 = IIO_MOD_LIGHT_IR,
533 /* Unindexed in current ABI. But perhaps it should be. */
534 .type = IIO_PROXIMITY,
538 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
539 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
540 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
541 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
542 show_resolution, store_resolution, 0);
543 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_supression,
545 show_prox_infrared_supression,
546 store_prox_infrared_supression, 0);
547 static IIO_DEVICE_ATTR(illuminance0_input, S_IRUGO, show_lux, NULL, 0);
548 static IIO_DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
549 show_lux_scale, store_lux_scale, 0);
550 static IIO_DEVICE_ATTR(intensity_infrared_raw, S_IRUGO, show_ir, NULL, 0);
551 static IIO_DEVICE_ATTR(proximity_raw, S_IRUGO, show_proxim_ir, NULL, 0);
553 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
554 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
555 static struct attribute *isl29018_attributes[] = {
556 ISL29018_DEV_ATTR(range),
557 ISL29018_CONST_ATTR(range_available),
558 ISL29018_DEV_ATTR(adc_resolution),
559 ISL29018_CONST_ATTR(adc_resolution_available),
560 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_supression),
561 ISL29018_DEV_ATTR(illuminance0_input),
562 ISL29018_DEV_ATTR(illuminance0_calibscale),
563 ISL29018_DEV_ATTR(intensity_infrared_raw),
564 ISL29018_DEV_ATTR(proximity_raw),
568 static const struct attribute_group isl29108_group = {
569 .attrs = isl29018_attributes,
572 static int isl29018_chip_init(struct i2c_client *client)
574 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
577 unsigned int new_range;
579 memset(chip->reg_cache, 0, sizeof(chip->reg_cache));
581 /* Code added per Intersil Application Note 1534:
582 * When VDD sinks to approximately 1.8V or below, some of
583 * the part's registers may change their state. When VDD
584 * recovers to 2.25V (or greater), the part may thus be in an
585 * unknown mode of operation. The user can return the part to
586 * a known mode of operation either by (a) setting VDD = 0V for
587 * 1 second or more and then powering back up with a slew rate
588 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
589 * conversions, clear the test registers, and then rewrite all
590 * registers to the desired values.
592 * FOR ISL29011, ISL29018, ISL29021, ISL29023
593 * 1. Write 0x00 to register 0x08 (TEST)
594 * 2. Write 0x00 to register 0x00 (CMD1)
595 * 3. Rewrite all registers to the desired values
597 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
598 * the same thing EXCEPT the data sheet asks for a 1ms delay after
599 * writing the CMD1 register.
601 status = isl29018_write_data(client, ISL29018_REG_TEST, 0,
602 ISL29018_TEST_MASK, ISL29018_TEST_SHIFT);
604 dev_err(&client->dev, "Failed to clear isl29018 TEST reg."
609 /* See Intersil AN1534 comments above.
610 * "Operating Mode" (COMMAND1) register is reprogrammed when
611 * data is read from the device.
613 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1, 0,
616 dev_err(&client->dev, "Failed to clear isl29018 CMD1 reg."
621 msleep(1); /* per data sheet, page 10 */
624 status = isl29018_set_range(client, chip->range, &new_range);
626 dev_err(&client->dev, "Init of isl29018 fails\n");
630 status = isl29018_set_resolution(client, chip->adc_bit,
636 static const struct iio_info isl29108_info = {
637 .attrs = &isl29108_group,
638 .driver_module = THIS_MODULE,
639 .read_raw = &isl29018_read_raw,
640 .write_raw = &isl29018_write_raw,
643 static int __devinit isl29018_probe(struct i2c_client *client,
644 const struct i2c_device_id *id)
646 struct isl29018_chip *chip;
647 struct iio_dev *indio_dev;
650 indio_dev = iio_allocate_device(sizeof(*chip));
651 if (indio_dev == NULL) {
652 dev_err(&client->dev, "iio allocation fails\n");
656 chip = iio_priv(indio_dev);
658 i2c_set_clientdata(client, indio_dev);
659 chip->client = client;
661 mutex_init(&chip->lock);
667 err = isl29018_chip_init(client);
671 indio_dev->info = &isl29108_info;
672 indio_dev->channels = isl29018_channels;
673 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
674 indio_dev->name = id->name;
675 indio_dev->dev.parent = &client->dev;
676 indio_dev->modes = INDIO_DIRECT_MODE;
677 err = iio_device_register(indio_dev);
679 dev_err(&client->dev, "iio registration fails\n");
685 iio_free_device(indio_dev);
690 static int __devexit isl29018_remove(struct i2c_client *client)
692 struct iio_dev *indio_dev = i2c_get_clientdata(client);
694 dev_dbg(&client->dev, "%s()\n", __func__);
695 iio_device_unregister(indio_dev);
696 iio_free_device(indio_dev);
701 static const struct i2c_device_id isl29018_id[] = {
706 MODULE_DEVICE_TABLE(i2c, isl29018_id);
708 static const struct of_device_id isl29018_of_match[] = {
709 { .compatible = "invn,isl29018", },
712 MODULE_DEVICE_TABLE(of, isl29018_of_match);
714 static struct i2c_driver isl29018_driver = {
715 .class = I2C_CLASS_HWMON,
718 .owner = THIS_MODULE,
719 .of_match_table = isl29018_of_match,
721 .probe = isl29018_probe,
722 .remove = __devexit_p(isl29018_remove),
723 .id_table = isl29018_id,
725 module_i2c_driver(isl29018_driver);
727 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
728 MODULE_LICENSE("GPL");