ibm-panel.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. unsigned long flags;
  69. struct ibm_panel *panel = i2c_get_clientdata(client);
  70. dev_dbg(&panel->input->dev, "event: %u data: %02x\n", event, *val);
  71. spin_lock_irqsave(&panel->lock, flags);
  72. switch (event) {
  73. case I2C_SLAVE_STOP:
  74. if (panel->idx == sizeof(panel->command))
  75. ibm_panel_process_command(panel);
  76. else
  77. dev_dbg(&panel->input->dev,
  78. "command incorrect size %u\n", panel->idx);
  79. fallthrough;
  80. case I2C_SLAVE_WRITE_REQUESTED:
  81. panel->idx = 0;
  82. break;
  83. case I2C_SLAVE_WRITE_RECEIVED:
  84. if (panel->idx < sizeof(panel->command))
  85. panel->command[panel->idx++] = *val;
  86. else
  87. /*
  88. * The command is too long and therefore invalid, so set the index
  89. * to it's largest possible value. When a STOP is finally received,
  90. * the command will be rejected upon processing.
  91. */
  92. panel->idx = U8_MAX;
  93. break;
  94. case I2C_SLAVE_READ_REQUESTED:
  95. case I2C_SLAVE_READ_PROCESSED:
  96. *val = 0xff;
  97. break;
  98. default:
  99. break;
  100. }
  101. spin_unlock_irqrestore(&panel->lock, flags);
  102. return 0;
  103. }
  104. static int ibm_panel_probe(struct i2c_client *client,
  105. const struct i2c_device_id *id)
  106. {
  107. struct ibm_panel *panel;
  108. int i;
  109. int error;
  110. panel = devm_kzalloc(&client->dev, sizeof(*panel), GFP_KERNEL);
  111. if (!panel)
  112. return -ENOMEM;
  113. spin_lock_init(&panel->lock);
  114. panel->input = devm_input_allocate_device(&client->dev);
  115. if (!panel->input)
  116. return -ENOMEM;
  117. panel->input->name = client->name;
  118. panel->input->id.bustype = BUS_I2C;
  119. error = device_property_read_u32_array(&client->dev,
  120. "linux,keycodes",
  121. panel->keycodes,
  122. PANEL_KEYCODES_COUNT);
  123. if (error) {
  124. /*
  125. * Use gamepad buttons as defaults for compatibility with
  126. * existing applications.
  127. */
  128. panel->keycodes[0] = BTN_NORTH;
  129. panel->keycodes[1] = BTN_SOUTH;
  130. panel->keycodes[2] = BTN_SELECT;
  131. }
  132. for (i = 0; i < PANEL_KEYCODES_COUNT; ++i)
  133. input_set_capability(panel->input, EV_KEY, panel->keycodes[i]);
  134. error = input_register_device(panel->input);
  135. if (error) {
  136. dev_err(&client->dev,
  137. "Failed to register input device: %d\n", error);
  138. return error;
  139. }
  140. i2c_set_clientdata(client, panel);
  141. error = i2c_slave_register(client, ibm_panel_i2c_slave_cb);
  142. if (error) {
  143. dev_err(&client->dev,
  144. "Failed to register as i2c slave: %d\n", error);
  145. return error;
  146. }
  147. return 0;
  148. }
  149. static void ibm_panel_remove(struct i2c_client *client)
  150. {
  151. i2c_slave_unregister(client);
  152. }
  153. static const struct of_device_id ibm_panel_match[] = {
  154. { .compatible = "ibm,op-panel" },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(of, ibm_panel_match);
  158. static struct i2c_driver ibm_panel_driver = {
  159. .driver = {
  160. .name = DEVICE_NAME,
  161. .of_match_table = ibm_panel_match,
  162. },
  163. .probe = ibm_panel_probe,
  164. .remove = ibm_panel_remove,
  165. };
  166. module_i2c_driver(ibm_panel_driver);
  167. MODULE_AUTHOR("Eddie James <[email protected]>");
  168. MODULE_DESCRIPTION("IBM Operation Panel Driver");
  169. MODULE_LICENSE("GPL");