ipaq-micro-ts.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * h3600 atmel micro companion support, touchscreen subdevice
  5. * Author : Alessandro Gardich <[email protected]>
  6. * Author : Dmitry Artamonow <[email protected]>
  7. * Author : Linus Walleij <[email protected]>
  8. */
  9. #include <asm/byteorder.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pm.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/input.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/mfd/ipaq-micro.h>
  20. struct touchscreen_data {
  21. struct input_dev *input;
  22. struct ipaq_micro *micro;
  23. };
  24. static void micro_ts_receive(void *data, int len, unsigned char *msg)
  25. {
  26. struct touchscreen_data *ts = data;
  27. if (len == 4) {
  28. input_report_abs(ts->input, ABS_X,
  29. be16_to_cpup((__be16 *) &msg[2]));
  30. input_report_abs(ts->input, ABS_Y,
  31. be16_to_cpup((__be16 *) &msg[0]));
  32. input_report_key(ts->input, BTN_TOUCH, 1);
  33. input_sync(ts->input);
  34. } else if (len == 0) {
  35. input_report_abs(ts->input, ABS_X, 0);
  36. input_report_abs(ts->input, ABS_Y, 0);
  37. input_report_key(ts->input, BTN_TOUCH, 0);
  38. input_sync(ts->input);
  39. }
  40. }
  41. static void micro_ts_toggle_receive(struct touchscreen_data *ts, bool enable)
  42. {
  43. struct ipaq_micro *micro = ts->micro;
  44. spin_lock_irq(&micro->lock);
  45. if (enable) {
  46. micro->ts = micro_ts_receive;
  47. micro->ts_data = ts;
  48. } else {
  49. micro->ts = NULL;
  50. micro->ts_data = NULL;
  51. }
  52. spin_unlock_irq(&ts->micro->lock);
  53. }
  54. static int micro_ts_open(struct input_dev *input)
  55. {
  56. struct touchscreen_data *ts = input_get_drvdata(input);
  57. micro_ts_toggle_receive(ts, true);
  58. return 0;
  59. }
  60. static void micro_ts_close(struct input_dev *input)
  61. {
  62. struct touchscreen_data *ts = input_get_drvdata(input);
  63. micro_ts_toggle_receive(ts, false);
  64. }
  65. static int micro_ts_probe(struct platform_device *pdev)
  66. {
  67. struct ipaq_micro *micro = dev_get_drvdata(pdev->dev.parent);
  68. struct touchscreen_data *ts;
  69. int error;
  70. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  71. if (!ts)
  72. return -ENOMEM;
  73. ts->micro = micro;
  74. ts->input = devm_input_allocate_device(&pdev->dev);
  75. if (!ts->input) {
  76. dev_err(&pdev->dev, "failed to allocate input device\n");
  77. return -ENOMEM;
  78. }
  79. ts->input->name = "ipaq micro ts";
  80. ts->input->open = micro_ts_open;
  81. ts->input->close = micro_ts_close;
  82. input_set_drvdata(ts->input, ts);
  83. input_set_capability(ts->input, EV_KEY, BTN_TOUCH);
  84. input_set_capability(ts->input, EV_ABS, ABS_X);
  85. input_set_capability(ts->input, EV_ABS, ABS_Y);
  86. input_set_abs_params(ts->input, ABS_X, 0, 1023, 0, 0);
  87. input_set_abs_params(ts->input, ABS_Y, 0, 1023, 0, 0);
  88. error = input_register_device(ts->input);
  89. if (error) {
  90. dev_err(&pdev->dev, "error registering touch input\n");
  91. return error;
  92. }
  93. platform_set_drvdata(pdev, ts);
  94. dev_info(&pdev->dev, "iPAQ micro touchscreen\n");
  95. return 0;
  96. }
  97. static int __maybe_unused micro_ts_suspend(struct device *dev)
  98. {
  99. struct touchscreen_data *ts = dev_get_drvdata(dev);
  100. micro_ts_toggle_receive(ts, false);
  101. return 0;
  102. }
  103. static int __maybe_unused micro_ts_resume(struct device *dev)
  104. {
  105. struct touchscreen_data *ts = dev_get_drvdata(dev);
  106. struct input_dev *input = ts->input;
  107. mutex_lock(&input->mutex);
  108. if (input_device_enabled(input))
  109. micro_ts_toggle_receive(ts, true);
  110. mutex_unlock(&input->mutex);
  111. return 0;
  112. }
  113. static const struct dev_pm_ops micro_ts_dev_pm_ops = {
  114. SET_SYSTEM_SLEEP_PM_OPS(micro_ts_suspend, micro_ts_resume)
  115. };
  116. static struct platform_driver micro_ts_device_driver = {
  117. .driver = {
  118. .name = "ipaq-micro-ts",
  119. .pm = &micro_ts_dev_pm_ops,
  120. },
  121. .probe = micro_ts_probe,
  122. };
  123. module_platform_driver(micro_ts_device_driver);
  124. MODULE_LICENSE("GPL");
  125. MODULE_DESCRIPTION("driver for iPAQ Atmel micro touchscreen");
  126. MODULE_ALIAS("platform:ipaq-micro-ts");