1 /* drivers/input/sensors/access/mpu6880_acc.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: oeh<oeh@rock-chips.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
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 */
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <linux/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33 #include <linux/mpu6880.h>
34
mpu6880_set_lpf(struct i2c_client * client,int rate)35 static int mpu6880_set_lpf(struct i2c_client *client, int rate)
36 {
37 const short hz[] = {184, 98, 41, 20, 10, 5};
38 const int d[] = {DLPF_CFG_184HZ, DLPF_CFG_98HZ,
39 DLPF_CFG_41HZ, DLPF_CFG_20HZ,
40 DLPF_CFG_10HZ, DLPF_CFG_5HZ};
41 int i, h, data, result;
42
43 h = (rate >> 1);
44 i = 0;
45 while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
46 i++;
47 data = d[i];
48
49 result = sensor_write_reg(client, MPU6880_CONFIG, data);
50 if (result)
51 return -1;
52
53 return 0;
54 }
55
mpu6880_set_rate(struct i2c_client * client,int rate)56 static int mpu6880_set_rate(struct i2c_client *client, int rate)
57 {
58 u8 data;
59 int result;
60 u16 fifo_rate;
61
62 /* always use poll mode, no need to set rate */
63 return 0;
64
65 if ((rate < 1) || (rate > 250))
66 return -1;
67
68 data = rate - 1;
69 result = sensor_write_reg(client, MPU6880_SMPLRT_DIV, data);
70 if (result)
71 return result;
72
73 fifo_rate = 1000 / rate;
74
75 result = mpu6880_set_lpf(client, fifo_rate);
76 if (result)
77 return -1;
78
79 return 0;
80 }
81
sensor_active(struct i2c_client * client,int enable,int rate)82 static int sensor_active(struct i2c_client *client, int enable, int rate)
83 {
84 struct sensor_private_data *sensor =
85 (struct sensor_private_data *) i2c_get_clientdata(client);
86 int result = 0;
87 int status = 0;
88 u8 pwrm1 = 0;
89
90 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
91 pwrm1 = sensor_read_reg(client, MPU6880_PWR_MGMT_1);
92
93 if (!enable) {
94 status = BIT_ACCEL_STBY;
95 sensor->ops->ctrl_data |= status;
96 if ((sensor->ops->ctrl_data & BIT_GYRO_STBY) == BIT_GYRO_STBY) {
97 pwrm1 |= MPU6880_PWRM1_SLEEP;
98 }
99 } else {
100 status = ~BIT_ACCEL_STBY;
101 sensor->ops->ctrl_data &= status;
102 pwrm1 &= ~MPU6880_PWRM1_SLEEP;
103
104 mpu6880_set_rate(client, rate);
105 }
106 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
107 if (result) {
108 dev_err(&client->dev, "%s:fail to set pwrm2\n", __func__);
109 return -1;
110 }
111 msleep(20);
112
113 result = sensor_write_reg(client, MPU6880_PWR_MGMT_1, pwrm1);
114 if (result) {
115 dev_err(&client->dev, "%s:fail to set pwrm1\n", __func__);
116 return -1;
117 }
118 msleep(50);
119
120 return result;
121 }
122
sensor_init(struct i2c_client * client)123 static int sensor_init(struct i2c_client *client)
124 {
125 int res = 0;
126 u8 read_data = 0;
127 struct sensor_private_data *sensor =
128 (struct sensor_private_data *) i2c_get_clientdata(client);
129
130 read_data = sensor_read_reg(client, sensor->ops->id_reg);
131 if (read_data != sensor->ops->id_data) {
132 dev_err(&client->dev, "%s:check id err,read_data:%d,ops->id_data:%d\n", __func__, read_data, sensor->ops->id_data);
133 return -1;
134 }
135
136 res = sensor_write_reg(client, MPU6880_PWR_MGMT_1, 0x80);
137 if (res) {
138 dev_err(&client->dev, "set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
139 return res;
140 }
141 msleep(40);
142
143 res = sensor_write_reg(client, MPU6880_GYRO_CONFIG, 0x18);
144 if (res) {
145 dev_err(&client->dev, "set MPU6880_GYRO_CONFIG error,res: %d!\n", res);
146 return res;
147 }
148 msleep(10);
149
150 res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG, 0x00);
151 if (res) {
152 dev_err(&client->dev, "set MPU6880_ACCEL_CONFIG error,res: %d!\n", res);
153 return res;
154 }
155 msleep(10);
156
157 res = sensor_write_reg(client, MPU6880_ACCEL_CONFIG2, 0x00);
158 if (res) {
159 dev_err(&client->dev, "set MPU6880_ACCEL_CONFIG2 error,res: %d!\n", res);
160 return res;
161 }
162 res = sensor_write_reg(client, MPU6880_PWR_MGMT_2, 0x3F);
163 if (res) {
164 dev_err(&client->dev, "set MPU6880_PWR_MGMT_2 error,res: %d!\n", res);
165 return res;
166 }
167 msleep(10);
168 res = sensor_write_reg(client, MPU6880_PWR_MGMT_1, 0x41);
169 if (res) {
170 dev_err(&client->dev, "set MPU6880_PWR_MGMT_1 error,res: %d!\n", res);
171 return res;
172 }
173 msleep(10);
174
175 res = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
176 if (res) {
177 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
178 return res;
179 }
180 return res;
181 }
182
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)183 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
184 {
185 struct sensor_private_data *sensor =
186 (struct sensor_private_data *) i2c_get_clientdata(client);
187
188 if (sensor->status_cur == SENSOR_ON) {
189 /* Report acceleration sensor information */
190 input_report_abs(sensor->input_dev, ABS_X, axis->x);
191 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
192 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
193 input_sync(sensor->input_dev);
194 }
195
196 return 0;
197 }
198
sensor_report_value(struct i2c_client * client)199 static int sensor_report_value(struct i2c_client *client)
200 {
201 struct sensor_private_data *sensor =
202 (struct sensor_private_data *) i2c_get_clientdata(client);
203 struct sensor_platform_data *pdata = sensor->pdata;
204 int ret = 0;
205 short x, y, z;
206 struct sensor_axis axis;
207 u8 buffer[6] = {0};
208 char value = 0;
209
210 if (sensor->ops->read_len < 6) {
211 dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
212 return -1;
213 }
214
215 memset(buffer, 0, 6);
216
217 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
218 do {
219 *buffer = sensor->ops->read_reg;
220 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
221 if (ret < 0)
222 return ret;
223 } while (0);
224
225 x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
226 y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
227 z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
228 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
229 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
230 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
231
232 gsensor_report_value(client, &axis);
233
234 mutex_lock(&(sensor->data_mutex));
235 sensor->axis = axis;
236 mutex_unlock(&(sensor->data_mutex));
237
238 if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
239 value = sensor_read_reg(client, sensor->ops->int_status_reg);
240
241 return ret;
242 }
243
244 static struct sensor_operate gsensor_mpu6880_ops = {
245 .name = "mpu6880_acc",
246 .type = SENSOR_TYPE_ACCEL,
247 .id_i2c = ACCEL_ID_MPU6880,
248 .read_reg = MPU6880_ACCEL_XOUT_H,
249 .read_len = 6,
250 .id_reg = MPU6880_WHOAMI,
251 .id_data = MPU6880_DEVICE_ID,
252 .precision = MPU6880_PRECISION,
253 .ctrl_reg = MPU6880_PWR_MGMT_2,
254 .int_status_reg = MPU6880_INT_STATUS,
255 .range = {-32768, 32768},
256 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
257 .active = sensor_active,
258 .init = sensor_init,
259 .report = sensor_report_value,
260 };
261
262 /****************operate according to sensor chip:end************/
gsensor_mpu6880_probe(struct i2c_client * client,const struct i2c_device_id * devid)263 static int gsensor_mpu6880_probe(struct i2c_client *client,
264 const struct i2c_device_id *devid)
265 {
266 return sensor_register_device(client, NULL, devid, &gsensor_mpu6880_ops);
267 }
268
gsensor_mpu6880_remove(struct i2c_client * client)269 static int gsensor_mpu6880_remove(struct i2c_client *client)
270 {
271 return sensor_unregister_device(client, NULL, &gsensor_mpu6880_ops);
272 }
273
274 static const struct i2c_device_id gsensor_mpu6880_id[] = {
275 {"mpu6880_acc", ACCEL_ID_MPU6880},
276 {}
277 };
278
279 static struct i2c_driver gsensor_mpu6880_driver = {
280 .probe = gsensor_mpu6880_probe,
281 .remove = gsensor_mpu6880_remove,
282 .shutdown = sensor_shutdown,
283 .id_table = gsensor_mpu6880_id,
284 .driver = {
285 .name = "gsensor_mpu6880",
286 #ifdef CONFIG_PM
287 .pm = &sensor_pm_ops,
288 #endif
289 },
290 };
291
292 module_i2c_driver(gsensor_mpu6880_driver);
293
294 MODULE_AUTHOR("oeh <oeh@rock-chips.com>");
295 MODULE_DESCRIPTION("mpu6880 3-Axis accelerometer driver");
296 MODULE_LICENSE("GPL");
297