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.
7 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
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_get_sync(dev);
344 pm_runtime_put_noidle(dev);
346 pm_runtime_mark_last_busy(dev);
347 ret = pm_runtime_put_autosuspend(dev);
354 static int isl29028_write_raw(struct iio_dev *indio_dev,
355 struct iio_chan_spec const *chan,
356 int val, int val2, long mask)
358 struct isl29028_chip *chip = iio_priv(indio_dev);
359 struct device *dev = regmap_get_device(chip->regmap);
362 ret = isl29028_set_pm_runtime_busy(chip, true);
366 mutex_lock(&chip->lock);
369 switch (chan->type) {
371 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
373 "%s(): proximity: Mask value 0x%08lx is not supported\n",
378 if (val < 1 || val > 100) {
380 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
385 ret = isl29028_set_proxim_sampling(chip, val, val2);
388 if (mask != IIO_CHAN_INFO_SCALE) {
390 "%s(): light: Mask value 0x%08lx is not supported\n",
395 if (val != 125 && val != 2000) {
397 "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
402 ret = isl29028_set_als_scale(chip, val);
405 dev_err(dev, "%s(): Unsupported channel type %x\n",
406 __func__, chan->type);
410 mutex_unlock(&chip->lock);
415 ret = isl29028_set_pm_runtime_busy(chip, false);
422 static int isl29028_read_raw(struct iio_dev *indio_dev,
423 struct iio_chan_spec const *chan,
424 int *val, int *val2, long mask)
426 struct isl29028_chip *chip = iio_priv(indio_dev);
427 struct device *dev = regmap_get_device(chip->regmap);
430 ret = isl29028_set_pm_runtime_busy(chip, true);
434 mutex_lock(&chip->lock);
438 case IIO_CHAN_INFO_RAW:
439 case IIO_CHAN_INFO_PROCESSED:
440 switch (chan->type) {
442 ret = isl29028_als_get(chip, val);
445 ret = isl29028_ir_get(chip, val);
448 ret = isl29028_read_proxim(chip, val);
459 case IIO_CHAN_INFO_SAMP_FREQ:
460 if (chan->type != IIO_PROXIMITY)
463 *val = chip->prox_sampling_int;
464 *val2 = chip->prox_sampling_frac;
467 case IIO_CHAN_INFO_SCALE:
468 if (chan->type != IIO_LIGHT)
470 *val = chip->lux_scale;
474 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
479 mutex_unlock(&chip->lock);
485 * Preserve the ret variable if the call to
486 * isl29028_set_pm_runtime_busy() is successful so the reading
487 * (if applicable) is returned to user space.
489 pm_ret = isl29028_set_pm_runtime_busy(chip, false);
496 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
497 "1.25 2.5 5 10 13.3 20 80 100");
498 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
500 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
501 static struct attribute *isl29028_attributes[] = {
502 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
503 ISL29028_CONST_ATTR(in_illuminance_scale_available),
507 static const struct attribute_group isl29108_group = {
508 .attrs = isl29028_attributes,
511 static const struct iio_chan_spec isl29028_channels[] = {
514 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
515 BIT(IIO_CHAN_INFO_SCALE),
517 .type = IIO_INTENSITY,
518 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
520 .type = IIO_PROXIMITY,
521 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
522 BIT(IIO_CHAN_INFO_SAMP_FREQ),
526 static const struct iio_info isl29028_info = {
527 .attrs = &isl29108_group,
528 .read_raw = isl29028_read_raw,
529 .write_raw = isl29028_write_raw,
532 static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
534 struct device *dev = regmap_get_device(chip->regmap);
537 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
539 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
542 chip->als_ir_mode = ISL29028_MODE_NONE;
543 chip->enable_prox = false;
548 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
551 case ISL29028_REG_INTERRUPT:
552 case ISL29028_REG_PROX_DATA:
553 case ISL29028_REG_ALSIR_L:
554 case ISL29028_REG_ALSIR_U:
561 static const struct regmap_config isl29028_regmap_config = {
564 .volatile_reg = isl29028_is_volatile_reg,
565 .max_register = ISL29028_NUM_REGS - 1,
566 .num_reg_defaults_raw = ISL29028_NUM_REGS,
567 .cache_type = REGCACHE_RBTREE,
570 static int isl29028_probe(struct i2c_client *client,
571 const struct i2c_device_id *id)
573 struct isl29028_chip *chip;
574 struct iio_dev *indio_dev;
577 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
581 chip = iio_priv(indio_dev);
583 i2c_set_clientdata(client, indio_dev);
584 mutex_init(&chip->lock);
586 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
587 if (IS_ERR(chip->regmap)) {
588 ret = PTR_ERR(chip->regmap);
589 dev_err(&client->dev, "%s: Error %d initializing regmap\n",
594 chip->enable_prox = false;
595 chip->prox_sampling_int = 20;
596 chip->prox_sampling_frac = 0;
597 chip->lux_scale = 2000;
599 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
601 dev_err(&client->dev,
602 "%s(): Error %d writing to TEST1_MODE register\n",
607 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
609 dev_err(&client->dev,
610 "%s(): Error %d writing to TEST2_MODE register\n",
615 ret = isl29028_clear_configure_reg(chip);
619 indio_dev->info = &isl29028_info;
620 indio_dev->channels = isl29028_channels;
621 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
622 indio_dev->name = id->name;
623 indio_dev->modes = INDIO_DIRECT_MODE;
625 pm_runtime_enable(&client->dev);
626 pm_runtime_set_autosuspend_delay(&client->dev,
627 ISL29028_POWER_OFF_DELAY_MS);
628 pm_runtime_use_autosuspend(&client->dev);
630 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
632 dev_err(&client->dev,
633 "%s(): iio registration failed with error %d\n",
641 static int isl29028_remove(struct i2c_client *client)
643 struct iio_dev *indio_dev = i2c_get_clientdata(client);
644 struct isl29028_chip *chip = iio_priv(indio_dev);
646 iio_device_unregister(indio_dev);
648 pm_runtime_disable(&client->dev);
649 pm_runtime_set_suspended(&client->dev);
650 pm_runtime_put_noidle(&client->dev);
652 return isl29028_clear_configure_reg(chip);
655 static int __maybe_unused isl29028_suspend(struct device *dev)
657 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
658 struct isl29028_chip *chip = iio_priv(indio_dev);
661 mutex_lock(&chip->lock);
663 ret = isl29028_clear_configure_reg(chip);
665 mutex_unlock(&chip->lock);
670 static int __maybe_unused isl29028_resume(struct device *dev)
673 * The specific component (ALS/IR or proximity) will enable itself as
674 * needed the next time that the user requests a reading. This is done
675 * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity().
680 static const struct dev_pm_ops isl29028_pm_ops = {
681 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
682 pm_runtime_force_resume)
683 SET_RUNTIME_PM_OPS(isl29028_suspend, isl29028_resume, NULL)
686 static const struct i2c_device_id isl29028_id[] = {
691 MODULE_DEVICE_TABLE(i2c, isl29028_id);
693 static const struct of_device_id isl29028_of_match[] = {
694 { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
695 { .compatible = "isil,isl29028", },
696 { .compatible = "isil,isl29030", },
699 MODULE_DEVICE_TABLE(of, isl29028_of_match);
701 static struct i2c_driver isl29028_driver = {
704 .pm = &isl29028_pm_ops,
705 .of_match_table = isl29028_of_match,
707 .probe = isl29028_probe,
708 .remove = isl29028_remove,
709 .id_table = isl29028_id,
712 module_i2c_driver(isl29028_driver);
714 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
715 MODULE_LICENSE("GPL v2");
716 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");