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 <mach/gpio.h>
29 #include <mach/board.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
33 #include <linux/sensor-dev.h>
36 #define LIS3DH_INT_COUNT (0x0E)
37 #define LIS3DH_WHO_AM_I (0x0F)
39 /* full scale setting - register & mask */
40 #define LIS3DH_TEMP_CFG_REG (0x1F)
41 #define LIS3DH_CTRL_REG1 (0x20)
42 #define LIS3DH_CTRL_REG2 (0x21)
43 #define LIS3DH_CTRL_REG3 (0x22)
44 #define LIS3DH_CTRL_REG4 (0x23)
45 #define LIS3DH_CTRL_REG5 (0x24)
46 #define LIS3DH_CTRL_REG6 (0x25)
47 #define LIS3DH_REFERENCE (0x26)
48 #define LIS3DH_STATUS_REG (0x27)
49 #define LIS3DH_OUT_X_L (0x28)
50 #define LIS3DH_OUT_X_H (0x29)
51 #define LIS3DH_OUT_Y_L (0x2a)
52 #define LIS3DH_OUT_Y_H (0x2b)
53 #define LIS3DH_OUT_Z_L (0x2c)
54 #define LIS3DH_OUT_Z_H (0x2d)
55 #define LIS3DH_FIFO_CTRL_REG (0x2E)
57 #define LIS3DH_INT1_CFG (0x30)
58 #define LIS3DH_INT1_SRC (0x31)
59 #define LIS3DH_INT1_THS (0x32)
60 #define LIS3DH_INT1_DURATION (0x33)
62 #define LIS3DH_DEVID (0x33) //chip id
63 #define LIS3DH_ACC_DISABLE (0x08)
65 #define LIS3DH_RANGE 2000000
68 #define LIS3DH_PRECISION 16
69 #define LIS3DH_BOUNDARY (0x1 << (LIS3DH_PRECISION - 1))
70 #define LIS3DH_GRAVITY_STEP (LIS3DH_RANGE / LIS3DH_BOUNDARY)
72 #define ODR1 0x10 /* 1Hz output data rate */
73 #define ODR10 0x20 /* 10Hz output data rate */
74 #define ODR25 0x30 /* 25Hz output data rate */
75 #define ODR50 0x40 /* 50Hz output data rate */
76 #define ODR100 0x50 /* 100Hz output data rate */
77 #define ODR200 0x60 /* 200Hz output data rate */
78 #define ODR400 0x70 /* 400Hz output data rate */
79 #define ODR1250 0x90 /* 1250Hz output data rate */
83 struct sensor_reg_data {
88 /****************operate according to sensor chip:start************/
90 static int sensor_active(struct i2c_client *client, int enable, int rate)
92 struct sensor_private_data *sensor =
93 (struct sensor_private_data *) i2c_get_clientdata(client);
97 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
99 sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down
101 //register setting according to chip datasheet
104 status = LIS3DH_ACC_DISABLE; //lis3dh
105 sensor->ops->ctrl_data |= status;
109 status = ~LIS3DH_ACC_DISABLE; //lis3dh
110 sensor->ops->ctrl_data &= status;
113 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
114 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
116 printk("%s:fail to active sensor\n",__func__);
122 static int sensor_init(struct i2c_client *client)
124 struct sensor_private_data *sensor =
125 (struct sensor_private_data *) i2c_get_clientdata(client);
128 struct sensor_reg_data reg_data[] =
130 {LIS3DH_CTRL_REG2,0X00},
131 {LIS3DH_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode
132 {LIS3DH_CTRL_REG6,0x40},
133 {LIS3DH_TEMP_CFG_REG,0x00}, //
134 {LIS3DH_FIFO_CTRL_REG,0x00}, //
135 {LIS3DH_INT1_CFG,0xFF}, //6 direction position recognition
136 {LIS3DH_INT1_THS,0x7F}, //Interrupt 1 threshold
137 {LIS3DH_INT1_DURATION,0x7F}, //Duration value 0x00->ox7f
140 result = sensor->ops->active(client,0,0);
143 printk("%s:line=%d,error\n",__func__,__LINE__);
147 sensor->status_cur = SENSOR_OFF;
149 for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
151 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
154 printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
160 if(sensor->pdata->irq_enable)
163 result = sensor_write_reg(client, LIS3DH_CTRL_REG3, 0x40);//I1_AOI1 =1 if motion
166 printk("%s:line=%d,error\n",__func__,__LINE__);
170 result = sensor_write_reg(client, LIS3DH_CTRL_REG5, 0x08);
173 printk("%s:line=%d,error\n",__func__,__LINE__);
182 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
185 struct sensor_private_data *sensor =
186 (struct sensor_private_data *) i2c_get_clientdata(client);
187 //int precision = sensor->ops->precision;
188 switch (sensor->devid) {
190 result = ((int)high_byte << 8) | (int)low_byte;
191 if (result < LIS3DH_BOUNDARY)
192 result = result* LIS3DH_GRAVITY_STEP;
194 result = ~( ((~result & (0x7fff>>(16-LIS3DH_PRECISION)) ) + 1)
195 * LIS3DH_GRAVITY_STEP) + 1;
199 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
206 static int angle_report_value(struct i2c_client *client, struct sensor_axis *axis)
208 struct sensor_private_data *sensor =
209 (struct sensor_private_data *) i2c_get_clientdata(client);
211 /* Report acceleration sensor information */
212 input_report_abs(sensor->input_dev, ABS_X, axis->x);
213 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
214 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
215 input_sync(sensor->input_dev);
216 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
221 #define GSENSOR_MIN 10
222 static int sensor_report_value(struct i2c_client *client)
224 struct sensor_private_data *sensor =
225 (struct sensor_private_data *) i2c_get_clientdata(client);
226 struct sensor_platform_data *pdata = sensor->pdata;
229 struct sensor_axis axis;
230 char buffer[6] = {0};
233 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
235 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
239 memset(buffer, 0, 6);
241 value = sensor_read_reg(client, LIS3DH_STATUS_REG);
242 if((value & 0x0f) == 0)
244 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
249 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
251 *buffer = sensor->ops->read_reg;
252 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
257 //this angle need 6 bytes buffer
258 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
259 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
260 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
262 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
263 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
264 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
266 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
268 //Report event only while value is changed to save some power
269 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))
271 angle_report_value(client, &axis);
273 /* »¥³âµØ»º´æÊý¾Ý. */
274 mutex_lock(&(sensor->data_mutex) );
276 mutex_unlock(&(sensor->data_mutex) );
279 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
282 value = sensor_read_reg(client, sensor->ops->int_status_reg);
283 DBG("%s:sensor int status :0x%x\n",__func__,value);
289 struct sensor_operate angle_lis3dh_ops = {
290 .name = "angle_lis3dh",
291 .type = SENSOR_TYPE_ANGLE, //sensor type and it should be correct
292 .id_i2c = ANGLE_ID_LIS3DH, //i2c id number
293 .read_reg = (LIS3DH_OUT_X_L | 0x80), //read data
294 .read_len = 6, //data length
295 .id_reg = LIS3DH_WHO_AM_I, //read device id from this register
296 .id_data = LIS3DH_DEVID, //device id
297 .precision = LIS3DH_PRECISION, //12 bits
298 .ctrl_reg = LIS3DH_CTRL_REG1, //enable or disable
299 .int_status_reg = LIS3DH_INT1_SRC, //intterupt status register
300 .range = {-LIS3DH_RANGE,LIS3DH_RANGE}, //range
301 .trig = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),
302 .active = sensor_active,
304 .report = sensor_report_value,
307 /****************operate according to sensor chip:end************/
309 //function name should not be changed
310 static struct sensor_operate *angle_get_ops(void)
312 return &angle_lis3dh_ops;
316 static int __init angle_lis3dh_init(void)
318 struct sensor_operate *ops = angle_get_ops();
320 int type = ops->type;
321 result = sensor_register_slave(type, NULL, NULL, angle_get_ops);
325 static void __exit angle_lis3dh_exit(void)
327 struct sensor_operate *ops = angle_get_ops();
328 int type = ops->type;
329 sensor_unregister_slave(type, NULL, NULL, angle_get_ops);
333 module_init(angle_lis3dh_init);
334 module_exit(angle_lis3dh_exit);