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/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. 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->platform_data == 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. error = request_threaded_irq(ad714x->irq, NULL, ad714x_interrupt_thread,
  992. plat_data->irqflags ?
  993. plat_data->irqflags : IRQF_TRIGGER_FALLING,
  994. "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");