as73211.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for AMS AS73211 JENCOLOR(R) Digital XYZ Sensor
  4. *
  5. * Author: Christian Eggers <[email protected]>
  6. *
  7. * Copyright (c) 2020 ARRI Lighting
  8. *
  9. * Color light sensor with 16-bit channels for x, y, z and temperature);
  10. * 7-bit I2C slave address 0x74 .. 0x77.
  11. *
  12. * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
  13. */
  14. #include <linux/bitfield.h>
  15. #include <linux/completion.h>
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/trigger_consumer.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/pm.h>
  26. #include <linux/units.h>
  27. #define AS73211_DRV_NAME "as73211"
  28. /* AS73211 configuration registers */
  29. #define AS73211_REG_OSR 0x0
  30. #define AS73211_REG_AGEN 0x2
  31. #define AS73211_REG_CREG1 0x6
  32. #define AS73211_REG_CREG2 0x7
  33. #define AS73211_REG_CREG3 0x8
  34. /* AS73211 output register bank */
  35. #define AS73211_OUT_OSR_STATUS 0
  36. #define AS73211_OUT_TEMP 1
  37. #define AS73211_OUT_MRES1 2
  38. #define AS73211_OUT_MRES2 3
  39. #define AS73211_OUT_MRES3 4
  40. #define AS73211_OSR_SS BIT(7)
  41. #define AS73211_OSR_PD BIT(6)
  42. #define AS73211_OSR_SW_RES BIT(3)
  43. #define AS73211_OSR_DOS_MASK GENMASK(2, 0)
  44. #define AS73211_OSR_DOS_CONFIG FIELD_PREP(AS73211_OSR_DOS_MASK, 0x2)
  45. #define AS73211_OSR_DOS_MEASURE FIELD_PREP(AS73211_OSR_DOS_MASK, 0x3)
  46. #define AS73211_AGEN_DEVID_MASK GENMASK(7, 4)
  47. #define AS73211_AGEN_DEVID(x) FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
  48. #define AS73211_AGEN_MUT_MASK GENMASK(3, 0)
  49. #define AS73211_AGEN_MUT(x) FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
  50. #define AS73211_CREG1_GAIN_MASK GENMASK(7, 4)
  51. #define AS73211_CREG1_GAIN_1 11
  52. #define AS73211_CREG1_TIME_MASK GENMASK(3, 0)
  53. #define AS73211_CREG3_CCLK_MASK GENMASK(1, 0)
  54. #define AS73211_OSR_STATUS_OUTCONVOF BIT(15)
  55. #define AS73211_OSR_STATUS_MRESOF BIT(14)
  56. #define AS73211_OSR_STATUS_ADCOF BIT(13)
  57. #define AS73211_OSR_STATUS_LDATA BIT(12)
  58. #define AS73211_OSR_STATUS_NDATA BIT(11)
  59. #define AS73211_OSR_STATUS_NOTREADY BIT(10)
  60. #define AS73211_SAMPLE_FREQ_BASE 1024000
  61. #define AS73211_SAMPLE_TIME_NUM 15
  62. #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
  63. /* Available sample frequencies are 1.024MHz multiplied by powers of two. */
  64. static const int as73211_samp_freq_avail[] = {
  65. AS73211_SAMPLE_FREQ_BASE * 1,
  66. AS73211_SAMPLE_FREQ_BASE * 2,
  67. AS73211_SAMPLE_FREQ_BASE * 4,
  68. AS73211_SAMPLE_FREQ_BASE * 8,
  69. };
  70. static const int as73211_hardwaregain_avail[] = {
  71. 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
  72. };
  73. /**
  74. * struct as73211_data - Instance data for one AS73211
  75. * @client: I2C client.
  76. * @osr: Cached Operational State Register.
  77. * @creg1: Cached Configuration Register 1.
  78. * @creg2: Cached Configuration Register 2.
  79. * @creg3: Cached Configuration Register 3.
  80. * @mutex: Keeps cached registers in sync with the device.
  81. * @completion: Completion to wait for interrupt.
  82. * @int_time_avail: Available integration times (depend on sampling frequency).
  83. */
  84. struct as73211_data {
  85. struct i2c_client *client;
  86. u8 osr;
  87. u8 creg1;
  88. u8 creg2;
  89. u8 creg3;
  90. struct mutex mutex;
  91. struct completion completion;
  92. int int_time_avail[AS73211_SAMPLE_TIME_NUM * 2];
  93. };
  94. #define AS73211_COLOR_CHANNEL(_color, _si, _addr) { \
  95. .type = IIO_INTENSITY, \
  96. .modified = 1, \
  97. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \
  98. .info_mask_shared_by_type = \
  99. BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  100. BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
  101. BIT(IIO_CHAN_INFO_INT_TIME), \
  102. .info_mask_shared_by_type_available = \
  103. BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  104. BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
  105. BIT(IIO_CHAN_INFO_INT_TIME), \
  106. .channel2 = IIO_MOD_##_color, \
  107. .address = _addr, \
  108. .scan_index = _si, \
  109. .scan_type = { \
  110. .sign = 'u', \
  111. .realbits = 16, \
  112. .storagebits = 16, \
  113. .endianness = IIO_LE, \
  114. }, \
  115. }
  116. #define AS73211_OFFSET_TEMP_INT (-66)
  117. #define AS73211_OFFSET_TEMP_MICRO 900000
  118. #define AS73211_SCALE_TEMP_INT 0
  119. #define AS73211_SCALE_TEMP_MICRO 50000
  120. #define AS73211_SCALE_X 277071108 /* nW/m^2 */
  121. #define AS73211_SCALE_Y 298384270 /* nW/m^2 */
  122. #define AS73211_SCALE_Z 160241927 /* nW/m^2 */
  123. /* Channel order MUST match devices result register order */
  124. #define AS73211_SCAN_INDEX_TEMP 0
  125. #define AS73211_SCAN_INDEX_X 1
  126. #define AS73211_SCAN_INDEX_Y 2
  127. #define AS73211_SCAN_INDEX_Z 3
  128. #define AS73211_SCAN_INDEX_TS 4
  129. #define AS73211_SCAN_MASK_COLOR ( \
  130. BIT(AS73211_SCAN_INDEX_X) | \
  131. BIT(AS73211_SCAN_INDEX_Y) | \
  132. BIT(AS73211_SCAN_INDEX_Z))
  133. #define AS73211_SCAN_MASK_ALL ( \
  134. BIT(AS73211_SCAN_INDEX_TEMP) | \
  135. AS73211_SCAN_MASK_COLOR)
  136. static const struct iio_chan_spec as73211_channels[] = {
  137. {
  138. .type = IIO_TEMP,
  139. .info_mask_separate =
  140. BIT(IIO_CHAN_INFO_RAW) |
  141. BIT(IIO_CHAN_INFO_OFFSET) |
  142. BIT(IIO_CHAN_INFO_SCALE),
  143. .address = AS73211_OUT_TEMP,
  144. .scan_index = AS73211_SCAN_INDEX_TEMP,
  145. .scan_type = {
  146. .sign = 'u',
  147. .realbits = 16,
  148. .storagebits = 16,
  149. .endianness = IIO_LE,
  150. }
  151. },
  152. AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
  153. AS73211_COLOR_CHANNEL(Y, AS73211_SCAN_INDEX_Y, AS73211_OUT_MRES2),
  154. AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
  155. IIO_CHAN_SOFT_TIMESTAMP(AS73211_SCAN_INDEX_TS),
  156. };
  157. static unsigned int as73211_integration_time_1024cyc(struct as73211_data *data)
  158. {
  159. /*
  160. * Return integration time in units of 1024 clock cycles. Integration time
  161. * in CREG1 is in powers of 2 (x 1024 cycles).
  162. */
  163. return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
  164. }
  165. static unsigned int as73211_integration_time_us(struct as73211_data *data,
  166. unsigned int integration_time_1024cyc)
  167. {
  168. /*
  169. * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
  170. * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
  171. * t_int_us = 1 / (f_samp) * t_cycl * US_PER_SEC
  172. * = 1 / (2^CREG3_CCLK * 1,024,000) * 2^CREG1_CYCLES * 1,024 * US_PER_SEC
  173. * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
  174. * In order to get rid of negative exponents, we extend the "fraction"
  175. * by 2^3 (CREG3_CCLK,max = 3)
  176. * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
  177. */
  178. return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
  179. integration_time_1024cyc * 125;
  180. }
  181. static void as73211_integration_time_calc_avail(struct as73211_data *data)
  182. {
  183. int i;
  184. for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) {
  185. unsigned int time_us = as73211_integration_time_us(data, BIT(i));
  186. data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
  187. data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
  188. }
  189. }
  190. static unsigned int as73211_gain(struct as73211_data *data)
  191. {
  192. /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
  193. return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
  194. }
  195. /* must be called with as73211_data::mutex held. */
  196. static int as73211_req_data(struct as73211_data *data)
  197. {
  198. unsigned int time_us = as73211_integration_time_us(data,
  199. as73211_integration_time_1024cyc(data));
  200. struct device *dev = &data->client->dev;
  201. union i2c_smbus_data smbus_data;
  202. u16 osr_status;
  203. int ret;
  204. if (data->client->irq)
  205. reinit_completion(&data->completion);
  206. /*
  207. * During measurement, there should be no traffic on the i2c bus as the
  208. * electrical noise would disturb the measurement process.
  209. */
  210. i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
  211. data->osr &= ~AS73211_OSR_DOS_MASK;
  212. data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
  213. smbus_data.byte = data->osr;
  214. ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
  215. data->client->flags, I2C_SMBUS_WRITE,
  216. AS73211_REG_OSR, I2C_SMBUS_BYTE_DATA, &smbus_data);
  217. if (ret < 0) {
  218. i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
  219. return ret;
  220. }
  221. /*
  222. * Reset AS73211_OSR_SS (is self clearing) in order to avoid unintentional
  223. * triggering of further measurements later.
  224. */
  225. data->osr &= ~AS73211_OSR_SS;
  226. /*
  227. * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%.
  228. */
  229. time_us += time_us / 3;
  230. if (data->client->irq) {
  231. ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
  232. if (!ret) {
  233. dev_err(dev, "timeout waiting for READY IRQ\n");
  234. i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
  235. return -ETIMEDOUT;
  236. }
  237. } else {
  238. /* Wait integration time */
  239. usleep_range(time_us, 2 * time_us);
  240. }
  241. i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
  242. ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
  243. if (ret < 0)
  244. return ret;
  245. osr_status = ret;
  246. if (osr_status != (AS73211_OSR_DOS_MEASURE | AS73211_OSR_STATUS_NDATA)) {
  247. if (osr_status & AS73211_OSR_SS) {
  248. dev_err(dev, "%s() Measurement has not stopped\n", __func__);
  249. return -ETIME;
  250. }
  251. if (osr_status & AS73211_OSR_STATUS_NOTREADY) {
  252. dev_err(dev, "%s() Data is not ready\n", __func__);
  253. return -ENODATA;
  254. }
  255. if (!(osr_status & AS73211_OSR_STATUS_NDATA)) {
  256. dev_err(dev, "%s() No new data available\n", __func__);
  257. return -ENODATA;
  258. }
  259. if (osr_status & AS73211_OSR_STATUS_LDATA) {
  260. dev_err(dev, "%s() Result buffer overrun\n", __func__);
  261. return -ENOBUFS;
  262. }
  263. if (osr_status & AS73211_OSR_STATUS_ADCOF) {
  264. dev_err(dev, "%s() ADC overflow\n", __func__);
  265. return -EOVERFLOW;
  266. }
  267. if (osr_status & AS73211_OSR_STATUS_MRESOF) {
  268. dev_err(dev, "%s() Measurement result overflow\n", __func__);
  269. return -EOVERFLOW;
  270. }
  271. if (osr_status & AS73211_OSR_STATUS_OUTCONVOF) {
  272. dev_err(dev, "%s() Timer overflow\n", __func__);
  273. return -EOVERFLOW;
  274. }
  275. dev_err(dev, "%s() Unexpected status value\n", __func__);
  276. return -EIO;
  277. }
  278. return 0;
  279. }
  280. static int as73211_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
  281. int *val, int *val2, long mask)
  282. {
  283. struct as73211_data *data = iio_priv(indio_dev);
  284. switch (mask) {
  285. case IIO_CHAN_INFO_RAW: {
  286. int ret;
  287. ret = iio_device_claim_direct_mode(indio_dev);
  288. if (ret < 0)
  289. return ret;
  290. ret = as73211_req_data(data);
  291. if (ret < 0) {
  292. iio_device_release_direct_mode(indio_dev);
  293. return ret;
  294. }
  295. ret = i2c_smbus_read_word_data(data->client, chan->address);
  296. iio_device_release_direct_mode(indio_dev);
  297. if (ret < 0)
  298. return ret;
  299. *val = ret;
  300. return IIO_VAL_INT;
  301. }
  302. case IIO_CHAN_INFO_OFFSET:
  303. *val = AS73211_OFFSET_TEMP_INT;
  304. *val2 = AS73211_OFFSET_TEMP_MICRO;
  305. return IIO_VAL_INT_PLUS_MICRO;
  306. case IIO_CHAN_INFO_SCALE:
  307. switch (chan->type) {
  308. case IIO_TEMP:
  309. *val = AS73211_SCALE_TEMP_INT;
  310. *val2 = AS73211_SCALE_TEMP_MICRO;
  311. return IIO_VAL_INT_PLUS_MICRO;
  312. case IIO_INTENSITY: {
  313. unsigned int scale;
  314. switch (chan->channel2) {
  315. case IIO_MOD_X:
  316. scale = AS73211_SCALE_X;
  317. break;
  318. case IIO_MOD_Y:
  319. scale = AS73211_SCALE_Y;
  320. break;
  321. case IIO_MOD_Z:
  322. scale = AS73211_SCALE_Z;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. scale /= as73211_gain(data);
  328. scale /= as73211_integration_time_1024cyc(data);
  329. *val = scale;
  330. return IIO_VAL_INT;
  331. default:
  332. return -EINVAL;
  333. }}
  334. case IIO_CHAN_INFO_SAMP_FREQ:
  335. /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
  336. *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
  337. AS73211_SAMPLE_FREQ_BASE;
  338. return IIO_VAL_INT;
  339. case IIO_CHAN_INFO_HARDWAREGAIN:
  340. *val = as73211_gain(data);
  341. return IIO_VAL_INT;
  342. case IIO_CHAN_INFO_INT_TIME: {
  343. unsigned int time_us;
  344. mutex_lock(&data->mutex);
  345. time_us = as73211_integration_time_us(data, as73211_integration_time_1024cyc(data));
  346. mutex_unlock(&data->mutex);
  347. *val = time_us / USEC_PER_SEC;
  348. *val2 = time_us % USEC_PER_SEC;
  349. return IIO_VAL_INT_PLUS_MICRO;
  350. default:
  351. return -EINVAL;
  352. }}
  353. }
  354. static int as73211_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
  355. const int **vals, int *type, int *length, long mask)
  356. {
  357. struct as73211_data *data = iio_priv(indio_dev);
  358. switch (mask) {
  359. case IIO_CHAN_INFO_SAMP_FREQ:
  360. *length = ARRAY_SIZE(as73211_samp_freq_avail);
  361. *vals = as73211_samp_freq_avail;
  362. *type = IIO_VAL_INT;
  363. return IIO_AVAIL_LIST;
  364. case IIO_CHAN_INFO_HARDWAREGAIN:
  365. *length = ARRAY_SIZE(as73211_hardwaregain_avail);
  366. *vals = as73211_hardwaregain_avail;
  367. *type = IIO_VAL_INT;
  368. return IIO_AVAIL_LIST;
  369. case IIO_CHAN_INFO_INT_TIME:
  370. *length = ARRAY_SIZE(data->int_time_avail);
  371. *vals = data->int_time_avail;
  372. *type = IIO_VAL_INT_PLUS_MICRO;
  373. return IIO_AVAIL_LIST;
  374. default:
  375. return -EINVAL;
  376. }
  377. }
  378. static int _as73211_write_raw(struct iio_dev *indio_dev,
  379. struct iio_chan_spec const *chan __always_unused,
  380. int val, int val2, long mask)
  381. {
  382. struct as73211_data *data = iio_priv(indio_dev);
  383. int ret;
  384. switch (mask) {
  385. case IIO_CHAN_INFO_SAMP_FREQ: {
  386. int reg_bits, freq_kHz = val / HZ_PER_KHZ; /* 1024, 2048, ... */
  387. /* val must be 1024 * 2^x */
  388. if (val < 0 || (freq_kHz * HZ_PER_KHZ) != val ||
  389. !is_power_of_2(freq_kHz) || val2)
  390. return -EINVAL;
  391. /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
  392. reg_bits = ilog2(freq_kHz) - 10;
  393. if (!FIELD_FIT(AS73211_CREG3_CCLK_MASK, reg_bits))
  394. return -EINVAL;
  395. data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
  396. data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
  397. as73211_integration_time_calc_avail(data);
  398. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
  399. if (ret < 0)
  400. return ret;
  401. return 0;
  402. }
  403. case IIO_CHAN_INFO_HARDWAREGAIN: {
  404. unsigned int reg_bits;
  405. if (val < 0 || !is_power_of_2(val) || val2)
  406. return -EINVAL;
  407. /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
  408. reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
  409. if (!FIELD_FIT(AS73211_CREG1_GAIN_MASK, reg_bits))
  410. return -EINVAL;
  411. data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
  412. data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
  413. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
  414. if (ret < 0)
  415. return ret;
  416. return 0;
  417. }
  418. case IIO_CHAN_INFO_INT_TIME: {
  419. int val_us = val * USEC_PER_SEC + val2;
  420. int time_ms;
  421. int reg_bits;
  422. /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
  423. int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
  424. /*
  425. * time_ms = time_us * US_PER_MS * f_samp_1_024mhz / MHZ_PER_HZ
  426. * = time_us * f_samp_1_024mhz / 1000
  427. */
  428. time_ms = (val_us * f_samp_1_024mhz) / 1000; /* 1 ms, 2 ms, ... (power of two) */
  429. if (time_ms < 0 || !is_power_of_2(time_ms) || time_ms > AS73211_SAMPLE_TIME_MAX_MS)
  430. return -EINVAL;
  431. reg_bits = ilog2(time_ms);
  432. if (!FIELD_FIT(AS73211_CREG1_TIME_MASK, reg_bits))
  433. return -EINVAL; /* not possible due to previous tests */
  434. data->creg1 &= ~AS73211_CREG1_TIME_MASK;
  435. data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
  436. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
  437. if (ret < 0)
  438. return ret;
  439. return 0;
  440. default:
  441. return -EINVAL;
  442. }}
  443. }
  444. static int as73211_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
  445. int val, int val2, long mask)
  446. {
  447. struct as73211_data *data = iio_priv(indio_dev);
  448. int ret;
  449. mutex_lock(&data->mutex);
  450. ret = iio_device_claim_direct_mode(indio_dev);
  451. if (ret < 0)
  452. goto error_unlock;
  453. /* Need to switch to config mode ... */
  454. if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
  455. data->osr &= ~AS73211_OSR_DOS_MASK;
  456. data->osr |= AS73211_OSR_DOS_CONFIG;
  457. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
  458. if (ret < 0)
  459. goto error_release;
  460. }
  461. ret = _as73211_write_raw(indio_dev, chan, val, val2, mask);
  462. error_release:
  463. iio_device_release_direct_mode(indio_dev);
  464. error_unlock:
  465. mutex_unlock(&data->mutex);
  466. return ret;
  467. }
  468. static irqreturn_t as73211_ready_handler(int irq __always_unused, void *priv)
  469. {
  470. struct as73211_data *data = iio_priv(priv);
  471. complete(&data->completion);
  472. return IRQ_HANDLED;
  473. }
  474. static irqreturn_t as73211_trigger_handler(int irq __always_unused, void *p)
  475. {
  476. struct iio_poll_func *pf = p;
  477. struct iio_dev *indio_dev = pf->indio_dev;
  478. struct as73211_data *data = iio_priv(indio_dev);
  479. struct {
  480. __le16 chan[4];
  481. s64 ts __aligned(8);
  482. } scan;
  483. int data_result, ret;
  484. mutex_lock(&data->mutex);
  485. data_result = as73211_req_data(data);
  486. if (data_result < 0 && data_result != -EOVERFLOW)
  487. goto done; /* don't push any data for errors other than EOVERFLOW */
  488. if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
  489. /* Optimization for reading all (color + temperature) channels */
  490. u8 addr = as73211_channels[0].address;
  491. struct i2c_msg msgs[] = {
  492. {
  493. .addr = data->client->addr,
  494. .flags = 0,
  495. .len = 1,
  496. .buf = &addr,
  497. },
  498. {
  499. .addr = data->client->addr,
  500. .flags = I2C_M_RD,
  501. .len = sizeof(scan.chan),
  502. .buf = (u8 *)&scan.chan,
  503. },
  504. };
  505. ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
  506. if (ret < 0)
  507. goto done;
  508. } else {
  509. /* Optimization for reading only color channels */
  510. /* AS73211 starts reading at address 2 */
  511. ret = i2c_master_recv(data->client,
  512. (char *)&scan.chan[1], 3 * sizeof(scan.chan[1]));
  513. if (ret < 0)
  514. goto done;
  515. }
  516. if (data_result) {
  517. /*
  518. * Saturate all channels (in case of overflows). Temperature channel
  519. * is not affected by overflows.
  520. */
  521. scan.chan[1] = cpu_to_le16(U16_MAX);
  522. scan.chan[2] = cpu_to_le16(U16_MAX);
  523. scan.chan[3] = cpu_to_le16(U16_MAX);
  524. }
  525. iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev));
  526. done:
  527. mutex_unlock(&data->mutex);
  528. iio_trigger_notify_done(indio_dev->trig);
  529. return IRQ_HANDLED;
  530. }
  531. static const struct iio_info as73211_info = {
  532. .read_raw = as73211_read_raw,
  533. .read_avail = as73211_read_avail,
  534. .write_raw = as73211_write_raw,
  535. };
  536. static int as73211_power(struct iio_dev *indio_dev, bool state)
  537. {
  538. struct as73211_data *data = iio_priv(indio_dev);
  539. int ret;
  540. mutex_lock(&data->mutex);
  541. if (state)
  542. data->osr &= ~AS73211_OSR_PD;
  543. else
  544. data->osr |= AS73211_OSR_PD;
  545. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
  546. mutex_unlock(&data->mutex);
  547. if (ret < 0)
  548. return ret;
  549. return 0;
  550. }
  551. static void as73211_power_disable(void *data)
  552. {
  553. struct iio_dev *indio_dev = data;
  554. as73211_power(indio_dev, false);
  555. }
  556. static int as73211_probe(struct i2c_client *client)
  557. {
  558. struct device *dev = &client->dev;
  559. struct as73211_data *data;
  560. struct iio_dev *indio_dev;
  561. int ret;
  562. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  563. if (!indio_dev)
  564. return -ENOMEM;
  565. data = iio_priv(indio_dev);
  566. i2c_set_clientdata(client, indio_dev);
  567. data->client = client;
  568. mutex_init(&data->mutex);
  569. init_completion(&data->completion);
  570. indio_dev->info = &as73211_info;
  571. indio_dev->name = AS73211_DRV_NAME;
  572. indio_dev->channels = as73211_channels;
  573. indio_dev->num_channels = ARRAY_SIZE(as73211_channels);
  574. indio_dev->modes = INDIO_DIRECT_MODE;
  575. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
  576. if (ret < 0)
  577. return ret;
  578. data->osr = ret;
  579. /* reset device */
  580. data->osr |= AS73211_OSR_SW_RES;
  581. ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
  582. if (ret < 0)
  583. return ret;
  584. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
  585. if (ret < 0)
  586. return ret;
  587. data->osr = ret;
  588. /*
  589. * Reading AGEN is only possible after reset (AGEN is not available if
  590. * device is in measurement mode).
  591. */
  592. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
  593. if (ret < 0)
  594. return ret;
  595. /* At the time of writing this driver, only DEVID 2 and MUT 1 are known. */
  596. if ((ret & AS73211_AGEN_DEVID_MASK) != AS73211_AGEN_DEVID(2) ||
  597. (ret & AS73211_AGEN_MUT_MASK) != AS73211_AGEN_MUT(1))
  598. return -ENODEV;
  599. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
  600. if (ret < 0)
  601. return ret;
  602. data->creg1 = ret;
  603. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
  604. if (ret < 0)
  605. return ret;
  606. data->creg2 = ret;
  607. ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
  608. if (ret < 0)
  609. return ret;
  610. data->creg3 = ret;
  611. as73211_integration_time_calc_avail(data);
  612. ret = as73211_power(indio_dev, true);
  613. if (ret < 0)
  614. return ret;
  615. ret = devm_add_action_or_reset(dev, as73211_power_disable, indio_dev);
  616. if (ret)
  617. return ret;
  618. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, as73211_trigger_handler, NULL);
  619. if (ret)
  620. return ret;
  621. if (client->irq) {
  622. ret = devm_request_threaded_irq(&client->dev, client->irq,
  623. NULL,
  624. as73211_ready_handler,
  625. IRQF_ONESHOT,
  626. client->name, indio_dev);
  627. if (ret)
  628. return ret;
  629. }
  630. return devm_iio_device_register(dev, indio_dev);
  631. }
  632. static int as73211_suspend(struct device *dev)
  633. {
  634. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  635. return as73211_power(indio_dev, false);
  636. }
  637. static int as73211_resume(struct device *dev)
  638. {
  639. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  640. return as73211_power(indio_dev, true);
  641. }
  642. static DEFINE_SIMPLE_DEV_PM_OPS(as73211_pm_ops, as73211_suspend,
  643. as73211_resume);
  644. static const struct of_device_id as73211_of_match[] = {
  645. { .compatible = "ams,as73211" },
  646. { }
  647. };
  648. MODULE_DEVICE_TABLE(of, as73211_of_match);
  649. static const struct i2c_device_id as73211_id[] = {
  650. { "as73211", 0 },
  651. { }
  652. };
  653. MODULE_DEVICE_TABLE(i2c, as73211_id);
  654. static struct i2c_driver as73211_driver = {
  655. .driver = {
  656. .name = AS73211_DRV_NAME,
  657. .of_match_table = as73211_of_match,
  658. .pm = pm_sleep_ptr(&as73211_pm_ops),
  659. },
  660. .probe_new = as73211_probe,
  661. .id_table = as73211_id,
  662. };
  663. module_i2c_driver(as73211_driver);
  664. MODULE_AUTHOR("Christian Eggers <[email protected]>");
  665. MODULE_DESCRIPTION("AS73211 XYZ True Color Sensor driver");
  666. MODULE_LICENSE("GPL");