pxa930_rotary.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Driver for the enhanced rotary controller on pxa930 and pxa935
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/input.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <mach/pxa930_rotary.h>
  16. #define SBCR (0x04)
  17. #define ERCR (0x0c)
  18. #define SBCR_ERSB (1 << 5)
  19. struct pxa930_rotary {
  20. struct input_dev *input_dev;
  21. void __iomem *mmio_base;
  22. int last_ercr;
  23. struct pxa930_rotary_platform_data *pdata;
  24. };
  25. static void clear_sbcr(struct pxa930_rotary *r)
  26. {
  27. uint32_t sbcr = __raw_readl(r->mmio_base + SBCR);
  28. __raw_writel(sbcr | SBCR_ERSB, r->mmio_base + SBCR);
  29. __raw_writel(sbcr & ~SBCR_ERSB, r->mmio_base + SBCR);
  30. }
  31. static irqreturn_t rotary_irq(int irq, void *dev_id)
  32. {
  33. struct pxa930_rotary *r = dev_id;
  34. struct pxa930_rotary_platform_data *pdata = r->pdata;
  35. int ercr, delta, key;
  36. ercr = __raw_readl(r->mmio_base + ERCR) & 0xf;
  37. clear_sbcr(r);
  38. delta = ercr - r->last_ercr;
  39. if (delta == 0)
  40. return IRQ_HANDLED;
  41. r->last_ercr = ercr;
  42. if (pdata->up_key && pdata->down_key) {
  43. key = (delta > 0) ? pdata->up_key : pdata->down_key;
  44. input_report_key(r->input_dev, key, 1);
  45. input_sync(r->input_dev);
  46. input_report_key(r->input_dev, key, 0);
  47. } else
  48. input_report_rel(r->input_dev, pdata->rel_code, delta);
  49. input_sync(r->input_dev);
  50. return IRQ_HANDLED;
  51. }
  52. static int pxa930_rotary_open(struct input_dev *dev)
  53. {
  54. struct pxa930_rotary *r = input_get_drvdata(dev);
  55. clear_sbcr(r);
  56. return 0;
  57. }
  58. static void pxa930_rotary_close(struct input_dev *dev)
  59. {
  60. struct pxa930_rotary *r = input_get_drvdata(dev);
  61. clear_sbcr(r);
  62. }
  63. static int __devinit pxa930_rotary_probe(struct platform_device *pdev)
  64. {
  65. struct pxa930_rotary_platform_data *pdata = pdev->dev.platform_data;
  66. struct pxa930_rotary *r;
  67. struct input_dev *input_dev;
  68. struct resource *res;
  69. int irq;
  70. int err;
  71. irq = platform_get_irq(pdev, 0);
  72. if (irq < 0) {
  73. dev_err(&pdev->dev, "no irq for rotary controller\n");
  74. return -ENXIO;
  75. }
  76. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  77. if (!res) {
  78. dev_err(&pdev->dev, "no I/O memory defined\n");
  79. return -ENXIO;
  80. }
  81. if (!pdata) {
  82. dev_err(&pdev->dev, "no platform data defined\n");
  83. return -EINVAL;
  84. }
  85. r = kzalloc(sizeof(struct pxa930_rotary), GFP_KERNEL);
  86. if (!r)
  87. return -ENOMEM;
  88. r->mmio_base = ioremap_nocache(res->start, resource_size(res));
  89. if (r->mmio_base == NULL) {
  90. dev_err(&pdev->dev, "failed to remap IO memory\n");
  91. err = -ENXIO;
  92. goto failed_free;
  93. }
  94. r->pdata = pdata;
  95. platform_set_drvdata(pdev, r);
  96. /* allocate and register the input device */
  97. input_dev = input_allocate_device();
  98. if (!input_dev) {
  99. dev_err(&pdev->dev, "failed to allocate input device\n");
  100. err = -ENOMEM;
  101. goto failed_free_io;
  102. }
  103. input_dev->name = pdev->name;
  104. input_dev->id.bustype = BUS_HOST;
  105. input_dev->open = pxa930_rotary_open;
  106. input_dev->close = pxa930_rotary_close;
  107. input_dev->dev.parent = &pdev->dev;
  108. if (pdata->up_key && pdata->down_key) {
  109. __set_bit(pdata->up_key, input_dev->keybit);
  110. __set_bit(pdata->down_key, input_dev->keybit);
  111. __set_bit(EV_KEY, input_dev->evbit);
  112. } else {
  113. __set_bit(pdata->rel_code, input_dev->relbit);
  114. __set_bit(EV_REL, input_dev->evbit);
  115. }
  116. r->input_dev = input_dev;
  117. input_set_drvdata(input_dev, r);
  118. err = request_irq(irq, rotary_irq, IRQF_DISABLED,
  119. "enhanced rotary", r);
  120. if (err) {
  121. dev_err(&pdev->dev, "failed to request IRQ\n");
  122. goto failed_free_input;
  123. }
  124. err = input_register_device(input_dev);
  125. if (err) {
  126. dev_err(&pdev->dev, "failed to register input device\n");
  127. goto failed_free_irq;
  128. }
  129. return 0;
  130. failed_free_irq:
  131. free_irq(irq, r);
  132. failed_free_input:
  133. input_free_device(input_dev);
  134. failed_free_io:
  135. iounmap(r->mmio_base);
  136. failed_free:
  137. kfree(r);
  138. return err;
  139. }
  140. static int __devexit pxa930_rotary_remove(struct platform_device *pdev)
  141. {
  142. struct pxa930_rotary *r = platform_get_drvdata(pdev);
  143. free_irq(platform_get_irq(pdev, 0), r);
  144. input_unregister_device(r->input_dev);
  145. iounmap(r->mmio_base);
  146. platform_set_drvdata(pdev, NULL);
  147. kfree(r);
  148. return 0;
  149. }
  150. static struct platform_driver pxa930_rotary_driver = {
  151. .driver = {
  152. .name = "pxa930-rotary",
  153. .owner = THIS_MODULE,
  154. },
  155. .probe = pxa930_rotary_probe,
  156. .remove = __devexit_p(pxa930_rotary_remove),
  157. };
  158. static int __init pxa930_rotary_init(void)
  159. {
  160. return platform_driver_register(&pxa930_rotary_driver);
  161. }
  162. module_init(pxa930_rotary_init);
  163. static void __exit pxa930_rotary_exit(void)
  164. {
  165. platform_driver_unregister(&pxa930_rotary_driver);
  166. }
  167. module_exit(pxa930_rotary_exit);
  168. MODULE_LICENSE("GPL");
  169. MODULE_DESCRIPTION("Driver for PXA93x Enhanced Rotary Controller");
  170. MODULE_AUTHOR("Yao Yong <yaoyong@marvell.com>");