xref: /openbmc/linux/drivers/input/touchscreen/ili210x.c (revision fbd1ec000213c8b457dd4fb15b6de9ba02ec5482)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
271f8e38aSDmitry Torokhov #include <linux/delay.h>
371f8e38aSDmitry Torokhov #include <linux/gpio/consumer.h>
45c6a7a62SOlivier Sobrie #include <linux/i2c.h>
55c6a7a62SOlivier Sobrie #include <linux/input.h>
65c6a7a62SOlivier Sobrie #include <linux/input/mt.h>
7f67cc3e9SMarek Vasut #include <linux/input/touchscreen.h>
871f8e38aSDmitry Torokhov #include <linux/interrupt.h>
971f8e38aSDmitry Torokhov #include <linux/module.h>
1049588917SMarek Vasut #include <linux/of_device.h>
11b32fbeaeSSven Van Asbroeck #include <linux/sizes.h>
1271f8e38aSDmitry Torokhov #include <linux/slab.h>
13e3559442SMarek Vasut #include <asm/unaligned.h>
145c6a7a62SOlivier Sobrie 
1571f8e38aSDmitry Torokhov #define ILI2XXX_POLL_PERIOD	20
165c6a7a62SOlivier Sobrie 
17ef536abdSDmitry Torokhov #define ILI210X_DATA_SIZE	64
18ef536abdSDmitry Torokhov #define ILI211X_DATA_SIZE	43
19ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE1	31
20ef536abdSDmitry Torokhov #define ILI251X_DATA_SIZE2	20
21ef536abdSDmitry Torokhov 
225c6a7a62SOlivier Sobrie /* Touchscreen commands */
235c6a7a62SOlivier Sobrie #define REG_TOUCHDATA		0x10
245c6a7a62SOlivier Sobrie #define REG_PANEL_INFO		0x20
255c6a7a62SOlivier Sobrie #define REG_CALIBRATE		0xcc
265c6a7a62SOlivier Sobrie 
27ef536abdSDmitry Torokhov struct ili2xxx_chip {
28ef536abdSDmitry Torokhov 	int (*read_reg)(struct i2c_client *client, u8 reg,
29ef536abdSDmitry Torokhov 			void *buf, size_t len);
30ef536abdSDmitry Torokhov 	int (*get_touch_data)(struct i2c_client *client, u8 *data);
31ef536abdSDmitry Torokhov 	bool (*parse_touch_data)(const u8 *data, unsigned int finger,
32ef536abdSDmitry Torokhov 				 unsigned int *x, unsigned int *y);
33ef536abdSDmitry Torokhov 	bool (*continue_polling)(const u8 *data, bool touch);
34ef536abdSDmitry Torokhov 	unsigned int max_touches;
35b32fbeaeSSven Van Asbroeck 	unsigned int resolution;
36cc12ba18SSven Van Asbroeck 	bool has_calibrate_reg;
3749588917SMarek Vasut };
3849588917SMarek Vasut 
395c6a7a62SOlivier Sobrie struct ili210x {
405c6a7a62SOlivier Sobrie 	struct i2c_client *client;
415c6a7a62SOlivier Sobrie 	struct input_dev *input;
42201f3c80SMarek Vasut 	struct gpio_desc *reset_gpio;
43f67cc3e9SMarek Vasut 	struct touchscreen_properties prop;
44ef536abdSDmitry Torokhov 	const struct ili2xxx_chip *chip;
4571f8e38aSDmitry Torokhov 	bool stop;
465c6a7a62SOlivier Sobrie };
475c6a7a62SOlivier Sobrie 
48ef536abdSDmitry Torokhov static int ili210x_read_reg(struct i2c_client *client,
49ef536abdSDmitry Torokhov 			    u8 reg, void *buf, size_t len)
505c6a7a62SOlivier Sobrie {
51ef536abdSDmitry Torokhov 	struct i2c_msg msg[] = {
525c6a7a62SOlivier Sobrie 		{
535c6a7a62SOlivier Sobrie 			.addr	= client->addr,
545c6a7a62SOlivier Sobrie 			.flags	= 0,
555c6a7a62SOlivier Sobrie 			.len	= 1,
565c6a7a62SOlivier Sobrie 			.buf	= &reg,
575c6a7a62SOlivier Sobrie 		},
585c6a7a62SOlivier Sobrie 		{
595c6a7a62SOlivier Sobrie 			.addr	= client->addr,
605c6a7a62SOlivier Sobrie 			.flags	= I2C_M_RD,
615c6a7a62SOlivier Sobrie 			.len	= len,
625c6a7a62SOlivier Sobrie 			.buf	= buf,
635c6a7a62SOlivier Sobrie 		}
645c6a7a62SOlivier Sobrie 	};
65ef536abdSDmitry Torokhov 	int error, ret;
665c6a7a62SOlivier Sobrie 
67ef536abdSDmitry Torokhov 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
68ef536abdSDmitry Torokhov 	if (ret != ARRAY_SIZE(msg)) {
69ef536abdSDmitry Torokhov 		error = ret < 0 ? ret : -EIO;
70ef536abdSDmitry Torokhov 		dev_err(&client->dev, "%s failed: %d\n", __func__, error);
71ef536abdSDmitry Torokhov 		return error;
7249588917SMarek Vasut 	}
7349588917SMarek Vasut 
7449588917SMarek Vasut 	return 0;
7549588917SMarek Vasut }
7649588917SMarek Vasut 
77ef536abdSDmitry Torokhov static int ili210x_read_touch_data(struct i2c_client *client, u8 *data)
7849588917SMarek Vasut {
79ef536abdSDmitry Torokhov 	return ili210x_read_reg(client, REG_TOUCHDATA,
80ef536abdSDmitry Torokhov 				data, ILI210X_DATA_SIZE);
8149588917SMarek Vasut }
825c6a7a62SOlivier Sobrie 
83ef536abdSDmitry Torokhov static bool ili210x_touchdata_to_coords(const u8 *touchdata,
84e3559442SMarek Vasut 					unsigned int finger,
85e3559442SMarek Vasut 					unsigned int *x, unsigned int *y)
865c6a7a62SOlivier Sobrie {
87e3559442SMarek Vasut 	if (touchdata[0] & BIT(finger))
88e3559442SMarek Vasut 		return false;
89e3559442SMarek Vasut 
90e3559442SMarek Vasut 	*x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0);
91e3559442SMarek Vasut 	*y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2);
92e3559442SMarek Vasut 
93e3559442SMarek Vasut 	return true;
94e3559442SMarek Vasut }
95e3559442SMarek Vasut 
96ef536abdSDmitry Torokhov static bool ili210x_check_continue_polling(const u8 *data, bool touch)
97ef536abdSDmitry Torokhov {
98ef536abdSDmitry Torokhov 	return data[0] & 0xf3;
99ef536abdSDmitry Torokhov }
100ef536abdSDmitry Torokhov 
101ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili210x_chip = {
102ef536abdSDmitry Torokhov 	.read_reg		= ili210x_read_reg,
103ef536abdSDmitry Torokhov 	.get_touch_data		= ili210x_read_touch_data,
104ef536abdSDmitry Torokhov 	.parse_touch_data	= ili210x_touchdata_to_coords,
105ef536abdSDmitry Torokhov 	.continue_polling	= ili210x_check_continue_polling,
106ef536abdSDmitry Torokhov 	.max_touches		= 2,
107cc12ba18SSven Van Asbroeck 	.has_calibrate_reg	= true,
108ef536abdSDmitry Torokhov };
109ef536abdSDmitry Torokhov 
110ef536abdSDmitry Torokhov static int ili211x_read_touch_data(struct i2c_client *client, u8 *data)
111ef536abdSDmitry Torokhov {
112ef536abdSDmitry Torokhov 	s16 sum = 0;
113ef536abdSDmitry Torokhov 	int error;
114ef536abdSDmitry Torokhov 	int ret;
115ef536abdSDmitry Torokhov 	int i;
116ef536abdSDmitry Torokhov 
117ef536abdSDmitry Torokhov 	ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE);
118ef536abdSDmitry Torokhov 	if (ret != ILI211X_DATA_SIZE) {
119ef536abdSDmitry Torokhov 		error = ret < 0 ? ret : -EIO;
120ef536abdSDmitry Torokhov 		dev_err(&client->dev, "%s failed: %d\n", __func__, error);
121ef536abdSDmitry Torokhov 		return error;
122ef536abdSDmitry Torokhov 	}
123ef536abdSDmitry Torokhov 
124ef536abdSDmitry Torokhov 	/* This chip uses custom checksum at the end of data */
125ef536abdSDmitry Torokhov 	for (i = 0; i < ILI211X_DATA_SIZE - 1; i++)
126ef536abdSDmitry Torokhov 		sum = (sum + data[i]) & 0xff;
127ef536abdSDmitry Torokhov 
128ef536abdSDmitry Torokhov 	if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) {
129ef536abdSDmitry Torokhov 		dev_err(&client->dev,
130ef536abdSDmitry Torokhov 			"CRC error (crc=0x%02x expected=0x%02x)\n",
131ef536abdSDmitry Torokhov 			sum, data[ILI211X_DATA_SIZE - 1]);
132ef536abdSDmitry Torokhov 		return -EIO;
133ef536abdSDmitry Torokhov 	}
134ef536abdSDmitry Torokhov 
135ef536abdSDmitry Torokhov 	return 0;
136ef536abdSDmitry Torokhov }
137ef536abdSDmitry Torokhov 
138ef536abdSDmitry Torokhov static bool ili211x_touchdata_to_coords(const u8 *touchdata,
139eb91ecc9SMarek Vasut 					unsigned int finger,
140eb91ecc9SMarek Vasut 					unsigned int *x, unsigned int *y)
141eb91ecc9SMarek Vasut {
142eb91ecc9SMarek Vasut 	u32 data;
143eb91ecc9SMarek Vasut 
144eb91ecc9SMarek Vasut 	data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0);
145eb91ecc9SMarek Vasut 	if (data == 0xffffffff)	/* Finger up */
146eb91ecc9SMarek Vasut 		return false;
147eb91ecc9SMarek Vasut 
148eb91ecc9SMarek Vasut 	*x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) |
149eb91ecc9SMarek Vasut 	     touchdata[1 + (finger * 4) + 1];
150eb91ecc9SMarek Vasut 	*y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) |
151eb91ecc9SMarek Vasut 	     touchdata[1 + (finger * 4) + 2];
152eb91ecc9SMarek Vasut 
153eb91ecc9SMarek Vasut 	return true;
154eb91ecc9SMarek Vasut }
155eb91ecc9SMarek Vasut 
156ef536abdSDmitry Torokhov static bool ili211x_decline_polling(const u8 *data, bool touch)
157ef536abdSDmitry Torokhov {
158ef536abdSDmitry Torokhov 	return false;
159ef536abdSDmitry Torokhov }
160ef536abdSDmitry Torokhov 
161ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili211x_chip = {
162ef536abdSDmitry Torokhov 	.read_reg		= ili210x_read_reg,
163ef536abdSDmitry Torokhov 	.get_touch_data		= ili211x_read_touch_data,
164ef536abdSDmitry Torokhov 	.parse_touch_data	= ili211x_touchdata_to_coords,
165ef536abdSDmitry Torokhov 	.continue_polling	= ili211x_decline_polling,
166ef536abdSDmitry Torokhov 	.max_touches		= 10,
167b32fbeaeSSven Van Asbroeck 	.resolution		= 2048,
168ef536abdSDmitry Torokhov };
169ef536abdSDmitry Torokhov 
170ef536abdSDmitry Torokhov static int ili251x_read_reg(struct i2c_client *client,
171ef536abdSDmitry Torokhov 			    u8 reg, void *buf, size_t len)
172ef536abdSDmitry Torokhov {
173ef536abdSDmitry Torokhov 	int error;
174ef536abdSDmitry Torokhov 	int ret;
175ef536abdSDmitry Torokhov 
176ef536abdSDmitry Torokhov 	ret = i2c_master_send(client, &reg, 1);
177ef536abdSDmitry Torokhov 	if (ret == 1) {
178ef536abdSDmitry Torokhov 		usleep_range(5000, 5500);
179ef536abdSDmitry Torokhov 
180ef536abdSDmitry Torokhov 		ret = i2c_master_recv(client, buf, len);
181ef536abdSDmitry Torokhov 		if (ret == len)
182ef536abdSDmitry Torokhov 			return 0;
183ef536abdSDmitry Torokhov 	}
184ef536abdSDmitry Torokhov 
185ef536abdSDmitry Torokhov 	error = ret < 0 ? ret : -EIO;
186ef536abdSDmitry Torokhov 	dev_err(&client->dev, "%s failed: %d\n", __func__, error);
187ef536abdSDmitry Torokhov 	return ret;
188ef536abdSDmitry Torokhov }
189ef536abdSDmitry Torokhov 
190ef536abdSDmitry Torokhov static int ili251x_read_touch_data(struct i2c_client *client, u8 *data)
191ef536abdSDmitry Torokhov {
192ef536abdSDmitry Torokhov 	int error;
193ef536abdSDmitry Torokhov 
194ef536abdSDmitry Torokhov 	error = ili251x_read_reg(client, REG_TOUCHDATA,
195ef536abdSDmitry Torokhov 				 data, ILI251X_DATA_SIZE1);
196ef536abdSDmitry Torokhov 	if (!error && data[0] == 2) {
197ef536abdSDmitry Torokhov 		error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1,
198ef536abdSDmitry Torokhov 					ILI251X_DATA_SIZE2);
199ef536abdSDmitry Torokhov 		if (error >= 0 && error != ILI251X_DATA_SIZE2)
200ef536abdSDmitry Torokhov 			error = -EIO;
201ef536abdSDmitry Torokhov 	}
202ef536abdSDmitry Torokhov 
203ef536abdSDmitry Torokhov 	return error;
204ef536abdSDmitry Torokhov }
205ef536abdSDmitry Torokhov 
206ef536abdSDmitry Torokhov static bool ili251x_touchdata_to_coords(const u8 *touchdata,
20749588917SMarek Vasut 					unsigned int finger,
20849588917SMarek Vasut 					unsigned int *x, unsigned int *y)
20949588917SMarek Vasut {
210ef536abdSDmitry Torokhov 	u16 val;
211ef536abdSDmitry Torokhov 
212ef536abdSDmitry Torokhov 	val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0);
213ef536abdSDmitry Torokhov 	if (!(val & BIT(15)))	/* Touch indication */
21449588917SMarek Vasut 		return false;
21549588917SMarek Vasut 
216ef536abdSDmitry Torokhov 	*x = val & 0x3fff;
21749588917SMarek Vasut 	*y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2);
21849588917SMarek Vasut 
21949588917SMarek Vasut 	return true;
22049588917SMarek Vasut }
22149588917SMarek Vasut 
222ef536abdSDmitry Torokhov static bool ili251x_check_continue_polling(const u8 *data, bool touch)
223ef536abdSDmitry Torokhov {
224ef536abdSDmitry Torokhov 	return touch;
225ef536abdSDmitry Torokhov }
226ef536abdSDmitry Torokhov 
227ef536abdSDmitry Torokhov static const struct ili2xxx_chip ili251x_chip = {
228ef536abdSDmitry Torokhov 	.read_reg		= ili251x_read_reg,
229ef536abdSDmitry Torokhov 	.get_touch_data		= ili251x_read_touch_data,
230ef536abdSDmitry Torokhov 	.parse_touch_data	= ili251x_touchdata_to_coords,
231ef536abdSDmitry Torokhov 	.continue_polling	= ili251x_check_continue_polling,
232ef536abdSDmitry Torokhov 	.max_touches		= 10,
233cc12ba18SSven Van Asbroeck 	.has_calibrate_reg	= true,
234ef536abdSDmitry Torokhov };
235ef536abdSDmitry Torokhov 
236e3559442SMarek Vasut static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata)
237e3559442SMarek Vasut {
238e3559442SMarek Vasut 	struct input_dev *input = priv->input;
2395c6a7a62SOlivier Sobrie 	int i;
240ef536abdSDmitry Torokhov 	bool contact = false, touch;
24149588917SMarek Vasut 	unsigned int x = 0, y = 0;
2425c6a7a62SOlivier Sobrie 
243ef536abdSDmitry Torokhov 	for (i = 0; i < priv->chip->max_touches; i++) {
244ef536abdSDmitry Torokhov 		touch = priv->chip->parse_touch_data(touchdata, i, &x, &y);
24549588917SMarek Vasut 
246f67cc3e9SMarek Vasut 		input_mt_slot(input, i);
247ef536abdSDmitry Torokhov 		if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) {
248ef536abdSDmitry Torokhov 			touchscreen_report_pos(input, &priv->prop, x, y, true);
249ef536abdSDmitry Torokhov 			contact = true;
250ef536abdSDmitry Torokhov 		}
2515c6a7a62SOlivier Sobrie 	}
2525c6a7a62SOlivier Sobrie 
2535c6a7a62SOlivier Sobrie 	input_mt_report_pointer_emulation(input, false);
2545c6a7a62SOlivier Sobrie 	input_sync(input);
255e3559442SMarek Vasut 
25649588917SMarek Vasut 	return contact;
2575c6a7a62SOlivier Sobrie }
2585c6a7a62SOlivier Sobrie 
25971f8e38aSDmitry Torokhov static irqreturn_t ili210x_irq(int irq, void *irq_data)
2605c6a7a62SOlivier Sobrie {
26171f8e38aSDmitry Torokhov 	struct ili210x *priv = irq_data;
2625c6a7a62SOlivier Sobrie 	struct i2c_client *client = priv->client;
263ef536abdSDmitry Torokhov 	const struct ili2xxx_chip *chip = priv->chip;
264ef536abdSDmitry Torokhov 	u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
265ef536abdSDmitry Torokhov 	bool keep_polling;
266e3559442SMarek Vasut 	bool touch;
26771f8e38aSDmitry Torokhov 	int error;
2685c6a7a62SOlivier Sobrie 
26971f8e38aSDmitry Torokhov 	do {
270ef536abdSDmitry Torokhov 		error = chip->get_touch_data(client, touchdata);
2715c6a7a62SOlivier Sobrie 		if (error) {
2725c6a7a62SOlivier Sobrie 			dev_err(&client->dev,
273ef536abdSDmitry Torokhov 				"Unable to get touch data: %d\n", error);
27471f8e38aSDmitry Torokhov 			break;
2755c6a7a62SOlivier Sobrie 		}
2765c6a7a62SOlivier Sobrie 
277e3559442SMarek Vasut 		touch = ili210x_report_events(priv, touchdata);
278ef536abdSDmitry Torokhov 		keep_polling = chip->continue_polling(touchdata, touch);
279ef536abdSDmitry Torokhov 		if (keep_polling)
28071f8e38aSDmitry Torokhov 			msleep(ILI2XXX_POLL_PERIOD);
281ef536abdSDmitry Torokhov 	} while (!priv->stop && keep_polling);
2825c6a7a62SOlivier Sobrie 
2835c6a7a62SOlivier Sobrie 	return IRQ_HANDLED;
2845c6a7a62SOlivier Sobrie }
2855c6a7a62SOlivier Sobrie 
2865c6a7a62SOlivier Sobrie static ssize_t ili210x_calibrate(struct device *dev,
2875c6a7a62SOlivier Sobrie 				 struct device_attribute *attr,
2885c6a7a62SOlivier Sobrie 				 const char *buf, size_t count)
2895c6a7a62SOlivier Sobrie {
2905c6a7a62SOlivier Sobrie 	struct i2c_client *client = to_i2c_client(dev);
2915c6a7a62SOlivier Sobrie 	struct ili210x *priv = i2c_get_clientdata(client);
2925c6a7a62SOlivier Sobrie 	unsigned long calibrate;
2935c6a7a62SOlivier Sobrie 	int rc;
2945c6a7a62SOlivier Sobrie 	u8 cmd = REG_CALIBRATE;
2955c6a7a62SOlivier Sobrie 
2965c6a7a62SOlivier Sobrie 	if (kstrtoul(buf, 10, &calibrate))
2975c6a7a62SOlivier Sobrie 		return -EINVAL;
2985c6a7a62SOlivier Sobrie 
2995c6a7a62SOlivier Sobrie 	if (calibrate > 1)
3005c6a7a62SOlivier Sobrie 		return -EINVAL;
3015c6a7a62SOlivier Sobrie 
3025c6a7a62SOlivier Sobrie 	if (calibrate) {
3035c6a7a62SOlivier Sobrie 		rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
3045c6a7a62SOlivier Sobrie 		if (rc != sizeof(cmd))
3055c6a7a62SOlivier Sobrie 			return -EIO;
3065c6a7a62SOlivier Sobrie 	}
3075c6a7a62SOlivier Sobrie 
3085c6a7a62SOlivier Sobrie 	return count;
3095c6a7a62SOlivier Sobrie }
310b27c0d0cSDmitry Torokhov static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
3115c6a7a62SOlivier Sobrie 
3125c6a7a62SOlivier Sobrie static struct attribute *ili210x_attributes[] = {
3135c6a7a62SOlivier Sobrie 	&dev_attr_calibrate.attr,
3145c6a7a62SOlivier Sobrie 	NULL,
3155c6a7a62SOlivier Sobrie };
3165c6a7a62SOlivier Sobrie 
317cc12ba18SSven Van Asbroeck static umode_t ili210x_calibrate_visible(struct kobject *kobj,
318cc12ba18SSven Van Asbroeck 					  struct attribute *attr, int index)
319cc12ba18SSven Van Asbroeck {
320cc12ba18SSven Van Asbroeck 	struct device *dev = kobj_to_dev(kobj);
321cc12ba18SSven Van Asbroeck 	struct i2c_client *client = to_i2c_client(dev);
322cc12ba18SSven Van Asbroeck 	struct ili210x *priv = i2c_get_clientdata(client);
323cc12ba18SSven Van Asbroeck 
324*fbd1ec00SLuca Weiss 	return priv->chip->has_calibrate_reg ? attr->mode : 0;
325cc12ba18SSven Van Asbroeck }
326cc12ba18SSven Van Asbroeck 
3275c6a7a62SOlivier Sobrie static const struct attribute_group ili210x_attr_group = {
3285c6a7a62SOlivier Sobrie 	.attrs = ili210x_attributes,
329cc12ba18SSven Van Asbroeck 	.is_visible = ili210x_calibrate_visible,
3305c6a7a62SOlivier Sobrie };
3315c6a7a62SOlivier Sobrie 
332201f3c80SMarek Vasut static void ili210x_power_down(void *data)
333201f3c80SMarek Vasut {
334201f3c80SMarek Vasut 	struct gpio_desc *reset_gpio = data;
335201f3c80SMarek Vasut 
336201f3c80SMarek Vasut 	gpiod_set_value_cansleep(reset_gpio, 1);
337201f3c80SMarek Vasut }
338201f3c80SMarek Vasut 
33971f8e38aSDmitry Torokhov static void ili210x_stop(void *data)
3401bdec5d9SMarek Vasut {
3411bdec5d9SMarek Vasut 	struct ili210x *priv = data;
3421bdec5d9SMarek Vasut 
34371f8e38aSDmitry Torokhov 	/* Tell ISR to quit even if there is a contact. */
34471f8e38aSDmitry Torokhov 	priv->stop = true;
3451bdec5d9SMarek Vasut }
3461bdec5d9SMarek Vasut 
3475298cc4cSBill Pemberton static int ili210x_i2c_probe(struct i2c_client *client,
3485c6a7a62SOlivier Sobrie 			     const struct i2c_device_id *id)
3495c6a7a62SOlivier Sobrie {
3505c6a7a62SOlivier Sobrie 	struct device *dev = &client->dev;
351ef536abdSDmitry Torokhov 	const struct ili2xxx_chip *chip;
3525c6a7a62SOlivier Sobrie 	struct ili210x *priv;
353201f3c80SMarek Vasut 	struct gpio_desc *reset_gpio;
3545c6a7a62SOlivier Sobrie 	struct input_dev *input;
3555c6a7a62SOlivier Sobrie 	int error;
356b32fbeaeSSven Van Asbroeck 	unsigned int max_xy;
3575c6a7a62SOlivier Sobrie 
3585c6a7a62SOlivier Sobrie 	dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
3595c6a7a62SOlivier Sobrie 
360ef536abdSDmitry Torokhov 	chip = device_get_match_data(dev);
361ef536abdSDmitry Torokhov 	if (!chip && id)
362ef536abdSDmitry Torokhov 		chip = (const struct ili2xxx_chip *)id->driver_data;
363ef536abdSDmitry Torokhov 	if (!chip) {
364ef536abdSDmitry Torokhov 		dev_err(&client->dev, "unknown device model\n");
365ef536abdSDmitry Torokhov 		return -ENODEV;
366ef536abdSDmitry Torokhov 	}
367ef536abdSDmitry Torokhov 
3685c6a7a62SOlivier Sobrie 	if (client->irq <= 0) {
3695c6a7a62SOlivier Sobrie 		dev_err(dev, "No IRQ!\n");
3705c6a7a62SOlivier Sobrie 		return -EINVAL;
3715c6a7a62SOlivier Sobrie 	}
3725c6a7a62SOlivier Sobrie 
373201f3c80SMarek Vasut 	reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
374201f3c80SMarek Vasut 	if (IS_ERR(reset_gpio))
375201f3c80SMarek Vasut 		return PTR_ERR(reset_gpio);
376201f3c80SMarek Vasut 
377201f3c80SMarek Vasut 	if (reset_gpio) {
378201f3c80SMarek Vasut 		error = devm_add_action_or_reset(dev, ili210x_power_down,
379201f3c80SMarek Vasut 						 reset_gpio);
380201f3c80SMarek Vasut 		if (error)
381201f3c80SMarek Vasut 			return error;
382201f3c80SMarek Vasut 
383201f3c80SMarek Vasut 		usleep_range(50, 100);
384201f3c80SMarek Vasut 		gpiod_set_value_cansleep(reset_gpio, 0);
385201f3c80SMarek Vasut 		msleep(100);
386201f3c80SMarek Vasut 	}
387201f3c80SMarek Vasut 
38812294577SMarek Vasut 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
38912294577SMarek Vasut 	if (!priv)
39012294577SMarek Vasut 		return -ENOMEM;
39112294577SMarek Vasut 
39212294577SMarek Vasut 	input = devm_input_allocate_device(dev);
39312294577SMarek Vasut 	if (!input)
39412294577SMarek Vasut 		return -ENOMEM;
39512294577SMarek Vasut 
39612294577SMarek Vasut 	priv->client = client;
39712294577SMarek Vasut 	priv->input = input;
39812294577SMarek Vasut 	priv->reset_gpio = reset_gpio;
399ef536abdSDmitry Torokhov 	priv->chip = chip;
40012294577SMarek Vasut 	i2c_set_clientdata(client, priv);
40112294577SMarek Vasut 
4025c6a7a62SOlivier Sobrie 	/* Setup input device */
4035c6a7a62SOlivier Sobrie 	input->name = "ILI210x Touchscreen";
4045c6a7a62SOlivier Sobrie 	input->id.bustype = BUS_I2C;
4055c6a7a62SOlivier Sobrie 
4065c6a7a62SOlivier Sobrie 	/* Multi touch */
407b32fbeaeSSven Van Asbroeck 	max_xy = (chip->resolution ?: SZ_64K) - 1;
408b32fbeaeSSven Van Asbroeck 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0);
409b32fbeaeSSven Van Asbroeck 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0);
410f67cc3e9SMarek Vasut 	touchscreen_parse_properties(input, true, &priv->prop);
41143f06a4cSDmitry Torokhov 
412ef536abdSDmitry Torokhov 	error = input_mt_init_slots(input, priv->chip->max_touches,
413ef536abdSDmitry Torokhov 				    INPUT_MT_DIRECT);
41443f06a4cSDmitry Torokhov 	if (error) {
41543f06a4cSDmitry Torokhov 		dev_err(dev, "Unable to set up slots, err: %d\n", error);
41643f06a4cSDmitry Torokhov 		return error;
41743f06a4cSDmitry Torokhov 	}
4185c6a7a62SOlivier Sobrie 
41971f8e38aSDmitry Torokhov 	error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq,
42071f8e38aSDmitry Torokhov 					  IRQF_ONESHOT, client->name, priv);
4215c6a7a62SOlivier Sobrie 	if (error) {
4225c6a7a62SOlivier Sobrie 		dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
4235c6a7a62SOlivier Sobrie 			error);
4241bdec5d9SMarek Vasut 		return error;
4255c6a7a62SOlivier Sobrie 	}
4265c6a7a62SOlivier Sobrie 
42771f8e38aSDmitry Torokhov 	error = devm_add_action_or_reset(dev, ili210x_stop, priv);
42871f8e38aSDmitry Torokhov 	if (error)
42971f8e38aSDmitry Torokhov 		return error;
43071f8e38aSDmitry Torokhov 
431576057bfSDmitry Torokhov 	error = devm_device_add_group(dev, &ili210x_attr_group);
4325c6a7a62SOlivier Sobrie 	if (error) {
4335c6a7a62SOlivier Sobrie 		dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
4345c6a7a62SOlivier Sobrie 			error);
4351bdec5d9SMarek Vasut 		return error;
4365c6a7a62SOlivier Sobrie 	}
4375c6a7a62SOlivier Sobrie 
4385c6a7a62SOlivier Sobrie 	error = input_register_device(priv->input);
4395c6a7a62SOlivier Sobrie 	if (error) {
440971bd8faSMasanari Iida 		dev_err(dev, "Cannot register input device, err: %d\n", error);
441576057bfSDmitry Torokhov 		return error;
4425c6a7a62SOlivier Sobrie 	}
4435c6a7a62SOlivier Sobrie 
4445c6a7a62SOlivier Sobrie 	return 0;
4455c6a7a62SOlivier Sobrie }
4465c6a7a62SOlivier Sobrie 
4475c6a7a62SOlivier Sobrie static const struct i2c_device_id ili210x_i2c_id[] = {
448ef536abdSDmitry Torokhov 	{ "ili210x", (long)&ili210x_chip },
449ef536abdSDmitry Torokhov 	{ "ili2117", (long)&ili211x_chip },
450ef536abdSDmitry Torokhov 	{ "ili251x", (long)&ili251x_chip },
4515c6a7a62SOlivier Sobrie 	{ }
4525c6a7a62SOlivier Sobrie };
4535c6a7a62SOlivier Sobrie MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
4545c6a7a62SOlivier Sobrie 
455c5d0e4b5SMarek Vasut static const struct of_device_id ili210x_dt_ids[] = {
456ef536abdSDmitry Torokhov 	{ .compatible = "ilitek,ili210x", .data = &ili210x_chip },
457ef536abdSDmitry Torokhov 	{ .compatible = "ilitek,ili2117", .data = &ili211x_chip },
458ef536abdSDmitry Torokhov 	{ .compatible = "ilitek,ili251x", .data = &ili251x_chip },
459ef536abdSDmitry Torokhov 	{ }
460c5d0e4b5SMarek Vasut };
461c5d0e4b5SMarek Vasut MODULE_DEVICE_TABLE(of, ili210x_dt_ids);
462c5d0e4b5SMarek Vasut 
4635c6a7a62SOlivier Sobrie static struct i2c_driver ili210x_ts_driver = {
4645c6a7a62SOlivier Sobrie 	.driver = {
4655c6a7a62SOlivier Sobrie 		.name = "ili210x_i2c",
466c5d0e4b5SMarek Vasut 		.of_match_table = ili210x_dt_ids,
4675c6a7a62SOlivier Sobrie 	},
4685c6a7a62SOlivier Sobrie 	.id_table = ili210x_i2c_id,
4695c6a7a62SOlivier Sobrie 	.probe = ili210x_i2c_probe,
4705c6a7a62SOlivier Sobrie };
4715c6a7a62SOlivier Sobrie 
4725c6a7a62SOlivier Sobrie module_i2c_driver(ili210x_ts_driver);
4735c6a7a62SOlivier Sobrie 
4745c6a7a62SOlivier Sobrie MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
4755c6a7a62SOlivier Sobrie MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
4765c6a7a62SOlivier Sobrie MODULE_LICENSE("GPL");
477