Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / touchscreen / eeti_ts.c
blobc6b85ba7f991fe5b43ec1d4b901df92fca31d817
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Touch Screen driver for EETI's I2C connected touch screen panels
4 * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
6 * See EETI's software guide for the protocol specification:
7 * http://home.eeti.com.tw/documentation.html
9 * Based on migor_ts.c
10 * Copyright (c) 2008 Magnus Damm
11 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/input.h>
17 #include <linux/input/touchscreen.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/timer.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23 #include <linux/slab.h>
24 #include <asm/unaligned.h>
26 struct eeti_ts {
27 struct i2c_client *client;
28 struct input_dev *input;
29 struct gpio_desc *attn_gpio;
30 struct touchscreen_properties props;
31 bool running;
34 #define EETI_TS_BITDEPTH (11)
35 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
37 #define REPORT_BIT_PRESSED BIT(0)
38 #define REPORT_BIT_AD0 BIT(1)
39 #define REPORT_BIT_AD1 BIT(2)
40 #define REPORT_BIT_HAS_PRESSURE BIT(6)
41 #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
43 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
45 unsigned int res;
46 u16 x, y;
48 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
50 x = get_unaligned_be16(&buf[1]);
51 y = get_unaligned_be16(&buf[3]);
53 /* fix the range to 11 bits */
54 x >>= res - EETI_TS_BITDEPTH;
55 y >>= res - EETI_TS_BITDEPTH;
57 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
58 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
60 touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
61 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
62 input_sync(eeti->input);
65 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
67 struct eeti_ts *eeti = dev_id;
68 int len;
69 int error;
70 char buf[6];
72 do {
73 len = i2c_master_recv(eeti->client, buf, sizeof(buf));
74 if (len != sizeof(buf)) {
75 error = len < 0 ? len : -EIO;
76 dev_err(&eeti->client->dev,
77 "failed to read touchscreen data: %d\n",
78 error);
79 break;
82 if (buf[0] & 0x80) {
83 /* Motion packet */
84 eeti_ts_report_event(eeti, buf);
86 } while (eeti->running &&
87 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
89 return IRQ_HANDLED;
92 static void eeti_ts_start(struct eeti_ts *eeti)
94 eeti->running = true;
95 wmb();
96 enable_irq(eeti->client->irq);
99 static void eeti_ts_stop(struct eeti_ts *eeti)
101 eeti->running = false;
102 wmb();
103 disable_irq(eeti->client->irq);
106 static int eeti_ts_open(struct input_dev *dev)
108 struct eeti_ts *eeti = input_get_drvdata(dev);
110 eeti_ts_start(eeti);
112 return 0;
115 static void eeti_ts_close(struct input_dev *dev)
117 struct eeti_ts *eeti = input_get_drvdata(dev);
119 eeti_ts_stop(eeti);
122 static int eeti_ts_probe(struct i2c_client *client,
123 const struct i2c_device_id *idp)
125 struct device *dev = &client->dev;
126 struct eeti_ts *eeti;
127 struct input_dev *input;
128 int error;
131 * In contrast to what's described in the datasheet, there seems
132 * to be no way of probing the presence of that device using I2C
133 * commands. So we need to blindly believe it is there, and wait
134 * for interrupts to occur.
137 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
138 if (!eeti) {
139 dev_err(dev, "failed to allocate driver data\n");
140 return -ENOMEM;
143 input = devm_input_allocate_device(dev);
144 if (!input) {
145 dev_err(dev, "Failed to allocate input device.\n");
146 return -ENOMEM;
149 input_set_capability(input, EV_KEY, BTN_TOUCH);
151 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
152 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
153 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
155 touchscreen_parse_properties(input, false, &eeti->props);
157 input->name = client->name;
158 input->id.bustype = BUS_I2C;
159 input->open = eeti_ts_open;
160 input->close = eeti_ts_close;
162 eeti->client = client;
163 eeti->input = input;
165 eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
166 if (IS_ERR(eeti->attn_gpio))
167 return PTR_ERR(eeti->attn_gpio);
169 i2c_set_clientdata(client, eeti);
170 input_set_drvdata(input, eeti);
172 error = devm_request_threaded_irq(dev, client->irq,
173 NULL, eeti_ts_isr,
174 IRQF_ONESHOT,
175 client->name, eeti);
176 if (error) {
177 dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
178 error);
179 return error;
183 * Disable the device for now. It will be enabled once the
184 * input device is opened.
186 eeti_ts_stop(eeti);
188 error = input_register_device(input);
189 if (error)
190 return error;
192 return 0;
195 static int __maybe_unused eeti_ts_suspend(struct device *dev)
197 struct i2c_client *client = to_i2c_client(dev);
198 struct eeti_ts *eeti = i2c_get_clientdata(client);
199 struct input_dev *input_dev = eeti->input;
201 mutex_lock(&input_dev->mutex);
203 if (input_dev->users)
204 eeti_ts_stop(eeti);
206 mutex_unlock(&input_dev->mutex);
208 if (device_may_wakeup(&client->dev))
209 enable_irq_wake(client->irq);
211 return 0;
214 static int __maybe_unused eeti_ts_resume(struct device *dev)
216 struct i2c_client *client = to_i2c_client(dev);
217 struct eeti_ts *eeti = i2c_get_clientdata(client);
218 struct input_dev *input_dev = eeti->input;
220 if (device_may_wakeup(&client->dev))
221 disable_irq_wake(client->irq);
223 mutex_lock(&input_dev->mutex);
225 if (input_dev->users)
226 eeti_ts_start(eeti);
228 mutex_unlock(&input_dev->mutex);
230 return 0;
233 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
235 static const struct i2c_device_id eeti_ts_id[] = {
236 { "eeti_ts", 0 },
239 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
241 #ifdef CONFIG_OF
242 static const struct of_device_id of_eeti_ts_match[] = {
243 { .compatible = "eeti,exc3000-i2c", },
246 #endif
248 static struct i2c_driver eeti_ts_driver = {
249 .driver = {
250 .name = "eeti_ts",
251 .pm = &eeti_ts_pm,
252 .of_match_table = of_match_ptr(of_eeti_ts_match),
254 .probe = eeti_ts_probe,
255 .id_table = eeti_ts_id,
258 module_i2c_driver(eeti_ts_driver);
260 MODULE_DESCRIPTION("EETI Touchscreen driver");
261 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
262 MODULE_LICENSE("GPL");