ad714x.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * AD714X CapTouch Programmable Controller driver supporting AD7142/3/7/8/7A
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/input.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/slab.h>
  12. #include <linux/input/ad714x.h>
  13. #include <linux/module.h>
  14. #include "ad714x.h"
  15. #define AD714X_PWR_CTRL 0x0
  16. #define AD714X_STG_CAL_EN_REG 0x1
  17. #define AD714X_AMB_COMP_CTRL0_REG 0x2
  18. #define AD714X_PARTID_REG 0x17
  19. #define AD7142_PARTID 0xE620
  20. #define AD7143_PARTID 0xE630
  21. #define AD7147_PARTID 0x1470
  22. #define AD7148_PARTID 0x1480
  23. #define AD714X_STAGECFG_REG 0x80
  24. #define AD714X_SYSCFG_REG 0x0
  25. #define STG_LOW_INT_EN_REG 0x5
  26. #define STG_HIGH_INT_EN_REG 0x6
  27. #define STG_COM_INT_EN_REG 0x7
  28. #define STG_LOW_INT_STA_REG 0x8
  29. #define STG_HIGH_INT_STA_REG 0x9
  30. #define STG_COM_INT_STA_REG 0xA
  31. #define CDC_RESULT_S0 0xB
  32. #define CDC_RESULT_S1 0xC
  33. #define CDC_RESULT_S2 0xD
  34. #define CDC_RESULT_S3 0xE
  35. #define CDC_RESULT_S4 0xF
  36. #define CDC_RESULT_S5 0x10
  37. #define CDC_RESULT_S6 0x11
  38. #define CDC_RESULT_S7 0x12
  39. #define CDC_RESULT_S8 0x13
  40. #define CDC_RESULT_S9 0x14
  41. #define CDC_RESULT_S10 0x15
  42. #define CDC_RESULT_S11 0x16
  43. #define STAGE0_AMBIENT 0xF1
  44. #define STAGE1_AMBIENT 0x115
  45. #define STAGE2_AMBIENT 0x139
  46. #define STAGE3_AMBIENT 0x15D
  47. #define STAGE4_AMBIENT 0x181
  48. #define STAGE5_AMBIENT 0x1A5
  49. #define STAGE6_AMBIENT 0x1C9
  50. #define STAGE7_AMBIENT 0x1ED
  51. #define STAGE8_AMBIENT 0x211
  52. #define STAGE9_AMBIENT 0x234
  53. #define STAGE10_AMBIENT 0x259
  54. #define STAGE11_AMBIENT 0x27D
  55. #define PER_STAGE_REG_NUM 36
  56. #define STAGE_CFGREG_NUM 8
  57. #define SYS_CFGREG_NUM 8
  58. /*
  59. * driver information which will be used to maintain the software flow
  60. */
  61. enum ad714x_device_state { IDLE, JITTER, ACTIVE, SPACE };
  62. struct ad714x_slider_drv {
  63. int highest_stage;
  64. int abs_pos;
  65. int flt_pos;
  66. enum ad714x_device_state state;
  67. struct input_dev *input;
  68. };
  69. struct ad714x_wheel_drv {
  70. int abs_pos;
  71. int flt_pos;
  72. int pre_highest_stage;
  73. int highest_stage;
  74. enum ad714x_device_state state;
  75. struct input_dev *input;
  76. };
  77. struct ad714x_touchpad_drv {
  78. int x_highest_stage;
  79. int x_flt_pos;
  80. int x_abs_pos;
  81. int y_highest_stage;
  82. int y_flt_pos;
  83. int y_abs_pos;
  84. int left_ep;
  85. int left_ep_val;
  86. int right_ep;
  87. int right_ep_val;
  88. int top_ep;
  89. int top_ep_val;
  90. int bottom_ep;
  91. int bottom_ep_val;
  92. enum ad714x_device_state state;
  93. struct input_dev *input;
  94. };
  95. struct ad714x_button_drv {
  96. enum ad714x_device_state state;
  97. /*
  98. * Unlike slider/wheel/touchpad, all buttons point to
  99. * same input_dev instance
  100. */
  101. struct input_dev *input;
  102. };
  103. struct ad714x_driver_data {
  104. struct ad714x_slider_drv *slider;
  105. struct ad714x_wheel_drv *wheel;
  106. struct ad714x_touchpad_drv *touchpad;
  107. struct ad714x_button_drv *button;
  108. };
  109. /*
  110. * information to integrate all things which will be private data
  111. * of spi/i2c device
  112. */
  113. static void ad714x_use_com_int(struct ad714x_chip *ad714x,
  114. int start_stage, int end_stage)
  115. {
  116. unsigned short data;
  117. unsigned short mask;
  118. mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
  119. ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);
  120. data |= 1 << end_stage;
  121. ad714x->write(ad714x, STG_COM_INT_EN_REG, data);
  122. ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);
  123. data &= ~mask;
  124. ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);
  125. }
  126. static void ad714x_use_thr_int(struct ad714x_chip *ad714x,
  127. int start_stage, int end_stage)
  128. {
  129. unsigned short data;
  130. unsigned short mask;
  131. mask = ((1 << (end_stage + 1)) - 1) - ((1 << start_stage) - 1);
  132. ad714x->read(ad714x, STG_COM_INT_EN_REG, &data, 1);
  133. data &= ~(1 << end_stage);
  134. ad714x->write(ad714x, STG_COM_INT_EN_REG, data);
  135. ad714x->read(ad714x, STG_HIGH_INT_EN_REG, &data, 1);
  136. data |= mask;
  137. ad714x->write(ad714x, STG_HIGH_INT_EN_REG, data);
  138. }
  139. static int ad714x_cal_highest_stage(struct ad714x_chip *ad714x,
  140. int start_stage, int end_stage)
  141. {
  142. int max_res = 0;
  143. int max_idx = 0;
  144. int i;
  145. for (i = start_stage; i <= end_stage; i++) {
  146. if (ad714x->sensor_val[i] > max_res) {
  147. max_res = ad714x->sensor_val[i];
  148. max_idx = i;
  149. }
  150. }
  151. return max_idx;
  152. }
  153. static int ad714x_cal_abs_pos(struct ad714x_chip *ad714x,
  154. int start_stage, int end_stage,
  155. int highest_stage, int max_coord)
  156. {
  157. int a_param, b_param;
  158. if (highest_stage == start_stage) {
  159. a_param = ad714x->sensor_val[start_stage + 1];
  160. b_param = ad714x->sensor_val[start_stage] +
  161. ad714x->sensor_val[start_stage + 1];
  162. } else if (highest_stage == end_stage) {
  163. a_param = ad714x->sensor_val[end_stage] *
  164. (end_stage - start_stage) +
  165. ad714x->sensor_val[end_stage - 1] *
  166. (end_stage - start_stage - 1);
  167. b_param = ad714x->sensor_val[end_stage] +
  168. ad714x->sensor_val[end_stage - 1];
  169. } else {
  170. a_param = ad714x->sensor_val[highest_stage] *
  171. (highest_stage - start_stage) +
  172. ad714x->sensor_val[highest_stage - 1] *
  173. (highest_stage - start_stage - 1) +
  174. ad714x->sensor_val[highest_stage + 1] *
  175. (highest_stage - start_stage + 1);
  176. b_param = ad714x->sensor_val[highest_stage] +
  177. ad714x->sensor_val[highest_stage - 1] +
  178. ad714x->sensor_val[highest_stage + 1];
  179. }
  180. return (max_coord / (end_stage - start_stage)) * a_param / b_param;
  181. }
  182. /*
  183. * One button can connect to multi positive and negative of CDCs
  184. * Multi-buttons can connect to same positive/negative of one CDC
  185. */
  186. static void ad714x_button_state_machine(struct ad714x_chip *ad714x, int idx)
  187. {
  188. struct ad714x_button_plat *hw = &ad714x->hw->button[idx];
  189. struct ad714x_button_drv *sw = &ad714x->sw->button[idx];
  190. switch (sw->state) {
  191. case IDLE:
  192. if (((ad714x->h_state & hw->h_mask) == hw->h_mask) &&
  193. ((ad714x->l_state & hw->l_mask) == hw->l_mask)) {
  194. dev_dbg(ad714x->dev, "button %d touched\n", idx);
  195. input_report_key(sw->input, hw->keycode, 1);
  196. input_sync(sw->input);
  197. sw->state = ACTIVE;
  198. }
  199. break;
  200. case ACTIVE:
  201. if (((ad714x->h_state & hw->h_mask) != hw->h_mask) ||
  202. ((ad714x->l_state & hw->l_mask) != hw->l_mask)) {
  203. dev_dbg(ad714x->dev, "button %d released\n", idx);
  204. input_report_key(sw->input, hw->keycode, 0);
  205. input_sync(sw->input);
  206. sw->state = IDLE;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. /*
  214. * The response of a sensor is defined by the absolute number of codes
  215. * between the current CDC value and the ambient value.
  216. */
  217. static void ad714x_slider_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
  218. {
  219. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  220. int i;
  221. ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
  222. &ad714x->adc_reg[hw->start_stage],
  223. hw->end_stage - hw->start_stage + 1);
  224. for (i = hw->start_stage; i <= hw->end_stage; i++) {
  225. ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
  226. &ad714x->amb_reg[i], 1);
  227. ad714x->sensor_val[i] =
  228. abs(ad714x->adc_reg[i] - ad714x->amb_reg[i]);
  229. }
  230. }
  231. static void ad714x_slider_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
  232. {
  233. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  234. struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
  235. sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
  236. hw->end_stage);
  237. dev_dbg(ad714x->dev, "slider %d highest_stage:%d\n", idx,
  238. sw->highest_stage);
  239. }
  240. /*
  241. * The formulae are very straight forward. It uses the sensor with the
  242. * highest response and the 2 adjacent ones.
  243. * When Sensor 0 has the highest response, only sensor 0 and sensor 1
  244. * are used in the calculations. Similarly when the last sensor has the
  245. * highest response, only the last sensor and the second last sensors
  246. * are used in the calculations.
  247. *
  248. * For i= idx_of_peak_Sensor-1 to i= idx_of_peak_Sensor+1
  249. * v += Sensor response(i)*i
  250. * w += Sensor response(i)
  251. * POS=(Number_of_Positions_Wanted/(Number_of_Sensors_Used-1)) *(v/w)
  252. */
  253. static void ad714x_slider_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
  254. {
  255. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  256. struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
  257. sw->abs_pos = ad714x_cal_abs_pos(ad714x, hw->start_stage, hw->end_stage,
  258. sw->highest_stage, hw->max_coord);
  259. dev_dbg(ad714x->dev, "slider %d absolute position:%d\n", idx,
  260. sw->abs_pos);
  261. }
  262. /*
  263. * To minimise the Impact of the noise on the algorithm, ADI developed a
  264. * routine that filters the CDC results after they have been read by the
  265. * host processor.
  266. * The filter used is an Infinite Input Response(IIR) filter implemented
  267. * in firmware and attenuates the noise on the CDC results after they've
  268. * been read by the host processor.
  269. * Filtered_CDC_result = (Filtered_CDC_result * (10 - Coefficient) +
  270. * Latest_CDC_result * Coefficient)/10
  271. */
  272. static void ad714x_slider_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
  273. {
  274. struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
  275. sw->flt_pos = (sw->flt_pos * (10 - 4) +
  276. sw->abs_pos * 4)/10;
  277. dev_dbg(ad714x->dev, "slider %d filter position:%d\n", idx,
  278. sw->flt_pos);
  279. }
  280. static void ad714x_slider_use_com_int(struct ad714x_chip *ad714x, int idx)
  281. {
  282. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  283. ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
  284. }
  285. static void ad714x_slider_use_thr_int(struct ad714x_chip *ad714x, int idx)
  286. {
  287. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  288. ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
  289. }
  290. static void ad714x_slider_state_machine(struct ad714x_chip *ad714x, int idx)
  291. {
  292. struct ad714x_slider_plat *hw = &ad714x->hw->slider[idx];
  293. struct ad714x_slider_drv *sw = &ad714x->sw->slider[idx];
  294. unsigned short h_state, c_state;
  295. unsigned short mask;
  296. mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
  297. h_state = ad714x->h_state & mask;
  298. c_state = ad714x->c_state & mask;
  299. switch (sw->state) {
  300. case IDLE:
  301. if (h_state) {
  302. sw->state = JITTER;
  303. /* In End of Conversion interrupt mode, the AD714X
  304. * continuously generates hardware interrupts.
  305. */
  306. ad714x_slider_use_com_int(ad714x, idx);
  307. dev_dbg(ad714x->dev, "slider %d touched\n", idx);
  308. }
  309. break;
  310. case JITTER:
  311. if (c_state == mask) {
  312. ad714x_slider_cal_sensor_val(ad714x, idx);
  313. ad714x_slider_cal_highest_stage(ad714x, idx);
  314. ad714x_slider_cal_abs_pos(ad714x, idx);
  315. sw->flt_pos = sw->abs_pos;
  316. sw->state = ACTIVE;
  317. }
  318. break;
  319. case ACTIVE:
  320. if (c_state == mask) {
  321. if (h_state) {
  322. ad714x_slider_cal_sensor_val(ad714x, idx);
  323. ad714x_slider_cal_highest_stage(ad714x, idx);
  324. ad714x_slider_cal_abs_pos(ad714x, idx);
  325. ad714x_slider_cal_flt_pos(ad714x, idx);
  326. input_report_abs(sw->input, ABS_X, sw->flt_pos);
  327. input_report_key(sw->input, BTN_TOUCH, 1);
  328. } else {
  329. /* When the user lifts off the sensor, configure
  330. * the AD714X back to threshold interrupt mode.
  331. */
  332. ad714x_slider_use_thr_int(ad714x, idx);
  333. sw->state = IDLE;
  334. input_report_key(sw->input, BTN_TOUCH, 0);
  335. dev_dbg(ad714x->dev, "slider %d released\n",
  336. idx);
  337. }
  338. input_sync(sw->input);
  339. }
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. /*
  346. * When the scroll wheel is activated, we compute the absolute position based
  347. * on the sensor values. To calculate the position, we first determine the
  348. * sensor that has the greatest response among the 8 sensors that constitutes
  349. * the scrollwheel. Then we determined the 2 sensors on either sides of the
  350. * sensor with the highest response and we apply weights to these sensors.
  351. */
  352. static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
  353. {
  354. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  355. struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
  356. sw->pre_highest_stage = sw->highest_stage;
  357. sw->highest_stage = ad714x_cal_highest_stage(ad714x, hw->start_stage,
  358. hw->end_stage);
  359. dev_dbg(ad714x->dev, "wheel %d highest_stage:%d\n", idx,
  360. sw->highest_stage);
  361. }
  362. static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
  363. {
  364. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  365. int i;
  366. ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
  367. &ad714x->adc_reg[hw->start_stage],
  368. hw->end_stage - hw->start_stage + 1);
  369. for (i = hw->start_stage; i <= hw->end_stage; i++) {
  370. ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
  371. &ad714x->amb_reg[i], 1);
  372. if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
  373. ad714x->sensor_val[i] =
  374. ad714x->adc_reg[i] - ad714x->amb_reg[i];
  375. else
  376. ad714x->sensor_val[i] = 0;
  377. }
  378. }
  379. /*
  380. * When the scroll wheel is activated, we compute the absolute position based
  381. * on the sensor values. To calculate the position, we first determine the
  382. * sensor that has the greatest response among the sensors that constitutes
  383. * the scrollwheel. Then we determined the sensors on either sides of the
  384. * sensor with the highest response and we apply weights to these sensors. The
  385. * result of this computation gives us the mean value.
  386. */
  387. static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
  388. {
  389. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  390. struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
  391. int stage_num = hw->end_stage - hw->start_stage + 1;
  392. int first_before, highest, first_after;
  393. int a_param, b_param;
  394. first_before = (sw->highest_stage + stage_num - 1) % stage_num;
  395. highest = sw->highest_stage;
  396. first_after = (sw->highest_stage + stage_num + 1) % stage_num;
  397. a_param = ad714x->sensor_val[highest] *
  398. (highest - hw->start_stage) +
  399. ad714x->sensor_val[first_before] *
  400. (highest - hw->start_stage - 1) +
  401. ad714x->sensor_val[first_after] *
  402. (highest - hw->start_stage + 1);
  403. b_param = ad714x->sensor_val[highest] +
  404. ad714x->sensor_val[first_before] +
  405. ad714x->sensor_val[first_after];
  406. sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *
  407. a_param) / b_param;
  408. if (sw->abs_pos > hw->max_coord)
  409. sw->abs_pos = hw->max_coord;
  410. else if (sw->abs_pos < 0)
  411. sw->abs_pos = 0;
  412. }
  413. static void ad714x_wheel_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
  414. {
  415. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  416. struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
  417. if (((sw->pre_highest_stage == hw->end_stage) &&
  418. (sw->highest_stage == hw->start_stage)) ||
  419. ((sw->pre_highest_stage == hw->start_stage) &&
  420. (sw->highest_stage == hw->end_stage)))
  421. sw->flt_pos = sw->abs_pos;
  422. else
  423. sw->flt_pos = ((sw->flt_pos * 30) + (sw->abs_pos * 71)) / 100;
  424. if (sw->flt_pos > hw->max_coord)
  425. sw->flt_pos = hw->max_coord;
  426. }
  427. static void ad714x_wheel_use_com_int(struct ad714x_chip *ad714x, int idx)
  428. {
  429. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  430. ad714x_use_com_int(ad714x, hw->start_stage, hw->end_stage);
  431. }
  432. static void ad714x_wheel_use_thr_int(struct ad714x_chip *ad714x, int idx)
  433. {
  434. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  435. ad714x_use_thr_int(ad714x, hw->start_stage, hw->end_stage);
  436. }
  437. static void ad714x_wheel_state_machine(struct ad714x_chip *ad714x, int idx)
  438. {
  439. struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
  440. struct ad714x_wheel_drv *sw = &ad714x->sw->wheel[idx];
  441. unsigned short h_state, c_state;
  442. unsigned short mask;
  443. mask = ((1 << (hw->end_stage + 1)) - 1) - ((1 << hw->start_stage) - 1);
  444. h_state = ad714x->h_state & mask;
  445. c_state = ad714x->c_state & mask;
  446. switch (sw->state) {
  447. case IDLE:
  448. if (h_state) {
  449. sw->state = JITTER;
  450. /* In End of Conversion interrupt mode, the AD714X
  451. * continuously generates hardware interrupts.
  452. */
  453. ad714x_wheel_use_com_int(ad714x, idx);
  454. dev_dbg(ad714x->dev, "wheel %d touched\n", idx);
  455. }
  456. break;
  457. case JITTER:
  458. if (c_state == mask) {
  459. ad714x_wheel_cal_sensor_val(ad714x, idx);
  460. ad714x_wheel_cal_highest_stage(ad714x, idx);
  461. ad714x_wheel_cal_abs_pos(ad714x, idx);
  462. sw->flt_pos = sw->abs_pos;
  463. sw->state = ACTIVE;
  464. }
  465. break;
  466. case ACTIVE:
  467. if (c_state == mask) {
  468. if (h_state) {
  469. ad714x_wheel_cal_sensor_val(ad714x, idx);
  470. ad714x_wheel_cal_highest_stage(ad714x, idx);
  471. ad714x_wheel_cal_abs_pos(ad714x, idx);
  472. ad714x_wheel_cal_flt_pos(ad714x, idx);
  473. input_report_abs(sw->input, ABS_WHEEL,
  474. sw->flt_pos);
  475. input_report_key(sw->input, BTN_TOUCH, 1);
  476. } else {
  477. /* When the user lifts off the sensor, configure
  478. * the AD714X back to threshold interrupt mode.
  479. */
  480. ad714x_wheel_use_thr_int(ad714x, idx);
  481. sw->state = IDLE;
  482. input_report_key(sw->input, BTN_TOUCH, 0);
  483. dev_dbg(ad714x->dev, "wheel %d released\n",
  484. idx);
  485. }
  486. input_sync(sw->input);
  487. }
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. static void touchpad_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
  494. {
  495. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  496. int i;
  497. ad714x->read(ad714x, CDC_RESULT_S0 + hw->x_start_stage,
  498. &ad714x->adc_reg[hw->x_start_stage],
  499. hw->x_end_stage - hw->x_start_stage + 1);
  500. for (i = hw->x_start_stage; i <= hw->x_end_stage; i++) {
  501. ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
  502. &ad714x->amb_reg[i], 1);
  503. if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
  504. ad714x->sensor_val[i] =
  505. ad714x->adc_reg[i] - ad714x->amb_reg[i];
  506. else
  507. ad714x->sensor_val[i] = 0;
  508. }
  509. }
  510. static void touchpad_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
  511. {
  512. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  513. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  514. sw->x_highest_stage = ad714x_cal_highest_stage(ad714x,
  515. hw->x_start_stage, hw->x_end_stage);
  516. sw->y_highest_stage = ad714x_cal_highest_stage(ad714x,
  517. hw->y_start_stage, hw->y_end_stage);
  518. dev_dbg(ad714x->dev,
  519. "touchpad %d x_highest_stage:%d, y_highest_stage:%d\n",
  520. idx, sw->x_highest_stage, sw->y_highest_stage);
  521. }
  522. /*
  523. * If 2 fingers are touching the sensor then 2 peaks can be observed in the
  524. * distribution.
  525. * The arithmetic doesn't support to get absolute coordinates for multi-touch
  526. * yet.
  527. */
  528. static int touchpad_check_second_peak(struct ad714x_chip *ad714x, int idx)
  529. {
  530. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  531. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  532. int i;
  533. for (i = hw->x_start_stage; i < sw->x_highest_stage; i++) {
  534. if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
  535. > (ad714x->sensor_val[i + 1] / 10))
  536. return 1;
  537. }
  538. for (i = sw->x_highest_stage; i < hw->x_end_stage; i++) {
  539. if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
  540. > (ad714x->sensor_val[i] / 10))
  541. return 1;
  542. }
  543. for (i = hw->y_start_stage; i < sw->y_highest_stage; i++) {
  544. if ((ad714x->sensor_val[i] - ad714x->sensor_val[i + 1])
  545. > (ad714x->sensor_val[i + 1] / 10))
  546. return 1;
  547. }
  548. for (i = sw->y_highest_stage; i < hw->y_end_stage; i++) {
  549. if ((ad714x->sensor_val[i + 1] - ad714x->sensor_val[i])
  550. > (ad714x->sensor_val[i] / 10))
  551. return 1;
  552. }
  553. return 0;
  554. }
  555. /*
  556. * If only one finger is used to activate the touch pad then only 1 peak will be
  557. * registered in the distribution. This peak and the 2 adjacent sensors will be
  558. * used in the calculation of the absolute position. This will prevent hand
  559. * shadows to affect the absolute position calculation.
  560. */
  561. static void touchpad_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
  562. {
  563. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  564. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  565. sw->x_abs_pos = ad714x_cal_abs_pos(ad714x, hw->x_start_stage,
  566. hw->x_end_stage, sw->x_highest_stage, hw->x_max_coord);
  567. sw->y_abs_pos = ad714x_cal_abs_pos(ad714x, hw->y_start_stage,
  568. hw->y_end_stage, sw->y_highest_stage, hw->y_max_coord);
  569. dev_dbg(ad714x->dev, "touchpad %d absolute position:(%d, %d)\n", idx,
  570. sw->x_abs_pos, sw->y_abs_pos);
  571. }
  572. static void touchpad_cal_flt_pos(struct ad714x_chip *ad714x, int idx)
  573. {
  574. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  575. sw->x_flt_pos = (sw->x_flt_pos * (10 - 4) +
  576. sw->x_abs_pos * 4)/10;
  577. sw->y_flt_pos = (sw->y_flt_pos * (10 - 4) +
  578. sw->y_abs_pos * 4)/10;
  579. dev_dbg(ad714x->dev, "touchpad %d filter position:(%d, %d)\n",
  580. idx, sw->x_flt_pos, sw->y_flt_pos);
  581. }
  582. /*
  583. * To prevent distortion from showing in the absolute position, it is
  584. * necessary to detect the end points. When endpoints are detected, the
  585. * driver stops updating the status variables with absolute positions.
  586. * End points are detected on the 4 edges of the touchpad sensor. The
  587. * method to detect them is the same for all 4.
  588. * To detect the end points, the firmware computes the difference in
  589. * percent between the sensor on the edge and the adjacent one. The
  590. * difference is calculated in percent in order to make the end point
  591. * detection independent of the pressure.
  592. */
  593. #define LEFT_END_POINT_DETECTION_LEVEL 550
  594. #define RIGHT_END_POINT_DETECTION_LEVEL 750
  595. #define LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL 850
  596. #define TOP_END_POINT_DETECTION_LEVEL 550
  597. #define BOTTOM_END_POINT_DETECTION_LEVEL 950
  598. #define TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL 700
  599. static int touchpad_check_endpoint(struct ad714x_chip *ad714x, int idx)
  600. {
  601. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  602. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  603. int percent_sensor_diff;
  604. /* left endpoint detect */
  605. percent_sensor_diff = (ad714x->sensor_val[hw->x_start_stage] -
  606. ad714x->sensor_val[hw->x_start_stage + 1]) * 100 /
  607. ad714x->sensor_val[hw->x_start_stage + 1];
  608. if (!sw->left_ep) {
  609. if (percent_sensor_diff >= LEFT_END_POINT_DETECTION_LEVEL) {
  610. sw->left_ep = 1;
  611. sw->left_ep_val =
  612. ad714x->sensor_val[hw->x_start_stage + 1];
  613. }
  614. } else {
  615. if ((percent_sensor_diff < LEFT_END_POINT_DETECTION_LEVEL) &&
  616. (ad714x->sensor_val[hw->x_start_stage + 1] >
  617. LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->left_ep_val))
  618. sw->left_ep = 0;
  619. }
  620. /* right endpoint detect */
  621. percent_sensor_diff = (ad714x->sensor_val[hw->x_end_stage] -
  622. ad714x->sensor_val[hw->x_end_stage - 1]) * 100 /
  623. ad714x->sensor_val[hw->x_end_stage - 1];
  624. if (!sw->right_ep) {
  625. if (percent_sensor_diff >= RIGHT_END_POINT_DETECTION_LEVEL) {
  626. sw->right_ep = 1;
  627. sw->right_ep_val =
  628. ad714x->sensor_val[hw->x_end_stage - 1];
  629. }
  630. } else {
  631. if ((percent_sensor_diff < RIGHT_END_POINT_DETECTION_LEVEL) &&
  632. (ad714x->sensor_val[hw->x_end_stage - 1] >
  633. LEFT_RIGHT_END_POINT_DEAVTIVALION_LEVEL + sw->right_ep_val))
  634. sw->right_ep = 0;
  635. }
  636. /* top endpoint detect */
  637. percent_sensor_diff = (ad714x->sensor_val[hw->y_start_stage] -
  638. ad714x->sensor_val[hw->y_start_stage + 1]) * 100 /
  639. ad714x->sensor_val[hw->y_start_stage + 1];
  640. if (!sw->top_ep) {
  641. if (percent_sensor_diff >= TOP_END_POINT_DETECTION_LEVEL) {
  642. sw->top_ep = 1;
  643. sw->top_ep_val =
  644. ad714x->sensor_val[hw->y_start_stage + 1];
  645. }
  646. } else {
  647. if ((percent_sensor_diff < TOP_END_POINT_DETECTION_LEVEL) &&
  648. (ad714x->sensor_val[hw->y_start_stage + 1] >
  649. TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->top_ep_val))
  650. sw->top_ep = 0;
  651. }
  652. /* bottom endpoint detect */
  653. percent_sensor_diff = (ad714x->sensor_val[hw->y_end_stage] -
  654. ad714x->sensor_val[hw->y_end_stage - 1]) * 100 /
  655. ad714x->sensor_val[hw->y_end_stage - 1];
  656. if (!sw->bottom_ep) {
  657. if (percent_sensor_diff >= BOTTOM_END_POINT_DETECTION_LEVEL) {
  658. sw->bottom_ep = 1;
  659. sw->bottom_ep_val =
  660. ad714x->sensor_val[hw->y_end_stage - 1];
  661. }
  662. } else {
  663. if ((percent_sensor_diff < BOTTOM_END_POINT_DETECTION_LEVEL) &&
  664. (ad714x->sensor_val[hw->y_end_stage - 1] >
  665. TOP_BOTTOM_END_POINT_DEAVTIVALION_LEVEL + sw->bottom_ep_val))
  666. sw->bottom_ep = 0;
  667. }
  668. return sw->left_ep || sw->right_ep || sw->top_ep || sw->bottom_ep;
  669. }
  670. static void touchpad_use_com_int(struct ad714x_chip *ad714x, int idx)
  671. {
  672. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  673. ad714x_use_com_int(ad714x, hw->x_start_stage, hw->x_end_stage);
  674. }
  675. static void touchpad_use_thr_int(struct ad714x_chip *ad714x, int idx)
  676. {
  677. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  678. ad714x_use_thr_int(ad714x, hw->x_start_stage, hw->x_end_stage);
  679. ad714x_use_thr_int(ad714x, hw->y_start_stage, hw->y_end_stage);
  680. }
  681. static void ad714x_touchpad_state_machine(struct ad714x_chip *ad714x, int idx)
  682. {
  683. struct ad714x_touchpad_plat *hw = &ad714x->hw->touchpad[idx];
  684. struct ad714x_touchpad_drv *sw = &ad714x->sw->touchpad[idx];
  685. unsigned short h_state, c_state;
  686. unsigned short mask;
  687. mask = (((1 << (hw->x_end_stage + 1)) - 1) -
  688. ((1 << hw->x_start_stage) - 1)) +
  689. (((1 << (hw->y_end_stage + 1)) - 1) -
  690. ((1 << hw->y_start_stage) - 1));
  691. h_state = ad714x->h_state & mask;
  692. c_state = ad714x->c_state & mask;
  693. switch (sw->state) {
  694. case IDLE:
  695. if (h_state) {
  696. sw->state = JITTER;
  697. /* In End of Conversion interrupt mode, the AD714X
  698. * continuously generates hardware interrupts.
  699. */
  700. touchpad_use_com_int(ad714x, idx);
  701. dev_dbg(ad714x->dev, "touchpad %d touched\n", idx);
  702. }
  703. break;
  704. case JITTER:
  705. if (c_state == mask) {
  706. touchpad_cal_sensor_val(ad714x, idx);
  707. touchpad_cal_highest_stage(ad714x, idx);
  708. if ((!touchpad_check_second_peak(ad714x, idx)) &&
  709. (!touchpad_check_endpoint(ad714x, idx))) {
  710. dev_dbg(ad714x->dev,
  711. "touchpad%d, 2 fingers or endpoint\n",
  712. idx);
  713. touchpad_cal_abs_pos(ad714x, idx);
  714. sw->x_flt_pos = sw->x_abs_pos;
  715. sw->y_flt_pos = sw->y_abs_pos;
  716. sw->state = ACTIVE;
  717. }
  718. }
  719. break;
  720. case ACTIVE:
  721. if (c_state == mask) {
  722. if (h_state) {
  723. touchpad_cal_sensor_val(ad714x, idx);
  724. touchpad_cal_highest_stage(ad714x, idx);
  725. if ((!touchpad_check_second_peak(ad714x, idx))
  726. && (!touchpad_check_endpoint(ad714x, idx))) {
  727. touchpad_cal_abs_pos(ad714x, idx);
  728. touchpad_cal_flt_pos(ad714x, idx);
  729. input_report_abs(sw->input, ABS_X,
  730. sw->x_flt_pos);
  731. input_report_abs(sw->input, ABS_Y,
  732. sw->y_flt_pos);
  733. input_report_key(sw->input, BTN_TOUCH,
  734. 1);
  735. }
  736. } else {
  737. /* When the user lifts off the sensor, configure
  738. * the AD714X back to threshold interrupt mode.
  739. */
  740. touchpad_use_thr_int(ad714x, idx);
  741. sw->state = IDLE;
  742. input_report_key(sw->input, BTN_TOUCH, 0);
  743. dev_dbg(ad714x->dev, "touchpad %d released\n",
  744. idx);
  745. }
  746. input_sync(sw->input);
  747. }
  748. break;
  749. default:
  750. break;
  751. }
  752. }
  753. static int ad714x_hw_detect(struct ad714x_chip *ad714x)
  754. {
  755. unsigned short data;
  756. ad714x->read(ad714x, AD714X_PARTID_REG, &data, 1);
  757. switch (data & 0xFFF0) {
  758. case AD7142_PARTID:
  759. ad714x->product = 0x7142;
  760. ad714x->version = data & 0xF;
  761. dev_info(ad714x->dev, "found AD7142 captouch, rev:%d\n",
  762. ad714x->version);
  763. return 0;
  764. case AD7143_PARTID:
  765. ad714x->product = 0x7143;
  766. ad714x->version = data & 0xF;
  767. dev_info(ad714x->dev, "found AD7143 captouch, rev:%d\n",
  768. ad714x->version);
  769. return 0;
  770. case AD7147_PARTID:
  771. ad714x->product = 0x7147;
  772. ad714x->version = data & 0xF;
  773. dev_info(ad714x->dev, "found AD7147(A) captouch, rev:%d\n",
  774. ad714x->version);
  775. return 0;
  776. case AD7148_PARTID:
  777. ad714x->product = 0x7148;
  778. ad714x->version = data & 0xF;
  779. dev_info(ad714x->dev, "found AD7148 captouch, rev:%d\n",
  780. ad714x->version);
  781. return 0;
  782. default:
  783. dev_err(ad714x->dev,
  784. "fail to detect AD714X captouch, read ID is %04x\n",
  785. data);
  786. return -ENODEV;
  787. }
  788. }
  789. static void ad714x_hw_init(struct ad714x_chip *ad714x)
  790. {
  791. int i, j;
  792. unsigned short reg_base;
  793. unsigned short data;
  794. /* configuration CDC and interrupts */
  795. for (i = 0; i < STAGE_NUM; i++) {
  796. reg_base = AD714X_STAGECFG_REG + i * STAGE_CFGREG_NUM;
  797. for (j = 0; j < STAGE_CFGREG_NUM; j++)
  798. ad714x->write(ad714x, reg_base + j,
  799. ad714x->hw->stage_cfg_reg[i][j]);
  800. }
  801. for (i = 0; i < SYS_CFGREG_NUM; i++)
  802. ad714x->write(ad714x, AD714X_SYSCFG_REG + i,
  803. ad714x->hw->sys_cfg_reg[i]);
  804. for (i = 0; i < SYS_CFGREG_NUM; i++)
  805. ad714x->read(ad714x, AD714X_SYSCFG_REG + i, &data, 1);
  806. ad714x->write(ad714x, AD714X_STG_CAL_EN_REG, 0xFFF);
  807. /* clear all interrupts */
  808. ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
  809. }
  810. static irqreturn_t ad714x_interrupt_thread(int irq, void *data)
  811. {
  812. struct ad714x_chip *ad714x = data;
  813. int i;
  814. mutex_lock(&ad714x->mutex);
  815. ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
  816. for (i = 0; i < ad714x->hw->button_num; i++)
  817. ad714x_button_state_machine(ad714x, i);
  818. for (i = 0; i < ad714x->hw->slider_num; i++)
  819. ad714x_slider_state_machine(ad714x, i);
  820. for (i = 0; i < ad714x->hw->wheel_num; i++)
  821. ad714x_wheel_state_machine(ad714x, i);
  822. for (i = 0; i < ad714x->hw->touchpad_num; i++)
  823. ad714x_touchpad_state_machine(ad714x, i);
  824. mutex_unlock(&ad714x->mutex);
  825. return IRQ_HANDLED;
  826. }
  827. struct ad714x_chip *ad714x_probe(struct device *dev, u16 bus_type, int irq,
  828. ad714x_read_t read, ad714x_write_t write)
  829. {
  830. int i;
  831. int error;
  832. struct input_dev *input;
  833. struct ad714x_platform_data *plat_data = dev_get_platdata(dev);
  834. struct ad714x_chip *ad714x;
  835. void *drv_mem;
  836. unsigned long irqflags;
  837. struct ad714x_button_drv *bt_drv;
  838. struct ad714x_slider_drv *sd_drv;
  839. struct ad714x_wheel_drv *wl_drv;
  840. struct ad714x_touchpad_drv *tp_drv;
  841. if (irq <= 0) {
  842. dev_err(dev, "IRQ not configured!\n");
  843. error = -EINVAL;
  844. return ERR_PTR(error);
  845. }
  846. if (dev_get_platdata(dev) == NULL) {
  847. dev_err(dev, "platform data for ad714x doesn't exist\n");
  848. error = -EINVAL;
  849. return ERR_PTR(error);
  850. }
  851. ad714x = devm_kzalloc(dev, sizeof(*ad714x) + sizeof(*ad714x->sw) +
  852. sizeof(*sd_drv) * plat_data->slider_num +
  853. sizeof(*wl_drv) * plat_data->wheel_num +
  854. sizeof(*tp_drv) * plat_data->touchpad_num +
  855. sizeof(*bt_drv) * plat_data->button_num,
  856. GFP_KERNEL);
  857. if (!ad714x) {
  858. error = -ENOMEM;
  859. return ERR_PTR(error);
  860. }
  861. ad714x->hw = plat_data;
  862. drv_mem = ad714x + 1;
  863. ad714x->sw = drv_mem;
  864. drv_mem += sizeof(*ad714x->sw);
  865. ad714x->sw->slider = sd_drv = drv_mem;
  866. drv_mem += sizeof(*sd_drv) * ad714x->hw->slider_num;
  867. ad714x->sw->wheel = wl_drv = drv_mem;
  868. drv_mem += sizeof(*wl_drv) * ad714x->hw->wheel_num;
  869. ad714x->sw->touchpad = tp_drv = drv_mem;
  870. drv_mem += sizeof(*tp_drv) * ad714x->hw->touchpad_num;
  871. ad714x->sw->button = bt_drv = drv_mem;
  872. drv_mem += sizeof(*bt_drv) * ad714x->hw->button_num;
  873. ad714x->read = read;
  874. ad714x->write = write;
  875. ad714x->irq = irq;
  876. ad714x->dev = dev;
  877. error = ad714x_hw_detect(ad714x);
  878. if (error)
  879. return ERR_PTR(error);
  880. /* initialize and request sw/hw resources */
  881. ad714x_hw_init(ad714x);
  882. mutex_init(&ad714x->mutex);
  883. /* a slider uses one input_dev instance */
  884. if (ad714x->hw->slider_num > 0) {
  885. struct ad714x_slider_plat *sd_plat = ad714x->hw->slider;
  886. for (i = 0; i < ad714x->hw->slider_num; i++) {
  887. input = devm_input_allocate_device(dev);
  888. if (!input)
  889. return ERR_PTR(-ENOMEM);
  890. __set_bit(EV_ABS, input->evbit);
  891. __set_bit(EV_KEY, input->evbit);
  892. __set_bit(ABS_X, input->absbit);
  893. __set_bit(BTN_TOUCH, input->keybit);
  894. input_set_abs_params(input,
  895. ABS_X, 0, sd_plat->max_coord, 0, 0);
  896. input->id.bustype = bus_type;
  897. input->id.product = ad714x->product;
  898. input->id.version = ad714x->version;
  899. input->name = "ad714x_captouch_slider";
  900. input->dev.parent = dev;
  901. error = input_register_device(input);
  902. if (error)
  903. return ERR_PTR(error);
  904. sd_drv[i].input = input;
  905. }
  906. }
  907. /* a wheel uses one input_dev instance */
  908. if (ad714x->hw->wheel_num > 0) {
  909. struct ad714x_wheel_plat *wl_plat = ad714x->hw->wheel;
  910. for (i = 0; i < ad714x->hw->wheel_num; i++) {
  911. input = devm_input_allocate_device(dev);
  912. if (!input)
  913. return ERR_PTR(-ENOMEM);
  914. __set_bit(EV_KEY, input->evbit);
  915. __set_bit(EV_ABS, input->evbit);
  916. __set_bit(ABS_WHEEL, input->absbit);
  917. __set_bit(BTN_TOUCH, input->keybit);
  918. input_set_abs_params(input,
  919. ABS_WHEEL, 0, wl_plat->max_coord, 0, 0);
  920. input->id.bustype = bus_type;
  921. input->id.product = ad714x->product;
  922. input->id.version = ad714x->version;
  923. input->name = "ad714x_captouch_wheel";
  924. input->dev.parent = dev;
  925. error = input_register_device(input);
  926. if (error)
  927. return ERR_PTR(error);
  928. wl_drv[i].input = input;
  929. }
  930. }
  931. /* a touchpad uses one input_dev instance */
  932. if (ad714x->hw->touchpad_num > 0) {
  933. struct ad714x_touchpad_plat *tp_plat = ad714x->hw->touchpad;
  934. for (i = 0; i < ad714x->hw->touchpad_num; i++) {
  935. input = devm_input_allocate_device(dev);
  936. if (!input)
  937. return ERR_PTR(-ENOMEM);
  938. __set_bit(EV_ABS, input->evbit);
  939. __set_bit(EV_KEY, input->evbit);
  940. __set_bit(ABS_X, input->absbit);
  941. __set_bit(ABS_Y, input->absbit);
  942. __set_bit(BTN_TOUCH, input->keybit);
  943. input_set_abs_params(input,
  944. ABS_X, 0, tp_plat->x_max_coord, 0, 0);
  945. input_set_abs_params(input,
  946. ABS_Y, 0, tp_plat->y_max_coord, 0, 0);
  947. input->id.bustype = bus_type;
  948. input->id.product = ad714x->product;
  949. input->id.version = ad714x->version;
  950. input->name = "ad714x_captouch_pad";
  951. input->dev.parent = dev;
  952. error = input_register_device(input);
  953. if (error)
  954. return ERR_PTR(error);
  955. tp_drv[i].input = input;
  956. }
  957. }
  958. /* all buttons use one input node */
  959. if (ad714x->hw->button_num > 0) {
  960. struct ad714x_button_plat *bt_plat = ad714x->hw->button;
  961. input = devm_input_allocate_device(dev);
  962. if (!input) {
  963. error = -ENOMEM;
  964. return ERR_PTR(error);
  965. }
  966. __set_bit(EV_KEY, input->evbit);
  967. for (i = 0; i < ad714x->hw->button_num; i++) {
  968. bt_drv[i].input = input;
  969. __set_bit(bt_plat[i].keycode, input->keybit);
  970. }
  971. input->id.bustype = bus_type;
  972. input->id.product = ad714x->product;
  973. input->id.version = ad714x->version;
  974. input->name = "ad714x_captouch_button";
  975. input->dev.parent = dev;
  976. error = input_register_device(input);
  977. if (error)
  978. return ERR_PTR(error);
  979. }
  980. irqflags = plat_data->irqflags ?: IRQF_TRIGGER_FALLING;
  981. irqflags |= IRQF_ONESHOT;
  982. error = devm_request_threaded_irq(dev, ad714x->irq, NULL,
  983. ad714x_interrupt_thread,
  984. irqflags, "ad714x_captouch", ad714x);
  985. if (error) {
  986. dev_err(dev, "can't allocate irq %d\n", ad714x->irq);
  987. return ERR_PTR(error);
  988. }
  989. return ad714x;
  990. }
  991. EXPORT_SYMBOL(ad714x_probe);
  992. #ifdef CONFIG_PM
  993. int ad714x_disable(struct ad714x_chip *ad714x)
  994. {
  995. unsigned short data;
  996. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  997. mutex_lock(&ad714x->mutex);
  998. data = ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL] | 0x3;
  999. ad714x->write(ad714x, AD714X_PWR_CTRL, data);
  1000. mutex_unlock(&ad714x->mutex);
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL(ad714x_disable);
  1004. int ad714x_enable(struct ad714x_chip *ad714x)
  1005. {
  1006. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  1007. mutex_lock(&ad714x->mutex);
  1008. /* resume to non-shutdown mode */
  1009. ad714x->write(ad714x, AD714X_PWR_CTRL,
  1010. ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL]);
  1011. /* make sure the interrupt output line is not low level after resume,
  1012. * otherwise we will get no chance to enter falling-edge irq again
  1013. */
  1014. ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
  1015. mutex_unlock(&ad714x->mutex);
  1016. return 0;
  1017. }
  1018. EXPORT_SYMBOL(ad714x_enable);
  1019. #endif
  1020. MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
  1021. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  1022. MODULE_LICENSE("GPL");