1
2
3
4
5
6
7
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/irq.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/unaligned.h>
27
28#define SILEAD_TS_NAME "silead_ts"
29
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
38
39#define SILEAD_STATUS_OK 0x5A5A5A5A
40#define SILEAD_TS_DATA_LEN 44
41#define SILEAD_CLOCK 0x04
42
43#define SILEAD_CMD_RESET 0x88
44#define SILEAD_CMD_START 0x00
45
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
52
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
57
58#define SILEAD_MAX_FINGERS 10
59
60enum silead_ts_power {
61 SILEAD_POWER_ON = 1,
62 SILEAD_POWER_OFF = 0
63};
64
65struct 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];
77};
78
79struct silead_fw_data {
80 u32 offset;
81 u32 val;
82};
83
84static int silead_ts_request_input_dev(struct silead_ts_data *data)
85{
86 struct device *dev = &data->client->dev;
87 int error;
88
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;
94 }
95
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);
99
100 input_mt_init_slots(data->input, data->max_fingers,
101 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
102 INPUT_MT_TRACK);
103
104 if (device_property_read_bool(dev, "silead,home-button"))
105 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
106
107 data->input->name = SILEAD_TS_NAME;
108 data->input->phys = "input/ts";
109 data->input->id.bustype = BUS_I2C;
110
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;
115 }
116
117 return 0;
118}
119
120static void silead_ts_set_power(struct i2c_client *client,
121 enum silead_ts_power state)
122{
123 struct silead_ts_data *data = i2c_get_clientdata(client);
124
125 if (data->gpio_power) {
126 gpiod_set_value_cansleep(data->gpio_power, state);
127 msleep(SILEAD_POWER_SLEEP);
128 }
129}
130
131static void silead_ts_read_data(struct i2c_client *client)
132{
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;
139
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;
145 }
146
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;
151 }
152
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;
158
159 if (softbutton) {
160
161
162
163
164
165 if (softbutton == 0x01)
166 softbutton_pressed = true;
167
168 continue;
169 }
170
171
172
173
174
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++;
181 }
182
183 input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
184
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);
190
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]);
193 }
194
195 input_mt_sync_frame(input);
196 input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
197 input_sync(input);
198}
199
200static int silead_ts_init(struct i2c_client *client)
201{
202 struct silead_ts_data *data = i2c_get_clientdata(client);
203 int error;
204
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);
210
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);
216
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);
222
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);
228
229 return 0;
230
231i2c_write_err:
232 dev_err(&client->dev, "Registers clear error %d\n", error);
233 return error;
234}
235
236static int silead_ts_reset(struct i2c_client *client)
237{
238 int error;
239
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);
245
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);
251
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);
257
258 return 0;
259
260i2c_write_err:
261 dev_err(&client->dev, "Chip reset error %d\n", error);
262 return error;
263}
264
265static int silead_ts_startup(struct i2c_client *client)
266{
267 int error;
268
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;
273 }
274
275 msleep(SILEAD_STARTUP_SLEEP);
276
277 return 0;
278}
279
280static int silead_ts_load_fw(struct i2c_client *client)
281{
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;
288
289 dev_dbg(dev, "Firmware file name: %s", data->fw_name);
290
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;
295 }
296
297 fw_size = fw->size / sizeof(*fw_data);
298 fw_data = (struct silead_fw_data *)fw->data;
299
300 for (i = 0; i < fw_size; i++) {
301 error = i2c_smbus_write_i2c_block_data(client,
302 fw_data[i].offset,
303 4,
304 (u8 *)&fw_data[i].val);
305 if (error) {
306 dev_err(dev, "Firmware load error %d\n", error);
307 break;
308 }
309 }
310
311 release_firmware(fw);
312 return error ?: 0;
313}
314
315static u32 silead_ts_get_status(struct i2c_client *client)
316{
317 int error;
318 __le32 status;
319
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;
325 }
326
327 return le32_to_cpu(status);
328}
329
330static int silead_ts_get_id(struct i2c_client *client)
331{
332 struct silead_ts_data *data = i2c_get_clientdata(client);
333 __le32 chip_id;
334 int error;
335
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;
341 }
342
343 data->chip_id = le32_to_cpu(chip_id);
344 dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
345
346 return 0;
347}
348
349static int silead_ts_setup(struct i2c_client *client)
350{
351 int error;
352 u32 status;
353
354 silead_ts_set_power(client, SILEAD_POWER_OFF);
355 silead_ts_set_power(client, SILEAD_POWER_ON);
356
357 error = silead_ts_get_id(client);
358 if (error)
359 return error;
360
361 error = silead_ts_init(client);
362 if (error)
363 return error;
364
365 error = silead_ts_reset(client);
366 if (error)
367 return error;
368
369 error = silead_ts_load_fw(client);
370 if (error)
371 return error;
372
373 error = silead_ts_startup(client);
374 if (error)
375 return error;
376
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;
382 }
383
384 return 0;
385}
386
387static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
388{
389 struct silead_ts_data *data = id;
390 struct i2c_client *client = data->client;
391
392 silead_ts_read_data(client);
393
394 return IRQ_HANDLED;
395}
396
397static void silead_ts_read_props(struct i2c_client *client)
398{
399 struct silead_ts_data *data = i2c_get_clientdata(client);
400 struct device *dev = &client->dev;
401 const char *str;
402 int error;
403
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;
409 }
410
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.");
417}
418
419#ifdef CONFIG_ACPI
420static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
421 const struct i2c_device_id *id)
422{
423 const struct acpi_device_id *acpi_id;
424 struct device *dev = &data->client->dev;
425 int i;
426
427 if (ACPI_HANDLE(dev)) {
428 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
429 if (!acpi_id)
430 return -ENODEV;
431
432 snprintf(data->fw_name, sizeof(data->fw_name),
433 "silead/%s.fw", acpi_id->id);
434
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);
440 }
441
442 return 0;
443}
444#else
445static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
446 const struct i2c_device_id *id)
447{
448 snprintf(data->fw_name, sizeof(data->fw_name),
449 "silead/%s.fw", id->name);
450 return 0;
451}
452#endif
453
454static void silead_disable_regulator(void *arg)
455{
456 struct silead_ts_data *data = arg;
457
458 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
459}
460
461static int silead_ts_probe(struct i2c_client *client,
462 const struct i2c_device_id *id)
463{
464 struct silead_ts_data *data;
465 struct device *dev = &client->dev;
466 int error;
467
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;
474 }
475
476 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
477 if (!data)
478 return -ENOMEM;
479
480 i2c_set_clientdata(client, data);
481 data->client = client;
482
483 error = silead_ts_set_default_fw_name(data, id);
484 if (error)
485 return error;
486
487 silead_ts_read_props(client);
488
489
490 if (client->irq <= 0)
491 return -ENODEV;
492
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;
499
500
501
502
503
504 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
505 data->regulators);
506 if (error)
507 return error;
508
509 error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
510 if (error)
511 return error;
512
513
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);
519 }
520
521 error = silead_ts_setup(client);
522 if (error)
523 return error;
524
525 error = silead_ts_request_input_dev(data);
526 if (error)
527 return error;
528
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;
536 }
537
538 return 0;
539}
540
541static int __maybe_unused silead_ts_suspend(struct device *dev)
542{
543 struct i2c_client *client = to_i2c_client(dev);
544
545 disable_irq(client->irq);
546 silead_ts_set_power(client, SILEAD_POWER_OFF);
547 return 0;
548}
549
550static int __maybe_unused silead_ts_resume(struct device *dev)
551{
552 struct i2c_client *client = to_i2c_client(dev);
553 bool second_try = false;
554 int error, status;
555
556 silead_ts_set_power(client, SILEAD_POWER_ON);
557
558 retry:
559 error = silead_ts_reset(client);
560 if (error)
561 return error;
562
563 if (second_try) {
564 error = silead_ts_load_fw(client);
565 if (error)
566 return error;
567 }
568
569 error = silead_ts_startup(client);
570 if (error)
571 return error;
572
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;
579 }
580 dev_err(dev, "Resume error, status: 0x%02x\n", status);
581 return -ENODEV;
582 }
583
584 enable_irq(client->irq);
585
586 return 0;
587}
588
589static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
590
591static 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 },
598 { }
599};
600MODULE_DEVICE_TABLE(i2c, silead_ts_id);
601
602#ifdef CONFIG_ACPI
603static 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 },
613 { }
614};
615MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
616#endif
617
618#ifdef CONFIG_OF
619static 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 { },
626};
627MODULE_DEVICE_TABLE(of, silead_ts_of_match);
628#endif
629
630static 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,
638 },
639};
640module_i2c_driver(silead_ts_driver);
641
642MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
643MODULE_DESCRIPTION("Silead I2C touchscreen driver");
644MODULE_LICENSE("GPL");
645