summaryrefslogtreecommitdiffstats
path: root/include/linux/mpu_iio.h
blob: 4828477f64aa1a56a12222719732e478aa568e3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (C) 2012 Invensense, Inc.
* Copyright (c) 2013-2018, NVIDIA CORPORATION.  All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

#ifndef __MPU_H_
#define __MPU_H_

#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/ioctl.h>
#endif

/* Mount matrices for mount orientation.
 * MTMAT_XXX_CCW_YYY
 *     XXX : mount position. TOP for top and BOT for bottom.
 *     YYY : couter-clockwise rotation angle in degree.
 */
#define MTMAT_TOP_CCW_0		{  1,  0,  0,  0,  1,  0,  0,  0,  1 }
#define MTMAT_TOP_CCW_90	{  0, -1,  0,  1,  0,  0,  0,  0,  1 }
#define MTMAT_TOP_CCW_180	{ -1,  0,  0,  0, -1,  0,  0,  0,  1 }
#define MTMAT_TOP_CCW_270	{  0,  1,  0, -1,  0,  0,  0,  0,  1 }
#define MTMAT_BOT_CCW_0		{ -1,  0,  0,  0,  1,  0,  0,  0, -1 }
#define MTMAT_BOT_CCW_90	{  0, -1,  0, -1,  0,  0,  0,  0, -1 }
#define MTMAT_BOT_CCW_180	{  1,  0,  0,  0, -1,  0,  0,  0, -1 }
#define MTMAT_BOT_CCW_270	{  0,  1,  0,  1,  0,  0,  0,  0, -1 }

enum secondary_slave_type {
	SECONDARY_SLAVE_TYPE_NONE,
	SECONDARY_SLAVE_TYPE_ACCEL,
	SECONDARY_SLAVE_TYPE_COMPASS,
	SECONDARY_SLAVE_TYPE_PRESSURE,

	SECONDARY_SLAVE_TYPE_TYPES
};

enum ext_slave_id {
	ID_INVALID = 0,
	GYRO_ID_MPU3050,
	GYRO_ID_MPU6050A2,
	GYRO_ID_MPU6050B1,
	GYRO_ID_MPU6050B1_NO_ACCEL,
	GYRO_ID_ITG3500,

	ACCEL_ID_LIS331,
	ACCEL_ID_LSM303DLX,
	ACCEL_ID_LIS3DH,
	ACCEL_ID_KXSD9,
	ACCEL_ID_KXTF9,
	ACCEL_ID_BMA150,
	ACCEL_ID_BMA222,
	ACCEL_ID_BMA250,
	ACCEL_ID_ADXL34X,
	ACCEL_ID_MMA8450,
	ACCEL_ID_MMA845X,
	ACCEL_ID_MPU6050,

	COMPASS_ID_AK8963,
	COMPASS_ID_AK8975,
	COMPASS_ID_AK8972,
	COMPASS_ID_AMI30X,
	COMPASS_ID_AMI306,
	COMPASS_ID_YAS529,
	COMPASS_ID_YAS530,
	COMPASS_ID_HMC5883,
	COMPASS_ID_LSM303DLH,
	COMPASS_ID_LSM303DLM,
	COMPASS_ID_MMC314X,
	COMPASS_ID_HSCDTD002B,
	COMPASS_ID_HSCDTD004A,
	COMPASS_ID_MLX90399,
	COMPASS_ID_AK09911,

	PRESSURE_ID_BMP085,
	PRESSURE_ID_BMP280,

	ID_INVALID_END /* always last, leave auto-assigned */
};

#define INV_PROD_KEY(ver, rev) (ver * 100 + rev)

#define NVI_CONFIG_BOOT_AUTO		(0) /* auto detect connection to MPU */
#define NVI_CONFIG_BOOT_MPU		(1) /* connected to MPU */
#define NVI_CONFIG_BOOT_HOST		(2) /* connected to host */
#define NVI_CONFIG_BOOT_MASK		(0x03)

/**
 * struct mpu_platform_data - Platform data for the mpu driver
 * @int_config:		Bits [7:3] of the int config register.
 * @level_shifter:	0: VLogic, 1: VDD
 * @orientation:	Orientation matrix of the gyroscope
 * @sec_slave_type:     secondary slave device type, can be compass, accel, etc
 * @sec_slave_id:       id of the secondary slave device
 * @secondary_i2c_address: secondary device's i2c address
 * @secondary_orientation: secondary device's orientation matrix
 * @key:                key for MPL library.
 * @nvi_config: the selection determines the device behavior.
 *              Select from the NVI_CONFIG_BOOT_ defines.
 *
 * Contains platform specific information on how to configure the MPU3050 to
 * work on this platform.  The orientation matricies are 3x3 rotation matricies
 * that are applied to the data to rotate from the mounting orientation to the
 * platform orientation.  The values must be one of 0, 1, or -1 and each row and
 * column should have exactly 1 non-zero value.
 */
struct mpu_platform_data {
	__u8 int_config;
	__u8 level_shifter;
	__s8 orientation[9];
	enum secondary_slave_type sec_slave_type;
	enum ext_slave_id sec_slave_id;
	__u16 secondary_i2c_addr;
	__s8 secondary_orientation[9];
	__u8 key[16];
	enum secondary_slave_type aux_slave_type;
	enum ext_slave_id aux_slave_id;
	__u16 aux_i2c_addr;

#ifdef CONFIG_DTS_INV_MPU_IIO
	int (*power_on)(struct mpu_platform_data *);
	int (*power_off)(struct mpu_platform_data *);
	struct regulator *vdd_ana;
	struct regulator *vdd_i2c;
#endif
	__u8 nvi_config;
};


/**
 * struct nvi_mpu_port: Allows an external driver to use the MPU
 *    auxiliary I2C master by providing the details for the I2C
 *    polling of a device connected to the MPU.
 * - addr: The 6:0 I2C address of the device connected to
 *      the MPU.
 *      7:7 = 0 if the port is to do write transactions.
 *          = 1 if the port is to do read transactions.
 * - reg: The device register the I2C transaction will
 *      use.
 * - ctrl: The number of consecutive registers to read in
 *      3:0. If the port is to do write transactions then this
 *      value must be 1.  See MPU documentation for the other
 *      bits in I2C_SLVx_CTRL that can be applied by this byte.
 * - data_out: The data byte written if the port is configured
 *      to do writes (addr 7:7 = 0).
 * - delay_ms: The polling delay time between I2C transactions
 *      in ms.  Note that the MPU HW only supports one delay
 *      time so the longest delay of all the MPU ports enabled
 *      is used.
 * - period_us: The period at which the read data is reported.
 * - shutdown_bypass: set if a connection to the host is needed
 *      when the system is shutdown.  The MPU API will be
 *      disabled as part of its shutdown but it will enable the
 *      bypass if this is true.
 * - *handler: The pointer to the function called when the data
 *      is available.  This can be NULL if the port is
 *      configured for write transactions.
 *      The function is called with the following parameters:
 *      - *data: The pointer to the data to read.
 *      - length: The number of bytes to be read (same value as
 *           length above).
 *      - timestamp: The timestamp of when the data was polled.
 *      - *pointer: A generic pointer defined next below.  Note
 *           that this can be NULL if this will be a write I2C
 *           transaction.
 * - *ext_driver: A generic pointer that can be used by the
 *      external driver.  Note that this is specifically for the
 *      external driver and not used by the MPU.
 * - matrix: device orientation on platform.
 *      Needed by the DMP.
 * - type: Define if device is to be used by the MPU DMP.
 * - id: Define if device is to be used by the MPU DMP.
 * - asa: compass axis sensitivity adjustment.
 *      Needed by the DMP.
 */
struct nvi_mpu_port {
	u8 addr;
	u8 reg;
	u8 ctrl;
	u8 data_out;
	unsigned int delay_ms;
	unsigned int period_us;
	bool shutdown_bypass;
	void (*handler)(u8 *data, unsigned int len,
			long long timestamp, void *ext_driver);
	void *ext_driver;
	signed char matrix[9];
	enum secondary_slave_type type;
	enum ext_slave_id id;
	u64 q30[3];
};

struct nvi_mpu_inf {
	unsigned int period_us_min;
	unsigned int period_us_max;
	unsigned int fifo_reserve;
	unsigned int fifo_max;
	unsigned int dmp_rd_len_sts;	/* status length from DMP */
	unsigned int dmp_rd_len_data;	/* data length from DMP */
	bool dmp_rd_be_sts;		/* status endian from DMP */
	bool dmp_rd_be_data;		/* data endian from DMP */
};

/**
 * Expected use of the nvi_mpu_ routines are as follows:
 * - nvi_mpu_dev_valid: Use to validate whether a device is
 *      connected to the MPU.
 * - nvi_mpu_port_alloc: Request a connection to the device.  If
 *      successful, a port number will be returned to identify
 *      the connection.  The port number is then used for all
 *      further communication with the connection.
 * - nvi_mpu_port_free: Use to close the port connection.
 * - nvi_mpu_enable: Use to enable/disable a port.
 *      The enable and FIFO enable is disabled by default so
 *      this will be required after a port is assigned.
 * - nvi_mpu_delay_us: Use to set the sampling rate in
 *      microseconds.  The fastest rate of all the enabled MPU
 *      devices will be used that does not exceed the
 *      nvi_mpu_delay_ms setting of an enabled device.
 * - nvi_mpu_delay_ms: Use to change the port polling delay at
 *      runtime. There is only one HW delay so the delay used
 *      will be the longest delay of all the enabled ports.
 *      This is separate from the sampling rate
 *      (nvi_mpu_delay_us).  See function notes below.
 * - nvi_mpu_data_out: Use to change the data written at runtime
 *      for ports that are configured as I2C write transactions.
 * - nvi_mpu_bypass request/release: Use to connect/disconnect
 *      the MPU host from the device.  When bypass is enabled,
 *      the connection from the device to the MPU will then be
 *      connected to the host (that the MPU is connected to).
 *      This is a global connection switch affecting all ports
 *      so a mechanism is in place of whether the request is
 *      honored or not.  See the function notes for
 *      nvi_mpu_bypass_request.
 */

/**
 * Use to validate a device connected to the MPU I2C master.
 * The function works by doing a single byte read or write to
 * the device and detecting a NACK.  Typically, the call would
 * be set up to read a byte ID of the device.
 * @param struct nvi_mpu_port *nmp
 *           Only the following is needed in nmp:
 *           - addr
 *           - reg
 *           - ctrl
 *           - data_out if a write transaction
 * @param *data: pointer for read data.  Can be NULL if write.
 * @return int error
 *            Possible return value or errors are:
 *            - 0: device is connected to MPU.
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -ENODEV: The device is not connected to the MPU.
 *            - -EINVAL: Problem with input parameters.
 *            - -EIO: The device is connected but responded with
 *                 a NACK.
 */
int nvi_mpu_dev_valid(struct nvi_mpu_port *nmp, u8 *data);

/**
 * Request a port.
 * @param struct nvi_mpu_port *nmp
 *           - addr: device I2C address 6:0.
 *                7:7 = 0 if the port is to do writes.
 *                7:7 = 1 if the port is to do reads.
 *           - reg: the starting register to write or read.
 *           - ctrl: number of bytes to read.  Use 1 if port
 *                is configured to do writes.
 *           - data_out: only valid if port is configured to do
 *                writes.
 *           - delay: polling delay
 *           - handler: function to call when data is read. This
 *                should be NULL if the port is configured to do
 *                writes.
 *           - ext_driver: this pointer is passed in handler for
 *                use by external driver.  This should be NULL
 *                if the port is configured for writes.
 * @return int error/port ID
 *            if return >= 0 then this is the port ID.  The ID
 *            will have a value of 0 to 3 (HW has 4 ports).
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -ENODEV: A port is not available.  The only way
 *                 to resolve this error is for a port to be
 *                 freed.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_port_alloc(struct nvi_mpu_port *nmp);

/**
 * Remove a port.
 * @param port
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_port_free(int port);

/**
 * Enable/disable ports.  Use of a port mask (port_mask) allows
 * enabling/disabling multiple ports at the same time.
 * @param port_mask
 * @param enable
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_enable(unsigned int port_mask, bool enable);

/**
 * Use to change the ports polling delay in milliseconds.
 * A delay value of 0 disables the delay for that port.  The
 * hardware only supports one delay value so the largest request
 * of all the enabled ports is used. The polling delay is in
 * addition to the sampling delay (nvi_mpu_delay_us).  This is
 * typically used to guarantee a delay after an I2C write to a
 * device to allow the device to process the request and be read
 * by another port before another write at the sampling delay.
 * @param port
 * @param delay_ms
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_delay_ms(int port, u8 delay_ms);

/**
 * Use to change the data written to the sensor.
 * @param port
 * @param data_out is the new data to be written
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_data_out(int port, u8 data_out);

/**
 * batch mode.
 * @param port
 * @param period_us
 * @param timeout_us
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: timeout_us not supported if > 0.
 */
int nvi_mpu_batch(int port, unsigned int period_us, unsigned int timeout_us);

/**
 * batch read.
 * @param port
 * @param period_us pointer
 * @param timeout_us pointer
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: timeout_us not supported if > 0.
 */
int nvi_mpu_batch_read(int port,
		       unsigned int *period_us, unsigned int *timeout_us);

/**
 * batch flush.
 * @param port
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_flush(int port);

/**
 * nvi_mpu_info.  MPU/ICM populates the nvi_mpu_inf structure
 * pointed to by inf.
 * @param port used for reading
 * @param ptr to nvi_mpu_inf structure.
 * @return int error
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 *            - -EINVAL: Problem with input parameters.
 */
int nvi_mpu_info(int read_port, struct nvi_mpu_inf *inf);

/**
 * Enable/disable the MPU bypass mode.  When enabled, the MPU
 * will connect its auxiliary I2C ports to the host.  This is
 * typically used to initialize a device that requires more I2C
 * transactions than the automated port polling can offer.
 * EVERY nvi_mpu_bypass_request call must be balanced with a
 * nvi_mpu_bypass_release call!
 * A bypass request does not need a following ~enable call.  The
 * release call will automatically handle the correct bypass
 * enable setting. The request locks the bypass setting if
 * successful.  The release unlocks and restores the setting if
 * need be.  Although odd, the purpose of the request call with
 * the enable cleared to false is to allow an external driver to
 * access its device that would normally conflict with a device
 * behind the MPU.  Note that this call must not be a permanent
 * solution, i.e. delayed or no release call.
 * When the MPU is in a shutdown state the return error will be
 * -EPERM and bypass will be enabled to allow access from the
 * host to the devices connected to the MPU for their own
 * shutdown needs.
 * @param enable
 * @return int error: calls that return with an error must not
 *            be balanced with a release call.
 *            Possible errors are:
 *            - -EAGAIN: MPU is not initialized yet.
 *            - -EPERM: MPU is shutdown.  MPU API won't be
 *                 available until a system restart.
 *            - -EBUSY: MPU is busy with another request.
 */
int nvi_mpu_bypass_request(bool enable);

/**
 * See the nvi_mpu_bypass_request notes.
 * @return int 0: Always returns 0.  The call return should be
 *         void but for backward compatibility it returns 0.
 */
int nvi_mpu_bypass_release(void);

#endif	/* __MPU_H_ */