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 * - 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
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
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
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.
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.
184 struct nvi_mpu_port {
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);
195 signed char matrix[9];
196 enum secondary_slave_type type;
197 enum ext_slave_id id;
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 */
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.
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:
255 * - data_out if a write transaction
256 * @param *data: pointer for read data. Can be NULL if write.
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
269 int nvi_mpu_dev_valid(struct nvi_mpu_port *nmp, u8 *data);
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
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
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
300 * - -EINVAL: Problem with input parameters.
302 int nvi_mpu_port_alloc(struct nvi_mpu_port *nmp);
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.
315 int nvi_mpu_port_free(int port);
318 * Enable/disable ports. Use of a port mask (port_mask) allows
319 * enabling/disabling multiple ports at the same time.
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.
330 int nvi_mpu_enable(unsigned int port_mask, bool enable);
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.
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.
351 int nvi_mpu_delay_ms(int port, u8 delay_ms);
354 * Use to change the data written to the sensor.
356 * @param data_out is the new data to be written
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.
365 int nvi_mpu_data_out(int port, u8 data_out);
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.
380 int nvi_mpu_batch(int port, unsigned int period_us, unsigned int timeout_us);
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.
393 int nvi_mpu_flush(int port);
396 * nvi_mpu_info. MPU/ICM populates the nvi_mpu_inf structure
398 * @param port used for reading
399 * @param ptr to nvi_mpu_inf structure.
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.
408 int nvi_mpu_info(int read_port, struct nvi_mpu_inf *inf);
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
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.
439 int nvi_mpu_bypass_request(bool enable);
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.
446 int nvi_mpu_bypass_release(void);
448 #endif /* __MPU_H_ */