1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2025 Bootlin 4 * 5 * Author: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com> 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/bitops.h> 10 #include <linux/dev_printk.h> 11 #include <linux/device/devres.h> 12 #include <linux/err.h> 13 #include <linux/init.h> 14 #include <linux/input.h> 15 #include <linux/input/matrix_keypad.h> 16 #include <linux/interrupt.h> 17 #include <linux/mfd/max7360.h> 18 #include <linux/mod_devicetable.h> 19 #include <linux/minmax.h> 20 #include <linux/module.h> 21 #include <linux/property.h> 22 #include <linux/platform_device.h> 23 #include <linux/pm_wakeirq.h> 24 #include <linux/regmap.h> 25 26 struct max7360_keypad { 27 struct input_dev *input; 28 unsigned int rows; 29 unsigned int cols; 30 unsigned int debounce_ms; 31 int irq; 32 struct regmap *regmap; 33 unsigned short keycodes[MAX7360_MAX_KEY_ROWS * MAX7360_MAX_KEY_COLS]; 34 }; 35 36 static irqreturn_t max7360_keypad_irq(int irq, void *data) 37 { 38 struct max7360_keypad *max7360_keypad = data; 39 struct device *dev = max7360_keypad->input->dev.parent; 40 unsigned int val; 41 unsigned int row, col; 42 unsigned int release; 43 unsigned int code; 44 int error; 45 46 error = regmap_read(max7360_keypad->regmap, MAX7360_REG_KEYFIFO, &val); 47 if (error) { 48 dev_err(dev, "Failed to read MAX7360 FIFO"); 49 return IRQ_NONE; 50 } 51 52 /* FIFO overflow: ignore it and get next event. */ 53 if (val == MAX7360_FIFO_OVERFLOW) { 54 dev_warn(dev, "max7360 FIFO overflow"); 55 error = regmap_read_poll_timeout(max7360_keypad->regmap, MAX7360_REG_KEYFIFO, 56 val, val != MAX7360_FIFO_OVERFLOW, 0, 1000); 57 if (error) { 58 dev_err(dev, "Failed to empty MAX7360 FIFO"); 59 return IRQ_NONE; 60 } 61 } 62 63 if (val == MAX7360_FIFO_EMPTY) { 64 dev_dbg(dev, "Got a spurious interrupt"); 65 66 return IRQ_NONE; 67 } 68 69 row = FIELD_GET(MAX7360_FIFO_ROW, val); 70 col = FIELD_GET(MAX7360_FIFO_COL, val); 71 release = val & MAX7360_FIFO_RELEASE; 72 73 code = MATRIX_SCAN_CODE(row, col, get_count_order(max7360_keypad->cols)); 74 75 dev_dbg(dev, "key[%d:%d] %s\n", row, col, release ? "release" : "press"); 76 77 input_event(max7360_keypad->input, EV_MSC, MSC_SCAN, code); 78 input_report_key(max7360_keypad->input, max7360_keypad->keycodes[code], !release); 79 input_sync(max7360_keypad->input); 80 81 return IRQ_HANDLED; 82 } 83 84 static int max7360_keypad_open(struct input_dev *pdev) 85 { 86 struct max7360_keypad *max7360_keypad = input_get_drvdata(pdev); 87 struct device *dev = max7360_keypad->input->dev.parent; 88 int error; 89 90 /* Somebody is using the device: get out of sleep. */ 91 error = regmap_write_bits(max7360_keypad->regmap, MAX7360_REG_CONFIG, 92 MAX7360_CFG_SLEEP, MAX7360_CFG_SLEEP); 93 if (error) 94 dev_err(dev, "Failed to write max7360 configuration: %d\n", error); 95 96 return error; 97 } 98 99 static void max7360_keypad_close(struct input_dev *pdev) 100 { 101 struct max7360_keypad *max7360_keypad = input_get_drvdata(pdev); 102 struct device *dev = max7360_keypad->input->dev.parent; 103 int error; 104 105 /* Nobody is using the device anymore: go to sleep. */ 106 error = regmap_write_bits(max7360_keypad->regmap, MAX7360_REG_CONFIG, MAX7360_CFG_SLEEP, 0); 107 if (error) 108 dev_err(dev, "Failed to write max7360 configuration: %d\n", error); 109 } 110 111 static int max7360_keypad_hw_init(struct max7360_keypad *max7360_keypad) 112 { 113 struct device *dev = max7360_keypad->input->dev.parent; 114 unsigned int val; 115 int error; 116 117 val = max7360_keypad->debounce_ms - MAX7360_DEBOUNCE_MIN; 118 error = regmap_write_bits(max7360_keypad->regmap, MAX7360_REG_DEBOUNCE, 119 MAX7360_DEBOUNCE, 120 FIELD_PREP(MAX7360_DEBOUNCE, val)); 121 if (error) 122 return dev_err_probe(dev, error, 123 "Failed to write max7360 debounce configuration\n"); 124 125 error = regmap_write_bits(max7360_keypad->regmap, MAX7360_REG_INTERRUPT, 126 MAX7360_INTERRUPT_TIME_MASK, 127 FIELD_PREP(MAX7360_INTERRUPT_TIME_MASK, 1)); 128 if (error) 129 return dev_err_probe(dev, error, 130 "Failed to write max7360 keypad interrupt configuration\n"); 131 132 return 0; 133 } 134 135 static int max7360_keypad_build_keymap(struct max7360_keypad *max7360_keypad) 136 { 137 struct input_dev *input_dev = max7360_keypad->input; 138 struct device *dev = input_dev->dev.parent->parent; 139 struct matrix_keymap_data keymap_data; 140 const char *propname = "linux,keymap"; 141 unsigned int max_keys; 142 int error; 143 int size; 144 145 size = device_property_count_u32(dev, propname); 146 if (size <= 0) { 147 dev_err(dev, "missing or malformed property %s: %d\n", propname, size); 148 return size < 0 ? size : -EINVAL; 149 } 150 151 max_keys = max7360_keypad->cols * max7360_keypad->rows; 152 if (size > max_keys) { 153 dev_err(dev, "%s size overflow (%d vs max %u)\n", propname, size, max_keys); 154 return -EINVAL; 155 } 156 157 u32 *keys __free(kfree) = kmalloc_array(size, sizeof(*keys), GFP_KERNEL); 158 if (!keys) 159 return -ENOMEM; 160 161 error = device_property_read_u32_array(dev, propname, keys, size); 162 if (error) { 163 dev_err(dev, "failed to read %s property: %d\n", propname, error); 164 return error; 165 } 166 167 keymap_data.keymap = keys; 168 keymap_data.keymap_size = size; 169 error = matrix_keypad_build_keymap(&keymap_data, NULL, 170 max7360_keypad->rows, max7360_keypad->cols, 171 max7360_keypad->keycodes, max7360_keypad->input); 172 if (error) 173 return error; 174 175 return 0; 176 } 177 178 static int max7360_keypad_parse_fw(struct device *dev, 179 struct max7360_keypad *max7360_keypad, 180 bool *autorepeat) 181 { 182 int error; 183 184 error = matrix_keypad_parse_properties(dev->parent, &max7360_keypad->rows, 185 &max7360_keypad->cols); 186 if (error) 187 return error; 188 189 if (!max7360_keypad->rows || !max7360_keypad->cols || 190 max7360_keypad->rows > MAX7360_MAX_KEY_ROWS || 191 max7360_keypad->cols > MAX7360_MAX_KEY_COLS) { 192 dev_err(dev, "Invalid number of columns or rows (%ux%u)\n", 193 max7360_keypad->cols, max7360_keypad->rows); 194 return -EINVAL; 195 } 196 197 *autorepeat = device_property_read_bool(dev->parent, "autorepeat"); 198 199 max7360_keypad->debounce_ms = MAX7360_DEBOUNCE_MIN; 200 error = device_property_read_u32(dev->parent, "keypad-debounce-delay-ms", 201 &max7360_keypad->debounce_ms); 202 if (error == -EINVAL) { 203 dev_info(dev, "Using default keypad-debounce-delay-ms: %u\n", 204 max7360_keypad->debounce_ms); 205 } else if (error < 0) { 206 dev_err(dev, "Failed to read keypad-debounce-delay-ms property\n"); 207 return error; 208 } 209 210 if (!in_range(max7360_keypad->debounce_ms, MAX7360_DEBOUNCE_MIN, 211 MAX7360_DEBOUNCE_MAX - MAX7360_DEBOUNCE_MIN + 1)) { 212 dev_err(dev, "Invalid keypad-debounce-delay-ms: %u, should be between %u and %u.\n", 213 max7360_keypad->debounce_ms, MAX7360_DEBOUNCE_MIN, MAX7360_DEBOUNCE_MAX); 214 return -EINVAL; 215 } 216 217 return 0; 218 } 219 220 static int max7360_keypad_probe(struct platform_device *pdev) 221 { 222 struct max7360_keypad *max7360_keypad; 223 struct device *dev = &pdev->dev; 224 struct input_dev *input; 225 struct regmap *regmap; 226 bool autorepeat; 227 int error; 228 int irq; 229 230 regmap = dev_get_regmap(dev->parent, NULL); 231 if (!regmap) 232 return dev_err_probe(dev, -ENODEV, "Could not get parent regmap\n"); 233 234 irq = fwnode_irq_get_byname(dev_fwnode(dev->parent), "intk"); 235 if (irq < 0) 236 return dev_err_probe(dev, irq, "Failed to get IRQ\n"); 237 238 max7360_keypad = devm_kzalloc(dev, sizeof(*max7360_keypad), GFP_KERNEL); 239 if (!max7360_keypad) 240 return -ENOMEM; 241 242 max7360_keypad->regmap = regmap; 243 244 error = max7360_keypad_parse_fw(dev, max7360_keypad, &autorepeat); 245 if (error) 246 return error; 247 248 input = devm_input_allocate_device(dev); 249 if (!input) 250 return -ENOMEM; 251 252 max7360_keypad->input = input; 253 254 input->id.bustype = BUS_I2C; 255 input->name = pdev->name; 256 input->open = max7360_keypad_open; 257 input->close = max7360_keypad_close; 258 259 error = max7360_keypad_build_keymap(max7360_keypad); 260 if (error) 261 return dev_err_probe(dev, error, "Failed to build keymap\n"); 262 263 input_set_capability(input, EV_MSC, MSC_SCAN); 264 if (autorepeat) 265 __set_bit(EV_REP, input->evbit); 266 267 input_set_drvdata(input, max7360_keypad); 268 269 error = devm_request_threaded_irq(dev, irq, NULL, max7360_keypad_irq, 270 IRQF_ONESHOT, 271 "max7360-keypad", max7360_keypad); 272 if (error) 273 return dev_err_probe(dev, error, "Failed to register interrupt\n"); 274 275 error = input_register_device(input); 276 if (error) 277 return dev_err_probe(dev, error, "Could not register input device\n"); 278 279 error = max7360_keypad_hw_init(max7360_keypad); 280 if (error) 281 return dev_err_probe(dev, error, "Failed to initialize max7360 keypad\n"); 282 283 device_init_wakeup(dev, true); 284 error = dev_pm_set_wake_irq(dev, irq); 285 if (error) 286 dev_warn(dev, "Failed to set up wakeup irq: %d\n", error); 287 288 return 0; 289 } 290 291 static void max7360_keypad_remove(struct platform_device *pdev) 292 { 293 dev_pm_clear_wake_irq(&pdev->dev); 294 device_init_wakeup(&pdev->dev, false); 295 } 296 297 static struct platform_driver max7360_keypad_driver = { 298 .driver = { 299 .name = "max7360-keypad", 300 }, 301 .probe = max7360_keypad_probe, 302 .remove = max7360_keypad_remove, 303 }; 304 module_platform_driver(max7360_keypad_driver); 305 306 MODULE_DESCRIPTION("MAX7360 Keypad driver"); 307 MODULE_AUTHOR("Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>"); 308 MODULE_LICENSE("GPL"); 309