2 * Copyright (C) 2012 Invensense, Inc.
3 * Copyright (c) 2013-2016, NVIDIA CORPORATION. All rights reserved.
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.
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.
19 #include <linux/types.h>
20 #include <linux/ioctl.h>
23 /* Mount matrices for mount orientation.
25 * XXX : mount position. TOP for top and BOT for bottom.
26 * YYY : couter-clockwise rotation angle in degree.
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 }
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,
43 SECONDARY_SLAVE_TYPE_TYPES
51 GYRO_ID_MPU6050B1_NO_ACCEL,
78 COMPASS_ID_HSCDTD002B,
79 COMPASS_ID_HSCDTD004A,
86 ID_INVALID_END /* always last, leave auto-assigned */
89 #define INV_PROD_KEY(ver, rev) (ver * 100 + rev)
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)
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.
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.
115 struct mpu_platform_data {
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];
124 enum secondary_slave_type aux_slave_type;
125 enum ext_slave_id aux_slave_id;
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;
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
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
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
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
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
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.
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.
189 struct nvi_mpu_port {
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);
201 signed char matrix[9];
202 enum secondary_slave_type type;
203 enum ext_slave_id id;
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.
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:
250 * - data_out if a write transaction
251 * @param *data: pointer for read data. Can be NULL if write.
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
264 int nvi_mpu_dev_valid(struct nvi_mpu_port *nmp, u8 *data);
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
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
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
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
303 * - -EINVAL: Problem with input parameters.
305 int nvi_mpu_port_alloc(struct nvi_mpu_port *nmp, int port);
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.
318 int nvi_mpu_port_free(int port);
321 * Enable/disable ports. Use of a port mask (port_mask) allows
322 * enabling/disabling multiple ports at the same time.
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.
333 int nvi_mpu_enable(unsigned int port_mask, bool enable);
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.
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.
354 int nvi_mpu_delay_ms(int port, u8 delay_ms);
357 * Use to change the data written to the sensor.
359 * @param data_out is the new data to be written
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.
368 int nvi_mpu_data_out(int port, u8 data_out);
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.
383 int nvi_mpu_batch(int port, unsigned int period_us, unsigned int timeout_us);
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.
396 int nvi_mpu_flush(int port);
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.
411 int nvi_mpu_fifo(int port, unsigned int *reserve, unsigned int *max);
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
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.
442 int nvi_mpu_bypass_request(bool enable);
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.
449 int nvi_mpu_bypass_release(void);
451 #endif /* __MPU_H_ */