Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  adv_swbutton.c - Software Button Interface Driver.
0004  *
0005  *  (C) Copyright 2020 Advantech Corporation, Inc
0006  *
0007  */
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/input.h>
0011 #include <linux/acpi.h>
0012 #include <linux/platform_device.h>
0013 
0014 #define ACPI_BUTTON_HID_SWBTN               "AHC0310"
0015 
0016 #define ACPI_BUTTON_NOTIFY_SWBTN_RELEASE    0x86
0017 #define ACPI_BUTTON_NOTIFY_SWBTN_PRESSED    0x85
0018 
0019 struct adv_swbutton {
0020     struct input_dev *input;
0021     char phys[32];
0022 };
0023 
0024 /*-------------------------------------------------------------------------
0025  *                               Driver Interface
0026  *--------------------------------------------------------------------------
0027  */
0028 static void adv_swbutton_notify(acpi_handle handle, u32 event, void *context)
0029 {
0030     struct platform_device *device = context;
0031     struct adv_swbutton *button = dev_get_drvdata(&device->dev);
0032 
0033     switch (event) {
0034     case ACPI_BUTTON_NOTIFY_SWBTN_RELEASE:
0035         input_report_key(button->input, KEY_PROG1, 0);
0036         input_sync(button->input);
0037         break;
0038     case ACPI_BUTTON_NOTIFY_SWBTN_PRESSED:
0039         input_report_key(button->input, KEY_PROG1, 1);
0040         input_sync(button->input);
0041         break;
0042     default:
0043         dev_dbg(&device->dev, "Unsupported event [0x%x]\n", event);
0044     }
0045 }
0046 
0047 static int adv_swbutton_probe(struct platform_device *device)
0048 {
0049     struct adv_swbutton *button;
0050     struct input_dev *input;
0051     acpi_handle handle = ACPI_HANDLE(&device->dev);
0052     acpi_status status;
0053     int error;
0054 
0055     button = devm_kzalloc(&device->dev, sizeof(*button), GFP_KERNEL);
0056     if (!button)
0057         return -ENOMEM;
0058 
0059     dev_set_drvdata(&device->dev, button);
0060 
0061     input = devm_input_allocate_device(&device->dev);
0062     if (!input)
0063         return -ENOMEM;
0064 
0065     button->input = input;
0066     snprintf(button->phys, sizeof(button->phys), "%s/button/input0", ACPI_BUTTON_HID_SWBTN);
0067 
0068     input->name = "Advantech Software Button";
0069     input->phys = button->phys;
0070     input->id.bustype = BUS_HOST;
0071     input->dev.parent = &device->dev;
0072     set_bit(EV_REP, input->evbit);
0073     input_set_capability(input, EV_KEY, KEY_PROG1);
0074 
0075     error = input_register_device(input);
0076     if (error)
0077         return error;
0078 
0079     device_init_wakeup(&device->dev, true);
0080 
0081     status = acpi_install_notify_handler(handle,
0082                          ACPI_DEVICE_NOTIFY,
0083                          adv_swbutton_notify,
0084                          device);
0085     if (ACPI_FAILURE(status)) {
0086         dev_err(&device->dev, "Error installing notify handler\n");
0087         return -EIO;
0088     }
0089 
0090     return 0;
0091 }
0092 
0093 static int adv_swbutton_remove(struct platform_device *device)
0094 {
0095     acpi_handle handle = ACPI_HANDLE(&device->dev);
0096 
0097     acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY,
0098                    adv_swbutton_notify);
0099 
0100     return 0;
0101 }
0102 
0103 static const struct acpi_device_id button_device_ids[] = {
0104     {ACPI_BUTTON_HID_SWBTN, 0},
0105     {"", 0},
0106 };
0107 MODULE_DEVICE_TABLE(acpi, button_device_ids);
0108 
0109 static struct platform_driver adv_swbutton_driver = {
0110     .driver = {
0111         .name = "adv_swbutton",
0112         .acpi_match_table = button_device_ids,
0113     },
0114     .probe = adv_swbutton_probe,
0115     .remove = adv_swbutton_remove,
0116 };
0117 module_platform_driver(adv_swbutton_driver);
0118 
0119 MODULE_AUTHOR("Andrea Ho");
0120 MODULE_DESCRIPTION("Advantech ACPI SW Button Driver");
0121 MODULE_LICENSE("GPL v2");