ariel-pwrbutton.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later
  2. /*
  3. * Dell Wyse 3020 a.k.a. "Ariel" Power Button Driver
  4. *
  5. * Copyright (C) 2020 Lubomir Rintel
  6. */
  7. #include <linux/device.h>
  8. #include <linux/gfp.h>
  9. #include <linux/input.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/spi/spi.h>
  14. #define RESP_COUNTER(response) (response.header & 0x3)
  15. #define RESP_SIZE(response) ((response.header >> 2) & 0x3)
  16. #define RESP_TYPE(response) ((response.header >> 4) & 0xf)
  17. struct ec_input_response {
  18. u8 reserved;
  19. u8 header;
  20. u8 data[3];
  21. } __packed;
  22. struct ariel_pwrbutton {
  23. struct spi_device *client;
  24. struct input_dev *input;
  25. u8 msg_counter;
  26. };
  27. static int ec_input_read(struct ariel_pwrbutton *priv,
  28. struct ec_input_response *response)
  29. {
  30. u8 read_request[] = { 0x00, 0x5a, 0xa5, 0x00, 0x00 };
  31. struct spi_device *spi = priv->client;
  32. struct spi_transfer t = {
  33. .tx_buf = read_request,
  34. .rx_buf = response,
  35. .len = sizeof(read_request),
  36. };
  37. compiletime_assert(sizeof(read_request) == sizeof(*response),
  38. "SPI xfer request/response size mismatch");
  39. return spi_sync_transfer(spi, &t, 1);
  40. }
  41. static irqreturn_t ec_input_interrupt(int irq, void *dev_id)
  42. {
  43. struct ariel_pwrbutton *priv = dev_id;
  44. struct spi_device *spi = priv->client;
  45. struct ec_input_response response;
  46. int error;
  47. int i;
  48. error = ec_input_read(priv, &response);
  49. if (error < 0) {
  50. dev_err(&spi->dev, "EC read failed: %d\n", error);
  51. goto out;
  52. }
  53. if (priv->msg_counter == RESP_COUNTER(response)) {
  54. dev_warn(&spi->dev, "No new data to read?\n");
  55. goto out;
  56. }
  57. priv->msg_counter = RESP_COUNTER(response);
  58. if (RESP_TYPE(response) != 0x3 && RESP_TYPE(response) != 0xc) {
  59. dev_dbg(&spi->dev, "Ignoring message that's not kbd data\n");
  60. goto out;
  61. }
  62. for (i = 0; i < RESP_SIZE(response); i++) {
  63. switch (response.data[i]) {
  64. case 0x74:
  65. input_report_key(priv->input, KEY_POWER, 1);
  66. input_sync(priv->input);
  67. break;
  68. case 0xf4:
  69. input_report_key(priv->input, KEY_POWER, 0);
  70. input_sync(priv->input);
  71. break;
  72. default:
  73. dev_dbg(&spi->dev, "Unknown scan code: %02x\n",
  74. response.data[i]);
  75. }
  76. }
  77. out:
  78. return IRQ_HANDLED;
  79. }
  80. static int ariel_pwrbutton_probe(struct spi_device *spi)
  81. {
  82. struct ec_input_response response;
  83. struct ariel_pwrbutton *priv;
  84. int error;
  85. if (!spi->irq) {
  86. dev_err(&spi->dev, "Missing IRQ.\n");
  87. return -EINVAL;
  88. }
  89. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  90. if (!priv)
  91. return -ENOMEM;
  92. priv->client = spi;
  93. spi_set_drvdata(spi, priv);
  94. priv->input = devm_input_allocate_device(&spi->dev);
  95. if (!priv->input)
  96. return -ENOMEM;
  97. priv->input->name = "Power Button";
  98. priv->input->dev.parent = &spi->dev;
  99. input_set_capability(priv->input, EV_KEY, KEY_POWER);
  100. error = input_register_device(priv->input);
  101. if (error) {
  102. dev_err(&spi->dev, "error registering input device: %d\n", error);
  103. return error;
  104. }
  105. error = ec_input_read(priv, &response);
  106. if (error < 0) {
  107. dev_err(&spi->dev, "EC read failed: %d\n", error);
  108. return error;
  109. }
  110. priv->msg_counter = RESP_COUNTER(response);
  111. error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
  112. ec_input_interrupt,
  113. IRQF_ONESHOT,
  114. "Ariel EC Input", priv);
  115. if (error) {
  116. dev_err(&spi->dev, "Failed to request IRQ %d: %d\n",
  117. spi->irq, error);
  118. return error;
  119. }
  120. return 0;
  121. }
  122. static const struct of_device_id ariel_pwrbutton_of_match[] = {
  123. { .compatible = "dell,wyse-ariel-ec-input" },
  124. { }
  125. };
  126. MODULE_DEVICE_TABLE(of, ariel_pwrbutton_of_match);
  127. static const struct spi_device_id ariel_pwrbutton_spi_ids[] = {
  128. { .name = "wyse-ariel-ec-input" },
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(spi, ariel_pwrbutton_spi_ids);
  132. static struct spi_driver ariel_pwrbutton_driver = {
  133. .driver = {
  134. .name = "dell-wyse-ariel-ec-input",
  135. .of_match_table = ariel_pwrbutton_of_match,
  136. },
  137. .probe = ariel_pwrbutton_probe,
  138. .id_table = ariel_pwrbutton_spi_ids,
  139. };
  140. module_spi_driver(ariel_pwrbutton_driver);
  141. MODULE_AUTHOR("Lubomir Rintel <[email protected]>");
  142. MODULE_DESCRIPTION("Dell Wyse 3020 Power Button Input Driver");
  143. MODULE_LICENSE("Dual BSD/GPL");