module: Convert symbol namespace to string literal
[linux.git] / drivers / iio / light / bh1745.c
blob56e32689bb97a8b450dd492e5836f58db409b2a5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ROHM BH1745 digital colour sensor driver
5 * Copyright (C) Mudit Sharma <muditsharma.info@gmail.com>
7 * 7-bit I2C slave addresses:
8 * 0x38 (ADDR pin low)
9 * 0x39 (ADDR pin high)
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 */
29 /* System control */
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
36 /* Mode control 1 */
37 #define BH1745_MODE_CTRL1 0x41
38 #define BH1745_CTRL1_MEASUREMENT_TIME_MASK GENMASK(2, 0)
40 /* Mode control 2 */
41 #define BH1745_MODE_CTRL2 0x42
42 #define BH1745_CTRL2_RGBC_EN BIT(4)
43 #define BH1745_CTRL2_ADC_GAIN_MASK GENMASK(1, 0)
45 /* Interrupt */
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
53 /* Threshold high */
54 #define BH1745_TH_LSB 0x62
55 #define BH1745_TH_MSB 0x63
57 /* Threshold low */
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,
83 enum bh1745_gain {
84 BH1745_ADC_GAIN_1X,
85 BH1745_ADC_GAIN_2X,
86 BH1745_ADC_GAIN_16X,
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),
120 struct bh1745_data {
122 * Lock to prevent device setting update or read before
123 * related calculations are completed
125 struct mutex lock;
126 struct regmap *regmap;
127 struct device *dev;
128 struct iio_trigger *trig;
129 struct iio_gts gts;
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 = {
168 .reg_bits = 8,
169 .val_bits = 8,
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, \
208 .scan_index = _si, \
209 .scan_type = { \
210 .sign = 'u', \
211 .realbits = 16, \
212 .storagebits = 16, \
213 .endianness = IIO_CPU, \
214 }, \
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;
242 int ret;
244 ret = regmap_clear_bits(data->regmap, BH1745_MODE_CTRL2,
245 BH1745_CTRL2_RGBC_EN);
246 if (ret)
247 dev_err(dev, "Failed to turn off device\n");
250 static int bh1745_get_scale(struct bh1745_data *data, int *val, int *val2)
252 int ret;
253 int value;
254 int gain_sel, int_time_sel;
255 int gain;
256 const struct iio_itime_sel_mul *int_time;
258 ret = regmap_read(data->regmap, BH1745_MODE_CTRL2, &value);
259 if (ret)
260 return ret;
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);
266 if (ret)
267 return ret;
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,
273 val2);
276 static int bh1745_set_scale(struct bh1745_data *data, int val)
278 struct device *dev = data->dev;
279 int ret;
280 int value;
281 int hw_gain_sel, current_int_time_sel, new_int_time_sel;
283 ret = regmap_read(data->regmap, BH1745_MODE_CTRL1, &value);
284 if (ret)
285 return ret;
287 current_int_time_sel = FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK,
288 value);
289 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
290 current_int_time_sel,
291 val, 0, &hw_gain_sel);
292 if (ret) {
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)
297 continue;
299 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
300 new_int_time_sel,
301 val, 0,
302 &hw_gain_sel);
303 if (!ret)
304 break;
307 if (ret) {
308 dev_dbg(dev, "Unsupported scale value requested: %d\n",
309 val);
310 return -EINVAL;
313 ret = regmap_write_bits(data->regmap, BH1745_MODE_CTRL1,
314 BH1745_CTRL1_MEASUREMENT_TIME_MASK,
315 new_int_time_sel);
316 if (ret)
317 return ret;
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)
326 int ret;
327 int value;
328 int int_time, int_time_sel;
330 ret = regmap_read(data->regmap, BH1745_MODE_CTRL1, &value);
331 if (ret)
332 return ret;
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);
336 if (int_time < 0)
337 return int_time;
339 *val = int_time;
341 return 0;
344 static int bh1745_set_int_time(struct bh1745_data *data, int val, int val2)
346 struct device *dev = data->dev;
347 int ret;
348 int value;
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",
355 req_int_time);
356 return -EINVAL;
359 ret = bh1745_get_int_time(data, &current_int_time);
360 if (ret)
361 return ret;
363 if (current_int_time == req_int_time)
364 return 0;
366 ret = regmap_read(data->regmap, BH1745_MODE_CTRL2, &value);
367 if (ret)
368 return ret;
370 current_hwgain_sel = FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK, value);
371 current_hwgain = iio_gts_find_gain_by_sel(&data->gts,
372 current_hwgain_sel);
373 ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, current_hwgain,
374 current_int_time,
375 req_int_time,
376 &new_hwgain);
377 if (new_hwgain < 0) {
378 dev_dbg(dev, "No corresponding gain for requested integration time\n");
379 return ret;
382 if (ret) {
383 bool in_range;
385 new_hwgain = iio_find_closest_gain_low(&data->gts, new_hwgain,
386 &in_range);
387 if (new_hwgain < 0) {
388 new_hwgain = iio_gts_get_min_gain(&data->gts);
389 if (new_hwgain < 0)
390 return ret;
393 if (!in_range)
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,
405 new_hwgain_sel);
406 if (ret)
407 return ret;
409 new_int_time_sel = iio_gts_find_sel_by_int_time(&data->gts,
410 req_int_time);
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,
416 new_int_time_sel);
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);
424 int ret;
425 int value;
427 switch (mask) {
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,
431 &value, 2);
432 if (ret)
433 return ret;
434 *val = value;
436 return IIO_VAL_INT;
438 unreachable();
440 case IIO_CHAN_INFO_SCALE: {
441 guard(mutex)(&data->lock);
442 ret = bh1745_get_scale(data, val, val2);
443 if (ret)
444 return ret;
446 return IIO_VAL_INT;
449 case IIO_CHAN_INFO_INT_TIME: {
450 guard(mutex)(&data->lock);
451 *val = 0;
452 ret = bh1745_get_int_time(data, val2);
453 if (ret)
454 return 0;
456 return IIO_VAL_INT_PLUS_MICRO;
459 default:
460 return -EINVAL;
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);
471 switch (mask) {
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);
478 default:
479 return -EINVAL;
483 static int bh1745_write_raw_get_fmt(struct iio_dev *indio_dev,
484 struct iio_chan_spec const *chan,
485 long mask)
487 switch (mask) {
488 case IIO_CHAN_INFO_SCALE:
489 return IIO_VAL_INT;
491 case IIO_CHAN_INFO_INT_TIME:
492 return IIO_VAL_INT_PLUS_MICRO;
494 default:
495 return -EINVAL;
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);
506 int ret;
508 switch (info) {
509 case IIO_EV_INFO_VALUE:
510 switch (dir) {
511 case IIO_EV_DIR_RISING:
512 ret = regmap_bulk_read(data->regmap, BH1745_TH_LSB,
513 val, 2);
514 if (ret)
515 return ret;
517 return IIO_VAL_INT;
519 case IIO_EV_DIR_FALLING:
520 ret = regmap_bulk_read(data->regmap, BH1745_TL_LSB,
521 val, 2);
522 if (ret)
523 return ret;
525 return IIO_VAL_INT;
527 default:
528 return -EINVAL;
531 case IIO_EV_INFO_PERIOD:
532 ret = regmap_read(data->regmap, BH1745_PERSISTENCE, val);
533 if (ret)
534 return ret;
536 return IIO_VAL_INT;
538 default:
539 return -EINVAL;
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);
550 int ret;
552 switch (info) {
553 case IIO_EV_INFO_VALUE:
554 if (val < 0x0 || val > 0xFFFF)
555 return -EINVAL;
557 switch (dir) {
558 case IIO_EV_DIR_RISING:
559 ret = regmap_bulk_write(data->regmap, BH1745_TH_LSB,
560 &val, 2);
561 if (ret)
562 return ret;
564 return IIO_VAL_INT;
566 case IIO_EV_DIR_FALLING:
567 ret = regmap_bulk_write(data->regmap, BH1745_TL_LSB,
568 &val, 2);
569 if (ret)
570 return ret;
572 return IIO_VAL_INT;
574 default:
575 return -EINVAL;
578 case IIO_EV_INFO_PERIOD:
579 if (val < BH1745_PRESISTENCE_UPDATE_TOGGLE ||
580 val > BH1745_PRESISTENCE_UPDATE_EIGHT_MEASUREMENT)
581 return -EINVAL;
582 ret = regmap_write(data->regmap, BH1745_PERSISTENCE, val);
583 if (ret)
584 return ret;
586 return IIO_VAL_INT;
588 default:
589 return -EINVAL;
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);
599 int ret;
600 int value;
601 int int_src;
603 ret = regmap_read(data->regmap, BH1745_INTR, &value);
604 if (ret)
605 return ret;
607 if (!FIELD_GET(BH1745_INTR_ENABLE, value))
608 return 0;
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)
615 return 1;
616 return 0;
618 case IIO_MOD_LIGHT_GREEN:
619 if (int_src == BH1745_INTR_SOURCE_GREEN)
620 return 1;
621 return 0;
623 case IIO_MOD_LIGHT_BLUE:
624 if (int_src == BH1745_INTR_SOURCE_BLUE)
625 return 1;
626 return 0;
628 case IIO_MOD_LIGHT_CLEAR:
629 if (int_src == BH1745_INTR_SOURCE_CLEAR)
630 return 1;
631 return 0;
633 default:
634 return -EINVAL;
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);
644 int value;
646 if (!state)
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));
674 default:
675 return -EINVAL;
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);
685 switch (mask) {
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);
692 default:
693 return -EINVAL;
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);
712 int ret;
713 int value;
714 int int_src;
716 ret = regmap_read(data->regmap, BH1745_INTR, &value);
717 if (ret)
718 return IRQ_NONE;
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,
725 IIO_EV_TYPE_THRESH,
726 IIO_EV_DIR_EITHER),
727 iio_get_time_ns(indio_dev));
729 return IRQ_HANDLED;
732 return IRQ_NONE;
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);
740 struct {
741 u16 chans[4];
742 s64 timestamp __aligned(8);
743 } scan;
744 u16 value;
745 int ret;
746 int i;
747 int j = 0;
749 iio_for_each_active_channel(indio_dev, i) {
750 ret = regmap_bulk_read(data->regmap, BH1745_RED_LSB + 2 * i,
751 &value, 2);
752 if (ret)
753 goto err;
755 scan.chans[j++] = value;
758 iio_push_to_buffers_with_timestamp(indio_dev, &scan,
759 iio_get_time_ns(indio_dev));
761 err:
762 iio_trigger_notify_done(indio_dev->trig);
764 return IRQ_HANDLED;
767 static int bh1745_setup_triggered_buffer(struct iio_dev *indio_dev,
768 struct device *parent,
769 int irq)
771 struct bh1745_data *data = iio_priv(indio_dev);
772 struct device *dev = data->dev;
773 int ret;
775 ret = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
776 bh1745_trigger_handler, NULL);
777 if (ret)
778 return dev_err_probe(dev, ret,
779 "Triggered buffer setup failed\n");
781 if (irq) {
782 ret = devm_request_threaded_irq(dev, irq, NULL,
783 bh1745_interrupt_handler,
784 IRQF_ONESHOT,
785 "bh1745_interrupt", indio_dev);
786 if (ret)
787 return dev_err_probe(dev, ret,
788 "Request for IRQ failed\n");
791 return 0;
794 static int bh1745_init(struct bh1745_data *data)
796 int ret;
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);
804 if (ret)
805 return ret;
807 ret = bh1745_reset(data);
808 if (ret)
809 return dev_err_probe(dev, ret, "Failed to reset sensor\n");
811 ret = bh1745_power_on(data);
812 if (ret)
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);
816 if (ret)
817 return dev_err_probe(dev, ret,
818 "Failed to add action or reset\n");
820 return 0;
823 static int bh1745_probe(struct i2c_client *client)
825 int ret;
826 int value;
827 int part_id;
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));
833 if (!indio_dev)
834 return -ENOMEM;
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);
849 if (ret)
850 return ret;
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");
857 if (ret)
858 return dev_err_probe(dev, ret,
859 "Failed to get and enable regulator\n");
861 ret = bh1745_init(data);
862 if (ret)
863 return ret;
865 ret = bh1745_setup_triggered_buffer(indio_dev, indio_dev->dev.parent,
866 client->irq);
867 if (ret)
868 return ret;
870 ret = devm_iio_device_register(dev, indio_dev);
871 if (ret)
872 return dev_err_probe(dev, ret, "Failed to register device\n");
874 return 0;
877 static const struct i2c_device_id bh1745_idtable[] = {
878 { "bh1745" },
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 = {
890 .driver = {
891 .name = "bh1745",
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");