Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0-jb
[firefly-linux-kernel-4.4.55.git] / include / linux / mpu.h
1 /*
2  $License:
3     Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
4  $
5  */
6
7 #ifndef __MPU_H_
8 #define __MPU_H_
9
10 #ifdef __KERNEL__
11 #include <linux/types.h>
12 #include <linux/ioctl.h>
13 #elif defined LINUX
14 #include <sys/ioctl.h>
15 #include <linux/types.h>
16 #else
17 #include "mltypes.h"
18 #endif
19
20 /* Number of axes on each sensor */
21 #define GYRO_NUM_AXES               (3)
22 #define ACCEL_NUM_AXES              (3)
23 #define COMPASS_NUM_AXES            (3)
24
25 struct mpu_read_write {
26         /* Memory address or register address depending on ioctl */
27         __u16 address;
28         __u16 length;
29         __u8 *data;
30 };
31
32 enum mpuirq_data_type {
33         MPUIRQ_DATA_TYPE_MPU_IRQ,
34         MPUIRQ_DATA_TYPE_SLAVE_IRQ,
35         MPUIRQ_DATA_TYPE_PM_EVENT,
36         MPUIRQ_DATA_TYPE_NUM_TYPES,
37 };
38
39 /* User space PM event notification */
40 #define MPU_PM_EVENT_SUSPEND_PREPARE (3)
41 #define MPU_PM_EVENT_POST_SUSPEND    (4)
42
43 struct mpuirq_data {
44         __u32 interruptcount;
45         __u64 irqtime;
46         __u32 data_type;
47         __s32 data;
48 };
49
50 enum ext_slave_config_key {
51         MPU_SLAVE_CONFIG_ODR_SUSPEND,
52         MPU_SLAVE_CONFIG_ODR_RESUME,
53         MPU_SLAVE_CONFIG_FSR_SUSPEND,
54         MPU_SLAVE_CONFIG_FSR_RESUME,
55         MPU_SLAVE_CONFIG_MOT_THS,
56         MPU_SLAVE_CONFIG_NMOT_THS,
57         MPU_SLAVE_CONFIG_MOT_DUR,
58         MPU_SLAVE_CONFIG_NMOT_DUR,
59         MPU_SLAVE_CONFIG_IRQ_SUSPEND,
60         MPU_SLAVE_CONFIG_IRQ_RESUME,
61         MPU_SLAVE_WRITE_REGISTERS,
62         MPU_SLAVE_READ_REGISTERS,
63         MPU_SLAVE_CONFIG_INTERNAL_REFERENCE,
64         /* AMI 306 specific config keys */
65         MPU_SLAVE_PARAM,
66         MPU_SLAVE_WINDOW,
67         MPU_SLAVE_READWINPARAMS,
68         MPU_SLAVE_SEARCHOFFSET,
69         /* AKM specific config keys */
70         MPU_SLAVE_READ_SCALE,
71         /* MPU3050 and MPU6050 Keys */
72         MPU_SLAVE_INT_CONFIG,
73         MPU_SLAVE_EXT_SYNC,
74         MPU_SLAVE_FULL_SCALE,
75         MPU_SLAVE_LPF,
76         MPU_SLAVE_CLK_SRC,
77         MPU_SLAVE_DIVIDER,
78         MPU_SLAVE_DMP_ENABLE,
79         MPU_SLAVE_FIFO_ENABLE,
80         MPU_SLAVE_DMP_CFG1,
81         MPU_SLAVE_DMP_CFG2,
82         MPU_SLAVE_TC,
83         MPU_SLAVE_GYRO,
84         MPU_SLAVE_ADDR,
85         MPU_SLAVE_PRODUCT_REVISION,
86         MPU_SLAVE_SILICON_REVISION,
87         MPU_SLAVE_PRODUCT_ID,
88         MPU_SLAVE_GYRO_SENS_TRIM,
89         MPU_SLAVE_ACCEL_SENS_TRIM,
90         MPU_SLAVE_RAM,
91         /* -------------------------- */
92         MPU_SLAVE_CONFIG_NUM_CONFIG_KEYS
93 };
94
95 /* For the MPU_SLAVE_CONFIG_IRQ_SUSPEND and MPU_SLAVE_CONFIG_IRQ_RESUME */
96 enum ext_slave_config_irq_type {
97         MPU_SLAVE_IRQ_TYPE_NONE,
98         MPU_SLAVE_IRQ_TYPE_MOTION,
99         MPU_SLAVE_IRQ_TYPE_DATA_READY,
100 };
101
102 /* Structure for the following IOCTS's
103  * MPU_CONFIG_GYRO
104  * MPU_CONFIG_ACCEL
105  * MPU_CONFIG_COMPASS
106  * MPU_CONFIG_PRESSURE
107  * MPU_GET_CONFIG_GYRO
108  * MPU_GET_CONFIG_ACCEL
109  * MPU_GET_CONFIG_COMPASS
110  * MPU_GET_CONFIG_PRESSURE
111  *
112  * @key one of enum ext_slave_config_key
113  * @len length of data pointed to by data
114  * @apply zero if communication with the chip is not necessary, false otherwise
115  *        This flag can be used to select cached data or to refresh cashed data
116  *        cache data to be pushed later or push immediately.  If true and the
117  *        slave is on the secondary bus the MPU will first enger bypass mode
118  *        before calling the slaves .config or .get_config funcion
119  * @data pointer to the data to confgure or get
120  */
121 struct ext_slave_config {
122         __u8 key;
123         __u16 len;
124         __u8 apply;
125         void *data;
126 };
127
128 enum ext_slave_type {
129         EXT_SLAVE_TYPE_GYROSCOPE,
130         EXT_SLAVE_TYPE_ACCEL,
131         EXT_SLAVE_TYPE_COMPASS,
132         EXT_SLAVE_TYPE_PRESSURE,
133         /*EXT_SLAVE_TYPE_TEMPERATURE */
134
135         EXT_SLAVE_NUM_TYPES
136 };
137
138 enum ext_slave_id {
139         ID_INVALID = 0,
140
141         ACCEL_ID_LIS331,
142         ACCEL_ID_LSM303DLX,
143         ACCEL_ID_LIS3DH,
144         ACCEL_ID_KXSD9,
145         ACCEL_ID_KXTF9,
146         ACCEL_ID_BMA150,
147         ACCEL_ID_BMA222,
148         ACCEL_ID_BMA250,
149         ACCEL_ID_ADXL34X,
150         ACCEL_ID_MMA8450,
151         ACCEL_ID_MMA845X,
152         ACCEL_ID_MPU6050,
153
154         COMPASS_ID_AK8975,
155         COMPASS_ID_AK8972,
156         COMPASS_ID_AMI30X,
157         COMPASS_ID_AMI306,
158         COMPASS_ID_YAS529,
159         COMPASS_ID_YAS530,
160         COMPASS_ID_HMC5883,
161         COMPASS_ID_LSM303DLH,
162         COMPASS_ID_LSM303DLM,
163         COMPASS_ID_MMC314X,
164         COMPASS_ID_HSCDTD002B,
165         COMPASS_ID_HSCDTD004A,
166
167         PRESSURE_ID_BMA085,
168 };
169
170 enum ext_slave_endian {
171         EXT_SLAVE_BIG_ENDIAN,
172         EXT_SLAVE_LITTLE_ENDIAN,
173         EXT_SLAVE_FS8_BIG_ENDIAN,
174         EXT_SLAVE_FS16_BIG_ENDIAN,
175 };
176
177 enum ext_slave_bus {
178         EXT_SLAVE_BUS_INVALID = -1,
179         EXT_SLAVE_BUS_PRIMARY = 0,
180         EXT_SLAVE_BUS_SECONDARY = 1
181 };
182
183
184 /**
185  *  struct ext_slave_platform_data - Platform data for mpu3050 and mpu6050
186  *  slave devices
187  *
188  *  @type: the type of slave device based on the enum ext_slave_type
189  *         definitions.
190  *  @irq: the irq number attached to the slave if any.
191  *  @adapt_num: the I2C adapter number.
192  *  @bus: the bus the slave is attached to: enum ext_slave_bus
193  *  @address: the I2C slave address of the slave device.
194  *  @orientation: the mounting matrix of the device relative to MPU.
195  *  @irq_data: private data for the slave irq handler
196  *  @private_data: additional data, user customizable.  Not touched by the MPU
197  *                 driver.
198  *
199  * The orientation matricies are 3x3 rotation matricies
200  * that are applied to the data to rotate from the mounting orientation to the
201  * platform orientation.  The values must be one of 0, 1, or -1 and each row and
202  * column should have exactly 1 non-zero value.
203  */
204 struct ext_slave_platform_data {
205         __u8 type;
206         __u32 irq;
207         __u32 adapt_num;
208         __u32 bus;
209         __u8 address;
210         __s8 orientation[9];
211         void *irq_data;
212         void *private_data;
213 };
214
215 struct fix_pnt_range {
216         __s32 mantissa;
217         __s32 fraction;
218 };
219
220 static inline long range_fixedpoint_to_long_mg(struct fix_pnt_range rng)
221 {
222         return (long)(rng.mantissa * 1000 + rng.fraction / 10);
223 }
224
225 struct ext_slave_read_trigger {
226         __u8 reg;
227         __u8 value;
228 };
229
230 /**
231  *  struct ext_slave_descr - Description of the slave device for programming.
232  *
233  *  @suspend:   function pointer to put the device in suspended state
234  *  @resume:    function pointer to put the device in running state
235  *  @read:      function that reads the device data
236  *  @init:      function used to preallocate memory used by the driver
237  *  @exit:      function used to free memory allocated for the driver
238  *  @config:    function used to configure the device
239  *  @get_config:function used to get the device's configuration
240  *
241  *  @name:      text name of the device
242  *  @type:      device type. enum ext_slave_type
243  *  @id:        enum ext_slave_id
244  *  @read_reg:  starting register address to retrieve data.
245  *  @read_len:  length in bytes of the sensor data.  Typically  6.
246  *  @endian:    byte order of the data. enum ext_slave_endian
247  *  @range:     full scale range of the slave ouput: struct fix_pnt_range
248  *  @trigger:   If reading data first requires writing a register this is the
249  *              data to write.
250  *
251  *  Defines the functions and information about the slave the mpu3050 and
252  *  mpu6050 needs to use the slave device.
253  */
254 struct ext_slave_descr {
255         int (*init) (void *mlsl_handle,
256                      struct ext_slave_descr *slave,
257                      struct ext_slave_platform_data *pdata);
258         int (*exit) (void *mlsl_handle,
259                      struct ext_slave_descr *slave,
260                      struct ext_slave_platform_data *pdata);
261         int (*suspend) (void *mlsl_handle,
262                         struct ext_slave_descr *slave,
263                         struct ext_slave_platform_data *pdata);
264         int (*resume) (void *mlsl_handle,
265                        struct ext_slave_descr *slave,
266                        struct ext_slave_platform_data *pdata);
267         int (*read) (void *mlsl_handle,
268                      struct ext_slave_descr *slave,
269                      struct ext_slave_platform_data *pdata,
270                      __u8 *data);
271         int (*config) (void *mlsl_handle,
272                        struct ext_slave_descr *slave,
273                        struct ext_slave_platform_data *pdata,
274                        struct ext_slave_config *config);
275         int (*get_config) (void *mlsl_handle,
276                            struct ext_slave_descr *slave,
277                            struct ext_slave_platform_data *pdata,
278                            struct ext_slave_config *config);
279
280         char *name;
281         __u8 type;
282         __u8 id;
283         __u8 read_reg;
284         __u8 read_len;
285         __u8 endian;
286         struct fix_pnt_range range;
287         struct ext_slave_read_trigger *trigger;
288 };
289
290 /**
291  * struct mpu_platform_data - Platform data for the mpu driver
292  * @int_config:         Bits [7:3] of the int config register.
293  * @level_shifter:      0: VLogic, 1: VDD
294  * @orientation:        Orientation matrix of the gyroscope
295  *
296  * Contains platform specific information on how to configure the MPU3050 to
297  * work on this platform.  The orientation matricies are 3x3 rotation matricies
298  * that are applied to the data to rotate from the mounting orientation to the
299  * platform orientation.  The values must be one of 0, 1, or -1 and each row and
300  * column should have exactly 1 non-zero value.
301  */
302 struct mpu_platform_data {
303         __u8 int_config;
304         __u8 level_shifter;
305         __s8 orientation[GYRO_NUM_AXES * GYRO_NUM_AXES];
306 };
307
308 #if defined __KERNEL__ || defined LINUX
309 #define MPU_IOCTL (0x81) /* Magic number for MPU Iocts */
310 /* IOCTL commands for /dev/mpu */
311
312 /*--------------------------------------------------------------------------
313  * Deprecated, debugging only
314  */
315 #define MPU_SET_MPU_PLATFORM_DATA       \
316         _IOWR(MPU_IOCTL, 0x01, struct mpu_platform_data)
317 #define MPU_SET_EXT_SLAVE_PLATFORM_DATA \
318         _IOWR(MPU_IOCTL, 0x01, struct ext_slave_platform_data)
319 /*--------------------------------------------------------------------------*/
320 #define MPU_GET_EXT_SLAVE_PLATFORM_DATA \
321         _IOWR(MPU_IOCTL, 0x02, struct ext_slave_platform_data)
322 #define MPU_GET_MPU_PLATFORM_DATA       \
323         _IOWR(MPU_IOCTL, 0x02, struct mpu_platform_data)
324 #define MPU_GET_EXT_SLAVE_DESCR \
325         _IOWR(MPU_IOCTL, 0x02, struct ext_slave_descr)
326
327 #define MPU_READ                _IOWR(MPU_IOCTL, 0x10, struct mpu_read_write)
328 #define MPU_WRITE               _IOW(MPU_IOCTL,  0x10, struct mpu_read_write)
329 #define MPU_READ_MEM            _IOWR(MPU_IOCTL, 0x11, struct mpu_read_write)
330 #define MPU_WRITE_MEM           _IOW(MPU_IOCTL,  0x11, struct mpu_read_write)
331 #define MPU_READ_FIFO           _IOWR(MPU_IOCTL, 0x12, struct mpu_read_write)
332 #define MPU_WRITE_FIFO          _IOW(MPU_IOCTL,  0x12, struct mpu_read_write)
333
334 #define MPU_READ_COMPASS        _IOR(MPU_IOCTL, 0x12, __u8)
335 #define MPU_READ_ACCEL          _IOR(MPU_IOCTL, 0x13, __u8)
336 #define MPU_READ_PRESSURE       _IOR(MPU_IOCTL, 0x14, __u8)
337
338 #define MPU_CONFIG_GYRO         _IOW(MPU_IOCTL, 0x20, struct ext_slave_config)
339 #define MPU_CONFIG_ACCEL        _IOW(MPU_IOCTL, 0x21, struct ext_slave_config)
340 #define MPU_CONFIG_COMPASS      _IOW(MPU_IOCTL, 0x22, struct ext_slave_config)
341 #define MPU_CONFIG_PRESSURE     _IOW(MPU_IOCTL, 0x23, struct ext_slave_config)
342
343 #define MPU_GET_CONFIG_GYRO     _IOWR(MPU_IOCTL, 0x20, struct ext_slave_config)
344 #define MPU_GET_CONFIG_ACCEL    _IOWR(MPU_IOCTL, 0x21, struct ext_slave_config)
345 #define MPU_GET_CONFIG_COMPASS  _IOWR(MPU_IOCTL, 0x22, struct ext_slave_config)
346 #define MPU_GET_CONFIG_PRESSURE _IOWR(MPU_IOCTL, 0x23, struct ext_slave_config)
347
348 #define MPU_SUSPEND             _IOW(MPU_IOCTL, 0x30, __u32)
349 #define MPU_RESUME              _IOW(MPU_IOCTL, 0x31, __u32)
350 /* Userspace PM Event response */
351 #define MPU_PM_EVENT_HANDLED    _IO(MPU_IOCTL, 0x32)
352
353 #define MPU_GET_REQUESTED_SENSORS       _IOR(MPU_IOCTL, 0x40, __u8)
354 #define MPU_SET_REQUESTED_SENSORS       _IOW(MPU_IOCTL, 0x40, __u8)
355 #define MPU_GET_IGNORE_SYSTEM_SUSPEND   _IOR(MPU_IOCTL, 0x41, __u8)
356 #define MPU_SET_IGNORE_SYSTEM_SUSPEND   _IOW(MPU_IOCTL, 0x41, __u8)
357 #define MPU_GET_MLDL_STATUS             _IOR(MPU_IOCTL, 0x42, __u8)
358 #define MPU_GET_I2C_SLAVES_ENABLED      _IOR(MPU_IOCTL, 0x43, __u8)
359
360 #endif
361
362 #endif                          /* __MPU_H_ */