X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=drivers%2Finput%2Fcros_ec_keyb.c;h=6f12ec8b0ac438cc4bf83bb2bd07185bf2e3bfbb;hb=b043597673855932bb824b3b4d73935ef47094bd;hp=e8dac237a995c0f492d3299921cc6f4fbbaf0f80;hpb=aaf5e825606a70ddc8fca8e366d8c16a6fd3cc7c;p=oweals%2Fu-boot.git diff --git a/drivers/input/cros_ec_keyb.c b/drivers/input/cros_ec_keyb.c index e8dac237a9..6f12ec8b0a 100644 --- a/drivers/input/cros_ec_keyb.c +++ b/drivers/input/cros_ec_keyb.c @@ -1,62 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0+ /* * Chromium OS Matrix Keyboard * * Copyright (c) 2012 The Chromium OS Authors. - * - * SPDX-License-Identifier: GPL-2.0+ */ #include #include -#include +#include +#include #include +#include #include +#include #include -DECLARE_GLOBAL_DATA_PTR; - enum { KBC_MAX_KEYS = 8, /* Maximum keys held down at once */ + KBC_REPEAT_RATE_MS = 30, + KBC_REPEAT_DELAY_MS = 240, }; -static struct keyb { - struct cros_ec_dev *dev; /* The CROS_EC device */ - struct input_config input; /* The input layer */ +struct cros_ec_keyb_priv { + struct input_config *input; /* The input layer */ struct key_matrix matrix; /* The key matrix layer */ int key_rows; /* Number of keyboard rows */ int key_cols; /* Number of keyboard columns */ - unsigned int repeat_delay_ms; /* Time before autorepeat starts */ - unsigned int repeat_rate_ms; /* Autorepeat rate in ms */ int ghost_filter; /* 1 to enable ghost filter, else 0 */ - int inited; /* 1 if keyboard is ready */ -} config; +}; /** * Check the keyboard controller and return a list of key matrix positions * for which a key is pressed * - * @param config Keyboard config + * @param dev Keyboard device * @param keys List of keys that we have detected * @param max_count Maximum number of keys to return - * @return number of pressed keys, 0 for none + * @param samep Set to true if this scan repeats the last, else false + * @return number of pressed keys, 0 for none, -EIO on error */ -static int check_for_keys(struct keyb *config, - struct key_matrix_key *keys, int max_count) +static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys, + int max_count, bool *samep) { + struct cros_ec_keyb_priv *priv = dev_get_priv(dev); struct key_matrix_key *key; + static struct mbkp_keyscan last_scan; + static bool last_scan_valid; struct mbkp_keyscan scan; unsigned int row, col, bit, data; int num_keys; - if (cros_ec_scan_keyboard(config->dev, &scan)) { + if (cros_ec_scan_keyboard(dev->parent, &scan)) { debug("%s: keyboard scan failed\n", __func__); - return -1; + return -EIO; } + *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan)); + + /* + * This is a bit odd. The EC has no way to tell us that it has run + * out of key scans. It just returns the same scan over and over + * again. So the only way to detect that we have run out is to detect + * that this scan is the same as the last. + */ + last_scan_valid = true; + memcpy(&last_scan, &scan, sizeof(last_scan)); - for (col = num_keys = bit = 0; col < config->matrix.num_cols; + for (col = num_keys = bit = 0; col < priv->matrix.num_cols; col++) { - for (row = 0; row < config->matrix.num_rows; row++) { + for (row = 0; row < priv->matrix.num_rows; row++) { unsigned int mask = 1 << (bit & 7); data = scan.data[bit / 8]; @@ -73,28 +85,6 @@ static int check_for_keys(struct keyb *config, return num_keys; } -/** - * Test if keys are available to be read - * - * @return 0 if no keys available, 1 if keys are available - */ -static int kbd_tstc(void) -{ - /* Just get input to do this for us */ - return config.inited ? input_tstc(&config.input) : 0; -} - -/** - * Read a key - * - * @return ASCII key code, or 0 if no key, or -1 if error - */ -static int kbd_getc(void) -{ - /* Just get input to do this for us */ - return config.inited ? input_getc(&config.input) : 0; -} - /** * Check the keyboard, and send any keys that are pressed. * @@ -106,12 +96,15 @@ static int kbd_getc(void) */ int cros_ec_kbc_check(struct input_config *input) { + struct udevice *dev = input->dev; + struct cros_ec_keyb_priv *priv = dev_get_priv(dev); static struct key_matrix_key last_keys[KBC_MAX_KEYS]; static int last_num_keys; struct key_matrix_key keys[KBC_MAX_KEYS]; int keycodes[KBC_MAX_KEYS]; int num_keys, num_keycodes; int irq_pending, sent; + bool same = false; /* * Loop until the EC has no more keyscan records, or we have @@ -123,9 +116,12 @@ int cros_ec_kbc_check(struct input_config *input) * may return 0 before all keys have been read from the EC. */ do { - irq_pending = cros_ec_interrupt_pending(config.dev); + irq_pending = cros_ec_interrupt_pending(dev->parent); if (irq_pending) { - num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS); + num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS, + &same); + if (num_keys < 0) + return 0; last_num_keys = num_keys; memcpy(last_keys, keys, sizeof(keys)); } else { @@ -139,9 +135,16 @@ int cros_ec_kbc_check(struct input_config *input) if (num_keys < 0) return -1; - num_keycodes = key_matrix_decode(&config.matrix, keys, + num_keycodes = key_matrix_decode(&priv->matrix, keys, num_keys, keycodes, KBC_MAX_KEYS); sent = input_send_keycodes(input, keycodes, num_keycodes); + + /* + * For those ECs without an interrupt, stop scanning when we + * see that the scan is the same as last time. + */ + if ((irq_pending < 0) && same) + break; } while (irq_pending && !sent); return 1; @@ -155,15 +158,15 @@ int cros_ec_kbc_check(struct input_config *input) * @param config Configuration data read from fdt * @return 0 if ok, -1 on error */ -static int cros_ec_keyb_decode_fdt(const void *blob, int node, - struct keyb *config) +static int cros_ec_keyb_decode_fdt(struct udevice *dev, + struct cros_ec_keyb_priv *config) { /* * Get keyboard rows and columns - at present we are limited to * 8 columns by the protocol (one byte per row scan) */ - config->key_rows = fdtdec_get_int(blob, node, "google,key-rows", 0); - config->key_cols = fdtdec_get_int(blob, node, "google,key-columns", 0); + config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0); + config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0); if (!config->key_rows || !config->key_cols || config->key_rows * config->key_cols / 8 > CROS_EC_KEYSCAN_COLS) { @@ -171,76 +174,62 @@ static int cros_ec_keyb_decode_fdt(const void *blob, int node, config->key_rows, config->key_cols); return -1; } - config->repeat_delay_ms = fdtdec_get_int(blob, node, - "google,repeat-delay-ms", 0); - config->repeat_rate_ms = fdtdec_get_int(blob, node, - "google,repeat-rate-ms", 0); - config->ghost_filter = fdtdec_get_bool(blob, node, - "google,ghost-filter"); + config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter"); + return 0; } -/** - * Set up the keyboard. This is called by the stdio device handler. - * - * We want to do this init when the keyboard is actually used rather than - * at start-up, since keyboard input may not currently be selected. - * - * @return 0 if ok, -1 on error - */ -static int cros_ec_init_keyboard(void) +static int cros_ec_kbd_probe(struct udevice *dev) { - const void *blob = gd->fdt_blob; - int node; - - config.dev = board_get_cros_ec_dev(); - if (!config.dev) { - debug("%s: no cros_ec device: cannot init keyboard\n", - __func__); - return -1; + struct cros_ec_keyb_priv *priv = dev_get_priv(dev); + struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); + struct stdio_dev *sdev = &uc_priv->sdev; + struct input_config *input = &uc_priv->input; + int ret; + + ret = cros_ec_keyb_decode_fdt(dev, priv); + if (ret) { + debug("%s: Cannot decode node (ret=%d)\n", __func__, ret); + return -EINVAL; } - node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB); - if (node < 0) { - debug("%s: Node not found\n", __func__); - return -1; - } - if (cros_ec_keyb_decode_fdt(blob, node, &config)) - return -1; - input_set_delays(&config.input, config.repeat_delay_ms, - config.repeat_rate_ms); - if (key_matrix_init(&config.matrix, config.key_rows, - config.key_cols, config.ghost_filter)) { + input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS); + ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols, + priv->ghost_filter); + if (ret) { debug("%s: cannot init key matrix\n", __func__); - return -1; + return ret; } - if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) { + ret = key_matrix_decode_fdt(dev, &priv->matrix); + if (ret) { debug("%s: Could not decode key matrix from fdt\n", __func__); - return -1; + return ret; } - config.inited = 1; - debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows, - config.key_cols); + debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows, + priv->key_cols); - return 0; -} + priv->input = input; + input->dev = dev; + input_add_tables(input, false); + input->read_keys = cros_ec_kbc_check; + strcpy(sdev->name, "cros-ec-keyb"); -int drv_keyboard_init(void) -{ - struct stdio_dev dev; + /* Register the device. cros_ec_init_keyboard() will be called soon */ + return input_stdio_register(sdev); +} - if (input_init(&config.input, 0)) { - debug("%s: Cannot set up input\n", __func__); - return -1; - } - config.input.read_keys = cros_ec_kbc_check; +static const struct keyboard_ops cros_ec_kbd_ops = { +}; - memset(&dev, '\0', sizeof(dev)); - strcpy(dev.name, "cros-ec-keyb"); - dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; - dev.getc = kbd_getc; - dev.tstc = kbd_tstc; - dev.start = cros_ec_init_keyboard; +static const struct udevice_id cros_ec_kbd_ids[] = { + { .compatible = "google,cros-ec-keyb" }, + { } +}; - /* Register the device. cros_ec_init_keyboard() will be called soon */ - return input_stdio_register(&dev); -} +U_BOOT_DRIVER(cros_ec_kbd) = { + .name = "cros_ec_kbd", + .id = UCLASS_KEYBOARD, + .of_match = cros_ec_kbd_ids, + .probe = cros_ec_kbd_probe, + .ops = &cros_ec_kbd_ops, + .priv_auto_alloc_size = sizeof(struct cros_ec_keyb_priv), +};