xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/accel/stk8baxx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  stk8baxx.c - Linux kernel modules for sensortek  stk8ba50 / stk8ba50-R /
3  *  stk8ba53 accelerometer
4  *
5  *  Copyright (C) 2012~2016 Lex Hsieh / Sensortek
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  */
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/input.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/workqueue.h>
24 #include <linux/mutex.h>
25 #include <linux/uaccess.h>
26 #include <linux/fs.h>
27 #include <linux/module.h>
28 #include <linux/math64.h>
29 #include <linux/init.h>
30 #include <linux/sensor-dev.h>
31 
32 #define STK_ACC_DRIVER_VERSION	"3.7.1_rk_0425_0428"
33 
34 /*------------------User-defined settings-------------------------*/
35 /* #define CONFIG_SENSORS_STK8BA53 */
36 #define CONFIG_SENSORS_STK8BA50
37 /* #define STK_DEBUG_PRINT */
38 /* #define STK_LOWPASS */
39 #define STK_FIR_LEN	4	/* 1~32 */
40 /* #define STK_TUNE */
41 /* #define STK_ZG_FILTER */
42 #define STK_HOLD_ODR
43 #define STK_DEBUG_CALI
44 #define STK8BAXX_DEF_PLACEMENT	7
45 
46 /*------------------Miscellaneous settings-------------------------*/
47 #define STK8BAXX_I2C_NAME	"stk8baxx"
48 #define ACC_IDEVICE_NAME	"accelerometer"
49 
50 #define STK8BAXX_INIT_ODR	0xD /* 0xB:125Hz, 0xA:62Hz */
51 
52 #define STK8BAXX_RNG_2G			0x3
53 #define STK8BAXX_RNG_4G			0x5
54 #define STK8BAXX_RNG_8G			0x8
55 #define STK8BAXX_RNG_16G		0xC
56 
57 #ifdef CONFIG_SENSORS_STK8BA53
58 /* Parameters under +-4g dynamic range */
59 #define STK_DEF_DYNAMIC_RANGE	STK8BAXX_RNG_4G
60 
61 #if (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_4G)
62 #define STK_LSB_1G			512
63 #define STK_DEF_RANGE		4
64 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_2G)
65 #define STK_LSB_1G			1024
66 #define STK_DEF_RANGE		2
67 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_8G)
68 #define STK_LSB_1G			256
69 #define STK_DEF_RANGE		8
70 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_16G)
71 #define STK_LSB_1G			128
72 #define STK_DEF_RANGE		16
73 #endif
74 
75 #define STK_ZG_COUNT		(STK_LSB_1G / 128)
76 #define STK_TUNE_XYOFFSET	(STK_LSB_1G * 3 / 10)
77 #define STK_TUNE_ZOFFSET	(STK_LSB_1G * 3 / 10) /* (STK_LSB_1G * 3 / 20) */
78 #define STK_TUNE_NOISE		(STK_LSB_1G / 10)
79 #else
80 /* Parameters under +-2g dynamic range */
81 #define STK_DEF_DYNAMIC_RANGE	STK8BAXX_RNG_2G
82 
83 #if (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_2G)
84 #define STK_LSB_1G			256
85 #define STK_DEF_RANGE		2
86 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_4G)
87 #define STK_LSB_1G			128
88 #define STK_DEF_RANGE		4
89 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_8G)
90 #define STK_LSB_1G			64
91 #define STK_DEF_RANGE		8
92 #elif (STK_DEF_DYNAMIC_RANGE == STK8BAXX_RNG_16G)
93 #define STK_LSB_1G			32
94 #define STK_DEF_RANGE		16
95 #endif
96 #define STK_ZG_COUNT		(STK_LSB_1G / 128 + 1)
97 #define STK_TUNE_XYOFFSET	(STK_LSB_1G * 4 / 10)
98 #define STK_TUNE_ZOFFSET	(STK_LSB_1G * 4 / 10) /* (STK_LSB_1G * 3 / 20) */
99 #define STK_TUNE_NOISE		(STK_LSB_1G / 10)
100 #endif
101 
102 #define STK8BAXX_RANGE_UG	(STK_DEF_RANGE * 16384)
103 
104 /* STK_OFFSET_REG_LSB_1G is fixed for all dynamic range */
105 #define STK_OFFSET_REG_LSB_1G	128
106 
107 #define STK_TUNE_NUM			60
108 #define STK_TUNE_DELAY			30
109 
110 #define STK_EVENT_SINCE_EN_LIMIT_DEF	(1)
111 
112 #define STK8BA50_ID				0x09
113 #define STK8BA50R_ID			0x86
114 #define STK8BA53_ID				0x87
115 
116 /*------------------Calibration prameters-------------------------*/
117 #define STK_SAMPLE_NO				10
118 #define STK_ACC_CALI_VER0			0x18
119 #define STK_ACC_CALI_VER1			0x03
120 #define STK_ACC_CALI_END			'\0'
121 #define STK_ACC_CALI_FILE			"/data/misc/stkacccali.conf"
122 #define STK_ACC_CALI_FILE_SDCARD	"/sdcard/.stkacccali.conf"
123 #define STK_ACC_CALI_FILE_SIZE		25
124 
125 #define STK_K_SUCCESS_TUNE		0x04
126 #define STK_K_SUCCESS_FT2			0x03
127 #define STK_K_SUCCESS_FT1			0x02
128 #define STK_K_SUCCESS_FILE			0x01
129 #define STK_K_NO_CALI				0xFF
130 #define STK_K_RUNNING				0xFE
131 #define STK_K_FAIL_LRG_DIFF			0xFD
132 #define STK_K_FAIL_OPEN_FILE		0xFC
133 #define STK_K_FAIL_W_FILE			0xFB
134 #define STK_K_FAIL_R_BACK			0xFA
135 #define STK_K_FAIL_R_BACK_COMP	0xF9
136 #define STK_K_FAIL_I2C				0xF8
137 #define STK_K_FAIL_K_PARA			0xF7
138 #define STK_K_FAIL_OUT_RG			0xF6
139 #define STK_K_FAIL_ENG_I2C			0xF5
140 #define STK_K_FAIL_FT1_USD			0xF4
141 #define STK_K_FAIL_FT2_USD			0xF3
142 #define STK_K_FAIL_WRITE_NOFST	0xF2
143 #define STK_K_FAIL_OTP_5T			0xF1
144 #define STK_K_FAIL_PLACEMENT		0xF0
145 
146 /*------------------stk8baxx registers-------------------------*/
147 #define STK8BAXX_XOUT1				0x02
148 #define STK8BAXX_XOUT2				0x03
149 #define STK8BAXX_YOUT1				0x04
150 #define STK8BAXX_YOUT2				0x05
151 #define STK8BAXX_ZOUT1				0x06
152 #define STK8BAXX_ZOUT2				0x07
153 #define STK8BAXX_INTSTS1			0x09
154 #define STK8BAXX_INTSTS2			0x0A
155 #define STK8BAXX_EVENTINFO1		0x0B
156 #define STK8BAXX_EVENTINFO2		0x0C
157 #define STK8BAXX_RANGESEL			0x0F
158 #define STK8BAXX_BWSEL				0x10
159 #define STK8BAXX_POWMODE			0x11
160 #define STK8BAXX_DATASETUP		0x13
161 #define STK8BAXX_SWRST			0x14
162 #define STK8BAXX_INTEN1			0x16
163 #define STK8BAXX_INTEN2			0x17
164 #define STK8BAXX_INTMAP1			0x19
165 #define STK8BAXX_INTMAP2			0x1A
166 #define STK8BAXX_INTMAP3			0x1B
167 #define STK8BAXX_DATASRC			0x1E
168 #define STK8BAXX_INTCFG1			0x20
169 #define STK8BAXX_INTCFG2			0x21
170 #define STK8BAXX_LGDLY				0x22
171 #define STK8BAXX_LGTHD				0x23
172 #define STK8BAXX_HLGCFG			0x24
173 #define STK8BAXX_HGDLY				0x25
174 #define STK8BAXX_HGTHD				0x26
175 #define STK8BAXX_SLOPEDLY			0x27
176 #define STK8BAXX_SLOPETHD			0x28
177 #define STK8BAXX_TAPTIME			0x2A
178 #define STK8BAXX_TAPCFG			0x2B
179 #define STK8BAXX_ORIENTCFG		0x2C
180 #define STK8BAXX_ORIENTTHETA		0x2D
181 #define STK8BAXX_FLATTHETA			0x2E
182 #define STK8BAXX_FLATHOLD			0x2F
183 #define STK8BAXX_SLFTST			0x32
184 #define STK8BAXX_INTFCFG			0x34
185 #define STK8BAXX_OFSTCOMP1		0x36
186 #define STK8BAXX_OFSTCOMP2		0x37
187 #define STK8BAXX_OFSTFILTX			0x38
188 #define STK8BAXX_OFSTFILTY			0x39
189 #define STK8BAXX_OFSTFILTZ			0x3A
190 #define STK8BAXX_OFSTUNFILTX		0x3B
191 #define STK8BAXX_OFSTUNFILTY		0x3C
192 #define STK8BAXX_OFSTUNFILTZ		0x3D
193 
194 /* ZOUT1 register */
195 #define STK8BAXX_O_NEW			0x01
196 
197 /* SWRST register */
198 #define  STK8BAXX_SWRST_VAL		0xB6
199 
200 /* STK8BAXX_POWMODE register */
201 #define STK8BAXX_MD_SUSPEND		0x80
202 #define STK8BAXX_MD_NORMAL		0x00
203 #define STK8BAXX_MD_SLP_MASK		0x1E
204 
205 /* RANGESEL register */
206 #define STK8BAXX_RANGE_MASK		0x0F
207 
208 /* OFSTCOMP1 register */
209 #define STK8BAXX_OF_CAL_DRY_MASK	0x10
210 #define CAL_AXIS_X_EN				0x20
211 #define CAL_AXIS_Y_EN				0x40
212 #define CAL_AXIS_Z_EN				0x60
213 #define CAL_OFST_RST				0x80
214 
215 /* OFSTCOMP2 register */
216 #define CAL_TG_X0_Y0_ZPOS1			0x20
217 #define CAL_TG_X0_Y0_ZNEG1			0x40
218 
219 /*no_create_attr:the initial is 1-->no create attr. if created, change no_create_att to 0.*/
220 static int no_create_att = 1;
221 static int enable_status = -1;
222 
223 /*------------------Data structure-------------------------*/
224 struct stk8baxx_acc {
225 	union {
226 		struct {
227 			s16 x;
228 			s16 y;
229 			s16 z;
230 		};
231 		s16 acc[3];
232 	};
233 };
234 
235 #if defined(STK_LOWPASS)
236 #define MAX_FIR_LEN 32
237 struct data_filter {
238 	s16 raw[MAX_FIR_LEN][3];
239 	int sum[3];
240 	int num;
241 	int idx;
242 };
243 #endif
244 
245 struct stk8baxx_data {
246 	struct i2c_client *client;
247 	struct input_dev *input_dev;
248 	int irq;
249 	struct stk8baxx_acc acc_xyz;
250 	atomic_t enabled;
251 	bool first_enable;
252 	struct work_struct stk_work;
253 	struct hrtimer acc_timer;
254 	struct workqueue_struct *stk_mems_work_queue;
255 	unsigned char stk8baxx_placement;
256 	atomic_t cali_status;
257 	atomic_t recv_reg;
258 	bool re_enable;
259 #if defined(STK_LOWPASS)
260 	atomic_t                firlength;
261 	atomic_t                fir_en;
262 	struct data_filter      fir;
263 #endif
264 	int event_since_en;
265 	int event_since_en_limit;
266 	u8 stk_tune_offset_record[3];
267 #ifdef STK_TUNE
268 	int stk_tune_offset[3];
269 	int stk_tune_sum[3];
270 	int stk_tune_max[3];
271 	int stk_tune_min[3];
272 	int stk_tune_index;
273 	int stk_tune_done;
274 	s64 stk_tune_square_sum[3];
275 	u32 variance[3];
276 #endif
277 };
278 
279 /*------------------Function prototype-------------------------*/
280 static int stk8baxx_set_enable(struct stk8baxx_data *stk, char en);
281 static int stk8baxx_read_sensor_data(struct stk8baxx_data *stk);
282 /*------------------Global variables-------------------------*/
283 static struct stk8baxx_data *stk8baxx_data_ptr;
284 static struct sensor_private_data *sensor_ptr;
285 /*------------------Main functions-------------------------*/
286 
stk8baxx_smbus_write_byte_data(u8 command,u8 value)287 static s32 stk8baxx_smbus_write_byte_data(u8 command, u8 value)
288 {
289 	return sensor_write_reg(stk8baxx_data_ptr->client, command, value);
290 }
291 
stk8baxx_smbus_read_byte_data(u8 command)292 static int stk8baxx_smbus_read_byte_data(u8 command)
293 {
294 	return sensor_read_reg(stk8baxx_data_ptr->client, command);
295 }
296 
stk8baxx_chk_for_addr(struct stk8baxx_data * stk,s32 org_address,unsigned short reset_address)297 static int stk8baxx_chk_for_addr(struct stk8baxx_data *stk, s32 org_address, unsigned short reset_address)
298 {
299 	int result;
300 	s32 expected_reg0 = 0x86;
301 
302 	if ((org_address & 0xFE) == 0x18)
303 		expected_reg0 = 0x86;
304 	else
305 		expected_reg0 = 0x87;
306 
307 	stk->client->addr = reset_address;
308 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_SWRST, STK8BAXX_SWRST_VAL);
309 	printk(KERN_INFO "%s:issue sw reset to 0x%x, result=%d\n", __func__, reset_address, result);
310 	usleep_range(2000, 3000);
311 
312 	stk->client->addr = org_address;
313 	printk(KERN_INFO "%s Revise I2C Address = 0x%x\n", __func__, org_address);
314 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_POWMODE, STK8BAXX_MD_NORMAL);
315 	result = stk8baxx_smbus_read_byte_data(0x0);
316 	if (result < 0) {
317 		printk(KERN_INFO "%s: read 0x0, result=%d\n", __func__, result);
318 		return result;
319 	}
320 	if (result == expected_reg0) {
321 		printk(KERN_INFO "%s:passed, expected_reg0=0x%x\n", __func__, expected_reg0);
322 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_SWRST, STK8BAXX_SWRST_VAL);
323 		if (result < 0) {
324 			printk(KERN_ERR "%s:failed to issue software reset, error=%d\n", __func__, result);
325 			return result;
326 		}
327 		usleep_range(2000, 3000);
328 		return 1;
329 	}
330 	return 0;
331 }
332 
stk8baxx_sw_reset(struct stk8baxx_data * stk)333 static int stk8baxx_sw_reset(struct stk8baxx_data *stk)
334 {
335 	unsigned short org_addr = 0;
336 	int result;
337 
338 	org_addr = stk->client->addr;
339 	printk(KERN_INFO "%s:org_addr=0x%x\n", __func__, org_addr);
340 
341 	if ((org_addr & 0xFE) == 0x18) {
342 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x18);
343 		if (result == 1)
344 			return 0;
345 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x19);
346 		if (result == 1)
347 			return 0;
348 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x08);
349 		if (result == 1)
350 			return 0;
351 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x28);
352 		if (result == 1)
353 			return 0;
354 	} else if (org_addr == 0x28) {
355 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x28);
356 		if (result == 1)
357 			return 0;
358 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x18);
359 		if (result == 1)
360 			return 0;
361 		result = stk8baxx_chk_for_addr(stk, org_addr, 0x08);
362 		if (result == 1)
363 			return 0;
364 	}
365 	result = stk8baxx_chk_for_addr(stk, org_addr, 0x0B);
366 	return 0;
367 }
368 
stk8baxx_reg_init(struct stk8baxx_data * stk,struct i2c_client * client,struct sensor_private_data * sensor)369 static int stk8baxx_reg_init(struct stk8baxx_data *stk, struct i2c_client *client, struct sensor_private_data *sensor)
370 {
371 	int result;
372 	int aa;
373 
374 #ifdef CONFIG_SENSORS_STK8BA53
375 	printk(KERN_INFO "%s: Initialize stk8ba53\n", __func__);
376 #else
377 	printk(KERN_INFO "%s: Initialize stk8ba50/stk8ba50-r\n", __func__);
378 #endif
379 
380 	/* sw reset */
381 	result = stk8baxx_sw_reset(stk);
382 	if (result < 0) {
383 		printk(KERN_ERR "%s:failed to stk8baxx_sw_reset, error=%d\n", __func__, result);
384 		return result;
385 	}
386 
387 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_POWMODE, STK8BAXX_MD_NORMAL);
388 	if (result < 0) {
389 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_POWMODE, result);
390 		return result;
391 	}
392 
393 	result = stk8baxx_smbus_read_byte_data(STK8BAXX_LGDLY);
394 	if (result < 0) {
395 		printk(KERN_ERR "%s: failed to read acc data, error=%d\n", __func__, result);
396 		return result;
397 	}
398 
399 	if (result == STK8BA50_ID) {
400 		printk(KERN_INFO "%s: chip is stk8ba50\n", __func__);
401 		sensor->devid = STK8BA50_ID;
402 	} else {
403 		result = stk8baxx_smbus_read_byte_data(0x0);
404 		if (result < 0) {
405 			printk(KERN_ERR "%s: failed to read acc data, error=%d\n", __func__, result);
406 			return result;
407 		}
408 		printk(KERN_INFO "%s: 0x0=0x%x\n", __func__, result);
409 		if (result == STK8BA50R_ID) {
410 			printk(KERN_INFO "%s: chip is stk8ba50-R\n", __func__);
411 			sensor->devid = STK8BA50R_ID;
412 		} else {
413 			printk(KERN_INFO "%s: chip is stk8ba53\n", __func__);
414 			sensor->devid = STK8BA53_ID;
415 		}
416 	}
417 #ifdef CONFIG_SENSORS_STK8BA53
418 	if (sensor->devid != STK8BA53_ID) {
419 		printk(KERN_ERR "%s: stk8ba53 is not attached, devid=0x%x\n", __func__, sensor->devid);
420 		return -ENODEV;
421 	}
422 #else
423 	if (sensor->devid == STK8BA53_ID) {
424 		printk(KERN_ERR "%s: stk8ba50/stk8ba50-R is not attached, devid=0x%x\n", __func__, sensor->devid);
425 		return -ENODEV;
426 	}
427 #endif
428 	if (sensor->pdata->irq_enable) {
429 		/* map new data int to int1 */
430 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_INTMAP2, 0x01);
431 		if (result < 0) {
432 			printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_INTMAP2, result);
433 			return result;
434 		}
435 		/* enable new data in */
436 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_INTEN2, 0x10);
437 		if (result < 0) {
438 			printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_INTEN2, result);
439 			return result;
440 		}
441 		/* non-latch int */
442 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_INTCFG2, 0x00);
443 		if (result < 0) {
444 			printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_INTCFG2, result);
445 			return result;
446 		}
447 		/* filtered data source for new data int */
448 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_DATASRC, 0x00);
449 		if (result < 0) {
450 			printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_DATASRC, result);
451 			return result;
452 		}
453 		/* int1, push-pull, active high */
454 		result = stk8baxx_smbus_write_byte_data(STK8BAXX_INTCFG1, 0x01);
455 		if (result < 0) {
456 			printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_INTCFG1, result);
457 			return result;
458 		}
459 	}
460 #ifdef CONFIG_SENSORS_STK8BA53
461 	/* +- 4g */
462 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_RANGESEL, STK_DEF_DYNAMIC_RANGE);
463 	if (result < 0) {
464 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_RANGESEL, result);
465 		return result;
466 	}
467 #else
468 	/* +- 2g */
469 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_RANGESEL, STK_DEF_DYNAMIC_RANGE);
470 	if (result < 0) {
471 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_RANGESEL, result);
472 		return result;
473 	}
474 #endif
475 	/* ODR = 62 Hz */
476 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_BWSEL, STK8BAXX_INIT_ODR);
477 	if (result < 0) {
478 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_BWSEL, result);
479 		return result;
480 	}
481 
482 	/* i2c watchdog enable, 1 ms timer perios */
483 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_INTFCFG, 0x04);
484 	if (result < 0) {
485 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_INTFCFG, result);
486 		return result;
487 	}
488 
489 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_POWMODE, STK8BAXX_MD_SUSPEND);
490 	if (result < 0) {
491 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_POWMODE, result);
492 		return result;
493 	}
494 	atomic_set(&stk->enabled, 0);
495 	stk->first_enable = true;
496 	atomic_set(&stk->cali_status, STK_K_NO_CALI);
497 	atomic_set(&stk->recv_reg, 0);
498 #ifdef STK_LOWPASS
499 	memset(&stk->fir, 0x00, sizeof(stk->fir));
500 	atomic_set(&stk->firlength, STK_FIR_LEN);
501 	atomic_set(&stk->fir_en, 1);
502 #endif
503 
504 	for (aa = 0; aa < 3; aa++)
505 		stk->stk_tune_offset_record[aa] = 0;
506 #ifdef STK_TUNE
507 	for (aa = 0; aa < 3; aa++) {
508 		stk->stk_tune_offset[aa] = 0;
509 		stk->stk_tune_sum[aa] = 0;
510 		stk->stk_tune_max[aa] = 0;
511 		stk->stk_tune_min[aa] = 0;
512 		stk->stk_tune_square_sum[aa] = 0LL;
513 		stk->variance[aa] = 0;
514 	}
515 	stk->stk_tune_done = 0;
516 	stk->stk_tune_index = 0;
517 #endif
518 	stk->event_since_en_limit = STK_EVENT_SINCE_EN_LIMIT_DEF;
519 
520 	return 0;
521 }
522 
523 #ifdef STK_LOWPASS
stk8baxx_low_pass(struct stk8baxx_data * stk,struct stk8baxx_acc * acc_lp)524 static void stk8baxx_low_pass(struct stk8baxx_data *stk, struct stk8baxx_acc *acc_lp)
525 {
526 	int idx, firlength = atomic_read(&stk->firlength);
527 #ifdef STK_ZG_FILTER
528 	s16 zero_fir = 0;
529 #endif
530 
531 	if (atomic_read(&stk->fir_en)) {
532 		if (stk->fir.num < firlength) {
533 			stk->fir.raw[stk->fir.num][0] = acc_lp->x;
534 			stk->fir.raw[stk->fir.num][1] = acc_lp->y;
535 			stk->fir.raw[stk->fir.num][2] = acc_lp->z;
536 			stk->fir.sum[0] += acc_lp->x;
537 			stk->fir.sum[1] += acc_lp->y;
538 			stk->fir.sum[2] += acc_lp->z;
539 			stk->fir.num++;
540 			stk->fir.idx++;
541 		} else {
542 			idx = stk->fir.idx % firlength;
543 			stk->fir.sum[0] -= stk->fir.raw[idx][0];
544 			stk->fir.sum[1] -= stk->fir.raw[idx][1];
545 			stk->fir.sum[2] -= stk->fir.raw[idx][2];
546 			stk->fir.raw[idx][0] = acc_lp->x;
547 			stk->fir.raw[idx][1] = acc_lp->y;
548 			stk->fir.raw[idx][2] = acc_lp->z;
549 			stk->fir.sum[0] += acc_lp->x;
550 			stk->fir.sum[1] += acc_lp->y;
551 			stk->fir.sum[2] += acc_lp->z;
552 			stk->fir.idx++;
553 #ifdef STK_ZG_FILTER
554 			if (abs(stk->fir.sum[0] / firlength) <= STK_ZG_COUNT)
555 				acc_lp->x = (stk->fir.sum[0] * zero_fir) / firlength;
556 			else
557 				acc_lp->x = stk->fir.sum[0] / firlength;
558 			if (abs(stk->fir.sum[1] / firlength) <= STK_ZG_COUNT)
559 				acc_lp->y = (stk->fir.sum[1] * zero_fir) / firlength;
560 			else
561 				acc_lp->y = stk->fir.sum[1] / firlength;
562 			if (abs(stk->fir.sum[2] / firlength) <= STK_ZG_COUNT)
563 				acc_lp->z = (stk->fir.sum[2] * zero_fir) / firlength;
564 			else
565 				acc_lp->z = stk->fir.sum[2] / firlength;
566 #else
567 			acc_lp->x = stk->fir.sum[0] / firlength;
568 			acc_lp->y = stk->fir.sum[1] / firlength;
569 			acc_lp->z = stk->fir.sum[2] / firlength;
570 #endif
571 		}
572 	}
573 }
574 #endif
575 
576 #ifdef STK_TUNE
stk8baxx_reset_para(struct stk8baxx_data * stk)577 static void stk8baxx_reset_para(struct stk8baxx_data *stk)
578 {
579 	int ii;
580 
581 	for (ii = 0; ii < 3; ii++) {
582 		stk->stk_tune_sum[ii] = 0;
583 		stk->stk_tune_square_sum[ii] = 0LL;
584 		stk->stk_tune_min[ii] = 4096;
585 		stk->stk_tune_max[ii] = -4096;
586 		stk->variance[ii] = 0;
587 	}
588 }
589 
stk8baxx_tune(struct stk8baxx_data * stk,struct stk8baxx_acc * acc_xyz)590 static void stk8baxx_tune(struct stk8baxx_data *stk, struct stk8baxx_acc *acc_xyz)
591 {
592 	int ii;
593 	u8 offset[3];
594 	s16 acc[3];
595 	s64 s64_temp;
596 	const s64 var_enlarge_scale = 64;
597 
598 	if (stk->stk_tune_done != 0)
599 		return;
600 
601 	acc[0] = acc_xyz->x;
602 	acc[1] = acc_xyz->y;
603 	acc[2] = acc_xyz->z;
604 
605 	if (stk->event_since_en >= STK_TUNE_DELAY) {
606 		if ((abs(acc[0]) <= STK_TUNE_XYOFFSET) && (abs(acc[1]) <= STK_TUNE_XYOFFSET) &&
607 		    (abs(abs(acc[2]) - STK_LSB_1G) <= STK_TUNE_ZOFFSET)) {
608 			stk->stk_tune_index++;
609 			/* printk("\n-qhy20161108--%s----acc[0]=0x%x,,acc[1]=0x%x,,acc[2]=0x%x\n",__func__,acc[0],acc[1],acc[2]); */
610 		} else {
611 			stk->stk_tune_index = 0;
612 		}
613 
614 		if (stk->stk_tune_index == 0) {
615 			stk8baxx_reset_para(stk);
616 			/* printk("\n--qhy20161108--%s-- %d--\n",__func__,__LINE__); */
617 		} else {
618 			for (ii = 0; ii < 3; ii++) {
619 				stk->stk_tune_sum[ii] += acc[ii];
620 				stk->stk_tune_square_sum[ii] += acc[ii] * acc[ii];
621 				if (acc[ii] > stk->stk_tune_max[ii])
622 					stk->stk_tune_max[ii] = acc[ii];
623 				if (acc[ii] < stk->stk_tune_min[ii])
624 					stk->stk_tune_min[ii] = acc[ii];
625 			}
626 		}
627 
628 		if (stk->stk_tune_index == STK_TUNE_NUM) {
629 			for (ii = 0; ii < 3; ii++) {
630 				if ((stk->stk_tune_max[ii] - stk->stk_tune_min[ii]) > STK_TUNE_NOISE) {
631 					stk->stk_tune_index = 0;
632 					stk8baxx_reset_para(stk);
633 					return;
634 				}
635 			}
636 
637 			stk->stk_tune_offset[0] = stk->stk_tune_sum[0] / STK_TUNE_NUM;
638 			stk->stk_tune_offset[1] = stk->stk_tune_sum[1] / STK_TUNE_NUM;
639 			if (acc[2] > 0)
640 				stk->stk_tune_offset[2] = stk->stk_tune_sum[2] / STK_TUNE_NUM - STK_LSB_1G;
641 			else
642 				stk->stk_tune_offset[2] = stk->stk_tune_sum[2] / STK_TUNE_NUM - (-STK_LSB_1G);
643 
644 			offset[0] = (u8)(-stk->stk_tune_offset[0]);
645 			offset[1] = (u8)(-stk->stk_tune_offset[1]);
646 			offset[2] = (u8)(-stk->stk_tune_offset[2]);
647 			stk->stk_tune_offset_record[0] = offset[0];
648 			stk->stk_tune_offset_record[1] = offset[1];
649 			stk->stk_tune_offset_record[2] = offset[2];
650 
651 			stk->stk_tune_done = 1;
652 			atomic_set(&stk->cali_status, STK_K_SUCCESS_TUNE);
653 			stk->event_since_en = 0;
654 			printk(KERN_INFO "%s:TUNE done, %d,%d,%d\n", __func__, offset[0], offset[1], offset[2]);
655 			printk(KERN_INFO "%s:TUNE done, var=%u,%u,%u\n", __func__, stk->variance[0], stk->variance[1], stk->variance[2]);
656 		}
657 	}
658 }
659 #endif
660 
stk8baxx_sign_conv(struct stk8baxx_data * stk,s16 raw_acc_data[],u8 acc_reg_data[])661 static void stk8baxx_sign_conv(struct stk8baxx_data *stk, s16 raw_acc_data[], u8 acc_reg_data[])
662 {
663 #ifdef CONFIG_SENSORS_STK8BA53
664 	raw_acc_data[0] = acc_reg_data[1] << 8 | acc_reg_data[0];
665 	raw_acc_data[0] >>= 4;
666 	raw_acc_data[1] = acc_reg_data[3] << 8 | acc_reg_data[2];
667 	raw_acc_data[1] >>= 4;
668 	raw_acc_data[2] = acc_reg_data[5] << 8 | acc_reg_data[4];
669 	raw_acc_data[2] >>= 4;
670 #else
671 	raw_acc_data[0] = acc_reg_data[1] << 8 | acc_reg_data[0];
672 	raw_acc_data[0] >>= 6;
673 	raw_acc_data[1] = acc_reg_data[3] << 8 | acc_reg_data[2];
674 	raw_acc_data[1] >>= 6;
675 	raw_acc_data[2] = acc_reg_data[5] << 8 | acc_reg_data[4];
676 	raw_acc_data[2] >>= 6;
677 #endif
678 }
679 
stk8baxx_set_enable(struct stk8baxx_data * stk,char en)680 static int stk8baxx_set_enable(struct stk8baxx_data *stk, char en)
681 {
682 	s8 result;
683 	s8 write_buffer = 0;
684 	int new_enabled = (en) ? 1 : 0;
685 
686 	/*int k_status = atomic_read(&stk->cali_status);*/
687 #ifdef STK_DEBUG_PRINT
688 	printk("%s:+++1+++--k_status=%d,first_enable=%d\n", __func__, k_status, stk->first_enable);
689 	if (stk->first_enable && k_status != STK_K_RUNNING) {
690 		stk->first_enable = false;
691 		printk("%s:+++2+++first_enable=%d\n", __func__, stk->first_enable);
692 		stk8baxx_load_cali(stk);
693 	}
694 #endif
695 	enable_status = new_enabled;
696 	if (new_enabled == atomic_read(&stk->enabled))
697 		return 0;
698 	/* printk(KERN_INFO "%s:%x\n", __func__, en); */
699 
700 	if (en)
701 		write_buffer = STK8BAXX_MD_NORMAL;
702 	else
703 		write_buffer = STK8BAXX_MD_SUSPEND;
704 
705 	result = stk8baxx_smbus_write_byte_data(STK8BAXX_POWMODE, write_buffer);
706 	if (result < 0) {
707 		printk(KERN_ERR "%s:failed to write reg 0x%x, error=%d\n", __func__, STK8BAXX_POWMODE, result);
708 		goto error_enable;
709 	}
710 
711 	if (en) {
712 		stk->event_since_en = 0;
713 #ifdef STK_TUNE
714 		if ((k_status & 0xF0) != 0 && stk->stk_tune_done == 0) {
715 			stk->stk_tune_index = 0;
716 			stk8baxx_reset_para(stk);
717 		}
718 #endif
719 	}
720 
721 	atomic_set(&stk->enabled, new_enabled);
722 	return 0;
723 
724 error_enable:
725 	return result;
726 }
727 
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)728 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
729 {
730 	struct sensor_private_data *sensor =
731 		(struct sensor_private_data *)i2c_get_clientdata(client);
732 
733 	/* Report acceleration sensor information */
734 	input_report_abs(sensor->input_dev, ABS_X, axis->x);
735 	input_report_abs(sensor->input_dev, ABS_Y, axis->y);
736 	input_report_abs(sensor->input_dev, ABS_Z, axis->z);
737 	input_sync(sensor->input_dev);
738 #ifdef STK_DEBUG_PRINT
739 	printk(KERN_INFO "Gsensor x==%d  y==%d z==%d\n", axis->x, axis->y, axis->z);
740 #endif
741 	return 0;
742 }
743 
stk8baxx_read_sensor_data(struct stk8baxx_data * stk)744 static int stk8baxx_read_sensor_data(struct stk8baxx_data *stk)
745 {
746 	int result;
747 	u8 acc_reg[6];
748 	int x, y, z;
749 	struct stk8baxx_acc acc;
750 	struct sensor_private_data *sensor =
751 		(struct sensor_private_data *)i2c_get_clientdata(stk->client);
752 	struct sensor_platform_data *pdata = sensor->pdata;
753 	s16 raw_acc[3];
754 
755 	acc.x = 0;
756 	acc.y = 0;
757 	acc.z = 0;
758 
759 	*acc_reg = sensor->ops->read_reg;
760 	result = sensor_rx_data(stk->client, (char *)acc_reg, sensor->ops->read_len);
761 	if (result < 0) {
762 		printk(KERN_ERR "%s: failed to read acc data, error=%d\n", __func__, result);
763 		return result;
764 	}
765 
766 	stk8baxx_sign_conv(stk, raw_acc, acc_reg);
767 #ifdef STK_DEBUG_PRINT
768 	printk(KERN_INFO "%s: raw_acc=%4d,%4d,%4d\n", __func__, (int)raw_acc[0], (int)raw_acc[1], (int)raw_acc[2]);
769 #endif
770 	acc.x = raw_acc[0];
771 	acc.y = raw_acc[1];
772 	acc.z = raw_acc[2];
773 #ifdef STK_TUNE
774 	if ((k_status & 0xF0) != 0)
775 		stk8baxx_tune(stk, &acc);
776 #endif
777 	x = acc.x;
778 	y = acc.y;
779 	z = acc.z;
780 	acc.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
781 	acc.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
782 	acc.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
783 
784 #ifdef STK_LOWPASS
785 	stk8baxx_low_pass(stk, &acc);
786 #endif
787 
788 	stk->acc_xyz.x = acc.x;
789 	stk->acc_xyz.y = acc.y;
790 	stk->acc_xyz.z = acc.z;
791 #ifdef STK_DEBUG_PRINT
792 	printk(KERN_INFO "stk8baxx acc= %4d, %4d, %4d\n", (int)stk->acc_xyz.x, (int)stk->acc_xyz.y, (int)stk->acc_xyz.z);
793 #endif
794 	return 0;
795 }
796 
sensor_report_value(struct i2c_client * client)797 static int sensor_report_value(struct i2c_client *client)
798 {
799 	unsigned int xyz_adc_rang = 0;
800 	struct sensor_axis axis;
801 	struct sensor_private_data *sensor =
802 		(struct sensor_private_data *)i2c_get_clientdata(client);
803 	static int flag;
804 
805 	stk8baxx_read_sensor_data(stk8baxx_data_ptr);
806 
807 	xyz_adc_rang = STK_LSB_1G * STK_DEF_RANGE;
808 	axis.x = stk8baxx_data_ptr->acc_xyz.x * (STK8BAXX_RANGE_UG / xyz_adc_rang);
809 	axis.y = stk8baxx_data_ptr->acc_xyz.y * (STK8BAXX_RANGE_UG / xyz_adc_rang);
810 	axis.z = stk8baxx_data_ptr->acc_xyz.z * (STK8BAXX_RANGE_UG / xyz_adc_rang);
811 
812 	/*
813 	*input dev will ignore report data if data value is the same with last_value,
814 	*sample rate will not enough by this way, so just avoid this case
815 	*/
816 	if ((sensor->axis.x == axis.x) && (sensor->axis.y == axis.y) && (sensor->axis.z == axis.z)) {
817 		if (flag) {
818 			flag = 0;
819 			axis.x += 1;
820 			axis.y += 1;
821 			axis.z += 1;
822 		} else {
823 			flag = 1;
824 			axis.x -= 1;
825 			axis.y -= 1;
826 			axis.z -= 1;
827 		}
828 	}
829 
830 	gsensor_report_value(client, &axis);
831 
832 	mutex_lock(&sensor->data_mutex);
833 	sensor->axis = axis;
834 	mutex_unlock(&sensor->data_mutex);
835 
836 	return 0;
837 }
838 
sensor_active(struct i2c_client * client,int enable,int rate)839 static int sensor_active(struct i2c_client *client, int enable, int rate)
840 {
841 	if (enable)
842 		stk8baxx_set_enable(stk8baxx_data_ptr, 1);
843 	else
844 		stk8baxx_set_enable(stk8baxx_data_ptr, 0);
845 
846 	return 0;
847 }
848 
sensor_init(struct i2c_client * client)849 static int sensor_init(struct i2c_client *client)
850 {
851 	int ret = 0;
852 	struct stk8baxx_data *stk;
853 	struct sensor_private_data *sensor =
854 	    (struct sensor_private_data *)i2c_get_clientdata(client);
855 
856 	printk(KERN_INFO "driver version:%s\n", STK_ACC_DRIVER_VERSION);
857 	if (!enable_status)
858 		return 0;
859 	stk = kzalloc(sizeof(*stk), GFP_KERNEL);
860 	if (!stk) {
861 		printk(KERN_ERR "%s:memory allocation error\n", __func__);
862 		return -ENOMEM;
863 	}
864 	stk8baxx_data_ptr = stk;
865 	sensor_ptr = sensor;
866 	stk->stk8baxx_placement = STK8BAXX_DEF_PLACEMENT;
867 	stk->client = client;
868 	ret = stk8baxx_reg_init(stk, client, sensor);
869 	if (ret) {
870 		printk(KERN_ERR "%s:stk8baxx initialization failed\n", __func__);
871 		return ret;
872 	}
873 	stk->re_enable = false;
874 	sensor->status_cur = SENSOR_OFF;
875 
876 	/* Sys Attribute Register */
877 	if (no_create_att) {
878 		struct input_dev *p_input_dev = NULL;
879 
880 		p_input_dev = input_allocate_device();
881 		if (!p_input_dev) {
882 			dev_err(&client->dev,
883 				"Failed to allocate input device\n");
884 			return -ENOMEM;
885 		}
886 
887 		p_input_dev->name = "stk8baxx_attr";
888 		set_bit(EV_ABS, p_input_dev->evbit);
889 		dev_set_drvdata(&p_input_dev->dev, stk);
890 		ret = input_register_device(p_input_dev);
891 		if (ret) {
892 			dev_err(&client->dev,
893 				"Unable to register input device %s\n", p_input_dev->name);
894 			return ret;
895 		}
896 
897 		DBG("Sys Attribute Register here %s is called for stk8baxx.\n", __func__);
898 		no_create_att = 0;
899 	}
900 
901 	return 0;
902 }
903 
904 static struct sensor_operate gsensor_stk8baxx_ops = {
905 	.name				= "gs_stk8baxx",
906 	.type				= SENSOR_TYPE_ACCEL,			/*sensor type and it should be correct*/
907 	.id_i2c				= ACCEL_ID_STK8BAXX,			/*i2c id number*/
908 	.read_reg			= STK8BAXX_XOUT1,			/*read data*/
909 	.read_len			= 6,					/*data length*/
910 	.id_reg				= SENSOR_UNKNOW_DATA,			/*read device id from this register*/
911 	.id_data			= SENSOR_UNKNOW_DATA,			/*device id*/
912 	.precision			= SENSOR_UNKNOW_DATA,			/*12 bit*/
913 	.ctrl_reg			= STK8BAXX_POWMODE,			/*enable or disable*/
914 	/*intterupt status register*/
915 	.int_status_reg			= STK8BAXX_INTSTS2,
916 	.range				= {-STK8BAXX_RANGE_UG, STK8BAXX_RANGE_UG},	/*range*/
917 	.trig				= IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
918 	.active				= sensor_active,
919 	.init				= sensor_init,
920 	.report				= sensor_report_value,
921 };
922 
gsensor_stk8baxx_probe(struct i2c_client * client,const struct i2c_device_id * devid)923 static int gsensor_stk8baxx_probe(struct i2c_client *client,
924 				  const struct i2c_device_id *devid)
925 {
926 	return sensor_register_device(client, NULL, devid, &gsensor_stk8baxx_ops);
927 }
928 
gsensor_stk8baxx_remove(struct i2c_client * client)929 static int gsensor_stk8baxx_remove(struct i2c_client *client)
930 {
931 	return sensor_unregister_device(client, NULL, &gsensor_stk8baxx_ops);
932 }
933 
934 static const struct i2c_device_id gsensor_stk8baxx_id[] = {
935 	{"gs_stk8baxx", ACCEL_ID_STK8BAXX},
936 	{}
937 };
938 
939 static struct i2c_driver gsensor_stk8baxx_driver = {
940 	.probe = gsensor_stk8baxx_probe,
941 	.remove = gsensor_stk8baxx_remove,
942 	.shutdown = sensor_shutdown,
943 	.id_table = gsensor_stk8baxx_id,
944 	.driver = {
945 		.name = "gsensor_stk8baxx",
946 	#ifdef CONFIG_PM
947 		.pm = &sensor_pm_ops,
948 	#endif
949 	},
950 };
951 
952 module_i2c_driver(gsensor_stk8baxx_driver);
953 
954 MODULE_AUTHOR("Lex Hsieh, Sensortek");
955 MODULE_DESCRIPTION("stk8baxx 3-Axis accelerometer driver");
956 MODULE_LICENSE("GPL");
957 MODULE_VERSION(STK_ACC_DRIVER_VERSION);
958