]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/input/key_matrix.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / drivers / input / key_matrix.c
1 /*
2  * Manage Keyboard Matrices
3  *
4  * Copyright (c) 2012 The Chromium OS Authors.
5  * (C) Copyright 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #include <fdtdec.h>
27 #include <key_matrix.h>
28 #include <malloc.h>
29 #include <linux/input.h>
30
31 /**
32  * Determine if the current keypress configuration can cause key ghosting
33  *
34  * We figure this out by seeing if we have two or more keys in the same
35  * column, as well as two or more keys in the same row.
36  *
37  * @param config        Keyboard matrix config
38  * @param keys          List of keys to check
39  * @param valid         Number of valid keypresses to check
40  * @return 0 if no ghosting is possible, 1 if it is
41  */
42 static int has_ghosting(struct key_matrix *config, struct key_matrix_key *keys,
43                         int valid)
44 {
45         int key_in_same_col = 0, key_in_same_row = 0;
46         int i, j;
47
48         for (i = 0; i < valid; i++) {
49                 /*
50                  * Find 2 keys such that one key is in the same row
51                  * and the other is in the same column as the i-th key.
52                  */
53                 for (j = i + 1; j < valid; j++) {
54                         if (keys[j].col == keys[i].col)
55                                 key_in_same_col = 1;
56                         if (keys[j].row == keys[i].row)
57                                 key_in_same_row = 1;
58                 }
59         }
60
61         if (key_in_same_col && key_in_same_row)
62                 return 1;
63         else
64                 return 0;
65 }
66
67 int key_matrix_decode(struct key_matrix *config, struct key_matrix_key keys[],
68                       int num_keys, int keycode[], int max_keycodes)
69 {
70         const u8 *keymap;
71         int valid, upto;
72         int pos;
73
74         debug("%s: num_keys = %d\n", __func__, num_keys);
75         keymap = config->plain_keycode;
76         for (valid = upto = 0; upto < num_keys; upto++) {
77                 struct key_matrix_key *key = &keys[upto];
78
79                 debug("  valid=%d, row=%d, col=%d\n", key->valid, key->row,
80                       key->col);
81                 if (!key->valid)
82                         continue;
83                 pos = key->row * config->num_cols + key->col;
84                 if (config->fn_keycode && pos == config->fn_pos)
85                         keymap = config->fn_keycode;
86
87                 /* Convert the (row, col) values into a keycode */
88                 if (valid < max_keycodes)
89                         keycode[valid++] = keymap[pos];
90                 debug("    keycode=%d\n", keymap[pos]);
91         }
92
93         /* For a ghost key config, ignore the keypresses for this iteration. */
94         if (valid >= 3 && has_ghosting(config, keys, valid)) {
95                 valid = 0;
96                 debug("    ghosting detected!\n");
97         }
98         debug("  %d valid keycodes found\n", valid);
99
100         return valid;
101 }
102
103 /**
104  * Create a new keycode map from some provided data
105  *
106  * This decodes a keycode map in the format used by the fdt, which is one
107  * word per entry, with the row, col and keycode encoded in that word.
108  *
109  * We create a (row x col) size byte array with each entry containing the
110  * keycode for that (row, col). We also search for map_keycode and return
111  * its position if found (this is used for finding the Fn key).
112  *
113  * @param config        Key matrix dimensions structure
114  * @param data          Keycode data
115  * @param len           Number of entries in keycode table
116  * @param map_keycode   Key code to find in the map
117  * @param pos           Returns position of map_keycode, if found, else -1
118  * @return map  Pointer to allocated map
119  */
120 static uchar *create_keymap(struct key_matrix *config, u32 *data, int len,
121                             int map_keycode, int *pos)
122 {
123         uchar *map;
124
125         if (pos)
126                 *pos = -1;
127         map = (uchar *)calloc(1, config->key_count);
128         if (!map) {
129                 debug("%s: failed to malloc %d bytes\n", __func__,
130                         config->key_count);
131                 return NULL;
132         }
133
134         for (; len >= sizeof(u32); data++, len -= 4) {
135                 u32 tmp = fdt32_to_cpu(*data);
136                 int key_code, row, col;
137                 int entry;
138
139                 row = (tmp >> 24) & 0xff;
140                 col = (tmp >> 16) & 0xff;
141                 key_code = tmp & 0xffff;
142                 entry = row * config->num_cols + col;
143                 map[entry] = key_code;
144                 if (pos && map_keycode == key_code)
145                         *pos = entry;
146         }
147
148         return map;
149 }
150
151 int key_matrix_decode_fdt(struct key_matrix *config, const void *blob,
152                           int node)
153 {
154         const struct fdt_property *prop;
155         int offset;
156
157         /* Check each property name for ones that we understand */
158         for (offset = fdt_first_property_offset(blob, node);
159                       offset > 0;
160                       offset = fdt_next_property_offset(blob, offset)) {
161                 const char *name;
162                 int len;
163
164                 prop = fdt_get_property_by_offset(blob, offset, NULL);
165                 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
166                 len = strlen(name);
167
168                 /* Name needs to match "1,<type>keymap" */
169                 debug("%s: property '%s'\n", __func__, name);
170                 if (strncmp(name, "1,", 2) || len < 8 ||
171                     strcmp(name + len - 6, "keymap"))
172                         continue;
173
174                 len -= 8;
175                 if (len == 0) {
176                         config->plain_keycode = create_keymap(config,
177                                 (u32 *)prop->data, fdt32_to_cpu(prop->len),
178                                 KEY_FN, &config->fn_pos);
179                 } else if (0 == strncmp(name + 2, "fn-", len)) {
180                         config->fn_keycode = create_keymap(config,
181                                 (u32 *)prop->data, fdt32_to_cpu(prop->len),
182                                 -1, NULL);
183                 } else {
184                         debug("%s: unrecognised property '%s'\n", __func__,
185                               name);
186                 }
187         }
188         debug("%s: Decoded key maps %p, %p from fdt\n", __func__,
189               config->plain_keycode, config->fn_keycode);
190
191         if (!config->plain_keycode) {
192                 debug("%s: cannot find keycode-plain map\n", __func__);
193                 return -1;
194         }
195
196         return 0;
197 }
198
199 int key_matrix_init(struct key_matrix *config, int rows, int cols)
200 {
201         memset(config, '\0', sizeof(*config));
202         config->num_rows = rows;
203         config->num_cols = cols;
204         config->key_count = rows * cols;
205         assert(config->key_count > 0);
206
207         return 0;
208 }