inv_mpu_iio.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2012 Invensense, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-mux.h>
  15. #include <linux/kfifo.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/regmap.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/kfifo_buf.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/platform_data/invensense_mpu6050.h>
  26. /**
  27. * struct inv_mpu6050_reg_map - Notable registers.
  28. * @sample_rate_div: Divider applied to gyro output rate.
  29. * @lpf: Configures internal low pass filter.
  30. * @user_ctrl: Enables/resets the FIFO.
  31. * @fifo_en: Determines which data will appear in FIFO.
  32. * @gyro_config: gyro config register.
  33. * @accl_config: accel config register
  34. * @fifo_count_h: Upper byte of FIFO count.
  35. * @fifo_r_w: FIFO register.
  36. * @raw_gyro: Address of first gyro register.
  37. * @raw_accl: Address of first accel register.
  38. * @temperature: temperature register
  39. * @int_enable: Interrupt enable register.
  40. * @pwr_mgmt_1: Controls chip's power state and clock source.
  41. * @pwr_mgmt_2: Controls power state of individual sensors.
  42. * @int_pin_cfg; Controls interrupt pin configuration.
  43. * @accl_offset: Controls the accelerometer calibration offset.
  44. * @gyro_offset: Controls the gyroscope calibration offset.
  45. */
  46. struct inv_mpu6050_reg_map {
  47. u8 sample_rate_div;
  48. u8 lpf;
  49. u8 user_ctrl;
  50. u8 fifo_en;
  51. u8 gyro_config;
  52. u8 accl_config;
  53. u8 fifo_count_h;
  54. u8 fifo_r_w;
  55. u8 raw_gyro;
  56. u8 raw_accl;
  57. u8 temperature;
  58. u8 int_enable;
  59. u8 pwr_mgmt_1;
  60. u8 pwr_mgmt_2;
  61. u8 int_pin_cfg;
  62. u8 accl_offset;
  63. u8 gyro_offset;
  64. };
  65. /*device enum */
  66. enum inv_devices {
  67. INV_MPU6050,
  68. INV_MPU6500,
  69. INV_MPU6000,
  70. INV_MPU9150,
  71. INV_ICM20608,
  72. INV_NUM_PARTS
  73. };
  74. /**
  75. * struct inv_mpu6050_chip_config - Cached chip configuration data.
  76. * @fsr: Full scale range.
  77. * @lpf: Digital low pass filter frequency.
  78. * @accl_fs: accel full scale range.
  79. * @enable: master enable state.
  80. * @accl_fifo_enable: enable accel data output
  81. * @gyro_fifo_enable: enable gyro data output
  82. * @fifo_rate: FIFO update rate.
  83. */
  84. struct inv_mpu6050_chip_config {
  85. unsigned int fsr:2;
  86. unsigned int lpf:3;
  87. unsigned int accl_fs:2;
  88. unsigned int enable:1;
  89. unsigned int accl_fifo_enable:1;
  90. unsigned int gyro_fifo_enable:1;
  91. u16 fifo_rate;
  92. };
  93. /**
  94. * struct inv_mpu6050_hw - Other important hardware information.
  95. * @whoami: Self identification byte from WHO_AM_I register
  96. * @name: name of the chip.
  97. * @reg: register map of the chip.
  98. * @config: configuration of the chip.
  99. */
  100. struct inv_mpu6050_hw {
  101. u8 whoami;
  102. u8 *name;
  103. const struct inv_mpu6050_reg_map *reg;
  104. const struct inv_mpu6050_chip_config *config;
  105. };
  106. /*
  107. * struct inv_mpu6050_state - Driver state variables.
  108. * @TIMESTAMP_FIFO_SIZE: fifo size for timestamp.
  109. * @trig: IIO trigger.
  110. * @chip_config: Cached attribute information.
  111. * @reg: Map of important registers.
  112. * @hw: Other hardware-specific information.
  113. * @chip_type: chip type.
  114. * @time_stamp_lock: spin lock to time stamp.
  115. * @plat_data: platform data (deprecated in favor of @orientation).
  116. * @orientation: sensor chip orientation relative to main hardware.
  117. * @timestamps: kfifo queue to store time stamp.
  118. * @map regmap pointer.
  119. * @irq interrupt number.
  120. */
  121. struct inv_mpu6050_state {
  122. #define TIMESTAMP_FIFO_SIZE 16
  123. struct iio_trigger *trig;
  124. struct inv_mpu6050_chip_config chip_config;
  125. const struct inv_mpu6050_reg_map *reg;
  126. const struct inv_mpu6050_hw *hw;
  127. enum inv_devices chip_type;
  128. spinlock_t time_stamp_lock;
  129. struct i2c_mux_core *muxc;
  130. struct i2c_client *mux_client;
  131. unsigned int powerup_count;
  132. struct inv_mpu6050_platform_data plat_data;
  133. struct iio_mount_matrix orientation;
  134. DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
  135. struct regmap *map;
  136. int irq;
  137. };
  138. /*register and associated bit definition*/
  139. #define INV_MPU6050_REG_ACCEL_OFFSET 0x06
  140. #define INV_MPU6050_REG_GYRO_OFFSET 0x13
  141. #define INV_MPU6050_REG_SAMPLE_RATE_DIV 0x19
  142. #define INV_MPU6050_REG_CONFIG 0x1A
  143. #define INV_MPU6050_REG_GYRO_CONFIG 0x1B
  144. #define INV_MPU6050_REG_ACCEL_CONFIG 0x1C
  145. #define INV_MPU6050_REG_FIFO_EN 0x23
  146. #define INV_MPU6050_BIT_ACCEL_OUT 0x08
  147. #define INV_MPU6050_BITS_GYRO_OUT 0x70
  148. #define INV_MPU6050_REG_INT_ENABLE 0x38
  149. #define INV_MPU6050_BIT_DATA_RDY_EN 0x01
  150. #define INV_MPU6050_BIT_DMP_INT_EN 0x02
  151. #define INV_MPU6050_REG_RAW_ACCEL 0x3B
  152. #define INV_MPU6050_REG_TEMPERATURE 0x41
  153. #define INV_MPU6050_REG_RAW_GYRO 0x43
  154. #define INV_MPU6050_REG_USER_CTRL 0x6A
  155. #define INV_MPU6050_BIT_FIFO_RST 0x04
  156. #define INV_MPU6050_BIT_DMP_RST 0x08
  157. #define INV_MPU6050_BIT_I2C_MST_EN 0x20
  158. #define INV_MPU6050_BIT_FIFO_EN 0x40
  159. #define INV_MPU6050_BIT_DMP_EN 0x80
  160. #define INV_MPU6050_BIT_I2C_IF_DIS 0x10
  161. #define INV_MPU6050_REG_PWR_MGMT_1 0x6B
  162. #define INV_MPU6050_BIT_H_RESET 0x80
  163. #define INV_MPU6050_BIT_SLEEP 0x40
  164. #define INV_MPU6050_BIT_CLK_MASK 0x7
  165. #define INV_MPU6050_REG_PWR_MGMT_2 0x6C
  166. #define INV_MPU6050_BIT_PWR_ACCL_STBY 0x38
  167. #define INV_MPU6050_BIT_PWR_GYRO_STBY 0x07
  168. #define INV_MPU6050_REG_FIFO_COUNT_H 0x72
  169. #define INV_MPU6050_REG_FIFO_R_W 0x74
  170. #define INV_MPU6050_BYTES_PER_3AXIS_SENSOR 6
  171. #define INV_MPU6050_FIFO_COUNT_BYTE 2
  172. #define INV_MPU6050_FIFO_THRESHOLD 500
  173. /* mpu6500 registers */
  174. #define INV_MPU6500_REG_ACCEL_OFFSET 0x77
  175. /* delay time in milliseconds */
  176. #define INV_MPU6050_POWER_UP_TIME 100
  177. #define INV_MPU6050_TEMP_UP_TIME 100
  178. #define INV_MPU6050_SENSOR_UP_TIME 30
  179. /* delay time in microseconds */
  180. #define INV_MPU6050_REG_UP_TIME_MIN 5000
  181. #define INV_MPU6050_REG_UP_TIME_MAX 10000
  182. #define INV_MPU6050_TEMP_OFFSET 12421
  183. #define INV_MPU6050_TEMP_SCALE 2941
  184. #define INV_MPU6050_MAX_GYRO_FS_PARAM 3
  185. #define INV_MPU6050_MAX_ACCL_FS_PARAM 3
  186. #define INV_MPU6050_THREE_AXIS 3
  187. #define INV_MPU6050_GYRO_CONFIG_FSR_SHIFT 3
  188. #define INV_MPU6050_ACCL_CONFIG_FSR_SHIFT 3
  189. /* 6 + 6 round up and plus 8 */
  190. #define INV_MPU6050_OUTPUT_DATA_SIZE 24
  191. #define INV_MPU6050_REG_INT_PIN_CFG 0x37
  192. #define INV_MPU6050_BIT_BYPASS_EN 0x2
  193. #define INV_MPU6050_INT_PIN_CFG 0
  194. /* init parameters */
  195. #define INV_MPU6050_INIT_FIFO_RATE 50
  196. #define INV_MPU6050_TIME_STAMP_TOR 5
  197. #define INV_MPU6050_MAX_FIFO_RATE 1000
  198. #define INV_MPU6050_MIN_FIFO_RATE 4
  199. #define INV_MPU6050_ONE_K_HZ 1000
  200. #define INV_MPU6050_REG_WHOAMI 117
  201. #define INV_MPU6000_WHOAMI_VALUE 0x68
  202. #define INV_MPU6050_WHOAMI_VALUE 0x68
  203. #define INV_MPU6500_WHOAMI_VALUE 0x70
  204. #define INV_MPU9150_WHOAMI_VALUE 0x68
  205. #define INV_ICM20608_WHOAMI_VALUE 0xAF
  206. /* scan element definition */
  207. enum inv_mpu6050_scan {
  208. INV_MPU6050_SCAN_ACCL_X,
  209. INV_MPU6050_SCAN_ACCL_Y,
  210. INV_MPU6050_SCAN_ACCL_Z,
  211. INV_MPU6050_SCAN_GYRO_X,
  212. INV_MPU6050_SCAN_GYRO_Y,
  213. INV_MPU6050_SCAN_GYRO_Z,
  214. INV_MPU6050_SCAN_TIMESTAMP,
  215. };
  216. enum inv_mpu6050_filter_e {
  217. INV_MPU6050_FILTER_256HZ_NOLPF2 = 0,
  218. INV_MPU6050_FILTER_188HZ,
  219. INV_MPU6050_FILTER_98HZ,
  220. INV_MPU6050_FILTER_42HZ,
  221. INV_MPU6050_FILTER_20HZ,
  222. INV_MPU6050_FILTER_10HZ,
  223. INV_MPU6050_FILTER_5HZ,
  224. INV_MPU6050_FILTER_2100HZ_NOLPF,
  225. NUM_MPU6050_FILTER
  226. };
  227. /* IIO attribute address */
  228. enum INV_MPU6050_IIO_ATTR_ADDR {
  229. ATTR_GYRO_MATRIX,
  230. ATTR_ACCL_MATRIX,
  231. };
  232. enum inv_mpu6050_accl_fs_e {
  233. INV_MPU6050_FS_02G = 0,
  234. INV_MPU6050_FS_04G,
  235. INV_MPU6050_FS_08G,
  236. INV_MPU6050_FS_16G,
  237. NUM_ACCL_FSR
  238. };
  239. enum inv_mpu6050_fsr_e {
  240. INV_MPU6050_FSR_250DPS = 0,
  241. INV_MPU6050_FSR_500DPS,
  242. INV_MPU6050_FSR_1000DPS,
  243. INV_MPU6050_FSR_2000DPS,
  244. NUM_MPU6050_FSR
  245. };
  246. enum inv_mpu6050_clock_sel_e {
  247. INV_CLK_INTERNAL = 0,
  248. INV_CLK_PLL,
  249. NUM_CLK
  250. };
  251. irqreturn_t inv_mpu6050_irq_handler(int irq, void *p);
  252. irqreturn_t inv_mpu6050_read_fifo(int irq, void *p);
  253. int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev);
  254. void inv_mpu6050_remove_trigger(struct inv_mpu6050_state *st);
  255. int inv_reset_fifo(struct iio_dev *indio_dev);
  256. int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask);
  257. int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
  258. int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
  259. int inv_mpu_acpi_create_mux_client(struct i2c_client *client);
  260. void inv_mpu_acpi_delete_mux_client(struct i2c_client *client);
  261. int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
  262. int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type);
  263. int inv_mpu_core_remove(struct device *dev);
  264. int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
  265. extern const struct dev_pm_ops inv_mpu_pmops;