]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - include/linux/mpu_iio.h
iio: imu: nvi v.336 Add DMP AUX support
[sojka/nv-tegra/linux-3.10.git] / include / linux / mpu_iio.h
1 /*
2 * Copyright (C) 2012 Invensense, Inc.
3 * Copyright (c) 2013-2016, NVIDIA CORPORATION.  All rights reserved.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 */
14
15 #ifndef __MPU_H_
16 #define __MPU_H_
17
18 #ifdef __KERNEL__
19 #include <linux/types.h>
20 #include <linux/ioctl.h>
21 #endif
22
23 /* Mount matrices for mount orientation.
24  * MTMAT_XXX_CCW_YYY
25  *     XXX : mount position. TOP for top and BOT for bottom.
26  *     YYY : couter-clockwise rotation angle in degree.
27  */
28 #define MTMAT_TOP_CCW_0         {  1,  0,  0,  0,  1,  0,  0,  0,  1 }
29 #define MTMAT_TOP_CCW_90        {  0, -1,  0,  1,  0,  0,  0,  0,  1 }
30 #define MTMAT_TOP_CCW_180       { -1,  0,  0,  0, -1,  0,  0,  0,  1 }
31 #define MTMAT_TOP_CCW_270       {  0,  1,  0, -1,  0,  0,  0,  0,  1 }
32 #define MTMAT_BOT_CCW_0         { -1,  0,  0,  0,  1,  0,  0,  0, -1 }
33 #define MTMAT_BOT_CCW_90        {  0, -1,  0, -1,  0,  0,  0,  0, -1 }
34 #define MTMAT_BOT_CCW_180       {  1,  0,  0,  0, -1,  0,  0,  0, -1 }
35 #define MTMAT_BOT_CCW_270       {  0,  1,  0,  1,  0,  0,  0,  0, -1 }
36
37 enum secondary_slave_type {
38         SECONDARY_SLAVE_TYPE_NONE,
39         SECONDARY_SLAVE_TYPE_ACCEL,
40         SECONDARY_SLAVE_TYPE_COMPASS,
41         SECONDARY_SLAVE_TYPE_PRESSURE,
42
43         SECONDARY_SLAVE_TYPE_TYPES
44 };
45
46 enum ext_slave_id {
47         ID_INVALID = 0,
48         GYRO_ID_MPU3050,
49         GYRO_ID_MPU6050A2,
50         GYRO_ID_MPU6050B1,
51         GYRO_ID_MPU6050B1_NO_ACCEL,
52         GYRO_ID_ITG3500,
53
54         ACCEL_ID_LIS331,
55         ACCEL_ID_LSM303DLX,
56         ACCEL_ID_LIS3DH,
57         ACCEL_ID_KXSD9,
58         ACCEL_ID_KXTF9,
59         ACCEL_ID_BMA150,
60         ACCEL_ID_BMA222,
61         ACCEL_ID_BMA250,
62         ACCEL_ID_ADXL34X,
63         ACCEL_ID_MMA8450,
64         ACCEL_ID_MMA845X,
65         ACCEL_ID_MPU6050,
66
67         COMPASS_ID_AK8963,
68         COMPASS_ID_AK8975,
69         COMPASS_ID_AK8972,
70         COMPASS_ID_AMI30X,
71         COMPASS_ID_AMI306,
72         COMPASS_ID_YAS529,
73         COMPASS_ID_YAS530,
74         COMPASS_ID_HMC5883,
75         COMPASS_ID_LSM303DLH,
76         COMPASS_ID_LSM303DLM,
77         COMPASS_ID_MMC314X,
78         COMPASS_ID_HSCDTD002B,
79         COMPASS_ID_HSCDTD004A,
80         COMPASS_ID_MLX90399,
81         COMPASS_ID_AK09911,
82
83         PRESSURE_ID_BMP085,
84         PRESSURE_ID_BMP280,
85
86         ID_INVALID_END /* always last, leave auto-assigned */
87 };
88
89 #define INV_PROD_KEY(ver, rev) (ver * 100 + rev)
90
91 #define NVI_CONFIG_BOOT_AUTO            (0) /* auto detect connection to MPU */
92 #define NVI_CONFIG_BOOT_MPU             (1) /* connected to MPU */
93 #define NVI_CONFIG_BOOT_HOST            (2) /* connected to host */
94 #define NVI_CONFIG_BOOT_MASK            (0x03)
95
96 /**
97  * struct mpu_platform_data - Platform data for the mpu driver
98  * @int_config:         Bits [7:3] of the int config register.
99  * @level_shifter:      0: VLogic, 1: VDD
100  * @orientation:        Orientation matrix of the gyroscope
101  * @sec_slave_type:     secondary slave device type, can be compass, accel, etc
102  * @sec_slave_id:       id of the secondary slave device
103  * @secondary_i2c_address: secondary device's i2c address
104  * @secondary_orientation: secondary device's orientation matrix
105  * @key:                key for MPL library.
106  * @nvi_config: the selection determines the device behavior.
107  *              Select from the NVI_CONFIG_BOOT_ defines.
108  *
109  * Contains platform specific information on how to configure the MPU3050 to
110  * work on this platform.  The orientation matricies are 3x3 rotation matricies
111  * that are applied to the data to rotate from the mounting orientation to the
112  * platform orientation.  The values must be one of 0, 1, or -1 and each row and
113  * column should have exactly 1 non-zero value.
114  */
115 struct mpu_platform_data {
116         __u8 int_config;
117         __u8 level_shifter;
118         __s8 orientation[9];
119         enum secondary_slave_type sec_slave_type;
120         enum ext_slave_id sec_slave_id;
121         __u16 secondary_i2c_addr;
122         __s8 secondary_orientation[9];
123         __u8 key[16];
124         enum secondary_slave_type aux_slave_type;
125         enum ext_slave_id aux_slave_id;
126         __u16 aux_i2c_addr;
127
128 #ifdef CONFIG_DTS_INV_MPU_IIO
129         int (*power_on)(struct mpu_platform_data *);
130         int (*power_off)(struct mpu_platform_data *);
131         struct regulator *vdd_ana;
132         struct regulator *vdd_i2c;
133 #endif
134         __u8 nvi_config;
135 };
136
137
138 /**
139  * struct nvi_mpu_port: Allows an external driver to use the MPU
140  *    auxiliary I2C master by providing the details for the I2C
141  *    polling of a device connected to the MPU.
142  * - addr: The 6:0 I2C address of the device connected to
143  *      the MPU.
144  *      7:7 = 0 if the port is to do write transactions.
145  *          = 1 if the port is to do read transactions.
146  * - reg: The device register the I2C transaction will
147  *      use.
148  * - ctrl: The number of consecutive registers to read in
149  *      3:0. If the port is to do write transactions then this
150  *      value must be 1.  See MPU documentation for the other
151  *      bits in I2C_SLVx_CTRL that can be applied by this byte.
152  * - data_out: The data byte written if the port is configured
153  *      to do writes (addr 7:7 = 0).
154  * - delay_ms: The polling delay time between I2C transactions
155  *      in ms.  Note that the MPU HW only supports one delay
156  *      time so the longest delay of all the MPU ports enabled
157  *      is used.
158  * - period_us: The period at which the read data is reported.
159  * - shutdown_bypass: set if a connection to the host is needed
160  *      when the system is shutdown.  The MPU API will be
161  *      disabled as part of its shutdown but it will enable the
162  *      bypass if this is true.
163  * - *handler: The pointer to the function called when the data
164  *      is available.  This can be NULL if the port is
165  *      configured for write transactions.
166  *      The function is called with the following parameters:
167  *      - *data: The pointer to the data to read.
168  *      - length: The number of bytes to be read (same value as
169  *           length above).
170  *      - timestamp: The timestamp of when the data was polled.
171  *      - *pointer: A generic pointer defined next below.  Note
172  *           that this can be NULL if this will be a write I2C
173  *           transaction.
174  * - *ext_driver: A generic pointer that can be used by the
175  *      external driver.  Note that this is specifically for the
176  *      external driver and not used by the MPU.
177  * - matrix: device orientation on platform.
178  *      Needed by the DMP.
179  * - type: Define if device is to be used by the MPU DMP.
180  * - id: Define if device is to be used by the MPU DMP.
181  * - asa: compass axis sensitivity adjustment.
182  *      Needed by the DMP.
183  */
184 struct nvi_mpu_port {
185         u8 addr;
186         u8 reg;
187         u8 ctrl;
188         u8 data_out;
189         unsigned int delay_ms;
190         unsigned int period_us;
191         bool shutdown_bypass;
192         void (*handler)(u8 *data, unsigned int len,
193                         long long timestamp, void *ext_driver);
194         void *ext_driver;
195         signed char matrix[9];
196         enum secondary_slave_type type;
197         enum ext_slave_id id;
198         u64 q30[3];
199 };
200
201 struct nvi_mpu_inf {
202         unsigned int period_us_min;
203         unsigned int period_us_max;
204         unsigned int fifo_reserve;
205         unsigned int fifo_max;
206         unsigned int dmp_rd_len_sts;    /* status length from DMP */
207         unsigned int dmp_rd_len_data;   /* data length from DMP */
208         bool dmp_rd_be_sts;             /* status endian from DMP */
209         bool dmp_rd_be_data;            /* data endian from DMP */
210 };
211
212 /**
213  * Expected use of the nvi_mpu_ routines are as follows:
214  * - nvi_mpu_dev_valid: Use to validate whether a device is
215  *      connected to the MPU.
216  * - nvi_mpu_port_alloc: Request a connection to the device.  If
217  *      successful, a port number will be returned to identify
218  *      the connection.  The port number is then used for all
219  *      further communication with the connection.
220  * - nvi_mpu_port_free: Use to close the port connection.
221  * - nvi_mpu_enable: Use to enable/disable a port.
222  *      The enable and FIFO enable is disabled by default so
223  *      this will be required after a port is assigned.
224  * - nvi_mpu_delay_us: Use to set the sampling rate in
225  *      microseconds.  The fastest rate of all the enabled MPU
226  *      devices will be used that does not exceed the
227  *      nvi_mpu_delay_ms setting of an enabled device.
228  * - nvi_mpu_delay_ms: Use to change the port polling delay at
229  *      runtime. There is only one HW delay so the delay used
230  *      will be the longest delay of all the enabled ports.
231  *      This is separate from the sampling rate
232  *      (nvi_mpu_delay_us).  See function notes below.
233  * - nvi_mpu_data_out: Use to change the data written at runtime
234  *      for ports that are configured as I2C write transactions.
235  * - nvi_mpu_bypass request/release: Use to connect/disconnect
236  *      the MPU host from the device.  When bypass is enabled,
237  *      the connection from the device to the MPU will then be
238  *      connected to the host (that the MPU is connected to).
239  *      This is a global connection switch affecting all ports
240  *      so a mechanism is in place of whether the request is
241  *      honored or not.  See the function notes for
242  *      nvi_mpu_bypass_request.
243  */
244
245 /**
246  * Use to validate a device connected to the MPU I2C master.
247  * The function works by doing a single byte read or write to
248  * the device and detecting a NACK.  Typically, the call would
249  * be set up to read a byte ID of the device.
250  * @param struct nvi_mpu_port *nmp
251  *           Only the following is needed in nmp:
252  *           - addr
253  *           - reg
254  *           - ctrl
255  *           - data_out if a write transaction
256  * @param *data: pointer for read data.  Can be NULL if write.
257  * @return int error
258  *            Possible return value or errors are:
259  *            - 0: device is connected to MPU.
260  *            - -EAGAIN: MPU is not initialized yet.
261  *            - -EPERM: MPU is shutdown.  MPU API won't be
262  *                 available until a system restart.
263  *            - -EBUSY: MPU is busy with another request.
264  *            - -ENODEV: The device is not connected to the MPU.
265  *            - -EINVAL: Problem with input parameters.
266  *            - -EIO: The device is connected but responded with
267  *                 a NACK.
268  */
269 int nvi_mpu_dev_valid(struct nvi_mpu_port *nmp, u8 *data);
270
271 /**
272  * Request a port.
273  * @param struct nvi_mpu_port *nmp
274  *           - addr: device I2C address 6:0.
275  *                7:7 = 0 if the port is to do writes.
276  *                7:7 = 1 if the port is to do reads.
277  *           - reg: the starting register to write or read.
278  *           - ctrl: number of bytes to read.  Use 1 if port
279  *                is configured to do writes.
280  *           - data_out: only valid if port is configured to do
281  *                writes.
282  *           - delay: polling delay
283  *           - handler: function to call when data is read. This
284  *                should be NULL if the port is configured to do
285  *                writes.
286  *           - ext_driver: this pointer is passed in handler for
287  *                use by external driver.  This should be NULL
288  *                if the port is configured for writes.
289  * @return int error/port ID
290  *            if return >= 0 then this is the port ID.  The ID
291  *            will have a value of 0 to 3 (HW has 4 ports).
292  *            Possible errors are:
293  *            - -EAGAIN: MPU is not initialized yet.
294  *            - -EPERM: MPU is shutdown.  MPU API won't be
295  *                 available until a system restart.
296  *            - -EBUSY: MPU is busy with another request.
297  *            - -ENODEV: A port is not available.  The only way
298  *                 to resolve this error is for a port to be
299  *                 freed.
300  *            - -EINVAL: Problem with input parameters.
301  */
302 int nvi_mpu_port_alloc(struct nvi_mpu_port *nmp);
303
304 /**
305  * Remove a port.
306  * @param port
307  * @return int error
308  *            Possible errors are:
309  *            - -EAGAIN: MPU is not initialized yet.
310  *            - -EPERM: MPU is shutdown.  MPU API won't be
311  *                 available until a system restart.
312  *            - -EBUSY: MPU is busy with another request.
313  *            - -EINVAL: Problem with input parameters.
314  */
315 int nvi_mpu_port_free(int port);
316
317 /**
318  * Enable/disable ports.  Use of a port mask (port_mask) allows
319  * enabling/disabling multiple ports at the same time.
320  * @param port_mask
321  * @param enable
322  * @return int error
323  *            Possible errors are:
324  *            - -EAGAIN: MPU is not initialized yet.
325  *            - -EPERM: MPU is shutdown.  MPU API won't be
326  *                 available until a system restart.
327  *            - -EBUSY: MPU is busy with another request.
328  *            - -EINVAL: Problem with input parameters.
329  */
330 int nvi_mpu_enable(unsigned int port_mask, bool enable);
331
332 /**
333  * Use to change the ports polling delay in milliseconds.
334  * A delay value of 0 disables the delay for that port.  The
335  * hardware only supports one delay value so the largest request
336  * of all the enabled ports is used. The polling delay is in
337  * addition to the sampling delay (nvi_mpu_delay_us).  This is
338  * typically used to guarantee a delay after an I2C write to a
339  * device to allow the device to process the request and be read
340  * by another port before another write at the sampling delay.
341  * @param port
342  * @param delay_ms
343  * @return int error
344  *            Possible errors are:
345  *            - -EAGAIN: MPU is not initialized yet.
346  *            - -EPERM: MPU is shutdown.  MPU API won't be
347  *                 available until a system restart.
348  *            - -EBUSY: MPU is busy with another request.
349  *            - -EINVAL: Problem with input parameters.
350  */
351 int nvi_mpu_delay_ms(int port, u8 delay_ms);
352
353 /**
354  * Use to change the data written to the sensor.
355  * @param port
356  * @param data_out is the new data to be written
357  * @return int error
358  *            Possible errors are:
359  *            - -EAGAIN: MPU is not initialized yet.
360  *            - -EPERM: MPU is shutdown.  MPU API won't be
361  *                 available until a system restart.
362  *            - -EBUSY: MPU is busy with another request.
363  *            - -EINVAL: Problem with input parameters.
364  */
365 int nvi_mpu_data_out(int port, u8 data_out);
366
367 /**
368  * batch mode.
369  * @param port
370  * @param period_us
371  * @param timeout_us
372  * @return int error
373  *            Possible errors are:
374  *            - -EAGAIN: MPU is not initialized yet.
375  *            - -EPERM: MPU is shutdown.  MPU API won't be
376  *                 available until a system restart.
377  *            - -EBUSY: MPU is busy with another request.
378  *            - -EINVAL: timeout_us not supported if > 0.
379  */
380 int nvi_mpu_batch(int port, unsigned int period_us, unsigned int timeout_us);
381
382 /**
383  * batch flush.
384  * @param port
385  * @return int error
386  *            Possible errors are:
387  *            - -EAGAIN: MPU is not initialized yet.
388  *            - -EPERM: MPU is shutdown.  MPU API won't be
389  *                 available until a system restart.
390  *            - -EBUSY: MPU is busy with another request.
391  *            - -EINVAL: Problem with input parameters.
392  */
393 int nvi_mpu_flush(int port);
394
395 /**
396  * nvi_mpu_info.  MPU/ICM populates the nvi_mpu_inf structure
397  * pointed to by inf.
398  * @param port used for reading
399  * @param ptr to nvi_mpu_inf structure.
400  * @return int error
401  *            Possible errors are:
402  *            - -EAGAIN: MPU is not initialized yet.
403  *            - -EPERM: MPU is shutdown.  MPU API won't be
404  *                 available until a system restart.
405  *            - -EBUSY: MPU is busy with another request.
406  *            - -EINVAL: Problem with input parameters.
407  */
408 int nvi_mpu_info(int read_port, struct nvi_mpu_inf *inf);
409
410 /**
411  * Enable/disable the MPU bypass mode.  When enabled, the MPU
412  * will connect its auxiliary I2C ports to the host.  This is
413  * typically used to initialize a device that requires more I2C
414  * transactions than the automated port polling can offer.
415  * EVERY nvi_mpu_bypass_request call must be balanced with a
416  * nvi_mpu_bypass_release call!
417  * A bypass request does not need a following ~enable call.  The
418  * release call will automatically handle the correct bypass
419  * enable setting. The request locks the bypass setting if
420  * successful.  The release unlocks and restores the setting if
421  * need be.  Although odd, the purpose of the request call with
422  * the enable cleared to false is to allow an external driver to
423  * access its device that would normally conflict with a device
424  * behind the MPU.  Note that this call must not be a permanent
425  * solution, i.e. delayed or no release call.
426  * When the MPU is in a shutdown state the return error will be
427  * -EPERM and bypass will be enabled to allow access from the
428  * host to the devices connected to the MPU for their own
429  * shutdown needs.
430  * @param enable
431  * @return int error: calls that return with an error must not
432  *            be balanced with a release call.
433  *            Possible errors are:
434  *            - -EAGAIN: MPU is not initialized yet.
435  *            - -EPERM: MPU is shutdown.  MPU API won't be
436  *                 available until a system restart.
437  *            - -EBUSY: MPU is busy with another request.
438  */
439 int nvi_mpu_bypass_request(bool enable);
440
441 /**
442  * See the nvi_mpu_bypass_request notes.
443  * @return int 0: Always returns 0.  The call return should be
444  *         void but for backward compatibility it returns 0.
445  */
446 int nvi_mpu_bypass_release(void);
447
448 #endif  /* __MPU_H_ */