1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Murata IRS-D200 PIR sensor.
5 * Copyright (C) 2023 Axis Communications AB
8 #include <linux/unaligned.h>
9 #include <linux/bitfield.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/events.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/types.h>
22 #define IRS_DRV_NAME "irsd200"
25 #define IRS_REG_OP 0x00 /* Operation mode. */
26 #define IRS_REG_DATA_LO 0x02 /* Sensor data LSB. */
27 #define IRS_REG_DATA_HI 0x03 /* Sensor data MSB. */
28 #define IRS_REG_STATUS 0x04 /* Interrupt status. */
29 #define IRS_REG_COUNT 0x05 /* Count of exceeding threshold. */
30 #define IRS_REG_DATA_RATE 0x06 /* Output data rate. */
31 #define IRS_REG_FILTER 0x07 /* High-pass and low-pass filter. */
32 #define IRS_REG_INTR 0x09 /* Interrupt mode. */
33 #define IRS_REG_NR_COUNT 0x0a /* Number of counts before interrupt. */
34 #define IRS_REG_THR_HI 0x0b /* Upper threshold. */
35 #define IRS_REG_THR_LO 0x0c /* Lower threshold. */
36 #define IRS_REG_TIMER_LO 0x0d /* Timer setting LSB. */
37 #define IRS_REG_TIMER_HI 0x0e /* Timer setting MSB. */
39 /* Interrupt status bits. */
40 #define IRS_INTR_DATA 0 /* Data update. */
41 #define IRS_INTR_TIMER 1 /* Timer expiration. */
42 #define IRS_INTR_COUNT_THR_AND 2 /* Count "AND" threshold. */
43 #define IRS_INTR_COUNT_THR_OR 3 /* Count "OR" threshold. */
45 /* Operation states. */
46 #define IRS_OP_ACTIVE 0x00
47 #define IRS_OP_SLEEP 0x01
50 * Quantization scale value for threshold. Used for conversion from/to register
53 #define IRS_THR_QUANT_SCALE 128
55 #define IRS_UPPER_COUNT(count) FIELD_GET(GENMASK(7, 4), count)
56 #define IRS_LOWER_COUNT(count) FIELD_GET(GENMASK(3, 0), count)
58 /* Index corresponds to the value of IRS_REG_DATA_RATE register. */
59 static const int irsd200_data_rates
[] = {
64 /* Index corresponds to the (field) value of IRS_REG_FILTER register. */
65 static const unsigned int irsd200_lp_filter_freq
[] = {
71 * Index corresponds to the (field) value of IRS_REG_FILTER register. Note that
72 * this represents a fractional value (e.g the first value corresponds to 3 / 10
75 static const unsigned int irsd200_hp_filter_freq
[][2] = {
80 /* Register fields. */
81 enum irsd200_regfield
{
84 /* Timer interrupt. */
86 /* AND count threshold interrupt. */
87 IRS_REGF_INTR_COUNT_THR_AND
,
88 /* OR count threshold interrupt. */
89 IRS_REGF_INTR_COUNT_THR_OR
,
91 /* Low-pass filter frequency. */
93 /* High-pass filter frequency. */
100 static const struct reg_field irsd200_regfields
[] = {
101 [IRS_REGF_INTR_DATA
] =
102 REG_FIELD(IRS_REG_INTR
, IRS_INTR_DATA
, IRS_INTR_DATA
),
103 [IRS_REGF_INTR_TIMER
] =
104 REG_FIELD(IRS_REG_INTR
, IRS_INTR_TIMER
, IRS_INTR_TIMER
),
105 [IRS_REGF_INTR_COUNT_THR_AND
] = REG_FIELD(
106 IRS_REG_INTR
, IRS_INTR_COUNT_THR_AND
, IRS_INTR_COUNT_THR_AND
),
107 [IRS_REGF_INTR_COUNT_THR_OR
] = REG_FIELD(
108 IRS_REG_INTR
, IRS_INTR_COUNT_THR_OR
, IRS_INTR_COUNT_THR_OR
),
110 [IRS_REGF_LP_FILTER
] = REG_FIELD(IRS_REG_FILTER
, 1, 1),
111 [IRS_REGF_HP_FILTER
] = REG_FIELD(IRS_REG_FILTER
, 0, 0),
114 static const struct regmap_config irsd200_regmap_config
= {
117 .max_register
= IRS_REG_TIMER_HI
,
120 struct irsd200_data
{
121 struct regmap
*regmap
;
122 struct regmap_field
*regfields
[IRS_REGF_MAX
];
126 static int irsd200_setup(struct irsd200_data
*data
)
131 /* Disable all interrupt sources. */
132 ret
= regmap_write(data
->regmap
, IRS_REG_INTR
, 0);
134 dev_err(data
->dev
, "Could not set interrupt sources (%d)\n",
139 /* Set operation to active. */
140 ret
= regmap_write(data
->regmap
, IRS_REG_OP
, IRS_OP_ACTIVE
);
142 dev_err(data
->dev
, "Could not set operation mode (%d)\n", ret
);
146 /* Clear threshold count. */
147 ret
= regmap_read(data
->regmap
, IRS_REG_COUNT
, &val
);
149 dev_err(data
->dev
, "Could not clear threshold count (%d)\n",
155 ret
= regmap_write(data
->regmap
, IRS_REG_STATUS
, 0x0f);
157 dev_err(data
->dev
, "Could not clear status (%d)\n", ret
);
164 static int irsd200_read_threshold(struct irsd200_data
*data
,
165 enum iio_event_direction dir
, int *val
)
172 /* Set quantization scale. */
173 if (dir
== IIO_EV_DIR_RISING
) {
174 scale
= IRS_THR_QUANT_SCALE
;
175 reg
= IRS_REG_THR_HI
;
176 } else if (dir
== IIO_EV_DIR_FALLING
) {
177 scale
= -IRS_THR_QUANT_SCALE
;
178 reg
= IRS_REG_THR_LO
;
183 ret
= regmap_read(data
->regmap
, reg
, ®val
);
185 dev_err(data
->dev
, "Could not read threshold (%d)\n", ret
);
189 *val
= ((int)regval
) * scale
;
194 static int irsd200_write_threshold(struct irsd200_data
*data
,
195 enum iio_event_direction dir
, int val
)
202 /* Set quantization scale. */
203 if (dir
== IIO_EV_DIR_RISING
) {
207 scale
= IRS_THR_QUANT_SCALE
;
208 reg
= IRS_REG_THR_HI
;
209 } else if (dir
== IIO_EV_DIR_FALLING
) {
213 scale
= -IRS_THR_QUANT_SCALE
;
214 reg
= IRS_REG_THR_LO
;
219 regval
= val
/ scale
;
221 if (regval
>= BIT(8))
224 ret
= regmap_write(data
->regmap
, reg
, regval
);
226 dev_err(data
->dev
, "Could not write threshold (%d)\n", ret
);
233 static int irsd200_read_data(struct irsd200_data
*data
, s16
*val
)
238 ret
= regmap_bulk_read(data
->regmap
, IRS_REG_DATA_LO
, &buf
,
241 dev_err(data
->dev
, "Could not bulk read data (%d)\n", ret
);
245 *val
= le16_to_cpu(buf
);
250 static int irsd200_read_data_rate(struct irsd200_data
*data
, int *val
)
255 ret
= regmap_read(data
->regmap
, IRS_REG_DATA_RATE
, ®val
);
257 dev_err(data
->dev
, "Could not read data rate (%d)\n", ret
);
261 if (regval
>= ARRAY_SIZE(irsd200_data_rates
))
264 *val
= irsd200_data_rates
[regval
];
269 static int irsd200_write_data_rate(struct irsd200_data
*data
, int val
)
274 for (idx
= 0; idx
< ARRAY_SIZE(irsd200_data_rates
); ++idx
) {
275 if (irsd200_data_rates
[idx
] == val
)
279 if (idx
== ARRAY_SIZE(irsd200_data_rates
))
282 ret
= regmap_write(data
->regmap
, IRS_REG_DATA_RATE
, idx
);
284 dev_err(data
->dev
, "Could not write data rate (%d)\n", ret
);
289 * Data sheet says the device needs 3 seconds of settling time. The
290 * device operates normally during this period though. This is more of a
291 * "guarantee" than trying to prevent other user space reads/writes.
298 static int irsd200_read_timer(struct irsd200_data
*data
, int *val
, int *val2
)
303 ret
= regmap_bulk_read(data
->regmap
, IRS_REG_TIMER_LO
, &buf
,
306 dev_err(data
->dev
, "Could not bulk read timer (%d)\n", ret
);
310 ret
= irsd200_read_data_rate(data
, val2
);
314 *val
= le16_to_cpu(buf
);
319 static int irsd200_write_timer(struct irsd200_data
*data
, int val
, int val2
)
326 if (val
< 0 || val2
< 0)
329 ret
= irsd200_read_data_rate(data
, &data_rate
);
333 /* Quantize from seconds. */
334 regval
= val
* data_rate
+ (val2
* data_rate
) / 1000000;
336 /* Value is 10 bits. */
337 if (regval
>= BIT(10))
340 buf
= cpu_to_le16((u16
)regval
);
342 ret
= regmap_bulk_write(data
->regmap
, IRS_REG_TIMER_LO
, &buf
,
345 dev_err(data
->dev
, "Could not bulk write timer (%d)\n", ret
);
352 static int irsd200_read_nr_count(struct irsd200_data
*data
, int *val
)
357 ret
= regmap_read(data
->regmap
, IRS_REG_NR_COUNT
, ®val
);
359 dev_err(data
->dev
, "Could not read nr count (%d)\n", ret
);
368 static int irsd200_write_nr_count(struct irsd200_data
*data
, int val
)
373 /* A value of zero means that IRS_REG_STATUS is never set. */
374 if (val
<= 0 || val
>= 8)
381 * According to the data sheet, timer must be also set in this
382 * case (i.e. be non-zero). Check and enforce that.
384 ret
= irsd200_read_timer(data
, &val
, &val
);
390 "Timer must be non-zero when nr count is %u\n",
396 ret
= regmap_write(data
->regmap
, IRS_REG_NR_COUNT
, regval
);
398 dev_err(data
->dev
, "Could not write nr count (%d)\n", ret
);
405 static int irsd200_read_lp_filter(struct irsd200_data
*data
, int *val
)
410 ret
= regmap_field_read(data
->regfields
[IRS_REGF_LP_FILTER
], ®val
);
412 dev_err(data
->dev
, "Could not read lp filter frequency (%d)\n",
417 *val
= irsd200_lp_filter_freq
[regval
];
422 static int irsd200_write_lp_filter(struct irsd200_data
*data
, int val
)
427 for (idx
= 0; idx
< ARRAY_SIZE(irsd200_lp_filter_freq
); ++idx
) {
428 if (irsd200_lp_filter_freq
[idx
] == val
)
432 if (idx
== ARRAY_SIZE(irsd200_lp_filter_freq
))
435 ret
= regmap_field_write(data
->regfields
[IRS_REGF_LP_FILTER
], idx
);
437 dev_err(data
->dev
, "Could not write lp filter frequency (%d)\n",
445 static int irsd200_read_hp_filter(struct irsd200_data
*data
, int *val
,
451 ret
= regmap_field_read(data
->regfields
[IRS_REGF_HP_FILTER
], ®val
);
453 dev_err(data
->dev
, "Could not read hp filter frequency (%d)\n",
458 *val
= irsd200_hp_filter_freq
[regval
][0];
459 *val2
= irsd200_hp_filter_freq
[regval
][1];
464 static int irsd200_write_hp_filter(struct irsd200_data
*data
, int val
, int val2
)
469 /* Truncate fractional part to one digit. */
472 for (idx
= 0; idx
< ARRAY_SIZE(irsd200_hp_filter_freq
); ++idx
) {
473 if (irsd200_hp_filter_freq
[idx
][0] == val2
)
477 if (idx
== ARRAY_SIZE(irsd200_hp_filter_freq
) || val
!= 0)
480 ret
= regmap_field_write(data
->regfields
[IRS_REGF_HP_FILTER
], idx
);
482 dev_err(data
->dev
, "Could not write hp filter frequency (%d)\n",
490 static int irsd200_read_raw(struct iio_dev
*indio_dev
,
491 struct iio_chan_spec
const *chan
, int *val
,
492 int *val2
, long mask
)
494 struct irsd200_data
*data
= iio_priv(indio_dev
);
499 case IIO_CHAN_INFO_RAW
:
500 ret
= irsd200_read_data(data
, &buf
);
506 case IIO_CHAN_INFO_SAMP_FREQ
:
507 ret
= irsd200_read_data_rate(data
, val
);
512 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
513 ret
= irsd200_read_lp_filter(data
, val
);
518 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
519 ret
= irsd200_read_hp_filter(data
, val
, val2
);
523 return IIO_VAL_FRACTIONAL
;
529 static int irsd200_read_avail(struct iio_dev
*indio_dev
,
530 struct iio_chan_spec
const *chan
,
531 const int **vals
, int *type
, int *length
,
535 case IIO_CHAN_INFO_SAMP_FREQ
:
536 *vals
= irsd200_data_rates
;
538 *length
= ARRAY_SIZE(irsd200_data_rates
);
539 return IIO_AVAIL_LIST
;
540 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
541 *vals
= irsd200_lp_filter_freq
;
543 *length
= ARRAY_SIZE(irsd200_lp_filter_freq
);
544 return IIO_AVAIL_LIST
;
545 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
546 *vals
= (int *)irsd200_hp_filter_freq
;
547 *type
= IIO_VAL_FRACTIONAL
;
548 *length
= 2 * ARRAY_SIZE(irsd200_hp_filter_freq
);
549 return IIO_AVAIL_LIST
;
555 static int irsd200_write_raw(struct iio_dev
*indio_dev
,
556 struct iio_chan_spec
const *chan
, int val
,
559 struct irsd200_data
*data
= iio_priv(indio_dev
);
562 case IIO_CHAN_INFO_SAMP_FREQ
:
563 return irsd200_write_data_rate(data
, val
);
564 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
565 return irsd200_write_lp_filter(data
, val
);
566 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
567 return irsd200_write_hp_filter(data
, val
, val2
);
573 static int irsd200_read_event(struct iio_dev
*indio_dev
,
574 const struct iio_chan_spec
*chan
,
575 enum iio_event_type type
,
576 enum iio_event_direction dir
,
577 enum iio_event_info info
, int *val
, int *val2
)
579 struct irsd200_data
*data
= iio_priv(indio_dev
);
583 case IIO_EV_INFO_VALUE
:
584 ret
= irsd200_read_threshold(data
, dir
, val
);
589 case IIO_EV_INFO_RUNNING_PERIOD
:
590 ret
= irsd200_read_timer(data
, val
, val2
);
594 return IIO_VAL_FRACTIONAL
;
595 case IIO_EV_INFO_RUNNING_COUNT
:
596 ret
= irsd200_read_nr_count(data
, val
);
606 static int irsd200_write_event(struct iio_dev
*indio_dev
,
607 const struct iio_chan_spec
*chan
,
608 enum iio_event_type type
,
609 enum iio_event_direction dir
,
610 enum iio_event_info info
, int val
, int val2
)
612 struct irsd200_data
*data
= iio_priv(indio_dev
);
615 case IIO_EV_INFO_VALUE
:
616 return irsd200_write_threshold(data
, dir
, val
);
617 case IIO_EV_INFO_RUNNING_PERIOD
:
618 return irsd200_write_timer(data
, val
, val2
);
619 case IIO_EV_INFO_RUNNING_COUNT
:
620 return irsd200_write_nr_count(data
, val
);
626 static int irsd200_read_event_config(struct iio_dev
*indio_dev
,
627 const struct iio_chan_spec
*chan
,
628 enum iio_event_type type
,
629 enum iio_event_direction dir
)
631 struct irsd200_data
*data
= iio_priv(indio_dev
);
636 case IIO_EV_TYPE_THRESH
:
637 ret
= regmap_field_read(
638 data
->regfields
[IRS_REGF_INTR_COUNT_THR_OR
], &val
);
648 static int irsd200_write_event_config(struct iio_dev
*indio_dev
,
649 const struct iio_chan_spec
*chan
,
650 enum iio_event_type type
,
651 enum iio_event_direction dir
,
654 struct irsd200_data
*data
= iio_priv(indio_dev
);
659 case IIO_EV_TYPE_THRESH
:
660 /* Clear the count register (by reading from it). */
661 ret
= regmap_read(data
->regmap
, IRS_REG_COUNT
, &tmp
);
665 return regmap_field_write(
666 data
->regfields
[IRS_REGF_INTR_COUNT_THR_OR
], state
);
672 static irqreturn_t
irsd200_irq_thread(int irq
, void *dev_id
)
674 struct iio_dev
*indio_dev
= dev_id
;
675 struct irsd200_data
*data
= iio_priv(indio_dev
);
676 enum iio_event_direction dir
;
677 unsigned int lower_count
;
678 unsigned int upper_count
;
679 unsigned int status
= 0;
680 unsigned int source
= 0;
681 unsigned int clear
= 0;
682 unsigned int count
= 0;
685 ret
= regmap_read(data
->regmap
, IRS_REG_INTR
, &source
);
687 dev_err(data
->dev
, "Could not read interrupt source (%d)\n",
692 ret
= regmap_read(data
->regmap
, IRS_REG_STATUS
, &status
);
694 dev_err(data
->dev
, "Could not acknowledge interrupt (%d)\n",
699 if (status
& BIT(IRS_INTR_DATA
) && iio_buffer_enabled(indio_dev
)) {
700 iio_trigger_poll_nested(indio_dev
->trig
);
701 clear
|= BIT(IRS_INTR_DATA
);
704 if (status
& BIT(IRS_INTR_COUNT_THR_OR
) &&
705 source
& BIT(IRS_INTR_COUNT_THR_OR
)) {
707 * The register value resets to zero after reading. We therefore
708 * need to read once and manually extract the lower and upper
709 * count register fields.
711 ret
= regmap_read(data
->regmap
, IRS_REG_COUNT
, &count
);
713 dev_err(data
->dev
, "Could not read count (%d)\n", ret
);
715 upper_count
= IRS_UPPER_COUNT(count
);
716 lower_count
= IRS_LOWER_COUNT(count
);
719 * We only check the OR mode to be able to push events for
720 * rising and falling thresholds. AND mode is covered when both
721 * upper and lower count is non-zero, and is signaled with
724 if (upper_count
&& !lower_count
)
725 dir
= IIO_EV_DIR_RISING
;
726 else if (!upper_count
&& lower_count
)
727 dir
= IIO_EV_DIR_FALLING
;
729 dir
= IIO_EV_DIR_EITHER
;
731 iio_push_event(indio_dev
,
732 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 0,
733 IIO_EV_TYPE_THRESH
, dir
),
734 iio_get_time_ns(indio_dev
));
737 * The OR mode will always trigger when the AND mode does, but
738 * not vice versa. However, it seems like the AND bit needs to
739 * be cleared if data capture _and_ threshold count interrupts
740 * are desirable, even though it hasn't explicitly been selected
741 * (with IRS_REG_INTR). Either way, it doesn't hurt...
743 clear
|= BIT(IRS_INTR_COUNT_THR_OR
) |
744 BIT(IRS_INTR_COUNT_THR_AND
);
750 ret
= regmap_write(data
->regmap
, IRS_REG_STATUS
, clear
);
753 "Could not clear interrupt status (%d)\n", ret
);
758 static irqreturn_t
irsd200_trigger_handler(int irq
, void *pollf
)
760 struct iio_dev
*indio_dev
= ((struct iio_poll_func
*)pollf
)->indio_dev
;
761 struct irsd200_data
*data
= iio_priv(indio_dev
);
765 ret
= irsd200_read_data(data
, (s16
*)buf
);
769 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
770 iio_get_time_ns(indio_dev
));
773 iio_trigger_notify_done(indio_dev
->trig
);
778 static int irsd200_set_trigger_state(struct iio_trigger
*trig
, bool state
)
780 struct irsd200_data
*data
= iio_trigger_get_drvdata(trig
);
783 ret
= regmap_field_write(data
->regfields
[IRS_REGF_INTR_DATA
], state
);
785 dev_err(data
->dev
, "Could not %s data interrupt source (%d)\n",
786 state
? "enable" : "disable", ret
);
792 static const struct iio_info irsd200_info
= {
793 .read_raw
= irsd200_read_raw
,
794 .read_avail
= irsd200_read_avail
,
795 .write_raw
= irsd200_write_raw
,
796 .read_event_value
= irsd200_read_event
,
797 .write_event_value
= irsd200_write_event
,
798 .read_event_config
= irsd200_read_event_config
,
799 .write_event_config
= irsd200_write_event_config
,
802 static const struct iio_trigger_ops irsd200_trigger_ops
= {
803 .set_trigger_state
= irsd200_set_trigger_state
,
804 .validate_device
= iio_trigger_validate_own_device
,
807 static const struct iio_event_spec irsd200_event_spec
[] = {
809 .type
= IIO_EV_TYPE_THRESH
,
810 .dir
= IIO_EV_DIR_RISING
,
811 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
814 .type
= IIO_EV_TYPE_THRESH
,
815 .dir
= IIO_EV_DIR_FALLING
,
816 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
819 .type
= IIO_EV_TYPE_THRESH
,
820 .dir
= IIO_EV_DIR_EITHER
,
822 BIT(IIO_EV_INFO_RUNNING_PERIOD
) |
823 BIT(IIO_EV_INFO_RUNNING_COUNT
) |
824 BIT(IIO_EV_INFO_ENABLE
),
828 static const struct iio_chan_spec irsd200_channels
[] = {
830 .type
= IIO_PROXIMITY
,
831 .info_mask_separate
=
832 BIT(IIO_CHAN_INFO_RAW
) |
833 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
834 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
) |
835 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
),
836 .info_mask_separate_available
=
837 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
838 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
) |
839 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
),
840 .event_spec
= irsd200_event_spec
,
841 .num_event_specs
= ARRAY_SIZE(irsd200_event_spec
),
846 .endianness
= IIO_CPU
,
851 static int irsd200_probe(struct i2c_client
*client
)
853 struct iio_trigger
*trigger
;
854 struct irsd200_data
*data
;
855 struct iio_dev
*indio_dev
;
859 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
861 return dev_err_probe(&client
->dev
, -ENOMEM
,
862 "Could not allocate iio device\n");
864 data
= iio_priv(indio_dev
);
865 data
->dev
= &client
->dev
;
867 data
->regmap
= devm_regmap_init_i2c(client
, &irsd200_regmap_config
);
868 if (IS_ERR(data
->regmap
))
869 return dev_err_probe(data
->dev
, PTR_ERR(data
->regmap
),
870 "Could not initialize regmap\n");
872 for (i
= 0; i
< IRS_REGF_MAX
; ++i
) {
873 data
->regfields
[i
] = devm_regmap_field_alloc(
874 data
->dev
, data
->regmap
, irsd200_regfields
[i
]);
875 if (IS_ERR(data
->regfields
[i
]))
876 return dev_err_probe(
877 data
->dev
, PTR_ERR(data
->regfields
[i
]),
878 "Could not allocate register field %zu\n", i
);
881 ret
= devm_regulator_get_enable(data
->dev
, "vdd");
883 return dev_err_probe(
885 "Could not get and enable regulator (%d)\n", ret
);
887 ret
= irsd200_setup(data
);
891 indio_dev
->info
= &irsd200_info
;
892 indio_dev
->name
= IRS_DRV_NAME
;
893 indio_dev
->channels
= irsd200_channels
;
894 indio_dev
->num_channels
= ARRAY_SIZE(irsd200_channels
);
895 indio_dev
->modes
= INDIO_DIRECT_MODE
;
898 return dev_err_probe(data
->dev
, -ENXIO
, "No irq available\n");
900 ret
= devm_iio_triggered_buffer_setup(data
->dev
, indio_dev
, NULL
,
901 irsd200_trigger_handler
, NULL
);
903 return dev_err_probe(
905 "Could not setup iio triggered buffer (%d)\n", ret
);
907 ret
= devm_request_threaded_irq(data
->dev
, client
->irq
, NULL
,
909 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
912 return dev_err_probe(data
->dev
, ret
,
913 "Could not request irq (%d)\n", ret
);
915 trigger
= devm_iio_trigger_alloc(data
->dev
, "%s-dev%d", indio_dev
->name
,
916 iio_device_id(indio_dev
));
918 return dev_err_probe(data
->dev
, -ENOMEM
,
919 "Could not allocate iio trigger\n");
921 trigger
->ops
= &irsd200_trigger_ops
;
922 iio_trigger_set_drvdata(trigger
, data
);
924 ret
= devm_iio_trigger_register(data
->dev
, trigger
);
926 return dev_err_probe(data
->dev
, ret
,
927 "Could not register iio trigger (%d)\n",
930 ret
= devm_iio_device_register(data
->dev
, indio_dev
);
932 return dev_err_probe(data
->dev
, ret
,
933 "Could not register iio device (%d)\n",
939 static const struct of_device_id irsd200_of_match
[] = {
941 .compatible
= "murata,irsd200",
945 MODULE_DEVICE_TABLE(of
, irsd200_of_match
);
947 static struct i2c_driver irsd200_driver
= {
949 .name
= IRS_DRV_NAME
,
950 .of_match_table
= irsd200_of_match
,
952 .probe
= irsd200_probe
,
954 module_i2c_driver(irsd200_driver
);
956 MODULE_AUTHOR("Waqar Hameed <waqar.hameed@axis.com>");
957 MODULE_DESCRIPTION("Murata IRS-D200 PIR sensor driver");
958 MODULE_LICENSE("GPL");