2 * IIO driver for the light sensor ISL29028.
3 * ISL29028 is Concurrent Ambient Light and Proximity Sensor
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/regmap.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 #define ISL29028_CONV_TIME_MS 100
32 #define ISL29028_REG_CONFIGURE 0x01
34 #define ISL29028_CONF_ALS_IR_MODE_ALS 0
35 #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
36 #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
38 #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
39 #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
40 #define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
42 #define ISL29028_CONF_ALS_DIS 0
43 #define ISL29028_CONF_ALS_EN BIT(2)
44 #define ISL29028_CONF_ALS_EN_MASK BIT(2)
46 #define ISL29028_CONF_PROX_SLP_SH 4
47 #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
49 #define ISL29028_CONF_PROX_EN BIT(7)
50 #define ISL29028_CONF_PROX_EN_MASK BIT(7)
52 #define ISL29028_REG_INTERRUPT 0x02
54 #define ISL29028_REG_PROX_DATA 0x08
55 #define ISL29028_REG_ALSIR_L 0x09
56 #define ISL29028_REG_ALSIR_U 0x0A
58 #define ISL29028_REG_TEST1_MODE 0x0E
59 #define ISL29028_REG_TEST2_MODE 0x0F
61 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
63 enum isl29028_als_ir_mode {
64 ISL29028_MODE_NONE = 0,
69 struct isl29028_chip {
71 struct regmap *regmap;
73 unsigned int prox_sampling;
77 enum isl29028_als_ir_mode als_ir_mode;
80 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
81 unsigned int sampling)
83 static unsigned int prox_period[] = {800, 400, 200, 100, 75, 50, 12, 0};
85 unsigned int period = DIV_ROUND_UP(1000, sampling);
87 for (sel = 0; sel < ARRAY_SIZE(prox_period); ++sel) {
88 if (period >= prox_period[sel])
91 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
92 ISL29028_CONF_PROX_SLP_MASK,
93 sel << ISL29028_CONF_PROX_SLP_SH);
96 static int isl29028_enable_proximity(struct isl29028_chip *chip, bool enable)
102 val = ISL29028_CONF_PROX_EN;
103 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
104 ISL29028_CONF_PROX_EN_MASK, val);
108 /* Wait for conversion to be complete for first sample */
109 mdelay(DIV_ROUND_UP(1000, chip->prox_sampling));
113 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
115 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
116 ISL29028_CONF_ALS_RANGE_LOW_LUX;
118 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
119 ISL29028_CONF_ALS_RANGE_MASK, val);
122 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
123 enum isl29028_als_ir_mode mode)
128 case ISL29028_MODE_ALS:
129 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
130 ISL29028_CONF_ALS_IR_MODE_MASK,
131 ISL29028_CONF_ALS_IR_MODE_ALS);
135 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
136 ISL29028_CONF_ALS_RANGE_MASK,
137 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
140 case ISL29028_MODE_IR:
141 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
142 ISL29028_CONF_ALS_IR_MODE_MASK,
143 ISL29028_CONF_ALS_IR_MODE_IR);
146 case ISL29028_MODE_NONE:
147 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
148 ISL29028_CONF_ALS_EN_MASK, ISL29028_CONF_ALS_DIS);
154 /* Enable the ALS/IR */
155 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
156 ISL29028_CONF_ALS_EN_MASK,
157 ISL29028_CONF_ALS_EN);
161 /* Need to wait for conversion time if ALS/IR mode enabled */
162 mdelay(ISL29028_CONV_TIME_MS);
166 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
168 struct device *dev = regmap_get_device(chip->regmap);
173 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
176 "Error in reading register ALSIR_L err %d\n", ret);
180 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
183 "Error in reading register ALSIR_U err %d\n", ret);
187 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
191 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
193 struct device *dev = regmap_get_device(chip->regmap);
197 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
199 dev_err(dev, "Error in reading register %d, error %d\n",
200 ISL29028_REG_PROX_DATA, ret);
207 static int isl29028_proxim_get(struct isl29028_chip *chip, int *prox_data)
211 if (!chip->enable_prox) {
212 ret = isl29028_enable_proximity(chip, true);
215 chip->enable_prox = true;
217 return isl29028_read_proxim(chip, prox_data);
220 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
222 struct device *dev = regmap_get_device(chip->regmap);
226 if (chip->als_ir_mode != ISL29028_MODE_ALS) {
227 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
230 "Error in enabling ALS mode err %d\n", ret);
233 chip->als_ir_mode = ISL29028_MODE_ALS;
236 ret = isl29028_read_als_ir(chip, &als_ir_data);
241 * convert als data count to lux.
242 * if lux_scale = 125, lux = count * 0.031
243 * if lux_scale = 2000, lux = count * 0.49
245 if (chip->lux_scale == 125)
246 als_ir_data = (als_ir_data * 31) / 1000;
248 als_ir_data = (als_ir_data * 49) / 100;
250 *als_data = als_ir_data;
254 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
256 struct device *dev = regmap_get_device(chip->regmap);
259 if (chip->als_ir_mode != ISL29028_MODE_IR) {
260 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
263 "Error in enabling IR mode err %d\n", ret);
266 chip->als_ir_mode = ISL29028_MODE_IR;
268 return isl29028_read_als_ir(chip, ir_data);
272 static int isl29028_write_raw(struct iio_dev *indio_dev,
273 struct iio_chan_spec const *chan,
274 int val, int val2, long mask)
276 struct isl29028_chip *chip = iio_priv(indio_dev);
277 struct device *dev = regmap_get_device(chip->regmap);
280 mutex_lock(&chip->lock);
281 switch (chan->type) {
283 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
285 "proximity: mask value 0x%08lx not supported\n",
289 if (val < 1 || val > 100) {
291 "Samp_freq %d is not in range[1:100]\n", val);
294 ret = isl29028_set_proxim_sampling(chip, val);
297 "Setting proximity samp_freq fail, err %d\n",
301 chip->prox_sampling = val;
305 if (mask != IIO_CHAN_INFO_SCALE) {
307 "light: mask value 0x%08lx not supported\n",
311 if ((val != 125) && (val != 2000)) {
313 "lux scale %d is invalid [125, 2000]\n", val);
316 ret = isl29028_set_als_scale(chip, val);
319 "Setting lux scale fail with error %d\n", ret);
322 chip->lux_scale = val;
326 dev_err(dev, "Unsupported channel type\n");
329 mutex_unlock(&chip->lock);
333 static int isl29028_read_raw(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan,
335 int *val, int *val2, long mask)
337 struct isl29028_chip *chip = iio_priv(indio_dev);
338 struct device *dev = regmap_get_device(chip->regmap);
341 mutex_lock(&chip->lock);
343 case IIO_CHAN_INFO_RAW:
344 case IIO_CHAN_INFO_PROCESSED:
345 switch (chan->type) {
347 ret = isl29028_als_get(chip, val);
350 ret = isl29028_ir_get(chip, val);
353 ret = isl29028_proxim_get(chip, val);
363 case IIO_CHAN_INFO_SAMP_FREQ:
364 if (chan->type != IIO_PROXIMITY)
366 *val = chip->prox_sampling;
370 case IIO_CHAN_INFO_SCALE:
371 if (chan->type != IIO_LIGHT)
373 *val = chip->lux_scale;
378 dev_err(dev, "mask value 0x%08lx not supported\n", mask);
381 mutex_unlock(&chip->lock);
385 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
386 "1 3 5 10 13 20 83 100");
387 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
389 #define ISL29028_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
390 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
391 static struct attribute *isl29028_attributes[] = {
392 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
393 ISL29028_CONST_ATTR(in_illuminance_scale_available),
397 static const struct attribute_group isl29108_group = {
398 .attrs = isl29028_attributes,
401 static const struct iio_chan_spec isl29028_channels[] = {
404 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
405 BIT(IIO_CHAN_INFO_SCALE),
407 .type = IIO_INTENSITY,
408 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
410 .type = IIO_PROXIMITY,
411 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
412 BIT(IIO_CHAN_INFO_SAMP_FREQ),
416 static const struct iio_info isl29028_info = {
417 .attrs = &isl29108_group,
418 .driver_module = THIS_MODULE,
419 .read_raw = isl29028_read_raw,
420 .write_raw = isl29028_write_raw,
423 static int isl29028_chip_init(struct isl29028_chip *chip)
425 struct device *dev = regmap_get_device(chip->regmap);
428 chip->enable_prox = false;
429 chip->prox_sampling = 20;
430 chip->lux_scale = 2000;
431 chip->als_ir_mode = ISL29028_MODE_NONE;
433 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
435 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
436 __func__, ISL29028_REG_TEST1_MODE, ret);
439 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
441 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
442 __func__, ISL29028_REG_TEST2_MODE, ret);
446 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
448 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
449 __func__, ISL29028_REG_CONFIGURE, ret);
453 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling);
455 dev_err(dev, "setting the proximity, err = %d\n", ret);
459 ret = isl29028_set_als_scale(chip, chip->lux_scale);
461 dev_err(dev, "setting als scale failed, err = %d\n", ret);
465 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
468 case ISL29028_REG_INTERRUPT:
469 case ISL29028_REG_PROX_DATA:
470 case ISL29028_REG_ALSIR_L:
471 case ISL29028_REG_ALSIR_U:
478 static const struct regmap_config isl29028_regmap_config = {
481 .volatile_reg = isl29028_is_volatile_reg,
482 .max_register = ISL29028_NUM_REGS - 1,
483 .num_reg_defaults_raw = ISL29028_NUM_REGS,
484 .cache_type = REGCACHE_RBTREE,
487 static int isl29028_probe(struct i2c_client *client,
488 const struct i2c_device_id *id)
490 struct isl29028_chip *chip;
491 struct iio_dev *indio_dev;
494 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
496 dev_err(&client->dev, "iio allocation fails\n");
500 chip = iio_priv(indio_dev);
502 i2c_set_clientdata(client, indio_dev);
503 mutex_init(&chip->lock);
505 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
506 if (IS_ERR(chip->regmap)) {
507 ret = PTR_ERR(chip->regmap);
508 dev_err(&client->dev, "regmap initialization failed: %d\n",
513 ret = isl29028_chip_init(chip);
515 dev_err(&client->dev, "chip initialization failed: %d\n", ret);
519 indio_dev->info = &isl29028_info;
520 indio_dev->channels = isl29028_channels;
521 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
522 indio_dev->name = id->name;
523 indio_dev->dev.parent = &client->dev;
524 indio_dev->modes = INDIO_DIRECT_MODE;
525 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
527 dev_err(&client->dev,
528 "iio registration fails with error %d\n",
535 static const struct i2c_device_id isl29028_id[] = {
539 MODULE_DEVICE_TABLE(i2c, isl29028_id);
541 static const struct of_device_id isl29028_of_match[] = {
542 { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
543 { .compatible = "isil,isl29028", },
546 MODULE_DEVICE_TABLE(of, isl29028_of_match);
548 static struct i2c_driver isl29028_driver = {
551 .of_match_table = isl29028_of_match,
553 .probe = isl29028_probe,
554 .id_table = isl29028_id,
557 module_i2c_driver(isl29028_driver);
559 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
560 MODULE_LICENSE("GPL v2");
561 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");