Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * ChromeOS EC keyboard driver
  3 *
  4 * Copyright (C) 2012 Google, Inc
  5 *
  6 * This software is licensed under the terms of the GNU General Public
  7 * License version 2, as published by the Free Software Foundation, and
  8 * may be copied, distributed, and modified under those terms.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * This driver uses the Chrome OS EC byte-level message-based protocol for
 16 * communicating the keyboard state (which keys are pressed) from a keyboard EC
 17 * to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
 18 * but everything else (including deghosting) is done here.  The main
 19 * motivation for this is to keep the EC firmware as simple as possible, since
 20 * it cannot be easily upgraded and EC flash/IRAM space is relatively
 21 * expensive.
 22 */
 23
 24#include <linux/module.h>
 25#include <linux/bitops.h>
 26#include <linux/i2c.h>
 27#include <linux/input.h>
 28#include <linux/interrupt.h>
 29#include <linux/kernel.h>
 
 30#include <linux/platform_device.h>
 31#include <linux/slab.h>
 
 32#include <linux/input/matrix_keypad.h>
 33#include <linux/mfd/cros_ec.h>
 34#include <linux/mfd/cros_ec_commands.h>
 
 
 35
 36/*
 37 * @rows: Number of rows in the keypad
 38 * @cols: Number of columns in the keypad
 39 * @row_shift: log2 or number of rows, rounded up
 40 * @keymap_data: Matrix keymap data used to convert to keyscan values
 41 * @ghost_filter: true to enable the matrix key-ghosting filter
 42 * @valid_keys: bitmap of existing keys for each matrix column
 43 * @old_kb_state: bitmap of keys pressed last scan
 44 * @dev: Device pointer
 45 * @idev: Input device
 46 * @ec: Top level ChromeOS device to use to talk to EC
 
 
 
 47 */
 48struct cros_ec_keyb {
 49	unsigned int rows;
 50	unsigned int cols;
 51	int row_shift;
 52	const struct matrix_keymap_data *keymap_data;
 53	bool ghost_filter;
 54	uint8_t *valid_keys;
 55	uint8_t *old_kb_state;
 56
 57	struct device *dev;
 58	struct input_dev *idev;
 59	struct cros_ec_device *ec;
 
 
 
 
 60};
 61
 62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 63/*
 64 * Returns true when there is at least one combination of pressed keys that
 65 * results in ghosting.
 66 */
 67static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
 68{
 69	int col1, col2, buf1, buf2;
 70	struct device *dev = ckdev->dev;
 71	uint8_t *valid_keys = ckdev->valid_keys;
 72
 73	/*
 74	 * Ghosting happens if for any pressed key X there are other keys
 75	 * pressed both in the same row and column of X as, for instance,
 76	 * in the following diagram:
 77	 *
 78	 * . . Y . g .
 79	 * . . . . . .
 80	 * . . . . . .
 81	 * . . X . Z .
 82	 *
 83	 * In this case only X, Y, and Z are pressed, but g appears to be
 84	 * pressed too (see Wikipedia).
 85	 */
 86	for (col1 = 0; col1 < ckdev->cols; col1++) {
 87		buf1 = buf[col1] & valid_keys[col1];
 88		for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
 89			buf2 = buf[col2] & valid_keys[col2];
 90			if (hweight8(buf1 & buf2) > 1) {
 91				dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
 92					col1, buf1, col2, buf2);
 93				return true;
 94			}
 95		}
 96	}
 97
 98	return false;
 99}
100
101
102/*
103 * Compares the new keyboard state to the old one and produces key
104 * press/release events accordingly.  The keyboard state is 13 bytes (one byte
105 * per column)
106 */
107static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
108			 uint8_t *kb_state, int len)
109{
110	struct input_dev *idev = ckdev->idev;
111	int col, row;
112	int new_state;
113	int old_state;
114	int num_cols;
115
116	num_cols = len;
117
118	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
119		/*
120		 * Simple-minded solution: ignore this state. The obvious
121		 * improvement is to only ignore changes to keys involved in
122		 * the ghosting, but process the other changes.
123		 */
124		dev_dbg(ckdev->dev, "ghosting found\n");
125		return;
126	}
127
128	for (col = 0; col < ckdev->cols; col++) {
129		for (row = 0; row < ckdev->rows; row++) {
130			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
131			const unsigned short *keycodes = idev->keycode;
132
133			new_state = kb_state[col] & (1 << row);
134			old_state = ckdev->old_kb_state[col] & (1 << row);
135			if (new_state != old_state) {
136				dev_dbg(ckdev->dev,
137					"changed: [r%d c%d]: byte %02x\n",
138					row, col, new_state);
139
140				input_report_key(idev, keycodes[pos],
141						 new_state);
142			}
143		}
144		ckdev->old_kb_state[col] = kb_state[col];
145	}
146	input_sync(ckdev->idev);
147}
148
149static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
 
 
 
 
 
 
 
 
 
 
 
 
150{
151	int ret = 0;
152	struct cros_ec_command *msg;
153
154	msg = kmalloc(sizeof(*msg) + ckdev->cols, GFP_KERNEL);
155	if (!msg)
156		return -ENOMEM;
157
158	msg->version = 0;
159	msg->command = EC_CMD_MKBP_STATE;
160	msg->insize = ckdev->cols;
161	msg->outsize = 0;
162
163	ret = cros_ec_cmd_xfer(ckdev->ec, msg);
164	if (ret < 0) {
165		dev_err(ckdev->dev, "Error transferring EC message %d\n", ret);
166		goto exit;
167	}
168
169	memcpy(kb_state, msg->data, ckdev->cols);
170exit:
171	kfree(msg);
172	return ret;
173}
174
175static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
 
176{
177	struct cros_ec_keyb *ckdev = data;
178	struct cros_ec_device *ec = ckdev->ec;
179	int ret;
180	uint8_t kb_state[ckdev->cols];
181
182	if (device_may_wakeup(ec->dev))
183		pm_wakeup_event(ec->dev, 0);
184
185	ret = cros_ec_keyb_get_state(ckdev, kb_state);
186	if (ret >= 0)
187		cros_ec_keyb_process(ckdev, kb_state, ret);
188	else
189		dev_err(ec->dev, "failed to get keyboard state: %d\n", ret);
190
191	return IRQ_HANDLED;
192}
193
194static int cros_ec_keyb_open(struct input_dev *dev)
195{
196	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
197	struct cros_ec_device *ec = ckdev->ec;
 
 
 
 
 
198
199	return request_threaded_irq(ec->irq, NULL, cros_ec_keyb_irq,
200					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
201					"cros_ec_keyb", ckdev);
202}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
204static void cros_ec_keyb_close(struct input_dev *dev)
205{
206	struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
207	struct cros_ec_device *ec = ckdev->ec;
208
209	free_irq(ec->irq, ckdev);
210}
211
212/*
213 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
214 * ghosting logic to ignore NULL or virtual keys.
215 */
216static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
217{
218	int row, col;
219	int row_shift = ckdev->row_shift;
220	unsigned short *keymap = ckdev->idev->keycode;
221	unsigned short code;
222
223	BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
224
225	for (col = 0; col < ckdev->cols; col++) {
226		for (row = 0; row < ckdev->rows; row++) {
227			code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
228			if (code && (code != KEY_BATTERY))
229				ckdev->valid_keys[col] |= 1 << row;
230		}
231		dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
232			col, ckdev->valid_keys[col]);
233	}
234}
235
236static int cros_ec_keyb_probe(struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237{
238	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
239	struct device *dev = ec->dev;
240	struct cros_ec_keyb *ckdev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241	struct input_dev *idev;
242	struct device_node *np;
243	int err;
 
 
 
 
244
245	np = pdev->dev.of_node;
246	if (!np)
247		return -ENODEV;
 
 
 
 
 
 
 
 
 
 
248
249	ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
250	if (!ckdev)
 
 
 
 
 
 
 
 
251		return -ENOMEM;
252	err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
253					    &ckdev->cols);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254	if (err)
255		return err;
256
257	ckdev->valid_keys = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
258	if (!ckdev->valid_keys)
259		return -ENOMEM;
260
261	ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
262	if (!ckdev->old_kb_state)
263		return -ENOMEM;
264
265	idev = devm_input_allocate_device(&pdev->dev);
266	if (!idev)
 
 
 
 
267		return -ENOMEM;
268
269	if (!ec->irq) {
270		dev_err(dev, "no EC IRQ specified\n");
271		return -EINVAL;
272	}
273
274	ckdev->ec = ec;
275	ckdev->dev = dev;
276	dev_set_drvdata(&pdev->dev, ckdev);
277
278	idev->name = CROS_EC_DEV_NAME;
279	idev->phys = ec->phys_name;
280	__set_bit(EV_REP, idev->evbit);
281
282	idev->id.bustype = BUS_VIRTUAL;
283	idev->id.version = 1;
284	idev->id.product = 0;
285	idev->dev.parent = &pdev->dev;
286	idev->open = cros_ec_keyb_open;
287	idev->close = cros_ec_keyb_close;
288
289	ckdev->ghost_filter = of_property_read_bool(np,
290					"google,needs-ghost-filter");
291
292	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
293					 NULL, idev);
294	if (err) {
295		dev_err(dev, "cannot build key matrix\n");
296		return err;
297	}
298
299	ckdev->row_shift = get_count_order(ckdev->cols);
300
301	input_set_capability(idev, EV_MSC, MSC_SCAN);
302	input_set_drvdata(idev, ckdev);
303	ckdev->idev = idev;
304	cros_ec_keyb_compute_valid_keys(ckdev);
305
306	err = input_register_device(ckdev->idev);
307	if (err) {
308		dev_err(dev, "cannot register input device\n");
309		return err;
310	}
311
312	return 0;
313}
314
315#ifdef CONFIG_PM_SLEEP
316/* Clear any keys in the buffer */
317static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
318{
319	uint8_t old_state[ckdev->cols];
320	uint8_t new_state[ckdev->cols];
321	unsigned long duration;
322	int i, ret;
323
324	/*
325	 * Keep reading until we see that the scan state does not change.
326	 * That indicates that we are done.
327	 *
328	 * Assume that the EC keyscan buffer is at most 32 deep.
329	 */
330	duration = jiffies;
331	ret = cros_ec_keyb_get_state(ckdev, new_state);
332	for (i = 1; !ret && i < 32; i++) {
333		memcpy(old_state, new_state, sizeof(old_state));
334		ret = cros_ec_keyb_get_state(ckdev, new_state);
335		if (0 == memcmp(old_state, new_state, sizeof(old_state)))
336			break;
337	}
338	duration = jiffies - duration;
339	dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
340		jiffies_to_usecs(duration));
341}
342
343static int cros_ec_keyb_resume(struct device *dev)
344{
345	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
346
347	/*
348	 * When the EC is not a wake source, then it could not have caused the
349	 * resume, so we clear the EC's key scan buffer. If the EC was a
350	 * wake source (e.g. the lid is open and the user might press a key to
351	 * wake) then the key scan buffer should be preserved.
352	 */
353	if (!ckdev->ec->was_wake_device)
354		cros_ec_keyb_clear_keyboard(ckdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
 
356	return 0;
357}
358
359#endif
 
 
360
361static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
 
 
 
 
362
363#ifdef CONFIG_OF
364static const struct of_device_id cros_ec_keyb_of_match[] = {
365	{ .compatible = "google,cros-ec-keyb" },
366	{},
367};
368MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
369#endif
370
 
 
371static struct platform_driver cros_ec_keyb_driver = {
372	.probe = cros_ec_keyb_probe,
 
373	.driver = {
374		.name = "cros-ec-keyb",
375		.of_match_table = of_match_ptr(cros_ec_keyb_of_match),
376		.pm	= &cros_ec_keyb_pm_ops,
377	},
378};
379
380module_platform_driver(cros_ec_keyb_driver);
381
382MODULE_LICENSE("GPL");
383MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
384MODULE_ALIAS("platform:cros-ec-keyb");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2// ChromeOS EC keyboard driver
  3//
  4// Copyright (C) 2012 Google, Inc.
  5//
  6// This driver uses the ChromeOS EC byte-level message-based protocol for
  7// communicating the keyboard state (which keys are pressed) from a keyboard EC
  8// to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
  9// but everything else (including deghosting) is done here.  The main
 10// motivation for this is to keep the EC firmware as simple as possible, since
 11// it cannot be easily upgraded and EC flash/IRAM space is relatively
 12// expensive.
 
 
 
 
 
 
 
 
 
 
 13
 14#include <linux/module.h>
 15#include <linux/bitops.h>
 16#include <linux/i2c.h>
 17#include <linux/input.h>
 18#include <linux/interrupt.h>
 19#include <linux/kernel.h>
 20#include <linux/notifier.h>
 21#include <linux/platform_device.h>
 22#include <linux/slab.h>
 23#include <linux/sysrq.h>
 24#include <linux/input/matrix_keypad.h>
 25#include <linux/platform_data/cros_ec_commands.h>
 26#include <linux/platform_data/cros_ec_proto.h>
 27
 28#include <asm/unaligned.h>
 29
 30/*
 31 * @rows: Number of rows in the keypad
 32 * @cols: Number of columns in the keypad
 33 * @row_shift: log2 or number of rows, rounded up
 34 * @keymap_data: Matrix keymap data used to convert to keyscan values
 35 * @ghost_filter: true to enable the matrix key-ghosting filter
 36 * @valid_keys: bitmap of existing keys for each matrix column
 37 * @old_kb_state: bitmap of keys pressed last scan
 38 * @dev: Device pointer
 
 39 * @ec: Top level ChromeOS device to use to talk to EC
 40 * @idev: The input device for the matrix keys.
 41 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
 42 * @notifier: interrupt event notifier for transport devices
 43 */
 44struct cros_ec_keyb {
 45	unsigned int rows;
 46	unsigned int cols;
 47	int row_shift;
 48	const struct matrix_keymap_data *keymap_data;
 49	bool ghost_filter;
 50	uint8_t *valid_keys;
 51	uint8_t *old_kb_state;
 52
 53	struct device *dev;
 
 54	struct cros_ec_device *ec;
 55
 56	struct input_dev *idev;
 57	struct input_dev *bs_idev;
 58	struct notifier_block notifier;
 59};
 60
 61
 62/**
 63 * cros_ec_bs_map - Struct mapping Linux keycodes to EC button/switch bitmap
 64 * #defines
 65 *
 66 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
 67 * @code: A linux keycode
 68 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
 69 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
 70 *            Only applicable to switches.
 71 */
 72struct cros_ec_bs_map {
 73	unsigned int ev_type;
 74	unsigned int code;
 75	u8 bit;
 76	bool inverted;
 77};
 78
 79/* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
 80static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
 81	/* Buttons */
 82	{
 83		.ev_type	= EV_KEY,
 84		.code		= KEY_POWER,
 85		.bit		= EC_MKBP_POWER_BUTTON,
 86	},
 87	{
 88		.ev_type	= EV_KEY,
 89		.code		= KEY_VOLUMEUP,
 90		.bit		= EC_MKBP_VOL_UP,
 91	},
 92	{
 93		.ev_type	= EV_KEY,
 94		.code		= KEY_VOLUMEDOWN,
 95		.bit		= EC_MKBP_VOL_DOWN,
 96	},
 97
 98	/* Switches */
 99	{
100		.ev_type	= EV_SW,
101		.code		= SW_LID,
102		.bit		= EC_MKBP_LID_OPEN,
103		.inverted	= true,
104	},
105	{
106		.ev_type	= EV_SW,
107		.code		= SW_TABLET_MODE,
108		.bit		= EC_MKBP_TABLET_MODE,
109	},
110};
111
112/*
113 * Returns true when there is at least one combination of pressed keys that
114 * results in ghosting.
115 */
116static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
117{
118	int col1, col2, buf1, buf2;
119	struct device *dev = ckdev->dev;
120	uint8_t *valid_keys = ckdev->valid_keys;
121
122	/*
123	 * Ghosting happens if for any pressed key X there are other keys
124	 * pressed both in the same row and column of X as, for instance,
125	 * in the following diagram:
126	 *
127	 * . . Y . g .
128	 * . . . . . .
129	 * . . . . . .
130	 * . . X . Z .
131	 *
132	 * In this case only X, Y, and Z are pressed, but g appears to be
133	 * pressed too (see Wikipedia).
134	 */
135	for (col1 = 0; col1 < ckdev->cols; col1++) {
136		buf1 = buf[col1] & valid_keys[col1];
137		for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
138			buf2 = buf[col2] & valid_keys[col2];
139			if (hweight8(buf1 & buf2) > 1) {
140				dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
141					col1, buf1, col2, buf2);
142				return true;
143			}
144		}
145	}
146
147	return false;
148}
149
150
151/*
152 * Compares the new keyboard state to the old one and produces key
153 * press/release events accordingly.  The keyboard state is 13 bytes (one byte
154 * per column)
155 */
156static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
157			 uint8_t *kb_state, int len)
158{
159	struct input_dev *idev = ckdev->idev;
160	int col, row;
161	int new_state;
162	int old_state;
 
 
 
163
164	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
165		/*
166		 * Simple-minded solution: ignore this state. The obvious
167		 * improvement is to only ignore changes to keys involved in
168		 * the ghosting, but process the other changes.
169		 */
170		dev_dbg(ckdev->dev, "ghosting found\n");
171		return;
172	}
173
174	for (col = 0; col < ckdev->cols; col++) {
175		for (row = 0; row < ckdev->rows; row++) {
176			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
177			const unsigned short *keycodes = idev->keycode;
178
179			new_state = kb_state[col] & (1 << row);
180			old_state = ckdev->old_kb_state[col] & (1 << row);
181			if (new_state != old_state) {
182				dev_dbg(ckdev->dev,
183					"changed: [r%d c%d]: byte %02x\n",
184					row, col, new_state);
185
186				input_report_key(idev, keycodes[pos],
187						 new_state);
188			}
189		}
190		ckdev->old_kb_state[col] = kb_state[col];
191	}
192	input_sync(ckdev->idev);
193}
194
195/**
196 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
197 *
198 * This takes a bitmap of buttons or switches from the EC and reports events,
199 * syncing at the end.
200 *
201 * @ckdev: The keyboard device.
202 * @ev_type: The input event type (e.g., EV_KEY).
203 * @mask: A bitmap of buttons from the EC.
204 */
205static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
206				   unsigned int ev_type, u32 mask)
207
208{
209	struct input_dev *idev = ckdev->bs_idev;
210	int i;
211
212	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
213		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
 
214
215		if (map->ev_type != ev_type)
216			continue;
 
 
 
 
 
 
 
 
217
218		input_event(idev, ev_type, map->code,
219			    !!(mask & BIT(map->bit)) ^ map->inverted);
220	}
221	input_sync(idev);
222}
223
224static int cros_ec_keyb_work(struct notifier_block *nb,
225			     unsigned long queued_during_suspend, void *_notify)
226{
227	struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
228						  notifier);
229	u32 val;
230	unsigned int ev_type;
 
 
 
231
232	/*
233	 * If not wake enabled, discard key state changes during
234	 * suspend. Switches will be re-checked in
235	 * cros_ec_keyb_resume() to be sure nothing is lost.
236	 */
237	if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
238		return NOTIFY_OK;
 
239
240	switch (ckdev->ec->event_data.event_type) {
241	case EC_MKBP_EVENT_KEY_MATRIX:
242		pm_wakeup_event(ckdev->dev, 0);
243
244		if (ckdev->ec->event_size != ckdev->cols) {
245			dev_err(ckdev->dev,
246				"Discarded incomplete key matrix event.\n");
247			return NOTIFY_OK;
248		}
249
250		cros_ec_keyb_process(ckdev,
251				     ckdev->ec->event_data.data.key_matrix,
252				     ckdev->ec->event_size);
253		break;
254
255	case EC_MKBP_EVENT_SYSRQ:
256		pm_wakeup_event(ckdev->dev, 0);
257
258		val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
259		dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
260		handle_sysrq(val);
261		break;
262
263	case EC_MKBP_EVENT_BUTTON:
264	case EC_MKBP_EVENT_SWITCH:
265		pm_wakeup_event(ckdev->dev, 0);
266
267		if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
268			val = get_unaligned_le32(
269					&ckdev->ec->event_data.data.buttons);
270			ev_type = EV_KEY;
271		} else {
272			val = get_unaligned_le32(
273					&ckdev->ec->event_data.data.switches);
274			ev_type = EV_SW;
275		}
276		cros_ec_keyb_report_bs(ckdev, ev_type, val);
277		break;
278
279	default:
280		return NOTIFY_DONE;
281	}
 
282
283	return NOTIFY_OK;
284}
285
286/*
287 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
288 * ghosting logic to ignore NULL or virtual keys.
289 */
290static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
291{
292	int row, col;
293	int row_shift = ckdev->row_shift;
294	unsigned short *keymap = ckdev->idev->keycode;
295	unsigned short code;
296
297	BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
298
299	for (col = 0; col < ckdev->cols; col++) {
300		for (row = 0; row < ckdev->rows; row++) {
301			code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
302			if (code && (code != KEY_BATTERY))
303				ckdev->valid_keys[col] |= 1 << row;
304		}
305		dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
306			col, ckdev->valid_keys[col]);
307	}
308}
309
310/**
311 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
312 *
313 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
314 * unmarshalling and different version nonsense into something simple.
315 *
316 * @ec_dev: The EC device
317 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
318 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH.  Actually
319 *              in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
320 *              EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
321 * @result: Where we'll store the result; a union
322 * @result_size: The size of the result.  Expected to be the size of one of
323 *               the elements in the union.
324 *
325 * Returns 0 if no error or -error upon error.
326 */
327static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
328			     enum ec_mkbp_info_type info_type,
329			     enum ec_mkbp_event event_type,
330			     union ec_response_get_next_data *result,
331			     size_t result_size)
332{
333	struct ec_params_mkbp_info *params;
334	struct cros_ec_command *msg;
335	int ret;
336
337	msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
338					   sizeof(*params)), GFP_KERNEL);
339	if (!msg)
340		return -ENOMEM;
341
342	msg->command = EC_CMD_MKBP_INFO;
343	msg->version = 1;
344	msg->outsize = sizeof(*params);
345	msg->insize = result_size;
346	params = (struct ec_params_mkbp_info *)msg->data;
347	params->info_type = info_type;
348	params->event_type = event_type;
349
350	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
351	if (ret == -ENOTSUPP) {
352		/* With older ECs we just return 0 for everything */
353		memset(result, 0, result_size);
354		ret = 0;
355	} else if (ret < 0) {
356		dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
357			 (int)info_type, (int)event_type, ret);
358	} else if (ret != result_size) {
359		dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
360			 (int)info_type, (int)event_type,
361			 ret, result_size);
362		ret = -EPROTO;
363	} else {
364		memcpy(result, msg->data, result_size);
365		ret = 0;
366	}
367
368	kfree(msg);
369
370	return ret;
371}
372
373/**
374 * cros_ec_keyb_query_switches - Query the state of switches and report
375 *
376 * This will ask the EC about the current state of switches and report to the
377 * kernel.  Note that we don't query for buttons because they are more
378 * transitory and we'll get an update on the next release / press.
379 *
380 * @ckdev: The keyboard device
381 *
382 * Returns 0 if no error or -error upon error.
383 */
384static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
385{
386	struct cros_ec_device *ec_dev = ckdev->ec;
387	union ec_response_get_next_data event_data = {};
388	int ret;
389
390	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
391				EC_MKBP_EVENT_SWITCH, &event_data,
392				sizeof(event_data.switches));
393	if (ret)
394		return ret;
395
396	cros_ec_keyb_report_bs(ckdev, EV_SW,
397			       get_unaligned_le32(&event_data.switches));
398
399	return 0;
400}
401
402/**
403 * cros_ec_keyb_resume - Resume the keyboard
404 *
405 * We use the resume notification as a chance to query the EC for switches.
406 *
407 * @dev: The keyboard device
408 *
409 * Returns 0 if no error or -error upon error.
410 */
411static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
412{
413	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
414
415	if (ckdev->bs_idev)
416		return cros_ec_keyb_query_switches(ckdev);
417
418	return 0;
419}
420
421/**
422 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
423 *
424 * Handles all the bits of the keyboard driver related to non-matrix buttons
425 * and switches, including asking the EC about which are present and telling
426 * the kernel to expect them.
427 *
428 * If this device has no support for buttons and switches we'll return no error
429 * but the ckdev->bs_idev will remain NULL when this function exits.
430 *
431 * @ckdev: The keyboard device
432 *
433 * Returns 0 if no error or -error upon error.
434 */
435static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
436{
437	struct cros_ec_device *ec_dev = ckdev->ec;
438	struct device *dev = ckdev->dev;
439	struct input_dev *idev;
440	union ec_response_get_next_data event_data = {};
441	const char *phys;
442	u32 buttons;
443	u32 switches;
444	int ret;
445	int i;
446
447	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
448				EC_MKBP_EVENT_BUTTON, &event_data,
449				sizeof(event_data.buttons));
450	if (ret)
451		return ret;
452	buttons = get_unaligned_le32(&event_data.buttons);
453
454	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
455				EC_MKBP_EVENT_SWITCH, &event_data,
456				sizeof(event_data.switches));
457	if (ret)
458		return ret;
459	switches = get_unaligned_le32(&event_data.switches);
460
461	if (!buttons && !switches)
462		return 0;
463
464	/*
465	 * We call the non-matrix buttons/switches 'input1', if present.
466	 * Allocate phys before input dev, to ensure correct tear-down
467	 * ordering.
468	 */
469	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
470	if (!phys)
471		return -ENOMEM;
472
473	idev = devm_input_allocate_device(dev);
474	if (!idev)
475		return -ENOMEM;
476
477	idev->name = "cros_ec_buttons";
478	idev->phys = phys;
479	__set_bit(EV_REP, idev->evbit);
480
481	idev->id.bustype = BUS_VIRTUAL;
482	idev->id.version = 1;
483	idev->id.product = 0;
484	idev->dev.parent = dev;
485
486	input_set_drvdata(idev, ckdev);
487	ckdev->bs_idev = idev;
488
489	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
490		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
491
492		if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
493		    (map->ev_type == EV_SW && (switches & BIT(map->bit))))
494			input_set_capability(idev, map->ev_type, map->code);
495	}
496
497	ret = cros_ec_keyb_query_switches(ckdev);
498	if (ret) {
499		dev_err(dev, "cannot query switches\n");
500		return ret;
501	}
502
503	ret = input_register_device(ckdev->bs_idev);
504	if (ret) {
505		dev_err(dev, "cannot register input device\n");
506		return ret;
507	}
508
509	return 0;
510}
511
512/**
513 * cros_ec_keyb_register_bs - Register matrix keys
514 *
515 * Handles all the bits of the keyboard driver related to matrix keys.
516 *
517 * @ckdev: The keyboard device
518 *
519 * Returns 0 if no error or -error upon error.
520 */
521static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
522{
523	struct cros_ec_device *ec_dev = ckdev->ec;
524	struct device *dev = ckdev->dev;
525	struct input_dev *idev;
526	const char *phys;
527	int err;
528
529	err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
530	if (err)
531		return err;
532
533	ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
534	if (!ckdev->valid_keys)
535		return -ENOMEM;
536
537	ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
538	if (!ckdev->old_kb_state)
539		return -ENOMEM;
540
541	/*
542	 * We call the keyboard matrix 'input0'. Allocate phys before input
543	 * dev, to ensure correct tear-down ordering.
544	 */
545	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
546	if (!phys)
547		return -ENOMEM;
548
549	idev = devm_input_allocate_device(dev);
550	if (!idev)
551		return -ENOMEM;
 
 
 
 
 
552
553	idev->name = CROS_EC_DEV_NAME;
554	idev->phys = phys;
555	__set_bit(EV_REP, idev->evbit);
556
557	idev->id.bustype = BUS_VIRTUAL;
558	idev->id.version = 1;
559	idev->id.product = 0;
560	idev->dev.parent = dev;
 
 
561
562	ckdev->ghost_filter = of_property_read_bool(dev->of_node,
563					"google,needs-ghost-filter");
564
565	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
566					 NULL, idev);
567	if (err) {
568		dev_err(dev, "cannot build key matrix\n");
569		return err;
570	}
571
572	ckdev->row_shift = get_count_order(ckdev->cols);
573
574	input_set_capability(idev, EV_MSC, MSC_SCAN);
575	input_set_drvdata(idev, ckdev);
576	ckdev->idev = idev;
577	cros_ec_keyb_compute_valid_keys(ckdev);
578
579	err = input_register_device(ckdev->idev);
580	if (err) {
581		dev_err(dev, "cannot register input device\n");
582		return err;
583	}
584
585	return 0;
586}
587
588static int cros_ec_keyb_probe(struct platform_device *pdev)
589{
590	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
591	struct device *dev = &pdev->dev;
592	struct cros_ec_keyb *ckdev;
593	int err;
 
 
594
595	if (!dev->of_node)
596		return -ENODEV;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
598	ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
599	if (!ckdev)
600		return -ENOMEM;
601
602	ckdev->ec = ec;
603	ckdev->dev = dev;
604	dev_set_drvdata(dev, ckdev);
605
606	err = cros_ec_keyb_register_matrix(ckdev);
607	if (err) {
608		dev_err(dev, "cannot register matrix inputs: %d\n", err);
609		return err;
610	}
611
612	err = cros_ec_keyb_register_bs(ckdev);
613	if (err) {
614		dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
615		return err;
616	}
617
618	ckdev->notifier.notifier_call = cros_ec_keyb_work;
619	err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
620					       &ckdev->notifier);
621	if (err) {
622		dev_err(dev, "cannot register notifier: %d\n", err);
623		return err;
624	}
625
626	device_init_wakeup(ckdev->dev, true);
627	return 0;
628}
629
630static int cros_ec_keyb_remove(struct platform_device *pdev)
631{
632	struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
633
634	blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
635					   &ckdev->notifier);
636
637	return 0;
638}
639
640#ifdef CONFIG_OF
641static const struct of_device_id cros_ec_keyb_of_match[] = {
642	{ .compatible = "google,cros-ec-keyb" },
643	{},
644};
645MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
646#endif
647
648static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
649
650static struct platform_driver cros_ec_keyb_driver = {
651	.probe = cros_ec_keyb_probe,
652	.remove = cros_ec_keyb_remove,
653	.driver = {
654		.name = "cros-ec-keyb",
655		.of_match_table = of_match_ptr(cros_ec_keyb_of_match),
656		.pm = &cros_ec_keyb_pm_ops,
657	},
658};
659
660module_platform_driver(cros_ec_keyb_driver);
661
662MODULE_LICENSE("GPL v2");
663MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
664MODULE_ALIAS("platform:cros-ec-keyb");