2 * A iio driver for the light sensor ISL 29028.
4 * IIO Light driver for monitoring ambient light intensity in lux and proximity
7 * Copyright (c) 2011-2012, NVIDIA Corporation, All Rights Reserved.
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>
30 #include <linux/completion.h>
31 #include <linux/interrupt.h>
32 #include <linux/regulator/consumer.h>
36 #define ISL29028_PROX_PERIOD 800
37 #define CONVERSION_TIME_MS 100
39 #define ISL29028_REG_ADD_CONFIGURE 0x01
41 #define CONFIGURE_PROX_EN_MASK (1 << 7)
42 #define CONFIGURE_PROX_EN_SH 7
44 #define CONFIGURE_PROX_SLP_SH 4
45 #define CONFIGURE_PROX_SLP_MASK (7 << CONFIGURE_PROX_SLP_SH)
47 #define CONFIGURE_PROX_DRIVE (1 << 3)
49 #define CONFIGURE_ALS_EN 1
50 #define CONFIGURE_ALS_DIS 0
51 #define CONFIGURE_ALS_EN_SH 2
52 #define CONFIGURE_ALS_EN_MASK (1 << CONFIGURE_ALS_EN_SH)
55 #define CONFIGURE_ALS_RANGE_LOW_LUX 0
56 #define CONFIGURE_ALS_RANGE_HIGH_LUX 1
57 #define CONFIGURE_ALS_RANGE_SH 1
58 #define CONFIGURE_ALS_RANGE_MASK (1 << CONFIGURE_ALS_RANGE_SH)
60 #define CONFIGURE_ALS_IR_MODE_MASK 1
61 #define CONFIGURE_ALS_IR_MODE_SH 0
62 #define CONFIGURE_ALS_IR_MODE_IR 1
63 #define CONFIGURE_ALS_IR_MODE_ALS 0
65 #define ISL29028_REG_ADD_INTERRUPT 0x02
66 #define INTERRUPT_PROX_FLAG_MASK (1 << 7)
67 #define INTERRUPT_PROX_FLAG_SH 7
68 #define INTERRUPT_PROX_FLAG_EN 1
69 #define INTERRUPT_PROX_FLAG_DIS 0
71 #define INTERRUPT_PROX_PERSIST_SH 5
72 #define INTERRUPT_PROX_PERSIST_MASK (3 << 5)
74 #define INTERRUPT_ALS_FLAG_MASK (1 << 3)
75 #define INTERRUPT_ALS_FLAG_SH 3
76 #define INTERRUPT_ALS_FLAG_EN 1
77 #define INTERRUPT_ALS_FLAG_DIS 0
79 #define INTERRUPT_ALS_PERSIST_SH 1
80 #define INTERRUPT_ALS_PERSIST_MASK (3 << 1)
82 #define ISL29028_REG_ADD_PROX_LOW_THRES 0x03
83 #define ISL29028_REG_ADD_PROX_HIGH_THRES 0x04
85 #define ISL29028_REG_ADD_ALSIR_LOW_THRES 0x05
86 #define ISL29028_REG_ADD_ALSIR_LH_THRES 0x06
87 #define ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH 0
88 #define ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH 4
89 #define ISL29028_REG_ADD_ALSIR_HIGH_THRES 0x07
91 #define ISL29028_REG_ADD_PROX_DATA 0x08
92 #define ISL29028_REG_ADD_ALSIR_L 0x09
93 #define ISL29028_REG_ADD_ALSIR_U 0x0A
95 #define ISL29028_REG_ADD_TEST1_MODE 0x0E
96 #define ISL29028_REG_ADD_TEST2_MODE 0x0F
98 #define ISL29028_MAX_REGS ISL29028_REG_ADD_TEST2_MODE
106 struct isl29028_chip {
107 struct i2c_client *client;
109 struct regulator *isl_reg;
131 bool is_proxim_int_waiting;
132 bool is_als_int_waiting;
133 struct completion prox_completion;
134 struct completion als_completion;
135 u8 reg_cache[ISL29028_MAX_REGS];
138 static bool isl29028_write_data(struct i2c_client *client, u8 reg,
139 u8 val, u8 mask, u8 shift)
143 struct iio_dev *indio_dev = i2c_get_clientdata(client);
144 struct isl29028_chip *chip = iio_priv(indio_dev);
146 regval = chip->reg_cache[reg];
148 regval |= val << shift;
150 ret = i2c_smbus_write_byte_data(client, reg, regval);
152 dev_err(&client->dev, "Write to device reg %d fails status "
156 chip->reg_cache[reg] = regval;
160 static bool isl29028_set_proxim_period(struct i2c_client *client,
161 bool is_enable, int period)
163 int prox_period[] = {0, 12, 50, 75, 100, 200, 400, 800};
170 for (i = 1; i < ARRAY_SIZE(prox_period) - 1; ++i) {
171 if ((prox_period[i] <= period) &&
172 period < prox_period[i + 1])
179 dev_dbg(&client->dev, "Disabling proximity sensing\n");
180 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
181 0, CONFIGURE_PROX_EN_MASK, CONFIGURE_PROX_EN_SH);
183 dev_dbg(&client->dev, "Enabling proximity sensing with period "
184 "of %d ms sel %d period %d\n", prox_period[7 - sel],
186 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
187 sel, CONFIGURE_PROX_SLP_MASK, CONFIGURE_PROX_SLP_SH);
189 st = isl29028_write_data(client,
190 ISL29028_REG_ADD_CONFIGURE, 1,
191 CONFIGURE_PROX_EN_MASK, CONFIGURE_PROX_EN_SH);
196 static bool isl29028_set_proxim_persist(struct i2c_client *client,
197 bool is_enable, int persist)
199 int prox_perstant[] = {1, 4, 8, 16};
204 for (i = 0; i < ARRAY_SIZE(prox_perstant) - 1; ++i) {
205 if ((prox_perstant[i] <= persist) &&
206 persist < prox_perstant[i+1])
213 dev_dbg(&client->dev, "Enabling proximity threshold interrupt\n");
214 st = isl29028_write_data(client, ISL29028_REG_ADD_INTERRUPT,
215 sel, INTERRUPT_PROX_PERSIST_MASK,
216 INTERRUPT_PROX_PERSIST_SH);
218 st = isl29028_write_data(client,
219 ISL29028_REG_ADD_INTERRUPT,
220 INTERRUPT_PROX_FLAG_EN,
221 INTERRUPT_PROX_FLAG_MASK,
222 INTERRUPT_PROX_FLAG_SH);
224 st = isl29028_write_data(client,
225 ISL29028_REG_ADD_INTERRUPT, INTERRUPT_PROX_FLAG_DIS,
226 INTERRUPT_PROX_FLAG_MASK, INTERRUPT_PROX_FLAG_SH);
231 static bool isl29028_set_als_persist(struct i2c_client *client, bool is_enable,
234 int prox_perstant[] = {1, 4, 8, 16};
239 for (i = 0; i < ARRAY_SIZE(prox_perstant) - 1; ++i) {
240 if ((prox_perstant[i] <= persist) &&
241 persist < prox_perstant[i+1])
248 dev_dbg(&client->dev, "Enabling als threshold interrupt\n");
249 st = isl29028_write_data(client, ISL29028_REG_ADD_INTERRUPT,
250 sel, INTERRUPT_ALS_PERSIST_MASK,
251 INTERRUPT_ALS_PERSIST_SH);
253 st = isl29028_write_data(client,
254 ISL29028_REG_ADD_INTERRUPT,
255 INTERRUPT_ALS_FLAG_EN,
256 INTERRUPT_ALS_FLAG_MASK,
257 INTERRUPT_ALS_FLAG_SH);
259 st = isl29028_write_data(client,
260 ISL29028_REG_ADD_INTERRUPT, INTERRUPT_ALS_FLAG_DIS,
261 INTERRUPT_ALS_FLAG_MASK, INTERRUPT_ALS_FLAG_SH);
266 static bool isl29028_set_proxim_high_threshold(struct i2c_client *client, u8 th)
268 return isl29028_write_data(client, ISL29028_REG_ADD_PROX_HIGH_THRES,
272 static bool isl29028_set_proxim_low_threshold(struct i2c_client *client, u8 th)
274 return isl29028_write_data(client, ISL29028_REG_ADD_PROX_LOW_THRES,
278 static bool isl29028_set_irals_high_threshold(struct i2c_client *client,
282 st = isl29028_write_data(client, ISL29028_REG_ADD_ALSIR_HIGH_THRES,
283 (als >> 4) & 0xFF, 0xFF, 0);
285 st = isl29028_write_data(client,
286 ISL29028_REG_ADD_ALSIR_LH_THRES, als & 0xF,
287 0xF << ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH,
288 ISL29028_REG_ADD_ALSIR_LH_THRES_H_SH);
292 static bool isl29028_set_irals_low_threshold(struct i2c_client *client, u32 als)
295 st = isl29028_write_data(client,
296 ISL29028_REG_ADD_ALSIR_LH_THRES, (als >> 8) & 0xF,
297 0xF << ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH,
298 ISL29028_REG_ADD_ALSIR_LH_THRES_L_SH);
300 st = isl29028_write_data(client,
301 ISL29028_REG_ADD_ALSIR_LOW_THRES,
302 als & 0xFF, 0xFF, 0);
306 static bool isl29028_set_als_ir_mode(struct i2c_client *client, bool is_enable,
309 struct iio_dev *indio_dev = i2c_get_clientdata(client);
310 struct isl29028_chip *chip = iio_priv(indio_dev);
314 dev_dbg(&client->dev, "Enabling ALS mode\n");
315 st = isl29028_write_data(client,
316 ISL29028_REG_ADD_CONFIGURE,
317 CONFIGURE_ALS_IR_MODE_ALS,
318 CONFIGURE_ALS_IR_MODE_MASK,
319 CONFIGURE_ALS_IR_MODE_SH);
321 st = isl29028_write_data(client,
322 ISL29028_REG_ADD_CONFIGURE,
323 CONFIGURE_ALS_RANGE_HIGH_LUX,
324 CONFIGURE_ALS_RANGE_MASK,
325 CONFIGURE_ALS_RANGE_SH);
327 st = isl29028_set_irals_high_threshold(client,
328 chip->als_high_thres);
330 st = isl29028_set_irals_low_threshold(client,
331 chip->als_low_thres);
333 dev_dbg(&client->dev, "Enabling IR mode\n");
334 st = isl29028_write_data(client,
335 ISL29028_REG_ADD_CONFIGURE,
336 CONFIGURE_ALS_IR_MODE_IR,
337 CONFIGURE_ALS_IR_MODE_MASK,
338 CONFIGURE_ALS_IR_MODE_SH);
340 st = isl29028_set_irals_high_threshold(client,
341 chip->ir_high_thres);
343 st = isl29028_set_irals_low_threshold(client,
347 st = isl29028_write_data(client,
348 ISL29028_REG_ADD_CONFIGURE,
350 CONFIGURE_ALS_EN_MASK,
351 CONFIGURE_ALS_EN_SH);
353 st = isl29028_write_data(client,
354 ISL29028_REG_ADD_CONFIGURE,
356 CONFIGURE_ALS_EN_MASK,
357 CONFIGURE_ALS_EN_SH);
362 static bool isl29028_read_als_ir(struct i2c_client *client, int *als_ir)
367 lsb = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_ALSIR_L);
369 dev_err(&client->dev, "Error in reading register %d, error %d\n",
370 ISL29028_REG_ADD_ALSIR_L, lsb);
374 msb = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_ALSIR_U);
376 dev_err(&client->dev, "Error in reading register %d, error %d\n",
377 ISL29028_REG_ADD_ALSIR_U, lsb);
380 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
384 static bool isl29028_read_proxim(struct i2c_client *client, int *prox)
388 data = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_PROX_DATA);
390 dev_err(&client->dev, "Error in reading register %d, error %d\n",
391 ISL29028_REG_ADD_PROX_DATA, data);
398 /* Sysfs interface */
399 /* proximity period */
400 static ssize_t show_prox_period(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 struct iio_dev *indio_dev = dev_get_drvdata(dev);
404 struct isl29028_chip *chip = iio_priv(indio_dev);
406 dev_vdbg(dev, "%s()\n", __func__);
407 return sprintf(buf, "%d\n", chip->prox_period);
410 static ssize_t store_prox_period(struct device *dev,
411 struct device_attribute *attr, const char *buf, size_t count)
413 struct iio_dev *indio_dev = dev_get_drvdata(dev);
414 struct isl29028_chip *chip = iio_priv(indio_dev);
415 struct i2c_client *client = chip->client;
419 dev_vdbg(dev, "%s()\n", __func__);
421 if (strict_strtoul(buf, 10, &lval))
424 mutex_lock(&chip->lock);
425 st = isl29028_set_proxim_period(client, chip->is_prox_enable,
428 chip->prox_period = (int)lval;
430 dev_err(dev, "Error in setting the proximity period\n");
432 mutex_unlock(&chip->lock);
436 /* proximity enable/disable */
437 static ssize_t show_prox_enable(struct device *dev,
438 struct device_attribute *attr, char *buf)
440 struct iio_dev *indio_dev = dev_get_drvdata(dev);
441 struct isl29028_chip *chip = iio_priv(indio_dev);
443 dev_vdbg(dev, "%s()\n", __func__);
444 if (chip->is_prox_enable)
445 return sprintf(buf, "1\n");
447 return sprintf(buf, "0\n");
450 static ssize_t store_prox_enable(struct device *dev,
451 struct device_attribute *attr, const char *buf, size_t count)
453 struct iio_dev *indio_dev = dev_get_drvdata(dev);
454 struct isl29028_chip *chip = iio_priv(indio_dev);
455 struct i2c_client *client = chip->client;
459 dev_vdbg(dev, "%s()\n", __func__);
461 if (strict_strtoul(buf, 10, &lval))
463 if ((lval != 1) && (lval != 0)) {
464 dev_err(dev, "illegal value %lu\n", lval);
468 mutex_lock(&chip->lock);
470 if (lval == chip->is_prox_enable) {
471 mutex_unlock(&chip->lock);
476 switch (chip->als_ir_mode) {
479 regulator_enable(chip->isl_reg);
482 st = isl29028_set_proxim_period(client, true,
486 switch (chip->als_ir_mode) {
489 st = isl29028_set_proxim_period(client, false,
494 regulator_disable(chip->isl_reg);
498 chip->is_prox_enable = (lval) ? true : false;
500 dev_err(dev, "Error in enabling proximity\n");
502 mutex_unlock(&chip->lock);
506 /* als/ir enable/disable */
507 static ssize_t show_als_ir_mode(struct device *dev,
508 struct device_attribute *attr, char *buf)
510 struct iio_dev *indio_dev = dev_get_drvdata(dev);
511 struct isl29028_chip *chip = iio_priv(indio_dev);
513 dev_vdbg(dev, "%s()\n", __func__);
514 return sprintf(buf, "Current Mode: %d [0:None, 1:ALS, 2:IR]\n",
518 static ssize_t store_als_ir_mode(struct device *dev,
519 struct device_attribute *attr, const char *buf, size_t count)
521 struct iio_dev *indio_dev = dev_get_drvdata(dev);
522 struct isl29028_chip *chip = iio_priv(indio_dev);
523 struct i2c_client *client = chip->client;
527 dev_vdbg(dev, "%s()\n", __func__);
529 if (strict_strtoul(buf, 10, &lval))
532 dev_err(dev, "illegal value %lu\n", lval);
536 mutex_lock(&chip->lock);
538 if (lval == chip->is_prox_enable) {
539 mutex_unlock(&chip->lock);
545 if (chip->is_prox_enable)
546 st = isl29028_set_als_ir_mode(client, false, false);
547 else if (chip->isl_reg)
548 regulator_disable(chip->isl_reg);
552 if (!chip->is_prox_enable && chip->isl_reg)
553 regulator_enable(chip->isl_reg);
554 st = isl29028_set_als_ir_mode(client, true, (lval == MODE_ALS));
558 chip->als_ir_mode = (int)lval;
560 dev_err(dev, "Error in enabling als/ir mode\n");
562 mutex_unlock(&chip->lock);
566 /* Proximity low thresholds */
567 static ssize_t show_proxim_low_threshold(struct device *dev,
568 struct device_attribute *attr, char *buf)
570 struct iio_dev *indio_dev = dev_get_drvdata(dev);
571 struct isl29028_chip *chip = iio_priv(indio_dev);
573 dev_vdbg(dev, "%s()\n", __func__);
574 return sprintf(buf, "%d\n", chip->prox_low_thres);
577 static ssize_t store_proxim_low_threshold(struct device *dev,
578 struct device_attribute *attr, const char *buf, size_t count)
580 struct iio_dev *indio_dev = dev_get_drvdata(dev);
581 struct isl29028_chip *chip = iio_priv(indio_dev);
582 struct i2c_client *client = chip->client;
586 dev_vdbg(dev, "%s()\n", __func__);
588 if (strict_strtoul(buf, 10, &lval))
591 if ((lval > 0xFF) || (lval < 0x0)) {
592 dev_err(dev, "The threshold is not supported\n");
596 mutex_lock(&chip->lock);
597 st = isl29028_set_proxim_low_threshold(client, (u8)lval);
599 chip->prox_low_thres = (int)lval;
601 dev_err(dev, "Error in setting proximity low threshold\n");
603 mutex_unlock(&chip->lock);
607 /* Proximity high thresholds */
608 static ssize_t show_proxim_high_threshold(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct iio_dev *indio_dev = dev_get_drvdata(dev);
612 struct isl29028_chip *chip = iio_priv(indio_dev);
614 dev_vdbg(dev, "%s()\n", __func__);
615 return sprintf(buf, "%d\n", chip->prox_high_thres);
618 static ssize_t store_proxim_high_threshold(struct device *dev,
619 struct device_attribute *attr, const char *buf, size_t count)
621 struct iio_dev *indio_dev = dev_get_drvdata(dev);
622 struct isl29028_chip *chip = iio_priv(indio_dev);
623 struct i2c_client *client = chip->client;
627 dev_vdbg(dev, "%s()\n", __func__);
629 if (strict_strtoul(buf, 10, &lval))
632 if ((lval > 0xFF) || (lval < 0x0)) {
633 dev_err(dev, "The threshold is not supported\n");
637 mutex_lock(&chip->lock);
638 st = isl29028_set_proxim_high_threshold(client, (u8)lval);
640 chip->prox_high_thres = (int)lval;
642 dev_err(dev, "Error in setting proximity high threshold\n");
644 mutex_unlock(&chip->lock);
648 /* als low thresholds */
649 static ssize_t show_als_low_threshold(struct device *dev,
650 struct device_attribute *attr, char *buf)
652 struct iio_dev *indio_dev = dev_get_drvdata(dev);
653 struct isl29028_chip *chip = iio_priv(indio_dev);
655 dev_vdbg(dev, "%s()\n", __func__);
656 return sprintf(buf, "%d\n", chip->als_low_thres);
659 static ssize_t store_als_low_threshold(struct device *dev,
660 struct device_attribute *attr, const char *buf, size_t count)
662 struct iio_dev *indio_dev = dev_get_drvdata(dev);
663 struct isl29028_chip *chip = iio_priv(indio_dev);
664 struct i2c_client *client = chip->client;
668 dev_vdbg(dev, "%s()\n", __func__);
670 if (strict_strtoul(buf, 10, &lval))
673 if ((lval > 0xFFFF) || (lval < 0x0)) {
674 dev_err(dev, "The ALS threshold is not supported\n");
678 mutex_lock(&chip->lock);
679 if (chip->als_ir_mode == MODE_ALS) {
680 st = isl29028_set_irals_low_threshold(client, (int)lval);
682 chip->als_low_thres = (int)lval;
684 dev_err(dev, "Error in setting als low threshold\n");
686 chip->als_low_thres = (int)lval;
687 mutex_unlock(&chip->lock);
691 /* Als high thresholds */
692 static ssize_t show_als_high_threshold(struct device *dev,
693 struct device_attribute *attr, char *buf)
695 struct iio_dev *indio_dev = dev_get_drvdata(dev);
696 struct isl29028_chip *chip = iio_priv(indio_dev);
698 dev_vdbg(dev, "%s()\n", __func__);
699 return sprintf(buf, "%d\n", chip->als_high_thres);
702 static ssize_t store_als_high_threshold(struct device *dev,
703 struct device_attribute *attr, const char *buf, size_t count)
705 struct iio_dev *indio_dev = dev_get_drvdata(dev);
706 struct isl29028_chip *chip = iio_priv(indio_dev);
707 struct i2c_client *client = chip->client;
711 dev_vdbg(dev, "%s()\n", __func__);
713 if (strict_strtoul(buf, 10, &lval))
716 if ((lval > 0xFFF) || (lval < 0x0)) {
717 dev_err(dev, "The als threshold is not supported\n");
721 mutex_lock(&chip->lock);
722 if (chip->als_ir_mode == MODE_ALS) {
723 st = isl29028_set_irals_high_threshold(client, (int)lval);
725 chip->als_high_thres = (int)lval;
727 dev_err(dev, "Error in setting als high threshold\n");
729 chip->als_high_thres = (int)lval;
730 mutex_unlock(&chip->lock);
734 /* IR low thresholds */
735 static ssize_t show_ir_low_threshold(struct device *dev,
736 struct device_attribute *attr, char *buf)
738 struct iio_dev *indio_dev = dev_get_drvdata(dev);
739 struct isl29028_chip *chip = iio_priv(indio_dev);
741 dev_vdbg(dev, "%s()\n", __func__);
742 return sprintf(buf, "%d\n", chip->ir_low_thres);
745 static ssize_t store_ir_low_threshold(struct device *dev,
746 struct device_attribute *attr, const char *buf, size_t count)
748 struct iio_dev *indio_dev = dev_get_drvdata(dev);
749 struct isl29028_chip *chip = iio_priv(indio_dev);
750 struct i2c_client *client = chip->client;
754 dev_vdbg(dev, "%s()\n", __func__);
756 if (strict_strtoul(buf, 10, &lval))
759 if ((lval > 0xFFF) || (lval < 0x0)) {
760 dev_err(dev, "The IR threshold is not supported\n");
764 mutex_lock(&chip->lock);
765 if (chip->als_ir_mode == MODE_IR) {
766 st = isl29028_set_irals_low_threshold(client, (int)lval);
768 chip->ir_low_thres = (int)lval;
770 dev_err(dev, "Error in setting als low threshold\n");
772 chip->ir_low_thres = (int)lval;
773 mutex_unlock(&chip->lock);
777 /* IR high thresholds */
778 static ssize_t show_ir_high_threshold(struct device *dev,
779 struct device_attribute *attr, char *buf)
781 struct iio_dev *indio_dev = dev_get_drvdata(dev);
782 struct isl29028_chip *chip = iio_priv(indio_dev);
784 dev_vdbg(dev, "%s()\n", __func__);
785 return sprintf(buf, "%d\n", chip->ir_high_thres);
788 static ssize_t store_ir_high_threshold(struct device *dev,
789 struct device_attribute *attr, const char *buf, size_t count)
791 struct iio_dev *indio_dev = dev_get_drvdata(dev);
792 struct isl29028_chip *chip = iio_priv(indio_dev);
793 struct i2c_client *client = chip->client;
797 dev_vdbg(dev, "%s()\n", __func__);
799 if (strict_strtoul(buf, 10, &lval))
802 if ((lval > 0xFFFF) || (lval < 0x0)) {
803 dev_err(dev, "The als threshold is not supported\n");
807 mutex_lock(&chip->lock);
808 if (chip->als_ir_mode == MODE_IR) {
809 st = isl29028_set_irals_high_threshold(client, (int)lval);
811 chip->ir_high_thres = (int)lval;
813 dev_err(dev, "Error in setting als high threshold\n");
815 chip->ir_high_thres = (int)lval;
816 mutex_unlock(&chip->lock);
820 /* Proximity persist */
821 static ssize_t show_proxim_persist(struct device *dev,
822 struct device_attribute *attr, char *buf)
824 struct iio_dev *indio_dev = dev_get_drvdata(dev);
825 struct isl29028_chip *chip = iio_priv(indio_dev);
827 dev_vdbg(dev, "%s()\n", __func__);
828 return sprintf(buf, "%d\n", chip->prox_persist);
831 static ssize_t store_proxim_persist(struct device *dev,
832 struct device_attribute *attr, const char *buf, size_t count)
834 struct iio_dev *indio_dev = dev_get_drvdata(dev);
835 struct isl29028_chip *chip = iio_priv(indio_dev);
838 dev_vdbg(dev, "%s()\n", __func__);
840 if (strict_strtoul(buf, 10, &lval))
843 if ((lval > 16) || (lval < 0x0)) {
844 dev_err(dev, "The proximity persist is not supported\n");
848 mutex_lock(&chip->lock);
849 chip->prox_persist = (int)lval;
850 mutex_unlock(&chip->lock);
855 static ssize_t show_als_persist(struct device *dev,
856 struct device_attribute *attr, char *buf)
858 struct iio_dev *indio_dev = dev_get_drvdata(dev);
859 struct isl29028_chip *chip = iio_priv(indio_dev);
861 dev_vdbg(dev, "%s()\n", __func__);
862 return sprintf(buf, "%d\n", chip->als_persist);
865 static ssize_t store_als_persist(struct device *dev,
866 struct device_attribute *attr, const char *buf, size_t count)
868 struct iio_dev *indio_dev = dev_get_drvdata(dev);
869 struct isl29028_chip *chip = iio_priv(indio_dev);
872 dev_vdbg(dev, "%s()\n", __func__);
874 if (strict_strtoul(buf, 10, &lval))
877 if ((lval > 16) || (lval < 0x0)) {
878 dev_err(dev, "The als persist is not supported\n");
882 mutex_lock(&chip->lock);
883 chip->als_persist = (int)lval;
884 mutex_unlock(&chip->lock);
888 /* Display proxim data */
889 static ssize_t show_proxim_data(struct device *dev,
890 struct device_attribute *attr, char *buf)
892 struct iio_dev *indio_dev = dev_get_drvdata(dev);
893 struct isl29028_chip *chip = iio_priv(indio_dev);
896 ssize_t buf_count = 0;
898 dev_vdbg(dev, "%s()\n", __func__);
899 mutex_lock(&chip->lock);
901 if (chip->is_prox_enable) {
902 st = isl29028_read_proxim(chip->client, &prox_data);
904 buf_count = sprintf(buf, "%d\n", prox_data);
905 chip->prox_reading = prox_data;
908 buf_count = sprintf(buf, "%d\n", chip->prox_reading);
910 mutex_unlock(&chip->lock);
914 /* Display als data */
915 static ssize_t show_als_data(struct device *dev,
916 struct device_attribute *attr, char *buf)
918 struct iio_dev *indio_dev = dev_get_drvdata(dev);
919 struct isl29028_chip *chip = iio_priv(indio_dev);
922 ssize_t buf_count = 0;
924 dev_vdbg(dev, "%s()\n", __func__);
925 mutex_lock(&chip->lock);
927 if (chip->als_ir_mode == MODE_ALS) {
928 st = isl29028_read_als_ir(chip->client, &als_ir_data);
930 /* convert als data count to lux */
931 /* if als_range = 0, lux = count * 0.0326 */
932 /* if als_range = 1, lux = count * 0.522 */
933 if (!chip->als_range)
934 als_ir_data = (als_ir_data * 326) / 10000;
936 als_ir_data = (als_ir_data * 522) / 1000;
938 buf_count = sprintf(buf, "%d\n", als_ir_data);
939 chip->als_reading = als_ir_data;
942 buf_count = sprintf(buf, "%d\n", chip->als_reading);
943 mutex_unlock(&chip->lock);
947 /* Display IR data */
948 static ssize_t show_ir_data(struct device *dev,
949 struct device_attribute *attr, char *buf)
951 struct iio_dev *indio_dev = dev_get_drvdata(dev);
952 struct isl29028_chip *chip = iio_priv(indio_dev);
955 ssize_t buf_count = 0;
957 dev_vdbg(dev, "%s()\n", __func__);
958 mutex_lock(&chip->lock);
960 if (chip->als_ir_mode == MODE_IR) {
961 st = isl29028_read_als_ir(chip->client, &als_ir_data);
963 buf_count = sprintf(buf, "%d\n", als_ir_data);
964 chip->ir_reading = als_ir_data;
967 buf_count = sprintf(buf, "%d\n", chip->ir_reading);
968 mutex_unlock(&chip->lock);
972 /* Wait for the proximity threshold interrupt*/
973 static ssize_t show_wait_proxim_int(struct device *dev,
974 struct device_attribute *attr, char *buf)
976 struct iio_dev *indio_dev = dev_get_drvdata(dev);
977 struct isl29028_chip *chip = iio_priv(indio_dev);
978 struct i2c_client *client = chip->client;
981 dev_vdbg(dev, "%s()\n", __func__);
982 if (!chip->is_int_enable) {
983 dev_err(dev, "%s() Interrupt mode not supported\n", __func__);
984 return sprintf(buf, "error\n");
987 mutex_lock(&chip->lock);
988 st = isl29028_set_proxim_persist(client, true, chip->prox_persist);
990 dev_err(dev, "%s() Error in configuration\n", __func__);
991 mutex_unlock(&chip->lock);
992 return sprintf(buf, "error\n");
995 chip->is_proxim_int_waiting = true;
996 mutex_unlock(&chip->lock);
997 wait_for_completion(&chip->prox_completion);
998 mutex_lock(&chip->lock);
999 chip->is_proxim_int_waiting = false;
1000 isl29028_set_proxim_persist(client, false, chip->prox_persist);
1001 mutex_unlock(&chip->lock);
1002 return sprintf(buf, "done\n");
1005 /* Wait for the als/ir interrupt*/
1006 static ssize_t show_wait_als_ir_int(struct device *dev,
1007 struct device_attribute *attr, char *buf)
1009 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1010 struct isl29028_chip *chip = iio_priv(indio_dev);
1011 struct i2c_client *client = chip->client;
1014 dev_vdbg(dev, "%s()\n", __func__);
1015 if (!chip->is_int_enable) {
1016 dev_err(dev, "%s() Interrupt mode not supported\n", __func__);
1017 return sprintf(buf, "error\n");
1020 mutex_lock(&chip->lock);
1022 st = isl29028_set_als_persist(client, true, chip->als_persist);
1024 dev_err(dev, "%s() Error in als ir int configuration\n",
1026 mutex_unlock(&chip->lock);
1027 return sprintf(buf, "error\n");
1030 chip->is_als_int_waiting = true;
1031 mutex_unlock(&chip->lock);
1032 wait_for_completion(&chip->als_completion);
1033 mutex_lock(&chip->lock);
1034 chip->is_als_int_waiting = false;
1035 st = isl29028_set_als_persist(client, false, chip->als_persist);
1036 mutex_unlock(&chip->lock);
1037 return sprintf(buf, "done\n");
1041 static ssize_t show_name(struct device *dev,
1042 struct device_attribute *attr, char *buf)
1044 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1045 struct isl29028_chip *chip = iio_priv(indio_dev);
1046 return sprintf(buf, "%s\n", chip->client->name);
1049 static IIO_DEVICE_ATTR(proximity_low_threshold, S_IRUGO | S_IWUSR,
1050 show_proxim_low_threshold, store_proxim_low_threshold, 0);
1051 static IIO_DEVICE_ATTR(proximity_high_threshold, S_IRUGO | S_IWUSR,
1052 show_proxim_high_threshold, store_proxim_high_threshold, 0);
1053 static IIO_DEVICE_ATTR(proximity_persist, S_IRUGO | S_IWUSR,
1054 show_proxim_persist, store_proxim_persist, 0);
1055 static IIO_DEVICE_ATTR(proximity_period, S_IRUGO | S_IWUSR,
1056 show_prox_period, store_prox_period, 0);
1057 static IIO_DEVICE_ATTR(proximity_enable, S_IRUGO | S_IWUSR,
1058 show_prox_enable, store_prox_enable, 0);
1059 static IIO_DEVICE_ATTR(wait_proxim_thres, S_IRUGO,
1060 show_wait_proxim_int, NULL, 0);
1061 static IIO_DEVICE_ATTR(proximity_value, S_IRUGO,
1062 show_proxim_data, NULL, 0);
1064 static IIO_DEVICE_ATTR(als_low_threshold, S_IRUGO | S_IWUSR,
1065 show_als_low_threshold, store_als_low_threshold, 0);
1066 static IIO_DEVICE_ATTR(als_high_threshold, S_IRUGO | S_IWUSR,
1067 show_als_high_threshold, store_als_high_threshold, 0);
1068 static IIO_DEVICE_ATTR(als_persist, S_IRUGO | S_IWUSR,
1069 show_als_persist, store_als_persist, 0);
1070 static IIO_DEVICE_ATTR(als_ir_mode, S_IRUGO | S_IWUSR,
1071 show_als_ir_mode, store_als_ir_mode, 0);
1072 static IIO_DEVICE_ATTR(als_value, S_IRUGO,
1073 show_als_data, NULL, 0);
1074 static IIO_DEVICE_ATTR(wait_als_ir_thres, S_IRUGO,
1075 show_wait_als_ir_int, NULL, 0);
1077 static IIO_DEVICE_ATTR(ir_value, S_IRUGO,
1078 show_ir_data, NULL, 0);
1079 static IIO_DEVICE_ATTR(ir_low_threshold, S_IRUGO | S_IWUSR,
1080 show_ir_low_threshold, store_ir_low_threshold, 0);
1081 static IIO_DEVICE_ATTR(ir_high_threshold, S_IRUGO | S_IWUSR,
1082 show_ir_high_threshold, store_ir_high_threshold, 0);
1084 static IIO_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
1086 static struct attribute *isl29028_attributes[] = {
1087 &iio_dev_attr_name.dev_attr.attr,
1089 &iio_dev_attr_ir_value.dev_attr.attr,
1091 &iio_dev_attr_als_low_threshold.dev_attr.attr,
1092 &iio_dev_attr_als_high_threshold.dev_attr.attr,
1093 &iio_dev_attr_als_persist.dev_attr.attr,
1094 &iio_dev_attr_als_ir_mode.dev_attr.attr,
1095 &iio_dev_attr_als_value.dev_attr.attr,
1096 &iio_dev_attr_wait_als_ir_thres.dev_attr.attr,
1097 &iio_dev_attr_ir_low_threshold.dev_attr.attr,
1098 &iio_dev_attr_ir_high_threshold.dev_attr.attr,
1100 &iio_dev_attr_proximity_low_threshold.dev_attr.attr,
1101 &iio_dev_attr_proximity_high_threshold.dev_attr.attr,
1102 &iio_dev_attr_proximity_enable.dev_attr.attr,
1103 &iio_dev_attr_proximity_period.dev_attr.attr,
1104 &iio_dev_attr_proximity_persist.dev_attr.attr,
1105 &iio_dev_attr_proximity_value.dev_attr.attr,
1106 &iio_dev_attr_wait_proxim_thres.dev_attr.attr,
1110 static const struct attribute_group isl29108_group = {
1111 .attrs = isl29028_attributes,
1114 static int isl29028_chip_init(struct i2c_client *client)
1116 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1117 struct isl29028_chip *chip = iio_priv(indio_dev);
1121 for (i = 0; i < ARRAY_SIZE(chip->reg_cache); i++)
1122 chip->reg_cache[i] = 0;
1124 chip->is_prox_enable = 0;
1125 chip->prox_low_thres = 0;
1126 chip->prox_high_thres = 0xFF;
1127 chip->prox_period = ISL29028_PROX_PERIOD;
1128 chip->prox_reading = 0;
1130 chip->als_low_thres = 0;
1131 chip->als_high_thres = 0xFFF;
1132 chip->als_range = 1;
1133 chip->als_reading = 0;
1134 chip->als_ir_mode = 0;
1136 chip->ir_high_thres = 0xFFF;
1137 chip->ir_low_thres = 0;
1138 chip->ir_reading = 0;
1140 chip->is_int_enable = false;
1141 chip->prox_persist = 1;
1142 chip->als_persist = 1;
1143 chip->is_proxim_int_waiting = false;
1144 chip->is_als_int_waiting = false;
1146 /* if regulator is not available, then proceed with i2c write or
1147 * if regulator is turned on then proceed with i2c write
1149 if (chip->isl_reg && !regulator_is_enabled(chip->isl_reg))
1152 st = isl29028_write_data(client, ISL29028_REG_ADD_TEST1_MODE,
1155 st = isl29028_write_data(client, ISL29028_REG_ADD_TEST2_MODE,
1158 st = isl29028_write_data(client, ISL29028_REG_ADD_CONFIGURE,
1163 dev_err(&client->dev, "%s(): fails\n", __func__);
1169 static irqreturn_t threshold_isr(int irq, void *irq_data)
1171 struct isl29028_chip *chip = (struct isl29028_chip *)irq_data;
1173 struct i2c_client *client = chip->client;
1175 int_reg = i2c_smbus_read_byte_data(client, ISL29028_REG_ADD_INTERRUPT);
1177 dev_err(&client->dev, "Error in reading register %d, error %d\n",
1178 ISL29028_REG_ADD_INTERRUPT, int_reg);
1182 if (int_reg & INTERRUPT_PROX_FLAG_MASK) {
1183 /* Write 0 to clear */
1184 isl29028_write_data(client,
1185 ISL29028_REG_ADD_INTERRUPT, INTERRUPT_PROX_FLAG_DIS,
1186 INTERRUPT_PROX_FLAG_MASK, INTERRUPT_PROX_FLAG_SH);
1187 if (chip->is_proxim_int_waiting)
1188 complete(&chip->prox_completion);
1191 if (int_reg & INTERRUPT_ALS_FLAG_MASK) {
1192 /* Write 0 to clear */
1193 isl29028_write_data(client,
1194 ISL29028_REG_ADD_INTERRUPT, INTERRUPT_ALS_FLAG_DIS,
1195 INTERRUPT_ALS_FLAG_MASK, INTERRUPT_ALS_FLAG_SH);
1196 if (chip->is_als_int_waiting)
1197 complete(&chip->als_completion);
1203 static const struct iio_info isl29028_info = {
1204 .attrs = &isl29108_group,
1205 .driver_module = THIS_MODULE,
1208 static int __devinit isl29028_probe(struct i2c_client *client,
1209 const struct i2c_device_id *id)
1211 struct isl29028_chip *chip;
1212 struct iio_dev *indio_dev;
1213 struct regulator *isl_reg = regulator_get(&client->dev, "vdd");
1216 dev_dbg(&client->dev, "%s() called\n", __func__);
1218 if (IS_ERR_OR_NULL(isl_reg)) {
1219 dev_info(&client->dev, "no regulator found," \
1220 "continue without regulator\n");
1224 indio_dev = iio_allocate_device(sizeof(*chip));
1225 if (indio_dev == NULL) {
1226 dev_err(&client->dev, "iio allocation fails\n");
1230 chip = iio_priv(indio_dev);
1232 i2c_set_clientdata(client, indio_dev);
1233 chip->client = client;
1234 chip->irq = client->irq;
1235 chip->isl_reg = isl_reg;
1237 mutex_init(&chip->lock);
1239 err = isl29028_chip_init(client);
1243 init_completion(&chip->prox_completion);
1244 init_completion(&chip->als_completion);
1246 if (chip->irq > 0) {
1247 err = request_threaded_irq(chip->irq, NULL, threshold_isr,
1248 IRQF_SHARED, "ISL29028", chip);
1250 dev_err(&client->dev, "Unable to register irq %d; "
1251 "error %d\n", chip->irq, err);
1256 chip->is_int_enable = true;
1257 indio_dev->info = &isl29028_info;
1258 indio_dev->dev.parent = &client->dev;
1259 indio_dev->modes = INDIO_DIRECT_MODE;
1260 err = iio_device_register(indio_dev);
1262 dev_err(&client->dev, "iio registration fails\n");
1265 dev_dbg(&client->dev, "%s() success\n", __func__);
1270 free_irq(chip->irq, chip);
1273 regulator_put(chip->isl_reg);
1274 iio_free_device(indio_dev);
1279 static int __devexit isl29028_remove(struct i2c_client *client)
1281 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1282 struct isl29028_chip *chip = iio_priv(indio_dev);
1284 dev_dbg(&client->dev, "%s()\n", __func__);
1286 free_irq(chip->irq, chip);
1288 regulator_put(chip->isl_reg);
1289 iio_device_unregister(indio_dev);
1293 static const struct i2c_device_id isl29028_id[] = {
1298 MODULE_DEVICE_TABLE(i2c, isl29028_id);
1300 static struct i2c_driver isl29028_driver = {
1301 .class = I2C_CLASS_HWMON,
1304 .owner = THIS_MODULE,
1306 .probe = isl29028_probe,
1307 .remove = __devexit_p(isl29028_remove),
1308 .id_table = isl29028_id,
1311 static int __init isl29028_init(void)
1313 return i2c_add_driver(&isl29028_driver);
1316 static void __exit isl29028_exit(void)
1318 i2c_del_driver(&isl29028_driver);
1321 module_init(isl29028_init);
1322 module_exit(isl29028_exit);