goldfish_events.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include <linux/acpi.h>
  25. enum {
  26. REG_READ = 0x00,
  27. REG_SET_PAGE = 0x00,
  28. REG_LEN = 0x04,
  29. REG_DATA = 0x08,
  30. PAGE_NAME = 0x00000,
  31. PAGE_EVBITS = 0x10000,
  32. PAGE_ABSDATA = 0x20000 | EV_ABS,
  33. };
  34. struct event_dev {
  35. struct input_dev *input;
  36. int irq;
  37. void __iomem *addr;
  38. char name[0];
  39. };
  40. static irqreturn_t events_interrupt(int irq, void *dev_id)
  41. {
  42. struct event_dev *edev = dev_id;
  43. unsigned int type, code, value;
  44. type = __raw_readl(edev->addr + REG_READ);
  45. code = __raw_readl(edev->addr + REG_READ);
  46. value = __raw_readl(edev->addr + REG_READ);
  47. input_event(edev->input, type, code, value);
  48. input_sync(edev->input);
  49. return IRQ_HANDLED;
  50. }
  51. static void events_import_bits(struct event_dev *edev,
  52. unsigned long bits[], unsigned int type, size_t count)
  53. {
  54. void __iomem *addr = edev->addr;
  55. int i, j;
  56. size_t size;
  57. uint8_t val;
  58. __raw_writel(PAGE_EVBITS | type, addr + REG_SET_PAGE);
  59. size = __raw_readl(addr + REG_LEN) * 8;
  60. if (size < count)
  61. count = size;
  62. addr += REG_DATA;
  63. for (i = 0; i < count; i += 8) {
  64. val = __raw_readb(addr++);
  65. for (j = 0; j < 8; j++)
  66. if (val & 1 << j)
  67. set_bit(i + j, bits);
  68. }
  69. }
  70. static void events_import_abs_params(struct event_dev *edev)
  71. {
  72. struct input_dev *input_dev = edev->input;
  73. void __iomem *addr = edev->addr;
  74. u32 val[4];
  75. int count;
  76. int i, j;
  77. __raw_writel(PAGE_ABSDATA, addr + REG_SET_PAGE);
  78. count = __raw_readl(addr + REG_LEN) / sizeof(val);
  79. if (count > ABS_MAX)
  80. count = ABS_MAX;
  81. for (i = 0; i < count; i++) {
  82. if (!test_bit(i, input_dev->absbit))
  83. continue;
  84. for (j = 0; j < ARRAY_SIZE(val); j++) {
  85. int offset = (i * ARRAY_SIZE(val) + j) * sizeof(u32);
  86. val[j] = __raw_readl(edev->addr + REG_DATA + offset);
  87. }
  88. input_set_abs_params(input_dev, i,
  89. val[0], val[1], val[2], val[3]);
  90. }
  91. }
  92. static int events_probe(struct platform_device *pdev)
  93. {
  94. struct input_dev *input_dev;
  95. struct event_dev *edev;
  96. struct resource *res;
  97. unsigned int keymapnamelen;
  98. void __iomem *addr;
  99. int irq;
  100. int i;
  101. int error;
  102. irq = platform_get_irq(pdev, 0);
  103. if (irq < 0)
  104. return -EINVAL;
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. if (!res)
  107. return -EINVAL;
  108. addr = devm_ioremap(&pdev->dev, res->start, 4096);
  109. if (!addr)
  110. return -ENOMEM;
  111. __raw_writel(PAGE_NAME, addr + REG_SET_PAGE);
  112. keymapnamelen = __raw_readl(addr + REG_LEN);
  113. edev = devm_kzalloc(&pdev->dev,
  114. sizeof(struct event_dev) + keymapnamelen + 1,
  115. GFP_KERNEL);
  116. if (!edev)
  117. return -ENOMEM;
  118. input_dev = devm_input_allocate_device(&pdev->dev);
  119. if (!input_dev)
  120. return -ENOMEM;
  121. edev->input = input_dev;
  122. edev->addr = addr;
  123. edev->irq = irq;
  124. for (i = 0; i < keymapnamelen; i++)
  125. edev->name[i] = __raw_readb(edev->addr + REG_DATA + i);
  126. pr_debug("%s: keymap=%s\n", __func__, edev->name);
  127. input_dev->name = edev->name;
  128. input_dev->id.bustype = BUS_HOST;
  129. events_import_bits(edev, input_dev->evbit, EV_SYN, EV_MAX);
  130. events_import_bits(edev, input_dev->keybit, EV_KEY, KEY_MAX);
  131. events_import_bits(edev, input_dev->relbit, EV_REL, REL_MAX);
  132. events_import_bits(edev, input_dev->absbit, EV_ABS, ABS_MAX);
  133. events_import_bits(edev, input_dev->mscbit, EV_MSC, MSC_MAX);
  134. events_import_bits(edev, input_dev->ledbit, EV_LED, LED_MAX);
  135. events_import_bits(edev, input_dev->sndbit, EV_SND, SND_MAX);
  136. events_import_bits(edev, input_dev->ffbit, EV_FF, FF_MAX);
  137. events_import_bits(edev, input_dev->swbit, EV_SW, SW_MAX);
  138. events_import_abs_params(edev);
  139. error = devm_request_irq(&pdev->dev, edev->irq, events_interrupt, 0,
  140. "goldfish-events-keypad", edev);
  141. if (error)
  142. return error;
  143. error = input_register_device(input_dev);
  144. if (error)
  145. return error;
  146. return 0;
  147. }
  148. static const struct of_device_id goldfish_events_of_match[] = {
  149. { .compatible = "google,goldfish-events-keypad", },
  150. {},
  151. };
  152. MODULE_DEVICE_TABLE(of, goldfish_events_of_match);
  153. #ifdef CONFIG_ACPI
  154. static const struct acpi_device_id goldfish_events_acpi_match[] = {
  155. { "GFSH0002", 0 },
  156. { },
  157. };
  158. MODULE_DEVICE_TABLE(acpi, goldfish_events_acpi_match);
  159. #endif
  160. static struct platform_driver events_driver = {
  161. .probe = events_probe,
  162. .driver = {
  163. .name = "goldfish_events",
  164. .of_match_table = goldfish_events_of_match,
  165. .acpi_match_table = ACPI_PTR(goldfish_events_acpi_match),
  166. },
  167. };
  168. module_platform_driver(events_driver);
  169. MODULE_AUTHOR("Brian Swetland");
  170. MODULE_DESCRIPTION("Goldfish Event Device");
  171. MODULE_LICENSE("GPL");