Linux Audio

Check our new training course

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