2 * Touch Screen driver for SiS 9200 family I2C Touch panels
4 * Copyright (C) 2015 SiS, Inc.
5 * Copyright (C) 2016 Nextfour Group
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/crc-itu-t.h>
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/interrupt.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <asm/unaligned.h>
28 #define SIS_I2C_NAME "sis_i2c_ts"
31 * The I2C packet format:
34 * <contact data - variable length>
35 * u8 Number of contacts
36 * le16 Scan Time (optional)
39 * One touch point information consists of 6+ bytes, the order is:
44 * u8 contact width (optional)
45 * u8 contact height (optional)
46 * u8 pressure (optional)
48 * Maximum amount of data transmitted in one shot is 64 bytes, if controller
49 * needs to report more contacts than fit in one packet it will send true
50 * number of contacts in first packet and 0 as number of contacts in second
54 #define SIS_MAX_PACKET_SIZE 64
56 #define SIS_PKT_LEN_OFFSET 0
57 #define SIS_PKT_REPORT_OFFSET 2 /* Report ID/type */
58 #define SIS_PKT_CONTACT_OFFSET 3 /* First contact */
60 #define SIS_SCAN_TIME_LEN 2
62 /* Supported report types */
63 #define SIS_ALL_IN_ONE_PACKAGE 0x10
64 #define SIS_PKT_IS_TOUCH(x) (((x) & 0x0f) == 0x01)
65 #define SIS_PKT_IS_HIDI2C(x) (((x) & 0x0f) == 0x06)
67 /* Contact properties within report */
68 #define SIS_PKT_HAS_AREA(x) ((x) & BIT(4))
69 #define SIS_PKT_HAS_PRESSURE(x) ((x) & BIT(5))
70 #define SIS_PKT_HAS_SCANTIME(x) ((x) & BIT(6))
73 #define SIS_BASE_LEN_PER_CONTACT 6
74 #define SIS_AREA_LEN_PER_CONTACT 2
75 #define SIS_PRESSURE_LEN_PER_CONTACT 1
77 /* Offsets within contact data */
78 #define SIS_CONTACT_STATUS_OFFSET 0
79 #define SIS_CONTACT_ID_OFFSET 1 /* Contact ID */
80 #define SIS_CONTACT_X_OFFSET 2
81 #define SIS_CONTACT_Y_OFFSET 4
82 #define SIS_CONTACT_WIDTH_OFFSET 6
83 #define SIS_CONTACT_HEIGHT_OFFSET 7
84 #define SIS_CONTACT_PRESSURE_OFFSET(id) (SIS_PKT_HAS_AREA(id) ? 8 : 6)
86 /* Individual contact state */
87 #define SIS_STATUS_UP 0x0
88 #define SIS_STATUS_DOWN 0x3
90 /* Touchscreen parameters */
91 #define SIS_MAX_FINGERS 10
92 #define SIS_MAX_X 4095
93 #define SIS_MAX_Y 4095
94 #define SIS_MAX_PRESSURE 255
96 /* Resolution diagonal */
97 #define SIS_AREA_LENGTH_LONGER 5792
98 /*((SIS_MAX_X^2) + (SIS_MAX_Y^2))^0.5*/
99 #define SIS_AREA_LENGTH_SHORT 5792
100 #define SIS_AREA_UNIT (5792 / 32)
103 struct i2c_client
*client
;
104 struct input_dev
*input
;
106 struct gpio_desc
*attn_gpio
;
107 struct gpio_desc
*reset_gpio
;
109 u8 packet
[SIS_MAX_PACKET_SIZE
];
112 static int sis_read_packet(struct i2c_client
*client
, u8
*buf
,
113 unsigned int *num_contacts
,
114 unsigned int *contact_size
)
122 ret
= i2c_master_recv(client
, buf
, SIS_MAX_PACKET_SIZE
);
126 len
= get_unaligned_le16(&buf
[SIS_PKT_LEN_OFFSET
]);
127 if (len
> SIS_MAX_PACKET_SIZE
) {
128 dev_err(&client
->dev
,
129 "%s: invalid packet length (%d vs %d)\n",
130 __func__
, len
, SIS_MAX_PACKET_SIZE
);
137 report_id
= buf
[SIS_PKT_REPORT_OFFSET
];
139 *contact_size
= SIS_BASE_LEN_PER_CONTACT
;
141 if (report_id
!= SIS_ALL_IN_ONE_PACKAGE
) {
142 if (SIS_PKT_IS_TOUCH(report_id
)) {
144 * Calculate CRC ignoring packet length
145 * in the beginning and CRC transmitted
146 * at the end of the packet.
148 crc
= crc_itu_t(0, buf
+ 2, len
- 2 - 2);
149 pkg_crc
= get_unaligned_le16(&buf
[len
- 2]);
151 if (crc
!= pkg_crc
) {
152 dev_err(&client
->dev
,
153 "%s: CRC Error (%d vs %d)\n",
154 __func__
, crc
, pkg_crc
);
160 } else if (!SIS_PKT_IS_HIDI2C(report_id
)) {
161 dev_err(&client
->dev
,
162 "%s: invalid packet ID %#02x\n",
163 __func__
, report_id
);
167 if (SIS_PKT_HAS_SCANTIME(report_id
))
168 count_idx
-= SIS_SCAN_TIME_LEN
;
170 if (SIS_PKT_HAS_AREA(report_id
))
171 *contact_size
+= SIS_AREA_LEN_PER_CONTACT
;
172 if (SIS_PKT_HAS_PRESSURE(report_id
))
173 *contact_size
+= SIS_PRESSURE_LEN_PER_CONTACT
;
176 *num_contacts
= buf
[count_idx
];
180 static int sis_ts_report_contact(struct sis_ts_data
*ts
, const u8
*data
, u8 id
)
182 struct input_dev
*input
= ts
->input
;
184 u8 status
= data
[SIS_CONTACT_STATUS_OFFSET
];
189 if (status
!= SIS_STATUS_DOWN
&& status
!= SIS_STATUS_UP
) {
190 dev_err(&ts
->client
->dev
, "Unexpected touch status: %#02x\n",
191 data
[SIS_CONTACT_STATUS_OFFSET
]);
195 slot
= input_mt_get_slot_by_key(input
, data
[SIS_CONTACT_ID_OFFSET
]);
199 input_mt_slot(input
, slot
);
200 input_mt_report_slot_state(input
, MT_TOOL_FINGER
,
201 status
== SIS_STATUS_DOWN
);
203 if (status
== SIS_STATUS_DOWN
) {
204 pressure
= height
= width
= 1;
205 if (id
!= SIS_ALL_IN_ONE_PACKAGE
) {
206 if (SIS_PKT_HAS_AREA(id
)) {
207 width
= data
[SIS_CONTACT_WIDTH_OFFSET
];
208 height
= data
[SIS_CONTACT_HEIGHT_OFFSET
];
211 if (SIS_PKT_HAS_PRESSURE(id
))
213 data
[SIS_CONTACT_PRESSURE_OFFSET(id
)];
216 x
= get_unaligned_le16(&data
[SIS_CONTACT_X_OFFSET
]);
217 y
= get_unaligned_le16(&data
[SIS_CONTACT_Y_OFFSET
]);
219 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
,
220 width
* SIS_AREA_UNIT
);
221 input_report_abs(input
, ABS_MT_TOUCH_MINOR
,
222 height
* SIS_AREA_UNIT
);
223 input_report_abs(input
, ABS_MT_PRESSURE
, pressure
);
224 input_report_abs(input
, ABS_MT_POSITION_X
, x
);
225 input_report_abs(input
, ABS_MT_POSITION_Y
, y
);
231 static void sis_ts_handle_packet(struct sis_ts_data
*ts
)
234 unsigned int num_to_report
= 0;
235 unsigned int num_contacts
;
236 unsigned int num_reported
;
237 unsigned int contact_size
;
242 error
= sis_read_packet(ts
->client
, ts
->packet
,
243 &num_contacts
, &contact_size
);
247 if (num_to_report
== 0) {
248 num_to_report
= num_contacts
;
249 } else if (num_contacts
!= 0) {
250 dev_err(&ts
->client
->dev
,
251 "%s: nonzero (%d) point count in tail packet\n",
252 __func__
, num_contacts
);
256 report_id
= ts
->packet
[SIS_PKT_REPORT_OFFSET
];
257 contact
= &ts
->packet
[SIS_PKT_CONTACT_OFFSET
];
260 while (num_to_report
> 0) {
261 error
= sis_ts_report_contact(ts
, contact
, report_id
);
265 contact
+= contact_size
;
269 if (report_id
!= SIS_ALL_IN_ONE_PACKAGE
&&
272 * The remainder of contacts is sent
278 } while (num_to_report
> 0);
280 input_mt_sync_frame(ts
->input
);
281 input_sync(ts
->input
);
284 static irqreturn_t
sis_ts_irq_handler(int irq
, void *dev_id
)
286 struct sis_ts_data
*ts
= dev_id
;
289 sis_ts_handle_packet(ts
);
290 } while (ts
->attn_gpio
&& gpiod_get_value_cansleep(ts
->attn_gpio
));
295 static void sis_ts_reset(struct sis_ts_data
*ts
)
297 if (ts
->reset_gpio
) {
298 /* Get out of reset */
299 usleep_range(1000, 2000);
300 gpiod_set_value(ts
->reset_gpio
, 1);
301 usleep_range(1000, 2000);
302 gpiod_set_value(ts
->reset_gpio
, 0);
307 static int sis_ts_probe(struct i2c_client
*client
,
308 const struct i2c_device_id
*id
)
310 struct sis_ts_data
*ts
;
311 struct input_dev
*input
;
314 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
320 ts
->attn_gpio
= devm_gpiod_get_optional(&client
->dev
,
322 if (IS_ERR(ts
->attn_gpio
)) {
323 error
= PTR_ERR(ts
->attn_gpio
);
324 if (error
!= -EPROBE_DEFER
)
325 dev_err(&client
->dev
,
326 "Failed to get attention GPIO: %d\n", error
);
330 ts
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
331 "reset", GPIOD_OUT_LOW
);
332 if (IS_ERR(ts
->reset_gpio
)) {
333 error
= PTR_ERR(ts
->reset_gpio
);
334 if (error
!= -EPROBE_DEFER
)
335 dev_err(&client
->dev
,
336 "Failed to get reset GPIO: %d\n", error
);
342 ts
->input
= input
= devm_input_allocate_device(&client
->dev
);
344 dev_err(&client
->dev
, "Failed to allocate input device\n");
348 input
->name
= "SiS Touchscreen";
349 input
->id
.bustype
= BUS_I2C
;
351 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, SIS_MAX_X
, 0, 0);
352 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, SIS_MAX_Y
, 0, 0);
353 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0, SIS_MAX_PRESSURE
, 0, 0);
354 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
,
355 0, SIS_AREA_LENGTH_LONGER
, 0, 0);
356 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
,
357 0, SIS_AREA_LENGTH_SHORT
, 0, 0);
359 error
= input_mt_init_slots(input
, SIS_MAX_FINGERS
, INPUT_MT_DIRECT
);
361 dev_err(&client
->dev
,
362 "Failed to initialize MT slots: %d\n", error
);
366 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
367 NULL
, sis_ts_irq_handler
,
371 dev_err(&client
->dev
, "Failed to request IRQ: %d\n", error
);
375 error
= input_register_device(ts
->input
);
377 dev_err(&client
->dev
,
378 "Failed to register input device: %d\n", error
);
386 static const struct of_device_id sis_ts_dt_ids
[] = {
387 { .compatible
= "sis,9200-ts" },
390 MODULE_DEVICE_TABLE(of
, sis_ts_dt_ids
);
393 static const struct i2c_device_id sis_ts_id
[] = {
398 MODULE_DEVICE_TABLE(i2c
, sis_ts_id
);
400 static struct i2c_driver sis_ts_driver
= {
402 .name
= SIS_I2C_NAME
,
403 .of_match_table
= of_match_ptr(sis_ts_dt_ids
),
405 .probe
= sis_ts_probe
,
406 .id_table
= sis_ts_id
,
408 module_i2c_driver(sis_ts_driver
);
410 MODULE_DESCRIPTION("SiS 9200 Family Touchscreen Driver");
411 MODULE_LICENSE("GPL v2");
412 MODULE_AUTHOR("Mika Penttilä <mika.penttila@nextfour.com>");