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>
32 #include <linux/regulator/consumer.h>
34 #include <asm/unaligned.h>
36 #define SILEAD_TS_NAME "silead_ts"
38 #define SILEAD_REG_RESET 0xE0
39 #define SILEAD_REG_DATA 0x80
40 #define SILEAD_REG_TOUCH_NR 0x80
41 #define SILEAD_REG_POWER 0xBC
42 #define SILEAD_REG_CLOCK 0xE4
43 #define SILEAD_REG_STATUS 0xB0
44 #define SILEAD_REG_ID 0xFC
45 #define SILEAD_REG_MEM_CHECK 0xB0
47 #define SILEAD_STATUS_OK 0x5A5A5A5A
48 #define SILEAD_TS_DATA_LEN 44
49 #define SILEAD_CLOCK 0x04
51 #define SILEAD_CMD_RESET 0x88
52 #define SILEAD_CMD_START 0x00
54 #define SILEAD_POINT_DATA_LEN 0x04
55 #define SILEAD_POINT_Y_OFF 0x00
56 #define SILEAD_POINT_Y_MSB_OFF 0x01
57 #define SILEAD_POINT_X_OFF 0x02
58 #define SILEAD_POINT_X_MSB_OFF 0x03
59 #define SILEAD_TOUCH_ID_MASK 0xF0
61 #define SILEAD_CMD_SLEEP_MIN 10000
62 #define SILEAD_CMD_SLEEP_MAX 20000
63 #define SILEAD_POWER_SLEEP 20
64 #define SILEAD_STARTUP_SLEEP 30
66 #define SILEAD_MAX_FINGERS 10
68 enum silead_ts_power
{
73 struct silead_ts_data
{
74 struct i2c_client
*client
;
75 struct gpio_desc
*gpio_power
;
76 struct input_dev
*input
;
77 struct regulator_bulk_data regulators
[2];
79 struct touchscreen_properties prop
;
82 struct input_mt_pos pos
[SILEAD_MAX_FINGERS
];
83 int slots
[SILEAD_MAX_FINGERS
];
84 int id
[SILEAD_MAX_FINGERS
];
87 struct silead_fw_data
{
92 static int silead_ts_request_input_dev(struct silead_ts_data
*data
)
94 struct device
*dev
= &data
->client
->dev
;
97 data
->input
= devm_input_allocate_device(dev
);
100 "Failed to allocate input device\n");
104 input_set_abs_params(data
->input
, ABS_MT_POSITION_X
, 0, 4095, 0, 0);
105 input_set_abs_params(data
->input
, ABS_MT_POSITION_Y
, 0, 4095, 0, 0);
106 touchscreen_parse_properties(data
->input
, true, &data
->prop
);
108 input_mt_init_slots(data
->input
, data
->max_fingers
,
109 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
|
112 data
->input
->name
= SILEAD_TS_NAME
;
113 data
->input
->phys
= "input/ts";
114 data
->input
->id
.bustype
= BUS_I2C
;
116 error
= input_register_device(data
->input
);
118 dev_err(dev
, "Failed to register input device: %d\n", error
);
125 static void silead_ts_set_power(struct i2c_client
*client
,
126 enum silead_ts_power state
)
128 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
130 if (data
->gpio_power
) {
131 gpiod_set_value_cansleep(data
->gpio_power
, state
);
132 msleep(SILEAD_POWER_SLEEP
);
136 static void silead_ts_read_data(struct i2c_client
*client
)
138 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
139 struct input_dev
*input
= data
->input
;
140 struct device
*dev
= &client
->dev
;
141 u8
*bufp
, buf
[SILEAD_TS_DATA_LEN
];
142 int touch_nr
, error
, i
;
144 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_DATA
,
145 SILEAD_TS_DATA_LEN
, buf
);
147 dev_err(dev
, "Data read error %d\n", error
);
152 if (touch_nr
> data
->max_fingers
) {
153 dev_warn(dev
, "More touches reported then supported %d > %d\n",
154 touch_nr
, data
->max_fingers
);
155 touch_nr
= data
->max_fingers
;
158 bufp
= buf
+ SILEAD_POINT_DATA_LEN
;
159 for (i
= 0; i
< touch_nr
; i
++, bufp
+= SILEAD_POINT_DATA_LEN
) {
160 /* Bits 4-7 are the touch id */
161 data
->id
[i
] = (bufp
[SILEAD_POINT_X_MSB_OFF
] &
162 SILEAD_TOUCH_ID_MASK
) >> 4;
163 touchscreen_set_mt_pos(&data
->pos
[i
], &data
->prop
,
164 get_unaligned_le16(&bufp
[SILEAD_POINT_X_OFF
]) & 0xfff,
165 get_unaligned_le16(&bufp
[SILEAD_POINT_Y_OFF
]) & 0xfff);
168 input_mt_assign_slots(input
, data
->slots
, data
->pos
, touch_nr
, 0);
170 for (i
= 0; i
< touch_nr
; i
++) {
171 input_mt_slot(input
, data
->slots
[i
]);
172 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
173 input_report_abs(input
, ABS_MT_POSITION_X
, data
->pos
[i
].x
);
174 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->pos
[i
].y
);
176 dev_dbg(dev
, "x=%d y=%d hw_id=%d sw_id=%d\n", data
->pos
[i
].x
,
177 data
->pos
[i
].y
, data
->id
[i
], data
->slots
[i
]);
180 input_mt_sync_frame(input
);
184 static int silead_ts_init(struct i2c_client
*client
)
186 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
189 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
193 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
195 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_TOUCH_NR
,
199 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
201 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
205 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
207 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
211 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
216 dev_err(&client
->dev
, "Registers clear error %d\n", error
);
220 static int silead_ts_reset(struct i2c_client
*client
)
224 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
,
228 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
230 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_CLOCK
,
234 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
236 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_POWER
,
240 usleep_range(SILEAD_CMD_SLEEP_MIN
, SILEAD_CMD_SLEEP_MAX
);
245 dev_err(&client
->dev
, "Chip reset error %d\n", error
);
249 static int silead_ts_startup(struct i2c_client
*client
)
253 error
= i2c_smbus_write_byte_data(client
, SILEAD_REG_RESET
, 0x00);
255 dev_err(&client
->dev
, "Startup error %d\n", error
);
259 msleep(SILEAD_STARTUP_SLEEP
);
264 static int silead_ts_load_fw(struct i2c_client
*client
)
266 struct device
*dev
= &client
->dev
;
267 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
268 unsigned int fw_size
, i
;
269 const struct firmware
*fw
;
270 struct silead_fw_data
*fw_data
;
273 dev_dbg(dev
, "Firmware file name: %s", data
->fw_name
);
275 error
= request_firmware(&fw
, data
->fw_name
, dev
);
277 dev_err(dev
, "Firmware request error %d\n", error
);
281 fw_size
= fw
->size
/ sizeof(*fw_data
);
282 fw_data
= (struct silead_fw_data
*)fw
->data
;
284 for (i
= 0; i
< fw_size
; i
++) {
285 error
= i2c_smbus_write_i2c_block_data(client
,
288 (u8
*)&fw_data
[i
].val
);
290 dev_err(dev
, "Firmware load error %d\n", error
);
295 release_firmware(fw
);
299 static u32
silead_ts_get_status(struct i2c_client
*client
)
304 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_STATUS
,
305 sizeof(status
), (u8
*)&status
);
307 dev_err(&client
->dev
, "Status read error %d\n", error
);
311 return le32_to_cpu(status
);
314 static int silead_ts_get_id(struct i2c_client
*client
)
316 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
320 error
= i2c_smbus_read_i2c_block_data(client
, SILEAD_REG_ID
,
321 sizeof(chip_id
), (u8
*)&chip_id
);
323 dev_err(&client
->dev
, "Chip ID read error %d\n", error
);
327 data
->chip_id
= le32_to_cpu(chip_id
);
328 dev_info(&client
->dev
, "Silead chip ID: 0x%8X", data
->chip_id
);
333 static int silead_ts_setup(struct i2c_client
*client
)
338 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
339 silead_ts_set_power(client
, SILEAD_POWER_ON
);
341 error
= silead_ts_get_id(client
);
345 error
= silead_ts_init(client
);
349 error
= silead_ts_reset(client
);
353 error
= silead_ts_load_fw(client
);
357 error
= silead_ts_startup(client
);
361 status
= silead_ts_get_status(client
);
362 if (status
!= SILEAD_STATUS_OK
) {
363 dev_err(&client
->dev
,
364 "Initialization error, status: 0x%X\n", status
);
371 static irqreturn_t
silead_ts_threaded_irq_handler(int irq
, void *id
)
373 struct silead_ts_data
*data
= id
;
374 struct i2c_client
*client
= data
->client
;
376 silead_ts_read_data(client
);
381 static void silead_ts_read_props(struct i2c_client
*client
)
383 struct silead_ts_data
*data
= i2c_get_clientdata(client
);
384 struct device
*dev
= &client
->dev
;
388 error
= device_property_read_u32(dev
, "silead,max-fingers",
391 dev_dbg(dev
, "Max fingers read error %d\n", error
);
392 data
->max_fingers
= 5; /* Most devices handle up-to 5 fingers */
395 error
= device_property_read_string(dev
, "firmware-name", &str
);
397 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
400 dev_dbg(dev
, "Firmware file name read error. Using default.");
404 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
405 const struct i2c_device_id
*id
)
407 const struct acpi_device_id
*acpi_id
;
408 struct device
*dev
= &data
->client
->dev
;
411 if (ACPI_HANDLE(dev
)) {
412 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
416 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
417 "silead/%s.fw", acpi_id
->id
);
419 for (i
= 0; i
< strlen(data
->fw_name
); i
++)
420 data
->fw_name
[i
] = tolower(data
->fw_name
[i
]);
422 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
423 "silead/%s.fw", id
->name
);
429 static int silead_ts_set_default_fw_name(struct silead_ts_data
*data
,
430 const struct i2c_device_id
*id
)
432 snprintf(data
->fw_name
, sizeof(data
->fw_name
),
433 "silead/%s.fw", id
->name
);
438 static void silead_disable_regulator(void *arg
)
440 struct silead_ts_data
*data
= arg
;
442 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
445 static int silead_ts_probe(struct i2c_client
*client
,
446 const struct i2c_device_id
*id
)
448 struct silead_ts_data
*data
;
449 struct device
*dev
= &client
->dev
;
452 if (!i2c_check_functionality(client
->adapter
,
454 I2C_FUNC_SMBUS_READ_I2C_BLOCK
|
455 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
)) {
456 dev_err(dev
, "I2C functionality check failed\n");
460 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
464 i2c_set_clientdata(client
, data
);
465 data
->client
= client
;
467 error
= silead_ts_set_default_fw_name(data
, id
);
471 silead_ts_read_props(client
);
473 /* We must have the IRQ provided by DT or ACPI subsytem */
474 if (client
->irq
<= 0)
477 data
->regulators
[0].supply
= "vddio";
478 data
->regulators
[1].supply
= "avdd";
479 error
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->regulators
),
485 * Enable regulators at probe and disable them at remove, we need
486 * to keep the chip powered otherwise it forgets its firmware.
488 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
),
493 error
= devm_add_action_or_reset(dev
, silead_disable_regulator
, data
);
498 data
->gpio_power
= devm_gpiod_get_optional(dev
, "power", GPIOD_OUT_LOW
);
499 if (IS_ERR(data
->gpio_power
)) {
500 if (PTR_ERR(data
->gpio_power
) != -EPROBE_DEFER
)
501 dev_err(dev
, "Shutdown GPIO request failed\n");
502 return PTR_ERR(data
->gpio_power
);
505 error
= silead_ts_setup(client
);
509 error
= silead_ts_request_input_dev(data
);
513 error
= devm_request_threaded_irq(dev
, client
->irq
,
514 NULL
, silead_ts_threaded_irq_handler
,
515 IRQF_ONESHOT
, client
->name
, data
);
517 if (error
!= -EPROBE_DEFER
)
518 dev_err(dev
, "IRQ request failed %d\n", error
);
525 static int __maybe_unused
silead_ts_suspend(struct device
*dev
)
527 struct i2c_client
*client
= to_i2c_client(dev
);
529 disable_irq(client
->irq
);
530 silead_ts_set_power(client
, SILEAD_POWER_OFF
);
534 static int __maybe_unused
silead_ts_resume(struct device
*dev
)
536 struct i2c_client
*client
= to_i2c_client(dev
);
539 silead_ts_set_power(client
, SILEAD_POWER_ON
);
541 error
= silead_ts_reset(client
);
545 error
= silead_ts_startup(client
);
549 status
= silead_ts_get_status(client
);
550 if (status
!= SILEAD_STATUS_OK
) {
551 dev_err(dev
, "Resume error, status: 0x%02x\n", status
);
555 enable_irq(client
->irq
);
560 static SIMPLE_DEV_PM_OPS(silead_ts_pm
, silead_ts_suspend
, silead_ts_resume
);
562 static const struct i2c_device_id silead_ts_id
[] = {
571 MODULE_DEVICE_TABLE(i2c
, silead_ts_id
);
574 static const struct acpi_device_id silead_ts_acpi_match
[] = {
583 MODULE_DEVICE_TABLE(acpi
, silead_ts_acpi_match
);
587 static const struct of_device_id silead_ts_of_match
[] = {
588 { .compatible
= "silead,gsl1680" },
589 { .compatible
= "silead,gsl1688" },
590 { .compatible
= "silead,gsl3670" },
591 { .compatible
= "silead,gsl3675" },
592 { .compatible
= "silead,gsl3692" },
595 MODULE_DEVICE_TABLE(of
, silead_ts_of_match
);
598 static struct i2c_driver silead_ts_driver
= {
599 .probe
= silead_ts_probe
,
600 .id_table
= silead_ts_id
,
602 .name
= SILEAD_TS_NAME
,
603 .acpi_match_table
= ACPI_PTR(silead_ts_acpi_match
),
604 .of_match_table
= of_match_ptr(silead_ts_of_match
),
608 module_i2c_driver(silead_ts_driver
);
610 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
611 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
612 MODULE_LICENSE("GPL");