Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helpers for matrix keyboard bindings |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc |
| 5 | * |
| 6 | * Author: |
| 7 | * Olof Johansson <olof@lixom.net> |
| 8 | * |
| 9 | * This software is licensed under the terms of the GNU General Public |
| 10 | * License version 2, as published by the Free Software Foundation, and |
| 11 | * may be copied, distributed, and modified under those terms. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/input.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/export.h> |
Florian Fainelli | 55220bb | 2012-12-10 12:25:32 -0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 27 | #include <linux/input/matrix_keypad.h> |
| 28 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 29 | static bool matrix_keypad_map_key(struct input_dev *input_dev, |
| 30 | unsigned int rows, unsigned int cols, |
| 31 | unsigned int row_shift, unsigned int key) |
| 32 | { |
Dmitry Torokhov | 8680917 | 2012-05-24 01:10:20 -0700 | [diff] [blame] | 33 | unsigned short *keymap = input_dev->keycode; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 34 | unsigned int row = KEY_ROW(key); |
| 35 | unsigned int col = KEY_COL(key); |
| 36 | unsigned short code = KEY_VAL(key); |
| 37 | |
| 38 | if (row >= rows || col >= cols) { |
| 39 | dev_err(input_dev->dev.parent, |
| 40 | "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n", |
| 41 | __func__, key, row, col, rows, cols); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code; |
| 46 | __set_bit(code, input_dev->keybit); |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | #ifdef CONFIG_OF |
| 52 | static int matrix_keypad_parse_of_keymap(const char *propname, |
| 53 | unsigned int rows, unsigned int cols, |
| 54 | struct input_dev *input_dev) |
| 55 | { |
| 56 | struct device *dev = input_dev->dev.parent; |
| 57 | struct device_node *np = dev->of_node; |
| 58 | unsigned int row_shift = get_count_order(cols); |
| 59 | unsigned int max_keys = rows << row_shift; |
| 60 | unsigned int proplen, i, size; |
| 61 | const __be32 *prop; |
| 62 | |
| 63 | if (!np) |
| 64 | return -ENOENT; |
| 65 | |
| 66 | if (!propname) |
| 67 | propname = "linux,keymap"; |
| 68 | |
| 69 | prop = of_get_property(np, propname, &proplen); |
| 70 | if (!prop) { |
| 71 | dev_err(dev, "OF: %s property not defined in %s\n", |
| 72 | propname, np->full_name); |
| 73 | return -ENOENT; |
| 74 | } |
| 75 | |
| 76 | if (proplen % sizeof(u32)) { |
| 77 | dev_err(dev, "OF: Malformed keycode property %s in %s\n", |
| 78 | propname, np->full_name); |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | size = proplen / sizeof(u32); |
| 83 | if (size > max_keys) { |
| 84 | dev_err(dev, "OF: %s size overflow\n", propname); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | for (i = 0; i < size; i++) { |
| 89 | unsigned int key = be32_to_cpup(prop + i); |
| 90 | |
| 91 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
| 92 | row_shift, key)) |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | #else |
| 99 | static int matrix_keypad_parse_of_keymap(const char *propname, |
| 100 | unsigned int rows, unsigned int cols, |
| 101 | struct input_dev *input_dev) |
| 102 | { |
| 103 | return -ENOSYS; |
| 104 | } |
| 105 | #endif |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * matrix_keypad_build_keymap - convert platform keymap into matrix keymap |
| 109 | * @keymap_data: keymap supplied by the platform code |
| 110 | * @keymap_name: name of device tree property containing keymap (if device |
| 111 | * tree support is enabled). |
| 112 | * @rows: number of rows in target keymap array |
| 113 | * @cols: number of cols in target keymap array |
| 114 | * @keymap: expanded version of keymap that is suitable for use by |
| 115 | * matrix keyboard driver |
| 116 | * @input_dev: input devices for which we are setting up the keymap |
| 117 | * |
| 118 | * This function converts platform keymap (encoded with KEY() macro) into |
| 119 | * an array of keycodes that is suitable for using in a standard matrix |
| 120 | * keyboard driver that uses row and col as indices. |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 121 | * |
| 122 | * If @keymap_data is not supplied and device tree support is enabled |
| 123 | * it will attempt load the keymap from property specified by @keymap_name |
| 124 | * argument (or "linux,keymap" if @keymap_name is %NULL). |
| 125 | * |
| 126 | * Callers are expected to set up input_dev->dev.parent before calling this |
| 127 | * function. |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 128 | */ |
| 129 | int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data, |
| 130 | const char *keymap_name, |
| 131 | unsigned int rows, unsigned int cols, |
| 132 | unsigned short *keymap, |
| 133 | struct input_dev *input_dev) |
| 134 | { |
| 135 | unsigned int row_shift = get_count_order(cols); |
| 136 | int i; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 137 | int error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 138 | |
| 139 | input_dev->keycode = keymap; |
| 140 | input_dev->keycodesize = sizeof(*keymap); |
| 141 | input_dev->keycodemax = rows << row_shift; |
| 142 | |
| 143 | __set_bit(EV_KEY, input_dev->evbit); |
| 144 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 145 | if (keymap_data) { |
| 146 | for (i = 0; i < keymap_data->keymap_size; i++) { |
| 147 | unsigned int key = keymap_data->keymap[i]; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 148 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 149 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
| 150 | row_shift, key)) |
| 151 | return -EINVAL; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 152 | } |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 153 | } else { |
| 154 | error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols, |
| 155 | input_dev); |
| 156 | if (error) |
| 157 | return error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 158 | } |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 159 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 160 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | EXPORT_SYMBOL(matrix_keypad_build_keymap); |
Florian Fainelli | 55220bb | 2012-12-10 12:25:32 -0800 | [diff] [blame] | 165 | |
| 166 | MODULE_LICENSE("GPL"); |