blob: 322f27d52c90448659746204342ecb231ef1c953 [file] [log] [blame]
Flemmarde2ce3b72013-04-25 21:59:45 -07001/* drivers/i2c/chips/cm3629.c - cm3629 optical sensors driver
2 *
3 * Copyright (C) 2010 HTC, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/delay.h>
17#include <linux/earlysuspend.h>
18#include <linux/i2c.h>
19#include <linux/input.h>
20#include <linux/interrupt.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/workqueue.h>
24#include <linux/irq.h>
25#include <linux/errno.h>
26#include <linux/err.h>
27#include <linux/gpio.h>
28#include <linux/miscdevice.h>
29#include <linux/lightsensor.h>
30#include <linux/slab.h>
31#include <asm/uaccess.h>
32#include <asm/mach-types.h>
33#include <linux/cm3629.h>
34#include <linux/pl_sensor.h>
35#include <linux/capella_cm3602.h>
36#include <asm/setup.h>
37#include <linux/wakelock.h>
38#include <linux/jiffies.h>
39#include <mach/board.h>
40
41#define D(x...) pr_info(x)
42
43#define I2C_RETRY_COUNT 10
44
45#define POLLING_PROXIMITY 1
46#define NO_IGNORE_BOOT_MODE 1
47
48#define NEAR_DELAY_TIME ((100 * HZ) / 1000)
49
50#ifdef POLLING_PROXIMITY
51#define POLLING_DELAY 200
52#define TH_ADD 10
53#endif
54static int record_init_fail = 0;
55static void sensor_irq_do_work(struct work_struct *work);
56static DECLARE_WORK(sensor_irq_work, sensor_irq_do_work);
57
58#ifdef POLLING_PROXIMITY
59static void polling_do_work(struct work_struct *w);
60static DECLARE_DELAYED_WORK(polling_work, polling_do_work);
61#endif
62
63static uint8_t sensor_chipId[3] = {0};
64static void report_near_do_work(struct work_struct *w);
65static DECLARE_DELAYED_WORK(report_near_work, report_near_do_work);
66static int inter_error = 0;
67static int is_probe_success;
68
69struct cm3629_info {
70 struct class *cm3629_class;
71 struct device *ls_dev;
72 struct device *ps_dev;
73
74 struct input_dev *ls_input_dev;
75 struct input_dev *ps_input_dev;
76
77 struct early_suspend early_suspend;
78 struct i2c_client *i2c_client;
79 struct workqueue_struct *lp_wq;
80
81 int model;
82
83 int intr_pin;
84
85 int als_enable;
86
87 int ps_enable;
88 int ps_irq_flag;
89 int led;
90
91 uint16_t *adc_table;
92 uint16_t cali_table[10];
93 int irq;
94
95 int ls_calibrate;
96
97 int (*power)(int, uint8_t);
98 int (*lpm_power)(uint8_t);
99 uint32_t als_kadc;
100 uint32_t als_gadc;
101 uint16_t golden_adc;
102
103 struct wake_lock ps_wake_lock;
104 int psensor_opened;
105 int lightsensor_opened;
106 uint16_t cm3629_slave_address;
107 uint8_t ps_select;
108 uint8_t ps1_thd_set;
109 uint8_t ps1_thh_diff;
110 uint8_t ps2_thd_set;
111 uint8_t original_ps_thd_set;
112 int current_level;
113 uint16_t current_adc;
114
115 uint8_t inte_ps1_canc;
116 uint8_t inte_ps2_canc;
117 uint8_t ps_conf1_val;
118 uint8_t ps_conf2_val;
119 uint8_t ps_conf1_val_from_board;
120 uint8_t ps_conf2_val_from_board;
121 uint8_t ps_conf3_val;
122 uint8_t ps_calibration_rule;
123 int ps_pocket_mode;
124
125 unsigned long j_start;
126 unsigned long j_end;
127 int mfg_mode;
128
129 uint8_t *mapping_table;
130 uint8_t mapping_size;
131 uint8_t ps_base_index;
132
133 uint8_t ps1_thd_no_cal;
134 uint8_t ps1_thd_with_cal;
135 uint8_t ps2_thd_no_cal;
136 uint8_t ps2_thd_with_cal;
137 uint8_t enable_polling_ignore;
138 uint8_t ls_cmd;
139 uint8_t ps1_adc_offset;
140 uint8_t ps2_adc_offset;
141 uint8_t ps_debounce;
142 uint16_t ps_delay_time;
143 unsigned int no_need_change_setting;
144 int ps_th_add;
145 uint8_t dark_level;
146};
147
148static uint8_t ps1_canc_set;
149static uint8_t ps2_canc_set;
150static uint8_t ps1_offset_adc;
151static uint8_t ps2_offset_adc;
152static struct cm3629_info *lp_info;
153int enable_cm3629_log;
154int f_cm3629_level = -1;
155static struct mutex als_enable_mutex, als_disable_mutex, als_get_adc_mutex;
156static struct mutex ps_enable_mutex;
157static int ps_hal_enable, ps_drv_enable;
158static int lightsensor_enable(struct cm3629_info *lpi);
159static int lightsensor_disable(struct cm3629_info *lpi);
160static void psensor_initial_cmd(struct cm3629_info *lpi);
161static int ps_near;
162static int pocket_mode_flag, psensor_enable_by_touch;
163#if (0)
164static int I2C_RxData(uint16_t slaveAddr, uint8_t *rxData, int length)
165{
166 uint8_t loop_i;
167 struct cm3629_info *lpi = lp_info;
168
169 struct i2c_msg msgs[] = {
170 {
171 .addr = slaveAddr,
172 .flags = I2C_M_RD,
173 .len = length,
174 .buf = rxData,
175 },
176 };
177
178 for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
179
180 if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 1) > 0)
181 break;
182
183
184
185 D("[PS][cm3629 warning] %s, i2c err, slaveAddr 0x%x ISR gpio %d , record_init_fail %d \n",
186 __func__, slaveAddr, lpi->intr_pin, record_init_fail);
187
Ethan Chenf969e322013-09-02 22:49:51 -0700188 usleep(10);
Flemmarde2ce3b72013-04-25 21:59:45 -0700189 }
190 if (loop_i >= I2C_RETRY_COUNT) {
191 printk(KERN_ERR "[PS_ERR][cm3629 error] %s retry over %d\n",
192 __func__, I2C_RETRY_COUNT);
193 return -EIO;
194 }
195
196 return 0;
197}
198#endif
199
200static int I2C_RxData_2(char *rxData, int length)
201{
202 uint8_t loop_i;
203 struct cm3629_info *lpi = lp_info;
204
205 struct i2c_msg msgs[] = {
206 {
207 .addr = lp_info->i2c_client->addr,
208 .flags = 0,
209 .len = 1,
210 .buf = rxData,
211 },
212 {
213 .addr = lp_info->i2c_client->addr,
214 .flags = I2C_M_RD,
215 .len = length,
216 .buf = rxData,
217 },
218 };
219
220 for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
221 if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 2) > 0)
222 break;
223
224 D("[PS][cm3629 warning] %s, i2c err, ISR gpio %d\n",
225 __func__, lpi->intr_pin);
Ethan Chenf969e322013-09-02 22:49:51 -0700226 usleep(10);
Flemmarde2ce3b72013-04-25 21:59:45 -0700227 }
228
229 if (loop_i >= I2C_RETRY_COUNT) {
230 printk(KERN_ERR "[PS_ERR][cm3629 error] %s retry over %d\n",
231 __func__, I2C_RETRY_COUNT);
232 return -EIO;
233 }
234
235 return 0;
236}
237static int I2C_TxData(uint16_t slaveAddr, uint8_t *txData, int length)
238{
239 uint8_t loop_i;
240 struct cm3629_info *lpi = lp_info;
241 struct i2c_msg msg[] = {
242 {
243 .addr = slaveAddr,
244 .flags = 0,
245 .len = length,
246 .buf = txData,
247 },
248 };
249
250 for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
251 if (i2c_transfer(lp_info->i2c_client->adapter, msg, 1) > 0)
252 break;
253
254 D("[PS][cm3629 warning] %s, i2c err, slaveAddr 0x%x, register 0x%x, value 0x%x, ISR gpio%d, record_init_fail %d\n",
255 __func__, slaveAddr, txData[0], txData[1], lpi->intr_pin, record_init_fail);
256
Ethan Chenf969e322013-09-02 22:49:51 -0700257 usleep(10);
Flemmarde2ce3b72013-04-25 21:59:45 -0700258 }
259
260 if (loop_i >= I2C_RETRY_COUNT) {
261 printk(KERN_ERR "[PS_ERR][cm3629 error] %s retry over %d\n",
262 __func__, I2C_RETRY_COUNT);
263 return -EIO;
264 }
265
266 return 0;
267}
268
269static int _cm3629_I2C_Read2(uint16_t slaveAddr,
270 uint8_t cmd, uint8_t *pdata, int length)
271{
272 char buffer[3] = {0};
273 int ret = 0, i;
274
275 if (pdata == NULL)
276 return -EFAULT;
277
278 if (length > 2) {
279 pr_err(
280 "[PS_ERR][cm3629 error]%s: length %d> 2: \n",
281 __func__, length);
282 return ret;
283 }
284 buffer[0] = cmd;
285 ret = I2C_RxData_2(buffer, length);
286 if (ret < 0) {
287 pr_err(
288 "[PS_ERR][cm3629 error]%s: I2C_RxData fail, slave addr: 0x%x\n",
289 __func__, slaveAddr);
290 return ret;
291 }
292
293 for (i = 0; i < length; i++) {
294 *(pdata+i) = buffer[i];
295 }
296#if 0
297
298 printk(KERN_DEBUG "[cm3629] %s: I2C_RxData[0x%x] = 0x%x\n",
299 __func__, slaveAddr, buffer);
300#endif
301 return ret;
302}
303
304static int _cm3629_I2C_Write2(uint16_t SlaveAddress,
305 uint8_t cmd, uint8_t *data, int length)
306{
307 char buffer[3];
308 int ret = 0;
309#if 0
310
311 printk(KERN_DEBUG
312 "[cm3629] %s: _cm3629_I2C_Write_Byte[0x%x, 0x%x, 0x%x]\n",
313 __func__, SlaveAddress, cmd, data);
314#endif
315 if (length > 3) {
316 pr_err(
317 "[PS_ERR][cm3629 error]%s: length %d> 2: \n",
318 __func__, length);
319 return ret;
320 }
321
322 buffer[0] = cmd;
323 buffer[1] = *data;
324 buffer[2] = *(data+1);
325 ret = I2C_TxData(SlaveAddress, buffer, length);
326 if (ret < 0) {
327 pr_err("[PS_ERR][cm3629 error]%s: I2C_TxData fail\n", __func__);
328 return -EIO;
329 }
330
331 return ret;
332}
333#if 0
334static int _cm3629_I2C_Read_Byte(uint16_t slaveAddr, uint8_t *pdata)
335{
336 uint8_t buffer = 0;
337 int ret = 0;
338
339 if (pdata == NULL)
340 return -EFAULT;
341 buffer = *pdata;
342 ret = I2C_RxData(slaveAddr, &buffer, 1);
343 if (ret < 0) {
344 pr_err(
345 "[CM3629_ error]%s: I2C_RxData fail, slave addr: 0x%x\n",
346 __func__, slaveAddr);
347 return ret;
348 }
349
350 *pdata = buffer;
351#if 0
352
353 printk(KERN_DEBUG "[CM3629_] %s: I2C_RxData[0x%x] = 0x%x\n",
354 __func__, slaveAddr, buffer);
355#endif
356 return ret;
357}
358static int _cm3629_I2C_Write_Byte(uint16_t SlaveAddress,
359 uint8_t cmd, uint8_t data)
360{
361 char buffer[2];
362 int ret = 0;
363#if 0
364
365 printk(KERN_DEBUG
366 "[cm3629] %s: _cm3629_I2C_Write_Byte[0x%x, 0x%x, 0x%x]\n",
367 __func__, SlaveAddress, cmd, data);
368#endif
369 buffer[0] = cmd;
370 buffer[1] = data;
371 ret = I2C_TxData(SlaveAddress, buffer, 2);
372 if (ret < 0) {
373 pr_err("[PS_ERR][cm3629 error]%s: I2C_TxData fail\n", __func__);
374 return -EIO;
375 }
376
377 return ret;
378}
379#endif
380static int sensor_lpm_power(int enable)
381{
382 struct cm3629_info *lpi = lp_info;
383
384 if (lpi->lpm_power)
385 lpi->lpm_power(enable);
386
387 return 0;
388}
389static int get_ls_adc_value(uint32_t *als_step, bool resume)
390{
391
392 struct cm3629_info *lpi = lp_info;
393 uint8_t lsb, msb;
394 int ret = 0;
395 char cmd[3];
396 char ls_cmd;
397
398 if (als_step == NULL)
399 return -EFAULT;
400
401 if (resume) {
402 if (sensor_chipId[0] != 0x29)
403 ls_cmd = (CM3629_ALS_IT_80ms | CM3629_ALS_PERS_1);
404 else
405 ls_cmd = (CM3629_ALS_IT_50ms | CM3629_ALS_PERS_1);
406 D("[LS][cm3629] %s:resume %d\n",
407 __func__, resume);
408 } else
409 ls_cmd = (lpi->ls_cmd);
410
411 cmd[0] = ls_cmd;
412 cmd[1] = 0;
413 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
414 ALS_config_cmd, cmd, 3);
415
416 if (ret < 0) {
417 pr_err(
418 "[LS][cm3629 error]%s: _cm3629_I2C_Write_Byte fail\n",
419 __func__);
420 return -EIO;
421 }
422
423
424
425 ret = _cm3629_I2C_Read2(lpi->cm3629_slave_address, ALS_data, cmd, 2);
426 if (ret < 0) {
427 pr_err(
428 "[LS][cm3629 error]%s: _cm3629_I2C_Read_Byte fail\n",
429 __func__);
430 return -EIO;
431 }
432 lsb = cmd[0];
433 msb = cmd[1];
434
435 *als_step = (uint32_t)msb;
436 *als_step <<= 8;
437 *als_step |= (uint32_t)lsb;
438
439 D("[LS][cm3629] %s: raw adc = 0x%X, ls_calibrate = %d\n",
440 __func__, *als_step, lpi->ls_calibrate);
441
442
443 if (!lpi->ls_calibrate) {
444 *als_step = (*als_step) * lpi->als_gadc / lpi->als_kadc;
445 if (*als_step > 0xFFFF)
446 *als_step = 0xFFFF;
447 }
448
449
450 return ret;
451}
452
453static int get_ps_adc_value(uint8_t *ps1_adc, uint8_t *ps2_adc)
454{
455 int ret = 0;
456 struct cm3629_info *lpi = lp_info;
457 char cmd[3];
458
459 if (ps1_adc == NULL || ps2_adc == NULL)
460 return -EFAULT;
461
462 ret = _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_data, cmd, 2);
463 if (ret < 0) {
464 pr_err("[PS_ERR][cm3629 error] %s: _cm3629_I2C_Read_Byte "
465 "MSB fail\n", __func__);
466 return -EIO;
467 }
468
469 *ps1_adc = cmd[0];
470 *ps2_adc = cmd[1];
471 return ret;
472}
473
474static int set_lsensor_range(uint16_t low_thd, uint16_t high_thd)
475{
476 int ret = 0;
477 struct cm3629_info *lpi = lp_info;
478 char cmd[3] = {0};
479
480 uint8_t high_msb;
481 uint8_t high_lsb;
482 uint8_t low_msb;
483 uint8_t low_lsb;
484 D("[cm3629] %s: low_thd = 0x%X, high_thd = 0x%x \n",
485 __func__, low_thd, high_thd);
486 high_msb = (uint8_t) (high_thd >> 8);
487 high_lsb = (uint8_t) (high_thd & 0x00ff);
488 low_msb = (uint8_t) (low_thd >> 8);
489 low_lsb = (uint8_t) (low_thd & 0x00ff);
490
491 cmd[0] = high_lsb;
492 cmd[1] = high_msb;
493 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
494 ALS_high_thd, cmd, 3);
495
496 cmd[0] = low_lsb;
497 cmd[1] = low_msb;
498 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
499 ALS_low_thd, cmd, 3);
500 return ret;
501}
502
503static void report_near_do_work(struct work_struct *w)
504{
505 struct cm3629_info *lpi = lp_info;
506
507 D("[PS][cm3629] %s: delay %dms, report proximity NEAR\n", __func__, lpi->ps_delay_time);
508
509 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, 0);
510 input_sync(lpi->ps_input_dev);
511 blocking_notifier_call_chain(&psensor_notifier_list, 2, NULL);
512}
513
514static void report_psensor_input_event(struct cm3629_info *lpi, int interrupt_flag)
515{
516 uint8_t ps_thd_set = 0;
517 uint8_t ps_adc = 0;
518 uint8_t ps1_adc = 0;
519 uint8_t ps2_adc = 0;
520 int val, ret = 0;
521 int index = 0;
522
523 if (interrupt_flag > 1 && lpi->ps_enable == 0) {
524 D("[PS][cm3629] P-sensor disable but intrrupt occur, "
525 "record_init_fail %d.\n", record_init_fail);
526 return;
527 }
528
529 if (lpi->ps_debounce == 1 && lpi->mfg_mode != NO_IGNORE_BOOT_MODE)
530 cancel_delayed_work(&report_near_work);
531
532 lpi->j_end = jiffies;
533
534
535 ret = get_ps_adc_value(&ps1_adc, &ps2_adc);
536 if (pocket_mode_flag == 1 || psensor_enable_by_touch == 1) {
537 D("[PS][cm3629] pocket_mode_flag: %d, psensor_enable_by_touch: %d", pocket_mode_flag, psensor_enable_by_touch);
538 while (index <= 10 && ps1_adc == 0) {
539 D("[PS][cm3629]ps1_adc = 0 retry");
540 get_ps_adc_value(&ps1_adc, &ps2_adc);
541 if(ps1_adc != 0) {
542 D("[PS][cm3629]retry work");
543 break;
544 }
545 mdelay(1);
546 index++;
547 }
548 }
549 if (lpi->ps_select == CM3629_PS2_ONLY) {
550 ps_thd_set = lpi->ps2_thd_set + 1;
551 ps_adc = ps2_adc;
552 } else {
553 if (lpi->ps1_thh_diff == 0)
554 ps_thd_set = lpi->ps1_thd_set + 1;
555 else
556 ps_thd_set = lpi->ps1_thd_set + lpi->ps1_thh_diff;
557 ps_adc = ps1_adc;
558 }
559 if (interrupt_flag == 0) {
560 if (ret == 0) {
561 val = (ps_adc >= ps_thd_set) ? 0 : 1;
562 } else {
563 val = 1;
564 ps_adc = 0;
565 D("[PS][cm3629] proximity i2c err, report %s, "
566 "ps_adc=%d, record_init_fail %d\n",
567 val ? "FAR" : "NEAR", ps_adc, record_init_fail);
568 }
569 } else {
570 val = (interrupt_flag == 2) ? 0 : 1;
571 }
572 ps_near = !val;
573
574 if (lpi->ps_debounce == 1 && lpi->mfg_mode != NO_IGNORE_BOOT_MODE) {
575 if (val == 0) {
576 D("[PS][cm3629] delay proximity %s, ps_adc=%d, High thd= %d, interrupt_flag %d\n",
577 val ? "FAR" : "NEAR", ps_adc, ps_thd_set, interrupt_flag);
578 queue_delayed_work(lpi->lp_wq, &report_near_work,
579 msecs_to_jiffies(lpi->ps_delay_time));
580 return;
581 } else {
582
583 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, -1);
584 input_sync(lpi->ps_input_dev);
585 }
586 }
587 D("[PS][cm3629] proximity %s, ps_adc=%d, , High thd= %d, interrupt_flag %d\n",
588 val ? "FAR" : "NEAR", ps_adc, ps_thd_set, interrupt_flag);
589 if ((lpi->enable_polling_ignore == 1) && (val == 0) &&
590 (lpi->mfg_mode != NO_IGNORE_BOOT_MODE) &&
591 (time_before(lpi->j_end, (lpi->j_start + NEAR_DELAY_TIME)))) {
592 D("[PS][cm3629] Ignore NEAR event\n");
593 lpi->ps_pocket_mode = 1;
594 } else {
595
596 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, val);
597 input_sync(lpi->ps_input_dev);
598 blocking_notifier_call_chain(&psensor_notifier_list, val+2, NULL);
599 }
600}
601
602static void enable_als_interrupt(void)
603{
604 char cmd[3];
605 struct cm3629_info *lpi = lp_info;
606 int ret = 0;
607
608 cmd[0] = (lpi->ls_cmd | CM3629_ALS_INT_EN);
609 cmd[1] = 0;
610 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
611 ALS_config_cmd, cmd, 3);
612 if (ret != 0) {
613 lpi->als_enable = 0;
614 D("[LS][cm3629] L-sensor i2c err, enable interrupt error\n");
615 } else
616 lpi->als_enable = 1;
617}
618
619static void report_lsensor_input_event(struct cm3629_info *lpi, bool resume)
620{
621 uint32_t adc_value = 0;
622 int level = 0, i, ret = 0;
623
624 mutex_lock(&als_get_adc_mutex);
625
626 ret = get_ls_adc_value(&adc_value, resume);
627 if (resume) {
628 if (sensor_chipId[0] != 0x29)
629 adc_value = adc_value*4;
630 else
631 adc_value = adc_value*8;
632 }
633 for (i = 0; i < 10; i++) {
634 if (adc_value <= (*(lpi->adc_table + i))) {
635 level = i;
636 if (*(lpi->adc_table + i))
637 break;
638 }
639 if (i == 9) {
640 level = i;
641 break;
642 }
643 }
644 ret = set_lsensor_range(((i == 0) || (adc_value == 0)) ? 0 :
645 *(lpi->cali_table + (i - 1)) + 1,
646 *(lpi->cali_table + i));
647
648 if (ret < 0)
649 printk(KERN_ERR "[LS][cm3629 error] %s fail\n", __func__);
650
651 if ((i == 0) || (adc_value == 0))
652 D("[LS][cm3629] %s: ADC=0x%03X, Level=%d, l_thd equal 0, h_thd = 0x%x \n",
653 __func__, adc_value, level, *(lpi->cali_table + i));
654 else
655 D("[LS][cm3629] %s: ADC=0x%03X, Level=%d, l_thd = 0x%x, h_thd = 0x%x \n",
656 __func__, adc_value, level, *(lpi->cali_table + (i - 1)) + 1, *(lpi->cali_table + i));
657
658 lpi->current_level = level;
659 lpi->current_adc = adc_value;
660
661 if (f_cm3629_level >= 0) {
662 D("[LS][cm3629] L-sensor force level enable level=%d f_cm3629_level=%d\n", level, f_cm3629_level);
663 level = f_cm3629_level;
664 }
665 input_report_abs(lpi->ls_input_dev, ABS_MISC, level);
666 input_sync(lpi->ls_input_dev);
667 enable_als_interrupt();
668 mutex_unlock(&als_get_adc_mutex);
669
670}
671
672static void enable_ps_interrupt(char *ps_conf)
673{
674 struct cm3629_info *lpi = lp_info;
675 int ret;
676 char cmd[2] = {0};
677
678 lpi->ps_enable = 1;
679
680 cmd[0] = lpi->ps1_thd_set;
681 if (lpi->ps1_thh_diff == 0)
682 cmd[1] = lpi->ps1_thd_set + 1;
683 else
684 cmd[1] = lpi->ps1_thd_set + lpi->ps1_thh_diff;
685 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
686 PS_1_thd, cmd, 3);
687
688 cmd[0] = lpi->ps2_thd_set;
689 cmd[1] = lpi->ps2_thd_set + 1;
690 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
691 PS_2_thd, cmd, 3);
692
693 cmd[0] = ps_conf[2];
694 cmd[1] = CM3629_PS_255_STEPS;
695 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
696 PS_config_ms, cmd, 3);
697
698 cmd[0] = ps_conf[0];
699 cmd[1] = ps_conf[1];
700 D("[PS][cm3629] %s, write cmd[0] = 0x%x, cmd[1] = 0x%x\n",
701 __func__, cmd[0], cmd[1]);
702 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_config, cmd, 3);
703
704 if (ret != 0) {
705 lpi->ps_enable = 0;
706 D("[PS][cm3629] P-sensor i2c err, enable interrupt error\n");
707 }
708 _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_config, cmd, 2);
709 D("[PS][cm3629] %s, read value => cmd[0] = 0x%x, cmd[1] = 0x%x\n", __func__, cmd[0], cmd[1]);
710}
711
712static void sensor_irq_do_work(struct work_struct *work)
713{
714 struct cm3629_info *lpi = lp_info;
715 uint8_t cmd[3];
716 uint8_t add = 0;
717
718
719 wake_lock_timeout(&(lpi->ps_wake_lock), 3*HZ);
720 _cm3629_I2C_Read2(lpi->cm3629_slave_address, INT_FLAG, cmd, 2);
721 add = cmd[1];
722
723 if ((add & CM3629_PS1_IF_AWAY) || (add & CM3629_PS1_IF_CLOSE) ||
724 (add & CM3629_PS2_IF_AWAY) || (add & CM3629_PS2_IF_CLOSE)) {
725 inter_error = 0;
726 if ((add & CM3629_PS1_IF_AWAY) || (add & CM3629_PS2_IF_AWAY))
727 report_psensor_input_event(lpi, 1);
728 else
729 report_psensor_input_event(lpi, 2);
730 } else if (((add & CM3629_ALS_IF_L) == CM3629_ALS_IF_L) ||
731 ((add & CM3629_ALS_IF_H) == CM3629_ALS_IF_H)) {
732 inter_error = 0;
733 report_lsensor_input_event(lpi, 0);
734 } else {
735 if (inter_error < 10) {
736 D("[PS][cm3629 warning]%s unkown interrupt: 0x%x!\n",
737 __func__, add);
738 inter_error++ ;
739 } else {
740 pr_err("[PS][cm3629 error]%s error: unkown interrupt: 0x%x!\n",
741 __func__, add);
742 }
743 }
744 enable_irq(lpi->irq);
745}
746
747#ifdef POLLING_PROXIMITY
748static uint8_t mid_value(uint8_t value[], uint8_t size)
749{
750 int i = 0, j = 0;
751 uint16_t temp = 0;
752
753 if (size < 3)
754 return 0;
755
756 for (i = 0; i < (size - 1); i++)
757 for (j = (i + 1); j < size; j++)
758 if (value[i] > value[j]) {
759 temp = value[i];
760 value[i] = value[j];
761 value[j] = temp;
762 }
763 return value[((size - 1) / 2)];
764}
765
766static int get_stable_ps_adc_value(uint8_t *ps_adc1, uint8_t *ps_adc2)
767{
768 int ret = 0;
769 int i = 0;
770 uint8_t mid_adc1 = 0;
771 uint8_t mid_adc2 = 0;
772 uint8_t adc1[3] = {0, 0, 0};
773 uint8_t adc2[3] = {0, 0, 0};
774
775 for (i = 0; i < 3; i++) {
776 ret = get_ps_adc_value(&adc1[i], &adc2[i]);
777 if (ret < 0) {
778 pr_err("[PS_ERR][cm3629 error]%s: get_ps_adc_value\n",
779 __func__);
780 return -EIO;
781 }
782 }
783
784 mid_adc1 = mid_value(adc1, 3);
785 mid_adc2 = mid_value(adc2, 3);
786
787 *ps_adc1 = mid_adc1;
788 *ps_adc2 = mid_adc2;
789
790 return 0;
791}
792
793static void polling_do_work(struct work_struct *w)
794{
795 struct cm3629_info *lpi = lp_info;
796 uint8_t ps_adc1 = 0;
797 uint8_t ps_adc2 = 0;
798 int i = 0;
799 int ret = 0;
800 char cmd[3];
801
802
803 if (lpi->ps_enable == 0)
804 return;
805
806 ret = get_stable_ps_adc_value(&ps_adc1, &ps_adc2);
807
808 if ((ps_adc1 == 0) || (ret < 0)) {
809 queue_delayed_work(lpi->lp_wq, &polling_work,
810 msecs_to_jiffies(POLLING_DELAY));
811 return;
812 }
813
814 for (i = lpi->ps_base_index; i >= 1; i--) {
815 if (ps_adc1 > lpi->mapping_table[i])
816 break;
817 else if ((ps_adc1 > lpi->mapping_table[(i-1)]) &&
818 (ps_adc1 <= lpi->mapping_table[i])) {
819 lpi->ps_base_index = (i-1);
820
821 if (i == (lpi->mapping_size - 1))
822 lpi->ps1_thd_set = 0xFF;
823 else
824 lpi->ps1_thd_set = (lpi->mapping_table[i] +
825 lpi->ps_th_add);
826
827 if (lpi->ps1_thd_set <= ps_adc1)
828 lpi->ps1_thd_set = 0xFF;
829
830
831 cmd[0] = lpi->ps1_thd_set;
832 if (lpi->ps1_thh_diff == 0)
833 cmd[1] = lpi->ps1_thd_set + 1;
834 else
835 cmd[1] = lpi->ps1_thd_set + lpi->ps1_thh_diff;
836
837 if (cmd[1] < cmd[0])
838 cmd[1] = cmd[0];
839
840 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
841 PS_1_thd, cmd, 3);
842 D("[PS][cm3629] SET THD: lpi->ps1_thd_set = %d,"
843 " cmd[0] = 0x%x, cmd[1] = 0x%x\n",
844 lpi->ps1_thd_set, cmd[0], cmd[1]);
845 break;
846 }
847 }
848
849 queue_delayed_work(lpi->lp_wq, &polling_work,
850 msecs_to_jiffies(POLLING_DELAY));
851}
852#endif
853
854static irqreturn_t cm3629_irq_handler(int irq, void *data)
855{
856 struct cm3629_info *lpi = data;
857
858 disable_irq_nosync(lpi->irq);
859 if (enable_cm3629_log)
860 D("[PS][cm3629] %s\n", __func__);
861
862 queue_work(lpi->lp_wq, &sensor_irq_work);
863
864 return IRQ_HANDLED;
865}
866
867static int als_power(int enable)
868{
869 struct cm3629_info *lpi = lp_info;
870
871 if (lpi->power)
872 lpi->power(LS_PWR_ON, enable);
873
874 return 0;
875}
876
877static void ls_initial_cmd(struct cm3629_info *lpi)
878{
879 char cmd[3] = {0};
880
881 cmd[0] = (lpi->ls_cmd | CM3629_ALS_SD);
882 cmd[1] = 0;
883 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
884 ALS_config_cmd, cmd, 3);
885
886 _cm3629_I2C_Read2(lpi->cm3629_slave_address, ALS_config_cmd, cmd, 2);
887 D("[LS][cm3629] %s, cmd[0] = 0x%x, cmd[1] = 0x%x\n", __func__, cmd[0], cmd[1]);
888}
889
890static void psensor_intelligent_cancel_cmd(struct cm3629_info *lpi)
891{
892 char cmd[2] = {0};
893
894 cmd[0] = lpi->inte_ps1_canc;
895 cmd[1] = lpi->inte_ps2_canc;
896 _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_CANC, cmd, 3);
897}
898
899static void psensor_initial_cmd(struct cm3629_info *lpi)
900{
901 char cmd[2] = {0};
902
903 cmd[0] = lpi->ps_conf1_val;
904 cmd[1] = lpi->ps_conf2_val;
905 _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_config, cmd, 3);
906
907 cmd[0] = lpi->ps_conf3_val;
908 cmd[1] = CM3629_PS_255_STEPS;
909 _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_config_ms, cmd, 3);
910
911 cmd[0] = lpi->ps1_thd_set;
912 if (lpi->ps1_thh_diff == 0)
913 cmd[1] = lpi->ps1_thd_set + 1;
914 else
915 cmd[1] = lpi->ps1_thd_set + lpi->ps1_thh_diff;
916 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
917 PS_1_thd, cmd, 3);
918
919 cmd[0] = lpi->ps2_thd_set;
920 cmd[1] = lpi->ps2_thd_set + 1;
921 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
922 PS_2_thd, cmd, 3);
923
924 psensor_intelligent_cancel_cmd(lpi);
925
926 D("[PS][cm3629] %s, finish\n", __func__);
927}
928
929static int psensor_enable(struct cm3629_info *lpi)
930{
931 int ret;
932 char ps_conf[3];
933 char cmd[2];
934#ifdef POLLING_PROXIMITY
935 uint8_t ps_adc1 = 0;
936 uint8_t ps_adc2 = 0;
937#endif
938 mutex_lock(&ps_enable_mutex);
939
940 D("[PS][cm3629] %s +\n", __func__);
941 if (lpi->ps_enable) {
942 D("[PS][cm3629] %s: already enabled %d\n", __func__, lpi->ps_enable);
943 lpi->ps_enable++;
944 mutex_unlock(&ps_enable_mutex);
945 return 0;
946 }
947 sensor_lpm_power(0);
948 blocking_notifier_call_chain(&psensor_notifier_list, 1, NULL);
949 lpi->j_start = jiffies;
950
951
952
953 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, -1);
954 input_sync(lpi->ps_input_dev);
955
956 psensor_initial_cmd(lpi);
957
958 if (lpi->enable_polling_ignore == 1 &&
959 lpi->mfg_mode != NO_IGNORE_BOOT_MODE) {
960
961 input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, 1);
962 input_sync(lpi->ps_input_dev);
963 blocking_notifier_call_chain(&psensor_notifier_list, 1+2, NULL);
964 } else
965 report_psensor_input_event(lpi, 0);
966
967 cmd[0] = lpi->ps_conf1_val | CM3629_PS1_SD | CM3629_PS2_SD;
968 cmd[1] = lpi->ps_conf2_val;
969 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
970 PS_config, cmd, 3);
971
972 psensor_intelligent_cancel_cmd(lpi);
973
974 ps_conf[0] = lpi->ps_conf1_val;
975 ps_conf[1] = lpi->ps_conf2_val;
976 ps_conf[2] = lpi->ps_conf3_val;
977
978 if (lpi->ps_select == CM3629_PS2_ONLY)
979 ps_conf[1] = (lpi->ps_conf2_val | CM3629_PS2_INT_BOTH);
980 else if (lpi->ps_select == CM3629_PS1_ONLY)
981 ps_conf[1] = (lpi->ps_conf2_val | CM3629_PS1_INT_BOTH);
982
983 enable_ps_interrupt(ps_conf);
984
985 ret = irq_set_irq_wake(lpi->irq, 1);
986 if (ret < 0) {
987 pr_err(
988 "[PS][cm3629 error]%s: fail to enable irq %d as wake interrupt\n",
989 __func__, lpi->irq);
990 mutex_unlock(&ps_enable_mutex);
991 return ret;
992 }
993
994#ifdef POLLING_PROXIMITY
995 if (lpi->enable_polling_ignore == 1) {
996 if (lpi->mfg_mode != NO_IGNORE_BOOT_MODE) {
Flemmarde2ce3b72013-04-25 21:59:45 -0700997 ret = get_stable_ps_adc_value(&ps_adc1, &ps_adc2);
998 D("[PS][cm3629] INITIAL ps_adc1 = 0x%02X\n", ps_adc1);
999 if ((ret == 0) && (lpi->mapping_table != NULL) &&
1000 ((ps_adc1 >= lpi->ps1_thd_set - 1)))
1001 queue_delayed_work(lpi->lp_wq, &polling_work,
1002 msecs_to_jiffies(POLLING_DELAY));
1003 }
1004 }
1005#endif
1006 mutex_unlock(&ps_enable_mutex);
1007 D("[PS][cm3629] %s -\n", __func__);
1008 return ret;
1009}
1010
1011static int psensor_disable(struct cm3629_info *lpi)
1012{
1013 int ret = -EIO;
1014 char cmd[2];
1015
1016 mutex_lock(&ps_enable_mutex);
1017
1018 D("[PS][cm3629] %s %d\n", __func__, lpi->ps_enable);
1019 if (lpi->ps_enable != 1) {
1020 if (lpi->ps_enable > 1)
1021 lpi->ps_enable--;
1022 else
1023 D("[PS][cm3629] %s: already disabled\n", __func__);
1024 mutex_unlock(&ps_enable_mutex);
1025 return 0;
1026 }
1027 lpi->ps_conf1_val = lpi->ps_conf1_val_from_board;
1028 lpi->ps_conf2_val = lpi->ps_conf2_val_from_board;
1029 lpi->ps_pocket_mode = 0;
1030
1031 ret = irq_set_irq_wake(lpi->irq, 0);
1032 if (ret < 0) {
1033 pr_err(
1034 "[PS][cm3629 error]%s: fail to disable irq %d as wake interrupt\n",
1035 __func__, lpi->irq);
1036 mutex_unlock(&ps_enable_mutex);
1037 return ret;
1038 }
1039
1040 cmd[0] = lpi->ps_conf1_val | CM3629_PS1_SD | CM3629_PS2_SD;
1041 cmd[1] = lpi->ps_conf2_val;
1042 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1043 PS_config, cmd, 3);
1044 if (ret < 0) {
1045 pr_err("[PS][cm3629 error]%s: disable psensor fail\n", __func__);
1046 mutex_unlock(&ps_enable_mutex);
1047 return ret;
1048 }
1049
1050 cmd[0] = lpi->ps_conf3_val;
1051 cmd[1] = CM3629_PS_255_STEPS;
1052 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1053 PS_config_ms, cmd, 3);
1054
1055 blocking_notifier_call_chain(&psensor_notifier_list, 0, NULL);
1056 lpi->ps_enable = 0;
1057
1058#ifdef POLLING_PROXIMITY
1059 if (lpi->enable_polling_ignore == 1 && lpi->mfg_mode != NO_IGNORE_BOOT_MODE) {
1060 cancel_delayed_work(&polling_work);
1061 lpi->ps_base_index = (lpi->mapping_size - 1);
1062
1063 lpi->ps1_thd_set = lpi->original_ps_thd_set;
1064
1065 cmd[0] = lpi->ps1_thd_set;
1066 if (lpi->ps1_thh_diff == 0)
1067 cmd[1] = lpi->ps1_thd_set + 1;
1068 else
1069 cmd[1] = lpi->ps1_thd_set + lpi->ps1_thh_diff;
1070 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1071 PS_1_thd, cmd, 3);
1072 }
1073#endif
1074 mutex_unlock(&ps_enable_mutex);
1075 D("[PS][cm3629] %s --%d\n", __func__, lpi->ps_enable);
1076 return ret;
1077}
1078
1079static int psensor_open(struct inode *inode, struct file *file)
1080{
1081 struct cm3629_info *lpi = lp_info;
1082
1083 D("[PS][cm3629] %s\n", __func__);
1084
1085 if (lpi->psensor_opened)
1086 return -EBUSY;
1087
1088 lpi->psensor_opened = 1;
1089
1090 return 0;
1091}
1092
1093static int psensor_release(struct inode *inode, struct file *file)
1094{
1095 struct cm3629_info *lpi = lp_info;
1096
1097 D("[PS][cm3629] %s\n", __func__);
1098
1099 lpi->psensor_opened = 0;
1100
1101 return ps_hal_enable ? psensor_disable(lpi) : 0 ;
1102}
1103
1104static long psensor_ioctl(struct file *file, unsigned int cmd,
1105 unsigned long arg)
1106{
1107 int val, err;
1108 struct cm3629_info *lpi = lp_info;
1109
1110 D("[PS][cm3629] %s cmd %d\n", __func__, _IOC_NR(cmd));
1111
1112 switch (cmd) {
1113 case CAPELLA_CM3602_IOCTL_ENABLE:
1114 if (get_user(val, (unsigned long __user *)arg))
1115 return -EFAULT;
1116 if (val) {
1117 err = psensor_enable(lpi);
1118 if (!err)
1119 ps_hal_enable = 1;
1120 return err;
1121 } else {
1122 err = psensor_disable(lpi);
1123 if (!err)
1124 ps_hal_enable = 0;
1125 return err;
1126 }
1127 break;
1128 case CAPELLA_CM3602_IOCTL_GET_ENABLED:
1129 return put_user(lpi->ps_enable, (unsigned long __user *)arg);
1130 break;
1131 default:
1132 pr_err("[PS][cm3629 error]%s: invalid cmd %d\n",
1133 __func__, _IOC_NR(cmd));
1134 return -EINVAL;
1135 }
1136}
1137
1138static const struct file_operations psensor_fops = {
1139 .owner = THIS_MODULE,
1140 .open = psensor_open,
1141 .release = psensor_release,
1142 .unlocked_ioctl = psensor_ioctl
1143};
1144
1145static struct miscdevice psensor_misc = {
1146 .minor = MISC_DYNAMIC_MINOR,
1147 .name = "cm3602",
1148 .fops = &psensor_fops
1149};
1150
1151static void lightsensor_set_kvalue(struct cm3629_info *lpi)
1152{
1153 if (!lpi) {
1154 pr_err("[LS][cm3629 error]%s: ls_info is empty\n", __func__);
1155 return;
1156 }
1157
1158 D("[LS][cm3629] %s: ALS calibrated als_kadc=0x%x\n",
1159 __func__, als_kadc);
1160
1161 if (als_kadc >> 16 == ALS_CALIBRATED)
1162 lpi->als_kadc = als_kadc & 0xFFFF;
1163 else {
1164 lpi->als_kadc = 0;
1165 D("[LS][cm3629] %s: no ALS calibrated\n", __func__);
1166 }
1167
1168 if (lpi->als_kadc && lpi->golden_adc > 0) {
1169 lpi->als_kadc = (lpi->als_kadc > 0) ?
1170 lpi->als_kadc : lpi->golden_adc;
1171 lpi->als_gadc = lpi->golden_adc;
1172 } else {
1173 lpi->als_kadc = 1;
1174 lpi->als_gadc = 1;
1175 }
1176 D("[LS][cm3629] %s: als_kadc=0x%x, als_gadc=0x%x\n",
1177 __func__, lpi->als_kadc, lpi->als_gadc);
1178}
1179
1180static void psensor_set_kvalue(struct cm3629_info *lpi)
1181{
1182 uint8_t ps_conf1_val;
1183
1184 D("[PS][cm3629] %s: PS calibrated ps_kparam1 = 0x%04X, "
1185 "ps_kparam2 = 0x%04X\n", __func__, ps_kparam1, ps_kparam2);
1186 ps_conf1_val = lpi->ps_conf1_val;
1187
1188 if (ps_kparam1 >> 16 == PS_CALIBRATED) {
1189 lpi->inte_ps1_canc = (uint8_t) (ps_kparam2 & 0xFF);
1190 lpi->inte_ps2_canc = (uint8_t) ((ps_kparam2 >> 8) & 0xFF);
1191 if (lpi->ps_calibration_rule == 3) {
1192
1193 if ((lpi->ps_conf1_val & CM3629_PS_IT_1_6T) == CM3629_PS_IT_1_6T) {
1194 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_6T\n",
1195 __func__, lpi->ps_conf1_val);
1196 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_6T));
1197 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1_3T) == CM3629_PS_IT_1_3T) {
1198 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_3T\n",
1199 __func__, lpi->ps_conf1_val);
1200 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_3T));
1201 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1T) == CM3629_PS_IT_1T) {
1202 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1T\n",
1203 __func__, lpi->ps_conf1_val);
1204 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1T));
1205 } else
1206 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, 2T or unknown\n",
1207 __func__, lpi->ps_conf1_val);
1208
1209 D("[PS][cm3629] %s: clear lpi->ps_conf1_val 0x%x\n",
1210 __func__, lpi->ps_conf1_val);
1211
1212 if (((ps_kparam2 >> 16) & 0xFF) == 0)
1213 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1T);
1214 else if (((ps_kparam2 >> 16) & 0xFF) == 0x1)
1215 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1_3T);
1216 else if (((ps_kparam2 >> 16) & 0xFF) == 0x2)
1217 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1_6T);
1218 else {
1219 lpi->ps_conf1_val = ps_conf1_val;
1220 D("[PS]%s: ((ps_kparam2 >> 16) & 0xFF) = 0x%X is strange\n",
1221 __func__, ((ps_kparam2 >> 16) & 0xFF));
1222 }
1223 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x\n",
1224 __func__, lpi->ps_conf1_val);
1225 }
1226 D("[PS][cm3629] %s: PS calibrated inte_ps1_canc = 0x%02X, "
1227 "inte_ps2_canc = 0x%02X, ((ps_kparam2 >> 16) & 0xFF) = 0x%X\n", __func__,
1228 lpi->inte_ps1_canc, lpi->inte_ps2_canc, ((ps_kparam2 >> 16) & 0xFF));
1229 } else {
1230 if (lpi->ps_calibration_rule >= 1) {
1231 lpi->ps1_thd_set = lpi->ps1_thd_no_cal;
1232 lpi->ps2_thd_set = lpi->ps2_thd_no_cal;
1233 D("[PS][cm3629] %s: PS1_THD=%d, PS2_THD=%d, "
1234 "no calibration\n", __func__,
1235 lpi->ps1_thd_set, lpi->ps2_thd_set);
1236 }
1237 D("[PS][cm3629] %s: Proximity NOT calibrated\n", __func__);
1238 }
1239
1240}
1241
1242static int lightsensor_update_table(struct cm3629_info *lpi)
1243{
1244 uint16_t data[10];
1245 int i;
1246 for (i = 0; i < 10; i++) {
1247 if (*(lpi->adc_table + i) < 0xFFFF) {
1248 data[i] = *(lpi->adc_table + i)
1249 * lpi->als_kadc / lpi->als_gadc;
1250 } else {
1251 data[i] = *(lpi->adc_table + i);
1252 }
1253 D("[LS][cm3629] %s: Calibrated adc_table: data[%d], %x\n",
1254 __func__, i, data[i]);
1255 }
1256 memcpy(lpi->cali_table, data, 20);
1257 return 0;
1258}
1259
1260static int lightsensor_enable(struct cm3629_info *lpi)
1261{
1262 int ret = 0;
1263 char cmd[3] = {0};
1264
1265 mutex_lock(&als_enable_mutex);
1266 sensor_lpm_power(0);
1267 D("[LS][cm3629] %s\n", __func__);
1268
1269 if (sensor_chipId[0] != 0x29)
1270 cmd[0] = (CM3629_ALS_IT_80ms | CM3629_ALS_PERS_1);
1271 else
1272 cmd[0] = (CM3629_ALS_IT_50ms | CM3629_ALS_PERS_1);
1273
1274 cmd[1] = 0;
1275 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1276 ALS_config_cmd, cmd, 3);
1277 if (ret < 0)
1278 pr_err(
1279 "[LS][cm3629 error]%s: set auto light sensor fail\n",
1280 __func__);
1281 else {
Flemmarde2ce3b72013-04-25 21:59:45 -07001282 input_report_abs(lpi->ls_input_dev, ABS_MISC, -1);
1283 input_sync(lpi->ls_input_dev);
1284 report_lsensor_input_event(lpi, 1);
1285 }
1286
1287 mutex_unlock(&als_enable_mutex);
1288 return ret;
1289}
1290
1291static int lightsensor_disable(struct cm3629_info *lpi)
1292{
1293 int ret = 0;
1294 char cmd[3] = {0};
1295 mutex_lock(&als_disable_mutex);
1296
1297 D("[LS][cm3629] %s\n", __func__);
1298
1299 cmd[0] = lpi->ls_cmd | CM3629_ALS_SD;
1300 cmd[1] = 0;
1301 ret = _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1302 ALS_config_cmd, cmd, 3);
1303
1304 if (ret < 0)
1305 pr_err("[LS][cm3629 error]%s: disable auto light sensor fail\n",
1306 __func__);
1307 else
1308 lpi->als_enable = 0;
1309
1310 mutex_unlock(&als_disable_mutex);
1311 return ret;
1312}
1313
1314static int lightsensor_open(struct inode *inode, struct file *file)
1315{
1316 struct cm3629_info *lpi = lp_info;
1317 int rc = 0;
1318
1319 D("[LS][cm3629] %s\n", __func__);
1320 if (lpi->lightsensor_opened) {
1321 pr_err("[LS][cm3629 error]%s: already opened\n", __func__);
1322 rc = -EBUSY;
1323 }
1324 lpi->lightsensor_opened = 1;
1325 return rc;
1326}
1327
1328static int lightsensor_release(struct inode *inode, struct file *file)
1329{
1330 struct cm3629_info *lpi = lp_info;
1331
1332 D("[LS][cm3629] %s\n", __func__);
1333 lpi->lightsensor_opened = 0;
1334 return 0;
1335}
1336
1337static long lightsensor_ioctl(struct file *file, unsigned int cmd,
1338 unsigned long arg)
1339{
1340 int rc, val;
1341 struct cm3629_info *lpi = lp_info;
1342
1343
1344
1345 switch (cmd) {
1346 case LIGHTSENSOR_IOCTL_ENABLE:
1347 if (get_user(val, (unsigned long __user *)arg)) {
1348 rc = -EFAULT;
1349 break;
1350 }
1351 D("[LS][cm3629] %s LIGHTSENSOR_IOCTL_ENABLE, value = %d\n",
1352 __func__, val);
1353 rc = val ? lightsensor_enable(lpi) : lightsensor_disable(lpi);
1354 break;
1355 case LIGHTSENSOR_IOCTL_GET_ENABLED:
1356 val = lpi->als_enable;
1357 D("[LS][cm3629] %s LIGHTSENSOR_IOCTL_GET_ENABLED, enabled %d\n",
1358 __func__, val);
1359 rc = put_user(val, (unsigned long __user *)arg);
1360 break;
1361 default:
1362 pr_err("[LS][cm3629 error]%s: invalid cmd %d\n",
1363 __func__, _IOC_NR(cmd));
1364 rc = -EINVAL;
1365 }
1366
1367 return rc;
1368}
1369
1370static const struct file_operations lightsensor_fops = {
1371 .owner = THIS_MODULE,
1372 .open = lightsensor_open,
1373 .release = lightsensor_release,
1374 .unlocked_ioctl = lightsensor_ioctl
1375};
1376
1377static struct miscdevice lightsensor_misc = {
1378 .minor = MISC_DYNAMIC_MINOR,
1379 .name = "lightsensor",
1380 .fops = &lightsensor_fops
1381};
1382
1383
1384static ssize_t ps_adc_show(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387
1388 uint8_t ps_adc1 = 0;
1389 uint8_t ps_adc2 = 0;
1390 int ret;
1391 struct cm3629_info *lpi = lp_info;
1392 int int_gpio;
1393
1394 int_gpio = gpio_get_value_cansleep(lpi->intr_pin);
1395 get_ps_adc_value(&ps_adc1, &ps_adc2);
1396
1397 if (lpi->ps_calibration_rule == 1) {
1398 D("[PS][cm3629] %s: PS1_ADC=0x%02X, PS2_ADC=0x%02X, "
1399 "PS1_Offset=0x%02X, PS2_Offset=0x%02X\n", __func__,
1400 ps_adc1, ps_adc2, lpi->ps1_adc_offset, lpi->ps2_adc_offset);
1401 ps_adc1 = (ps_adc1 >= lpi->ps1_adc_offset) ?
1402 ps_adc1 - lpi->ps1_adc_offset : 0;
1403 ps_adc2 = (ps_adc2 >= lpi->ps2_adc_offset) ?
1404 ps_adc2 - lpi->ps2_adc_offset : 0;
1405 }
1406
1407 ret = sprintf(buf, "ADC[0x%02X], ENABLE = %d, intr_pin = %d, "
1408 "ps_pocket_mode = %d, model = %s, ADC2[0x%02X]\n",
1409 ps_adc1, lpi->ps_enable, int_gpio, lpi->ps_pocket_mode,
1410 (lpi->model == CAPELLA_CM36282) ? "CM36282" : "CM36292",
1411 ps_adc2);
1412
1413 return ret;
1414}
1415
1416static ssize_t ps_enable_store(struct device *dev,
1417 struct device_attribute *attr,
1418 const char *buf, size_t count)
1419{
1420 int ps_en, err;
1421 struct cm3629_info *lpi = lp_info;
1422
1423 ps_en = -1;
1424 sscanf(buf, "%d", &ps_en);
1425
1426 if (ps_en != 0 && ps_en != 1
1427 && ps_en != 10 && ps_en != 13 && ps_en != 16)
1428 return -EINVAL;
1429
1430 if (lpi->ps_calibration_rule == 3 &&
1431 (ps_en == 10 || ps_en == 13 || ps_en == 16)) {
1432 if ((lpi->ps_conf1_val & CM3629_PS_IT_1_6T) == CM3629_PS_IT_1_6T) {
1433 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_6T\n",
1434 __func__, lpi->ps_conf1_val);
1435 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_6T));
1436 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1_3T) == CM3629_PS_IT_1_3T) {
1437 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_3T\n",
1438 __func__, lpi->ps_conf1_val);
1439 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_3T));
1440 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1T) == CM3629_PS_IT_1T) {
1441 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1T\n",
1442 __func__, lpi->ps_conf1_val);
1443 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1T));
1444 } else
1445 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, 2T or unknown\n",
1446 __func__, lpi->ps_conf1_val);
1447
1448 D("[PS][cm3629] %s: clear lpi->ps_conf1_val 0x%x\n",
1449 __func__, lpi->ps_conf1_val);
1450
1451 if (ps_en == 10)
1452 lpi->ps_conf1_val = lpi->ps_conf1_val | CM3629_PS_IT_1T;
1453 else if (ps_en == 13)
1454 lpi->ps_conf1_val = lpi->ps_conf1_val | CM3629_PS_IT_1_3T;
1455 else if (ps_en == 16)
1456 lpi->ps_conf1_val = lpi->ps_conf1_val | CM3629_PS_IT_1_6T;
1457
1458 D("[PS][cm3629] %s: change lpi->ps_conf1_val 0x%x\n",
1459 __func__, lpi->ps_conf1_val);
1460 }
1461 D("[PS][cm3629] %s: ps_en=%d\n",
1462 __func__, ps_en);
1463
1464 if (ps_en && !ps_drv_enable) {
1465 err = psensor_enable(lpi);
1466 if (!err)
1467 ps_drv_enable = 1;
1468 } else if (!ps_en && ps_drv_enable) {
1469 ps_drv_enable = 0;
1470 psensor_disable(lpi);
1471 }
1472
1473 return count;
1474}
1475
1476static DEVICE_ATTR(ps_adc, 0664, ps_adc_show, ps_enable_store);
1477static int kcalibrated;
1478static ssize_t ps_kadc_show(struct device *dev,
1479 struct device_attribute *attr, char *buf)
1480{
1481 int ret = 0;
1482 struct cm3629_info *lpi = lp_info;
1483
1484 if ((ps_kparam1 >> 16 == PS_CALIBRATED) || kcalibrated == 1)
1485 ret = sprintf(buf, "P-sensor calibrated,"
1486 "INTE_PS1_CANC = (0x%02X), "
1487 "INTE_PS2_CANC = (0x%02X)\n",
1488 lpi->inte_ps1_canc, lpi->inte_ps2_canc);
1489 else
1490 ret = sprintf(buf, "P-sensor NOT calibrated,"
1491 "INTE_PS1_CANC = (0x%02X), "
1492 "INTE_PS2_CANC = (0x%02X)\n",
1493 lpi->inte_ps1_canc, lpi->inte_ps2_canc);
1494
1495 return ret;
1496}
1497
1498static ssize_t ps_kadc_store(struct device *dev,
1499 struct device_attribute *attr,
1500 const char *buf, size_t count)
1501{
1502 int param1, param2;
1503 char ps_conf[3];
1504 struct cm3629_info *lpi = lp_info;
1505 uint8_t ps_conf1_val;
1506
1507 sscanf(buf, "0x%x 0x%x", &param1, &param2);
1508 D("[PS]%s: store value = 0x%X, 0x%X\n", __func__, param1, param2);
1509 ps_conf1_val = lpi->ps_conf1_val;
1510 if (lpi->ps_calibration_rule == 3) {
1511
1512 if ((lpi->ps_conf1_val & CM3629_PS_IT_1_6T) == CM3629_PS_IT_1_6T) {
1513 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_6T\n",
1514 __func__, lpi->ps_conf1_val);
1515 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_6T));
1516 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1_3T) == CM3629_PS_IT_1_3T) {
1517 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1_3T\n",
1518 __func__, lpi->ps_conf1_val);
1519 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1_3T));
1520 } else if ((lpi->ps_conf1_val & CM3629_PS_IT_1T) == CM3629_PS_IT_1T) {
1521 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, CM3629_PS_IT_1T\n",
1522 __func__, lpi->ps_conf1_val);
1523 lpi->ps_conf1_val = (lpi->ps_conf1_val & (~CM3629_PS_IT_1T));
1524 } else
1525 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x, 2T or unknown\n",
1526 __func__, lpi->ps_conf1_val);
1527
1528 D("[PS][cm3629] %s: clear lpi->ps_conf1_val 0x%x\n",
1529 __func__, lpi->ps_conf1_val);
1530
1531 if (((param2 >> 16) & 0xFF) == 0)
1532 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1T);
1533 else if (((param2 >> 16) & 0xFF) == 0x1)
1534 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1_3T);
1535 else if (((param2 >> 16) & 0xFF) == 0x2)
1536 lpi->ps_conf1_val = (lpi->ps_conf1_val | CM3629_PS_IT_1_6T);
1537 else {
1538 lpi->ps_conf1_val = ps_conf1_val;
1539 D("[PS]%s: ((param2 >> 16) & 0xFF) = 0x%X is diffrent\n",
1540 __func__, ((param2 >> 16) & 0xFF));
1541 }
1542 D("[PS]%s: ((param2 >> 16) & 0xFF) = 0x%X\n",
1543 __func__, ((param2 >> 16) & 0xFF));
1544 D("[PS][cm3629] %s: lpi->ps_conf1_val 0x%x\n",
1545 __func__, lpi->ps_conf1_val);
1546 }
1547 if (lpi->ps_calibration_rule >= 1) {
1548 lpi->ps1_thd_set = lpi->ps1_thd_with_cal;
1549 lpi->ps2_thd_set = lpi->ps2_thd_with_cal;
1550 D("[PS][cm3629] %s: PS1_THD=%d, PS2_THD=%d, "
1551 "after calibration\n", __func__,
1552 lpi->ps1_thd_set, lpi->ps2_thd_set);
1553 }
1554 if (lpi->ps_enable) {
1555 ps_conf[0] = lpi->ps_conf1_val;
1556 ps_conf[1] = lpi->ps_conf2_val;
1557 ps_conf[2] = lpi->ps_conf3_val;
1558
1559 if (lpi->ps_select == CM3629_PS2_ONLY)
1560 ps_conf[1] = (lpi->ps_conf2_val | CM3629_PS2_INT_BOTH);
1561 else if (lpi->ps_select == CM3629_PS1_ONLY)
1562 ps_conf[1] = (lpi->ps_conf2_val | CM3629_PS1_INT_BOTH);
1563
1564 enable_ps_interrupt(ps_conf);
1565 }
1566
1567 ps1_canc_set = lpi->inte_ps1_canc = (param2 & 0xFF);
1568 ps2_canc_set = lpi->inte_ps2_canc = ((param2 >> 8) & 0xFF);
1569 psensor_intelligent_cancel_cmd(lpi);
1570
1571 D("[PS]%s: inte_ps1_canc = 0x%02X, inte_ps2_canc = 0x%02X, lpi->ps_conf1_val = 0x%02X\n",
1572 __func__, lpi->inte_ps1_canc, lpi->inte_ps2_canc, lpi->ps_conf1_val);
1573 kcalibrated = 1;
1574 return count;
1575}
1576
1577static DEVICE_ATTR(ps_kadc, 0664, ps_kadc_show, ps_kadc_store);
1578
1579
1580static ssize_t ps_canc_show(struct device *dev,
1581 struct device_attribute *attr, char *buf)
1582{
1583 int ret = 0;
1584 struct cm3629_info *lpi = lp_info;
1585
1586 ret = sprintf(buf, "PS1_CANC = 0x%02X, PS2_CANC = 0x%02X\n",
1587 lpi->inte_ps1_canc, lpi->inte_ps2_canc);
1588
1589 return ret;
1590}
1591static ssize_t ps_canc_store(struct device *dev,
1592 struct device_attribute *attr,
1593 const char *buf, size_t count)
1594{
1595 int ps1_canc = 0;
1596 int ps2_canc = 0;
1597 struct cm3629_info *lpi = lp_info;
1598
1599 sscanf(buf, "0x%x 0x%x", &ps1_canc, &ps2_canc);
1600
1601 lpi->inte_ps1_canc = (uint8_t) ps1_canc;
1602 lpi->inte_ps2_canc = (uint8_t) ps2_canc;
1603 psensor_intelligent_cancel_cmd(lpi);
1604
1605 D("[PS] %s: PS1_CANC = 0x%02X, PS2_CANC = 0x%02X\n",
1606 __func__, lpi->inte_ps1_canc, lpi->inte_ps2_canc);
1607
1608 return count;
1609}
1610static DEVICE_ATTR(ps_canc, 0664, ps_canc_show, ps_canc_store);
1611
1612static ssize_t ps_i2c_show(struct device *dev,
1613 struct device_attribute *attr, char *buf)
1614{
1615 int ret = 0, i;
1616 struct cm3629_info *lpi = lp_info;
1617 uint8_t chip_id[3] = {0};
1618 uint8_t data[26] = {0};
1619
1620 for (i = 0; i <= CH_ID; i++) {
1621 _cm3629_I2C_Read2(lpi->cm3629_slave_address, ALS_config_cmd + i,
1622 chip_id, 2);
1623 data[i*2] = chip_id[0];
1624 data[(i*2)+1] = chip_id[1];
1625 }
1626 ret = sprintf(buf,
1627 "0x0L=0x%02X, 0x0H=0x%02X, 0x1L=0x%02X, 0x1H=0x%02X, "
1628 "0x2L=0x%02X, 0x2H=0x%02X, 0x3L=0x%02X, 0x3H=0x%02X,\n"
1629 "0x4L=0x%02X, 0x4H=0x%02X, 0x5L=0x%02X, 0x5H=0x%02X, "
1630 "0x6L=0x%02X, 0x6H=0x%02X, 0x7L=0x%02X, 0x7H=0x%02X,\n"
1631 "0x8L=0x%02X, 0x8H=0x%02X, 0x9L=0x%02X, 0x9H=0x%02X, "
1632 "0xaL=0x%02X, 0xaH=0x%02X, 0xbL=0x%02X, 0xbH=0x%02X,\n"
1633 "0xcL=0x%02X, 0xcH=0x%02X.\n",
1634 data[0], data[1], data[2], data[3],
1635 data[4], data[5], data[6], data[7],
1636 data[8], data[9], data[10], data[11],
1637 data[12], data[13], data[14], data[15],
1638 data[16], data[17], data[18], data[19],
1639 data[20], data[21], data[22], data[23],
1640 data[24], data[25]);
1641
1642 return ret;
1643}
1644static ssize_t ps_i2c_store(struct device *dev,
1645 struct device_attribute *attr,
1646 const char *buf, size_t count)
1647{
1648 struct cm3629_info *lpi = lp_info;
1649 char *token[10];
1650 int i, ret = 0;
1651 uint8_t reg = 0, value[3] = {0}, read_value[3] = {0};
1652 unsigned long ul_reg = 0, ul_value[3] = {0};
1653
1654 printk(KERN_INFO "[CM3629_] %s\n", buf);
1655
1656 for (i = 0; i < 3; i++) {
1657 token[i] = strsep((char **)&buf, " ");
1658 D("%s: token[%d] = %s\n", __func__, i, token[i]);
1659 }
1660
1661 ret = strict_strtoul(token[0], 16, &ul_reg);
1662 ret = strict_strtoul(token[1], 16, &(ul_value[0]));
1663 ret = strict_strtoul(token[2], 16, &(ul_value[1]));
1664
1665 reg = ul_reg;
1666 value[0] = ul_value[0];
1667 value[1] = ul_value[1];
1668
1669 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1670 reg, value, 3);
1671
1672 D("[CM3629] Set REG=0x%x, value[0]=0x%x, value[1]=0x%x\n",
1673 reg, value[0], value[1]);
1674
1675 _cm3629_I2C_Read2(lpi->cm3629_slave_address, reg, read_value, 2);
1676
1677 D("[CM3629] Get REG=0x%x, value[0]=0x%x, value[1]=0x%x\n",
1678 reg, read_value[0], read_value[1]);
1679
1680 switch (reg) {
1681 case PS_1_thd:
1682 lpi->ps1_thd_set = value[0];
1683 case PS_2_thd:
1684 lpi->ps2_thd_set = value[0];
1685 default:
1686 D("[CM3629] NO parameter update for register 0x%02X\n", reg);
1687 }
1688
1689 return count;
1690}
1691static DEVICE_ATTR(ps_i2c, 0664, ps_i2c_show, ps_i2c_store);
1692
1693static ssize_t ps_hw_show(struct device *dev,
1694 struct device_attribute *attr, char *buf)
1695{
1696 int ret = 0;
1697 struct cm3629_info *lpi = lp_info;
1698
1699 ret = sprintf(buf, "PS: ps_conf1_val=0x%x, ps1_thd_set=0x%x, "
1700 "inte_ps1_canc=0x%02X, inte_ps2_canc=0x%02X, "
1701 "ps_conf2_val=0x%x, ps1_adc_offset=0x%02X, "
1702 "ps2_adc_offset=0x%02X, LS: ls_cmd=0x%x\n",
1703 lpi->ps_conf1_val, lpi->ps1_thd_set, lpi->inte_ps1_canc,
1704 lpi->inte_ps2_canc, lpi->ps_conf2_val,
1705 lpi->ps1_adc_offset, lpi->ps2_adc_offset, lpi->ls_cmd);
1706
1707 return ret;
1708}
1709static ssize_t ps_hw_store(struct device *dev,
1710 struct device_attribute *attr,
1711 const char *buf, size_t count)
1712{
1713 int code;
1714 struct cm3629_info *lpi = lp_info;
1715
1716 sscanf(buf, "0x%x", &code);
1717
1718 D("[PS]%s: store value = 0x%x\n", __func__, code);
1719 if (code == 1) {
1720 lpi->inte_ps1_canc = 0;
1721 lpi->inte_ps2_canc = 0;
1722 lpi->ps1_adc_offset = 0;
1723 lpi->ps2_adc_offset = 0;
1724 psensor_intelligent_cancel_cmd(lpi);
1725 D("[PS]%s: Reset ps1_canc=%d, ps2_canc=%d, adc_offset1=%d, "
1726 "adc_offset2=%d\n", __func__, lpi->inte_ps1_canc,
1727 lpi->inte_ps2_canc, lpi->ps1_adc_offset, lpi->ps2_adc_offset);
1728 } else {
1729 lpi->inte_ps1_canc = ps1_canc_set;
1730 lpi->inte_ps2_canc = ps2_canc_set;
1731 lpi->ps1_adc_offset = ps1_offset_adc;
1732 lpi->ps2_adc_offset = ps2_offset_adc;
1733 psensor_intelligent_cancel_cmd(lpi);
1734 D("[PS]%s: Recover ps1_canc=%d, ps2_canc=%d, adc_offset1=%d, "
1735 "adc_offset2=%d\n", __func__, lpi->inte_ps1_canc,
1736 lpi->inte_ps2_canc, lpi->ps1_adc_offset, lpi->ps2_adc_offset);
1737 }
1738
1739 return count;
1740}
1741static DEVICE_ATTR(ps_hw, 0664, ps_hw_show, ps_hw_store);
1742
1743static ssize_t ps_headset_bt_plugin_show(struct device *dev,
1744 struct device_attribute *attr, char *buf)
1745{
1746 int ret = 0;
1747 struct cm3629_info *lpi = lp_info;
1748
1749 ret = sprintf(buf, "ps_conf1_val = 0x%02X, ps_conf2_val = 0x%02X\n",
1750 lpi->ps_conf1_val, lpi->ps_conf2_val);
1751
1752 return ret;
1753}
1754static ssize_t ps_headset_bt_plugin_store(struct device *dev,
1755 struct device_attribute *attr,
1756 const char *buf, size_t count)
1757{
1758 int headset_bt_plugin = 0;
1759 struct cm3629_info *lpi = lp_info;
1760 char cmd[2] = {0};
1761
1762 sscanf(buf, "%d", &headset_bt_plugin);
1763 D("[PS] %s: headset_bt_plugin = %d\n", __func__, headset_bt_plugin);
1764
1765 if (lpi->no_need_change_setting == 1) {
1766 D("[PS] %s: no_need_change_setting = 0x%x.\n", __func__, lpi->no_need_change_setting);
1767 return count;
1768 } else {
1769 if (headset_bt_plugin == 1) {
1770 D("[PS][cm3629] %s, Headset or BT or Speaker ON\n", __func__);
1771
1772 _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_config, cmd, 2);
1773 D("[PS][cm3629] %s, read value => cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1774 __func__, cmd[0], cmd[1]);
1775
1776 D("[PS][cm3629] %s, Before setting: ps_conf1_val = 0x%x\n",
1777 __func__, lpi->ps_conf1_val);
1778 lpi->ps_conf1_val = (cmd[0] & 0x3) | (CM3629_PS_DR_1_320 |
1779 CM3629_PS_IT_1_6T |
1780 CM3629_PS1_PERS_1);
1781 D("[PS][cm3629] %s, After setting: ps_conf1_val = 0x%x\n",
1782 __func__, lpi->ps_conf1_val);
1783
1784 D("[PS][cm3629] %s, Before setting: ps_conf2_val = 0x%x\n",
1785 __func__, lpi->ps_conf2_val);
1786 lpi->ps_conf2_val = (cmd[1] & 0xF) | (CM3629_PS_ITB_1 |
1787 CM3629_PS_ITR_1);
1788 D("[PS][cm3629] %s, After setting: ps_conf2_val = 0x%x\n",
1789 __func__, lpi->ps_conf2_val);
1790
1791 cmd[0] = lpi->ps_conf1_val;
1792 cmd[1] = lpi->ps_conf2_val;
1793 D("[PS][cm3629] %s, write cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1794 __func__, cmd[0], cmd[1]);
1795 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1796 PS_config, cmd, 3);
1797
1798 _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_config, cmd, 2);
1799 D("[PS][cm3629] %s, read 0x3 cmd value after set =>"
1800 " cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1801 __func__, cmd[0], cmd[1]);
1802 } else {
1803 D("[PS][cm3629] %s, Headset or BT or Speaker OFF\n", __func__);
1804
1805 _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_config, cmd, 2);
1806 D("[PS][cm3629] %s, read value => cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1807 __func__, cmd[0], cmd[1]);
1808
1809 lpi->ps_conf1_val = lpi->ps_conf1_val_from_board;
1810 lpi->ps_conf2_val = lpi->ps_conf2_val_from_board;
1811
1812 cmd[0] = ((cmd[0] & 0x3) | lpi->ps_conf1_val);
1813 cmd[1] = ((cmd[1] & 0xF) | lpi->ps_conf2_val);
1814 D("[PS][cm3629] %s, write cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1815 __func__, cmd[0], cmd[1]);
1816 _cm3629_I2C_Write2(lpi->cm3629_slave_address,
1817 PS_config, cmd, 3);
1818
1819 _cm3629_I2C_Read2(lpi->cm3629_slave_address, PS_config, cmd, 2);
1820 D("[PS][cm3629] %s, read 0x3 cmd value after set =>"
1821 " cmd[0] = 0x%x, cmd[1] = 0x%x\n",
1822 __func__, cmd[0], cmd[1]);
1823 }
1824
1825 }
1826
1827 return count;
1828}
1829static DEVICE_ATTR(ps_headset_bt_plugin, 0664, ps_headset_bt_plugin_show, ps_headset_bt_plugin_store);
1830
1831static ssize_t ls_adc_show(struct device *dev,
1832 struct device_attribute *attr, char *buf)
1833{
1834 int ret;
1835 struct cm3629_info *lpi = lp_info;
1836
1837
1838 report_lsensor_input_event(lpi, 0);
1839
1840 D("[LS][cm3629] %s: ADC = 0x%04X, Level = %d \n",
1841 __func__, lpi->current_adc, lpi->current_level);
1842 ret = sprintf(buf, "ADC[0x%04X] => level %d\n",
1843 lpi->current_adc, lpi->current_level);
1844
1845 return ret;
1846}
1847
1848static DEVICE_ATTR(ls_adc, 0664, ls_adc_show, NULL);
1849
1850static ssize_t ls_enable_show(struct device *dev,
1851 struct device_attribute *attr, char *buf)
1852{
1853
1854 int ret = 0;
1855 struct cm3629_info *lpi = lp_info;
1856
1857 ret = sprintf(buf, "Light sensor Auto Enable = %d\n",
1858 lpi->als_enable);
1859
1860 return ret;
1861}
1862
1863static ssize_t ls_enable_store(struct device *dev,
1864 struct device_attribute *attr,
1865 const char *buf, size_t count)
1866{
1867 int ret = 0;
1868 int ls_auto;
1869 struct cm3629_info *lpi = lp_info;
1870
1871 ls_auto = -1;
1872 sscanf(buf, "%d", &ls_auto);
1873
1874 if (ls_auto != 0 && ls_auto != 1 && ls_auto != 147)
1875 return -EINVAL;
1876
1877 if (ls_auto) {
1878 lpi->ls_calibrate = (ls_auto == 147) ? 1 : 0;
1879 ret = lightsensor_enable(lpi);
1880 } else {
1881 lpi->ls_calibrate = 0;
1882 ret = lightsensor_disable(lpi);
1883 }
1884
1885 D("[LS][cm3629] %s: lpi->als_enable = %d, lpi->ls_calibrate = %d, ls_auto=%d\n",
1886 __func__, lpi->als_enable, lpi->ls_calibrate, ls_auto);
1887
1888 if (ret < 0)
1889 pr_err(
1890 "[LS][cm3629 error]%s: set auto light sensor fail\n",
1891 __func__);
1892
1893 return count;
1894}
1895
1896static DEVICE_ATTR(ls_auto, 0664,
1897 ls_enable_show, ls_enable_store);
1898
1899static ssize_t ls_kadc_show(struct device *dev,
1900 struct device_attribute *attr, char *buf)
1901{
1902 struct cm3629_info *lpi = lp_info;
1903 int ret;
1904
1905 ret = sprintf(buf, "kadc = 0x%x, gadc = 0x%x, kadc while this boot"
1906 " = 0x%x\n",
1907 lpi->als_kadc, lpi->als_gadc, als_kadc);
1908
1909 return ret;
1910}
1911
1912static ssize_t ls_kadc_store(struct device *dev,
1913 struct device_attribute *attr,
1914 const char *buf, size_t count)
1915{
1916 struct cm3629_info *lpi = lp_info;
1917 int kadc_temp = 0;
1918
1919 sscanf(buf, "%d", &kadc_temp);
1920 if (kadc_temp <= 0 || lpi->golden_adc <= 0) {
1921 printk(KERN_ERR "[LS][cm3629 error] %s: kadc_temp=0x%x, als_gadc=0x%x\n",
1922 __func__, kadc_temp, lpi->golden_adc);
1923 return -EINVAL;
1924 }
1925 mutex_lock(&als_get_adc_mutex);
1926 lpi->als_kadc = kadc_temp;
1927 lpi->als_gadc = lpi->golden_adc;
1928 printk(KERN_INFO "[LS]%s: als_kadc=0x%x, als_gadc=0x%x\n",
1929 __func__, lpi->als_kadc, lpi->als_gadc);
1930
1931 if (lightsensor_update_table(lpi) < 0)
1932 printk(KERN_ERR "[LS][cm3629 error] %s: update ls table fail\n", __func__);
1933 mutex_unlock(&als_get_adc_mutex);
1934 return count;
1935}
1936
1937static DEVICE_ATTR(ls_kadc, 0664, ls_kadc_show, ls_kadc_store);
1938
1939static ssize_t ls_adc_table_show(struct device *dev,
1940 struct device_attribute *attr, char *buf)
1941{
1942 unsigned length = 0;
1943 int i;
1944
1945 for (i = 0; i < 10; i++) {
1946 length += sprintf(buf + length,
1947 "[cm3629]Get adc_table[%d] = 0x%x ; %d, Get cali_table[%d] = 0x%x ; %d, \n",
1948 i, *(lp_info->adc_table + i),
1949 *(lp_info->adc_table + i),
1950 i, *(lp_info->cali_table + i),
1951 *(lp_info->cali_table + i));
1952 }
1953 return length;
1954}
1955
1956static ssize_t ls_adc_table_store(struct device *dev,
1957 struct device_attribute *attr,
1958 const char *buf, size_t count)
1959{
1960
1961 struct cm3629_info *lpi = lp_info;
1962 char *token[10];
1963 unsigned long tempdata[10];
1964 int i, ret;
1965
1966 printk(KERN_INFO "[LS][cm3629]%s\n", buf);
1967 for (i = 0; i < 10; i++) {
1968 token[i] = strsep((char **)&buf, " ");
1969 ret = strict_strtoul(token[i], 16, &(tempdata[i]));
1970 if (tempdata[i] < 1 || tempdata[i] > 0xffff) {
1971 printk(KERN_ERR
1972 "[LS][cm3629 error] adc_table[%d] = 0x%lx Err\n",
1973 i, tempdata[i]);
1974 return count;
1975 }
1976 }
1977 mutex_lock(&als_get_adc_mutex);
1978 for (i = 0; i < 10; i++) {
1979 lpi->adc_table[i] = tempdata[i];
1980 printk(KERN_INFO
1981 "[LS][cm3629]Set lpi->adc_table[%d] = 0x%x\n",
1982 i, *(lp_info->adc_table + i));
1983 }
1984 if (lightsensor_update_table(lpi) < 0)
1985 printk(KERN_ERR "[LS][cm3629 error] %s: update ls table fail\n",
1986 __func__);
1987 mutex_unlock(&als_get_adc_mutex);
1988 D("[LS][cm3629] %s\n", __func__);
1989
1990 return count;
1991}
1992
1993static DEVICE_ATTR(ls_adc_table, 0664,
1994 ls_adc_table_show, ls_adc_table_store);
1995
1996
1997static ssize_t ls_fLevel_show(struct device *dev,
1998 struct device_attribute *attr, char *buf)
1999{
2000 return sprintf(buf, "fLevel = %d\n", f_cm3629_level);
2001}
2002static ssize_t ls_fLevel_store(struct device *dev,
2003 struct device_attribute *attr,
2004 const char *buf, size_t count)
2005{
2006 struct cm3629_info *lpi = lp_info;
2007 int value = 0;
2008 sscanf(buf, "%d", &value);
2009 (value >= 0)?(value = min(value, 10)):(value = max(value, -1));
2010 f_cm3629_level = value;
2011 input_report_abs(lpi->ls_input_dev, ABS_MISC, f_cm3629_level);
2012 input_sync(lpi->ls_input_dev);
2013 printk(KERN_INFO "[LS]set fLevel = %d\n", f_cm3629_level);
2014
Ethan Chenf969e322013-09-02 22:49:51 -07002015 hr_msleep(1000);
Flemmarde2ce3b72013-04-25 21:59:45 -07002016 f_cm3629_level = -1;
2017 return count;
2018}
2019static DEVICE_ATTR(ls_flevel, 0664, ls_fLevel_show, ls_fLevel_store);
2020
2021
2022static ssize_t ps_workaround_table_show(struct device *dev,
2023 struct device_attribute *attr, char *buf)
2024{
2025 struct cm3629_info *lpi = lp_info;
2026 int i = 0;
2027 char table_str[952] = "";
2028 char temp_str[64] = "";
2029
2030 sprintf(table_str, "mapping table size = %d\n", lpi->mapping_size);
2031 printk(KERN_DEBUG "%s: table_str = %s\n", __func__, table_str);
2032 for (i = 0; i < lpi->mapping_size; i++) {
2033 memset(temp_str, 0, 64);
2034 if ((i == 0) || ((i % 10) == 1))
2035 sprintf(temp_str, "[%d] = 0x%x", i, lpi->mapping_table[i]);
2036 else
2037 sprintf(temp_str, ", [%d] = 0x%x", i, lpi->mapping_table[i]);
2038 strcat(table_str, temp_str);
2039 printk(KERN_DEBUG "%s: [%d]: table_str = %s\n", __func__, i, table_str);
2040 if ((i != 0) && (i % 10) == 0)
2041 strcat(table_str, "\n");
2042 }
2043
2044 return sprintf(buf, "%s\n", table_str);
2045}
2046static ssize_t ps_workaround_table_store(struct device *dev,
2047 struct device_attribute *attr,
2048 const char *buf, size_t count)
2049{
2050 struct cm3629_info *lpi = lp_info;
2051 int index = 0;
2052 unsigned int value = 0;
2053
2054 sscanf(buf, "%d 0x%x", &index, &value);
2055
2056 D("%s: input: index = %d, value = 0x%x\n", __func__, index, value);
2057
2058 if ((index < lpi->mapping_size) && (index >= 0) && (value <= 255) && (index >= 0))
2059 lpi->mapping_table[index] = value;
2060
2061 if ((index < lpi->mapping_size) && (index >= 0)) {
2062 printk(KERN_INFO "%s: lpi->mapping_table[%d] = 0x%x, "
2063 "lpi->mapping_size = %d\n",
2064 __func__, index, lpi->mapping_table[index],
2065 lpi->mapping_size);
2066 }
2067
2068 return count;
2069}
2070static DEVICE_ATTR(ps_workaround_table, 0664, ps_workaround_table_show, ps_workaround_table_store);
2071
2072
2073static ssize_t ps_fixed_thd_add_show(struct device *dev,
2074 struct device_attribute *attr, char *buf)
2075{
2076 struct cm3629_info *lpi = lp_info;
2077
2078 return sprintf(buf, "Fixed added threshold = %d\n", lpi->ps_th_add);
2079}
2080static ssize_t ps_fixed_thd_add_store(struct device *dev,
2081 struct device_attribute *attr,
2082 const char *buf, size_t count)
2083{
2084 struct cm3629_info *lpi = lp_info;
2085 int value = 0;
2086
2087 sscanf(buf, "%d", &value);
2088
2089 D("%s: input: value = %d\n", __func__, value);
2090
2091 if ((value >= 0) && (value <= 255))
2092 lpi->ps_th_add = value;
2093
2094 D("%s: lpi->ps_th_add = %d\n", __func__, lpi->ps_th_add);
2095
2096 return count;
2097}
2098static DEVICE_ATTR(ps_fixed_thd_add, 0664, ps_fixed_thd_add_show, ps_fixed_thd_add_store);
2099
2100static ssize_t ls_dark_level_show(struct device *dev,
2101 struct device_attribute *attr, char *buf)
2102{
2103 int ret = 0;
2104 struct cm3629_info *lpi = lp_info;
2105
2106 ret = sprintf(buf, "LS_dark_level = %d\n", lpi->dark_level);
2107
2108 return ret;
2109}
2110static ssize_t ls_dark_level_store(struct device *dev,
2111 struct device_attribute *attr,
2112 const char *buf, size_t count)
2113{
2114 int ls_dark_level = 0;
2115 struct cm3629_info *lpi = lp_info;
2116
2117 sscanf(buf, "%d" , &ls_dark_level);
2118
2119 lpi->dark_level = (uint8_t) ls_dark_level;
2120
2121 D("[LS] %s: LS_dark_level = %d\n",
2122 __func__, lpi->dark_level);
2123
2124 return count;
2125}
2126static DEVICE_ATTR(ls_dark_level, 0664, ls_dark_level_show, ls_dark_level_store);
2127
2128static int lightsensor_setup(struct cm3629_info *lpi)
2129{
2130 int ret;
2131
2132 lpi->ls_input_dev = input_allocate_device();
2133 if (!lpi->ls_input_dev) {
2134 pr_err(
2135 "[LS][cm3629 error]%s: could not allocate ls input device\n",
2136 __func__);
2137 return -ENOMEM;
2138 }
2139 lpi->ls_input_dev->name = "lightsensor-level";
2140 set_bit(EV_ABS, lpi->ls_input_dev->evbit);
2141 input_set_abs_params(lpi->ls_input_dev, ABS_MISC, 0, 9, 0, 0);
2142
2143 ret = input_register_device(lpi->ls_input_dev);
2144 if (ret < 0) {
2145 pr_err("[LS][cm3629 error]%s: can not register ls input device\n",
2146 __func__);
2147 goto err_free_ls_input_device;
2148 }
2149
2150 ret = misc_register(&lightsensor_misc);
2151 if (ret < 0) {
2152 pr_err("[LS][cm3629 error]%s: can not register ls misc device\n",
2153 __func__);
2154 goto err_unregister_ls_input_device;
2155 }
2156
2157 return ret;
2158
2159err_unregister_ls_input_device:
2160 input_unregister_device(lpi->ls_input_dev);
2161err_free_ls_input_device:
2162 input_free_device(lpi->ls_input_dev);
2163 return ret;
2164}
2165
2166static int psensor_setup(struct cm3629_info *lpi)
2167{
2168 int ret;
2169
2170 lpi->ps_input_dev = input_allocate_device();
2171 if (!lpi->ps_input_dev) {
2172 pr_err(
2173 "[PS][cm3629 error]%s: could not allocate ps input device\n",
2174 __func__);
2175 return -ENOMEM;
2176 }
2177 lpi->ps_input_dev->name = "proximity";
2178 set_bit(EV_ABS, lpi->ps_input_dev->evbit);
2179 input_set_abs_params(lpi->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
2180
2181 ret = input_register_device(lpi->ps_input_dev);
2182 if (ret < 0) {
2183 pr_err(
2184 "[PS][cm3629 error]%s: could not register ps input device\n",
2185 __func__);
2186 goto err_free_ps_input_device;
2187 }
2188
2189 ret = misc_register(&psensor_misc);
2190 if (ret < 0) {
2191 pr_err(
2192 "[PS][cm3629 error]%s: could not register ps misc device\n",
2193 __func__);
2194 goto err_unregister_ps_input_device;
2195 }
2196
2197 return ret;
2198
2199err_unregister_ps_input_device:
2200 input_unregister_device(lpi->ps_input_dev);
2201err_free_ps_input_device:
2202 input_free_device(lpi->ps_input_dev);
2203 return ret;
2204}
2205
2206int power_key_check_in_pocket(void)
2207{
2208 struct cm3629_info *lpi = lp_info;
2209 int ls_dark;
2210
2211 uint32_t ls_adc = 0;
2212 int ls_level = 0;
2213 int i;
2214 if (!is_probe_success) {
2215 D("[cm3629] %s return by cm3629 probe fail\n", __func__);
2216 return 0;
2217 }
2218 pocket_mode_flag = 1;
2219 D("[cm3629] %s +++\n", __func__);
2220
2221 psensor_enable(lpi);
2222 D("[cm3629] %s ps_near %d\n", __func__, ps_near);
2223 psensor_disable(lpi);
2224
2225
2226 mutex_lock(&als_get_adc_mutex);
2227 get_ls_adc_value(&ls_adc, 0);
2228 enable_als_interrupt();
2229 mutex_unlock(&als_get_adc_mutex);
2230 for (i = 0; i < 10; i++) {
2231 if (ls_adc <= (*(lpi->adc_table + i))) {
2232 ls_level = i;
2233 if (*(lpi->adc_table + i))
2234 break;
2235 }
2236 if (i == 9) {
2237 ls_level = i;
2238 break;
2239 }
2240 }
2241 D("[cm3629] %s ls_adc %d, ls_level %d\n", __func__, ls_adc, ls_level);
2242 ls_dark = (ls_level <= lpi->dark_level) ? 1 : 0;
2243
2244 D("[cm3629] %s --- ls_dark %d\n", __func__, ls_dark);
2245 pocket_mode_flag = 0;
2246 return (ls_dark && ps_near);
2247}
2248
2249int psensor_enable_by_touch_driver(int on)
2250{
2251 struct cm3629_info *lpi = lp_info;
2252
2253 if (!is_probe_success) {
2254 D("[PS][cm3629] %s return by cm3629 probe fail\n", __func__);
2255 return 0;
2256 }
2257 psensor_enable_by_touch = 1;
2258
2259 D("[PS][cm3629] %s on:%d\n", __func__, on);
2260 if (on) {
2261 psensor_enable(lpi);
2262 } else {
2263 psensor_disable(lpi);
2264 }
2265
2266 psensor_enable_by_touch = 0;
2267 return 0;
2268}
2269static int cm3629_read_chip_id(struct cm3629_info *lpi)
2270{
2271 uint8_t chip_id[3] = {0};
2272 int ret = 0;
2273
2274 als_power(0);
Ethan Chenf969e322013-09-02 22:49:51 -07002275 hr_msleep(5);
Flemmarde2ce3b72013-04-25 21:59:45 -07002276 als_power(1);
Ethan Chenf969e322013-09-02 22:49:51 -07002277 hr_msleep(5);
Flemmarde2ce3b72013-04-25 21:59:45 -07002278
2279 ret = _cm3629_I2C_Read2(lpi->cm3629_slave_address, CH_ID, chip_id, 2);
2280 if (ret >= 0) {
2281 if ((chip_id[0] != 0x29) && (chip_id[0] != 0x92) && (chip_id[0] != 0x82) && (chip_id[0] != 0x83)) {
2282 ret = -1;
2283 D("[PS][cm3629] %s, chip_id Err value = 0x%x, 0x%x, ret %d\n",
2284 __func__, chip_id[0], chip_id[1], ret);
2285 } else
2286 D("[PS][cm3629] %s, chip_id value = 0x%x, 0x%x, ret %d\n",
2287 __func__, chip_id[0], chip_id[1], ret);
2288 } else
2289 D("[PS][cm3629] %s, read chip_id i2c err ret %d\n",
2290 __func__, ret);
2291 sensor_chipId[0] = chip_id[0];
2292
2293 return ret;
2294}
2295static int cm3629_setup(struct cm3629_info *lpi)
2296{
2297 int ret = 0;
2298 char cmd[3] = {0};
2299
2300 ret = gpio_request(lpi->intr_pin, "gpio_cm3629_intr");
2301 if (ret < 0) {
2302 pr_err("[PS][cm3629 error]%s: gpio %d request failed (%d)\n",
2303 __func__, lpi->intr_pin, ret);
2304 return ret;
2305 }
2306
2307 ret = gpio_direction_input(lpi->intr_pin);
2308 if (ret < 0) {
2309 pr_err(
2310 "[PS][cm3629 error]%s: fail to set gpio %d as input (%d)\n",
2311 __func__, lpi->intr_pin, ret);
2312 goto fail_free_intr_pin;
2313 }
2314
2315 ls_initial_cmd(lpi);
2316 psensor_initial_cmd(lpi);
2317
2318
2319 cmd[0] = lpi->ps_conf1_val | CM3629_PS1_SD | CM3629_PS2_SD;
2320 cmd[1] = lpi->ps_conf2_val;
2321 _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_config, cmd, 3);
2322
2323 cmd[0] = lpi->ps_conf3_val;
2324 cmd[1] = CM3629_PS_255_STEPS;
2325 _cm3629_I2C_Write2(lpi->cm3629_slave_address, PS_config_ms, cmd, 3);
2326
2327 ret = request_any_context_irq(lpi->irq,
2328 cm3629_irq_handler,
2329 IRQF_TRIGGER_LOW,
2330 "cm3629",
2331 lpi);
2332 if (ret < 0) {
2333 pr_err(
2334 "[PS][cm3629 error]%s: req_irq(%d) fail for gpio %d (%d)\n",
2335 __func__, lpi->irq,
2336 lpi->intr_pin, ret);
2337 goto fail_free_intr_pin;
2338 }
2339
2340 return ret;
2341
2342fail_free_intr_pin:
2343 gpio_free(lpi->intr_pin);
2344 return ret;
2345}
2346
2347static void cm3629_early_suspend(struct early_suspend *h)
2348{
2349 struct cm3629_info *lpi = lp_info;
2350
2351 D("[LS][cm3629] %s\n", __func__);
2352
2353 if (lpi->ps_enable == 0)
2354 sensor_lpm_power(1);
2355 else
2356 D("[PS][cm3629] %s: Psensor enable, so did not enter lpm\n", __func__);
2357}
2358
2359static void cm3629_late_resume(struct early_suspend *h)
2360{
2361 sensor_lpm_power(0);
2362 D("[LS][cm3629] %s\n", __func__);
2363
2364}
2365
2366static int cm3629_probe(struct i2c_client *client,
2367 const struct i2c_device_id *id)
2368{
2369 int ret = 0;
2370 struct cm3629_info *lpi;
2371 struct cm3629_platform_data *pdata;
2372
2373 D("[PS][cm3629] %s\n", __func__);
2374
2375
2376 lpi = kzalloc(sizeof(struct cm3629_info), GFP_KERNEL);
2377 if (!lpi)
2378 return -ENOMEM;
2379
2380
2381
2382 lpi->i2c_client = client;
2383 pdata = client->dev.platform_data;
2384 if (!pdata) {
2385 pr_err("[PS][cm3629 error]%s: Assign platform_data error!!\n",
2386 __func__);
2387 ret = -EBUSY;
2388 goto err_platform_data_null;
2389 }
2390
2391 lpi->irq = client->irq;
2392
2393 lpi->mfg_mode = board_mfg_mode();
2394
2395 i2c_set_clientdata(client, lpi);
2396 lpi->model = pdata->model;
2397 lpi->intr_pin = pdata->intr;
2398 lpi->adc_table = pdata->levels;
2399 lpi->golden_adc = pdata->golden_adc;
2400 lpi->power = pdata->power;
2401 lpi->lpm_power = pdata->lpm_power;
2402 lpi->cm3629_slave_address = pdata->cm3629_slave_address;
2403 lpi->ps_select = pdata->ps_select;
2404 lpi->ps1_thd_set = pdata->ps1_thd_set;
2405 lpi->ps1_thh_diff = pdata->ps1_thh_diff;
2406 lpi->ps2_thd_set = pdata->ps2_thd_set;
2407 lpi->ps_conf1_val = pdata->ps_conf1_val;
2408 lpi->ps_conf2_val = pdata->ps_conf2_val;
2409 lpi->ps_conf1_val_from_board = pdata->ps_conf1_val;
2410 lpi->ps_conf2_val_from_board = pdata->ps_conf2_val;
2411 lpi->ps_conf3_val = pdata->ps_conf3_val;
2412 lpi->ps_calibration_rule = pdata->ps_calibration_rule;
2413 lpi->j_start = 0;
2414 lpi->j_end = 0;
2415 lpi->mapping_table = pdata->mapping_table;
2416 lpi->mapping_size = pdata->mapping_size;
2417 lpi->ps_base_index = (pdata->mapping_size - 1);
2418 lpi->enable_polling_ignore = pdata->enable_polling_ignore;
2419 lpi->ps1_thd_no_cal = pdata->ps1_thd_no_cal;
2420 lpi->ps1_thd_with_cal = pdata->ps1_thd_with_cal;
2421 lpi->ps2_thd_no_cal = pdata->ps2_thd_no_cal;
2422 lpi->ps2_thd_with_cal = pdata->ps2_thd_with_cal;
2423 lpi->ls_cmd = pdata->ls_cmd;
2424 lpi->ps1_adc_offset = pdata->ps1_adc_offset;
2425 lpi->ps2_adc_offset = pdata->ps2_adc_offset;
2426 lpi->ps_debounce = pdata->ps_debounce;
2427 lpi->ps_delay_time = pdata->ps_delay_time;
2428 lpi->no_need_change_setting = pdata->no_need_change_setting;
2429 lpi->ps_th_add = TH_ADD;
2430 lpi->dark_level = pdata->dark_level;
2431
2432 lp_info = lpi;
2433
2434 ret = cm3629_read_chip_id(lpi);
2435 if (ret < 0) {
2436 pr_err("[PS_ERR][cm3629 error]%s: cm3629_read_chip_id error!\n", __func__);
2437 goto err_cm3629_read_chip_id;
2438 }
2439
2440 if (pdata->ls_cmd == 0) {
2441 if (sensor_chipId[0] != 0x29)
2442 lpi->ls_cmd = CM3629_ALS_IT_320ms | CM3629_ALS_PERS_1;
2443 else
2444 lpi->ls_cmd = CM3629_ALS_IT_400ms | CM3629_ALS_PERS_1;
2445
2446 pr_info("[PS][cm3629]%s: lp_info->ls_cmd = 0x%x!\n",
2447 __func__, lp_info->ls_cmd);
2448 }
2449 D("[PS][cm3629] %s: ls_cmd 0x%x, ps1_adc_offset=0x%02X, "
2450 "ps2_adc_offset=0x%02X, ps_debounce=0x%x, ps1_thh_diff %d\n",
2451 __func__, lpi->ls_cmd, lpi->ps1_adc_offset,
2452 lpi->ps2_adc_offset, lpi->ps_debounce, lpi->ps1_thh_diff);
2453
2454 mutex_init(&als_enable_mutex);
2455 mutex_init(&als_disable_mutex);
2456 mutex_init(&als_get_adc_mutex);
2457 mutex_init(&ps_enable_mutex);
2458
2459 ps_hal_enable = ps_drv_enable = 0;
2460
2461 ret = lightsensor_setup(lpi);
2462 if (ret < 0) {
2463 pr_err("[LS][cm3629 error]%s: lightsensor_setup error!!\n",
2464 __func__);
2465 goto err_lightsensor_setup;
2466 }
2467
2468 ret = psensor_setup(lpi);
2469 if (ret < 0) {
2470 pr_err("[PS][cm3629 error]%s: psensor_setup error!!\n",
2471 __func__);
2472 goto err_psensor_setup;
2473 }
2474
2475 lightsensor_set_kvalue(lpi);
2476 ret = lightsensor_update_table(lpi);
2477 if (ret < 0) {
2478 pr_err("[LS][cm3629 error]%s: update ls table fail\n",
2479 __func__);
2480 goto err_lightsensor_update_table;
2481 }
2482
2483 lpi->lp_wq = create_singlethread_workqueue("cm3629_wq");
2484 if (!lpi->lp_wq) {
2485 pr_err("[PS][cm3629 error]%s: can't create workqueue\n", __func__);
2486 ret = -ENOMEM;
2487 goto err_create_singlethread_workqueue;
2488 }
2489
2490 wake_lock_init(&(lpi->ps_wake_lock), WAKE_LOCK_SUSPEND, "proximity");
2491
2492 psensor_set_kvalue(lpi);
2493
2494#ifdef POLLING_PROXIMITY
2495 if (lpi->enable_polling_ignore == 1)
2496 lpi->original_ps_thd_set = lpi->ps1_thd_set;
2497#endif
2498 ret = cm3629_setup(lpi);
2499 if (ret < 0) {
2500 pr_err("[PS_ERR][cm3629 error]%s: cm3629_setup error!\n", __func__);
2501 goto err_cm3629_setup;
2502 }
2503 ps1_canc_set = lpi->inte_ps1_canc;
2504 ps2_canc_set = lpi->inte_ps2_canc;
2505 ps1_offset_adc = lpi->ps1_adc_offset;
2506 ps2_offset_adc = lpi->ps2_adc_offset;
2507 lpi->cm3629_class = class_create(THIS_MODULE, "optical_sensors");
2508 if (IS_ERR(lpi->cm3629_class)) {
2509 ret = PTR_ERR(lpi->cm3629_class);
2510 lpi->cm3629_class = NULL;
2511 goto err_create_class;
2512 }
2513
2514 lpi->ls_dev = device_create(lpi->cm3629_class,
2515 NULL, 0, "%s", "lightsensor");
2516 if (unlikely(IS_ERR(lpi->ls_dev))) {
2517 ret = PTR_ERR(lpi->ls_dev);
2518 lpi->ls_dev = NULL;
2519 goto err_create_ls_device;
2520 }
2521
2522
2523 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc);
2524 if (ret)
2525 goto err_create_ls_device_file;
2526
2527
2528 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_auto);
2529 if (ret)
2530 goto err_create_ls_device_file;
2531
2532
2533 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_kadc);
2534 if (ret)
2535 goto err_create_ls_device_file;
2536
2537 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_adc_table);
2538 if (ret)
2539 goto err_create_ls_device_file;
2540
2541 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_flevel);
2542 if (ret)
2543 goto err_create_ls_device_file;
2544
2545 ret = device_create_file(lpi->ls_dev, &dev_attr_ls_dark_level);
2546 if (ret)
2547 goto err_create_ls_device_file;
2548
2549 lpi->ps_dev = device_create(lpi->cm3629_class,
2550 NULL, 0, "%s", "proximity");
2551 if (unlikely(IS_ERR(lpi->ps_dev))) {
2552 ret = PTR_ERR(lpi->ps_dev);
2553 lpi->ps_dev = NULL;
2554 goto err_create_ls_device_file;
2555 }
2556
2557
2558 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_adc);
2559 if (ret)
2560 goto err_create_ps_device;
2561
2562
2563 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_kadc);
2564 if (ret)
2565 goto err_create_ps_device;
2566
2567
2568 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_canc);
2569 if (ret)
2570 goto err_create_ps_device;
2571
2572 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_hw);
2573 if (ret)
2574 goto err_create_ps_device;
2575
2576 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_i2c);
2577 if (ret)
2578 goto err_create_ps_device;
2579
2580 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_headset_bt_plugin);
2581 if (ret)
2582 goto err_create_ps_device;
2583
2584 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_workaround_table);
2585 if (ret)
2586 goto err_create_ps_device;
2587
2588 ret = device_create_file(lpi->ps_dev, &dev_attr_ps_fixed_thd_add);
2589 if (ret)
2590 goto err_create_ps_device;
2591
2592 lpi->early_suspend.level =
2593 EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2594 lpi->early_suspend.suspend = cm3629_early_suspend;
2595 lpi->early_suspend.resume = cm3629_late_resume;
2596 register_early_suspend(&lpi->early_suspend);
2597 sensor_lpm_power(0);
2598 D("[PS][cm3629] %s: Probe success!\n", __func__);
2599 is_probe_success = 1;
2600 return ret;
2601
2602err_create_ps_device:
2603 device_unregister(lpi->ps_dev);
2604err_create_ls_device_file:
2605 device_unregister(lpi->ls_dev);
2606err_create_ls_device:
2607 class_destroy(lpi->cm3629_class);
2608err_create_class:
2609err_cm3629_setup:
2610 destroy_workqueue(lpi->lp_wq);
2611 wake_lock_destroy(&(lpi->ps_wake_lock));
2612 input_unregister_device(lpi->ls_input_dev);
2613 input_free_device(lpi->ls_input_dev);
2614 input_unregister_device(lpi->ps_input_dev);
2615 input_free_device(lpi->ps_input_dev);
2616err_create_singlethread_workqueue:
2617err_lightsensor_update_table:
2618 misc_deregister(&psensor_misc);
2619err_psensor_setup:
2620 misc_deregister(&lightsensor_misc);
2621err_lightsensor_setup:
2622 mutex_destroy(&als_enable_mutex);
2623 mutex_destroy(&als_disable_mutex);
2624 mutex_destroy(&als_get_adc_mutex);
2625err_cm3629_read_chip_id:
2626err_platform_data_null:
2627 kfree(lpi);
2628 return ret;
2629}
2630
2631static const struct i2c_device_id cm3629_i2c_id[] = {
2632 {CM3629_I2C_NAME, 0},
2633 {}
2634};
2635
2636static struct i2c_driver cm3629_driver = {
2637 .id_table = cm3629_i2c_id,
2638 .probe = cm3629_probe,
2639 .driver = {
2640 .name = CM3629_I2C_NAME,
2641 .owner = THIS_MODULE,
2642 },
2643};
2644
2645static int __init cm3629_init(void)
2646{
2647 return i2c_add_driver(&cm3629_driver);
2648}
2649
2650static void __exit cm3629_exit(void)
2651{
2652 i2c_del_driver(&cm3629_driver);
2653}
2654
2655module_init(cm3629_init);
2656module_exit(cm3629_exit);
2657
2658MODULE_DESCRIPTION("cm3629 Driver");
2659MODULE_LICENSE("GPL");