blob: 8ccefc15c7a4dfcfb03295a681f52620ebf47d4c [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.
Dmitry Torokhov19328112012-05-10 22:37:08 -070017 */
18
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070019#include <linux/device.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070020#include <linux/export.h>
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080021#include <linux/gfp.h>
22#include <linux/input.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070023#include <linux/input/matrix_keypad.h>
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080024#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/property.h>
27#include <linux/slab.h>
28#include <linux/types.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070029
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
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080052/**
53 * matrix_keypad_parse_properties() - Read properties of matrix keypad
54 *
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
59 */
60int matrix_keypad_parse_properties(struct device *dev,
61 unsigned int *rows, unsigned int *cols)
Simon Glass43840412013-02-25 14:08:40 -080062{
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080063 *rows = *cols = 0;
Simon Glass43840412013-02-25 14:08:40 -080064
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080065 device_property_read_u32(dev, "keypad,num-rows", rows);
66 device_property_read_u32(dev, "keypad,num-columns", cols);
67
Simon Glass43840412013-02-25 14:08:40 -080068 if (!*rows || !*cols) {
69 dev_err(dev, "number of keypad rows/columns not specified\n");
70 return -EINVAL;
71 }
72
73 return 0;
74}
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080075EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties);
Simon Glass43840412013-02-25 14:08:40 -080076
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080077static int matrix_keypad_parse_keymap(const char *propname,
78 unsigned int rows, unsigned int cols,
79 struct input_dev *input_dev)
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070080{
81 struct device *dev = input_dev->dev.parent;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070082 unsigned int row_shift = get_count_order(cols);
83 unsigned int max_keys = rows << row_shift;
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080084 u32 *keys;
85 int i;
86 int size;
87 int retval;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070088
89 if (!propname)
90 propname = "linux,keymap";
91
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080092 size = device_property_read_u32_array(dev, propname, NULL, 0);
93 if (size <= 0) {
94 dev_err(dev, "missing or malformed property %s: %d\n",
95 propname, size);
96 return size < 0 ? size : -EINVAL;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070097 }
98
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070099 if (size > max_keys) {
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800100 dev_err(dev, "%s size overflow (%d vs max %u)\n",
101 propname, size, max_keys);
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700102 return -EINVAL;
103 }
104
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800105 keys = kmalloc_array(size, sizeof(u32), GFP_KERNEL);
106 if (!keys)
107 return -ENOMEM;
108
109 retval = device_property_read_u32_array(dev, propname, keys, size);
110 if (retval) {
111 dev_err(dev, "failed to read %s property: %d\n",
112 propname, retval);
113 goto out;
114 }
115
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700116 for (i = 0; i < size; i++) {
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700117 if (!matrix_keypad_map_key(input_dev, rows, cols,
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800118 row_shift, keys[i])) {
119 retval = -EINVAL;
120 goto out;
121 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700122 }
123
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800124 retval = 0;
125
126out:
127 kfree(keys);
128 return retval;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700129}
Dmitry Torokhov19328112012-05-10 22:37:08 -0700130
131/**
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
141 *
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.
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700145 *
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).
149 *
Dmitry Torokhov53831162012-11-05 10:32:55 -0800150 * 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
153 * driver.
154 *
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700155 * Callers are expected to set up input_dev->dev.parent before calling this
156 * function.
Dmitry Torokhov19328112012-05-10 22:37:08 -0700157 */
158int 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)
163{
164 unsigned int row_shift = get_count_order(cols);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800165 size_t max_keys = rows << row_shift;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700166 int i;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700167 int error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700168
Dmitry Torokhov53831162012-11-05 10:32:55 -0800169 if (WARN_ON(!input_dev->dev.parent))
170 return -EINVAL;
171
172 if (!keymap) {
173 keymap = devm_kzalloc(input_dev->dev.parent,
174 max_keys * sizeof(*keymap),
175 GFP_KERNEL);
176 if (!keymap) {
177 dev_err(input_dev->dev.parent,
178 "Unable to allocate memory for keymap");
179 return -ENOMEM;
180 }
181 }
182
Dmitry Torokhov19328112012-05-10 22:37:08 -0700183 input_dev->keycode = keymap;
184 input_dev->keycodesize = sizeof(*keymap);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800185 input_dev->keycodemax = max_keys;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700186
187 __set_bit(EV_KEY, input_dev->evbit);
188
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700189 if (keymap_data) {
190 for (i = 0; i < keymap_data->keymap_size; i++) {
191 unsigned int key = keymap_data->keymap[i];
Dmitry Torokhov19328112012-05-10 22:37:08 -0700192
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700193 if (!matrix_keypad_map_key(input_dev, rows, cols,
194 row_shift, key))
195 return -EINVAL;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700196 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700197 } else {
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800198 error = matrix_keypad_parse_keymap(keymap_name, rows, cols,
199 input_dev);
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700200 if (error)
201 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700202 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700203
Dmitry Torokhov19328112012-05-10 22:37:08 -0700204 __clear_bit(KEY_RESERVED, input_dev->keybit);
205
206 return 0;
207}
208EXPORT_SYMBOL(matrix_keypad_build_keymap);
Florian Fainelli516d7982012-12-10 12:18:20 -0800209
210MODULE_LICENSE("GPL");