1 // SPDX-License-Identifier: GPL-2.0
3 * Support for the four N64 controllers.
5 * Copyright (c) 2021 Lauri Kasanen
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/input.h>
13 #include <linux/limits.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/timer.h>
21 MODULE_AUTHOR("Lauri Kasanen <cand@gmx.com>");
22 MODULE_DESCRIPTION("Driver for N64 controllers");
23 MODULE_LICENSE("GPL");
25 #define PIF_RAM 0x1fc007c0
29 #define SI_WRITE_REG 4
30 #define SI_STATUS_REG 6
32 #define SI_STATUS_DMA_BUSY BIT(0)
33 #define SI_STATUS_IO_BUSY BIT(1)
35 #define N64_CONTROLLER_ID 0x0500
37 #define MAX_CONTROLLERS 4
39 static const char *n64joy_phys[MAX_CONTROLLERS] = {
47 u64 si_buf[8] ____cacheline_aligned;
48 struct timer_list timer;
49 struct mutex n64joy_mutex;
50 struct input_dev *n64joy_dev[MAX_CONTROLLERS];
51 u32 __iomem *reg_base;
56 unsigned int: 16; /* unused */
58 unsigned int: 14; /* unused */
67 unsigned int start: 1;
71 unsigned int right: 1;
72 unsigned int: 2; /* unused */
76 unsigned int c_down: 1;
77 unsigned int c_left: 1;
78 unsigned int c_right: 1;
85 static void n64joy_write_reg(u32 __iomem *reg_base, const u8 reg, const u32 value)
87 writel(value, reg_base + reg);
90 static u32 n64joy_read_reg(u32 __iomem *reg_base, const u8 reg)
92 return readl(reg_base + reg);
95 static void n64joy_wait_si_dma(u32 __iomem *reg_base)
97 while (n64joy_read_reg(reg_base, SI_STATUS_REG) &
98 (SI_STATUS_DMA_BUSY | SI_STATUS_IO_BUSY))
102 static void n64joy_exec_pif(struct n64joy_priv *priv, const u64 in[8])
106 dma_cache_wback_inv((unsigned long) in, 8 * 8);
107 dma_cache_inv((unsigned long) priv->si_buf, 8 * 8);
109 local_irq_save(flags);
111 n64joy_wait_si_dma(priv->reg_base);
114 n64joy_write_reg(priv->reg_base, SI_DRAM_REG, virt_to_phys(in));
116 n64joy_write_reg(priv->reg_base, SI_WRITE_REG, PIF_RAM);
119 n64joy_wait_si_dma(priv->reg_base);
122 n64joy_write_reg(priv->reg_base, SI_DRAM_REG, virt_to_phys(priv->si_buf));
124 n64joy_write_reg(priv->reg_base, SI_READ_REG, PIF_RAM);
127 n64joy_wait_si_dma(priv->reg_base);
129 local_irq_restore(flags);
132 static const u64 polldata[] ____cacheline_aligned = {
143 static void n64joy_poll(struct timer_list *t)
145 const struct joydata *data;
146 struct n64joy_priv *priv = container_of(t, struct n64joy_priv, timer);
147 struct input_dev *dev;
150 n64joy_exec_pif(priv, polldata);
152 data = (struct joydata *) priv->si_buf;
154 for (i = 0; i < MAX_CONTROLLERS; i++) {
155 if (!priv->n64joy_dev[i])
158 dev = priv->n64joy_dev[i];
161 input_report_key(dev, BTN_DPAD_UP, data[i].up);
162 input_report_key(dev, BTN_DPAD_DOWN, data[i].down);
163 input_report_key(dev, BTN_DPAD_LEFT, data[i].left);
164 input_report_key(dev, BTN_DPAD_RIGHT, data[i].right);
167 input_report_key(dev, BTN_FORWARD, data[i].c_up);
168 input_report_key(dev, BTN_BACK, data[i].c_down);
169 input_report_key(dev, BTN_LEFT, data[i].c_left);
170 input_report_key(dev, BTN_RIGHT, data[i].c_right);
172 /* matching buttons */
173 input_report_key(dev, BTN_START, data[i].start);
174 input_report_key(dev, BTN_Z, data[i].z);
176 /* remaining ones: a, b, l, r */
177 input_report_key(dev, BTN_0, data[i].a);
178 input_report_key(dev, BTN_1, data[i].b);
179 input_report_key(dev, BTN_2, data[i].l);
180 input_report_key(dev, BTN_3, data[i].r);
182 input_report_abs(dev, ABS_X, data[i].x);
183 input_report_abs(dev, ABS_Y, data[i].y);
188 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16));
191 static int n64joy_open(struct input_dev *dev)
193 struct n64joy_priv *priv = input_get_drvdata(dev);
196 err = mutex_lock_interruptible(&priv->n64joy_mutex);
200 if (!priv->n64joy_opened) {
202 * We could use the vblank irq, but it's not important if
203 * the poll point slightly changes.
205 timer_setup(&priv->timer, n64joy_poll, 0);
206 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16));
209 priv->n64joy_opened++;
211 mutex_unlock(&priv->n64joy_mutex);
215 static void n64joy_close(struct input_dev *dev)
217 struct n64joy_priv *priv = input_get_drvdata(dev);
219 mutex_lock(&priv->n64joy_mutex);
220 if (!--priv->n64joy_opened)
221 del_timer_sync(&priv->timer);
222 mutex_unlock(&priv->n64joy_mutex);
225 static const u64 __initconst scandata[] ____cacheline_aligned = {
237 * The target device is embedded and RAM-constrained. We save RAM
238 * by initializing in __init code that gets dropped late in boot.
239 * For the same reason there is no module or unloading support.
241 static int __init n64joy_probe(struct platform_device *pdev)
243 const struct joydata *data;
244 struct n64joy_priv *priv;
245 struct input_dev *dev;
249 priv = kzalloc(sizeof(struct n64joy_priv), GFP_KERNEL);
252 mutex_init(&priv->n64joy_mutex);
254 priv->reg_base = devm_platform_ioremap_resource(pdev, 0);
255 if (IS_ERR(priv->reg_base)) {
256 err = PTR_ERR(priv->reg_base);
260 /* The controllers are not hotpluggable, so we can scan in init */
261 n64joy_exec_pif(priv, scandata);
263 data = (struct joydata *) priv->si_buf;
265 for (i = 0; i < MAX_CONTROLLERS; i++) {
266 if (!data[i].err && data[i].data >> 16 == N64_CONTROLLER_ID) {
269 dev = priv->n64joy_dev[i] = input_allocate_device();
270 if (!priv->n64joy_dev[i]) {
275 input_set_drvdata(dev, priv);
277 dev->name = "N64 controller";
278 dev->phys = n64joy_phys[i];
279 dev->id.bustype = BUS_HOST;
281 dev->id.product = data[i].data >> 16;
283 dev->dev.parent = &pdev->dev;
285 dev->open = n64joy_open;
286 dev->close = n64joy_close;
289 input_set_capability(dev, EV_KEY, BTN_DPAD_UP);
290 input_set_capability(dev, EV_KEY, BTN_DPAD_DOWN);
291 input_set_capability(dev, EV_KEY, BTN_DPAD_LEFT);
292 input_set_capability(dev, EV_KEY, BTN_DPAD_RIGHT);
294 input_set_capability(dev, EV_KEY, BTN_LEFT);
295 input_set_capability(dev, EV_KEY, BTN_RIGHT);
296 input_set_capability(dev, EV_KEY, BTN_FORWARD);
297 input_set_capability(dev, EV_KEY, BTN_BACK);
298 /* matching buttons */
299 input_set_capability(dev, EV_KEY, BTN_START);
300 input_set_capability(dev, EV_KEY, BTN_Z);
301 /* remaining ones: a, b, l, r */
302 input_set_capability(dev, EV_KEY, BTN_0);
303 input_set_capability(dev, EV_KEY, BTN_1);
304 input_set_capability(dev, EV_KEY, BTN_2);
305 input_set_capability(dev, EV_KEY, BTN_3);
307 for (j = 0; j < 2; j++)
308 input_set_abs_params(dev, ABS_X + j,
309 S8_MIN, S8_MAX, 0, 0);
311 err = input_register_device(dev);
313 input_free_device(dev);
319 pr_info("%u controller(s) connected\n", found);
326 for (i = 0; i < MAX_CONTROLLERS; i++) {
327 if (!priv->n64joy_dev[i])
329 input_unregister_device(priv->n64joy_dev[i]);
334 static struct platform_driver n64joy_driver = {
340 static int __init n64joy_init(void)
342 return platform_driver_probe(&n64joy_driver, n64joy_probe);
345 module_init(n64joy_init);