summaryrefslogtreecommitdiffstats
path: root/drivers/input/joystick/qwiic-joystick.c
blob: d4da31c0616c3a886d558d9e474560c363f1e7d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2021 Oleh Kravchenko <oleg@kaa.org.ua>
 *
 * SparkFun Qwiic Joystick
 * Product page:https://www.sparkfun.com/products/15168
 * Firmware and hardware sources:https://github.com/sparkfun/Qwiic_Joystick
 */

#include <linux/bits.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/module.h>

#define DRV_NAME "qwiic-joystick"

#define QWIIC_JSK_REG_VERS	1
#define QWIIC_JSK_REG_DATA	3

#define QWIIC_JSK_MAX_AXIS	GENMASK(9, 0)
#define QWIIC_JSK_FUZZ		2
#define QWIIC_JSK_FLAT		2
#define QWIIC_JSK_POLL_INTERVAL	16
#define QWIIC_JSK_POLL_MIN	8
#define QWIIC_JSK_POLL_MAX	32

struct qwiic_jsk {
	char phys[32];
	struct input_dev *dev;
	struct i2c_client *client;
};

struct qwiic_ver {
	u8 major;
	u8 minor;
};

struct qwiic_data {
	__be16 x;
	__be16 y;
	u8 thumb;
};

static void qwiic_poll(struct input_dev *input)
{
	struct qwiic_jsk *priv = input_get_drvdata(input);
	struct qwiic_data data;
	int err;

	err = i2c_smbus_read_i2c_block_data(priv->client, QWIIC_JSK_REG_DATA,
					    sizeof(data), (u8 *)&data);
	if (err != sizeof(data))
		return;

	input_report_abs(input, ABS_X, be16_to_cpu(data.x) >> 6);
	input_report_abs(input, ABS_Y, be16_to_cpu(data.y) >> 6);
	input_report_key(input, BTN_THUMBL, !data.thumb);
	input_sync(input);
}

static int qwiic_probe(struct i2c_client *client)
{
	struct qwiic_jsk *priv;
	struct qwiic_ver vers;
	int err;

	err = i2c_smbus_read_i2c_block_data(client, QWIIC_JSK_REG_VERS,
					    sizeof(vers), (u8 *)&vers);
	if (err < 0)
		return err;
	if (err != sizeof(vers))
		return -EIO;

	dev_dbg(&client->dev, "SparkFun Qwiic Joystick, FW: %u.%u\n",
		vers.major, vers.minor);

	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->client = client;
	snprintf(priv->phys, sizeof(priv->phys),
		 "i2c/%s", dev_name(&client->dev));
	i2c_set_clientdata(client, priv);

	priv->dev = devm_input_allocate_device(&client->dev);
	if (!priv->dev)
		return -ENOMEM;

	priv->dev->id.bustype = BUS_I2C;
	priv->dev->name = "SparkFun Qwiic Joystick";
	priv->dev->phys = priv->phys;
	input_set_drvdata(priv->dev, priv);

	input_set_abs_params(priv->dev, ABS_X, 0, QWIIC_JSK_MAX_AXIS,
			     QWIIC_JSK_FUZZ, QWIIC_JSK_FLAT);
	input_set_abs_params(priv->dev, ABS_Y, 0, QWIIC_JSK_MAX_AXIS,
			     QWIIC_JSK_FUZZ, QWIIC_JSK_FLAT);
	input_set_capability(priv->dev, EV_KEY, BTN_THUMBL);

	err = input_setup_polling(priv->dev, qwiic_poll);
	if (err) {
		dev_err(&client->dev, "failed to set up polling: %d\n", err);
		return err;
	}
	input_set_poll_interval(priv->dev, QWIIC_JSK_POLL_INTERVAL);
	input_set_min_poll_interval(priv->dev, QWIIC_JSK_POLL_MIN);
	input_set_max_poll_interval(priv->dev, QWIIC_JSK_POLL_MAX);

	err = input_register_device(priv->dev);
	if (err) {
		dev_err(&client->dev, "failed to register joystick: %d\n", err);
		return err;
	}

	return 0;
}

#ifdef CONFIG_OF
static const struct of_device_id of_qwiic_match[] = {
	{ .compatible = "sparkfun,qwiic-joystick", },
	{ },
};
MODULE_DEVICE_TABLE(of, of_qwiic_match);
#endif /* CONFIG_OF */

static const struct i2c_device_id qwiic_id_table[] = {
	{ KBUILD_MODNAME, 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, qwiic_id_table);

static struct i2c_driver qwiic_driver = {
	.driver = {
		.name		= DRV_NAME,
		.of_match_table	= of_match_ptr(of_qwiic_match),
	},
	.id_table	= qwiic_id_table,
	.probe_new	= qwiic_probe,
};
module_i2c_driver(qwiic_driver);

MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
MODULE_DESCRIPTION("SparkFun Qwiic Joystick driver");
MODULE_LICENSE("GPL v2");