st_lis2dw12.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * STMicroelectronics lis2dw12 driver
  4. *
  5. * MEMS Software Solutions Team
  6. *
  7. * Copyright 2016 STMicroelectronics Inc.
  8. */
  9. #ifndef ST_LIS2DW12_H
  10. #define ST_LIS2DW12_H
  11. #include <linux/bitfield.h>
  12. #include <linux/device.h>
  13. #include <linux/iio/events.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/of_device.h>
  16. #include <linux/regmap.h>
  17. #include "../common/stm_iio_types.h"
  18. #define ST_LIS2DW12_DEV_NAME "lis2dw12"
  19. #define ST_IIS2DLPC_DEV_NAME "iis2dlpc"
  20. #define ST_AIS2IH_DEV_NAME "ais2ih"
  21. #define ST_LIS2DW12_REGMAP "lis2dw12_regmap"
  22. #define ST_LIS2DW12_MAX_WATERMARK 31
  23. #define ST_LIS2DW12_DATA_SIZE 6
  24. #define ST_LIS2DW12_WHOAMI_ADDR 0x0f
  25. #define ST_LIS2DW12_WHOAMI_VAL 0x44
  26. #define ST_LIS2DW12_CTRL1_ADDR 0x20
  27. #define ST_LIS2DW12_ODR_MASK GENMASK(7, 4)
  28. #define ST_LIS2DW12_MODE_MASK GENMASK(3, 2)
  29. #define ST_LIS2DW12_LP_MODE_MASK GENMASK(1, 0)
  30. #define ST_LIS2DW12_CTRL2_ADDR 0x21
  31. #define ST_LIS2DW12_BDU_MASK BIT(3)
  32. #define ST_LIS2DW12_RESET_MASK BIT(6)
  33. #define ST_LIS2DW12_CTRL3_ADDR 0x22
  34. #define ST_LIS2DW12_LIR_MASK BIT(4)
  35. #define ST_LIS2DW12_ST_MASK GENMASK(7, 6)
  36. #define ST_LIS2DW12_CTRL4_INT1_CTRL_ADDR 0x23
  37. #define ST_LIS2DW12_DRDY_MASK BIT(0)
  38. #define ST_LIS2DW12_FTH_INT_MASK BIT(1)
  39. #define ST_LIS2DW12_TAP_INT1_MASK BIT(6)
  40. #define ST_LIS2DW12_TAP_TAP_INT1_MASK BIT(3)
  41. #define ST_LIS2DW12_FF_INT1_MASK BIT(4)
  42. #define ST_LIS2DW12_WU_INT1_MASK BIT(5)
  43. #define ST_LIS2DW12_CTRL5_INT2_CTRL_ADDR 0x24
  44. #define ST_LIS2DW12_CTRL6_ADDR 0x25
  45. #define ST_LIS2DW12_LN_MASK BIT(2)
  46. #define ST_LIS2DW12_FS_MASK GENMASK(5, 4)
  47. #define ST_LIS2DW12_BW_MASK GENMASK(7, 6)
  48. #define ST_LIS2DW12_STATUS_ADDR 0x27
  49. #define ST_LIS2DW12_STATUS_FF_MASK BIT(0)
  50. #define ST_LIS2DW12_STATUS_TAP_TAP_MASK BIT(4)
  51. #define ST_LIS2DW12_STATUS_TAP_MASK BIT(3)
  52. #define ST_LIS2DW12_STATUS_WU_MASK BIT(6)
  53. #define ST_LIS2DW12_STATUS_FTH_MASK BIT(7)
  54. #define ST_LIS2DW12_OUT_X_L_ADDR 0x28
  55. #define ST_LIS2DW12_OUT_Y_L_ADDR 0x2a
  56. #define ST_LIS2DW12_OUT_Z_L_ADDR 0x2c
  57. #define ST_LIS2DW12_FIFO_CTRL_ADDR 0x2e
  58. #define ST_LIS2DW12_FIFOMODE_MASK GENMASK(7, 5)
  59. #define ST_LIS2DW12_FTH_MASK GENMASK(4, 0)
  60. #define ST_LIS2DW12_FIFO_SAMPLES_ADDR 0x2f
  61. #define ST_LIS2DW12_FIFO_SAMPLES_FTH_MASK BIT(7)
  62. #define ST_LIS2DW12_FIFO_SAMPLES_OVR_MASK BIT(6)
  63. #define ST_LIS2DW12_TAP_THS_X_ADDR 0x30
  64. #define ST_LIS2DW12_TAP_THS_Y_ADDR 0x31
  65. #define ST_LIS2DW12_TAP_THS_Z_ADDR 0x32
  66. #define ST_LIS2DW12_TAP_AXIS_MASK GENMASK(7, 5)
  67. #define ST_LIS2DW12_TAP_THS_MAK GENMASK(4, 0)
  68. #define ST_LIS2DW12_INT_DUR_ADDR 0x33
  69. #define ST_LIS2DW12_WAKE_UP_THS_ADDR 0x34
  70. #define ST_LIS2DW12_WAKE_UP_THS_MAK GENMASK(5, 0)
  71. #define ST_LIS2DW12_SINGLE_DOUBLE_TAP_MAK BIT(7)
  72. #define ST_LIS2DW12_FREE_FALL_ADDR 0x36
  73. #define ST_LIS2DW12_FREE_FALL_THS_MASK GENMASK(2, 0)
  74. #define ST_LIS2DW12_FREE_FALL_DUR_MASK GENMASK(7, 3)
  75. #define ST_LIS2DW12_WU_SRC_ADDR 0x38
  76. #define ST_LIS2DW12_TAP_SRC_ADDR 0x39
  77. #define ST_LIS2DW12_STAP_SRC_MASK BIT(5)
  78. #define ST_LIS2DW12_DTAP_SRC_MASK BIT(4)
  79. #define ST_LIS2DW12_TAP_EVT_MASK GENMASK(2, 0)
  80. #define ST_LIS2DW12_FIFO_SAMPLES_DIFF_MASK GENMASK(5, 0)
  81. #define ST_LIS2DW12_ALL_INT_SRC_ADDR 0x3b
  82. #define ST_LIS2DW12_ALL_INT_SRC_FF_MASK BIT(0)
  83. #define ST_LIS2DW12_ALL_INT_SRC_WU_MASK BIT(1)
  84. #define ST_LIS2DW12_ALL_INT_SRC_TAP_MASK BIT(2)
  85. #define ST_LIS2DW12_ALL_INT_SRC_TAP_TAP_MASK BIT(3)
  86. #define ST_LIS2DW12_ABS_INT_CFG_ADDR 0x3f
  87. #define ST_LIS2DW12_ALL_INT_MASK BIT(5)
  88. #define ST_LIS2DW12_INT2_ON_INT1_MASK BIT(6)
  89. #define ST_LIS2DW12_DRDY_PULSED_MASK BIT(7)
  90. #define ST_LIS2DW12_FS_2G_GAIN IIO_G_TO_M_S_2(244)
  91. #define ST_LIS2DW12_FS_4G_GAIN IIO_G_TO_M_S_2(488)
  92. #define ST_LIS2DW12_FS_8G_GAIN IIO_G_TO_M_S_2(976)
  93. #define ST_LIS2DW12_FS_16G_GAIN IIO_G_TO_M_S_2(1952)
  94. #define ST_LIS2DW12_SELFTEST_MIN 285
  95. #define ST_LIS2DW12_SELFTEST_MAX 6150
  96. #define ST_LIS2DW12_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & \
  97. (mask))
  98. enum st_lis2dw12_fifo_mode {
  99. ST_LIS2DW12_FIFO_BYPASS = 0x0,
  100. ST_LIS2DW12_FIFO_CONTINUOUS = 0x6,
  101. };
  102. enum st_lis2dw12_selftest_status {
  103. ST_LIS2DW12_ST_RESET,
  104. ST_LIS2DW12_ST_PASS,
  105. ST_LIS2DW12_ST_FAIL,
  106. };
  107. enum st_lis2dw12_sensor_id {
  108. ST_LIS2DW12_ID_ACC,
  109. ST_LIS2DW12_ID_TAP_TAP,
  110. ST_LIS2DW12_ID_TAP,
  111. ST_LIS2DW12_ID_WU,
  112. ST_LIS2DW12_ID_MAX,
  113. };
  114. struct st_lis2dw12_sensor {
  115. enum st_lis2dw12_sensor_id id;
  116. struct st_lis2dw12_hw *hw;
  117. char name[32];
  118. u16 gain;
  119. u16 odr;
  120. };
  121. struct st_lis2dw12_hw {
  122. struct regmap *regmap;
  123. struct device *dev;
  124. int irq;
  125. int irq_emb;
  126. int irq_pin;
  127. u8 irq_reg;
  128. char name[32];
  129. struct mutex fifo_lock;
  130. struct mutex lock;
  131. struct iio_dev *iio_devs[ST_LIS2DW12_ID_MAX];
  132. enum st_lis2dw12_selftest_status st_status;
  133. u16 enable_mask;
  134. u8 watermark;
  135. u8 std_level;
  136. u64 samples;
  137. s64 delta_ts;
  138. s64 ts_irq;
  139. s64 ts;
  140. };
  141. /* HW devices that can wakeup the target */
  142. #define ST_LIS2DW12_WAKE_UP_SENSORS (BIT(ST_LIS2DW12_ID_ACC) | \
  143. BIT(ST_LIS2DW12_ID_TAP_TAP) | \
  144. BIT(ST_LIS2DW12_ID_TAP) | \
  145. BIT(ST_LIS2DW12_ID_WU))
  146. /* this is the minimal ODR for wake-up sensors and dependencies */
  147. #define ST_LIS2DW12_MIN_ODR_IN_WAKEUP 25
  148. static inline int
  149. __st_lis2dw12_write_with_mask(struct st_lis2dw12_hw *hw,
  150. unsigned int addr, int mask,
  151. unsigned int data)
  152. {
  153. int err;
  154. unsigned int val = ST_LIS2DW12_SHIFT_VAL(data, mask);
  155. err = regmap_update_bits(hw->regmap, addr, mask, val);
  156. return err;
  157. }
  158. static inline int
  159. st_lis2dw12_update_bits_locked(struct st_lis2dw12_hw *hw,
  160. unsigned int addr, unsigned int mask,
  161. unsigned int val)
  162. {
  163. int err;
  164. mutex_lock(&hw->lock);
  165. err = __st_lis2dw12_write_with_mask(hw, addr, mask, val);
  166. mutex_unlock(&hw->lock);
  167. return err;
  168. }
  169. static inline int
  170. st_lis2dw12_write_with_mask_locked(struct st_lis2dw12_hw *hw,
  171. unsigned int addr, unsigned int mask,
  172. unsigned int data)
  173. {
  174. int err;
  175. mutex_lock(&hw->lock);
  176. err = __st_lis2dw12_write_with_mask(hw, addr, mask, data);
  177. mutex_unlock(&hw->lock);
  178. return err;
  179. }
  180. static inline int st_lis2dw12_write_locked(struct st_lis2dw12_hw *hw,
  181. unsigned int addr, u8 *val,
  182. unsigned int len)
  183. {
  184. int err;
  185. mutex_lock(&hw->lock);
  186. err = regmap_bulk_write(hw->regmap, addr, val, len);
  187. mutex_unlock(&hw->lock);
  188. return err;
  189. }
  190. static inline int st_lis2dw12_read(struct st_lis2dw12_hw *hw, unsigned int addr,
  191. void *val, unsigned int len)
  192. {
  193. return regmap_bulk_read(hw->regmap, addr, val, len);
  194. }
  195. static inline bool st_lis2dw12_is_volatile_reg(struct device *dev,
  196. unsigned int reg)
  197. {
  198. switch (reg) {
  199. case ST_LIS2DW12_WHOAMI_ADDR:
  200. case ST_LIS2DW12_STATUS_ADDR:
  201. case ST_LIS2DW12_OUT_X_L_ADDR:
  202. case ST_LIS2DW12_OUT_X_L_ADDR + 1:
  203. case ST_LIS2DW12_OUT_Y_L_ADDR:
  204. case ST_LIS2DW12_OUT_Y_L_ADDR + 1:
  205. case ST_LIS2DW12_OUT_Z_L_ADDR:
  206. case ST_LIS2DW12_OUT_Z_L_ADDR + 1:
  207. case ST_LIS2DW12_FIFO_SAMPLES_ADDR:
  208. case ST_LIS2DW12_TAP_SRC_ADDR:
  209. case ST_LIS2DW12_ALL_INT_SRC_ADDR:
  210. return true;
  211. default:
  212. return false;
  213. }
  214. }
  215. static inline int
  216. st_lis2dw12_set_fifo_mode(struct st_lis2dw12_hw *hw,
  217. enum st_lis2dw12_fifo_mode mode)
  218. {
  219. return st_lis2dw12_write_with_mask_locked(hw,
  220. ST_LIS2DW12_FIFO_CTRL_ADDR,
  221. ST_LIS2DW12_FIFOMODE_MASK,
  222. mode);
  223. }
  224. static inline int st_lis2dw12_is_fifo_enabled(struct st_lis2dw12_hw *hw)
  225. {
  226. return hw->enable_mask & (BIT(ST_LIS2DW12_ID_ACC));
  227. }
  228. extern const struct dev_pm_ops st_lis2dw12_pm_ops;
  229. int st_lis2dw12_probe(struct device *dev, int irq, const char *name,
  230. struct regmap *regmap);
  231. int st_lis2dw12_fifo_setup(struct st_lis2dw12_hw *hw);
  232. int st_lis2dw12_update_fifo_watermark(struct st_lis2dw12_hw *hw, u8 watermark);
  233. ssize_t st_lis2dw12_flush_fifo(struct device *dev,
  234. struct device_attribute *attr,
  235. const char *buf, size_t size);
  236. ssize_t st_lis2dw12_set_hwfifo_watermark(struct device *device,
  237. struct device_attribute *attr,
  238. const char *buf, size_t size);
  239. int st_lis2dw12_sensor_set_enable(struct st_lis2dw12_sensor *sensor,
  240. bool enable);
  241. int st_lis2dw12_suspend_fifo(struct st_lis2dw12_hw *hw);
  242. #endif /* ST_LIS2DW12_H */