Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Cobalt button interface driver.
  4 *
  5 *  Copyright (C) 2007-2008  Yoichi Yuasa <yuasa@linux-mips.org>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7#include <linux/input.h>
  8#include <linux/io.h>
  9#include <linux/ioport.h>
 10#include <linux/module.h>
 11#include <linux/platform_device.h>
 12#include <linux/slab.h>
 13
 14#define BUTTONS_POLL_INTERVAL	30	/* msec */
 15#define BUTTONS_COUNT_THRESHOLD	3
 16#define BUTTONS_STATUS_MASK	0xfe000000
 17
 18static const unsigned short cobalt_map[] = {
 19	KEY_RESERVED,
 20	KEY_RESTART,
 21	KEY_LEFT,
 22	KEY_UP,
 23	KEY_DOWN,
 24	KEY_RIGHT,
 25	KEY_ENTER,
 26	KEY_SELECT
 27};
 28
 29struct buttons_dev {
 
 30	unsigned short keymap[ARRAY_SIZE(cobalt_map)];
 31	int count[ARRAY_SIZE(cobalt_map)];
 32	void __iomem *reg;
 33};
 34
 35static void handle_buttons(struct input_dev *input)
 36{
 37	struct buttons_dev *bdev = input_get_drvdata(input);
 
 38	uint32_t status;
 39	int i;
 40
 41	status = ~readl(bdev->reg) >> 24;
 42
 43	for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
 44		if (status & (1U << i)) {
 45			if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
 46				input_event(input, EV_MSC, MSC_SCAN, i);
 47				input_report_key(input, bdev->keymap[i], 1);
 48				input_sync(input);
 49			}
 50		} else {
 51			if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
 52				input_event(input, EV_MSC, MSC_SCAN, i);
 53				input_report_key(input, bdev->keymap[i], 0);
 54				input_sync(input);
 55			}
 56			bdev->count[i] = 0;
 57		}
 58	}
 59}
 60
 61static int cobalt_buttons_probe(struct platform_device *pdev)
 62{
 63	struct buttons_dev *bdev;
 
 64	struct input_dev *input;
 65	struct resource *res;
 66	int error, i;
 67
 68	bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
 69	if (!bdev)
 70		return -ENOMEM;
 71
 72	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 73	if (!res)
 74		return -EBUSY;
 75
 76	bdev->reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
 77	if (!bdev->reg)
 78		return -ENOMEM;
 79
 80	memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
 81
 82	input = devm_input_allocate_device(&pdev->dev);
 83	if (!input)
 84		return -ENOMEM;
 85
 86	input_set_drvdata(input, bdev);
 87
 
 88	input->name = "Cobalt buttons";
 89	input->phys = "cobalt/input0";
 90	input->id.bustype = BUS_HOST;
 
 91
 92	input->keycode = bdev->keymap;
 93	input->keycodemax = ARRAY_SIZE(bdev->keymap);
 94	input->keycodesize = sizeof(unsigned short);
 95
 96	input_set_capability(input, EV_MSC, MSC_SCAN);
 97	__set_bit(EV_KEY, input->evbit);
 98	for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
 99		__set_bit(bdev->keymap[i], input->keybit);
100	__clear_bit(KEY_RESERVED, input->keybit);
101
 
 
 
 
 
 
 
 
 
102
103	error = input_setup_polling(input, handle_buttons);
104	if (error)
105		return error;
106
107	input_set_poll_interval(input, BUTTONS_POLL_INTERVAL);
108
109	error = input_register_device(input);
110	if (error)
111		return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
113	return 0;
114}
115
116MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
117MODULE_DESCRIPTION("Cobalt button interface driver");
118MODULE_LICENSE("GPL");
119/* work with hotplug and coldplug */
120MODULE_ALIAS("platform:Cobalt buttons");
121
122static struct platform_driver cobalt_buttons_driver = {
123	.probe	= cobalt_buttons_probe,
 
124	.driver	= {
125		.name	= "Cobalt buttons",
126	},
127};
128module_platform_driver(cobalt_buttons_driver);
v4.17
 
  1/*
  2 *  Cobalt button interface driver.
  3 *
  4 *  Copyright (C) 2007-2008  Yoichi Yuasa <yuasa@linux-mips.org>
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License as published by
  8 *  the Free Software Foundation; either version 2 of the License, or
  9 *  (at your option) any later version.
 10 *
 11 *  This program is distributed in the hope that it will be useful,
 12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 *  GNU General Public License for more details.
 15 *
 16 *  You should have received a copy of the GNU General Public License
 17 *  along with this program; if not, write to the Free Software
 18 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 19 */
 20#include <linux/input-polldev.h>
 
 21#include <linux/ioport.h>
 22#include <linux/module.h>
 23#include <linux/platform_device.h>
 24#include <linux/slab.h>
 25
 26#define BUTTONS_POLL_INTERVAL	30	/* msec */
 27#define BUTTONS_COUNT_THRESHOLD	3
 28#define BUTTONS_STATUS_MASK	0xfe000000
 29
 30static const unsigned short cobalt_map[] = {
 31	KEY_RESERVED,
 32	KEY_RESTART,
 33	KEY_LEFT,
 34	KEY_UP,
 35	KEY_DOWN,
 36	KEY_RIGHT,
 37	KEY_ENTER,
 38	KEY_SELECT
 39};
 40
 41struct buttons_dev {
 42	struct input_polled_dev *poll_dev;
 43	unsigned short keymap[ARRAY_SIZE(cobalt_map)];
 44	int count[ARRAY_SIZE(cobalt_map)];
 45	void __iomem *reg;
 46};
 47
 48static void handle_buttons(struct input_polled_dev *dev)
 49{
 50	struct buttons_dev *bdev = dev->private;
 51	struct input_dev *input = dev->input;
 52	uint32_t status;
 53	int i;
 54
 55	status = ~readl(bdev->reg) >> 24;
 56
 57	for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
 58		if (status & (1U << i)) {
 59			if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
 60				input_event(input, EV_MSC, MSC_SCAN, i);
 61				input_report_key(input, bdev->keymap[i], 1);
 62				input_sync(input);
 63			}
 64		} else {
 65			if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
 66				input_event(input, EV_MSC, MSC_SCAN, i);
 67				input_report_key(input, bdev->keymap[i], 0);
 68				input_sync(input);
 69			}
 70			bdev->count[i] = 0;
 71		}
 72	}
 73}
 74
 75static int cobalt_buttons_probe(struct platform_device *pdev)
 76{
 77	struct buttons_dev *bdev;
 78	struct input_polled_dev *poll_dev;
 79	struct input_dev *input;
 80	struct resource *res;
 81	int error, i;
 82
 83	bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
 84	poll_dev = input_allocate_polled_device();
 85	if (!bdev || !poll_dev) {
 86		error = -ENOMEM;
 87		goto err_free_mem;
 88	}
 
 
 
 
 
 89
 90	memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
 91
 92	poll_dev->private = bdev;
 93	poll_dev->poll = handle_buttons;
 94	poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
 
 
 95
 96	input = poll_dev->input;
 97	input->name = "Cobalt buttons";
 98	input->phys = "cobalt/input0";
 99	input->id.bustype = BUS_HOST;
100	input->dev.parent = &pdev->dev;
101
102	input->keycode = bdev->keymap;
103	input->keycodemax = ARRAY_SIZE(bdev->keymap);
104	input->keycodesize = sizeof(unsigned short);
105
106	input_set_capability(input, EV_MSC, MSC_SCAN);
107	__set_bit(EV_KEY, input->evbit);
108	for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
109		__set_bit(bdev->keymap[i], input->keybit);
110	__clear_bit(KEY_RESERVED, input->keybit);
111
112	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113	if (!res) {
114		error = -EBUSY;
115		goto err_free_mem;
116	}
117
118	bdev->poll_dev = poll_dev;
119	bdev->reg = ioremap(res->start, resource_size(res));
120	dev_set_drvdata(&pdev->dev, bdev);
121
122	error = input_register_polled_device(poll_dev);
123	if (error)
124		goto err_iounmap;
125
126	return 0;
127
128 err_iounmap:
129	iounmap(bdev->reg);
130 err_free_mem:
131	input_free_polled_device(poll_dev);
132	kfree(bdev);
133	return error;
134}
135
136static int cobalt_buttons_remove(struct platform_device *pdev)
137{
138	struct device *dev = &pdev->dev;
139	struct buttons_dev *bdev = dev_get_drvdata(dev);
140
141	input_unregister_polled_device(bdev->poll_dev);
142	input_free_polled_device(bdev->poll_dev);
143	iounmap(bdev->reg);
144	kfree(bdev);
145
146	return 0;
147}
148
149MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
150MODULE_DESCRIPTION("Cobalt button interface driver");
151MODULE_LICENSE("GPL");
152/* work with hotplug and coldplug */
153MODULE_ALIAS("platform:Cobalt buttons");
154
155static struct platform_driver cobalt_buttons_driver = {
156	.probe	= cobalt_buttons_probe,
157	.remove	= cobalt_buttons_remove,
158	.driver	= {
159		.name	= "Cobalt buttons",
160	},
161};
162module_platform_driver(cobalt_buttons_driver);