blob: 026a0b7e3444836634444c7e255d0514edc93901 [file] [log] [blame]
Flemmardeefd2802014-03-08 10:03:28 +01001/*
2 $License:
3 Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 $
18 */
19
20#ifndef __MPU_H_
21#define __MPU_H_
22
23#ifdef __KERNEL__
24#include <linux/types.h>
25#endif
26
27#ifdef M_HW
28#include "mpu6000.h"
29#else
30#include "mpu3050.h"
31#endif
32
33/* Number of axes on each sensor */
34#define GYRO_NUM_AXES (3)
35#define ACCEL_NUM_AXES (3)
36#define COMPASS_NUM_AXES (3)
37
38/* Structure for the following IOCTL's:
39 MPU_READ
40 MPU_WRITE
41 MPU_READ_MEM
42 MPU_WRITE_MEM
43 MPU_READ_FIFO
44 MPU_WRITE_FIFO
45*/
46struct mpu_read_write {
47 /* Memory address or register address depending on ioctl */
48 unsigned short address;
49 unsigned short length;
50 unsigned char *data;
51};
52
53enum mpuirq_data_type {
54 MPUIRQ_DATA_TYPE_MPU_IRQ,
55 MPUIRQ_DATA_TYPE_SLAVE_IRQ,
56 MPUIRQ_DATA_TYPE_PM_EVENT,
57 MPUIRQ_DATA_TYPE_NUM_TYPES,
58};
59
60/* User space PM event notification */
61#define MPU_PM_EVENT_SUSPEND_PREPARE (3)
62#define MPU_PM_EVENT_POST_SUSPEND (4)
63
64struct mpuirq_data {
65 int interruptcount;
66 unsigned long long irqtime;
67 int data_type;
68 long data;
69};
70
71enum ext_slave_config_key {
72 MPU_SLAVE_CONFIG_ODR_SUSPEND,
73 MPU_SLAVE_CONFIG_ODR_RESUME,
74 MPU_SLAVE_CONFIG_FSR_SUSPEND,
75 MPU_SLAVE_CONFIG_FSR_RESUME,
76 MPU_SLAVE_CONFIG_MOT_THS,
77 MPU_SLAVE_CONFIG_NMOT_THS,
78 MPU_SLAVE_CONFIG_MOT_DUR,
79 MPU_SLAVE_CONFIG_NMOT_DUR,
80 MPU_SLAVE_CONFIG_IRQ_SUSPEND,
81 MPU_SLAVE_CONFIG_IRQ_RESUME,
82 MPU_SLAVE_WRITE_REGISTERS,
83 MPU_SLAVE_READ_REGISTERS,
84 /* AMI 306 specific config keys */
85 MPU_SLAVE_PARAM,
86 MPU_SLAVE_WINDOW,
87 MPU_SLAVE_READWINPARAMS,
88 MPU_SLAVE_SEARCHOFFSET,
89 /* AKM specific config keys */
90 MPU_SLAVE_READ_SCALE,
91 /* YAS specific config keys */
92 MPU_SLAVE_OFFSET_VALS,
93 MPU_SLAVE_RANGE_CHECK,
94
95 MPU_SLAVE_CONFIG_NUM_CONFIG_KEYS,
96};
97
98/* For the MPU_SLAVE_CONFIG_IRQ_SUSPEND and MPU_SLAVE_CONFIG_IRQ_RESUME */
99enum ext_slave_config_irq_type {
100 MPU_SLAVE_IRQ_TYPE_NONE,
101 MPU_SLAVE_IRQ_TYPE_MOTION,
102 MPU_SLAVE_IRQ_TYPE_DATA_READY,
103};
104
105/* Structure for the following IOCTS's
106 * MPU_CONFIG_ACCEL
107 * MPU_CONFIG_COMPASS
108 * MPU_CONFIG_PRESSURE
109 * MPU_GET_CONFIG_ACCEL
110 * MPU_GET_CONFIG_COMPASS
111 * MPU_GET_CONFIG_PRESSURE
112 *
113 * @key one of enum ext_slave_config_key
114 * @len length of data pointed to by data
115 * @apply zero if communication with the chip is not necessary, false otherwise
116 * This flag can be used to select cached data or to refresh cashed data
117 * cache data to be pushed later or push immediately. If true and the
118 * slave is on the secondary bus the MPU will first enger bypass mode
119 * before calling the slaves .config or .get_config funcion
120 * @data pointer to the data to confgure or get
121 */
122struct ext_slave_config {
123 int key;
124 int len;
125 int apply;
126 void *data;
127};
128
129enum ext_slave_type {
130 EXT_SLAVE_TYPE_GYROSCOPE,
131 EXT_SLAVE_TYPE_ACCELEROMETER,
132 EXT_SLAVE_TYPE_COMPASS,
133 EXT_SLAVE_TYPE_PRESSURE,
134 /*EXT_SLAVE_TYPE_TEMPERATURE */
135
136 EXT_SLAVE_NUM_TYPES
137};
138
139enum ext_slave_id {
140 ID_INVALID = 0,
141
142 ACCEL_ID_LIS331,
143 ACCEL_ID_LSM303,
144 ACCEL_ID_KXSD9,
145 ACCEL_ID_KXTF9,
146 ACCEL_ID_BMA150,
147 ACCEL_ID_BMA222,
148 ACCEL_ID_ADI346,
149 ACCEL_ID_MMA8450,
150 ACCEL_ID_MMA845X,
151 ACCEL_ID_MPU6000,
152 ACCEL_ID_LIS3DH,
153
154 COMPASS_ID_AKM,
155 COMPASS_ID_AMI30X,
156 COMPASS_ID_YAS529,
157 COMPASS_ID_HMC5883,
158 COMPASS_ID_LSM303,
159 COMPASS_ID_MMC314X,
160 COMPASS_ID_HSCDTD002B,
161 COMPASS_ID_HSCDTD004A,
162
163 PRESSURE_ID_BMA085,
164 ACCEL_ID_BMA250,
165};
166
167enum ext_slave_endian {
168 EXT_SLAVE_BIG_ENDIAN,
169 EXT_SLAVE_LITTLE_ENDIAN,
170 EXT_SLAVE_FS8_BIG_ENDIAN,
171 EXT_SLAVE_FS16_BIG_ENDIAN,
172};
173
174enum ext_slave_bus {
175 EXT_SLAVE_BUS_INVALID = -1,
176 EXT_SLAVE_BUS_PRIMARY = 0,
177 EXT_SLAVE_BUS_SECONDARY = 1
178};
179
180
181/**
182 * struct ext_slave_platform_data - Platform data for mpu3050 and mpu6050
183 * slave devices
184 *
185 * @get_slave_descr: Function pointer to retrieve the struct ext_slave_descr
186 * for this slave
187 * @irq: the irq number attached to the slave if any.
188 * @adapt_num: the I2C adapter number.
189 * @bus: the bus the slave is attached to: enum ext_slave_bus
190 * @address: the I2C slave address of the slave device.
191 * @orientation: the mounting matrix of the device relative to MPU.
192 * @irq_data: private data for the slave irq handler
193 * @private_data: additional data, user customizable. Not touched by the MPU
194 * driver.
195 *
196 * The orientation matricies are 3x3 rotation matricies
197 * that are applied to the data to rotate from the mounting orientation to the
198 * platform orientation. The values must be one of 0, 1, or -1 and each row and
199 * column should have exactly 1 non-zero value.
200 */
201struct ext_slave_platform_data {
202 struct ext_slave_descr *(*get_slave_descr) (void);
203 int irq;
204 int adapt_num;
205 int bus;
206 unsigned char address;
207 signed char orientation[9];
208 void *irq_data;
209 void *private_data;
210 void (*bypass_state)(int, char*);
211 int (*check_sleep_status)(void);
212 void (*vote_sleep_status)(int, int);
213
214};
215
216struct fix_pnt_range {
217 long mantissa;
218 long fraction;
219};
220
221static inline long range_fixedpoint_to_long_mg(struct fix_pnt_range rng)
222{
223 return (long)(rng.mantissa * 1000 + rng.fraction / 10);
224}
225
226struct ext_slave_read_trigger {
227 unsigned char reg;
228 unsigned char value;
229};
230
231/**
232 * struct ext_slave_descr - Description of the slave device for programming.
233 *
234 * @suspend: function pointer to put the device in suspended state
235 * @resume: function pointer to put the device in running state
236 * @read: function that reads the device data
237 * @init: function used to preallocate memory used by the driver
238 * @exit: function used to free memory allocated for the driver
239 * @config: function used to configure the device
240 * @get_config:function used to get the device's configuration
241 *
242 * @name: text name of the device
243 * @type: device type. enum ext_slave_type
244 * @id: enum ext_slave_id
245 * @reg: starting register address to retrieve data.
246 * @len: length in bytes of the sensor data. Should be 6.
247 * @endian: byte order of the data. enum ext_slave_endian
248 * @range: full scale range of the slave ouput: struct tFixPntRange
249 *
250 * Defines the functions and information about the slave the mpu3050 needs to
251 * use the slave device.
252 */
253struct ext_slave_descr {
254 int (*init) (void *mlsl_handle,
255 struct ext_slave_descr *slave,
256#ifdef CONFIG_CIR_ALWAYS_READY
257 struct ext_slave_platform_data *pdata,
258 int (*power_LPM)(int on)
259 );
260#else
261 struct ext_slave_platform_data *pdata);
262#endif
263
264 int (*exit) (void *mlsl_handle,
265 struct ext_slave_descr *slave,
266 struct ext_slave_platform_data *pdata);
267 int (*suspend) (void *mlsl_handle,
268 struct ext_slave_descr *slave,
269 struct ext_slave_platform_data *pdata);
270 int (*resume) (void *mlsl_handle,
271 struct ext_slave_descr *slave,
272 struct ext_slave_platform_data *pdata);
273 int (*read) (void *mlsl_handle,
274 struct ext_slave_descr *slave,
275 struct ext_slave_platform_data *pdata,
276 unsigned char *data);
277 int (*config) (void *mlsl_handle,
278 struct ext_slave_descr *slave,
279 struct ext_slave_platform_data *pdata,
280 struct ext_slave_config *config);
281 int (*get_config) (void *mlsl_handle,
282 struct ext_slave_descr *slave,
283 struct ext_slave_platform_data *pdata,
284 struct ext_slave_config *config);
285
286 char *name;
287 unsigned char type;
288 unsigned char id;
289 unsigned char reg;
290 unsigned int len;
291 unsigned char endian;
292 struct fix_pnt_range range;
293 struct ext_slave_read_trigger *trigger;
294};
295
296/**
297 * struct mpu3050_platform_data - Platform data for the mpu driver
298 * @int_config: Bits [7:3] of the int config register.
299 * @orientation: Orientation matrix of the gyroscope
300 * @level_shifter: 0: VLogic, 1: VDD
301 * @accel: Accel platform data
302 * @compass: Compass platform data
303 * @pressure: Pressure platform data
304 *
305 * Contains platform specific information on how to configure the MPU3050 to
306 * work on this platform. The orientation matricies are 3x3 rotation matricies
307 * that are applied to the data to rotate from the mounting orientation to the
308 * platform orientation. The values must be one of 0, 1, or -1 and each row and
309 * column should have exactly 1 non-zero value.
310 */
311struct mpu3050_platform_data {
312 unsigned char int_config;
313 signed char orientation[GYRO_NUM_AXES * GYRO_NUM_AXES];
314 unsigned char level_shifter;
315 struct ext_slave_platform_data accel;
316 struct ext_slave_platform_data compass;
317 struct ext_slave_platform_data pressure;
318 int (*setup)(struct device *dev, int enable);
319 void (*hw_config)(int enable);
320 void (*power_mode)(int enable);
321};
322
323
324/*
325 Accelerometer
326*/
327#define get_accel_slave_descr NULL
328
329#ifdef CONFIG_MPU_SENSORS_ADXL346 /* ADI accelerometer */
330struct ext_slave_descr *adxl346_get_slave_descr(void);
331#undef get_accel_slave_descr
332#define get_accel_slave_descr adxl346_get_slave_descr
333#endif
334
335#ifdef CONFIG_MPU_SENSORS_BMA150 /* Bosch accelerometer */
336struct ext_slave_descr *bma150_get_slave_descr(void);
337#undef get_accel_slave_descr
338#define get_accel_slave_descr bma150_get_slave_descr
339#endif
340
341#ifdef CONFIG_MPU_SENSORS_BMA250 /* Bosch 250 accelerometer */
342struct ext_slave_descr *bma250_get_slave_descr(void);
343#undef get_accel_slave_descr
344#define get_accel_slave_descr bma250_get_slave_descr
345#endif
346
347#ifdef CONFIG_MPU_SENSORS_BMA222 /* Bosch 222 accelerometer */
348struct ext_slave_descr *bma222_get_slave_descr(void);
349#undef get_accel_slave_descr
350#define get_accel_slave_descr bma222_get_slave_descr
351#endif
352
353#ifdef CONFIG_MPU_SENSORS_KXSD9 /* Kionix accelerometer */
354struct ext_slave_descr *kxsd9_get_slave_descr(void);
355#undef get_accel_slave_descr
356#define get_accel_slave_descr kxsd9_get_slave_descr
357#endif
358
359#ifdef CONFIG_MPU_SENSORS_KXTF9 /* Kionix accelerometer */
360struct ext_slave_descr *kxtf9_get_slave_descr(void);
361#undef get_accel_slave_descr
362#define get_accel_slave_descr kxtf9_get_slave_descr
363#endif
364
365#ifdef CONFIG_MPU_SENSORS_LIS331DLH /* ST accelerometer */
366struct ext_slave_descr *lis331dlh_get_slave_descr(void);
367#undef get_accel_slave_descr
368#define get_accel_slave_descr lis331dlh_get_slave_descr
369#endif
370
371
372#ifdef CONFIG_MPU_SENSORS_LIS3DH /* ST accelerometer */
373struct ext_slave_descr *lis3dh_get_slave_descr(void);
374#undef get_accel_slave_descr
375#define get_accel_slave_descr lis3dh_get_slave_descr
376#endif
377
378#ifdef CONFIG_MPU_SENSORS_LSM303DLHA /* ST accelerometer */
379struct ext_slave_descr *lsm303dlha_get_slave_descr(void);
380#undef get_accel_slave_descr
381#define get_accel_slave_descr lsm303dlha_get_slave_descr
382#endif
383
384/* MPU6000 Accel */
385#if defined(CONFIG_MPU_SENSORS_MPU6000) || \
386 defined(CONFIG_MPU_SENSORS_MPU6000_MODULE)
387struct ext_slave_descr *mantis_get_slave_descr(void);
388#undef get_accel_slave_descr
389#define get_accel_slave_descr mantis_get_slave_descr
390#endif
391
392#ifdef CONFIG_MPU_SENSORS_MMA8450 /* Freescale accelerometer */
393struct ext_slave_descr *mma8450_get_slave_descr(void);
394#undef get_accel_slave_descr
395#define get_accel_slave_descr mma8450_get_slave_descr
396#endif
397
398#ifdef CONFIG_MPU_SENSORS_MMA845X /* Freescale accelerometer */
399struct ext_slave_descr *mma845x_get_slave_descr(void);
400#undef get_accel_slave_descr
401#define get_accel_slave_descr mma845x_get_slave_descr
402#endif
403
404
405/*
406 Compass
407*/
408#define get_compass_slave_descr NULL
409
410#ifdef CONFIG_MPU_SENSORS_AK8975 /* AKM compass */
411struct ext_slave_descr *ak8975_get_slave_descr(void);
412#undef get_compass_slave_descr
413#define get_compass_slave_descr ak8975_get_slave_descr
414#endif
415
416#ifdef CONFIG_MPU_SENSORS_AMI30X /* AICHI Steel compass */
417struct ext_slave_descr *ami30x_get_slave_descr(void);
418#undef get_compass_slave_descr
419#define get_compass_slave_descr ami30x_get_slave_descr
420#endif
421
422#ifdef CONFIG_MPU_SENSORS_HMC5883 /* Honeywell compass */
423struct ext_slave_descr *hmc5883_get_slave_descr(void);
424#undef get_compass_slave_descr
425#define get_compass_slave_descr hmc5883_get_slave_descr
426#endif
427
428#ifdef CONFIG_MPU_SENSORS_MMC314X /* MEMSIC compass */
429struct ext_slave_descr *mmc314x_get_slave_descr(void);
430#undef get_compass_slave_descr
431#define get_compass_slave_descr mmc314x_get_slave_descr
432#endif
433
434#ifdef CONFIG_MPU_SENSORS_LSM303DLHM /* ST compass */
435struct ext_slave_descr *lsm303dlhm_get_slave_descr(void);
436#undef get_compass_slave_descr
437#define get_compass_slave_descr lsm303dlhm_get_slave_descr
438#endif
439
440#ifdef CONFIG_MPU_SENSORS_YAS529 /* Yamaha compass */
441struct ext_slave_descr *yas529_get_slave_descr(void);
442#undef get_compass_slave_descr
443#define get_compass_slave_descr yas529_get_slave_descr
444#endif
445
446#ifdef CONFIG_MPU_SENSORS_HSCDTD002B /* Alps HSCDTD002B compass */
447struct ext_slave_descr *hscdtd002b_get_slave_descr(void);
448#undef get_compass_slave_descr
449#define get_compass_slave_descr hscdtd002b_get_slave_descr
450#endif
451
452#ifdef CONFIG_MPU_SENSORS_HSCDTD004A /* Alps HSCDTD004A compass */
453struct ext_slave_descr *hscdtd004a_get_slave_descr(void);
454#undef get_compass_slave_descr
455#define get_compass_slave_descr hscdtd004a_get_slave_descr
456#endif
457/*
458 Pressure
459*/
460#define get_pressure_slave_descr NULL
461
462#ifdef CONFIG_MPU_SENSORS_BMA085 /* BMA pressure */
463struct ext_slave_descr *bma085_get_slave_descr(void);
464#undef get_pressure_slave_descr
465#define get_pressure_slave_descr bma085_get_slave_descr
466#endif
467
468#define MPU_IOCTL (0x81) /* Magic number for MPU Iocts */
469/* IOCTL commands for /dev/mpu */
470#define MPU_SET_MPU_CONFIG _IOWR(MPU_IOCTL, 0x00, struct mldl_cfg)
471#define MPU_GET_MPU_CONFIG _IOW(MPU_IOCTL, 0x00, struct mldl_cfg)
472
473#define MPU_SET_PLATFORM_DATA _IOWR(MPU_IOCTL, 0x01, struct mldl_cfg)
474
475#define MPU_READ _IOWR(MPU_IOCTL, 0x10, struct mpu_read_write)
476#define MPU_WRITE _IOW(MPU_IOCTL, 0x10, struct mpu_read_write)
477#define MPU_READ_MEM _IOWR(MPU_IOCTL, 0x11, struct mpu_read_write)
478#define MPU_WRITE_MEM _IOW(MPU_IOCTL, 0x11, struct mpu_read_write)
479#define MPU_READ_FIFO _IOWR(MPU_IOCTL, 0x12, struct mpu_read_write)
480#define MPU_WRITE_FIFO _IOW(MPU_IOCTL, 0x12, struct mpu_read_write)
481
482#define MPU_READ_COMPASS _IOR(MPU_IOCTL, 0x12, unsigned char)
483#define MPU_READ_ACCEL _IOR(MPU_IOCTL, 0x13, unsigned char)
484#define MPU_READ_PRESSURE _IOR(MPU_IOCTL, 0x14, unsigned char)
485
486#define MPU_CONFIG_ACCEL _IOW(MPU_IOCTL, 0x20, struct ext_slave_config)
487#define MPU_CONFIG_COMPASS _IOW(MPU_IOCTL, 0x21, struct ext_slave_config)
488#define MPU_CONFIG_PRESSURE _IOW(MPU_IOCTL, 0x22, struct ext_slave_config)
489
490#define MPU_GET_CONFIG_ACCEL _IOWR(MPU_IOCTL, 0x20, struct ext_slave_config)
491#define MPU_GET_CONFIG_COMPASS _IOWR(MPU_IOCTL, 0x21, struct ext_slave_config)
492#define MPU_GET_CONFIG_PRESSURE _IOWR(MPU_IOCTL, 0x22, struct ext_slave_config)
493
494#define MPU_SUSPEND _IO(MPU_IOCTL, 0x30)
495#define MPU_RESUME _IO(MPU_IOCTL, 0x31)
496/* Userspace PM Event response */
497#define MPU_PM_EVENT_HANDLED _IO(MPU_IOCTL, 0x32)
498
499#endif /* __MPU_H_ */