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/**
20 *  @addtogroup MLDL
21 *
22 *  @{
23 *      @file   mldl_cfg.h
24 *      @brief  The Motion Library Driver Layer Configuration header file.
25 */
26
27#ifndef __MLDL_CFG_H__
28#define __MLDL_CFG_H__
29
30#include "mltypes.h"
31#include "mlsl.h"
32#include <linux/mpu.h>
33#if defined CONFIG_MPU_SENSORS_MPU6050A2
34#    include "mpu6050a2.h"
35#elif defined CONFIG_MPU_SENSORS_MPU6050B1
36#    include "mpu6050b1.h"
37#elif defined CONFIG_MPU_SENSORS_MPU3050
38#  include "mpu3050.h"
39#else
40#error Invalid or undefined CONFIG_MPU_SENSORS_MPUxxxx
41#endif
42
43#include "log.h"
44
45/*************************************************************************
46 *  Sensors
47 *************************************************************************/
48
49#define INV_X_GYRO			(0x0001)
50#define INV_Y_GYRO			(0x0002)
51#define INV_Z_GYRO			(0x0004)
52#define INV_DMP_PROCESSOR		(0x0008)
53
54#define INV_X_ACCEL			(0x0010)
55#define INV_Y_ACCEL			(0x0020)
56#define INV_Z_ACCEL			(0x0040)
57
58#define INV_X_COMPASS			(0x0080)
59#define INV_Y_COMPASS			(0x0100)
60#define INV_Z_COMPASS			(0x0200)
61
62#define INV_X_PRESSURE			(0x0300)
63#define INV_Y_PRESSURE			(0x0800)
64#define INV_Z_PRESSURE			(0x1000)
65
66#define INV_TEMPERATURE			(0x2000)
67#define INV_TIME			(0x4000)
68
69#define INV_THREE_AXIS_GYRO		(0x000F)
70#define INV_THREE_AXIS_ACCEL		(0x0070)
71#define INV_THREE_AXIS_COMPASS		(0x0380)
72#define INV_THREE_AXIS_PRESSURE		(0x1C00)
73
74#define INV_FIVE_AXIS			(0x007B)
75#define INV_SIX_AXIS_GYRO_ACCEL		(0x007F)
76#define INV_SIX_AXIS_ACCEL_COMPASS	(0x03F0)
77#define INV_NINE_AXIS			(0x03FF)
78#define INV_ALL_SENSORS			(0x7FFF)
79
80#define MPL_PROD_KEY(ver, rev) (ver * 100 + rev)
81
82/* -------------------------------------------------------------------------- */
83
84/* Platform data for the MPU */
85struct mldl_cfg {
86	/* MPU related configuration */
87	unsigned long requested_sensors;
88	unsigned char ignore_system_suspend;
89	unsigned char addr;
90	unsigned char int_config;
91	unsigned char ext_sync;
92	unsigned char full_scale;
93	unsigned char lpf;
94	unsigned char clk_src;
95	unsigned char divider;
96	unsigned char dmp_enable;
97	unsigned char fifo_enable;
98	unsigned char dmp_cfg1;
99	unsigned char dmp_cfg2;
100	unsigned char offset_tc[GYRO_NUM_AXES];
101	unsigned short offset[GYRO_NUM_AXES];
102	unsigned char ram[MPU_MEM_NUM_RAM_BANKS][MPU_MEM_BANK_SIZE];
103
104	/* MPU Related stored status and info */
105	unsigned char product_revision;
106	unsigned char silicon_revision;
107	unsigned char product_id;
108	unsigned short gyro_sens_trim;
109#if defined CONFIG_MPU_SENSORS_MPU6050A2 || \
110	defined CONFIG_MPU_SENSORS_MPU6050B1
111	unsigned short accel_sens_trim;
112#endif
113
114	/* Driver/Kernel related state information */
115	int gyro_is_bypassed;
116	int i2c_slaves_enabled;
117	int dmp_is_running;
118	int gyro_is_suspended;
119	int accel_is_suspended;
120	int compass_is_suspended;
121	int pressure_is_suspended;
122	int gyro_needs_reset;
123
124	/* Slave related information */
125	struct ext_slave_descr *accel;
126	struct ext_slave_descr *compass;
127	struct ext_slave_descr *pressure;
128
129	/* Platform Data */
130	struct mpu_platform_data *pdata;
131};
132
133/* -------------------------------------------------------------------------- */
134
135int inv_mpu_open(struct mldl_cfg *mldl_cfg,
136		 void *mlsl_handle,
137		 void *accel_handle,
138		 void *compass_handle,
139		 void *pressure_handle);
140int inv_mpu_close(struct mldl_cfg *mldl_cfg,
141		  void *mlsl_handle,
142		  void *accel_handle,
143		  void *compass_handle,
144		  void *pressure_handle);
145int inv_mpu_resume(struct mldl_cfg *mldl_cfg,
146		   void *gyro_handle,
147		   void *accel_handle,
148		   void *compass_handle,
149		   void *pressure_handle,
150		   unsigned long sensors);
151int inv_mpu_suspend(struct mldl_cfg *mldl_cfg,
152		    void *gyro_handle,
153		    void *accel_handle,
154		    void *compass_handle,
155		    void *pressure_handle,
156		    unsigned long sensors);
157
158/* Slave Read functions */
159int inv_mpu_slave_read(struct mldl_cfg *mldl_cfg,
160		       void *gyro_handle,
161		       void *slave_handle,
162		       struct ext_slave_descr *slave,
163		       struct ext_slave_platform_data *pdata,
164		       unsigned char *data);
165static inline int inv_mpu_read_accel(struct mldl_cfg *mldl_cfg,
166				     void *gyro_handle,
167				     void *accel_handle, unsigned char *data)
168{
169	if (!mldl_cfg || !(mldl_cfg->pdata)) {
170		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
171		return INV_ERROR_INVALID_PARAMETER;
172	}
173
174	return inv_mpu_slave_read(mldl_cfg, gyro_handle, accel_handle,
175				  mldl_cfg->accel, &mldl_cfg->pdata->accel,
176				  data);
177}
178
179static inline int inv_mpu_read_compass(struct mldl_cfg *mldl_cfg,
180				       void *gyro_handle,
181				       void *compass_handle,
182				       unsigned char *data)
183{
184	if (!mldl_cfg || !(mldl_cfg->pdata)) {
185		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
186		return INV_ERROR_INVALID_PARAMETER;
187	}
188
189	return inv_mpu_slave_read(mldl_cfg, gyro_handle, compass_handle,
190				  mldl_cfg->compass, &mldl_cfg->pdata->compass,
191				  data);
192}
193
194static inline int inv_mpu_read_pressure(struct mldl_cfg *mldl_cfg,
195					void *gyro_handle,
196					void *pressure_handle,
197					unsigned char *data)
198{
199	if (!mldl_cfg || !(mldl_cfg->pdata)) {
200		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
201		return INV_ERROR_INVALID_PARAMETER;
202	}
203
204	return inv_mpu_slave_read(mldl_cfg, gyro_handle, pressure_handle,
205				  mldl_cfg->pressure,
206				  &mldl_cfg->pdata->pressure, data);
207}
208
209/* Slave Config functions */
210int inv_mpu_slave_config(struct mldl_cfg *mldl_cfg,
211			 void *gyro_handle,
212			 void *slave_handle,
213			 struct ext_slave_config *data,
214			 struct ext_slave_descr *slave,
215			 struct ext_slave_platform_data *pdata);
216static inline int inv_mpu_config_accel(struct mldl_cfg *mldl_cfg,
217				       void *gyro_handle,
218				       void *accel_handle,
219				       struct ext_slave_config *data)
220{
221	if (!mldl_cfg || !(mldl_cfg->pdata)) {
222		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
223		return INV_ERROR_INVALID_PARAMETER;
224	}
225
226	return inv_mpu_slave_config(mldl_cfg, gyro_handle, accel_handle, data,
227				    mldl_cfg->accel, &mldl_cfg->pdata->accel);
228}
229
230static inline int inv_mpu_config_compass(struct mldl_cfg *mldl_cfg,
231					 void *gyro_handle,
232					 void *compass_handle,
233					 struct ext_slave_config *data)
234{
235	if (!mldl_cfg || !(mldl_cfg->pdata)) {
236		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
237		return INV_ERROR_INVALID_PARAMETER;
238	}
239
240	return inv_mpu_slave_config(mldl_cfg, gyro_handle, compass_handle, data,
241				    mldl_cfg->compass,
242				    &mldl_cfg->pdata->compass);
243}
244
245static inline int inv_mpu_config_pressure(struct mldl_cfg *mldl_cfg,
246					  void *gyro_handle,
247					  void *pressure_handle,
248					  struct ext_slave_config *data)
249{
250	if (!mldl_cfg || !(mldl_cfg->pdata)) {
251		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
252		return INV_ERROR_INVALID_PARAMETER;
253	}
254
255	return inv_mpu_slave_config(mldl_cfg, gyro_handle, pressure_handle,
256				    data, mldl_cfg->pressure,
257				    &mldl_cfg->pdata->pressure);
258}
259
260/* Slave get config functions */
261int inv_mpu_get_slave_config(struct mldl_cfg *mldl_cfg,
262			     void *gyro_handle,
263			     void *slave_handle,
264			     struct ext_slave_config *data,
265			     struct ext_slave_descr *slave,
266			     struct ext_slave_platform_data *pdata);
267
268static inline int inv_mpu_get_accel_config(struct mldl_cfg *mldl_cfg,
269					   void *gyro_handle,
270					   void *accel_handle,
271					   struct ext_slave_config *data)
272{
273	if (!mldl_cfg || !(mldl_cfg->pdata)) {
274		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
275		return INV_ERROR_INVALID_PARAMETER;
276	}
277
278	return inv_mpu_get_slave_config(mldl_cfg, gyro_handle, accel_handle,
279					data, mldl_cfg->accel,
280					&mldl_cfg->pdata->accel);
281}
282
283static inline int inv_mpu_get_compass_config(struct mldl_cfg *mldl_cfg,
284					     void *gyro_handle,
285					     void *compass_handle,
286					     struct ext_slave_config *data)
287{
288	if (!mldl_cfg || !(mldl_cfg->pdata)) {
289		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
290		return INV_ERROR_INVALID_PARAMETER;
291	}
292
293	return inv_mpu_get_slave_config(mldl_cfg, gyro_handle, compass_handle,
294					data, mldl_cfg->compass,
295					&mldl_cfg->pdata->compass);
296}
297
298static inline int inv_mpu_get_pressure_config(struct mldl_cfg *mldl_cfg,
299					      void *gyro_handle,
300					      void *pressure_handle,
301					      struct ext_slave_config *data)
302{
303	if (!mldl_cfg || !(mldl_cfg->pdata)) {
304		LOG_RESULT_LOCATION(INV_ERROR_INVALID_PARAMETER);
305		return INV_ERROR_INVALID_PARAMETER;
306	}
307
308	return inv_mpu_get_slave_config(mldl_cfg, gyro_handle,
309					pressure_handle, data,
310					mldl_cfg->pressure,
311					&mldl_cfg->pdata->pressure);
312}
313
314/* -------------------------------------------------------------------------- */
315
316static inline long inv_mpu_get_sampling_rate_hz(struct mldl_cfg *mldl_cfg)
317{
318	if (((mldl_cfg->lpf) == 0) || ((mldl_cfg->lpf) == 7))
319		return 8000L / (mldl_cfg->divider + 1);
320	else
321		return 1000L / (mldl_cfg->divider + 1);
322}
323
324static inline long inv_mpu_get_sampling_period_us(struct mldl_cfg *mldl_cfg)
325{
326	if (((mldl_cfg->lpf) == 0) || ((mldl_cfg->lpf) == 7))
327		return (long) (1000000L * (mldl_cfg->divider + 1)) / 8000L;
328	else
329		return (long) (1000000L * (mldl_cfg->divider + 1)) / 1000L;
330}
331
332#endif				/* __MLDL_CFG_H__ */
333
334/**
335 *@}
336 */
337