ibm-panel.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) IBM Corporation 2020
  4. */
  5. #include <linux/i2c.h>
  6. #include <linux/init.h>
  7. #include <linux/input.h>
  8. #include <linux/kernel.h>
  9. #include <linux/limits.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/spinlock.h>
  13. #define DEVICE_NAME "ibm-panel"
  14. #define PANEL_KEYCODES_COUNT 3
  15. struct ibm_panel {
  16. u8 idx;
  17. u8 command[11];
  18. u32 keycodes[PANEL_KEYCODES_COUNT];
  19. spinlock_t lock; /* protects writes to idx and command */
  20. struct input_dev *input;
  21. };
  22. static u8 ibm_panel_calculate_checksum(struct ibm_panel *panel)
  23. {
  24. u8 chksum;
  25. u16 sum = 0;
  26. unsigned int i;
  27. for (i = 0; i < sizeof(panel->command) - 1; ++i) {
  28. sum += panel->command[i];
  29. if (sum & 0xff00) {
  30. sum &= 0xff;
  31. sum++;
  32. }
  33. }
  34. chksum = sum & 0xff;
  35. chksum = ~chksum;
  36. chksum++;
  37. return chksum;
  38. }
  39. static void ibm_panel_process_command(struct ibm_panel *panel)
  40. {
  41. u8 button;
  42. u8 chksum;
  43. if (panel->command[0] != 0xff && panel->command[1] != 0xf0) {
  44. dev_dbg(&panel->input->dev, "command invalid: %02x %02x\n",
  45. panel->command[0], panel->command[1]);
  46. return;
  47. }
  48. chksum = ibm_panel_calculate_checksum(panel);
  49. if (chksum != panel->command[sizeof(panel->command) - 1]) {
  50. dev_dbg(&panel->input->dev,
  51. "command failed checksum: %u != %u\n", chksum,
  52. panel->command[sizeof(panel->command) - 1]);
  53. return;
  54. }
  55. button = panel->command[2] & 0xf;
  56. if (button < PANEL_KEYCODES_COUNT) {
  57. input_report_key(panel->input, panel->keycodes[button],
  58. !(panel->command[2] & 0x80));
  59. input_sync(panel->input);
  60. } else {
  61. dev_dbg(&panel->input->dev, "unknown button %u\n",
  62. button);
  63. }
  64. }
  65. static int ibm_panel_i2c_slave_cb(struct i2c_client *client,
  66. enum i2c_slave_event event, u8 *val)
  67. {
  68. struct ibm_panel *panel = i2c_get_clientdata(client);
  69. dev_dbg(&panel->input->dev, "event: %u data: %02x\n", event, *val);
  70. guard(spinlock_irqsave)(&panel->lock);
  71. switch (event) {
  72. case I2C_SLAVE_STOP:
  73. if (panel->idx == sizeof(panel->command))
  74. ibm_panel_process_command(panel);
  75. else
  76. dev_dbg(&panel->input->dev,
  77. "command incorrect size %u\n", panel->idx);
  78. fallthrough;
  79. case I2C_SLAVE_WRITE_REQUESTED:
  80. panel->idx = 0;
  81. break;
  82. case I2C_SLAVE_WRITE_RECEIVED:
  83. if (panel->idx < sizeof(panel->command))
  84. panel->command[panel->idx++] = *val;
  85. else
  86. /*
  87. * The command is too long and therefore invalid, so set the index
  88. * to it's largest possible value. When a STOP is finally received,
  89. * the command will be rejected upon processing.
  90. */
  91. panel->idx = U8_MAX;
  92. break;
  93. case I2C_SLAVE_READ_REQUESTED:
  94. case I2C_SLAVE_READ_PROCESSED:
  95. *val = 0xff;
  96. break;
  97. default:
  98. break;
  99. }
  100. return 0;
  101. }
  102. static int ibm_panel_probe(struct i2c_client *client)
  103. {
  104. struct ibm_panel *panel;
  105. int i;
  106. int error;
  107. panel = devm_kzalloc(&client->dev, sizeof(*panel), GFP_KERNEL);
  108. if (!panel)
  109. return -ENOMEM;
  110. spin_lock_init(&panel->lock);
  111. panel->input = devm_input_allocate_device(&client->dev);
  112. if (!panel->input)
  113. return -ENOMEM;
  114. panel->input->name = client->name;
  115. panel->input->id.bustype = BUS_I2C;
  116. error = device_property_read_u32_array(&client->dev,
  117. "linux,keycodes",
  118. panel->keycodes,
  119. PANEL_KEYCODES_COUNT);
  120. if (error) {
  121. /*
  122. * Use gamepad buttons as defaults for compatibility with
  123. * existing applications.
  124. */
  125. panel->keycodes[0] = BTN_NORTH;
  126. panel->keycodes[1] = BTN_SOUTH;
  127. panel->keycodes[2] = BTN_SELECT;
  128. }
  129. for (i = 0; i < PANEL_KEYCODES_COUNT; ++i)
  130. input_set_capability(panel->input, EV_KEY, panel->keycodes[i]);
  131. error = input_register_device(panel->input);
  132. if (error) {
  133. dev_err(&client->dev,
  134. "Failed to register input device: %d\n", error);
  135. return error;
  136. }
  137. i2c_set_clientdata(client, panel);
  138. error = i2c_slave_register(client, ibm_panel_i2c_slave_cb);
  139. if (error) {
  140. dev_err(&client->dev,
  141. "Failed to register as i2c slave: %d\n", error);
  142. return error;
  143. }
  144. return 0;
  145. }
  146. static void ibm_panel_remove(struct i2c_client *client)
  147. {
  148. i2c_slave_unregister(client);
  149. }
  150. static const struct of_device_id ibm_panel_match[] = {
  151. { .compatible = "ibm,op-panel" },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(of, ibm_panel_match);
  155. static struct i2c_driver ibm_panel_driver = {
  156. .driver = {
  157. .name = DEVICE_NAME,
  158. .of_match_table = ibm_panel_match,
  159. },
  160. .probe = ibm_panel_probe,
  161. .remove = ibm_panel_remove,
  162. };
  163. module_i2c_driver(ibm_panel_driver);
  164. MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
  165. MODULE_DESCRIPTION("IBM Operation Panel Driver");
  166. MODULE_LICENSE("GPL");