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 CONVERSION_TIME_MS 100
32 #define ISL29028_REG_CONFIGURE 0x01
34 #define CONFIGURE_ALS_IR_MODE_ALS 0
35 #define CONFIGURE_ALS_IR_MODE_IR BIT(0)
36 #define CONFIGURE_ALS_IR_MODE_MASK BIT(0)
38 #define CONFIGURE_ALS_RANGE_LOW_LUX 0
39 #define CONFIGURE_ALS_RANGE_HIGH_LUX BIT(1)
40 #define CONFIGURE_ALS_RANGE_MASK BIT(1)
42 #define CONFIGURE_ALS_DIS 0
43 #define CONFIGURE_ALS_EN BIT(2)
44 #define CONFIGURE_ALS_EN_MASK BIT(2)
46 #define CONFIGURE_PROX_DRIVE BIT(3)
48 #define CONFIGURE_PROX_SLP_SH 4
49 #define CONFIGURE_PROX_SLP_MASK (7 << CONFIGURE_PROX_SLP_SH)
51 #define CONFIGURE_PROX_EN BIT(7)
52 #define CONFIGURE_PROX_EN_MASK BIT(7)
54 #define ISL29028_REG_INTERRUPT 0x02
56 #define ISL29028_REG_PROX_DATA 0x08
57 #define ISL29028_REG_ALSIR_L 0x09
58 #define ISL29028_REG_ALSIR_U 0x0A
60 #define ISL29028_REG_TEST1_MODE 0x0E
61 #define ISL29028_REG_TEST2_MODE 0x0F
63 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
71 struct isl29028_chip {
74 struct regmap *regmap;
76 unsigned int prox_sampling;
83 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
84 unsigned int sampling)
86 static unsigned int prox_period[] = {800, 400, 200, 100, 75, 50, 12, 0};
88 unsigned int period = DIV_ROUND_UP(1000, sampling);
90 for (sel = 0; sel < ARRAY_SIZE(prox_period); ++sel) {
91 if (period >= prox_period[sel])
94 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
95 CONFIGURE_PROX_SLP_MASK, sel << CONFIGURE_PROX_SLP_SH);
98 static int isl29028_enable_proximity(struct isl29028_chip *chip, bool enable)
104 val = CONFIGURE_PROX_EN;
105 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
106 CONFIGURE_PROX_EN_MASK, val);
110 /* Wait for conversion to be complete for first sample */
111 mdelay(DIV_ROUND_UP(1000, chip->prox_sampling));
115 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
117 int val = (lux_scale == 2000) ? CONFIGURE_ALS_RANGE_HIGH_LUX :
118 CONFIGURE_ALS_RANGE_LOW_LUX;
120 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
121 CONFIGURE_ALS_RANGE_MASK, val);
124 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
125 enum als_ir_mode mode)
131 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
132 CONFIGURE_ALS_IR_MODE_MASK, CONFIGURE_ALS_IR_MODE_ALS);
136 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
137 CONFIGURE_ALS_RANGE_MASK, CONFIGURE_ALS_RANGE_HIGH_LUX);
141 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
142 CONFIGURE_ALS_IR_MODE_MASK, CONFIGURE_ALS_IR_MODE_IR);
146 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
147 CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_DIS);
153 /* Enable the ALS/IR */
154 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
155 CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_EN);
159 /* Need to wait for conversion time if ALS/IR mode enabled */
160 mdelay(CONVERSION_TIME_MS);
164 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
170 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
173 "Error in reading register ALSIR_L err %d\n", ret);
177 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
180 "Error in reading register ALSIR_U err %d\n", ret);
184 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
188 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
193 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
195 dev_err(chip->dev, "Error in reading register %d, error %d\n",
196 ISL29028_REG_PROX_DATA, ret);
203 static int isl29028_proxim_get(struct isl29028_chip *chip, int *prox_data)
207 if (!chip->enable_prox) {
208 ret = isl29028_enable_proximity(chip, true);
211 chip->enable_prox = true;
213 return isl29028_read_proxim(chip, prox_data);
216 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
221 if (chip->als_ir_mode != MODE_ALS) {
222 ret = isl29028_set_als_ir_mode(chip, MODE_ALS);
225 "Error in enabling ALS mode err %d\n", ret);
228 chip->als_ir_mode = MODE_ALS;
231 ret = isl29028_read_als_ir(chip, &als_ir_data);
236 * convert als data count to lux.
237 * if lux_scale = 125, lux = count * 0.031
238 * if lux_scale = 2000, lux = count * 0.49
240 if (chip->lux_scale == 125)
241 als_ir_data = (als_ir_data * 31) / 1000;
243 als_ir_data = (als_ir_data * 49) / 100;
245 *als_data = als_ir_data;
249 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
253 if (chip->als_ir_mode != MODE_IR) {
254 ret = isl29028_set_als_ir_mode(chip, MODE_IR);
257 "Error in enabling IR mode err %d\n", ret);
260 chip->als_ir_mode = MODE_IR;
262 return isl29028_read_als_ir(chip, ir_data);
266 static int isl29028_write_raw(struct iio_dev *indio_dev,
267 struct iio_chan_spec const *chan, int val, int val2, long mask)
269 struct isl29028_chip *chip = iio_priv(indio_dev);
272 mutex_lock(&chip->lock);
273 switch (chan->type) {
275 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
277 "proximity: mask value 0x%08lx not supported\n",
281 if (val < 1 || val > 100) {
283 "Samp_freq %d is not in range[1:100]\n", val);
286 ret = isl29028_set_proxim_sampling(chip, val);
289 "Setting proximity samp_freq fail, err %d\n",
293 chip->prox_sampling = val;
297 if (mask != IIO_CHAN_INFO_SCALE) {
299 "light: mask value 0x%08lx not supported\n",
303 if ((val != 125) && (val != 2000)) {
305 "lux scale %d is invalid [125, 2000]\n", val);
308 ret = isl29028_set_als_scale(chip, val);
311 "Setting lux scale fail with error %d\n", ret);
314 chip->lux_scale = val;
318 dev_err(chip->dev, "Unsupported channel type\n");
321 mutex_unlock(&chip->lock);
325 static int isl29028_read_raw(struct iio_dev *indio_dev,
326 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
328 struct isl29028_chip *chip = iio_priv(indio_dev);
331 mutex_lock(&chip->lock);
333 case IIO_CHAN_INFO_RAW:
334 case IIO_CHAN_INFO_PROCESSED:
335 switch (chan->type) {
337 ret = isl29028_als_get(chip, val);
340 ret = isl29028_ir_get(chip, val);
343 ret = isl29028_proxim_get(chip, val);
353 case IIO_CHAN_INFO_SAMP_FREQ:
354 if (chan->type != IIO_PROXIMITY)
356 *val = chip->prox_sampling;
360 case IIO_CHAN_INFO_SCALE:
361 if (chan->type != IIO_LIGHT)
363 *val = chip->lux_scale;
368 dev_err(chip->dev, "mask value 0x%08lx not supported\n", mask);
371 mutex_unlock(&chip->lock);
375 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
376 "1, 3, 5, 10, 13, 20, 83, 100");
377 static IIO_CONST_ATTR(in_illuminance_scale_available, "125, 2000");
379 #define ISL29028_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
380 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
381 static struct attribute *isl29028_attributes[] = {
382 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
383 ISL29028_CONST_ATTR(in_illuminance_scale_available),
387 static const struct attribute_group isl29108_group = {
388 .attrs = isl29028_attributes,
391 static const struct iio_chan_spec isl29028_channels[] = {
394 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
395 BIT(IIO_CHAN_INFO_SCALE),
397 .type = IIO_INTENSITY,
398 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
400 .type = IIO_PROXIMITY,
401 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
402 BIT(IIO_CHAN_INFO_SAMP_FREQ),
406 static const struct iio_info isl29028_info = {
407 .attrs = &isl29108_group,
408 .driver_module = THIS_MODULE,
409 .read_raw = &isl29028_read_raw,
410 .write_raw = &isl29028_write_raw,
413 static int isl29028_chip_init(struct isl29028_chip *chip)
417 chip->enable_prox = false;
418 chip->prox_sampling = 20;
419 chip->lux_scale = 2000;
420 chip->als_ir_mode = MODE_NONE;
422 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
424 dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
425 __func__, ISL29028_REG_TEST1_MODE, ret);
428 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
430 dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
431 __func__, ISL29028_REG_TEST2_MODE, ret);
435 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
437 dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
438 __func__, ISL29028_REG_CONFIGURE, ret);
442 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling);
444 dev_err(chip->dev, "%s(): setting the proximity, err = %d\n",
449 ret = isl29028_set_als_scale(chip, chip->lux_scale);
451 dev_err(chip->dev, "%s(): setting als scale failed, err = %d\n",
456 static bool is_volatile_reg(struct device *dev, unsigned int reg)
459 case ISL29028_REG_INTERRUPT:
460 case ISL29028_REG_PROX_DATA:
461 case ISL29028_REG_ALSIR_L:
462 case ISL29028_REG_ALSIR_U:
469 static const struct regmap_config isl29028_regmap_config = {
472 .volatile_reg = is_volatile_reg,
473 .max_register = ISL29028_NUM_REGS - 1,
474 .num_reg_defaults_raw = ISL29028_NUM_REGS,
475 .cache_type = REGCACHE_RBTREE,
478 static int isl29028_probe(struct i2c_client *client,
479 const struct i2c_device_id *id)
481 struct isl29028_chip *chip;
482 struct iio_dev *indio_dev;
485 indio_dev = iio_device_alloc(sizeof(*chip));
487 dev_err(&client->dev, "iio allocation fails\n");
491 chip = iio_priv(indio_dev);
493 i2c_set_clientdata(client, indio_dev);
494 chip->dev = &client->dev;
495 mutex_init(&chip->lock);
497 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
498 if (IS_ERR(chip->regmap)) {
499 ret = PTR_ERR(chip->regmap);
500 dev_err(chip->dev, "regmap initialization failed: %d\n", ret);
504 ret = isl29028_chip_init(chip);
506 dev_err(chip->dev, "chip initialization failed: %d\n", ret);
510 indio_dev->info = &isl29028_info;
511 indio_dev->channels = isl29028_channels;
512 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
513 indio_dev->name = id->name;
514 indio_dev->dev.parent = &client->dev;
515 indio_dev->modes = INDIO_DIRECT_MODE;
516 ret = iio_device_register(indio_dev);
518 dev_err(chip->dev, "iio registration fails with error %d\n",
525 iio_device_free(indio_dev);
529 static int isl29028_remove(struct i2c_client *client)
531 struct iio_dev *indio_dev = i2c_get_clientdata(client);
533 iio_device_unregister(indio_dev);
534 iio_device_free(indio_dev);
538 static const struct i2c_device_id isl29028_id[] = {
542 MODULE_DEVICE_TABLE(i2c, isl29028_id);
544 static const struct of_device_id isl29028_of_match[] = {
545 { .compatible = "isil,isl29028", },
548 MODULE_DEVICE_TABLE(of, isl29028_of_match);
550 static struct i2c_driver isl29028_driver = {
551 .class = I2C_CLASS_HWMON,
554 .owner = THIS_MODULE,
555 .of_match_table = isl29028_of_match,
557 .probe = isl29028_probe,
558 .remove = isl29028_remove,
559 .id_table = isl29028_id,
562 module_i2c_driver(isl29028_driver);
564 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
565 MODULE_LICENSE("GPL v2");
566 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");