2 * AD714X CapTouch Programmable Controller driver supporting AD7142/3/7/8/7A
4 * Copyright 2009-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/device.h>
10 #include <linux/init.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/input/ad714x.h>
17 #define AD714X_PWR_CTRL 0x0
18 #define AD714X_STG_CAL_EN_REG 0x1
19 #define AD714X_AMB_COMP_CTRL0_REG 0x2
20 #define AD714X_PARTID_REG 0x17
21 #define AD7142_PARTID 0xE620
22 #define AD7143_PARTID 0xE630
23 #define AD7147_PARTID 0x1470
24 #define AD7148_PARTID 0x1480
25 #define AD714X_STAGECFG_REG 0x80
26 #define AD714X_SYSCFG_REG 0x0
28 #define STG_LOW_INT_EN_REG 0x5
29 #define STG_HIGH_INT_EN_REG 0x6
30 #define STG_COM_INT_EN_REG 0x7
31 #define STG_LOW_INT_STA_REG 0x8
32 #define STG_HIGH_INT_STA_REG 0x9
33 #define STG_COM_INT_STA_REG 0xA
35 #define CDC_RESULT_S0 0xB
36 #define CDC_RESULT_S1 0xC
37 #define CDC_RESULT_S2 0xD
38 #define CDC_RESULT_S3 0xE
39 #define CDC_RESULT_S4 0xF
40 #define CDC_RESULT_S5 0x10
41 #define CDC_RESULT_S6 0x11
42 #define CDC_RESULT_S7 0x12
43 #define CDC_RESULT_S8 0x13
44 #define CDC_RESULT_S9 0x14
45 #define CDC_RESULT_S10 0x15
46 #define CDC_RESULT_S11 0x16
48 #define STAGE0_AMBIENT 0xF1
49 #define STAGE1_AMBIENT 0x115
50 #define STAGE2_AMBIENT 0x139
51 #define STAGE3_AMBIENT 0x15D
52 #define STAGE4_AMBIENT 0x181
53 #define STAGE5_AMBIENT 0x1A5
54 #define STAGE6_AMBIENT 0x1C9
55 #define STAGE7_AMBIENT 0x1ED
56 #define STAGE8_AMBIENT 0x211
57 #define STAGE9_AMBIENT 0x234
58 #define STAGE10_AMBIENT 0x259
59 #define STAGE11_AMBIENT 0x27D
61 #define PER_STAGE_REG_NUM 36
62 #define STAGE_CFGREG_NUM 8
63 #define SYS_CFGREG_NUM 8
66 * driver information which will be used to maintain the software flow
68 enum ad714x_device_state
{ IDLE
, JITTER
, ACTIVE
, SPACE
};
70 struct ad714x_slider_drv
{
74 enum ad714x_device_state state
;
75 struct input_dev
*input
;
78 struct ad714x_wheel_drv
{
81 int pre_highest_stage
;
83 enum ad714x_device_state state
;
84 struct input_dev
*input
;
87 struct ad714x_touchpad_drv
{
102 enum ad714x_device_state state
;
103 struct input_dev
*input
;
106 struct ad714x_button_drv
{
107 enum ad714x_device_state state
;
109 * Unlike slider/wheel/touchpad, all buttons point to
110 * same input_dev instance
112 struct input_dev
*input
;
115 struct ad714x_driver_data
{
116 struct ad714x_slider_drv
*slider
;
117 struct ad714x_wheel_drv
*wheel
;
118 struct ad714x_touchpad_drv
*touchpad
;
119 struct ad714x_button_drv
*button
;
123 * information to integrate all things which will be private data
127 static void ad714x_use_com_int(struct ad714x_chip
*ad714x
,
128 int start_stage
, int end_stage
)
133 mask
= ((1 << (end_stage
+ 1)) - 1) - ((1 << start_stage
) - 1);
135 ad714x
->read(ad714x
, STG_COM_INT_EN_REG
, &data
, 1);
136 data
|= 1 << end_stage
;
137 ad714x
->write(ad714x
, STG_COM_INT_EN_REG
, data
);
139 ad714x
->read(ad714x
, STG_HIGH_INT_EN_REG
, &data
, 1);
141 ad714x
->write(ad714x
, STG_HIGH_INT_EN_REG
, data
);
144 static void ad714x_use_thr_int(struct ad714x_chip
*ad714x
,
145 int start_stage
, int end_stage
)
150 mask
= ((1 << (end_stage
+ 1)) - 1) - ((1 << start_stage
) - 1);
152 ad714x
->read(ad714x
, STG_COM_INT_EN_REG
, &data
, 1);
153 data
&= ~(1 << end_stage
);
154 ad714x
->write(ad714x
, STG_COM_INT_EN_REG
, data
);
156 ad714x
->read(ad714x
, STG_HIGH_INT_EN_REG
, &data
, 1);
158 ad714x
->write(ad714x
, STG_HIGH_INT_EN_REG
, data
);
161 static int ad714x_cal_highest_stage(struct ad714x_chip
*ad714x
,
162 int start_stage
, int end_stage
)
168 for (i
= start_stage
; i
<= end_stage
; i
++) {
169 if (ad714x
->sensor_val
[i
] > max_res
) {
170 max_res
= ad714x
->sensor_val
[i
];
178 static int ad714x_cal_abs_pos(struct ad714x_chip
*ad714x
,
179 int start_stage
, int end_stage
,
180 int highest_stage
, int max_coord
)
182 int a_param
, b_param
;
184 if (highest_stage
== start_stage
) {
185 a_param
= ad714x
->sensor_val
[start_stage
+ 1];
186 b_param
= ad714x
->sensor_val
[start_stage
] +
187 ad714x
->sensor_val
[start_stage
+ 1];
188 } else if (highest_stage
== end_stage
) {
189 a_param
= ad714x
->sensor_val
[end_stage
] *
190 (end_stage
- start_stage
) +
191 ad714x
->sensor_val
[end_stage
- 1] *
192 (end_stage
- start_stage
- 1);
193 b_param
= ad714x
->sensor_val
[end_stage
] +
194 ad714x
->sensor_val
[end_stage
- 1];
196 a_param
= ad714x
->sensor_val
[highest_stage
] *
197 (highest_stage
- start_stage
) +
198 ad714x
->sensor_val
[highest_stage
- 1] *
199 (highest_stage
- start_stage
- 1) +
200 ad714x
->sensor_val
[highest_stage
+ 1] *
201 (highest_stage
- start_stage
+ 1);
202 b_param
= ad714x
->sensor_val
[highest_stage
] +
203 ad714x
->sensor_val
[highest_stage
- 1] +
204 ad714x
->sensor_val
[highest_stage
+ 1];
207 return (max_coord
/ (end_stage
- start_stage
)) * a_param
/ b_param
;
211 * One button can connect to multi positive and negative of CDCs
212 * Multi-buttons can connect to same positive/negative of one CDC
214 static void ad714x_button_state_machine(struct ad714x_chip
*ad714x
, int idx
)
216 struct ad714x_button_plat
*hw
= &ad714x
->hw
->button
[idx
];
217 struct ad714x_button_drv
*sw
= &ad714x
->sw
->button
[idx
];
221 if (((ad714x
->h_state
& hw
->h_mask
) == hw
->h_mask
) &&
222 ((ad714x
->l_state
& hw
->l_mask
) == hw
->l_mask
)) {
223 dev_dbg(ad714x
->dev
, "button %d touched\n", idx
);
224 input_report_key(sw
->input
, hw
->keycode
, 1);
225 input_sync(sw
->input
);
231 if (((ad714x
->h_state
& hw
->h_mask
) != hw
->h_mask
) ||
232 ((ad714x
->l_state
& hw
->l_mask
) != hw
->l_mask
)) {
233 dev_dbg(ad714x
->dev
, "button %d released\n", idx
);
234 input_report_key(sw
->input
, hw
->keycode
, 0);
235 input_sync(sw
->input
);
246 * The response of a sensor is defined by the absolute number of codes
247 * between the current CDC value and the ambient value.
249 static void ad714x_slider_cal_sensor_val(struct ad714x_chip
*ad714x
, int idx
)
251 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
254 ad714x
->read(ad714x
, CDC_RESULT_S0
+ hw
->start_stage
,
255 &ad714x
->adc_reg
[hw
->start_stage
],
256 hw
->end_stage
- hw
->start_stage
+ 1);
258 for (i
= hw
->start_stage
; i
<= hw
->end_stage
; i
++) {
259 ad714x
->read(ad714x
, STAGE0_AMBIENT
+ i
* PER_STAGE_REG_NUM
,
260 &ad714x
->amb_reg
[i
], 1);
262 ad714x
->sensor_val
[i
] =
263 abs(ad714x
->adc_reg
[i
] - ad714x
->amb_reg
[i
]);
267 static void ad714x_slider_cal_highest_stage(struct ad714x_chip
*ad714x
, int idx
)
269 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
270 struct ad714x_slider_drv
*sw
= &ad714x
->sw
->slider
[idx
];
272 sw
->highest_stage
= ad714x_cal_highest_stage(ad714x
, hw
->start_stage
,
275 dev_dbg(ad714x
->dev
, "slider %d highest_stage:%d\n", idx
,
280 * The formulae are very straight forward. It uses the sensor with the
281 * highest response and the 2 adjacent ones.
282 * When Sensor 0 has the highest response, only sensor 0 and sensor 1
283 * are used in the calculations. Similarly when the last sensor has the
284 * highest response, only the last sensor and the second last sensors
285 * are used in the calculations.
287 * For i= idx_of_peak_Sensor-1 to i= idx_of_peak_Sensor+1
288 * v += Sensor response(i)*i
289 * w += Sensor response(i)
290 * POS=(Number_of_Positions_Wanted/(Number_of_Sensors_Used-1)) *(v/w)
292 static void ad714x_slider_cal_abs_pos(struct ad714x_chip
*ad714x
, int idx
)
294 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
295 struct ad714x_slider_drv
*sw
= &ad714x
->sw
->slider
[idx
];
297 sw
->abs_pos
= ad714x_cal_abs_pos(ad714x
, hw
->start_stage
, hw
->end_stage
,
298 sw
->highest_stage
, hw
->max_coord
);
300 dev_dbg(ad714x
->dev
, "slider %d absolute position:%d\n", idx
,
305 * To minimise the Impact of the noise on the algorithm, ADI developed a
306 * routine that filters the CDC results after they have been read by the
308 * The filter used is an Infinite Input Response(IIR) filter implemented
309 * in firmware and attenuates the noise on the CDC results after they've
310 * been read by the host processor.
311 * Filtered_CDC_result = (Filtered_CDC_result * (10 - Coefficient) +
312 * Latest_CDC_result * Coefficient)/10
314 static void ad714x_slider_cal_flt_pos(struct ad714x_chip
*ad714x
, int idx
)
316 struct ad714x_slider_drv
*sw
= &ad714x
->sw
->slider
[idx
];
318 sw
->flt_pos
= (sw
->flt_pos
* (10 - 4) +
321 dev_dbg(ad714x
->dev
, "slider %d filter position:%d\n", idx
,
325 static void ad714x_slider_use_com_int(struct ad714x_chip
*ad714x
, int idx
)
327 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
329 ad714x_use_com_int(ad714x
, hw
->start_stage
, hw
->end_stage
);
332 static void ad714x_slider_use_thr_int(struct ad714x_chip
*ad714x
, int idx
)
334 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
336 ad714x_use_thr_int(ad714x
, hw
->start_stage
, hw
->end_stage
);
339 static void ad714x_slider_state_machine(struct ad714x_chip
*ad714x
, int idx
)
341 struct ad714x_slider_plat
*hw
= &ad714x
->hw
->slider
[idx
];
342 struct ad714x_slider_drv
*sw
= &ad714x
->sw
->slider
[idx
];
343 unsigned short h_state
, c_state
;
346 mask
= ((1 << (hw
->end_stage
+ 1)) - 1) - ((1 << hw
->start_stage
) - 1);
348 h_state
= ad714x
->h_state
& mask
;
349 c_state
= ad714x
->c_state
& mask
;
355 /* In End of Conversion interrupt mode, the AD714X
356 * continuously generates hardware interrupts.
358 ad714x_slider_use_com_int(ad714x
, idx
);
359 dev_dbg(ad714x
->dev
, "slider %d touched\n", idx
);
364 if (c_state
== mask
) {
365 ad714x_slider_cal_sensor_val(ad714x
, idx
);
366 ad714x_slider_cal_highest_stage(ad714x
, idx
);
367 ad714x_slider_cal_abs_pos(ad714x
, idx
);
368 sw
->flt_pos
= sw
->abs_pos
;
374 if (c_state
== mask
) {
376 ad714x_slider_cal_sensor_val(ad714x
, idx
);
377 ad714x_slider_cal_highest_stage(ad714x
, idx
);
378 ad714x_slider_cal_abs_pos(ad714x
, idx
);
379 ad714x_slider_cal_flt_pos(ad714x
, idx
);
380 input_report_abs(sw
->input
, ABS_X
, sw
->flt_pos
);
381 input_report_key(sw
->input
, BTN_TOUCH
, 1);
383 /* When the user lifts off the sensor, configure
384 * the AD714X back to threshold interrupt mode.
386 ad714x_slider_use_thr_int(ad714x
, idx
);
388 input_report_key(sw
->input
, BTN_TOUCH
, 0);
389 dev_dbg(ad714x
->dev
, "slider %d released\n",
392 input_sync(sw
->input
);
402 * When the scroll wheel is activated, we compute the absolute position based
403 * on the sensor values. To calculate the position, we first determine the
404 * sensor that has the greatest response among the 8 sensors that constitutes
405 * the scrollwheel. Then we determined the 2 sensors on either sides of the
406 * sensor with the highest response and we apply weights to these sensors.
408 static void ad714x_wheel_cal_highest_stage(struct ad714x_chip
*ad714x
, int idx
)
410 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
411 struct ad714x_wheel_drv
*sw
= &ad714x
->sw
->wheel
[idx
];
413 sw
->pre_highest_stage
= sw
->highest_stage
;
414 sw
->highest_stage
= ad714x_cal_highest_stage(ad714x
, hw
->start_stage
,
417 dev_dbg(ad714x
->dev
, "wheel %d highest_stage:%d\n", idx
,
421 static void ad714x_wheel_cal_sensor_val(struct ad714x_chip
*ad714x
, int idx
)
423 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
426 ad714x
->read(ad714x
, CDC_RESULT_S0
+ hw
->start_stage
,
427 &ad714x
->adc_reg
[hw
->start_stage
],
428 hw
->end_stage
- hw
->start_stage
+ 1);
430 for (i
= hw
->start_stage
; i
<= hw
->end_stage
; i
++) {
431 ad714x
->read(ad714x
, STAGE0_AMBIENT
+ i
* PER_STAGE_REG_NUM
,
432 &ad714x
->amb_reg
[i
], 1);
433 if (ad714x
->adc_reg
[i
] > ad714x
->amb_reg
[i
])
434 ad714x
->sensor_val
[i
] =
435 ad714x
->adc_reg
[i
] - ad714x
->amb_reg
[i
];
437 ad714x
->sensor_val
[i
] = 0;
442 * When the scroll wheel is activated, we compute the absolute position based
443 * on the sensor values. To calculate the position, we first determine the
444 * sensor that has the greatest response among the sensors that constitutes
445 * the scrollwheel. Then we determined the sensors on either sides of the
446 * sensor with the highest response and we apply weights to these sensors. The
447 * result of this computation gives us the mean value.
450 static void ad714x_wheel_cal_abs_pos(struct ad714x_chip
*ad714x
, int idx
)
452 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
453 struct ad714x_wheel_drv
*sw
= &ad714x
->sw
->wheel
[idx
];
454 int stage_num
= hw
->end_stage
- hw
->start_stage
+ 1;
455 int first_before
, highest
, first_after
;
456 int a_param
, b_param
;
458 first_before
= (sw
->highest_stage
+ stage_num
- 1) % stage_num
;
459 highest
= sw
->highest_stage
;
460 first_after
= (sw
->highest_stage
+ stage_num
+ 1) % stage_num
;
462 a_param
= ad714x
->sensor_val
[highest
] *
463 (highest
- hw
->start_stage
) +
464 ad714x
->sensor_val
[first_before
] *
465 (highest
- hw
->start_stage
- 1) +
466 ad714x
->sensor_val
[first_after
] *
467 (highest
- hw
->start_stage
+ 1);
468 b_param
= ad714x
->sensor_val
[highest
] +
469 ad714x
->sensor_val
[first_before
] +
470 ad714x
->sensor_val
[first_after
];
472 sw
->abs_pos
= ((hw
->max_coord
/ (hw
->end_stage
- hw
->start_stage
)) *
475 if (sw
->abs_pos
> hw
->max_coord
)
476 sw
->abs_pos
= hw
->max_coord
;
477 else if (sw
->abs_pos
< 0)
481 static void ad714x_wheel_cal_flt_pos(struct ad714x_chip
*ad714x
, int idx
)
483 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
484 struct ad714x_wheel_drv
*sw
= &ad714x
->sw
->wheel
[idx
];
485 if (((sw
->pre_highest_stage
== hw
->end_stage
) &&
486 (sw
->highest_stage
== hw
->start_stage
)) ||
487 ((sw
->pre_highest_stage
== hw
->start_stage
) &&
488 (sw
->highest_stage
== hw
->end_stage
)))
489 sw
->flt_pos
= sw
->abs_pos
;
491 sw
->flt_pos
= ((sw
->flt_pos
* 30) + (sw
->abs_pos
* 71)) / 100;
493 if (sw
->flt_pos
> hw
->max_coord
)
494 sw
->flt_pos
= hw
->max_coord
;
497 static void ad714x_wheel_use_com_int(struct ad714x_chip
*ad714x
, int idx
)
499 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
501 ad714x_use_com_int(ad714x
, hw
->start_stage
, hw
->end_stage
);
504 static void ad714x_wheel_use_thr_int(struct ad714x_chip
*ad714x
, int idx
)
506 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
508 ad714x_use_thr_int(ad714x
, hw
->start_stage
, hw
->end_stage
);
511 static void ad714x_wheel_state_machine(struct ad714x_chip
*ad714x
, int idx
)
513 struct ad714x_wheel_plat
*hw
= &ad714x
->hw
->wheel
[idx
];
514 struct ad714x_wheel_drv
*sw
= &ad714x
->sw
->wheel
[idx
];
515 unsigned short h_state
, c_state
;
518 mask
= ((1 << (hw
->end_stage
+ 1)) - 1) - ((1 << hw
->start_stage
) - 1);
520 h_state
= ad714x
->h_state
& mask
;
521 c_state
= ad714x
->c_state
& mask
;
527 /* In End of Conversion interrupt mode, the AD714X
528 * continuously generates hardware interrupts.
530 ad714x_wheel_use_com_int(ad714x
, idx
);
531 dev_dbg(ad714x
->dev
, "wheel %d touched\n", idx
);
536 if (c_state
== mask
) {
537 ad714x_wheel_cal_sensor_val(ad714x
, idx
);
538 ad714x_wheel_cal_highest_stage(ad714x
, idx
);
539 ad714x_wheel_cal_abs_pos(ad714x
, idx
);
540 sw
->flt_pos
= sw
->abs_pos
;
546 if (c_state
== mask
) {
548 ad714x_wheel_cal_sensor_val(ad714x
, idx
);
549 ad714x_wheel_cal_highest_stage(ad714x
, idx
);
550 ad714x_wheel_cal_abs_pos(ad714x
, idx
);
551 ad714x_wheel_cal_flt_pos(ad714x
, idx
);
552 input_report_abs(sw
->input
, ABS_WHEEL
,
554 input_report_key(sw
->input
, BTN_TOUCH
, 1);
556 /* When the user lifts off the sensor, configure
557 * the AD714X back to threshold interrupt mode.
559 ad714x_wheel_use_thr_int(ad714x
, idx
);
561 input_report_key(sw
->input
, BTN_TOUCH
, 0);
563 dev_dbg(ad714x
->dev
, "wheel %d released\n",
566 input_sync(sw
->input
);
575 static void touchpad_cal_sensor_val(struct ad714x_chip
*ad714x
, int idx
)
577 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
580 ad714x
->read(ad714x
, CDC_RESULT_S0
+ hw
->x_start_stage
,
581 &ad714x
->adc_reg
[hw
->x_start_stage
],
582 hw
->x_end_stage
- hw
->x_start_stage
+ 1);
584 for (i
= hw
->x_start_stage
; i
<= hw
->x_end_stage
; i
++) {
585 ad714x
->read(ad714x
, STAGE0_AMBIENT
+ i
* PER_STAGE_REG_NUM
,
586 &ad714x
->amb_reg
[i
], 1);
587 if (ad714x
->adc_reg
[i
] > ad714x
->amb_reg
[i
])
588 ad714x
->sensor_val
[i
] =
589 ad714x
->adc_reg
[i
] - ad714x
->amb_reg
[i
];
591 ad714x
->sensor_val
[i
] = 0;
595 static void touchpad_cal_highest_stage(struct ad714x_chip
*ad714x
, int idx
)
597 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
598 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
600 sw
->x_highest_stage
= ad714x_cal_highest_stage(ad714x
,
601 hw
->x_start_stage
, hw
->x_end_stage
);
602 sw
->y_highest_stage
= ad714x_cal_highest_stage(ad714x
,
603 hw
->y_start_stage
, hw
->y_end_stage
);
606 "touchpad %d x_highest_stage:%d, y_highest_stage:%d\n",
607 idx
, sw
->x_highest_stage
, sw
->y_highest_stage
);
611 * If 2 fingers are touching the sensor then 2 peaks can be observed in the
613 * The arithmetic doesn't support to get absolute coordinates for multi-touch
616 static int touchpad_check_second_peak(struct ad714x_chip
*ad714x
, int idx
)
618 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
619 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
622 for (i
= hw
->x_start_stage
; i
< sw
->x_highest_stage
; i
++) {
623 if ((ad714x
->sensor_val
[i
] - ad714x
->sensor_val
[i
+ 1])
624 > (ad714x
->sensor_val
[i
+ 1] / 10))
628 for (i
= sw
->x_highest_stage
; i
< hw
->x_end_stage
; i
++) {
629 if ((ad714x
->sensor_val
[i
+ 1] - ad714x
->sensor_val
[i
])
630 > (ad714x
->sensor_val
[i
] / 10))
634 for (i
= hw
->y_start_stage
; i
< sw
->y_highest_stage
; i
++) {
635 if ((ad714x
->sensor_val
[i
] - ad714x
->sensor_val
[i
+ 1])
636 > (ad714x
->sensor_val
[i
+ 1] / 10))
640 for (i
= sw
->y_highest_stage
; i
< hw
->y_end_stage
; i
++) {
641 if ((ad714x
->sensor_val
[i
+ 1] - ad714x
->sensor_val
[i
])
642 > (ad714x
->sensor_val
[i
] / 10))
650 * If only one finger is used to activate the touch pad then only 1 peak will be
651 * registered in the distribution. This peak and the 2 adjacent sensors will be
652 * used in the calculation of the absolute position. This will prevent hand
653 * shadows to affect the absolute position calculation.
655 static void touchpad_cal_abs_pos(struct ad714x_chip
*ad714x
, int idx
)
657 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
658 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
660 sw
->x_abs_pos
= ad714x_cal_abs_pos(ad714x
, hw
->x_start_stage
,
661 hw
->x_end_stage
, sw
->x_highest_stage
, hw
->x_max_coord
);
662 sw
->y_abs_pos
= ad714x_cal_abs_pos(ad714x
, hw
->y_start_stage
,
663 hw
->y_end_stage
, sw
->y_highest_stage
, hw
->y_max_coord
);
665 dev_dbg(ad714x
->dev
, "touchpad %d absolute position:(%d, %d)\n", idx
,
666 sw
->x_abs_pos
, sw
->y_abs_pos
);
669 static void touchpad_cal_flt_pos(struct ad714x_chip
*ad714x
, int idx
)
671 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
673 sw
->x_flt_pos
= (sw
->x_flt_pos
* (10 - 4) +
674 sw
->x_abs_pos
* 4)/10;
675 sw
->y_flt_pos
= (sw
->y_flt_pos
* (10 - 4) +
676 sw
->y_abs_pos
* 4)/10;
678 dev_dbg(ad714x
->dev
, "touchpad %d filter position:(%d, %d)\n",
679 idx
, sw
->x_flt_pos
, sw
->y_flt_pos
);
683 * To prevent distortion from showing in the absolute position, it is
684 * necessary to detect the end points. When endpoints are detected, the
685 * driver stops updating the status variables with absolute positions.
686 * End points are detected on the 4 edges of the touchpad sensor. The
687 * method to detect them is the same for all 4.
688 * To detect the end points, the firmware computes the difference in
689 * percent between the sensor on the edge and the adjacent one. The
690 * difference is calculated in percent in order to make the end point
691 * detection independent of the pressure.
694 #define LEFT_END_POINT_DETECTION_LEVEL 550
695 #define RIGHT_END_POINT_DETECTION_LEVEL 750
696 #define LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL 850
697 #define TOP_END_POINT_DETECTION_LEVEL 550
698 #define BOTTOM_END_POINT_DETECTION_LEVEL 950
699 #define TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL 700
700 static int touchpad_check_endpoint(struct ad714x_chip
*ad714x
, int idx
)
702 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
703 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
704 int percent_sensor_diff
;
706 /* left endpoint detect */
707 percent_sensor_diff
= (ad714x
->sensor_val
[hw
->x_start_stage
] -
708 ad714x
->sensor_val
[hw
->x_start_stage
+ 1]) * 100 /
709 ad714x
->sensor_val
[hw
->x_start_stage
+ 1];
711 if (percent_sensor_diff
>= LEFT_END_POINT_DETECTION_LEVEL
) {
714 ad714x
->sensor_val
[hw
->x_start_stage
+ 1];
717 if ((percent_sensor_diff
< LEFT_END_POINT_DETECTION_LEVEL
) &&
718 (ad714x
->sensor_val
[hw
->x_start_stage
+ 1] >
719 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL
+ sw
->left_ep_val
))
723 /* right endpoint detect */
724 percent_sensor_diff
= (ad714x
->sensor_val
[hw
->x_end_stage
] -
725 ad714x
->sensor_val
[hw
->x_end_stage
- 1]) * 100 /
726 ad714x
->sensor_val
[hw
->x_end_stage
- 1];
728 if (percent_sensor_diff
>= RIGHT_END_POINT_DETECTION_LEVEL
) {
731 ad714x
->sensor_val
[hw
->x_end_stage
- 1];
734 if ((percent_sensor_diff
< RIGHT_END_POINT_DETECTION_LEVEL
) &&
735 (ad714x
->sensor_val
[hw
->x_end_stage
- 1] >
736 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL
+ sw
->right_ep_val
))
740 /* top endpoint detect */
741 percent_sensor_diff
= (ad714x
->sensor_val
[hw
->y_start_stage
] -
742 ad714x
->sensor_val
[hw
->y_start_stage
+ 1]) * 100 /
743 ad714x
->sensor_val
[hw
->y_start_stage
+ 1];
745 if (percent_sensor_diff
>= TOP_END_POINT_DETECTION_LEVEL
) {
748 ad714x
->sensor_val
[hw
->y_start_stage
+ 1];
751 if ((percent_sensor_diff
< TOP_END_POINT_DETECTION_LEVEL
) &&
752 (ad714x
->sensor_val
[hw
->y_start_stage
+ 1] >
753 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL
+ sw
->top_ep_val
))
757 /* bottom endpoint detect */
758 percent_sensor_diff
= (ad714x
->sensor_val
[hw
->y_end_stage
] -
759 ad714x
->sensor_val
[hw
->y_end_stage
- 1]) * 100 /
760 ad714x
->sensor_val
[hw
->y_end_stage
- 1];
761 if (!sw
->bottom_ep
) {
762 if (percent_sensor_diff
>= BOTTOM_END_POINT_DETECTION_LEVEL
) {
765 ad714x
->sensor_val
[hw
->y_end_stage
- 1];
768 if ((percent_sensor_diff
< BOTTOM_END_POINT_DETECTION_LEVEL
) &&
769 (ad714x
->sensor_val
[hw
->y_end_stage
- 1] >
770 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL
+ sw
->bottom_ep_val
))
774 return sw
->left_ep
|| sw
->right_ep
|| sw
->top_ep
|| sw
->bottom_ep
;
777 static void touchpad_use_com_int(struct ad714x_chip
*ad714x
, int idx
)
779 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
781 ad714x_use_com_int(ad714x
, hw
->x_start_stage
, hw
->x_end_stage
);
784 static void touchpad_use_thr_int(struct ad714x_chip
*ad714x
, int idx
)
786 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
788 ad714x_use_thr_int(ad714x
, hw
->x_start_stage
, hw
->x_end_stage
);
789 ad714x_use_thr_int(ad714x
, hw
->y_start_stage
, hw
->y_end_stage
);
792 static void ad714x_touchpad_state_machine(struct ad714x_chip
*ad714x
, int idx
)
794 struct ad714x_touchpad_plat
*hw
= &ad714x
->hw
->touchpad
[idx
];
795 struct ad714x_touchpad_drv
*sw
= &ad714x
->sw
->touchpad
[idx
];
796 unsigned short h_state
, c_state
;
799 mask
= (((1 << (hw
->x_end_stage
+ 1)) - 1) -
800 ((1 << hw
->x_start_stage
) - 1)) +
801 (((1 << (hw
->y_end_stage
+ 1)) - 1) -
802 ((1 << hw
->y_start_stage
) - 1));
804 h_state
= ad714x
->h_state
& mask
;
805 c_state
= ad714x
->c_state
& mask
;
811 /* In End of Conversion interrupt mode, the AD714X
812 * continuously generates hardware interrupts.
814 touchpad_use_com_int(ad714x
, idx
);
815 dev_dbg(ad714x
->dev
, "touchpad %d touched\n", idx
);
820 if (c_state
== mask
) {
821 touchpad_cal_sensor_val(ad714x
, idx
);
822 touchpad_cal_highest_stage(ad714x
, idx
);
823 if ((!touchpad_check_second_peak(ad714x
, idx
)) &&
824 (!touchpad_check_endpoint(ad714x
, idx
))) {
826 "touchpad%d, 2 fingers or endpoint\n",
828 touchpad_cal_abs_pos(ad714x
, idx
);
829 sw
->x_flt_pos
= sw
->x_abs_pos
;
830 sw
->y_flt_pos
= sw
->y_abs_pos
;
837 if (c_state
== mask
) {
839 touchpad_cal_sensor_val(ad714x
, idx
);
840 touchpad_cal_highest_stage(ad714x
, idx
);
841 if ((!touchpad_check_second_peak(ad714x
, idx
))
842 && (!touchpad_check_endpoint(ad714x
, idx
))) {
843 touchpad_cal_abs_pos(ad714x
, idx
);
844 touchpad_cal_flt_pos(ad714x
, idx
);
845 input_report_abs(sw
->input
, ABS_X
,
847 input_report_abs(sw
->input
, ABS_Y
,
849 input_report_key(sw
->input
, BTN_TOUCH
,
853 /* When the user lifts off the sensor, configure
854 * the AD714X back to threshold interrupt mode.
856 touchpad_use_thr_int(ad714x
, idx
);
858 input_report_key(sw
->input
, BTN_TOUCH
, 0);
859 dev_dbg(ad714x
->dev
, "touchpad %d released\n",
862 input_sync(sw
->input
);
871 static int ad714x_hw_detect(struct ad714x_chip
*ad714x
)
875 ad714x
->read(ad714x
, AD714X_PARTID_REG
, &data
, 1);
876 switch (data
& 0xFFF0) {
878 ad714x
->product
= 0x7142;
879 ad714x
->version
= data
& 0xF;
880 dev_info(ad714x
->dev
, "found AD7142 captouch, rev:%d\n",
885 ad714x
->product
= 0x7143;
886 ad714x
->version
= data
& 0xF;
887 dev_info(ad714x
->dev
, "found AD7143 captouch, rev:%d\n",
892 ad714x
->product
= 0x7147;
893 ad714x
->version
= data
& 0xF;
894 dev_info(ad714x
->dev
, "found AD7147(A) captouch, rev:%d\n",
899 ad714x
->product
= 0x7148;
900 ad714x
->version
= data
& 0xF;
901 dev_info(ad714x
->dev
, "found AD7148 captouch, rev:%d\n",
907 "fail to detect AD714X captouch, read ID is %04x\n",
913 static void ad714x_hw_init(struct ad714x_chip
*ad714x
)
916 unsigned short reg_base
;
919 /* configuration CDC and interrupts */
921 for (i
= 0; i
< STAGE_NUM
; i
++) {
922 reg_base
= AD714X_STAGECFG_REG
+ i
* STAGE_CFGREG_NUM
;
923 for (j
= 0; j
< STAGE_CFGREG_NUM
; j
++)
924 ad714x
->write(ad714x
, reg_base
+ j
,
925 ad714x
->hw
->stage_cfg_reg
[i
][j
]);
928 for (i
= 0; i
< SYS_CFGREG_NUM
; i
++)
929 ad714x
->write(ad714x
, AD714X_SYSCFG_REG
+ i
,
930 ad714x
->hw
->sys_cfg_reg
[i
]);
931 for (i
= 0; i
< SYS_CFGREG_NUM
; i
++)
932 ad714x
->read(ad714x
, AD714X_SYSCFG_REG
+ i
, &data
, 1);
934 ad714x
->write(ad714x
, AD714X_STG_CAL_EN_REG
, 0xFFF);
936 /* clear all interrupts */
937 ad714x
->read(ad714x
, STG_LOW_INT_STA_REG
, &ad714x
->l_state
, 3);
940 static irqreturn_t
ad714x_interrupt_thread(int irq
, void *data
)
942 struct ad714x_chip
*ad714x
= data
;
945 mutex_lock(&ad714x
->mutex
);
947 ad714x
->read(ad714x
, STG_LOW_INT_STA_REG
, &ad714x
->l_state
, 3);
949 for (i
= 0; i
< ad714x
->hw
->button_num
; i
++)
950 ad714x_button_state_machine(ad714x
, i
);
951 for (i
= 0; i
< ad714x
->hw
->slider_num
; i
++)
952 ad714x_slider_state_machine(ad714x
, i
);
953 for (i
= 0; i
< ad714x
->hw
->wheel_num
; i
++)
954 ad714x_wheel_state_machine(ad714x
, i
);
955 for (i
= 0; i
< ad714x
->hw
->touchpad_num
; i
++)
956 ad714x_touchpad_state_machine(ad714x
, i
);
958 mutex_unlock(&ad714x
->mutex
);
963 #define MAX_DEVICE_NUM 8
964 struct ad714x_chip
*ad714x_probe(struct device
*dev
, u16 bus_type
, int irq
,
965 ad714x_read_t read
, ad714x_write_t write
)
969 struct input_dev
*input
[MAX_DEVICE_NUM
];
971 struct ad714x_platform_data
*plat_data
= dev
->platform_data
;
972 struct ad714x_chip
*ad714x
;
975 struct ad714x_button_drv
*bt_drv
;
976 struct ad714x_slider_drv
*sd_drv
;
977 struct ad714x_wheel_drv
*wl_drv
;
978 struct ad714x_touchpad_drv
*tp_drv
;
982 dev_err(dev
, "IRQ not configured!\n");
987 if (dev
->platform_data
== NULL
) {
988 dev_err(dev
, "platform data for ad714x doesn't exist\n");
993 ad714x
= kzalloc(sizeof(*ad714x
) + sizeof(*ad714x
->sw
) +
994 sizeof(*sd_drv
) * plat_data
->slider_num
+
995 sizeof(*wl_drv
) * plat_data
->wheel_num
+
996 sizeof(*tp_drv
) * plat_data
->touchpad_num
+
997 sizeof(*bt_drv
) * plat_data
->button_num
, GFP_KERNEL
);
1003 ad714x
->hw
= plat_data
;
1005 drv_mem
= ad714x
+ 1;
1006 ad714x
->sw
= drv_mem
;
1007 drv_mem
+= sizeof(*ad714x
->sw
);
1008 ad714x
->sw
->slider
= sd_drv
= drv_mem
;
1009 drv_mem
+= sizeof(*sd_drv
) * ad714x
->hw
->slider_num
;
1010 ad714x
->sw
->wheel
= wl_drv
= drv_mem
;
1011 drv_mem
+= sizeof(*wl_drv
) * ad714x
->hw
->wheel_num
;
1012 ad714x
->sw
->touchpad
= tp_drv
= drv_mem
;
1013 drv_mem
+= sizeof(*tp_drv
) * ad714x
->hw
->touchpad_num
;
1014 ad714x
->sw
->button
= bt_drv
= drv_mem
;
1015 drv_mem
+= sizeof(*bt_drv
) * ad714x
->hw
->button_num
;
1017 ad714x
->read
= read
;
1018 ad714x
->write
= write
;
1022 error
= ad714x_hw_detect(ad714x
);
1026 /* initialize and request sw/hw resources */
1028 ad714x_hw_init(ad714x
);
1029 mutex_init(&ad714x
->mutex
);
1032 * Allocate and register AD714X input device
1036 /* a slider uses one input_dev instance */
1037 if (ad714x
->hw
->slider_num
> 0) {
1038 struct ad714x_slider_plat
*sd_plat
= ad714x
->hw
->slider
;
1040 for (i
= 0; i
< ad714x
->hw
->slider_num
; i
++) {
1041 sd_drv
[i
].input
= input
[alloc_idx
] = input_allocate_device();
1042 if (!input
[alloc_idx
]) {
1047 __set_bit(EV_ABS
, input
[alloc_idx
]->evbit
);
1048 __set_bit(EV_KEY
, input
[alloc_idx
]->evbit
);
1049 __set_bit(ABS_X
, input
[alloc_idx
]->absbit
);
1050 __set_bit(BTN_TOUCH
, input
[alloc_idx
]->keybit
);
1051 input_set_abs_params(input
[alloc_idx
],
1052 ABS_X
, 0, sd_plat
->max_coord
, 0, 0);
1054 input
[alloc_idx
]->id
.bustype
= bus_type
;
1055 input
[alloc_idx
]->id
.product
= ad714x
->product
;
1056 input
[alloc_idx
]->id
.version
= ad714x
->version
;
1057 input
[alloc_idx
]->name
= "ad714x_captouch_slider";
1058 input
[alloc_idx
]->dev
.parent
= dev
;
1060 error
= input_register_device(input
[alloc_idx
]);
1068 /* a wheel uses one input_dev instance */
1069 if (ad714x
->hw
->wheel_num
> 0) {
1070 struct ad714x_wheel_plat
*wl_plat
= ad714x
->hw
->wheel
;
1072 for (i
= 0; i
< ad714x
->hw
->wheel_num
; i
++) {
1073 wl_drv
[i
].input
= input
[alloc_idx
] = input_allocate_device();
1074 if (!input
[alloc_idx
]) {
1079 __set_bit(EV_KEY
, input
[alloc_idx
]->evbit
);
1080 __set_bit(EV_ABS
, input
[alloc_idx
]->evbit
);
1081 __set_bit(ABS_WHEEL
, input
[alloc_idx
]->absbit
);
1082 __set_bit(BTN_TOUCH
, input
[alloc_idx
]->keybit
);
1083 input_set_abs_params(input
[alloc_idx
],
1084 ABS_WHEEL
, 0, wl_plat
->max_coord
, 0, 0);
1086 input
[alloc_idx
]->id
.bustype
= bus_type
;
1087 input
[alloc_idx
]->id
.product
= ad714x
->product
;
1088 input
[alloc_idx
]->id
.version
= ad714x
->version
;
1089 input
[alloc_idx
]->name
= "ad714x_captouch_wheel";
1090 input
[alloc_idx
]->dev
.parent
= dev
;
1092 error
= input_register_device(input
[alloc_idx
]);
1100 /* a touchpad uses one input_dev instance */
1101 if (ad714x
->hw
->touchpad_num
> 0) {
1102 struct ad714x_touchpad_plat
*tp_plat
= ad714x
->hw
->touchpad
;
1104 for (i
= 0; i
< ad714x
->hw
->touchpad_num
; i
++) {
1105 tp_drv
[i
].input
= input
[alloc_idx
] = input_allocate_device();
1106 if (!input
[alloc_idx
]) {
1111 __set_bit(EV_ABS
, input
[alloc_idx
]->evbit
);
1112 __set_bit(EV_KEY
, input
[alloc_idx
]->evbit
);
1113 __set_bit(ABS_X
, input
[alloc_idx
]->absbit
);
1114 __set_bit(ABS_Y
, input
[alloc_idx
]->absbit
);
1115 __set_bit(BTN_TOUCH
, input
[alloc_idx
]->keybit
);
1116 input_set_abs_params(input
[alloc_idx
],
1117 ABS_X
, 0, tp_plat
->x_max_coord
, 0, 0);
1118 input_set_abs_params(input
[alloc_idx
],
1119 ABS_Y
, 0, tp_plat
->y_max_coord
, 0, 0);
1121 input
[alloc_idx
]->id
.bustype
= bus_type
;
1122 input
[alloc_idx
]->id
.product
= ad714x
->product
;
1123 input
[alloc_idx
]->id
.version
= ad714x
->version
;
1124 input
[alloc_idx
]->name
= "ad714x_captouch_pad";
1125 input
[alloc_idx
]->dev
.parent
= dev
;
1127 error
= input_register_device(input
[alloc_idx
]);
1135 /* all buttons use one input node */
1136 if (ad714x
->hw
->button_num
> 0) {
1137 struct ad714x_button_plat
*bt_plat
= ad714x
->hw
->button
;
1139 input
[alloc_idx
] = input_allocate_device();
1140 if (!input
[alloc_idx
]) {
1145 __set_bit(EV_KEY
, input
[alloc_idx
]->evbit
);
1146 for (i
= 0; i
< ad714x
->hw
->button_num
; i
++) {
1147 bt_drv
[i
].input
= input
[alloc_idx
];
1148 __set_bit(bt_plat
[i
].keycode
, input
[alloc_idx
]->keybit
);
1151 input
[alloc_idx
]->id
.bustype
= bus_type
;
1152 input
[alloc_idx
]->id
.product
= ad714x
->product
;
1153 input
[alloc_idx
]->id
.version
= ad714x
->version
;
1154 input
[alloc_idx
]->name
= "ad714x_captouch_button";
1155 input
[alloc_idx
]->dev
.parent
= dev
;
1157 error
= input_register_device(input
[alloc_idx
]);
1164 error
= request_threaded_irq(ad714x
->irq
, NULL
, ad714x_interrupt_thread
,
1165 plat_data
->irqflags
?
1166 plat_data
->irqflags
: IRQF_TRIGGER_FALLING
,
1167 "ad714x_captouch", ad714x
);
1169 dev_err(dev
, "can't allocate irq %d\n", ad714x
->irq
);
1176 dev_err(dev
, "failed to setup AD714x input device %i\n", alloc_idx
);
1177 input_free_device(input
[alloc_idx
]);
1179 while (--alloc_idx
>= 0)
1180 input_unregister_device(input
[alloc_idx
]);
1184 return ERR_PTR(error
);
1186 EXPORT_SYMBOL(ad714x_probe
);
1188 void ad714x_remove(struct ad714x_chip
*ad714x
)
1190 struct ad714x_platform_data
*hw
= ad714x
->hw
;
1191 struct ad714x_driver_data
*sw
= ad714x
->sw
;
1194 free_irq(ad714x
->irq
, ad714x
);
1196 /* unregister and free all input devices */
1198 for (i
= 0; i
< hw
->slider_num
; i
++)
1199 input_unregister_device(sw
->slider
[i
].input
);
1201 for (i
= 0; i
< hw
->wheel_num
; i
++)
1202 input_unregister_device(sw
->wheel
[i
].input
);
1204 for (i
= 0; i
< hw
->touchpad_num
; i
++)
1205 input_unregister_device(sw
->touchpad
[i
].input
);
1208 input_unregister_device(sw
->button
[0].input
);
1212 EXPORT_SYMBOL(ad714x_remove
);
1215 int ad714x_disable(struct ad714x_chip
*ad714x
)
1217 unsigned short data
;
1219 dev_dbg(ad714x
->dev
, "%s enter\n", __func__
);
1221 mutex_lock(&ad714x
->mutex
);
1223 data
= ad714x
->hw
->sys_cfg_reg
[AD714X_PWR_CTRL
] | 0x3;
1224 ad714x
->write(ad714x
, AD714X_PWR_CTRL
, data
);
1226 mutex_unlock(&ad714x
->mutex
);
1230 EXPORT_SYMBOL(ad714x_disable
);
1232 int ad714x_enable(struct ad714x_chip
*ad714x
)
1234 dev_dbg(ad714x
->dev
, "%s enter\n", __func__
);
1236 mutex_lock(&ad714x
->mutex
);
1238 /* resume to non-shutdown mode */
1240 ad714x
->write(ad714x
, AD714X_PWR_CTRL
,
1241 ad714x
->hw
->sys_cfg_reg
[AD714X_PWR_CTRL
]);
1243 /* make sure the interrupt output line is not low level after resume,
1244 * otherwise we will get no chance to enter falling-edge irq again
1247 ad714x
->read(ad714x
, STG_LOW_INT_STA_REG
, &ad714x
->l_state
, 3);
1249 mutex_unlock(&ad714x
->mutex
);
1253 EXPORT_SYMBOL(ad714x_enable
);
1256 MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
1257 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
1258 MODULE_LICENSE("GPL");