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