blob: 0657d785b3cc2ae133aefcebd1e8cfdf0b144881 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -04002/*
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -04003 * SGI Volume Button interface driver
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -04004 *
5 * Copyright (C) 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -04006 */
Dmitry Torokhovaede7a12019-10-29 17:04:18 -07007#include <linux/input.h>
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -04008#include <linux/ioport.h>
9#include <linux/module.h>
10#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040012
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040013#ifdef CONFIG_SGI_IP22
14#include <asm/sgi/ioc.h>
15
16static inline u8 button_status(void)
17{
18 u8 status;
19
20 status = readb(&sgioc->panel) ^ 0xa0;
21 return ((status & 0x80) >> 6) | ((status & 0x20) >> 5);
22}
23#endif
24
25#ifdef CONFIG_SGI_IP32
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040026#include <asm/ip32/mace.h>
27
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040028static inline u8 button_status(void)
29{
30 u64 status;
31
32 status = readq(&mace->perif.audio.control);
33 writeq(status & ~(3U << 23), &mace->perif.audio.control);
34
35 return (status >> 23) & 3;
36}
37#endif
38
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040039#define BUTTONS_POLL_INTERVAL 30 /* msec */
40#define BUTTONS_COUNT_THRESHOLD 3
41
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040042static const unsigned short sgi_map[] = {
43 KEY_VOLUMEDOWN,
44 KEY_VOLUMEUP
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040045};
46
47struct buttons_dev {
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040048 unsigned short keymap[ARRAY_SIZE(sgi_map)];
49 int count[ARRAY_SIZE(sgi_map)];
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040050};
51
Dmitry Torokhovaede7a12019-10-29 17:04:18 -070052static void handle_buttons(struct input_dev *input)
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040053{
Dmitry Torokhovaede7a12019-10-29 17:04:18 -070054 struct buttons_dev *bdev = input_get_drvdata(input);
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040055 u8 status;
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040056 int i;
57
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040058 status = button_status();
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040059
60 for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
61 if (status & (1U << i)) {
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040062 if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
63 input_event(input, EV_MSC, MSC_SCAN, i);
64 input_report_key(input, bdev->keymap[i], 1);
65 input_sync(input);
66 }
67 } else {
68 if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
69 input_event(input, EV_MSC, MSC_SCAN, i);
70 input_report_key(input, bdev->keymap[i], 0);
71 input_sync(input);
72 }
73 bdev->count[i] = 0;
74 }
75 }
76}
77
Bill Pemberton5298cc42012-11-23 21:38:25 -080078static int sgi_buttons_probe(struct platform_device *pdev)
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040079{
80 struct buttons_dev *bdev;
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040081 struct input_dev *input;
82 int error, i;
83
Dmitry Torokhov9e085dd2019-10-29 17:04:10 -070084 bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
85 if (!bdev)
86 return -ENOMEM;
87
Dmitry Torokhovaede7a12019-10-29 17:04:18 -070088 input = devm_input_allocate_device(&pdev->dev);
89 if (!input)
Dmitry Torokhov9e085dd2019-10-29 17:04:10 -070090 return -ENOMEM;
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040091
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040092 memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap));
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040093
Dmitry Torokhovaede7a12019-10-29 17:04:18 -070094 input_set_drvdata(input, bdev);
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040095
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -040096 input->name = "SGI buttons";
97 input->phys = "sgi/input0";
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040098 input->id.bustype = BUS_HOST;
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -040099
100 input->keycode = bdev->keymap;
101 input->keycodemax = ARRAY_SIZE(bdev->keymap);
102 input->keycodesize = sizeof(unsigned short);
103
104 input_set_capability(input, EV_MSC, MSC_SCAN);
105 __set_bit(EV_KEY, input->evbit);
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -0400106 for (i = 0; i < ARRAY_SIZE(sgi_map); i++)
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400107 __set_bit(bdev->keymap[i], input->keybit);
108 __clear_bit(KEY_RESERVED, input->keybit);
109
Dmitry Torokhovaede7a12019-10-29 17:04:18 -0700110 error = input_setup_polling(input, handle_buttons);
111 if (error)
112 return error;
113
114 input_set_poll_interval(input, BUTTONS_POLL_INTERVAL);
115
116 error = input_register_device(input);
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400117 if (error)
Dmitry Torokhov9e085dd2019-10-29 17:04:10 -0700118 return error;
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400119
120 return 0;
121}
122
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -0400123static struct platform_driver sgi_buttons_driver = {
124 .probe = sgi_buttons_probe,
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400125 .driver = {
Thomas Bogendoerfer48ad88b2008-07-19 00:14:26 -0400126 .name = "sgibtns",
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400127 },
128};
JJ Ding840a7462011-11-29 11:08:40 -0800129module_platform_driver(sgi_buttons_driver);
Thomas Bogendoerfer3bee2a02008-07-07 09:07:31 -0400130
Dmitri Vorobiev4c2bdcd2008-10-25 01:46:57 +0300131MODULE_LICENSE("GPL");