blob: 1aef97ed5e84575cc43225fd020038e28659f599 [file] [log] [blame]
Yoichi Yuasabebb8a22007-02-18 01:50:18 -05001/*
2 * Cobalt button interface driver.
3 *
4 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
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/init.h>
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040021#include <linux/input-polldev.h>
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050022#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050025
26#define BUTTONS_POLL_INTERVAL 30 /* msec */
27#define BUTTONS_COUNT_THRESHOLD 3
28#define BUTTONS_STATUS_MASK 0xfe000000
29
30struct buttons_dev {
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040031 struct input_polled_dev *poll_dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050032 void __iomem *reg;
33};
34
35struct buttons_map {
36 uint32_t mask;
37 int keycode;
38 int count;
39};
40
41static struct buttons_map buttons_map[] = {
42 { 0x02000000, KEY_RESTART, },
43 { 0x04000000, KEY_LEFT, },
44 { 0x08000000, KEY_UP, },
45 { 0x10000000, KEY_DOWN, },
46 { 0x20000000, KEY_RIGHT, },
47 { 0x40000000, KEY_ENTER, },
48 { 0x80000000, KEY_SELECT, },
49};
50
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040051static void handle_buttons(struct input_polled_dev *dev)
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050052{
53 struct buttons_map *button = buttons_map;
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040054 struct buttons_dev *bdev = dev->private;
55 struct input_dev *input = dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050056 uint32_t status;
57 int i;
58
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050059 status = readl(bdev->reg);
60 status = ~status & BUTTONS_STATUS_MASK;
61
62 for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
63 if (status & button->mask) {
64 button->count++;
65 } else {
66 if (button->count >= BUTTONS_COUNT_THRESHOLD) {
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040067 input_report_key(input, button->keycode, 0);
68 input_sync(input);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050069 }
70 button->count = 0;
71 }
72
73 if (button->count == BUTTONS_COUNT_THRESHOLD) {
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040074 input_report_key(input, button->keycode, 1);
75 input_sync(input);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050076 }
77
78 button++;
79 }
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050080}
81
82static int __devinit cobalt_buttons_probe(struct platform_device *pdev)
83{
84 struct buttons_dev *bdev;
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040085 struct input_polled_dev *poll_dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050086 struct input_dev *input;
87 struct resource *res;
88 int error, i;
89
90 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040091 poll_dev = input_allocate_polled_device();
92 if (!bdev || !poll_dev) {
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050093 error = -ENOMEM;
94 goto err_free_mem;
95 }
96
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040097 poll_dev->private = bdev;
98 poll_dev->poll = handle_buttons;
99 poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
100
101 input = poll_dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500102 input->name = "Cobalt buttons";
103 input->phys = "cobalt/input0";
104 input->id.bustype = BUS_HOST;
105 input->cdev.dev = &pdev->dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500106
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700107 input->evbit[0] = BIT_MASK(EV_KEY);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500108 for (i = 0; i < ARRAY_SIZE(buttons_map); i++) {
109 set_bit(buttons_map[i].keycode, input->keybit);
110 buttons_map[i].count = 0;
111 }
112
113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114 if (!res) {
115 error = -EBUSY;
116 goto err_free_mem;
117 }
118
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400119 bdev->poll_dev = poll_dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500120 bdev->reg = ioremap(res->start, res->end - res->start + 1);
121 dev_set_drvdata(&pdev->dev, bdev);
122
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400123 error = input_register_polled_device(poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500124 if (error)
125 goto err_iounmap;
126
127 return 0;
128
129 err_iounmap:
130 iounmap(bdev->reg);
131 err_free_mem:
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400132 input_free_polled_device(poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500133 kfree(bdev);
134 dev_set_drvdata(&pdev->dev, NULL);
135 return error;
136}
137
138static int __devexit cobalt_buttons_remove(struct platform_device *pdev)
139{
140 struct device *dev = &pdev->dev;
141 struct buttons_dev *bdev = dev_get_drvdata(dev);
142
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400143 input_unregister_polled_device(bdev->poll_dev);
144 input_free_polled_device(bdev->poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500145 iounmap(bdev->reg);
146 kfree(bdev);
147 dev_set_drvdata(dev, NULL);
148
149 return 0;
150}
151
152static struct platform_driver cobalt_buttons_driver = {
153 .probe = cobalt_buttons_probe,
154 .remove = __devexit_p(cobalt_buttons_remove),
155 .driver = {
156 .name = "Cobalt buttons",
157 .owner = THIS_MODULE,
158 },
159};
160
161static int __init cobalt_buttons_init(void)
162{
Dmitry Torokhovd0a05152007-04-12 01:36:12 -0400163 return platform_driver_register(&cobalt_buttons_driver);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500164}
165
166static void __exit cobalt_buttons_exit(void)
167{
168 platform_driver_unregister(&cobalt_buttons_driver);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500169}
170
171module_init(cobalt_buttons_init);
172module_exit(cobalt_buttons_exit);