ar1021_i2c.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Microchip AR1021 driver for I2C
  3. *
  4. * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
  5. *
  6. * License: GPLv2 as published by the FSF.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/input.h>
  10. #include <linux/of.h>
  11. #include <linux/i2c.h>
  12. #include <linux/irq.h>
  13. #include <linux/interrupt.h>
  14. #define AR1021_TOCUH_PKG_SIZE 5
  15. #define AR1021_MAX_X 4095
  16. #define AR1021_MAX_Y 4095
  17. struct ar1021_i2c {
  18. struct i2c_client *client;
  19. struct input_dev *input;
  20. u8 data[AR1021_TOCUH_PKG_SIZE];
  21. };
  22. static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
  23. {
  24. struct ar1021_i2c *ar1021 = dev_id;
  25. struct input_dev *input = ar1021->input;
  26. u8 *data = ar1021->data;
  27. unsigned int x, y, button;
  28. int retval;
  29. retval = i2c_master_recv(ar1021->client,
  30. ar1021->data, sizeof(ar1021->data));
  31. if (retval != sizeof(ar1021->data))
  32. goto out;
  33. /* sync bit set ? */
  34. if ((data[0] & 0x80) == 0)
  35. goto out;
  36. button = data[0] & BIT(0);
  37. x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
  38. y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
  39. input_report_abs(input, ABS_X, x);
  40. input_report_abs(input, ABS_Y, y);
  41. input_report_key(input, BTN_TOUCH, button);
  42. input_sync(input);
  43. out:
  44. return IRQ_HANDLED;
  45. }
  46. static int ar1021_i2c_open(struct input_dev *dev)
  47. {
  48. struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
  49. struct i2c_client *client = ar1021->client;
  50. enable_irq(client->irq);
  51. return 0;
  52. }
  53. static void ar1021_i2c_close(struct input_dev *dev)
  54. {
  55. struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
  56. struct i2c_client *client = ar1021->client;
  57. disable_irq(client->irq);
  58. }
  59. static int ar1021_i2c_probe(struct i2c_client *client,
  60. const struct i2c_device_id *id)
  61. {
  62. struct ar1021_i2c *ar1021;
  63. struct input_dev *input;
  64. int error;
  65. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  66. dev_err(&client->dev, "i2c_check_functionality error\n");
  67. return -ENXIO;
  68. }
  69. ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
  70. if (!ar1021)
  71. return -ENOMEM;
  72. input = devm_input_allocate_device(&client->dev);
  73. if (!input)
  74. return -ENOMEM;
  75. ar1021->client = client;
  76. ar1021->input = input;
  77. input->name = "ar1021 I2C Touchscreen";
  78. input->id.bustype = BUS_I2C;
  79. input->dev.parent = &client->dev;
  80. input->open = ar1021_i2c_open;
  81. input->close = ar1021_i2c_close;
  82. input_set_capability(input, EV_KEY, BTN_TOUCH);
  83. input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
  84. input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
  85. input_set_drvdata(input, ar1021);
  86. error = devm_request_threaded_irq(&client->dev, client->irq,
  87. NULL, ar1021_i2c_irq,
  88. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  89. "ar1021_i2c", ar1021);
  90. if (error) {
  91. dev_err(&client->dev,
  92. "Failed to enable IRQ, error: %d\n", error);
  93. return error;
  94. }
  95. /* Disable the IRQ, we'll enable it in ar1021_i2c_open() */
  96. disable_irq(client->irq);
  97. error = input_register_device(ar1021->input);
  98. if (error) {
  99. dev_err(&client->dev,
  100. "Failed to register input device, error: %d\n", error);
  101. return error;
  102. }
  103. i2c_set_clientdata(client, ar1021);
  104. return 0;
  105. }
  106. static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
  107. {
  108. struct i2c_client *client = to_i2c_client(dev);
  109. disable_irq(client->irq);
  110. return 0;
  111. }
  112. static int __maybe_unused ar1021_i2c_resume(struct device *dev)
  113. {
  114. struct i2c_client *client = to_i2c_client(dev);
  115. enable_irq(client->irq);
  116. return 0;
  117. }
  118. static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
  119. static const struct i2c_device_id ar1021_i2c_id[] = {
  120. { "ar1021", 0 },
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
  124. static const struct of_device_id ar1021_i2c_of_match[] = {
  125. { .compatible = "microchip,ar1021-i2c", },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
  129. static struct i2c_driver ar1021_i2c_driver = {
  130. .driver = {
  131. .name = "ar1021_i2c",
  132. .pm = &ar1021_i2c_pm,
  133. .of_match_table = ar1021_i2c_of_match,
  134. },
  135. .probe = ar1021_i2c_probe,
  136. .id_table = ar1021_i2c_id,
  137. };
  138. module_i2c_driver(ar1021_i2c_driver);
  139. MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
  140. MODULE_DESCRIPTION("Microchip AR1021 I2C Driver");
  141. MODULE_LICENSE("GPL");