9 #include <linux/device.h>
11 #include <linux/input.h>
13 #include <linux/slab.h>
15 #include <linux/module.h>
18 #define AD714X_PWR_CTRL 0x0
19 #define AD714X_STG_CAL_EN_REG 0x1
20 #define AD714X_AMB_COMP_CTRL0_REG 0x2
21 #define AD714X_PARTID_REG 0x17
22 #define AD7142_PARTID 0xE620
23 #define AD7143_PARTID 0xE630
24 #define AD7147_PARTID 0x1470
25 #define AD7148_PARTID 0x1480
26 #define AD714X_STAGECFG_REG 0x80
27 #define AD714X_SYSCFG_REG 0x0
29 #define STG_LOW_INT_EN_REG 0x5
30 #define STG_HIGH_INT_EN_REG 0x6
31 #define STG_COM_INT_EN_REG 0x7
32 #define STG_LOW_INT_STA_REG 0x8
33 #define STG_HIGH_INT_STA_REG 0x9
34 #define STG_COM_INT_STA_REG 0xA
36 #define CDC_RESULT_S0 0xB
37 #define CDC_RESULT_S1 0xC
38 #define CDC_RESULT_S2 0xD
39 #define CDC_RESULT_S3 0xE
40 #define CDC_RESULT_S4 0xF
41 #define CDC_RESULT_S5 0x10
42 #define CDC_RESULT_S6 0x11
43 #define CDC_RESULT_S7 0x12
44 #define CDC_RESULT_S8 0x13
45 #define CDC_RESULT_S9 0x14
46 #define CDC_RESULT_S10 0x15
47 #define CDC_RESULT_S11 0x16
49 #define STAGE0_AMBIENT 0xF1
50 #define STAGE1_AMBIENT 0x115
51 #define STAGE2_AMBIENT 0x139
52 #define STAGE3_AMBIENT 0x15D
53 #define STAGE4_AMBIENT 0x181
54 #define STAGE5_AMBIENT 0x1A5
55 #define STAGE6_AMBIENT 0x1C9
56 #define STAGE7_AMBIENT 0x1ED
57 #define STAGE8_AMBIENT 0x211
58 #define STAGE9_AMBIENT 0x234
59 #define STAGE10_AMBIENT 0x259
60 #define STAGE11_AMBIENT 0x27D
62 #define PER_STAGE_REG_NUM 36
63 #define STAGE_CFGREG_NUM 8
64 #define SYS_CFGREG_NUM 8
128 static void ad714x_use_com_int(
struct ad714x_chip *ad714x,
129 int start_stage,
int end_stage)
134 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
137 data |= 1 << end_stage;
145 static void ad714x_use_thr_int(
struct ad714x_chip *ad714x,
146 int start_stage,
int end_stage)
151 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
154 data &= ~(1 << end_stage);
162 static int ad714x_cal_highest_stage(
struct ad714x_chip *ad714x,
163 int start_stage,
int end_stage)
169 for (i = start_stage; i <= end_stage; i++) {
179 static int ad714x_cal_abs_pos(
struct ad714x_chip *ad714x,
180 int start_stage,
int end_stage,
181 int highest_stage,
int max_coord)
183 int a_param, b_param;
185 if (highest_stage == start_stage) {
186 a_param = ad714x->
sensor_val[start_stage + 1];
189 }
else if (highest_stage == end_stage) {
191 (end_stage - start_stage) +
193 (end_stage - start_stage - 1);
198 (highest_stage - start_stage) +
200 (highest_stage - start_stage - 1) +
202 (highest_stage - start_stage + 1);
208 return (max_coord / (end_stage - start_stage)) * a_param / b_param;
215 static void ad714x_button_state_machine(
struct ad714x_chip *ad714x,
int idx)
224 dev_dbg(ad714x->
dev,
"button %d touched\n", idx);
226 input_sync(sw->
input);
234 dev_dbg(ad714x->
dev,
"button %d released\n", idx);
236 input_sync(sw->
input);
250 static void ad714x_slider_cal_sensor_val(
struct ad714x_chip *ad714x,
int idx)
259 for (i = hw->
start_stage; i <= hw->end_stage; i++) {
268 static void ad714x_slider_cal_highest_stage(
struct ad714x_chip *ad714x,
int idx)
276 dev_dbg(ad714x->
dev,
"slider %d highest_stage:%d\n", idx,
293 static void ad714x_slider_cal_abs_pos(
struct ad714x_chip *ad714x,
int idx)
301 dev_dbg(ad714x->
dev,
"slider %d absolute position:%d\n", idx,
315 static void ad714x_slider_cal_flt_pos(
struct ad714x_chip *ad714x,
int idx)
322 dev_dbg(ad714x->
dev,
"slider %d filter position:%d\n", idx,
326 static void ad714x_slider_use_com_int(
struct ad714x_chip *ad714x,
int idx)
333 static void ad714x_slider_use_thr_int(
struct ad714x_chip *ad714x,
int idx)
340 static void ad714x_slider_state_machine(
struct ad714x_chip *ad714x,
int idx)
344 unsigned short h_state, c_state;
359 ad714x_slider_use_com_int(ad714x, idx);
360 dev_dbg(ad714x->
dev,
"slider %d touched\n", idx);
365 if (c_state == mask) {
366 ad714x_slider_cal_sensor_val(ad714x, idx);
367 ad714x_slider_cal_highest_stage(ad714x, idx);
368 ad714x_slider_cal_abs_pos(ad714x, idx);
375 if (c_state == mask) {
377 ad714x_slider_cal_sensor_val(ad714x, idx);
378 ad714x_slider_cal_highest_stage(ad714x, idx);
379 ad714x_slider_cal_abs_pos(ad714x, idx);
380 ad714x_slider_cal_flt_pos(ad714x, idx);
387 ad714x_slider_use_thr_int(ad714x, idx);
393 input_sync(sw->
input);
409 static void ad714x_wheel_cal_highest_stage(
struct ad714x_chip *ad714x,
int idx)
418 dev_dbg(ad714x->
dev,
"wheel %d highest_stage:%d\n", idx,
422 static void ad714x_wheel_cal_sensor_val(
struct ad714x_chip *ad714x,
int idx)
431 for (i = hw->
start_stage; i <= hw->end_stage; i++) {
451 static void ad714x_wheel_cal_abs_pos(
struct ad714x_chip *ad714x,
int idx)
456 int first_before,
highest, first_after;
457 int a_param, b_param;
459 first_before = (sw->
highest_stage + stage_num - 1) % stage_num;
461 first_after = (sw->
highest_stage + stage_num + 1) % stage_num;
482 static void ad714x_wheel_cal_flt_pos(
struct ad714x_chip *ad714x,
int idx)
498 static void ad714x_wheel_use_com_int(
struct ad714x_chip *ad714x,
int idx)
505 static void ad714x_wheel_use_thr_int(
struct ad714x_chip *ad714x,
int idx)
512 static void ad714x_wheel_state_machine(
struct ad714x_chip *ad714x,
int idx)
516 unsigned short h_state, c_state;
531 ad714x_wheel_use_com_int(ad714x, idx);
532 dev_dbg(ad714x->
dev,
"wheel %d touched\n", idx);
537 if (c_state == mask) {
538 ad714x_wheel_cal_sensor_val(ad714x, idx);
539 ad714x_wheel_cal_highest_stage(ad714x, idx);
540 ad714x_wheel_cal_abs_pos(ad714x, idx);
547 if (c_state == mask) {
549 ad714x_wheel_cal_sensor_val(ad714x, idx);
550 ad714x_wheel_cal_highest_stage(ad714x, idx);
551 ad714x_wheel_cal_abs_pos(ad714x, idx);
552 ad714x_wheel_cal_flt_pos(ad714x, idx);
560 ad714x_wheel_use_thr_int(ad714x, idx);
567 input_sync(sw->
input);
576 static void touchpad_cal_sensor_val(
struct ad714x_chip *ad714x,
int idx)
596 static void touchpad_cal_highest_stage(
struct ad714x_chip *ad714x,
int idx)
607 "touchpad %d x_highest_stage:%d, y_highest_stage:%d\n",
617 static int touchpad_check_second_peak(
struct ad714x_chip *ad714x,
int idx)
656 static void touchpad_cal_abs_pos(
struct ad714x_chip *ad714x,
int idx)
666 dev_dbg(ad714x->
dev,
"touchpad %d absolute position:(%d, %d)\n", idx,
670 static void touchpad_cal_flt_pos(
struct ad714x_chip *ad714x,
int idx)
679 dev_dbg(ad714x->
dev,
"touchpad %d filter position:(%d, %d)\n",
695 #define LEFT_END_POINT_DETECTION_LEVEL 550
696 #define RIGHT_END_POINT_DETECTION_LEVEL 750
697 #define LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL 850
698 #define TOP_END_POINT_DETECTION_LEVEL 550
699 #define BOTTOM_END_POINT_DETECTION_LEVEL 950
700 #define TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL 700
701 static int touchpad_check_endpoint(
struct ad714x_chip *ad714x,
int idx)
705 int percent_sensor_diff;
778 static void touchpad_use_com_int(
struct ad714x_chip *ad714x,
int idx)
785 static void touchpad_use_thr_int(
struct ad714x_chip *ad714x,
int idx)
793 static void ad714x_touchpad_state_machine(
struct ad714x_chip *ad714x,
int idx)
797 unsigned short h_state, c_state;
815 touchpad_use_com_int(ad714x, idx);
816 dev_dbg(ad714x->
dev,
"touchpad %d touched\n", idx);
821 if (c_state == mask) {
822 touchpad_cal_sensor_val(ad714x, idx);
823 touchpad_cal_highest_stage(ad714x, idx);
824 if ((!touchpad_check_second_peak(ad714x, idx)) &&
825 (!touchpad_check_endpoint(ad714x, idx))) {
827 "touchpad%d, 2 fingers or endpoint\n",
829 touchpad_cal_abs_pos(ad714x, idx);
838 if (c_state == mask) {
840 touchpad_cal_sensor_val(ad714x, idx);
841 touchpad_cal_highest_stage(ad714x, idx);
842 if ((!touchpad_check_second_peak(ad714x, idx))
843 && (!touchpad_check_endpoint(ad714x, idx))) {
844 touchpad_cal_abs_pos(ad714x, idx);
845 touchpad_cal_flt_pos(ad714x, idx);
857 touchpad_use_thr_int(ad714x, idx);
860 dev_dbg(ad714x->
dev,
"touchpad %d released\n",
863 input_sync(sw->
input);
872 static int ad714x_hw_detect(
struct ad714x_chip *ad714x)
877 switch (data & 0xFFF0) {
881 dev_info(ad714x->
dev,
"found AD7142 captouch, rev:%d\n",
888 dev_info(ad714x->
dev,
"found AD7143 captouch, rev:%d\n",
895 dev_info(ad714x->
dev,
"found AD7147(A) captouch, rev:%d\n",
902 dev_info(ad714x->
dev,
"found AD7148 captouch, rev:%d\n",
908 "fail to detect AD714X captouch, read ID is %04x\n",
914 static void ad714x_hw_init(
struct ad714x_chip *ad714x)
917 unsigned short reg_base;
925 ad714x->
write(ad714x, reg_base + j,
926 ad714x->
hw->stage_cfg_reg[i][j]);
931 ad714x->
hw->sys_cfg_reg[i]);
941 static irqreturn_t ad714x_interrupt_thread(
int irq,
void *data)
950 for (i = 0; i < ad714x->
hw->button_num; i++)
951 ad714x_button_state_machine(ad714x, i);
952 for (i = 0; i < ad714x->
hw->slider_num; i++)
953 ad714x_slider_state_machine(ad714x, i);
954 for (i = 0; i < ad714x->
hw->wheel_num; i++)
955 ad714x_wheel_state_machine(ad714x, i);
956 for (i = 0; i < ad714x->
hw->touchpad_num; i++)
957 ad714x_touchpad_state_machine(ad714x, i);
964 #define MAX_DEVICE_NUM 8
975 unsigned long irqflags;
984 dev_err(dev,
"IRQ not configured!\n");
990 dev_err(dev,
"platform data for ad714x doesn't exist\n");
995 ad714x = kzalloc(
sizeof(*ad714x) +
sizeof(*ad714x->
sw) +
1005 ad714x->
hw = plat_data;
1007 drv_mem = ad714x + 1;
1008 ad714x->
sw = drv_mem;
1009 drv_mem +=
sizeof(*ad714x->
sw);
1010 ad714x->
sw->slider = sd_drv = drv_mem;
1011 drv_mem +=
sizeof(*sd_drv) * ad714x->
hw->slider_num;
1012 ad714x->
sw->wheel = wl_drv = drv_mem;
1013 drv_mem +=
sizeof(*wl_drv) * ad714x->
hw->wheel_num;
1014 ad714x->
sw->touchpad = tp_drv = drv_mem;
1015 drv_mem +=
sizeof(*tp_drv) * ad714x->
hw->touchpad_num;
1016 ad714x->
sw->button = bt_drv = drv_mem;
1017 drv_mem +=
sizeof(*bt_drv) * ad714x->
hw->button_num;
1024 error = ad714x_hw_detect(ad714x);
1030 ad714x_hw_init(ad714x);
1039 if (ad714x->
hw->slider_num > 0) {
1042 for (i = 0; i < ad714x->
hw->slider_num; i++) {
1043 sd_drv[
i].
input = input[alloc_idx] = input_allocate_device();
1044 if (!input[alloc_idx]) {
1053 input_set_abs_params(input[alloc_idx],
1056 input[alloc_idx]->id.bustype = bus_type;
1057 input[alloc_idx]->id.product = ad714x->
product;
1058 input[alloc_idx]->id.version = ad714x->
version;
1059 input[alloc_idx]->name =
"ad714x_captouch_slider";
1060 input[alloc_idx]->dev.parent =
dev;
1062 error = input_register_device(input[alloc_idx]);
1071 if (ad714x->
hw->wheel_num > 0) {
1074 for (i = 0; i < ad714x->
hw->wheel_num; i++) {
1075 wl_drv[
i].
input = input[alloc_idx] = input_allocate_device();
1076 if (!input[alloc_idx]) {
1085 input_set_abs_params(input[alloc_idx],
1088 input[alloc_idx]->id.bustype = bus_type;
1089 input[alloc_idx]->id.product = ad714x->
product;
1090 input[alloc_idx]->id.version = ad714x->
version;
1091 input[alloc_idx]->name =
"ad714x_captouch_wheel";
1092 input[alloc_idx]->dev.parent =
dev;
1094 error = input_register_device(input[alloc_idx]);
1103 if (ad714x->
hw->touchpad_num > 0) {
1106 for (i = 0; i < ad714x->
hw->touchpad_num; i++) {
1107 tp_drv[
i].
input = input[alloc_idx] = input_allocate_device();
1108 if (!input[alloc_idx]) {
1118 input_set_abs_params(input[alloc_idx],
1120 input_set_abs_params(input[alloc_idx],
1123 input[alloc_idx]->id.bustype = bus_type;
1124 input[alloc_idx]->id.product = ad714x->
product;
1125 input[alloc_idx]->id.version = ad714x->
version;
1126 input[alloc_idx]->name =
"ad714x_captouch_pad";
1127 input[alloc_idx]->dev.parent =
dev;
1129 error = input_register_device(input[alloc_idx]);
1138 if (ad714x->
hw->button_num > 0) {
1141 input[alloc_idx] = input_allocate_device();
1142 if (!input[alloc_idx]) {
1148 for (i = 0; i < ad714x->
hw->button_num; i++) {
1149 bt_drv[
i].
input = input[alloc_idx];
1153 input[alloc_idx]->id.bustype = bus_type;
1154 input[alloc_idx]->id.product = ad714x->
product;
1155 input[alloc_idx]->id.version = ad714x->
version;
1156 input[alloc_idx]->name =
"ad714x_captouch_button";
1157 input[alloc_idx]->dev.parent =
dev;
1159 error = input_register_device(input[alloc_idx]);
1170 irqflags,
"ad714x_captouch", ad714x);
1172 dev_err(dev,
"can't allocate irq %d\n", ad714x->
irq);
1179 dev_err(dev,
"failed to setup AD714x input device %i\n", alloc_idx);
1180 input_free_device(input[alloc_idx]);
1182 while (--alloc_idx >= 0)
1183 input_unregister_device(input[alloc_idx]);
1187 return ERR_PTR(error);
1202 input_unregister_device(sw->
slider[i].input);
1205 input_unregister_device(sw->
wheel[i].input);
1208 input_unregister_device(sw->
touchpad[i].input);
1211 input_unregister_device(sw->
button[0].input);
1220 unsigned short data;
1222 dev_dbg(ad714x->
dev,
"%s enter\n", __func__);
1237 dev_dbg(ad714x->
dev,
"%s enter\n", __func__);