1 /* drivers/input/sensors/access/kxtik.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: Bruins <xwj@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.
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/irq.h>
21 #include <linux/miscdevice.h>
22 #include <linux/gpio.h>
23 #include <asm/uaccess.h>
24 #include <asm/atomic.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/workqueue.h>
28 #include <linux/freezer.h>
29 #include <linux/of_gpio.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
33 #include <linux/sensor-dev.h>
36 #define LSM303D_WHO_AM_I (0x0F)
38 /* full scale setting - register & mask */
39 #define LSM303D_CTRL_REG0 (0x1F)
40 #define LSM303D_CTRL_REG1 (0x20)
41 #define LSM303D_CTRL_REG2 (0x21)
42 #define LSM303D_CTRL_REG3 (0x22)
43 #define LSM303D_CTRL_REG4 (0x23)
44 #define LSM303D_CTRL_REG5 (0x24)
45 #define LSM303D_CTRL_REG6 (0x25)
46 #define LSM303D_CTRL_REG7 (0x26)
47 #define LSM303D_STATUS_REG (0x27)
48 #define LSM303D_OUT_X_L (0x28)
49 #define LSM303D_OUT_X_H (0x29)
50 #define LSM303D_OUT_Y_L (0x2a)
51 #define LSM303D_OUT_Y_H (0x2b)
52 #define LSM303D_OUT_Z_L (0x2c)
53 #define LSM303D_OUT_Z_H (0x2d)
54 #define LSM303D_FIFO_CTRL_REG (0x2E)
55 #define LSM303D_FIFO_SRC_REG (0X2F)
57 #define LSM303D_IG_CFG1 (0x30)
58 #define LSM303D_IG_SRC1 (0x31)
59 #define LSM303D_IG_THS1 (0x32)
60 #define LSM303D_IG_DURATION1 (0x33)
62 #define LSM303D_IG_CFG2 (0x34)
63 #define LSM303D_IG_SRC2 (0x35)
64 #define LSM303D_IG_THS2 (0x36)
65 #define LSM303D_IG_DURATION2 (0x37)
68 #define LSM303D_DEVID (0x49) //chip id
69 #define LSM303D_ACC_DISABLE (0x08)
71 #define LSM303D_RANGE 2000000
74 #define LSM303D_PRECISION 16
75 #define LSM303D_BOUNDARY (0x1 << (LSM303D_PRECISION - 1))
76 #define LSM303D_GRAVITY_STEP (LSM303D_RANGE / LSM303D_BOUNDARY)
78 #define ODR3P25 0x10 /* 3.25Hz output data rate */
79 #define ODR6P25 0x20 /* 6.25Hz output data rate */
80 #define ODR12P5 0x30 /* 12.5Hz output data rate */
81 #define ODR25 0x40 /* 25Hz output data rate */
82 #define ODR50 0x50 /* 50Hz output data rate */
83 #define ODR100 0x60 /* 100Hz output data rate */
84 #define ODR200 0x70 /* 200Hz output data rate */
85 #define ODR400 0x80 /* 400Hz output data rate */
86 #define ODR800 0x90 /* 800Hz output data rate */
87 #define ODR1600 0xA0 /* 1600Hz output data rate */
90 struct sensor_reg_data {
95 /****************operate according to sensor chip:start************/
96 static int sensor_active(struct i2c_client *client, int enable, int rate)
98 struct sensor_private_data *sensor =
99 (struct sensor_private_data *) i2c_get_clientdata(client);
103 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
105 sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down
107 //register setting according to chip datasheet
110 status = LSM303D_ACC_DISABLE; //lis3dh
111 sensor->ops->ctrl_data |= status;
115 status = ~LSM303D_ACC_DISABLE; //lis3dh
116 sensor->ops->ctrl_data &= status;
119 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
120 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
122 printk("%s:fail to active sensor\n",__func__);
129 static int sensor_init(struct i2c_client *client)
131 struct sensor_private_data *sensor =
132 (struct sensor_private_data *) i2c_get_clientdata(client);
136 struct sensor_reg_data reg_data[] =
138 {LSM303D_CTRL_REG0,0x00},
139 {LSM303D_CTRL_REG1,0x07},
140 {LSM303D_CTRL_REG2,0x00},
141 {LSM303D_CTRL_REG3,0x00},
142 {LSM303D_CTRL_REG4,0x00},
143 {LSM303D_CTRL_REG5,0x78}, //High resolution output mode:11,
144 {LSM303D_CTRL_REG6,0x20},
145 {LSM303D_CTRL_REG7,0x00},
146 {LSM303D_FIFO_CTRL_REG,0x00},
147 {LSM303D_IG_CFG1,0xFF}, //6 direction position recognition
148 {LSM303D_IG_THS1,0x7F}, //Interrupt 1 threshold
149 {LSM303D_IG_DURATION1,0x7F}, //Duration value 0x00->ox7f
152 {LSM303D_CTRL_REG7,0x00},
153 {LSM303D_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode
154 {LSM303D_CTRL_REG6,0x40},
156 {LSM303D_FIFO_CTRL_REG,0x00}, //
157 {LSM303D_IG_CFG1,0xFF}, //6 direction position recognition
158 {LSM303D_IG_THS1,0x7F}, //Interrupt 1 threshold
159 {LSM303D_IG_DURATION1,0x7F}, //Duration value 0x00->ox7f
163 result = sensor->ops->active(client,0,0);
166 printk("%s:line=%d,error\n",__func__,__LINE__);
170 sensor->status_cur = SENSOR_OFF;
172 for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
174 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
177 printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
183 if(sensor->pdata->irq_enable)
186 result = sensor_write_reg(client, LSM303D_CTRL_REG3, 0x20);
189 printk("%s:line=%d,error\n",__func__,__LINE__);
193 i = sensor_read_reg(client,LSM303D_CTRL_REG5);
195 result = sensor_write_reg(client, LSM303D_CTRL_REG5, (i|0x01));
198 printk("%s:line=%d,error\n",__func__,__LINE__);
208 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
211 struct sensor_private_data *sensor =
212 (struct sensor_private_data *) i2c_get_clientdata(client);
214 switch (sensor->devid) {
216 result = ((int)high_byte << 8) | (int)low_byte;
217 if (result < LSM303D_BOUNDARY)
218 result = result* LSM303D_GRAVITY_STEP;
220 result = ~( ((~result & (0x7fff>>(16-LSM303D_PRECISION)) ) + 1)
221 * LSM303D_GRAVITY_STEP) + 1;
225 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
232 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
234 struct sensor_private_data *sensor =
235 (struct sensor_private_data *) i2c_get_clientdata(client);
237 /* Report acceleration sensor information */
238 input_report_abs(sensor->input_dev, ABS_X, axis->x);
239 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
240 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
241 input_sync(sensor->input_dev);
242 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
247 #define GSENSOR_MIN 10
248 static int sensor_report_value(struct i2c_client *client)
250 struct sensor_private_data *sensor =
251 (struct sensor_private_data *) i2c_get_clientdata(client);
252 struct sensor_platform_data *pdata = sensor->pdata;
255 struct sensor_axis axis;
256 char buffer[6] = {0};
259 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
261 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
265 memset(buffer, 0, 6);
267 value = sensor_read_reg(client, LSM303D_STATUS_REG);
268 if((value & 0x0f) == 0)
270 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
275 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
277 *buffer = sensor->ops->read_reg;
278 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
283 //this gsensor need 6 bytes buffer
284 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
285 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
286 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
288 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
289 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
290 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
292 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
293 //printk( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
295 //Report event only while value is changed to save some power
296 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))
298 gsensor_report_value(client, &axis);
300 /* »¥³âµØ»º´æÊý¾Ý. */
301 mutex_lock(&(sensor->data_mutex) );
303 mutex_unlock(&(sensor->data_mutex) );
306 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
309 value = sensor_read_reg(client, sensor->ops->int_status_reg);
310 DBG("%s:sensor int status :0x%x\n",__func__,value);
316 struct sensor_operate gsensor_lsm303d_ops = {
318 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
319 .id_i2c = ACCEL_ID_LSM303D, //i2c id number
320 .read_reg = (LSM303D_OUT_X_L | 0x80), //read data
321 .read_len = 6, //data length
322 .id_reg = LSM303D_WHO_AM_I, //read device id from this register
323 .id_data = LSM303D_DEVID, //device id
324 .precision = LSM303D_PRECISION, //16 bits
325 .ctrl_reg = LSM303D_CTRL_REG1, //enable or disable
326 .int_status_reg = LSM303D_IG_SRC1, //intterupt status register
327 .range = {-LSM303D_RANGE,LSM303D_RANGE}, //range
328 .trig = (IRQF_TRIGGER_LOW|IRQF_ONESHOT),
329 .active = sensor_active,
331 .report = sensor_report_value,
335 /****************operate according to sensor chip:end************/
337 //function name should not be changed
338 static struct sensor_operate *gsensor_get_ops(void)
340 return &gsensor_lsm303d_ops;
344 static int __init gsensor_lis3dh_init(void)
346 struct sensor_operate *ops = gsensor_get_ops();
348 int type = ops->type;
349 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
353 static void __exit gsensor_lis3dh_exit(void)
355 struct sensor_operate *ops = gsensor_get_ops();
356 int type = ops->type;
357 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
361 module_init(gsensor_lis3dh_init);
362 module_exit(gsensor_lis3dh_exit);