1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ST1232 Touchscreen Controller Driver
4  *
5  * Copyright (C) 2010 Renesas Solutions Corp.
6  *	Tony SIM <chinyeow.sim.xt@renesas.com>
7  *
8  * Using code from:
9  *  - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
10  *	Copyright (C) 2007 Google, Inc.
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/input/mt.h>
18 #include <linux/input/touchscreen.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/pm_qos.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 
26 #define ST1232_TS_NAME	"st1232-ts"
27 #define ST1633_TS_NAME	"st1633-ts"
28 
29 #define REG_STATUS		0x01	/* Device Status | Error Code */
30 
31 #define STATUS_NORMAL		0x00
32 #define STATUS_INIT		0x01
33 #define STATUS_ERROR		0x02
34 #define STATUS_AUTO_TUNING	0x03
35 #define STATUS_IDLE		0x04
36 #define STATUS_POWER_DOWN	0x05
37 
38 #define ERROR_NONE		0x00
39 #define ERROR_INVALID_ADDRESS	0x10
40 #define ERROR_INVALID_VALUE	0x20
41 #define ERROR_INVALID_PLATFORM	0x30
42 
43 #define REG_XY_RESOLUTION	0x04
44 #define REG_XY_COORDINATES	0x12
45 #define ST_TS_MAX_FINGERS	10
46 
47 struct st_chip_info {
48 	bool	have_z;
49 	u16	max_area;
50 	u16	max_fingers;
51 };
52 
53 struct st1232_ts_data {
54 	struct i2c_client *client;
55 	struct input_dev *input_dev;
56 	struct touchscreen_properties prop;
57 	struct dev_pm_qos_request low_latency_req;
58 	struct gpio_desc *reset_gpio;
59 	const struct st_chip_info *chip_info;
60 	int read_buf_len;
61 	u8 *read_buf;
62 };
63 
64 static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
65 			       unsigned int n)
66 {
67 	struct i2c_client *client = ts->client;
68 	struct i2c_msg msg[] = {
69 		{
70 			.addr	= client->addr,
71 			.len	= sizeof(reg),
72 			.buf	= &reg,
73 		},
74 		{
75 			.addr	= client->addr,
76 			.flags	= I2C_M_RD | I2C_M_DMA_SAFE,
77 			.len	= n,
78 			.buf	= ts->read_buf,
79 		}
80 	};
81 	int ret;
82 
83 	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
84 	if (ret != ARRAY_SIZE(msg))
85 		return ret < 0 ? ret : -EIO;
86 
87 	return 0;
88 }
89 
90 static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
91 {
92 	unsigned int retries;
93 	int error;
94 
95 	for (retries = 100; retries; retries--) {
96 		error = st1232_ts_read_data(ts, REG_STATUS, 1);
97 		if (!error) {
98 			switch (ts->read_buf[0]) {
99 			case STATUS_NORMAL | ERROR_NONE:
100 			case STATUS_IDLE | ERROR_NONE:
101 				return 0;
102 			}
103 		}
104 
105 		usleep_range(1000, 2000);
106 	}
107 
108 	return -ENXIO;
109 }
110 
111 static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
112 				     u16 *max_y)
113 {
114 	u8 *buf;
115 	int error;
116 
117 	/* select resolution register */
118 	error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
119 	if (error)
120 		return error;
121 
122 	buf = ts->read_buf;
123 
124 	*max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
125 	*max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
126 
127 	return 0;
128 }
129 
130 static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
131 {
132 	struct input_dev *input = ts->input_dev;
133 	struct input_mt_pos pos[ST_TS_MAX_FINGERS];
134 	u8 z[ST_TS_MAX_FINGERS];
135 	int slots[ST_TS_MAX_FINGERS];
136 	int n_contacts = 0;
137 	int i;
138 
139 	for (i = 0; i < ts->chip_info->max_fingers; i++) {
140 		u8 *buf = &ts->read_buf[i * 4];
141 
142 		if (buf[0] & BIT(7)) {
143 			unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
144 			unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
145 
146 			touchscreen_set_mt_pos(&pos[n_contacts],
147 					       &ts->prop, x, y);
148 
149 			/* st1232 includes a z-axis / touch strength */
150 			if (ts->chip_info->have_z)
151 				z[n_contacts] = ts->read_buf[i + 6];
152 
153 			n_contacts++;
154 		}
155 	}
156 
157 	input_mt_assign_slots(input, slots, pos, n_contacts, 0);
158 	for (i = 0; i < n_contacts; i++) {
159 		input_mt_slot(input, slots[i]);
160 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
161 		input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
162 		input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
163 		if (ts->chip_info->have_z)
164 			input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
165 	}
166 
167 	input_mt_sync_frame(input);
168 	input_sync(input);
169 
170 	return n_contacts;
171 }
172 
173 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
174 {
175 	struct st1232_ts_data *ts = dev_id;
176 	int count;
177 	int error;
178 
179 	error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
180 	if (error)
181 		goto out;
182 
183 	count = st1232_ts_parse_and_report(ts);
184 	if (!count) {
185 		if (ts->low_latency_req.dev) {
186 			dev_pm_qos_remove_request(&ts->low_latency_req);
187 			ts->low_latency_req.dev = NULL;
188 		}
189 	} else if (!ts->low_latency_req.dev) {
190 		/* First contact, request 100 us latency. */
191 		dev_pm_qos_add_ancestor_request(&ts->client->dev,
192 						&ts->low_latency_req,
193 						DEV_PM_QOS_RESUME_LATENCY, 100);
194 	}
195 
196 out:
197 	return IRQ_HANDLED;
198 }
199 
200 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
201 {
202 	if (ts->reset_gpio)
203 		gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
204 }
205 
206 static void st1232_ts_power_off(void *data)
207 {
208 	st1232_ts_power(data, false);
209 }
210 
211 static const struct st_chip_info st1232_chip_info = {
212 	.have_z		= true,
213 	.max_area	= 0xff,
214 	.max_fingers	= 2,
215 };
216 
217 static const struct st_chip_info st1633_chip_info = {
218 	.have_z		= false,
219 	.max_area	= 0x00,
220 	.max_fingers	= 5,
221 };
222 
223 static int st1232_ts_probe(struct i2c_client *client,
224 			   const struct i2c_device_id *id)
225 {
226 	const struct st_chip_info *match;
227 	struct st1232_ts_data *ts;
228 	struct input_dev *input_dev;
229 	u16 max_x, max_y;
230 	int error;
231 
232 	match = device_get_match_data(&client->dev);
233 	if (!match && id)
234 		match = (const void *)id->driver_data;
235 	if (!match) {
236 		dev_err(&client->dev, "unknown device model\n");
237 		return -ENODEV;
238 	}
239 
240 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
241 		dev_err(&client->dev, "need I2C_FUNC_I2C\n");
242 		return -EIO;
243 	}
244 
245 	if (!client->irq) {
246 		dev_err(&client->dev, "no IRQ?\n");
247 		return -EINVAL;
248 	}
249 
250 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
251 	if (!ts)
252 		return -ENOMEM;
253 
254 	ts->chip_info = match;
255 
256 	/* allocate a buffer according to the number of registers to read */
257 	ts->read_buf_len = ts->chip_info->max_fingers * 4;
258 	ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
259 	if (!ts->read_buf)
260 		return -ENOMEM;
261 
262 	input_dev = devm_input_allocate_device(&client->dev);
263 	if (!input_dev)
264 		return -ENOMEM;
265 
266 	ts->client = client;
267 	ts->input_dev = input_dev;
268 
269 	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
270 						 GPIOD_OUT_HIGH);
271 	if (IS_ERR(ts->reset_gpio)) {
272 		error = PTR_ERR(ts->reset_gpio);
273 		dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
274 			error);
275 		return error;
276 	}
277 
278 	st1232_ts_power(ts, true);
279 
280 	error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
281 	if (error) {
282 		dev_err(&client->dev,
283 			"Failed to install power off action: %d\n", error);
284 		return error;
285 	}
286 
287 	input_dev->name = "st1232-touchscreen";
288 	input_dev->id.bustype = BUS_I2C;
289 
290 	/* Wait until device is ready */
291 	error = st1232_ts_wait_ready(ts);
292 	if (error)
293 		return error;
294 
295 	/* Read resolution from the chip */
296 	error = st1232_ts_read_resolution(ts, &max_x, &max_y);
297 	if (error) {
298 		dev_err(&client->dev,
299 			"Failed to read resolution: %d\n", error);
300 		return error;
301 	}
302 
303 	if (ts->chip_info->have_z)
304 		input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
305 				     ts->chip_info->max_area, 0, 0);
306 
307 	input_set_abs_params(input_dev, ABS_MT_POSITION_X,
308 			     0, max_x, 0, 0);
309 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
310 			     0, max_y, 0, 0);
311 
312 	touchscreen_parse_properties(input_dev, true, &ts->prop);
313 
314 	error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
315 				    INPUT_MT_DIRECT | INPUT_MT_TRACK |
316 					INPUT_MT_DROP_UNUSED);
317 	if (error) {
318 		dev_err(&client->dev, "failed to initialize MT slots\n");
319 		return error;
320 	}
321 
322 	error = devm_request_threaded_irq(&client->dev, client->irq,
323 					  NULL, st1232_ts_irq_handler,
324 					  IRQF_ONESHOT,
325 					  client->name, ts);
326 	if (error) {
327 		dev_err(&client->dev, "Failed to register interrupt\n");
328 		return error;
329 	}
330 
331 	error = input_register_device(ts->input_dev);
332 	if (error) {
333 		dev_err(&client->dev, "Unable to register %s input device\n",
334 			input_dev->name);
335 		return error;
336 	}
337 
338 	i2c_set_clientdata(client, ts);
339 
340 	return 0;
341 }
342 
343 static int __maybe_unused st1232_ts_suspend(struct device *dev)
344 {
345 	struct i2c_client *client = to_i2c_client(dev);
346 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
347 
348 	disable_irq(client->irq);
349 
350 	if (!device_may_wakeup(&client->dev))
351 		st1232_ts_power(ts, false);
352 
353 	return 0;
354 }
355 
356 static int __maybe_unused st1232_ts_resume(struct device *dev)
357 {
358 	struct i2c_client *client = to_i2c_client(dev);
359 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
360 
361 	if (!device_may_wakeup(&client->dev))
362 		st1232_ts_power(ts, true);
363 
364 	enable_irq(client->irq);
365 
366 	return 0;
367 }
368 
369 static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
370 			 st1232_ts_suspend, st1232_ts_resume);
371 
372 static const struct i2c_device_id st1232_ts_id[] = {
373 	{ ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
374 	{ ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
375 	{ }
376 };
377 MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
378 
379 static const struct of_device_id st1232_ts_dt_ids[] = {
380 	{ .compatible = "sitronix,st1232", .data = &st1232_chip_info },
381 	{ .compatible = "sitronix,st1633", .data = &st1633_chip_info },
382 	{ }
383 };
384 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
385 
386 static struct i2c_driver st1232_ts_driver = {
387 	.probe		= st1232_ts_probe,
388 	.id_table	= st1232_ts_id,
389 	.driver = {
390 		.name	= ST1232_TS_NAME,
391 		.of_match_table = st1232_ts_dt_ids,
392 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
393 		.pm	= &st1232_ts_pm_ops,
394 	},
395 };
396 
397 module_i2c_driver(st1232_ts_driver);
398 
399 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
400 MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
401 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
402 MODULE_LICENSE("GPL v2");
403