Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 1 | /* |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 2 | * SGI Volume Button interface driver |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de> |
| 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 | */ |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 20 | #include <linux/input-polldev.h> |
| 21 | #include <linux/ioport.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 25 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 26 | #ifdef CONFIG_SGI_IP22 |
| 27 | #include <asm/sgi/ioc.h> |
| 28 | |
| 29 | static inline u8 button_status(void) |
| 30 | { |
| 31 | u8 status; |
| 32 | |
| 33 | status = readb(&sgioc->panel) ^ 0xa0; |
| 34 | return ((status & 0x80) >> 6) | ((status & 0x20) >> 5); |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | #ifdef CONFIG_SGI_IP32 |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 39 | #include <asm/ip32/mace.h> |
| 40 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 41 | static inline u8 button_status(void) |
| 42 | { |
| 43 | u64 status; |
| 44 | |
| 45 | status = readq(&mace->perif.audio.control); |
| 46 | writeq(status & ~(3U << 23), &mace->perif.audio.control); |
| 47 | |
| 48 | return (status >> 23) & 3; |
| 49 | } |
| 50 | #endif |
| 51 | |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 52 | #define BUTTONS_POLL_INTERVAL 30 /* msec */ |
| 53 | #define BUTTONS_COUNT_THRESHOLD 3 |
| 54 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 55 | static const unsigned short sgi_map[] = { |
| 56 | KEY_VOLUMEDOWN, |
| 57 | KEY_VOLUMEUP |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | struct buttons_dev { |
| 61 | struct input_polled_dev *poll_dev; |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 62 | unsigned short keymap[ARRAY_SIZE(sgi_map)]; |
| 63 | int count[ARRAY_SIZE(sgi_map)]; |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static void handle_buttons(struct input_polled_dev *dev) |
| 67 | { |
| 68 | struct buttons_dev *bdev = dev->private; |
| 69 | struct input_dev *input = dev->input; |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 70 | u8 status; |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 71 | int i; |
| 72 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 73 | status = button_status(); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 74 | |
| 75 | for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) { |
| 76 | if (status & (1U << i)) { |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 77 | if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) { |
| 78 | input_event(input, EV_MSC, MSC_SCAN, i); |
| 79 | input_report_key(input, bdev->keymap[i], 1); |
| 80 | input_sync(input); |
| 81 | } |
| 82 | } else { |
| 83 | if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) { |
| 84 | input_event(input, EV_MSC, MSC_SCAN, i); |
| 85 | input_report_key(input, bdev->keymap[i], 0); |
| 86 | input_sync(input); |
| 87 | } |
| 88 | bdev->count[i] = 0; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 93 | static int sgi_buttons_probe(struct platform_device *pdev) |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 94 | { |
| 95 | struct buttons_dev *bdev; |
| 96 | struct input_polled_dev *poll_dev; |
| 97 | struct input_dev *input; |
| 98 | int error, i; |
| 99 | |
| 100 | bdev = kzalloc(sizeof(struct buttons_dev), GFP_KERNEL); |
| 101 | poll_dev = input_allocate_polled_device(); |
| 102 | if (!bdev || !poll_dev) { |
| 103 | error = -ENOMEM; |
| 104 | goto err_free_mem; |
| 105 | } |
| 106 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 107 | memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap)); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 108 | |
| 109 | poll_dev->private = bdev; |
| 110 | poll_dev->poll = handle_buttons; |
| 111 | poll_dev->poll_interval = BUTTONS_POLL_INTERVAL; |
| 112 | |
| 113 | input = poll_dev->input; |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 114 | input->name = "SGI buttons"; |
| 115 | input->phys = "sgi/input0"; |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 116 | input->id.bustype = BUS_HOST; |
| 117 | input->dev.parent = &pdev->dev; |
| 118 | |
| 119 | input->keycode = bdev->keymap; |
| 120 | input->keycodemax = ARRAY_SIZE(bdev->keymap); |
| 121 | input->keycodesize = sizeof(unsigned short); |
| 122 | |
| 123 | input_set_capability(input, EV_MSC, MSC_SCAN); |
| 124 | __set_bit(EV_KEY, input->evbit); |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 125 | for (i = 0; i < ARRAY_SIZE(sgi_map); i++) |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 126 | __set_bit(bdev->keymap[i], input->keybit); |
| 127 | __clear_bit(KEY_RESERVED, input->keybit); |
| 128 | |
| 129 | bdev->poll_dev = poll_dev; |
Jingoo Han | 35c4b12 | 2013-05-23 09:20:21 -0700 | [diff] [blame] | 130 | platform_set_drvdata(pdev, bdev); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 131 | |
| 132 | error = input_register_polled_device(poll_dev); |
| 133 | if (error) |
| 134 | goto err_free_mem; |
| 135 | |
| 136 | return 0; |
| 137 | |
| 138 | err_free_mem: |
| 139 | input_free_polled_device(poll_dev); |
| 140 | kfree(bdev); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 141 | return error; |
| 142 | } |
| 143 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 144 | static int sgi_buttons_remove(struct platform_device *pdev) |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 145 | { |
Jingoo Han | 35c4b12 | 2013-05-23 09:20:21 -0700 | [diff] [blame] | 146 | struct buttons_dev *bdev = platform_get_drvdata(pdev); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 147 | |
| 148 | input_unregister_polled_device(bdev->poll_dev); |
| 149 | input_free_polled_device(bdev->poll_dev); |
| 150 | kfree(bdev); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 155 | static struct platform_driver sgi_buttons_driver = { |
| 156 | .probe = sgi_buttons_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 157 | .remove = sgi_buttons_remove, |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 158 | .driver = { |
Thomas Bogendoerfer | 48ad88b | 2008-07-19 00:14:26 -0400 | [diff] [blame] | 159 | .name = "sgibtns", |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 160 | .owner = THIS_MODULE, |
| 161 | }, |
| 162 | }; |
JJ Ding | 840a746 | 2011-11-29 11:08:40 -0800 | [diff] [blame] | 163 | module_platform_driver(sgi_buttons_driver); |
Thomas Bogendoerfer | 3bee2a0 | 2008-07-07 09:07:31 -0400 | [diff] [blame] | 164 | |
Dmitri Vorobiev | 4c2bdcd | 2008-10-25 01:46:57 +0300 | [diff] [blame] | 165 | MODULE_LICENSE("GPL"); |