ad714x.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. input[alloc_idx]->name = "ad714x_captouch_slider";
  989. input[alloc_idx]->dev.parent = dev;
  990. error = input_register_device(input[alloc_idx]);
  991. if (error)
  992. goto err_free_dev;
  993. alloc_idx++;
  994. }
  995. }
  996. /* a wheel uses one input_dev instance */
  997. if (ad714x->hw->wheel_num > 0) {
  998. struct ad714x_wheel_plat *wl_plat = ad714x->hw->wheel;
  999. for (i = 0; i < ad714x->hw->wheel_num; i++) {
  1000. wl_drv[i].input = input[alloc_idx] = input_allocate_device();
  1001. if (!input[alloc_idx]) {
  1002. error = -ENOMEM;
  1003. goto err_free_dev;
  1004. }
  1005. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  1006. __set_bit(EV_ABS, input[alloc_idx]->evbit);
  1007. __set_bit(ABS_WHEEL, input[alloc_idx]->absbit);
  1008. __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
  1009. input_set_abs_params(input[alloc_idx],
  1010. ABS_WHEEL, 0, wl_plat->max_coord, 0, 0);
  1011. input[alloc_idx]->id.bustype = bus_type;
  1012. input[alloc_idx]->id.product = ad714x->product;
  1013. input[alloc_idx]->id.version = ad714x->version;
  1014. input[alloc_idx]->name = "ad714x_captouch_wheel";
  1015. input[alloc_idx]->dev.parent = dev;
  1016. error = input_register_device(input[alloc_idx]);
  1017. if (error)
  1018. goto err_free_dev;
  1019. alloc_idx++;
  1020. }
  1021. }
  1022. /* a touchpad uses one input_dev instance */
  1023. if (ad714x->hw->touchpad_num > 0) {
  1024. struct ad714x_touchpad_plat *tp_plat = ad714x->hw->touchpad;
  1025. for (i = 0; i < ad714x->hw->touchpad_num; i++) {
  1026. tp_drv[i].input = input[alloc_idx] = input_allocate_device();
  1027. if (!input[alloc_idx]) {
  1028. error = -ENOMEM;
  1029. goto err_free_dev;
  1030. }
  1031. __set_bit(EV_ABS, input[alloc_idx]->evbit);
  1032. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  1033. __set_bit(ABS_X, input[alloc_idx]->absbit);
  1034. __set_bit(ABS_Y, input[alloc_idx]->absbit);
  1035. __set_bit(BTN_TOUCH, input[alloc_idx]->keybit);
  1036. input_set_abs_params(input[alloc_idx],
  1037. ABS_X, 0, tp_plat->x_max_coord, 0, 0);
  1038. input_set_abs_params(input[alloc_idx],
  1039. ABS_Y, 0, tp_plat->y_max_coord, 0, 0);
  1040. input[alloc_idx]->id.bustype = bus_type;
  1041. input[alloc_idx]->id.product = ad714x->product;
  1042. input[alloc_idx]->id.version = ad714x->version;
  1043. input[alloc_idx]->name = "ad714x_captouch_pad";
  1044. input[alloc_idx]->dev.parent = dev;
  1045. error = input_register_device(input[alloc_idx]);
  1046. if (error)
  1047. goto err_free_dev;
  1048. alloc_idx++;
  1049. }
  1050. }
  1051. /* all buttons use one input node */
  1052. if (ad714x->hw->button_num > 0) {
  1053. struct ad714x_button_plat *bt_plat = ad714x->hw->button;
  1054. input[alloc_idx] = input_allocate_device();
  1055. if (!input[alloc_idx]) {
  1056. error = -ENOMEM;
  1057. goto err_free_dev;
  1058. }
  1059. __set_bit(EV_KEY, input[alloc_idx]->evbit);
  1060. for (i = 0; i < ad714x->hw->button_num; i++) {
  1061. bt_drv[i].input = input[alloc_idx];
  1062. __set_bit(bt_plat[i].keycode, input[alloc_idx]->keybit);
  1063. }
  1064. input[alloc_idx]->id.bustype = bus_type;
  1065. input[alloc_idx]->id.product = ad714x->product;
  1066. input[alloc_idx]->id.version = ad714x->version;
  1067. input[alloc_idx]->name = "ad714x_captouch_button";
  1068. input[alloc_idx]->dev.parent = dev;
  1069. error = input_register_device(input[alloc_idx]);
  1070. if (error)
  1071. goto err_free_dev;
  1072. alloc_idx++;
  1073. }
  1074. error = request_threaded_irq(ad714x->irq, NULL, ad714x_interrupt_thread,
  1075. IRQF_TRIGGER_FALLING, "ad714x_captouch", ad714x);
  1076. if (error) {
  1077. dev_err(dev, "can't allocate irq %d\n", ad714x->irq);
  1078. goto err_unreg_dev;
  1079. }
  1080. return ad714x;
  1081. err_free_dev:
  1082. dev_err(dev, "failed to setup AD714x input device %i\n", alloc_idx);
  1083. input_free_device(input[alloc_idx]);
  1084. err_unreg_dev:
  1085. while (--alloc_idx >= 0)
  1086. input_unregister_device(input[alloc_idx]);
  1087. err_free_mem:
  1088. kfree(ad714x);
  1089. err_out:
  1090. return ERR_PTR(error);
  1091. }
  1092. EXPORT_SYMBOL(ad714x_probe);
  1093. void ad714x_remove(struct ad714x_chip *ad714x)
  1094. {
  1095. struct ad714x_platform_data *hw = ad714x->hw;
  1096. struct ad714x_driver_data *sw = ad714x->sw;
  1097. int i;
  1098. free_irq(ad714x->irq, ad714x);
  1099. /* unregister and free all input devices */
  1100. for (i = 0; i < hw->slider_num; i++)
  1101. input_unregister_device(sw->slider[i].input);
  1102. for (i = 0; i < hw->wheel_num; i++)
  1103. input_unregister_device(sw->wheel[i].input);
  1104. for (i = 0; i < hw->touchpad_num; i++)
  1105. input_unregister_device(sw->touchpad[i].input);
  1106. if (hw->button_num)
  1107. input_unregister_device(sw->button[0].input);
  1108. kfree(ad714x);
  1109. }
  1110. EXPORT_SYMBOL(ad714x_remove);
  1111. #ifdef CONFIG_PM
  1112. int ad714x_disable(struct ad714x_chip *ad714x)
  1113. {
  1114. unsigned short data;
  1115. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  1116. mutex_lock(&ad714x->mutex);
  1117. data = ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL] | 0x3;
  1118. ad714x->write(ad714x->dev, AD714X_PWR_CTRL, data);
  1119. mutex_unlock(&ad714x->mutex);
  1120. return 0;
  1121. }
  1122. EXPORT_SYMBOL(ad714x_disable);
  1123. int ad714x_enable(struct ad714x_chip *ad714x)
  1124. {
  1125. unsigned short data;
  1126. dev_dbg(ad714x->dev, "%s enter\n", __func__);
  1127. mutex_lock(&ad714x->mutex);
  1128. /* resume to non-shutdown mode */
  1129. ad714x->write(ad714x->dev, AD714X_PWR_CTRL,
  1130. ad714x->hw->sys_cfg_reg[AD714X_PWR_CTRL]);
  1131. /* make sure the interrupt output line is not low level after resume,
  1132. * otherwise we will get no chance to enter falling-edge irq again
  1133. */
  1134. ad714x->read(ad714x->dev, STG_LOW_INT_STA_REG, &data);
  1135. ad714x->read(ad714x->dev, STG_HIGH_INT_STA_REG, &data);
  1136. ad714x->read(ad714x->dev, STG_COM_INT_STA_REG, &data);
  1137. mutex_unlock(&ad714x->mutex);
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL(ad714x_enable);
  1141. #endif
  1142. MODULE_DESCRIPTION("Analog Devices AD714X Capacitance Touch Sensor Driver");
  1143. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  1144. MODULE_LICENSE("GPL");