pxa930_rotary.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/interrupt.h>
  11. #include <linux/input.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_data/keyboard-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 pxa930_rotary_probe(struct platform_device *pdev)
  64. {
  65. struct pxa930_rotary_platform_data *pdata =
  66. dev_get_platdata(&pdev->dev);
  67. struct pxa930_rotary *r;
  68. struct input_dev *input_dev;
  69. struct resource *res;
  70. int irq;
  71. int err;
  72. irq = platform_get_irq(pdev, 0);
  73. if (irq < 0) {
  74. dev_err(&pdev->dev, "no irq for rotary controller\n");
  75. return -ENXIO;
  76. }
  77. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  78. if (!res) {
  79. dev_err(&pdev->dev, "no I/O memory defined\n");
  80. return -ENXIO;
  81. }
  82. if (!pdata) {
  83. dev_err(&pdev->dev, "no platform data defined\n");
  84. return -EINVAL;
  85. }
  86. r = kzalloc(sizeof(struct pxa930_rotary), GFP_KERNEL);
  87. if (!r)
  88. return -ENOMEM;
  89. r->mmio_base = ioremap_nocache(res->start, resource_size(res));
  90. if (r->mmio_base == NULL) {
  91. dev_err(&pdev->dev, "failed to remap IO memory\n");
  92. err = -ENXIO;
  93. goto failed_free;
  94. }
  95. r->pdata = pdata;
  96. platform_set_drvdata(pdev, r);
  97. /* allocate and register the input device */
  98. input_dev = input_allocate_device();
  99. if (!input_dev) {
  100. dev_err(&pdev->dev, "failed to allocate input device\n");
  101. err = -ENOMEM;
  102. goto failed_free_io;
  103. }
  104. input_dev->name = pdev->name;
  105. input_dev->id.bustype = BUS_HOST;
  106. input_dev->open = pxa930_rotary_open;
  107. input_dev->close = pxa930_rotary_close;
  108. input_dev->dev.parent = &pdev->dev;
  109. if (pdata->up_key && pdata->down_key) {
  110. __set_bit(pdata->up_key, input_dev->keybit);
  111. __set_bit(pdata->down_key, input_dev->keybit);
  112. __set_bit(EV_KEY, input_dev->evbit);
  113. } else {
  114. __set_bit(pdata->rel_code, input_dev->relbit);
  115. __set_bit(EV_REL, input_dev->evbit);
  116. }
  117. r->input_dev = input_dev;
  118. input_set_drvdata(input_dev, r);
  119. err = request_irq(irq, rotary_irq, 0,
  120. "enhanced rotary", r);
  121. if (err) {
  122. dev_err(&pdev->dev, "failed to request IRQ\n");
  123. goto failed_free_input;
  124. }
  125. err = input_register_device(input_dev);
  126. if (err) {
  127. dev_err(&pdev->dev, "failed to register input device\n");
  128. goto failed_free_irq;
  129. }
  130. return 0;
  131. failed_free_irq:
  132. free_irq(irq, r);
  133. failed_free_input:
  134. input_free_device(input_dev);
  135. failed_free_io:
  136. iounmap(r->mmio_base);
  137. failed_free:
  138. kfree(r);
  139. return err;
  140. }
  141. static int pxa930_rotary_remove(struct platform_device *pdev)
  142. {
  143. struct pxa930_rotary *r = platform_get_drvdata(pdev);
  144. free_irq(platform_get_irq(pdev, 0), r);
  145. input_unregister_device(r->input_dev);
  146. iounmap(r->mmio_base);
  147. kfree(r);
  148. return 0;
  149. }
  150. static struct platform_driver pxa930_rotary_driver = {
  151. .driver = {
  152. .name = "pxa930-rotary",
  153. },
  154. .probe = pxa930_rotary_probe,
  155. .remove = pxa930_rotary_remove,
  156. };
  157. module_platform_driver(pxa930_rotary_driver);
  158. MODULE_LICENSE("GPL");
  159. MODULE_DESCRIPTION("Driver for PXA93x Enhanced Rotary Controller");
  160. MODULE_AUTHOR("Yao Yong <yaoyong@marvell.com>");