1 // SPDX-License-Identifier: GPL-2.0
2 // Samsung S6SY761 Touchscreen device driver
3 //
4 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
5 // Copyright (c) 2017 Andi Shyti <andi@etezian.org>
6 
7 #include <asm/unaligned.h>
8 #include <linux/delay.h>
9 #include <linux/i2c.h>
10 #include <linux/input/mt.h>
11 #include <linux/input/touchscreen.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regulator/consumer.h>
17 
18 /* commands */
19 #define S6SY761_SENSE_ON		0x10
20 #define S6SY761_SENSE_OFF		0x11
21 #define S6SY761_TOUCH_FUNCTION		0x30 /* R/W for get/set */
22 #define S6SY761_FIRMWARE_INTEGRITY	0x21
23 #define S6SY761_PANEL_INFO		0x23
24 #define S6SY761_DEVICE_ID		0x52
25 #define S6SY761_BOOT_STATUS		0x55
26 #define S6SY761_READ_ONE_EVENT		0x60
27 #define S6SY761_READ_ALL_EVENT		0x61
28 #define S6SY761_CLEAR_EVENT_STACK	0x62
29 #define S6SY761_APPLICATION_MODE	0xe4
30 
31 /* events */
32 #define S6SY761_EVENT_INFO		0x02
33 #define S6SY761_EVENT_VENDOR_INFO	0x07
34 
35 /* info */
36 #define S6SY761_INFO_BOOT_COMPLETE	0x00
37 
38 /* firmware status */
39 #define S6SY761_FW_OK			0x80
40 
41 /*
42  * the functionalities are put as a reference
43  * as in the device I am using none of them
44  * works therefore not used in this driver yet.
45  */
46 /* touchscreen functionalities */
47 #define S6SY761_MASK_TOUCH		BIT(0)
48 #define S6SY761_MASK_HOVER		BIT(1)
49 #define S6SY761_MASK_COVER		BIT(2)
50 #define S6SY761_MASK_GLOVE		BIT(3)
51 #define S6SY761_MASK_STYLUS		BIT(4)
52 #define S6SY761_MASK_PALM		BIT(5)
53 #define S6SY761_MASK_WET		BIT(6)
54 #define S6SY761_MASK_PROXIMITY		BIT(7)
55 
56 /* boot status (BS) */
57 #define S6SY761_BS_BOOT_LOADER		0x10
58 #define S6SY761_BS_APPLICATION		0x20
59 
60 /* event id */
61 #define S6SY761_EVENT_ID_COORDINATE	0x00
62 #define S6SY761_EVENT_ID_STATUS		0x01
63 
64 /* event register masks */
65 #define S6SY761_MASK_TOUCH_STATE	0xc0 /* byte 0 */
66 #define S6SY761_MASK_TID		0x3c
67 #define S6SY761_MASK_EID		0x03
68 #define S6SY761_MASK_X			0xf0 /* byte 3 */
69 #define S6SY761_MASK_Y			0x0f
70 #define S6SY761_MASK_Z			0x3f /* byte 6 */
71 #define S6SY761_MASK_LEFT_EVENTS	0x3f /* byte 7 */
72 #define S6SY761_MASK_TOUCH_TYPE		0xc0 /* MSB in byte 6, LSB in byte 7 */
73 
74 /* event touch state values */
75 #define S6SY761_TS_NONE			0x00
76 #define S6SY761_TS_PRESS		0x01
77 #define S6SY761_TS_MOVE			0x02
78 #define S6SY761_TS_RELEASE		0x03
79 
80 /* application modes */
81 #define S6SY761_APP_NORMAL		0x0
82 #define S6SY761_APP_LOW_POWER		0x1
83 #define S6SY761_APP_TEST		0x2
84 #define S6SY761_APP_FLASH		0x3
85 #define S6SY761_APP_SLEEP		0x4
86 
87 #define S6SY761_EVENT_SIZE		8
88 #define S6SY761_EVENT_COUNT		32
89 #define S6SY761_DEVID_SIZE		3
90 #define S6SY761_PANEL_ID_SIZE		11
91 #define S6SY761_TS_STATUS_SIZE		5
92 #define S6SY761_MAX_FINGERS		10
93 
94 #define S6SY761_DEV_NAME	"s6sy761"
95 
96 enum s6sy761_regulators {
97 	S6SY761_REGULATOR_VDD,
98 	S6SY761_REGULATOR_AVDD,
99 };
100 
101 struct s6sy761_data {
102 	struct i2c_client *client;
103 	struct regulator_bulk_data regulators[2];
104 	struct input_dev *input;
105 	struct touchscreen_properties prop;
106 
107 	u8 data[S6SY761_EVENT_SIZE * S6SY761_EVENT_COUNT];
108 
109 	u16 devid;
110 	u8 tx_channel;
111 };
112 
113 /*
114  * We can't simply use i2c_smbus_read_i2c_block_data because we
115  * need to read more than 255 bytes
116  */
s6sy761_read_events(struct s6sy761_data * sdata,u16 n_events)117 static int s6sy761_read_events(struct s6sy761_data *sdata, u16 n_events)
118 {
119 	u8 cmd = S6SY761_READ_ALL_EVENT;
120 	struct i2c_msg msgs[2] = {
121 		{
122 			.addr	= sdata->client->addr,
123 			.len	= 1,
124 			.buf	= &cmd,
125 		},
126 		{
127 			.addr	= sdata->client->addr,
128 			.flags	= I2C_M_RD,
129 			.len	= (n_events * S6SY761_EVENT_SIZE),
130 			.buf	= sdata->data + S6SY761_EVENT_SIZE,
131 		},
132 	};
133 	int ret;
134 
135 	ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs));
136 	if (ret < 0)
137 		return ret;
138 
139 	return ret == ARRAY_SIZE(msgs) ? 0 : -EIO;
140 }
141 
s6sy761_report_coordinates(struct s6sy761_data * sdata,u8 * event,u8 tid)142 static void s6sy761_report_coordinates(struct s6sy761_data *sdata,
143 				       u8 *event, u8 tid)
144 {
145 	u8 major = event[4];
146 	u8 minor = event[5];
147 	u8 z = event[6] & S6SY761_MASK_Z;
148 	u16 x = (event[1] << 4) | ((event[3] & S6SY761_MASK_X) >> 4);
149 	u16 y = (event[2] << 4) | (event[3] & S6SY761_MASK_Y);
150 
151 	input_mt_slot(sdata->input, tid);
152 
153 	input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true);
154 	input_report_abs(sdata->input, ABS_MT_POSITION_X, x);
155 	input_report_abs(sdata->input, ABS_MT_POSITION_Y, y);
156 	input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, major);
157 	input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, minor);
158 	input_report_abs(sdata->input, ABS_MT_PRESSURE, z);
159 
160 	input_sync(sdata->input);
161 }
162 
s6sy761_report_release(struct s6sy761_data * sdata,u8 * event,u8 tid)163 static void s6sy761_report_release(struct s6sy761_data *sdata,
164 				   u8 *event, u8 tid)
165 {
166 	input_mt_slot(sdata->input, tid);
167 	input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, false);
168 
169 	input_sync(sdata->input);
170 }
171 
s6sy761_handle_coordinates(struct s6sy761_data * sdata,u8 * event)172 static void s6sy761_handle_coordinates(struct s6sy761_data *sdata, u8 *event)
173 {
174 	u8 tid;
175 	u8 touch_state;
176 
177 	if (unlikely(!(event[0] & S6SY761_MASK_TID)))
178 		return;
179 
180 	tid = ((event[0] & S6SY761_MASK_TID) >> 2) - 1;
181 	touch_state = (event[0] & S6SY761_MASK_TOUCH_STATE) >> 6;
182 
183 	switch (touch_state) {
184 
185 	case S6SY761_TS_NONE:
186 		break;
187 	case S6SY761_TS_RELEASE:
188 		s6sy761_report_release(sdata, event, tid);
189 		break;
190 	case S6SY761_TS_PRESS:
191 	case S6SY761_TS_MOVE:
192 		s6sy761_report_coordinates(sdata, event, tid);
193 		break;
194 	}
195 }
196 
s6sy761_handle_events(struct s6sy761_data * sdata,u8 n_events)197 static void s6sy761_handle_events(struct s6sy761_data *sdata, u8 n_events)
198 {
199 	int i;
200 
201 	for (i = 0; i < n_events; i++) {
202 		u8 *event = &sdata->data[i * S6SY761_EVENT_SIZE];
203 		u8 event_id = event[0] & S6SY761_MASK_EID;
204 
205 		if (!event[0])
206 			return;
207 
208 		switch (event_id) {
209 
210 		case S6SY761_EVENT_ID_COORDINATE:
211 			s6sy761_handle_coordinates(sdata, event);
212 			break;
213 
214 		case S6SY761_EVENT_ID_STATUS:
215 			break;
216 
217 		default:
218 			break;
219 		}
220 	}
221 }
222 
s6sy761_irq_handler(int irq,void * dev)223 static irqreturn_t s6sy761_irq_handler(int irq, void *dev)
224 {
225 	struct s6sy761_data *sdata = dev;
226 	int ret;
227 	u8 n_events;
228 
229 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
230 					    S6SY761_READ_ONE_EVENT,
231 					    S6SY761_EVENT_SIZE,
232 					    sdata->data);
233 	if (ret < 0) {
234 		dev_err(&sdata->client->dev, "failed to read events\n");
235 		return IRQ_HANDLED;
236 	}
237 
238 	if (!sdata->data[0])
239 		return IRQ_HANDLED;
240 
241 	n_events = sdata->data[7] & S6SY761_MASK_LEFT_EVENTS;
242 	if (unlikely(n_events > S6SY761_EVENT_COUNT - 1))
243 		return IRQ_HANDLED;
244 
245 	if (n_events) {
246 		ret = s6sy761_read_events(sdata, n_events);
247 		if (ret < 0) {
248 			dev_err(&sdata->client->dev, "failed to read events\n");
249 			return IRQ_HANDLED;
250 		}
251 	}
252 
253 	s6sy761_handle_events(sdata, n_events +  1);
254 
255 	return IRQ_HANDLED;
256 }
257 
s6sy761_input_open(struct input_dev * dev)258 static int s6sy761_input_open(struct input_dev *dev)
259 {
260 	struct s6sy761_data *sdata = input_get_drvdata(dev);
261 
262 	return i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_ON);
263 }
264 
s6sy761_input_close(struct input_dev * dev)265 static void s6sy761_input_close(struct input_dev *dev)
266 {
267 	struct s6sy761_data *sdata = input_get_drvdata(dev);
268 	int ret;
269 
270 	ret = i2c_smbus_write_byte(sdata->client, S6SY761_SENSE_OFF);
271 	if (ret)
272 		dev_err(&sdata->client->dev, "failed to turn off sensing\n");
273 }
274 
s6sy761_sysfs_devid(struct device * dev,struct device_attribute * attr,char * buf)275 static ssize_t s6sy761_sysfs_devid(struct device *dev,
276 				struct device_attribute *attr, char *buf)
277 {
278 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
279 
280 	return sprintf(buf, "%#x\n", sdata->devid);
281 }
282 
283 static DEVICE_ATTR(devid, 0444, s6sy761_sysfs_devid, NULL);
284 
285 static struct attribute *s6sy761_sysfs_attrs[] = {
286 	&dev_attr_devid.attr,
287 	NULL
288 };
289 
290 static struct attribute_group s6sy761_attribute_group = {
291 	.attrs = s6sy761_sysfs_attrs
292 };
293 
s6sy761_power_on(struct s6sy761_data * sdata)294 static int s6sy761_power_on(struct s6sy761_data *sdata)
295 {
296 	u8 buffer[S6SY761_EVENT_SIZE];
297 	u8 event;
298 	int ret;
299 
300 	ret = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
301 				    sdata->regulators);
302 	if (ret)
303 		return ret;
304 
305 	msleep(140);
306 
307 	/* double check whether the touch is functional */
308 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
309 					    S6SY761_READ_ONE_EVENT,
310 					    S6SY761_EVENT_SIZE,
311 					    buffer);
312 	if (ret < 0)
313 		return ret;
314 
315 	event = (buffer[0] >> 2) & 0xf;
316 
317 	if ((event != S6SY761_EVENT_INFO &&
318 	     event != S6SY761_EVENT_VENDOR_INFO) ||
319 	    buffer[1] != S6SY761_INFO_BOOT_COMPLETE) {
320 		return -ENODEV;
321 	}
322 
323 	ret = i2c_smbus_read_byte_data(sdata->client, S6SY761_BOOT_STATUS);
324 	if (ret < 0)
325 		return ret;
326 
327 	/* for some reasons the device might be stuck in the bootloader */
328 	if (ret != S6SY761_BS_APPLICATION)
329 		return -ENODEV;
330 
331 	/* enable touch functionality */
332 	ret = i2c_smbus_write_word_data(sdata->client,
333 					S6SY761_TOUCH_FUNCTION,
334 					S6SY761_MASK_TOUCH);
335 	if (ret)
336 		return ret;
337 
338 	return 0;
339 }
340 
s6sy761_hw_init(struct s6sy761_data * sdata,unsigned int * max_x,unsigned int * max_y)341 static int s6sy761_hw_init(struct s6sy761_data *sdata,
342 			   unsigned int *max_x, unsigned int *max_y)
343 {
344 	u8 buffer[S6SY761_PANEL_ID_SIZE]; /* larger read size */
345 	int ret;
346 
347 	ret = s6sy761_power_on(sdata);
348 	if (ret)
349 		return ret;
350 
351 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
352 					    S6SY761_DEVICE_ID,
353 					    S6SY761_DEVID_SIZE,
354 					    buffer);
355 	if (ret < 0)
356 		return ret;
357 
358 	sdata->devid = get_unaligned_be16(buffer + 1);
359 
360 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
361 					    S6SY761_PANEL_INFO,
362 					    S6SY761_PANEL_ID_SIZE,
363 					    buffer);
364 	if (ret < 0)
365 		return ret;
366 
367 	*max_x = get_unaligned_be16(buffer);
368 	*max_y = get_unaligned_be16(buffer + 2);
369 
370 	/* if no tx channels defined, at least keep one */
371 	sdata->tx_channel = max_t(u8, buffer[8], 1);
372 
373 	ret = i2c_smbus_read_byte_data(sdata->client,
374 				       S6SY761_FIRMWARE_INTEGRITY);
375 	if (ret < 0)
376 		return ret;
377 	else if (ret != S6SY761_FW_OK)
378 		return -ENODEV;
379 
380 	return 0;
381 }
382 
s6sy761_power_off(void * data)383 static void s6sy761_power_off(void *data)
384 {
385 	struct s6sy761_data *sdata = data;
386 
387 	disable_irq(sdata->client->irq);
388 	regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
389 						sdata->regulators);
390 }
391 
s6sy761_probe(struct i2c_client * client)392 static int s6sy761_probe(struct i2c_client *client)
393 {
394 	struct s6sy761_data *sdata;
395 	unsigned int max_x, max_y;
396 	int err;
397 
398 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
399 						I2C_FUNC_SMBUS_BYTE_DATA |
400 						I2C_FUNC_SMBUS_I2C_BLOCK))
401 		return -ENODEV;
402 
403 	sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
404 	if (!sdata)
405 		return -ENOMEM;
406 
407 	i2c_set_clientdata(client, sdata);
408 	sdata->client = client;
409 
410 	sdata->regulators[S6SY761_REGULATOR_VDD].supply = "vdd";
411 	sdata->regulators[S6SY761_REGULATOR_AVDD].supply = "avdd";
412 	err = devm_regulator_bulk_get(&client->dev,
413 				      ARRAY_SIZE(sdata->regulators),
414 				      sdata->regulators);
415 	if (err)
416 		return err;
417 
418 	err = devm_add_action_or_reset(&client->dev, s6sy761_power_off, sdata);
419 	if (err)
420 		return err;
421 
422 	err = s6sy761_hw_init(sdata, &max_x, &max_y);
423 	if (err)
424 		return err;
425 
426 	sdata->input = devm_input_allocate_device(&client->dev);
427 	if (!sdata->input)
428 		return -ENOMEM;
429 
430 	sdata->input->name = S6SY761_DEV_NAME;
431 	sdata->input->id.bustype = BUS_I2C;
432 	sdata->input->open = s6sy761_input_open;
433 	sdata->input->close = s6sy761_input_close;
434 
435 	input_set_abs_params(sdata->input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
436 	input_set_abs_params(sdata->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
437 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
438 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
439 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
440 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
441 	input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
442 
443 	touchscreen_parse_properties(sdata->input, true, &sdata->prop);
444 
445 	if (!input_abs_get_max(sdata->input, ABS_X) ||
446 	    !input_abs_get_max(sdata->input, ABS_Y)) {
447 		dev_warn(&client->dev, "the axis have not been set\n");
448 	}
449 
450 	err = input_mt_init_slots(sdata->input, sdata->tx_channel,
451 				  INPUT_MT_DIRECT);
452 	if (err)
453 		return err;
454 
455 	input_set_drvdata(sdata->input, sdata);
456 
457 	err = input_register_device(sdata->input);
458 	if (err)
459 		return err;
460 
461 	err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
462 					s6sy761_irq_handler,
463 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
464 					"s6sy761_irq", sdata);
465 	if (err)
466 		return err;
467 
468 	err = devm_device_add_group(&client->dev, &s6sy761_attribute_group);
469 	if (err)
470 		return err;
471 
472 	pm_runtime_enable(&client->dev);
473 
474 	return 0;
475 }
476 
s6sy761_remove(struct i2c_client * client)477 static void s6sy761_remove(struct i2c_client *client)
478 {
479 	pm_runtime_disable(&client->dev);
480 }
481 
s6sy761_runtime_suspend(struct device * dev)482 static int s6sy761_runtime_suspend(struct device *dev)
483 {
484 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
485 
486 	return i2c_smbus_write_byte_data(sdata->client,
487 				S6SY761_APPLICATION_MODE, S6SY761_APP_SLEEP);
488 }
489 
s6sy761_runtime_resume(struct device * dev)490 static int s6sy761_runtime_resume(struct device *dev)
491 {
492 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
493 
494 	return i2c_smbus_write_byte_data(sdata->client,
495 				S6SY761_APPLICATION_MODE, S6SY761_APP_NORMAL);
496 }
497 
s6sy761_suspend(struct device * dev)498 static int s6sy761_suspend(struct device *dev)
499 {
500 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
501 
502 	s6sy761_power_off(sdata);
503 
504 	return 0;
505 }
506 
s6sy761_resume(struct device * dev)507 static int s6sy761_resume(struct device *dev)
508 {
509 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
510 
511 	enable_irq(sdata->client->irq);
512 
513 	return s6sy761_power_on(sdata);
514 }
515 
516 static const struct dev_pm_ops s6sy761_pm_ops = {
517 	SYSTEM_SLEEP_PM_OPS(s6sy761_suspend, s6sy761_resume)
518 	RUNTIME_PM_OPS(s6sy761_runtime_suspend, s6sy761_runtime_resume, NULL)
519 };
520 
521 #ifdef CONFIG_OF
522 static const struct of_device_id s6sy761_of_match[] = {
523 	{ .compatible = "samsung,s6sy761", },
524 	{ },
525 };
526 MODULE_DEVICE_TABLE(of, s6sy761_of_match);
527 #endif
528 
529 static const struct i2c_device_id s6sy761_id[] = {
530 	{ "s6sy761", 0 },
531 	{ },
532 };
533 MODULE_DEVICE_TABLE(i2c, s6sy761_id);
534 
535 static struct i2c_driver s6sy761_driver = {
536 	.driver = {
537 		.name = S6SY761_DEV_NAME,
538 		.of_match_table = of_match_ptr(s6sy761_of_match),
539 		.pm = pm_ptr(&s6sy761_pm_ops),
540 	},
541 	.probe = s6sy761_probe,
542 	.remove = s6sy761_remove,
543 	.id_table = s6sy761_id,
544 };
545 
546 module_i2c_driver(s6sy761_driver);
547 
548 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
549 MODULE_DESCRIPTION("Samsung S6SY761 Touch Screen");
550 MODULE_LICENSE("GPL v2");
551