]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/iio/imu/nvi_mpu/nvi.h
f81be14f6d24a8fd832589c64ff4f7a37532dcbe
[sojka/nv-tegra/linux-3.10.git] / drivers / iio / imu / nvi_mpu / nvi.h
1 /* Copyright (c) 2014-2016, NVIDIA CORPORATION.  All rights reserved.
2  *
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.
6  *
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.
11  */
12
13 /* NVS = NVidia Sensor framework */
14 /* See nvs_iio.c and nvs.h for documentation */
15
16 #ifndef _NVI_H_
17 #define _NVI_H_
18
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>
25
26 #define NVI_BYPASS_TIMEOUT_MS           (1000)
27 #define POWER_UP_TIME                   (100)
28 #define REG_UP_TIME                     (5)
29 #define POR_MS                          (100)
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)
34 #define KBUF_SZ                         (64)
35 #define SRC_MPU                         (0)
36 #define SRC_GYR                         (0)
37 #define SRC_ACC                         (1)
38 #define SRC_AUX                         (2)
39 #define SRC_DMP                         (3)
40 #define SRC_N                           (4)
41
42 #define AXIS_X                          (0)
43 #define AXIS_Y                          (1)
44 #define AXIS_Z                          (2)
45 #define AXIS_N                          (3)
46 #define DEV_ACC                         (0)
47 #define DEV_GYR                         (1)
48 #define DEV_AXIS_N                      (2)
49 #define DEV_TMP                         (2)
50 #define DEV_MPU_N                       (3)
51 #define DEV_SM                          (3)
52 #define DEV_STP                         (4)
53 #define DEV_QTN                         (5)
54 #define DEV_GMR                         (6)
55 #define DEV_GYU                         (7)
56 #define DEV_N                           (8)
57 #define DEV_AUX                         (8)
58 #define DEV_N_AUX                       (9)
59 #define DEV_DMP                         (9)
60 #define FW_LOADED                       (16)
61 #define EN_STDBY                        (17)
62 #define EN_LP                           (18)
63 #define MSK_RST                         ((1 << EN_STDBY) | \
64                                          (1 << EN_LP))
65 #define MSK_DEV_MPU                     ((1 << DEV_ACC) | \
66                                          (1 << DEV_GYR) | \
67                                          (1 << DEV_TMP))
68 #define MSK_DEV_MPU_AUX                 (MSK_DEV_MPU | (1 << DEV_AUX))
69 #define MSK_DEV_DMP                     ((1 << DEV_SM) | \
70                                          (1 << DEV_STP) | \
71                                          (1 << DEV_QTN) | \
72                                          (1 << DEV_GMR) | \
73                                          (1 << DEV_GYU))
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) | \
78                                          (1 << DEV_AUX) | \
79                                          MSK_DEV_DMP)
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)
89 #define NVI_PM_ON                       (6)
90 #define NVI_PM_ON_FULL                  (7)
91 #define INV_CLK_INTERNAL                (0)
92 #define INV_CLK_PLL                     (1)
93
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))
99
100 #define NVI_RC_BANK_REG_BANK            (0x7F / 64)
101 #define NVI_RC_MSK_REG_BANK             ((u64)(1ULL << (0x7F % 64)))
102
103 /* register bits */
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)
140
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)
146
147 struct nvi_state;
148
149 struct nvi_rr {
150         struct nvs_float max_range;
151         struct nvs_float resolution;
152 };
153
154 struct nvi_hal_dev {
155         int version;
156         int src;
157         unsigned int rr_0n;
158         struct nvi_rr *rr;
159         struct nvs_float scale;
160         struct nvs_float offset;
161         struct nvs_float milliamp;
162         u16 fifo_en_msk;
163         int fifo;
164         unsigned int fifo_data_n;
165 };
166
167 struct nvi_hal_src {
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);
172 };
173
174 struct nvi_src {
175         bool ts_reset;
176         s64 ts_1st;
177         s64 ts_end;
178         s64 ts_period;
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;
184         u32 base_t;
185 };
186
187 struct nvi_br {
188         u8 bank;
189         u8 reg;
190         u8 len;
191         u32 dflt;
192 };
193
194 struct nvi_hal_reg {
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;
215         struct nvi_br pm1;
216         struct nvi_br pm2;
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;
239 };
240
241 struct nvi_hal_bit {
242         u8 dmp_int_sm;
243         u8 dmp_int_stp;
244         u8 int_i2c_mst;
245         u8 int_dmp;
246         u8 int_pll_rdy;
247         u8 int_wom;
248         u8 int_wof;
249         u8 int_data_rdy_0;
250         u8 int_data_rdy_1;
251         u8 int_data_rdy_2;
252         u8 int_data_rdy_3;
253         u8 int_fifo_ovrflw_0;
254         u8 int_fifo_ovrflw_1;
255         u8 int_fifo_ovrflw_2;
256         u8 int_fifo_ovrflw_3;
257         u8 int_fifo_wm_0;
258         u8 int_fifo_wm_1;
259         u8 int_fifo_wm_2;
260         u8 int_fifo_wm_3;
261         u8 slv_fifo_en[AUX_PORT_IO];
262 };
263
264 struct nvi_rc {
265         u16 accel_offset[AXIS_N];
266         u16 gyro_offset[AXIS_N];
267         u16 smplrt[SRC_N];
268         u8 gyro_config1;
269         u8 gyro_config2;
270         u8 accel_config;
271         u8 accel_config2;
272         u8 lp_config;
273         u8 int_pin_cfg;
274         u32 int_enable;
275         u8 int_dmp;
276         u32 int_status;
277         u8 i2c_mst_odr_config;
278         u8 i2c_mst_ctrl;
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];
283         u8 i2c_slv4_ctrl;
284         u8 i2c_slv_do[AUX_PORT_MAX];
285         u8 user_ctrl;
286         u8 pm1;
287         u8 pm2;
288         u16 fifo_en;
289         u8 fifo_sz;
290         u8 fifo_cfg;
291         u8 reg_bank;
292 };
293
294 struct nvi_mc_icm {
295         u32 data_out_ctl;
296         u32 data_intr_ctl;
297         u32 motion_event_ctl;
298         u32 acc_scale;
299         u32 acc_scale2;
300         u32 accel_cal_rate;
301         u32 ped_rate;
302         u32 accel_alpha_var;
303         u32 accel_a_var;
304         u32 accel_only_gain;
305         u32 cpass_time_buffer;
306         u32 cpass_alpha_var;
307         u32 cpass_a_var;
308         u32 cpass_rad_3d_thr;
309         u32 cpass_nomot_var_thr;
310         u32 smd_thld;
311         u32 smd_thld_n;
312         u32 smd_delay;
313         u32 smd_delay2;
314         u32 smd_reset;
315         u32 wom_enable;
316         u32 wtf_8a;
317 };
318
319 struct nvi_mc_mpu {
320         u32 cfg_fifo_int;
321         u32 d_smd_mot_thld;
322         u32 d_smd_delay_thld;
323         u32 d_smd_delay2_thld;
324         u32 d_smd_exe_state;
325         u32 d_smd_delay_cntr;
326 };
327
328 struct nvi_mc {
329         union {
330                 struct nvi_mc_icm icm;
331                 struct nvi_mc_mpu mpu;
332         };
333 };
334
335 struct nvi_dmp {
336         const u8 const *fw;
337         unsigned int fw_ver;
338         unsigned int fw_len;
339         unsigned int fw_crc32;
340         unsigned int fw_mem_addr;
341         unsigned int fw_start;
342         unsigned int dmp_reset_delay_ms;
343         int fifo_mode;
344         unsigned int dev_msk;
345         unsigned int en_msk;
346         unsigned int dd_n;
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);
354 };
355
356 struct nvi_fn {
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);
363 };
364
365 struct nvi_hal {
366         unsigned int regs_n;
367         unsigned int reg_bank_n;
368         const struct nvi_hal_src *src;
369         unsigned int src_n;
370         int *fifo_dev;
371         unsigned int fifo_n;
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;
377         struct nvi_fn *fn;
378         struct nvi_dmp *dmp;
379 };
380
381 struct nvi_snsr {
382         void *nvs_st;
383         struct sensor_cfg cfg;
384         unsigned int usr_cfg;
385         int buf_shft;
386         unsigned int buf_n;
387         unsigned int enable;
388         unsigned int period_us;
389         unsigned int timeout_us;
390         unsigned int sts;
391         unsigned int odr;
392         unsigned int ts_n;
393         s64 ts_push_delay;
394         s64 push_delay_ns;
395         s64 ts_last;
396         bool ts_reset;
397         bool flush;
398         bool matrix;
399 };
400
401 struct aux_port {
402         struct nvi_mpu_port nmp;
403         unsigned int ext_data_offset;
404         unsigned int period_us;
405         unsigned int timeout_us;
406         unsigned int odr;
407         s64 ts_last;
408         bool ts_reset;
409         bool flush;
410         bool hw_valid;
411         bool hw_en;
412         bool hw_do;
413 };
414
415 struct aux_ports {
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;
422         u8 delay_hw;
423         unsigned char ext_data[AUX_EXT_DATA_REG_MAX];
424         unsigned char clock_i2c;
425         bool reset_i2c;
426         bool reset_fifo;
427 };
428
429 /**
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.
438  */
439 struct inv_chip_info_s {
440         u8 product_id;
441         u8 product_revision;
442         u8 silicon_revision;
443         u8 software_revision;
444         u8 multi;
445         u32 gyro_sens_trim;
446         u32 accel_sens_trim;
447 };
448
449 struct nvi_state {
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;
455         struct nvi_rc rc;
456         struct nvi_mc mc;
457         struct aux_ports aux;
458         unsigned int sts;
459         unsigned int errs;
460         unsigned int info;
461         unsigned int en_msk;
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];
467         int fifo_src;
468         u64 rc_msk[8];
469         bool rc_dis;
470         bool mc_dis;
471         bool irq_dis;
472         bool irq_set_irq_wake;
473         bool icm_dmp_war;
474         int pm;
475         u32 dmp_clk_n;
476         s64 ts_now;
477         s64 ts_vreg_en[2];
478         atomic64_t ts_irq;
479
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];
485
486         unsigned int bypass_timeout_ms;
487         unsigned int irq_storm_n;
488         unsigned int buf_i;
489         u8 buf[NVI_FIFO_SAMPLE_SIZE_MAX * 2]; /* (* 2)=FIFO OVERFLOW OFFSET */
490 };
491
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,
501                bool validate);
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,
504                      u32 val, u32 *mc);
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);
526
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;
533
534 #endif /* _NVI_H_ */
535