ad714x.c 34 KB

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