uor6x5x.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/input.h>
  6. #include <linux/init.h>
  7. #include <linux/serio.h>
  8. #include <linux/delay.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/clk.h>
  11. #include <linux/irq.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/i2c.h>
  14. #include <linux/timer.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/poll.h>
  17. #include <linux/spinlock.h>
  18. #include <asm/io.h>
  19. #include <asm/irq.h>
  20. #include <asm/gpio.h>
  21. #include <linux/i2c/uor6x5x.h>
  22. #ifdef CONFIG_HAS_EARLYSUSPEND
  23. #include <linux/earlysuspend.h>
  24. static struct early_suspend uor6x5x_early_suspend;
  25. #endif
  26. #define TRUE 1
  27. #define FALSE 0
  28. #define UOR6X5X_DEBUG FALSE
  29. #define InitX 0x00
  30. #define InitY 0x10
  31. #define MSRX_2T 0x40
  32. #define MSRY_2T 0x50
  33. #define MSRX_1T 0xC0
  34. #define MSRY_1T 0xD0
  35. #define MSRZ1_1T 0xE0
  36. #define MSRZ2_1T 0xF0
  37. //#define GESTURE_IN_DRIVER//!!! Turn on Gesture Detection in Driver !!!
  38. #define Tap 1 //Tap
  39. #define RHorizontal 3 //Right horizontal
  40. #define LHorizontal 4 //Left horizontal
  41. #define UVertical 5 //Up vertical
  42. #define DVertical 6 //Down vertical
  43. #define RArc 7 //Right arc
  44. #define LArc 8 //Left arc
  45. #define CWCircle 9 //Clockwise circle
  46. #define CCWCircle 10 //Counter-clockwise circle
  47. #define RPan 11 //Right pan
  48. #define LPan 12 //Left pan
  49. #define DPan 13 //Right pan
  50. #define UPan 14 //Left pan
  51. #define PressTap 15 //Press and tap
  52. #define PinchIn 16 //Pinch in
  53. #define PinchOut 17 //Pinch out
  54. #define R_Threshold 11000 //ting
  55. #define R_Threshold2 450 //ting // 600
  56. #define ZERO_TOUCH 0
  57. #define ONE_TOUCH 1
  58. #define TWO_TOUCH 2
  59. #define DX_T 48 // 72
  60. #define DY_T 48 // 72
  61. #define DXY_SKIP 0x80
  62. #define NumberFilter 6
  63. #define NumberDrop 4 //This value must not bigger than (NumberFilter-1)
  64. #define WMT_FILTER_NUM 4
  65. #define FIRSTTOUCHCOUNT 1 //ting
  66. #define ONETOUCHCountAfter2 500
  67. #define JITTER_THRESHOLD 800 //ting
  68. #define MAX_READ_PERIOD 6 //ting
  69. #define FIRST_TWO_TOUCH_FILTER 1
  70. #define JITTER_THRESHOLD_DXDY 24
  71. #define PERIOD_PER_FILTER 1
  72. #define DROP_POINT_DELAY_J msecs_to_jiffies(1)
  73. #define READ_DATA_DELAY_J msecs_to_jiffies(6)
  74. #define FILTER_FUNC
  75. #define NFilt NumberFilter
  76. #define NDrop NumberDrop
  77. typedef signed char VINT8;
  78. typedef unsigned char VUINT8;
  79. typedef signed short VINT16;
  80. typedef unsigned short VUINT16;
  81. typedef unsigned long VUINT32;
  82. typedef signed long VINT32;
  83. struct uor6x5x_touch_screen_struct {
  84. struct i2c_client *client;
  85. struct input_dev *dev;
  86. long xp;
  87. long yp;
  88. long pX;
  89. long pY;
  90. long pDX;
  91. long pDY;
  92. int count;
  93. int shift;
  94. unsigned char n_touch;
  95. wait_queue_head_t wq;
  96. spinlock_t lock;
  97. struct timer_list ts_timer;
  98. unsigned char ges_status;
  99. unsigned char GesNo;
  100. struct mutex mutex;
  101. struct work_struct work_q;
  102. struct uor6x5x_platform_data *pdata;
  103. };
  104. static struct uor6x5x_touch_screen_struct ts;
  105. #ifdef CONFIG_PM
  106. static int uor_suspend(struct i2c_client *client, pm_message_t mesg)
  107. {
  108. if(UOR6X5X_DEBUG)
  109. printk(KERN_ERR "uor6x5x.c: uor_suspend() !\n");
  110. disable_irq(client->irq);
  111. return 0;
  112. }
  113. static int uor_resume(struct i2c_client *client)
  114. {
  115. if(UOR6X5X_DEBUG)
  116. printk(KERN_ERR "uor6x5x.c: uor_resume() !\n");
  117. enable_irq(client->irq);
  118. return 0;
  119. }
  120. #else
  121. #define uor_suspend NULL
  122. #define uor_resume NULL
  123. #endif
  124. #ifdef CONFIG_HAS_EARLYSUSPEND
  125. static void uor_early_suspend(struct early_suspend *h)
  126. {
  127. struct i2c_client *client = (struct i2c_client *)(h->param);
  128. if(UOR6X5X_DEBUG)
  129. printk(KERN_ERR "uor6x5x.c: uor_early_suspend() !\n");
  130. disable_irq(client->irq);
  131. }
  132. static void uor_late_resume(struct early_suspend *h)
  133. {
  134. struct i2c_client *client = (struct i2c_client *)(h->param);
  135. if(UOR6X5X_DEBUG)
  136. printk(KERN_ERR "uor6x5x.c: uor_late_resume() !\n");
  137. enable_irq(client->irq);
  138. }
  139. #endif
  140. static int uor_probe(struct i2c_client *client, const struct i2c_device_id *id);
  141. static int uor_remove(struct i2c_client *client);
  142. static const struct i2c_device_id uor_idtable[] = {
  143. { "uor6x5x", 0 },
  144. { }
  145. };
  146. MODULE_DEVICE_TABLE(i2c, uor_idtable);
  147. static struct i2c_driver uor_i2c_driver = {
  148. .driver = {
  149. .name = "uor6x5x",
  150. .owner = THIS_MODULE,
  151. },
  152. .id_table = uor_idtable,
  153. .probe = uor_probe,
  154. .remove = __devexit_p(uor_remove),
  155. .suspend = uor_suspend,
  156. .resume = uor_resume,
  157. };
  158. VINT8 UOR_IICRead(VUINT8 Command, VUINT8 *readdata, VINT8 nread)
  159. {
  160. struct i2c_msg msgs1[] = {
  161. {
  162. .addr = ts.client->addr,//UOR7x5x
  163. .flags = 0,
  164. .len = 1,
  165. .buf = &Command,
  166. },
  167. {
  168. .addr = ts.client->addr,//UOR7x5x
  169. .flags = I2C_M_RD,
  170. .len = nread,
  171. .buf = readdata,
  172. }
  173. };
  174. if (i2c_transfer(ts.client->adapter, msgs1, 2) != 2)//tranfer two messages
  175. {
  176. printk(KERN_ERR "[IIC_Read]: i2c_transfer() return error !!!\n");
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. static int uor_get_pendown_state(void)
  182. {
  183. int state = 0;
  184. if (ts.pdata && ts.pdata->get_irq_level)
  185. state = ts.pdata->get_irq_level();
  186. return state;
  187. }
  188. int xFilter[NFilt], yFilter[NFilt],DxFilter[NFilt], DyFilter[NFilt];
  189. unsigned int XYIndex = 0;
  190. static int RX=1024;
  191. int XYFilter(int *xFilter, int *yFilter, int Num,int Drop){
  192. unsigned int i,SumTempX=0,SumTempY=0;
  193. int Dp,checkSmx,checkSmy,checkBgx,checkBgy;
  194. int SmX =0, SmY = 0;
  195. int LaX = 0, LaY = 0;
  196. int SmInX = 0, SmInY = 0;
  197. int LaInX = 0, LaInY =0;
  198. if( (Num <=2) && (Drop > (Num-1)) )
  199. return FALSE; // not enough to sample
  200. for(i=0;i<Num;i++){
  201. SumTempX += xFilter[i];
  202. SumTempY += yFilter[i];
  203. }
  204. Dp = Drop;
  205. checkSmx = 0;
  206. checkSmy = 0;
  207. checkBgx = 0;
  208. checkBgy = 0;
  209. while(Dp>0){
  210. SmX = 0x0FFF;SmY = 0x0FFF;
  211. LaX = 0x0;LaY = 0x0;
  212. SmInX = 0;SmInY = 0;
  213. LaInX = 0;LaInY =0;
  214. for(i = 0; i < Num; i++){
  215. if(checkSmx&(1<<i)){
  216. }else if(SmX > xFilter[i]){
  217. SmX = xFilter[i];
  218. SmInX= i;
  219. }
  220. if(checkSmy&(1<<i)){
  221. }else if(SmY > yFilter[i]){
  222. SmY = yFilter[i];
  223. SmInY = i;
  224. }
  225. if(checkBgx&(1<<i)){
  226. }else if(LaX < xFilter[i]){
  227. LaX = xFilter[i];
  228. LaInX = i;
  229. }
  230. if(checkBgy&(1<<i)){
  231. }else if(LaY < yFilter[i]){
  232. LaY = yFilter[i];
  233. LaInY = i;
  234. }
  235. }
  236. if(Dp){
  237. SumTempX-= xFilter[SmInX];
  238. SumTempX-= xFilter[LaInX];
  239. SumTempY-= yFilter[SmInY];
  240. SumTempY-= yFilter[LaInY];
  241. Dp -= 2;
  242. if(UOR6X5X_DEBUG)
  243. printk(KERN_ERR "in filter :SmInX %d,LaInX %d, SmInY %d , LaInY %d!!!\n", SmInX,LaInX, SmInY, LaInY);
  244. }
  245. checkSmx |= 1<<SmInX;
  246. checkSmy |= 1<<SmInY;
  247. checkBgx |= 1<<LaInX;
  248. checkBgy |= 1<<LaInY;
  249. }
  250. xFilter[0] = SumTempX/(Num-Drop);
  251. yFilter[0] = SumTempY/(Num-Drop);
  252. return TRUE;
  253. }
  254. VINT8 Init_UOR_HW(void){
  255. VUINT8 i,icdata[2];
  256. VUINT32 Dx_REF,Dy_REF,Dx_Check,Dy_Check;
  257. int TempDx[NumberFilter],TempDy[NumberFilter];
  258. for(i=0;i<NumberFilter;i++){
  259. UOR_IICRead(InitX,icdata,2);
  260. TempDx[i] = (icdata[0]<<4 | icdata[1]>>4);
  261. UOR_IICRead(InitY,icdata,2);
  262. TempDy[i] = (icdata[0]<<4 | icdata[1]>>4);
  263. if(UOR6X5X_DEBUG)
  264. printk(KERN_ERR "filter test:#%d (x,y)=(%d,%d) !!!\n", i,TempDx[i], TempDy[i]);
  265. }
  266. XYFilter(TempDx,TempDy,NumberFilter,2);
  267. Dx_REF = TempDx[0];
  268. Dy_REF = TempDy[0];
  269. if(UOR6X5X_DEBUG)
  270. printk(KERN_ERR "filter result:(x,y)=(%d,%d) !!!\n", Dx_REF, Dy_REF);
  271. i = 0;
  272. do{
  273. UOR_IICRead(InitX,icdata,2);
  274. Dx_Check = abs((icdata[0]<<4 | icdata[1]>>4) - Dx_REF);
  275. UOR_IICRead(InitY,icdata,2);
  276. Dy_Check = abs((icdata[0]<<4 | icdata[1]>>4) - Dy_REF);
  277. i++;
  278. if(i>NumberFilter)
  279. return -1;
  280. }while(Dx_Check > 4 || Dy_Check > 4);
  281. return 0;
  282. }
  283. #ifdef GESTURE_IN_DRIVER
  284. VINT8 PressAndTap = 0,n_touch=0,circle_flag = 0;
  285. VUINT8 touch_flag[2]={0,0},direction_flag1[4],flag_pinch = 0,flag_pan = 0,flag_count = 0;
  286. VUINT16 Dx1,Dx2,Dy1,Dy2,X1,X2,Y1,Y2,XX1,XX2,YY1,YY2;
  287. VUINT16 DIS_1T,DIS_PAN,DIS_PINCH;
  288. VUINT8 RightPan = 0, LeftPan = 0, DownPan = 0, UpPan = 0,circle_direction_flag=0;
  289. void SendGestureKey(VUINT8 Gesture ){
  290. switch(Gesture){
  291. case Tap:
  292. ts.GesNo = 'T';
  293. if(UOR6X5X_DEBUG)
  294. printk(KERN_INFO "Gesture is TAP\r\n");
  295. break;
  296. case RHorizontal:
  297. ts.GesNo = 'R';
  298. if(UOR6X5X_DEBUG)
  299. printk(KERN_INFO "Gesture is RHorizontal\r\n");
  300. break;
  301. case LHorizontal:
  302. ts.GesNo = 'L';
  303. if(UOR6X5X_DEBUG)
  304. printk(KERN_INFO "Gesture is LHorizontal\r\n");
  305. break;
  306. case UVertical:
  307. ts.GesNo = 'U';
  308. if(UOR6X5X_DEBUG)
  309. printk(KERN_INFO "Gesture is UVertical\r\n");
  310. break;
  311. case DVertical:
  312. ts.GesNo = 'D';
  313. if(UOR6X5X_DEBUG)
  314. printk(KERN_INFO "Gesture is DVertical\r\n");
  315. break;
  316. case RArc:
  317. ts.GesNo = 'A';
  318. if(UOR6X5X_DEBUG)
  319. printk(KERN_INFO "Gesture is RArc\r\n");
  320. break;
  321. case LArc:
  322. ts.GesNo = 'A';
  323. if(UOR6X5X_DEBUG)
  324. printk(KERN_INFO "Gesture is LArc\r\n");
  325. break;
  326. case CWCircle:
  327. ts.GesNo = 'C';
  328. if(UOR6X5X_DEBUG
  329. printk(KERN_INFO "Gesture is CWCircle\r\n");
  330. break;
  331. case CCWCircle:
  332. ts.GesNo = 'c';
  333. if(UOR6X5X_DEBUG)
  334. printk(KERN_INFO "Gesture is CCWCircle\r\n");
  335. break;
  336. case RPan:
  337. ts.GesNo = 'r';
  338. if(UOR6X5X_DEBUG)
  339. printk(KERN_INFO "Gesture is RPan\r\n");
  340. break;
  341. case LPan:
  342. ts.GesNo = 'l';
  343. if(UOR6X5X_DEBUG)
  344. printk(KERN_INFO "Gesture is LPan\r\n");
  345. break;
  346. case DPan:
  347. ts.GesNo = 'd';
  348. if(UOR6X5X_DEBUG)
  349. printk(KERN_INFO "Gesture is DPan\r\n");
  350. break;
  351. case UPan:
  352. ts.GesNo = 'u';
  353. if(UOR6X5X_DEBUG)
  354. printk(KERN_INFO "Gesture is UPan\r\n");
  355. break;
  356. case PressTap:
  357. ts.GesNo = 'p';
  358. if(UOR6X5X_DEBUG)
  359. printk(KERN_INFO "Gesture is PressTap\r\n");
  360. break;
  361. case PinchIn:
  362. ts.GesNo = 'I';
  363. if(UOR6X5X_DEBUG)
  364. printk(KERN_INFO "Gesture is PinchIn\r\n");
  365. break;
  366. case PinchOut:
  367. ts.GesNo = 'O';
  368. if(UOR6X5X_DEBUG)
  369. printk(KERN_INFO "Gesture is PinchOut\r\n");
  370. break;
  371. default:
  372. if(UOR6X5X_DEBUG)
  373. printk(KERN_INFO "Gesture key is %d \n", Gesture);
  374. break;
  375. }
  376. }
  377. VUINT8 check_event(VUINT16 x1,VUINT16 x2,VUINT16 y1,VUINT16 y2,VUINT16 dis){
  378. VUINT8 direction = 0x00;
  379. VUINT16 absX = 0, absY = 0;
  380. //Decide what the direction is from (x1,y1) to (x2,y2)
  381. absX = (x1>x2)?(x1-x2):(x2-x1);
  382. absY = (y1>y2)?(y1-y2):(y2-y1);
  383. if(absX>dis)
  384. direction |= (x1 > x2 ? 0x02:0x01); //0x01:Right 0x02:Left
  385. else if(absY>dis)
  386. direction |= (y1 > y2 ? 0x08:0x04); //0x08:Up 0x04:Down
  387. return direction;
  388. }
  389. //Gesture decision function
  390. VUINT8 gesture_decision(VUINT8 n_touch,VUINT16 x,VUINT16 y, VUINT16 Dx, VUINT16 Dy){
  391. VUINT8 gesture =0,flag_d=0,flag_v = 0;
  392. VUINT16 event_flag = 0;
  393. DIS_PINCH = 0x28; // 0x18;
  394. // DIS_PAN = 2500;
  395. DIS_PAN = 800;
  396. // DIS_1T = 500;
  397. DIS_1T = 150; // 350;
  398. if(n_touch == 2){
  399. //Number of touch is 2,
  400. //clear flags for 1-touch gesture decision.
  401. touch_flag[0] = 0;
  402. memset(direction_flag1,0,4);
  403. if(!touch_flag[1]){
  404. //start a new 2Touch gesture decision flow
  405. touch_flag[1] = 1;
  406. PressAndTap = 1;
  407. XX1 = x;
  408. YY1 = y;
  409. Dx1 = Dx;
  410. Dy1 = Dy;
  411. }else{
  412. //Check to see if any 2-Touch event happens.
  413. XX2 = x;
  414. YY2 = y;
  415. //If the variation in Dx or Dy is bigger than DIS_PINCH or the variation in
  416. //the coordinate of central point between the two touch is bigger than DIS_PAN,
  417. //clear the flag PressAndTap.
  418. if(check_event(XX1,XX2,YY1,YY2,50))
  419. PressAndTap = 0;
  420. Dx2 = Dx;
  421. Dy2 = Dy;
  422. /***********************擋掉跳躍超過距離DXY_SKIP的兩點*****************************************/
  423. if(check_event(Dx1,Dx2,Dy1,Dy2,DXY_SKIP)){
  424. Dx1 = Dx2;
  425. Dy1 = Dy2;
  426. return 0;
  427. }
  428. /****************************************************************************************/
  429. if(flag_v = check_event(Dx1,Dx2,Dy1,Dy2,DIS_PINCH)){
  430. //If the variation in Dx or Dy is bigger than DIS_PINCH,
  431. //return gesture PinchIn/PinchOut.
  432. PressAndTap = 0;
  433. Dx1 = Dx2;
  434. Dy1 = Dy2;
  435. XX1 = XX2;
  436. YY1 = YY2;
  437. switch(flag_v){
  438. case 0x1:
  439. case 0x4:
  440. gesture = PinchOut;
  441. break;
  442. case 0x2:
  443. case 0x8:
  444. gesture = PinchIn;
  445. break;
  446. default:
  447. gesture = 0x2f;
  448. break;
  449. }
  450. }else if(flag_d = check_event(XX1,XX2,YY1,YY2,DIS_PAN)){
  451. //If the variation in Dx or Dy is smaller than DIS_PINCH and the variation in
  452. //the coordinate of central point between the two touch is bigger than DIS_PAN,
  453. //set the flag RightPan/LeftPan
  454. PressAndTap = 0;
  455. Dx1 = Dx2;
  456. Dy1 = Dy2;
  457. XX1 = XX2;
  458. YY1 = YY2;
  459. switch(flag_d){
  460. case 0x1:
  461. RightPan = 1;
  462. break;
  463. case 0x2:
  464. LeftPan = 1;
  465. break;
  466. case 0x4:
  467. DownPan = 1;
  468. break;
  469. case 0x8:
  470. UpPan = 1;
  471. break;
  472. default:
  473. gesture = 0x20;
  474. break;
  475. }
  476. }
  477. }
  478. }else if(PressAndTap){
  479. //n_touch is changed from 2 to 1 or from 2 to 0,
  480. //and the flag PressAndTap is set.
  481. gesture = PressTap;
  482. PressAndTap = 0;
  483. touch_flag[0] = 0;
  484. touch_flag[1] = 0;
  485. memset(direction_flag1,0,4);
  486. }else if(n_touch == 1){
  487. //Number of touch is 1,
  488. //clear flags for 2-touch gesture decision.
  489. touch_flag[1] = 0;
  490. if(!touch_flag[0]){
  491. //start a new gesture decision flow
  492. memset(direction_flag1,0,4);
  493. touch_flag[0] = 1;
  494. X1 = x;
  495. Y1 = y;
  496. }else{
  497. //Check to see if any 1-Touch event happens(Up/Down/Right/Left).
  498. X2 = x;
  499. Y2 = y;
  500. if(flag_d = check_event(X1,X2,Y1,Y2,DIS_1T)){
  501. X1 = X2;
  502. Y1 = Y2;
  503. direction_flag1[flag_count] = flag_d;
  504. if(circle_flag){
  505. //If the previous recognized gesture is circle.
  506. event_flag = circle_direction_flag<<4 | direction_flag1[0];
  507. switch(event_flag){
  508. case 0x14:
  509. case 0x42:
  510. case 0x28:
  511. case 0x81:
  512. gesture = CWCircle;
  513. break;
  514. case 0x24:
  515. case 0x41:
  516. case 0x18:
  517. case 0x82:
  518. gesture = CCWCircle;
  519. break;
  520. default:
  521. gesture = 0;
  522. break;
  523. }
  524. circle_direction_flag = direction_flag1[0];
  525. flag_count=0;
  526. circle_flag = 1;
  527. }else if(flag_count == 0){
  528. flag_count++;
  529. }else if(flag_count <3){
  530. if(direction_flag1[flag_count] != direction_flag1[flag_count-1]){
  531. flag_count++;
  532. }
  533. }else if(direction_flag1[flag_count] != direction_flag1[flag_count-1]){
  534. event_flag = direction_flag1[3] | direction_flag1[2]<<4 | direction_flag1[1] << 8 | direction_flag1[0]<<12;
  535. switch(event_flag){
  536. case 0x1428:
  537. case 0x4281:
  538. case 0x2814:
  539. case 0x8142:
  540. circle_direction_flag = direction_flag1[3];
  541. gesture = CWCircle;
  542. flag_count=0;
  543. circle_flag = 1;
  544. memset(direction_flag1,0,4);
  545. break;
  546. case 0x2418:
  547. case 0x4182:
  548. case 0x1824:
  549. case 0x8241:
  550. circle_direction_flag = direction_flag1[3];
  551. gesture = CCWCircle;
  552. flag_count=0;
  553. circle_flag = 1;
  554. memset(direction_flag1,0,4);
  555. break;
  556. default:
  557. gesture = 0;
  558. break;
  559. }//switch
  560. }//if(direction_flag1[3]!=direction_flag1[2])
  561. }
  562. }
  563. }else if(n_touch == 0){
  564. if(circle_flag){
  565. circle_flag=0;
  566. touch_flag[0] = 0;
  567. flag_count=0;
  568. memset(direction_flag1,0,4);
  569. }
  570. if(direction_flag1[2]){
  571. event_flag = direction_flag1[2] | direction_flag1[1] << 4 | direction_flag1[0]<<8;
  572. switch(event_flag){
  573. case 0x144:
  574. case 0x142:
  575. case 0x422:
  576. case 0x428:
  577. case 0x288:
  578. case 0x281:
  579. case 0x811:
  580. case 0x814:
  581. gesture = RArc;
  582. break;
  583. case 0x244:
  584. case 0x241:
  585. case 0x411:
  586. case 0x418:
  587. case 0x188:
  588. case 0x182:
  589. case 0x822:
  590. case 0x824:
  591. gesture = LArc;
  592. break;
  593. default:
  594. gesture = 0;
  595. break;
  596. }
  597. }else if(direction_flag1[1]){
  598. event_flag = direction_flag1[1] | direction_flag1[0] << 4;
  599. switch(event_flag){
  600. case 0x11:
  601. gesture = RHorizontal;
  602. break;
  603. case 0x22:
  604. gesture = LHorizontal;
  605. break;
  606. case 0x88:
  607. gesture = UVertical;
  608. break;
  609. case 0x44:
  610. gesture = DVertical;
  611. break;
  612. case 0x14:
  613. case 0x42:
  614. case 0x28:
  615. case 0x81:
  616. gesture = RArc;
  617. break;
  618. case 0x24:
  619. case 0x41:
  620. case 0x18:
  621. case 0x82:
  622. gesture = LArc;
  623. break;
  624. default:
  625. gesture = 0;
  626. break;
  627. }
  628. }else if(direction_flag1[0]){
  629. switch(direction_flag1[0]){
  630. case 1:
  631. gesture = RHorizontal;
  632. break;
  633. case 2:
  634. gesture = LHorizontal;
  635. break;
  636. case 4:
  637. gesture = DVertical;
  638. break;
  639. case 8:
  640. gesture = UVertical;
  641. break;
  642. }
  643. }else if(RightPan){
  644. gesture = RPan;
  645. RightPan = 0;
  646. }else if(LeftPan){
  647. gesture = LPan;
  648. LeftPan = 0;
  649. }else if(UpPan){
  650. gesture = UPan;
  651. UpPan = 0;
  652. }else if(DownPan){
  653. gesture = DPan;
  654. DownPan = 0;
  655. }else if(touch_flag[0] && !check_event(X1,X2,Y1,Y2,50)){
  656. gesture = Tap;
  657. }
  658. flag_count=0;
  659. touch_flag[0] = 0;
  660. touch_flag[1] = 0;
  661. memset(direction_flag1,0,4);
  662. }
  663. return gesture;
  664. }
  665. #endif
  666. struct ST_UOR_BUF {
  667. unsigned short pXbuf[WMT_FILTER_NUM];
  668. unsigned short pYbuf[WMT_FILTER_NUM];
  669. unsigned short pDXbuf[WMT_FILTER_NUM];
  670. unsigned short pDYbuf[WMT_FILTER_NUM];
  671. };
  672. static unsigned short uor_avg_XY(unsigned short *pData )
  673. {
  674. int i,j,k=1;
  675. unsigned int avg;
  676. for(i=WMT_FILTER_NUM-1; i>0;i--){
  677. for(j=i-1;j>=0;j--){
  678. if(pData[j] > pData[i]){
  679. k =pData[j];
  680. pData[j] = pData[i];
  681. pData[i] = k;
  682. }
  683. }
  684. }
  685. i = WMT_FILTER_NUM/2;
  686. avg = pData[i]+pData[i-1];
  687. if(abs(pData[i]-pData[i-1])>350)
  688. avg=4096;
  689. else
  690. avg/=2;
  691. return avg;
  692. }
  693. static unsigned short uor_avg_DXY(unsigned short *pData )
  694. {
  695. int i,j,k,swap = 1;
  696. unsigned int avg;
  697. for(i=WMT_FILTER_NUM-1; i>0;i--){
  698. for(j=i-1;j>=0;j--){
  699. if(pData[j] > pData[i]){
  700. k =pData[j];
  701. pData[j] = pData[i];
  702. pData[i] = k;
  703. }
  704. }
  705. }
  706. i = WMT_FILTER_NUM/2;
  707. avg =pData[i]+pData[i-1];
  708. if(abs(pData[i]-pData[i-1])>12)
  709. avg=256;
  710. else
  711. avg /=2;
  712. return avg;
  713. }
  714. static void uor_read_XY(unsigned short *X, unsigned short *Y)
  715. {
  716. int i = 0;
  717. VUINT8 EpBuf[2];
  718. struct ST_UOR_BUF uor_point;
  719. for(i=0; i<WMT_FILTER_NUM;i++){
  720. memset(EpBuf, 0, sizeof(EpBuf));
  721. UOR_IICRead(MSRX_1T, EpBuf, 2);
  722. uor_point.pXbuf[i] = (EpBuf[0]<<4)|(EpBuf[1]>>4);
  723. UOR_IICRead(MSRY_1T, EpBuf, 2);
  724. uor_point.pYbuf[i] = (EpBuf[0]<<4)|(EpBuf[1]>>4);
  725. }
  726. *X = uor_avg_XY(uor_point.pXbuf);
  727. *Y = uor_avg_XY(uor_point.pYbuf);
  728. return;
  729. }
  730. static void uor_read_DXY(unsigned short *DX, unsigned short *DY)
  731. {
  732. int i = 0;
  733. VUINT8 EpBuf[4];
  734. struct ST_UOR_BUF uor_point;
  735. for(i=0; i<WMT_FILTER_NUM;i++){
  736. memset(EpBuf, 0, sizeof(EpBuf));
  737. UOR_IICRead(MSRX_2T, (EpBuf), 3);
  738. uor_point.pDXbuf[i] = EpBuf[2];
  739. UOR_IICRead(MSRY_2T, (EpBuf), 3);
  740. uor_point.pDYbuf[i] = EpBuf[2];
  741. }
  742. *DX = uor_avg_DXY(uor_point.pDXbuf);
  743. *DY = uor_avg_DXY(uor_point.pDYbuf);
  744. return;
  745. }
  746. static struct workqueue_struct *queue = NULL;
  747. static struct delayed_work work;
  748. static int FirstTC = 0,OneTCountAfter2 = 0,TWOTouchFlag = 0;
  749. static int two_touch_count = 0, pre_dx = 0, pre_dy = 0;
  750. static int pre_outx1 = 0, pre_outy1 = 0, pre_outx2 = 0, pre_outy2 = 0;
  751. static void uor_read_data(unsigned short *X, unsigned short *Y,
  752. unsigned short *DX, unsigned short *DY)
  753. {
  754. VUINT8 EpBuf[4];
  755. unsigned short x, y;
  756. unsigned short Dx, Dy;
  757. memset(EpBuf, 0, sizeof(EpBuf));
  758. UOR_IICRead(MSRX_1T, EpBuf, 2);
  759. x= EpBuf[0];
  760. x <<=4;
  761. x |= (EpBuf[1]>>4);
  762. UOR_IICRead(MSRY_1T, EpBuf, 2);
  763. y = EpBuf[0];
  764. y <<=4;
  765. y |= (EpBuf[1]>>4);
  766. UOR_IICRead(MSRX_2T, (EpBuf), 3);
  767. Dx = EpBuf[2];
  768. UOR_IICRead(MSRY_2T, (EpBuf), 3);
  769. Dy = EpBuf[2];
  770. *X = x;
  771. *Y = y;
  772. *DX = Dx;
  773. *DY = Dy;
  774. }
  775. static void uor_read_loop(struct work_struct *data)
  776. {
  777. VUINT8 EpBuf[4];
  778. unsigned short SBufx_0 = 0;
  779. unsigned short SBufx_1 = 0;
  780. unsigned short SBufx_2 = 0;
  781. unsigned short SBufy_0 = 0;
  782. unsigned short SBufy_1 = 0;
  783. unsigned short SBufy_2 = 0;
  784. unsigned short SBufxs_0 = 0;
  785. unsigned short SBufxs_1 = 0;
  786. unsigned short SBufxs_2 = 0;
  787. unsigned short SBufys_0 = 0;
  788. unsigned short SBufys_1 = 0;
  789. unsigned short SBufys_2 = 0;
  790. unsigned short x, y , y1, x2, y2;
  791. unsigned short out_x, out_y;
  792. unsigned short Dx, Dy, z1, z2, Dx1, Dx2, Dy1, Dy2;
  793. unsigned int R_touch;
  794. unsigned int Rt;
  795. unsigned int nTouch = 0;
  796. int xy = 0;
  797. unsigned int slid_index=0;
  798. unsigned int slid_indexs=0;
  799. if(UOR6X5X_DEBUG)
  800. printk(KERN_ERR "uor.c: uor_read_loop() !\n");
  801. while(1){
  802. if(!uor_get_pendown_state()) {
  803. #ifdef FILTER_FUNC
  804. uor_read_XY(&x, &y);
  805. uor_read_DXY(&Dx, &Dy);
  806. ts.xp = x-22;
  807. ts.yp = y-22;
  808. /*
  809. //first point
  810. uor_read_data(&x, &y, &Dx, &Dy);
  811. xFilter[ts.count] = x;
  812. yFilter[ts.count] = y;
  813. DxFilter[ts.count] = Dx;
  814. DyFilter[ts.count] = Dy;
  815. //printk(KERN_ERR "Data before filter:#%d (x,y)=(%d,%d) (dx,dy)=(%d,%d) !!!\n",ts.count , x, y, Dx, Dy);
  816. ts.count ++;
  817. //udelay(PERIOD_PER_FILTER); //ting //Per Read Point Delay
  818. while(ts.count < NFilt)//collect other point
  819. {
  820. uor_read_data(&x, &y, &Dx, &Dy);
  821. xFilter[ts.count] = x;
  822. yFilter[ts.count] = y;
  823. DxFilter[ts.count] = Dx;
  824. DyFilter[ts.count] = Dy;
  825. //printk(KERN_ERR "Data before filter:#%d (x,y)=(%d,%d) (dx,dy)=(%d,%d) !!!\n",ts.count , x, y, Dx, Dy);
  826. ts.count ++;
  827. }
  828. if(!XYFilter(xFilter, yFilter, NFilt,NDrop)){ // no correct point
  829. printk(KERN_ERR "%s: X Y filter error !!!\n",__FUNCTION__);
  830. }
  831. ts.xp =xFilter[0];
  832. ts.yp =yFilter[0];
  833. if(!XYFilter(DxFilter, DyFilter, NFilt,NDrop)){ // no correct point
  834. printk(KERN_ERR "%s: DX DY filter error !!!\n",__FUNCTION__);
  835. }
  836. Dx = DxFilter[0];
  837. Dy = DyFilter[0];
  838. ts.count = 0;
  839. */
  840. #else // no filter
  841. memset(EpBuf, 0, sizeof(EpBuf));
  842. UOR_IICRead(MSRX_1T, EpBuf, 2);
  843. x= EpBuf[0];
  844. x<<=4;
  845. x|= (EpBuf[1]>>4);
  846. UOR_IICRead(MSRX_1T, EpBuf, 2);
  847. x2= EpBuf[0];
  848. x2<<=4;
  849. x2|= (EpBuf[1]>>4);
  850. UOR_IICRead(MSRY_1T, EpBuf, 2);
  851. y1= EpBuf[0];
  852. y1<<=4;
  853. y1 |= (EpBuf[1]>>4);
  854. UOR_IICRead(MSRY_1T, EpBuf, 2);
  855. y2= EpBuf[0];
  856. y2<<=4;
  857. y2 |= (EpBuf[1]>>4);
  858. if (abs(x-x2)>500)
  859. ts.xp = 4095;
  860. else
  861. ts.xp = (x+x2)/2;
  862. if (abs(y1-y2)>500)
  863. ts.yp = 4095;
  864. else
  865. ts.yp = (y1+y2)/2;
  866. UOR_IICRead(MSRX_2T, (EpBuf), 3);
  867. Dx1 = EpBuf[2];
  868. UOR_IICRead(MSRX_2T, (EpBuf), 3);
  869. Dx2 = EpBuf[2];
  870. UOR_IICRead(MSRY_2T, (EpBuf), 3);
  871. Dy1 = EpBuf[2];
  872. UOR_IICRead(MSRY_2T, (EpBuf), 3);
  873. Dy2 = EpBuf[2];
  874. if ((Dx1-Dx2)>16||(Dx2-Dx1)>16)
  875. Dx = 256;
  876. else
  877. Dx =( (Dx1+Dx2)>>1)-20;
  878. if ((Dy1-Dy2)>16 ||(Dy2-Dy1)>16)
  879. Dy = 256;
  880. else
  881. Dy = ((Dy1+Dy2)>>1)-20;
  882. #endif
  883. if(Dx>252 || Dx<16)
  884. Dx=36;
  885. if(Dy>252 || Dy<16)
  886. Dy=36;
  887. memset(EpBuf, 0, sizeof(EpBuf));
  888. UOR_IICRead(MSRZ1_1T, EpBuf, 2);
  889. z1 = EpBuf[0];
  890. z1 <<=4;
  891. z1 |= (EpBuf[1]>>4);
  892. UOR_IICRead(MSRZ2_1T, EpBuf, 2);
  893. z2 = EpBuf[0];
  894. z2 <<=4;
  895. z2 |= (EpBuf[1]>>4);
  896. if(z1 ==0) {
  897. z1 =1;//avoid divde by zero
  898. }
  899. R_touch =(abs(((z2*x)/z1-x)))/4; //(float)((((float) z2)/((float) z1) -1)*(float)x)/4096;
  900. Rt =R_touch;
  901. if( ((256>Dx) && (256>Dy)) && ((Dx> DX_T) ||(Dy > DY_T)) && (Rt < R_Threshold2) ) {
  902. nTouch = TWO_TOUCH;
  903. }
  904. else {
  905. nTouch = ONE_TOUCH;
  906. }
  907. if(UOR6X5X_DEBUG)
  908. printk(KERN_ERR "%s:after filter (x,y)=(%d,%d) (dx,dy)=(%d,%d) n_touch %d, R_touch %d, (z1,z2)=(%d,%d) !!!\n",__FUNCTION__, ts.xp , ts.yp , Dx, Dy, nTouch, R_touch, z1, z2);
  909. }
  910. else {//ting
  911. nTouch = ZERO_TOUCH;
  912. if(UOR6X5X_DEBUG)
  913. printk("there is no touch!\n");
  914. }
  915. if(nTouch == ONE_TOUCH || nTouch == TWO_TOUCH){ // pen down
  916. if(nTouch == TWO_TOUCH){
  917. if(two_touch_count < FIRST_TWO_TOUCH_FILTER){
  918. if(UOR6X5X_DEBUG)
  919. printk(KERN_ERR "%s:filter for first two touch -(x,y)=(%d,%d) (dx,dy)=(%d,%d),count = %d, FIRST_TWO_TOUCH_FILTER = %d !!!\n",__FUNCTION__, x, y, Dx, Dy,two_touch_count, FIRST_TWO_TOUCH_FILTER);
  920. two_touch_count++;
  921. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  922. goto READ_LOOP_OUT;
  923. }
  924. else if(ts.xp>4000 || ts.yp>4000){
  925. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  926. goto READ_LOOP_OUT;
  927. }
  928. else if( (pre_dx!=0) && (pre_dy!=0) && (abs(Dx - pre_dx) > JITTER_THRESHOLD_DXDY) ||(abs( Dy - pre_dy) > JITTER_THRESHOLD_DXDY)){//single touch point 前後差距JITTER_THRESHOLD 則濾點
  929. if(UOR6X5X_DEBUG)
  930. printk(KERN_ERR "%s:filter for jitter(dual) --(pre_dx,pre_dy)=(%d,%d) ,(dx,dy)=(%d,%d) , JITTER_THRESHOLD_DXDY = %d !!!\n",__FUNCTION__, pre_dx, pre_dy , Dx, Dy, JITTER_THRESHOLD_DXDY);
  931. pre_dx = Dx;
  932. pre_dy = Dy;
  933. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  934. goto READ_LOOP_OUT;
  935. }
  936. else{
  937. if(UOR6X5X_DEBUG)
  938. printk(KERN_ERR "%s:report dual touch-- (x,y)=(%d,%d) (dx,dy)=(%d,%d) !!!\n",__FUNCTION__, x, y, Dx, Dy);
  939. //report x,y,pressure,dx,dy to Linux/Android
  940. if(((two_touch_count<3) ||(Dx>80) || (Dy>80))&&(abs(Dx - pre_dx) <20 && abs(Dy - pre_dy )<20)){
  941. Dx=pre_dx;
  942. Dy=pre_dy;
  943. pre_dx =((7* pre_dx)+Dx)>>3;
  944. pre_dy =((7*pre_dy)+Dy)>>3;
  945. }
  946. if((pre_dx!=0) && (pre_dy!=0) && (abs(Dx - pre_dx) <8) && (abs(Dy - pre_dy )<8)){
  947. Dx=pre_dx;
  948. Dy=pre_dy;
  949. pre_dx =((7* pre_dx)+Dx)>>3;
  950. pre_dy =((7*pre_dy)+Dy)>>3;
  951. }
  952. // if ( (ts.pX!=0) && (ts.pY!=0) && ((ts.xp - ts.pX) <100 && (ts.pX-ts.xp) <100 && (ts.yp - ts.pY )<100 && (ts.pY-ts.yp )<100)){
  953. if ( (ts.pX!=0) && (ts.pY!=0) && (abs(ts.xp - ts.pX) <1500) && (abs(ts.yp - ts.pY )<1500)){
  954. ts.xp = ts.pX;
  955. ts.yp = ts.pY;
  956. }
  957. if(UOR6X5X_DEBUG) {
  958. printk(KERN_ERR "%s:TWO_TOUCH (cx,cy)=(%d,%d)\n",__FUNCTION__, ts.xp, ts.yp);
  959. printk(KERN_ERR "%s:TWO_TOUCH (pcx,pcy)=(%d,%d)\n",__FUNCTION__, ts.pX, ts.pY);
  960. printk(KERN_ERR "%s:TWO_TOUCH (dx,dy)=(%d,%d)\n",__FUNCTION__, Dx, Dy);
  961. printk(KERN_ERR "%s:TWO_TOUCH (pdx,pdy)=(%d,%d)\n",__FUNCTION__, pre_dx,pre_dy);
  962. }
  963. int dx_coord =(Dx - DX_T< 0) ? 5 : (((Dx - 18-22) & 0x00fc)/2)+5;
  964. int dy_coord =(Dy - DY_T< 0) ? 5 : (((Dy - 18-22) & 0x00fc)/2)+5;
  965. if(UOR6X5X_DEBUG)
  966. printk(KERN_ERR "%s:TWO_TOUCH (dx_coord,dy_coord)=(%d,%d)\n",__FUNCTION__, dx_coord, dy_coord);
  967. input_report_abs(ts.dev, ABS_MT_TOUCH_MAJOR, 600 + (Rt%400));
  968. //input_report_abs(ts.dev, ABS_MT_WIDTH_MAJOR, 500+press);
  969. if(slid_index == 2){
  970. SBufx_0 = SBufx_1;
  971. SBufx_1 = SBufx_2;
  972. SBufx_2 = dx_coord;
  973. dx_coord = (SBufx_0+ SBufx_1+ SBufx_2)/3;
  974. SBufy_0 = SBufy_1;
  975. SBufy_1 = SBufy_2;
  976. SBufy_2 = dy_coord;
  977. dy_coord = (SBufy_0+ SBufy_1+ SBufy_2)/3;
  978. }
  979. else if(slid_index == 1){
  980. SBufx_1 = SBufx_2;
  981. SBufx_2 = dx_coord;
  982. dx_coord = (SBufx_1+ SBufx_2)/2;
  983. SBufy_1 = SBufy_2;
  984. SBufy_2 = dy_coord;
  985. dy_coord = (SBufy_1+ SBufy_2)/2;
  986. slid_index ++;
  987. }
  988. else{
  989. SBufx_2 = dx_coord;
  990. SBufy_2 = dy_coord;
  991. slid_index ++;
  992. }
  993. xy = 0;
  994. out_x = ts.xp - dx_coord;
  995. out_y = ts.yp - dy_coord;
  996. if(ts.pdata->convert){
  997. xy = ts.pdata->convert(out_x, out_y);
  998. out_x = xy >> 16;
  999. out_y = xy & 0xffff;
  1000. }
  1001. if((pre_outx1!=0) && (pre_outy1!=0) && (abs(out_x - pre_outx1) <2) && (abs(out_y - pre_outy1 )<2)){
  1002. out_x = pre_outx1;
  1003. out_y = pre_outy1;
  1004. }
  1005. if(UOR6X5X_DEBUG)
  1006. printk(KERN_ERR "%s:TWO_TOUCH (x1,y1)=(%d,%d)\n",__FUNCTION__, out_x, out_y);
  1007. input_report_abs(ts.dev, ABS_MT_POSITION_X, out_x);
  1008. input_report_abs(ts.dev, ABS_MT_POSITION_Y, out_y);
  1009. input_mt_sync(ts.dev);
  1010. input_report_abs(ts.dev, ABS_MT_TOUCH_MAJOR, 600 + (Rt%400));
  1011. pre_outx1 = out_x;
  1012. pre_outy1 = out_y;
  1013. //input_report_abs(ts.dev, ABS_MT_WIDTH_MAJOR, 600+press);
  1014. xy = 0;
  1015. out_x = ts.xp + dx_coord;
  1016. out_y = ts.yp + dy_coord;
  1017. if(ts.pdata->convert){
  1018. xy = ts.pdata->convert(out_x, out_y);
  1019. out_x = xy >> 16;
  1020. out_y = xy & 0xffff;
  1021. }
  1022. if((pre_outx2!=0) && (pre_outy2!=0) && (abs(out_x - pre_outx2) <2) && (abs(out_y - pre_outy2 )<2) ){
  1023. out_x = pre_outx2;
  1024. out_y = pre_outy2;
  1025. }
  1026. if(UOR6X5X_DEBUG)
  1027. printk(KERN_ERR "%s:TWO_TOUCH (x2,y2)=(%d,%d)\n",__FUNCTION__, out_x, out_y);
  1028. input_report_abs(ts.dev, ABS_MT_POSITION_X, out_x);
  1029. input_report_abs(ts.dev, ABS_MT_POSITION_Y, out_y);
  1030. input_mt_sync(ts.dev);
  1031. input_sync(ts.dev);
  1032. TWOTouchFlag = 1;
  1033. OneTCountAfter2 = 0;
  1034. pre_dx = Dx;
  1035. pre_dy = Dy;
  1036. ts.pX = ts.xp;
  1037. ts.pY = ts.yp;
  1038. pre_outx2 = out_x;
  1039. pre_outy2 = out_y;
  1040. if(two_touch_count<5)
  1041. two_touch_count++;
  1042. queue_delayed_work(queue, &work, READ_DATA_DELAY_J);
  1043. goto READ_LOOP_OUT;
  1044. }
  1045. }
  1046. else if(nTouch == ONE_TOUCH){
  1047. if((TWOTouchFlag == 1) && (OneTCountAfter2 < ONETOUCHCountAfter2)){
  1048. if(UOR6X5X_DEBUG)
  1049. printk(KERN_ERR "%s:filter after two touch -- (x,y)=(%d,%d) ,OneTCountAfter2 = %d, ONETOUCHCountAfter2 = %d !!!\n",__FUNCTION__, x, y, OneTCountAfter2, ONETOUCHCountAfter2);
  1050. OneTCountAfter2++;
  1051. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  1052. goto READ_LOOP_OUT;
  1053. }
  1054. else if((ts.xp>4000||ts.yp>4000)||((TWOTouchFlag == 0) && (FirstTC < FIRSTTOUCHCOUNT)) || (Rt > R_Threshold)){ //ting
  1055. if(UOR6X5X_DEBUG)
  1056. printk(KERN_ERR "%s:filter before single touch -- (x,y)=(%d,%d) ,FirstTC = %d, FIRSTTOUCHCOUNT = %d !!!\n",__FUNCTION__, x, y, FirstTC, FIRSTTOUCHCOUNT);
  1057. FirstTC++;
  1058. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  1059. goto READ_LOOP_OUT;
  1060. }
  1061. else if( (ts.pX!=0) && (ts.pY!=0) && (abs(ts.xp - ts.pX) > JITTER_THRESHOLD) || (abs(ts.yp - ts.pY) > JITTER_THRESHOLD)){
  1062. if(UOR6X5X_DEBUG)
  1063. printk(KERN_ERR "%s:filter for jitter -- (px,py)=(%d,%d) ,(x,y)=(%d,%d) , JITTER_THRESHOLD = %d !!!\n",__FUNCTION__, ts.pX, ts.pY ,x, y, JITTER_THRESHOLD);
  1064. ts.pX = ts.xp;
  1065. ts.pY = ts.yp;
  1066. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  1067. goto READ_LOOP_OUT;
  1068. }
  1069. else{
  1070. if(UOR6X5X_DEBUG)
  1071. printk(KERN_ERR "%s: (Pen down)report single touch-- (x,y)=(%d,%d) !!!\n",__FUNCTION__, x, y);
  1072. //report x,y,pressure,size to Linux/Android
  1073. if((ts.pX!=0) && (ts.pY!=0) && (abs(ts.xp - ts.pX) <40) && (abs(ts.yp - ts.pY )<40)){
  1074. ts.xp = ts.pX;
  1075. ts.yp = ts.pY;
  1076. }
  1077. if(slid_indexs == 2){
  1078. SBufxs_0 = SBufxs_1;
  1079. SBufxs_1 = SBufxs_2;
  1080. SBufxs_2 = ts.xp;
  1081. ts.xp= (SBufxs_0+ SBufxs_1)/4+ (SBufxs_2)/2;
  1082. SBufys_0 = SBufys_1;
  1083. SBufys_1 = SBufys_2;
  1084. SBufys_2 = ts.yp;
  1085. ts.yp = (SBufys_0+ SBufys_1)/4+( SBufys_2)/2;
  1086. }
  1087. else if(slid_index == 1){
  1088. SBufxs_1 = SBufxs_2;
  1089. SBufxs_2 = ts.xp;
  1090. ts.xp= (SBufxs_1+ SBufxs_2)/2;
  1091. SBufys_1 = SBufys_2;
  1092. SBufys_2 = ts.yp;
  1093. ts.yp= (SBufys_1+ SBufys_2)/2;
  1094. slid_indexs ++;
  1095. }
  1096. else{
  1097. SBufxs_2 = ts.xp;
  1098. SBufys_2 = ts.yp;
  1099. slid_indexs ++;
  1100. }
  1101. input_report_abs(ts.dev, ABS_MT_TOUCH_MAJOR, 600 + (Rt%400) );
  1102. //input_report_abs(ts.dev, ABS_MT_WIDTH_MAJOR, 300);
  1103. xy = 0;
  1104. out_x = ts.xp;
  1105. out_y = ts.yp;
  1106. if(ts.pdata->convert){
  1107. xy = ts.pdata->convert(out_x, out_y);
  1108. out_x = xy >> 16;
  1109. out_y = xy & 0xffff;
  1110. }
  1111. if(UOR6X5X_DEBUG)
  1112. printk(KERN_ERR "%s:ONE_TOUCH (x1,y1)=(%d,%d)\n",__FUNCTION__, out_x, out_y);
  1113. input_report_abs(ts.dev, ABS_MT_POSITION_X, out_x);
  1114. input_report_abs(ts.dev, ABS_MT_POSITION_Y, out_y);
  1115. input_mt_sync(ts.dev);
  1116. input_sync(ts.dev);
  1117. //save previous single touch point
  1118. ts.pX = ts.xp;
  1119. ts.pY = ts.yp;
  1120. queue_delayed_work(queue, &work, READ_DATA_DELAY_J);
  1121. goto READ_LOOP_OUT;
  1122. }
  1123. }
  1124. }
  1125. else if(nTouch == ZERO_TOUCH){ // pen release
  1126. udelay(250);
  1127. if(!uor_get_pendown_state()){
  1128. queue_delayed_work(queue, &work, READ_DATA_DELAY_J);
  1129. goto READ_LOOP_OUT;
  1130. }
  1131. if(UOR6X5X_DEBUG)
  1132. printk(KERN_ERR "%s: (Pen release)!!!\n",__FUNCTION__);
  1133. input_report_abs(ts.dev, ABS_MT_TOUCH_MAJOR, 0);
  1134. //input_report_abs(ts.dev, ABS_MT_WIDTH_MAJOR, 0);
  1135. input_mt_sync(ts.dev);
  1136. input_sync(ts.dev);
  1137. //reset filter parameters
  1138. FirstTC = 0;
  1139. OneTCountAfter2 = 0;
  1140. TWOTouchFlag = 0;
  1141. two_touch_count = 0;
  1142. ts.xp= 0;
  1143. ts.yp = 0;
  1144. ts.pX = 0;
  1145. ts.pY = 0;
  1146. pre_dx = 0;
  1147. pre_dy = 0;
  1148. pre_outx1 = 0;
  1149. pre_outy1 = 0;
  1150. pre_outx2 = 0;
  1151. pre_outy2 = 0;
  1152. slid_index = 0;
  1153. slid_indexs = 0;
  1154. Init_UOR_HW();
  1155. enable_irq(ts.client->irq);
  1156. break;
  1157. }
  1158. else{
  1159. if(UOR6X5X_DEBUG)
  1160. printk(KERN_ERR "uor_read_loop(): n_touch state error !!!\n");
  1161. }
  1162. }
  1163. READ_LOOP_OUT:
  1164. return;
  1165. }
  1166. static irqreturn_t uor_isr(int irq,void *dev_id)
  1167. {
  1168. struct i2c_client *client = (struct i2c_client *)dev_id;
  1169. if(UOR6X5X_DEBUG)
  1170. printk(KERN_ERR "uor.c: uor_isr\n");
  1171. udelay(250);
  1172. if(uor_get_pendown_state()){//ting debounce
  1173. return IRQ_HANDLED;
  1174. }
  1175. disable_irq_nosync(client->irq);
  1176. //queue_work(queue, &work);
  1177. queue_delayed_work(queue, &work, DROP_POINT_DELAY_J);
  1178. if(UOR6X5X_DEBUG)
  1179. printk(KERN_ERR "uor_isr ok!\n");
  1180. return IRQ_HANDLED;
  1181. }
  1182. static int uor_register_input(void)
  1183. {
  1184. int ret;
  1185. struct input_dev * input_device;
  1186. input_device = input_allocate_device();
  1187. if (!input_device) {
  1188. if(UOR6X5X_DEBUG)
  1189. printk(KERN_ERR "Unable to allocate the input device !!\n");
  1190. return -ENOMEM;
  1191. }
  1192. input_device->name = "UOR-touch";
  1193. ts.dev = input_device;
  1194. set_bit(EV_SYN, ts.dev->evbit);
  1195. set_bit(EV_ABS, ts.dev->evbit);
  1196. input_set_abs_params(ts.dev, ABS_MT_TOUCH_MAJOR, 0, 1000, 0, 0);
  1197. //input_set_abs_params(codec_ts_input, ABS_MT_WIDTH_MAJOR, 0, 1000, 0, 0);
  1198. int max_x = ts.pdata->abs_xmax ? ts.pdata->abs_xmax : 4096;
  1199. input_set_abs_params(ts.dev, ABS_MT_POSITION_X, 0, max_x, 0, 0);
  1200. int max_y = ts.pdata->abs_ymax ? ts.pdata->abs_ymax : 4096;
  1201. input_set_abs_params(ts.dev, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
  1202. ret = input_register_device(ts.dev);
  1203. if (ret) {
  1204. input_free_device(ts.dev);
  1205. if(UOR6X5X_DEBUG)
  1206. printk(KERN_ERR "%s: unabled to register input device, ret = %d\n", __FUNCTION__, ret);
  1207. return ret;
  1208. }
  1209. return 0;
  1210. }
  1211. static int __init uor_init(void)
  1212. {
  1213. int ret = 0;
  1214. if(UOR6X5X_DEBUG)
  1215. printk(KERN_ERR "uor.c: uor_init() !\n");
  1216. memset(&ts, 0, sizeof(struct uor6x5x_touch_screen_struct));//init data struct ts
  1217. ret = i2c_add_driver(&uor_i2c_driver);
  1218. if(ret < 0) {
  1219. if(UOR6X5X_DEBUG)
  1220. printk(KERN_ERR "uor.c: i2c_add_driver() fail in uor_init()!\n");
  1221. return ret;
  1222. }
  1223. ret = Init_UOR_HW();
  1224. if(ret < 0) {
  1225. if(UOR6X5X_DEBUG)
  1226. printk(KERN_ERR "uor.c: Init_UOR_HW() fail in uor_init()!\n");
  1227. return ret;
  1228. }
  1229. return ret;
  1230. }
  1231. static int __devinit uor_probe(struct i2c_client *client,
  1232. const struct i2c_device_id *id)
  1233. {
  1234. int err = 0;
  1235. struct uor6x5x_platform_data *pdata = pdata = client->dev.platform_data;
  1236. if(UOR6X5X_DEBUG)
  1237. printk(KERN_ERR "uor.c: uor_probe() !\n");
  1238. if (!pdata) {
  1239. dev_err(&client->dev, "platform data is required!\n");
  1240. return -EINVAL;
  1241. }
  1242. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  1243. return -EIO;
  1244. queue = create_singlethread_workqueue("uor-touch-screen-read-loop");
  1245. //INIT_WORK(&work, uor_read_loop);
  1246. INIT_DELAYED_WORK(&work, uor_read_loop);
  1247. if (pdata->init_irq)
  1248. pdata->init_irq();
  1249. ts.client = client; // save the client we get
  1250. ts.pdata = pdata;
  1251. if (uor_register_input() < 0) {
  1252. dev_err(&client->dev, "register input fail!\n");
  1253. return -ENOMEM;
  1254. }
  1255. err = request_irq(client->irq, uor_isr, IRQF_TRIGGER_FALLING,client->dev.driver->name, client);
  1256. if(err < 0){
  1257. input_free_device(ts.dev);
  1258. if(UOR6X5X_DEBUG)
  1259. printk(KERN_ERR "uor.c: Could not allocate GPIO intrrupt for touch screen !!!\n");
  1260. free_irq(client->irq, client);
  1261. err = -ENOMEM;
  1262. return err;
  1263. }
  1264. #ifdef CONFIG_HAS_EARLYSUSPEND
  1265. uor6x5x_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  1266. uor6x5x_early_suspend.suspend = uor_early_suspend;
  1267. uor6x5x_early_suspend.resume = uor_late_resume;
  1268. uor6x5x_early_suspend.param = client;
  1269. register_early_suspend(&uor6x5x_early_suspend);
  1270. #endif
  1271. if(UOR6X5X_DEBUG)
  1272. printk(KERN_ERR "uor_probe ok!\n");
  1273. return err;
  1274. }
  1275. static int __devexit uor_remove(struct i2c_client *client)
  1276. {
  1277. free_irq(client->irq, client);
  1278. return 0;
  1279. }
  1280. static void __exit uor_exit(void)
  1281. {
  1282. i2c_del_driver(&uor_i2c_driver);
  1283. }
  1284. module_init(uor_init);
  1285. module_exit(uor_exit);
  1286. MODULE_DESCRIPTION("UOR Touchscreen driver");
  1287. MODULE_AUTHOR("Ming-Wei Chang <mingwei@uutek.com.tw>");
  1288. MODULE_LICENSE("GPL");