bf54x-keys.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * File: drivers/input/keyboard/bf54x-keys.c
  3. * Based on:
  4. * Author: Michael Hennerich <hennerich@blackfin.uclinux.org>
  5. *
  6. * Created:
  7. * Description: keypad driver for Analog Devices Blackfin BF54x Processors
  8. *
  9. *
  10. * Modified:
  11. * Copyright 2007-2008 Analog Devices Inc.
  12. *
  13. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see the file COPYING, or write
  27. * to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/fs.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/irq.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/pm.h>
  38. #include <linux/sysctl.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/delay.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/input.h>
  43. #include <asm/portmux.h>
  44. #include <mach/bf54x_keys.h>
  45. #define DRV_NAME "bf54x-keys"
  46. #define TIME_SCALE 100 /* 100 ns */
  47. #define MAX_MULT (0xFF * TIME_SCALE)
  48. #define MAX_RC 8 /* Max Row/Col */
  49. static const u16 per_rows[] = {
  50. P_KEY_ROW7,
  51. P_KEY_ROW6,
  52. P_KEY_ROW5,
  53. P_KEY_ROW4,
  54. P_KEY_ROW3,
  55. P_KEY_ROW2,
  56. P_KEY_ROW1,
  57. P_KEY_ROW0,
  58. 0
  59. };
  60. static const u16 per_cols[] = {
  61. P_KEY_COL7,
  62. P_KEY_COL6,
  63. P_KEY_COL5,
  64. P_KEY_COL4,
  65. P_KEY_COL3,
  66. P_KEY_COL2,
  67. P_KEY_COL1,
  68. P_KEY_COL0,
  69. 0
  70. };
  71. struct bf54x_kpad {
  72. struct input_dev *input;
  73. int irq;
  74. unsigned short lastkey;
  75. unsigned short *keycode;
  76. struct timer_list timer;
  77. unsigned int keyup_test_jiffies;
  78. unsigned short kpad_msel;
  79. unsigned short kpad_prescale;
  80. unsigned short kpad_ctl;
  81. };
  82. static inline int bfin_kpad_find_key(struct bf54x_kpad *bf54x_kpad,
  83. struct input_dev *input, u16 keyident)
  84. {
  85. u16 i;
  86. for (i = 0; i < input->keycodemax; i++)
  87. if (bf54x_kpad->keycode[i + input->keycodemax] == keyident)
  88. return bf54x_kpad->keycode[i];
  89. return -1;
  90. }
  91. static inline void bfin_keycodecpy(unsigned short *keycode,
  92. const unsigned int *pdata_kc,
  93. unsigned short keymapsize)
  94. {
  95. unsigned int i;
  96. for (i = 0; i < keymapsize; i++) {
  97. keycode[i] = pdata_kc[i] & 0xffff;
  98. keycode[i + keymapsize] = pdata_kc[i] >> 16;
  99. }
  100. }
  101. static inline u16 bfin_kpad_get_prescale(u32 timescale)
  102. {
  103. u32 sclk = get_sclk();
  104. return ((((sclk / 1000) * timescale) / 1024) - 1);
  105. }
  106. static inline u16 bfin_kpad_get_keypressed(struct bf54x_kpad *bf54x_kpad)
  107. {
  108. return (bfin_read_KPAD_STAT() & KPAD_PRESSED);
  109. }
  110. static inline void bfin_kpad_clear_irq(void)
  111. {
  112. bfin_write_KPAD_STAT(0xFFFF);
  113. bfin_write_KPAD_ROWCOL(0xFFFF);
  114. }
  115. static void bfin_kpad_timer(unsigned long data)
  116. {
  117. struct platform_device *pdev = (struct platform_device *) data;
  118. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  119. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  120. /* Try again later */
  121. mod_timer(&bf54x_kpad->timer,
  122. jiffies + bf54x_kpad->keyup_test_jiffies);
  123. return;
  124. }
  125. input_report_key(bf54x_kpad->input, bf54x_kpad->lastkey, 0);
  126. input_sync(bf54x_kpad->input);
  127. /* Clear IRQ Status */
  128. bfin_kpad_clear_irq();
  129. enable_irq(bf54x_kpad->irq);
  130. }
  131. static irqreturn_t bfin_kpad_isr(int irq, void *dev_id)
  132. {
  133. struct platform_device *pdev = dev_id;
  134. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  135. struct input_dev *input = bf54x_kpad->input;
  136. int key;
  137. u16 rowcol = bfin_read_KPAD_ROWCOL();
  138. key = bfin_kpad_find_key(bf54x_kpad, input, rowcol);
  139. input_report_key(input, key, 1);
  140. input_sync(input);
  141. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  142. disable_irq_nosync(bf54x_kpad->irq);
  143. bf54x_kpad->lastkey = key;
  144. mod_timer(&bf54x_kpad->timer,
  145. jiffies + bf54x_kpad->keyup_test_jiffies);
  146. } else {
  147. input_report_key(input, key, 0);
  148. input_sync(input);
  149. bfin_kpad_clear_irq();
  150. }
  151. return IRQ_HANDLED;
  152. }
  153. static int __devinit bfin_kpad_probe(struct platform_device *pdev)
  154. {
  155. struct bf54x_kpad *bf54x_kpad;
  156. struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
  157. struct input_dev *input;
  158. int i, error;
  159. if (!pdata->rows || !pdata->cols || !pdata->keymap) {
  160. dev_err(&pdev->dev, "no rows, cols or keymap from pdata\n");
  161. return -EINVAL;
  162. }
  163. if (!pdata->keymapsize ||
  164. pdata->keymapsize > (pdata->rows * pdata->cols)) {
  165. dev_err(&pdev->dev, "invalid keymapsize\n");
  166. return -EINVAL;
  167. }
  168. bf54x_kpad = kzalloc(sizeof(struct bf54x_kpad), GFP_KERNEL);
  169. if (!bf54x_kpad)
  170. return -ENOMEM;
  171. platform_set_drvdata(pdev, bf54x_kpad);
  172. /* Allocate memory for keymap followed by private LUT */
  173. bf54x_kpad->keycode = kmalloc(pdata->keymapsize *
  174. sizeof(unsigned short) * 2, GFP_KERNEL);
  175. if (!bf54x_kpad->keycode) {
  176. error = -ENOMEM;
  177. goto out;
  178. }
  179. if (!pdata->debounce_time || pdata->debounce_time > MAX_MULT ||
  180. !pdata->coldrive_time || pdata->coldrive_time > MAX_MULT) {
  181. dev_warn(&pdev->dev,
  182. "invalid platform debounce/columndrive time\n");
  183. bfin_write_KPAD_MSEL(0xFF0); /* Default MSEL */
  184. } else {
  185. bfin_write_KPAD_MSEL(
  186. ((pdata->debounce_time / TIME_SCALE)
  187. & DBON_SCALE) |
  188. (((pdata->coldrive_time / TIME_SCALE) << 8)
  189. & COLDRV_SCALE));
  190. }
  191. if (!pdata->keyup_test_interval)
  192. bf54x_kpad->keyup_test_jiffies = msecs_to_jiffies(50);
  193. else
  194. bf54x_kpad->keyup_test_jiffies =
  195. msecs_to_jiffies(pdata->keyup_test_interval);
  196. if (peripheral_request_list((u16 *)&per_rows[MAX_RC - pdata->rows],
  197. DRV_NAME)) {
  198. dev_err(&pdev->dev, "requesting peripherals failed\n");
  199. error = -EFAULT;
  200. goto out0;
  201. }
  202. if (peripheral_request_list((u16 *)&per_cols[MAX_RC - pdata->cols],
  203. DRV_NAME)) {
  204. dev_err(&pdev->dev, "requesting peripherals failed\n");
  205. error = -EFAULT;
  206. goto out1;
  207. }
  208. bf54x_kpad->irq = platform_get_irq(pdev, 0);
  209. if (bf54x_kpad->irq < 0) {
  210. error = -ENODEV;
  211. goto out2;
  212. }
  213. error = request_irq(bf54x_kpad->irq, bfin_kpad_isr,
  214. 0, DRV_NAME, pdev);
  215. if (error) {
  216. dev_err(&pdev->dev, "unable to claim irq %d\n",
  217. bf54x_kpad->irq);
  218. goto out2;
  219. }
  220. input = input_allocate_device();
  221. if (!input) {
  222. error = -ENOMEM;
  223. goto out3;
  224. }
  225. bf54x_kpad->input = input;
  226. input->name = pdev->name;
  227. input->phys = "bf54x-keys/input0";
  228. input->dev.parent = &pdev->dev;
  229. input_set_drvdata(input, bf54x_kpad);
  230. input->id.bustype = BUS_HOST;
  231. input->id.vendor = 0x0001;
  232. input->id.product = 0x0001;
  233. input->id.version = 0x0100;
  234. input->keycodesize = sizeof(unsigned short);
  235. input->keycodemax = pdata->keymapsize;
  236. input->keycode = bf54x_kpad->keycode;
  237. bfin_keycodecpy(bf54x_kpad->keycode, pdata->keymap, pdata->keymapsize);
  238. /* setup input device */
  239. __set_bit(EV_KEY, input->evbit);
  240. if (pdata->repeat)
  241. __set_bit(EV_REP, input->evbit);
  242. for (i = 0; i < input->keycodemax; i++)
  243. __set_bit(bf54x_kpad->keycode[i] & KEY_MAX, input->keybit);
  244. __clear_bit(KEY_RESERVED, input->keybit);
  245. error = input_register_device(input);
  246. if (error) {
  247. dev_err(&pdev->dev, "unable to register input device\n");
  248. goto out4;
  249. }
  250. /* Init Keypad Key Up/Release test timer */
  251. setup_timer(&bf54x_kpad->timer, bfin_kpad_timer, (unsigned long) pdev);
  252. bfin_write_KPAD_PRESCALE(bfin_kpad_get_prescale(TIME_SCALE));
  253. bfin_write_KPAD_CTL((((pdata->cols - 1) << 13) & KPAD_COLEN) |
  254. (((pdata->rows - 1) << 10) & KPAD_ROWEN) |
  255. (2 & KPAD_IRQMODE));
  256. bfin_write_KPAD_CTL(bfin_read_KPAD_CTL() | KPAD_EN);
  257. device_init_wakeup(&pdev->dev, 1);
  258. return 0;
  259. out4:
  260. input_free_device(input);
  261. out3:
  262. free_irq(bf54x_kpad->irq, pdev);
  263. out2:
  264. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  265. out1:
  266. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  267. out0:
  268. kfree(bf54x_kpad->keycode);
  269. out:
  270. kfree(bf54x_kpad);
  271. platform_set_drvdata(pdev, NULL);
  272. return error;
  273. }
  274. static int __devexit bfin_kpad_remove(struct platform_device *pdev)
  275. {
  276. struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
  277. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  278. del_timer_sync(&bf54x_kpad->timer);
  279. free_irq(bf54x_kpad->irq, pdev);
  280. input_unregister_device(bf54x_kpad->input);
  281. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  282. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  283. kfree(bf54x_kpad->keycode);
  284. kfree(bf54x_kpad);
  285. platform_set_drvdata(pdev, NULL);
  286. return 0;
  287. }
  288. #ifdef CONFIG_PM
  289. static int bfin_kpad_suspend(struct platform_device *pdev, pm_message_t state)
  290. {
  291. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  292. bf54x_kpad->kpad_msel = bfin_read_KPAD_MSEL();
  293. bf54x_kpad->kpad_prescale = bfin_read_KPAD_PRESCALE();
  294. bf54x_kpad->kpad_ctl = bfin_read_KPAD_CTL();
  295. if (device_may_wakeup(&pdev->dev))
  296. enable_irq_wake(bf54x_kpad->irq);
  297. return 0;
  298. }
  299. static int bfin_kpad_resume(struct platform_device *pdev)
  300. {
  301. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  302. bfin_write_KPAD_MSEL(bf54x_kpad->kpad_msel);
  303. bfin_write_KPAD_PRESCALE(bf54x_kpad->kpad_prescale);
  304. bfin_write_KPAD_CTL(bf54x_kpad->kpad_ctl);
  305. if (device_may_wakeup(&pdev->dev))
  306. disable_irq_wake(bf54x_kpad->irq);
  307. return 0;
  308. }
  309. #else
  310. # define bfin_kpad_suspend NULL
  311. # define bfin_kpad_resume NULL
  312. #endif
  313. static struct platform_driver bfin_kpad_device_driver = {
  314. .driver = {
  315. .name = DRV_NAME,
  316. .owner = THIS_MODULE,
  317. },
  318. .probe = bfin_kpad_probe,
  319. .remove = __devexit_p(bfin_kpad_remove),
  320. .suspend = bfin_kpad_suspend,
  321. .resume = bfin_kpad_resume,
  322. };
  323. module_platform_driver(bfin_kpad_device_driver);
  324. MODULE_LICENSE("GPL");
  325. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  326. MODULE_DESCRIPTION("Keypad driver for BF54x Processors");
  327. MODULE_ALIAS("platform:bf54x-keys");