ad714x.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. #define MAX_DEVICE_NUM 8
  828. struct ad714x_chip *ad714x_probe(struct device *dev, u16 bus_type, int irq,
  829. ad714x_read_t read, ad714x_write_t write)
  830. {
  831. int i, alloc_idx;
  832. int error;
  833. struct input_dev *input[MAX_DEVICE_NUM];
  834. struct ad714x_platform_data *plat_data = dev_get_platdata(dev);
  835. struct ad714x_chip *ad714x;
  836. void *drv_mem;
  837. unsigned long irqflags;
  838. struct ad714x_button_drv *bt_drv;
  839. struct ad714x_slider_drv *sd_drv;
  840. struct ad714x_wheel_drv *wl_drv;
  841. struct ad714x_touchpad_drv *tp_drv;
  842. if (irq <= 0) {
  843. dev_err(dev, "IRQ not configured!\n");
  844. error = -EINVAL;
  845. goto err_out;
  846. }
  847. if (dev_get_platdata(dev) == NULL) {
  848. dev_err(dev, "platform data for ad714x doesn't exist\n");
  849. error = -EINVAL;
  850. goto err_out;
  851. }
  852. ad714x = kzalloc(sizeof(*ad714x) + sizeof(*ad714x->sw) +
  853. sizeof(*sd_drv) * plat_data->slider_num +
  854. sizeof(*wl_drv) * plat_data->wheel_num +
  855. sizeof(*tp_drv) * plat_data->touchpad_num +
  856. sizeof(*bt_drv) * plat_data->button_num, GFP_KERNEL);
  857. if (!ad714x) {
  858. error = -ENOMEM;
  859. goto err_out;
  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. goto err_free_mem;
  880. /* initialize and request sw/hw resources */
  881. ad714x_hw_init(ad714x);
  882. mutex_init(&ad714x->mutex);
  883. /*
  884. * Allocate and register AD714X input device
  885. */
  886. alloc_idx = 0;
  887. /* a slider uses one input_dev instance */
  888. if (ad714x->hw->slider_num > 0) {
  889. struct ad714x_slider_plat *sd_plat = ad714x->hw->slider;
  890. for (i = 0; i < ad714x->hw->slider_num; i++) {
  891. sd_drv[i].input = input[alloc_idx] = input_allocate_device();
  892. if (!input[alloc_idx]) {
  893. error = -ENOMEM;
  894. goto err_free_dev;
  895. }
  896. __set_bit(EV_ABS, input[alloc_idx]->evbit);
  897. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  898. __set_bit(ABS_X, input[alloc_idx]->absbit);
  899. __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
  900. input_set_abs_params(input[alloc_idx],
  901. ABS_X, 0, sd_plat->max_coord, 0, 0);
  902. input[alloc_idx]->id.bustype = bus_type;
  903. input[alloc_idx]->id.product = ad714x->product;
  904. input[alloc_idx]->id.version = ad714x->version;
  905. input[alloc_idx]->name = "ad714x_captouch_slider";
  906. input[alloc_idx]->dev.parent = dev;
  907. error = input_register_device(input[alloc_idx]);
  908. if (error)
  909. goto err_free_dev;
  910. alloc_idx++;
  911. }
  912. }
  913. /* a wheel uses one input_dev instance */
  914. if (ad714x->hw->wheel_num > 0) {
  915. struct ad714x_wheel_plat *wl_plat = ad714x->hw->wheel;
  916. for (i = 0; i < ad714x->hw->wheel_num; i++) {
  917. wl_drv[i].input = input[alloc_idx] = input_allocate_device();
  918. if (!input[alloc_idx]) {
  919. error = -ENOMEM;
  920. goto err_free_dev;
  921. }
  922. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  923. __set_bit(EV_ABS, input[alloc_idx]->evbit);
  924. __set_bit(ABS_WHEEL, input[alloc_idx]->absbit);
  925. __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
  926. input_set_abs_params(input[alloc_idx],
  927. ABS_WHEEL, 0, wl_plat->max_coord, 0, 0);
  928. input[alloc_idx]->id.bustype = bus_type;
  929. input[alloc_idx]->id.product = ad714x->product;
  930. input[alloc_idx]->id.version = ad714x->version;
  931. input[alloc_idx]->name = "ad714x_captouch_wheel";
  932. input[alloc_idx]->dev.parent = dev;
  933. error = input_register_device(input[alloc_idx]);
  934. if (error)
  935. goto err_free_dev;
  936. alloc_idx++;
  937. }
  938. }
  939. /* a touchpad uses one input_dev instance */
  940. if (ad714x->hw->touchpad_num > 0) {
  941. struct ad714x_touchpad_plat *tp_plat = ad714x->hw->touchpad;
  942. for (i = 0; i < ad714x->hw->touchpad_num; i++) {
  943. tp_drv[i].input = input[alloc_idx] = input_allocate_device();
  944. if (!input[alloc_idx]) {
  945. error = -ENOMEM;
  946. goto err_free_dev;
  947. }
  948. __set_bit(EV_ABS, input[alloc_idx]->evbit);
  949. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  950. __set_bit(ABS_X, input[alloc_idx]->absbit);
  951. __set_bit(ABS_Y, input[alloc_idx]->absbit);
  952. __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
  953. input_set_abs_params(input[alloc_idx],
  954. ABS_X, 0, tp_plat->x_max_coord, 0, 0);
  955. input_set_abs_params(input[alloc_idx],
  956. ABS_Y, 0, tp_plat->y_max_coord, 0, 0);
  957. input[alloc_idx]->id.bustype = bus_type;
  958. input[alloc_idx]->id.product = ad714x->product;
  959. input[alloc_idx]->id.version = ad714x->version;
  960. input[alloc_idx]->name = "ad714x_captouch_pad";
  961. input[alloc_idx]->dev.parent = dev;
  962. error = input_register_device(input[alloc_idx]);
  963. if (error)
  964. goto err_free_dev;
  965. alloc_idx++;
  966. }
  967. }
  968. /* all buttons use one input node */
  969. if (ad714x->hw->button_num > 0) {
  970. struct ad714x_button_plat *bt_plat = ad714x->hw->button;
  971. input[alloc_idx] = input_allocate_device();
  972. if (!input[alloc_idx]) {
  973. error = -ENOMEM;
  974. goto err_free_dev;
  975. }
  976. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  977. for (i = 0; i < ad714x->hw->button_num; i++) {
  978. bt_drv[i].input = input[alloc_idx];
  979. __set_bit(bt_plat[i].keycode, input[alloc_idx]->keybit);
  980. }
  981. input[alloc_idx]->id.bustype = bus_type;
  982. input[alloc_idx]->id.product = ad714x->product;
  983. input[alloc_idx]->id.version = ad714x->version;
  984. input[alloc_idx]->name = "ad714x_captouch_button";
  985. input[alloc_idx]->dev.parent = dev;
  986. error = input_register_device(input[alloc_idx]);
  987. if (error)
  988. goto err_free_dev;
  989. alloc_idx++;
  990. }
  991. irqflags = plat_data->irqflags ?: IRQF_TRIGGER_FALLING;
  992. irqflags |= IRQF_ONESHOT;
  993. error = request_threaded_irq(ad714x->irq, NULL, ad714x_interrupt_thread,
  994. irqflags, "ad714x_captouch", ad714x);
  995. if (error) {
  996. dev_err(dev, "can't allocate irq %d\n", ad714x->irq);
  997. goto err_unreg_dev;
  998. }
  999. return ad714x;
  1000. err_free_dev:
  1001. dev_err(dev, "failed to setup AD714x input device %i\n", alloc_idx);
  1002. input_free_device(input[alloc_idx]);
  1003. err_unreg_dev:
  1004. while (--alloc_idx >= 0)
  1005. input_unregister_device(input[alloc_idx]);
  1006. err_free_mem:
  1007. kfree(ad714x);
  1008. err_out:
  1009. return ERR_PTR(error);
  1010. }
  1011. EXPORT_SYMBOL(ad714x_probe);
  1012. void ad714x_remove(struct ad714x_chip *ad714x)
  1013. {
  1014. struct ad714x_platform_data *hw = ad714x->hw;
  1015. struct ad714x_driver_data *sw = ad714x->sw;
  1016. int i;
  1017. free_irq(ad714x->irq, ad714x);
  1018. /* unregister and free all input devices */
  1019. for (i = 0; i < hw->slider_num; i++)
  1020. input_unregister_device(sw->slider[i].input);
  1021. for (i = 0; i < hw->wheel_num; i++)
  1022. input_unregister_device(sw->wheel[i].input);
  1023. for (i = 0; i < hw->touchpad_num; i++)
  1024. input_unregister_device(sw->touchpad[i].input);
  1025. if (hw->button_num)
  1026. input_unregister_device(sw->button[0].input);
  1027. kfree(ad714x);
  1028. }
  1029. EXPORT_SYMBOL(ad714x_remove);
  1030. #ifdef CONFIG_PM
  1031. int ad714x_disable(struct ad714x_chip *ad714x)
  1032. {
  1033. unsigned short data;
  1034. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  1035. mutex_lock(&ad714x->mutex);
  1036. data = ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL] | 0x3;
  1037. ad714x->write(ad714x, AD714X_PWR_CTRL, data);
  1038. mutex_unlock(&ad714x->mutex);
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(ad714x_disable);
  1042. int ad714x_enable(struct ad714x_chip *ad714x)
  1043. {
  1044. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  1045. mutex_lock(&ad714x->mutex);
  1046. /* resume to non-shutdown mode */
  1047. ad714x->write(ad714x, AD714X_PWR_CTRL,
  1048. ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL]);
  1049. /* make sure the interrupt output line is not low level after resume,
  1050. * otherwise we will get no chance to enter falling-edge irq again
  1051. */
  1052. ad714x->read(ad714x, STG_LOW_INT_STA_REG, &ad714x->l_state, 3);
  1053. mutex_unlock(&ad714x->mutex);
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL(ad714x_enable);
  1057. #endif
  1058. MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
  1059. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  1060. MODULE_LICENSE("GPL");