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 IST3038C_HIB_ACCESS		(0x800B << 16)
16 #define IST3038C_DIRECT_ACCESS		BIT(31)
17 #define IST3038C_REG_CHIPID		0x40001000
18 #define IST3038C_REG_HIB_BASE		0x30000100
19 #define IST3038C_REG_TOUCH_STATUS	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS)
20 #define IST3038C_REG_TOUCH_COORD	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x8)
21 #define IST3038C_REG_INTR_MESSAGE	(IST3038C_REG_HIB_BASE | IST3038C_HIB_ACCESS | 0x4)
22 #define IST3038C_WHOAMI			0x38c
23 #define IST3038C_CHIP_ON_DELAY_MS	60
24 #define IST3038C_I2C_RETRY_COUNT	3
25 #define IST3038C_MAX_FINGER_NUM		10
26 #define IST3038C_X_MASK			GENMASK(23, 12)
27 #define IST3038C_Y_MASK			GENMASK(11, 0)
28 #define IST3038C_AREA_MASK		GENMASK(27, 24)
29 #define IST3038C_FINGER_COUNT_MASK	GENMASK(15, 12)
30 #define IST3038C_FINGER_STATUS_MASK	GENMASK(9, 0)
31 
32 struct imagis_ts {
33 	struct i2c_client *client;
34 	struct input_dev *input_dev;
35 	struct touchscreen_properties prop;
36 	struct regulator_bulk_data supplies[2];
37 };
38 
imagis_i2c_read_reg(struct imagis_ts * ts,unsigned int reg,u32 * data)39 static int imagis_i2c_read_reg(struct imagis_ts *ts,
40 			       unsigned int reg, u32 *data)
41 {
42 	__be32 ret_be;
43 	__be32 reg_be = cpu_to_be32(reg);
44 	struct i2c_msg msg[] = {
45 		{
46 			.addr = ts->client->addr,
47 			.flags = 0,
48 			.buf = (unsigned char *)&reg_be,
49 			.len = sizeof(reg_be),
50 		}, {
51 			.addr = ts->client->addr,
52 			.flags = I2C_M_RD,
53 			.buf = (unsigned char *)&ret_be,
54 			.len = sizeof(ret_be),
55 		},
56 	};
57 	int ret, error;
58 	int retry = IST3038C_I2C_RETRY_COUNT;
59 
60 	/* Retry in case the controller fails to respond */
61 	do {
62 		ret = i2c_transfer(ts->client->adapter, msg, ARRAY_SIZE(msg));
63 		if (ret == ARRAY_SIZE(msg)) {
64 			*data = be32_to_cpu(ret_be);
65 			return 0;
66 		}
67 
68 		error = ret < 0 ? ret : -EIO;
69 		dev_err(&ts->client->dev,
70 			"%s - i2c_transfer failed: %d (%d)\n",
71 			__func__, error, ret);
72 	} while (--retry);
73 
74 	return error;
75 }
76 
imagis_interrupt(int irq,void * dev_id)77 static irqreturn_t imagis_interrupt(int irq, void *dev_id)
78 {
79 	struct imagis_ts *ts = dev_id;
80 	u32 intr_message, finger_status;
81 	unsigned int finger_count, finger_pressed;
82 	int i;
83 	int error;
84 
85 	error = imagis_i2c_read_reg(ts, IST3038C_REG_INTR_MESSAGE,
86 				    &intr_message);
87 	if (error) {
88 		dev_err(&ts->client->dev,
89 			"failed to read the interrupt message: %d\n", error);
90 		goto out;
91 	}
92 
93 	finger_count = FIELD_GET(IST3038C_FINGER_COUNT_MASK, intr_message);
94 	if (finger_count > IST3038C_MAX_FINGER_NUM) {
95 		dev_err(&ts->client->dev,
96 			"finger count %d is more than maximum supported\n",
97 			finger_count);
98 		goto out;
99 	}
100 
101 	finger_pressed = FIELD_GET(IST3038C_FINGER_STATUS_MASK, intr_message);
102 
103 	for (i = 0; i < finger_count; i++) {
104 		error = imagis_i2c_read_reg(ts,
105 					    IST3038C_REG_TOUCH_COORD + (i * 4),
106 					    &finger_status);
107 		if (error) {
108 			dev_err(&ts->client->dev,
109 				"failed to read coordinates for finger %d: %d\n",
110 				i, error);
111 			goto out;
112 		}
113 
114 		input_mt_slot(ts->input_dev, i);
115 		input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER,
116 					   finger_pressed & BIT(i));
117 		touchscreen_report_pos(ts->input_dev, &ts->prop,
118 				       FIELD_GET(IST3038C_X_MASK, finger_status),
119 				       FIELD_GET(IST3038C_Y_MASK, finger_status),
120 				       true);
121 		input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR,
122 				 FIELD_GET(IST3038C_AREA_MASK, finger_status));
123 	}
124 
125 	input_mt_sync_frame(ts->input_dev);
126 	input_sync(ts->input_dev);
127 
128 out:
129 	return IRQ_HANDLED;
130 }
131 
imagis_power_off(void * _ts)132 static void imagis_power_off(void *_ts)
133 {
134 	struct imagis_ts *ts = _ts;
135 
136 	regulator_bulk_disable(ARRAY_SIZE(ts->supplies), ts->supplies);
137 }
138 
imagis_power_on(struct imagis_ts * ts)139 static int imagis_power_on(struct imagis_ts *ts)
140 {
141 	int error;
142 
143 	error = regulator_bulk_enable(ARRAY_SIZE(ts->supplies), ts->supplies);
144 	if (error)
145 		return error;
146 
147 	msleep(IST3038C_CHIP_ON_DELAY_MS);
148 
149 	return 0;
150 }
151 
imagis_start(struct imagis_ts * ts)152 static int imagis_start(struct imagis_ts *ts)
153 {
154 	int error;
155 
156 	error = imagis_power_on(ts);
157 	if (error)
158 		return error;
159 
160 	enable_irq(ts->client->irq);
161 
162 	return 0;
163 }
164 
imagis_stop(struct imagis_ts * ts)165 static int imagis_stop(struct imagis_ts *ts)
166 {
167 	disable_irq(ts->client->irq);
168 
169 	imagis_power_off(ts);
170 
171 	return 0;
172 }
173 
imagis_input_open(struct input_dev * dev)174 static int imagis_input_open(struct input_dev *dev)
175 {
176 	struct imagis_ts *ts = input_get_drvdata(dev);
177 
178 	return imagis_start(ts);
179 }
180 
imagis_input_close(struct input_dev * dev)181 static void imagis_input_close(struct input_dev *dev)
182 {
183 	struct imagis_ts *ts = input_get_drvdata(dev);
184 
185 	imagis_stop(ts);
186 }
187 
imagis_init_input_dev(struct imagis_ts * ts)188 static int imagis_init_input_dev(struct imagis_ts *ts)
189 {
190 	struct input_dev *input_dev;
191 	int error;
192 
193 	input_dev = devm_input_allocate_device(&ts->client->dev);
194 	if (!input_dev)
195 		return -ENOMEM;
196 
197 	ts->input_dev = input_dev;
198 
199 	input_dev->name = "Imagis capacitive touchscreen";
200 	input_dev->phys = "input/ts";
201 	input_dev->id.bustype = BUS_I2C;
202 	input_dev->open = imagis_input_open;
203 	input_dev->close = imagis_input_close;
204 
205 	input_set_drvdata(input_dev, ts);
206 
207 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
208 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
209 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 16, 0, 0);
210 
211 	touchscreen_parse_properties(input_dev, true, &ts->prop);
212 	if (!ts->prop.max_x || !ts->prop.max_y) {
213 		dev_err(&ts->client->dev,
214 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
215 		return -EINVAL;
216 	}
217 
218 	error = input_mt_init_slots(input_dev,
219 				    IST3038C_MAX_FINGER_NUM,
220 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
221 	if (error) {
222 		dev_err(&ts->client->dev,
223 			"Failed to initialize MT slots: %d", error);
224 		return error;
225 	}
226 
227 	error = input_register_device(input_dev);
228 	if (error) {
229 		dev_err(&ts->client->dev,
230 			"Failed to register input device: %d", error);
231 		return error;
232 	}
233 
234 	return 0;
235 }
236 
imagis_init_regulators(struct imagis_ts * ts)237 static int imagis_init_regulators(struct imagis_ts *ts)
238 {
239 	struct i2c_client *client = ts->client;
240 
241 	ts->supplies[0].supply = "vdd";
242 	ts->supplies[1].supply = "vddio";
243 	return devm_regulator_bulk_get(&client->dev,
244 				       ARRAY_SIZE(ts->supplies),
245 				       ts->supplies);
246 }
247 
imagis_probe(struct i2c_client * i2c)248 static int imagis_probe(struct i2c_client *i2c)
249 {
250 	struct device *dev = &i2c->dev;
251 	struct imagis_ts *ts;
252 	int chip_id, error;
253 
254 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
255 	if (!ts)
256 		return -ENOMEM;
257 
258 	ts->client = i2c;
259 
260 	error = imagis_init_regulators(ts);
261 	if (error) {
262 		dev_err(dev, "regulator init error: %d\n", error);
263 		return error;
264 	}
265 
266 	error = imagis_power_on(ts);
267 	if (error) {
268 		dev_err(dev, "failed to enable regulators: %d\n", error);
269 		return error;
270 	}
271 
272 	error = devm_add_action_or_reset(dev, imagis_power_off, ts);
273 	if (error) {
274 		dev_err(dev, "failed to install poweroff action: %d\n", error);
275 		return error;
276 	}
277 
278 	error = imagis_i2c_read_reg(ts,
279 			IST3038C_REG_CHIPID | IST3038C_DIRECT_ACCESS,
280 			&chip_id);
281 	if (error) {
282 		dev_err(dev, "chip ID read failure: %d\n", error);
283 		return error;
284 	}
285 
286 	if (chip_id != IST3038C_WHOAMI) {
287 		dev_err(dev, "unknown chip ID: 0x%x\n", chip_id);
288 		return -EINVAL;
289 	}
290 
291 	error = devm_request_threaded_irq(dev, i2c->irq,
292 					  NULL, imagis_interrupt,
293 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
294 					  "imagis-touchscreen", ts);
295 	if (error) {
296 		dev_err(dev, "IRQ %d allocation failure: %d\n",
297 			i2c->irq, error);
298 		return error;
299 	}
300 
301 	error = imagis_init_input_dev(ts);
302 	if (error)
303 		return error;
304 
305 	return 0;
306 }
307 
imagis_suspend(struct device * dev)308 static int imagis_suspend(struct device *dev)
309 {
310 	struct i2c_client *client = to_i2c_client(dev);
311 	struct imagis_ts *ts = i2c_get_clientdata(client);
312 	int retval = 0;
313 
314 	mutex_lock(&ts->input_dev->mutex);
315 
316 	if (input_device_enabled(ts->input_dev))
317 		retval = imagis_stop(ts);
318 
319 	mutex_unlock(&ts->input_dev->mutex);
320 
321 	return retval;
322 }
323 
imagis_resume(struct device * dev)324 static int imagis_resume(struct device *dev)
325 {
326 	struct i2c_client *client = to_i2c_client(dev);
327 	struct imagis_ts *ts = i2c_get_clientdata(client);
328 	int retval = 0;
329 
330 	mutex_lock(&ts->input_dev->mutex);
331 
332 	if (input_device_enabled(ts->input_dev))
333 		retval = imagis_start(ts);
334 
335 	mutex_unlock(&ts->input_dev->mutex);
336 
337 	return retval;
338 }
339 
340 static DEFINE_SIMPLE_DEV_PM_OPS(imagis_pm_ops, imagis_suspend, imagis_resume);
341 
342 #ifdef CONFIG_OF
343 static const struct of_device_id imagis_of_match[] = {
344 	{ .compatible = "imagis,ist3038c", },
345 	{ },
346 };
347 MODULE_DEVICE_TABLE(of, imagis_of_match);
348 #endif
349 
350 static struct i2c_driver imagis_ts_driver = {
351 	.driver = {
352 		.name = "imagis-touchscreen",
353 		.pm = pm_sleep_ptr(&imagis_pm_ops),
354 		.of_match_table = of_match_ptr(imagis_of_match),
355 	},
356 	.probe = imagis_probe,
357 };
358 
359 module_i2c_driver(imagis_ts_driver);
360 
361 MODULE_DESCRIPTION("Imagis IST3038C Touchscreen Driver");
362 MODULE_AUTHOR("Markuss Broks <markuss.broks@gmail.com>");
363 MODULE_LICENSE("GPL");
364