David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Support for the S1 button on Routerboard 532 |
| 4 | * |
| 5 | * Copyright (C) 2009 Phil Sutter <n0-1@freewrt.org> |
| 6 | */ |
| 7 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8 | #include <linux/input.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/gpio.h> |
| 12 | |
| 13 | #include <asm/mach-rc32434/gpio.h> |
| 14 | #include <asm/mach-rc32434/rb.h> |
| 15 | |
| 16 | #define DRV_NAME "rb532-button" |
| 17 | |
| 18 | #define RB532_BTN_RATE 100 /* msec */ |
| 19 | #define RB532_BTN_KSYM BTN_0 |
| 20 | |
| 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. |
| 33 | */ |
| 34 | static bool rb532_button_pressed(void) |
| 35 | { |
| 36 | int val; |
| 37 | |
| 38 | set_latch_u5(0, LO_FOFF); |
| 39 | gpio_direction_input(GPIO_BTN_S1); |
| 40 | |
| 41 | val = gpio_get_value(GPIO_BTN_S1); |
| 42 | |
| 43 | rb532_gpio_set_func(GPIO_BTN_S1); |
| 44 | set_latch_u5(LO_FOFF, 0); |
| 45 | |
| 46 | return !val; |
| 47 | } |
| 48 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 49 | static void rb532_button_poll(struct input_dev *input) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 51 | input_report_key(input, RB532_BTN_KSYM, rb532_button_pressed()); |
| 52 | input_sync(input); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int rb532_button_probe(struct platform_device *pdev) |
| 56 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 57 | struct input_dev *input; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | int error; |
| 59 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 60 | input = devm_input_allocate_device(&pdev->dev); |
| 61 | if (!input) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 62 | return -ENOMEM; |
| 63 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 64 | input->name = "rb532 button"; |
| 65 | input->phys = "rb532/button0"; |
| 66 | input->id.bustype = BUS_HOST; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 68 | input_set_capability(input, EV_KEY, RB532_BTN_KSYM); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 69 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 70 | error = input_setup_polling(input, rb532_button_poll); |
| 71 | if (error) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 72 | return error; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 73 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 74 | input_set_poll_interval(input, RB532_BTN_RATE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 76 | error = input_register_device(input); |
| 77 | if (error) |
| 78 | return error; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static struct platform_driver rb532_button_driver = { |
| 84 | .probe = rb532_button_probe, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 85 | .driver = { |
| 86 | .name = DRV_NAME, |
| 87 | }, |
| 88 | }; |
| 89 | module_platform_driver(rb532_button_driver); |
| 90 | |
| 91 | MODULE_AUTHOR("Phil Sutter <n0-1@freewrt.org>"); |
| 92 | MODULE_LICENSE("GPL"); |
| 93 | MODULE_DESCRIPTION("Support for S1 button on Routerboard 532"); |
| 94 | MODULE_ALIAS("platform:" DRV_NAME); |