davinci_keyscan.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * DaVinci Key Scan Driver for TI platforms
  3. *
  4. * Copyright (C) 2009 Texas Instruments, Inc
  5. *
  6. * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
  7. *
  8. * Initial Code: Sandeep Paulraj <s-paulraj@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/types.h>
  28. #include <linux/input.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/errno.h>
  33. #include <linux/slab.h>
  34. #include <asm/irq.h>
  35. #include <mach/hardware.h>
  36. #include <mach/irqs.h>
  37. #include <linux/platform_data/keyscan-davinci.h>
  38. /* Key scan registers */
  39. #define DAVINCI_KEYSCAN_KEYCTRL 0x0000
  40. #define DAVINCI_KEYSCAN_INTENA 0x0004
  41. #define DAVINCI_KEYSCAN_INTFLAG 0x0008
  42. #define DAVINCI_KEYSCAN_INTCLR 0x000c
  43. #define DAVINCI_KEYSCAN_STRBWIDTH 0x0010
  44. #define DAVINCI_KEYSCAN_INTERVAL 0x0014
  45. #define DAVINCI_KEYSCAN_CONTTIME 0x0018
  46. #define DAVINCI_KEYSCAN_CURRENTST 0x001c
  47. #define DAVINCI_KEYSCAN_PREVSTATE 0x0020
  48. #define DAVINCI_KEYSCAN_EMUCTRL 0x0024
  49. #define DAVINCI_KEYSCAN_IODFTCTRL 0x002c
  50. /* Key Control Register (KEYCTRL) */
  51. #define DAVINCI_KEYSCAN_KEYEN 0x00000001
  52. #define DAVINCI_KEYSCAN_PREVMODE 0x00000002
  53. #define DAVINCI_KEYSCAN_CHATOFF 0x00000004
  54. #define DAVINCI_KEYSCAN_AUTODET 0x00000008
  55. #define DAVINCI_KEYSCAN_SCANMODE 0x00000010
  56. #define DAVINCI_KEYSCAN_OUTTYPE 0x00000020
  57. /* Masks for the interrupts */
  58. #define DAVINCI_KEYSCAN_INT_CONT 0x00000008
  59. #define DAVINCI_KEYSCAN_INT_OFF 0x00000004
  60. #define DAVINCI_KEYSCAN_INT_ON 0x00000002
  61. #define DAVINCI_KEYSCAN_INT_CHANGE 0x00000001
  62. #define DAVINCI_KEYSCAN_INT_ALL 0x0000000f
  63. struct davinci_ks {
  64. struct input_dev *input;
  65. struct davinci_ks_platform_data *pdata;
  66. int irq;
  67. void __iomem *base;
  68. resource_size_t pbase;
  69. size_t base_size;
  70. unsigned short keymap[];
  71. };
  72. /* Initializing the kp Module */
  73. static int __init davinci_ks_initialize(struct davinci_ks *davinci_ks)
  74. {
  75. struct device *dev = &davinci_ks->input->dev;
  76. struct davinci_ks_platform_data *pdata = davinci_ks->pdata;
  77. u32 matrix_ctrl;
  78. /* Enable all interrupts */
  79. __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
  80. davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
  81. /* Clear interrupts if any */
  82. __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
  83. davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
  84. /* Setup the scan period = strobe + interval */
  85. __raw_writel(pdata->strobe,
  86. davinci_ks->base + DAVINCI_KEYSCAN_STRBWIDTH);
  87. __raw_writel(pdata->interval,
  88. davinci_ks->base + DAVINCI_KEYSCAN_INTERVAL);
  89. __raw_writel(0x01,
  90. davinci_ks->base + DAVINCI_KEYSCAN_CONTTIME);
  91. /* Define matrix type */
  92. switch (pdata->matrix_type) {
  93. case DAVINCI_KEYSCAN_MATRIX_4X4:
  94. matrix_ctrl = 0;
  95. break;
  96. case DAVINCI_KEYSCAN_MATRIX_5X3:
  97. matrix_ctrl = (1 << 6);
  98. break;
  99. default:
  100. dev_err(dev->parent, "wrong matrix type\n");
  101. return -EINVAL;
  102. }
  103. /* Enable key scan module and set matrix type */
  104. __raw_writel(DAVINCI_KEYSCAN_AUTODET | DAVINCI_KEYSCAN_KEYEN |
  105. matrix_ctrl, davinci_ks->base + DAVINCI_KEYSCAN_KEYCTRL);
  106. return 0;
  107. }
  108. static irqreturn_t davinci_ks_interrupt(int irq, void *dev_id)
  109. {
  110. struct davinci_ks *davinci_ks = dev_id;
  111. struct device *dev = &davinci_ks->input->dev;
  112. unsigned short *keymap = davinci_ks->keymap;
  113. int keymapsize = davinci_ks->pdata->keymapsize;
  114. u32 prev_status, new_status, changed;
  115. bool release;
  116. int keycode = KEY_UNKNOWN;
  117. int i;
  118. /* Disable interrupt */
  119. __raw_writel(0x0, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
  120. /* Reading previous and new status of the key scan */
  121. prev_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_PREVSTATE);
  122. new_status = __raw_readl(davinci_ks->base + DAVINCI_KEYSCAN_CURRENTST);
  123. changed = prev_status ^ new_status;
  124. if (changed) {
  125. /*
  126. * It goes through all bits in 'changed' to ensure
  127. * that no key changes are being missed
  128. */
  129. for (i = 0 ; i < keymapsize; i++) {
  130. if ((changed>>i) & 0x1) {
  131. keycode = keymap[i];
  132. release = (new_status >> i) & 0x1;
  133. dev_dbg(dev->parent, "key %d %s\n", keycode,
  134. release ? "released" : "pressed");
  135. input_report_key(davinci_ks->input, keycode,
  136. !release);
  137. input_sync(davinci_ks->input);
  138. }
  139. }
  140. /* Clearing interrupt */
  141. __raw_writel(DAVINCI_KEYSCAN_INT_ALL,
  142. davinci_ks->base + DAVINCI_KEYSCAN_INTCLR);
  143. }
  144. /* Enable interrupts */
  145. __raw_writel(0x1, davinci_ks->base + DAVINCI_KEYSCAN_INTENA);
  146. return IRQ_HANDLED;
  147. }
  148. static int __init davinci_ks_probe(struct platform_device *pdev)
  149. {
  150. struct davinci_ks *davinci_ks;
  151. struct input_dev *key_dev;
  152. struct resource *res, *mem;
  153. struct device *dev = &pdev->dev;
  154. struct davinci_ks_platform_data *pdata = dev_get_platdata(&pdev->dev);
  155. int error, i;
  156. if (pdata->device_enable) {
  157. error = pdata->device_enable(dev);
  158. if (error < 0) {
  159. dev_dbg(dev, "device enable function failed\n");
  160. return error;
  161. }
  162. }
  163. if (!pdata->keymap) {
  164. dev_dbg(dev, "no keymap from pdata\n");
  165. return -EINVAL;
  166. }
  167. davinci_ks = kzalloc(sizeof(struct davinci_ks) +
  168. sizeof(unsigned short) * pdata->keymapsize, GFP_KERNEL);
  169. if (!davinci_ks) {
  170. dev_dbg(dev, "could not allocate memory for private data\n");
  171. return -ENOMEM;
  172. }
  173. memcpy(davinci_ks->keymap, pdata->keymap,
  174. sizeof(unsigned short) * pdata->keymapsize);
  175. key_dev = input_allocate_device();
  176. if (!key_dev) {
  177. dev_dbg(dev, "could not allocate input device\n");
  178. error = -ENOMEM;
  179. goto fail1;
  180. }
  181. davinci_ks->input = key_dev;
  182. davinci_ks->irq = platform_get_irq(pdev, 0);
  183. if (davinci_ks->irq < 0) {
  184. dev_err(dev, "no key scan irq\n");
  185. error = davinci_ks->irq;
  186. goto fail2;
  187. }
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. if (!res) {
  190. dev_err(dev, "no mem resource\n");
  191. error = -EINVAL;
  192. goto fail2;
  193. }
  194. davinci_ks->pbase = res->start;
  195. davinci_ks->base_size = resource_size(res);
  196. mem = request_mem_region(davinci_ks->pbase, davinci_ks->base_size,
  197. pdev->name);
  198. if (!mem) {
  199. dev_err(dev, "key scan registers at %08x are not free\n",
  200. davinci_ks->pbase);
  201. error = -EBUSY;
  202. goto fail2;
  203. }
  204. davinci_ks->base = ioremap(davinci_ks->pbase, davinci_ks->base_size);
  205. if (!davinci_ks->base) {
  206. dev_err(dev, "can't ioremap MEM resource.\n");
  207. error = -ENOMEM;
  208. goto fail3;
  209. }
  210. /* Enable auto repeat feature of Linux input subsystem */
  211. if (pdata->rep)
  212. __set_bit(EV_REP, key_dev->evbit);
  213. /* Setup input device */
  214. __set_bit(EV_KEY, key_dev->evbit);
  215. /* Setup the platform data */
  216. davinci_ks->pdata = pdata;
  217. for (i = 0; i < davinci_ks->pdata->keymapsize; i++)
  218. __set_bit(davinci_ks->pdata->keymap[i], key_dev->keybit);
  219. key_dev->name = "davinci_keyscan";
  220. key_dev->phys = "davinci_keyscan/input0";
  221. key_dev->dev.parent = &pdev->dev;
  222. key_dev->id.bustype = BUS_HOST;
  223. key_dev->id.vendor = 0x0001;
  224. key_dev->id.product = 0x0001;
  225. key_dev->id.version = 0x0001;
  226. key_dev->keycode = davinci_ks->keymap;
  227. key_dev->keycodesize = sizeof(davinci_ks->keymap[0]);
  228. key_dev->keycodemax = davinci_ks->pdata->keymapsize;
  229. error = input_register_device(davinci_ks->input);
  230. if (error < 0) {
  231. dev_err(dev, "unable to register davinci key scan device\n");
  232. goto fail4;
  233. }
  234. error = request_irq(davinci_ks->irq, davinci_ks_interrupt,
  235. 0, pdev->name, davinci_ks);
  236. if (error < 0) {
  237. dev_err(dev, "unable to register davinci key scan interrupt\n");
  238. goto fail5;
  239. }
  240. error = davinci_ks_initialize(davinci_ks);
  241. if (error < 0) {
  242. dev_err(dev, "unable to initialize davinci key scan device\n");
  243. goto fail6;
  244. }
  245. platform_set_drvdata(pdev, davinci_ks);
  246. return 0;
  247. fail6:
  248. free_irq(davinci_ks->irq, davinci_ks);
  249. fail5:
  250. input_unregister_device(davinci_ks->input);
  251. key_dev = NULL;
  252. fail4:
  253. iounmap(davinci_ks->base);
  254. fail3:
  255. release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
  256. fail2:
  257. input_free_device(key_dev);
  258. fail1:
  259. kfree(davinci_ks);
  260. return error;
  261. }
  262. static int davinci_ks_remove(struct platform_device *pdev)
  263. {
  264. struct davinci_ks *davinci_ks = platform_get_drvdata(pdev);
  265. free_irq(davinci_ks->irq, davinci_ks);
  266. input_unregister_device(davinci_ks->input);
  267. iounmap(davinci_ks->base);
  268. release_mem_region(davinci_ks->pbase, davinci_ks->base_size);
  269. kfree(davinci_ks);
  270. return 0;
  271. }
  272. static struct platform_driver davinci_ks_driver = {
  273. .driver = {
  274. .name = "davinci_keyscan",
  275. },
  276. .remove = davinci_ks_remove,
  277. };
  278. module_platform_driver_probe(davinci_ks_driver, davinci_ks_probe);
  279. MODULE_AUTHOR("Miguel Aguilar");
  280. MODULE_DESCRIPTION("Texas Instruments DaVinci Key Scan Driver");
  281. MODULE_LICENSE("GPL");