]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - include/linux/mpu_iio.h
7307ca7fc9d7a403178dfcf8b1b08c638b75565b
[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  * - dmp_ctrl: When the DMP in enabled, the number of
153  *      consecutive registers to read in 3:0. If the port is to
154  *      do write transactions then this value must be 1.  See
155  *      MPU documentation for the other bits in I2C_SLVx_CTRL
156  *      that can be applied by this byte.
157  * - data_out: The data byte written if the port is configured
158  *      to do writes (addr 7:7 = 0).
159  * - delay_ms: The polling delay time between I2C transactions
160  *      in ms.  Note that the MPU HW only supports one delay
161  *      time so the longest delay of all the MPU ports enabled
162  *      is used.
163  * - delay_us: The delay at which the read data is reported.
164  * - shutdown_bypass: set if a connection to the host is needed
165  *      when the system is shutdown.  The MPU API will be
166  *      disabled as part of its shutdown but it will enable the
167  *      bypass if this is true.
168  * - *handler: The pointer to the function called when the data
169  *      is available.  This can be NULL if the port is
170  *      configured for write transactions.
171  *      The function is called with the following parameters:
172  *      - *data: The pointer to the data to read.
173  *      - length: The number of bytes to be read (same value as
174  *           length above).
175  *      - timestamp: The timestamp of when the data was polled.
176  *      - *pointer: A generic pointer defined next below.  Note
177  *           that this can be NULL if this will be a write I2C
178  *           transaction.
179  * - *ext_driver: A generic pointer that can be used by the
180  *      external driver.  Note that this is specifically for the
181  *      external driver and not used by the MPU.
182  * - matrix: device orientation on platform.
183  *      Needed by the DMP.
184  * - type: Define if device is to be used by the MPU DMP.
185  * - id: Define if device is to be used by the MPU DMP.
186  * - asa: compass axis sensitivity adjustment.
187  *      Needed by the DMP.
188  */
189 struct nvi_mpu_port {
190         u8 addr;
191         u8 reg;
192         u8 ctrl;
193         u8 dmp_ctrl;
194         u8 data_out;
195         unsigned int delay_ms;
196         unsigned long delay_us;
197         bool shutdown_bypass;
198         void (*handler)(u8 *data, unsigned int len,
199                         long long timestamp, void *ext_driver);
200         void *ext_driver;
201         signed char matrix[9];
202         enum secondary_slave_type type;
203         enum ext_slave_id id;
204         u64 q30[3];
205 };
206
207 /**
208  * Expected use of the nvi_mpu_ routines are as follows:
209  * - nvi_mpu_dev_valid: Use to validate whether a device is
210  *      connected to the MPU.
211  * - nvi_mpu_port_alloc: Request a connection to the device.  If
212  *      successful, a port number will be returned to identify
213  *      the connection.  The port number is then used for all
214  *      further communication with the connection.
215  * - nvi_mpu_port_free: Use to close the port connection.
216  * - nvi_mpu_enable: Use to enable/disable a port.
217  *      The enable and FIFO enable is disabled by default so
218  *      this will be required after a port is assigned.
219  * - nvi_mpu_delay_us: Use to set the sampling rate in
220  *      microseconds.  The fastest rate of all the enabled MPU
221  *      devices will be used that does not exceed the
222  *      nvi_mpu_delay_ms setting of an enabled device.
223  * - nvi_mpu_delay_ms: Use to change the port polling delay at
224  *      runtime. There is only one HW delay so the delay used
225  *      will be the longest delay of all the enabled ports.
226  *      This is separate from the sampling rate
227  *      (nvi_mpu_delay_us).  See function notes below.
228  * - nvi_mpu_data_out: Use to change the data written at runtime
229  *      for ports that are configured as I2C write transactions.
230  * - nvi_mpu_bypass request/release: Use to connect/disconnect
231  *      the MPU host from the device.  When bypass is enabled,
232  *      the connection from the device to the MPU will then be
233  *      connected to the host (that the MPU is connected to).
234  *      This is a global connection switch affecting all ports
235  *      so a mechanism is in place of whether the request is
236  *      honored or not.  See the function notes for
237  *      nvi_mpu_bypass_request.
238  */
239
240 /**
241  * Use to validate a device connected to the MPU I2C master.
242  * The function works by doing a single byte read or write to
243  * the device and detecting a NACK.  Typically, the call would
244  * be set up to read a byte ID of the device.
245  * @param struct nvi_mpu_port *nmp
246  *           Only the following is needed in nmp:
247  *           - addr
248  *           - reg
249  *           - ctrl
250  *           - data_out if a write transaction
251  * @param *data: pointer for read data.  Can be NULL if write.
252  * @return int error
253  *            Possible return value or errors are:
254  *            - 0: device is connected to MPU.
255  *            - -EAGAIN: MPU is not initialized yet.
256  *            - -EPERM: MPU is shutdown.  MPU API won't be
257  *                 available until a system restart.
258  *            - -EBUSY: MPU is busy with another request.
259  *            - -ENODEV: The device is not connected to the MPU.
260  *            - -EINVAL: Problem with input parameters.
261  *            - -EIO: The device is connected but responded with
262  *                 a NACK.
263  */
264 int nvi_mpu_dev_valid(struct nvi_mpu_port *nmp, u8 *data);
265
266 /**
267  * Request a port.
268  * @param struct nvi_mpu_port *nmp
269  *           - addr: device I2C address 6:0.
270  *                7:7 = 0 if the port is to do writes.
271  *                7:7 = 1 if the port is to do reads.
272  *           - reg: the starting register to write or read.
273  *           - ctrl: number of bytes to read.  Use 1 if port
274  *                is configured to do writes.
275  *           - data_out: only valid if port is configured to do
276  *                writes.
277  *           - delay: polling delay
278  *           - handler: function to call when data is read. This
279  *                should be NULL if the port is configured to do
280  *                writes.
281  *           - ext_driver: this pointer is passed in handler for
282  *                use by external driver.  This should be NULL
283  *                if the port is configured for writes.
284  * @param port: request a specific port (0 to 3).
285  *            If port is -1 then the returned port ID will be
286  *            automatically selected.
287  *            Requesting a specific port is used when the device
288  *            is expected to work with the Invensense DMP, since
289  *            the DMP FW is pathetically designed and has strict
290  *            limitations of how it will work with auxiliary
291  *            devices.
292  * @return int error/port ID
293  *            if return >= 0 then this is the port ID.  The ID
294  *            will have a value of 0 to 3 (HW has 4 ports).
295  *            Possible errors are:
296  *            - -EAGAIN: MPU is not initialized yet.
297  *            - -EPERM: MPU is shutdown.  MPU API won't be
298  *                 available until a system restart.
299  *            - -EBUSY: MPU is busy with another request.
300  *            - -ENODEV: A port is not available.  The only way
301  *                 to resolve this error is for a port to be
302  *                 freed.
303  *            - -EINVAL: Problem with input parameters.
304  */
305 int nvi_mpu_port_alloc(struct nvi_mpu_port *nmp, int port);
306
307 /**
308  * Remove a port.
309  * @param port
310  * @return int error
311  *            Possible errors are:
312  *            - -EAGAIN: MPU is not initialized yet.
313  *            - -EPERM: MPU is shutdown.  MPU API won't be
314  *                 available until a system restart.
315  *            - -EBUSY: MPU is busy with another request.
316  *            - -EINVAL: Problem with input parameters.
317  */
318 int nvi_mpu_port_free(int port);
319
320 /**
321  * Enable/disable ports.  Use of a port mask (port_mask) allows
322  * enabling/disabling multiple ports at the same time.
323  * @param port_mask
324  * @param enable
325  * @return int error
326  *            Possible errors are:
327  *            - -EAGAIN: MPU is not initialized yet.
328  *            - -EPERM: MPU is shutdown.  MPU API won't be
329  *                 available until a system restart.
330  *            - -EBUSY: MPU is busy with another request.
331  *            - -EINVAL: Problem with input parameters.
332  */
333 int nvi_mpu_enable(unsigned int port_mask, bool enable);
334
335 /**
336  * Use to change the ports polling delay in milliseconds.
337  * A delay value of 0 disables the delay for that port.  The
338  * hardware only supports one delay value so the largest request
339  * of all the enabled ports is used. The polling delay is in
340  * addition to the sampling delay (nvi_mpu_delay_us).  This is
341  * typically used to guarantee a delay after an I2C write to a
342  * device to allow the device to process the request and be read
343  * by another port before another write at the sampling delay.
344  * @param port
345  * @param delay_ms
346  * @return int error
347  *            Possible errors are:
348  *            - -EAGAIN: MPU is not initialized yet.
349  *            - -EPERM: MPU is shutdown.  MPU API won't be
350  *                 available until a system restart.
351  *            - -EBUSY: MPU is busy with another request.
352  *            - -EINVAL: Problem with input parameters.
353  */
354 int nvi_mpu_delay_ms(int port, u8 delay_ms);
355
356 /**
357  * Use to change the data written to the sensor.
358  * @param port
359  * @param data_out is the new data to be written
360  * @return int error
361  *            Possible errors are:
362  *            - -EAGAIN: MPU is not initialized yet.
363  *            - -EPERM: MPU is shutdown.  MPU API won't be
364  *                 available until a system restart.
365  *            - -EBUSY: MPU is busy with another request.
366  *            - -EINVAL: Problem with input parameters.
367  */
368 int nvi_mpu_data_out(int port, u8 data_out);
369
370 /**
371  * batch mode.
372  * @param port
373  * @param period_us
374  * @param timeout_us
375  * @return int error
376  *            Possible errors are:
377  *            - -EAGAIN: MPU is not initialized yet.
378  *            - -EPERM: MPU is shutdown.  MPU API won't be
379  *                 available until a system restart.
380  *            - -EBUSY: MPU is busy with another request.
381  *            - -EINVAL: timeout_us not supported if > 0.
382  */
383 int nvi_mpu_batch(int port, unsigned int period_us, unsigned int timeout_us);
384
385 /**
386  * batch flush.
387  * @param port
388  * @return int error
389  *            Possible errors are:
390  *            - -EAGAIN: MPU is not initialized yet.
391  *            - -EPERM: MPU is shutdown.  MPU API won't be
392  *                 available until a system restart.
393  *            - -EBUSY: MPU is busy with another request.
394  *            - -EINVAL: Problem with input parameters.
395  */
396 int nvi_mpu_flush(int port);
397
398 /**
399  * batch fifo.
400  * @param port
401  * @param reserve
402  * @param max
403  * @return int error
404  *            Possible errors are:
405  *            - -EAGAIN: MPU is not initialized yet.
406  *            - -EPERM: MPU is shutdown.  MPU API won't be
407  *                 available until a system restart.
408  *            - -EBUSY: MPU is busy with another request.
409  *            - -EINVAL: Problem with input parameters.
410  */
411 int nvi_mpu_fifo(int port, unsigned int *reserve, unsigned int *max);
412
413 /**
414  * Enable/disable the MPU bypass mode.  When enabled, the MPU
415  * will connect its auxiliary I2C ports to the host.  This is
416  * typically used to initialize a device that requires more I2C
417  * transactions than the automated port polling can offer.
418  * EVERY nvi_mpu_bypass_request call must be balanced with a
419  * nvi_mpu_bypass_release call!
420  * A bypass request does not need a following ~enable call.  The
421  * release call will automatically handle the correct bypass
422  * enable setting. The request locks the bypass setting if
423  * successful.  The release unlocks and restores the setting if
424  * need be.  Although odd, the purpose of the request call with
425  * the enable cleared to false is to allow an external driver to
426  * access its device that would normally conflict with a device
427  * behind the MPU.  Note that this call must not be a permanent
428  * solution, i.e. delayed or no release call.
429  * When the MPU is in a shutdown state the return error will be
430  * -EPERM and bypass will be enabled to allow access from the
431  * host to the devices connected to the MPU for their own
432  * shutdown needs.
433  * @param enable
434  * @return int error: calls that return with an error must not
435  *            be balanced with a release call.
436  *            Possible errors are:
437  *            - -EAGAIN: MPU is not initialized yet.
438  *            - -EPERM: MPU is shutdown.  MPU API won't be
439  *                 available until a system restart.
440  *            - -EBUSY: MPU is busy with another request.
441  */
442 int nvi_mpu_bypass_request(bool enable);
443
444 /**
445  * See the nvi_mpu_bypass_request notes.
446  * @return int 0: Always returns 0.  The call return should be
447  *         void but for backward compatibility it returns 0.
448  */
449 int nvi_mpu_bypass_release(void);
450
451 #endif  /* __MPU_H_ */