1 // SPDX-License-Identifier: GPL-2.0
3 * ROHM BH1745 digital colour sensor driver
5 * Copyright (C) Mudit Sharma <muditsharma.info@gmail.com>
7 * 7-bit I2C slave addresses:
12 #include <linux/i2c.h>
13 #include <linux/mutex.h>
14 #include <linux/util_macros.h>
15 #include <linux/iio/events.h>
16 #include <linux/regmap.h>
17 #include <linux/bits.h>
18 #include <linux/bitfield.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/iio-gts-helper.h>
27 /* BH1745 configuration registers */
30 #define BH1745_SYS_CTRL 0x40
31 #define BH1745_SYS_CTRL_SW_RESET BIT(7)
32 #define BH1745_SYS_CTRL_INTR_RESET BIT(6)
33 #define BH1745_SYS_CTRL_PART_ID_MASK GENMASK(5, 0)
34 #define BH1745_PART_ID 0x0B
37 #define BH1745_MODE_CTRL1 0x41
38 #define BH1745_CTRL1_MEASUREMENT_TIME_MASK GENMASK(2, 0)
41 #define BH1745_MODE_CTRL2 0x42
42 #define BH1745_CTRL2_RGBC_EN BIT(4)
43 #define BH1745_CTRL2_ADC_GAIN_MASK GENMASK(1, 0)
46 #define BH1745_INTR 0x60
47 #define BH1745_INTR_STATUS BIT(7)
48 #define BH1745_INTR_SOURCE_MASK GENMASK(3, 2)
49 #define BH1745_INTR_ENABLE BIT(0)
51 #define BH1745_PERSISTENCE 0x61
54 #define BH1745_TH_LSB 0x62
55 #define BH1745_TH_MSB 0x63
58 #define BH1745_TL_LSB 0x64
59 #define BH1745_TL_MSB 0x65
61 /* BH1745 data output regs */
62 #define BH1745_RED_LSB 0x50
63 #define BH1745_RED_MSB 0x51
64 #define BH1745_GREEN_LSB 0x52
65 #define BH1745_GREEN_MSB 0x53
66 #define BH1745_BLUE_LSB 0x54
67 #define BH1745_BLUE_MSB 0x55
68 #define BH1745_CLEAR_LSB 0x56
69 #define BH1745_CLEAR_MSB 0x57
71 #define BH1745_MANU_ID_REG 0x92
73 /* From 16x max HW gain and 32x max integration time */
74 #define BH1745_MAX_GAIN 512
76 enum bh1745_int_source
{
77 BH1745_INTR_SOURCE_RED
,
78 BH1745_INTR_SOURCE_GREEN
,
79 BH1745_INTR_SOURCE_BLUE
,
80 BH1745_INTR_SOURCE_CLEAR
,
89 enum bh1745_measurement_time
{
90 BH1745_MEASUREMENT_TIME_160MS
,
91 BH1745_MEASUREMENT_TIME_320MS
,
92 BH1745_MEASUREMENT_TIME_640MS
,
93 BH1745_MEASUREMENT_TIME_1280MS
,
94 BH1745_MEASUREMENT_TIME_2560MS
,
95 BH1745_MEASUREMENT_TIME_5120MS
,
98 enum bh1745_presistence_value
{
99 BH1745_PRESISTENCE_UPDATE_TOGGLE
,
100 BH1745_PRESISTENCE_UPDATE_EACH_MEASUREMENT
,
101 BH1745_PRESISTENCE_UPDATE_FOUR_MEASUREMENT
,
102 BH1745_PRESISTENCE_UPDATE_EIGHT_MEASUREMENT
,
105 static const struct iio_gain_sel_pair bh1745_gain
[] = {
106 GAIN_SCALE_GAIN(1, BH1745_ADC_GAIN_1X
),
107 GAIN_SCALE_GAIN(2, BH1745_ADC_GAIN_2X
),
108 GAIN_SCALE_GAIN(16, BH1745_ADC_GAIN_16X
),
111 static const struct iio_itime_sel_mul bh1745_itimes
[] = {
112 GAIN_SCALE_ITIME_US(5120000, BH1745_MEASUREMENT_TIME_5120MS
, 32),
113 GAIN_SCALE_ITIME_US(2560000, BH1745_MEASUREMENT_TIME_2560MS
, 16),
114 GAIN_SCALE_ITIME_US(1280000, BH1745_MEASUREMENT_TIME_1280MS
, 8),
115 GAIN_SCALE_ITIME_US(640000, BH1745_MEASUREMENT_TIME_640MS
, 4),
116 GAIN_SCALE_ITIME_US(320000, BH1745_MEASUREMENT_TIME_320MS
, 2),
117 GAIN_SCALE_ITIME_US(160000, BH1745_MEASUREMENT_TIME_160MS
, 1),
122 * Lock to prevent device setting update or read before
123 * related calculations are completed
126 struct regmap
*regmap
;
128 struct iio_trigger
*trig
;
132 static const struct regmap_range bh1745_volatile_ranges
[] = {
133 regmap_reg_range(BH1745_MODE_CTRL2
, BH1745_MODE_CTRL2
), /* VALID */
134 regmap_reg_range(BH1745_RED_LSB
, BH1745_CLEAR_MSB
), /* Data */
135 regmap_reg_range(BH1745_INTR
, BH1745_INTR
), /* Interrupt */
138 static const struct regmap_access_table bh1745_volatile_regs
= {
139 .yes_ranges
= bh1745_volatile_ranges
,
140 .n_yes_ranges
= ARRAY_SIZE(bh1745_volatile_ranges
),
143 static const struct regmap_range bh1745_readable_ranges
[] = {
144 regmap_reg_range(BH1745_SYS_CTRL
, BH1745_MODE_CTRL2
),
145 regmap_reg_range(BH1745_RED_LSB
, BH1745_CLEAR_MSB
),
146 regmap_reg_range(BH1745_INTR
, BH1745_INTR
),
147 regmap_reg_range(BH1745_PERSISTENCE
, BH1745_TL_MSB
),
148 regmap_reg_range(BH1745_MANU_ID_REG
, BH1745_MANU_ID_REG
),
151 static const struct regmap_access_table bh1745_readable_regs
= {
152 .yes_ranges
= bh1745_readable_ranges
,
153 .n_yes_ranges
= ARRAY_SIZE(bh1745_readable_ranges
),
156 static const struct regmap_range bh1745_writable_ranges
[] = {
157 regmap_reg_range(BH1745_SYS_CTRL
, BH1745_MODE_CTRL2
),
158 regmap_reg_range(BH1745_INTR
, BH1745_INTR
),
159 regmap_reg_range(BH1745_PERSISTENCE
, BH1745_TL_MSB
),
162 static const struct regmap_access_table bh1745_writable_regs
= {
163 .yes_ranges
= bh1745_writable_ranges
,
164 .n_yes_ranges
= ARRAY_SIZE(bh1745_writable_ranges
),
167 static const struct regmap_config bh1745_regmap
= {
170 .max_register
= BH1745_MANU_ID_REG
,
171 .cache_type
= REGCACHE_RBTREE
,
172 .volatile_table
= &bh1745_volatile_regs
,
173 .wr_table
= &bh1745_writable_regs
,
174 .rd_table
= &bh1745_readable_regs
,
177 static const struct iio_event_spec bh1745_event_spec
[] = {
179 .type
= IIO_EV_TYPE_THRESH
,
180 .dir
= IIO_EV_DIR_RISING
,
181 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
184 .type
= IIO_EV_TYPE_THRESH
,
185 .dir
= IIO_EV_DIR_FALLING
,
186 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
189 .type
= IIO_EV_TYPE_THRESH
,
190 .dir
= IIO_EV_DIR_EITHER
,
191 .mask_shared_by_type
= BIT(IIO_EV_INFO_PERIOD
),
192 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
196 #define BH1745_CHANNEL(_colour, _si, _addr) \
198 .type = IIO_INTENSITY, .modified = 1, \
199 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
200 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) | \
201 BIT(IIO_CHAN_INFO_INT_TIME), \
202 .info_mask_shared_by_all_available = \
203 BIT(IIO_CHAN_INFO_SCALE) | \
204 BIT(IIO_CHAN_INFO_INT_TIME), \
205 .event_spec = bh1745_event_spec, \
206 .num_event_specs = ARRAY_SIZE(bh1745_event_spec), \
207 .channel2 = IIO_MOD_LIGHT_##_colour, .address = _addr, \
213 .endianness = IIO_CPU, \
217 static const struct iio_chan_spec bh1745_channels
[] = {
218 BH1745_CHANNEL(RED
, 0, BH1745_RED_LSB
),
219 BH1745_CHANNEL(GREEN
, 1, BH1745_GREEN_LSB
),
220 BH1745_CHANNEL(BLUE
, 2, BH1745_BLUE_LSB
),
221 BH1745_CHANNEL(CLEAR
, 3, BH1745_CLEAR_LSB
),
222 IIO_CHAN_SOFT_TIMESTAMP(4),
225 static int bh1745_reset(struct bh1745_data
*data
)
227 return regmap_set_bits(data
->regmap
, BH1745_SYS_CTRL
,
228 BH1745_SYS_CTRL_SW_RESET
|
229 BH1745_SYS_CTRL_INTR_RESET
);
232 static int bh1745_power_on(struct bh1745_data
*data
)
234 return regmap_set_bits(data
->regmap
, BH1745_MODE_CTRL2
,
235 BH1745_CTRL2_RGBC_EN
);
238 static void bh1745_power_off(void *data_ptr
)
240 struct bh1745_data
*data
= data_ptr
;
241 struct device
*dev
= data
->dev
;
244 ret
= regmap_clear_bits(data
->regmap
, BH1745_MODE_CTRL2
,
245 BH1745_CTRL2_RGBC_EN
);
247 dev_err(dev
, "Failed to turn off device\n");
250 static int bh1745_get_scale(struct bh1745_data
*data
, int *val
, int *val2
)
254 int gain_sel
, int_time_sel
;
256 const struct iio_itime_sel_mul
*int_time
;
258 ret
= regmap_read(data
->regmap
, BH1745_MODE_CTRL2
, &value
);
262 gain_sel
= FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK
, value
);
263 gain
= iio_gts_find_gain_by_sel(&data
->gts
, gain_sel
);
265 ret
= regmap_read(data
->regmap
, BH1745_MODE_CTRL1
, &value
);
269 int_time_sel
= FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK
, value
);
270 int_time
= iio_gts_find_itime_by_sel(&data
->gts
, int_time_sel
);
272 return iio_gts_get_scale(&data
->gts
, gain
, int_time
->time_us
, val
,
276 static int bh1745_set_scale(struct bh1745_data
*data
, int val
)
278 struct device
*dev
= data
->dev
;
281 int hw_gain_sel
, current_int_time_sel
, new_int_time_sel
;
283 ret
= regmap_read(data
->regmap
, BH1745_MODE_CTRL1
, &value
);
287 current_int_time_sel
= FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK
,
289 ret
= iio_gts_find_gain_sel_for_scale_using_time(&data
->gts
,
290 current_int_time_sel
,
291 val
, 0, &hw_gain_sel
);
293 for (int i
= 0; i
< data
->gts
.num_itime
; i
++) {
294 new_int_time_sel
= data
->gts
.itime_table
[i
].sel
;
296 if (new_int_time_sel
== current_int_time_sel
)
299 ret
= iio_gts_find_gain_sel_for_scale_using_time(&data
->gts
,
308 dev_dbg(dev
, "Unsupported scale value requested: %d\n",
313 ret
= regmap_write_bits(data
->regmap
, BH1745_MODE_CTRL1
,
314 BH1745_CTRL1_MEASUREMENT_TIME_MASK
,
320 return regmap_write_bits(data
->regmap
, BH1745_MODE_CTRL2
,
321 BH1745_CTRL2_ADC_GAIN_MASK
, hw_gain_sel
);
324 static int bh1745_get_int_time(struct bh1745_data
*data
, int *val
)
328 int int_time
, int_time_sel
;
330 ret
= regmap_read(data
->regmap
, BH1745_MODE_CTRL1
, &value
);
334 int_time_sel
= FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK
, value
);
335 int_time
= iio_gts_find_int_time_by_sel(&data
->gts
, int_time_sel
);
344 static int bh1745_set_int_time(struct bh1745_data
*data
, int val
, int val2
)
346 struct device
*dev
= data
->dev
;
349 int current_int_time
, current_hwgain_sel
, current_hwgain
;
350 int new_hwgain
, new_hwgain_sel
, new_int_time_sel
;
351 int req_int_time
= (1000000 * val
) + val2
;
353 if (!iio_gts_valid_time(&data
->gts
, req_int_time
)) {
354 dev_dbg(dev
, "Unsupported integration time requested: %d\n",
359 ret
= bh1745_get_int_time(data
, ¤t_int_time
);
363 if (current_int_time
== req_int_time
)
366 ret
= regmap_read(data
->regmap
, BH1745_MODE_CTRL2
, &value
);
370 current_hwgain_sel
= FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK
, value
);
371 current_hwgain
= iio_gts_find_gain_by_sel(&data
->gts
,
373 ret
= iio_gts_find_new_gain_by_old_gain_time(&data
->gts
, current_hwgain
,
377 if (new_hwgain
< 0) {
378 dev_dbg(dev
, "No corresponding gain for requested integration time\n");
385 new_hwgain
= iio_find_closest_gain_low(&data
->gts
, new_hwgain
,
387 if (new_hwgain
< 0) {
388 new_hwgain
= iio_gts_get_min_gain(&data
->gts
);
394 dev_dbg(dev
, "Optimal gain out of range\n");
396 dev_dbg(dev
, "Scale changed, new hw_gain %d\n", new_hwgain
);
399 new_hwgain_sel
= iio_gts_find_sel_by_gain(&data
->gts
, new_hwgain
);
400 if (new_hwgain_sel
< 0)
401 return new_hwgain_sel
;
403 ret
= regmap_write_bits(data
->regmap
, BH1745_MODE_CTRL2
,
404 BH1745_CTRL2_ADC_GAIN_MASK
,
409 new_int_time_sel
= iio_gts_find_sel_by_int_time(&data
->gts
,
411 if (new_int_time_sel
< 0)
412 return new_int_time_sel
;
414 return regmap_write_bits(data
->regmap
, BH1745_MODE_CTRL1
,
415 BH1745_CTRL1_MEASUREMENT_TIME_MASK
,
419 static int bh1745_read_raw(struct iio_dev
*indio_dev
,
420 struct iio_chan_spec
const *chan
,
421 int *val
, int *val2
, long mask
)
423 struct bh1745_data
*data
= iio_priv(indio_dev
);
428 case IIO_CHAN_INFO_RAW
:
429 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
) {
430 ret
= regmap_bulk_read(data
->regmap
, chan
->address
,
440 case IIO_CHAN_INFO_SCALE
: {
441 guard(mutex
)(&data
->lock
);
442 ret
= bh1745_get_scale(data
, val
, val2
);
449 case IIO_CHAN_INFO_INT_TIME
: {
450 guard(mutex
)(&data
->lock
);
452 ret
= bh1745_get_int_time(data
, val2
);
456 return IIO_VAL_INT_PLUS_MICRO
;
464 static int bh1745_write_raw(struct iio_dev
*indio_dev
,
465 struct iio_chan_spec
const *chan
,
466 int val
, int val2
, long mask
)
468 struct bh1745_data
*data
= iio_priv(indio_dev
);
470 guard(mutex
)(&data
->lock
);
472 case IIO_CHAN_INFO_SCALE
:
473 return bh1745_set_scale(data
, val
);
475 case IIO_CHAN_INFO_INT_TIME
:
476 return bh1745_set_int_time(data
, val
, val2
);
483 static int bh1745_write_raw_get_fmt(struct iio_dev
*indio_dev
,
484 struct iio_chan_spec
const *chan
,
488 case IIO_CHAN_INFO_SCALE
:
491 case IIO_CHAN_INFO_INT_TIME
:
492 return IIO_VAL_INT_PLUS_MICRO
;
499 static int bh1745_read_thresh(struct iio_dev
*indio_dev
,
500 const struct iio_chan_spec
*chan
,
501 enum iio_event_type type
,
502 enum iio_event_direction dir
,
503 enum iio_event_info info
, int *val
, int *val2
)
505 struct bh1745_data
*data
= iio_priv(indio_dev
);
509 case IIO_EV_INFO_VALUE
:
511 case IIO_EV_DIR_RISING
:
512 ret
= regmap_bulk_read(data
->regmap
, BH1745_TH_LSB
,
519 case IIO_EV_DIR_FALLING
:
520 ret
= regmap_bulk_read(data
->regmap
, BH1745_TL_LSB
,
531 case IIO_EV_INFO_PERIOD
:
532 ret
= regmap_read(data
->regmap
, BH1745_PERSISTENCE
, val
);
543 static int bh1745_write_thresh(struct iio_dev
*indio_dev
,
544 const struct iio_chan_spec
*chan
,
545 enum iio_event_type type
,
546 enum iio_event_direction dir
,
547 enum iio_event_info info
, int val
, int val2
)
549 struct bh1745_data
*data
= iio_priv(indio_dev
);
553 case IIO_EV_INFO_VALUE
:
554 if (val
< 0x0 || val
> 0xFFFF)
558 case IIO_EV_DIR_RISING
:
559 ret
= regmap_bulk_write(data
->regmap
, BH1745_TH_LSB
,
566 case IIO_EV_DIR_FALLING
:
567 ret
= regmap_bulk_write(data
->regmap
, BH1745_TL_LSB
,
578 case IIO_EV_INFO_PERIOD
:
579 if (val
< BH1745_PRESISTENCE_UPDATE_TOGGLE
||
580 val
> BH1745_PRESISTENCE_UPDATE_EIGHT_MEASUREMENT
)
582 ret
= regmap_write(data
->regmap
, BH1745_PERSISTENCE
, val
);
593 static int bh1745_read_event_config(struct iio_dev
*indio_dev
,
594 const struct iio_chan_spec
*chan
,
595 enum iio_event_type type
,
596 enum iio_event_direction dir
)
598 struct bh1745_data
*data
= iio_priv(indio_dev
);
603 ret
= regmap_read(data
->regmap
, BH1745_INTR
, &value
);
607 if (!FIELD_GET(BH1745_INTR_ENABLE
, value
))
610 int_src
= FIELD_GET(BH1745_INTR_SOURCE_MASK
, value
);
612 switch (chan
->channel2
) {
613 case IIO_MOD_LIGHT_RED
:
614 if (int_src
== BH1745_INTR_SOURCE_RED
)
618 case IIO_MOD_LIGHT_GREEN
:
619 if (int_src
== BH1745_INTR_SOURCE_GREEN
)
623 case IIO_MOD_LIGHT_BLUE
:
624 if (int_src
== BH1745_INTR_SOURCE_BLUE
)
628 case IIO_MOD_LIGHT_CLEAR
:
629 if (int_src
== BH1745_INTR_SOURCE_CLEAR
)
638 static int bh1745_write_event_config(struct iio_dev
*indio_dev
,
639 const struct iio_chan_spec
*chan
,
640 enum iio_event_type type
,
641 enum iio_event_direction dir
, bool state
)
643 struct bh1745_data
*data
= iio_priv(indio_dev
);
647 return regmap_clear_bits(data
->regmap
,
648 BH1745_INTR
, BH1745_INTR_ENABLE
);
650 /* Latch is always enabled when enabling interrupt */
651 value
= BH1745_INTR_ENABLE
;
653 switch (chan
->channel2
) {
654 case IIO_MOD_LIGHT_RED
:
655 return regmap_write(data
->regmap
, BH1745_INTR
,
656 value
| FIELD_PREP(BH1745_INTR_SOURCE_MASK
,
657 BH1745_INTR_SOURCE_RED
));
659 case IIO_MOD_LIGHT_GREEN
:
660 return regmap_write(data
->regmap
, BH1745_INTR
,
661 value
| FIELD_PREP(BH1745_INTR_SOURCE_MASK
,
662 BH1745_INTR_SOURCE_GREEN
));
664 case IIO_MOD_LIGHT_BLUE
:
665 return regmap_write(data
->regmap
, BH1745_INTR
,
666 value
| FIELD_PREP(BH1745_INTR_SOURCE_MASK
,
667 BH1745_INTR_SOURCE_BLUE
));
669 case IIO_MOD_LIGHT_CLEAR
:
670 return regmap_write(data
->regmap
, BH1745_INTR
,
671 value
| FIELD_PREP(BH1745_INTR_SOURCE_MASK
,
672 BH1745_INTR_SOURCE_CLEAR
));
679 static int bh1745_read_avail(struct iio_dev
*indio_dev
,
680 struct iio_chan_spec
const *chan
, const int **vals
,
681 int *type
, int *length
, long mask
)
683 struct bh1745_data
*data
= iio_priv(indio_dev
);
686 case IIO_CHAN_INFO_INT_TIME
:
687 return iio_gts_avail_times(&data
->gts
, vals
, type
, length
);
689 case IIO_CHAN_INFO_SCALE
:
690 return iio_gts_all_avail_scales(&data
->gts
, vals
, type
, length
);
697 static const struct iio_info bh1745_info
= {
698 .read_raw
= bh1745_read_raw
,
699 .write_raw
= bh1745_write_raw
,
700 .write_raw_get_fmt
= bh1745_write_raw_get_fmt
,
701 .read_event_value
= bh1745_read_thresh
,
702 .write_event_value
= bh1745_write_thresh
,
703 .read_event_config
= bh1745_read_event_config
,
704 .write_event_config
= bh1745_write_event_config
,
705 .read_avail
= bh1745_read_avail
,
708 static irqreturn_t
bh1745_interrupt_handler(int interrupt
, void *p
)
710 struct iio_dev
*indio_dev
= p
;
711 struct bh1745_data
*data
= iio_priv(indio_dev
);
716 ret
= regmap_read(data
->regmap
, BH1745_INTR
, &value
);
720 int_src
= FIELD_GET(BH1745_INTR_SOURCE_MASK
, value
);
722 if (value
& BH1745_INTR_STATUS
) {
723 iio_push_event(indio_dev
,
724 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, int_src
,
727 iio_get_time_ns(indio_dev
));
735 static irqreturn_t
bh1745_trigger_handler(int interrupt
, void *p
)
737 struct iio_poll_func
*pf
= p
;
738 struct iio_dev
*indio_dev
= pf
->indio_dev
;
739 struct bh1745_data
*data
= iio_priv(indio_dev
);
742 s64 timestamp
__aligned(8);
749 iio_for_each_active_channel(indio_dev
, i
) {
750 ret
= regmap_bulk_read(data
->regmap
, BH1745_RED_LSB
+ 2 * i
,
755 scan
.chans
[j
++] = value
;
758 iio_push_to_buffers_with_timestamp(indio_dev
, &scan
,
759 iio_get_time_ns(indio_dev
));
762 iio_trigger_notify_done(indio_dev
->trig
);
767 static int bh1745_setup_triggered_buffer(struct iio_dev
*indio_dev
,
768 struct device
*parent
,
771 struct bh1745_data
*data
= iio_priv(indio_dev
);
772 struct device
*dev
= data
->dev
;
775 ret
= devm_iio_triggered_buffer_setup(parent
, indio_dev
, NULL
,
776 bh1745_trigger_handler
, NULL
);
778 return dev_err_probe(dev
, ret
,
779 "Triggered buffer setup failed\n");
782 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
783 bh1745_interrupt_handler
,
785 "bh1745_interrupt", indio_dev
);
787 return dev_err_probe(dev
, ret
,
788 "Request for IRQ failed\n");
794 static int bh1745_init(struct bh1745_data
*data
)
797 struct device
*dev
= data
->dev
;
799 mutex_init(&data
->lock
);
801 ret
= devm_iio_init_iio_gts(dev
, BH1745_MAX_GAIN
, 0, bh1745_gain
,
802 ARRAY_SIZE(bh1745_gain
), bh1745_itimes
,
803 ARRAY_SIZE(bh1745_itimes
), &data
->gts
);
807 ret
= bh1745_reset(data
);
809 return dev_err_probe(dev
, ret
, "Failed to reset sensor\n");
811 ret
= bh1745_power_on(data
);
813 return dev_err_probe(dev
, ret
, "Failed to turn on sensor\n");
815 ret
= devm_add_action_or_reset(dev
, bh1745_power_off
, data
);
817 return dev_err_probe(dev
, ret
,
818 "Failed to add action or reset\n");
823 static int bh1745_probe(struct i2c_client
*client
)
828 struct bh1745_data
*data
;
829 struct iio_dev
*indio_dev
;
830 struct device
*dev
= &client
->dev
;
832 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
836 indio_dev
->info
= &bh1745_info
;
837 indio_dev
->name
= "bh1745";
838 indio_dev
->channels
= bh1745_channels
;
839 indio_dev
->modes
= INDIO_DIRECT_MODE
;
840 indio_dev
->num_channels
= ARRAY_SIZE(bh1745_channels
);
841 data
= iio_priv(indio_dev
);
842 data
->dev
= &client
->dev
;
843 data
->regmap
= devm_regmap_init_i2c(client
, &bh1745_regmap
);
844 if (IS_ERR(data
->regmap
))
845 return dev_err_probe(dev
, PTR_ERR(data
->regmap
),
846 "Failed to initialize Regmap\n");
848 ret
= regmap_read(data
->regmap
, BH1745_SYS_CTRL
, &value
);
852 part_id
= FIELD_GET(BH1745_SYS_CTRL_PART_ID_MASK
, value
);
853 if (part_id
!= BH1745_PART_ID
)
854 dev_warn(dev
, "Unknown part ID 0x%x\n", part_id
);
856 ret
= devm_regulator_get_enable(dev
, "vdd");
858 return dev_err_probe(dev
, ret
,
859 "Failed to get and enable regulator\n");
861 ret
= bh1745_init(data
);
865 ret
= bh1745_setup_triggered_buffer(indio_dev
, indio_dev
->dev
.parent
,
870 ret
= devm_iio_device_register(dev
, indio_dev
);
872 return dev_err_probe(dev
, ret
, "Failed to register device\n");
877 static const struct i2c_device_id bh1745_idtable
[] = {
881 MODULE_DEVICE_TABLE(i2c
, bh1745_idtable
);
883 static const struct of_device_id bh1745_of_match
[] = {
884 { .compatible
= "rohm,bh1745" },
887 MODULE_DEVICE_TABLE(of
, bh1745_of_match
);
889 static struct i2c_driver bh1745_driver
= {
892 .of_match_table
= bh1745_of_match
,
894 .probe
= bh1745_probe
,
895 .id_table
= bh1745_idtable
,
897 module_i2c_driver(bh1745_driver
);
899 MODULE_LICENSE("GPL");
900 MODULE_AUTHOR("Mudit Sharma <muditsharma.info@gmail.com>");
901 MODULE_DESCRIPTION("BH1745 colour sensor driver");
902 MODULE_IMPORT_NS("IIO_GTS_HELPER");