st_lsm6dsx.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * STMicroelectronics st_lsm6dsx sensor driver
  3. *
  4. * Copyright 2016 STMicroelectronics Inc.
  5. *
  6. * Lorenzo Bianconi <lorenzo.bianconi@st.com>
  7. * Denis Ciocca <denis.ciocca@st.com>
  8. *
  9. * Licensed under the GPL-2.
  10. */
  11. #ifndef ST_LSM6DSX_H
  12. #define ST_LSM6DSX_H
  13. #include <linux/device.h>
  14. #define ST_LSM6DS3_DEV_NAME "lsm6ds3"
  15. #define ST_LSM6DS3H_DEV_NAME "lsm6ds3h"
  16. #define ST_LSM6DSL_DEV_NAME "lsm6dsl"
  17. #define ST_LSM6DSM_DEV_NAME "lsm6dsm"
  18. #define ST_ISM330DLC_DEV_NAME "ism330dlc"
  19. enum st_lsm6dsx_hw_id {
  20. ST_LSM6DS3_ID,
  21. ST_LSM6DS3H_ID,
  22. ST_LSM6DSL_ID,
  23. ST_LSM6DSM_ID,
  24. ST_ISM330DLC_ID,
  25. ST_LSM6DSX_MAX_ID,
  26. };
  27. #define ST_LSM6DSX_BUFF_SIZE 400
  28. #define ST_LSM6DSX_CHAN_SIZE 2
  29. #define ST_LSM6DSX_SAMPLE_SIZE 6
  30. #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
  31. ST_LSM6DSX_SAMPLE_SIZE)
  32. #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
  33. struct st_lsm6dsx_reg {
  34. u8 addr;
  35. u8 mask;
  36. };
  37. /**
  38. * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
  39. * @fifo_th: FIFO threshold register info (addr + mask).
  40. * @fifo_diff: FIFO diff status register info (addr + mask).
  41. * @th_wl: FIFO threshold word length.
  42. */
  43. struct st_lsm6dsx_fifo_ops {
  44. struct {
  45. u8 addr;
  46. u16 mask;
  47. } fifo_th;
  48. struct {
  49. u8 addr;
  50. u16 mask;
  51. } fifo_diff;
  52. u8 th_wl;
  53. };
  54. /**
  55. * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
  56. * @timer_en: Hw timer enable register info (addr + mask).
  57. * @hr_timer: Hw timer resolution register info (addr + mask).
  58. * @fifo_en: Hw timer FIFO enable register info (addr + mask).
  59. * @decimator: Hw timer FIFO decimator register info (addr + mask).
  60. */
  61. struct st_lsm6dsx_hw_ts_settings {
  62. struct st_lsm6dsx_reg timer_en;
  63. struct st_lsm6dsx_reg hr_timer;
  64. struct st_lsm6dsx_reg fifo_en;
  65. struct st_lsm6dsx_reg decimator;
  66. };
  67. /**
  68. * struct st_lsm6dsx_settings - ST IMU sensor settings
  69. * @wai: Sensor WhoAmI default value.
  70. * @max_fifo_size: Sensor max fifo length in FIFO words.
  71. * @id: List of hw id supported by the driver configuration.
  72. * @decimator: List of decimator register info (addr + mask).
  73. * @fifo_ops: Sensor hw FIFO parameters.
  74. * @ts_settings: Hw timer related settings.
  75. */
  76. struct st_lsm6dsx_settings {
  77. u8 wai;
  78. u16 max_fifo_size;
  79. enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
  80. struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
  81. struct st_lsm6dsx_fifo_ops fifo_ops;
  82. struct st_lsm6dsx_hw_ts_settings ts_settings;
  83. };
  84. enum st_lsm6dsx_sensor_id {
  85. ST_LSM6DSX_ID_ACC,
  86. ST_LSM6DSX_ID_GYRO,
  87. ST_LSM6DSX_ID_MAX,
  88. };
  89. enum st_lsm6dsx_fifo_mode {
  90. ST_LSM6DSX_FIFO_BYPASS = 0x0,
  91. ST_LSM6DSX_FIFO_CONT = 0x6,
  92. };
  93. /**
  94. * struct st_lsm6dsx_sensor - ST IMU sensor instance
  95. * @name: Sensor name.
  96. * @id: Sensor identifier.
  97. * @hw: Pointer to instance of struct st_lsm6dsx_hw.
  98. * @gain: Configured sensor sensitivity.
  99. * @odr: Output data rate of the sensor [Hz].
  100. * @watermark: Sensor watermark level.
  101. * @sip: Number of samples in a given pattern.
  102. * @decimator: FIFO decimation factor.
  103. * @ts_ref: Sensor timestamp reference for hw one.
  104. */
  105. struct st_lsm6dsx_sensor {
  106. char name[32];
  107. enum st_lsm6dsx_sensor_id id;
  108. struct st_lsm6dsx_hw *hw;
  109. u32 gain;
  110. u16 odr;
  111. u16 watermark;
  112. u8 sip;
  113. u8 decimator;
  114. s64 ts_ref;
  115. };
  116. /**
  117. * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
  118. * @dev: Pointer to instance of struct device (I2C or SPI).
  119. * @regmap: Register map of the device.
  120. * @irq: Device interrupt line (I2C or SPI).
  121. * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
  122. * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
  123. * @fifo_mode: FIFO operating mode supported by the device.
  124. * @enable_mask: Enabled sensor bitmask.
  125. * @ts_sip: Total number of timestamp samples in a given pattern.
  126. * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
  127. * @buff: Device read buffer.
  128. * @iio_devs: Pointers to acc/gyro iio_dev instances.
  129. * @settings: Pointer to the specific sensor settings in use.
  130. */
  131. struct st_lsm6dsx_hw {
  132. struct device *dev;
  133. struct regmap *regmap;
  134. int irq;
  135. struct mutex fifo_lock;
  136. struct mutex conf_lock;
  137. enum st_lsm6dsx_fifo_mode fifo_mode;
  138. u8 enable_mask;
  139. u8 ts_sip;
  140. u8 sip;
  141. u8 *buff;
  142. struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
  143. const struct st_lsm6dsx_settings *settings;
  144. };
  145. extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
  146. int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
  147. struct regmap *regmap);
  148. int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
  149. int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
  150. int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
  151. int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
  152. u16 watermark);
  153. int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
  154. int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
  155. enum st_lsm6dsx_fifo_mode fifo_mode);
  156. #endif /* ST_LSM6DSX_H */