xref: /linux/drivers/input/touchscreen/s6sy761.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
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 <linux/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 ATTRIBUTE_GROUPS(s6sy761_sysfs);
290 
s6sy761_power_on(struct s6sy761_data * sdata)291 static int s6sy761_power_on(struct s6sy761_data *sdata)
292 {
293 	u8 buffer[S6SY761_EVENT_SIZE];
294 	u8 event;
295 	int ret;
296 
297 	ret = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
298 				    sdata->regulators);
299 	if (ret)
300 		return ret;
301 
302 	msleep(140);
303 
304 	/* double check whether the touch is functional */
305 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
306 					    S6SY761_READ_ONE_EVENT,
307 					    S6SY761_EVENT_SIZE,
308 					    buffer);
309 	if (ret < 0)
310 		return ret;
311 
312 	event = (buffer[0] >> 2) & 0xf;
313 
314 	if ((event != S6SY761_EVENT_INFO &&
315 	     event != S6SY761_EVENT_VENDOR_INFO) ||
316 	    buffer[1] != S6SY761_INFO_BOOT_COMPLETE) {
317 		return -ENODEV;
318 	}
319 
320 	ret = i2c_smbus_read_byte_data(sdata->client, S6SY761_BOOT_STATUS);
321 	if (ret < 0)
322 		return ret;
323 
324 	/* for some reasons the device might be stuck in the bootloader */
325 	if (ret != S6SY761_BS_APPLICATION)
326 		return -ENODEV;
327 
328 	/* enable touch functionality */
329 	ret = i2c_smbus_write_word_data(sdata->client,
330 					S6SY761_TOUCH_FUNCTION,
331 					S6SY761_MASK_TOUCH);
332 	if (ret)
333 		return ret;
334 
335 	return 0;
336 }
337 
s6sy761_hw_init(struct s6sy761_data * sdata,unsigned int * max_x,unsigned int * max_y)338 static int s6sy761_hw_init(struct s6sy761_data *sdata,
339 			   unsigned int *max_x, unsigned int *max_y)
340 {
341 	u8 buffer[S6SY761_PANEL_ID_SIZE]; /* larger read size */
342 	int ret;
343 
344 	ret = s6sy761_power_on(sdata);
345 	if (ret)
346 		return ret;
347 
348 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
349 					    S6SY761_DEVICE_ID,
350 					    S6SY761_DEVID_SIZE,
351 					    buffer);
352 	if (ret < 0)
353 		return ret;
354 
355 	sdata->devid = get_unaligned_be16(buffer + 1);
356 
357 	ret = i2c_smbus_read_i2c_block_data(sdata->client,
358 					    S6SY761_PANEL_INFO,
359 					    S6SY761_PANEL_ID_SIZE,
360 					    buffer);
361 	if (ret < 0)
362 		return ret;
363 
364 	*max_x = get_unaligned_be16(buffer);
365 	*max_y = get_unaligned_be16(buffer + 2);
366 
367 	/* if no tx channels defined, at least keep one */
368 	sdata->tx_channel = max_t(u8, buffer[8], 1);
369 
370 	ret = i2c_smbus_read_byte_data(sdata->client,
371 				       S6SY761_FIRMWARE_INTEGRITY);
372 	if (ret < 0)
373 		return ret;
374 	else if (ret != S6SY761_FW_OK)
375 		return -ENODEV;
376 
377 	return 0;
378 }
379 
s6sy761_power_off(void * data)380 static void s6sy761_power_off(void *data)
381 {
382 	struct s6sy761_data *sdata = data;
383 
384 	disable_irq(sdata->client->irq);
385 	regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
386 						sdata->regulators);
387 }
388 
s6sy761_probe(struct i2c_client * client)389 static int s6sy761_probe(struct i2c_client *client)
390 {
391 	struct s6sy761_data *sdata;
392 	unsigned int max_x, max_y;
393 	int err;
394 
395 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
396 						I2C_FUNC_SMBUS_BYTE_DATA |
397 						I2C_FUNC_SMBUS_I2C_BLOCK))
398 		return -ENODEV;
399 
400 	sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
401 	if (!sdata)
402 		return -ENOMEM;
403 
404 	i2c_set_clientdata(client, sdata);
405 	sdata->client = client;
406 
407 	sdata->regulators[S6SY761_REGULATOR_VDD].supply = "vdd";
408 	sdata->regulators[S6SY761_REGULATOR_AVDD].supply = "avdd";
409 	err = devm_regulator_bulk_get(&client->dev,
410 				      ARRAY_SIZE(sdata->regulators),
411 				      sdata->regulators);
412 	if (err)
413 		return err;
414 
415 	err = devm_add_action_or_reset(&client->dev, s6sy761_power_off, sdata);
416 	if (err)
417 		return err;
418 
419 	err = s6sy761_hw_init(sdata, &max_x, &max_y);
420 	if (err)
421 		return err;
422 
423 	sdata->input = devm_input_allocate_device(&client->dev);
424 	if (!sdata->input)
425 		return -ENOMEM;
426 
427 	sdata->input->name = S6SY761_DEV_NAME;
428 	sdata->input->id.bustype = BUS_I2C;
429 	sdata->input->open = s6sy761_input_open;
430 	sdata->input->close = s6sy761_input_close;
431 
432 	input_set_abs_params(sdata->input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
433 	input_set_abs_params(sdata->input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
434 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
435 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
436 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
437 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
438 	input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
439 
440 	touchscreen_parse_properties(sdata->input, true, &sdata->prop);
441 
442 	if (!input_abs_get_max(sdata->input, ABS_X) ||
443 	    !input_abs_get_max(sdata->input, ABS_Y)) {
444 		dev_warn(&client->dev, "the axis have not been set\n");
445 	}
446 
447 	err = input_mt_init_slots(sdata->input, sdata->tx_channel,
448 				  INPUT_MT_DIRECT);
449 	if (err)
450 		return err;
451 
452 	input_set_drvdata(sdata->input, sdata);
453 
454 	err = input_register_device(sdata->input);
455 	if (err)
456 		return err;
457 
458 	err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
459 					s6sy761_irq_handler,
460 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
461 					"s6sy761_irq", sdata);
462 	if (err)
463 		return err;
464 
465 	pm_runtime_enable(&client->dev);
466 
467 	return 0;
468 }
469 
s6sy761_remove(struct i2c_client * client)470 static void s6sy761_remove(struct i2c_client *client)
471 {
472 	pm_runtime_disable(&client->dev);
473 }
474 
s6sy761_runtime_suspend(struct device * dev)475 static int s6sy761_runtime_suspend(struct device *dev)
476 {
477 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
478 
479 	return i2c_smbus_write_byte_data(sdata->client,
480 				S6SY761_APPLICATION_MODE, S6SY761_APP_SLEEP);
481 }
482 
s6sy761_runtime_resume(struct device * dev)483 static int s6sy761_runtime_resume(struct device *dev)
484 {
485 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
486 
487 	return i2c_smbus_write_byte_data(sdata->client,
488 				S6SY761_APPLICATION_MODE, S6SY761_APP_NORMAL);
489 }
490 
s6sy761_suspend(struct device * dev)491 static int s6sy761_suspend(struct device *dev)
492 {
493 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
494 
495 	s6sy761_power_off(sdata);
496 
497 	return 0;
498 }
499 
s6sy761_resume(struct device * dev)500 static int s6sy761_resume(struct device *dev)
501 {
502 	struct s6sy761_data *sdata = dev_get_drvdata(dev);
503 
504 	enable_irq(sdata->client->irq);
505 
506 	return s6sy761_power_on(sdata);
507 }
508 
509 static const struct dev_pm_ops s6sy761_pm_ops = {
510 	SYSTEM_SLEEP_PM_OPS(s6sy761_suspend, s6sy761_resume)
511 	RUNTIME_PM_OPS(s6sy761_runtime_suspend, s6sy761_runtime_resume, NULL)
512 };
513 
514 #ifdef CONFIG_OF
515 static const struct of_device_id s6sy761_of_match[] = {
516 	{ .compatible = "samsung,s6sy761", },
517 	{ },
518 };
519 MODULE_DEVICE_TABLE(of, s6sy761_of_match);
520 #endif
521 
522 static const struct i2c_device_id s6sy761_id[] = {
523 	{ "s6sy761" },
524 	{ }
525 };
526 MODULE_DEVICE_TABLE(i2c, s6sy761_id);
527 
528 static struct i2c_driver s6sy761_driver = {
529 	.driver = {
530 		.name = S6SY761_DEV_NAME,
531 		.dev_groups = s6sy761_sysfs_groups,
532 		.of_match_table = of_match_ptr(s6sy761_of_match),
533 		.pm = pm_ptr(&s6sy761_pm_ops),
534 	},
535 	.probe = s6sy761_probe,
536 	.remove = s6sy761_remove,
537 	.id_table = s6sy761_id,
538 };
539 
540 module_i2c_driver(s6sy761_driver);
541 
542 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
543 MODULE_DESCRIPTION("Samsung S6SY761 Touch Screen");
544 MODULE_LICENSE("GPL v2");
545