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");
v6.13.7
  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/acpi.h>
 16#include <linux/bitops.h>
 17#include <linux/i2c.h>
 18#include <linux/input.h>
 19#include <linux/input/vivaldi-fmap.h>
 20#include <linux/interrupt.h>
 21#include <linux/kernel.h>
 22#include <linux/notifier.h>
 23#include <linux/platform_device.h>
 24#include <linux/slab.h>
 25#include <linux/sysrq.h>
 26#include <linux/input/matrix_keypad.h>
 27#include <linux/platform_data/cros_ec_commands.h>
 28#include <linux/platform_data/cros_ec_proto.h>
 29
 30#include <linux/unaligned.h>
 31
 32/**
 33 * struct cros_ec_keyb - Structure representing EC keyboard device
 34 *
 35 * @rows: Number of rows in the keypad
 36 * @cols: Number of columns in the keypad
 37 * @row_shift: log2 or number of rows, rounded up
 
 38 * @ghost_filter: true to enable the matrix key-ghosting filter
 39 * @valid_keys: bitmap of existing keys for each matrix column
 40 * @old_kb_state: bitmap of keys pressed last scan
 41 * @dev: Device pointer
 
 42 * @ec: Top level ChromeOS device to use to talk to EC
 43 * @idev: The input device for the matrix keys.
 44 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
 45 * @notifier: interrupt event notifier for transport devices
 46 * @vdata: vivaldi function row data
 47 */
 48struct cros_ec_keyb {
 49	unsigned int rows;
 50	unsigned int cols;
 51	int row_shift;
 
 52	bool ghost_filter;
 53	uint8_t *valid_keys;
 54	uint8_t *old_kb_state;
 55
 56	struct device *dev;
 
 57	struct cros_ec_device *ec;
 58
 59	struct input_dev *idev;
 60	struct input_dev *bs_idev;
 61	struct notifier_block notifier;
 62
 63	struct vivaldi_data vdata;
 64};
 65
 66/**
 67 * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch
 68 *	bitmap #defines
 69 *
 70 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
 71 * @code: A linux keycode
 72 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
 73 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
 74 *            Only applicable to switches.
 75 */
 76struct cros_ec_bs_map {
 77	unsigned int ev_type;
 78	unsigned int code;
 79	u8 bit;
 80	bool inverted;
 81};
 82
 83/* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
 84static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
 85	/* Buttons */
 86	{
 87		.ev_type	= EV_KEY,
 88		.code		= KEY_POWER,
 89		.bit		= EC_MKBP_POWER_BUTTON,
 90	},
 91	{
 92		.ev_type	= EV_KEY,
 93		.code		= KEY_VOLUMEUP,
 94		.bit		= EC_MKBP_VOL_UP,
 95	},
 96	{
 97		.ev_type	= EV_KEY,
 98		.code		= KEY_VOLUMEDOWN,
 99		.bit		= EC_MKBP_VOL_DOWN,
100	},
101	{
102		.ev_type        = EV_KEY,
103		.code           = KEY_BRIGHTNESSUP,
104		.bit            = EC_MKBP_BRI_UP,
105	},
106	{
107		.ev_type        = EV_KEY,
108		.code           = KEY_BRIGHTNESSDOWN,
109		.bit            = EC_MKBP_BRI_DOWN,
110	},
111	{
112		.ev_type        = EV_KEY,
113		.code           = KEY_SCREENLOCK,
114		.bit            = EC_MKBP_SCREEN_LOCK,
115	},
116
117	/* Switches */
118	{
119		.ev_type	= EV_SW,
120		.code		= SW_LID,
121		.bit		= EC_MKBP_LID_OPEN,
122		.inverted	= true,
123	},
124	{
125		.ev_type	= EV_SW,
126		.code		= SW_TABLET_MODE,
127		.bit		= EC_MKBP_TABLET_MODE,
128	},
129};
130
131/*
132 * Returns true when there is at least one combination of pressed keys that
133 * results in ghosting.
134 */
135static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
136{
137	int col1, col2, buf1, buf2;
138	struct device *dev = ckdev->dev;
139	uint8_t *valid_keys = ckdev->valid_keys;
140
141	/*
142	 * Ghosting happens if for any pressed key X there are other keys
143	 * pressed both in the same row and column of X as, for instance,
144	 * in the following diagram:
145	 *
146	 * . . Y . g .
147	 * . . . . . .
148	 * . . . . . .
149	 * . . X . Z .
150	 *
151	 * In this case only X, Y, and Z are pressed, but g appears to be
152	 * pressed too (see Wikipedia).
153	 */
154	for (col1 = 0; col1 < ckdev->cols; col1++) {
155		buf1 = buf[col1] & valid_keys[col1];
156		for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
157			buf2 = buf[col2] & valid_keys[col2];
158			if (hweight8(buf1 & buf2) > 1) {
159				dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
160					col1, buf1, col2, buf2);
161				return true;
162			}
163		}
164	}
165
166	return false;
167}
168
169
170/*
171 * Compares the new keyboard state to the old one and produces key
172 * press/release events accordingly.  The keyboard state is 13 bytes (one byte
173 * per column)
174 */
175static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
176			 uint8_t *kb_state, int len)
177{
178	struct input_dev *idev = ckdev->idev;
179	int col, row;
180	int new_state;
181	int old_state;
 
 
 
182
183	if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
184		/*
185		 * Simple-minded solution: ignore this state. The obvious
186		 * improvement is to only ignore changes to keys involved in
187		 * the ghosting, but process the other changes.
188		 */
189		dev_dbg(ckdev->dev, "ghosting found\n");
190		return;
191	}
192
193	for (col = 0; col < ckdev->cols; col++) {
194		for (row = 0; row < ckdev->rows; row++) {
195			int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
196			const unsigned short *keycodes = idev->keycode;
197
198			new_state = kb_state[col] & (1 << row);
199			old_state = ckdev->old_kb_state[col] & (1 << row);
200			if (new_state != old_state) {
201				dev_dbg(ckdev->dev,
202					"changed: [r%d c%d]: byte %02x\n",
203					row, col, new_state);
204
205				input_event(idev, EV_MSC, MSC_SCAN, pos);
206				input_report_key(idev, keycodes[pos],
207						 new_state);
208			}
209		}
210		ckdev->old_kb_state[col] = kb_state[col];
211	}
212	input_sync(ckdev->idev);
213}
214
215/**
216 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
217 *
218 * This takes a bitmap of buttons or switches from the EC and reports events,
219 * syncing at the end.
220 *
221 * @ckdev: The keyboard device.
222 * @ev_type: The input event type (e.g., EV_KEY).
223 * @mask: A bitmap of buttons from the EC.
224 */
225static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
226				   unsigned int ev_type, u32 mask)
227
228{
229	struct input_dev *idev = ckdev->bs_idev;
230	int i;
231
232	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
233		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
 
234
235		if (map->ev_type != ev_type)
236			continue;
 
 
 
 
 
 
 
 
237
238		input_event(idev, ev_type, map->code,
239			    !!(mask & BIT(map->bit)) ^ map->inverted);
240	}
241	input_sync(idev);
242}
243
244static int cros_ec_keyb_work(struct notifier_block *nb,
245			     unsigned long queued_during_suspend, void *_notify)
246{
247	struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
248						  notifier);
249	u32 val;
250	unsigned int ev_type;
 
 
 
251
252	/*
253	 * If not wake enabled, discard key state changes during
254	 * suspend. Switches will be re-checked in
255	 * cros_ec_keyb_resume() to be sure nothing is lost.
256	 */
257	if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
258		return NOTIFY_OK;
 
259
260	switch (ckdev->ec->event_data.event_type) {
261	case EC_MKBP_EVENT_KEY_MATRIX:
262		pm_wakeup_event(ckdev->dev, 0);
263
264		if (ckdev->ec->event_size != ckdev->cols) {
265			dev_err(ckdev->dev,
266				"Discarded incomplete key matrix event.\n");
267			return NOTIFY_OK;
268		}
269
270		cros_ec_keyb_process(ckdev,
271				     ckdev->ec->event_data.data.key_matrix,
272				     ckdev->ec->event_size);
273		break;
274
275	case EC_MKBP_EVENT_SYSRQ:
276		pm_wakeup_event(ckdev->dev, 0);
277
278		val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
279		dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
280		handle_sysrq(val);
281		break;
282
283	case EC_MKBP_EVENT_BUTTON:
284	case EC_MKBP_EVENT_SWITCH:
285		pm_wakeup_event(ckdev->dev, 0);
286
287		if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
288			val = get_unaligned_le32(
289					&ckdev->ec->event_data.data.buttons);
290			ev_type = EV_KEY;
291		} else {
292			val = get_unaligned_le32(
293					&ckdev->ec->event_data.data.switches);
294			ev_type = EV_SW;
295		}
296		cros_ec_keyb_report_bs(ckdev, ev_type, val);
297		break;
298
299	default:
300		return NOTIFY_DONE;
301	}
 
302
303	return NOTIFY_OK;
304}
305
306/*
307 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
308 * ghosting logic to ignore NULL or virtual keys.
309 */
310static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
311{
312	int row, col;
313	int row_shift = ckdev->row_shift;
314	unsigned short *keymap = ckdev->idev->keycode;
315	unsigned short code;
316
317	BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
318
319	for (col = 0; col < ckdev->cols; col++) {
320		for (row = 0; row < ckdev->rows; row++) {
321			code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
322			if (code && (code != KEY_BATTERY))
323				ckdev->valid_keys[col] |= 1 << row;
324		}
325		dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
326			col, ckdev->valid_keys[col]);
327	}
328}
329
330/**
331 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
332 *
333 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
334 * unmarshalling and different version nonsense into something simple.
335 *
336 * @ec_dev: The EC device
337 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
338 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH.  Actually
339 *              in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
340 *              EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
341 * @result: Where we'll store the result; a union
342 * @result_size: The size of the result.  Expected to be the size of one of
343 *               the elements in the union.
344 *
345 * Returns 0 if no error or -error upon error.
346 */
347static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
348			     enum ec_mkbp_info_type info_type,
349			     enum ec_mkbp_event event_type,
350			     union ec_response_get_next_data *result,
351			     size_t result_size)
352{
353	struct ec_params_mkbp_info *params;
354	struct cros_ec_command *msg;
355	int ret;
356
357	msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
358					   sizeof(*params)), GFP_KERNEL);
359	if (!msg)
360		return -ENOMEM;
361
362	msg->command = EC_CMD_MKBP_INFO;
363	msg->version = 1;
364	msg->outsize = sizeof(*params);
365	msg->insize = result_size;
366	params = (struct ec_params_mkbp_info *)msg->data;
367	params->info_type = info_type;
368	params->event_type = event_type;
369
370	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
371	if (ret == -ENOPROTOOPT) {
372		/* With older ECs we just return 0 for everything */
373		memset(result, 0, result_size);
374		ret = 0;
375	} else if (ret < 0) {
376		dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
377			 (int)info_type, (int)event_type, ret);
378	} else if (ret != result_size) {
379		dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
380			 (int)info_type, (int)event_type,
381			 ret, result_size);
382		ret = -EPROTO;
383	} else {
384		memcpy(result, msg->data, result_size);
385		ret = 0;
386	}
387
388	kfree(msg);
389
390	return ret;
391}
392
393/**
394 * cros_ec_keyb_query_switches - Query the state of switches and report
395 *
396 * This will ask the EC about the current state of switches and report to the
397 * kernel.  Note that we don't query for buttons because they are more
398 * transitory and we'll get an update on the next release / press.
399 *
400 * @ckdev: The keyboard device
401 *
402 * Returns 0 if no error or -error upon error.
403 */
404static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
405{
406	struct cros_ec_device *ec_dev = ckdev->ec;
407	union ec_response_get_next_data event_data = {};
408	int ret;
409
410	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
411				EC_MKBP_EVENT_SWITCH, &event_data,
412				sizeof(event_data.switches));
413	if (ret)
414		return ret;
415
416	cros_ec_keyb_report_bs(ckdev, EV_SW,
417			       get_unaligned_le32(&event_data.switches));
418
419	return 0;
420}
421
422/**
423 * cros_ec_keyb_resume - Resume the keyboard
424 *
425 * We use the resume notification as a chance to query the EC for switches.
426 *
427 * @dev: The keyboard device
428 *
429 * Returns 0 if no error or -error upon error.
430 */
431static int cros_ec_keyb_resume(struct device *dev)
432{
433	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
434
435	if (ckdev->bs_idev)
436		return cros_ec_keyb_query_switches(ckdev);
437
438	return 0;
439}
440
441/**
442 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
443 *
444 * Handles all the bits of the keyboard driver related to non-matrix buttons
445 * and switches, including asking the EC about which are present and telling
446 * the kernel to expect them.
447 *
448 * If this device has no support for buttons and switches we'll return no error
449 * but the ckdev->bs_idev will remain NULL when this function exits.
450 *
451 * @ckdev: The keyboard device
452 * @expect_buttons_switches: Indicates that EC must report button and/or
453 *   switch events
454 *
455 * Returns 0 if no error or -error upon error.
456 */
457static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
458				    bool expect_buttons_switches)
459{
460	struct cros_ec_device *ec_dev = ckdev->ec;
461	struct device *dev = ckdev->dev;
462	struct input_dev *idev;
463	union ec_response_get_next_data event_data = {};
464	const char *phys;
465	u32 buttons;
466	u32 switches;
467	int ret;
468	int i;
469
470	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
471				EC_MKBP_EVENT_BUTTON, &event_data,
472				sizeof(event_data.buttons));
473	if (ret)
474		return ret;
475	buttons = get_unaligned_le32(&event_data.buttons);
476
477	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
478				EC_MKBP_EVENT_SWITCH, &event_data,
479				sizeof(event_data.switches));
480	if (ret)
481		return ret;
482	switches = get_unaligned_le32(&event_data.switches);
483
484	if (!buttons && !switches)
485		return expect_buttons_switches ? -EINVAL : 0;
486
487	/*
488	 * We call the non-matrix buttons/switches 'input1', if present.
489	 * Allocate phys before input dev, to ensure correct tear-down
490	 * ordering.
491	 */
492	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
493	if (!phys)
494		return -ENOMEM;
495
496	idev = devm_input_allocate_device(dev);
497	if (!idev)
498		return -ENOMEM;
499
500	idev->name = "cros_ec_buttons";
501	idev->phys = phys;
502	__set_bit(EV_REP, idev->evbit);
503
504	idev->id.bustype = BUS_VIRTUAL;
505	idev->id.version = 1;
506	idev->id.product = 0;
507	idev->dev.parent = dev;
508
509	input_set_drvdata(idev, ckdev);
510	ckdev->bs_idev = idev;
511
512	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
513		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
514
515		if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
516		    (map->ev_type == EV_SW && (switches & BIT(map->bit))))
517			input_set_capability(idev, map->ev_type, map->code);
518	}
519
520	ret = cros_ec_keyb_query_switches(ckdev);
521	if (ret) {
522		dev_err(dev, "cannot query switches\n");
523		return ret;
524	}
525
526	ret = input_register_device(ckdev->bs_idev);
527	if (ret) {
528		dev_err(dev, "cannot register input device\n");
529		return ret;
530	}
531
532	return 0;
533}
534
535static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
536{
537	u32 *physmap = ckdev->vdata.function_row_physmap;
538	unsigned int row, col, scancode;
539	int n_physmap;
540	int error;
541	int i;
542
543	n_physmap = device_property_count_u32(ckdev->dev,
544					      "function-row-physmap");
545	if (n_physmap <= 0)
546		return;
547
548	if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
549		dev_warn(ckdev->dev,
550			 "only up to %d top row keys is supported (%d specified)\n",
551			 VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
552		n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
553	}
554
555	error = device_property_read_u32_array(ckdev->dev,
556					       "function-row-physmap",
557					       physmap, n_physmap);
558	if (error) {
559		dev_warn(ckdev->dev,
560			 "failed to parse function-row-physmap property: %d\n",
561			 error);
562		return;
563	}
564
565	/*
566	 * Convert (in place) from row/column encoding to matrix "scancode"
567	 * used by the driver.
568	 */
569	for (i = 0; i < n_physmap; i++) {
570		row = KEY_ROW(physmap[i]);
571		col = KEY_COL(physmap[i]);
572		scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
573		physmap[i] = scancode;
574	}
575
576	ckdev->vdata.num_function_row_keys = n_physmap;
577}
578
579/**
580 * cros_ec_keyb_register_matrix - Register matrix keys
581 *
582 * Handles all the bits of the keyboard driver related to matrix keys.
583 *
584 * @ckdev: The keyboard device
585 *
586 * Returns 0 if no error or -error upon error.
587 */
588static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
589{
590	struct cros_ec_device *ec_dev = ckdev->ec;
591	struct device *dev = ckdev->dev;
592	struct input_dev *idev;
593	const char *phys;
594	int err;
595
596	err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
597	if (err)
598		return err;
599
600	ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
601	if (!ckdev->valid_keys)
602		return -ENOMEM;
603
604	ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
605	if (!ckdev->old_kb_state)
606		return -ENOMEM;
607
608	/*
609	 * We call the keyboard matrix 'input0'. Allocate phys before input
610	 * dev, to ensure correct tear-down ordering.
611	 */
612	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
613	if (!phys)
614		return -ENOMEM;
615
616	idev = devm_input_allocate_device(dev);
617	if (!idev)
618		return -ENOMEM;
 
 
 
 
 
619
620	idev->name = CROS_EC_DEV_NAME;
621	idev->phys = phys;
622	__set_bit(EV_REP, idev->evbit);
623
624	idev->id.bustype = BUS_VIRTUAL;
625	idev->id.version = 1;
626	idev->id.product = 0;
627	idev->dev.parent = dev;
 
 
628
629	ckdev->ghost_filter = device_property_read_bool(dev,
630					"google,needs-ghost-filter");
631
632	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
633					 NULL, idev);
634	if (err) {
635		dev_err(dev, "cannot build key matrix\n");
636		return err;
637	}
638
639	ckdev->row_shift = get_count_order(ckdev->cols);
640
641	input_set_capability(idev, EV_MSC, MSC_SCAN);
642	input_set_drvdata(idev, ckdev);
643	ckdev->idev = idev;
644	cros_ec_keyb_compute_valid_keys(ckdev);
645	cros_ec_keyb_parse_vivaldi_physmap(ckdev);
646
647	err = input_register_device(ckdev->idev);
648	if (err) {
649		dev_err(dev, "cannot register input device\n");
650		return err;
651	}
652
653	return 0;
654}
655
656static ssize_t function_row_physmap_show(struct device *dev,
657					 struct device_attribute *attr,
658					 char *buf)
659{
660	const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
661	const struct vivaldi_data *data = &ckdev->vdata;
 
 
662
663	return vivaldi_function_row_physmap_show(data, buf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664}
665
666static DEVICE_ATTR_RO(function_row_physmap);
667
668static struct attribute *cros_ec_keyb_attrs[] = {
669	&dev_attr_function_row_physmap.attr,
670	NULL,
671};
672
673static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
674					    struct attribute *attr,
675					    int n)
676{
677	struct device *dev = kobj_to_dev(kobj);
678	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
679
680	if (attr == &dev_attr_function_row_physmap.attr &&
681	    !ckdev->vdata.num_function_row_keys)
682		return 0;
683
684	return attr->mode;
685}
686
687static const struct attribute_group cros_ec_keyb_group = {
688	.is_visible = cros_ec_keyb_attr_is_visible,
689	.attrs = cros_ec_keyb_attrs,
690};
691__ATTRIBUTE_GROUPS(cros_ec_keyb);
692
693static int cros_ec_keyb_probe(struct platform_device *pdev)
694{
695	struct cros_ec_device *ec;
696	struct device *dev = &pdev->dev;
697	struct cros_ec_keyb *ckdev;
698	bool buttons_switches_only = device_get_match_data(dev);
699	int err;
700
701	/*
702	 * If the parent ec device has not been probed yet, defer the probe of
703	 * this keyboard/button driver until later.
 
 
704	 */
705	ec = dev_get_drvdata(pdev->dev.parent);
706	if (!ec)
707		return -EPROBE_DEFER;
708
709	ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
710	if (!ckdev)
711		return -ENOMEM;
712
713	ckdev->ec = ec;
714	ckdev->dev = dev;
715	dev_set_drvdata(dev, ckdev);
716
717	if (!buttons_switches_only) {
718		err = cros_ec_keyb_register_matrix(ckdev);
719		if (err) {
720			dev_err(dev, "cannot register matrix inputs: %d\n",
721				err);
722			return err;
723		}
724	}
725
726	err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
727	if (err) {
728		dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
729		return err;
730	}
731
732	ckdev->notifier.notifier_call = cros_ec_keyb_work;
733	err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
734					       &ckdev->notifier);
735	if (err) {
736		dev_err(dev, "cannot register notifier: %d\n", err);
737		return err;
738	}
739
740	device_init_wakeup(ckdev->dev, true);
741	return 0;
742}
743
744static void cros_ec_keyb_remove(struct platform_device *pdev)
745{
746	struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
747
748	blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
749					   &ckdev->notifier);
750}
751
752#ifdef CONFIG_ACPI
753static const struct acpi_device_id cros_ec_keyb_acpi_match[] = {
754	{ "GOOG0007", true },
755	{ }
756};
757MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match);
758#endif
759
760#ifdef CONFIG_OF
761static const struct of_device_id cros_ec_keyb_of_match[] = {
762	{ .compatible = "google,cros-ec-keyb" },
763	{ .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
764	{}
765};
766MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
767#endif
768
769static DEFINE_SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
770
771static struct platform_driver cros_ec_keyb_driver = {
772	.probe = cros_ec_keyb_probe,
773	.remove = cros_ec_keyb_remove,
774	.driver = {
775		.name = "cros-ec-keyb",
776		.dev_groups = cros_ec_keyb_groups,
777		.of_match_table = of_match_ptr(cros_ec_keyb_of_match),
778		.acpi_match_table = ACPI_PTR(cros_ec_keyb_acpi_match),
779		.pm = pm_sleep_ptr(&cros_ec_keyb_pm_ops),
780	},
781};
782
783module_platform_driver(cros_ec_keyb_driver);
784
785MODULE_LICENSE("GPL v2");
786MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
787MODULE_ALIAS("platform:cros-ec-keyb");