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. |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 19 | #include <linux/device.h> |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 20 | #include <linux/export.h> |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 21 | #include <linux/gfp.h> |
| 22 | #include <linux/input.h> |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 23 | #include <linux/input/matrix_keypad.h> |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/property.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/types.h> |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 29 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 30 | static bool matrix_keypad_map_key(struct input_dev *input_dev, |
| 31 | unsigned int rows, unsigned int cols, |
| 32 | unsigned int row_shift, unsigned int key) |
| 33 | { |
Dmitry Torokhov | 8680917 | 2012-05-24 01:10:20 -0700 | [diff] [blame] | 34 | unsigned short *keymap = input_dev->keycode; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 35 | unsigned int row = KEY_ROW(key); |
| 36 | unsigned int col = KEY_COL(key); |
| 37 | unsigned short code = KEY_VAL(key); |
| 38 | |
| 39 | if (row >= rows || col >= cols) { |
| 40 | dev_err(input_dev->dev.parent, |
| 41 | "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n", |
| 42 | __func__, key, row, col, rows, cols); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code; |
| 47 | __set_bit(code, input_dev->keybit); |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 52 | /** |
| 53 | * matrix_keypad_parse_properties() - Read properties of matrix keypad |
| 54 | * |
| 55 | * @dev: Device containing properties |
| 56 | * @rows: Returns number of matrix rows |
| 57 | * @cols: Returns number of matrix columns |
| 58 | * @return 0 if OK, <0 on error |
| 59 | */ |
| 60 | int matrix_keypad_parse_properties(struct device *dev, |
| 61 | unsigned int *rows, unsigned int *cols) |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 62 | { |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 63 | *rows = *cols = 0; |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 64 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 65 | device_property_read_u32(dev, "keypad,num-rows", rows); |
| 66 | device_property_read_u32(dev, "keypad,num-columns", cols); |
| 67 | |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 68 | if (!*rows || !*cols) { |
| 69 | dev_err(dev, "number of keypad rows/columns not specified\n"); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 75 | EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties); |
Simon Glass | 4384041 | 2013-02-25 14:08:40 -0800 | [diff] [blame] | 76 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 77 | static int matrix_keypad_parse_keymap(const char *propname, |
| 78 | unsigned int rows, unsigned int cols, |
| 79 | struct input_dev *input_dev) |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 80 | { |
| 81 | struct device *dev = input_dev->dev.parent; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 82 | unsigned int row_shift = get_count_order(cols); |
| 83 | unsigned int max_keys = rows << row_shift; |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 84 | u32 *keys; |
| 85 | int i; |
| 86 | int size; |
| 87 | int retval; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 88 | |
| 89 | if (!propname) |
| 90 | propname = "linux,keymap"; |
| 91 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 92 | size = device_property_read_u32_array(dev, propname, NULL, 0); |
| 93 | if (size <= 0) { |
| 94 | dev_err(dev, "missing or malformed property %s: %d\n", |
| 95 | propname, size); |
| 96 | return size < 0 ? size : -EINVAL; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 99 | if (size > max_keys) { |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 100 | dev_err(dev, "%s size overflow (%d vs max %u)\n", |
| 101 | propname, size, max_keys); |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 102 | return -EINVAL; |
| 103 | } |
| 104 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 105 | keys = kmalloc_array(size, sizeof(u32), GFP_KERNEL); |
| 106 | if (!keys) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | retval = device_property_read_u32_array(dev, propname, keys, size); |
| 110 | if (retval) { |
| 111 | dev_err(dev, "failed to read %s property: %d\n", |
| 112 | propname, retval); |
| 113 | goto out; |
| 114 | } |
| 115 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 116 | for (i = 0; i < size; i++) { |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 117 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 118 | row_shift, keys[i])) { |
| 119 | retval = -EINVAL; |
| 120 | goto out; |
| 121 | } |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 124 | retval = 0; |
| 125 | |
| 126 | out: |
| 127 | kfree(keys); |
| 128 | return retval; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 129 | } |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * matrix_keypad_build_keymap - convert platform keymap into matrix keymap |
| 133 | * @keymap_data: keymap supplied by the platform code |
| 134 | * @keymap_name: name of device tree property containing keymap (if device |
| 135 | * tree support is enabled). |
| 136 | * @rows: number of rows in target keymap array |
| 137 | * @cols: number of cols in target keymap array |
| 138 | * @keymap: expanded version of keymap that is suitable for use by |
| 139 | * matrix keyboard driver |
| 140 | * @input_dev: input devices for which we are setting up the keymap |
| 141 | * |
| 142 | * This function converts platform keymap (encoded with KEY() macro) into |
| 143 | * an array of keycodes that is suitable for using in a standard matrix |
| 144 | * keyboard driver that uses row and col as indices. |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 145 | * |
| 146 | * If @keymap_data is not supplied and device tree support is enabled |
| 147 | * it will attempt load the keymap from property specified by @keymap_name |
| 148 | * argument (or "linux,keymap" if @keymap_name is %NULL). |
| 149 | * |
Dmitry Torokhov | 5383116 | 2012-11-05 10:32:55 -0800 | [diff] [blame] | 150 | * If @keymap is %NULL the function will automatically allocate managed |
| 151 | * block of memory to store the keymap. This memory will be associated with |
| 152 | * the parent device and automatically freed when device unbinds from the |
| 153 | * driver. |
| 154 | * |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 155 | * Callers are expected to set up input_dev->dev.parent before calling this |
| 156 | * function. |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 157 | */ |
| 158 | int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data, |
| 159 | const char *keymap_name, |
| 160 | unsigned int rows, unsigned int cols, |
| 161 | unsigned short *keymap, |
| 162 | struct input_dev *input_dev) |
| 163 | { |
| 164 | unsigned int row_shift = get_count_order(cols); |
Dmitry Torokhov | 5383116 | 2012-11-05 10:32:55 -0800 | [diff] [blame] | 165 | size_t max_keys = rows << row_shift; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 166 | int i; |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 167 | int error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 168 | |
Dmitry Torokhov | 5383116 | 2012-11-05 10:32:55 -0800 | [diff] [blame] | 169 | if (WARN_ON(!input_dev->dev.parent)) |
| 170 | return -EINVAL; |
| 171 | |
| 172 | if (!keymap) { |
| 173 | keymap = devm_kzalloc(input_dev->dev.parent, |
| 174 | max_keys * sizeof(*keymap), |
| 175 | GFP_KERNEL); |
| 176 | if (!keymap) { |
| 177 | dev_err(input_dev->dev.parent, |
| 178 | "Unable to allocate memory for keymap"); |
| 179 | return -ENOMEM; |
| 180 | } |
| 181 | } |
| 182 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 183 | input_dev->keycode = keymap; |
| 184 | input_dev->keycodesize = sizeof(*keymap); |
Dmitry Torokhov | 5383116 | 2012-11-05 10:32:55 -0800 | [diff] [blame] | 185 | input_dev->keycodemax = max_keys; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 186 | |
| 187 | __set_bit(EV_KEY, input_dev->evbit); |
| 188 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 189 | if (keymap_data) { |
| 190 | for (i = 0; i < keymap_data->keymap_size; i++) { |
| 191 | unsigned int key = keymap_data->keymap[i]; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 192 | |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 193 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
| 194 | row_shift, key)) |
| 195 | return -EINVAL; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 196 | } |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 197 | } else { |
Dmitry Torokhov | aef01aa | 2016-11-11 12:43:12 -0800 | [diff] [blame] | 198 | error = matrix_keypad_parse_keymap(keymap_name, rows, cols, |
| 199 | input_dev); |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 200 | if (error) |
| 201 | return error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 202 | } |
Dmitry Torokhov | b45c8f3 | 2012-05-10 22:37:15 -0700 | [diff] [blame] | 203 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 204 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | EXPORT_SYMBOL(matrix_keypad_build_keymap); |
Florian Fainelli | 516d798 | 2012-12-10 12:18:20 -0800 | [diff] [blame] | 209 | |
| 210 | MODULE_LICENSE("GPL"); |