1 /* drivers/input/sensors/access/kxtj9.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 KXTJ9_DEVID 0x09 //chip id
37 #define KXTJ9_RANGE 2000000
39 #define KXTJ9_XOUT_HPF_L (0x00) /* 0000 0000 */
40 #define KXTJ9_XOUT_HPF_H (0x01) /* 0000 0001 */
41 #define KXTJ9_YOUT_HPF_L (0x02) /* 0000 0010 */
42 #define KXTJ9_YOUT_HPF_H (0x03) /* 0000 0011 */
43 #define KXTJ9_ZOUT_HPF_L (0x04) /* 0001 0100 */
44 #define KXTJ9_ZOUT_HPF_H (0x05) /* 0001 0101 */
45 #define KXTJ9_XOUT_L (0x06) /* 0000 0110 */
46 #define KXTJ9_XOUT_H (0x07) /* 0000 0111 */
47 #define KXTJ9_YOUT_L (0x08) /* 0000 1000 */
48 #define KXTJ9_YOUT_H (0x09) /* 0000 1001 */
49 #define KXTJ9_ZOUT_L (0x0A) /* 0001 1010 */
50 #define KXTJ9_ZOUT_H (0x0B) /* 0001 1011 */
51 #define KXTJ9_ST_RESP (0x0C) /* 0000 1100 */
52 #define KXTJ9_WHO_AM_I (0x0F) /* 0000 1111 */
53 #define KXTJ9_TILT_POS_CUR (0x10) /* 0001 0000 */
54 #define KXTJ9_TILT_POS_PRE (0x11) /* 0001 0001 */
55 #define KXTJ9_INT_SRC_REG1 (0x15) /* 0001 0101 */
56 #define KXTJ9_INT_SRC_REG2 (0x16) /* 0001 0110 */
57 #define KXTJ9_STATUS_REG (0x18) /* 0001 1000 */
58 #define KXTJ9_INT_REL (0x1A) /* 0001 1010 */
59 #define KXTJ9_CTRL_REG1 (0x1B) /* 0001 1011 */
60 #define KXTJ9_CTRL_REG2 (0x1C) /* 0001 1100 */
61 #define KXTJ9_CTRL_REG3 (0x1D) /* 0001 1101 */
62 #define KXTJ9_INT_CTRL_REG1 (0x1E) /* 0001 1110 */
63 #define KXTJ9_INT_CTRL_REG2 (0x1F) /* 0001 1111 */
64 #define KXTJ9_INT_CTRL_REG3 (0x20) /* 0010 0000 */
65 #define KXTJ9_DATA_CTRL_REG (0x21) /* 0010 0001 */
66 #define KXTJ9_TILT_TIMER (0x28) /* 0010 1000 */
67 #define KXTJ9_WUF_TIMER (0x29) /* 0010 1001 */
68 #define KXTJ9_TDT_TIMER (0x2B) /* 0010 1011 */
69 #define KXTJ9_TDT_H_THRESH (0x2C) /* 0010 1100 */
70 #define KXTJ9_TDT_L_THRESH (0x2D) /* 0010 1101 */
71 #define KXTJ9_TDT_TAP_TIMER (0x2E) /* 0010 1110 */
72 #define KXTJ9_TDT_TOTAL_TIMER (0x2F) /* 0010 1111 */
73 #define KXTJ9_TDT_LATENCY_TIMER (0x30) /* 0011 0000 */
74 #define KXTJ9_TDT_WINDOW_TIMER (0x31) /* 0011 0001 */
75 #define KXTJ9_WUF_THRESH (0x5A) /* 0101 1010 */
76 #define KXTJ9_TILT_ANGLE (0x5C) /* 0101 1100 */
77 #define KXTJ9_HYST_SET (0x5F) /* 0101 1111 */
79 /* CONTROL REGISTER 1 BITS */
80 #define KXTJ9_DISABLE 0x7F
81 #define KXTJ9_ENABLE (1 << 7)
82 /* INPUT_ABS CONSTANTS */
85 /* RESUME STATE INDICES */
86 #define RES_DATA_CTRL 0
87 #define RES_CTRL_REG1 1
88 #define RES_INT_CTRL1 2
89 #define RESUME_ENTRIES 3
91 /* CTRL_REG1: set resolution, g-range, data ready enable */
92 /* Output resolution: 8-bit valid or 12-bit valid */
93 #define KXTJ9_RES_8BIT 0
94 #define KXTJ9_RES_12BIT (1 << 6)
95 /* Output g-range: +/-2g, 4g, or 8g */
97 #define KXTJ9_G_4G (1 << 3)
98 #define KXTJ9_G_8G (1 << 4)
100 /* DATA_CTRL_REG: controls the output data rate of the part */
101 #define KXTJ9_ODR12_5F 0
102 #define KXTJ9_ODR25F 1
103 #define KXTJ9_ODR50F 2
104 #define KXTJ9_ODR100F 3
105 #define KXTJ9_ODR200F 4
106 #define KXTJ9_ODR400F 5
107 #define KXTJ9_ODR800F 6
110 #define KXTJ9_PRECISION 12
111 #define KXTJ9_BOUNDARY (0x1 << (KXTJ9_PRECISION - 1))
112 #define KXTJ9_GRAVITY_STEP KXTJ9_RANGE / KXTJ9_BOUNDARY
115 /****************operate according to sensor chip:start************/
117 static int sensor_active(struct i2c_client *client, int enable, int rate)
119 struct sensor_private_data *sensor =
120 (struct sensor_private_data *) i2c_get_clientdata(client);
124 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
126 //register setting according to chip datasheet
129 status = KXTJ9_ENABLE; //kxtj9
130 sensor->ops->ctrl_data |= status;
134 status = ~KXTJ9_ENABLE; //kxtj9
135 sensor->ops->ctrl_data &= status;
138 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
139 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
141 printk("%s:fail to active sensor\n",__func__);
147 static int sensor_init(struct i2c_client *client)
149 struct sensor_private_data *sensor =
150 (struct sensor_private_data *) i2c_get_clientdata(client);
153 result = sensor->ops->active(client,0,0);
156 printk("%s:line=%d,error\n",__func__,__LINE__);
160 sensor->status_cur = SENSOR_OFF;
162 result = sensor_write_reg(client, KXTJ9_DATA_CTRL_REG, KXTJ9_ODR400F);
165 printk("%s:line=%d,error\n",__func__,__LINE__);
169 if(sensor->pdata->irq_enable) //open interrupt
171 result = sensor_write_reg(client, KXTJ9_INT_CTRL_REG1, 0x34);//enable int,active high,need read INT_REL
174 printk("%s:line=%d,error\n",__func__,__LINE__);
179 sensor->ops->ctrl_data = (KXTJ9_RES_12BIT | KXTJ9_G_2G);
180 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
183 printk("%s:line=%d,error\n",__func__,__LINE__);
190 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
193 struct sensor_private_data *sensor =
194 (struct sensor_private_data *) i2c_get_clientdata(client);
195 //int precision = sensor->ops->precision;
196 switch (sensor->devid) {
198 result = (((int)high_byte << 8) | ((int)low_byte ))>>4;
199 if (result < KXTJ9_BOUNDARY)
200 result = result* KXTJ9_GRAVITY_STEP;
202 result = ~( ((~result & (0x7fff>>(16-KXTJ9_PRECISION)) ) + 1)
203 * KXTJ9_GRAVITY_STEP) + 1;
207 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
214 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
216 struct sensor_private_data *sensor =
217 (struct sensor_private_data *) i2c_get_clientdata(client);
219 /* Report acceleration sensor information */
220 input_report_abs(sensor->input_dev, ABS_X, axis->x);
221 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
222 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
223 input_sync(sensor->input_dev);
224 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
229 #define GSENSOR_MIN 10
230 static int sensor_report_value(struct i2c_client *client)
232 struct sensor_private_data *sensor =
233 (struct sensor_private_data *) i2c_get_clientdata(client);
234 struct sensor_platform_data *pdata = sensor->pdata;
237 struct sensor_axis axis;
238 char buffer[6] = {0};
241 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
243 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
247 memset(buffer, 0, 6);
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 gsensor 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 gsensor_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 gsensor_kxtj9_ops = {
291 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
292 .id_i2c = ACCEL_ID_KXTJ9, //i2c id number
293 .read_reg = KXTJ9_XOUT_L, //read data
294 .read_len = 6, //data length
295 .id_reg = KXTJ9_WHO_AM_I, //read device id from this register
296 .id_data = KXTJ9_DEVID, //device id
297 .precision = KXTJ9_PRECISION, //12 bits
298 .ctrl_reg = KXTJ9_CTRL_REG1, //enable or disable
299 .int_status_reg = KXTJ9_INT_REL, //intterupt status register
300 .range = {-KXTJ9_RANGE,KXTJ9_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 *gsensor_get_ops(void)
312 return &gsensor_kxtj9_ops;
316 static int __init gsensor_kxtj9_init(void)
318 struct sensor_operate *ops = gsensor_get_ops();
320 int type = ops->type;
321 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
325 static void __exit gsensor_kxtj9_exit(void)
327 struct sensor_operate *ops = gsensor_get_ops();
328 int type = ops->type;
329 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
333 module_init(gsensor_kxtj9_init);
334 module_exit(gsensor_kxtj9_exit);