1 /* drivers/input/sensors/access/kxtj9.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
5 *
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.
9 *
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.
14 *
15 */
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 <linux/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 <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32 #include <linux/sensor-dev.h>
33
34
35 #define KXTJ9_DEVID 0x09 //chip id
36 #define KXTJ9_RANGE (2 * 16384)
37
38 #define KXTJ9_XOUT_HPF_L (0x00) /* 0000 0000 */
39 #define KXTJ9_XOUT_HPF_H (0x01) /* 0000 0001 */
40 #define KXTJ9_YOUT_HPF_L (0x02) /* 0000 0010 */
41 #define KXTJ9_YOUT_HPF_H (0x03) /* 0000 0011 */
42 #define KXTJ9_ZOUT_HPF_L (0x04) /* 0001 0100 */
43 #define KXTJ9_ZOUT_HPF_H (0x05) /* 0001 0101 */
44 #define KXTJ9_XOUT_L (0x06) /* 0000 0110 */
45 #define KXTJ9_XOUT_H (0x07) /* 0000 0111 */
46 #define KXTJ9_YOUT_L (0x08) /* 0000 1000 */
47 #define KXTJ9_YOUT_H (0x09) /* 0000 1001 */
48 #define KXTJ9_ZOUT_L (0x0A) /* 0001 1010 */
49 #define KXTJ9_ZOUT_H (0x0B) /* 0001 1011 */
50 #define KXTJ9_ST_RESP (0x0C) /* 0000 1100 */
51 #define KXTJ9_WHO_AM_I (0x0F) /* 0000 1111 */
52 #define KXTJ9_TILT_POS_CUR (0x10) /* 0001 0000 */
53 #define KXTJ9_TILT_POS_PRE (0x11) /* 0001 0001 */
54 #define KXTJ9_INT_SRC_REG1 (0x15) /* 0001 0101 */
55 #define KXTJ9_INT_SRC_REG2 (0x16) /* 0001 0110 */
56 #define KXTJ9_STATUS_REG (0x18) /* 0001 1000 */
57 #define KXTJ9_INT_REL (0x1A) /* 0001 1010 */
58 #define KXTJ9_CTRL_REG1 (0x1B) /* 0001 1011 */
59 #define KXTJ9_CTRL_REG2 (0x1C) /* 0001 1100 */
60 #define KXTJ9_CTRL_REG3 (0x1D) /* 0001 1101 */
61 #define KXTJ9_INT_CTRL_REG1 (0x1E) /* 0001 1110 */
62 #define KXTJ9_INT_CTRL_REG2 (0x1F) /* 0001 1111 */
63 #define KXTJ9_INT_CTRL_REG3 (0x20) /* 0010 0000 */
64 #define KXTJ9_DATA_CTRL_REG (0x21) /* 0010 0001 */
65 #define KXTJ9_TILT_TIMER (0x28) /* 0010 1000 */
66 #define KXTJ9_WUF_TIMER (0x29) /* 0010 1001 */
67 #define KXTJ9_TDT_TIMER (0x2B) /* 0010 1011 */
68 #define KXTJ9_TDT_H_THRESH (0x2C) /* 0010 1100 */
69 #define KXTJ9_TDT_L_THRESH (0x2D) /* 0010 1101 */
70 #define KXTJ9_TDT_TAP_TIMER (0x2E) /* 0010 1110 */
71 #define KXTJ9_TDT_TOTAL_TIMER (0x2F) /* 0010 1111 */
72 #define KXTJ9_TDT_LATENCY_TIMER (0x30) /* 0011 0000 */
73 #define KXTJ9_TDT_WINDOW_TIMER (0x31) /* 0011 0001 */
74 #define KXTJ9_WUF_THRESH (0x5A) /* 0101 1010 */
75 #define KXTJ9_TILT_ANGLE (0x5C) /* 0101 1100 */
76 #define KXTJ9_HYST_SET (0x5F) /* 0101 1111 */
77
78 /* CONTROL REGISTER 1 BITS */
79 #define KXTJ9_DISABLE 0x7F
80 #define KXTJ9_ENABLE (1 << 7)
81 /* INPUT_ABS CONSTANTS */
82 #define FUZZ 3
83 #define FLAT 3
84 /* RESUME STATE INDICES */
85 #define RES_DATA_CTRL 0
86 #define RES_CTRL_REG1 1
87 #define RES_INT_CTRL1 2
88 #define RESUME_ENTRIES 3
89
90 /* CTRL_REG1: set resolution, g-range, data ready enable */
91 /* Output resolution: 8-bit valid or 12-bit valid */
92 #define KXTJ9_RES_8BIT 0
93 #define KXTJ9_RES_12BIT (1 << 6)
94 /* Output g-range: +/-2g, 4g, or 8g */
95 #define KXTJ9_G_2G 0
96 #define KXTJ9_G_4G (1 << 3)
97 #define KXTJ9_G_8G (1 << 4)
98
99 /* DATA_CTRL_REG: controls the output data rate of the part */
100 #define KXTJ9_ODR12_5F 0
101 #define KXTJ9_ODR25F 1
102 #define KXTJ9_ODR50F 2
103 #define KXTJ9_ODR100F 3
104 #define KXTJ9_ODR200F 4
105 #define KXTJ9_ODR400F 5
106 #define KXTJ9_ODR800F 6
107
108 /* kxtj9 */
109 #define KXTJ9_PRECISION 12
110 #define KXTJ9_BOUNDARY (0x1 << (KXTJ9_PRECISION - 1))
111 #define KXTJ9_GRAVITY_STEP KXTJ9_RANGE / KXTJ9_BOUNDARY
112
113
114 /****************operate according to sensor chip:start************/
115
sensor_active(struct i2c_client * client,int enable,int rate)116 static int sensor_active(struct i2c_client *client, int enable, int rate)
117 {
118 struct sensor_private_data *sensor =
119 (struct sensor_private_data *) i2c_get_clientdata(client);
120 int result = 0;
121 int status = 0;
122
123 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
124
125 //register setting according to chip datasheet
126 if(enable)
127 {
128 status = KXTJ9_ENABLE; //kxtj9
129 sensor->ops->ctrl_data |= status;
130 }
131 else
132 {
133 status = ~KXTJ9_ENABLE; //kxtj9
134 sensor->ops->ctrl_data &= status;
135 }
136
137 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
138 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
139 if(result)
140 printk("%s:fail to active sensor\n",__func__);
141
142 return result;
143
144 }
145
sensor_init(struct i2c_client * client)146 static int sensor_init(struct i2c_client *client)
147 {
148 struct sensor_private_data *sensor =
149 (struct sensor_private_data *) i2c_get_clientdata(client);
150 int result = 0;
151
152 result = sensor->ops->active(client,0,0);
153 if(result)
154 {
155 printk("%s:line=%d,error\n",__func__,__LINE__);
156 return result;
157 }
158
159 sensor->status_cur = SENSOR_OFF;
160
161 result = sensor_write_reg(client, KXTJ9_DATA_CTRL_REG, KXTJ9_ODR400F);
162 if(result)
163 {
164 printk("%s:line=%d,error\n",__func__,__LINE__);
165 return result;
166 }
167
168 if(sensor->pdata->irq_enable) //open interrupt
169 {
170 result = sensor_write_reg(client, KXTJ9_INT_CTRL_REG1, 0x34);//enable int,active high,need read INT_REL
171 if(result)
172 {
173 printk("%s:line=%d,error\n",__func__,__LINE__);
174 return result;
175 }
176 }
177
178 sensor->ops->ctrl_data = (KXTJ9_RES_12BIT | KXTJ9_G_2G);
179 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
180 if(result)
181 {
182 printk("%s:line=%d,error\n",__func__,__LINE__);
183 return result;
184 }
185
186 return result;
187 }
188
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)189 static short sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
190 {
191 short result;
192 struct sensor_private_data *sensor =
193 (struct sensor_private_data *) i2c_get_clientdata(client);
194 //int precision = sensor->ops->precision;
195 switch (sensor->devid) {
196 case KXTJ9_DEVID:
197 result = (((short)high_byte << 8) | ((short)low_byte)) >> 4;
198 result *= KXTJ9_GRAVITY_STEP;
199 break;
200
201 default:
202 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
203 return -EFAULT;
204 }
205
206 return result;
207 }
208
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)209 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
210 {
211 struct sensor_private_data *sensor =
212 (struct sensor_private_data *) i2c_get_clientdata(client);
213
214 if (sensor->status_cur == SENSOR_ON) {
215 /* Report acceleration sensor information */
216 input_report_abs(sensor->input_dev, ABS_X, axis->x);
217 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
218 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
219 input_sync(sensor->input_dev);
220 }
221
222 return 0;
223 }
224
225 #define GSENSOR_MIN 10
sensor_report_value(struct i2c_client * client)226 static int sensor_report_value(struct i2c_client *client)
227 {
228 struct sensor_private_data *sensor =
229 (struct sensor_private_data *) i2c_get_clientdata(client);
230 struct sensor_platform_data *pdata = sensor->pdata;
231 int ret = 0;
232 short x, y, z;
233 struct sensor_axis axis;
234 char buffer[6] = {0};
235 char value = 0;
236
237 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
238 {
239 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
240 return -1;
241 }
242
243 memset(buffer, 0, 6);
244
245 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
246 do {
247 *buffer = sensor->ops->read_reg;
248 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
249 if (ret < 0)
250 return ret;
251 } while (0);
252
253 //this gsensor need 6 bytes buffer
254 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
255 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
256 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
257
258 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
259 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
260 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
261
262 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
263
264 gsensor_report_value(client, &axis);
265
266 mutex_lock(&sensor->data_mutex);
267 sensor->axis = axis;
268 mutex_unlock(&sensor->data_mutex);
269
270 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
271 {
272
273 value = sensor_read_reg(client, sensor->ops->int_status_reg);
274 DBG("%s:sensor int status :0x%x\n",__func__,value);
275 }
276
277 return ret;
278 }
279
280 static struct sensor_operate gsensor_kxtj9_ops = {
281 .name = "kxtj9",
282 .type = SENSOR_TYPE_ACCEL,
283 .id_i2c = ACCEL_ID_KXTJ9,
284 .read_reg = KXTJ9_XOUT_L,
285 .read_len = 6,
286 .id_reg = KXTJ9_WHO_AM_I,
287 .id_data = KXTJ9_DEVID,
288 .precision = KXTJ9_PRECISION,
289 .ctrl_reg = KXTJ9_CTRL_REG1,
290 .int_status_reg = KXTJ9_INT_REL,
291 .range = {-32768, 32768},
292 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
293 .active = sensor_active,
294 .init = sensor_init,
295 .report = sensor_report_value,
296 };
297
298 /****************operate according to sensor chip:end************/
gsensor_kxtj9_probe(struct i2c_client * client,const struct i2c_device_id * devid)299 static int gsensor_kxtj9_probe(struct i2c_client *client,
300 const struct i2c_device_id *devid)
301 {
302 return sensor_register_device(client, NULL, devid, &gsensor_kxtj9_ops);
303 }
304
gsensor_kxtj9_remove(struct i2c_client * client)305 static int gsensor_kxtj9_remove(struct i2c_client *client)
306 {
307 return sensor_unregister_device(client, NULL, &gsensor_kxtj9_ops);
308 }
309
310 static const struct i2c_device_id gsensor_kxtj9_id[] = {
311 {"gs_kxtj9", ACCEL_ID_KXTJ9},
312 {}
313 };
314
315 static struct i2c_driver gsensor_kxtj9_driver = {
316 .probe = gsensor_kxtj9_probe,
317 .remove = gsensor_kxtj9_remove,
318 .shutdown = sensor_shutdown,
319 .id_table = gsensor_kxtj9_id,
320 .driver = {
321 .name = "gsensor_kxtj9",
322 #ifdef CONFIG_PM
323 .pm = &sensor_pm_ops,
324 #endif
325 },
326 };
327
328 module_i2c_driver(gsensor_kxtj9_driver);
329
330 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
331 MODULE_DESCRIPTION("kxtj9 3-Axis accelerometer driver");
332 MODULE_LICENSE("GPL");
333