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)
69 * Time to wait for conversion result to be ready. The device datasheet
70 * worst-case max value is 880ms. Add some slack to be on the safe side.
72 #define OPT3001_RESULT_READY_TIMEOUT msecs_to_jiffies(1000)
75 struct i2c_client *client;
79 u16 ok_to_ignore_lock:1;
81 wait_queue_head_t result_ready_queue;
87 u16 high_thresh_mantissa;
88 u16 low_thresh_mantissa;
94 struct opt3001_scale {
99 static const struct opt3001_scale opt3001_scales[] = {
146 static int opt3001_find_scale(const struct opt3001 *opt, int val,
147 int val2, u8 *exponent)
151 for (i = 0; i < ARRAY_SIZE(opt3001_scales); i++) {
152 const struct opt3001_scale *scale = &opt3001_scales[i];
155 * Combine the integer and micro parts for comparison
156 * purposes. Use milli lux precision to avoid 32-bit integer
159 if ((val * 1000 + val2 / 1000) <=
160 (scale->val * 1000 + scale->val2 / 1000)) {
169 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
170 u16 mantissa, int *val, int *val2)
174 lux = 10 * (mantissa << exponent);
176 *val2 = (lux - (*val * 1000)) * 1000;
179 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode)
181 *reg &= ~OPT3001_CONFIGURATION_M_MASK;
186 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.8");
188 static struct attribute *opt3001_attributes[] = {
189 &iio_const_attr_integration_time_available.dev_attr.attr,
193 static const struct attribute_group opt3001_attribute_group = {
194 .attrs = opt3001_attributes,
197 static const struct iio_event_spec opt3001_event_spec[] = {
199 .type = IIO_EV_TYPE_THRESH,
200 .dir = IIO_EV_DIR_RISING,
201 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
202 BIT(IIO_EV_INFO_ENABLE),
205 .type = IIO_EV_TYPE_THRESH,
206 .dir = IIO_EV_DIR_FALLING,
207 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
208 BIT(IIO_EV_INFO_ENABLE),
212 static const struct iio_chan_spec opt3001_channels[] = {
215 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
216 BIT(IIO_CHAN_INFO_INT_TIME),
217 .event_spec = opt3001_event_spec,
218 .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
220 IIO_CHAN_SOFT_TIMESTAMP(1),
223 static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
232 * Enable the end-of-conversion interrupt mechanism. Note that doing
233 * so will overwrite the low-level limit value however we will restore
234 * this value later on.
236 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT,
237 OPT3001_LOW_LIMIT_EOC_ENABLE);
239 dev_err(opt->dev, "failed to write register %02x\n",
244 /* Reset data-ready indicator flag (will be set in the IRQ routine) */
245 opt->result_ready = false;
247 /* Allow IRQ to access the device despite lock being set */
248 opt->ok_to_ignore_lock = true;
250 /* Configure for single-conversion mode and start a new conversion */
251 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
253 dev_err(opt->dev, "failed to read register %02x\n",
254 OPT3001_CONFIGURATION);
259 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE);
261 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
264 dev_err(opt->dev, "failed to write register %02x\n",
265 OPT3001_CONFIGURATION);
269 /* Wait for the IRQ to indicate the conversion is complete */
270 ret = wait_event_timeout(opt->result_ready_queue, opt->result_ready,
271 OPT3001_RESULT_READY_TIMEOUT);
274 /* Disallow IRQ to access the device while lock is active */
275 opt->ok_to_ignore_lock = false;
283 * Disable the end-of-conversion interrupt mechanism by restoring the
284 * low-level limit value (clearing OPT3001_LOW_LIMIT_EOC_ENABLE). Note
285 * that selectively clearing those enable bits would affect the actual
286 * limit value due to bit-overlap and therefore can't be done.
288 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
289 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT,
292 dev_err(opt->dev, "failed to write register %02x\n",
297 exponent = OPT3001_REG_EXPONENT(opt->result);
298 mantissa = OPT3001_REG_MANTISSA(opt->result);
300 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2);
302 return IIO_VAL_INT_PLUS_MICRO;
305 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2)
308 *val2 = opt->int_time;
310 return IIO_VAL_INT_PLUS_MICRO;
313 static int opt3001_set_int_time(struct opt3001 *opt, int time)
318 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
320 dev_err(opt->dev, "failed to read register %02x\n",
321 OPT3001_CONFIGURATION);
329 reg &= ~OPT3001_CONFIGURATION_CT;
330 opt->int_time = 100000;
333 reg |= OPT3001_CONFIGURATION_CT;
334 opt->int_time = 800000;
340 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
344 static int opt3001_read_raw(struct iio_dev *iio,
345 struct iio_chan_spec const *chan, int *val, int *val2,
348 struct opt3001 *opt = iio_priv(iio);
351 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
354 if (chan->type != IIO_LIGHT)
357 mutex_lock(&opt->lock);
360 case IIO_CHAN_INFO_PROCESSED:
361 ret = opt3001_get_lux(opt, val, val2);
363 case IIO_CHAN_INFO_INT_TIME:
364 ret = opt3001_get_int_time(opt, val, val2);
370 mutex_unlock(&opt->lock);
375 static int opt3001_write_raw(struct iio_dev *iio,
376 struct iio_chan_spec const *chan, int val, int val2,
379 struct opt3001 *opt = iio_priv(iio);
382 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
385 if (chan->type != IIO_LIGHT)
388 if (mask != IIO_CHAN_INFO_INT_TIME)
394 mutex_lock(&opt->lock);
395 ret = opt3001_set_int_time(opt, val2);
396 mutex_unlock(&opt->lock);
401 static int opt3001_read_event_value(struct iio_dev *iio,
402 const struct iio_chan_spec *chan, enum iio_event_type type,
403 enum iio_event_direction dir, enum iio_event_info info,
406 struct opt3001 *opt = iio_priv(iio);
407 int ret = IIO_VAL_INT_PLUS_MICRO;
409 mutex_lock(&opt->lock);
412 case IIO_EV_DIR_RISING:
413 opt3001_to_iio_ret(opt, opt->high_thresh_exp,
414 opt->high_thresh_mantissa, val, val2);
416 case IIO_EV_DIR_FALLING:
417 opt3001_to_iio_ret(opt, opt->low_thresh_exp,
418 opt->low_thresh_mantissa, val, val2);
424 mutex_unlock(&opt->lock);
429 static int opt3001_write_event_value(struct iio_dev *iio,
430 const struct iio_chan_spec *chan, enum iio_event_type type,
431 enum iio_event_direction dir, enum iio_event_info info,
434 struct opt3001 *opt = iio_priv(iio);
446 mutex_lock(&opt->lock);
448 ret = opt3001_find_scale(opt, val, val2, &exponent);
450 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
454 mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
455 value = (exponent << 12) | mantissa;
458 case IIO_EV_DIR_RISING:
459 reg = OPT3001_HIGH_LIMIT;
460 opt->high_thresh_mantissa = mantissa;
461 opt->high_thresh_exp = exponent;
463 case IIO_EV_DIR_FALLING:
464 reg = OPT3001_LOW_LIMIT;
465 opt->low_thresh_mantissa = mantissa;
466 opt->low_thresh_exp = exponent;
473 ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
475 dev_err(opt->dev, "failed to write register %02x\n", reg);
480 mutex_unlock(&opt->lock);
485 static int opt3001_read_event_config(struct iio_dev *iio,
486 const struct iio_chan_spec *chan, enum iio_event_type type,
487 enum iio_event_direction dir)
489 struct opt3001 *opt = iio_priv(iio);
491 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS;
494 static int opt3001_write_event_config(struct iio_dev *iio,
495 const struct iio_chan_spec *chan, enum iio_event_type type,
496 enum iio_event_direction dir, int state)
498 struct opt3001 *opt = iio_priv(iio);
503 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
506 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
509 mutex_lock(&opt->lock);
511 mode = state ? OPT3001_CONFIGURATION_M_CONTINUOUS
512 : OPT3001_CONFIGURATION_M_SHUTDOWN;
514 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
516 dev_err(opt->dev, "failed to read register %02x\n",
517 OPT3001_CONFIGURATION);
522 opt3001_set_mode(opt, ®, mode);
524 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
527 dev_err(opt->dev, "failed to write register %02x\n",
528 OPT3001_CONFIGURATION);
533 mutex_unlock(&opt->lock);
538 static const struct iio_info opt3001_info = {
539 .driver_module = THIS_MODULE,
540 .attrs = &opt3001_attribute_group,
541 .read_raw = opt3001_read_raw,
542 .write_raw = opt3001_write_raw,
543 .read_event_value = opt3001_read_event_value,
544 .write_event_value = opt3001_write_event_value,
545 .read_event_config = opt3001_read_event_config,
546 .write_event_config = opt3001_write_event_config,
549 static int opt3001_read_id(struct opt3001 *opt)
551 char manufacturer[2];
555 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
557 dev_err(opt->dev, "failed to read register %02x\n",
558 OPT3001_MANUFACTURER_ID);
562 manufacturer[0] = ret >> 8;
563 manufacturer[1] = ret & 0xff;
565 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
567 dev_err(opt->dev, "failed to read register %02x\n",
574 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
575 manufacturer[1], device_id);
580 static int opt3001_configure(struct opt3001 *opt)
585 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
587 dev_err(opt->dev, "failed to read register %02x\n",
588 OPT3001_CONFIGURATION);
594 /* Enable automatic full-scale setting mode */
595 reg &= ~OPT3001_CONFIGURATION_RN_MASK;
596 reg |= OPT3001_CONFIGURATION_RN_AUTO;
598 /* Reflect status of the device's integration time setting */
599 if (reg & OPT3001_CONFIGURATION_CT)
600 opt->int_time = 800000;
602 opt->int_time = 100000;
604 /* Ensure device is in shutdown initially */
605 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
607 /* Configure for latched window-style comparison operation */
608 reg |= OPT3001_CONFIGURATION_L;
609 reg &= ~OPT3001_CONFIGURATION_POL;
610 reg &= ~OPT3001_CONFIGURATION_ME;
611 reg &= ~OPT3001_CONFIGURATION_FC_MASK;
613 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
616 dev_err(opt->dev, "failed to write register %02x\n",
617 OPT3001_CONFIGURATION);
621 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
623 dev_err(opt->dev, "failed to read register %02x\n",
628 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
629 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret);
631 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT);
633 dev_err(opt->dev, "failed to read register %02x\n",
638 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
639 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
644 static irqreturn_t opt3001_irq(int irq, void *_iio)
646 struct iio_dev *iio = _iio;
647 struct opt3001 *opt = iio_priv(iio);
649 bool wake_result_ready_queue = false;
651 if (!opt->ok_to_ignore_lock)
652 mutex_lock(&opt->lock);
654 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
656 dev_err(opt->dev, "failed to read register %02x\n",
657 OPT3001_CONFIGURATION);
661 if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
662 OPT3001_CONFIGURATION_M_CONTINUOUS) {
663 if (ret & OPT3001_CONFIGURATION_FH)
665 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
669 if (ret & OPT3001_CONFIGURATION_FL)
671 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
675 } else if (ret & OPT3001_CONFIGURATION_CRF) {
676 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
678 dev_err(opt->dev, "failed to read register %02x\n",
683 opt->result_ready = true;
684 wake_result_ready_queue = true;
688 if (!opt->ok_to_ignore_lock)
689 mutex_unlock(&opt->lock);
691 if (wake_result_ready_queue)
692 wake_up(&opt->result_ready_queue);
697 static int opt3001_probe(struct i2c_client *client,
698 const struct i2c_device_id *id)
700 struct device *dev = &client->dev;
704 int irq = client->irq;
707 iio = devm_iio_device_alloc(dev, sizeof(*opt));
712 opt->client = client;
715 mutex_init(&opt->lock);
716 init_waitqueue_head(&opt->result_ready_queue);
717 i2c_set_clientdata(client, iio);
719 ret = opt3001_read_id(opt);
723 ret = opt3001_configure(opt);
727 iio->name = client->name;
728 iio->channels = opt3001_channels;
729 iio->num_channels = ARRAY_SIZE(opt3001_channels);
730 iio->dev.parent = dev;
731 iio->modes = INDIO_DIRECT_MODE;
732 iio->info = &opt3001_info;
734 ret = devm_iio_device_register(dev, iio);
736 dev_err(dev, "failed to register IIO device\n");
740 ret = request_threaded_irq(irq, NULL, opt3001_irq,
741 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
744 dev_err(dev, "failed to request IRQ #%d\n", irq);
751 static int opt3001_remove(struct i2c_client *client)
753 struct iio_dev *iio = i2c_get_clientdata(client);
754 struct opt3001 *opt = iio_priv(iio);
758 free_irq(client->irq, iio);
760 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
762 dev_err(opt->dev, "failed to read register %02x\n",
763 OPT3001_CONFIGURATION);
768 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
770 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
773 dev_err(opt->dev, "failed to write register %02x\n",
774 OPT3001_CONFIGURATION);
781 static const struct i2c_device_id opt3001_id[] = {
783 { } /* Terminating Entry */
785 MODULE_DEVICE_TABLE(i2c, opt3001_id);
787 static const struct of_device_id opt3001_of_match[] = {
788 { .compatible = "ti,opt3001" },
792 static struct i2c_driver opt3001_driver = {
793 .probe = opt3001_probe,
794 .remove = opt3001_remove,
795 .id_table = opt3001_id,
799 .of_match_table = of_match_ptr(opt3001_of_match),
803 module_i2c_driver(opt3001_driver);
805 MODULE_LICENSE("GPL v2");
806 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
807 MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");