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>
32 #define CONVERSION_TIME_MS 100
34 #define ISL29018_REG_ADD_COMMAND1 0x00
35 #define COMMMAND1_OPMODE_SHIFT 5
36 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
37 #define COMMMAND1_OPMODE_POWER_DOWN 0
38 #define COMMMAND1_OPMODE_ALS_ONCE 1
39 #define COMMMAND1_OPMODE_IR_ONCE 2
40 #define COMMMAND1_OPMODE_PROX_ONCE 3
42 #define ISL29018_REG_ADD_COMMANDII 0x01
43 #define COMMANDII_RESOLUTION_SHIFT 2
44 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
46 #define COMMANDII_RANGE_SHIFT 0
47 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
49 #define COMMANDII_SCHEME_SHIFT 7
50 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
52 #define ISL29018_REG_ADD_DATA_LSB 0x02
53 #define ISL29018_REG_ADD_DATA_MSB 0x03
54 #define ISL29018_MAX_REGS (ISL29018_REG_ADD_DATA_MSB+1)
56 #define ISL29018_REG_TEST 0x08
57 #define ISL29018_TEST_SHIFT 0
58 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
60 #define ISL29018_REG_TEST 0x08
61 #define ISL29018_TEST_SHIFT 0
62 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
64 struct isl29018_chip {
65 struct i2c_client *client;
67 unsigned int lux_scale;
71 u8 reg_cache[ISL29018_MAX_REGS];
74 static int isl29018_write_data(struct i2c_client *client, u8 reg,
75 u8 val, u8 mask, u8 shift)
79 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
81 /* don't cache or mask REG_TEST */
82 if (reg < ISL29018_MAX_REGS) {
83 regval = chip->reg_cache[reg];
85 regval |= val << shift;
88 ret = i2c_smbus_write_byte_data(client, reg, regval);
90 dev_err(&client->dev, "Write to device fails status %x\n", ret);
92 /* don't update cache on err */
93 if (reg < ISL29018_MAX_REGS)
94 chip->reg_cache[reg] = regval;
100 static int isl29018_set_range(struct i2c_client *client, unsigned long range,
101 unsigned int *new_range)
103 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
106 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
107 if (range <= supp_ranges[i]) {
108 *new_range = (unsigned int)supp_ranges[i];
113 if (i >= ARRAY_SIZE(supp_ranges))
116 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
117 i, COMMANDII_RANGE_MASK, COMMANDII_RANGE_SHIFT);
120 static int isl29018_set_resolution(struct i2c_client *client,
121 unsigned long adcbit, unsigned int *conf_adc_bit)
123 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
126 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
127 if (adcbit >= supp_adcbit[i]) {
128 *conf_adc_bit = (unsigned int)supp_adcbit[i];
133 if (i >= ARRAY_SIZE(supp_adcbit))
136 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
137 i, COMMANDII_RESOLUTION_MASK,
138 COMMANDII_RESOLUTION_SHIFT);
141 static int isl29018_read_sensor_input(struct i2c_client *client, int mode)
147 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1,
148 mode, COMMMAND1_OPMODE_MASK, COMMMAND1_OPMODE_SHIFT);
150 dev_err(&client->dev, "Error in setting operating mode\n");
153 msleep(CONVERSION_TIME_MS);
154 status = i2c_smbus_read_word_data(client, ISL29018_REG_ADD_DATA_LSB);
156 dev_err(&client->dev, "Error in reading Lux DATA\n");
160 lux = le16_to_cpu(status);
161 dev_vdbg(&client->dev, "lux = %u\n", lux);
166 static int isl29018_read_lux(struct i2c_client *client, int *lux)
169 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
171 lux_data = isl29018_read_sensor_input(client,
172 COMMMAND1_OPMODE_ALS_ONCE);
177 *lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
182 static int isl29018_read_ir(struct i2c_client *client, int *ir)
186 ir_data = isl29018_read_sensor_input(client, COMMMAND1_OPMODE_IR_ONCE);
196 static int isl29018_read_proximity_ir(struct i2c_client *client, int scheme,
203 /* Do proximity sensing with required scheme */
204 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
205 scheme, COMMANDII_SCHEME_MASK, COMMANDII_SCHEME_SHIFT);
207 dev_err(&client->dev, "Error in setting operating mode\n");
211 prox_data = isl29018_read_sensor_input(client,
212 COMMMAND1_OPMODE_PROX_ONCE);
217 *near_ir = prox_data;
221 ir_data = isl29018_read_sensor_input(client,
222 COMMMAND1_OPMODE_IR_ONCE);
227 if (prox_data >= ir_data)
228 *near_ir = prox_data - ir_data;
235 /* Sysfs interface */
237 static ssize_t show_lux_scale(struct device *dev,
238 struct device_attribute *attr, char *buf)
240 struct iio_dev *indio_dev = dev_get_drvdata(dev);
241 struct isl29018_chip *chip = iio_priv(indio_dev);
243 return sprintf(buf, "%d\n", chip->lux_scale);
246 static ssize_t store_lux_scale(struct device *dev,
247 struct device_attribute *attr, const char *buf, size_t count)
249 struct iio_dev *indio_dev = dev_get_drvdata(dev);
250 struct isl29018_chip *chip = iio_priv(indio_dev);
253 lval = simple_strtoul(buf, NULL, 10);
257 mutex_lock(&chip->lock);
258 chip->lux_scale = lval;
259 mutex_unlock(&chip->lock);
264 static ssize_t get_sensor_data(struct device *dev, char *buf, int mode)
266 struct iio_dev *indio_dev = dev_get_drvdata(dev);
267 struct isl29018_chip *chip = iio_priv(indio_dev);
268 struct i2c_client *client = chip->client;
272 mutex_lock(&chip->lock);
274 case COMMMAND1_OPMODE_PROX_ONCE:
275 status = isl29018_read_proximity_ir(client,
276 chip->prox_scheme, &value);
279 case COMMMAND1_OPMODE_ALS_ONCE:
280 status = isl29018_read_lux(client, &value);
283 case COMMMAND1_OPMODE_IR_ONCE:
284 status = isl29018_read_ir(client, &value);
288 dev_err(&client->dev, "Mode %d is not supported\n", mode);
289 mutex_unlock(&chip->lock);
294 dev_err(&client->dev, "Error in Reading data");
295 mutex_unlock(&chip->lock);
299 mutex_unlock(&chip->lock);
301 return sprintf(buf, "%d\n", value);
306 static ssize_t show_lux(struct device *dev,
307 struct device_attribute *devattr, char *buf)
309 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_ALS_ONCE);
313 static ssize_t show_ir(struct device *dev,
314 struct device_attribute *devattr, char *buf)
316 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_IR_ONCE);
319 /* Read nearest ir */
320 static ssize_t show_proxim_ir(struct device *dev,
321 struct device_attribute *devattr, char *buf)
323 return get_sensor_data(dev, buf, COMMMAND1_OPMODE_PROX_ONCE);
327 static ssize_t show_range(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct iio_dev *indio_dev = dev_get_drvdata(dev);
331 struct isl29018_chip *chip = iio_priv(indio_dev);
333 return sprintf(buf, "%u\n", chip->range);
336 static ssize_t store_range(struct device *dev,
337 struct device_attribute *attr, const char *buf, size_t count)
339 struct iio_dev *indio_dev = dev_get_drvdata(dev);
340 struct isl29018_chip *chip = iio_priv(indio_dev);
341 struct i2c_client *client = chip->client;
344 unsigned int new_range;
346 if (strict_strtoul(buf, 10, &lval))
349 if (!(lval == 1000UL || lval == 4000UL ||
350 lval == 16000UL || lval == 64000UL)) {
351 dev_err(dev, "The range is not supported\n");
355 mutex_lock(&chip->lock);
356 status = isl29018_set_range(client, lval, &new_range);
358 mutex_unlock(&chip->lock);
359 dev_err(dev, "Error in setting max range\n");
362 chip->range = new_range;
363 mutex_unlock(&chip->lock);
369 static ssize_t show_resolution(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 struct iio_dev *indio_dev = dev_get_drvdata(dev);
373 struct isl29018_chip *chip = iio_priv(indio_dev);
375 return sprintf(buf, "%u\n", chip->adc_bit);
378 static ssize_t store_resolution(struct device *dev,
379 struct device_attribute *attr, const char *buf, size_t count)
381 struct iio_dev *indio_dev = dev_get_drvdata(dev);
382 struct isl29018_chip *chip = iio_priv(indio_dev);
383 struct i2c_client *client = chip->client;
386 unsigned int new_adc_bit;
388 if (strict_strtoul(buf, 10, &lval))
390 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
391 dev_err(dev, "The resolution is not supported\n");
395 mutex_lock(&chip->lock);
396 status = isl29018_set_resolution(client, lval, &new_adc_bit);
398 mutex_unlock(&chip->lock);
399 dev_err(dev, "Error in setting resolution\n");
402 chip->adc_bit = new_adc_bit;
403 mutex_unlock(&chip->lock);
408 /* proximity scheme */
409 static ssize_t show_prox_infrared_supression(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct iio_dev *indio_dev = dev_get_drvdata(dev);
413 struct isl29018_chip *chip = iio_priv(indio_dev);
415 /* return the "proximity scheme" i.e. if the chip does on chip
416 infrared supression (1 means perform on chip supression) */
417 return sprintf(buf, "%d\n", chip->prox_scheme);
420 static ssize_t store_prox_infrared_supression(struct device *dev,
421 struct device_attribute *attr, const char *buf, size_t count)
423 struct iio_dev *indio_dev = dev_get_drvdata(dev);
424 struct isl29018_chip *chip = iio_priv(indio_dev);
427 if (strict_strtoul(buf, 10, &lval))
429 if (!(lval == 0UL || lval == 1UL)) {
430 dev_err(dev, "The mode is not supported\n");
434 /* get the "proximity scheme" i.e. if the chip does on chip
435 infrared supression (1 means perform on chip supression) */
436 mutex_lock(&chip->lock);
437 chip->prox_scheme = (int)lval;
438 mutex_unlock(&chip->lock);
444 static int isl29018_write_raw(struct iio_dev *indio_dev,
445 struct iio_chan_spec const *chan,
450 struct isl29018_chip *chip = iio_priv(indio_dev);
453 mutex_lock(&chip->lock);
454 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
455 chip->lux_scale = val;
458 mutex_unlock(&chip->lock);
463 static int isl29018_read_raw(struct iio_dev *indio_dev,
464 struct iio_chan_spec const *chan,
470 struct isl29018_chip *chip = iio_priv(indio_dev);
471 struct i2c_client *client = chip->client;
473 mutex_lock(&chip->lock);
476 switch (chan->type) {
478 ret = isl29018_read_lux(client, val);
481 ret = isl29018_read_ir(client, val);
484 ret = isl29018_read_proximity_ir(client,
485 chip->prox_scheme, val);
493 case IIO_CHAN_INFO_CALIBSCALE:
494 if (chan->type == IIO_LIGHT) {
495 *val = chip->lux_scale;
502 mutex_unlock(&chip->lock);
506 static const struct iio_chan_spec isl29018_channels[] = {
511 .processed_val = IIO_PROCESSED,
512 .info_mask = IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
514 .type = IIO_INTENSITY,
516 .channel2 = IIO_MOD_LIGHT_IR,
518 /* Unindexed in current ABI. But perhaps it should be. */
519 .type = IIO_PROXIMITY,
523 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
524 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
525 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
526 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
527 show_resolution, store_resolution, 0);
528 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_supression,
530 show_prox_infrared_supression,
531 store_prox_infrared_supression, 0);
532 static IIO_DEVICE_ATTR(illuminance0_input, S_IRUGO, show_lux, NULL, 0);
533 static IIO_DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR,
534 show_lux_scale, store_lux_scale, 0);
535 static IIO_DEVICE_ATTR(intensity_infrared_raw, S_IRUGO, show_ir, NULL, 0);
536 static IIO_DEVICE_ATTR(proximity_raw, S_IRUGO, show_proxim_ir, NULL, 0);
538 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
539 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
540 static struct attribute *isl29018_attributes[] = {
541 ISL29018_DEV_ATTR(range),
542 ISL29018_CONST_ATTR(range_available),
543 ISL29018_DEV_ATTR(adc_resolution),
544 ISL29018_CONST_ATTR(adc_resolution_available),
545 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_supression),
546 ISL29018_DEV_ATTR(illuminance0_input),
547 ISL29018_DEV_ATTR(illuminance0_calibscale),
548 ISL29018_DEV_ATTR(intensity_infrared_raw),
549 ISL29018_DEV_ATTR(proximity_raw),
553 static const struct attribute_group isl29108_group = {
554 .attrs = isl29018_attributes,
557 static int isl29018_chip_init(struct i2c_client *client)
559 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
562 unsigned int new_range;
564 memset(chip->reg_cache, 0, sizeof(chip->reg_cache));
566 /* Code added per Intersil Application Note 1534:
567 * When VDD sinks to approximately 1.8V or below, some of
568 * the part's registers may change their state. When VDD
569 * recovers to 2.25V (or greater), the part may thus be in an
570 * unknown mode of operation. The user can return the part to
571 * a known mode of operation either by (a) setting VDD = 0V for
572 * 1 second or more and then powering back up with a slew rate
573 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
574 * conversions, clear the test registers, and then rewrite all
575 * registers to the desired values.
577 * FOR ISL29011, ISL29018, ISL29021, ISL29023
578 * 1. Write 0x00 to register 0x08 (TEST)
579 * 2. Write 0x00 to register 0x00 (CMD1)
580 * 3. Rewrite all registers to the desired values
582 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
583 * the same thing EXCEPT the data sheet asks for a 1ms delay after
584 * writing the CMD1 register.
586 status = isl29018_write_data(client, ISL29018_REG_TEST, 0,
587 ISL29018_TEST_MASK, ISL29018_TEST_SHIFT);
589 dev_err(&client->dev, "Failed to clear isl29018 TEST reg."
594 /* See Intersil AN1534 comments above.
595 * "Operating Mode" (COMMAND1) register is reprogrammed when
596 * data is read from the device.
598 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1, 0,
601 dev_err(&client->dev, "Failed to clear isl29018 CMD1 reg."
606 msleep(1); /* per data sheet, page 10 */
609 status = isl29018_set_range(client, chip->range, &new_range);
611 dev_err(&client->dev, "Init of isl29018 fails\n");
615 status = isl29018_set_resolution(client, chip->adc_bit,
621 static const struct iio_info isl29108_info = {
622 .attrs = &isl29108_group,
623 .driver_module = THIS_MODULE,
624 .read_raw = &isl29018_read_raw,
625 .write_raw = &isl29018_write_raw,
628 static int __devinit isl29018_probe(struct i2c_client *client,
629 const struct i2c_device_id *id)
631 struct isl29018_chip *chip;
632 struct iio_dev *indio_dev;
635 indio_dev = iio_allocate_device(sizeof(*chip));
636 if (indio_dev == NULL) {
637 dev_err(&client->dev, "iio allocation fails\n");
641 chip = iio_priv(indio_dev);
643 i2c_set_clientdata(client, indio_dev);
644 chip->client = client;
646 mutex_init(&chip->lock);
652 err = isl29018_chip_init(client);
656 indio_dev->info = &isl29108_info;
657 indio_dev->channels = isl29018_channels;
658 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
659 indio_dev->name = id->name;
660 indio_dev->dev.parent = &client->dev;
661 indio_dev->modes = INDIO_DIRECT_MODE;
662 err = iio_device_register(indio_dev);
664 dev_err(&client->dev, "iio registration fails\n");
670 iio_free_device(indio_dev);
675 static int __devexit isl29018_remove(struct i2c_client *client)
677 struct iio_dev *indio_dev = i2c_get_clientdata(client);
679 dev_dbg(&client->dev, "%s()\n", __func__);
680 iio_device_unregister(indio_dev);
681 iio_free_device(indio_dev);
686 static const struct i2c_device_id isl29018_id[] = {
691 MODULE_DEVICE_TABLE(i2c, isl29018_id);
693 static const struct of_device_id isl29018_of_match[] = {
694 { .compatible = "invn,isl29018", },
697 MODULE_DEVICE_TABLE(of, isl29018_of_match);
699 static struct i2c_driver isl29018_driver = {
700 .class = I2C_CLASS_HWMON,
703 .owner = THIS_MODULE,
704 .of_match_table = isl29018_of_match,
706 .probe = isl29018_probe,
707 .remove = __devexit_p(isl29018_remove),
708 .id_table = isl29018_id,
710 module_i2c_driver(isl29018_driver);
712 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
713 MODULE_LICENSE("GPL");