Lines Matching +full:ts +full:- +full:coeff +full:- +full:h

1 // SPDX-License-Identifier: GPL-2.0-only
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>
55 * inevitable even if the sensor clock would be perfectly phase-locked to CPU
56 * clock - which we can't say is the case.
59 * risk of losing a sample because things can in a rainy-day scenario be
72 * Downside is that the time-stamps would be very inaccurate as the wake-up
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
100 * Available scales with gain 1x - 4096x, timings 55, 100, 200, 400 mS
138 * the data collection to data0-channel only and cuts the supported range to
141 * "normal" modes are 55, 100, 200 and 400 mS modes - which do have direct
144 * This means that if meas-mode is changed for example from 400 => 200,
222 s64 ts __aligned(8);
292 ret = regmap_read(data->regmap, reg[chan], &val); in bu27034_get_gain_sel()
302 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL2, &val); in bu27034_get_gain_sel()
308 * [7:6], [2:0]. Thus when we combine the 5-bit 'selector' in bu27034_get_gain_sel()
315 return -EINVAL; in bu27034_get_gain_sel()
329 ret = iio_gts_find_gain_by_sel(&data->gts, sel); in bu27034_get_gain()
331 dev_err(data->dev, "chan %u: unknown gain value 0x%x\n", chan, in bu27034_get_gain()
346 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); in bu27034_get_int_time()
350 return iio_gts_find_int_time_by_sel(&data->gts, in bu27034_get_int_time()
367 return iio_gts_get_scale(&data->gts, gain, ret, val, val2); in _bu27034_get_scale()
381 mutex_lock(&data->mutex); in bu27034_get_scale()
383 mutex_unlock(&data->mutex); in bu27034_get_scale()
400 return -EINVAL; in bu27034_write_gain_sel()
419 * channel 2 to be used in any generic cases - the intensity in bu27034_write_gain_sel()
424 * channel 2 - then it is probably specifically targeted to this in bu27034_write_gain_sel()
437 return regmap_update_bits(data->regmap, reg[chan], mask, val); in bu27034_write_gain_sel()
446 * gain for channel 0 - which shares the high bits in bu27034_set_gain()
449 return -EINVAL; in bu27034_set_gain()
451 ret = iio_gts_find_sel_by_gain(&data->gts, gain); in bu27034_set_gain()
458 /* Caller should hold the lock to protect data->int_time */
463 ret = iio_gts_find_sel_by_int_time(&data->gts, time); in bu27034_set_int_time()
467 return regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, in bu27034_set_int_time()
484 mutex_lock(&data->mutex); in bu27034_try_set_int_time()
491 if (!iio_gts_valid_time(&data->gts, time_us)) { in bu27034_try_set_int_time()
492 dev_err(data->dev, "Unsupported integration time %u\n", in bu27034_try_set_int_time()
494 ret = -EINVAL; in bu27034_try_set_int_time()
509 ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, in bu27034_try_set_int_time()
518 dev_dbg(data->dev, in bu27034_try_set_int_time()
527 * can't support the scale - then the caller should be in bu27034_try_set_int_time()
531 ret = iio_find_closest_gain_low(&data->gts, in bu27034_try_set_int_time()
535 dev_dbg(data->dev, in bu27034_try_set_int_time()
540 dev_dbg(data->dev, in bu27034_try_set_int_time()
542 ret = iio_gts_get_min_gain(&data->gts); in bu27034_try_set_int_time()
546 dev_dbg(data->dev, "chan %u scale changed\n", in bu27034_try_set_int_time()
549 dev_dbg(data->dev, "chan %u new gain %u\n", in bu27034_try_set_int_time()
563 mutex_unlock(&data->mutex); in bu27034_try_set_int_time()
575 return -EINVAL; in bu27034_set_scale()
581 return -EINVAL; in bu27034_set_scale()
584 mutex_lock(&data->mutex); in bu27034_set_scale()
585 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &time_sel); in bu27034_set_scale()
589 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel, in bu27034_set_scale()
619 for (i = 0; i < data->gts.num_itime; i++) { in bu27034_set_scale()
620 new_time_sel = data->gts.itime_table[i].sel; in bu27034_set_scale()
627 &data->gts, new_time_sel, val, val2, in bu27034_set_scale()
634 &data->gts, gain.old_gain, time_sel, in bu27034_set_scale()
637 /* Yes - we found suitable time */ in bu27034_set_scale()
643 dev_dbg(data->dev, in bu27034_set_scale()
645 ret = -EINVAL; in bu27034_set_scale()
654 ret = regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, in bu27034_set_scale()
662 mutex_unlock(&data->mutex); in bu27034_set_scale()
669 * lx = 0.004521097 * D1 - 0.002663996 * D0 +
672 * => 115.7400832 * ch1 / gain1 / mt -
687 * lx = (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) /
689 * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) /
693 * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0 +
700 * 100 * ch1 / gain1 / mt) * ((D1/D0 - 0.87) * (0.385) + 1)
702 * ((D1/D0 - 0.87) * (0.385) + 1)
704 * (0.385 * D1/D0 - 0.66505)
706 * (0.385 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) - 0.66505)
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)
725 * 100 * ch1 / gain1 / mt) * (-0.05D1/D0 + 1.1)
727 * (-0.05 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) + 1.1)
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)
732 * + 0.90624 * ch1 * -1280 * ch1 *gain0 * mt / (gain1 * mt *gain1 * mt * 25600 * ch0)
734 * => -43614.208 * ch1 / (gain1 * mt * 25600)
736 * - 1159.9872 * ch1 * ch1 * gain0 / (gain1 * gain1 * mt * 25600 * ch0)
739 * - 0.045312 * ch1 * ch1 * gain0 / (gain1 * gain1 * ch0)
740 * - 0.706816 * ch1 / gain1
749 * -68.1982976 * ch0 / gain0
760 * => [-0.045312 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) +
761 * -0.706816 * ch1 / gain1 +
775 * C = -68.1982976
783 * A = -0.045312
784 * B = -0.706816
821 static u64 bu27034_fixp_calc_t1_64bit(unsigned int coeff, unsigned int ch0, in bu27034_fixp_calc_t1_64bit() argument
828 helper64 = (u64)coeff * (u64)ch1 * (u64)ch1; in bu27034_fixp_calc_t1_64bit()
843 static u64 bu27034_fixp_calc_t1(unsigned int coeff, unsigned int ch0, in bu27034_fixp_calc_t1() argument
851 * multiply with gain0 only after the divisions - even though in bu27034_fixp_calc_t1()
854 helper = coeff * ch1 * ch1; in bu27034_fixp_calc_t1()
859 if (check_mul_overflow(helper, coeff, &helper)) in bu27034_fixp_calc_t1()
860 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1); in bu27034_fixp_calc_t1()
863 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1); in bu27034_fixp_calc_t1()
869 static u64 bu27034_fixp_calc_t23(unsigned int coeff, unsigned int ch, in bu27034_fixp_calc_t23() argument
875 if (!check_mul_overflow(coeff, ch, &helper)) in bu27034_fixp_calc_t23()
878 helper64 = (u64)coeff * (u64)ch; in bu27034_fixp_calc_t23()
888 static const struct bu27034_lx_coeff coeff[] = { in bu27034_fixp_calc_lx() local
892 .C = 681982976, /* -68.1982976 */ in bu27034_fixp_calc_lx()
900 .A = 453120, /* -0.045312 */ in bu27034_fixp_calc_lx()
901 .B = 7068160, /* -0.706816 */ in bu27034_fixp_calc_lx()
906 const struct bu27034_lx_coeff *c = &coeff[coeff_idx]; in bu27034_fixp_calc_lx()
910 if (coeff_idx >= ARRAY_SIZE(coeff)) in bu27034_fixp_calc_lx()
911 return -EINVAL; in bu27034_fixp_calc_lx()
913 terms[0] = bu27034_fixp_calc_t1(c->A, ch0, ch1, gain0, gain1); in bu27034_fixp_calc_lx()
914 terms[1] = bu27034_fixp_calc_t23(c->B, ch1, gain1); in bu27034_fixp_calc_lx()
915 terms[2] = bu27034_fixp_calc_t23(c->C, ch0, gain0); in bu27034_fixp_calc_lx()
919 if (!c->is_neg[i]) in bu27034_fixp_calc_lx()
928 if (c->is_neg[i]) { in bu27034_fixp_calc_lx()
930 * If the negative term is greater than positive - then in bu27034_fixp_calc_lx()
937 res -= terms[i]; in bu27034_fixp_calc_lx()
950 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL4, &val); in bu27034_has_valid_sample()
952 dev_err(data->dev, "Read failed %d\n", ret); in bu27034_has_valid_sample()
981 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_read_result()
987 ret = regmap_bulk_read(data->regmap, reg[chan], &val, sizeof(val)); in bu27034_read_result()
1004 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, in bu27034_get_result_unlocked()
1015 dev_err(data->dev, "No data from sensor\n"); in bu27034_get_result_unlocked()
1017 return -ETIMEDOUT; in bu27034_get_result_unlocked()
1031 return regmap_set_bits(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_meas_set()
1034 return regmap_clear_bits(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_meas_set()
1044 return -EINVAL; in bu27034_get_single_result()
1069 * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 0.87) * 3.45 + 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)
1090 * easy to spot from the buffers especially if raw-data channels show in bu27034_calc_mlux()
1159 dev_err(data->dev, "failed to disable measurement\n"); in bu27034_get_mlux()
1181 return bu27034_get_scale(data, chan->channel, val, val2); in bu27034_read_raw()
1187 if (chan->type == IIO_INTENSITY) in bu27034_read_raw()
1189 else if (chan->type == IIO_LIGHT) in bu27034_read_raw()
1192 return -EINVAL; in bu27034_read_raw()
1199 mutex_lock(&data->mutex); in bu27034_read_raw()
1205 ret = result_get(data, chan->channel, val); in bu27034_read_raw()
1207 mutex_unlock(&data->mutex); in bu27034_read_raw()
1216 return -EINVAL; in bu27034_read_raw()
1231 return -EINVAL; in bu27034_write_raw_get_fmt()
1248 ret = bu27034_set_scale(data, chan->channel, val, val2); in bu27034_write_raw()
1254 ret = -EINVAL; in bu27034_write_raw()
1257 ret = -EINVAL; in bu27034_write_raw()
1274 return iio_gts_avail_times(&data->gts, vals, type, length); in bu27034_read_avail()
1276 return iio_gts_all_avail_scales(&data->gts, vals, type, length); in bu27034_read_avail()
1278 return -EINVAL; in bu27034_read_avail()
1294 ret = regmap_write_bits(data->regmap, BU27034_REG_SYSTEM_CONTROL, in bu27034_chip_init()
1297 return dev_err_probe(data->dev, ret, "Sensor reset failed\n"); in bu27034_chip_init()
1301 ret = regmap_reinit_cache(data->regmap, &bu27034_regmap); in bu27034_chip_init()
1303 dev_err(data->dev, "Failed to reinit reg cache\n"); in bu27034_chip_init()
1309 * this to speed-up the int-time acquisition in the start of the buffer in bu27034_chip_init()
1314 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); in bu27034_chip_init()
1316 dev_err(data->dev, "reading integration time failed\n"); in bu27034_chip_init()
1325 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_wait_for_data()
1330 dev_err(data->dev, "data polling %s\n", in bu27034_wait_for_data()
1336 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, in bu27034_wait_for_data()
1337 &data->scan.channels[0], in bu27034_wait_for_data()
1338 sizeof(data->scan.channels)); in bu27034_wait_for_data()
1358 wait_ms -= BU27034_MEAS_WAIT_PREMATURE_MS; in bu27034_buffer_thread()
1371 if (test_bit(BU27034_CHAN_ALS, idev->active_scan_mask)) { in bu27034_buffer_thread()
1374 ret = bu27034_calc_mlux(data, &data->scan.channels[0], in bu27034_buffer_thread()
1377 dev_err(data->dev, "failed to calculate lux\n"); in bu27034_buffer_thread()
1385 data->scan.mlux = (u32)mlux; in bu27034_buffer_thread()
1387 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp); in bu27034_buffer_thread()
1399 mutex_lock(&data->mutex); in bu27034_buffer_enable()
1405 "bu27034-buffering-%u", in bu27034_buffer_enable()
1412 data->task = task; in bu27034_buffer_enable()
1415 mutex_unlock(&data->mutex); in bu27034_buffer_enable()
1425 mutex_lock(&data->mutex); in bu27034_buffer_disable()
1426 if (data->task) { in bu27034_buffer_disable()
1427 kthread_stop(data->task); in bu27034_buffer_disable()
1428 data->task = NULL; in bu27034_buffer_disable()
1432 mutex_unlock(&data->mutex); in bu27034_buffer_disable()
1444 struct device *dev = &i2c->dev; in bu27034_probe()
1458 return -ENOMEM; in bu27034_probe()
1477 ARRAY_SIZE(bu27034_itimes), &data->gts); in bu27034_probe()
1481 mutex_init(&data->mutex); in bu27034_probe()
1482 data->regmap = regmap; in bu27034_probe()
1483 data->dev = dev; in bu27034_probe()
1485 idev->channels = bu27034_channels; in bu27034_probe()
1486 idev->num_channels = ARRAY_SIZE(bu27034_channels); in bu27034_probe()
1487 idev->name = "bu27034"; in bu27034_probe()
1488 idev->info = &bu27034_info; in bu27034_probe()
1490 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; in bu27034_probe()
1491 idev->available_scan_masks = bu27034_scan_masks; in bu27034_probe()
1517 .name = "bu27034-als",