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.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pm_qos.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 
25 #define ST1232_TS_NAME	"st1232-ts"
26 
27 #define MIN_X		0x00
28 #define MIN_Y		0x00
29 #define MAX_X		0x31f	/* (800 - 1) */
30 #define MAX_Y		0x1df	/* (480 - 1) */
31 #define MAX_AREA	0xff
32 #define MAX_FINGERS	2
33 
34 struct st1232_ts_finger {
35 	u16 x;
36 	u16 y;
37 	u8 t;
38 	bool is_valid;
39 };
40 
41 struct st1232_ts_data {
42 	struct i2c_client *client;
43 	struct input_dev *input_dev;
44 	struct st1232_ts_finger finger[MAX_FINGERS];
45 	struct dev_pm_qos_request low_latency_req;
46 	int reset_gpio;
47 };
48 
49 static int st1232_ts_read_data(struct st1232_ts_data *ts)
50 {
51 	struct st1232_ts_finger *finger = ts->finger;
52 	struct i2c_client *client = ts->client;
53 	struct i2c_msg msg[2];
54 	int error;
55 	u8 start_reg;
56 	u8 buf[10];
57 
58 	/* read touchscreen data from ST1232 */
59 	msg[0].addr = client->addr;
60 	msg[0].flags = 0;
61 	msg[0].len = 1;
62 	msg[0].buf = &start_reg;
63 	start_reg = 0x10;
64 
65 	msg[1].addr = ts->client->addr;
66 	msg[1].flags = I2C_M_RD;
67 	msg[1].len = sizeof(buf);
68 	msg[1].buf = buf;
69 
70 	error = i2c_transfer(client->adapter, msg, 2);
71 	if (error < 0)
72 		return error;
73 
74 	/* get "valid" bits */
75 	finger[0].is_valid = buf[2] >> 7;
76 	finger[1].is_valid = buf[5] >> 7;
77 
78 	/* get xy coordinate */
79 	if (finger[0].is_valid) {
80 		finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3];
81 		finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4];
82 		finger[0].t = buf[8];
83 	}
84 
85 	if (finger[1].is_valid) {
86 		finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6];
87 		finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7];
88 		finger[1].t = buf[9];
89 	}
90 
91 	return 0;
92 }
93 
94 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
95 {
96 	struct st1232_ts_data *ts = dev_id;
97 	struct st1232_ts_finger *finger = ts->finger;
98 	struct input_dev *input_dev = ts->input_dev;
99 	int count = 0;
100 	int i, ret;
101 
102 	ret = st1232_ts_read_data(ts);
103 	if (ret < 0)
104 		goto end;
105 
106 	/* multi touch protocol */
107 	for (i = 0; i < MAX_FINGERS; i++) {
108 		if (!finger[i].is_valid)
109 			continue;
110 
111 		input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t);
112 		input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x);
113 		input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y);
114 		input_mt_sync(input_dev);
115 		count++;
116 	}
117 
118 	/* SYN_MT_REPORT only if no contact */
119 	if (!count) {
120 		input_mt_sync(input_dev);
121 		if (ts->low_latency_req.dev) {
122 			dev_pm_qos_remove_request(&ts->low_latency_req);
123 			ts->low_latency_req.dev = NULL;
124 		}
125 	} else if (!ts->low_latency_req.dev) {
126 		/* First contact, request 100 us latency. */
127 		dev_pm_qos_add_ancestor_request(&ts->client->dev,
128 						&ts->low_latency_req,
129 						DEV_PM_QOS_RESUME_LATENCY, 100);
130 	}
131 
132 	/* SYN_REPORT */
133 	input_sync(input_dev);
134 
135 end:
136 	return IRQ_HANDLED;
137 }
138 
139 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
140 {
141 	if (gpio_is_valid(ts->reset_gpio))
142 		gpio_direction_output(ts->reset_gpio, poweron);
143 }
144 
145 static int st1232_ts_probe(struct i2c_client *client,
146 			   const struct i2c_device_id *id)
147 {
148 	struct st1232_ts_data *ts;
149 	struct input_dev *input_dev;
150 	int error;
151 
152 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
153 		dev_err(&client->dev, "need I2C_FUNC_I2C\n");
154 		return -EIO;
155 	}
156 
157 	if (!client->irq) {
158 		dev_err(&client->dev, "no IRQ?\n");
159 		return -EINVAL;
160 	}
161 
162 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
163 	if (!ts)
164 		return -ENOMEM;
165 
166 	input_dev = devm_input_allocate_device(&client->dev);
167 	if (!input_dev)
168 		return -ENOMEM;
169 
170 	ts->client = client;
171 	ts->input_dev = input_dev;
172 
173 	ts->reset_gpio = of_get_gpio(client->dev.of_node, 0);
174 	if (gpio_is_valid(ts->reset_gpio)) {
175 		error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL);
176 		if (error) {
177 			dev_err(&client->dev,
178 				"Unable to request GPIO pin %d.\n",
179 				ts->reset_gpio);
180 				return error;
181 		}
182 	}
183 
184 	st1232_ts_power(ts, true);
185 
186 	input_dev->name = "st1232-touchscreen";
187 	input_dev->id.bustype = BUS_I2C;
188 	input_dev->dev.parent = &client->dev;
189 
190 	__set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
191 	__set_bit(EV_SYN, input_dev->evbit);
192 	__set_bit(EV_KEY, input_dev->evbit);
193 	__set_bit(EV_ABS, input_dev->evbit);
194 
195 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
196 	input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
197 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
198 
199 	error = devm_request_threaded_irq(&client->dev, client->irq,
200 					  NULL, st1232_ts_irq_handler,
201 					  IRQF_ONESHOT,
202 					  client->name, ts);
203 	if (error) {
204 		dev_err(&client->dev, "Failed to register interrupt\n");
205 		return error;
206 	}
207 
208 	error = input_register_device(ts->input_dev);
209 	if (error) {
210 		dev_err(&client->dev, "Unable to register %s input device\n",
211 			input_dev->name);
212 		return error;
213 	}
214 
215 	i2c_set_clientdata(client, ts);
216 	device_init_wakeup(&client->dev, 1);
217 
218 	return 0;
219 }
220 
221 static int st1232_ts_remove(struct i2c_client *client)
222 {
223 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
224 
225 	st1232_ts_power(ts, false);
226 
227 	return 0;
228 }
229 
230 static int __maybe_unused st1232_ts_suspend(struct device *dev)
231 {
232 	struct i2c_client *client = to_i2c_client(dev);
233 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
234 
235 	if (device_may_wakeup(&client->dev)) {
236 		enable_irq_wake(client->irq);
237 	} else {
238 		disable_irq(client->irq);
239 		st1232_ts_power(ts, false);
240 	}
241 
242 	return 0;
243 }
244 
245 static int __maybe_unused st1232_ts_resume(struct device *dev)
246 {
247 	struct i2c_client *client = to_i2c_client(dev);
248 	struct st1232_ts_data *ts = i2c_get_clientdata(client);
249 
250 	if (device_may_wakeup(&client->dev)) {
251 		disable_irq_wake(client->irq);
252 	} else {
253 		st1232_ts_power(ts, true);
254 		enable_irq(client->irq);
255 	}
256 
257 	return 0;
258 }
259 
260 static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
261 			 st1232_ts_suspend, st1232_ts_resume);
262 
263 static const struct i2c_device_id st1232_ts_id[] = {
264 	{ ST1232_TS_NAME, 0 },
265 	{ }
266 };
267 MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
268 
269 static const struct of_device_id st1232_ts_dt_ids[] = {
270 	{ .compatible = "sitronix,st1232", },
271 	{ }
272 };
273 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
274 
275 static struct i2c_driver st1232_ts_driver = {
276 	.probe		= st1232_ts_probe,
277 	.remove		= st1232_ts_remove,
278 	.id_table	= st1232_ts_id,
279 	.driver = {
280 		.name	= ST1232_TS_NAME,
281 		.of_match_table = st1232_ts_dt_ids,
282 		.pm	= &st1232_ts_pm_ops,
283 	},
284 };
285 
286 module_i2c_driver(st1232_ts_driver);
287 
288 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
289 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
290 MODULE_LICENSE("GPL v2");
291