ad714x.c 38 KB

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