inv_icm42600_gyro.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2020 Invensense, Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/device.h>
  7. #include <linux/mutex.h>
  8. #include <linux/pm_runtime.h>
  9. #include <linux/regmap.h>
  10. #include <linux/delay.h>
  11. #include <linux/math64.h>
  12. #include <linux/iio/buffer.h>
  13. #include <linux/iio/common/inv_sensors_timestamp.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/kfifo_buf.h>
  16. #include "inv_icm42600.h"
  17. #include "inv_icm42600_temp.h"
  18. #include "inv_icm42600_buffer.h"
  19. #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info) \
  20. { \
  21. .type = IIO_ANGL_VEL, \
  22. .modified = 1, \
  23. .channel2 = _modifier, \
  24. .info_mask_separate = \
  25. BIT(IIO_CHAN_INFO_RAW) | \
  26. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  27. .info_mask_shared_by_type = \
  28. BIT(IIO_CHAN_INFO_SCALE), \
  29. .info_mask_shared_by_type_available = \
  30. BIT(IIO_CHAN_INFO_SCALE) | \
  31. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  32. .info_mask_shared_by_all = \
  33. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  34. .info_mask_shared_by_all_available = \
  35. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  36. .scan_index = _index, \
  37. .scan_type = { \
  38. .sign = 's', \
  39. .realbits = 16, \
  40. .storagebits = 16, \
  41. .endianness = IIO_BE, \
  42. }, \
  43. .ext_info = _ext_info, \
  44. }
  45. enum inv_icm42600_gyro_scan {
  46. INV_ICM42600_GYRO_SCAN_X,
  47. INV_ICM42600_GYRO_SCAN_Y,
  48. INV_ICM42600_GYRO_SCAN_Z,
  49. INV_ICM42600_GYRO_SCAN_TEMP,
  50. INV_ICM42600_GYRO_SCAN_TIMESTAMP,
  51. };
  52. static const struct iio_chan_spec_ext_info inv_icm42600_gyro_ext_infos[] = {
  53. IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
  54. {},
  55. };
  56. static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
  57. INV_ICM42600_GYRO_CHAN(IIO_MOD_X, INV_ICM42600_GYRO_SCAN_X,
  58. inv_icm42600_gyro_ext_infos),
  59. INV_ICM42600_GYRO_CHAN(IIO_MOD_Y, INV_ICM42600_GYRO_SCAN_Y,
  60. inv_icm42600_gyro_ext_infos),
  61. INV_ICM42600_GYRO_CHAN(IIO_MOD_Z, INV_ICM42600_GYRO_SCAN_Z,
  62. inv_icm42600_gyro_ext_infos),
  63. INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
  64. IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_GYRO_SCAN_TIMESTAMP),
  65. };
  66. /*
  67. * IIO buffer data: size must be a power of 2 and timestamp aligned
  68. * 16 bytes: 6 bytes angular velocity, 2 bytes temperature, 8 bytes timestamp
  69. */
  70. struct inv_icm42600_gyro_buffer {
  71. struct inv_icm42600_fifo_sensor_data gyro;
  72. int16_t temp;
  73. int64_t timestamp __aligned(8);
  74. };
  75. #define INV_ICM42600_SCAN_MASK_GYRO_3AXIS \
  76. (BIT(INV_ICM42600_GYRO_SCAN_X) | \
  77. BIT(INV_ICM42600_GYRO_SCAN_Y) | \
  78. BIT(INV_ICM42600_GYRO_SCAN_Z))
  79. #define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_GYRO_SCAN_TEMP)
  80. static const unsigned long inv_icm42600_gyro_scan_masks[] = {
  81. /* 3-axis gyro + temperature */
  82. INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
  83. 0,
  84. };
  85. /* enable gyroscope sensor and FIFO write */
  86. static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
  87. const unsigned long *scan_mask)
  88. {
  89. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  90. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  91. unsigned int fifo_en = 0;
  92. unsigned int sleep_gyro = 0;
  93. unsigned int sleep_temp = 0;
  94. unsigned int sleep;
  95. int ret;
  96. mutex_lock(&st->lock);
  97. if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
  98. /* enable temp sensor */
  99. ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
  100. if (ret)
  101. goto out_unlock;
  102. fifo_en |= INV_ICM42600_SENSOR_TEMP;
  103. }
  104. if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
  105. /* enable gyro sensor */
  106. conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
  107. ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
  108. if (ret)
  109. goto out_unlock;
  110. fifo_en |= INV_ICM42600_SENSOR_GYRO;
  111. }
  112. /* update data FIFO write */
  113. ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
  114. out_unlock:
  115. mutex_unlock(&st->lock);
  116. /* sleep maximum required time */
  117. sleep = max(sleep_gyro, sleep_temp);
  118. if (sleep)
  119. msleep(sleep);
  120. return ret;
  121. }
  122. static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
  123. struct iio_chan_spec const *chan,
  124. int16_t *val)
  125. {
  126. struct device *dev = regmap_get_device(st->map);
  127. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  128. unsigned int reg;
  129. __be16 *data;
  130. int ret;
  131. if (chan->type != IIO_ANGL_VEL)
  132. return -EINVAL;
  133. switch (chan->channel2) {
  134. case IIO_MOD_X:
  135. reg = INV_ICM42600_REG_GYRO_DATA_X;
  136. break;
  137. case IIO_MOD_Y:
  138. reg = INV_ICM42600_REG_GYRO_DATA_Y;
  139. break;
  140. case IIO_MOD_Z:
  141. reg = INV_ICM42600_REG_GYRO_DATA_Z;
  142. break;
  143. default:
  144. return -EINVAL;
  145. }
  146. pm_runtime_get_sync(dev);
  147. mutex_lock(&st->lock);
  148. /* enable gyro sensor */
  149. conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
  150. ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
  151. if (ret)
  152. goto exit;
  153. /* read gyro register data */
  154. data = (__be16 *)&st->buffer[0];
  155. ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
  156. if (ret)
  157. goto exit;
  158. *val = (int16_t)be16_to_cpup(data);
  159. if (*val == INV_ICM42600_DATA_INVALID)
  160. ret = -EINVAL;
  161. exit:
  162. mutex_unlock(&st->lock);
  163. pm_runtime_mark_last_busy(dev);
  164. pm_runtime_put_autosuspend(dev);
  165. return ret;
  166. }
  167. /* IIO format int + nano */
  168. static const int inv_icm42600_gyro_scale[] = {
  169. /* +/- 2000dps => 0.001065264 rad/s */
  170. [2 * INV_ICM42600_GYRO_FS_2000DPS] = 0,
  171. [2 * INV_ICM42600_GYRO_FS_2000DPS + 1] = 1065264,
  172. /* +/- 1000dps => 0.000532632 rad/s */
  173. [2 * INV_ICM42600_GYRO_FS_1000DPS] = 0,
  174. [2 * INV_ICM42600_GYRO_FS_1000DPS + 1] = 532632,
  175. /* +/- 500dps => 0.000266316 rad/s */
  176. [2 * INV_ICM42600_GYRO_FS_500DPS] = 0,
  177. [2 * INV_ICM42600_GYRO_FS_500DPS + 1] = 266316,
  178. /* +/- 250dps => 0.000133158 rad/s */
  179. [2 * INV_ICM42600_GYRO_FS_250DPS] = 0,
  180. [2 * INV_ICM42600_GYRO_FS_250DPS + 1] = 133158,
  181. /* +/- 125dps => 0.000066579 rad/s */
  182. [2 * INV_ICM42600_GYRO_FS_125DPS] = 0,
  183. [2 * INV_ICM42600_GYRO_FS_125DPS + 1] = 66579,
  184. /* +/- 62.5dps => 0.000033290 rad/s */
  185. [2 * INV_ICM42600_GYRO_FS_62_5DPS] = 0,
  186. [2 * INV_ICM42600_GYRO_FS_62_5DPS + 1] = 33290,
  187. /* +/- 31.25dps => 0.000016645 rad/s */
  188. [2 * INV_ICM42600_GYRO_FS_31_25DPS] = 0,
  189. [2 * INV_ICM42600_GYRO_FS_31_25DPS + 1] = 16645,
  190. /* +/- 15.625dps => 0.000008322 rad/s */
  191. [2 * INV_ICM42600_GYRO_FS_15_625DPS] = 0,
  192. [2 * INV_ICM42600_GYRO_FS_15_625DPS + 1] = 8322,
  193. };
  194. static const int inv_icm42686_gyro_scale[] = {
  195. /* +/- 4000dps => 0.002130529 rad/s */
  196. [2 * INV_ICM42686_GYRO_FS_4000DPS] = 0,
  197. [2 * INV_ICM42686_GYRO_FS_4000DPS + 1] = 2130529,
  198. /* +/- 2000dps => 0.001065264 rad/s */
  199. [2 * INV_ICM42686_GYRO_FS_2000DPS] = 0,
  200. [2 * INV_ICM42686_GYRO_FS_2000DPS + 1] = 1065264,
  201. /* +/- 1000dps => 0.000532632 rad/s */
  202. [2 * INV_ICM42686_GYRO_FS_1000DPS] = 0,
  203. [2 * INV_ICM42686_GYRO_FS_1000DPS + 1] = 532632,
  204. /* +/- 500dps => 0.000266316 rad/s */
  205. [2 * INV_ICM42686_GYRO_FS_500DPS] = 0,
  206. [2 * INV_ICM42686_GYRO_FS_500DPS + 1] = 266316,
  207. /* +/- 250dps => 0.000133158 rad/s */
  208. [2 * INV_ICM42686_GYRO_FS_250DPS] = 0,
  209. [2 * INV_ICM42686_GYRO_FS_250DPS + 1] = 133158,
  210. /* +/- 125dps => 0.000066579 rad/s */
  211. [2 * INV_ICM42686_GYRO_FS_125DPS] = 0,
  212. [2 * INV_ICM42686_GYRO_FS_125DPS + 1] = 66579,
  213. /* +/- 62.5dps => 0.000033290 rad/s */
  214. [2 * INV_ICM42686_GYRO_FS_62_5DPS] = 0,
  215. [2 * INV_ICM42686_GYRO_FS_62_5DPS + 1] = 33290,
  216. /* +/- 31.25dps => 0.000016645 rad/s */
  217. [2 * INV_ICM42686_GYRO_FS_31_25DPS] = 0,
  218. [2 * INV_ICM42686_GYRO_FS_31_25DPS + 1] = 16645,
  219. };
  220. static int inv_icm42600_gyro_read_scale(struct iio_dev *indio_dev,
  221. int *val, int *val2)
  222. {
  223. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  224. struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
  225. unsigned int idx;
  226. idx = st->conf.gyro.fs;
  227. *val = gyro_st->scales[2 * idx];
  228. *val2 = gyro_st->scales[2 * idx + 1];
  229. return IIO_VAL_INT_PLUS_NANO;
  230. }
  231. static int inv_icm42600_gyro_write_scale(struct iio_dev *indio_dev,
  232. int val, int val2)
  233. {
  234. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  235. struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
  236. struct device *dev = regmap_get_device(st->map);
  237. unsigned int idx;
  238. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  239. int ret;
  240. for (idx = 0; idx < gyro_st->scales_len; idx += 2) {
  241. if (val == gyro_st->scales[idx] &&
  242. val2 == gyro_st->scales[idx + 1])
  243. break;
  244. }
  245. if (idx >= gyro_st->scales_len)
  246. return -EINVAL;
  247. conf.fs = idx / 2;
  248. pm_runtime_get_sync(dev);
  249. mutex_lock(&st->lock);
  250. ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
  251. mutex_unlock(&st->lock);
  252. pm_runtime_mark_last_busy(dev);
  253. pm_runtime_put_autosuspend(dev);
  254. return ret;
  255. }
  256. /* IIO format int + micro */
  257. static const int inv_icm42600_gyro_odr[] = {
  258. /* 12.5Hz */
  259. 12, 500000,
  260. /* 25Hz */
  261. 25, 0,
  262. /* 50Hz */
  263. 50, 0,
  264. /* 100Hz */
  265. 100, 0,
  266. /* 200Hz */
  267. 200, 0,
  268. /* 1kHz */
  269. 1000, 0,
  270. /* 2kHz */
  271. 2000, 0,
  272. /* 4kHz */
  273. 4000, 0,
  274. };
  275. static const int inv_icm42600_gyro_odr_conv[] = {
  276. INV_ICM42600_ODR_12_5HZ,
  277. INV_ICM42600_ODR_25HZ,
  278. INV_ICM42600_ODR_50HZ,
  279. INV_ICM42600_ODR_100HZ,
  280. INV_ICM42600_ODR_200HZ,
  281. INV_ICM42600_ODR_1KHZ_LN,
  282. INV_ICM42600_ODR_2KHZ_LN,
  283. INV_ICM42600_ODR_4KHZ_LN,
  284. };
  285. static int inv_icm42600_gyro_read_odr(struct inv_icm42600_state *st,
  286. int *val, int *val2)
  287. {
  288. unsigned int odr;
  289. unsigned int i;
  290. odr = st->conf.gyro.odr;
  291. for (i = 0; i < ARRAY_SIZE(inv_icm42600_gyro_odr_conv); ++i) {
  292. if (inv_icm42600_gyro_odr_conv[i] == odr)
  293. break;
  294. }
  295. if (i >= ARRAY_SIZE(inv_icm42600_gyro_odr_conv))
  296. return -EINVAL;
  297. *val = inv_icm42600_gyro_odr[2 * i];
  298. *val2 = inv_icm42600_gyro_odr[2 * i + 1];
  299. return IIO_VAL_INT_PLUS_MICRO;
  300. }
  301. static int inv_icm42600_gyro_write_odr(struct iio_dev *indio_dev,
  302. int val, int val2)
  303. {
  304. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  305. struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
  306. struct inv_sensors_timestamp *ts = &gyro_st->ts;
  307. struct device *dev = regmap_get_device(st->map);
  308. unsigned int idx;
  309. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  310. int ret;
  311. for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_gyro_odr); idx += 2) {
  312. if (val == inv_icm42600_gyro_odr[idx] &&
  313. val2 == inv_icm42600_gyro_odr[idx + 1])
  314. break;
  315. }
  316. if (idx >= ARRAY_SIZE(inv_icm42600_gyro_odr))
  317. return -EINVAL;
  318. conf.odr = inv_icm42600_gyro_odr_conv[idx / 2];
  319. pm_runtime_get_sync(dev);
  320. mutex_lock(&st->lock);
  321. ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
  322. iio_buffer_enabled(indio_dev));
  323. if (ret)
  324. goto out_unlock;
  325. ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
  326. if (ret)
  327. goto out_unlock;
  328. inv_icm42600_buffer_update_fifo_period(st);
  329. inv_icm42600_buffer_update_watermark(st);
  330. out_unlock:
  331. mutex_unlock(&st->lock);
  332. pm_runtime_mark_last_busy(dev);
  333. pm_runtime_put_autosuspend(dev);
  334. return ret;
  335. }
  336. /*
  337. * Calibration bias values, IIO range format int + nano.
  338. * Value is limited to +/-64dps coded on 12 bits signed. Step is 1/32 dps.
  339. */
  340. static int inv_icm42600_gyro_calibbias[] = {
  341. -1, 117010721, /* min: -1.117010721 rad/s */
  342. 0, 545415, /* step: 0.000545415 rad/s */
  343. 1, 116465306, /* max: 1.116465306 rad/s */
  344. };
  345. static int inv_icm42600_gyro_read_offset(struct inv_icm42600_state *st,
  346. struct iio_chan_spec const *chan,
  347. int *val, int *val2)
  348. {
  349. struct device *dev = regmap_get_device(st->map);
  350. int64_t val64;
  351. int32_t bias;
  352. unsigned int reg;
  353. int16_t offset;
  354. uint8_t data[2];
  355. int ret;
  356. if (chan->type != IIO_ANGL_VEL)
  357. return -EINVAL;
  358. switch (chan->channel2) {
  359. case IIO_MOD_X:
  360. reg = INV_ICM42600_REG_OFFSET_USER0;
  361. break;
  362. case IIO_MOD_Y:
  363. reg = INV_ICM42600_REG_OFFSET_USER1;
  364. break;
  365. case IIO_MOD_Z:
  366. reg = INV_ICM42600_REG_OFFSET_USER3;
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. pm_runtime_get_sync(dev);
  372. mutex_lock(&st->lock);
  373. ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
  374. memcpy(data, st->buffer, sizeof(data));
  375. mutex_unlock(&st->lock);
  376. pm_runtime_mark_last_busy(dev);
  377. pm_runtime_put_autosuspend(dev);
  378. if (ret)
  379. return ret;
  380. /* 12 bits signed value */
  381. switch (chan->channel2) {
  382. case IIO_MOD_X:
  383. offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
  384. break;
  385. case IIO_MOD_Y:
  386. offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
  387. break;
  388. case IIO_MOD_Z:
  389. offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
  390. break;
  391. default:
  392. return -EINVAL;
  393. }
  394. /*
  395. * convert raw offset to dps then to rad/s
  396. * 12 bits signed raw max 64 to dps: 64 / 2048
  397. * dps to rad: Pi / 180
  398. * result in nano (1000000000)
  399. * (offset * 64 * Pi * 1000000000) / (2048 * 180)
  400. */
  401. val64 = (int64_t)offset * 64LL * 3141592653LL;
  402. /* for rounding, add + or - divisor (2048 * 180) divided by 2 */
  403. if (val64 >= 0)
  404. val64 += 2048 * 180 / 2;
  405. else
  406. val64 -= 2048 * 180 / 2;
  407. bias = div_s64(val64, 2048 * 180);
  408. *val = bias / 1000000000L;
  409. *val2 = bias % 1000000000L;
  410. return IIO_VAL_INT_PLUS_NANO;
  411. }
  412. static int inv_icm42600_gyro_write_offset(struct inv_icm42600_state *st,
  413. struct iio_chan_spec const *chan,
  414. int val, int val2)
  415. {
  416. struct device *dev = regmap_get_device(st->map);
  417. int64_t val64, min, max;
  418. unsigned int reg, regval;
  419. int16_t offset;
  420. int ret;
  421. if (chan->type != IIO_ANGL_VEL)
  422. return -EINVAL;
  423. switch (chan->channel2) {
  424. case IIO_MOD_X:
  425. reg = INV_ICM42600_REG_OFFSET_USER0;
  426. break;
  427. case IIO_MOD_Y:
  428. reg = INV_ICM42600_REG_OFFSET_USER1;
  429. break;
  430. case IIO_MOD_Z:
  431. reg = INV_ICM42600_REG_OFFSET_USER3;
  432. break;
  433. default:
  434. return -EINVAL;
  435. }
  436. /* inv_icm42600_gyro_calibbias: min - step - max in nano */
  437. min = (int64_t)inv_icm42600_gyro_calibbias[0] * 1000000000LL +
  438. (int64_t)inv_icm42600_gyro_calibbias[1];
  439. max = (int64_t)inv_icm42600_gyro_calibbias[4] * 1000000000LL +
  440. (int64_t)inv_icm42600_gyro_calibbias[5];
  441. val64 = (int64_t)val * 1000000000LL + (int64_t)val2;
  442. if (val64 < min || val64 > max)
  443. return -EINVAL;
  444. /*
  445. * convert rad/s to dps then to raw value
  446. * rad to dps: 180 / Pi
  447. * dps to raw 12 bits signed, max 64: 2048 / 64
  448. * val in nano (1000000000)
  449. * val * 180 * 2048 / (Pi * 1000000000 * 64)
  450. */
  451. val64 = val64 * 180LL * 2048LL;
  452. /* for rounding, add + or - divisor (3141592653 * 64) divided by 2 */
  453. if (val64 >= 0)
  454. val64 += 3141592653LL * 64LL / 2LL;
  455. else
  456. val64 -= 3141592653LL * 64LL / 2LL;
  457. offset = div64_s64(val64, 3141592653LL * 64LL);
  458. /* clamp value limited to 12 bits signed */
  459. if (offset < -2048)
  460. offset = -2048;
  461. else if (offset > 2047)
  462. offset = 2047;
  463. pm_runtime_get_sync(dev);
  464. mutex_lock(&st->lock);
  465. switch (chan->channel2) {
  466. case IIO_MOD_X:
  467. /* OFFSET_USER1 register is shared */
  468. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
  469. &regval);
  470. if (ret)
  471. goto out_unlock;
  472. st->buffer[0] = offset & 0xFF;
  473. st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
  474. break;
  475. case IIO_MOD_Y:
  476. /* OFFSET_USER1 register is shared */
  477. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER1,
  478. &regval);
  479. if (ret)
  480. goto out_unlock;
  481. st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
  482. st->buffer[1] = offset & 0xFF;
  483. break;
  484. case IIO_MOD_Z:
  485. /* OFFSET_USER4 register is shared */
  486. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
  487. &regval);
  488. if (ret)
  489. goto out_unlock;
  490. st->buffer[0] = offset & 0xFF;
  491. st->buffer[1] = (regval & 0xF0) | ((offset & 0xF00) >> 8);
  492. break;
  493. default:
  494. ret = -EINVAL;
  495. goto out_unlock;
  496. }
  497. ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
  498. out_unlock:
  499. mutex_unlock(&st->lock);
  500. pm_runtime_mark_last_busy(dev);
  501. pm_runtime_put_autosuspend(dev);
  502. return ret;
  503. }
  504. static int inv_icm42600_gyro_read_raw(struct iio_dev *indio_dev,
  505. struct iio_chan_spec const *chan,
  506. int *val, int *val2, long mask)
  507. {
  508. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  509. int16_t data;
  510. int ret;
  511. switch (chan->type) {
  512. case IIO_ANGL_VEL:
  513. break;
  514. case IIO_TEMP:
  515. return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
  516. default:
  517. return -EINVAL;
  518. }
  519. switch (mask) {
  520. case IIO_CHAN_INFO_RAW:
  521. ret = iio_device_claim_direct_mode(indio_dev);
  522. if (ret)
  523. return ret;
  524. ret = inv_icm42600_gyro_read_sensor(st, chan, &data);
  525. iio_device_release_direct_mode(indio_dev);
  526. if (ret)
  527. return ret;
  528. *val = data;
  529. return IIO_VAL_INT;
  530. case IIO_CHAN_INFO_SCALE:
  531. return inv_icm42600_gyro_read_scale(indio_dev, val, val2);
  532. case IIO_CHAN_INFO_SAMP_FREQ:
  533. return inv_icm42600_gyro_read_odr(st, val, val2);
  534. case IIO_CHAN_INFO_CALIBBIAS:
  535. return inv_icm42600_gyro_read_offset(st, chan, val, val2);
  536. default:
  537. return -EINVAL;
  538. }
  539. }
  540. static int inv_icm42600_gyro_read_avail(struct iio_dev *indio_dev,
  541. struct iio_chan_spec const *chan,
  542. const int **vals,
  543. int *type, int *length, long mask)
  544. {
  545. struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
  546. if (chan->type != IIO_ANGL_VEL)
  547. return -EINVAL;
  548. switch (mask) {
  549. case IIO_CHAN_INFO_SCALE:
  550. *vals = gyro_st->scales;
  551. *type = IIO_VAL_INT_PLUS_NANO;
  552. *length = gyro_st->scales_len;
  553. return IIO_AVAIL_LIST;
  554. case IIO_CHAN_INFO_SAMP_FREQ:
  555. *vals = inv_icm42600_gyro_odr;
  556. *type = IIO_VAL_INT_PLUS_MICRO;
  557. *length = ARRAY_SIZE(inv_icm42600_gyro_odr);
  558. return IIO_AVAIL_LIST;
  559. case IIO_CHAN_INFO_CALIBBIAS:
  560. *vals = inv_icm42600_gyro_calibbias;
  561. *type = IIO_VAL_INT_PLUS_NANO;
  562. return IIO_AVAIL_RANGE;
  563. default:
  564. return -EINVAL;
  565. }
  566. }
  567. static int inv_icm42600_gyro_write_raw(struct iio_dev *indio_dev,
  568. struct iio_chan_spec const *chan,
  569. int val, int val2, long mask)
  570. {
  571. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  572. int ret;
  573. if (chan->type != IIO_ANGL_VEL)
  574. return -EINVAL;
  575. switch (mask) {
  576. case IIO_CHAN_INFO_SCALE:
  577. ret = iio_device_claim_direct_mode(indio_dev);
  578. if (ret)
  579. return ret;
  580. ret = inv_icm42600_gyro_write_scale(indio_dev, val, val2);
  581. iio_device_release_direct_mode(indio_dev);
  582. return ret;
  583. case IIO_CHAN_INFO_SAMP_FREQ:
  584. return inv_icm42600_gyro_write_odr(indio_dev, val, val2);
  585. case IIO_CHAN_INFO_CALIBBIAS:
  586. ret = iio_device_claim_direct_mode(indio_dev);
  587. if (ret)
  588. return ret;
  589. ret = inv_icm42600_gyro_write_offset(st, chan, val, val2);
  590. iio_device_release_direct_mode(indio_dev);
  591. return ret;
  592. default:
  593. return -EINVAL;
  594. }
  595. }
  596. static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
  597. struct iio_chan_spec const *chan,
  598. long mask)
  599. {
  600. if (chan->type != IIO_ANGL_VEL)
  601. return -EINVAL;
  602. switch (mask) {
  603. case IIO_CHAN_INFO_SCALE:
  604. return IIO_VAL_INT_PLUS_NANO;
  605. case IIO_CHAN_INFO_SAMP_FREQ:
  606. return IIO_VAL_INT_PLUS_MICRO;
  607. case IIO_CHAN_INFO_CALIBBIAS:
  608. return IIO_VAL_INT_PLUS_NANO;
  609. default:
  610. return -EINVAL;
  611. }
  612. }
  613. static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
  614. unsigned int val)
  615. {
  616. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  617. int ret;
  618. mutex_lock(&st->lock);
  619. st->fifo.watermark.gyro = val;
  620. ret = inv_icm42600_buffer_update_watermark(st);
  621. mutex_unlock(&st->lock);
  622. return ret;
  623. }
  624. static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
  625. unsigned int count)
  626. {
  627. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  628. int ret;
  629. if (count == 0)
  630. return 0;
  631. mutex_lock(&st->lock);
  632. ret = inv_icm42600_buffer_hwfifo_flush(st, count);
  633. if (!ret)
  634. ret = st->fifo.nb.gyro;
  635. mutex_unlock(&st->lock);
  636. return ret;
  637. }
  638. static const struct iio_info inv_icm42600_gyro_info = {
  639. .read_raw = inv_icm42600_gyro_read_raw,
  640. .read_avail = inv_icm42600_gyro_read_avail,
  641. .write_raw = inv_icm42600_gyro_write_raw,
  642. .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
  643. .debugfs_reg_access = inv_icm42600_debugfs_reg,
  644. .update_scan_mode = inv_icm42600_gyro_update_scan_mode,
  645. .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
  646. .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
  647. };
  648. struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st)
  649. {
  650. struct device *dev = regmap_get_device(st->map);
  651. const char *name;
  652. struct inv_icm42600_sensor_state *gyro_st;
  653. struct inv_sensors_timestamp_chip ts_chip;
  654. struct iio_dev *indio_dev;
  655. int ret;
  656. name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
  657. if (!name)
  658. return ERR_PTR(-ENOMEM);
  659. indio_dev = devm_iio_device_alloc(dev, sizeof(*gyro_st));
  660. if (!indio_dev)
  661. return ERR_PTR(-ENOMEM);
  662. gyro_st = iio_priv(indio_dev);
  663. switch (st->chip) {
  664. case INV_CHIP_ICM42686:
  665. gyro_st->scales = inv_icm42686_gyro_scale;
  666. gyro_st->scales_len = ARRAY_SIZE(inv_icm42686_gyro_scale);
  667. break;
  668. default:
  669. gyro_st->scales = inv_icm42600_gyro_scale;
  670. gyro_st->scales_len = ARRAY_SIZE(inv_icm42600_gyro_scale);
  671. break;
  672. }
  673. /*
  674. * clock period is 32kHz (31250ns)
  675. * jitter is +/- 2% (20 per mille)
  676. */
  677. ts_chip.clock_period = 31250;
  678. ts_chip.jitter = 20;
  679. ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
  680. inv_sensors_timestamp_init(&gyro_st->ts, &ts_chip);
  681. iio_device_set_drvdata(indio_dev, st);
  682. indio_dev->name = name;
  683. indio_dev->info = &inv_icm42600_gyro_info;
  684. indio_dev->modes = INDIO_DIRECT_MODE;
  685. indio_dev->channels = inv_icm42600_gyro_channels;
  686. indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
  687. indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
  688. indio_dev->setup_ops = &inv_icm42600_buffer_ops;
  689. ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
  690. &inv_icm42600_buffer_ops);
  691. if (ret)
  692. return ERR_PTR(ret);
  693. ret = devm_iio_device_register(dev, indio_dev);
  694. if (ret)
  695. return ERR_PTR(ret);
  696. return indio_dev;
  697. }
  698. int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
  699. {
  700. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  701. struct inv_icm42600_sensor_state *gyro_st = iio_priv(indio_dev);
  702. struct inv_sensors_timestamp *ts = &gyro_st->ts;
  703. ssize_t i, size;
  704. unsigned int no;
  705. const void *accel, *gyro, *timestamp;
  706. const int8_t *temp;
  707. unsigned int odr;
  708. int64_t ts_val;
  709. struct inv_icm42600_gyro_buffer buffer;
  710. /* parse all fifo packets */
  711. for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
  712. size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
  713. &accel, &gyro, &temp, &timestamp, &odr);
  714. /* quit if error or FIFO is empty */
  715. if (size <= 0)
  716. return size;
  717. /* skip packet if no gyro data or data is invalid */
  718. if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
  719. continue;
  720. /* update odr */
  721. if (odr & INV_ICM42600_SENSOR_GYRO)
  722. inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
  723. st->fifo.nb.total, no);
  724. /* buffer is copied to userspace, zeroing it to avoid any data leak */
  725. memset(&buffer, 0, sizeof(buffer));
  726. memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
  727. /* convert 8 bits FIFO temperature in high resolution format */
  728. buffer.temp = temp ? (*temp * 64) : 0;
  729. ts_val = inv_sensors_timestamp_pop(ts);
  730. iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
  731. }
  732. return 0;
  733. }