1 // SPDX-License-Identifier: GPL-2.0-only
3 * BU27034ANUC ROHM Ambient Light Sensor
5 * Copyright (c) 2023, ROHM Semiconductor.
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/device.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/property.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/units.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/iio-gts-helper.h>
21 #include <linux/iio/kfifo_buf.h>
23 #define BU27034_REG_SYSTEM_CONTROL 0x40
24 #define BU27034_MASK_SW_RESET BIT(7)
25 #define BU27034_MASK_PART_ID GENMASK(5, 0)
26 #define BU27034_ID 0x19
27 #define BU27034_REG_MODE_CONTROL1 0x41
28 #define BU27034_MASK_MEAS_MODE GENMASK(2, 0)
30 #define BU27034_REG_MODE_CONTROL2 0x42
31 #define BU27034_MASK_D01_GAIN GENMASK(7, 3)
33 #define BU27034_REG_MODE_CONTROL3 0x43
34 #define BU27034_REG_MODE_CONTROL4 0x44
35 #define BU27034_MASK_MEAS_EN BIT(0)
36 #define BU27034_MASK_VALID BIT(7)
37 #define BU27034_NUM_HW_DATA_CHANS 2
38 #define BU27034_REG_DATA0_LO 0x50
39 #define BU27034_REG_DATA1_LO 0x52
40 #define BU27034_REG_DATA1_HI 0x53
41 #define BU27034_REG_MANUFACTURER_ID 0x92
42 #define BU27034_REG_MAX BU27034_REG_MANUFACTURER_ID
45 * The BU27034 does not have interrupt to trigger the data read when a
46 * measurement has finished. Hence we poll the VALID bit in a thread. We will
47 * try to wake the thread BU27034_MEAS_WAIT_PREMATURE_MS milliseconds before
48 * the expected sampling time to prevent the drifting.
50 * If we constantly wake up a bit too late we would eventually skip a sample.
51 * And because the sleep can't wake up _exactly_ at given time this would be
52 * inevitable even if the sensor clock would be perfectly phase-locked to CPU
53 * clock - which we can't say is the case.
55 * This is still fragile. No matter how big advance do we have, we will still
56 * risk of losing a sample because things can in a rainy-day scenario be
57 * delayed a lot. Yet, more we reserve the time for polling, more we also lose
58 * the performance by spending cycles polling the register. So, selecting this
59 * value is a balancing dance between severity of wasting CPU time and severity
62 * In most cases losing the samples is not _that_ crucial because light levels
63 * tend to change slowly.
65 * Other option that was pointed to me would be always sleeping 1/2 of the
66 * measurement time, checking the VALID bit and just sleeping again if the bit
67 * was not set. That should be pretty tolerant against missing samples due to
68 * the scheduling delays while also not wasting much of cycles for polling.
69 * Downside is that the time-stamps would be very inaccurate as the wake-up
70 * would not really be tied to the sensor toggling the valid bit. This would also
71 * result 'jumps' in the time-stamps when the delay drifted so that wake-up was
72 * performed during the consecutive wake-ups (Or, when sensor and CPU clocks
73 * were very different and scheduling the wake-ups was very close to given
74 * timeout - and when the time-outs were very close to the actual sensor
75 * sampling, Eg. once in a blue moon, two consecutive time-outs would occur
76 * without having a sample ready).
78 #define BU27034_MEAS_WAIT_PREMATURE_MS 5
79 #define BU27034_DATA_WAIT_TIME_US 1000
80 #define BU27034_TOTAL_DATA_WAIT_TIME_US (BU27034_MEAS_WAIT_PREMATURE_MS * 1000)
82 #define BU27034_RETRY_LIMIT 18
91 static const unsigned long bu27034_scan_masks
[] = {
92 GENMASK(BU27034_CHAN_DATA1
, BU27034_CHAN_DATA0
),
93 GENMASK(BU27034_CHAN_DATA1
, BU27034_CHAN_ALS
), 0
97 * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS
98 * Time impacts to gain: 1x, 2x, 4x, 8x.
100 * => Max total gain is HWGAIN * gain by integration time (8 * 1024) = 8192
101 * if 1x gain is scale 1, scale for 2x gain is 0.5, 4x => 0.25,
102 * ... 8192x => 0.0001220703125 => 122070.3125 nanos
104 * Using NANO precision for scale, we must use scale 16x corresponding gain 1x
105 * to avoid precision loss. (8x would result scale 976 562.5(nanos).
107 #define BU27034_SCALE_1X 16
109 /* See the data sheet for the "Gain Setting" table */
110 #define BU27034_GSEL_1X 0x00 /* 00000 */
111 #define BU27034_GSEL_4X 0x08 /* 01000 */
112 #define BU27034_GSEL_32X 0x0b /* 01011 */
113 #define BU27034_GSEL_256X 0x18 /* 11000 */
114 #define BU27034_GSEL_512X 0x19 /* 11001 */
115 #define BU27034_GSEL_1024X 0x1a /* 11010 */
117 /* Available gain settings */
118 static const struct iio_gain_sel_pair bu27034_gains
[] = {
119 GAIN_SCALE_GAIN(1, BU27034_GSEL_1X
),
120 GAIN_SCALE_GAIN(4, BU27034_GSEL_4X
),
121 GAIN_SCALE_GAIN(32, BU27034_GSEL_32X
),
122 GAIN_SCALE_GAIN(256, BU27034_GSEL_256X
),
123 GAIN_SCALE_GAIN(512, BU27034_GSEL_512X
),
124 GAIN_SCALE_GAIN(1024, BU27034_GSEL_1024X
),
128 * Measurement modes are 55, 100, 200 and 400 mS modes - which do have direct
129 * multiplying impact to the data register values (similar to gain).
131 * This means that if meas-mode is changed for example from 400 => 200,
132 * the scale is doubled. Eg, time impact to total gain is x1, x2, x4, x8.
134 #define BU27034_MEAS_MODE_100MS 0
135 #define BU27034_MEAS_MODE_55MS 1
136 #define BU27034_MEAS_MODE_200MS 2
137 #define BU27034_MEAS_MODE_400MS 4
139 static const struct iio_itime_sel_mul bu27034_itimes
[] = {
140 GAIN_SCALE_ITIME_US(400000, BU27034_MEAS_MODE_400MS
, 8),
141 GAIN_SCALE_ITIME_US(200000, BU27034_MEAS_MODE_200MS
, 4),
142 GAIN_SCALE_ITIME_US(100000, BU27034_MEAS_MODE_100MS
, 2),
143 GAIN_SCALE_ITIME_US(55000, BU27034_MEAS_MODE_55MS
, 1),
146 #define BU27034_CHAN_DATA(_name) \
148 .type = IIO_INTENSITY, \
149 .channel = BU27034_CHAN_##_name, \
150 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
151 BIT(IIO_CHAN_INFO_SCALE) | \
152 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
153 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), \
154 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
155 .info_mask_shared_by_all_available = \
156 BIT(IIO_CHAN_INFO_INT_TIME), \
157 .address = BU27034_REG_##_name##_LO, \
158 .scan_index = BU27034_CHAN_##_name, \
163 .endianness = IIO_LE, \
168 static const struct iio_chan_spec bu27034_channels
[] = {
171 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
172 BIT(IIO_CHAN_INFO_SCALE
),
173 .channel
= BU27034_CHAN_ALS
,
174 .scan_index
= BU27034_CHAN_ALS
,
179 .endianness
= IIO_CPU
,
183 * The BU27034 DATA0 and DATA1 channels are both on the visible light
184 * area (mostly). The data0 sensitivity peaks at 500nm, DATA1 at 600nm.
185 * These wave lengths are cyan(ish) and orange(ish), making these
186 * sub-optiomal candidates for R/G/B standardization. Hence the
187 * colour modifier is omitted.
189 BU27034_CHAN_DATA(DATA0
),
190 BU27034_CHAN_DATA(DATA1
),
191 IIO_CHAN_SOFT_TIMESTAMP(4),
194 struct bu27034_data
{
195 struct regmap
*regmap
;
198 * Protect gain and time during scale adjustment and data reading.
199 * Protect measurement enabling/disabling.
203 struct task_struct
*task
;
204 __le16 raw
[BU27034_NUM_HW_DATA_CHANS
];
207 __le16 channels
[BU27034_NUM_HW_DATA_CHANS
];
212 static const struct regmap_range bu27034_volatile_ranges
[] = {
214 .range_min
= BU27034_REG_SYSTEM_CONTROL
,
215 .range_max
= BU27034_REG_SYSTEM_CONTROL
,
217 .range_min
= BU27034_REG_MODE_CONTROL4
,
218 .range_max
= BU27034_REG_MODE_CONTROL4
,
220 .range_min
= BU27034_REG_DATA0_LO
,
221 .range_max
= BU27034_REG_DATA1_HI
,
225 static const struct regmap_access_table bu27034_volatile_regs
= {
226 .yes_ranges
= &bu27034_volatile_ranges
[0],
227 .n_yes_ranges
= ARRAY_SIZE(bu27034_volatile_ranges
),
230 static const struct regmap_range bu27034_read_only_ranges
[] = {
232 .range_min
= BU27034_REG_DATA0_LO
,
233 .range_max
= BU27034_REG_DATA1_HI
,
235 .range_min
= BU27034_REG_MANUFACTURER_ID
,
236 .range_max
= BU27034_REG_MANUFACTURER_ID
,
240 static const struct regmap_access_table bu27034_ro_regs
= {
241 .no_ranges
= &bu27034_read_only_ranges
[0],
242 .n_no_ranges
= ARRAY_SIZE(bu27034_read_only_ranges
),
245 static const struct regmap_config bu27034_regmap
= {
248 .max_register
= BU27034_REG_MAX
,
249 .cache_type
= REGCACHE_RBTREE
,
250 .volatile_table
= &bu27034_volatile_regs
,
251 .wr_table
= &bu27034_ro_regs
,
254 struct bu27034_gain_check
{
260 static int bu27034_get_gain_sel(struct bu27034_data
*data
, int chan
)
263 [BU27034_CHAN_DATA0
] = BU27034_REG_MODE_CONTROL2
,
264 [BU27034_CHAN_DATA1
] = BU27034_REG_MODE_CONTROL3
,
268 ret
= regmap_read(data
->regmap
, reg
[chan
], &val
);
272 return FIELD_GET(BU27034_MASK_D01_GAIN
, val
);
275 static int bu27034_get_gain(struct bu27034_data
*data
, int chan
, int *gain
)
279 ret
= bu27034_get_gain_sel(data
, chan
);
285 ret
= iio_gts_find_gain_by_sel(&data
->gts
, sel
);
287 dev_err(data
->dev
, "chan %u: unknown gain value 0x%x\n", chan
,
298 static int bu27034_get_int_time(struct bu27034_data
*data
)
302 ret
= regmap_read(data
->regmap
, BU27034_REG_MODE_CONTROL1
, &sel
);
306 return iio_gts_find_int_time_by_sel(&data
->gts
,
307 sel
& BU27034_MASK_MEAS_MODE
);
310 static int _bu27034_get_scale(struct bu27034_data
*data
, int channel
, int *val
,
315 ret
= bu27034_get_gain(data
, channel
, &gain
);
319 ret
= bu27034_get_int_time(data
);
323 return iio_gts_get_scale(&data
->gts
, gain
, ret
, val
, val2
);
326 static int bu27034_get_scale(struct bu27034_data
*data
, int channel
, int *val
,
331 if (channel
== BU27034_CHAN_ALS
) {
334 return IIO_VAL_INT_PLUS_MICRO
;
337 mutex_lock(&data
->mutex
);
338 ret
= _bu27034_get_scale(data
, channel
, val
, val2
);
339 mutex_unlock(&data
->mutex
);
343 return IIO_VAL_INT_PLUS_NANO
;
346 /* Caller should hold the lock to protect lux reading */
347 static int bu27034_write_gain_sel(struct bu27034_data
*data
, int chan
, int sel
)
349 static const int reg
[] = {
350 [BU27034_CHAN_DATA0
] = BU27034_REG_MODE_CONTROL2
,
351 [BU27034_CHAN_DATA1
] = BU27034_REG_MODE_CONTROL3
,
355 val
= FIELD_PREP(BU27034_MASK_D01_GAIN
, sel
);
356 mask
= BU27034_MASK_D01_GAIN
;
358 return regmap_update_bits(data
->regmap
, reg
[chan
], mask
, val
);
361 static int bu27034_set_gain(struct bu27034_data
*data
, int chan
, int gain
)
365 ret
= iio_gts_find_sel_by_gain(&data
->gts
, gain
);
369 return bu27034_write_gain_sel(data
, chan
, ret
);
372 /* Caller should hold the lock to protect data->int_time */
373 static int bu27034_set_int_time(struct bu27034_data
*data
, int time
)
377 ret
= iio_gts_find_sel_by_int_time(&data
->gts
, time
);
381 return regmap_update_bits(data
->regmap
, BU27034_REG_MODE_CONTROL1
,
382 BU27034_MASK_MEAS_MODE
, ret
);
386 * We try to change the time in such way that the scale is maintained for
387 * given channels by adjusting gain so that it compensates the time change.
389 static int bu27034_try_set_int_time(struct bu27034_data
*data
, int time_us
)
391 struct bu27034_gain_check gains
[] = {
392 { .chan
= BU27034_CHAN_DATA0
},
393 { .chan
= BU27034_CHAN_DATA1
},
395 int numg
= ARRAY_SIZE(gains
);
396 int ret
, int_time_old
, i
;
398 mutex_lock(&data
->mutex
);
399 ret
= bu27034_get_int_time(data
);
405 if (!iio_gts_valid_time(&data
->gts
, time_us
)) {
406 dev_err(data
->dev
, "Unsupported integration time %u\n",
413 if (time_us
== int_time_old
) {
418 for (i
= 0; i
< numg
; i
++) {
419 ret
= bu27034_get_gain(data
, gains
[i
].chan
, &gains
[i
].old_gain
);
423 ret
= iio_gts_find_new_gain_by_old_gain_time(&data
->gts
,
425 int_time_old
, time_us
,
431 _bu27034_get_scale(data
, gains
[i
].chan
, &scale1
, &scale2
);
433 "chan %u, can't support time %u with scale %u %u\n",
434 gains
[i
].chan
, time_us
, scale1
, scale2
);
436 if (gains
[i
].new_gain
< 0)
440 * If caller requests for integration time change and we
441 * can't support the scale - then the caller should be
442 * prepared to 'pick up the pieces and deal with the
443 * fact that the scale changed'.
445 ret
= iio_find_closest_gain_low(&data
->gts
,
446 gains
[i
].new_gain
, &ok
);
450 "optimal gain out of range for chan %u\n",
455 "Total gain increase. Risk of saturation");
456 ret
= iio_gts_get_min_gain(&data
->gts
);
460 dev_dbg(data
->dev
, "chan %u scale changed\n",
462 gains
[i
].new_gain
= ret
;
463 dev_dbg(data
->dev
, "chan %u new gain %u\n",
464 gains
[i
].chan
, gains
[i
].new_gain
);
468 for (i
= 0; i
< numg
; i
++) {
469 ret
= bu27034_set_gain(data
, gains
[i
].chan
, gains
[i
].new_gain
);
474 ret
= bu27034_set_int_time(data
, time_us
);
477 mutex_unlock(&data
->mutex
);
482 static int bu27034_set_scale(struct bu27034_data
*data
, int chan
,
485 int ret
, time_sel
, gain_sel
, i
;
488 if (chan
== BU27034_CHAN_ALS
) {
489 if (val
== 0 && val2
== 1000000)
495 mutex_lock(&data
->mutex
);
496 ret
= regmap_read(data
->regmap
, BU27034_REG_MODE_CONTROL1
, &time_sel
);
500 ret
= iio_gts_find_gain_sel_for_scale_using_time(&data
->gts
, time_sel
,
501 val
, val2
, &gain_sel
);
504 * Could not support scale with given time. Need to change time.
505 * We still want to maintain the scale for all channels
507 struct bu27034_gain_check gain
;
511 * Populate information for the other channel which should also
512 * maintain the scale.
514 if (chan
== BU27034_CHAN_DATA0
)
515 gain
.chan
= BU27034_CHAN_DATA1
;
516 else if (chan
== BU27034_CHAN_DATA1
)
517 gain
.chan
= BU27034_CHAN_DATA0
;
519 ret
= bu27034_get_gain(data
, gain
.chan
, &gain
.old_gain
);
524 * Iterate through all the times to see if we find one which
525 * can support requested scale for requested channel, while
526 * maintaining the scale for the other channel
528 for (i
= 0; i
< data
->gts
.num_itime
; i
++) {
529 new_time_sel
= data
->gts
.itime_table
[i
].sel
;
531 if (new_time_sel
== time_sel
)
534 /* Can we provide requested scale with this time? */
535 ret
= iio_gts_find_gain_sel_for_scale_using_time(
536 &data
->gts
, new_time_sel
, val
, val2
,
541 /* Can the other channel maintain scale? */
542 ret
= iio_gts_find_new_gain_sel_by_old_gain_time(
543 &data
->gts
, gain
.old_gain
, time_sel
,
544 new_time_sel
, &gain
.new_gain
);
546 /* Yes - we found suitable time */
553 "Can't set scale maintaining other channel\n");
559 ret
= bu27034_set_gain(data
, gain
.chan
, gain
.new_gain
);
563 ret
= regmap_update_bits(data
->regmap
, BU27034_REG_MODE_CONTROL1
,
564 BU27034_MASK_MEAS_MODE
, new_time_sel
);
569 ret
= bu27034_write_gain_sel(data
, chan
, gain_sel
);
571 mutex_unlock(&data
->mutex
);
578 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1/D0 – 1.5) * (0.25) + 1)
580 * => -0.000745625 * D0 + 0.0002515625 * D1 + -0.000018675 * D1 * D1 / D0
582 * => (6.44 * ch1 / gain1 + 19.088 * ch0 / gain0 -
583 * 0.47808 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0) /
587 * lx = 0.001193 * D0 - 0.0000747 * D1
589 * => (1.91232 * ch1 / gain1 + 30.5408 * ch0 / gain0 +
590 * [0 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0] ) /
593 * This can be unified to format:
595 * A * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
611 struct bu27034_lx_coeff
{
615 /* Indicate which of the coefficients above are negative */
619 static inline u64
gain_mul_div_helper(u64 val
, unsigned int gain
,
623 * Max gain for a channel is 4096. The max u64 (0xffffffffffffffffULL)
624 * divided by 4096 is 0xFFFFFFFFFFFFF (GENMASK_ULL(51, 0)) (floored).
625 * Thus, the 0xFFFFFFFFFFFFF is the largest value we can safely multiply
626 * with the gain, no matter what gain is set.
628 * So, multiplication with max gain may overflow if val is greater than
629 * 0xFFFFFFFFFFFFF (52 bits set)..
631 * If this is the case we divide first.
633 if (val
< GENMASK_ULL(51, 0)) {
644 static u64
bu27034_fixp_calc_t1_64bit(unsigned int coeff
, unsigned int ch0
,
645 unsigned int ch1
, unsigned int gain0
,
651 helper64
= (u64
)coeff
* (u64
)ch1
* (u64
)ch1
;
653 helper
= gain1
* gain1
;
655 do_div(helper64
, helper
);
657 return gain_mul_div_helper(helper64
, gain0
, ch0
);
660 do_div(helper64
, ch0
);
662 return gain_mul_div_helper(helper64
, gain0
, helper
);
666 static u64
bu27034_fixp_calc_t1(unsigned int coeff
, unsigned int ch0
,
667 unsigned int ch1
, unsigned int gain0
,
670 unsigned int helper
, tmp
;
673 * Here we could overflow even the 64bit value. Hence we
674 * multiply with gain0 only after the divisions - even though
675 * it may result loss of accuracy
677 helper
= coeff
* ch1
* ch1
;
678 tmp
= helper
* gain0
;
682 if (check_mul_overflow(helper
, coeff
, &helper
))
683 return bu27034_fixp_calc_t1_64bit(coeff
, ch0
, ch1
, gain0
, gain1
);
685 if (check_mul_overflow(helper
, gain0
, &tmp
))
686 return bu27034_fixp_calc_t1_64bit(coeff
, ch0
, ch1
, gain0
, gain1
);
688 return tmp
/ (gain1
* gain1
) / ch0
;
692 static u64
bu27034_fixp_calc_t23(unsigned int coeff
, unsigned int ch
,
698 if (!check_mul_overflow(coeff
, ch
, &helper
))
699 return helper
/ gain
;
701 helper64
= (u64
)coeff
* (u64
)ch
;
702 do_div(helper64
, gain
);
707 static int bu27034_fixp_calc_lx(unsigned int ch0
, unsigned int ch1
,
708 unsigned int gain0
, unsigned int gain1
,
709 unsigned int meastime
, int coeff_idx
)
711 static const struct bu27034_lx_coeff coeff
[] = {
713 .A
= 4780800, /* -0.47808 */
714 .B
= 64400000, /* 6.44 */
715 .C
= 190880000, /* 19.088 */
716 .is_neg
= { true, false, false },
719 .B
= 19123200, /* 1.91232 */
720 .C
= 305408000, /* 30.5408 */
721 /* All terms positive */
724 const struct bu27034_lx_coeff
*c
= &coeff
[coeff_idx
];
725 u64 res
= 0, terms
[3];
728 if (coeff_idx
>= ARRAY_SIZE(coeff
))
731 terms
[0] = bu27034_fixp_calc_t1(c
->A
, ch0
, ch1
, gain0
, gain1
);
732 terms
[1] = bu27034_fixp_calc_t23(c
->B
, ch1
, gain1
);
733 terms
[2] = bu27034_fixp_calc_t23(c
->C
, ch0
, gain0
);
735 /* First, add positive terms */
736 for (i
= 0; i
< 3; i
++)
740 /* No positive term => zero lux */
744 /* Then, subtract negative terms (if any) */
745 for (i
= 0; i
< 3; i
++)
748 * If the negative term is greater than positive - then
749 * the darkness has taken over and we are all doomed! Eh,
750 * I mean, then we can just return 0 lx and go out
759 do_div(res
, meastime
);
764 static bool bu27034_has_valid_sample(struct bu27034_data
*data
)
768 ret
= regmap_read(data
->regmap
, BU27034_REG_MODE_CONTROL4
, &val
);
770 dev_err(data
->dev
, "Read failed %d\n", ret
);
775 return val
& BU27034_MASK_VALID
;
779 * Reading the register where VALID bit is clears this bit. (So does changing
780 * any gain / integration time configuration registers) The bit gets
781 * set when we have acquired new data. We use this bit to indicate data
784 static void bu27034_invalidate_read_data(struct bu27034_data
*data
)
786 bu27034_has_valid_sample(data
);
789 static int bu27034_read_result(struct bu27034_data
*data
, int chan
, int *res
)
792 [BU27034_CHAN_DATA0
] = BU27034_REG_DATA0_LO
,
793 [BU27034_CHAN_DATA1
] = BU27034_REG_DATA1_LO
,
798 ret
= regmap_read_poll_timeout(data
->regmap
, BU27034_REG_MODE_CONTROL4
,
799 valid
, (valid
& BU27034_MASK_VALID
),
800 BU27034_DATA_WAIT_TIME_US
, 0);
804 ret
= regmap_bulk_read(data
->regmap
, reg
[chan
], &val
, sizeof(val
));
808 *res
= le16_to_cpu(val
);
813 static int bu27034_get_result_unlocked(struct bu27034_data
*data
, __le16
*res
,
816 int ret
= 0, retry_cnt
= 0;
819 /* Get new value from sensor if data is ready */
820 if (bu27034_has_valid_sample(data
)) {
821 ret
= regmap_bulk_read(data
->regmap
, BU27034_REG_DATA0_LO
,
826 bu27034_invalidate_read_data(data
);
828 /* No new data in sensor. Wait and retry */
831 if (retry_cnt
> BU27034_RETRY_LIMIT
) {
832 dev_err(data
->dev
, "No data from sensor\n");
845 static int bu27034_meas_set(struct bu27034_data
*data
, bool en
)
848 return regmap_set_bits(data
->regmap
, BU27034_REG_MODE_CONTROL4
,
849 BU27034_MASK_MEAS_EN
);
851 return regmap_clear_bits(data
->regmap
, BU27034_REG_MODE_CONTROL4
,
852 BU27034_MASK_MEAS_EN
);
855 static int bu27034_get_single_result(struct bu27034_data
*data
, int chan
,
860 if (chan
< BU27034_CHAN_DATA0
|| chan
> BU27034_CHAN_DATA1
)
863 ret
= bu27034_meas_set(data
, true);
867 ret
= bu27034_get_int_time(data
);
873 return bu27034_read_result(data
, chan
, val
);
877 * The formula given by vendor for computing luxes out of data0 and data1
878 * (in open air) is as follows:
881 * D0 = data0/ch0_gain/meas_time_ms * 25600
882 * D1 = data1/ch1_gain/meas_time_ms * 25600
886 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1 / D0 – 1.5) * 0.25 + 1)
888 * lx = (0.001193 * D0 + (-0.0000747) * D1)
890 * We use it here. Users who have for example some colored lens
891 * need to modify the calculation but I hope this gives a starting point for
892 * those working with such devices.
895 static int bu27034_calc_mlux(struct bu27034_data
*data
, __le16
*res
, int *val
)
897 unsigned int gain0
, gain1
, meastime
;
898 unsigned int d1_d0_ratio_scaled
;
904 * We return 0 lux if calculation fails. This should be reasonably
905 * easy to spot from the buffers especially if raw-data channels show
910 ch0
= max_t(u16
, 1, le16_to_cpu(res
[0]));
911 ch1
= max_t(u16
, 1, le16_to_cpu(res
[1]));
913 ret
= bu27034_get_gain(data
, BU27034_CHAN_DATA0
, &gain0
);
917 ret
= bu27034_get_gain(data
, BU27034_CHAN_DATA1
, &gain1
);
921 ret
= bu27034_get_int_time(data
);
927 d1_d0_ratio_scaled
= (unsigned int)ch1
* (unsigned int)gain0
* 100;
928 helper64
= (u64
)ch1
* (u64
)gain0
* 100LLU;
930 if (helper64
!= d1_d0_ratio_scaled
) {
931 unsigned int div
= (unsigned int)ch0
* gain1
;
933 do_div(helper64
, div
);
934 d1_d0_ratio_scaled
= helper64
;
936 d1_d0_ratio_scaled
/= ch0
* gain1
;
939 if (d1_d0_ratio_scaled
< 150)
940 ret
= bu27034_fixp_calc_lx(ch0
, ch1
, gain0
, gain1
, meastime
, 0);
942 ret
= bu27034_fixp_calc_lx(ch0
, ch1
, gain0
, gain1
, meastime
, 1);
953 static int bu27034_get_mlux(struct bu27034_data
*data
, int chan
, int *val
)
955 __le16 res
[BU27034_NUM_HW_DATA_CHANS
];
958 ret
= bu27034_meas_set(data
, true);
962 ret
= bu27034_get_result_unlocked(data
, &res
[0], sizeof(res
));
966 ret
= bu27034_calc_mlux(data
, res
, val
);
970 ret
= bu27034_meas_set(data
, false);
972 dev_err(data
->dev
, "failed to disable measurement\n");
977 static int bu27034_read_raw(struct iio_dev
*idev
,
978 struct iio_chan_spec
const *chan
,
979 int *val
, int *val2
, long mask
)
981 struct bu27034_data
*data
= iio_priv(idev
);
985 case IIO_CHAN_INFO_INT_TIME
:
987 *val2
= bu27034_get_int_time(data
);
991 return IIO_VAL_INT_PLUS_MICRO
;
993 case IIO_CHAN_INFO_HARDWAREGAIN
:
994 ret
= bu27034_get_gain(data
, chan
->channel
, val
);
1000 case IIO_CHAN_INFO_SCALE
:
1001 return bu27034_get_scale(data
, chan
->channel
, val
, val2
);
1003 case IIO_CHAN_INFO_RAW
:
1005 int (*result_get
)(struct bu27034_data
*data
, int chan
, int *val
);
1007 if (chan
->type
== IIO_INTENSITY
)
1008 result_get
= bu27034_get_single_result
;
1009 else if (chan
->type
== IIO_LIGHT
)
1010 result_get
= bu27034_get_mlux
;
1014 /* Don't mess with measurement enabling while buffering */
1015 ret
= iio_device_claim_direct_mode(idev
);
1019 mutex_lock(&data
->mutex
);
1021 * Reading one channel at a time is inefficient but we
1022 * don't care here. Buffered version should be used if
1023 * performance is an issue.
1025 ret
= result_get(data
, chan
->channel
, val
);
1027 mutex_unlock(&data
->mutex
);
1028 iio_device_release_direct_mode(idev
);
1040 static int bu27034_write_raw_get_fmt(struct iio_dev
*indio_dev
,
1041 struct iio_chan_spec
const *chan
,
1044 struct bu27034_data
*data
= iio_priv(indio_dev
);
1047 case IIO_CHAN_INFO_SCALE
:
1048 return IIO_VAL_INT_PLUS_NANO
;
1049 case IIO_CHAN_INFO_INT_TIME
:
1050 return IIO_VAL_INT_PLUS_MICRO
;
1051 case IIO_CHAN_INFO_HARDWAREGAIN
:
1053 "HARDWAREGAIN is read-only, use scale to set\n");
1060 static int bu27034_write_raw(struct iio_dev
*idev
,
1061 struct iio_chan_spec
const *chan
,
1062 int val
, int val2
, long mask
)
1064 struct bu27034_data
*data
= iio_priv(idev
);
1067 ret
= iio_device_claim_direct_mode(idev
);
1072 case IIO_CHAN_INFO_SCALE
:
1073 ret
= bu27034_set_scale(data
, chan
->channel
, val
, val2
);
1075 case IIO_CHAN_INFO_INT_TIME
:
1077 ret
= bu27034_try_set_int_time(data
, val2
);
1086 iio_device_release_direct_mode(idev
);
1091 static int bu27034_read_avail(struct iio_dev
*idev
,
1092 struct iio_chan_spec
const *chan
, const int **vals
,
1093 int *type
, int *length
, long mask
)
1095 struct bu27034_data
*data
= iio_priv(idev
);
1098 case IIO_CHAN_INFO_INT_TIME
:
1099 return iio_gts_avail_times(&data
->gts
, vals
, type
, length
);
1100 case IIO_CHAN_INFO_SCALE
:
1101 return iio_gts_all_avail_scales(&data
->gts
, vals
, type
, length
);
1107 static const struct iio_info bu27034_info
= {
1108 .read_raw
= &bu27034_read_raw
,
1109 .write_raw
= &bu27034_write_raw
,
1110 .write_raw_get_fmt
= &bu27034_write_raw_get_fmt
,
1111 .read_avail
= &bu27034_read_avail
,
1114 static int bu27034_chip_init(struct bu27034_data
*data
)
1119 ret
= regmap_write_bits(data
->regmap
, BU27034_REG_SYSTEM_CONTROL
,
1120 BU27034_MASK_SW_RESET
, BU27034_MASK_SW_RESET
);
1122 return dev_err_probe(data
->dev
, ret
, "Sensor reset failed\n");
1126 ret
= regmap_reinit_cache(data
->regmap
, &bu27034_regmap
);
1128 dev_err(data
->dev
, "Failed to reinit reg cache\n");
1133 * Read integration time here to ensure it is in regmap cache. We do
1134 * this to speed-up the int-time acquisition in the start of the buffer
1135 * handling thread where longer delays could make it more likely we end
1136 * up skipping a sample, and where the longer delays make timestamps
1139 ret
= regmap_read(data
->regmap
, BU27034_REG_MODE_CONTROL1
, &sel
);
1141 dev_err(data
->dev
, "reading integration time failed\n");
1146 static int bu27034_wait_for_data(struct bu27034_data
*data
)
1150 ret
= regmap_read_poll_timeout(data
->regmap
, BU27034_REG_MODE_CONTROL4
,
1151 val
, val
& BU27034_MASK_VALID
,
1152 BU27034_DATA_WAIT_TIME_US
,
1153 BU27034_TOTAL_DATA_WAIT_TIME_US
);
1155 dev_err(data
->dev
, "data polling %s\n",
1156 !(val
& BU27034_MASK_VALID
) ? "timeout" : "fail");
1161 ret
= regmap_bulk_read(data
->regmap
, BU27034_REG_DATA0_LO
,
1162 &data
->scan
.channels
[0],
1163 sizeof(data
->scan
.channels
));
1167 bu27034_invalidate_read_data(data
);
1172 static int bu27034_buffer_thread(void *arg
)
1174 struct iio_dev
*idev
= arg
;
1175 struct bu27034_data
*data
;
1178 data
= iio_priv(idev
);
1180 wait_ms
= bu27034_get_int_time(data
);
1183 wait_ms
-= BU27034_MEAS_WAIT_PREMATURE_MS
;
1185 while (!kthread_should_stop()) {
1190 ret
= bu27034_wait_for_data(data
);
1194 tstamp
= iio_get_time_ns(idev
);
1196 if (test_bit(BU27034_CHAN_ALS
, idev
->active_scan_mask
)) {
1199 ret
= bu27034_calc_mlux(data
, &data
->scan
.channels
[0],
1202 dev_err(data
->dev
, "failed to calculate lux\n");
1205 * The maximum Milli lux value we get with gain 1x time
1206 * 55mS data ch0 = 0xffff ch1 = 0xffff fits in 26 bits
1207 * so there should be no problem returning int from
1208 * computations and casting it to u32
1210 data
->scan
.mlux
= (u32
)mlux
;
1212 iio_push_to_buffers_with_timestamp(idev
, &data
->scan
, tstamp
);
1218 static int bu27034_buffer_enable(struct iio_dev
*idev
)
1220 struct bu27034_data
*data
= iio_priv(idev
);
1221 struct task_struct
*task
;
1224 mutex_lock(&data
->mutex
);
1225 ret
= bu27034_meas_set(data
, true);
1229 task
= kthread_run(bu27034_buffer_thread
, idev
,
1230 "bu27034-buffering-%u",
1231 iio_device_id(idev
));
1233 ret
= PTR_ERR(task
);
1240 mutex_unlock(&data
->mutex
);
1245 static int bu27034_buffer_disable(struct iio_dev
*idev
)
1247 struct bu27034_data
*data
= iio_priv(idev
);
1250 mutex_lock(&data
->mutex
);
1252 kthread_stop(data
->task
);
1256 ret
= bu27034_meas_set(data
, false);
1257 mutex_unlock(&data
->mutex
);
1262 static const struct iio_buffer_setup_ops bu27034_buffer_ops
= {
1263 .postenable
= &bu27034_buffer_enable
,
1264 .predisable
= &bu27034_buffer_disable
,
1267 static int bu27034_probe(struct i2c_client
*i2c
)
1269 struct device
*dev
= &i2c
->dev
;
1270 struct bu27034_data
*data
;
1271 struct regmap
*regmap
;
1272 struct iio_dev
*idev
;
1273 unsigned int part_id
, reg
;
1276 regmap
= devm_regmap_init_i2c(i2c
, &bu27034_regmap
);
1278 return dev_err_probe(dev
, PTR_ERR(regmap
),
1279 "Failed to initialize Regmap\n");
1281 idev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1285 ret
= devm_regulator_get_enable(dev
, "vdd");
1287 return dev_err_probe(dev
, ret
, "Failed to get regulator\n");
1289 data
= iio_priv(idev
);
1291 ret
= regmap_read(regmap
, BU27034_REG_SYSTEM_CONTROL
, ®
);
1293 return dev_err_probe(dev
, ret
, "Failed to access sensor\n");
1295 part_id
= FIELD_GET(BU27034_MASK_PART_ID
, reg
);
1297 if (part_id
!= BU27034_ID
)
1298 dev_warn(dev
, "unknown device 0x%x\n", part_id
);
1300 ret
= devm_iio_init_iio_gts(dev
, BU27034_SCALE_1X
, 0, bu27034_gains
,
1301 ARRAY_SIZE(bu27034_gains
), bu27034_itimes
,
1302 ARRAY_SIZE(bu27034_itimes
), &data
->gts
);
1306 mutex_init(&data
->mutex
);
1307 data
->regmap
= regmap
;
1310 idev
->channels
= bu27034_channels
;
1311 idev
->num_channels
= ARRAY_SIZE(bu27034_channels
);
1312 idev
->name
= "bu27034";
1313 idev
->info
= &bu27034_info
;
1315 idev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1316 idev
->available_scan_masks
= bu27034_scan_masks
;
1318 ret
= bu27034_chip_init(data
);
1322 ret
= devm_iio_kfifo_buffer_setup(dev
, idev
, &bu27034_buffer_ops
);
1324 return dev_err_probe(dev
, ret
, "buffer setup failed\n");
1326 ret
= devm_iio_device_register(dev
, idev
);
1328 return dev_err_probe(dev
, ret
,
1329 "Unable to register iio device\n");
1334 static const struct of_device_id bu27034_of_match
[] = {
1335 { .compatible
= "rohm,bu27034anuc" },
1338 MODULE_DEVICE_TABLE(of
, bu27034_of_match
);
1340 static struct i2c_driver bu27034_i2c_driver
= {
1342 .name
= "bu27034-als",
1343 .of_match_table
= bu27034_of_match
,
1344 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1346 .probe
= bu27034_probe
,
1348 module_i2c_driver(bu27034_i2c_driver
);
1350 MODULE_LICENSE("GPL");
1351 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
1352 MODULE_DESCRIPTION("ROHM BU27034 ambient light sensor driver");
1353 MODULE_IMPORT_NS("IIO_GTS_HELPER");