blob: 619b3824563c9ce4a7751c9a6a14930f2c1340a9 [file] [log] [blame]
Dmitry Torokhov19328112012-05-10 22:37:08 -07001/*
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
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070020#include <linux/device.h>
Dmitry Torokhov53831162012-11-05 10:32:55 -080021#include <linux/gfp.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/input.h>
25#include <linux/of.h>
26#include <linux/export.h>
Florian Fainelli516d7982012-12-10 12:18:20 -080027#include <linux/module.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070028#include <linux/input/matrix_keypad.h>
29
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070030static 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{
Dmitry Torokhov86809172012-05-24 01:10:20 -070034 unsigned short *keymap = input_dev->keycode;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070035 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
Simon Glass43840412013-02-25 14:08:40 -080053int 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}
71
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070072static int matrix_keypad_parse_of_keymap(const char *propname,
73 unsigned int rows, unsigned int cols,
74 struct input_dev *input_dev)
75{
76 struct device *dev = input_dev->dev.parent;
77 struct device_node *np = dev->of_node;
78 unsigned int row_shift = get_count_order(cols);
79 unsigned int max_keys = rows << row_shift;
80 unsigned int proplen, i, size;
81 const __be32 *prop;
82
83 if (!np)
84 return -ENOENT;
85
86 if (!propname)
87 propname = "linux,keymap";
88
89 prop = of_get_property(np, propname, &proplen);
90 if (!prop) {
91 dev_err(dev, "OF: %s property not defined in %s\n",
92 propname, np->full_name);
93 return -ENOENT;
94 }
95
96 if (proplen % sizeof(u32)) {
97 dev_err(dev, "OF: Malformed keycode property %s in %s\n",
98 propname, np->full_name);
99 return -EINVAL;
100 }
101
102 size = proplen / sizeof(u32);
103 if (size > max_keys) {
104 dev_err(dev, "OF: %s size overflow\n", propname);
105 return -EINVAL;
106 }
107
108 for (i = 0; i < size; i++) {
109 unsigned int key = be32_to_cpup(prop + i);
110
111 if (!matrix_keypad_map_key(input_dev, rows, cols,
112 row_shift, key))
113 return -EINVAL;
114 }
115
116 return 0;
117}
118#else
119static int matrix_keypad_parse_of_keymap(const char *propname,
120 unsigned int rows, unsigned int cols,
121 struct input_dev *input_dev)
122{
123 return -ENOSYS;
124}
125#endif
Dmitry Torokhov19328112012-05-10 22:37:08 -0700126
127/**
128 * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
129 * @keymap_data: keymap supplied by the platform code
130 * @keymap_name: name of device tree property containing keymap (if device
131 * tree support is enabled).
132 * @rows: number of rows in target keymap array
133 * @cols: number of cols in target keymap array
134 * @keymap: expanded version of keymap that is suitable for use by
135 * matrix keyboard driver
136 * @input_dev: input devices for which we are setting up the keymap
137 *
138 * This function converts platform keymap (encoded with KEY() macro) into
139 * an array of keycodes that is suitable for using in a standard matrix
140 * keyboard driver that uses row and col as indices.
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700141 *
142 * If @keymap_data is not supplied and device tree support is enabled
143 * it will attempt load the keymap from property specified by @keymap_name
144 * argument (or "linux,keymap" if @keymap_name is %NULL).
145 *
Dmitry Torokhov53831162012-11-05 10:32:55 -0800146 * If @keymap is %NULL the function will automatically allocate managed
147 * block of memory to store the keymap. This memory will be associated with
148 * the parent device and automatically freed when device unbinds from the
149 * driver.
150 *
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700151 * Callers are expected to set up input_dev->dev.parent before calling this
152 * function.
Dmitry Torokhov19328112012-05-10 22:37:08 -0700153 */
154int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
155 const char *keymap_name,
156 unsigned int rows, unsigned int cols,
157 unsigned short *keymap,
158 struct input_dev *input_dev)
159{
160 unsigned int row_shift = get_count_order(cols);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800161 size_t max_keys = rows << row_shift;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700162 int i;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700163 int error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700164
Dmitry Torokhov53831162012-11-05 10:32:55 -0800165 if (WARN_ON(!input_dev->dev.parent))
166 return -EINVAL;
167
168 if (!keymap) {
169 keymap = devm_kzalloc(input_dev->dev.parent,
170 max_keys * sizeof(*keymap),
171 GFP_KERNEL);
172 if (!keymap) {
173 dev_err(input_dev->dev.parent,
174 "Unable to allocate memory for keymap");
175 return -ENOMEM;
176 }
177 }
178
Dmitry Torokhov19328112012-05-10 22:37:08 -0700179 input_dev->keycode = keymap;
180 input_dev->keycodesize = sizeof(*keymap);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800181 input_dev->keycodemax = max_keys;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700182
183 __set_bit(EV_KEY, input_dev->evbit);
184
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700185 if (keymap_data) {
186 for (i = 0; i < keymap_data->keymap_size; i++) {
187 unsigned int key = keymap_data->keymap[i];
Dmitry Torokhov19328112012-05-10 22:37:08 -0700188
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700189 if (!matrix_keypad_map_key(input_dev, rows, cols,
190 row_shift, key))
191 return -EINVAL;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700192 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700193 } else {
194 error = matrix_keypad_parse_of_keymap(keymap_name, rows, cols,
195 input_dev);
196 if (error)
197 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700198 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700199
Dmitry Torokhov19328112012-05-10 22:37:08 -0700200 __clear_bit(KEY_RESERVED, input_dev->keybit);
201
202 return 0;
203}
204EXPORT_SYMBOL(matrix_keypad_build_keymap);
Florian Fainelli516d7982012-12-10 12:18:20 -0800205
206MODULE_LICENSE("GPL");