Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // ChromeOS EC keyboard driver
0003 //
0004 // Copyright (C) 2012 Google, Inc.
0005 //
0006 // This driver uses the ChromeOS EC byte-level message-based protocol for
0007 // communicating the keyboard state (which keys are pressed) from a keyboard EC
0008 // to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
0009 // but everything else (including deghosting) is done here.  The main
0010 // motivation for this is to keep the EC firmware as simple as possible, since
0011 // it cannot be easily upgraded and EC flash/IRAM space is relatively
0012 // expensive.
0013 
0014 #include <linux/module.h>
0015 #include <linux/acpi.h>
0016 #include <linux/bitops.h>
0017 #include <linux/i2c.h>
0018 #include <linux/input.h>
0019 #include <linux/input/vivaldi-fmap.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/kernel.h>
0022 #include <linux/notifier.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/slab.h>
0025 #include <linux/sysrq.h>
0026 #include <linux/input/matrix_keypad.h>
0027 #include <linux/platform_data/cros_ec_commands.h>
0028 #include <linux/platform_data/cros_ec_proto.h>
0029 
0030 #include <asm/unaligned.h>
0031 
0032 /**
0033  * struct cros_ec_keyb - Structure representing EC keyboard device
0034  *
0035  * @rows: Number of rows in the keypad
0036  * @cols: Number of columns in the keypad
0037  * @row_shift: log2 or number of rows, rounded up
0038  * @keymap_data: Matrix keymap data used to convert to keyscan values
0039  * @ghost_filter: true to enable the matrix key-ghosting filter
0040  * @valid_keys: bitmap of existing keys for each matrix column
0041  * @old_kb_state: bitmap of keys pressed last scan
0042  * @dev: Device pointer
0043  * @ec: Top level ChromeOS device to use to talk to EC
0044  * @idev: The input device for the matrix keys.
0045  * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
0046  * @notifier: interrupt event notifier for transport devices
0047  * @vdata: vivaldi function row data
0048  */
0049 struct cros_ec_keyb {
0050     unsigned int rows;
0051     unsigned int cols;
0052     int row_shift;
0053     const struct matrix_keymap_data *keymap_data;
0054     bool ghost_filter;
0055     uint8_t *valid_keys;
0056     uint8_t *old_kb_state;
0057 
0058     struct device *dev;
0059     struct cros_ec_device *ec;
0060 
0061     struct input_dev *idev;
0062     struct input_dev *bs_idev;
0063     struct notifier_block notifier;
0064 
0065     struct vivaldi_data vdata;
0066 };
0067 
0068 /**
0069  * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch
0070  *  bitmap #defines
0071  *
0072  * @ev_type: The type of the input event to generate (e.g., EV_KEY).
0073  * @code: A linux keycode
0074  * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
0075  * @inverted: If the #define and EV_SW have opposite meanings, this is true.
0076  *            Only applicable to switches.
0077  */
0078 struct cros_ec_bs_map {
0079     unsigned int ev_type;
0080     unsigned int code;
0081     u8 bit;
0082     bool inverted;
0083 };
0084 
0085 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
0086 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
0087     /* Buttons */
0088     {
0089         .ev_type    = EV_KEY,
0090         .code       = KEY_POWER,
0091         .bit        = EC_MKBP_POWER_BUTTON,
0092     },
0093     {
0094         .ev_type    = EV_KEY,
0095         .code       = KEY_VOLUMEUP,
0096         .bit        = EC_MKBP_VOL_UP,
0097     },
0098     {
0099         .ev_type    = EV_KEY,
0100         .code       = KEY_VOLUMEDOWN,
0101         .bit        = EC_MKBP_VOL_DOWN,
0102     },
0103 
0104     /* Switches */
0105     {
0106         .ev_type    = EV_SW,
0107         .code       = SW_LID,
0108         .bit        = EC_MKBP_LID_OPEN,
0109         .inverted   = true,
0110     },
0111     {
0112         .ev_type    = EV_SW,
0113         .code       = SW_TABLET_MODE,
0114         .bit        = EC_MKBP_TABLET_MODE,
0115     },
0116 };
0117 
0118 /*
0119  * Returns true when there is at least one combination of pressed keys that
0120  * results in ghosting.
0121  */
0122 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
0123 {
0124     int col1, col2, buf1, buf2;
0125     struct device *dev = ckdev->dev;
0126     uint8_t *valid_keys = ckdev->valid_keys;
0127 
0128     /*
0129      * Ghosting happens if for any pressed key X there are other keys
0130      * pressed both in the same row and column of X as, for instance,
0131      * in the following diagram:
0132      *
0133      * . . Y . g .
0134      * . . . . . .
0135      * . . . . . .
0136      * . . X . Z .
0137      *
0138      * In this case only X, Y, and Z are pressed, but g appears to be
0139      * pressed too (see Wikipedia).
0140      */
0141     for (col1 = 0; col1 < ckdev->cols; col1++) {
0142         buf1 = buf[col1] & valid_keys[col1];
0143         for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
0144             buf2 = buf[col2] & valid_keys[col2];
0145             if (hweight8(buf1 & buf2) > 1) {
0146                 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
0147                     col1, buf1, col2, buf2);
0148                 return true;
0149             }
0150         }
0151     }
0152 
0153     return false;
0154 }
0155 
0156 
0157 /*
0158  * Compares the new keyboard state to the old one and produces key
0159  * press/release events accordingly.  The keyboard state is 13 bytes (one byte
0160  * per column)
0161  */
0162 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
0163              uint8_t *kb_state, int len)
0164 {
0165     struct input_dev *idev = ckdev->idev;
0166     int col, row;
0167     int new_state;
0168     int old_state;
0169 
0170     if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
0171         /*
0172          * Simple-minded solution: ignore this state. The obvious
0173          * improvement is to only ignore changes to keys involved in
0174          * the ghosting, but process the other changes.
0175          */
0176         dev_dbg(ckdev->dev, "ghosting found\n");
0177         return;
0178     }
0179 
0180     for (col = 0; col < ckdev->cols; col++) {
0181         for (row = 0; row < ckdev->rows; row++) {
0182             int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
0183             const unsigned short *keycodes = idev->keycode;
0184 
0185             new_state = kb_state[col] & (1 << row);
0186             old_state = ckdev->old_kb_state[col] & (1 << row);
0187             if (new_state != old_state) {
0188                 dev_dbg(ckdev->dev,
0189                     "changed: [r%d c%d]: byte %02x\n",
0190                     row, col, new_state);
0191 
0192                 input_event(idev, EV_MSC, MSC_SCAN, pos);
0193                 input_report_key(idev, keycodes[pos],
0194                          new_state);
0195             }
0196         }
0197         ckdev->old_kb_state[col] = kb_state[col];
0198     }
0199     input_sync(ckdev->idev);
0200 }
0201 
0202 /**
0203  * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
0204  *
0205  * This takes a bitmap of buttons or switches from the EC and reports events,
0206  * syncing at the end.
0207  *
0208  * @ckdev: The keyboard device.
0209  * @ev_type: The input event type (e.g., EV_KEY).
0210  * @mask: A bitmap of buttons from the EC.
0211  */
0212 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
0213                    unsigned int ev_type, u32 mask)
0214 
0215 {
0216     struct input_dev *idev = ckdev->bs_idev;
0217     int i;
0218 
0219     for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
0220         const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
0221 
0222         if (map->ev_type != ev_type)
0223             continue;
0224 
0225         input_event(idev, ev_type, map->code,
0226                 !!(mask & BIT(map->bit)) ^ map->inverted);
0227     }
0228     input_sync(idev);
0229 }
0230 
0231 static int cros_ec_keyb_work(struct notifier_block *nb,
0232                  unsigned long queued_during_suspend, void *_notify)
0233 {
0234     struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
0235                           notifier);
0236     u32 val;
0237     unsigned int ev_type;
0238 
0239     /*
0240      * If not wake enabled, discard key state changes during
0241      * suspend. Switches will be re-checked in
0242      * cros_ec_keyb_resume() to be sure nothing is lost.
0243      */
0244     if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
0245         return NOTIFY_OK;
0246 
0247     switch (ckdev->ec->event_data.event_type) {
0248     case EC_MKBP_EVENT_KEY_MATRIX:
0249         pm_wakeup_event(ckdev->dev, 0);
0250 
0251         if (ckdev->ec->event_size != ckdev->cols) {
0252             dev_err(ckdev->dev,
0253                 "Discarded incomplete key matrix event.\n");
0254             return NOTIFY_OK;
0255         }
0256 
0257         cros_ec_keyb_process(ckdev,
0258                      ckdev->ec->event_data.data.key_matrix,
0259                      ckdev->ec->event_size);
0260         break;
0261 
0262     case EC_MKBP_EVENT_SYSRQ:
0263         pm_wakeup_event(ckdev->dev, 0);
0264 
0265         val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
0266         dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
0267         handle_sysrq(val);
0268         break;
0269 
0270     case EC_MKBP_EVENT_BUTTON:
0271     case EC_MKBP_EVENT_SWITCH:
0272         pm_wakeup_event(ckdev->dev, 0);
0273 
0274         if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
0275             val = get_unaligned_le32(
0276                     &ckdev->ec->event_data.data.buttons);
0277             ev_type = EV_KEY;
0278         } else {
0279             val = get_unaligned_le32(
0280                     &ckdev->ec->event_data.data.switches);
0281             ev_type = EV_SW;
0282         }
0283         cros_ec_keyb_report_bs(ckdev, ev_type, val);
0284         break;
0285 
0286     default:
0287         return NOTIFY_DONE;
0288     }
0289 
0290     return NOTIFY_OK;
0291 }
0292 
0293 /*
0294  * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
0295  * ghosting logic to ignore NULL or virtual keys.
0296  */
0297 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
0298 {
0299     int row, col;
0300     int row_shift = ckdev->row_shift;
0301     unsigned short *keymap = ckdev->idev->keycode;
0302     unsigned short code;
0303 
0304     BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
0305 
0306     for (col = 0; col < ckdev->cols; col++) {
0307         for (row = 0; row < ckdev->rows; row++) {
0308             code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
0309             if (code && (code != KEY_BATTERY))
0310                 ckdev->valid_keys[col] |= 1 << row;
0311         }
0312         dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
0313             col, ckdev->valid_keys[col]);
0314     }
0315 }
0316 
0317 /**
0318  * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
0319  *
0320  * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
0321  * unmarshalling and different version nonsense into something simple.
0322  *
0323  * @ec_dev: The EC device
0324  * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
0325  * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH.  Actually
0326  *              in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
0327  *              EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
0328  * @result: Where we'll store the result; a union
0329  * @result_size: The size of the result.  Expected to be the size of one of
0330  *               the elements in the union.
0331  *
0332  * Returns 0 if no error or -error upon error.
0333  */
0334 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
0335                  enum ec_mkbp_info_type info_type,
0336                  enum ec_mkbp_event event_type,
0337                  union ec_response_get_next_data *result,
0338                  size_t result_size)
0339 {
0340     struct ec_params_mkbp_info *params;
0341     struct cros_ec_command *msg;
0342     int ret;
0343 
0344     msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
0345                        sizeof(*params)), GFP_KERNEL);
0346     if (!msg)
0347         return -ENOMEM;
0348 
0349     msg->command = EC_CMD_MKBP_INFO;
0350     msg->version = 1;
0351     msg->outsize = sizeof(*params);
0352     msg->insize = result_size;
0353     params = (struct ec_params_mkbp_info *)msg->data;
0354     params->info_type = info_type;
0355     params->event_type = event_type;
0356 
0357     ret = cros_ec_cmd_xfer_status(ec_dev, msg);
0358     if (ret == -ENOPROTOOPT) {
0359         /* With older ECs we just return 0 for everything */
0360         memset(result, 0, result_size);
0361         ret = 0;
0362     } else if (ret < 0) {
0363         dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
0364              (int)info_type, (int)event_type, ret);
0365     } else if (ret != result_size) {
0366         dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
0367              (int)info_type, (int)event_type,
0368              ret, result_size);
0369         ret = -EPROTO;
0370     } else {
0371         memcpy(result, msg->data, result_size);
0372         ret = 0;
0373     }
0374 
0375     kfree(msg);
0376 
0377     return ret;
0378 }
0379 
0380 /**
0381  * cros_ec_keyb_query_switches - Query the state of switches and report
0382  *
0383  * This will ask the EC about the current state of switches and report to the
0384  * kernel.  Note that we don't query for buttons because they are more
0385  * transitory and we'll get an update on the next release / press.
0386  *
0387  * @ckdev: The keyboard device
0388  *
0389  * Returns 0 if no error or -error upon error.
0390  */
0391 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
0392 {
0393     struct cros_ec_device *ec_dev = ckdev->ec;
0394     union ec_response_get_next_data event_data = {};
0395     int ret;
0396 
0397     ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
0398                 EC_MKBP_EVENT_SWITCH, &event_data,
0399                 sizeof(event_data.switches));
0400     if (ret)
0401         return ret;
0402 
0403     cros_ec_keyb_report_bs(ckdev, EV_SW,
0404                    get_unaligned_le32(&event_data.switches));
0405 
0406     return 0;
0407 }
0408 
0409 /**
0410  * cros_ec_keyb_resume - Resume the keyboard
0411  *
0412  * We use the resume notification as a chance to query the EC for switches.
0413  *
0414  * @dev: The keyboard device
0415  *
0416  * Returns 0 if no error or -error upon error.
0417  */
0418 static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
0419 {
0420     struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
0421 
0422     if (ckdev->bs_idev)
0423         return cros_ec_keyb_query_switches(ckdev);
0424 
0425     return 0;
0426 }
0427 
0428 /**
0429  * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
0430  *
0431  * Handles all the bits of the keyboard driver related to non-matrix buttons
0432  * and switches, including asking the EC about which are present and telling
0433  * the kernel to expect them.
0434  *
0435  * If this device has no support for buttons and switches we'll return no error
0436  * but the ckdev->bs_idev will remain NULL when this function exits.
0437  *
0438  * @ckdev: The keyboard device
0439  * @expect_buttons_switches: Indicates that EC must report button and/or
0440  *   switch events
0441  *
0442  * Returns 0 if no error or -error upon error.
0443  */
0444 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
0445                     bool expect_buttons_switches)
0446 {
0447     struct cros_ec_device *ec_dev = ckdev->ec;
0448     struct device *dev = ckdev->dev;
0449     struct input_dev *idev;
0450     union ec_response_get_next_data event_data = {};
0451     const char *phys;
0452     u32 buttons;
0453     u32 switches;
0454     int ret;
0455     int i;
0456 
0457     ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
0458                 EC_MKBP_EVENT_BUTTON, &event_data,
0459                 sizeof(event_data.buttons));
0460     if (ret)
0461         return ret;
0462     buttons = get_unaligned_le32(&event_data.buttons);
0463 
0464     ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
0465                 EC_MKBP_EVENT_SWITCH, &event_data,
0466                 sizeof(event_data.switches));
0467     if (ret)
0468         return ret;
0469     switches = get_unaligned_le32(&event_data.switches);
0470 
0471     if (!buttons && !switches)
0472         return expect_buttons_switches ? -EINVAL : 0;
0473 
0474     /*
0475      * We call the non-matrix buttons/switches 'input1', if present.
0476      * Allocate phys before input dev, to ensure correct tear-down
0477      * ordering.
0478      */
0479     phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
0480     if (!phys)
0481         return -ENOMEM;
0482 
0483     idev = devm_input_allocate_device(dev);
0484     if (!idev)
0485         return -ENOMEM;
0486 
0487     idev->name = "cros_ec_buttons";
0488     idev->phys = phys;
0489     __set_bit(EV_REP, idev->evbit);
0490 
0491     idev->id.bustype = BUS_VIRTUAL;
0492     idev->id.version = 1;
0493     idev->id.product = 0;
0494     idev->dev.parent = dev;
0495 
0496     input_set_drvdata(idev, ckdev);
0497     ckdev->bs_idev = idev;
0498 
0499     for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
0500         const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
0501 
0502         if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
0503             (map->ev_type == EV_SW && (switches & BIT(map->bit))))
0504             input_set_capability(idev, map->ev_type, map->code);
0505     }
0506 
0507     ret = cros_ec_keyb_query_switches(ckdev);
0508     if (ret) {
0509         dev_err(dev, "cannot query switches\n");
0510         return ret;
0511     }
0512 
0513     ret = input_register_device(ckdev->bs_idev);
0514     if (ret) {
0515         dev_err(dev, "cannot register input device\n");
0516         return ret;
0517     }
0518 
0519     return 0;
0520 }
0521 
0522 static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
0523 {
0524     u32 *physmap = ckdev->vdata.function_row_physmap;
0525     unsigned int row, col, scancode;
0526     int n_physmap;
0527     int error;
0528     int i;
0529 
0530     n_physmap = device_property_count_u32(ckdev->dev,
0531                           "function-row-physmap");
0532     if (n_physmap <= 0)
0533         return;
0534 
0535     if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
0536         dev_warn(ckdev->dev,
0537              "only up to %d top row keys is supported (%d specified)\n",
0538              VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
0539         n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
0540     }
0541 
0542     error = device_property_read_u32_array(ckdev->dev,
0543                            "function-row-physmap",
0544                            physmap, n_physmap);
0545     if (error) {
0546         dev_warn(ckdev->dev,
0547              "failed to parse function-row-physmap property: %d\n",
0548              error);
0549         return;
0550     }
0551 
0552     /*
0553      * Convert (in place) from row/column encoding to matrix "scancode"
0554      * used by the driver.
0555      */
0556     for (i = 0; i < n_physmap; i++) {
0557         row = KEY_ROW(physmap[i]);
0558         col = KEY_COL(physmap[i]);
0559         scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
0560         physmap[i] = scancode;
0561     }
0562 
0563     ckdev->vdata.num_function_row_keys = n_physmap;
0564 }
0565 
0566 /**
0567  * cros_ec_keyb_register_matrix - Register matrix keys
0568  *
0569  * Handles all the bits of the keyboard driver related to matrix keys.
0570  *
0571  * @ckdev: The keyboard device
0572  *
0573  * Returns 0 if no error or -error upon error.
0574  */
0575 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
0576 {
0577     struct cros_ec_device *ec_dev = ckdev->ec;
0578     struct device *dev = ckdev->dev;
0579     struct input_dev *idev;
0580     const char *phys;
0581     int err;
0582 
0583     err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
0584     if (err)
0585         return err;
0586 
0587     ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
0588     if (!ckdev->valid_keys)
0589         return -ENOMEM;
0590 
0591     ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
0592     if (!ckdev->old_kb_state)
0593         return -ENOMEM;
0594 
0595     /*
0596      * We call the keyboard matrix 'input0'. Allocate phys before input
0597      * dev, to ensure correct tear-down ordering.
0598      */
0599     phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
0600     if (!phys)
0601         return -ENOMEM;
0602 
0603     idev = devm_input_allocate_device(dev);
0604     if (!idev)
0605         return -ENOMEM;
0606 
0607     idev->name = CROS_EC_DEV_NAME;
0608     idev->phys = phys;
0609     __set_bit(EV_REP, idev->evbit);
0610 
0611     idev->id.bustype = BUS_VIRTUAL;
0612     idev->id.version = 1;
0613     idev->id.product = 0;
0614     idev->dev.parent = dev;
0615 
0616     ckdev->ghost_filter = device_property_read_bool(dev,
0617                     "google,needs-ghost-filter");
0618 
0619     err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
0620                      NULL, idev);
0621     if (err) {
0622         dev_err(dev, "cannot build key matrix\n");
0623         return err;
0624     }
0625 
0626     ckdev->row_shift = get_count_order(ckdev->cols);
0627 
0628     input_set_capability(idev, EV_MSC, MSC_SCAN);
0629     input_set_drvdata(idev, ckdev);
0630     ckdev->idev = idev;
0631     cros_ec_keyb_compute_valid_keys(ckdev);
0632     cros_ec_keyb_parse_vivaldi_physmap(ckdev);
0633 
0634     err = input_register_device(ckdev->idev);
0635     if (err) {
0636         dev_err(dev, "cannot register input device\n");
0637         return err;
0638     }
0639 
0640     return 0;
0641 }
0642 
0643 static ssize_t function_row_physmap_show(struct device *dev,
0644                      struct device_attribute *attr,
0645                      char *buf)
0646 {
0647     const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
0648     const struct vivaldi_data *data = &ckdev->vdata;
0649 
0650     return vivaldi_function_row_physmap_show(data, buf);
0651 }
0652 
0653 static DEVICE_ATTR_RO(function_row_physmap);
0654 
0655 static struct attribute *cros_ec_keyb_attrs[] = {
0656     &dev_attr_function_row_physmap.attr,
0657     NULL,
0658 };
0659 
0660 static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
0661                         struct attribute *attr,
0662                         int n)
0663 {
0664     struct device *dev = kobj_to_dev(kobj);
0665     struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
0666 
0667     if (attr == &dev_attr_function_row_physmap.attr &&
0668         !ckdev->vdata.num_function_row_keys)
0669         return 0;
0670 
0671     return attr->mode;
0672 }
0673 
0674 static const struct attribute_group cros_ec_keyb_attr_group = {
0675     .is_visible = cros_ec_keyb_attr_is_visible,
0676     .attrs = cros_ec_keyb_attrs,
0677 };
0678 
0679 static int cros_ec_keyb_probe(struct platform_device *pdev)
0680 {
0681     struct cros_ec_device *ec;
0682     struct device *dev = &pdev->dev;
0683     struct cros_ec_keyb *ckdev;
0684     bool buttons_switches_only = device_get_match_data(dev);
0685     int err;
0686 
0687     /*
0688      * If the parent ec device has not been probed yet, defer the probe of
0689      * this keyboard/button driver until later.
0690      */
0691     ec = dev_get_drvdata(pdev->dev.parent);
0692     if (!ec)
0693         return -EPROBE_DEFER;
0694 
0695     ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
0696     if (!ckdev)
0697         return -ENOMEM;
0698 
0699     ckdev->ec = ec;
0700     ckdev->dev = dev;
0701     dev_set_drvdata(dev, ckdev);
0702 
0703     if (!buttons_switches_only) {
0704         err = cros_ec_keyb_register_matrix(ckdev);
0705         if (err) {
0706             dev_err(dev, "cannot register matrix inputs: %d\n",
0707                 err);
0708             return err;
0709         }
0710     }
0711 
0712     err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
0713     if (err) {
0714         dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
0715         return err;
0716     }
0717 
0718     err = devm_device_add_group(dev, &cros_ec_keyb_attr_group);
0719     if (err) {
0720         dev_err(dev, "failed to create attributes: %d\n", err);
0721         return err;
0722     }
0723 
0724     ckdev->notifier.notifier_call = cros_ec_keyb_work;
0725     err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
0726                            &ckdev->notifier);
0727     if (err) {
0728         dev_err(dev, "cannot register notifier: %d\n", err);
0729         return err;
0730     }
0731 
0732     device_init_wakeup(ckdev->dev, true);
0733     return 0;
0734 }
0735 
0736 static int cros_ec_keyb_remove(struct platform_device *pdev)
0737 {
0738     struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
0739 
0740     blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
0741                        &ckdev->notifier);
0742 
0743     return 0;
0744 }
0745 
0746 #ifdef CONFIG_ACPI
0747 static const struct acpi_device_id cros_ec_keyb_acpi_match[] = {
0748     { "GOOG0007", true },
0749     { }
0750 };
0751 MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match);
0752 #endif
0753 
0754 #ifdef CONFIG_OF
0755 static const struct of_device_id cros_ec_keyb_of_match[] = {
0756     { .compatible = "google,cros-ec-keyb" },
0757     { .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
0758     {}
0759 };
0760 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
0761 #endif
0762 
0763 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
0764 
0765 static struct platform_driver cros_ec_keyb_driver = {
0766     .probe = cros_ec_keyb_probe,
0767     .remove = cros_ec_keyb_remove,
0768     .driver = {
0769         .name = "cros-ec-keyb",
0770         .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
0771         .acpi_match_table = ACPI_PTR(cros_ec_keyb_acpi_match),
0772         .pm = &cros_ec_keyb_pm_ops,
0773     },
0774 };
0775 
0776 module_platform_driver(cros_ec_keyb_driver);
0777 
0778 MODULE_LICENSE("GPL v2");
0779 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
0780 MODULE_ALIAS("platform:cros-ec-keyb");