]>
Commit | Line | Data |
---|---|---|
2cd36877 | 1 | /* |
1932811f | 2 | * Helpers for matrix keyboard bindings |
2cd36877 OJ |
3 | * |
4 | * Copyright (C) 2012 Google, Inc | |
5 | * | |
6 | * Author: | |
7 | * Olof Johansson <[email protected]> | |
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. | |
2cd36877 OJ |
17 | */ |
18 | ||
b45c8f35 | 19 | #include <linux/device.h> |
aef01aad | 20 | #include <linux/export.h> |
5383116b | 21 | #include <linux/gfp.h> |
2cd36877 | 22 | #include <linux/input.h> |
1932811f | 23 | #include <linux/input/matrix_keypad.h> |
aef01aad DT |
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> | |
1932811f | 29 | |
b45c8f35 DT |
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 | { | |
86809173 | 34 | unsigned short *keymap = input_dev->keycode; |
b45c8f35 DT |
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 | ||
aef01aad DT |
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) | |
43840415 | 62 | { |
aef01aad DT |
63 | *rows = *cols = 0; |
64 | ||
65 | device_property_read_u32(dev, "keypad,num-rows", rows); | |
66 | device_property_read_u32(dev, "keypad,num-columns", cols); | |
43840415 | 67 | |
43840415 SG |
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 | } | |
aef01aad | 75 | EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties); |
43840415 | 76 | |
aef01aad DT |
77 | static int matrix_keypad_parse_keymap(const char *propname, |
78 | unsigned int rows, unsigned int cols, | |
79 | struct input_dev *input_dev) | |
b45c8f35 DT |
80 | { |
81 | struct device *dev = input_dev->dev.parent; | |
b45c8f35 DT |
82 | unsigned int row_shift = get_count_order(cols); |
83 | unsigned int max_keys = rows << row_shift; | |
aef01aad DT |
84 | u32 *keys; |
85 | int i; | |
86 | int size; | |
87 | int retval; | |
b45c8f35 DT |
88 | |
89 | if (!propname) | |
90 | propname = "linux,keymap"; | |
91 | ||
aef01aad DT |
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; | |
b45c8f35 DT |
97 | } |
98 | ||
aef01aad DT |
99 | if (size > max_keys) { |
100 | dev_err(dev, "%s size overflow (%d vs max %u)\n", | |
101 | propname, size, max_keys); | |
b45c8f35 DT |
102 | return -EINVAL; |
103 | } | |
104 | ||
aef01aad DT |
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; | |
b45c8f35 DT |
114 | } |
115 | ||
116 | for (i = 0; i < size; i++) { | |
b45c8f35 | 117 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
aef01aad DT |
118 | row_shift, keys[i])) { |
119 | retval = -EINVAL; | |
120 | goto out; | |
121 | } | |
b45c8f35 DT |
122 | } |
123 | ||
aef01aad DT |
124 | retval = 0; |
125 | ||
126 | out: | |
127 | kfree(keys); | |
128 | return retval; | |
b45c8f35 | 129 | } |
1932811f DT |
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. | |
b45c8f35 DT |
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 | * | |
5383116b DT |
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 | * | |
b45c8f35 DT |
155 | * Callers are expected to set up input_dev->dev.parent before calling this |
156 | * function. | |
1932811f DT |
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); | |
5383116b | 165 | size_t max_keys = rows << row_shift; |
1932811f | 166 | int i; |
b45c8f35 | 167 | int error; |
1932811f | 168 | |
5383116b DT |
169 | if (WARN_ON(!input_dev->dev.parent)) |
170 | return -EINVAL; | |
171 | ||
172 | if (!keymap) { | |
a86854d0 KC |
173 | keymap = devm_kcalloc(input_dev->dev.parent, |
174 | max_keys, sizeof(*keymap), | |
5383116b DT |
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 | ||
1932811f DT |
183 | input_dev->keycode = keymap; |
184 | input_dev->keycodesize = sizeof(*keymap); | |
5383116b | 185 | input_dev->keycodemax = max_keys; |
1932811f DT |
186 | |
187 | __set_bit(EV_KEY, input_dev->evbit); | |
188 | ||
b45c8f35 DT |
189 | if (keymap_data) { |
190 | for (i = 0; i < keymap_data->keymap_size; i++) { | |
191 | unsigned int key = keymap_data->keymap[i]; | |
1932811f | 192 | |
b45c8f35 DT |
193 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
194 | row_shift, key)) | |
195 | return -EINVAL; | |
1932811f | 196 | } |
b45c8f35 | 197 | } else { |
aef01aad DT |
198 | error = matrix_keypad_parse_keymap(keymap_name, rows, cols, |
199 | input_dev); | |
b45c8f35 DT |
200 | if (error) |
201 | return error; | |
1932811f | 202 | } |
b45c8f35 | 203 | |
1932811f DT |
204 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
205 | ||
206 | return 0; | |
207 | } | |
208 | EXPORT_SYMBOL(matrix_keypad_build_keymap); | |
516d798f FF |
209 | |
210 | MODULE_LICENSE("GPL"); |