lis3lv02d.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. * LIS35DE, or LIS202DL. They are very similar in terms of programming, with
  29. * almost the same registers. In addition to differing on physical properties,
  30. * they differ on the number of axes (2/3), precision (8/12 bits), and special
  31. * features (freefall detection, click...). Unfortunately, not all the
  32. * differences can be probed via a register.
  33. * 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_3DC = 0x33, /* 8 bits: LIS3DC, HP3DC */
  94. WAI_12B = 0x3A, /* 12 bits: LIS3LV02D[LQ]... */
  95. WAI_8B = 0x3B, /* 8 bits: LIS[23]02D[LQ]... */
  96. WAI_6B = 0x52, /* 6 bits: LIS331DLF - not supported */
  97. };
  98. enum lis3lv02d_ctrl1_12b {
  99. CTRL1_Xen = 0x01,
  100. CTRL1_Yen = 0x02,
  101. CTRL1_Zen = 0x04,
  102. CTRL1_ST = 0x08,
  103. CTRL1_DF0 = 0x10,
  104. CTRL1_DF1 = 0x20,
  105. CTRL1_PD0 = 0x40,
  106. CTRL1_PD1 = 0x80,
  107. };
  108. /* Delta to ctrl1_12b version */
  109. enum lis3lv02d_ctrl1_8b {
  110. CTRL1_STM = 0x08,
  111. CTRL1_STP = 0x10,
  112. CTRL1_FS = 0x20,
  113. CTRL1_PD = 0x40,
  114. CTRL1_DR = 0x80,
  115. };
  116. enum lis3lv02d_ctrl1_3dc {
  117. CTRL1_ODR0 = 0x10,
  118. CTRL1_ODR1 = 0x20,
  119. CTRL1_ODR2 = 0x40,
  120. CTRL1_ODR3 = 0x80,
  121. };
  122. enum lis3lv02d_ctrl2 {
  123. CTRL2_DAS = 0x01,
  124. CTRL2_SIM = 0x02,
  125. CTRL2_DRDY = 0x04,
  126. CTRL2_IEN = 0x08,
  127. CTRL2_BOOT = 0x10,
  128. CTRL2_BLE = 0x20,
  129. CTRL2_BDU = 0x40, /* Block Data Update */
  130. CTRL2_FS = 0x80, /* Full Scale selection */
  131. };
  132. enum lis3lv02d_ctrl4_3dc {
  133. CTRL4_SIM = 0x01,
  134. CTRL4_ST0 = 0x02,
  135. CTRL4_ST1 = 0x04,
  136. CTRL4_FS0 = 0x10,
  137. CTRL4_FS1 = 0x20,
  138. };
  139. enum lis302d_ctrl2 {
  140. HP_FF_WU2 = 0x08,
  141. HP_FF_WU1 = 0x04,
  142. CTRL2_BOOT_8B = 0x40,
  143. };
  144. enum lis3lv02d_ctrl3 {
  145. CTRL3_CFS0 = 0x01,
  146. CTRL3_CFS1 = 0x02,
  147. CTRL3_FDS = 0x10,
  148. CTRL3_HPFF = 0x20,
  149. CTRL3_HPDD = 0x40,
  150. CTRL3_ECK = 0x80,
  151. };
  152. enum lis3lv02d_status_reg {
  153. STATUS_XDA = 0x01,
  154. STATUS_YDA = 0x02,
  155. STATUS_ZDA = 0x04,
  156. STATUS_XYZDA = 0x08,
  157. STATUS_XOR = 0x10,
  158. STATUS_YOR = 0x20,
  159. STATUS_ZOR = 0x40,
  160. STATUS_XYZOR = 0x80,
  161. };
  162. enum lis3lv02d_ff_wu_cfg {
  163. FF_WU_CFG_XLIE = 0x01,
  164. FF_WU_CFG_XHIE = 0x02,
  165. FF_WU_CFG_YLIE = 0x04,
  166. FF_WU_CFG_YHIE = 0x08,
  167. FF_WU_CFG_ZLIE = 0x10,
  168. FF_WU_CFG_ZHIE = 0x20,
  169. FF_WU_CFG_LIR = 0x40,
  170. FF_WU_CFG_AOI = 0x80,
  171. };
  172. enum lis3lv02d_ff_wu_src {
  173. FF_WU_SRC_XL = 0x01,
  174. FF_WU_SRC_XH = 0x02,
  175. FF_WU_SRC_YL = 0x04,
  176. FF_WU_SRC_YH = 0x08,
  177. FF_WU_SRC_ZL = 0x10,
  178. FF_WU_SRC_ZH = 0x20,
  179. FF_WU_SRC_IA = 0x40,
  180. };
  181. enum lis3lv02d_dd_cfg {
  182. DD_CFG_XLIE = 0x01,
  183. DD_CFG_XHIE = 0x02,
  184. DD_CFG_YLIE = 0x04,
  185. DD_CFG_YHIE = 0x08,
  186. DD_CFG_ZLIE = 0x10,
  187. DD_CFG_ZHIE = 0x20,
  188. DD_CFG_LIR = 0x40,
  189. DD_CFG_IEND = 0x80,
  190. };
  191. enum lis3lv02d_dd_src {
  192. DD_SRC_XL = 0x01,
  193. DD_SRC_XH = 0x02,
  194. DD_SRC_YL = 0x04,
  195. DD_SRC_YH = 0x08,
  196. DD_SRC_ZL = 0x10,
  197. DD_SRC_ZH = 0x20,
  198. DD_SRC_IA = 0x40,
  199. };
  200. enum lis3lv02d_click_src_8b {
  201. CLICK_SINGLE_X = 0x01,
  202. CLICK_DOUBLE_X = 0x02,
  203. CLICK_SINGLE_Y = 0x04,
  204. CLICK_DOUBLE_Y = 0x08,
  205. CLICK_SINGLE_Z = 0x10,
  206. CLICK_DOUBLE_Z = 0x20,
  207. CLICK_IA = 0x40,
  208. };
  209. enum lis3lv02d_reg_state {
  210. LIS3_REG_OFF = 0x00,
  211. LIS3_REG_ON = 0x01,
  212. };
  213. union axis_conversion {
  214. struct {
  215. int x, y, z;
  216. };
  217. int as_array[3];
  218. };
  219. struct lis3lv02d {
  220. void *bus_priv; /* used by the bus layer only */
  221. struct device *pm_dev; /* for pm_runtime purposes */
  222. int (*init) (struct lis3lv02d *lis3);
  223. int (*write) (struct lis3lv02d *lis3, int reg, u8 val);
  224. int (*read) (struct lis3lv02d *lis3, int reg, u8 *ret);
  225. int (*blkread) (struct lis3lv02d *lis3, int reg, int len, u8 *ret);
  226. int (*reg_ctrl) (struct lis3lv02d *lis3, bool state);
  227. int *odrs; /* Supported output data rates */
  228. u8 *regs; /* Regs to store / restore */
  229. int regs_size;
  230. u8 *reg_cache;
  231. bool regs_stored;
  232. u8 odr_mask; /* ODR bit mask */
  233. u8 whoami; /* indicates measurement precision */
  234. s16 (*read_data) (struct lis3lv02d *lis3, int reg);
  235. int mdps_max_val;
  236. int pwron_delay;
  237. int scale; /*
  238. * relationship between 1 LBS and mG
  239. * (1/1000th of earth gravity)
  240. */
  241. struct input_polled_dev *idev; /* input device */
  242. struct platform_device *pdev; /* platform device */
  243. struct regulator_bulk_data regulators[2];
  244. atomic_t count; /* interrupt count after last read */
  245. union axis_conversion ac; /* hw -> logical axis */
  246. int mapped_btns[3];
  247. u32 irq; /* IRQ number */
  248. struct fasync_struct *async_queue; /* queue for the misc device */
  249. wait_queue_head_t misc_wait; /* Wait queue for the misc device */
  250. unsigned long misc_opened; /* bit0: whether the device is open */
  251. struct miscdevice miscdev;
  252. int data_ready_count[2];
  253. atomic_t wake_thread;
  254. unsigned char irq_cfg;
  255. struct lis3lv02d_platform_data *pdata; /* for passing board config */
  256. struct mutex mutex; /* Serialize poll and selftest */
  257. };
  258. int lis3lv02d_init_device(struct lis3lv02d *lis3);
  259. int lis3lv02d_joystick_enable(struct lis3lv02d *lis3);
  260. void lis3lv02d_joystick_disable(struct lis3lv02d *lis3);
  261. void lis3lv02d_poweroff(struct lis3lv02d *lis3);
  262. int lis3lv02d_poweron(struct lis3lv02d *lis3);
  263. int lis3lv02d_remove_fs(struct lis3lv02d *lis3);
  264. extern struct lis3lv02d lis3_dev;