ipaq-micro-ts.c 3.8 KB

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