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