2 * ChromeOS EC keyboard driver
4 * Copyright (C) 2012 Google, Inc
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.
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.
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
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>
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
46 * @ec: Top level ChromeOS device to use to talk to EC
52 const struct matrix_keymap_data
*keymap_data
;
55 uint8_t *old_kb_state
;
58 struct input_dev
*idev
;
59 struct cros_ec_device
*ec
;
64 * Returns true when there is at least one combination of pressed keys that
65 * results in ghosting.
67 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb
*ckdev
, uint8_t *buf
)
69 int col1
, col2
, buf1
, buf2
;
70 struct device
*dev
= ckdev
->dev
;
71 uint8_t *valid_keys
= ckdev
->valid_keys
;
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:
83 * In this case only X, Y, and Z are pressed, but g appears to be
84 * pressed too (see Wikipedia).
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
);
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
107 static void cros_ec_keyb_process(struct cros_ec_keyb
*ckdev
,
108 uint8_t *kb_state
, int len
)
110 struct input_dev
*idev
= ckdev
->idev
;
118 if (ckdev
->ghost_filter
&& cros_ec_keyb_has_ghosting(ckdev
, kb_state
)) {
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.
124 dev_dbg(ckdev
->dev
, "ghosting found\n");
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
;
133 new_state
= kb_state
[col
] & (1 << row
);
134 old_state
= ckdev
->old_kb_state
[col
] & (1 << row
);
135 if (new_state
!= old_state
) {
137 "changed: [r%d c%d]: byte %02x\n",
138 row
, col
, new_state
);
140 input_report_key(idev
, keycodes
[pos
],
144 ckdev
->old_kb_state
[col
] = kb_state
[col
];
146 input_sync(ckdev
->idev
);
149 static int cros_ec_keyb_get_state(struct cros_ec_keyb
*ckdev
, uint8_t *kb_state
)
152 struct cros_ec_command msg
= {
153 .command
= EC_CMD_MKBP_STATE
,
154 .insize
= ckdev
->cols
,
157 ret
= cros_ec_cmd_xfer(ckdev
->ec
, &msg
);
161 memcpy(kb_state
, msg
.indata
, ckdev
->cols
);
166 static irqreturn_t
cros_ec_keyb_irq(int irq
, void *data
)
168 struct cros_ec_keyb
*ckdev
= data
;
169 struct cros_ec_device
*ec
= ckdev
->ec
;
171 uint8_t kb_state
[ckdev
->cols
];
173 if (device_may_wakeup(ec
->dev
))
174 pm_wakeup_event(ec
->dev
, 0);
176 ret
= cros_ec_keyb_get_state(ckdev
, kb_state
);
178 cros_ec_keyb_process(ckdev
, kb_state
, ret
);
180 dev_err(ec
->dev
, "failed to get keyboard state: %d\n", ret
);
185 static int cros_ec_keyb_open(struct input_dev
*dev
)
187 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
188 struct cros_ec_device
*ec
= ckdev
->ec
;
190 return request_threaded_irq(ec
->irq
, NULL
, cros_ec_keyb_irq
,
191 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
192 "cros_ec_keyb", ckdev
);
195 static void cros_ec_keyb_close(struct input_dev
*dev
)
197 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
198 struct cros_ec_device
*ec
= ckdev
->ec
;
200 free_irq(ec
->irq
, ckdev
);
204 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
205 * ghosting logic to ignore NULL or virtual keys.
207 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb
*ckdev
)
210 int row_shift
= ckdev
->row_shift
;
211 unsigned short *keymap
= ckdev
->idev
->keycode
;
214 BUG_ON(ckdev
->idev
->keycodesize
!= sizeof(*keymap
));
216 for (col
= 0; col
< ckdev
->cols
; col
++) {
217 for (row
= 0; row
< ckdev
->rows
; row
++) {
218 code
= keymap
[MATRIX_SCAN_CODE(row
, col
, row_shift
)];
219 if (code
&& (code
!= KEY_BATTERY
))
220 ckdev
->valid_keys
[col
] |= 1 << row
;
222 dev_dbg(ckdev
->dev
, "valid_keys[%02d] = 0x%02x\n",
223 col
, ckdev
->valid_keys
[col
]);
227 static int cros_ec_keyb_probe(struct platform_device
*pdev
)
229 struct cros_ec_device
*ec
= dev_get_drvdata(pdev
->dev
.parent
);
230 struct device
*dev
= ec
->dev
;
231 struct cros_ec_keyb
*ckdev
;
232 struct input_dev
*idev
;
233 struct device_node
*np
;
236 np
= pdev
->dev
.of_node
;
240 ckdev
= devm_kzalloc(&pdev
->dev
, sizeof(*ckdev
), GFP_KERNEL
);
243 err
= matrix_keypad_parse_of_params(&pdev
->dev
, &ckdev
->rows
,
248 ckdev
->valid_keys
= devm_kzalloc(&pdev
->dev
, ckdev
->cols
, GFP_KERNEL
);
249 if (!ckdev
->valid_keys
)
252 ckdev
->old_kb_state
= devm_kzalloc(&pdev
->dev
, ckdev
->cols
, GFP_KERNEL
);
253 if (!ckdev
->old_kb_state
)
256 idev
= devm_input_allocate_device(&pdev
->dev
);
261 dev_err(dev
, "no EC IRQ specified\n");
267 dev_set_drvdata(&pdev
->dev
, ckdev
);
269 idev
->name
= ec
->ec_name
;
270 idev
->phys
= ec
->phys_name
;
271 __set_bit(EV_REP
, idev
->evbit
);
273 idev
->id
.bustype
= BUS_VIRTUAL
;
274 idev
->id
.version
= 1;
275 idev
->id
.product
= 0;
276 idev
->dev
.parent
= &pdev
->dev
;
277 idev
->open
= cros_ec_keyb_open
;
278 idev
->close
= cros_ec_keyb_close
;
280 ckdev
->ghost_filter
= of_property_read_bool(np
,
281 "google,needs-ghost-filter");
283 err
= matrix_keypad_build_keymap(NULL
, NULL
, ckdev
->rows
, ckdev
->cols
,
286 dev_err(dev
, "cannot build key matrix\n");
290 ckdev
->row_shift
= get_count_order(ckdev
->cols
);
292 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
293 input_set_drvdata(idev
, ckdev
);
295 cros_ec_keyb_compute_valid_keys(ckdev
);
297 err
= input_register_device(ckdev
->idev
);
299 dev_err(dev
, "cannot register input device\n");
306 #ifdef CONFIG_PM_SLEEP
307 /* Clear any keys in the buffer */
308 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb
*ckdev
)
310 uint8_t old_state
[ckdev
->cols
];
311 uint8_t new_state
[ckdev
->cols
];
312 unsigned long duration
;
316 * Keep reading until we see that the scan state does not change.
317 * That indicates that we are done.
319 * Assume that the EC keyscan buffer is at most 32 deep.
322 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
323 for (i
= 1; !ret
&& i
< 32; i
++) {
324 memcpy(old_state
, new_state
, sizeof(old_state
));
325 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
326 if (0 == memcmp(old_state
, new_state
, sizeof(old_state
)))
329 duration
= jiffies
- duration
;
330 dev_info(ckdev
->dev
, "Discarded %d keyscan(s) in %dus\n", i
,
331 jiffies_to_usecs(duration
));
334 static int cros_ec_keyb_resume(struct device
*dev
)
336 struct cros_ec_keyb
*ckdev
= dev_get_drvdata(dev
);
339 * When the EC is not a wake source, then it could not have caused the
340 * resume, so we clear the EC's key scan buffer. If the EC was a
341 * wake source (e.g. the lid is open and the user might press a key to
342 * wake) then the key scan buffer should be preserved.
344 if (!ckdev
->ec
->was_wake_device
)
345 cros_ec_keyb_clear_keyboard(ckdev
);
352 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops
, NULL
, cros_ec_keyb_resume
);
355 static const struct of_device_id cros_ec_keyb_of_match
[] = {
356 { .compatible
= "google,cros-ec-keyb" },
359 MODULE_DEVICE_TABLE(of
, cros_ec_keyb_of_match
);
362 static struct platform_driver cros_ec_keyb_driver
= {
363 .probe
= cros_ec_keyb_probe
,
365 .name
= "cros-ec-keyb",
366 .of_match_table
= of_match_ptr(cros_ec_keyb_of_match
),
367 .pm
= &cros_ec_keyb_pm_ops
,
371 module_platform_driver(cros_ec_keyb_driver
);
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
375 MODULE_ALIAS("platform:cros-ec-keyb");