Linux Audio

Check our new training course

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