Skip to content

Commit 52c4e59

Browse files
ArchUsr64dtor
authored andcommitted
Input: driver for Adafruit Seesaw Gamepad
Adds a driver for a mini gamepad that communicates over i2c, the gamepad has bidirectional thumb stick input and six buttons. The gamepad chip utilizes the open framework from Adafruit called 'Seesaw' to transmit the ADC data for the joystick and digital pin state for the buttons. I have only implemented the functionality required to receive the thumb stick and button state. Steps in reading the gamepad state over i2c: 1. Reset the registers 2. Set the pin mode of the pins specified by the `BUTTON_MASK` to input `BUTTON_MASK`: A bit-map for the six digital pins internally connected to the joystick buttons. 3. Enable internal pullup resistors for the `BUTTON_MASK` 4. Bulk set the pin state HIGH for `BUTTON_MASK` 5. Poll the device for button and joystick state done by: `seesaw_read_data(struct i2c_client *client, struct seesaw_data *data)` Product page: https://www.adafruit.com/product/5743 Arduino driver: https://github.com/adafruit/Adafruit_Seesaw Driver tested on RPi Zero 2W Reviewed-by: Thomas Weißschuh <linux@weissschuh.net> Signed-off-by: Anshul Dalal <anshulusr@gmail.com> Link: https://lore.kernel.org/r/20240106015111.882325-2-anshulusr@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent dc5b562 commit 52c4e59

File tree

4 files changed

+333
-0
lines changed

4 files changed

+333
-0
lines changed

MAINTAINERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ W: http://wiki.analog.com/AD7879
441441
W: https://ez.analog.com/linux-software-drivers
442442
F: drivers/input/touchscreen/ad7879.c
443443

444+
ADAFRUIT MINI I2C GAMEPAD
445+
M: Anshul Dalal <anshulusr@gmail.com>
446+
L: linux-input@vger.kernel.org
447+
S: Maintained
448+
F: Documentation/devicetree/bindings/input/adafruit,seesaw-gamepad.yaml
449+
F: drivers/input/joystick/adafruit-seesaw.c
450+
444451
ADDRESS SPACE LAYOUT RANDOMIZATION (ASLR)
445452
M: Jiri Kosina <jikos@kernel.org>
446453
S: Maintained

drivers/input/joystick/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,14 @@ config JOYSTICK_SENSEHAT
412412
To compile this driver as a module, choose M here: the
413413
module will be called sensehat_joystick.
414414

415+
config JOYSTICK_SEESAW
416+
tristate "Adafruit Mini I2C Gamepad with Seesaw"
417+
depends on I2C
418+
select INPUT_SPARSEKMAP
419+
help
420+
Say Y here if you want to use the Adafruit Mini I2C Gamepad.
421+
422+
To compile this driver as a module, choose M here: the module will be
423+
called adafruit-seesaw.
424+
415425
endif

drivers/input/joystick/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ obj-$(CONFIG_JOYSTICK_N64) += n64joy.o
2828
obj-$(CONFIG_JOYSTICK_PSXPAD_SPI) += psxpad-spi.o
2929
obj-$(CONFIG_JOYSTICK_PXRC) += pxrc.o
3030
obj-$(CONFIG_JOYSTICK_QWIIC) += qwiic-joystick.o
31+
obj-$(CONFIG_JOYSTICK_SEESAW) += adafruit-seesaw.o
3132
obj-$(CONFIG_JOYSTICK_SENSEHAT) += sensehat-joystick.o
3233
obj-$(CONFIG_JOYSTICK_SIDEWINDER) += sidewinder.o
3334
obj-$(CONFIG_JOYSTICK_SPACEBALL) += spaceball.o
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2023 Anshul Dalal <anshulusr@gmail.com>
4+
*
5+
* Driver for Adafruit Mini I2C Gamepad
6+
*
7+
* Based on the work of:
8+
* Oleh Kravchenko (Sparkfun Qwiic Joystick driver)
9+
*
10+
* Datasheet: https://cdn-learn.adafruit.com/downloads/pdf/gamepad-qt.pdf
11+
* Product page: https://www.adafruit.com/product/5743
12+
* Firmware and hardware sources: https://github.com/adafruit/Adafruit_Seesaw
13+
*
14+
* TODO:
15+
* - Add interrupt support
16+
*/
17+
18+
#include <asm/unaligned.h>
19+
#include <linux/bits.h>
20+
#include <linux/delay.h>
21+
#include <linux/i2c.h>
22+
#include <linux/input.h>
23+
#include <linux/input/sparse-keymap.h>
24+
#include <linux/kernel.h>
25+
#include <linux/module.h>
26+
27+
#define SEESAW_DEVICE_NAME "seesaw-gamepad"
28+
29+
#define SEESAW_ADC_BASE 0x0900
30+
31+
#define SEESAW_GPIO_DIRCLR_BULK 0x0103
32+
#define SEESAW_GPIO_BULK 0x0104
33+
#define SEESAW_GPIO_BULK_SET 0x0105
34+
#define SEESAW_GPIO_PULLENSET 0x010b
35+
36+
#define SEESAW_STATUS_HW_ID 0x0001
37+
#define SEESAW_STATUS_SWRST 0x007f
38+
39+
#define SEESAW_ADC_OFFSET 0x07
40+
41+
#define SEESAW_BUTTON_A 0x05
42+
#define SEESAW_BUTTON_B 0x01
43+
#define SEESAW_BUTTON_X 0x06
44+
#define SEESAW_BUTTON_Y 0x02
45+
#define SEESAW_BUTTON_START 0x10
46+
#define SEESAW_BUTTON_SELECT 0x00
47+
48+
#define SEESAW_ANALOG_X 0x0e
49+
#define SEESAW_ANALOG_Y 0x0f
50+
51+
#define SEESAW_JOYSTICK_MAX_AXIS 1023
52+
#define SEESAW_JOYSTICK_FUZZ 2
53+
#define SEESAW_JOYSTICK_FLAT 4
54+
55+
#define SEESAW_GAMEPAD_POLL_INTERVAL_MS 16
56+
#define SEESAW_GAMEPAD_POLL_MIN 8
57+
#define SEESAW_GAMEPAD_POLL_MAX 32
58+
59+
static const unsigned long SEESAW_BUTTON_MASK =
60+
BIT(SEESAW_BUTTON_A) | BIT(SEESAW_BUTTON_B) | BIT(SEESAW_BUTTON_X) |
61+
BIT(SEESAW_BUTTON_Y) | BIT(SEESAW_BUTTON_START) |
62+
BIT(SEESAW_BUTTON_SELECT);
63+
64+
struct seesaw_gamepad {
65+
struct input_dev *input_dev;
66+
struct i2c_client *i2c_client;
67+
};
68+
69+
struct seesaw_data {
70+
u16 x;
71+
u16 y;
72+
u32 button_state;
73+
};
74+
75+
static const struct key_entry seesaw_buttons_new[] = {
76+
{ KE_KEY, SEESAW_BUTTON_A, .keycode = BTN_SOUTH },
77+
{ KE_KEY, SEESAW_BUTTON_B, .keycode = BTN_EAST },
78+
{ KE_KEY, SEESAW_BUTTON_X, .keycode = BTN_NORTH },
79+
{ KE_KEY, SEESAW_BUTTON_Y, .keycode = BTN_WEST },
80+
{ KE_KEY, SEESAW_BUTTON_START, .keycode = BTN_START },
81+
{ KE_KEY, SEESAW_BUTTON_SELECT, .keycode = BTN_SELECT },
82+
{ KE_END, 0 }
83+
};
84+
85+
static int seesaw_register_read(struct i2c_client *client, u16 reg, void *buf,
86+
int count)
87+
{
88+
__be16 register_buf = cpu_to_be16(reg);
89+
struct i2c_msg message_buf[2] = {
90+
{
91+
.addr = client->addr,
92+
.flags = client->flags,
93+
.len = sizeof(register_buf),
94+
.buf = (u8 *)&register_buf,
95+
},
96+
{
97+
.addr = client->addr,
98+
.flags = client->flags | I2C_M_RD,
99+
.len = count,
100+
.buf = (u8 *)buf,
101+
},
102+
};
103+
int ret;
104+
105+
ret = i2c_transfer(client->adapter, message_buf,
106+
ARRAY_SIZE(message_buf));
107+
if (ret < 0)
108+
return ret;
109+
110+
return 0;
111+
}
112+
113+
static int seesaw_register_write_u8(struct i2c_client *client, u16 reg,
114+
u8 value)
115+
{
116+
u8 write_buf[sizeof(reg) + sizeof(value)];
117+
int ret;
118+
119+
put_unaligned_be16(reg, write_buf);
120+
write_buf[sizeof(reg)] = value;
121+
122+
ret = i2c_master_send(client, write_buf, sizeof(write_buf));
123+
if (ret < 0)
124+
return ret;
125+
126+
return 0;
127+
}
128+
129+
static int seesaw_register_write_u32(struct i2c_client *client, u16 reg,
130+
u32 value)
131+
{
132+
u8 write_buf[sizeof(reg) + sizeof(value)];
133+
int ret;
134+
135+
put_unaligned_be16(reg, write_buf);
136+
put_unaligned_be32(value, write_buf + sizeof(reg));
137+
ret = i2c_master_send(client, write_buf, sizeof(write_buf));
138+
if (ret < 0)
139+
return ret;
140+
141+
return 0;
142+
}
143+
144+
static int seesaw_read_data(struct i2c_client *client, struct seesaw_data *data)
145+
{
146+
__be16 adc_data;
147+
__be32 read_buf;
148+
int err;
149+
150+
err = seesaw_register_read(client, SEESAW_GPIO_BULK,
151+
&read_buf, sizeof(read_buf));
152+
if (err)
153+
return err;
154+
155+
data->button_state = ~be32_to_cpu(read_buf);
156+
157+
err = seesaw_register_read(client,
158+
SEESAW_ADC_BASE |
159+
(SEESAW_ADC_OFFSET + SEESAW_ANALOG_X),
160+
&adc_data, sizeof(adc_data));
161+
if (err)
162+
return err;
163+
/*
164+
* ADC reads left as max and right as 0, must be reversed since kernel
165+
* expects reports in opposite order.
166+
*/
167+
data->x = SEESAW_JOYSTICK_MAX_AXIS - be16_to_cpu(adc_data);
168+
169+
err = seesaw_register_read(client,
170+
SEESAW_ADC_BASE |
171+
(SEESAW_ADC_OFFSET + SEESAW_ANALOG_Y),
172+
&adc_data, sizeof(adc_data));
173+
if (err)
174+
return err;
175+
176+
data->y = be16_to_cpu(adc_data);
177+
178+
return 0;
179+
}
180+
181+
static void seesaw_poll(struct input_dev *input)
182+
{
183+
struct seesaw_gamepad *private = input_get_drvdata(input);
184+
struct seesaw_data data;
185+
int err, i;
186+
187+
err = seesaw_read_data(private->i2c_client, &data);
188+
if (err) {
189+
dev_err_ratelimited(&input->dev,
190+
"failed to read joystick state: %d\n", err);
191+
return;
192+
}
193+
194+
input_report_abs(input, ABS_X, data.x);
195+
input_report_abs(input, ABS_Y, data.y);
196+
197+
for_each_set_bit(i, &SEESAW_BUTTON_MASK,
198+
BITS_PER_TYPE(SEESAW_BUTTON_MASK)) {
199+
if (!sparse_keymap_report_event(input, i,
200+
data.button_state & BIT(i),
201+
false))
202+
dev_err_ratelimited(&input->dev,
203+
"failed to report keymap event");
204+
}
205+
206+
input_sync(input);
207+
}
208+
209+
static int seesaw_probe(struct i2c_client *client)
210+
{
211+
struct seesaw_gamepad *seesaw;
212+
u8 hardware_id;
213+
int err;
214+
215+
err = seesaw_register_write_u8(client, SEESAW_STATUS_SWRST, 0xFF);
216+
if (err)
217+
return err;
218+
219+
/* Wait for the registers to reset before proceeding */
220+
usleep_range(10000, 15000);
221+
222+
seesaw = devm_kzalloc(&client->dev, sizeof(*seesaw), GFP_KERNEL);
223+
if (!seesaw)
224+
return -ENOMEM;
225+
226+
err = seesaw_register_read(client, SEESAW_STATUS_HW_ID,
227+
&hardware_id, sizeof(hardware_id));
228+
if (err)
229+
return err;
230+
231+
dev_dbg(&client->dev, "Adafruit Seesaw Gamepad, Hardware ID: %02x\n",
232+
hardware_id);
233+
234+
/* Set Pin Mode to input and enable pull-up resistors */
235+
err = seesaw_register_write_u32(client, SEESAW_GPIO_DIRCLR_BULK,
236+
SEESAW_BUTTON_MASK);
237+
if (err)
238+
return err;
239+
err = seesaw_register_write_u32(client, SEESAW_GPIO_PULLENSET,
240+
SEESAW_BUTTON_MASK);
241+
if (err)
242+
return err;
243+
err = seesaw_register_write_u32(client, SEESAW_GPIO_BULK_SET,
244+
SEESAW_BUTTON_MASK);
245+
if (err)
246+
return err;
247+
248+
seesaw->i2c_client = client;
249+
seesaw->input_dev = devm_input_allocate_device(&client->dev);
250+
if (!seesaw->input_dev)
251+
return -ENOMEM;
252+
253+
seesaw->input_dev->id.bustype = BUS_I2C;
254+
seesaw->input_dev->name = "Adafruit Seesaw Gamepad";
255+
seesaw->input_dev->phys = "i2c/" SEESAW_DEVICE_NAME;
256+
input_set_drvdata(seesaw->input_dev, seesaw);
257+
input_set_abs_params(seesaw->input_dev, ABS_X,
258+
0, SEESAW_JOYSTICK_MAX_AXIS,
259+
SEESAW_JOYSTICK_FUZZ, SEESAW_JOYSTICK_FLAT);
260+
input_set_abs_params(seesaw->input_dev, ABS_Y,
261+
0, SEESAW_JOYSTICK_MAX_AXIS,
262+
SEESAW_JOYSTICK_FUZZ, SEESAW_JOYSTICK_FLAT);
263+
264+
err = sparse_keymap_setup(seesaw->input_dev, seesaw_buttons_new, NULL);
265+
if (err) {
266+
dev_err(&client->dev,
267+
"failed to set up input device keymap: %d\n", err);
268+
return err;
269+
}
270+
271+
err = input_setup_polling(seesaw->input_dev, seesaw_poll);
272+
if (err) {
273+
dev_err(&client->dev, "failed to set up polling: %d\n", err);
274+
return err;
275+
}
276+
277+
input_set_poll_interval(seesaw->input_dev,
278+
SEESAW_GAMEPAD_POLL_INTERVAL_MS);
279+
input_set_max_poll_interval(seesaw->input_dev, SEESAW_GAMEPAD_POLL_MAX);
280+
input_set_min_poll_interval(seesaw->input_dev, SEESAW_GAMEPAD_POLL_MIN);
281+
282+
err = input_register_device(seesaw->input_dev);
283+
if (err) {
284+
dev_err(&client->dev, "failed to register joystick: %d\n", err);
285+
return err;
286+
}
287+
288+
return 0;
289+
}
290+
291+
static const struct i2c_device_id seesaw_id_table[] = {
292+
{ SEESAW_DEVICE_NAME },
293+
{ /* Sentinel */ }
294+
};
295+
MODULE_DEVICE_TABLE(i2c, seesaw_id_table);
296+
297+
static const struct of_device_id seesaw_of_table[] = {
298+
{ .compatible = "adafruit,seesaw-gamepad"},
299+
{ /* Sentinel */ }
300+
};
301+
MODULE_DEVICE_TABLE(of, seesaw_of_table);
302+
303+
static struct i2c_driver seesaw_driver = {
304+
.driver = {
305+
.name = SEESAW_DEVICE_NAME,
306+
.of_match_table = seesaw_of_table,
307+
},
308+
.id_table = seesaw_id_table,
309+
.probe = seesaw_probe,
310+
};
311+
module_i2c_driver(seesaw_driver);
312+
313+
MODULE_AUTHOR("Anshul Dalal <anshulusr@gmail.com>");
314+
MODULE_DESCRIPTION("Adafruit Mini I2C Gamepad driver");
315+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)