ar1021_i2c.c 4.3 KB

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