• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  stk3x1x.c - Linux kernel modules for sensortek stk301x, stk321x and stk331x
3  *  proximity/ambient light sensor
4  *
5  *  Copyright (C) 2012~2013 Lex Hsieh / sensortek <lex_hsieh@sensortek.com.tw>
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 
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
22 #include <linux/mutex.h>
23 #include <linux/kdev_t.h>
24 #include <linux/fs.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/kthread.h>
31 #include <linux/errno.h>
32 #include <linux/interrupt.h>
33 #include <linux/gpio.h>
34 #include   <linux/fs.h>
35 #include  <asm/uaccess.h>
36 #include "../../init-input.h"
37 #include "stk3x1x.h"
38 
39 #include <linux/regulator/consumer.h>
40 #include <asm/uaccess.h>
41 
42 #include <linux/pinctrl/consumer.h>
43 
44 #if IS_ENABLED(CONFIG_PM)
45 #include <linux/pm.h>
46 #endif
47 
48 #define DRIVER_VERSION  "3.5.2"
49 
50 #define REPORT_KEY 0
51 #define KEY_PROX_NEAR KEY_SLEEP
52 #define KEY_PROX_FAR  KEY_WAKEUP
53 
54 /* Driver Settings */
55 #define CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
56 #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
57 #define STK_ALS_CHANGE_THD	       0	/* The threshold to trigger ALS interrupt, unit: lux */
58 #endif
59 
60 #define STK_INT_PS_MODE			1	/* 1, 2, or 3	*/
61 #define STK_POLL_PS
62 /* ALS interrupt is valid only when STK_INT_PS_MODE = 1	or 4*/
63 #define STK_POLL_ALS
64 /* #define STK_TUNE0 */
65 /*#define STK_DEBUG_PRINTF
66 #define STK_ALS_FIR
67 #define STK_IRS
68 */
69 #define STK_CHK_REG
70 #define STK_STATE_REG 			       0x00
71 #define STK_PSCTRL_REG 		       0x01
72 #define STK_ALSCTRL_REG 		       0x02
73 #define STK_LEDCTRL_REG 		       0x03
74 #define STK_INT_REG 			       0x04
75 #define STK_WAIT_REG 			       0x05
76 #define STK_THDH1_PS_REG 		       0x06
77 #define STK_THDH2_PS_REG 		       0x07
78 #define STK_THDL1_PS_REG 		       0x08
79 #define STK_THDL2_PS_REG 		       0x09
80 #define STK_THDH1_ALS_REG 	       0x0A
81 #define STK_THDH2_ALS_REG 	       0x0B
82 #define STK_THDL1_ALS_REG 	       0x0C
83 #define STK_THDL2_ALS_REG 	       0x0D
84 #define STK_FLAG_REG 			       0x10
85 #define STK_DATA1_PS_REG	 	       0x11
86 #define STK_DATA2_PS_REG 		       0x12
87 #define STK_DATA1_ALS_REG 	       0x13
88 #define STK_DATA2_ALS_REG 	       0x14
89 #define STK_DATA1_OFFSET_REG 	       0x15
90 #define STK_DATA2_OFFSET_REG 	       0x16
91 #define STK_DATA1_IR_REG 		       0x17
92 #define STK_DATA2_IR_REG 		       0x18
93 #define STK_PDT_ID_REG 		       0x3E
94 #define STK_RSRVD_REG 		       0x3F
95 #define STK_SW_RESET_REG		       0x80
96 
97 
98 /* Define state reg */
99 #define STK_STATE_EN_IRS_SHIFT  	7
100 #define STK_STATE_EN_AK_SHIFT  	        6
101 #define STK_STATE_EN_ASO_SHIFT  	5
102 #define STK_STATE_EN_IRO_SHIFT  	4
103 #define STK_STATE_EN_WAIT_SHIFT  	2
104 #define STK_STATE_EN_ALS_SHIFT  	1
105 #define STK_STATE_EN_PS_SHIFT  	         0
106 
107 #define STK_STATE_EN_IRS_MASK	      0x80
108 #define STK_STATE_EN_AK_MASK	      0x40
109 #define STK_STATE_EN_ASO_MASK      0x20
110 #define STK_STATE_EN_IRO_MASK	       0x10
111 #define STK_STATE_EN_WAIT_MASK    0x04
112 #define STK_STATE_EN_ALS_MASK	       0x02
113 #define STK_STATE_EN_PS_MASK	       0x01
114 
115 /* Define PS ctrl reg */
116 #define STK_PS_PRS_SHIFT  		        6
117 #define STK_PS_GAIN_SHIFT  		        4
118 #define STK_PS_IT_SHIFT  			0
119 
120 #define STK_PS_PRS_MASK			0xC0
121 #define STK_PS_GAIN_MASK			0x30
122 #define STK_PS_IT_MASK			0x0F
123 
124 /* Define ALS ctrl reg */
125 #define STK_ALS_PRS_SHIFT  		        6
126 #define STK_ALS_GAIN_SHIFT  		4
127 #define STK_ALS_IT_SHIFT  			0
128 
129 #define STK_ALS_PRS_MASK		        0xC0
130 #define STK_ALS_GAIN_MASK		0x30
131 #define STK_ALS_IT_MASK			0x0F
132 
133 /* Define LED ctrl reg */
134 #define STK_LED_IRDR_SHIFT  		6
135 #define STK_LED_DT_SHIFT  		         0
136 
137 #define STK_LED_IRDR_MASK		        0xC0
138 #define STK_LED_DT_MASK			0x3F
139 
140 /* Define interrupt reg */
141 #define STK_INT_CTRL_SHIFT  		 7
142 #define STK_INT_OUI_SHIFT  		         4
143 #define STK_INT_ALS_SHIFT  		         3
144 #define STK_INT_PS_SHIFT  			 0
145 
146 #define STK_INT_CTRL_MASK		         0x80
147 #define STK_INT_OUI_MASK			0x10
148 #define STK_INT_ALS_MASK			0x08
149 #define STK_INT_PS_MASK			0x07
150 
151 #define STK_INT_ALS				0x08
152 
153 /* Define flag reg */
154 #define STK_FLG_ALSDR_SHIFT  		7
155 #define STK_FLG_PSDR_SHIFT  		6
156 #define STK_FLG_ALSINT_SHIFT  		5
157 #define STK_FLG_PSINT_SHIFT  		4
158 #define STK_FLG_OUI_SHIFT  		         2
159 #define STK_FLG_IR_RDY_SHIFT  		1
160 #define STK_FLG_NF_SHIFT  		         0
161 
162 #define STK_FLG_ALSDR_MASK		0x80
163 #define STK_FLG_PSDR_MASK		0x40
164 #define STK_FLG_ALSINT_MASK		0x20
165 #define STK_FLG_PSINT_MASK		0x10
166 #define STK_FLG_OUI_MASK			0x04
167 #define STK_FLG_IR_RDY_MASK		0x02
168 #define STK_FLG_NF_MASK			0x01
169 
170 /* misc define */
171 #define MIN_ALS_POLL_DELAY_NS	20000000
172 
173 #define STK2213_PID			        0x23
174 #define STK3010_PID			        0x33
175 #define STK3311_9_BLK_PID		        0x11
176 #define STK3311_8_PID			        0x12
177 #define STK3210_STK3310_PID	        0x13
178 #define STK3311_9_PID			        0x15
179 
180 #define STK2213C_PID				0x24
181 #define STK3210C_PID				0x18
182 #define STK3311_9C_PID			0x19
183 #define STK3311_8C_PID			0x1A
184 #define STK3310C_PID			0x1B
185 
186 #define STK3310SA_PID		0x17
187 #define STK3311SA_PID		0x1E
188 #define STK3311WV_PID		0x1D
189 #define STK_ERR_PID			0x0
190 
191 
192 #ifdef STK_TUNE0
193 #define STK_MAX_MIN_DIFF	                200
194 #define STK_LT_N_CT	                         100
195 #define STK_HT_N_CT	                         150
196 #endif	/* #ifdef STK_TUNE0 */
197 
198 #define STK_IRC_MAX_ALS_CODE		20000
199 #define STK_IRC_MIN_ALS_CODE		25
200 #define STK_IRC_MIN_IR_CODE		50
201 #define STK_IRC_ALS_DENOMI		2
202 #define STK_IRC_ALS_NUMERA		5
203 #define STK_IRC_ALS_CORREC		748
204 
205 #define DEVICE_NAME		"stk3x1x"
206 #define ALS_NAME        "stk3x1x_ls"
207 #define PS_NAME         "stk3x1x_ps"
208 
209 /* report 1-5 data to android */
210 /*
211 #define NEED_REPORT_MUTIL_LEVEL_DATA 1
212 */
213 #ifdef NEED_REPORT_MUTIL_LEVEL_DATA
214 #define PS_DISTANCE_0 1700
215 #define PS_DISTANCE_1 1360
216 #define PS_DISTANCE_2 1020
217 #define PS_DISTANCE_3 700
218 #define PS_DISTANCE_4 350
219 #define PS_DISTANCE_5 0
220 #endif
221 
222 static int sProbeSuccess;
223 
224 static struct stk3x1x_platform_data stk3x1x_pfdata = {
225 		.state_reg         = 0x0,        /* disable all */
226 		.psctrl_reg         = 0x71,     /* ps_persistance=4, ps_gain=64X, PS_IT=0.391ms */
227 		.alsctrl_reg        = 0x38,     /* als_persistance=1, als_gain=64X, ALS_IT=50ms */
228 		.ledctrl_reg       = 0xFF,     /* 100mA IRDR, 64/64 LED duty */
229 		.wait_reg          = 0x07,      /* 50 ms */
230 		.ps_thd_h          = 1700,
231 		.ps_thd_l           = 1500,
232 		.int_pin             = 0,            /* sprd_3rdparty_gpio_pls_irq*/
233 		.transmittance = 500,
234 };
235 
236 #ifdef STK_ALS_FIR
237 #define STK_FIR_LEN	8
238 #define MAX_FIR_LEN    32
239 struct data_filter {
240 		u16 raw[MAX_FIR_LEN];
241 		int sum;
242 		int number;
243 		int idx;
244 };
245 #endif
246 
247 struct stk3x1x_data {
248 	struct i2c_client *client;
249 #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
250 		int32_t irq;
251 		struct work_struct stk_work;
252 		struct workqueue_struct *stk_wq;
253 #endif
254 	uint16_t ir_code;
255 	uint16_t als_correct_factor;
256 	uint8_t alsctrl_reg;
257 	uint8_t psctrl_reg;
258 	uint8_t ledctrl_reg;
259 	uint8_t state_reg;
260 	int		int_pin;
261 	uint8_t wait_reg;
262 	uint8_t int_reg;
263 	uint16_t ps_thd_h;
264 	uint16_t ps_thd_l;
265 	struct mutex io_lock;
266 	struct input_dev *ps_input_dev;
267 	int32_t ps_distance_last;
268 	int32_t ps_near_state_last;
269 	bool ps_enabled;
270 	bool re_enabled_ps;
271 	/*struct wake_lock ps_wakelock;*/
272 #ifdef STK_POLL_PS
273 	struct hrtimer ps_timer;
274 	struct work_struct stk_ps_work;
275 	struct workqueue_struct *stk_ps_wq;
276 	/*struct wake_lock ps_nosuspend_wl;*/
277 #endif
278 	struct input_dev *als_input_dev;
279 	int32_t als_lux_last;
280 	uint32_t als_transmittance;
281 	bool als_enabled;
282 	bool re_enable_als;
283 	ktime_t ps_poll_delay;
284 	ktime_t als_poll_delay;
285 #ifdef STK_POLL_ALS
286     struct work_struct stk_als_work;
287 	struct hrtimer als_timer;
288 	struct workqueue_struct *stk_als_wq;
289 #endif
290 	bool first_boot;
291 #ifdef STK_TUNE0
292 	uint16_t psa;
293 	uint16_t psi;
294 	uint16_t psi_set;
295 	struct hrtimer ps_tune0_timer;
296 	struct workqueue_struct *stk_ps_tune0_wq;
297     struct work_struct stk_ps_tune0_work;
298 	ktime_t ps_tune0_delay;
299 	bool tune_zero_init_proc;
300 	uint32_t ps_stat_data[3];
301 	int data_count;
302 #endif
303 #ifdef STK_ALS_FIR
304 	struct data_filter      fir;
305 	atomic_t                firlength;
306 #endif
307 	atomic_t	recv_reg;
308 };
309 
310 #if (!defined(CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD))
311 static uint32_t lux_threshold_table[] = {
312 	3,
313 	10,
314 	40,
315 	65,
316 	145,
317 	300,
318 	550,
319 	930,
320 	1250,
321 	1700,
322 };
323 
324 
325 #define LUX_THD_TABLE_SIZE (sizeof(lux_threshold_table) / sizeof(uint32_t) + 1)
326 static uint16_t code_threshold_table[LUX_THD_TABLE_SIZE + 1];
327 #endif
328 
329 static u32 debug_mask;
330 static struct sensor_config_info ls_sensor_info = {
331 	.input_type = LS_TYPE,
332 	.int_number = 0,
333 	.ldo = NULL,
334 };
335 
336 enum {
337 	DEBUG_INIT = 1U << 0,
338 	DEBUG_REPORT_ALS_DATA = 1U << 1,
339 	DEBUG_REPORT_PS_DATA = 1U << 2,
340 	DEBUG_SUSPEND = 1U << 3,
341 	DEBUG_CONTROL_INFO = 1U << 4,
342 	DEBUG_INT = 1U << 5,
343 };
344 
345 #define dprintk(level_mask, fmt, arg...)	{if (unlikely(debug_mask & level_mask)) \
346 	printk("*stk3x1x:*" fmt, ## arg); }
347 
348 static int startup(void);
349 static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable, uint8_t validate_reg);
350 static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable);
351 static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
352 static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
353 static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
354 static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
355 static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data);
356 #ifdef STK_TUNE0
357 static int stk_ps_tune_zero_func_fae(struct stk3x1x_data *ps_data);
358 #endif
359 #ifdef STK_CHK_REG
360 static int stk3x1x_validate_n_handle(struct i2c_client *client);
361 #endif
362 
363 static const unsigned short normal_i2c[2] = {0x48, I2C_CLIENT_END};
364 
stk_detect(struct i2c_client * client,struct i2c_board_info * info)365 static int stk_detect(struct i2c_client *client, struct i2c_board_info *info)
366 {
367 	struct i2c_adapter *adapter = client->adapter;
368     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
369 		return -ENODEV;
370 	if (ls_sensor_info.twi_id == adapter->nr) {
371 		printk("%s: ===========addr= %x\n", __func__, client->addr);
372 		strlcpy(info->type, DEVICE_NAME, I2C_NAME_SIZE);
373 		return 0;
374 	} else {
375 		return -ENODEV;
376 	}
377 }
378 
stk3x1x_i2c_read_data(struct i2c_client * client,unsigned char command,int length,unsigned char * values)379 static int stk3x1x_i2c_read_data(struct i2c_client *client, unsigned char command, int length, unsigned char *values)
380 {
381 	uint8_t retry;
382 	int err;
383 
384 	struct i2c_msg msgs[] = {
385 		{
386 			.addr = client->addr,
387 			.flags = 0,
388 			.len = 1,
389 			.buf = &command,
390 		},
391 		{
392 			.addr = client->addr,
393 			.flags = I2C_M_RD,
394 			.len = length,
395 			.buf = values,
396 		},
397 	};
398 
399 	for (retry = 0; retry < 5; retry++) {
400 		err = i2c_transfer(client->adapter, msgs, 2);
401 		if (err == 2)
402 			break;
403 		else
404 			mdelay(5);
405 	}
406 
407 	if (retry >= 5) {
408 		printk(KERN_ERR "%s: i2c read fail, err=%d\n", __func__, err);
409 		return -EIO;
410 	}
411 	return 0;
412 }
413 
stk3x1x_i2c_write_data(struct i2c_client * client,unsigned char command,int length,unsigned char * values)414 static int stk3x1x_i2c_write_data(struct i2c_client *client, unsigned char command, int length, unsigned char *values)
415 {
416 	int retry;
417 	int err;
418 	unsigned char data[11];
419 	struct i2c_msg msg;
420 	int index;
421 
422 	if (!client)
423 		return -EINVAL;
424 	else if (length >= 10) {
425 		printk(KERN_ERR "%s:length %d exceeds 10\n", __func__, length);
426 		return -EINVAL;
427     }
428 
429 	data[0] = command;
430 	for (index = 1; index <= length; index++)
431 		data[index] = values[index - 1];
432 
433 	msg.addr = client->addr;
434 	msg.flags = 0;
435 	msg.len = length+1;
436 	msg.buf = data;
437 
438 	for (retry = 0; retry < 5; retry++) {
439 		err = i2c_transfer(client->adapter, &msg, 1);
440 		if (err == 1)
441 			break;
442 		else
443 			mdelay(5);
444 	}
445 
446 	if (retry >= 5) {
447 		printk(KERN_ERR "%s: i2c write fail, err=%d\n", __func__, err);
448 		return -EIO;
449 	}
450 	return 0;
451 }
452 
stk3x1x_i2c_smbus_read_byte_data(struct i2c_client * client,unsigned char command)453 static int stk3x1x_i2c_smbus_read_byte_data(struct i2c_client *client, unsigned char command)
454 {
455 	unsigned char value;
456 	int err;
457 	err = stk3x1x_i2c_read_data(client, command, 1, &value);
458 	if (err < 0)
459 		return err;
460 	return value;
461 }
462 
stk3x1x_i2c_smbus_write_byte_data(struct i2c_client * client,unsigned char command,unsigned char value)463 static int stk3x1x_i2c_smbus_write_byte_data(struct i2c_client *client, unsigned char command, unsigned char value)
464 {
465 	int err;
466 	err = stk3x1x_i2c_write_data(client, command, 1, &value);
467 	return err;
468 }
469 
stk_alscode2lux(struct stk3x1x_data * ps_data,uint32_t alscode)470 inline uint32_t stk_alscode2lux(struct stk3x1x_data *ps_data, uint32_t alscode)
471 {
472 	alscode += ((alscode << 7) + (alscode << 3) + (alscode >> 1));
473 	alscode <<= 3;
474 	alscode /= ps_data->als_transmittance;
475 	return alscode;
476 }
477 
stk_lux2alscode(struct stk3x1x_data * ps_data,uint32_t lux)478 uint32_t stk_lux2alscode(struct stk3x1x_data *ps_data, uint32_t lux)
479 {
480     lux *= ps_data->als_transmittance;
481     lux /= 1100;
482     if (unlikely(lux >= (1 << 16)))
483 		lux = (1 << 16) - 1;
484 
485 	return lux;
486 }
487 
488 #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
stk_init_code_threshold_table(struct stk3x1x_data * ps_data)489 static void stk_init_code_threshold_table(struct stk3x1x_data *ps_data)
490 {
491 	uint32_t i, j;
492     uint32_t alscode;
493 
494     code_threshold_table[0] = 0;
495 #ifdef STK_DEBUG_PRINTF
496     printk(KERN_INFO "alscode[0]=%d\n", 0);
497 #endif
498     for (i = 1, j = 0; i < LUX_THD_TABLE_SIZE; i++, j++) {
499 		alscode = stk_lux2alscode(ps_data, lux_threshold_table[j]);
500 		printk(KERN_INFO "alscode[%d]=%d\n", i, alscode);
501 		code_threshold_table[i] = (uint16_t)(alscode);
502     }
503     code_threshold_table[i] = 0xffff;
504     printk(KERN_INFO "alscode[%d]=%d\n", i, alscode);
505 }
506 
stk_get_lux_interval_index(uint16_t alscode)507 static uint32_t stk_get_lux_interval_index(uint16_t alscode)
508 {
509     uint32_t i;
510     for (i = 1; i <= LUX_THD_TABLE_SIZE; i++) {
511 		if ((alscode >= code_threshold_table[i-1]) && (alscode < code_threshold_table[i]))
512 			return i;
513     }
514     return LUX_THD_TABLE_SIZE;
515 }
516 #else
stk_als_set_new_thd(struct stk3x1x_data * ps_data,uint16_t alscode)517 void stk_als_set_new_thd(struct stk3x1x_data *ps_data, uint16_t alscode)
518 {
519     int32_t high_thd, low_thd;
520     high_thd = alscode + stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
521     low_thd = alscode - stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
522     if (high_thd >= (1 << 16))
523 		high_thd = (1 << 16) - 1;
524     if (low_thd < 0)
525 		low_thd = 0;
526     stk3x1x_set_als_thd_h(ps_data, (uint16_t)high_thd);
527     stk3x1x_set_als_thd_l(ps_data, (uint16_t)low_thd);
528 }
529 #endif /*CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD*/
530 
531 
stk3x1x_proc_plat_data(struct stk3x1x_data * ps_data,struct stk3x1x_platform_data * plat_data)532 static void stk3x1x_proc_plat_data(struct stk3x1x_data *ps_data, struct stk3x1x_platform_data *plat_data)
533 {
534 	uint8_t w_reg;
535 
536 	ps_data->state_reg = plat_data->state_reg;
537 	ps_data->psctrl_reg = plat_data->psctrl_reg;
538 #ifdef STK_POLL_PS
539 	ps_data->psctrl_reg &= 0x3F;
540 #endif
541 	ps_data->alsctrl_reg = plat_data->alsctrl_reg;
542 	ps_data->ledctrl_reg = plat_data->ledctrl_reg;
543 
544 	ps_data->wait_reg = plat_data->wait_reg;
545 	if (ps_data->wait_reg < 2) {
546 		printk(KERN_WARNING "%s: wait_reg should be larger than 2, force to write 2\n", __func__);
547 		ps_data->wait_reg = 2;
548 	} else if (ps_data->wait_reg > 0xFF) {
549 		printk(KERN_WARNING "%s: wait_reg should be less than 0xFF, force to write 0xFF\n", __func__);
550 		ps_data->wait_reg = 0xFF;
551 	}
552 #ifndef STK_TUNE0
553 	ps_data->ps_thd_h = plat_data->ps_thd_h;
554 	ps_data->ps_thd_l = plat_data->ps_thd_l;
555 #endif
556 
557 	w_reg = 0;
558 #ifndef STK_POLL_PS
559 	w_reg |= STK_INT_PS_MODE;
560 #else
561 	w_reg |= 0x01;
562 #endif
563 
564 #if (!defined(STK_POLL_ALS) && (STK_INT_PS_MODE != 0x02) && (STK_INT_PS_MODE != 0x03))
565 	w_reg |= STK_INT_ALS;
566 #endif
567 	ps_data->int_reg = w_reg;
568 	return;
569 }
570 
stk3x1x_init_all_reg(struct stk3x1x_data * ps_data)571 static int32_t stk3x1x_init_all_reg(struct stk3x1x_data *ps_data)
572 {
573 	int32_t ret;
574 
575 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, ps_data->state_reg);
576 	if (ret < 0) {
577 		printk(KERN_ERR "%s: write i2c error\n", __func__);
578 		return ret;
579     }
580     ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_PSCTRL_REG, ps_data->psctrl_reg);
581 
582 	if (ret < 0) {
583 		printk(KERN_ERR "%s: write i2c error\n", __func__);
584 		return ret;
585     }
586     ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, ps_data->alsctrl_reg);
587 
588 	if (ret < 0) {
589 		printk(KERN_ERR "%s: write i2c error\n", __func__);
590 		return ret;
591     }
592 
593 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_LEDCTRL_REG, ps_data->ledctrl_reg);
594 
595 	if (ret < 0) {
596 		printk(KERN_ERR "%s: write i2c error\n", __func__);
597 		return ret;
598     }
599 
600 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, ps_data->wait_reg);
601 
602 	if (ret < 0) {
603 		printk(KERN_ERR "%s: write i2c error\n", __func__);
604 		return ret;
605     }
606 
607 #ifdef STK_TUNE0
608 	ps_data->psa = 0x0;
609 	ps_data->psi = 0xFFFF;
610 #else
611 	stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
612 	stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
613 #endif
614 
615 
616 
617 		ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, ps_data->int_reg);
618 		if (ret < 0) {
619 			printk(KERN_ERR "%s: write i2c error\n", __func__);
620 			return ret;
621 			}
622 		return 0;
623 }
624 
625 
stk3x1x_check_pid(struct stk3x1x_data * ps_data)626 static int32_t stk3x1x_check_pid(struct stk3x1x_data *ps_data)
627 {
628 	unsigned char value[2];
629 	int err;
630 
631 	err = stk3x1x_i2c_read_data(ps_data->client, STK_PDT_ID_REG, 2, &value[0]);
632 
633 	if (err < 0) {
634 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, err);
635 		return err;
636 	}
637 
638 	printk(KERN_INFO "%s: PID=0x%x, RID=0x%x\n", __func__, value[0], value[1]);
639 
640 	if (value[1] == 0xC0)
641 		printk(KERN_INFO "%s: RID=0xC0!!!!!!!!!!!!!\n", __func__);
642 
643 	switch (value[0]) {
644 	case STK2213_PID:
645 	case STK3010_PID:
646 	case STK3311_9_BLK_PID:
647 	case STK3311_8_PID:
648 	case STK3210_STK3310_PID:
649 	case STK3311_9_PID:
650 	case STK2213C_PID:
651 	case STK3210C_PID:
652 	case STK3311_9C_PID:
653 	case STK3311_8C_PID:
654 	case STK3310C_PID:
655 	case STK3310SA_PID:
656 	case STK3311SA_PID:
657 	case STK3311WV_PID:
658 		return 0;
659 	case STK_ERR_PID:
660 		printk(KERN_ERR "PID=0x0, please make sure the chip is stk3x1x!\n");
661 		return -2;
662 	default:
663 		printk(KERN_ERR "%s: invalid PID(%#x)\n", __func__, value[0]);
664 		return -1;
665 	}
666 
667 	return 0;
668 }
669 
670 
stk3x1x_software_reset(struct stk3x1x_data * ps_data)671 static int32_t stk3x1x_software_reset(struct stk3x1x_data *ps_data)
672 {
673     int32_t r;
674     uint8_t w_reg;
675 
676     w_reg = 0x7F;
677 
678     r = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, w_reg);
679     if (r < 0) {
680 		printk(KERN_ERR "%s: software reset: write i2c error, ret=%d\n", __func__, r);
681 		return r;
682     }
683     r = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_WAIT_REG);
684     if (w_reg != r) {
685 		printk(KERN_ERR "%s: software reset: read-back value is not the same\n", __func__);
686 		return -1;
687     }
688 
689     r = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_SW_RESET_REG, 0);
690     if (r < 0) {
691 		printk(KERN_ERR "%s: software reset: read error after reset\n", __func__);
692 		return r;
693     }
694     mdelay(1);
695     return 0;
696 }
697 
698 
stk3x1x_set_als_thd_l(struct stk3x1x_data * ps_data,uint16_t thd_l)699 static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
700 {
701 	unsigned char val[2];
702 	int ret;
703 	val[0] = (thd_l & 0xFF00) >> 8;
704 	val[1] = thd_l & 0x00FF;
705 	ret = stk3x1x_i2c_write_data(ps_data->client, STK_THDL1_ALS_REG, 2, val);
706 	if (ret < 0)
707 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
708 	return ret;
709 }
stk3x1x_set_als_thd_h(struct stk3x1x_data * ps_data,uint16_t thd_h)710 static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
711 {
712 	unsigned char val[2];
713 	int ret;
714 	val[0] = (thd_h & 0xFF00) >> 8;
715 	val[1] = thd_h & 0x00FF;
716 	ret = stk3x1x_i2c_write_data(ps_data->client, STK_THDH1_ALS_REG, 2, val);
717 	if (ret < 0)
718 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
719 	return ret;
720 }
721 
stk3x1x_set_ps_thd_l(struct stk3x1x_data * ps_data,uint16_t thd_l)722 static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
723 {
724 	unsigned char val[2];
725 	int ret;
726 	val[0] = (thd_l & 0xFF00) >> 8;
727 	val[1] = thd_l & 0x00FF;
728 	ret = stk3x1x_i2c_write_data(ps_data->client, STK_THDL1_PS_REG, 2, val);
729 	if (ret < 0)
730 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
731 	return ret;
732 }
stk3x1x_set_ps_thd_h(struct stk3x1x_data * ps_data,uint16_t thd_h)733 static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
734 {
735 	unsigned char val[2];
736 	int ret;
737 	val[0] = (thd_h & 0xFF00) >> 8;
738 	val[1] = thd_h & 0x00FF;
739 	ret = stk3x1x_i2c_write_data(ps_data->client, STK_THDH1_PS_REG, 2, val);
740 	if (ret < 0)
741 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
742 	return ret;
743 }
744 
stk3x1x_get_ps_reading(struct stk3x1x_data * ps_data)745 static unsigned short stk3x1x_get_ps_reading(struct stk3x1x_data *ps_data)
746 {
747 	unsigned char value[2];
748 	int err;
749 	err = stk3x1x_i2c_read_data(ps_data->client, STK_DATA1_PS_REG, 2, &value[0]);
750 	if (err < 0) {
751 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, err);
752 		return err;
753 	}
754 	return (value[0] << 8) | value[1];
755 }
756 
757 
stk3x1x_set_flag(struct stk3x1x_data * ps_data,uint8_t org_flag_reg,uint8_t clr)758 static int32_t stk3x1x_set_flag(struct stk3x1x_data *ps_data, uint8_t org_flag_reg, uint8_t clr)
759 {
760 	uint8_t w_flag;
761 	int ret;
762 
763 	w_flag = org_flag_reg | (STK_FLG_ALSINT_MASK | STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK | STK_FLG_IR_RDY_MASK);
764 	w_flag &= (~clr);
765 	/*printk(KERN_INFO "%s: org_flag_reg=0x%x, w_flag = 0x%x\n", __func__, org_flag_reg, w_flag);	*/
766 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_FLAG_REG, w_flag);
767 	if (ret < 0)
768 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
769 	return ret;
770 }
771 
stk3x1x_get_flag(struct stk3x1x_data * ps_data)772 static int32_t stk3x1x_get_flag(struct stk3x1x_data *ps_data)
773 {
774 	int ret;
775 	ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_FLAG_REG);
776 	if (ret < 0)
777 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, ret);
778 	return ret;
779 }
780 
stk3x1x_enable_ps(struct stk3x1x_data * ps_data,uint8_t enable,uint8_t validate_reg)781 static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable, uint8_t validate_reg)
782 {
783     int32_t ret;
784 	uint8_t w_state_reg;
785 	uint8_t curr_ps_enable;
786 	uint32_t reading;
787 	int32_t near_far_state;
788 	dprintk(DEBUG_CONTROL_INFO, "%s  data === %d\n", __func__, enable);
789 #ifdef STK_CHK_REG
790 	if (validate_reg) {
791 		ret = stk3x1x_validate_n_handle(ps_data->client);
792 		if (ret < 0)
793 			printk(KERN_ERR "stk3x1x_validate_n_handle fail: %d\n", ret);
794 	}
795 #endif /* #ifdef STK_CHK_REG */
796 	curr_ps_enable = ps_data->ps_enabled ? 1 : 0;
797 	if (curr_ps_enable == enable)
798 		return 0;
799 
800 #ifdef STK_TUNE0
801 	if (!(ps_data->psi_set) && !enable) {
802 		hrtimer_cancel(&ps_data->ps_tune0_timer);
803 		cancel_work_sync(&ps_data->stk_ps_tune0_work);
804 	}
805 #endif
806 
807 	if (ps_data->first_boot == true) {
808 		ps_data->first_boot = false;
809 	}
810 
811 	ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
812 	if (ret < 0) {
813 			printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
814 			return ret;
815 			}
816 	w_state_reg = ret;
817 
818 	w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | STK_STATE_EN_AK_MASK);
819 	if (enable) {
820 		w_state_reg |= STK_STATE_EN_PS_MASK;
821 		if (!(ps_data->als_enabled))
822 			w_state_reg |= STK_STATE_EN_WAIT_MASK;
823 	}
824 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
825 	if (ret < 0) {
826 		printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
827 		return ret;
828 	}
829 
830 	if (enable) {
831 		printk(KERN_INFO "%s: HT=%d,LT=%d\n", __func__, ps_data->ps_thd_h,  ps_data->ps_thd_l);
832 #ifdef STK_TUNE0
833 		if (!(ps_data->psi_set))
834 			hrtimer_start(&ps_data->ps_tune0_timer, ps_data->ps_tune0_delay, HRTIMER_MODE_REL);
835 #endif
836 
837 #ifdef STK_POLL_PS
838 		hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL);
839 		ps_data->ps_distance_last = -1;
840 		ps_data->ps_near_state_last = 0;
841 #endif
842 #ifndef STK_POLL_PS
843 	#ifndef STK_POLL_ALS
844 		if (!(ps_data->als_enabled))
845 	#endif	/* #ifndef STK_POLL_ALS	*/
846 			enable_irq(ps_data->irq);
847 #endif	/* #ifndef STK_POLL_PS */
848 		ps_data->ps_enabled = true;
849 #ifdef STK_CHK_REG
850 
851 		if (!validate_reg) {
852 			ps_data->ps_distance_last = 1;
853 			input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, 1);
854 			input_sync(ps_data->ps_input_dev);
855 			/*support wake lock for ps
856 			wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
857 			*/
858 			reading = stk3x1x_get_ps_reading(ps_data);
859 			printk(KERN_INFO "%s: force report ps input event=1, ps code = %d\n", __func__, reading);
860 		} else
861 #endif /* #ifdef STK_CHK_REG */
862 		{
863 			msleep(4);
864 			ret = stk3x1x_get_flag(ps_data);
865 			if (ret < 0)
866 				return ret;
867 			near_far_state = ret & STK_FLG_NF_MASK;
868 			ps_data->ps_distance_last = near_far_state;
869 			input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
870 			input_sync(ps_data->ps_input_dev);
871 			/*support wake lock for ps*/
872 			/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
873 			reading = stk3x1x_get_ps_reading(ps_data);
874 			printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n", __func__, near_far_state, reading);
875 		}
876 
877 	} else {
878 #ifdef STK_POLL_PS
879 		hrtimer_cancel(&ps_data->ps_timer);
880 		cancel_work_sync(&ps_data->stk_ps_work);
881 #else
882 #ifndef STK_POLL_ALS
883 		if (!(ps_data->als_enabled))
884 #endif
885 			/*disable_irq(ps_data->irq);
886 */
887 			printk(KERN_ERR "%s: //disable_irq(ps_data->irq);\n", __func__);
888 
889 #endif
890 		ps_data->ps_enabled = false;
891 	}
892 	return ret;
893 }
894 
stk3x1x_enable_als(struct stk3x1x_data * ps_data,uint8_t enable)895 static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable)
896 {
897 	int32_t ret;
898 	uint8_t w_state_reg;
899 	uint8_t curr_als_enable = (ps_data->als_enabled) ? 1 : 0;
900 	dprintk(DEBUG_CONTROL_INFO, "%s  data === %d\n", __func__, enable);
901 	if (curr_als_enable == enable)
902 		return 0;
903 #ifdef STK_IRS
904 	if (enable && !(ps_data->ps_enabled)) {
905 		ret = stk3x1x_get_ir_reading(ps_data);
906 		if (ret > 0)
907 			ps_data->ir_code = ret;
908 	}
909 #endif
910 
911 
912 #ifndef STK_POLL_ALS
913 if (enable) {
914 		stk3x1x_set_als_thd_h(ps_data, 0x0000);
915 		stk3x1x_set_als_thd_l(ps_data, 0xFFFF);
916 	}
917 #endif
918 	ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
919 	if (ret < 0) {
920 			printk(KERN_ERR "%s: write i2c error\n", __func__);
921 			return ret;
922 			}
923 	w_state_reg = (uint8_t)(ret & (~(STK_STATE_EN_ALS_MASK | STK_STATE_EN_WAIT_MASK)));
924 	if (enable)
925 		w_state_reg |= STK_STATE_EN_ALS_MASK;
926 	else if (ps_data->ps_enabled)
927 		w_state_reg |= STK_STATE_EN_WAIT_MASK;
928 
929 
930 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
931 	if (ret < 0) {
932 		printk(KERN_ERR "%s: write i2c error\n", __func__);
933 		return ret;
934 		}
935 	if (enable) {
936 		ps_data->als_enabled = true;
937 #ifdef STK_POLL_ALS
938 		hrtimer_start(&ps_data->als_timer, ps_data->als_poll_delay, HRTIMER_MODE_REL);
939 #else
940 #ifndef STK_POLL_PS
941 		if (!(ps_data->ps_enabled))
942 #endif
943 			enable_irq(ps_data->irq);
944 #endif
945 		} else {
946 		ps_data->als_enabled = false;
947 #ifdef STK_POLL_ALS
948 		hrtimer_cancel(&ps_data->als_timer);
949 		cancel_work_sync(&ps_data->stk_als_work);
950 #else
951 #ifndef STK_POLL_PS
952 		if (!(ps_data->ps_enabled))
953 #endif
954 			disable_irq(ps_data->irq);
955 #endif
956 }
957 		return ret;
958 	}
959 
stk3x1x_get_als_reading(struct stk3x1x_data * ps_data)960 static int32_t stk3x1x_get_als_reading(struct stk3x1x_data *ps_data)
961 {
962     int32_t word_data;
963 #ifdef STK_ALS_FIR
964 	int index;
965 	int firlen = atomic_read(&ps_data->firlength);
966 #endif
967 	unsigned char value[2];
968 	int ret;
969 
970 	ret = stk3x1x_i2c_read_data(ps_data->client, STK_DATA1_ALS_REG, 2, &value[0]);
971 	if (ret < 0) {
972 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
973 		return ret;
974 	}
975 	word_data = (value[0] << 8) | value[1];
976 
977 #ifdef STK_ALS_FIR
978 	if (ps_data->fir.number < firlen) {
979 		ps_data->fir.raw[ps_data->fir.number] = word_data;
980 		ps_data->fir.sum += word_data;
981 		ps_data->fir.number++;
982 		ps_data->fir.idx++;
983 	} else {
984 		index = ps_data->fir.idx % firlen;
985 		ps_data->fir.sum -= ps_data->fir.raw[index];
986 		ps_data->fir.raw[index] = word_data;
987 		ps_data->fir.sum += word_data;
988 		ps_data->fir.idx++;
989 		word_data = ps_data->fir.sum/firlen;
990 	}
991 #endif
992 
993 	return word_data;
994 }
995 
stk3x1x_set_irs_it_slp(struct stk3x1x_data * ps_data,uint16_t * slp_time)996 static int32_t stk3x1x_set_irs_it_slp(struct stk3x1x_data *ps_data, uint16_t *slp_time)
997 {
998 	uint8_t irs_alsctrl;
999 	int32_t ret;
1000 
1001 	irs_alsctrl = (ps_data->alsctrl_reg & 0x0F) - 2;
1002 		switch (irs_alsctrl) {
1003 		case 6:
1004 			*slp_time = 12;
1005 			break;
1006 		case 7:
1007 			*slp_time = 24;
1008 			break;
1009 		case 8:
1010 			*slp_time = 48;
1011 			break;
1012 		case 9:
1013 			*slp_time = 96;
1014 			break;
1015 		default:
1016 			printk(KERN_ERR "%s: unknown ALS IT=0x%x\n", __func__, irs_alsctrl);
1017 			ret = -EINVAL;
1018 			return ret;
1019 	}
1020 	irs_alsctrl |= (ps_data->alsctrl_reg & 0xF0);
1021 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, irs_alsctrl);
1022 	if (ret < 0) {
1023 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1024 		return ret;
1025 	}
1026 	return 0;
1027 }
1028 
stk3x1x_get_ir_reading(struct stk3x1x_data * ps_data)1029 static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data)
1030 {
1031 	int32_t word_data, ret;
1032 	uint8_t w_reg, retry = 0;
1033 	uint16_t irs_slp_time = 100;
1034 	bool re_enable_ps = false;
1035 	unsigned char value[2];
1036 
1037 	if (ps_data->ps_enabled) {
1038 #ifdef STK_TUNE0
1039 		if (!(ps_data->psi_set)) {
1040 			hrtimer_cancel(&ps_data->ps_tune0_timer);
1041 			cancel_work_sync(&ps_data->stk_ps_tune0_work);
1042 		}
1043 #endif
1044 		stk3x1x_enable_ps(ps_data, 0, 1);
1045 		re_enable_ps = true;
1046 	}
1047 
1048 	ret = stk3x1x_set_irs_it_slp(ps_data, &irs_slp_time);
1049 	if (ret < 0)
1050 		goto irs_err_i2c_rw;
1051 
1052 	ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1053 	if (ret < 0) {
1054 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1055 		goto irs_err_i2c_rw;
1056 	}
1057 
1058 	 w_reg = ret | STK_STATE_EN_IRS_MASK;
1059 	 ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
1060 	 if (ret < 0) {
1061 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1062 		goto irs_err_i2c_rw;
1063 	}
1064 	msleep(irs_slp_time);
1065 
1066 	do {
1067 		msleep(3);
1068 		ret = stk3x1x_get_flag(ps_data);
1069 		if (ret < 0)
1070 			goto irs_err_i2c_rw;
1071 		retry++;
1072 	} while (retry < 10 && ((ret&STK_FLG_IR_RDY_MASK) == 0));
1073 
1074 	if (retry == 10) {
1075 		printk(KERN_ERR "%s: ir data is not ready for 300ms\n", __func__);
1076 		ret = -EINVAL;
1077 		goto irs_err_i2c_rw;
1078 	}
1079 
1080 	ret = stk3x1x_set_flag(ps_data, ret, STK_FLG_IR_RDY_MASK);
1081 	if (ret < 0)
1082 		goto irs_err_i2c_rw;
1083 
1084 	ret = stk3x1x_i2c_read_data(ps_data->client, STK_DATA1_IR_REG, 2, &value[0]);
1085 	if (ret < 0) {
1086 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
1087 		goto irs_err_i2c_rw;
1088 	}
1089 	word_data = ((value[0]<<8) | value[1]);
1090 
1091 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, ps_data->alsctrl_reg);
1092 	if (ret < 0) {
1093 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1094 		goto irs_err_i2c_rw;
1095 	}
1096 	if (re_enable_ps)
1097 		stk3x1x_enable_ps(ps_data, 1, 1);
1098 	return word_data;
1099 
1100 irs_err_i2c_rw:
1101 	if (re_enable_ps)
1102 		stk3x1x_enable_ps(ps_data, 1, 1);
1103 	return ret;
1104 }
1105 
1106 #ifdef STK_CHK_REG
stk3x1x_chk_reg_valid(struct stk3x1x_data * ps_data)1107 static int stk3x1x_chk_reg_valid(struct stk3x1x_data *ps_data)
1108 {
1109 	unsigned char value[9];
1110 	int err;
1111 	/*
1112 	uint8_t cnt;
1113 
1114 	for (cnt=0; cnt<9; cnt++) {
1115 		value[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, (cnt+1));
1116 		if (value[cnt] < 0) {
1117 			printk(KERN_ERR "%s fail, ret=%d", __func__, value[cnt]);
1118 			return value[cnt];
1119 		}
1120 	}
1121 	*/
1122 	err = stk3x1x_i2c_read_data(ps_data->client, STK_PSCTRL_REG, 9, &value[0]);
1123 	if (err < 0) {
1124 		printk(KERN_ERR "%s: fail, ret=%d\n", __func__, err);
1125 		return err;
1126 	}
1127 
1128 	if (value[0] != ps_data->psctrl_reg) {
1129 		printk(KERN_ERR "%s: invalid reg 0x01=0x%2x\n", __func__, value[0]);
1130 		return 0xFF;
1131 	}
1132 	if (value[1] != ps_data->alsctrl_reg) {
1133 		printk(KERN_ERR "%s: invalid reg 0x02=0x%2x\n", __func__, value[1]);
1134 		return 0xFF;
1135 	}
1136 	if (value[2] != ps_data->ledctrl_reg) {
1137 		printk(KERN_ERR "%s: invalid reg 0x03=0x%2x\n", __func__, value[2]);
1138 		return 0xFF;
1139 	}
1140 	if (value[3] != ps_data->int_reg) {
1141 		printk(KERN_ERR "%s: invalid reg 0x04=0x%2x\n", __func__, value[3]);
1142 		return 0xFF;
1143 	}
1144 	if (value[4] != ps_data->wait_reg) {
1145 		printk(KERN_ERR "%s: invalid reg 0x05=0x%2x\n", __func__, value[4]);
1146 		return 0xFF;
1147 	}
1148 	if (value[5] != ((ps_data->ps_thd_h & 0xFF00) >> 8)) {
1149 		printk(KERN_ERR "%s: invalid reg 0x06=0x%2x\n", __func__, value[5]);
1150 		return 0xFF;
1151 	}
1152 	if (value[6] != (ps_data->ps_thd_h & 0x00FF)) {
1153 		printk(KERN_ERR "%s: invalid reg 0x07=0x%2x\n", __func__, value[6]);
1154 		return 0xFF;
1155 	}
1156 	if (value[7] != ((ps_data->ps_thd_l & 0xFF00) >> 8)) {
1157 		printk(KERN_ERR "%s: invalid reg 0x08=0x%2x\n", __func__, value[7]);
1158 		return 0xFF;
1159 	}
1160 	if (value[8] != (ps_data->ps_thd_l & 0x00FF)) {
1161 		printk(KERN_ERR "%s: invalid reg 0x09=0x%2x\n", __func__, value[8]);
1162 		return 0xFF;
1163 	}
1164 
1165 	return 0;
1166 }
1167 
stk3x1x_validate_n_handle(struct i2c_client * client)1168 static int stk3x1x_validate_n_handle(struct i2c_client *client)
1169 {
1170 	struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
1171 	int err;
1172 
1173 	err = stk3x1x_chk_reg_valid(ps_data);
1174 	if (err < 0) {
1175 		printk(KERN_ERR "stk3x1x_chk_reg_valid fail: %d\n", err);
1176 		return err;
1177 	}
1178 
1179 	if (err == 0xFF) {
1180 		printk(KERN_ERR "%s: Re-init chip\n", __func__);
1181 		err = stk3x1x_software_reset(ps_data);
1182 		if (err < 0)
1183 			return err;
1184 		err = stk3x1x_init_all_reg(ps_data);
1185 		if (err < 0)
1186 			return err;
1187 
1188 		/*ps_data->psa = 0;
1189 		ps_data->psi = 0xFFFF;
1190 		*/
1191 		stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
1192 		stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
1193 #ifdef STK_ALS_FIR
1194 		memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
1195 #endif
1196 		return 0xFF;
1197 	}
1198 	return 0;
1199 }
1200 #endif /* #ifdef STK_CHK_REG */
stk_als_code_show(struct device * dev,struct device_attribute * attr,char * buf)1201 static ssize_t stk_als_code_show(struct device *dev, struct device_attribute *attr, char *buf)
1202 {
1203 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1204     int32_t reading;
1205 
1206     reading = stk3x1x_get_als_reading(ps_data);
1207     return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
1208 }
1209 
1210 
stk_als_enable_show(struct device * dev,struct device_attribute * attr,char * buf)1211 static ssize_t stk_als_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
1212 {
1213 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1214     int32_t enable, ret;
1215 
1216     mutex_lock(&ps_data->io_lock);
1217 	enable = (ps_data->als_enabled) ? 1 : 0;
1218     mutex_unlock(&ps_data->io_lock);
1219     ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1220     ret = (ret & STK_STATE_EN_ALS_MASK) ? 1 : 0;
1221 
1222 	if (enable != ret)
1223 		printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
1224 
1225 	return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
1226 }
1227 
stk_als_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1228 static ssize_t stk_als_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1229 {
1230 	struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1231 	uint8_t en;
1232 	if (sysfs_streq(buf, "1"))
1233 		en = 1;
1234 	else if (sysfs_streq(buf, "0"))
1235 		en = 0;
1236 	else {
1237 		printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
1238 		return -EINVAL;
1239 	}
1240 		printk(KERN_INFO "%s: Enable ALS : %d\n", __func__, en);
1241 		mutex_lock(&ps_data->io_lock);
1242 		stk3x1x_enable_als(ps_data, en);
1243 		mutex_unlock(&ps_data->io_lock);
1244 		return size;
1245 }
1246 
stk_als_lux_show(struct device * dev,struct device_attribute * attr,char * buf)1247 static ssize_t stk_als_lux_show(struct device *dev, struct device_attribute *attr, char *buf)
1248 {
1249 	struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1250 	int32_t als_reading;
1251 	uint32_t als_lux;
1252 	als_reading = stk3x1x_get_als_reading(ps_data);
1253 	als_lux = stk_alscode2lux(ps_data, als_reading);
1254 	return scnprintf(buf, PAGE_SIZE, "%d lux\n", als_lux);
1255 }
1256 
stk_als_lux_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1257 static ssize_t stk_als_lux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1258 {
1259 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1260 	unsigned long value = 0;
1261 	int ret;
1262 	ret = kstrtoul(buf, 16, &value);
1263 	if (ret < 0) {
1264 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1265 		return ret;
1266 	}
1267 	ps_data->als_lux_last = value;
1268 	input_report_abs(ps_data->als_input_dev, ABS_MISC, value);
1269 	input_sync(ps_data->als_input_dev);
1270 	printk(KERN_INFO "%s: als input event %ld lux\n", __func__, value);
1271 
1272 	return size;
1273 }
1274 
1275 
stk_als_transmittance_show(struct device * dev,struct device_attribute * attr,char * buf)1276 static ssize_t stk_als_transmittance_show(struct device *dev, struct device_attribute *attr, char *buf)
1277 {
1278 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1279 	int32_t transmittance;
1280 	transmittance = ps_data->als_transmittance;
1281 	return scnprintf(buf, PAGE_SIZE, "%d\n", transmittance);
1282 }
1283 
1284 
stk_als_transmittance_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1285 static ssize_t stk_als_transmittance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1286 {
1287 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1288 	unsigned long value = 0;
1289 	int ret;
1290 	ret = kstrtoul(buf, 10, &value);
1291 	if (ret < 0) {
1292 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1293 		return ret;
1294 	}
1295 	ps_data->als_transmittance = value;
1296 	return size;
1297 }
1298 
stk_als_delay_show(struct device * dev,struct device_attribute * attr,char * buf)1299 static ssize_t stk_als_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
1300 {
1301 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1302 	int64_t delay;
1303 	mutex_lock(&ps_data->io_lock);
1304 	delay = ktime_to_ms(ps_data->als_poll_delay);
1305 	mutex_unlock(&ps_data->io_lock);
1306 	return scnprintf(buf, PAGE_SIZE, "%lld\n", delay);
1307 }
1308 
1309 
stk_als_delay_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1310 static ssize_t stk_als_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1311 {
1312 	uint64_t value = 0;
1313 	int ret;
1314 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1315 	ret = kstrtoull(buf, 10, &value);
1316 	if (ret < 0) {
1317 		printk(KERN_ERR "%s:kstrtoull failed, ret=0x%x\n", __func__, ret);
1318 		return ret;
1319 	}
1320 #ifdef STK_DEBUG_PRINTF
1321 	printk(KERN_INFO "%s: set als poll delay=%lld\n", __func__, value);
1322 #endif
1323     value = value * 1000 * 1000;
1324 	if (value < MIN_ALS_POLL_DELAY_NS) {
1325 		printk(KERN_ERR "%s: delay is too small\n", __func__);
1326 		value = MIN_ALS_POLL_DELAY_NS;
1327 	}
1328 	mutex_lock(&ps_data->io_lock);
1329 	if (value != ktime_to_ns(ps_data->als_poll_delay))
1330 		ps_data->als_poll_delay = ns_to_ktime(value);
1331 #ifdef STK_ALS_FIR
1332 	ps_data->fir.number = 0;
1333 	ps_data->fir.idx = 0;
1334 	ps_data->fir.sum = 0;
1335 #endif
1336 	mutex_unlock(&ps_data->io_lock);
1337 	return size;
1338 }
1339 
stk_als_ir_code_show(struct device * dev,struct device_attribute * attr,char * buf)1340 static ssize_t stk_als_ir_code_show(struct device *dev, struct device_attribute *attr, char *buf)
1341 {
1342 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1343 	int32_t reading;
1344 	reading = stk3x1x_get_ir_reading(ps_data);
1345 	return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
1346 }
1347 
1348 #ifdef STK_ALS_FIR
stk_als_firlen_show(struct device * dev,struct device_attribute * attr,char * buf)1349 static ssize_t stk_als_firlen_show(struct device *dev, struct device_attribute *attr, char *buf)
1350 {
1351 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1352 	int len = atomic_read(&ps_data->firlength);
1353 
1354 	printk(KERN_INFO "%s: len = %2d, idx = %2d\n", __func__, len, ps_data->fir.idx);
1355 	printk(KERN_INFO "%s: sum = %5d, ave = %5d\n", __func__, ps_data->fir.sum, ps_data->fir.sum / len);
1356 
1357 	return scnprintf(buf, PAGE_SIZE, "%d\n", len);
1358 }
1359 
1360 
stk_als_firlen_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1361 static ssize_t stk_als_firlen_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1362 {
1363     uint64_t value = 0;
1364 	int ret;
1365 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1366 	ret = kstrtoull(buf, 10, &value);
1367 	if (ret < 0) {
1368 		printk(KERN_ERR "%s:kstrtoull failed, ret=0x%x\n", __func__, ret);
1369 		return ret;
1370 	}
1371 
1372 	if (value > MAX_FIR_LEN) {
1373 		printk(KERN_ERR "%s: firlen exceed maximum filter length\n", __func__);
1374 	} else if (value < 1) {
1375 		atomic_set(&ps_data->firlength, 1);
1376 		memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
1377 	} else {
1378 		atomic_set(&ps_data->firlength, value);
1379 		memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
1380 	}
1381 	return size;
1382 }
1383 #endif  /* #ifdef STK_ALS_FIR */
1384 
stk_ps_code_show(struct device * dev,struct device_attribute * attr,char * buf)1385 static ssize_t stk_ps_code_show(struct device *dev, struct device_attribute *attr, char *buf)
1386 {
1387 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1388     uint32_t reading;
1389     reading = stk3x1x_get_ps_reading(ps_data);
1390     return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
1391 }
1392 
stk_ps_enable_show(struct device * dev,struct device_attribute * attr,char * buf)1393 static ssize_t stk_ps_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
1394 {
1395     int32_t enable, ret;
1396 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1397 
1398     mutex_lock(&ps_data->io_lock);
1399 	enable = (ps_data->ps_enabled) ? 1 : 0;
1400     mutex_unlock(&ps_data->io_lock);
1401     ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1402     ret = (ret & STK_STATE_EN_PS_MASK) ? 1 : 0;
1403 
1404 	if (enable != ret)
1405 		printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
1406 
1407 	return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
1408 }
1409 
stk_ps_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1410 static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1411 {
1412 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1413 	uint8_t en;
1414 	if (sysfs_streq(buf, "1"))
1415 		en = 1;
1416 	else if (sysfs_streq(buf, "0"))
1417 		en = 0;
1418 	else {
1419 		printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
1420 		return -EINVAL;
1421 	}
1422     printk(KERN_INFO "%s: Enable PS : %d\n", __func__, en);
1423     mutex_lock(&ps_data->io_lock);
1424     stk3x1x_enable_ps(ps_data, en, 1);
1425     mutex_unlock(&ps_data->io_lock);
1426     return size;
1427 }
1428 
stk_ps_delay_show(struct device * dev,struct device_attribute * attr,char * buf)1429 static ssize_t stk_ps_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
1430 {
1431 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1432 	int64_t delay;
1433 	mutex_lock(&ps_data->io_lock);
1434 	delay = ktime_to_ms(ps_data->ps_poll_delay);
1435 	mutex_unlock(&ps_data->io_lock);
1436 	return scnprintf(buf, PAGE_SIZE, "%lld\n", delay);
1437 }
1438 
1439 
stk_ps_delay_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1440 static ssize_t stk_ps_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1441 {
1442 	uint64_t value = 0;
1443 	int ret;
1444 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1445 	ret = kstrtoull(buf, 10, &value);
1446 	if (ret < 0) {
1447 		printk(KERN_ERR "%s:kstrtoull failed, ret=0x%x\n", __func__, ret);
1448 		return ret;
1449 	}
1450 #ifdef STK_DEBUG_PRINTF
1451 	printk(KERN_INFO "%s: set ps poll delay=%lld\n", __func__, value);
1452 #endif
1453     value = value * 1000 * 1000;
1454 	if (value < MIN_ALS_POLL_DELAY_NS) {
1455 		printk(KERN_ERR "%s: delay is too small\n", __func__);
1456 		value = MIN_ALS_POLL_DELAY_NS;
1457 	}
1458 	mutex_lock(&ps_data->io_lock);
1459 	if (value != ktime_to_ns(ps_data->ps_poll_delay))
1460 		ps_data->ps_poll_delay = ns_to_ktime(value);
1461 #ifdef STK_ALS_FIR
1462 	ps_data->fir.number = 0;
1463 	ps_data->fir.idx = 0;
1464 	ps_data->fir.sum = 0;
1465 #endif
1466 	mutex_unlock(&ps_data->io_lock);
1467 	return size;
1468 }
1469 
stk_ps_enable_aso_show(struct device * dev,struct device_attribute * attr,char * buf)1470 static ssize_t stk_ps_enable_aso_show(struct device *dev, struct device_attribute *attr, char *buf)
1471 {
1472     int32_t ret;
1473     struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1474 
1475     ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1476     ret = (ret & STK_STATE_EN_ASO_MASK) ? 1 : 0;
1477 
1478     return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
1479 }
1480 
stk_ps_enable_aso_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1481 static ssize_t stk_ps_enable_aso_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1482 {
1483 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1484 	uint8_t en;
1485 	int32_t ret;
1486 	uint8_t w_state_reg;
1487 
1488 	 if (sysfs_streq(buf, "1"))
1489 		en = 1;
1490 	 else if (sysfs_streq(buf, "0"))
1491 		en = 0;
1492 	 else {
1493 		printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
1494 		return -EINVAL;
1495 	 }
1496 	 printk(KERN_INFO "%s: Enable PS ASO : %d\n", __func__, en);
1497 
1498 	 ret = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1499 
1500 	 if (ret < 0) {
1501 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1502 		return ret;
1503 		}
1504 	w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_ASO_MASK));
1505 	if (en)
1506 		w_state_reg |= STK_STATE_EN_ASO_MASK;
1507 
1508 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
1509 
1510 	if (ret < 0) {
1511 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1512 		return ret;
1513 	}
1514 
1515 	return size;
1516 }
1517 
1518 
stk_ps_offset_show(struct device * dev,struct device_attribute * attr,char * buf)1519 static ssize_t stk_ps_offset_show(struct device *dev, struct device_attribute *attr, char *buf)
1520 {
1521 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1522 	int32_t word_data;
1523 	unsigned char value[2];
1524 	int ret;
1525 
1526 	ret = stk3x1x_i2c_read_data(ps_data->client, STK_DATA1_OFFSET_REG, 2, &value[0]);
1527 	if (ret < 0) {
1528 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
1529 		return ret;
1530 	}
1531 	word_data = (value[0] << 8) | value[1];
1532 
1533 	return scnprintf(buf, PAGE_SIZE, "%d\n", word_data);
1534 }
1535 
stk_ps_offset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1536 static ssize_t stk_ps_offset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1537 {
1538 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1539 	unsigned long offset = 0;
1540 	int ret;
1541 	unsigned char val[2];
1542 
1543 	ret = kstrtoul(buf, 10, &offset);
1544 	if (ret < 0) {
1545 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1546 		return ret;
1547 	}
1548 	if (offset > 65535) {
1549 		printk(KERN_ERR "%s: invalid value, offset=%ld\n", __func__, offset);
1550 		return -EINVAL;
1551 	}
1552 
1553 	val[0] = (offset & 0xFF00) >> 8;
1554 	val[1] = offset & 0x00FF;
1555 	ret = stk3x1x_i2c_write_data(ps_data->client, STK_DATA1_OFFSET_REG, 2, val);
1556 	if (ret < 0) {
1557 		printk(KERN_ERR "%s: write i2c error\n", __func__);
1558 		return ret;
1559 	}
1560 
1561 	return size;
1562 }
1563 
1564 
stk_ps_distance_show(struct device * dev,struct device_attribute * attr,char * buf)1565 static ssize_t stk_ps_distance_show(struct device *dev, struct device_attribute *attr, char *buf)
1566 {
1567 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1568 	int32_t dist = 1, ret;
1569 	ret = stk3x1x_get_flag(ps_data);
1570 	if (ret < 0)
1571 		return ret;
1572 	dist = (ret & STK_FLG_NF_MASK) ? 1 : 0;
1573 
1574 	ps_data->ps_distance_last = dist;
1575 	input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, dist);
1576 	input_sync(ps_data->ps_input_dev);
1577 	/*support wake lock for ps*/
1578 	/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
1579 	printk(KERN_INFO "%s: ps input event %d cm\n", __func__, dist);
1580 	return scnprintf(buf, PAGE_SIZE, "%d\n", dist);
1581 }
1582 
1583 
1584 
stk_ps_distance_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1585 static ssize_t stk_ps_distance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1586 {
1587 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1588 	unsigned long value = 0;
1589 	int ret;
1590 	ret = kstrtoul(buf, 10, &value);
1591 	if (ret < 0) {
1592 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1593 		return ret;
1594 	}
1595 	ps_data->ps_distance_last = value;
1596 	input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, value);
1597 	input_sync(ps_data->ps_input_dev);
1598 	/*support wake lock for ps*/
1599 	/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
1600 	printk(KERN_INFO "%s: ps input event %ld cm\n", __func__, value);
1601 	return size;
1602 }
1603 
1604 
stk_ps_code_thd_l_show(struct device * dev,struct device_attribute * attr,char * buf)1605 static ssize_t stk_ps_code_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
1606 {
1607 	int32_t ps_thd_l1_reg, ps_thd_l2_reg;
1608 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1609 	ps_thd_l1_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDL1_PS_REG);
1610 	if (ps_thd_l1_reg < 0) {
1611 		printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l1_reg);
1612 		return -EINVAL;
1613 	}
1614 	ps_thd_l2_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDL2_PS_REG);
1615 	if (ps_thd_l2_reg < 0) {
1616 		printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l2_reg);
1617 		return -EINVAL;
1618 	}
1619 	ps_thd_l1_reg = ps_thd_l1_reg << 8 | ps_thd_l2_reg;
1620 	return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg);
1621 }
1622 
1623 
stk_ps_code_thd_l_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1624 static ssize_t stk_ps_code_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1625 {
1626 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1627 	unsigned long value = 0;
1628 	int ret;
1629 	ret = kstrtoul(buf, 10, &value);
1630 	if (ret < 0) {
1631 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1632 		return ret;
1633 	}
1634 	stk3x1x_set_ps_thd_l(ps_data, value);
1635 	return size;
1636 }
1637 
stk_ps_code_thd_h_show(struct device * dev,struct device_attribute * attr,char * buf)1638 static ssize_t stk_ps_code_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
1639 {
1640 	int32_t ps_thd_h1_reg, ps_thd_h2_reg;
1641 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1642 	ps_thd_h1_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDH1_PS_REG);
1643 	if (ps_thd_h1_reg < 0) {
1644 		printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h1_reg);
1645 		return -EINVAL;
1646 	}
1647 	ps_thd_h2_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDH2_PS_REG);
1648 	if (ps_thd_h2_reg < 0) {
1649 		printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h2_reg);
1650 		return -EINVAL;
1651 	}
1652 	ps_thd_h1_reg = ps_thd_h1_reg << 8 | ps_thd_h2_reg;
1653 	return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg);
1654 }
1655 
1656 
stk_ps_code_thd_h_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1657 static ssize_t stk_ps_code_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1658 {
1659 	struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1660 	unsigned long value = 0;
1661 	int ret;
1662 	ret = kstrtoul(buf, 10, &value);
1663 	if (ret < 0) {
1664 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1665 		return ret;
1666 	}
1667 	stk3x1x_set_ps_thd_h(ps_data, value);
1668 	return size;
1669 }
1670 
1671 #if 0
1672 static ssize_t stk_als_lux_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
1673 {
1674     int32_t als_thd_l0_reg, als_thd_l1_reg;
1675 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1676 	uint32_t als_lux;
1677 	als_thd_l0_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDL1_ALS_REG);
1678 	als_thd_l1_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDL2_ALS_REG);
1679     if (als_thd_l0_reg < 0) {
1680 		printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l0_reg);
1681 		return -EINVAL;
1682 	}
1683 	if (als_thd_l1_reg < 0) {
1684 		printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l1_reg);
1685 		return -EINVAL;
1686 	}
1687     als_thd_l0_reg |= (als_thd_l1_reg << 8);
1688 	als_lux = stk_alscode2lux(ps_data, als_thd_l0_reg);
1689     return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
1690 }
1691 
1692 
1693 static ssize_t stk_als_lux_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1694 {
1695 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1696 	unsigned long value = 0;
1697 	int ret;
1698 	ret = kstrtoul(buf, 10, &value);
1699 	if (ret < 0) {
1700 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1701 		return ret;
1702 	}
1703 	value = stk_lux2alscode(ps_data, value);
1704     stk3x1x_set_als_thd_l(ps_data, value);
1705     return size;
1706 }
1707 
1708 static ssize_t stk_als_lux_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
1709 {
1710     int32_t als_thd_h0_reg, als_thd_h1_reg;
1711 	struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1712 	uint32_t als_lux;
1713 	als_thd_h0_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDH1_ALS_REG);
1714     als_thd_h1_reg = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_THDH2_ALS_REG);
1715     if (als_thd_h0_reg < 0) {
1716 		printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h0_reg);
1717 		return -EINVAL;
1718 	}
1719 	if (als_thd_h1_reg < 0) {
1720 		printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h1_reg);
1721 		return -EINVAL;
1722 	}
1723     als_thd_h0_reg |= (als_thd_h1_reg << 8);
1724 	als_lux = stk_alscode2lux(ps_data, als_thd_h0_reg);
1725     return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
1726 }
1727 
1728 
1729 static ssize_t stk_als_lux_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1730 {
1731 	struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1732 	unsigned long value = 0;
1733 	int ret;
1734 	ret = kstrtoul(buf, 10, &value);
1735 	if (ret < 0) {
1736 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1737 		return ret;
1738 	}
1739     value = stk_lux2alscode(ps_data, value);
1740     stk3x1x_set_als_thd_h(ps_data, value);
1741     return size;
1742 }
1743 #endif
1744 
stk_all_reg_show(struct device * dev,struct device_attribute * attr,char * buf)1745 static ssize_t stk_all_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
1746 {
1747     int32_t ps_reg[0x22];
1748 	uint8_t cnt;
1749 	int len = 0;
1750 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1751 
1752 	for (cnt = 0; cnt < 0x20; cnt++) {
1753 		ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, (cnt));
1754 		if (ps_reg[cnt] < 0) {
1755 			printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1756 			return -EINVAL;
1757 		} else {
1758 			printk(KERN_INFO "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]);
1759 			len += scnprintf(buf+len, PAGE_SIZE-len, "[%2X]%2X,", cnt, ps_reg[cnt]);
1760 		}
1761 	}
1762 	ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG);
1763 	if (ps_reg[cnt] < 0) {
1764 		printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1765 		return -EINVAL;
1766 	}
1767 	printk(KERN_INFO "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]);
1768 	cnt++;
1769 	ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG);
1770 	if (ps_reg[cnt] < 0) {
1771 		printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1772 		return -EINVAL;
1773 	}
1774 	printk(KERN_INFO "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]);
1775 	len += scnprintf(buf+len, PAGE_SIZE-len, "[%2X]%2X,[%2X]%2X\n", cnt-1, ps_reg[cnt-1], cnt, ps_reg[cnt]);
1776 	return len;
1777 	/*return scnprintf(buf, PAGE_SIZE, "[0]%2X [1]%2X [2]%2X [3]%2X [4]%2X [5]%2X [6/7 HTHD]%2X,%2X [8/9 LTHD]%2X, %2X [A]%2X [B]%2X [C]%2X [D]%2X [E/F Aoff]%2X,%2X,[10]%2X [11/12 PS]%2X,%2X [13]%2X [14]%2X [15/16 Foff]%2X,%2X [17]%2X [18]%2X [3E]%2X [3F]%2X\n",
1778 		ps_reg[0], ps_reg[1], ps_reg[2], ps_reg[3], ps_reg[4], ps_reg[5], ps_reg[6], ps_reg[7], ps_reg[8],
1779 		ps_reg[9], ps_reg[10], ps_reg[11], ps_reg[12], ps_reg[13], ps_reg[14], ps_reg[15], ps_reg[16], ps_reg[17],
1780 		ps_reg[18], ps_reg[19], ps_reg[20], ps_reg[21], ps_reg[22], ps_reg[23], ps_reg[24], ps_reg[25], ps_reg[26]);
1781 		*/
1782 }
1783 
stk_status_show(struct device * dev,struct device_attribute * attr,char * buf)1784 static ssize_t stk_status_show(struct device *dev, struct device_attribute *attr, char *buf)
1785 {
1786 	int32_t ps_reg[27];
1787 	uint8_t cnt;
1788 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1789 	for (cnt = 0; cnt < 25; cnt++) {
1790 		ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, (cnt));
1791 		if (ps_reg[cnt] < 0) {
1792 			printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1793 			return -EINVAL;
1794 		} else {
1795 			printk(KERN_INFO "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]);
1796 		}
1797 	}
1798 	ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG);
1799 	if (ps_reg[cnt] < 0) {
1800 		printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1801 		return -EINVAL;
1802 	}
1803 	printk(KERN_INFO "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]);
1804 	cnt++;
1805 	ps_reg[cnt] = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG);
1806 	if (ps_reg[cnt] < 0) {
1807 		printk(KERN_ERR "%s fail, ret=%d", __func__, ps_reg[cnt]);
1808 		return -EINVAL;
1809 	}
1810 	printk(KERN_INFO "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]);
1811 	return scnprintf(buf, PAGE_SIZE, "[PS=%2X] [ALS=%2X] [WAIT=0x%4Xms] [EN_ASO=%2X] [EN_AK=%2X] [NEAR/FAR=%2X] [FLAG_OUI=%2X] [FLAG_PSINT=%2X] [FLAG_ALSINT=%2X]\n",
1812 		ps_reg[0] & 0x01, (ps_reg[0] & 0x02) >> 1, ((ps_reg[0] & 0x04) >> 2) * ps_reg[5] * 6, (ps_reg[0]&0x20) >> 5,
1813 		(ps_reg[0] & 0x40) >> 6, ps_reg[16] & 0x01, (ps_reg[16] & 0x04) >> 2, (ps_reg[16] & 0x10) >> 4, (ps_reg[16] & 0x20) >> 5);
1814 }
1815 
stk_recv_show(struct device * dev,struct device_attribute * attr,char * buf)1816 static ssize_t stk_recv_show(struct device *dev, struct device_attribute *attr, char *buf)
1817 {
1818 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1819 	return scnprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&ps_data->recv_reg));
1820 }
1821 
1822 
stk_recv_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1823 static ssize_t stk_recv_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1824 {
1825 	unsigned long value = 0;
1826 	int ret;
1827 	int32_t recv_data;
1828 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1829 	ret = kstrtoul(buf, 16, &value);
1830 	if (ret < 0) {
1831 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1832 		return ret;
1833 	}
1834 	recv_data = stk3x1x_i2c_smbus_read_byte_data(ps_data->client, value);
1835 	printk("%s: reg 0x%x=0x%x\n", __func__, (int)value, recv_data);
1836 	atomic_set(&ps_data->recv_reg, recv_data);
1837 	return size;
1838 }
1839 
stk_send_show(struct device * dev,struct device_attribute * attr,char * buf)1840 static ssize_t stk_send_show(struct device *dev, struct device_attribute *attr, char *buf)
1841 {
1842 	return 0;
1843 }
1844 
1845 
stk_send_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1846 static ssize_t stk_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1847 {
1848 	int addr, cmd;
1849 	int32_t ret, i;
1850 	char *token[10];
1851 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1852 	for (i = 0; i < 2; i++)
1853 		token[i] = strsep((char **)&buf, " ");
1854 	ret = kstrtoul(token[0], 16, (unsigned long *)&(addr));
1855 	if (ret < 0) {
1856 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1857 		return ret;
1858 	}
1859 	ret = kstrtoul(token[1], 16, (unsigned long *)&(cmd));
1860 	if (ret < 0) {
1861 		printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n", __func__, ret);
1862 		return ret;
1863 	}
1864 	printk(KERN_INFO "%s: write reg 0x%x=0x%x\n", __func__, addr, cmd);
1865 
1866 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, (unsigned char)addr, (unsigned char)cmd);
1867 	if (0 != ret) {
1868 		printk(KERN_ERR "%s: stk3x1x_i2c_smbus_write_byte_data fail\n", __func__);
1869 		return ret;
1870 	}
1871 	return size;
1872 }
1873 
1874 #ifdef STK_TUNE0
1875 
stk_ps_cali_show(struct device * dev,struct device_attribute * attr,char * buf)1876 static ssize_t stk_ps_cali_show(struct device *dev, struct device_attribute *attr, char *buf)
1877 {
1878 	struct stk3x1x_data *ps_data =  dev_get_drvdata(dev);
1879 	int32_t word_data;
1880 	unsigned char value[2];
1881 	int ret;
1882 
1883 	ret = stk3x1x_i2c_read_data(ps_data->client, 0x20, 2, &value[0]);
1884 	if (ret < 0) {
1885 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
1886 		return ret;
1887 	}
1888 	word_data = (value[0] << 8) | value[1];
1889 
1890 	ret = stk3x1x_i2c_read_data(ps_data->client, 0x22, 2, &value[0]);
1891 	if (ret < 0) {
1892 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
1893 		return ret;
1894 	}
1895 	word_data += ((value[0] << 8) | value[1]);
1896 
1897 	printk("%s: psi_set=%d, psa=%d,psi=%d, word_data=%d\n", __func__,
1898 		ps_data->psi_set, ps_data->psa, ps_data->psi, word_data);
1899 	return 0;
1900 }
1901 
1902 #endif	/* #ifdef STK_TUNE0 */
1903 
1904 static struct device_attribute als_enable_attribute = __ATTR(enable, 0660, stk_als_enable_show, stk_als_enable_store);
1905 static struct device_attribute als_lux_attribute = __ATTR(lux, 0664, stk_als_lux_show, stk_als_lux_store);
1906 static struct device_attribute als_code_attribute = __ATTR(code, 0444, stk_als_code_show, NULL);
1907 static struct device_attribute als_transmittance_attribute = __ATTR(transmittance, 0664, stk_als_transmittance_show, stk_als_transmittance_store);
1908 #if 0
1909 static struct device_attribute als_lux_thd_l_attribute = __ATTR(luxthdl, 0664, stk_als_lux_thd_l_show, stk_als_lux_thd_l_store);
1910 static struct device_attribute als_lux_thd_h_attribute = __ATTR(luxthdh, 0664, stk_als_lux_thd_h_show, stk_als_lux_thd_h_store);
1911 #endif
1912 static struct device_attribute als_poll_delay_attribute = __ATTR(ls_poll_delay, 0660, stk_als_delay_show, stk_als_delay_store);
1913 static struct device_attribute als_ir_code_attribute = __ATTR(ircode, 0444, stk_als_ir_code_show, NULL);
1914 #ifdef STK_ALS_FIR
1915 static struct device_attribute als_firlen_attribute = __ATTR(firlen, 0664, stk_als_firlen_show, stk_als_firlen_store);
1916 #endif
1917 
1918 
1919 static struct attribute *stk_als_attrs[] = {
1920 	&als_enable_attribute.attr,
1921     &als_lux_attribute.attr,
1922     &als_code_attribute.attr,
1923     &als_transmittance_attribute.attr,
1924 #if 0
1925 	&als_lux_thd_l_attribute.attr,
1926 	&als_lux_thd_h_attribute.attr,
1927 #endif
1928 	&als_poll_delay_attribute.attr,
1929 	&als_ir_code_attribute.attr,
1930 #ifdef STK_ALS_FIR
1931 	&als_firlen_attribute.attr,
1932 #endif
1933     NULL
1934 };
1935 
1936 /*
1937 static struct attribute_group stk_als_attribute_group = {
1938 	.name = "driver",
1939 	.attrs = stk_als_attrs,
1940 };
1941 */
1942 
1943 static struct device_attribute ps_enable_attribute = __ATTR(enable, 0660, stk_ps_enable_show, stk_ps_enable_store);
1944 static struct device_attribute ps_delay_attribute = __ATTR(ps_poll_delay, 0660, stk_ps_delay_show, stk_ps_delay_store);
1945 static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso, 0664, stk_ps_enable_aso_show, stk_ps_enable_aso_store);
1946 static struct device_attribute ps_distance_attribute = __ATTR(distance, 0664, stk_ps_distance_show, stk_ps_distance_store);
1947 static struct device_attribute ps_offset_attribute = __ATTR(offset, 0664, stk_ps_offset_show, stk_ps_offset_store);
1948 static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
1949 static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl, 0664, stk_ps_code_thd_l_show, stk_ps_code_thd_l_store);
1950 static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh, 0664, stk_ps_code_thd_h_show, stk_ps_code_thd_h_store);
1951 static struct device_attribute recv_attribute = __ATTR(recv, 0664, stk_recv_show, stk_recv_store);
1952 static struct device_attribute send_attribute = __ATTR(send, 0664, stk_send_show, stk_send_store);
1953 static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
1954 static struct device_attribute status_attribute = __ATTR(status, 0444, stk_status_show, NULL);
1955 #ifdef STK_TUNE0
1956 static struct device_attribute ps_cali_attribute = __ATTR(cali, 0444, stk_ps_cali_show, NULL);
1957 #endif
1958 
1959 
1960 static struct attribute *stk_ps_attrs[] = {
1961     &ps_enable_attribute.attr,
1962     &ps_delay_attribute.attr,
1963     &ps_enable_aso_attribute.attr,
1964     &ps_distance_attribute.attr,
1965 	&ps_offset_attribute.attr,
1966     &ps_code_attribute.attr,
1967 	&ps_code_thd_l_attribute.attr,
1968 	&ps_code_thd_h_attribute.attr,
1969 	&recv_attribute.attr,
1970 	&send_attribute.attr,
1971 	&all_reg_attribute.attr,
1972 	&status_attribute.attr,
1973 #ifdef STK_TUNE0
1974 	&ps_cali_attribute.attr,
1975 #endif
1976     NULL
1977 };
1978 
1979 /*
1980 static struct attribute_group stk_ps_attribute_group = {
1981 	.name = "driver",
1982 	.attrs = stk_ps_attrs,
1983 };*/
1984 
1985 #ifdef STK_TUNE0
stk_ps_tune_zero_val(struct stk3x1x_data * ps_data)1986 static int stk_ps_tune_zero_val(struct stk3x1x_data *ps_data)
1987 {
1988 	int mode;
1989 	int32_t word_data, lii;
1990 	unsigned char value[2];
1991 	int ret;
1992 
1993 	ret = stk3x1x_i2c_read_data(ps_data->client, 0x20, 2, &value[0]);
1994 	if (ret < 0) {
1995 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
1996 		return ret;
1997 	}
1998 	word_data = (value[0] << 8) | value[1];
1999 
2000 	ret = stk3x1x_i2c_read_data(ps_data->client, 0x22, 2, &value[0]);
2001 	if (ret < 0) {
2002 		printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
2003 		return ret;
2004 	}
2005 	word_data += ((value[0] << 8) | value[1]);
2006 
2007 	mode = (ps_data->psctrl_reg) & 0x3F;
2008 	if (mode == 0x30)
2009 		lii = 100;
2010 	else if (mode == 0x31)
2011 		lii = 200;
2012 	else if (mode == 0x32)
2013 		lii = 400;
2014 	else if (mode == 0x33)
2015 		lii = 800;
2016 	else {
2017 		printk(KERN_ERR "%s: unsupported PS_IT(0x%x)\n", __func__, mode);
2018 		return -1;
2019 	}
2020 
2021 	if (word_data > lii) {
2022 		printk(KERN_INFO "%s: word_data=%d, lii=%d\n", __func__, word_data, lii);
2023 		return 0xFFFF;
2024 	}
2025 	return 0;
2026 }
2027 
stk_ps_tune_zero_final(struct stk3x1x_data * ps_data)2028 static int stk_ps_tune_zero_final(struct stk3x1x_data *ps_data)
2029 {
2030 	int ret;
2031 
2032 	ps_data->tune_zero_init_proc = false;
2033 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, ps_data->int_reg);
2034 	if (ret < 0) {
2035 		printk(KERN_ERR "%s: write i2c error\n", __func__);
2036 		return ret;
2037 	}
2038 
2039 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, 0);
2040 	if (ret < 0) {
2041 		printk(KERN_ERR "%s: write i2c error\n", __func__);
2042 		return ret;
2043 	}
2044 
2045 	if (ps_data->data_count == -1) {
2046 		printk(KERN_INFO "%s: exceed limit\n", __func__);
2047 		hrtimer_cancel(&ps_data->ps_tune0_timer);
2048 		return 0;
2049 	}
2050 
2051 	ps_data->psa = ps_data->ps_stat_data[0];
2052 	ps_data->psi = ps_data->ps_stat_data[2];
2053 	ps_data->ps_thd_h = ps_data->ps_stat_data[1] + STK_HT_N_CT;
2054 	ps_data->ps_thd_l = ps_data->ps_stat_data[1] + STK_LT_N_CT;
2055 	stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
2056 	stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
2057 	printk(KERN_INFO "%s: set HT=%d,LT=%d\n", __func__, ps_data->ps_thd_h, ps_data->ps_thd_l);
2058 	hrtimer_cancel(&ps_data->ps_tune0_timer);
2059 	return 0;
2060 }
2061 
stk_tune_zero_get_ps_data(struct stk3x1x_data * ps_data)2062 static int32_t stk_tune_zero_get_ps_data(struct stk3x1x_data *ps_data)
2063 {
2064 	uint32_t ps_adc;
2065 	int ret;
2066 
2067 	ret = stk_ps_tune_zero_val(ps_data);
2068 	if (ret == 0xFFFF) {
2069 		ps_data->data_count = -1;
2070 		stk_ps_tune_zero_final(ps_data);
2071 		return 0;
2072 	}
2073 
2074 	ps_adc = stk3x1x_get_ps_reading(ps_data);
2075 	printk(KERN_INFO "%s: ps_adc #%d=%d\n", __func__, ps_data->data_count, ps_adc);
2076 	if (ps_adc < 0)
2077 		return ps_adc;
2078 
2079 	ps_data->ps_stat_data[1]  +=  ps_adc;
2080 	if (ps_adc > ps_data->ps_stat_data[0])
2081 		ps_data->ps_stat_data[0] = ps_adc;
2082 	if (ps_adc < ps_data->ps_stat_data[2])
2083 		ps_data->ps_stat_data[2] = ps_adc;
2084 	ps_data->data_count++;
2085 
2086 	if (ps_data->data_count == 5) {
2087 		ps_data->ps_stat_data[1] /= ps_data->data_count;
2088 		stk_ps_tune_zero_final(ps_data);
2089 	}
2090 
2091 	return 0;
2092 }
2093 
stk_ps_tune_zero_init(struct stk3x1x_data * ps_data)2094 static int stk_ps_tune_zero_init(struct stk3x1x_data *ps_data)
2095 {
2096 	int32_t ret = 0;
2097 	uint8_t w_state_reg;
2098 
2099 	ps_data->psi_set = 0;
2100 	ps_data->tune_zero_init_proc = true;
2101 	ps_data->ps_stat_data[0] = 0;
2102 	ps_data->ps_stat_data[2] = 9999;
2103 	ps_data->ps_stat_data[1] = 0;
2104 	ps_data->data_count = 0;
2105 
2106 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, 0);
2107 	if (ret < 0) {
2108 		printk(KERN_ERR "%s: write i2c error\n", __func__);
2109 		return ret;
2110 	}
2111 
2112 	w_state_reg = (STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK);
2113 	ret = stk3x1x_i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
2114 	if (ret < 0) {
2115 		printk(KERN_ERR "%s: write i2c error\n", __func__);
2116 		return ret;
2117 	}
2118 	hrtimer_start(&ps_data->ps_tune0_timer, ps_data->ps_tune0_delay, HRTIMER_MODE_REL);
2119 	return 0;
2120 }
2121 
stk_ps_tune_zero_func_fae(struct stk3x1x_data * ps_data)2122 static int stk_ps_tune_zero_func_fae(struct stk3x1x_data *ps_data)
2123 {
2124 	int32_t word_data;
2125 	int ret, diff;
2126 	unsigned char value[2];
2127 
2128 	if (ps_data->psi_set || !(ps_data->ps_enabled))
2129 		return 0;
2130 
2131 	ret = stk3x1x_get_flag(ps_data);
2132 	if (ret < 0)
2133 		return ret;
2134 	if (!(ret&STK_FLG_PSDR_MASK)) {
2135 		/*printk(KERN_INFO "%s: ps data is not ready yet\n", __func__);*/
2136 		return 0;
2137 	}
2138 
2139 	ret = stk_ps_tune_zero_val(ps_data);
2140 	if (ret == 0) {
2141 		ret = stk3x1x_i2c_read_data(ps_data->client, 0x11, 2, &value[0]);
2142 		if (ret < 0) {
2143 			printk(KERN_ERR "%s fail, ret=0x%x", __func__, ret);
2144 			return ret;
2145 		}
2146 		word_data = (value[0]<<8) | value[1];
2147 		/*printk(KERN_INFO "%s: word_data=%d\n", __func__, word_data);*/
2148 
2149 		if (word_data == 0) {
2150 			/*printk(KERN_ERR "%s: incorrect word data (0)\n", __func__);*/
2151 			return 0xFFFF;
2152 		}
2153 
2154 		if (word_data > ps_data->psa) {
2155 			ps_data->psa = word_data;
2156 			printk(KERN_INFO "%s: update psa: psa=%d,psi=%d\n", __func__, ps_data->psa, ps_data->psi);
2157 		}
2158 		if (word_data < ps_data->psi) {
2159 			ps_data->psi = word_data;
2160 			printk(KERN_INFO "%s: update psi: psa=%d,psi=%d\n", __func__, ps_data->psa, ps_data->psi);
2161 		}
2162 	}
2163 	diff = ps_data->psa - ps_data->psi;
2164 	if (diff > STK_MAX_MIN_DIFF) {
2165 		ps_data->psi_set = ps_data->psi;
2166 		ps_data->ps_thd_h = ps_data->psi + STK_HT_N_CT;
2167 		ps_data->ps_thd_l = ps_data->psi + STK_LT_N_CT;
2168 		stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
2169 		stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
2170 #ifdef STK_DEBUG_PRINTF
2171 	printk(KERN_INFO "%s: FAE tune0 psa-psi(%d) > STK_DIFF found\n", __func__, diff);
2172 #endif
2173 	hrtimer_cancel(&ps_data->ps_tune0_timer);
2174 	}
2175 
2176 	return 0;
2177 }
2178 
stk_ps_tune0_work_func(struct work_struct * work)2179 static void stk_ps_tune0_work_func(struct work_struct *work)
2180 {
2181 	struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_tune0_work);
2182 	if (ps_data->tune_zero_init_proc)
2183 		stk_tune_zero_get_ps_data(ps_data);
2184 	else
2185 		stk_ps_tune_zero_func_fae(ps_data);
2186 	return;
2187 }
2188 
2189 
stk_ps_tune0_timer_func(struct hrtimer * timer)2190 static enum hrtimer_restart stk_ps_tune0_timer_func(struct hrtimer *timer)
2191 {
2192 	struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_tune0_timer);
2193 	queue_work(ps_data->stk_ps_tune0_wq, &ps_data->stk_ps_tune0_work);
2194 	hrtimer_forward_now(&ps_data->ps_tune0_timer, ps_data->ps_tune0_delay);
2195 	return HRTIMER_RESTART;
2196 }
2197 #endif
2198 
2199 #ifdef STK_POLL_ALS
stk_als_timer_func(struct hrtimer * timer)2200 static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer)
2201 {
2202 	struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer);
2203 	queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work);
2204 	hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay);
2205 	return HRTIMER_RESTART;
2206 }
2207 
stk_als_poll_work_func(struct work_struct * work)2208 static void stk_als_poll_work_func(struct work_struct *work)
2209 {
2210 	struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_als_work);
2211 	int32_t reading, reading_lux, als_comperator, flag_reg;
2212 	flag_reg = stk3x1x_get_flag(ps_data);
2213 	if (flag_reg < 0)
2214 		return;
2215 
2216 	if (!(flag_reg&STK_FLG_ALSDR_MASK))
2217 		return;
2218 
2219 	reading = stk3x1x_get_als_reading(ps_data);
2220 	if (reading < 0)
2221 		return;
2222 
2223 	if (ps_data->ir_code) {
2224 		ps_data->als_correct_factor = 1000;
2225 		if (reading < STK_IRC_MAX_ALS_CODE && reading > STK_IRC_MIN_ALS_CODE &&
2226 			ps_data->ir_code > STK_IRC_MIN_IR_CODE) {
2227 			als_comperator = reading * STK_IRC_ALS_NUMERA / STK_IRC_ALS_DENOMI;
2228 			if (ps_data->ir_code > als_comperator)
2229 				ps_data->als_correct_factor = STK_IRC_ALS_CORREC;
2230 		}
2231 #ifdef STK_DEBUG_PRINTF
2232 		printk(KERN_INFO "%s: als=%d, ir=%d, als_correct_factor=%d", __func__, reading, ps_data->ir_code, ps_data->als_correct_factor);
2233 #endif
2234 		ps_data->ir_code = 0;
2235 	}
2236 	reading = reading * ps_data->als_correct_factor / 1000;
2237 
2238 	reading_lux = stk_alscode2lux(ps_data, reading);
2239 	if (abs(ps_data->als_lux_last - reading_lux) >= STK_ALS_CHANGE_THD) {
2240 		ps_data->als_lux_last = reading_lux;
2241 		dprintk(DEBUG_REPORT_ALS_DATA, "lightsensor data = %d\n", reading_lux);
2242 		input_report_abs(ps_data->als_input_dev, ABS_MISC, reading_lux);
2243 		input_sync(ps_data->als_input_dev);
2244 #ifdef STK_DEBUG_PRINTF
2245 		printk(KERN_INFO "%s: als input event %d lux\n", __func__, reading_lux);
2246 #endif
2247 	}
2248 	return;
2249 }
2250 #endif /* #ifdef STK_POLL_ALS */
2251 
2252 
2253 #ifdef STK_POLL_PS
stk_ps_timer_func(struct hrtimer * timer)2254 static enum hrtimer_restart stk_ps_timer_func(struct hrtimer *timer)
2255 {
2256 	struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_timer);
2257 	queue_work(ps_data->stk_ps_wq, &ps_data->stk_ps_work);
2258 	hrtimer_forward_now(&ps_data->ps_timer, ps_data->ps_poll_delay);
2259 	return HRTIMER_RESTART;
2260 }
2261 
2262 #define CODE_H_THD  1800
2263 #define CODE_L_THD  1400
2264 
2265 #ifdef NEED_REPORT_MUTIL_LEVEL_DATA
stk_get_distance_form_code(unsigned short code)2266 static int32_t stk_get_distance_form_code(unsigned short code)
2267 {
2268 	int32_t distance;
2269 	if (code >= PS_DISTANCE_0)
2270 		distance = 0;
2271 	else if (code >= PS_DISTANCE_1)
2272 		distance = 1;
2273 	else if (code >= PS_DISTANCE_2)
2274 		distance = 2;
2275 	else if (code >= PS_DISTANCE_3)
2276 		distance = 3;
2277 	else if (code >= PS_DISTANCE_4)
2278 		distance = 4;
2279 	else
2280 		distance = 5;
2281 	return distance;
2282 }
2283 #endif
2284 
stk_ps_poll_work_func(struct work_struct * work)2285 static void stk_ps_poll_work_func(struct work_struct *work)
2286 {
2287 	struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work);
2288 	unsigned short reading;
2289 	int32_t near_far_state;
2290 	uint8_t org_flag_reg;
2291 	int32_t ret;
2292 	uint8_t disable_flag = 0;
2293 #ifdef STK_TUNE0
2294 	if (!(ps_data->psi_set) || !(ps_data->ps_enabled))
2295 		return;
2296 #endif
2297 
2298 	org_flag_reg = stk3x1x_get_flag(ps_data);
2299 	if (org_flag_reg < 0)
2300 		goto err_i2c_rw;
2301 	if (!(org_flag_reg&STK_FLG_PSDR_MASK))
2302 		return;
2303 
2304 #ifdef NEED_REPORT_MUTIL_LEVEL_DATA
2305 	reading = stk3x1x_get_ps_reading(ps_data);
2306 	near_far_state = stk_get_distance_form_code(reading);
2307 #else
2308 	near_far_state = (org_flag_reg & STK_FLG_NF_MASK) ? 1 : 0;
2309 	reading = stk3x1x_get_ps_reading(ps_data);
2310 #endif
2311 	ps_data->ps_distance_last = near_far_state;
2312 	dprintk(DEBUG_REPORT_ALS_DATA, "proximity state = %d\n", near_far_state);
2313 	input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
2314 	input_sync(ps_data->ps_input_dev);
2315 	/*support wake lock for ps*/
2316 	/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
2317 
2318 #ifdef STK_DEBUG_PRINTF
2319 	printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n", __func__, near_far_state, reading);
2320 #endif
2321 	ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
2322 	if (ret < 0)
2323 		goto err_i2c_rw;
2324 
2325 #if REPORT_KEY
2326 	if (ps_data->ps_near_state_last == 0 && reading > CODE_H_THD) {
2327 		printk(KERN_INFO "%s: >>>>>>>>>>>>\n", __func__);
2328 		input_report_key(ps_data->ps_input_dev, KEY_PROX_NEAR, 1);
2329 		input_sync(ps_data->ps_input_dev);
2330 		msleep(100);
2331 		input_report_key(ps_data->ps_input_dev, KEY_PROX_NEAR, 0);
2332 		input_sync(ps_data->ps_input_dev);
2333 		ps_data->ps_near_state_last = 1;
2334 	}
2335 	if (ps_data->ps_near_state_last == 1 && reading < CODE_L_THD) {
2336 		printk(KERN_INFO "%s: <<<<<<<<<<<<\n", __func__);
2337 		input_report_key(ps_data->ps_input_dev, KEY_PROX_FAR, 1);
2338 		input_sync(ps_data->ps_input_dev);
2339 		msleep(100);
2340 		input_report_key(ps_data->ps_input_dev, KEY_PROX_FAR, 0);
2341 		input_sync(ps_data->ps_input_dev);
2342 		ps_data->ps_near_state_last = 0;
2343 	}
2344 #endif
2345 	return;
2346 
2347 err_i2c_rw:
2348 	msleep(30);
2349 	return;
2350 }
2351 #endif
2352 
2353 #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
stk_work_func(struct work_struct * work)2354 static void stk_work_func(struct work_struct *work)
2355 {
2356 	uint32_t reading;
2357 #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02))
2358 	int32_t ret;
2359 	uint8_t disable_flag = 0;
2360 	uint8_t org_flag_reg = 0;
2361 #endif	/* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */
2362 
2363 #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
2364 	uint32_t nLuxIndex;
2365 #endif
2366 	struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_work);
2367 	int32_t near_far_state;
2368 	int32_t als_comperator;
2369 
2370 #if (STK_INT_PS_MODE == 0x03)
2371 	near_far_state = gpio_get_value(ps_data->int_pin);
2372 #elif (STK_INT_PS_MODE	== 0x02)
2373 	near_far_state = !(gpio_get_value(ps_data->int_pin));
2374 #endif
2375 
2376 #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE	== 0x02))
2377 	ps_data->ps_distance_last = near_far_state;
2378 	input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
2379 	input_sync(ps_data->ps_input_dev);
2380 	/*support wake lock for ps*/
2381 	/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
2382 	reading = stk3x1x_get_ps_reading(ps_data);
2383 #ifdef STK_DEBUG_PRINTF
2384 	printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n", __func__, near_far_state, reading);
2385 #endif
2386 #else
2387 	/* mode 0x01 or 0x04 */
2388 	org_flag_reg = stk3x1x_get_flag(ps_data);
2389 	if (org_flag_reg < 0)
2390 		goto err_i2c_rw;
2391 	if (org_flag_reg & STK_FLG_ALSINT_MASK) {
2392 		disable_flag |= STK_FLG_ALSINT_MASK;
2393 		reading = stk3x1x_get_als_reading(ps_data);
2394 		if (reading < 0) {
2395 			printk(KERN_ERR "%s: stk3x1x_get_als_reading fail, ret=%d", __func__, reading);
2396 			goto err_i2c_rw;
2397 		}
2398 #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
2399 		nLuxIndex = stk_get_lux_interval_index(reading);
2400 		stk3x1x_set_als_thd_h(ps_data, code_threshold_table[nLuxIndex]);
2401 		stk3x1x_set_als_thd_l(ps_data, code_threshold_table[nLuxIndex-1]);
2402 #else
2403 		stk_als_set_new_thd(ps_data, reading);
2404 #endif /*CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD*/
2405 
2406 		if (ps_data->ir_code) {
2407 			if (reading < STK_IRC_MAX_ALS_CODE && reading > STK_IRC_MIN_ALS_CODE &&
2408 			ps_data->ir_code > STK_IRC_MIN_IR_CODE) {
2409 				als_comperator = reading * STK_IRC_ALS_NUMERA / STK_IRC_ALS_DENOMI;
2410 				if (ps_data->ir_code > als_comperator)
2411 					ps_data->als_correct_factor = STK_IRC_ALS_CORREC;
2412 				else
2413 					ps_data->als_correct_factor = 1000;
2414 			}
2415 			printk(KERN_INFO "%s: als=%d, ir=%d, als_correct_factor=%d", __func__, reading, ps_data->ir_code, ps_data->als_correct_factor);
2416 			ps_data->ir_code = 0;
2417 		}
2418 
2419 		reading = reading * ps_data->als_correct_factor / 1000;
2420 
2421 		ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
2422 		input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
2423 		input_sync(ps_data->als_input_dev);
2424 #ifdef STK_DEBUG_PRINTF
2425 		printk(KERN_INFO "%s: als input event %d lux\n", __func__, ps_data->als_lux_last);
2426 #endif
2427     }
2428     if (org_flag_reg & STK_FLG_PSINT_MASK) {
2429 		disable_flag |= STK_FLG_PSINT_MASK;
2430 		near_far_state = (org_flag_reg & STK_FLG_NF_MASK) ? 1 : 0;
2431 
2432 		ps_data->ps_distance_last = near_far_state;
2433 		input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
2434 		input_sync(ps_data->ps_input_dev);
2435 		/*support wake lock for ps*/
2436 		/*wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);*/
2437 		reading = stk3x1x_get_ps_reading(ps_data);
2438 		printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n", __func__, near_far_state, reading);
2439 #ifdef STK_DEBUG_PRINTF
2440 		printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n", __func__, near_far_state, reading);
2441 #endif
2442 
2443 		ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
2444 		if (ret < 0)
2445 			goto err_i2c_rw;
2446 
2447 		/*added by guoying if we are in far away  status, asleep the device*/
2448 		/*if (near_far_state) {
2449 			printk(KERN_ERR "%s:object leave before report power key event\n", __func__);
2450 			input_report_key(ps_data->ps_input_dev, KEY_PROX_SLEEP, 1);*/
2451 			input_sync(ps_data->ps_input_dev);
2452 			msleep(100);
2453 			/*input_report_key(ps_data->ps_input_dev, KEY_PROX_SLEEP, 0);*/
2454 			input_sync(ps_data->ps_input_dev);
2455 			/*printk(KERN_ERR "%s: object leave after report power key event\n", __func__);
2456 			}*/
2457 		if (!near_far_state) {
2458 			/*printk(KERN_ERR "%s: object come before report power key event\n", __func__);
2459 			input_report_key(ps_data->ps_input_dev, KEY_PROX_WAKE, 1);*/
2460 			input_sync(ps_data->ps_input_dev);
2461 			msleep(100);
2462 			/*input_report_key(ps_data->ps_input_dev, KEY_PROX_WAKE, 0);*/
2463 			input_sync(ps_data->ps_input_dev);
2464 			/*printk(KERN_ERR "%s: object come after report power key event\n", __func__);*/
2465 		}
2466 	}
2467 
2468 #endif
2469 
2470 	msleep(1);
2471 	input_set_int_enable(&(ls_sensor_info.input_type), 1);
2472 	return;
2473 
2474 err_i2c_rw:
2475 	msleep(30);
2476 	input_set_int_enable(&(ls_sensor_info.input_type), 1);
2477 	return;
2478 }
2479 /*#include <mach/platform.h>*/
2480 #include <asm/io.h>
2481 
2482 /*#define PLDATA_INTERRUPT_REG_VADDR SUNXI_R_PIO_VBASE + 0x238*/
stk_oss_irq_handler(int irq,void * data)2483 static irqreturn_t stk_oss_irq_handler(int irq, void *data)
2484 {
2485 	struct stk3x1x_data *pData = data;
2486 	input_set_int_enable(&(ls_sensor_info.input_type), 0);
2487 
2488 	/*pr_err("aw==== in the stk_oss_irq_handle:0x%x\n", readl(PLDATA_INTERRUPT_REG_VADDR));*/
2489 	queue_work(pData->stk_wq, &pData->stk_work);
2490 	return IRQ_HANDLED;
2491 }
2492 #endif	/*	#if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))	*/
stk3x1x_init_all_setting(struct i2c_client * client,struct stk3x1x_platform_data * plat_data)2493 static int32_t stk3x1x_init_all_setting(struct i2c_client *client, struct stk3x1x_platform_data *plat_data)
2494 {
2495 	int32_t ret;
2496 	struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
2497 
2498 	stk3x1x_proc_plat_data(ps_data, plat_data);
2499 	ret = stk3x1x_software_reset(ps_data);
2500 	if (ret < 0)
2501 		return ret;
2502 
2503 	ret = stk3x1x_check_pid(ps_data);
2504 	if (ret < 0)
2505 		return ret;
2506 	ret = stk3x1x_init_all_reg(ps_data);
2507 	if (ret < 0)
2508 		return ret;
2509 
2510 	ps_data->als_enabled = false;
2511 	ps_data->ps_enabled = false;
2512 	ps_data->re_enable_als = false;
2513 	ps_data->re_enabled_ps = false;
2514 	ps_data->ir_code = 0;
2515 	ps_data->als_correct_factor = 1000;
2516 	ps_data->first_boot = true;
2517 #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
2518 	stk_init_code_threshold_table(ps_data);
2519 #endif
2520 #ifdef STK_TUNE0
2521 	stk_ps_tune_zero_init(ps_data);
2522 #endif
2523 #ifdef STK_ALS_FIR
2524 	memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
2525 	atomic_set(&ps_data->firlength, STK_FIR_LEN);
2526 #endif
2527 	atomic_set(&ps_data->recv_reg, 0);
2528     return 0;
2529 }
2530 
2531 #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
stk3x1x_setup_irq(struct i2c_client * client)2532 static int stk3x1x_setup_irq(struct i2c_client *client)
2533 {
2534 	int err = -EIO;
2535 	struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
2536 
2537 	pr_err("aw==== %s:light sensor irq_number= %d\n", __func__,
2538 			ls_sensor_info.int_number);
2539 
2540 	ls_sensor_info.dev = &(ps_data->ps_input_dev->dev);
2541 	if (0 != ls_sensor_info.int_number) {
2542 		err = input_request_int(&(ls_sensor_info.input_type), stk_oss_irq_handler,
2543 				IRQF_TRIGGER_FALLING, ps_data);
2544 		if (err) {
2545 			printk("Failed to request gpio irq \n");
2546 			return err;
2547 		}
2548 	}
2549 	err = 0;
2550 	return err;
2551 
2552 }
2553 #endif
2554 
2555 
2556 #if IS_ENABLED(CONFIG_PM)
stk3x1x_suspend(struct device * dev)2557 static int stk3x1x_suspend(struct device *dev)
2558 {
2559 	struct i2c_client *client =  to_i2c_client(dev);
2560 	struct stk3x1x_data *ps_data =  i2c_get_clientdata(client);
2561 	int err;
2562 #ifndef STK_POLL_PS
2563 	/*struct i2c_client *client = to_i2c_client(dev);*/
2564 #endif
2565 #if 0
2566 	if (NORMAL_STANDBY == standby_type) {
2567 
2568 		/* process for super standby */
2569 	} else if (SUPER_STANDBY == standby_type) {
2570 
2571 		if (check_scene_locked(SCENE_TALKING_STANDBY) == 0) {
2572 			printk("lradc-key: talking standby, enable wakeup source lradc!!\n");
2573 			enable_wakeup_src(CPUS_GPIO_SRC, 0);
2574 		} else {
2575 				}
2576 	}
2577 
2578 	return 0;
2579 #endif
2580 
2581 	printk(KERN_INFO "%s\n", __func__);
2582 	mutex_lock(&ps_data->io_lock);
2583 #ifdef STK_CHK_REG
2584 	err = stk3x1x_validate_n_handle(ps_data->client);
2585 	if (err < 0)
2586 		printk(KERN_ERR "stk3x1x_validate_n_handle fail: %d\n", err);
2587 	else if (err == 0xFF) {
2588 		if (ps_data->ps_enabled)
2589 			stk3x1x_enable_ps(ps_data, 1, 0);
2590 	}
2591 #endif
2592 	if (ps_data->als_enabled) {
2593 		stk3x1x_enable_als(ps_data, 0);
2594 		ps_data->re_enable_als = true;
2595 	}
2596 	if (ps_data->ps_enabled) {
2597 #ifdef STK_POLL_PS
2598 	/*wake_lock(&ps_data->ps_nosuspend_wl);*/
2599        {
2600 			stk3x1x_enable_ps(ps_data, 0, 1);
2601 			ps_data->re_enabled_ps = true;
2602 			pr_err("aw==== in the re enable ps \n");
2603        }
2604 #else
2605 		pr_err("aw==== suspend \n");
2606 
2607 
2608 		if (SUPER_STANDBY == standby_type) {
2609 			err = enable_wakeup_src(CPUS_GPIO_SRC, 0);
2610 			if (err)
2611 				printk(KERN_WARNING "%s: set_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
2612 		} else {
2613 			printk(KERN_ERR "%s: not support wakeup source", __func__);
2614 		}
2615 #endif
2616 	}
2617 	mutex_unlock(&ps_data->io_lock);
2618 
2619 	if (ls_sensor_info.sensor_power_ldo != NULL) {
2620 		err = regulator_disable(ls_sensor_info.sensor_power_ldo);
2621 		if (err)
2622 			printk("stk power down failed\n");
2623 	}
2624 
2625 	return 0;
2626 }
2627 
stk3x1x_resume(struct device * dev)2628 static int stk3x1x_resume(struct device *dev)
2629 {
2630 	struct i2c_client *client =  to_i2c_client(dev);
2631 	/*added by guoying*/
2632 	uint8_t disable_flag = 0;
2633 	uint8_t org_flag_reg;
2634 	int32_t ret;
2635 	int err;
2636 	int32_t near_far_state;
2637 	uint32_t reading;
2638 	struct stk3x1x_data *ps_data;
2639 	/*ended by guoying*/
2640 	#if 0
2641 	if (NORMAL_STANDBY == standby_type) {
2642 
2643 	/* process for super standby */
2644 	} else if (SUPER_STANDBY == standby_type) {
2645 		if (check_scene_locked(SCENE_TALKING_STANDBY) != 0) {
2646 		} else {
2647 			disable_wakeup_src(CPUS_GPIO_SRC, 0);
2648 			printk("aw=== ps-key: resume from talking standby!!\n");
2649 		}
2650 	}
2651 	return 0;
2652 	#endif
2653 	if (ls_sensor_info.sensor_power_ldo != NULL) {
2654 		err = regulator_enable(ls_sensor_info.sensor_power_ldo);
2655 		if (err)
2656 			printk("stk power on failed\n");
2657 		}
2658 	ps_data =  i2c_get_clientdata(client);
2659 #ifndef STK_POLL_PS
2660 	/*struct i2c_client *client = to_i2c_client(dev);*/
2661 #endif
2662 	printk(KERN_INFO "%s\n", __func__);
2663 
2664 	mutex_lock(&ps_data->io_lock);
2665 #ifdef STK_CHK_REG
2666 	err = stk3x1x_validate_n_handle(ps_data->client);
2667 
2668 	if (err < 0) {
2669 		printk(KERN_ERR "stk3x1x_validate_n_handle fail: %d\n", err);
2670 	} else if (err == 0xFF) {
2671 		if (ps_data->ps_enabled)
2672 		stk3x1x_enable_ps(ps_data, 1, 0);
2673 	}
2674 #endif
2675 	if (ps_data->re_enable_als) {
2676 		stk3x1x_enable_als(ps_data, 1);
2677 		ps_data->re_enable_als = false;
2678 	}
2679 	if (ps_data->ps_enabled) {
2680 #ifdef STK_POLL_PS
2681 
2682 	/*wake_unlock(&ps_data->ps_nosuspend_wl);*/
2683 #else
2684 		if (SUPER_STANDBY == standby_type) {
2685 			err = disable_wakeup_src(CPUS_GPIO_SRC, 0);
2686 			if (err)
2687 				printk(KERN_WARNING "%s: disable_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
2688 		}
2689 #endif
2690 	} else if (ps_data->re_enabled_ps) {
2691 		stk3x1x_enable_ps(ps_data, 1, 1);
2692 		ps_data->re_enabled_ps = false;
2693 	}
2694 	mutex_unlock(&ps_data->io_lock);
2695 
2696 	/* added by guoying */
2697 	org_flag_reg = stk3x1x_get_flag(ps_data);
2698 	if (org_flag_reg & STK_FLG_PSINT_MASK) {
2699 		printk(KERN_ERR "%s:before stk3x1x_set_flag ,org_flag_reg = 0x%X\n", __func__, org_flag_reg);
2700 		disable_flag |= STK_FLG_PSINT_MASK;
2701 		near_far_state = (org_flag_reg & STK_FLG_NF_MASK) ? 1 : 0;
2702 	    reading = stk3x1x_get_ps_reading(ps_data);
2703 		printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n", __func__, near_far_state, reading);
2704 		ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
2705 	    if (ret < 0)
2706 			return 0;
2707 		/*if we are in near status, wake up the device*/
2708 		/*if (!near_far_state) {
2709 		printk(KERN_ERR "%s: object come in resume before report power key event\n", __func__);
2710 		input_report_key(ps_data->ps_input_dev, KEY_PROX_WAKE, 1);*/
2711 		input_sync(ps_data->ps_input_dev);
2712 		msleep(100);
2713 		/*input_report_key(ps_data->ps_input_dev, KEY_PROX_WAKE, 0);*/
2714 		input_sync(ps_data->ps_input_dev);
2715 		printk(KERN_ERR "%s: after report power key event\n", __func__);	/*
2716 		}	*/
2717 	}
2718 	return 0;
2719 }
2720 
2721 #endif
2722 
stk3x1x_sysfs_create_files(struct kobject * kobj,struct attribute ** attrs)2723 static int stk3x1x_sysfs_create_files(struct kobject *kobj, struct attribute **attrs)
2724 {
2725 		int err;
2726 		while (*attrs != NULL) {
2727 			err = sysfs_create_file(kobj, *attrs);
2728 			if (err)
2729 				return err;
2730 			attrs++;
2731        }
2732 		return 0;
2733 }
2734 
stk3x1x_sysfs_remove_files(struct kobject * kobj,struct attribute ** attrs)2735 static int stk3x1x_sysfs_remove_files(struct kobject *kobj, struct attribute **attrs)
2736 {
2737 			while (*attrs != NULL) {
2738 				sysfs_remove_file(kobj, *attrs);
2739 				attrs++;
2740 				}
2741 			return 0;
2742 }
2743 
stk3x1x_probe(struct i2c_client * client,const struct i2c_device_id * id)2744 static int stk3x1x_probe(struct i2c_client *client,
2745 			const struct i2c_device_id *id)
2746 {
2747 		int err = -ENODEV;
2748 		struct stk3x1x_data *ps_data;
2749 		struct stk3x1x_platform_data *plat_data;
2750 /*
2751 *added by guoying
2752 */
2753 		struct gpio_config *pin_cfg = &ls_sensor_info.irq_gpio;
2754 		unsigned long config;
2755 /*
2756 *ended by guoying
2757 */
2758 		if (ls_sensor_info.dev == NULL)
2759 			ls_sensor_info.dev = &client->dev;
2760 		printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION);
2761 
2762 		if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2763 			printk(KERN_ERR "%s: No Support for I2C_FUNC_I2C\n", __func__);
2764 			return -ENODEV;
2765 			}
2766 
2767 		ps_data = kzalloc(sizeof(struct stk3x1x_data), GFP_KERNEL);
2768 		if (!ps_data) {
2769 			printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__);
2770 			return -ENOMEM;
2771 			}
2772 		ps_data->client = client;
2773 		i2c_set_clientdata(client, ps_data);
2774 		mutex_init(&ps_data->io_lock);
2775 
2776 	/*support wake lock for ps*/
2777 /*	wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock");
2778 #ifdef STK_POLL_PS
2779 	wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock");
2780 #endif	*/
2781 		plat_data = &stk3x1x_pfdata;
2782 		ps_data->als_transmittance = plat_data->transmittance;
2783 		ps_data->int_pin = plat_data->int_pin;
2784 		if (ps_data->als_transmittance == 0) {
2785 			printk(KERN_ERR "%s: Please set als_transmittance in platform data\n", __func__);
2786 			goto err_als_input_allocate;
2787 			}
2788 
2789 	ps_data->als_input_dev = input_allocate_device();
2790 	if (ps_data->als_input_dev == NULL) {
2791 		printk(KERN_ERR "%s: could not allocate als device\n", __func__);
2792 		err = -ENOMEM;
2793 		goto err_als_input_allocate;
2794 		}
2795 	ps_data->ps_input_dev = input_allocate_device();
2796 	if (ps_data->ps_input_dev == NULL) {
2797 		printk(KERN_ERR "%s: could not allocate ps device\n", __func__);
2798 		err = -ENOMEM;
2799 		goto err_ps_input_allocate;
2800 		}
2801 	ps_data->als_input_dev->name = ALS_NAME;
2802 	ps_data->ps_input_dev->name = PS_NAME;
2803 	set_bit(EV_ABS, ps_data->als_input_dev->evbit);
2804 	set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
2805 #if REPORT_KEY
2806 	/*added by guoying for wake up system*/
2807 	set_bit(EV_KEY, ps_data->ps_input_dev->evbit);
2808 	set_bit(EV_REL, ps_data->ps_input_dev->evbit);
2809 	set_bit(KEY_PROX_NEAR, ps_data->ps_input_dev->keybit);
2810 	set_bit(KEY_PROX_FAR, ps_data->ps_input_dev->keybit);
2811 #endif
2812 	input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1 << 16) - 1), 0, 0);
2813 #ifdef NEED_REPORT_MUTIL_LEVEL_DATA
2814 	input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0, 5, 0, 0);
2815 #else
2816 	input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
2817 #endif
2818 	err = input_register_device(ps_data->als_input_dev);
2819 	if (err < 0) {
2820 		printk(KERN_ERR "%s: can not register als input device\n", __func__);
2821 		input_free_device(ps_data->als_input_dev);
2822 		goto err_als_input_register;
2823 		}
2824 	err = input_register_device(ps_data->ps_input_dev);
2825 	if (err < 0) {
2826 		printk(KERN_ERR "%s: can not register ps input device\n", __func__);
2827 		input_free_device(ps_data->ps_input_dev);
2828 		goto err_ps_input_register;
2829 		}
2830 
2831 	err = stk3x1x_sysfs_create_files(&ps_data->als_input_dev->dev.kobj, stk_als_attrs);
2832 	if (err < 0) {
2833 		printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__);
2834 		goto err_als_sysfs_create_group;
2835 	}
2836 	kobject_uevent(&ps_data->als_input_dev->dev.kobj, KOBJ_CHANGE);
2837 	err = stk3x1x_sysfs_create_files(&ps_data->ps_input_dev->dev.kobj, stk_ps_attrs);
2838 	if (err < 0) {
2839 		printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__);
2840 		goto err_ps_sysfs_create_group;
2841 	}
2842 	kobject_uevent(&ps_data->ps_input_dev->dev.kobj, KOBJ_CHANGE);
2843 	input_set_drvdata(ps_data->als_input_dev, ps_data);
2844 	input_set_drvdata(ps_data->ps_input_dev, ps_data);
2845 
2846 #ifdef STK_POLL_ALS
2847 	ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq");
2848 	INIT_WORK(&ps_data->stk_als_work, stk_als_poll_work_func);
2849 	hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2850 	ps_data->als_poll_delay = ns_to_ktime(20 * NSEC_PER_MSEC);
2851 	ps_data->als_timer.function = stk_als_timer_func;
2852 #endif
2853 
2854 #ifdef STK_POLL_PS
2855 	ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq");
2856 	INIT_WORK(&ps_data->stk_ps_work, stk_ps_poll_work_func);
2857 	hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2858 	ps_data->ps_poll_delay = ns_to_ktime(60 * NSEC_PER_MSEC);
2859 	ps_data->ps_timer.function = stk_ps_timer_func;
2860 #endif
2861 
2862 #ifdef STK_TUNE0
2863 	ps_data->stk_ps_tune0_wq = create_singlethread_workqueue("stk_ps_tune0_wq");
2864 	INIT_WORK(&ps_data->stk_ps_tune0_work, stk_ps_tune0_work_func);
2865 	hrtimer_init(&ps_data->ps_tune0_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2866 	ps_data->ps_tune0_delay = ns_to_ktime(60 * NSEC_PER_MSEC);
2867 	ps_data->ps_tune0_timer.function = stk_ps_tune0_timer_func;
2868 #endif
2869 
2870 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
2871 	ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
2872 	INIT_WORK(&ps_data->stk_work, stk_work_func);
2873 
2874 	err = stk3x1x_setup_irq(client);
2875 	if (err < 0)
2876 		goto err_stk3x1x_setup_irq;
2877 #endif
2878 	device_init_wakeup(&client->dev, true);
2879 	err = stk3x1x_init_all_setting(client, plat_data);
2880 	if (err < 0)
2881 		goto err_init_all_setting;
2882 
2883 	sProbeSuccess = 1;
2884 	printk(KERN_INFO "%s: probe successfully\n", __func__);
2885 /*
2886 added by guoying
2887 */
2888 	/*
2889 	*printk(KERN_ERR "%s:pin_cfg->mul_sel = %d\n", __func__, pin_cfg->mul_sel);
2890 	*/
2891 	if (pin_cfg->pull != STK_GPIO_PULL_DEFAULT) {
2892 		config = PIN_CONF_PACKED(STK_PINCFG_TYPE_PUD, pin_cfg->pull);
2893 		pinctrl_gpio_set_config(pin_cfg->gpio, config);
2894 	}
2895 	if (pin_cfg->drv_level != STK_GPIO_DRVLVL_DEFAULT) {
2896 		config = PIN_CONF_PACKED(STK_PINCFG_TYPE_DRV, pin_cfg->drv_level);
2897 		pinctrl_gpio_set_config(pin_cfg->gpio, config);
2898 	}
2899 	if (pin_cfg->data != STK_GPIO_DATA_DEFAULT) {
2900 		config = PIN_CONF_PACKED(STK_PINCFG_TYPE_DAT, pin_cfg->data);
2901 		pinctrl_gpio_set_config(pin_cfg->gpio, config);
2902 	}
2903 /*
2904 ended by guoying
2905 */
2906 	return 0;
2907 
2908 err_init_all_setting:
2909 	input_sensor_free(&(ls_sensor_info.input_type));
2910 	device_init_wakeup(&client->dev, false);
2911 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
2912 err_stk3x1x_setup_irq:
2913 	if (0 != ls_sensor_info.int_number)
2914 		input_free_int(&(ls_sensor_info.input_type), ps_data);
2915 #endif
2916 #ifdef STK_POLL_ALS
2917 	hrtimer_try_to_cancel(&ps_data->als_timer);
2918 	destroy_workqueue(ps_data->stk_als_wq);
2919 #endif
2920 #ifdef STK_TUNE0
2921 	destroy_workqueue(ps_data->stk_ps_tune0_wq);
2922 #endif
2923 #ifdef STK_POLL_PS
2924 	hrtimer_try_to_cancel(&ps_data->ps_timer);
2925 	destroy_workqueue(ps_data->stk_ps_wq);
2926 #endif
2927 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
2928 	destroy_workqueue(ps_data->stk_wq);
2929 #endif
2930 	stk3x1x_sysfs_remove_files(&ps_data->ps_input_dev->dev.kobj, stk_ps_attrs);
2931 err_ps_sysfs_create_group:
2932 	stk3x1x_sysfs_remove_files(&ps_data->als_input_dev->dev.kobj, stk_als_attrs);
2933 err_als_sysfs_create_group:
2934 	input_unregister_device(ps_data->ps_input_dev);
2935 err_ps_input_register:
2936 	input_unregister_device(ps_data->als_input_dev);
2937 err_als_input_register:
2938 err_ps_input_allocate:
2939 err_als_input_allocate:
2940 
2941 	/*support wake lock for ps*/
2942 /*#ifdef STK_POLL_PS
2943 	wake_lock_destroy(&ps_data->ps_nosuspend_wl);
2944 #endif
2945 	wake_lock_destroy(&ps_data->ps_wakelock);	*/
2946     mutex_destroy(&ps_data->io_lock);
2947 	kfree(ps_data);
2948     return err;
2949 }
2950 
2951 
stk3x1x_remove(struct i2c_client * client)2952 static int stk3x1x_remove(struct i2c_client *client)
2953 {
2954 	struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
2955 
2956 	device_init_wakeup(&client->dev, false);
2957 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
2958 	if (0 != ls_sensor_info.int_number)
2959 		input_free_int(&(ls_sensor_info.input_type), ps_data);
2960 #endif
2961 #ifdef STK_POLL_ALS
2962 	hrtimer_try_to_cancel(&ps_data->als_timer);
2963 	destroy_workqueue(ps_data->stk_als_wq);
2964 #endif
2965 #ifdef STK_TUNE0
2966 	destroy_workqueue(ps_data->stk_ps_tune0_wq);
2967 #endif
2968 #ifdef STK_POLL_PS
2969 	hrtimer_try_to_cancel(&ps_data->ps_timer);
2970 	destroy_workqueue(ps_data->stk_ps_wq);
2971 #endif
2972 #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
2973 	destroy_workqueue(ps_data->stk_wq);
2974 #endif
2975 	stk3x1x_sysfs_remove_files(&ps_data->ps_input_dev->dev.kobj, stk_ps_attrs);
2976 	stk3x1x_sysfs_remove_files(&ps_data->als_input_dev->dev.kobj, stk_als_attrs);
2977 	input_unregister_device(ps_data->ps_input_dev);
2978 	input_unregister_device(ps_data->als_input_dev);
2979 
2980 	/*support wake lock for ps*
2981 #ifdef STK_POLL_PS
2982 	wake_lock_destroy(&ps_data->ps_nosuspend_wl);
2983 #endif
2984 	wake_lock_destroy(&ps_data->ps_wakelock);	*/
2985 	mutex_destroy(&ps_data->io_lock);
2986 	kfree(ps_data);
2987 
2988     return 0;
2989 }
2990 
2991 static const struct i2c_device_id stk_ps_id[] = {
2992 		{ "stk3x1x", 0},
2993 		{}
2994 };
2995 
2996 MODULE_DEVICE_TABLE(i2c, stk_ps_id);
2997 
2998 static const struct of_device_id stk3x1x_of_match[] = {
2999 	{.compatible = "allwinner,stk3x1x"},
3000 	{},
3001 };
3002 
3003 #if IS_ENABLED(CONFIG_PM)
3004 static UNIVERSAL_DEV_PM_OPS(stk_pm_ops, stk3x1x_suspend,
3005 		stk3x1x_resume, NULL);
3006 #endif
3007 
3008 static struct i2c_driver stk_ps_driver = {
3009 		.class   = I2C_CLASS_HWMON,
3010 		.driver = {
3011 			.of_match_table = stk3x1x_of_match,
3012 			.name  = DEVICE_NAME,
3013 			.owner = THIS_MODULE,
3014 			#if IS_ENABLED(CONFIG_PM)
3015 			.pm = &stk_pm_ops,
3016 			#endif
3017 		},
3018 		.probe    = stk3x1x_probe,
3019 		.remove = stk3x1x_remove,
3020 		.id_table = stk_ps_id,
3021 		.address_list	= normal_i2c,
3022 };
3023 
startup(void)3024 static int startup(void)
3025 {
3026 	int ret = 0;
3027 	dprintk(DEBUG_INIT, "%s:light sensor driver init\n", __func__);
3028 
3029 	if (input_sensor_startup(&(ls_sensor_info.input_type))) {
3030 		printk("%s: ls_fetch_sysconfig_para err.\n", __func__);
3031 		return -1;
3032 	} else {
3033 		ret = input_sensor_init(&(ls_sensor_info.input_type));
3034 		if (0 != ret) {
3035 			printk("%s:ls_init_platform_resource err. \n", __func__);
3036 		}
3037 	}
3038 	if (ls_sensor_info.sensor_used == 0) {
3039 		printk("*** ls_used set to 0 !\n");
3040 		printk("*** if use light_sensor,please put the sys_config.fex ls_used set to 1. \n");
3041 		return -1;
3042 	}
3043 	return 0;
3044 }
3045 
stk3x1x_init(void)3046 static int __init stk3x1x_init(void)
3047 {
3048 	if (startup() != 0)
3049 		return -1;
3050 	if (!ls_sensor_info.isI2CClient)
3051 		stk_ps_driver.detect = stk_detect;
3052 
3053 	i2c_add_driver(&stk_ps_driver);
3054 
3055 	return sProbeSuccess ? 0 : -ENODEV;
3056 }
3057 
stk3x1x_exit(void)3058 static void __exit stk3x1x_exit(void)
3059 {
3060 		printk("%s  exit !!\n", __func__);
3061 		i2c_del_driver(&stk_ps_driver);
3062 		input_sensor_free(&(ls_sensor_info.input_type));
3063 }
3064 
3065 late_initcall(stk3x1x_init);
3066 module_exit(stk3x1x_exit);
3067 module_param_named(debug_mask, debug_mask, int, 0644);
3068 MODULE_AUTHOR("allwinner");
3069 MODULE_DESCRIPTION("Sensortek stk3x1x Proximity Sensor driver");
3070 MODULE_LICENSE("GPL");
3071 MODULE_VERSION(DRIVER_VERSION);
3072 MODULE_VERSION("1.0.2");
3073