lis3lv02d.h 7.7 KB

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