blob: 4fa53423f56c21376ba292511c8aa9dc9bcd5882 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Torokhov19328112012-05-10 22:37:08 -07002/*
3 * Helpers for matrix keyboard bindings
4 *
5 * Copyright (C) 2012 Google, Inc
6 *
7 * Author:
8 * Olof Johansson <olof@lixom.net>
Dmitry Torokhov19328112012-05-10 22:37:08 -07009 */
10
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070011#include <linux/device.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070012#include <linux/export.h>
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080013#include <linux/gfp.h>
14#include <linux/input.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070015#include <linux/input/matrix_keypad.h>
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/property.h>
19#include <linux/slab.h>
20#include <linux/types.h>
Dmitry Torokhov19328112012-05-10 22:37:08 -070021
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070022static bool matrix_keypad_map_key(struct input_dev *input_dev,
23 unsigned int rows, unsigned int cols,
24 unsigned int row_shift, unsigned int key)
25{
Dmitry Torokhov86809172012-05-24 01:10:20 -070026 unsigned short *keymap = input_dev->keycode;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070027 unsigned int row = KEY_ROW(key);
28 unsigned int col = KEY_COL(key);
29 unsigned short code = KEY_VAL(key);
30
31 if (row >= rows || col >= cols) {
32 dev_err(input_dev->dev.parent,
33 "%s: invalid keymap entry 0x%x (row: %d, col: %d, rows: %d, cols: %d)\n",
34 __func__, key, row, col, rows, cols);
35 return false;
36 }
37
38 keymap[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
39 __set_bit(code, input_dev->keybit);
40
41 return true;
42}
43
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080044/**
45 * matrix_keypad_parse_properties() - Read properties of matrix keypad
46 *
47 * @dev: Device containing properties
48 * @rows: Returns number of matrix rows
49 * @cols: Returns number of matrix columns
50 * @return 0 if OK, <0 on error
51 */
52int matrix_keypad_parse_properties(struct device *dev,
53 unsigned int *rows, unsigned int *cols)
Simon Glass43840412013-02-25 14:08:40 -080054{
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080055 *rows = *cols = 0;
Simon Glass43840412013-02-25 14:08:40 -080056
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080057 device_property_read_u32(dev, "keypad,num-rows", rows);
58 device_property_read_u32(dev, "keypad,num-columns", cols);
59
Simon Glass43840412013-02-25 14:08:40 -080060 if (!*rows || !*cols) {
61 dev_err(dev, "number of keypad rows/columns not specified\n");
62 return -EINVAL;
63 }
64
65 return 0;
66}
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080067EXPORT_SYMBOL_GPL(matrix_keypad_parse_properties);
Simon Glass43840412013-02-25 14:08:40 -080068
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080069static int matrix_keypad_parse_keymap(const char *propname,
70 unsigned int rows, unsigned int cols,
71 struct input_dev *input_dev)
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070072{
73 struct device *dev = input_dev->dev.parent;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070074 unsigned int row_shift = get_count_order(cols);
75 unsigned int max_keys = rows << row_shift;
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080076 u32 *keys;
77 int i;
78 int size;
79 int retval;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070080
81 if (!propname)
82 propname = "linux,keymap";
83
Andy Shevchenko656d29c2019-08-12 00:00:55 -070084 size = device_property_count_u32(dev, propname);
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080085 if (size <= 0) {
86 dev_err(dev, "missing or malformed property %s: %d\n",
87 propname, size);
88 return size < 0 ? size : -EINVAL;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070089 }
90
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070091 if (size > max_keys) {
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080092 dev_err(dev, "%s size overflow (%d vs max %u)\n",
93 propname, size, max_keys);
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -070094 return -EINVAL;
95 }
96
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -080097 keys = kmalloc_array(size, sizeof(u32), GFP_KERNEL);
98 if (!keys)
99 return -ENOMEM;
100
101 retval = device_property_read_u32_array(dev, propname, keys, size);
102 if (retval) {
103 dev_err(dev, "failed to read %s property: %d\n",
104 propname, retval);
105 goto out;
106 }
107
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700108 for (i = 0; i < size; i++) {
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700109 if (!matrix_keypad_map_key(input_dev, rows, cols,
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800110 row_shift, keys[i])) {
111 retval = -EINVAL;
112 goto out;
113 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700114 }
115
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800116 retval = 0;
117
118out:
119 kfree(keys);
120 return retval;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700121}
Dmitry Torokhov19328112012-05-10 22:37:08 -0700122
123/**
124 * matrix_keypad_build_keymap - convert platform keymap into matrix keymap
125 * @keymap_data: keymap supplied by the platform code
126 * @keymap_name: name of device tree property containing keymap (if device
127 * tree support is enabled).
128 * @rows: number of rows in target keymap array
129 * @cols: number of cols in target keymap array
130 * @keymap: expanded version of keymap that is suitable for use by
131 * matrix keyboard driver
132 * @input_dev: input devices for which we are setting up the keymap
133 *
134 * This function converts platform keymap (encoded with KEY() macro) into
135 * an array of keycodes that is suitable for using in a standard matrix
136 * keyboard driver that uses row and col as indices.
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700137 *
138 * If @keymap_data is not supplied and device tree support is enabled
139 * it will attempt load the keymap from property specified by @keymap_name
140 * argument (or "linux,keymap" if @keymap_name is %NULL).
141 *
Dmitry Torokhov53831162012-11-05 10:32:55 -0800142 * If @keymap is %NULL the function will automatically allocate managed
143 * block of memory to store the keymap. This memory will be associated with
144 * the parent device and automatically freed when device unbinds from the
145 * driver.
146 *
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700147 * Callers are expected to set up input_dev->dev.parent before calling this
148 * function.
Dmitry Torokhov19328112012-05-10 22:37:08 -0700149 */
150int matrix_keypad_build_keymap(const struct matrix_keymap_data *keymap_data,
151 const char *keymap_name,
152 unsigned int rows, unsigned int cols,
153 unsigned short *keymap,
154 struct input_dev *input_dev)
155{
156 unsigned int row_shift = get_count_order(cols);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800157 size_t max_keys = rows << row_shift;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700158 int i;
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700159 int error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700160
Dmitry Torokhov53831162012-11-05 10:32:55 -0800161 if (WARN_ON(!input_dev->dev.parent))
162 return -EINVAL;
163
164 if (!keymap) {
Kees Cooka86854d2018-06-12 14:07:58 -0700165 keymap = devm_kcalloc(input_dev->dev.parent,
166 max_keys, sizeof(*keymap),
Dmitry Torokhov53831162012-11-05 10:32:55 -0800167 GFP_KERNEL);
168 if (!keymap) {
169 dev_err(input_dev->dev.parent,
170 "Unable to allocate memory for keymap");
171 return -ENOMEM;
172 }
173 }
174
Dmitry Torokhov19328112012-05-10 22:37:08 -0700175 input_dev->keycode = keymap;
176 input_dev->keycodesize = sizeof(*keymap);
Dmitry Torokhov53831162012-11-05 10:32:55 -0800177 input_dev->keycodemax = max_keys;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700178
179 __set_bit(EV_KEY, input_dev->evbit);
180
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700181 if (keymap_data) {
182 for (i = 0; i < keymap_data->keymap_size; i++) {
183 unsigned int key = keymap_data->keymap[i];
Dmitry Torokhov19328112012-05-10 22:37:08 -0700184
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700185 if (!matrix_keypad_map_key(input_dev, rows, cols,
186 row_shift, key))
187 return -EINVAL;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700188 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700189 } else {
Dmitry Torokhovaef01aa2016-11-11 12:43:12 -0800190 error = matrix_keypad_parse_keymap(keymap_name, rows, cols,
191 input_dev);
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700192 if (error)
193 return error;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700194 }
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700195
Dmitry Torokhov19328112012-05-10 22:37:08 -0700196 __clear_bit(KEY_RESERVED, input_dev->keybit);
197
198 return 0;
199}
200EXPORT_SYMBOL(matrix_keypad_build_keymap);
Florian Fainelli516d7982012-12-10 12:18:20 -0800201
202MODULE_LICENSE("GPL");