1 /* drivers/input/sensors/access/mxc6225.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
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.
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.
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 <asm/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>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
33 #include <linux/sensor-dev.h>
36 #define MXC6225_ENABLE 1
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
49 #define MXC6225_RANGE 2000000//1500000
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)
56 #define MXC6225_COUNT_AVERAGE 2
58 struct sensor_axis_average {
65 static struct sensor_axis_average axis_average;
67 /****************operate according to sensor chip:start************/
69 static int sensor_active(struct i2c_client *client, int enable, int rate)
71 struct sensor_private_data *sensor =
72 (struct sensor_private_data *) i2c_get_clientdata(client);
76 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
78 //register setting according to chip datasheet
81 status = MXC6225_ENABLE; //mxc6225
82 sensor->ops->ctrl_data |= status;
86 status = ~MXC6225_ENABLE; //mxc6225
87 sensor->ops->ctrl_data &= status;
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);
93 printk("%s:fail to active sensor\n",__func__);
99 static int sensor_init(struct i2c_client *client)
101 struct sensor_private_data *sensor =
102 (struct sensor_private_data *) i2c_get_clientdata(client);
105 result = sensor->ops->active(client,0,0);
108 printk("%s:line=%d,error\n",__func__,__LINE__);
112 sensor->status_cur = SENSOR_OFF;
114 //DBG("%s:MXC6225_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MXC6225_REG_TILT));
116 result = sensor_write_reg(client, MXC6225_REG_SR, (0x01<<5)| 0x02); //32 Samples/Second Active and Auto-Sleep Mode
119 printk("%s:line=%d,error\n",__func__,__LINE__);
123 if(sensor->pdata->irq_enable) //open interrupt
125 result = sensor_write_reg(client, MXC6225_REG_INTSU, 1<<4);//enable int,GINT=1
128 printk("%s:line=%d,error\n",__func__,__LINE__);
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);
137 printk("%s:line=%d,error\n",__func__,__LINE__);
142 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
148 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
151 //struct sensor_private_data *sensor =
152 // (struct sensor_private_data *) i2c_get_clientdata(client);
153 //int precision = sensor->ops->precision;
155 result = (int)low_byte;
156 if (result < MXC6225_BOUNDARY)
157 result = result* MXC6225_GRAVITY_STEP;
159 result = ~(((~result & 0x7f) + 1) * MXC6225_GRAVITY_STEP) + 1;
164 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
166 struct sensor_private_data *sensor =
167 (struct sensor_private_data *) i2c_get_clientdata(client);
169 /* Report acceleration sensor information */
170 input_report_abs(sensor->input_dev, ABS_X, axis->x);
171 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
172 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
173 input_sync(sensor->input_dev);
174 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
179 #define GSENSOR_MIN 2
180 static int sensor_report_value(struct i2c_client *client)
182 struct sensor_private_data *sensor =
183 (struct sensor_private_data *) i2c_get_clientdata(client);
184 struct sensor_platform_data *pdata = sensor->pdata;
187 struct sensor_axis axis;
188 char buffer[3] = {0};
191 if(sensor->ops->read_len < 3) //sensor->ops->read_len = 3
193 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
197 memset(buffer, 0, 3);
199 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
201 *buffer = sensor->ops->read_reg;
202 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
208 //this gsensor need 6 bytes buffer
209 x = sensor_convert_data(sensor->client, 0, buffer[0]); //buffer[1]:high bit
210 y = sensor_convert_data(sensor->client, 0, buffer[1]);
211 z = sensor_convert_data(sensor->client, 0, buffer[2]);
213 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
214 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
215 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
218 axis_average.x_average += axis.x;
219 axis_average.y_average += axis.y;
220 axis_average.z_average += axis.z;
221 axis_average.count++;
223 if(axis_average.count >= MXC6225_COUNT_AVERAGE)
225 axis.x = axis_average.x_average / axis_average.count;
226 axis.y = axis_average.y_average / axis_average.count;
227 axis.z = axis_average.z_average / axis_average.count;
229 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
231 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
233 //Report event only while value is changed to save some power
234 if((abs(sensor->axis.x - axis.x) > GSENSOR_MIN) || (abs(sensor->axis.y - axis.y) > GSENSOR_MIN) || (abs(sensor->axis.z - axis.z) > GSENSOR_MIN))
236 gsensor_report_value(client, &axis);
238 /* »¥³âµØ»º´æÊý¾Ý. */
239 mutex_lock(&(sensor->data_mutex) );
241 mutex_unlock(&(sensor->data_mutex) );
245 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
248 value = sensor_read_reg(client, sensor->ops->int_status_reg);
249 DBG("%s:sensor int status :0x%x\n",__func__,value);
256 struct sensor_operate gsensor_mxc6225_ops = {
258 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
259 .id_i2c = ACCEL_ID_MXC6225, //i2c id number
260 .read_reg = MXC6225_REG_DATA, //read data
261 .read_len = 3, //data length
262 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
263 .id_data = SENSOR_UNKNOW_DATA, //device id
264 .precision = MXC6225_PRECISION, //12 bit
265 .ctrl_reg = MXC6225_REG_MODE, //enable or disable
266 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
267 .range = {-MXC6225_RANGE,MXC6225_RANGE}, //range
268 .trig = IRQF_TRIGGER_LOW |IRQF_ONESHOT,
269 .active = sensor_active,
271 .report = sensor_report_value,
274 /****************operate according to sensor chip:end************/
276 //function name should not be changed
277 static struct sensor_operate *gsensor_get_ops(void)
279 return &gsensor_mxc6225_ops;
283 static int __init gsensor_mxc6225_init(void)
285 struct sensor_operate *ops = gsensor_get_ops();
287 int type = ops->type;
289 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
293 static void __exit gsensor_mxc6225_exit(void)
295 struct sensor_operate *ops = gsensor_get_ops();
296 int type = ops->type;
297 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
301 module_init(gsensor_mxc6225_init);
302 module_exit(gsensor_mxc6225_exit);