1*4882a593Smuzhiyun /* drivers/input/sensors/access/kxtik.c
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (C) 2012-2015 ROCKCHIP.
4*4882a593Smuzhiyun * Author: luowei <lw@rock-chips.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This software is licensed under the terms of the GNU General Public
7*4882a593Smuzhiyun * License version 2, as published by the Free Software Foundation, and
8*4882a593Smuzhiyun * may be copied, distributed, and modified under those terms.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*4882a593Smuzhiyun * GNU General Public License for more details.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/irq.h>
20*4882a593Smuzhiyun #include <linux/miscdevice.h>
21*4882a593Smuzhiyun #include <linux/gpio.h>
22*4882a593Smuzhiyun #include <linux/uaccess.h>
23*4882a593Smuzhiyun #include <asm/atomic.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun #include <linux/input.h>
26*4882a593Smuzhiyun #include <linux/workqueue.h>
27*4882a593Smuzhiyun #include <linux/freezer.h>
28*4882a593Smuzhiyun #include <linux/of_gpio.h>
29*4882a593Smuzhiyun #ifdef CONFIG_HAS_EARLYSUSPEND
30*4882a593Smuzhiyun #include <linux/earlysuspend.h>
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun #include <linux/sensor-dev.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define LIS3DH_INT_COUNT (0x0E)
36*4882a593Smuzhiyun #define LIS3DH_WHO_AM_I (0x0F)
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* full scale setting - register & mask */
39*4882a593Smuzhiyun #define LIS3DH_TEMP_CFG_REG (0x1F)
40*4882a593Smuzhiyun #define LIS3DH_CTRL_REG1 (0x20)
41*4882a593Smuzhiyun #define LIS3DH_CTRL_REG2 (0x21)
42*4882a593Smuzhiyun #define LIS3DH_CTRL_REG3 (0x22)
43*4882a593Smuzhiyun #define LIS3DH_CTRL_REG4 (0x23)
44*4882a593Smuzhiyun #define LIS3DH_CTRL_REG5 (0x24)
45*4882a593Smuzhiyun #define LIS3DH_CTRL_REG6 (0x25)
46*4882a593Smuzhiyun #define LIS3DH_REFERENCE (0x26)
47*4882a593Smuzhiyun #define LIS3DH_STATUS_REG (0x27)
48*4882a593Smuzhiyun #define LIS3DH_OUT_X_L (0x28)
49*4882a593Smuzhiyun #define LIS3DH_OUT_X_H (0x29)
50*4882a593Smuzhiyun #define LIS3DH_OUT_Y_L (0x2a)
51*4882a593Smuzhiyun #define LIS3DH_OUT_Y_H (0x2b)
52*4882a593Smuzhiyun #define LIS3DH_OUT_Z_L (0x2c)
53*4882a593Smuzhiyun #define LIS3DH_OUT_Z_H (0x2d)
54*4882a593Smuzhiyun #define LIS3DH_FIFO_CTRL_REG (0x2E)
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #define LIS3DH_INT1_CFG (0x30)
57*4882a593Smuzhiyun #define LIS3DH_INT1_SRC (0x31)
58*4882a593Smuzhiyun #define LIS3DH_INT1_THS (0x32)
59*4882a593Smuzhiyun #define LIS3DH_INT1_DURATION (0x33)
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define LIS3DH_DEVID (0x33) //chip id
62*4882a593Smuzhiyun #define LIS3DH_ACC_DISABLE (0x08)
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define LIS3DH_RANGE 2000000
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* LIS3DH */
67*4882a593Smuzhiyun #define LIS3DH_PRECISION 16
68*4882a593Smuzhiyun #define LIS3DH_BOUNDARY (0x1 << (LIS3DH_PRECISION - 1))
69*4882a593Smuzhiyun #define LIS3DH_GRAVITY_STEP (LIS3DH_RANGE / LIS3DH_BOUNDARY)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define ODR1 0x10 /* 1Hz output data rate */
72*4882a593Smuzhiyun #define ODR10 0x20 /* 10Hz output data rate */
73*4882a593Smuzhiyun #define ODR25 0x30 /* 25Hz output data rate */
74*4882a593Smuzhiyun #define ODR50 0x40 /* 50Hz output data rate */
75*4882a593Smuzhiyun #define ODR100 0x50 /* 100Hz output data rate */
76*4882a593Smuzhiyun #define ODR200 0x60 /* 200Hz output data rate */
77*4882a593Smuzhiyun #define ODR400 0x70 /* 400Hz output data rate */
78*4882a593Smuzhiyun #define ODR1250 0x90 /* 1250Hz output data rate */
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun struct sensor_reg_data {
83*4882a593Smuzhiyun char reg;
84*4882a593Smuzhiyun char data;
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /****************operate according to sensor chip:start************/
88*4882a593Smuzhiyun
sensor_active(struct i2c_client * client,int enable,int rate)89*4882a593Smuzhiyun static int sensor_active(struct i2c_client *client, int enable, int rate)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct sensor_private_data *sensor =
92*4882a593Smuzhiyun (struct sensor_private_data *) i2c_get_clientdata(client);
93*4882a593Smuzhiyun int result = 0;
94*4882a593Smuzhiyun int status = 0;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun //register setting according to chip datasheet
101*4882a593Smuzhiyun if(!enable)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun status = LIS3DH_ACC_DISABLE; //lis3dh
104*4882a593Smuzhiyun sensor->ops->ctrl_data |= status;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun else
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun status = ~LIS3DH_ACC_DISABLE; //lis3dh
109*4882a593Smuzhiyun sensor->ops->ctrl_data &= status;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
113*4882a593Smuzhiyun result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
114*4882a593Smuzhiyun if(result)
115*4882a593Smuzhiyun printk("%s:fail to active sensor\n",__func__);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun return result;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
sensor_init(struct i2c_client * client)121*4882a593Smuzhiyun static int sensor_init(struct i2c_client *client)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun struct sensor_private_data *sensor =
124*4882a593Smuzhiyun (struct sensor_private_data *) i2c_get_clientdata(client);
125*4882a593Smuzhiyun int result = 0;
126*4882a593Smuzhiyun int i;
127*4882a593Smuzhiyun struct sensor_reg_data reg_data[] =
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun {LIS3DH_CTRL_REG2,0X00},
130*4882a593Smuzhiyun {LIS3DH_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode
131*4882a593Smuzhiyun {LIS3DH_CTRL_REG6,0x40},
132*4882a593Smuzhiyun {LIS3DH_TEMP_CFG_REG,0x00}, //
133*4882a593Smuzhiyun {LIS3DH_FIFO_CTRL_REG,0x00}, //
134*4882a593Smuzhiyun {LIS3DH_INT1_CFG,0xFF}, //6 direction position recognition
135*4882a593Smuzhiyun {LIS3DH_INT1_THS,0x7F}, //Interrupt 1 threshold
136*4882a593Smuzhiyun {LIS3DH_INT1_DURATION,0x7F}, //Duration value 0x00->ox7f
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun result = sensor->ops->active(client,0,0);
140*4882a593Smuzhiyun if(result)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun printk("%s:line=%d,error\n",__func__,__LINE__);
143*4882a593Smuzhiyun return result;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun sensor->status_cur = SENSOR_OFF;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
151*4882a593Smuzhiyun if(result)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
154*4882a593Smuzhiyun return result;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if(sensor->pdata->irq_enable)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun result = sensor_write_reg(client, LIS3DH_CTRL_REG3, 0x40);//I1_AOI1 =1 if motion
163*4882a593Smuzhiyun if(result)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun printk("%s:line=%d,error\n",__func__,__LINE__);
166*4882a593Smuzhiyun return result;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun result = sensor_write_reg(client, LIS3DH_CTRL_REG5, 0x08);
170*4882a593Smuzhiyun if(result)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun printk("%s:line=%d,error\n",__func__,__LINE__);
173*4882a593Smuzhiyun return result;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun return result;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)181*4882a593Smuzhiyun static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun s64 result;
184*4882a593Smuzhiyun struct sensor_private_data *sensor =
185*4882a593Smuzhiyun (struct sensor_private_data *) i2c_get_clientdata(client);
186*4882a593Smuzhiyun /* int precision = sensor->ops->precision; */
187*4882a593Smuzhiyun switch (sensor->devid) {
188*4882a593Smuzhiyun case LIS3DH_DEVID:
189*4882a593Smuzhiyun result = ((int)high_byte << 8) | (int)low_byte;
190*4882a593Smuzhiyun if (result < LIS3DH_BOUNDARY)
191*4882a593Smuzhiyun result = result * LIS3DH_GRAVITY_STEP;
192*4882a593Smuzhiyun else
193*4882a593Smuzhiyun result = ~(((~result & (0x7fff >> (16 - LIS3DH_PRECISION))) + 1)
194*4882a593Smuzhiyun * LIS3DH_GRAVITY_STEP) + 1;
195*4882a593Smuzhiyun break;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun default:
198*4882a593Smuzhiyun printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
199*4882a593Smuzhiyun return -EFAULT;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun return (int)result;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
angle_report_value(struct i2c_client * client,struct sensor_axis * axis)205*4882a593Smuzhiyun static int angle_report_value(struct i2c_client *client, struct sensor_axis *axis)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct sensor_private_data *sensor =
208*4882a593Smuzhiyun (struct sensor_private_data *) i2c_get_clientdata(client);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /* Report acceleration sensor information */
211*4882a593Smuzhiyun input_report_abs(sensor->input_dev, ABS_X, axis->x);
212*4882a593Smuzhiyun input_report_abs(sensor->input_dev, ABS_Y, axis->y);
213*4882a593Smuzhiyun input_report_abs(sensor->input_dev, ABS_Z, axis->z);
214*4882a593Smuzhiyun input_sync(sensor->input_dev);
215*4882a593Smuzhiyun DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun #define GSENSOR_MIN 10
sensor_report_value(struct i2c_client * client)221*4882a593Smuzhiyun static int sensor_report_value(struct i2c_client *client)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct sensor_private_data *sensor =
224*4882a593Smuzhiyun (struct sensor_private_data *) i2c_get_clientdata(client);
225*4882a593Smuzhiyun struct sensor_platform_data *pdata = sensor->pdata;
226*4882a593Smuzhiyun int ret = 0;
227*4882a593Smuzhiyun int x,y,z;
228*4882a593Smuzhiyun struct sensor_axis axis;
229*4882a593Smuzhiyun char buffer[6] = {0};
230*4882a593Smuzhiyun char value = 0;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
235*4882a593Smuzhiyun return -1;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun memset(buffer, 0, 6);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun value = sensor_read_reg(client, LIS3DH_STATUS_REG);
241*4882a593Smuzhiyun if((value & 0x0f) == 0)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
244*4882a593Smuzhiyun return -1;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
249*4882a593Smuzhiyun do {
250*4882a593Smuzhiyun *buffer = sensor->ops->read_reg;
251*4882a593Smuzhiyun ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
252*4882a593Smuzhiyun if (ret < 0)
253*4882a593Smuzhiyun return ret;
254*4882a593Smuzhiyun } while (0);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun //this angle need 6 bytes buffer
257*4882a593Smuzhiyun x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
258*4882a593Smuzhiyun y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
259*4882a593Smuzhiyun z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
262*4882a593Smuzhiyun axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
263*4882a593Smuzhiyun axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun //Report event only while value is changed to save some power
268*4882a593Smuzhiyun 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))
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun angle_report_value(client, &axis);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* ����ػ�������. */
273*4882a593Smuzhiyun mutex_lock(&(sensor->data_mutex) );
274*4882a593Smuzhiyun sensor->axis = axis;
275*4882a593Smuzhiyun mutex_unlock(&(sensor->data_mutex) );
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun value = sensor_read_reg(client, sensor->ops->int_status_reg);
282*4882a593Smuzhiyun DBG("%s:sensor int status :0x%x\n",__func__,value);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return ret;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun static struct sensor_operate angle_lis3dh_ops = {
289*4882a593Smuzhiyun .name = "angle_lis3dh",
290*4882a593Smuzhiyun .type = SENSOR_TYPE_ANGLE, //sensor type and it should be correct
291*4882a593Smuzhiyun .id_i2c = ANGLE_ID_LIS3DH, //i2c id number
292*4882a593Smuzhiyun .read_reg = (LIS3DH_OUT_X_L | 0x80), //read data
293*4882a593Smuzhiyun .read_len = 6, //data length
294*4882a593Smuzhiyun .id_reg = LIS3DH_WHO_AM_I, //read device id from this register
295*4882a593Smuzhiyun .id_data = LIS3DH_DEVID, //device id
296*4882a593Smuzhiyun .precision = LIS3DH_PRECISION, //12 bits
297*4882a593Smuzhiyun .ctrl_reg = LIS3DH_CTRL_REG1, //enable or disable
298*4882a593Smuzhiyun .int_status_reg = LIS3DH_INT1_SRC, //intterupt status register
299*4882a593Smuzhiyun .range = {-LIS3DH_RANGE,LIS3DH_RANGE}, //range
300*4882a593Smuzhiyun .trig = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),
301*4882a593Smuzhiyun .active = sensor_active,
302*4882a593Smuzhiyun .init = sensor_init,
303*4882a593Smuzhiyun .report = sensor_report_value,
304*4882a593Smuzhiyun };
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /****************operate according to sensor chip:end************/
angle_lis3dh_probe(struct i2c_client * client,const struct i2c_device_id * devid)307*4882a593Smuzhiyun static int angle_lis3dh_probe(struct i2c_client *client,
308*4882a593Smuzhiyun const struct i2c_device_id *devid)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun return sensor_register_device(client, NULL, devid, &angle_lis3dh_ops);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
angle_lis3dh_remove(struct i2c_client * client)313*4882a593Smuzhiyun static int angle_lis3dh_remove(struct i2c_client *client)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun return sensor_unregister_device(client, NULL, &angle_lis3dh_ops);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun static const struct i2c_device_id angle_lis3dh_id[] = {
319*4882a593Smuzhiyun {"angle_lis3dh", ANGLE_ID_LIS3DH},
320*4882a593Smuzhiyun {}
321*4882a593Smuzhiyun };
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun static struct i2c_driver angle_lis3dh_driver = {
324*4882a593Smuzhiyun .probe = angle_lis3dh_probe,
325*4882a593Smuzhiyun .remove = angle_lis3dh_remove,
326*4882a593Smuzhiyun .shutdown = sensor_shutdown,
327*4882a593Smuzhiyun .id_table = angle_lis3dh_id,
328*4882a593Smuzhiyun .driver = {
329*4882a593Smuzhiyun .name = "angle_lis3dh",
330*4882a593Smuzhiyun #ifdef CONFIG_PM
331*4882a593Smuzhiyun .pm = &sensor_pm_ops,
332*4882a593Smuzhiyun #endif
333*4882a593Smuzhiyun },
334*4882a593Smuzhiyun };
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun module_i2c_driver(angle_lis3dh_driver);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun MODULE_AUTHOR("luowei <lw@rock-chips.com>");
339*4882a593Smuzhiyun MODULE_DESCRIPTION("lis3dh angle driver");
340*4882a593Smuzhiyun MODULE_LICENSE("GPL");
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun
343