st_lsm6dsx_buffer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * STMicroelectronics st_lsm6dsx FIFO buffer library driver
  3. *
  4. * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM: The FIFO buffer can be configured
  5. * to store data from gyroscope and accelerometer. Samples are queued
  6. * without any tag according to a specific pattern based on 'FIFO data sets'
  7. * (6 bytes each):
  8. * - 1st data set is reserved for gyroscope data
  9. * - 2nd data set is reserved for accelerometer data
  10. * The FIFO pattern changes depending on the ODRs and decimation factors
  11. * assigned to the FIFO data sets. The first sequence of data stored in FIFO
  12. * buffer contains the data of all the enabled FIFO data sets
  13. * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated depending on the
  14. * value of the decimation factor and ODR set for each FIFO data set.
  15. * FIFO supported modes:
  16. * - BYPASS: FIFO disabled
  17. * - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
  18. * restarts from the beginning and the oldest sample is overwritten
  19. *
  20. * Copyright 2016 STMicroelectronics Inc.
  21. *
  22. * Lorenzo Bianconi <lorenzo.bianconi@st.com>
  23. * Denis Ciocca <denis.ciocca@st.com>
  24. *
  25. * Licensed under the GPL-2.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/irq.h>
  30. #include <linux/iio/kfifo_buf.h>
  31. #include <linux/iio/iio.h>
  32. #include <linux/iio/buffer.h>
  33. #include <linux/platform_data/st_sensors_pdata.h>
  34. #include "st_lsm6dsx.h"
  35. #define ST_LSM6DSX_REG_FIFO_THL_ADDR 0x06
  36. #define ST_LSM6DSX_REG_FIFO_THH_ADDR 0x07
  37. #define ST_LSM6DSX_FIFO_TH_MASK GENMASK(11, 0)
  38. #define ST_LSM6DSX_REG_FIFO_DEC_GXL_ADDR 0x08
  39. #define ST_LSM6DSX_REG_HLACTIVE_ADDR 0x12
  40. #define ST_LSM6DSX_REG_HLACTIVE_MASK BIT(5)
  41. #define ST_LSM6DSX_REG_PP_OD_ADDR 0x12
  42. #define ST_LSM6DSX_REG_PP_OD_MASK BIT(4)
  43. #define ST_LSM6DSX_REG_FIFO_MODE_ADDR 0x0a
  44. #define ST_LSM6DSX_FIFO_MODE_MASK GENMASK(2, 0)
  45. #define ST_LSM6DSX_FIFO_ODR_MASK GENMASK(6, 3)
  46. #define ST_LSM6DSX_REG_FIFO_DIFFL_ADDR 0x3a
  47. #define ST_LSM6DSX_FIFO_DIFF_MASK GENMASK(11, 0)
  48. #define ST_LSM6DSX_FIFO_EMPTY_MASK BIT(12)
  49. #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR 0x3e
  50. #define ST_LSM6DSX_MAX_FIFO_ODR_VAL 0x08
  51. struct st_lsm6dsx_decimator_entry {
  52. u8 decimator;
  53. u8 val;
  54. };
  55. static const
  56. struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
  57. { 0, 0x0 },
  58. { 1, 0x1 },
  59. { 2, 0x2 },
  60. { 3, 0x3 },
  61. { 4, 0x4 },
  62. { 8, 0x5 },
  63. { 16, 0x6 },
  64. { 32, 0x7 },
  65. };
  66. static int st_lsm6dsx_get_decimator_val(u8 val)
  67. {
  68. const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
  69. int i;
  70. for (i = 0; i < max_size; i++)
  71. if (st_lsm6dsx_decimator_table[i].decimator == val)
  72. break;
  73. return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
  74. }
  75. static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
  76. u16 *max_odr, u16 *min_odr)
  77. {
  78. struct st_lsm6dsx_sensor *sensor;
  79. int i;
  80. *max_odr = 0, *min_odr = ~0;
  81. for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
  82. sensor = iio_priv(hw->iio_devs[i]);
  83. if (!(hw->enable_mask & BIT(sensor->id)))
  84. continue;
  85. *max_odr = max_t(u16, *max_odr, sensor->odr);
  86. *min_odr = min_t(u16, *min_odr, sensor->odr);
  87. }
  88. }
  89. static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
  90. {
  91. struct st_lsm6dsx_sensor *sensor;
  92. u16 max_odr, min_odr, sip = 0;
  93. int err, i;
  94. u8 data;
  95. st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
  96. for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
  97. sensor = iio_priv(hw->iio_devs[i]);
  98. /* update fifo decimators and sample in pattern */
  99. if (hw->enable_mask & BIT(sensor->id)) {
  100. sensor->sip = sensor->odr / min_odr;
  101. sensor->decimator = max_odr / sensor->odr;
  102. data = st_lsm6dsx_get_decimator_val(sensor->decimator);
  103. } else {
  104. sensor->sip = 0;
  105. sensor->decimator = 0;
  106. data = 0;
  107. }
  108. err = st_lsm6dsx_write_with_mask(hw,
  109. ST_LSM6DSX_REG_FIFO_DEC_GXL_ADDR,
  110. sensor->decimator_mask, data);
  111. if (err < 0)
  112. return err;
  113. sip += sensor->sip;
  114. }
  115. hw->sip = sip;
  116. return 0;
  117. }
  118. int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
  119. enum st_lsm6dsx_fifo_mode fifo_mode)
  120. {
  121. u8 data;
  122. int err;
  123. switch (fifo_mode) {
  124. case ST_LSM6DSX_FIFO_BYPASS:
  125. data = fifo_mode;
  126. break;
  127. case ST_LSM6DSX_FIFO_CONT:
  128. data = (ST_LSM6DSX_MAX_FIFO_ODR_VAL <<
  129. __ffs(ST_LSM6DSX_FIFO_ODR_MASK)) | fifo_mode;
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. err = hw->tf->write(hw->dev, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
  135. sizeof(data), &data);
  136. if (err < 0)
  137. return err;
  138. hw->fifo_mode = fifo_mode;
  139. return 0;
  140. }
  141. int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
  142. {
  143. u16 fifo_watermark = ~0, cur_watermark, sip = 0;
  144. struct st_lsm6dsx_hw *hw = sensor->hw;
  145. struct st_lsm6dsx_sensor *cur_sensor;
  146. __le16 wdata;
  147. int i, err;
  148. u8 data;
  149. for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
  150. cur_sensor = iio_priv(hw->iio_devs[i]);
  151. if (!(hw->enable_mask & BIT(cur_sensor->id)))
  152. continue;
  153. cur_watermark = (cur_sensor == sensor) ? watermark
  154. : cur_sensor->watermark;
  155. fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
  156. sip += cur_sensor->sip;
  157. }
  158. if (!sip)
  159. return 0;
  160. fifo_watermark = max_t(u16, fifo_watermark, sip);
  161. fifo_watermark = (fifo_watermark / sip) * sip;
  162. fifo_watermark = fifo_watermark * ST_LSM6DSX_SAMPLE_DEPTH;
  163. mutex_lock(&hw->lock);
  164. err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_THH_ADDR,
  165. sizeof(data), &data);
  166. if (err < 0)
  167. goto out;
  168. fifo_watermark = ((data << 8) & ~ST_LSM6DSX_FIFO_TH_MASK) |
  169. (fifo_watermark & ST_LSM6DSX_FIFO_TH_MASK);
  170. wdata = cpu_to_le16(fifo_watermark);
  171. err = hw->tf->write(hw->dev, ST_LSM6DSX_REG_FIFO_THL_ADDR,
  172. sizeof(wdata), (u8 *)&wdata);
  173. out:
  174. mutex_unlock(&hw->lock);
  175. return err < 0 ? err : 0;
  176. }
  177. /**
  178. * st_lsm6dsx_read_fifo() - LSM6DS3-LSM6DS3H-LSM6DSL-LSM6DSM read FIFO routine
  179. * @hw: Pointer to instance of struct st_lsm6dsx_hw.
  180. *
  181. * Read samples from the hw FIFO and push them to IIO buffers.
  182. *
  183. * Return: Number of bytes read from the FIFO
  184. */
  185. static int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
  186. {
  187. u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
  188. int err, acc_sip, gyro_sip, read_len, samples, offset;
  189. struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor;
  190. s64 acc_ts, acc_delta_ts, gyro_ts, gyro_delta_ts;
  191. u8 iio_buff[ALIGN(ST_LSM6DSX_SAMPLE_SIZE, sizeof(s64)) + sizeof(s64)];
  192. u8 buff[pattern_len];
  193. __le16 fifo_status;
  194. err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_DIFFL_ADDR,
  195. sizeof(fifo_status), (u8 *)&fifo_status);
  196. if (err < 0)
  197. return err;
  198. if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
  199. return 0;
  200. fifo_len = (le16_to_cpu(fifo_status) & ST_LSM6DSX_FIFO_DIFF_MASK) *
  201. ST_LSM6DSX_CHAN_SIZE;
  202. samples = fifo_len / ST_LSM6DSX_SAMPLE_SIZE;
  203. fifo_len = (fifo_len / pattern_len) * pattern_len;
  204. /*
  205. * compute delta timestamp between two consecutive samples
  206. * in order to estimate queueing time of data generated
  207. * by the sensor
  208. */
  209. acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
  210. acc_ts = acc_sensor->ts - acc_sensor->delta_ts;
  211. acc_delta_ts = div_s64(acc_sensor->delta_ts * acc_sensor->decimator,
  212. samples);
  213. gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
  214. gyro_ts = gyro_sensor->ts - gyro_sensor->delta_ts;
  215. gyro_delta_ts = div_s64(gyro_sensor->delta_ts * gyro_sensor->decimator,
  216. samples);
  217. for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
  218. err = hw->tf->read(hw->dev, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
  219. sizeof(buff), buff);
  220. if (err < 0)
  221. return err;
  222. /*
  223. * Data are written to the FIFO with a specific pattern
  224. * depending on the configured ODRs. The first sequence of data
  225. * stored in FIFO contains the data of all enabled sensors
  226. * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated
  227. * depending on the value of the decimation factor set for each
  228. * sensor.
  229. *
  230. * Supposing the FIFO is storing data from gyroscope and
  231. * accelerometer at different ODRs:
  232. * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
  233. * Since the gyroscope ODR is twice the accelerometer one, the
  234. * following pattern is repeated every 9 samples:
  235. * - Gx, Gy, Gz, Ax, Ay, Az, Gx, Gy, Gz
  236. */
  237. gyro_sip = gyro_sensor->sip;
  238. acc_sip = acc_sensor->sip;
  239. offset = 0;
  240. while (acc_sip > 0 || gyro_sip > 0) {
  241. if (gyro_sip-- > 0) {
  242. memcpy(iio_buff, &buff[offset],
  243. ST_LSM6DSX_SAMPLE_SIZE);
  244. iio_push_to_buffers_with_timestamp(
  245. hw->iio_devs[ST_LSM6DSX_ID_GYRO],
  246. iio_buff, gyro_ts);
  247. offset += ST_LSM6DSX_SAMPLE_SIZE;
  248. gyro_ts += gyro_delta_ts;
  249. }
  250. if (acc_sip-- > 0) {
  251. memcpy(iio_buff, &buff[offset],
  252. ST_LSM6DSX_SAMPLE_SIZE);
  253. iio_push_to_buffers_with_timestamp(
  254. hw->iio_devs[ST_LSM6DSX_ID_ACC],
  255. iio_buff, acc_ts);
  256. offset += ST_LSM6DSX_SAMPLE_SIZE;
  257. acc_ts += acc_delta_ts;
  258. }
  259. }
  260. }
  261. return read_len;
  262. }
  263. int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
  264. {
  265. int err;
  266. mutex_lock(&hw->fifo_lock);
  267. st_lsm6dsx_read_fifo(hw);
  268. err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
  269. mutex_unlock(&hw->fifo_lock);
  270. return err;
  271. }
  272. static int st_lsm6dsx_update_fifo(struct iio_dev *iio_dev, bool enable)
  273. {
  274. struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
  275. struct st_lsm6dsx_hw *hw = sensor->hw;
  276. int err;
  277. if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) {
  278. err = st_lsm6dsx_flush_fifo(hw);
  279. if (err < 0)
  280. return err;
  281. }
  282. if (enable) {
  283. err = st_lsm6dsx_sensor_enable(sensor);
  284. if (err < 0)
  285. return err;
  286. } else {
  287. err = st_lsm6dsx_sensor_disable(sensor);
  288. if (err < 0)
  289. return err;
  290. }
  291. err = st_lsm6dsx_update_decimators(hw);
  292. if (err < 0)
  293. return err;
  294. err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
  295. if (err < 0)
  296. return err;
  297. if (hw->enable_mask) {
  298. err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
  299. if (err < 0)
  300. return err;
  301. /*
  302. * store enable buffer timestamp as reference to compute
  303. * first delta timestamp
  304. */
  305. sensor->ts = iio_get_time_ns(iio_dev);
  306. }
  307. return 0;
  308. }
  309. static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
  310. {
  311. struct st_lsm6dsx_hw *hw = private;
  312. struct st_lsm6dsx_sensor *sensor;
  313. int i;
  314. if (!hw->sip)
  315. return IRQ_NONE;
  316. for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
  317. sensor = iio_priv(hw->iio_devs[i]);
  318. if (sensor->sip > 0) {
  319. s64 timestamp;
  320. timestamp = iio_get_time_ns(hw->iio_devs[i]);
  321. sensor->delta_ts = timestamp - sensor->ts;
  322. sensor->ts = timestamp;
  323. }
  324. }
  325. return IRQ_WAKE_THREAD;
  326. }
  327. static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
  328. {
  329. struct st_lsm6dsx_hw *hw = private;
  330. int fifo_len = 0, len;
  331. /*
  332. * If we are using edge IRQs, new samples can arrive while
  333. * processing current interrupt since there are no hw
  334. * guarantees the irq line stays "low" long enough to properly
  335. * detect the new interrupt. In this case the new sample will
  336. * be missed.
  337. * Polling FIFO status register allow us to read new
  338. * samples even if the interrupt arrives while processing
  339. * previous data and the timeslot where the line is "low" is
  340. * too short to be properly detected.
  341. */
  342. do {
  343. mutex_lock(&hw->fifo_lock);
  344. len = st_lsm6dsx_read_fifo(hw);
  345. mutex_unlock(&hw->fifo_lock);
  346. if (len > 0)
  347. fifo_len += len;
  348. } while (len > 0);
  349. return fifo_len ? IRQ_HANDLED : IRQ_NONE;
  350. }
  351. static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
  352. {
  353. return st_lsm6dsx_update_fifo(iio_dev, true);
  354. }
  355. static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
  356. {
  357. return st_lsm6dsx_update_fifo(iio_dev, false);
  358. }
  359. static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
  360. .preenable = st_lsm6dsx_buffer_preenable,
  361. .postdisable = st_lsm6dsx_buffer_postdisable,
  362. };
  363. int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
  364. {
  365. struct device_node *np = hw->dev->of_node;
  366. struct st_sensors_platform_data *pdata;
  367. struct iio_buffer *buffer;
  368. unsigned long irq_type;
  369. bool irq_active_low;
  370. int i, err;
  371. irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
  372. switch (irq_type) {
  373. case IRQF_TRIGGER_HIGH:
  374. case IRQF_TRIGGER_RISING:
  375. irq_active_low = false;
  376. break;
  377. case IRQF_TRIGGER_LOW:
  378. case IRQF_TRIGGER_FALLING:
  379. irq_active_low = true;
  380. break;
  381. default:
  382. dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
  383. return -EINVAL;
  384. }
  385. err = st_lsm6dsx_write_with_mask(hw, ST_LSM6DSX_REG_HLACTIVE_ADDR,
  386. ST_LSM6DSX_REG_HLACTIVE_MASK,
  387. irq_active_low);
  388. if (err < 0)
  389. return err;
  390. pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
  391. if ((np && of_property_read_bool(np, "drive-open-drain")) ||
  392. (pdata && pdata->open_drain)) {
  393. err = st_lsm6dsx_write_with_mask(hw, ST_LSM6DSX_REG_PP_OD_ADDR,
  394. ST_LSM6DSX_REG_PP_OD_MASK, 1);
  395. if (err < 0)
  396. return err;
  397. irq_type |= IRQF_SHARED;
  398. }
  399. err = devm_request_threaded_irq(hw->dev, hw->irq,
  400. st_lsm6dsx_handler_irq,
  401. st_lsm6dsx_handler_thread,
  402. irq_type | IRQF_ONESHOT,
  403. "lsm6dsx", hw);
  404. if (err) {
  405. dev_err(hw->dev, "failed to request trigger irq %d\n",
  406. hw->irq);
  407. return err;
  408. }
  409. for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
  410. buffer = devm_iio_kfifo_allocate(hw->dev);
  411. if (!buffer)
  412. return -ENOMEM;
  413. iio_device_attach_buffer(hw->iio_devs[i], buffer);
  414. hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
  415. hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;
  416. }
  417. return 0;
  418. }