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