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