1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
5 #include <linux/delay.h>
6 #include <linux/i2c.h>
7 #include <linux/input.h>
8 #include <linux/input/mt.h>
9 #include <linux/input/touchscreen.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/regulator/consumer.h>
14 
15 #define IST30XX_REG_STATUS		0x20
16 #define IST30XX_REG_CHIPID		(0x40000000 | IST3038C_DIRECT_ACCESS)
17 
18 #define IST30XX_WHOAMI			0x30003000
19 #define IST30XXA_WHOAMI			0x300a300a
20 #define IST30XXB_WHOAMI			0x300b300b
21 #define IST3038_WHOAMI			0x30383038
22 
23 #define IST3032C_WHOAMI			0x32c
24 #define IST3038C_WHOAMI			0x38c
25 
26 #define IST3038B_REG_CHIPID		0x30
27 #define IST3038B_WHOAMI			0x30380b
28 
29 #define IST3038C_HIB_ACCESS		(0x800B << 16)
30 #define IST3038C_DIRECT_ACCESS		BIT(31)
31 #define IST3038C_REG_CHIPID		(0x40001000 | IST3038C_DIRECT_ACCESS)
32 #define IST3038C_REG_HIB_BASE		0x30000100
33 #define IST3038C_REG_TOUCH_STATUS	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS)
34 #define IST3038C_REG_TOUCH_COORD	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x8)
35 #define IST3038C_REG_INTR_MESSAGE	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x4)
36 #define IST3038C_CHIP_ON_DELAY_MS	60
37 #define IST3038C_I2C_RETRY_COUNT	3
38 #define IST3038C_MAX_FINGER_NUM		10
39 #define IST3038C_X_MASK			GENMASK(23, 12)
40 #define IST3038C_Y_MASK			GENMASK(11, 0)
41 #define IST3038C_AREA_MASK		GENMASK(27, 24)
42 #define IST3038C_FINGER_COUNT_MASK	GENMASK(15, 12)
43 #define IST3038C_FINGER_STATUS_MASK	GENMASK(9, 0)
44 #define IST3032C_KEY_STATUS_MASK	GENMASK(20, 16)
45 
46 struct imagis_properties {
47 	unsigned int interrupt_msg_cmd;
48 	unsigned int touch_coord_cmd;
49 	unsigned int whoami_cmd;
50 	unsigned int whoami_val;
51 	bool protocol_b;
52 	bool touch_keys_supported;
53 };
54 
55 struct imagis_ts {
56 	struct i2c_client *client;
57 	const struct imagis_properties *tdata;
58 	struct input_dev *input_dev;
59 	struct touchscreen_properties prop;
60 	struct regulator_bulk_data supplies[2];
61 	u32 keycodes[5];
62 	int num_keycodes;
63 };
64 
imagis_i2c_read_reg(struct imagis_ts * ts,unsigned int reg,u32 * data)65 static int imagis_i2c_read_reg(struct imagis_ts *ts,
66 			       unsigned int reg, u32 *data)
67 {
68 	__be32 ret_be;
69 	__be32 reg_be = cpu_to_be32(reg);
70 	struct i2c_msg msg[] = {
71 		{
72 			.addr = ts->client->addr,
73 			.flags = 0,
74 			.buf = (unsigned char *)&reg_be,
75 			.len = sizeof(reg_be),
76 		}, {
77 			.addr = ts->client->addr,
78 			.flags = I2C_M_RD,
79 			.buf = (unsigned char *)&ret_be,
80 			.len = sizeof(ret_be),
81 		},
82 	};
83 	int ret, error;
84 	int retry = IST3038C_I2C_RETRY_COUNT;
85 
86 	/* Retry in case the controller fails to respond */
87 	do {
88 		ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
89 		if (ret == ARRAY_SIZE(msg)) {
90 			*data = be32_to_cpu(ret_be);
91 			return 0;
92 		}
93 
94 		error = ret < 0 ? ret : -EIO;
95 		dev_err(&ts->client->dev,
96 			"%s - i2c_transfer failed: %d (%d)\n",
97 			__func__, error, ret);
98 	} while (--retry);
99 
100 	return error;
101 }
102 
imagis_interrupt(int irq,void * dev_id)103 static irqreturn_t imagis_interrupt(int irq, void *dev_id)
104 {
105 	struct imagis_ts *ts = dev_id;
106 	u32 intr_message, finger_status;
107 	unsigned int finger_count, finger_pressed, key_pressed;
108 	int i;
109 	int error;
110 
111 	error = imagis_i2c_read_reg(ts, ts->tdata->interrupt_msg_cmd, &intr_message);
112 	if (error) {
113 		dev_err(&ts->client->dev,
114 			"failed to read the interrupt message: %d\n", error);
115 		goto out;
116 	}
117 
118 	finger_count = FIELD_GET(IST3038C_FINGER_COUNT_MASK, intr_message);
119 	if (finger_count > IST3038C_MAX_FINGER_NUM) {
120 		dev_err(&ts->client->dev,
121 			"finger count %d is more than maximum supported\n",
122 			finger_count);
123 		goto out;
124 	}
125 
126 	finger_pressed = FIELD_GET(IST3038C_FINGER_STATUS_MASK, intr_message);
127 
128 	for (i = 0; i < finger_count; i++) {
129 		if (ts->tdata->protocol_b)
130 			error = imagis_i2c_read_reg(ts,
131 						    ts->tdata->touch_coord_cmd + (i * 4),
132 						    &finger_status);
133 		else
134 			error = imagis_i2c_read_reg(ts,
135 						    ts->tdata->touch_coord_cmd, &finger_status);
136 		if (error) {
137 			dev_err(&ts->client->dev,
138 				"failed to read coordinates for finger %d: %d\n",
139 				i, error);
140 			goto out;
141 		}
142 
143 		input_mt_slot(ts->input_dev, i);
144 		input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER,
145 					   finger_pressed & BIT(i));
146 		touchscreen_report_pos(ts->input_dev, &ts->prop,
147 				       FIELD_GET(IST3038C_X_MASK, finger_status),
148 				       FIELD_GET(IST3038C_Y_MASK, finger_status),
149 				       true);
150 		input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR,
151 				 FIELD_GET(IST3038C_AREA_MASK, finger_status));
152 	}
153 
154 	key_pressed = FIELD_GET(IST3032C_KEY_STATUS_MASK, intr_message);
155 
156 	for (int i = 0; i < ts->num_keycodes; i++)
157 		input_report_key(ts->input_dev, ts->keycodes[i],
158 				 key_pressed & BIT(i));
159 
160 	input_mt_sync_frame(ts->input_dev);
161 	input_sync(ts->input_dev);
162 
163 out:
164 	return IRQ_HANDLED;
165 }
166 
imagis_power_off(void * _ts)167 static void imagis_power_off(void *_ts)
168 {
169 	struct imagis_ts *ts = _ts;
170 
171 	regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
172 }
173 
imagis_power_on(struct imagis_ts * ts)174 static int imagis_power_on(struct imagis_ts *ts)
175 {
176 	int error;
177 
178 	error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
179 	if (error)
180 		return error;
181 
182 	msleep(IST3038C_CHIP_ON_DELAY_MS);
183 
184 	return 0;
185 }
186 
imagis_start(struct imagis_ts * ts)187 static int imagis_start(struct imagis_ts *ts)
188 {
189 	int error;
190 
191 	error = imagis_power_on(ts);
192 	if (error)
193 		return error;
194 
195 	enable_irq(ts->client->irq);
196 
197 	return 0;
198 }
199 
imagis_stop(struct imagis_ts * ts)200 static int imagis_stop(struct imagis_ts *ts)
201 {
202 	disable_irq(ts->client->irq);
203 
204 	imagis_power_off(ts);
205 
206 	return 0;
207 }
208 
imagis_input_open(struct input_dev * dev)209 static int imagis_input_open(struct input_dev *dev)
210 {
211 	struct imagis_ts *ts = input_get_drvdata(dev);
212 
213 	return imagis_start(ts);
214 }
215 
imagis_input_close(struct input_dev * dev)216 static void imagis_input_close(struct input_dev *dev)
217 {
218 	struct imagis_ts *ts = input_get_drvdata(dev);
219 
220 	imagis_stop(ts);
221 }
222 
imagis_init_input_dev(struct imagis_ts * ts)223 static int imagis_init_input_dev(struct imagis_ts *ts)
224 {
225 	struct input_dev *input_dev;
226 	int error;
227 
228 	input_dev = devm_input_allocate_device(&ts->client->dev);
229 	if (!input_dev)
230 		return -ENOMEM;
231 
232 	ts->input_dev = input_dev;
233 
234 	input_dev->name = "Imagis capacitive touchscreen";
235 	input_dev->phys = "input/ts";
236 	input_dev->id.bustype = BUS_I2C;
237 	input_dev->open = imagis_input_open;
238 	input_dev->close = imagis_input_close;
239 
240 	input_set_drvdata(input_dev, ts);
241 
242 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
243 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
244 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 16, 0, 0);
245 	if (ts->tdata->touch_keys_supported) {
246 		ts->num_keycodes = of_property_read_variable_u32_array(
247 				ts->client->dev.of_node, "linux,keycodes",
248 				ts->keycodes, 0, ARRAY_SIZE(ts->keycodes));
249 		if (ts->num_keycodes <= 0) {
250 			ts->keycodes[0] = KEY_APPSELECT;
251 			ts->keycodes[1] = KEY_BACK;
252 			ts->num_keycodes = 2;
253 		}
254 
255 		input_dev->keycodemax = ts->num_keycodes;
256 		input_dev->keycodesize = sizeof(ts->keycodes[0]);
257 		input_dev->keycode = ts->keycodes;
258 	}
259 
260 	for (int i = 0; i < ts->num_keycodes; i++)
261 		input_set_capability(input_dev, EV_KEY, ts->keycodes[i]);
262 
263 	touchscreen_parse_properties(input_dev, true, &ts->prop);
264 	if (!ts->prop.max_x || !ts->prop.max_y) {
265 		dev_err(&ts->client->dev,
266 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
267 		return -EINVAL;
268 	}
269 
270 	error = input_mt_init_slots(input_dev,
271 				    IST3038C_MAX_FINGER_NUM,
272 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
273 	if (error) {
274 		dev_err(&ts->client->dev,
275 			"Failed to initialize MT slots: %d", error);
276 		return error;
277 	}
278 
279 	error = input_register_device(input_dev);
280 	if (error) {
281 		dev_err(&ts->client->dev,
282 			"Failed to register input device: %d", error);
283 		return error;
284 	}
285 
286 	return 0;
287 }
288 
imagis_init_regulators(struct imagis_ts * ts)289 static int imagis_init_regulators(struct imagis_ts *ts)
290 {
291 	struct i2c_client *client = ts->client;
292 
293 	ts->supplies[0].supply = "vdd";
294 	ts->supplies[1].supply = "vddio";
295 	return devm_regulator_bulk_get(&client->dev,
296 				       ARRAY_SIZE(ts->supplies),
297 				       ts->supplies);
298 }
299 
imagis_probe(struct i2c_client * i2c)300 static int imagis_probe(struct i2c_client *i2c)
301 {
302 	struct device *dev = &i2c->dev;
303 	struct imagis_ts *ts;
304 	int chip_id, error;
305 
306 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
307 	if (!ts)
308 		return -ENOMEM;
309 
310 	ts->client = i2c;
311 
312 	ts->tdata = device_get_match_data(dev);
313 	if (!ts->tdata) {
314 		dev_err(dev, "missing chip data\n");
315 		return -EINVAL;
316 	}
317 
318 	error = imagis_init_regulators(ts);
319 	if (error) {
320 		dev_err(dev, "regulator init error: %d\n", error);
321 		return error;
322 	}
323 
324 	error = imagis_power_on(ts);
325 	if (error) {
326 		dev_err(dev, "failed to enable regulators: %d\n", error);
327 		return error;
328 	}
329 
330 	error = devm_add_action_or_reset(dev, imagis_power_off, ts);
331 	if (error) {
332 		dev_err(dev, "failed to install poweroff action: %d\n", error);
333 		return error;
334 	}
335 
336 	error = imagis_i2c_read_reg(ts, ts->tdata->whoami_cmd, &chip_id);
337 	if (error) {
338 		dev_err(dev, "chip ID read failure: %d\n", error);
339 		return error;
340 	}
341 
342 	if (chip_id != ts->tdata->whoami_val) {
343 		dev_err(dev, "unknown chip ID: 0x%x\n", chip_id);
344 		return -EINVAL;
345 	}
346 
347 	error = devm_request_threaded_irq(dev, i2c->irq,
348 					  NULL, imagis_interrupt,
349 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
350 					  "imagis-touchscreen", ts);
351 	if (error) {
352 		dev_err(dev, "IRQ %d allocation failure: %d\n",
353 			i2c->irq, error);
354 		return error;
355 	}
356 
357 	error = imagis_init_input_dev(ts);
358 	if (error)
359 		return error;
360 
361 	return 0;
362 }
363 
imagis_suspend(struct device * dev)364 static int imagis_suspend(struct device *dev)
365 {
366 	struct i2c_client *client = to_i2c_client(dev);
367 	struct imagis_ts *ts = i2c_get_clientdata(client);
368 	int retval = 0;
369 
370 	mutex_lock(&ts->input_dev->mutex);
371 
372 	if (input_device_enabled(ts->input_dev))
373 		retval = imagis_stop(ts);
374 
375 	mutex_unlock(&ts->input_dev->mutex);
376 
377 	return retval;
378 }
379 
imagis_resume(struct device * dev)380 static int imagis_resume(struct device *dev)
381 {
382 	struct i2c_client *client = to_i2c_client(dev);
383 	struct imagis_ts *ts = i2c_get_clientdata(client);
384 	int retval = 0;
385 
386 	mutex_lock(&ts->input_dev->mutex);
387 
388 	if (input_device_enabled(ts->input_dev))
389 		retval = imagis_start(ts);
390 
391 	mutex_unlock(&ts->input_dev->mutex);
392 
393 	return retval;
394 }
395 
396 static DEFINE_SIMPLE_DEV_PM_OPS(imagis_pm_ops, imagis_suspend, imagis_resume);
397 
398 static const struct imagis_properties imagis_3032c_data = {
399 	.interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE,
400 	.touch_coord_cmd = IST3038C_REG_TOUCH_COORD,
401 	.whoami_cmd = IST3038C_REG_CHIPID,
402 	.whoami_val = IST3032C_WHOAMI,
403 	.touch_keys_supported = true,
404 	.protocol_b = true,
405 };
406 
407 static const struct imagis_properties imagis_3038_data = {
408 	.interrupt_msg_cmd = IST30XX_REG_STATUS,
409 	.touch_coord_cmd = IST30XX_REG_STATUS,
410 	.whoami_cmd = IST30XX_REG_CHIPID,
411 	.whoami_val = IST3038_WHOAMI,
412 	.touch_keys_supported = true,
413 };
414 
415 static const struct imagis_properties imagis_3038b_data = {
416 	.interrupt_msg_cmd = IST30XX_REG_STATUS,
417 	.touch_coord_cmd = IST30XX_REG_STATUS,
418 	.whoami_cmd = IST3038B_REG_CHIPID,
419 	.whoami_val = IST3038B_WHOAMI,
420 };
421 
422 static const struct imagis_properties imagis_3038c_data = {
423 	.interrupt_msg_cmd = IST3038C_REG_INTR_MESSAGE,
424 	.touch_coord_cmd = IST3038C_REG_TOUCH_COORD,
425 	.whoami_cmd = IST3038C_REG_CHIPID,
426 	.whoami_val = IST3038C_WHOAMI,
427 	.protocol_b = true,
428 };
429 
430 #ifdef CONFIG_OF
431 static const struct of_device_id imagis_of_match[] = {
432 	{ .compatible = "imagis,ist3032c", .data = &imagis_3032c_data },
433 	{ .compatible = "imagis,ist3038", .data = &imagis_3038_data },
434 	{ .compatible = "imagis,ist3038b", .data = &imagis_3038b_data },
435 	{ .compatible = "imagis,ist3038c", .data = &imagis_3038c_data },
436 	{ },
437 };
438 MODULE_DEVICE_TABLE(of, imagis_of_match);
439 #endif
440 
441 static struct i2c_driver imagis_ts_driver = {
442 	.driver = {
443 		.name = "imagis-touchscreen",
444 		.pm = pm_sleep_ptr(&imagis_pm_ops),
445 		.of_match_table = of_match_ptr(imagis_of_match),
446 	},
447 	.probe = imagis_probe,
448 };
449 
450 module_i2c_driver(imagis_ts_driver);
451 
452 MODULE_DESCRIPTION("Imagis IST3038C Touchscreen Driver");
453 MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>");
454 MODULE_LICENSE("GPL");
455