blob: 2e5d5e1de64787f17c2349e1a3c575144411518f [file] [log] [blame]
Bryan Wu31a62962010-03-21 23:23:24 -07001/*
Barry Song6c04d7b2010-03-21 23:23:29 -07002 * AD714X CapTouch Programmable Controller driver supporting AD7142/3/7/8/7A
Bryan Wu31a62962010-03-21 23:23:24 -07003 *
Michael Hennerich9eff794b72011-08-22 09:45:42 -07004 * Copyright 2009-2011 Analog Devices Inc.
Bryan Wu31a62962010-03-21 23:23:24 -07005 *
6 * Licensed under the GPL-2 or later.
7 */
8
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>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040015#include <linux/module.h>
Bryan Wu31a62962010-03-21 23:23:24 -070016#include "ad714x.h"
17
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
Bryan Wu31a62962010-03-21 23:23:24 -070022#define AD7142_PARTID 0xE620
Barry Song6c04d7b2010-03-21 23:23:29 -070023#define AD7143_PARTID 0xE630
24#define AD7147_PARTID 0x1470
25#define AD7148_PARTID 0x1480
Bryan Wu31a62962010-03-21 23:23:24 -070026#define AD714X_STAGECFG_REG 0x80
27#define AD714X_SYSCFG_REG 0x0
28
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
35
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
48
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
61
62#define PER_STAGE_REG_NUM 36
Bryan Wu31a62962010-03-21 23:23:24 -070063#define STAGE_CFGREG_NUM 8
64#define SYS_CFGREG_NUM 8
65
66/*
67 * driver information which will be used to maintain the software flow
68 */
69enum ad714x_device_state { IDLE, JITTER, ACTIVE, SPACE };
70
71struct ad714x_slider_drv {
72 int highest_stage;
73 int abs_pos;
74 int flt_pos;
75 enum ad714x_device_state state;
76 struct input_dev *input;
77};
78
79struct ad714x_wheel_drv {
80 int abs_pos;
81 int flt_pos;
Bryan Wu31a62962010-03-21 23:23:24 -070082 int pre_highest_stage;
Bryan Wu31a62962010-03-21 23:23:24 -070083 int highest_stage;
84 enum ad714x_device_state state;
85 struct input_dev *input;
86};
87
88struct ad714x_touchpad_drv {
89 int x_highest_stage;
90 int x_flt_pos;
91 int x_abs_pos;
92 int y_highest_stage;
93 int y_flt_pos;
94 int y_abs_pos;
95 int left_ep;
96 int left_ep_val;
97 int right_ep;
98 int right_ep_val;
99 int top_ep;
100 int top_ep_val;
101 int bottom_ep;
102 int bottom_ep_val;
103 enum ad714x_device_state state;
104 struct input_dev *input;
105};
106
107struct ad714x_button_drv {
108 enum ad714x_device_state state;
109 /*
110 * Unlike slider/wheel/touchpad, all buttons point to
111 * same input_dev instance
112 */
113 struct input_dev *input;
114};
115
116struct 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;
121};
122
123/*
124 * information to integrate all things which will be private data
125 * of spi/i2c device
126 */
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700127
Bryan Wu31a62962010-03-21 23:23:24 -0700128static void ad714x_use_com_int(struct ad714x_chip *ad714x,
129 int start_stage, int end_stage)
130{
131 unsigned short data;
132 unsigned short mask;
133
Michael Henneriche223cc72011-05-16 23:17:23 -0700134 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700135
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700136 ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);
Michael Henneriche223cc72011-05-16 23:17:23 -0700137 data |= 1 << end_stage;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700138 ad714x->write(ad714x, STG_COM_INT_EN_REG, data);
Bryan Wu31a62962010-03-21 23:23:24 -0700139
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700140 ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700141 data &= ~mask;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700142 ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);
Bryan Wu31a62962010-03-21 23:23:24 -0700143}
144
145static void ad714x_use_thr_int(struct ad714x_chip *ad714x,
146 int start_stage, int end_stage)
147{
148 unsigned short data;
149 unsigned short mask;
150
Michael Henneriche223cc72011-05-16 23:17:23 -0700151 mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700152
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700153 ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);
Michael Henneriche223cc72011-05-16 23:17:23 -0700154 data &= ~(1 << end_stage);
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700155 ad714x->write(ad714x, STG_COM_INT_EN_REG, data);
Bryan Wu31a62962010-03-21 23:23:24 -0700156
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700157 ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700158 data |= mask;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700159 ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);
Bryan Wu31a62962010-03-21 23:23:24 -0700160}
161
162static int ad714x_cal_highest_stage(struct ad714x_chip *ad714x,
163 int start_stage, int end_stage)
164{
165 int max_res = 0;
166 int max_idx = 0;
167 int i;
168
169 for (i = start_stage; i <= end_stage; i++) {
170 if (ad714x->sensor_val[i] > max_res) {
171 max_res = ad714x->sensor_val[i];
172 max_idx = i;
173 }
174 }
175
176 return max_idx;
177}
178
179static int ad714x_cal_abs_pos(struct ad714x_chip *ad714x,
180 int start_stage, int end_stage,
181 int highest_stage, int max_coord)
182{
183 int a_param, b_param;
184
185 if (highest_stage == start_stage) {
186 a_param = ad714x->sensor_val[start_stage + 1];
187 b_param = ad714x->sensor_val[start_stage] +
188 ad714x->sensor_val[start_stage + 1];
189 } else if (highest_stage == end_stage) {
190 a_param = ad714x->sensor_val[end_stage] *
191 (end_stage - start_stage) +
192 ad714x->sensor_val[end_stage - 1] *
193 (end_stage - start_stage - 1);
194 b_param = ad714x->sensor_val[end_stage] +
195 ad714x->sensor_val[end_stage - 1];
196 } else {
197 a_param = ad714x->sensor_val[highest_stage] *
198 (highest_stage - start_stage) +
199 ad714x->sensor_val[highest_stage - 1] *
200 (highest_stage - start_stage - 1) +
201 ad714x->sensor_val[highest_stage + 1] *
202 (highest_stage - start_stage + 1);
203 b_param = ad714x->sensor_val[highest_stage] +
204 ad714x->sensor_val[highest_stage - 1] +
205 ad714x->sensor_val[highest_stage + 1];
206 }
207
208 return (max_coord / (end_stage - start_stage)) * a_param / b_param;
209}
210
211/*
212 * One button can connect to multi positive and negative of CDCs
213 * Multi-buttons can connect to same positive/negative of one CDC
214 */
215static void ad714x_button_state_machine(struct ad714x_chip *ad714x, int idx)
216{
217 struct ad714x_button_plat *hw = &ad714x->hw->button[idx];
218 struct ad714x_button_drv *sw = &ad714x->sw->button[idx];
219
220 switch (sw->state) {
221 case IDLE:
222 if (((ad714x->h_state & hw->h_mask) == hw->h_mask) &&
223 ((ad714x->l_state & hw->l_mask) == hw->l_mask)) {
224 dev_dbg(ad714x->dev, "button %d touched\n", idx);
225 input_report_key(sw->input, hw->keycode, 1);
226 input_sync(sw->input);
227 sw->state = ACTIVE;
228 }
229 break;
230
231 case ACTIVE:
232 if (((ad714x->h_state & hw->h_mask) != hw->h_mask) ||
233 ((ad714x->l_state & hw->l_mask) != hw->l_mask)) {
234 dev_dbg(ad714x->dev, "button %d released\n", idx);
235 input_report_key(sw->input, hw->keycode, 0);
236 input_sync(sw->input);
237 sw->state = IDLE;
238 }
239 break;
240
241 default:
242 break;
243 }
244}
245
246/*
247 * The response of a sensor is defined by the absolute number of codes
248 * between the current CDC value and the ambient value.
249 */
250static void ad714x_slider_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
251{
252 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
253 int i;
254
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700255 ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
256 &ad714x->adc_reg[hw->start_stage],
257 hw->end_stage - hw->start_stage + 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700258
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700259 for (i = hw->start_stage; i <= hw->end_stage; i++) {
260 ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
261 &ad714x->amb_reg[i], 1);
262
263 ad714x->sensor_val[i] =
264 abs(ad714x->adc_reg[i] - ad714x->amb_reg[i]);
Bryan Wu31a62962010-03-21 23:23:24 -0700265 }
266}
267
268static void ad714x_slider_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
269{
270 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
271 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
272
273 sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
274 hw->end_stage);
275
276 dev_dbg(ad714x->dev, "slider %d highest_stage:%d\n", idx,
277 sw->highest_stage);
278}
279
280/*
281 * The formulae are very straight forward. It uses the sensor with the
282 * highest response and the 2 adjacent ones.
283 * When Sensor 0 has the highest response, only sensor 0 and sensor 1
284 * are used in the calculations. Similarly when the last sensor has the
285 * highest response, only the last sensor and the second last sensors
286 * are used in the calculations.
287 *
288 * For i= idx_of_peak_Sensor-1 to i= idx_of_peak_Sensor+1
289 * v += Sensor response(i)*i
290 * w += Sensor response(i)
291 * POS=(Number_of_Positions_Wanted/(Number_of_Sensors_Used-1)) *(v/w)
292 */
293static void ad714x_slider_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
294{
295 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
296 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
297
298 sw->abs_pos = ad714x_cal_abs_pos(ad714x, hw->start_stage, hw->end_stage,
299 sw->highest_stage, hw->max_coord);
300
301 dev_dbg(ad714x->dev, "slider %d absolute position:%d\n", idx,
302 sw->abs_pos);
303}
304
305/*
306 * To minimise the Impact of the noise on the algorithm, ADI developed a
307 * routine that filters the CDC results after they have been read by the
308 * host processor.
309 * The filter used is an Infinite Input Response(IIR) filter implemented
310 * in firmware and attenuates the noise on the CDC results after they've
311 * been read by the host processor.
312 * Filtered_CDC_result = (Filtered_CDC_result * (10 - Coefficient) +
313 * Latest_CDC_result * Coefficient)/10
314 */
315static void ad714x_slider_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
316{
317 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
318
319 sw->flt_pos = (sw->flt_pos * (10 - 4) +
320 sw->abs_pos * 4)/10;
321
322 dev_dbg(ad714x->dev, "slider %d filter position:%d\n", idx,
323 sw->flt_pos);
324}
325
326static void ad714x_slider_use_com_int(struct ad714x_chip *ad714x, int idx)
327{
328 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
329
330 ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
331}
332
333static void ad714x_slider_use_thr_int(struct ad714x_chip *ad714x, int idx)
334{
335 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
336
337 ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
338}
339
340static void ad714x_slider_state_machine(struct ad714x_chip *ad714x, int idx)
341{
342 struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
343 struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
344 unsigned short h_state, c_state;
345 unsigned short mask;
346
347 mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
348
349 h_state = ad714x->h_state & mask;
350 c_state = ad714x->c_state & mask;
351
352 switch (sw->state) {
353 case IDLE:
354 if (h_state) {
355 sw->state = JITTER;
356 /* In End of Conversion interrupt mode, the AD714X
357 * continuously generates hardware interrupts.
358 */
359 ad714x_slider_use_com_int(ad714x, idx);
360 dev_dbg(ad714x->dev, "slider %d touched\n", idx);
361 }
362 break;
363
364 case JITTER:
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);
369 sw->flt_pos = sw->abs_pos;
370 sw->state = ACTIVE;
371 }
372 break;
373
374 case ACTIVE:
375 if (c_state == mask) {
376 if (h_state) {
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);
Bryan Wu31a62962010-03-21 23:23:24 -0700381 input_report_abs(sw->input, ABS_X, sw->flt_pos);
382 input_report_key(sw->input, BTN_TOUCH, 1);
383 } else {
384 /* When the user lifts off the sensor, configure
385 * the AD714X back to threshold interrupt mode.
386 */
387 ad714x_slider_use_thr_int(ad714x, idx);
388 sw->state = IDLE;
389 input_report_key(sw->input, BTN_TOUCH, 0);
390 dev_dbg(ad714x->dev, "slider %d released\n",
391 idx);
392 }
393 input_sync(sw->input);
394 }
395 break;
396
397 default:
398 break;
399 }
400}
401
402/*
403 * When the scroll wheel is activated, we compute the absolute position based
404 * on the sensor values. To calculate the position, we first determine the
405 * sensor that has the greatest response among the 8 sensors that constitutes
406 * the scrollwheel. Then we determined the 2 sensors on either sides of the
407 * sensor with the highest response and we apply weights to these sensors.
408 */
409static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
410{
411 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
412 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
413
414 sw->pre_highest_stage = sw->highest_stage;
415 sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
416 hw->end_stage);
417
418 dev_dbg(ad714x->dev, "wheel %d highest_stage:%d\n", idx,
419 sw->highest_stage);
420}
421
422static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
423{
424 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
425 int i;
426
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700427 ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
428 &ad714x->adc_reg[hw->start_stage],
429 hw->end_stage - hw->start_stage + 1);
430
Bryan Wu31a62962010-03-21 23:23:24 -0700431 for (i = hw->start_stage; i <= hw->end_stage; i++) {
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700432 ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700433 &ad714x->amb_reg[i], 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700434 if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700435 ad714x->sensor_val[i] =
436 ad714x->adc_reg[i] - ad714x->amb_reg[i];
Bryan Wu31a62962010-03-21 23:23:24 -0700437 else
438 ad714x->sensor_val[i] = 0;
439 }
440}
441
442/*
443 * When the scroll wheel is activated, we compute the absolute position based
444 * on the sensor values. To calculate the position, we first determine the
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700445 * sensor that has the greatest response among the sensors that constitutes
446 * the scrollwheel. Then we determined the sensors on either sides of the
Bryan Wu31a62962010-03-21 23:23:24 -0700447 * sensor with the highest response and we apply weights to these sensors. The
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700448 * result of this computation gives us the mean value.
Bryan Wu31a62962010-03-21 23:23:24 -0700449 */
450
Bryan Wu31a62962010-03-21 23:23:24 -0700451static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
452{
453 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
454 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
455 int stage_num = hw->end_stage - hw->start_stage + 1;
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700456 int first_before, highest, first_after;
Bryan Wu31a62962010-03-21 23:23:24 -0700457 int a_param, b_param;
458
Bryan Wu31a62962010-03-21 23:23:24 -0700459 first_before = (sw->highest_stage + stage_num - 1) % stage_num;
460 highest = sw->highest_stage;
461 first_after = (sw->highest_stage + stage_num + 1) % stage_num;
Bryan Wu31a62962010-03-21 23:23:24 -0700462
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700463 a_param = ad714x->sensor_val[highest] *
464 (highest - hw->start_stage) +
465 ad714x->sensor_val[first_before] *
466 (highest - hw->start_stage - 1) +
467 ad714x->sensor_val[first_after] *
468 (highest - hw->start_stage + 1);
469 b_param = ad714x->sensor_val[highest] +
Bryan Wu31a62962010-03-21 23:23:24 -0700470 ad714x->sensor_val[first_before] +
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700471 ad714x->sensor_val[first_after];
Bryan Wu31a62962010-03-21 23:23:24 -0700472
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700473 sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *
474 a_param) / b_param;
Bryan Wu31a62962010-03-21 23:23:24 -0700475
Bryan Wu31a62962010-03-21 23:23:24 -0700476 if (sw->abs_pos > hw->max_coord)
477 sw->abs_pos = hw->max_coord;
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700478 else if (sw->abs_pos < 0)
479 sw->abs_pos = 0;
Bryan Wu31a62962010-03-21 23:23:24 -0700480}
481
482static void ad714x_wheel_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
483{
484 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
485 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
486 if (((sw->pre_highest_stage == hw->end_stage) &&
487 (sw->highest_stage == hw->start_stage)) ||
488 ((sw->pre_highest_stage == hw->start_stage) &&
489 (sw->highest_stage == hw->end_stage)))
490 sw->flt_pos = sw->abs_pos;
491 else
492 sw->flt_pos = ((sw->flt_pos * 30) + (sw->abs_pos * 71)) / 100;
493
494 if (sw->flt_pos > hw->max_coord)
495 sw->flt_pos = hw->max_coord;
496}
497
498static void ad714x_wheel_use_com_int(struct ad714x_chip *ad714x, int idx)
499{
500 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
501
502 ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
503}
504
505static void ad714x_wheel_use_thr_int(struct ad714x_chip *ad714x, int idx)
506{
507 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
508
509 ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
510}
511
512static void ad714x_wheel_state_machine(struct ad714x_chip *ad714x, int idx)
513{
514 struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
515 struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
516 unsigned short h_state, c_state;
517 unsigned short mask;
518
519 mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
520
521 h_state = ad714x->h_state & mask;
522 c_state = ad714x->c_state & mask;
523
524 switch (sw->state) {
525 case IDLE:
526 if (h_state) {
527 sw->state = JITTER;
528 /* In End of Conversion interrupt mode, the AD714X
529 * continuously generates hardware interrupts.
530 */
531 ad714x_wheel_use_com_int(ad714x, idx);
532 dev_dbg(ad714x->dev, "wheel %d touched\n", idx);
533 }
534 break;
535
536 case JITTER:
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);
541 sw->flt_pos = sw->abs_pos;
542 sw->state = ACTIVE;
543 }
544 break;
545
546 case ACTIVE:
547 if (c_state == mask) {
548 if (h_state) {
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);
Bryan Wu31a62962010-03-21 23:23:24 -0700553 input_report_abs(sw->input, ABS_WHEEL,
Michael Hennerichf1e430e2011-05-16 23:17:40 -0700554 sw->flt_pos);
Bryan Wu31a62962010-03-21 23:23:24 -0700555 input_report_key(sw->input, BTN_TOUCH, 1);
556 } else {
557 /* When the user lifts off the sensor, configure
558 * the AD714X back to threshold interrupt mode.
559 */
560 ad714x_wheel_use_thr_int(ad714x, idx);
561 sw->state = IDLE;
562 input_report_key(sw->input, BTN_TOUCH, 0);
563
564 dev_dbg(ad714x->dev, "wheel %d released\n",
565 idx);
566 }
567 input_sync(sw->input);
568 }
569 break;
570
571 default:
572 break;
573 }
574}
575
576static void touchpad_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
577{
578 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
579 int i;
580
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700581 ad714x->read(ad714x, CDC_RESULT_S0 + hw->x_start_stage,
582 &ad714x->adc_reg[hw->x_start_stage],
583 hw->x_end_stage - hw->x_start_stage + 1);
584
Bryan Wu31a62962010-03-21 23:23:24 -0700585 for (i = hw->x_start_stage; i <= hw->x_end_stage; i++) {
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700586 ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700587 &ad714x->amb_reg[i], 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700588 if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700589 ad714x->sensor_val[i] =
590 ad714x->adc_reg[i] - ad714x->amb_reg[i];
Bryan Wu31a62962010-03-21 23:23:24 -0700591 else
592 ad714x->sensor_val[i] = 0;
593 }
594}
595
596static void touchpad_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
597{
598 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
599 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
600
601 sw->x_highest_stage = ad714x_cal_highest_stage(ad714x,
602 hw->x_start_stage, hw->x_end_stage);
603 sw->y_highest_stage = ad714x_cal_highest_stage(ad714x,
604 hw->y_start_stage, hw->y_end_stage);
605
606 dev_dbg(ad714x->dev,
607 "touchpad %d x_highest_stage:%d, y_highest_stage:%d\n",
608 idx, sw->x_highest_stage, sw->y_highest_stage);
609}
610
611/*
612 * If 2 fingers are touching the sensor then 2 peaks can be observed in the
613 * distribution.
614 * The arithmetic doesn't support to get absolute coordinates for multi-touch
615 * yet.
616 */
617static int touchpad_check_second_peak(struct ad714x_chip *ad714x, int idx)
618{
619 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
620 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
621 int i;
622
623 for (i = hw->x_start_stage; i < sw->x_highest_stage; i++) {
624 if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
625 > (ad714x->sensor_val[i + 1] / 10))
626 return 1;
627 }
628
629 for (i = sw->x_highest_stage; i < hw->x_end_stage; i++) {
630 if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
631 > (ad714x->sensor_val[i] / 10))
632 return 1;
633 }
634
635 for (i = hw->y_start_stage; i < sw->y_highest_stage; i++) {
636 if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
637 > (ad714x->sensor_val[i + 1] / 10))
638 return 1;
639 }
640
641 for (i = sw->y_highest_stage; i < hw->y_end_stage; i++) {
642 if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
643 > (ad714x->sensor_val[i] / 10))
644 return 1;
645 }
646
647 return 0;
648}
649
650/*
651 * If only one finger is used to activate the touch pad then only 1 peak will be
652 * registered in the distribution. This peak and the 2 adjacent sensors will be
653 * used in the calculation of the absolute position. This will prevent hand
654 * shadows to affect the absolute position calculation.
655 */
656static void touchpad_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
657{
658 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
659 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
660
661 sw->x_abs_pos = ad714x_cal_abs_pos(ad714x, hw->x_start_stage,
662 hw->x_end_stage, sw->x_highest_stage, hw->x_max_coord);
663 sw->y_abs_pos = ad714x_cal_abs_pos(ad714x, hw->y_start_stage,
664 hw->y_end_stage, sw->y_highest_stage, hw->y_max_coord);
665
666 dev_dbg(ad714x->dev, "touchpad %d absolute position:(%d, %d)\n", idx,
667 sw->x_abs_pos, sw->y_abs_pos);
668}
669
670static void touchpad_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
671{
672 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
673
674 sw->x_flt_pos = (sw->x_flt_pos * (10 - 4) +
675 sw->x_abs_pos * 4)/10;
676 sw->y_flt_pos = (sw->y_flt_pos * (10 - 4) +
677 sw->y_abs_pos * 4)/10;
678
679 dev_dbg(ad714x->dev, "touchpad %d filter position:(%d, %d)\n",
680 idx, sw->x_flt_pos, sw->y_flt_pos);
681}
682
683/*
684 * To prevent distortion from showing in the absolute position, it is
685 * necessary to detect the end points. When endpoints are detected, the
686 * driver stops updating the status variables with absolute positions.
687 * End points are detected on the 4 edges of the touchpad sensor. The
688 * method to detect them is the same for all 4.
689 * To detect the end points, the firmware computes the difference in
690 * percent between the sensor on the edge and the adjacent one. The
691 * difference is calculated in percent in order to make the end point
692 * detection independent of the pressure.
693 */
694
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
701static int touchpad_check_endpoint(struct ad714x_chip *ad714x, int idx)
702{
703 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
704 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
705 int percent_sensor_diff;
706
707 /* left endpoint detect */
708 percent_sensor_diff = (ad714x->sensor_val[hw->x_start_stage] -
709 ad714x->sensor_val[hw->x_start_stage + 1]) * 100 /
710 ad714x->sensor_val[hw->x_start_stage + 1];
711 if (!sw->left_ep) {
712 if (percent_sensor_diff >= LEFT_END_POINT_DETECTION_LEVEL) {
713 sw->left_ep = 1;
714 sw->left_ep_val =
715 ad714x->sensor_val[hw->x_start_stage + 1];
716 }
717 } else {
718 if ((percent_sensor_diff < LEFT_END_POINT_DETECTION_LEVEL) &&
719 (ad714x->sensor_val[hw->x_start_stage + 1] >
720 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->left_ep_val))
721 sw->left_ep = 0;
722 }
723
724 /* right endpoint detect */
725 percent_sensor_diff = (ad714x->sensor_val[hw->x_end_stage] -
726 ad714x->sensor_val[hw->x_end_stage - 1]) * 100 /
727 ad714x->sensor_val[hw->x_end_stage - 1];
728 if (!sw->right_ep) {
729 if (percent_sensor_diff >= RIGHT_END_POINT_DETECTION_LEVEL) {
730 sw->right_ep = 1;
731 sw->right_ep_val =
732 ad714x->sensor_val[hw->x_end_stage - 1];
733 }
734 } else {
735 if ((percent_sensor_diff < RIGHT_END_POINT_DETECTION_LEVEL) &&
736 (ad714x->sensor_val[hw->x_end_stage - 1] >
737 LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->right_ep_val))
738 sw->right_ep = 0;
739 }
740
741 /* top endpoint detect */
742 percent_sensor_diff = (ad714x->sensor_val[hw->y_start_stage] -
743 ad714x->sensor_val[hw->y_start_stage + 1]) * 100 /
744 ad714x->sensor_val[hw->y_start_stage + 1];
745 if (!sw->top_ep) {
746 if (percent_sensor_diff >= TOP_END_POINT_DETECTION_LEVEL) {
747 sw->top_ep = 1;
748 sw->top_ep_val =
749 ad714x->sensor_val[hw->y_start_stage + 1];
750 }
751 } else {
752 if ((percent_sensor_diff < TOP_END_POINT_DETECTION_LEVEL) &&
753 (ad714x->sensor_val[hw->y_start_stage + 1] >
754 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->top_ep_val))
755 sw->top_ep = 0;
756 }
757
758 /* bottom endpoint detect */
759 percent_sensor_diff = (ad714x->sensor_val[hw->y_end_stage] -
760 ad714x->sensor_val[hw->y_end_stage - 1]) * 100 /
761 ad714x->sensor_val[hw->y_end_stage - 1];
762 if (!sw->bottom_ep) {
763 if (percent_sensor_diff >= BOTTOM_END_POINT_DETECTION_LEVEL) {
764 sw->bottom_ep = 1;
765 sw->bottom_ep_val =
766 ad714x->sensor_val[hw->y_end_stage - 1];
767 }
768 } else {
769 if ((percent_sensor_diff < BOTTOM_END_POINT_DETECTION_LEVEL) &&
770 (ad714x->sensor_val[hw->y_end_stage - 1] >
771 TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->bottom_ep_val))
772 sw->bottom_ep = 0;
773 }
774
775 return sw->left_ep || sw->right_ep || sw->top_ep || sw->bottom_ep;
776}
777
778static void touchpad_use_com_int(struct ad714x_chip *ad714x, int idx)
779{
780 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
781
782 ad714x_use_com_int(ad714x, hw->x_start_stage, hw->x_end_stage);
783}
784
785static void touchpad_use_thr_int(struct ad714x_chip *ad714x, int idx)
786{
787 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
788
789 ad714x_use_thr_int(ad714x, hw->x_start_stage, hw->x_end_stage);
790 ad714x_use_thr_int(ad714x, hw->y_start_stage, hw->y_end_stage);
791}
792
793static void ad714x_touchpad_state_machine(struct ad714x_chip *ad714x, int idx)
794{
795 struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
796 struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
797 unsigned short h_state, c_state;
798 unsigned short mask;
799
800 mask = (((1 << (hw->x_end_stage + 1)) - 1) -
801 ((1 << hw->x_start_stage) - 1)) +
802 (((1 << (hw->y_end_stage + 1)) - 1) -
803 ((1 << hw->y_start_stage) - 1));
804
805 h_state = ad714x->h_state & mask;
806 c_state = ad714x->c_state & mask;
807
808 switch (sw->state) {
809 case IDLE:
810 if (h_state) {
811 sw->state = JITTER;
812 /* In End of Conversion interrupt mode, the AD714X
813 * continuously generates hardware interrupts.
814 */
815 touchpad_use_com_int(ad714x, idx);
816 dev_dbg(ad714x->dev, "touchpad %d touched\n", idx);
817 }
818 break;
819
820 case JITTER:
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))) {
826 dev_dbg(ad714x->dev,
827 "touchpad%d, 2 fingers or endpoint\n",
828 idx);
829 touchpad_cal_abs_pos(ad714x, idx);
830 sw->x_flt_pos = sw->x_abs_pos;
831 sw->y_flt_pos = sw->y_abs_pos;
832 sw->state = ACTIVE;
833 }
834 }
835 break;
836
837 case ACTIVE:
838 if (c_state == mask) {
839 if (h_state) {
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);
846 input_report_abs(sw->input, ABS_X,
847 sw->x_flt_pos);
848 input_report_abs(sw->input, ABS_Y,
849 sw->y_flt_pos);
850 input_report_key(sw->input, BTN_TOUCH,
851 1);
852 }
853 } else {
854 /* When the user lifts off the sensor, configure
855 * the AD714X back to threshold interrupt mode.
856 */
857 touchpad_use_thr_int(ad714x, idx);
858 sw->state = IDLE;
859 input_report_key(sw->input, BTN_TOUCH, 0);
860 dev_dbg(ad714x->dev, "touchpad %d released\n",
861 idx);
862 }
863 input_sync(sw->input);
864 }
865 break;
866
867 default:
868 break;
869 }
870}
871
872static int ad714x_hw_detect(struct ad714x_chip *ad714x)
873{
874 unsigned short data;
875
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700876 ad714x->read(ad714x, AD714X_PARTID_REG, &data, 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700877 switch (data & 0xFFF0) {
Bryan Wu31a62962010-03-21 23:23:24 -0700878 case AD7142_PARTID:
879 ad714x->product = 0x7142;
880 ad714x->version = data & 0xF;
881 dev_info(ad714x->dev, "found AD7142 captouch, rev:%d\n",
882 ad714x->version);
883 return 0;
884
Barry Song6c04d7b2010-03-21 23:23:29 -0700885 case AD7143_PARTID:
886 ad714x->product = 0x7143;
887 ad714x->version = data & 0xF;
888 dev_info(ad714x->dev, "found AD7143 captouch, rev:%d\n",
889 ad714x->version);
890 return 0;
891
892 case AD7147_PARTID:
893 ad714x->product = 0x7147;
894 ad714x->version = data & 0xF;
895 dev_info(ad714x->dev, "found AD7147(A) captouch, rev:%d\n",
896 ad714x->version);
897 return 0;
898
899 case AD7148_PARTID:
900 ad714x->product = 0x7148;
901 ad714x->version = data & 0xF;
902 dev_info(ad714x->dev, "found AD7148 captouch, rev:%d\n",
903 ad714x->version);
904 return 0;
905
Bryan Wu31a62962010-03-21 23:23:24 -0700906 default:
907 dev_err(ad714x->dev,
908 "fail to detect AD714X captouch, read ID is %04x\n",
909 data);
910 return -ENODEV;
911 }
912}
913
914static void ad714x_hw_init(struct ad714x_chip *ad714x)
915{
916 int i, j;
917 unsigned short reg_base;
918 unsigned short data;
919
920 /* configuration CDC and interrupts */
921
922 for (i = 0; i < STAGE_NUM; i++) {
923 reg_base = AD714X_STAGECFG_REG + i * STAGE_CFGREG_NUM;
924 for (j = 0; j < STAGE_CFGREG_NUM; j++)
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700925 ad714x->write(ad714x, reg_base + j,
Bryan Wu31a62962010-03-21 23:23:24 -0700926 ad714x->hw->stage_cfg_reg[i][j]);
927 }
928
929 for (i = 0; i < SYS_CFGREG_NUM; i++)
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700930 ad714x->write(ad714x, AD714X_SYSCFG_REG + i,
Bryan Wu31a62962010-03-21 23:23:24 -0700931 ad714x->hw->sys_cfg_reg[i]);
932 for (i = 0; i < SYS_CFGREG_NUM; i++)
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700933 ad714x->read(ad714x, AD714X_SYSCFG_REG + i, &data, 1);
Bryan Wu31a62962010-03-21 23:23:24 -0700934
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -0700935 ad714x->write(ad714x, AD714X_STG_CAL_EN_REG, 0xFFF);
Bryan Wu31a62962010-03-21 23:23:24 -0700936
937 /* clear all interrupts */
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700938 ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
Bryan Wu31a62962010-03-21 23:23:24 -0700939}
940
941static irqreturn_t ad714x_interrupt_thread(int irq, void *data)
942{
943 struct ad714x_chip *ad714x = data;
944 int i;
945
946 mutex_lock(&ad714x->mutex);
947
Michael Hennerich9eff794b72011-08-22 09:45:42 -0700948 ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
Bryan Wu31a62962010-03-21 23:23:24 -0700949
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);
958
959 mutex_unlock(&ad714x->mutex);
960
961 return IRQ_HANDLED;
962}
963
964#define MAX_DEVICE_NUM 8
965struct ad714x_chip *ad714x_probe(struct device *dev, u16 bus_type, int irq,
966 ad714x_read_t read, ad714x_write_t write)
967{
968 int i, alloc_idx;
969 int error;
970 struct input_dev *input[MAX_DEVICE_NUM];
971
972 struct ad714x_platform_data *plat_data = dev->platform_data;
973 struct ad714x_chip *ad714x;
974 void *drv_mem;
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -0700975 unsigned long irqflags;
Bryan Wu31a62962010-03-21 23:23:24 -0700976
977 struct ad714x_button_drv *bt_drv;
978 struct ad714x_slider_drv *sd_drv;
979 struct ad714x_wheel_drv *wl_drv;
980 struct ad714x_touchpad_drv *tp_drv;
981
982
983 if (irq <= 0) {
984 dev_err(dev, "IRQ not configured!\n");
985 error = -EINVAL;
986 goto err_out;
987 }
988
989 if (dev->platform_data == NULL) {
990 dev_err(dev, "platform data for ad714x doesn't exist\n");
991 error = -EINVAL;
992 goto err_out;
993 }
994
995 ad714x = kzalloc(sizeof(*ad714x) + sizeof(*ad714x->sw) +
996 sizeof(*sd_drv) * plat_data->slider_num +
997 sizeof(*wl_drv) * plat_data->wheel_num +
998 sizeof(*tp_drv) * plat_data->touchpad_num +
999 sizeof(*bt_drv) * plat_data->button_num, GFP_KERNEL);
1000 if (!ad714x) {
1001 error = -ENOMEM;
1002 goto err_out;
1003 }
1004
1005 ad714x->hw = plat_data;
1006
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;
1018
1019 ad714x->read = read;
1020 ad714x->write = write;
1021 ad714x->irq = irq;
1022 ad714x->dev = dev;
1023
1024 error = ad714x_hw_detect(ad714x);
1025 if (error)
1026 goto err_free_mem;
1027
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001028 /* initialize and request sw/hw resources */
Bryan Wu31a62962010-03-21 23:23:24 -07001029
1030 ad714x_hw_init(ad714x);
1031 mutex_init(&ad714x->mutex);
1032
1033 /*
1034 * Allocate and register AD714X input device
1035 */
1036 alloc_idx = 0;
1037
1038 /* a slider uses one input_dev instance */
1039 if (ad714x->hw->slider_num > 0) {
1040 struct ad714x_slider_plat *sd_plat = ad714x->hw->slider;
1041
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]) {
1045 error = -ENOMEM;
1046 goto err_free_dev;
1047 }
1048
1049 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1050 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1051 __set_bit(ABS_X, input[alloc_idx]->absbit);
1052 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1053 input_set_abs_params(input[alloc_idx],
1054 ABS_X, 0, sd_plat->max_coord, 0, 0);
1055
1056 input[alloc_idx]->id.bustype = bus_type;
1057 input[alloc_idx]->id.product = ad714x->product;
1058 input[alloc_idx]->id.version = ad714x->version;
Michael Hennerichdc5f4f52011-05-16 23:17:11 -07001059 input[alloc_idx]->name = "ad714x_captouch_slider";
1060 input[alloc_idx]->dev.parent = dev;
Bryan Wu31a62962010-03-21 23:23:24 -07001061
1062 error = input_register_device(input[alloc_idx]);
1063 if (error)
1064 goto err_free_dev;
1065
1066 alloc_idx++;
1067 }
1068 }
1069
1070 /* a wheel uses one input_dev instance */
1071 if (ad714x->hw->wheel_num > 0) {
1072 struct ad714x_wheel_plat *wl_plat = ad714x->hw->wheel;
1073
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]) {
1077 error = -ENOMEM;
1078 goto err_free_dev;
1079 }
1080
1081 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1082 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1083 __set_bit(ABS_WHEEL, input[alloc_idx]->absbit);
1084 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1085 input_set_abs_params(input[alloc_idx],
1086 ABS_WHEEL, 0, wl_plat->max_coord, 0, 0);
1087
1088 input[alloc_idx]->id.bustype = bus_type;
1089 input[alloc_idx]->id.product = ad714x->product;
1090 input[alloc_idx]->id.version = ad714x->version;
Michael Hennerichdc5f4f52011-05-16 23:17:11 -07001091 input[alloc_idx]->name = "ad714x_captouch_wheel";
1092 input[alloc_idx]->dev.parent = dev;
Bryan Wu31a62962010-03-21 23:23:24 -07001093
1094 error = input_register_device(input[alloc_idx]);
1095 if (error)
1096 goto err_free_dev;
1097
1098 alloc_idx++;
1099 }
1100 }
1101
1102 /* a touchpad uses one input_dev instance */
1103 if (ad714x->hw->touchpad_num > 0) {
1104 struct ad714x_touchpad_plat *tp_plat = ad714x->hw->touchpad;
1105
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]) {
1109 error = -ENOMEM;
1110 goto err_free_dev;
1111 }
1112
1113 __set_bit(EV_ABS, input[alloc_idx]->evbit);
1114 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1115 __set_bit(ABS_X, input[alloc_idx]->absbit);
1116 __set_bit(ABS_Y, input[alloc_idx]->absbit);
1117 __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
1118 input_set_abs_params(input[alloc_idx],
1119 ABS_X, 0, tp_plat->x_max_coord, 0, 0);
1120 input_set_abs_params(input[alloc_idx],
1121 ABS_Y, 0, tp_plat->y_max_coord, 0, 0);
1122
1123 input[alloc_idx]->id.bustype = bus_type;
1124 input[alloc_idx]->id.product = ad714x->product;
1125 input[alloc_idx]->id.version = ad714x->version;
Michael Hennerichdc5f4f52011-05-16 23:17:11 -07001126 input[alloc_idx]->name = "ad714x_captouch_pad";
1127 input[alloc_idx]->dev.parent = dev;
Bryan Wu31a62962010-03-21 23:23:24 -07001128
1129 error = input_register_device(input[alloc_idx]);
1130 if (error)
1131 goto err_free_dev;
1132
1133 alloc_idx++;
1134 }
1135 }
1136
1137 /* all buttons use one input node */
1138 if (ad714x->hw->button_num > 0) {
1139 struct ad714x_button_plat *bt_plat = ad714x->hw->button;
1140
1141 input[alloc_idx] = input_allocate_device();
1142 if (!input[alloc_idx]) {
1143 error = -ENOMEM;
1144 goto err_free_dev;
1145 }
1146
1147 __set_bit(EV_KEY, input[alloc_idx]->evbit);
1148 for (i = 0; i < ad714x->hw->button_num; i++) {
1149 bt_drv[i].input = input[alloc_idx];
1150 __set_bit(bt_plat[i].keycode, input[alloc_idx]->keybit);
1151 }
1152
1153 input[alloc_idx]->id.bustype = bus_type;
1154 input[alloc_idx]->id.product = ad714x->product;
1155 input[alloc_idx]->id.version = ad714x->version;
Michael Hennerichdc5f4f52011-05-16 23:17:11 -07001156 input[alloc_idx]->name = "ad714x_captouch_button";
1157 input[alloc_idx]->dev.parent = dev;
Bryan Wu31a62962010-03-21 23:23:24 -07001158
1159 error = input_register_device(input[alloc_idx]);
1160 if (error)
1161 goto err_free_dev;
1162
1163 alloc_idx++;
1164 }
1165
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001166 irqflags = plat_data->irqflags ?: IRQF_TRIGGER_FALLING;
1167 irqflags |= IRQF_ONESHOT;
1168
Bryan Wu31a62962010-03-21 23:23:24 -07001169 error = request_threaded_irq(ad714x->irq, NULL, ad714x_interrupt_thread,
Lars-Peter Clausen9b7e31b2012-07-04 13:02:56 -07001170 irqflags, "ad714x_captouch", ad714x);
Bryan Wu31a62962010-03-21 23:23:24 -07001171 if (error) {
1172 dev_err(dev, "can't allocate irq %d\n", ad714x->irq);
1173 goto err_unreg_dev;
1174 }
1175
1176 return ad714x;
1177
1178 err_free_dev:
1179 dev_err(dev, "failed to setup AD714x input device %i\n", alloc_idx);
1180 input_free_device(input[alloc_idx]);
1181 err_unreg_dev:
1182 while (--alloc_idx >= 0)
1183 input_unregister_device(input[alloc_idx]);
1184 err_free_mem:
1185 kfree(ad714x);
1186 err_out:
1187 return ERR_PTR(error);
1188}
1189EXPORT_SYMBOL(ad714x_probe);
1190
1191void ad714x_remove(struct ad714x_chip *ad714x)
1192{
1193 struct ad714x_platform_data *hw = ad714x->hw;
1194 struct ad714x_driver_data *sw = ad714x->sw;
1195 int i;
1196
1197 free_irq(ad714x->irq, ad714x);
1198
1199 /* unregister and free all input devices */
1200
1201 for (i = 0; i < hw->slider_num; i++)
1202 input_unregister_device(sw->slider[i].input);
1203
1204 for (i = 0; i < hw->wheel_num; i++)
1205 input_unregister_device(sw->wheel[i].input);
1206
1207 for (i = 0; i < hw->touchpad_num; i++)
1208 input_unregister_device(sw->touchpad[i].input);
1209
1210 if (hw->button_num)
1211 input_unregister_device(sw->button[0].input);
1212
1213 kfree(ad714x);
1214}
1215EXPORT_SYMBOL(ad714x_remove);
1216
1217#ifdef CONFIG_PM
1218int ad714x_disable(struct ad714x_chip *ad714x)
1219{
1220 unsigned short data;
1221
1222 dev_dbg(ad714x->dev, "%s enter\n", __func__);
1223
1224 mutex_lock(&ad714x->mutex);
1225
1226 data = ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL] | 0x3;
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -07001227 ad714x->write(ad714x, AD714X_PWR_CTRL, data);
Bryan Wu31a62962010-03-21 23:23:24 -07001228
1229 mutex_unlock(&ad714x->mutex);
1230
1231 return 0;
1232}
1233EXPORT_SYMBOL(ad714x_disable);
1234
1235int ad714x_enable(struct ad714x_chip *ad714x)
1236{
Bryan Wu31a62962010-03-21 23:23:24 -07001237 dev_dbg(ad714x->dev, "%s enter\n", __func__);
1238
1239 mutex_lock(&ad714x->mutex);
1240
1241 /* resume to non-shutdown mode */
1242
Dmitry Torokhovc0409fe2011-08-22 09:45:39 -07001243 ad714x->write(ad714x, AD714X_PWR_CTRL,
Bryan Wu31a62962010-03-21 23:23:24 -07001244 ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL]);
1245
1246 /* make sure the interrupt output line is not low level after resume,
1247 * otherwise we will get no chance to enter falling-edge irq again
1248 */
1249
Michael Hennerich9eff794b72011-08-22 09:45:42 -07001250 ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
Bryan Wu31a62962010-03-21 23:23:24 -07001251
1252 mutex_unlock(&ad714x->mutex);
1253
1254 return 0;
1255}
1256EXPORT_SYMBOL(ad714x_enable);
1257#endif
1258
1259MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
1260MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
1261MODULE_LICENSE("GPL");