2 * AD714X CapTouch Programmable Controller driver supporting AD7142/3/7/8/7A
4 * Copyright 2009 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
63 #define STAGE_CFGREG_NUM 8
64 #define SYS_CFGREG_NUM 8
67 * driver information which will be used to maintain the software flow
69 enum ad714x_device_state { IDLE, JITTER, ACTIVE, SPACE };
71 struct ad714x_slider_drv {
75 enum ad714x_device_state state;
76 struct input_dev *input;
79 struct ad714x_wheel_drv {
82 int pre_highest_stage;
84 enum ad714x_device_state state;
85 struct input_dev *input;
88 struct ad714x_touchpad_drv {
103 enum ad714x_device_state state;
104 struct input_dev *input;
107 struct ad714x_button_drv {
108 enum ad714x_device_state state;
110 * Unlike slider/wheel/touchpad, all buttons point to
111 * same input_dev instance
113 struct input_dev *input;
116 struct ad714x_driver_data {
117 struct ad714x_slider_drv *slider;
118 struct ad714x_wheel_drv *wheel;
119 struct ad714x_touchpad_drv *touchpad;
120 struct ad714x_button_drv *button;
124 * information to integrate all things which will be private data
128 unsigned short h_state;
129 unsigned short l_state;
130 unsigned short c_state;
131 unsigned short adc_reg[STAGE_NUM];
132 unsigned short amb_reg[STAGE_NUM];
133 unsigned short sensor_val[STAGE_NUM];
135 struct ad714x_platform_data *hw;
136 struct ad714x_driver_data *sw;
141 ad714x_write_t write;
149 static void ad714x_use_com_int(struct ad714x_chip *ad714x,
150 int start_stage, int end_stage)
155 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
157 ad714x->read(ad714x->dev, STG_COM_INT_EN_REG, &data);
158 data |= 1 << end_stage;
159 ad714x->write(ad714x->dev, STG_COM_INT_EN_REG, data);
161 ad714x->read(ad714x->dev, STG_HIGH_INT_EN_REG, &data);
163 ad714x->write(ad714x->dev, STG_HIGH_INT_EN_REG, data);
166 static void ad714x_use_thr_int(struct ad714x_chip *ad714x,
167 int start_stage, int end_stage)
172 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
174 ad714x->read(ad714x->dev, STG_COM_INT_EN_REG, &data);
175 data &= ~(1 << end_stage);
176 ad714x->write(ad714x->dev, STG_COM_INT_EN_REG, data);
178 ad714x->read(ad714x->dev, STG_HIGH_INT_EN_REG, &data);
180 ad714x->write(ad714x->dev, STG_HIGH_INT_EN_REG, data);
183 static int ad714x_cal_highest_stage(struct ad714x_chip *ad714x,
184 int start_stage, int end_stage)
190 for (i = start_stage; i <= end_stage; i++) {
191 if (ad714x->sensor_val[i] > max_res) {
192 max_res = ad714x->sensor_val[i];
200 static int ad714x_cal_abs_pos(struct ad714x_chip *ad714x,
201 int start_stage, int end_stage,
202 int highest_stage, int max_coord)
204 int a_param, b_param;
206 if (highest_stage == start_stage) {
207 a_param = ad714x->sensor_val[start_stage + 1];
208 b_param = ad714x->sensor_val[start_stage] +
209 ad714x->sensor_val[start_stage + 1];
210 } else if (highest_stage == end_stage) {
211 a_param = ad714x->sensor_val[end_stage] *
212 (end_stage - start_stage) +
213 ad714x->sensor_val[end_stage - 1] *
214 (end_stage - start_stage - 1);
215 b_param = ad714x->sensor_val[end_stage] +
216 ad714x->sensor_val[end_stage - 1];
218 a_param = ad714x->sensor_val[highest_stage] *
219 (highest_stage - start_stage) +
220 ad714x->sensor_val[highest_stage - 1] *
221 (highest_stage - start_stage - 1) +
222 ad714x->sensor_val[highest_stage + 1] *
223 (highest_stage - start_stage + 1);
224 b_param = ad714x->sensor_val[highest_stage] +
225 ad714x->sensor_val[highest_stage - 1] +
226 ad714x->sensor_val[highest_stage + 1];
229 return (max_coord / (end_stage - start_stage)) * a_param / b_param;
233 * One button can connect to multi positive and negative of CDCs
234 * Multi-buttons can connect to same positive/negative of one CDC
236 static void ad714x_button_state_machine(struct ad714x_chip *ad714x, int idx)
238 struct ad714x_button_plat *hw = &ad714x->hw->button[idx];
239 struct ad714x_button_drv *sw = &ad714x->sw->button[idx];
243 if (((ad714x->h_state & hw->h_mask) == hw->h_mask) &&
244 ((ad714x->l_state & hw->l_mask) == hw->l_mask)) {
245 dev_dbg(ad714x->dev, "button %d touched\n", idx);
246 input_report_key(sw->input, hw->keycode, 1);
247 input_sync(sw->input);
253 if (((ad714x->h_state & hw->h_mask) != hw->h_mask) ||
254 ((ad714x->l_state & hw->l_mask) != hw->l_mask)) {
255 dev_dbg(ad714x->dev, "button %d released\n", idx);
256 input_report_key(sw->input, hw->keycode, 0);
257 input_sync(sw->input);
268 * The response of a sensor is defined by the absolute number of codes
269 * between the current CDC value and the ambient value.
271 static void ad714x_slider_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
273 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
276 for (i = hw->start_stage; i <= hw->end_stage; i++) {
277 ad714x->read(ad714x->dev, CDC_RESULT_S0 + i,
278 &ad714x->adc_reg[i]);
279 ad714x->read(ad714x->dev,
280 STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
281 &ad714x->amb_reg[i]);
283 ad714x->sensor_val[i] = abs(ad714x->adc_reg[i] -
288 static void ad714x_slider_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
290 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
291 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
293 sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
296 dev_dbg(ad714x->dev, "slider %d highest_stage:%d\n", idx,
301 * The formulae are very straight forward. It uses the sensor with the
302 * highest response and the 2 adjacent ones.
303 * When Sensor 0 has the highest response, only sensor 0 and sensor 1
304 * are used in the calculations. Similarly when the last sensor has the
305 * highest response, only the last sensor and the second last sensors
306 * are used in the calculations.
308 * For i= idx_of_peak_Sensor-1 to i= idx_of_peak_Sensor+1
309 * v += Sensor response(i)*i
310 * w += Sensor response(i)
311 * POS=(Number_of_Positions_Wanted/(Number_of_Sensors_Used-1)) *(v/w)
313 static void ad714x_slider_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
315 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
316 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
318 sw->abs_pos = ad714x_cal_abs_pos(ad714x, hw->start_stage, hw->end_stage,
319 sw->highest_stage, hw->max_coord);
321 dev_dbg(ad714x->dev, "slider %d absolute position:%d\n", idx,
326 * To minimise the Impact of the noise on the algorithm, ADI developed a
327 * routine that filters the CDC results after they have been read by the
329 * The filter used is an Infinite Input Response(IIR) filter implemented
330 * in firmware and attenuates the noise on the CDC results after they've
331 * been read by the host processor.
332 * Filtered_CDC_result = (Filtered_CDC_result * (10 - Coefficient) +
333 * Latest_CDC_result * Coefficient)/10
335 static void ad714x_slider_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
337 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
339 sw->flt_pos = (sw->flt_pos * (10 - 4) +
342 dev_dbg(ad714x->dev, "slider %d filter position:%d\n", idx,
346 static void ad714x_slider_use_com_int(struct ad714x_chip *ad714x, int idx)
348 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
350 ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
353 static void ad714x_slider_use_thr_int(struct ad714x_chip *ad714x, int idx)
355 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
357 ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
360 static void ad714x_slider_state_machine(struct ad714x_chip *ad714x, int idx)
362 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
363 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
364 unsigned short h_state, c_state;
367 mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
369 h_state = ad714x->h_state & mask;
370 c_state = ad714x->c_state & mask;
376 /* In End of Conversion interrupt mode, the AD714X
377 * continuously generates hardware interrupts.
379 ad714x_slider_use_com_int(ad714x, idx);
380 dev_dbg(ad714x->dev, "slider %d touched\n", idx);
385 if (c_state == mask) {
386 ad714x_slider_cal_sensor_val(ad714x, idx);
387 ad714x_slider_cal_highest_stage(ad714x, idx);
388 ad714x_slider_cal_abs_pos(ad714x, idx);
389 sw->flt_pos = sw->abs_pos;
395 if (c_state == mask) {
397 ad714x_slider_cal_sensor_val(ad714x, idx);
398 ad714x_slider_cal_highest_stage(ad714x, idx);
399 ad714x_slider_cal_abs_pos(ad714x, idx);
400 ad714x_slider_cal_flt_pos(ad714x, idx);
401 input_report_abs(sw->input, ABS_X, sw->flt_pos);
402 input_report_key(sw->input, BTN_TOUCH, 1);
404 /* When the user lifts off the sensor, configure
405 * the AD714X back to threshold interrupt mode.
407 ad714x_slider_use_thr_int(ad714x, idx);
409 input_report_key(sw->input, BTN_TOUCH, 0);
410 dev_dbg(ad714x->dev, "slider %d released\n",
413 input_sync(sw->input);
423 * When the scroll wheel is activated, we compute the absolute position based
424 * on the sensor values. To calculate the position, we first determine the
425 * sensor that has the greatest response among the 8 sensors that constitutes
426 * the scrollwheel. Then we determined the 2 sensors on either sides of the
427 * sensor with the highest response and we apply weights to these sensors.
429 static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
431 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
432 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
434 sw->pre_highest_stage = sw->highest_stage;
435 sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
438 dev_dbg(ad714x->dev, "wheel %d highest_stage:%d\n", idx,
442 static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
444 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
447 for (i = hw->start_stage; i <= hw->end_stage; i++) {
448 ad714x->read(ad714x->dev, CDC_RESULT_S0 + i,
449 &ad714x->adc_reg[i]);
450 ad714x->read(ad714x->dev,
451 STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
452 &ad714x->amb_reg[i]);
453 if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
454 ad714x->sensor_val[i] = ad714x->adc_reg[i] -
457 ad714x->sensor_val[i] = 0;
462 * When the scroll wheel is activated, we compute the absolute position based
463 * on the sensor values. To calculate the position, we first determine the
464 * sensor that has the greatest response among the sensors that constitutes
465 * the scrollwheel. Then we determined the sensors on either sides of the
466 * sensor with the highest response and we apply weights to these sensors. The
467 * result of this computation gives us the mean value.
470 static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
472 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
473 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
474 int stage_num = hw->end_stage - hw->start_stage + 1;
475 int first_before, highest, first_after;
476 int a_param, b_param;
478 first_before = (sw->highest_stage + stage_num - 1) % stage_num;
479 highest = sw->highest_stage;
480 first_after = (sw->highest_stage + stage_num + 1) % stage_num;
482 a_param = ad714x->sensor_val[highest] *
483 (highest - hw->start_stage) +
484 ad714x->sensor_val[first_before] *
485 (highest - hw->start_stage - 1) +
486 ad714x->sensor_val[first_after] *
487 (highest - hw->start_stage + 1);
488 b_param = ad714x->sensor_val[highest] +
489 ad714x->sensor_val[first_before] +
490 ad714x->sensor_val[first_after];
492 sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *
495 if (sw->abs_pos > hw->max_coord)
496 sw->abs_pos = hw->max_coord;
497 else if (sw->abs_pos < 0)
501 static void ad714x_wheel_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
503 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
504 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
505 if (((sw->pre_highest_stage == hw->end_stage) &&
506 (sw->highest_stage == hw->start_stage)) ||
507 ((sw->pre_highest_stage == hw->start_stage) &&
508 (sw->highest_stage == hw->end_stage)))
509 sw->flt_pos = sw->abs_pos;
511 sw->flt_pos = ((sw->flt_pos * 30) + (sw->abs_pos * 71)) / 100;
513 if (sw->flt_pos > hw->max_coord)
514 sw->flt_pos = hw->max_coord;
517 static void ad714x_wheel_use_com_int(struct ad714x_chip *ad714x, int idx)
519 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
521 ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
524 static void ad714x_wheel_use_thr_int(struct ad714x_chip *ad714x, int idx)
526 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
528 ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
531 static void ad714x_wheel_state_machine(struct ad714x_chip *ad714x, int idx)
533 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
534 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
535 unsigned short h_state, c_state;
538 mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
540 h_state = ad714x->h_state & mask;
541 c_state = ad714x->c_state & mask;
547 /* In End of Conversion interrupt mode, the AD714X
548 * continuously generates hardware interrupts.
550 ad714x_wheel_use_com_int(ad714x, idx);
551 dev_dbg(ad714x->dev, "wheel %d touched\n", idx);
556 if (c_state == mask) {
557 ad714x_wheel_cal_sensor_val(ad714x, idx);
558 ad714x_wheel_cal_highest_stage(ad714x, idx);
559 ad714x_wheel_cal_abs_pos(ad714x, idx);
560 sw->flt_pos = sw->abs_pos;
566 if (c_state == mask) {
568 ad714x_wheel_cal_sensor_val(ad714x, idx);
569 ad714x_wheel_cal_highest_stage(ad714x, idx);
570 ad714x_wheel_cal_abs_pos(ad714x, idx);
571 ad714x_wheel_cal_flt_pos(ad714x, idx);
572 input_report_abs(sw->input, ABS_WHEEL,
574 input_report_key(sw->input, BTN_TOUCH, 1);
576 /* When the user lifts off the sensor, configure
577 * the AD714X back to threshold interrupt mode.
579 ad714x_wheel_use_thr_int(ad714x, idx);
581 input_report_key(sw->input, BTN_TOUCH, 0);
583 dev_dbg(ad714x->dev, "wheel %d released\n",
586 input_sync(sw->input);
595 static void touchpad_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
597 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
600 for (i = hw->x_start_stage; i <= hw->x_end_stage; i++) {
601 ad714x->read(ad714x->dev, CDC_RESULT_S0 + i,
602 &ad714x->adc_reg[i]);
603 ad714x->read(ad714x->dev,
604 STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
605 &ad714x->amb_reg[i]);
606 if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
607 ad714x->sensor_val[i] = ad714x->adc_reg[i] -
610 ad714x->sensor_val[i] = 0;
614 static void touchpad_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
616 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
617 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
619 sw->x_highest_stage = ad714x_cal_highest_stage(ad714x,
620 hw->x_start_stage, hw->x_end_stage);
621 sw->y_highest_stage = ad714x_cal_highest_stage(ad714x,
622 hw->y_start_stage, hw->y_end_stage);
625 "touchpad %d x_highest_stage:%d, y_highest_stage:%d\n",
626 idx, sw->x_highest_stage, sw->y_highest_stage);
630 * If 2 fingers are touching the sensor then 2 peaks can be observed in the
632 * The arithmetic doesn't support to get absolute coordinates for multi-touch
635 static int touchpad_check_second_peak(struct ad714x_chip *ad714x, int idx)
637 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
638 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
641 for (i = hw->x_start_stage; i < sw->x_highest_stage; i++) {
642 if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
643 > (ad714x->sensor_val[i + 1] / 10))
647 for (i = sw->x_highest_stage; i < hw->x_end_stage; i++) {
648 if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
649 > (ad714x->sensor_val[i] / 10))
653 for (i = hw->y_start_stage; i < sw->y_highest_stage; i++) {
654 if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
655 > (ad714x->sensor_val[i + 1] / 10))
659 for (i = sw->y_highest_stage; i < hw->y_end_stage; i++) {
660 if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
661 > (ad714x->sensor_val[i] / 10))
669 * If only one finger is used to activate the touch pad then only 1 peak will be
670 * registered in the distribution. This peak and the 2 adjacent sensors will be
671 * used in the calculation of the absolute position. This will prevent hand
672 * shadows to affect the absolute position calculation.
674 static void touchpad_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
676 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
677 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
679 sw->x_abs_pos = ad714x_cal_abs_pos(ad714x, hw->x_start_stage,
680 hw->x_end_stage, sw->x_highest_stage, hw->x_max_coord);
681 sw->y_abs_pos = ad714x_cal_abs_pos(ad714x, hw->y_start_stage,
682 hw->y_end_stage, sw->y_highest_stage, hw->y_max_coord);
684 dev_dbg(ad714x->dev, "touchpad %d absolute position:(%d, %d)\n", idx,
685 sw->x_abs_pos, sw->y_abs_pos);
688 static void touchpad_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
690 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
692 sw->x_flt_pos = (sw->x_flt_pos * (10 - 4) +
693 sw->x_abs_pos * 4)/10;
694 sw->y_flt_pos = (sw->y_flt_pos * (10 - 4) +
695 sw->y_abs_pos * 4)/10;
697 dev_dbg(ad714x->dev, "touchpad %d filter position:(%d, %d)\n",
698 idx, sw->x_flt_pos, sw->y_flt_pos);
702 * To prevent distortion from showing in the absolute position, it is
703 * necessary to detect the end points. When endpoints are detected, the
704 * driver stops updating the status variables with absolute positions.
705 * End points are detected on the 4 edges of the touchpad sensor. The
706 * method to detect them is the same for all 4.
707 * To detect the end points, the firmware computes the difference in
708 * percent between the sensor on the edge and the adjacent one. The
709 * difference is calculated in percent in order to make the end point
710 * detection independent of the pressure.
713 #define LEFT_END_POINT_DETECTION_LEVEL 550
714 #define RIGHT_END_POINT_DETECTION_LEVEL 750
715 #define LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL 850
716 #define TOP_END_POINT_DETECTION_LEVEL 550
717 #define BOTTOM_END_POINT_DETECTION_LEVEL 950
718 #define TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL 700
719 static int touchpad_check_endpoint(struct ad714x_chip *ad714x, int idx)
721 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
722 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
723 int percent_sensor_diff;
725 /* left endpoint detect */
726 percent_sensor_diff = (ad714x->sensor_val[hw->x_start_stage] -
727 ad714x->sensor_val[hw->x_start_stage + 1]) * 100 /
728 ad714x->sensor_val[hw->x_start_stage + 1];
730 if (percent_sensor_diff >= LEFT_END_POINT_DETECTION_LEVEL) {
733 ad714x->sensor_val[hw->x_start_stage + 1];
736 if ((percent_sensor_diff < LEFT_END_POINT_DETECTION_LEVEL) &&
737 (ad714x->sensor_val[hw->x_start_stage + 1] >
738 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->left_ep_val))
742 /* right endpoint detect */
743 percent_sensor_diff = (ad714x->sensor_val[hw->x_end_stage] -
744 ad714x->sensor_val[hw->x_end_stage - 1]) * 100 /
745 ad714x->sensor_val[hw->x_end_stage - 1];
747 if (percent_sensor_diff >= RIGHT_END_POINT_DETECTION_LEVEL) {
750 ad714x->sensor_val[hw->x_end_stage - 1];
753 if ((percent_sensor_diff < RIGHT_END_POINT_DETECTION_LEVEL) &&
754 (ad714x->sensor_val[hw->x_end_stage - 1] >
755 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->right_ep_val))
759 /* top endpoint detect */
760 percent_sensor_diff = (ad714x->sensor_val[hw->y_start_stage] -
761 ad714x->sensor_val[hw->y_start_stage + 1]) * 100 /
762 ad714x->sensor_val[hw->y_start_stage + 1];
764 if (percent_sensor_diff >= TOP_END_POINT_DETECTION_LEVEL) {
767 ad714x->sensor_val[hw->y_start_stage + 1];
770 if ((percent_sensor_diff < TOP_END_POINT_DETECTION_LEVEL) &&
771 (ad714x->sensor_val[hw->y_start_stage + 1] >
772 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->top_ep_val))
776 /* bottom endpoint detect */
777 percent_sensor_diff = (ad714x->sensor_val[hw->y_end_stage] -
778 ad714x->sensor_val[hw->y_end_stage - 1]) * 100 /
779 ad714x->sensor_val[hw->y_end_stage - 1];
780 if (!sw->bottom_ep) {
781 if (percent_sensor_diff >= BOTTOM_END_POINT_DETECTION_LEVEL) {
784 ad714x->sensor_val[hw->y_end_stage - 1];
787 if ((percent_sensor_diff < BOTTOM_END_POINT_DETECTION_LEVEL) &&
788 (ad714x->sensor_val[hw->y_end_stage - 1] >
789 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->bottom_ep_val))
793 return sw->left_ep || sw->right_ep || sw->top_ep || sw->bottom_ep;
796 static void touchpad_use_com_int(struct ad714x_chip *ad714x, int idx)
798 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
800 ad714x_use_com_int(ad714x, hw->x_start_stage, hw->x_end_stage);
803 static void touchpad_use_thr_int(struct ad714x_chip *ad714x, int idx)
805 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
807 ad714x_use_thr_int(ad714x, hw->x_start_stage, hw->x_end_stage);
808 ad714x_use_thr_int(ad714x, hw->y_start_stage, hw->y_end_stage);
811 static void ad714x_touchpad_state_machine(struct ad714x_chip *ad714x, int idx)
813 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
814 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
815 unsigned short h_state, c_state;
818 mask = (((1 << (hw->x_end_stage + 1)) - 1) -
819 ((1 << hw->x_start_stage) - 1)) +
820 (((1 << (hw->y_end_stage + 1)) - 1) -
821 ((1 << hw->y_start_stage) - 1));
823 h_state = ad714x->h_state & mask;
824 c_state = ad714x->c_state & mask;
830 /* In End of Conversion interrupt mode, the AD714X
831 * continuously generates hardware interrupts.
833 touchpad_use_com_int(ad714x, idx);
834 dev_dbg(ad714x->dev, "touchpad %d touched\n", idx);
839 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))) {
845 "touchpad%d, 2 fingers or endpoint\n",
847 touchpad_cal_abs_pos(ad714x, idx);
848 sw->x_flt_pos = sw->x_abs_pos;
849 sw->y_flt_pos = sw->y_abs_pos;
856 if (c_state == mask) {
858 touchpad_cal_sensor_val(ad714x, idx);
859 touchpad_cal_highest_stage(ad714x, idx);
860 if ((!touchpad_check_second_peak(ad714x, idx))
861 && (!touchpad_check_endpoint(ad714x, idx))) {
862 touchpad_cal_abs_pos(ad714x, idx);
863 touchpad_cal_flt_pos(ad714x, idx);
864 input_report_abs(sw->input, ABS_X,
866 input_report_abs(sw->input, ABS_Y,
868 input_report_key(sw->input, BTN_TOUCH,
872 /* When the user lifts off the sensor, configure
873 * the AD714X back to threshold interrupt mode.
875 touchpad_use_thr_int(ad714x, idx);
877 input_report_key(sw->input, BTN_TOUCH, 0);
878 dev_dbg(ad714x->dev, "touchpad %d released\n",
881 input_sync(sw->input);
890 static int ad714x_hw_detect(struct ad714x_chip *ad714x)
894 ad714x->read(ad714x->dev, AD714X_PARTID_REG, &data);
895 switch (data & 0xFFF0) {
897 ad714x->product = 0x7142;
898 ad714x->version = data & 0xF;
899 dev_info(ad714x->dev, "found AD7142 captouch, rev:%d\n",
904 ad714x->product = 0x7143;
905 ad714x->version = data & 0xF;
906 dev_info(ad714x->dev, "found AD7143 captouch, rev:%d\n",
911 ad714x->product = 0x7147;
912 ad714x->version = data & 0xF;
913 dev_info(ad714x->dev, "found AD7147(A) captouch, rev:%d\n",
918 ad714x->product = 0x7148;
919 ad714x->version = data & 0xF;
920 dev_info(ad714x->dev, "found AD7148 captouch, rev:%d\n",
926 "fail to detect AD714X captouch, read ID is %04x\n",
932 static void ad714x_hw_init(struct ad714x_chip *ad714x)
935 unsigned short reg_base;
938 /* configuration CDC and interrupts */
940 for (i = 0; i < STAGE_NUM; i++) {
941 reg_base = AD714X_STAGECFG_REG + i * STAGE_CFGREG_NUM;
942 for (j = 0; j < STAGE_CFGREG_NUM; j++)
943 ad714x->write(ad714x->dev, reg_base + j,
944 ad714x->hw->stage_cfg_reg[i][j]);
947 for (i = 0; i < SYS_CFGREG_NUM; i++)
948 ad714x->write(ad714x->dev, AD714X_SYSCFG_REG + i,
949 ad714x->hw->sys_cfg_reg[i]);
950 for (i = 0; i < SYS_CFGREG_NUM; i++)
951 ad714x->read(ad714x->dev, AD714X_SYSCFG_REG + i,
954 ad714x->write(ad714x->dev, AD714X_STG_CAL_EN_REG, 0xFFF);
956 /* clear all interrupts */
957 ad714x->read(ad714x->dev, STG_LOW_INT_STA_REG, &data);
958 ad714x->read(ad714x->dev, STG_HIGH_INT_STA_REG, &data);
959 ad714x->read(ad714x->dev, STG_COM_INT_STA_REG, &data);
962 static irqreturn_t ad714x_interrupt_thread(int irq, void *data)
964 struct ad714x_chip *ad714x = data;
967 mutex_lock(&ad714x->mutex);
969 ad714x->read(ad714x->dev, STG_LOW_INT_STA_REG, &ad714x->l_state);
970 ad714x->read(ad714x->dev, STG_HIGH_INT_STA_REG, &ad714x->h_state);
971 ad714x->read(ad714x->dev, STG_COM_INT_STA_REG, &ad714x->c_state);
973 for (i = 0; i < ad714x->hw->button_num; i++)
974 ad714x_button_state_machine(ad714x, i);
975 for (i = 0; i < ad714x->hw->slider_num; i++)
976 ad714x_slider_state_machine(ad714x, i);
977 for (i = 0; i < ad714x->hw->wheel_num; i++)
978 ad714x_wheel_state_machine(ad714x, i);
979 for (i = 0; i < ad714x->hw->touchpad_num; i++)
980 ad714x_touchpad_state_machine(ad714x, i);
982 mutex_unlock(&ad714x->mutex);
987 #define MAX_DEVICE_NUM 8
988 struct ad714x_chip *ad714x_probe(struct device *dev, u16 bus_type, int irq,
989 ad714x_read_t read, ad714x_write_t write)
993 struct input_dev *input[MAX_DEVICE_NUM];
995 struct ad714x_platform_data *plat_data = dev->platform_data;
996 struct ad714x_chip *ad714x;
999 struct ad714x_button_drv *bt_drv;
1000 struct ad714x_slider_drv *sd_drv;
1001 struct ad714x_wheel_drv *wl_drv;
1002 struct ad714x_touchpad_drv *tp_drv;
1006 dev_err(dev, "IRQ not configured!\n");
1011 if (dev->platform_data == NULL) {
1012 dev_err(dev, "platform data for ad714x doesn't exist\n");
1017 ad714x = kzalloc(sizeof(*ad714x) + sizeof(*ad714x->sw) +
1018 sizeof(*sd_drv) * plat_data->slider_num +
1019 sizeof(*wl_drv) * plat_data->wheel_num +
1020 sizeof(*tp_drv) * plat_data->touchpad_num +
1021 sizeof(*bt_drv) * plat_data->button_num, GFP_KERNEL);
1027 ad714x->hw = plat_data;
1029 drv_mem = ad714x + 1;
1030 ad714x->sw = drv_mem;
1031 drv_mem += sizeof(*ad714x->sw);
1032 ad714x->sw->slider = sd_drv = drv_mem;
1033 drv_mem += sizeof(*sd_drv) * ad714x->hw->slider_num;
1034 ad714x->sw->wheel = wl_drv = drv_mem;
1035 drv_mem += sizeof(*wl_drv) * ad714x->hw->wheel_num;
1036 ad714x->sw->touchpad = tp_drv = drv_mem;
1037 drv_mem += sizeof(*tp_drv) * ad714x->hw->touchpad_num;
1038 ad714x->sw->button = bt_drv = drv_mem;
1039 drv_mem += sizeof(*bt_drv) * ad714x->hw->button_num;
1041 ad714x->read = read;
1042 ad714x->write = write;
1046 error = ad714x_hw_detect(ad714x);
1050 /* initialize and request sw/hw resources */
1052 ad714x_hw_init(ad714x);
1053 mutex_init(&ad714x->mutex);
1056 * Allocate and register AD714X input device
1060 /* a slider uses one input_dev instance */
1061 if (ad714x->hw->slider_num > 0) {
1062 struct ad714x_slider_plat *sd_plat = ad714x->hw->slider;
1064 for (i = 0; i < ad714x->hw->slider_num; i++) {
1065 sd_drv[i].input = input[alloc_idx] = input_allocate_device();
1066 if (!input[alloc_idx]) {
1071 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1072 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1073 __set_bit(ABS_X, input[alloc_idx]->absbit);
1074 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1075 input_set_abs_params(input[alloc_idx],
1076 ABS_X, 0, sd_plat->max_coord, 0, 0);
1078 input[alloc_idx]->id.bustype = bus_type;
1079 input[alloc_idx]->id.product = ad714x->product;
1080 input[alloc_idx]->id.version = ad714x->version;
1081 input[alloc_idx]->name = "ad714x_captouch_slider";
1082 input[alloc_idx]->dev.parent = dev;
1084 error = input_register_device(input[alloc_idx]);
1092 /* a wheel uses one input_dev instance */
1093 if (ad714x->hw->wheel_num > 0) {
1094 struct ad714x_wheel_plat *wl_plat = ad714x->hw->wheel;
1096 for (i = 0; i < ad714x->hw->wheel_num; i++) {
1097 wl_drv[i].input = input[alloc_idx] = input_allocate_device();
1098 if (!input[alloc_idx]) {
1103 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1104 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1105 __set_bit(ABS_WHEEL, input[alloc_idx]->absbit);
1106 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1107 input_set_abs_params(input[alloc_idx],
1108 ABS_WHEEL, 0, wl_plat->max_coord, 0, 0);
1110 input[alloc_idx]->id.bustype = bus_type;
1111 input[alloc_idx]->id.product = ad714x->product;
1112 input[alloc_idx]->id.version = ad714x->version;
1113 input[alloc_idx]->name = "ad714x_captouch_wheel";
1114 input[alloc_idx]->dev.parent = dev;
1116 error = input_register_device(input[alloc_idx]);
1124 /* a touchpad uses one input_dev instance */
1125 if (ad714x->hw->touchpad_num > 0) {
1126 struct ad714x_touchpad_plat *tp_plat = ad714x->hw->touchpad;
1128 for (i = 0; i < ad714x->hw->touchpad_num; i++) {
1129 tp_drv[i].input = input[alloc_idx] = input_allocate_device();
1130 if (!input[alloc_idx]) {
1135 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1136 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1137 __set_bit(ABS_X, input[alloc_idx]->absbit);
1138 __set_bit(ABS_Y, input[alloc_idx]->absbit);
1139 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1140 input_set_abs_params(input[alloc_idx],
1141 ABS_X, 0, tp_plat->x_max_coord, 0, 0);
1142 input_set_abs_params(input[alloc_idx],
1143 ABS_Y, 0, tp_plat->y_max_coord, 0, 0);
1145 input[alloc_idx]->id.bustype = bus_type;
1146 input[alloc_idx]->id.product = ad714x->product;
1147 input[alloc_idx]->id.version = ad714x->version;
1148 input[alloc_idx]->name = "ad714x_captouch_pad";
1149 input[alloc_idx]->dev.parent = dev;
1151 error = input_register_device(input[alloc_idx]);
1159 /* all buttons use one input node */
1160 if (ad714x->hw->button_num > 0) {
1161 struct ad714x_button_plat *bt_plat = ad714x->hw->button;
1163 input[alloc_idx] = input_allocate_device();
1164 if (!input[alloc_idx]) {
1169 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1170 for (i = 0; i < ad714x->hw->button_num; i++) {
1171 bt_drv[i].input = input[alloc_idx];
1172 __set_bit(bt_plat[i].keycode, input[alloc_idx]->keybit);
1175 input[alloc_idx]->id.bustype = bus_type;
1176 input[alloc_idx]->id.product = ad714x->product;
1177 input[alloc_idx]->id.version = ad714x->version;
1178 input[alloc_idx]->name = "ad714x_captouch_button";
1179 input[alloc_idx]->dev.parent = dev;
1181 error = input_register_device(input[alloc_idx]);
1188 error = request_threaded_irq(ad714x->irq, NULL, ad714x_interrupt_thread,
1189 plat_data->irqflags ?
1190 plat_data->irqflags : IRQF_TRIGGER_FALLING,
1191 "ad714x_captouch", ad714x);
1193 dev_err(dev, "can't allocate irq %d\n", ad714x->irq);
1200 dev_err(dev, "failed to setup AD714x input device %i\n", alloc_idx);
1201 input_free_device(input[alloc_idx]);
1203 while (--alloc_idx >= 0)
1204 input_unregister_device(input[alloc_idx]);
1208 return ERR_PTR(error);
1210 EXPORT_SYMBOL(ad714x_probe);
1212 void ad714x_remove(struct ad714x_chip *ad714x)
1214 struct ad714x_platform_data *hw = ad714x->hw;
1215 struct ad714x_driver_data *sw = ad714x->sw;
1218 free_irq(ad714x->irq, ad714x);
1220 /* unregister and free all input devices */
1222 for (i = 0; i < hw->slider_num; i++)
1223 input_unregister_device(sw->slider[i].input);
1225 for (i = 0; i < hw->wheel_num; i++)
1226 input_unregister_device(sw->wheel[i].input);
1228 for (i = 0; i < hw->touchpad_num; i++)
1229 input_unregister_device(sw->touchpad[i].input);
1232 input_unregister_device(sw->button[0].input);
1236 EXPORT_SYMBOL(ad714x_remove);
1239 int ad714x_disable(struct ad714x_chip *ad714x)
1241 unsigned short data;
1243 dev_dbg(ad714x->dev, "%s enter\n", __func__);
1245 mutex_lock(&ad714x->mutex);
1247 data = ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL] | 0x3;
1248 ad714x->write(ad714x->dev, AD714X_PWR_CTRL, data);
1250 mutex_unlock(&ad714x->mutex);
1254 EXPORT_SYMBOL(ad714x_disable);
1256 int ad714x_enable(struct ad714x_chip *ad714x)
1258 unsigned short data;
1260 dev_dbg(ad714x->dev, "%s enter\n", __func__);
1262 mutex_lock(&ad714x->mutex);
1264 /* resume to non-shutdown mode */
1266 ad714x->write(ad714x->dev, AD714X_PWR_CTRL,
1267 ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL]);
1269 /* make sure the interrupt output line is not low level after resume,
1270 * otherwise we will get no chance to enter falling-edge irq again
1273 ad714x->read(ad714x->dev, STG_LOW_INT_STA_REG, &data);
1274 ad714x->read(ad714x->dev, STG_HIGH_INT_STA_REG, &data);
1275 ad714x->read(ad714x->dev, STG_COM_INT_STA_REG, &data);
1277 mutex_unlock(&ad714x->mutex);
1281 EXPORT_SYMBOL(ad714x_enable);
1284 MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
1285 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
1286 MODULE_LICENSE("GPL");