st_lis2duxs12.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * STMicroelectronics st_lis2duxs12 sensor driver
  4. *
  5. * MEMS Software Solutions Team
  6. *
  7. * Copyright 2022 STMicroelectronics Inc.
  8. */
  9. #ifndef ST_LIS2DUXS12_H
  10. #define ST_LIS2DUXS12_H
  11. #include <linux/device.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/delay.h>
  14. #include <linux/regmap.h>
  15. #include <linux/bitfield.h>
  16. #include "../common/stm_iio_types.h"
  17. #define ST_LIS2DUXS12_ODR_EXPAND(odr, uodr) (((odr) * 1000000) + (uodr))
  18. #define ST_LIS2DUX12_DEV_NAME "lis2dux12"
  19. #define ST_LIS2DUXS12_DEV_NAME "lis2duxs12"
  20. /* default configuration values */
  21. #define ST_LIS2DUXS12_DEFAULT_WK_TH 154000
  22. #define ST_LIS2DUXS12_PIN_CTRL_ADDR 0x0c
  23. #define ST_LIS2DUXS12_PP_OD_MASK BIT(1)
  24. #define ST_LIS2DUXS12_CS_PU_DIS_MASK BIT(2)
  25. #define ST_LIS2DUXS12_H_LACTIVE_MASK BIT(3)
  26. #define ST_LIS2DUXS12_PD_DIS_INT1_MASK BIT(4)
  27. #define ST_LIS2DUXS12_PD_DIS_INT2_MASK BIT(5)
  28. #define ST_LIS2DUXS12_SDA_PU_EN_MASK BIT(6)
  29. #define ST_LIS2DUXS12_SDO_PU_EN_MASK BIT(7)
  30. #define ST_LIS2DUXS12_WHOAMI_ADDR 0x0f
  31. #define ST_LIS2DUXS12_WHOAMI_VAL 0x47
  32. #define ST_LIS2DUXS12_CTRL1_ADDR 0x10
  33. #define ST_LIS2DUXS12_WU_EN_MASK GENMASK(2, 0)
  34. #define ST_LIS2DUXS12_DRDY_PULSED_MASK BIT(3)
  35. #define ST_LIS2DUXS12_IF_ADD_INC_MASK BIT(4)
  36. #define ST_LIS2DUXS12_SW_RESET_MASK BIT(5)
  37. #define ST_LIS2DUXS12_INT1_ON_RES_MASK BIT(6)
  38. #define ST_LIS2DUXS12_CTRL2_ADDR 0x11
  39. #define ST_LIS2DUXS12_CTRL3_ADDR 0x12
  40. #define ST_LIS2DUXS12_HP_EN_MASK BIT(2)
  41. #define ST_LIS2DUXS12_INT_FIFO_TH_MASK BIT(5)
  42. #define ST_LIS2DUXS12_CTRL4_ADDR 0x13
  43. #define ST_LIS2DUXS12_BOOT_MASK BIT(0)
  44. #define ST_LIS2DUXS12_SOC_MASK BIT(1)
  45. #define ST_LIS2DUXS12_FIFO_EN_MASK BIT(3)
  46. #define ST_LIS2DUXS12_EMB_FUNC_EN_MASK BIT(4)
  47. #define ST_LIS2DUXS12_BDU_MASK BIT(5)
  48. #define ST_LIS2DUXS12_INACT_ODR_MASK GENMASK(7, 6)
  49. #define ST_LIS2DUXS12_CTRL5_ADDR 0x14
  50. #define ST_LIS2DUXS12_FS_MASK GENMASK(1, 0)
  51. #define ST_LIS2DUXS12_ODR_MASK GENMASK(7, 4)
  52. #define ST_LIS2DUXS12_FIFO_CTRL_ADDR 0x15
  53. #define ST_LIS2DUXS12_FIFO_MODE_MASK GENMASK(2, 0)
  54. #define ST_LIS2DUXS12_FIFO_WTM_ADDR 0x16
  55. #define ST_LIS2DUXS12_FIFO_WTM_MASK GENMASK(6, 0)
  56. #define ST_LIS2DUXS12_XL_ONLY_FIFO_MASK BIT(7)
  57. #define ST_LIS2DUXS12_INTERRUPT_CFG_ADDR 0x17
  58. #define ST_LIS2DUXS12_INTERRUPTS_ENABLE_MASK BIT(0)
  59. #define ST_LIS2DUXS12_LIR_MASK BIT(1)
  60. #define ST_LIS2DUXS12_TIMESTAMP_EN_MASK BIT(7)
  61. #define ST_LIS2DUXS12_SIXD_ADDR 0x18
  62. #define ST_LIS2DUXS12_D6D_THS_MASK GENMASK(6, 5)
  63. #define ST_LIS2DUXS12_WAKE_UP_THS_ADDR 0x1c
  64. #define ST_LIS2DUXS12_WK_THS_MASK GENMASK(5, 0)
  65. #define ST_LIS2DUXS12_SLEEP_ON_MASK BIT(6)
  66. #define ST_LIS2DUXS12_WAKE_UP_DUR_ADDR 0x1d
  67. #define ST_LIS2DUXS12_SLEEP_DUR_MASK GENMASK(3, 0)
  68. #define ST_LIS2DUXS12_WAKE_DUR_MASK GENMASK(6, 5)
  69. #define ST_LIS2DUXS12_FF_DUR5_MASK BIT(7)
  70. #define ST_LIS2DUXS12_FREE_FALL_ADDR 0x1e
  71. #define ST_LIS2DUXS12_FF_THS_MASK GENMASK(2, 0)
  72. #define ST_LIS2DUXS12_FF_DUR_MASK GENMASK(7, 3)
  73. #define ST_LIS2DUXS12_FF_DUR5_MASK BIT(7)
  74. #define ST_LIS2DUXS12_MD1_CFG_ADDR 0x1f
  75. #define ST_LIS2DUXS12_MD2_CFG_ADDR 0x20
  76. #define ST_LIS2DUXS12_INT_SLEEP_CHANGE_MASK BIT(7)
  77. #define ST_LIS2DUXS12_INT_WU_MASK BIT(5)
  78. #define ST_LIS2DUXS12_INT_FF_MASK BIT(4)
  79. #define ST_LIS2DUXS12_INT_TAP_MASK BIT(3)
  80. #define ST_LIS2DUXS12_INT_6D_MASK BIT(2)
  81. #define ST_LIS2DUXS12_INT_TIMESTAMP_MASK BIT(1)
  82. #define ST_LIS2DUXS12_INT_EMB_FUNC_MASK BIT(0)
  83. #define ST_LIS2DUXS12_WAKE_UP_SRC_ADDR 0x21
  84. #define ST_LIS2DUXS12_WK_MASK GENMASK(2, 0)
  85. #define ST_LIS2DUXS12_WU_IA_MASK BIT(3)
  86. #define ST_LIS2DUXS12_SLEEP_STATE_MASK BIT(4)
  87. #define ST_LIS2DUXS12_FF_IA_MASK BIT(5)
  88. #define ST_LIS2DUXS12_SLEEP_CHANGE_IA_MASK BIT(6)
  89. #define ST_LIS2DUXS12_TAP_SRC_ADDR 0x22
  90. #define ST_LIS2DUXS12_TRIPLE_TAP_IA_MASK BIT(4)
  91. #define ST_LIS2DUXS12_DOUBLE_TAP_IA_MASK BIT(5)
  92. #define ST_LIS2DUXS12_SINGLE_TAP_IA_MASK BIT(6)
  93. #define ST_LIS2DUXS12_TAP_IA_MASK BIT(7)
  94. #define ST_LIS2DUXS12_SIXD_SRC_ADDR 0x23
  95. #define ST_LIS2DUXS12_X_Y_Z_MASK GENMASK(5, 0)
  96. #define ST_LIS2DUXS12_D6D_IA_MASK BIT(6)
  97. #define ST_LIS2DUXS12_ALL_INT_SRC_ADDR 0x24
  98. #define ST_LIS2DUXS12_FF_IA_ALL_MASK BIT(0)
  99. #define ST_LIS2DUXS12_WU_IA_ALL_MASK BIT(1)
  100. #define ST_LIS2DUXS12_SINGLE_TAP_ALL_MASK BIT(2)
  101. #define ST_LIS2DUXS12_DOUBLE_TAP_ALL_MASK BIT(3)
  102. #define ST_LIS2DUXS12_TRIPLE_TAP_ALL_MASK BIT(4)
  103. #define ST_LIS2DUXS12_D6D_IA_ALL_MASK BIT(5)
  104. #define ST_LIS2DUXS12_SLEEP_CHANGE_ALL_MASK BIT(6)
  105. #define ST_LIS2DUXS12_STATUS_ADDR 0x25
  106. #define ST_LIS2DUXS12_DRDY_MASK BIT(0)
  107. #define ST_LIS2DUXS12_INT_GLOBAL_MASK BIT(5)
  108. #define ST_LIS2DUXS12_FIFO_STATUS1_ADDR 0x26
  109. #define ST_LIS2DUXS12_FIFO_WTM_IA_MASK BIT(7)
  110. #define ST_LIS2DUXS12_FIFO_STATUS2_ADDR 0x27
  111. #define ST_LIS2DUXS12_FIFO_FSS_MASK GENMASK(7, 0)
  112. #define ST_LIS2DUXS12_OUT_X_L_ADDR 0x28
  113. #define ST_LIS2DUXS12_OUT_Y_L_ADDR 0x2a
  114. #define ST_LIS2DUXS12_OUT_Z_L_ADDR 0x2c
  115. #define ST_LIS2DUXS12_OUT_T_AH_QVAR_L_ADDR 0x2e
  116. #define ST_LIS2DUXS12_AH_QVAR_CFG_ADDR 0x31
  117. #define ST_LIS2DUXS12_AH_QVAR_EN_MASK BIT(7)
  118. #define ST_LIS2DUXS12_AH_QVAR_NOTCH_EN_MASK BIT(6)
  119. #define ST_LIS2DUXS12_AH_QVAR_NOTCH_CUTOFF_MASK BIT(5)
  120. #define ST_LIS2DUXS12_AH_QVAR_C_ZIN_MASK GENMASK(4, 3)
  121. #define ST_LIS2DUXS12_AH_QVAR_GAIN_MASK GENMASK(2, 1)
  122. #define ST_LIS2DUXS12_SELF_TEST_ADDR 0x32
  123. #define ST_LIS2DUXS12_ST_MASK GENMASK(5, 4)
  124. #define ST_LIS2DUXS12_EMB_FUNC_STATUS_MAINPAGE_ADDR 0x34
  125. #define ST_LIS2DUXS12_IS_STEP_DET_MASK BIT(3)
  126. #define ST_LIS2DUXS12_IS_TILT_MASK BIT(4)
  127. #define ST_LIS2DUXS12_IS_SIGMOT_MASK BIT(5)
  128. #define ST_LIS2DUXS12_FSM_STATUS_MAINPAGE_ADDR 0x35
  129. #define ST_LIS2DUXS12_MLC_STATUS_MAINPAGE_ADDR 0x36
  130. #define ST_LIS2DUXS12_FUNC_CFG_ACCESS_ADDR 0x3f
  131. #define ST_LIS2DUXS12_EMB_FUNC_REG_ACCESS_MASK BIT(7)
  132. #define ST_LIS2DUXS12_FSM_WR_CTRL_EN_MASK BIT(0)
  133. #define ST_LIS2DUXS12_FIFO_DATA_OUT_TAG_ADDR 0x40
  134. #define ST_LIS2DUXS12_FIFO_BATCH_DEC_ADDR 0x47
  135. #define ST_LIS2DUXS12_BDR_XL_MASK GENMASK(2, 0)
  136. #define ST_LIS2DUXS12_DEC_TS_MASK GENMASK(4, 3)
  137. #define ST_LIS2DUXS12_TAP_CFG0_ADDR 0x6f
  138. #define ST_LIS2DUXS12_AXIS_MASK GENMASK(7, 6)
  139. #define ST_LIS2DUXS12_INVERT_T_MASK GENMASK(5, 1)
  140. #define ST_LIS2DUXS12_TAP_CFG1_ADDR 0x70
  141. #define ST_LIS2DUXS12_POST_STILL_T_MASK GENMASK(3, 0)
  142. #define ST_LIS2DUXS12_PRE_STILL_THS_MASK GENMASK(7, 4)
  143. #define ST_LIS2DUXS12_TAP_CFG2_ADDR 0x71
  144. #define ST_LIS2DUXS12_WAIT_T_MASK GENMASK(5, 0)
  145. #define ST_LIS2DUXS12_POST_STILL_TH_MASK GENMASK(7, 6)
  146. #define ST_LIS2DUXS12_TAP_CFG3_ADDR 0x72
  147. #define ST_LIS2DUXS12_LATENCY_T_MASK GENMASK(3, 0)
  148. #define ST_LIS2DUXS12_POST_STILL_THS_MASK GENMASK(7, 4)
  149. #define ST_LIS2DUXS12_TAP_CFG4_ADDR 0x73
  150. #define ST_LIS2DUXS12_PEAK_THS_MASK GENMASK(5, 0)
  151. #define ST_LIS2DUXS12_WAIT_END_LATENCY_MASK BIT(7)
  152. #define ST_LIS2DUXS12_TAP_CFG5_ADDR 0x74
  153. #define ST_LIS2DUXS12_REBOUND_T_MASK GENMASK(4, 0)
  154. #define ST_LIS2DUXS12_SINGLE_TAP_EN_MASK BIT(5)
  155. #define ST_LIS2DUXS12_DOUBLE_TAP_EN_MASK BIT(6)
  156. #define ST_LIS2DUXS12_TRIPLE_TAP_EN_MASK BIT(7)
  157. #define ST_LIS2DUXS12_TAP_CFG6_ADDR 0x75
  158. #define ST_LIS2DUXS12_PRE_STILL_N_MASK GENMASK(3, 0)
  159. #define ST_LIS2DUXS12_PRE_STILL_ST_MASK GENMASK(7, 4)
  160. #define ST_LIS2DUXS12_TIMESTAMP2_ADDR 0x7c
  161. #define ST_LIS2DUXS12_SELFTEST_ACCEL_MIN 204
  162. #define ST_LIS2DUXS12_SELFTEST_ACCEL_MAX 4918
  163. /* embedded function registers */
  164. #define ST_LIS2DUXS12_PAGE_SEL_ADDR 0x02
  165. #define ST_LIS2DUXS12_EMB_FUNC_EN_A_ADDR 0x04
  166. #define ST_LIS2DUXS12_PEDO_EN_MASK BIT(3)
  167. #define ST_LIS2DUXS12_TILT_EN_MASK BIT(4)
  168. #define ST_LIS2DUXS12_SIGN_MOTION_EN_MASK BIT(5)
  169. #define ST_LIS2DUXS12_MLC_BEFORE_FSM_EN_MASK BIT(7)
  170. #define ST_LIS2DUXS12_EMB_FUNC_EN_B_ADDR 0x05
  171. #define ST_LIS2DUXS12_FSM_EN_MASK BIT(0)
  172. #define ST_LIS2DUXS12_MLC_EN_MASK BIT(4)
  173. #define ST_LIS2DUXS12_EMB_FUNC_EXEC_STATUS_ADDR 0x07
  174. #define ST_LIS2DUXS12_FUNC_ENDOP_MASK BIT(0)
  175. #define ST_LIS2DUXS12_FUNC_EXEC_OVR_MASK BIT(1)
  176. #define ST_LIS2DUXS12_PAGE_ADDRESS_ADDR 0x08
  177. #define ST_LIS2DUXS12_PAGE_VALUE_ADDR 0x09
  178. #define ST_LIS2DUXS12_EMB_FUNC_INT1_ADDR 0x0a
  179. #define ST_LIS2DUXS12_INT_STEP_DETECTOR_MASK BIT(3)
  180. #define ST_LIS2DUXS12_INT_TILT_MASK BIT(4)
  181. #define ST_LIS2DUXS12_INT_SIG_MOT_MASK BIT(5)
  182. #define ST_LIS2DUXS12_FSM_INT1_ADDR 0x0b
  183. #define ST_LIS2DUXS12_MLC_INT1_ADDR 0x0d
  184. #define ST_LIS2DUXS12_EMB_FUNC_INT2_ADDR 0x0e
  185. #define ST_LIS2DUXS12_FSM_INT2_ADDR 0x0f
  186. #define ST_LIS2DUXS12_MLC_INT2_ADDR 0x11
  187. #define ST_LIS2DUXS12_EMB_FUNC_STATUS_ADDR 0x12
  188. #define ST_LIS2DUXS12_EMB_IS_STEP_DET_MASK BIT(3)
  189. #define ST_LIS2DUXS12_EMB_IS_TILT_MASK BIT(4)
  190. #define ST_LIS2DUXS12_EMB_IS_SIGMOT_MASK BIT(5)
  191. #define ST_LIS2DUXS12_FSM_STATUS_ADDR 0x13
  192. #define ST_LIS2DUXS12_MLC_STATUS_ADDR 0x15
  193. #define ST_LIS2DUXS12_PAGE_RW_ADDR 0x17
  194. #define ST_LIS2DUXS12_EMB_FUNC_LIR_MASK BIT(7)
  195. #define ST_LIS2DUXS12_EMB_FUNC_FIFO_EN_ADDR 0x18
  196. #define ST_LIS2DUXS12_STEP_COUNTER_FIFO_EN_MASK BIT(0)
  197. #define ST_LIS2DUXS12_MLC_FIFO_EN_MASK BIT(1)
  198. #define ST_LIS2DUXS12_MLC_FILTER_FEATURE_FIFO_EN_MASK BIT(2)
  199. #define ST_LIS2DUXS12_FSM_FIFO_EN_MASK BIT(3)
  200. #define ST_LIS2DUXS12_FSM_ENABLE_ADDR 0x1a
  201. #define ST_LIS2DUXS12_FSM_OUTS1_ADDR 0x20
  202. #define ST_LIS2DUXS12_STEP_COUNTER_L_ADDR 0x28
  203. #define ST_LIS2DUXS12_STEP_COUNTER_H_ADDR 0x29
  204. #define ST_LIS2DUXS12_EMB_FUNC_SRC_ADDR 0x2a
  205. #define ST_LIS2DUXS12_STEP_DETECTED_MASK BIT(5)
  206. #define ST_LIS2DUXS12_PEDO_RST_STEP_MASK BIT(7)
  207. #define ST_LIS2DUXS12_EMB_FUNC_INIT_A_ADDR 0x2c
  208. #define ST_LIS2DUXS12_STEP_DET_INIT_MASK BIT(3)
  209. #define ST_LIS2DUXS12_TILT_INIT_MASK BIT(4)
  210. #define ST_LIS2DUXS12_SIG_MOT_INIT_MASK BIT(5)
  211. #define ST_LIS2DUXS12_MLC_BEFORE_FSM_INIT_MASK BIT(7)
  212. #define ST_LIS2DUXS12_EMB_FUNC_INIT_B_ADDR 0x2d
  213. #define ST_LIS2DUXS12_FSM_INIT_MASK BIT(0)
  214. #define ST_LIS2DUXS12_MLC_INIT_MASK BIT(4)
  215. #define ST_LIS2DUXS12_MLC1_SRC_ADDR 0x34
  216. #define ST_LIS2DUXS12_FSM_ODR_ADDR 0x39
  217. #define ST_LIS2DUXS12_FSM_ODR_MASK GENMASK(5, 3)
  218. #define ST_LIS2DUXS12_MLC_ODR_ADDR 0x3a
  219. #define ST_LIS2DUXS12_MLC_ODR_MASK GENMASK(6, 4)
  220. /* Timestamp Tick 10us/LSB */
  221. #define ST_LIS2DUXS12_TS_DELTA_NS 10000ULL
  222. /* Temperature in uC */
  223. #define ST_LIS2DUXS12_TEMP_GAIN 256
  224. #define ST_LIS2DUXS12_TEMP_OFFSET 6400
  225. /* FIFO simple size and depth */
  226. #define ST_LIS2DUXS12_SAMPLE_SIZE 6
  227. #define ST_LIS2DUXS12_TS_SAMPLE_SIZE 4
  228. #define ST_LIS2DUXS12_TAG_SIZE 1
  229. #define ST_LIS2DUXS12_FIFO_SAMPLE_SIZE (ST_LIS2DUXS12_SAMPLE_SIZE + \
  230. ST_LIS2DUXS12_TAG_SIZE)
  231. #define ST_LIS2DUXS12_MAX_FIFO_DEPTH 127
  232. struct __packed raw_data_compact_t {
  233. __le16 x:12;
  234. __le16 y:12;
  235. __le16 z:12;
  236. __le16 t:12;
  237. };
  238. struct __packed raw_data_t {
  239. __le16 x;
  240. __le16 y;
  241. __le16 z;
  242. };
  243. #define ST_LIS2DUXS12_DATA_CHANNEL(chan_type, addr, mod, ch2, scan_idx, \
  244. rb, sb, sg, ext_inf) \
  245. { \
  246. .type = chan_type, \
  247. .address = addr, \
  248. .modified = mod, \
  249. .channel2 = ch2, \
  250. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  251. BIT(IIO_CHAN_INFO_SCALE), \
  252. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  253. .scan_index = scan_idx, \
  254. .scan_type = { \
  255. .sign = sg, \
  256. .realbits = rb, \
  257. .storagebits = sb, \
  258. .shift = sb - rb, \
  259. .endianness = IIO_LE, \
  260. }, \
  261. .ext_info = ext_inf, \
  262. }
  263. static const struct iio_event_spec st_lis2duxs12_flush_event = {
  264. .type = STM_IIO_EV_TYPE_FIFO_FLUSH,
  265. .dir = IIO_EV_DIR_EITHER,
  266. };
  267. static const struct iio_event_spec st_lis2duxs12_thr_event = {
  268. .type = IIO_EV_TYPE_THRESH,
  269. .dir = IIO_EV_DIR_RISING,
  270. .mask_separate = BIT(IIO_EV_INFO_ENABLE),
  271. };
  272. #define ST_LIS2DUXS12_EVENT_CHANNEL(ctype, etype) \
  273. { \
  274. .type = ctype, \
  275. .modified = 0, \
  276. .scan_index = -1, \
  277. .indexed = -1, \
  278. .event_spec = &st_lis2duxs12_##etype##_event, \
  279. .num_event_specs = 1, \
  280. }
  281. #define ST_LIS2DUXS12_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
  282. #define ST_LIS2DUXS12_DESHIFT_VAL(val, mask) (((val) & (mask)) >> __ffs(mask))
  283. enum st_lis2duxs12_pm_t {
  284. ST_LIS2DUXS12_LP_MODE = 0,
  285. ST_LIS2DUXS12_HP_MODE,
  286. ST_LIS2DUXS12_NO_MODE,
  287. };
  288. enum st_lis2duxs12_fsm_mlc_enable_id {
  289. ST_LIS2DUXS12_MLC_FSM_DISABLED = 0,
  290. ST_LIS2DUXS12_MLC_ENABLED = BIT(0),
  291. ST_LIS2DUXS12_FSM_ENABLED = BIT(1),
  292. };
  293. /**
  294. * struct mlc_config_t -
  295. * @mlc_int_addr: interrupt register address.
  296. * @mlc_int_mask: interrupt register mask.
  297. * @fsm_int_addr: interrupt register address.
  298. * @fsm_int_mask: interrupt register mask.
  299. * @mlc_configured: number of mlc configured.
  300. * @fsm_configured: number of fsm configured.
  301. * @bin_len: fw binary size.
  302. * @requested_odr: Min ODR requested to works properly.
  303. * @requested_device: Device bitmask requested by firmware.
  304. * @status: MLC / FSM enabled status.
  305. */
  306. struct st_lis2duxs12_mlc_config_t {
  307. uint8_t mlc_int_addr;
  308. uint8_t mlc_int_mask;
  309. uint8_t fsm_int_addr;
  310. uint8_t fsm_int_mask;
  311. uint8_t mlc_configured;
  312. uint8_t fsm_configured;
  313. uint16_t bin_len;
  314. uint16_t requested_odr;
  315. uint32_t requested_device;
  316. enum st_lis2duxs12_fsm_mlc_enable_id status;
  317. };
  318. /**
  319. * struct st_lis2duxs12_ff_th - Free Fall threshold table
  320. * @mg: Threshold in mg.
  321. * @val: Register value.
  322. */
  323. struct st_lis2duxs12_ff_th {
  324. u32 mg;
  325. u8 val;
  326. };
  327. /**
  328. * struct st_lis2duxs12_6D_th - 6D threshold table
  329. * @deg: Threshold in degrees.
  330. * @val: Register value.
  331. */
  332. struct st_lis2duxs12_6D_th {
  333. u8 deg;
  334. u8 val;
  335. };
  336. /**
  337. * struct st_lis2duxs12_reg - Generic sensor register description addr +
  338. * mask
  339. * @addr: Address of register.
  340. * @mask: Bitmask register for proper usage.
  341. */
  342. struct st_lis2duxs12_reg {
  343. u8 addr;
  344. u8 mask;
  345. };
  346. /**
  347. * Define embedded functions register access
  348. *
  349. * FUNC_CFG_ACCESS_0 is default bank
  350. * FUNC_CFG_ACCESS_FUNC_CFG Enable access to the embedded functions
  351. * configuration registers.
  352. */
  353. enum st_lis2duxs12_page_sel_register {
  354. FUNC_CFG_ACCESS_0 = 0,
  355. FUNC_CFG_ACCESS_FUNC_CFG,
  356. };
  357. /**
  358. * struct st_lis2duxs12_odr - Single ODR entry
  359. * @hz: Most significant part of the sensor ODR (Hz).
  360. * @uhz: Less significant part of the sensor ODR (micro Hz).
  361. * @val: ODR register value.
  362. */
  363. struct st_lis2duxs12_odr {
  364. u16 hz;
  365. u32 uhz;
  366. u8 val;
  367. };
  368. /**
  369. * struct st_lis2duxs12_odr_table_entry - Sensor ODR table
  370. * @size: Size of ODR table.
  371. * @reg: ODR register.
  372. * @pm: Power mode register.
  373. * @batching_reg: ODR register for batching on fifo.
  374. * @odr_avl: Array of supported ODR value.
  375. */
  376. struct st_lis2duxs12_odr_table_entry {
  377. u8 size;
  378. struct st_lis2duxs12_reg reg;
  379. struct st_lis2duxs12_reg pm;
  380. struct st_lis2duxs12_odr odr_avl[10];
  381. };
  382. /**
  383. * struct st_lis2duxs12_fs - Full Scale sensor table entry
  384. * @gain: Sensor sensitivity (mdps/LSB, mg/LSB and uC/LSB).
  385. * @val: FS register value.
  386. */
  387. struct st_lis2duxs12_fs {
  388. u32 gain;
  389. u8 val;
  390. };
  391. /**
  392. * struct st_lis2duxs12_fs_table_entry - Full Scale sensor table
  393. * @size: Full Scale sensor table size.
  394. * @reg: Register description for FS settings.
  395. * @fs_avl: Full Scale list entries.
  396. */
  397. struct st_lis2duxs12_fs_table_entry {
  398. u8 size;
  399. struct st_lis2duxs12_reg reg;
  400. struct st_lis2duxs12_fs fs_avl[4];
  401. };
  402. enum st_lis2duxs12_sensor_id {
  403. ST_LIS2DUXS12_ID_ACC = 0,
  404. ST_LIS2DUXS12_ID_TEMP,
  405. ST_LIS2DUXS12_ID_STEP_COUNTER,
  406. ST_LIS2DUXS12_ID_STEP_DETECTOR,
  407. ST_LIS2DUXS12_ID_SIGN_MOTION,
  408. ST_LIS2DUXS12_ID_TILT,
  409. ST_LIS2DUXS12_ID_QVAR,
  410. ST_LIS2DUXS12_ID_FF,
  411. ST_LIS2DUXS12_ID_SC,
  412. ST_LIS2DUXS12_ID_WK,
  413. ST_LIS2DUXS12_ID_6D,
  414. ST_LIS2DUXS12_ID_TAP,
  415. ST_LIS2DUXS12_ID_DTAP,
  416. ST_LIS2DUXS12_ID_TTAP,
  417. ST_LIS2DUXS12_ID_MLC,
  418. ST_LIS2DUXS12_ID_MLC_0,
  419. ST_LIS2DUXS12_ID_MLC_1,
  420. ST_LIS2DUXS12_ID_MLC_2,
  421. ST_LIS2DUXS12_ID_MLC_3,
  422. ST_LIS2DUXS12_ID_FSM_0,
  423. ST_LIS2DUXS12_ID_FSM_1,
  424. ST_LIS2DUXS12_ID_FSM_2,
  425. ST_LIS2DUXS12_ID_FSM_3,
  426. ST_LIS2DUXS12_ID_FSM_4,
  427. ST_LIS2DUXS12_ID_FSM_5,
  428. ST_LIS2DUXS12_ID_FSM_6,
  429. ST_LIS2DUXS12_ID_FSM_7,
  430. ST_LIS2DUXS12_ID_MAX,
  431. };
  432. static const enum
  433. st_lis2duxs12_sensor_id st_lis2duxs12_buffered_sensor_list[] = {
  434. [0] = ST_LIS2DUXS12_ID_ACC,
  435. [1] = ST_LIS2DUXS12_ID_TEMP,
  436. [2] = ST_LIS2DUXS12_ID_STEP_COUNTER,
  437. [3] = ST_LIS2DUXS12_ID_QVAR,
  438. };
  439. #define ST_LIS2DUXS12_BUFFERED_ENABLED (BIT(ST_LIS2DUXS12_ID_ACC) | \
  440. BIT(ST_LIS2DUXS12_ID_TEMP) | \
  441. BIT(ST_LIS2DUXS12_ID_STEP_COUNTER) | \
  442. BIT(ST_LIS2DUXS12_ID_QVAR))
  443. static const enum
  444. st_lis2duxs12_sensor_id st_lis2duxs12_mlc_sensor_list[] = {
  445. [0] = ST_LIS2DUXS12_ID_MLC_0,
  446. [1] = ST_LIS2DUXS12_ID_MLC_1,
  447. [2] = ST_LIS2DUXS12_ID_MLC_2,
  448. [3] = ST_LIS2DUXS12_ID_MLC_3,
  449. };
  450. static const enum
  451. st_lis2duxs12_sensor_id st_lis2duxs12_fsm_sensor_list[] = {
  452. [0] = ST_LIS2DUXS12_ID_FSM_0,
  453. [1] = ST_LIS2DUXS12_ID_FSM_1,
  454. [2] = ST_LIS2DUXS12_ID_FSM_2,
  455. [3] = ST_LIS2DUXS12_ID_FSM_3,
  456. [4] = ST_LIS2DUXS12_ID_FSM_4,
  457. [5] = ST_LIS2DUXS12_ID_FSM_5,
  458. [6] = ST_LIS2DUXS12_ID_FSM_6,
  459. [7] = ST_LIS2DUXS12_ID_FSM_7,
  460. };
  461. #define ST_LIS2DUXS12_EMB_FUNC_ENABLED (BIT(ST_LIS2DUXS12_ID_STEP_DETECTOR) | \
  462. BIT(ST_LIS2DUXS12_ID_SIGN_MOTION) | \
  463. BIT(ST_LIS2DUXS12_ID_TILT))
  464. #define ST_LIS2DUXS12_BASIC_FUNC_ENABLED (GENMASK(ST_LIS2DUXS12_ID_TTAP, \
  465. ST_LIS2DUXS12_ID_FF))
  466. /* HW devices that can wakeup the target */
  467. #define ST_LIS2DUXS12_WAKE_UP_SENSORS (BIT(ST_LIS2DUXS12_ID_ACC) | \
  468. BIT(ST_LIS2DUXS12_ID_MLC_0) | \
  469. BIT(ST_LIS2DUXS12_ID_MLC_1) | \
  470. BIT(ST_LIS2DUXS12_ID_MLC_2) | \
  471. BIT(ST_LIS2DUXS12_ID_MLC_3) | \
  472. BIT(ST_LIS2DUXS12_ID_FSM_0) | \
  473. BIT(ST_LIS2DUXS12_ID_FSM_1) | \
  474. BIT(ST_LIS2DUXS12_ID_FSM_2) | \
  475. BIT(ST_LIS2DUXS12_ID_FSM_3) | \
  476. BIT(ST_LIS2DUXS12_ID_FSM_4) | \
  477. BIT(ST_LIS2DUXS12_ID_FSM_5) | \
  478. BIT(ST_LIS2DUXS12_ID_FSM_6) | \
  479. BIT(ST_LIS2DUXS12_ID_FSM_7))
  480. /* this is the minimal ODR for wake-up sensors and dependencies */
  481. #define ST_LIS2DUXS12_MIN_ODR_IN_WAKEUP 25
  482. enum st_lis2duxs12_fifo_mode {
  483. ST_LIS2DUXS12_FIFO_BYPASS = 0x0,
  484. ST_LIS2DUXS12_FIFO_CONT = 0x6,
  485. };
  486. enum {
  487. ST_LIS2DUXS12_HW_FLUSH,
  488. ST_LIS2DUXS12_HW_OPERATIONAL,
  489. };
  490. enum st_lis2duxs12_hw_id {
  491. ST_LIS2DUX12_ID,
  492. ST_LIS2DUXS12_ID,
  493. ST_LIS2DUXS12_MAX_ID,
  494. };
  495. /**
  496. * struct st_lis2duxs12_settings - ST IMU sensor settings
  497. *
  498. * @hw_id: Hw id supported by the driver configuration.
  499. * @name: Device name supported by the driver configuration.
  500. * @st_qvar_support: QVAR supported flag.
  501. */
  502. struct st_lis2duxs12_settings {
  503. struct {
  504. enum st_lis2duxs12_hw_id hw_id;
  505. const char *name;
  506. } id;
  507. bool st_qvar_support;
  508. };
  509. /**
  510. * struct st_lis2duxs12_sensor - ST ACC sensor instance
  511. */
  512. struct st_lis2duxs12_sensor {
  513. char name[32];
  514. enum st_lis2duxs12_sensor_id id;
  515. struct st_lis2duxs12_hw *hw;
  516. struct iio_trigger *trig;
  517. int odr;
  518. int uodr;
  519. union {
  520. /* sensor with odrs, gain and offset */
  521. struct {
  522. u32 gain;
  523. u32 offset;
  524. u8 decimator;
  525. u8 dec_counter;
  526. __le16 old_data;
  527. u8 max_watermark;
  528. u8 watermark;
  529. enum st_lis2duxs12_pm_t pm;
  530. /* self test */
  531. int8_t selftest_status;
  532. int min_st;
  533. int max_st;
  534. };
  535. /* mlc/fsm event sensors */
  536. struct {
  537. uint8_t status_reg;
  538. uint8_t outreg_addr;
  539. enum st_lis2duxs12_fsm_mlc_enable_id status;
  540. };
  541. /* sensor specific data configuration */
  542. struct {
  543. u32 conf[6];
  544. /* Ensure natural alignment of timestamp */
  545. struct {
  546. u8 event;
  547. s64 ts __aligned(8);
  548. } scan;
  549. };
  550. };
  551. };
  552. /**
  553. * struct st_lis2duxs12_hw - ST ACC MEMS hw instance
  554. */
  555. struct st_lis2duxs12_hw {
  556. struct device *dev;
  557. int irq;
  558. struct regmap *regmap;
  559. struct mutex page_lock;
  560. struct mutex fifo_lock;
  561. enum st_lis2duxs12_fifo_mode fifo_mode;
  562. unsigned long state;
  563. bool xl_only;
  564. bool timestamp;
  565. u8 std_level;
  566. u64 samples;
  567. u32 enable_mask;
  568. u32 requested_mask;
  569. s64 ts_offset;
  570. s64 hw_ts;
  571. s64 tsample;
  572. s64 delta_ts;
  573. s64 ts;
  574. s64 last_fifo_timestamp;
  575. struct iio_mount_matrix orientation;
  576. struct regulator *vdd_supply;
  577. struct regulator *vddio_supply;
  578. struct st_lis2duxs12_mlc_config_t *mlc_config;
  579. const struct st_lis2duxs12_odr_table_entry *odr_table_entry;
  580. const struct st_lis2duxs12_fs_table_entry *fs_table_entry;
  581. bool preload_mlc;
  582. u8 int_pin;
  583. u8 ft_int_reg;
  584. u8 md_int_reg;
  585. u8 emb_int_reg;
  586. struct iio_dev *iio_devs[ST_LIS2DUXS12_ID_MAX];
  587. const struct st_lis2duxs12_settings *settings;
  588. };
  589. extern const struct dev_pm_ops st_lis2duxs12_pm_ops;
  590. static inline bool
  591. st_lis2duxs12_is_fifo_enabled(struct st_lis2duxs12_hw *hw)
  592. {
  593. return hw->enable_mask & (BIT(ST_LIS2DUXS12_ID_ACC) |
  594. BIT(ST_LIS2DUXS12_ID_TEMP));
  595. }
  596. static inline int
  597. __st_lis2duxs12_write_with_mask(struct st_lis2duxs12_hw *hw,
  598. unsigned int addr,
  599. unsigned int mask,
  600. unsigned int data)
  601. {
  602. int err;
  603. unsigned int val = ST_LIS2DUXS12_SHIFT_VAL(data, mask);
  604. err = regmap_update_bits(hw->regmap, addr, mask, val);
  605. return err;
  606. }
  607. static inline int
  608. st_lis2duxs12_update_bits_locked(struct st_lis2duxs12_hw *hw,
  609. unsigned int addr, unsigned int mask,
  610. unsigned int val)
  611. {
  612. int err;
  613. mutex_lock(&hw->page_lock);
  614. err = __st_lis2duxs12_write_with_mask(hw, addr, mask, val);
  615. mutex_unlock(&hw->page_lock);
  616. return err;
  617. }
  618. /* use when mask is constant */
  619. static inline int
  620. st_lis2duxs12_write_with_mask_locked(struct st_lis2duxs12_hw *hw,
  621. unsigned int addr,
  622. unsigned int mask,
  623. unsigned int data)
  624. {
  625. int err;
  626. unsigned int val = FIELD_PREP(mask, data);
  627. mutex_lock(&hw->page_lock);
  628. err = regmap_update_bits(hw->regmap, addr, mask, val);
  629. mutex_unlock(&hw->page_lock);
  630. return err;
  631. }
  632. static inline int
  633. st_lis2duxs12_read_locked(struct st_lis2duxs12_hw *hw,
  634. unsigned int addr, void *val,
  635. unsigned int len)
  636. {
  637. int err;
  638. mutex_lock(&hw->page_lock);
  639. err = regmap_bulk_read(hw->regmap, addr, val, len);
  640. mutex_unlock(&hw->page_lock);
  641. return err;
  642. }
  643. static inline int
  644. st_lis2duxs12_read_with_mask(struct st_lis2duxs12_hw *hw,
  645. unsigned int addr, unsigned int mask,
  646. u8 *val)
  647. {
  648. unsigned int data;
  649. int err;
  650. err = regmap_read(hw->regmap, addr, &data);
  651. *val = (u8)ST_LIS2DUXS12_DESHIFT_VAL(data, mask);
  652. return err;
  653. }
  654. static inline int
  655. st_lis2duxs12_read_with_mask_locked(struct st_lis2duxs12_hw *hw,
  656. unsigned int addr,
  657. unsigned int mask, u8 *val)
  658. {
  659. unsigned int data;
  660. int err;
  661. mutex_lock(&hw->page_lock);
  662. err = regmap_read(hw->regmap, addr, &data);
  663. mutex_unlock(&hw->page_lock);
  664. *val = (u8)ST_LIS2DUXS12_DESHIFT_VAL(data, mask);
  665. return err;
  666. }
  667. static inline int
  668. st_lis2duxs12_write_locked(struct st_lis2duxs12_hw *hw,
  669. unsigned int addr, unsigned int val)
  670. {
  671. int err;
  672. mutex_lock(&hw->page_lock);
  673. err = regmap_write(hw->regmap, addr, val);
  674. mutex_unlock(&hw->page_lock);
  675. return err;
  676. }
  677. static inline int
  678. st_lis2duxs12_set_emb_access(struct st_lis2duxs12_hw *hw,
  679. unsigned int val)
  680. {
  681. return regmap_write(hw->regmap,
  682. ST_LIS2DUXS12_FUNC_CFG_ACCESS_ADDR,
  683. val ? ST_LIS2DUXS12_EMB_FUNC_REG_ACCESS_MASK : 0);
  684. }
  685. static inline int
  686. st_lis2duxs12_read_page_locked(struct st_lis2duxs12_hw *hw,
  687. unsigned int addr, void *val,
  688. unsigned int len)
  689. {
  690. int err;
  691. mutex_lock(&hw->page_lock);
  692. st_lis2duxs12_set_emb_access(hw, 1);
  693. err = regmap_bulk_read(hw->regmap, addr, val, len);
  694. st_lis2duxs12_set_emb_access(hw, 0);
  695. mutex_unlock(&hw->page_lock);
  696. return err;
  697. }
  698. static inline int
  699. st_lis2duxs12_write_page_locked(struct st_lis2duxs12_hw *hw,
  700. unsigned int addr, unsigned int *val,
  701. unsigned int len)
  702. {
  703. int err;
  704. mutex_lock(&hw->page_lock);
  705. st_lis2duxs12_set_emb_access(hw, 1);
  706. err = regmap_bulk_write(hw->regmap, addr, val, len);
  707. st_lis2duxs12_set_emb_access(hw, 0);
  708. mutex_unlock(&hw->page_lock);
  709. return err;
  710. }
  711. static inline int
  712. st_lis2duxs12_update_page_bits_locked(struct st_lis2duxs12_hw *hw,
  713. unsigned int addr,
  714. unsigned int mask,
  715. unsigned int val)
  716. {
  717. int err;
  718. mutex_lock(&hw->page_lock);
  719. st_lis2duxs12_set_emb_access(hw, 1);
  720. err = regmap_update_bits(hw->regmap, addr, mask, val);
  721. st_lis2duxs12_set_emb_access(hw, 0);
  722. mutex_unlock(&hw->page_lock);
  723. return err;
  724. }
  725. int st_lis2duxs12_probe(struct device *dev, int irq,
  726. enum st_lis2duxs12_hw_id hw_id, struct regmap *regmap);
  727. int st_lis2duxs12_remove(struct device *dev);
  728. int st_lis2duxs12_sensor_set_enable(struct st_lis2duxs12_sensor *sensor,
  729. bool enable);
  730. int st_lis2duxs12_buffers_setup(struct st_lis2duxs12_hw *hw);
  731. ssize_t st_lis2duxs12_flush_fifo(struct device *dev,
  732. struct device_attribute *attr,
  733. const char *buf, size_t size);
  734. ssize_t st_lis2duxs12_get_max_watermark(struct device *dev,
  735. struct device_attribute *attr,
  736. char *buf);
  737. ssize_t st_lis2duxs12_get_watermark(struct device *dev,
  738. struct device_attribute *attr,
  739. char *buf);
  740. ssize_t st_lis2duxs12_set_watermark(struct device *dev,
  741. struct device_attribute *attr,
  742. const char *buf, size_t size);
  743. int st_lis2duxs12_suspend_fifo(struct st_lis2duxs12_hw *hw);
  744. int st_lis2duxs12_set_fifo_mode(struct st_lis2duxs12_hw *hw,
  745. enum st_lis2duxs12_fifo_mode fifo_mode);
  746. int st_lis2duxs12_update_batching(struct iio_dev *iio_dev, bool enable);
  747. /* mlc / fsm */
  748. int st_lis2duxs12_mlc_probe(struct st_lis2duxs12_hw *hw);
  749. int st_lis2duxs12_mlc_remove(struct device *dev);
  750. int st_lis2duxs12_mlc_check_status(struct st_lis2duxs12_hw *hw);
  751. int st_lis2duxs12_mlc_init_preload(struct st_lis2duxs12_hw *hw);
  752. int st_lis2duxs12_reset_step_counter(struct iio_dev *iio_dev);
  753. int st_lis2duxs12_embedded_function_init(struct st_lis2duxs12_hw *hw);
  754. int st_lis2duxs12_step_counter_set_enable(struct st_lis2duxs12_sensor *sensor,
  755. bool enable);
  756. int st_lis2duxs12_embfunc_sensor_set_enable(struct st_lis2duxs12_sensor *sensor,
  757. bool enable);
  758. int st_lis2duxs12_probe_basicfunc(struct st_lis2duxs12_hw *hw);
  759. int st_lis2duxs12_event_handler(struct st_lis2duxs12_hw *hw);
  760. /* qvar */
  761. int st_lis2duxs12_qvar_probe(struct st_lis2duxs12_hw *hw);
  762. int st_lis2duxs12_qvar_set_enable(struct st_lis2duxs12_sensor *sensor,
  763. bool enable);
  764. #endif /* ST_LIS2DUXS12_H */