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
73 struct opt3001_scale {
78 struct opt3001_chip_info {
79 const struct iio_chan_spec (*channels)[2];
80 enum iio_chan_type chan_type;
83 const struct opt3001_scale (*scales)[12];
85 * Factor as specified by conversion equation in datasheet.
86 * eg. 0.01 (scaled to integer 10) for opt3001.
90 * Factor to compensate for potentially scaled factor_whole.
94 * Factor used to align decimal part of proccessed value to six decimal
103 struct i2c_client *client;
107 bool ok_to_ignore_lock;
109 wait_queue_head_t result_ready_queue;
111 const struct opt3001_chip_info *chip_info;
116 u16 high_thresh_mantissa;
117 u16 low_thresh_mantissa;
125 static const struct opt3001_scale opt3001_scales[] = {
176 static const struct opt3001_scale opt3002_scales[] = {
227 static int opt3001_find_scale(const struct opt3001 *opt, int val,
228 int val2, u8 *exponent)
231 for (i = 0; i < ARRAY_SIZE(*opt->chip_info->scales); i++) {
232 const struct opt3001_scale *scale = &(*opt->chip_info->scales)[i];
234 * Compare the integer and micro parts to determine value scale.
236 if (val < scale->val ||
237 (val == scale->val && val2 <= scale->val2)) {
246 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
247 u16 mantissa, int *val, int *val2)
250 int whole = opt->chip_info->factor_whole;
251 int integer = opt->chip_info->factor_integer;
252 int decimal = opt->chip_info->factor_decimal;
254 ret = whole * (mantissa << exponent);
255 *val = ret / integer;
256 *val2 = (ret - (*val * integer)) * decimal;
259 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode)
261 *reg &= ~OPT3001_CONFIGURATION_M_MASK;
266 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.8");
268 static struct attribute *opt3001_attributes[] = {
269 &iio_const_attr_integration_time_available.dev_attr.attr,
273 static const struct attribute_group opt3001_attribute_group = {
274 .attrs = opt3001_attributes,
277 static const struct iio_event_spec opt3001_event_spec[] = {
279 .type = IIO_EV_TYPE_THRESH,
280 .dir = IIO_EV_DIR_RISING,
281 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
282 BIT(IIO_EV_INFO_ENABLE),
285 .type = IIO_EV_TYPE_THRESH,
286 .dir = IIO_EV_DIR_FALLING,
287 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
288 BIT(IIO_EV_INFO_ENABLE),
292 static const struct iio_chan_spec opt3001_channels[] = {
295 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
296 BIT(IIO_CHAN_INFO_INT_TIME),
297 .event_spec = opt3001_event_spec,
298 .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
300 IIO_CHAN_SOFT_TIMESTAMP(1),
303 static const struct iio_chan_spec opt3002_channels[] = {
305 .type = IIO_INTENSITY,
306 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
307 BIT(IIO_CHAN_INFO_INT_TIME),
308 .event_spec = opt3001_event_spec,
309 .num_event_specs = ARRAY_SIZE(opt3001_event_spec),
311 IIO_CHAN_SOFT_TIMESTAMP(1),
314 static int opt3001_get_processed(struct opt3001 *opt, int *val, int *val2)
325 * Enable the end-of-conversion interrupt mechanism. Note that
326 * doing so will overwrite the low-level limit value however we
327 * will restore this value later on.
329 ret = i2c_smbus_write_word_swapped(opt->client,
331 OPT3001_LOW_LIMIT_EOC_ENABLE);
333 dev_err(opt->dev, "failed to write register %02x\n",
338 /* Allow IRQ to access the device despite lock being set */
339 opt->ok_to_ignore_lock = true;
342 /* Reset data-ready indicator flag */
343 opt->result_ready = false;
345 /* Configure for single-conversion mode and start a new conversion */
346 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
348 dev_err(opt->dev, "failed to read register %02x\n",
349 OPT3001_CONFIGURATION);
354 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE);
356 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
359 dev_err(opt->dev, "failed to write register %02x\n",
360 OPT3001_CONFIGURATION);
365 /* Wait for the IRQ to indicate the conversion is complete */
366 ret = wait_event_timeout(opt->result_ready_queue,
368 msecs_to_jiffies(OPT3001_RESULT_READY_LONG));
372 /* Sleep for result ready time */
373 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ?
374 OPT3001_RESULT_READY_SHORT : OPT3001_RESULT_READY_LONG;
377 /* Check result ready flag */
378 ret = i2c_smbus_read_word_swapped(opt->client,
379 OPT3001_CONFIGURATION);
381 dev_err(opt->dev, "failed to read register %02x\n",
382 OPT3001_CONFIGURATION);
386 if (!(ret & OPT3001_CONFIGURATION_CRF)) {
392 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
394 dev_err(opt->dev, "failed to read register %02x\n",
399 opt->result_ready = true;
404 /* Disallow IRQ to access the device while lock is active */
405 opt->ok_to_ignore_lock = false;
412 * Disable the end-of-conversion interrupt mechanism by
413 * restoring the low-level limit value (clearing
414 * OPT3001_LOW_LIMIT_EOC_ENABLE). Note that selectively clearing
415 * those enable bits would affect the actual limit value due to
416 * bit-overlap and therefore can't be done.
418 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
419 ret = i2c_smbus_write_word_swapped(opt->client,
423 dev_err(opt->dev, "failed to write register %02x\n",
429 exponent = OPT3001_REG_EXPONENT(opt->result);
430 mantissa = OPT3001_REG_MANTISSA(opt->result);
432 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2);
434 return IIO_VAL_INT_PLUS_MICRO;
437 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2)
440 *val2 = opt->int_time;
442 return IIO_VAL_INT_PLUS_MICRO;
445 static int opt3001_set_int_time(struct opt3001 *opt, int time)
450 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
452 dev_err(opt->dev, "failed to read register %02x\n",
453 OPT3001_CONFIGURATION);
460 case OPT3001_INT_TIME_SHORT:
461 reg &= ~OPT3001_CONFIGURATION_CT;
462 opt->int_time = OPT3001_INT_TIME_SHORT;
464 case OPT3001_INT_TIME_LONG:
465 reg |= OPT3001_CONFIGURATION_CT;
466 opt->int_time = OPT3001_INT_TIME_LONG;
472 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
476 static int opt3001_read_raw(struct iio_dev *iio,
477 struct iio_chan_spec const *chan, int *val, int *val2,
480 struct opt3001 *opt = iio_priv(iio);
483 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
486 if (chan->type != opt->chip_info->chan_type)
489 mutex_lock(&opt->lock);
492 case IIO_CHAN_INFO_RAW:
493 case IIO_CHAN_INFO_PROCESSED:
494 ret = opt3001_get_processed(opt, val, val2);
496 case IIO_CHAN_INFO_INT_TIME:
497 ret = opt3001_get_int_time(opt, val, val2);
503 mutex_unlock(&opt->lock);
508 static int opt3001_write_raw(struct iio_dev *iio,
509 struct iio_chan_spec const *chan, int val, int val2,
512 struct opt3001 *opt = iio_priv(iio);
515 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
518 if (chan->type != opt->chip_info->chan_type)
521 if (mask != IIO_CHAN_INFO_INT_TIME)
527 mutex_lock(&opt->lock);
528 ret = opt3001_set_int_time(opt, val2);
529 mutex_unlock(&opt->lock);
534 static int opt3001_read_event_value(struct iio_dev *iio,
535 const struct iio_chan_spec *chan, enum iio_event_type type,
536 enum iio_event_direction dir, enum iio_event_info info,
539 struct opt3001 *opt = iio_priv(iio);
540 int ret = IIO_VAL_INT_PLUS_MICRO;
542 mutex_lock(&opt->lock);
545 case IIO_EV_DIR_RISING:
546 opt3001_to_iio_ret(opt, opt->high_thresh_exp,
547 opt->high_thresh_mantissa, val, val2);
549 case IIO_EV_DIR_FALLING:
550 opt3001_to_iio_ret(opt, opt->low_thresh_exp,
551 opt->low_thresh_mantissa, val, val2);
557 mutex_unlock(&opt->lock);
562 static int opt3001_write_event_value(struct iio_dev *iio,
563 const struct iio_chan_spec *chan, enum iio_event_type type,
564 enum iio_event_direction dir, enum iio_event_info info,
567 struct opt3001 *opt = iio_priv(iio);
582 mutex_lock(&opt->lock);
584 ret = opt3001_find_scale(opt, val, val2, &exponent);
586 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
590 whole = opt->chip_info->factor_whole;
591 integer = opt->chip_info->factor_integer;
592 decimal = opt->chip_info->factor_decimal;
594 mantissa = (((val * integer) + (val2 / decimal)) / whole) >> exponent;
596 value = (exponent << 12) | mantissa;
599 case IIO_EV_DIR_RISING:
600 reg = OPT3001_HIGH_LIMIT;
601 opt->high_thresh_mantissa = mantissa;
602 opt->high_thresh_exp = exponent;
604 case IIO_EV_DIR_FALLING:
605 reg = OPT3001_LOW_LIMIT;
606 opt->low_thresh_mantissa = mantissa;
607 opt->low_thresh_exp = exponent;
614 ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
616 dev_err(opt->dev, "failed to write register %02x\n", reg);
621 mutex_unlock(&opt->lock);
626 static int opt3001_read_event_config(struct iio_dev *iio,
627 const struct iio_chan_spec *chan, enum iio_event_type type,
628 enum iio_event_direction dir)
630 struct opt3001 *opt = iio_priv(iio);
632 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS;
635 static int opt3001_write_event_config(struct iio_dev *iio,
636 const struct iio_chan_spec *chan, enum iio_event_type type,
637 enum iio_event_direction dir, bool state)
639 struct opt3001 *opt = iio_priv(iio);
644 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
647 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
650 mutex_lock(&opt->lock);
652 mode = state ? OPT3001_CONFIGURATION_M_CONTINUOUS
653 : OPT3001_CONFIGURATION_M_SHUTDOWN;
655 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
657 dev_err(opt->dev, "failed to read register %02x\n",
658 OPT3001_CONFIGURATION);
663 opt3001_set_mode(opt, ®, mode);
665 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
668 dev_err(opt->dev, "failed to write register %02x\n",
669 OPT3001_CONFIGURATION);
674 mutex_unlock(&opt->lock);
679 static const struct iio_info opt3001_info = {
680 .attrs = &opt3001_attribute_group,
681 .read_raw = opt3001_read_raw,
682 .write_raw = opt3001_write_raw,
683 .read_event_value = opt3001_read_event_value,
684 .write_event_value = opt3001_write_event_value,
685 .read_event_config = opt3001_read_event_config,
686 .write_event_config = opt3001_write_event_config,
689 static int opt3001_read_id(struct opt3001 *opt)
691 char manufacturer[2];
695 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
697 dev_err(opt->dev, "failed to read register %02x\n",
698 OPT3001_MANUFACTURER_ID);
702 manufacturer[0] = ret >> 8;
703 manufacturer[1] = ret & 0xff;
705 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
707 dev_err(opt->dev, "failed to read register %02x\n",
714 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
715 manufacturer[1], device_id);
720 static int opt3001_configure(struct opt3001 *opt)
725 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
727 dev_err(opt->dev, "failed to read register %02x\n",
728 OPT3001_CONFIGURATION);
734 /* Enable automatic full-scale setting mode */
735 reg &= ~OPT3001_CONFIGURATION_RN_MASK;
736 reg |= OPT3001_CONFIGURATION_RN_AUTO;
738 /* Reflect status of the device's integration time setting */
739 if (reg & OPT3001_CONFIGURATION_CT)
740 opt->int_time = OPT3001_INT_TIME_LONG;
742 opt->int_time = OPT3001_INT_TIME_SHORT;
744 /* Ensure device is in shutdown initially */
745 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
747 /* Configure for latched window-style comparison operation */
748 reg |= OPT3001_CONFIGURATION_L;
749 reg &= ~OPT3001_CONFIGURATION_POL;
750 reg &= ~OPT3001_CONFIGURATION_ME;
751 reg &= ~OPT3001_CONFIGURATION_FC_MASK;
753 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
756 dev_err(opt->dev, "failed to write register %02x\n",
757 OPT3001_CONFIGURATION);
761 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
763 dev_err(opt->dev, "failed to read register %02x\n",
768 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
769 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret);
771 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT);
773 dev_err(opt->dev, "failed to read register %02x\n",
778 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
779 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
784 static irqreturn_t opt3001_irq(int irq, void *_iio)
786 struct iio_dev *iio = _iio;
787 struct opt3001 *opt = iio_priv(iio);
789 bool wake_result_ready_queue = false;
790 enum iio_chan_type chan_type = opt->chip_info->chan_type;
792 if (!opt->ok_to_ignore_lock)
793 mutex_lock(&opt->lock);
795 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
797 dev_err(opt->dev, "failed to read register %02x\n",
798 OPT3001_CONFIGURATION);
802 if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
803 OPT3001_CONFIGURATION_M_CONTINUOUS) {
804 if (ret & OPT3001_CONFIGURATION_FH)
806 IIO_UNMOD_EVENT_CODE(chan_type, 0,
809 iio_get_time_ns(iio));
810 if (ret & OPT3001_CONFIGURATION_FL)
812 IIO_UNMOD_EVENT_CODE(chan_type, 0,
815 iio_get_time_ns(iio));
816 } else if (ret & OPT3001_CONFIGURATION_CRF) {
817 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
819 dev_err(opt->dev, "failed to read register %02x\n",
824 opt->result_ready = true;
825 wake_result_ready_queue = true;
829 if (!opt->ok_to_ignore_lock)
830 mutex_unlock(&opt->lock);
832 if (wake_result_ready_queue)
833 wake_up(&opt->result_ready_queue);
838 static int opt3001_probe(struct i2c_client *client)
840 struct device *dev = &client->dev;
844 int irq = client->irq;
847 iio = devm_iio_device_alloc(dev, sizeof(*opt));
852 opt->client = client;
854 opt->chip_info = i2c_get_match_data(client);
856 mutex_init(&opt->lock);
857 init_waitqueue_head(&opt->result_ready_queue);
858 i2c_set_clientdata(client, iio);
860 if (opt->chip_info->has_id) {
861 ret = opt3001_read_id(opt);
866 ret = opt3001_configure(opt);
870 iio->name = client->name;
871 iio->channels = *opt->chip_info->channels;
872 iio->num_channels = opt->chip_info->num_channels;
873 iio->modes = INDIO_DIRECT_MODE;
874 iio->info = &opt3001_info;
876 ret = devm_iio_device_register(dev, iio);
878 dev_err(dev, "failed to register IIO device\n");
882 /* Make use of INT pin only if valid IRQ no. is given */
884 ret = request_threaded_irq(irq, NULL, opt3001_irq,
885 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
888 dev_err(dev, "failed to request IRQ #%d\n", irq);
893 dev_dbg(opt->dev, "enabling interrupt-less operation\n");
899 static void opt3001_remove(struct i2c_client *client)
901 struct iio_dev *iio = i2c_get_clientdata(client);
902 struct opt3001 *opt = iio_priv(iio);
907 free_irq(client->irq, iio);
909 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
911 dev_err(opt->dev, "failed to read register %02x\n",
912 OPT3001_CONFIGURATION);
917 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN);
919 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
922 dev_err(opt->dev, "failed to write register %02x\n",
923 OPT3001_CONFIGURATION);
927 static const struct opt3001_chip_info opt3001_chip_information = {
928 .channels = &opt3001_channels,
929 .chan_type = IIO_LIGHT,
930 .num_channels = ARRAY_SIZE(opt3001_channels),
931 .scales = &opt3001_scales,
933 .factor_integer = 1000,
934 .factor_decimal = 1000,
938 static const struct opt3001_chip_info opt3002_chip_information = {
939 .channels = &opt3002_channels,
940 .chan_type = IIO_INTENSITY,
941 .num_channels = ARRAY_SIZE(opt3002_channels),
942 .scales = &opt3002_scales,
944 .factor_integer = 10,
945 .factor_decimal = 100000,
949 static const struct i2c_device_id opt3001_id[] = {
950 { "opt3001", (kernel_ulong_t)&opt3001_chip_information },
951 { "opt3002", (kernel_ulong_t)&opt3002_chip_information },
952 { } /* Terminating Entry */
954 MODULE_DEVICE_TABLE(i2c, opt3001_id);
956 static const struct of_device_id opt3001_of_match[] = {
957 { .compatible = "ti,opt3001", .data = &opt3001_chip_information },
958 { .compatible = "ti,opt3002", .data = &opt3002_chip_information },
961 MODULE_DEVICE_TABLE(of, opt3001_of_match);
963 static struct i2c_driver opt3001_driver = {
964 .probe = opt3001_probe,
965 .remove = opt3001_remove,
966 .id_table = opt3001_id,
970 .of_match_table = opt3001_of_match,
974 module_i2c_driver(opt3001_driver);
976 MODULE_LICENSE("GPL v2");
978 MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");