blob: fbe72afc9347a3c09c230250021654f63a2405ba [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 */
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040020#include <linux/input-polldev.h>
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050021#include <linux/ioport.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.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
Dmitry Torokhovb037b082007-11-04 00:41:36 -040030static 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
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050041struct buttons_dev {
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040042 struct input_polled_dev *poll_dev;
Dmitry Torokhovb037b082007-11-04 00:41:36 -040043 unsigned short keymap[ARRAY_SIZE(cobalt_map)];
44 int count[ARRAY_SIZE(cobalt_map)];
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050045 void __iomem *reg;
46};
47
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040048static void handle_buttons(struct input_polled_dev *dev)
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050049{
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040050 struct buttons_dev *bdev = dev->private;
51 struct input_dev *input = dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050052 uint32_t status;
53 int i;
54
Dmitry Torokhovb037b082007-11-04 00:41:36 -040055 status = ~readl(bdev->reg) >> 24;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050056
Dmitry Torokhovb037b082007-11-04 00:41:36 -040057 for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
Yoichi Yuasa3c514382008-03-14 11:52:37 -040058 if (status & (1U << i)) {
Dmitry Torokhovb037b082007-11-04 00:41:36 -040059 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);
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040062 input_sync(input);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050063 }
Dmitry Torokhovb037b082007-11-04 00:41:36 -040064 } 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;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050071 }
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050072 }
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050073}
74
Bill Pemberton5298cc42012-11-23 21:38:25 -080075static int cobalt_buttons_probe(struct platform_device *pdev)
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050076{
77 struct buttons_dev *bdev;
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040078 struct input_polled_dev *poll_dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050079 struct input_dev *input;
80 struct resource *res;
81 int error, i;
82
83 bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL);
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040084 poll_dev = input_allocate_polled_device();
85 if (!bdev || !poll_dev) {
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050086 error = -ENOMEM;
87 goto err_free_mem;
88 }
89
Dmitry Torokhovb037b082007-11-04 00:41:36 -040090 memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
91
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -040092 poll_dev->private = bdev;
93 poll_dev->poll = handle_buttons;
94 poll_dev->poll_interval = BUTTONS_POLL_INTERVAL;
95
96 input = poll_dev->input;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -050097 input->name = "Cobalt buttons";
98 input->phys = "cobalt/input0";
99 input->id.bustype = BUS_HOST;
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400100 input->dev.parent = &pdev->dev;
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500101
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400102 input->keycode = bdev->keymap;
103 input->keycodemax = ARRAY_SIZE(bdev->keymap);
Dmitry Torokhovb037b082007-11-04 00:41:36 -0400104 input->keycodesize = sizeof(unsigned short);
105
106 input_set_capability(input, EV_MSC, MSC_SCAN);
107 __set_bit(EV_KEY, input->evbit);
Yoichi Yuasa3c514382008-03-14 11:52:37 -0400108 for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
109 __set_bit(bdev->keymap[i], input->keybit);
Dmitry Torokhovb037b082007-11-04 00:41:36 -0400110 __clear_bit(KEY_RESERVED, input->keybit);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500111
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 if (!res) {
114 error = -EBUSY;
115 goto err_free_mem;
116 }
117
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400118 bdev->poll_dev = poll_dev;
Julia Lawall72398e42009-07-07 22:04:55 -0700119 bdev->reg = ioremap(res->start, resource_size(res));
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500120 dev_set_drvdata(&pdev->dev, bdev);
121
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400122 error = input_register_polled_device(poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500123 if (error)
124 goto err_iounmap;
125
126 return 0;
127
128 err_iounmap:
129 iounmap(bdev->reg);
130 err_free_mem:
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400131 input_free_polled_device(poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500132 kfree(bdev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500133 return error;
134}
135
Bill Pembertone2619cf2012-11-23 21:50:47 -0800136static int cobalt_buttons_remove(struct platform_device *pdev)
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500137{
138 struct device *dev = &pdev->dev;
139 struct buttons_dev *bdev = dev_get_drvdata(dev);
140
Dmitry Torokhov3d29cdf2007-04-29 23:43:06 -0400141 input_unregister_polled_device(bdev->poll_dev);
142 input_free_polled_device(bdev->poll_dev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500143 iounmap(bdev->reg);
144 kfree(bdev);
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500145
146 return 0;
147}
148
Yoichi Yuasaada8e952009-07-03 00:39:38 +0900149MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
Martin Michlmayr26135ed2008-08-15 15:06:21 -0400150MODULE_DESCRIPTION("Cobalt button interface driver");
151MODULE_LICENSE("GPL");
Kay Sieversd7b52472008-04-18 00:24:42 -0400152/* work with hotplug and coldplug */
153MODULE_ALIAS("platform:Cobalt buttons");
154
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500155static struct platform_driver cobalt_buttons_driver = {
156 .probe = cobalt_buttons_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800157 .remove = cobalt_buttons_remove,
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500158 .driver = {
159 .name = "Cobalt buttons",
Yoichi Yuasabebb8a22007-02-18 01:50:18 -0500160 },
161};
JJ Ding840a7462011-11-29 11:08:40 -0800162module_platform_driver(cobalt_buttons_driver);