| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Analog Devices ADP5585 Keys driver |
| 4 | * |
| 5 | * Copyright (C) 2025 Analog Devices, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/bitmap.h> |
| 9 | #include <linux/container_of.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/find.h> |
| 12 | #include <linux/input.h> |
| 13 | #include <linux/input/matrix_keypad.h> |
| 14 | #include <linux/mfd/adp5585.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/mod_devicetable.h> |
| 17 | #include <linux/notifier.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/property.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/types.h> |
| 22 | |
| 23 | /* As needed for the matrix parsing code */ |
| 24 | #define ADP5589_MAX_KEYMAPSIZE 123 |
| 25 | |
| 26 | struct adp5585_kpad_chip { |
| 27 | u8 key_ev_min; |
| 28 | u8 key_ev_max; |
| 29 | u8 max_rows; |
| 30 | u8 max_cols; |
| 31 | }; |
| 32 | |
| 33 | struct adp5585_kpad { |
| 34 | const struct adp5585_kpad_chip *info; |
| 35 | struct notifier_block nb; |
| 36 | struct input_dev *input; |
| 37 | unsigned short keycode[ADP5589_MAX_KEYMAPSIZE]; |
| 38 | struct device *dev; |
| 39 | unsigned long keypad; |
| 40 | int row_shift; |
| 41 | }; |
| 42 | |
| 43 | static int adp5585_keys_validate_events(const struct adp5585_kpad *kpad, |
| 44 | const u32 *events, u32 n_events) |
| 45 | { |
| 46 | unsigned int ev; |
| 47 | u32 row, col; |
| 48 | |
| 49 | for (ev = 0; ev < n_events; ev++) { |
| 50 | if (events[ev] < kpad->info->key_ev_min || |
| 51 | events[ev] > kpad->info->key_ev_max) |
| 52 | continue; |
| 53 | |
| 54 | /* |
| 55 | * if the event is to be generated by the keymap, we need to make |
| 56 | * sure that the pins are part of it! |
| 57 | */ |
| 58 | row = (events[ev] - 1) / kpad->info->max_cols; |
| 59 | col = (events[ev] - 1) % kpad->info->max_cols; |
| 60 | |
| 61 | if (test_bit(row, &kpad->keypad) && |
| 62 | test_bit(col + kpad->info->max_rows, &kpad->keypad)) |
| 63 | continue; |
| 64 | |
| 65 | return dev_err_probe(dev: kpad->dev, err: -EINVAL, |
| 66 | fmt: "Invalid unlock/reset event(%u) not used in the keypad\n" , |
| 67 | events[ev]); |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int adp5585_keys_check_special_events(const struct adp5585_dev *adp5585, |
| 74 | const struct adp5585_kpad *kpad) |
| 75 | { |
| 76 | int error; |
| 77 | |
| 78 | error = adp5585_keys_validate_events(kpad, events: adp5585->unlock_keys, |
| 79 | n_events: adp5585->nkeys_unlock); |
| 80 | if (error) |
| 81 | return error; |
| 82 | |
| 83 | error = adp5585_keys_validate_events(kpad, events: adp5585->reset1_keys, |
| 84 | n_events: adp5585->nkeys_reset1); |
| 85 | if (error) |
| 86 | return error; |
| 87 | |
| 88 | return adp5585_keys_validate_events(kpad, events: adp5585->reset2_keys, |
| 89 | n_events: adp5585->nkeys_reset2); |
| 90 | } |
| 91 | |
| 92 | static void adp5585_keys_pins_free(void *data) |
| 93 | { |
| 94 | struct adp5585_kpad *kpad = data; |
| 95 | struct adp5585_dev *adp5585 = dev_get_drvdata(dev: kpad->dev->parent); |
| 96 | unsigned int pin; |
| 97 | |
| 98 | for_each_set_bit(pin, &kpad->keypad, adp5585->n_pins) |
| 99 | clear_bit(nr: pin, addr: adp5585->pin_usage); |
| 100 | } |
| 101 | |
| 102 | static int adp5585_keys_parse_fw(const struct adp5585_dev *adp5585, |
| 103 | struct adp5585_kpad *kpad) |
| 104 | { |
| 105 | struct device *dev = kpad->dev; |
| 106 | u32 cols = 0, rows = 0, pin; |
| 107 | int error, n_pins; |
| 108 | |
| 109 | /* |
| 110 | * We do not check for errors (or no value) since the input device is |
| 111 | * only added if this property is present in the first place. |
| 112 | */ |
| 113 | n_pins = device_property_count_u32(dev, propname: "adi,keypad-pins" ); |
| 114 | if (n_pins > adp5585->n_pins) |
| 115 | return dev_err_probe(dev, err: -EINVAL, |
| 116 | fmt: "Too many keypad pins (%d) defined (max=%d)\n" , |
| 117 | n_pins, adp5585->n_pins); |
| 118 | |
| 119 | unsigned int *keypad_pins __free(kfree) = kcalloc(n_pins, sizeof(*keypad_pins), |
| 120 | GFP_KERNEL); |
| 121 | if (!keypad_pins) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | error = device_property_read_u32_array(dev, propname: "adi,keypad-pins" , |
| 125 | val: keypad_pins, nval: n_pins); |
| 126 | if (error) |
| 127 | return error; |
| 128 | |
| 129 | /* |
| 130 | * We can add the action here since it makes the code easier and nothing |
| 131 | * "bad" will happen out of it. Worst case, it will be a no-op and no |
| 132 | * bit will set. |
| 133 | */ |
| 134 | error = devm_add_action_or_reset(dev, adp5585_keys_pins_free, kpad); |
| 135 | if (error) |
| 136 | return error; |
| 137 | |
| 138 | for (pin = 0; pin < n_pins; pin++) { |
| 139 | if (keypad_pins[pin] >= adp5585->n_pins) |
| 140 | return dev_err_probe(dev, err: -EINVAL, |
| 141 | fmt: "Invalid keypad pin(%u) defined\n" , |
| 142 | keypad_pins[pin]); |
| 143 | |
| 144 | if (test_and_set_bit(nr: keypad_pins[pin], addr: adp5585->pin_usage)) |
| 145 | return dev_err_probe(dev, err: -EBUSY, |
| 146 | fmt: "Keypad pin(%u) already used\n" , |
| 147 | keypad_pins[pin]); |
| 148 | |
| 149 | __set_bit(keypad_pins[pin], &kpad->keypad); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Note that given that we get a mask (and the HW allows it), we |
| 154 | * can have holes in our keypad (eg: row0, row1 and row7 enabled). |
| 155 | * However, for the matrix parsing functions we need to pass the |
| 156 | * number of rows/cols as the maximum row/col used plus 1. This |
| 157 | * pretty much means we will also have holes in our SW keypad. |
| 158 | */ |
| 159 | |
| 160 | rows = find_last_bit(addr: &kpad->keypad, size: kpad->info->max_rows) + 1; |
| 161 | if (rows == kpad->info->max_rows + 1) |
| 162 | return dev_err_probe(dev, err: -EINVAL, |
| 163 | fmt: "Now rows defined in the keypad!\n" ); |
| 164 | |
| 165 | cols = find_last_bit(addr: &kpad->keypad, size: kpad->info->max_cols + kpad->info->max_rows); |
| 166 | if (cols < kpad->info->max_rows) |
| 167 | return dev_err_probe(dev, err: -EINVAL, |
| 168 | fmt: "No columns defined in the keypad!\n" ); |
| 169 | |
| 170 | cols = cols + 1 - kpad->info->max_rows; |
| 171 | |
| 172 | error = matrix_keypad_build_keymap(NULL, NULL, rows, cols, |
| 173 | keymap: kpad->keycode, input_dev: kpad->input); |
| 174 | if (error) |
| 175 | return error; |
| 176 | |
| 177 | kpad->row_shift = get_count_order(count: cols); |
| 178 | |
| 179 | if (device_property_read_bool(dev: kpad->dev, propname: "autorepeat" )) |
| 180 | __set_bit(EV_REP, kpad->input->evbit); |
| 181 | |
| 182 | error = adp5585_keys_check_special_events(adp5585, kpad); |
| 183 | if (error) |
| 184 | return error; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int adp5585_keys_setup(const struct adp5585_dev *adp5585, |
| 190 | struct adp5585_kpad *kpad) |
| 191 | { |
| 192 | unsigned long keys_bits, start = 0, nbits = kpad->info->max_rows; |
| 193 | const struct adp5585_regs *regs = adp5585->regs; |
| 194 | unsigned int i = 0, max_cols = kpad->info->max_cols; |
| 195 | int error; |
| 196 | |
| 197 | /* |
| 198 | * Take care as the below assumes max_rows is always less or equal than |
| 199 | * 8 which is true for the supported devices. If we happen to add |
| 200 | * another device we need to make sure this still holds true. Although |
| 201 | * adding a new device is very unlikely. |
| 202 | */ |
| 203 | do { |
| 204 | keys_bits = bitmap_read(map: &kpad->keypad, start, nbits); |
| 205 | if (keys_bits) { |
| 206 | error = regmap_write(map: adp5585->regmap, reg: regs->pin_cfg_a + i, |
| 207 | val: keys_bits); |
| 208 | if (error) |
| 209 | return error; |
| 210 | } |
| 211 | |
| 212 | start += nbits; |
| 213 | if (max_cols > 8) { |
| 214 | nbits = 8; |
| 215 | max_cols -= nbits; |
| 216 | } else { |
| 217 | nbits = max_cols; |
| 218 | } |
| 219 | |
| 220 | i++; |
| 221 | } while (start < kpad->info->max_rows + kpad->info->max_cols); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int adp5585_keys_ev_handle(struct notifier_block *nb, unsigned long key, |
| 227 | void *data) |
| 228 | { |
| 229 | struct adp5585_kpad *kpad = container_of(nb, struct adp5585_kpad, nb); |
| 230 | unsigned long key_press = (unsigned long)data; |
| 231 | unsigned int row, col, code; |
| 232 | |
| 233 | /* make sure the event is for us */ |
| 234 | if (key < kpad->info->key_ev_min || key > kpad->info->key_ev_max) |
| 235 | return NOTIFY_DONE; |
| 236 | |
| 237 | /* |
| 238 | * Unlikely but lets be on the safe side! We do not return any error |
| 239 | * because the event was indeed for us but with some weird value. So, |
| 240 | * we still want the caller know that the right handler was called. |
| 241 | */ |
| 242 | if (!key) |
| 243 | return NOTIFY_BAD; |
| 244 | |
| 245 | row = (key - 1) / (kpad->info->max_cols); |
| 246 | col = (key - 1) % (kpad->info->max_cols); |
| 247 | code = MATRIX_SCAN_CODE(row, col, kpad->row_shift); |
| 248 | |
| 249 | dev_dbg_ratelimited(kpad->dev, "report key(%lu) r(%d) c(%d) code(%d)\n" , |
| 250 | key, row, col, kpad->keycode[code]); |
| 251 | |
| 252 | input_report_key(dev: kpad->input, code: kpad->keycode[code], value: key_press); |
| 253 | input_sync(dev: kpad->input); |
| 254 | |
| 255 | return NOTIFY_STOP; |
| 256 | } |
| 257 | |
| 258 | static void adp5585_keys_unreg_notifier(void *data) |
| 259 | { |
| 260 | struct adp5585_kpad *kpad = data; |
| 261 | struct adp5585_dev *adp5585 = dev_get_drvdata(dev: kpad->dev->parent); |
| 262 | |
| 263 | blocking_notifier_chain_unregister(nh: &adp5585->event_notifier, |
| 264 | nb: &kpad->nb); |
| 265 | } |
| 266 | |
| 267 | static int adp5585_keys_probe(struct platform_device *pdev) |
| 268 | { |
| 269 | const struct platform_device_id *id = platform_get_device_id(pdev); |
| 270 | struct adp5585_dev *adp5585 = dev_get_drvdata(dev: pdev->dev.parent); |
| 271 | struct device *dev = &pdev->dev; |
| 272 | struct adp5585_kpad *kpad; |
| 273 | unsigned int revid; |
| 274 | const char *phys; |
| 275 | int error; |
| 276 | |
| 277 | kpad = devm_kzalloc(dev, size: sizeof(*kpad), GFP_KERNEL); |
| 278 | if (!kpad) |
| 279 | return -ENOMEM; |
| 280 | |
| 281 | if (!adp5585->irq) |
| 282 | return dev_err_probe(dev, err: -EINVAL, |
| 283 | fmt: "IRQ is mandatory for the keypad\n" ); |
| 284 | |
| 285 | kpad->dev = dev; |
| 286 | |
| 287 | kpad->input = devm_input_allocate_device(dev); |
| 288 | if (!kpad->input) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | kpad->info = (const struct adp5585_kpad_chip *)id->driver_data; |
| 292 | if (!kpad->info) |
| 293 | return -ENODEV; |
| 294 | |
| 295 | error = regmap_read(map: adp5585->regmap, ADP5585_ID, val: &revid); |
| 296 | if (error) |
| 297 | return dev_err_probe(dev, err: error, fmt: "Failed to read device ID\n" ); |
| 298 | |
| 299 | phys = devm_kasprintf(dev, GFP_KERNEL, fmt: "%s/input0" , pdev->name); |
| 300 | if (!phys) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | kpad->input->name = pdev->name; |
| 304 | kpad->input->phys = phys; |
| 305 | |
| 306 | kpad->input->id.bustype = BUS_I2C; |
| 307 | kpad->input->id.vendor = 0x0001; |
| 308 | kpad->input->id.product = 0x0001; |
| 309 | kpad->input->id.version = revid & ADP5585_REV_ID_MASK; |
| 310 | |
| 311 | device_set_of_node_from_dev(dev, dev2: dev->parent); |
| 312 | |
| 313 | error = adp5585_keys_parse_fw(adp5585, kpad); |
| 314 | if (error) |
| 315 | return error; |
| 316 | |
| 317 | error = adp5585_keys_setup(adp5585, kpad); |
| 318 | if (error) |
| 319 | return error; |
| 320 | |
| 321 | kpad->nb.notifier_call = adp5585_keys_ev_handle; |
| 322 | error = blocking_notifier_chain_register(nh: &adp5585->event_notifier, |
| 323 | nb: &kpad->nb); |
| 324 | if (error) |
| 325 | return error; |
| 326 | |
| 327 | error = devm_add_action_or_reset(dev, adp5585_keys_unreg_notifier, kpad); |
| 328 | if (error) |
| 329 | return error; |
| 330 | |
| 331 | error = input_register_device(kpad->input); |
| 332 | if (error) |
| 333 | return dev_err_probe(dev, err: error, |
| 334 | fmt: "Failed to register input device\n" ); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static const struct adp5585_kpad_chip adp5585_kpad_chip_info = { |
| 340 | .max_rows = 6, |
| 341 | .max_cols = 5, |
| 342 | .key_ev_min = ADP5585_ROW5_KEY_EVENT_START, |
| 343 | .key_ev_max = ADP5585_ROW5_KEY_EVENT_END, |
| 344 | }; |
| 345 | |
| 346 | static const struct adp5585_kpad_chip adp5589_kpad_chip_info = { |
| 347 | .max_rows = 8, |
| 348 | .max_cols = 11, |
| 349 | .key_ev_min = ADP5589_KEY_EVENT_START, |
| 350 | .key_ev_max = ADP5589_KEY_EVENT_END, |
| 351 | }; |
| 352 | |
| 353 | static const struct platform_device_id adp5585_keys_id_table[] = { |
| 354 | { "adp5585-keys" , (kernel_ulong_t)&adp5585_kpad_chip_info }, |
| 355 | { "adp5589-keys" , (kernel_ulong_t)&adp5589_kpad_chip_info }, |
| 356 | { } |
| 357 | }; |
| 358 | MODULE_DEVICE_TABLE(platform, adp5585_keys_id_table); |
| 359 | |
| 360 | static struct platform_driver adp5585_keys_driver = { |
| 361 | .driver = { |
| 362 | .name = "adp5585-keys" , |
| 363 | }, |
| 364 | .probe = adp5585_keys_probe, |
| 365 | .id_table = adp5585_keys_id_table, |
| 366 | }; |
| 367 | module_platform_driver(adp5585_keys_driver); |
| 368 | |
| 369 | MODULE_AUTHOR("Nuno Sá <nuno.sa@analog.com>" ); |
| 370 | MODULE_DESCRIPTION("ADP5585 Keys Driver" ); |
| 371 | MODULE_LICENSE("GPL" ); |
| 372 | |