ts4800-ts.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Touchscreen driver for the TS-4800 board
  3. *
  4. * Copyright (c) 2015 - Savoir-faire Linux
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/input.h>
  12. #include <linux/input-polldev.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. /* polling interval in ms */
  21. #define POLL_INTERVAL 3
  22. #define DEBOUNCE_COUNT 1
  23. /* sensor values are 12-bit wide */
  24. #define MAX_12BIT ((1 << 12) - 1)
  25. #define PENDOWN_MASK 0x1
  26. #define X_OFFSET 0x0
  27. #define Y_OFFSET 0x2
  28. struct ts4800_ts {
  29. struct input_polled_dev *poll_dev;
  30. struct device *dev;
  31. char phys[32];
  32. void __iomem *base;
  33. struct regmap *regmap;
  34. unsigned int reg;
  35. unsigned int bit;
  36. bool pendown;
  37. int debounce;
  38. };
  39. static void ts4800_ts_open(struct input_polled_dev *dev)
  40. {
  41. struct ts4800_ts *ts = dev->private;
  42. int ret;
  43. ts->pendown = false;
  44. ts->debounce = DEBOUNCE_COUNT;
  45. ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit);
  46. if (ret)
  47. dev_warn(ts->dev, "Failed to enable touchscreen\n");
  48. }
  49. static void ts4800_ts_close(struct input_polled_dev *dev)
  50. {
  51. struct ts4800_ts *ts = dev->private;
  52. int ret;
  53. ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0);
  54. if (ret)
  55. dev_warn(ts->dev, "Failed to disable touchscreen\n");
  56. }
  57. static void ts4800_ts_poll(struct input_polled_dev *dev)
  58. {
  59. struct input_dev *input_dev = dev->input;
  60. struct ts4800_ts *ts = dev->private;
  61. u16 last_x = readw(ts->base + X_OFFSET);
  62. u16 last_y = readw(ts->base + Y_OFFSET);
  63. bool pendown = last_x & PENDOWN_MASK;
  64. if (pendown) {
  65. if (ts->debounce) {
  66. ts->debounce--;
  67. return;
  68. }
  69. if (!ts->pendown) {
  70. input_report_key(input_dev, BTN_TOUCH, 1);
  71. ts->pendown = true;
  72. }
  73. last_x = ((~last_x) >> 4) & MAX_12BIT;
  74. last_y = ((~last_y) >> 4) & MAX_12BIT;
  75. input_report_abs(input_dev, ABS_X, last_x);
  76. input_report_abs(input_dev, ABS_Y, last_y);
  77. input_sync(input_dev);
  78. } else if (ts->pendown) {
  79. ts->pendown = false;
  80. ts->debounce = DEBOUNCE_COUNT;
  81. input_report_key(input_dev, BTN_TOUCH, 0);
  82. input_sync(input_dev);
  83. }
  84. }
  85. static int ts4800_parse_dt(struct platform_device *pdev,
  86. struct ts4800_ts *ts)
  87. {
  88. struct device *dev = &pdev->dev;
  89. struct device_node *np = dev->of_node;
  90. struct device_node *syscon_np;
  91. u32 reg, bit;
  92. int error;
  93. syscon_np = of_parse_phandle(np, "syscon", 0);
  94. if (!syscon_np) {
  95. dev_err(dev, "no syscon property\n");
  96. return -ENODEV;
  97. }
  98. ts->regmap = syscon_node_to_regmap(syscon_np);
  99. of_node_put(syscon_np);
  100. if (IS_ERR(ts->regmap)) {
  101. dev_err(dev, "cannot get parent's regmap\n");
  102. return PTR_ERR(ts->regmap);
  103. }
  104. error = of_property_read_u32_index(np, "syscon", 1, &reg);
  105. if (error < 0) {
  106. dev_err(dev, "no offset in syscon\n");
  107. return error;
  108. }
  109. ts->reg = reg;
  110. error = of_property_read_u32_index(np, "syscon", 2, &bit);
  111. if (error < 0) {
  112. dev_err(dev, "no bit in syscon\n");
  113. return error;
  114. }
  115. ts->bit = BIT(bit);
  116. return 0;
  117. }
  118. static int ts4800_ts_probe(struct platform_device *pdev)
  119. {
  120. struct input_polled_dev *poll_dev;
  121. struct ts4800_ts *ts;
  122. struct resource *res;
  123. int error;
  124. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  125. if (!ts)
  126. return -ENOMEM;
  127. error = ts4800_parse_dt(pdev, ts);
  128. if (error)
  129. return error;
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. ts->base = devm_ioremap_resource(&pdev->dev, res);
  132. if (IS_ERR(ts->base))
  133. return PTR_ERR(ts->base);
  134. poll_dev = devm_input_allocate_polled_device(&pdev->dev);
  135. if (!poll_dev)
  136. return -ENOMEM;
  137. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev));
  138. ts->poll_dev = poll_dev;
  139. ts->dev = &pdev->dev;
  140. poll_dev->private = ts;
  141. poll_dev->poll_interval = POLL_INTERVAL;
  142. poll_dev->open = ts4800_ts_open;
  143. poll_dev->close = ts4800_ts_close;
  144. poll_dev->poll = ts4800_ts_poll;
  145. poll_dev->input->name = "TS-4800 Touchscreen";
  146. poll_dev->input->phys = ts->phys;
  147. input_set_capability(poll_dev->input, EV_KEY, BTN_TOUCH);
  148. input_set_abs_params(poll_dev->input, ABS_X, 0, MAX_12BIT, 0, 0);
  149. input_set_abs_params(poll_dev->input, ABS_Y, 0, MAX_12BIT, 0, 0);
  150. error = input_register_polled_device(poll_dev);
  151. if (error) {
  152. dev_err(&pdev->dev,
  153. "Unabled to register polled input device (%d)\n",
  154. error);
  155. return error;
  156. }
  157. return 0;
  158. }
  159. static const struct of_device_id ts4800_ts_of_match[] = {
  160. { .compatible = "technologic,ts4800-ts", },
  161. { },
  162. };
  163. MODULE_DEVICE_TABLE(of, ts4800_ts_of_match);
  164. static struct platform_driver ts4800_ts_driver = {
  165. .driver = {
  166. .name = "ts4800-ts",
  167. .of_match_table = ts4800_ts_of_match,
  168. },
  169. .probe = ts4800_ts_probe,
  170. };
  171. module_platform_driver(ts4800_ts_driver);
  172. MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
  173. MODULE_DESCRIPTION("TS-4800 Touchscreen Driver");
  174. MODULE_LICENSE("GPL v2");
  175. MODULE_ALIAS("platform:ts4800_ts");