1 // SPDX-License-Identifier: GPL-2.0
2 // Samsung S6SY761 Touchscreen device driver
4 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
5 // Copyright (c) 2017 Andi Shyti <andi@etezian.org>
7 #include <linux/unaligned.h>
8 #include <linux/delay.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>
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
32 #define S6SY761_EVENT_INFO 0x02
33 #define S6SY761_EVENT_VENDOR_INFO 0x07
36 #define S6SY761_INFO_BOOT_COMPLETE 0x00
39 #define S6SY761_FW_OK 0x80
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.
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)
56 /* boot status (BS) */
57 #define S6SY761_BS_BOOT_LOADER 0x10
58 #define S6SY761_BS_APPLICATION 0x20
61 #define S6SY761_EVENT_ID_COORDINATE 0x00
62 #define S6SY761_EVENT_ID_STATUS 0x01
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 */
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
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
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
94 #define S6SY761_DEV_NAME "s6sy761"
96 enum s6sy761_regulators
{
97 S6SY761_REGULATOR_VDD
,
98 S6SY761_REGULATOR_AVDD
,
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
;
107 u8 data
[S6SY761_EVENT_SIZE
* S6SY761_EVENT_COUNT
];
114 * We can't simply use i2c_smbus_read_i2c_block_data because we
115 * need to read more than 255 bytes
117 static int s6sy761_read_events(struct s6sy761_data
*sdata
, u16 n_events
)
119 u8 cmd
= S6SY761_READ_ALL_EVENT
;
120 struct i2c_msg msgs
[2] = {
122 .addr
= sdata
->client
->addr
,
127 .addr
= sdata
->client
->addr
,
129 .len
= (n_events
* S6SY761_EVENT_SIZE
),
130 .buf
= sdata
->data
+ S6SY761_EVENT_SIZE
,
135 ret
= i2c_transfer(sdata
->client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
139 return ret
== ARRAY_SIZE(msgs
) ? 0 : -EIO
;
142 static void s6sy761_report_coordinates(struct s6sy761_data
*sdata
,
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
);
151 input_mt_slot(sdata
->input
, tid
);
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
);
160 input_sync(sdata
->input
);
163 static void s6sy761_report_release(struct s6sy761_data
*sdata
,
166 input_mt_slot(sdata
->input
, tid
);
167 input_mt_report_slot_state(sdata
->input
, MT_TOOL_FINGER
, false);
169 input_sync(sdata
->input
);
172 static void s6sy761_handle_coordinates(struct s6sy761_data
*sdata
, u8
*event
)
177 if (unlikely(!(event
[0] & S6SY761_MASK_TID
)))
180 tid
= ((event
[0] & S6SY761_MASK_TID
) >> 2) - 1;
181 touch_state
= (event
[0] & S6SY761_MASK_TOUCH_STATE
) >> 6;
183 switch (touch_state
) {
185 case S6SY761_TS_NONE
:
187 case S6SY761_TS_RELEASE
:
188 s6sy761_report_release(sdata
, event
, tid
);
190 case S6SY761_TS_PRESS
:
191 case S6SY761_TS_MOVE
:
192 s6sy761_report_coordinates(sdata
, event
, tid
);
197 static void s6sy761_handle_events(struct s6sy761_data
*sdata
, u8 n_events
)
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
;
210 case S6SY761_EVENT_ID_COORDINATE
:
211 s6sy761_handle_coordinates(sdata
, event
);
214 case S6SY761_EVENT_ID_STATUS
:
223 static irqreturn_t
s6sy761_irq_handler(int irq
, void *dev
)
225 struct s6sy761_data
*sdata
= dev
;
229 ret
= i2c_smbus_read_i2c_block_data(sdata
->client
,
230 S6SY761_READ_ONE_EVENT
,
234 dev_err(&sdata
->client
->dev
, "failed to read events\n");
241 n_events
= sdata
->data
[7] & S6SY761_MASK_LEFT_EVENTS
;
242 if (unlikely(n_events
> S6SY761_EVENT_COUNT
- 1))
246 ret
= s6sy761_read_events(sdata
, n_events
);
248 dev_err(&sdata
->client
->dev
, "failed to read events\n");
253 s6sy761_handle_events(sdata
, n_events
+ 1);
258 static int s6sy761_input_open(struct input_dev
*dev
)
260 struct s6sy761_data
*sdata
= input_get_drvdata(dev
);
262 return i2c_smbus_write_byte(sdata
->client
, S6SY761_SENSE_ON
);
265 static void s6sy761_input_close(struct input_dev
*dev
)
267 struct s6sy761_data
*sdata
= input_get_drvdata(dev
);
270 ret
= i2c_smbus_write_byte(sdata
->client
, S6SY761_SENSE_OFF
);
272 dev_err(&sdata
->client
->dev
, "failed to turn off sensing\n");
275 static ssize_t
s6sy761_sysfs_devid(struct device
*dev
,
276 struct device_attribute
*attr
, char *buf
)
278 struct s6sy761_data
*sdata
= dev_get_drvdata(dev
);
280 return sprintf(buf
, "%#x\n", sdata
->devid
);
283 static DEVICE_ATTR(devid
, 0444, s6sy761_sysfs_devid
, NULL
);
285 static struct attribute
*s6sy761_sysfs_attrs
[] = {
286 &dev_attr_devid
.attr
,
289 ATTRIBUTE_GROUPS(s6sy761_sysfs
);
291 static int s6sy761_power_on(struct s6sy761_data
*sdata
)
293 u8 buffer
[S6SY761_EVENT_SIZE
];
297 ret
= regulator_bulk_enable(ARRAY_SIZE(sdata
->regulators
),
304 /* double check whether the touch is functional */
305 ret
= i2c_smbus_read_i2c_block_data(sdata
->client
,
306 S6SY761_READ_ONE_EVENT
,
312 event
= (buffer
[0] >> 2) & 0xf;
314 if ((event
!= S6SY761_EVENT_INFO
&&
315 event
!= S6SY761_EVENT_VENDOR_INFO
) ||
316 buffer
[1] != S6SY761_INFO_BOOT_COMPLETE
) {
320 ret
= i2c_smbus_read_byte_data(sdata
->client
, S6SY761_BOOT_STATUS
);
324 /* for some reasons the device might be stuck in the bootloader */
325 if (ret
!= S6SY761_BS_APPLICATION
)
328 /* enable touch functionality */
329 ret
= i2c_smbus_write_word_data(sdata
->client
,
330 S6SY761_TOUCH_FUNCTION
,
338 static int s6sy761_hw_init(struct s6sy761_data
*sdata
,
339 unsigned int *max_x
, unsigned int *max_y
)
341 u8 buffer
[S6SY761_PANEL_ID_SIZE
]; /* larger read size */
344 ret
= s6sy761_power_on(sdata
);
348 ret
= i2c_smbus_read_i2c_block_data(sdata
->client
,
355 sdata
->devid
= get_unaligned_be16(buffer
+ 1);
357 ret
= i2c_smbus_read_i2c_block_data(sdata
->client
,
359 S6SY761_PANEL_ID_SIZE
,
364 *max_x
= get_unaligned_be16(buffer
);
365 *max_y
= get_unaligned_be16(buffer
+ 2);
367 /* if no tx channels defined, at least keep one */
368 sdata
->tx_channel
= max_t(u8
, buffer
[8], 1);
370 ret
= i2c_smbus_read_byte_data(sdata
->client
,
371 S6SY761_FIRMWARE_INTEGRITY
);
374 else if (ret
!= S6SY761_FW_OK
)
380 static void s6sy761_power_off(void *data
)
382 struct s6sy761_data
*sdata
= data
;
384 disable_irq(sdata
->client
->irq
);
385 regulator_bulk_disable(ARRAY_SIZE(sdata
->regulators
),
389 static int s6sy761_probe(struct i2c_client
*client
)
391 struct s6sy761_data
*sdata
;
392 unsigned int max_x
, max_y
;
395 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
396 I2C_FUNC_SMBUS_BYTE_DATA
|
397 I2C_FUNC_SMBUS_I2C_BLOCK
))
400 sdata
= devm_kzalloc(&client
->dev
, sizeof(*sdata
), GFP_KERNEL
);
404 i2c_set_clientdata(client
, sdata
);
405 sdata
->client
= client
;
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
),
415 err
= devm_add_action_or_reset(&client
->dev
, s6sy761_power_off
, sdata
);
419 err
= s6sy761_hw_init(sdata
, &max_x
, &max_y
);
423 sdata
->input
= devm_input_allocate_device(&client
->dev
);
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
;
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);
440 touchscreen_parse_properties(sdata
->input
, true, &sdata
->prop
);
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");
447 err
= input_mt_init_slots(sdata
->input
, sdata
->tx_channel
,
452 input_set_drvdata(sdata
->input
, sdata
);
454 err
= input_register_device(sdata
->input
);
458 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
460 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
461 "s6sy761_irq", sdata
);
465 pm_runtime_enable(&client
->dev
);
470 static void s6sy761_remove(struct i2c_client
*client
)
472 pm_runtime_disable(&client
->dev
);
475 static int s6sy761_runtime_suspend(struct device
*dev
)
477 struct s6sy761_data
*sdata
= dev_get_drvdata(dev
);
479 return i2c_smbus_write_byte_data(sdata
->client
,
480 S6SY761_APPLICATION_MODE
, S6SY761_APP_SLEEP
);
483 static int s6sy761_runtime_resume(struct device
*dev
)
485 struct s6sy761_data
*sdata
= dev_get_drvdata(dev
);
487 return i2c_smbus_write_byte_data(sdata
->client
,
488 S6SY761_APPLICATION_MODE
, S6SY761_APP_NORMAL
);
491 static int s6sy761_suspend(struct device
*dev
)
493 struct s6sy761_data
*sdata
= dev_get_drvdata(dev
);
495 s6sy761_power_off(sdata
);
500 static int s6sy761_resume(struct device
*dev
)
502 struct s6sy761_data
*sdata
= dev_get_drvdata(dev
);
504 enable_irq(sdata
->client
->irq
);
506 return s6sy761_power_on(sdata
);
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
)
515 static const struct of_device_id s6sy761_of_match
[] = {
516 { .compatible
= "samsung,s6sy761", },
519 MODULE_DEVICE_TABLE(of
, s6sy761_of_match
);
522 static const struct i2c_device_id s6sy761_id
[] = {
526 MODULE_DEVICE_TABLE(i2c
, s6sy761_id
);
528 static struct i2c_driver s6sy761_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
),
535 .probe
= s6sy761_probe
,
536 .remove
= s6sy761_remove
,
537 .id_table
= s6sy761_id
,
540 module_i2c_driver(s6sy761_driver
);
542 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
543 MODULE_DESCRIPTION("Samsung S6SY761 Touch Screen");
544 MODULE_LICENSE("GPL v2");