2 * Cobalt button interface driver.
4 * Copyright (C) 2007-2008 Yoichi Yuasa <yuasa@linux-mips.org>
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.
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.
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
20 #include <linux/init.h>
21 #include <linux/input-polldev.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #define BUTTONS_POLL_INTERVAL 30 /* msec */
28 #define BUTTONS_COUNT_THRESHOLD 3
29 #define BUTTONS_STATUS_MASK 0xfe000000
31 static const unsigned short cobalt_map
[] = {
43 struct input_polled_dev
*poll_dev
;
44 unsigned short keymap
[ARRAY_SIZE(cobalt_map
)];
45 int count
[ARRAY_SIZE(cobalt_map
)];
49 static void handle_buttons(struct input_polled_dev
*dev
)
51 struct buttons_dev
*bdev
= dev
->private;
52 struct input_dev
*input
= dev
->input
;
56 status
= ~readl(bdev
->reg
) >> 24;
58 for (i
= 0; i
< ARRAY_SIZE(bdev
->keymap
); i
++) {
59 if (status
& (1U << i
)) {
60 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);
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);
76 static int __devinit
cobalt_buttons_probe(struct platform_device
*pdev
)
78 struct buttons_dev
*bdev
;
79 struct input_polled_dev
*poll_dev
;
80 struct input_dev
*input
;
84 bdev
= kzalloc(sizeof(struct buttons_dev
), GFP_KERNEL
);
85 poll_dev
= input_allocate_polled_device();
86 if (!bdev
|| !poll_dev
) {
91 memcpy(bdev
->keymap
, cobalt_map
, sizeof(bdev
->keymap
));
93 poll_dev
->private = bdev
;
94 poll_dev
->poll
= handle_buttons
;
95 poll_dev
->poll_interval
= BUTTONS_POLL_INTERVAL
;
97 input
= poll_dev
->input
;
98 input
->name
= "Cobalt buttons";
99 input
->phys
= "cobalt/input0";
100 input
->id
.bustype
= BUS_HOST
;
101 input
->dev
.parent
= &pdev
->dev
;
103 input
->keycode
= bdev
->keymap
;
104 input
->keycodemax
= ARRAY_SIZE(bdev
->keymap
);
105 input
->keycodesize
= sizeof(unsigned short);
107 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
108 __set_bit(EV_KEY
, input
->evbit
);
109 for (i
= 0; i
< ARRAY_SIZE(cobalt_map
); i
++)
110 __set_bit(bdev
->keymap
[i
], input
->keybit
);
111 __clear_bit(KEY_RESERVED
, input
->keybit
);
113 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
119 bdev
->poll_dev
= poll_dev
;
120 bdev
->reg
= ioremap(res
->start
, resource_size(res
));
121 dev_set_drvdata(&pdev
->dev
, bdev
);
123 error
= input_register_polled_device(poll_dev
);
132 input_free_polled_device(poll_dev
);
134 dev_set_drvdata(&pdev
->dev
, NULL
);
138 static int __devexit
cobalt_buttons_remove(struct platform_device
*pdev
)
140 struct device
*dev
= &pdev
->dev
;
141 struct buttons_dev
*bdev
= dev_get_drvdata(dev
);
143 input_unregister_polled_device(bdev
->poll_dev
);
144 input_free_polled_device(bdev
->poll_dev
);
147 dev_set_drvdata(dev
, NULL
);
152 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
153 MODULE_DESCRIPTION("Cobalt button interface driver");
154 MODULE_LICENSE("GPL");
155 /* work with hotplug and coldplug */
156 MODULE_ALIAS("platform:Cobalt buttons");
158 static struct platform_driver cobalt_buttons_driver
= {
159 .probe
= cobalt_buttons_probe
,
160 .remove
= __devexit_p(cobalt_buttons_remove
),
162 .name
= "Cobalt buttons",
163 .owner
= THIS_MODULE
,
167 static int __init
cobalt_buttons_init(void)
169 return platform_driver_register(&cobalt_buttons_driver
);
172 static void __exit
cobalt_buttons_exit(void)
174 platform_driver_unregister(&cobalt_buttons_driver
);
177 module_init(cobalt_buttons_init
);
178 module_exit(cobalt_buttons_exit
);