1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) IBM Corporation 2020
7 #include <linux/init.h>
8 #include <linux/input.h>
9 #include <linux/kernel.h>
10 #include <linux/limits.h>
11 #include <linux/module.h>
13 #include <linux/spinlock.h>
15 #define DEVICE_NAME "ibm-panel"
16 #define PANEL_KEYCODES_COUNT 3
21 u32 keycodes[PANEL_KEYCODES_COUNT];
22 spinlock_t lock; /* protects writes to idx and command */
23 struct input_dev *input;
26 static u8 ibm_panel_calculate_checksum(struct ibm_panel *panel)
32 for (i = 0; i < sizeof(panel->command) - 1; ++i) {
33 sum += panel->command[i];
47 static void ibm_panel_process_command(struct ibm_panel *panel)
52 if (panel->command[0] != 0xff && panel->command[1] != 0xf0) {
53 dev_dbg(&panel->input->dev, "command invalid: %02x %02x\n",
54 panel->command[0], panel->command[1]);
58 chksum = ibm_panel_calculate_checksum(panel);
59 if (chksum != panel->command[sizeof(panel->command) - 1]) {
60 dev_dbg(&panel->input->dev,
61 "command failed checksum: %u != %u\n", chksum,
62 panel->command[sizeof(panel->command) - 1]);
66 button = panel->command[2] & 0xf;
67 if (button < PANEL_KEYCODES_COUNT) {
68 input_report_key(panel->input, panel->keycodes[button],
69 !(panel->command[2] & 0x80));
70 input_sync(panel->input);
72 dev_dbg(&panel->input->dev, "unknown button %u\n",
77 static int ibm_panel_i2c_slave_cb(struct i2c_client *client,
78 enum i2c_slave_event event, u8 *val)
81 struct ibm_panel *panel = i2c_get_clientdata(client);
83 dev_dbg(&panel->input->dev, "event: %u data: %02x\n", event, *val);
85 spin_lock_irqsave(&panel->lock, flags);
89 if (panel->idx == sizeof(panel->command))
90 ibm_panel_process_command(panel);
92 dev_dbg(&panel->input->dev,
93 "command incorrect size %u\n", panel->idx);
95 case I2C_SLAVE_WRITE_REQUESTED:
98 case I2C_SLAVE_WRITE_RECEIVED:
99 if (panel->idx < sizeof(panel->command))
100 panel->command[panel->idx++] = *val;
103 * The command is too long and therefore invalid, so set the index
104 * to it's largest possible value. When a STOP is finally received,
105 * the command will be rejected upon processing.
109 case I2C_SLAVE_READ_REQUESTED:
110 case I2C_SLAVE_READ_PROCESSED:
117 spin_unlock_irqrestore(&panel->lock, flags);
122 static int ibm_panel_probe(struct i2c_client *client)
124 struct ibm_panel *panel;
128 panel = devm_kzalloc(&client->dev, sizeof(*panel), GFP_KERNEL);
132 spin_lock_init(&panel->lock);
134 panel->input = devm_input_allocate_device(&client->dev);
138 panel->input->name = client->name;
139 panel->input->id.bustype = BUS_I2C;
141 error = device_property_read_u32_array(&client->dev,
144 PANEL_KEYCODES_COUNT);
147 * Use gamepad buttons as defaults for compatibility with
148 * existing applications.
150 panel->keycodes[0] = BTN_NORTH;
151 panel->keycodes[1] = BTN_SOUTH;
152 panel->keycodes[2] = BTN_SELECT;
155 for (i = 0; i < PANEL_KEYCODES_COUNT; ++i)
156 input_set_capability(panel->input, EV_KEY, panel->keycodes[i]);
158 error = input_register_device(panel->input);
160 dev_err(&client->dev,
161 "Failed to register input device: %d\n", error);
165 i2c_set_clientdata(client, panel);
166 error = i2c_slave_register(client, ibm_panel_i2c_slave_cb);
168 dev_err(&client->dev,
169 "Failed to register as i2c slave: %d\n", error);
176 static void ibm_panel_remove(struct i2c_client *client)
178 i2c_slave_unregister(client);
181 static const struct of_device_id ibm_panel_match[] = {
182 { .compatible = "ibm,op-panel" },
185 MODULE_DEVICE_TABLE(of, ibm_panel_match);
187 static struct i2c_driver ibm_panel_driver = {
190 .of_match_table = ibm_panel_match,
192 .probe_new = ibm_panel_probe,
193 .remove = ibm_panel_remove,
195 module_i2c_driver(ibm_panel_driver);
198 MODULE_DESCRIPTION("IBM Operation Panel Driver");
199 MODULE_LICENSE("GPL");