blob: 53e43d295148a25bcbb0b3eced305a1889be2448 [file] [log] [blame]
Yoichi Yuasabebb8a22007-02-18 01:50:18 -05001/*
2 * Cobalt button interface driver.
3 *
Yoichi Yuasaada8e952009-07-03 00:39:38 +09004 * Copyright (C) 2007-2008 Yoichi Yuasa <yuasa@linux-mips.org>
Yoichi Yuasabebb8a22007-02-18 01:50:18 -05005 *
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
Yoichi Yuasa3c514382008-03-14 11:52:37 -040018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050019 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050026
27#define BUTTONS_POLL_INTERVAL 30 /* msec */
28#define BUTTONS_COUNT_THRESHOLD 3
29#define BUTTONS_STATUS_MASK 0xfe000000
30
Dmitry Torokhovb037b082007-11-04 00:41:36 -040031static const unsigned short cobalt_map[] = {
32 KEY_RESERVED,
33 KEY_RESTART,
34 KEY_LEFT,
35 KEY_UP,
36 KEY_DOWN,
37 KEY_RIGHT,
38 KEY_ENTER,
39 KEY_SELECT
40};
41
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050042struct buttons_dev {
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040043 struct input_polled_dev *poll_dev;
Dmitry Torokhovb037b082007-11-04 00:41:36 -040044 unsigned short keymap[ARRAY_SIZE(cobalt_map)];
45 int count[ARRAY_SIZE(cobalt_map)];
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050046 void __iomem *reg;
47};
48
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040049static void handle_buttons(struct input_polled_dev *dev)
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050050{
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040051 struct buttons_dev *bdev = dev->private;
52 struct input_dev *input = dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050053 uint32_t status;
54 int i;
55
Dmitry Torokhovb037b082007-11-04 00:41:36 -040056 status = ~readl(bdev->reg) >> 24;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050057
Dmitry Torokhovb037b082007-11-04 00:41:36 -040058 for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
Yoichi Yuasa3c514382008-03-14 11:52:37 -040059 if (status & (1U << i)) {
Dmitry Torokhovb037b082007-11-04 00:41:36 -040060 if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
61 input_event(input, EV_MSC, MSC_SCAN, i);
62 input_report_key(input, bdev->keymap[i], 1);
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040063 input_sync(input);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050064 }
Dmitry Torokhovb037b082007-11-04 00:41:36 -040065 } else {
66 if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
67 input_event(input, EV_MSC, MSC_SCAN, i);
68 input_report_key(input, bdev->keymap[i], 0);
69 input_sync(input);
70 }
71 bdev->count[i] = 0;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050072 }
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050073 }
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050074}
75
76static int __devinit cobalt_buttons_probe(struct platform_device *pdev)
77{
78 struct buttons_dev *bdev;
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040079 struct input_polled_dev *poll_dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050080 struct input_dev *input;
81 struct resource *res;
82 int error, i;
83
84 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040085 poll_dev = input_allocate_polled_device();
86 if (!bdev || !poll_dev) {
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050087 error = -ENOMEM;
88 goto err_free_mem;
89 }
90
Dmitry Torokhovb037b082007-11-04 00:41:36 -040091 memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
92
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040093 poll_dev->private = bdev;
94 poll_dev->poll = handle_buttons;
95 poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
96
97 input = poll_dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050098 input->name = "Cobalt buttons";
99 input->phys = "cobalt/input0";
100 input->id.bustype = BUS_HOST;
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400101 input->dev.parent = &pdev->dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500102
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400103 input->keycode = bdev->keymap;
104 input->keycodemax = ARRAY_SIZE(bdev->keymap);
Dmitry Torokhovb037b082007-11-04 00:41:36 -0400105 input->keycodesize = sizeof(unsigned short);
106
107 input_set_capability(input, EV_MSC, MSC_SCAN);
108 __set_bit(EV_KEY, input->evbit);
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400109 for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
110 __set_bit(bdev->keymap[i], input->keybit);
Dmitry Torokhovb037b082007-11-04 00:41:36 -0400111 __clear_bit(KEY_RESERVED, input->keybit);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500112
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;
Julia Lawall72398e42009-07-07 22:04:55 -0700120 bdev->reg = ioremap(res->start, resource_size(res));
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500121 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
Yoichi Yuasaada8e952009-07-03 00:39:38 +0900152MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
Martin Michlmayr26135ed2008-08-15 15:06:21 -0400153MODULE_DESCRIPTION("Cobalt button interface driver");
154MODULE_LICENSE("GPL");
Kay Sieversd7b52472008-04-18 00:24:42 -0400155/* work with hotplug and coldplug */
156MODULE_ALIAS("platform:Cobalt buttons");
157
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500158static struct platform_driver cobalt_buttons_driver = {
159 .probe = cobalt_buttons_probe,
160 .remove = __devexit_p(cobalt_buttons_remove),
161 .driver = {
162 .name = "Cobalt buttons",
163 .owner = THIS_MODULE,
164 },
165};
JJ Ding840a7462011-11-29 11:08:40 -0800166module_platform_driver(cobalt_buttons_driver);