h3600_ts_input.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * $Id: h3600_ts_input.c,v 1.4 2002/01/23 06:39:37 jsimmons Exp $
  3. *
  4. * Copyright (c) 2001 "Crazy" James Simmons jsimmons@transvirtual.com
  5. *
  6. * Sponsored by Transvirtual Technology.
  7. *
  8. * Derived from the code in h3600_ts.[ch] by Charles Flynn
  9. */
  10. /*
  11. * Driver for the h3600 Touch Screen and other Atmel controlled devices.
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * Should you need to contact me, the author, you can do so by
  29. * e-mail - mail your message to <jsimmons@transvirtual.com>.
  30. */
  31. #include <linux/errno.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/input.h>
  36. #include <linux/serio.h>
  37. #include <linux/init.h>
  38. #include <linux/delay.h>
  39. #include <linux/pm.h>
  40. /* SA1100 serial defines */
  41. #include <asm/arch/hardware.h>
  42. #include <asm/arch/irqs.h>
  43. #define DRIVER_DESC "H3600 touchscreen driver"
  44. MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
  45. MODULE_DESCRIPTION(DRIVER_DESC);
  46. MODULE_LICENSE("GPL");
  47. /*
  48. * Definitions & global arrays.
  49. */
  50. /* The start and end of frame characters SOF and EOF */
  51. #define CHAR_SOF 0x02
  52. #define CHAR_EOF 0x03
  53. #define FRAME_OVERHEAD 3 /* CHAR_SOF,CHAR_EOF,LENGTH = 3 */
  54. /*
  55. Atmel events and response IDs contained in frame.
  56. Programmer has no control over these numbers.
  57. TODO there are holes - specifically 1,7,0x0a
  58. */
  59. #define VERSION_ID 0 /* Get Version (request/respose) */
  60. #define KEYBD_ID 2 /* Keyboard (event) */
  61. #define TOUCHS_ID 3 /* Touch Screen (event)*/
  62. #define EEPROM_READ_ID 4 /* (request/response) */
  63. #define EEPROM_WRITE_ID 5 /* (request/response) */
  64. #define THERMAL_ID 6 /* (request/response) */
  65. #define NOTIFY_LED_ID 8 /* (request/response) */
  66. #define BATTERY_ID 9 /* (request/response) */
  67. #define SPI_READ_ID 0x0b /* ( request/response) */
  68. #define SPI_WRITE_ID 0x0c /* ( request/response) */
  69. #define FLITE_ID 0x0d /* backlight ( request/response) */
  70. #define STX_ID 0xa1 /* extension pack status (req/resp) */
  71. #define MAX_ID 14
  72. #define H3600_MAX_LENGTH 16
  73. #define H3600_KEY 0xf
  74. #define H3600_SCANCODE_RECORD 1 /* 1 -> record button */
  75. #define H3600_SCANCODE_CALENDAR 2 /* 2 -> calendar */
  76. #define H3600_SCANCODE_CONTACTS 3 /* 3 -> contact */
  77. #define H3600_SCANCODE_Q 4 /* 4 -> Q button */
  78. #define H3600_SCANCODE_START 5 /* 5 -> start menu */
  79. #define H3600_SCANCODE_UP 6 /* 6 -> up */
  80. #define H3600_SCANCODE_RIGHT 7 /* 7 -> right */
  81. #define H3600_SCANCODE_LEFT 8 /* 8 -> left */
  82. #define H3600_SCANCODE_DOWN 9 /* 9 -> down */
  83. static char *h3600_name = "H3600 TouchScreen";
  84. /*
  85. * Per-touchscreen data.
  86. */
  87. struct h3600_dev {
  88. struct input_dev dev;
  89. struct pm_dev *pm_dev;
  90. struct serio *serio;
  91. struct pm_dev *pm_dev;
  92. unsigned char event; /* event ID from packet */
  93. unsigned char chksum;
  94. unsigned char len;
  95. unsigned char idx;
  96. unsigned char buf[H3600_MAX_LENGTH];
  97. char phys[32];
  98. };
  99. static irqreturn_t action_button_handler(int irq, void *dev_id, struct pt_regs *regs)
  100. {
  101. int down = (GPLR & GPIO_BITSY_ACTION_BUTTON) ? 0 : 1;
  102. struct input_dev *dev = (struct input_dev *) dev_id;
  103. input_regs(dev, regs);
  104. input_report_key(dev, KEY_ENTER, down);
  105. input_sync(dev);
  106. return IRQ_HANDLED;
  107. }
  108. static irqreturn_t npower_button_handler(int irq, void *dev_id, struct pt_regs *regs)
  109. {
  110. int down = (GPLR & GPIO_BITSY_NPOWER_BUTTON) ? 0 : 1;
  111. struct input_dev *dev = (struct input_dev *) dev_id;
  112. /*
  113. * This interrupt is only called when we release the key. So we have
  114. * to fake a key press.
  115. */
  116. input_regs(dev, regs);
  117. input_report_key(dev, KEY_SUSPEND, 1);
  118. input_report_key(dev, KEY_SUSPEND, down);
  119. input_sync(dev);
  120. return IRQ_HANDLED;
  121. }
  122. #ifdef CONFIG_PM
  123. static int flite_brightness = 25;
  124. enum flite_pwr {
  125. FLITE_PWR_OFF = 0,
  126. FLITE_PWR_ON = 1
  127. };
  128. /*
  129. * h3600_flite_power: enables or disables power to frontlight, using last bright */
  130. unsigned int h3600_flite_power(struct input_dev *dev, enum flite_pwr pwr)
  131. {
  132. unsigned char brightness = (pwr == FLITE_PWR_OFF) ? 0 : flite_brightness;
  133. struct h3600_dev *ts = dev->private;
  134. /* Must be in this order */
  135. ts->serio->write(ts->serio, 1);
  136. ts->serio->write(ts->serio, pwr);
  137. ts->serio->write(ts->serio, brightness);
  138. return 0;
  139. }
  140. static int suspended = 0;
  141. static int h3600ts_pm_callback(struct pm_dev *pm_dev, pm_request_t req,
  142. void *data)
  143. {
  144. struct input_dev *dev = (struct input_dev *) data;
  145. switch (req) {
  146. case PM_SUSPEND: /* enter D1-D3 */
  147. suspended = 1;
  148. h3600_flite_power(dev, FLITE_PWR_OFF);
  149. break;
  150. case PM_BLANK:
  151. if (!suspended)
  152. h3600_flite_power(dev, FLITE_PWR_OFF);
  153. break;
  154. case PM_RESUME: /* enter D0 */
  155. /* same as unblank */
  156. case PM_UNBLANK:
  157. if (suspended) {
  158. //initSerial();
  159. suspended = 0;
  160. }
  161. h3600_flite_power(dev, FLITE_PWR_ON);
  162. break;
  163. }
  164. return 0;
  165. }
  166. #endif
  167. /*
  168. * This function translates the native event packets to linux input event
  169. * packets. Some packets coming from serial are not touchscreen related. In
  170. * this case we send them off to be processed elsewhere.
  171. */
  172. static void h3600ts_process_packet(struct h3600_dev *ts, struct pt_regs *regs)
  173. {
  174. struct input_dev *dev = &ts->dev;
  175. static int touched = 0;
  176. int key, down = 0;
  177. input_regs(dev, regs);
  178. switch (ts->event) {
  179. /*
  180. Buttons - returned as a single byte
  181. 7 6 5 4 3 2 1 0
  182. S x x x N N N N
  183. S switch state ( 0=pressed 1=released)
  184. x Unused.
  185. NNNN switch number 0-15
  186. Note: This is true for non interrupt generated key events.
  187. */
  188. case KEYBD_ID:
  189. down = (ts->buf[0] & 0x80) ? 0 : 1;
  190. switch (ts->buf[0] & 0x7f) {
  191. case H3600_SCANCODE_RECORD:
  192. key = KEY_RECORD;
  193. break;
  194. case H3600_SCANCODE_CALENDAR:
  195. key = KEY_PROG1;
  196. break;
  197. case H3600_SCANCODE_CONTACTS:
  198. key = KEY_PROG2;
  199. break;
  200. case H3600_SCANCODE_Q:
  201. key = KEY_Q;
  202. break;
  203. case H3600_SCANCODE_START:
  204. key = KEY_PROG3;
  205. break;
  206. case H3600_SCANCODE_UP:
  207. key = KEY_UP;
  208. break;
  209. case H3600_SCANCODE_RIGHT:
  210. key = KEY_RIGHT;
  211. break;
  212. case H3600_SCANCODE_LEFT:
  213. key = KEY_LEFT;
  214. break;
  215. case H3600_SCANCODE_DOWN:
  216. key = KEY_DOWN;
  217. break;
  218. default:
  219. key = 0;
  220. }
  221. if (key)
  222. input_report_key(dev, key, down);
  223. break;
  224. /*
  225. * Native touchscreen event data is formatted as shown below:-
  226. *
  227. * +-------+-------+-------+-------+
  228. * | Xmsb | Xlsb | Ymsb | Ylsb |
  229. * +-------+-------+-------+-------+
  230. * byte 0 1 2 3
  231. */
  232. case TOUCHS_ID:
  233. if (!touched) {
  234. input_report_key(dev, BTN_TOUCH, 1);
  235. touched = 1;
  236. }
  237. if (ts->len) {
  238. unsigned short x, y;
  239. x = ts->buf[0]; x <<= 8; x += ts->buf[1];
  240. y = ts->buf[2]; y <<= 8; y += ts->buf[3];
  241. input_report_abs(dev, ABS_X, x);
  242. input_report_abs(dev, ABS_Y, y);
  243. } else {
  244. input_report_key(dev, BTN_TOUCH, 0);
  245. touched = 0;
  246. }
  247. break;
  248. default:
  249. /* Send a non input event elsewhere */
  250. break;
  251. }
  252. input_sync(dev);
  253. }
  254. /*
  255. * h3600ts_event() handles events from the input module.
  256. */
  257. static int h3600ts_event(struct input_dev *dev, unsigned int type,
  258. unsigned int code, int value)
  259. {
  260. struct h3600_dev *ts = dev->private;
  261. switch (type) {
  262. case EV_LED: {
  263. // ts->serio->write(ts->serio, SOME_CMD);
  264. return 0;
  265. }
  266. }
  267. return -1;
  268. }
  269. /*
  270. Frame format
  271. byte 1 2 3 len + 4
  272. +-------+---------------+---------------+--=------------+
  273. |SOF |id |len | len bytes | Chksum |
  274. +-------+---------------+---------------+--=------------+
  275. bit 0 7 8 11 12 15 16
  276. +-------+---------------+-------+
  277. |SOF |id |0 |Chksum | - Note Chksum does not include SOF
  278. +-------+---------------+-------+
  279. bit 0 7 8 11 12 15 16
  280. */
  281. static int state;
  282. /* decode States */
  283. #define STATE_SOF 0 /* start of FRAME */
  284. #define STATE_ID 1 /* state where we decode the ID & len */
  285. #define STATE_DATA 2 /* state where we decode data */
  286. #define STATE_EOF 3 /* state where we decode checksum or EOF */
  287. static irqreturn_t h3600ts_interrupt(struct serio *serio, unsigned char data,
  288. unsigned int flags, struct pt_regs *regs)
  289. {
  290. struct h3600_dev *ts = serio_get_drvdata(serio);
  291. /*
  292. * We have a new frame coming in.
  293. */
  294. switch (state) {
  295. case STATE_SOF:
  296. if (data == CHAR_SOF)
  297. state = STATE_ID;
  298. break;
  299. case STATE_ID:
  300. ts->event = (data & 0xf0) >> 4;
  301. ts->len = (data & 0xf);
  302. ts->idx = 0;
  303. if (ts->event >= MAX_ID) {
  304. state = STATE_SOF;
  305. break;
  306. }
  307. ts->chksum = data;
  308. state = (ts->len > 0) ? STATE_DATA : STATE_EOF;
  309. break;
  310. case STATE_DATA:
  311. ts->chksum += data;
  312. ts->buf[ts->idx]= data;
  313. if (++ts->idx == ts->len)
  314. state = STATE_EOF;
  315. break;
  316. case STATE_EOF:
  317. state = STATE_SOF;
  318. if (data == CHAR_EOF || data == ts->chksum)
  319. h3600ts_process_packet(ts, regs);
  320. break;
  321. default:
  322. printk("Error3\n");
  323. break;
  324. }
  325. return IRQ_HANDLED;
  326. }
  327. /*
  328. * h3600ts_connect() is the routine that is called when someone adds a
  329. * new serio device that supports H3600 protocol and registers it as
  330. * an input device.
  331. */
  332. static int h3600ts_connect(struct serio *serio, struct serio_driver *drv)
  333. {
  334. struct h3600_dev *ts;
  335. int err;
  336. if (!(ts = kmalloc(sizeof(struct h3600_dev), GFP_KERNEL)))
  337. return -ENOMEM;
  338. memset(ts, 0, sizeof(struct h3600_dev));
  339. init_input_dev(&ts->dev);
  340. /* Device specific stuff */
  341. set_GPIO_IRQ_edge(GPIO_BITSY_ACTION_BUTTON, GPIO_BOTH_EDGES);
  342. set_GPIO_IRQ_edge(GPIO_BITSY_NPOWER_BUTTON, GPIO_RISING_EDGE);
  343. if (request_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, action_button_handler,
  344. SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
  345. "h3600_action", &ts->dev)) {
  346. printk(KERN_ERR "h3600ts.c: Could not allocate Action Button IRQ!\n");
  347. kfree(ts);
  348. return -EBUSY;
  349. }
  350. if (request_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, npower_button_handler,
  351. SA_SHIRQ | SA_INTERRUPT | SA_SAMPLE_RANDOM,
  352. "h3600_suspend", &ts->dev)) {
  353. free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
  354. printk(KERN_ERR "h3600ts.c: Could not allocate Power Button IRQ!\n");
  355. kfree(ts);
  356. return -EBUSY;
  357. }
  358. /* Now we have things going we setup our input device */
  359. ts->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_LED) | BIT(EV_PWR);
  360. ts->dev.ledbit[0] = BIT(LED_SLEEP);
  361. input_set_abs_params(&ts->dev, ABS_X, 60, 985, 0, 0);
  362. input_set_abs_params(&ts->dev, ABS_Y, 35, 1024, 0, 0);
  363. set_bit(KEY_RECORD, ts->dev.keybit);
  364. set_bit(KEY_Q, ts->dev.keybit);
  365. set_bit(KEY_PROG1, ts->dev.keybit);
  366. set_bit(KEY_PROG2, ts->dev.keybit);
  367. set_bit(KEY_PROG3, ts->dev.keybit);
  368. set_bit(KEY_UP, ts->dev.keybit);
  369. set_bit(KEY_RIGHT, ts->dev.keybit);
  370. set_bit(KEY_LEFT, ts->dev.keybit);
  371. set_bit(KEY_DOWN, ts->dev.keybit);
  372. set_bit(KEY_ENTER, ts->dev.keybit);
  373. ts->dev.keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
  374. ts->dev.keybit[LONG(KEY_SUSPEND)] |= BIT(KEY_SUSPEND);
  375. ts->serio = serio;
  376. sprintf(ts->phys, "%s/input0", serio->phys);
  377. ts->dev.event = h3600ts_event;
  378. ts->dev.private = ts;
  379. ts->dev.name = h3600_name;
  380. ts->dev.phys = ts->phys;
  381. ts->dev.id.bustype = BUS_RS232;
  382. ts->dev.id.vendor = SERIO_H3600;
  383. ts->dev.id.product = 0x0666; /* FIXME !!! We can ask the hardware */
  384. ts->dev.id.version = 0x0100;
  385. serio_set_drvdata(serio, ts);
  386. err = serio_open(serio, drv);
  387. if (err) {
  388. free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, ts);
  389. free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, ts);
  390. serio_set_drvdata(serio, NULL);
  391. kfree(ts);
  392. return err;
  393. }
  394. //h3600_flite_control(1, 25); /* default brightness */
  395. #ifdef CONFIG_PM
  396. ts->pm_dev = pm_register(PM_ILLUMINATION_DEV, PM_SYS_LIGHT,
  397. h3600ts_pm_callback);
  398. printk("registered pm callback\n");
  399. #endif
  400. input_register_device(&ts->dev);
  401. printk(KERN_INFO "input: %s on %s\n", h3600_name, serio->phys);
  402. return 0;
  403. }
  404. /*
  405. * h3600ts_disconnect() is the opposite of h3600ts_connect()
  406. */
  407. static void h3600ts_disconnect(struct serio *serio)
  408. {
  409. struct h3600_dev *ts = serio_get_drvdata(serio);
  410. free_irq(IRQ_GPIO_BITSY_ACTION_BUTTON, &ts->dev);
  411. free_irq(IRQ_GPIO_BITSY_NPOWER_BUTTON, &ts->dev);
  412. input_unregister_device(&ts->dev);
  413. serio_close(serio);
  414. serio_set_drvdata(serio, NULL);
  415. kfree(ts);
  416. }
  417. /*
  418. * The serio driver structure.
  419. */
  420. static struct serio_device_id h3600ts_serio_ids[] = {
  421. {
  422. .type = SERIO_RS232,
  423. .proto = SERIO_H3600,
  424. .id = SERIO_ANY,
  425. .extra = SERIO_ANY,
  426. },
  427. { 0 }
  428. };
  429. MODULE_DEVICE_TABLE(serio, h3600ts_serio_ids);
  430. static struct serio_driver h3600ts_drv = {
  431. .driver = {
  432. .name = "h3600ts",
  433. },
  434. .description = DRIVER_DESC,
  435. .id_table = h3600ts_serio_ids,
  436. .interrupt = h3600ts_interrupt,
  437. .connect = h3600ts_connect,
  438. .disconnect = h3600ts_disconnect,
  439. };
  440. /*
  441. * The functions for inserting/removing us as a module.
  442. */
  443. static int __init h3600ts_init(void)
  444. {
  445. serio_register_driver(&h3600ts_drv);
  446. return 0;
  447. }
  448. static void __exit h3600ts_exit(void)
  449. {
  450. serio_unregister_driver(&h3600ts_drv);
  451. }
  452. module_init(h3600ts_init);
  453. module_exit(h3600ts_exit);