1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO driver for the light sensor ISL29028.
4 * ISL29028 is Concurrent Ambient Light and Proximity Sensor
6 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
10 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf
11 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/pm_runtime.h>
25 #define ISL29028_CONV_TIME_MS 100
27 #define ISL29028_REG_CONFIGURE 0x01
29 #define ISL29028_CONF_ALS_IR_MODE_ALS 0
30 #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
31 #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
33 #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
34 #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
35 #define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
37 #define ISL29028_CONF_ALS_DIS 0
38 #define ISL29028_CONF_ALS_EN BIT(2)
39 #define ISL29028_CONF_ALS_EN_MASK BIT(2)
41 #define ISL29028_CONF_PROX_SLP_SH 4
42 #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
44 #define ISL29028_CONF_PROX_EN BIT(7)
45 #define ISL29028_CONF_PROX_EN_MASK BIT(7)
47 #define ISL29028_REG_INTERRUPT 0x02
49 #define ISL29028_REG_PROX_DATA 0x08
50 #define ISL29028_REG_ALSIR_L 0x09
51 #define ISL29028_REG_ALSIR_U 0x0A
53 #define ISL29028_REG_TEST1_MODE 0x0E
54 #define ISL29028_REG_TEST2_MODE 0x0F
56 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
58 #define ISL29028_POWER_OFF_DELAY_MS 2000
60 struct isl29028_prox_data {
66 static const struct isl29028_prox_data isl29028_prox_data[] = {
74 * Note: Data sheet lists 12.5 ms sleep time.
75 * Round up a half millisecond for msleep().
80 enum isl29028_als_ir_mode {
81 ISL29028_MODE_NONE = 0,
86 struct isl29028_chip {
88 struct regmap *regmap;
89 int prox_sampling_int;
90 int prox_sampling_frac;
93 enum isl29028_als_ir_mode als_ir_mode;
96 static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
100 for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
101 if (isl29028_prox_data[i].sampling_int == sampling_int &&
102 isl29028_prox_data[i].sampling_fract == sampling_fract)
109 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
110 int sampling_int, int sampling_fract)
112 struct device *dev = regmap_get_device(chip->regmap);
113 int sleep_index, ret;
115 sleep_index = isl29028_find_prox_sleep_index(sampling_int,
120 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
121 ISL29028_CONF_PROX_SLP_MASK,
122 sleep_index << ISL29028_CONF_PROX_SLP_SH);
125 dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
130 chip->prox_sampling_int = sampling_int;
131 chip->prox_sampling_frac = sampling_fract;
136 static int isl29028_enable_proximity(struct isl29028_chip *chip)
140 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
141 chip->prox_sampling_frac);
145 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
146 ISL29028_CONF_PROX_EN_MASK,
147 ISL29028_CONF_PROX_EN);
151 /* Wait for conversion to be complete for first sample */
152 prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
153 chip->prox_sampling_frac);
157 msleep(isl29028_prox_data[prox_index].sleep_time);
162 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
164 struct device *dev = regmap_get_device(chip->regmap);
165 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
166 ISL29028_CONF_ALS_RANGE_LOW_LUX;
169 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
170 ISL29028_CONF_ALS_RANGE_MASK, val);
172 dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
177 chip->lux_scale = lux_scale;
182 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
183 enum isl29028_als_ir_mode mode)
187 if (chip->als_ir_mode == mode)
190 ret = isl29028_set_als_scale(chip, chip->lux_scale);
195 case ISL29028_MODE_ALS:
196 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
197 ISL29028_CONF_ALS_IR_MODE_MASK,
198 ISL29028_CONF_ALS_IR_MODE_ALS);
202 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
203 ISL29028_CONF_ALS_RANGE_MASK,
204 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
206 case ISL29028_MODE_IR:
207 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
208 ISL29028_CONF_ALS_IR_MODE_MASK,
209 ISL29028_CONF_ALS_IR_MODE_IR);
211 case ISL29028_MODE_NONE:
212 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
213 ISL29028_CONF_ALS_EN_MASK,
214 ISL29028_CONF_ALS_DIS);
220 /* Enable the ALS/IR */
221 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
222 ISL29028_CONF_ALS_EN_MASK,
223 ISL29028_CONF_ALS_EN);
227 /* Need to wait for conversion time if ALS/IR mode enabled */
228 msleep(ISL29028_CONV_TIME_MS);
230 chip->als_ir_mode = mode;
235 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
237 struct device *dev = regmap_get_device(chip->regmap);
242 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
245 "%s(): Error %d reading register ALSIR_L\n",
250 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
253 "%s(): Error %d reading register ALSIR_U\n",
258 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
263 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
265 struct device *dev = regmap_get_device(chip->regmap);
269 if (!chip->enable_prox) {
270 ret = isl29028_enable_proximity(chip);
274 chip->enable_prox = true;
277 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
279 dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
289 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
291 struct device *dev = regmap_get_device(chip->regmap);
295 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
297 dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
302 ret = isl29028_read_als_ir(chip, &als_ir_data);
307 * convert als data count to lux.
308 * if lux_scale = 125, lux = count * 0.031
309 * if lux_scale = 2000, lux = count * 0.49
311 if (chip->lux_scale == 125)
312 als_ir_data = (als_ir_data * 31) / 1000;
314 als_ir_data = (als_ir_data * 49) / 100;
316 *als_data = als_ir_data;
321 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
323 struct device *dev = regmap_get_device(chip->regmap);
326 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
328 dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
333 return isl29028_read_als_ir(chip, ir_data);
336 static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
338 struct device *dev = regmap_get_device(chip->regmap);
342 ret = pm_runtime_resume_and_get(dev);
344 pm_runtime_mark_last_busy(dev);
345 ret = pm_runtime_put_autosuspend(dev);
352 static int isl29028_write_raw(struct iio_dev *indio_dev,
353 struct iio_chan_spec const *chan,
354 int val, int val2, long mask)
356 struct isl29028_chip *chip = iio_priv(indio_dev);
357 struct device *dev = regmap_get_device(chip->regmap);
360 ret = isl29028_set_pm_runtime_busy(chip, true);
364 mutex_lock(&chip->lock);
367 switch (chan->type) {
369 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
371 "%s(): proximity: Mask value 0x%08lx is not supported\n",
376 if (val < 1 || val > 100) {
378 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
383 ret = isl29028_set_proxim_sampling(chip, val, val2);
386 if (mask != IIO_CHAN_INFO_SCALE) {
388 "%s(): light: Mask value 0x%08lx is not supported\n",
393 if (val != 125 && val != 2000) {
395 "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
400 ret = isl29028_set_als_scale(chip, val);
403 dev_err(dev, "%s(): Unsupported channel type %x\n",
404 __func__, chan->type);
408 mutex_unlock(&chip->lock);
413 ret = isl29028_set_pm_runtime_busy(chip, false);
420 static int isl29028_read_raw(struct iio_dev *indio_dev,
421 struct iio_chan_spec const *chan,
422 int *val, int *val2, long mask)
424 struct isl29028_chip *chip = iio_priv(indio_dev);
425 struct device *dev = regmap_get_device(chip->regmap);
428 ret = isl29028_set_pm_runtime_busy(chip, true);
432 mutex_lock(&chip->lock);
436 case IIO_CHAN_INFO_RAW:
437 case IIO_CHAN_INFO_PROCESSED:
438 switch (chan->type) {
440 ret = isl29028_als_get(chip, val);
443 ret = isl29028_ir_get(chip, val);
446 ret = isl29028_read_proxim(chip, val);
457 case IIO_CHAN_INFO_SAMP_FREQ:
458 if (chan->type != IIO_PROXIMITY)
461 *val = chip->prox_sampling_int;
462 *val2 = chip->prox_sampling_frac;
465 case IIO_CHAN_INFO_SCALE:
466 if (chan->type != IIO_LIGHT)
468 *val = chip->lux_scale;
472 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
477 mutex_unlock(&chip->lock);
483 * Preserve the ret variable if the call to
484 * isl29028_set_pm_runtime_busy() is successful so the reading
485 * (if applicable) is returned to user space.
487 pm_ret = isl29028_set_pm_runtime_busy(chip, false);
494 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
495 "1.25 2.5 5 10 13.3 20 80 100");
496 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
498 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
499 static struct attribute *isl29028_attributes[] = {
500 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
501 ISL29028_CONST_ATTR(in_illuminance_scale_available),
505 static const struct attribute_group isl29108_group = {
506 .attrs = isl29028_attributes,
509 static const struct iio_chan_spec isl29028_channels[] = {
512 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
513 BIT(IIO_CHAN_INFO_SCALE),
515 .type = IIO_INTENSITY,
516 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
518 .type = IIO_PROXIMITY,
519 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
520 BIT(IIO_CHAN_INFO_SAMP_FREQ),
524 static const struct iio_info isl29028_info = {
525 .attrs = &isl29108_group,
526 .read_raw = isl29028_read_raw,
527 .write_raw = isl29028_write_raw,
530 static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
532 struct device *dev = regmap_get_device(chip->regmap);
535 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
537 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
540 chip->als_ir_mode = ISL29028_MODE_NONE;
541 chip->enable_prox = false;
546 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
549 case ISL29028_REG_INTERRUPT:
550 case ISL29028_REG_PROX_DATA:
551 case ISL29028_REG_ALSIR_L:
552 case ISL29028_REG_ALSIR_U:
559 static const struct regmap_config isl29028_regmap_config = {
562 .volatile_reg = isl29028_is_volatile_reg,
563 .max_register = ISL29028_NUM_REGS - 1,
564 .num_reg_defaults_raw = ISL29028_NUM_REGS,
565 .cache_type = REGCACHE_RBTREE,
568 static int isl29028_probe(struct i2c_client *client)
570 const struct i2c_device_id *id = i2c_client_get_device_id(client);
571 struct isl29028_chip *chip;
572 struct iio_dev *indio_dev;
575 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
579 chip = iio_priv(indio_dev);
581 i2c_set_clientdata(client, indio_dev);
582 mutex_init(&chip->lock);
584 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
585 if (IS_ERR(chip->regmap)) {
586 ret = PTR_ERR(chip->regmap);
587 dev_err(&client->dev, "%s: Error %d initializing regmap\n",
592 chip->enable_prox = false;
593 chip->prox_sampling_int = 20;
594 chip->prox_sampling_frac = 0;
595 chip->lux_scale = 2000;
597 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
599 dev_err(&client->dev,
600 "%s(): Error %d writing to TEST1_MODE register\n",
605 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
607 dev_err(&client->dev,
608 "%s(): Error %d writing to TEST2_MODE register\n",
613 ret = isl29028_clear_configure_reg(chip);
617 indio_dev->info = &isl29028_info;
618 indio_dev->channels = isl29028_channels;
619 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
620 indio_dev->name = id->name;
621 indio_dev->modes = INDIO_DIRECT_MODE;
623 pm_runtime_enable(&client->dev);
624 pm_runtime_set_autosuspend_delay(&client->dev,
625 ISL29028_POWER_OFF_DELAY_MS);
626 pm_runtime_use_autosuspend(&client->dev);
628 ret = iio_device_register(indio_dev);
630 dev_err(&client->dev,
631 "%s(): iio registration failed with error %d\n",
639 static void isl29028_remove(struct i2c_client *client)
641 struct iio_dev *indio_dev = i2c_get_clientdata(client);
642 struct isl29028_chip *chip = iio_priv(indio_dev);
644 iio_device_unregister(indio_dev);
646 pm_runtime_disable(&client->dev);
647 pm_runtime_set_suspended(&client->dev);
649 isl29028_clear_configure_reg(chip);
652 static int isl29028_suspend(struct device *dev)
654 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
655 struct isl29028_chip *chip = iio_priv(indio_dev);
658 mutex_lock(&chip->lock);
660 ret = isl29028_clear_configure_reg(chip);
662 mutex_unlock(&chip->lock);
667 static int isl29028_resume(struct device *dev)
670 * The specific component (ALS/IR or proximity) will enable itself as
671 * needed the next time that the user requests a reading. This is done
672 * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity().
677 static DEFINE_RUNTIME_DEV_PM_OPS(isl29028_pm_ops, isl29028_suspend,
678 isl29028_resume, NULL);
680 static const struct i2c_device_id isl29028_id[] = {
685 MODULE_DEVICE_TABLE(i2c, isl29028_id);
687 static const struct of_device_id isl29028_of_match[] = {
688 { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
689 { .compatible = "isil,isl29028", },
690 { .compatible = "isil,isl29030", },
693 MODULE_DEVICE_TABLE(of, isl29028_of_match);
695 static struct i2c_driver isl29028_driver = {
698 .pm = pm_ptr(&isl29028_pm_ops),
699 .of_match_table = isl29028_of_match,
701 .probe = isl29028_probe,
702 .remove = isl29028_remove,
703 .id_table = isl29028_id,
706 module_i2c_driver(isl29028_driver);
708 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
709 MODULE_LICENSE("GPL v2");