xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/accel/mxc622x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* drivers/input/sensors/access/mxc6225.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@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 
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34 
35 
36 #define MXC6225_ENABLE		1
37 
38 #define MXC6225_REG_DATA       0x00
39 #define MXC6225_REG_TILT        0x3
40 #define MXC6225_REG_SRST        0x4
41 #define MXC6225_REG_SPCNT       0x5
42 #define MXC6225_REG_INTSU      	0x6
43 #define MXC6225_REG_MODE        0x7
44 #define MXC6225_REG_SR          0x8
45 #define MXC6225_REG_PDET        0x9
46 #define MXC6225_REG_PD          0xa
47 
48 
49 #define MXC6225_RANGE			2000000//1500000
50 
51 /* LIS3DH */
52 #define MXC6225_PRECISION       8 // 8bit data
53 #define MXC6225_BOUNDARY		(0x1 << (MXC6225_PRECISION - 1))
54 #define MXC6225_GRAVITY_STEP	15625 //	(MXC6225_RANGE / MXC6225_BOUNDARY)
55 
56 #define MXC6225_COUNT_AVERAGE	2
57 
58 struct sensor_axis_average {
59 		int x_average;
60 		int y_average;
61 		int z_average;
62 		int count;
63 };
64 
65 static struct sensor_axis_average axis_average;
66 
67 /****************operate according to sensor chip:start************/
68 
sensor_active(struct i2c_client * client,int enable,int rate)69 static int sensor_active(struct i2c_client *client, int enable, int rate)
70 {
71 	struct sensor_private_data *sensor =
72 	    (struct sensor_private_data *) i2c_get_clientdata(client);
73 	int result = 0;
74 	int status = 0;
75 
76 	sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
77 
78 	//register setting according to chip datasheet
79 	if(enable)
80 	{
81 		status = MXC6225_ENABLE;	//mxc6225
82 		sensor->ops->ctrl_data |= status;
83 	}
84 	else
85 	{
86 		status = ~MXC6225_ENABLE;	//mxc6225
87 		sensor->ops->ctrl_data &= status;
88 	}
89 
90 	DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
91 	result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
92 	if(result)
93 		printk("%s:fail to active sensor\n",__func__);
94 
95 	return result;
96 
97 }
98 
sensor_init(struct i2c_client * client)99 static int sensor_init(struct i2c_client *client)
100 {
101 	struct sensor_private_data *sensor =
102 	    (struct sensor_private_data *) i2c_get_clientdata(client);
103 	int result = 0;
104 
105 	result = sensor->ops->active(client,0,0);
106 	if(result)
107 	{
108 		printk("%s:line=%d,error\n",__func__,__LINE__);
109 		return result;
110 	}
111 
112 	sensor->status_cur = SENSOR_OFF;
113 
114 	//DBG("%s:MXC6225_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MXC6225_REG_TILT));
115 
116 	result = sensor_write_reg(client, MXC6225_REG_SR, (0x01<<5)| 0x02);	//32 Samples/Second Active and Auto-Sleep Mode
117 	if(result)
118 	{
119 		printk("%s:line=%d,error\n",__func__,__LINE__);
120 		return result;
121 	}
122 
123 	if(sensor->pdata->irq_enable)	//open interrupt
124 	{
125 		result = sensor_write_reg(client, MXC6225_REG_INTSU, 1<<4);//enable int,GINT=1
126 		if(result)
127 		{
128 			printk("%s:line=%d,error\n",__func__,__LINE__);
129 			return result;
130 		}
131 	}
132 
133 	sensor->ops->ctrl_data = 1<<8;	//Interrupt output INT is push-pull
134 	result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
135 	if(result)
136 	{
137 		printk("%s:line=%d,error\n",__func__,__LINE__);
138 		return result;
139 	}
140 
141 
142 	memset(&axis_average, 0, sizeof(struct sensor_axis_average));
143 
144 	return result;
145 }
146 
147 
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)148 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
149 {
150     s64 result;
151 	//struct sensor_private_data *sensor =
152 	//    (struct sensor_private_data *) i2c_get_clientdata(client);
153 	//int precision = sensor->ops->precision;
154 
155 	result = (int)low_byte;
156 	result *= 256;
157 
158     	return (int)result;
159 }
160 
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)161 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
162 {
163 	struct sensor_private_data *sensor =
164 	    (struct sensor_private_data *) i2c_get_clientdata(client);
165 
166 	if (sensor->status_cur == SENSOR_ON) {
167 		/* Report acceleration sensor information */
168 		input_report_abs(sensor->input_dev, ABS_X, axis->x);
169 		input_report_abs(sensor->input_dev, ABS_Y, axis->y);
170 		input_report_abs(sensor->input_dev, ABS_Z, axis->z);
171 		input_sync(sensor->input_dev);
172 	}
173 
174 	return 0;
175 }
176 
177 #define GSENSOR_MIN  		2
sensor_report_value(struct i2c_client * client)178 static int sensor_report_value(struct i2c_client *client)
179 {
180 	struct sensor_private_data *sensor =
181 		(struct sensor_private_data *) i2c_get_clientdata(client);
182 	struct sensor_platform_data *pdata = sensor->pdata;
183 	int ret = 0;
184 	int x,y,z;
185 	struct sensor_axis axis;
186 	char buffer[3] = {0};
187 	char value = 0;
188 
189 	if(sensor->ops->read_len < 3)	//sensor->ops->read_len = 3
190 	{
191 		printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
192 		return -1;
193 	}
194 
195 	memset(buffer, 0, 3);
196 
197 	/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
198 	do {
199 		*buffer = sensor->ops->read_reg;
200 		ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
201 		if (ret < 0)
202 		return ret;
203 	} while (0);
204 
205 
206 	//this gsensor need 6 bytes buffer
207 	x = sensor_convert_data(sensor->client, 0, buffer[0]);	//buffer[1]:high bit
208 	y = sensor_convert_data(sensor->client, 0, buffer[1]);
209 	z = sensor_convert_data(sensor->client, 0, buffer[2]);
210 
211 	axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
212 	axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
213 	axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
214 
215 	gsensor_report_value(client, &axis);
216 
217 	mutex_lock(&sensor->data_mutex);
218 	sensor->axis = axis;
219 	mutex_unlock(&sensor->data_mutex);
220 
221 	if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))	//read sensor intterupt status register
222 	{
223 
224 		value = sensor_read_reg(client, sensor->ops->int_status_reg);
225 		DBG("%s:sensor int status :0x%x\n",__func__,value);
226 	}
227 
228 	return ret;
229 }
230 
231 
232 static struct sensor_operate gsensor_mxc6225_ops = {
233 	.name			= "mxc6225",
234 	.type			= SENSOR_TYPE_ACCEL,
235 	.id_i2c			= ACCEL_ID_MXC6225,
236 	.read_reg			= MXC6225_REG_DATA,
237 	.read_len			= 3,
238 	.id_reg			= SENSOR_UNKNOW_DATA,
239 	.id_data			= SENSOR_UNKNOW_DATA,
240 	.precision			= MXC6225_PRECISION,
241 	.ctrl_reg			= MXC6225_REG_MODE,
242 	.int_status_reg	= SENSOR_UNKNOW_DATA,
243 	.range			= {-32768, 32768},
244 	.trig				= IRQF_TRIGGER_LOW | IRQF_ONESHOT,
245 	.active			= sensor_active,
246 	.init				= sensor_init,
247 	.report 			= sensor_report_value,
248 };
249 
250 /****************operate according to sensor chip:end************/
gsensor_mxc6225_probe(struct i2c_client * client,const struct i2c_device_id * devid)251 static int gsensor_mxc6225_probe(struct i2c_client *client,
252 				const struct i2c_device_id *devid)
253 {
254 	return sensor_register_device(client, NULL, devid, &gsensor_mxc6225_ops);
255 }
256 
gsensor_mxc6225_remove(struct i2c_client * client)257 static int gsensor_mxc6225_remove(struct i2c_client *client)
258 {
259 	return sensor_unregister_device(client, NULL, &gsensor_mxc6225_ops);
260 }
261 
262 static const struct i2c_device_id gsensor_mxc6225_id[] = {
263 	{"gs_mxc6225", ACCEL_ID_MXC6225},
264 	{}
265 };
266 
267 static struct i2c_driver gsensor_mxc6225_driver = {
268 	.probe = gsensor_mxc6225_probe,
269 	.remove = gsensor_mxc6225_remove,
270 	.shutdown = sensor_shutdown,
271 	.id_table = gsensor_mxc6225_id,
272 	.driver = {
273 		.name = "gsensor_mxc6225",
274 	#ifdef CONFIG_PM
275 		.pm = &sensor_pm_ops,
276 	#endif
277 	},
278 };
279 
280 module_i2c_driver(gsensor_mxc6225_driver);
281 
282 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
283 MODULE_DESCRIPTION("mxc6225 3-Axis accelerometer driver");
284 MODULE_LICENSE("GPL");
285