blob: 7fe41965c5d1879d88f8f7acf54e9404453166c5 [file] [log] [blame]
Daniel Mack10494dc2009-05-18 16:10:39 -07001/*
2 * Touch Screen driver for EETI's I2C connected touch screen panels
Daniel Mackfd8135b2018-07-04 15:46:55 +00003 * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
Daniel Mack10494dc2009-05-18 16:10:39 -07004 *
5 * See EETI's software guide for the protocol specification:
Daniel Mackfd8135b2018-07-04 15:46:55 +00006 * http://home.eeti.com.tw/documentation.html
Daniel Mack10494dc2009-05-18 16:10:39 -07007 *
8 * Based on migor_ts.c
9 * Copyright (c) 2008 Magnus Damm
10 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
11 *
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.
16 *
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.
21 *
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
25 */
26
27#include <linux/module.h>
Daniel Mack10494dc2009-05-18 16:10:39 -070028#include <linux/kernel.h>
29#include <linux/input.h>
Daniel Macka114cbd2018-07-04 15:46:22 +000030#include <linux/input/touchscreen.h>
Daniel Mack10494dc2009-05-18 16:10:39 -070031#include <linux/interrupt.h>
32#include <linux/i2c.h>
33#include <linux/timer.h>
Dmitry Torokhovd99caa42017-02-19 17:21:56 -080034#include <linux/gpio/consumer.h>
Daniel Macke32d7f12018-07-04 15:45:47 +000035#include <linux/of.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Dmitry Torokhove9f66cd2017-02-20 21:50:13 -080037#include <asm/unaligned.h>
Daniel Mack10494dc2009-05-18 16:10:39 -070038
Dmitry Torokhov720bebd2017-02-19 17:28:11 -080039struct eeti_ts {
Daniel Mack10494dc2009-05-18 16:10:39 -070040 struct i2c_client *client;
41 struct input_dev *input;
Dmitry Torokhovd99caa42017-02-19 17:21:56 -080042 struct gpio_desc *attn_gpio;
Daniel Macka114cbd2018-07-04 15:46:22 +000043 struct touchscreen_properties props;
Dmitry Torokhov03780082017-02-19 17:23:47 -080044 bool running;
Daniel Mack10494dc2009-05-18 16:10:39 -070045};
46
47#define EETI_TS_BITDEPTH (11)
48#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
49
Dmitry Torokhov272c03b2017-02-20 21:42:43 -080050#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)
Daniel Mack10494dc2009-05-18 16:10:39 -070054#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
55
Dmitry Torokhov03780082017-02-19 17:23:47 -080056static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
Daniel Mack10494dc2009-05-18 16:10:39 -070057{
Dmitry Torokhov03780082017-02-19 17:23:47 -080058 unsigned int res;
59 u16 x, y;
Daniel Mack10494dc2009-05-18 16:10:39 -070060
Daniel Mack10494dc2009-05-18 16:10:39 -070061 res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
Dmitry Torokhove9f66cd2017-02-20 21:50:13 -080062
63 x = get_unaligned_be16(&buf[1]);
64 y = get_unaligned_be16(&buf[3]);
Daniel Mack10494dc2009-05-18 16:10:39 -070065
66 /* fix the range to 11 bits */
67 x >>= res - EETI_TS_BITDEPTH;
68 y >>= res - EETI_TS_BITDEPTH;
69
Daniel Mack10494dc2009-05-18 16:10:39 -070070 if (buf[0] & REPORT_BIT_HAS_PRESSURE)
Dmitry Torokhov720bebd2017-02-19 17:28:11 -080071 input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
Daniel Mack10494dc2009-05-18 16:10:39 -070072
Daniel Macka114cbd2018-07-04 15:46:22 +000073 touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
Dmitry Torokhov03780082017-02-19 17:23:47 -080074 input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
Dmitry Torokhov720bebd2017-02-19 17:28:11 -080075 input_sync(eeti->input);
Daniel Mack10494dc2009-05-18 16:10:39 -070076}
77
78static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
79{
Dmitry Torokhov720bebd2017-02-19 17:28:11 -080080 struct eeti_ts *eeti = dev_id;
Dmitry Torokhov03780082017-02-19 17:23:47 -080081 int len;
82 int error;
83 char buf[6];
Daniel Mack10494dc2009-05-18 16:10:39 -070084
Dmitry Torokhov03780082017-02-19 17:23:47 -080085 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;
93 }
94
95 if (buf[0] & 0x80) {
96 /* Motion packet */
97 eeti_ts_report_event(eeti, buf);
98 }
Dmitry Torokhovd99caa42017-02-19 17:21:56 -080099 } while (eeti->running &&
100 eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
Daniel Mack10494dc2009-05-18 16:10:39 -0700101
102 return IRQ_HANDLED;
103}
104
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800105static void eeti_ts_start(struct eeti_ts *eeti)
Daniel Mack10494dc2009-05-18 16:10:39 -0700106{
Dmitry Torokhov03780082017-02-19 17:23:47 -0800107 eeti->running = true;
108 wmb();
Dmitry Torokhov2d388492017-02-19 17:14:33 -0800109 enable_irq(eeti->client->irq);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700110}
111
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800112static void eeti_ts_stop(struct eeti_ts *eeti)
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700113{
Dmitry Torokhov03780082017-02-19 17:23:47 -0800114 eeti->running = false;
115 wmb();
Dmitry Torokhov2d388492017-02-19 17:14:33 -0800116 disable_irq(eeti->client->irq);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700117}
118
119static int eeti_ts_open(struct input_dev *dev)
120{
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800121 struct eeti_ts *eeti = input_get_drvdata(dev);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700122
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800123 eeti_ts_start(eeti);
Daniel Mack10494dc2009-05-18 16:10:39 -0700124
125 return 0;
126}
127
128static void eeti_ts_close(struct input_dev *dev)
129{
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800130 struct eeti_ts *eeti = input_get_drvdata(dev);
Daniel Mack10494dc2009-05-18 16:10:39 -0700131
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800132 eeti_ts_stop(eeti);
Daniel Mack10494dc2009-05-18 16:10:39 -0700133}
134
Bill Pemberton5298cc42012-11-23 21:38:25 -0800135static int eeti_ts_probe(struct i2c_client *client,
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800136 const struct i2c_device_id *idp)
Daniel Mack10494dc2009-05-18 16:10:39 -0700137{
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800138 struct device *dev = &client->dev;
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800139 struct eeti_ts *eeti;
Daniel Mack10494dc2009-05-18 16:10:39 -0700140 struct input_dev *input;
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800141 int error;
Daniel Mack10494dc2009-05-18 16:10:39 -0700142
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700143 /*
144 * In contrast to what's described in the datasheet, there seems
Daniel Mack10494dc2009-05-18 16:10:39 -0700145 * 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
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700147 * for interrupts to occur.
148 */
Daniel Mack10494dc2009-05-18 16:10:39 -0700149
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800150 eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800151 if (!eeti) {
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800152 dev_err(dev, "failed to allocate driver data\n");
Guenter Roeckad03ae42017-01-18 11:24:06 -0800153 return -ENOMEM;
Daniel Mack10494dc2009-05-18 16:10:39 -0700154 }
155
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800156 input = devm_input_allocate_device(dev);
Daniel Mack10494dc2009-05-18 16:10:39 -0700157 if (!input) {
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800158 dev_err(dev, "Failed to allocate input device.\n");
159 return -ENOMEM;
Daniel Mack10494dc2009-05-18 16:10:39 -0700160 }
161
Dmitry Torokhov42e02a62017-02-20 21:53:51 -0800162 input_set_capability(input, EV_KEY, BTN_TOUCH);
Daniel Mack10494dc2009-05-18 16:10:39 -0700163
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);
167
Daniel Macka114cbd2018-07-04 15:46:22 +0000168 touchscreen_parse_properties(input, false, &eeti->props);
169
Daniel Mack10494dc2009-05-18 16:10:39 -0700170 input->name = client->name;
171 input->id.bustype = BUS_I2C;
Daniel Mack10494dc2009-05-18 16:10:39 -0700172 input->open = eeti_ts_open;
173 input->close = eeti_ts_close;
174
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800175 eeti->client = client;
176 eeti->input = input;
Daniel Mack10494dc2009-05-18 16:10:39 -0700177
Dmitry Torokhovd99caa42017-02-19 17:21:56 -0800178 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);
Daniel Mack25a70e32009-08-12 00:50:09 -0700181
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800182 i2c_set_clientdata(client, eeti);
183 input_set_drvdata(input, eeti);
Daniel Mack10494dc2009-05-18 16:10:39 -0700184
Dmitry Torokhov03780082017-02-19 17:23:47 -0800185 error = devm_request_threaded_irq(dev, client->irq,
186 NULL, eeti_ts_isr,
Dmitry Torokhovd422be52017-02-19 16:22:13 -0800187 IRQF_ONESHOT,
Dmitry Torokhov03780082017-02-19 17:23:47 -0800188 client->name, eeti);
Dmitry Torokhov6f9fab62017-02-17 15:54:22 -0800189 if (error) {
190 dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
191 error);
192 return error;
Daniel Mack10494dc2009-05-18 16:10:39 -0700193 }
194
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700195 /*
196 * Disable the device for now. It will be enabled once the
197 * input device is opened.
198 */
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800199 eeti_ts_stop(eeti);
Daniel Mack10494dc2009-05-18 16:10:39 -0700200
Dmitry Torokhov03780082017-02-19 17:23:47 -0800201 error = input_register_device(input);
202 if (error)
203 return error;
204
Daniel Mack10494dc2009-05-18 16:10:39 -0700205 return 0;
Daniel Mack10494dc2009-05-18 16:10:39 -0700206}
207
Jingoo Han02b6a582014-11-02 00:04:14 -0700208static int __maybe_unused eeti_ts_suspend(struct device *dev)
Daniel Mack10494dc2009-05-18 16:10:39 -0700209{
Mark Brown85012ff2011-01-06 23:01:02 -0800210 struct i2c_client *client = to_i2c_client(dev);
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800211 struct eeti_ts *eeti = i2c_get_clientdata(client);
212 struct input_dev *input_dev = eeti->input;
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700213
214 mutex_lock(&input_dev->mutex);
215
216 if (input_dev->users)
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800217 eeti_ts_stop(eeti);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700218
219 mutex_unlock(&input_dev->mutex);
Daniel Mack10494dc2009-05-18 16:10:39 -0700220
221 if (device_may_wakeup(&client->dev))
Dmitry Torokhov2d388492017-02-19 17:14:33 -0800222 enable_irq_wake(client->irq);
Daniel Mack10494dc2009-05-18 16:10:39 -0700223
224 return 0;
225}
226
Jingoo Han02b6a582014-11-02 00:04:14 -0700227static int __maybe_unused eeti_ts_resume(struct device *dev)
Daniel Mack10494dc2009-05-18 16:10:39 -0700228{
Mark Brown85012ff2011-01-06 23:01:02 -0800229 struct i2c_client *client = to_i2c_client(dev);
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800230 struct eeti_ts *eeti = i2c_get_clientdata(client);
231 struct input_dev *input_dev = eeti->input;
Daniel Mack10494dc2009-05-18 16:10:39 -0700232
233 if (device_may_wakeup(&client->dev))
Dmitry Torokhov2d388492017-02-19 17:14:33 -0800234 disable_irq_wake(client->irq);
Daniel Mack10494dc2009-05-18 16:10:39 -0700235
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700236 mutex_lock(&input_dev->mutex);
237
238 if (input_dev->users)
Dmitry Torokhov720bebd2017-02-19 17:28:11 -0800239 eeti_ts_start(eeti);
Daniel Mack7fbef0d2010-04-19 00:42:16 -0700240
241 mutex_unlock(&input_dev->mutex);
242
Daniel Mack10494dc2009-05-18 16:10:39 -0700243 return 0;
244}
Mark Brown85012ff2011-01-06 23:01:02 -0800245
246static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
Daniel Mack10494dc2009-05-18 16:10:39 -0700247
248static const struct i2c_device_id eeti_ts_id[] = {
249 { "eeti_ts", 0 },
250 { }
251};
252MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
253
Daniel Macke32d7f12018-07-04 15:45:47 +0000254#ifdef CONFIG_OF
255static const struct of_device_id of_eeti_ts_match[] = {
256 { .compatible = "eeti,exc3000-i2c", },
257 { }
258};
259#endif
260
Daniel Mack10494dc2009-05-18 16:10:39 -0700261static struct i2c_driver eeti_ts_driver = {
262 .driver = {
263 .name = "eeti_ts",
Mark Brown85012ff2011-01-06 23:01:02 -0800264 .pm = &eeti_ts_pm,
Daniel Macke32d7f12018-07-04 15:45:47 +0000265 .of_match_table = of_match_ptr(of_eeti_ts_match),
Daniel Mack10494dc2009-05-18 16:10:39 -0700266 },
267 .probe = eeti_ts_probe,
Daniel Mack10494dc2009-05-18 16:10:39 -0700268 .id_table = eeti_ts_id,
269};
270
Axel Lin1b92c1c2012-03-16 23:05:41 -0700271module_i2c_driver(eeti_ts_driver);
Daniel Mack10494dc2009-05-18 16:10:39 -0700272
273MODULE_DESCRIPTION("EETI Touchscreen driver");
Daniel Mackfd8135b2018-07-04 15:46:55 +0000274MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
Daniel Mack10494dc2009-05-18 16:10:39 -0700275MODULE_LICENSE("GPL");