1 // SPDX-License-Identifier: GPL-2.0-only
3 * opt3001.c - Texas Instruments OPT3001 Light Sensor
5 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
24 #include <linux/iio/events.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
28 #define OPT3001_RESULT 0x00
29 #define OPT3001_CONFIGURATION 0x01
30 #define OPT3001_LOW_LIMIT 0x02
31 #define OPT3001_HIGH_LIMIT 0x03
32 #define OPT3001_MANUFACTURER_ID 0x7e
33 #define OPT3001_DEVICE_ID 0x7f
35 #define OPT3001_CONFIGURATION_RN_MASK (0xf << 12)
36 #define OPT3001_CONFIGURATION_RN_AUTO (0xc << 12)
38 #define OPT3001_CONFIGURATION_CT BIT(11)
40 #define OPT3001_CONFIGURATION_M_MASK (3 << 9)
41 #define OPT3001_CONFIGURATION_M_SHUTDOWN (0 << 9)
42 #define OPT3001_CONFIGURATION_M_SINGLE (1 << 9)
43 #define OPT3001_CONFIGURATION_M_CONTINUOUS (2 << 9) /* also 3 << 9 */
45 #define OPT3001_CONFIGURATION_OVF BIT(8)
46 #define OPT3001_CONFIGURATION_CRF BIT(7)
47 #define OPT3001_CONFIGURATION_FH BIT(6)
48 #define OPT3001_CONFIGURATION_FL BIT(5)
49 #define OPT3001_CONFIGURATION_L BIT(4)
50 #define OPT3001_CONFIGURATION_POL BIT(3)
51 #define OPT3001_CONFIGURATION_ME BIT(2)
53 #define OPT3001_CONFIGURATION_FC_MASK (3 << 0)
55 /* The end-of-conversion enable is located in the low-limit register */
56 #define OPT3001_LOW_LIMIT_EOC_ENABLE 0xc000
58 #define OPT3001_REG_EXPONENT(n) ((n) >> 12)
59 #define OPT3001_REG_MANTISSA(n) ((n) & 0xfff)
61 #define OPT3001_INT_TIME_LONG 800000
62 #define OPT3001_INT_TIME_SHORT 100000
65 * Time to wait for conversion result to be ready. The device datasheet
66 * sect. 6.5 states results are ready after total integration time plus 3ms.
67 * This results in worst-case max values of 113ms or 883ms, respectively.
68 * Add some slack to be on the safe side.
70 #define OPT3001_RESULT_READY_SHORT 150
71 #define OPT3001_RESULT_READY_LONG 1000
74 struct i2c_client *client;
78 bool ok_to_ignore_lock;
80 wait_queue_head_t result_ready_queue;
86 u16 high_thresh_mantissa;
87 u16 low_thresh_mantissa;
95 struct opt3001_scale {
100 static const struct opt3001_scale opt3001_scales[] = {
147 static int opt3001_find_scale(const struct opt3001 *opt, int val,
148 int val2, u8 *exponent)
152 for (i = 0; i < ARRAY_SIZE(opt3001_scales); i++) {
153 const struct opt3001_scale *scale = &opt3001_scales[i];
156 * Combine the integer and micro parts for comparison
157 * purposes. Use milli lux precision to avoid 32-bit integer
160 if ((val * 1000 + val2 / 1000) <=
161 (scale->val * 1000 + scale->val2 / 1000)) {
170 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
171 u16 mantissa, int *val, int *val2)
175 lux = 10 * (mantissa << exponent);
177 *val2 = (lux - (*val * 1000)) * 1000;
180 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode)
182 *reg &= ~OPT3001_CONFIGURATION_M_MASK;
187 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.8");
189 static struct attribute *opt3001_attributes[] = {
190 &iio_const_attr_integration_time_available.dev_attr.attr,
194 static const struct attribute_group opt3001_attribute_group = {
195 .attrs = opt3001_attributes,
198 static const struct iio_event_spec opt3001_event_spec[] = {
200 .type = IIO_EV_TYPE_THRESH,
201 .dir = IIO_EV_DIR_RISING,
202 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
203 BIT(IIO_EV_INFO_ENABLE),
206 .type = IIO_EV_TYPE_THRESH,
207 .dir = IIO_EV_DIR_FALLING,
208 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
209 BIT(IIO_EV_INFO_ENABLE),
213 static const struct iio_chan_spec opt3001_channels[] = {
216 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
217 BIT(IIO_CHAN_INFO_INT_TIME),
218 .event_spec = opt3001_event_spec,
219 .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
221 IIO_CHAN_SOFT_TIMESTAMP(1),
224 static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
235 * Enable the end-of-conversion interrupt mechanism. Note that
236 * doing so will overwrite the low-level limit value however we
237 * will restore this value later on.
239 ret = i2c_smbus_write_word_swapped(opt->client,
241 OPT3001_LOW_LIMIT_EOC_ENABLE);
243 dev_err(opt->dev, "failed to write register %02x\n",
248 /* Allow IRQ to access the device despite lock being set */
249 opt->ok_to_ignore_lock = true;
252 /* Reset data-ready indicator flag */
253 opt->result_ready = false;
255 /* Configure for single-conversion mode and start a new conversion */
256 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
258 dev_err(opt->dev, "failed to read register %02x\n",
259 OPT3001_CONFIGURATION);
264 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE);
266 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
269 dev_err(opt->dev, "failed to write register %02x\n",
270 OPT3001_CONFIGURATION);
275 /* Wait for the IRQ to indicate the conversion is complete */
276 ret = wait_event_timeout(opt->result_ready_queue,
278 msecs_to_jiffies(OPT3001_RESULT_READY_LONG));
282 /* Sleep for result ready time */
283 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ?
284 OPT3001_RESULT_READY_SHORT : OPT3001_RESULT_READY_LONG;
287 /* Check result ready flag */
288 ret = i2c_smbus_read_word_swapped(opt->client,
289 OPT3001_CONFIGURATION);
291 dev_err(opt->dev, "failed to read register %02x\n",
292 OPT3001_CONFIGURATION);
296 if (!(ret & OPT3001_CONFIGURATION_CRF)) {
302 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
304 dev_err(opt->dev, "failed to read register %02x\n",
309 opt->result_ready = true;
314 /* Disallow IRQ to access the device while lock is active */
315 opt->ok_to_ignore_lock = false;
322 * Disable the end-of-conversion interrupt mechanism by
323 * restoring the low-level limit value (clearing
324 * OPT3001_LOW_LIMIT_EOC_ENABLE). Note that selectively clearing
325 * those enable bits would affect the actual limit value due to
326 * bit-overlap and therefore can't be done.
328 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
329 ret = i2c_smbus_write_word_swapped(opt->client,
333 dev_err(opt->dev, "failed to write register %02x\n",
339 exponent = OPT3001_REG_EXPONENT(opt->result);
340 mantissa = OPT3001_REG_MANTISSA(opt->result);
342 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2);
344 return IIO_VAL_INT_PLUS_MICRO;
347 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2)
350 *val2 = opt->int_time;
352 return IIO_VAL_INT_PLUS_MICRO;
355 static int opt3001_set_int_time(struct opt3001 *opt, int time)
360 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
362 dev_err(opt->dev, "failed to read register %02x\n",
363 OPT3001_CONFIGURATION);
370 case OPT3001_INT_TIME_SHORT:
371 reg &= ~OPT3001_CONFIGURATION_CT;
372 opt->int_time = OPT3001_INT_TIME_SHORT;
374 case OPT3001_INT_TIME_LONG:
375 reg |= OPT3001_CONFIGURATION_CT;
376 opt->int_time = OPT3001_INT_TIME_LONG;
382 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
386 static int opt3001_read_raw(struct iio_dev *iio,
387 struct iio_chan_spec const *chan, int *val, int *val2,
390 struct opt3001 *opt = iio_priv(iio);
393 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
396 if (chan->type != IIO_LIGHT)
399 mutex_lock(&opt->lock);
402 case IIO_CHAN_INFO_PROCESSED:
403 ret = opt3001_get_lux(opt, val, val2);
405 case IIO_CHAN_INFO_INT_TIME:
406 ret = opt3001_get_int_time(opt, val, val2);
412 mutex_unlock(&opt->lock);
417 static int opt3001_write_raw(struct iio_dev *iio,
418 struct iio_chan_spec const *chan, int val, int val2,
421 struct opt3001 *opt = iio_priv(iio);
424 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
427 if (chan->type != IIO_LIGHT)
430 if (mask != IIO_CHAN_INFO_INT_TIME)
436 mutex_lock(&opt->lock);
437 ret = opt3001_set_int_time(opt, val2);
438 mutex_unlock(&opt->lock);
443 static int opt3001_read_event_value(struct iio_dev *iio,
444 const struct iio_chan_spec *chan, enum iio_event_type type,
445 enum iio_event_direction dir, enum iio_event_info info,
448 struct opt3001 *opt = iio_priv(iio);
449 int ret = IIO_VAL_INT_PLUS_MICRO;
451 mutex_lock(&opt->lock);
454 case IIO_EV_DIR_RISING:
455 opt3001_to_iio_ret(opt, opt->high_thresh_exp,
456 opt->high_thresh_mantissa, val, val2);
458 case IIO_EV_DIR_FALLING:
459 opt3001_to_iio_ret(opt, opt->low_thresh_exp,
460 opt->low_thresh_mantissa, val, val2);
466 mutex_unlock(&opt->lock);
471 static int opt3001_write_event_value(struct iio_dev *iio,
472 const struct iio_chan_spec *chan, enum iio_event_type type,
473 enum iio_event_direction dir, enum iio_event_info info,
476 struct opt3001 *opt = iio_priv(iio);
488 mutex_lock(&opt->lock);
490 ret = opt3001_find_scale(opt, val, val2, &exponent);
492 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
496 mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
497 value = (exponent << 12) | mantissa;
500 case IIO_EV_DIR_RISING:
501 reg = OPT3001_HIGH_LIMIT;
502 opt->high_thresh_mantissa = mantissa;
503 opt->high_thresh_exp = exponent;
505 case IIO_EV_DIR_FALLING:
506 reg = OPT3001_LOW_LIMIT;
507 opt->low_thresh_mantissa = mantissa;
508 opt->low_thresh_exp = exponent;
515 ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
517 dev_err(opt->dev, "failed to write register %02x\n", reg);
522 mutex_unlock(&opt->lock);
527 static int opt3001_read_event_config(struct iio_dev *iio,
528 const struct iio_chan_spec *chan, enum iio_event_type type,
529 enum iio_event_direction dir)
531 struct opt3001 *opt = iio_priv(iio);
533 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS;
536 static int opt3001_write_event_config(struct iio_dev *iio,
537 const struct iio_chan_spec *chan, enum iio_event_type type,
538 enum iio_event_direction dir, int state)
540 struct opt3001 *opt = iio_priv(iio);
545 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
548 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
551 mutex_lock(&opt->lock);
553 mode = state ? OPT3001_CONFIGURATION_M_CONTINUOUS
554 : OPT3001_CONFIGURATION_M_SHUTDOWN;
556 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
558 dev_err(opt->dev, "failed to read register %02x\n",
559 OPT3001_CONFIGURATION);
564 opt3001_set_mode(opt, ®, mode);
566 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
569 dev_err(opt->dev, "failed to write register %02x\n",
570 OPT3001_CONFIGURATION);
575 mutex_unlock(&opt->lock);
580 static const struct iio_info opt3001_info = {
581 .attrs = &opt3001_attribute_group,
582 .read_raw = opt3001_read_raw,
583 .write_raw = opt3001_write_raw,
584 .read_event_value = opt3001_read_event_value,
585 .write_event_value = opt3001_write_event_value,
586 .read_event_config = opt3001_read_event_config,
587 .write_event_config = opt3001_write_event_config,
590 static int opt3001_read_id(struct opt3001 *opt)
592 char manufacturer[2];
596 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
598 dev_err(opt->dev, "failed to read register %02x\n",
599 OPT3001_MANUFACTURER_ID);
603 manufacturer[0] = ret >> 8;
604 manufacturer[1] = ret & 0xff;
606 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
608 dev_err(opt->dev, "failed to read register %02x\n",
615 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
616 manufacturer[1], device_id);
621 static int opt3001_configure(struct opt3001 *opt)
626 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
628 dev_err(opt->dev, "failed to read register %02x\n",
629 OPT3001_CONFIGURATION);
635 /* Enable automatic full-scale setting mode */
636 reg &= ~OPT3001_CONFIGURATION_RN_MASK;
637 reg |= OPT3001_CONFIGURATION_RN_AUTO;
639 /* Reflect status of the device's integration time setting */
640 if (reg & OPT3001_CONFIGURATION_CT)
641 opt->int_time = OPT3001_INT_TIME_LONG;
643 opt->int_time = OPT3001_INT_TIME_SHORT;
645 /* Ensure device is in shutdown initially */
646 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
648 /* Configure for latched window-style comparison operation */
649 reg |= OPT3001_CONFIGURATION_L;
650 reg &= ~OPT3001_CONFIGURATION_POL;
651 reg &= ~OPT3001_CONFIGURATION_ME;
652 reg &= ~OPT3001_CONFIGURATION_FC_MASK;
654 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
657 dev_err(opt->dev, "failed to write register %02x\n",
658 OPT3001_CONFIGURATION);
662 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
664 dev_err(opt->dev, "failed to read register %02x\n",
669 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
670 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret);
672 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT);
674 dev_err(opt->dev, "failed to read register %02x\n",
679 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
680 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
685 static irqreturn_t opt3001_irq(int irq, void *_iio)
687 struct iio_dev *iio = _iio;
688 struct opt3001 *opt = iio_priv(iio);
690 bool wake_result_ready_queue = false;
692 if (!opt->ok_to_ignore_lock)
693 mutex_lock(&opt->lock);
695 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
697 dev_err(opt->dev, "failed to read register %02x\n",
698 OPT3001_CONFIGURATION);
702 if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
703 OPT3001_CONFIGURATION_M_CONTINUOUS) {
704 if (ret & OPT3001_CONFIGURATION_FH)
706 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
709 iio_get_time_ns(iio));
710 if (ret & OPT3001_CONFIGURATION_FL)
712 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
715 iio_get_time_ns(iio));
716 } else if (ret & OPT3001_CONFIGURATION_CRF) {
717 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
719 dev_err(opt->dev, "failed to read register %02x\n",
724 opt->result_ready = true;
725 wake_result_ready_queue = true;
729 if (!opt->ok_to_ignore_lock)
730 mutex_unlock(&opt->lock);
732 if (wake_result_ready_queue)
733 wake_up(&opt->result_ready_queue);
738 static int opt3001_probe(struct i2c_client *client)
740 struct device *dev = &client->dev;
744 int irq = client->irq;
747 iio = devm_iio_device_alloc(dev, sizeof(*opt));
752 opt->client = client;
755 mutex_init(&opt->lock);
756 init_waitqueue_head(&opt->result_ready_queue);
757 i2c_set_clientdata(client, iio);
759 ret = opt3001_read_id(opt);
763 ret = opt3001_configure(opt);
767 iio->name = client->name;
768 iio->channels = opt3001_channels;
769 iio->num_channels = ARRAY_SIZE(opt3001_channels);
770 iio->modes = INDIO_DIRECT_MODE;
771 iio->info = &opt3001_info;
773 ret = devm_iio_device_register(dev, iio);
775 dev_err(dev, "failed to register IIO device\n");
779 /* Make use of INT pin only if valid IRQ no. is given */
781 ret = request_threaded_irq(irq, NULL, opt3001_irq,
782 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
785 dev_err(dev, "failed to request IRQ #%d\n", irq);
790 dev_dbg(opt->dev, "enabling interrupt-less operation\n");
796 static void opt3001_remove(struct i2c_client *client)
798 struct iio_dev *iio = i2c_get_clientdata(client);
799 struct opt3001 *opt = iio_priv(iio);
804 free_irq(client->irq, iio);
806 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
808 dev_err(opt->dev, "failed to read register %02x\n",
809 OPT3001_CONFIGURATION);
814 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
816 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
819 dev_err(opt->dev, "failed to write register %02x\n",
820 OPT3001_CONFIGURATION);
824 static const struct i2c_device_id opt3001_id[] = {
826 { } /* Terminating Entry */
828 MODULE_DEVICE_TABLE(i2c, opt3001_id);
830 static const struct of_device_id opt3001_of_match[] = {
831 { .compatible = "ti,opt3001" },
834 MODULE_DEVICE_TABLE(of, opt3001_of_match);
836 static struct i2c_driver opt3001_driver = {
837 .probe_new = opt3001_probe,
838 .remove = opt3001_remove,
839 .id_table = opt3001_id,
843 .of_match_table = opt3001_of_match,
847 module_i2c_driver(opt3001_driver);
849 MODULE_LICENSE("GPL v2");
851 MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");