ad714x.c 36 KB

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