tosakbd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Keyboard driver for Sharp Tosa models (SL-6000x)
  3. *
  4. * Copyright (c) 2005 Dirk Opfer
  5. * Copyright (c) 2007 Dmitry Baryshkov
  6. *
  7. * Based on xtkbd.c/locomkbd.c/corgikbd.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/input.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/arch/gpio.h>
  21. #include <asm/arch/tosa.h>
  22. #define KB_ROWMASK(r) (1 << (r))
  23. #define SCANCODE(r, c) (((r)<<4) + (c) + 1)
  24. #define NR_SCANCODES SCANCODE(TOSA_KEY_SENSE_NUM - 1, TOSA_KEY_STROBE_NUM - 1) + 1
  25. #define SCAN_INTERVAL (HZ/10)
  26. #define KB_DISCHARGE_DELAY 10
  27. #define KB_ACTIVATE_DELAY 10
  28. static unsigned int tosakbd_keycode[NR_SCANCODES] = {
  29. 0,
  30. 0, KEY_W, 0, 0, 0, KEY_K, KEY_BACKSPACE, KEY_P,
  31. 0, 0, 0, 0, 0, 0, 0, 0,
  32. KEY_Q, KEY_E, KEY_T, KEY_Y, 0, KEY_O, KEY_I, KEY_COMMA,
  33. 0, 0, 0, 0, 0, 0, 0, 0,
  34. KEY_A, KEY_D, KEY_G, KEY_U, 0, KEY_L, KEY_ENTER, KEY_DOT,
  35. 0, 0, 0, 0, 0, 0, 0, 0,
  36. KEY_Z, KEY_C, KEY_V, KEY_J, TOSA_KEY_ADDRESSBOOK, TOSA_KEY_CANCEL, TOSA_KEY_CENTER, TOSA_KEY_OK,
  37. KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, 0,
  38. KEY_S, KEY_R, KEY_B, KEY_N, TOSA_KEY_CALENDAR, TOSA_KEY_HOMEPAGE, KEY_LEFTCTRL, TOSA_KEY_LIGHT,
  39. 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,
  40. KEY_TAB, KEY_SLASH, KEY_H, KEY_M, TOSA_KEY_MENU, 0, KEY_UP, 0,
  41. 0, 0, TOSA_KEY_FN, 0, 0, 0, 0, 0,
  42. KEY_X, KEY_F, KEY_SPACE, KEY_APOSTROPHE, TOSA_KEY_MAIL, KEY_LEFT, KEY_DOWN, KEY_RIGHT,
  43. 0, 0, 0,
  44. };
  45. struct tosakbd {
  46. unsigned int keycode[ARRAY_SIZE(tosakbd_keycode)];
  47. struct input_dev *input;
  48. int suspended;
  49. spinlock_t lock; /* protect kbd scanning */
  50. struct timer_list timer;
  51. };
  52. /* Helper functions for reading the keyboard matrix
  53. * Note: We should really be using pxa_gpio_mode to alter GPDR but it
  54. * requires a function call per GPIO bit which is excessive
  55. * when we need to access 12 bits at once, multiple times.
  56. * These functions must be called within local_irq_save()/local_irq_restore()
  57. * or similar.
  58. */
  59. #define GET_ROWS_STATUS(c) ((GPLR2 & TOSA_GPIO_ALL_SENSE_BIT) >> TOSA_GPIO_ALL_SENSE_RSHIFT)
  60. static inline void tosakbd_discharge_all(void)
  61. {
  62. /* STROBE All HiZ */
  63. GPCR1 = TOSA_GPIO_HIGH_STROBE_BIT;
  64. GPDR1 &= ~TOSA_GPIO_HIGH_STROBE_BIT;
  65. GPCR2 = TOSA_GPIO_LOW_STROBE_BIT;
  66. GPDR2 &= ~TOSA_GPIO_LOW_STROBE_BIT;
  67. }
  68. static inline void tosakbd_activate_all(void)
  69. {
  70. /* STROBE ALL -> High */
  71. GPSR1 = TOSA_GPIO_HIGH_STROBE_BIT;
  72. GPDR1 |= TOSA_GPIO_HIGH_STROBE_BIT;
  73. GPSR2 = TOSA_GPIO_LOW_STROBE_BIT;
  74. GPDR2 |= TOSA_GPIO_LOW_STROBE_BIT;
  75. udelay(KB_DISCHARGE_DELAY);
  76. /* STATE CLEAR */
  77. GEDR2 |= TOSA_GPIO_ALL_SENSE_BIT;
  78. }
  79. static inline void tosakbd_activate_col(int col)
  80. {
  81. if (col <= 5) {
  82. /* STROBE col -> High, not col -> HiZ */
  83. GPSR1 = TOSA_GPIO_STROBE_BIT(col);
  84. GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  85. } else {
  86. /* STROBE col -> High, not col -> HiZ */
  87. GPSR2 = TOSA_GPIO_STROBE_BIT(col);
  88. GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  89. }
  90. }
  91. static inline void tosakbd_reset_col(int col)
  92. {
  93. if (col <= 5) {
  94. /* STROBE col -> Low */
  95. GPCR1 = TOSA_GPIO_STROBE_BIT(col);
  96. /* STROBE col -> out, not col -> HiZ */
  97. GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  98. } else {
  99. /* STROBE col -> Low */
  100. GPCR2 = TOSA_GPIO_STROBE_BIT(col);
  101. /* STROBE col -> out, not col -> HiZ */
  102. GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  103. }
  104. }
  105. /*
  106. * The tosa keyboard only generates interrupts when a key is pressed.
  107. * So when a key is pressed, we enable a timer. This timer scans the
  108. * keyboard, and this is how we detect when the key is released.
  109. */
  110. /* Scan the hardware keyboard and push any changes up through the input layer */
  111. static void tosakbd_scankeyboard(struct platform_device *dev)
  112. {
  113. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  114. unsigned int row, col, rowd;
  115. unsigned long flags;
  116. unsigned int num_pressed = 0;
  117. spin_lock_irqsave(&tosakbd->lock, flags);
  118. if (tosakbd->suspended)
  119. goto out;
  120. for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
  121. /*
  122. * Discharge the output driver capacitatance
  123. * in the keyboard matrix. (Yes it is significant..)
  124. */
  125. tosakbd_discharge_all();
  126. udelay(KB_DISCHARGE_DELAY);
  127. tosakbd_activate_col(col);
  128. udelay(KB_ACTIVATE_DELAY);
  129. rowd = GET_ROWS_STATUS(col);
  130. for (row = 0; row < TOSA_KEY_SENSE_NUM; row++) {
  131. unsigned int scancode, pressed;
  132. scancode = SCANCODE(row, col);
  133. pressed = rowd & KB_ROWMASK(row);
  134. if (pressed && !tosakbd->keycode[scancode])
  135. dev_warn(&dev->dev,
  136. "unhandled scancode: 0x%02x\n",
  137. scancode);
  138. input_report_key(tosakbd->input,
  139. tosakbd->keycode[scancode],
  140. pressed);
  141. if (pressed)
  142. num_pressed++;
  143. }
  144. tosakbd_reset_col(col);
  145. }
  146. tosakbd_activate_all();
  147. input_sync(tosakbd->input);
  148. /* if any keys are pressed, enable the timer */
  149. if (num_pressed)
  150. mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
  151. out:
  152. spin_unlock_irqrestore(&tosakbd->lock, flags);
  153. }
  154. /*
  155. * tosa keyboard interrupt handler.
  156. */
  157. static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
  158. {
  159. struct platform_device *dev = __dev;
  160. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  161. if (!timer_pending(&tosakbd->timer)) {
  162. /** wait chattering delay **/
  163. udelay(20);
  164. tosakbd_scankeyboard(dev);
  165. }
  166. return IRQ_HANDLED;
  167. }
  168. /*
  169. * tosa timer checking for released keys
  170. */
  171. static void tosakbd_timer_callback(unsigned long __dev)
  172. {
  173. struct platform_device *dev = (struct platform_device *)__dev;
  174. tosakbd_scankeyboard(dev);
  175. }
  176. #ifdef CONFIG_PM
  177. static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
  178. {
  179. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  180. unsigned long flags;
  181. spin_lock_irqsave(&tosakbd->lock, flags);
  182. PGSR1 = (PGSR1 & ~TOSA_GPIO_LOW_STROBE_BIT);
  183. PGSR2 = (PGSR2 & ~TOSA_GPIO_HIGH_STROBE_BIT);
  184. tosakbd->suspended = 1;
  185. spin_unlock_irqrestore(&tosakbd->lock, flags);
  186. del_timer_sync(&tosakbd->timer);
  187. return 0;
  188. }
  189. static int tosakbd_resume(struct platform_device *dev)
  190. {
  191. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  192. tosakbd->suspended = 0;
  193. tosakbd_scankeyboard(dev);
  194. return 0;
  195. }
  196. #else
  197. #define tosakbd_suspend NULL
  198. #define tosakbd_resume NULL
  199. #endif
  200. static int __devinit tosakbd_probe(struct platform_device *pdev) {
  201. int i;
  202. struct tosakbd *tosakbd;
  203. struct input_dev *input_dev;
  204. int error;
  205. tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
  206. if (!tosakbd)
  207. return -ENOMEM;
  208. input_dev = input_allocate_device();
  209. if (!input_dev) {
  210. kfree(tosakbd);
  211. return -ENOMEM;
  212. }
  213. platform_set_drvdata(pdev, tosakbd);
  214. spin_lock_init(&tosakbd->lock);
  215. /* Init Keyboard rescan timer */
  216. init_timer(&tosakbd->timer);
  217. tosakbd->timer.function = tosakbd_timer_callback;
  218. tosakbd->timer.data = (unsigned long) pdev;
  219. tosakbd->input = input_dev;
  220. input_set_drvdata(input_dev, tosakbd);
  221. input_dev->name = "Tosa Keyboard";
  222. input_dev->phys = "tosakbd/input0";
  223. input_dev->dev.parent = &pdev->dev;
  224. input_dev->id.bustype = BUS_HOST;
  225. input_dev->id.vendor = 0x0001;
  226. input_dev->id.product = 0x0001;
  227. input_dev->id.version = 0x0100;
  228. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  229. input_dev->keycode = tosakbd->keycode;
  230. input_dev->keycodesize = sizeof(unsigned int);
  231. input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
  232. memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
  233. for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
  234. __set_bit(tosakbd->keycode[i], input_dev->keybit);
  235. clear_bit(0, input_dev->keybit);
  236. /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
  237. for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
  238. int gpio = TOSA_GPIO_KEY_SENSE(i);
  239. int irq;
  240. error = gpio_request(gpio, "tosakbd");
  241. if (error < 0) {
  242. printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
  243. " error %d\n", gpio, error);
  244. goto fail;
  245. }
  246. error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
  247. if (error < 0) {
  248. printk(KERN_ERR "tosakbd: failed to configure input"
  249. " direction for GPIO %d, error %d\n",
  250. gpio, error);
  251. gpio_free(gpio);
  252. goto fail;
  253. }
  254. irq = gpio_to_irq(gpio);
  255. if (irq < 0) {
  256. error = irq;
  257. printk(KERN_ERR "gpio-keys: Unable to get irq number"
  258. " for GPIO %d, error %d\n",
  259. gpio, error);
  260. gpio_free(gpio);
  261. goto fail;
  262. }
  263. error = request_irq(irq, tosakbd_interrupt,
  264. IRQF_DISABLED | IRQF_TRIGGER_RISING,
  265. "tosakbd", pdev);
  266. if (error) {
  267. printk("tosakbd: Can't get IRQ: %d: error %d!\n",
  268. irq, error);
  269. gpio_free(gpio);
  270. goto fail;
  271. }
  272. }
  273. /* Set Strobe lines as outputs - set high */
  274. for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
  275. int gpio = TOSA_GPIO_KEY_STROBE(i);
  276. error = gpio_request(gpio, "tosakbd");
  277. if (error < 0) {
  278. printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
  279. " error %d\n", gpio, error);
  280. goto fail2;
  281. }
  282. error = gpio_direction_output(gpio, 1);
  283. if (error < 0) {
  284. printk(KERN_ERR "tosakbd: failed to configure input"
  285. " direction for GPIO %d, error %d\n",
  286. gpio, error);
  287. gpio_free(gpio);
  288. goto fail;
  289. }
  290. }
  291. error = input_register_device(input_dev);
  292. if (error) {
  293. printk(KERN_ERR "tosakbd: Unable to register input device, "
  294. "error: %d\n", error);
  295. goto fail;
  296. }
  297. printk(KERN_INFO "input: Tosa Keyboard Registered\n");
  298. return 0;
  299. fail2:
  300. while (--i >= 0)
  301. gpio_free(TOSA_GPIO_KEY_STROBE(i));
  302. i = TOSA_KEY_SENSE_NUM;
  303. fail:
  304. while (--i >= 0) {
  305. free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
  306. gpio_free(TOSA_GPIO_KEY_SENSE(i));
  307. }
  308. platform_set_drvdata(pdev, NULL);
  309. input_free_device(input_dev);
  310. kfree(tosakbd);
  311. return error;
  312. }
  313. static int __devexit tosakbd_remove(struct platform_device *dev)
  314. {
  315. int i;
  316. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  317. for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
  318. gpio_free(TOSA_GPIO_KEY_STROBE(i));
  319. for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
  320. free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
  321. gpio_free(TOSA_GPIO_KEY_SENSE(i));
  322. }
  323. del_timer_sync(&tosakbd->timer);
  324. input_unregister_device(tosakbd->input);
  325. kfree(tosakbd);
  326. return 0;
  327. }
  328. static struct platform_driver tosakbd_driver = {
  329. .probe = tosakbd_probe,
  330. .remove = __devexit_p(tosakbd_remove),
  331. .suspend = tosakbd_suspend,
  332. .resume = tosakbd_resume,
  333. .driver = {
  334. .name = "tosa-keyboard",
  335. .owner = THIS_MODULE,
  336. },
  337. };
  338. static int __devinit tosakbd_init(void)
  339. {
  340. return platform_driver_register(&tosakbd_driver);
  341. }
  342. static void __exit tosakbd_exit(void)
  343. {
  344. platform_driver_unregister(&tosakbd_driver);
  345. }
  346. module_init(tosakbd_init);
  347. module_exit(tosakbd_exit);
  348. MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
  349. MODULE_DESCRIPTION("Tosa Keyboard Driver");
  350. MODULE_LICENSE("GPL v2");
  351. MODULE_ALIAS("platform:tosa-keyboard");