1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for the S1 button on Routerboard 532
5 * Copyright (C) 2009 Phil Sutter <n0-1@freewrt.org>
8 #include <linux/input-polldev.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/gpio.h>
13 #include <asm/mach-rc32434/gpio.h>
14 #include <asm/mach-rc32434/rb.h>
16 #define DRV_NAME "rb532-button"
18 #define RB532_BTN_RATE 100 /* msec */
19 #define RB532_BTN_KSYM BTN_0
21 /* The S1 button state is provided by GPIO pin 1. But as this
22 * pin is also used for uart input as alternate function, the
23 * operational modes must be switched first:
24 * 1) disable uart using set_latch_u5()
25 * 2) turn off alternate function implicitly through
26 * gpio_direction_input()
27 * 3) read the GPIO's current value
28 * 4) undo step 2 by enabling alternate function (in this
29 * mode the GPIO direction is fixed, so no change needed)
30 * 5) turn on uart again
31 * The GPIO value occurs to be inverted, so pin high means
32 * button is not pressed.
34 static bool rb532_button_pressed(void)
38 set_latch_u5(0, LO_FOFF);
39 gpio_direction_input(GPIO_BTN_S1);
41 val = gpio_get_value(GPIO_BTN_S1);
43 rb532_gpio_set_func(GPIO_BTN_S1);
44 set_latch_u5(LO_FOFF, 0);
49 static void rb532_button_poll(struct input_polled_dev *poll_dev)
51 input_report_key(poll_dev->input, RB532_BTN_KSYM,
52 rb532_button_pressed());
53 input_sync(poll_dev->input);
56 static int rb532_button_probe(struct platform_device *pdev)
58 struct input_polled_dev *poll_dev;
61 poll_dev = input_allocate_polled_device();
65 poll_dev->poll = rb532_button_poll;
66 poll_dev->poll_interval = RB532_BTN_RATE;
68 poll_dev->input->name = "rb532 button";
69 poll_dev->input->phys = "rb532/button0";
70 poll_dev->input->id.bustype = BUS_HOST;
71 poll_dev->input->dev.parent = &pdev->dev;
73 dev_set_drvdata(&pdev->dev, poll_dev);
75 input_set_capability(poll_dev->input, EV_KEY, RB532_BTN_KSYM);
77 error = input_register_polled_device(poll_dev);
79 input_free_polled_device(poll_dev);
86 static int rb532_button_remove(struct platform_device *pdev)
88 struct input_polled_dev *poll_dev = dev_get_drvdata(&pdev->dev);
90 input_unregister_polled_device(poll_dev);
91 input_free_polled_device(poll_dev);
96 static struct platform_driver rb532_button_driver = {
97 .probe = rb532_button_probe,
98 .remove = rb532_button_remove,
103 module_platform_driver(rb532_button_driver);
105 MODULE_AUTHOR("Phil Sutter <n0-1@freewrt.org>");
106 MODULE_LICENSE("GPL");
107 MODULE_DESCRIPTION("Support for S1 button on Routerboard 532");
108 MODULE_ALIAS("platform:" DRV_NAME);