Merge tag 'media/v4.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-block.git] / drivers / input / misc / cobalt_btns.c
CommitLineData
bebb8a2b
YY
1/*
2 * Cobalt button interface driver.
3 *
ada8e951 4 * Copyright (C) 2007-2008 Yoichi Yuasa <yuasa@linux-mips.org>
bebb8a2b
YY
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
3c514387 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bebb8a2b 19 */
3d29cdff 20#include <linux/input-polldev.h>
bebb8a2b
YY
21#include <linux/ioport.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
5a0e3ad6 24#include <linux/slab.h>
bebb8a2b
YY
25
26#define BUTTONS_POLL_INTERVAL 30 /* msec */
27#define BUTTONS_COUNT_THRESHOLD 3
28#define BUTTONS_STATUS_MASK 0xfe000000
29
b037b08e
DT
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
bebb8a2b 41struct buttons_dev {
3d29cdff 42 struct input_polled_dev *poll_dev;
b037b08e
DT
43 unsigned short keymap[ARRAY_SIZE(cobalt_map)];
44 int count[ARRAY_SIZE(cobalt_map)];
bebb8a2b
YY
45 void __iomem *reg;
46};
47
3d29cdff 48static void handle_buttons(struct input_polled_dev *dev)
bebb8a2b 49{
3d29cdff
DT
50 struct buttons_dev *bdev = dev->private;
51 struct input_dev *input = dev->input;
bebb8a2b
YY
52 uint32_t status;
53 int i;
54
b037b08e 55 status = ~readl(bdev->reg) >> 24;
bebb8a2b 56
b037b08e 57 for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
3c514387 58 if (status & (1U << i)) {
b037b08e
DT
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 }
bebb8a2b 64 } else {
b037b08e
DT
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);
3d29cdff 68 input_sync(input);
bebb8a2b 69 }
b037b08e 70 bdev->count[i] = 0;
bebb8a2b 71 }
bebb8a2b 72 }
bebb8a2b
YY
73}
74
5298cc4c 75static int cobalt_buttons_probe(struct platform_device *pdev)
bebb8a2b
YY
76{
77 struct buttons_dev *bdev;
3d29cdff 78 struct input_polled_dev *poll_dev;
bebb8a2b
YY
79 struct input_dev *input;
80 struct resource *res;
81 int error, i;
82
83 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
3d29cdff
DT
84 poll_dev = input_allocate_polled_device();
85 if (!bdev || !poll_dev) {
bebb8a2b
YY
86 error = -ENOMEM;
87 goto err_free_mem;
88 }
89
b037b08e
DT
90 memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
91
3d29cdff
DT
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;
bebb8a2b
YY
97 input->name = "Cobalt buttons";
98 input->phys = "cobalt/input0";
99 input->id.bustype = BUS_HOST;
3c514387 100 input->dev.parent = &pdev->dev;
bebb8a2b 101
3c514387
YY
102 input->keycode = bdev->keymap;
103 input->keycodemax = ARRAY_SIZE(bdev->keymap);
b037b08e
DT
104 input->keycodesize = sizeof(unsigned short);
105
106 input_set_capability(input, EV_MSC, MSC_SCAN);
107 __set_bit(EV_KEY, input->evbit);
3c514387
YY
108 for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
109 __set_bit(bdev->keymap[i], input->keybit);
b037b08e 110 __clear_bit(KEY_RESERVED, input->keybit);
bebb8a2b
YY
111
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 if (!res) {
114 error = -EBUSY;
115 goto err_free_mem;
116 }
117
3d29cdff 118 bdev->poll_dev = poll_dev;
72398e4b 119 bdev->reg = ioremap(res->start, resource_size(res));
bebb8a2b
YY
120 dev_set_drvdata(&pdev->dev, bdev);
121
3d29cdff 122 error = input_register_polled_device(poll_dev);
bebb8a2b
YY
123 if (error)
124 goto err_iounmap;
125
126 return 0;
127
128 err_iounmap:
129 iounmap(bdev->reg);
130 err_free_mem:
3d29cdff 131 input_free_polled_device(poll_dev);
bebb8a2b 132 kfree(bdev);
bebb8a2b
YY
133 return error;
134}
135
e2619cf7 136static int cobalt_buttons_remove(struct platform_device *pdev)
bebb8a2b
YY
137{
138 struct device *dev = &pdev->dev;
139 struct buttons_dev *bdev = dev_get_drvdata(dev);
140
3d29cdff
DT
141 input_unregister_polled_device(bdev->poll_dev);
142 input_free_polled_device(bdev->poll_dev);
bebb8a2b
YY
143 iounmap(bdev->reg);
144 kfree(bdev);
bebb8a2b
YY
145
146 return 0;
147}
148
ada8e951 149MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
26135ed3
MM
150MODULE_DESCRIPTION("Cobalt button interface driver");
151MODULE_LICENSE("GPL");
d7b5247b
KS
152/* work with hotplug and coldplug */
153MODULE_ALIAS("platform:Cobalt buttons");
154
bebb8a2b
YY
155static struct platform_driver cobalt_buttons_driver = {
156 .probe = cobalt_buttons_probe,
1cb0aa88 157 .remove = cobalt_buttons_remove,
bebb8a2b
YY
158 .driver = {
159 .name = "Cobalt buttons",
bebb8a2b
YY
160 },
161};
840a746b 162module_platform_driver(cobalt_buttons_driver);