blob: eee9a28048feca795ce2f7d347c338dd38351d95 [file] [log] [blame]
zzzb7673aa2013-07-24 02:55:43 +08001/*
2 * stk3x1x.c - Linux kernel modules for sensortek stk301x, stk321x and stk331x
3 * proximity/ambient light sensor
4 *
5 * Copyright (C) 2012 Lex Hsieh / sensortek <lex_hsieh@sitronix.com.tw> or
6 * <lex_hsieh@sensortek.com.tw>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/mutex.h>
28#include <linux/kdev_t.h>
29#include <linux/fs.h>
30#include <linux/input.h>
31#include <linux/workqueue.h>
32#include <linux/irq.h>
33#include <linux/delay.h>
34#include <linux/sched.h>
35#include <linux/kthread.h>
36#include <linux/errno.h>
37#include <linux/wakelock.h>
38#include <linux/interrupt.h>
39#include <linux/gpio.h>
40#include <linux/fs.h>
41#include <asm/uaccess.h>
42#ifdef CONFIG_HAS_EARLYSUSPEND
43#include <linux/earlysuspend.h>
44#endif
45#include "linux/stk3x1x.h"
46
47#define DRIVER_VERSION "3.4.4ts"
48
49/* Driver Settings */
50#define CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
51#ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
52#define STK_ALS_CHANGE_THD 20 /* The threshold to trigger ALS interrupt, unit: lux */
53#endif /* #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD */
54#define STK_INT_PS_MODE 1 /* 1, 2, or 3 */
55#define STK_POLL_PS
56#define STK_POLL_ALS /* ALS interrupt is valid only when STK_PS_INT_MODE = 1 or 4*/
57
58#define STK_DEBUG_PRINTF
59
60/* Define Register Map */
61#define STK_STATE_REG 0x00
62#define STK_PSCTRL_REG 0x01
63#define STK_ALSCTRL_REG 0x02
64#define STK_LEDCTRL_REG 0x03
65#define STK_INT_REG 0x04
66#define STK_WAIT_REG 0x05
67#define STK_THDH1_PS_REG 0x06
68#define STK_THDH2_PS_REG 0x07
69#define STK_THDL1_PS_REG 0x08
70#define STK_THDL2_PS_REG 0x09
71#define STK_THDH1_ALS_REG 0x0A
72#define STK_THDH2_ALS_REG 0x0B
73#define STK_THDL1_ALS_REG 0x0C
74#define STK_THDL2_ALS_REG 0x0D
75#define STK_FLAG_REG 0x10
76#define STK_DATA1_PS_REG 0x11
77#define STK_DATA2_PS_REG 0x12
78#define STK_DATA1_ALS_REG 0x13
79#define STK_DATA2_ALS_REG 0x14
80#define STK_DATA1_OFFSET_REG 0x15
81#define STK_DATA2_OFFSET_REG 0x16
82#define STK_DATA1_IR_REG 0x17
83#define STK_DATA2_IR_REG 0x18
84#define STK_PDT_ID_REG 0x3E
85#define STK_RSRVD_REG 0x3F
86#define STK_SW_RESET_REG 0x80
87
88
89/* Define state reg */
90#define STK_STATE_EN_IRS_SHIFT 7
91#define STK_STATE_EN_AK_SHIFT 6
92#define STK_STATE_EN_ASO_SHIFT 5
93#define STK_STATE_EN_IRO_SHIFT 4
94#define STK_STATE_EN_WAIT_SHIFT 2
95#define STK_STATE_EN_ALS_SHIFT 1
96#define STK_STATE_EN_PS_SHIFT 0
97
98#define STK_STATE_EN_IRS_MASK 0x80
99#define STK_STATE_EN_AK_MASK 0x40
100#define STK_STATE_EN_ASO_MASK 0x20
101#define STK_STATE_EN_IRO_MASK 0x10
102#define STK_STATE_EN_WAIT_MASK 0x04
103#define STK_STATE_EN_ALS_MASK 0x02
104#define STK_STATE_EN_PS_MASK 0x01
105
106/* Define PS ctrl reg */
107#define STK_PS_PRS_SHIFT 6
108#define STK_PS_GAIN_SHIFT 4
109#define STK_PS_IT_SHIFT 0
110
111#define STK_PS_PRS_MASK 0xC0
112#define STK_PS_GAIN_MASK 0x30
113#define STK_PS_IT_MASK 0x0F
114
115/* Define ALS ctrl reg */
116#define STK_ALS_PRS_SHIFT 6
117#define STK_ALS_GAIN_SHIFT 4
118#define STK_ALS_IT_SHIFT 0
119
120#define STK_ALS_PRS_MASK 0xC0
121#define STK_ALS_GAIN_MASK 0x30
122#define STK_ALS_IT_MASK 0x0F
123
124/* Define LED ctrl reg */
125#define STK_LED_IRDR_SHIFT 6
126#define STK_LED_DT_SHIFT 0
127
128#define STK_LED_IRDR_MASK 0xC0
129#define STK_LED_DT_MASK 0x3F
130
131/* Define interrupt reg */
132#define STK_INT_CTRL_SHIFT 7
133#define STK_INT_OUI_SHIFT 4
134#define STK_INT_ALS_SHIFT 3
135#define STK_INT_PS_SHIFT 0
136
137#define STK_INT_CTRL_MASK 0x80
138#define STK_INT_OUI_MASK 0x10
139#define STK_INT_ALS_MASK 0x08
140#define STK_INT_PS_MASK 0x07
141
142#define STK_INT_ALS 0x08
143
144/* Define flag reg */
145#define STK_FLG_ALSDR_SHIFT 7
146#define STK_FLG_PSDR_SHIFT 6
147#define STK_FLG_ALSINT_SHIFT 5
148#define STK_FLG_PSINT_SHIFT 4
149#define STK_FLG_OUI_SHIFT 2
150#define STK_FLG_IR_RDY_SHIFT 1
151#define STK_FLG_NF_SHIFT 0
152
153#define STK_FLG_ALSDR_MASK 0x80
154#define STK_FLG_PSDR_MASK 0x40
155#define STK_FLG_ALSINT_MASK 0x20
156#define STK_FLG_PSINT_MASK 0x10
157#define STK_FLG_OUI_MASK 0x04
158#define STK_FLG_IR_RDY_MASK 0x02
159#define STK_FLG_NF_MASK 0x01
160
161/* misc define */
162#define MIN_ALS_POLL_DELAY_NS 110000000
163
164#define DEVICE_NAME "stk_ps"
165#define ALS_NAME "lightsensor-level"
166#define PS_NAME "proximity"
167
168struct stk3x1x_data {
169 struct i2c_client *client;
170#if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
171 int32_t irq;
172 struct work_struct stk_work;
173 struct workqueue_struct *stk_wq;
174#endif
175 int int_pin;
176 uint8_t wait_reg;
177#ifdef CONFIG_HAS_EARLYSUSPEND
178 struct early_suspend stk_early_suspend;
179#endif
180 uint16_t ps_thd_h;
181 uint16_t ps_thd_l;
182 struct mutex io_lock;
183 struct input_dev *ps_input_dev;
184 int32_t ps_distance_last;
185 bool ps_enabled;
186 struct wake_lock ps_wakelock;
187 struct work_struct stk_ps_work;
188 struct workqueue_struct *stk_ps_wq;
189#ifdef STK_POLL_PS
190 struct wake_lock ps_nosuspend_wl;
191#endif
192 struct input_dev *als_input_dev;
193 int32_t als_lux_last;
194 uint32_t als_transmittance;
195 bool als_enabled;
196 struct hrtimer als_timer;
197 struct hrtimer ps_timer;
198 ktime_t als_poll_delay;
199 ktime_t ps_poll_delay;
200#ifdef STK_POLL_ALS
201 struct work_struct stk_als_work;
202 struct workqueue_struct *stk_als_wq;
203#endif
204};
205
206#if( !defined(CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD))
207static uint32_t lux_threshold_table[] =
208{
209 3,
210 10,
211 40,
212 65,
213 145,
214 300,
215 550,
216 930,
217 1250,
218 1700,
219};
220
221#define LUX_THD_TABLE_SIZE (sizeof(lux_threshold_table)/sizeof(uint32_t)+1)
222static uint16_t code_threshold_table[LUX_THD_TABLE_SIZE+1];
223#endif
224
225static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable);
226static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable);
227static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
228static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
229static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
230static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
231//static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset);
232
233inline uint32_t stk_alscode2lux(struct stk3x1x_data *ps_data, uint32_t alscode)
234{
235 alscode += ((alscode<<7)+(alscode<<3)+(alscode>>1));
236 alscode<<=3;
237 alscode/=ps_data->als_transmittance;
238 return alscode;
239}
240
241inline uint32_t stk_lux2alscode(struct stk3x1x_data *ps_data, uint32_t lux)
242{
243 lux*=ps_data->als_transmittance;
244 lux/=1100;
245 if (unlikely(lux>=(1<<16)))
246 lux = (1<<16) -1;
247 return lux;
248}
249
250#ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
251static void stk_init_code_threshold_table(struct stk3x1x_data *ps_data)
252{
253 uint32_t i,j;
254 uint32_t alscode;
255
256 code_threshold_table[0] = 0;
257#ifdef STK_DEBUG_PRINTF
258 printk(KERN_INFO "alscode[0]=%d\n",0);
259#endif
260 for (i=1,j=0;i<LUX_THD_TABLE_SIZE;i++,j++)
261 {
262 alscode = stk_lux2alscode(ps_data, lux_threshold_table[j]);
263 printk(KERN_INFO "alscode[%d]=%d\n",i,alscode);
264 code_threshold_table[i] = (uint16_t)(alscode);
265 }
266 code_threshold_table[i] = 0xffff;
267 printk(KERN_INFO "alscode[%d]=%d\n",i,alscode);
268}
269
270static uint32_t stk_get_lux_interval_index(uint16_t alscode)
271{
272 uint32_t i;
273 for (i=1;i<=LUX_THD_TABLE_SIZE;i++)
274 {
275 if ((alscode>=code_threshold_table[i-1])&&(alscode<code_threshold_table[i]))
276 {
277 return i;
278 }
279 }
280 return LUX_THD_TABLE_SIZE;
281}
282#else
283inline void stk_als_set_new_thd(struct stk3x1x_data *ps_data, uint16_t alscode)
284{
285 int32_t high_thd,low_thd;
286 high_thd = alscode + stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
287 low_thd = alscode - stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
288 if (high_thd >= (1<<16))
289 high_thd = (1<<16) -1;
290 if (low_thd <0)
291 low_thd = 0;
292 stk3x1x_set_als_thd_h(ps_data, (uint16_t)high_thd);
293 stk3x1x_set_als_thd_l(ps_data, (uint16_t)low_thd);
294}
295#endif // CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
296
297
298static int32_t stk3x1x_init_all_reg(struct stk3x1x_data *ps_data, struct stk3x1x_platform_data *plat_data)
299{
300 int32_t ret;
301 uint8_t w_reg;
302
303 w_reg = plat_data->state_reg;
304 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
305 if (ret < 0)
306 {
307 printk(KERN_ERR "%s: write i2c error\n", __func__);
308 return ret;
309 }
310
311 ps_data->ps_thd_h = plat_data->ps_thd_h;
312 ps_data->ps_thd_l = plat_data->ps_thd_l;
313
314 w_reg = plat_data->psctrl_reg;
315 ret = i2c_smbus_write_byte_data(ps_data->client, STK_PSCTRL_REG, w_reg);
316 if (ret < 0)
317 {
318 printk(KERN_ERR "%s: write i2c error\n", __func__);
319 return ret;
320 }
321 w_reg = plat_data->alsctrl_reg;
322 ret = i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, w_reg);
323 if (ret < 0)
324 {
325 printk(KERN_ERR "%s: write i2c error\n", __func__);
326 return ret;
327 }
328 w_reg = plat_data->ledctrl_reg;
329 ret = i2c_smbus_write_byte_data(ps_data->client, STK_LEDCTRL_REG, w_reg);
330 if (ret < 0)
331 {
332 printk(KERN_ERR "%s: write i2c error\n", __func__);
333 return ret;
334 }
335 ps_data->wait_reg = plat_data->wait_reg;
336
337 if(ps_data->wait_reg < 2)
338 {
339 printk(KERN_WARNING "%s: wait_reg should be larger than 2, force to write 2\n", __func__);
340 ps_data->wait_reg = 2;
341 }
342 else if (ps_data->wait_reg > 0xFF)
343 {
344 printk(KERN_WARNING "%s: wait_reg should be less than 0xFF, force to write 0xFF\n", __func__);
345 ps_data->wait_reg = 0xFF;
346 }
347 w_reg = plat_data->wait_reg;
348 ret = i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, w_reg);
349 if (ret < 0)
350 {
351 printk(KERN_ERR "%s: write i2c error\n", __func__);
352 return ret;
353 }
354 stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
355 stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
356
357 w_reg = 0;
358#ifndef STK_POLL_PS
359 w_reg |= STK_INT_PS_MODE;
360#else
361 w_reg |= 0x01;
362#endif
363
364#if (!defined(STK_POLL_ALS) && (STK_INT_PS_MODE != 0x02) && (STK_INT_PS_MODE != 0x03))
365 w_reg |= STK_INT_ALS;
366#endif
367 ret = i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, w_reg);
368 if (ret < 0)
369 {
370 printk(KERN_ERR "%s: write i2c error\n", __func__);
371 return ret;
372 }
373 return 0;
374}
375
376static int32_t stk3x1x_check_pid(struct stk3x1x_data *ps_data)
377{
378 int32_t err1, err2;
379
380 err1 = i2c_smbus_read_byte_data(ps_data->client,STK_PDT_ID_REG);
381 if (err1 < 0)
382 {
383 printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err1);
384 return err1;
385 }
386
387 err2 = i2c_smbus_read_byte_data(ps_data->client,STK_RSRVD_REG);
388 if (err2 < 0)
389 {
390 printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err2);
391 return -1;
392 }
393 printk(KERN_INFO "%s: PID=0x%x, RID=0x%x\n", __func__, err1, err2);
394 if(err2 == 0xC0)
395 printk(KERN_INFO "%s: RID=0xC0!!!!!!!!!!!!!\n", __func__);
396
397 return 0;
398}
399
400
401static int32_t stk3x1x_software_reset(struct stk3x1x_data *ps_data)
402{
403 int32_t r;
404 uint8_t w_reg;
405
406 w_reg = 0x7F;
407 r = i2c_smbus_write_byte_data(ps_data->client,STK_WAIT_REG,w_reg);
408 if (r<0)
409 {
410 printk(KERN_ERR "%s: software reset: write i2c error, ret=%d\n", __func__, r);
411 return r;
412 }
413 r = i2c_smbus_read_byte_data(ps_data->client,STK_WAIT_REG);
414 if (w_reg != r)
415 {
416 printk(KERN_ERR "%s: software reset: read-back value is not the same\n", __func__);
417 return -1;
418 }
419
420 r = i2c_smbus_write_byte_data(ps_data->client,STK_SW_RESET_REG,0);
421 if (r<0)
422 {
423 printk(KERN_ERR "%s: software reset: read error after reset\n", __func__);
424 return r;
425 }
426 msleep(1);
427 return 0;
428}
429
430
431static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
432{
433 uint8_t temp;
434 uint8_t* pSrc = (uint8_t*)&thd_l;
435 temp = *pSrc;
436 *pSrc = *(pSrc+1);
437 *(pSrc+1) = temp;
438 return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_ALS_REG,thd_l);
439}
440static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
441{
442 uint8_t temp;
443 uint8_t* pSrc = (uint8_t*)&thd_h;
444 temp = *pSrc;
445 *pSrc = *(pSrc+1);
446 *(pSrc+1) = temp;
447 return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_ALS_REG,thd_h);
448}
449
450static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
451{
452 uint8_t temp;
453 uint8_t* pSrc = (uint8_t*)&thd_l;
454
455 temp = *pSrc;
456 *pSrc = *(pSrc+1);
457 *(pSrc+1) = temp;
458 ps_data->ps_thd_l = thd_l;
459 return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_PS_REG,thd_l);
460}
461
462static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
463{
464 uint8_t temp;
465 uint8_t* pSrc = (uint8_t*)&thd_h;
466
467 temp = *pSrc;
468 *pSrc = *(pSrc+1);
469 *(pSrc+1) = temp;
470 ps_data->ps_thd_h = thd_h;
471 return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_PS_REG,thd_h);
472}
473
474/*
475static int32_t stk3x1x_set_ps_foffset(struct stk3x1x_data *ps_data, uint16_t offset)
476{
477 uint8_t temp;
478 uint8_t* pSrc = (uint8_t*)&offset;
479 temp = *pSrc;
480 *pSrc = *(pSrc+1);
481 *(pSrc+1) = temp;
482 return i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
483}
484
485static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset)
486{
487 uint8_t temp;
488 uint8_t* pSrc = (uint8_t*)&offset;
489 int ret;
490 uint8_t w_state_reg;
491 uint8_t re_en;
492
493 ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
494 if (ret < 0)
495 {
496 printk(KERN_ERR "%s: write i2c error\n", __func__);
497 return ret;
498 }
499 re_en = (ret & STK_STATE_EN_AK_MASK) ? 1: 0;
500 if(re_en)
501 {
502 w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_AK_MASK));
503 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
504 if (ret < 0)
505 {
506 printk(KERN_ERR "%s: write i2c error\n", __func__);
507 return ret;
508 }
509 msleep(1);
510 }
511 temp = *pSrc;
512 *pSrc = *(pSrc+1);
513 *(pSrc+1) = temp;
514 ret = i2c_smbus_write_word_data(ps_data->client,0x0E,offset);
515 if(!re_en)
516 return ret;
517
518 w_state_reg |= STK_STATE_EN_AK_MASK;
519 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
520 if (ret < 0)
521 {
522 printk(KERN_ERR "%s: write i2c error\n", __func__);
523 return ret;
524 }
525
526 return 0;
527}
528*/
529
530static inline uint32_t stk3x1x_get_ps_reading(struct stk3x1x_data *ps_data)
531{
532 int32_t word_data, tmp_word_data;
533
534 tmp_word_data = i2c_smbus_read_word_data(ps_data->client,STK_DATA1_PS_REG);
535 if(tmp_word_data < 0)
536 {
537 printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
538 return tmp_word_data;
539 }
540 word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
541 return word_data;
542}
543
544static int32_t stk3x1x_set_flag(struct stk3x1x_data *ps_data, uint8_t org_flag_reg, uint8_t clr)
545{
546 uint8_t w_flag;
547 w_flag = org_flag_reg | (STK_FLG_ALSINT_MASK | STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK | STK_FLG_IR_RDY_MASK);
548 w_flag &= (~clr);
549 //printk(KERN_INFO "%s: org_flag_reg=0x%x, w_flag = 0x%x\n", __func__, org_flag_reg, w_flag);
550 return i2c_smbus_write_byte_data(ps_data->client,STK_FLAG_REG, w_flag);
551}
552
553static int32_t stk3x1x_get_flag(struct stk3x1x_data *ps_data)
554{
555 return i2c_smbus_read_byte_data(ps_data->client,STK_FLAG_REG);
556}
557
558static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable)
559{
560 int32_t ret;
561 uint8_t w_state_reg;
562 uint8_t curr_ps_enable;
563 curr_ps_enable = ps_data->ps_enabled?1:0;
564 if(curr_ps_enable == enable)
565 return 0;
566
567 ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
568 if (ret < 0)
569 {
570 printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
571 return ret;
572 }
573 w_state_reg = ret;
574 w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | 0x60);
575 if(enable)
576 {
577 w_state_reg |= STK_STATE_EN_PS_MASK;
578 if(!(ps_data->als_enabled))
579 w_state_reg |= STK_STATE_EN_WAIT_MASK;
580 }
581 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
582 if (ret < 0)
583 {
584 printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
585 return ret;
586 }
587
588 if(enable)
589 {
590#ifdef STK_POLL_PS
591 hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL);
592 ps_data->ps_distance_last = -1;
593#endif
594 ps_data->ps_enabled = true;
595#ifndef STK_POLL_PS
596#ifndef STK_POLL_ALS
597 if(!(ps_data->als_enabled))
598#endif /* #ifndef STK_POLL_ALS */
599 enable_irq(ps_data->irq);
600 msleep(1);
601 ret = stk3x1x_get_flag(ps_data);
602 if (ret < 0)
603 {
604 printk(KERN_ERR "%s: read i2c error, ret=%d\n", __func__, ret);
605 return ret;
606 }
607
608 near_far_state = ret & STK_FLG_NF_MASK;
609 ps_data->ps_distance_last = near_far_state;
610 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
611 input_sync(ps_data->ps_input_dev);
612 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
613 reading = stk3x1x_get_ps_reading(ps_data);
614 printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n",__func__, near_far_state, reading);
615#endif /* #ifndef STK_POLL_PS */
616 }
617 else
618 {
619#ifdef STK_POLL_PS
620 hrtimer_cancel(&ps_data->ps_timer);
621#else
622#ifndef STK_POLL_ALS
623 if(!(ps_data->als_enabled))
624#endif
625 disable_irq(ps_data->irq);
626#endif
627 ps_data->ps_enabled = false;
628 }
629 return ret;
630}
631
632static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable)
633{
634 int32_t ret;
635 uint8_t w_state_reg;
636 uint8_t curr_als_enable = (ps_data->als_enabled)?1:0;
637
638 if(curr_als_enable == enable)
639 return 0;
640
641#ifndef STK_POLL_ALS
642 if (enable)
643 {
644 stk3x1x_set_als_thd_h(ps_data, 0x0000);
645 stk3x1x_set_als_thd_l(ps_data, 0xFFFF);
646 }
647#endif
648 ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
649 if (ret < 0)
650 {
651 printk(KERN_ERR "%s: write i2c error\n", __func__);
652 return ret;
653 }
654 w_state_reg = (uint8_t)(ret & (~(STK_STATE_EN_ALS_MASK | STK_STATE_EN_WAIT_MASK)));
655 if(enable)
656 w_state_reg |= STK_STATE_EN_ALS_MASK;
657 else if (ps_data->ps_enabled)
658 w_state_reg |= STK_STATE_EN_WAIT_MASK;
659
660 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
661 if (ret < 0)
662 {
663 printk(KERN_ERR "%s: write i2c error\n", __func__);
664 return ret;
665 }
666
667 if (enable)
668 {
669 ps_data->als_enabled = true;
670#ifdef STK_POLL_ALS
671 hrtimer_start(&ps_data->als_timer, ps_data->als_poll_delay, HRTIMER_MODE_REL);
672#else
673#ifndef STK_POLL_PS
674 if(!(ps_data->ps_enabled))
675#endif
676 enable_irq(ps_data->irq);
677#endif
678 }
679 else
680 {
681 ps_data->als_enabled = false;
682#ifdef STK_POLL_ALS
683 hrtimer_cancel(&ps_data->als_timer);
684#else
685#ifndef STK_POLL_PS
686 if(!(ps_data->ps_enabled))
687#endif
688 disable_irq(ps_data->irq);
689#endif
690 }
691 return ret;
692}
693
694static inline int32_t stk3x1x_get_als_reading(struct stk3x1x_data *ps_data)
695{
696 int32_t word_data, tmp_word_data;
697 tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_ALS_REG);
698 if(tmp_word_data < 0)
699 {
700 printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
701 return tmp_word_data;
702 }
703 word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
704 return word_data;
705}
706
707static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data)
708{
709 int32_t word_data, tmp_word_data;
710 int32_t ret;
711 uint8_t w_reg, retry = 0;
712
713 if(ps_data->ps_enabled)
714 {
715 stk3x1x_enable_ps(ps_data, 0);
716 ps_data->ps_enabled = true;
717 }
718 ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
719 if (ret < 0)
720 {
721 printk(KERN_ERR "%s: write i2c error\n", __func__);
722 return ret;
723 }
724 w_reg = (uint8_t)(ret & (~STK_STATE_EN_IRS_MASK));
725 w_reg |= STK_STATE_EN_IRS_MASK;
726
727 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
728 if (ret < 0)
729 {
730 printk(KERN_ERR "%s: write i2c error\n", __func__);
731 return ret;
732 }
733 msleep(100);
734
735 do
736 {
737 msleep(50);
738 ret = stk3x1x_get_flag(ps_data);
739 if (ret < 0)
740 {
741 printk(KERN_ERR "%s: write i2c error\n", __func__);
742 return ret;
743 }
744 retry++;
745 }while(retry < 5 && ((ret&STK_FLG_IR_RDY_MASK) == 0));
746
747 if(retry == 5)
748 {
749 printk(KERN_ERR "%s: ir data is not ready for 300ms\n", __func__);
750 return -EINVAL;
751 }
752
753 ret = stk3x1x_get_flag(ps_data);
754 if (ret < 0)
755 {
756 printk(KERN_ERR "%s: write i2c error\n", __func__);
757 return ret;
758 }
759
760 ret = stk3x1x_set_flag(ps_data, ret, STK_FLG_IR_RDY_MASK);
761 if (ret < 0)
762 {
763 printk(KERN_ERR "%s: write i2c error\n", __func__);
764 return ret;
765 }
766
767 tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_IR_REG);
768 if(tmp_word_data < 0)
769 {
770 printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
771 return tmp_word_data;
772 }
773 word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
774
775 if(ps_data->ps_enabled)
776 stk3x1x_enable_ps(ps_data, 1);
777 return word_data;
778}
779
780
781static ssize_t stk_als_code_show(struct device *dev, struct device_attribute *attr, char *buf)
782{
783 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
784 int32_t reading;
785
786 reading = stk3x1x_get_als_reading(ps_data);
787 return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
788}
789
790
791static ssize_t stk_als_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
792{
793 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
794 int32_t enable, ret;
795
796 mutex_lock(&ps_data->io_lock);
797 enable = (ps_data->als_enabled)?1:0;
798 mutex_unlock(&ps_data->io_lock);
799 ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
800 ret = (ret & STK_STATE_EN_ALS_MASK)?1:0;
801
802 if(enable != ret)
803 printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
804
805 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
806}
807
808static ssize_t stk_als_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
809{
810 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
811 uint8_t en;
812 if (sysfs_streq(buf, "1"))
813 en = 1;
814 else if (sysfs_streq(buf, "0"))
815 en = 0;
816 else
817 {
818 printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
819 return -EINVAL;
820 }
821 printk(KERN_INFO "%s: Enable ALS : %d\n", __func__, en);
822 mutex_lock(&ps_data->io_lock);
823 stk3x1x_enable_als(ps_data, en);
824 mutex_unlock(&ps_data->io_lock);
825 return size;
826}
827
828static ssize_t stk_als_lux_show(struct device *dev, struct device_attribute *attr, char *buf)
829{
830 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
831 int32_t als_reading;
832 uint32_t als_lux;
833 als_reading = stk3x1x_get_als_reading(ps_data);
834 mutex_lock(&ps_data->io_lock);
835 als_lux = stk_alscode2lux(ps_data, als_reading);
836 mutex_unlock(&ps_data->io_lock);
837 return scnprintf(buf, PAGE_SIZE, "%d lux\n", als_lux);
838}
839
840static ssize_t stk_als_lux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
841{
842 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
843 unsigned long value = 0;
844 int ret;
845 ret = strict_strtoul(buf, 16, &value);
846 if(ret < 0)
847 {
848 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
849 return ret;
850 }
851 mutex_lock(&ps_data->io_lock);
852 ps_data->als_lux_last = value;
853 input_report_abs(ps_data->als_input_dev, ABS_MISC, value);
854 input_sync(ps_data->als_input_dev);
855 mutex_unlock(&ps_data->io_lock);
856 printk(KERN_INFO "%s: als input event %ld lux\n",__func__, value);
857
858 return size;
859}
860
861
862static ssize_t stk_als_transmittance_show(struct device *dev, struct device_attribute *attr, char *buf)
863{
864 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
865 int32_t transmittance;
866 mutex_lock(&ps_data->io_lock);
867 transmittance = ps_data->als_transmittance;
868 mutex_unlock(&ps_data->io_lock);
869 return scnprintf(buf, PAGE_SIZE, "%d\n", transmittance);
870}
871
872
873static ssize_t stk_als_transmittance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
874{
875 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
876 unsigned long value = 0;
877 int ret;
878 ret = strict_strtoul(buf, 10, &value);
879 if(ret < 0)
880 {
881 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
882 return ret;
883 }
884 mutex_lock(&ps_data->io_lock);
885 ps_data->als_transmittance = value;
886 mutex_unlock(&ps_data->io_lock);
887 return size;
888}
889
890static ssize_t stk_als_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
891{
892 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
893 return scnprintf(buf, PAGE_SIZE, "%lld\n", ktime_to_ns(ps_data->als_poll_delay));
894}
895
896
897static ssize_t stk_als_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
898{
899 uint64_t value = 0;
900 int ret;
901 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
902 ret = strict_strtoull(buf, 10, &value);
903 if(ret < 0)
904 {
905 printk(KERN_ERR "%s:strict_strtoull failed, ret=0x%x\n", __func__, ret);
906 return ret;
907 }
908#ifdef STK_DEBUG_PRINTF
909 printk(KERN_INFO "%s: set als poll delay=%lld\n", __func__, value);
910#endif
911 if(value < MIN_ALS_POLL_DELAY_NS)
912 {
913 printk(KERN_ERR "%s: delay is too small\n", __func__);
914 value = MIN_ALS_POLL_DELAY_NS;
915 }
916 mutex_lock(&ps_data->io_lock);
917 if(value != ktime_to_ns(ps_data->als_poll_delay))
918 ps_data->als_poll_delay = ns_to_ktime(value);
919 mutex_unlock(&ps_data->io_lock);
920 return size;
921}
922
923static ssize_t stk_als_ir_code_show(struct device *dev, struct device_attribute *attr, char *buf)
924{
925 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
926 int32_t reading;
927 reading = stk3x1x_get_ir_reading(ps_data);
928 return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
929}
930
931static ssize_t stk_ps_code_show(struct device *dev, struct device_attribute *attr, char *buf)
932{
933 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
934 uint32_t reading;
935 reading = stk3x1x_get_ps_reading(ps_data);
936 return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
937}
938
939static ssize_t stk_ps_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
940{
941 int32_t enable, ret;
942 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
943
944 mutex_lock(&ps_data->io_lock);
945 enable = (ps_data->ps_enabled)?1:0;
946 mutex_unlock(&ps_data->io_lock);
947 ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
948 ret = (ret & STK_STATE_EN_PS_MASK)?1:0;
949
950 if(enable != ret)
951 printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
952
953 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
954}
955
956static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
957{
958 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
959 uint8_t en;
960 if (sysfs_streq(buf, "1"))
961 en = 1;
962 else if (sysfs_streq(buf, "0"))
963 en = 0;
964 else
965 {
966 printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
967 return -EINVAL;
968 }
969 printk(KERN_INFO "%s: Enable PS : %d\n", __func__, en);
970 mutex_lock(&ps_data->io_lock);
971 stk3x1x_enable_ps(ps_data, en);
972 mutex_unlock(&ps_data->io_lock);
973 return size;
974}
975
976static ssize_t stk_ps_enable_aso_show(struct device *dev, struct device_attribute *attr, char *buf)
977{
978 int32_t ret;
979 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
980
981 ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
982 ret = (ret & STK_STATE_EN_ASO_MASK)?1:0;
983
984 return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
985}
986
987static ssize_t stk_ps_enable_aso_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
988{
989 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
990 uint8_t en;
991 int32_t ret;
992 uint8_t w_state_reg;
993
994 if (sysfs_streq(buf, "1"))
995 en = 1;
996 else if (sysfs_streq(buf, "0"))
997 en = 0;
998 else
999 {
1000 printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
1001 return -EINVAL;
1002 }
1003 printk(KERN_INFO "%s: Enable PS ASO : %d\n", __func__, en);
1004
1005 ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
1006 if (ret < 0)
1007 {
1008 printk(KERN_ERR "%s: write i2c error\n", __func__);
1009 return ret;
1010 }
1011 w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_ASO_MASK));
1012 if(en)
1013 w_state_reg |= STK_STATE_EN_ASO_MASK;
1014
1015 ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
1016 if (ret < 0)
1017 {
1018 printk(KERN_ERR "%s: write i2c error\n", __func__);
1019 return ret;
1020 }
1021
1022 return size;
1023}
1024
1025
1026static ssize_t stk_ps_offset_show(struct device *dev, struct device_attribute *attr, char *buf)
1027{
1028 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1029 int32_t word_data, tmp_word_data;
1030
1031 tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_OFFSET_REG);
1032 if(tmp_word_data < 0)
1033 {
1034 printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
1035 return tmp_word_data;
1036 }
1037 word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
1038 return scnprintf(buf, PAGE_SIZE, "%d\n", word_data);
1039}
1040
1041static ssize_t stk_ps_offset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1042{
1043 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1044 unsigned long value = 0;
1045 int ret;
1046 uint16_t offset;
1047
1048 ret = strict_strtoul(buf, 10, &value);
1049 if(ret < 0)
1050 {
1051 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1052 return ret;
1053 }
1054 if(value > 65535)
1055 {
1056 printk(KERN_ERR "%s: invalid value, offset=%ld\n", __func__, value);
1057 return -EINVAL;
1058 }
1059
1060 offset = (uint16_t) ((value&0x00FF) << 8) | ((value&0xFF00) >>8);
1061 ret = i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
1062 if(ret < 0)
1063 {
1064 printk(KERN_ERR "%s: write i2c error\n", __func__);
1065 return ret;
1066 }
1067 return size;
1068}
1069
1070
1071static ssize_t stk_ps_distance_show(struct device *dev, struct device_attribute *attr, char *buf)
1072{
1073 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1074 int32_t dist=1, ret;
1075
1076 mutex_lock(&ps_data->io_lock);
1077 ret = stk3x1x_get_flag(ps_data);
1078 if(ret < 0)
1079 {
1080 printk(KERN_ERR "%s: stk3x1x_get_flag failed, ret=0x%x\n", __func__, ret);
1081 return ret;
1082 }
1083 dist = (ret & STK_FLG_NF_MASK)?1:0;
1084
1085 ps_data->ps_distance_last = dist;
1086 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, dist);
1087 input_sync(ps_data->ps_input_dev);
1088 mutex_unlock(&ps_data->io_lock);
1089 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
1090 printk(KERN_INFO "%s: ps input event %d cm\n",__func__, dist);
1091 return scnprintf(buf, PAGE_SIZE, "%d\n", dist);
1092}
1093
1094
1095static ssize_t stk_ps_distance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1096{
1097 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1098 unsigned long value = 0;
1099 int ret;
1100 ret = strict_strtoul(buf, 10, &value);
1101 if(ret < 0)
1102 {
1103 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1104 return ret;
1105 }
1106 mutex_lock(&ps_data->io_lock);
1107 ps_data->ps_distance_last = value;
1108 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, value);
1109 input_sync(ps_data->ps_input_dev);
1110 mutex_unlock(&ps_data->io_lock);
1111 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
1112 printk(KERN_INFO "%s: ps input event %ld cm\n",__func__, value);
1113 return size;
1114}
1115
1116
1117static ssize_t stk_ps_code_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
1118{
1119 int32_t ps_thd_l1_reg, ps_thd_l2_reg;
1120 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1121 mutex_lock(&ps_data->io_lock);
1122 ps_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_PS_REG);
1123 if(ps_thd_l1_reg < 0)
1124 {
1125 printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l1_reg);
1126 return -EINVAL;
1127 }
1128 ps_thd_l2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_PS_REG);
1129 if(ps_thd_l2_reg < 0)
1130 {
1131 printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l2_reg);
1132 return -EINVAL;
1133 }
1134 mutex_unlock(&ps_data->io_lock);
1135 ps_thd_l1_reg = ps_thd_l1_reg<<8 | ps_thd_l2_reg;
1136 return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg);
1137}
1138
1139
1140static ssize_t stk_ps_code_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1141{
1142 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1143 unsigned long value = 0;
1144 int ret;
1145 ret = strict_strtoul(buf, 10, &value);
1146 if(ret < 0)
1147 {
1148 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1149 return ret;
1150 }
1151 mutex_lock(&ps_data->io_lock);
1152 stk3x1x_set_ps_thd_l(ps_data, value);
1153 mutex_unlock(&ps_data->io_lock);
1154 return size;
1155}
1156
1157static ssize_t stk_ps_code_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
1158{
1159 int32_t ps_thd_h1_reg, ps_thd_h2_reg;
1160 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1161 mutex_lock(&ps_data->io_lock);
1162 ps_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_PS_REG);
1163 if(ps_thd_h1_reg < 0)
1164 {
1165 printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h1_reg);
1166 return -EINVAL;
1167 }
1168 ps_thd_h2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_PS_REG);
1169 if(ps_thd_h2_reg < 0)
1170 {
1171 printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h2_reg);
1172 return -EINVAL;
1173 }
1174 mutex_unlock(&ps_data->io_lock);
1175 ps_thd_h1_reg = ps_thd_h1_reg<<8 | ps_thd_h2_reg;
1176 return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg);
1177}
1178
1179
1180static ssize_t stk_ps_code_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1181{
1182 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1183 unsigned long value = 0;
1184 int ret;
1185 ret = strict_strtoul(buf, 10, &value);
1186 if(ret < 0)
1187 {
1188 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1189 return ret;
1190 }
1191 mutex_lock(&ps_data->io_lock);
1192 stk3x1x_set_ps_thd_h(ps_data, value);
1193 mutex_unlock(&ps_data->io_lock);
1194 return size;
1195}
1196
1197#if 0
1198static ssize_t stk_als_lux_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
1199{
1200 int32_t als_thd_l0_reg,als_thd_l1_reg;
1201 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1202 uint32_t als_lux;
1203
1204 mutex_lock(&ps_data->io_lock);
1205 als_thd_l0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_ALS_REG);
1206 als_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_ALS_REG);
1207 if(als_thd_l0_reg < 0)
1208 {
1209 printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l0_reg);
1210 return -EINVAL;
1211 }
1212 if(als_thd_l1_reg < 0)
1213 {
1214 printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l1_reg);
1215 return -EINVAL;
1216 }
1217 als_thd_l0_reg|=(als_thd_l1_reg<<8);
1218 als_lux = stk_alscode2lux(ps_data, als_thd_l0_reg);
1219 mutex_unlock(&ps_data->io_lock);
1220 return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
1221}
1222
1223
1224static ssize_t stk_als_lux_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1225{
1226 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1227 unsigned long value = 0;
1228 int ret;
1229 ret = strict_strtoul(buf, 10, &value);
1230 if(ret < 0)
1231 {
1232 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1233 return ret;
1234 }
1235 mutex_lock(&ps_data->io_lock);
1236 value = stk_lux2alscode(ps_data, value);
1237 stk3x1x_set_als_thd_l(ps_data, value);
1238 mutex_unlock(&ps_data->io_lock);
1239 return size;
1240}
1241
1242static ssize_t stk_als_lux_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
1243{
1244 int32_t als_thd_h0_reg,als_thd_h1_reg;
1245 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1246 uint32_t als_lux;
1247
1248 mutex_lock(&ps_data->io_lock);
1249 als_thd_h0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_ALS_REG);
1250 als_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_ALS_REG);
1251 if(als_thd_h0_reg < 0)
1252 {
1253 printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h0_reg);
1254 return -EINVAL;
1255 }
1256 if(als_thd_h1_reg < 0)
1257 {
1258 printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h1_reg);
1259 return -EINVAL;
1260 }
1261 als_thd_h0_reg|=(als_thd_h1_reg<<8);
1262 als_lux = stk_alscode2lux(ps_data, als_thd_h0_reg);
1263 mutex_unlock(&ps_data->io_lock);
1264 return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
1265}
1266
1267
1268static ssize_t stk_als_lux_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1269{
1270 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1271 unsigned long value = 0;
1272 int ret;
1273 ret = strict_strtoul(buf, 10, &value);
1274 if(ret < 0)
1275 {
1276 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1277 return ret;
1278 }
1279 mutex_lock(&ps_data->io_lock);
1280 value = stk_lux2alscode(ps_data, value);
1281 stk3x1x_set_als_thd_h(ps_data, value);
1282 mutex_unlock(&ps_data->io_lock);
1283 return size;
1284}
1285#endif
1286
1287
1288static ssize_t stk_all_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
1289{
1290 int32_t ps_reg[27];
1291 uint8_t cnt;
1292 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1293 mutex_lock(&ps_data->io_lock);
1294 for(cnt=0;cnt<25;cnt++)
1295 {
1296 ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, (cnt));
1297 if(ps_reg[cnt] < 0)
1298 {
1299 mutex_unlock(&ps_data->io_lock);
1300 printk(KERN_ERR "stk_all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
1301 return -EINVAL;
1302 }
1303 else
1304 {
1305 printk(KERN_INFO "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]);
1306 }
1307 }
1308 ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG);
1309 if(ps_reg[cnt] < 0)
1310 {
1311 mutex_unlock(&ps_data->io_lock);
1312 printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
1313 return -EINVAL;
1314 }
1315 printk( KERN_INFO "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]);
1316 cnt++;
1317 ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG);
1318 if(ps_reg[cnt] < 0)
1319 {
1320 mutex_unlock(&ps_data->io_lock);
1321 printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
1322 return -EINVAL;
1323 }
1324 printk( KERN_INFO "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]);
1325 mutex_unlock(&ps_data->io_lock);
1326
1327 return scnprintf(buf, PAGE_SIZE, "%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X\n",
1328 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],
1329 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],
1330 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]);
1331}
1332
1333static ssize_t stk_recv_show(struct device *dev, struct device_attribute *attr, char *buf)
1334{
1335 return 0;
1336}
1337
1338
1339static ssize_t stk_recv_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1340{
1341 unsigned long value = 0;
1342 int ret;
1343 int32_t recv_data;
1344 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1345
1346 if((ret = strict_strtoul(buf, 16, &value)) < 0)
1347 {
1348 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1349 return ret;
1350 }
1351 recv_data = i2c_smbus_read_byte_data(ps_data->client,value);
1352 printk("%s: reg 0x%x=0x%x\n", __func__, (int)value, recv_data);
1353 return size;
1354}
1355
1356
1357static ssize_t stk_send_show(struct device *dev, struct device_attribute *attr, char *buf)
1358{
1359 return 0;
1360}
1361
1362
1363static ssize_t stk_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
1364{
1365 int addr, cmd;
1366 u8 addr_u8, cmd_u8;
1367 int32_t ret, i;
1368 char *token[10];
1369 struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
1370
1371 for (i = 0; i < 2; i++)
1372 token[i] = strsep((char **)&buf, " ");
1373 if((ret = strict_strtoul(token[0], 16, (unsigned long *)&(addr))) < 0)
1374 {
1375 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1376 return ret;
1377 }
1378 if((ret = strict_strtoul(token[1], 16, (unsigned long *)&(cmd))) < 0)
1379 {
1380 printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
1381 return ret;
1382 }
1383 printk(KERN_INFO "%s: write reg 0x%x=0x%x\n", __func__, addr, cmd);
1384
1385 addr_u8 = (u8) addr;
1386 cmd_u8 = (u8) cmd;
1387 //mutex_lock(&ps_data->io_lock);
1388 ret = i2c_smbus_write_byte_data(ps_data->client,addr_u8,cmd_u8);
1389 //mutex_unlock(&ps_data->io_lock);
1390 if (0 != ret)
1391 {
1392 printk(KERN_ERR "%s: i2c_smbus_write_byte_data fail\n", __func__);
1393 return ret;
1394 }
1395
1396 return size;
1397}
1398
1399
1400static struct device_attribute als_enable_attribute = __ATTR(enable,0664,stk_als_enable_show,stk_als_enable_store);
1401static struct device_attribute als_lux_attribute = __ATTR(lux,0664,stk_als_lux_show,stk_als_lux_store);
1402static struct device_attribute als_code_attribute = __ATTR(code, 0444, stk_als_code_show, NULL);
1403static struct device_attribute als_transmittance_attribute = __ATTR(transmittance,0664,stk_als_transmittance_show,stk_als_transmittance_store);
1404static struct device_attribute als_poll_delay_attribute = __ATTR(delay,0664,stk_als_delay_show,stk_als_delay_store);
1405static struct device_attribute als_ir_code_attribute = __ATTR(ircode,0444,stk_als_ir_code_show,NULL);
1406
1407
1408static struct attribute *stk_als_attrs [] =
1409{
1410 &als_enable_attribute.attr,
1411 &als_lux_attribute.attr,
1412 &als_code_attribute.attr,
1413 &als_transmittance_attribute.attr,
1414 &als_poll_delay_attribute.attr,
1415 &als_ir_code_attribute.attr,
1416 NULL
1417};
1418
1419static struct attribute_group stk_als_attribute_group = {
1420 .name = "driver",
1421 .attrs = stk_als_attrs,
1422};
1423
1424
1425static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store);
1426static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store);
1427static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store);
1428static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store);
1429static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
1430static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store);
1431static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store);
1432static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store);
1433static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store);
1434static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
1435
1436static struct attribute *stk_ps_attrs [] =
1437{
1438 &ps_enable_attribute.attr,
1439 &ps_enable_aso_attribute.attr,
1440 &ps_distance_attribute.attr,
1441 &ps_offset_attribute.attr,
1442 &ps_code_attribute.attr,
1443 &ps_code_thd_l_attribute.attr,
1444 &ps_code_thd_h_attribute.attr,
1445 &recv_attribute.attr,
1446 &send_attribute.attr,
1447 &all_reg_attribute.attr,
1448 NULL
1449};
1450
1451static struct attribute_group stk_ps_attribute_group = {
1452 .name = "driver",
1453 .attrs = stk_ps_attrs,
1454};
1455
1456#ifdef STK_POLL_ALS
1457static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer)
1458{
1459 struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer);
1460 queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work);
1461 hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay);
1462 return HRTIMER_RESTART;
1463}
1464
1465static void stk_als_work_func(struct work_struct *work)
1466{
1467 struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_als_work);
1468 int32_t reading;
1469
1470 mutex_lock(&ps_data->io_lock);
1471 reading = stk3x1x_get_als_reading(ps_data);
1472 if(reading < 0)
1473 return;
1474 ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
1475 input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
1476 input_sync(ps_data->als_input_dev);
1477 mutex_unlock(&ps_data->io_lock);
1478 //printk(KERN_INFO "%s: als input event %d lux\n",__func__, ps_data->als_lux_last);
1479}
1480#endif
1481
1482static enum hrtimer_restart stk_ps_timer_func(struct hrtimer *timer)
1483{
1484 struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_timer);
1485 queue_work(ps_data->stk_ps_wq, &ps_data->stk_ps_work);
1486#ifdef STK_POLL_PS
1487 hrtimer_forward_now(&ps_data->ps_timer, ps_data->ps_poll_delay);
1488 return HRTIMER_RESTART;
1489#else
1490 hrtimer_cancel(&ps_data->ps_timer);
1491 return HRTIMER_NORESTART;
1492#endif
1493}
1494
1495static void stk_ps_work_func(struct work_struct *work)
1496{
1497 struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work);
1498 uint32_t reading;
1499 int32_t near_far_state;
1500 uint8_t org_flag_reg;
1501 int32_t ret;
1502 uint8_t disable_flag = 0;
1503 mutex_lock(&ps_data->io_lock);
1504
1505 org_flag_reg = stk3x1x_get_flag(ps_data);
1506 if(org_flag_reg < 0)
1507 {
1508 printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg);
1509 goto err_i2c_rw;
1510 }
1511 near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
1512 reading = stk3x1x_get_ps_reading(ps_data);
1513 if(ps_data->ps_distance_last != near_far_state)
1514 {
1515 ps_data->ps_distance_last = near_far_state;
1516 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
1517 input_sync(ps_data->ps_input_dev);
1518 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
1519#ifdef STK_DEBUG_PRINTF
1520 printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
1521#endif
1522 }
1523 ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
1524 if(ret < 0)
1525 {
1526 printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%d\n", __func__, ret);
1527 goto err_i2c_rw;
1528 }
1529
1530 mutex_unlock(&ps_data->io_lock);
1531 return;
1532
1533err_i2c_rw:
1534 mutex_unlock(&ps_data->io_lock);
1535 msleep(30);
1536 return;
1537}
1538
1539
1540#if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
1541static void stk_work_func(struct work_struct *work)
1542{
1543 uint32_t reading;
1544#if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02))
1545 int32_t ret;
1546 uint8_t disable_flag = 0;
1547 uint8_t org_flag_reg;
1548#endif /* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */
1549
1550#ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
1551 uint32_t nLuxIndex;
1552#endif
1553 struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_work);
1554 int32_t near_far_state;
1555
1556 mutex_lock(&ps_data->io_lock);
1557
1558#if (STK_INT_PS_MODE == 0x03)
1559 near_far_state = gpio_get_value(ps_data->int_pin);
1560#elif (STK_INT_PS_MODE == 0x02)
1561 near_far_state = !(gpio_get_value(ps_data->int_pin));
1562#endif
1563
1564#if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
1565 ps_data->ps_distance_last = near_far_state;
1566 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
1567 input_sync(ps_data->ps_input_dev);
1568 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
1569 reading = stk3x1x_get_ps_reading(ps_data);
1570#ifdef STK_DEBUG_PRINTF
1571 printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
1572#endif
1573#else
1574 /* mode 0x01 or 0x04 */
1575 org_flag_reg = stk3x1x_get_flag(ps_data);
1576 if(org_flag_reg < 0)
1577 {
1578 printk(KERN_ERR "%s: get_status_reg fail, org_flag_reg=%d", __func__, org_flag_reg);
1579 goto err_i2c_rw;
1580 }
1581
1582 if (org_flag_reg & STK_FLG_ALSINT_MASK)
1583 {
1584 disable_flag |= STK_FLG_ALSINT_MASK;
1585 reading = stk3x1x_get_als_reading(ps_data);
1586 if(reading < 0)
1587 {
1588 printk(KERN_ERR "%s: stk3x1x_get_als_reading fail, ret=%d", __func__, reading);
1589 goto err_i2c_rw;
1590 }
1591#ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
1592 nLuxIndex = stk_get_lux_interval_index(reading);
1593 stk3x1x_set_als_thd_h(ps_data, code_threshold_table[nLuxIndex]);
1594 stk3x1x_set_als_thd_l(ps_data, code_threshold_table[nLuxIndex-1]);
1595#else
1596 stk_als_set_new_thd(ps_data, reading);
1597#endif //CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
1598 ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
1599 input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
1600 input_sync(ps_data->als_input_dev);
1601#ifdef STK_DEBUG_PRINTF
1602 printk(KERN_INFO "%s: als input event %d lux\n",__func__, ps_data->als_lux_last);
1603#endif
1604 }
1605 if (org_flag_reg & STK_FLG_PSINT_MASK)
1606 {
1607 disable_flag |= STK_FLG_PSINT_MASK;
1608 near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
1609
1610 ps_data->ps_distance_last = near_far_state;
1611 input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
1612 input_sync(ps_data->ps_input_dev);
1613 wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
1614 reading = stk3x1x_get_ps_reading(ps_data);
1615#ifdef STK_DEBUG_PRINTF
1616 printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n",__func__, near_far_state, reading);
1617#endif
1618 }
1619
1620 ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
1621 if(ret < 0)
1622 {
1623 printk(KERN_ERR "%s:reset_int_flag fail, ret=%d\n", __func__, ret);
1624 goto err_i2c_rw;
1625 }
1626#endif
1627
1628 msleep(1);
1629 enable_irq(ps_data->irq);
1630 mutex_unlock(&ps_data->io_lock);
1631 return;
1632
1633err_i2c_rw:
1634 mutex_unlock(&ps_data->io_lock);
1635 msleep(30);
1636 enable_irq(ps_data->irq);
1637 return;
1638}
1639#endif
1640
1641#if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
1642static irqreturn_t stk_oss_irq_handler(int irq, void *data)
1643{
1644 struct stk3x1x_data *pData = data;
1645 disable_irq_nosync(irq);
1646 queue_work(pData->stk_wq,&pData->stk_work);
1647 return IRQ_HANDLED;
1648}
1649#endif /* #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) */
1650static int32_t stk3x1x_init_all_setting(struct i2c_client *client, struct stk3x1x_platform_data *plat_data)
1651{
1652 int32_t ret;
1653 struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
1654
1655 mutex_lock(&ps_data->io_lock);
1656 ps_data->als_enabled = false;
1657 ps_data->ps_enabled = false;
1658 mutex_unlock(&ps_data->io_lock);
1659
1660 ret = stk3x1x_software_reset(ps_data);
1661 if(ret < 0)
1662 return ret;
1663
1664 stk3x1x_check_pid(ps_data);
1665 if(ret < 0)
1666 return ret;
1667
1668 ret = stk3x1x_init_all_reg(ps_data, plat_data);
1669 if(ret < 0)
1670 return ret;
1671#ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
1672 stk_init_code_threshold_table(ps_data);
1673#endif
1674 return 0;
1675}
1676
1677#if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
1678static int stk3x1x_setup_irq(struct i2c_client *client)
1679{
1680 int irq, err = -EIO;
1681 struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
1682
1683 irq = gpio_to_irq(ps_data->int_pin);
1684#ifdef STK_DEBUG_PRINTF
1685 printk(KERN_INFO "%s: int pin #=%d, irq=%d\n",__func__, ps_data->int_pin, irq);
1686#endif
1687 if (irq <= 0)
1688 {
1689 printk(KERN_ERR "irq number is not specified, irq # = %d, int pin=%d\n",irq, ps_data->int_pin);
1690 return irq;
1691 }
1692 ps_data->irq = irq;
1693 err = gpio_request(ps_data->int_pin,"stk-int");
1694 if(err < 0)
1695 {
1696 printk(KERN_ERR "%s: gpio_request, err=%d", __func__, err);
1697 return err;
1698 }
1699 err = gpio_direction_input(ps_data->int_pin);
1700 if(err < 0)
1701 {
1702 printk(KERN_ERR "%s: gpio_direction_input, err=%d", __func__, err);
1703 return err;
1704 }
1705#if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
1706 err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, DEVICE_NAME, ps_data);
1707#else
1708 err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_LOW, DEVICE_NAME, ps_data);
1709#endif
1710 if (err < 0)
1711 {
1712 printk(KERN_WARNING "%s: request_any_context_irq(%d) failed for (%d)\n", __func__, irq, err);
1713 goto err_request_any_context_irq;
1714 }
1715 disable_irq(irq);
1716
1717 return 0;
1718err_request_any_context_irq:
1719 gpio_free(ps_data->int_pin);
1720 return err;
1721}
1722#endif
1723
1724#ifdef CONFIG_HAS_EARLYSUSPEND
1725static void stk3x1x_early_suspend(struct early_suspend *h)
1726{
1727 struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
1728#ifndef STK_POLL_PS
1729 int err;
1730#endif
1731
1732 printk(KERN_INFO "%s", __func__);
1733 mutex_lock(&ps_data->io_lock);
1734 if(ps_data->als_enabled)
1735 {
1736 stk3x1x_enable_als(ps_data, 0);
1737 ps_data->als_enabled = true;
1738 }
1739 if(ps_data->ps_enabled)
1740 {
1741#ifdef STK_POLL_PS
1742 wake_lock(&ps_data->ps_nosuspend_wl);
1743#else
1744 err = enable_irq_wake(ps_data->irq);
1745 if (err)
1746 printk(KERN_WARNING "%s: set_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
1747#endif
1748 }
1749 mutex_unlock(&ps_data->io_lock);
1750 return;
1751}
1752
1753static void stk3x1x_late_resume(struct early_suspend *h)
1754{
1755 struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
1756#ifndef STK_POLL_PS
1757 int err;
1758#endif
1759
1760 printk(KERN_INFO "%s", __func__);
1761 mutex_lock(&ps_data->io_lock);
1762 if(ps_data->als_enabled)
1763 stk3x1x_enable_als(ps_data, 1);
1764
1765 if(ps_data->ps_enabled)
1766 {
1767#ifdef STK_POLL_PS
1768 wake_lock(&ps_data->ps_nosuspend_wl);
1769#else
1770 err = disable_irq_wake(ps_data->irq);
1771 if (err)
1772 printk(KERN_WARNING "%s: disable_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
1773#endif
1774 }
1775 mutex_unlock(&ps_data->io_lock);
1776 return;
1777}
1778#endif //#ifdef CONFIG_HAS_EARLYSUSPEND
1779
1780
1781static int stk3x1x_probe(struct i2c_client *client,
1782 const struct i2c_device_id *id)
1783{
1784 int err = -ENODEV;
1785 struct stk3x1x_data *ps_data;
1786 struct stk3x1x_platform_data *plat_data;
1787 printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION);
1788
1789 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1790 {
1791 printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATA\n", __func__);
1792 return -ENODEV;
1793 }
1794 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
1795 {
1796 printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATA\n", __func__);
1797 return -ENODEV;
1798 }
1799
1800 ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL);
1801 if(!ps_data)
1802 {
1803 printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__);
1804 return -ENOMEM;
1805 }
1806 ps_data->client = client;
1807 i2c_set_clientdata(client,ps_data);
1808 mutex_init(&ps_data->io_lock);
1809 wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock");
1810
1811#ifdef STK_POLL_PS
1812 wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock");
1813#endif
1814 if(client->dev.platform_data != NULL)
1815 {
1816 plat_data = client->dev.platform_data;
1817 ps_data->als_transmittance = plat_data->transmittance;
1818 ps_data->int_pin = plat_data->int_pin;
1819 if(ps_data->als_transmittance == 0)
1820 {
1821 printk(KERN_ERR "%s: Please set als_transmittance in platform data\n", __func__);
1822 goto err_als_input_allocate;
1823 }
1824 }
1825 else
1826 {
1827 printk(KERN_ERR "%s: no stk3x1x platform data!\n", __func__);
1828 goto err_als_input_allocate;
1829 }
1830
1831 ps_data->als_input_dev = input_allocate_device();
1832 if (ps_data->als_input_dev==NULL)
1833 {
1834 printk(KERN_ERR "%s: could not allocate als device\n", __func__);
1835 err = -ENOMEM;
1836 goto err_als_input_allocate;
1837 }
1838 ps_data->ps_input_dev = input_allocate_device();
1839 if (ps_data->ps_input_dev==NULL)
1840 {
1841 printk(KERN_ERR "%s: could not allocate ps device\n", __func__);
1842 err = -ENOMEM;
1843 goto err_ps_input_allocate;
1844 }
1845 ps_data->als_input_dev->name = ALS_NAME;
1846 ps_data->ps_input_dev->name = PS_NAME;
1847 set_bit(EV_ABS, ps_data->als_input_dev->evbit);
1848 set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
1849 input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0);
1850 input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0);
1851 err = input_register_device(ps_data->als_input_dev);
1852 if (err<0)
1853 {
1854 printk(KERN_ERR "%s: can not register als input device\n", __func__);
1855 goto err_als_input_register;
1856 }
1857 err = input_register_device(ps_data->ps_input_dev);
1858 if (err<0)
1859 {
1860 printk(KERN_ERR "%s: can not register ps input device\n", __func__);
1861 goto err_ps_input_register;
1862 }
1863
1864 err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
1865 if (err < 0)
1866 {
1867 printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__);
1868 goto err_als_sysfs_create_group;
1869 }
1870 err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
1871 if (err < 0)
1872 {
1873 printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__);
1874 goto err_ps_sysfs_create_group;
1875 }
1876 input_set_drvdata(ps_data->als_input_dev, ps_data);
1877 input_set_drvdata(ps_data->ps_input_dev, ps_data);
1878
1879#ifdef STK_POLL_ALS
1880 ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq");
1881 INIT_WORK(&ps_data->stk_als_work, stk_als_work_func);
1882 hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1883 ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
1884 ps_data->als_timer.function = stk_als_timer_func;
1885#endif
1886
1887 ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq");
1888 INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);
1889 hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1890 ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
1891 ps_data->ps_timer.function = stk_ps_timer_func;
1892#if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
1893 ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
1894 INIT_WORK(&ps_data->stk_work, stk_work_func);
1895 err = stk3x1x_setup_irq(client);
1896 if(err < 0)
1897 goto err_stk3x1x_setup_irq;
1898#endif
1899
1900 err = stk3x1x_init_all_setting(client, plat_data);
1901 if(err < 0)
1902 goto err_init_all_setting;
1903#ifdef CONFIG_HAS_EARLYSUSPEND
1904 ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1905 ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend;
1906 ps_data->stk_early_suspend.resume = stk3x1x_late_resume;
1907 register_early_suspend(&ps_data->stk_early_suspend);
1908#endif
1909 printk(KERN_INFO "%s: probe successfully", __func__);
1910 return 0;
1911
1912err_init_all_setting:
1913#ifndef STK_POLL_PS
1914 free_irq(ps_data->irq, ps_data);
1915 gpio_free(plat_data->int_pin);
1916#endif
1917#if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
1918err_stk3x1x_setup_irq:
1919#endif
1920#ifdef STK_POLL_ALS
1921 hrtimer_try_to_cancel(&ps_data->als_timer);
1922 destroy_workqueue(ps_data->stk_als_wq);
1923#endif
1924 destroy_workqueue(ps_data->stk_ps_wq);
1925#if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
1926 destroy_workqueue(ps_data->stk_wq);
1927#endif
1928 sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
1929err_ps_sysfs_create_group:
1930 sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
1931err_als_sysfs_create_group:
1932 input_unregister_device(ps_data->ps_input_dev);
1933err_ps_input_register:
1934 input_unregister_device(ps_data->als_input_dev);
1935err_als_input_register:
1936 input_free_device(ps_data->ps_input_dev);
1937err_ps_input_allocate:
1938 input_free_device(ps_data->als_input_dev);
1939err_als_input_allocate:
1940#ifdef STK_POLL_PS
1941 wake_lock_destroy(&ps_data->ps_nosuspend_wl);
1942#endif
1943 wake_lock_destroy(&ps_data->ps_wakelock);
1944 mutex_destroy(&ps_data->io_lock);
1945 kfree(ps_data);
1946 return err;
1947}
1948
1949
1950static int stk3x1x_remove(struct i2c_client *client)
1951{
1952 struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
1953#ifndef STK_POLL_PS
1954 free_irq(ps_data->irq, ps_data);
1955 gpio_free(ps_data->int_pin);
1956#endif
1957#ifdef STK_POLL_ALS
1958 hrtimer_try_to_cancel(&ps_data->als_timer);
1959 destroy_workqueue(ps_data->stk_als_wq);
1960#endif
1961 destroy_workqueue(ps_data->stk_ps_wq);
1962#if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
1963 destroy_workqueue(ps_data->stk_wq);
1964#endif
1965 sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
1966 sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
1967 input_unregister_device(ps_data->ps_input_dev);
1968 input_unregister_device(ps_data->als_input_dev);
1969 input_free_device(ps_data->ps_input_dev);
1970 input_free_device(ps_data->als_input_dev);
1971#ifdef STK_POLL_PS
1972 wake_lock_destroy(&ps_data->ps_nosuspend_wl);
1973#endif
1974 wake_lock_destroy(&ps_data->ps_wakelock);
1975 mutex_destroy(&ps_data->io_lock);
1976 kfree(ps_data);
1977
1978 return 0;
1979}
1980
1981static const struct i2c_device_id stk_ps_id[] =
1982{
1983 { "stk_ps", 0},
1984 {}
1985};
1986MODULE_DEVICE_TABLE(i2c, stk_ps_id);
1987
1988static struct i2c_driver stk_ps_driver =
1989{
1990 .driver = {
1991 .name = DEVICE_NAME,
1992 .owner = THIS_MODULE,
1993 },
1994 .probe = stk3x1x_probe,
1995 .remove = stk3x1x_remove,
1996 .id_table = stk_ps_id,
1997};
1998
1999
2000static int __init stk3x1x_init(void)
2001{
2002 int ret;
2003 ret = i2c_add_driver(&stk_ps_driver);
2004 if (ret)
2005 return ret;
2006
2007 return 0;
2008}
2009
2010static void __exit stk3x1x_exit(void)
2011{
2012 i2c_del_driver(&stk_ps_driver);
2013}
2014
2015module_init(stk3x1x_init);
2016module_exit(stk3x1x_exit);
2017MODULE_AUTHOR("Lex Hsieh <lex_hsieh@sitronix.com.tw>");
2018MODULE_DESCRIPTION("Sensortek stk3x1x Proximity Sensor driver");
2019MODULE_LICENSE("GPL");
2020MODULE_VERSION(DRIVER_VERSION);