Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / touchscreen / eeti_ts.c
blob7fe41965c5d1879d88f8f7acf54e9404453166c5
1 /*
2 * Touch Screen driver for EETI's I2C connected touch screen panels
3 * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
5 * See EETI's software guide for the protocol specification:
6 * http://home.eeti.com.tw/documentation.html
8 * Based on migor_ts.c
9 * Copyright (c) 2008 Magnus Damm
10 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
12 * This file is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This file is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/input.h>
30 #include <linux/input/touchscreen.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2c.h>
33 #include <linux/timer.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/of.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
39 struct eeti_ts {
40 struct i2c_client *client;
41 struct input_dev *input;
42 struct gpio_desc *attn_gpio;
43 struct touchscreen_properties props;
44 bool running;
47 #define EETI_TS_BITDEPTH (11)
48 #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
50 #define REPORT_BIT_PRESSED BIT(0)
51 #define REPORT_BIT_AD0 BIT(1)
52 #define REPORT_BIT_AD1 BIT(2)
53 #define REPORT_BIT_HAS_PRESSURE BIT(6)
54 #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
56 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
58 unsigned int res;
59 u16 x, y;
61 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
63 x = get_unaligned_be16(&buf[1]);
64 y = get_unaligned_be16(&buf[3]);
66 /* fix the range to 11 bits */
67 x >>= res - EETI_TS_BITDEPTH;
68 y >>= res - EETI_TS_BITDEPTH;
70 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
71 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
73 touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
74 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
75 input_sync(eeti->input);
78 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
80 struct eeti_ts *eeti = dev_id;
81 int len;
82 int error;
83 char buf[6];
85 do {
86 len = i2c_master_recv(eeti->client, buf, sizeof(buf));
87 if (len != sizeof(buf)) {
88 error = len < 0 ? len : -EIO;
89 dev_err(&eeti->client->dev,
90 "failed to read touchscreen data: %d\n",
91 error);
92 break;
95 if (buf[0] & 0x80) {
96 /* Motion packet */
97 eeti_ts_report_event(eeti, buf);
99 } while (eeti->running &&
100 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
102 return IRQ_HANDLED;
105 static void eeti_ts_start(struct eeti_ts *eeti)
107 eeti->running = true;
108 wmb();
109 enable_irq(eeti->client->irq);
112 static void eeti_ts_stop(struct eeti_ts *eeti)
114 eeti->running = false;
115 wmb();
116 disable_irq(eeti->client->irq);
119 static int eeti_ts_open(struct input_dev *dev)
121 struct eeti_ts *eeti = input_get_drvdata(dev);
123 eeti_ts_start(eeti);
125 return 0;
128 static void eeti_ts_close(struct input_dev *dev)
130 struct eeti_ts *eeti = input_get_drvdata(dev);
132 eeti_ts_stop(eeti);
135 static int eeti_ts_probe(struct i2c_client *client,
136 const struct i2c_device_id *idp)
138 struct device *dev = &client->dev;
139 struct eeti_ts *eeti;
140 struct input_dev *input;
141 int error;
144 * In contrast to what's described in the datasheet, there seems
145 * to be no way of probing the presence of that device using I2C
146 * commands. So we need to blindly believe it is there, and wait
147 * for interrupts to occur.
150 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
151 if (!eeti) {
152 dev_err(dev, "failed to allocate driver data\n");
153 return -ENOMEM;
156 input = devm_input_allocate_device(dev);
157 if (!input) {
158 dev_err(dev, "Failed to allocate input device.\n");
159 return -ENOMEM;
162 input_set_capability(input, EV_KEY, BTN_TOUCH);
164 input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
165 input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
166 input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
168 touchscreen_parse_properties(input, false, &eeti->props);
170 input->name = client->name;
171 input->id.bustype = BUS_I2C;
172 input->open = eeti_ts_open;
173 input->close = eeti_ts_close;
175 eeti->client = client;
176 eeti->input = input;
178 eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
179 if (IS_ERR(eeti->attn_gpio))
180 return PTR_ERR(eeti->attn_gpio);
182 i2c_set_clientdata(client, eeti);
183 input_set_drvdata(input, eeti);
185 error = devm_request_threaded_irq(dev, client->irq,
186 NULL, eeti_ts_isr,
187 IRQF_ONESHOT,
188 client->name, eeti);
189 if (error) {
190 dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
191 error);
192 return error;
196 * Disable the device for now. It will be enabled once the
197 * input device is opened.
199 eeti_ts_stop(eeti);
201 error = input_register_device(input);
202 if (error)
203 return error;
205 return 0;
208 static int __maybe_unused eeti_ts_suspend(struct device *dev)
210 struct i2c_client *client = to_i2c_client(dev);
211 struct eeti_ts *eeti = i2c_get_clientdata(client);
212 struct input_dev *input_dev = eeti->input;
214 mutex_lock(&input_dev->mutex);
216 if (input_dev->users)
217 eeti_ts_stop(eeti);
219 mutex_unlock(&input_dev->mutex);
221 if (device_may_wakeup(&client->dev))
222 enable_irq_wake(client->irq);
224 return 0;
227 static int __maybe_unused eeti_ts_resume(struct device *dev)
229 struct i2c_client *client = to_i2c_client(dev);
230 struct eeti_ts *eeti = i2c_get_clientdata(client);
231 struct input_dev *input_dev = eeti->input;
233 if (device_may_wakeup(&client->dev))
234 disable_irq_wake(client->irq);
236 mutex_lock(&input_dev->mutex);
238 if (input_dev->users)
239 eeti_ts_start(eeti);
241 mutex_unlock(&input_dev->mutex);
243 return 0;
246 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
248 static const struct i2c_device_id eeti_ts_id[] = {
249 { "eeti_ts", 0 },
252 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
254 #ifdef CONFIG_OF
255 static const struct of_device_id of_eeti_ts_match[] = {
256 { .compatible = "eeti,exc3000-i2c", },
259 #endif
261 static struct i2c_driver eeti_ts_driver = {
262 .driver = {
263 .name = "eeti_ts",
264 .pm = &eeti_ts_pm,
265 .of_match_table = of_match_ptr(of_eeti_ts_match),
267 .probe = eeti_ts_probe,
268 .id_table = eeti_ts_id,
271 module_i2c_driver(eeti_ts_driver);
273 MODULE_DESCRIPTION("EETI Touchscreen driver");
274 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
275 MODULE_LICENSE("GPL");