Lines Matching +full:colour +full:- +full:sensor
1 // SPDX-License-Identifier: GPL-2.0-only
3 * BU27034ANUC ROHM Ambient Light Sensor
21 #include <linux/iio/iio-gts-helper.h>
53 * inevitable even if the sensor clock would be perfectly phase-locked to CPU
54 * clock - which we can't say is the case.
57 * risk of losing a sample because things can in a rainy-day scenario be
70 * Downside is that the time-stamps would be very inaccurate as the wake-up
71 * would not really be tied to the sensor toggling the valid bit. This would also
72 * result 'jumps' in the time-stamps when the delay drifted so that wake-up was
73 * performed during the consecutive wake-ups (Or, when sensor and CPU clocks
74 * were very different and scheduling the wake-ups was very close to given
75 * timeout - and when the time-outs were very close to the actual sensor
76 * sampling, Eg. once in a blue moon, two consecutive time-outs would occur
98 * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS
129 * Measurement modes are 55, 100, 200 and 400 mS modes - which do have direct
132 * This means that if meas-mode is changed for example from 400 => 200,
187 * sub-optiomal candidates for R/G/B standardization. Hence the
188 * colour modifier is omitted.
269 ret = regmap_read(data->regmap, reg[chan], &val); in bu27034_get_gain_sel()
286 ret = iio_gts_find_gain_by_sel(&data->gts, sel); in bu27034_get_gain()
288 dev_err(data->dev, "chan %u: unknown gain value 0x%x\n", chan, in bu27034_get_gain()
303 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); in bu27034_get_int_time()
307 return iio_gts_find_int_time_by_sel(&data->gts, in bu27034_get_int_time()
324 return iio_gts_get_scale(&data->gts, gain, ret, val, val2); in _bu27034_get_scale()
338 mutex_lock(&data->mutex); in bu27034_get_scale()
340 mutex_unlock(&data->mutex); in bu27034_get_scale()
359 return regmap_update_bits(data->regmap, reg[chan], mask, val); in bu27034_write_gain_sel()
366 ret = iio_gts_find_sel_by_gain(&data->gts, gain); in bu27034_set_gain()
373 /* Caller should hold the lock to protect data->int_time */
378 ret = iio_gts_find_sel_by_int_time(&data->gts, time); in bu27034_set_int_time()
382 return regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, in bu27034_set_int_time()
399 guard(mutex)(&data->mutex); in bu27034_try_set_int_time()
406 if (!iio_gts_valid_time(&data->gts, time_us)) { in bu27034_try_set_int_time()
407 dev_err(data->dev, "Unsupported integration time %u\n", in bu27034_try_set_int_time()
409 return -EINVAL; in bu27034_try_set_int_time()
420 ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, in bu27034_try_set_int_time()
429 dev_dbg(data->dev, in bu27034_try_set_int_time()
438 * can't support the scale - then the caller should be in bu27034_try_set_int_time()
442 ret = iio_find_closest_gain_low(&data->gts, in bu27034_try_set_int_time()
446 dev_dbg(data->dev, in bu27034_try_set_int_time()
451 dev_dbg(data->dev, in bu27034_try_set_int_time()
453 ret = iio_gts_get_min_gain(&data->gts); in bu27034_try_set_int_time()
457 dev_dbg(data->dev, "chan %u scale changed\n", in bu27034_try_set_int_time()
460 dev_dbg(data->dev, "chan %u new gain %u\n", in bu27034_try_set_int_time()
484 return -EINVAL; in bu27034_set_scale()
487 guard(mutex)(&data->mutex); in bu27034_set_scale()
488 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &time_sel); in bu27034_set_scale()
492 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel, in bu27034_set_scale()
520 for (i = 0; i < data->gts.num_itime; i++) { in bu27034_set_scale()
521 new_time_sel = data->gts.itime_table[i].sel; in bu27034_set_scale()
528 &data->gts, new_time_sel, val, val2, in bu27034_set_scale()
535 &data->gts, gain.old_gain, time_sel, in bu27034_set_scale()
538 /* Yes - we found suitable time */ in bu27034_set_scale()
544 dev_dbg(data->dev, in bu27034_set_scale()
546 return -EINVAL; in bu27034_set_scale()
553 ret = regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, in bu27034_set_scale()
564 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1/D0 – 1.5) * (0.25) + 1)
566 * => -0.000745625 * D0 + 0.0002515625 * D1 + -0.000018675 * D1 * D1 / D0
568 * => (6.44 * ch1 / gain1 + 19.088 * ch0 / gain0 -
573 * lx = 0.001193 * D0 - 0.0000747 * D1
587 * A = -0.47808,
660 * multiply with gain0 only after the divisions - even though in bu27034_fixp_calc_t1()
699 .A = 4780800, /* -0.47808 */ in bu27034_fixp_calc_lx()
715 return -EINVAL; in bu27034_fixp_calc_lx()
717 terms[0] = bu27034_fixp_calc_t1(c->A, ch0, ch1, gain0, gain1); in bu27034_fixp_calc_lx()
718 terms[1] = bu27034_fixp_calc_t23(c->B, ch1, gain1); in bu27034_fixp_calc_lx()
719 terms[2] = bu27034_fixp_calc_t23(c->C, ch0, gain0); in bu27034_fixp_calc_lx()
723 if (!c->is_neg[i]) in bu27034_fixp_calc_lx()
732 if (c->is_neg[i]) { in bu27034_fixp_calc_lx()
734 * If the negative term is greater than positive - then in bu27034_fixp_calc_lx()
741 res -= terms[i]; in bu27034_fixp_calc_lx()
754 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL4, &val); in bu27034_has_valid_sample()
756 dev_err(data->dev, "Read failed %d\n", ret); in bu27034_has_valid_sample()
784 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_read_result()
790 ret = regmap_bulk_read(data->regmap, reg[chan], &val, sizeof(val)); in bu27034_read_result()
805 /* Get new value from sensor if data is ready */ in bu27034_get_result_unlocked()
807 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, in bu27034_get_result_unlocked()
814 /* No new data in sensor. Wait and retry */ in bu27034_get_result_unlocked()
818 dev_err(data->dev, "No data from sensor\n"); in bu27034_get_result_unlocked()
820 return -ETIMEDOUT; in bu27034_get_result_unlocked()
834 return regmap_set_bits(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_meas_set()
837 return regmap_clear_bits(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_meas_set()
847 return -EINVAL; in bu27034_get_single_result()
872 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1 / D0 – 1.5) * 0.25 + 1)
874 * lx = (0.001193 * D0 + (-0.0000747) * D1)
891 * easy to spot from the buffers especially if raw-data channels show in bu27034_calc_mlux()
958 dev_err(data->dev, "failed to disable measurement\n"); in bu27034_get_mlux()
980 ret = bu27034_get_gain(data, chan->channel, val); in bu27034_read_raw()
987 return bu27034_get_scale(data, chan->channel, val, val2); in bu27034_read_raw()
993 if (chan->type == IIO_INTENSITY) in bu27034_read_raw()
995 else if (chan->type == IIO_LIGHT) in bu27034_read_raw()
998 return -EINVAL; in bu27034_read_raw()
1005 mutex_lock(&data->mutex); in bu27034_read_raw()
1011 ret = result_get(data, chan->channel, val); in bu27034_read_raw()
1013 mutex_unlock(&data->mutex); in bu27034_read_raw()
1022 return -EINVAL; in bu27034_read_raw()
1038 dev_dbg(data->dev, in bu27034_write_raw_get_fmt()
1039 "HARDWAREGAIN is read-only, use scale to set\n"); in bu27034_write_raw_get_fmt()
1040 return -EINVAL; in bu27034_write_raw_get_fmt()
1042 return -EINVAL; in bu27034_write_raw_get_fmt()
1059 ret = bu27034_set_scale(data, chan->channel, val, val2); in bu27034_write_raw()
1065 ret = -EINVAL; in bu27034_write_raw()
1068 ret = -EINVAL; in bu27034_write_raw()
1085 return iio_gts_avail_times(&data->gts, vals, type, length); in bu27034_read_avail()
1087 return iio_gts_all_avail_scales(&data->gts, vals, type, length); in bu27034_read_avail()
1089 return -EINVAL; in bu27034_read_avail()
1105 ret = regmap_write_bits(data->regmap, BU27034_REG_SYSTEM_CONTROL, in bu27034_chip_init()
1108 return dev_err_probe(data->dev, ret, "Sensor reset failed\n"); in bu27034_chip_init()
1112 ret = regmap_reinit_cache(data->regmap, &bu27034_regmap); in bu27034_chip_init()
1114 dev_err(data->dev, "Failed to reinit reg cache\n"); in bu27034_chip_init()
1120 * this to speed-up the int-time acquisition in the start of the buffer in bu27034_chip_init()
1125 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); in bu27034_chip_init()
1127 dev_err(data->dev, "reading integration time failed\n"); in bu27034_chip_init()
1136 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, in bu27034_wait_for_data()
1141 dev_err(data->dev, "data polling %s\n", in bu27034_wait_for_data()
1147 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, in bu27034_wait_for_data()
1148 &data->scan.channels[0], in bu27034_wait_for_data()
1149 sizeof(data->scan.channels)); in bu27034_wait_for_data()
1169 wait_ms -= BU27034_MEAS_WAIT_PREMATURE_MS; in bu27034_buffer_thread()
1182 if (test_bit(BU27034_CHAN_ALS, idev->active_scan_mask)) { in bu27034_buffer_thread()
1185 ret = bu27034_calc_mlux(data, &data->scan.channels[0], in bu27034_buffer_thread()
1188 dev_err(data->dev, "failed to calculate lux\n"); in bu27034_buffer_thread()
1196 data->scan.mlux = (u32)mlux; in bu27034_buffer_thread()
1198 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp); in bu27034_buffer_thread()
1210 guard(mutex)(&data->mutex); in bu27034_buffer_enable()
1216 "bu27034-buffering-%u", in bu27034_buffer_enable()
1221 data->task = task; in bu27034_buffer_enable()
1230 guard(mutex)(&data->mutex); in bu27034_buffer_disable()
1231 if (data->task) { in bu27034_buffer_disable()
1232 kthread_stop(data->task); in bu27034_buffer_disable()
1233 data->task = NULL; in bu27034_buffer_disable()
1246 struct device *dev = &i2c->dev; in bu27034_probe()
1260 return -ENOMEM; in bu27034_probe()
1270 return dev_err_probe(dev, ret, "Failed to access sensor\n"); in bu27034_probe()
1279 ARRAY_SIZE(bu27034_itimes), &data->gts); in bu27034_probe()
1283 mutex_init(&data->mutex); in bu27034_probe()
1284 data->regmap = regmap; in bu27034_probe()
1285 data->dev = dev; in bu27034_probe()
1287 idev->channels = bu27034_channels; in bu27034_probe()
1288 idev->num_channels = ARRAY_SIZE(bu27034_channels); in bu27034_probe()
1289 idev->name = "bu27034"; in bu27034_probe()
1290 idev->info = &bu27034_info; in bu27034_probe()
1292 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; in bu27034_probe()
1293 idev->available_scan_masks = bu27034_scan_masks; in bu27034_probe()
1319 .name = "bu27034-als",
1329 MODULE_DESCRIPTION("ROHM BU27034 ambient light sensor driver");