1 /* -------------------------------------------------------------------------
2  * Copyright (C) 2014-2015, Intel Corporation
3  *
4  * Derived from:
5  *  gslX68X.c
6  *  Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
7  *
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.
12  *
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  * -------------------------------------------------------------------------
18  */
19 
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>
30 #include <linux/pm.h>
31 #include <linux/irq.h>
32 #include <linux/regulator/consumer.h>
33 
34 #include <asm/unaligned.h>
35 
36 #define SILEAD_TS_NAME		"silead_ts"
37 
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
46 
47 #define SILEAD_STATUS_OK	0x5A5A5A5A
48 #define SILEAD_TS_DATA_LEN	44
49 #define SILEAD_CLOCK		0x04
50 
51 #define SILEAD_CMD_RESET	0x88
52 #define SILEAD_CMD_START	0x00
53 
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_EXTRA_DATA_MASK	0xF0
60 
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
65 
66 #define SILEAD_MAX_FINGERS	10
67 
68 enum silead_ts_power {
69 	SILEAD_POWER_ON  = 1,
70 	SILEAD_POWER_OFF = 0
71 };
72 
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];
78 	char fw_name[64];
79 	struct touchscreen_properties prop;
80 	u32 max_fingers;
81 	u32 chip_id;
82 	struct input_mt_pos pos[SILEAD_MAX_FINGERS];
83 	int slots[SILEAD_MAX_FINGERS];
84 	int id[SILEAD_MAX_FINGERS];
85 };
86 
87 struct silead_fw_data {
88 	u32 offset;
89 	u32 val;
90 };
91 
92 static int silead_ts_request_input_dev(struct silead_ts_data *data)
93 {
94 	struct device *dev = &data->client->dev;
95 	int error;
96 
97 	data->input = devm_input_allocate_device(dev);
98 	if (!data->input) {
99 		dev_err(dev,
100 			"Failed to allocate input device\n");
101 		return -ENOMEM;
102 	}
103 
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);
107 
108 	input_mt_init_slots(data->input, data->max_fingers,
109 			    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
110 			    INPUT_MT_TRACK);
111 
112 	if (device_property_read_bool(dev, "silead,home-button"))
113 		input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
114 
115 	data->input->name = SILEAD_TS_NAME;
116 	data->input->phys = "input/ts";
117 	data->input->id.bustype = BUS_I2C;
118 
119 	error = input_register_device(data->input);
120 	if (error) {
121 		dev_err(dev, "Failed to register input device: %d\n", error);
122 		return error;
123 	}
124 
125 	return 0;
126 }
127 
128 static void silead_ts_set_power(struct i2c_client *client,
129 				enum silead_ts_power state)
130 {
131 	struct silead_ts_data *data = i2c_get_clientdata(client);
132 
133 	if (data->gpio_power) {
134 		gpiod_set_value_cansleep(data->gpio_power, state);
135 		msleep(SILEAD_POWER_SLEEP);
136 	}
137 }
138 
139 static void silead_ts_read_data(struct i2c_client *client)
140 {
141 	struct silead_ts_data *data = i2c_get_clientdata(client);
142 	struct input_dev *input = data->input;
143 	struct device *dev = &client->dev;
144 	u8 *bufp, buf[SILEAD_TS_DATA_LEN];
145 	int touch_nr, softbutton, error, i;
146 	bool softbutton_pressed = false;
147 
148 	error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
149 					      SILEAD_TS_DATA_LEN, buf);
150 	if (error < 0) {
151 		dev_err(dev, "Data read error %d\n", error);
152 		return;
153 	}
154 
155 	if (buf[0] > data->max_fingers) {
156 		dev_warn(dev, "More touches reported then supported %d > %d\n",
157 			 buf[0], data->max_fingers);
158 		buf[0] = data->max_fingers;
159 	}
160 
161 	touch_nr = 0;
162 	bufp = buf + SILEAD_POINT_DATA_LEN;
163 	for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
164 		softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
165 			      SILEAD_EXTRA_DATA_MASK) >> 4;
166 
167 		if (softbutton) {
168 			/*
169 			 * For now only respond to softbutton == 0x01, some
170 			 * tablets *without* a capacative button send 0x04
171 			 * when crossing the edges of the screen.
172 			 */
173 			if (softbutton == 0x01)
174 				softbutton_pressed = true;
175 
176 			continue;
177 		}
178 
179 		/*
180 		 * Bits 4-7 are the touch id, note not all models have
181 		 * hardware touch ids so atm we don't use these.
182 		 */
183 		data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
184 				      SILEAD_EXTRA_DATA_MASK) >> 4;
185 		touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
186 			get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
187 			get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
188 		touch_nr++;
189 	}
190 
191 	input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
192 
193 	for (i = 0; i < touch_nr; i++) {
194 		input_mt_slot(input, data->slots[i]);
195 		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
196 		input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
197 		input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
198 
199 		dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
200 			data->pos[i].y, data->id[i], data->slots[i]);
201 	}
202 
203 	input_mt_sync_frame(input);
204 	input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
205 	input_sync(input);
206 }
207 
208 static int silead_ts_init(struct i2c_client *client)
209 {
210 	struct silead_ts_data *data = i2c_get_clientdata(client);
211 	int error;
212 
213 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
214 					  SILEAD_CMD_RESET);
215 	if (error)
216 		goto i2c_write_err;
217 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
218 
219 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
220 					data->max_fingers);
221 	if (error)
222 		goto i2c_write_err;
223 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
224 
225 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
226 					  SILEAD_CLOCK);
227 	if (error)
228 		goto i2c_write_err;
229 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
230 
231 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
232 					  SILEAD_CMD_START);
233 	if (error)
234 		goto i2c_write_err;
235 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
236 
237 	return 0;
238 
239 i2c_write_err:
240 	dev_err(&client->dev, "Registers clear error %d\n", error);
241 	return error;
242 }
243 
244 static int silead_ts_reset(struct i2c_client *client)
245 {
246 	int error;
247 
248 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
249 					  SILEAD_CMD_RESET);
250 	if (error)
251 		goto i2c_write_err;
252 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
253 
254 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
255 					  SILEAD_CLOCK);
256 	if (error)
257 		goto i2c_write_err;
258 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
259 
260 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
261 					  SILEAD_CMD_START);
262 	if (error)
263 		goto i2c_write_err;
264 	usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
265 
266 	return 0;
267 
268 i2c_write_err:
269 	dev_err(&client->dev, "Chip reset error %d\n", error);
270 	return error;
271 }
272 
273 static int silead_ts_startup(struct i2c_client *client)
274 {
275 	int error;
276 
277 	error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
278 	if (error) {
279 		dev_err(&client->dev, "Startup error %d\n", error);
280 		return error;
281 	}
282 
283 	msleep(SILEAD_STARTUP_SLEEP);
284 
285 	return 0;
286 }
287 
288 static int silead_ts_load_fw(struct i2c_client *client)
289 {
290 	struct device *dev = &client->dev;
291 	struct silead_ts_data *data = i2c_get_clientdata(client);
292 	unsigned int fw_size, i;
293 	const struct firmware *fw;
294 	struct silead_fw_data *fw_data;
295 	int error;
296 
297 	dev_dbg(dev, "Firmware file name: %s", data->fw_name);
298 
299 	error = request_firmware(&fw, data->fw_name, dev);
300 	if (error) {
301 		dev_err(dev, "Firmware request error %d\n", error);
302 		return error;
303 	}
304 
305 	fw_size = fw->size / sizeof(*fw_data);
306 	fw_data = (struct silead_fw_data *)fw->data;
307 
308 	for (i = 0; i < fw_size; i++) {
309 		error = i2c_smbus_write_i2c_block_data(client,
310 						       fw_data[i].offset,
311 						       4,
312 						       (u8 *)&fw_data[i].val);
313 		if (error) {
314 			dev_err(dev, "Firmware load error %d\n", error);
315 			break;
316 		}
317 	}
318 
319 	release_firmware(fw);
320 	return error ?: 0;
321 }
322 
323 static u32 silead_ts_get_status(struct i2c_client *client)
324 {
325 	int error;
326 	__le32 status;
327 
328 	error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
329 					      sizeof(status), (u8 *)&status);
330 	if (error < 0) {
331 		dev_err(&client->dev, "Status read error %d\n", error);
332 		return error;
333 	}
334 
335 	return le32_to_cpu(status);
336 }
337 
338 static int silead_ts_get_id(struct i2c_client *client)
339 {
340 	struct silead_ts_data *data = i2c_get_clientdata(client);
341 	__le32 chip_id;
342 	int error;
343 
344 	error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
345 					      sizeof(chip_id), (u8 *)&chip_id);
346 	if (error < 0) {
347 		dev_err(&client->dev, "Chip ID read error %d\n", error);
348 		return error;
349 	}
350 
351 	data->chip_id = le32_to_cpu(chip_id);
352 	dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
353 
354 	return 0;
355 }
356 
357 static int silead_ts_setup(struct i2c_client *client)
358 {
359 	int error;
360 	u32 status;
361 
362 	silead_ts_set_power(client, SILEAD_POWER_OFF);
363 	silead_ts_set_power(client, SILEAD_POWER_ON);
364 
365 	error = silead_ts_get_id(client);
366 	if (error)
367 		return error;
368 
369 	error = silead_ts_init(client);
370 	if (error)
371 		return error;
372 
373 	error = silead_ts_reset(client);
374 	if (error)
375 		return error;
376 
377 	error = silead_ts_load_fw(client);
378 	if (error)
379 		return error;
380 
381 	error = silead_ts_startup(client);
382 	if (error)
383 		return error;
384 
385 	status = silead_ts_get_status(client);
386 	if (status != SILEAD_STATUS_OK) {
387 		dev_err(&client->dev,
388 			"Initialization error, status: 0x%X\n", status);
389 		return -ENODEV;
390 	}
391 
392 	return 0;
393 }
394 
395 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
396 {
397 	struct silead_ts_data *data = id;
398 	struct i2c_client *client = data->client;
399 
400 	silead_ts_read_data(client);
401 
402 	return IRQ_HANDLED;
403 }
404 
405 static void silead_ts_read_props(struct i2c_client *client)
406 {
407 	struct silead_ts_data *data = i2c_get_clientdata(client);
408 	struct device *dev = &client->dev;
409 	const char *str;
410 	int error;
411 
412 	error = device_property_read_u32(dev, "silead,max-fingers",
413 					 &data->max_fingers);
414 	if (error) {
415 		dev_dbg(dev, "Max fingers read error %d\n", error);
416 		data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
417 	}
418 
419 	error = device_property_read_string(dev, "firmware-name", &str);
420 	if (!error)
421 		snprintf(data->fw_name, sizeof(data->fw_name),
422 			 "silead/%s", str);
423 	else
424 		dev_dbg(dev, "Firmware file name read error. Using default.");
425 }
426 
427 #ifdef CONFIG_ACPI
428 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
429 					 const struct i2c_device_id *id)
430 {
431 	const struct acpi_device_id *acpi_id;
432 	struct device *dev = &data->client->dev;
433 	int i;
434 
435 	if (ACPI_HANDLE(dev)) {
436 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
437 		if (!acpi_id)
438 			return -ENODEV;
439 
440 		snprintf(data->fw_name, sizeof(data->fw_name),
441 			 "silead/%s.fw", acpi_id->id);
442 
443 		for (i = 0; i < strlen(data->fw_name); i++)
444 			data->fw_name[i] = tolower(data->fw_name[i]);
445 	} else {
446 		snprintf(data->fw_name, sizeof(data->fw_name),
447 			 "silead/%s.fw", id->name);
448 	}
449 
450 	return 0;
451 }
452 #else
453 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
454 					 const struct i2c_device_id *id)
455 {
456 	snprintf(data->fw_name, sizeof(data->fw_name),
457 		 "silead/%s.fw", id->name);
458 	return 0;
459 }
460 #endif
461 
462 static void silead_disable_regulator(void *arg)
463 {
464 	struct silead_ts_data *data = arg;
465 
466 	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
467 }
468 
469 static int silead_ts_probe(struct i2c_client *client,
470 			   const struct i2c_device_id *id)
471 {
472 	struct silead_ts_data *data;
473 	struct device *dev = &client->dev;
474 	int error;
475 
476 	if (!i2c_check_functionality(client->adapter,
477 				     I2C_FUNC_I2C |
478 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK |
479 				     I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
480 		dev_err(dev, "I2C functionality check failed\n");
481 		return -ENXIO;
482 	}
483 
484 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
485 	if (!data)
486 		return -ENOMEM;
487 
488 	i2c_set_clientdata(client, data);
489 	data->client = client;
490 
491 	error = silead_ts_set_default_fw_name(data, id);
492 	if (error)
493 		return error;
494 
495 	silead_ts_read_props(client);
496 
497 	/* We must have the IRQ provided by DT or ACPI subsytem */
498 	if (client->irq <= 0)
499 		return -ENODEV;
500 
501 	data->regulators[0].supply = "vddio";
502 	data->regulators[1].supply = "avdd";
503 	error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
504 					data->regulators);
505 	if (error)
506 		return error;
507 
508 	/*
509 	 * Enable regulators at probe and disable them at remove, we need
510 	 * to keep the chip powered otherwise it forgets its firmware.
511 	 */
512 	error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
513 				      data->regulators);
514 	if (error)
515 		return error;
516 
517 	error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
518 	if (error)
519 		return error;
520 
521 	/* Power GPIO pin */
522 	data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
523 	if (IS_ERR(data->gpio_power)) {
524 		if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
525 			dev_err(dev, "Shutdown GPIO request failed\n");
526 		return PTR_ERR(data->gpio_power);
527 	}
528 
529 	error = silead_ts_setup(client);
530 	if (error)
531 		return error;
532 
533 	error = silead_ts_request_input_dev(data);
534 	if (error)
535 		return error;
536 
537 	error = devm_request_threaded_irq(dev, client->irq,
538 					  NULL, silead_ts_threaded_irq_handler,
539 					  IRQF_ONESHOT, client->name, data);
540 	if (error) {
541 		if (error != -EPROBE_DEFER)
542 			dev_err(dev, "IRQ request failed %d\n", error);
543 		return error;
544 	}
545 
546 	return 0;
547 }
548 
549 static int __maybe_unused silead_ts_suspend(struct device *dev)
550 {
551 	struct i2c_client *client = to_i2c_client(dev);
552 
553 	disable_irq(client->irq);
554 	silead_ts_set_power(client, SILEAD_POWER_OFF);
555 	return 0;
556 }
557 
558 static int __maybe_unused silead_ts_resume(struct device *dev)
559 {
560 	struct i2c_client *client = to_i2c_client(dev);
561 	bool second_try = false;
562 	int error, status;
563 
564 	silead_ts_set_power(client, SILEAD_POWER_ON);
565 
566  retry:
567 	error = silead_ts_reset(client);
568 	if (error)
569 		return error;
570 
571 	if (second_try) {
572 		error = silead_ts_load_fw(client);
573 		if (error)
574 			return error;
575 	}
576 
577 	error = silead_ts_startup(client);
578 	if (error)
579 		return error;
580 
581 	status = silead_ts_get_status(client);
582 	if (status != SILEAD_STATUS_OK) {
583 		if (!second_try) {
584 			second_try = true;
585 			dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
586 			goto retry;
587 		}
588 		dev_err(dev, "Resume error, status: 0x%02x\n", status);
589 		return -ENODEV;
590 	}
591 
592 	enable_irq(client->irq);
593 
594 	return 0;
595 }
596 
597 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
598 
599 static const struct i2c_device_id silead_ts_id[] = {
600 	{ "gsl1680", 0 },
601 	{ "gsl1688", 0 },
602 	{ "gsl3670", 0 },
603 	{ "gsl3675", 0 },
604 	{ "gsl3692", 0 },
605 	{ "mssl1680", 0 },
606 	{ }
607 };
608 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
609 
610 #ifdef CONFIG_ACPI
611 static const struct acpi_device_id silead_ts_acpi_match[] = {
612 	{ "GSL1680", 0 },
613 	{ "GSL1688", 0 },
614 	{ "GSL3670", 0 },
615 	{ "GSL3675", 0 },
616 	{ "GSL3692", 0 },
617 	{ "MSSL1680", 0 },
618 	{ "MSSL0001", 0 },
619 	{ "MSSL0002", 0 },
620 	{ }
621 };
622 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
623 #endif
624 
625 #ifdef CONFIG_OF
626 static const struct of_device_id silead_ts_of_match[] = {
627 	{ .compatible = "silead,gsl1680" },
628 	{ .compatible = "silead,gsl1688" },
629 	{ .compatible = "silead,gsl3670" },
630 	{ .compatible = "silead,gsl3675" },
631 	{ .compatible = "silead,gsl3692" },
632 	{ },
633 };
634 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
635 #endif
636 
637 static struct i2c_driver silead_ts_driver = {
638 	.probe = silead_ts_probe,
639 	.id_table = silead_ts_id,
640 	.driver = {
641 		.name = SILEAD_TS_NAME,
642 		.acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
643 		.of_match_table = of_match_ptr(silead_ts_of_match),
644 		.pm = &silead_ts_pm,
645 	},
646 };
647 module_i2c_driver(silead_ts_driver);
648 
649 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
650 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
651 MODULE_LICENSE("GPL");
652