1 /* drivers/input/sensors/access/mma7660.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>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
32 #include <linux/sensor-dev.h>
35 #define MMA7660_ENABLE 1
37 #define MMA7660_REG_X_OUT 0x0
38 #define MMA7660_REG_Y_OUT 0x1
39 #define MMA7660_REG_Z_OUT 0x2
40 #define MMA7660_REG_TILT 0x3
41 #define MMA7660_REG_SRST 0x4
42 #define MMA7660_REG_SPCNT 0x5
43 #define MMA7660_REG_INTSU 0x6
44 #define MMA7660_REG_MODE 0x7
45 #define MMA7660_REG_SR 0x8
46 #define MMA7660_REG_PDET 0x9
47 #define MMA7660_REG_PD 0xa
50 #define MMA7660_RANGE 1500000
53 #define MMA7660_PRECISION 6
54 #define MMA7660_BOUNDARY (0x1 << (MMA7660_PRECISION - 1))
55 #define MMA7660_GRAVITY_STEP (MMA7660_RANGE / MMA7660_BOUNDARY)
57 #define MMA7660_COUNT_AVERAGE 2
59 struct sensor_axis_average {
66 static struct sensor_axis_average axis_average;
68 /****************operate according to sensor chip:start************/
70 static int sensor_active(struct i2c_client *client, int enable, int rate)
72 struct sensor_private_data *sensor =
73 (struct sensor_private_data *) i2c_get_clientdata(client);
77 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
79 //register setting according to chip datasheet
82 status = MMA7660_ENABLE; //mma7660
83 sensor->ops->ctrl_data |= status;
87 status = ~MMA7660_ENABLE; //mma7660
88 sensor->ops->ctrl_data &= status;
91 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
92 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
94 printk("%s:fail to active sensor\n",__func__);
100 static int sensor_init(struct i2c_client *client)
102 struct sensor_private_data *sensor =
103 (struct sensor_private_data *) i2c_get_clientdata(client);
106 result = sensor->ops->active(client,0,0);
109 printk("%s:line=%d,error\n",__func__,__LINE__);
113 sensor->status_cur = SENSOR_OFF;
115 DBG("%s:MMA7660_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MMA7660_REG_TILT));
117 result = sensor_write_reg(client, MMA7660_REG_SR, (0x01<<5)| 0x02); //32 Samples/Second Active and Auto-Sleep Mode
120 printk("%s:line=%d,error\n",__func__,__LINE__);
124 if(sensor->pdata->irq_enable) //open interrupt
126 result = sensor_write_reg(client, MMA7660_REG_INTSU, 1<<4);//enable int,GINT=1
129 printk("%s:line=%d,error\n",__func__,__LINE__);
134 sensor->ops->ctrl_data = 1<<6; //Interrupt output INT is push-pull
135 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
138 printk("%s:line=%d,error\n",__func__,__LINE__);
143 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
149 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
152 //struct sensor_private_data *sensor =
153 // (struct sensor_private_data *) i2c_get_clientdata(client);
154 //int precision = sensor->ops->precision;
156 result = (int)low_byte;
157 if (result < MMA7660_BOUNDARY)
158 result = result* MMA7660_GRAVITY_STEP;
160 result = ~( ((~result & (0x7fff>>(16-MMA7660_PRECISION)) ) + 1)
161 * MMA7660_GRAVITY_STEP) + 1;
166 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
168 struct sensor_private_data *sensor =
169 (struct sensor_private_data *) i2c_get_clientdata(client);
171 /* Report acceleration sensor information */
172 input_report_abs(sensor->input_dev, ABS_X, axis->x);
173 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
174 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
175 input_sync(sensor->input_dev);
176 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
181 #define GSENSOR_MIN 2
182 static int sensor_report_value(struct i2c_client *client)
184 struct sensor_private_data *sensor =
185 (struct sensor_private_data *) i2c_get_clientdata(client);
186 struct sensor_platform_data *pdata = sensor->pdata;
189 struct sensor_axis axis;
190 char buffer[3] = {0};
193 if(sensor->ops->read_len < 3) //sensor->ops->read_len = 3
195 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
199 memset(buffer, 0, 3);
201 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
203 *buffer = sensor->ops->read_reg;
204 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
210 //this gsensor need 6 bytes buffer
211 x = sensor_convert_data(sensor->client, 0, buffer[0]); //buffer[1]:high bit
212 y = sensor_convert_data(sensor->client, 0, buffer[1]);
213 z = sensor_convert_data(sensor->client, 0, buffer[2]);
215 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
216 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
217 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
220 axis_average.x_average += axis.x;
221 axis_average.y_average += axis.y;
222 axis_average.z_average += axis.z;
223 axis_average.count++;
225 if(axis_average.count >= MMA7660_COUNT_AVERAGE)
227 axis.x = axis_average.x_average / axis_average.count;
228 axis.y = axis_average.y_average / axis_average.count;
229 axis.z = axis_average.z_average / axis_average.count;
231 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
233 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
235 //Report event only while value is changed to save some power
236 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))
238 gsensor_report_value(client, &axis);
240 /* »¥³âµØ»º´æÊý¾Ý. */
241 mutex_lock(&(sensor->data_mutex) );
243 mutex_unlock(&(sensor->data_mutex) );
247 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
250 value = sensor_read_reg(client, sensor->ops->int_status_reg);
251 DBG("%s:sensor int status :0x%x\n",__func__,value);
258 struct sensor_operate gsensor_mma7660_ops = {
260 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
261 .id_i2c = ACCEL_ID_MMA7660, //i2c id number
262 .read_reg = MMA7660_REG_X_OUT, //read data
263 .read_len = 3, //data length
264 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
265 .id_data = SENSOR_UNKNOW_DATA, //device id
266 .precision = MMA7660_PRECISION, //12 bit
267 .ctrl_reg = MMA7660_REG_MODE, //enable or disable
268 .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register
269 .range = {-MMA7660_RANGE,MMA7660_RANGE}, //range
270 .trig = IRQF_TRIGGER_LOW|IRQF_ONESHOT,
271 .active = sensor_active,
273 .report = sensor_report_value,
276 /****************operate according to sensor chip:end************/
278 //function name should not be changed
279 static struct sensor_operate *gsensor_get_ops(void)
281 return &gsensor_mma7660_ops;
285 static int __init gsensor_mma7660_init(void)
287 struct sensor_operate *ops = gsensor_get_ops();
289 int type = ops->type;
290 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
294 static void __exit gsensor_mma7660_exit(void)
296 struct sensor_operate *ops = gsensor_get_ops();
297 int type = ops->type;
298 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
302 module_init(gsensor_mma7660_init);
303 module_exit(gsensor_mma7660_exit);