ar1021_i2c.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. __set_bit(INPUT_PROP_DIRECT, input->propbit);
  96. input_set_capability(input, EV_KEY, BTN_TOUCH);
  97. input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
  98. input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
  99. input_set_drvdata(input, ar1021);
  100. error = devm_request_threaded_irq(&client->dev, client->irq,
  101. NULL, ar1021_i2c_irq,
  102. IRQF_ONESHOT,
  103. "ar1021_i2c", ar1021);
  104. if (error) {
  105. dev_err(&client->dev,
  106. "Failed to enable IRQ, error: %d\n", error);
  107. return error;
  108. }
  109. /* Disable the IRQ, we'll enable it in ar1021_i2c_open() */
  110. disable_irq(client->irq);
  111. error = input_register_device(ar1021->input);
  112. if (error) {
  113. dev_err(&client->dev,
  114. "Failed to register input device, error: %d\n", error);
  115. return error;
  116. }
  117. return 0;
  118. }
  119. static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
  120. {
  121. struct i2c_client *client = to_i2c_client(dev);
  122. disable_irq(client->irq);
  123. return 0;
  124. }
  125. static int __maybe_unused ar1021_i2c_resume(struct device *dev)
  126. {
  127. struct i2c_client *client = to_i2c_client(dev);
  128. enable_irq(client->irq);
  129. return 0;
  130. }
  131. static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
  132. static const struct i2c_device_id ar1021_i2c_id[] = {
  133. { "ar1021", 0 },
  134. { },
  135. };
  136. MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
  137. static const struct of_device_id ar1021_i2c_of_match[] = {
  138. { .compatible = "microchip,ar1021-i2c", },
  139. { }
  140. };
  141. MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
  142. static struct i2c_driver ar1021_i2c_driver = {
  143. .driver = {
  144. .name = "ar1021_i2c",
  145. .pm = &ar1021_i2c_pm,
  146. .of_match_table = ar1021_i2c_of_match,
  147. },
  148. .probe = ar1021_i2c_probe,
  149. .id_table = ar1021_i2c_id,
  150. };
  151. module_i2c_driver(ar1021_i2c_driver);
  152. MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
  153. MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver");
  154. MODULE_LICENSE("GPL");