inv_icm42600_accel.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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_ACCEL_CHAN(_modifier, _index, _ext_info) \
  20. { \
  21. .type = IIO_ACCEL, \
  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_accel_scan {
  46. INV_ICM42600_ACCEL_SCAN_X,
  47. INV_ICM42600_ACCEL_SCAN_Y,
  48. INV_ICM42600_ACCEL_SCAN_Z,
  49. INV_ICM42600_ACCEL_SCAN_TEMP,
  50. INV_ICM42600_ACCEL_SCAN_TIMESTAMP,
  51. };
  52. static const char * const inv_icm42600_accel_power_mode_items[] = {
  53. "low-noise",
  54. "low-power",
  55. };
  56. static const int inv_icm42600_accel_power_mode_values[] = {
  57. INV_ICM42600_SENSOR_MODE_LOW_NOISE,
  58. INV_ICM42600_SENSOR_MODE_LOW_POWER,
  59. };
  60. static const int inv_icm42600_accel_filter_values[] = {
  61. INV_ICM42600_FILTER_BW_ODR_DIV_2,
  62. INV_ICM42600_FILTER_AVG_16X,
  63. };
  64. static int inv_icm42600_accel_power_mode_set(struct iio_dev *indio_dev,
  65. const struct iio_chan_spec *chan,
  66. unsigned int idx)
  67. {
  68. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  69. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  70. int power_mode, filter;
  71. if (chan->type != IIO_ACCEL)
  72. return -EINVAL;
  73. if (idx >= ARRAY_SIZE(inv_icm42600_accel_power_mode_values))
  74. return -EINVAL;
  75. if (iio_buffer_enabled(indio_dev))
  76. return -EBUSY;
  77. power_mode = inv_icm42600_accel_power_mode_values[idx];
  78. filter = inv_icm42600_accel_filter_values[idx];
  79. guard(mutex)(&st->lock);
  80. /* prevent change if power mode is not supported by the ODR */
  81. switch (power_mode) {
  82. case INV_ICM42600_SENSOR_MODE_LOW_NOISE:
  83. if (st->conf.accel.odr >= INV_ICM42600_ODR_6_25HZ_LP &&
  84. st->conf.accel.odr <= INV_ICM42600_ODR_1_5625HZ_LP)
  85. return -EPERM;
  86. break;
  87. case INV_ICM42600_SENSOR_MODE_LOW_POWER:
  88. default:
  89. if (st->conf.accel.odr <= INV_ICM42600_ODR_1KHZ_LN)
  90. return -EPERM;
  91. break;
  92. }
  93. accel_st->power_mode = power_mode;
  94. accel_st->filter = filter;
  95. return 0;
  96. }
  97. static int inv_icm42600_accel_power_mode_get(struct iio_dev *indio_dev,
  98. const struct iio_chan_spec *chan)
  99. {
  100. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  101. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  102. unsigned int idx;
  103. int power_mode;
  104. if (chan->type != IIO_ACCEL)
  105. return -EINVAL;
  106. guard(mutex)(&st->lock);
  107. /* if sensor is on, returns actual power mode and not configured one */
  108. switch (st->conf.accel.mode) {
  109. case INV_ICM42600_SENSOR_MODE_LOW_POWER:
  110. case INV_ICM42600_SENSOR_MODE_LOW_NOISE:
  111. power_mode = st->conf.accel.mode;
  112. break;
  113. default:
  114. power_mode = accel_st->power_mode;
  115. break;
  116. }
  117. for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_accel_power_mode_values); ++idx) {
  118. if (power_mode == inv_icm42600_accel_power_mode_values[idx])
  119. break;
  120. }
  121. if (idx >= ARRAY_SIZE(inv_icm42600_accel_power_mode_values))
  122. return -EINVAL;
  123. return idx;
  124. }
  125. static const struct iio_enum inv_icm42600_accel_power_mode_enum = {
  126. .items = inv_icm42600_accel_power_mode_items,
  127. .num_items = ARRAY_SIZE(inv_icm42600_accel_power_mode_items),
  128. .set = inv_icm42600_accel_power_mode_set,
  129. .get = inv_icm42600_accel_power_mode_get,
  130. };
  131. static const struct iio_chan_spec_ext_info inv_icm42600_accel_ext_infos[] = {
  132. IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, inv_icm42600_get_mount_matrix),
  133. IIO_ENUM_AVAILABLE("power_mode", IIO_SHARED_BY_TYPE,
  134. &inv_icm42600_accel_power_mode_enum),
  135. IIO_ENUM("power_mode", IIO_SHARED_BY_TYPE,
  136. &inv_icm42600_accel_power_mode_enum),
  137. {},
  138. };
  139. static const struct iio_chan_spec inv_icm42600_accel_channels[] = {
  140. INV_ICM42600_ACCEL_CHAN(IIO_MOD_X, INV_ICM42600_ACCEL_SCAN_X,
  141. inv_icm42600_accel_ext_infos),
  142. INV_ICM42600_ACCEL_CHAN(IIO_MOD_Y, INV_ICM42600_ACCEL_SCAN_Y,
  143. inv_icm42600_accel_ext_infos),
  144. INV_ICM42600_ACCEL_CHAN(IIO_MOD_Z, INV_ICM42600_ACCEL_SCAN_Z,
  145. inv_icm42600_accel_ext_infos),
  146. INV_ICM42600_TEMP_CHAN(INV_ICM42600_ACCEL_SCAN_TEMP),
  147. IIO_CHAN_SOFT_TIMESTAMP(INV_ICM42600_ACCEL_SCAN_TIMESTAMP),
  148. };
  149. /*
  150. * IIO buffer data: size must be a power of 2 and timestamp aligned
  151. * 16 bytes: 6 bytes acceleration, 2 bytes temperature, 8 bytes timestamp
  152. */
  153. struct inv_icm42600_accel_buffer {
  154. struct inv_icm42600_fifo_sensor_data accel;
  155. s16 temp;
  156. aligned_s64 timestamp;
  157. };
  158. #define INV_ICM42600_SCAN_MASK_ACCEL_3AXIS \
  159. (BIT(INV_ICM42600_ACCEL_SCAN_X) | \
  160. BIT(INV_ICM42600_ACCEL_SCAN_Y) | \
  161. BIT(INV_ICM42600_ACCEL_SCAN_Z))
  162. #define INV_ICM42600_SCAN_MASK_TEMP BIT(INV_ICM42600_ACCEL_SCAN_TEMP)
  163. static const unsigned long inv_icm42600_accel_scan_masks[] = {
  164. /* 3-axis accel + temperature */
  165. INV_ICM42600_SCAN_MASK_ACCEL_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
  166. 0,
  167. };
  168. /* enable accelerometer sensor and FIFO write */
  169. static int inv_icm42600_accel_update_scan_mode(struct iio_dev *indio_dev,
  170. const unsigned long *scan_mask)
  171. {
  172. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  173. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  174. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  175. unsigned int fifo_en = 0;
  176. unsigned int sleep_temp = 0;
  177. unsigned int sleep_accel = 0;
  178. unsigned int sleep;
  179. int ret;
  180. mutex_lock(&st->lock);
  181. if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
  182. /* enable temp sensor */
  183. ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
  184. if (ret)
  185. goto out_unlock;
  186. fifo_en |= INV_ICM42600_SENSOR_TEMP;
  187. }
  188. if (*scan_mask & INV_ICM42600_SCAN_MASK_ACCEL_3AXIS) {
  189. /* enable accel sensor */
  190. conf.mode = accel_st->power_mode;
  191. conf.filter = accel_st->filter;
  192. ret = inv_icm42600_set_accel_conf(st, &conf, &sleep_accel);
  193. if (ret)
  194. goto out_unlock;
  195. fifo_en |= INV_ICM42600_SENSOR_ACCEL;
  196. }
  197. /* update data FIFO write */
  198. ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
  199. out_unlock:
  200. mutex_unlock(&st->lock);
  201. /* sleep maximum required time */
  202. sleep = max(sleep_accel, sleep_temp);
  203. if (sleep)
  204. msleep(sleep);
  205. return ret;
  206. }
  207. static int inv_icm42600_accel_read_sensor(struct iio_dev *indio_dev,
  208. struct iio_chan_spec const *chan,
  209. s16 *val)
  210. {
  211. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  212. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  213. struct device *dev = regmap_get_device(st->map);
  214. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  215. unsigned int reg;
  216. __be16 *data;
  217. int ret;
  218. if (chan->type != IIO_ACCEL)
  219. return -EINVAL;
  220. switch (chan->channel2) {
  221. case IIO_MOD_X:
  222. reg = INV_ICM42600_REG_ACCEL_DATA_X;
  223. break;
  224. case IIO_MOD_Y:
  225. reg = INV_ICM42600_REG_ACCEL_DATA_Y;
  226. break;
  227. case IIO_MOD_Z:
  228. reg = INV_ICM42600_REG_ACCEL_DATA_Z;
  229. break;
  230. default:
  231. return -EINVAL;
  232. }
  233. pm_runtime_get_sync(dev);
  234. mutex_lock(&st->lock);
  235. /* enable accel sensor */
  236. conf.mode = accel_st->power_mode;
  237. conf.filter = accel_st->filter;
  238. ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
  239. if (ret)
  240. goto exit;
  241. /* read accel register data */
  242. data = (__be16 *)&st->buffer[0];
  243. ret = regmap_bulk_read(st->map, reg, data, sizeof(*data));
  244. if (ret)
  245. goto exit;
  246. *val = (s16)be16_to_cpup(data);
  247. if (*val == INV_ICM42600_DATA_INVALID)
  248. ret = -EINVAL;
  249. exit:
  250. mutex_unlock(&st->lock);
  251. pm_runtime_mark_last_busy(dev);
  252. pm_runtime_put_autosuspend(dev);
  253. return ret;
  254. }
  255. /* IIO format int + nano */
  256. static const int inv_icm42600_accel_scale[] = {
  257. /* +/- 16G => 0.004788403 m/s-2 */
  258. [2 * INV_ICM42600_ACCEL_FS_16G] = 0,
  259. [2 * INV_ICM42600_ACCEL_FS_16G + 1] = 4788403,
  260. /* +/- 8G => 0.002394202 m/s-2 */
  261. [2 * INV_ICM42600_ACCEL_FS_8G] = 0,
  262. [2 * INV_ICM42600_ACCEL_FS_8G + 1] = 2394202,
  263. /* +/- 4G => 0.001197101 m/s-2 */
  264. [2 * INV_ICM42600_ACCEL_FS_4G] = 0,
  265. [2 * INV_ICM42600_ACCEL_FS_4G + 1] = 1197101,
  266. /* +/- 2G => 0.000598550 m/s-2 */
  267. [2 * INV_ICM42600_ACCEL_FS_2G] = 0,
  268. [2 * INV_ICM42600_ACCEL_FS_2G + 1] = 598550,
  269. };
  270. static const int inv_icm42686_accel_scale[] = {
  271. /* +/- 32G => 0.009576807 m/s-2 */
  272. [2 * INV_ICM42686_ACCEL_FS_32G] = 0,
  273. [2 * INV_ICM42686_ACCEL_FS_32G + 1] = 9576807,
  274. /* +/- 16G => 0.004788403 m/s-2 */
  275. [2 * INV_ICM42686_ACCEL_FS_16G] = 0,
  276. [2 * INV_ICM42686_ACCEL_FS_16G + 1] = 4788403,
  277. /* +/- 8G => 0.002394202 m/s-2 */
  278. [2 * INV_ICM42686_ACCEL_FS_8G] = 0,
  279. [2 * INV_ICM42686_ACCEL_FS_8G + 1] = 2394202,
  280. /* +/- 4G => 0.001197101 m/s-2 */
  281. [2 * INV_ICM42686_ACCEL_FS_4G] = 0,
  282. [2 * INV_ICM42686_ACCEL_FS_4G + 1] = 1197101,
  283. /* +/- 2G => 0.000598550 m/s-2 */
  284. [2 * INV_ICM42686_ACCEL_FS_2G] = 0,
  285. [2 * INV_ICM42686_ACCEL_FS_2G + 1] = 598550,
  286. };
  287. static int inv_icm42600_accel_read_scale(struct iio_dev *indio_dev,
  288. int *val, int *val2)
  289. {
  290. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  291. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  292. unsigned int idx;
  293. idx = st->conf.accel.fs;
  294. *val = accel_st->scales[2 * idx];
  295. *val2 = accel_st->scales[2 * idx + 1];
  296. return IIO_VAL_INT_PLUS_NANO;
  297. }
  298. static int inv_icm42600_accel_write_scale(struct iio_dev *indio_dev,
  299. int val, int val2)
  300. {
  301. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  302. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  303. struct device *dev = regmap_get_device(st->map);
  304. unsigned int idx;
  305. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  306. int ret;
  307. for (idx = 0; idx < accel_st->scales_len; idx += 2) {
  308. if (val == accel_st->scales[idx] &&
  309. val2 == accel_st->scales[idx + 1])
  310. break;
  311. }
  312. if (idx >= accel_st->scales_len)
  313. return -EINVAL;
  314. conf.fs = idx / 2;
  315. pm_runtime_get_sync(dev);
  316. mutex_lock(&st->lock);
  317. ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
  318. mutex_unlock(&st->lock);
  319. pm_runtime_mark_last_busy(dev);
  320. pm_runtime_put_autosuspend(dev);
  321. return ret;
  322. }
  323. /* IIO format int + micro */
  324. static const int inv_icm42600_accel_odr[] = {
  325. /* 1.5625Hz */
  326. 1, 562500,
  327. /* 3.125Hz */
  328. 3, 125000,
  329. /* 6.25Hz */
  330. 6, 250000,
  331. /* 12.5Hz */
  332. 12, 500000,
  333. /* 25Hz */
  334. 25, 0,
  335. /* 50Hz */
  336. 50, 0,
  337. /* 100Hz */
  338. 100, 0,
  339. /* 200Hz */
  340. 200, 0,
  341. /* 1kHz */
  342. 1000, 0,
  343. /* 2kHz */
  344. 2000, 0,
  345. /* 4kHz */
  346. 4000, 0,
  347. };
  348. static const int inv_icm42600_accel_odr_conv[] = {
  349. INV_ICM42600_ODR_1_5625HZ_LP,
  350. INV_ICM42600_ODR_3_125HZ_LP,
  351. INV_ICM42600_ODR_6_25HZ_LP,
  352. INV_ICM42600_ODR_12_5HZ,
  353. INV_ICM42600_ODR_25HZ,
  354. INV_ICM42600_ODR_50HZ,
  355. INV_ICM42600_ODR_100HZ,
  356. INV_ICM42600_ODR_200HZ,
  357. INV_ICM42600_ODR_1KHZ_LN,
  358. INV_ICM42600_ODR_2KHZ_LN,
  359. INV_ICM42600_ODR_4KHZ_LN,
  360. };
  361. static int inv_icm42600_accel_read_odr(struct inv_icm42600_state *st,
  362. int *val, int *val2)
  363. {
  364. unsigned int odr;
  365. unsigned int i;
  366. odr = st->conf.accel.odr;
  367. for (i = 0; i < ARRAY_SIZE(inv_icm42600_accel_odr_conv); ++i) {
  368. if (inv_icm42600_accel_odr_conv[i] == odr)
  369. break;
  370. }
  371. if (i >= ARRAY_SIZE(inv_icm42600_accel_odr_conv))
  372. return -EINVAL;
  373. *val = inv_icm42600_accel_odr[2 * i];
  374. *val2 = inv_icm42600_accel_odr[2 * i + 1];
  375. return IIO_VAL_INT_PLUS_MICRO;
  376. }
  377. static int inv_icm42600_accel_write_odr(struct iio_dev *indio_dev,
  378. int val, int val2)
  379. {
  380. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  381. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  382. struct inv_sensors_timestamp *ts = &accel_st->ts;
  383. struct device *dev = regmap_get_device(st->map);
  384. unsigned int idx;
  385. struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
  386. int ret;
  387. for (idx = 0; idx < ARRAY_SIZE(inv_icm42600_accel_odr); idx += 2) {
  388. if (val == inv_icm42600_accel_odr[idx] &&
  389. val2 == inv_icm42600_accel_odr[idx + 1])
  390. break;
  391. }
  392. if (idx >= ARRAY_SIZE(inv_icm42600_accel_odr))
  393. return -EINVAL;
  394. conf.odr = inv_icm42600_accel_odr_conv[idx / 2];
  395. pm_runtime_get_sync(dev);
  396. mutex_lock(&st->lock);
  397. ret = inv_sensors_timestamp_update_odr(ts, inv_icm42600_odr_to_period(conf.odr),
  398. iio_buffer_enabled(indio_dev));
  399. if (ret)
  400. goto out_unlock;
  401. ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
  402. if (ret)
  403. goto out_unlock;
  404. inv_icm42600_buffer_update_fifo_period(st);
  405. inv_icm42600_buffer_update_watermark(st);
  406. out_unlock:
  407. mutex_unlock(&st->lock);
  408. pm_runtime_mark_last_busy(dev);
  409. pm_runtime_put_autosuspend(dev);
  410. return ret;
  411. }
  412. /*
  413. * Calibration bias values, IIO range format int + micro.
  414. * Value is limited to +/-1g coded on 12 bits signed. Step is 0.5mg.
  415. */
  416. static int inv_icm42600_accel_calibbias[] = {
  417. -10, 42010, /* min: -10.042010 m/s² */
  418. 0, 4903, /* step: 0.004903 m/s² */
  419. 10, 37106, /* max: 10.037106 m/s² */
  420. };
  421. static int inv_icm42600_accel_read_offset(struct inv_icm42600_state *st,
  422. struct iio_chan_spec const *chan,
  423. int *val, int *val2)
  424. {
  425. struct device *dev = regmap_get_device(st->map);
  426. s64 val64;
  427. s32 bias;
  428. unsigned int reg;
  429. s16 offset;
  430. u8 data[2];
  431. int ret;
  432. if (chan->type != IIO_ACCEL)
  433. return -EINVAL;
  434. switch (chan->channel2) {
  435. case IIO_MOD_X:
  436. reg = INV_ICM42600_REG_OFFSET_USER4;
  437. break;
  438. case IIO_MOD_Y:
  439. reg = INV_ICM42600_REG_OFFSET_USER6;
  440. break;
  441. case IIO_MOD_Z:
  442. reg = INV_ICM42600_REG_OFFSET_USER7;
  443. break;
  444. default:
  445. return -EINVAL;
  446. }
  447. pm_runtime_get_sync(dev);
  448. mutex_lock(&st->lock);
  449. ret = regmap_bulk_read(st->map, reg, st->buffer, sizeof(data));
  450. memcpy(data, st->buffer, sizeof(data));
  451. mutex_unlock(&st->lock);
  452. pm_runtime_mark_last_busy(dev);
  453. pm_runtime_put_autosuspend(dev);
  454. if (ret)
  455. return ret;
  456. /* 12 bits signed value */
  457. switch (chan->channel2) {
  458. case IIO_MOD_X:
  459. offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
  460. break;
  461. case IIO_MOD_Y:
  462. offset = sign_extend32(((data[1] & 0x0F) << 8) | data[0], 11);
  463. break;
  464. case IIO_MOD_Z:
  465. offset = sign_extend32(((data[0] & 0xF0) << 4) | data[1], 11);
  466. break;
  467. default:
  468. return -EINVAL;
  469. }
  470. /*
  471. * convert raw offset to g then to m/s²
  472. * 12 bits signed raw step 0.5mg to g: 5 / 10000
  473. * g to m/s²: 9.806650
  474. * result in micro (1000000)
  475. * (offset * 5 * 9.806650 * 1000000) / 10000
  476. */
  477. val64 = (s64)offset * 5LL * 9806650LL;
  478. /* for rounding, add + or - divisor (10000) divided by 2 */
  479. if (val64 >= 0)
  480. val64 += 10000LL / 2LL;
  481. else
  482. val64 -= 10000LL / 2LL;
  483. bias = div_s64(val64, 10000L);
  484. *val = bias / 1000000L;
  485. *val2 = bias % 1000000L;
  486. return IIO_VAL_INT_PLUS_MICRO;
  487. }
  488. static int inv_icm42600_accel_write_offset(struct inv_icm42600_state *st,
  489. struct iio_chan_spec const *chan,
  490. int val, int val2)
  491. {
  492. struct device *dev = regmap_get_device(st->map);
  493. s64 val64;
  494. s32 min, max;
  495. unsigned int reg, regval;
  496. s16 offset;
  497. int ret;
  498. if (chan->type != IIO_ACCEL)
  499. return -EINVAL;
  500. switch (chan->channel2) {
  501. case IIO_MOD_X:
  502. reg = INV_ICM42600_REG_OFFSET_USER4;
  503. break;
  504. case IIO_MOD_Y:
  505. reg = INV_ICM42600_REG_OFFSET_USER6;
  506. break;
  507. case IIO_MOD_Z:
  508. reg = INV_ICM42600_REG_OFFSET_USER7;
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. /* inv_icm42600_accel_calibbias: min - step - max in micro */
  514. min = inv_icm42600_accel_calibbias[0] * 1000000L +
  515. inv_icm42600_accel_calibbias[1];
  516. max = inv_icm42600_accel_calibbias[4] * 1000000L +
  517. inv_icm42600_accel_calibbias[5];
  518. val64 = (s64)val * 1000000LL + (s64)val2;
  519. if (val64 < min || val64 > max)
  520. return -EINVAL;
  521. /*
  522. * convert m/s² to g then to raw value
  523. * m/s² to g: 1 / 9.806650
  524. * g to raw 12 bits signed, step 0.5mg: 10000 / 5
  525. * val in micro (1000000)
  526. * val * 10000 / (9.806650 * 1000000 * 5)
  527. */
  528. val64 = val64 * 10000LL;
  529. /* for rounding, add + or - divisor (9806650 * 5) divided by 2 */
  530. if (val64 >= 0)
  531. val64 += 9806650 * 5 / 2;
  532. else
  533. val64 -= 9806650 * 5 / 2;
  534. offset = div_s64(val64, 9806650 * 5);
  535. /* clamp value limited to 12 bits signed */
  536. if (offset < -2048)
  537. offset = -2048;
  538. else if (offset > 2047)
  539. offset = 2047;
  540. pm_runtime_get_sync(dev);
  541. mutex_lock(&st->lock);
  542. switch (chan->channel2) {
  543. case IIO_MOD_X:
  544. /* OFFSET_USER4 register is shared */
  545. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER4,
  546. &regval);
  547. if (ret)
  548. goto out_unlock;
  549. st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
  550. st->buffer[1] = offset & 0xFF;
  551. break;
  552. case IIO_MOD_Y:
  553. /* OFFSET_USER7 register is shared */
  554. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER7,
  555. &regval);
  556. if (ret)
  557. goto out_unlock;
  558. st->buffer[0] = offset & 0xFF;
  559. st->buffer[1] = ((offset & 0xF00) >> 8) | (regval & 0xF0);
  560. break;
  561. case IIO_MOD_Z:
  562. /* OFFSET_USER7 register is shared */
  563. ret = regmap_read(st->map, INV_ICM42600_REG_OFFSET_USER7,
  564. &regval);
  565. if (ret)
  566. goto out_unlock;
  567. st->buffer[0] = ((offset & 0xF00) >> 4) | (regval & 0x0F);
  568. st->buffer[1] = offset & 0xFF;
  569. break;
  570. default:
  571. ret = -EINVAL;
  572. goto out_unlock;
  573. }
  574. ret = regmap_bulk_write(st->map, reg, st->buffer, 2);
  575. out_unlock:
  576. mutex_unlock(&st->lock);
  577. pm_runtime_mark_last_busy(dev);
  578. pm_runtime_put_autosuspend(dev);
  579. return ret;
  580. }
  581. static int inv_icm42600_accel_read_raw(struct iio_dev *indio_dev,
  582. struct iio_chan_spec const *chan,
  583. int *val, int *val2, long mask)
  584. {
  585. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  586. s16 data;
  587. int ret;
  588. switch (chan->type) {
  589. case IIO_ACCEL:
  590. break;
  591. case IIO_TEMP:
  592. return inv_icm42600_temp_read_raw(indio_dev, chan, val, val2, mask);
  593. default:
  594. return -EINVAL;
  595. }
  596. switch (mask) {
  597. case IIO_CHAN_INFO_RAW:
  598. ret = iio_device_claim_direct_mode(indio_dev);
  599. if (ret)
  600. return ret;
  601. ret = inv_icm42600_accel_read_sensor(indio_dev, chan, &data);
  602. iio_device_release_direct_mode(indio_dev);
  603. if (ret)
  604. return ret;
  605. *val = data;
  606. return IIO_VAL_INT;
  607. case IIO_CHAN_INFO_SCALE:
  608. return inv_icm42600_accel_read_scale(indio_dev, val, val2);
  609. case IIO_CHAN_INFO_SAMP_FREQ:
  610. return inv_icm42600_accel_read_odr(st, val, val2);
  611. case IIO_CHAN_INFO_CALIBBIAS:
  612. return inv_icm42600_accel_read_offset(st, chan, val, val2);
  613. default:
  614. return -EINVAL;
  615. }
  616. }
  617. static int inv_icm42600_accel_read_avail(struct iio_dev *indio_dev,
  618. struct iio_chan_spec const *chan,
  619. const int **vals,
  620. int *type, int *length, long mask)
  621. {
  622. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  623. if (chan->type != IIO_ACCEL)
  624. return -EINVAL;
  625. switch (mask) {
  626. case IIO_CHAN_INFO_SCALE:
  627. *vals = accel_st->scales;
  628. *type = IIO_VAL_INT_PLUS_NANO;
  629. *length = accel_st->scales_len;
  630. return IIO_AVAIL_LIST;
  631. case IIO_CHAN_INFO_SAMP_FREQ:
  632. *vals = inv_icm42600_accel_odr;
  633. *type = IIO_VAL_INT_PLUS_MICRO;
  634. *length = ARRAY_SIZE(inv_icm42600_accel_odr);
  635. return IIO_AVAIL_LIST;
  636. case IIO_CHAN_INFO_CALIBBIAS:
  637. *vals = inv_icm42600_accel_calibbias;
  638. *type = IIO_VAL_INT_PLUS_MICRO;
  639. return IIO_AVAIL_RANGE;
  640. default:
  641. return -EINVAL;
  642. }
  643. }
  644. static int inv_icm42600_accel_write_raw(struct iio_dev *indio_dev,
  645. struct iio_chan_spec const *chan,
  646. int val, int val2, long mask)
  647. {
  648. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  649. int ret;
  650. if (chan->type != IIO_ACCEL)
  651. return -EINVAL;
  652. switch (mask) {
  653. case IIO_CHAN_INFO_SCALE:
  654. ret = iio_device_claim_direct_mode(indio_dev);
  655. if (ret)
  656. return ret;
  657. ret = inv_icm42600_accel_write_scale(indio_dev, val, val2);
  658. iio_device_release_direct_mode(indio_dev);
  659. return ret;
  660. case IIO_CHAN_INFO_SAMP_FREQ:
  661. return inv_icm42600_accel_write_odr(indio_dev, val, val2);
  662. case IIO_CHAN_INFO_CALIBBIAS:
  663. ret = iio_device_claim_direct_mode(indio_dev);
  664. if (ret)
  665. return ret;
  666. ret = inv_icm42600_accel_write_offset(st, chan, val, val2);
  667. iio_device_release_direct_mode(indio_dev);
  668. return ret;
  669. default:
  670. return -EINVAL;
  671. }
  672. }
  673. static int inv_icm42600_accel_write_raw_get_fmt(struct iio_dev *indio_dev,
  674. struct iio_chan_spec const *chan,
  675. long mask)
  676. {
  677. if (chan->type != IIO_ACCEL)
  678. return -EINVAL;
  679. switch (mask) {
  680. case IIO_CHAN_INFO_SCALE:
  681. return IIO_VAL_INT_PLUS_NANO;
  682. case IIO_CHAN_INFO_SAMP_FREQ:
  683. return IIO_VAL_INT_PLUS_MICRO;
  684. case IIO_CHAN_INFO_CALIBBIAS:
  685. return IIO_VAL_INT_PLUS_MICRO;
  686. default:
  687. return -EINVAL;
  688. }
  689. }
  690. static int inv_icm42600_accel_hwfifo_set_watermark(struct iio_dev *indio_dev,
  691. unsigned int val)
  692. {
  693. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  694. int ret;
  695. mutex_lock(&st->lock);
  696. st->fifo.watermark.accel = val;
  697. ret = inv_icm42600_buffer_update_watermark(st);
  698. mutex_unlock(&st->lock);
  699. return ret;
  700. }
  701. static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev,
  702. unsigned int count)
  703. {
  704. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  705. int ret;
  706. if (count == 0)
  707. return 0;
  708. mutex_lock(&st->lock);
  709. ret = inv_icm42600_buffer_hwfifo_flush(st, count);
  710. if (!ret)
  711. ret = st->fifo.nb.accel;
  712. mutex_unlock(&st->lock);
  713. return ret;
  714. }
  715. static const struct iio_info inv_icm42600_accel_info = {
  716. .read_raw = inv_icm42600_accel_read_raw,
  717. .read_avail = inv_icm42600_accel_read_avail,
  718. .write_raw = inv_icm42600_accel_write_raw,
  719. .write_raw_get_fmt = inv_icm42600_accel_write_raw_get_fmt,
  720. .debugfs_reg_access = inv_icm42600_debugfs_reg,
  721. .update_scan_mode = inv_icm42600_accel_update_scan_mode,
  722. .hwfifo_set_watermark = inv_icm42600_accel_hwfifo_set_watermark,
  723. .hwfifo_flush_to_buffer = inv_icm42600_accel_hwfifo_flush,
  724. };
  725. struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st)
  726. {
  727. struct device *dev = regmap_get_device(st->map);
  728. const char *name;
  729. struct inv_icm42600_sensor_state *accel_st;
  730. struct inv_sensors_timestamp_chip ts_chip;
  731. struct iio_dev *indio_dev;
  732. int ret;
  733. name = devm_kasprintf(dev, GFP_KERNEL, "%s-accel", st->name);
  734. if (!name)
  735. return ERR_PTR(-ENOMEM);
  736. indio_dev = devm_iio_device_alloc(dev, sizeof(*accel_st));
  737. if (!indio_dev)
  738. return ERR_PTR(-ENOMEM);
  739. accel_st = iio_priv(indio_dev);
  740. switch (st->chip) {
  741. case INV_CHIP_ICM42686:
  742. accel_st->scales = inv_icm42686_accel_scale;
  743. accel_st->scales_len = ARRAY_SIZE(inv_icm42686_accel_scale);
  744. break;
  745. default:
  746. accel_st->scales = inv_icm42600_accel_scale;
  747. accel_st->scales_len = ARRAY_SIZE(inv_icm42600_accel_scale);
  748. break;
  749. }
  750. /* low-power by default at init */
  751. accel_st->power_mode = INV_ICM42600_SENSOR_MODE_LOW_POWER;
  752. accel_st->filter = INV_ICM42600_FILTER_AVG_16X;
  753. /*
  754. * clock period is 32kHz (31250ns)
  755. * jitter is +/- 2% (20 per mille)
  756. */
  757. ts_chip.clock_period = 31250;
  758. ts_chip.jitter = 20;
  759. ts_chip.init_period = inv_icm42600_odr_to_period(st->conf.accel.odr);
  760. inv_sensors_timestamp_init(&accel_st->ts, &ts_chip);
  761. iio_device_set_drvdata(indio_dev, st);
  762. indio_dev->name = name;
  763. indio_dev->info = &inv_icm42600_accel_info;
  764. indio_dev->modes = INDIO_DIRECT_MODE;
  765. indio_dev->channels = inv_icm42600_accel_channels;
  766. indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_accel_channels);
  767. indio_dev->available_scan_masks = inv_icm42600_accel_scan_masks;
  768. ret = devm_iio_kfifo_buffer_setup(dev, indio_dev,
  769. &inv_icm42600_buffer_ops);
  770. if (ret)
  771. return ERR_PTR(ret);
  772. ret = devm_iio_device_register(dev, indio_dev);
  773. if (ret)
  774. return ERR_PTR(ret);
  775. return indio_dev;
  776. }
  777. int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev)
  778. {
  779. struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
  780. struct inv_icm42600_sensor_state *accel_st = iio_priv(indio_dev);
  781. struct inv_sensors_timestamp *ts = &accel_st->ts;
  782. ssize_t i, size;
  783. unsigned int no;
  784. const void *accel, *gyro, *timestamp;
  785. const int8_t *temp;
  786. unsigned int odr;
  787. int64_t ts_val;
  788. /* buffer is copied to userspace, zeroing it to avoid any data leak */
  789. struct inv_icm42600_accel_buffer buffer = { };
  790. /* parse all fifo packets */
  791. for (i = 0, no = 0; i < st->fifo.count; i += size, ++no) {
  792. size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
  793. &accel, &gyro, &temp, &timestamp, &odr);
  794. /* quit if error or FIFO is empty */
  795. if (size <= 0)
  796. return size;
  797. /* skip packet if no accel data or data is invalid */
  798. if (accel == NULL || !inv_icm42600_fifo_is_data_valid(accel))
  799. continue;
  800. /* update odr */
  801. if (odr & INV_ICM42600_SENSOR_ACCEL)
  802. inv_sensors_timestamp_apply_odr(ts, st->fifo.period,
  803. st->fifo.nb.total, no);
  804. memcpy(&buffer.accel, accel, sizeof(buffer.accel));
  805. /* convert 8 bits FIFO temperature in high resolution format */
  806. buffer.temp = temp ? (*temp * 64) : 0;
  807. ts_val = inv_sensors_timestamp_pop(ts);
  808. iio_push_to_buffers_with_timestamp(indio_dev, &buffer, ts_val);
  809. }
  810. return 0;
  811. }