pxa930_rotary.c 4.3 KB

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