1 // SPDX-License-Identifier: GPL-2.0-only
3 * ROHM Colour Sensor driver for
4 * - BU27008 RGBC sensor
5 * - BU27010 RGBC + Flickering sensor
7 * Copyright (c) 2023, ROHM Semiconductor.
10 #include <linux/bitfield.h>
11 #include <linux/bitops.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/units.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/iio-gts-helper.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
28 * A word about register address and mask definitions.
30 * At a quick glance to the data-sheet register tables, the BU27010 has all the
31 * registers that the BU27008 has. On top of that the BU27010 adds couple of new
34 * So, all definitions BU27008_REG_* are there also for BU27010 but none of the
35 * BU27010_REG_* are present on BU27008. This makes sense as BU27010 just adds
36 * some features (Flicker FIFO, more power control) on top of the BU27008.
38 * Unfortunately, some of the wheel has been re-invented. Even though the names
39 * of the registers have stayed the same, pretty much all of the functionality
40 * provided by the registers has changed place. Contents of all MODE_CONTROL
41 * registers on BU27008 and BU27010 are different.
43 * Chip-specific mapping from register addresses/bits to functionality is done
44 * in bu27_chip_data structures.
46 #define BU27008_REG_SYSTEM_CONTROL 0x40
47 #define BU27008_MASK_SW_RESET BIT(7)
48 #define BU27008_MASK_PART_ID GENMASK(5, 0)
49 #define BU27008_ID 0x1a
50 #define BU27008_REG_MODE_CONTROL1 0x41
51 #define BU27008_MASK_MEAS_MODE GENMASK(2, 0)
52 #define BU27008_MASK_CHAN_SEL GENMASK(3, 2)
54 #define BU27008_REG_MODE_CONTROL2 0x42
55 #define BU27008_MASK_RGBC_GAIN GENMASK(7, 3)
56 #define BU27008_MASK_IR_GAIN_LO GENMASK(2, 0)
57 #define BU27008_SHIFT_IR_GAIN 3
59 #define BU27008_REG_MODE_CONTROL3 0x43
60 #define BU27008_MASK_VALID BIT(7)
61 #define BU27008_MASK_INT_EN BIT(1)
62 #define BU27008_INT_EN BU27008_MASK_INT_EN
63 #define BU27008_INT_DIS 0
64 #define BU27008_MASK_MEAS_EN BIT(0)
65 #define BU27008_MEAS_EN BIT(0)
66 #define BU27008_MEAS_DIS 0
68 #define BU27008_REG_DATA0_LO 0x50
69 #define BU27008_REG_DATA1_LO 0x52
70 #define BU27008_REG_DATA2_LO 0x54
71 #define BU27008_REG_DATA3_LO 0x56
72 #define BU27008_REG_DATA3_HI 0x57
73 #define BU27008_REG_MANUFACTURER_ID 0x92
74 #define BU27008_REG_MAX BU27008_REG_MANUFACTURER_ID
76 /* BU27010 specific definitions */
78 #define BU27010_MASK_SW_RESET BIT(7)
79 #define BU27010_ID 0x1b
80 #define BU27010_REG_POWER 0x3e
81 #define BU27010_MASK_POWER BIT(0)
83 #define BU27010_REG_RESET 0x3f
84 #define BU27010_MASK_RESET BIT(0)
85 #define BU27010_RESET_RELEASE BU27010_MASK_RESET
87 #define BU27010_MASK_MEAS_EN BIT(1)
89 #define BU27010_MASK_CHAN_SEL GENMASK(7, 6)
90 #define BU27010_MASK_MEAS_MODE GENMASK(5, 4)
91 #define BU27010_MASK_RGBC_GAIN GENMASK(3, 0)
93 #define BU27010_MASK_DATA3_GAIN GENMASK(7, 6)
94 #define BU27010_MASK_DATA2_GAIN GENMASK(5, 4)
95 #define BU27010_MASK_DATA1_GAIN GENMASK(3, 2)
96 #define BU27010_MASK_DATA0_GAIN GENMASK(1, 0)
98 #define BU27010_MASK_FLC_MODE BIT(7)
99 #define BU27010_MASK_FLC_GAIN GENMASK(4, 0)
101 #define BU27010_REG_MODE_CONTROL4 0x44
102 /* If flicker is ever to be supported the IRQ must be handled as a field */
103 #define BU27010_IRQ_DIS_ALL GENMASK(1, 0)
104 #define BU27010_DRDY_EN BIT(0)
105 #define BU27010_MASK_INT_SEL GENMASK(1, 0)
107 #define BU27010_REG_MODE_CONTROL5 0x45
108 #define BU27010_MASK_RGB_VALID BIT(7)
109 #define BU27010_MASK_FLC_VALID BIT(6)
110 #define BU27010_MASK_WAIT_EN BIT(3)
111 #define BU27010_MASK_FIFO_EN BIT(2)
112 #define BU27010_MASK_RGB_EN BIT(1)
113 #define BU27010_MASK_FLC_EN BIT(0)
115 #define BU27010_REG_DATA_FLICKER_LO 0x56
116 #define BU27010_MASK_DATA_FLICKER_HI GENMASK(2, 0)
117 #define BU27010_REG_FLICKER_COUNT 0x5a
118 #define BU27010_REG_FIFO_LEVEL_LO 0x5b
119 #define BU27010_MASK_FIFO_LEVEL_HI BIT(0)
120 #define BU27010_REG_FIFO_DATA_LO 0x5d
121 #define BU27010_REG_FIFO_DATA_HI 0x5e
122 #define BU27010_MASK_FIFO_DATA_HI GENMASK(2, 0)
123 #define BU27010_REG_MANUFACTURER_ID 0x92
124 #define BU27010_REG_MAX BU27010_REG_MANUFACTURER_ID
127 * enum bu27008_chan_type - BU27008 channel types
128 * @BU27008_RED: Red channel. Always via data0.
129 * @BU27008_GREEN: Green channel. Always via data1.
130 * @BU27008_BLUE: Blue channel. Via data2 (when used).
131 * @BU27008_CLEAR: Clear channel. Via data2 or data3 (when used).
132 * @BU27008_IR: IR channel. Via data3 (when used).
133 * @BU27008_NUM_CHANS: Number of channel types.
135 enum bu27008_chan_type {
145 * enum bu27008_chan - BU27008 physical data channel
146 * @BU27008_DATA0: Always red.
147 * @BU27008_DATA1: Always green.
148 * @BU27008_DATA2: Blue or clear.
149 * @BU27008_DATA3: IR or clear.
150 * @BU27008_NUM_HW_CHANS: Number of physical channels
160 /* We can always measure red and green at same time */
161 #define ALWAYS_SCANNABLE (BIT(BU27008_RED) | BIT(BU27008_GREEN))
163 /* We use these data channel configs. Ensure scan_masks below follow them too */
164 #define BU27008_BLUE2_CLEAR3 0x0 /* buffer is R, G, B, C */
165 #define BU27008_CLEAR2_IR3 0x1 /* buffer is R, G, C, IR */
166 #define BU27008_BLUE2_IR3 0x2 /* buffer is R, G, B, IR */
168 static const unsigned long bu27008_scan_masks[] = {
169 /* buffer is R, G, B, C */
170 ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_CLEAR),
171 /* buffer is R, G, C, IR */
172 ALWAYS_SCANNABLE | BIT(BU27008_CLEAR) | BIT(BU27008_IR),
173 /* buffer is R, G, B, IR */
174 ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_IR),
179 * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS
180 * Time impacts to gain: 1x, 2x, 4x, 8x.
182 * => Max total gain is HWGAIN * gain by integration time (8 * 1024) = 8192
184 * Max amplification is (HWGAIN * MAX integration-time multiplier) 1024 * 8
185 * = 8192. With NANO scale we get rid of accuracy loss when we start with the
186 * scale 16.0 for HWGAIN1, INT-TIME 55 mS. This way the nano scale for MAX
187 * total gain 8192 will be 1953125
189 #define BU27008_SCALE_1X 16
192 * On BU27010 available scales with gain 1x - 4096x,
193 * timings 55, 100, 200, 400 mS. Time impacts to gain: 1x, 2x, 4x, 8x.
195 * => Max total gain is HWGAIN * gain by integration time (8 * 4096)
197 * Using NANO precision for scale we must use scale 64x corresponding gain 1x
198 * to avoid precision loss.
200 #define BU27010_SCALE_1X 64
202 /* See the data sheet for the "Gain Setting" table */
203 #define BU27008_GSEL_1X 0x00
204 #define BU27008_GSEL_4X 0x08
205 #define BU27008_GSEL_8X 0x09
206 #define BU27008_GSEL_16X 0x0a
207 #define BU27008_GSEL_32X 0x0b
208 #define BU27008_GSEL_64X 0x0c
209 #define BU27008_GSEL_256X 0x18
210 #define BU27008_GSEL_512X 0x19
211 #define BU27008_GSEL_1024X 0x1a
213 static const struct iio_gain_sel_pair bu27008_gains[] = {
214 GAIN_SCALE_GAIN(1, BU27008_GSEL_1X),
215 GAIN_SCALE_GAIN(4, BU27008_GSEL_4X),
216 GAIN_SCALE_GAIN(8, BU27008_GSEL_8X),
217 GAIN_SCALE_GAIN(16, BU27008_GSEL_16X),
218 GAIN_SCALE_GAIN(32, BU27008_GSEL_32X),
219 GAIN_SCALE_GAIN(64, BU27008_GSEL_64X),
220 GAIN_SCALE_GAIN(256, BU27008_GSEL_256X),
221 GAIN_SCALE_GAIN(512, BU27008_GSEL_512X),
222 GAIN_SCALE_GAIN(1024, BU27008_GSEL_1024X),
225 static const struct iio_gain_sel_pair bu27008_gains_ir[] = {
226 GAIN_SCALE_GAIN(2, BU27008_GSEL_1X),
227 GAIN_SCALE_GAIN(4, BU27008_GSEL_4X),
228 GAIN_SCALE_GAIN(8, BU27008_GSEL_8X),
229 GAIN_SCALE_GAIN(16, BU27008_GSEL_16X),
230 GAIN_SCALE_GAIN(32, BU27008_GSEL_32X),
231 GAIN_SCALE_GAIN(64, BU27008_GSEL_64X),
232 GAIN_SCALE_GAIN(256, BU27008_GSEL_256X),
233 GAIN_SCALE_GAIN(512, BU27008_GSEL_512X),
234 GAIN_SCALE_GAIN(1024, BU27008_GSEL_1024X),
237 #define BU27010_GSEL_1X 0x00 /* 000000 */
238 #define BU27010_GSEL_4X 0x08 /* 001000 */
239 #define BU27010_GSEL_16X 0x09 /* 001001 */
240 #define BU27010_GSEL_64X 0x0e /* 001110 */
241 #define BU27010_GSEL_256X 0x1e /* 011110 */
242 #define BU27010_GSEL_1024X 0x2e /* 101110 */
243 #define BU27010_GSEL_4096X 0x3f /* 111111 */
245 static const struct iio_gain_sel_pair bu27010_gains[] = {
246 GAIN_SCALE_GAIN(1, BU27010_GSEL_1X),
247 GAIN_SCALE_GAIN(4, BU27010_GSEL_4X),
248 GAIN_SCALE_GAIN(16, BU27010_GSEL_16X),
249 GAIN_SCALE_GAIN(64, BU27010_GSEL_64X),
250 GAIN_SCALE_GAIN(256, BU27010_GSEL_256X),
251 GAIN_SCALE_GAIN(1024, BU27010_GSEL_1024X),
252 GAIN_SCALE_GAIN(4096, BU27010_GSEL_4096X),
255 static const struct iio_gain_sel_pair bu27010_gains_ir[] = {
256 GAIN_SCALE_GAIN(2, BU27010_GSEL_1X),
257 GAIN_SCALE_GAIN(4, BU27010_GSEL_4X),
258 GAIN_SCALE_GAIN(16, BU27010_GSEL_16X),
259 GAIN_SCALE_GAIN(64, BU27010_GSEL_64X),
260 GAIN_SCALE_GAIN(256, BU27010_GSEL_256X),
261 GAIN_SCALE_GAIN(1024, BU27010_GSEL_1024X),
262 GAIN_SCALE_GAIN(4096, BU27010_GSEL_4096X),
265 #define BU27008_MEAS_MODE_100MS 0x00
266 #define BU27008_MEAS_MODE_55MS 0x01
267 #define BU27008_MEAS_MODE_200MS 0x02
268 #define BU27008_MEAS_MODE_400MS 0x04
270 #define BU27010_MEAS_MODE_100MS 0x00
271 #define BU27010_MEAS_MODE_55MS 0x03
272 #define BU27010_MEAS_MODE_200MS 0x01
273 #define BU27010_MEAS_MODE_400MS 0x02
275 #define BU27008_MEAS_TIME_MAX_MS 400
277 static const struct iio_itime_sel_mul bu27008_itimes[] = {
278 GAIN_SCALE_ITIME_US(400000, BU27008_MEAS_MODE_400MS, 8),
279 GAIN_SCALE_ITIME_US(200000, BU27008_MEAS_MODE_200MS, 4),
280 GAIN_SCALE_ITIME_US(100000, BU27008_MEAS_MODE_100MS, 2),
281 GAIN_SCALE_ITIME_US(55000, BU27008_MEAS_MODE_55MS, 1),
284 static const struct iio_itime_sel_mul bu27010_itimes[] = {
285 GAIN_SCALE_ITIME_US(400000, BU27010_MEAS_MODE_400MS, 8),
286 GAIN_SCALE_ITIME_US(200000, BU27010_MEAS_MODE_200MS, 4),
287 GAIN_SCALE_ITIME_US(100000, BU27010_MEAS_MODE_100MS, 2),
288 GAIN_SCALE_ITIME_US(55000, BU27010_MEAS_MODE_55MS, 1),
292 * All the RGBC channels share the same gain.
293 * IR gain can be fine-tuned from the gain set for the RGBC by 2 bit, but this
294 * would yield quite complex gain setting. Especially since not all bit
295 * compinations are supported. And in any case setting GAIN for RGBC will
296 * always also change the IR-gain.
298 * On top of this, the selector '0' which corresponds to hw-gain 1X on RGBC,
299 * corresponds to gain 2X on IR. Rest of the selctors correspond to same gains
300 * though. This, however, makes it not possible to use shared gain for all
301 * RGBC and IR settings even though they are all changed at the one go.
303 #define BU27008_CHAN(color, data, separate_avail) \
305 .type = IIO_INTENSITY, \
307 .channel2 = IIO_MOD_LIGHT_##color, \
308 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
309 BIT(IIO_CHAN_INFO_SCALE), \
310 .info_mask_separate_available = (separate_avail), \
311 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
312 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), \
313 .address = BU27008_REG_##data##_LO, \
314 .scan_index = BU27008_##color, \
319 .endianness = IIO_LE, \
323 /* For raw reads we always configure DATA3 for CLEAR */
324 static const struct iio_chan_spec bu27008_channels[] = {
325 BU27008_CHAN(RED, DATA0, BIT(IIO_CHAN_INFO_SCALE)),
326 BU27008_CHAN(GREEN, DATA1, BIT(IIO_CHAN_INFO_SCALE)),
327 BU27008_CHAN(BLUE, DATA2, BIT(IIO_CHAN_INFO_SCALE)),
328 BU27008_CHAN(CLEAR, DATA2, BIT(IIO_CHAN_INFO_SCALE)),
330 * We don't allow setting scale for IR (because of shared gain bits).
331 * Hence we don't advertise available ones either.
333 BU27008_CHAN(IR, DATA3, 0),
334 IIO_CHAN_SOFT_TIMESTAMP(BU27008_NUM_CHANS),
339 struct bu27_chip_data {
341 int (*chip_init)(struct bu27008_data *data);
342 int (*get_gain_sel)(struct bu27008_data *data, int *sel);
343 int (*write_gain_sel)(struct bu27008_data *data, int sel);
344 const struct regmap_config *regmap_cfg;
345 const struct iio_gain_sel_pair *gains;
346 const struct iio_gain_sel_pair *gains_ir;
347 const struct iio_itime_sel_mul *itimes;
364 struct bu27008_data {
365 const struct bu27_chip_data *cd;
366 struct regmap *regmap;
367 struct iio_trigger *trig;
370 struct iio_gts gts_ir;
374 * Prevent changing gain/time config when scale is read/written.
375 * Similarly, protect the integration_time read/change sequence.
376 * Prevent changing gain/time when data is read.
381 static const struct regmap_range bu27008_volatile_ranges[] = {
383 .range_min = BU27008_REG_SYSTEM_CONTROL, /* SWRESET */
384 .range_max = BU27008_REG_SYSTEM_CONTROL,
386 .range_min = BU27008_REG_MODE_CONTROL3, /* VALID */
387 .range_max = BU27008_REG_MODE_CONTROL3,
389 .range_min = BU27008_REG_DATA0_LO, /* DATA */
390 .range_max = BU27008_REG_DATA3_HI,
394 static const struct regmap_range bu27010_volatile_ranges[] = {
396 .range_min = BU27010_REG_RESET, /* RSTB */
397 .range_max = BU27008_REG_SYSTEM_CONTROL, /* RESET */
399 .range_min = BU27010_REG_MODE_CONTROL5, /* VALID bits */
400 .range_max = BU27010_REG_MODE_CONTROL5,
402 .range_min = BU27008_REG_DATA0_LO,
403 .range_max = BU27010_REG_FIFO_DATA_HI,
407 static const struct regmap_access_table bu27008_volatile_regs = {
408 .yes_ranges = &bu27008_volatile_ranges[0],
409 .n_yes_ranges = ARRAY_SIZE(bu27008_volatile_ranges),
412 static const struct regmap_access_table bu27010_volatile_regs = {
413 .yes_ranges = &bu27010_volatile_ranges[0],
414 .n_yes_ranges = ARRAY_SIZE(bu27010_volatile_ranges),
417 static const struct regmap_range bu27008_read_only_ranges[] = {
419 .range_min = BU27008_REG_DATA0_LO,
420 .range_max = BU27008_REG_DATA3_HI,
422 .range_min = BU27008_REG_MANUFACTURER_ID,
423 .range_max = BU27008_REG_MANUFACTURER_ID,
427 static const struct regmap_range bu27010_read_only_ranges[] = {
429 .range_min = BU27008_REG_DATA0_LO,
430 .range_max = BU27010_REG_FIFO_DATA_HI,
432 .range_min = BU27010_REG_MANUFACTURER_ID,
433 .range_max = BU27010_REG_MANUFACTURER_ID,
437 static const struct regmap_access_table bu27008_ro_regs = {
438 .no_ranges = &bu27008_read_only_ranges[0],
439 .n_no_ranges = ARRAY_SIZE(bu27008_read_only_ranges),
442 static const struct regmap_access_table bu27010_ro_regs = {
443 .no_ranges = &bu27010_read_only_ranges[0],
444 .n_no_ranges = ARRAY_SIZE(bu27010_read_only_ranges),
447 static const struct regmap_config bu27008_regmap = {
450 .max_register = BU27008_REG_MAX,
451 .cache_type = REGCACHE_RBTREE,
452 .volatile_table = &bu27008_volatile_regs,
453 .wr_table = &bu27008_ro_regs,
455 * All register writes are serialized by the mutex which protects the
456 * scale setting/getting. This is needed because scale is combined by
457 * gain and integration time settings and we need to ensure those are
458 * not read / written when scale is being computed.
460 * As a result of this serializing, we don't need regmap locking. Note,
461 * this is not true if we add any configurations which are not
462 * serialized by the mutex and which may need for example a protected
463 * read-modify-write cycle (eg. regmap_update_bits()). Please, revise
464 * this when adding features to the driver.
466 .disable_locking = true,
469 static const struct regmap_config bu27010_regmap = {
473 .max_register = BU27010_REG_MAX,
474 .cache_type = REGCACHE_RBTREE,
475 .volatile_table = &bu27010_volatile_regs,
476 .wr_table = &bu27010_ro_regs,
477 .disable_locking = true,
480 static int bu27008_write_gain_sel(struct bu27008_data *data, int sel)
484 regval = FIELD_PREP(BU27008_MASK_RGBC_GAIN, sel);
487 * We do always set also the LOW bits of IR-gain because othervice we
488 * would risk resulting an invalid GAIN register value.
490 * We could allow setting separate gains for RGBC and IR when the
491 * values were such that HW could support both gain settings.
492 * Eg, when the shared bits were same for both gain values.
494 * This, however, has a negligible benefit compared to the increased
495 * software complexity when we would need to go through the gains
496 * for both channels separately when the integration time changes.
497 * This would end up with nasty logic for computing gain values for
498 * both channels - and rejecting them if shared bits changed.
500 * We should then build the logic by guessing what a user prefers.
501 * RGBC or IR gains correctly set while other jumps to odd value?
502 * Maybe look-up a value where both gains are somehow optimized
503 * <what this somehow is, is ATM unknown to us>. Or maybe user would
504 * expect us to reject changes when optimal gains can't be set to both
505 * channels w/given integration time. At best that would result
506 * solution that works well for a very specific subset of
507 * configurations but causes unexpected corner-cases.
509 * So, we keep it simple. Always set same selector to IR and RGBC.
510 * We disallow setting IR (as I expect that most of the users are
511 * interested in RGBC). This way we can show the user that the scales
512 * for RGBC and IR channels are different (1X Vs 2X with sel 0) while
513 * still keeping the operation deterministic.
515 regval |= FIELD_PREP(BU27008_MASK_IR_GAIN_LO, sel);
517 return regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL2,
518 BU27008_MASK_RGBC_GAIN, regval);
521 static int bu27010_write_gain_sel(struct bu27008_data *data, int sel)
524 int ret, chan_selector;
527 * Gain 'selector' is composed of two registers. Selector is 6bit value,
528 * 4 high bits being the RGBC gain fieild in MODE_CONTROL1 register and
529 * two low bits being the channel specific gain in MODE_CONTROL2.
531 * Let's take the 4 high bits of whole 6 bit selector, and prepare
532 * the MODE_CONTROL1 value (RGBC gain part).
534 regval = FIELD_PREP(BU27010_MASK_RGBC_GAIN, (sel >> 2));
536 ret = regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL1,
537 BU27010_MASK_RGBC_GAIN, regval);
542 * Two low two bits of the selector must be written for all 4
543 * channels in the MODE_CONTROL2 register. Copy these two bits for
546 chan_selector = sel & GENMASK(1, 0);
548 regval = FIELD_PREP(BU27010_MASK_DATA0_GAIN, chan_selector);
549 regval |= FIELD_PREP(BU27010_MASK_DATA1_GAIN, chan_selector);
550 regval |= FIELD_PREP(BU27010_MASK_DATA2_GAIN, chan_selector);
551 regval |= FIELD_PREP(BU27010_MASK_DATA3_GAIN, chan_selector);
553 return regmap_write(data->regmap, BU27008_REG_MODE_CONTROL2, regval);
556 static int bu27008_get_gain_sel(struct bu27008_data *data, int *sel)
561 * If we always "lock" the gain selectors for all channels to prevent
562 * unsupported configs, then it does not matter which channel is used
563 * we can just return selector from any of them.
565 * This, however is not true if we decide to support only 4X and 16X
566 * and then individual gains for channels. Currently this is not the
569 * If we some day decide to support individual gains, then we need to
570 * have channel information here.
573 ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL2, sel);
577 *sel = FIELD_GET(BU27008_MASK_RGBC_GAIN, *sel);
582 static int bu27010_get_gain_sel(struct bu27008_data *data, int *sel)
587 * We always "lock" the gain selectors for all channels to prevent
588 * unsupported configs. It does not matter which channel is used
589 * we can just return selector from any of them.
591 * Read the channel0 gain.
593 ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL2, sel);
597 *sel = FIELD_GET(BU27010_MASK_DATA0_GAIN, *sel);
599 /* Read the shared gain */
600 ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL1, &tmp);
605 * The gain selector is made as a combination of common RGBC gain and
606 * the channel specific gain. The channel specific gain forms the low
607 * bits of selector and RGBC gain is appended right after it.
609 * Compose the selector from channel0 gain and shared RGBC gain.
611 *sel |= FIELD_GET(BU27010_MASK_RGBC_GAIN, tmp) << fls(BU27010_MASK_DATA0_GAIN);
616 static int bu27008_chip_init(struct bu27008_data *data)
620 ret = regmap_write_bits(data->regmap, BU27008_REG_SYSTEM_CONTROL,
621 BU27008_MASK_SW_RESET, BU27008_MASK_SW_RESET);
623 return dev_err_probe(data->dev, ret, "Sensor reset failed\n");
626 * The data-sheet does not tell how long performing the IC reset takes.
627 * However, the data-sheet says the minimum time it takes the IC to be
628 * able to take inputs after power is applied, is 100 uS. I'd assume
633 ret = regmap_reinit_cache(data->regmap, data->cd->regmap_cfg);
635 dev_err(data->dev, "Failed to reinit reg cache\n");
640 static int bu27010_chip_init(struct bu27008_data *data)
644 ret = regmap_write_bits(data->regmap, BU27008_REG_SYSTEM_CONTROL,
645 BU27010_MASK_SW_RESET, BU27010_MASK_SW_RESET);
647 return dev_err_probe(data->dev, ret, "Sensor reset failed\n");
652 ret = regmap_write_bits(data->regmap, BU27010_REG_POWER,
653 BU27010_MASK_POWER, BU27010_MASK_POWER);
655 return dev_err_probe(data->dev, ret, "Sensor power-on failed\n");
659 /* Release blocks from reset */
660 ret = regmap_write_bits(data->regmap, BU27010_REG_RESET,
661 BU27010_MASK_RESET, BU27010_RESET_RELEASE);
663 return dev_err_probe(data->dev, ret, "Sensor powering failed\n");
668 * The IRQ enabling on BU27010 is done in a peculiar way. The IRQ
669 * enabling is not a bit mask where individual IRQs could be enabled but
670 * a field which values are:
671 * 00 => IRQs disabled
672 * 01 => Data-ready (RGBC/IR)
673 * 10 => Data-ready (flicker)
676 * So, only one IRQ can be enabled at a time and enabling for example
677 * flicker FIFO would automagically disable data-ready IRQ.
679 * Currently the driver does not support the flicker. Hence, we can
680 * just treat the RGBC data-ready as single bit which can be enabled /
681 * disabled. This works for as long as the second bit in the field
682 * stays zero. Here we ensure it gets zeroed.
684 return regmap_clear_bits(data->regmap, BU27010_REG_MODE_CONTROL4,
685 BU27010_IRQ_DIS_ALL);
688 static const struct bu27_chip_data bu27010_chip = {
690 .chip_init = bu27010_chip_init,
691 .get_gain_sel = bu27010_get_gain_sel,
692 .write_gain_sel = bu27010_write_gain_sel,
693 .regmap_cfg = &bu27010_regmap,
694 .gains = &bu27010_gains[0],
695 .gains_ir = &bu27010_gains_ir[0],
696 .itimes = &bu27010_itimes[0],
697 .num_gains = ARRAY_SIZE(bu27010_gains),
698 .num_gains_ir = ARRAY_SIZE(bu27010_gains_ir),
699 .num_itimes = ARRAY_SIZE(bu27010_itimes),
700 .scale1x = BU27010_SCALE_1X,
701 .drdy_en_reg = BU27010_REG_MODE_CONTROL4,
702 .drdy_en_mask = BU27010_DRDY_EN,
703 .meas_en_reg = BU27010_REG_MODE_CONTROL5,
704 .meas_en_mask = BU27010_MASK_MEAS_EN,
705 .valid_reg = BU27010_REG_MODE_CONTROL5,
706 .chan_sel_reg = BU27008_REG_MODE_CONTROL1,
707 .chan_sel_mask = BU27010_MASK_CHAN_SEL,
708 .int_time_mask = BU27010_MASK_MEAS_MODE,
709 .part_id = BU27010_ID,
712 static const struct bu27_chip_data bu27008_chip = {
714 .chip_init = bu27008_chip_init,
715 .get_gain_sel = bu27008_get_gain_sel,
716 .write_gain_sel = bu27008_write_gain_sel,
717 .regmap_cfg = &bu27008_regmap,
718 .gains = &bu27008_gains[0],
719 .gains_ir = &bu27008_gains_ir[0],
720 .itimes = &bu27008_itimes[0],
721 .num_gains = ARRAY_SIZE(bu27008_gains),
722 .num_gains_ir = ARRAY_SIZE(bu27008_gains_ir),
723 .num_itimes = ARRAY_SIZE(bu27008_itimes),
724 .scale1x = BU27008_SCALE_1X,
725 .drdy_en_reg = BU27008_REG_MODE_CONTROL3,
726 .drdy_en_mask = BU27008_MASK_INT_EN,
727 .valid_reg = BU27008_REG_MODE_CONTROL3,
728 .meas_en_reg = BU27008_REG_MODE_CONTROL3,
729 .meas_en_mask = BU27008_MASK_MEAS_EN,
730 .chan_sel_reg = BU27008_REG_MODE_CONTROL3,
731 .chan_sel_mask = BU27008_MASK_CHAN_SEL,
732 .int_time_mask = BU27008_MASK_MEAS_MODE,
733 .part_id = BU27008_ID,
736 #define BU27008_MAX_VALID_RESULT_WAIT_US 50000
737 #define BU27008_VALID_RESULT_WAIT_QUANTA_US 1000
739 static int bu27008_chan_read_data(struct bu27008_data *data, int reg, int *val)
744 ret = regmap_read_poll_timeout(data->regmap, data->cd->valid_reg,
745 valid, (valid & BU27008_MASK_VALID),
746 BU27008_VALID_RESULT_WAIT_QUANTA_US,
747 BU27008_MAX_VALID_RESULT_WAIT_US);
751 ret = regmap_bulk_read(data->regmap, reg, &tmp, sizeof(tmp));
753 dev_err(data->dev, "Reading channel data failed\n");
755 *val = le16_to_cpu(tmp);
760 static int bu27008_get_gain(struct bu27008_data *data, struct iio_gts *gts, int *gain)
764 ret = data->cd->get_gain_sel(data, &sel);
768 ret = iio_gts_find_gain_by_sel(gts, sel);
770 dev_err(data->dev, "unknown gain value 0x%x\n", sel);
779 static int bu27008_set_gain(struct bu27008_data *data, int gain)
783 ret = iio_gts_find_sel_by_gain(&data->gts, gain);
787 return data->cd->write_gain_sel(data, ret);
790 static int bu27008_get_int_time_sel(struct bu27008_data *data, int *sel)
794 ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL1, &val);
798 val &= data->cd->int_time_mask;
799 val >>= ffs(data->cd->int_time_mask) - 1;
806 static int bu27008_set_int_time_sel(struct bu27008_data *data, int sel)
808 sel <<= ffs(data->cd->int_time_mask) - 1;
810 return regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL1,
811 data->cd->int_time_mask, sel);
814 static int bu27008_get_int_time_us(struct bu27008_data *data)
818 ret = bu27008_get_int_time_sel(data, &sel);
822 return iio_gts_find_int_time_by_sel(&data->gts, sel);
825 static int _bu27008_get_scale(struct bu27008_data *data, bool ir, int *val,
836 ret = bu27008_get_gain(data, gts, &gain);
840 ret = bu27008_get_int_time_us(data);
844 return iio_gts_get_scale(gts, gain, ret, val, val2);
847 static int bu27008_get_scale(struct bu27008_data *data, bool ir, int *val,
852 mutex_lock(&data->mutex);
853 ret = _bu27008_get_scale(data, ir, val, val2);
854 mutex_unlock(&data->mutex);
859 static int bu27008_set_int_time(struct bu27008_data *data, int time)
863 ret = iio_gts_find_sel_by_int_time(&data->gts, time);
867 return bu27008_set_int_time_sel(data, ret);
870 /* Try to change the time so that the scale is maintained */
871 static int bu27008_try_set_int_time(struct bu27008_data *data, int int_time_new)
873 int ret, old_time_sel, new_time_sel, old_gain, new_gain;
875 mutex_lock(&data->mutex);
877 ret = bu27008_get_int_time_sel(data, &old_time_sel);
881 if (!iio_gts_valid_time(&data->gts, int_time_new)) {
882 dev_dbg(data->dev, "Unsupported integration time %u\n",
889 /* If we already use requested time, then we're done */
890 new_time_sel = iio_gts_find_sel_by_int_time(&data->gts, int_time_new);
891 if (new_time_sel == old_time_sel)
894 ret = bu27008_get_gain(data, &data->gts, &old_gain);
898 ret = iio_gts_find_new_gain_sel_by_old_gain_time(&data->gts, old_gain,
899 old_time_sel, new_time_sel, &new_gain);
904 _bu27008_get_scale(data, false, &scale1, &scale2);
906 "Can't support time %u with current scale %u %u\n",
907 int_time_new, scale1, scale2);
913 * If caller requests for integration time change and we
914 * can't support the scale - then the caller should be
915 * prepared to 'pick up the pieces and deal with the
916 * fact that the scale changed'.
918 ret = iio_find_closest_gain_low(&data->gts, new_gain, &ok);
920 dev_dbg(data->dev, "optimal gain out of range\n");
924 "Total gain increase. Risk of saturation");
925 ret = iio_gts_get_min_gain(&data->gts);
930 dev_dbg(data->dev, "scale changed, new gain %u\n", new_gain);
933 ret = bu27008_set_gain(data, new_gain);
937 ret = bu27008_set_int_time(data, int_time_new);
940 mutex_unlock(&data->mutex);
945 static int bu27008_meas_set(struct bu27008_data *data, bool enable)
948 return regmap_set_bits(data->regmap, data->cd->meas_en_reg,
949 data->cd->meas_en_mask);
950 return regmap_clear_bits(data->regmap, data->cd->meas_en_reg,
951 data->cd->meas_en_mask);
954 static int bu27008_chan_cfg(struct bu27008_data *data,
955 struct iio_chan_spec const *chan)
959 if (chan->scan_index == BU27008_BLUE)
960 chan_sel = BU27008_BLUE2_CLEAR3;
962 chan_sel = BU27008_CLEAR2_IR3;
965 * prepare bitfield for channel sel. The FIELD_PREP works only when
966 * mask is constant. In our case the mask is assigned based on the
967 * chip type. Hence the open-coded FIELD_PREP here. We don't bother
968 * zeroing the irrelevant bits though - update_bits takes care of that.
970 chan_sel <<= ffs(data->cd->chan_sel_mask) - 1;
972 return regmap_update_bits(data->regmap, data->cd->chan_sel_reg,
973 BU27008_MASK_CHAN_SEL, chan_sel);
976 static int bu27008_read_one(struct bu27008_data *data, struct iio_dev *idev,
977 struct iio_chan_spec const *chan, int *val, int *val2)
981 ret = bu27008_chan_cfg(data, chan);
985 ret = bu27008_meas_set(data, true);
989 ret = bu27008_get_int_time_us(data);
991 int_time = BU27008_MEAS_TIME_MAX_MS;
993 int_time = ret / USEC_PER_MSEC;
997 ret = bu27008_chan_read_data(data, chan->address, val);
1001 if (bu27008_meas_set(data, false))
1002 dev_warn(data->dev, "measurement disabling failed\n");
1007 static int bu27008_read_raw(struct iio_dev *idev,
1008 struct iio_chan_spec const *chan,
1009 int *val, int *val2, long mask)
1011 struct bu27008_data *data = iio_priv(idev);
1015 case IIO_CHAN_INFO_RAW:
1016 busy = iio_device_claim_direct_mode(idev);
1020 mutex_lock(&data->mutex);
1021 ret = bu27008_read_one(data, idev, chan, val, val2);
1022 mutex_unlock(&data->mutex);
1024 iio_device_release_direct_mode(idev);
1028 case IIO_CHAN_INFO_SCALE:
1029 ret = bu27008_get_scale(data, chan->scan_index == BU27008_IR,
1034 return IIO_VAL_INT_PLUS_NANO;
1036 case IIO_CHAN_INFO_INT_TIME:
1037 ret = bu27008_get_int_time_us(data);
1044 return IIO_VAL_INT_PLUS_MICRO;
1051 /* Called if the new scale could not be supported with existing int-time */
1052 static int bu27008_try_find_new_time_gain(struct bu27008_data *data, int val,
1053 int val2, int *gain_sel)
1055 int i, ret, new_time_sel;
1057 for (i = 0; i < data->gts.num_itime; i++) {
1058 new_time_sel = data->gts.itime_table[i].sel;
1059 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
1060 new_time_sel, val, val2, gain_sel);
1064 if (i == data->gts.num_itime) {
1065 dev_err(data->dev, "Can't support scale %u %u\n", val, val2);
1070 return bu27008_set_int_time_sel(data, new_time_sel);
1073 static int bu27008_set_scale(struct bu27008_data *data,
1074 struct iio_chan_spec const *chan,
1077 int ret, gain_sel, time_sel;
1079 if (chan->scan_index == BU27008_IR)
1082 mutex_lock(&data->mutex);
1084 ret = bu27008_get_int_time_sel(data, &time_sel);
1088 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel,
1089 val, val2, &gain_sel);
1091 ret = bu27008_try_find_new_time_gain(data, val, val2, &gain_sel);
1096 ret = data->cd->write_gain_sel(data, gain_sel);
1099 mutex_unlock(&data->mutex);
1104 static int bu27008_write_raw_get_fmt(struct iio_dev *indio_dev,
1105 struct iio_chan_spec const *chan,
1110 case IIO_CHAN_INFO_SCALE:
1111 return IIO_VAL_INT_PLUS_NANO;
1112 case IIO_CHAN_INFO_INT_TIME:
1113 return IIO_VAL_INT_PLUS_MICRO;
1119 static int bu27008_write_raw(struct iio_dev *idev,
1120 struct iio_chan_spec const *chan,
1121 int val, int val2, long mask)
1123 struct bu27008_data *data = iio_priv(idev);
1127 * Do not allow changing scale when measurement is ongoing as doing so
1128 * could make values in the buffer inconsistent.
1130 ret = iio_device_claim_direct_mode(idev);
1135 case IIO_CHAN_INFO_SCALE:
1136 ret = bu27008_set_scale(data, chan, val, val2);
1138 case IIO_CHAN_INFO_INT_TIME:
1143 ret = bu27008_try_set_int_time(data, val2);
1149 iio_device_release_direct_mode(idev);
1154 static int bu27008_read_avail(struct iio_dev *idev,
1155 struct iio_chan_spec const *chan, const int **vals,
1156 int *type, int *length, long mask)
1158 struct bu27008_data *data = iio_priv(idev);
1161 case IIO_CHAN_INFO_INT_TIME:
1162 return iio_gts_avail_times(&data->gts, vals, type, length);
1163 case IIO_CHAN_INFO_SCALE:
1164 if (chan->channel2 == IIO_MOD_LIGHT_IR)
1165 return iio_gts_all_avail_scales(&data->gts_ir, vals,
1167 return iio_gts_all_avail_scales(&data->gts, vals, type, length);
1173 static int bu27008_update_scan_mode(struct iio_dev *idev,
1174 const unsigned long *scan_mask)
1176 struct bu27008_data *data = iio_priv(idev);
1179 /* Configure channel selection */
1180 if (test_bit(BU27008_BLUE, idev->active_scan_mask)) {
1181 if (test_bit(BU27008_CLEAR, idev->active_scan_mask))
1182 chan_sel = BU27008_BLUE2_CLEAR3;
1184 chan_sel = BU27008_BLUE2_IR3;
1186 chan_sel = BU27008_CLEAR2_IR3;
1189 chan_sel <<= ffs(data->cd->chan_sel_mask) - 1;
1191 return regmap_update_bits(data->regmap, data->cd->chan_sel_reg,
1192 data->cd->chan_sel_mask, chan_sel);
1195 static const struct iio_info bu27008_info = {
1196 .read_raw = &bu27008_read_raw,
1197 .write_raw = &bu27008_write_raw,
1198 .write_raw_get_fmt = &bu27008_write_raw_get_fmt,
1199 .read_avail = &bu27008_read_avail,
1200 .update_scan_mode = bu27008_update_scan_mode,
1201 .validate_trigger = iio_validate_own_trigger,
1204 static int bu27008_trigger_set_state(struct iio_trigger *trig, bool state)
1206 struct bu27008_data *data = iio_trigger_get_drvdata(trig);
1211 ret = regmap_set_bits(data->regmap, data->cd->drdy_en_reg,
1212 data->cd->drdy_en_mask);
1214 ret = regmap_clear_bits(data->regmap, data->cd->drdy_en_reg,
1215 data->cd->drdy_en_mask);
1217 dev_err(data->dev, "Failed to set trigger state\n");
1222 static void bu27008_trigger_reenable(struct iio_trigger *trig)
1224 struct bu27008_data *data = iio_trigger_get_drvdata(trig);
1226 enable_irq(data->irq);
1229 static const struct iio_trigger_ops bu27008_trigger_ops = {
1230 .set_trigger_state = bu27008_trigger_set_state,
1231 .reenable = bu27008_trigger_reenable,
1234 static irqreturn_t bu27008_trigger_handler(int irq, void *p)
1236 struct iio_poll_func *pf = p;
1237 struct iio_dev *idev = pf->indio_dev;
1238 struct bu27008_data *data = iio_priv(idev);
1240 __le16 chan[BU27008_NUM_HW_CHANS];
1241 s64 ts __aligned(8);
1245 memset(&raw, 0, sizeof(raw));
1248 * After some measurements, it seems reading the
1249 * BU27008_REG_MODE_CONTROL3 debounces the IRQ line
1251 ret = regmap_read(data->regmap, data->cd->valid_reg, &dummy);
1255 ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, &raw.chan,
1260 iio_push_to_buffers_with_timestamp(idev, &raw, pf->timestamp);
1262 iio_trigger_notify_done(idev->trig);
1267 static int bu27008_buffer_preenable(struct iio_dev *idev)
1269 struct bu27008_data *data = iio_priv(idev);
1271 return bu27008_meas_set(data, true);
1274 static int bu27008_buffer_postdisable(struct iio_dev *idev)
1276 struct bu27008_data *data = iio_priv(idev);
1278 return bu27008_meas_set(data, false);
1281 static const struct iio_buffer_setup_ops bu27008_buffer_ops = {
1282 .preenable = bu27008_buffer_preenable,
1283 .postdisable = bu27008_buffer_postdisable,
1286 static irqreturn_t bu27008_data_rdy_poll(int irq, void *private)
1289 * The BU27008 keeps IRQ asserted until we read the VALID bit from
1290 * a register. We need to keep the IRQ disabled until then.
1292 disable_irq_nosync(irq);
1293 iio_trigger_poll(private);
1298 static int bu27008_setup_trigger(struct bu27008_data *data, struct iio_dev *idev)
1300 struct iio_trigger *itrig;
1304 ret = devm_iio_triggered_buffer_setup(data->dev, idev,
1305 &iio_pollfunc_store_time,
1306 bu27008_trigger_handler,
1307 &bu27008_buffer_ops);
1309 return dev_err_probe(data->dev, ret,
1310 "iio_triggered_buffer_setup_ext FAIL\n");
1312 itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d",
1313 idev->name, iio_device_id(idev));
1319 itrig->ops = &bu27008_trigger_ops;
1320 iio_trigger_set_drvdata(itrig, data);
1322 name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bu27008",
1323 dev_name(data->dev));
1325 ret = devm_request_irq(data->dev, data->irq,
1326 &bu27008_data_rdy_poll,
1329 return dev_err_probe(data->dev, ret, "Could not request IRQ\n");
1331 ret = devm_iio_trigger_register(data->dev, itrig);
1333 return dev_err_probe(data->dev, ret,
1334 "Trigger registration failed\n");
1336 /* set default trigger */
1337 idev->trig = iio_trigger_get(itrig);
1342 static int bu27008_probe(struct i2c_client *i2c)
1344 struct device *dev = &i2c->dev;
1345 struct bu27008_data *data;
1346 struct regmap *regmap;
1347 unsigned int part_id, reg;
1348 struct iio_dev *idev;
1351 idev = devm_iio_device_alloc(dev, sizeof(*data));
1355 ret = devm_regulator_get_enable(dev, "vdd");
1357 return dev_err_probe(dev, ret, "Failed to get regulator\n");
1359 data = iio_priv(idev);
1361 data->cd = device_get_match_data(&i2c->dev);
1365 regmap = devm_regmap_init_i2c(i2c, data->cd->regmap_cfg);
1367 return dev_err_probe(dev, PTR_ERR(regmap),
1368 "Failed to initialize Regmap\n");
1371 ret = regmap_read(regmap, BU27008_REG_SYSTEM_CONTROL, ®);
1373 return dev_err_probe(dev, ret, "Failed to access sensor\n");
1375 part_id = FIELD_GET(BU27008_MASK_PART_ID, reg);
1377 if (part_id != data->cd->part_id)
1378 dev_warn(dev, "unknown device 0x%x\n", part_id);
1380 ret = devm_iio_init_iio_gts(dev, data->cd->scale1x, 0, data->cd->gains,
1381 data->cd->num_gains, data->cd->itimes,
1382 data->cd->num_itimes, &data->gts);
1386 ret = devm_iio_init_iio_gts(dev, data->cd->scale1x, 0, data->cd->gains_ir,
1387 data->cd->num_gains_ir, data->cd->itimes,
1388 data->cd->num_itimes, &data->gts_ir);
1392 mutex_init(&data->mutex);
1393 data->regmap = regmap;
1395 data->irq = i2c->irq;
1397 idev->channels = bu27008_channels;
1398 idev->num_channels = ARRAY_SIZE(bu27008_channels);
1399 idev->name = data->cd->name;
1400 idev->info = &bu27008_info;
1401 idev->modes = INDIO_DIRECT_MODE;
1402 idev->available_scan_masks = bu27008_scan_masks;
1404 ret = data->cd->chip_init(data);
1409 ret = bu27008_setup_trigger(data, idev);
1413 dev_info(dev, "No IRQ, buffered mode disabled\n");
1416 ret = devm_iio_device_register(dev, idev);
1418 return dev_err_probe(dev, ret,
1419 "Unable to register iio device\n");
1424 static const struct of_device_id bu27008_of_match[] = {
1425 { .compatible = "rohm,bu27008", .data = &bu27008_chip },
1426 { .compatible = "rohm,bu27010", .data = &bu27010_chip },
1429 MODULE_DEVICE_TABLE(of, bu27008_of_match);
1431 static struct i2c_driver bu27008_i2c_driver = {
1434 .of_match_table = bu27008_of_match,
1435 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1437 .probe = bu27008_probe,
1439 module_i2c_driver(bu27008_i2c_driver);
1441 MODULE_DESCRIPTION("ROHM BU27008 and BU27010 colour sensor driver");
1442 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1443 MODULE_LICENSE("GPL");
1444 MODULE_IMPORT_NS(IIO_GTS_HELPER);