mpu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  4. $
  5. */
  6. #ifndef __MPU_H_
  7. #define __MPU_H_
  8. #ifdef __KERNEL__
  9. #include <linux/types.h>
  10. #include <linux/ioctl.h>
  11. #elif defined LINUX
  12. #include <sys/ioctl.h>
  13. #include <linux/types.h>
  14. #else
  15. #include "mltypes.h"
  16. #endif
  17. struct mpu_read_write {
  18. /* Memory address or register address depending on ioctl */
  19. __u16 address;
  20. __u16 length;
  21. __u8 *data;
  22. };
  23. enum mpuirq_data_type {
  24. MPUIRQ_DATA_TYPE_MPU_DATA_READY_IRQ,
  25. MPUIRQ_DATA_TYPE_MPU_FIFO_READY_IRQ,
  26. MPUIRQ_DATA_TYPE_SLAVE_IRQ,
  27. MPUIRQ_DATA_TYPE_PM_EVENT,
  28. MPUIRQ_DATA_TYPE_NUM_TYPES,
  29. };
  30. /* User space PM event notification */
  31. #define MPU_PM_EVENT_SUSPEND_PREPARE (3)
  32. #define MPU_PM_EVENT_POST_SUSPEND (4)
  33. /**
  34. * struct mpuirq_data - structure to report what and when
  35. * @interruptcount : The number of times this IRQ has occured since open
  36. * @irqtime : monotonic time of the IRQ in ns
  37. * @data_type : The type of this IRQ enum mpuirq_data_type
  38. * @data : Data associated with this IRQ
  39. */
  40. struct mpuirq_data {
  41. __u32 interruptcount;
  42. #ifdef EMPL_NO_64BIT
  43. __s32 irqtime_ns;
  44. #else
  45. __s64 irqtime_ns;
  46. #endif
  47. __u32 data_type;
  48. __s32 data;
  49. };
  50. enum ext_slave_config_key {
  51. /* TODO: Remove these first six. */
  52. MPU_SLAVE_CONFIG_ODR_SUSPEND,
  53. MPU_SLAVE_CONFIG_ODR_RESUME,
  54. MPU_SLAVE_CONFIG_FSR_SUSPEND,
  55. MPU_SLAVE_CONFIG_FSR_RESUME,
  56. MPU_SLAVE_CONFIG_IRQ_SUSPEND,
  57. MPU_SLAVE_CONFIG_IRQ_RESUME,
  58. MPU_SLAVE_CONFIG_ODR,
  59. MPU_SLAVE_CONFIG_FSR,
  60. MPU_SLAVE_CONFIG_MOT_THS,
  61. MPU_SLAVE_CONFIG_NMOT_THS,
  62. MPU_SLAVE_CONFIG_MOT_DUR,
  63. MPU_SLAVE_CONFIG_NMOT_DUR,
  64. MPU_SLAVE_CONFIG_IRQ,
  65. MPU_SLAVE_WRITE_REGISTERS,
  66. MPU_SLAVE_READ_REGISTERS,
  67. MPU_SLAVE_CONFIG_INTERNAL_REFERENCE,
  68. /* AMI 306 specific config keys */
  69. MPU_SLAVE_PARAM,
  70. MPU_SLAVE_WINDOW,
  71. MPU_SLAVE_READWINPARAMS,
  72. MPU_SLAVE_SEARCHOFFSET,
  73. /* MPU3050 and MPU6050 Keys */
  74. MPU_SLAVE_INT_CONFIG,
  75. MPU_SLAVE_EXT_SYNC,
  76. MPU_SLAVE_FULL_SCALE,
  77. MPU_SLAVE_LPF,
  78. MPU_SLAVE_CLK_SRC,
  79. MPU_SLAVE_DIVIDER,
  80. MPU_SLAVE_DMP_ENABLE,
  81. MPU_SLAVE_FIFO_ENABLE,
  82. MPU_SLAVE_DMP_CFG1,
  83. MPU_SLAVE_DMP_CFG2,
  84. MPU_SLAVE_TC,
  85. MPU_SLAVE_GYRO,
  86. MPU_SLAVE_ADDR,
  87. MPU_SLAVE_PRODUCT_REVISION,
  88. MPU_SLAVE_SILICON_REVISION,
  89. MPU_SLAVE_PRODUCT_ID,
  90. MPU_SLAVE_GYRO_SENS_TRIM,
  91. MPU_SLAVE_ACCEL_SENS_TRIM,
  92. MPU_SLAVE_RAM,
  93. /* -------------------------- */
  94. MPU_SLAVE_CONFIG_NUM_CONFIG_KEYS
  95. };
  96. /* For the MPU_SLAVE_CONFIG_IRQ_SUSPEND and MPU_SLAVE_CONFIG_IRQ_RESUME */
  97. enum ext_slave_config_irq_type {
  98. MPU_SLAVE_IRQ_TYPE_NONE,
  99. MPU_SLAVE_IRQ_TYPE_MOTION,
  100. MPU_SLAVE_IRQ_TYPE_DATA_READY,
  101. };
  102. /* Structure for the following IOCTS's
  103. * MPU_CONFIG_GYRO
  104. * MPU_CONFIG_ACCEL
  105. * MPU_CONFIG_COMPASS
  106. * MPU_CONFIG_PRESSURE
  107. * MPU_GET_CONFIG_GYRO
  108. * MPU_GET_CONFIG_ACCEL
  109. * MPU_GET_CONFIG_COMPASS
  110. * MPU_GET_CONFIG_PRESSURE
  111. *
  112. * @key one of enum ext_slave_config_key
  113. * @len length of data pointed to by data
  114. * @apply zero if communication with the chip is not necessary, false otherwise
  115. * This flag can be used to select cached data or to refresh cashed data
  116. * cache data to be pushed later or push immediately. If true and the
  117. * slave is on the secondary bus the MPU will first enger bypass mode
  118. * before calling the slaves .config or .get_config funcion
  119. * @data pointer to the data to confgure or get
  120. */
  121. struct ext_slave_config {
  122. __u8 key;
  123. __u16 len;
  124. __u8 apply;
  125. void *data;
  126. };
  127. enum ext_slave_type {
  128. EXT_SLAVE_TYPE_GYROSCOPE,
  129. EXT_SLAVE_TYPE_ACCEL,
  130. EXT_SLAVE_TYPE_COMPASS,
  131. EXT_SLAVE_TYPE_PRESSURE,
  132. /*EXT_SLAVE_TYPE_TEMPERATURE */
  133. EXT_SLAVE_NUM_TYPES
  134. };
  135. enum ext_slave_id {
  136. ID_INVALID = 0,
  137. GYRO_ID_MPU3050,
  138. GYRO_ID_MPU6050A2,
  139. GYRO_ID_MPU6050B1,
  140. GYRO_ID_MPU6050B1_NO_ACCEL,
  141. GYRO_ID_ITG3500,
  142. ACCEL_ID_LIS331,
  143. ACCEL_ID_LSM303DLX,
  144. ACCEL_ID_LIS3DH,
  145. ACCEL_ID_KXSD9,
  146. ACCEL_ID_KXTF9,
  147. ACCEL_ID_BMA150,
  148. ACCEL_ID_BMA222,
  149. ACCEL_ID_BMA250,
  150. ACCEL_ID_ADXL34X,
  151. ACCEL_ID_MMA8450,
  152. ACCEL_ID_MMA845X,
  153. ACCEL_ID_MPU6050,
  154. COMPASS_ID_AK8975,
  155. COMPASS_ID_AK8972,
  156. COMPASS_ID_AMI30X,
  157. COMPASS_ID_AMI306,
  158. COMPASS_ID_YAS529,
  159. COMPASS_ID_YAS530,
  160. COMPASS_ID_HMC5883,
  161. COMPASS_ID_LSM303DLH,
  162. COMPASS_ID_LSM303DLM,
  163. COMPASS_ID_MMC314X,
  164. COMPASS_ID_HSCDTD002B,
  165. COMPASS_ID_HSCDTD004A,
  166. PRESSURE_ID_BMA085,
  167. };
  168. #define INV_PROD_KEY(ver, rev) (ver * 100 + rev)
  169. enum ext_slave_endian {
  170. EXT_SLAVE_BIG_ENDIAN,
  171. EXT_SLAVE_LITTLE_ENDIAN,
  172. EXT_SLAVE_FS8_BIG_ENDIAN,
  173. EXT_SLAVE_FS16_BIG_ENDIAN,
  174. };
  175. enum ext_slave_bus {
  176. EXT_SLAVE_BUS_INVALID = -1,
  177. EXT_SLAVE_BUS_PRIMARY = 0,
  178. EXT_SLAVE_BUS_SECONDARY = 1
  179. };
  180. /**
  181. * struct ext_slave_platform_data - Platform data for mpu3050 and mpu6050
  182. * slave devices
  183. *
  184. * @type: the type of slave device based on the enum ext_slave_type
  185. * definitions.
  186. * @irq: the irq number attached to the slave if any.
  187. * @adapt_num: the I2C adapter number.
  188. * @bus: the bus the slave is attached to: enum ext_slave_bus
  189. * @address: the I2C slave address of the slave device.
  190. * @orientation: the mounting matrix of the device relative to MPU.
  191. * @irq_data: private data for the slave irq handler
  192. * @private_data: additional data, user customizable. Not touched by the MPU
  193. * driver.
  194. *
  195. * The orientation matricies are 3x3 rotation matricies
  196. * that are applied to the data to rotate from the mounting orientation to the
  197. * platform orientation. The values must be one of 0, 1, or -1 and each row and
  198. * column should have exactly 1 non-zero value.
  199. */
  200. struct ext_slave_platform_data {
  201. __u8 type;
  202. __u32 irq;
  203. __u32 adapt_num;
  204. __s32 bus;
  205. __u8 address;
  206. __s8 orientation[9];
  207. void *irq_data;
  208. void *private_data;
  209. };
  210. struct fix_pnt_range {
  211. __s32 mantissa;
  212. __s32 fraction;
  213. };
  214. static inline long range_fixedpoint_to_long_mg(struct fix_pnt_range rng)
  215. {
  216. return (long)(rng.mantissa * 1000 + rng.fraction / 10);
  217. }
  218. struct ext_slave_read_trigger {
  219. __u8 reg;
  220. __u8 value;
  221. };
  222. /**
  223. * struct ext_slave_descr - Description of the slave device for programming.
  224. *
  225. * @suspend: function pointer to put the device in suspended state
  226. * @resume: function pointer to put the device in running state
  227. * @read: function that reads the device data
  228. * @init: function used to preallocate memory used by the driver
  229. * @exit: function used to free memory allocated for the driver
  230. * @config: function used to configure the device
  231. * @get_config:function used to get the device's configuration
  232. *
  233. * @name: text name of the device
  234. * @type: device type. enum ext_slave_type
  235. * @id: enum ext_slave_id
  236. * @read_reg: starting register address to retrieve data.
  237. * @read_len: length in bytes of the sensor data. Typically 6.
  238. * @endian: byte order of the data. enum ext_slave_endian
  239. * @range: full scale range of the slave ouput: struct fix_pnt_range
  240. * @trigger: If reading data first requires writing a register this is the
  241. * data to write.
  242. *
  243. * Defines the functions and information about the slave the mpu3050 and
  244. * mpu6050 needs to use the slave device.
  245. */
  246. struct ext_slave_descr {
  247. int (*init) (void *mlsl_handle,
  248. struct ext_slave_descr *slave,
  249. struct ext_slave_platform_data *pdata);
  250. int (*exit) (void *mlsl_handle,
  251. struct ext_slave_descr *slave,
  252. struct ext_slave_platform_data *pdata);
  253. int (*suspend) (void *mlsl_handle,
  254. struct ext_slave_descr *slave,
  255. struct ext_slave_platform_data *pdata);
  256. int (*resume) (void *mlsl_handle,
  257. struct ext_slave_descr *slave,
  258. struct ext_slave_platform_data *pdata);
  259. int (*read) (void *mlsl_handle,
  260. struct ext_slave_descr *slave,
  261. struct ext_slave_platform_data *pdata,
  262. __u8 *data);
  263. int (*config) (void *mlsl_handle,
  264. struct ext_slave_descr *slave,
  265. struct ext_slave_platform_data *pdata,
  266. struct ext_slave_config *config);
  267. int (*get_config) (void *mlsl_handle,
  268. struct ext_slave_descr *slave,
  269. struct ext_slave_platform_data *pdata,
  270. struct ext_slave_config *config);
  271. char *name;
  272. __u8 type;
  273. __u8 id;
  274. __u8 read_reg;
  275. __u8 read_len;
  276. __u8 endian;
  277. struct fix_pnt_range range;
  278. struct ext_slave_read_trigger *trigger;
  279. };
  280. /**
  281. * struct mpu_platform_data - Platform data for the mpu driver
  282. * @int_config: Bits [7:3] of the int config register.
  283. * @level_shifter: 0: VLogic, 1: VDD
  284. * @orientation: Orientation matrix of the gyroscope
  285. *
  286. * Contains platform specific information on how to configure the MPU3050 to
  287. * work on this platform. The orientation matricies are 3x3 rotation matricies
  288. * that are applied to the data to rotate from the mounting orientation to the
  289. * platform orientation. The values must be one of 0, 1, or -1 and each row and
  290. * column should have exactly 1 non-zero value.
  291. */
  292. struct mpu_platform_data {
  293. __u8 int_config;
  294. __u8 level_shifter;
  295. __s8 orientation[9];
  296. };
  297. #if defined __KERNEL__ || defined LINUX
  298. #define MPU_IOCTL (0x81) /* Magic number for MPU Iocts */
  299. /* IOCTL commands for /dev/mpu */
  300. /*--------------------------------------------------------------------------
  301. * Deprecated, debugging only
  302. */
  303. #define MPU_SET_MPU_PLATFORM_DATA \
  304. _IOWR(MPU_IOCTL, 0x01, struct mpu_platform_data)
  305. #define MPU_SET_EXT_SLAVE_PLATFORM_DATA \
  306. _IOWR(MPU_IOCTL, 0x01, struct ext_slave_platform_data)
  307. /*--------------------------------------------------------------------------*/
  308. #define MPU_GET_EXT_SLAVE_PLATFORM_DATA \
  309. _IOWR(MPU_IOCTL, 0x02, struct ext_slave_platform_data)
  310. #define MPU_GET_MPU_PLATFORM_DATA \
  311. _IOWR(MPU_IOCTL, 0x02, struct mpu_platform_data)
  312. #define MPU_GET_EXT_SLAVE_DESCR \
  313. _IOWR(MPU_IOCTL, 0x02, struct ext_slave_descr)
  314. #define MPU_READ _IOWR(MPU_IOCTL, 0x10, struct mpu_read_write)
  315. #define MPU_WRITE _IOW(MPU_IOCTL, 0x10, struct mpu_read_write)
  316. #define MPU_READ_MEM _IOWR(MPU_IOCTL, 0x11, struct mpu_read_write)
  317. #define MPU_WRITE_MEM _IOW(MPU_IOCTL, 0x11, struct mpu_read_write)
  318. #define MPU_READ_FIFO _IOWR(MPU_IOCTL, 0x12, struct mpu_read_write)
  319. #define MPU_WRITE_FIFO _IOW(MPU_IOCTL, 0x12, struct mpu_read_write)
  320. #define MPU_READ_COMPASS _IOR(MPU_IOCTL, 0x12, __u8)
  321. #define MPU_READ_ACCEL _IOR(MPU_IOCTL, 0x13, __u8)
  322. #define MPU_READ_PRESSURE _IOR(MPU_IOCTL, 0x14, __u8)
  323. #define MPU_CONFIG_GYRO _IOW(MPU_IOCTL, 0x20, struct ext_slave_config)
  324. #define MPU_CONFIG_ACCEL _IOW(MPU_IOCTL, 0x21, struct ext_slave_config)
  325. #define MPU_CONFIG_COMPASS _IOW(MPU_IOCTL, 0x22, struct ext_slave_config)
  326. #define MPU_CONFIG_PRESSURE _IOW(MPU_IOCTL, 0x23, struct ext_slave_config)
  327. #define MPU_GET_CONFIG_GYRO _IOWR(MPU_IOCTL, 0x20, struct ext_slave_config)
  328. #define MPU_GET_CONFIG_ACCEL _IOWR(MPU_IOCTL, 0x21, struct ext_slave_config)
  329. #define MPU_GET_CONFIG_COMPASS _IOWR(MPU_IOCTL, 0x22, struct ext_slave_config)
  330. #define MPU_GET_CONFIG_PRESSURE _IOWR(MPU_IOCTL, 0x23, struct ext_slave_config)
  331. #define MPU_SUSPEND _IOW(MPU_IOCTL, 0x30, __u32)
  332. #define MPU_RESUME _IOW(MPU_IOCTL, 0x31, __u32)
  333. /* Userspace PM Event response */
  334. #define MPU_PM_EVENT_HANDLED _IO(MPU_IOCTL, 0x32)
  335. #define MPU_GET_REQUESTED_SENSORS _IOR(MPU_IOCTL, 0x40, __u32)
  336. #define MPU_SET_REQUESTED_SENSORS _IOW(MPU_IOCTL, 0x40, __u32)
  337. #define MPU_GET_IRQ_TO_FIFO_DIVIDER _IOR(MPU_IOCTL, 0x41, __u16)
  338. #define MPU_SET_IRQ_TO_FIFO_DIVIDER _IOW(MPU_IOCTL, 0x41, __u16)
  339. #define MPU_GET_IGNORE_SYSTEM_SUSPEND _IOR(MPU_IOCTL, 0x42, __u8)
  340. #define MPU_SET_IGNORE_SYSTEM_SUSPEND _IOW(MPU_IOCTL, 0x42, __u8)
  341. #define MPU_GET_MLDL_STATUS _IOR(MPU_IOCTL, 0x43, __u8)
  342. #define MPU_GET_I2C_SLAVES_ENABLED _IOR(MPU_IOCTL, 0x44, __u8)
  343. #endif
  344. #endif /* __MPU_H_ */