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 = 10; retries; retries--) {
96 		error = st1232_ts_read_data(ts, REG_STATUS, 1);
97 		if (!error && ts->read_buf[0] == (STATUS_NORMAL | ERROR_NONE))
98 			return 0;
99 
100 		usleep_range(1000, 2000);
101 	}
102 
103 	return -ENXIO;
104 }
105 
106 static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
107 				     u16 *max_y)
108 {
109 	u8 *buf;
110 	int error;
111 
112 	/* select resolution register */
113 	error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
114 	if (error)
115 		return error;
116 
117 	buf = ts->read_buf;
118 
119 	*max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
120 	*max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
121 
122 	return 0;
123 }
124 
125 static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
126 {
127 	struct input_dev *input = ts->input_dev;
128 	struct input_mt_pos pos[ST_TS_MAX_FINGERS];
129 	u8 z[ST_TS_MAX_FINGERS];
130 	int slots[ST_TS_MAX_FINGERS];
131 	int n_contacts = 0;
132 	int i;
133 
134 	for (i = 0; i < ts->chip_info->max_fingers; i++) {
135 		u8 *buf = &ts->read_buf[i * 4];
136 
137 		if (buf[0] & BIT(7)) {
138 			unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
139 			unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
140 
141 			touchscreen_set_mt_pos(&pos[n_contacts],
142 					       &ts->prop, x, y);
143 
144 			/* st1232 includes a z-axis / touch strength */
145 			if (ts->chip_info->have_z)
146 				z[n_contacts] = ts->read_buf[i + 6];
147 
148 			n_contacts++;
149 		}
150 	}
151 
152 	input_mt_assign_slots(input, slots, pos, n_contacts, 0);
153 	for (i = 0; i < n_contacts; i++) {
154 		input_mt_slot(input, slots[i]);
155 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
156 		input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
157 		input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
158 		if (ts->chip_info->have_z)
159 			input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
160 	}
161 
162 	input_mt_sync_frame(input);
163 	input_sync(input);
164 
165 	return n_contacts;
166 }
167 
168 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
169 {
170 	struct st1232_ts_data *ts = dev_id;
171 	int count;
172 	int error;
173 
174 	error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
175 	if (error)
176 		goto out;
177 
178 	count = st1232_ts_parse_and_report(ts);
179 	if (!count) {
180 		if (ts->low_latency_req.dev) {
181 			dev_pm_qos_remove_request(&ts->low_latency_req);
182 			ts->low_latency_req.dev = NULL;
183 		}
184 	} else if (!ts->low_latency_req.dev) {
185 		/* First contact, request 100 us latency. */
186 		dev_pm_qos_add_ancestor_request(&ts->client->dev,
187 						&ts->low_latency_req,
188 						DEV_PM_QOS_RESUME_LATENCY, 100);
189 	}
190 
191 out:
192 	return IRQ_HANDLED;
193 }
194 
195 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
196 {
197 	if (ts->reset_gpio)
198 		gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
199 }
200 
201 static void st1232_ts_power_off(void *data)
202 {
203 	st1232_ts_power(data, false);
204 }
205 
206 static const struct st_chip_info st1232_chip_info = {
207 	.have_z		= true,
208 	.max_area	= 0xff,
209 	.max_fingers	= 2,
210 };
211 
212 static const struct st_chip_info st1633_chip_info = {
213 	.have_z		= false,
214 	.max_area	= 0x00,
215 	.max_fingers	= 5,
216 };
217 
218 static int st1232_ts_probe(struct i2c_client *client,
219 			   const struct i2c_device_id *id)
220 {
221 	const struct st_chip_info *match;
222 	struct st1232_ts_data *ts;
223 	struct input_dev *input_dev;
224 	u16 max_x, max_y;
225 	int error;
226 
227 	match = device_get_match_data(&client->dev);
228 	if (!match && id)
229 		match = (const void *)id->driver_data;
230 	if (!match) {
231 		dev_err(&client->dev, "unknown device model\n");
232 		return -ENODEV;
233 	}
234 
235 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
236 		dev_err(&client->dev, "need I2C_FUNC_I2C\n");
237 		return -EIO;
238 	}
239 
240 	if (!client->irq) {
241 		dev_err(&client->dev, "no IRQ?\n");
242 		return -EINVAL;
243 	}
244 
245 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
246 	if (!ts)
247 		return -ENOMEM;
248 
249 	ts->chip_info = match;
250 
251 	/* allocate a buffer according to the number of registers to read */
252 	ts->read_buf_len = ts->chip_info->max_fingers * 4;
253 	ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
254 	if (!ts->read_buf)
255 		return -ENOMEM;
256 
257 	input_dev = devm_input_allocate_device(&client->dev);
258 	if (!input_dev)
259 		return -ENOMEM;
260 
261 	ts->client = client;
262 	ts->input_dev = input_dev;
263 
264 	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
265 						 GPIOD_OUT_HIGH);
266 	if (IS_ERR(ts->reset_gpio)) {
267 		error = PTR_ERR(ts->reset_gpio);
268 		dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
269 			error);
270 		return error;
271 	}
272 
273 	st1232_ts_power(ts, true);
274 
275 	error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
276 	if (error) {
277 		dev_err(&client->dev,
278 			"Failed to install power off action: %d\n", error);
279 		return error;
280 	}
281 
282 	input_dev->name = "st1232-touchscreen";
283 	input_dev->id.bustype = BUS_I2C;
284 
285 	/* Wait until device is ready */
286 	error = st1232_ts_wait_ready(ts);
287 	if (error)
288 		return error;
289 
290 	/* Read resolution from the chip */
291 	error = st1232_ts_read_resolution(ts, &max_x, &max_y);
292 	if (error) {
293 		dev_err(&client->dev,
294 			"Failed to read resolution: %d\n", error);
295 		return error;
296 	}
297 
298 	if (ts->chip_info->have_z)
299 		input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
300 				     ts->chip_info->max_area, 0, 0);
301 
302 	input_set_abs_params(input_dev, ABS_MT_POSITION_X,
303 			     0, max_x, 0, 0);
304 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
305 			     0, max_y, 0, 0);
306 
307 	touchscreen_parse_properties(input_dev, true, &ts->prop);
308 
309 	error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
310 				    INPUT_MT_DIRECT | INPUT_MT_TRACK |
311 					INPUT_MT_DROP_UNUSED);
312 	if (error) {
313 		dev_err(&client->dev, "failed to initialize MT slots\n");
314 		return error;
315 	}
316 
317 	error = devm_request_threaded_irq(&client->dev, client->irq,
318 					  NULL, st1232_ts_irq_handler,
319 					  IRQF_ONESHOT,
320 					  client->name, ts);
321 	if (error) {
322 		dev_err(&client->dev, "Failed to register interrupt\n");
323 		return error;
324 	}
325 
326 	error = input_register_device(ts->input_dev);
327 	if (error) {
328 		dev_err(&client->dev, "Unable to register %s input device\n",
329 			input_dev->name);
330 		return error;
331 	}
332 
333 	i2c_set_clientdata(client, ts);
334 
335 	return 0;
336 }
337 
338 static int __maybe_unused st1232_ts_suspend(struct device *dev)
339 {
340 	struct i2c_client *client = to_i2c_client(dev);
341 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
342 
343 	disable_irq(client->irq);
344 
345 	if (!device_may_wakeup(&client->dev))
346 		st1232_ts_power(ts, false);
347 
348 	return 0;
349 }
350 
351 static int __maybe_unused st1232_ts_resume(struct device *dev)
352 {
353 	struct i2c_client *client = to_i2c_client(dev);
354 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
355 
356 	if (!device_may_wakeup(&client->dev))
357 		st1232_ts_power(ts, true);
358 
359 	enable_irq(client->irq);
360 
361 	return 0;
362 }
363 
364 static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
365 			 st1232_ts_suspend, st1232_ts_resume);
366 
367 static const struct i2c_device_id st1232_ts_id[] = {
368 	{ ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
369 	{ ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
370 	{ }
371 };
372 MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
373 
374 static const struct of_device_id st1232_ts_dt_ids[] = {
375 	{ .compatible = "sitronix,st1232", .data = &st1232_chip_info },
376 	{ .compatible = "sitronix,st1633", .data = &st1633_chip_info },
377 	{ }
378 };
379 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
380 
381 static struct i2c_driver st1232_ts_driver = {
382 	.probe		= st1232_ts_probe,
383 	.id_table	= st1232_ts_id,
384 	.driver = {
385 		.name	= ST1232_TS_NAME,
386 		.of_match_table = st1232_ts_dt_ids,
387 		.pm	= &st1232_ts_pm_ops,
388 	},
389 };
390 
391 module_i2c_driver(st1232_ts_driver);
392 
393 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
394 MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
395 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
396 MODULE_LICENSE("GPL v2");
397