2 * opt3001.c - Texas Instruments OPT3001 Light Sensor
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Andreas Dannenberg <dannenberg@ti.com>
7 * Based on previous work from: Felipe Balbi <balbi@ti.com>
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 of the License
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 #include <linux/bitops.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/iio.h>
33 #include <linux/iio/sysfs.h>
35 #define OPT3001_RESULT 0x00
36 #define OPT3001_CONFIGURATION 0x01
37 #define OPT3001_LOW_LIMIT 0x02
38 #define OPT3001_HIGH_LIMIT 0x03
39 #define OPT3001_MANUFACTURER_ID 0x7e
40 #define OPT3001_DEVICE_ID 0x7f
42 #define OPT3001_CONFIGURATION_RN_MASK (0xf << 12)
43 #define OPT3001_CONFIGURATION_RN_AUTO (0xc << 12)
45 #define OPT3001_CONFIGURATION_CT BIT(11)
47 #define OPT3001_CONFIGURATION_M_MASK (3 << 9)
48 #define OPT3001_CONFIGURATION_M_SHUTDOWN (0 << 9)
49 #define OPT3001_CONFIGURATION_M_SINGLE (1 << 9)
50 #define OPT3001_CONFIGURATION_M_CONTINUOUS (2 << 9) /* also 3 << 9 */
52 #define OPT3001_CONFIGURATION_OVF BIT(8)
53 #define OPT3001_CONFIGURATION_CRF BIT(7)
54 #define OPT3001_CONFIGURATION_FH BIT(6)
55 #define OPT3001_CONFIGURATION_FL BIT(5)
56 #define OPT3001_CONFIGURATION_L BIT(4)
57 #define OPT3001_CONFIGURATION_POL BIT(3)
58 #define OPT3001_CONFIGURATION_ME BIT(2)
60 #define OPT3001_CONFIGURATION_FC_MASK (3 << 0)
62 /* The end-of-conversion enable is located in the low-limit register */
63 #define OPT3001_LOW_LIMIT_EOC_ENABLE 0xc000
65 #define OPT3001_REG_EXPONENT(n) ((n) >> 12)
66 #define OPT3001_REG_MANTISSA(n) ((n) & 0xfff)
68 #define OPT3001_INT_TIME_LONG 800000
69 #define OPT3001_INT_TIME_SHORT 100000
72 * Time to wait for conversion result to be ready. The device datasheet
73 * worst-case max value is 880ms. Add some slack to be on the safe side.
75 #define OPT3001_RESULT_READY_TIMEOUT msecs_to_jiffies(1000)
78 struct i2c_client *client;
82 bool ok_to_ignore_lock;
84 wait_queue_head_t result_ready_queue;
90 u16 high_thresh_mantissa;
91 u16 low_thresh_mantissa;
97 struct opt3001_scale {
102 static const struct opt3001_scale opt3001_scales[] = {
149 static int opt3001_find_scale(const struct opt3001 *opt, int val,
150 int val2, u8 *exponent)
154 for (i = 0; i < ARRAY_SIZE(opt3001_scales); i++) {
155 const struct opt3001_scale *scale = &opt3001_scales[i];
158 * Combine the integer and micro parts for comparison
159 * purposes. Use milli lux precision to avoid 32-bit integer
162 if ((val * 1000 + val2 / 1000) <=
163 (scale->val * 1000 + scale->val2 / 1000)) {
172 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
173 u16 mantissa, int *val, int *val2)
177 lux = 10 * (mantissa << exponent);
179 *val2 = (lux - (*val * 1000)) * 1000;
182 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode)
184 *reg &= ~OPT3001_CONFIGURATION_M_MASK;
189 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.8");
191 static struct attribute *opt3001_attributes[] = {
192 &iio_const_attr_integration_time_available.dev_attr.attr,
196 static const struct attribute_group opt3001_attribute_group = {
197 .attrs = opt3001_attributes,
200 static const struct iio_event_spec opt3001_event_spec[] = {
202 .type = IIO_EV_TYPE_THRESH,
203 .dir = IIO_EV_DIR_RISING,
204 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
205 BIT(IIO_EV_INFO_ENABLE),
208 .type = IIO_EV_TYPE_THRESH,
209 .dir = IIO_EV_DIR_FALLING,
210 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
211 BIT(IIO_EV_INFO_ENABLE),
215 static const struct iio_chan_spec opt3001_channels[] = {
218 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
219 BIT(IIO_CHAN_INFO_INT_TIME),
220 .event_spec = opt3001_event_spec,
221 .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
223 IIO_CHAN_SOFT_TIMESTAMP(1),
226 static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
235 * Enable the end-of-conversion interrupt mechanism. Note that doing
236 * so will overwrite the low-level limit value however we will restore
237 * this value later on.
239 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT,
240 OPT3001_LOW_LIMIT_EOC_ENABLE);
242 dev_err(opt->dev, "failed to write register %02x\n",
247 /* Reset data-ready indicator flag (will be set in the IRQ routine) */
248 opt->result_ready = false;
250 /* Allow IRQ to access the device despite lock being set */
251 opt->ok_to_ignore_lock = true;
253 /* Configure for single-conversion mode and start a new conversion */
254 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
256 dev_err(opt->dev, "failed to read register %02x\n",
257 OPT3001_CONFIGURATION);
262 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE);
264 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
267 dev_err(opt->dev, "failed to write register %02x\n",
268 OPT3001_CONFIGURATION);
272 /* Wait for the IRQ to indicate the conversion is complete */
273 ret = wait_event_timeout(opt->result_ready_queue, opt->result_ready,
274 OPT3001_RESULT_READY_TIMEOUT);
277 /* Disallow IRQ to access the device while lock is active */
278 opt->ok_to_ignore_lock = false;
286 * Disable the end-of-conversion interrupt mechanism by restoring the
287 * low-level limit value (clearing OPT3001_LOW_LIMIT_EOC_ENABLE). Note
288 * that selectively clearing those enable bits would affect the actual
289 * limit value due to bit-overlap and therefore can't be done.
291 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
292 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT,
295 dev_err(opt->dev, "failed to write register %02x\n",
300 exponent = OPT3001_REG_EXPONENT(opt->result);
301 mantissa = OPT3001_REG_MANTISSA(opt->result);
303 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2);
305 return IIO_VAL_INT_PLUS_MICRO;
308 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2)
311 *val2 = opt->int_time;
313 return IIO_VAL_INT_PLUS_MICRO;
316 static int opt3001_set_int_time(struct opt3001 *opt, int time)
321 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
323 dev_err(opt->dev, "failed to read register %02x\n",
324 OPT3001_CONFIGURATION);
331 case OPT3001_INT_TIME_SHORT:
332 reg &= ~OPT3001_CONFIGURATION_CT;
333 opt->int_time = OPT3001_INT_TIME_SHORT;
335 case OPT3001_INT_TIME_LONG:
336 reg |= OPT3001_CONFIGURATION_CT;
337 opt->int_time = OPT3001_INT_TIME_LONG;
343 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
347 static int opt3001_read_raw(struct iio_dev *iio,
348 struct iio_chan_spec const *chan, int *val, int *val2,
351 struct opt3001 *opt = iio_priv(iio);
354 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
357 if (chan->type != IIO_LIGHT)
360 mutex_lock(&opt->lock);
363 case IIO_CHAN_INFO_PROCESSED:
364 ret = opt3001_get_lux(opt, val, val2);
366 case IIO_CHAN_INFO_INT_TIME:
367 ret = opt3001_get_int_time(opt, val, val2);
373 mutex_unlock(&opt->lock);
378 static int opt3001_write_raw(struct iio_dev *iio,
379 struct iio_chan_spec const *chan, int val, int val2,
382 struct opt3001 *opt = iio_priv(iio);
385 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
388 if (chan->type != IIO_LIGHT)
391 if (mask != IIO_CHAN_INFO_INT_TIME)
397 mutex_lock(&opt->lock);
398 ret = opt3001_set_int_time(opt, val2);
399 mutex_unlock(&opt->lock);
404 static int opt3001_read_event_value(struct iio_dev *iio,
405 const struct iio_chan_spec *chan, enum iio_event_type type,
406 enum iio_event_direction dir, enum iio_event_info info,
409 struct opt3001 *opt = iio_priv(iio);
410 int ret = IIO_VAL_INT_PLUS_MICRO;
412 mutex_lock(&opt->lock);
415 case IIO_EV_DIR_RISING:
416 opt3001_to_iio_ret(opt, opt->high_thresh_exp,
417 opt->high_thresh_mantissa, val, val2);
419 case IIO_EV_DIR_FALLING:
420 opt3001_to_iio_ret(opt, opt->low_thresh_exp,
421 opt->low_thresh_mantissa, val, val2);
427 mutex_unlock(&opt->lock);
432 static int opt3001_write_event_value(struct iio_dev *iio,
433 const struct iio_chan_spec *chan, enum iio_event_type type,
434 enum iio_event_direction dir, enum iio_event_info info,
437 struct opt3001 *opt = iio_priv(iio);
449 mutex_lock(&opt->lock);
451 ret = opt3001_find_scale(opt, val, val2, &exponent);
453 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
457 mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
458 value = (exponent << 12) | mantissa;
461 case IIO_EV_DIR_RISING:
462 reg = OPT3001_HIGH_LIMIT;
463 opt->high_thresh_mantissa = mantissa;
464 opt->high_thresh_exp = exponent;
466 case IIO_EV_DIR_FALLING:
467 reg = OPT3001_LOW_LIMIT;
468 opt->low_thresh_mantissa = mantissa;
469 opt->low_thresh_exp = exponent;
476 ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
478 dev_err(opt->dev, "failed to write register %02x\n", reg);
483 mutex_unlock(&opt->lock);
488 static int opt3001_read_event_config(struct iio_dev *iio,
489 const struct iio_chan_spec *chan, enum iio_event_type type,
490 enum iio_event_direction dir)
492 struct opt3001 *opt = iio_priv(iio);
494 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS;
497 static int opt3001_write_event_config(struct iio_dev *iio,
498 const struct iio_chan_spec *chan, enum iio_event_type type,
499 enum iio_event_direction dir, int state)
501 struct opt3001 *opt = iio_priv(iio);
506 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
509 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
512 mutex_lock(&opt->lock);
514 mode = state ? OPT3001_CONFIGURATION_M_CONTINUOUS
515 : OPT3001_CONFIGURATION_M_SHUTDOWN;
517 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
519 dev_err(opt->dev, "failed to read register %02x\n",
520 OPT3001_CONFIGURATION);
525 opt3001_set_mode(opt, ®, mode);
527 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
530 dev_err(opt->dev, "failed to write register %02x\n",
531 OPT3001_CONFIGURATION);
536 mutex_unlock(&opt->lock);
541 static const struct iio_info opt3001_info = {
542 .driver_module = THIS_MODULE,
543 .attrs = &opt3001_attribute_group,
544 .read_raw = opt3001_read_raw,
545 .write_raw = opt3001_write_raw,
546 .read_event_value = opt3001_read_event_value,
547 .write_event_value = opt3001_write_event_value,
548 .read_event_config = opt3001_read_event_config,
549 .write_event_config = opt3001_write_event_config,
552 static int opt3001_read_id(struct opt3001 *opt)
554 char manufacturer[2];
558 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
560 dev_err(opt->dev, "failed to read register %02x\n",
561 OPT3001_MANUFACTURER_ID);
565 manufacturer[0] = ret >> 8;
566 manufacturer[1] = ret & 0xff;
568 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
570 dev_err(opt->dev, "failed to read register %02x\n",
577 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
578 manufacturer[1], device_id);
583 static int opt3001_configure(struct opt3001 *opt)
588 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
590 dev_err(opt->dev, "failed to read register %02x\n",
591 OPT3001_CONFIGURATION);
597 /* Enable automatic full-scale setting mode */
598 reg &= ~OPT3001_CONFIGURATION_RN_MASK;
599 reg |= OPT3001_CONFIGURATION_RN_AUTO;
601 /* Reflect status of the device's integration time setting */
602 if (reg & OPT3001_CONFIGURATION_CT)
603 opt->int_time = OPT3001_INT_TIME_LONG;
605 opt->int_time = OPT3001_INT_TIME_SHORT;
607 /* Ensure device is in shutdown initially */
608 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
610 /* Configure for latched window-style comparison operation */
611 reg |= OPT3001_CONFIGURATION_L;
612 reg &= ~OPT3001_CONFIGURATION_POL;
613 reg &= ~OPT3001_CONFIGURATION_ME;
614 reg &= ~OPT3001_CONFIGURATION_FC_MASK;
616 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
619 dev_err(opt->dev, "failed to write register %02x\n",
620 OPT3001_CONFIGURATION);
624 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
626 dev_err(opt->dev, "failed to read register %02x\n",
631 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
632 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret);
634 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT);
636 dev_err(opt->dev, "failed to read register %02x\n",
641 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
642 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
647 static irqreturn_t opt3001_irq(int irq, void *_iio)
649 struct iio_dev *iio = _iio;
650 struct opt3001 *opt = iio_priv(iio);
653 if (!opt->ok_to_ignore_lock)
654 mutex_lock(&opt->lock);
656 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
658 dev_err(opt->dev, "failed to read register %02x\n",
659 OPT3001_CONFIGURATION);
663 if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
664 OPT3001_CONFIGURATION_M_CONTINUOUS) {
665 if (ret & OPT3001_CONFIGURATION_FH)
667 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
671 if (ret & OPT3001_CONFIGURATION_FL)
673 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
677 } else if (ret & OPT3001_CONFIGURATION_CRF) {
678 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
680 dev_err(opt->dev, "failed to read register %02x\n",
685 opt->result_ready = true;
686 wake_up(&opt->result_ready_queue);
690 if (!opt->ok_to_ignore_lock)
691 mutex_unlock(&opt->lock);
696 static int opt3001_probe(struct i2c_client *client,
697 const struct i2c_device_id *id)
699 struct device *dev = &client->dev;
703 int irq = client->irq;
706 iio = devm_iio_device_alloc(dev, sizeof(*opt));
711 opt->client = client;
714 mutex_init(&opt->lock);
715 init_waitqueue_head(&opt->result_ready_queue);
716 i2c_set_clientdata(client, iio);
718 ret = opt3001_read_id(opt);
722 ret = opt3001_configure(opt);
726 iio->name = client->name;
727 iio->channels = opt3001_channels;
728 iio->num_channels = ARRAY_SIZE(opt3001_channels);
729 iio->dev.parent = dev;
730 iio->modes = INDIO_DIRECT_MODE;
731 iio->info = &opt3001_info;
733 ret = devm_iio_device_register(dev, iio);
735 dev_err(dev, "failed to register IIO device\n");
739 ret = request_threaded_irq(irq, NULL, opt3001_irq,
740 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
743 dev_err(dev, "failed to request IRQ #%d\n", irq);
750 static int opt3001_remove(struct i2c_client *client)
752 struct iio_dev *iio = i2c_get_clientdata(client);
753 struct opt3001 *opt = iio_priv(iio);
757 free_irq(client->irq, iio);
759 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
761 dev_err(opt->dev, "failed to read register %02x\n",
762 OPT3001_CONFIGURATION);
767 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
769 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
772 dev_err(opt->dev, "failed to write register %02x\n",
773 OPT3001_CONFIGURATION);
780 static const struct i2c_device_id opt3001_id[] = {
782 { } /* Terminating Entry */
784 MODULE_DEVICE_TABLE(i2c, opt3001_id);
786 static const struct of_device_id opt3001_of_match[] = {
787 { .compatible = "ti,opt3001" },
791 static struct i2c_driver opt3001_driver = {
792 .probe = opt3001_probe,
793 .remove = opt3001_remove,
794 .id_table = opt3001_id,
798 .of_match_table = of_match_ptr(opt3001_of_match),
802 module_i2c_driver(opt3001_driver);
804 MODULE_LICENSE("GPL v2");
805 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
806 MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");