Skip to content

Commit d82f1c3

Browse files
ericmiaodtor
authored andcommitted
Input: matrix_keypad - make matrix keymap size dynamic
Remove assumption on the shift and size of rows/columns form matrix_keypad driver. Signed-off-by: Eric Miao <eric.y.miao@gmail.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 1949347 commit d82f1c3

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

‎drivers/input/keyboard/matrix_keypad.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct matrix_keypad {
2727
const struct matrix_keypad_platform_data *pdata;
2828
struct input_dev *input_dev;
2929
unsigned short *keycodes;
30+
unsigned int row_shift;
3031

3132
uint32_t last_key_state[MATRIX_MAX_COLS];
3233
struct delayed_work work;
@@ -136,7 +137,7 @@ static void matrix_keypad_scan(struct work_struct *work)
136137
if ((bits_changed & (1 << row)) == 0)
137138
continue;
138139

139-
code = (row << 4) + col;
140+
code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
140141
input_event(input_dev, EV_MSC, MSC_SCAN, code);
141142
input_report_key(input_dev,
142143
keypad->keycodes[code],
@@ -317,6 +318,7 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)
317318
struct matrix_keypad *keypad;
318319
struct input_dev *input_dev;
319320
unsigned short *keycodes;
321+
unsigned int row_shift;
320322
int i;
321323
int err;
322324

@@ -332,14 +334,11 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)
332334
return -EINVAL;
333335
}
334336

335-
if (!keymap_data->max_keymap_size) {
336-
dev_err(&pdev->dev, "invalid keymap data supplied\n");
337-
return -EINVAL;
338-
}
337+
row_shift = get_count_order(pdata->num_col_gpios);
339338

340339
keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
341-
keycodes = kzalloc(keymap_data->max_keymap_size *
342-
sizeof(keypad->keycodes),
340+
keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
341+
sizeof(*keycodes),
343342
GFP_KERNEL);
344343
input_dev = input_allocate_device();
345344
if (!keypad || !keycodes || !input_dev) {
@@ -350,6 +349,7 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)
350349
keypad->input_dev = input_dev;
351350
keypad->pdata = pdata;
352351
keypad->keycodes = keycodes;
352+
keypad->row_shift = row_shift;
353353
keypad->stopped = true;
354354
INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
355355
spin_lock_init(&keypad->lock);
@@ -363,15 +363,15 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)
363363

364364
input_dev->keycode = keycodes;
365365
input_dev->keycodesize = sizeof(*keycodes);
366-
input_dev->keycodemax = keymap_data->max_keymap_size;
366+
input_dev->keycodemax = pdata->num_row_gpios << keypad->row_shift;
367367

368368
for (i = 0; i < keymap_data->keymap_size; i++) {
369369
unsigned int key = keymap_data->keymap[i];
370370
unsigned int row = KEY_ROW(key);
371371
unsigned int col = KEY_COL(key);
372372
unsigned short code = KEY_VAL(key);
373373

374-
keycodes[(row << 4) + col] = code;
374+
keycodes[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
375375
__set_bit(code, input_dev->keybit);
376376
}
377377
__clear_bit(KEY_RESERVED, input_dev->keybit);

‎include/linux/input/matrix_keypad.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@
1515
#define KEY_COL(k) (((k) >> 16) & 0xff)
1616
#define KEY_VAL(k) ((k) & 0xffff)
1717

18+
#define MATRIX_SCAN_CODE(row, col, row_shift) (((row) << (row_shift)) + (col))
19+
1820
/**
1921
* struct matrix_keymap_data - keymap for matrix keyboards
2022
* @keymap: pointer to array of uint32 values encoded with KEY() macro
2123
* representing keymap
2224
* @keymap_size: number of entries (initialized) in this keymap
23-
* @max_keymap_size: maximum size of keymap supported by the device
2425
*
2526
* This structure is supposed to be used by platform code to supply
2627
* keymaps to drivers that implement matrix-like keypads/keyboards.
2728
*/
2829
struct matrix_keymap_data {
2930
const uint32_t *keymap;
3031
unsigned int keymap_size;
31-
unsigned int max_keymap_size;
3232
};
3333

3434
/**
3535
* struct matrix_keypad_platform_data - platform-dependent keypad data
3636
* @keymap_data: pointer to &matrix_keymap_data
37-
* @row_gpios: array of gpio numbers reporesenting rows
38-
* @col_gpios: array of gpio numbers reporesenting colums
37+
* @row_gpios: pointer to array of gpio numbers representing rows
38+
* @col_gpios: pointer to array of gpio numbers reporesenting colums
3939
* @num_row_gpios: actual number of row gpios used by device
4040
* @num_col_gpios: actual number of col gpios used by device
4141
* @col_scan_delay_us: delay, measured in microseconds, that is
@@ -48,8 +48,9 @@ struct matrix_keymap_data {
4848
struct matrix_keypad_platform_data {
4949
const struct matrix_keymap_data *keymap_data;
5050

51-
unsigned int row_gpios[MATRIX_MAX_ROWS];
52-
unsigned int col_gpios[MATRIX_MAX_COLS];
51+
const unsigned int *row_gpios;
52+
const unsigned int *col_gpios;
53+
5354
unsigned int num_row_gpios;
5455
unsigned int num_col_gpios;
5556

0 commit comments

Comments
 (0)