^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) * Cobalt 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) 2007-2008 Yoichi Yuasa <yuasa@linux-mips.org>
^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/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define BUTTONS_POLL_INTERVAL 30 /* msec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define BUTTONS_COUNT_THRESHOLD 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define BUTTONS_STATUS_MASK 0xfe000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static const unsigned short cobalt_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) KEY_RESTART,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) KEY_LEFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) KEY_UP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) KEY_DOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) KEY_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) KEY_ENTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) KEY_SELECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct buttons_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned short keymap[ARRAY_SIZE(cobalt_map)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int count[ARRAY_SIZE(cobalt_map)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void __iomem *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static void handle_buttons(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct buttons_dev *bdev = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) uint32_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) status = ~readl(bdev->reg) >> 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for (i = 0; i < ARRAY_SIZE(bdev->keymap); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (status & (1U << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (++bdev->count[i] == BUTTONS_COUNT_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) input_event(input, EV_MSC, MSC_SCAN, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) input_report_key(input, bdev->keymap[i], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (bdev->count[i] >= BUTTONS_COUNT_THRESHOLD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) input_event(input, EV_MSC, MSC_SCAN, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) input_report_key(input, bdev->keymap[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) bdev->count[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int cobalt_buttons_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct buttons_dev *bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) bdev->reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!bdev->reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) memcpy(bdev->keymap, cobalt_map, sizeof(bdev->keymap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) input = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) input_set_drvdata(input, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) input->name = "Cobalt buttons";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) input->phys = "cobalt/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) input->keycode = bdev->keymap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) input->keycodemax = ARRAY_SIZE(bdev->keymap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) input->keycodesize = sizeof(unsigned short);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input_set_capability(input, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) __set_bit(EV_KEY, input->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (i = 0; i < ARRAY_SIZE(cobalt_map); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __set_bit(bdev->keymap[i], input->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __clear_bit(KEY_RESERVED, input->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) error = input_setup_polling(input, handle_buttons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) input_set_poll_interval(input, BUTTONS_POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_DESCRIPTION("Cobalt button interface driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* work with hotplug and coldplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_ALIAS("platform:Cobalt buttons");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static struct platform_driver cobalt_buttons_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .probe = cobalt_buttons_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .name = "Cobalt buttons",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) module_platform_driver(cobalt_buttons_driver);