1 /* drivers/input/sensors/access/mpu6500_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/mpu6500.h>
34
mpu6500_set_lpf(struct i2c_client * client,int rate)35 static int mpu6500_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, MPU6500_CONFIG, data);
50 if (result)
51 return -1;
52
53 return 0;
54 }
55
mpu6500_set_rate(struct i2c_client * client,int rate)56 static int mpu6500_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, MPU6500_SMPLRT_DIV, data);
70 if (result)
71 return result;
72
73 fifo_rate = 1000 / rate;
74
75 result = mpu6500_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, MPU6500_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 |= MPU6500_PWRM1_SLEEP;
98 }
99 } else {
100 status = ~BIT_ACCEL_STBY;
101 sensor->ops->ctrl_data &= status;
102 pwrm1 &= ~MPU6500_PWRM1_SLEEP;
103
104 mpu6500_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, MPU6500_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
132 #if 0
133 if (read_data != sensor->ops->id_data) {
134 dev_err(&client->dev, "%s:check id err,read_data:%d,ops->id_data:%d\n", __func__, read_data, sensor->ops->id_data);
135 return -1;
136 }
137 #endif
138
139 res = sensor_write_reg(client, MPU6500_PWR_MGMT_1, 0x80);
140 if (res) {
141 dev_err(&client->dev, "set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
142 return res;
143 }
144 msleep(40);
145
146 res = sensor_write_reg(client, MPU6500_GYRO_CONFIG, 0x18);
147 if (res) {
148 dev_err(&client->dev, "set MPU6500_GYRO_CONFIG error,res: %d!\n", res);
149 return res;
150 }
151 msleep(10);
152
153 res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG, 0x00);
154 if (res) {
155 dev_err(&client->dev, "set MPU6500_ACCEL_CONFIG error,res: %d!\n", res);
156 return res;
157 }
158 msleep(10);
159
160 res = sensor_write_reg(client, MPU6500_ACCEL_CONFIG2, 0x00);
161 if (res) {
162 dev_err(&client->dev, "set MPU6500_ACCEL_CONFIG2 error,res: %d!\n", res);
163 return res;
164 }
165 res = sensor_write_reg(client, MPU6500_PWR_MGMT_2, 0x3F);
166 if (res) {
167 dev_err(&client->dev, "set MPU6500_PWR_MGMT_2 error,res: %d!\n", res);
168 return res;
169 }
170 msleep(10);
171 res = sensor_write_reg(client, MPU6500_PWR_MGMT_1, 0x41);
172 if (res) {
173 dev_err(&client->dev, "set MPU6500_PWR_MGMT_1 error,res: %d!\n", res);
174 return res;
175 }
176 msleep(10);
177
178 res = sensor->ops->active(client, 0, sensor->pdata->poll_delay_ms);
179 if (res) {
180 dev_err(&client->dev, "%s:line=%d,error\n", __func__, __LINE__);
181 return res;
182 }
183 return res;
184 }
185
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)186 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
187 {
188 struct sensor_private_data *sensor =
189 (struct sensor_private_data *) i2c_get_clientdata(client);
190
191 if (sensor->status_cur == SENSOR_ON) {
192 /* Report acceleration sensor information */
193 input_report_abs(sensor->input_dev, ABS_X, axis->x);
194 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
195 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
196 input_sync(sensor->input_dev);
197 }
198
199 return 0;
200 }
201
sensor_report_value(struct i2c_client * client)202 static int sensor_report_value(struct i2c_client *client)
203 {
204 struct sensor_private_data *sensor =
205 (struct sensor_private_data *) i2c_get_clientdata(client);
206 struct sensor_platform_data *pdata = sensor->pdata;
207 int ret = 0;
208 short x, y, z;
209 struct sensor_axis axis;
210 u8 buffer[6] = {0};
211 char value = 0;
212
213 if (sensor->ops->read_len < 6) {
214 dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
215 return -1;
216 }
217
218 memset(buffer, 0, 6);
219
220 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
221 do {
222 *buffer = sensor->ops->read_reg;
223 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
224 if (ret < 0)
225 return ret;
226 } while (0);
227
228 x = ((buffer[0] << 8) & 0xff00) + (buffer[1] & 0xFF);
229 y = ((buffer[2] << 8) & 0xff00) + (buffer[3] & 0xFF);
230 z = ((buffer[4] << 8) & 0xff00) + (buffer[5] & 0xFF);
231
232 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
233 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
234 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
235
236 gsensor_report_value(client, &axis);
237
238 mutex_lock(&(sensor->data_mutex));
239 sensor->axis = axis;
240 mutex_unlock(&(sensor->data_mutex));
241
242 if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
243 value = sensor_read_reg(client, sensor->ops->int_status_reg);
244
245 return ret;
246 }
247
248 static struct sensor_operate gsensor_mpu6500_ops = {
249 .name = "mpu6500_acc",
250 .type = SENSOR_TYPE_ACCEL,
251 .id_i2c = ACCEL_ID_MPU6500,
252 .read_reg = MPU6500_ACCEL_XOUT_H,
253 .read_len = 6,
254 .id_reg = SENSOR_UNKNOW_DATA,
255 .id_data = SENSOR_UNKNOW_DATA,
256 .precision = MPU6500_PRECISION,
257 .ctrl_reg = MPU6500_PWR_MGMT_2,
258 .int_status_reg = MPU6500_INT_STATUS,
259 .range = {-32768, 32768},
260 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
261 .active = sensor_active,
262 .init = sensor_init,
263 .report = sensor_report_value,
264 };
265
266 /****************operate according to sensor chip:end************/
gsensor_mpu6500_probe(struct i2c_client * client,const struct i2c_device_id * devid)267 static int gsensor_mpu6500_probe(struct i2c_client *client,
268 const struct i2c_device_id *devid)
269 {
270 return sensor_register_device(client, NULL, devid, &gsensor_mpu6500_ops);
271 }
272
gsensor_mpu6500_remove(struct i2c_client * client)273 static int gsensor_mpu6500_remove(struct i2c_client *client)
274 {
275 return sensor_unregister_device(client, NULL, &gsensor_mpu6500_ops);
276 }
277
278 static const struct i2c_device_id gsensor_mpu6500_id[] = {
279 {"mpu6500_acc", ACCEL_ID_MPU6500},
280 {}
281 };
282
283 static struct i2c_driver gsensor_mpu6500_driver = {
284 .probe = gsensor_mpu6500_probe,
285 .remove = gsensor_mpu6500_remove,
286 .shutdown = sensor_shutdown,
287 .id_table = gsensor_mpu6500_id,
288 .driver = {
289 .name = "gsensor_mpu6500",
290 #ifdef CONFIG_PM
291 .pm = &sensor_pm_ops,
292 #endif
293 },
294 };
295
296 module_i2c_driver(gsensor_mpu6500_driver);
297
298 MODULE_AUTHOR("oeh <oeh@rock-chips.com>");
299 MODULE_DESCRIPTION("mpu6500_acc 3-Axis accelerometer driver");
300 MODULE_LICENSE("GPL");
301