blob: de7992d55da259264e696ee6346078bb6838a358 [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
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/input.h>
23#include <linux/of.h>
24#include <linux/export.h>
25#include <linux/gfp.h>
26#include <linux/slab.h>
27#include <linux/input/matrix_keypad.h>
28
29
30/**
31 * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
32 * @keymap_data: keymap supplied by the platform code
33 * @keymap_name: name of device tree property containing keymap (if device
34 * tree support is enabled).
35 * @rows: number of rows in target keymap array
36 * @cols: number of cols in target keymap array
37 * @keymap: expanded version of keymap that is suitable for use by
38 * matrix keyboard driver
39 * @input_dev: input devices for which we are setting up the keymap
40 *
41 * This function converts platform keymap (encoded with KEY() macro) into
42 * an array of keycodes that is suitable for using in a standard matrix
43 * keyboard driver that uses row and col as indices.
44 */
45int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
46 const char *keymap_name,
47 unsigned int rows, unsigned int cols,
48 unsigned short *keymap,
49 struct input_dev *input_dev)
50{
51 unsigned int row_shift = get_count_order(cols);
52 int i;
53
54 input_dev->keycode = keymap;
55 input_dev->keycodesize = sizeof(*keymap);
56 input_dev->keycodemax = rows << row_shift;
57
58 __set_bit(EV_KEY, input_dev->evbit);
59
60 for (i = 0; i < keymap_data->keymap_size; i++) {
61 unsigned int key = keymap_data->keymap[i];
62 unsigned int row = KEY_ROW(key);
63 unsigned int col = KEY_COL(key);
64 unsigned short code = KEY_VAL(key);
65
66 if (row >= rows || col >= cols) {
67 dev_err(input_dev->dev.parent,
68 "%s: invalid keymap entry %d (row: %d, col: %d, rows: %d, cols: %d)\n",
69 __func__, i, row, col, rows, cols);
70 return -EINVAL;
71 }
72
73 keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
74 __set_bit(code, input_dev->keybit);
75 }
76 __clear_bit(KEY_RESERVED, input_dev->keybit);
77
78 return 0;
79}
80EXPORT_SYMBOL(matrix_keypad_build_keymap);
81
82#ifdef CONFIG_OF
83struct matrix_keymap_data *
84matrix_keyboard_of_fill_keymap(struct device_node *np,
85 const char *propname)
86{
87 struct matrix_keymap_data *kd;
88 u32 *keymap;
89 int proplen, i;
90 const __be32 *prop;
91
92 if (!np)
93 return NULL;
94
95 if (!propname)
96 propname = "linux,keymap";
97
98 prop = of_get_property(np, propname, &proplen);
99 if (!prop)
100 return NULL;
101
102 if (proplen % sizeof(u32)) {
103 pr_warn("Malformed keymap property %s in %s\n",
104 propname, np->full_name);
105 return NULL;
106 }
107
108 kd = kzalloc(sizeof(*kd), GFP_KERNEL);
109 if (!kd)
110 return NULL;
111
112 kd->keymap = keymap = kzalloc(proplen, GFP_KERNEL);
113 if (!kd->keymap) {
114 kfree(kd);
115 return NULL;
116 }
117
118 kd->keymap_size = proplen / sizeof(u32);
119
120 for (i = 0; i < kd->keymap_size; i++) {
121 u32 tmp = be32_to_cpup(prop + i);
122 int key_code, row, col;
123
124 row = (tmp >> 24) & 0xff;
125 col = (tmp >> 16) & 0xff;
126 key_code = tmp & 0xffff;
127 keymap[i] = KEY(row, col, key_code);
128 }
129
130 return kd;
131}
132EXPORT_SYMBOL_GPL(matrix_keyboard_of_fill_keymap);
133
134void matrix_keyboard_of_free_keymap(const struct matrix_keymap_data *kd)
135{
136 if (kd) {
137 kfree(kd->keymap);
138 kfree(kd);
139 }
140}
141EXPORT_SYMBOL_GPL(matrix_keyboard_of_free_keymap);
142#endif