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 <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 | ||
b45c8f35 | 20 | #include <linux/device.h> |
5383116b | 21 | #include <linux/gfp.h> |
2cd36877 OJ |
22 | #include <linux/kernel.h> |
23 | #include <linux/types.h> | |
24 | #include <linux/input.h> | |
25 | #include <linux/of.h> | |
2cd36877 | 26 | #include <linux/export.h> |
516d798f | 27 | #include <linux/module.h> |
1932811f DT |
28 | #include <linux/input/matrix_keypad.h> |
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 | ||
52 | #ifdef CONFIG_OF | |
43840415 SG |
53 | int matrix_keypad_parse_of_params(struct device *dev, |
54 | unsigned int *rows, unsigned int *cols) | |
55 | { | |
56 | struct device_node *np = dev->of_node; | |
57 | ||
58 | if (!np) { | |
59 | dev_err(dev, "missing DT data"); | |
60 | return -EINVAL; | |
61 | } | |
62 | of_property_read_u32(np, "keypad,num-rows", rows); | |
63 | of_property_read_u32(np, "keypad,num-columns", cols); | |
64 | if (!*rows || !*cols) { | |
65 | dev_err(dev, "number of keypad rows/columns not specified\n"); | |
66 | return -EINVAL; | |
67 | } | |
68 | ||
69 | return 0; | |
70 | } | |
a79fc765 | 71 | EXPORT_SYMBOL_GPL(matrix_keypad_parse_of_params); |
43840415 | 72 | |
b45c8f35 DT |
73 | static int matrix_keypad_parse_of_keymap(const char *propname, |
74 | unsigned int rows, unsigned int cols, | |
75 | struct input_dev *input_dev) | |
76 | { | |
77 | struct device *dev = input_dev->dev.parent; | |
78 | struct device_node *np = dev->of_node; | |
79 | unsigned int row_shift = get_count_order(cols); | |
80 | unsigned int max_keys = rows << row_shift; | |
81 | unsigned int proplen, i, size; | |
82 | const __be32 *prop; | |
83 | ||
84 | if (!np) | |
85 | return -ENOENT; | |
86 | ||
87 | if (!propname) | |
88 | propname = "linux,keymap"; | |
89 | ||
90 | prop = of_get_property(np, propname, &proplen); | |
91 | if (!prop) { | |
92 | dev_err(dev, "OF: %s property not defined in %s\n", | |
93 | propname, np->full_name); | |
94 | return -ENOENT; | |
95 | } | |
96 | ||
97 | if (proplen % sizeof(u32)) { | |
98 | dev_err(dev, "OF: Malformed keycode property %s in %s\n", | |
99 | propname, np->full_name); | |
100 | return -EINVAL; | |
101 | } | |
102 | ||
103 | size = proplen / sizeof(u32); | |
104 | if (size > max_keys) { | |
105 | dev_err(dev, "OF: %s size overflow\n", propname); | |
106 | return -EINVAL; | |
107 | } | |
108 | ||
109 | for (i = 0; i < size; i++) { | |
110 | unsigned int key = be32_to_cpup(prop + i); | |
111 | ||
112 | if (!matrix_keypad_map_key(input_dev, rows, cols, | |
113 | row_shift, key)) | |
114 | return -EINVAL; | |
115 | } | |
116 | ||
117 | return 0; | |
118 | } | |
119 | #else | |
120 | static int matrix_keypad_parse_of_keymap(const char *propname, | |
121 | unsigned int rows, unsigned int cols, | |
122 | struct input_dev *input_dev) | |
123 | { | |
124 | return -ENOSYS; | |
125 | } | |
126 | #endif | |
1932811f DT |
127 | |
128 | /** | |
129 | * matrix_keypad_build_keymap - convert platform keymap into matrix keymap | |
130 | * @keymap_data: keymap supplied by the platform code | |
131 | * @keymap_name: name of device tree property containing keymap (if device | |
132 | * tree support is enabled). | |
133 | * @rows: number of rows in target keymap array | |
134 | * @cols: number of cols in target keymap array | |
135 | * @keymap: expanded version of keymap that is suitable for use by | |
136 | * matrix keyboard driver | |
137 | * @input_dev: input devices for which we are setting up the keymap | |
138 | * | |
139 | * This function converts platform keymap (encoded with KEY() macro) into | |
140 | * an array of keycodes that is suitable for using in a standard matrix | |
141 | * keyboard driver that uses row and col as indices. | |
b45c8f35 DT |
142 | * |
143 | * If @keymap_data is not supplied and device tree support is enabled | |
144 | * it will attempt load the keymap from property specified by @keymap_name | |
145 | * argument (or "linux,keymap" if @keymap_name is %NULL). | |
146 | * | |
5383116b DT |
147 | * If @keymap is %NULL the function will automatically allocate managed |
148 | * block of memory to store the keymap. This memory will be associated with | |
149 | * the parent device and automatically freed when device unbinds from the | |
150 | * driver. | |
151 | * | |
b45c8f35 DT |
152 | * Callers are expected to set up input_dev->dev.parent before calling this |
153 | * function. | |
1932811f DT |
154 | */ |
155 | int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data, | |
156 | const char *keymap_name, | |
157 | unsigned int rows, unsigned int cols, | |
158 | unsigned short *keymap, | |
159 | struct input_dev *input_dev) | |
160 | { | |
161 | unsigned int row_shift = get_count_order(cols); | |
5383116b | 162 | size_t max_keys = rows << row_shift; |
1932811f | 163 | int i; |
b45c8f35 | 164 | int error; |
1932811f | 165 | |
5383116b DT |
166 | if (WARN_ON(!input_dev->dev.parent)) |
167 | return -EINVAL; | |
168 | ||
169 | if (!keymap) { | |
170 | keymap = devm_kzalloc(input_dev->dev.parent, | |
171 | max_keys * sizeof(*keymap), | |
172 | GFP_KERNEL); | |
173 | if (!keymap) { | |
174 | dev_err(input_dev->dev.parent, | |
175 | "Unable to allocate memory for keymap"); | |
176 | return -ENOMEM; | |
177 | } | |
178 | } | |
179 | ||
1932811f DT |
180 | input_dev->keycode = keymap; |
181 | input_dev->keycodesize = sizeof(*keymap); | |
5383116b | 182 | input_dev->keycodemax = max_keys; |
1932811f DT |
183 | |
184 | __set_bit(EV_KEY, input_dev->evbit); | |
185 | ||
b45c8f35 DT |
186 | if (keymap_data) { |
187 | for (i = 0; i < keymap_data->keymap_size; i++) { | |
188 | unsigned int key = keymap_data->keymap[i]; | |
1932811f | 189 | |
b45c8f35 DT |
190 | if (!matrix_keypad_map_key(input_dev, rows, cols, |
191 | row_shift, key)) | |
192 | return -EINVAL; | |
1932811f | 193 | } |
b45c8f35 DT |
194 | } else { |
195 | error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols, | |
196 | input_dev); | |
197 | if (error) | |
198 | return error; | |
1932811f | 199 | } |
b45c8f35 | 200 | |
1932811f DT |
201 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
202 | ||
203 | return 0; | |
204 | } | |
205 | EXPORT_SYMBOL(matrix_keypad_build_keymap); | |
516d798f FF |
206 | |
207 | MODULE_LICENSE("GPL"); |