Skip to contentSkip to conversation

input: cyapa: instruction of cyapa patches

PATCH Applied Dudley Du 3 participants 32 messages Nov 3, 2014 – Dec 5, 2014

Subsystems (2)

v9 of 15 Applied Latest is v15 →

lore
Share & tools
lore

Dudley Du

author
#0 lore raw

PATCH v9 1/18 input: cyapa: instruction of cyapa patches

V9 patches have below updates compared with v8 patches:
1) Removed all async thread stuff from the driver.
2) Split driver into 18 patches for each function change one patch.

V8 patches have below updates compared with v7 patches:
1) [PATCH v8 01/13] - Remove the async thread for device detect in probe routine, now the device detect process is completely done within the device probe routine.
2) [PATCH v8 01/13] - Split the irq cmd hander function to separated function cyapa_default_irq_cmd_handler() and set it to interface cyapa_default_ops.irq_cmd_handler.
3) [PATCH v8 06/13] - Add cyapa->gen check in cyapa_gen3_irq_cmd_handler() to avoid miss-enter when device protocol is still in detecting.

V7 patches have below updates compared with v6 patches:
1) [PATCH v7 01/13] - Split the irq cmd hander function to separated function cyapa_default_irq_cmd_handler() and set it to interface cyapa_default_ops.irq_cmd_handler.
2) [PATCH v7 06/13] - Add cyapa->gen check in cyapa_gen3_irq_cmd_handler() to avoid miss-enter when device protocol is still in detecting.

V6 patches have below updates compared with v5 patches:
1) Remove patch 14 of the lid filtering from the cyapa driver.

V5 patches have below updates compared with v4 patches:
1) Uses get_device()/put_device() instead of kobject_get()/kobject_put();
2) Fix memories freed before debugfs entries issue;
3) Make cyapa_debugs_root valid in driver module level
in module_init()/moudle_exit() ;
4) Fix i2c_transfer() may return partial transfer issues.
5) Add cyapa->removed flag to avoid detecting thread may still running when driver module is removed.
6) Fix the meanings of some comments and return error code not clear issue.

This patch set is aimed to re-design the cyapa driver to support old gen3 trackpad devices and new gen5 trackpad devices in one cyapa driver, it's for easily productions support based on customers' requirements. And add sysfs functions and interfaces supported that required by users and customers.

Since the earlier gen3 and the latest gen5 trackpad devices using two different chipsets, and have different protocols and interfaces, so if supported these two type trackpad devices in two different drivers, then it will be difficult to manage productions and later firmware updates. e.g.: It will cause customer don't know which one trackpad device firmware image to use and update when it has been used and integrated
in same one productions, so here we support these two trackpad devices in same on driver.

The new design cyapa driver contains:
cyapa.c - the core of the re-design, supply interfaces and functions to system and read trackpad devices.
cyapa.h - header file including macros and data structure definitions. cyapa_gen3.c - functions support for gen3 trackpad devices, cyapa_gen5.c - functions support for gen5 trackpad devices.

Beside this introduction patch, it has 18 patches listed as below. For these patches each one is patched based on previous one.

patch 1/18: add device resource management infrastructure supported.

patch 2/18: re-design cyapa driver with core functions and interface to support multi-type trackpad devices.

patch 3/18: add gen3 trackpad device basic functions supported into the re-design cyapa driver.

patch 4/18: add gen5 trackpad device basic functions supported into the re-design cyapa driver.

patch 5/18: add power management interfaces supported for the deivce.

patch 6/18: add runtime power management interfaces supported for the device.

patch 7/18: add sysfs interfaces supported in the cyapa driver. Including read firmware version, get production ID, read baseline, re-calibrate trackpad baselines and do trackpad firmware update.

patch 8/18: add gen3 trackpad device's firmware update function supported.

patch 9/18: add gen3 trackpad device's read baseline function supported.

patch 10/18: add gen3 trackpad device's force re-calibrate function supported.

patch 11/18: add gen5 trackpad device's firmware update function supported.

patch 12/18: add gen5 trackpad device's read baseline function supported.

patch 13/18: add gen5 trackpad device's force re-calibrate function.

patch 14/18: add read firmware image debugfs interface supported in the cyapa driver.

patch 15/18: add gen3 trackpad device's read firmware image function supported.

patch 16/18: add gen5 trackpad device's read firmware image function supported.

patch 17/18: add read trackpad device's sensors' raw data debugfs interface supported in the cyapa driver.

patch 18/18: add gen5 trackpad device's read read raw function supported.

author
#1 lore raw

PATCH v9 1/18 input: cyapa: add device resource management infrastructure support

Modify cyapa driver to support device resource management infrastructure to reduce the mistakes of resource management.
TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 48 ++++++++++++++++++---------------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
drivers/input/mouse/cyapa.c +19 -29
index b409c3d..b3d7a2a 100644
@@ -409,11 +409,11 @@ static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
 		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
 		len = cyapa_smbus_cmds[cmd_idx].len;
 		return cyapa_smbus_read_block(cyapa, cmd, len, values);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-		len = cyapa_i2c_cmds[cmd_idx].len;
-		return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
 	}
+
+	cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+	len = cyapa_i2c_cmds[cmd_idx].len;
+	return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
 }
 
 /*
@@ -762,7 +762,7 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
 		return -EINVAL;
 
-	input = cyapa->input = input_allocate_device();
+	input = cyapa->input = devm_input_allocate_device(dev);
 	if (!input) {
 		dev_err(dev, "allocate memory for input device failed\n");
 		return -ENOMEM;
@@ -837,11 +837,9 @@ static int cyapa_probe(struct i2c_client *client,
 		return -EIO;
 	}
 
-	cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
-	if (!cyapa) {
-		dev_err(dev, "allocate memory for cyapa failed\n");
+	cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
+	if (!cyapa)
 		return -ENOMEM;
-	}
 
 	cyapa->gen = CYAPA_GEN3;
 	cyapa->client = client;
@@ -856,51 +854,43 @@ static int cyapa_probe(struct i2c_client *client,
 	ret = cyapa_check_is_operational(cyapa);
 	if (ret) {
 		dev_err(dev, "device not operational, %d\n", ret);
-		goto err_mem_free;
+		return ret;
 	}
 
 	ret = cyapa_create_input_dev(cyapa);
 	if (ret) {
 		dev_err(dev, "create input_dev instance failed, %d\n", ret);
-		goto err_mem_free;
+		return ret;
 	}
 
 	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
 	if (ret) {
 		dev_err(dev, "set active power failed, %d\n", ret);
-		goto err_unregister_device;
+		return ret;
 	}
 
 	cyapa->irq = client->irq;
-	ret = request_threaded_irq(cyapa->irq,
-				   NULL,
-				   cyapa_irq,
-				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-				   "cyapa",
-				   cyapa);
+	ret = devm_request_threaded_irq(dev,
+					cyapa->irq,
+					NULL,
+					cyapa_irq,
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					"cyapa",
+					cyapa);
 	if (ret) {
 		dev_err(dev, "IRQ request failed: %d\n, ", ret);
-		goto err_unregister_device;
+		return ret;
 	}
 
 	return 0;
-
-err_unregister_device:
-	input_unregister_device(cyapa->input);
-err_mem_free:
-	kfree(cyapa);
-
-	return ret;
 }
 
 static int cyapa_remove(struct i2c_client *client)
 {
 	struct cyapa *cyapa = i2c_get_clientdata(client);
 
-	free_irq(cyapa->irq, cyapa);
-	input_unregister_device(cyapa->input);
+	disable_irq(cyapa->irq);
 	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
-	kfree(cyapa);
 
 	return 0;
 }
Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH v9 01/18] input: cyapa: add device resource management infrastructure support

Hi Dudley,

Quoted 16 lines · Dudley Du

Modify cyapa driver to support device resource management infrastructure to reduce the mistakes of resource management.
TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 48 ++++++++++++++++++---------------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
drivers/input/mouse/cyapa.c +0 -0
index b409c3d..b3d7a2a 100644
@@ -409,11 +409,11 @@ static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
 		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
 		len = cyapa_smbus_cmds[cmd_idx].len;

drivers/input/mouse/cyapa.c static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values) · line 416

Quoted 10 lines
drivers/input/mouse/cyapa.c +4 -4
 		return cyapa_smbus_read_block(cyapa, cmd, len, values);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-		len = cyapa_i2c_cmds[cmd_idx].len;
-		return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
 	}
+
+	cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+	len = cyapa_i2c_cmds[cmd_idx].len;
+	return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);

I am not sure why you are changing this code, it has nothing to do with managed resources and the original was just fine.

drivers/input/mouse/cyapa.c static int cyapa_create_input_dev(struct cyapa *cyapa) · line 765

Quoted 9 lines
drivers/input/mouse/cyapa.c +1 -1
 }
 
 /*
@@ -762,7 +762,7 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
 		return -EINVAL;
 
-	input = cyapa->input = input_allocate_device();
+	input = cyapa->input = devm_input_allocate_device(dev);

If you are using devm_* then you do not need to manually cal input_free_device() (further down in this fucntion).

Quoted 68 lines
drivers/input/mouse/cyapa.c +13 -21
 	if (!input) {
 		dev_err(dev, "allocate memory for input device failed\n");
 		return -ENOMEM;
@@ -837,11 +837,9 @@ static int cyapa_probe(struct i2c_client *client,
 		return -EIO;
 	}
 
-	cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
-	if (!cyapa) {
-		dev_err(dev, "allocate memory for cyapa failed\n");
+	cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
+	if (!cyapa)
 		return -ENOMEM;
-	}
 
 	cyapa->gen = CYAPA_GEN3;
 	cyapa->client = client;
@@ -856,51 +854,43 @@ static int cyapa_probe(struct i2c_client *client,
 	ret = cyapa_check_is_operational(cyapa);
 	if (ret) {
 		dev_err(dev, "device not operational, %d\n", ret);
-		goto err_mem_free;
+		return ret;
 	}
 
 	ret = cyapa_create_input_dev(cyapa);
 	if (ret) {
 		dev_err(dev, "create input_dev instance failed, %d\n", ret);
-		goto err_mem_free;
+		return ret;
 	}
 
 	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
 	if (ret) {
 		dev_err(dev, "set active power failed, %d\n", ret);
-		goto err_unregister_device;
+		return ret;
 	}
 
 	cyapa->irq = client->irq;
-	ret = request_threaded_irq(cyapa->irq,
-				   NULL,
-				   cyapa_irq,
-				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-				   "cyapa",
-				   cyapa);
+	ret = devm_request_threaded_irq(dev,
+					cyapa->irq,
+					NULL,
+					cyapa_irq,
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					"cyapa",
+					cyapa);
 	if (ret) {
 		dev_err(dev, "IRQ request failed: %d\n, ", ret);
-		goto err_unregister_device;
+		return ret;
 	}
 
 	return 0;
-
-err_unregister_device:
-	input_unregister_device(cyapa->input);
-err_mem_free:
-	kfree(cyapa);
-
-	return ret;
 }

drivers/input/mouse/cyapa.c static int cyapa_probe(struct i2c_client *client, · line 903

Quoted 10 lines
drivers/input/mouse/cyapa.c +1 -3
 
 static int cyapa_remove(struct i2c_client *client)
 {
 	struct cyapa *cyapa = i2c_get_clientdata(client);
 
-	free_irq(cyapa->irq, cyapa);
-	input_unregister_device(cyapa->input);
+	disable_irq(cyapa->irq);
 	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
-	kfree(cyapa);

I refer devm* conversions to completely eradicate ->remove() methods. I took the liberty to edit the patch a bit, does the version below work for you?

Thanks!

Signature · 15 lines
Dmitry


Input: cyapa - switch to using managed resources

From: Dudley Du <[email protected]>

Use of managed resources simplifies error handling and device removal code.

Signed-off-by: Dudley Du <[email protected]>
[Dmitry: added open/close methods so cyapa_remove is no longer needed.]
Signed-off-by: Dmitry Torokhov <[email protected]>
---
 drivers/input/mouse/cyapa.c |  184 +++++++++++++++++++++++++------------------
 1 file changed, 105 insertions(+), 79 deletions(-)
drivers/input/mouse/cyapa.c +105 -79
index 1d978c7..c84a9eb 100644
@@ -577,10 +577,13 @@ static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
 	power = ret & ~PWR_MODE_MASK;
 	power |= power_mode & PWR_MODE_MASK;
 	ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
-	if (ret < 0)
+	if (ret < 0) {
 		dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
 			power_mode, ret);
-	return ret;
+		return ret;
+	}
+
+	return 0;
 }
 
 static int cyapa_get_query_data(struct cyapa *cyapa)
@@ -753,16 +756,40 @@ static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
 	return ret;
 }
 
+static int cyapa_open(struct input_dev *input)
+{
+	struct cyapa *cyapa = input_get_drvdata(input);
+	struct i2c_client *client = cyapa->client;
+	int error;
+
+	error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+	if (error) {
+		dev_err(&client->dev, "set active power failed: %d\n", error);
+		return error;
+	}
+
+	enable_irq(client->irq);
+	return 0;
+}
+
+static void cyapa_close(struct input_dev *input)
+{
+	struct cyapa *cyapa = input_get_drvdata(input);
+
+	disable_irq(cyapa->client->irq);
+	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+}
+
 static int cyapa_create_input_dev(struct cyapa *cyapa)
 {
 	struct device *dev = &cyapa->client->dev;
-	int ret;
 	struct input_dev *input;
+	int error;
 
 	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
 		return -EINVAL;
 
-	input = cyapa->input = input_allocate_device();
+	input = devm_input_allocate_device(dev);
 	if (!input) {
 		dev_err(dev, "allocate memory for input device failed\n");
 		return -ENOMEM;
@@ -775,6 +802,9 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 	input->id.product = 0;  /* means any product in eventcomm. */
 	input->dev.parent = &cyapa->client->dev;
 
+	input->open = cyapa_open;
+	input->close = cyapa_close;
+
 	input_set_drvdata(input, cyapa);
 
 	__set_bit(EV_ABS, input->evbit);
@@ -802,34 +832,24 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 
 	/* handle pointer emulation and unused slots in core */
-	ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
-				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
-	if (ret) {
-		dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
-		goto err_free_device;
+	error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
+				    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+	if (error) {
+		dev_err(dev, "failed to initialize MT slots: %d\n", error);
+		return error;
 	}
 
-	/* Register the device in input subsystem */
-	ret = input_register_device(input);
-	if (ret) {
-		dev_err(dev, "input device register failed, %d\n", ret);
-		goto err_free_device;
-	}
+	cyapa->input = input;
 	return 0;
-
-err_free_device:
-	input_free_device(input);
-	cyapa->input = NULL;
-	return ret;
 }
 
 static int cyapa_probe(struct i2c_client *client,
 		       const struct i2c_device_id *dev_id)
 {
-	int ret;
-	u8 adapter_func;
-	struct cyapa *cyapa;
 	struct device *dev = &client->dev;
+	struct cyapa *cyapa;
+	u8 adapter_func;
+	int error;
 
 	adapter_func = cyapa_check_adapter_functionality(client);
 	if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
@@ -837,11 +857,9 @@ static int cyapa_probe(struct i2c_client *client,
 		return -EIO;
 	}
 
-	cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
-	if (!cyapa) {
-		dev_err(dev, "allocate memory for cyapa failed\n");
+	cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
+	if (!cyapa)
 		return -ENOMEM;
-	}
 
 	cyapa->gen = CYAPA_GEN3;
 	cyapa->client = client;
@@ -852,66 +870,61 @@ static int cyapa_probe(struct i2c_client *client,
 	/* i2c isn't supported, use smbus */
 	if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
 		cyapa->smbus = true;
+
 	cyapa->state = CYAPA_STATE_NO_DEVICE;
-	ret = cyapa_check_is_operational(cyapa);
-	if (ret) {
-		dev_err(dev, "device not operational, %d\n", ret);
-		goto err_mem_free;
-	}
 
-	ret = cyapa_create_input_dev(cyapa);
-	if (ret) {
-		dev_err(dev, "create input_dev instance failed, %d\n", ret);
-		goto err_mem_free;
+	error = cyapa_check_is_operational(cyapa);
+	if (error) {
+		dev_err(dev, "device not operational, %d\n", error);
+		return error;
 	}
 
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-	if (ret) {
-		dev_err(dev, "set active power failed, %d\n", ret);
-		goto err_unregister_device;
+	/* Power down the device until we need it */
+	error = cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+	if (error) {
+		dev_err(dev, "failed to quiesce the device: %d\n", error);
+		return error;
 	}
 
-	cyapa->irq = client->irq;
-	ret = request_threaded_irq(cyapa->irq,
-				   NULL,
-				   cyapa_irq,
-				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-				   "cyapa",
-				   cyapa);
-	if (ret) {
-		dev_err(dev, "IRQ request failed: %d\n, ", ret);
-		goto err_unregister_device;
+	error = cyapa_create_input_dev(cyapa);
+	if (error)
+		return error;
+
+	error = devm_request_threaded_irq(dev, client->irq,
+					  NULL, cyapa_irq,
+					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					  "cyapa", cyapa);
+	if (error) {
+		dev_err(dev, "IRQ request failed: %d\n, ", error);
+		return error;
 	}
 
-	return 0;
-
-err_unregister_device:
-	input_unregister_device(cyapa->input);
-err_mem_free:
-	kfree(cyapa);
+	/* Disable IRQ until the device is opened */
+	disable_irq(client->irq);
 
-	return ret;
-}
-
-static int cyapa_remove(struct i2c_client *client)
-{
-	struct cyapa *cyapa = i2c_get_clientdata(client);
-
-	free_irq(cyapa->irq, cyapa);
-	input_unregister_device(cyapa->input);
-	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
-	kfree(cyapa);
+	/* Register the device in input subsystem */
+	error = input_register_device(cyapa->input);
+	if (error) {
+		dev_err(dev, "failed to register input device: %d\n", error);
+		return error;
+	}
 
 	return 0;
 }
 
 static int __maybe_unused cyapa_suspend(struct device *dev)
 {
-	int ret;
+	struct i2c_client *client = to_i2c_client(dev);
+	struct cyapa *cyapa = i2c_get_clientdata(client);
+	struct input_dev *input = cyapa->input;
 	u8 power_mode;
-	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int error;
+
+	error = mutex_lock_interruptible(&input->mutex);
+	if (error)
+		return error;
 
-	disable_irq(cyapa->irq);
+	disable_irq(client->irq);
 
 	/*
 	 * Set trackpad device to idle mode if wakeup is allowed,
@@ -919,28 +932,42 @@ static int __maybe_unused cyapa_suspend(struct device *dev)
 	 */
 	power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
 					    : PWR_MODE_OFF;
-	ret = cyapa_set_power_mode(cyapa, power_mode);
-	if (ret < 0)
-		dev_err(dev, "set power mode failed, %d\n", ret);
+	error = cyapa_set_power_mode(cyapa, power_mode);
+	if (error)
+		dev_err(dev, "resume: set power mode to %d failed: %d\n",
+			 power_mode, error);
 
 	if (device_may_wakeup(dev))
 		cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
+
+	mutex_unlock(&input->mutex);
+
 	return 0;
 }
 
 static int __maybe_unused cyapa_resume(struct device *dev)
 {
-	int ret;
-	struct cyapa *cyapa = dev_get_drvdata(dev);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct cyapa *cyapa = i2c_get_clientdata(client);
+	struct input_dev *input = cyapa->input;
+	u8 power_mode;
+	int error;
+
+	mutex_lock(&input->mutex);
 
 	if (device_may_wakeup(dev) && cyapa->irq_wake)
 		disable_irq_wake(cyapa->irq);
 
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-	if (ret)
-		dev_warn(dev, "resume active power failed, %d\n", ret);
+	power_mode = input->users ? PWR_MODE_FULL_ACTIVE : PWR_MODE_OFF;
+	error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+	if (error)
+		dev_warn(dev, "resume: set power mode to %d failed: %d\n",
+			 power_mode, error);
 
 	enable_irq(cyapa->irq);
+
+	mutex_unlock(&input->mutex);
+
 	return 0;
 }
 
@@ -960,7 +987,6 @@ static struct i2c_driver cyapa_driver = {
 	},
 
 	.probe = cyapa_probe,
-	.remove = cyapa_remove,
 	.id_table = cyapa_id_table,
 };
 
#3 lore raw

RE: [PATCH v9 01/18] input: cyapa: add device resource management infrastructure support

Thanks, Dmitry.
I will fix this patch as shown below.
Should I need to fix and resubmit the patches immedaitely or wait for other pathces' comments and submit together?

Thanks,
Dudley

Quoted 27 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 5:26
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 01/18] input: cyapa: add device resource management infrastructure support

Hi Dudley,

drivers/input/mouse/cyapa.c static ssize_t cyapa_read_block(struct cyapa *cyapa,

Quoted 14 lines · Dudley Du

Modify cyapa driver to support device resource management infrastructure to reduce the mistakes of resource management.
TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 48 ++++++++++++++++++---------------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
drivers/input/mouse/cyapa.c +0 -0
index b409c3d..b3d7a2a 100644
@@ -409,11 +409,11 @@ static ssize_t cyapa_read_block(struct cyapa *cyapa,

u8 cmd_idx, u8 *values)

Quoted 15 lines

drivers/input/mouse/cyapa.c static ssize_t cyapa_read_block(struct cyapa *cyapa, · line 416

Quoted 12 lines
drivers/input/mouse/cyapa.c +4 -4
 cmd = cyapa_smbus_cmds[cmd_idx].cmd;
 len = cyapa_smbus_cmds[cmd_idx].len;
 return cyapa_smbus_read_block(cyapa, cmd, len, values);
-} else {
-cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-len = cyapa_i2c_cmds[cmd_idx].len;
-return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
 }
+
+cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+len = cyapa_i2c_cmds[cmd_idx].len;
+return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);

I am not sure why you are changing this code, it has nothing to do with managed resources and the original was just fine.

It's changed just to fix the warning when running the patch checking script.

Quoted 8 lines
Quoted 8 lines
drivers/input/mouse/cyapa.c +0 -0
 }
 
 /*
@@ -762,7 +762,7 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
 return -EINVAL;
 
Quoted 5 lines

drivers/input/mouse/cyapa.c static int cyapa_create_input_dev(struct cyapa *cyapa) · line 765

Quoted 2 lines
drivers/input/mouse/cyapa.c +1 -1
-input = cyapa->input = input_allocate_device();
+input = cyapa->input = devm_input_allocate_device(dev);

If you are using devm_* then you do not need to manually cal input_free_device() (further down in this fucntion).

Yes, it's correct. It's my mistake to lot the code here.

Quoted 74 lines
Quoted 74 lines
drivers/input/mouse/cyapa.c +13 -21
 if (!input) {
 dev_err(dev, "allocate memory for input device failed\n");
 return -ENOMEM;
@@ -837,11 +837,9 @@ static int cyapa_probe(struct i2c_client *client,
 return -EIO;
 }
 
-cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
-if (!cyapa) {
-dev_err(dev, "allocate memory for cyapa failed\n");
+cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
+if (!cyapa)
 return -ENOMEM;
-}
 
 cyapa->gen = CYAPA_GEN3;
 cyapa->client = client;
@@ -856,51 +854,43 @@ static int cyapa_probe(struct i2c_client *client,
 ret = cyapa_check_is_operational(cyapa);
 if (ret) {
 dev_err(dev, "device not operational, %d\n", ret);
-goto err_mem_free;
+return ret;
 }
 
 ret = cyapa_create_input_dev(cyapa);
 if (ret) {
 dev_err(dev, "create input_dev instance failed, %d\n", ret);
-goto err_mem_free;
+return ret;
 }
 
 ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
 if (ret) {
 dev_err(dev, "set active power failed, %d\n", ret);
-goto err_unregister_device;
+return ret;
 }
 
 cyapa->irq = client->irq;
-ret = request_threaded_irq(cyapa->irq,
-   NULL,
-   cyapa_irq,
-   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-   "cyapa",
-   cyapa);
+ret = devm_request_threaded_irq(dev,
+cyapa->irq,
+NULL,
+cyapa_irq,
+IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+"cyapa",
+cyapa);
 if (ret) {
 dev_err(dev, "IRQ request failed: %d\n, ", ret);
-goto err_unregister_device;
+return ret;
 }
 
 return 0;
-
-err_unregister_device:
-input_unregister_device(cyapa->input);
-err_mem_free:
-kfree(cyapa);
-
-return ret;
 }
 
 static int cyapa_remove(struct i2c_client *client)
 {
 struct cyapa *cyapa = i2c_get_clientdata(client);
 
Quoted 9 lines

drivers/input/mouse/cyapa.c static int cyapa_probe(struct i2c_client *client, · line 903

Quoted 5 lines
drivers/input/mouse/cyapa.c +1 -3
-free_irq(cyapa->irq, cyapa);
-input_unregister_device(cyapa->input);
+disable_irq(cyapa->irq);
 cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
-kfree(cyapa);

I refer devm* conversions to completely eradicate ->remove() methods. I took the liberty to edit the patch a bit, does the version below work for you?

Yes, it's working in the below version.
The code in ->remove() method is just to turn the device off, and it can be controlled through open()/close(). Thanks.

Quoted 311 lines

Thanks!

--
Dmitry

Input: cyapa - switch to using managed resources

From: Dudley Du <[email protected]>

Use of managed resources simplifies error handling and device removal code.

[Dmitry: added open/close methods so cyapa_remove is no longer needed.]
---
 drivers/input/mouse/cyapa.c |  184 +++++++++++++++++++++++++------------------
 1 file changed, 105 insertions(+), 79 deletions(-)
drivers/input/mouse/cyapa.c +5 -2
index 1d978c7..c84a9eb 100644
@@ -577,10 +577,13 @@ static int cyapa_set_power_mode(struct cyapa *cyapa,
 u8 power_mode)
 power = ret & ~PWR_MODE_MASK;
 power |= power_mode & PWR_MODE_MASK;
 ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
-if (ret < 0)
+if (ret < 0) {
 dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
 power_mode, ret);
-return ret;
+return ret;
+}
+
+return 0;
 }
 

static int cyapa_get_query_data(struct cyapa *cyapa)

drivers/input/mouse/cyapa.c +26 -2
@@ -753,16 +756,40 @@ static u8 cyapa_check_adapter_functionality(struct
 i2c_client *client)
 return ret;
 }
 
+static int cyapa_open(struct input_dev *input)
+{
+struct cyapa *cyapa = input_get_drvdata(input);
+struct i2c_client *client = cyapa->client;
+int error;
+
+error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+if (error) {
+dev_err(&client->dev, "set active power failed: %d\n", error);
+return error;
+}
+
+enable_irq(client->irq);
+return 0;
+}
+
+static void cyapa_close(struct input_dev *input)
+{
+struct cyapa *cyapa = input_get_drvdata(input);
+
+disable_irq(cyapa->client->irq);
+cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+}
+
 static int cyapa_create_input_dev(struct cyapa *cyapa)
 {
 struct device *dev = &cyapa->client->dev;
-int ret;
 struct input_dev *input;
+int error;
 
 if (!cyapa->physical_size_x || !cyapa->physical_size_y)
 return -EINVAL;
 
-input = cyapa->input = input_allocate_device();
+input = devm_input_allocate_device(dev);
 if (!input) {
 dev_err(dev, "allocate memory for input device failed\n");

return -ENOMEM;

drivers/input/mouse/cyapa.c +74 -74
@@ -775,6 +802,9 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 input->id.product = 0;  /* means any product in eventcomm. */
 input->dev.parent = &cyapa->client->dev;
 
+input->open = cyapa_open;
+input->close = cyapa_close;
+
 input_set_drvdata(input, cyapa);
 
 __set_bit(EV_ABS, input->evbit);
@@ -802,34 +832,24 @@ static int cyapa_create_input_dev(struct cyapa *cyapa)
 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 
 /* handle pointer emulation and unused slots in core */
-ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
-  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
-if (ret) {
-dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
-goto err_free_device;
+error = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
+    INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+if (error) {
+dev_err(dev, "failed to initialize MT slots: %d\n", error);
+return error;
 }
 
-/* Register the device in input subsystem */
-ret = input_register_device(input);
-if (ret) {
-dev_err(dev, "input device register failed, %d\n", ret);
-goto err_free_device;
-}
+cyapa->input = input;
 return 0;
-
-err_free_device:
-input_free_device(input);
-cyapa->input = NULL;
-return ret;
 }
 
 static int cyapa_probe(struct i2c_client *client,
        const struct i2c_device_id *dev_id)
 {
-int ret;
-u8 adapter_func;
-struct cyapa *cyapa;
 struct device *dev = &client->dev;
+struct cyapa *cyapa;
+u8 adapter_func;
+int error;
 
 adapter_func = cyapa_check_adapter_functionality(client);
 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
@@ -837,11 +857,9 @@ static int cyapa_probe(struct i2c_client *client,
 return -EIO;
 }
 
-cyapa = kzalloc(sizeof(struct cyapa), GFP_KERNEL);
-if (!cyapa) {
-dev_err(dev, "allocate memory for cyapa failed\n");
+cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
+if (!cyapa)
 return -ENOMEM;
-}
 
 cyapa->gen = CYAPA_GEN3;
 cyapa->client = client;
@@ -852,66 +870,61 @@ static int cyapa_probe(struct i2c_client *client,
 /* i2c isn't supported, use smbus */
 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
 cyapa->smbus = true;
+
 cyapa->state = CYAPA_STATE_NO_DEVICE;
-ret = cyapa_check_is_operational(cyapa);
-if (ret) {
-dev_err(dev, "device not operational, %d\n", ret);
-goto err_mem_free;
-}
 
-ret = cyapa_create_input_dev(cyapa);
-if (ret) {
-dev_err(dev, "create input_dev instance failed, %d\n", ret);
-goto err_mem_free;
+error = cyapa_check_is_operational(cyapa);
+if (error) {
+dev_err(dev, "device not operational, %d\n", error);
+return error;
 }
 
-ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-if (ret) {
-dev_err(dev, "set active power failed, %d\n", ret);
-goto err_unregister_device;
+/* Power down the device until we need it */
+error = cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+if (error) {
+dev_err(dev, "failed to quiesce the device: %d\n", error);
+return error;
 }
 
-cyapa->irq = client->irq;
-ret = request_threaded_irq(cyapa->irq,
-   NULL,
-   cyapa_irq,
-   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-   "cyapa",
-   cyapa);
-if (ret) {
-dev_err(dev, "IRQ request failed: %d\n, ", ret);
-goto err_unregister_device;
+error = cyapa_create_input_dev(cyapa);
+if (error)
+return error;
+
+error = devm_request_threaded_irq(dev, client->irq,
+  NULL, cyapa_irq,
+  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+  "cyapa", cyapa);
+if (error) {
+dev_err(dev, "IRQ request failed: %d\n, ", error);
+return error;
 }
 
-return 0;
-
-err_unregister_device:
-input_unregister_device(cyapa->input);
-err_mem_free:
-kfree(cyapa);
+/* Disable IRQ until the device is opened */
+disable_irq(client->irq);
 
-return ret;
-}
-
-static int cyapa_remove(struct i2c_client *client)
-{
-struct cyapa *cyapa = i2c_get_clientdata(client);
-
-free_irq(cyapa->irq, cyapa);
-input_unregister_device(cyapa->input);
-cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
-kfree(cyapa);
+/* Register the device in input subsystem */
+error = input_register_device(cyapa->input);
+if (error) {
+dev_err(dev, "failed to register input device: %d\n", error);
+return error;
+}
 
 return 0;
 }
 
 static int __maybe_unused cyapa_suspend(struct device *dev)
 {
-int ret;
+struct i2c_client *client = to_i2c_client(dev);
+struct cyapa *cyapa = i2c_get_clientdata(client);
+struct input_dev *input = cyapa->input;
 u8 power_mode;
-struct cyapa *cyapa = dev_get_drvdata(dev);
+int error;
+
+error = mutex_lock_interruptible(&input->mutex);
+if (error)
+return error;
 
-disable_irq(cyapa->irq);
+disable_irq(client->irq);
 
 /*
  * Set trackpad device to idle mode if wakeup is allowed,
@@ -919,28 +932,42 @@ static int __maybe_unused cyapa_suspend(struct device
 *dev)
  */
 power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
     : PWR_MODE_OFF;
-ret = cyapa_set_power_mode(cyapa, power_mode);
-if (ret < 0)
-dev_err(dev, "set power mode failed, %d\n", ret);
+error = cyapa_set_power_mode(cyapa, power_mode);
+if (error)
+dev_err(dev, "resume: set power mode to %d failed: %d\n",
+ power_mode, error);
 
 if (device_may_wakeup(dev))
 cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
+
+mutex_unlock(&input->mutex);
+
 return 0;
 }
 
 static int __maybe_unused cyapa_resume(struct device *dev)
 {
-int ret;
-struct cyapa *cyapa = dev_get_drvdata(dev);
+struct i2c_client *client = to_i2c_client(dev);
+struct cyapa *cyapa = i2c_get_clientdata(client);
+struct input_dev *input = cyapa->input;
+u8 power_mode;
+int error;
+
+mutex_lock(&input->mutex);
 
 if (device_may_wakeup(dev) && cyapa->irq_wake)
 disable_irq_wake(cyapa->irq);
 
-ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-if (ret)
-dev_warn(dev, "resume active power failed, %d\n", ret);
+power_mode = input->users ? PWR_MODE_FULL_ACTIVE : PWR_MODE_OFF;
+error = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+if (error)
+dev_warn(dev, "resume: set power mode to %d failed: %d\n",
+ power_mode, error);
 
 enable_irq(cyapa->irq);
+
+mutex_unlock(&input->mutex);
+
 return 0;
 }

drivers/input/mouse/cyapa.c static struct i2c_driver cyapa_driver = { · line 992

Quoted 7 lines
drivers/input/mouse/cyapa.c +0 -1
@@ -960,7 +987,6 @@ static struct i2c_driver cyapa_driver = {
 },
 
 .probe = cyapa_probe,
-.remove = cyapa_remove,
 .id_table = cyapa_id_table,
 };
 

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

maintainer
#4 lore raw

RE: [PATCH v9 01/18] input: cyapa: add device resource management infrastructure support

Quoted 4 lines · Dudley Du

Thanks, Dmitry.
I will fix this patch as shown below.
Should I need to fix and resubmit the patches immedaitely or wait for other pathces' comments and submit together?

No, if the version that I sent you works you do not need to resubmit, I'll apply it as is. I will have comments on other patches later tonight.

Thanks.

Signature · 1 line
Dmitry
author
#5 lore raw

PATCH v9 2/18 input: cyapa: re-design driver to support multi-trackpad in one driver

In reply to Dudley Du

In order to support multiple different chipsets and communication protocols trackpad devices in one cyapa driver, the new cyapa driver is re-designed with one cyapa driver core and multiple device specific functions component. The cyapa driver core is contained in this patch, it supplies basic functions that working with kernel and input subsystem, and also supplies the interfaces that the specific devices' component can connect and work together with as one driver.

---
 drivers/input/mouse/cyapa.c | 1087 ++++++++++++++-----------------------------
 drivers/input/mouse/cyapa.h |  321 +++++++++++++
 2 files changed, 682 insertions(+), 726 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa.h
drivers/input/mouse/cyapa.c +361 -726
index b3d7a2a..5029618 100644
@@ -6,7 +6,7 @@
  *   Daniel Kurtz <[email protected]>
  *   Benson Leung <[email protected]>
  *
- * Copyright (C) 2011-2012 Cypress Semiconductor, Inc.
+ * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
  * Copyright (C) 2011-2012 Google, Inc.
  *
  * This file is subject to the terms and conditions of the GNU General Public
@@ -20,601 +20,193 @@
 #include <linux/input/mt.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
+#include "cyapa.h"
 
-/* APA trackpad firmware generation */
-#define CYAPA_GEN3   0x03   /* support MT-protocol B with tracking ID. */
-
-#define CYAPA_NAME   "Cypress APA Trackpad (cyapa)"
-
-/* commands for read/write registers of Cypress trackpad */
-#define CYAPA_CMD_SOFT_RESET       0x00
-#define CYAPA_CMD_POWER_MODE       0x01
-#define CYAPA_CMD_DEV_STATUS       0x02
-#define CYAPA_CMD_GROUP_DATA       0x03
-#define CYAPA_CMD_GROUP_CMD        0x04
-#define CYAPA_CMD_GROUP_QUERY      0x05
-#define CYAPA_CMD_BL_STATUS        0x06
-#define CYAPA_CMD_BL_HEAD          0x07
-#define CYAPA_CMD_BL_CMD           0x08
-#define CYAPA_CMD_BL_DATA          0x09
-#define CYAPA_CMD_BL_ALL           0x0a
-#define CYAPA_CMD_BLK_PRODUCT_ID   0x0b
-#define CYAPA_CMD_BLK_HEAD         0x0c
-
-/* report data start reg offset address. */
-#define DATA_REG_START_OFFSET  0x0000
-
-#define BL_HEAD_OFFSET 0x00
-#define BL_DATA_OFFSET 0x10
-
-/*
- * Operational Device Status Register
- *
- * bit 7: Valid interrupt source
- * bit 6 - 4: Reserved
- * bit 3 - 2: Power status
- * bit 1 - 0: Device status
- */
-#define REG_OP_STATUS     0x00
-#define OP_STATUS_SRC     0x80
-#define OP_STATUS_POWER   0x0c
-#define OP_STATUS_DEV     0x03
-#define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER | OP_STATUS_DEV)
-
-/*
- * Operational Finger Count/Button Flags Register
- *
- * bit 7 - 4: Number of touched finger
- * bit 3: Valid data
- * bit 2: Middle Physical Button
- * bit 1: Right Physical Button
- * bit 0: Left physical Button
- */
-#define REG_OP_DATA1       0x01
-#define OP_DATA_VALID      0x08
-#define OP_DATA_MIDDLE_BTN 0x04
-#define OP_DATA_RIGHT_BTN  0x02
-#define OP_DATA_LEFT_BTN   0x01
-#define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN | \
-			  OP_DATA_LEFT_BTN)
-
-/*
- * Bootloader Status Register
- *
- * bit 7: Busy
- * bit 6 - 5: Reserved
- * bit 4: Bootloader running
- * bit 3 - 1: Reserved
- * bit 0: Checksum valid
- */
-#define REG_BL_STATUS        0x01
-#define BL_STATUS_BUSY       0x80
-#define BL_STATUS_RUNNING    0x10
-#define BL_STATUS_DATA_VALID 0x08
-#define BL_STATUS_CSUM_VALID 0x01
-
-/*
- * Bootloader Error Register
- *
- * bit 7: Invalid
- * bit 6: Invalid security key
- * bit 5: Bootloading
- * bit 4: Command checksum
- * bit 3: Flash protection error
- * bit 2: Flash checksum error
- * bit 1 - 0: Reserved
- */
-#define REG_BL_ERROR         0x02
-#define BL_ERROR_INVALID     0x80
-#define BL_ERROR_INVALID_KEY 0x40
-#define BL_ERROR_BOOTLOADING 0x20
-#define BL_ERROR_CMD_CSUM    0x10
-#define BL_ERROR_FLASH_PROT  0x08
-#define BL_ERROR_FLASH_CSUM  0x04
-
-#define BL_STATUS_SIZE  3  /* length of bootloader status registers */
-#define BLK_HEAD_BYTES 32
-
-#define PRODUCT_ID_SIZE  16
-#define QUERY_DATA_SIZE  27
-#define REG_PROTOCOL_GEN_QUERY_OFFSET  20
-
-#define REG_OFFSET_DATA_BASE     0x0000
-#define REG_OFFSET_COMMAND_BASE  0x0028
-#define REG_OFFSET_QUERY_BASE    0x002a
-
-#define CAPABILITY_LEFT_BTN_MASK	(0x01 << 3)
-#define CAPABILITY_RIGHT_BTN_MASK	(0x01 << 4)
-#define CAPABILITY_MIDDLE_BTN_MASK	(0x01 << 5)
-#define CAPABILITY_BTN_MASK  (CAPABILITY_LEFT_BTN_MASK | \
-			      CAPABILITY_RIGHT_BTN_MASK | \
-			      CAPABILITY_MIDDLE_BTN_MASK)
-
-#define CYAPA_OFFSET_SOFT_RESET  REG_OFFSET_COMMAND_BASE
-
-#define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
-
-#define PWR_MODE_MASK   0xfc
-#define PWR_MODE_FULL_ACTIVE (0x3f << 2)
-#define PWR_MODE_IDLE        (0x05 << 2) /* default sleep time is 50 ms. */
-#define PWR_MODE_OFF         (0x00 << 2)
-
-#define PWR_STATUS_MASK      0x0c
-#define PWR_STATUS_ACTIVE    (0x03 << 2)
-#define PWR_STATUS_IDLE      (0x02 << 2)
-#define PWR_STATUS_OFF       (0x00 << 2)
-
-/*
- * CYAPA trackpad device states.
- * Used in register 0x00, bit1-0, DeviceStatus field.
- * Other values indicate device is in an abnormal state and must be reset.
- */
-#define CYAPA_DEV_NORMAL  0x03
-#define CYAPA_DEV_BUSY    0x01
-
-enum cyapa_state {
-	CYAPA_STATE_OP,
-	CYAPA_STATE_BL_IDLE,
-	CYAPA_STATE_BL_ACTIVE,
-	CYAPA_STATE_BL_BUSY,
-	CYAPA_STATE_NO_DEVICE,
-};
-
-
-struct cyapa_touch {
-	/*
-	 * high bits or x/y position value
-	 * bit 7 - 4: high 4 bits of x position value
-	 * bit 3 - 0: high 4 bits of y position value
-	 */
-	u8 xy_hi;
-	u8 x_lo;  /* low 8 bits of x position value. */
-	u8 y_lo;  /* low 8 bits of y position value. */
-	u8 pressure;
-	/* id range is 1 - 15.  It is incremented with every new touch. */
-	u8 id;
-} __packed;
-
-/* The touch.id is used as the MT slot id, thus max MT slot is 15 */
-#define CYAPA_MAX_MT_SLOTS  15
-
-struct cyapa_reg_data {
-	/*
-	 * bit 0 - 1: device status
-	 * bit 3 - 2: power mode
-	 * bit 6 - 4: reserved
-	 * bit 7: interrupt valid bit
-	 */
-	u8 device_status;
-	/*
-	 * bit 7 - 4: number of fingers currently touching pad
-	 * bit 3: valid data check bit
-	 * bit 2: middle mechanism button state if exists
-	 * bit 1: right mechanism button state if exists
-	 * bit 0: left mechanism button state if exists
-	 */
-	u8 finger_btn;
-	/* CYAPA reports up to 5 touches per packet. */
-	struct cyapa_touch touches[5];
-} __packed;
-
-/* The main device structure */
-struct cyapa {
-	enum cyapa_state state;
-
-	struct i2c_client *client;
-	struct input_dev *input;
-	char phys[32];	/* device physical location */
-	int irq;
-	bool irq_wake;  /* irq wake is enabled */
-	bool smbus;
-
-	/* read from query data region. */
-	char product_id[16];
-	u8 btn_capability;
-	u8 gen;
-	int max_abs_x;
-	int max_abs_y;
-	int physical_size_x;
-	int physical_size_y;
-};
-
-static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
-		0x04, 0x05, 0x06, 0x07 };
-static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
-		0x05, 0x06, 0x07 };
-
-struct cyapa_cmd_len {
-	u8 cmd;
-	u8 len;
-};
 
 #define CYAPA_ADAPTER_FUNC_NONE   0
 #define CYAPA_ADAPTER_FUNC_I2C    1
 #define CYAPA_ADAPTER_FUNC_SMBUS  2
 #define CYAPA_ADAPTER_FUNC_BOTH   3
 
-/*
- * macros for SMBus communication
- */
-#define SMBUS_READ   0x01
-#define SMBUS_WRITE 0x00
-#define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
-#define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
-#define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
-#define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
-
- /* for byte read/write command */
-#define CMD_RESET 0
-#define CMD_POWER_MODE 1
-#define CMD_DEV_STATUS 2
-#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
-#define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
-#define CYAPA_SMBUS_POWER_MODE SMBUS_BYTE_CMD(CMD_POWER_MODE)
-#define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
-
- /* for group registers read/write command */
-#define REG_GROUP_DATA 0
-#define REG_GROUP_CMD 2
-#define REG_GROUP_QUERY 3
-#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
-#define CYAPA_SMBUS_GROUP_DATA SMBUS_GROUP_CMD(REG_GROUP_DATA)
-#define CYAPA_SMBUS_GROUP_CMD SMBUS_GROUP_CMD(REG_GROUP_CMD)
-#define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
-
- /* for register block read/write command */
-#define CMD_BL_STATUS 0
-#define CMD_BL_HEAD 1
-#define CMD_BL_CMD 2
-#define CMD_BL_DATA 3
-#define CMD_BL_ALL 4
-#define CMD_BLK_PRODUCT_ID 5
-#define CMD_BLK_HEAD 6
-#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
-
-/* register block read/write command in bootloader mode */
-#define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
-#define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
-#define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
-#define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
-#define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
-
-/* register block read/write command in operational mode */
-#define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
-#define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
-
-static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
-	{ CYAPA_OFFSET_SOFT_RESET, 1 },
-	{ REG_OFFSET_COMMAND_BASE + 1, 1 },
-	{ REG_OFFSET_DATA_BASE, 1 },
-	{ REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
-	{ REG_OFFSET_COMMAND_BASE, 0 },
-	{ REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE },
-	{ BL_HEAD_OFFSET, 3 },
-	{ BL_HEAD_OFFSET, 16 },
-	{ BL_HEAD_OFFSET, 16 },
-	{ BL_DATA_OFFSET, 16 },
-	{ BL_HEAD_OFFSET, 32 },
-	{ REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
-	{ REG_OFFSET_DATA_BASE, 32 }
-};
+#define CYAPA_DEBUGFS_READ_FW	"read_fw"
+#define CYAPA_DEBUGFS_RAW_DATA	"raw_data"
+#define CYAPA_FW_NAME		"cyapa.bin"
+
+const char unique_str[] = "CYTRA";
 
-static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
-	{ CYAPA_SMBUS_RESET, 1 },
-	{ CYAPA_SMBUS_POWER_MODE, 1 },
-	{ CYAPA_SMBUS_DEV_STATUS, 1 },
-	{ CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
-	{ CYAPA_SMBUS_GROUP_CMD, 2 },
-	{ CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
-	{ CYAPA_SMBUS_BL_STATUS, 3 },
-	{ CYAPA_SMBUS_BL_HEAD, 16 },
-	{ CYAPA_SMBUS_BL_CMD, 16 },
-	{ CYAPA_SMBUS_BL_DATA, 16 },
-	{ CYAPA_SMBUS_BL_ALL, 32 },
-	{ CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
-	{ CYAPA_SMBUS_BLK_HEAD, 16 },
-};
 
-static ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
+ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
 					u8 *values)
 {
 	return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
+ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
 					 size_t len, const u8 *values)
 {
 	return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-/*
- * cyapa_smbus_read_block - perform smbus block read command
- * @cyapa  - private data structure of the driver
- * @cmd    - the properly encoded smbus command
- * @len    - expected length of smbus command result
- * @values - buffer to store smbus command result
- *
- * Returns negative errno, else the number of bytes written.
- *
- * Note:
- * In trackpad device, the memory block allocated for I2C register map
- * is 256 bytes, so the max read block for I2C bus is 256 bytes.
- */
-static ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
-				      u8 *values)
-{
-	ssize_t ret;
-	u8 index;
-	u8 smbus_cmd;
-	u8 *buf;
-	struct i2c_client *client = cyapa->client;
-
-	if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
-		return -EINVAL;
-
-	if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
-		/* read specific block registers command. */
-		smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-		ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
-		goto out;
-	}
-
-	ret = 0;
-	for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
-		smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
-		smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
-		buf = values + I2C_SMBUS_BLOCK_MAX * index;
-		ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
-		if (ret < 0)
-			goto out;
-	}
-
-out:
-	return ret > 0 ? len : ret;
-}
-
-static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
-{
-	u8 cmd;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	}
-	return i2c_smbus_read_byte_data(cyapa->client, cmd);
-}
-
-static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
-{
-	u8 cmd;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	}
-	return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
-}
-
-static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
-{
-	u8 cmd;
-	size_t len;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		len = cyapa_smbus_cmds[cmd_idx].len;
-		return cyapa_smbus_read_block(cyapa, cmd, len, values);
-	}
-
-	cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	len = cyapa_i2c_cmds[cmd_idx].len;
-	return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
-}
-
-/*
- * Query device for its current operating state.
- *
- */
-static int cyapa_get_state(struct cyapa *cyapa)
+/* Returns 0 on success, else negative errno on failure. */
+ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
+					u8 *values)
 {
 	int ret;
-	u8 status[BL_STATUS_SIZE];
-
-	cyapa->state = CYAPA_STATE_NO_DEVICE;
-
-	/*
-	 * Get trackpad status by reading 3 registers starting from 0.
-	 * If the device is in the bootloader, this will be BL_HEAD.
-	 * If the device is in operation mode, this will be the DATA regs.
-	 *
-	 */
-	ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
-				       status);
-
-	/*
-	 * On smbus systems in OP mode, the i2c_reg_read will fail with
-	 * -ETIMEDOUT.  In this case, try again using the smbus equivalent
-	 * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
-	 */
-	if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO))
-		ret = cyapa_read_block(cyapa, CYAPA_CMD_BL_STATUS, status);
-
-	if (ret != BL_STATUS_SIZE)
-		goto error;
-
-	if ((status[REG_OP_STATUS] & OP_STATUS_SRC) == OP_STATUS_SRC) {
-		switch (status[REG_OP_STATUS] & OP_STATUS_DEV) {
-		case CYAPA_DEV_NORMAL:
-		case CYAPA_DEV_BUSY:
-			cyapa->state = CYAPA_STATE_OP;
-			break;
-		default:
-			ret = -EAGAIN;
-			goto error;
-		}
-	} else {
-		if (status[REG_BL_STATUS] & BL_STATUS_BUSY)
-			cyapa->state = CYAPA_STATE_BL_BUSY;
-		else if (status[REG_BL_ERROR] & BL_ERROR_BOOTLOADING)
-			cyapa->state = CYAPA_STATE_BL_ACTIVE;
-		else
-			cyapa->state = CYAPA_STATE_BL_IDLE;
-	}
+	struct i2c_client *client = cyapa->client;
+	struct i2c_msg msgs[] = {
+		{
+			.addr = client->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &reg,
+		},
+		{
+			.addr = client->addr,
+			.flags = I2C_M_RD,
+			.len = len,
+			.buf = values,
+		},
+	};
+
+	ret = i2c_transfer(client->adapter, msgs, 2);
+
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
 
 	return 0;
-error:
-	return (ret < 0) ? ret : -EAGAIN;
 }
 
-/*
- * Poll device for its status in a loop, waiting up to timeout for a response.
- *
- * When the device switches state, it usually takes ~300 ms.
- * However, when running a new firmware image, the device must calibrate its
- * sensors, which can take as long as 2 seconds.
+/**
+ * cyapa_i2c_write - Execute i2c block data write operation
+ * @cyapa: Handle to this driver
+ * @ret: Offset of the data to written in the register map
+ * @len: number of bytes to write
+ * @values: Data to be written
  *
- * Note: The timeout has granularity of the polling rate, which is 100 ms.
- *
- * Returns:
- *   0 when the device eventually responds with a valid non-busy state.
- *   -ETIMEDOUT if device never responds (too many -EAGAIN)
- *   < 0    other errors
+ * Return negative errno code on error; return zero when success.
  */
-static int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+ssize_t cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
+					 size_t len, const void *values)
 {
 	int ret;
-	int tries = timeout / 100;
-
-	ret = cyapa_get_state(cyapa);
-	while ((ret || cyapa->state >= CYAPA_STATE_BL_BUSY) && tries--) {
-		msleep(100);
-		ret = cyapa_get_state(cyapa);
-	}
-	return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
-}
-
-static int cyapa_bl_deactivate(struct cyapa *cyapa)
-{
-	int ret;
-
-	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
-					bl_deactivate);
-	if (ret < 0)
-		return ret;
-
-	/* wait for bootloader to switch to idle state; should take < 100ms */
-	msleep(100);
-	ret = cyapa_poll_state(cyapa, 500);
-	if (ret < 0)
-		return ret;
-	if (cyapa->state != CYAPA_STATE_BL_IDLE)
-		return -EAGAIN;
-	return 0;
+	struct i2c_client *client = cyapa->client;
+	char data[32], *buf;
+
+	if (len > 31) {
+		buf = kzalloc(len + 1, GFP_KERNEL);
+		if (!buf)
+			return -ENOMEM;
+	} else
+		buf = data;
+
+	buf[0] = reg;
+	memcpy(&buf[1], values, len);
+	ret = i2c_master_send(client, buf, len + 1);
+
+	if (buf != data)
+		kfree(buf);
+	return (ret == (len + 1)) ? 0 : ((ret < 0) ? ret : -EIO);
 }
 
-/*
- * Exit bootloader
- *
- * Send bl_exit command, then wait 50 - 100 ms to let device transition to
- * operational mode.  If this is the first time the device's firmware is
- * running, it can take up to 2 seconds to calibrate its sensors.  So, poll
- * the device's new state for up to 2 seconds.
- *
- * Returns:
- *   -EIO    failure while reading from device
- *   -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
- *   0       device is supported and in operational mode
- */
-static int cyapa_bl_exit(struct cyapa *cyapa)
+static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
 {
-	int ret;
-
-	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
-	if (ret < 0)
-		return ret;
-
-	/*
-	 * Wait for bootloader to exit, and operation mode to start.
-	 * Normally, this takes at least 50 ms.
-	 */
-	usleep_range(50000, 100000);
-	/*
-	 * In addition, when a device boots for the first time after being
-	 * updated to new firmware, it must first calibrate its sensors, which
-	 * can take up to an additional 2 seconds.
-	 */
-	ret = cyapa_poll_state(cyapa, 2000);
-	if (ret < 0)
-		return ret;
-	if (cyapa->state != CYAPA_STATE_OP)
-		return -EAGAIN;
+	u8 ret = CYAPA_ADAPTER_FUNC_NONE;
 
-	return 0;
+	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		ret |= CYAPA_ADAPTER_FUNC_I2C;
+	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+				     I2C_FUNC_SMBUS_BLOCK_DATA |
+				     I2C_FUNC_SMBUS_I2C_BLOCK))
+		ret |= CYAPA_ADAPTER_FUNC_SMBUS;
+	return ret;
 }
 
-/*
- * Set device power mode
- *
- */
-static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
+static int cyapa_create_input_dev(struct cyapa *cyapa)
 {
 	struct device *dev = &cyapa->client->dev;
 	int ret;
-	u8 power;
+	struct input_dev *input;
 
-	if (cyapa->state != CYAPA_STATE_OP)
-		return 0;
+	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
+		return -EINVAL;
 
-	ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
-	if (ret < 0)
-		return ret;
+	input = cyapa->input = devm_input_allocate_device(dev);
+	if (!input) {
+		dev_err(dev, "failed to allocate memory for input device.\n");
+		return -ENOMEM;
+	}
 
-	power = ret & ~PWR_MODE_MASK;
-	power |= power_mode & PWR_MODE_MASK;
-	ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
-	if (ret < 0)
-		dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
-			power_mode, ret);
-	return ret;
-}
+	input->name = CYAPA_NAME;
+	input->phys = cyapa->phys;
+	input->id.bustype = BUS_I2C;
+	input->id.version = 1;
+	input->id.product = 0;  /* Means any product in eventcomm. */
+	input->dev.parent = &cyapa->client->dev;
 
-static int cyapa_get_query_data(struct cyapa *cyapa)
-{
-	u8 query_data[QUERY_DATA_SIZE];
-	int ret;
+	input_set_drvdata(input, cyapa);
 
-	if (cyapa->state != CYAPA_STATE_OP)
-		return -EBUSY;
+	__set_bit(EV_ABS, input->evbit);
 
-	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
-	if (ret < 0)
-		return ret;
-	if (ret != QUERY_DATA_SIZE)
-		return -EIO;
+	/* Finger position */
+	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
+			     0);
+	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
+			     0);
+	input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
+	if (cyapa->gen > CYAPA_GEN3) {
+		input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
+		input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
+		/*
+		 * Orientation is the angle between the vertical axis and
+		 * the major axis of the contact ellipse.
+		 * The range is -127 to 127.
+		 * the positive direction is clockwise form the vertical axis.
+		 * If the ellipse of contact degenerates into a circle,
+		 * orientation is reported as 0.
+		 *
+		 * Also, for Gen5 trackpad the accurate of this orientation
+		 * value is value + (-30 ~ 30).
+		 */
+		input_set_abs_params(input, ABS_MT_ORIENTATION,
+				-127, 127, 0, 0);
+	}
+	if (cyapa->gen >= CYAPA_GEN5) {
+		input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
+		input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
+	}
 
-	memcpy(&cyapa->product_id[0], &query_data[0], 5);
-	cyapa->product_id[5] = '-';
-	memcpy(&cyapa->product_id[6], &query_data[5], 6);
-	cyapa->product_id[12] = '-';
-	memcpy(&cyapa->product_id[13], &query_data[11], 2);
-	cyapa->product_id[15] = '\0';
+	input_abs_set_res(input, ABS_MT_POSITION_X,
+			  cyapa->max_abs_x / cyapa->physical_size_x);
+	input_abs_set_res(input, ABS_MT_POSITION_Y,
+			  cyapa->max_abs_y / cyapa->physical_size_y);
 
-	cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
+	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
+		__set_bit(BTN_LEFT, input->keybit);
+	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
+		__set_bit(BTN_MIDDLE, input->keybit);
+	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
+		__set_bit(BTN_RIGHT, input->keybit);
 
-	cyapa->gen = query_data[20] & 0x0f;
+	if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
+		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 
-	cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
-	cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
+	/* Handle pointer emulation and unused slots in core */
+	ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
+				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+	if (ret) {
+		dev_err(dev, "failed to initialize MT slots, %d\n", ret);
+		return ret;
+	}
 
-	cyapa->physical_size_x =
-		((query_data[24] & 0xf0) << 4) | query_data[25];
-	cyapa->physical_size_y =
-		((query_data[24] & 0x0f) << 8) | query_data[26];
+	/* Register the device in input subsystem */
+	ret = input_register_device(input);
+	if (ret) {
+		dev_err(dev, "failed to register input device, %d\n", ret);
+		return ret;
+	}
 
 	return 0;
 }
@@ -635,192 +227,213 @@ static int cyapa_get_query_data(struct cyapa *cyapa)
  */
 static int cyapa_check_is_operational(struct cyapa *cyapa)
 {
-	struct device *dev = &cyapa->client->dev;
-	static const char unique_str[] = "CYTRA";
 	int ret;
 
-	ret = cyapa_poll_state(cyapa, 2000);
-	if (ret < 0)
+	ret = cyapa_poll_state(cyapa, 4000);
+	if (ret)
 		return ret;
-	switch (cyapa->state) {
-	case CYAPA_STATE_BL_ACTIVE:
-		ret = cyapa_bl_deactivate(cyapa);
-		if (ret)
-			return ret;
-
-	/* Fallthrough state */
-	case CYAPA_STATE_BL_IDLE:
-		ret = cyapa_bl_exit(cyapa);
-		if (ret)
-			return ret;
-
-	/* Fallthrough state */
-	case CYAPA_STATE_OP:
-		ret = cyapa_get_query_data(cyapa);
-		if (ret < 0)
-			return ret;
-
-		/* only support firmware protocol gen3 */
-		if (cyapa->gen != CYAPA_GEN3) {
-			dev_err(dev, "unsupported protocol version (%d)",
-				cyapa->gen);
-			return -EINVAL;
-		}
-
-		/* only support product ID starting with CYTRA */
-		if (memcmp(cyapa->product_id, unique_str,
-			   sizeof(unique_str) - 1) != 0) {
-			dev_err(dev, "unsupported product ID (%s)\n",
-				cyapa->product_id);
-			return -EINVAL;
-		}
-		return 0;
 
+	switch (cyapa->gen) {
 	default:
-		return -EIO;
+		return -ENODEV;
 	}
-	return 0;
+
+	if (cyapa->ops->operational_check)
+		ret = cyapa->ops->operational_check(cyapa);
+
+	return ret;
 }
 
+
 static irqreturn_t cyapa_irq(int irq, void *dev_id)
 {
 	struct cyapa *cyapa = dev_id;
 	struct device *dev = &cyapa->client->dev;
 	struct input_dev *input = cyapa->input;
-	struct cyapa_reg_data data;
-	int i;
+	bool cont;
 	int ret;
-	int num_fingers;
 
 	if (device_may_wakeup(dev))
 		pm_wakeup_event(dev, 0);
 
-	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
-	if (ret != sizeof(data))
-		goto out;
-
-	if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
-	    (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
-	    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
-		goto out;
-	}
-
-	num_fingers = (data.finger_btn >> 4) & 0x0f;
-	for (i = 0; i < num_fingers; i++) {
-		const struct cyapa_touch *touch = &data.touches[i];
-		/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
-		int slot = touch->id - 1;
-
-		input_mt_slot(input, slot);
-		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
-		input_report_abs(input, ABS_MT_POSITION_X,
-				 ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
-		input_report_abs(input, ABS_MT_POSITION_Y,
-				 ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
-		input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
+	/* Interrupt event maybe cuased by host command to trackpad device. */
+	cont = true;
+	if (cyapa->ops->irq_cmd_handler)
+		cont = cyapa->ops->irq_cmd_handler(cyapa);
+
+	/* Interrupt event maybe from trackpad device input reporting. */
+	if (cont && cyapa->ops->irq_handler) {
+		if (!input || cyapa->ops->irq_handler(cyapa)) {
+			ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+			if (ret && cyapa->ops->sort_empty_output_data) {
+				cyapa->ops->sort_empty_output_data(cyapa,
+					NULL, NULL, NULL);
+				goto out;
+			}
+			cyapa_detect(cyapa);
+			mutex_unlock(&cyapa->state_sync_lock);
+		}
 	}
 
-	input_mt_sync_frame(input);
-
-	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-		input_report_key(input, BTN_LEFT,
-				 data.finger_btn & OP_DATA_LEFT_BTN);
-
-	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-		input_report_key(input, BTN_MIDDLE,
-				 data.finger_btn & OP_DATA_MIDDLE_BTN);
-
-	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-		input_report_key(input, BTN_RIGHT,
-				 data.finger_btn & OP_DATA_RIGHT_BTN);
-
-	input_sync(input);
-
 out:
 	return IRQ_HANDLED;
 }
 
-static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
+/*
+ * Query device for its current operating state.
+ */
+static int cyapa_get_state(struct cyapa *cyapa)
 {
-	u8 ret = CYAPA_ADAPTER_FUNC_NONE;
+	return -ENODEV;
+}
 
-	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
-		ret |= CYAPA_ADAPTER_FUNC_I2C;
-	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
-				     I2C_FUNC_SMBUS_BLOCK_DATA |
-				     I2C_FUNC_SMBUS_I2C_BLOCK))
-		ret |= CYAPA_ADAPTER_FUNC_SMBUS;
-	return ret;
+/*
+ * Poll device for its status in a loop, waiting up to timeout for a response.
+ *
+ * When the device switches state, it usually takes ~300 ms.
+ * However, when running a new firmware image, the device must calibrate its
+ * sensors, which can take as long as 2 seconds.
+ *
+ * Note: The timeout has granularity of the polling rate, which is 100 ms.
+ *
+ * Returns:
+ *   0 when the device eventually responds with a valid non-busy state.
+ *   -ETIMEDOUT if device never responds (too many -EAGAIN)
+ *   < 0    other errors
+ */
+int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+{
+	int ret;
+	int tries = timeout / 100;
+
+	ret = cyapa_get_state(cyapa);
+	while ((ret || cyapa->state <= CYAPA_STATE_BL_BUSY) && tries--) {
+		msleep(100);
+		ret = cyapa_get_state(cyapa);
+	}
+
+	return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
 }
 
-static int cyapa_create_input_dev(struct cyapa *cyapa)
+/*
+ * Returns:
+ *   0    when device is detected, and operatioinal.
+ *   > 0  when device is detected, but not operational.
+ *   < 0  when device is not detected, or other errors.
+ *          e.g.: IO communication error with the device.
+ */
+int cyapa_detect(struct cyapa *cyapa)
 {
 	struct device *dev = &cyapa->client->dev;
+	char *envp[2] = {"ERROR=1", NULL};
 	int ret;
-	struct input_dev *input;
 
-	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
-		return -EINVAL;
+	ret = cyapa_check_is_operational(cyapa);
+	if (ret) {
+		if (ret != -ETIMEDOUT && ret != -ENODEV &&
+			((cyapa->gen == CYAPA_GEN3 &&
+				cyapa->state >= CYAPA_STATE_BL_BUSY &&
+				cyapa->state <= CYAPA_STATE_BL_ACTIVE) ||
+			(cyapa->gen == CYAPA_GEN5 &&
+				cyapa->state == CYAPA_STATE_GEN5_BL))) {
+			dev_warn(dev, "device detected, but no operatinal.\n");
+			dev_warn(dev, "device gen=%d, state=0x%02x\n",
+				cyapa->gen, cyapa->state);
+			dev_warn(dev, "munually fw image recover required.\n");
+			return 1;
+		}
 
-	input = cyapa->input = devm_input_allocate_device(dev);
-	if (!input) {
-		dev_err(dev, "allocate memory for input device failed\n");
-		return -ENOMEM;
+		dev_err(dev, "no device detected, (%d)\n", ret);
+		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
+		return ret;
 	}
 
-	input->name = CYAPA_NAME;
-	input->phys = cyapa->phys;
-	input->id.bustype = BUS_I2C;
-	input->id.version = 1;
-	input->id.product = 0;  /* means any product in eventcomm. */
-	input->dev.parent = &cyapa->client->dev;
+	if (!cyapa->input) {
+		ret = cyapa_create_input_dev(cyapa);
+		if (ret) {
+			dev_err(dev, "create input_dev instance failed, (%d)\n",
+				ret);
+			return ret;
+		}
 
-	input_set_drvdata(input, cyapa);
+		/*
+		 * On some systems, a system crash / warm boot does not reset
+		 * the device's current power mode to FULL_ACTIVE.
+		 * If such an event happens during suspend, after the device
+		 * has been put in a low power mode, the device will still be
+		 * in low power mode on a subsequent boot, since there was
+		 * never a matching resume().
+		 * Handle this by always forcing full power here, when a
+		 * device is first detected to be in operational mode.
+		 */
+		if (cyapa->ops->set_power_mode) {
+			ret = cyapa->ops->set_power_mode(cyapa,
+					PWR_MODE_FULL_ACTIVE, 0);
+			if (ret)
+				dev_warn(dev, "set active power failed, (%d)\n",
+						ret);
+		}
+	}
 
-	__set_bit(EV_ABS, input->evbit);
+	return 0;
+}
 
-	/* finger position */
-	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
-			     0);
-	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
-			     0);
-	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
+/*
+ * Sysfs Interface.
+ */
 
-	input_abs_set_res(input, ABS_MT_POSITION_X,
-			  cyapa->max_abs_x / cyapa->physical_size_x);
-	input_abs_set_res(input, ABS_MT_POSITION_Y,
-			  cyapa->max_abs_y / cyapa->physical_size_y);
+/*
+ * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
+ *
+ * These are helper functions that convert to and from integer idle
+ * times and register settings to write to the PowerMode register.
+ * The trackpad supports between 20ms to 1000ms scan intervals.
+ * The time will be increased in increments of 10ms from 20ms to 100ms.
+ * From 100ms to 1000ms, time will be increased in increments of 20ms.
+ *
+ * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 10;
+ * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 20 + 5;
+ */
+u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
+{
+	sleep_time = clamp_val(sleep_time, 20, 1000);
 
-	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-		__set_bit(BTN_LEFT, input->keybit);
-	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-		__set_bit(BTN_MIDDLE, input->keybit);
-	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-		__set_bit(BTN_RIGHT, input->keybit);
+	if (sleep_time < 100)
+		return ((sleep_time / 10) << 2) & PWR_MODE_MASK;
+	return ((sleep_time / 20 + 5) << 2) & PWR_MODE_MASK;
+}
 
-	if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
-		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
+u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
+{
+	u8 encoded_time = pwr_mode >> 2;
 
-	/* handle pointer emulation and unused slots in core */
-	ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
-				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
-	if (ret) {
-		dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
-		goto err_free_device;
-	}
+	return (encoded_time < 10) ? encoded_time * 10
+				   : (encoded_time - 5) * 20;
+}
 
-	/* Register the device in input subsystem */
-	ret = input_register_device(input);
-	if (ret) {
-		dev_err(dev, "input device register failed, %d\n", ret);
-		goto err_free_device;
-	}
-	return 0;
+/*
+ * Returns:
+ *   0    Driver and device initialization successfully done.
+ *   > 0  Driver initialized and device is detected, but not operational yet.
+ *   < 0  Device is not detected, or driver initialization failed.
+ */
+static int cyapa_initialize(struct cyapa *cyapa)
+{
+	cyapa->state = CYAPA_STATE_NO_DEVICE;
+	cyapa->gen = CYAPA_GEN_UNKNOWN;
+	mutex_init(&cyapa->state_sync_lock);
 
-err_free_device:
-	input_free_device(input);
-	cyapa->input = NULL;
-	return ret;
+	/*
+	 * Set to hard code default, they will be updated with trackpad set
+	 * default values after probe and initialized.
+	 */
+	cyapa->suspend_power_mode = PWR_MODE_SLEEP;
+	cyapa->suspend_sleep_time =
+		cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
+
+	return cyapa_detect(cyapa);
 }
 
 static int cyapa_probe(struct i2c_client *client,
@@ -830,6 +443,7 @@ static int cyapa_probe(struct i2c_client *client,
 	u8 adapter_func;
 	struct cyapa *cyapa;
 	struct device *dev = &client->dev;
+	union i2c_smbus_data dummy;
 
 	adapter_func = cyapa_check_adapter_functionality(client);
 	if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
@@ -837,48 +451,39 @@ static int cyapa_probe(struct i2c_client *client,
 		return -EIO;
 	}
 
+	/* Make sure there is something at this address */
+	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
+			I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
+		return -ENODEV;
+
 	cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
 	if (!cyapa)
 		return -ENOMEM;
 
-	cyapa->gen = CYAPA_GEN3;
-	cyapa->client = client;
-	i2c_set_clientdata(client, cyapa);
-	sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
-		client->addr);
-
 	/* i2c isn't supported, use smbus */
 	if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
 		cyapa->smbus = true;
-	cyapa->state = CYAPA_STATE_NO_DEVICE;
-	ret = cyapa_check_is_operational(cyapa);
-	if (ret) {
-		dev_err(dev, "device not operational, %d\n", ret);
-		return ret;
-	}
 
-	ret = cyapa_create_input_dev(cyapa);
-	if (ret) {
-		dev_err(dev, "create input_dev instance failed, %d\n", ret);
-		return ret;
-	}
+	cyapa->client = client;
+	i2c_set_clientdata(client, cyapa);
+	sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
+		client->addr);
 
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-	if (ret) {
-		dev_err(dev, "set active power failed, %d\n", ret);
+	ret = cyapa_initialize(cyapa);
+	if (ret < 0) {
+		dev_err(dev, "failed to detect and initialize tp device.\n");
 		return ret;
 	}
 
-	cyapa->irq = client->irq;
 	ret = devm_request_threaded_irq(dev,
-					cyapa->irq,
+					client->irq,
 					NULL,
 					cyapa_irq,
 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 					"cyapa",
 					cyapa);
 	if (ret) {
-		dev_err(dev, "IRQ request failed: %d\n, ", ret);
+		dev_err(dev, "failed to request threaded irq, (%d)\n, ", ret);
 		return ret;
 	}
 
@@ -889,8 +494,10 @@ static int cyapa_remove(struct i2c_client *client)
 {
 	struct cyapa *cyapa = i2c_get_clientdata(client);
 
-	disable_irq(cyapa->irq);
-	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+	disable_irq(cyapa->client->irq);
+
+	if (cyapa->ops->set_power_mode)
+		cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
 
 	return 0;
 }
@@ -902,20 +509,38 @@ static int cyapa_suspend(struct device *dev)
 	u8 power_mode;
 	struct cyapa *cyapa = dev_get_drvdata(dev);
 
-	disable_irq(cyapa->irq);
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret) {
+		dev_err(dev, "suspend interrupted by signal, (%d)\n", ret);
+		return ret;
+	}
+
+	/*
+	 * Disable IRQ to avoid the command response interrupt cause system
+	 * suspending process interrupted and failed.
+	 * Because the gen5 devices will always assert interrupt to host after
+	 * executed the set power mode command.
+	 */
+	disable_irq(cyapa->client->irq);
 
 	/*
 	 * Set trackpad device to idle mode if wakeup is allowed,
 	 * otherwise turn off.
 	 */
-	power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
+	power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
 					    : PWR_MODE_OFF;
-	ret = cyapa_set_power_mode(cyapa, power_mode);
-	if (ret < 0)
-		dev_err(dev, "set power mode failed, %d\n", ret);
+	if (cyapa->input && cyapa->ops->set_power_mode) {
+		ret = cyapa->ops->set_power_mode(cyapa, power_mode,
+				cyapa->suspend_sleep_time);
+		if (ret < 0)
+			dev_err(dev, "suspend set power mode failed, %d\n",
+					ret);
+	}
 
 	if (device_may_wakeup(dev))
-		cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
+		cyapa->irq_wake = (enable_irq_wake(cyapa->client->irq) == 0);
+
+	mutex_unlock(&cyapa->state_sync_lock);
 	return 0;
 }
 
@@ -924,19 +549,29 @@ static int cyapa_resume(struct device *dev)
 	int ret;
 	struct cyapa *cyapa = dev_get_drvdata(dev);
 
-	if (device_may_wakeup(dev) && cyapa->irq_wake)
-		disable_irq_wake(cyapa->irq);
-
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
 	if (ret)
-		dev_warn(dev, "resume active power failed, %d\n", ret);
+		dev_err(dev, "resume interrupted by signal, (%d)\n", ret);
 
-	enable_irq(cyapa->irq);
+	if (cyapa->irq_wake) {
+		disable_irq_wake(cyapa->client->irq);
+		cyapa->irq_wake = false;
+	}
+
+	/* Reset to active power state after re-detected. */
+	cyapa_detect(cyapa);
+
+	enable_irq(cyapa->client->irq);
+
+	if (!ret)
+		mutex_unlock(&cyapa->state_sync_lock);
 	return 0;
 }
 #endif /* CONFIG_PM_SLEEP */
 
-static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
+static const struct dev_pm_ops cyapa_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
+};
 
 static const struct i2c_device_id cyapa_id_table[] = {
 	{ "cyapa", 0 },
drivers/input/mouse/cyapa.h new file +321 -0

321 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#6 lore raw

Re: [PATCH v9 02/18] input: cyapa: re-design driver to support multi-trackpad in one driver

Hi Dudley,

Quoted 9 lines · Dudley Du

In order to support multiple different chipsets and communication protocols trackpad devices in one cyapa driver, the new cyapa driver is re-designed with one cyapa driver core and multiple device specific functions component. The cyapa driver core is contained in this patch, it supplies basic functions that working with kernel and input subsystem, and also supplies the interfaces that the specific devices' component can connect and work together with as one driver.

Thank you for addressing my previous comments, the driver looks much better now. Still, a few comments.

We should try to avoid breaking the driver withing the patch series, so please merge this change together with the next patch re-adding the gen3 support. The gen5 is new functionality and can stay in separate patch(es).

Quoted 298 lines
---
 drivers/input/mouse/cyapa.c | 1087 ++++++++++++++-----------------------------
 drivers/input/mouse/cyapa.h |  321 +++++++++++++
 2 files changed, 682 insertions(+), 726 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa.h
drivers/input/mouse/cyapa.c +4 -265
index b3d7a2a..5029618 100644
@@ -6,7 +6,7 @@
  *   Daniel Kurtz <[email protected]>
  *   Benson Leung <[email protected]>
  *
- * Copyright (C) 2011-2012 Cypress Semiconductor, Inc.
+ * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
  * Copyright (C) 2011-2012 Google, Inc.
  *
  * This file is subject to the terms and conditions of the GNU General Public
@@ -20,601 +20,193 @@
 #include <linux/input/mt.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
+#include "cyapa.h"
 
-/* APA trackpad firmware generation */
-#define CYAPA_GEN3   0x03   /* support MT-protocol B with tracking ID. */
-
-#define CYAPA_NAME   "Cypress APA Trackpad (cyapa)"
-
-/* commands for read/write registers of Cypress trackpad */
-#define CYAPA_CMD_SOFT_RESET       0x00
-#define CYAPA_CMD_POWER_MODE       0x01
-#define CYAPA_CMD_DEV_STATUS       0x02
-#define CYAPA_CMD_GROUP_DATA       0x03
-#define CYAPA_CMD_GROUP_CMD        0x04
-#define CYAPA_CMD_GROUP_QUERY      0x05
-#define CYAPA_CMD_BL_STATUS        0x06
-#define CYAPA_CMD_BL_HEAD          0x07
-#define CYAPA_CMD_BL_CMD           0x08
-#define CYAPA_CMD_BL_DATA          0x09
-#define CYAPA_CMD_BL_ALL           0x0a
-#define CYAPA_CMD_BLK_PRODUCT_ID   0x0b
-#define CYAPA_CMD_BLK_HEAD         0x0c
-
-/* report data start reg offset address. */
-#define DATA_REG_START_OFFSET  0x0000
-
-#define BL_HEAD_OFFSET 0x00
-#define BL_DATA_OFFSET 0x10
-
-/*
- * Operational Device Status Register
- *
- * bit 7: Valid interrupt source
- * bit 6 - 4: Reserved
- * bit 3 - 2: Power status
- * bit 1 - 0: Device status
- */
-#define REG_OP_STATUS     0x00
-#define OP_STATUS_SRC     0x80
-#define OP_STATUS_POWER   0x0c
-#define OP_STATUS_DEV     0x03
-#define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER | OP_STATUS_DEV)
-
-/*
- * Operational Finger Count/Button Flags Register
- *
- * bit 7 - 4: Number of touched finger
- * bit 3: Valid data
- * bit 2: Middle Physical Button
- * bit 1: Right Physical Button
- * bit 0: Left physical Button
- */
-#define REG_OP_DATA1       0x01
-#define OP_DATA_VALID      0x08
-#define OP_DATA_MIDDLE_BTN 0x04
-#define OP_DATA_RIGHT_BTN  0x02
-#define OP_DATA_LEFT_BTN   0x01
-#define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN | \
-			  OP_DATA_LEFT_BTN)
-
-/*
- * Bootloader Status Register
- *
- * bit 7: Busy
- * bit 6 - 5: Reserved
- * bit 4: Bootloader running
- * bit 3 - 1: Reserved
- * bit 0: Checksum valid
- */
-#define REG_BL_STATUS        0x01
-#define BL_STATUS_BUSY       0x80
-#define BL_STATUS_RUNNING    0x10
-#define BL_STATUS_DATA_VALID 0x08
-#define BL_STATUS_CSUM_VALID 0x01
-
-/*
- * Bootloader Error Register
- *
- * bit 7: Invalid
- * bit 6: Invalid security key
- * bit 5: Bootloading
- * bit 4: Command checksum
- * bit 3: Flash protection error
- * bit 2: Flash checksum error
- * bit 1 - 0: Reserved
- */
-#define REG_BL_ERROR         0x02
-#define BL_ERROR_INVALID     0x80
-#define BL_ERROR_INVALID_KEY 0x40
-#define BL_ERROR_BOOTLOADING 0x20
-#define BL_ERROR_CMD_CSUM    0x10
-#define BL_ERROR_FLASH_PROT  0x08
-#define BL_ERROR_FLASH_CSUM  0x04
-
-#define BL_STATUS_SIZE  3  /* length of bootloader status registers */
-#define BLK_HEAD_BYTES 32
-
-#define PRODUCT_ID_SIZE  16
-#define QUERY_DATA_SIZE  27
-#define REG_PROTOCOL_GEN_QUERY_OFFSET  20
-
-#define REG_OFFSET_DATA_BASE     0x0000
-#define REG_OFFSET_COMMAND_BASE  0x0028
-#define REG_OFFSET_QUERY_BASE    0x002a
-
-#define CAPABILITY_LEFT_BTN_MASK	(0x01 << 3)
-#define CAPABILITY_RIGHT_BTN_MASK	(0x01 << 4)
-#define CAPABILITY_MIDDLE_BTN_MASK	(0x01 << 5)
-#define CAPABILITY_BTN_MASK  (CAPABILITY_LEFT_BTN_MASK | \
-			      CAPABILITY_RIGHT_BTN_MASK | \
-			      CAPABILITY_MIDDLE_BTN_MASK)
-
-#define CYAPA_OFFSET_SOFT_RESET  REG_OFFSET_COMMAND_BASE
-
-#define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
-
-#define PWR_MODE_MASK   0xfc
-#define PWR_MODE_FULL_ACTIVE (0x3f << 2)
-#define PWR_MODE_IDLE        (0x05 << 2) /* default sleep time is 50 ms. */
-#define PWR_MODE_OFF         (0x00 << 2)
-
-#define PWR_STATUS_MASK      0x0c
-#define PWR_STATUS_ACTIVE    (0x03 << 2)
-#define PWR_STATUS_IDLE      (0x02 << 2)
-#define PWR_STATUS_OFF       (0x00 << 2)
-
-/*
- * CYAPA trackpad device states.
- * Used in register 0x00, bit1-0, DeviceStatus field.
- * Other values indicate device is in an abnormal state and must be reset.
- */
-#define CYAPA_DEV_NORMAL  0x03
-#define CYAPA_DEV_BUSY    0x01
-
-enum cyapa_state {
-	CYAPA_STATE_OP,
-	CYAPA_STATE_BL_IDLE,
-	CYAPA_STATE_BL_ACTIVE,
-	CYAPA_STATE_BL_BUSY,
-	CYAPA_STATE_NO_DEVICE,
-};
-
-
-struct cyapa_touch {
-	/*
-	 * high bits or x/y position value
-	 * bit 7 - 4: high 4 bits of x position value
-	 * bit 3 - 0: high 4 bits of y position value
-	 */
-	u8 xy_hi;
-	u8 x_lo;  /* low 8 bits of x position value. */
-	u8 y_lo;  /* low 8 bits of y position value. */
-	u8 pressure;
-	/* id range is 1 - 15.  It is incremented with every new touch. */
-	u8 id;
-} __packed;
-
-/* The touch.id is used as the MT slot id, thus max MT slot is 15 */
-#define CYAPA_MAX_MT_SLOTS  15
-
-struct cyapa_reg_data {
-	/*
-	 * bit 0 - 1: device status
-	 * bit 3 - 2: power mode
-	 * bit 6 - 4: reserved
-	 * bit 7: interrupt valid bit
-	 */
-	u8 device_status;
-	/*
-	 * bit 7 - 4: number of fingers currently touching pad
-	 * bit 3: valid data check bit
-	 * bit 2: middle mechanism button state if exists
-	 * bit 1: right mechanism button state if exists
-	 * bit 0: left mechanism button state if exists
-	 */
-	u8 finger_btn;
-	/* CYAPA reports up to 5 touches per packet. */
-	struct cyapa_touch touches[5];
-} __packed;
-
-/* The main device structure */
-struct cyapa {
-	enum cyapa_state state;
-
-	struct i2c_client *client;
-	struct input_dev *input;
-	char phys[32];	/* device physical location */
-	int irq;
-	bool irq_wake;  /* irq wake is enabled */
-	bool smbus;
-
-	/* read from query data region. */
-	char product_id[16];
-	u8 btn_capability;
-	u8 gen;
-	int max_abs_x;
-	int max_abs_y;
-	int physical_size_x;
-	int physical_size_y;
-};
-
-static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
-		0x04, 0x05, 0x06, 0x07 };
-static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
-		0x05, 0x06, 0x07 };
-
-struct cyapa_cmd_len {
-	u8 cmd;
-	u8 len;
-};
 
 #define CYAPA_ADAPTER_FUNC_NONE   0
 #define CYAPA_ADAPTER_FUNC_I2C    1
 #define CYAPA_ADAPTER_FUNC_SMBUS  2
 #define CYAPA_ADAPTER_FUNC_BOTH   3
 
-/*
- * macros for SMBus communication
- */
-#define SMBUS_READ   0x01
-#define SMBUS_WRITE 0x00
-#define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
-#define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
-#define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
-#define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
-
- /* for byte read/write command */
-#define CMD_RESET 0
-#define CMD_POWER_MODE 1
-#define CMD_DEV_STATUS 2
-#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
-#define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
-#define CYAPA_SMBUS_POWER_MODE SMBUS_BYTE_CMD(CMD_POWER_MODE)
-#define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
-
- /* for group registers read/write command */
-#define REG_GROUP_DATA 0
-#define REG_GROUP_CMD 2
-#define REG_GROUP_QUERY 3
-#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
-#define CYAPA_SMBUS_GROUP_DATA SMBUS_GROUP_CMD(REG_GROUP_DATA)
-#define CYAPA_SMBUS_GROUP_CMD SMBUS_GROUP_CMD(REG_GROUP_CMD)
-#define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
-
- /* for register block read/write command */
-#define CMD_BL_STATUS 0
-#define CMD_BL_HEAD 1
-#define CMD_BL_CMD 2
-#define CMD_BL_DATA 3
-#define CMD_BL_ALL 4
-#define CMD_BLK_PRODUCT_ID 5
-#define CMD_BLK_HEAD 6
-#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
-
-/* register block read/write command in bootloader mode */
-#define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
-#define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
-#define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
-#define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
-#define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
-
-/* register block read/write command in operational mode */
-#define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
-#define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
-
-static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
-	{ CYAPA_OFFSET_SOFT_RESET, 1 },
-	{ REG_OFFSET_COMMAND_BASE + 1, 1 },
-	{ REG_OFFSET_DATA_BASE, 1 },
-	{ REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
-	{ REG_OFFSET_COMMAND_BASE, 0 },
-	{ REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE },
-	{ BL_HEAD_OFFSET, 3 },

drivers/input/mouse/cyapa.c line 36

Quoted 10 lines
drivers/input/mouse/cyapa.c +3 -7
-	{ BL_HEAD_OFFSET, 16 },
-	{ BL_HEAD_OFFSET, 16 },
-	{ BL_DATA_OFFSET, 16 },
-	{ BL_HEAD_OFFSET, 32 },
-	{ REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
-	{ REG_OFFSET_DATA_BASE, 32 }
-};
+#define CYAPA_DEBUGFS_READ_FW	"read_fw"
+#define CYAPA_DEBUGFS_RAW_DATA	"raw_data"
+#define CYAPA_FW_NAME		"cyapa.bin"

These do not seem to belong to the current patch, please introduce them only when you need them.

Quoted 631 lines
drivers/input/mouse/cyapa.c +191 -364
+
+const char unique_str[] = "CYTRA";
 
-static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
-	{ CYAPA_SMBUS_RESET, 1 },
-	{ CYAPA_SMBUS_POWER_MODE, 1 },
-	{ CYAPA_SMBUS_DEV_STATUS, 1 },
-	{ CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
-	{ CYAPA_SMBUS_GROUP_CMD, 2 },
-	{ CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
-	{ CYAPA_SMBUS_BL_STATUS, 3 },
-	{ CYAPA_SMBUS_BL_HEAD, 16 },
-	{ CYAPA_SMBUS_BL_CMD, 16 },
-	{ CYAPA_SMBUS_BL_DATA, 16 },
-	{ CYAPA_SMBUS_BL_ALL, 32 },
-	{ CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
-	{ CYAPA_SMBUS_BLK_HEAD, 16 },
-};
 
-static ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
+ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
 					u8 *values)
 {
 	return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
+ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
 					 size_t len, const u8 *values)
 {
 	return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-/*
- * cyapa_smbus_read_block - perform smbus block read command
- * @cyapa  - private data structure of the driver
- * @cmd    - the properly encoded smbus command
- * @len    - expected length of smbus command result
- * @values - buffer to store smbus command result
- *
- * Returns negative errno, else the number of bytes written.
- *
- * Note:
- * In trackpad device, the memory block allocated for I2C register map
- * is 256 bytes, so the max read block for I2C bus is 256 bytes.
- */
-static ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
-				      u8 *values)
-{
-	ssize_t ret;
-	u8 index;
-	u8 smbus_cmd;
-	u8 *buf;
-	struct i2c_client *client = cyapa->client;
-
-	if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
-		return -EINVAL;
-
-	if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
-		/* read specific block registers command. */
-		smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-		ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
-		goto out;
-	}
-
-	ret = 0;
-	for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
-		smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
-		smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
-		buf = values + I2C_SMBUS_BLOCK_MAX * index;
-		ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
-		if (ret < 0)
-			goto out;
-	}
-
-out:
-	return ret > 0 ? len : ret;
-}
-
-static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
-{
-	u8 cmd;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	}
-	return i2c_smbus_read_byte_data(cyapa->client, cmd);
-}
-
-static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
-{
-	u8 cmd;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
-	} else {
-		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	}
-	return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
-}
-
-static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
-{
-	u8 cmd;
-	size_t len;
-
-	if (cyapa->smbus) {
-		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-		len = cyapa_smbus_cmds[cmd_idx].len;
-		return cyapa_smbus_read_block(cyapa, cmd, len, values);
-	}
-
-	cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-	len = cyapa_i2c_cmds[cmd_idx].len;
-	return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
-}
-
-/*
- * Query device for its current operating state.
- *
- */
-static int cyapa_get_state(struct cyapa *cyapa)
+/* Returns 0 on success, else negative errno on failure. */
+ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
+					u8 *values)
 {
 	int ret;
-	u8 status[BL_STATUS_SIZE];
-
-	cyapa->state = CYAPA_STATE_NO_DEVICE;
-
-	/*
-	 * Get trackpad status by reading 3 registers starting from 0.
-	 * If the device is in the bootloader, this will be BL_HEAD.
-	 * If the device is in operation mode, this will be the DATA regs.
-	 *
-	 */
-	ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
-				       status);
-
-	/*
-	 * On smbus systems in OP mode, the i2c_reg_read will fail with
-	 * -ETIMEDOUT.  In this case, try again using the smbus equivalent
-	 * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
-	 */
-	if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO))
-		ret = cyapa_read_block(cyapa, CYAPA_CMD_BL_STATUS, status);
-
-	if (ret != BL_STATUS_SIZE)
-		goto error;
-
-	if ((status[REG_OP_STATUS] & OP_STATUS_SRC) == OP_STATUS_SRC) {
-		switch (status[REG_OP_STATUS] & OP_STATUS_DEV) {
-		case CYAPA_DEV_NORMAL:
-		case CYAPA_DEV_BUSY:
-			cyapa->state = CYAPA_STATE_OP;
-			break;
-		default:
-			ret = -EAGAIN;
-			goto error;
-		}
-	} else {
-		if (status[REG_BL_STATUS] & BL_STATUS_BUSY)
-			cyapa->state = CYAPA_STATE_BL_BUSY;
-		else if (status[REG_BL_ERROR] & BL_ERROR_BOOTLOADING)
-			cyapa->state = CYAPA_STATE_BL_ACTIVE;
-		else
-			cyapa->state = CYAPA_STATE_BL_IDLE;
-	}
+	struct i2c_client *client = cyapa->client;
+	struct i2c_msg msgs[] = {
+		{
+			.addr = client->addr,
+			.flags = 0,
+			.len = 1,
+			.buf = &reg,
+		},
+		{
+			.addr = client->addr,
+			.flags = I2C_M_RD,
+			.len = len,
+			.buf = values,
+		},
+	};
+
+	ret = i2c_transfer(client->adapter, msgs, 2);
+
+	if (ret != ARRAY_SIZE(msgs))
+		return ret < 0 ? ret : -EIO;
 
 	return 0;
-error:
-	return (ret < 0) ? ret : -EAGAIN;
 }
 
-/*
- * Poll device for its status in a loop, waiting up to timeout for a response.
- *
- * When the device switches state, it usually takes ~300 ms.
- * However, when running a new firmware image, the device must calibrate its
- * sensors, which can take as long as 2 seconds.
+/**
+ * cyapa_i2c_write - Execute i2c block data write operation
+ * @cyapa: Handle to this driver
+ * @ret: Offset of the data to written in the register map
+ * @len: number of bytes to write
+ * @values: Data to be written
  *
- * Note: The timeout has granularity of the polling rate, which is 100 ms.
- *
- * Returns:
- *   0 when the device eventually responds with a valid non-busy state.
- *   -ETIMEDOUT if device never responds (too many -EAGAIN)
- *   < 0    other errors
+ * Return negative errno code on error; return zero when success.
  */
-static int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+ssize_t cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
+					 size_t len, const void *values)
 {
 	int ret;
-	int tries = timeout / 100;
-
-	ret = cyapa_get_state(cyapa);
-	while ((ret || cyapa->state >= CYAPA_STATE_BL_BUSY) && tries--) {
-		msleep(100);
-		ret = cyapa_get_state(cyapa);
-	}
-	return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
-}
-
-static int cyapa_bl_deactivate(struct cyapa *cyapa)
-{
-	int ret;
-
-	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
-					bl_deactivate);
-	if (ret < 0)
-		return ret;
-
-	/* wait for bootloader to switch to idle state; should take < 100ms */
-	msleep(100);
-	ret = cyapa_poll_state(cyapa, 500);
-	if (ret < 0)
-		return ret;
-	if (cyapa->state != CYAPA_STATE_BL_IDLE)
-		return -EAGAIN;
-	return 0;
+	struct i2c_client *client = cyapa->client;
+	char data[32], *buf;
+
+	if (len > 31) {
+		buf = kzalloc(len + 1, GFP_KERNEL);
+		if (!buf)
+			return -ENOMEM;
+	} else
+		buf = data;
+
+	buf[0] = reg;
+	memcpy(&buf[1], values, len);
+	ret = i2c_master_send(client, buf, len + 1);
+
+	if (buf != data)
+		kfree(buf);
+	return (ret == (len + 1)) ? 0 : ((ret < 0) ? ret : -EIO);
 }
 
-/*
- * Exit bootloader
- *
- * Send bl_exit command, then wait 50 - 100 ms to let device transition to
- * operational mode.  If this is the first time the device's firmware is
- * running, it can take up to 2 seconds to calibrate its sensors.  So, poll
- * the device's new state for up to 2 seconds.
- *
- * Returns:
- *   -EIO    failure while reading from device
- *   -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
- *   0       device is supported and in operational mode
- */
-static int cyapa_bl_exit(struct cyapa *cyapa)
+static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
 {
-	int ret;
-
-	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
-	if (ret < 0)
-		return ret;
-
-	/*
-	 * Wait for bootloader to exit, and operation mode to start.
-	 * Normally, this takes at least 50 ms.
-	 */
-	usleep_range(50000, 100000);
-	/*
-	 * In addition, when a device boots for the first time after being
-	 * updated to new firmware, it must first calibrate its sensors, which
-	 * can take up to an additional 2 seconds.
-	 */
-	ret = cyapa_poll_state(cyapa, 2000);
-	if (ret < 0)
-		return ret;
-	if (cyapa->state != CYAPA_STATE_OP)
-		return -EAGAIN;
+	u8 ret = CYAPA_ADAPTER_FUNC_NONE;
 
-	return 0;
+	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		ret |= CYAPA_ADAPTER_FUNC_I2C;
+	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+				     I2C_FUNC_SMBUS_BLOCK_DATA |
+				     I2C_FUNC_SMBUS_I2C_BLOCK))
+		ret |= CYAPA_ADAPTER_FUNC_SMBUS;
+	return ret;
 }
 
-/*
- * Set device power mode
- *
- */
-static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
+static int cyapa_create_input_dev(struct cyapa *cyapa)
 {
 	struct device *dev = &cyapa->client->dev;
 	int ret;
-	u8 power;
+	struct input_dev *input;
 
-	if (cyapa->state != CYAPA_STATE_OP)
-		return 0;
+	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
+		return -EINVAL;
 
-	ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
-	if (ret < 0)
-		return ret;
+	input = cyapa->input = devm_input_allocate_device(dev);
+	if (!input) {
+		dev_err(dev, "failed to allocate memory for input device.\n");
+		return -ENOMEM;
+	}
 
-	power = ret & ~PWR_MODE_MASK;
-	power |= power_mode & PWR_MODE_MASK;
-	ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
-	if (ret < 0)
-		dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
-			power_mode, ret);
-	return ret;
-}
+	input->name = CYAPA_NAME;
+	input->phys = cyapa->phys;
+	input->id.bustype = BUS_I2C;
+	input->id.version = 1;
+	input->id.product = 0;  /* Means any product in eventcomm. */
+	input->dev.parent = &cyapa->client->dev;
 
-static int cyapa_get_query_data(struct cyapa *cyapa)
-{
-	u8 query_data[QUERY_DATA_SIZE];
-	int ret;
+	input_set_drvdata(input, cyapa);
 
-	if (cyapa->state != CYAPA_STATE_OP)
-		return -EBUSY;
+	__set_bit(EV_ABS, input->evbit);
 
-	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
-	if (ret < 0)
-		return ret;
-	if (ret != QUERY_DATA_SIZE)
-		return -EIO;
+	/* Finger position */
+	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
+			     0);
+	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
+			     0);
+	input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
+	if (cyapa->gen > CYAPA_GEN3) {
+		input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
+		input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
+		/*
+		 * Orientation is the angle between the vertical axis and
+		 * the major axis of the contact ellipse.
+		 * The range is -127 to 127.
+		 * the positive direction is clockwise form the vertical axis.
+		 * If the ellipse of contact degenerates into a circle,
+		 * orientation is reported as 0.
+		 *
+		 * Also, for Gen5 trackpad the accurate of this orientation
+		 * value is value + (-30 ~ 30).
+		 */
+		input_set_abs_params(input, ABS_MT_ORIENTATION,
+				-127, 127, 0, 0);
+	}
+	if (cyapa->gen >= CYAPA_GEN5) {
+		input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
+		input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
+	}
 
-	memcpy(&cyapa->product_id[0], &query_data[0], 5);
-	cyapa->product_id[5] = '-';
-	memcpy(&cyapa->product_id[6], &query_data[5], 6);
-	cyapa->product_id[12] = '-';
-	memcpy(&cyapa->product_id[13], &query_data[11], 2);
-	cyapa->product_id[15] = '\0';
+	input_abs_set_res(input, ABS_MT_POSITION_X,
+			  cyapa->max_abs_x / cyapa->physical_size_x);
+	input_abs_set_res(input, ABS_MT_POSITION_Y,
+			  cyapa->max_abs_y / cyapa->physical_size_y);
 
-	cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
+	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
+		__set_bit(BTN_LEFT, input->keybit);
+	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
+		__set_bit(BTN_MIDDLE, input->keybit);
+	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
+		__set_bit(BTN_RIGHT, input->keybit);
 
-	cyapa->gen = query_data[20] & 0x0f;
+	if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
+		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 
-	cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
-	cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
+	/* Handle pointer emulation and unused slots in core */
+	ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
+				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+	if (ret) {
+		dev_err(dev, "failed to initialize MT slots, %d\n", ret);
+		return ret;
+	}
 
-	cyapa->physical_size_x =
-		((query_data[24] & 0xf0) << 4) | query_data[25];
-	cyapa->physical_size_y =
-		((query_data[24] & 0x0f) << 8) | query_data[26];
+	/* Register the device in input subsystem */
+	ret = input_register_device(input);
+	if (ret) {
+		dev_err(dev, "failed to register input device, %d\n", ret);
+		return ret;
+	}
 
 	return 0;
 }
@@ -635,192 +227,213 @@ static int cyapa_get_query_data(struct cyapa *cyapa)
  */
 static int cyapa_check_is_operational(struct cyapa *cyapa)
 {
-	struct device *dev = &cyapa->client->dev;
-	static const char unique_str[] = "CYTRA";
 	int ret;
 
-	ret = cyapa_poll_state(cyapa, 2000);
-	if (ret < 0)
+	ret = cyapa_poll_state(cyapa, 4000);
+	if (ret)
 		return ret;
-	switch (cyapa->state) {
-	case CYAPA_STATE_BL_ACTIVE:
-		ret = cyapa_bl_deactivate(cyapa);
-		if (ret)
-			return ret;
-
-	/* Fallthrough state */
-	case CYAPA_STATE_BL_IDLE:
-		ret = cyapa_bl_exit(cyapa);
-		if (ret)
-			return ret;
-
-	/* Fallthrough state */
-	case CYAPA_STATE_OP:
-		ret = cyapa_get_query_data(cyapa);
-		if (ret < 0)
-			return ret;
-
-		/* only support firmware protocol gen3 */
-		if (cyapa->gen != CYAPA_GEN3) {
-			dev_err(dev, "unsupported protocol version (%d)",
-				cyapa->gen);
-			return -EINVAL;
-		}
-
-		/* only support product ID starting with CYTRA */
-		if (memcmp(cyapa->product_id, unique_str,
-			   sizeof(unique_str) - 1) != 0) {
-			dev_err(dev, "unsupported product ID (%s)\n",
-				cyapa->product_id);
-			return -EINVAL;
-		}
-		return 0;
 
+	switch (cyapa->gen) {
 	default:
-		return -EIO;
+		return -ENODEV;
 	}
-	return 0;
+
+	if (cyapa->ops->operational_check)
+		ret = cyapa->ops->operational_check(cyapa);
+
+	return ret;
 }
 
+
 static irqreturn_t cyapa_irq(int irq, void *dev_id)
 {
 	struct cyapa *cyapa = dev_id;
 	struct device *dev = &cyapa->client->dev;
 	struct input_dev *input = cyapa->input;
-	struct cyapa_reg_data data;
-	int i;
+	bool cont;
 	int ret;
-	int num_fingers;
 
 	if (device_may_wakeup(dev))
 		pm_wakeup_event(dev, 0);
 
-	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
-	if (ret != sizeof(data))
-		goto out;
-
-	if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
-	    (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
-	    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
-		goto out;
-	}
-
-	num_fingers = (data.finger_btn >> 4) & 0x0f;
-	for (i = 0; i < num_fingers; i++) {
-		const struct cyapa_touch *touch = &data.touches[i];
-		/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
-		int slot = touch->id - 1;
-
-		input_mt_slot(input, slot);
-		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
-		input_report_abs(input, ABS_MT_POSITION_X,
-				 ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
-		input_report_abs(input, ABS_MT_POSITION_Y,
-				 ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
-		input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
+	/* Interrupt event maybe cuased by host command to trackpad device. */
+	cont = true;
+	if (cyapa->ops->irq_cmd_handler)
+		cont = cyapa->ops->irq_cmd_handler(cyapa);
+
+	/* Interrupt event maybe from trackpad device input reporting. */
+	if (cont && cyapa->ops->irq_handler) {
+		if (!input || cyapa->ops->irq_handler(cyapa)) {
+			ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+			if (ret && cyapa->ops->sort_empty_output_data) {
+				cyapa->ops->sort_empty_output_data(cyapa,
+					NULL, NULL, NULL);
+				goto out;
+			}
+			cyapa_detect(cyapa);
+			mutex_unlock(&cyapa->state_sync_lock);
+		}
 	}
 
-	input_mt_sync_frame(input);
-
-	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-		input_report_key(input, BTN_LEFT,
-				 data.finger_btn & OP_DATA_LEFT_BTN);
-
-	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-		input_report_key(input, BTN_MIDDLE,
-				 data.finger_btn & OP_DATA_MIDDLE_BTN);
-
-	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-		input_report_key(input, BTN_RIGHT,
-				 data.finger_btn & OP_DATA_RIGHT_BTN);
-
-	input_sync(input);
-
 out:
 	return IRQ_HANDLED;
 }
 
-static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
+/*
+ * Query device for its current operating state.
+ */
+static int cyapa_get_state(struct cyapa *cyapa)
 {
-	u8 ret = CYAPA_ADAPTER_FUNC_NONE;
+	return -ENODEV;
+}
 
-	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
-		ret |= CYAPA_ADAPTER_FUNC_I2C;
-	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
-				     I2C_FUNC_SMBUS_BLOCK_DATA |
-				     I2C_FUNC_SMBUS_I2C_BLOCK))
-		ret |= CYAPA_ADAPTER_FUNC_SMBUS;
-	return ret;
+/*
+ * Poll device for its status in a loop, waiting up to timeout for a response.
+ *
+ * When the device switches state, it usually takes ~300 ms.
+ * However, when running a new firmware image, the device must calibrate its
+ * sensors, which can take as long as 2 seconds.
+ *
+ * Note: The timeout has granularity of the polling rate, which is 100 ms.
+ *
+ * Returns:
+ *   0 when the device eventually responds with a valid non-busy state.
+ *   -ETIMEDOUT if device never responds (too many -EAGAIN)
+ *   < 0    other errors
+ */
+int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+{
+	int ret;
+	int tries = timeout / 100;
+
+	ret = cyapa_get_state(cyapa);
+	while ((ret || cyapa->state <= CYAPA_STATE_BL_BUSY) && tries--) {
+		msleep(100);
+		ret = cyapa_get_state(cyapa);
+	}
+
+	return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
 }

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 325

Quoted 10 lines
drivers/input/mouse/cyapa.c +8 -1
 
-static int cyapa_create_input_dev(struct cyapa *cyapa)
+/*
+ * Returns:
+ *   0    when device is detected, and operatioinal.
+ *   > 0  when device is detected, but not operational.
+ *   < 0  when device is not detected, or other errors.
+ *          e.g.: IO communication error with the device.
+ */
+int cyapa_detect(struct cyapa *cyapa)

We have cyapa->state. Can we check it to see if device is operational or not? Or introduce another variable instead of returning tristate from cyapa_detect?

Quoted 11 lines
drivers/input/mouse/cyapa.c +4 -3
 {
 	struct device *dev = &cyapa->client->dev;
+	char *envp[2] = {"ERROR=1", NULL};
 	int ret;
-	struct input_dev *input;
 
-	if (!cyapa->physical_size_x || !cyapa->physical_size_y)
-		return -EINVAL;
+	ret = cyapa_check_is_operational(cyapa);
+	if (ret) {
+		if (ret != -ETIMEDOUT && ret != -ENODEV &&

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 343

Quoted 10 lines
drivers/input/mouse/cyapa.c +10 -0
+			((cyapa->gen == CYAPA_GEN3 &&
+				cyapa->state >= CYAPA_STATE_BL_BUSY &&
+				cyapa->state <= CYAPA_STATE_BL_ACTIVE) ||
+			(cyapa->gen == CYAPA_GEN5 &&
+				cyapa->state == CYAPA_STATE_GEN5_BL))) {
+			dev_warn(dev, "device detected, but no operatinal.\n");
+			dev_warn(dev, "device gen=%d, state=0x%02x\n",
+				cyapa->gen, cyapa->state);
+			dev_warn(dev, "munually fw image recover required.\n");
+			return 1;

Hmm, what userspace uses this kind of notification?

Quoted 81 lines
drivers/input/mouse/cyapa.c +51 -22
+		}
 
-	input = cyapa->input = devm_input_allocate_device(dev);
-	if (!input) {
-		dev_err(dev, "allocate memory for input device failed\n");
-		return -ENOMEM;
+		dev_err(dev, "no device detected, (%d)\n", ret);
+		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
+		return ret;
 	}
 
-	input->name = CYAPA_NAME;
-	input->phys = cyapa->phys;
-	input->id.bustype = BUS_I2C;
-	input->id.version = 1;
-	input->id.product = 0;  /* means any product in eventcomm. */
-	input->dev.parent = &cyapa->client->dev;
+	if (!cyapa->input) {
+		ret = cyapa_create_input_dev(cyapa);
+		if (ret) {
+			dev_err(dev, "create input_dev instance failed, (%d)\n",
+				ret);
+			return ret;
+		}
 
-	input_set_drvdata(input, cyapa);
+		/*
+		 * On some systems, a system crash / warm boot does not reset
+		 * the device's current power mode to FULL_ACTIVE.
+		 * If such an event happens during suspend, after the device
+		 * has been put in a low power mode, the device will still be
+		 * in low power mode on a subsequent boot, since there was
+		 * never a matching resume().
+		 * Handle this by always forcing full power here, when a
+		 * device is first detected to be in operational mode.
+		 */
+		if (cyapa->ops->set_power_mode) {
+			ret = cyapa->ops->set_power_mode(cyapa,
+					PWR_MODE_FULL_ACTIVE, 0);
+			if (ret)
+				dev_warn(dev, "set active power failed, (%d)\n",
+						ret);
+		}
+	}
 
-	__set_bit(EV_ABS, input->evbit);
+	return 0;
+}
 
-	/* finger position */
-	input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
-			     0);
-	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
-			     0);
-	input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
+/*
+ * Sysfs Interface.
+ */
 
-	input_abs_set_res(input, ABS_MT_POSITION_X,
-			  cyapa->max_abs_x / cyapa->physical_size_x);
-	input_abs_set_res(input, ABS_MT_POSITION_Y,
-			  cyapa->max_abs_y / cyapa->physical_size_y);
+/*
+ * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
+ *
+ * These are helper functions that convert to and from integer idle
+ * times and register settings to write to the PowerMode register.
+ * The trackpad supports between 20ms to 1000ms scan intervals.
+ * The time will be increased in increments of 10ms from 20ms to 100ms.
+ * From 100ms to 1000ms, time will be increased in increments of 20ms.
+ *
+ * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 10;
+ * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 20 + 5;
+ */
+u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
+{
+	sleep_time = clamp_val(sleep_time, 20, 1000);
 

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 406

Quoted 10 lines
drivers/input/mouse/cyapa.c +4 -6
-	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-		__set_bit(BTN_LEFT, input->keybit);
-	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-		__set_bit(BTN_MIDDLE, input->keybit);
-	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-		__set_bit(BTN_RIGHT, input->keybit);
+	if (sleep_time < 100)
+		return ((sleep_time / 10) << 2) & PWR_MODE_MASK;
+	return ((sleep_time / 20 + 5) << 2) & PWR_MODE_MASK;
+}

Maybe:

	encoded_time = sleep_time < 100 ?
			sleep_time / 10 : sleep_time / 20 + 5;
	return (encode_time << 2) & PWR_MODE_MASK;
Quoted 138 lines
drivers/input/mouse/cyapa.c +45 -45
-	if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
-		__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
+u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
+{
+	u8 encoded_time = pwr_mode >> 2;
 
-	/* handle pointer emulation and unused slots in core */
-	ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
-				  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
-	if (ret) {
-		dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
-		goto err_free_device;
-	}
+	return (encoded_time < 10) ? encoded_time * 10
+				   : (encoded_time - 5) * 20;
+}
 
-	/* Register the device in input subsystem */
-	ret = input_register_device(input);
-	if (ret) {
-		dev_err(dev, "input device register failed, %d\n", ret);
-		goto err_free_device;
-	}
-	return 0;
+/*
+ * Returns:
+ *   0    Driver and device initialization successfully done.
+ *   > 0  Driver initialized and device is detected, but not operational yet.
+ *   < 0  Device is not detected, or driver initialization failed.
+ */
+static int cyapa_initialize(struct cyapa *cyapa)
+{
+	cyapa->state = CYAPA_STATE_NO_DEVICE;
+	cyapa->gen = CYAPA_GEN_UNKNOWN;
+	mutex_init(&cyapa->state_sync_lock);
 
-err_free_device:
-	input_free_device(input);
-	cyapa->input = NULL;
-	return ret;
+	/*
+	 * Set to hard code default, they will be updated with trackpad set
+	 * default values after probe and initialized.
+	 */
+	cyapa->suspend_power_mode = PWR_MODE_SLEEP;
+	cyapa->suspend_sleep_time =
+		cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
+
+	return cyapa_detect(cyapa);
 }
 
 static int cyapa_probe(struct i2c_client *client,
@@ -830,6 +443,7 @@ static int cyapa_probe(struct i2c_client *client,
 	u8 adapter_func;
 	struct cyapa *cyapa;
 	struct device *dev = &client->dev;
+	union i2c_smbus_data dummy;
 
 	adapter_func = cyapa_check_adapter_functionality(client);
 	if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
@@ -837,48 +451,39 @@ static int cyapa_probe(struct i2c_client *client,
 		return -EIO;
 	}
 
+	/* Make sure there is something at this address */
+	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
+			I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
+		return -ENODEV;
+
 	cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
 	if (!cyapa)
 		return -ENOMEM;
 
-	cyapa->gen = CYAPA_GEN3;
-	cyapa->client = client;
-	i2c_set_clientdata(client, cyapa);
-	sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
-		client->addr);
-
 	/* i2c isn't supported, use smbus */
 	if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
 		cyapa->smbus = true;
-	cyapa->state = CYAPA_STATE_NO_DEVICE;
-	ret = cyapa_check_is_operational(cyapa);
-	if (ret) {
-		dev_err(dev, "device not operational, %d\n", ret);
-		return ret;
-	}
 
-	ret = cyapa_create_input_dev(cyapa);
-	if (ret) {
-		dev_err(dev, "create input_dev instance failed, %d\n", ret);
-		return ret;
-	}
+	cyapa->client = client;
+	i2c_set_clientdata(client, cyapa);
+	sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
+		client->addr);
 
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-	if (ret) {
-		dev_err(dev, "set active power failed, %d\n", ret);
+	ret = cyapa_initialize(cyapa);
+	if (ret < 0) {
+		dev_err(dev, "failed to detect and initialize tp device.\n");
 		return ret;
 	}
 
-	cyapa->irq = client->irq;
 	ret = devm_request_threaded_irq(dev,
-					cyapa->irq,
+					client->irq,
 					NULL,
 					cyapa_irq,
 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 					"cyapa",
 					cyapa);
 	if (ret) {
-		dev_err(dev, "IRQ request failed: %d\n, ", ret);
+		dev_err(dev, "failed to request threaded irq, (%d)\n, ", ret);
 		return ret;
 	}
 
@@ -889,8 +494,10 @@ static int cyapa_remove(struct i2c_client *client)
 {
 	struct cyapa *cyapa = i2c_get_clientdata(client);
 
-	disable_irq(cyapa->irq);
-	cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+	disable_irq(cyapa->client->irq);
+
+	if (cyapa->ops->set_power_mode)
+		cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
 
 	return 0;
 }

drivers/input/mouse/cyapa.c static int cyapa_suspend(struct device *dev) · line 516

Quoted 9 lines
drivers/input/mouse/cyapa.c +5 -1
@@ -902,20 +509,38 @@ static int cyapa_suspend(struct device *dev)
 	u8 power_mode;
 	struct cyapa *cyapa = dev_get_drvdata(dev);
 
-	disable_irq(cyapa->irq);
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret) {
+		dev_err(dev, "suspend interrupted by signal, (%d)\n", ret);
+		return ret;
+	}

For patterns:

	var = action();
	if (var) {
		.. error handling ...
		return var;
	}

	return 0;

i.e. when value of 'var' ony used in error handling path I strongly prefer to call 'var' error. If you do return value of 'var' in both error and success paths then 'ret' or 'retval' are preferred names.

Quoted 10 lines
drivers/input/mouse/cyapa.c +8 -0
+
+	/*
+	 * Disable IRQ to avoid the command response interrupt cause system
+	 * suspending process interrupted and failed.
+	 * Because the gen5 devices will always assert interrupt to host after
+	 * executed the set power mode command.
+	 */
+	disable_irq(cyapa->client->irq);
 
 	/*

drivers/input/mouse/cyapa.c static int cyapa_suspend(struct device *dev) · line 532

Quoted 10 lines
drivers/input/mouse/cyapa.c +2 -4
 	 * Set trackpad device to idle mode if wakeup is allowed,
 	 * otherwise turn off.
 	 */
-	power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
+	power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
 					    : PWR_MODE_OFF;
-	ret = cyapa_set_power_mode(cyapa, power_mode);
-	if (ret < 0)
-		dev_err(dev, "set power mode failed, %d\n", ret);
+	if (cyapa->input && cyapa->ops->set_power_mode) {

The gating on cyapa->input is not obvious. Should we check the device status (or introduce one to indicate that the device in fully initialized state). Also, instead of checking presence of various methods I'd rather have providers supply stubs if they do not implement something.

Quoted 308 lines
drivers/input/mouse/cyapa.c +26 -8
+		ret = cyapa->ops->set_power_mode(cyapa, power_mode,
+				cyapa->suspend_sleep_time);
+		if (ret < 0)
+			dev_err(dev, "suspend set power mode failed, %d\n",
+					ret);
+	}
 
 	if (device_may_wakeup(dev))
-		cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
+		cyapa->irq_wake = (enable_irq_wake(cyapa->client->irq) == 0);
+
+	mutex_unlock(&cyapa->state_sync_lock);
 	return 0;
 }
 
@@ -924,19 +549,29 @@ static int cyapa_resume(struct device *dev)
 	int ret;
 	struct cyapa *cyapa = dev_get_drvdata(dev);
 
-	if (device_may_wakeup(dev) && cyapa->irq_wake)
-		disable_irq_wake(cyapa->irq);
-
-	ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
 	if (ret)
-		dev_warn(dev, "resume active power failed, %d\n", ret);
+		dev_err(dev, "resume interrupted by signal, (%d)\n", ret);
 
-	enable_irq(cyapa->irq);
+	if (cyapa->irq_wake) {
+		disable_irq_wake(cyapa->client->irq);
+		cyapa->irq_wake = false;
+	}
+
+	/* Reset to active power state after re-detected. */
+	cyapa_detect(cyapa);
+
+	enable_irq(cyapa->client->irq);
+
+	if (!ret)
+		mutex_unlock(&cyapa->state_sync_lock);
 	return 0;
 }
 #endif /* CONFIG_PM_SLEEP */
 
-static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
+static const struct dev_pm_ops cyapa_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
+};
 
 static const struct i2c_device_id cyapa_id_table[] = {
 	{ "cyapa", 0 },
drivers/input/mouse/cyapa.h new file +250 -0
new file mode 100644
index 0000000..ee97d7c
@@ -0,0 +1,321 @@
+/*
+ * Cypress APA trackpad with I2C interface
+ *
+ * Author: Dudley Du <[email protected]>
+ *
+ * Copyright (C) 2014 Cypress Semiconductor, Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#ifndef _CYAPA_H
+#define _CYAPA_H
+
+#include <linux/async.h>
+#include <linux/firmware.h>
+#include <linux/regulator/consumer.h>
+#include <linux/regulator/driver.h>
+
+/* APA trackpad firmware generation number. */
+#define CYAPA_GEN_UNKNOWN   0x00   /* unknown protocol. */
+#define CYAPA_GEN3   0x03   /* support MT-protocol B with tracking ID. */
+#define CYAPA_GEN5   0x05   /* support TrueTouch GEN5 trackpad device. */
+
+#define CYAPA_NAME   "Cypress APA Trackpad (cyapa)"
+
+/*
+ * Macros for SMBus communication
+ */
+#define SMBUS_READ   0x01
+#define SMBUS_WRITE 0x00
+#define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
+#define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
+#define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
+#define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
+
+/* Commands for read/write registers of Cypress trackpad */
+#define CYAPA_CMD_SOFT_RESET       0x00
+#define CYAPA_CMD_POWER_MODE       0x01
+#define CYAPA_CMD_DEV_STATUS       0x02
+#define CYAPA_CMD_GROUP_DATA       0x03
+#define CYAPA_CMD_GROUP_CMD        0x04
+#define CYAPA_CMD_GROUP_QUERY      0x05
+#define CYAPA_CMD_BL_STATUS        0x06
+#define CYAPA_CMD_BL_HEAD          0x07
+#define CYAPA_CMD_BL_CMD           0x08
+#define CYAPA_CMD_BL_DATA          0x09
+#define CYAPA_CMD_BL_ALL           0x0a
+#define CYAPA_CMD_BLK_PRODUCT_ID   0x0b
+#define CYAPA_CMD_BLK_HEAD         0x0c
+#define CYAPA_CMD_MAX_BASELINE     0x0d
+#define CYAPA_CMD_MIN_BASELINE     0x0e
+
+#define BL_HEAD_OFFSET 0x00
+#define BL_DATA_OFFSET 0x10
+
+#define BL_STATUS_SIZE  3  /* Length of gen3 bootloader status registers */
+#define CYAPA_REG_MAP_SIZE  256
+
+/*
+ * Gen3 Operational Device Status Register
+ *
+ * bit 7: Valid interrupt source
+ * bit 6 - 4: Reserved
+ * bit 3 - 2: Power status
+ * bit 1 - 0: Device status
+ */
+#define REG_OP_STATUS     0x00
+#define OP_STATUS_SRC     0x80
+#define OP_STATUS_POWER   0x0c
+#define OP_STATUS_DEV     0x03
+#define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER | OP_STATUS_DEV)
+
+/*
+ * Operational Finger Count/Button Flags Register
+ *
+ * bit 7 - 4: Number of touched finger
+ * bit 3: Valid data
+ * bit 2: Middle Physical Button
+ * bit 1: Right Physical Button
+ * bit 0: Left physical Button
+ */
+#define REG_OP_DATA1       0x01
+#define OP_DATA_VALID      0x08
+#define OP_DATA_MIDDLE_BTN 0x04
+#define OP_DATA_RIGHT_BTN  0x02
+#define OP_DATA_LEFT_BTN   0x01
+#define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN | \
+			  OP_DATA_LEFT_BTN)
+
+/*
+ * Write-only command file register used to issue commands and
+ * parameters to the bootloader.
+ * The default value read from it is always 0x00.
+ */
+#define REG_BL_FILE	0x00
+#define BL_FILE		0x00
+
+/*
+ * Bootloader Status Register
+ *
+ * bit 7: Busy
+ * bit 6 - 5: Reserved
+ * bit 4: Bootloader running
+ * bit 3 - 2: Reserved
+ * bit 1: Watchdog Reset
+ * bit 0: Checksum valid
+ */
+#define REG_BL_STATUS        0x01
+#define BL_STATUS_REV_6_5    0x60
+#define BL_STATUS_BUSY       0x80
+#define BL_STATUS_RUNNING    0x10
+#define BL_STATUS_REV_3_2    0x0c
+#define BL_STATUS_WATCHDOG   0x02
+#define BL_STATUS_CSUM_VALID 0x01
+#define BL_STATUS_REV_MASK (BL_STATUS_WATCHDOG | BL_STATUS_REV_3_2 | \
+			    BL_STATUS_REV_6_5)
+
+/*
+ * Bootloader Error Register
+ *
+ * bit 7: Invalid
+ * bit 6: Invalid security key
+ * bit 5: Bootloading
+ * bit 4: Command checksum
+ * bit 3: Flash protection error
+ * bit 2: Flash checksum error
+ * bit 1 - 0: Reserved
+ */
+#define REG_BL_ERROR         0x02
+#define BL_ERROR_INVALID     0x80
+#define BL_ERROR_INVALID_KEY 0x40
+#define BL_ERROR_BOOTLOADING 0x20
+#define BL_ERROR_CMD_CSUM    0x10
+#define BL_ERROR_FLASH_PROT  0x08
+#define BL_ERROR_FLASH_CSUM  0x04
+#define BL_ERROR_RESERVED    0x03
+#define BL_ERROR_NO_ERR_IDLE    0x00
+#define BL_ERROR_NO_ERR_ACTIVE  (BL_ERROR_BOOTLOADING)
+
+#define CAPABILITY_BTN_SHIFT            3
+#define CAPABILITY_LEFT_BTN_MASK	(0x01 << 3)
+#define CAPABILITY_RIGHT_BTN_MASK	(0x01 << 4)
+#define CAPABILITY_MIDDLE_BTN_MASK	(0x01 << 5)
+#define CAPABILITY_BTN_MASK  (CAPABILITY_LEFT_BTN_MASK | \
+			      CAPABILITY_RIGHT_BTN_MASK | \
+			      CAPABILITY_MIDDLE_BTN_MASK)
+
+#define PWR_MODE_MASK   0xfc
+#define PWR_MODE_FULL_ACTIVE (0x3f << 2)
+#define PWR_MODE_IDLE        (0x03 << 2) /* Default rt suspend scanrate: 30ms */
+#define PWR_MODE_SLEEP       (0x05 << 2) /* Default suspend scanrate: 50ms */
+#define PWR_MODE_BTN_ONLY    (0x01 << 2)
+#define PWR_MODE_OFF         (0x00 << 2)
+
+#define PWR_STATUS_MASK      0x0c
+#define PWR_STATUS_ACTIVE    (0x03 << 2)
+#define PWR_STATUS_IDLE      (0x02 << 2)
+#define PWR_STATUS_BTN_ONLY  (0x01 << 2)
+#define PWR_STATUS_OFF       (0x00 << 2)
+
+#define AUTOSUSPEND_DELAY   2000 /* unit : ms */
+
+#define UNINIT_SLEEP_TIME 0xFFFF
+#define UNINIT_PWR_MODE   0xFF
+
+#define BTN_ONLY_MODE_NAME   "buttononly"
+#define OFF_MODE_NAME        "off"
+
+/* The touch.id is used as the MT slot id, thus max MT slot is 15 */
+#define CYAPA_MAX_MT_SLOTS  15
+
+struct cyapa;
+
+typedef bool (*cb_sort)(struct cyapa *, u8 *, int);
+
+struct cyapa_dev_ops {
+	int (*check_fw)(struct cyapa *, const struct firmware *);
+	int (*bl_enter)(struct cyapa *);
+	int (*bl_activate)(struct cyapa *);
+	int (*bl_initiate)(struct cyapa *, const struct firmware *);
+	int (*update_fw)(struct cyapa *, const struct firmware *);
+	int (*bl_verify_app_integrity)(struct cyapa *);
+	int (*bl_deactivate)(struct cyapa *);
+
+	ssize_t (*show_baseline)(struct device *,
+			struct device_attribute *, char *);
+	ssize_t (*calibrate_store)(struct device *,
+			struct device_attribute *, const char *, size_t);
+
+	int (*initialize)(struct cyapa *cyapa);
+
+	int (*state_parse)(struct cyapa *cyapa, u8 *reg_status, int len);
+	int (*operational_check)(struct cyapa *cyapa);
+
+	int (*irq_handler)(struct cyapa *);
+	bool (*irq_cmd_handler)(struct cyapa *);
+	int (*sort_empty_output_data)(struct cyapa *,
+			u8 *, int *, cb_sort);
+
+	int (*set_power_mode)(struct cyapa *, u8, u16);
+};
+
+struct cyapa_gen5_cmd_states {
+	struct mutex cmd_lock;
+	struct completion cmd_ready;
+	atomic_t cmd_issued;
+	u8 in_progress_cmd;
+	bool is_irq_mode;
+
+	cb_sort resp_sort_func;
+	u8 *resp_data;
+	int *resp_len;
+
+	u8 irq_cmd_buf[CYAPA_REG_MAP_SIZE];
+	u8 empty_buf[CYAPA_REG_MAP_SIZE];
+};
+
+union cyapa_cmd_states {
+	struct cyapa_gen5_cmd_states gen5;
+};
+
+enum cyapa_state {
+	CYAPA_STATE_NO_DEVICE,
+	CYAPA_STATE_BL_BUSY,
+	CYAPA_STATE_BL_IDLE,
+	CYAPA_STATE_BL_ACTIVE,
+	CYAPA_STATE_OP,
+	CYAPA_STATE_GEN5_BL,
+	CYAPA_STATE_GEN5_APP,
+};
+
+struct cyapa_tsg_bin_image_head {
+	u8 head_size;  /* Unit: bytes, including itself. */
+	u8 ttda_driver_major_version;  /* Reserved as 0. */
+	u8 ttda_driver_minor_version;  /* Reserved as 0. */
+	u8 fw_major_version;
+	u8 fw_minor_version;
+	u8 fw_revision_control_number[8];
+} __packed;
+
+/* The main device structure */
+struct cyapa {
+	enum cyapa_state state;
+	u8 status[BL_STATUS_SIZE];
+
+	struct i2c_client *client;
+	struct input_dev *input;
+	char phys[32];	/* Device physical location */

drivers/input/mouse/cyapa.h line 260

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+	bool irq_wake;  /* Irq wake is enabled */
+	bool smbus;
+
+	/* power mode settings */
+	u8 suspend_power_mode;
+	u16 suspend_sleep_time;
+#ifdef CONFIG_PM_RUNTIME
+	u8 runtime_suspend_power_mode;
+	u16 runtime_suspend_sleep_time;
+#endif /* CONFIG_PM_RUNTIME */

This I think should be introduce later, with runtime PM changes.

Quoted 35 lines
drivers/input/mouse/cyapa.h new file +35 -0
+	u8 dev_pwr_mode;
+	u16 dev_sleep_time;
+
+	/* Read from query data region. */
+	char product_id[16];
+	u8 fw_maj_ver;  /* Firmware major version. */
+	u8 fw_min_ver;  /* Firmware minor version. */
+	u8 btn_capability;
+	u8 gen;
+	int max_abs_x;
+	int max_abs_y;
+	int physical_size_x;
+	int physical_size_y;
+
+	/* Used in ttsp and truetouch based trackpad devices. */
+	u8 x_origin;  /* X Axis Origin: 0 = left side; 1 = rigth side. */
+	u8 y_origin;  /* Y Axis Origin: 0 = top; 1 = bottom. */
+	int electrodes_x;  /* Number of electrodes on the X Axis*/
+	int electrodes_y;  /* Number of electrodes on the Y Axis*/
+	int electrodes_rx;  /* Number of Rx electrodes */
+	int max_z;
+
+	/*
+	 * Used to synchronize the access or update the device state.
+	 * And since update firmware and read firmware image process will take
+	 * quite long time, maybe more than 10 seconds, so use mutex_lock
+	 * to sync and wait other interface and detecting are done or ready.
+	 */
+	struct mutex state_sync_lock;
+
+	const struct cyapa_dev_ops *ops;
+
+	union cyapa_cmd_states cmd_states;
+};
+

drivers/input/mouse/cyapa.h line 305

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+
+ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
+				u8 *values);
+ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
+				size_t len, const u8 *values);
+ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
+				u8 *values);
+
+s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx);
+s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value);

Do we still use these 2 fucntions?

Quoted 19 lines
drivers/input/mouse/cyapa.h new file +16 -0
+ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values);
+
+ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len, u8 *values);
+ssize_t cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
+				size_t len, const void *values);
+
+int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout);
+int cyapa_detect(struct cyapa *cyapa);
+
+u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time);
+u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
+
+
+extern const char unique_str[];
+
+#endif
Signature · 1 line
1.9.1

Thanks.

Signature · 1 line
Dmitry
Applied
#7 lore raw

RE: [PATCH v9 02/18] input: cyapa: re-design driver to support multi-trackpad in one driver

Thanks, Dmitry

Quoted 22 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:19
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 02/18] input: cyapa: re-design driver to support multi-trackpad in one driver

Hi Dudley,

Quoted 9 lines · Dudley Du

In order to support multiple different chipsets and communication protocols trackpad devices in one cyapa driver, the new cyapa driver is re-designed with one cyapa driver core and multiple device specific functions component. The cyapa driver core is contained in this patch, it supplies basic functions that working with kernel and input subsystem, and also supplies the interfaces that the specific devices' component can connect and work together with as one driver.

Quoted 7 lines

Thank you for addressing my previous comments, the driver looks much better now. Still, a few comments.

We should try to avoid breaking the driver withing the patch series, so please merge this change together with the next patch re-adding the gen3 support. The gen5 is new functionality and can stay in separate patch(es).

Thanks for your review and comments.
I will re-adding the gen3 support in next upstream.

Quoted 311 lines
Quoted 58 lines
---
 drivers/input/mouse/cyapa.c | 1087 ++++++++++++++-----------------------------
 drivers/input/mouse/cyapa.h |  321 +++++++++++++
 2 files changed, 682 insertions(+), 726 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa.h
drivers/input/mouse/cyapa.c +4 -30
index b3d7a2a..5029618 100644
@@ -6,7 +6,7 @@
  *   Daniel Kurtz <[email protected]>
  *   Benson Leung <[email protected]>
  *
- * Copyright (C) 2011-2012 Cypress Semiconductor, Inc.
+ * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
  * Copyright (C) 2011-2012 Google, Inc.
  *
  * This file is subject to the terms and conditions of the GNU General Public
@@ -20,601 +20,193 @@
 #include <linux/input/mt.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/uaccess.h>
+#include "cyapa.h"
 
-/* APA trackpad firmware generation */
-#define CYAPA_GEN3   0x03   /* support MT-protocol B with tracking ID. */
-
-#define CYAPA_NAME   "Cypress APA Trackpad (cyapa)"
-
-/* commands for read/write registers of Cypress trackpad */
-#define CYAPA_CMD_SOFT_RESET       0x00
-#define CYAPA_CMD_POWER_MODE       0x01
-#define CYAPA_CMD_DEV_STATUS       0x02
-#define CYAPA_CMD_GROUP_DATA       0x03
-#define CYAPA_CMD_GROUP_CMD        0x04
-#define CYAPA_CMD_GROUP_QUERY      0x05
-#define CYAPA_CMD_BL_STATUS        0x06
-#define CYAPA_CMD_BL_HEAD          0x07
-#define CYAPA_CMD_BL_CMD           0x08
-#define CYAPA_CMD_BL_DATA          0x09
-#define CYAPA_CMD_BL_ALL           0x0a
-#define CYAPA_CMD_BLK_PRODUCT_ID   0x0b
-#define CYAPA_CMD_BLK_HEAD         0x0c
-
-/* report data start reg offset address. */
-#define DATA_REG_START_OFFSET  0x0000
-
-#define BL_HEAD_OFFSET 0x00
-#define BL_DATA_OFFSET 0x10
-
-/*
- * Operational Device Status Register
- *

drivers/input/mouse/cyapa.c line 63

Quoted 10 lines
drivers/input/mouse/cyapa.c +0 -10
- * bit 7: Valid interrupt source
- * bit 6 - 4: Reserved
- * bit 3 - 2: Power status
- * bit 1 - 0: Device status
- */
-#define REG_OP_STATUS     0x00
-#define OP_STATUS_SRC     0x80
-#define OP_STATUS_POWER   0x0c
-#define OP_STATUS_DEV     0x03
-#define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER |

OP_STATUS_DEV)

Quoted 6 lines
drivers/input/mouse/cyapa.c +0 -6
-
-/*
- * Operational Finger Count/Button Flags Register
- *
- * bit 7 - 4: Number of touched finger
- * bit 3: Valid data

drivers/input/mouse/cyapa.c

Quoted 10 lines
drivers/input/mouse/cyapa.c +0 -10
- * bit 2: Middle Physical Button
- * bit 1: Right Physical Button
- * bit 0: Left physical Button
- */
-#define REG_OP_DATA1       0x01
-#define OP_DATA_VALID      0x08
-#define OP_DATA_MIDDLE_BTN 0x04
-#define OP_DATA_RIGHT_BTN  0x02
-#define OP_DATA_LEFT_BTN   0x01
-#define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN

| \

Quoted 165 lines
drivers/input/mouse/cyapa.c +0 -159
-  OP_DATA_LEFT_BTN)
-
-/*
- * Bootloader Status Register
- *
- * bit 7: Busy
- * bit 6 - 5: Reserved
- * bit 4: Bootloader running
- * bit 3 - 1: Reserved
- * bit 0: Checksum valid
- */
-#define REG_BL_STATUS        0x01
-#define BL_STATUS_BUSY       0x80
-#define BL_STATUS_RUNNING    0x10
-#define BL_STATUS_DATA_VALID 0x08
-#define BL_STATUS_CSUM_VALID 0x01
-
-/*
- * Bootloader Error Register
- *
- * bit 7: Invalid
- * bit 6: Invalid security key
- * bit 5: Bootloading
- * bit 4: Command checksum
- * bit 3: Flash protection error
- * bit 2: Flash checksum error
- * bit 1 - 0: Reserved
- */
-#define REG_BL_ERROR         0x02
-#define BL_ERROR_INVALID     0x80
-#define BL_ERROR_INVALID_KEY 0x40
-#define BL_ERROR_BOOTLOADING 0x20
-#define BL_ERROR_CMD_CSUM    0x10
-#define BL_ERROR_FLASH_PROT  0x08
-#define BL_ERROR_FLASH_CSUM  0x04
-
-#define BL_STATUS_SIZE  3  /* length of bootloader status registers */
-#define BLK_HEAD_BYTES 32
-
-#define PRODUCT_ID_SIZE  16
-#define QUERY_DATA_SIZE  27
-#define REG_PROTOCOL_GEN_QUERY_OFFSET  20
-
-#define REG_OFFSET_DATA_BASE     0x0000
-#define REG_OFFSET_COMMAND_BASE  0x0028
-#define REG_OFFSET_QUERY_BASE    0x002a
-
-#define CAPABILITY_LEFT_BTN_MASK(0x01 << 3)
-#define CAPABILITY_RIGHT_BTN_MASK(0x01 << 4)
-#define CAPABILITY_MIDDLE_BTN_MASK(0x01 << 5)
-#define CAPABILITY_BTN_MASK  (CAPABILITY_LEFT_BTN_MASK | \
-      CAPABILITY_RIGHT_BTN_MASK | \
-      CAPABILITY_MIDDLE_BTN_MASK)
-
-#define CYAPA_OFFSET_SOFT_RESET  REG_OFFSET_COMMAND_BASE
-
-#define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
-
-#define PWR_MODE_MASK   0xfc
-#define PWR_MODE_FULL_ACTIVE (0x3f << 2)
-#define PWR_MODE_IDLE        (0x05 << 2) /* default sleep time is 50 ms. */
-#define PWR_MODE_OFF         (0x00 << 2)
-
-#define PWR_STATUS_MASK      0x0c
-#define PWR_STATUS_ACTIVE    (0x03 << 2)
-#define PWR_STATUS_IDLE      (0x02 << 2)
-#define PWR_STATUS_OFF       (0x00 << 2)
-
-/*
- * CYAPA trackpad device states.
- * Used in register 0x00, bit1-0, DeviceStatus field.
- * Other values indicate device is in an abnormal state and must be reset.
- */
-#define CYAPA_DEV_NORMAL  0x03
-#define CYAPA_DEV_BUSY    0x01
-
-enum cyapa_state {
-CYAPA_STATE_OP,
-CYAPA_STATE_BL_IDLE,
-CYAPA_STATE_BL_ACTIVE,
-CYAPA_STATE_BL_BUSY,
-CYAPA_STATE_NO_DEVICE,
-};
-
-
-struct cyapa_touch {
-/*
- * high bits or x/y position value
- * bit 7 - 4: high 4 bits of x position value
- * bit 3 - 0: high 4 bits of y position value
- */
-u8 xy_hi;
-u8 x_lo;  /* low 8 bits of x position value. */
-u8 y_lo;  /* low 8 bits of y position value. */
-u8 pressure;
-/* id range is 1 - 15.  It is incremented with every new touch. */
-u8 id;
-} __packed;
-
-/* The touch.id is used as the MT slot id, thus max MT slot is 15 */
-#define CYAPA_MAX_MT_SLOTS  15
-
-struct cyapa_reg_data {
-/*
- * bit 0 - 1: device status
- * bit 3 - 2: power mode
- * bit 6 - 4: reserved
- * bit 7: interrupt valid bit
- */
-u8 device_status;
-/*
- * bit 7 - 4: number of fingers currently touching pad
- * bit 3: valid data check bit
- * bit 2: middle mechanism button state if exists
- * bit 1: right mechanism button state if exists
- * bit 0: left mechanism button state if exists
- */
-u8 finger_btn;
-/* CYAPA reports up to 5 touches per packet. */
-struct cyapa_touch touches[5];
-} __packed;
-
-/* The main device structure */
-struct cyapa {
-enum cyapa_state state;
-
-struct i2c_client *client;
-struct input_dev *input;
-char phys[32];/* device physical location */
-int irq;
-bool irq_wake;  /* irq wake is enabled */
-bool smbus;
-
-/* read from query data region. */
-char product_id[16];
-u8 btn_capability;
-u8 gen;
-int max_abs_x;
-int max_abs_y;
-int physical_size_x;
-int physical_size_y;
-};
-
-static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
-0x04, 0x05, 0x06, 0x07 };
-static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
-0x05, 0x06, 0x07 };
-
-struct cyapa_cmd_len {
-u8 cmd;
-u8 len;
-};
 
 #define CYAPA_ADAPTER_FUNC_NONE   0
 #define CYAPA_ADAPTER_FUNC_I2C    1
 #define CYAPA_ADAPTER_FUNC_SMBUS  2
 #define CYAPA_ADAPTER_FUNC_BOTH   3
 
-/*
- * macros for SMBus communication
- */
-#define SMBUS_READ   0x01
-#define SMBUS_WRITE 0x00
-#define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
-#define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))

drivers/input/mouse/cyapa.c

Quoted 10 lines
drivers/input/mouse/cyapa.c +0 -10
-#define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
-#define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
-
- /* for byte read/write command */
-#define CMD_RESET 0
-#define CMD_POWER_MODE 1
-#define CMD_DEV_STATUS 2
-#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
-#define CYAPA_SMBUS_RESET SMBUS_BYTE_CMD(CMD_RESET)
-#define CYAPA_SMBUS_POWER_MODE

SMBUS_BYTE_CMD(CMD_POWER_MODE)

drivers/input/mouse/cyapa.c

Quoted 8 lines
drivers/input/mouse/cyapa.c +0 -8
-#define CYAPA_SMBUS_DEV_STATUS SMBUS_BYTE_CMD(CMD_DEV_STATUS)
-
- /* for group registers read/write command */
-#define REG_GROUP_DATA 0
-#define REG_GROUP_CMD 2
-#define REG_GROUP_QUERY 3
-#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
-#define CYAPA_SMBUS_GROUP_DATA

SMBUS_GROUP_CMD(REG_GROUP_DATA)

drivers/input/mouse/cyapa.c line 263

Quoted 1 line
drivers/input/mouse/cyapa.c +0 -1
-#define CYAPA_SMBUS_GROUP_CMD

SMBUS_GROUP_CMD(REG_GROUP_CMD)

drivers/input/mouse/cyapa.c line 264

Quoted 1 line
drivers/input/mouse/cyapa.c +0 -1
-#define CYAPA_SMBUS_GROUP_QUERY

SMBUS_GROUP_CMD(REG_GROUP_QUERY)

Quoted 10 lines
drivers/input/mouse/cyapa.c +0 -10
-
- /* for register block read/write command */
-#define CMD_BL_STATUS 0
-#define CMD_BL_HEAD 1
-#define CMD_BL_CMD 2
-#define CMD_BL_DATA 3
-#define CMD_BL_ALL 4
-#define CMD_BLK_PRODUCT_ID 5
-#define CMD_BLK_HEAD 6
-#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))

drivers/input/mouse/cyapa.c

Quoted 10 lines
drivers/input/mouse/cyapa.c +0 -10
-
-/* register block read/write command in bootloader mode */
-#define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
-#define CYAPA_SMBUS_BL_HEAD SMBUS_BLOCK_CMD(CMD_BL_HEAD)
-#define CYAPA_SMBUS_BL_CMD SMBUS_BLOCK_CMD(CMD_BL_CMD)
-#define CYAPA_SMBUS_BL_DATA SMBUS_BLOCK_CMD(CMD_BL_DATA)
-#define CYAPA_SMBUS_BL_ALL SMBUS_BLOCK_CMD(CMD_BL_ALL)
-
-/* register block read/write command in operational mode */
-#define CYAPA_SMBUS_BLK_PRODUCT_ID

SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)

Quoted 15 lines
drivers/input/mouse/cyapa.c +0 -15
-#define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
-
-static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
-{ CYAPA_OFFSET_SOFT_RESET, 1 },
-{ REG_OFFSET_COMMAND_BASE + 1, 1 },
-{ REG_OFFSET_DATA_BASE, 1 },
-{ REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
-{ REG_OFFSET_COMMAND_BASE, 0 },
-{ REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE },
-{ BL_HEAD_OFFSET, 3 },
-{ BL_HEAD_OFFSET, 16 },
-{ BL_HEAD_OFFSET, 16 },
-{ BL_DATA_OFFSET, 16 },
-{ BL_HEAD_OFFSET, 32 },
-{ REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
Quoted 8 lines

drivers/input/mouse/cyapa.c line 36

Quoted 5 lines
drivers/input/mouse/cyapa.c +3 -2
-{ REG_OFFSET_DATA_BASE, 32 }
-};
+#define CYAPA_DEBUGFS_READ_FW"read_fw"
+#define CYAPA_DEBUGFS_RAW_DATA"raw_data"
+#define CYAPA_FW_NAME"cyapa.bin"

These do not seem to belong to the current patch, please introduce them only when you need them.

Thanks, I will fix it in next upstream.

Quoted 634 lines

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa

Quoted 452 lines
drivers/input/mouse/cyapa.c +131 -274
+
+const char unique_str[] = "CYTRA";
 
-static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
-{ CYAPA_SMBUS_RESET, 1 },
-{ CYAPA_SMBUS_POWER_MODE, 1 },
-{ CYAPA_SMBUS_DEV_STATUS, 1 },
-{ CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
-{ CYAPA_SMBUS_GROUP_CMD, 2 },
-{ CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
-{ CYAPA_SMBUS_BL_STATUS, 3 },
-{ CYAPA_SMBUS_BL_HEAD, 16 },
-{ CYAPA_SMBUS_BL_CMD, 16 },
-{ CYAPA_SMBUS_BL_DATA, 16 },
-{ CYAPA_SMBUS_BL_ALL, 32 },
-{ CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
-{ CYAPA_SMBUS_BLK_HEAD, 16 },
-};
 
-static ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
+ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
 u8 *values)
 {
 return i2c_smbus_read_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-static ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
+ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
  size_t len, const u8 *values)
 {
 return i2c_smbus_write_i2c_block_data(cyapa->client, reg, len, values);
 }
 
-/*
- * cyapa_smbus_read_block - perform smbus block read command
- * @cyapa  - private data structure of the driver
- * @cmd    - the properly encoded smbus command
- * @len    - expected length of smbus command result
- * @values - buffer to store smbus command result
- *
- * Returns negative errno, else the number of bytes written.
- *
- * Note:
- * In trackpad device, the memory block allocated for I2C register map
- * is 256 bytes, so the max read block for I2C bus is 256 bytes.
- */
-static ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
-      u8 *values)
-{
-ssize_t ret;
-u8 index;
-u8 smbus_cmd;
-u8 *buf;
-struct i2c_client *client = cyapa->client;
-
-if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
-return -EINVAL;
-
-if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
-/* read specific block registers command. */
-smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
-goto out;
-}
-
-ret = 0;
-for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
-smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
-smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
-buf = values + I2C_SMBUS_BLOCK_MAX * index;
-ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
-if (ret < 0)
-goto out;
-}
-
-out:
-return ret > 0 ? len : ret;
-}
-
-static s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
-{
-u8 cmd;
-
-if (cyapa->smbus) {
-cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
-} else {
-cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-}
-return i2c_smbus_read_byte_data(cyapa->client, cmd);
-}
-
-static s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
-{
-u8 cmd;
-
-if (cyapa->smbus) {
-cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
-} else {
-cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-}
-return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
-}
-
-static ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
-{
-u8 cmd;
-size_t len;
-
-if (cyapa->smbus) {
-cmd = cyapa_smbus_cmds[cmd_idx].cmd;
-len = cyapa_smbus_cmds[cmd_idx].len;
-return cyapa_smbus_read_block(cyapa, cmd, len, values);
-}
-
-cmd = cyapa_i2c_cmds[cmd_idx].cmd;
-len = cyapa_i2c_cmds[cmd_idx].len;
-return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
-}
-
-/*
- * Query device for its current operating state.
- *
- */
-static int cyapa_get_state(struct cyapa *cyapa)
+/* Returns 0 on success, else negative errno on failure. */
+ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len,
+u8 *values)
 {
 int ret;
-u8 status[BL_STATUS_SIZE];
-
-cyapa->state = CYAPA_STATE_NO_DEVICE;
-
-/*
- * Get trackpad status by reading 3 registers starting from 0.
- * If the device is in the bootloader, this will be BL_HEAD.
- * If the device is in operation mode, this will be the DATA regs.
- *
- */
-ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
-       status);
-
-/*
- * On smbus systems in OP mode, the i2c_reg_read will fail with
- * -ETIMEDOUT.  In this case, try again using the smbus equivalent
- * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
- */
-if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO))
-ret = cyapa_read_block(cyapa, CYAPA_CMD_BL_STATUS, status);
-
-if (ret != BL_STATUS_SIZE)
-goto error;
-
-if ((status[REG_OP_STATUS] & OP_STATUS_SRC) == OP_STATUS_SRC) {
-switch (status[REG_OP_STATUS] & OP_STATUS_DEV) {
-case CYAPA_DEV_NORMAL:
-case CYAPA_DEV_BUSY:
-cyapa->state = CYAPA_STATE_OP;
-break;
-default:
-ret = -EAGAIN;
-goto error;
-}
-} else {
-if (status[REG_BL_STATUS] & BL_STATUS_BUSY)
-cyapa->state = CYAPA_STATE_BL_BUSY;
-else if (status[REG_BL_ERROR] & BL_ERROR_BOOTLOADING)
-cyapa->state = CYAPA_STATE_BL_ACTIVE;
-else
-cyapa->state = CYAPA_STATE_BL_IDLE;
-}
+struct i2c_client *client = cyapa->client;
+struct i2c_msg msgs[] = {
+{
+.addr = client->addr,
+.flags = 0,
+.len = 1,
+.buf = &reg,
+},
+{
+.addr = client->addr,
+.flags = I2C_M_RD,
+.len = len,
+.buf = values,
+},
+};
+
+ret = i2c_transfer(client->adapter, msgs, 2);
+
+if (ret != ARRAY_SIZE(msgs))
+return ret < 0 ? ret : -EIO;
 
 return 0;
-error:
-return (ret < 0) ? ret : -EAGAIN;
 }
 
-/*
- * Poll device for its status in a loop, waiting up to timeout for a response.
- *
- * When the device switches state, it usually takes ~300 ms.
- * However, when running a new firmware image, the device must calibrate its
- * sensors, which can take as long as 2 seconds.
+/**
+ * cyapa_i2c_write - Execute i2c block data write operation
+ * @cyapa: Handle to this driver
+ * @ret: Offset of the data to written in the register map
+ * @len: number of bytes to write
+ * @values: Data to be written
  *
- * Note: The timeout has granularity of the polling rate, which is 100 ms.
- *
- * Returns:
- *   0 when the device eventually responds with a valid non-busy state.
- *   -ETIMEDOUT if device never responds (too many -EAGAIN)
- *   < 0    other errors
+ * Return negative errno code on error; return zero when success.
  */
-static int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+ssize_t cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
+ size_t len, const void *values)
 {
 int ret;
-int tries = timeout / 100;
-
-ret = cyapa_get_state(cyapa);
-while ((ret || cyapa->state >= CYAPA_STATE_BL_BUSY) && tries--) {
-msleep(100);
-ret = cyapa_get_state(cyapa);
-}
-return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
-}
-
-static int cyapa_bl_deactivate(struct cyapa *cyapa)
-{
-int ret;
-
-ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
-bl_deactivate);
-if (ret < 0)
-return ret;
-
-/* wait for bootloader to switch to idle state; should take < 100ms */
-msleep(100);
-ret = cyapa_poll_state(cyapa, 500);
-if (ret < 0)
-return ret;
-if (cyapa->state != CYAPA_STATE_BL_IDLE)
-return -EAGAIN;
-return 0;
+struct i2c_client *client = cyapa->client;
+char data[32], *buf;
+
+if (len > 31) {
+buf = kzalloc(len + 1, GFP_KERNEL);
+if (!buf)
+return -ENOMEM;
+} else
+buf = data;
+
+buf[0] = reg;
+memcpy(&buf[1], values, len);
+ret = i2c_master_send(client, buf, len + 1);
+
+if (buf != data)
+kfree(buf);
+return (ret == (len + 1)) ? 0 : ((ret < 0) ? ret : -EIO);
 }
 
-/*
- * Exit bootloader
- *
- * Send bl_exit command, then wait 50 - 100 ms to let device transition to
- * operational mode.  If this is the first time the device's firmware is
- * running, it can take up to 2 seconds to calibrate its sensors.  So, poll
- * the device's new state for up to 2 seconds.
- *
- * Returns:
- *   -EIO    failure while reading from device
- *   -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
- *   0       device is supported and in operational mode
- */
-static int cyapa_bl_exit(struct cyapa *cyapa)
+static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
 {
-int ret;
-
-ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
-if (ret < 0)
-return ret;
-
-/*
- * Wait for bootloader to exit, and operation mode to start.
- * Normally, this takes at least 50 ms.
- */
-usleep_range(50000, 100000);
-/*
- * In addition, when a device boots for the first time after being
- * updated to new firmware, it must first calibrate its sensors, which
- * can take up to an additional 2 seconds.
- */
-ret = cyapa_poll_state(cyapa, 2000);
-if (ret < 0)
-return ret;
-if (cyapa->state != CYAPA_STATE_OP)
-return -EAGAIN;
+u8 ret = CYAPA_ADAPTER_FUNC_NONE;
 
-return 0;
+if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+ret |= CYAPA_ADAPTER_FUNC_I2C;
+if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+     I2C_FUNC_SMBUS_BLOCK_DATA |
+     I2C_FUNC_SMBUS_I2C_BLOCK))
+ret |= CYAPA_ADAPTER_FUNC_SMBUS;
+return ret;
 }
 
-/*
- * Set device power mode
- *
- */
-static int cyapa_set_power_mode(struct cyapa *cyapa, u8 power_mode)
+static int cyapa_create_input_dev(struct cyapa *cyapa)
 {
 struct device *dev = &cyapa->client->dev;
 int ret;
-u8 power;
+struct input_dev *input;
 
-if (cyapa->state != CYAPA_STATE_OP)
-return 0;
+if (!cyapa->physical_size_x || !cyapa->physical_size_y)
+return -EINVAL;
 
-ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
-if (ret < 0)
-return ret;
+input = cyapa->input = devm_input_allocate_device(dev);
+if (!input) {
+dev_err(dev, "failed to allocate memory for input device.\n");
+return -ENOMEM;
+}
 
-power = ret & ~PWR_MODE_MASK;
-power |= power_mode & PWR_MODE_MASK;
-ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
-if (ret < 0)
-dev_err(dev, "failed to set power_mode 0x%02x err = %d\n",
-power_mode, ret);
-return ret;
-}
+input->name = CYAPA_NAME;
+input->phys = cyapa->phys;
+input->id.bustype = BUS_I2C;
+input->id.version = 1;
+input->id.product = 0;  /* Means any product in eventcomm. */
+input->dev.parent = &cyapa->client->dev;
 
-static int cyapa_get_query_data(struct cyapa *cyapa)
-{
-u8 query_data[QUERY_DATA_SIZE];
-int ret;
+input_set_drvdata(input, cyapa);
 
-if (cyapa->state != CYAPA_STATE_OP)
-return -EBUSY;
+__set_bit(EV_ABS, input->evbit);
 
-ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
-if (ret < 0)
-return ret;
-if (ret != QUERY_DATA_SIZE)
-return -EIO;
+/* Finger position */
+input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
+     0);
+input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
+     0);
+input_set_abs_params(input, ABS_MT_PRESSURE, 0, cyapa->max_z, 0, 0);
+if (cyapa->gen > CYAPA_GEN3) {
+input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
+input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
+/*
+ * Orientation is the angle between the vertical axis and
+ * the major axis of the contact ellipse.
+ * The range is -127 to 127.
+ * the positive direction is clockwise form the vertical axis.
+ * If the ellipse of contact degenerates into a circle,
+ * orientation is reported as 0.
+ *
+ * Also, for Gen5 trackpad the accurate of this orientation
+ * value is value + (-30 ~ 30).
+ */
+input_set_abs_params(input, ABS_MT_ORIENTATION,
+-127, 127, 0, 0);
+}
+if (cyapa->gen >= CYAPA_GEN5) {
+input_set_abs_params(input, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
+input_set_abs_params(input, ABS_MT_WIDTH_MINOR, 0, 255, 0, 0);
+}
 
-memcpy(&cyapa->product_id[0], &query_data[0], 5);
-cyapa->product_id[5] = '-';
-memcpy(&cyapa->product_id[6], &query_data[5], 6);
-cyapa->product_id[12] = '-';
-memcpy(&cyapa->product_id[13], &query_data[11], 2);
-cyapa->product_id[15] = '\0';
+input_abs_set_res(input, ABS_MT_POSITION_X,
+  cyapa->max_abs_x / cyapa->physical_size_x);
+input_abs_set_res(input, ABS_MT_POSITION_Y,
+  cyapa->max_abs_y / cyapa->physical_size_y);
 
-cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
+if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
+__set_bit(BTN_LEFT, input->keybit);
+if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
+__set_bit(BTN_MIDDLE, input->keybit);
+if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
+__set_bit(BTN_RIGHT, input->keybit);
 
-cyapa->gen = query_data[20] & 0x0f;
+if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
+__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
 
-cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
-cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
+/* Handle pointer emulation and unused slots in core */
+ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
+  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
+if (ret) {
+dev_err(dev, "failed to initialize MT slots, %d\n", ret);
+return ret;
+}
 
-cyapa->physical_size_x =
-((query_data[24] & 0xf0) << 4) | query_data[25];
-cyapa->physical_size_y =
-((query_data[24] & 0x0f) << 8) | query_data[26];
+/* Register the device in input subsystem */
+ret = input_register_device(input);
+if (ret) {
+dev_err(dev, "failed to register input device, %d\n", ret);
+return ret;
+}
 
 return 0;
 }
@@ -635,192 +227,213 @@ static int cyapa_get_query_data(struct cyapa

*cyapa)

Quoted 181 lines
drivers/input/mouse/cyapa.c +60 -90
  */
 static int cyapa_check_is_operational(struct cyapa *cyapa)
 {
-struct device *dev = &cyapa->client->dev;
-static const char unique_str[] = "CYTRA";
 int ret;
 
-ret = cyapa_poll_state(cyapa, 2000);
-if (ret < 0)
+ret = cyapa_poll_state(cyapa, 4000);
+if (ret)
 return ret;
-switch (cyapa->state) {
-case CYAPA_STATE_BL_ACTIVE:
-ret = cyapa_bl_deactivate(cyapa);
-if (ret)
-return ret;
-
-/* Fallthrough state */
-case CYAPA_STATE_BL_IDLE:
-ret = cyapa_bl_exit(cyapa);
-if (ret)
-return ret;
-
-/* Fallthrough state */
-case CYAPA_STATE_OP:
-ret = cyapa_get_query_data(cyapa);
-if (ret < 0)
-return ret;
-
-/* only support firmware protocol gen3 */
-if (cyapa->gen != CYAPA_GEN3) {
-dev_err(dev, "unsupported protocol version (%d)",
-cyapa->gen);
-return -EINVAL;
-}
-
-/* only support product ID starting with CYTRA */
-if (memcmp(cyapa->product_id, unique_str,
-   sizeof(unique_str) - 1) != 0) {
-dev_err(dev, "unsupported product ID (%s)\n",
-cyapa->product_id);
-return -EINVAL;
-}
-return 0;
 
+switch (cyapa->gen) {
 default:
-return -EIO;
+return -ENODEV;
 }
-return 0;
+
+if (cyapa->ops->operational_check)
+ret = cyapa->ops->operational_check(cyapa);
+
+return ret;
 }
 
+
 static irqreturn_t cyapa_irq(int irq, void *dev_id)
 {
 struct cyapa *cyapa = dev_id;
 struct device *dev = &cyapa->client->dev;
 struct input_dev *input = cyapa->input;
-struct cyapa_reg_data data;
-int i;
+bool cont;
 int ret;
-int num_fingers;
 
 if (device_may_wakeup(dev))
 pm_wakeup_event(dev, 0);
 
-ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
-if (ret != sizeof(data))
-goto out;
-
-if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
-    (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
-    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
-goto out;
-}
-
-num_fingers = (data.finger_btn >> 4) & 0x0f;
-for (i = 0; i < num_fingers; i++) {
-const struct cyapa_touch *touch = &data.touches[i];
-/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
-int slot = touch->id - 1;
-
-input_mt_slot(input, slot);
-input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
-input_report_abs(input, ABS_MT_POSITION_X,
- ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
-input_report_abs(input, ABS_MT_POSITION_Y,
- ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
-input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
+/* Interrupt event maybe cuased by host command to trackpad device. */
+cont = true;
+if (cyapa->ops->irq_cmd_handler)
+cont = cyapa->ops->irq_cmd_handler(cyapa);
+
+/* Interrupt event maybe from trackpad device input reporting. */
+if (cont && cyapa->ops->irq_handler) {
+if (!input || cyapa->ops->irq_handler(cyapa)) {
+ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+if (ret && cyapa->ops->sort_empty_output_data) {
+cyapa->ops->sort_empty_output_data(cyapa,
+NULL, NULL, NULL);
+goto out;
+}
+cyapa_detect(cyapa);
+mutex_unlock(&cyapa->state_sync_lock);
+}
 }
 
-input_mt_sync_frame(input);
-
-if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-input_report_key(input, BTN_LEFT,
- data.finger_btn & OP_DATA_LEFT_BTN);
-
-if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-input_report_key(input, BTN_MIDDLE,
- data.finger_btn & OP_DATA_MIDDLE_BTN);
-
-if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-input_report_key(input, BTN_RIGHT,
- data.finger_btn & OP_DATA_RIGHT_BTN);
-
-input_sync(input);
-
 out:
 return IRQ_HANDLED;
 }
 
-static u8 cyapa_check_adapter_functionality(struct i2c_client *client)
+/*
+ * Query device for its current operating state.
+ */
+static int cyapa_get_state(struct cyapa *cyapa)
 {
-u8 ret = CYAPA_ADAPTER_FUNC_NONE;
+return -ENODEV;
+}
 
-if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
-ret |= CYAPA_ADAPTER_FUNC_I2C;
-if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
-     I2C_FUNC_SMBUS_BLOCK_DATA |
-     I2C_FUNC_SMBUS_I2C_BLOCK))
-ret |= CYAPA_ADAPTER_FUNC_SMBUS;
-return ret;
+/*
+ * Poll device for its status in a loop, waiting up to timeout for a response.
+ *
+ * When the device switches state, it usually takes ~300 ms.
+ * However, when running a new firmware image, the device must calibrate its
+ * sensors, which can take as long as 2 seconds.
+ *
+ * Note: The timeout has granularity of the polling rate, which is 100 ms.
+ *
+ * Returns:
+ *   0 when the device eventually responds with a valid non-busy state.
+ *   -ETIMEDOUT if device never responds (too many -EAGAIN)
+ *   < 0    other errors
+ */
+int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout)
+{
+int ret;
+int tries = timeout / 100;
+
+ret = cyapa_get_state(cyapa);
+while ((ret || cyapa->state <= CYAPA_STATE_BL_BUSY) && tries--) {
+msleep(100);
+ret = cyapa_get_state(cyapa);
+}
+
+return (ret == -EAGAIN || ret == -ETIMEDOUT) ? -ETIMEDOUT : ret;
 }
 
Quoted 13 lines

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 325

Quoted 9 lines
drivers/input/mouse/cyapa.c +8 -1
-static int cyapa_create_input_dev(struct cyapa *cyapa)
+/*
+ * Returns:
+ *   0    when device is detected, and operatioinal.
+ *   > 0  when device is detected, but not operational.
+ *   < 0  when device is not detected, or other errors.
+ *          e.g.: IO communication error with the device.
+ */
+int cyapa_detect(struct cyapa *cyapa)

We have cyapa->state. Can we check it to see if device is operational or not? Or introduce another variable instead of returning tristate from cyapa_detect?

Do you mean in cyapa_detect() method, just get the result of operational or not? If not operational, then in other method to get the detail state of the device, right?

Quoted 16 lines
Quoted 16 lines
drivers/input/mouse/cyapa.c +8 -3
 {
 struct device *dev = &cyapa->client->dev;
+char *envp[2] = {"ERROR=1", NULL};
 int ret;
-struct input_dev *input;
 
-if (!cyapa->physical_size_x || !cyapa->physical_size_y)
-return -EINVAL;
+ret = cyapa_check_is_operational(cyapa);
+if (ret) {
+if (ret != -ETIMEDOUT && ret != -ENODEV &&
+((cyapa->gen == CYAPA_GEN3 &&
+cyapa->state >= CYAPA_STATE_BL_BUSY &&
+cyapa->state <= CYAPA_STATE_BL_ACTIVE) ||
+(cyapa->gen == CYAPA_GEN5 &&
Quoted 8 lines

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 343

Quoted 6 lines
drivers/input/mouse/cyapa.c +6 -0
+cyapa->state == CYAPA_STATE_GEN5_BL))) {
+dev_warn(dev, "device detected, but no operatinal.\n");
+dev_warn(dev, "device gen=%d, state=0x%02x\n",
+cyapa->gen, cyapa->state);
+dev_warn(dev, "munually fw image recover required.\n");
+return 1;

Hmm, what userspace uses this kind of notification?

My idea is, when device waiting in bootloader for firmware image error, then
the driver should keep actived, so user/system can have the chance to update the firmware to recover it. And you are correct, usersapce may don't know the return status of 1.
We could add an sysfs interface, such state, so usersapce can read the state (0: operational, 1 or non-0: bootloader).

Quoted 95 lines
Quoted 82 lines
drivers/input/mouse/cyapa.c +51 -22
+}
 
-input = cyapa->input = devm_input_allocate_device(dev);
-if (!input) {
-dev_err(dev, "allocate memory for input device failed\n");
-return -ENOMEM;
+dev_err(dev, "no device detected, (%d)\n", ret);
+kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
+return ret;
 }
 
-input->name = CYAPA_NAME;
-input->phys = cyapa->phys;
-input->id.bustype = BUS_I2C;
-input->id.version = 1;
-input->id.product = 0;  /* means any product in eventcomm. */
-input->dev.parent = &cyapa->client->dev;
+if (!cyapa->input) {
+ret = cyapa_create_input_dev(cyapa);
+if (ret) {
+dev_err(dev, "create input_dev instance failed, (%d)\n",
+ret);
+return ret;
+}
 
-input_set_drvdata(input, cyapa);
+/*
+ * On some systems, a system crash / warm boot does not reset
+ * the device's current power mode to FULL_ACTIVE.
+ * If such an event happens during suspend, after the device
+ * has been put in a low power mode, the device will still be
+ * in low power mode on a subsequent boot, since there was
+ * never a matching resume().
+ * Handle this by always forcing full power here, when a
+ * device is first detected to be in operational mode.
+ */
+if (cyapa->ops->set_power_mode) {
+ret = cyapa->ops->set_power_mode(cyapa,
+PWR_MODE_FULL_ACTIVE, 0);
+if (ret)
+dev_warn(dev, "set active power failed, (%d)\n",
+ret);
+}
+}
 
-__set_bit(EV_ABS, input->evbit);
+return 0;
+}
 
-/* finger position */
-input_set_abs_params(input, ABS_MT_POSITION_X, 0, cyapa->max_abs_x, 0,
-     0);
-input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cyapa->max_abs_y, 0,
-     0);
-input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
+/*
+ * Sysfs Interface.
+ */
 
-input_abs_set_res(input, ABS_MT_POSITION_X,
-  cyapa->max_abs_x / cyapa->physical_size_x);
-input_abs_set_res(input, ABS_MT_POSITION_Y,
-  cyapa->max_abs_y / cyapa->physical_size_y);
+/*
+ * cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
+ *
+ * These are helper functions that convert to and from integer idle
+ * times and register settings to write to the PowerMode register.
+ * The trackpad supports between 20ms to 1000ms scan intervals.
+ * The time will be increased in increments of 10ms from 20ms to 100ms.
+ * From 100ms to 1000ms, time will be increased in increments of 20ms.
+ *
+ * When Idle_Time < 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 10;
+ * When Idle_Time >= 100, the format to convert Idle_Time to Idle_Command is:
+ *   Idle_Command = Idle Time / 20 + 5;
+ */
+u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time)
+{
+sleep_time = clamp_val(sleep_time, 20, 1000);
 

drivers/input/mouse/cyapa.c static int cyapa_get_query_data(struct cyapa *cyapa) · line 406

Quoted 10 lines
drivers/input/mouse/cyapa.c +4 -6
-if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
-__set_bit(BTN_LEFT, input->keybit);
-if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
-__set_bit(BTN_MIDDLE, input->keybit);
-if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
-__set_bit(BTN_RIGHT, input->keybit);
+if (sleep_time < 100)
+return ((sleep_time / 10) << 2) & PWR_MODE_MASK;
+return ((sleep_time / 20 + 5) << 2) & PWR_MODE_MASK;
+}

Maybe:

Quoted 3 lines

encoded_time = sleep_time < 100 ?
sleep_time / 10 : sleep_time / 20 + 5;
return (encode_time << 2) & PWR_MODE_MASK;

Thanks, applied.

Quoted 156 lines
Quoted 138 lines
drivers/input/mouse/cyapa.c +45 -45
-if (cyapa->btn_capability == CAPABILITY_LEFT_BTN_MASK)
-__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
+u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
+{
+u8 encoded_time = pwr_mode >> 2;
 
-/* handle pointer emulation and unused slots in core */
-ret = input_mt_init_slots(input, CYAPA_MAX_MT_SLOTS,
-  INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
-if (ret) {
-dev_err(dev, "allocate memory for MT slots failed, %d\n", ret);
-goto err_free_device;
-}
+return (encoded_time < 10) ? encoded_time * 10
+   : (encoded_time - 5) * 20;
+}
 
-/* Register the device in input subsystem */
-ret = input_register_device(input);
-if (ret) {
-dev_err(dev, "input device register failed, %d\n", ret);
-goto err_free_device;
-}
-return 0;
+/*
+ * Returns:
+ *   0    Driver and device initialization successfully done.
+ *   > 0  Driver initialized and device is detected, but not operational yet.
+ *   < 0  Device is not detected, or driver initialization failed.
+ */
+static int cyapa_initialize(struct cyapa *cyapa)
+{
+cyapa->state = CYAPA_STATE_NO_DEVICE;
+cyapa->gen = CYAPA_GEN_UNKNOWN;
+mutex_init(&cyapa->state_sync_lock);
 
-err_free_device:
-input_free_device(input);
-cyapa->input = NULL;
-return ret;
+/*
+ * Set to hard code default, they will be updated with trackpad set
+ * default values after probe and initialized.
+ */
+cyapa->suspend_power_mode = PWR_MODE_SLEEP;
+cyapa->suspend_sleep_time =
+cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
+
+return cyapa_detect(cyapa);
 }
 
 static int cyapa_probe(struct i2c_client *client,
@@ -830,6 +443,7 @@ static int cyapa_probe(struct i2c_client *client,
 u8 adapter_func;
 struct cyapa *cyapa;
 struct device *dev = &client->dev;
+union i2c_smbus_data dummy;
 
 adapter_func = cyapa_check_adapter_functionality(client);
 if (adapter_func == CYAPA_ADAPTER_FUNC_NONE) {
@@ -837,48 +451,39 @@ static int cyapa_probe(struct i2c_client *client,
 return -EIO;
 }
 
+/* Make sure there is something at this address */
+if (i2c_smbus_xfer(client->adapter, client->addr, 0,
+I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0)
+return -ENODEV;
+
 cyapa = devm_kzalloc(dev, sizeof(struct cyapa), GFP_KERNEL);
 if (!cyapa)
 return -ENOMEM;
 
-cyapa->gen = CYAPA_GEN3;
-cyapa->client = client;
-i2c_set_clientdata(client, cyapa);
-sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
-client->addr);
-
 /* i2c isn't supported, use smbus */
 if (adapter_func == CYAPA_ADAPTER_FUNC_SMBUS)
 cyapa->smbus = true;
-cyapa->state = CYAPA_STATE_NO_DEVICE;
-ret = cyapa_check_is_operational(cyapa);
-if (ret) {
-dev_err(dev, "device not operational, %d\n", ret);
-return ret;
-}
 
-ret = cyapa_create_input_dev(cyapa);
-if (ret) {
-dev_err(dev, "create input_dev instance failed, %d\n", ret);
-return ret;
-}
+cyapa->client = client;
+i2c_set_clientdata(client, cyapa);
+sprintf(cyapa->phys, "i2c-%d-%04x/input0", client->adapter->nr,
+client->addr);
 
-ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
-if (ret) {
-dev_err(dev, "set active power failed, %d\n", ret);
+ret = cyapa_initialize(cyapa);
+if (ret < 0) {
+dev_err(dev, "failed to detect and initialize tp device.\n");
 return ret;
 }
 
-cyapa->irq = client->irq;
 ret = devm_request_threaded_irq(dev,
-cyapa->irq,
+client->irq,
 NULL,
 cyapa_irq,
 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 "cyapa",
 cyapa);
 if (ret) {
-dev_err(dev, "IRQ request failed: %d\n, ", ret);
+dev_err(dev, "failed to request threaded irq, (%d)\n, ", ret);
 return ret;
 }
 
@@ -889,8 +494,10 @@ static int cyapa_remove(struct i2c_client *client)
 {
 struct cyapa *cyapa = i2c_get_clientdata(client);
 
-disable_irq(cyapa->irq);
-cyapa_set_power_mode(cyapa, PWR_MODE_OFF);
+disable_irq(cyapa->client->irq);
+
+if (cyapa->ops->set_power_mode)
+cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
 
 return 0;
 }

drivers/input/mouse/cyapa.c static int cyapa_suspend(struct device *dev) · line 516

Quoted 9 lines
drivers/input/mouse/cyapa.c +5 -1
@@ -902,20 +509,38 @@ static int cyapa_suspend(struct device *dev)
 u8 power_mode;
 struct cyapa *cyapa = dev_get_drvdata(dev);
 
-disable_irq(cyapa->irq);
+ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+if (ret) {
+dev_err(dev, "suspend interrupted by signal, (%d)\n", ret);
+return ret;
+}

For patterns:

var = action();
if (var) {
.. error handling ...
return var;
}

Quoted 5 lines

return 0;

i.e. when value of 'var' ony used in error handling path I strongly prefer to call 'var' error. If you do return value of 'var' in both error and success paths then 'ret' or 'retval' are preferred names.

Thanks for the suggestion, I will update it to keep them consistence.

Quoted 19 lines
Quoted 19 lines
drivers/input/mouse/cyapa.c +9 -3
+
+/*
+ * Disable IRQ to avoid the command response interrupt cause system
+ * suspending process interrupted and failed.
+ * Because the gen5 devices will always assert interrupt to host after
+ * executed the set power mode command.
+ */
+disable_irq(cyapa->client->irq);
 
 /*
  * Set trackpad device to idle mode if wakeup is allowed,
  * otherwise turn off.
  */
-power_mode = device_may_wakeup(dev) ? PWR_MODE_IDLE
+power_mode = device_may_wakeup(dev) ? cyapa->suspend_power_mode
     : PWR_MODE_OFF;
-ret = cyapa_set_power_mode(cyapa, power_mode);
-if (ret < 0)
Quoted 8 lines

drivers/input/mouse/cyapa.c static int cyapa_suspend(struct device *dev) · line 532

Quoted 2 lines
drivers/input/mouse/cyapa.c +1 -1
-dev_err(dev, "set power mode failed, %d\n", ret);
+if (cyapa->input && cyapa->ops->set_power_mode) {

The gating on cyapa->input is not obvious. Should we check the device status (or introduce one to indicate that the device in fully initialized state). Also, instead of checking presence of various methods I'd rather have providers supply stubs if they do not implement something.

I think it can be solved as your suggestion before for cyapa_detect() only return operational or not. and add other variable for detail of the device state.

Quoted 318 lines
Quoted 122 lines
drivers/input/mouse/cyapa.c +26 -8
+ret = cyapa->ops->set_power_mode(cyapa, power_mode,
+cyapa->suspend_sleep_time);
+if (ret < 0)
+dev_err(dev, "suspend set power mode failed, %d\n",
+ret);
+}
 
 if (device_may_wakeup(dev))
-cyapa->irq_wake = (enable_irq_wake(cyapa->irq) == 0);
+cyapa->irq_wake = (enable_irq_wake(cyapa->client->irq) == 0);
+
+mutex_unlock(&cyapa->state_sync_lock);
 return 0;
 }
 
@@ -924,19 +549,29 @@ static int cyapa_resume(struct device *dev)
 int ret;
 struct cyapa *cyapa = dev_get_drvdata(dev);
 
-if (device_may_wakeup(dev) && cyapa->irq_wake)
-disable_irq_wake(cyapa->irq);
-
-ret = cyapa_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE);
+ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
 if (ret)
-dev_warn(dev, "resume active power failed, %d\n", ret);
+dev_err(dev, "resume interrupted by signal, (%d)\n", ret);
 
-enable_irq(cyapa->irq);
+if (cyapa->irq_wake) {
+disable_irq_wake(cyapa->client->irq);
+cyapa->irq_wake = false;
+}
+
+/* Reset to active power state after re-detected. */
+cyapa_detect(cyapa);
+
+enable_irq(cyapa->client->irq);
+
+if (!ret)
+mutex_unlock(&cyapa->state_sync_lock);
 return 0;
 }
 #endif /* CONFIG_PM_SLEEP */
 
-static SIMPLE_DEV_PM_OPS(cyapa_pm_ops, cyapa_suspend, cyapa_resume);
+static const struct dev_pm_ops cyapa_pm_ops = {
+SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
+};
 
 static const struct i2c_device_id cyapa_id_table[] = {
 { "cyapa", 0 },
drivers/input/mouse/cyapa.h new file +63 -0
new file mode 100644
index 0000000..ee97d7c
@@ -0,0 +1,321 @@
+/*
+ * Cypress APA trackpad with I2C interface
+ *
+ * Author: Dudley Du <[email protected]>
+ *
+ * Copyright (C) 2014 Cypress Semiconductor, Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#ifndef _CYAPA_H
+#define _CYAPA_H
+
+#include <linux/async.h>
+#include <linux/firmware.h>
+#include <linux/regulator/consumer.h>
+#include <linux/regulator/driver.h>
+
+/* APA trackpad firmware generation number. */
+#define CYAPA_GEN_UNKNOWN   0x00   /* unknown protocol. */
+#define CYAPA_GEN3   0x03   /* support MT-protocol B with tracking ID. */
+#define CYAPA_GEN5   0x05   /* support TrueTouch GEN5 trackpad device. */
+
+#define CYAPA_NAME   "Cypress APA Trackpad (cyapa)"
+
+/*
+ * Macros for SMBus communication
+ */
+#define SMBUS_READ   0x01
+#define SMBUS_WRITE 0x00
+#define SMBUS_ENCODE_IDX(cmd, idx) ((cmd) | (((idx) & 0x03) << 1))
+#define SMBUS_ENCODE_RW(cmd, rw) ((cmd) | ((rw) & 0x01))
+#define SMBUS_BYTE_BLOCK_CMD_MASK 0x80
+#define SMBUS_GROUP_BLOCK_CMD_MASK 0x40
+
+/* Commands for read/write registers of Cypress trackpad */
+#define CYAPA_CMD_SOFT_RESET       0x00
+#define CYAPA_CMD_POWER_MODE       0x01
+#define CYAPA_CMD_DEV_STATUS       0x02
+#define CYAPA_CMD_GROUP_DATA       0x03
+#define CYAPA_CMD_GROUP_CMD        0x04
+#define CYAPA_CMD_GROUP_QUERY      0x05
+#define CYAPA_CMD_BL_STATUS        0x06
+#define CYAPA_CMD_BL_HEAD          0x07
+#define CYAPA_CMD_BL_CMD           0x08
+#define CYAPA_CMD_BL_DATA          0x09
+#define CYAPA_CMD_BL_ALL           0x0a
+#define CYAPA_CMD_BLK_PRODUCT_ID   0x0b
+#define CYAPA_CMD_BLK_HEAD         0x0c
+#define CYAPA_CMD_MAX_BASELINE     0x0d
+#define CYAPA_CMD_MIN_BASELINE     0x0e
+
+#define BL_HEAD_OFFSET 0x00
+#define BL_DATA_OFFSET 0x10
+
+#define BL_STATUS_SIZE  3  /* Length of gen3 bootloader status registers */
+#define CYAPA_REG_MAP_SIZE  256
+
+/*
+ * Gen3 Operational Device Status Register
+ *

drivers/input/mouse/cyapa.h line 73

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+ * bit 7: Valid interrupt source
+ * bit 6 - 4: Reserved
+ * bit 3 - 2: Power status
+ * bit 1 - 0: Device status
+ */
+#define REG_OP_STATUS     0x00
+#define OP_STATUS_SRC     0x80
+#define OP_STATUS_POWER   0x0c
+#define OP_STATUS_DEV     0x03
+#define OP_STATUS_MASK (OP_STATUS_SRC | OP_STATUS_POWER |

OP_STATUS_DEV)

Quoted 6 lines
drivers/input/mouse/cyapa.h new file +6 -0
+
+/*
+ * Operational Finger Count/Button Flags Register
+ *
+ * bit 7 - 4: Number of touched finger
+ * bit 3: Valid data

drivers/input/mouse/cyapa.h

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+ * bit 2: Middle Physical Button
+ * bit 1: Right Physical Button
+ * bit 0: Left physical Button
+ */
+#define REG_OP_DATA1       0x01
+#define OP_DATA_VALID      0x08
+#define OP_DATA_MIDDLE_BTN 0x04
+#define OP_DATA_RIGHT_BTN  0x02
+#define OP_DATA_LEFT_BTN   0x01
+#define OP_DATA_BTN_MASK (OP_DATA_MIDDLE_BTN | OP_DATA_RIGHT_BTN

| \

Quoted 18 lines
drivers/input/mouse/cyapa.h new file +18 -0
+  OP_DATA_LEFT_BTN)
+
+/*
+ * Write-only command file register used to issue commands and
+ * parameters to the bootloader.
+ * The default value read from it is always 0x00.
+ */
+#define REG_BL_FILE0x00
+#define BL_FILE0x00
+
+/*
+ * Bootloader Status Register
+ *
+ * bit 7: Busy
+ * bit 6 - 5: Reserved
+ * bit 4: Bootloader running
+ * bit 3 - 2: Reserved
+ * bit 1: Watchdog Reset

drivers/input/mouse/cyapa.h

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+ * bit 0: Checksum valid
+ */
+#define REG_BL_STATUS        0x01
+#define BL_STATUS_REV_6_5    0x60
+#define BL_STATUS_BUSY       0x80
+#define BL_STATUS_RUNNING    0x10
+#define BL_STATUS_REV_3_2    0x0c
+#define BL_STATUS_WATCHDOG   0x02
+#define BL_STATUS_CSUM_VALID 0x01
+#define BL_STATUS_REV_MASK (BL_STATUS_WATCHDOG | BL_STATUS_REV_3_2

| \

Quoted 25 lines
drivers/input/mouse/cyapa.h new file +25 -0
+    BL_STATUS_REV_6_5)
+
+/*
+ * Bootloader Error Register
+ *
+ * bit 7: Invalid
+ * bit 6: Invalid security key
+ * bit 5: Bootloading
+ * bit 4: Command checksum
+ * bit 3: Flash protection error
+ * bit 2: Flash checksum error
+ * bit 1 - 0: Reserved
+ */
+#define REG_BL_ERROR         0x02
+#define BL_ERROR_INVALID     0x80
+#define BL_ERROR_INVALID_KEY 0x40
+#define BL_ERROR_BOOTLOADING 0x20
+#define BL_ERROR_CMD_CSUM    0x10
+#define BL_ERROR_FLASH_PROT  0x08
+#define BL_ERROR_FLASH_CSUM  0x04
+#define BL_ERROR_RESERVED    0x03
+#define BL_ERROR_NO_ERR_IDLE    0x00
+#define BL_ERROR_NO_ERR_ACTIVE  (BL_ERROR_BOOTLOADING)
+
+#define CAPABILITY_BTN_SHIFT            3

drivers/input/mouse/cyapa.h

Quoted 10 lines
drivers/input/mouse/cyapa.h new file +10 -0
+#define CAPABILITY_LEFT_BTN_MASK(0x01 << 3)
+#define CAPABILITY_RIGHT_BTN_MASK(0x01 << 4)
+#define CAPABILITY_MIDDLE_BTN_MASK(0x01 << 5)
+#define CAPABILITY_BTN_MASK  (CAPABILITY_LEFT_BTN_MASK | \
+      CAPABILITY_RIGHT_BTN_MASK | \
+      CAPABILITY_MIDDLE_BTN_MASK)
+
+#define PWR_MODE_MASK   0xfc
+#define PWR_MODE_FULL_ACTIVE (0x3f << 2)
+#define PWR_MODE_IDLE        (0x03 << 2) /* Default rt suspend scanrate:

30ms */

drivers/input/mouse/cyapa.h line 153

Quoted 1 line
drivers/input/mouse/cyapa.h new file +1 -0
+#define PWR_MODE_SLEEP       (0x05 << 2) /* Default suspend scanrate:

50ms */

Quoted 101 lines
drivers/input/mouse/cyapa.h new file +101 -0
+#define PWR_MODE_BTN_ONLY    (0x01 << 2)
+#define PWR_MODE_OFF         (0x00 << 2)
+
+#define PWR_STATUS_MASK      0x0c
+#define PWR_STATUS_ACTIVE    (0x03 << 2)
+#define PWR_STATUS_IDLE      (0x02 << 2)
+#define PWR_STATUS_BTN_ONLY  (0x01 << 2)
+#define PWR_STATUS_OFF       (0x00 << 2)
+
+#define AUTOSUSPEND_DELAY   2000 /* unit : ms */
+
+#define UNINIT_SLEEP_TIME 0xFFFF
+#define UNINIT_PWR_MODE   0xFF
+
+#define BTN_ONLY_MODE_NAME   "buttononly"
+#define OFF_MODE_NAME        "off"
+
+/* The touch.id is used as the MT slot id, thus max MT slot is 15 */
+#define CYAPA_MAX_MT_SLOTS  15
+
+struct cyapa;
+
+typedef bool (*cb_sort)(struct cyapa *, u8 *, int);
+
+struct cyapa_dev_ops {
+int (*check_fw)(struct cyapa *, const struct firmware *);
+int (*bl_enter)(struct cyapa *);
+int (*bl_activate)(struct cyapa *);
+int (*bl_initiate)(struct cyapa *, const struct firmware *);
+int (*update_fw)(struct cyapa *, const struct firmware *);
+int (*bl_verify_app_integrity)(struct cyapa *);
+int (*bl_deactivate)(struct cyapa *);
+
+ssize_t (*show_baseline)(struct device *,
+struct device_attribute *, char *);
+ssize_t (*calibrate_store)(struct device *,
+struct device_attribute *, const char *, size_t);
+
+int (*initialize)(struct cyapa *cyapa);
+
+int (*state_parse)(struct cyapa *cyapa, u8 *reg_status, int len);
+int (*operational_check)(struct cyapa *cyapa);
+
+int (*irq_handler)(struct cyapa *);
+bool (*irq_cmd_handler)(struct cyapa *);
+int (*sort_empty_output_data)(struct cyapa *,
+u8 *, int *, cb_sort);
+
+int (*set_power_mode)(struct cyapa *, u8, u16);
+};
+
+struct cyapa_gen5_cmd_states {
+struct mutex cmd_lock;
+struct completion cmd_ready;
+atomic_t cmd_issued;
+u8 in_progress_cmd;
+bool is_irq_mode;
+
+cb_sort resp_sort_func;
+u8 *resp_data;
+int *resp_len;
+
+u8 irq_cmd_buf[CYAPA_REG_MAP_SIZE];
+u8 empty_buf[CYAPA_REG_MAP_SIZE];
+};
+
+union cyapa_cmd_states {
+struct cyapa_gen5_cmd_states gen5;
+};
+
+enum cyapa_state {
+CYAPA_STATE_NO_DEVICE,
+CYAPA_STATE_BL_BUSY,
+CYAPA_STATE_BL_IDLE,
+CYAPA_STATE_BL_ACTIVE,
+CYAPA_STATE_OP,
+CYAPA_STATE_GEN5_BL,
+CYAPA_STATE_GEN5_APP,
+};
+
+struct cyapa_tsg_bin_image_head {
+u8 head_size;  /* Unit: bytes, including itself. */
+u8 ttda_driver_major_version;  /* Reserved as 0. */
+u8 ttda_driver_minor_version;  /* Reserved as 0. */
+u8 fw_major_version;
+u8 fw_minor_version;
+u8 fw_revision_control_number[8];
+} __packed;
+
+/* The main device structure */
+struct cyapa {
+enum cyapa_state state;
+u8 status[BL_STATUS_SIZE];
+
+struct i2c_client *client;
+struct input_dev *input;
+char phys[32];/* Device physical location */
+bool irq_wake;  /* Irq wake is enabled */
+bool smbus;
+
+/* power mode settings */
Quoted 8 lines

drivers/input/mouse/cyapa.h line 260

Quoted 6 lines
drivers/input/mouse/cyapa.h new file +6 -0
+u8 suspend_power_mode;
+u16 suspend_sleep_time;
+#ifdef CONFIG_PM_RUNTIME
+u8 runtime_suspend_power_mode;
+u16 runtime_suspend_sleep_time;
+#endif /* CONFIG_PM_RUNTIME */

This I think should be introduce later, with runtime PM changes.

Thanks.

Quoted 40 lines
Quoted 40 lines
drivers/input/mouse/cyapa.h new file +39 -0
+u8 dev_pwr_mode;
+u16 dev_sleep_time;
+
+/* Read from query data region. */
+char product_id[16];
+u8 fw_maj_ver;  /* Firmware major version. */
+u8 fw_min_ver;  /* Firmware minor version. */
+u8 btn_capability;
+u8 gen;
+int max_abs_x;
+int max_abs_y;
+int physical_size_x;
+int physical_size_y;
+
+/* Used in ttsp and truetouch based trackpad devices. */
+u8 x_origin;  /* X Axis Origin: 0 = left side; 1 = rigth side. */
+u8 y_origin;  /* Y Axis Origin: 0 = top; 1 = bottom. */
+int electrodes_x;  /* Number of electrodes on the X Axis*/
+int electrodes_y;  /* Number of electrodes on the Y Axis*/
+int electrodes_rx;  /* Number of Rx electrodes */
+int max_z;
+
+/*
+ * Used to synchronize the access or update the device state.
+ * And since update firmware and read firmware image process will take
+ * quite long time, maybe more than 10 seconds, so use mutex_lock
+ * to sync and wait other interface and detecting are done or ready.
+ */
+struct mutex state_sync_lock;
+
+const struct cyapa_dev_ops *ops;
+
+union cyapa_cmd_states cmd_states;
+};
+
+
+ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
+u8 *values);
+ssize_t cyapa_i2c_reg_write_block(struct cyapa *cyapa, u8 reg,
Quoted 8 lines

drivers/input/mouse/cyapa.h line 305

Quoted 6 lines
drivers/input/mouse/cyapa.h new file +6 -0
+size_t len, const u8 *values);
+ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
+u8 *values);
+
+s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx);
+s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value);

Do we still use these 2 fucntions?

You are correct, Thanks.
It has been moved to and only used in cyapa_gen3.c.

Quoted 23 lines
Quoted 18 lines
drivers/input/mouse/cyapa.h new file +16 -1
+ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values);
+
+ssize_t cyapa_i2c_read(struct cyapa *cyapa, u8 reg, size_t len, u8 *values);
+ssize_t cyapa_i2c_write(struct cyapa *cyapa, u8 reg,
+size_t len, const void *values);
+
+int cyapa_poll_state(struct cyapa *cyapa, unsigned int timeout);
+int cyapa_detect(struct cyapa *cyapa);
+
+u8 cyapa_sleep_time_to_pwr_cmd(u16 sleep_time);
+u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
+
+
+extern const char unique_str[];
+
+#endif
--
Quoted 2 lines

1.9.1

Thanks.

Quoted 2 lines

--
Dmitry

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

author
#8 lore raw

PATCH v9 3/18 input: cyapa: add gen3 trackpad device basic functions support

In reply to Dudley Du

Based on the cyapa core, add the gen3 trackpad device's basic functions supported, so gen3 trackpad device can work with kernel input system. The basic function is absolutely same as previous cyapa driver. TEST=test on Chromebooks.

---
 drivers/input/mouse/Makefile     |   3 +-
 drivers/input/mouse/cyapa.c      |  90 ++++-
 drivers/input/mouse/cyapa.h      |   1 +
 drivers/input/mouse/cyapa_gen3.c | 788 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 880 insertions(+), 2 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa_gen3.c
drivers/input/mouse/Makefile +2 -1
index dda507f..4bf6c83 100644
@@ -8,7 +8,7 @@ obj-$(CONFIG_MOUSE_AMIGA)		+= amimouse.o
 obj-$(CONFIG_MOUSE_APPLETOUCH)		+= appletouch.o
 obj-$(CONFIG_MOUSE_ATARI)		+= atarimouse.o
 obj-$(CONFIG_MOUSE_BCM5974)		+= bcm5974.o
-obj-$(CONFIG_MOUSE_CYAPA)		+= cyapa.o
+obj-$(CONFIG_MOUSE_CYAPA)		+= cyapatp.o
 obj-$(CONFIG_MOUSE_GPIO)		+= gpio_mouse.o
 obj-$(CONFIG_MOUSE_INPORT)		+= inport.o
 obj-$(CONFIG_MOUSE_LOGIBM)		+= logibm.o
@@ -23,6 +23,7 @@ obj-$(CONFIG_MOUSE_SYNAPTICS_I2C)	+= synaptics_i2c.o
 obj-$(CONFIG_MOUSE_SYNAPTICS_USB)	+= synaptics_usb.o
 obj-$(CONFIG_MOUSE_VSXXXAA)		+= vsxxxaa.o
 
+cyapatp-objs := cyapa.o cyapa_gen3.o
 psmouse-objs := psmouse-base.o synaptics.o focaltech.o
 
 psmouse-$(CONFIG_MOUSE_PS2_ALPS)	+= alps.o
drivers/input/mouse/cyapa.c +89 -1

114 lines not on this page · raw message

drivers/input/mouse/cyapa.h +1 -0
index ee97d7c..b281dcb 100644
@@ -317,5 +317,6 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
 
 
 extern const char unique_str[];
+extern const struct cyapa_dev_ops cyapa_gen3_ops;
 
 #endif
drivers/input/mouse/cyapa_gen3.c new file +788 -0

788 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#9 lore raw

Re: [PATCH v9 03/18] input: cyapa: add gen3 trackpad device basic functions support

Quoted 100 lines · Dudley Du

Based on the cyapa core, add the gen3 trackpad device's basic functions supported, so gen3 trackpad device can work with kernel input system. The basic function is absolutely same as previous cyapa driver. TEST=test on Chromebooks.

---
 drivers/input/mouse/Makefile     |   3 +-
 drivers/input/mouse/cyapa.c      |  90 ++++-
 drivers/input/mouse/cyapa.h      |   1 +
 drivers/input/mouse/cyapa_gen3.c | 788 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 880 insertions(+), 2 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa_gen3.c
drivers/input/mouse/Makefile +2 -1
index dda507f..4bf6c83 100644
@@ -8,7 +8,7 @@ obj-$(CONFIG_MOUSE_AMIGA)		+= amimouse.o
 obj-$(CONFIG_MOUSE_APPLETOUCH)		+= appletouch.o
 obj-$(CONFIG_MOUSE_ATARI)		+= atarimouse.o
 obj-$(CONFIG_MOUSE_BCM5974)		+= bcm5974.o
-obj-$(CONFIG_MOUSE_CYAPA)		+= cyapa.o
+obj-$(CONFIG_MOUSE_CYAPA)		+= cyapatp.o
 obj-$(CONFIG_MOUSE_GPIO)		+= gpio_mouse.o
 obj-$(CONFIG_MOUSE_INPORT)		+= inport.o
 obj-$(CONFIG_MOUSE_LOGIBM)		+= logibm.o
@@ -23,6 +23,7 @@ obj-$(CONFIG_MOUSE_SYNAPTICS_I2C)	+= synaptics_i2c.o
 obj-$(CONFIG_MOUSE_SYNAPTICS_USB)	+= synaptics_usb.o
 obj-$(CONFIG_MOUSE_VSXXXAA)		+= vsxxxaa.o
 
+cyapatp-objs := cyapa.o cyapa_gen3.o
 psmouse-objs := psmouse-base.o synaptics.o focaltech.o
 
 psmouse-$(CONFIG_MOUSE_PS2_ALPS)	+= alps.o
drivers/input/mouse/cyapa.c +49 -1
index 5029618..cb81baf 100644
@@ -234,6 +234,9 @@ static int cyapa_check_is_operational(struct cyapa *cyapa)
 		return ret;
 
 	switch (cyapa->gen) {
+	case CYAPA_GEN3:
+		cyapa->ops = &cyapa_gen3_ops;
+		break;
 	default:
 		return -ENODEV;
 	}
@@ -284,7 +287,85 @@ out:
  */
 static int cyapa_get_state(struct cyapa *cyapa)
 {
-	return -ENODEV;
+	int ret;
+	u8 status[BL_STATUS_SIZE];
+	u8 cmd[32];
+	/* The i2c address of gen4 and gen5 trackpad device must be even. */
+	bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
+	bool smbus = false;
+	int retries = 2;
+
+	cyapa->state = CYAPA_STATE_NO_DEVICE;
+
+	/*
+	 * Get trackpad status by reading 3 registers starting from 0.
+	 * If the device is in the bootloader, this will be BL_HEAD.
+	 * If the device is in operation mode, this will be the DATA regs.
+	 *
+	 */
+	ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
+				       status);
+
+	/*
+	 * On smbus systems in OP mode, the i2c_reg_read will fail with
+	 * -ETIMEDOUT.  In this case, try again using the smbus equivalent
+	 * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
+	 */
+	if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO)) {
+		if (!even_addr)
+			ret = cyapa_read_block(cyapa,
+					CYAPA_CMD_BL_STATUS, status);
+		smbus = true;
+	}
+	if (ret != BL_STATUS_SIZE)
+		goto error;
+
+	/*
+	 * Detect trackpad protocol based on characristic registers and bits.
+	 */
+	do {
+		cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
+		cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
+		cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
+
+		if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
+				cyapa->gen == CYAPA_GEN3) {
+			ret = cyapa_gen3_ops.state_parse(cyapa,
+					status, BL_STATUS_SIZE);
+			if (ret == 0)

drivers/input/mouse/cyapa.c out: · line 345

Quoted 10 lines
drivers/input/mouse/cyapa.c +10 -0
+				goto out_detected;
+		}
+
+		/*
+		 * Cannot detect communication protocol based on current
+		 * charateristic registers and bits.
+		 * So write error command to do further detection.
+		 * this method only valid on I2C bus.
+		 * for smbus interface, it won't have overwrite issue.
+		 */

I do not quite understand this, can you re-phrase?

Quoted 306 lines
drivers/input/mouse/cyapa.c +30 -0
+		if (!smbus) {
+			cmd[0] = 0x00;
+			cmd[1] = 0x00;
+			ret = cyapa_i2c_write(cyapa, 0, 2, cmd);
+			if (ret)
+				goto error;
+
+			msleep(50);
+
+			ret = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
+					BL_STATUS_SIZE, status);
+			if (ret < 0)
+				goto error;
+		}
+	} while (--retries > 0 && !smbus);
+
+	goto error;
+
+out_detected:
+	return 0;
+
+error:
+	return (ret < 0) ? ret : -EAGAIN;
 }
 
 /*
@@ -421,6 +502,8 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
  */
 static int cyapa_initialize(struct cyapa *cyapa)
 {
+	int ret = 0;
+
 	cyapa->state = CYAPA_STATE_NO_DEVICE;
 	cyapa->gen = CYAPA_GEN_UNKNOWN;
 	mutex_init(&cyapa->state_sync_lock);
@@ -433,6 +516,11 @@ static int cyapa_initialize(struct cyapa *cyapa)
 	cyapa->suspend_sleep_time =
 		cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
 
+	if (cyapa_gen3_ops.initialize)
+		ret = cyapa_gen3_ops.initialize(cyapa);
+	if (ret)
+		return ret;
+
 	return cyapa_detect(cyapa);
 }
 
drivers/input/mouse/cyapa.h +1 -0
index ee97d7c..b281dcb 100644
@@ -317,5 +317,6 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
 
 
 extern const char unique_str[];
+extern const struct cyapa_dev_ops cyapa_gen3_ops;
 
 #endif
drivers/input/mouse/cyapa_gen3.c new file +242 -0
new file mode 100644
index 0000000..bd00c6e
@@ -0,0 +1,788 @@
+/*
+ * Cypress APA trackpad with I2C interface
+ *
+ * Author: Dudley Du <[email protected]>
+ * Further cleanup and restructuring by:
+ *   Daniel Kurtz <[email protected]>
+ *   Benson Leung <[email protected]>
+ *
+ * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
+ * Copyright (C) 2011-2012 Google, Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include "cyapa.h"
+
+
+#define GEN3_MAX_FINGERS 5
+#define GEN3_FINGER_NUM(x) (((x) >> 4) & 0x07)
+
+#define BLK_HEAD_BYTES 32
+
+/* Macro for register map group offset. */
+#define PRODUCT_ID_SIZE  16
+#define QUERY_DATA_SIZE  27
+#define REG_PROTOCOL_GEN_QUERY_OFFSET  20
+
+#define REG_OFFSET_DATA_BASE     0x0000
+#define REG_OFFSET_COMMAND_BASE  0x0028
+#define REG_OFFSET_QUERY_BASE    0x002a
+
+#define CYAPA_OFFSET_SOFT_RESET  REG_OFFSET_COMMAND_BASE
+#define OP_RECALIBRATION_MASK    0x80
+#define OP_REPORT_BASELINE_MASK  0x40
+#define REG_OFFSET_MAX_BASELINE  0x0026
+#define REG_OFFSET_MIN_BASELINE  0x0027
+
+#define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
+#define SET_POWER_MODE_DELAY   10000  /* unit: us */
+#define SET_POWER_MODE_TRIES   5
+
+/*
+ * CYAPA trackpad device states.
+ * Used in register 0x00, bit1-0, DeviceStatus field.
+ * Other values indicate device is in an abnormal state and must be reset.
+ */
+#define CYAPA_DEV_NORMAL  0x03
+#define CYAPA_DEV_BUSY    0x01
+
+#define CYAPA_FW_BLOCK_SIZE	64
+#define CYAPA_FW_READ_SIZE	16
+#define CYAPA_FW_HDR_START	0x0780
+#define CYAPA_FW_HDR_BLOCK_COUNT  2
+#define CYAPA_FW_HDR_BLOCK_START  (CYAPA_FW_HDR_START / CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_HDR_SIZE	  (CYAPA_FW_HDR_BLOCK_COUNT * \
+					CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_DATA_START	0x0800
+#define CYAPA_FW_DATA_BLOCK_COUNT  480
+#define CYAPA_FW_DATA_BLOCK_START  (CYAPA_FW_DATA_START / CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_DATA_SIZE	(CYAPA_FW_DATA_BLOCK_COUNT * \
+				 CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_SIZE		(CYAPA_FW_HDR_SIZE + CYAPA_FW_DATA_SIZE)
+#define CYAPA_CMD_LEN		16
+
+#define GEN3_BL_IDLE_FW_MAJ_VER_OFFSET 0x0b
+#define GEN3_BL_IDLE_FW_MIN_VER_OFFSET (GEN3_BL_IDLE_FW_MAJ_VER_OFFSET + 1)
+
+
+struct cyapa_touch {
+	/*
+	 * high bits or x/y position value
+	 * bit 7 - 4: high 4 bits of x position value
+	 * bit 3 - 0: high 4 bits of y position value
+	 */
+	u8 xy_hi;
+	u8 x_lo;  /* low 8 bits of x position value. */
+	u8 y_lo;  /* low 8 bits of y position value. */
+	u8 pressure;
+	/* id range is 1 - 15.  It is incremented with every new touch. */
+	u8 id;
+} __packed;
+
+struct cyapa_reg_data {
+	/*
+	 * bit 0 - 1: device status
+	 * bit 3 - 2: power mode
+	 * bit 6 - 4: reserved
+	 * bit 7: interrupt valid bit
+	 */
+	u8 device_status;
+	/*
+	 * bit 7 - 4: number of fingers currently touching pad
+	 * bit 3: valid data check bit
+	 * bit 2: middle mechanism button state if exists
+	 * bit 1: right mechanism button state if exists
+	 * bit 0: left mechanism button state if exists
+	 */
+	u8 finger_btn;
+	/* CYAPA reports up to 5 touches per packet. */
+	struct cyapa_touch touches[5];
+} __packed;
+
+static const u8 bl_activate[] = { 0x00, 0xff, 0x38, 0x00, 0x01, 0x02, 0x03,
+		0x04, 0x05, 0x06, 0x07 };
+static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
+		0x04, 0x05, 0x06, 0x07 };
+static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
+		0x05, 0x06, 0x07 };
+
+
+ /* for byte read/write command */
+#define CMD_RESET      0
+#define CMD_POWER_MODE 1
+#define CMD_DEV_STATUS 2
+#define CMD_REPORT_MAX_BASELINE 3
+#define CMD_REPORT_MIN_BASELINE 4
+#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
+#define CYAPA_SMBUS_RESET         SMBUS_BYTE_CMD(CMD_RESET)
+#define CYAPA_SMBUS_POWER_MODE    SMBUS_BYTE_CMD(CMD_POWER_MODE)
+#define CYAPA_SMBUS_DEV_STATUS    SMBUS_BYTE_CMD(CMD_DEV_STATUS)
+#define CYAPA_SMBUS_MAX_BASELINE  SMBUS_BYTE_CMD(CMD_REPORT_MAX_BASELINE)
+#define CYAPA_SMBUS_MIN_BASELINE  SMBUS_BYTE_CMD(CMD_REPORT_MIN_BASELINE)
+
+ /* for group registers read/write command */
+#define REG_GROUP_DATA 0
+#define REG_GROUP_CMD 2
+#define REG_GROUP_QUERY 3
+#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
+#define CYAPA_SMBUS_GROUP_DATA	 SMBUS_GROUP_CMD(REG_GROUP_DATA)
+#define CYAPA_SMBUS_GROUP_CMD	 SMBUS_GROUP_CMD(REG_GROUP_CMD)
+#define CYAPA_SMBUS_GROUP_QUERY	 SMBUS_GROUP_CMD(REG_GROUP_QUERY)
+
+ /* for register block read/write command */
+#define CMD_BL_STATUS 0
+#define CMD_BL_HEAD 1
+#define CMD_BL_CMD 2
+#define CMD_BL_DATA 3
+#define CMD_BL_ALL 4
+#define CMD_BLK_PRODUCT_ID 5
+#define CMD_BLK_HEAD 6
+#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
+
+/* register block read/write command in bootloader mode */
+#define CYAPA_SMBUS_BL_STATUS  SMBUS_BLOCK_CMD(CMD_BL_STATUS)
+#define CYAPA_SMBUS_BL_HEAD    SMBUS_BLOCK_CMD(CMD_BL_HEAD)
+#define CYAPA_SMBUS_BL_CMD     SMBUS_BLOCK_CMD(CMD_BL_CMD)
+#define CYAPA_SMBUS_BL_DATA    SMBUS_BLOCK_CMD(CMD_BL_DATA)
+#define CYAPA_SMBUS_BL_ALL     SMBUS_BLOCK_CMD(CMD_BL_ALL)
+
+/* register block read/write command in operational mode */
+#define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
+#define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
+
+ /* for byte read/write command */
+#define CMD_RESET 0
+#define CMD_POWER_MODE 1
+#define CMD_DEV_STATUS 2
+#define CMD_REPORT_MAX_BASELINE 3
+#define CMD_REPORT_MIN_BASELINE 4
+#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
+#define CYAPA_SMBUS_RESET         SMBUS_BYTE_CMD(CMD_RESET)
+#define CYAPA_SMBUS_POWER_MODE    SMBUS_BYTE_CMD(CMD_POWER_MODE)
+#define CYAPA_SMBUS_DEV_STATUS    SMBUS_BYTE_CMD(CMD_DEV_STATUS)
+#define CYAPA_SMBUS_MAX_BASELINE  SMBUS_BYTE_CMD(CMD_REPORT_MAX_BASELINE)
+#define CYAPA_SMBUS_MIN_BASELINE  SMBUS_BYTE_CMD(CMD_REPORT_MIN_BASELINE)
+
+ /* for group registers read/write command */
+#define REG_GROUP_DATA  0
+#define REG_GROUP_CMD   2
+#define REG_GROUP_QUERY 3
+#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
+#define CYAPA_SMBUS_GROUP_DATA  SMBUS_GROUP_CMD(REG_GROUP_DATA)
+#define CYAPA_SMBUS_GROUP_CMD   SMBUS_GROUP_CMD(REG_GROUP_CMD)
+#define CYAPA_SMBUS_GROUP_QUERY SMBUS_GROUP_CMD(REG_GROUP_QUERY)
+
+ /* for register block read/write command */
+#define CMD_BL_STATUS		0
+#define CMD_BL_HEAD		1
+#define CMD_BL_CMD		2
+#define CMD_BL_DATA		3
+#define CMD_BL_ALL		4
+#define CMD_BLK_PRODUCT_ID	5
+#define CMD_BLK_HEAD		6
+#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))
+
+/* register block read/write command in bootloader mode */
+#define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
+#define CYAPA_SMBUS_BL_HEAD   SMBUS_BLOCK_CMD(CMD_BL_HEAD)
+#define CYAPA_SMBUS_BL_CMD    SMBUS_BLOCK_CMD(CMD_BL_CMD)
+#define CYAPA_SMBUS_BL_DATA   SMBUS_BLOCK_CMD(CMD_BL_DATA)
+#define CYAPA_SMBUS_BL_ALL    SMBUS_BLOCK_CMD(CMD_BL_ALL)
+
+/* register block read/write command in operational mode */
+#define CYAPA_SMBUS_BLK_PRODUCT_ID SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)
+#define CYAPA_SMBUS_BLK_HEAD       SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
+
+struct cyapa_cmd_len {
+	u8 cmd;
+	u8 len;
+};
+
+/* maps generic CYAPA_CMD_* code to the I2C equivalent */
+static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
+	{ CYAPA_OFFSET_SOFT_RESET, 1 },		/* CYAPA_CMD_SOFT_RESET */
+	{ REG_OFFSET_COMMAND_BASE + 1, 1 },	/* CYAPA_CMD_POWER_MODE */
+	{ REG_OFFSET_DATA_BASE, 1 },		/* CYAPA_CMD_DEV_STATUS */
+	{ REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
+						/* CYAPA_CMD_GROUP_DATA */
+	{ REG_OFFSET_COMMAND_BASE, 0 },		/* CYAPA_CMD_GROUP_CMD */
+	{ REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE }, /* CYAPA_CMD_GROUP_QUERY */
+	{ BL_HEAD_OFFSET, 3 },			/* CYAPA_CMD_BL_STATUS */
+	{ BL_HEAD_OFFSET, 16 },			/* CYAPA_CMD_BL_HEAD */
+	{ BL_HEAD_OFFSET, 16 },			/* CYAPA_CMD_BL_CMD */
+	{ BL_DATA_OFFSET, 16 },			/* CYAPA_CMD_BL_DATA */
+	{ BL_HEAD_OFFSET, 32 },			/* CYAPA_CMD_BL_ALL */
+	{ REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
+						/* CYAPA_CMD_BLK_PRODUCT_ID */
+	{ REG_OFFSET_DATA_BASE, 32 },		/* CYAPA_CMD_BLK_HEAD */
+	{ REG_OFFSET_MAX_BASELINE, 1 },		/* CYAPA_CMD_MAX_BASELINE */
+	{ REG_OFFSET_MIN_BASELINE, 1 },		/* CYAPA_CMD_MIN_BASELINE */
+};
+
+static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
+	{ CYAPA_SMBUS_RESET, 1 },		/* CYAPA_CMD_SOFT_RESET */
+	{ CYAPA_SMBUS_POWER_MODE, 1 },		/* CYAPA_CMD_POWER_MODE */
+	{ CYAPA_SMBUS_DEV_STATUS, 1 },		/* CYAPA_CMD_DEV_STATUS */
+	{ CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
+						/* CYAPA_CMD_GROUP_DATA */
+	{ CYAPA_SMBUS_GROUP_CMD, 2 },		/* CYAPA_CMD_GROUP_CMD */
+	{ CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
+						/* CYAPA_CMD_GROUP_QUERY */
+	{ CYAPA_SMBUS_BL_STATUS, 3 },		/* CYAPA_CMD_BL_STATUS */
+	{ CYAPA_SMBUS_BL_HEAD, 16 },		/* CYAPA_CMD_BL_HEAD */
+	{ CYAPA_SMBUS_BL_CMD, 16 },		/* CYAPA_CMD_BL_CMD */

drivers/input/mouse/cyapa_gen3.c line 252

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+	{ CYAPA_SMBUS_BL_DATA, 16 },		/* CYAPA_CMD_BL_DATA */
+	{ CYAPA_SMBUS_BL_ALL, 32 },		/* CYAPA_CMD_BL_ALL */
+	{ CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
+						/* CYAPA_CMD_BLK_PRODUCT_ID */
+	{ CYAPA_SMBUS_BLK_HEAD, 16 },		/* CYAPA_CMD_BLK_HEAD */
+	{ CYAPA_SMBUS_MAX_BASELINE, 1 },	/* CYAPA_CMD_MAX_BASELINE */
+	{ CYAPA_SMBUS_MIN_BASELINE, 1 },	/* CYAPA_CMD_MIN_BASELINE */
+};
+
+static bool data_reporting_started;

Why is this a global?

Quoted 539 lines
drivers/input/mouse/cyapa_gen3.c new file +536 -0
+
+
+/*
+ * cyapa_smbus_read_block - perform smbus block read command
+ * @cyapa  - private data structure of the driver
+ * @cmd    - the properly encoded smbus command
+ * @len    - expected length of smbus command result
+ * @values - buffer to store smbus command result
+ *
+ * Returns negative errno, else the number of bytes written.
+ *
+ * Note:
+ * In trackpad device, the memory block allocated for I2C register map
+ * is 256 bytes, so the max read block for I2C bus is 256 bytes.
+ */
+ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
+				      u8 *values)
+{
+	ssize_t ret;
+	u8 index;
+	u8 smbus_cmd;
+	u8 *buf;
+	struct i2c_client *client = cyapa->client;
+
+	if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
+		return -EINVAL;
+
+	if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
+		/* read specific block registers command. */
+		smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
+		ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
+		goto out;
+	}
+
+	ret = 0;
+	for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
+		smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
+		smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
+		buf = values + I2C_SMBUS_BLOCK_MAX * index;
+		ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
+		if (ret < 0)
+			goto out;
+	}
+
+out:
+	return ret > 0 ? len : ret;
+}
+
+s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
+{
+	u8 cmd;
+
+	if (cyapa->smbus) {
+		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
+	} else {
+		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+	}
+	return i2c_smbus_read_byte_data(cyapa->client, cmd);
+}
+
+s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
+{
+	u8 cmd;
+
+	if (cyapa->smbus) {
+		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+		cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
+	} else {
+		cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+	}
+	return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
+}
+
+ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
+{
+	u8 cmd;
+	size_t len;
+
+	if (cyapa->smbus) {
+		cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+		len = cyapa_smbus_cmds[cmd_idx].len;
+		return cyapa_smbus_read_block(cyapa, cmd, len, values);
+	}
+	cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+	len = cyapa_i2c_cmds[cmd_idx].len;
+	return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
+}
+
+/*
+ * Determine the Gen3 trackpad device's current operating state.
+ *
+ */
+static int cyapa_gen3_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
+{
+	/*
+	 * Must be in detecting and should not do data reporting.
+	 * It will be reenabled when all detecting done and lauched into
+	 * applicaiton mode successfully.
+	 */
+	data_reporting_started = false;
+	cyapa->state = CYAPA_STATE_NO_DEVICE;
+
+	/* Parse based on Gen3 characteristic registers and bits */
+	if (reg_data[REG_BL_FILE] == BL_FILE &&
+		reg_data[REG_BL_ERROR] == BL_ERROR_NO_ERR_IDLE &&
+		(reg_data[REG_BL_STATUS] ==
+			(BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID) ||
+			reg_data[REG_BL_STATUS] == BL_STATUS_RUNNING)) {
+		/*
+		 * Normal state after power on or reset,
+		 * REG_BL_STATUS == 0x11, firmware image checksum is valid.
+		 * REG_BL_STATUS == 0x10, firmware image checksum is invalid.
+		 */
+		cyapa->gen = CYAPA_GEN3;
+		cyapa->state = CYAPA_STATE_BL_IDLE;
+	} else if (reg_data[REG_BL_FILE] == BL_FILE &&
+		(reg_data[REG_BL_STATUS] & BL_STATUS_RUNNING) ==
+			BL_STATUS_RUNNING) {
+		cyapa->gen = CYAPA_GEN3;
+		if (reg_data[REG_BL_STATUS] & BL_STATUS_BUSY) {
+			cyapa->state = CYAPA_STATE_BL_BUSY;
+		} else {
+			if ((reg_data[REG_BL_ERROR] & BL_ERROR_BOOTLOADING) ==
+					BL_ERROR_BOOTLOADING)
+				cyapa->state = CYAPA_STATE_BL_ACTIVE;
+			else
+				cyapa->state = CYAPA_STATE_BL_IDLE;
+		}
+	} else if ((reg_data[REG_OP_STATUS] & OP_STATUS_SRC) &&
+			(reg_data[REG_OP_DATA1] & OP_DATA_VALID)) {
+		/*
+		 * Normal state when running in operaitonal mode,
+		 * may also not in full power state or
+		 * busying in command process.
+		 */
+		if (GEN3_FINGER_NUM(reg_data[REG_OP_DATA1]) <=
+				GEN3_MAX_FINGERS) {
+			/* Finger number data is valid. */
+			cyapa->gen = CYAPA_GEN3;
+			cyapa->state = CYAPA_STATE_OP;
+		}
+	} else if (reg_data[REG_OP_STATUS] == 0x0C &&
+			reg_data[REG_OP_DATA1] == 0x08) {
+		/* Op state when first two registers overwritten with 0x00 */
+		cyapa->gen = CYAPA_GEN3;
+		cyapa->state = CYAPA_STATE_OP;
+	} else if (reg_data[REG_BL_STATUS] &
+			(BL_STATUS_RUNNING | BL_STATUS_BUSY)) {
+		cyapa->gen = CYAPA_GEN3;
+		cyapa->state = CYAPA_STATE_BL_BUSY;
+	}
+
+	if (cyapa->gen == CYAPA_GEN3 && (cyapa->state == CYAPA_STATE_OP ||
+		cyapa->state == CYAPA_STATE_BL_IDLE ||
+		cyapa->state == CYAPA_STATE_BL_ACTIVE ||
+		cyapa->state == CYAPA_STATE_BL_BUSY))
+		return 0;
+
+	return -EAGAIN;
+}
+
+static int cyapa_gen3_bl_deactivate(struct cyapa *cyapa)
+{
+	int ret;
+
+	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
+					bl_deactivate);
+	if (ret < 0)
+		return ret;
+
+	/* Wait for bootloader to switch to idle state; should take < 100ms */
+	msleep(100);
+	ret = cyapa_poll_state(cyapa, 500);
+	if (ret < 0)
+		return ret;
+	if (cyapa->state != CYAPA_STATE_BL_IDLE)
+		return -EAGAIN;
+	return 0;
+}
+
+/*
+ * Exit bootloader
+ *
+ * Send bl_exit command, then wait 50 - 100 ms to let device transition to
+ * operational mode.  If this is the first time the device's firmware is
+ * running, it can take up to 2 seconds to calibrate its sensors.  So, poll
+ * the device's new state for up to 2 seconds.
+ *
+ * Returns:
+ *   -EIO    failure while reading from device
+ *   -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
+ *   0       device is supported and in operational mode
+ */
+static int cyapa_gen3_bl_exit(struct cyapa *cyapa)
+{
+	int ret;
+
+	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Wait for bootloader to exit, and operation mode to start.
+	 * Normally, this takes at least 50 ms.
+	 */
+	usleep_range(50000, 100000);
+	/*
+	 * In addition, when a device boots for the first time after being
+	 * updated to new firmware, it must first calibrate its sensors, which
+	 * can take up to an additional 2 seconds. If the device power is
+	 * running low, this may take even longer.
+	 */
+	ret = cyapa_poll_state(cyapa, 4000);
+	if (ret < 0)
+		return ret;
+	if (cyapa->state != CYAPA_STATE_OP)
+		return -EAGAIN;
+
+	return 0;
+}
+
+/*
+ * cyapa_get_wait_time_for_pwr_cmd
+ *
+ * Compute the amount of time we need to wait after updating the touchpad
+ * power mode. The touchpad needs to consume the incoming power mode set
+ * command at the current clock rate.
+ */
+
+static u16 cyapa_get_wait_time_for_pwr_cmd(u8 pwr_mode)
+{
+	switch (pwr_mode) {
+	case PWR_MODE_FULL_ACTIVE: return 20;
+	case PWR_MODE_BTN_ONLY: return 20;
+	case PWR_MODE_OFF: return 20;
+	default: return cyapa_pwr_cmd_to_sleep_time(pwr_mode) + 50;
+	}
+}
+
+/*
+ * Set device power mode
+ *
+ * Write to the field to configure power state. Power states include :
+ *   Full : Max scans and report rate.
+ *   Idle : Report rate set by user specified time.
+ *   ButtonOnly : No scans for fingers. When the button is triggered,
+ *     a slave interrupt is asserted to notify host to wake up.
+ *   Off : Only awake for i2c commands from host. No function for button
+ *     or touch sensors.
+ *
+ * The power_mode command should conform to the following :
+ *   Full : 0x3f
+ *   Idle : Configurable from 20 to 1000ms. See note below for
+ *     cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
+ *   ButtonOnly : 0x01
+ *   Off : 0x00
+ *
+ * Device power mode can only be set when device is in operational mode.
+ */
+static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
+		u16 always_unused)
+{
+	int ret;
+	u8 power;
+	int tries = SET_POWER_MODE_TRIES;
+	u16 sleep_time;
+
+	always_unused = 0;
+	if (cyapa->state != CYAPA_STATE_OP)
+		return 0;
+
+	while (true) {
+		ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
+		if (ret >= 0 || --tries < 1)
+			break;
+		usleep_range(SET_POWER_MODE_DELAY, 2 * SET_POWER_MODE_DELAY);
+	}
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Return early if the power mode to set is the same as the current
+	 * one.
+	 */
+	if ((ret & PWR_MODE_MASK) == power_mode)
+		return 0;
+
+	sleep_time = cyapa_get_wait_time_for_pwr_cmd(ret & PWR_MODE_MASK);
+	power = ret;
+	power &= ~PWR_MODE_MASK;
+	power |= power_mode & PWR_MODE_MASK;
+	while (true) {
+		ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
+		if (!ret || --tries < 1)
+			break;
+		usleep_range(SET_POWER_MODE_DELAY, 2 * SET_POWER_MODE_DELAY);
+	}
+
+	/*
+	 * Wait for the newly set power command to go in at the previous
+	 * clock speed (scanrate) used by the touchpad firmware. Not
+	 * doing so before issuing the next command may result in errors
+	 * depending on the command's content.
+	 */
+	msleep(sleep_time);
+	return ret;
+}
+
+static int cyapa_gen3_get_query_data(struct cyapa *cyapa)
+{
+	u8 query_data[QUERY_DATA_SIZE];
+	int ret;
+
+	if (cyapa->state != CYAPA_STATE_OP)
+		return -EBUSY;
+
+	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
+	if (ret != QUERY_DATA_SIZE)
+		return (ret < 0) ? ret : -EIO;
+
+	memcpy(&cyapa->product_id[0], &query_data[0], 5);
+	cyapa->product_id[5] = '-';
+	memcpy(&cyapa->product_id[6], &query_data[5], 6);
+	cyapa->product_id[12] = '-';
+	memcpy(&cyapa->product_id[13], &query_data[11], 2);
+	cyapa->product_id[15] = '\0';
+
+	cyapa->fw_maj_ver = query_data[15];
+	cyapa->fw_min_ver = query_data[16];
+
+	cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
+
+	cyapa->gen = query_data[20] & 0x0f;
+
+	cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
+	cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
+
+	cyapa->physical_size_x =
+		((query_data[24] & 0xf0) << 4) | query_data[25];
+	cyapa->physical_size_y =
+		((query_data[24] & 0x0f) << 8) | query_data[26];
+
+	cyapa->max_z = 255;
+
+	return 0;
+}
+
+static int cyapa_gen3_bl_query_data(struct cyapa *cyapa)
+{
+	u8 bl_data[CYAPA_CMD_LEN];
+	int ret;
+
+	ret = cyapa_i2c_reg_read_block(cyapa, 0, CYAPA_CMD_LEN, bl_data);
+	if (ret != CYAPA_CMD_LEN)
+		return (ret < 0) ? ret : -EIO;
+
+	/*
+	 * This value will be updated again when entered application mode.
+	 * If TP failed to enter application mode, this fw version values
+	 * can be used as a reference.
+	 * This firmware version valid when fw image checksum is valid.
+	 */
+	if (bl_data[REG_BL_STATUS] ==
+			(BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID)) {
+		cyapa->fw_maj_ver = bl_data[GEN3_BL_IDLE_FW_MAJ_VER_OFFSET];
+		cyapa->fw_min_ver = bl_data[GEN3_BL_IDLE_FW_MIN_VER_OFFSET];
+	}
+
+	return 0;
+}
+
+/*
+ * Check if device is operational.
+ *
+ * An operational device is responding, has exited bootloader, and has
+ * firmware supported by this driver.
+ *
+ * Returns:
+ *   -EBUSY  no device or in bootloader
+ *   -EIO    failure while reading from device
+ *   -EAGAIN device is still in bootloader
+ *           if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
+ *   -EINVAL device is in operational mode, but not supported by this driver
+ *   0       device is supported
+ */
+static int cyapa_gen3_do_operational_check(struct cyapa *cyapa)
+{
+	struct device *dev = &cyapa->client->dev;
+	int ret;
+
+	switch (cyapa->state) {
+	case CYAPA_STATE_BL_ACTIVE:
+		ret = cyapa_gen3_bl_deactivate(cyapa);
+		if (ret) {
+			dev_err(dev, "failed to bl_deactivate. %d\n", ret);
+			return ret;
+		}
+
+	/* Fallthrough state */
+	case CYAPA_STATE_BL_IDLE:
+		/* Try to get firmware version in bootloader mode. */
+		cyapa_gen3_bl_query_data(cyapa);
+
+		ret = cyapa_gen3_bl_exit(cyapa);
+		if (ret) {
+			dev_err(dev, "failed to bl_exit. %d\n", ret);
+			return ret;
+		}
+
+	/* Fallthrough state */
+	case CYAPA_STATE_OP:
+		/*
+		 * Reading query data before going back to the full mode
+		 * may cause problems, so we set the power mode first here.
+		 */
+		ret = cyapa_gen3_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE, 0);
+		if (ret)
+			dev_err(dev, "%s: set full power mode failed, (%d)\n",
+				__func__, ret);
+		ret = cyapa_gen3_get_query_data(cyapa);
+		if (ret < 0)
+			return ret;
+
+		/* Only support firmware protocol gen3 */
+		if (cyapa->gen != CYAPA_GEN3) {
+			dev_err(dev, "unsupported protocol version (%d)",
+				cyapa->gen);
+			return -EINVAL;
+		}
+
+		/* Only support product ID starting with CYTRA */
+		if (memcmp(cyapa->product_id, unique_str,
+				strlen(unique_str)) != 0) {
+			dev_err(dev, "unsupported product ID (%s)\n",
+				cyapa->product_id);
+			return -EINVAL;
+		}
+
+		data_reporting_started = true;
+		return 0;
+
+	default:
+		return -EIO;
+	}
+	return 0;
+}
+
+/*
+ * Return false, do not continue process
+ * Return true, continue process.
+ */
+static bool cyapa_gen3_irq_cmd_handler(struct cyapa *cyapa)
+{
+	/* Not gen3 irq command response, skip for continue. */
+	if (cyapa->gen != CYAPA_GEN3)
+		return true;
+
+	if (cyapa->input && data_reporting_started)
+		return true;
+
+	/*
+	 * Driver in detecting or other interface function processing,
+	 * so, stop cyapa_gen3_irq_handler to continue process to
+	 * avoid unwanted to error detecting and processing.
+	 *
+	 * And also, avoid the periodicly accerted interrupts to be processed
+	 * as touch inputs when gen3 failed to launch into application mode,
+	 * which will cause gen3 stays in bootloader mode.
+	 */
+	return false;
+}
+
+static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
+{
+	struct input_dev *input = cyapa->input;
+	struct device *dev = &cyapa->client->dev;
+	struct cyapa_reg_data data;
+	int i;
+	int ret;
+	int num_fingers;
+
+	ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
+	if (ret != sizeof(data)) {
+		dev_err(dev, "failed to read report data, (%d)\n", ret);
+		return -EINVAL;
+	}
+
+	if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
+	    (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
+	    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
+		dev_err(dev, "invalid device state bytes, %02x %02x\n",
+			data.device_status, data.finger_btn);
+		return -EINVAL;
+	}
+
+	num_fingers = (data.finger_btn >> 4) & 0x0f;
+	for (i = 0; i < num_fingers; i++) {
+		const struct cyapa_touch *touch = &data.touches[i];
+		/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
+		int slot = touch->id - 1;
+
+		input_mt_slot(input, slot);
+		input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
+		input_report_abs(input, ABS_MT_POSITION_X,
+				 ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
+		input_report_abs(input, ABS_MT_POSITION_Y,
+				 ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
+		input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
+	}
+
+	input_mt_sync_frame(input);
+
+	if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
+		input_report_key(input, BTN_LEFT,
+				 !!(data.finger_btn & OP_DATA_LEFT_BTN));
+	if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
+		input_report_key(input, BTN_MIDDLE,
+				 !!(data.finger_btn & OP_DATA_MIDDLE_BTN));
+	if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
+		input_report_key(input, BTN_RIGHT,
+				 !!(data.finger_btn & OP_DATA_RIGHT_BTN));
+	input_sync(input);
+
+	return 0;
+}
+
+const struct cyapa_dev_ops cyapa_gen3_ops = {
+	.state_parse = cyapa_gen3_state_parse,
+	.operational_check = cyapa_gen3_do_operational_check,
+
+	.irq_handler = cyapa_gen3_irq_handler,
+	.irq_cmd_handler = cyapa_gen3_irq_cmd_handler,
+
+	.set_power_mode = cyapa_gen3_set_power_mode,
+};
Signature · 1 line
1.9.1
Signature · 1 line
Dmitry
#10 lore raw

RE: [PATCH v9 03/18] input: cyapa: add gen3 trackpad device basic functions support

Thanks, Dmitry

Quoted 116 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:30
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 03/18] input: cyapa: add gen3 trackpad device basic functions support

Quoted 11 lines · Dudley Du

Based on the cyapa core, add the gen3 trackpad device's basic functions supported, so gen3 trackpad device can work with kernel input system. The basic function is absolutely same as previous cyapa driver. TEST=test on Chromebooks.

---
 drivers/input/mouse/Makefile     |   3 +-
 drivers/input/mouse/cyapa.c      |  90 ++++-
 drivers/input/mouse/cyapa.h      |   1 +
 drivers/input/mouse/cyapa_gen3.c | 788

+++++++++++++++++++++++++++++++++++++++

drivers/input/mouse/cyapa.c static int cyapa_check_is_operational(struct cyapa

Quoted 29 lines
 4 files changed, 880 insertions(+), 2 deletions(-)
 create mode 100644 drivers/input/mouse/cyapa_gen3.c
drivers/input/mouse/Makefile +2 -1
index dda507f..4bf6c83 100644
@@ -8,7 +8,7 @@ obj-$(CONFIG_MOUSE_AMIGA)+= amimouse.o
 obj-$(CONFIG_MOUSE_APPLETOUCH)+= appletouch.o
 obj-$(CONFIG_MOUSE_ATARI)+= atarimouse.o
 obj-$(CONFIG_MOUSE_BCM5974)+= bcm5974.o
-obj-$(CONFIG_MOUSE_CYAPA)+= cyapa.o
+obj-$(CONFIG_MOUSE_CYAPA)+= cyapatp.o
 obj-$(CONFIG_MOUSE_GPIO)+= gpio_mouse.o
 obj-$(CONFIG_MOUSE_INPORT)+= inport.o
 obj-$(CONFIG_MOUSE_LOGIBM)+= logibm.o
@@ -23,6 +23,7 @@ obj-$(CONFIG_MOUSE_SYNAPTICS_I2C)+= synaptics_i2c.o
 obj-$(CONFIG_MOUSE_SYNAPTICS_USB)+= synaptics_usb.o
 obj-$(CONFIG_MOUSE_VSXXXAA)+= vsxxxaa.o
 
+cyapatp-objs := cyapa.o cyapa_gen3.o
 psmouse-objs := psmouse-base.o synaptics.o focaltech.o
 
 psmouse-$(CONFIG_MOUSE_PS2_ALPS)+= alps.o
drivers/input/mouse/cyapa.c +0 -0
index 5029618..cb81baf 100644
@@ -234,6 +234,9 @@ static int cyapa_check_is_operational(struct cyapa

*cyapa)

Quoted 64 lines
drivers/input/mouse/cyapa.c +53 -1
 return ret;
 
 switch (cyapa->gen) {
+case CYAPA_GEN3:
+cyapa->ops = &cyapa_gen3_ops;
+break;
 default:
 return -ENODEV;
 }
@@ -284,7 +287,85 @@ out:
  */
 static int cyapa_get_state(struct cyapa *cyapa)
 {
-return -ENODEV;
+int ret;
+u8 status[BL_STATUS_SIZE];
+u8 cmd[32];
+/* The i2c address of gen4 and gen5 trackpad device must be even. */
+bool even_addr = ((cyapa->client->addr & 0x0001) == 0);
+bool smbus = false;
+int retries = 2;
+
+cyapa->state = CYAPA_STATE_NO_DEVICE;
+
+/*
+ * Get trackpad status by reading 3 registers starting from 0.
+ * If the device is in the bootloader, this will be BL_HEAD.
+ * If the device is in operation mode, this will be the DATA regs.
+ *
+ */
+ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET, BL_STATUS_SIZE,
+       status);
+
+/*
+ * On smbus systems in OP mode, the i2c_reg_read will fail with
+ * -ETIMEDOUT.  In this case, try again using the smbus equivalent
+ * command.  This should return a BL_HEAD indicating CYAPA_STATE_OP.
+ */
+if (cyapa->smbus && (ret == -ETIMEDOUT || ret == -ENXIO)) {
+if (!even_addr)
+ret = cyapa_read_block(cyapa,
+CYAPA_CMD_BL_STATUS, status);
+smbus = true;
+}
+if (ret != BL_STATUS_SIZE)
+goto error;
+
+/*
+ * Detect trackpad protocol based on characristic registers and bits.
+ */
+do {
+cyapa->status[REG_OP_STATUS] = status[REG_OP_STATUS];
+cyapa->status[REG_BL_STATUS] = status[REG_BL_STATUS];
+cyapa->status[REG_BL_ERROR] = status[REG_BL_ERROR];
+
+if (cyapa->gen == CYAPA_GEN_UNKNOWN ||
+cyapa->gen == CYAPA_GEN3) {
+ret = cyapa_gen3_ops.state_parse(cyapa,
+status, BL_STATUS_SIZE);
+if (ret == 0)
+goto out_detected;
+}
+
+/*
Quoted 8 lines

drivers/input/mouse/cyapa.c out: · line 345

Quoted 6 lines
drivers/input/mouse/cyapa.c +6 -0
+ * Cannot detect communication protocol based on current
+ * charateristic registers and bits.
+ * So write error command to do further detection.
+ * this method only valid on I2C bus.
+ * for smbus interface, it won't have overwrite issue.
+ */

I do not quite understand this, can you re-phrase?

Sorry for the confusion, I means write the command to deivce to reset its status, so we can try again to detect it correctly.
I would like change the comments to:
/*
* Write 0x00 0x00 to attached trackpad device to force it update its status registers,
* so later, can do the detection again.
*/

Quoted 337 lines

Review

Quoted 60 lines
drivers/input/mouse/cyapa.c +30 -0
+if (!smbus) {
+cmd[0] = 0x00;
+cmd[1] = 0x00;
+ret = cyapa_i2c_write(cyapa, 0, 2, cmd);
+if (ret)
+goto error;
+
+msleep(50);
+
+ret = cyapa_i2c_read(cyapa, BL_HEAD_OFFSET,
+BL_STATUS_SIZE, status);
+if (ret < 0)
+goto error;
+}
+} while (--retries > 0 && !smbus);
+
+goto error;
+
+out_detected:
+return 0;
+
+error:
+return (ret < 0) ? ret : -EAGAIN;
 }
 
 /*
@@ -421,6 +502,8 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
  */
 static int cyapa_initialize(struct cyapa *cyapa)
 {
+int ret = 0;
+
 cyapa->state = CYAPA_STATE_NO_DEVICE;
 cyapa->gen = CYAPA_GEN_UNKNOWN;
 mutex_init(&cyapa->state_sync_lock);
@@ -433,6 +516,11 @@ static int cyapa_initialize(struct cyapa *cyapa)
 cyapa->suspend_sleep_time =
 cyapa_pwr_cmd_to_sleep_time(cyapa->suspend_power_mode);
 
+if (cyapa_gen3_ops.initialize)
+ret = cyapa_gen3_ops.initialize(cyapa);
+if (ret)
+return ret;
+
 return cyapa_detect(cyapa);
 }
 
drivers/input/mouse/cyapa.h +1 -0
index ee97d7c..b281dcb 100644
@@ -317,5 +317,6 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
 
 
 extern const char unique_str[];
+extern const struct cyapa_dev_ops cyapa_gen3_ops;
 
 #endif
(quoted diff) +0 -0

b/drivers/input/mouse/cyapa_gen3.c

Quoted 57 lines

new file mode 100644
index 0000000..bd00c6e

drivers/input/mouse/cyapa_gen3.c new file +52 -0
@@ -0,0 +1,788 @@
+/*
+ * Cypress APA trackpad with I2C interface
+ *
+ * Author: Dudley Du <[email protected]>
+ * Further cleanup and restructuring by:
+ *   Daniel Kurtz <[email protected]>
+ *   Benson Leung <[email protected]>
+ *
+ * Copyright (C) 2011-2014 Cypress Semiconductor, Inc.
+ * Copyright (C) 2011-2012 Google, Inc.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include "cyapa.h"
+
+
+#define GEN3_MAX_FINGERS 5
+#define GEN3_FINGER_NUM(x) (((x) >> 4) & 0x07)
+
+#define BLK_HEAD_BYTES 32
+
+/* Macro for register map group offset. */
+#define PRODUCT_ID_SIZE  16
+#define QUERY_DATA_SIZE  27
+#define REG_PROTOCOL_GEN_QUERY_OFFSET  20
+
+#define REG_OFFSET_DATA_BASE     0x0000
+#define REG_OFFSET_COMMAND_BASE  0x0028
+#define REG_OFFSET_QUERY_BASE    0x002a
+
+#define CYAPA_OFFSET_SOFT_RESET  REG_OFFSET_COMMAND_BASE
+#define OP_RECALIBRATION_MASK    0x80
+#define OP_REPORT_BASELINE_MASK  0x40
+#define REG_OFFSET_MAX_BASELINE  0x0026
+#define REG_OFFSET_MIN_BASELINE  0x0027
+
+#define REG_OFFSET_POWER_MODE (REG_OFFSET_COMMAND_BASE + 1)
+#define SET_POWER_MODE_DELAY   10000  /* unit: us */
+#define SET_POWER_MODE_TRIES   5
+
+/*
+ * CYAPA trackpad device states.
+ * Used in register 0x00, bit1-0, DeviceStatus field.

drivers/input/mouse/cyapa_gen3.c line 62

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+ * Other values indicate device is in an abnormal state and must be reset.
+ */
+#define CYAPA_DEV_NORMAL  0x03
+#define CYAPA_DEV_BUSY    0x01
+
+#define CYAPA_FW_BLOCK_SIZE64
+#define CYAPA_FW_READ_SIZE16
+#define CYAPA_FW_HDR_START0x0780
+#define CYAPA_FW_HDR_BLOCK_COUNT  2
+#define CYAPA_FW_HDR_BLOCK_START  (CYAPA_FW_HDR_START /

CYAPA_FW_BLOCK_SIZE)

drivers/input/mouse/cyapa_gen3.c

Quoted 5 lines
drivers/input/mouse/cyapa_gen3.c new file +5 -0
+#define CYAPA_FW_HDR_SIZE  (CYAPA_FW_HDR_BLOCK_COUNT * \
+CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_DATA_START0x0800
+#define CYAPA_FW_DATA_BLOCK_COUNT  480
+#define CYAPA_FW_DATA_BLOCK_START  (CYAPA_FW_DATA_START /

CYAPA_FW_BLOCK_SIZE)

drivers/input/mouse/cyapa_gen3.c line 74

Quoted 7 lines
drivers/input/mouse/cyapa_gen3.c new file +7 -0
+#define CYAPA_FW_DATA_SIZE(CYAPA_FW_DATA_BLOCK_COUNT * \
+ CYAPA_FW_BLOCK_SIZE)
+#define CYAPA_FW_SIZE(CYAPA_FW_HDR_SIZE + CYAPA_FW_DATA_SIZE)
+#define CYAPA_CMD_LEN16
+
+#define GEN3_BL_IDLE_FW_MAJ_VER_OFFSET 0x0b
+#define GEN3_BL_IDLE_FW_MIN_VER_OFFSET

(GEN3_BL_IDLE_FW_MAJ_VER_OFFSET + 1)

Quoted 43 lines
drivers/input/mouse/cyapa_gen3.c new file +43 -0
+
+
+struct cyapa_touch {
+/*
+ * high bits or x/y position value
+ * bit 7 - 4: high 4 bits of x position value
+ * bit 3 - 0: high 4 bits of y position value
+ */
+u8 xy_hi;
+u8 x_lo;  /* low 8 bits of x position value. */
+u8 y_lo;  /* low 8 bits of y position value. */
+u8 pressure;
+/* id range is 1 - 15.  It is incremented with every new touch. */
+u8 id;
+} __packed;
+
+struct cyapa_reg_data {
+/*
+ * bit 0 - 1: device status
+ * bit 3 - 2: power mode
+ * bit 6 - 4: reserved
+ * bit 7: interrupt valid bit
+ */
+u8 device_status;
+/*
+ * bit 7 - 4: number of fingers currently touching pad
+ * bit 3: valid data check bit
+ * bit 2: middle mechanism button state if exists
+ * bit 1: right mechanism button state if exists
+ * bit 0: left mechanism button state if exists
+ */
+u8 finger_btn;
+/* CYAPA reports up to 5 touches per packet. */
+struct cyapa_touch touches[5];
+} __packed;
+
+static const u8 bl_activate[] = { 0x00, 0xff, 0x38, 0x00, 0x01, 0x02, 0x03,
+0x04, 0x05, 0x06, 0x07 };
+static const u8 bl_deactivate[] = { 0x00, 0xff, 0x3b, 0x00, 0x01, 0x02, 0x03,
+0x04, 0x05, 0x06, 0x07 };
+static const u8 bl_exit[] = { 0x00, 0xff, 0xa5, 0x00, 0x01, 0x02, 0x03, 0x04,
+0x05, 0x06, 0x07 };
+

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+ /* for byte read/write command */
+#define CMD_RESET      0
+#define CMD_POWER_MODE 1
+#define CMD_DEV_STATUS 2
+#define CMD_REPORT_MAX_BASELINE 3
+#define CMD_REPORT_MIN_BASELINE 4
+#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
+#define CYAPA_SMBUS_RESET         SMBUS_BYTE_CMD(CMD_RESET)
+#define CYAPA_SMBUS_POWER_MODE

SMBUS_BYTE_CMD(CMD_POWER_MODE)

drivers/input/mouse/cyapa_gen3.c line 128

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_DEV_STATUS

SMBUS_BYTE_CMD(CMD_DEV_STATUS)

drivers/input/mouse/cyapa_gen3.c line 129

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_MAX_BASELINE

SMBUS_BYTE_CMD(CMD_REPORT_MAX_BASELINE)

drivers/input/mouse/cyapa_gen3.c line 130

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_MIN_BASELINE

SMBUS_BYTE_CMD(CMD_REPORT_MIN_BASELINE)

drivers/input/mouse/cyapa_gen3.c

Quoted 7 lines
drivers/input/mouse/cyapa_gen3.c new file +7 -0
+
+ /* for group registers read/write command */
+#define REG_GROUP_DATA 0
+#define REG_GROUP_CMD 2
+#define REG_GROUP_QUERY 3
+#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
+#define CYAPA_SMBUS_GROUP_DATA

SMBUS_GROUP_CMD(REG_GROUP_DATA)

drivers/input/mouse/cyapa_gen3.c line 138

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_GROUP_CMD

SMBUS_GROUP_CMD(REG_GROUP_CMD)

drivers/input/mouse/cyapa_gen3.c line 139

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_GROUP_QUERY

SMBUS_GROUP_CMD(REG_GROUP_QUERY)

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+ /* for register block read/write command */
+#define CMD_BL_STATUS 0
+#define CMD_BL_HEAD 1
+#define CMD_BL_CMD 2
+#define CMD_BL_DATA 3
+#define CMD_BL_ALL 4
+#define CMD_BLK_PRODUCT_ID 5
+#define CMD_BLK_HEAD 6
+#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+/* register block read/write command in bootloader mode */
+#define CYAPA_SMBUS_BL_STATUS  SMBUS_BLOCK_CMD(CMD_BL_STATUS)
+#define CYAPA_SMBUS_BL_HEAD    SMBUS_BLOCK_CMD(CMD_BL_HEAD)
+#define CYAPA_SMBUS_BL_CMD     SMBUS_BLOCK_CMD(CMD_BL_CMD)
+#define CYAPA_SMBUS_BL_DATA    SMBUS_BLOCK_CMD(CMD_BL_DATA)
+#define CYAPA_SMBUS_BL_ALL     SMBUS_BLOCK_CMD(CMD_BL_ALL)
+
+/* register block read/write command in operational mode */
+#define CYAPA_SMBUS_BLK_PRODUCT_ID

SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)

drivers/input/mouse/cyapa_gen3.c

Quoted 11 lines
drivers/input/mouse/cyapa_gen3.c new file +11 -0
+#define CYAPA_SMBUS_BLK_HEAD SMBUS_BLOCK_CMD(CMD_BLK_HEAD)
+
+ /* for byte read/write command */
+#define CMD_RESET 0
+#define CMD_POWER_MODE 1
+#define CMD_DEV_STATUS 2
+#define CMD_REPORT_MAX_BASELINE 3
+#define CMD_REPORT_MIN_BASELINE 4
+#define SMBUS_BYTE_CMD(cmd) (((cmd) & 0x3f) << 1)
+#define CYAPA_SMBUS_RESET         SMBUS_BYTE_CMD(CMD_RESET)
+#define CYAPA_SMBUS_POWER_MODE

SMBUS_BYTE_CMD(CMD_POWER_MODE)

drivers/input/mouse/cyapa_gen3.c line 171

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_DEV_STATUS

SMBUS_BYTE_CMD(CMD_DEV_STATUS)

drivers/input/mouse/cyapa_gen3.c line 172

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_MAX_BASELINE

SMBUS_BYTE_CMD(CMD_REPORT_MAX_BASELINE)

drivers/input/mouse/cyapa_gen3.c line 173

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_MIN_BASELINE

SMBUS_BYTE_CMD(CMD_REPORT_MIN_BASELINE)

drivers/input/mouse/cyapa_gen3.c

Quoted 7 lines
drivers/input/mouse/cyapa_gen3.c new file +7 -0
+
+ /* for group registers read/write command */
+#define REG_GROUP_DATA  0
+#define REG_GROUP_CMD   2
+#define REG_GROUP_QUERY 3
+#define SMBUS_GROUP_CMD(grp) (0x80 | (((grp) & 0x07) << 3))
+#define CYAPA_SMBUS_GROUP_DATA

SMBUS_GROUP_CMD(REG_GROUP_DATA)

drivers/input/mouse/cyapa_gen3.c line 181

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_GROUP_CMD

SMBUS_GROUP_CMD(REG_GROUP_CMD)

drivers/input/mouse/cyapa_gen3.c line 182

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_GROUP_QUERY

SMBUS_GROUP_CMD(REG_GROUP_QUERY)

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+ /* for register block read/write command */
+#define CMD_BL_STATUS0
+#define CMD_BL_HEAD1
+#define CMD_BL_CMD2
+#define CMD_BL_DATA3
+#define CMD_BL_ALL4
+#define CMD_BLK_PRODUCT_ID5
+#define CMD_BLK_HEAD6
+#define SMBUS_BLOCK_CMD(cmd) (0xc0 | (((cmd) & 0x1f) << 1))

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+/* register block read/write command in bootloader mode */
+#define CYAPA_SMBUS_BL_STATUS SMBUS_BLOCK_CMD(CMD_BL_STATUS)
+#define CYAPA_SMBUS_BL_HEAD   SMBUS_BLOCK_CMD(CMD_BL_HEAD)
+#define CYAPA_SMBUS_BL_CMD    SMBUS_BLOCK_CMD(CMD_BL_CMD)
+#define CYAPA_SMBUS_BL_DATA   SMBUS_BLOCK_CMD(CMD_BL_DATA)
+#define CYAPA_SMBUS_BL_ALL    SMBUS_BLOCK_CMD(CMD_BL_ALL)
+
+/* register block read/write command in operational mode */
+#define CYAPA_SMBUS_BLK_PRODUCT_ID

SMBUS_BLOCK_CMD(CMD_BLK_PRODUCT_ID)

drivers/input/mouse/cyapa_gen3.c line 203

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+#define CYAPA_SMBUS_BLK_HEAD

SMBUS_BLOCK_CMD(CMD_BLK_HEAD)

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+struct cyapa_cmd_len {
+u8 cmd;
+u8 len;
+};
+
+/* maps generic CYAPA_CMD_* code to the I2C equivalent */
+static const struct cyapa_cmd_len cyapa_i2c_cmds[] = {
+{ CYAPA_OFFSET_SOFT_RESET, 1 },/* CYAPA_CMD_SOFT_RESET */
+{ REG_OFFSET_COMMAND_BASE + 1, 1 },/* CYAPA_CMD_POWER_MODE

*/

drivers/input/mouse/cyapa_gen3.c line 218

Quoted 5 lines
drivers/input/mouse/cyapa_gen3.c new file +5 -0
+{ REG_OFFSET_DATA_BASE, 1 },/* CYAPA_CMD_DEV_STATUS */
+{ REG_OFFSET_DATA_BASE, sizeof(struct cyapa_reg_data) },
+/* CYAPA_CMD_GROUP_DATA */
+{ REG_OFFSET_COMMAND_BASE, 0 },/* CYAPA_CMD_GROUP_CMD */
+{ REG_OFFSET_QUERY_BASE, QUERY_DATA_SIZE }, /*

CYAPA_CMD_GROUP_QUERY */

drivers/input/mouse/cyapa_gen3.c line 227

Quoted 9 lines
drivers/input/mouse/cyapa_gen3.c new file +9 -0
+{ BL_HEAD_OFFSET, 3 },/* CYAPA_CMD_BL_STATUS */
+{ BL_HEAD_OFFSET, 16 },/* CYAPA_CMD_BL_HEAD */
+{ BL_HEAD_OFFSET, 16 },/* CYAPA_CMD_BL_CMD */
+{ BL_DATA_OFFSET, 16 },/* CYAPA_CMD_BL_DATA */
+{ BL_HEAD_OFFSET, 32 },/* CYAPA_CMD_BL_ALL */
+{ REG_OFFSET_QUERY_BASE, PRODUCT_ID_SIZE },
+/* CYAPA_CMD_BLK_PRODUCT_ID */
+{ REG_OFFSET_DATA_BASE, 32 },/* CYAPA_CMD_BLK_HEAD */
+{ REG_OFFSET_MAX_BASELINE, 1 },/* CYAPA_CMD_MAX_BASELINE

*/

drivers/input/mouse/cyapa_gen3.c line 228

Quoted 1 line
drivers/input/mouse/cyapa_gen3.c new file +1 -0
+{ REG_OFFSET_MIN_BASELINE, 1 },/* CYAPA_CMD_MIN_BASELINE

*/

drivers/input/mouse/cyapa_gen3.c

Quoted 5 lines
drivers/input/mouse/cyapa_gen3.c new file +5 -0
+};
+
+static const struct cyapa_cmd_len cyapa_smbus_cmds[] = {
+{ CYAPA_SMBUS_RESET, 1 },/* CYAPA_CMD_SOFT_RESET */
+{ CYAPA_SMBUS_POWER_MODE, 1 },/* CYAPA_CMD_POWER_MODE

*/

Quoted 13 lines
drivers/input/mouse/cyapa_gen3.c new file +13 -0
+{ CYAPA_SMBUS_DEV_STATUS, 1 },/* CYAPA_CMD_DEV_STATUS */
+{ CYAPA_SMBUS_GROUP_DATA, sizeof(struct cyapa_reg_data) },
+/* CYAPA_CMD_GROUP_DATA */
+{ CYAPA_SMBUS_GROUP_CMD, 2 },/* CYAPA_CMD_GROUP_CMD */
+{ CYAPA_SMBUS_GROUP_QUERY, QUERY_DATA_SIZE },
+/* CYAPA_CMD_GROUP_QUERY */
+{ CYAPA_SMBUS_BL_STATUS, 3 },/* CYAPA_CMD_BL_STATUS */
+{ CYAPA_SMBUS_BL_HEAD, 16 },/* CYAPA_CMD_BL_HEAD */
+{ CYAPA_SMBUS_BL_CMD, 16 },/* CYAPA_CMD_BL_CMD */
+{ CYAPA_SMBUS_BL_DATA, 16 },/* CYAPA_CMD_BL_DATA */
+{ CYAPA_SMBUS_BL_ALL, 32 },/* CYAPA_CMD_BL_ALL */
+{ CYAPA_SMBUS_BLK_PRODUCT_ID, PRODUCT_ID_SIZE },
+/* CYAPA_CMD_BLK_PRODUCT_ID */
Quoted 8 lines

drivers/input/mouse/cyapa_gen3.c line 252

Quoted 6 lines
drivers/input/mouse/cyapa_gen3.c new file +6 -0
+{ CYAPA_SMBUS_BLK_HEAD, 16 },/* CYAPA_CMD_BLK_HEAD */
+{ CYAPA_SMBUS_MAX_BASELINE, 1 },/* CYAPA_CMD_MAX_BASELINE */
+{ CYAPA_SMBUS_MIN_BASELINE, 1 },/* CYAPA_CMD_MIN_BASELINE */
+};
+
+static bool data_reporting_started;

Why is this a global?

Thanks, it should not be global, I will fix it and included in the gen3 private data.

Quoted 538 lines
Quoted 268 lines
drivers/input/mouse/cyapa_gen3.c new file +267 -0
+
+
+/*
+ * cyapa_smbus_read_block - perform smbus block read command
+ * @cyapa  - private data structure of the driver
+ * @cmd    - the properly encoded smbus command
+ * @len    - expected length of smbus command result
+ * @values - buffer to store smbus command result
+ *
+ * Returns negative errno, else the number of bytes written.
+ *
+ * Note:
+ * In trackpad device, the memory block allocated for I2C register map
+ * is 256 bytes, so the max read block for I2C bus is 256 bytes.
+ */
+ssize_t cyapa_smbus_read_block(struct cyapa *cyapa, u8 cmd, size_t len,
+      u8 *values)
+{
+ssize_t ret;
+u8 index;
+u8 smbus_cmd;
+u8 *buf;
+struct i2c_client *client = cyapa->client;
+
+if (!(SMBUS_BYTE_BLOCK_CMD_MASK & cmd))
+return -EINVAL;
+
+if (SMBUS_GROUP_BLOCK_CMD_MASK & cmd) {
+/* read specific block registers command. */
+smbus_cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
+ret = i2c_smbus_read_block_data(client, smbus_cmd, values);
+goto out;
+}
+
+ret = 0;
+for (index = 0; index * I2C_SMBUS_BLOCK_MAX < len; index++) {
+smbus_cmd = SMBUS_ENCODE_IDX(cmd, index);
+smbus_cmd = SMBUS_ENCODE_RW(smbus_cmd, SMBUS_READ);
+buf = values + I2C_SMBUS_BLOCK_MAX * index;
+ret = i2c_smbus_read_block_data(client, smbus_cmd, buf);
+if (ret < 0)
+goto out;
+}
+
+out:
+return ret > 0 ? len : ret;
+}
+
+s32 cyapa_read_byte(struct cyapa *cyapa, u8 cmd_idx)
+{
+u8 cmd;
+
+if (cyapa->smbus) {
+cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+cmd = SMBUS_ENCODE_RW(cmd, SMBUS_READ);
+} else {
+cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+}
+return i2c_smbus_read_byte_data(cyapa->client, cmd);
+}
+
+s32 cyapa_write_byte(struct cyapa *cyapa, u8 cmd_idx, u8 value)
+{
+u8 cmd;
+
+if (cyapa->smbus) {
+cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+cmd = SMBUS_ENCODE_RW(cmd, SMBUS_WRITE);
+} else {
+cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+}
+return i2c_smbus_write_byte_data(cyapa->client, cmd, value);
+}
+
+ssize_t cyapa_read_block(struct cyapa *cyapa, u8 cmd_idx, u8 *values)
+{
+u8 cmd;
+size_t len;
+
+if (cyapa->smbus) {
+cmd = cyapa_smbus_cmds[cmd_idx].cmd;
+len = cyapa_smbus_cmds[cmd_idx].len;
+return cyapa_smbus_read_block(cyapa, cmd, len, values);
+}
+cmd = cyapa_i2c_cmds[cmd_idx].cmd;
+len = cyapa_i2c_cmds[cmd_idx].len;
+return cyapa_i2c_reg_read_block(cyapa, cmd, len, values);
+}
+
+/*
+ * Determine the Gen3 trackpad device's current operating state.
+ *
+ */
+static int cyapa_gen3_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
+{
+/*
+ * Must be in detecting and should not do data reporting.
+ * It will be reenabled when all detecting done and lauched into
+ * applicaiton mode successfully.
+ */
+data_reporting_started = false;
+cyapa->state = CYAPA_STATE_NO_DEVICE;
+
+/* Parse based on Gen3 characteristic registers and bits */
+if (reg_data[REG_BL_FILE] == BL_FILE &&
+reg_data[REG_BL_ERROR] == BL_ERROR_NO_ERR_IDLE &&
+(reg_data[REG_BL_STATUS] ==
+(BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID) ||
+reg_data[REG_BL_STATUS] == BL_STATUS_RUNNING)) {
+/*
+ * Normal state after power on or reset,
+ * REG_BL_STATUS == 0x11, firmware image checksum is valid.
+ * REG_BL_STATUS == 0x10, firmware image checksum is invalid.
+ */
+cyapa->gen = CYAPA_GEN3;
+cyapa->state = CYAPA_STATE_BL_IDLE;
+} else if (reg_data[REG_BL_FILE] == BL_FILE &&
+(reg_data[REG_BL_STATUS] & BL_STATUS_RUNNING) ==
+BL_STATUS_RUNNING) {
+cyapa->gen = CYAPA_GEN3;
+if (reg_data[REG_BL_STATUS] & BL_STATUS_BUSY) {
+cyapa->state = CYAPA_STATE_BL_BUSY;
+} else {
+if ((reg_data[REG_BL_ERROR] & BL_ERROR_BOOTLOADING) ==
+BL_ERROR_BOOTLOADING)
+cyapa->state = CYAPA_STATE_BL_ACTIVE;
+else
+cyapa->state = CYAPA_STATE_BL_IDLE;
+}
+} else if ((reg_data[REG_OP_STATUS] & OP_STATUS_SRC) &&
+(reg_data[REG_OP_DATA1] & OP_DATA_VALID)) {
+/*
+ * Normal state when running in operaitonal mode,
+ * may also not in full power state or
+ * busying in command process.
+ */
+if (GEN3_FINGER_NUM(reg_data[REG_OP_DATA1]) <=
+GEN3_MAX_FINGERS) {
+/* Finger number data is valid. */
+cyapa->gen = CYAPA_GEN3;
+cyapa->state = CYAPA_STATE_OP;
+}
+} else if (reg_data[REG_OP_STATUS] == 0x0C &&
+reg_data[REG_OP_DATA1] == 0x08) {
+/* Op state when first two registers overwritten with 0x00 */
+cyapa->gen = CYAPA_GEN3;
+cyapa->state = CYAPA_STATE_OP;
+} else if (reg_data[REG_BL_STATUS] &
+(BL_STATUS_RUNNING | BL_STATUS_BUSY)) {
+cyapa->gen = CYAPA_GEN3;
+cyapa->state = CYAPA_STATE_BL_BUSY;
+}
+
+if (cyapa->gen == CYAPA_GEN3 && (cyapa->state == CYAPA_STATE_OP ||
+cyapa->state == CYAPA_STATE_BL_IDLE ||
+cyapa->state == CYAPA_STATE_BL_ACTIVE ||
+cyapa->state == CYAPA_STATE_BL_BUSY))
+return 0;
+
+return -EAGAIN;
+}
+
+static int cyapa_gen3_bl_deactivate(struct cyapa *cyapa)
+{
+int ret;
+
+ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_deactivate),
+bl_deactivate);
+if (ret < 0)
+return ret;
+
+/* Wait for bootloader to switch to idle state; should take < 100ms */
+msleep(100);
+ret = cyapa_poll_state(cyapa, 500);
+if (ret < 0)
+return ret;
+if (cyapa->state != CYAPA_STATE_BL_IDLE)
+return -EAGAIN;
+return 0;
+}
+
+/*
+ * Exit bootloader
+ *
+ * Send bl_exit command, then wait 50 - 100 ms to let device transition to
+ * operational mode.  If this is the first time the device's firmware is
+ * running, it can take up to 2 seconds to calibrate its sensors.  So, poll
+ * the device's new state for up to 2 seconds.
+ *
+ * Returns:
+ *   -EIO    failure while reading from device
+ *   -EAGAIN device is stuck in bootloader, b/c it has invalid firmware
+ *   0       device is supported and in operational mode
+ */
+static int cyapa_gen3_bl_exit(struct cyapa *cyapa)
+{
+int ret;
+
+ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_exit), bl_exit);
+if (ret < 0)
+return ret;
+
+/*
+ * Wait for bootloader to exit, and operation mode to start.
+ * Normally, this takes at least 50 ms.
+ */
+usleep_range(50000, 100000);
+/*
+ * In addition, when a device boots for the first time after being
+ * updated to new firmware, it must first calibrate its sensors, which
+ * can take up to an additional 2 seconds. If the device power is
+ * running low, this may take even longer.
+ */
+ret = cyapa_poll_state(cyapa, 4000);
+if (ret < 0)
+return ret;
+if (cyapa->state != CYAPA_STATE_OP)
+return -EAGAIN;
+
+return 0;
+}
+
+/*
+ * cyapa_get_wait_time_for_pwr_cmd
+ *
+ * Compute the amount of time we need to wait after updating the touchpad
+ * power mode. The touchpad needs to consume the incoming power mode set
+ * command at the current clock rate.
+ */
+
+static u16 cyapa_get_wait_time_for_pwr_cmd(u8 pwr_mode)
+{
+switch (pwr_mode) {
+case PWR_MODE_FULL_ACTIVE: return 20;
+case PWR_MODE_BTN_ONLY: return 20;
+case PWR_MODE_OFF: return 20;
+default: return cyapa_pwr_cmd_to_sleep_time(pwr_mode) + 50;
+}
+}
+
+/*
+ * Set device power mode
+ *
+ * Write to the field to configure power state. Power states include :
+ *   Full : Max scans and report rate.
+ *   Idle : Report rate set by user specified time.
+ *   ButtonOnly : No scans for fingers. When the button is triggered,
+ *     a slave interrupt is asserted to notify host to wake up.
+ *   Off : Only awake for i2c commands from host. No function for button
+ *     or touch sensors.
+ *
+ * The power_mode command should conform to the following :
+ *   Full : 0x3f
+ *   Idle : Configurable from 20 to 1000ms. See note below for
+ *     cyapa_sleep_time_to_pwr_cmd and cyapa_pwr_cmd_to_sleep_time
+ *   ButtonOnly : 0x01
+ *   Off : 0x00
+ *
+ * Device power mode can only be set when device is in operational mode.
+ */
+static int cyapa_gen3_set_power_mode(struct cyapa *cyapa, u8 power_mode,
+u16 always_unused)
+{
+int ret;
+u8 power;
+int tries = SET_POWER_MODE_TRIES;
+u16 sleep_time;

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+always_unused = 0;
+if (cyapa->state != CYAPA_STATE_OP)
+return 0;
+
+while (true) {
+ret = cyapa_read_byte(cyapa, CYAPA_CMD_POWER_MODE);
+if (ret >= 0 || --tries < 1)
+break;
+usleep_range(SET_POWER_MODE_DELAY, 2 *

SET_POWER_MODE_DELAY);

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+}
+if (ret < 0)
+return ret;
+
+/*
+ * Return early if the power mode to set is the same as the current
+ * one.
+ */
+if ((ret & PWR_MODE_MASK) == power_mode)
+return 0;

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+
+sleep_time = cyapa_get_wait_time_for_pwr_cmd(ret & PWR_MODE_MASK);
+power = ret;
+power &= ~PWR_MODE_MASK;
+power |= power_mode & PWR_MODE_MASK;
+while (true) {
+ret = cyapa_write_byte(cyapa, CYAPA_CMD_POWER_MODE, power);
+if (!ret || --tries < 1)
+break;
+usleep_range(SET_POWER_MODE_DELAY, 2 *

SET_POWER_MODE_DELAY);

Quoted 110 lines
drivers/input/mouse/cyapa_gen3.c new file +110 -0
+}
+
+/*
+ * Wait for the newly set power command to go in at the previous
+ * clock speed (scanrate) used by the touchpad firmware. Not
+ * doing so before issuing the next command may result in errors
+ * depending on the command's content.
+ */
+msleep(sleep_time);
+return ret;
+}
+
+static int cyapa_gen3_get_query_data(struct cyapa *cyapa)
+{
+u8 query_data[QUERY_DATA_SIZE];
+int ret;
+
+if (cyapa->state != CYAPA_STATE_OP)
+return -EBUSY;
+
+ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_QUERY, query_data);
+if (ret != QUERY_DATA_SIZE)
+return (ret < 0) ? ret : -EIO;
+
+memcpy(&cyapa->product_id[0], &query_data[0], 5);
+cyapa->product_id[5] = '-';
+memcpy(&cyapa->product_id[6], &query_data[5], 6);
+cyapa->product_id[12] = '-';
+memcpy(&cyapa->product_id[13], &query_data[11], 2);
+cyapa->product_id[15] = '\0';
+
+cyapa->fw_maj_ver = query_data[15];
+cyapa->fw_min_ver = query_data[16];
+
+cyapa->btn_capability = query_data[19] & CAPABILITY_BTN_MASK;
+
+cyapa->gen = query_data[20] & 0x0f;
+
+cyapa->max_abs_x = ((query_data[21] & 0xf0) << 4) | query_data[22];
+cyapa->max_abs_y = ((query_data[21] & 0x0f) << 8) | query_data[23];
+
+cyapa->physical_size_x =
+((query_data[24] & 0xf0) << 4) | query_data[25];
+cyapa->physical_size_y =
+((query_data[24] & 0x0f) << 8) | query_data[26];
+
+cyapa->max_z = 255;
+
+return 0;
+}
+
+static int cyapa_gen3_bl_query_data(struct cyapa *cyapa)
+{
+u8 bl_data[CYAPA_CMD_LEN];
+int ret;
+
+ret = cyapa_i2c_reg_read_block(cyapa, 0, CYAPA_CMD_LEN, bl_data);
+if (ret != CYAPA_CMD_LEN)
+return (ret < 0) ? ret : -EIO;
+
+/*
+ * This value will be updated again when entered application mode.
+ * If TP failed to enter application mode, this fw version values
+ * can be used as a reference.
+ * This firmware version valid when fw image checksum is valid.
+ */
+if (bl_data[REG_BL_STATUS] ==
+(BL_STATUS_RUNNING | BL_STATUS_CSUM_VALID)) {
+cyapa->fw_maj_ver = bl_data[GEN3_BL_IDLE_FW_MAJ_VER_OFFSET];
+cyapa->fw_min_ver = bl_data[GEN3_BL_IDLE_FW_MIN_VER_OFFSET];
+}
+
+return 0;
+}
+
+/*
+ * Check if device is operational.
+ *
+ * An operational device is responding, has exited bootloader, and has
+ * firmware supported by this driver.
+ *
+ * Returns:
+ *   -EBUSY  no device or in bootloader
+ *   -EIO    failure while reading from device
+ *   -EAGAIN device is still in bootloader
+ *           if ->state = CYAPA_STATE_BL_IDLE, device has invalid firmware
+ *   -EINVAL device is in operational mode, but not supported by this driver
+ *   0       device is supported
+ */
+static int cyapa_gen3_do_operational_check(struct cyapa *cyapa)
+{
+struct device *dev = &cyapa->client->dev;
+int ret;
+
+switch (cyapa->state) {
+case CYAPA_STATE_BL_ACTIVE:
+ret = cyapa_gen3_bl_deactivate(cyapa);
+if (ret) {
+dev_err(dev, "failed to bl_deactivate. %d\n", ret);
+return ret;
+}
+
+/* Fallthrough state */
+case CYAPA_STATE_BL_IDLE:
+/* Try to get firmware version in bootloader mode. */
+cyapa_gen3_bl_query_data(cyapa);
+
+ret = cyapa_gen3_bl_exit(cyapa);
+if (ret) {
+dev_err(dev, "failed to bl_exit. %d\n", ret);

drivers/input/mouse/cyapa_gen3.c

Quoted 10 lines
drivers/input/mouse/cyapa_gen3.c new file +10 -0
+return ret;
+}
+
+/* Fallthrough state */
+case CYAPA_STATE_OP:
+/*
+ * Reading query data before going back to the full mode
+ * may cause problems, so we set the power mode first here.
+ */
+ret = cyapa_gen3_set_power_mode(cyapa, PWR_MODE_FULL_ACTIVE,

0);

Quoted 117 lines
drivers/input/mouse/cyapa_gen3.c new file +117 -0
+if (ret)
+dev_err(dev, "%s: set full power mode failed, (%d)\n",
+__func__, ret);
+ret = cyapa_gen3_get_query_data(cyapa);
+if (ret < 0)
+return ret;
+
+/* Only support firmware protocol gen3 */
+if (cyapa->gen != CYAPA_GEN3) {
+dev_err(dev, "unsupported protocol version (%d)",
+cyapa->gen);
+return -EINVAL;
+}
+
+/* Only support product ID starting with CYTRA */
+if (memcmp(cyapa->product_id, unique_str,
+strlen(unique_str)) != 0) {
+dev_err(dev, "unsupported product ID (%s)\n",
+cyapa->product_id);
+return -EINVAL;
+}
+
+data_reporting_started = true;
+return 0;
+
+default:
+return -EIO;
+}
+return 0;
+}
+
+/*
+ * Return false, do not continue process
+ * Return true, continue process.
+ */
+static bool cyapa_gen3_irq_cmd_handler(struct cyapa *cyapa)
+{
+/* Not gen3 irq command response, skip for continue. */
+if (cyapa->gen != CYAPA_GEN3)
+return true;
+
+if (cyapa->input && data_reporting_started)
+return true;
+
+/*
+ * Driver in detecting or other interface function processing,
+ * so, stop cyapa_gen3_irq_handler to continue process to
+ * avoid unwanted to error detecting and processing.
+ *
+ * And also, avoid the periodicly accerted interrupts to be processed
+ * as touch inputs when gen3 failed to launch into application mode,
+ * which will cause gen3 stays in bootloader mode.
+ */
+return false;
+}
+
+static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
+{
+struct input_dev *input = cyapa->input;
+struct device *dev = &cyapa->client->dev;
+struct cyapa_reg_data data;
+int i;
+int ret;
+int num_fingers;
+
+ret = cyapa_read_block(cyapa, CYAPA_CMD_GROUP_DATA, (u8 *)&data);
+if (ret != sizeof(data)) {
+dev_err(dev, "failed to read report data, (%d)\n", ret);
+return -EINVAL;
+}
+
+if ((data.device_status & OP_STATUS_SRC) != OP_STATUS_SRC ||
+    (data.device_status & OP_STATUS_DEV) != CYAPA_DEV_NORMAL ||
+    (data.finger_btn & OP_DATA_VALID) != OP_DATA_VALID) {
+dev_err(dev, "invalid device state bytes, %02x %02x\n",
+data.device_status, data.finger_btn);
+return -EINVAL;
+}
+
+num_fingers = (data.finger_btn >> 4) & 0x0f;
+for (i = 0; i < num_fingers; i++) {
+const struct cyapa_touch *touch = &data.touches[i];
+/* Note: touch->id range is 1 to 15; slots are 0 to 14. */
+int slot = touch->id - 1;
+
+input_mt_slot(input, slot);
+input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
+input_report_abs(input, ABS_MT_POSITION_X,
+ ((touch->xy_hi & 0xf0) << 4) | touch->x_lo);
+input_report_abs(input, ABS_MT_POSITION_Y,
+ ((touch->xy_hi & 0x0f) << 8) | touch->y_lo);
+input_report_abs(input, ABS_MT_PRESSURE, touch->pressure);
+}
+
+input_mt_sync_frame(input);
+
+if (cyapa->btn_capability & CAPABILITY_LEFT_BTN_MASK)
+input_report_key(input, BTN_LEFT,
+ !!(data.finger_btn & OP_DATA_LEFT_BTN));
+if (cyapa->btn_capability & CAPABILITY_MIDDLE_BTN_MASK)
+input_report_key(input, BTN_MIDDLE,
+ !!(data.finger_btn & OP_DATA_MIDDLE_BTN));
+if (cyapa->btn_capability & CAPABILITY_RIGHT_BTN_MASK)
+input_report_key(input, BTN_RIGHT,
+ !!(data.finger_btn & OP_DATA_RIGHT_BTN));
+input_sync(input);
+
+return 0;
+}
+
+const struct cyapa_dev_ops cyapa_gen3_ops = {
+.state_parse = cyapa_gen3_state_parse,
+.operational_check = cyapa_gen3_do_operational_check,
+
+.irq_handler = cyapa_gen3_irq_handler,
+.irq_cmd_handler = cyapa_gen3_irq_cmd_handler,
+
Quoted 8 lines
Quoted 5 lines
drivers/input/mouse/cyapa_gen3.c new file +2 -1
+.set_power_mode = cyapa_gen3_set_power_mode,
+};
--

1.9.1

--
Dmitry

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

author
#11 lore raw

PATCH v9 4/18 input: cyapa: add gen5 trackpad device basic functions support

In reply to Dudley Du

Based on the cyapa core, add the gen5 trackpad device's basic functions supported, so gen5 trackpad device can work with kernel input system. And also based on the state parse interface, the cyapa driver can automatically determine the attached is gen3 or gen5 protocol trackpad device, then set the correct protocol to work with the attached trackpad device.
TEST=test on Chromebooks.

---
 drivers/input/mouse/Makefile     |    2 +-
 drivers/input/mouse/cyapa.c      |   13 +
 drivers/input/mouse/cyapa.h      |    1 +
 drivers/input/mouse/cyapa_gen5.c | 1656 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 1671 insertions(+), 1 deletion(-)
 create mode 100644 drivers/input/mouse/cyapa_gen5.c
drivers/input/mouse/Makefile +1 -1
index 4bf6c83..1d3d997 100644
@@ -23,7 +23,7 @@ obj-$(CONFIG_MOUSE_SYNAPTICS_I2C)	+= synaptics_i2c.o
 obj-$(CONFIG_MOUSE_SYNAPTICS_USB)	+= synaptics_usb.o
 obj-$(CONFIG_MOUSE_VSXXXAA)		+= vsxxxaa.o
 
-cyapatp-objs := cyapa.o cyapa_gen3.o
+cyapatp-objs := cyapa.o cyapa_gen3.o cyapa_gen5.o
 psmouse-objs := psmouse-base.o synaptics.o focaltech.o
 
 psmouse-$(CONFIG_MOUSE_PS2_ALPS)	+= alps.o
drivers/input/mouse/cyapa.c +13 -0
index cb81baf..fac361a 100644
@@ -234,6 +234,9 @@ static int cyapa_check_is_operational(struct cyapa *cyapa)
 		return ret;
 
 	switch (cyapa->gen) {
+	case CYAPA_GEN5:
+		cyapa->ops = &cyapa_gen5_ops;
+		break;
 	case CYAPA_GEN3:
 		cyapa->ops = &cyapa_gen3_ops;
 		break;
@@ -335,6 +338,14 @@ static int cyapa_get_state(struct cyapa *cyapa)
 			if (ret == 0)
 				goto out_detected;
 		}
+		if ((cyapa->gen == CYAPA_GEN_UNKNOWN ||
+				cyapa->gen == CYAPA_GEN5) &&
+			!smbus && even_addr) {
+			ret = cyapa_gen5_ops.state_parse(cyapa,
+					status, BL_STATUS_SIZE);
+			if (ret == 0)
+				goto out_detected;
+		}
 
 		/*
 		 * Cannot detect communication protocol based on current
@@ -518,6 +529,8 @@ static int cyapa_initialize(struct cyapa *cyapa)
 
 	if (cyapa_gen3_ops.initialize)
 		ret = cyapa_gen3_ops.initialize(cyapa);
+	if (!ret && cyapa_gen5_ops.initialize)
+		ret = cyapa_gen5_ops.initialize(cyapa);
 	if (ret)
 		return ret;
 
drivers/input/mouse/cyapa.h +1 -0
index b281dcb..060ac7d 100644
@@ -318,5 +318,6 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode);
 
 extern const char unique_str[];
 extern const struct cyapa_dev_ops cyapa_gen3_ops;
+extern const struct cyapa_dev_ops cyapa_gen5_ops;
 
 #endif
drivers/input/mouse/cyapa_gen5.c new file +1656 -0

1,656 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#12 lore raw

PATCH v9 5/18 input: cyapa: add power management interfaces supported for the device

In reply to Dudley Du

Add suspend_scanrate_ms power management interfaces in device's power group, so users or applications can control the power management strategy of trackpad device as their requirements. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 112 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
drivers/input/mouse/cyapa.c +112 -0
index fac361a..420696d 100644
@@ -505,6 +505,96 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
 				   : (encoded_time - 5) * 20;
 }
 
+#ifdef CONFIG_PM_SLEEP
+static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u8 pwr_cmd = cyapa->suspend_power_mode;
+	u16 sleep_time;
+	int len;
+	int ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	pwr_cmd = cyapa->suspend_power_mode;
+	sleep_time = cyapa->suspend_sleep_time;
+	mutex_unlock(&cyapa->state_sync_lock);
+
+	if (pwr_cmd == PWR_MODE_BTN_ONLY)
+		len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
+	else if (pwr_cmd == PWR_MODE_OFF)
+		len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
+	else {
+		if (cyapa->gen == CYAPA_GEN3)
+			sleep_time = cyapa_pwr_cmd_to_sleep_time(pwr_cmd);
+		len = scnprintf(buf, PAGE_SIZE, "%u\n", sleep_time);
+	}
+
+	return len;
+}
+
+static u16 cyapa_clamp_sleep_time(u16 sleep_time)
+{
+	if (sleep_time > 1000)
+		sleep_time = 1000;
+	return sleep_time;
+}
+
+static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
+					     struct device_attribute *attr,
+					     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u16 sleep_time;
+	int ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (sysfs_streq(buf, BTN_ONLY_MODE_NAME))
+		cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
+	else if (sysfs_streq(buf, OFF_MODE_NAME))
+		cyapa->suspend_power_mode = PWR_MODE_OFF;
+	else if (!kstrtou16(buf, 10, &sleep_time)) {
+		cyapa->suspend_sleep_time = cyapa_clamp_sleep_time(sleep_time);
+		cyapa->suspend_power_mode =
+			cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
+	} else
+		count = 0;
+
+	mutex_unlock(&cyapa->state_sync_lock);
+	if (!count)
+		dev_err(dev, "invalid suspend scanrate ms parameters\n");
+	return count ? count : -EINVAL;
+}
+
+static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
+		   cyapa_show_suspend_scanrate,
+		   cyapa_update_suspend_scanrate);
+
+static struct attribute *cyapa_power_wakeup_entries[] = {
+	&dev_attr_suspend_scanrate_ms.attr,
+	NULL,
+};
+
+static const struct attribute_group cyapa_power_wakeup_group = {
+	.name = power_group_name,
+	.attrs = cyapa_power_wakeup_entries,
+};
+
+static void cyapa_remove_power_wakeup_group(void *data)
+{
+	struct cyapa *cyapa = data;
+
+	sysfs_unmerge_group(&cyapa->client->dev.kobj,
+				&cyapa_power_wakeup_group);
+}
+#endif /* CONFIG_PM_SLEEP */
+
 /*
  * Returns:
  *   0    Driver and device initialization successfully done.
@@ -588,6 +678,28 @@ static int cyapa_probe(struct i2c_client *client,
 		return ret;
 	}
 
+#ifdef CONFIG_PM_SLEEP
+	if (device_can_wakeup(dev)) {
+		ret = sysfs_merge_group(&client->dev.kobj,
+					&cyapa_power_wakeup_group);
+		if (ret) {
+			dev_err(dev, "failed to add power wakeup group, (%d)\n",
+				ret);
+			return ret;
+		}
+
+		ret = devm_add_action(dev,
+				cyapa_remove_power_wakeup_group, cyapa);
+		if (ret) {
+			cyapa_remove_power_wakeup_group(cyapa);
+			dev_err(dev,
+				"failed to add power cleanup action, (%d)\n",
+				ret);
+			return ret;
+		}
+	}
+#endif /* CONFIG_PM_SLEEP */
+
 	return 0;
 }
 
Signature · 1 line
1.9.1
maintainer
#13 lore raw

Re: [PATCH v9 05/18] input: cyapa: add power management interfaces supported for the device

Quoted 64 lines · Dudley Du

Add suspend_scanrate_ms power management interfaces in device's power group, so users or applications can control the power management strategy of trackpad device as their requirements. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 112 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
drivers/input/mouse/cyapa.c +46 -0
index fac361a..420696d 100644
@@ -505,6 +505,96 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
 				   : (encoded_time - 5) * 20;
 }
 
+#ifdef CONFIG_PM_SLEEP
+static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
+					   struct device_attribute *attr,
+					   char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u8 pwr_cmd = cyapa->suspend_power_mode;
+	u16 sleep_time;
+	int len;
+	int ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	pwr_cmd = cyapa->suspend_power_mode;
+	sleep_time = cyapa->suspend_sleep_time;
+	mutex_unlock(&cyapa->state_sync_lock);
+
+	if (pwr_cmd == PWR_MODE_BTN_ONLY)
+		len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
+	else if (pwr_cmd == PWR_MODE_OFF)
+		len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
+	else {
+		if (cyapa->gen == CYAPA_GEN3)
+			sleep_time = cyapa_pwr_cmd_to_sleep_time(pwr_cmd);
+		len = scnprintf(buf, PAGE_SIZE, "%u\n", sleep_time);
+	}
+
+	return len;
+}
+
+static u16 cyapa_clamp_sleep_time(u16 sleep_time)
+{
+	if (sleep_time > 1000)
+		sleep_time = 1000;
+	return sleep_time;
+}
+
+static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
+					     struct device_attribute *attr,
+					     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u16 sleep_time;
+	int ret;
+

drivers/input/mouse/cyapa.c u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) · line 563

Quoted 10 lines
drivers/input/mouse/cyapa.c +10 -0
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (sysfs_streq(buf, BTN_ONLY_MODE_NAME))
+		cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
+	else if (sysfs_streq(buf, OFF_MODE_NAME))
+		cyapa->suspend_power_mode = PWR_MODE_OFF;
+	else if (!kstrtou16(buf, 10, &sleep_time)) {
+		cyapa->suspend_sleep_time = cyapa_clamp_sleep_time(sleep_time);

I'd simply do

		cyapa->suspend_sleep_time = max_t(u16, sleep_time, 1000);

here.

drivers/input/mouse/cyapa.c u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) · line 568

Quoted 5 lines
drivers/input/mouse/cyapa.c +5 -0
+		cyapa->suspend_power_mode =
+			cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
+	} else
+		count = 0;
+

If one branch uses curly braces then all branches shoudl use them, even if they contain only one statement.

Quoted 64 lines
drivers/input/mouse/cyapa.c +51 -0
+	mutex_unlock(&cyapa->state_sync_lock);
+	if (!count)
+		dev_err(dev, "invalid suspend scanrate ms parameters\n");
+	return count ? count : -EINVAL;
+}
+
+static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
+		   cyapa_show_suspend_scanrate,
+		   cyapa_update_suspend_scanrate);
+
+static struct attribute *cyapa_power_wakeup_entries[] = {
+	&dev_attr_suspend_scanrate_ms.attr,
+	NULL,
+};
+
+static const struct attribute_group cyapa_power_wakeup_group = {
+	.name = power_group_name,
+	.attrs = cyapa_power_wakeup_entries,
+};
+
+static void cyapa_remove_power_wakeup_group(void *data)
+{
+	struct cyapa *cyapa = data;
+
+	sysfs_unmerge_group(&cyapa->client->dev.kobj,
+				&cyapa_power_wakeup_group);
+}
+#endif /* CONFIG_PM_SLEEP */
+
 /*
  * Returns:
  *   0    Driver and device initialization successfully done.
@@ -588,6 +678,28 @@ static int cyapa_probe(struct i2c_client *client,
 		return ret;
 	}
 
+#ifdef CONFIG_PM_SLEEP
+	if (device_can_wakeup(dev)) {
+		ret = sysfs_merge_group(&client->dev.kobj,
+					&cyapa_power_wakeup_group);
+		if (ret) {
+			dev_err(dev, "failed to add power wakeup group, (%d)\n",
+				ret);
+			return ret;
+		}
+
+		ret = devm_add_action(dev,
+				cyapa_remove_power_wakeup_group, cyapa);
+		if (ret) {
+			cyapa_remove_power_wakeup_group(cyapa);
+			dev_err(dev,
+				"failed to add power cleanup action, (%d)\n",
+				ret);
+			return ret;
+		}
+	}
+#endif /* CONFIG_PM_SLEEP */
+
 	return 0;
 }
 
Signature · 1 line
1.9.1
Signature · 1 line
Dmitry
Applied
#14 lore raw

RE: [PATCH v9 05/18] input: cyapa: add power management interfaces supported for the device

Thanks, Dmity

Quoted 88 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:38
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 05/18] input: cyapa: add power management interfaces supported for the device

Quoted 8 lines · Dudley Du

Add suspend_scanrate_ms power management interfaces in device's power group, so users or applications can control the power management strategy of trackpad device as their requirements. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 112

++++++++++++++++++++++++++++++++++++++++++++

Quoted 56 lines
 1 file changed, 112 insertions(+)
drivers/input/mouse/cyapa.c +46 -0
index fac361a..420696d 100644
@@ -505,6 +505,96 @@ u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode)
    : (encoded_time - 5) * 20;
 }
 
+#ifdef CONFIG_PM_SLEEP
+static ssize_t cyapa_show_suspend_scanrate(struct device *dev,
+   struct device_attribute *attr,
+   char *buf)
+{
+struct cyapa *cyapa = dev_get_drvdata(dev);
+u8 pwr_cmd = cyapa->suspend_power_mode;
+u16 sleep_time;
+int len;
+int ret;
+
+ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+if (ret)
+return ret;
+pwr_cmd = cyapa->suspend_power_mode;
+sleep_time = cyapa->suspend_sleep_time;
+mutex_unlock(&cyapa->state_sync_lock);
+
+if (pwr_cmd == PWR_MODE_BTN_ONLY)
+len = scnprintf(buf, PAGE_SIZE, "%s\n", BTN_ONLY_MODE_NAME);
+else if (pwr_cmd == PWR_MODE_OFF)
+len = scnprintf(buf, PAGE_SIZE, "%s\n", OFF_MODE_NAME);
+else {
+if (cyapa->gen == CYAPA_GEN3)
+sleep_time = cyapa_pwr_cmd_to_sleep_time(pwr_cmd);
+len = scnprintf(buf, PAGE_SIZE, "%u\n", sleep_time);
+}
+
+return len;
+}
+
+static u16 cyapa_clamp_sleep_time(u16 sleep_time)
+{
+if (sleep_time > 1000)
+sleep_time = 1000;
+return sleep_time;
+}
+
+static ssize_t cyapa_update_suspend_scanrate(struct device *dev,
+     struct device_attribute *attr,
+     const char *buf, size_t count)
+{
+struct cyapa *cyapa = dev_get_drvdata(dev);
+u16 sleep_time;
+int ret;
+

drivers/input/mouse/cyapa.c u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) · line 563

Quoted 10 lines
drivers/input/mouse/cyapa.c +10 -0
+ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+if (ret)
+return ret;
+
+if (sysfs_streq(buf, BTN_ONLY_MODE_NAME))
+cyapa->suspend_power_mode = PWR_MODE_BTN_ONLY;
+else if (sysfs_streq(buf, OFF_MODE_NAME))
+cyapa->suspend_power_mode = PWR_MODE_OFF;
+else if (!kstrtou16(buf, 10, &sleep_time)) {
+cyapa->suspend_sleep_time = cyapa_clamp_sleep_time(sleep_time);

I'd simply do

Quoted 3 lines

cyapa->suspend_sleep_time = max_t(u16, sleep_time, 1000);

here.

Thanks, applied.

Quoted 9 lines

drivers/input/mouse/cyapa.c u16 cyapa_pwr_cmd_to_sleep_time(u8 pwr_mode) · line 568

Quoted 6 lines
drivers/input/mouse/cyapa.c +5 -0
+cyapa->suspend_power_mode =
+cyapa_sleep_time_to_pwr_cmd(cyapa->suspend_sleep_time);
+} else
+count = 0;
+

If one branch uses curly braces then all branches shoudl use them, even if they contain only one statement.

Thanks, I will go through all code to follow this rule.

Quoted 62 lines
Quoted 62 lines
drivers/input/mouse/cyapa.c +51 -0
+mutex_unlock(&cyapa->state_sync_lock);
+if (!count)
+dev_err(dev, "invalid suspend scanrate ms parameters\n");
+return count ? count : -EINVAL;
+}
+
+static DEVICE_ATTR(suspend_scanrate_ms, S_IRUGO|S_IWUSR,
+   cyapa_show_suspend_scanrate,
+   cyapa_update_suspend_scanrate);
+
+static struct attribute *cyapa_power_wakeup_entries[] = {
+&dev_attr_suspend_scanrate_ms.attr,
+NULL,
+};
+
+static const struct attribute_group cyapa_power_wakeup_group = {
+.name = power_group_name,
+.attrs = cyapa_power_wakeup_entries,
+};
+
+static void cyapa_remove_power_wakeup_group(void *data)
+{
+struct cyapa *cyapa = data;
+
+sysfs_unmerge_group(&cyapa->client->dev.kobj,
+&cyapa_power_wakeup_group);
+}
+#endif /* CONFIG_PM_SLEEP */
+
 /*
  * Returns:
  *   0    Driver and device initialization successfully done.
@@ -588,6 +678,28 @@ static int cyapa_probe(struct i2c_client *client,
 return ret;
 }
 
+#ifdef CONFIG_PM_SLEEP
+if (device_can_wakeup(dev)) {
+ret = sysfs_merge_group(&client->dev.kobj,
+&cyapa_power_wakeup_group);
+if (ret) {
+dev_err(dev, "failed to add power wakeup group, (%d)\n",
+ret);
+return ret;
+}
+
+ret = devm_add_action(dev,
+cyapa_remove_power_wakeup_group, cyapa);
+if (ret) {
+cyapa_remove_power_wakeup_group(cyapa);
+dev_err(dev,
+"failed to add power cleanup action, (%d)\n",
+ret);
+return ret;
+}
+}
+#endif /* CONFIG_PM_SLEEP */
+
 return 0;
 }
 
Quoted 6 lines
Quoted 3 lines

--
1.9.1

--
Dmitry

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

author
#15 lore raw

PATCH v9 6/18 input: cyapa: add runtime power management interfaces supported for the device

In reply to Dudley Du

Add runtime_suspend_scanrate_ms power management interfaces in device's power group, so users or applications can control the runtime power management strategy of trackpad device as their requirements. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 168 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
drivers/input/mouse/cyapa.c +168 -0
index 420696d..d4b7c42 100644
@@ -23,6 +23,7 @@
 #include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
+#include <linux/pm_runtime.h>
 #include "cyapa.h"
 
 
@@ -259,6 +260,7 @@ static irqreturn_t cyapa_irq(int irq, void *dev_id)
 	bool cont;
 	int ret;
 
+	pm_runtime_get_sync(dev);
 	if (device_may_wakeup(dev))
 		pm_wakeup_event(dev, 0);
 
@@ -282,6 +284,8 @@ static irqreturn_t cyapa_irq(int irq, void *dev_id)
 	}
 
 out:
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_sync_autosuspend(dev);
 	return IRQ_HANDLED;
 }
 
@@ -595,6 +599,116 @@ static void cyapa_remove_power_wakeup_group(void *data)
 }
 #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_PM_RUNTIME
+static ssize_t cyapa_show_rt_suspend_scanrate(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u8 pwr_cmd;
+	u16 sleep_time;
+	int ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	pwr_cmd = cyapa->runtime_suspend_power_mode;
+	sleep_time = cyapa->runtime_suspend_sleep_time;
+	mutex_unlock(&cyapa->state_sync_lock);
+
+	if (cyapa->gen == CYAPA_GEN3)
+		return scnprintf(buf, PAGE_SIZE, "%u\n",
+			cyapa_pwr_cmd_to_sleep_time(pwr_cmd));
+	return scnprintf(buf, PAGE_SIZE, "%u\n", sleep_time);
+}
+
+static ssize_t cyapa_update_rt_suspend_scanrate(struct device *dev,
+						struct device_attribute *attr,
+						const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	u16 time;
+	int ret;
+
+	if (buf == NULL || count == 0 || kstrtou16(buf, 10, &time)) {
+		dev_err(dev, "invalid runtime suspend scanrate ms parameter\n");
+		return -EINVAL;
+	}
+
+	/*
+	 * When the suspend scanrate is changed, pm_runtime_get to resume
+	 * a potentially suspended device, update to the new pwr_cmd
+	 * and then pm_runtime_put to suspend into the new power mode.
+	 */
+	pm_runtime_get_sync(dev);
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	cyapa->runtime_suspend_sleep_time = cyapa_clamp_sleep_time(time);
+	cyapa->runtime_suspend_power_mode =
+		cyapa_sleep_time_to_pwr_cmd(cyapa->runtime_suspend_sleep_time);
+	mutex_unlock(&cyapa->state_sync_lock);
+	pm_runtime_put_sync_autosuspend(dev);
+
+	return count;
+}
+
+static DEVICE_ATTR(runtime_suspend_scanrate_ms, S_IRUGO|S_IWUSR,
+		   cyapa_show_rt_suspend_scanrate,
+		   cyapa_update_rt_suspend_scanrate);
+
+static struct attribute *cyapa_power_runtime_entries[] = {
+	&dev_attr_runtime_suspend_scanrate_ms.attr,
+	NULL,
+};
+
+static const struct attribute_group cyapa_power_runtime_group = {
+	.name = power_group_name,
+	.attrs = cyapa_power_runtime_entries,
+};
+
+static void cyapa_remove_power_runtime_group(void *data)
+{
+	struct cyapa *cyapa = data;
+
+	sysfs_unmerge_group(&cyapa->client->dev.kobj,
+				&cyapa_power_runtime_group);
+}
+
+static int cyapa_start_runtime(struct cyapa *cyapa)
+{
+	int ret;
+	struct device *dev = &cyapa->client->dev;
+
+	cyapa->runtime_suspend_power_mode = PWR_MODE_IDLE;
+	cyapa->runtime_suspend_sleep_time =
+		cyapa_pwr_cmd_to_sleep_time(cyapa->runtime_suspend_power_mode);
+
+	ret = sysfs_merge_group(&dev->kobj, &cyapa_power_runtime_group);
+	if (ret) {
+		dev_err(dev,
+			"failed to create power runtime group, %d\n", ret);
+		return ret;
+	}
+
+	ret = devm_add_action(dev, cyapa_remove_power_runtime_group, cyapa);
+	if (ret) {
+		cyapa_remove_power_runtime_group(cyapa);
+		dev_err(dev,
+			"failed to add power runtime cleanup action, (%d)\n",
+			ret);
+		return ret;
+	}
+
+	pm_runtime_set_active(dev);
+	pm_runtime_use_autosuspend(dev);
+	pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_DELAY);
+	pm_runtime_enable(dev);
+
+	return 0;
+}
+#endif /* CONFIG_PM_RUNTIME */
+
 /*
  * Returns:
  *   0    Driver and device initialization successfully done.
@@ -700,6 +814,14 @@ static int cyapa_probe(struct i2c_client *client,
 	}
 #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_PM_RUNTIME
+	ret = cyapa_start_runtime(cyapa);
+	if (ret) {
+		dev_err(dev, "failed to start pm_runtime, (%d)\n, ", ret);
+		return ret;
+	}
+#endif /* CONFIG_PM_RUNTIME */
+
 	return 0;
 }
 
@@ -712,6 +834,8 @@ static int cyapa_remove(struct i2c_client *client)
 	if (cyapa->ops->set_power_mode)
 		cyapa->ops->set_power_mode(cyapa, PWR_MODE_OFF, 0);
 
+	pm_runtime_disable(&client->dev);
+
 	return 0;
 }
 
@@ -728,6 +852,7 @@ static int cyapa_suspend(struct device *dev)
 		return ret;
 	}
 
+	pm_runtime_disable(dev);
 	/*
 	 * Disable IRQ to avoid the command response interrupt cause system
 	 * suspending process interrupted and failed.
@@ -750,6 +875,8 @@ static int cyapa_suspend(struct device *dev)
 					ret);
 	}
 
+	pm_runtime_set_suspended(dev);
+
 	if (device_may_wakeup(dev))
 		cyapa->irq_wake = (enable_irq_wake(cyapa->client->irq) == 0);
 
@@ -771,19 +898,60 @@ static int cyapa_resume(struct device *dev)
 		cyapa->irq_wake = false;
 	}
 
+	/* Runtime set active to reflect active state. */
+	pm_runtime_set_active(dev);
+
 	/* Reset to active power state after re-detected. */
 	cyapa_detect(cyapa);
 
 	enable_irq(cyapa->client->irq);
 
+	pm_runtime_enable(dev);
+
 	if (!ret)
 		mutex_unlock(&cyapa->state_sync_lock);
 	return 0;
 }
 #endif /* CONFIG_PM_SLEEP */
 
+#ifdef CONFIG_PM_RUNTIME
+static int cyapa_runtime_suspend(struct device *dev)
+{
+	int ret;
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+
+	if (cyapa->input && cyapa->ops->set_power_mode) {
+		/* Set trackpad device to idle mode. */
+		ret = cyapa->ops->set_power_mode(cyapa,
+				cyapa->runtime_suspend_power_mode,
+				cyapa->runtime_suspend_sleep_time);
+		if (ret)
+			dev_err(dev, "runtime suspend failed, %d\n", ret);
+	}
+
+	return 0;
+}
+
+static int cyapa_runtime_resume(struct device *dev)
+{
+	int ret;
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+
+	if (cyapa->input && cyapa->ops->set_power_mode) {
+		/* Resume to full active mode. */
+		ret = cyapa->ops->set_power_mode(cyapa,
+				PWR_MODE_FULL_ACTIVE, 0);
+		if (ret)
+			dev_err(dev, "runtime resume failed, %d\n", ret);
+	}
+
+	return 0;
+}
+#endif /* CONFIG_PM_RUNTIME */
+
 static const struct dev_pm_ops cyapa_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(cyapa_suspend, cyapa_resume)
+	SET_RUNTIME_PM_OPS(cyapa_runtime_suspend, cyapa_runtime_resume, NULL)
 };
 
 static const struct i2c_device_id cyapa_id_table[] = {
Signature · 1 line
1.9.1
author
#16 lore raw

PATCH v9 7/18 input: cyapa: add sysfs interfaces supported in the cyapa driver

In reply to Dudley Du

Add device's basic control and features supported in cyapa driver through sysfs file system interfaces. These interfaces are commonly used in pre- and after production, for trackpad device state checking, managing and firmware image updating.
These interfaces including firmware_version and product_id interfaces for reading firmware version and trackpad device product id values, and including update_fw interface to command firmware image update process. Also including baseline and calibrate interfaces for reading and checking trackpad device's sensors states. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 220 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 220 insertions(+)
drivers/input/mouse/cyapa.c +220 -0
index d4b7c42..5f384ff 100644
@@ -709,6 +709,213 @@ static int cyapa_start_runtime(struct cyapa *cyapa)
 }
 #endif /* CONFIG_PM_RUNTIME */
 
+static ssize_t cyapa_show_fm_ver(struct device *dev,
+				 struct device_attribute *attr, char *buf)
+{
+	int ret;
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	ret = scnprintf(buf, PAGE_SIZE, "%d.%d\n", cyapa->fw_maj_ver,
+			 cyapa->fw_min_ver);
+	mutex_unlock(&cyapa->state_sync_lock);
+	return ret;
+}
+
+static ssize_t cyapa_show_product_id(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	int ret;
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+	ret = scnprintf(buf, PAGE_SIZE, "%s\n", cyapa->product_id);
+	mutex_unlock(&cyapa->state_sync_lock);
+	return ret;
+}
+
+static int cyapa_firmware(struct cyapa *cyapa, const char *fw_name)
+{
+	struct device *dev = &cyapa->client->dev;
+	int ret;
+	const struct firmware *fw;
+
+	ret = request_firmware(&fw, fw_name, dev);
+	if (ret) {
+		dev_err(dev, "Could not load firmware from %s, %d\n",
+			fw_name, ret);
+		return ret;
+	}
+
+	if (cyapa->ops->check_fw) {
+		ret = cyapa->ops->check_fw(cyapa, fw);
+		if (ret) {
+			dev_err(dev, "Invalid CYAPA firmware image: %s\n",
+					fw_name);
+			goto done;
+		}
+	} else {
+		dev_err(dev, "No valid device ops->check_fw handler set.\n");
+		ret = -ENODEV;
+		goto done;
+	}
+
+	/*
+	 * Resume the potentially suspended device because doing FW
+	 * update on a device not in the FULL mode has a chance to
+	 * fail.
+	 */
+	pm_runtime_get_sync(dev);
+
+	if (cyapa->ops->bl_enter) {
+		ret = cyapa->ops->bl_enter(cyapa);
+		if (ret)
+			goto err_detect;
+	}
+
+	if (cyapa->ops->bl_activate) {
+		ret = cyapa->ops->bl_activate(cyapa);
+		if (ret)
+			goto err_detect;
+	}
+
+	if (cyapa->ops->bl_initiate) {
+		ret = cyapa->ops->bl_initiate(cyapa, fw);
+		if (ret)
+			goto err_detect;
+	}
+
+	if (cyapa->ops->update_fw) {
+		ret = cyapa->ops->update_fw(cyapa, fw);
+		if (ret)
+			goto err_detect;
+	}
+
+	if (cyapa->ops->bl_verify_app_integrity) {
+		ret = cyapa->ops->bl_verify_app_integrity(cyapa);
+		if (ret)
+			goto err_detect;
+	}
+
+err_detect:
+	pm_runtime_put_noidle(dev);
+
+done:
+	release_firmware(fw);
+	return ret;
+}
+
+static ssize_t cyapa_update_fw_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	char fw_name[64];
+	int ret;
+
+	if (count > 64) {
+		dev_err(dev, "File name too long\n");
+		return -EINVAL;
+	}
+
+	memcpy(fw_name, buf, count);
+	if (fw_name[count - 1] == '\n')
+		fw_name[count - 1] = '\0';
+	else
+		fw_name[count] = '\0';
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	ret = cyapa_firmware(cyapa, fw_name);
+	if (ret)
+		dev_err(dev, "firmware update failed, %d\n", ret);
+	else
+		dev_dbg(dev, "firmware update successfully done.\n");
+
+	/*
+	 * Redetect trackpad device states because firmware update process
+	 * will reset trackpad device into bootloader mode.
+	 */
+	cyapa_detect(cyapa);
+
+	mutex_unlock(&cyapa->state_sync_lock);
+
+	return ret ? ret : count;
+}
+
+static ssize_t cyapa_calibrate_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (!cyapa->ops->calibrate_store) {
+		dev_err(dev, "Calibrate operation not supported.\n");
+		ret = -ENOTSUPP;
+	} else
+		ret = cyapa->ops->calibrate_store(dev, attr, buf, count);
+
+	mutex_unlock(&cyapa->state_sync_lock);
+	return ret < 0 ? ret : count;
+}
+
+static ssize_t cyapa_show_baseline(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	ssize_t ret;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (!cyapa->ops->show_baseline) {
+		dev_err(dev, "Calibrate operation not supported.\n");
+		ret = -ENOTSUPP;
+	} else
+		ret = cyapa->ops->show_baseline(dev, attr, buf);
+
+	mutex_unlock(&cyapa->state_sync_lock);
+	return ret;
+}
+
+static DEVICE_ATTR(firmware_version, S_IRUGO, cyapa_show_fm_ver, NULL);
+static DEVICE_ATTR(product_id, S_IRUGO, cyapa_show_product_id, NULL);
+static DEVICE_ATTR(update_fw, S_IWUSR, NULL, cyapa_update_fw_store);
+static DEVICE_ATTR(baseline, S_IRUGO, cyapa_show_baseline, NULL);
+static DEVICE_ATTR(calibrate, S_IWUSR, NULL, cyapa_calibrate_store);
+
+static struct attribute *cyapa_sysfs_entries[] = {
+	&dev_attr_firmware_version.attr,
+	&dev_attr_product_id.attr,
+	&dev_attr_update_fw.attr,
+	&dev_attr_baseline.attr,
+	&dev_attr_calibrate.attr,
+	NULL,
+};
+
+static const struct attribute_group cyapa_sysfs_group = {
+	.attrs = cyapa_sysfs_entries,
+};
+
+static void cyapa_remove_sysfs_group(void *data)
+{
+	struct cyapa *cyapa = data;
+
+	sysfs_remove_group(&cyapa->client->dev.kobj, &cyapa_sysfs_group);
+}
+
 /*
  * Returns:
  *   0    Driver and device initialization successfully done.
@@ -792,6 +999,19 @@ static int cyapa_probe(struct i2c_client *client,
 		return ret;
 	}
 
+	ret = sysfs_create_group(&client->dev.kobj, &cyapa_sysfs_group);
+	if (ret) {
+		dev_err(dev, "failed to create sysfs entries, (%d)\n", ret);
+		return ret;
+	}
+
+	ret = devm_add_action(dev, cyapa_remove_sysfs_group, cyapa);
+	if (ret) {
+		cyapa_remove_sysfs_group(cyapa);
+		dev_err(dev, "failed to add sysfs cleanup action, (%d)\n", ret);
+		return ret;
+	}
+
 #ifdef CONFIG_PM_SLEEP
 	if (device_can_wakeup(dev)) {
 		ret = sysfs_merge_group(&client->dev.kobj,
Signature · 1 line
1.9.1
author
#17 lore raw

PATCH v9 8/18 input: cyapa: add gen3 trackpad device firmware update function support

In reply to Dudley Du

Add firmware image update function supported for gen3 trackpad device, it can be used through sysfs update_fw interface.
TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen3.c | 290 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 290 insertions(+)
drivers/input/mouse/cyapa_gen3.c +290 -0
index bd00c6e..7caa93f 100644
@@ -412,6 +412,78 @@ static int cyapa_gen3_state_parse(struct cyapa *cyapa, u8 *reg_data, int len)
 	return -EAGAIN;
 }
 
+/*
+ * Enter bootloader by soft resetting the device.
+ *
+ * If device is already in the bootloader, the function just returns.
+ * Otherwise, reset the device; after reset, device enters bootloader idle
+ * state immediately.
+ *
+ * Also, if device was unregister device from input core.  Device will
+ * re-register after it is detected following resumption of operational mode.
+ *
+ * Returns:
+ *   0 on success
+ *   -EAGAIN  device was reset, but is not now in bootloader idle state
+ *   < 0 if the device never responds within the timeout
+ */
+static int cyapa_gen3_bl_enter(struct cyapa *cyapa)
+{
+	int ret;
+
+	if (cyapa->input) {
+		data_reporting_started = false;
+		input_unregister_device(cyapa->input);
+		cyapa->input = NULL;
+	}
+
+	ret = cyapa_poll_state(cyapa, 500);
+	if (ret < 0)
+		return ret;
+	if (cyapa->state == CYAPA_STATE_BL_IDLE) {
+		/* Already in BL_IDLE. Skipping reset. */
+		return 0;
+	}
+
+	if (cyapa->state != CYAPA_STATE_OP)
+		return -EAGAIN;
+
+	cyapa->state = CYAPA_STATE_NO_DEVICE;
+	ret = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET, 0x01);
+	if (ret < 0)
+		return -EIO;
+
+	usleep_range(25000, 50000);
+	ret = cyapa_poll_state(cyapa, 500);
+	if (ret < 0)
+		return ret;
+	if ((cyapa->state != CYAPA_STATE_BL_IDLE) ||
+		(cyapa->status[REG_BL_STATUS] & BL_STATUS_WATCHDOG))
+		return -EAGAIN;
+
+	return 0;
+}
+
+static int cyapa_gen3_bl_activate(struct cyapa *cyapa)
+{
+	int ret;
+
+	ret = cyapa_i2c_reg_write_block(cyapa, 0, sizeof(bl_activate),
+					bl_activate);
+	if (ret < 0)
+		return ret;
+
+	/* Wait for bootloader to activate; takes between 2 and 12 seconds */
+	msleep(2000);
+	ret = cyapa_poll_state(cyapa, 11000);
+	if (ret < 0)
+		return ret;
+	if (cyapa->state != CYAPA_STATE_BL_ACTIVE)
+		return -EAGAIN;
+
+	return 0;
+}
+
 static int cyapa_gen3_bl_deactivate(struct cyapa *cyapa)
 {
 	int ret;
@@ -472,6 +544,218 @@ static int cyapa_gen3_bl_exit(struct cyapa *cyapa)
 	return 0;
 }
 
+/* Used in gen3 bootloader commands. */
+static u16 cyapa_gen3_csum(const u8 *buf, size_t count)
+{
+	int i;
+	u16 csum = 0;
+
+	for (i = 0; i < count; i++)
+		csum += buf[i];
+
+	return csum;
+}
+
+/*
+ * Verify the integrity of a CYAPA firmware image file.
+ *
+ * The firmware image file is 30848 bytes, composed of 482 64-byte blocks.
+ *
+ * The first 2 blocks are the firmware header.
+ * The next 480 blocks are the firmware image.
+ *
+ * The first two bytes of the header hold the header checksum, computed by
+ * summing the other 126 bytes of the header.
+ * The last two bytes of the header hold the firmware image checksum, computed
+ * by summing the 30720 bytes of the image modulo 0xffff.
+ *
+ * Both checksums are stored little-endian.
+ */
+static int cyapa_gen3_check_fw(struct cyapa *cyapa, const struct firmware *fw)
+{
+	struct device *dev = &cyapa->client->dev;
+	u16 csum;
+	u16 csum_expected;
+
+	/* Firmware must match exact 30848 bytes = 482 64-byte blocks. */
+	if (fw->size != CYAPA_FW_SIZE) {
+		dev_err(dev, "invalid firmware size = %zu, expected %u.\n",
+			fw->size, CYAPA_FW_SIZE);
+		return -EINVAL;
+	}
+
+	/* Verify header block */
+	csum_expected = (fw->data[0] << 8) | fw->data[1];
+	csum = cyapa_gen3_csum(&fw->data[2], CYAPA_FW_HDR_SIZE - 2);
+	if (csum != csum_expected) {
+		dev_err(dev, "%s %04x, expected: %04x\n",
+			"invalid firmware header checksum = ",
+			csum, csum_expected);
+		return -EINVAL;
+	}
+
+	/* Verify firmware image */
+	csum_expected = (fw->data[CYAPA_FW_HDR_SIZE - 2] << 8) |
+			 fw->data[CYAPA_FW_HDR_SIZE - 1];
+	csum = cyapa_gen3_csum(&fw->data[CYAPA_FW_HDR_SIZE],
+			CYAPA_FW_DATA_SIZE);
+	if (csum != csum_expected) {
+		dev_err(dev, "%s %04x, expected: %04x\n",
+			"invalid firmware header checksum = ",
+			csum, csum_expected);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+/*
+ * Write a |len| byte long buffer |buf| to the device, by chopping it up into a
+ * sequence of smaller |CYAPA_CMD_LEN|-length write commands.
+ *
+ * The data bytes for a write command are prepended with the 1-byte offset
+ * of the data relative to the start of |buf|.
+ */
+static int cyapa_gen3_write_buffer(struct cyapa *cyapa,
+		const u8 *buf, size_t len)
+{
+	int ret;
+	size_t i;
+	unsigned char cmd[CYAPA_CMD_LEN + 1];
+	size_t cmd_len;
+
+	for (i = 0; i < len; i += CYAPA_CMD_LEN) {
+		const u8 *payload = &buf[i];
+
+		cmd_len = (len - i >= CYAPA_CMD_LEN) ? CYAPA_CMD_LEN : len - i;
+		cmd[0] = i;
+		memcpy(&cmd[1], payload, cmd_len);
+
+		ret = cyapa_i2c_reg_write_block(cyapa, 0, cmd_len + 1, cmd);
+		if (ret < 0)
+			return ret;
+	}
+	return 0;
+}
+
+/*
+ * A firmware block write command writes 64 bytes of data to a single flash
+ * page in the device.  The 78-byte block write command has the format:
+ *   <0xff> <CMD> <Key> <Start> <Data> <Data-Checksum> <CMD Checksum>
+ *
+ *  <0xff>  - every command starts with 0xff
+ *  <CMD>   - the write command value is 0x39
+ *  <Key>   - write commands include an 8-byte key: { 00 01 02 03 04 05 06 07 }
+ *  <Block> - Memory Block number (address / 64) (16-bit, big-endian)
+ *  <Data>  - 64 bytes of firmware image data
+ *  <Data Checksum> - sum of 64 <Data> bytes, modulo 0xff
+ *  <CMD Checksum> - sum of 77 bytes, from 0xff to <Data Checksum>
+ *
+ * Each write command is split into 5 i2c write transactions of up to 16 bytes.
+ * Each transaction starts with an i2c register offset: (00, 10, 20, 30, 40).
+ */
+static int cyapa_gen3_write_fw_block(struct cyapa *cyapa,
+		u16 block, const u8 *data)
+{
+	int ret;
+	u8 cmd[78];
+	u8 status[BL_STATUS_SIZE];
+	/* Programming for one block can take about 100ms. */
+	int tries = 11;
+	u8 bl_status, bl_error;
+
+	/* Set write command and security key bytes. */
+	cmd[0] = 0xff;
+	cmd[1] = 0x39;
+	cmd[2] = 0x00;
+	cmd[3] = 0x01;
+	cmd[4] = 0x02;
+	cmd[5] = 0x03;
+	cmd[6] = 0x04;
+	cmd[7] = 0x05;
+	cmd[8] = 0x06;
+	cmd[9] = 0x07;
+	cmd[10] = block >> 8;
+	cmd[11] = block;
+	memcpy(&cmd[12], data, CYAPA_FW_BLOCK_SIZE);
+	cmd[76] = cyapa_gen3_csum(data, CYAPA_FW_BLOCK_SIZE);
+	cmd[77] = cyapa_gen3_csum(cmd, sizeof(cmd) - 1);
+
+	ret = cyapa_gen3_write_buffer(cyapa, cmd, sizeof(cmd));
+	if (ret)
+		return ret;
+
+	/* Wait for write to finish */
+	do {
+		usleep_range(10000, 20000);
+
+		/* Check block write command result status. */
+		ret = cyapa_i2c_reg_read_block(cyapa, BL_HEAD_OFFSET,
+					       BL_STATUS_SIZE, status);
+		if (ret != BL_STATUS_SIZE)
+			return (ret < 0) ? ret : -EIO;
+	} while ((status[REG_BL_STATUS] & BL_STATUS_BUSY) && --tries);
+
+	/* Ignore WATCHDOG bit and reserved bits. */
+	bl_status = status[REG_BL_STATUS] & ~BL_STATUS_REV_MASK;
+	bl_error = status[REG_BL_ERROR] & ~BL_ERROR_RESERVED;
+
+	if (bl_status & BL_STATUS_BUSY)
+		ret = -ETIMEDOUT;
+	else if (bl_status != BL_STATUS_RUNNING ||
+		bl_error != BL_ERROR_BOOTLOADING)
+		ret = -EIO;
+	else
+		ret = 0;
+
+	return ret;
+}
+
+static int cyapa_gen3_write_blocks(struct cyapa *cyapa,
+		size_t start_block, size_t block_count,
+		const u8 *image_data)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i < block_count; i++) {
+		size_t block = start_block + i;
+		size_t addr = i * CYAPA_FW_BLOCK_SIZE;
+		const u8 *data = &image_data[addr];
+
+		ret = cyapa_gen3_write_fw_block(cyapa, block, data);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+static int cyapa_gen3_do_fw_update(struct cyapa *cyapa,
+		const struct firmware *fw)
+{
+	struct device *dev = &cyapa->client->dev;
+	int ret;
+
+	/* First write data, starting at byte 128  of fw->data */
+	ret = cyapa_gen3_write_blocks(cyapa,
+		CYAPA_FW_DATA_BLOCK_START, CYAPA_FW_DATA_BLOCK_COUNT,
+		&fw->data[CYAPA_FW_HDR_BLOCK_COUNT * CYAPA_FW_BLOCK_SIZE]);
+	if (ret) {
+		dev_err(dev, "FW update aborted, write image, %d\n", ret);
+		return ret;
+	}
+
+	/* Then write checksum */
+	ret = cyapa_gen3_write_blocks(cyapa,
+		CYAPA_FW_HDR_BLOCK_START, CYAPA_FW_HDR_BLOCK_COUNT,
+		&fw->data[0]);
+	if (ret) {
+		dev_err(dev, "FW update aborted, write checksum, %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 /*
  * cyapa_get_wait_time_for_pwr_cmd
  *
@@ -778,6 +1062,12 @@ static int cyapa_gen3_irq_handler(struct cyapa *cyapa)
 }
 
 const struct cyapa_dev_ops cyapa_gen3_ops = {
+	.check_fw = cyapa_gen3_check_fw,
+	.bl_enter = cyapa_gen3_bl_enter,
+	.bl_activate = cyapa_gen3_bl_activate,
+	.update_fw = cyapa_gen3_do_fw_update,
+	.bl_deactivate = cyapa_gen3_bl_deactivate,
+
 	.state_parse = cyapa_gen3_state_parse,
 	.operational_check = cyapa_gen3_do_operational_check,
 
Signature · 1 line
1.9.1
author
#18 lore raw

PATCH v9 9/18 input: cyapa: add gen3 trackpad device read baseline function support

In reply to Dudley Du

Add read baseline function supported for gen3 trackpad device, it can be used through sysfs baseline interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen3.c | 74 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
drivers/input/mouse/cyapa_gen3.c +74 -0
index 7caa93f..406b5db 100644
@@ -756,6 +756,78 @@ static int cyapa_gen3_do_fw_update(struct cyapa *cyapa,
 	return 0;
 }
 
+static ssize_t cyapa_gen3_show_baseline(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int max_baseline, min_baseline;
+	int tries = 3;
+	int ret;
+
+	data_reporting_started = false;
+
+	ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
+	if (ret < 0) {
+		dev_err(dev, "Error reading dev status. err = %d\n", ret);
+		goto out;
+	}
+	if ((ret & CYAPA_DEV_NORMAL) != CYAPA_DEV_NORMAL) {
+		dev_warn(dev, "Trackpad device is busy. device state = 0x%x\n",
+			 ret);
+		ret = -EAGAIN;
+		goto out;
+	}
+
+	ret = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET,
+			       OP_REPORT_BASELINE_MASK);
+	if (ret < 0) {
+		dev_err(dev, "Failed to send report baseline command. %d\n",
+			ret);
+		goto out;
+	}
+
+	do {
+		usleep_range(10000, 20000);
+
+		ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
+		if (ret < 0) {
+			dev_err(dev, "Error reading dev status. err = %d\n",
+				ret);
+			goto out;
+		}
+		if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL)
+			break;
+	} while (--tries);
+
+	if (tries == 0) {
+		dev_err(dev, "Device timed out going to Normal state.\n");
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+
+	ret = cyapa_read_byte(cyapa, CYAPA_CMD_MAX_BASELINE);
+	if (ret < 0) {
+		dev_err(dev, "Failed to read max baseline. err = %d\n", ret);
+		goto out;
+	}
+	max_baseline = ret;
+
+	ret = cyapa_read_byte(cyapa, CYAPA_CMD_MIN_BASELINE);
+	if (ret < 0) {
+		dev_err(dev, "Failed to read min baseline. err = %d\n", ret);
+		goto out;
+	}
+	min_baseline = ret;
+
+	dev_dbg(dev, "Baseline report successful. Max: %d Min: %d\n",
+		max_baseline, min_baseline);
+	ret = scnprintf(buf, PAGE_SIZE, "%d %d\n", max_baseline, min_baseline);
+
+out:
+	data_reporting_started = true;
+	return ret;
+}
+
 /*
  * cyapa_get_wait_time_for_pwr_cmd
  *
@@ -1068,6 +1140,8 @@ const struct cyapa_dev_ops cyapa_gen3_ops = {
 	.update_fw = cyapa_gen3_do_fw_update,
 	.bl_deactivate = cyapa_gen3_bl_deactivate,
 
+	.show_baseline = cyapa_gen3_show_baseline,
+
 	.state_parse = cyapa_gen3_state_parse,
 	.operational_check = cyapa_gen3_do_operational_check,
 
Signature · 1 line
1.9.1
author
#19 lore raw

PATCH v9 10/18 input: cyapa: add gen3 trackpad device force re-calibrate function support

In reply to Dudley Du

Add force re-calibrate function supported for gen3 trackpad device, it can be used through sysfs calibrate interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen3.c | 61 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
drivers/input/mouse/cyapa_gen3.c +61 -0
index 406b5db..b477d67 100644
@@ -756,6 +756,66 @@ static int cyapa_gen3_do_fw_update(struct cyapa *cyapa,
 	return 0;
 }
 
+static ssize_t cyapa_gen3_do_calibrate(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int tries = 20;  /* max recalibration timeout 2s. */
+	int ret;
+
+	data_reporting_started = false;
+
+	ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
+	if (ret < 0) {
+		dev_err(dev, "Error reading dev status. err = %d\n", ret);
+		goto out;
+	}
+	if ((ret & CYAPA_DEV_NORMAL) != CYAPA_DEV_NORMAL) {
+		dev_warn(dev, "Trackpad device is busy. device state = 0x%x\n",
+			 ret);
+		ret = -EAGAIN;
+		goto out;
+	}
+
+	ret = cyapa_write_byte(cyapa, CYAPA_CMD_SOFT_RESET,
+			       OP_RECALIBRATION_MASK);
+	if (ret < 0) {
+		dev_err(dev, "Failed to send calibrate command. ret = %d\n",
+			ret);
+		goto out;
+	}
+
+	do {
+		/*
+		 * For this recalibration, the max time will not exceed 2s.
+		 * The average time is approximately 500 - 700 ms, and we
+		 * will check the status every 100 - 200ms.
+		 */
+		usleep_range(100000, 200000);
+
+		ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS);
+		if (ret < 0) {
+			dev_err(dev, "Error reading dev status. err = %d\n",
+				ret);
+			goto out;
+		}
+		if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL)
+			break;
+	} while (--tries);
+
+	if (tries == 0) {
+		dev_err(dev, "Failed to calibrate. Timeout.\n");
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+	dev_dbg(dev, "Calibration successful.\n");
+
+out:
+	data_reporting_started = true;
+	return ret < 0 ? ret : count;
+}
+
 static ssize_t cyapa_gen3_show_baseline(struct device *dev,
 				   struct device_attribute *attr, char *buf)
 {
@@ -1141,6 +1201,7 @@ const struct cyapa_dev_ops cyapa_gen3_ops = {
 	.bl_deactivate = cyapa_gen3_bl_deactivate,
 
 	.show_baseline = cyapa_gen3_show_baseline,
+	.calibrate_store = cyapa_gen3_do_calibrate,
 
 	.state_parse = cyapa_gen3_state_parse,
 	.operational_check = cyapa_gen3_do_operational_check,
Signature · 1 line
1.9.1
author
#20 lore raw

PATCH v9 11/18 input: cyapa: add gen5 trackpad device firmware update function support

In reply to Dudley Du

Add firmware image update function supported for gen5 trackpad device, it can be used through sysfs update_fw interface.
TEST=test on Chromebooks.

---
 drivers/input/mouse/Kconfig      |   2 +-
 drivers/input/mouse/cyapa_gen5.c | 294 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 295 insertions(+), 1 deletion(-)
drivers/input/mouse/Kconfig +1 -1
index 366fc7a..005d69b 100644
@@ -205,7 +205,7 @@ config MOUSE_BCM5974
 
 config MOUSE_CYAPA
 	tristate "Cypress APA I2C Trackpad support"
-	depends on I2C
+	depends on I2C && CRC_ITU_T
 	help
 	  This driver adds support for Cypress All Points Addressable (APA)
 	  I2C Trackpads, including the ones used in 2012 Samsung Chromebooks.
drivers/input/mouse/cyapa_gen5.c +294 -0

312 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#21 lore raw

PATCH v9 12/18 input: cyapa: add gen5 trackpad device read baseline function support

In reply to Dudley Du

Add read baseline function supported for gen5 trackpad device, it can be used through sysfs baseline interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen5.c | 606 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 606 insertions(+)
drivers/input/mouse/cyapa_gen5.c +606 -0
index a6df556..011c8f5 100644
@@ -1535,6 +1535,610 @@ static int cyapa_gen5_set_power_mode(struct cyapa *cyapa,
 	return 0;
 }
 
+static int cyapa_gen5_resume_scanning(struct cyapa *cyapa)
+{
+	u8 cmd[7] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x04 };
+	u8 resp_data[6];
+	int resp_len;
+	int ret;
+
+	/* Try to dump all bufferred data before doing command. */
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	resp_len = 6;
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, 7,
+			resp_data, &resp_len,
+			500, cyapa_gen5_sort_tsg_pip_app_resp_data, true);
+	if (ret || !VALID_CMD_RESP_HEADER(resp_data, 0x04))
+		return -EINVAL;
+
+	/* Try to dump all bufferred data when resuming scanning. */
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	return 0;
+}
+
+static int cyapa_gen5_suspend_scanning(struct cyapa *cyapa)
+{
+	u8 cmd[7] = { 0x04, 0x00, 0x05, 0x00, 0x2f, 0x00, 0x03 };
+	u8 resp_data[6];
+	int resp_len;
+	int ret;
+
+	/* Try to dump all bufferred data before doing command. */
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	resp_len = 6;
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, 7,
+			resp_data, &resp_len,
+			500, cyapa_gen5_sort_tsg_pip_app_resp_data, true);
+	if (ret || !VALID_CMD_RESP_HEADER(resp_data, 0x03))
+		return -EINVAL;
+
+	/* Try to dump all bufferred data when suspending scanning. */
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	return 0;
+}
+
+static s32 two_complement_to_s32(s32 value, int num_bits)
+{
+	if (value >> (num_bits - 1))
+		value |=  -1 << num_bits;
+	return value;
+}
+
+static s32 cyapa_parse_structure_data(u8 data_format, u8 *buf, int buf_len)
+{
+	int data_size;
+	bool big_endian;
+	bool unsigned_type;
+	s32 value;
+
+	data_size = (data_format & 0x07);
+	big_endian = ((data_format & 0x10) == 0x00);
+	unsigned_type = ((data_format & 0x20) == 0x00);
+
+	if (buf_len < data_size)
+		return 0;
+
+	switch (data_size) {
+	case 1:
+		value  = buf[0];
+		break;
+	case 2:
+		if (big_endian)
+			value = get_unaligned_be16(buf);
+		else
+			value = get_unaligned_le16(buf);
+		break;
+	case 4:
+		if (big_endian)
+			value = get_unaligned_be32(buf);
+		else
+			value = get_unaligned_le32(buf);
+		break;
+	default:
+		/* Should not happen, just as default case here. */
+		value = 0;
+		break;
+	}
+
+	if (!unsigned_type)
+		value = two_complement_to_s32(value, data_size * 8);
+
+	return value;
+}
+
+
+/*
+ * Read all the global mutual or self idac data or mutual or self local PWC
+ * data based on the @idac_data_type.
+ * If the input value of @data_size is 0, then means read global mutual or
+ * self idac data. For read global mutual idac data, @idac_max, @idac_min and
+ * @idac_ave are in order used to return the max value of global mutual idac
+ * data, the min value of global mutual idac and the average value of the
+ * global mutual idac data. For read global self idac data, @idac_max is used
+ * to return the global self cap idac data in Rx direction, @idac_min is used
+ * to return the global self cap idac data in Tx direction. @idac_ave is not
+ * used.
+ * If the input value of @data_size is not 0, than means read the mutual or
+ * self local PWC data. The @idac_max, @idac_min and @idac_ave are used to
+ * return the max, min and average value of the mutual or self local PWC data.
+ * Note, in order to raed mutual local PWC data, must read invoke this function
+ * to read the mutual global idac data firstly to set the correct Rx number
+ * value, otherwise, the read mutual idac and PWC data may not correct.
+ */
+static int cyapa_gen5_read_idac_data(struct cyapa *cyapa,
+		u8 cmd_code, u8 idac_data_type, int *data_size,
+		int *idac_max, int *idac_min, int *idac_ave)
+{
+	int ret;
+	int i;
+	u8 cmd[12];
+	u8 resp_data[256];
+	int resp_len;
+	int read_len;
+	int value;
+	u16 offset;
+	int read_elements;
+	bool read_global_idac;
+	int sum, count, max_element_cnt;
+	int tmp_max, tmp_min, tmp_ave, tmp_sum, tmp_count, tmp_max_elements;
+	int electrodes_rx;
+
+	if (cmd_code != GEN5_CMD_RETRIEVE_DATA_STRUCTURE ||
+		(idac_data_type != GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
+		idac_data_type != GEN5_RETRIEVE_SELF_CAP_PWC_DATA) ||
+		!data_size || !idac_max || !idac_min || !idac_ave)
+		return -EINVAL;
+
+	*idac_max = INT_MIN;
+	*idac_min = INT_MAX;
+	sum = count = tmp_count = 0;
+	electrodes_rx = 0;
+	tmp_max_elements = 0;
+	if (*data_size == 0) {
+		/*
+		 * Read global idac values firstly.
+		 * Currently, no idac data exceed 4 bytes.
+		 */
+		read_global_idac = true;
+		offset = 0;
+		*data_size = 4;
+
+		if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
+			if (cyapa->electrodes_rx == 0) {
+				if (cyapa->electrodes_y > cyapa->electrodes_x) {
+					electrodes_rx = cyapa->electrodes_y;
+					tmp_max_elements = cyapa->electrodes_x;
+				} else {
+					electrodes_rx =	cyapa->electrodes_x;
+					tmp_max_elements = cyapa->electrodes_y;
+				}
+			} else {
+				electrodes_rx = cyapa->electrodes_rx;
+				tmp_max_elements = 0;  /* Disable Rx detect. */
+			}
+			max_element_cnt = ((electrodes_rx + 7) / 8) * 8;
+			tmp_max = INT_MIN;
+			tmp_min = INT_MAX;
+			tmp_ave = tmp_sum = tmp_count = 0;
+		} else
+			max_element_cnt = 2;
+	} else {
+		read_global_idac = false;
+		if (*data_size > 4)
+			*data_size = 4;
+		/* Calculate the start offset in bytes of local PWC data. */
+		if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
+			offset = ((cyapa->electrodes_rx + 7) / 8) * 8
+						* (*data_size);
+			if (cyapa->electrodes_rx == cyapa->electrodes_x)
+				tmp_count = cyapa->electrodes_y;
+			else
+				tmp_count = cyapa->electrodes_x;
+			max_element_cnt = ((cyapa->electrodes_rx + 7) / 8) *
+						8 * tmp_count;
+		} else if (idac_data_type == GEN5_RETRIEVE_SELF_CAP_PWC_DATA) {
+			offset = 2;
+			max_element_cnt = cyapa->electrodes_x +
+						cyapa->electrodes_y;
+		}
+	}
+
+	do {
+		read_elements = (256 - 10) / (*data_size);
+		read_elements = min(read_elements, max_element_cnt - count);
+		read_len = read_elements * (*data_size);
+
+		cmd[0] = 0x04;
+		cmd[1] = 0x00;
+		cmd[2] = 0x0a;
+		cmd[3] = 0x00;
+		cmd[4] = GEN5_APP_CMD_REPORT_ID;
+		cmd[5] = 0x00;
+		cmd[6] = cmd_code;
+		put_unaligned_le16(offset, &cmd[7]); /* Read Offset[15:0] */
+		put_unaligned_le16(read_len, &cmd[9]); /* Read Length[15:0] */
+		cmd[11] = idac_data_type;
+		resp_len = 10 + read_len;
+		ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+				cmd, 12,
+				resp_data, &resp_len,
+				500, cyapa_gen5_sort_tsg_pip_app_resp_data,
+				true);
+		if (ret || resp_len < 10 ||
+				!VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
+				!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5]) ||
+				resp_data[6] != idac_data_type)
+			return (ret < 0) ? ret : -EAGAIN;
+		read_len = get_unaligned_le16(&resp_data[7]);
+		if (read_len == 0)
+			break;
+
+		*data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
+		if (read_len < *data_size)
+			return -EINVAL;
+
+		if (read_global_idac &&
+			idac_data_type == GEN5_RETRIEVE_SELF_CAP_PWC_DATA) {
+			/* Rx's self global idac data. */
+			*idac_max = cyapa_parse_structure_data(
+					resp_data[9], &resp_data[10],
+					*data_size);
+			/* Tx's self global idac data. */
+			*idac_min = cyapa_parse_structure_data(
+					resp_data[9],
+					&resp_data[10 + *data_size],
+					*data_size);
+			break;
+		}
+
+		/* Read mutual global idac or local mutual/self PWC data. */
+		offset += read_len;
+		for (i = 10; i < (read_len + 10); i += *data_size) {
+			value = cyapa_parse_structure_data(resp_data[9],
+					&resp_data[i], *data_size);
+			*idac_min = min(value, *idac_min);
+			*idac_max = max(value, *idac_max);
+
+			if (idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA &&
+				tmp_count < tmp_max_elements &&
+				read_global_idac) {
+				tmp_min = min(value, tmp_min);
+				tmp_max = max(value, tmp_max);
+				tmp_sum += value;
+				tmp_count++;
+			}
+
+			sum += value;
+			count++;
+
+			if (count >= max_element_cnt)
+				goto out;
+		}
+	} while (true);
+
+out:
+	*idac_ave = count ? (sum / count) : 0;
+
+	if (read_global_idac &&
+		idac_data_type == GEN5_RETRIEVE_MUTUAL_PWC_DATA) {
+		if (tmp_count == 0)
+			return 0;
+		/* Algorithm to detect electrodes_rx value. */
+		tmp_ave = tmp_sum / tmp_count;
+		tmp_count = tmp_ave * 15 / 100;
+		if (abs(tmp_ave - *idac_ave) > tmp_count ||
+			(abs(tmp_ave - *idac_min) > (tmp_count * 2) &&
+				*idac_min < tmp_min) ||
+			(abs(*idac_max - tmp_ave) > (tmp_count * 2) &&
+				*idac_max > tmp_max)) {
+			/* Overcount the mutual global idac values. */
+			cyapa->electrodes_rx = tmp_max_elements;
+			*idac_min = tmp_min;
+			*idac_max = tmp_max;
+			*idac_ave = tmp_ave;
+		} else
+			cyapa->electrodes_rx = electrodes_rx;
+	}
+
+	return 0;
+}
+
+static int cyapa_gen5_read_mutual_idac_data(struct cyapa *cyapa,
+	int *gidac_mutual_max, int *gidac_mutual_min, int *gidac_mutual_ave,
+	int *lidac_mutual_max, int *lidac_mutual_min, int *lidac_mutual_ave)
+{
+	int ret;
+	int data_size;
+
+	*gidac_mutual_max = *gidac_mutual_min = *gidac_mutual_ave = 0;
+	*lidac_mutual_max = *lidac_mutual_min = *lidac_mutual_ave = 0;
+
+	data_size = 0;
+	ret = cyapa_gen5_read_idac_data(cyapa,
+		GEN5_CMD_RETRIEVE_DATA_STRUCTURE,
+		GEN5_RETRIEVE_MUTUAL_PWC_DATA,
+		&data_size,
+		gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave);
+	if (ret)
+		return ret;
+
+	ret = cyapa_gen5_read_idac_data(cyapa,
+		GEN5_CMD_RETRIEVE_DATA_STRUCTURE,
+		GEN5_RETRIEVE_MUTUAL_PWC_DATA,
+		&data_size,
+		lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave);
+	return ret;
+}
+
+static int cyapa_gen5_read_self_idac_data(struct cyapa *cyapa,
+		int *gidac_self_rx, int *gidac_self_tx,
+		int *lidac_self_max, int *lidac_self_min, int *lidac_self_ave)
+{
+	int ret;
+	int data_size;
+
+	*gidac_self_rx = *gidac_self_tx = 0;
+	*lidac_self_max = *lidac_self_min = *lidac_self_ave = 0;
+
+	data_size = 0;
+	ret = cyapa_gen5_read_idac_data(cyapa,
+		GEN5_CMD_RETRIEVE_DATA_STRUCTURE,
+		GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
+		&data_size,
+		lidac_self_max, lidac_self_min, lidac_self_ave);
+	if (ret)
+		return ret;
+	*gidac_self_rx = *lidac_self_max;
+	*gidac_self_tx = *lidac_self_min;
+
+	ret = cyapa_gen5_read_idac_data(cyapa,
+		GEN5_CMD_RETRIEVE_DATA_STRUCTURE,
+		GEN5_RETRIEVE_SELF_CAP_PWC_DATA,
+		&data_size,
+		lidac_self_max, lidac_self_min, lidac_self_ave);
+	return ret;
+}
+
+static ssize_t cyapa_gen5_execute_panel_scan(struct cyapa *cyapa)
+{
+	int ret;
+	u8 cmd[7];
+	u8 resp_data[6];
+	int resp_len;
+
+	cmd[0] = 0x04;
+	cmd[1] = 0x00;
+	cmd[2] = 0x05;
+	cmd[3] = 0x00;
+	cmd[4] = GEN5_APP_CMD_REPORT_ID;
+	cmd[5] = 0x00;
+	cmd[6] = GEN5_CMD_EXECUTE_PANEL_SCAN;  /* Command code */
+	resp_len = 6;
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, 7,
+			resp_data, &resp_len,
+			500, cyapa_gen5_sort_tsg_pip_app_resp_data, true);
+	if (ret || resp_len != 6 ||
+			!VALID_CMD_RESP_HEADER(resp_data,
+				GEN5_CMD_EXECUTE_PANEL_SCAN) ||
+			!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5])) {
+		cyapa_gen5_resume_scanning(cyapa);
+		return (ret < 0) ? ret : -EAGAIN;
+	}
+
+	return 0;
+}
+
+static int cyapa_gen5_read_panel_scan_raw_data(struct cyapa *cyapa,
+		u8 cmd_code, u8 raw_data_type, int raw_data_max_num,
+		int *raw_data_max, int *raw_data_min, int *raw_data_ave,
+		u8 *buffer)
+{
+	int ret;
+	int i;
+	u8 cmd[12];
+	u8 resp_data[256];  /* Max bytes can transfer one time. */
+	int resp_len;
+	int read_elements;
+	int read_len;
+	u16 offset;
+	s32 value;
+	int sum, count;
+	int data_size;
+	s32 *intp;
+
+	if (cmd_code != GEN5_CMD_RETRIEVE_PANEL_SCAN ||
+		(raw_data_type > GEN5_PANEL_SCAN_SELF_DIFFCOUNT) ||
+		!raw_data_max || !raw_data_min || !raw_data_ave)
+		return -EINVAL;
+
+	intp = (s32 *)buffer;
+	*raw_data_max = INT_MIN;
+	*raw_data_min = INT_MAX;
+	sum = count = 0;
+	offset = 0;
+	read_elements = (256 - 10) / 4;  /* Currently, max element size is 4. */
+	read_len = read_elements * 4;
+	do {
+		cmd[0] = 0x04;
+		cmd[1] = 0x00;
+		cmd[2] = 0x0a;
+		cmd[3] = 0x00;
+		cmd[4] = GEN5_APP_CMD_REPORT_ID;
+		cmd[5] = 0x00;
+		cmd[6] = cmd_code;  /* Command code */
+		put_unaligned_le16(offset, &cmd[7]);
+		put_unaligned_le16(read_elements, &cmd[9]);
+		cmd[11] = raw_data_type;
+		resp_len = 10 + read_len;
+
+		ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, 12,
+			resp_data, &resp_len,
+			500, cyapa_gen5_sort_tsg_pip_app_resp_data, true);
+		if (ret || resp_len < 10 ||
+				!VALID_CMD_RESP_HEADER(resp_data, cmd_code) ||
+				!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5]) ||
+				resp_data[6] != raw_data_type)
+			return (ret < 0) ? ret : -EAGAIN;
+
+		read_elements = get_unaligned_le16(&resp_data[7]);
+		if (read_elements == 0)
+			break;
+
+		data_size = (resp_data[9] & GEN5_PWC_DATA_ELEMENT_SIZE_MASK);
+		offset += read_elements;
+		if (read_elements) {
+			for (i = 10;
+			     i < (read_elements * data_size + 10);
+			     i += data_size) {
+				value = cyapa_parse_structure_data(resp_data[9],
+						&resp_data[i], data_size);
+				*raw_data_min = min(value, *raw_data_min);
+				*raw_data_max = max(value, *raw_data_max);
+
+				if (intp)
+					put_unaligned_le32(value, &intp[count]);
+
+				sum += value;
+				count++;
+
+			}
+		}
+
+		if (count >= raw_data_max_num)
+			break;
+
+		read_elements = (sizeof(resp_data) - 10) / data_size;
+		read_len = read_elements * data_size;
+	} while (true);
+
+	*raw_data_ave = count ? (sum / count) : 0;
+
+	return 0;
+}
+
+static ssize_t cyapa_gen5_show_baseline(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int ret, err;
+	int gidac_mutual_max, gidac_mutual_min, gidac_mutual_ave;
+	int lidac_mutual_max, lidac_mutual_min, lidac_mutual_ave;
+	int gidac_self_rx, gidac_self_tx;
+	int lidac_self_max, lidac_self_min, lidac_self_ave;
+	int raw_cap_mutual_max, raw_cap_mutual_min, raw_cap_mutual_ave;
+	int raw_cap_self_max, raw_cap_self_min, raw_cap_self_ave;
+	int mutual_diffdata_max, mutual_diffdata_min, mutual_diffdata_ave;
+	int self_diffdata_max, self_diffdata_min, self_diffdata_ave;
+	int mutual_baseline_max, mutual_baseline_min, mutual_baseline_ave;
+	int self_baseline_max, self_baseline_min, self_baseline_ave;
+
+	if (cyapa->state != CYAPA_STATE_GEN5_APP)
+		return -EBUSY;
+
+	/* 1. Suspend Scanning*/
+	ret = cyapa_gen5_suspend_scanning(cyapa);
+	if (ret)
+		return ret;
+
+	/* 2.  Read global and local mutual IDAC data. */
+	gidac_self_rx = gidac_self_tx = 0;
+	err = cyapa_gen5_read_mutual_idac_data(cyapa,
+				&gidac_mutual_max, &gidac_mutual_min,
+				&gidac_mutual_ave, &lidac_mutual_max,
+				&lidac_mutual_min, &lidac_mutual_ave);
+	if (err)
+		goto resume_scanning;
+
+	/* 3.  Read global and local self IDAC data. */
+	err = cyapa_gen5_read_self_idac_data(cyapa,
+				&gidac_self_rx, &gidac_self_tx,
+				&lidac_self_max, &lidac_self_min,
+				&lidac_self_ave);
+	if (err)
+		goto resume_scanning;
+
+	/* 4. Execuate panel scan. It must be executed before read data. */
+	err = cyapa_gen5_execute_panel_scan(cyapa);
+	if (err)
+		goto resume_scanning;
+
+	/* 5. Retrieve panel scan, mutual cap raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_MUTUAL_RAW_DATA,
+				cyapa->electrodes_x * cyapa->electrodes_y,
+				&raw_cap_mutual_max, &raw_cap_mutual_min,
+				&raw_cap_mutual_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+	/* 6. Retrieve panel scan, self cap raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_SELF_RAW_DATA,
+				cyapa->electrodes_x + cyapa->electrodes_y,
+				&raw_cap_self_max, &raw_cap_self_min,
+				&raw_cap_self_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+	/* 7. Retrieve panel scan, mutual cap diffcount raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT,
+				cyapa->electrodes_x * cyapa->electrodes_y,
+				&mutual_diffdata_max, &mutual_diffdata_min,
+				&mutual_diffdata_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+	/* 8. Retrieve panel scan, self cap diffcount raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_SELF_DIFFCOUNT,
+				cyapa->electrodes_x + cyapa->electrodes_y,
+				&self_diffdata_max, &self_diffdata_min,
+				&self_diffdata_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+	/* 9. Retrieve panel scan, mutual cap baseline raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_MUTUAL_BASELINE,
+				cyapa->electrodes_x * cyapa->electrodes_y,
+				&mutual_baseline_max, &mutual_baseline_min,
+				&mutual_baseline_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+	/* 10. Retrieve panel scan, self cap baseline raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_SELF_BASELINE,
+				cyapa->electrodes_x + cyapa->electrodes_y,
+				&self_baseline_max, &self_baseline_min,
+				&self_baseline_ave,
+				NULL);
+	if (err)
+		goto resume_scanning;
+
+resume_scanning:
+	/* 11. Resume Scanning*/
+	ret = cyapa_gen5_resume_scanning(cyapa);
+	if (ret || err)
+		return ret ? ret : err;
+
+	/* 12. Output data strings */
+	ret = scnprintf(buf, PAGE_SIZE, "%d %d %d %d %d %d %d %d %d %d %d ",
+		gidac_mutual_min, gidac_mutual_max, gidac_mutual_ave,
+		lidac_mutual_min, lidac_mutual_max, lidac_mutual_ave,
+		gidac_self_rx, gidac_self_tx,
+		lidac_self_min, lidac_self_max, lidac_self_ave);
+	err = scnprintf(buf + ret, PAGE_SIZE - ret,
+		"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
+		raw_cap_mutual_min, raw_cap_mutual_max, raw_cap_mutual_ave,
+		raw_cap_self_min, raw_cap_self_max, raw_cap_self_ave,
+		mutual_diffdata_min, mutual_diffdata_max, mutual_diffdata_ave,
+		self_diffdata_min, self_diffdata_max, self_diffdata_ave,
+		mutual_baseline_min, mutual_baseline_max, mutual_baseline_ave,
+		self_baseline_min, self_baseline_max, self_baseline_ave);
+	return ret + err;
+}
+
 static bool cyapa_gen5_sort_system_info_data(struct cyapa *cyapa,
 		u8 *buf, int len)
 {
@@ -1938,6 +2542,8 @@ const struct cyapa_dev_ops cyapa_gen5_ops = {
 	.bl_initiate = cyapa_gen5_bl_initiate,
 	.update_fw = cyapa_gen5_do_fw_update,
 
+	.show_baseline = cyapa_gen5_show_baseline,
+
 	.initialize = cyapa_gen5_initialize,
 
 	.state_parse = cyapa_gen5_state_parse,
Signature · 1 line
1.9.1
author
#22 lore raw

PATCH v9 13/18 input: cyapa: add gen5 trackpad device force re-calibrate function support

In reply to Dudley Du

Add force re-calibrate function supported for gen5 trackpad device, it can be used through sysfs calibrate interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen5.c | 65 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
drivers/input/mouse/cyapa_gen5.c +65 -0
index 011c8f5..7e7de12 100644
@@ -1583,6 +1583,70 @@ static int cyapa_gen5_suspend_scanning(struct cyapa *cyapa)
 	return 0;
 }
 
+static int cyapa_gen5_calibrate_pwcs(struct cyapa *cyapa,
+		u8 calibrate_sensing_mode_type)
+{
+	int ret;
+	u8 cmd[8];
+	u8 resp_data[6];
+	int resp_len;
+
+	/* Try to dump all bufferred data before doing command. */
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	cmd[0] = 0x04;
+	cmd[1] = 0x00;
+	cmd[2] = 0x06;
+	cmd[3] = 0x00;
+	cmd[4] = GEN5_APP_CMD_REPORT_ID;
+	cmd[5] = 0x00;
+	cmd[6] = GEN5_CMD_CALIBRATE;
+	cmd[7] = calibrate_sensing_mode_type;
+	resp_len = sizeof(resp_data);
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, sizeof(cmd),
+			resp_data, &resp_len,
+			5000, cyapa_gen5_sort_tsg_pip_app_resp_data, true);
+	if (ret || !VALID_CMD_RESP_HEADER(resp_data, GEN5_CMD_CALIBRATE) ||
+			!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5]))
+		return ret < 0 ? ret : -EAGAIN;
+
+	return 0;
+}
+
+static ssize_t cyapa_gen5_do_calibrate(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct cyapa *cyapa = dev_get_drvdata(dev);
+	int ret, calibrate_ret;
+
+	/* 1. Suspend Scanning*/
+	ret = cyapa_gen5_suspend_scanning(cyapa);
+	if (ret)
+		return ret;
+
+	/* 2. Do mutual capacitance fine calibrate. */
+	calibrate_ret = cyapa_gen5_calibrate_pwcs(cyapa,
+				CYAPA_SENSING_MODE_MUTUAL_CAP_FINE);
+	if (calibrate_ret)
+		goto resume_scanning;
+
+	/* 3. Do self capacitance calibrate. */
+	calibrate_ret = cyapa_gen5_calibrate_pwcs(cyapa,
+				CYAPA_SENSING_MODE_SELF_CAP);
+	if (calibrate_ret)
+		goto resume_scanning;
+
+resume_scanning:
+	/* 4. Resume Scanning*/
+	ret = cyapa_gen5_resume_scanning(cyapa);
+	if (ret || calibrate_ret)
+		return ret ? ret : calibrate_ret;
+
+	return count;
+}
+
 static s32 two_complement_to_s32(s32 value, int num_bits)
 {
 	if (value >> (num_bits - 1))
@@ -2543,6 +2607,7 @@ const struct cyapa_dev_ops cyapa_gen5_ops = {
 	.update_fw = cyapa_gen5_do_fw_update,
 
 	.show_baseline = cyapa_gen5_show_baseline,
+	.calibrate_store = cyapa_gen5_do_calibrate,
 
 	.initialize = cyapa_gen5_initialize,
 
Signature · 1 line
1.9.1
author
#23 lore raw

PATCH v9 14/18 input: cyapa: add read firmware image debugfs interface support

In reply to Dudley Du

Add read firmware image from trackpad device interface supported in cyapa driver through debugfs read_fw interface.
Through this interface user can read out, check and backup the firmware image of the trackpad device before any firmware update, or can use the backed image to do firmware image recovery.
TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 174 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/input/mouse/cyapa.h |  11 +++
 2 files changed, 184 insertions(+), 1 deletion(-)
drivers/input/mouse/cyapa.c +173 -1
index 5f384ff..bc1d75a 100644
@@ -14,6 +14,7 @@
  * more details.
  */
 
+#include <linux/debugfs.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/input.h>
@@ -38,6 +39,9 @@
 
 const char unique_str[] = "CYTRA";
 
+/* Global root node of the cyapa debugfs directory. */
+static struct dentry *cyapa_debugfs_root;
+
 
 ssize_t cyapa_i2c_reg_read_block(struct cyapa *cyapa, u8 reg, size_t len,
 					u8 *values)
@@ -475,6 +479,134 @@ int cyapa_detect(struct cyapa *cyapa)
 }
 
 /*
+ **************************************************************
+ * debugfs interface
+ **************************************************************
+*/
+static int cyapa_debugfs_open(struct inode *inode, struct file *file)
+{
+	struct cyapa *cyapa = inode->i_private;
+	int ret;
+
+	if (!cyapa)
+		return -ENODEV;
+
+	ret = mutex_lock_interruptible(&cyapa->debugfs_mutex);
+	if (ret)
+		return ret;
+
+	if (!get_device(&cyapa->client->dev)) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	file->private_data = cyapa;
+
+	if (cyapa->fw_image && cyapa->fw_image_size) {
+		ret = 0;
+		goto out;
+	}
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		goto out;
+	/*
+	 * If firmware hasn't been read yet, read it all in one pass.
+	 * Subsequent opens will reuse the data in this same buffer.
+	 */
+	if (cyapa->ops->read_fw) {
+		ret = cyapa->ops->read_fw(cyapa);
+
+		/*
+		 * Redetect trackpad device states because read_fw will
+		 * reset trackpad device into bootloader mode.
+		 */
+		cyapa_detect(cyapa);
+	} else
+		ret = -EPERM;
+
+	mutex_unlock(&cyapa->state_sync_lock);
+out:
+	mutex_unlock(&cyapa->debugfs_mutex);
+	return ret;
+}
+
+static int cyapa_debugfs_release(struct inode *inode, struct file *file)
+{
+	struct cyapa *cyapa = file->private_data;
+	int ret;
+
+	if (!cyapa)
+		return 0;
+
+	ret = mutex_lock_interruptible(&cyapa->debugfs_mutex);
+	if (ret)
+		return ret;
+	file->private_data = NULL;
+	put_device(&cyapa->client->dev);
+	mutex_unlock(&cyapa->debugfs_mutex);
+
+	return 0;
+}
+
+/* Return some bytes from the buffered firmware image, starting from *ppos */
+static ssize_t cyapa_debugfs_read_fw(struct file *file, char __user *buffer,
+				     size_t count, loff_t *ppos)
+{
+	struct cyapa *cyapa = file->private_data;
+
+	if (!cyapa->fw_image)
+		return -EINVAL;
+
+	if (*ppos >= cyapa->fw_image_size)
+		return 0;
+
+	if (count + *ppos > cyapa->fw_image_size)
+		count = cyapa->fw_image_size - *ppos;
+
+	if (copy_to_user(buffer, &cyapa->fw_image[*ppos], count))
+		return -EFAULT;
+
+	*ppos += count;
+	return count;
+}
+
+static const struct file_operations cyapa_read_fw_fops = {
+	.open = cyapa_debugfs_open,
+	.release = cyapa_debugfs_release,
+	.read = cyapa_debugfs_read_fw
+};
+
+static int cyapa_debugfs_init(struct cyapa *cyapa)
+{
+	struct device *dev = &cyapa->client->dev;
+
+	if (!cyapa_debugfs_root)
+		return -ENODEV;
+
+	cyapa->dentry_dev = debugfs_create_dir(kobject_name(&dev->kobj),
+					       cyapa_debugfs_root);
+
+	if (!cyapa->dentry_dev)
+		return -ENODEV;
+
+	mutex_init(&cyapa->debugfs_mutex);
+
+	debugfs_create_file(CYAPA_DEBUGFS_READ_FW, S_IRUSR, cyapa->dentry_dev,
+			    cyapa, &cyapa_read_fw_fops);
+
+	return 0;
+}
+
+static void cyapa_remove_debugfs(void *data)
+{
+	struct cyapa *cyapa = data;
+
+	debugfs_remove_recursive(cyapa->dentry_dev);
+	mutex_destroy(&cyapa->debugfs_mutex);
+}
+
+/*
  * Sysfs Interface.
  */
 
@@ -1012,6 +1144,20 @@ static int cyapa_probe(struct i2c_client *client,
 		return ret;
 	}
 
+	ret = cyapa_debugfs_init(cyapa);
+	if (ret) {
+		dev_err(dev, "failed to create debugfs entries, (%d).\n", ret);
+		return ret;
+	}
+
+	ret = devm_add_action(dev, cyapa_remove_debugfs, cyapa);
+	if (ret) {
+		cyapa_remove_debugfs(cyapa);
+		dev_err(dev, "failed to add debugfs cleanup action, (%d)\n",
+			ret);
+		return ret;
+	}
+
 #ifdef CONFIG_PM_SLEEP
 	if (device_can_wakeup(dev)) {
 		ret = sysfs_merge_group(&client->dev.kobj,
@@ -1192,7 +1338,33 @@ static struct i2c_driver cyapa_driver = {
 	.id_table = cyapa_id_table,
 };
 
-module_i2c_driver(cyapa_driver);
+static int __init cyapa_init(void)
+{
+	int ret;
+
+	/* Create a global debugfs root for all cyapa devices */
+	cyapa_debugfs_root = debugfs_create_dir("cyapa", NULL);
+	if (cyapa_debugfs_root == ERR_PTR(-ENODEV))
+		cyapa_debugfs_root = NULL;
+
+	ret = i2c_add_driver(&cyapa_driver);
+	if (ret) {
+		pr_err("cyapa driver register FAILED.\n");
+		return ret;
+	}
+
+	return ret;
+}
+
+static void __exit cyapa_exit(void)
+{
+	debugfs_remove_recursive(cyapa_debugfs_root);
+
+	i2c_del_driver(&cyapa_driver);
+}
+
+module_init(cyapa_init);
+module_exit(cyapa_exit);
 
 MODULE_DESCRIPTION("Cypress APA I2C Trackpad Driver");
 MODULE_AUTHOR("Dudley Du <[email protected]>");
drivers/input/mouse/cyapa.h +11 -0
index 060ac7d..288c7de 100644
@@ -189,6 +189,8 @@ struct cyapa_dev_ops {
 	ssize_t (*calibrate_store)(struct device *,
 			struct device_attribute *, const char *, size_t);
 
+	int (*read_fw)(struct cyapa *);
+
 	int (*initialize)(struct cyapa *cyapa);
 
 	int (*state_parse)(struct cyapa *cyapa, u8 *reg_status, int len);
@@ -288,6 +290,15 @@ struct cyapa {
 	 */
 	struct mutex state_sync_lock;
 
+	/* Per-instance debugfs root */
+	struct dentry *dentry_dev;
+
+	/* Buffer to store firmware read using debugfs */
+	struct mutex debugfs_mutex;
+	struct cyapa_tsg_bin_image_head fw_img_head;
+	u8 *fw_image;
+	size_t fw_image_size;
+
 	const struct cyapa_dev_ops *ops;
 
 	union cyapa_cmd_states cmd_states;
Signature · 1 line
1.9.1
author
#24 lore raw

PATCH v9 15/18 input: cyapa: add gen3 trackpad device read firmware image function support

In reply to Dudley Du

Add read firmware image function supported for gen3 trackpad device, it can be used through debugfs read_fw interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen3.c | 67 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
drivers/input/mouse/cyapa_gen3.c +67 -0
index b477d67..f8aeb58 100644
@@ -710,6 +710,36 @@ static int cyapa_gen3_write_fw_block(struct cyapa *cyapa,
 	return ret;
 }
 
+/*
+ * A firmware block read command reads 16 bytes of data from flash starting
+ * from a given address.  The 12-byte block read command has the format:
+ *   <0xff> <CMD> <Key> <Addr>
+ *
+ *  <0xff>  - every command starts with 0xff
+ *  <CMD>   - the read command value is 0x3c
+ *  <Key>   - read commands include an 8-byte key: { 00 01 02 03 04 05 06 07 }
+ *  <Addr>  - Memory address (16-bit, big-endian)
+ *
+ * The command is followed by an i2c block read to read the 16 bytes of data.
+ */
+static int cyapa_gen3_read_fw_bytes(struct cyapa *cyapa, u16 addr, u8 *data)
+{
+	int ret;
+	u8 cmd[] = { 0xff, 0x3c, 0x00, 0x01, 0x02, 0x03, 0x04,
+		0x05, 0x06, 0x07, addr >> 8, addr };
+
+	ret = cyapa_gen3_write_buffer(cyapa, cmd, sizeof(cmd));
+	if (ret)
+		return ret;
+
+	/* Read data buffer starting from offset 16 */
+	ret = cyapa_i2c_reg_read_block(cyapa, 16, CYAPA_FW_READ_SIZE, data);
+	if (ret != CYAPA_FW_READ_SIZE)
+		return (ret < 0) ? ret : -EIO;
+
+	return 0;
+}
+
 static int cyapa_gen3_write_blocks(struct cyapa *cyapa,
 		size_t start_block, size_t block_count,
 		const u8 *image_data)
@@ -756,6 +786,41 @@ static int cyapa_gen3_do_fw_update(struct cyapa *cyapa,
 	return 0;
 }
 
+/*
+ * Read the entire firmware image into ->fw_image.
+ * If the ->fw_image has already been allocated, then this function
+ * doesn't do anything and just returns 0.
+ * If an error occurs while reading the image, ->fw_image is freed, and
+ * the error is returned.
+ *
+ * The firmware is a fixed size (CYAPA_FW_SIZE), and is read out in
+ * fixed length (CYAPA_FW_READ_SIZE) chunks.
+ */
+static int cyapa_gen3_read_fw(struct cyapa *cyapa)
+{
+	int ret;
+	int addr;
+
+	ret = cyapa_gen3_bl_enter(cyapa);
+	if (ret)
+		return ret;
+
+	cyapa->fw_image = cyapa->fw_image ? cyapa->fw_image :
+		devm_kzalloc(&cyapa->client->dev, CYAPA_FW_SIZE, GFP_KERNEL);
+	if (!cyapa->fw_image)
+		return -ENOMEM;
+
+	for (addr = 0; addr < CYAPA_FW_SIZE; addr += CYAPA_FW_READ_SIZE) {
+		ret = cyapa_gen3_read_fw_bytes(cyapa, CYAPA_FW_HDR_START + addr,
+					  &cyapa->fw_image[addr]);
+		if (ret)
+			return ret;
+	}
+
+	cyapa->fw_image_size = CYAPA_FW_SIZE;
+	return 0;
+}
+
 static ssize_t cyapa_gen3_do_calibrate(struct device *dev,
 				     struct device_attribute *attr,
 				     const char *buf, size_t count)
@@ -1203,6 +1268,8 @@ const struct cyapa_dev_ops cyapa_gen3_ops = {
 	.show_baseline = cyapa_gen3_show_baseline,
 	.calibrate_store = cyapa_gen3_do_calibrate,
 
+	.read_fw = cyapa_gen3_read_fw,
+
 	.state_parse = cyapa_gen3_state_parse,
 	.operational_check = cyapa_gen3_do_operational_check,
 
Signature · 1 line
1.9.1
maintainer
#25 lore raw

Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 2 lines · Dudley Du

Add read firmware image function supported for gen3 trackpad device, it can be used through debugfs read_fw interface.

Why do we need this? Can we do the same via usespace program accessing the i2c device through /dev/i2c-N?

Thanks.

Signature · 1 line
Dmitry
#26 lore raw

RE: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Thanks, Dmitry

Quoted 9 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:40
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 6 lines
Quoted 2 lines · Dudley Du

Add read firmware image function supported for gen3 trackpad device, it can be used through debugfs read_fw interface.

Why do we need this? Can we do the same via usespace program accessing the i2c device through /dev/i2c-N?

This interface is used to test and debug only. It's defined and required by the chromium projects.

And in the driver side, becase the firmware image read process is done based on interrupt signal, if do this in userspace through /dev/i2c-N, there will be two issues:
1) for gen5, after each command, an interrupt will be asserted, so if throug /dev/i2c-N, userspace program cannot get the interrupt signal.
2) and when the interrupt signal assert, driver won't know it’s a command signal for image read, so it will try to process it as data report.
To avoid this, additional interface must be added to mark the image read status and block interrupt signal to be process as data report.

Quoted 5 lines

Thanks.

--
Dmitry

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

maintainer
#27 lore raw

Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 20 lines · Dudley Du

Thanks, Dmitry

Quoted 9 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:40
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 6 lines
Quoted 2 lines · Dudley Du

Add read firmware image function supported for gen3 trackpad device, it can be used through debugfs read_fw interface.

Why do we need this? Can we do the same via usespace program accessing the i2c device through /dev/i2c-N?

This interface is used to test and debug only. It's defined and required by the chromium projects.

Quoted 8 lines

And in the driver side, becase the firmware image read process is done based on interrupt signal, if do this in userspace through /dev/i2c-N, there will be two issues:
1) for gen5, after each command, an interrupt will be asserted, so if throug /dev/i2c-N, userspace program cannot get the interrupt signal.
2) and when the interrupt signal assert, driver won't know it’s a command signal for image read, so it will try to process it as data report.
To avoid this, additional interface must be added to mark the image read status and block interrupt signal to be process as data report.

I believe that while userspace acc4esses the device through /dev/i2c-N you should unbind the cyapa driver from the device (via sysfs driver core bind/unbind attribute) so that it doe snot get in the way. Once diagnostics is done you can bind the driver back to device, restoring normal operation.

That is how we are planning to handle other devices at factory as well.

Thanks.

Signature · 5 lines
Dmitry
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#28 lore raw

RE: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 46 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014年12月5日 1:47
To: Dudley Du
Cc: Dudley Du; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 11 lines · Dudley Du

Thanks, Dmitry

Quoted 9 lines

-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: 2014?11?10? 16:40
To: Dudley Du
Cc: [email protected]; Dudley Du; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v9 15/18] input: cyapa: add gen3 trackpad device read firmware image function support

Quoted 8 lines
Quoted 6 lines · Dudley Du
Quoted 2 lines

Add read firmware image function supported for gen3 trackpad device, it can be used through debugfs read_fw interface.

Why do we need this? Can we do the same via usespace program accessing the i2c device through /dev/i2c-N?

This interface is used to test and debug only. It's defined and required by the

chromium projects.

Quoted 2 lines

And in the driver side, becase the firmware image read process is done based on

interrupt signal,

Quoted 2 lines

if do this in userspace through /dev/i2c-N, there will be two issues:
1) for gen5, after each command, an interrupt will be asserted, so if throug

/dev/i2c-N,

Quoted 2 lines

userspace program cannot get the interrupt signal.
2) and when the interrupt signal assert, driver won't know it’s a command signal

for image read,

Quoted 2 lines

so it will try to process it as data report.
To avoid this, additional interface must be added to mark the image read status

and

Quoted 1 line

block interrupt signal to be process as data report.

Quoted 8 lines

I believe that while userspace acc4esses the device through /dev/i2c-N you should unbind the cyapa driver from the device (via sysfs driver core bind/unbind attribute) so that it doe snot get in the way. Once diagnostics is done you can bind the driver back to device, restoring normal operation.

That is how we are planning to handle other devices at factory as well.

Thanks, I will remove the debugfs interfaces from the patach series.

Quoted 5 lines

Thanks.

--
Dmitry

This message and any attachments may contain Cypress (or its subsidiaries) confidential information. If it has been received in error, please advise the sender and immediately delete this message.

author
#29 lore raw

PATCH v9 16/18 input: cyapa: add gen5 trackpad device read firmware image function support

In reply to Dudley Du

Add read firmware image function supported for gen5 trackpad device, it can be used through debugfs read_fw interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen5.c | 154 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 154 insertions(+)
drivers/input/mouse/cyapa_gen5.c +154 -0
index 7e7de12..35334dc 100644
@@ -1213,6 +1213,153 @@ static int cyapa_gen5_write_fw_block(struct cyapa *cyapa,
 	return 0;
 }
 
+static int cyapa_gen5_read_fw_bytes(struct cyapa *cyapa, u16 row_num, u8 *data)
+{
+	int ret;
+	u8 cmd[16];
+	size_t cmd_len;
+	u8 resp_data[CYAPA_TSG_FW_ROW_SIZE / 2 + GEN5_MIN_BL_RESP_LENGTH];
+	int resp_len;
+	u16 offset;
+	u16 cmd_crc;
+	struct cyapa_tsg_bin_image_data_record *fw_img_record;
+
+	fw_img_record = (struct cyapa_tsg_bin_image_data_record *)data;
+
+	cmd[0] = 0x04;  /* Register address */
+	cmd[1] = 0x00;
+	cmd[2] = 0x0e;
+	cmd[3] = 0x00;
+	cmd[4] = 0x40;  /* Report id 40h */
+	cmd[5] = 0x00;
+	cmd[6] = GEN5_SOP_KEY;
+	cmd[7] = 0x3d;  /* Read application image command code */
+	cmd[8] = 0x03;
+	cmd[9] = 0x00;
+	offset = row_num * CYAPA_TSG_FW_ROW_SIZE -
+			CYAPA_TSG_START_OF_APPLICATION;
+	put_unaligned_le16(offset, &cmd[10]);
+	cmd[12] = CYAPA_TSG_IMG_READ_SIZE;
+	cmd_crc = crc_itu_t(0xffff, &cmd[6], 7);
+	put_unaligned_le16(cmd_crc, &cmd[13]);  /* CRC[15:0] */
+	cmd[15] = GEN5_EOP_KEY;  /* EOP = 17h */
+	cmd_len = 16;
+
+	resp_len = CYAPA_TSG_IMG_READ_SIZE + GEN5_MIN_BL_RESP_LENGTH;
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, cmd_len,
+			resp_data, &resp_len,
+			50, cyapa_gen5_sort_tsg_pip_bl_resp_data, true);
+	if (resp_len != (CYAPA_TSG_IMG_READ_SIZE + GEN5_MIN_BL_RESP_LENGTH) ||
+			ret || resp_data[2] != GEN5_BL_RESP_REPORT_ID ||
+			!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5]))
+		return (ret < 0) ? ret : -EAGAIN;
+
+	/* Copy first 64 bytes in the row. */
+	memcpy(&fw_img_record->record_data[0], &resp_data[8],
+			CYAPA_TSG_IMG_READ_SIZE);
+
+	if (row_num == CYAPA_TSG_IMG_APP_INTEGRITY_ROW_NUM) {
+		/* Last row's rest 64 bytes are bootloader metadata,
+		 * it's not allowed to be read out, will respond with error. */
+		memset(&fw_img_record->record_data[CYAPA_TSG_IMG_READ_SIZE],
+			0, CYAPA_TSG_IMG_READ_SIZE);
+		goto skip_last_row;
+	}
+
+	/* Read next 64 bytes in the row. */
+	offset = offset + CYAPA_TSG_IMG_READ_SIZE;
+	put_unaligned_le16(offset, &cmd[10]);
+	cmd_crc = crc_itu_t(0xffff, &cmd[6], 7);
+	put_unaligned_le16(cmd_crc, &cmd[13]);  /* CRC[15:0] */
+	ret = cyapa_i2c_pip_cmd_irq_sync(cyapa,
+			cmd, cmd_len,
+			resp_data, &resp_len,
+			500, cyapa_gen5_sort_tsg_pip_bl_resp_data, true);
+	if (resp_len != (CYAPA_TSG_IMG_READ_SIZE + GEN5_MIN_BL_RESP_LENGTH) ||
+			ret || resp_data[2] != GEN5_BL_RESP_REPORT_ID ||
+			!GEN5_CMD_COMPLETE_SUCCESS(resp_data[5]))
+		return (ret < 0) ? ret : -EAGAIN;
+
+	/* Copy last 64 bytes in the row. */
+	memcpy(&fw_img_record->record_data[CYAPA_TSG_IMG_READ_SIZE],
+		&resp_data[8], CYAPA_TSG_IMG_READ_SIZE);
+
+skip_last_row:
+	fw_img_record->flash_array_id = 0;
+	put_unaligned_be16(row_num, &fw_img_record->row_number);
+	put_unaligned_be16(CYAPA_TSG_FW_ROW_SIZE, &fw_img_record->record_len);
+
+	return 0;
+}
+
+static int cyapa_gen5_read_fw(struct cyapa *cyapa)
+{
+	int ret;
+	int fw_img_head_size;
+	int fw_img_record_size;
+	int fw_img_size;
+	int row_index;
+	int array_index;
+	u32 img_start;
+	u16 img_len;
+	u16 img_start_row;
+	u16 img_end_row;
+	struct cyapa_tsg_bin_image_data_record app_integrity;
+	u8 *record_data;
+
+	ret = cyapa_gen5_bl_enter(cyapa);
+	if (ret)
+		return ret;
+
+	cyapa_empty_pip_output_data(cyapa, NULL, NULL, NULL);
+
+	fw_img_head_size = sizeof(struct cyapa_tsg_bin_image_head);
+	fw_img_record_size = sizeof(struct cyapa_tsg_bin_image_data_record);
+
+	/* Read app integrity block data. */
+	row_index = CYAPA_TSG_IMG_APP_INTEGRITY_ROW_NUM;
+	ret = cyapa_gen5_read_fw_bytes(cyapa, row_index, (u8 *)&app_integrity);
+	if (ret)
+		return ret;
+	img_start = get_unaligned_le32(&app_integrity.record_data[16]);
+	img_len = get_unaligned_le16(&app_integrity.record_data[20]);
+	if ((img_start + img_len) % CYAPA_TSG_FW_ROW_SIZE)
+		return -EINVAL;
+	img_start_row = img_start / CYAPA_TSG_FW_ROW_SIZE;
+	img_end_row = (img_start + img_len) / CYAPA_TSG_FW_ROW_SIZE - 1;
+
+	/* Allocate memory for image. */
+	fw_img_size = fw_img_head_size +
+			(img_end_row -  img_start_row + 2) * fw_img_record_size;
+	cyapa->fw_image = cyapa->fw_image ? cyapa->fw_image :
+		devm_kzalloc(&cyapa->client->dev, fw_img_size, GFP_KERNEL);
+	if (!cyapa->fw_image)
+			return -ENOMEM;
+
+	/* Set image head data. */
+	memcpy(cyapa->fw_image, &cyapa->fw_img_head, fw_img_head_size);
+
+	/* Read image blocks. */
+	for (row_index = img_start_row, array_index = 0;
+			row_index <= img_end_row;
+			row_index++, array_index++) {
+		record_data = &cyapa->fw_image[fw_img_head_size +
+				array_index * fw_img_record_size];
+		ret = cyapa_gen5_read_fw_bytes(cyapa, row_index, record_data);
+		if (ret)
+			return ret;
+	}
+
+	/* Append last app integrity block data. */
+	record_data = &cyapa->fw_image[fw_img_head_size +
+				array_index * fw_img_record_size];
+	memcpy(record_data, &app_integrity, fw_img_record_size);
+
+	cyapa->fw_image_size = fw_img_size;
+	return 0;
+}
+
 static int cyapa_gen5_do_fw_update(struct cyapa *cyapa,
 		const struct firmware *fw)
 {
@@ -2276,6 +2423,11 @@ static int cyapa_gen5_get_query_data(struct cyapa *cyapa)
 	if (ret || resp_len < sizeof(resp_data))
 		return ret;
 
+	cyapa->fw_img_head.head_size =
+		sizeof(struct cyapa_tsg_bin_image_head) - 1;
+	memcpy(&cyapa->fw_img_head.ttda_driver_major_version,
+		&resp_data[5], cyapa->fw_img_head.head_size);
+
 	product_family = get_unaligned_le16(&resp_data[7]);
 	if ((product_family & GEN5_PRODUCT_FAMILY_MASK) !=
 		GEN5_PRODUCT_FAMILY_TRACKPAD)
@@ -2609,6 +2761,8 @@ const struct cyapa_dev_ops cyapa_gen5_ops = {
 	.show_baseline = cyapa_gen5_show_baseline,
 	.calibrate_store = cyapa_gen5_do_calibrate,
 
+	.read_fw = cyapa_gen5_read_fw,
+
 	.initialize = cyapa_gen5_initialize,
 
 	.state_parse = cyapa_gen5_state_parse,
Signature · 1 line
1.9.1
author
#30 lore raw

PATCH v9 17/18 input: cyapa: add read sensors raw data debugfs interface support

In reply to Dudley Du

Add read sensors' raw data from trackpad device interface supported in cyapa driver through debugfs raw_data interface.
Through this interface, user can read difference count map of each sensors directly from trackpad device (some customers want). And it's useful to help users to find out the root cause when there is performance gap happened. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa.c | 89 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/input/mouse/cyapa.h |  4 ++
 2 files changed, 93 insertions(+)
drivers/input/mouse/cyapa.c +89 -0
index bc1d75a..e200beb 100644
@@ -577,6 +577,91 @@ static const struct file_operations cyapa_read_fw_fops = {
 	.read = cyapa_debugfs_read_fw
 };
 
+static int cyapa_debugfs_raw_data_open(struct inode *inode, struct file *file)
+{
+	struct cyapa *cyapa = inode->i_private;
+	int ret;
+
+	if (!cyapa)
+		return -ENODEV;
+
+	/* Start to be supported after Gen5 trackpad devices. */
+	if (cyapa->gen < CYAPA_GEN5)
+		return -ENOTSUPP;
+
+	ret = mutex_lock_interruptible(&cyapa->debugfs_mutex);
+	if (ret)
+		return ret;
+
+	if (!get_device(&cyapa->client->dev)) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	file->private_data = cyapa;
+out:
+	mutex_unlock(&cyapa->debugfs_mutex);
+	return ret;
+}
+
+static int cyapa_debugfs_raw_data_release(struct inode *inode,
+				struct file *file)
+{
+	struct cyapa *cyapa = file->private_data;
+	int ret;
+
+	if (!cyapa)
+		return 0;
+
+	ret = mutex_lock_interruptible(&cyapa->debugfs_mutex);
+	if (ret)
+		return ret;
+	file->private_data = NULL;
+	put_device(&cyapa->client->dev);
+	mutex_unlock(&cyapa->debugfs_mutex);
+
+	return 0;
+}
+
+/* Always return the sensors' latest raw data from trackpad device. */
+static ssize_t cyapa_debugfs_read_raw_data(struct file *file,
+				     char __user *buffer,
+				     size_t count, loff_t *ppos)
+{
+	int ret;
+	struct cyapa *cyapa = file->private_data;
+
+	ret = mutex_lock_interruptible(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (cyapa->ops->read_raw_data)
+		ret = cyapa->ops->read_raw_data(cyapa);
+	else
+		ret = -EPERM;
+	mutex_unlock(&cyapa->state_sync_lock);
+	if (ret)
+		return ret;
+
+	if (*ppos >= cyapa->tp_raw_data_size)
+		return 0;
+
+	if (count + *ppos > cyapa->tp_raw_data_size)
+		count = cyapa->tp_raw_data_size - *ppos;
+
+	if (copy_to_user(buffer, &cyapa->tp_raw_data[*ppos], count))
+		return -EFAULT;
+
+	*ppos += count;
+	return count;
+}
+
+static const struct file_operations cyapa_read_raw_data_fops = {
+	.open = cyapa_debugfs_raw_data_open,
+	.release = cyapa_debugfs_raw_data_release,
+	.read = cyapa_debugfs_read_raw_data
+};
+
 static int cyapa_debugfs_init(struct cyapa *cyapa)
 {
 	struct device *dev = &cyapa->client->dev;
@@ -595,6 +680,10 @@ static int cyapa_debugfs_init(struct cyapa *cyapa)
 	debugfs_create_file(CYAPA_DEBUGFS_READ_FW, S_IRUSR, cyapa->dentry_dev,
 			    cyapa, &cyapa_read_fw_fops);
 
+	if (cyapa->gen >= CYAPA_GEN5)
+		debugfs_create_file(CYAPA_DEBUGFS_RAW_DATA, S_IRUSR,
+			cyapa->dentry_dev, cyapa, &cyapa_read_raw_data_fops);
+
 	return 0;
 }
 
drivers/input/mouse/cyapa.h +4 -0
index 288c7de..75b0c33 100644
@@ -190,6 +190,7 @@ struct cyapa_dev_ops {
 			struct device_attribute *, const char *, size_t);
 
 	int (*read_fw)(struct cyapa *);
+	int (*read_raw_data)(struct cyapa *);
 
 	int (*initialize)(struct cyapa *cyapa);
 
@@ -298,6 +299,9 @@ struct cyapa {
 	struct cyapa_tsg_bin_image_head fw_img_head;
 	u8 *fw_image;
 	size_t fw_image_size;
+	/* Buffer to store sensors' raw data */
+	u8 *tp_raw_data;
+	size_t tp_raw_data_size;
 
 	const struct cyapa_dev_ops *ops;
 
Signature · 1 line
1.9.1
author
#31 lore raw

PATCH v9 18/18 input: cyapa: add gen5 trackpad device read raw data function support

In reply to Dudley Du

Add read raw data function supported for gen5 trackpad device, it can be used through debugfs raw_data interface. TEST=test on Chromebooks.

---
 drivers/input/mouse/cyapa_gen5.c | 109 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)
drivers/input/mouse/cyapa_gen5.c +109 -0
index 35334dc..bbc2e20 100644
@@ -2350,6 +2350,114 @@ resume_scanning:
 	return ret + err;
 }
 
+static int cyapa_gen5_read_raw_data(struct cyapa *cyapa)
+{
+	int ret, err;
+	int raw_cap_mutual_max, raw_cap_mutual_min, raw_cap_mutual_ave;
+	int raw_cap_self_max, raw_cap_self_min, raw_cap_self_ave;
+	int offset;
+	int data_size, max, min, ave;
+	ktime_t time_mono;
+
+	offset = 0;
+	if (!cyapa->tp_raw_data) {
+		if (cyapa->state != CYAPA_STATE_GEN5_APP ||
+			!cyapa->electrodes_x || !cyapa->electrodes_y)
+			return  -EINVAL;
+
+		cyapa->tp_raw_data_size = sizeof(s32) * (cyapa->electrodes_x *
+			cyapa->electrodes_y + cyapa->electrodes_x +
+			cyapa->electrodes_y) + GEN5_RAW_DATA_HEAD_SIZE;
+		/*
+		 * This buffer will be hold after used until the driver is
+		 * unloaded, the purpose of it is to improve the performace
+		 * to avoid frequently allocate and release the buffer.
+		 */
+		cyapa->tp_raw_data = devm_kzalloc(&cyapa->client->dev,
+					cyapa->tp_raw_data_size, GFP_KERNEL);
+		if (!cyapa->tp_raw_data)
+			return -ENOMEM;
+	}
+
+	/*
+	 * 1. Suspend Scanning.
+	 *
+	 * After suspend scanning, the raw data will not be updated,
+	 * so the time of the raw data is before scanning suspended.
+	 */
+	time_mono = ktime_get();
+	ret = cyapa_gen5_suspend_scanning(cyapa);
+	if (ret)
+		return ret;
+
+	/* 2. Get the correct electrodes_rx number. */
+	if (cyapa->electrodes_rx == 0) {
+		/*
+		 * Through the read global idac interface to get the Rx number.
+		 * this value is useful to the raw data map.
+		 */
+		data_size = 0;
+		err = cyapa_gen5_read_idac_data(cyapa,
+				GEN5_CMD_RETRIEVE_DATA_STRUCTURE,
+				GEN5_RETRIEVE_MUTUAL_PWC_DATA,
+				&data_size, &max, &min, &ave);
+		if (err || cyapa->electrodes_rx == 0)
+			goto resume_scanning;
+	}
+
+	/* 3. Execuate panel scan. It must be executed before read data. */
+	err = cyapa_gen5_execute_panel_scan(cyapa);
+	if (err)
+		goto resume_scanning;
+
+	/* 4. Retrieve panel scan, mutual cap raw data. */
+	offset = GEN5_RAW_DATA_HEAD_SIZE;
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_MUTUAL_DIFFCOUNT,
+				cyapa->electrodes_x * cyapa->electrodes_y,
+				&raw_cap_mutual_max, &raw_cap_mutual_min,
+				&raw_cap_mutual_ave,
+				cyapa->tp_raw_data + offset);
+	if (err)
+		goto resume_scanning;
+
+	offset += sizeof(s32) * cyapa->electrodes_x * cyapa->electrodes_y;
+
+	/* 5. Retrieve panel scan, self cap raw data. */
+	err = cyapa_gen5_read_panel_scan_raw_data(cyapa,
+				GEN5_CMD_RETRIEVE_PANEL_SCAN,
+				GEN5_PANEL_SCAN_SELF_DIFFCOUNT,
+				cyapa->electrodes_x + cyapa->electrodes_y,
+				&raw_cap_self_max, &raw_cap_self_min,
+				&raw_cap_self_ave,
+				cyapa->tp_raw_data + offset);
+	if (err)
+		goto resume_scanning;
+
+	offset += sizeof(s32) * (cyapa->electrodes_x + cyapa->electrodes_y);
+
+resume_scanning:
+	/* 6. Resume Scanning*/
+	ret = cyapa_gen5_resume_scanning(cyapa);
+	if (ret || err)
+		return ret ? ret : err;
+
+	*((struct timeval *)&cyapa->tp_raw_data[0]) =
+						ktime_to_timeval(time_mono);
+	cyapa->tp_raw_data[16] = (u8)cyapa->electrodes_x;
+	cyapa->tp_raw_data[17] = (u8)cyapa->electrodes_y;
+	cyapa->tp_raw_data[18] = (u8)cyapa->x_origin;
+	cyapa->tp_raw_data[19] = (u8)cyapa->y_origin;
+	cyapa->tp_raw_data[20] = (u8)sizeof(s32);
+	cyapa->tp_raw_data[21] = (u8)sizeof(s32);
+	cyapa->tp_raw_data[22] = (u8)cyapa->electrodes_rx;
+	cyapa->tp_raw_data[23] = 0;  /* Reserved. */
+
+	cyapa->tp_raw_data_size = offset;
+	return 0;
+}
+
 static bool cyapa_gen5_sort_system_info_data(struct cyapa *cyapa,
 		u8 *buf, int len)
 {
@@ -2762,6 +2870,7 @@ const struct cyapa_dev_ops cyapa_gen5_ops = {
 	.calibrate_store = cyapa_gen5_do_calibrate,
 
 	.read_fw = cyapa_gen5_read_fw,
+	.read_raw_data = cyapa_gen5_read_raw_data,
 
 	.initialize = cyapa_gen5_initialize,
 
Signature · 1 line
1.9.1