rohm-bu27008.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM Colour Sensor driver for
  4. * - BU27008 RGBC sensor
  5. * - BU27010 RGBC + Flickering sensor
  6. *
  7. * Copyright (c) 2023, ROHM Semiconductor.
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/property.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/units.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/iio-gts-helper.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. /*
  25. * A word about register address and mask definitions.
  26. *
  27. * At a quick glance to the data-sheet register tables, the BU27010 has all the
  28. * registers that the BU27008 has. On top of that the BU27010 adds couple of new
  29. * ones.
  30. *
  31. * So, all definitions BU27008_REG_* are there also for BU27010 but none of the
  32. * BU27010_REG_* are present on BU27008. This makes sense as BU27010 just adds
  33. * some features (Flicker FIFO, more power control) on top of the BU27008.
  34. *
  35. * Unfortunately, some of the wheel has been re-invented. Even though the names
  36. * of the registers have stayed the same, pretty much all of the functionality
  37. * provided by the registers has changed place. Contents of all MODE_CONTROL
  38. * registers on BU27008 and BU27010 are different.
  39. *
  40. * Chip-specific mapping from register addresses/bits to functionality is done
  41. * in bu27_chip_data structures.
  42. */
  43. #define BU27008_REG_SYSTEM_CONTROL 0x40
  44. #define BU27008_MASK_SW_RESET BIT(7)
  45. #define BU27008_MASK_PART_ID GENMASK(5, 0)
  46. #define BU27008_ID 0x1a
  47. #define BU27008_REG_MODE_CONTROL1 0x41
  48. #define BU27008_MASK_MEAS_MODE GENMASK(2, 0)
  49. #define BU27008_MASK_CHAN_SEL GENMASK(3, 2)
  50. #define BU27008_REG_MODE_CONTROL2 0x42
  51. #define BU27008_MASK_RGBC_GAIN GENMASK(7, 3)
  52. #define BU27008_MASK_IR_GAIN_LO GENMASK(2, 0)
  53. #define BU27008_SHIFT_IR_GAIN 3
  54. #define BU27008_REG_MODE_CONTROL3 0x43
  55. #define BU27008_MASK_VALID BIT(7)
  56. #define BU27008_MASK_INT_EN BIT(1)
  57. #define BU27008_INT_EN BU27008_MASK_INT_EN
  58. #define BU27008_INT_DIS 0
  59. #define BU27008_MASK_MEAS_EN BIT(0)
  60. #define BU27008_MEAS_EN BIT(0)
  61. #define BU27008_MEAS_DIS 0
  62. #define BU27008_REG_DATA0_LO 0x50
  63. #define BU27008_REG_DATA1_LO 0x52
  64. #define BU27008_REG_DATA2_LO 0x54
  65. #define BU27008_REG_DATA3_LO 0x56
  66. #define BU27008_REG_DATA3_HI 0x57
  67. #define BU27008_REG_MANUFACTURER_ID 0x92
  68. #define BU27008_REG_MAX BU27008_REG_MANUFACTURER_ID
  69. /* BU27010 specific definitions */
  70. #define BU27010_MASK_SW_RESET BIT(7)
  71. #define BU27010_ID 0x1b
  72. #define BU27010_REG_POWER 0x3e
  73. #define BU27010_MASK_POWER BIT(0)
  74. #define BU27010_REG_RESET 0x3f
  75. #define BU27010_MASK_RESET BIT(0)
  76. #define BU27010_RESET_RELEASE BU27010_MASK_RESET
  77. #define BU27010_MASK_MEAS_EN BIT(1)
  78. #define BU27010_MASK_CHAN_SEL GENMASK(7, 6)
  79. #define BU27010_MASK_MEAS_MODE GENMASK(5, 4)
  80. #define BU27010_MASK_RGBC_GAIN GENMASK(3, 0)
  81. #define BU27010_MASK_DATA3_GAIN GENMASK(7, 6)
  82. #define BU27010_MASK_DATA2_GAIN GENMASK(5, 4)
  83. #define BU27010_MASK_DATA1_GAIN GENMASK(3, 2)
  84. #define BU27010_MASK_DATA0_GAIN GENMASK(1, 0)
  85. #define BU27010_MASK_FLC_MODE BIT(7)
  86. #define BU27010_MASK_FLC_GAIN GENMASK(4, 0)
  87. #define BU27010_REG_MODE_CONTROL4 0x44
  88. /* If flicker is ever to be supported the IRQ must be handled as a field */
  89. #define BU27010_IRQ_DIS_ALL GENMASK(1, 0)
  90. #define BU27010_DRDY_EN BIT(0)
  91. #define BU27010_MASK_INT_SEL GENMASK(1, 0)
  92. #define BU27010_REG_MODE_CONTROL5 0x45
  93. #define BU27010_MASK_RGB_VALID BIT(7)
  94. #define BU27010_MASK_FLC_VALID BIT(6)
  95. #define BU27010_MASK_WAIT_EN BIT(3)
  96. #define BU27010_MASK_FIFO_EN BIT(2)
  97. #define BU27010_MASK_RGB_EN BIT(1)
  98. #define BU27010_MASK_FLC_EN BIT(0)
  99. #define BU27010_REG_DATA_FLICKER_LO 0x56
  100. #define BU27010_MASK_DATA_FLICKER_HI GENMASK(2, 0)
  101. #define BU27010_REG_FLICKER_COUNT 0x5a
  102. #define BU27010_REG_FIFO_LEVEL_LO 0x5b
  103. #define BU27010_MASK_FIFO_LEVEL_HI BIT(0)
  104. #define BU27010_REG_FIFO_DATA_LO 0x5d
  105. #define BU27010_REG_FIFO_DATA_HI 0x5e
  106. #define BU27010_MASK_FIFO_DATA_HI GENMASK(2, 0)
  107. #define BU27010_REG_MANUFACTURER_ID 0x92
  108. #define BU27010_REG_MAX BU27010_REG_MANUFACTURER_ID
  109. /**
  110. * enum bu27008_chan_type - BU27008 channel types
  111. * @BU27008_RED: Red channel. Always via data0.
  112. * @BU27008_GREEN: Green channel. Always via data1.
  113. * @BU27008_BLUE: Blue channel. Via data2 (when used).
  114. * @BU27008_CLEAR: Clear channel. Via data2 or data3 (when used).
  115. * @BU27008_IR: IR channel. Via data3 (when used).
  116. * @BU27008_LUX: Illuminance channel, computed using RGB and IR.
  117. * @BU27008_NUM_CHANS: Number of channel types.
  118. */
  119. enum bu27008_chan_type {
  120. BU27008_RED,
  121. BU27008_GREEN,
  122. BU27008_BLUE,
  123. BU27008_CLEAR,
  124. BU27008_IR,
  125. BU27008_LUX,
  126. BU27008_NUM_CHANS
  127. };
  128. /**
  129. * enum bu27008_chan - BU27008 physical data channel
  130. * @BU27008_DATA0: Always red.
  131. * @BU27008_DATA1: Always green.
  132. * @BU27008_DATA2: Blue or clear.
  133. * @BU27008_DATA3: IR or clear.
  134. * @BU27008_NUM_HW_CHANS: Number of physical channels
  135. */
  136. enum bu27008_chan {
  137. BU27008_DATA0,
  138. BU27008_DATA1,
  139. BU27008_DATA2,
  140. BU27008_DATA3,
  141. BU27008_NUM_HW_CHANS
  142. };
  143. /* We can always measure red and green at same time */
  144. #define ALWAYS_SCANNABLE (BIT(BU27008_RED) | BIT(BU27008_GREEN))
  145. /* We use these data channel configs. Ensure scan_masks below follow them too */
  146. #define BU27008_BLUE2_CLEAR3 0x0 /* buffer is R, G, B, C */
  147. #define BU27008_CLEAR2_IR3 0x1 /* buffer is R, G, C, IR */
  148. #define BU27008_BLUE2_IR3 0x2 /* buffer is R, G, B, IR */
  149. static const unsigned long bu27008_scan_masks[] = {
  150. /* buffer is R, G, B, C */
  151. ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_CLEAR),
  152. /* buffer is R, G, C, IR */
  153. ALWAYS_SCANNABLE | BIT(BU27008_CLEAR) | BIT(BU27008_IR),
  154. /* buffer is R, G, B, IR */
  155. ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_IR),
  156. /* buffer is R, G, B, IR, LUX */
  157. ALWAYS_SCANNABLE | BIT(BU27008_BLUE) | BIT(BU27008_IR) | BIT(BU27008_LUX),
  158. 0
  159. };
  160. /*
  161. * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS
  162. * Time impacts to gain: 1x, 2x, 4x, 8x.
  163. *
  164. * => Max total gain is HWGAIN * gain by integration time (8 * 1024) = 8192
  165. *
  166. * Max amplification is (HWGAIN * MAX integration-time multiplier) 1024 * 8
  167. * = 8192. With NANO scale we get rid of accuracy loss when we start with the
  168. * scale 16.0 for HWGAIN1, INT-TIME 55 mS. This way the nano scale for MAX
  169. * total gain 8192 will be 1953125
  170. */
  171. #define BU27008_SCALE_1X 16
  172. /*
  173. * On BU27010 available scales with gain 1x - 4096x,
  174. * timings 55, 100, 200, 400 mS. Time impacts to gain: 1x, 2x, 4x, 8x.
  175. *
  176. * => Max total gain is HWGAIN * gain by integration time (8 * 4096)
  177. *
  178. * Using NANO precision for scale we must use scale 64x corresponding gain 1x
  179. * to avoid precision loss.
  180. */
  181. #define BU27010_SCALE_1X 64
  182. /* See the data sheet for the "Gain Setting" table */
  183. #define BU27008_GSEL_1X 0x00
  184. #define BU27008_GSEL_4X 0x08
  185. #define BU27008_GSEL_8X 0x09
  186. #define BU27008_GSEL_16X 0x0a
  187. #define BU27008_GSEL_32X 0x0b
  188. #define BU27008_GSEL_64X 0x0c
  189. #define BU27008_GSEL_256X 0x18
  190. #define BU27008_GSEL_512X 0x19
  191. #define BU27008_GSEL_1024X 0x1a
  192. static const struct iio_gain_sel_pair bu27008_gains[] = {
  193. GAIN_SCALE_GAIN(1, BU27008_GSEL_1X),
  194. GAIN_SCALE_GAIN(4, BU27008_GSEL_4X),
  195. GAIN_SCALE_GAIN(8, BU27008_GSEL_8X),
  196. GAIN_SCALE_GAIN(16, BU27008_GSEL_16X),
  197. GAIN_SCALE_GAIN(32, BU27008_GSEL_32X),
  198. GAIN_SCALE_GAIN(64, BU27008_GSEL_64X),
  199. GAIN_SCALE_GAIN(256, BU27008_GSEL_256X),
  200. GAIN_SCALE_GAIN(512, BU27008_GSEL_512X),
  201. GAIN_SCALE_GAIN(1024, BU27008_GSEL_1024X),
  202. };
  203. static const struct iio_gain_sel_pair bu27008_gains_ir[] = {
  204. GAIN_SCALE_GAIN(2, BU27008_GSEL_1X),
  205. GAIN_SCALE_GAIN(4, BU27008_GSEL_4X),
  206. GAIN_SCALE_GAIN(8, BU27008_GSEL_8X),
  207. GAIN_SCALE_GAIN(16, BU27008_GSEL_16X),
  208. GAIN_SCALE_GAIN(32, BU27008_GSEL_32X),
  209. GAIN_SCALE_GAIN(64, BU27008_GSEL_64X),
  210. GAIN_SCALE_GAIN(256, BU27008_GSEL_256X),
  211. GAIN_SCALE_GAIN(512, BU27008_GSEL_512X),
  212. GAIN_SCALE_GAIN(1024, BU27008_GSEL_1024X),
  213. };
  214. #define BU27010_GSEL_1X 0x00 /* 000000 */
  215. #define BU27010_GSEL_4X 0x08 /* 001000 */
  216. #define BU27010_GSEL_16X 0x09 /* 001001 */
  217. #define BU27010_GSEL_64X 0x0e /* 001110 */
  218. #define BU27010_GSEL_256X 0x1e /* 011110 */
  219. #define BU27010_GSEL_1024X 0x2e /* 101110 */
  220. #define BU27010_GSEL_4096X 0x3f /* 111111 */
  221. static const struct iio_gain_sel_pair bu27010_gains[] = {
  222. GAIN_SCALE_GAIN(1, BU27010_GSEL_1X),
  223. GAIN_SCALE_GAIN(4, BU27010_GSEL_4X),
  224. GAIN_SCALE_GAIN(16, BU27010_GSEL_16X),
  225. GAIN_SCALE_GAIN(64, BU27010_GSEL_64X),
  226. GAIN_SCALE_GAIN(256, BU27010_GSEL_256X),
  227. GAIN_SCALE_GAIN(1024, BU27010_GSEL_1024X),
  228. GAIN_SCALE_GAIN(4096, BU27010_GSEL_4096X),
  229. };
  230. static const struct iio_gain_sel_pair bu27010_gains_ir[] = {
  231. GAIN_SCALE_GAIN(2, BU27010_GSEL_1X),
  232. GAIN_SCALE_GAIN(4, BU27010_GSEL_4X),
  233. GAIN_SCALE_GAIN(16, BU27010_GSEL_16X),
  234. GAIN_SCALE_GAIN(64, BU27010_GSEL_64X),
  235. GAIN_SCALE_GAIN(256, BU27010_GSEL_256X),
  236. GAIN_SCALE_GAIN(1024, BU27010_GSEL_1024X),
  237. GAIN_SCALE_GAIN(4096, BU27010_GSEL_4096X),
  238. };
  239. #define BU27008_MEAS_MODE_100MS 0x00
  240. #define BU27008_MEAS_MODE_55MS 0x01
  241. #define BU27008_MEAS_MODE_200MS 0x02
  242. #define BU27008_MEAS_MODE_400MS 0x04
  243. #define BU27010_MEAS_MODE_100MS 0x00
  244. #define BU27010_MEAS_MODE_55MS 0x03
  245. #define BU27010_MEAS_MODE_200MS 0x01
  246. #define BU27010_MEAS_MODE_400MS 0x02
  247. #define BU27008_MEAS_TIME_MAX_MS 400
  248. static const struct iio_itime_sel_mul bu27008_itimes[] = {
  249. GAIN_SCALE_ITIME_US(400000, BU27008_MEAS_MODE_400MS, 8),
  250. GAIN_SCALE_ITIME_US(200000, BU27008_MEAS_MODE_200MS, 4),
  251. GAIN_SCALE_ITIME_US(100000, BU27008_MEAS_MODE_100MS, 2),
  252. GAIN_SCALE_ITIME_US(55000, BU27008_MEAS_MODE_55MS, 1),
  253. };
  254. static const struct iio_itime_sel_mul bu27010_itimes[] = {
  255. GAIN_SCALE_ITIME_US(400000, BU27010_MEAS_MODE_400MS, 8),
  256. GAIN_SCALE_ITIME_US(200000, BU27010_MEAS_MODE_200MS, 4),
  257. GAIN_SCALE_ITIME_US(100000, BU27010_MEAS_MODE_100MS, 2),
  258. GAIN_SCALE_ITIME_US(55000, BU27010_MEAS_MODE_55MS, 1),
  259. };
  260. /*
  261. * All the RGBC channels share the same gain.
  262. * IR gain can be fine-tuned from the gain set for the RGBC by 2 bit, but this
  263. * would yield quite complex gain setting. Especially since not all bit
  264. * compinations are supported. And in any case setting GAIN for RGBC will
  265. * always also change the IR-gain.
  266. *
  267. * On top of this, the selector '0' which corresponds to hw-gain 1X on RGBC,
  268. * corresponds to gain 2X on IR. Rest of the selctors correspond to same gains
  269. * though. This, however, makes it not possible to use shared gain for all
  270. * RGBC and IR settings even though they are all changed at the one go.
  271. */
  272. #define BU27008_CHAN(color, data, separate_avail) \
  273. { \
  274. .type = IIO_INTENSITY, \
  275. .modified = 1, \
  276. .channel2 = IIO_MOD_LIGHT_##color, \
  277. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  278. BIT(IIO_CHAN_INFO_SCALE), \
  279. .info_mask_separate_available = (separate_avail), \
  280. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
  281. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), \
  282. .address = BU27008_REG_##data##_LO, \
  283. .scan_index = BU27008_##color, \
  284. .scan_type = { \
  285. .sign = 'u', \
  286. .realbits = 16, \
  287. .storagebits = 16, \
  288. .endianness = IIO_LE, \
  289. }, \
  290. }
  291. /* For raw reads we always configure DATA3 for CLEAR */
  292. static const struct iio_chan_spec bu27008_channels[] = {
  293. BU27008_CHAN(RED, DATA0, BIT(IIO_CHAN_INFO_SCALE)),
  294. BU27008_CHAN(GREEN, DATA1, BIT(IIO_CHAN_INFO_SCALE)),
  295. BU27008_CHAN(BLUE, DATA2, BIT(IIO_CHAN_INFO_SCALE)),
  296. BU27008_CHAN(CLEAR, DATA2, BIT(IIO_CHAN_INFO_SCALE)),
  297. /*
  298. * We don't allow setting scale for IR (because of shared gain bits).
  299. * Hence we don't advertise available ones either.
  300. */
  301. BU27008_CHAN(IR, DATA3, 0),
  302. {
  303. .type = IIO_LIGHT,
  304. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  305. BIT(IIO_CHAN_INFO_SCALE),
  306. .channel = BU27008_LUX,
  307. .scan_index = BU27008_LUX,
  308. .scan_type = {
  309. .sign = 'u',
  310. .realbits = 64,
  311. .storagebits = 64,
  312. .endianness = IIO_CPU,
  313. },
  314. },
  315. IIO_CHAN_SOFT_TIMESTAMP(BU27008_NUM_CHANS),
  316. };
  317. struct bu27008_data;
  318. struct bu27_chip_data {
  319. const char *name;
  320. int (*chip_init)(struct bu27008_data *data);
  321. int (*get_gain_sel)(struct bu27008_data *data, int *sel);
  322. int (*write_gain_sel)(struct bu27008_data *data, int sel);
  323. const struct regmap_config *regmap_cfg;
  324. const struct iio_gain_sel_pair *gains;
  325. const struct iio_gain_sel_pair *gains_ir;
  326. const struct iio_itime_sel_mul *itimes;
  327. int num_gains;
  328. int num_gains_ir;
  329. int num_itimes;
  330. int scale1x;
  331. int drdy_en_reg;
  332. int drdy_en_mask;
  333. int meas_en_reg;
  334. int meas_en_mask;
  335. int valid_reg;
  336. int chan_sel_reg;
  337. int chan_sel_mask;
  338. int int_time_mask;
  339. u8 part_id;
  340. };
  341. struct bu27008_data {
  342. const struct bu27_chip_data *cd;
  343. struct regmap *regmap;
  344. struct iio_trigger *trig;
  345. struct device *dev;
  346. struct iio_gts gts;
  347. struct iio_gts gts_ir;
  348. int irq;
  349. /*
  350. * Prevent changing gain/time config when scale is read/written.
  351. * Similarly, protect the integration_time read/change sequence.
  352. * Prevent changing gain/time when data is read.
  353. */
  354. struct mutex mutex;
  355. };
  356. static const struct regmap_range bu27008_volatile_ranges[] = {
  357. {
  358. .range_min = BU27008_REG_SYSTEM_CONTROL, /* SWRESET */
  359. .range_max = BU27008_REG_SYSTEM_CONTROL,
  360. }, {
  361. .range_min = BU27008_REG_MODE_CONTROL3, /* VALID */
  362. .range_max = BU27008_REG_MODE_CONTROL3,
  363. }, {
  364. .range_min = BU27008_REG_DATA0_LO, /* DATA */
  365. .range_max = BU27008_REG_DATA3_HI,
  366. },
  367. };
  368. static const struct regmap_range bu27010_volatile_ranges[] = {
  369. {
  370. .range_min = BU27010_REG_RESET, /* RSTB */
  371. .range_max = BU27008_REG_SYSTEM_CONTROL, /* RESET */
  372. }, {
  373. .range_min = BU27010_REG_MODE_CONTROL5, /* VALID bits */
  374. .range_max = BU27010_REG_MODE_CONTROL5,
  375. }, {
  376. .range_min = BU27008_REG_DATA0_LO,
  377. .range_max = BU27010_REG_FIFO_DATA_HI,
  378. },
  379. };
  380. static const struct regmap_access_table bu27008_volatile_regs = {
  381. .yes_ranges = &bu27008_volatile_ranges[0],
  382. .n_yes_ranges = ARRAY_SIZE(bu27008_volatile_ranges),
  383. };
  384. static const struct regmap_access_table bu27010_volatile_regs = {
  385. .yes_ranges = &bu27010_volatile_ranges[0],
  386. .n_yes_ranges = ARRAY_SIZE(bu27010_volatile_ranges),
  387. };
  388. static const struct regmap_range bu27008_read_only_ranges[] = {
  389. {
  390. .range_min = BU27008_REG_DATA0_LO,
  391. .range_max = BU27008_REG_DATA3_HI,
  392. }, {
  393. .range_min = BU27008_REG_MANUFACTURER_ID,
  394. .range_max = BU27008_REG_MANUFACTURER_ID,
  395. },
  396. };
  397. static const struct regmap_range bu27010_read_only_ranges[] = {
  398. {
  399. .range_min = BU27008_REG_DATA0_LO,
  400. .range_max = BU27010_REG_FIFO_DATA_HI,
  401. }, {
  402. .range_min = BU27010_REG_MANUFACTURER_ID,
  403. .range_max = BU27010_REG_MANUFACTURER_ID,
  404. }
  405. };
  406. static const struct regmap_access_table bu27008_ro_regs = {
  407. .no_ranges = &bu27008_read_only_ranges[0],
  408. .n_no_ranges = ARRAY_SIZE(bu27008_read_only_ranges),
  409. };
  410. static const struct regmap_access_table bu27010_ro_regs = {
  411. .no_ranges = &bu27010_read_only_ranges[0],
  412. .n_no_ranges = ARRAY_SIZE(bu27010_read_only_ranges),
  413. };
  414. static const struct regmap_config bu27008_regmap = {
  415. .reg_bits = 8,
  416. .val_bits = 8,
  417. .max_register = BU27008_REG_MAX,
  418. .cache_type = REGCACHE_RBTREE,
  419. .volatile_table = &bu27008_volatile_regs,
  420. .wr_table = &bu27008_ro_regs,
  421. /*
  422. * All register writes are serialized by the mutex which protects the
  423. * scale setting/getting. This is needed because scale is combined by
  424. * gain and integration time settings and we need to ensure those are
  425. * not read / written when scale is being computed.
  426. *
  427. * As a result of this serializing, we don't need regmap locking. Note,
  428. * this is not true if we add any configurations which are not
  429. * serialized by the mutex and which may need for example a protected
  430. * read-modify-write cycle (eg. regmap_update_bits()). Please, revise
  431. * this when adding features to the driver.
  432. */
  433. .disable_locking = true,
  434. };
  435. static const struct regmap_config bu27010_regmap = {
  436. .reg_bits = 8,
  437. .val_bits = 8,
  438. .max_register = BU27010_REG_MAX,
  439. .cache_type = REGCACHE_RBTREE,
  440. .volatile_table = &bu27010_volatile_regs,
  441. .wr_table = &bu27010_ro_regs,
  442. .disable_locking = true,
  443. };
  444. static int bu27008_write_gain_sel(struct bu27008_data *data, int sel)
  445. {
  446. int regval;
  447. regval = FIELD_PREP(BU27008_MASK_RGBC_GAIN, sel);
  448. /*
  449. * We do always set also the LOW bits of IR-gain because othervice we
  450. * would risk resulting an invalid GAIN register value.
  451. *
  452. * We could allow setting separate gains for RGBC and IR when the
  453. * values were such that HW could support both gain settings.
  454. * Eg, when the shared bits were same for both gain values.
  455. *
  456. * This, however, has a negligible benefit compared to the increased
  457. * software complexity when we would need to go through the gains
  458. * for both channels separately when the integration time changes.
  459. * This would end up with nasty logic for computing gain values for
  460. * both channels - and rejecting them if shared bits changed.
  461. *
  462. * We should then build the logic by guessing what a user prefers.
  463. * RGBC or IR gains correctly set while other jumps to odd value?
  464. * Maybe look-up a value where both gains are somehow optimized
  465. * <what this somehow is, is ATM unknown to us>. Or maybe user would
  466. * expect us to reject changes when optimal gains can't be set to both
  467. * channels w/given integration time. At best that would result
  468. * solution that works well for a very specific subset of
  469. * configurations but causes unexpected corner-cases.
  470. *
  471. * So, we keep it simple. Always set same selector to IR and RGBC.
  472. * We disallow setting IR (as I expect that most of the users are
  473. * interested in RGBC). This way we can show the user that the scales
  474. * for RGBC and IR channels are different (1X Vs 2X with sel 0) while
  475. * still keeping the operation deterministic.
  476. */
  477. regval |= FIELD_PREP(BU27008_MASK_IR_GAIN_LO, sel);
  478. return regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL2,
  479. BU27008_MASK_RGBC_GAIN, regval);
  480. }
  481. static int bu27010_write_gain_sel(struct bu27008_data *data, int sel)
  482. {
  483. unsigned int regval;
  484. int ret, chan_selector;
  485. /*
  486. * Gain 'selector' is composed of two registers. Selector is 6bit value,
  487. * 4 high bits being the RGBC gain fieild in MODE_CONTROL1 register and
  488. * two low bits being the channel specific gain in MODE_CONTROL2.
  489. *
  490. * Let's take the 4 high bits of whole 6 bit selector, and prepare
  491. * the MODE_CONTROL1 value (RGBC gain part).
  492. */
  493. regval = FIELD_PREP(BU27010_MASK_RGBC_GAIN, (sel >> 2));
  494. ret = regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL1,
  495. BU27010_MASK_RGBC_GAIN, regval);
  496. if (ret)
  497. return ret;
  498. /*
  499. * Two low two bits of the selector must be written for all 4
  500. * channels in the MODE_CONTROL2 register. Copy these two bits for
  501. * all channels.
  502. */
  503. chan_selector = sel & GENMASK(1, 0);
  504. regval = FIELD_PREP(BU27010_MASK_DATA0_GAIN, chan_selector);
  505. regval |= FIELD_PREP(BU27010_MASK_DATA1_GAIN, chan_selector);
  506. regval |= FIELD_PREP(BU27010_MASK_DATA2_GAIN, chan_selector);
  507. regval |= FIELD_PREP(BU27010_MASK_DATA3_GAIN, chan_selector);
  508. return regmap_write(data->regmap, BU27008_REG_MODE_CONTROL2, regval);
  509. }
  510. static int bu27008_get_gain_sel(struct bu27008_data *data, int *sel)
  511. {
  512. int ret;
  513. /*
  514. * If we always "lock" the gain selectors for all channels to prevent
  515. * unsupported configs, then it does not matter which channel is used
  516. * we can just return selector from any of them.
  517. *
  518. * This, however is not true if we decide to support only 4X and 16X
  519. * and then individual gains for channels. Currently this is not the
  520. * case.
  521. *
  522. * If we some day decide to support individual gains, then we need to
  523. * have channel information here.
  524. */
  525. ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL2, sel);
  526. if (ret)
  527. return ret;
  528. *sel = FIELD_GET(BU27008_MASK_RGBC_GAIN, *sel);
  529. return 0;
  530. }
  531. static int bu27010_get_gain_sel(struct bu27008_data *data, int *sel)
  532. {
  533. int ret, tmp;
  534. /*
  535. * We always "lock" the gain selectors for all channels to prevent
  536. * unsupported configs. It does not matter which channel is used
  537. * we can just return selector from any of them.
  538. *
  539. * Read the channel0 gain.
  540. */
  541. ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL2, sel);
  542. if (ret)
  543. return ret;
  544. *sel = FIELD_GET(BU27010_MASK_DATA0_GAIN, *sel);
  545. /* Read the shared gain */
  546. ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL1, &tmp);
  547. if (ret)
  548. return ret;
  549. /*
  550. * The gain selector is made as a combination of common RGBC gain and
  551. * the channel specific gain. The channel specific gain forms the low
  552. * bits of selector and RGBC gain is appended right after it.
  553. *
  554. * Compose the selector from channel0 gain and shared RGBC gain.
  555. */
  556. *sel |= FIELD_GET(BU27010_MASK_RGBC_GAIN, tmp) << fls(BU27010_MASK_DATA0_GAIN);
  557. return ret;
  558. }
  559. static int bu27008_chip_init(struct bu27008_data *data)
  560. {
  561. int ret;
  562. ret = regmap_write_bits(data->regmap, BU27008_REG_SYSTEM_CONTROL,
  563. BU27008_MASK_SW_RESET, BU27008_MASK_SW_RESET);
  564. if (ret)
  565. return dev_err_probe(data->dev, ret, "Sensor reset failed\n");
  566. /*
  567. * The data-sheet does not tell how long performing the IC reset takes.
  568. * However, the data-sheet says the minimum time it takes the IC to be
  569. * able to take inputs after power is applied, is 100 uS. I'd assume
  570. * > 1 mS is enough.
  571. */
  572. msleep(1);
  573. ret = regmap_reinit_cache(data->regmap, data->cd->regmap_cfg);
  574. if (ret)
  575. dev_err(data->dev, "Failed to reinit reg cache\n");
  576. return ret;
  577. }
  578. static int bu27010_chip_init(struct bu27008_data *data)
  579. {
  580. int ret;
  581. ret = regmap_write_bits(data->regmap, BU27008_REG_SYSTEM_CONTROL,
  582. BU27010_MASK_SW_RESET, BU27010_MASK_SW_RESET);
  583. if (ret)
  584. return dev_err_probe(data->dev, ret, "Sensor reset failed\n");
  585. msleep(1);
  586. /* Power ON*/
  587. ret = regmap_write_bits(data->regmap, BU27010_REG_POWER,
  588. BU27010_MASK_POWER, BU27010_MASK_POWER);
  589. if (ret)
  590. return dev_err_probe(data->dev, ret, "Sensor power-on failed\n");
  591. msleep(1);
  592. /* Release blocks from reset */
  593. ret = regmap_write_bits(data->regmap, BU27010_REG_RESET,
  594. BU27010_MASK_RESET, BU27010_RESET_RELEASE);
  595. if (ret)
  596. return dev_err_probe(data->dev, ret, "Sensor powering failed\n");
  597. msleep(1);
  598. /*
  599. * The IRQ enabling on BU27010 is done in a peculiar way. The IRQ
  600. * enabling is not a bit mask where individual IRQs could be enabled but
  601. * a field which values are:
  602. * 00 => IRQs disabled
  603. * 01 => Data-ready (RGBC/IR)
  604. * 10 => Data-ready (flicker)
  605. * 11 => Flicker FIFO
  606. *
  607. * So, only one IRQ can be enabled at a time and enabling for example
  608. * flicker FIFO would automagically disable data-ready IRQ.
  609. *
  610. * Currently the driver does not support the flicker. Hence, we can
  611. * just treat the RGBC data-ready as single bit which can be enabled /
  612. * disabled. This works for as long as the second bit in the field
  613. * stays zero. Here we ensure it gets zeroed.
  614. */
  615. return regmap_clear_bits(data->regmap, BU27010_REG_MODE_CONTROL4,
  616. BU27010_IRQ_DIS_ALL);
  617. }
  618. static const struct bu27_chip_data bu27010_chip = {
  619. .name = "bu27010",
  620. .chip_init = bu27010_chip_init,
  621. .get_gain_sel = bu27010_get_gain_sel,
  622. .write_gain_sel = bu27010_write_gain_sel,
  623. .regmap_cfg = &bu27010_regmap,
  624. .gains = &bu27010_gains[0],
  625. .gains_ir = &bu27010_gains_ir[0],
  626. .itimes = &bu27010_itimes[0],
  627. .num_gains = ARRAY_SIZE(bu27010_gains),
  628. .num_gains_ir = ARRAY_SIZE(bu27010_gains_ir),
  629. .num_itimes = ARRAY_SIZE(bu27010_itimes),
  630. .scale1x = BU27010_SCALE_1X,
  631. .drdy_en_reg = BU27010_REG_MODE_CONTROL4,
  632. .drdy_en_mask = BU27010_DRDY_EN,
  633. .meas_en_reg = BU27010_REG_MODE_CONTROL5,
  634. .meas_en_mask = BU27010_MASK_MEAS_EN,
  635. .valid_reg = BU27010_REG_MODE_CONTROL5,
  636. .chan_sel_reg = BU27008_REG_MODE_CONTROL1,
  637. .chan_sel_mask = BU27010_MASK_CHAN_SEL,
  638. .int_time_mask = BU27010_MASK_MEAS_MODE,
  639. .part_id = BU27010_ID,
  640. };
  641. static const struct bu27_chip_data bu27008_chip = {
  642. .name = "bu27008",
  643. .chip_init = bu27008_chip_init,
  644. .get_gain_sel = bu27008_get_gain_sel,
  645. .write_gain_sel = bu27008_write_gain_sel,
  646. .regmap_cfg = &bu27008_regmap,
  647. .gains = &bu27008_gains[0],
  648. .gains_ir = &bu27008_gains_ir[0],
  649. .itimes = &bu27008_itimes[0],
  650. .num_gains = ARRAY_SIZE(bu27008_gains),
  651. .num_gains_ir = ARRAY_SIZE(bu27008_gains_ir),
  652. .num_itimes = ARRAY_SIZE(bu27008_itimes),
  653. .scale1x = BU27008_SCALE_1X,
  654. .drdy_en_reg = BU27008_REG_MODE_CONTROL3,
  655. .drdy_en_mask = BU27008_MASK_INT_EN,
  656. .valid_reg = BU27008_REG_MODE_CONTROL3,
  657. .meas_en_reg = BU27008_REG_MODE_CONTROL3,
  658. .meas_en_mask = BU27008_MASK_MEAS_EN,
  659. .chan_sel_reg = BU27008_REG_MODE_CONTROL3,
  660. .chan_sel_mask = BU27008_MASK_CHAN_SEL,
  661. .int_time_mask = BU27008_MASK_MEAS_MODE,
  662. .part_id = BU27008_ID,
  663. };
  664. #define BU27008_MAX_VALID_RESULT_WAIT_US 50000
  665. #define BU27008_VALID_RESULT_WAIT_QUANTA_US 1000
  666. static int bu27008_chan_read_data(struct bu27008_data *data, int reg, int *val)
  667. {
  668. int ret, valid;
  669. __le16 tmp;
  670. ret = regmap_read_poll_timeout(data->regmap, data->cd->valid_reg,
  671. valid, (valid & BU27008_MASK_VALID),
  672. BU27008_VALID_RESULT_WAIT_QUANTA_US,
  673. BU27008_MAX_VALID_RESULT_WAIT_US);
  674. if (ret)
  675. return ret;
  676. ret = regmap_bulk_read(data->regmap, reg, &tmp, sizeof(tmp));
  677. if (ret)
  678. dev_err(data->dev, "Reading channel data failed\n");
  679. *val = le16_to_cpu(tmp);
  680. return ret;
  681. }
  682. static int bu27008_get_gain(struct bu27008_data *data, struct iio_gts *gts, int *gain)
  683. {
  684. int ret, sel;
  685. ret = data->cd->get_gain_sel(data, &sel);
  686. if (ret)
  687. return ret;
  688. ret = iio_gts_find_gain_by_sel(gts, sel);
  689. if (ret < 0) {
  690. dev_err(data->dev, "unknown gain value 0x%x\n", sel);
  691. return ret;
  692. }
  693. *gain = ret;
  694. return 0;
  695. }
  696. static int bu27008_set_gain(struct bu27008_data *data, int gain)
  697. {
  698. int ret;
  699. ret = iio_gts_find_sel_by_gain(&data->gts, gain);
  700. if (ret < 0)
  701. return ret;
  702. return data->cd->write_gain_sel(data, ret);
  703. }
  704. static int bu27008_get_int_time_sel(struct bu27008_data *data, int *sel)
  705. {
  706. int ret, val;
  707. ret = regmap_read(data->regmap, BU27008_REG_MODE_CONTROL1, &val);
  708. if (ret)
  709. return ret;
  710. val &= data->cd->int_time_mask;
  711. val >>= ffs(data->cd->int_time_mask) - 1;
  712. *sel = val;
  713. return 0;
  714. }
  715. static int bu27008_set_int_time_sel(struct bu27008_data *data, int sel)
  716. {
  717. sel <<= ffs(data->cd->int_time_mask) - 1;
  718. return regmap_update_bits(data->regmap, BU27008_REG_MODE_CONTROL1,
  719. data->cd->int_time_mask, sel);
  720. }
  721. static int bu27008_get_int_time_us(struct bu27008_data *data)
  722. {
  723. int ret, sel;
  724. ret = bu27008_get_int_time_sel(data, &sel);
  725. if (ret)
  726. return ret;
  727. return iio_gts_find_int_time_by_sel(&data->gts, sel);
  728. }
  729. static int _bu27008_get_scale(struct bu27008_data *data, bool ir, int *val,
  730. int *val2)
  731. {
  732. struct iio_gts *gts;
  733. int gain, ret;
  734. if (ir)
  735. gts = &data->gts_ir;
  736. else
  737. gts = &data->gts;
  738. ret = bu27008_get_gain(data, gts, &gain);
  739. if (ret)
  740. return ret;
  741. ret = bu27008_get_int_time_us(data);
  742. if (ret < 0)
  743. return ret;
  744. return iio_gts_get_scale(gts, gain, ret, val, val2);
  745. }
  746. static int bu27008_get_scale(struct bu27008_data *data, bool ir, int *val,
  747. int *val2)
  748. {
  749. int ret;
  750. mutex_lock(&data->mutex);
  751. ret = _bu27008_get_scale(data, ir, val, val2);
  752. mutex_unlock(&data->mutex);
  753. return ret;
  754. }
  755. static int bu27008_set_int_time(struct bu27008_data *data, int time)
  756. {
  757. int ret;
  758. ret = iio_gts_find_sel_by_int_time(&data->gts, time);
  759. if (ret < 0)
  760. return ret;
  761. return bu27008_set_int_time_sel(data, ret);
  762. }
  763. /* Try to change the time so that the scale is maintained */
  764. static int bu27008_try_set_int_time(struct bu27008_data *data, int int_time_new)
  765. {
  766. int ret, old_time_sel, new_time_sel, old_gain, new_gain;
  767. mutex_lock(&data->mutex);
  768. ret = bu27008_get_int_time_sel(data, &old_time_sel);
  769. if (ret < 0)
  770. goto unlock_out;
  771. if (!iio_gts_valid_time(&data->gts, int_time_new)) {
  772. dev_dbg(data->dev, "Unsupported integration time %u\n",
  773. int_time_new);
  774. ret = -EINVAL;
  775. goto unlock_out;
  776. }
  777. /* If we already use requested time, then we're done */
  778. new_time_sel = iio_gts_find_sel_by_int_time(&data->gts, int_time_new);
  779. if (new_time_sel == old_time_sel)
  780. goto unlock_out;
  781. ret = bu27008_get_gain(data, &data->gts, &old_gain);
  782. if (ret)
  783. goto unlock_out;
  784. ret = iio_gts_find_new_gain_sel_by_old_gain_time(&data->gts, old_gain,
  785. old_time_sel, new_time_sel, &new_gain);
  786. if (ret) {
  787. int scale1, scale2;
  788. bool ok;
  789. _bu27008_get_scale(data, false, &scale1, &scale2);
  790. dev_dbg(data->dev,
  791. "Can't support time %u with current scale %u %u\n",
  792. int_time_new, scale1, scale2);
  793. if (new_gain < 0)
  794. goto unlock_out;
  795. /*
  796. * If caller requests for integration time change and we
  797. * can't support the scale - then the caller should be
  798. * prepared to 'pick up the pieces and deal with the
  799. * fact that the scale changed'.
  800. */
  801. ret = iio_find_closest_gain_low(&data->gts, new_gain, &ok);
  802. if (!ok)
  803. dev_dbg(data->dev, "optimal gain out of range\n");
  804. if (ret < 0) {
  805. dev_dbg(data->dev,
  806. "Total gain increase. Risk of saturation");
  807. ret = iio_gts_get_min_gain(&data->gts);
  808. if (ret < 0)
  809. goto unlock_out;
  810. }
  811. new_gain = ret;
  812. dev_dbg(data->dev, "scale changed, new gain %u\n", new_gain);
  813. }
  814. ret = bu27008_set_gain(data, new_gain);
  815. if (ret)
  816. goto unlock_out;
  817. ret = bu27008_set_int_time(data, int_time_new);
  818. unlock_out:
  819. mutex_unlock(&data->mutex);
  820. return ret;
  821. }
  822. static int bu27008_meas_set(struct bu27008_data *data, bool enable)
  823. {
  824. if (enable)
  825. return regmap_set_bits(data->regmap, data->cd->meas_en_reg,
  826. data->cd->meas_en_mask);
  827. return regmap_clear_bits(data->regmap, data->cd->meas_en_reg,
  828. data->cd->meas_en_mask);
  829. }
  830. static int bu27008_chan_cfg(struct bu27008_data *data,
  831. struct iio_chan_spec const *chan)
  832. {
  833. int chan_sel;
  834. if (chan->scan_index == BU27008_BLUE)
  835. chan_sel = BU27008_BLUE2_CLEAR3;
  836. else
  837. chan_sel = BU27008_CLEAR2_IR3;
  838. /*
  839. * prepare bitfield for channel sel. The FIELD_PREP works only when
  840. * mask is constant. In our case the mask is assigned based on the
  841. * chip type. Hence the open-coded FIELD_PREP here. We don't bother
  842. * zeroing the irrelevant bits though - update_bits takes care of that.
  843. */
  844. chan_sel <<= ffs(data->cd->chan_sel_mask) - 1;
  845. return regmap_update_bits(data->regmap, data->cd->chan_sel_reg,
  846. BU27008_MASK_CHAN_SEL, chan_sel);
  847. }
  848. static int bu27008_read_one(struct bu27008_data *data, struct iio_dev *idev,
  849. struct iio_chan_spec const *chan, int *val, int *val2)
  850. {
  851. int ret, int_time;
  852. ret = bu27008_chan_cfg(data, chan);
  853. if (ret)
  854. return ret;
  855. ret = bu27008_meas_set(data, true);
  856. if (ret)
  857. return ret;
  858. ret = bu27008_get_int_time_us(data);
  859. if (ret < 0)
  860. int_time = BU27008_MEAS_TIME_MAX_MS;
  861. else
  862. int_time = ret / USEC_PER_MSEC;
  863. msleep(int_time);
  864. ret = bu27008_chan_read_data(data, chan->address, val);
  865. if (!ret)
  866. ret = IIO_VAL_INT;
  867. if (bu27008_meas_set(data, false))
  868. dev_warn(data->dev, "measurement disabling failed\n");
  869. return ret;
  870. }
  871. #define BU27008_LUX_DATA_RED 0
  872. #define BU27008_LUX_DATA_GREEN 1
  873. #define BU27008_LUX_DATA_BLUE 2
  874. #define BU27008_LUX_DATA_IR 3
  875. #define LUX_DATA_SIZE (BU27008_NUM_HW_CHANS * sizeof(__le16))
  876. static int bu27008_read_lux_chans(struct bu27008_data *data, unsigned int time,
  877. __le16 *chan_data)
  878. {
  879. int ret, chan_sel, tmpret, valid;
  880. chan_sel = BU27008_BLUE2_IR3 << (ffs(data->cd->chan_sel_mask) - 1);
  881. ret = regmap_update_bits(data->regmap, data->cd->chan_sel_reg,
  882. data->cd->chan_sel_mask, chan_sel);
  883. if (ret)
  884. return ret;
  885. ret = bu27008_meas_set(data, true);
  886. if (ret)
  887. return ret;
  888. msleep(time / USEC_PER_MSEC);
  889. ret = regmap_read_poll_timeout(data->regmap, data->cd->valid_reg,
  890. valid, (valid & BU27008_MASK_VALID),
  891. BU27008_VALID_RESULT_WAIT_QUANTA_US,
  892. BU27008_MAX_VALID_RESULT_WAIT_US);
  893. if (ret)
  894. goto out;
  895. ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, chan_data,
  896. LUX_DATA_SIZE);
  897. if (ret)
  898. goto out;
  899. out:
  900. tmpret = bu27008_meas_set(data, false);
  901. if (tmpret)
  902. dev_warn(data->dev, "Stopping measurement failed\n");
  903. return ret;
  904. }
  905. /*
  906. * Following equation for computing lux out of register values was given by
  907. * ROHM HW colleagues;
  908. *
  909. * Red = RedData*1024 / Gain * 20 / meas_mode
  910. * Green = GreenData* 1024 / Gain * 20 / meas_mode
  911. * Blue = BlueData* 1024 / Gain * 20 / meas_mode
  912. * IR = IrData* 1024 / Gain * 20 / meas_mode
  913. *
  914. * where meas_mode is the integration time in mS / 10
  915. *
  916. * IRratio = (IR > 0.18 * Green) ? 0 : 1
  917. *
  918. * Lx = max(c1*Red + c2*Green + c3*Blue,0)
  919. *
  920. * for
  921. * IRratio 0: c1 = -0.00002237, c2 = 0.0003219, c3 = -0.000120371
  922. * IRratio 1: c1 = -0.00001074, c2 = 0.000305415, c3 = -0.000129367
  923. */
  924. /*
  925. * The max chan data is 0xffff. When we multiply it by 1024 * 20, we'll get
  926. * 0x4FFFB000 which still fits in 32-bit integer. This won't overflow.
  927. */
  928. #define NORM_CHAN_DATA_FOR_LX_CALC(chan, gain, time) (le16_to_cpu(chan) * \
  929. 1024 * 20 / (gain) / (time))
  930. static u64 bu27008_calc_nlux(struct bu27008_data *data, __le16 *lux_data,
  931. unsigned int gain, unsigned int gain_ir, unsigned int time)
  932. {
  933. unsigned int red, green, blue, ir;
  934. s64 c1, c2, c3, nlux;
  935. time /= 10000;
  936. ir = NORM_CHAN_DATA_FOR_LX_CALC(lux_data[BU27008_LUX_DATA_IR], gain_ir, time);
  937. red = NORM_CHAN_DATA_FOR_LX_CALC(lux_data[BU27008_LUX_DATA_RED], gain, time);
  938. green = NORM_CHAN_DATA_FOR_LX_CALC(lux_data[BU27008_LUX_DATA_GREEN], gain, time);
  939. blue = NORM_CHAN_DATA_FOR_LX_CALC(lux_data[BU27008_LUX_DATA_BLUE], gain, time);
  940. if ((u64)ir * 100LLU > (u64)green * 18LLU) {
  941. c1 = -22370;
  942. c2 = 321900;
  943. c3 = -120371;
  944. } else {
  945. c1 = -10740;
  946. c2 = 305415;
  947. c3 = -129367;
  948. }
  949. nlux = c1 * red + c2 * green + c3 * blue;
  950. return max_t(s64, 0, nlux);
  951. }
  952. static int bu27008_get_time_n_gains(struct bu27008_data *data,
  953. unsigned int *gain, unsigned int *gain_ir, unsigned int *time)
  954. {
  955. int ret;
  956. ret = bu27008_get_gain(data, &data->gts, gain);
  957. if (ret < 0)
  958. return ret;
  959. ret = bu27008_get_gain(data, &data->gts_ir, gain_ir);
  960. if (ret < 0)
  961. return ret;
  962. ret = bu27008_get_int_time_us(data);
  963. if (ret < 0)
  964. return ret;
  965. /* Max integration time is 400000. Fits in signed int. */
  966. *time = ret;
  967. return 0;
  968. }
  969. struct bu27008_buf {
  970. __le16 chan[BU27008_NUM_HW_CHANS];
  971. u64 lux __aligned(8);
  972. s64 ts __aligned(8);
  973. };
  974. static int bu27008_buffer_fill_lux(struct bu27008_data *data,
  975. struct bu27008_buf *raw)
  976. {
  977. unsigned int gain, gain_ir, time;
  978. int ret;
  979. ret = bu27008_get_time_n_gains(data, &gain, &gain_ir, &time);
  980. if (ret)
  981. return ret;
  982. raw->lux = bu27008_calc_nlux(data, raw->chan, gain, gain_ir, time);
  983. return 0;
  984. }
  985. static int bu27008_read_lux(struct bu27008_data *data, struct iio_dev *idev,
  986. struct iio_chan_spec const *chan,
  987. int *val, int *val2)
  988. {
  989. __le16 lux_data[BU27008_NUM_HW_CHANS];
  990. unsigned int gain, gain_ir, time;
  991. u64 nlux;
  992. int ret;
  993. ret = bu27008_get_time_n_gains(data, &gain, &gain_ir, &time);
  994. if (ret)
  995. return ret;
  996. ret = bu27008_read_lux_chans(data, time, lux_data);
  997. if (ret)
  998. return ret;
  999. nlux = bu27008_calc_nlux(data, lux_data, gain, gain_ir, time);
  1000. *val = (int)nlux;
  1001. *val2 = nlux >> 32LLU;
  1002. return IIO_VAL_INT_64;
  1003. }
  1004. static int bu27008_read_raw(struct iio_dev *idev,
  1005. struct iio_chan_spec const *chan,
  1006. int *val, int *val2, long mask)
  1007. {
  1008. struct bu27008_data *data = iio_priv(idev);
  1009. int busy, ret;
  1010. switch (mask) {
  1011. case IIO_CHAN_INFO_RAW:
  1012. busy = iio_device_claim_direct_mode(idev);
  1013. if (busy)
  1014. return -EBUSY;
  1015. mutex_lock(&data->mutex);
  1016. if (chan->type == IIO_LIGHT)
  1017. ret = bu27008_read_lux(data, idev, chan, val, val2);
  1018. else
  1019. ret = bu27008_read_one(data, idev, chan, val, val2);
  1020. mutex_unlock(&data->mutex);
  1021. iio_device_release_direct_mode(idev);
  1022. return ret;
  1023. case IIO_CHAN_INFO_SCALE:
  1024. if (chan->type == IIO_LIGHT) {
  1025. *val = 0;
  1026. *val2 = 1;
  1027. return IIO_VAL_INT_PLUS_NANO;
  1028. }
  1029. ret = bu27008_get_scale(data, chan->scan_index == BU27008_IR,
  1030. val, val2);
  1031. if (ret)
  1032. return ret;
  1033. return IIO_VAL_INT_PLUS_NANO;
  1034. case IIO_CHAN_INFO_INT_TIME:
  1035. ret = bu27008_get_int_time_us(data);
  1036. if (ret < 0)
  1037. return ret;
  1038. *val = 0;
  1039. *val2 = ret;
  1040. return IIO_VAL_INT_PLUS_MICRO;
  1041. default:
  1042. return -EINVAL;
  1043. }
  1044. }
  1045. /* Called if the new scale could not be supported with existing int-time */
  1046. static int bu27008_try_find_new_time_gain(struct bu27008_data *data, int val,
  1047. int val2, int *gain_sel)
  1048. {
  1049. int i, ret, new_time_sel;
  1050. for (i = 0; i < data->gts.num_itime; i++) {
  1051. new_time_sel = data->gts.itime_table[i].sel;
  1052. ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts,
  1053. new_time_sel, val, val2, gain_sel);
  1054. if (!ret)
  1055. break;
  1056. }
  1057. if (i == data->gts.num_itime) {
  1058. dev_err(data->dev, "Can't support scale %u %u\n", val, val2);
  1059. return -EINVAL;
  1060. }
  1061. return bu27008_set_int_time_sel(data, new_time_sel);
  1062. }
  1063. static int bu27008_set_scale(struct bu27008_data *data,
  1064. struct iio_chan_spec const *chan,
  1065. int val, int val2)
  1066. {
  1067. int ret, gain_sel, time_sel;
  1068. if (chan->scan_index == BU27008_IR)
  1069. return -EINVAL;
  1070. mutex_lock(&data->mutex);
  1071. ret = bu27008_get_int_time_sel(data, &time_sel);
  1072. if (ret < 0)
  1073. goto unlock_out;
  1074. ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, time_sel,
  1075. val, val2, &gain_sel);
  1076. if (ret) {
  1077. ret = bu27008_try_find_new_time_gain(data, val, val2, &gain_sel);
  1078. if (ret)
  1079. goto unlock_out;
  1080. }
  1081. ret = data->cd->write_gain_sel(data, gain_sel);
  1082. unlock_out:
  1083. mutex_unlock(&data->mutex);
  1084. return ret;
  1085. }
  1086. static int bu27008_write_raw_get_fmt(struct iio_dev *indio_dev,
  1087. struct iio_chan_spec const *chan,
  1088. long mask)
  1089. {
  1090. switch (mask) {
  1091. case IIO_CHAN_INFO_SCALE:
  1092. return IIO_VAL_INT_PLUS_NANO;
  1093. case IIO_CHAN_INFO_INT_TIME:
  1094. return IIO_VAL_INT_PLUS_MICRO;
  1095. default:
  1096. return -EINVAL;
  1097. }
  1098. }
  1099. static int bu27008_write_raw(struct iio_dev *idev,
  1100. struct iio_chan_spec const *chan,
  1101. int val, int val2, long mask)
  1102. {
  1103. struct bu27008_data *data = iio_priv(idev);
  1104. int ret;
  1105. /*
  1106. * Do not allow changing scale when measurement is ongoing as doing so
  1107. * could make values in the buffer inconsistent.
  1108. */
  1109. ret = iio_device_claim_direct_mode(idev);
  1110. if (ret)
  1111. return ret;
  1112. switch (mask) {
  1113. case IIO_CHAN_INFO_SCALE:
  1114. ret = bu27008_set_scale(data, chan, val, val2);
  1115. break;
  1116. case IIO_CHAN_INFO_INT_TIME:
  1117. if (val) {
  1118. ret = -EINVAL;
  1119. break;
  1120. }
  1121. ret = bu27008_try_set_int_time(data, val2);
  1122. break;
  1123. default:
  1124. ret = -EINVAL;
  1125. break;
  1126. }
  1127. iio_device_release_direct_mode(idev);
  1128. return ret;
  1129. }
  1130. static int bu27008_read_avail(struct iio_dev *idev,
  1131. struct iio_chan_spec const *chan, const int **vals,
  1132. int *type, int *length, long mask)
  1133. {
  1134. struct bu27008_data *data = iio_priv(idev);
  1135. switch (mask) {
  1136. case IIO_CHAN_INFO_INT_TIME:
  1137. return iio_gts_avail_times(&data->gts, vals, type, length);
  1138. case IIO_CHAN_INFO_SCALE:
  1139. if (chan->channel2 == IIO_MOD_LIGHT_IR)
  1140. return iio_gts_all_avail_scales(&data->gts_ir, vals,
  1141. type, length);
  1142. return iio_gts_all_avail_scales(&data->gts, vals, type, length);
  1143. default:
  1144. return -EINVAL;
  1145. }
  1146. }
  1147. static int bu27008_update_scan_mode(struct iio_dev *idev,
  1148. const unsigned long *scan_mask)
  1149. {
  1150. struct bu27008_data *data = iio_priv(idev);
  1151. int chan_sel;
  1152. /* Configure channel selection */
  1153. if (test_bit(BU27008_BLUE, idev->active_scan_mask)) {
  1154. if (test_bit(BU27008_CLEAR, idev->active_scan_mask))
  1155. chan_sel = BU27008_BLUE2_CLEAR3;
  1156. else
  1157. chan_sel = BU27008_BLUE2_IR3;
  1158. } else {
  1159. chan_sel = BU27008_CLEAR2_IR3;
  1160. }
  1161. chan_sel <<= ffs(data->cd->chan_sel_mask) - 1;
  1162. return regmap_update_bits(data->regmap, data->cd->chan_sel_reg,
  1163. data->cd->chan_sel_mask, chan_sel);
  1164. }
  1165. static const struct iio_info bu27008_info = {
  1166. .read_raw = &bu27008_read_raw,
  1167. .write_raw = &bu27008_write_raw,
  1168. .write_raw_get_fmt = &bu27008_write_raw_get_fmt,
  1169. .read_avail = &bu27008_read_avail,
  1170. .update_scan_mode = bu27008_update_scan_mode,
  1171. .validate_trigger = iio_validate_own_trigger,
  1172. };
  1173. static int bu27008_trigger_set_state(struct iio_trigger *trig, bool state)
  1174. {
  1175. struct bu27008_data *data = iio_trigger_get_drvdata(trig);
  1176. int ret;
  1177. if (state)
  1178. ret = regmap_set_bits(data->regmap, data->cd->drdy_en_reg,
  1179. data->cd->drdy_en_mask);
  1180. else
  1181. ret = regmap_clear_bits(data->regmap, data->cd->drdy_en_reg,
  1182. data->cd->drdy_en_mask);
  1183. if (ret)
  1184. dev_err(data->dev, "Failed to set trigger state\n");
  1185. return ret;
  1186. }
  1187. static void bu27008_trigger_reenable(struct iio_trigger *trig)
  1188. {
  1189. struct bu27008_data *data = iio_trigger_get_drvdata(trig);
  1190. enable_irq(data->irq);
  1191. }
  1192. static const struct iio_trigger_ops bu27008_trigger_ops = {
  1193. .set_trigger_state = bu27008_trigger_set_state,
  1194. .reenable = bu27008_trigger_reenable,
  1195. };
  1196. static irqreturn_t bu27008_trigger_handler(int irq, void *p)
  1197. {
  1198. struct iio_poll_func *pf = p;
  1199. struct iio_dev *idev = pf->indio_dev;
  1200. struct bu27008_data *data = iio_priv(idev);
  1201. struct bu27008_buf raw;
  1202. int ret, dummy;
  1203. memset(&raw, 0, sizeof(raw));
  1204. /*
  1205. * After some measurements, it seems reading the
  1206. * BU27008_REG_MODE_CONTROL3 debounces the IRQ line
  1207. */
  1208. ret = regmap_read(data->regmap, data->cd->valid_reg, &dummy);
  1209. if (ret < 0)
  1210. goto err_read;
  1211. ret = regmap_bulk_read(data->regmap, BU27008_REG_DATA0_LO, &raw.chan,
  1212. sizeof(raw.chan));
  1213. if (ret < 0)
  1214. goto err_read;
  1215. if (test_bit(BU27008_LUX, idev->active_scan_mask)) {
  1216. ret = bu27008_buffer_fill_lux(data, &raw);
  1217. if (ret)
  1218. goto err_read;
  1219. }
  1220. iio_push_to_buffers_with_timestamp(idev, &raw, pf->timestamp);
  1221. err_read:
  1222. iio_trigger_notify_done(idev->trig);
  1223. return IRQ_HANDLED;
  1224. }
  1225. static int bu27008_buffer_preenable(struct iio_dev *idev)
  1226. {
  1227. struct bu27008_data *data = iio_priv(idev);
  1228. return bu27008_meas_set(data, true);
  1229. }
  1230. static int bu27008_buffer_postdisable(struct iio_dev *idev)
  1231. {
  1232. struct bu27008_data *data = iio_priv(idev);
  1233. return bu27008_meas_set(data, false);
  1234. }
  1235. static const struct iio_buffer_setup_ops bu27008_buffer_ops = {
  1236. .preenable = bu27008_buffer_preenable,
  1237. .postdisable = bu27008_buffer_postdisable,
  1238. };
  1239. static irqreturn_t bu27008_data_rdy_poll(int irq, void *private)
  1240. {
  1241. /*
  1242. * The BU27008 keeps IRQ asserted until we read the VALID bit from
  1243. * a register. We need to keep the IRQ disabled until then.
  1244. */
  1245. disable_irq_nosync(irq);
  1246. iio_trigger_poll(private);
  1247. return IRQ_HANDLED;
  1248. }
  1249. static int bu27008_setup_trigger(struct bu27008_data *data, struct iio_dev *idev)
  1250. {
  1251. struct iio_trigger *itrig;
  1252. char *name;
  1253. int ret;
  1254. ret = devm_iio_triggered_buffer_setup(data->dev, idev,
  1255. &iio_pollfunc_store_time,
  1256. bu27008_trigger_handler,
  1257. &bu27008_buffer_ops);
  1258. if (ret)
  1259. return dev_err_probe(data->dev, ret,
  1260. "iio_triggered_buffer_setup_ext FAIL\n");
  1261. itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d",
  1262. idev->name, iio_device_id(idev));
  1263. if (!itrig)
  1264. return -ENOMEM;
  1265. data->trig = itrig;
  1266. itrig->ops = &bu27008_trigger_ops;
  1267. iio_trigger_set_drvdata(itrig, data);
  1268. name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bu27008",
  1269. dev_name(data->dev));
  1270. ret = devm_request_irq(data->dev, data->irq,
  1271. &bu27008_data_rdy_poll,
  1272. 0, name, itrig);
  1273. if (ret)
  1274. return dev_err_probe(data->dev, ret, "Could not request IRQ\n");
  1275. ret = devm_iio_trigger_register(data->dev, itrig);
  1276. if (ret)
  1277. return dev_err_probe(data->dev, ret,
  1278. "Trigger registration failed\n");
  1279. /* set default trigger */
  1280. idev->trig = iio_trigger_get(itrig);
  1281. return 0;
  1282. }
  1283. static int bu27008_probe(struct i2c_client *i2c)
  1284. {
  1285. struct device *dev = &i2c->dev;
  1286. struct bu27008_data *data;
  1287. struct regmap *regmap;
  1288. unsigned int part_id, reg;
  1289. struct iio_dev *idev;
  1290. int ret;
  1291. idev = devm_iio_device_alloc(dev, sizeof(*data));
  1292. if (!idev)
  1293. return -ENOMEM;
  1294. ret = devm_regulator_get_enable(dev, "vdd");
  1295. if (ret)
  1296. return dev_err_probe(dev, ret, "Failed to get regulator\n");
  1297. data = iio_priv(idev);
  1298. data->cd = device_get_match_data(&i2c->dev);
  1299. if (!data->cd)
  1300. return -ENODEV;
  1301. regmap = devm_regmap_init_i2c(i2c, data->cd->regmap_cfg);
  1302. if (IS_ERR(regmap))
  1303. return dev_err_probe(dev, PTR_ERR(regmap),
  1304. "Failed to initialize Regmap\n");
  1305. ret = regmap_read(regmap, BU27008_REG_SYSTEM_CONTROL, &reg);
  1306. if (ret)
  1307. return dev_err_probe(dev, ret, "Failed to access sensor\n");
  1308. part_id = FIELD_GET(BU27008_MASK_PART_ID, reg);
  1309. if (part_id != data->cd->part_id)
  1310. dev_warn(dev, "unknown device 0x%x\n", part_id);
  1311. ret = devm_iio_init_iio_gts(dev, data->cd->scale1x, 0, data->cd->gains,
  1312. data->cd->num_gains, data->cd->itimes,
  1313. data->cd->num_itimes, &data->gts);
  1314. if (ret)
  1315. return ret;
  1316. ret = devm_iio_init_iio_gts(dev, data->cd->scale1x, 0, data->cd->gains_ir,
  1317. data->cd->num_gains_ir, data->cd->itimes,
  1318. data->cd->num_itimes, &data->gts_ir);
  1319. if (ret)
  1320. return ret;
  1321. mutex_init(&data->mutex);
  1322. data->regmap = regmap;
  1323. data->dev = dev;
  1324. data->irq = i2c->irq;
  1325. idev->channels = bu27008_channels;
  1326. idev->num_channels = ARRAY_SIZE(bu27008_channels);
  1327. idev->name = data->cd->name;
  1328. idev->info = &bu27008_info;
  1329. idev->modes = INDIO_DIRECT_MODE;
  1330. idev->available_scan_masks = bu27008_scan_masks;
  1331. ret = data->cd->chip_init(data);
  1332. if (ret)
  1333. return ret;
  1334. if (i2c->irq) {
  1335. ret = bu27008_setup_trigger(data, idev);
  1336. if (ret)
  1337. return ret;
  1338. } else {
  1339. dev_info(dev, "No IRQ, buffered mode disabled\n");
  1340. }
  1341. ret = devm_iio_device_register(dev, idev);
  1342. if (ret)
  1343. return dev_err_probe(dev, ret,
  1344. "Unable to register iio device\n");
  1345. return 0;
  1346. }
  1347. static const struct of_device_id bu27008_of_match[] = {
  1348. { .compatible = "rohm,bu27008", .data = &bu27008_chip },
  1349. { .compatible = "rohm,bu27010", .data = &bu27010_chip },
  1350. { }
  1351. };
  1352. MODULE_DEVICE_TABLE(of, bu27008_of_match);
  1353. static struct i2c_driver bu27008_i2c_driver = {
  1354. .driver = {
  1355. .name = "bu27008",
  1356. .of_match_table = bu27008_of_match,
  1357. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1358. },
  1359. .probe = bu27008_probe,
  1360. };
  1361. module_i2c_driver(bu27008_i2c_driver);
  1362. MODULE_DESCRIPTION("ROHM BU27008 and BU27010 colour sensor driver");
  1363. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  1364. MODULE_LICENSE("GPL");
  1365. MODULE_IMPORT_NS(IIO_GTS_HELPER);