ad714x.c 36 KB

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