File Index
Files and directories
Sign in
FILE

cros_ec_keyb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/input/keyboard/cros_ec_keyb.c C
  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->idev) {
265			dev_warn_once(ckdev->dev,
266				      "Unexpected key matrix event\n");
267			return NOTIFY_OK;
268		}
269
270		if (ckdev->ec->event_size != ckdev->cols) {
271			dev_err(ckdev->dev,
272				"Discarded incomplete key matrix event.\n");
273			return NOTIFY_OK;
274		}
275
276		cros_ec_keyb_process(ckdev,
277				     ckdev->ec->event_data.data.key_matrix,
278				     ckdev->ec->event_size);
279		break;
280
281	case EC_MKBP_EVENT_SYSRQ:
282		pm_wakeup_event(ckdev->dev, 0);
283
284		val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
285		dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
286		handle_sysrq(val);
287		break;
288
289	case EC_MKBP_EVENT_BUTTON:
290	case EC_MKBP_EVENT_SWITCH:
291		pm_wakeup_event(ckdev->dev, 0);
292
293		if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
294			val = get_unaligned_le32(
295					&ckdev->ec->event_data.data.buttons);
296			ev_type = EV_KEY;
297		} else {
298			val = get_unaligned_le32(
299					&ckdev->ec->event_data.data.switches);
300			ev_type = EV_SW;
301		}
302		cros_ec_keyb_report_bs(ckdev, ev_type, val);
303		break;
304
305	default:
306		return NOTIFY_DONE;
307	}
308
309	return NOTIFY_OK;
310}
311
312/*
313 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
314 * ghosting logic to ignore NULL or virtual keys.
315 */
316static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
317{
318	int row, col;
319	int row_shift = ckdev->row_shift;
320	unsigned short *keymap = ckdev->idev->keycode;
321	unsigned short code;
322
323	BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
324
325	for (col = 0; col < ckdev->cols; col++) {
326		for (row = 0; row < ckdev->rows; row++) {
327			code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
328			if (code && (code != KEY_BATTERY))
329				ckdev->valid_keys[col] |= 1 << row;
330		}
331		dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
332			col, ckdev->valid_keys[col]);
333	}
334}
335
336/**
337 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
338 *
339 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
340 * unmarshalling and different version nonsense into something simple.
341 *
342 * @ec_dev: The EC device
343 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
344 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH.  Actually
345 *              in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
346 *              EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
347 * @result: Where we'll store the result; a union
348 * @result_size: The size of the result.  Expected to be the size of one of
349 *               the elements in the union.
350 *
351 * Returns 0 if no error or -error upon error.
352 */
353static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
354			     enum ec_mkbp_info_type info_type,
355			     enum ec_mkbp_event event_type,
356			     union ec_response_get_next_data *result,
357			     size_t result_size)
358{
359	struct ec_params_mkbp_info *params;
360	struct cros_ec_command *msg;
361	int ret;
362
363	msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
364					   sizeof(*params)), GFP_KERNEL);
365	if (!msg)
366		return -ENOMEM;
367
368	msg->command = EC_CMD_MKBP_INFO;
369	msg->version = 1;
370	msg->outsize = sizeof(*params);
371	msg->insize = result_size;
372	params = (struct ec_params_mkbp_info *)msg->data;
373	params->info_type = info_type;
374	params->event_type = event_type;
375
376	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
377	if (ret == -ENOPROTOOPT) {
378		/* With older ECs we just return 0 for everything */
379		memset(result, 0, result_size);
380		ret = 0;
381	} else if (ret < 0) {
382		dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
383			 (int)info_type, (int)event_type, ret);
384	} else if (ret != result_size) {
385		dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
386			 (int)info_type, (int)event_type,
387			 ret, result_size);
388		ret = -EPROTO;
389	} else {
390		memcpy(result, msg->data, result_size);
391		ret = 0;
392	}
393
394	kfree(msg);
395
396	return ret;
397}
398
399/**
400 * cros_ec_keyb_query_switches - Query the state of switches and report
401 *
402 * This will ask the EC about the current state of switches and report to the
403 * kernel.  Note that we don't query for buttons because they are more
404 * transitory and we'll get an update on the next release / press.
405 *
406 * @ckdev: The keyboard device
407 *
408 * Returns 0 if no error or -error upon error.
409 */
410static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
411{
412	struct cros_ec_device *ec_dev = ckdev->ec;
413	union ec_response_get_next_data event_data = {};
414	int ret;
415
416	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
417				EC_MKBP_EVENT_SWITCH, &event_data,
418				sizeof(event_data.switches));
419	if (ret)
420		return ret;
421
422	cros_ec_keyb_report_bs(ckdev, EV_SW,
423			       get_unaligned_le32(&event_data.switches));
424
425	return 0;
426}
427
428/**
429 * cros_ec_keyb_resume - Resume the keyboard
430 *
431 * We use the resume notification as a chance to query the EC for switches.
432 *
433 * @dev: The keyboard device
434 *
435 * Returns 0 if no error or -error upon error.
436 */
437static int cros_ec_keyb_resume(struct device *dev)
438{
439	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
440
441	if (ckdev->bs_idev)
442		return cros_ec_keyb_query_switches(ckdev);
443
444	return 0;
445}
446
447/**
448 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
449 *
450 * Handles all the bits of the keyboard driver related to non-matrix buttons
451 * and switches, including asking the EC about which are present and telling
452 * the kernel to expect them.
453 *
454 * If this device has no support for buttons and switches we'll return no error
455 * but the ckdev->bs_idev will remain NULL when this function exits.
456 *
457 * @ckdev: The keyboard device
458 * @expect_buttons_switches: Indicates that EC must report button and/or
459 *   switch events
460 *
461 * Returns 0 if no error or -error upon error.
462 */
463static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
464				    bool expect_buttons_switches)
465{
466	struct cros_ec_device *ec_dev = ckdev->ec;
467	struct device *dev = ckdev->dev;
468	struct input_dev *idev;
469	union ec_response_get_next_data event_data = {};
470	const char *phys;
471	u32 buttons;
472	u32 switches;
473	int ret;
474	int i;
475
476	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
477				EC_MKBP_EVENT_BUTTON, &event_data,
478				sizeof(event_data.buttons));
479	if (ret)
480		return ret;
481	buttons = get_unaligned_le32(&event_data.buttons);
482
483	ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
484				EC_MKBP_EVENT_SWITCH, &event_data,
485				sizeof(event_data.switches));
486	if (ret)
487		return ret;
488	switches = get_unaligned_le32(&event_data.switches);
489
490	if (!buttons && !switches)
491		return expect_buttons_switches ? -EINVAL : 0;
492
493	/*
494	 * We call the non-matrix buttons/switches 'input1', if present.
495	 * Allocate phys before input dev, to ensure correct tear-down
496	 * ordering.
497	 */
498	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
499	if (!phys)
500		return -ENOMEM;
501
502	idev = devm_input_allocate_device(dev);
503	if (!idev)
504		return -ENOMEM;
505
506	idev->name = "cros_ec_buttons";
507	idev->phys = phys;
508	__set_bit(EV_REP, idev->evbit);
509
510	idev->id.bustype = BUS_VIRTUAL;
511	idev->id.version = 1;
512	idev->id.product = 0;
513	idev->dev.parent = dev;
514
515	input_set_drvdata(idev, ckdev);
516	ckdev->bs_idev = idev;
517
518	for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
519		const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
520
521		if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
522		    (map->ev_type == EV_SW && (switches & BIT(map->bit))))
523			input_set_capability(idev, map->ev_type, map->code);
524	}
525
526	ret = cros_ec_keyb_query_switches(ckdev);
527	if (ret) {
528		dev_err(dev, "cannot query switches\n");
529		return ret;
530	}
531
532	ret = input_register_device(ckdev->bs_idev);
533	if (ret) {
534		dev_err(dev, "cannot register input device\n");
535		return ret;
536	}
537
538	return 0;
539}
540
541static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
542{
543	u32 *physmap = ckdev->vdata.function_row_physmap;
544	unsigned int row, col, scancode;
545	int n_physmap;
546	int error;
547	int i;
548
549	n_physmap = device_property_count_u32(ckdev->dev,
550					      "function-row-physmap");
551	if (n_physmap <= 0)
552		return;
553
554	if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
555		dev_warn(ckdev->dev,
556			 "only up to %d top row keys is supported (%d specified)\n",
557			 VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
558		n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
559	}
560
561	error = device_property_read_u32_array(ckdev->dev,
562					       "function-row-physmap",
563					       physmap, n_physmap);
564	if (error) {
565		dev_warn(ckdev->dev,
566			 "failed to parse function-row-physmap property: %d\n",
567			 error);
568		return;
569	}
570
571	/*
572	 * Convert (in place) from row/column encoding to matrix "scancode"
573	 * used by the driver.
574	 */
575	for (i = 0; i < n_physmap; i++) {
576		row = KEY_ROW(physmap[i]);
577		col = KEY_COL(physmap[i]);
578		scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
579		physmap[i] = scancode;
580	}
581
582	ckdev->vdata.num_function_row_keys = n_physmap;
583}
584
585/**
586 * cros_ec_keyb_register_matrix - Register matrix keys
587 *
588 * Handles all the bits of the keyboard driver related to matrix keys.
589 *
590 * @ckdev: The keyboard device
591 *
592 * Returns 0 if no error or -error upon error.
593 */
594static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
595{
596	struct cros_ec_device *ec_dev = ckdev->ec;
597	struct device *dev = ckdev->dev;
598	struct input_dev *idev;
599	const char *phys;
600	int err;
601
602	err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
603	if (err)
604		return err;
605
606	ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
607	if (!ckdev->valid_keys)
608		return -ENOMEM;
609
610	ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
611	if (!ckdev->old_kb_state)
612		return -ENOMEM;
613
614	/*
615	 * We call the keyboard matrix 'input0'. Allocate phys before input
616	 * dev, to ensure correct tear-down ordering.
617	 */
618	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
619	if (!phys)
620		return -ENOMEM;
621
622	idev = devm_input_allocate_device(dev);
623	if (!idev)
624		return -ENOMEM;
625
626	idev->name = CROS_EC_DEV_NAME;
627	idev->phys = phys;
628	__set_bit(EV_REP, idev->evbit);
629
630	idev->id.bustype = BUS_VIRTUAL;
631	idev->id.version = 1;
632	idev->id.product = 0;
633	idev->dev.parent = dev;
634
635	ckdev->ghost_filter = device_property_read_bool(dev,
636					"google,needs-ghost-filter");
637
638	err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
639					 NULL, idev);
640	if (err) {
641		dev_err(dev, "cannot build key matrix\n");
642		return err;
643	}
644
645	ckdev->row_shift = get_count_order(ckdev->cols);
646
647	input_set_capability(idev, EV_MSC, MSC_SCAN);
648	input_set_drvdata(idev, ckdev);
649	ckdev->idev = idev;
650	cros_ec_keyb_compute_valid_keys(ckdev);
651	cros_ec_keyb_parse_vivaldi_physmap(ckdev);
652
653	err = input_register_device(ckdev->idev);
654	if (err) {
655		dev_err(dev, "cannot register input device\n");
656		return err;
657	}
658
659	return 0;
660}
661
662static ssize_t function_row_physmap_show(struct device *dev,
663					 struct device_attribute *attr,
664					 char *buf)
665{
666	const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
667	const struct vivaldi_data *data = &ckdev->vdata;
668
669	return vivaldi_function_row_physmap_show(data, buf);
670}
671
672static DEVICE_ATTR_RO(function_row_physmap);
673
674static struct attribute *cros_ec_keyb_attrs[] = {
675	&dev_attr_function_row_physmap.attr,
676	NULL,
677};
678
679static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
680					    struct attribute *attr,
681					    int n)
682{
683	struct device *dev = kobj_to_dev(kobj);
684	struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
685
686	if (attr == &dev_attr_function_row_physmap.attr &&
687	    !ckdev->vdata.num_function_row_keys)
688		return 0;
689
690	return attr->mode;
691}
692
693static const struct attribute_group cros_ec_keyb_group = {
694	.is_visible = cros_ec_keyb_attr_is_visible,
695	.attrs = cros_ec_keyb_attrs,
696};
697__ATTRIBUTE_GROUPS(cros_ec_keyb);
698
699static int cros_ec_keyb_probe(struct platform_device *pdev)
700{
701	struct cros_ec_device *ec;
702	struct device *dev = &pdev->dev;
703	struct cros_ec_keyb *ckdev;
704	bool buttons_switches_only = device_get_match_data(dev);
705	int err;
706
707	/*
708	 * If the parent ec device has not been probed yet, defer the probe of
709	 * this keyboard/button driver until later.
710	 */
711	ec = dev_get_drvdata(pdev->dev.parent);
712	if (!ec)
713		return -EPROBE_DEFER;
714	/*
715	 * Even if the cros_ec_device pointer is available, still need to check
716	 * if the device is fully registered before using it.
717	 */
718	if (!cros_ec_device_registered(ec))
719		return -EPROBE_DEFER;
720
721	ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
722	if (!ckdev)
723		return -ENOMEM;
724
725	ckdev->ec = ec;
726	ckdev->dev = dev;
727	dev_set_drvdata(dev, ckdev);
728
729	if (!buttons_switches_only) {
730		err = cros_ec_keyb_register_matrix(ckdev);
731		if (err) {
732			dev_err(dev, "cannot register matrix inputs: %d\n",
733				err);
734			return err;
735		}
736	}
737
738	err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
739	if (err) {
740		dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
741		return err;
742	}
743
744	ckdev->notifier.notifier_call = cros_ec_keyb_work;
745	err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
746					       &ckdev->notifier);
747	if (err) {
748		dev_err(dev, "cannot register notifier: %d\n", err);
749		return err;
750	}
751
752	device_init_wakeup(ckdev->dev, true);
753	return 0;
754}
755
756static void cros_ec_keyb_remove(struct platform_device *pdev)
757{
758	struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
759
760	blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
761					   &ckdev->notifier);
762}
763
764#ifdef CONFIG_ACPI
765static const struct acpi_device_id cros_ec_keyb_acpi_match[] = {
766	{ "GOOG0007", true },
767	{ }
768};
769MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match);
770#endif
771
772#ifdef CONFIG_OF
773static const struct of_device_id cros_ec_keyb_of_match[] = {
774	{ .compatible = "google,cros-ec-keyb" },
775	{ .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
776	{}
777};
778MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
779#endif
780
781static DEFINE_SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
782
783static struct platform_driver cros_ec_keyb_driver = {
784	.probe = cros_ec_keyb_probe,
785	.remove = cros_ec_keyb_remove,
786	.driver = {
787		.name = "cros-ec-keyb",
788		.dev_groups = cros_ec_keyb_groups,
789		.of_match_table = of_match_ptr(cros_ec_keyb_of_match),
790		.acpi_match_table = ACPI_PTR(cros_ec_keyb_acpi_match),
791		.pm = pm_sleep_ptr(&cros_ec_keyb_pm_ops),
792	},
793};
794
795module_platform_driver(cros_ec_keyb_driver);
796
797MODULE_LICENSE("GPL v2");
798MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
799MODULE_ALIAS("platform:cros-ec-keyb");
~/ Read-only file preview.