goldfish_events.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2007 Google, Inc.
  3. * Copyright (C) 2012 Intel, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/types.h>
  18. #include <linux/input.h>
  19. #include <linux/kernel.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. enum {
  25. REG_READ = 0x00,
  26. REG_SET_PAGE = 0x00,
  27. REG_LEN = 0x04,
  28. REG_DATA = 0x08,
  29. PAGE_NAME = 0x00000,
  30. PAGE_EVBITS = 0x10000,
  31. PAGE_ABSDATA = 0x20000 | EV_ABS,
  32. };
  33. struct event_dev {
  34. struct input_dev *input;
  35. int irq;
  36. void __iomem *addr;
  37. char name[0];
  38. };
  39. static irqreturn_t events_interrupt(int irq, void *dev_id)
  40. {
  41. struct event_dev *edev = dev_id;
  42. unsigned type, code, value;
  43. type = __raw_readl(edev->addr + REG_READ);
  44. code = __raw_readl(edev->addr + REG_READ);
  45. value = __raw_readl(edev->addr + REG_READ);
  46. input_event(edev->input, type, code, value);
  47. input_sync(edev->input);
  48. return IRQ_HANDLED;
  49. }
  50. static void events_import_bits(struct event_dev *edev,
  51. unsigned long bits[], unsigned type, size_t count)
  52. {
  53. void __iomem *addr = edev->addr;
  54. int i, j;
  55. size_t size;
  56. uint8_t val;
  57. __raw_writel(PAGE_EVBITS | type, addr + REG_SET_PAGE);
  58. size = __raw_readl(addr + REG_LEN) * 8;
  59. if (size < count)
  60. count = size;
  61. addr += REG_DATA;
  62. for (i = 0; i < count; i += 8) {
  63. val = __raw_readb(addr++);
  64. for (j = 0; j < 8; j++)
  65. if (val & 1 << j)
  66. set_bit(i + j, bits);
  67. }
  68. }
  69. static void events_import_abs_params(struct event_dev *edev)
  70. {
  71. struct input_dev *input_dev = edev->input;
  72. void __iomem *addr = edev->addr;
  73. u32 val[4];
  74. int count;
  75. int i, j;
  76. __raw_writel(PAGE_ABSDATA, addr + REG_SET_PAGE);
  77. count = __raw_readl(addr + REG_LEN) / sizeof(val);
  78. if (count > ABS_MAX)
  79. count = ABS_MAX;
  80. for (i = 0; i < count; i++) {
  81. if (!test_bit(i, input_dev->absbit))
  82. continue;
  83. for (j = 0; j < ARRAY_SIZE(val); j++) {
  84. int offset = (i * ARRAY_SIZE(val) + j) * sizeof(u32);
  85. val[j] = __raw_readl(edev->addr + REG_DATA + offset);
  86. }
  87. input_set_abs_params(input_dev, i,
  88. val[0], val[1], val[2], val[3]);
  89. }
  90. }
  91. static int events_probe(struct platform_device *pdev)
  92. {
  93. struct input_dev *input_dev;
  94. struct event_dev *edev;
  95. struct resource *res;
  96. unsigned keymapnamelen;
  97. void __iomem *addr;
  98. int irq;
  99. int i;
  100. int error;
  101. irq = platform_get_irq(pdev, 0);
  102. if (irq < 0)
  103. return -EINVAL;
  104. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. if (!res)
  106. return -EINVAL;
  107. addr = devm_ioremap(&pdev->dev, res->start, 4096);
  108. if (!addr)
  109. return -ENOMEM;
  110. __raw_writel(PAGE_NAME, addr + REG_SET_PAGE);
  111. keymapnamelen = __raw_readl(addr + REG_LEN);
  112. edev = devm_kzalloc(&pdev->dev,
  113. sizeof(struct event_dev) + keymapnamelen + 1,
  114. GFP_KERNEL);
  115. if (!edev)
  116. return -ENOMEM;
  117. input_dev = devm_input_allocate_device(&pdev->dev);
  118. if (!input_dev)
  119. return -ENOMEM;
  120. edev->input = input_dev;
  121. edev->addr = addr;
  122. edev->irq = irq;
  123. for (i = 0; i < keymapnamelen; i++)
  124. edev->name[i] = __raw_readb(edev->addr + REG_DATA + i);
  125. pr_debug("events_probe() keymap=%s\n", edev->name);
  126. input_dev->name = edev->name;
  127. input_dev->id.bustype = BUS_HOST;
  128. events_import_bits(edev, input_dev->evbit, EV_SYN, EV_MAX);
  129. events_import_bits(edev, input_dev->keybit, EV_KEY, KEY_MAX);
  130. events_import_bits(edev, input_dev->relbit, EV_REL, REL_MAX);
  131. events_import_bits(edev, input_dev->absbit, EV_ABS, ABS_MAX);
  132. events_import_bits(edev, input_dev->mscbit, EV_MSC, MSC_MAX);
  133. events_import_bits(edev, input_dev->ledbit, EV_LED, LED_MAX);
  134. events_import_bits(edev, input_dev->sndbit, EV_SND, SND_MAX);
  135. events_import_bits(edev, input_dev->ffbit, EV_FF, FF_MAX);
  136. events_import_bits(edev, input_dev->swbit, EV_SW, SW_MAX);
  137. events_import_abs_params(edev);
  138. error = devm_request_irq(&pdev->dev, edev->irq, events_interrupt, 0,
  139. "goldfish-events-keypad", edev);
  140. if (error)
  141. return error;
  142. error = input_register_device(input_dev);
  143. if (error)
  144. return error;
  145. return 0;
  146. }
  147. static struct platform_driver events_driver = {
  148. .probe = events_probe,
  149. .driver = {
  150. .name = "goldfish_events",
  151. },
  152. };
  153. module_platform_driver(events_driver);
  154. MODULE_AUTHOR("Brian Swetland");
  155. MODULE_DESCRIPTION("Goldfish Event Device");
  156. MODULE_LICENSE("GPL");