]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/input/touchscreen/ili210x.c
treewide: Add SPDX license identifier for missed files
[mirror_ubuntu-hirsute-kernel.git] / drivers / input / touchscreen / ili210x.c
CommitLineData
5c6a7a62
OS
1#include <linux/module.h>
2#include <linux/i2c.h>
3#include <linux/interrupt.h>
4#include <linux/slab.h>
5#include <linux/input.h>
6#include <linux/input/mt.h>
f67cc3e9 7#include <linux/input/touchscreen.h>
5c6a7a62
OS
8#include <linux/delay.h>
9#include <linux/workqueue.h>
201f3c80 10#include <linux/gpio/consumer.h>
49588917 11#include <linux/of_device.h>
e3559442 12#include <asm/unaligned.h>
5c6a7a62 13
49588917
MV
14#define ILI210X_TOUCHES 2
15#define ILI251X_TOUCHES 10
5c6a7a62
OS
16#define DEFAULT_POLL_PERIOD 20
17
18/* Touchscreen commands */
19#define REG_TOUCHDATA 0x10
20#define REG_PANEL_INFO 0x20
21#define REG_FIRMWARE_VERSION 0x40
22#define REG_CALIBRATE 0xcc
23
5c6a7a62
OS
24struct firmware_version {
25 u8 id;
26 u8 major;
27 u8 minor;
28} __packed;
29
49588917
MV
30enum ili2xxx_model {
31 MODEL_ILI210X,
32 MODEL_ILI251X,
33};
34
5c6a7a62
OS
35struct ili210x {
36 struct i2c_client *client;
37 struct input_dev *input;
5c6a7a62
OS
38 unsigned int poll_period;
39 struct delayed_work dwork;
201f3c80 40 struct gpio_desc *reset_gpio;
f67cc3e9 41 struct touchscreen_properties prop;
49588917
MV
42 enum ili2xxx_model model;
43 unsigned int max_touches;
5c6a7a62
OS
44};
45
46static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf,
47 size_t len)
48{
49588917 49 struct ili210x *priv = i2c_get_clientdata(client);
5c6a7a62
OS
50 struct i2c_msg msg[2] = {
51 {
52 .addr = client->addr,
53 .flags = 0,
54 .len = 1,
55 .buf = &reg,
56 },
57 {
58 .addr = client->addr,
59 .flags = I2C_M_RD,
60 .len = len,
61 .buf = buf,
62 }
63 };
64
49588917
MV
65 if (priv->model == MODEL_ILI251X) {
66 if (i2c_transfer(client->adapter, msg, 1) != 1) {
67 dev_err(&client->dev, "i2c transfer failed\n");
68 return -EIO;
69 }
70
71 usleep_range(5000, 5500);
72
73 if (i2c_transfer(client->adapter, msg + 1, 1) != 1) {
74 dev_err(&client->dev, "i2c transfer failed\n");
75 return -EIO;
76 }
77 } else {
78 if (i2c_transfer(client->adapter, msg, 2) != 2) {
79 dev_err(&client->dev, "i2c transfer failed\n");
80 return -EIO;
81 }
82 }
83
84 return 0;
85}
86
87static int ili210x_read(struct i2c_client *client, void *buf, size_t len)
88{
89 struct i2c_msg msg = {
90 .addr = client->addr,
91 .flags = I2C_M_RD,
92 .len = len,
93 .buf = buf,
94 };
95
96 if (i2c_transfer(client->adapter, &msg, 1) != 1) {
5c6a7a62
OS
97 dev_err(&client->dev, "i2c transfer failed\n");
98 return -EIO;
99 }
100
101 return 0;
102}
103
e3559442
MV
104static bool ili210x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
105 unsigned int finger,
106 unsigned int *x, unsigned int *y)
107{
49588917 108 if (finger >= ILI210X_TOUCHES)
e3559442
MV
109 return false;
110
111 if (touchdata[0] & BIT(finger))
112 return false;
113
114 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
115 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
116
117 return true;
118}
119
49588917
MV
120static bool ili251x_touchdata_to_coords(struct ili210x *priv, u8 *touchdata,
121 unsigned int finger,
122 unsigned int *x, unsigned int *y)
123{
124 if (finger >= ILI251X_TOUCHES)
125 return false;
126
127 *x = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
128 if (!(*x & BIT(15))) /* Touch indication */
129 return false;
130
131 *x &= 0x3fff;
132 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
133
134 return true;
135}
136
e3559442 137static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
5c6a7a62 138{
e3559442 139 struct input_dev *input = priv->input;
5c6a7a62 140 int i;
49588917
MV
141 bool contact = false, touch = false;
142 unsigned int x = 0, y = 0;
5c6a7a62 143
49588917 144 for (i = 0; i < priv->max_touches; i++) {
49588917
MV
145 if (priv->model == MODEL_ILI210X) {
146 touch = ili210x_touchdata_to_coords(priv, touchdata,
147 i, &x, &y);
148 } else if (priv->model == MODEL_ILI251X) {
149 touch = ili251x_touchdata_to_coords(priv, touchdata,
150 i, &x, &y);
151 if (touch)
152 contact = true;
153 }
154
f67cc3e9 155 input_mt_slot(input, i);
5c6a7a62 156 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
f67cc3e9
MV
157 if (!touch)
158 continue;
159 touchscreen_report_pos(input, &priv->prop, x, y,
160 true);
5c6a7a62
OS
161 }
162
163 input_mt_report_pointer_emulation(input, false);
164 input_sync(input);
e3559442 165
49588917
MV
166 if (priv->model == MODEL_ILI210X)
167 contact = touchdata[0] & 0xf3;
168
169 return contact;
5c6a7a62
OS
170}
171
5c6a7a62
OS
172static void ili210x_work(struct work_struct *work)
173{
174 struct ili210x *priv = container_of(work, struct ili210x,
175 dwork.work);
176 struct i2c_client *client = priv->client;
49588917 177 u8 touchdata[64] = { 0 };
e3559442 178 bool touch;
49588917
MV
179 int error = -EINVAL;
180
181 if (priv->model == MODEL_ILI210X) {
182 error = ili210x_read_reg(client, REG_TOUCHDATA,
183 touchdata, sizeof(touchdata));
184 } else if (priv->model == MODEL_ILI251X) {
185 error = ili210x_read_reg(client, REG_TOUCHDATA,
186 touchdata, 31);
187 if (!error && touchdata[0] == 2)
188 error = ili210x_read(client, &touchdata[31], 20);
189 }
5c6a7a62 190
5c6a7a62
OS
191 if (error) {
192 dev_err(&client->dev,
193 "Unable to get touchdata, err = %d\n", error);
194 return;
195 }
196
e3559442 197 touch = ili210x_report_events(priv, touchdata);
5c6a7a62 198
e3559442 199 if (touch)
5c6a7a62
OS
200 schedule_delayed_work(&priv->dwork,
201 msecs_to_jiffies(priv->poll_period));
202}
203
204static irqreturn_t ili210x_irq(int irq, void *irq_data)
205{
206 struct ili210x *priv = irq_data;
207
208 schedule_delayed_work(&priv->dwork, 0);
209
210 return IRQ_HANDLED;
211}
212
213static ssize_t ili210x_calibrate(struct device *dev,
214 struct device_attribute *attr,
215 const char *buf, size_t count)
216{
217 struct i2c_client *client = to_i2c_client(dev);
218 struct ili210x *priv = i2c_get_clientdata(client);
219 unsigned long calibrate;
220 int rc;
221 u8 cmd = REG_CALIBRATE;
222
223 if (kstrtoul(buf, 10, &calibrate))
224 return -EINVAL;
225
226 if (calibrate > 1)
227 return -EINVAL;
228
229 if (calibrate) {
230 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
231 if (rc != sizeof(cmd))
232 return -EIO;
233 }
234
235 return count;
236}
b27c0d0c 237static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
5c6a7a62
OS
238
239static struct attribute *ili210x_attributes[] = {
240 &dev_attr_calibrate.attr,
241 NULL,
242};
243
244static const struct attribute_group ili210x_attr_group = {
245 .attrs = ili210x_attributes,
246};
247
201f3c80
MV
248static void ili210x_power_down(void *data)
249{
250 struct gpio_desc *reset_gpio = data;
251
252 gpiod_set_value_cansleep(reset_gpio, 1);
253}
254
1bdec5d9
MV
255static void ili210x_cancel_work(void *data)
256{
257 struct ili210x *priv = data;
258
259 cancel_delayed_work_sync(&priv->dwork);
260}
261
5298cc4c 262static int ili210x_i2c_probe(struct i2c_client *client,
5c6a7a62
OS
263 const struct i2c_device_id *id)
264{
265 struct device *dev = &client->dev;
5c6a7a62 266 struct ili210x *priv;
201f3c80 267 struct gpio_desc *reset_gpio;
5c6a7a62 268 struct input_dev *input;
5c6a7a62 269 struct firmware_version firmware;
49588917 270 enum ili2xxx_model model;
5c6a7a62
OS
271 int error;
272
49588917
MV
273 model = (enum ili2xxx_model)id->driver_data;
274
5c6a7a62
OS
275 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
276
5c6a7a62
OS
277 if (client->irq <= 0) {
278 dev_err(dev, "No IRQ!\n");
279 return -EINVAL;
280 }
281
201f3c80
MV
282 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
283 if (IS_ERR(reset_gpio))
284 return PTR_ERR(reset_gpio);
285
286 if (reset_gpio) {
287 error = devm_add_action_or_reset(dev, ili210x_power_down,
288 reset_gpio);
289 if (error)
290 return error;
291
292 usleep_range(50, 100);
293 gpiod_set_value_cansleep(reset_gpio, 0);
294 msleep(100);
295 }
296
12294577
MV
297 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
298 if (!priv)
299 return -ENOMEM;
300
301 input = devm_input_allocate_device(dev);
302 if (!input)
303 return -ENOMEM;
304
305 priv->client = client;
306 priv->input = input;
307 priv->poll_period = DEFAULT_POLL_PERIOD;
308 INIT_DELAYED_WORK(&priv->dwork, ili210x_work);
309 priv->reset_gpio = reset_gpio;
49588917
MV
310 priv->model = model;
311 if (model == MODEL_ILI210X)
312 priv->max_touches = ILI210X_TOUCHES;
313 if (model == MODEL_ILI251X)
314 priv->max_touches = ILI251X_TOUCHES;
12294577
MV
315
316 i2c_set_clientdata(client, priv);
317
5c6a7a62
OS
318 /* Get firmware version */
319 error = ili210x_read_reg(client, REG_FIRMWARE_VERSION,
320 &firmware, sizeof(firmware));
321 if (error) {
322 dev_err(dev, "Failed to get firmware version, err: %d\n",
323 error);
324 return error;
325 }
326
5c6a7a62
OS
327 /* Setup input device */
328 input->name = "ILI210x Touchscreen";
329 input->id.bustype = BUS_I2C;
330 input->dev.parent = dev;
331
5c6a7a62 332 /* Multi touch */
f67cc3e9
MV
333 input_set_abs_params(input, ABS_MT_POSITION_X, 0, 0xffff, 0, 0);
334 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, 0xffff, 0, 0);
335 touchscreen_parse_properties(input, true, &priv->prop);
336 input_mt_init_slots(input, priv->max_touches, INPUT_MT_DIRECT);
5c6a7a62 337
1bdec5d9
MV
338 error = devm_add_action(dev, ili210x_cancel_work, priv);
339 if (error)
340 return error;
341
342 error = devm_request_irq(dev, client->irq, ili210x_irq, 0,
343 client->name, priv);
5c6a7a62
OS
344 if (error) {
345 dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
346 error);
1bdec5d9 347 return error;
5c6a7a62
OS
348 }
349
576057bf 350 error = devm_device_add_group(dev, &ili210x_attr_group);
5c6a7a62
OS
351 if (error) {
352 dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
353 error);
1bdec5d9 354 return error;
5c6a7a62
OS
355 }
356
357 error = input_register_device(priv->input);
358 if (error) {
971bd8fa 359 dev_err(dev, "Cannot register input device, err: %d\n", error);
576057bf 360 return error;
5c6a7a62
OS
361 }
362
d7ddf154 363 device_init_wakeup(dev, 1);
5c6a7a62
OS
364
365 dev_dbg(dev,
366 "ILI210x initialized (IRQ: %d), firmware version %d.%d.%d",
367 client->irq, firmware.id, firmware.major, firmware.minor);
368
369 return 0;
5c6a7a62
OS
370}
371
02b6a58b 372static int __maybe_unused ili210x_i2c_suspend(struct device *dev)
5c6a7a62
OS
373{
374 struct i2c_client *client = to_i2c_client(dev);
375
376 if (device_may_wakeup(&client->dev))
377 enable_irq_wake(client->irq);
378
379 return 0;
380}
381
02b6a58b 382static int __maybe_unused ili210x_i2c_resume(struct device *dev)
5c6a7a62
OS
383{
384 struct i2c_client *client = to_i2c_client(dev);
385
386 if (device_may_wakeup(&client->dev))
387 disable_irq_wake(client->irq);
388
389 return 0;
390}
5c6a7a62
OS
391
392static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm,
393 ili210x_i2c_suspend, ili210x_i2c_resume);
394
395static const struct i2c_device_id ili210x_i2c_id[] = {
49588917
MV
396 { "ili210x", MODEL_ILI210X },
397 { "ili251x", MODEL_ILI251X },
5c6a7a62
OS
398 { }
399};
400MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
401
c5d0e4b5 402static const struct of_device_id ili210x_dt_ids[] = {
49588917
MV
403 { .compatible = "ilitek,ili210x", .data = (void *)MODEL_ILI210X },
404 { .compatible = "ilitek,ili251x", .data = (void *)MODEL_ILI251X },
c5d0e4b5
MV
405 { },
406};
407MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
408
5c6a7a62
OS
409static struct i2c_driver ili210x_ts_driver = {
410 .driver = {
411 .name = "ili210x_i2c",
5c6a7a62 412 .pm = &ili210x_i2c_pm,
c5d0e4b5 413 .of_match_table = ili210x_dt_ids,
5c6a7a62
OS
414 },
415 .id_table = ili210x_i2c_id,
416 .probe = ili210x_i2c_probe,
5c6a7a62
OS
417};
418
419module_i2c_driver(ili210x_ts_driver);
420
421MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
422MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
423MODULE_LICENSE("GPL");