1 /* drivers/input/sensors/access/kxtik.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: Bruins <xwj@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
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 <linux/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>
32 #endif
33 #include <linux/sensor-dev.h>
34
35
36 #define LSM303D_WHO_AM_I (0x0F)
37
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)
56
57 #define LSM303D_IG_CFG1 (0x30)
58 #define LSM303D_IG_SRC1 (0x31)
59 #define LSM303D_IG_THS1 (0x32)
60 #define LSM303D_IG_DURATION1 (0x33)
61
62 #define LSM303D_IG_CFG2 (0x34)
63 #define LSM303D_IG_SRC2 (0x35)
64 #define LSM303D_IG_THS2 (0x36)
65 #define LSM303D_IG_DURATION2 (0x37)
66
67
68 #define LSM303D_DEVID (0x49) //chip id
69 #define LSM303D_ACC_DISABLE (0x08)
70
71 #define LSM303D_RANGE 32768
72
73 /* LSM303D */
74 #define LSM303D_PRECISION 16
75 #define LSM303D_BOUNDARY (0x1 << (LSM303D_PRECISION - 1))
76 #define LSM303D_GRAVITY_STEP (LSM303D_RANGE / LSM303D_BOUNDARY)
77
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 */
88
89
90 struct sensor_reg_data {
91 char reg;
92 char data;
93 };
94
95 /****************operate according to sensor chip:start************/
sensor_active(struct i2c_client * client,int enable,int rate)96 static int sensor_active(struct i2c_client *client, int enable, int rate)
97 {
98 struct sensor_private_data *sensor =
99 (struct sensor_private_data *) i2c_get_clientdata(client);
100 int result = 0;
101 int status = 0;
102
103 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
104
105 sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down
106
107 //register setting according to chip datasheet
108 if(!enable)
109 {
110 status = LSM303D_ACC_DISABLE; //lis3dh
111 sensor->ops->ctrl_data |= status;
112 }
113 else
114 {
115 status = ~LSM303D_ACC_DISABLE; //lis3dh
116 sensor->ops->ctrl_data &= status;
117 }
118
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);
121 if(result)
122 printk("%s:fail to active sensor\n",__func__);
123
124 return result;
125
126 }
127
128
sensor_init(struct i2c_client * client)129 static int sensor_init(struct i2c_client *client)
130 {
131 struct sensor_private_data *sensor =
132 (struct sensor_private_data *) i2c_get_clientdata(client);
133 int result = 0;
134 int i;
135
136 struct sensor_reg_data reg_data[] =
137 {
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
150
151 /*
152 {LSM303D_CTRL_REG7,0x00},
153 {LSM303D_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode
154 {LSM303D_CTRL_REG6,0x40},
155
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
160 */
161 };
162
163 result = sensor->ops->active(client,0,0);
164 if(result)
165 {
166 printk("%s:line=%d,error\n",__func__,__LINE__);
167 return result;
168 }
169
170 sensor->status_cur = SENSOR_OFF;
171
172 for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)
173 {
174 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
175 if(result)
176 {
177 printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);
178 return result;
179 }
180 }
181
182
183 if(sensor->pdata->irq_enable)
184 {
185
186 result = sensor_write_reg(client, LSM303D_CTRL_REG3, 0x20);
187 if(result)
188 {
189 printk("%s:line=%d,error\n",__func__,__LINE__);
190 return result;
191 }
192
193 i = sensor_read_reg(client,LSM303D_CTRL_REG5);
194
195 result = sensor_write_reg(client, LSM303D_CTRL_REG5, (i|0x01));
196 if(result)
197 {
198 printk("%s:line=%d,error\n",__func__,__LINE__);
199 return result;
200 }
201
202 }
203
204 return result;
205 }
206
207
sensor_convert_data(struct i2c_client * client,char high_byte,char low_byte)208 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
209 {
210 int result;
211 struct sensor_private_data *sensor =
212 (struct sensor_private_data *) i2c_get_clientdata(client);
213
214 switch (sensor->devid) {
215 case LSM303D_DEVID:
216 result = ((int)high_byte << 8) | (int)low_byte;
217 if (result < LSM303D_BOUNDARY)
218 result = result * LSM303D_GRAVITY_STEP;
219 else
220 result = ~(((~result & (0x7fff >> (16 - LSM303D_PRECISION))) + 1)
221 * LSM303D_GRAVITY_STEP) + 1;
222 break;
223
224 default:
225 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
226 return -EFAULT;
227 }
228
229 return (int)result;
230 }
231
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)232 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
233 {
234 struct sensor_private_data *sensor =
235 (struct sensor_private_data *) i2c_get_clientdata(client);
236
237 if (sensor->status_cur == SENSOR_ON) {
238 /* Report acceleration sensor information */
239 input_report_abs(sensor->input_dev, ABS_X, axis->x);
240 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
241 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
242 input_sync(sensor->input_dev);
243 }
244
245 return 0;
246 }
247
248 #define GSENSOR_MIN 10
sensor_report_value(struct i2c_client * client)249 static int sensor_report_value(struct i2c_client *client)
250 {
251 struct sensor_private_data *sensor =
252 (struct sensor_private_data *) i2c_get_clientdata(client);
253 struct sensor_platform_data *pdata = sensor->pdata;
254 int ret = 0;
255 int x,y,z;
256 struct sensor_axis axis;
257 char buffer[6] = {0};
258 char value = 0;
259
260 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
261 {
262 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
263 return -1;
264 }
265
266 memset(buffer, 0, 6);
267
268 value = sensor_read_reg(client, LSM303D_STATUS_REG);
269 if((value & 0x0f) == 0)
270 {
271 printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);
272 return -1;
273 }
274
275
276 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
277 do {
278 *buffer = sensor->ops->read_reg;
279 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
280 if (ret < 0)
281 return ret;
282 } while (0);
283
284 //this gsensor need 6 bytes buffer
285 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
286 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
287 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
288
289 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
290 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
291 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
292
293 gsensor_report_value(client, &axis);
294
295 mutex_lock(&sensor->data_mutex);
296 sensor->axis = axis;
297 mutex_unlock(&sensor->data_mutex);
298
299 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
300 {
301
302 value = sensor_read_reg(client, sensor->ops->int_status_reg);
303 DBG("%s:sensor int status :0x%x\n",__func__,value);
304 }
305
306 return ret;
307 }
308
309 static struct sensor_operate gsensor_lsm303d_ops = {
310 .name = "lsm303d",
311 .type = SENSOR_TYPE_ACCEL,
312 .id_i2c = ACCEL_ID_LSM303D,
313 .read_reg = (LSM303D_OUT_X_L | 0x80),
314 .read_len = 6,
315 .id_reg = LSM303D_WHO_AM_I,
316 .id_data = LSM303D_DEVID,
317 .precision = LSM303D_PRECISION,
318 .ctrl_reg = LSM303D_CTRL_REG1,
319 .int_status_reg = LSM303D_IG_SRC1,
320 .range = {-LSM303D_RANGE, LSM303D_RANGE},
321 .trig = (IRQF_TRIGGER_LOW | IRQF_ONESHOT),
322 .active = sensor_active,
323 .init = sensor_init,
324 .report = sensor_report_value,
325 };
326
327
328 /****************operate according to sensor chip:end************/
gsensor_lsm303d_probe(struct i2c_client * client,const struct i2c_device_id * devid)329 static int gsensor_lsm303d_probe(struct i2c_client *client,
330 const struct i2c_device_id *devid)
331 {
332 return sensor_register_device(client, NULL, devid, &gsensor_lsm303d_ops);
333 }
334
gsensor_lsm303d_remove(struct i2c_client * client)335 static int gsensor_lsm303d_remove(struct i2c_client *client)
336 {
337 return sensor_unregister_device(client, NULL, &gsensor_lsm303d_ops);
338 }
339
340 static const struct i2c_device_id gsensor_lsm303d_id[] = {
341 {"gs_lsm303d", ACCEL_ID_LSM303D},
342 {}
343 };
344
345 static struct i2c_driver gsensor_lsm303d_driver = {
346 .probe = gsensor_lsm303d_probe,
347 .remove = gsensor_lsm303d_remove,
348 .shutdown = sensor_shutdown,
349 .id_table = gsensor_lsm303d_id,
350 .driver = {
351 .name = "gsensor_lsm303d",
352 #ifdef CONFIG_PM
353 .pm = &sensor_pm_ops,
354 #endif
355 },
356 };
357
358 module_i2c_driver(gsensor_lsm303d_driver);
359
360 MODULE_AUTHOR("xwj <xwj@rock-chips.com>");
361 MODULE_DESCRIPTION("lsm303d 3-Axis accelerometer driver");
362 MODULE_LICENSE("GPL");
363