Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Support for the S1 button on Routerboard 532
0004  *
0005  * Copyright (C) 2009  Phil Sutter <n0-1@freewrt.org>
0006  */
0007 
0008 #include <linux/input.h>
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/gpio.h>
0012 
0013 #include <asm/mach-rc32434/gpio.h>
0014 #include <asm/mach-rc32434/rb.h>
0015 
0016 #define DRV_NAME "rb532-button"
0017 
0018 #define RB532_BTN_RATE 100 /* msec */
0019 #define RB532_BTN_KSYM BTN_0
0020 
0021 /* The S1 button state is provided by GPIO pin 1. But as this
0022  * pin is also used for uart input as alternate function, the
0023  * operational modes must be switched first:
0024  * 1) disable uart using set_latch_u5()
0025  * 2) turn off alternate function implicitly through
0026  *    gpio_direction_input()
0027  * 3) read the GPIO's current value
0028  * 4) undo step 2 by enabling alternate function (in this
0029  *    mode the GPIO direction is fixed, so no change needed)
0030  * 5) turn on uart again
0031  * The GPIO value occurs to be inverted, so pin high means
0032  * button is not pressed.
0033  */
0034 static bool rb532_button_pressed(void)
0035 {
0036     int val;
0037 
0038     set_latch_u5(0, LO_FOFF);
0039     gpio_direction_input(GPIO_BTN_S1);
0040 
0041     val = gpio_get_value(GPIO_BTN_S1);
0042 
0043     rb532_gpio_set_func(GPIO_BTN_S1);
0044     set_latch_u5(LO_FOFF, 0);
0045 
0046     return !val;
0047 }
0048 
0049 static void rb532_button_poll(struct input_dev *input)
0050 {
0051     input_report_key(input, RB532_BTN_KSYM, rb532_button_pressed());
0052     input_sync(input);
0053 }
0054 
0055 static int rb532_button_probe(struct platform_device *pdev)
0056 {
0057     struct input_dev *input;
0058     int error;
0059 
0060     input = devm_input_allocate_device(&pdev->dev);
0061     if (!input)
0062         return -ENOMEM;
0063 
0064     input->name = "rb532 button";
0065     input->phys = "rb532/button0";
0066     input->id.bustype = BUS_HOST;
0067 
0068     input_set_capability(input, EV_KEY, RB532_BTN_KSYM);
0069 
0070     error = input_setup_polling(input, rb532_button_poll);
0071     if (error)
0072         return error;
0073 
0074     input_set_poll_interval(input, RB532_BTN_RATE);
0075 
0076     error = input_register_device(input);
0077     if (error)
0078         return error;
0079 
0080     return 0;
0081 }
0082 
0083 static struct platform_driver rb532_button_driver = {
0084     .probe = rb532_button_probe,
0085     .driver = {
0086         .name = DRV_NAME,
0087     },
0088 };
0089 module_platform_driver(rb532_button_driver);
0090 
0091 MODULE_AUTHOR("Phil Sutter <n0-1@freewrt.org>");
0092 MODULE_LICENSE("GPL");
0093 MODULE_DESCRIPTION("Support for S1 button on Routerboard 532");
0094 MODULE_ALIAS("platform:" DRV_NAME);