1 /* drivers/input/sensors/access/kxtik.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 LIS3DH_INT_COUNT (0x0E)
36 #define LIS3DH_WHO_AM_I (0x0F)
38 /* full scale setting - register & mask */
39 #define LIS3DH_TEMP_CFG_REG (0x1F)
40 #define LIS3DH_CTRL_REG1 (0x20)
41 #define LIS3DH_CTRL_REG2 (0x21)
42 #define LIS3DH_CTRL_REG3 (0x22)
43 #define LIS3DH_CTRL_REG4 (0x23)
44 #define LIS3DH_CTRL_REG5 (0x24)
45 #define LIS3DH_CTRL_REG6 (0x25)
46 #define LIS3DH_REFERENCE (0x26)
47 #define LIS3DH_STATUS_REG (0x27)
48 #define LIS3DH_OUT_X_L (0x28)
49 #define LIS3DH_OUT_X_H (0x29)
50 #define LIS3DH_OUT_Y_L (0x2a)
51 #define LIS3DH_OUT_Y_H (0x2b)
52 #define LIS3DH_OUT_Z_L (0x2c)
53 #define LIS3DH_OUT_Z_H (0x2d)
54 #define LIS3DH_FIFO_CTRL_REG (0x2E)
56 #define LIS3DH_INT1_CFG (0x30)
57 #define LIS3DH_INT1_SRC (0x31)
58 #define LIS3DH_INT1_THS (0x32)
59 #define LIS3DH_INT1_DURATION (0x33)
61 #define LIS3DH_DEVID (0x33) //chip id
62 #define LIS3DH_ACC_DISABLE (0x08)
64 #define LIS3DH_RANGE 2000000
67 #define LIS3DH_PRECISION 16
68 #define LIS3DH_BOUNDARY (0x1 << (LIS3DH_PRECISION - 1))
69 #define LIS3DH_GRAVITY_STEP (LIS3DH_RANGE / LIS3DH_BOUNDARY)
71 #define ODR1 0x10 /* 1Hz output data rate */
72 #define ODR10 0x20 /* 10Hz output data rate */
73 #define ODR25 0x30 /* 25Hz output data rate */
74 #define ODR50 0x40 /* 50Hz output data rate */
75 #define ODR100 0x50 /* 100Hz output data rate */
76 #define ODR200 0x60 /* 200Hz output data rate */
77 #define ODR400 0x70 /* 400Hz output data rate */
78 #define ODR1250 0x90 /* 1250Hz output data rate */
82 struct sensor_reg_data {
87 /****************operate according to sensor chip:start************/
89 static int sensor_active(struct i2c_client *client, int enable, int rate)
91 struct sensor_private_data *sensor =
92 (struct sensor_private_data *) i2c_get_clientdata(client);
96 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
98 sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down
100 //register setting according to chip datasheet
103 status = LIS3DH_ACC_DISABLE; //lis3dh
104 sensor->ops->ctrl_data |= status;
108 status = ~LIS3DH_ACC_DISABLE; //lis3dh
109 sensor->ops->ctrl_data &= status;
112 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
113 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
115 printk("%s:fail to active sensor\n",__func__);
121 static int sensor_init(struct i2c_client *client)
123 struct sensor_private_data *sensor =
124 (struct sensor_private_data *) i2c_get_clientdata(client);
127 struct sensor_reg_data reg_data[] =
129 {LIS3DH_CTRL_REG2,0X00},
130 {LIS3DH_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode
131 {LIS3DH_CTRL_REG6,0x40},
132 {LIS3DH_TEMP_CFG_REG,0x00}, //
133 {LIS3DH_FIFO_CTRL_REG,0x00}, //
134 {LIS3DH_INT1_CFG,0xFF}, //6 direction position recognition
135 {LIS3DH_INT1_THS,0x7F}, //Interrupt 1 threshold
136 {LIS3DH_INT1_DURATION,0x7F}, //Duration value 0x00->ox7f
139 result = sensor->ops->active(client,0,0);
142 printk("%s:line=%d,error\n",__func__,__LINE__);
146 sensor->status_cur = SENSOR_OFF;
148 for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
150 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
153 printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
159 if(sensor->pdata->irq_enable)
162 result = sensor_write_reg(client, LIS3DH_CTRL_REG3, 0x40);//I1_AOI1 =1 if motion
165 printk("%s:line=%d,error\n",__func__,__LINE__);
169 result = sensor_write_reg(client, LIS3DH_CTRL_REG5, 0x08);
172 printk("%s:line=%d,error\n",__func__,__LINE__);
181 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
184 struct sensor_private_data *sensor =
185 (struct sensor_private_data *) i2c_get_clientdata(client);
186 //int precision = sensor->ops->precision;
187 switch (sensor->devid) {
189 result = ((int)high_byte << 8) | (int)low_byte;
190 if (result < LIS3DH_BOUNDARY)
191 result = result* LIS3DH_GRAVITY_STEP;
193 result = ~( ((~result & (0x7fff>>(16-LIS3DH_PRECISION)) ) + 1)
194 * LIS3DH_GRAVITY_STEP) + 1;
198 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
205 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
207 struct sensor_private_data *sensor =
208 (struct sensor_private_data *) i2c_get_clientdata(client);
210 /* Report acceleration sensor information */
211 input_report_abs(sensor->input_dev, ABS_X, axis->x);
212 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
213 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
214 input_sync(sensor->input_dev);
215 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
220 #define GSENSOR_MIN 10
221 static int sensor_report_value(struct i2c_client *client)
223 struct sensor_private_data *sensor =
224 (struct sensor_private_data *) i2c_get_clientdata(client);
225 struct sensor_platform_data *pdata = sensor->pdata;
228 struct sensor_axis axis;
229 char buffer[6] = {0};
232 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
234 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
238 memset(buffer, 0, 6);
240 value = sensor_read_reg(client, LIS3DH_STATUS_REG);
241 if((value & 0x0f) == 0)
243 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
248 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
250 *buffer = sensor->ops->read_reg;
251 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
256 //this gsensor need 6 bytes buffer
257 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
258 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
259 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
261 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
262 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
263 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
265 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
267 //Report event only while value is changed to save some power
268 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))
270 gsensor_report_value(client, &axis);
272 /* »¥³âµØ»º´æÊý¾Ý. */
273 mutex_lock(&(sensor->data_mutex) );
275 mutex_unlock(&(sensor->data_mutex) );
278 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
281 value = sensor_read_reg(client, sensor->ops->int_status_reg);
282 DBG("%s:sensor int status :0x%x\n",__func__,value);
288 struct sensor_operate gsensor_lis3dh_ops = {
290 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
291 .id_i2c = ACCEL_ID_LIS3DH, //i2c id number
292 .read_reg = (LIS3DH_OUT_X_L | 0x80), //read data
293 .read_len = 6, //data length
294 .id_reg = LIS3DH_WHO_AM_I, //read device id from this register
295 .id_data = LIS3DH_DEVID, //device id
296 .precision = LIS3DH_PRECISION, //12 bits
297 .ctrl_reg = LIS3DH_CTRL_REG1, //enable or disable
298 .int_status_reg = LIS3DH_INT1_SRC, //intterupt status register
299 .range = {-LIS3DH_RANGE,LIS3DH_RANGE}, //range
300 .trig = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),
301 .active = sensor_active,
303 .report = sensor_report_value,
306 /****************operate according to sensor chip:end************/
308 //function name should not be changed
309 static struct sensor_operate *gsensor_get_ops(void)
311 return &gsensor_lis3dh_ops;
315 static int __init gsensor_lis3dh_init(void)
317 struct sensor_operate *ops = gsensor_get_ops();
319 int type = ops->type;
320 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
324 static void __exit gsensor_lis3dh_exit(void)
326 struct sensor_operate *ops = gsensor_get_ops();
327 int type = ops->type;
328 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
332 module_init(gsensor_lis3dh_init);
333 module_exit(gsensor_lis3dh_exit);