^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SGI Volume Button interface driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #ifdef CONFIG_SGI_IP22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/sgi/ioc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static inline u8 button_status(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) status = readb(&sgioc->panel) ^ 0xa0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return ((status & 0x80) >> 6) | ((status & 0x20) >> 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifdef CONFIG_SGI_IP32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/ip32/mace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static inline u8 button_status(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u64 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) status = readq(&mace->perif.audio.control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) writeq(status & ~(3U << 23), &mace->perif.audio.control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return (status >> 23) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define BUTTONS_POLL_INTERVAL 30 /* msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define BUTTONS_COUNT_THRESHOLD 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static const unsigned short sgi_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) KEY_VOLUMEDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) KEY_VOLUMEUP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct buttons_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned short keymap[ARRAY_SIZE(sgi_map)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int count[ARRAY_SIZE(sgi_map)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void handle_buttons(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct buttons_dev *bdev = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) status = button_status();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (status & (1U << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) input_event(input, EV_MSC, MSC_SCAN, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) input_report_key(input, bdev->keymap[i], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) input_event(input, EV_MSC, MSC_SCAN, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) input_report_key(input, bdev->keymap[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) bdev->count[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int sgi_buttons_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct buttons_dev *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) input = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) memcpy(bdev->keymap, sgi_map, sizeof(bdev->keymap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) input_set_drvdata(input, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input->name = "SGI buttons";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) input->phys = "sgi/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) input->keycode = bdev->keymap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) input->keycodemax = ARRAY_SIZE(bdev->keymap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) input->keycodesize = sizeof(unsigned short);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) input_set_capability(input, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __set_bit(EV_KEY, input->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) for (i = 0; i < ARRAY_SIZE(sgi_map); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __set_bit(bdev->keymap[i], input->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __clear_bit(KEY_RESERVED, input->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) error = input_setup_polling(input, handle_buttons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) input_set_poll_interval(input, BUTTONS_POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct platform_driver sgi_buttons_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .probe = sgi_buttons_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .name = "sgibtns",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) module_platform_driver(sgi_buttons_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) MODULE_LICENSE("GPL");