lis3lv02d.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * lis3lv02d.h - ST LIS3LV02DL accelerometer driver
  4. *
  5. * Copyright (C) 2007-2008 Yan Burman
  6. * Copyright (C) 2008-2009 Eric Piel
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/input.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/miscdevice.h>
  12. /*
  13. * This driver tries to support the "digital" accelerometer chips from
  14. * STMicroelectronics such as LIS3LV02DL, LIS302DL, LIS3L02DQ, LIS331DL,
  15. * LIS331DLH, LIS35DE, or LIS202DL. They are very similar in terms of
  16. * programming, with almost the same registers. In addition to differing
  17. * on physical properties, they differ on the number of axes (2/3),
  18. * precision (8/12 bits), and special features (freefall detection,
  19. * click...). Unfortunately, not all the differences can be probed via
  20. * a register. They can be connected either via I²C or SPI.
  21. */
  22. #include <linux/lis3lv02d.h>
  23. enum lis3_reg {
  24. WHO_AM_I = 0x0F,
  25. OFFSET_X = 0x16,
  26. OFFSET_Y = 0x17,
  27. OFFSET_Z = 0x18,
  28. GAIN_X = 0x19,
  29. GAIN_Y = 0x1A,
  30. GAIN_Z = 0x1B,
  31. CTRL_REG1 = 0x20,
  32. CTRL_REG2 = 0x21,
  33. CTRL_REG3 = 0x22,
  34. CTRL_REG4 = 0x23,
  35. HP_FILTER_RESET = 0x23,
  36. STATUS_REG = 0x27,
  37. OUTX_L = 0x28,
  38. OUTX_H = 0x29,
  39. OUTX = 0x29,
  40. OUTY_L = 0x2A,
  41. OUTY_H = 0x2B,
  42. OUTY = 0x2B,
  43. OUTZ_L = 0x2C,
  44. OUTZ_H = 0x2D,
  45. OUTZ = 0x2D,
  46. };
  47. enum lis302d_reg {
  48. FF_WU_CFG_1 = 0x30,
  49. FF_WU_SRC_1 = 0x31,
  50. FF_WU_THS_1 = 0x32,
  51. FF_WU_DURATION_1 = 0x33,
  52. FF_WU_CFG_2 = 0x34,
  53. FF_WU_SRC_2 = 0x35,
  54. FF_WU_THS_2 = 0x36,
  55. FF_WU_DURATION_2 = 0x37,
  56. CLICK_CFG = 0x38,
  57. CLICK_SRC = 0x39,
  58. CLICK_THSY_X = 0x3B,
  59. CLICK_THSZ = 0x3C,
  60. CLICK_TIMELIMIT = 0x3D,
  61. CLICK_LATENCY = 0x3E,
  62. CLICK_WINDOW = 0x3F,
  63. };
  64. enum lis3lv02d_reg {
  65. FF_WU_CFG = 0x30,
  66. FF_WU_SRC = 0x31,
  67. FF_WU_ACK = 0x32,
  68. FF_WU_THS_L = 0x34,
  69. FF_WU_THS_H = 0x35,
  70. FF_WU_DURATION = 0x36,
  71. DD_CFG = 0x38,
  72. DD_SRC = 0x39,
  73. DD_ACK = 0x3A,
  74. DD_THSI_L = 0x3C,
  75. DD_THSI_H = 0x3D,
  76. DD_THSE_L = 0x3E,
  77. DD_THSE_H = 0x3F,
  78. };
  79. enum lis3_who_am_i {
  80. WAI_3DLH = 0x32, /* 16 bits: LIS331DLH */
  81. WAI_3DC = 0x33, /* 8 bits: LIS3DC, HP3DC */
  82. WAI_12B = 0x3A, /* 12 bits: LIS3LV02D[LQ]... */
  83. WAI_8B = 0x3B, /* 8 bits: LIS[23]02D[LQ]... */
  84. WAI_6B = 0x52, /* 6 bits: LIS331DLF - not supported */
  85. };
  86. enum lis3_type {
  87. LIS3LV02D,
  88. LIS3DC,
  89. HP3DC,
  90. LIS2302D,
  91. LIS331DLF,
  92. LIS331DLH,
  93. };
  94. enum lis3lv02d_ctrl1_12b {
  95. CTRL1_Xen = 0x01,
  96. CTRL1_Yen = 0x02,
  97. CTRL1_Zen = 0x04,
  98. CTRL1_ST = 0x08,
  99. CTRL1_DF0 = 0x10,
  100. CTRL1_DF1 = 0x20,
  101. CTRL1_PD0 = 0x40,
  102. CTRL1_PD1 = 0x80,
  103. };
  104. /* Delta to ctrl1_12b version */
  105. enum lis3lv02d_ctrl1_8b {
  106. CTRL1_STM = 0x08,
  107. CTRL1_STP = 0x10,
  108. CTRL1_FS = 0x20,
  109. CTRL1_PD = 0x40,
  110. CTRL1_DR = 0x80,
  111. };
  112. enum lis3lv02d_ctrl1_3dc {
  113. CTRL1_ODR0 = 0x10,
  114. CTRL1_ODR1 = 0x20,
  115. CTRL1_ODR2 = 0x40,
  116. CTRL1_ODR3 = 0x80,
  117. };
  118. enum lis331dlh_ctrl1 {
  119. CTRL1_DR0 = 0x08,
  120. CTRL1_DR1 = 0x10,
  121. CTRL1_PM0 = 0x20,
  122. CTRL1_PM1 = 0x40,
  123. CTRL1_PM2 = 0x80,
  124. };
  125. enum lis331dlh_ctrl2 {
  126. CTRL2_HPEN1 = 0x04,
  127. CTRL2_HPEN2 = 0x08,
  128. CTRL2_FDS_3DLH = 0x10,
  129. CTRL2_BOOT_3DLH = 0x80,
  130. };
  131. enum lis331dlh_ctrl4 {
  132. CTRL4_STSIGN = 0x08,
  133. CTRL4_BLE = 0x40,
  134. CTRL4_BDU = 0x80,
  135. };
  136. enum lis3lv02d_ctrl2 {
  137. CTRL2_DAS = 0x01,
  138. CTRL2_SIM = 0x02,
  139. CTRL2_DRDY = 0x04,
  140. CTRL2_IEN = 0x08,
  141. CTRL2_BOOT = 0x10,
  142. CTRL2_BLE = 0x20,
  143. CTRL2_BDU = 0x40, /* Block Data Update */
  144. CTRL2_FS = 0x80, /* Full Scale selection */
  145. };
  146. enum lis3lv02d_ctrl4_3dc {
  147. CTRL4_SIM = 0x01,
  148. CTRL4_ST0 = 0x02,
  149. CTRL4_ST1 = 0x04,
  150. CTRL4_FS0 = 0x10,
  151. CTRL4_FS1 = 0x20,
  152. };
  153. enum lis302d_ctrl2 {
  154. HP_FF_WU2 = 0x08,
  155. HP_FF_WU1 = 0x04,
  156. CTRL2_BOOT_8B = 0x40,
  157. };
  158. enum lis3lv02d_ctrl3 {
  159. CTRL3_CFS0 = 0x01,
  160. CTRL3_CFS1 = 0x02,
  161. CTRL3_FDS = 0x10,
  162. CTRL3_HPFF = 0x20,
  163. CTRL3_HPDD = 0x40,
  164. CTRL3_ECK = 0x80,
  165. };
  166. enum lis3lv02d_status_reg {
  167. STATUS_XDA = 0x01,
  168. STATUS_YDA = 0x02,
  169. STATUS_ZDA = 0x04,
  170. STATUS_XYZDA = 0x08,
  171. STATUS_XOR = 0x10,
  172. STATUS_YOR = 0x20,
  173. STATUS_ZOR = 0x40,
  174. STATUS_XYZOR = 0x80,
  175. };
  176. enum lis3lv02d_ff_wu_cfg {
  177. FF_WU_CFG_XLIE = 0x01,
  178. FF_WU_CFG_XHIE = 0x02,
  179. FF_WU_CFG_YLIE = 0x04,
  180. FF_WU_CFG_YHIE = 0x08,
  181. FF_WU_CFG_ZLIE = 0x10,
  182. FF_WU_CFG_ZHIE = 0x20,
  183. FF_WU_CFG_LIR = 0x40,
  184. FF_WU_CFG_AOI = 0x80,
  185. };
  186. enum lis3lv02d_ff_wu_src {
  187. FF_WU_SRC_XL = 0x01,
  188. FF_WU_SRC_XH = 0x02,
  189. FF_WU_SRC_YL = 0x04,
  190. FF_WU_SRC_YH = 0x08,
  191. FF_WU_SRC_ZL = 0x10,
  192. FF_WU_SRC_ZH = 0x20,
  193. FF_WU_SRC_IA = 0x40,
  194. };
  195. enum lis3lv02d_dd_cfg {
  196. DD_CFG_XLIE = 0x01,
  197. DD_CFG_XHIE = 0x02,
  198. DD_CFG_YLIE = 0x04,
  199. DD_CFG_YHIE = 0x08,
  200. DD_CFG_ZLIE = 0x10,
  201. DD_CFG_ZHIE = 0x20,
  202. DD_CFG_LIR = 0x40,
  203. DD_CFG_IEND = 0x80,
  204. };
  205. enum lis3lv02d_dd_src {
  206. DD_SRC_XL = 0x01,
  207. DD_SRC_XH = 0x02,
  208. DD_SRC_YL = 0x04,
  209. DD_SRC_YH = 0x08,
  210. DD_SRC_ZL = 0x10,
  211. DD_SRC_ZH = 0x20,
  212. DD_SRC_IA = 0x40,
  213. };
  214. enum lis3lv02d_click_src_8b {
  215. CLICK_SINGLE_X = 0x01,
  216. CLICK_DOUBLE_X = 0x02,
  217. CLICK_SINGLE_Y = 0x04,
  218. CLICK_DOUBLE_Y = 0x08,
  219. CLICK_SINGLE_Z = 0x10,
  220. CLICK_DOUBLE_Z = 0x20,
  221. CLICK_IA = 0x40,
  222. };
  223. enum lis3lv02d_reg_state {
  224. LIS3_REG_OFF = 0x00,
  225. LIS3_REG_ON = 0x01,
  226. };
  227. union axis_conversion {
  228. struct {
  229. int x, y, z;
  230. };
  231. int as_array[3];
  232. };
  233. struct lis3lv02d {
  234. void *bus_priv; /* used by the bus layer only */
  235. struct device *pm_dev; /* for pm_runtime purposes */
  236. int (*init) (struct lis3lv02d *lis3);
  237. int (*write) (struct lis3lv02d *lis3, int reg, u8 val);
  238. int (*read) (struct lis3lv02d *lis3, int reg, u8 *ret);
  239. int (*blkread) (struct lis3lv02d *lis3, int reg, int len, u8 *ret);
  240. int (*reg_ctrl) (struct lis3lv02d *lis3, bool state);
  241. int *odrs; /* Supported output data rates */
  242. u8 *regs; /* Regs to store / restore */
  243. int regs_size;
  244. u8 *reg_cache;
  245. bool regs_stored;
  246. u8 odr_mask; /* ODR bit mask */
  247. u8 whoami; /* indicates measurement precision */
  248. s16 (*read_data) (struct lis3lv02d *lis3, int reg);
  249. int mdps_max_val;
  250. int pwron_delay;
  251. int scale; /*
  252. * relationship between 1 LBS and mG
  253. * (1/1000th of earth gravity)
  254. */
  255. struct input_dev *idev; /* input device */
  256. struct platform_device *pdev; /* platform device */
  257. struct regulator_bulk_data regulators[2];
  258. atomic_t count; /* interrupt count after last read */
  259. union axis_conversion ac; /* hw -> logical axis */
  260. int mapped_btns[3];
  261. u32 irq; /* IRQ number */
  262. struct fasync_struct *async_queue; /* queue for the misc device */
  263. wait_queue_head_t misc_wait; /* Wait queue for the misc device */
  264. unsigned long misc_opened; /* bit0: whether the device is open */
  265. struct miscdevice miscdev;
  266. int data_ready_count[2];
  267. atomic_t wake_thread;
  268. unsigned char irq_cfg;
  269. unsigned int shift_adj;
  270. struct lis3lv02d_platform_data *pdata; /* for passing board config */
  271. struct mutex mutex; /* Serialize poll and selftest */
  272. #ifdef CONFIG_OF
  273. struct device_node *of_node;
  274. #endif
  275. };
  276. int lis3lv02d_init_device(struct lis3lv02d *lis3);
  277. int lis3lv02d_joystick_enable(struct lis3lv02d *lis3);
  278. void lis3lv02d_joystick_disable(struct lis3lv02d *lis3);
  279. void lis3lv02d_poweroff(struct lis3lv02d *lis3);
  280. int lis3lv02d_poweron(struct lis3lv02d *lis3);
  281. void lis3lv02d_remove_fs(struct lis3lv02d *lis3);
  282. int lis3lv02d_init_dt(struct lis3lv02d *lis3);
  283. extern struct lis3lv02d lis3_dev;