Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: Add IBM Operation Panel driver

Add a driver to get the button events from the panel and provide
them to userspace with the input subsystem. The panel is
connected with I2C and controls the bus, so the driver registers
as an I2C slave device.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
Reviewed-by: Joel Stanley <joel@jms.id.au>
Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com> # I2C slave parts
Link: https://lore.kernel.org/r/20220809204147.238132-3-eajames@linux.ibm.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

authored by

Eddie James and committed by
Dmitry Torokhov
2e6f34fa bc604fbb

+219
+1
MAINTAINERS
··· 9648 9648 L: linux-input@vger.kernel.org 9649 9649 S: Maintained 9650 9650 F: Documentation/devicetree/bindings/input/ibm,op-panel.yaml 9651 + F: drivers/input/misc/ibm-panel.c 9651 9652 9652 9653 IBM Power 842 compression accelerator 9653 9654 M: Haren Myneni <haren@us.ibm.com>
+18
drivers/input/misc/Kconfig
··· 730 730 To compile this driver as a module, choose M here: the 731 731 module will be called adxl34x-spi. 732 732 733 + config INPUT_IBM_PANEL 734 + tristate "IBM Operation Panel driver" 735 + depends on I2C && I2C_SLAVE 736 + help 737 + Say Y here if you have an IBM Operation Panel connected to your system 738 + over I2C. The panel is typically connected only to a system's service 739 + processor (BMC). 740 + 741 + If unsure, say N. 742 + 743 + The Operation Panel is a controller with some buttons and an LCD 744 + display that allows someone with physical access to the system to 745 + perform various administrative tasks. This driver only supports the part 746 + of the controller that sends commands to the system. 747 + 748 + To compile this driver as a module, choose M here: the module will be 749 + called ibm-panel. 750 + 733 751 config INPUT_IMS_PCU 734 752 tristate "IMS Passenger Control Unit driver" 735 753 depends on USB
+1
drivers/input/misc/Makefile
··· 41 41 obj-$(CONFIG_INPUT_GPIO_VIBRA) += gpio-vibra.o 42 42 obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o 43 43 obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o 44 + obj-$(CONFIG_INPUT_IBM_PANEL) += ibm-panel.o 44 45 obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.o 45 46 obj-$(CONFIG_INPUT_IQS269A) += iqs269a.o 46 47 obj-$(CONFIG_INPUT_IQS626A) += iqs626a.o
+199
drivers/input/misc/ibm-panel.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) IBM Corporation 2020 4 + */ 5 + 6 + #include <linux/i2c.h> 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> 12 + #include <linux/of.h> 13 + #include <linux/spinlock.h> 14 + 15 + #define DEVICE_NAME "ibm-panel" 16 + #define PANEL_KEYCODES_COUNT 3 17 + 18 + struct ibm_panel { 19 + u8 idx; 20 + u8 command[11]; 21 + u32 keycodes[PANEL_KEYCODES_COUNT]; 22 + spinlock_t lock; /* protects writes to idx and command */ 23 + struct input_dev *input; 24 + }; 25 + 26 + static u8 ibm_panel_calculate_checksum(struct ibm_panel *panel) 27 + { 28 + u8 chksum; 29 + u16 sum = 0; 30 + unsigned int i; 31 + 32 + for (i = 0; i < sizeof(panel->command) - 1; ++i) { 33 + sum += panel->command[i]; 34 + if (sum & 0xff00) { 35 + sum &= 0xff; 36 + sum++; 37 + } 38 + } 39 + 40 + chksum = sum & 0xff; 41 + chksum = ~chksum; 42 + chksum++; 43 + 44 + return chksum; 45 + } 46 + 47 + static void ibm_panel_process_command(struct ibm_panel *panel) 48 + { 49 + u8 button; 50 + u8 chksum; 51 + 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]); 55 + return; 56 + } 57 + 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]); 63 + return; 64 + } 65 + 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); 71 + } else { 72 + dev_dbg(&panel->input->dev, "unknown button %u\n", 73 + button); 74 + } 75 + } 76 + 77 + static int ibm_panel_i2c_slave_cb(struct i2c_client *client, 78 + enum i2c_slave_event event, u8 *val) 79 + { 80 + unsigned long flags; 81 + struct ibm_panel *panel = i2c_get_clientdata(client); 82 + 83 + dev_dbg(&panel->input->dev, "event: %u data: %02x\n", event, *val); 84 + 85 + spin_lock_irqsave(&panel->lock, flags); 86 + 87 + switch (event) { 88 + case I2C_SLAVE_STOP: 89 + if (panel->idx == sizeof(panel->command)) 90 + ibm_panel_process_command(panel); 91 + else 92 + dev_dbg(&panel->input->dev, 93 + "command incorrect size %u\n", panel->idx); 94 + fallthrough; 95 + case I2C_SLAVE_WRITE_REQUESTED: 96 + panel->idx = 0; 97 + break; 98 + case I2C_SLAVE_WRITE_RECEIVED: 99 + if (panel->idx < sizeof(panel->command)) 100 + panel->command[panel->idx++] = *val; 101 + else 102 + /* 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. 106 + */ 107 + panel->idx = U8_MAX; 108 + break; 109 + case I2C_SLAVE_READ_REQUESTED: 110 + case I2C_SLAVE_READ_PROCESSED: 111 + *val = 0xff; 112 + break; 113 + default: 114 + break; 115 + } 116 + 117 + spin_unlock_irqrestore(&panel->lock, flags); 118 + 119 + return 0; 120 + } 121 + 122 + static int ibm_panel_probe(struct i2c_client *client, 123 + const struct i2c_device_id *id) 124 + { 125 + struct ibm_panel *panel; 126 + int i; 127 + int error; 128 + 129 + panel = devm_kzalloc(&client->dev, sizeof(*panel), GFP_KERNEL); 130 + if (!panel) 131 + return -ENOMEM; 132 + 133 + spin_lock_init(&panel->lock); 134 + 135 + panel->input = devm_input_allocate_device(&client->dev); 136 + if (!panel->input) 137 + return -ENOMEM; 138 + 139 + panel->input->name = client->name; 140 + panel->input->id.bustype = BUS_I2C; 141 + 142 + error = device_property_read_u32_array(&client->dev, 143 + "linux,keycodes", 144 + panel->keycodes, 145 + PANEL_KEYCODES_COUNT); 146 + if (error) { 147 + /* 148 + * Use gamepad buttons as defaults for compatibility with 149 + * existing applications. 150 + */ 151 + panel->keycodes[0] = BTN_NORTH; 152 + panel->keycodes[1] = BTN_SOUTH; 153 + panel->keycodes[2] = BTN_SELECT; 154 + } 155 + 156 + for (i = 0; i < PANEL_KEYCODES_COUNT; ++i) 157 + input_set_capability(panel->input, EV_KEY, panel->keycodes[i]); 158 + 159 + error = input_register_device(panel->input); 160 + if (error) { 161 + dev_err(&client->dev, 162 + "Failed to register input device: %d\n", error); 163 + return error; 164 + } 165 + 166 + i2c_set_clientdata(client, panel); 167 + error = i2c_slave_register(client, ibm_panel_i2c_slave_cb); 168 + if (error) { 169 + dev_err(&client->dev, 170 + "Failed to register as i2c slave: %d\n", error); 171 + return error; 172 + } 173 + 174 + return 0; 175 + } 176 + 177 + static void ibm_panel_remove(struct i2c_client *client) 178 + { 179 + i2c_slave_unregister(client); 180 + } 181 + 182 + static const struct of_device_id ibm_panel_match[] = { 183 + { .compatible = "ibm,op-panel" }, 184 + { } 185 + }; 186 + 187 + static struct i2c_driver ibm_panel_driver = { 188 + .driver = { 189 + .name = DEVICE_NAME, 190 + .of_match_table = ibm_panel_match, 191 + }, 192 + .probe = ibm_panel_probe, 193 + .remove = ibm_panel_remove, 194 + }; 195 + module_i2c_driver(ibm_panel_driver); 196 + 197 + MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>"); 198 + MODULE_DESCRIPTION("IBM Operation Panel Driver"); 199 + MODULE_LICENSE("GPL");