Input: cros_ec_keyb - handle x86 detachable/convertible Chromebooks
[platform/kernel/linux-starfive.git] / drivers / input / keyboard / cros_ec_keyb.c
index fc02c54..c14136b 100644 (file)
 // expensive.
 
 #include <linux/module.h>
+#include <linux/acpi.h>
 #include <linux/bitops.h>
 #include <linux/i2c.h>
 #include <linux/input.h>
+#include <linux/input/vivaldi-fmap.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
 #include <linux/notifier.h>
@@ -27,8 +29,6 @@
 
 #include <asm/unaligned.h>
 
-#define MAX_NUM_TOP_ROW_KEYS   15
-
 /**
  * struct cros_ec_keyb - Structure representing EC keyboard device
  *
@@ -44,9 +44,7 @@
  * @idev: The input device for the matrix keys.
  * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
  * @notifier: interrupt event notifier for transport devices
- * @function_row_physmap: An array of the encoded rows/columns for the top
- *                        row function keys, in an order from left to right
- * @num_function_row_keys: The number of top row keys in a custom keyboard
+ * @vdata: vivaldi function row data
  */
 struct cros_ec_keyb {
        unsigned int rows;
@@ -64,8 +62,7 @@ struct cros_ec_keyb {
        struct input_dev *bs_idev;
        struct notifier_block notifier;
 
-       u16 function_row_physmap[MAX_NUM_TOP_ROW_KEYS];
-       size_t num_function_row_keys;
+       struct vivaldi_data vdata;
 };
 
 /**
@@ -439,10 +436,13 @@ static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
  * but the ckdev->bs_idev will remain NULL when this function exits.
  *
  * @ckdev: The keyboard device
+ * @expect_buttons_switches: Indicates that EC must report button and/or
+ *   switch events
  *
  * Returns 0 if no error or -error upon error.
  */
-static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
+static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
+                                   bool expect_buttons_switches)
 {
        struct cros_ec_device *ec_dev = ckdev->ec;
        struct device *dev = ckdev->dev;
@@ -469,7 +469,7 @@ static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
        switches = get_unaligned_le32(&event_data.switches);
 
        if (!buttons && !switches)
-               return 0;
+               return expect_buttons_switches ? -EINVAL : 0;
 
        /*
         * We call the non-matrix buttons/switches 'input1', if present.
@@ -519,8 +519,52 @@ static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
        return 0;
 }
 
+static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
+{
+       u32 *physmap = ckdev->vdata.function_row_physmap;
+       unsigned int row, col, scancode;
+       int n_physmap;
+       int error;
+       int i;
+
+       n_physmap = device_property_count_u32(ckdev->dev,
+                                             "function-row-physmap");
+       if (n_physmap <= 0)
+               return;
+
+       if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
+               dev_warn(ckdev->dev,
+                        "only up to %d top row keys is supported (%d specified)\n",
+                        VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
+               n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
+       }
+
+       error = device_property_read_u32_array(ckdev->dev,
+                                              "function-row-physmap",
+                                              physmap, n_physmap);
+       if (error) {
+               dev_warn(ckdev->dev,
+                        "failed to parse function-row-physmap property: %d\n",
+                        error);
+               return;
+       }
+
+       /*
+        * Convert (in place) from row/column encoding to matrix "scancode"
+        * used by the driver.
+        */
+       for (i = 0; i < n_physmap; i++) {
+               row = KEY_ROW(physmap[i]);
+               col = KEY_COL(physmap[i]);
+               scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
+               physmap[i] = scancode;
+       }
+
+       ckdev->vdata.num_function_row_keys = n_physmap;
+}
+
 /**
- * cros_ec_keyb_register_bs - Register matrix keys
+ * cros_ec_keyb_register_matrix - Register matrix keys
  *
  * Handles all the bits of the keyboard driver related to matrix keys.
  *
@@ -535,11 +579,6 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
        struct input_dev *idev;
        const char *phys;
        int err;
-       struct property *prop;
-       const __be32 *p;
-       u16 *physmap;
-       u32 key_pos;
-       int row, col;
 
        err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
        if (err)
@@ -574,7 +613,7 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
        idev->id.product = 0;
        idev->dev.parent = dev;
 
-       ckdev->ghost_filter = of_property_read_bool(dev->of_node,
+       ckdev->ghost_filter = device_property_read_bool(dev,
                                        "google,needs-ghost-filter");
 
        err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
@@ -590,21 +629,7 @@ static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
        input_set_drvdata(idev, ckdev);
        ckdev->idev = idev;
        cros_ec_keyb_compute_valid_keys(ckdev);
-
-       physmap = ckdev->function_row_physmap;
-       of_property_for_each_u32(dev->of_node, "function-row-physmap",
-                                prop, p, key_pos) {
-               if (ckdev->num_function_row_keys == MAX_NUM_TOP_ROW_KEYS) {
-                       dev_warn(dev, "Only support up to %d top row keys\n",
-                                MAX_NUM_TOP_ROW_KEYS);
-                       break;
-               }
-               row = KEY_ROW(key_pos);
-               col = KEY_COL(key_pos);
-               *physmap = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
-               physmap++;
-               ckdev->num_function_row_keys++;
-       }
+       cros_ec_keyb_parse_vivaldi_physmap(ckdev);
 
        err = input_register_device(ckdev->idev);
        if (err) {
@@ -619,18 +644,10 @@ static ssize_t function_row_physmap_show(struct device *dev,
                                         struct device_attribute *attr,
                                         char *buf)
 {
-       ssize_t size = 0;
-       int i;
-       struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
-       u16 *physmap = ckdev->function_row_physmap;
-
-       for (i = 0; i < ckdev->num_function_row_keys; i++)
-               size += scnprintf(buf + size, PAGE_SIZE - size,
-                                 "%s%02X", size ? " " : "", physmap[i]);
-       if (size)
-               size += scnprintf(buf + size, PAGE_SIZE - size, "\n");
+       const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
+       const struct vivaldi_data *data = &ckdev->vdata;
 
-       return size;
+       return vivaldi_function_row_physmap_show(data, buf);
 }
 
 static DEVICE_ATTR_RO(function_row_physmap);
@@ -648,7 +665,7 @@ static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
        struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
 
        if (attr == &dev_attr_function_row_physmap.attr &&
-           !ckdev->num_function_row_keys)
+           !ckdev->vdata.num_function_row_keys)
                return 0;
 
        return attr->mode;
@@ -659,16 +676,21 @@ static const struct attribute_group cros_ec_keyb_attr_group = {
        .attrs = cros_ec_keyb_attrs,
 };
 
-
 static int cros_ec_keyb_probe(struct platform_device *pdev)
 {
-       struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
+       struct cros_ec_device *ec;
        struct device *dev = &pdev->dev;
        struct cros_ec_keyb *ckdev;
+       bool buttons_switches_only = device_get_match_data(dev);
        int err;
 
-       if (!dev->of_node)
-               return -ENODEV;
+       /*
+        * If the parent ec device has not been probed yet, defer the probe of
+        * this keyboard/button driver until later.
+        */
+       ec = dev_get_drvdata(pdev->dev.parent);
+       if (!ec)
+               return -EPROBE_DEFER;
 
        ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
        if (!ckdev)
@@ -678,13 +700,16 @@ static int cros_ec_keyb_probe(struct platform_device *pdev)
        ckdev->dev = dev;
        dev_set_drvdata(dev, ckdev);
 
-       err = cros_ec_keyb_register_matrix(ckdev);
-       if (err) {
-               dev_err(dev, "cannot register matrix inputs: %d\n", err);
-               return err;
+       if (!buttons_switches_only) {
+               err = cros_ec_keyb_register_matrix(ckdev);
+               if (err) {
+                       dev_err(dev, "cannot register matrix inputs: %d\n",
+                               err);
+                       return err;
+               }
        }
 
-       err = cros_ec_keyb_register_bs(ckdev);
+       err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
        if (err) {
                dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
                return err;
@@ -692,7 +717,7 @@ static int cros_ec_keyb_probe(struct platform_device *pdev)
 
        err = devm_device_add_group(dev, &cros_ec_keyb_attr_group);
        if (err) {
-               dev_err(dev, "failed to create attributes. err=%d\n", err);
+               dev_err(dev, "failed to create attributes%d\n", err);
                return err;
        }
 
@@ -718,10 +743,19 @@ static int cros_ec_keyb_remove(struct platform_device *pdev)
        return 0;
 }
 
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id cros_ec_keyb_acpi_match[] = {
+       { "GOOG0007", true },
+       { }
+};
+MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match);
+#endif
+
 #ifdef CONFIG_OF
 static const struct of_device_id cros_ec_keyb_of_match[] = {
        { .compatible = "google,cros-ec-keyb" },
-       {},
+       { .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
+       {}
 };
 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
 #endif
@@ -734,6 +768,7 @@ static struct platform_driver cros_ec_keyb_driver = {
        .driver = {
                .name = "cros-ec-keyb",
                .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
+               .acpi_match_table = ACPI_PTR(cros_ec_keyb_acpi_match),
                .pm = &cros_ec_keyb_pm_ops,
        },
 };