tosakbd.c 11 KB

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