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
);
195 scoped_guard(mutex_intr
, &priv
->n64joy_mutex
) {
196 if (!priv
->n64joy_opened
) {
198 * We could use the vblank irq, but it's not important
199 * if the poll point slightly changes.
201 timer_setup(&priv
->timer
, n64joy_poll
, 0);
202 mod_timer(&priv
->timer
, jiffies
+ msecs_to_jiffies(16));
205 priv
->n64joy_opened
++;
212 static void n64joy_close(struct input_dev
*dev
)
214 struct n64joy_priv
*priv
= input_get_drvdata(dev
);
216 guard(mutex
)(&priv
->n64joy_mutex
);
218 if (!--priv
->n64joy_opened
)
219 del_timer_sync(&priv
->timer
);
222 static const u64 __initconst scandata
[] ____cacheline_aligned
= {
234 * The target device is embedded and RAM-constrained. We save RAM
235 * by initializing in __init code that gets dropped late in boot.
236 * For the same reason there is no module or unloading support.
238 static int __init
n64joy_probe(struct platform_device
*pdev
)
240 const struct joydata
*data
;
241 struct n64joy_priv
*priv
;
242 struct input_dev
*dev
;
246 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
249 mutex_init(&priv
->n64joy_mutex
);
251 priv
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
252 if (IS_ERR(priv
->reg_base
)) {
253 err
= PTR_ERR(priv
->reg_base
);
257 /* The controllers are not hotpluggable, so we can scan in init */
258 n64joy_exec_pif(priv
, scandata
);
260 data
= (struct joydata
*) priv
->si_buf
;
262 for (i
= 0; i
< MAX_CONTROLLERS
; i
++) {
263 if (!data
[i
].err
&& data
[i
].data
>> 16 == N64_CONTROLLER_ID
) {
266 dev
= priv
->n64joy_dev
[i
] = input_allocate_device();
267 if (!priv
->n64joy_dev
[i
]) {
272 input_set_drvdata(dev
, priv
);
274 dev
->name
= "N64 controller";
275 dev
->phys
= n64joy_phys
[i
];
276 dev
->id
.bustype
= BUS_HOST
;
278 dev
->id
.product
= data
[i
].data
>> 16;
280 dev
->dev
.parent
= &pdev
->dev
;
282 dev
->open
= n64joy_open
;
283 dev
->close
= n64joy_close
;
286 input_set_capability(dev
, EV_KEY
, BTN_DPAD_UP
);
287 input_set_capability(dev
, EV_KEY
, BTN_DPAD_DOWN
);
288 input_set_capability(dev
, EV_KEY
, BTN_DPAD_LEFT
);
289 input_set_capability(dev
, EV_KEY
, BTN_DPAD_RIGHT
);
291 input_set_capability(dev
, EV_KEY
, BTN_LEFT
);
292 input_set_capability(dev
, EV_KEY
, BTN_RIGHT
);
293 input_set_capability(dev
, EV_KEY
, BTN_FORWARD
);
294 input_set_capability(dev
, EV_KEY
, BTN_BACK
);
295 /* matching buttons */
296 input_set_capability(dev
, EV_KEY
, BTN_START
);
297 input_set_capability(dev
, EV_KEY
, BTN_Z
);
298 /* remaining ones: a, b, l, r */
299 input_set_capability(dev
, EV_KEY
, BTN_0
);
300 input_set_capability(dev
, EV_KEY
, BTN_1
);
301 input_set_capability(dev
, EV_KEY
, BTN_2
);
302 input_set_capability(dev
, EV_KEY
, BTN_3
);
304 for (j
= 0; j
< 2; j
++)
305 input_set_abs_params(dev
, ABS_X
+ j
,
306 S8_MIN
, S8_MAX
, 0, 0);
308 err
= input_register_device(dev
);
310 input_free_device(dev
);
316 pr_info("%u controller(s) connected\n", found
);
323 for (i
= 0; i
< MAX_CONTROLLERS
; i
++) {
324 if (!priv
->n64joy_dev
[i
])
326 input_unregister_device(priv
->n64joy_dev
[i
]);
331 static struct platform_driver n64joy_driver
= {
337 static int __init
n64joy_init(void)
339 return platform_driver_probe(&n64joy_driver
, n64joy_probe
);
342 module_init(n64joy_init
);