ambakmi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * linux/drivers/input/serio/ambakmi.c
  3. *
  4. * Copyright (C) 2000-2003 Deep Blue Solutions Ltd.
  5. * Copyright (C) 2002 Russell King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/serio.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/device.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/amba/kmi.h>
  24. #include <linux/clk.h>
  25. #include <asm/io.h>
  26. #include <asm/irq.h>
  27. #define KMI_BASE (kmi->base)
  28. struct amba_kmi_port {
  29. struct serio *io;
  30. struct clk *clk;
  31. void __iomem *base;
  32. unsigned int irq;
  33. unsigned int divisor;
  34. unsigned int open;
  35. };
  36. static irqreturn_t amba_kmi_int(int irq, void *dev_id)
  37. {
  38. struct amba_kmi_port *kmi = dev_id;
  39. unsigned int status = readb(KMIIR);
  40. int handled = IRQ_NONE;
  41. while (status & KMIIR_RXINTR) {
  42. serio_interrupt(kmi->io, readb(KMIDATA), 0);
  43. status = readb(KMIIR);
  44. handled = IRQ_HANDLED;
  45. }
  46. return handled;
  47. }
  48. static int amba_kmi_write(struct serio *io, unsigned char val)
  49. {
  50. struct amba_kmi_port *kmi = io->port_data;
  51. unsigned int timeleft = 10000; /* timeout in 100ms */
  52. while ((readb(KMISTAT) & KMISTAT_TXEMPTY) == 0 && --timeleft)
  53. udelay(10);
  54. if (timeleft)
  55. writeb(val, KMIDATA);
  56. return timeleft ? 0 : SERIO_TIMEOUT;
  57. }
  58. static int amba_kmi_open(struct serio *io)
  59. {
  60. struct amba_kmi_port *kmi = io->port_data;
  61. unsigned int divisor;
  62. int ret;
  63. ret = clk_enable(kmi->clk);
  64. if (ret)
  65. goto out;
  66. divisor = clk_get_rate(kmi->clk) / 8000000 - 1;
  67. writeb(divisor, KMICLKDIV);
  68. writeb(KMICR_EN, KMICR);
  69. ret = request_irq(kmi->irq, amba_kmi_int, 0, "kmi-pl050", kmi);
  70. if (ret) {
  71. printk(KERN_ERR "kmi: failed to claim IRQ%d\n", kmi->irq);
  72. writeb(0, KMICR);
  73. goto clk_disable;
  74. }
  75. writeb(KMICR_EN | KMICR_RXINTREN, KMICR);
  76. return 0;
  77. clk_disable:
  78. clk_disable(kmi->clk);
  79. out:
  80. return ret;
  81. }
  82. static void amba_kmi_close(struct serio *io)
  83. {
  84. struct amba_kmi_port *kmi = io->port_data;
  85. writeb(0, KMICR);
  86. free_irq(kmi->irq, kmi);
  87. clk_disable(kmi->clk);
  88. }
  89. static int __devinit amba_kmi_probe(struct amba_device *dev,
  90. const struct amba_id *id)
  91. {
  92. struct amba_kmi_port *kmi;
  93. struct serio *io;
  94. int ret;
  95. ret = amba_request_regions(dev, NULL);
  96. if (ret)
  97. return ret;
  98. kmi = kzalloc(sizeof(struct amba_kmi_port), GFP_KERNEL);
  99. io = kzalloc(sizeof(struct serio), GFP_KERNEL);
  100. if (!kmi || !io) {
  101. ret = -ENOMEM;
  102. goto out;
  103. }
  104. io->id.type = SERIO_8042;
  105. io->write = amba_kmi_write;
  106. io->open = amba_kmi_open;
  107. io->close = amba_kmi_close;
  108. strlcpy(io->name, dev_name(&dev->dev), sizeof(io->name));
  109. strlcpy(io->phys, dev_name(&dev->dev), sizeof(io->phys));
  110. io->port_data = kmi;
  111. io->dev.parent = &dev->dev;
  112. kmi->io = io;
  113. kmi->base = ioremap(dev->res.start, resource_size(&dev->res));
  114. if (!kmi->base) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  118. kmi->clk = clk_get(&dev->dev, "KMIREFCLK");
  119. if (IS_ERR(kmi->clk)) {
  120. ret = PTR_ERR(kmi->clk);
  121. goto unmap;
  122. }
  123. kmi->irq = dev->irq[0];
  124. amba_set_drvdata(dev, kmi);
  125. serio_register_port(kmi->io);
  126. return 0;
  127. unmap:
  128. iounmap(kmi->base);
  129. out:
  130. kfree(kmi);
  131. kfree(io);
  132. amba_release_regions(dev);
  133. return ret;
  134. }
  135. static int __devexit amba_kmi_remove(struct amba_device *dev)
  136. {
  137. struct amba_kmi_port *kmi = amba_get_drvdata(dev);
  138. amba_set_drvdata(dev, NULL);
  139. serio_unregister_port(kmi->io);
  140. clk_put(kmi->clk);
  141. iounmap(kmi->base);
  142. kfree(kmi);
  143. amba_release_regions(dev);
  144. return 0;
  145. }
  146. static int amba_kmi_resume(struct amba_device *dev)
  147. {
  148. struct amba_kmi_port *kmi = amba_get_drvdata(dev);
  149. /* kick the serio layer to rescan this port */
  150. serio_reconnect(kmi->io);
  151. return 0;
  152. }
  153. static struct amba_id amba_kmi_idtable[] = {
  154. {
  155. .id = 0x00041050,
  156. .mask = 0x000fffff,
  157. },
  158. { 0, 0 }
  159. };
  160. MODULE_DEVICE_TABLE(amba, amba_kmi_idtable);
  161. static struct amba_driver ambakmi_driver = {
  162. .drv = {
  163. .name = "kmi-pl050",
  164. .owner = THIS_MODULE,
  165. },
  166. .id_table = amba_kmi_idtable,
  167. .probe = amba_kmi_probe,
  168. .remove = __devexit_p(amba_kmi_remove),
  169. .resume = amba_kmi_resume,
  170. };
  171. module_amba_driver(ambakmi_driver);
  172. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  173. MODULE_DESCRIPTION("AMBA KMI controller driver");
  174. MODULE_LICENSE("GPL");