1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright (C) 2012 Invensense, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/sysfs.h>
#include <linux/jiffies.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/kfifo.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include "inv_mpu_iio.h"
#define MPU3050_NACK_MIN_TIME (2 * 1000)
#define MPU3050_NACK_MAX_TIME (3 * 1000)
#define MPU3050_ONE_MPU_TIME 20
#define MPU3050_BOGUS_ADDR 0x7F
int __attribute__((weak)) inv_register_mpu3050_slave(struct inv_mpu_state *st)
{
return 0;
}
int set_3050_bypass(struct inv_mpu_state *st, bool enable)
{
struct inv_reg_map_s *reg;
int result;
u8 b;
reg = &st->reg;
result = inv_i2c_read(st, reg->user_ctrl, 1, &b);
if (result)
return result;
if (((b & BIT_3050_AUX_IF_EN) == 0) && enable)
return 0;
if ((b & BIT_3050_AUX_IF_EN) && (enable == 0))
return 0;
b &= ~BIT_3050_AUX_IF_EN;
if (!enable) {
b |= BIT_3050_AUX_IF_EN;
result = inv_i2c_single_write(st, reg->user_ctrl, b);
return result;
} else {
/* Coming out of I2C is tricky due to several erratta. Do not
* modify this algorithm
*/
/*
* 1) wait for the right time and send the command to change
* the aux i2c slave address to an invalid address that will
* get nack'ed
*
* 0x00 is broadcast. 0x7F is unlikely to be used by any aux.
*/
result = inv_i2c_single_write(st, REG_3050_SLAVE_ADDR,
MPU3050_BOGUS_ADDR);
if (result)
return result;
/*
* 2) wait enough time for a nack to occur, then go into
* bypass mode:
*/
usleep_range(MPU3050_NACK_MIN_TIME, MPU3050_NACK_MAX_TIME);
result = inv_i2c_single_write(st, reg->user_ctrl, b);
if (result)
return result;
/*
* 3) wait for up to one MPU cycle then restore the slave
* address
*/
msleep(MPU3050_ONE_MPU_TIME);
result = inv_i2c_single_write(st, REG_3050_SLAVE_ADDR,
st->plat_data.secondary_i2c_addr);
if (result)
return result;
result = inv_i2c_single_write(st, reg->user_ctrl, b);
if (result)
return result;
usleep_range(MPU3050_NACK_MIN_TIME, MPU3050_NACK_MAX_TIME);
}
return 0;
}
void inv_setup_reg_mpu3050(struct inv_reg_map_s *reg)
{
reg->fifo_en = REG_3050_FIFO_EN;
reg->sample_rate_div = REG_3050_SAMPLE_RATE_DIV;
reg->lpf = REG_3050_LPF;
reg->fifo_count_h = REG_3050_FIFO_COUNT_H;
reg->fifo_r_w = REG_3050_FIFO_R_W;
reg->user_ctrl = REG_3050_USER_CTRL;
reg->pwr_mgmt_1 = REG_3050_PWR_MGMT_1;
reg->raw_accel = REG_3050_AUX_XOUT_H;
reg->temperature = REG_3050_TEMPERATURE;
reg->int_enable = REG_3050_INT_ENABLE;
reg->int_status = REG_3050_INT_STATUS;
}
int inv_switch_3050_gyro_engine(struct inv_mpu_state *st, bool en)
{
struct inv_reg_map_s *reg;
u8 data, p;
int result;
reg = &st->reg;
if (en) {
data = INV_CLK_PLL;
p = (BITS_3050_POWER1 | data);
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, p);
if (result)
return result;
p = (BITS_3050_POWER2 | data);
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, p);
if (result)
return result;
p = data;
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, p);
msleep(SENSOR_UP_TIME);
} else {
p = BITS_3050_GYRO_STANDBY;
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, p);
}
return result;
}
int inv_switch_3050_accel_engine(struct inv_mpu_state *st, bool en)
{
int result;
if (NULL == st->slave_accel)
return -EPERM;
if (en)
result = st->slave_accel->resume(st);
else
result = st->slave_accel->suspend(st);
return result;
}
/**
* inv_init_config_mpu3050() - Initialize hardware, disable FIFO.
* @st: Device driver instance.
* Initial configuration:
* FSR: +/- 2000DPS
* DLPF: 42Hz
* FIFO rate: 50Hz
* Clock source: Gyro PLL
*/
int inv_init_config_mpu3050(struct iio_dev *indio_dev)
{
struct inv_reg_map_s *reg;
int result;
u8 data;
struct inv_mpu_state *st = iio_priv(indio_dev);
if (st->chip_config.is_asleep)
return -EPERM;
/*reading AUX VDDIO register */
result = inv_i2c_read(st, REG_3050_AUX_VDDIO, 1, &data);
if (result)
return result;
data &= ~BIT_3050_VDDIO;
if (st->plat_data.level_shifter)
data |= BIT_3050_VDDIO;
result = inv_i2c_single_write(st, REG_3050_AUX_VDDIO, data);
if (result)
return result;
reg = &st->reg;
/*2000dps full scale range*/
result = inv_i2c_single_write(st, reg->lpf,
(INV_FSR_2000DPS << GYRO_CONFIG_FSR_SHIFT)
| INV_FILTER_42HZ);
if (result)
return result;
st->chip_config.fsr = INV_FSR_2000DPS;
st->chip_config.lpf = INV_FILTER_42HZ;
st->chip_info.multi = 1;
result = inv_i2c_single_write(st, reg->sample_rate_div,
ONE_K_HZ/INIT_FIFO_RATE - 1);
if (result)
return result;
st->chip_config.fifo_rate = INIT_FIFO_RATE;
st->irq_dur_ns = INIT_DUR_TIME;
st->chip_config.prog_start_addr = DMP_START_ADDR;
if ((SECONDARY_SLAVE_TYPE_ACCEL == st->plat_data.sec_slave_type) &&
st->slave_accel) {
result = st->slave_accel->setup(st);
if (result)
return result;
result = st->slave_accel->set_fs(st, INV_FS_02G);
if (result)
return result;
result = st->slave_accel->set_lpf(st, INIT_FIFO_RATE);
if (result)
return result;
}
return 0;
}
/**
* set_power_mpu3050() - set power of mpu3050.
* @st: Device driver instance.
* @power_on: on/off
*/
int set_power_mpu3050(struct inv_mpu_state *st, bool power_on)
{
struct inv_reg_map_s *reg;
u8 data, p;
int result;
reg = &st->reg;
if (power_on) {
data = 0;
} else {
if (st->slave_accel) {
result = st->slave_accel->suspend(st);
if (result)
return result;
}
data = BIT_SLEEP;
}
if (st->chip_config.gyro_enable) {
p = (BITS_3050_POWER1 | INV_CLK_PLL);
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, data | p);
if (result)
return result;
p = (BITS_3050_POWER2 | INV_CLK_PLL);
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, data | p);
if (result)
return result;
p = INV_CLK_PLL;
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, data | p);
if (result)
return result;
} else {
data |= (BITS_3050_GYRO_STANDBY | INV_CLK_INTERNAL);
result = inv_i2c_single_write(st, reg->pwr_mgmt_1, data);
if (result)
return result;
}
if (power_on) {
msleep(POWER_UP_TIME);
if (st->slave_accel) {
result = st->slave_accel->resume(st);
if (result)
return result;
}
}
st->chip_config.is_asleep = !power_on;
return 0;
}
|