1 /* -------------------------------------------------------------------------
2 * Copyright (C) 2014-2015, Intel Corporation
6 * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * -------------------------------------------------------------------------
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/acpi.h>
23 #include <linux/interrupt.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/delay.h>
26 #include <linux/firmware.h>
27 #include <linux/input.h>
28 #include <linux/input/mt.h>
29 #include <linux/input/touchscreen.h>
31 #include <linux/irq.h>
33 #include <asm/unaligned.h>
35 #define SILEAD_TS_NAME "silead_ts"
37 #define SILEAD_REG_RESET 0xE0
38 #define SILEAD_REG_DATA 0x80
39 #define SILEAD_REG_TOUCH_NR 0x80
40 #define SILEAD_REG_POWER 0xBC
41 #define SILEAD_REG_CLOCK 0xE4
42 #define SILEAD_REG_STATUS 0xB0
43 #define SILEAD_REG_ID 0xFC
44 #define SILEAD_REG_MEM_CHECK 0xB0
46 #define SILEAD_STATUS_OK 0x5A5A5A5A
47 #define SILEAD_TS_DATA_LEN 44
48 #define SILEAD_CLOCK 0x04
50 #define SILEAD_CMD_RESET 0x88
51 #define SILEAD_CMD_START 0x00
53 #define SILEAD_POINT_DATA_LEN 0x04
54 #define SILEAD_POINT_Y_OFF 0x00
55 #define SILEAD_POINT_Y_MSB_OFF 0x01
56 #define SILEAD_POINT_X_OFF 0x02
57 #define SILEAD_POINT_X_MSB_OFF 0x03
58 #define SILEAD_TOUCH_ID_MASK 0xF0
60 #define SILEAD_CMD_SLEEP_MIN 10000
61 #define SILEAD_CMD_SLEEP_MAX 20000
62 #define SILEAD_POWER_SLEEP 20
63 #define SILEAD_STARTUP_SLEEP 30
65 #define SILEAD_MAX_FINGERS 10
67 enum silead_ts_power
{
72 struct silead_ts_data
{
73 struct i2c_client
*client
;
74 struct gpio_desc
*gpio_power
;
75 struct input_dev
*input
;
77 struct touchscreen_properties prop
;
80 struct input_mt_pos pos
[SILEAD_MAX_FINGERS
];
81 int slots
[SILEAD_MAX_FINGERS
];
82 int id
[SILEAD_MAX_FINGERS
];
85 struct silead_fw_data
{
90 static int silead_ts_request_input_dev(struct silead_ts_data
*data
)
92 struct device
*dev
= &data
->client
->dev
;
95 data
->input
= devm_input_allocate_device(dev
);
98 "Failed to allocate input device\n");
102 input_set_abs_params(data
->input
, ABS_MT_POSITION_X
, 0, 4095, 0, 0);
103 input_set_abs_params(data
->input
, ABS_MT_POSITION_Y
, 0, 4095, 0, 0);
104 touchscreen_parse_properties(data
->input
, true, &data
->prop
);
106 input_mt_init_slots(data
->input
, data
->max_fingers
,
107 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
|
110 data
->input
->name
= SILEAD_TS_NAME
;
111 data
->input
->phys
= "input/ts";
112 data
->input
->id
.bustype
= BUS_I2C
;
114 error
= input_register_device(data
->input
);
116 dev_err(dev
, "Failed to register input device: %d\n", error
);
123 static void silead_ts_set_power(struct i2c_client
*client
,
124 enum silead_ts_power state
)
126 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
128 if (data
->gpio_power
) {
129 gpiod_set_value_cansleep(data
->gpio_power
, state
);
130 msleep(SILEAD_POWER_SLEEP
);
134 static void silead_ts_read_data(struct i2c_client
*client
)
136 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
137 struct input_dev
*input
= data
->input
;
138 struct device
*dev
= &client
->dev
;
139 u8
*bufp
, buf
[SILEAD_TS_DATA_LEN
];
140 int touch_nr
, error
, i
;
142 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_DATA
,
143 SILEAD_TS_DATA_LEN
, buf
);
145 dev_err(dev
, "Data read error %d\n", error
);
150 if (touch_nr
> data
->max_fingers
) {
151 dev_warn(dev
, "More touches reported then supported %d > %d\n",
152 touch_nr
, data
->max_fingers
);
153 touch_nr
= data
->max_fingers
;
156 bufp
= buf
+ SILEAD_POINT_DATA_LEN
;
157 for (i
= 0; i
< touch_nr
; i
++, bufp
+= SILEAD_POINT_DATA_LEN
) {
158 /* Bits 4-7 are the touch id */
159 data
->id
[i
] = (bufp
[SILEAD_POINT_X_MSB_OFF
] &
160 SILEAD_TOUCH_ID_MASK
) >> 4;
161 touchscreen_set_mt_pos(&data
->pos
[i
], &data
->prop
,
162 get_unaligned_le16(&bufp
[SILEAD_POINT_X_OFF
]) & 0xfff,
163 get_unaligned_le16(&bufp
[SILEAD_POINT_Y_OFF
]) & 0xfff);
166 input_mt_assign_slots(input
, data
->slots
, data
->pos
, touch_nr
, 0);
168 for (i
= 0; i
< touch_nr
; i
++) {
169 input_mt_slot(input
, data
->slots
[i
]);
170 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
171 input_report_abs(input
, ABS_MT_POSITION_X
, data
->pos
[i
].x
);
172 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->pos
[i
].y
);
174 dev_dbg(dev
, "x=%d y=%d hw_id=%d sw_id=%d\n", data
->pos
[i
].x
,
175 data
->pos
[i
].y
, data
->id
[i
], data
->slots
[i
]);
178 input_mt_sync_frame(input
);
182 static int silead_ts_init(struct i2c_client
*client
)
184 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
187 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
191 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
193 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_TOUCH_NR
,
197 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
199 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
203 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
205 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
209 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
214 dev_err(&client
->dev
, "Registers clear error %d\n", error
);
218 static int silead_ts_reset(struct i2c_client
*client
)
222 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
226 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
228 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
232 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
234 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_POWER
,
238 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
243 dev_err(&client
->dev
, "Chip reset error %d\n", error
);
247 static int silead_ts_startup(struct i2c_client
*client
)
251 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
, 0x00);
253 dev_err(&client
->dev
, "Startup error %d\n", error
);
257 msleep(SILEAD_STARTUP_SLEEP
);
262 static int silead_ts_load_fw(struct i2c_client
*client
)
264 struct device
*dev
= &client
->dev
;
265 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
266 unsigned int fw_size
, i
;
267 const struct firmware
*fw
;
268 struct silead_fw_data
*fw_data
;
271 dev_dbg(dev
, "Firmware file name: %s", data
->fw_name
);
273 error
= request_firmware(&fw
, data
->fw_name
, dev
);
275 dev_err(dev
, "Firmware request error %d\n", error
);
279 fw_size
= fw
->size
/ sizeof(*fw_data
);
280 fw_data
= (struct silead_fw_data
*)fw
->data
;
282 for (i
= 0; i
< fw_size
; i
++) {
283 error
= i2c_smbus_write_i2c_block_data(client
,
286 (u8
*)&fw_data
[i
].val
);
288 dev_err(dev
, "Firmware load error %d\n", error
);
293 release_firmware(fw
);
297 static u32
silead_ts_get_status(struct i2c_client
*client
)
302 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_STATUS
,
303 sizeof(status
), (u8
*)&status
);
305 dev_err(&client
->dev
, "Status read error %d\n", error
);
309 return le32_to_cpu(status
);
312 static int silead_ts_get_id(struct i2c_client
*client
)
314 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
318 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_ID
,
319 sizeof(chip_id
), (u8
*)&chip_id
);
321 dev_err(&client
->dev
, "Chip ID read error %d\n", error
);
325 data
->chip_id
= le32_to_cpu(chip_id
);
326 dev_info(&client
->dev
, "Silead chip ID: 0x%8X", data
->chip_id
);
331 static int silead_ts_setup(struct i2c_client
*client
)
336 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
337 silead_ts_set_power(client
, SILEAD_POWER_ON
);
339 error
= silead_ts_get_id(client
);
343 error
= silead_ts_init(client
);
347 error
= silead_ts_reset(client
);
351 error
= silead_ts_load_fw(client
);
355 error
= silead_ts_startup(client
);
359 status
= silead_ts_get_status(client
);
360 if (status
!= SILEAD_STATUS_OK
) {
361 dev_err(&client
->dev
,
362 "Initialization error, status: 0x%X\n", status
);
369 static irqreturn_t
silead_ts_threaded_irq_handler(int irq
, void *id
)
371 struct silead_ts_data
*data
= id
;
372 struct i2c_client
*client
= data
->client
;
374 silead_ts_read_data(client
);
379 static void silead_ts_read_props(struct i2c_client
*client
)
381 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
382 struct device
*dev
= &client
->dev
;
386 error
= device_property_read_u32(dev
, "silead,max-fingers",
389 dev_dbg(dev
, "Max fingers read error %d\n", error
);
390 data
->max_fingers
= 5; /* Most devices handle up-to 5 fingers */
393 error
= device_property_read_string(dev
, "firmware-name", &str
);
395 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
398 dev_dbg(dev
, "Firmware file name read error. Using default.");
402 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
403 const struct i2c_device_id
*id
)
405 const struct acpi_device_id
*acpi_id
;
406 struct device
*dev
= &data
->client
->dev
;
409 if (ACPI_HANDLE(dev
)) {
410 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
414 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
415 "silead/%s.fw", acpi_id
->id
);
417 for (i
= 0; i
< strlen(data
->fw_name
); i
++)
418 data
->fw_name
[i
] = tolower(data
->fw_name
[i
]);
420 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
421 "silead/%s.fw", id
->name
);
427 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
428 const struct i2c_device_id
*id
)
430 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
431 "silead/%s.fw", id
->name
);
436 static int silead_ts_probe(struct i2c_client
*client
,
437 const struct i2c_device_id
*id
)
439 struct silead_ts_data
*data
;
440 struct device
*dev
= &client
->dev
;
443 if (!i2c_check_functionality(client
->adapter
,
445 I2C_FUNC_SMBUS_READ_I2C_BLOCK
|
446 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
)) {
447 dev_err(dev
, "I2C functionality check failed\n");
451 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
455 i2c_set_clientdata(client
, data
);
456 data
->client
= client
;
458 error
= silead_ts_set_default_fw_name(data
, id
);
462 silead_ts_read_props(client
);
464 /* We must have the IRQ provided by DT or ACPI subsytem */
465 if (client
->irq
<= 0)
469 data
->gpio_power
= devm_gpiod_get_optional(dev
, "power", GPIOD_OUT_LOW
);
470 if (IS_ERR(data
->gpio_power
)) {
471 if (PTR_ERR(data
->gpio_power
) != -EPROBE_DEFER
)
472 dev_err(dev
, "Shutdown GPIO request failed\n");
473 return PTR_ERR(data
->gpio_power
);
476 error
= silead_ts_setup(client
);
480 error
= silead_ts_request_input_dev(data
);
484 error
= devm_request_threaded_irq(dev
, client
->irq
,
485 NULL
, silead_ts_threaded_irq_handler
,
486 IRQF_ONESHOT
, client
->name
, data
);
488 if (error
!= -EPROBE_DEFER
)
489 dev_err(dev
, "IRQ request failed %d\n", error
);
496 static int __maybe_unused
silead_ts_suspend(struct device
*dev
)
498 struct i2c_client
*client
= to_i2c_client(dev
);
500 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
504 static int __maybe_unused
silead_ts_resume(struct device
*dev
)
506 struct i2c_client
*client
= to_i2c_client(dev
);
509 silead_ts_set_power(client
, SILEAD_POWER_ON
);
511 error
= silead_ts_reset(client
);
515 error
= silead_ts_startup(client
);
519 status
= silead_ts_get_status(client
);
520 if (status
!= SILEAD_STATUS_OK
) {
521 dev_err(dev
, "Resume error, status: 0x%02x\n", status
);
528 static SIMPLE_DEV_PM_OPS(silead_ts_pm
, silead_ts_suspend
, silead_ts_resume
);
530 static const struct i2c_device_id silead_ts_id
[] = {
539 MODULE_DEVICE_TABLE(i2c
, silead_ts_id
);
542 static const struct acpi_device_id silead_ts_acpi_match
[] = {
551 MODULE_DEVICE_TABLE(acpi
, silead_ts_acpi_match
);
554 static struct i2c_driver silead_ts_driver
= {
555 .probe
= silead_ts_probe
,
556 .id_table
= silead_ts_id
,
558 .name
= SILEAD_TS_NAME
,
559 .acpi_match_table
= ACPI_PTR(silead_ts_acpi_match
),
563 module_i2c_driver(silead_ts_driver
);
565 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
566 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
567 MODULE_LICENSE("GPL");