1 // SPDX-License-Identifier: GPL-2.0-only
3 * BU27034 ROHM Ambient Light Sensor
5 * Copyright (c) 2023, ROHM Semiconductor.
6 * https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/light/bu27034nuc-e.pdf
9 #include <linux/bitfield.h>
10 #include <linux/bits.h>
11 #include <linux/device.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/property.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/units.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/iio-gts-helper.h>
22 #include <linux/iio/kfifo_buf.h>
24 #define BU27034_REG_SYSTEM_CONTROL 0x40
25 #define BU27034_MASK_SW_RESET BIT(7)
26 #define BU27034_MASK_PART_ID GENMASK(5, 0)
27 #define BU27034_ID 0x19
28 #define BU27034_REG_MODE_CONTROL1 0x41
29 #define BU27034_MASK_MEAS_MODE GENMASK(2, 0)
31 #define BU27034_REG_MODE_CONTROL2 0x42
32 #define BU27034_MASK_D01_GAIN GENMASK(7, 3)
33 #define BU27034_MASK_D2_GAIN_HI GENMASK(7, 6)
34 #define BU27034_MASK_D2_GAIN_LO GENMASK(2, 0)
36 #define BU27034_REG_MODE_CONTROL3 0x43
37 #define BU27034_REG_MODE_CONTROL4 0x44
38 #define BU27034_MASK_MEAS_EN BIT(0)
39 #define BU27034_MASK_VALID BIT(7)
40 #define BU27034_REG_DATA0_LO 0x50
41 #define BU27034_REG_DATA1_LO 0x52
42 #define BU27034_REG_DATA2_LO 0x54
43 #define BU27034_REG_DATA2_HI 0x55
44 #define BU27034_REG_MANUFACTURER_ID 0x92
45 #define BU27034_REG_MAX BU27034_REG_MANUFACTURER_ID
48 * The BU27034 does not have interrupt to trigger the data read when a
49 * measurement has finished. Hence we poll the VALID bit in a thread. We will
50 * try to wake the thread BU27034_MEAS_WAIT_PREMATURE_MS milliseconds before
51 * the expected sampling time to prevent the drifting.
53 * If we constantly wake up a bit too late we would eventually skip a sample.
54 * And because the sleep can't wake up _exactly_ at given time this would be
55 * inevitable even if the sensor clock would be perfectly phase-locked to CPU
56 * clock - which we can't say is the case.
58 * This is still fragile. No matter how big advance do we have, we will still
59 * risk of losing a sample because things can in a rainy-day scenario be
60 * delayed a lot. Yet, more we reserve the time for polling, more we also lose
61 * the performance by spending cycles polling the register. So, selecting this
62 * value is a balancing dance between severity of wasting CPU time and severity
65 * In most cases losing the samples is not _that_ crucial because light levels
66 * tend to change slowly.
68 * Other option that was pointed to me would be always sleeping 1/2 of the
69 * measurement time, checking the VALID bit and just sleeping again if the bit
70 * was not set. That should be pretty tolerant against missing samples due to
71 * the scheduling delays while also not wasting much of cycles for polling.
72 * Downside is that the time-stamps would be very inaccurate as the wake-up
73 * would not really be tied to the sensor toggling the valid bit. This would also
74 * result 'jumps' in the time-stamps when the delay drifted so that wake-up was
75 * performed during the consecutive wake-ups (Or, when sensor and CPU clocks
76 * were very different and scheduling the wake-ups was very close to given
77 * timeout - and when the time-outs were very close to the actual sensor
78 * sampling, Eg. once in a blue moon, two consecutive time-outs would occur
79 * without having a sample ready).
81 #define BU27034_MEAS_WAIT_PREMATURE_MS 5
82 #define BU27034_DATA_WAIT_TIME_US 1000
83 #define BU27034_TOTAL_DATA_WAIT_TIME_US (BU27034_MEAS_WAIT_PREMATURE_MS * 1000)
85 #define BU27034_RETRY_LIMIT 18
95 static const unsigned long bu27034_scan_masks[] = {
96 GENMASK(BU27034_CHAN_DATA2, BU27034_CHAN_ALS), 0
100 * Available scales with gain 1x - 4096x, timings 55, 100, 200, 400 mS
101 * Time impacts to gain: 1x, 2x, 4x, 8x.
103 * => Max total gain is HWGAIN * gain by integration time (8 * 4096) = 32768
105 * Using NANO precision for scale we must use scale 64x corresponding gain 1x
106 * to avoid precision loss. (32x would result scale 976 562.5(nanos).
108 #define BU27034_SCALE_1X 64
110 /* See the data sheet for the "Gain Setting" table */
111 #define BU27034_GSEL_1X 0x00 /* 00000 */
112 #define BU27034_GSEL_4X 0x08 /* 01000 */
113 #define BU27034_GSEL_16X 0x0a /* 01010 */
114 #define BU27034_GSEL_32X 0x0b /* 01011 */
115 #define BU27034_GSEL_64X 0x0c /* 01100 */
116 #define BU27034_GSEL_256X 0x18 /* 11000 */
117 #define BU27034_GSEL_512X 0x19 /* 11001 */
118 #define BU27034_GSEL_1024X 0x1a /* 11010 */
119 #define BU27034_GSEL_2048X 0x1b /* 11011 */
120 #define BU27034_GSEL_4096X 0x1c /* 11100 */
122 /* Available gain settings */
123 static const struct iio_gain_sel_pair bu27034_gains[] = {
124 GAIN_SCALE_GAIN(1, BU27034_GSEL_1X),
125 GAIN_SCALE_GAIN(4, BU27034_GSEL_4X),
126 GAIN_SCALE_GAIN(16, BU27034_GSEL_16X),
127 GAIN_SCALE_GAIN(32, BU27034_GSEL_32X),
128 GAIN_SCALE_GAIN(64, BU27034_GSEL_64X),
129 GAIN_SCALE_GAIN(256, BU27034_GSEL_256X),
130 GAIN_SCALE_GAIN(512, BU27034_GSEL_512X),
131 GAIN_SCALE_GAIN(1024, BU27034_GSEL_1024X),
132 GAIN_SCALE_GAIN(2048, BU27034_GSEL_2048X),
133 GAIN_SCALE_GAIN(4096, BU27034_GSEL_4096X),
137 * The IC has 5 modes for sampling time. 5 mS mode is exceptional as it limits
138 * the data collection to data0-channel only and cuts the supported range to
139 * 10 bit. It is not supported by the driver.
141 * "normal" modes are 55, 100, 200 and 400 mS modes - which do have direct
142 * multiplying impact to the register values (similar to gain).
144 * This means that if meas-mode is changed for example from 400 => 200,
145 * the scale is doubled. Eg, time impact to total gain is x1, x2, x4, x8.
147 #define BU27034_MEAS_MODE_100MS 0
148 #define BU27034_MEAS_MODE_55MS 1
149 #define BU27034_MEAS_MODE_200MS 2
150 #define BU27034_MEAS_MODE_400MS 4
152 static const struct iio_itime_sel_mul bu27034_itimes[] = {
153 GAIN_SCALE_ITIME_US(400000, BU27034_MEAS_MODE_400MS, 8),
154 GAIN_SCALE_ITIME_US(200000, BU27034_MEAS_MODE_200MS, 4),
155 GAIN_SCALE_ITIME_US(100000, BU27034_MEAS_MODE_100MS, 2),
156 GAIN_SCALE_ITIME_US(55000, BU27034_MEAS_MODE_55MS, 1),
159 #define BU27034_CHAN_DATA(_name, _ch2) \
161 .type = IIO_INTENSITY, \
162 .channel = BU27034_CHAN_##_name, \
163 .channel2 = (_ch2), \
164 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
165 BIT(IIO_CHAN_INFO_SCALE), \
166 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), \
167 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
168 .info_mask_shared_by_all_available = \
169 BIT(IIO_CHAN_INFO_INT_TIME), \
170 .address = BU27034_REG_##_name##_LO, \
171 .scan_index = BU27034_CHAN_##_name, \
176 .endianness = IIO_LE, \
181 static const struct iio_chan_spec bu27034_channels[] = {
184 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
185 BIT(IIO_CHAN_INFO_SCALE),
186 .channel = BU27034_CHAN_ALS,
187 .scan_index = BU27034_CHAN_ALS,
192 .endianness = IIO_CPU,
196 * The BU27034 DATA0 and DATA1 channels are both on the visible light
197 * area (mostly). The data0 sensitivity peaks at 500nm, DATA1 at 600nm.
198 * These wave lengths are pretty much on the border of colours making
199 * these a poor candidates for R/G/B standardization. Hence they're both
200 * marked as clear channels
202 BU27034_CHAN_DATA(DATA0, IIO_MOD_LIGHT_CLEAR),
203 BU27034_CHAN_DATA(DATA1, IIO_MOD_LIGHT_CLEAR),
204 BU27034_CHAN_DATA(DATA2, IIO_MOD_LIGHT_IR),
205 IIO_CHAN_SOFT_TIMESTAMP(4),
208 struct bu27034_data {
209 struct regmap *regmap;
212 * Protect gain and time during scale adjustment and data reading.
213 * Protect measurement enabling/disabling.
217 struct task_struct *task;
226 struct bu27034_result {
232 static const struct regmap_range bu27034_volatile_ranges[] = {
234 .range_min = BU27034_REG_SYSTEM_CONTROL,
235 .range_max = BU27034_REG_SYSTEM_CONTROL,
237 .range_min = BU27034_REG_MODE_CONTROL4,
238 .range_max = BU27034_REG_MODE_CONTROL4,
240 .range_min = BU27034_REG_DATA0_LO,
241 .range_max = BU27034_REG_DATA2_HI,
245 static const struct regmap_access_table bu27034_volatile_regs = {
246 .yes_ranges = &bu27034_volatile_ranges[0],
247 .n_yes_ranges = ARRAY_SIZE(bu27034_volatile_ranges),
250 static const struct regmap_range bu27034_read_only_ranges[] = {
252 .range_min = BU27034_REG_DATA0_LO,
253 .range_max = BU27034_REG_DATA2_HI,
255 .range_min = BU27034_REG_MANUFACTURER_ID,
256 .range_max = BU27034_REG_MANUFACTURER_ID,
260 static const struct regmap_access_table bu27034_ro_regs = {
261 .no_ranges = &bu27034_read_only_ranges[0],
262 .n_no_ranges = ARRAY_SIZE(bu27034_read_only_ranges),
265 static const struct regmap_config bu27034_regmap = {
268 .max_register = BU27034_REG_MAX,
269 .cache_type = REGCACHE_RBTREE,
270 .volatile_table = &bu27034_volatile_regs,
271 .wr_table = &bu27034_ro_regs,
274 struct bu27034_gain_check {
280 static int bu27034_get_gain_sel(struct bu27034_data *data, int chan)
285 case BU27034_CHAN_DATA0:
286 case BU27034_CHAN_DATA1:
289 [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2,
290 [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3,
292 ret = regmap_read(data->regmap, reg[chan], &val);
296 return FIELD_GET(BU27034_MASK_D01_GAIN, val);
298 case BU27034_CHAN_DATA2:
300 int d2_lo_bits = fls(BU27034_MASK_D2_GAIN_LO);
302 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL2, &val);
307 * The data2 channel gain is composed by 5 non continuous bits
308 * [7:6], [2:0]. Thus when we combine the 5-bit 'selector'
309 * from register value we must right shift the high bits by 3.
311 return FIELD_GET(BU27034_MASK_D2_GAIN_HI, val) << d2_lo_bits |
312 FIELD_GET(BU27034_MASK_D2_GAIN_LO, val);
319 static int bu27034_get_gain(struct bu27034_data *data, int chan, int *gain)
323 ret = bu27034_get_gain_sel(data, chan);
329 ret = iio_gts_find_gain_by_sel(&data->gts, sel);
331 dev_err(data->dev, "chan %u: unknown gain value 0x%x\n", chan,
342 static int bu27034_get_int_time(struct bu27034_data *data)
346 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel);
350 return iio_gts_find_int_time_by_sel(&data->gts,
351 sel & BU27034_MASK_MEAS_MODE);
354 static int _bu27034_get_scale(struct bu27034_data *data, int channel, int *val,
359 ret = bu27034_get_gain(data, channel, &gain);
363 ret = bu27034_get_int_time(data);
367 return iio_gts_get_scale(&data->gts, gain, ret, val, val2);
370 static int bu27034_get_scale(struct bu27034_data *data, int channel, int *val,
375 if (channel == BU27034_CHAN_ALS) {
378 return IIO_VAL_INT_PLUS_MICRO;
381 mutex_lock(&data->mutex);
382 ret = _bu27034_get_scale(data, channel, val, val2);
383 mutex_unlock(&data->mutex);
387 return IIO_VAL_INT_PLUS_NANO;
390 /* Caller should hold the lock to protect lux reading */
391 static int bu27034_write_gain_sel(struct bu27034_data *data, int chan, int sel)
393 static const int reg[] = {
394 [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2,
395 [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3,
399 if (chan != BU27034_CHAN_DATA0 && chan != BU27034_CHAN_DATA1)
402 val = FIELD_PREP(BU27034_MASK_D01_GAIN, sel);
404 mask = BU27034_MASK_D01_GAIN;
406 if (chan == BU27034_CHAN_DATA0) {
408 * We keep the same gain for channel 2 as we set for channel 0
409 * We can't allow them to be individually controlled because
410 * setting one will impact also the other. Also, if we don't
411 * always update both gains we may result unsupported bit
414 * This is not nice but this is yet another place where the
415 * user space must be prepared to surprizes. Namely, see chan 2
416 * gain changed when chan 0 gain is changed.
418 * This is not fatal for most users though. I don't expect the
419 * channel 2 to be used in any generic cases - the intensity
420 * values provided by the sensor for IR area are not openly
421 * documented. Also, channel 2 is not used for visible light.
423 * So, if there is application which is written to utilize the
424 * channel 2 - then it is probably specifically targeted to this
425 * sensor and knows how to utilize those values. It is safe to
426 * hope such user can also cope with the gain changes.
428 mask |= BU27034_MASK_D2_GAIN_LO;
431 * The D2 gain bits are directly the lowest bits of selector.
432 * Just do add those bits to the value
434 val |= sel & BU27034_MASK_D2_GAIN_LO;
437 return regmap_update_bits(data->regmap, reg[chan], mask, val);
440 static int bu27034_set_gain(struct bu27034_data *data, int chan, int gain)
445 * We don't allow setting channel 2 gain as it messes up the
446 * gain for channel 0 - which shares the high bits
448 if (chan != BU27034_CHAN_DATA0 && chan != BU27034_CHAN_DATA1)
451 ret = iio_gts_find_sel_by_gain(&data->gts, gain);
455 return bu27034_write_gain_sel(data, chan, ret);
458 /* Caller should hold the lock to protect data->int_time */
459 static int bu27034_set_int_time(struct bu27034_data *data, int time)
463 ret = iio_gts_find_sel_by_int_time(&data->gts, time);
467 return regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1,
468 BU27034_MASK_MEAS_MODE, ret);
472 * We try to change the time in such way that the scale is maintained for
473 * given channels by adjusting gain so that it compensates the time change.
475 static int bu27034_try_set_int_time(struct bu27034_data *data, int time_us)
477 struct bu27034_gain_check gains[] = {
478 { .chan = BU27034_CHAN_DATA0 },
479 { .chan = BU27034_CHAN_DATA1 },
481 int numg = ARRAY_SIZE(gains);
482 int ret, int_time_old, i;
484 mutex_lock(&data->mutex);
485 ret = bu27034_get_int_time(data);
491 if (!iio_gts_valid_time(&data->gts, time_us)) {
492 dev_err(data->dev, "Unsupported integration time %u\n",
499 if (time_us == int_time_old) {
504 for (i = 0; i < numg; i++) {
505 ret = bu27034_get_gain(data, gains[i].chan, &gains[i].old_gain);
509 ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts,
511 int_time_old, time_us,
517 _bu27034_get_scale(data, gains[i].chan, &scale1, &scale2);
519 "chan %u, can't support time %u with scale %u %u\n",
520 gains[i].chan, time_us, scale1, scale2);
522 if (gains[i].new_gain < 0)
526 * If caller requests for integration time change and we
527 * can't support the scale - then the caller should be
528 * prepared to 'pick up the pieces and deal with the
529 * fact that the scale changed'.
531 ret = iio_find_closest_gain_low(&data->gts,
532 gains[i].new_gain, &ok);
536 "optimal gain out of range for chan %u\n",
541 "Total gain increase. Risk of saturation");
542 ret = iio_gts_get_min_gain(&data->gts);
546 dev_dbg(data->dev, "chan %u scale changed\n",
548 gains[i].new_gain = ret;
549 dev_dbg(data->dev, "chan %u new gain %u\n",
550 gains[i].chan, gains[i].new_gain);
554 for (i = 0; i < numg; i++) {
555 ret = bu27034_set_gain(data, gains[i].chan, gains[i].new_gain);
560 ret = bu27034_set_int_time(data, time_us);
563 mutex_unlock(&data->mutex);
568 static int bu27034_set_scale(struct bu27034_data *data, int chan,
571 int ret, time_sel, gain_sel, i;
574 if (chan == BU27034_CHAN_DATA2)
577 if (chan == BU27034_CHAN_ALS) {
578 if (val == 0 && val2 == 1000000)
584 mutex_lock(&data->mutex);
585 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &time_sel);
589 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel,
590 val, val2, &gain_sel);
593 * Could not support scale with given time. Need to change time.
594 * We still want to maintain the scale for all channels
596 struct bu27034_gain_check gain;
600 * Populate information for the other channel which should also
601 * maintain the scale. (Due to the HW limitations the chan2
602 * gets the same gain as chan0, so we only need to explicitly
603 * set the chan 0 and 1).
605 if (chan == BU27034_CHAN_DATA0)
606 gain.chan = BU27034_CHAN_DATA1;
607 else if (chan == BU27034_CHAN_DATA1)
608 gain.chan = BU27034_CHAN_DATA0;
610 ret = bu27034_get_gain(data, gain.chan, &gain.old_gain);
615 * Iterate through all the times to see if we find one which
616 * can support requested scale for requested channel, while
617 * maintaining the scale for other channels
619 for (i = 0; i < data->gts.num_itime; i++) {
620 new_time_sel = data->gts.itime_table[i].sel;
622 if (new_time_sel == time_sel)
625 /* Can we provide requested scale with this time? */
626 ret = iio_gts_find_gain_sel_for_scale_using_time(
627 &data->gts, new_time_sel, val, val2,
632 /* Can the other channel(s) maintain scale? */
633 ret = iio_gts_find_new_gain_sel_by_old_gain_time(
634 &data->gts, gain.old_gain, time_sel,
635 new_time_sel, &gain.new_gain);
637 /* Yes - we found suitable time */
644 "Can't set scale maintaining other channels\n");
650 ret = bu27034_set_gain(data, gain.chan, gain.new_gain);
654 ret = regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1,
655 BU27034_MASK_MEAS_MODE, new_time_sel);
660 ret = bu27034_write_gain_sel(data, chan, gain_sel);
662 mutex_unlock(&data->mutex);
668 * for (D1/D0 < 0.87):
669 * lx = 0.004521097 * D1 - 0.002663996 * D0 +
670 * 0.00012213 * D1 * D1 / D0
672 * => 115.7400832 * ch1 / gain1 / mt -
673 * 68.1982976 * ch0 / gain0 / mt +
674 * 0.00012213 * 25600 * (ch1 / gain1 / mt) * 25600 *
675 * (ch1 /gain1 / mt) / (25600 * ch0 / gain0 / mt)
677 * A = 0.00012213 * 25600 * (ch1 /gain1 / mt) * 25600 *
678 * (ch1 /gain1 / mt) / (25600 * ch0 / gain0 / mt)
679 * => 0.00012213 * 25600 * (ch1 /gain1 / mt) *
680 * (ch1 /gain1 / mt) / (ch0 / gain0 / mt)
681 * => 0.00012213 * 25600 * (ch1 / gain1) * (ch1 /gain1 / mt) /
683 * => 0.00012213 * 25600 * (ch1 / gain1) * (ch1 /gain1 / mt) *
685 * => 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / mt /ch0
687 * lx = (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) /
689 * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) /
690 * mt + 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / mt /
693 * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0 +
694 * 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0) /
697 * For (0.87 <= D1/D0 < 1.00)
698 * lx = (0.001331* D0 + 0.0000354 * D1) * ((D1/D0 – 0.87) * (0.385) + 1)
699 * => (0.001331 * 256 * 100 * ch0 / gain0 / mt + 0.0000354 * 256 *
700 * 100 * ch1 / gain1 / mt) * ((D1/D0 - 0.87) * (0.385) + 1)
701 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
702 * ((D1/D0 - 0.87) * (0.385) + 1)
703 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
704 * (0.385 * D1/D0 - 0.66505)
705 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
706 * (0.385 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) - 0.66505)
707 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
708 * (9856 * ch1 / gain1 / mt / (25600 * ch0 / gain0 / mt) + 0.66505)
709 * => 13.118336 * ch1 / (gain1 * mt)
710 * + 22.66064768 * ch0 / (gain0 * mt)
711 * + 8931.90144 * ch1 * ch1 * gain0 /
712 * (25600 * ch0 * gain1 * gain1 * mt)
713 * + 0.602694912 * ch1 / (gain1 * mt)
715 * => [0.3489024 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1)
716 * + 22.66064768 * ch0 / gain0
717 * + 13.721030912 * ch1 / gain1
720 * For (D1/D0 >= 1.00)
722 * lx = (0.001331* D0 + 0.0000354 * D1) * ((D1/D0 – 2.0) * (-0.05) + 1)
723 * => (0.001331* D0 + 0.0000354 * D1) * (-0.05D1/D0 + 1.1)
724 * => (0.001331 * 256 * 100 * ch0 / gain0 / mt + 0.0000354 * 256 *
725 * 100 * ch1 / gain1 / mt) * (-0.05D1/D0 + 1.1)
726 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
727 * (-0.05 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) + 1.1)
728 * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) *
729 * (-1280 * ch1 / (gain1 * mt * 25600 * ch0 / gain0 / mt) + 1.1)
730 * => (34.0736 * ch0 * -1280 * ch1 * gain0 * mt /( gain0 * mt * gain1 * mt * 25600 * ch0)
731 * + 34.0736 * 1.1 * ch0 / (gain0 * mt)
732 * + 0.90624 * ch1 * -1280 * ch1 *gain0 * mt / (gain1 * mt *gain1 * mt * 25600 * ch0)
733 * + 1.1 * 0.90624 * ch1 / (gain1 * mt)
734 * => -43614.208 * ch1 / (gain1 * mt * 25600)
735 * + 37.48096 ch0 / (gain0 * mt)
736 * - 1159.9872 * ch1 * ch1 * gain0 / (gain1 * gain1 * mt * 25600 * ch0)
737 * + 0.996864 ch1 / (gain1 * mt)
739 * - 0.045312 * ch1 * ch1 * gain0 / (gain1 * gain1 * ch0)
740 * - 0.706816 * ch1 / gain1
741 * + 37.48096 ch0 /gain0
745 * So, the first case (D1/D0 < 0.87) can be computed to a form:
747 * lx = (3.126528 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
748 * 115.7400832 * ch1 / gain1 +
749 * -68.1982976 * ch0 / gain0
752 * Second case (0.87 <= D1/D0 < 1.00) goes to form:
754 * => [0.3489024 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
755 * 13.721030912 * ch1 / gain1 +
756 * 22.66064768 * ch0 / gain0
759 * Third case (D1/D0 >= 1.00) goes to form:
760 * => [-0.045312 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
761 * -0.706816 * ch1 / gain1 +
762 * 37.48096 ch0 /(gain0
765 * This can be unified to format:
767 * A * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
788 struct bu27034_lx_coeff {
792 /* Indicate which of the coefficients above are negative */
796 static inline u64 gain_mul_div_helper(u64 val, unsigned int gain,
800 * Max gain for a channel is 4096. The max u64 (0xffffffffffffffffULL)
801 * divided by 4096 is 0xFFFFFFFFFFFFF (GENMASK_ULL(51, 0)) (floored).
802 * Thus, the 0xFFFFFFFFFFFFF is the largest value we can safely multiply
803 * with the gain, no matter what gain is set.
805 * So, multiplication with max gain may overflow if val is greater than
806 * 0xFFFFFFFFFFFFF (52 bits set)..
808 * If this is the case we divide first.
810 if (val < GENMASK_ULL(51, 0)) {
821 static u64 bu27034_fixp_calc_t1_64bit(unsigned int coeff, unsigned int ch0,
822 unsigned int ch1, unsigned int gain0,
828 helper64 = (u64)coeff * (u64)ch1 * (u64)ch1;
830 helper = gain1 * gain1;
832 do_div(helper64, helper);
834 return gain_mul_div_helper(helper64, gain0, ch0);
837 do_div(helper64, ch0);
839 return gain_mul_div_helper(helper64, gain0, helper);
843 static u64 bu27034_fixp_calc_t1(unsigned int coeff, unsigned int ch0,
844 unsigned int ch1, unsigned int gain0,
847 unsigned int helper, tmp;
850 * Here we could overflow even the 64bit value. Hence we
851 * multiply with gain0 only after the divisions - even though
852 * it may result loss of accuracy
854 helper = coeff * ch1 * ch1;
855 tmp = helper * gain0;
859 if (check_mul_overflow(helper, coeff, &helper))
860 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1);
862 if (check_mul_overflow(helper, gain0, &tmp))
863 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1);
865 return tmp / (gain1 * gain1) / ch0;
869 static u64 bu27034_fixp_calc_t23(unsigned int coeff, unsigned int ch,
875 if (!check_mul_overflow(coeff, ch, &helper))
876 return helper / gain;
878 helper64 = (u64)coeff * (u64)ch;
879 do_div(helper64, gain);
884 static int bu27034_fixp_calc_lx(unsigned int ch0, unsigned int ch1,
885 unsigned int gain0, unsigned int gain1,
886 unsigned int meastime, int coeff_idx)
888 static const struct bu27034_lx_coeff coeff[] = {
890 .A = 31265280, /* 3.126528 */
891 .B = 1157400832, /*115.7400832 */
892 .C = 681982976, /* -68.1982976 */
893 .is_neg = {false, false, true},
895 .A = 3489024, /* 0.3489024 */
896 .B = 137210309, /* 13.721030912 */
897 .C = 226606476, /* 22.66064768 */
898 /* All terms positive */
900 .A = 453120, /* -0.045312 */
901 .B = 7068160, /* -0.706816 */
902 .C = 374809600, /* 37.48096 */
903 .is_neg = {true, true, false},
906 const struct bu27034_lx_coeff *c = &coeff[coeff_idx];
907 u64 res = 0, terms[3];
910 if (coeff_idx >= ARRAY_SIZE(coeff))
913 terms[0] = bu27034_fixp_calc_t1(c->A, ch0, ch1, gain0, gain1);
914 terms[1] = bu27034_fixp_calc_t23(c->B, ch1, gain1);
915 terms[2] = bu27034_fixp_calc_t23(c->C, ch0, gain0);
917 /* First, add positive terms */
918 for (i = 0; i < 3; i++)
922 /* No positive term => zero lux */
926 /* Then, subtract negative terms (if any) */
927 for (i = 0; i < 3; i++)
930 * If the negative term is greater than positive - then
931 * the darkness has taken over and we are all doomed! Eh,
932 * I mean, then we can just return 0 lx and go out
941 do_div(res, meastime);
946 static bool bu27034_has_valid_sample(struct bu27034_data *data)
950 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL4, &val);
952 dev_err(data->dev, "Read failed %d\n", ret);
957 return val & BU27034_MASK_VALID;
961 * Reading the register where VALID bit is clears this bit. (So does changing
962 * any gain / integration time configuration registers) The bit gets
963 * set when we have acquired new data. We use this bit to indicate data
966 static void bu27034_invalidate_read_data(struct bu27034_data *data)
968 bu27034_has_valid_sample(data);
971 static int bu27034_read_result(struct bu27034_data *data, int chan, int *res)
974 [BU27034_CHAN_DATA0] = BU27034_REG_DATA0_LO,
975 [BU27034_CHAN_DATA1] = BU27034_REG_DATA1_LO,
976 [BU27034_CHAN_DATA2] = BU27034_REG_DATA2_LO,
981 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4,
982 valid, (valid & BU27034_MASK_VALID),
983 BU27034_DATA_WAIT_TIME_US, 0);
987 ret = regmap_bulk_read(data->regmap, reg[chan], &val, sizeof(val));
991 *res = le16_to_cpu(val);
996 static int bu27034_get_result_unlocked(struct bu27034_data *data, __le16 *res,
999 int ret = 0, retry_cnt = 0;
1002 /* Get new value from sensor if data is ready */
1003 if (bu27034_has_valid_sample(data)) {
1004 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO,
1009 bu27034_invalidate_read_data(data);
1011 /* No new data in sensor. Wait and retry */
1014 if (retry_cnt > BU27034_RETRY_LIMIT) {
1015 dev_err(data->dev, "No data from sensor\n");
1028 static int bu27034_meas_set(struct bu27034_data *data, bool en)
1031 return regmap_set_bits(data->regmap, BU27034_REG_MODE_CONTROL4,
1032 BU27034_MASK_MEAS_EN);
1034 return regmap_clear_bits(data->regmap, BU27034_REG_MODE_CONTROL4,
1035 BU27034_MASK_MEAS_EN);
1038 static int bu27034_get_single_result(struct bu27034_data *data, int chan,
1043 if (chan < BU27034_CHAN_DATA0 || chan > BU27034_CHAN_DATA2)
1046 ret = bu27034_meas_set(data, true);
1050 ret = bu27034_get_int_time(data);
1056 return bu27034_read_result(data, chan, val);
1060 * The formula given by vendor for computing luxes out of data0 and data1
1061 * (in open air) is as follows:
1064 * D0 = data0/ch0_gain/meas_time_ms * 25600
1065 * D1 = data1/ch1_gain/meas_time_ms * 25600
1069 * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 0.87) * 3.45 + 1)
1070 * else if (D1/D0 < 1)
1071 * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 0.87) * 0.385 + 1)
1073 * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 2) * -0.05 + 1)
1075 * We use it here. Users who have for example some colored lens
1076 * need to modify the calculation but I hope this gives a starting point for
1077 * those working with such devices.
1080 static int bu27034_calc_mlux(struct bu27034_data *data, __le16 *res, int *val)
1082 unsigned int gain0, gain1, meastime;
1083 unsigned int d1_d0_ratio_scaled;
1089 * We return 0 lux if calculation fails. This should be reasonably
1090 * easy to spot from the buffers especially if raw-data channels show
1095 ch0 = max_t(u16, 1, le16_to_cpu(res[0]));
1096 ch1 = max_t(u16, 1, le16_to_cpu(res[1]));
1098 ret = bu27034_get_gain(data, BU27034_CHAN_DATA0, &gain0);
1102 ret = bu27034_get_gain(data, BU27034_CHAN_DATA1, &gain1);
1106 ret = bu27034_get_int_time(data);
1112 d1_d0_ratio_scaled = (unsigned int)ch1 * (unsigned int)gain0 * 100;
1113 helper64 = (u64)ch1 * (u64)gain0 * 100LLU;
1115 if (helper64 != d1_d0_ratio_scaled) {
1116 unsigned int div = (unsigned int)ch0 * gain1;
1118 do_div(helper64, div);
1119 d1_d0_ratio_scaled = helper64;
1121 d1_d0_ratio_scaled /= ch0 * gain1;
1124 if (d1_d0_ratio_scaled < 87)
1125 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 0);
1126 else if (d1_d0_ratio_scaled < 100)
1127 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 1);
1129 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 2);
1140 static int bu27034_get_mlux(struct bu27034_data *data, int chan, int *val)
1145 ret = bu27034_meas_set(data, true);
1149 ret = bu27034_get_result_unlocked(data, &res[0], sizeof(res));
1153 ret = bu27034_calc_mlux(data, res, val);
1157 ret = bu27034_meas_set(data, false);
1159 dev_err(data->dev, "failed to disable measurement\n");
1164 static int bu27034_read_raw(struct iio_dev *idev,
1165 struct iio_chan_spec const *chan,
1166 int *val, int *val2, long mask)
1168 struct bu27034_data *data = iio_priv(idev);
1172 case IIO_CHAN_INFO_INT_TIME:
1174 *val2 = bu27034_get_int_time(data);
1178 return IIO_VAL_INT_PLUS_MICRO;
1180 case IIO_CHAN_INFO_SCALE:
1181 return bu27034_get_scale(data, chan->channel, val, val2);
1183 case IIO_CHAN_INFO_RAW:
1185 int (*result_get)(struct bu27034_data *data, int chan, int *val);
1187 if (chan->type == IIO_INTENSITY)
1188 result_get = bu27034_get_single_result;
1189 else if (chan->type == IIO_LIGHT)
1190 result_get = bu27034_get_mlux;
1194 /* Don't mess with measurement enabling while buffering */
1195 ret = iio_device_claim_direct_mode(idev);
1199 mutex_lock(&data->mutex);
1201 * Reading one channel at a time is inefficient but we
1202 * don't care here. Buffered version should be used if
1203 * performance is an issue.
1205 ret = result_get(data, chan->channel, val);
1207 mutex_unlock(&data->mutex);
1208 iio_device_release_direct_mode(idev);
1220 static int bu27034_write_raw_get_fmt(struct iio_dev *indio_dev,
1221 struct iio_chan_spec const *chan,
1226 case IIO_CHAN_INFO_SCALE:
1227 return IIO_VAL_INT_PLUS_NANO;
1228 case IIO_CHAN_INFO_INT_TIME:
1229 return IIO_VAL_INT_PLUS_MICRO;
1235 static int bu27034_write_raw(struct iio_dev *idev,
1236 struct iio_chan_spec const *chan,
1237 int val, int val2, long mask)
1239 struct bu27034_data *data = iio_priv(idev);
1242 ret = iio_device_claim_direct_mode(idev);
1247 case IIO_CHAN_INFO_SCALE:
1248 ret = bu27034_set_scale(data, chan->channel, val, val2);
1250 case IIO_CHAN_INFO_INT_TIME:
1252 ret = bu27034_try_set_int_time(data, val2);
1261 iio_device_release_direct_mode(idev);
1266 static int bu27034_read_avail(struct iio_dev *idev,
1267 struct iio_chan_spec const *chan, const int **vals,
1268 int *type, int *length, long mask)
1270 struct bu27034_data *data = iio_priv(idev);
1273 case IIO_CHAN_INFO_INT_TIME:
1274 return iio_gts_avail_times(&data->gts, vals, type, length);
1275 case IIO_CHAN_INFO_SCALE:
1276 return iio_gts_all_avail_scales(&data->gts, vals, type, length);
1282 static const struct iio_info bu27034_info = {
1283 .read_raw = &bu27034_read_raw,
1284 .write_raw = &bu27034_write_raw,
1285 .write_raw_get_fmt = &bu27034_write_raw_get_fmt,
1286 .read_avail = &bu27034_read_avail,
1289 static int bu27034_chip_init(struct bu27034_data *data)
1294 ret = regmap_write_bits(data->regmap, BU27034_REG_SYSTEM_CONTROL,
1295 BU27034_MASK_SW_RESET, BU27034_MASK_SW_RESET);
1297 return dev_err_probe(data->dev, ret, "Sensor reset failed\n");
1301 ret = regmap_reinit_cache(data->regmap, &bu27034_regmap);
1303 dev_err(data->dev, "Failed to reinit reg cache\n");
1308 * Read integration time here to ensure it is in regmap cache. We do
1309 * this to speed-up the int-time acquisition in the start of the buffer
1310 * handling thread where longer delays could make it more likely we end
1311 * up skipping a sample, and where the longer delays make timestamps
1314 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel);
1316 dev_err(data->dev, "reading integration time failed\n");
1321 static int bu27034_wait_for_data(struct bu27034_data *data)
1325 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4,
1326 val, val & BU27034_MASK_VALID,
1327 BU27034_DATA_WAIT_TIME_US,
1328 BU27034_TOTAL_DATA_WAIT_TIME_US);
1330 dev_err(data->dev, "data polling %s\n",
1331 !(val & BU27034_MASK_VALID) ? "timeout" : "fail");
1336 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO,
1337 &data->scan.channels[0],
1338 sizeof(data->scan.channels));
1342 bu27034_invalidate_read_data(data);
1347 static int bu27034_buffer_thread(void *arg)
1349 struct iio_dev *idev = arg;
1350 struct bu27034_data *data;
1353 data = iio_priv(idev);
1355 wait_ms = bu27034_get_int_time(data);
1358 wait_ms -= BU27034_MEAS_WAIT_PREMATURE_MS;
1360 while (!kthread_should_stop()) {
1365 ret = bu27034_wait_for_data(data);
1369 tstamp = iio_get_time_ns(idev);
1371 if (test_bit(BU27034_CHAN_ALS, idev->active_scan_mask)) {
1374 ret = bu27034_calc_mlux(data, &data->scan.channels[0],
1377 dev_err(data->dev, "failed to calculate lux\n");
1380 * The maximum Milli lux value we get with gain 1x time
1381 * 55mS data ch0 = 0xffff ch1 = 0xffff fits in 26 bits
1382 * so there should be no problem returning int from
1383 * computations and casting it to u32
1385 data->scan.mlux = (u32)mlux;
1387 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp);
1393 static int bu27034_buffer_enable(struct iio_dev *idev)
1395 struct bu27034_data *data = iio_priv(idev);
1396 struct task_struct *task;
1399 mutex_lock(&data->mutex);
1400 ret = bu27034_meas_set(data, true);
1404 task = kthread_run(bu27034_buffer_thread, idev,
1405 "bu27034-buffering-%u",
1406 iio_device_id(idev));
1408 ret = PTR_ERR(task);
1415 mutex_unlock(&data->mutex);
1420 static int bu27034_buffer_disable(struct iio_dev *idev)
1422 struct bu27034_data *data = iio_priv(idev);
1425 mutex_lock(&data->mutex);
1427 kthread_stop(data->task);
1431 ret = bu27034_meas_set(data, false);
1432 mutex_unlock(&data->mutex);
1437 static const struct iio_buffer_setup_ops bu27034_buffer_ops = {
1438 .postenable = &bu27034_buffer_enable,
1439 .predisable = &bu27034_buffer_disable,
1442 static int bu27034_probe(struct i2c_client *i2c)
1444 struct device *dev = &i2c->dev;
1445 struct bu27034_data *data;
1446 struct regmap *regmap;
1447 struct iio_dev *idev;
1448 unsigned int part_id, reg;
1451 regmap = devm_regmap_init_i2c(i2c, &bu27034_regmap);
1453 return dev_err_probe(dev, PTR_ERR(regmap),
1454 "Failed to initialize Regmap\n");
1456 idev = devm_iio_device_alloc(dev, sizeof(*data));
1460 ret = devm_regulator_get_enable(dev, "vdd");
1462 return dev_err_probe(dev, ret, "Failed to get regulator\n");
1464 data = iio_priv(idev);
1466 ret = regmap_read(regmap, BU27034_REG_SYSTEM_CONTROL, ®);
1468 return dev_err_probe(dev, ret, "Failed to access sensor\n");
1470 part_id = FIELD_GET(BU27034_MASK_PART_ID, reg);
1472 if (part_id != BU27034_ID)
1473 dev_warn(dev, "unknown device 0x%x\n", part_id);
1475 ret = devm_iio_init_iio_gts(dev, BU27034_SCALE_1X, 0, bu27034_gains,
1476 ARRAY_SIZE(bu27034_gains), bu27034_itimes,
1477 ARRAY_SIZE(bu27034_itimes), &data->gts);
1481 mutex_init(&data->mutex);
1482 data->regmap = regmap;
1485 idev->channels = bu27034_channels;
1486 idev->num_channels = ARRAY_SIZE(bu27034_channels);
1487 idev->name = "bu27034";
1488 idev->info = &bu27034_info;
1490 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
1491 idev->available_scan_masks = bu27034_scan_masks;
1493 ret = bu27034_chip_init(data);
1497 ret = devm_iio_kfifo_buffer_setup(dev, idev, &bu27034_buffer_ops);
1499 return dev_err_probe(dev, ret, "buffer setup failed\n");
1501 ret = devm_iio_device_register(dev, idev);
1503 return dev_err_probe(dev, ret,
1504 "Unable to register iio device\n");
1509 static const struct of_device_id bu27034_of_match[] = {
1510 { .compatible = "rohm,bu27034" },
1513 MODULE_DEVICE_TABLE(of, bu27034_of_match);
1515 static struct i2c_driver bu27034_i2c_driver = {
1517 .name = "bu27034-als",
1518 .of_match_table = bu27034_of_match,
1519 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1521 .probe = bu27034_probe,
1523 module_i2c_driver(bu27034_i2c_driver);
1525 MODULE_LICENSE("GPL");
1526 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1527 MODULE_DESCRIPTION("ROHM BU27034 ambient light sensor driver");
1528 MODULE_IMPORT_NS(IIO_GTS_HELPER);