1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2015, Intel Corporation
4 *
5 * Derived from:
6 * gslX68X.c
7 * Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
8 *
9 * -------------------------------------------------------------------------
10 */
11
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/pm_runtime.h>
24 #include <linux/irq.h>
25 #include <linux/regulator/consumer.h>
26
27 #include <asm/unaligned.h>
28
29 #define SILEAD_TS_NAME "silead_ts"
30
31 #define SILEAD_REG_RESET 0xE0
32 #define SILEAD_REG_DATA 0x80
33 #define SILEAD_REG_TOUCH_NR 0x80
34 #define SILEAD_REG_POWER 0xBC
35 #define SILEAD_REG_CLOCK 0xE4
36 #define SILEAD_REG_STATUS 0xB0
37 #define SILEAD_REG_ID 0xFC
38 #define SILEAD_REG_MEM_CHECK 0xB0
39
40 #define SILEAD_STATUS_OK 0x5A5A5A5A
41 #define SILEAD_TS_DATA_LEN 44
42 #define SILEAD_CLOCK 0x04
43
44 #define SILEAD_CMD_RESET 0x88
45 #define SILEAD_CMD_START 0x00
46
47 #define SILEAD_POINT_DATA_LEN 0x04
48 #define SILEAD_POINT_Y_OFF 0x00
49 #define SILEAD_POINT_Y_MSB_OFF 0x01
50 #define SILEAD_POINT_X_OFF 0x02
51 #define SILEAD_POINT_X_MSB_OFF 0x03
52 #define SILEAD_EXTRA_DATA_MASK 0xF0
53
54 #define SILEAD_CMD_SLEEP_MIN 10000
55 #define SILEAD_CMD_SLEEP_MAX 20000
56 #define SILEAD_POWER_SLEEP 20
57 #define SILEAD_STARTUP_SLEEP 30
58
59 #define SILEAD_MAX_FINGERS 10
60
61 enum silead_ts_power {
62 SILEAD_POWER_ON = 1,
63 SILEAD_POWER_OFF = 0
64 };
65
66 struct silead_ts_data {
67 struct i2c_client *client;
68 struct gpio_desc *gpio_power;
69 struct input_dev *input;
70 struct input_dev *pen_input;
71 struct regulator_bulk_data regulators[2];
72 char fw_name[64];
73 struct touchscreen_properties prop;
74 u32 chip_id;
75 struct input_mt_pos pos[SILEAD_MAX_FINGERS];
76 int slots[SILEAD_MAX_FINGERS];
77 int id[SILEAD_MAX_FINGERS];
78 u32 efi_fw_min_max[4];
79 bool efi_fw_min_max_set;
80 bool pen_supported;
81 bool pen_down;
82 u32 pen_x_res;
83 u32 pen_y_res;
84 int pen_up_count;
85 };
86
87 struct silead_fw_data {
88 u32 offset;
89 u32 val;
90 };
91
silead_apply_efi_fw_min_max(struct silead_ts_data * data)92 static void silead_apply_efi_fw_min_max(struct silead_ts_data *data)
93 {
94 struct input_absinfo *absinfo_x = &data->input->absinfo[ABS_MT_POSITION_X];
95 struct input_absinfo *absinfo_y = &data->input->absinfo[ABS_MT_POSITION_Y];
96
97 if (!data->efi_fw_min_max_set)
98 return;
99
100 absinfo_x->minimum = data->efi_fw_min_max[0];
101 absinfo_x->maximum = data->efi_fw_min_max[1];
102 absinfo_y->minimum = data->efi_fw_min_max[2];
103 absinfo_y->maximum = data->efi_fw_min_max[3];
104
105 if (data->prop.invert_x) {
106 absinfo_x->maximum -= absinfo_x->minimum;
107 absinfo_x->minimum = 0;
108 }
109
110 if (data->prop.invert_y) {
111 absinfo_y->maximum -= absinfo_y->minimum;
112 absinfo_y->minimum = 0;
113 }
114
115 if (data->prop.swap_x_y) {
116 swap(absinfo_x->minimum, absinfo_y->minimum);
117 swap(absinfo_x->maximum, absinfo_y->maximum);
118 }
119 }
120
silead_ts_request_input_dev(struct silead_ts_data * data)121 static int silead_ts_request_input_dev(struct silead_ts_data *data)
122 {
123 struct device *dev = &data->client->dev;
124 int error;
125
126 data->input = devm_input_allocate_device(dev);
127 if (!data->input) {
128 dev_err(dev,
129 "Failed to allocate input device\n");
130 return -ENOMEM;
131 }
132
133 input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
134 input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
135 touchscreen_parse_properties(data->input, true, &data->prop);
136 silead_apply_efi_fw_min_max(data);
137
138 input_mt_init_slots(data->input, SILEAD_MAX_FINGERS,
139 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
140 INPUT_MT_TRACK);
141
142 if (device_property_read_bool(dev, "silead,home-button"))
143 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
144
145 data->input->name = SILEAD_TS_NAME;
146 data->input->phys = "input/ts";
147 data->input->id.bustype = BUS_I2C;
148
149 error = input_register_device(data->input);
150 if (error) {
151 dev_err(dev, "Failed to register input device: %d\n", error);
152 return error;
153 }
154
155 return 0;
156 }
157
silead_ts_request_pen_input_dev(struct silead_ts_data * data)158 static int silead_ts_request_pen_input_dev(struct silead_ts_data *data)
159 {
160 struct device *dev = &data->client->dev;
161 int error;
162
163 if (!data->pen_supported)
164 return 0;
165
166 data->pen_input = devm_input_allocate_device(dev);
167 if (!data->pen_input)
168 return -ENOMEM;
169
170 input_set_abs_params(data->pen_input, ABS_X, 0, 4095, 0, 0);
171 input_set_abs_params(data->pen_input, ABS_Y, 0, 4095, 0, 0);
172 input_set_capability(data->pen_input, EV_KEY, BTN_TOUCH);
173 input_set_capability(data->pen_input, EV_KEY, BTN_TOOL_PEN);
174 set_bit(INPUT_PROP_DIRECT, data->pen_input->propbit);
175 touchscreen_parse_properties(data->pen_input, false, &data->prop);
176 input_abs_set_res(data->pen_input, ABS_X, data->pen_x_res);
177 input_abs_set_res(data->pen_input, ABS_Y, data->pen_y_res);
178
179 data->pen_input->name = SILEAD_TS_NAME " pen";
180 data->pen_input->phys = "input/pen";
181 data->input->id.bustype = BUS_I2C;
182
183 error = input_register_device(data->pen_input);
184 if (error) {
185 dev_err(dev, "Failed to register pen input device: %d\n", error);
186 return error;
187 }
188
189 return 0;
190 }
191
silead_ts_set_power(struct i2c_client * client,enum silead_ts_power state)192 static void silead_ts_set_power(struct i2c_client *client,
193 enum silead_ts_power state)
194 {
195 struct silead_ts_data *data = i2c_get_clientdata(client);
196
197 if (data->gpio_power) {
198 gpiod_set_value_cansleep(data->gpio_power, state);
199 msleep(SILEAD_POWER_SLEEP);
200 }
201 }
202
silead_ts_handle_pen_data(struct silead_ts_data * data,u8 * buf)203 static bool silead_ts_handle_pen_data(struct silead_ts_data *data, u8 *buf)
204 {
205 u8 *coord = buf + SILEAD_POINT_DATA_LEN;
206 struct input_mt_pos pos;
207
208 if (!data->pen_supported || buf[2] != 0x00 || buf[3] != 0x00)
209 return false;
210
211 if (buf[0] == 0x00 && buf[1] == 0x00 && data->pen_down) {
212 data->pen_up_count++;
213 if (data->pen_up_count == 6) {
214 data->pen_down = false;
215 goto sync;
216 }
217 return true;
218 }
219
220 if (buf[0] == 0x01 && buf[1] == 0x08) {
221 touchscreen_set_mt_pos(&pos, &data->prop,
222 get_unaligned_le16(&coord[SILEAD_POINT_X_OFF]) & 0xfff,
223 get_unaligned_le16(&coord[SILEAD_POINT_Y_OFF]) & 0xfff);
224
225 input_report_abs(data->pen_input, ABS_X, pos.x);
226 input_report_abs(data->pen_input, ABS_Y, pos.y);
227
228 data->pen_up_count = 0;
229 data->pen_down = true;
230 goto sync;
231 }
232
233 return false;
234
235 sync:
236 input_report_key(data->pen_input, BTN_TOOL_PEN, data->pen_down);
237 input_report_key(data->pen_input, BTN_TOUCH, data->pen_down);
238 input_sync(data->pen_input);
239 return true;
240 }
241
silead_ts_read_data(struct i2c_client * client)242 static void silead_ts_read_data(struct i2c_client *client)
243 {
244 struct silead_ts_data *data = i2c_get_clientdata(client);
245 struct input_dev *input = data->input;
246 struct device *dev = &client->dev;
247 u8 *bufp, buf[SILEAD_TS_DATA_LEN];
248 int touch_nr, softbutton, error, i;
249 bool softbutton_pressed = false;
250
251 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
252 SILEAD_TS_DATA_LEN, buf);
253 if (error < 0) {
254 dev_err(dev, "Data read error %d\n", error);
255 return;
256 }
257
258 if (buf[0] > SILEAD_MAX_FINGERS) {
259 dev_warn(dev, "More touches reported then supported %d > %d\n",
260 buf[0], SILEAD_MAX_FINGERS);
261 buf[0] = SILEAD_MAX_FINGERS;
262 }
263
264 if (silead_ts_handle_pen_data(data, buf))
265 goto sync; /* Pen is down, release all previous touches */
266
267 touch_nr = 0;
268 bufp = buf + SILEAD_POINT_DATA_LEN;
269 for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
270 softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
271 SILEAD_EXTRA_DATA_MASK) >> 4;
272
273 if (softbutton) {
274 /*
275 * For now only respond to softbutton == 0x01, some
276 * tablets *without* a capacative button send 0x04
277 * when crossing the edges of the screen.
278 */
279 if (softbutton == 0x01)
280 softbutton_pressed = true;
281
282 continue;
283 }
284
285 /*
286 * Bits 4-7 are the touch id, note not all models have
287 * hardware touch ids so atm we don't use these.
288 */
289 data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
290 SILEAD_EXTRA_DATA_MASK) >> 4;
291 touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
292 get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
293 get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
294 touch_nr++;
295 }
296
297 input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
298
299 for (i = 0; i < touch_nr; i++) {
300 input_mt_slot(input, data->slots[i]);
301 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
302 input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
303 input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
304
305 dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
306 data->pos[i].y, data->id[i], data->slots[i]);
307 }
308
309 sync:
310 input_mt_sync_frame(input);
311 input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
312 input_sync(input);
313 }
314
silead_ts_init(struct i2c_client * client)315 static int silead_ts_init(struct i2c_client *client)
316 {
317 int error;
318
319 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
320 SILEAD_CMD_RESET);
321 if (error)
322 goto i2c_write_err;
323 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
324
325 error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
326 SILEAD_MAX_FINGERS);
327 if (error)
328 goto i2c_write_err;
329 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
330
331 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
332 SILEAD_CLOCK);
333 if (error)
334 goto i2c_write_err;
335 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
336
337 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
338 SILEAD_CMD_START);
339 if (error)
340 goto i2c_write_err;
341 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
342
343 return 0;
344
345 i2c_write_err:
346 dev_err(&client->dev, "Registers clear error %d\n", error);
347 return error;
348 }
349
silead_ts_reset(struct i2c_client * client)350 static int silead_ts_reset(struct i2c_client *client)
351 {
352 int error;
353
354 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
355 SILEAD_CMD_RESET);
356 if (error)
357 goto i2c_write_err;
358 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
359
360 error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
361 SILEAD_CLOCK);
362 if (error)
363 goto i2c_write_err;
364 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
365
366 error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
367 SILEAD_CMD_START);
368 if (error)
369 goto i2c_write_err;
370 usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
371
372 return 0;
373
374 i2c_write_err:
375 dev_err(&client->dev, "Chip reset error %d\n", error);
376 return error;
377 }
378
silead_ts_startup(struct i2c_client * client)379 static int silead_ts_startup(struct i2c_client *client)
380 {
381 int error;
382
383 error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
384 if (error) {
385 dev_err(&client->dev, "Startup error %d\n", error);
386 return error;
387 }
388
389 msleep(SILEAD_STARTUP_SLEEP);
390
391 return 0;
392 }
393
silead_ts_load_fw(struct i2c_client * client)394 static int silead_ts_load_fw(struct i2c_client *client)
395 {
396 struct device *dev = &client->dev;
397 struct silead_ts_data *data = i2c_get_clientdata(client);
398 const struct firmware *fw = NULL;
399 struct silead_fw_data *fw_data;
400 unsigned int fw_size, i;
401 int error;
402
403 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
404
405 /*
406 * Unfortunately, at the time of writing this comment, we have been unable to
407 * get permission from Silead, or from device OEMs, to distribute the necessary
408 * Silead firmware files in linux-firmware.
409 *
410 * On a whole bunch of devices the UEFI BIOS code contains a touchscreen driver,
411 * which contains an embedded copy of the firmware. The fw-loader code has a
412 * "platform" fallback mechanism, which together with info on the firmware
413 * from drivers/platform/x86/touchscreen_dmi.c will use the firmware from the
414 * UEFI driver when the firmware is missing from /lib/firmware. This makes the
415 * touchscreen work OOTB without users needing to manually download the firmware.
416 *
417 * The firmware bundled with the original Windows/Android is usually newer then
418 * the firmware in the UEFI driver and it is better calibrated. This better
419 * calibration can lead to significant differences in the reported min/max
420 * coordinates.
421 *
422 * To deal with this we first try to load the firmware without "platform"
423 * fallback. If that fails we retry with "platform" fallback and if that
424 * succeeds we apply an (optional) set of alternative min/max values from the
425 * "silead,efi-fw-min-max" property.
426 */
427 error = firmware_request_nowarn(&fw, data->fw_name, dev);
428 if (error) {
429 error = firmware_request_platform(&fw, data->fw_name, dev);
430 if (error) {
431 dev_err(dev, "Firmware request error %d\n", error);
432 return error;
433 }
434
435 error = device_property_read_u32_array(dev, "silead,efi-fw-min-max",
436 data->efi_fw_min_max,
437 ARRAY_SIZE(data->efi_fw_min_max));
438 if (!error)
439 data->efi_fw_min_max_set = true;
440
441 /* The EFI (platform) embedded fw does not have pen support */
442 if (data->pen_supported) {
443 dev_warn(dev, "Warning loading '%s' from filesystem failed, using EFI embedded copy.\n",
444 data->fw_name);
445 dev_warn(dev, "Warning pen support is known to be broken in the EFI embedded fw version\n");
446 data->pen_supported = false;
447 }
448 }
449
450 fw_size = fw->size / sizeof(*fw_data);
451 fw_data = (struct silead_fw_data *)fw->data;
452
453 for (i = 0; i < fw_size; i++) {
454 error = i2c_smbus_write_i2c_block_data(client,
455 fw_data[i].offset,
456 4,
457 (u8 *)&fw_data[i].val);
458 if (error) {
459 dev_err(dev, "Firmware load error %d\n", error);
460 break;
461 }
462 }
463
464 release_firmware(fw);
465 return error ?: 0;
466 }
467
silead_ts_get_status(struct i2c_client * client)468 static u32 silead_ts_get_status(struct i2c_client *client)
469 {
470 int error;
471 __le32 status;
472
473 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
474 sizeof(status), (u8 *)&status);
475 if (error < 0) {
476 dev_err(&client->dev, "Status read error %d\n", error);
477 return error;
478 }
479
480 return le32_to_cpu(status);
481 }
482
silead_ts_get_id(struct i2c_client * client)483 static int silead_ts_get_id(struct i2c_client *client)
484 {
485 struct silead_ts_data *data = i2c_get_clientdata(client);
486 __le32 chip_id;
487 int error;
488
489 error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
490 sizeof(chip_id), (u8 *)&chip_id);
491 if (error < 0)
492 return error;
493
494 data->chip_id = le32_to_cpu(chip_id);
495 dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
496
497 return 0;
498 }
499
silead_ts_setup(struct i2c_client * client)500 static int silead_ts_setup(struct i2c_client *client)
501 {
502 int error;
503 u32 status;
504
505 /*
506 * Some buggy BIOS-es bring up the chip in a stuck state where it
507 * blocks the I2C bus. The following steps are necessary to
508 * unstuck the chip / bus:
509 * 1. Turn off the Silead chip.
510 * 2. Try to do an I2C transfer with the chip, this will fail in
511 * response to which the I2C-bus-driver will call:
512 * i2c_recover_bus() which will unstuck the I2C-bus. Note the
513 * unstuck-ing of the I2C bus only works if we first drop the
514 * chip off the bus by turning it off.
515 * 3. Turn the chip back on.
516 *
517 * On the x86/ACPI systems were this problem is seen, step 1. and
518 * 3. require making ACPI calls and dealing with ACPI Power
519 * Resources. The workaround below runtime-suspends the chip to
520 * turn it off, leaving it up to the ACPI subsystem to deal with
521 * this.
522 */
523
524 if (device_property_read_bool(&client->dev,
525 "silead,stuck-controller-bug")) {
526 pm_runtime_set_active(&client->dev);
527 pm_runtime_enable(&client->dev);
528 pm_runtime_allow(&client->dev);
529
530 pm_runtime_suspend(&client->dev);
531
532 dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
533 silead_ts_get_id(client);
534
535 /* The forbid will also resume the device */
536 pm_runtime_forbid(&client->dev);
537 pm_runtime_disable(&client->dev);
538 }
539
540 silead_ts_set_power(client, SILEAD_POWER_OFF);
541 silead_ts_set_power(client, SILEAD_POWER_ON);
542
543 error = silead_ts_get_id(client);
544 if (error) {
545 dev_err(&client->dev, "Chip ID read error %d\n", error);
546 return error;
547 }
548
549 error = silead_ts_init(client);
550 if (error)
551 return error;
552
553 error = silead_ts_reset(client);
554 if (error)
555 return error;
556
557 error = silead_ts_load_fw(client);
558 if (error)
559 return error;
560
561 error = silead_ts_startup(client);
562 if (error)
563 return error;
564
565 status = silead_ts_get_status(client);
566 if (status != SILEAD_STATUS_OK) {
567 dev_err(&client->dev,
568 "Initialization error, status: 0x%X\n", status);
569 return -ENODEV;
570 }
571
572 return 0;
573 }
574
silead_ts_threaded_irq_handler(int irq,void * id)575 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
576 {
577 struct silead_ts_data *data = id;
578 struct i2c_client *client = data->client;
579
580 silead_ts_read_data(client);
581
582 return IRQ_HANDLED;
583 }
584
silead_ts_read_props(struct i2c_client * client)585 static void silead_ts_read_props(struct i2c_client *client)
586 {
587 struct silead_ts_data *data = i2c_get_clientdata(client);
588 struct device *dev = &client->dev;
589 const char *str;
590 int error;
591
592 error = device_property_read_string(dev, "firmware-name", &str);
593 if (!error)
594 snprintf(data->fw_name, sizeof(data->fw_name),
595 "silead/%s", str);
596 else
597 dev_dbg(dev, "Firmware file name read error. Using default.");
598
599 data->pen_supported = device_property_read_bool(dev, "silead,pen-supported");
600 device_property_read_u32(dev, "silead,pen-resolution-x", &data->pen_x_res);
601 device_property_read_u32(dev, "silead,pen-resolution-y", &data->pen_y_res);
602 }
603
604 #ifdef CONFIG_ACPI
silead_ts_set_default_fw_name(struct silead_ts_data * data,const struct i2c_device_id * id)605 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
606 const struct i2c_device_id *id)
607 {
608 const struct acpi_device_id *acpi_id;
609 struct device *dev = &data->client->dev;
610 int i;
611
612 if (ACPI_HANDLE(dev)) {
613 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
614 if (!acpi_id)
615 return -ENODEV;
616
617 snprintf(data->fw_name, sizeof(data->fw_name),
618 "silead/%s.fw", acpi_id->id);
619
620 for (i = 0; i < strlen(data->fw_name); i++)
621 data->fw_name[i] = tolower(data->fw_name[i]);
622 } else {
623 snprintf(data->fw_name, sizeof(data->fw_name),
624 "silead/%s.fw", id->name);
625 }
626
627 return 0;
628 }
629 #else
silead_ts_set_default_fw_name(struct silead_ts_data * data,const struct i2c_device_id * id)630 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
631 const struct i2c_device_id *id)
632 {
633 snprintf(data->fw_name, sizeof(data->fw_name),
634 "silead/%s.fw", id->name);
635 return 0;
636 }
637 #endif
638
silead_disable_regulator(void * arg)639 static void silead_disable_regulator(void *arg)
640 {
641 struct silead_ts_data *data = arg;
642
643 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
644 }
645
silead_ts_probe(struct i2c_client * client)646 static int silead_ts_probe(struct i2c_client *client)
647 {
648 const struct i2c_device_id *id = i2c_client_get_device_id(client);
649 struct silead_ts_data *data;
650 struct device *dev = &client->dev;
651 int error;
652
653 if (!i2c_check_functionality(client->adapter,
654 I2C_FUNC_I2C |
655 I2C_FUNC_SMBUS_READ_I2C_BLOCK |
656 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
657 dev_err(dev, "I2C functionality check failed\n");
658 return -ENXIO;
659 }
660
661 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
662 if (!data)
663 return -ENOMEM;
664
665 i2c_set_clientdata(client, data);
666 data->client = client;
667
668 error = silead_ts_set_default_fw_name(data, id);
669 if (error)
670 return error;
671
672 silead_ts_read_props(client);
673
674 /* We must have the IRQ provided by DT or ACPI subsystem */
675 if (client->irq <= 0)
676 return -ENODEV;
677
678 data->regulators[0].supply = "vddio";
679 data->regulators[1].supply = "avdd";
680 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
681 data->regulators);
682 if (error)
683 return error;
684
685 /*
686 * Enable regulators at probe and disable them at remove, we need
687 * to keep the chip powered otherwise it forgets its firmware.
688 */
689 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
690 data->regulators);
691 if (error)
692 return error;
693
694 error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
695 if (error)
696 return error;
697
698 /* Power GPIO pin */
699 data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
700 if (IS_ERR(data->gpio_power))
701 return dev_err_probe(dev, PTR_ERR(data->gpio_power),
702 "Shutdown GPIO request failed\n");
703
704 error = silead_ts_setup(client);
705 if (error)
706 return error;
707
708 error = silead_ts_request_input_dev(data);
709 if (error)
710 return error;
711
712 error = silead_ts_request_pen_input_dev(data);
713 if (error)
714 return error;
715
716 error = devm_request_threaded_irq(dev, client->irq,
717 NULL, silead_ts_threaded_irq_handler,
718 IRQF_ONESHOT, client->name, data);
719 if (error) {
720 if (error != -EPROBE_DEFER)
721 dev_err(dev, "IRQ request failed %d\n", error);
722 return error;
723 }
724
725 return 0;
726 }
727
silead_ts_suspend(struct device * dev)728 static int silead_ts_suspend(struct device *dev)
729 {
730 struct i2c_client *client = to_i2c_client(dev);
731
732 disable_irq(client->irq);
733 silead_ts_set_power(client, SILEAD_POWER_OFF);
734 return 0;
735 }
736
silead_ts_resume(struct device * dev)737 static int silead_ts_resume(struct device *dev)
738 {
739 struct i2c_client *client = to_i2c_client(dev);
740 bool second_try = false;
741 int error, status;
742
743 silead_ts_set_power(client, SILEAD_POWER_ON);
744
745 retry:
746 error = silead_ts_reset(client);
747 if (error)
748 return error;
749
750 if (second_try) {
751 error = silead_ts_load_fw(client);
752 if (error)
753 return error;
754 }
755
756 error = silead_ts_startup(client);
757 if (error)
758 return error;
759
760 status = silead_ts_get_status(client);
761 if (status != SILEAD_STATUS_OK) {
762 if (!second_try) {
763 second_try = true;
764 dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
765 goto retry;
766 }
767 dev_err(dev, "Resume error, status: 0x%02x\n", status);
768 return -ENODEV;
769 }
770
771 enable_irq(client->irq);
772
773 return 0;
774 }
775
776 static DEFINE_SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
777
778 static const struct i2c_device_id silead_ts_id[] = {
779 { "gsl1680", 0 },
780 { "gsl1688", 0 },
781 { "gsl3670", 0 },
782 { "gsl3675", 0 },
783 { "gsl3692", 0 },
784 { "mssl1680", 0 },
785 { }
786 };
787 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
788
789 #ifdef CONFIG_ACPI
790 static const struct acpi_device_id silead_ts_acpi_match[] = {
791 { "GSL1680", 0 },
792 { "GSL1688", 0 },
793 { "GSL3670", 0 },
794 { "GSL3675", 0 },
795 { "GSL3692", 0 },
796 { "MSSL1680", 0 },
797 { "MSSL0001", 0 },
798 { "MSSL0002", 0 },
799 { "MSSL0017", 0 },
800 { }
801 };
802 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
803 #endif
804
805 #ifdef CONFIG_OF
806 static const struct of_device_id silead_ts_of_match[] = {
807 { .compatible = "silead,gsl1680" },
808 { .compatible = "silead,gsl1688" },
809 { .compatible = "silead,gsl3670" },
810 { .compatible = "silead,gsl3675" },
811 { .compatible = "silead,gsl3692" },
812 { },
813 };
814 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
815 #endif
816
817 static struct i2c_driver silead_ts_driver = {
818 .probe = silead_ts_probe,
819 .id_table = silead_ts_id,
820 .driver = {
821 .name = SILEAD_TS_NAME,
822 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
823 .of_match_table = of_match_ptr(silead_ts_of_match),
824 .pm = pm_sleep_ptr(&silead_ts_pm),
825 },
826 };
827 module_i2c_driver(silead_ts_driver);
828
829 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
830 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
831 MODULE_LICENSE("GPL");
832