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 struct isl29018_chip {
61 struct i2c_client *client;
63 unsigned int lux_scale;
67 u8 reg_cache[ISL29018_MAX_REGS];
70 static int isl29018_write_data(struct i2c_client *client, u8 reg,
71 u8 val, u8 mask, u8 shift)
75 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
77 /* don't cache or mask REG_TEST */
78 if (reg < ISL29018_MAX_REGS) {
79 regval = chip->reg_cache[reg];
81 regval |= val << shift;
84 ret = i2c_smbus_write_byte_data(client, reg, regval);
86 dev_err(&client->dev, "Write to device fails status %x\n", ret);
88 /* don't update cache on err */
89 if (reg < ISL29018_MAX_REGS)
90 chip->reg_cache[reg] = regval;
96 static int isl29018_set_range(struct i2c_client *client, unsigned long range,
97 unsigned int *new_range)
99 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
102 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
103 if (range <= supp_ranges[i]) {
104 *new_range = (unsigned int)supp_ranges[i];
109 if (i >= ARRAY_SIZE(supp_ranges))
112 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
113 i, COMMANDII_RANGE_MASK, COMMANDII_RANGE_SHIFT);
116 static int isl29018_set_resolution(struct i2c_client *client,
117 unsigned long adcbit, unsigned int *conf_adc_bit)
119 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
122 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
123 if (adcbit >= supp_adcbit[i]) {
124 *conf_adc_bit = (unsigned int)supp_adcbit[i];
129 if (i >= ARRAY_SIZE(supp_adcbit))
132 return isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
133 i, COMMANDII_RESOLUTION_MASK,
134 COMMANDII_RESOLUTION_SHIFT);
137 static int isl29018_read_sensor_input(struct i2c_client *client, int mode)
144 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1,
145 mode, COMMMAND1_OPMODE_MASK, COMMMAND1_OPMODE_SHIFT);
147 dev_err(&client->dev, "Error in setting operating mode\n");
150 msleep(CONVERSION_TIME_MS);
151 lsb = i2c_smbus_read_byte_data(client, ISL29018_REG_ADD_DATA_LSB);
153 dev_err(&client->dev, "Error in reading LSB DATA\n");
157 msb = i2c_smbus_read_byte_data(client, ISL29018_REG_ADD_DATA_MSB);
159 dev_err(&client->dev, "Error in reading MSB DATA\n");
162 dev_vdbg(&client->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
164 return (msb << 8) | lsb;
167 static int isl29018_read_lux(struct i2c_client *client, int *lux)
170 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
172 lux_data = isl29018_read_sensor_input(client,
173 COMMMAND1_OPMODE_ALS_ONCE);
178 *lux = (lux_data * chip->range * chip->lux_scale) >> chip->adc_bit;
183 static int isl29018_read_ir(struct i2c_client *client, int *ir)
187 ir_data = isl29018_read_sensor_input(client, COMMMAND1_OPMODE_IR_ONCE);
197 static int isl29018_read_proximity_ir(struct i2c_client *client, int scheme,
204 /* Do proximity sensing with required scheme */
205 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMANDII,
206 scheme, COMMANDII_SCHEME_MASK, COMMANDII_SCHEME_SHIFT);
208 dev_err(&client->dev, "Error in setting operating mode\n");
212 prox_data = isl29018_read_sensor_input(client,
213 COMMMAND1_OPMODE_PROX_ONCE);
218 *near_ir = prox_data;
222 ir_data = isl29018_read_sensor_input(client,
223 COMMMAND1_OPMODE_IR_ONCE);
228 if (prox_data >= ir_data)
229 *near_ir = prox_data - ir_data;
236 /* Sysfs interface */
238 static ssize_t show_range(struct device *dev,
239 struct device_attribute *attr, char *buf)
241 struct iio_dev *indio_dev = dev_get_drvdata(dev);
242 struct isl29018_chip *chip = iio_priv(indio_dev);
244 return sprintf(buf, "%u\n", chip->range);
247 static ssize_t store_range(struct device *dev,
248 struct device_attribute *attr, const char *buf, size_t count)
250 struct iio_dev *indio_dev = dev_get_drvdata(dev);
251 struct isl29018_chip *chip = iio_priv(indio_dev);
252 struct i2c_client *client = chip->client;
255 unsigned int new_range;
257 if (strict_strtoul(buf, 10, &lval))
260 if (!(lval == 1000UL || lval == 4000UL ||
261 lval == 16000UL || lval == 64000UL)) {
262 dev_err(dev, "The range is not supported\n");
266 mutex_lock(&chip->lock);
267 status = isl29018_set_range(client, lval, &new_range);
269 mutex_unlock(&chip->lock);
270 dev_err(dev, "Error in setting max range\n");
273 chip->range = new_range;
274 mutex_unlock(&chip->lock);
280 static ssize_t show_resolution(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct iio_dev *indio_dev = dev_get_drvdata(dev);
284 struct isl29018_chip *chip = iio_priv(indio_dev);
286 return sprintf(buf, "%u\n", chip->adc_bit);
289 static ssize_t store_resolution(struct device *dev,
290 struct device_attribute *attr, const char *buf, size_t count)
292 struct iio_dev *indio_dev = dev_get_drvdata(dev);
293 struct isl29018_chip *chip = iio_priv(indio_dev);
294 struct i2c_client *client = chip->client;
297 unsigned int new_adc_bit;
299 if (strict_strtoul(buf, 10, &lval))
301 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
302 dev_err(dev, "The resolution is not supported\n");
306 mutex_lock(&chip->lock);
307 status = isl29018_set_resolution(client, lval, &new_adc_bit);
309 mutex_unlock(&chip->lock);
310 dev_err(dev, "Error in setting resolution\n");
313 chip->adc_bit = new_adc_bit;
314 mutex_unlock(&chip->lock);
319 /* proximity scheme */
320 static ssize_t show_prox_infrared_supression(struct device *dev,
321 struct device_attribute *attr, char *buf)
323 struct iio_dev *indio_dev = dev_get_drvdata(dev);
324 struct isl29018_chip *chip = iio_priv(indio_dev);
326 /* return the "proximity scheme" i.e. if the chip does on chip
327 infrared supression (1 means perform on chip supression) */
328 return sprintf(buf, "%d\n", chip->prox_scheme);
331 static ssize_t store_prox_infrared_supression(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t count)
334 struct iio_dev *indio_dev = dev_get_drvdata(dev);
335 struct isl29018_chip *chip = iio_priv(indio_dev);
338 if (strict_strtoul(buf, 10, &lval))
340 if (!(lval == 0UL || lval == 1UL)) {
341 dev_err(dev, "The mode is not supported\n");
345 /* get the "proximity scheme" i.e. if the chip does on chip
346 infrared supression (1 means perform on chip supression) */
347 mutex_lock(&chip->lock);
348 chip->prox_scheme = (int)lval;
349 mutex_unlock(&chip->lock);
355 static int isl29018_write_raw(struct iio_dev *indio_dev,
356 struct iio_chan_spec const *chan,
361 struct isl29018_chip *chip = iio_priv(indio_dev);
364 mutex_lock(&chip->lock);
365 if (mask == (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) &&
366 chan->type == IIO_LIGHT) {
367 chip->lux_scale = val;
370 mutex_unlock(&chip->lock);
375 static int isl29018_read_raw(struct iio_dev *indio_dev,
376 struct iio_chan_spec const *chan,
382 struct isl29018_chip *chip = iio_priv(indio_dev);
383 struct i2c_client *client = chip->client;
385 mutex_lock(&chip->lock);
388 switch (chan->type) {
390 ret = isl29018_read_lux(client, val);
393 ret = isl29018_read_ir(client, val);
396 ret = isl29018_read_proximity_ir(client,
397 chip->prox_scheme, val);
405 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
406 if (chan->type == IIO_LIGHT) {
407 *val = chip->lux_scale;
414 mutex_unlock(&chip->lock);
418 static const struct iio_chan_spec isl29018_channels[] = {
423 .processed_val = IIO_PROCESSED,
424 .info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE),
426 .type = IIO_INTENSITY,
428 .channel2 = IIO_MOD_LIGHT_IR,
430 /* Unindexed in current ABI. But perhaps it should be. */
431 .type = IIO_PROXIMITY,
435 static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
436 static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
437 static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
438 static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
439 show_resolution, store_resolution, 0);
440 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_supression,
442 show_prox_infrared_supression,
443 store_prox_infrared_supression, 0);
445 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
446 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
447 static struct attribute *isl29018_attributes[] = {
448 ISL29018_DEV_ATTR(range),
449 ISL29018_CONST_ATTR(range_available),
450 ISL29018_DEV_ATTR(adc_resolution),
451 ISL29018_CONST_ATTR(adc_resolution_available),
452 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_supression),
456 static const struct attribute_group isl29108_group = {
457 .attrs = isl29018_attributes,
460 static int isl29018_chip_init(struct i2c_client *client)
462 struct isl29018_chip *chip = iio_priv(i2c_get_clientdata(client));
465 unsigned int new_range;
467 memset(chip->reg_cache, 0, sizeof(chip->reg_cache));
469 /* Code added per Intersil Application Note 1534:
470 * When VDD sinks to approximately 1.8V or below, some of
471 * the part's registers may change their state. When VDD
472 * recovers to 2.25V (or greater), the part may thus be in an
473 * unknown mode of operation. The user can return the part to
474 * a known mode of operation either by (a) setting VDD = 0V for
475 * 1 second or more and then powering back up with a slew rate
476 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
477 * conversions, clear the test registers, and then rewrite all
478 * registers to the desired values.
480 * FOR ISL29011, ISL29018, ISL29021, ISL29023
481 * 1. Write 0x00 to register 0x08 (TEST)
482 * 2. Write 0x00 to register 0x00 (CMD1)
483 * 3. Rewrite all registers to the desired values
485 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
486 * the same thing EXCEPT the data sheet asks for a 1ms delay after
487 * writing the CMD1 register.
489 status = isl29018_write_data(client, ISL29018_REG_TEST, 0,
490 ISL29018_TEST_MASK, ISL29018_TEST_SHIFT);
492 dev_err(&client->dev, "Failed to clear isl29018 TEST reg."
497 /* See Intersil AN1534 comments above.
498 * "Operating Mode" (COMMAND1) register is reprogrammed when
499 * data is read from the device.
501 status = isl29018_write_data(client, ISL29018_REG_ADD_COMMAND1, 0,
504 dev_err(&client->dev, "Failed to clear isl29018 CMD1 reg."
509 msleep(1); /* per data sheet, page 10 */
512 status = isl29018_set_range(client, chip->range, &new_range);
514 dev_err(&client->dev, "Init of isl29018 fails\n");
518 status = isl29018_set_resolution(client, chip->adc_bit,
524 static const struct iio_info isl29108_info = {
525 .attrs = &isl29108_group,
526 .driver_module = THIS_MODULE,
527 .read_raw = &isl29018_read_raw,
528 .write_raw = &isl29018_write_raw,
531 static int __devinit isl29018_probe(struct i2c_client *client,
532 const struct i2c_device_id *id)
534 struct isl29018_chip *chip;
535 struct iio_dev *indio_dev;
538 indio_dev = iio_allocate_device(sizeof(*chip));
539 if (indio_dev == NULL) {
540 dev_err(&client->dev, "iio allocation fails\n");
544 chip = iio_priv(indio_dev);
546 i2c_set_clientdata(client, indio_dev);
547 chip->client = client;
549 mutex_init(&chip->lock);
555 err = isl29018_chip_init(client);
559 indio_dev->info = &isl29108_info;
560 indio_dev->channels = isl29018_channels;
561 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
562 indio_dev->name = id->name;
563 indio_dev->dev.parent = &client->dev;
564 indio_dev->modes = INDIO_DIRECT_MODE;
565 err = iio_device_register(indio_dev);
567 dev_err(&client->dev, "iio registration fails\n");
573 iio_free_device(indio_dev);
578 static int __devexit isl29018_remove(struct i2c_client *client)
580 struct iio_dev *indio_dev = i2c_get_clientdata(client);
582 dev_dbg(&client->dev, "%s()\n", __func__);
583 iio_device_unregister(indio_dev);
584 iio_free_device(indio_dev);
589 static const struct i2c_device_id isl29018_id[] = {
594 MODULE_DEVICE_TABLE(i2c, isl29018_id);
596 static struct i2c_driver isl29018_driver = {
597 .class = I2C_CLASS_HWMON,
600 .owner = THIS_MODULE,
602 .probe = isl29018_probe,
603 .remove = __devexit_p(isl29018_remove),
604 .id_table = isl29018_id,
607 static int __init isl29018_init(void)
609 return i2c_add_driver(&isl29018_driver);
612 static void __exit isl29018_exit(void)
614 i2c_del_driver(&isl29018_driver);
617 module_init(isl29018_init);
618 module_exit(isl29018_exit);
620 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
621 MODULE_LICENSE("GPL");