itk.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. * linux/drivers/input/touchscreen/itk.c
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Written by x
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/input.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/i2c/itk.h>
  20. #include <linux/cdev.h>
  21. #include <asm/uaccess.h>
  22. #ifdef CONFIG_HAS_EARLYSUSPEND
  23. #include <linux/earlysuspend.h>
  24. static struct early_suspend itk_early_suspend;
  25. #endif
  26. // definition
  27. #define ILITEK_I2C_RETRY_COUNT 3
  28. #define ILITEK_I2C_DEFAULT_ADDRESS 0x41
  29. #define ILITEK_I2C_DRIVER_NAME "ilitek_i2c"
  30. #define ILITEK_FILE_DRIVER_NAME "ilitek_file"
  31. #define ILITEK_DEBUG_LEVEL KERN_INFO
  32. #define ILITEK_ERROR_LEVEL KERN_ALERT
  33. // i2c command for ilitek touch screen
  34. #define ILITEK_TP_CMD_READ_DATA 0x10
  35. #define ILITEK_TP_CMD_GET_RESOLUTION 0x20
  36. #define ILITEK_TP_CMD_SET_SLEEP_MODE 0x30
  37. #define ILITEK_TP_CMD_GET_VERSION 0x40
  38. #define ILITEK_TP_CMD_CALIBRATION 0xCC
  39. #define ILITEK_TP_CMD_ERASE_BACKGROUND 0xCE
  40. // define the application command
  41. #define ILITEK_IOCTL_BASE 100
  42. #define ILITEK_IOCTL_I2C_WRITE_DATA _IOWR(ILITEK_IOCTL_BASE, 0, unsigned char*)
  43. #define ILITEK_IOCTL_I2C_WRITE_LENGTH _IOWR(ILITEK_IOCTL_BASE, 1, int)
  44. #define ILITEK_IOCTL_I2C_READ_DATA _IOWR(ILITEK_IOCTL_BASE, 2, unsigned char*)
  45. #define ILITEK_IOCTL_I2C_READ_LENGTH _IOWR(ILITEK_IOCTL_BASE, 3, int)
  46. #define ILITEK_IOCTL_USB_WRITE_DATA _IOWR(ILITEK_IOCTL_BASE, 4, unsigned char*)
  47. #define ILITEK_IOCTL_USB_WRITE_LENGTH _IOWR(ILITEK_IOCTL_BASE, 5, int)
  48. #define ILITEK_IOCTL_USB_READ_DATA _IOWR(ILITEK_IOCTL_BASE, 6, unsigned char*)
  49. #define ILITEK_IOCTL_USB_READ_LENGTH _IOWR(ILITEK_IOCTL_BASE, 7, int)
  50. #define ILITEK_IOCTL_I2C_UPDATE_RESOLUTION _IOWR(ILITEK_IOCTL_BASE, 8, int)
  51. #define ILITEK_IOCTL_USB_UPDATE_RESOLUTION _IOWR(ILITEK_IOCTL_BASE, 9, int)
  52. #define ILITEK_IOCTL_I2C_SET_ADDRESS _IOWR(ILITEK_IOCTL_BASE, 10, int)
  53. #define ILITEK_IOCTL_I2C_UPDATE _IOWR(ILITEK_IOCTL_BASE, 11, int)
  54. #define ILITEK_IOCTL_STOP_READ_DATA _IOWR(ILITEK_IOCTL_BASE, 12, int)
  55. #define ILITEK_IOCTL_START_READ_DATA _IOWR(ILITEK_IOCTL_BASE, 13, int)
  56. static int ilitek_i2c_transfer(struct i2c_client*, struct i2c_msg*, int);
  57. static long ilitek_file_ioctl(struct file*, unsigned int, unsigned long);
  58. static int ilitek_file_open(struct inode*, struct file*);
  59. static ssize_t ilitek_file_write(struct file*, const char*, size_t, loff_t*);
  60. static ssize_t ilitek_file_read(struct file*, char*, size_t, loff_t*);
  61. static int ilitek_file_close(struct inode*, struct file*);
  62. struct i2c_client i2c_dev;
  63. struct semaphore wr_sem;
  64. // declare i2c data member
  65. struct i2c_data {
  66. // input device
  67. struct input_dev *input_dev;
  68. // i2c client
  69. struct i2c_client *client;
  70. // polling thread
  71. struct task_struct *thread;
  72. // maximum x
  73. int max_x;
  74. // maximum y
  75. int max_y;
  76. // maximum touch point
  77. int max_tp;
  78. // maximum key button
  79. int max_btn;
  80. // the total number of x channel
  81. int x_ch;
  82. // the total number of y channel
  83. int y_ch;
  84. // check whether i2c driver is registered success
  85. int valid_i2c_register;
  86. // check whether input driver is registered success
  87. int valid_input_register;
  88. // check whether the i2c enter suspend or not
  89. int stop_polling;
  90. // read semaphore
  91. struct semaphore wr_sem;
  92. // protocol version
  93. int protocol_ver;
  94. // valid irq request
  95. int valid_irq_request;
  96. // work queue for interrupt use only
  97. struct workqueue_struct *irq_work_queue;
  98. // work struct for work queue
  99. struct work_struct irq_work;
  100. struct timer_list timer;
  101. #ifdef CONFIG_HAS_EARLYSUSPEND
  102. struct early_suspend early_suspend;
  103. #endif
  104. //struct hrtimer timer;
  105. };
  106. struct dev_data {
  107. // device number
  108. dev_t devno;
  109. // character device
  110. struct cdev cdev;
  111. // class device
  112. struct class *class;
  113. };
  114. // global variables
  115. static struct i2c_data i2c;
  116. static struct dev_data dev;
  117. /*
  118. // file operation functions
  119. static int ilitek_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
  120. static int ilitek_file_open(struct inode*, struct file*);
  121. static ssize_t ilitek_file_write(struct file*, const char*, size_t, loff_t*);
  122. static int ilitek_file_close(struct inode*, struct file*);
  123. */
  124. // declare file operations
  125. struct file_operations ilitek_fops = {
  126. .unlocked_ioctl = ilitek_file_ioctl,
  127. .read = ilitek_file_read,
  128. .write = ilitek_file_write,
  129. .open = ilitek_file_open,
  130. .release = ilitek_file_close,
  131. };
  132. static int
  133. ilitek_file_open(
  134. struct inode *inode, struct file *filp)
  135. {
  136. return 0;
  137. }
  138. static ssize_t
  139. ilitek_file_write(
  140. struct file *filp, const char *buf, size_t count, loff_t *f_pos)
  141. {
  142. int ret;
  143. unsigned char buffer[128]={0};
  144. struct i2c_msg msgs[] = {
  145. {.addr = i2c.client->addr, .flags = 0, .len = count, .buf = buffer,}
  146. };
  147. // before sending data to touch device, we need to check whether the device is working or not
  148. // if(i2c.valid_i2c_register == 0){
  149. // printk(ILITEK_ERROR_LEVEL "%s, i2c device driver doesn't be registered\n", __func__);
  150. // return -1;
  151. // }
  152. // check the buffer size whether it exceeds the local buffer size or not
  153. if(count > 128){
  154. printk(ILITEK_ERROR_LEVEL "%s, buffer exceed 128 bytes\n", __func__);
  155. return -1;
  156. }
  157. // copy data from user space
  158. ret = copy_from_user(buffer, buf, count-1);
  159. if(ret < 0){
  160. printk(ILITEK_ERROR_LEVEL "%s, copy data from user space, failed", __func__);
  161. return -1;
  162. }
  163. // parsing command
  164. if(strcmp(buffer, "calibrate") == 0){
  165. buffer[0] = ILITEK_TP_CMD_ERASE_BACKGROUND;
  166. msgs[0].len = 1;
  167. ret = ilitek_i2c_transfer(i2c.client, msgs, 1);
  168. if(ret < 0){
  169. printk(ILITEK_DEBUG_LEVEL "%s, i2c erase background, failed\n", __func__);
  170. }
  171. else{
  172. printk(ILITEK_DEBUG_LEVEL "%s, i2c erase background, success\n", __func__);
  173. }
  174. buffer[0] = ILITEK_TP_CMD_CALIBRATION;
  175. msgs[0].len = 1;
  176. msleep(2000);
  177. ret = ilitek_i2c_transfer(i2c.client, msgs, 1);
  178. if(ret < 0){
  179. printk(ILITEK_DEBUG_LEVEL "%s, i2c calibration, failed\n", __func__);
  180. }
  181. else{
  182. printk(ILITEK_DEBUG_LEVEL "%s, i2c calibration, success\n", __func__);
  183. }
  184. msleep(1000);
  185. return count;
  186. }
  187. return -1;
  188. }
  189. static long
  190. ilitek_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  191. {
  192. static unsigned char buffer[64]={0};
  193. static int len=0;
  194. int ret;
  195. struct i2c_msg msgs[] = {
  196. {.addr = i2c.client->addr, .flags = 0, .len = len, .buf = buffer,}
  197. };
  198. // parsing ioctl command
  199. switch(cmd){
  200. case ILITEK_IOCTL_I2C_WRITE_DATA:
  201. ret = copy_from_user(buffer, (unsigned char*)arg, len);
  202. if(ret < 0){
  203. printk(ILITEK_ERROR_LEVEL "%s, copy data from user space, failed\n", __func__);
  204. return -1;
  205. }
  206. ret = ilitek_i2c_transfer(i2c.client, msgs, 1);
  207. if(ret < 0){
  208. printk(ILITEK_ERROR_LEVEL "%s, i2c write, failed\n", __func__);
  209. return -1;
  210. }
  211. break;
  212. case ILITEK_IOCTL_I2C_READ_DATA:
  213. msgs[0].flags = I2C_M_RD;
  214. ret = ilitek_i2c_transfer(i2c.client, msgs, 1);
  215. if(ret < 0){
  216. printk(ILITEK_ERROR_LEVEL "%s, i2c read, failed\n", __func__);
  217. return -1;
  218. }
  219. ret = copy_to_user((unsigned char*)arg, buffer, len);
  220. if(ret < 0){
  221. printk(ILITEK_ERROR_LEVEL "%s, copy data to user space, failed\n", __func__);
  222. return -1;
  223. }
  224. break;
  225. case ILITEK_IOCTL_I2C_WRITE_LENGTH:
  226. case ILITEK_IOCTL_I2C_READ_LENGTH:
  227. len = arg;
  228. break;
  229. case ILITEK_IOCTL_I2C_UPDATE_RESOLUTION:
  230. case ILITEK_IOCTL_I2C_SET_ADDRESS:
  231. case ILITEK_IOCTL_I2C_UPDATE:
  232. break;
  233. case ILITEK_IOCTL_START_READ_DATA:
  234. i2c.stop_polling = 0;
  235. break;
  236. case ILITEK_IOCTL_STOP_READ_DATA:
  237. i2c.stop_polling = 1;
  238. break;
  239. default:
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. static ssize_t
  245. ilitek_file_read(
  246. struct file *filp, char *buf, size_t count, loff_t *f_pos)
  247. {
  248. return 0;
  249. }
  250. static int
  251. ilitek_file_close(
  252. struct inode *inode, struct file *filp)
  253. {
  254. return 0;
  255. }
  256. #define DRIVER_NAME "itk"
  257. #define DRIVER_VERSION "1"
  258. //#define ITK_TS_DEBUG_REPORT
  259. //#define ITK_TS_DEBUG_READ
  260. //#define ITK_TS_DEBUG_INFO
  261. //#define TS_DELAY_WORK
  262. /* periodic polling delay and period */
  263. #define TS_POLL_DELAY (1 * 1000000)
  264. #define TS_POLL_PERIOD (5 * 1000000)
  265. #define MAX_SUPPORT_POINT 5 //just support 2 point now
  266. #define ITK_INFO_ADDR 0x10
  267. #define ITK_INFO_LEN 9
  268. /**
  269. * struct ts_event - touchscreen event structure
  270. * @contactid: num id
  271. * @pendown: state of the pen
  272. * @valid: is valid data
  273. * @x: X-coordinate of the event
  274. * @y: Y-coordinate of the event
  275. * @z: pressure of the event
  276. */
  277. struct ts_event {
  278. short contactid;
  279. short pendown;
  280. short valid;
  281. short x;
  282. short y;
  283. short xz;
  284. short yz;
  285. short xw;
  286. short yw;
  287. };
  288. /**
  289. * struct itk - touchscreen controller context
  290. * @client: I2C client
  291. * @input: touchscreen input device
  292. * @lock: lock for resource protection
  293. * @timer: timer for periodical polling
  294. * @work: workqueue structure
  295. * @event[]: touchscreen event buff
  296. * @pendown: current pen state
  297. * @touching_num: count for check touching fingers
  298. * @lcd_xmax: lcd resolution
  299. * @lcd_ymax: lcd resolution
  300. * @tp_xmax: max virtual resolution
  301. * @tp_ymax: max virtual resolution
  302. * @pdata: platform-specific information
  303. * @running: workqueue is running
  304. * @work_exit: the workqueue need to exit
  305. */
  306. struct itk {
  307. struct i2c_client *client;
  308. struct input_dev *input;
  309. spinlock_t lock;
  310. struct hrtimer timer;
  311. #ifdef TS_DELAY_WORK
  312. struct delayed_work work;
  313. #else
  314. struct work_struct work;
  315. struct workqueue_struct *workqueue;
  316. #endif
  317. struct ts_event event[MAX_SUPPORT_POINT];
  318. unsigned pendown:1;
  319. unsigned touching_num;
  320. int lcd_xmax;
  321. int lcd_ymax;
  322. int tp_xmax;
  323. int tp_ymax;
  324. struct itk_platform_data *pdata;
  325. int running; //add by sz.zhuw 20110927
  326. int work_exit;
  327. };
  328. static int
  329. ilitek_i2c_transfer(
  330. struct i2c_client *client, struct i2c_msg *msgs, int cnt)
  331. {
  332. int ret, count=ILITEK_I2C_RETRY_COUNT;
  333. while(count >= 0){
  334. count-= 1;
  335. ret = down_interruptible(&wr_sem);
  336. ret = i2c_transfer(client->adapter, msgs, cnt);
  337. up(&wr_sem);
  338. if(ret < 0){
  339. msleep(500);
  340. continue;
  341. }
  342. break;
  343. }
  344. return ret;
  345. }
  346. /**
  347. * itk_get_pendown_state() - obtain the current pen state
  348. * @ts: touchscreen controller context
  349. */
  350. static int itk_get_pendown_state(struct itk *ts)
  351. {
  352. int state = 0;
  353. if (ts && ts->pdata && ts->pdata->get_irq_level)
  354. state = !ts->pdata->get_irq_level();
  355. return state;
  356. }
  357. static int itk_register_input(struct itk *ts)
  358. {
  359. int ret;
  360. struct input_dev *dev;
  361. dev = input_allocate_device();
  362. if (dev == NULL)
  363. return -1;
  364. dev->name = "Touch Screen";
  365. dev->phys = "I2C";
  366. dev->id.bustype = BUS_I2C;
  367. dev->id.vendor = 0x222a;
  368. dev->id.product = 0x0001;
  369. dev->id.version = 0x0001;
  370. set_bit(EV_ABS, dev->evbit);
  371. set_bit(EV_KEY, dev->evbit);
  372. set_bit(BTN_TOUCH, dev->keybit);
  373. set_bit(ABS_MT_TOUCH_MAJOR, dev->absbit);
  374. set_bit(ABS_MT_WIDTH_MAJOR, dev->absbit);
  375. set_bit(ABS_MT_POSITION_X, dev->absbit);
  376. set_bit(ABS_MT_POSITION_Y, dev->absbit);
  377. set_bit(ABS_MT_TRACKING_ID, dev->absbit);
  378. //set_bit(ABS_MT_PRESSURE, dev->absbit);
  379. input_set_abs_params(dev, ABS_X, 0, ts->tp_xmax, 0, 0);
  380. input_set_abs_params(dev, ABS_Y, 0, ts->tp_ymax, 0, 0);
  381. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, ts->tp_xmax, 0, 0);
  382. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, ts->tp_ymax, 0, 0);
  383. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
  384. input_set_abs_params(dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
  385. input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);
  386. //input_set_abs_params(dev, ABS_MT_PRESSURE, 0, ???, 0, 0);
  387. ret = input_register_device(dev);
  388. if (ret < 0) {
  389. input_free_device(dev);
  390. return -1;
  391. }
  392. ts->input = dev;
  393. return 0;
  394. }
  395. static int itk_read_block(struct i2c_client *client, u8 addr, u8 len, u8 *data)
  396. {
  397. int ret;
  398. u8 msgbuf0[1] = { addr };
  399. u16 slave = client->addr;
  400. u16 flags = client->flags;
  401. struct i2c_msg msg[2] = {
  402. { slave, flags, 1, msgbuf0 },
  403. { slave, flags|I2C_M_RD, len, data }
  404. };
  405. ret = down_interruptible(&wr_sem);
  406. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  407. up(&wr_sem);
  408. return ret;
  409. }
  410. /* //just mark for not used warning
  411. static int itk_write_block(struct i2c_client *client, u8 addr, u8 len, u8 *data)
  412. {
  413. u8 msgbuf0[1] = { addr };
  414. u16 slave = client->addr;
  415. u16 flags = client->flags;
  416. struct i2c_msg msg[2] = {
  417. { slave, flags, 1, msgbuf0 },
  418. { slave, flags, len, data }
  419. };
  420. return i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  421. }
  422. */
  423. static void itk_reset(struct itk *ts)
  424. {
  425. int i = 0;
  426. if (NULL == ts)
  427. return;
  428. memset(ts->event, 0, sizeof(struct ts_event)*MAX_SUPPORT_POINT);
  429. for (i=0; i<MAX_SUPPORT_POINT; i++)
  430. {
  431. ts->event[i].pendown = -1;
  432. }
  433. return;
  434. }
  435. static int itk_read_sensor(struct itk *ts)
  436. {
  437. int ret=-1, status = 0;
  438. u8 data[ITK_INFO_LEN];
  439. /* To ensure data coherency, read the sensor with a single transaction. */
  440. ret = itk_read_block(ts->client, ITK_INFO_ADDR, ITK_INFO_LEN, data);
  441. if (ret < 0) {
  442. dev_err(&ts->client->dev, "Read block failed: %d\n", ret);
  443. return ret;
  444. }
  445. ret = 0;
  446. status = data[0]&0x3;
  447. ts->event[0].x = data[2]<<8|data[1];
  448. ts->event[0].y = data[4]<<8|data[3];
  449. ts->event[1].x = data[6]<<8|data[5];
  450. ts->event[1].y = data[8]<<8|data[7];
  451. if(status & 0x1)
  452. {
  453. if((ts->event[0].x > (ts->pdata->tp_max_width - 4)) || (ts->event[0].x < 4))
  454. return 2;//status = status & 2;
  455. if((ts->event[0].y > (ts->pdata->tp_max_height - 4)) || (ts->event[0].y < 4))
  456. return 2;//status = status & 2;
  457. }
  458. if(status & 0x2)
  459. {
  460. if((ts->event[1].x > (ts->pdata->tp_max_width - 4)) || (ts->event[1].x < 4))
  461. return 2;//status = status & 1;
  462. if((ts->event[1].y > (ts->pdata->tp_max_height - 4)) || (ts->event[1].y < 4))
  463. return 2;//status = status & 1;
  464. }
  465. if (ts->pdata->swap_xy){
  466. swap(ts->event[0].x, ts->event[0].y);
  467. swap(ts->event[1].x, ts->event[1].y);
  468. }
  469. if (ts->pdata->xpol){
  470. ts->event[0].x = ts->pdata->tp_max_width - ts->event[0].x;
  471. ts->event[1].x = ts->pdata->tp_max_width - ts->event[1].x;
  472. }
  473. if (ts->pdata->ypol){
  474. ts->event[0].y = ts->pdata->tp_max_height - ts->event[0].y;
  475. ts->event[1].y = ts->pdata->tp_max_height - ts->event[1].y;
  476. }
  477. ts->touching_num = status;
  478. #ifdef ITK_TS_DEBUG_READ
  479. printk(KERN_INFO "\nread_sensor status = %d, event[0]->x = %d, event[0]->y = %d, event[1]->x = %d, event[1]->y = %d\n",
  480. ts->touching_num, ts->event[0].x, ts->event[0].y, ts->event[1].x, ts->event[1].y);
  481. #endif
  482. return 0;
  483. }
  484. /**
  485. * itk_work() - work queue handler (initiated by the interrupt handler)
  486. * @work: work queue to handle
  487. */
  488. static void itk_work(struct work_struct *work)
  489. {
  490. #ifdef TS_DELAY_WORK
  491. struct itk *ts = container_of(to_delayed_work(work), struct itk, work);
  492. #else
  493. struct itk *ts = container_of(work, struct itk, work);
  494. #endif
  495. struct ts_event *event;
  496. int i = 0, j = 1;
  497. int ret = -1;
  498. if(ts->work_exit){
  499. ts->running = 0;
  500. ts->work_exit = 0;
  501. return;
  502. }
  503. if (itk_get_pendown_state(ts))
  504. {
  505. ret = itk_read_sensor(ts);
  506. if (ret < 0) {
  507. printk(KERN_INFO "work read i2c failed\n");
  508. goto restart;
  509. }
  510. else if(ret == 2) //有手超過邊界...所有點都不上報
  511. {
  512. ts->touching_num = 0;
  513. }
  514. if (!ts->pendown) {
  515. ts->pendown = 1;
  516. #ifdef ITK_TS_DEBUG_INFO
  517. printk(KERN_INFO "DOWN\n");
  518. #endif
  519. input_report_key(ts->input, BTN_TOUCH, 1);
  520. }
  521. switch (ts->touching_num)
  522. {
  523. case 0x1:
  524. i = 0;
  525. j = 1;
  526. break;
  527. case 0x2:
  528. i = 1;
  529. j = 2;
  530. break;
  531. case 0x3:
  532. i = 0;
  533. j = 2;
  534. break;
  535. case 0x0:
  536. default:
  537. i = 0;
  538. j = 0;
  539. break;
  540. }
  541. for (; i<j; i++)
  542. {
  543. event = &ts->event[i];
  544. input_report_abs(ts->input, ABS_MT_TRACKING_ID, i);
  545. #ifdef ITK_TS_DEBUG_REPORT
  546. printk(KERN_INFO "\nreport ABS_MT_TRACKING_ID %d\n", i);
  547. #endif
  548. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 1);
  549. #ifdef ITK_TS_DEBUG_REPORT
  550. printk(KERN_INFO "report ABS_MT_TOUCH_MAJOR %d\n", 1);
  551. #endif
  552. input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 0);
  553. #ifdef ITK_TS_DEBUG_REPORT
  554. printk(KERN_INFO "report ABS_MT_WIDTH_MAJOR %d\n", 0);
  555. #endif
  556. input_report_abs(ts->input, ABS_MT_POSITION_X, event->x);
  557. input_report_abs(ts->input, ABS_MT_POSITION_Y, event->y);
  558. #ifdef ITK_TS_DEBUG_REPORT
  559. printk(KERN_INFO "report ABS_MT_POSITION_XY %d,%d\n", event->x, event->y);
  560. #endif
  561. input_mt_sync(ts->input);
  562. #ifdef ITK_TS_DEBUG_REPORT
  563. printk(KERN_INFO "input_mt_sync\n");
  564. #endif
  565. if (ts->touching_num == 0x3)
  566. {
  567. if (i == 0)
  568. continue;
  569. }
  570. input_sync(ts->input);
  571. #ifdef ITK_TS_DEBUG_REPORT
  572. printk(KERN_INFO "input_sync\n");
  573. #endif
  574. }
  575. restart:
  576. #ifdef TS_DELAY_WORK
  577. schedule_delayed_work(&ts->work, msecs_to_jiffies(TS_POLL_PERIOD));
  578. #else
  579. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_MODE_REL);
  580. #endif
  581. }
  582. else {
  583. /* enable IRQ after the pen was lifted */
  584. if (ts->pendown) {
  585. ts->pendown = 0;
  586. #ifdef ITK_TS_DEBUG_INFO
  587. printk(KERN_INFO "UP\n");
  588. #endif
  589. input_report_key(ts->input, BTN_TOUCH, 0);
  590. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 0);
  591. #ifdef ITK_TS_DEBUG_REPORT
  592. printk(KERN_INFO "report ABS_MT_TOUCH_MAJOR %d\n", 0);
  593. #endif
  594. input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 0);
  595. #ifdef ITK_TS_DEBUG_REPORT
  596. printk(KERN_INFO "report ABS_MT_WIDTH_MAJOR %d\n", 0);
  597. #endif
  598. input_mt_sync(ts->input);
  599. #ifdef ITK_TS_DEBUG_REPORT
  600. printk(KERN_INFO "input_mt_sync\n");
  601. #endif
  602. input_sync(ts->input);
  603. #ifdef ITK_TS_DEBUG_REPORT
  604. printk(KERN_INFO "input_sync\n");
  605. #endif
  606. itk_reset(ts);
  607. }
  608. ts->touching_num = 0;
  609. enable_irq(ts->client->irq);
  610. ts->running = 0;
  611. }
  612. }
  613. #ifndef TS_DELAY_WORK
  614. /**
  615. * itk_timer() - timer callback function
  616. * @timer: timer that caused this function call
  617. */
  618. static enum hrtimer_restart itk_timer(struct hrtimer *timer)
  619. {
  620. struct itk *ts = container_of(timer, struct itk, timer);
  621. unsigned long flags = 0;
  622. ts->running = 1;
  623. spin_lock_irqsave(&ts->lock, flags);
  624. // printk(KERN_INFO "enter timer\n");
  625. queue_work(ts->workqueue, &ts->work);
  626. spin_unlock_irqrestore(&ts->lock, flags);
  627. return HRTIMER_NORESTART;
  628. }
  629. #endif
  630. /**
  631. * itk_interrupt() - interrupt handler for touch events
  632. * @irq: interrupt to handle
  633. * @dev_id: device-specific information
  634. */
  635. static irqreturn_t itk_interrupt(int irq, void *dev_id)
  636. {
  637. struct i2c_client *client = (struct i2c_client *)dev_id;
  638. struct itk *ts = i2c_get_clientdata(client);
  639. unsigned long flags;
  640. spin_lock_irqsave(&ts->lock, flags);
  641. #ifdef ITK_TS_DEBUG_REPORT
  642. printk(KERN_INFO "itk_interrupt() enter penirq\n");
  643. #endif
  644. /* if the pen is down, disable IRQ and start timer chain */
  645. if (itk_get_pendown_state(ts)) {
  646. disable_irq_nosync(client->irq);
  647. ts->running = 1;
  648. #ifdef TS_DELAY_WORK
  649. schedule_delayed_work(&ts->work, msecs_to_jiffies(TS_POLL_DELAY));
  650. #else
  651. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY), HRTIMER_MODE_REL);
  652. #endif
  653. }
  654. spin_unlock_irqrestore(&ts->lock, flags);
  655. return IRQ_HANDLED;
  656. }
  657. /**
  658. * itk_probe() - initialize the I2C client
  659. * @client: client to initialize
  660. * @id: I2C device ID
  661. */
  662. struct itk_platform_data * itk_data;
  663. #ifdef CONFIG_HAS_EARLYSUSPEND
  664. static void aml_itk_early_suspend(struct early_suspend *h)
  665. {
  666. int ret;
  667. uint8_t cmd = ILITEK_TP_CMD_SET_SLEEP_MODE;
  668. struct itk *ts = (struct itk *)(h->param);
  669. struct i2c_msg msgs_cmd[] = {
  670. { .addr = i2c.client->addr, .flags = 0, .len = 1, .buf = &cmd, },
  671. };
  672. printk(KERN_INFO "%s() enter\n", __FUNCTION__);
  673. if (ts->running) {
  674. ts->work_exit = 1;
  675. mdelay(10);
  676. }
  677. else {
  678. disable_irq(i2c.client->irq);
  679. }
  680. printk(ILITEK_DEBUG_LEVEL "%s() Disable i2c IRQ\n", __func__);
  681. ret = ilitek_i2c_transfer(i2c.client, msgs_cmd, 1);
  682. if (ret < 0)
  683. printk(KERN_ERR "%s() i2c write error %d\n", __func__, ret);
  684. }
  685. static void aml_read_itk_version(void)
  686. {
  687. u8 data_fv[3];
  688. u8 data_pv[2];
  689. int ret;
  690. ret = itk_read_block(i2c.client, 0x40, 3, data_fv);
  691. if(ret < 0){
  692. printk("%s read Firmware Version failed\n",__FUNCTION__);
  693. }else{
  694. printk("%s read Firmware:%d:%d:%d\n",__FUNCTION__,data_fv[0],data_fv[1],data_fv[2]);
  695. }
  696. ret = itk_read_block(i2c.client, 0x42, 2, data_pv);
  697. if(ret < 0){
  698. printk("%s read Protocol Version failed\n",__FUNCTION__);
  699. }else{
  700. printk("%s read Protocol:%d:%d\n",__FUNCTION__,data_pv[0],data_pv[1]);
  701. }
  702. }
  703. static void aml_itk_once_reset(void)
  704. {
  705. int ret;
  706. uint8_t cmd = ILITEK_TP_CMD_SET_SLEEP_MODE;
  707. struct i2c_msg msgs_cmd[] = {
  708. {
  709. .addr = i2c.client->addr, .flags = 0, .len = 1, .buf = &cmd,},
  710. };
  711. ret = ilitek_i2c_transfer(i2c.client, msgs_cmd, 1);
  712. mdelay(5);
  713. if(itk_data && itk_data->get_irq_level){
  714. itk_data->touch_on(0);
  715. mdelay(20);
  716. itk_data->touch_on(1);
  717. mdelay(10);
  718. printk("%s\n",__FUNCTION__);
  719. }
  720. }
  721. static void aml_itk_late_resume(struct early_suspend *h)
  722. {
  723. printk("enter -----> %s \n",__FUNCTION__);
  724. if(itk_data->touch_on){
  725. itk_data->touch_on(0);
  726. msleep(20);
  727. itk_data->touch_on(1);
  728. msleep(10);
  729. enable_irq(i2c.client->irq);
  730. printk(ILITEK_DEBUG_LEVEL "%s, disable i2c irq\n", __func__);
  731. }
  732. }
  733. #endif
  734. static int itk_probe(struct i2c_client *client,
  735. const struct i2c_device_id *id)
  736. {
  737. struct itk *ts;
  738. int err = 0;
  739. ts = kzalloc(sizeof(struct itk), GFP_KERNEL);
  740. if (!ts) {
  741. err = -ENOMEM;
  742. goto fail;
  743. }
  744. ts->client = client;
  745. i2c.client = client;
  746. itk_reset(ts);
  747. /* setup platform-specific hooks */
  748. ts->pdata = (struct itk_platform_data*)client->dev.platform_data;
  749. itk_data = (struct itk_platform_data*)client->dev.platform_data;
  750. if (!ts->pdata || !ts->pdata->init_irq || !ts->pdata->get_irq_level) {
  751. dev_err(&client->dev, "no platform-specific callbacks "
  752. "provided\n");
  753. err = -ENXIO;
  754. goto fail;
  755. }
  756. else
  757. {
  758. ts->lcd_xmax = ((struct itk_platform_data*) client->dev.platform_data)->lcd_max_width;
  759. ts->lcd_ymax = ((struct itk_platform_data*) client->dev.platform_data)->lcd_max_height;
  760. ts->tp_xmax = ((struct itk_platform_data*) client->dev.platform_data)->tp_max_width;
  761. ts->tp_ymax = ((struct itk_platform_data*) client->dev.platform_data)->tp_max_height;
  762. }
  763. if (itk_register_input(ts) < 0) {
  764. dev_err(&client->dev, "register input fail!\n");
  765. goto fail;
  766. }
  767. #ifdef CONFIG_HAS_EARLYSUSPEND
  768. aml_itk_once_reset();
  769. aml_read_itk_version();
  770. #endif
  771. ts->running = 0;
  772. ts->work_exit = 0;
  773. if (ts->pdata->init_irq) {
  774. err = ts->pdata->init_irq();
  775. if (err < 0) {
  776. dev_err(&client->dev, "failed to initialize IRQ#%d: "
  777. "%d\n", client->irq, err);
  778. goto fail;
  779. }
  780. }
  781. memcpy(&i2c_dev, client, sizeof(struct i2c_client));
  782. spin_lock_init(&ts->lock);
  783. #ifdef TS_DELAY_WORK
  784. INIT_DELAYED_WORK(&ts->work, itk_work);
  785. #else
  786. hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  787. ts->timer.function = itk_timer;
  788. INIT_WORK(&ts->work, itk_work);
  789. ts->workqueue = create_singlethread_workqueue("itk");
  790. if (ts->workqueue == NULL) {
  791. dev_err(&client->dev, "can't create work queue\n");
  792. err = -ENOMEM;
  793. goto fail;
  794. }
  795. #ifdef ITK_TS_DEBUG_REPORT
  796. printk("work create: %x\n", ts->workqueue);
  797. #endif
  798. #endif
  799. ts->pendown = 0;
  800. ts->touching_num = 0;
  801. err = request_irq(client->irq, itk_interrupt, IRQF_TRIGGER_FALLING,
  802. client->dev.driver->name, client);
  803. if (err) {
  804. dev_err(&client->dev, "failed to request IRQ#%d: %d\n",
  805. client->irq, err);
  806. goto fail_irq;
  807. }
  808. #ifdef CONFIG_HAS_EARLYSUSPEND
  809. itk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  810. itk_early_suspend.suspend = aml_itk_early_suspend;
  811. itk_early_suspend.resume = aml_itk_late_resume;
  812. itk_early_suspend.param = ts;
  813. register_early_suspend(&itk_early_suspend);
  814. #endif
  815. i2c_set_clientdata(client, ts);
  816. //schedule_delayed_work(&ts->cal_work, 20*HZ);
  817. err = 0;
  818. goto out;
  819. fail_irq:
  820. free_irq(client->irq, client);
  821. fail:
  822. if (ts) {
  823. input_free_device(ts->input);
  824. kfree(ts);
  825. }
  826. i2c_set_clientdata(client, NULL);
  827. out:
  828. itk_read_sensor(ts);
  829. itk_reset(ts);
  830. itk_data->touch_on(0);
  831. msleep(10);
  832. itk_data->touch_on(1);
  833. printk("itk touch screen driver ok\n");
  834. return err;
  835. }
  836. /**
  837. * itk_remove() - cleanup the I2C client
  838. * @client: client to clean up
  839. */
  840. static int itk_remove(struct i2c_client *client)
  841. {
  842. struct itk *priv = i2c_get_clientdata(client);
  843. free_irq(client->irq, client);
  844. i2c_set_clientdata(client, NULL);
  845. input_unregister_device(priv->input);
  846. kfree(priv);
  847. #ifdef CONFIG_HAS_EARLYSUSPEND
  848. unregister_early_suspend(&itk_early_suspend);
  849. #endif
  850. return 0;
  851. }
  852. static const struct i2c_device_id itk_ids[] = {
  853. { DRIVER_NAME, 0 },
  854. { }
  855. };
  856. MODULE_DEVICE_TABLE(i2c, itk_ids);
  857. /* ITK I2C Capacitive Touch Screen driver */
  858. static struct i2c_driver itk_driver = {
  859. .driver = {
  860. .name = DRIVER_NAME,
  861. .owner = THIS_MODULE,
  862. },
  863. .probe = itk_probe,
  864. .remove = __devexit_p(itk_remove),
  865. .id_table = itk_ids,
  866. };
  867. /**
  868. * itk_init() - module initialization
  869. */
  870. static int __init itk_init(void)
  871. {
  872. int ret = 0;
  873. sema_init(&wr_sem, 1);
  874. ret = i2c_add_driver(&itk_driver);
  875. ret |= alloc_chrdev_region(&dev.devno, 0, 1, ILITEK_FILE_DRIVER_NAME);
  876. if(ret){
  877. printk("%s, can't alloc chrdev\n", __func__);
  878. }
  879. printk("%s, register chrdev(%d, %d)\n", __func__, MAJOR(dev.devno), MINOR(dev.devno));
  880. cdev_init(&dev.cdev, &ilitek_fops);
  881. dev.cdev.owner = THIS_MODULE;
  882. ret |= cdev_add(&dev.cdev, dev.devno, 1);
  883. if(ret < 0){
  884. printk("%s, add char devive error, ret %d\n", __func__, ret);
  885. }
  886. dev.class = class_create(THIS_MODULE, ILITEK_FILE_DRIVER_NAME);
  887. if(IS_ERR(dev.class)){
  888. printk("%s, creating class error\n", __func__);
  889. }
  890. device_create(dev.class, NULL, dev.devno, NULL, "ilitek_ctrl");
  891. return ret;
  892. }
  893. /**
  894. * itk_exit() - module cleanup
  895. */
  896. static void __exit itk_exit(void)
  897. {
  898. i2c_del_driver(&itk_driver);
  899. cdev_del(&dev.cdev);
  900. unregister_chrdev_region(dev.devno, 1);
  901. device_destroy(dev.class, dev.devno);
  902. class_destroy(dev.class);
  903. }
  904. module_init(itk_init);
  905. module_exit(itk_exit);
  906. MODULE_AUTHOR("");
  907. MODULE_DESCRIPTION("itk I2C Capacitive Touch Screen driver");
  908. MODULE_LICENSE("GPL v2");
  909. MODULE_VERSION(DRIVER_VERSION);