| 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(kpad->dev, -EINVAL, |
| 66 |
"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, adp5585->unlock_keys, |
| 79 |
adp5585->nkeys_unlock); |
| 80 |
if (error) |
| 81 |
return error; |
| 82 |
|
| 83 |
error = adp5585_keys_validate_events(kpad, adp5585->reset1_keys, |
| 84 |
adp5585->nkeys_reset1); |
| 85 |
if (error) |
| 86 |
return error; |
| 87 |
|
| 88 |
return adp5585_keys_validate_events(kpad, adp5585->reset2_keys, |
| 89 |
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(kpad->dev->parent); |
| 96 |
unsigned int pin; |
| 97 |
|
| 98 |
for_each_set_bit(pin, &kpad->keypad, adp5585->n_pins) |
| 99 |
clear_bit(pin, 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, "adi,keypad-pins"); |
| 114 |
if (n_pins > adp5585->n_pins) |
| 115 |
return dev_err_probe(dev, -EINVAL, |
| 116 |
"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, "adi,keypad-pins", |
| 125 |
keypad_pins, 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, -EINVAL, |
| 141 |
"Invalid keypad pin(%u) defined\n", |
| 142 |
keypad_pins[pin]); |
| 143 |
|
| 144 |
if (test_and_set_bit(keypad_pins[pin], adp5585->pin_usage)) |
| 145 |
return dev_err_probe(dev, -EBUSY, |
| 146 |
"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(&kpad->keypad, kpad->info->max_rows) + 1; |
| 161 |
if (rows == kpad->info->max_rows + 1) |
| 162 |
return dev_err_probe(dev, -EINVAL, |
| 163 |
"Now rows defined in the keypad!\n"); |
| 164 |
|
| 165 |
cols = find_last_bit(&kpad->keypad, kpad->info->max_cols + kpad->info->max_rows); |
| 166 |
if (cols < kpad->info->max_rows) |
| 167 |
return dev_err_probe(dev, -EINVAL, |
| 168 |
"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 |
kpad->keycode, kpad->input); |
| 174 |
if (error) |
| 175 |
return error; |
| 176 |
|
| 177 |
kpad->row_shift = get_count_order(cols); |
| 178 |
|
| 179 |
if (device_property_read_bool(kpad->dev, "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(&kpad->keypad, start, nbits); |
| 205 |
if (keys_bits) { |
| 206 |
error = regmap_write(adp5585->regmap, regs->pin_cfg_a + i, |
| 207 |
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(kpad->input, kpad->keycode[code], key_press); |
| 253 |
input_sync(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(kpad->dev->parent); |
| 262 |
|
| 263 |
blocking_notifier_chain_unregister(&adp5585->event_notifier, |
| 264 |
&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(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, sizeof(*kpad), GFP_KERNEL); |
| 278 |
if (!kpad) |
| 279 |
return -ENOMEM; |
| 280 |
|
| 281 |
if (!adp5585->irq) |
| 282 |
return dev_err_probe(dev, -EINVAL, |
| 283 |
"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(adp5585->regmap, ADP5585_ID, &revid); |
| 296 |
if (error) |
| 297 |
return dev_err_probe(dev, error, "Failed to read device ID\n"); |
| 298 |
|
| 299 |
phys = devm_kasprintf(dev, GFP_KERNEL, "%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, 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(&adp5585->event_notifier, |
| 323 |
&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, error, |
| 334 |
"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á <[email protected]>"); |
| 370 |
MODULE_DESCRIPTION("ADP5585 Keys Driver"); |
| 371 |
MODULE_LICENSE("GPL"); |