1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for the Winmate FM07 front-panel keys
5 // Author: Daniel Beer <daniel.beer@tirotech.co.nz>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/input.h>
10 #include <linux/ioport.h>
11 #include <linux/platform_device.h>
12 #include <linux/dmi.h>
15 #define DRV_NAME "winmate-fm07keys"
18 #define PORT_DATA 0x68
20 #define EC_ADDR_KEYS 0x3b
21 #define EC_CMD_READ 0x80
23 #define BASE_KEY KEY_F13
26 /* Typically we're done in fewer than 10 iterations */
27 #define LOOP_TIMEOUT 1000
29 static void fm07keys_poll(struct input_dev
*input
)
34 /* Flush output buffer */
36 while (inb(PORT_CMD
) & 0x01) {
37 if (++i
>= LOOP_TIMEOUT
)
42 /* Send request and wait for write completion */
43 outb(EC_CMD_READ
, PORT_CMD
);
45 while (inb(PORT_CMD
) & 0x02)
46 if (++i
>= LOOP_TIMEOUT
)
49 outb(EC_ADDR_KEYS
, PORT_DATA
);
51 while (inb(PORT_CMD
) & 0x02)
52 if (++i
>= LOOP_TIMEOUT
)
55 /* Wait for data ready */
57 while (!(inb(PORT_CMD
) & 0x01))
58 if (++i
>= LOOP_TIMEOUT
)
62 /* Notify of new key states */
63 for (i
= 0; i
< NUM_KEYS
; i
++) {
64 input_report_key(input
, BASE_KEY
+ i
, (~k
) & 1);
72 dev_warn_ratelimited(&input
->dev
, "timeout polling IO memory\n");
75 static int fm07keys_probe(struct platform_device
*pdev
)
77 struct device
*dev
= &pdev
->dev
;
78 struct input_dev
*input
;
82 input
= devm_input_allocate_device(dev
);
84 dev_err(dev
, "no memory for input device\n");
88 if (!devm_request_region(dev
, PORT_CMD
, 1, "Winmate FM07 EC"))
90 if (!devm_request_region(dev
, PORT_DATA
, 1, "Winmate FM07 EC"))
93 input
->name
= "Winmate FM07 front-panel keys";
94 input
->phys
= DRV_NAME
"/input0";
96 input
->id
.bustype
= BUS_HOST
;
97 input
->id
.vendor
= 0x0001;
98 input
->id
.product
= 0x0001;
99 input
->id
.version
= 0x0100;
101 __set_bit(EV_KEY
, input
->evbit
);
103 for (i
= 0; i
< NUM_KEYS
; i
++)
104 __set_bit(BASE_KEY
+ i
, input
->keybit
);
106 ret
= input_setup_polling(input
, fm07keys_poll
);
108 dev_err(dev
, "unable to set up polling, err=%d\n", ret
);
112 /* These are silicone buttons. They can't be pressed in rapid
113 * succession too quickly, and 50 Hz seems to be an adequate
114 * sampling rate without missing any events when tested.
116 input_set_poll_interval(input
, 20);
118 ret
= input_register_device(input
);
120 dev_err(dev
, "unable to register polled device, err=%d\n",
129 static struct platform_driver fm07keys_driver
= {
130 .probe
= fm07keys_probe
,
136 static struct platform_device
*dev
;
138 static const struct dmi_system_id fm07keys_dmi_table
[] __initconst
= {
142 DMI_MATCH(DMI_SYS_VENDOR
, "Winmate Inc."),
143 DMI_MATCH(DMI_PRODUCT_NAME
, "IP30"),
149 MODULE_DEVICE_TABLE(dmi
, fm07keys_dmi_table
);
151 static int __init
fm07keys_init(void)
155 if (!dmi_check_system(fm07keys_dmi_table
))
158 ret
= platform_driver_register(&fm07keys_driver
);
160 pr_err("fm07keys: failed to register driver, err=%d\n", ret
);
164 dev
= platform_device_register_simple(DRV_NAME
, PLATFORM_DEVID_NONE
, NULL
, 0);
167 pr_err("fm07keys: failed to allocate device, err = %d\n", ret
);
174 platform_driver_unregister(&fm07keys_driver
);
178 static void __exit
fm07keys_exit(void)
180 platform_driver_unregister(&fm07keys_driver
);
181 platform_device_unregister(dev
);
184 module_init(fm07keys_init
);
185 module_exit(fm07keys_exit
);
187 MODULE_AUTHOR("Daniel Beer <daniel.beer@tirotech.co.nz>");
188 MODULE_DESCRIPTION("Winmate FM07 front-panel keys driver");
189 MODULE_LICENSE("GPL");