ar1021_i2c.c 4.3 KB

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