lis3lv02d.h 7.0 KB

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