xref: /openbmc/linux/drivers/input/matrix-keymap.c (revision 4a075bd4)
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 #include <linux/device.h>
20 #include <linux/export.h>
21 #include <linux/gfp.h>
22 #include <linux/input.h>
23 #include <linux/input/matrix_keypad.h>
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>
29 
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 {
34 	unsigned short *keymap = input_dev->keycode;
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 /**
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)
62 {
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);
67 
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 }
75 EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties);
76 
77 static int matrix_keypad_parse_keymap(const char *propname,
78 				      unsigned int rows, unsigned int cols,
79 				      struct input_dev *input_dev)
80 {
81 	struct device *dev = input_dev->dev.parent;
82 	unsigned int row_shift = get_count_order(cols);
83 	unsigned int max_keys = rows << row_shift;
84 	u32 *keys;
85 	int i;
86 	int size;
87 	int retval;
88 
89 	if (!propname)
90 		propname = "linux,keymap";
91 
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;
97 	}
98 
99 	if (size > max_keys) {
100 		dev_err(dev, "%s size overflow (%d vs max %u)\n",
101 			propname, size, max_keys);
102 		return -EINVAL;
103 	}
104 
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 
116 	for (i = 0; i < size; i++) {
117 		if (!matrix_keypad_map_key(input_dev, rows, cols,
118 					   row_shift, keys[i])) {
119 			retval = -EINVAL;
120 			goto out;
121 		}
122 	}
123 
124 	retval = 0;
125 
126 out:
127 	kfree(keys);
128 	return retval;
129 }
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.
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  *
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  *
155  * Callers are expected to set up input_dev->dev.parent before calling this
156  * function.
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);
165 	size_t max_keys = rows << row_shift;
166 	int i;
167 	int error;
168 
169 	if (WARN_ON(!input_dev->dev.parent))
170 		return -EINVAL;
171 
172 	if (!keymap) {
173 		keymap = devm_kcalloc(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 
183 	input_dev->keycode = keymap;
184 	input_dev->keycodesize = sizeof(*keymap);
185 	input_dev->keycodemax = max_keys;
186 
187 	__set_bit(EV_KEY, input_dev->evbit);
188 
189 	if (keymap_data) {
190 		for (i = 0; i < keymap_data->keymap_size; i++) {
191 			unsigned int key = keymap_data->keymap[i];
192 
193 			if (!matrix_keypad_map_key(input_dev, rows, cols,
194 						   row_shift, key))
195 				return -EINVAL;
196 		}
197 	} else {
198 		error = matrix_keypad_parse_keymap(keymap_name, rows, cols,
199 						   input_dev);
200 		if (error)
201 			return error;
202 	}
203 
204 	__clear_bit(KEY_RESERVED, input_dev->keybit);
205 
206 	return 0;
207 }
208 EXPORT_SYMBOL(matrix_keypad_build_keymap);
209 
210 MODULE_LICENSE("GPL");
211