Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 1 | #ifndef _MATRIX_KEYPAD_H |
| 2 | #define _MATRIX_KEYPAD_H |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/input.h> |
Olof Johansson | 2cd3687 | 2012-03-13 21:35:51 -0700 | [diff] [blame] | 6 | #include <linux/of.h> |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 7 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 8 | #define MATRIX_MAX_ROWS 18 |
| 9 | #define MATRIX_MAX_COLS 18 |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 10 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 11 | #define KEY(row, col, val) ((((row) % (MATRIX_MAX_ROWS)) << 24) |\ |
| 12 | (((col) % (MATRIX_MAX_COLS)) << 16) |\ |
| 13 | (val & 0xffff)) |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 14 | |
| 15 | #define KEY_ROW(k) (((k) >> 24) & 0xff) |
| 16 | #define KEY_COL(k) (((k) >> 16) & 0xff) |
| 17 | #define KEY_VAL(k) ((k) & 0xffff) |
| 18 | |
Eric Miao | d82f1c3 | 2009-08-05 01:24:41 -0700 | [diff] [blame] | 19 | #define MATRIX_SCAN_CODE(row, col, row_shift) (((row) << (row_shift)) + (col)) |
| 20 | |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 21 | /** |
| 22 | * struct matrix_keymap_data - keymap for matrix keyboards |
| 23 | * @keymap: pointer to array of uint32 values encoded with KEY() macro |
| 24 | * representing keymap |
| 25 | * @keymap_size: number of entries (initialized) in this keymap |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 26 | * |
| 27 | * This structure is supposed to be used by platform code to supply |
| 28 | * keymaps to drivers that implement matrix-like keypads/keyboards. |
| 29 | */ |
| 30 | struct matrix_keymap_data { |
| 31 | const uint32_t *keymap; |
| 32 | unsigned int keymap_size; |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * struct matrix_keypad_platform_data - platform-dependent keypad data |
| 37 | * @keymap_data: pointer to &matrix_keymap_data |
Eric Miao | d82f1c3 | 2009-08-05 01:24:41 -0700 | [diff] [blame] | 38 | * @row_gpios: pointer to array of gpio numbers representing rows |
| 39 | * @col_gpios: pointer to array of gpio numbers reporesenting colums |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 40 | * @num_row_gpios: actual number of row gpios used by device |
| 41 | * @num_col_gpios: actual number of col gpios used by device |
| 42 | * @col_scan_delay_us: delay, measured in microseconds, that is |
| 43 | * needed before we can keypad after activating column gpio |
| 44 | * @debounce_ms: debounce interval in milliseconds |
Luotao Fu | fb76dd1 | 2010-06-10 12:05:23 -0700 | [diff] [blame] | 45 | * @clustered_irq: may be specified if interrupts of all row/column GPIOs |
| 46 | * are bundled to one single irq |
| 47 | * @clustered_irq_flags: flags that are needed for the clustered irq |
Dmitry Torokhov | d69249f | 2009-11-16 22:12:20 -0800 | [diff] [blame] | 48 | * @active_low: gpio polarity |
| 49 | * @wakeup: controls whether the device should be set up as wakeup |
| 50 | * source |
H Hartley Sweeten | 9d32c30 | 2010-04-05 22:29:09 -0700 | [diff] [blame] | 51 | * @no_autorepeat: disable key autorepeat |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 52 | * |
| 53 | * This structure represents platform-specific data that use used by |
| 54 | * matrix_keypad driver to perform proper initialization. |
| 55 | */ |
| 56 | struct matrix_keypad_platform_data { |
| 57 | const struct matrix_keymap_data *keymap_data; |
| 58 | |
Eric Miao | d82f1c3 | 2009-08-05 01:24:41 -0700 | [diff] [blame] | 59 | const unsigned int *row_gpios; |
| 60 | const unsigned int *col_gpios; |
| 61 | |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 62 | unsigned int num_row_gpios; |
| 63 | unsigned int num_col_gpios; |
| 64 | |
| 65 | unsigned int col_scan_delay_us; |
| 66 | |
| 67 | /* key debounce interval in milli-second */ |
| 68 | unsigned int debounce_ms; |
| 69 | |
Luotao Fu | fb76dd1 | 2010-06-10 12:05:23 -0700 | [diff] [blame] | 70 | unsigned int clustered_irq; |
| 71 | unsigned int clustered_irq_flags; |
| 72 | |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 73 | bool active_low; |
| 74 | bool wakeup; |
H Hartley Sweeten | 9d32c30 | 2010-04-05 22:29:09 -0700 | [diff] [blame] | 75 | bool no_autorepeat; |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Dmitry Torokhov | 77a53fd | 2009-08-25 19:24:13 -0700 | [diff] [blame] | 78 | /** |
| 79 | * matrix_keypad_build_keymap - convert platform keymap into matrix keymap |
| 80 | * @keymap_data: keymap supplied by the platform code |
| 81 | * @row_shift: number of bits to shift row value by to advance to the next |
| 82 | * line in the keymap |
| 83 | * @keymap: expanded version of keymap that is suitable for use by |
| 84 | * matrix keyboad driver |
| 85 | * @keybit: pointer to bitmap of keys supported by input device |
| 86 | * |
| 87 | * This function converts platform keymap (encoded with KEY() macro) into |
| 88 | * an array of keycodes that is suitable for using in a standard matrix |
| 89 | * keyboard driver that uses row and col as indices. |
| 90 | */ |
| 91 | static inline void |
| 92 | matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data, |
| 93 | unsigned int row_shift, |
| 94 | unsigned short *keymap, unsigned long *keybit) |
| 95 | { |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < keymap_data->keymap_size; i++) { |
| 99 | unsigned int key = keymap_data->keymap[i]; |
| 100 | unsigned int row = KEY_ROW(key); |
| 101 | unsigned int col = KEY_COL(key); |
| 102 | unsigned short code = KEY_VAL(key); |
| 103 | |
| 104 | keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code; |
| 105 | __set_bit(code, keybit); |
| 106 | } |
| 107 | __clear_bit(KEY_RESERVED, keybit); |
| 108 | } |
| 109 | |
Olof Johansson | 2cd3687 | 2012-03-13 21:35:51 -0700 | [diff] [blame] | 110 | #ifdef CONFIG_INPUT_OF_MATRIX_KEYMAP |
| 111 | struct matrix_keymap_data * |
| 112 | matrix_keyboard_of_fill_keymap(struct device_node *np, const char *propname); |
| 113 | |
| 114 | void matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd); |
| 115 | #else |
| 116 | static inline struct matrix_keymap_data * |
| 117 | matrix_keyboard_of_fill_keymap(struct device_node *np, const char *propname) |
| 118 | { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | static inline void |
| 123 | matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd) |
| 124 | { |
| 125 | } |
| 126 | #endif |
| 127 | |
Eric Miao | bab7614 | 2009-06-29 00:20:52 -0700 | [diff] [blame] | 128 | #endif /* _MATRIX_KEYPAD_H */ |