2 * Helpers for matrix keyboard bindings
4 * Copyright (C) 2012 Google, Inc
7 * Olof Johansson <olof@lixom.net>
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.
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.
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>
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
)
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
);
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
);
46 keymap
[MATRIX_SCAN_CODE(row
, col
, row_shift
)] = code
;
47 __set_bit(code
, input_dev
->keybit
);
53 * matrix_keypad_parse_properties() - Read properties of matrix keypad
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
60 int matrix_keypad_parse_properties(struct device
*dev
,
61 unsigned int *rows
, unsigned int *cols
)
65 device_property_read_u32(dev
, "keypad,num-rows", rows
);
66 device_property_read_u32(dev
, "keypad,num-columns", cols
);
68 if (!*rows
|| !*cols
) {
69 dev_err(dev
, "number of keypad rows/columns not specified\n");
75 EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties
);
77 static int matrix_keypad_parse_keymap(const char *propname
,
78 unsigned int rows
, unsigned int cols
,
79 struct input_dev
*input_dev
)
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
;
90 propname
= "linux,keymap";
92 size
= device_property_read_u32_array(dev
, propname
, NULL
, 0);
94 dev_err(dev
, "missing or malformed property %s: %d\n",
96 return size
< 0 ? size
: -EINVAL
;
99 if (size
> max_keys
) {
100 dev_err(dev
, "%s size overflow (%d vs max %u)\n",
101 propname
, size
, max_keys
);
105 keys
= kmalloc_array(size
, sizeof(u32
), GFP_KERNEL
);
109 retval
= device_property_read_u32_array(dev
, propname
, keys
, size
);
111 dev_err(dev
, "failed to read %s property: %d\n",
116 for (i
= 0; i
< size
; i
++) {
117 if (!matrix_keypad_map_key(input_dev
, rows
, cols
,
118 row_shift
, keys
[i
])) {
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
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.
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).
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
155 * Callers are expected to set up input_dev->dev.parent before calling this
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
)
164 unsigned int row_shift
= get_count_order(cols
);
165 size_t max_keys
= rows
<< row_shift
;
169 if (WARN_ON(!input_dev
->dev
.parent
))
173 keymap
= devm_kzalloc(input_dev
->dev
.parent
,
174 max_keys
* sizeof(*keymap
),
177 dev_err(input_dev
->dev
.parent
,
178 "Unable to allocate memory for keymap");
183 input_dev
->keycode
= keymap
;
184 input_dev
->keycodesize
= sizeof(*keymap
);
185 input_dev
->keycodemax
= max_keys
;
187 __set_bit(EV_KEY
, input_dev
->evbit
);
190 for (i
= 0; i
< keymap_data
->keymap_size
; i
++) {
191 unsigned int key
= keymap_data
->keymap
[i
];
193 if (!matrix_keypad_map_key(input_dev
, rows
, cols
,
198 error
= matrix_keypad_parse_keymap(keymap_name
, rows
, cols
,
204 __clear_bit(KEY_RESERVED
, input_dev
->keybit
);
208 EXPORT_SYMBOL(matrix_keypad_build_keymap
);
210 MODULE_LICENSE("GPL");