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>
35 #define KXTIK_DEVID_1004 0x05 //chip id
36 #define KXTIK_DEVID_J9_1005 0x07 //chip id
37 #define KXTIK_DEVID_J2_1009 0x09 //chip id
38 #define KXTIK_DEVID_1013 0x11 //chip id
39 #define KXTIK_RANGE 2000000
41 #define KXTIK_XOUT_HPF_L (0x00) /* 0000 0000 */
42 #define KXTIK_XOUT_HPF_H (0x01) /* 0000 0001 */
43 #define KXTIK_YOUT_HPF_L (0x02) /* 0000 0010 */
44 #define KXTIK_YOUT_HPF_H (0x03) /* 0000 0011 */
45 #define KXTIK_ZOUT_HPF_L (0x04) /* 0001 0100 */
46 #define KXTIK_ZOUT_HPF_H (0x05) /* 0001 0101 */
47 #define KXTIK_XOUT_L (0x06) /* 0000 0110 */
48 #define KXTIK_XOUT_H (0x07) /* 0000 0111 */
49 #define KXTIK_YOUT_L (0x08) /* 0000 1000 */
50 #define KXTIK_YOUT_H (0x09) /* 0000 1001 */
51 #define KXTIK_ZOUT_L (0x0A) /* 0001 1010 */
52 #define KXTIK_ZOUT_H (0x0B) /* 0001 1011 */
53 #define KXTIK_ST_RESP (0x0C) /* 0000 1100 */
54 #define KXTIK_WHO_AM_I (0x0F) /* 0000 1111 */
55 #define KXTIK_TILT_POS_CUR (0x10) /* 0001 0000 */
56 #define KXTIK_TILT_POS_PRE (0x11) /* 0001 0001 */
57 #define KXTIK_INT_SRC_REG1 (0x15) /* 0001 0101 */
58 #define KXTIK_INT_SRC_REG2 (0x16) /* 0001 0110 */
59 #define KXTIK_STATUS_REG (0x18) /* 0001 1000 */
60 #define KXTIK_INT_REL (0x1A) /* 0001 1010 */
61 #define KXTIK_CTRL_REG1 (0x1B) /* 0001 1011 */
62 #define KXTIK_CTRL_REG2 (0x1C) /* 0001 1100 */
63 #define KXTIK_CTRL_REG3 (0x1D) /* 0001 1101 */
64 #define KXTIK_INT_CTRL_REG1 (0x1E) /* 0001 1110 */
65 #define KXTIK_INT_CTRL_REG2 (0x1F) /* 0001 1111 */
66 #define KXTIK_INT_CTRL_REG3 (0x20) /* 0010 0000 */
67 #define KXTIK_DATA_CTRL_REG (0x21) /* 0010 0001 */
68 #define KXTIK_TILT_TIMER (0x28) /* 0010 1000 */
69 #define KXTIK_WUF_TIMER (0x29) /* 0010 1001 */
70 #define KXTIK_TDT_TIMER (0x2B) /* 0010 1011 */
71 #define KXTIK_TDT_H_THRESH (0x2C) /* 0010 1100 */
72 #define KXTIK_TDT_L_THRESH (0x2D) /* 0010 1101 */
73 #define KXTIK_TDT_TAP_TIMER (0x2E) /* 0010 1110 */
74 #define KXTIK_TDT_TOTAL_TIMER (0x2F) /* 0010 1111 */
75 #define KXTIK_TDT_LATENCY_TIMER (0x30) /* 0011 0000 */
76 #define KXTIK_TDT_WINDOW_TIMER (0x31) /* 0011 0001 */
77 #define KXTIK_WUF_THRESH (0x5A) /* 0101 1010 */
78 #define KXTIK_TILT_ANGLE (0x5C) /* 0101 1100 */
79 #define KXTIK_HYST_SET (0x5F) /* 0101 1111 */
81 /* CONTROL REGISTER 1 BITS */
82 #define KXTIK_DISABLE 0x7F
83 #define KXTIK_ENABLE (1 << 7)
84 /* INPUT_ABS CONSTANTS */
87 /* RESUME STATE INDICES */
88 #define RES_DATA_CTRL 0
89 #define RES_CTRL_REG1 1
90 #define RES_INT_CTRL1 2
91 #define RESUME_ENTRIES 3
93 /* CTRL_REG1: set resolution, g-range, data ready enable */
94 /* Output resolution: 8-bit valid or 12-bit valid */
95 #define KXTIK_RES_8BIT 0
96 #define KXTIK_RES_12BIT (1 << 6)
97 /* Output g-range: +/-2g, 4g, or 8g */
99 #define KXTIK_G_4G (1 << 3)
100 #define KXTIK_G_8G (1 << 4)
102 /* DATA_CTRL_REG: controls the output data rate of the part */
103 #define KXTIK_ODR12_5F 0
104 #define KXTIK_ODR25F 1
105 #define KXTIK_ODR50F 2
106 #define KXTIK_ODR100F 3
107 #define KXTIK_ODR200F 4
108 #define KXTIK_ODR400F 5
109 #define KXTIK_ODR800F 6
112 #define KXTIK_PRECISION 12
113 #define KXTIK_BOUNDARY (0x1 << (KXTIK_PRECISION - 1))
114 #define KXTIK_GRAVITY_STEP KXTIK_RANGE / KXTIK_BOUNDARY
117 /****************operate according to sensor chip:start************/
119 static int sensor_active(struct i2c_client *client, int enable, int rate)
121 struct sensor_private_data *sensor =
122 (struct sensor_private_data *) i2c_get_clientdata(client);
126 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
128 //register setting according to chip datasheet
131 status = KXTIK_ENABLE; //kxtik
132 sensor->ops->ctrl_data |= status;
136 status = ~KXTIK_ENABLE; //kxtik
137 sensor->ops->ctrl_data &= status;
140 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
141 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
143 printk("%s:fail to active sensor\n",__func__);
149 static int sensor_init(struct i2c_client *client)
151 struct sensor_private_data *sensor =
152 (struct sensor_private_data *) i2c_get_clientdata(client);
155 unsigned char id_reg = KXTIK_WHO_AM_I;
156 unsigned char id_data = 0;
158 result = sensor->ops->active(client,0,0);
161 printk("%s:line=%d,error\n",__func__,__LINE__);
165 sensor->status_cur = SENSOR_OFF;
169 result = sensor_rx_data(client, &id_reg, 1);
177 printk("%s:fail to read id,result=%d\n",__func__, result);
181 sensor->devid = id_data;
183 result = sensor_write_reg(client, KXTIK_DATA_CTRL_REG, KXTIK_ODR400F);
186 printk("%s:line=%d,error\n",__func__,__LINE__);
190 if(sensor->pdata->irq_enable) //open interrupt
193 if (id_data == KXTIK_DEVID_1004)
194 result = sensor_write_reg(client, KXTIK_INT_CTRL_REG1, 0x34);//enable int,active high,need read INT_REL
196 result = sensor_write_reg(client, KXTIK_INT_CTRL_REG1, 0x30);//enable int,active high,need read INT_REL
200 printk("%s:line=%d,error\n",__func__,__LINE__);
205 sensor->ops->ctrl_data = (KXTIK_RES_12BIT | KXTIK_G_2G);
206 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
209 printk("%s:line=%d,error\n",__func__,__LINE__);
213 printk("%s:%s id=0x%x\n",__func__,sensor->ops->name, id_data);
217 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
220 struct sensor_private_data *sensor =
221 (struct sensor_private_data *) i2c_get_clientdata(client);
222 //int precision = sensor->ops->precision;
223 switch (sensor->devid) {
224 case KXTIK_DEVID_1004:
225 case KXTIK_DEVID_1013:
226 case KXTIK_DEVID_J9_1005:
227 case KXTIK_DEVID_J2_1009:
228 result = (((int)high_byte << 8) | ((int)low_byte ))>>4;
229 if (result < KXTIK_BOUNDARY)
230 result = result* KXTIK_GRAVITY_STEP;
232 result = ~( ((~result & (0x7fff>>(16-KXTIK_PRECISION)) ) + 1)
233 * KXTIK_GRAVITY_STEP) + 1;
237 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
243 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
245 struct sensor_private_data *sensor =
246 (struct sensor_private_data *) i2c_get_clientdata(client);
248 /* Report acceleration sensor information */
249 input_report_abs(sensor->input_dev, ABS_X, axis->x);
250 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
251 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
252 input_sync(sensor->input_dev);
253 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
258 #define GSENSOR_MIN 10
259 static int sensor_report_value(struct i2c_client *client)
261 struct sensor_private_data *sensor =
262 (struct sensor_private_data *) i2c_get_clientdata(client);
263 struct sensor_platform_data *pdata = sensor->pdata;
266 struct sensor_axis axis;
267 char buffer[6] = {0};
270 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
272 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
276 memset(buffer, 0, 6);
278 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
280 *buffer = sensor->ops->read_reg;
281 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
286 //this gsensor need 6 bytes buffer
287 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
288 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
289 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
291 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
292 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
293 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
295 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
297 //Report event only while value is changed to save some power
298 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))
300 gsensor_report_value(client, &axis);
302 /* »¥³âµØ»º´æÊý¾Ý. */
303 mutex_lock(&(sensor->data_mutex) );
305 mutex_unlock(&(sensor->data_mutex) );
308 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
311 value = sensor_read_reg(client, sensor->ops->int_status_reg);
312 DBG("%s:sensor int status :0x%x\n",__func__,value);
318 struct sensor_operate gsensor_kxtik_ops = {
320 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
321 .id_i2c = ACCEL_ID_KXTIK, //i2c id number
322 .read_reg = KXTIK_XOUT_L, //read data
323 .read_len = 6, //data length
324 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
325 .id_data = SENSOR_UNKNOW_DATA,
326 .precision = KXTIK_PRECISION, //12 bits
327 .ctrl_reg = KXTIK_CTRL_REG1, //enable or disable
328 .int_status_reg = KXTIK_INT_REL, //intterupt status register
329 .range = {-KXTIK_RANGE,KXTIK_RANGE}, //range
330 .trig = IRQF_TRIGGER_LOW|IRQF_ONESHOT,
331 .active = sensor_active,
333 .report = sensor_report_value,
336 /****************operate according to sensor chip:end************/
338 //function name should not be changed
339 static struct sensor_operate *gsensor_get_ops(void)
341 return &gsensor_kxtik_ops;
345 static int __init gsensor_kxtik_init(void)
347 struct sensor_operate *ops = gsensor_get_ops();
349 int type = ops->type;
350 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
354 static void __exit gsensor_kxtik_exit(void)
356 struct sensor_operate *ops = gsensor_get_ops();
357 int type = ops->type;
358 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
362 module_init(gsensor_kxtik_init);
363 module_exit(gsensor_kxtik_exit);