xref: /openbmc/linux/sound/pci/au88x0/au88x0_game.c (revision e3d786a3)
1 /*
2  *  Manuel Jander.
3  *
4  *  Based on the work of:
5  *  Vojtech Pavlik
6  *  Raymond Ingles
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * Should you need to contact me, the author, you can do so either by
23  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
24  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
25  *
26  * Based 90% on Vojtech Pavlik pcigame driver.
27  * Merged and modified by Manuel Jander, for the OpenVortex
28  * driver. (email: mjander@embedded.cl).
29  */
30 
31 #include <linux/time.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <sound/core.h>
35 #include "au88x0.h"
36 #include <linux/gameport.h>
37 #include <linux/export.h>
38 
39 #if IS_REACHABLE(CONFIG_GAMEPORT)
40 
41 #define VORTEX_GAME_DWAIT	20	/* 20 ms */
42 
43 static unsigned char vortex_game_read(struct gameport *gameport)
44 {
45 	vortex_t *vortex = gameport_get_port_data(gameport);
46 	return hwread(vortex->mmio, VORTEX_GAME_LEGACY);
47 }
48 
49 static void vortex_game_trigger(struct gameport *gameport)
50 {
51 	vortex_t *vortex = gameport_get_port_data(gameport);
52 	hwwrite(vortex->mmio, VORTEX_GAME_LEGACY, 0xff);
53 }
54 
55 static int
56 vortex_game_cooked_read(struct gameport *gameport, int *axes, int *buttons)
57 {
58 	vortex_t *vortex = gameport_get_port_data(gameport);
59 	int i;
60 
61 	*buttons = (~hwread(vortex->mmio, VORTEX_GAME_LEGACY) >> 4) & 0xf;
62 
63 	for (i = 0; i < 4; i++) {
64 		axes[i] =
65 		    hwread(vortex->mmio, VORTEX_GAME_AXIS + (i * AXIS_SIZE));
66 		if (axes[i] == AXIS_RANGE)
67 			axes[i] = -1;
68 	}
69 	return 0;
70 }
71 
72 static int vortex_game_open(struct gameport *gameport, int mode)
73 {
74 	vortex_t *vortex = gameport_get_port_data(gameport);
75 
76 	switch (mode) {
77 	case GAMEPORT_MODE_COOKED:
78 		hwwrite(vortex->mmio, VORTEX_CTRL2,
79 			hwread(vortex->mmio,
80 			       VORTEX_CTRL2) | CTRL2_GAME_ADCMODE);
81 		msleep(VORTEX_GAME_DWAIT);
82 		return 0;
83 	case GAMEPORT_MODE_RAW:
84 		hwwrite(vortex->mmio, VORTEX_CTRL2,
85 			hwread(vortex->mmio,
86 			       VORTEX_CTRL2) & ~CTRL2_GAME_ADCMODE);
87 		return 0;
88 	default:
89 		return -1;
90 	}
91 
92 	return 0;
93 }
94 
95 static int vortex_gameport_register(vortex_t *vortex)
96 {
97 	struct gameport *gp;
98 
99 	vortex->gameport = gp = gameport_allocate_port();
100 	if (!gp) {
101 		dev_err(vortex->card->dev,
102 			"cannot allocate memory for gameport\n");
103 		return -ENOMEM;
104 	}
105 
106 	gameport_set_name(gp, "AU88x0 Gameport");
107 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(vortex->pci_dev));
108 	gameport_set_dev_parent(gp, &vortex->pci_dev->dev);
109 
110 	gp->read = vortex_game_read;
111 	gp->trigger = vortex_game_trigger;
112 	gp->cooked_read = vortex_game_cooked_read;
113 	gp->open = vortex_game_open;
114 
115 	gameport_set_port_data(gp, vortex);
116 	gp->fuzz = 64;
117 
118 	gameport_register_port(gp);
119 
120 	return 0;
121 }
122 
123 static void vortex_gameport_unregister(vortex_t * vortex)
124 {
125 	if (vortex->gameport) {
126 		gameport_unregister_port(vortex->gameport);
127 		vortex->gameport = NULL;
128 	}
129 }
130 
131 #else
132 static inline int vortex_gameport_register(vortex_t * vortex) { return -ENOSYS; }
133 static inline void vortex_gameport_unregister(vortex_t * vortex) { }
134 #endif
135