goldfish_events.c 4.7 KB

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