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