1 /* Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
3 * This software is licensed under the terms of the GNU General Public
4 * License version 2, as published by the Free Software Foundation, and
5 * may be copied, distributed, and modified under those terms.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 /* NVS = NVidia Sensor framework */
14 /* See nvs_iio.c and nvs.h for documentation */
19 #include <asm/atomic.h>
20 #include <linux/i2c.h>
21 #include <linux/miscdevice.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/mpu_iio.h>
24 #include <linux/nvs.h>
26 #define NVI_BYPASS_TIMEOUT_MS (1000)
27 #define POWER_UP_TIME (100)
28 #define REG_UP_TIME (5)
30 #define GYRO_STARTUP_DELAY_NS (100000000) /* 100ms */
31 #define NVI_IRQ_STORM_MIN_NS (1000000) /* storm if irq faster 1ms */
32 #define NVI_IRQ_STORM_MAX_N (100) /* max storm irqs b4 dis irq */
33 #define NVI_FIFO_SAMPLE_SIZE_MAX (38)
48 #define DEV_AXIS_N (2)
60 #define FW_LOADED (16)
63 #define MSK_RST ((1 << EN_STDBY) | \
65 #define MSK_DEV_MPU ((1 << DEV_ACC) | \
68 #define MSK_DEV_MPU_AUX (MSK_DEV_MPU | (1 << DEV_AUX))
69 #define MSK_DEV_DMP ((1 << DEV_SM) | \
74 #define MSK_DEV_SNSR (MSK_DEV_MPU | MSK_DEV_DMP)
75 #define MSK_DEV_ALL (MSK_DEV_SNSR | (1 << DEV_AUX))
76 #define MSK_PM_ON_FULL ((1 << DEV_GYR) | (1 << DEV_GYU))
77 #define MSK_PM_ON ((1 << DEV_TMP) | \
80 #define MSK_PM_LP ((1 << EN_LP) | (1 << DEV_ACC))
81 #define MSK_PM_STDBY ((1 << EN_STDBY) | (1 << FW_LOADED))
82 #define MSK_PM_ACC_EN ((1 << DEV_ACC) | (1 << DEV_SM))
83 #define NVI_PM_ERR (0)
84 #define NVI_PM_AUTO (1)
85 #define NVI_PM_OFF_FORCE (2)
86 #define NVI_PM_OFF (3)
87 #define NVI_PM_STDBY (4)
88 #define NVI_PM_ON_CYCLE (5)
90 #define NVI_PM_ON_FULL (7)
91 #define INV_CLK_INTERNAL (0)
92 #define INV_CLK_PLL (1)
94 #define NVI_DBG_SPEW_MSG (1 << NVS_STS_EXT_N)
95 #define NVI_DBG_SPEW_AUX (1 << (NVS_STS_EXT_N + 1))
96 #define NVI_DBG_SPEW_FIFO (1 << (NVS_STS_EXT_N + 2))
97 #define NVI_DBG_SPEW_TS (1 << (NVS_STS_EXT_N + 3))
98 #define NVI_DBG_SPEW_SNSR (1 << (NVS_STS_EXT_N + 4))
100 #define NVI_RC_BANK_REG_BANK (0x7F / 64)
101 #define NVI_RC_MSK_REG_BANK ((u64)(1ULL << (0x7F % 64)))
104 #define BITS_SELF_TEST_EN (0xE0)
105 #define BIT_ACCEL_FCHOCIE_B (0x08)
106 #define BIT_FIFO_SIZE_1K (0x40)
107 #define BITS_GYRO_OUT (0x70)
108 #define BIT_I2C_MST_P_NSR (0x10)
109 #define BIT_I2C_READ (0x80)
110 #define BITS_I2C_SLV_CTRL_LEN (0x0F)
111 #define BIT_I2C_SLV_REG_DIS (0x10)
112 #define BIT_SLV_EN (0x80)
113 #define BITS_I2C_MST_DLY (0x1F)
114 #define BIT_BYPASS_EN (0x02)
115 #define BIT_DATA_RDY_EN (0x01)
116 #define BIT_DMP_INT_EN (0x02)
117 #define BIT_FIFO_OVERFLOW (0x10)
118 #define BIT_ZMOT_EN (0x20)
119 #define BIT_MOT_EN (0x40)
120 #define BIT_6500_WOM_EN (0x40)
121 #define BIT_DELAY_ES_SHADOW (0x80)
122 #define BIT_ACCEL_INTEL_MODE (0x40)
123 #define BIT_ACCEL_INTEL_ENABLE (0x80)
124 #define BITS_USER_CTRL_RST (0x0F)
125 #define BIT_SIG_COND_RST (0x01)
126 #define BIT_I2C_MST_RST (0x02)
127 #define BIT_FIFO_RST (0x04)
128 #define BIT_DMP_RST (0x08)
129 #define BIT_I2C_MST_EN (0x20)
130 #define BIT_FIFO_EN (0x40)
131 #define BIT_DMP_EN (0x80)
132 #define BIT_CLK_MASK (0x07)
133 #define BIT_CYCLE (0x20)
134 #define BIT_SLEEP (0x40)
135 #define BIT_H_RESET (0x80)
136 #define BIT_PWR_GYRO_STBY (0x07)
137 #define BIT_PWR_ACCEL_STBY (0x38)
138 #define BIT_PWR_PRESSURE_STBY (0x40)
139 #define BIT_LPA_FREQ (0xC0)
141 #define AUX_PORT_MAX (5)
142 #define AUX_PORT_IO (4)
143 #define AUX_EXT_DATA_REG_MAX (24)
144 #define AUX_DEV_VALID_READ_LOOP_MAX (20)
145 #define AUX_DEV_VALID_READ_DELAY_MS (5)
150 struct nvs_float max_range;
151 struct nvs_float resolution;
159 struct nvs_float scale;
160 struct nvs_float offset;
161 struct nvs_float milliamp;
164 unsigned int fifo_data_n;
168 unsigned int dev_msk;
169 unsigned int period_us_min;
170 unsigned int period_us_max;
171 int (*fn_period)(struct nvi_state *st);
179 unsigned int period_us_src;
180 unsigned int period_us_req;
181 unsigned int period_us_min;
182 unsigned int period_us_max;
183 unsigned int fifo_data_n;
195 struct nvi_br self_test_g[AXIS_N];
196 struct nvi_br self_test_a[AXIS_N];
197 struct nvi_br g_offset_h[AXIS_N];
198 struct nvi_br a_offset_h[AXIS_N];
199 struct nvi_br tbc_pll;
200 struct nvi_br tbc_rcosc;
201 struct nvi_br smplrt[SRC_N];
202 struct nvi_br gyro_config1;
203 struct nvi_br gyro_config2;
204 struct nvi_br accel_config;
205 struct nvi_br accel_config2;
206 struct nvi_br lp_config;
207 struct nvi_br int_pin_cfg;
208 struct nvi_br int_enable;
209 struct nvi_br int_dmp;
210 struct nvi_br int_status;
211 struct nvi_br out_h[DEV_MPU_N];
212 struct nvi_br ext_sens_data_00;
213 struct nvi_br signal_path_reset;
214 struct nvi_br user_ctrl;
217 struct nvi_br fifo_en;
218 struct nvi_br fifo_rst;
219 struct nvi_br fifo_sz;
220 struct nvi_br fifo_count_h;
221 struct nvi_br fifo_rw;
222 struct nvi_br fifo_cfg;
223 struct nvi_br who_am_i;
224 struct nvi_br i2c_mst_status;
225 struct nvi_br i2c_mst_odr_config;
226 struct nvi_br i2c_mst_ctrl;
227 struct nvi_br i2c_mst_delay_ctrl;
228 struct nvi_br i2c_slv_addr[AUX_PORT_MAX];
229 struct nvi_br i2c_slv_reg[AUX_PORT_MAX];
230 struct nvi_br i2c_slv_ctrl[AUX_PORT_IO];
231 struct nvi_br i2c_slv4_ctrl;
232 struct nvi_br i2c_slv_do[AUX_PORT_MAX];
233 struct nvi_br i2c_slv4_di;
234 struct nvi_br mem_addr;
235 struct nvi_br mem_rw;
236 struct nvi_br mem_bank;
237 struct nvi_br fw_start;
238 struct nvi_br reg_bank;
253 u8 int_fifo_ovrflw_0;
254 u8 int_fifo_ovrflw_1;
255 u8 int_fifo_ovrflw_2;
256 u8 int_fifo_ovrflw_3;
261 u8 slv_fifo_en[AUX_PORT_IO];
265 u16 accel_offset[AXIS_N];
266 u16 gyro_offset[AXIS_N];
277 u8 i2c_mst_odr_config;
279 u8 i2c_mst_delay_ctrl;
280 u8 i2c_slv_addr[AUX_PORT_MAX];
281 u8 i2c_slv_reg[AUX_PORT_MAX];
282 u8 i2c_slv_ctrl[AUX_PORT_IO];
284 u8 i2c_slv_do[AUX_PORT_MAX];
297 u32 motion_event_ctl;
305 u32 cpass_time_buffer;
308 u32 cpass_rad_3d_thr;
309 u32 cpass_nomot_var_thr;
322 u32 d_smd_delay_thld;
323 u32 d_smd_delay2_thld;
325 u32 d_smd_delay_cntr;
330 struct nvi_mc_icm icm;
331 struct nvi_mc_mpu mpu;
339 unsigned int fw_crc32;
340 unsigned int fw_mem_addr;
341 unsigned int fw_start;
342 unsigned int dmp_reset_delay_ms;
344 unsigned int dev_msk;
347 const struct nvi_dmp_dev *dd;
348 int (*fn_rd)(struct nvi_state *st, s64 ts, unsigned int n);
349 int (*fn_clk_n)(struct nvi_state *st, u32 *clk_n);
350 int (*fn_init)(struct nvi_state *st);
351 int (*fn_en)(struct nvi_state *st);
352 int (*fn_dev_init)(struct nvi_state *st, unsigned int dev);
353 int (*fn_dev_batch)(struct nvi_state *st, unsigned int dev, int port);
357 int (*pm)(struct nvi_state *st, u8 pm1, u8 pm2, u8 lp);
358 int (*init)(struct nvi_state *st);
359 int (*st_acc)(struct nvi_state *st);
360 int (*st_gyr)(struct nvi_state *st);
361 int (*en_acc)(struct nvi_state *st);
362 int (*en_gyr)(struct nvi_state *st);
367 unsigned int reg_bank_n;
368 const struct nvi_hal_src *src;
372 const unsigned long *lp_tbl;
373 unsigned int lp_tbl_n;
374 const struct nvi_hal_dev *dev[DEV_N_AUX];
375 const struct nvi_hal_reg *reg;
376 const struct nvi_hal_bit *bit;
383 struct sensor_cfg cfg;
384 unsigned int usr_cfg;
388 unsigned int period_us;
389 unsigned int timeout_us;
402 struct nvi_mpu_port nmp;
403 unsigned int ext_data_offset;
404 unsigned int period_us;
405 unsigned int timeout_us;
416 struct aux_port port[AUX_PORT_MAX];
417 s64 bypass_timeout_ns;
418 unsigned int bypass_lock;
419 unsigned int dmp_en_msk;
420 unsigned int dmp_ctrl_msk;
421 unsigned int ext_data_n;
423 unsigned char ext_data[AUX_EXT_DATA_REG_MAX];
424 unsigned char clock_i2c;
430 * struct inv_chip_info_s - Chip related information.
431 * @product_id: Product id.
432 * @product_revision: Product revision.
433 * @silicon_revision: Silicon revision.
434 * @software_revision: software revision.
435 * @multi: accel specific multiplier.
436 * @gyro_sens_trim: Gyro sensitivity trim factor.
437 * @accel_sens_trim: accel sensitivity trim factor.
439 struct inv_chip_info_s {
443 u8 software_revision;
450 struct i2c_client *i2c;
451 struct nvs_fn_if *nvs;
452 struct regulator_bulk_data vreg[2];
453 struct notifier_block nb_vreg[2];
454 const struct nvi_hal *hal;
457 struct aux_ports aux;
462 unsigned int dmp_en_msk;
463 unsigned int dmp_dev_msk;
464 unsigned int bm_timeout_us;
465 struct nvi_snsr snsr[DEV_N_AUX];
466 struct nvi_src src[SRC_N];
472 bool irq_set_irq_wake;
480 struct inv_chip_info_s chip_info;
481 int bias[DEV_AXIS_N][AXIS_N];
482 s16 dev_offset[DEV_AXIS_N][AXIS_N];
483 s16 rom_offset[DEV_AXIS_N][AXIS_N];
484 u8 st_data[DEV_AXIS_N][AXIS_N];
486 unsigned int bypass_timeout_ms;
487 unsigned int irq_storm_n;
489 u8 buf[NVI_FIFO_SAMPLE_SIZE_MAX * 2]; /* (* 2)=FIFO OVERFLOW OFFSET */
492 int nvi_i2c_wr(struct nvi_state *st, const struct nvi_br *br,
493 u8 val, const char *fn);
494 int nvi_i2c_wr_rc(struct nvi_state *st, const struct nvi_br *br,
495 u8 val, const char *fn, u8 *rc);
496 int nvi_i2c_write_rc(struct nvi_state *st, const struct nvi_br *br, u32 val,
497 const char *fn, u8 *rc, bool be);
498 int nvi_i2c_r(struct nvi_state *st, u8 bank, u8 reg, u16 len, u8 *buf);
499 int nvi_i2c_rd(struct nvi_state *st, const struct nvi_br *br, u8 *buf);
500 int nvi_mem_wr(struct nvi_state *st, u16 addr, u16 len, u8 *data,
502 int nvi_mem_wr_be(struct nvi_state *st, u16 addr, u16 len, u32 val);
503 int nvi_mem_wr_be_mc(struct nvi_state *st, u16 addr, u16 len,
505 int nvi_mem_rd(struct nvi_state *st, u16 addr, u16 len, u8 *data);
506 int nvi_mem_rd_le(struct nvi_state *st, u16 addr, u16 len, u32 *val);
507 int nvi_wr_accel_offset(struct nvi_state *st, unsigned int axis, u16 offset);
508 int nvi_wr_gyro_offset(struct nvi_state *st, unsigned int axis, u16 offset);
509 int nvi_wr_fifo_cfg(struct nvi_state *st, int fifo);
510 int nvi_int_able(struct nvi_state *st, const char *fn, bool enable);
511 int nvi_reset(struct nvi_state *st, const char *fn,
512 bool rst_fifo, bool rst_i2c, bool en_irq);
513 int nvi_user_ctrl_en(struct nvi_state *st, const char *fn,
514 bool en_dmp, bool en_fifo, bool en_i2c, bool en_irq);
515 int nvi_wr_pm1(struct nvi_state *st, const char *fn, u8 pm1);
516 int nvi_pm_wr(struct nvi_state *st, const char *fn, u8 pm1, u8 pm2, u8 lp);
517 int nvi_aux_enable(struct nvi_state *st, const char *fn,
518 bool en_req, bool force);
519 int nvi_period_aux(struct nvi_state *st);
520 int nvi_aux_delay(struct nvi_state *st, const char *fn);
521 void nvi_push_delay(struct nvi_state *st);
522 int nvi_push(struct nvi_state *st, unsigned int dev, u8 *buf, s64 ts);
523 s64 nvi_ts_dev(struct nvi_state *st, s64 ts_now,
524 unsigned int dev, unsigned int aux_port);
525 void nvi_err(struct nvi_state *st);
527 extern const struct nvi_hal nvi_hal_20628;
528 extern const struct nvi_hal nvi_hal_6515;
529 extern const struct nvi_hal nvi_hal_6500;
530 extern const struct nvi_hal nvi_hal_6050;
531 extern struct nvi_dmp nvi_dmp_icm;
532 extern struct nvi_dmp nvi_dmp_mpu;