^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Support for the S1 button on Routerboard 532
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 Phil Sutter <n0-1@freewrt.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/mach-rc32434/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/mach-rc32434/rb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define DRV_NAME "rb532-button"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define RB532_BTN_RATE 100 /* msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define RB532_BTN_KSYM BTN_0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* The S1 button state is provided by GPIO pin 1. But as this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * pin is also used for uart input as alternate function, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * operational modes must be switched first:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * 1) disable uart using set_latch_u5()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * 2) turn off alternate function implicitly through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * gpio_direction_input()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * 3) read the GPIO's current value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * 4) undo step 2 by enabling alternate function (in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * mode the GPIO direction is fixed, so no change needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * 5) turn on uart again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * The GPIO value occurs to be inverted, so pin high means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * button is not pressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static bool rb532_button_pressed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) set_latch_u5(0, LO_FOFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) gpio_direction_input(GPIO_BTN_S1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) val = gpio_get_value(GPIO_BTN_S1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) rb532_gpio_set_func(GPIO_BTN_S1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) set_latch_u5(LO_FOFF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return !val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static void rb532_button_poll(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) input_report_key(input, RB532_BTN_KSYM, rb532_button_pressed());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int rb532_button_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) input = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) input->name = "rb532 button";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) input->phys = "rb532/button0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) input_set_capability(input, EV_KEY, RB532_BTN_KSYM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) error = input_setup_polling(input, rb532_button_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) input_set_poll_interval(input, RB532_BTN_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static struct platform_driver rb532_button_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .probe = rb532_button_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .name = DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) module_platform_driver(rb532_button_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) MODULE_AUTHOR("Phil Sutter <n0-1@freewrt.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) MODULE_DESCRIPTION("Support for S1 button on Routerboard 532");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) MODULE_ALIAS("platform:" DRV_NAME);