stkxxx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * linux/drivers/input/touchscreen/stkxxx.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/stkxxx.h>
  20. //#define STKXXX_DEBUG
  21. #define DRIVER_NAME "stkxxx"
  22. #define DRIVER_VERSION "1"
  23. /* basic commands */
  24. #define CANDO 1
  25. #define SINTEK 0
  26. #define SINTEK_NEW 2
  27. //#define TS_DELAY_WORK
  28. #define MULTI_TOUCH
  29. /* periodic polling delay and period */
  30. #define TS_POLL_DELAY (1 * 1000000)
  31. #define TS_POLL_PERIOD (5 * 1000000)
  32. /**
  33. * struct ts_event - touchscreen event structure
  34. * @pendown: state of the pen
  35. * @x: X-coordinate of the event
  36. * @y: Y-coordinate of the event
  37. * @z: pressure of the event
  38. */
  39. struct ts_event {
  40. short x;
  41. short y;
  42. short xz;
  43. short yz;
  44. short xw;
  45. short yw;
  46. };
  47. /**
  48. * struct stkxxx - touchscreen controller context
  49. * @client: I2C client
  50. * @input: touchscreen input device
  51. * @lock: lock for resource protection
  52. * @timer: timer for periodical polling
  53. * @work: workqueue structure
  54. * @pendown: current pen state
  55. * @event: current touchscreen event
  56. * @pdata: platform-specific information
  57. */
  58. struct stkxxx {
  59. struct i2c_client *client;
  60. struct input_dev *input;
  61. spinlock_t lock;
  62. struct hrtimer timer;
  63. #ifdef TS_DELAY_WORK
  64. struct delayed_work work;
  65. #else
  66. struct work_struct work;
  67. struct workqueue_struct *workqueue;
  68. #endif
  69. struct ts_event event[5];
  70. unsigned pendown:1;
  71. unsigned touching_num;
  72. unsigned pre_touching_num;
  73. int lcd_xmax;
  74. int lcd_ymax;
  75. int tp_xmax;
  76. int tp_ymax;
  77. int vendor;
  78. struct stkxxx_platform_data *pdata;
  79. struct delayed_work cal_work;
  80. };
  81. /**
  82. * stkxxx_get_pendown_state() - obtain the current pen state
  83. * @ts: touchscreen controller context
  84. */
  85. static int stkxxx_get_pendown_state(struct stkxxx *ts)
  86. {
  87. int state = 0;
  88. if (ts && ts->pdata && ts->pdata->get_irq_level)
  89. state = !ts->pdata->get_irq_level();
  90. return state;
  91. }
  92. static int stkxxx_register_input(struct stkxxx *ts)
  93. {
  94. int ret;
  95. struct input_dev *dev;
  96. dev = input_allocate_device();
  97. if (dev == NULL)
  98. return -1;
  99. dev->name = "sintek capacitive touchscreen";
  100. dev->phys = "I2C";
  101. dev->id.bustype = BUS_I2C;
  102. set_bit(EV_ABS, dev->evbit);
  103. set_bit(EV_KEY, dev->evbit);
  104. set_bit(BTN_TOUCH, dev->keybit);
  105. set_bit(ABS_MT_TOUCH_MAJOR, dev->absbit);
  106. set_bit(ABS_MT_WIDTH_MAJOR, dev->absbit);
  107. set_bit(ABS_MT_POSITION_X, dev->absbit);
  108. set_bit(ABS_MT_POSITION_Y, dev->absbit);
  109. set_bit(ABS_MT_TRACKING_ID, dev->absbit);
  110. //set_bit(ABS_MT_PRESSURE, dev->absbit);
  111. input_set_abs_params(dev, ABS_X, 0, ts->lcd_xmax, 0, 0);
  112. input_set_abs_params(dev, ABS_Y, 0, ts->lcd_ymax, 0, 0);
  113. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, ts->lcd_xmax, 0, 0);
  114. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, ts->lcd_ymax, 0, 0);
  115. input_set_abs_params(dev, ABS_MT_TOUCH_MAJOR, 0, ts->lcd_xmax, 0, 0);
  116. input_set_abs_params(dev, ABS_MT_WIDTH_MAJOR, 0, ts->lcd_xmax, 0, 0);
  117. input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, 10, 0, 0);
  118. //input_set_abs_params(dev, ABS_MT_PRESSURE, 0, ???, 0, 0);
  119. ret = input_register_device(dev);
  120. if (ret < 0) {
  121. input_free_device(dev);
  122. return -1;
  123. }
  124. ts->input = dev;
  125. return 0;
  126. }
  127. static int stkxxx_read_block(struct i2c_client *client, u8 addr, u8 len, u8 *data)
  128. {
  129. u8 msgbuf0[1] = { addr };
  130. u16 slave = client->addr;
  131. u16 flags = client->flags;
  132. struct i2c_msg msg[2] = {
  133. { slave, flags, 1, msgbuf0 },
  134. { slave, flags|I2C_M_RD, len, data }
  135. };
  136. return i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  137. }
  138. static int stkxxx_write_block(struct i2c_client *client, u8 addr, u8 len, u8 *data)
  139. {
  140. u8 msgbuf0[1] = { addr };
  141. u16 slave = client->addr;
  142. u16 flags = client->flags;
  143. struct i2c_msg msg[2] = {
  144. { slave, flags, 1, msgbuf0 },
  145. { slave, flags, len, data }
  146. };
  147. return i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  148. }
  149. static int stkxxx_write_reg(struct i2c_client *client, u8 addr, u8 data)
  150. {
  151. u8 buf[2] = {addr, data};
  152. struct i2c_msg msg = {
  153. .addr = client->addr,
  154. .flags = !I2C_M_RD,
  155. .len = 2,
  156. .buf = buf,
  157. };
  158. return i2c_transfer(client->adapter, &msg, 1);
  159. }
  160. static void stkxxx_reset(struct stkxxx *ts)
  161. {
  162. unsigned char data[6] = {0, 0, 0, 0 ,0, 0};
  163. int ret;
  164. ret = stkxxx_read_block(ts->client, 26, 6, data);
  165. #ifdef STKXXX_DEBUG
  166. printk(" (26)=%d, (27)=%d, (28)=%d, (29)=%d, (30)=%d,(31)=%d , kk=%d\n",
  167. data[0], data[1], data[2], data[3],data[4], data[5], ret);
  168. #endif
  169. if (ret < 0) {
  170. dev_err(&ts->client->dev, "Read pannel info failed: %d\n", ret);
  171. return ret;
  172. }
  173. if (data[4] ==CANDO)
  174. {
  175. ts->lcd_xmax = data[1]<<8|data[0];
  176. ts->lcd_ymax = data[3]<<8|data[2];
  177. #ifdef STKXXX_DEBUG
  178. printk("CANDO: xmax=%d, ymax=%d\n", ts->lcd_xmax, ts->lcd_ymax);
  179. #endif
  180. ts->vendor = CANDO;
  181. }
  182. else if (data[4] ==SINTEK||data[4] ==SINTEK_NEW)
  183. {
  184. ts->lcd_xmax = 1024;
  185. ts->lcd_ymax = 600;
  186. #ifdef STKXXX_DEBUG
  187. printk("SINTEK: xmax=%d, ymax=%d\n", ts->lcd_xmax, ts->lcd_ymax);
  188. #endif
  189. ts->vendor = SINTEK;
  190. }
  191. if (stkxxx_write_reg(ts->client, 55, 0x03) < 0) {
  192. printk("calibration reg failed\n");
  193. }
  194. else
  195. printk("calibration reg ok\n");
  196. }
  197. #define STK_INFO_LEN 20
  198. #define STK_INFO_ADDR 0
  199. #define PRE_INFO_ADDR 1
  200. #define FIRST_POINT_ADDR 2
  201. #define X_OFFSET 0
  202. #define Y_OFFSET 2
  203. #define XW_OFFSET 8
  204. #define YW_OFFSET 9
  205. #define XZ_OFFSET 12
  206. #define YZ_OFFSET 13
  207. static int stkxxx_read_sensor(struct stkxxx *ts)
  208. {
  209. int ret,i;
  210. u8 data[STK_INFO_LEN];
  211. struct ts_event *event;
  212. /* To ensure data coherency, read the sensor with a single transaction. */
  213. ret = stkxxx_read_block(ts->client, STK_INFO_ADDR, STK_INFO_LEN, data);
  214. if (ret < 0) {
  215. dev_err(&ts->client->dev, "Read block failed: %d\n", ret);
  216. return ret;
  217. }
  218. ts->touching_num = data[0];
  219. ts->pre_touching_num = data[1];
  220. event = &ts->event[0];
  221. int ba = 2;
  222. #ifdef STKXXX_DEBUG
  223. printk(KERN_INFO "num = %d, pre_num = %d\n", ts->touching_num, ts->pre_touching_num);
  224. #endif
  225. for (i=0; i<ts->touching_num; i++) {
  226. event->x = (data[ba+X_OFFSET+1] << 8) | data[ba+X_OFFSET];
  227. event->y = (data[ba+Y_OFFSET+1] << 8) | data[ba+Y_OFFSET];
  228. event->xw = data[ba+XW_OFFSET];
  229. event->yw = data[ba+YW_OFFSET];
  230. event->xz = data[ba+XZ_OFFSET];
  231. event->yz = data[ba+YZ_OFFSET];
  232. #ifdef STKXXX_DEBUG
  233. printk(KERN_INFO "id = %d, x = %d, y = %d\n", i, event->x, event->y);
  234. #endif
  235. ba += 4;
  236. event++;
  237. }
  238. return 0;
  239. }
  240. /**
  241. * stkxxx_work() - work queue handler (initiated by the interrupt handler)
  242. * @work: work queue to handle
  243. */
  244. static void stkxxx_work(struct work_struct *work)
  245. {
  246. #ifdef TS_DELAY_WORK
  247. struct stkxxx *ts = container_of(to_delayed_work(work), struct stkxxx, work);
  248. #else
  249. struct stkxxx *ts = container_of(work, struct stkxxx, work);
  250. #endif
  251. struct ts_event *event;
  252. int i;
  253. if (stkxxx_get_pendown_state(ts)) {
  254. if (stkxxx_read_sensor(ts) < 0) {
  255. printk(KERN_INFO "work read i2c failed\n");
  256. goto restart;
  257. }
  258. event = &ts->event[0];
  259. if (!ts->pendown) {
  260. ts->pendown = 1;
  261. //input_report_key(ts->input, BTN_TOUCH, 1);
  262. printk(KERN_INFO "DOWN\n");
  263. }
  264. for (i=0; i<ts->touching_num; i++) {
  265. input_report_abs(ts->input, ABS_MT_TRACKING_ID, i);
  266. #ifdef STKXXX_DEBUG
  267. printk(KERN_INFO "ABS_MT_TRACKING_ID = %d\n", i);
  268. #endif
  269. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 1);
  270. #ifdef STKXXX_DEBUG
  271. printk(KERN_INFO "ABS_MT_TOUCH_MAJOR = %d\n", 1);
  272. #endif
  273. input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 0);
  274. #ifdef STKXXX_DEBUG
  275. printk(KERN_INFO "ABS_MT_WIDTH_MAJOR = %d\n", 0);
  276. #endif
  277. input_report_abs(ts->input, ABS_MT_POSITION_X, event->x);
  278. #ifdef STKXXX_DEBUG
  279. printk(KERN_INFO "ABS_MT_POSITION_X = %d\n", event->x);
  280. #endif
  281. input_report_abs(ts->input, ABS_MT_POSITION_Y, event->y);
  282. #ifdef STKXXX_DEBUG
  283. printk(KERN_INFO "ABS_MT_POSITION_Y = %d\n", event->y);
  284. #endif
  285. input_mt_sync(ts->input);
  286. #ifdef STKXXX_DEBUG
  287. printk(KERN_INFO "input_mt_sync\n");
  288. #endif
  289. event++;
  290. }
  291. input_sync(ts->input);
  292. #ifdef STKXXX_DEBUG
  293. printk(KERN_INFO "input_sync\n");
  294. #endif
  295. restart:
  296. #ifdef TS_DELAY_WORK
  297. schedule_delayed_work(&ts->work, msecs_to_jiffies(TS_POLL_PERIOD));
  298. #else
  299. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_MODE_REL);
  300. #endif
  301. }
  302. else {
  303. /* enable IRQ after the pen was lifted */
  304. if (ts->pendown) {
  305. ts->pendown = 0;
  306. input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, 0);
  307. input_report_abs(ts->input, ABS_MT_WIDTH_MAJOR, 0);
  308. input_mt_sync(ts->input);
  309. input_sync(ts->input);
  310. printk(KERN_INFO "UP\n");
  311. }
  312. ts->touching_num = 0;
  313. ts->pre_touching_num = 0;
  314. enable_irq(ts->client->irq);
  315. }
  316. }
  317. static void stkxxx_cal_work(struct work_struct *work)
  318. {
  319. struct stkxxx *ts = container_of(to_delayed_work(work), struct stkxxx, cal_work);
  320. if (stkxxx_write_reg(ts->client, 55, 0x03) < 0) {
  321. printk("re-calibration reg failed\n");
  322. }
  323. else
  324. printk("re-calibration reg ok\n");
  325. }
  326. #ifndef TS_DELAY_WORK
  327. /**
  328. * stkxxx_timer() - timer callback function
  329. * @timer: timer that caused this function call
  330. */
  331. static enum hrtimer_restart stkxxx_timer(struct hrtimer *timer)
  332. {
  333. struct stkxxx *ts = container_of(timer, struct stkxxx, timer);
  334. unsigned long flags = 0;
  335. spin_lock_irqsave(&ts->lock, flags);
  336. // printk(KERN_INFO "enter timer\n");
  337. queue_work(ts->workqueue, &ts->work);
  338. spin_unlock_irqrestore(&ts->lock, flags);
  339. return HRTIMER_NORESTART;
  340. }
  341. #endif
  342. /**
  343. * stkxxx_interrupt() - interrupt handler for touch events
  344. * @irq: interrupt to handle
  345. * @dev_id: device-specific information
  346. */
  347. static irqreturn_t stkxxx_interrupt(int irq, void *dev_id)
  348. {
  349. struct i2c_client *client = (struct i2c_client *)dev_id;
  350. struct stkxxx *ts = i2c_get_clientdata(client);
  351. unsigned long flags;
  352. spin_lock_irqsave(&ts->lock, flags);
  353. // printk(KERN_INFO "enter penirq\n");
  354. /* if the pen is down, disable IRQ and start timer chain */
  355. if (stkxxx_get_pendown_state(ts)) {
  356. disable_irq_nosync(client->irq);
  357. #ifdef TS_DELAY_WORK
  358. schedule_delayed_work(&ts->work, msecs_to_jiffies(TS_POLL_DELAY));
  359. #else
  360. hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY), HRTIMER_MODE_REL);
  361. #endif
  362. }
  363. spin_unlock_irqrestore(&ts->lock, flags);
  364. return IRQ_HANDLED;
  365. }
  366. /**
  367. * stkxxx_probe() - initialize the I2C client
  368. * @client: client to initialize
  369. * @id: I2C device ID
  370. */
  371. static int stkxxx_probe(struct i2c_client *client,
  372. const struct i2c_device_id *id)
  373. {
  374. struct stkxxx *ts;
  375. int err = 0;
  376. ts = kzalloc(sizeof(struct stkxxx), GFP_KERNEL);
  377. if (!ts) {
  378. err = -ENOMEM;
  379. goto fail;
  380. }
  381. ts->client = client;
  382. stkxxx_reset(ts);
  383. if (stkxxx_register_input(ts) < 0) {
  384. dev_err(&client->dev, "register input fail!\n");
  385. goto fail;
  386. }
  387. /* setup platform-specific hooks */
  388. ts->pdata = client->dev.platform_data;
  389. if (!ts->pdata || !ts->pdata->init_irq || !ts->pdata->get_irq_level) {
  390. dev_err(&client->dev, "no platform-specific callbacks "
  391. "provided\n");
  392. err = -ENXIO;
  393. goto fail;
  394. }
  395. if (ts->pdata->init_irq) {
  396. err = ts->pdata->init_irq();
  397. if (err < 0) {
  398. dev_err(&client->dev, "failed to initialize IRQ#%d: "
  399. "%d\n", client->irq, err);
  400. goto fail;
  401. }
  402. }
  403. spin_lock_init(&ts->lock);
  404. #ifdef TS_DELAY_WORK
  405. INIT_DELAYED_WORK(&ts->work, stkxxx_work);
  406. #else
  407. hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  408. ts->timer.function = stkxxx_timer;
  409. INIT_WORK(&ts->work, stkxxx_work);
  410. ts->workqueue = create_singlethread_workqueue("stkxxx");
  411. if (ts->workqueue == NULL) {
  412. dev_err(&client->dev, "can't create work queue\n");
  413. err = -ENOMEM;
  414. goto fail;
  415. }
  416. printk("work create: %x\n", ts->workqueue);
  417. #endif
  418. ts->pendown = 0;
  419. ts->touching_num = 0;
  420. ts->pre_touching_num = 0;
  421. err = request_irq(client->irq, stkxxx_interrupt, IRQF_TRIGGER_FALLING,
  422. client->dev.driver->name, client);
  423. if (err) {
  424. dev_err(&client->dev, "failed to request IRQ#%d: %d\n",
  425. client->irq, err);
  426. goto fail_irq;
  427. }
  428. i2c_set_clientdata(client, ts);
  429. // INIT_DELAYED_WORK(&ts->cal_work, stkxxx_cal_work);
  430. // schedule_delayed_work(&ts->cal_work, 20*HZ);
  431. err = 0;
  432. goto out;
  433. fail_irq:
  434. free_irq(client->irq, client);
  435. fail:
  436. if (ts) {
  437. input_free_device(ts->input);
  438. kfree(ts);
  439. }
  440. i2c_set_clientdata(client, NULL);
  441. out:
  442. printk("stkxxx touch screen driver ok\n");
  443. return err;
  444. }
  445. /**
  446. * stkxxx_remove() - cleanup the I2C client
  447. * @client: client to clean up
  448. */
  449. static int stkxxx_remove(struct i2c_client *client)
  450. {
  451. struct stkxxx *priv = i2c_get_clientdata(client);
  452. free_irq(client->irq, client);
  453. i2c_set_clientdata(client, NULL);
  454. input_unregister_device(priv->input);
  455. kfree(priv);
  456. return 0;
  457. }
  458. static const struct i2c_device_id stkxxx_ids[] = {
  459. { DRIVER_NAME, 0 },
  460. { }
  461. };
  462. MODULE_DEVICE_TABLE(i2c, stkxxx_ids);
  463. /* SINTEK I2C Capacitive Touch Screen driver */
  464. static struct i2c_driver stkxxx_driver = {
  465. .driver = {
  466. .name = DRIVER_NAME,
  467. .owner = THIS_MODULE,
  468. },
  469. .probe = stkxxx_probe,
  470. .remove = __devexit_p(stkxxx_remove),
  471. .id_table = stkxxx_ids,
  472. };
  473. /**
  474. * stkxxx_init() - module initialization
  475. */
  476. static int __init stkxxx_init(void)
  477. {
  478. return i2c_add_driver(&stkxxx_driver);
  479. }
  480. /**
  481. * stkxxx_exit() - module cleanup
  482. */
  483. static void __exit stkxxx_exit(void)
  484. {
  485. i2c_del_driver(&stkxxx_driver);
  486. }
  487. module_init(stkxxx_init);
  488. module_exit(stkxxx_exit);
  489. MODULE_AUTHOR("");
  490. MODULE_DESCRIPTION("SINTEK I2C Capacitive Touch Screen driver");
  491. MODULE_LICENSE("GPL v2");
  492. MODULE_VERSION(DRIVER_VERSION);