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