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