1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BU27034ANUC ROHM Ambient Light Sensor 4 * 5 * Copyright (c) 2023, ROHM Semiconductor. 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/bits.h> 10 #include <linux/cleanup.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> 18 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> 23 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) 30 31 #define BU27034_REG_MODE_CONTROL2 0x42 32 #define BU27034_MASK_D01_GAIN GENMASK(7, 3) 33 34 #define BU27034_REG_MODE_CONTROL3 0x43 35 #define BU27034_REG_MODE_CONTROL4 0x44 36 #define BU27034_MASK_MEAS_EN BIT(0) 37 #define BU27034_MASK_VALID BIT(7) 38 #define BU27034_NUM_HW_DATA_CHANS 2 39 #define BU27034_REG_DATA0_LO 0x50 40 #define BU27034_REG_DATA1_LO 0x52 41 #define BU27034_REG_DATA1_HI 0x53 42 #define BU27034_REG_MANUFACTURER_ID 0x92 43 #define BU27034_REG_MAX BU27034_REG_MANUFACTURER_ID 44 45 /* 46 * The BU27034 does not have interrupt to trigger the data read when a 47 * measurement has finished. Hence we poll the VALID bit in a thread. We will 48 * try to wake the thread BU27034_MEAS_WAIT_PREMATURE_MS milliseconds before 49 * the expected sampling time to prevent the drifting. 50 * 51 * If we constantly wake up a bit too late we would eventually skip a sample. 52 * And because the sleep can't wake up _exactly_ at given time this would be 53 * inevitable even if the sensor clock would be perfectly phase-locked to CPU 54 * clock - which we can't say is the case. 55 * 56 * This is still fragile. No matter how big advance do we have, we will still 57 * risk of losing a sample because things can in a rainy-day scenario be 58 * delayed a lot. Yet, more we reserve the time for polling, more we also lose 59 * the performance by spending cycles polling the register. So, selecting this 60 * value is a balancing dance between severity of wasting CPU time and severity 61 * of losing samples. 62 * 63 * In most cases losing the samples is not _that_ crucial because light levels 64 * tend to change slowly. 65 * 66 * Other option that was pointed to me would be always sleeping 1/2 of the 67 * measurement time, checking the VALID bit and just sleeping again if the bit 68 * was not set. That should be pretty tolerant against missing samples due to 69 * the scheduling delays while also not wasting much of cycles for polling. 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 77 * without having a sample ready). 78 */ 79 #define BU27034_MEAS_WAIT_PREMATURE_MS 5 80 #define BU27034_DATA_WAIT_TIME_US 1000 81 #define BU27034_TOTAL_DATA_WAIT_TIME_US (BU27034_MEAS_WAIT_PREMATURE_MS * 1000) 82 83 #define BU27034_RETRY_LIMIT 18 84 85 enum { 86 BU27034_CHAN_ALS, 87 BU27034_CHAN_DATA0, 88 BU27034_CHAN_DATA1, 89 BU27034_NUM_CHANS 90 }; 91 92 static const unsigned long bu27034_scan_masks[] = { 93 GENMASK(BU27034_CHAN_DATA1, BU27034_CHAN_DATA0), 94 GENMASK(BU27034_CHAN_DATA1, BU27034_CHAN_ALS), 0 95 }; 96 97 /* 98 * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS 99 * Time impacts to gain: 1x, 2x, 4x, 8x. 100 * 101 * => Max total gain is HWGAIN * gain by integration time (8 * 1024) = 8192 102 * if 1x gain is scale 1, scale for 2x gain is 0.5, 4x => 0.25, 103 * ... 8192x => 0.0001220703125 => 122070.3125 nanos 104 * 105 * Using NANO precision for scale, we must use scale 16x corresponding gain 1x 106 * to avoid precision loss. (8x would result scale 976 562.5(nanos). 107 */ 108 #define BU27034_SCALE_1X 16 109 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_32X 0x0b /* 01011 */ 114 #define BU27034_GSEL_256X 0x18 /* 11000 */ 115 #define BU27034_GSEL_512X 0x19 /* 11001 */ 116 #define BU27034_GSEL_1024X 0x1a /* 11010 */ 117 118 /* Available gain settings */ 119 static const struct iio_gain_sel_pair bu27034_gains[] = { 120 GAIN_SCALE_GAIN(1, BU27034_GSEL_1X), 121 GAIN_SCALE_GAIN(4, BU27034_GSEL_4X), 122 GAIN_SCALE_GAIN(32, BU27034_GSEL_32X), 123 GAIN_SCALE_GAIN(256, BU27034_GSEL_256X), 124 GAIN_SCALE_GAIN(512, BU27034_GSEL_512X), 125 GAIN_SCALE_GAIN(1024, BU27034_GSEL_1024X), 126 }; 127 128 /* 129 * Measurement modes are 55, 100, 200 and 400 mS modes - which do have direct 130 * multiplying impact to the data register values (similar to gain). 131 * 132 * This means that if meas-mode is changed for example from 400 => 200, 133 * the scale is doubled. Eg, time impact to total gain is x1, x2, x4, x8. 134 */ 135 #define BU27034_MEAS_MODE_100MS 0 136 #define BU27034_MEAS_MODE_55MS 1 137 #define BU27034_MEAS_MODE_200MS 2 138 #define BU27034_MEAS_MODE_400MS 4 139 140 static const struct iio_itime_sel_mul bu27034_itimes[] = { 141 GAIN_SCALE_ITIME_US(400000, BU27034_MEAS_MODE_400MS, 8), 142 GAIN_SCALE_ITIME_US(200000, BU27034_MEAS_MODE_200MS, 4), 143 GAIN_SCALE_ITIME_US(100000, BU27034_MEAS_MODE_100MS, 2), 144 GAIN_SCALE_ITIME_US(55000, BU27034_MEAS_MODE_55MS, 1), 145 }; 146 147 #define BU27034_CHAN_DATA(_name) \ 148 { \ 149 .type = IIO_INTENSITY, \ 150 .channel = BU27034_CHAN_##_name, \ 151 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 152 BIT(IIO_CHAN_INFO_SCALE) | \ 153 BIT(IIO_CHAN_INFO_HARDWAREGAIN), \ 154 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), \ 155 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \ 156 .info_mask_shared_by_all_available = \ 157 BIT(IIO_CHAN_INFO_INT_TIME), \ 158 .address = BU27034_REG_##_name##_LO, \ 159 .scan_index = BU27034_CHAN_##_name, \ 160 .scan_type = { \ 161 .sign = 'u', \ 162 .realbits = 16, \ 163 .storagebits = 16, \ 164 .endianness = IIO_LE, \ 165 }, \ 166 .indexed = 1, \ 167 } 168 169 static const struct iio_chan_spec bu27034_channels[] = { 170 { 171 .type = IIO_LIGHT, 172 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 173 BIT(IIO_CHAN_INFO_SCALE), 174 .channel = BU27034_CHAN_ALS, 175 .scan_index = BU27034_CHAN_ALS, 176 .scan_type = { 177 .sign = 'u', 178 .realbits = 32, 179 .storagebits = 32, 180 .endianness = IIO_CPU, 181 }, 182 }, 183 /* 184 * The BU27034 DATA0 and DATA1 channels are both on the visible light 185 * area (mostly). The data0 sensitivity peaks at 500nm, DATA1 at 600nm. 186 * These wave lengths are cyan(ish) and orange(ish), making these 187 * sub-optiomal candidates for R/G/B standardization. Hence the 188 * colour modifier is omitted. 189 */ 190 BU27034_CHAN_DATA(DATA0), 191 BU27034_CHAN_DATA(DATA1), 192 IIO_CHAN_SOFT_TIMESTAMP(4), 193 }; 194 195 struct bu27034_data { 196 struct regmap *regmap; 197 struct device *dev; 198 /* 199 * Protect gain and time during scale adjustment and data reading. 200 * Protect measurement enabling/disabling. 201 */ 202 struct mutex mutex; 203 struct iio_gts gts; 204 struct task_struct *task; 205 __le16 raw[BU27034_NUM_HW_DATA_CHANS]; 206 struct { 207 u32 mlux; 208 __le16 channels[BU27034_NUM_HW_DATA_CHANS]; 209 aligned_s64 ts; 210 } scan; 211 }; 212 213 static const struct regmap_range bu27034_volatile_ranges[] = { 214 { 215 .range_min = BU27034_REG_SYSTEM_CONTROL, 216 .range_max = BU27034_REG_SYSTEM_CONTROL, 217 }, { 218 .range_min = BU27034_REG_MODE_CONTROL4, 219 .range_max = BU27034_REG_MODE_CONTROL4, 220 }, { 221 .range_min = BU27034_REG_DATA0_LO, 222 .range_max = BU27034_REG_DATA1_HI, 223 }, 224 }; 225 226 static const struct regmap_access_table bu27034_volatile_regs = { 227 .yes_ranges = &bu27034_volatile_ranges[0], 228 .n_yes_ranges = ARRAY_SIZE(bu27034_volatile_ranges), 229 }; 230 231 static const struct regmap_range bu27034_read_only_ranges[] = { 232 { 233 .range_min = BU27034_REG_DATA0_LO, 234 .range_max = BU27034_REG_DATA1_HI, 235 }, { 236 .range_min = BU27034_REG_MANUFACTURER_ID, 237 .range_max = BU27034_REG_MANUFACTURER_ID, 238 } 239 }; 240 241 static const struct regmap_access_table bu27034_ro_regs = { 242 .no_ranges = &bu27034_read_only_ranges[0], 243 .n_no_ranges = ARRAY_SIZE(bu27034_read_only_ranges), 244 }; 245 246 static const struct regmap_config bu27034_regmap = { 247 .reg_bits = 8, 248 .val_bits = 8, 249 .max_register = BU27034_REG_MAX, 250 .cache_type = REGCACHE_RBTREE, 251 .volatile_table = &bu27034_volatile_regs, 252 .wr_table = &bu27034_ro_regs, 253 }; 254 255 struct bu27034_gain_check { 256 int old_gain; 257 int new_gain; 258 int chan; 259 }; 260 261 static int bu27034_get_gain_sel(struct bu27034_data *data, int chan) 262 { 263 int reg[] = { 264 [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2, 265 [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3, 266 }; 267 int ret, val; 268 269 ret = regmap_read(data->regmap, reg[chan], &val); 270 if (ret) 271 return ret; 272 273 return FIELD_GET(BU27034_MASK_D01_GAIN, val); 274 } 275 276 static int bu27034_get_gain(struct bu27034_data *data, int chan, int *gain) 277 { 278 int ret, sel; 279 280 ret = bu27034_get_gain_sel(data, chan); 281 if (ret < 0) 282 return ret; 283 284 sel = ret; 285 286 ret = iio_gts_find_gain_by_sel(&data->gts, sel); 287 if (ret < 0) { 288 dev_err(data->dev, "chan %u: unknown gain value 0x%x\n", chan, 289 sel); 290 291 return ret; 292 } 293 294 *gain = ret; 295 296 return 0; 297 } 298 299 static int bu27034_get_int_time(struct bu27034_data *data) 300 { 301 int ret, sel; 302 303 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); 304 if (ret) 305 return ret; 306 307 return iio_gts_find_int_time_by_sel(&data->gts, 308 sel & BU27034_MASK_MEAS_MODE); 309 } 310 311 static int _bu27034_get_scale(struct bu27034_data *data, int channel, int *val, 312 int *val2) 313 { 314 int gain, ret; 315 316 ret = bu27034_get_gain(data, channel, &gain); 317 if (ret) 318 return ret; 319 320 ret = bu27034_get_int_time(data); 321 if (ret < 0) 322 return ret; 323 324 return iio_gts_get_scale(&data->gts, gain, ret, val, val2); 325 } 326 327 static int bu27034_get_scale(struct bu27034_data *data, int channel, int *val, 328 int *val2) 329 { 330 int ret; 331 332 if (channel == BU27034_CHAN_ALS) { 333 *val = 0; 334 *val2 = 1000; 335 return IIO_VAL_INT_PLUS_MICRO; 336 } 337 338 mutex_lock(&data->mutex); 339 ret = _bu27034_get_scale(data, channel, val, val2); 340 mutex_unlock(&data->mutex); 341 if (ret) 342 return ret; 343 344 return IIO_VAL_INT_PLUS_NANO; 345 } 346 347 /* Caller should hold the lock to protect lux reading */ 348 static int bu27034_write_gain_sel(struct bu27034_data *data, int chan, int sel) 349 { 350 static const int reg[] = { 351 [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2, 352 [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3, 353 }; 354 int mask, val; 355 356 val = FIELD_PREP(BU27034_MASK_D01_GAIN, sel); 357 mask = BU27034_MASK_D01_GAIN; 358 359 return regmap_update_bits(data->regmap, reg[chan], mask, val); 360 } 361 362 static int bu27034_set_gain(struct bu27034_data *data, int chan, int gain) 363 { 364 int ret; 365 366 ret = iio_gts_find_sel_by_gain(&data->gts, gain); 367 if (ret < 0) 368 return ret; 369 370 return bu27034_write_gain_sel(data, chan, ret); 371 } 372 373 /* Caller should hold the lock to protect data->int_time */ 374 static int bu27034_set_int_time(struct bu27034_data *data, int time) 375 { 376 int ret; 377 378 ret = iio_gts_find_sel_by_int_time(&data->gts, time); 379 if (ret < 0) 380 return ret; 381 382 return regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, 383 BU27034_MASK_MEAS_MODE, ret); 384 } 385 386 /* 387 * We try to change the time in such way that the scale is maintained for 388 * given channels by adjusting gain so that it compensates the time change. 389 */ 390 static int bu27034_try_set_int_time(struct bu27034_data *data, int time_us) 391 { 392 struct bu27034_gain_check gains[] = { 393 { .chan = BU27034_CHAN_DATA0 }, 394 { .chan = BU27034_CHAN_DATA1 }, 395 }; 396 int numg = ARRAY_SIZE(gains); 397 int ret, int_time_old, i; 398 399 guard(mutex)(&data->mutex); 400 ret = bu27034_get_int_time(data); 401 if (ret < 0) 402 return ret; 403 404 int_time_old = ret; 405 406 if (!iio_gts_valid_time(&data->gts, time_us)) { 407 dev_err(data->dev, "Unsupported integration time %u\n", 408 time_us); 409 return -EINVAL; 410 } 411 412 if (time_us == int_time_old) 413 return 0; 414 415 for (i = 0; i < numg; i++) { 416 ret = bu27034_get_gain(data, gains[i].chan, &gains[i].old_gain); 417 if (ret) 418 return 0; 419 420 ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, 421 gains[i].old_gain, 422 int_time_old, time_us, 423 &gains[i].new_gain); 424 if (ret) { 425 int scale1, scale2; 426 bool ok; 427 428 _bu27034_get_scale(data, gains[i].chan, &scale1, &scale2); 429 dev_dbg(data->dev, 430 "chan %u, can't support time %u with scale %u %u\n", 431 gains[i].chan, time_us, scale1, scale2); 432 433 if (gains[i].new_gain < 0) 434 return ret; 435 436 /* 437 * If caller requests for integration time change and we 438 * can't support the scale - then the caller should be 439 * prepared to 'pick up the pieces and deal with the 440 * fact that the scale changed'. 441 */ 442 ret = iio_find_closest_gain_low(&data->gts, 443 gains[i].new_gain, &ok); 444 445 if (!ok) 446 dev_dbg(data->dev, 447 "optimal gain out of range for chan %u\n", 448 gains[i].chan); 449 450 if (ret < 0) { 451 dev_dbg(data->dev, 452 "Total gain increase. Risk of saturation"); 453 ret = iio_gts_get_min_gain(&data->gts); 454 if (ret < 0) 455 return ret; 456 } 457 dev_dbg(data->dev, "chan %u scale changed\n", 458 gains[i].chan); 459 gains[i].new_gain = ret; 460 dev_dbg(data->dev, "chan %u new gain %u\n", 461 gains[i].chan, gains[i].new_gain); 462 } 463 } 464 465 for (i = 0; i < numg; i++) { 466 ret = bu27034_set_gain(data, gains[i].chan, gains[i].new_gain); 467 if (ret) 468 return ret; 469 } 470 471 return bu27034_set_int_time(data, time_us); 472 } 473 474 static int bu27034_set_scale(struct bu27034_data *data, int chan, 475 int val, int val2) 476 { 477 int ret, time_sel, gain_sel, i; 478 bool found = false; 479 480 if (chan == BU27034_CHAN_ALS) { 481 if (val == 0 && val2 == 1000000) 482 return 0; 483 484 return -EINVAL; 485 } 486 487 guard(mutex)(&data->mutex); 488 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &time_sel); 489 if (ret) 490 return ret; 491 492 ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel, 493 val, val2, &gain_sel); 494 if (ret) { 495 /* 496 * Could not support scale with given time. Need to change time. 497 * We still want to maintain the scale for all channels 498 */ 499 struct bu27034_gain_check gain; 500 int new_time_sel; 501 502 /* 503 * Populate information for the other channel which should also 504 * maintain the scale. 505 */ 506 if (chan == BU27034_CHAN_DATA0) 507 gain.chan = BU27034_CHAN_DATA1; 508 else if (chan == BU27034_CHAN_DATA1) 509 gain.chan = BU27034_CHAN_DATA0; 510 511 ret = bu27034_get_gain(data, gain.chan, &gain.old_gain); 512 if (ret) 513 return ret; 514 515 /* 516 * Iterate through all the times to see if we find one which 517 * can support requested scale for requested channel, while 518 * maintaining the scale for the other channel 519 */ 520 for (i = 0; i < data->gts.num_itime; i++) { 521 new_time_sel = data->gts.itime_table[i].sel; 522 523 if (new_time_sel == time_sel) 524 continue; 525 526 /* Can we provide requested scale with this time? */ 527 ret = iio_gts_find_gain_sel_for_scale_using_time( 528 &data->gts, new_time_sel, val, val2, 529 &gain_sel); 530 if (ret) 531 continue; 532 533 /* Can the other channel maintain scale? */ 534 ret = iio_gts_find_new_gain_sel_by_old_gain_time( 535 &data->gts, gain.old_gain, time_sel, 536 new_time_sel, &gain.new_gain); 537 if (!ret) { 538 /* Yes - we found suitable time */ 539 found = true; 540 break; 541 } 542 } 543 if (!found) { 544 dev_dbg(data->dev, 545 "Can't set scale maintaining other channel\n"); 546 return -EINVAL; 547 } 548 549 ret = bu27034_set_gain(data, gain.chan, gain.new_gain); 550 if (ret) 551 return ret; 552 553 ret = regmap_update_bits(data->regmap, BU27034_REG_MODE_CONTROL1, 554 BU27034_MASK_MEAS_MODE, new_time_sel); 555 if (ret) 556 return ret; 557 } 558 559 return bu27034_write_gain_sel(data, chan, gain_sel); 560 } 561 562 /* 563 * for (D1/D0 < 1.5): 564 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1/D0 – 1.5) * (0.25) + 1) 565 * 566 * => -0.000745625 * D0 + 0.0002515625 * D1 + -0.000018675 * D1 * D1 / D0 567 * 568 * => (6.44 * ch1 / gain1 + 19.088 * ch0 / gain0 - 569 * 0.47808 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0) / 570 * mt 571 * 572 * Else 573 * lx = 0.001193 * D0 - 0.0000747 * D1 574 * 575 * => (1.91232 * ch1 / gain1 + 30.5408 * ch0 / gain0 + 576 * [0 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0] ) / 577 * mt 578 * 579 * This can be unified to format: 580 * lx = [ 581 * A * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) + 582 * B * ch1 / gain1 + 583 * C * ch0 / gain0 584 * ] / mt 585 * 586 * For case 1: 587 * A = -0.47808, 588 * B = 6.44, 589 * C = 19.088 590 * 591 * For case 2: 592 * A = 0 593 * B = 1.91232 594 * C = 30.5408 595 */ 596 597 struct bu27034_lx_coeff { 598 unsigned int A; 599 unsigned int B; 600 unsigned int C; 601 /* Indicate which of the coefficients above are negative */ 602 bool is_neg[3]; 603 }; 604 605 static inline u64 gain_mul_div_helper(u64 val, unsigned int gain, 606 unsigned int div) 607 { 608 /* 609 * Max gain for a channel is 4096. The max u64 (0xffffffffffffffffULL) 610 * divided by 4096 is 0xFFFFFFFFFFFFF (GENMASK_ULL(51, 0)) (floored). 611 * Thus, the 0xFFFFFFFFFFFFF is the largest value we can safely multiply 612 * with the gain, no matter what gain is set. 613 * 614 * So, multiplication with max gain may overflow if val is greater than 615 * 0xFFFFFFFFFFFFF (52 bits set).. 616 * 617 * If this is the case we divide first. 618 */ 619 if (val < GENMASK_ULL(51, 0)) { 620 val *= gain; 621 do_div(val, div); 622 } else { 623 do_div(val, div); 624 val *= gain; 625 } 626 627 return val; 628 } 629 630 static u64 bu27034_fixp_calc_t1_64bit(unsigned int coeff, unsigned int ch0, 631 unsigned int ch1, unsigned int gain0, 632 unsigned int gain1) 633 { 634 unsigned int helper; 635 u64 helper64; 636 637 helper64 = (u64)coeff * (u64)ch1 * (u64)ch1; 638 639 helper = gain1 * gain1; 640 if (helper > ch0) { 641 do_div(helper64, helper); 642 643 return gain_mul_div_helper(helper64, gain0, ch0); 644 } 645 646 do_div(helper64, ch0); 647 648 return gain_mul_div_helper(helper64, gain0, helper); 649 650 } 651 652 static u64 bu27034_fixp_calc_t1(unsigned int coeff, unsigned int ch0, 653 unsigned int ch1, unsigned int gain0, 654 unsigned int gain1) 655 { 656 unsigned int helper, tmp; 657 658 /* 659 * Here we could overflow even the 64bit value. Hence we 660 * multiply with gain0 only after the divisions - even though 661 * it may result loss of accuracy 662 */ 663 helper = coeff * ch1 * ch1; 664 tmp = helper * gain0; 665 666 helper = ch1 * ch1; 667 668 if (check_mul_overflow(helper, coeff, &helper)) 669 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1); 670 671 if (check_mul_overflow(helper, gain0, &tmp)) 672 return bu27034_fixp_calc_t1_64bit(coeff, ch0, ch1, gain0, gain1); 673 674 return tmp / (gain1 * gain1) / ch0; 675 676 } 677 678 static u64 bu27034_fixp_calc_t23(unsigned int coeff, unsigned int ch, 679 unsigned int gain) 680 { 681 unsigned int helper; 682 u64 helper64; 683 684 if (!check_mul_overflow(coeff, ch, &helper)) 685 return helper / gain; 686 687 helper64 = (u64)coeff * (u64)ch; 688 do_div(helper64, gain); 689 690 return helper64; 691 } 692 693 static int bu27034_fixp_calc_lx(unsigned int ch0, unsigned int ch1, 694 unsigned int gain0, unsigned int gain1, 695 unsigned int meastime, int coeff_idx) 696 { 697 static const struct bu27034_lx_coeff coeff[] = { 698 { 699 .A = 4780800, /* -0.47808 */ 700 .B = 64400000, /* 6.44 */ 701 .C = 190880000, /* 19.088 */ 702 .is_neg = { true, false, false }, 703 }, { 704 .A = 0, /* 0 */ 705 .B = 19123200, /* 1.91232 */ 706 .C = 305408000, /* 30.5408 */ 707 /* All terms positive */ 708 }, 709 }; 710 const struct bu27034_lx_coeff *c = &coeff[coeff_idx]; 711 u64 res = 0, terms[3]; 712 int i; 713 714 if (coeff_idx >= ARRAY_SIZE(coeff)) 715 return -EINVAL; 716 717 terms[0] = bu27034_fixp_calc_t1(c->A, ch0, ch1, gain0, gain1); 718 terms[1] = bu27034_fixp_calc_t23(c->B, ch1, gain1); 719 terms[2] = bu27034_fixp_calc_t23(c->C, ch0, gain0); 720 721 /* First, add positive terms */ 722 for (i = 0; i < 3; i++) 723 if (!c->is_neg[i]) 724 res += terms[i]; 725 726 /* No positive term => zero lux */ 727 if (!res) 728 return 0; 729 730 /* Then, subtract negative terms (if any) */ 731 for (i = 0; i < 3; i++) 732 if (c->is_neg[i]) { 733 /* 734 * If the negative term is greater than positive - then 735 * the darkness has taken over and we are all doomed! Eh, 736 * I mean, then we can just return 0 lx and go out 737 */ 738 if (terms[i] >= res) 739 return 0; 740 741 res -= terms[i]; 742 } 743 744 meastime *= 10; 745 do_div(res, meastime); 746 747 return (int) res; 748 } 749 750 static bool bu27034_has_valid_sample(struct bu27034_data *data) 751 { 752 int ret, val; 753 754 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL4, &val); 755 if (ret) { 756 dev_err(data->dev, "Read failed %d\n", ret); 757 758 return false; 759 } 760 761 return val & BU27034_MASK_VALID; 762 } 763 764 /* 765 * Reading the register where VALID bit is clears this bit. (So does changing 766 * any gain / integration time configuration registers) The bit gets 767 * set when we have acquired new data. We use this bit to indicate data 768 * validity. 769 */ 770 static void bu27034_invalidate_read_data(struct bu27034_data *data) 771 { 772 bu27034_has_valid_sample(data); 773 } 774 775 static int bu27034_read_result(struct bu27034_data *data, int chan, int *res) 776 { 777 int reg[] = { 778 [BU27034_CHAN_DATA0] = BU27034_REG_DATA0_LO, 779 [BU27034_CHAN_DATA1] = BU27034_REG_DATA1_LO, 780 }; 781 int valid, ret; 782 __le16 val; 783 784 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, 785 valid, (valid & BU27034_MASK_VALID), 786 BU27034_DATA_WAIT_TIME_US, 0); 787 if (ret) 788 return ret; 789 790 ret = regmap_bulk_read(data->regmap, reg[chan], &val, sizeof(val)); 791 if (ret) 792 return ret; 793 794 *res = le16_to_cpu(val); 795 796 return 0; 797 } 798 799 static int bu27034_get_result_unlocked(struct bu27034_data *data, __le16 *res, 800 int size) 801 { 802 int ret = 0, retry_cnt = 0; 803 804 retry: 805 /* Get new value from sensor if data is ready */ 806 if (bu27034_has_valid_sample(data)) { 807 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, 808 res, size); 809 if (ret) 810 return ret; 811 812 bu27034_invalidate_read_data(data); 813 } else { 814 /* No new data in sensor. Wait and retry */ 815 retry_cnt++; 816 817 if (retry_cnt > BU27034_RETRY_LIMIT) { 818 dev_err(data->dev, "No data from sensor\n"); 819 820 return -ETIMEDOUT; 821 } 822 823 msleep(25); 824 825 goto retry; 826 } 827 828 return ret; 829 } 830 831 static int bu27034_meas_set(struct bu27034_data *data, bool en) 832 { 833 if (en) 834 return regmap_set_bits(data->regmap, BU27034_REG_MODE_CONTROL4, 835 BU27034_MASK_MEAS_EN); 836 837 return regmap_clear_bits(data->regmap, BU27034_REG_MODE_CONTROL4, 838 BU27034_MASK_MEAS_EN); 839 } 840 841 static int bu27034_get_single_result(struct bu27034_data *data, int chan, 842 int *val) 843 { 844 int ret; 845 846 if (chan < BU27034_CHAN_DATA0 || chan > BU27034_CHAN_DATA1) 847 return -EINVAL; 848 849 ret = bu27034_meas_set(data, true); 850 if (ret) 851 return ret; 852 853 ret = bu27034_get_int_time(data); 854 if (ret < 0) 855 return ret; 856 857 msleep(ret / 1000); 858 859 return bu27034_read_result(data, chan, val); 860 } 861 862 /* 863 * The formula given by vendor for computing luxes out of data0 and data1 864 * (in open air) is as follows: 865 * 866 * Let's mark: 867 * D0 = data0/ch0_gain/meas_time_ms * 25600 868 * D1 = data1/ch1_gain/meas_time_ms * 25600 869 * 870 * Then: 871 * If (D1/D0 < 1.5) 872 * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1 / D0 – 1.5) * 0.25 + 1) 873 * Else 874 * lx = (0.001193 * D0 + (-0.0000747) * D1) 875 * 876 * We use it here. Users who have for example some colored lens 877 * need to modify the calculation but I hope this gives a starting point for 878 * those working with such devices. 879 */ 880 881 static int bu27034_calc_mlux(struct bu27034_data *data, __le16 *res, int *val) 882 { 883 unsigned int gain0, gain1, meastime; 884 unsigned int d1_d0_ratio_scaled; 885 u16 ch0, ch1; 886 u64 helper64; 887 int ret; 888 889 /* 890 * We return 0 lux if calculation fails. This should be reasonably 891 * easy to spot from the buffers especially if raw-data channels show 892 * valid values 893 */ 894 *val = 0; 895 896 ch0 = max_t(u16, 1, le16_to_cpu(res[0])); 897 ch1 = max_t(u16, 1, le16_to_cpu(res[1])); 898 899 ret = bu27034_get_gain(data, BU27034_CHAN_DATA0, &gain0); 900 if (ret) 901 return ret; 902 903 ret = bu27034_get_gain(data, BU27034_CHAN_DATA1, &gain1); 904 if (ret) 905 return ret; 906 907 ret = bu27034_get_int_time(data); 908 if (ret < 0) 909 return ret; 910 911 meastime = ret; 912 913 d1_d0_ratio_scaled = (unsigned int)ch1 * (unsigned int)gain0 * 100; 914 helper64 = (u64)ch1 * (u64)gain0 * 100LLU; 915 916 if (helper64 != d1_d0_ratio_scaled) { 917 unsigned int div = (unsigned int)ch0 * gain1; 918 919 do_div(helper64, div); 920 d1_d0_ratio_scaled = helper64; 921 } else { 922 d1_d0_ratio_scaled /= ch0 * gain1; 923 } 924 925 if (d1_d0_ratio_scaled < 150) 926 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 0); 927 else 928 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 1); 929 930 if (ret < 0) 931 return ret; 932 933 *val = ret; 934 935 return 0; 936 937 } 938 939 static int bu27034_get_mlux(struct bu27034_data *data, int chan, int *val) 940 { 941 __le16 res[BU27034_NUM_HW_DATA_CHANS]; 942 int ret; 943 944 ret = bu27034_meas_set(data, true); 945 if (ret) 946 return ret; 947 948 ret = bu27034_get_result_unlocked(data, &res[0], sizeof(res)); 949 if (ret) 950 return ret; 951 952 ret = bu27034_calc_mlux(data, res, val); 953 if (ret) 954 return ret; 955 956 ret = bu27034_meas_set(data, false); 957 if (ret) 958 dev_err(data->dev, "failed to disable measurement\n"); 959 960 return 0; 961 } 962 963 static int bu27034_read_raw(struct iio_dev *idev, 964 struct iio_chan_spec const *chan, 965 int *val, int *val2, long mask) 966 { 967 struct bu27034_data *data = iio_priv(idev); 968 int ret; 969 970 switch (mask) { 971 case IIO_CHAN_INFO_INT_TIME: 972 *val = 0; 973 *val2 = bu27034_get_int_time(data); 974 if (*val2 < 0) 975 return *val2; 976 977 return IIO_VAL_INT_PLUS_MICRO; 978 979 case IIO_CHAN_INFO_HARDWAREGAIN: 980 ret = bu27034_get_gain(data, chan->channel, val); 981 if (ret) 982 return ret; 983 984 return IIO_VAL_INT; 985 986 case IIO_CHAN_INFO_SCALE: 987 return bu27034_get_scale(data, chan->channel, val, val2); 988 989 case IIO_CHAN_INFO_RAW: 990 { 991 int (*result_get)(struct bu27034_data *data, int chan, int *val); 992 993 if (chan->type == IIO_INTENSITY) 994 result_get = bu27034_get_single_result; 995 else if (chan->type == IIO_LIGHT) 996 result_get = bu27034_get_mlux; 997 else 998 return -EINVAL; 999 1000 /* Don't mess with measurement enabling while buffering */ 1001 if (!iio_device_claim_direct(idev)) 1002 return -EBUSY; 1003 1004 mutex_lock(&data->mutex); 1005 /* 1006 * Reading one channel at a time is inefficient but we 1007 * don't care here. Buffered version should be used if 1008 * performance is an issue. 1009 */ 1010 ret = result_get(data, chan->channel, val); 1011 1012 mutex_unlock(&data->mutex); 1013 iio_device_release_direct(idev); 1014 1015 if (ret) 1016 return ret; 1017 1018 return IIO_VAL_INT; 1019 } 1020 default: 1021 return -EINVAL; 1022 } 1023 } 1024 1025 static int bu27034_write_raw_get_fmt(struct iio_dev *indio_dev, 1026 struct iio_chan_spec const *chan, 1027 long mask) 1028 { 1029 struct bu27034_data *data = iio_priv(indio_dev); 1030 1031 switch (mask) { 1032 case IIO_CHAN_INFO_SCALE: 1033 return IIO_VAL_INT_PLUS_NANO; 1034 case IIO_CHAN_INFO_INT_TIME: 1035 return IIO_VAL_INT_PLUS_MICRO; 1036 case IIO_CHAN_INFO_HARDWAREGAIN: 1037 dev_dbg(data->dev, 1038 "HARDWAREGAIN is read-only, use scale to set\n"); 1039 return -EINVAL; 1040 default: 1041 return -EINVAL; 1042 } 1043 } 1044 1045 static int bu27034_write_raw(struct iio_dev *idev, 1046 struct iio_chan_spec const *chan, 1047 int val, int val2, long mask) 1048 { 1049 struct bu27034_data *data = iio_priv(idev); 1050 int ret; 1051 1052 if (!iio_device_claim_direct(idev)) 1053 return -EBUSY; 1054 1055 switch (mask) { 1056 case IIO_CHAN_INFO_SCALE: 1057 ret = bu27034_set_scale(data, chan->channel, val, val2); 1058 break; 1059 case IIO_CHAN_INFO_INT_TIME: 1060 if (!val) 1061 ret = bu27034_try_set_int_time(data, val2); 1062 else 1063 ret = -EINVAL; 1064 break; 1065 default: 1066 ret = -EINVAL; 1067 break; 1068 } 1069 1070 iio_device_release_direct(idev); 1071 1072 return ret; 1073 } 1074 1075 static int bu27034_read_avail(struct iio_dev *idev, 1076 struct iio_chan_spec const *chan, const int **vals, 1077 int *type, int *length, long mask) 1078 { 1079 struct bu27034_data *data = iio_priv(idev); 1080 1081 switch (mask) { 1082 case IIO_CHAN_INFO_INT_TIME: 1083 return iio_gts_avail_times(&data->gts, vals, type, length); 1084 case IIO_CHAN_INFO_SCALE: 1085 return iio_gts_all_avail_scales(&data->gts, vals, type, length); 1086 default: 1087 return -EINVAL; 1088 } 1089 } 1090 1091 static const struct iio_info bu27034_info = { 1092 .read_raw = &bu27034_read_raw, 1093 .write_raw = &bu27034_write_raw, 1094 .write_raw_get_fmt = &bu27034_write_raw_get_fmt, 1095 .read_avail = &bu27034_read_avail, 1096 }; 1097 1098 static int bu27034_chip_init(struct bu27034_data *data) 1099 { 1100 int ret, sel; 1101 1102 /* Reset */ 1103 ret = regmap_write_bits(data->regmap, BU27034_REG_SYSTEM_CONTROL, 1104 BU27034_MASK_SW_RESET, BU27034_MASK_SW_RESET); 1105 if (ret) 1106 return dev_err_probe(data->dev, ret, "Sensor reset failed\n"); 1107 1108 msleep(1); 1109 1110 ret = regmap_reinit_cache(data->regmap, &bu27034_regmap); 1111 if (ret) { 1112 dev_err(data->dev, "Failed to reinit reg cache\n"); 1113 return ret; 1114 } 1115 1116 /* 1117 * Read integration time here to ensure it is in regmap cache. We do 1118 * this to speed-up the int-time acquisition in the start of the buffer 1119 * handling thread where longer delays could make it more likely we end 1120 * up skipping a sample, and where the longer delays make timestamps 1121 * less accurate. 1122 */ 1123 ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL1, &sel); 1124 if (ret) 1125 dev_err(data->dev, "reading integration time failed\n"); 1126 1127 return 0; 1128 } 1129 1130 static int bu27034_wait_for_data(struct bu27034_data *data) 1131 { 1132 int ret, val; 1133 1134 ret = regmap_read_poll_timeout(data->regmap, BU27034_REG_MODE_CONTROL4, 1135 val, val & BU27034_MASK_VALID, 1136 BU27034_DATA_WAIT_TIME_US, 1137 BU27034_TOTAL_DATA_WAIT_TIME_US); 1138 if (ret) { 1139 dev_err(data->dev, "data polling %s\n", 1140 !(val & BU27034_MASK_VALID) ? "timeout" : "fail"); 1141 1142 return ret; 1143 } 1144 1145 ret = regmap_bulk_read(data->regmap, BU27034_REG_DATA0_LO, 1146 &data->scan.channels[0], 1147 sizeof(data->scan.channels)); 1148 if (ret) 1149 return ret; 1150 1151 bu27034_invalidate_read_data(data); 1152 1153 return 0; 1154 } 1155 1156 static int bu27034_buffer_thread(void *arg) 1157 { 1158 struct iio_dev *idev = arg; 1159 struct bu27034_data *data; 1160 int wait_ms; 1161 1162 data = iio_priv(idev); 1163 1164 wait_ms = bu27034_get_int_time(data); 1165 wait_ms /= 1000; 1166 1167 wait_ms -= BU27034_MEAS_WAIT_PREMATURE_MS; 1168 1169 while (!kthread_should_stop()) { 1170 int ret; 1171 int64_t tstamp; 1172 1173 msleep(wait_ms); 1174 ret = bu27034_wait_for_data(data); 1175 if (ret) 1176 continue; 1177 1178 tstamp = iio_get_time_ns(idev); 1179 1180 if (test_bit(BU27034_CHAN_ALS, idev->active_scan_mask)) { 1181 int mlux; 1182 1183 ret = bu27034_calc_mlux(data, &data->scan.channels[0], 1184 &mlux); 1185 if (ret) 1186 dev_err(data->dev, "failed to calculate lux\n"); 1187 1188 /* 1189 * The maximum Milli lux value we get with gain 1x time 1190 * 55mS data ch0 = 0xffff ch1 = 0xffff fits in 26 bits 1191 * so there should be no problem returning int from 1192 * computations and casting it to u32 1193 */ 1194 data->scan.mlux = (u32)mlux; 1195 } 1196 iio_push_to_buffers_with_timestamp(idev, &data->scan, tstamp); 1197 } 1198 1199 return 0; 1200 } 1201 1202 static int bu27034_buffer_enable(struct iio_dev *idev) 1203 { 1204 struct bu27034_data *data = iio_priv(idev); 1205 struct task_struct *task; 1206 int ret; 1207 1208 guard(mutex)(&data->mutex); 1209 ret = bu27034_meas_set(data, true); 1210 if (ret) 1211 return ret; 1212 1213 task = kthread_run(bu27034_buffer_thread, idev, 1214 "bu27034-buffering-%u", 1215 iio_device_id(idev)); 1216 if (IS_ERR(task)) 1217 return PTR_ERR(task); 1218 1219 data->task = task; 1220 1221 return 0; 1222 } 1223 1224 static int bu27034_buffer_disable(struct iio_dev *idev) 1225 { 1226 struct bu27034_data *data = iio_priv(idev); 1227 1228 guard(mutex)(&data->mutex); 1229 if (data->task) { 1230 kthread_stop(data->task); 1231 data->task = NULL; 1232 } 1233 1234 return bu27034_meas_set(data, false); 1235 } 1236 1237 static const struct iio_buffer_setup_ops bu27034_buffer_ops = { 1238 .postenable = &bu27034_buffer_enable, 1239 .predisable = &bu27034_buffer_disable, 1240 }; 1241 1242 static int bu27034_probe(struct i2c_client *i2c) 1243 { 1244 struct device *dev = &i2c->dev; 1245 struct bu27034_data *data; 1246 struct regmap *regmap; 1247 struct iio_dev *idev; 1248 unsigned int part_id, reg; 1249 int ret; 1250 1251 regmap = devm_regmap_init_i2c(i2c, &bu27034_regmap); 1252 if (IS_ERR(regmap)) 1253 return dev_err_probe(dev, PTR_ERR(regmap), 1254 "Failed to initialize Regmap\n"); 1255 1256 idev = devm_iio_device_alloc(dev, sizeof(*data)); 1257 if (!idev) 1258 return -ENOMEM; 1259 1260 ret = devm_regulator_get_enable(dev, "vdd"); 1261 if (ret) 1262 return dev_err_probe(dev, ret, "Failed to get regulator\n"); 1263 1264 data = iio_priv(idev); 1265 1266 ret = regmap_read(regmap, BU27034_REG_SYSTEM_CONTROL, ®); 1267 if (ret) 1268 return dev_err_probe(dev, ret, "Failed to access sensor\n"); 1269 1270 part_id = FIELD_GET(BU27034_MASK_PART_ID, reg); 1271 1272 if (part_id != BU27034_ID) 1273 dev_warn(dev, "unknown device 0x%x\n", part_id); 1274 1275 ret = devm_iio_init_iio_gts(dev, BU27034_SCALE_1X, 0, bu27034_gains, 1276 ARRAY_SIZE(bu27034_gains), bu27034_itimes, 1277 ARRAY_SIZE(bu27034_itimes), &data->gts); 1278 if (ret) 1279 return ret; 1280 1281 mutex_init(&data->mutex); 1282 data->regmap = regmap; 1283 data->dev = dev; 1284 1285 idev->channels = bu27034_channels; 1286 idev->num_channels = ARRAY_SIZE(bu27034_channels); 1287 idev->name = "bu27034"; 1288 idev->info = &bu27034_info; 1289 1290 idev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 1291 idev->available_scan_masks = bu27034_scan_masks; 1292 1293 ret = bu27034_chip_init(data); 1294 if (ret) 1295 return ret; 1296 1297 ret = devm_iio_kfifo_buffer_setup(dev, idev, &bu27034_buffer_ops); 1298 if (ret) 1299 return dev_err_probe(dev, ret, "buffer setup failed\n"); 1300 1301 ret = devm_iio_device_register(dev, idev); 1302 if (ret < 0) 1303 return dev_err_probe(dev, ret, 1304 "Unable to register iio device\n"); 1305 1306 return ret; 1307 } 1308 1309 static const struct of_device_id bu27034_of_match[] = { 1310 { .compatible = "rohm,bu27034anuc" }, 1311 { } 1312 }; 1313 MODULE_DEVICE_TABLE(of, bu27034_of_match); 1314 1315 static struct i2c_driver bu27034_i2c_driver = { 1316 .driver = { 1317 .name = "bu27034-als", 1318 .of_match_table = bu27034_of_match, 1319 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1320 }, 1321 .probe = bu27034_probe, 1322 }; 1323 module_i2c_driver(bu27034_i2c_driver); 1324 1325 MODULE_LICENSE("GPL"); 1326 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); 1327 MODULE_DESCRIPTION("ROHM BU27034 ambient light sensor driver"); 1328 MODULE_IMPORT_NS("IIO_GTS_HELPER"); 1329