ts4800-ts.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. /* polling interval in ms */
  20. #define POLL_INTERVAL 3
  21. #define DEBOUNCE_COUNT 1
  22. /* sensor values are 12-bit wide */
  23. #define MAX_12BIT ((1 << 12) - 1)
  24. #define PENDOWN_MASK 0x1
  25. #define X_OFFSET 0x0
  26. #define Y_OFFSET 0x2
  27. struct ts4800_ts {
  28. struct input_dev *input;
  29. struct device *dev;
  30. char phys[32];
  31. void __iomem *base;
  32. struct regmap *regmap;
  33. unsigned int reg;
  34. unsigned int bit;
  35. bool pendown;
  36. int debounce;
  37. };
  38. static int ts4800_ts_open(struct input_dev *input_dev)
  39. {
  40. struct ts4800_ts *ts = input_get_drvdata(input_dev);
  41. int error;
  42. ts->pendown = false;
  43. ts->debounce = DEBOUNCE_COUNT;
  44. error = regmap_update_bits(ts->regmap, ts->reg, ts->bit, ts->bit);
  45. if (error) {
  46. dev_warn(ts->dev, "Failed to enable touchscreen: %d\n", error);
  47. return error;
  48. }
  49. return 0;
  50. }
  51. static void ts4800_ts_close(struct input_dev *input_dev)
  52. {
  53. struct ts4800_ts *ts = input_get_drvdata(input_dev);
  54. int ret;
  55. ret = regmap_update_bits(ts->regmap, ts->reg, ts->bit, 0);
  56. if (ret)
  57. dev_warn(ts->dev, "Failed to disable touchscreen\n");
  58. }
  59. static void ts4800_ts_poll(struct input_dev *input_dev)
  60. {
  61. struct ts4800_ts *ts = input_get_drvdata(input_dev);
  62. u16 last_x = readw(ts->base + X_OFFSET);
  63. u16 last_y = readw(ts->base + Y_OFFSET);
  64. bool pendown = last_x & PENDOWN_MASK;
  65. if (pendown) {
  66. if (ts->debounce) {
  67. ts->debounce--;
  68. return;
  69. }
  70. if (!ts->pendown) {
  71. input_report_key(input_dev, BTN_TOUCH, 1);
  72. ts->pendown = true;
  73. }
  74. last_x = ((~last_x) >> 4) & MAX_12BIT;
  75. last_y = ((~last_y) >> 4) & MAX_12BIT;
  76. input_report_abs(input_dev, ABS_X, last_x);
  77. input_report_abs(input_dev, ABS_Y, last_y);
  78. input_sync(input_dev);
  79. } else if (ts->pendown) {
  80. ts->pendown = false;
  81. ts->debounce = DEBOUNCE_COUNT;
  82. input_report_key(input_dev, BTN_TOUCH, 0);
  83. input_sync(input_dev);
  84. }
  85. }
  86. static int ts4800_parse_dt(struct platform_device *pdev,
  87. struct ts4800_ts *ts)
  88. {
  89. struct device *dev = &pdev->dev;
  90. struct device_node *np = dev->of_node;
  91. struct device_node *syscon_np;
  92. u32 reg, bit;
  93. int error;
  94. syscon_np = of_parse_phandle(np, "syscon", 0);
  95. if (!syscon_np) {
  96. dev_err(dev, "no syscon property\n");
  97. return -ENODEV;
  98. }
  99. ts->regmap = syscon_node_to_regmap(syscon_np);
  100. of_node_put(syscon_np);
  101. if (IS_ERR(ts->regmap)) {
  102. dev_err(dev, "cannot get parent's regmap\n");
  103. return PTR_ERR(ts->regmap);
  104. }
  105. error = of_property_read_u32_index(np, "syscon", 1, &reg);
  106. if (error < 0) {
  107. dev_err(dev, "no offset in syscon\n");
  108. return error;
  109. }
  110. ts->reg = reg;
  111. error = of_property_read_u32_index(np, "syscon", 2, &bit);
  112. if (error < 0) {
  113. dev_err(dev, "no bit in syscon\n");
  114. return error;
  115. }
  116. ts->bit = BIT(bit);
  117. return 0;
  118. }
  119. static int ts4800_ts_probe(struct platform_device *pdev)
  120. {
  121. struct input_dev *input_dev;
  122. struct ts4800_ts *ts;
  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. ts->base = devm_platform_ioremap_resource(pdev, 0);
  131. if (IS_ERR(ts->base))
  132. return PTR_ERR(ts->base);
  133. input_dev = devm_input_allocate_device(&pdev->dev);
  134. if (!input_dev)
  135. return -ENOMEM;
  136. snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&pdev->dev));
  137. ts->input = input_dev;
  138. ts->dev = &pdev->dev;
  139. input_set_drvdata(input_dev, ts);
  140. input_dev->name = "TS-4800 Touchscreen";
  141. input_dev->phys = ts->phys;
  142. input_dev->open = ts4800_ts_open;
  143. input_dev->close = ts4800_ts_close;
  144. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  145. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  146. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  147. error = input_setup_polling(input_dev, ts4800_ts_poll);
  148. if (error) {
  149. dev_err(&pdev->dev, "Unable to set up polling: %d\n", error);
  150. return error;
  151. }
  152. input_set_poll_interval(input_dev, POLL_INTERVAL);
  153. error = input_register_device(input_dev);
  154. if (error) {
  155. dev_err(&pdev->dev,
  156. "Unable to register input device: %d\n", error);
  157. return error;
  158. }
  159. return 0;
  160. }
  161. static const struct of_device_id ts4800_ts_of_match[] = {
  162. { .compatible = "technologic,ts4800-ts", },
  163. { },
  164. };
  165. MODULE_DEVICE_TABLE(of, ts4800_ts_of_match);
  166. static struct platform_driver ts4800_ts_driver = {
  167. .driver = {
  168. .name = "ts4800-ts",
  169. .of_match_table = ts4800_ts_of_match,
  170. },
  171. .probe = ts4800_ts_probe,
  172. };
  173. module_platform_driver(ts4800_ts_driver);
  174. MODULE_AUTHOR("Damien Riegel <[email protected]>");
  175. MODULE_DESCRIPTION("TS-4800 Touchscreen Driver");
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_ALIAS("platform:ts4800_ts");