treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / touchscreen / silead.c
blobad8b6a2bfd3640bcc55f07fdfbdb25649a9f0dbd
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2015, Intel Corporation
5 * Derived from:
6 * gslX68X.c
7 * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
9 * -------------------------------------------------------------------------
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/interrupt.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
18 #include <linux/firmware.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/pm.h>
23 #include <linux/irq.h>
24 #include <linux/regulator/consumer.h>
26 #include <asm/unaligned.h>
28 #define SILEAD_TS_NAME "silead_ts"
30 #define SILEAD_REG_RESET 0xE0
31 #define SILEAD_REG_DATA 0x80
32 #define SILEAD_REG_TOUCH_NR 0x80
33 #define SILEAD_REG_POWER 0xBC
34 #define SILEAD_REG_CLOCK 0xE4
35 #define SILEAD_REG_STATUS 0xB0
36 #define SILEAD_REG_ID 0xFC
37 #define SILEAD_REG_MEM_CHECK 0xB0
39 #define SILEAD_STATUS_OK 0x5A5A5A5A
40 #define SILEAD_TS_DATA_LEN 44
41 #define SILEAD_CLOCK 0x04
43 #define SILEAD_CMD_RESET 0x88
44 #define SILEAD_CMD_START 0x00
46 #define SILEAD_POINT_DATA_LEN 0x04
47 #define SILEAD_POINT_Y_OFF 0x00
48 #define SILEAD_POINT_Y_MSB_OFF 0x01
49 #define SILEAD_POINT_X_OFF 0x02
50 #define SILEAD_POINT_X_MSB_OFF 0x03
51 #define SILEAD_EXTRA_DATA_MASK 0xF0
53 #define SILEAD_CMD_SLEEP_MIN 10000
54 #define SILEAD_CMD_SLEEP_MAX 20000
55 #define SILEAD_POWER_SLEEP 20
56 #define SILEAD_STARTUP_SLEEP 30
58 #define SILEAD_MAX_FINGERS 10
60 enum silead_ts_power {
61 SILEAD_POWER_ON = 1,
62 SILEAD_POWER_OFF = 0
65 struct silead_ts_data {
66 struct i2c_client *client;
67 struct gpio_desc *gpio_power;
68 struct input_dev *input;
69 struct regulator_bulk_data regulators[2];
70 char fw_name[64];
71 struct touchscreen_properties prop;
72 u32 max_fingers;
73 u32 chip_id;
74 struct input_mt_pos pos[SILEAD_MAX_FINGERS];
75 int slots[SILEAD_MAX_FINGERS];
76 int id[SILEAD_MAX_FINGERS];
79 struct silead_fw_data {
80 u32 offset;
81 u32 val;
84 static int silead_ts_request_input_dev(struct silead_ts_data *data)
86 struct device *dev = &data->client->dev;
87 int error;
89 data->input = devm_input_allocate_device(dev);
90 if (!data->input) {
91 dev_err(dev,
92 "Failed to allocate input device\n");
93 return -ENOMEM;
96 input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
97 input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
98 touchscreen_parse_properties(data->input, true, &data->prop);
100 input_mt_init_slots(data->input, data->max_fingers,
101 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
102 INPUT_MT_TRACK);
104 if (device_property_read_bool(dev, "silead,home-button"))
105 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
107 data->input->name = SILEAD_TS_NAME;
108 data->input->phys = "input/ts";
109 data->input->id.bustype = BUS_I2C;
111 error = input_register_device(data->input);
112 if (error) {
113 dev_err(dev, "Failed to register input device: %d\n", error);
114 return error;
117 return 0;
120 static void silead_ts_set_power(struct i2c_client *client,
121 enum silead_ts_power state)
123 struct silead_ts_data *data = i2c_get_clientdata(client);
125 if (data->gpio_power) {
126 gpiod_set_value_cansleep(data->gpio_power, state);
127 msleep(SILEAD_POWER_SLEEP);
131 static void silead_ts_read_data(struct i2c_client *client)
133 struct silead_ts_data *data = i2c_get_clientdata(client);
134 struct input_dev *input = data->input;
135 struct device *dev = &client->dev;
136 u8 *bufp, buf[SILEAD_TS_DATA_LEN];
137 int touch_nr, softbutton, error, i;
138 bool softbutton_pressed = false;
140 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
141 SILEAD_TS_DATA_LEN, buf);
142 if (error < 0) {
143 dev_err(dev, "Data read error %d\n", error);
144 return;
147 if (buf[0] > data->max_fingers) {
148 dev_warn(dev, "More touches reported then supported %d > %d\n",
149 buf[0], data->max_fingers);
150 buf[0] = data->max_fingers;
153 touch_nr = 0;
154 bufp = buf + SILEAD_POINT_DATA_LEN;
155 for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
156 softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
157 SILEAD_EXTRA_DATA_MASK) >> 4;
159 if (softbutton) {
161 * For now only respond to softbutton == 0x01, some
162 * tablets *without* a capacative button send 0x04
163 * when crossing the edges of the screen.
165 if (softbutton == 0x01)
166 softbutton_pressed = true;
168 continue;
172 * Bits 4-7 are the touch id, note not all models have
173 * hardware touch ids so atm we don't use these.
175 data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
176 SILEAD_EXTRA_DATA_MASK) >> 4;
177 touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
178 get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
179 get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
180 touch_nr++;
183 input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
185 for (i = 0; i < touch_nr; i++) {
186 input_mt_slot(input, data->slots[i]);
187 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
188 input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
189 input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
191 dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
192 data->pos[i].y, data->id[i], data->slots[i]);
195 input_mt_sync_frame(input);
196 input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
197 input_sync(input);
200 static int silead_ts_init(struct i2c_client *client)
202 struct silead_ts_data *data = i2c_get_clientdata(client);
203 int error;
205 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
206 SILEAD_CMD_RESET);
207 if (error)
208 goto i2c_write_err;
209 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
211 error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
212 data->max_fingers);
213 if (error)
214 goto i2c_write_err;
215 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
217 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
218 SILEAD_CLOCK);
219 if (error)
220 goto i2c_write_err;
221 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
223 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
224 SILEAD_CMD_START);
225 if (error)
226 goto i2c_write_err;
227 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
229 return 0;
231 i2c_write_err:
232 dev_err(&client->dev, "Registers clear error %d\n", error);
233 return error;
236 static int silead_ts_reset(struct i2c_client *client)
238 int error;
240 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
241 SILEAD_CMD_RESET);
242 if (error)
243 goto i2c_write_err;
244 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
246 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
247 SILEAD_CLOCK);
248 if (error)
249 goto i2c_write_err;
250 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
252 error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
253 SILEAD_CMD_START);
254 if (error)
255 goto i2c_write_err;
256 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
258 return 0;
260 i2c_write_err:
261 dev_err(&client->dev, "Chip reset error %d\n", error);
262 return error;
265 static int silead_ts_startup(struct i2c_client *client)
267 int error;
269 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
270 if (error) {
271 dev_err(&client->dev, "Startup error %d\n", error);
272 return error;
275 msleep(SILEAD_STARTUP_SLEEP);
277 return 0;
280 static int silead_ts_load_fw(struct i2c_client *client)
282 struct device *dev = &client->dev;
283 struct silead_ts_data *data = i2c_get_clientdata(client);
284 unsigned int fw_size, i;
285 const struct firmware *fw;
286 struct silead_fw_data *fw_data;
287 int error;
289 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
291 error = request_firmware(&fw, data->fw_name, dev);
292 if (error) {
293 dev_err(dev, "Firmware request error %d\n", error);
294 return error;
297 fw_size = fw->size / sizeof(*fw_data);
298 fw_data = (struct silead_fw_data *)fw->data;
300 for (i = 0; i < fw_size; i++) {
301 error = i2c_smbus_write_i2c_block_data(client,
302 fw_data[i].offset,
304 (u8 *)&fw_data[i].val);
305 if (error) {
306 dev_err(dev, "Firmware load error %d\n", error);
307 break;
311 release_firmware(fw);
312 return error ?: 0;
315 static u32 silead_ts_get_status(struct i2c_client *client)
317 int error;
318 __le32 status;
320 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
321 sizeof(status), (u8 *)&status);
322 if (error < 0) {
323 dev_err(&client->dev, "Status read error %d\n", error);
324 return error;
327 return le32_to_cpu(status);
330 static int silead_ts_get_id(struct i2c_client *client)
332 struct silead_ts_data *data = i2c_get_clientdata(client);
333 __le32 chip_id;
334 int error;
336 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
337 sizeof(chip_id), (u8 *)&chip_id);
338 if (error < 0) {
339 dev_err(&client->dev, "Chip ID read error %d\n", error);
340 return error;
343 data->chip_id = le32_to_cpu(chip_id);
344 dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
346 return 0;
349 static int silead_ts_setup(struct i2c_client *client)
351 int error;
352 u32 status;
354 silead_ts_set_power(client, SILEAD_POWER_OFF);
355 silead_ts_set_power(client, SILEAD_POWER_ON);
357 error = silead_ts_get_id(client);
358 if (error)
359 return error;
361 error = silead_ts_init(client);
362 if (error)
363 return error;
365 error = silead_ts_reset(client);
366 if (error)
367 return error;
369 error = silead_ts_load_fw(client);
370 if (error)
371 return error;
373 error = silead_ts_startup(client);
374 if (error)
375 return error;
377 status = silead_ts_get_status(client);
378 if (status != SILEAD_STATUS_OK) {
379 dev_err(&client->dev,
380 "Initialization error, status: 0x%X\n", status);
381 return -ENODEV;
384 return 0;
387 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
389 struct silead_ts_data *data = id;
390 struct i2c_client *client = data->client;
392 silead_ts_read_data(client);
394 return IRQ_HANDLED;
397 static void silead_ts_read_props(struct i2c_client *client)
399 struct silead_ts_data *data = i2c_get_clientdata(client);
400 struct device *dev = &client->dev;
401 const char *str;
402 int error;
404 error = device_property_read_u32(dev, "silead,max-fingers",
405 &data->max_fingers);
406 if (error) {
407 dev_dbg(dev, "Max fingers read error %d\n", error);
408 data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
411 error = device_property_read_string(dev, "firmware-name", &str);
412 if (!error)
413 snprintf(data->fw_name, sizeof(data->fw_name),
414 "silead/%s", str);
415 else
416 dev_dbg(dev, "Firmware file name read error. Using default.");
419 #ifdef CONFIG_ACPI
420 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
421 const struct i2c_device_id *id)
423 const struct acpi_device_id *acpi_id;
424 struct device *dev = &data->client->dev;
425 int i;
427 if (ACPI_HANDLE(dev)) {
428 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
429 if (!acpi_id)
430 return -ENODEV;
432 snprintf(data->fw_name, sizeof(data->fw_name),
433 "silead/%s.fw", acpi_id->id);
435 for (i = 0; i < strlen(data->fw_name); i++)
436 data->fw_name[i] = tolower(data->fw_name[i]);
437 } else {
438 snprintf(data->fw_name, sizeof(data->fw_name),
439 "silead/%s.fw", id->name);
442 return 0;
444 #else
445 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
446 const struct i2c_device_id *id)
448 snprintf(data->fw_name, sizeof(data->fw_name),
449 "silead/%s.fw", id->name);
450 return 0;
452 #endif
454 static void silead_disable_regulator(void *arg)
456 struct silead_ts_data *data = arg;
458 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
461 static int silead_ts_probe(struct i2c_client *client,
462 const struct i2c_device_id *id)
464 struct silead_ts_data *data;
465 struct device *dev = &client->dev;
466 int error;
468 if (!i2c_check_functionality(client->adapter,
469 I2C_FUNC_I2C |
470 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
471 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
472 dev_err(dev, "I2C functionality check failed\n");
473 return -ENXIO;
476 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
477 if (!data)
478 return -ENOMEM;
480 i2c_set_clientdata(client, data);
481 data->client = client;
483 error = silead_ts_set_default_fw_name(data, id);
484 if (error)
485 return error;
487 silead_ts_read_props(client);
489 /* We must have the IRQ provided by DT or ACPI subsytem */
490 if (client->irq <= 0)
491 return -ENODEV;
493 data->regulators[0].supply = "vddio";
494 data->regulators[1].supply = "avdd";
495 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
496 data->regulators);
497 if (error)
498 return error;
501 * Enable regulators at probe and disable them at remove, we need
502 * to keep the chip powered otherwise it forgets its firmware.
504 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
505 data->regulators);
506 if (error)
507 return error;
509 error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
510 if (error)
511 return error;
513 /* Power GPIO pin */
514 data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
515 if (IS_ERR(data->gpio_power)) {
516 if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
517 dev_err(dev, "Shutdown GPIO request failed\n");
518 return PTR_ERR(data->gpio_power);
521 error = silead_ts_setup(client);
522 if (error)
523 return error;
525 error = silead_ts_request_input_dev(data);
526 if (error)
527 return error;
529 error = devm_request_threaded_irq(dev, client->irq,
530 NULL, silead_ts_threaded_irq_handler,
531 IRQF_ONESHOT, client->name, data);
532 if (error) {
533 if (error != -EPROBE_DEFER)
534 dev_err(dev, "IRQ request failed %d\n", error);
535 return error;
538 return 0;
541 static int __maybe_unused silead_ts_suspend(struct device *dev)
543 struct i2c_client *client = to_i2c_client(dev);
545 disable_irq(client->irq);
546 silead_ts_set_power(client, SILEAD_POWER_OFF);
547 return 0;
550 static int __maybe_unused silead_ts_resume(struct device *dev)
552 struct i2c_client *client = to_i2c_client(dev);
553 bool second_try = false;
554 int error, status;
556 silead_ts_set_power(client, SILEAD_POWER_ON);
558 retry:
559 error = silead_ts_reset(client);
560 if (error)
561 return error;
563 if (second_try) {
564 error = silead_ts_load_fw(client);
565 if (error)
566 return error;
569 error = silead_ts_startup(client);
570 if (error)
571 return error;
573 status = silead_ts_get_status(client);
574 if (status != SILEAD_STATUS_OK) {
575 if (!second_try) {
576 second_try = true;
577 dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
578 goto retry;
580 dev_err(dev, "Resume error, status: 0x%02x\n", status);
581 return -ENODEV;
584 enable_irq(client->irq);
586 return 0;
589 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
591 static const struct i2c_device_id silead_ts_id[] = {
592 { "gsl1680", 0 },
593 { "gsl1688", 0 },
594 { "gsl3670", 0 },
595 { "gsl3675", 0 },
596 { "gsl3692", 0 },
597 { "mssl1680", 0 },
600 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
602 #ifdef CONFIG_ACPI
603 static const struct acpi_device_id silead_ts_acpi_match[] = {
604 { "GSL1680", 0 },
605 { "GSL1688", 0 },
606 { "GSL3670", 0 },
607 { "GSL3675", 0 },
608 { "GSL3692", 0 },
609 { "MSSL1680", 0 },
610 { "MSSL0001", 0 },
611 { "MSSL0002", 0 },
612 { "MSSL0017", 0 },
615 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
616 #endif
618 #ifdef CONFIG_OF
619 static const struct of_device_id silead_ts_of_match[] = {
620 { .compatible = "silead,gsl1680" },
621 { .compatible = "silead,gsl1688" },
622 { .compatible = "silead,gsl3670" },
623 { .compatible = "silead,gsl3675" },
624 { .compatible = "silead,gsl3692" },
625 { },
627 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
628 #endif
630 static struct i2c_driver silead_ts_driver = {
631 .probe = silead_ts_probe,
632 .id_table = silead_ts_id,
633 .driver = {
634 .name = SILEAD_TS_NAME,
635 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
636 .of_match_table = of_match_ptr(silead_ts_of_match),
637 .pm = &silead_ts_pm,
640 module_i2c_driver(silead_ts_driver);
642 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
643 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
644 MODULE_LICENSE("GPL");