inv_mpu_iio.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/mutex.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/regmap.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/kfifo_buf.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/platform_data/invensense_mpu6050.h>
  25. /**
  26. * struct inv_mpu6050_reg_map - Notable registers.
  27. * @sample_rate_div: Divider applied to gyro output rate.
  28. * @lpf: Configures internal low pass filter.
  29. * @accel_lpf: Configures accelerometer 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. * @int_status: Interrupt status register.
  41. * @pwr_mgmt_1: Controls chip's power state and clock source.
  42. * @pwr_mgmt_2: Controls power state of individual sensors.
  43. * @int_pin_cfg; Controls interrupt pin configuration.
  44. * @accl_offset: Controls the accelerometer calibration offset.
  45. * @gyro_offset: Controls the gyroscope calibration offset.
  46. */
  47. struct inv_mpu6050_reg_map {
  48. u8 sample_rate_div;
  49. u8 lpf;
  50. u8 accel_lpf;
  51. u8 user_ctrl;
  52. u8 fifo_en;
  53. u8 gyro_config;
  54. u8 accl_config;
  55. u8 fifo_count_h;
  56. u8 fifo_r_w;
  57. u8 raw_gyro;
  58. u8 raw_accl;
  59. u8 temperature;
  60. u8 int_enable;
  61. u8 int_status;
  62. u8 pwr_mgmt_1;
  63. u8 pwr_mgmt_2;
  64. u8 int_pin_cfg;
  65. u8 accl_offset;
  66. u8 gyro_offset;
  67. };
  68. /*device enum */
  69. enum inv_devices {
  70. INV_MPU6050,
  71. INV_MPU6500,
  72. INV_MPU6515,
  73. INV_MPU6000,
  74. INV_MPU9150,
  75. INV_MPU9250,
  76. INV_MPU9255,
  77. INV_ICM20608,
  78. INV_NUM_PARTS
  79. };
  80. /**
  81. * struct inv_mpu6050_chip_config - Cached chip configuration data.
  82. * @fsr: Full scale range.
  83. * @lpf: Digital low pass filter frequency.
  84. * @accl_fs: accel full scale range.
  85. * @accl_fifo_enable: enable accel data output
  86. * @gyro_fifo_enable: enable gyro data output
  87. * @divider: chip sample rate divider (sample rate divider - 1)
  88. */
  89. struct inv_mpu6050_chip_config {
  90. unsigned int fsr:2;
  91. unsigned int lpf:3;
  92. unsigned int accl_fs:2;
  93. unsigned int accl_fifo_enable:1;
  94. unsigned int gyro_fifo_enable:1;
  95. u8 divider;
  96. u8 user_ctrl;
  97. };
  98. /**
  99. * struct inv_mpu6050_hw - Other important hardware information.
  100. * @whoami: Self identification byte from WHO_AM_I register
  101. * @name: name of the chip.
  102. * @reg: register map of the chip.
  103. * @config: configuration of the chip.
  104. */
  105. struct inv_mpu6050_hw {
  106. u8 whoami;
  107. u8 *name;
  108. const struct inv_mpu6050_reg_map *reg;
  109. const struct inv_mpu6050_chip_config *config;
  110. };
  111. /*
  112. * struct inv_mpu6050_state - Driver state variables.
  113. * @lock: Chip access lock.
  114. * @trig: IIO trigger.
  115. * @chip_config: Cached attribute information.
  116. * @reg: Map of important registers.
  117. * @hw: Other hardware-specific information.
  118. * @chip_type: chip type.
  119. * @plat_data: platform data (deprecated in favor of @orientation).
  120. * @orientation: sensor chip orientation relative to main hardware.
  121. * @map regmap pointer.
  122. * @irq interrupt number.
  123. * @irq_mask the int_pin_cfg mask to configure interrupt type.
  124. * @chip_period: chip internal period estimation (~1kHz).
  125. * @it_timestamp: timestamp from previous interrupt.
  126. * @data_timestamp: timestamp for next data sample.
  127. */
  128. struct inv_mpu6050_state {
  129. struct mutex lock;
  130. struct iio_trigger *trig;
  131. struct inv_mpu6050_chip_config chip_config;
  132. const struct inv_mpu6050_reg_map *reg;
  133. const struct inv_mpu6050_hw *hw;
  134. enum inv_devices chip_type;
  135. struct i2c_mux_core *muxc;
  136. struct i2c_client *mux_client;
  137. unsigned int powerup_count;
  138. struct inv_mpu6050_platform_data plat_data;
  139. struct iio_mount_matrix orientation;
  140. struct regmap *map;
  141. int irq;
  142. u8 irq_mask;
  143. unsigned skip_samples;
  144. s64 chip_period;
  145. s64 it_timestamp;
  146. s64 data_timestamp;
  147. };
  148. /*register and associated bit definition*/
  149. #define INV_MPU6050_REG_ACCEL_OFFSET 0x06
  150. #define INV_MPU6050_REG_GYRO_OFFSET 0x13
  151. #define INV_MPU6050_REG_SAMPLE_RATE_DIV 0x19
  152. #define INV_MPU6050_REG_CONFIG 0x1A
  153. #define INV_MPU6050_REG_GYRO_CONFIG 0x1B
  154. #define INV_MPU6050_REG_ACCEL_CONFIG 0x1C
  155. #define INV_MPU6050_REG_FIFO_EN 0x23
  156. #define INV_MPU6050_BIT_ACCEL_OUT 0x08
  157. #define INV_MPU6050_BITS_GYRO_OUT 0x70
  158. #define INV_MPU6050_REG_INT_ENABLE 0x38
  159. #define INV_MPU6050_BIT_DATA_RDY_EN 0x01
  160. #define INV_MPU6050_BIT_DMP_INT_EN 0x02
  161. #define INV_MPU6050_REG_RAW_ACCEL 0x3B
  162. #define INV_MPU6050_REG_TEMPERATURE 0x41
  163. #define INV_MPU6050_REG_RAW_GYRO 0x43
  164. #define INV_MPU6050_REG_INT_STATUS 0x3A
  165. #define INV_MPU6050_BIT_FIFO_OVERFLOW_INT 0x10
  166. #define INV_MPU6050_BIT_RAW_DATA_RDY_INT 0x01
  167. #define INV_MPU6050_REG_USER_CTRL 0x6A
  168. #define INV_MPU6050_BIT_FIFO_RST 0x04
  169. #define INV_MPU6050_BIT_DMP_RST 0x08
  170. #define INV_MPU6050_BIT_I2C_MST_EN 0x20
  171. #define INV_MPU6050_BIT_FIFO_EN 0x40
  172. #define INV_MPU6050_BIT_DMP_EN 0x80
  173. #define INV_MPU6050_BIT_I2C_IF_DIS 0x10
  174. #define INV_MPU6050_REG_PWR_MGMT_1 0x6B
  175. #define INV_MPU6050_BIT_H_RESET 0x80
  176. #define INV_MPU6050_BIT_SLEEP 0x40
  177. #define INV_MPU6050_BIT_CLK_MASK 0x7
  178. #define INV_MPU6050_REG_PWR_MGMT_2 0x6C
  179. #define INV_MPU6050_BIT_PWR_ACCL_STBY 0x38
  180. #define INV_MPU6050_BIT_PWR_GYRO_STBY 0x07
  181. #define INV_MPU6050_REG_FIFO_COUNT_H 0x72
  182. #define INV_MPU6050_REG_FIFO_R_W 0x74
  183. #define INV_MPU6050_BYTES_PER_3AXIS_SENSOR 6
  184. #define INV_MPU6050_FIFO_COUNT_BYTE 2
  185. /* mpu6500 registers */
  186. #define INV_MPU6500_REG_ACCEL_CONFIG_2 0x1D
  187. #define INV_MPU6500_REG_ACCEL_OFFSET 0x77
  188. /* delay time in milliseconds */
  189. #define INV_MPU6050_POWER_UP_TIME 100
  190. #define INV_MPU6050_TEMP_UP_TIME 100
  191. #define INV_MPU6050_SENSOR_UP_TIME 30
  192. /* delay time in microseconds */
  193. #define INV_MPU6050_REG_UP_TIME_MIN 5000
  194. #define INV_MPU6050_REG_UP_TIME_MAX 10000
  195. #define INV_MPU6050_TEMP_OFFSET 12421
  196. #define INV_MPU6050_TEMP_SCALE 2941
  197. #define INV_MPU6050_MAX_GYRO_FS_PARAM 3
  198. #define INV_MPU6050_MAX_ACCL_FS_PARAM 3
  199. #define INV_MPU6050_THREE_AXIS 3
  200. #define INV_MPU6050_GYRO_CONFIG_FSR_SHIFT 3
  201. #define INV_MPU6050_ACCL_CONFIG_FSR_SHIFT 3
  202. /* 6 + 6 round up and plus 8 */
  203. #define INV_MPU6050_OUTPUT_DATA_SIZE 24
  204. #define INV_MPU6050_REG_INT_PIN_CFG 0x37
  205. #define INV_MPU6050_ACTIVE_HIGH 0x00
  206. #define INV_MPU6050_ACTIVE_LOW 0x80
  207. /* enable level triggering */
  208. #define INV_MPU6050_LATCH_INT_EN 0x20
  209. #define INV_MPU6050_BIT_BYPASS_EN 0x2
  210. /* Allowed timestamp period jitter in percent */
  211. #define INV_MPU6050_TS_PERIOD_JITTER 4
  212. /* init parameters */
  213. #define INV_MPU6050_INIT_FIFO_RATE 50
  214. #define INV_MPU6050_MAX_FIFO_RATE 1000
  215. #define INV_MPU6050_MIN_FIFO_RATE 4
  216. /* chip internal frequency: 1KHz */
  217. #define INV_MPU6050_INTERNAL_FREQ_HZ 1000
  218. /* return the frequency divider (chip sample rate divider + 1) */
  219. #define INV_MPU6050_FREQ_DIVIDER(st) \
  220. ((st)->chip_config.divider + 1)
  221. /* chip sample rate divider to fifo rate */
  222. #define INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate) \
  223. ((INV_MPU6050_INTERNAL_FREQ_HZ / (fifo_rate)) - 1)
  224. #define INV_MPU6050_DIVIDER_TO_FIFO_RATE(divider) \
  225. (INV_MPU6050_INTERNAL_FREQ_HZ / ((divider) + 1))
  226. #define INV_MPU6050_REG_WHOAMI 117
  227. #define INV_MPU6000_WHOAMI_VALUE 0x68
  228. #define INV_MPU6050_WHOAMI_VALUE 0x68
  229. #define INV_MPU6500_WHOAMI_VALUE 0x70
  230. #define INV_MPU9150_WHOAMI_VALUE 0x68
  231. #define INV_MPU9250_WHOAMI_VALUE 0x71
  232. #define INV_MPU9255_WHOAMI_VALUE 0x73
  233. #define INV_MPU6515_WHOAMI_VALUE 0x74
  234. #define INV_ICM20608_WHOAMI_VALUE 0xAF
  235. /* scan element definition */
  236. enum inv_mpu6050_scan {
  237. INV_MPU6050_SCAN_ACCL_X,
  238. INV_MPU6050_SCAN_ACCL_Y,
  239. INV_MPU6050_SCAN_ACCL_Z,
  240. INV_MPU6050_SCAN_GYRO_X,
  241. INV_MPU6050_SCAN_GYRO_Y,
  242. INV_MPU6050_SCAN_GYRO_Z,
  243. INV_MPU6050_SCAN_TIMESTAMP,
  244. };
  245. enum inv_mpu6050_filter_e {
  246. INV_MPU6050_FILTER_256HZ_NOLPF2 = 0,
  247. INV_MPU6050_FILTER_188HZ,
  248. INV_MPU6050_FILTER_98HZ,
  249. INV_MPU6050_FILTER_42HZ,
  250. INV_MPU6050_FILTER_20HZ,
  251. INV_MPU6050_FILTER_10HZ,
  252. INV_MPU6050_FILTER_5HZ,
  253. INV_MPU6050_FILTER_2100HZ_NOLPF,
  254. NUM_MPU6050_FILTER
  255. };
  256. /* IIO attribute address */
  257. enum INV_MPU6050_IIO_ATTR_ADDR {
  258. ATTR_GYRO_MATRIX,
  259. ATTR_ACCL_MATRIX,
  260. };
  261. enum inv_mpu6050_accl_fs_e {
  262. INV_MPU6050_FS_02G = 0,
  263. INV_MPU6050_FS_04G,
  264. INV_MPU6050_FS_08G,
  265. INV_MPU6050_FS_16G,
  266. NUM_ACCL_FSR
  267. };
  268. enum inv_mpu6050_fsr_e {
  269. INV_MPU6050_FSR_250DPS = 0,
  270. INV_MPU6050_FSR_500DPS,
  271. INV_MPU6050_FSR_1000DPS,
  272. INV_MPU6050_FSR_2000DPS,
  273. NUM_MPU6050_FSR
  274. };
  275. enum inv_mpu6050_clock_sel_e {
  276. INV_CLK_INTERNAL = 0,
  277. INV_CLK_PLL,
  278. NUM_CLK
  279. };
  280. irqreturn_t inv_mpu6050_read_fifo(int irq, void *p);
  281. int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type);
  282. int inv_reset_fifo(struct iio_dev *indio_dev);
  283. int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask);
  284. int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
  285. int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
  286. int inv_mpu_acpi_create_mux_client(struct i2c_client *client);
  287. void inv_mpu_acpi_delete_mux_client(struct i2c_client *client);
  288. int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
  289. int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type);
  290. extern const struct dev_pm_ops inv_mpu_pmops;