rohm-bu27034.c 33 KB

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