Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / light / opt3001.c
blob2d48d61909a4de45c1e14c3b6aef2fc2159b560e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /**
3 * opt3001.c - Texas Instruments OPT3001 Light Sensor
5 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com
7 * Author: Andreas Dannenberg <dannenberg@ti.com>
8 * Based on previous work from: Felipe Balbi <balbi@ti.com>
9 */
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 {
74 struct i2c_client *client;
75 struct device *dev;
77 struct mutex lock;
78 bool ok_to_ignore_lock;
79 bool result_ready;
80 wait_queue_head_t result_ready_queue;
81 u16 result;
83 u32 int_time;
84 u32 mode;
86 u16 high_thresh_mantissa;
87 u16 low_thresh_mantissa;
89 u8 high_thresh_exp;
90 u8 low_thresh_exp;
92 bool use_irq;
95 struct opt3001_scale {
96 int val;
97 int val2;
100 static const struct opt3001_scale opt3001_scales[] = {
102 .val = 40,
103 .val2 = 950000,
106 .val = 81,
107 .val2 = 900000,
110 .val = 163,
111 .val2 = 800000,
114 .val = 327,
115 .val2 = 600000,
118 .val = 655,
119 .val2 = 200000,
122 .val = 1310,
123 .val2 = 400000,
126 .val = 2620,
127 .val2 = 800000,
130 .val = 5241,
131 .val2 = 600000,
134 .val = 10483,
135 .val2 = 200000,
138 .val = 20966,
139 .val2 = 400000,
142 .val = 83865,
143 .val2 = 600000,
147 static int opt3001_find_scale(const struct opt3001 *opt, int val,
148 int val2, u8 *exponent)
150 int i;
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
158 * overflows.
160 if ((val * 1000 + val2 / 1000) <=
161 (scale->val * 1000 + scale->val2 / 1000)) {
162 *exponent = i;
163 return 0;
167 return -EINVAL;
170 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent,
171 u16 mantissa, int *val, int *val2)
173 int lux;
175 lux = 10 * (mantissa << exponent);
176 *val = lux / 1000;
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;
183 *reg |= mode;
184 opt->mode = mode;
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,
191 NULL
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[] = {
215 .type = IIO_LIGHT,
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)
226 int ret;
227 u16 mantissa;
228 u16 reg;
229 u8 exponent;
230 u16 value;
231 long timeout;
233 if (opt->use_irq) {
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,
240 OPT3001_LOW_LIMIT,
241 OPT3001_LOW_LIMIT_EOC_ENABLE);
242 if (ret < 0) {
243 dev_err(opt->dev, "failed to write register %02x\n",
244 OPT3001_LOW_LIMIT);
245 return ret;
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);
257 if (ret < 0) {
258 dev_err(opt->dev, "failed to read register %02x\n",
259 OPT3001_CONFIGURATION);
260 goto err;
263 reg = ret;
264 opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SINGLE);
266 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
267 reg);
268 if (ret < 0) {
269 dev_err(opt->dev, "failed to write register %02x\n",
270 OPT3001_CONFIGURATION);
271 goto err;
274 if (opt->use_irq) {
275 /* Wait for the IRQ to indicate the conversion is complete */
276 ret = wait_event_timeout(opt->result_ready_queue,
277 opt->result_ready,
278 msecs_to_jiffies(OPT3001_RESULT_READY_LONG));
279 } else {
280 /* Sleep for result ready time */
281 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ?
282 OPT3001_RESULT_READY_SHORT : OPT3001_RESULT_READY_LONG;
283 msleep(timeout);
285 /* Check result ready flag */
286 ret = i2c_smbus_read_word_swapped(opt->client,
287 OPT3001_CONFIGURATION);
288 if (ret < 0) {
289 dev_err(opt->dev, "failed to read register %02x\n",
290 OPT3001_CONFIGURATION);
291 goto err;
294 if (!(ret & OPT3001_CONFIGURATION_CRF)) {
295 ret = -ETIMEDOUT;
296 goto err;
299 /* Obtain value */
300 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
301 if (ret < 0) {
302 dev_err(opt->dev, "failed to read register %02x\n",
303 OPT3001_RESULT);
304 goto err;
306 opt->result = ret;
307 opt->result_ready = true;
310 err:
311 if (opt->use_irq)
312 /* Disallow IRQ to access the device while lock is active */
313 opt->ok_to_ignore_lock = false;
315 if (ret == 0)
316 return -ETIMEDOUT;
317 else if (ret < 0)
318 return ret;
320 if (opt->use_irq) {
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,
330 OPT3001_LOW_LIMIT,
331 value);
332 if (ret < 0) {
333 dev_err(opt->dev, "failed to write register %02x\n",
334 OPT3001_LOW_LIMIT);
335 return ret;
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)
349 *val = 0;
350 *val2 = opt->int_time;
352 return IIO_VAL_INT_PLUS_MICRO;
355 static int opt3001_set_int_time(struct opt3001 *opt, int time)
357 int ret;
358 u16 reg;
360 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
361 if (ret < 0) {
362 dev_err(opt->dev, "failed to read register %02x\n",
363 OPT3001_CONFIGURATION);
364 return ret;
367 reg = ret;
369 switch (time) {
370 case OPT3001_INT_TIME_SHORT:
371 reg &= ~OPT3001_CONFIGURATION_CT;
372 opt->int_time = OPT3001_INT_TIME_SHORT;
373 break;
374 case OPT3001_INT_TIME_LONG:
375 reg |= OPT3001_CONFIGURATION_CT;
376 opt->int_time = OPT3001_INT_TIME_LONG;
377 break;
378 default:
379 return -EINVAL;
382 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
383 reg);
386 static int opt3001_read_raw(struct iio_dev *iio,
387 struct iio_chan_spec const *chan, int *val, int *val2,
388 long mask)
390 struct opt3001 *opt = iio_priv(iio);
391 int ret;
393 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
394 return -EBUSY;
396 if (chan->type != IIO_LIGHT)
397 return -EINVAL;
399 mutex_lock(&opt->lock);
401 switch (mask) {
402 case IIO_CHAN_INFO_PROCESSED:
403 ret = opt3001_get_lux(opt, val, val2);
404 break;
405 case IIO_CHAN_INFO_INT_TIME:
406 ret = opt3001_get_int_time(opt, val, val2);
407 break;
408 default:
409 ret = -EINVAL;
412 mutex_unlock(&opt->lock);
414 return ret;
417 static int opt3001_write_raw(struct iio_dev *iio,
418 struct iio_chan_spec const *chan, int val, int val2,
419 long mask)
421 struct opt3001 *opt = iio_priv(iio);
422 int ret;
424 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
425 return -EBUSY;
427 if (chan->type != IIO_LIGHT)
428 return -EINVAL;
430 if (mask != IIO_CHAN_INFO_INT_TIME)
431 return -EINVAL;
433 if (val != 0)
434 return -EINVAL;
436 mutex_lock(&opt->lock);
437 ret = opt3001_set_int_time(opt, val2);
438 mutex_unlock(&opt->lock);
440 return ret;
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,
446 int *val, int *val2)
448 struct opt3001 *opt = iio_priv(iio);
449 int ret = IIO_VAL_INT_PLUS_MICRO;
451 mutex_lock(&opt->lock);
453 switch (dir) {
454 case IIO_EV_DIR_RISING:
455 opt3001_to_iio_ret(opt, opt->high_thresh_exp,
456 opt->high_thresh_mantissa, val, val2);
457 break;
458 case IIO_EV_DIR_FALLING:
459 opt3001_to_iio_ret(opt, opt->low_thresh_exp,
460 opt->low_thresh_mantissa, val, val2);
461 break;
462 default:
463 ret = -EINVAL;
466 mutex_unlock(&opt->lock);
468 return ret;
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,
474 int val, int val2)
476 struct opt3001 *opt = iio_priv(iio);
477 int ret;
479 u16 mantissa;
480 u16 value;
481 u16 reg;
483 u8 exponent;
485 if (val < 0)
486 return -EINVAL;
488 mutex_lock(&opt->lock);
490 ret = opt3001_find_scale(opt, val, val2, &exponent);
491 if (ret < 0) {
492 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2);
493 goto err;
496 mantissa = (((val * 1000) + (val2 / 1000)) / 10) >> exponent;
497 value = (exponent << 12) | mantissa;
499 switch (dir) {
500 case IIO_EV_DIR_RISING:
501 reg = OPT3001_HIGH_LIMIT;
502 opt->high_thresh_mantissa = mantissa;
503 opt->high_thresh_exp = exponent;
504 break;
505 case IIO_EV_DIR_FALLING:
506 reg = OPT3001_LOW_LIMIT;
507 opt->low_thresh_mantissa = mantissa;
508 opt->low_thresh_exp = exponent;
509 break;
510 default:
511 ret = -EINVAL;
512 goto err;
515 ret = i2c_smbus_write_word_swapped(opt->client, reg, value);
516 if (ret < 0) {
517 dev_err(opt->dev, "failed to write register %02x\n", reg);
518 goto err;
521 err:
522 mutex_unlock(&opt->lock);
524 return ret;
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);
541 int ret;
542 u16 mode;
543 u16 reg;
545 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS)
546 return 0;
548 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN)
549 return 0;
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);
557 if (ret < 0) {
558 dev_err(opt->dev, "failed to read register %02x\n",
559 OPT3001_CONFIGURATION);
560 goto err;
563 reg = ret;
564 opt3001_set_mode(opt, &reg, mode);
566 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
567 reg);
568 if (ret < 0) {
569 dev_err(opt->dev, "failed to write register %02x\n",
570 OPT3001_CONFIGURATION);
571 goto err;
574 err:
575 mutex_unlock(&opt->lock);
577 return ret;
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];
593 u16 device_id;
594 int ret;
596 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID);
597 if (ret < 0) {
598 dev_err(opt->dev, "failed to read register %02x\n",
599 OPT3001_MANUFACTURER_ID);
600 return ret;
603 manufacturer[0] = ret >> 8;
604 manufacturer[1] = ret & 0xff;
606 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID);
607 if (ret < 0) {
608 dev_err(opt->dev, "failed to read register %02x\n",
609 OPT3001_DEVICE_ID);
610 return ret;
613 device_id = ret;
615 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0],
616 manufacturer[1], device_id);
618 return 0;
621 static int opt3001_configure(struct opt3001 *opt)
623 int ret;
624 u16 reg;
626 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
627 if (ret < 0) {
628 dev_err(opt->dev, "failed to read register %02x\n",
629 OPT3001_CONFIGURATION);
630 return ret;
633 reg = ret;
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;
642 else
643 opt->int_time = OPT3001_INT_TIME_SHORT;
645 /* Ensure device is in shutdown initially */
646 opt3001_set_mode(opt, &reg, 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,
655 reg);
656 if (ret < 0) {
657 dev_err(opt->dev, "failed to write register %02x\n",
658 OPT3001_CONFIGURATION);
659 return ret;
662 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT);
663 if (ret < 0) {
664 dev_err(opt->dev, "failed to read register %02x\n",
665 OPT3001_LOW_LIMIT);
666 return ret;
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);
673 if (ret < 0) {
674 dev_err(opt->dev, "failed to read register %02x\n",
675 OPT3001_HIGH_LIMIT);
676 return ret;
679 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret);
680 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret);
682 return 0;
685 static irqreturn_t opt3001_irq(int irq, void *_iio)
687 struct iio_dev *iio = _iio;
688 struct opt3001 *opt = iio_priv(iio);
689 int ret;
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);
696 if (ret < 0) {
697 dev_err(opt->dev, "failed to read register %02x\n",
698 OPT3001_CONFIGURATION);
699 goto out;
702 if ((ret & OPT3001_CONFIGURATION_M_MASK) ==
703 OPT3001_CONFIGURATION_M_CONTINUOUS) {
704 if (ret & OPT3001_CONFIGURATION_FH)
705 iio_push_event(iio,
706 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
707 IIO_EV_TYPE_THRESH,
708 IIO_EV_DIR_RISING),
709 iio_get_time_ns(iio));
710 if (ret & OPT3001_CONFIGURATION_FL)
711 iio_push_event(iio,
712 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
713 IIO_EV_TYPE_THRESH,
714 IIO_EV_DIR_FALLING),
715 iio_get_time_ns(iio));
716 } else if (ret & OPT3001_CONFIGURATION_CRF) {
717 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
718 if (ret < 0) {
719 dev_err(opt->dev, "failed to read register %02x\n",
720 OPT3001_RESULT);
721 goto out;
723 opt->result = ret;
724 opt->result_ready = true;
725 wake_result_ready_queue = true;
728 out:
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);
735 return IRQ_HANDLED;
738 static int opt3001_probe(struct i2c_client *client,
739 const struct i2c_device_id *id)
741 struct device *dev = &client->dev;
743 struct iio_dev *iio;
744 struct opt3001 *opt;
745 int irq = client->irq;
746 int ret;
748 iio = devm_iio_device_alloc(dev, sizeof(*opt));
749 if (!iio)
750 return -ENOMEM;
752 opt = iio_priv(iio);
753 opt->client = client;
754 opt->dev = dev;
756 mutex_init(&opt->lock);
757 init_waitqueue_head(&opt->result_ready_queue);
758 i2c_set_clientdata(client, iio);
760 ret = opt3001_read_id(opt);
761 if (ret)
762 return ret;
764 ret = opt3001_configure(opt);
765 if (ret)
766 return ret;
768 iio->name = client->name;
769 iio->channels = opt3001_channels;
770 iio->num_channels = ARRAY_SIZE(opt3001_channels);
771 iio->modes = INDIO_DIRECT_MODE;
772 iio->info = &opt3001_info;
774 ret = devm_iio_device_register(dev, iio);
775 if (ret) {
776 dev_err(dev, "failed to register IIO device\n");
777 return ret;
780 /* Make use of INT pin only if valid IRQ no. is given */
781 if (irq > 0) {
782 ret = request_threaded_irq(irq, NULL, opt3001_irq,
783 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
784 "opt3001", iio);
785 if (ret) {
786 dev_err(dev, "failed to request IRQ #%d\n", irq);
787 return ret;
789 opt->use_irq = true;
790 } else {
791 dev_dbg(opt->dev, "enabling interrupt-less operation\n");
794 return 0;
797 static int opt3001_remove(struct i2c_client *client)
799 struct iio_dev *iio = i2c_get_clientdata(client);
800 struct opt3001 *opt = iio_priv(iio);
801 int ret;
802 u16 reg;
804 if (opt->use_irq)
805 free_irq(client->irq, iio);
807 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
808 if (ret < 0) {
809 dev_err(opt->dev, "failed to read register %02x\n",
810 OPT3001_CONFIGURATION);
811 return ret;
814 reg = ret;
815 opt3001_set_mode(opt, &reg, OPT3001_CONFIGURATION_M_SHUTDOWN);
817 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION,
818 reg);
819 if (ret < 0) {
820 dev_err(opt->dev, "failed to write register %02x\n",
821 OPT3001_CONFIGURATION);
822 return ret;
825 return 0;
828 static const struct i2c_device_id opt3001_id[] = {
829 { "opt3001", 0 },
830 { } /* Terminating Entry */
832 MODULE_DEVICE_TABLE(i2c, opt3001_id);
834 static const struct of_device_id opt3001_of_match[] = {
835 { .compatible = "ti,opt3001" },
838 MODULE_DEVICE_TABLE(of, opt3001_of_match);
840 static struct i2c_driver opt3001_driver = {
841 .probe = opt3001_probe,
842 .remove = opt3001_remove,
843 .id_table = opt3001_id,
845 .driver = {
846 .name = "opt3001",
847 .of_match_table = opt3001_of_match,
851 module_i2c_driver(opt3001_driver);
853 MODULE_LICENSE("GPL v2");
854 MODULE_AUTHOR("Andreas Dannenberg <dannenberg@ti.com>");
855 MODULE_DESCRIPTION("Texas Instruments OPT3001 Light Sensor Driver");