Skip to content

Commit a485923

Browse files
westeriJiri Kosina
authored andcommitted
HID: i2c-hid: Add support for ACPI GPIO interrupts
The HID over I2C specification allows to have the interrupt for a HID device to be GPIO instead of directly connected to the IO-APIC. Add support for this so that when the driver does not find proper interrupt number from the I2C client structure we check if it has ACPI GpioInt() resource listed in _CRS. If it is found we convert it to an interrupt number and use it instead. Signed-off-by: Mika Westerberg <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent c4bbb39 commit a485923

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

drivers/hid/i2c-hid/i2c-hid.c

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/mutex.h>
3838
#include <linux/acpi.h>
3939
#include <linux/of.h>
40+
#include <linux/gpio/consumer.h>
4041

4142
#include <linux/i2c/i2c-hid.h>
4243

@@ -144,6 +145,8 @@ struct i2c_hid {
144145
unsigned long flags; /* device flags */
145146

146147
wait_queue_head_t wait; /* For waiting the interrupt */
148+
struct gpio_desc *desc;
149+
int irq;
147150

148151
struct i2c_hid_platform_data pdata;
149152
};
@@ -785,16 +788,16 @@ static int i2c_hid_init_irq(struct i2c_client *client)
785788
struct i2c_hid *ihid = i2c_get_clientdata(client);
786789
int ret;
787790

788-
dev_dbg(&client->dev, "Requesting IRQ: %d\n", client->irq);
791+
dev_dbg(&client->dev, "Requesting IRQ: %d\n", ihid->irq);
789792

790-
ret = request_threaded_irq(client->irq, NULL, i2c_hid_irq,
793+
ret = request_threaded_irq(ihid->irq, NULL, i2c_hid_irq,
791794
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
792795
client->name, ihid);
793796
if (ret < 0) {
794797
dev_warn(&client->dev,
795798
"Could not register for %s interrupt, irq = %d,"
796799
" ret = %d\n",
797-
client->name, client->irq, ret);
800+
client->name, ihid->irq, ret);
798801

799802
return ret;
800803
}
@@ -841,6 +844,14 @@ static int i2c_hid_fetch_hid_descriptor(struct i2c_hid *ihid)
841844
}
842845

843846
#ifdef CONFIG_ACPI
847+
848+
/* Default GPIO mapping */
849+
static const struct acpi_gpio_params i2c_hid_irq_gpio = { 0, 0, true };
850+
static const struct acpi_gpio_mapping i2c_hid_acpi_gpios[] = {
851+
{ "gpios", &i2c_hid_irq_gpio, 1 },
852+
{ },
853+
};
854+
844855
static int i2c_hid_acpi_pdata(struct i2c_client *client,
845856
struct i2c_hid_platform_data *pdata)
846857
{
@@ -866,7 +877,7 @@ static int i2c_hid_acpi_pdata(struct i2c_client *client,
866877
pdata->hid_descriptor_address = obj->integer.value;
867878
ACPI_FREE(obj);
868879

869-
return 0;
880+
return acpi_dev_add_driver_gpios(adev, i2c_hid_acpi_gpios);
870881
}
871882

872883
static const struct acpi_device_id i2c_hid_acpi_match[] = {
@@ -930,12 +941,6 @@ static int i2c_hid_probe(struct i2c_client *client,
930941

931942
dbg_hid("HID probe called for i2c 0x%02x\n", client->addr);
932943

933-
if (!client->irq) {
934-
dev_err(&client->dev,
935-
"HID over i2c has not been provided an Int IRQ\n");
936-
return -EINVAL;
937-
}
938-
939944
ihid = kzalloc(sizeof(struct i2c_hid), GFP_KERNEL);
940945
if (!ihid)
941946
return -ENOMEM;
@@ -955,6 +960,23 @@ static int i2c_hid_probe(struct i2c_client *client,
955960
ihid->pdata = *platform_data;
956961
}
957962

963+
if (client->irq > 0) {
964+
ihid->irq = client->irq;
965+
} else if (ACPI_COMPANION(&client->dev)) {
966+
ihid->desc = gpiod_get(&client->dev, NULL, GPIOD_IN);
967+
if (IS_ERR(ihid->desc)) {
968+
dev_err(&client->dev, "Failed to get GPIO interrupt\n");
969+
return PTR_ERR(ihid->desc);
970+
}
971+
972+
ihid->irq = gpiod_to_irq(ihid->desc);
973+
if (ihid->irq < 0) {
974+
gpiod_put(ihid->desc);
975+
dev_err(&client->dev, "Failed to convert GPIO to IRQ\n");
976+
return ihid->irq;
977+
}
978+
}
979+
958980
i2c_set_clientdata(client, ihid);
959981

960982
ihid->client = client;
@@ -1017,13 +1039,16 @@ static int i2c_hid_probe(struct i2c_client *client,
10171039
hid_destroy_device(hid);
10181040

10191041
err_irq:
1020-
free_irq(client->irq, ihid);
1042+
free_irq(ihid->irq, ihid);
10211043

10221044
err_pm:
10231045
pm_runtime_put_noidle(&client->dev);
10241046
pm_runtime_disable(&client->dev);
10251047

10261048
err:
1049+
if (ihid->desc)
1050+
gpiod_put(ihid->desc);
1051+
10271052
i2c_hid_free_buffers(ihid);
10281053
kfree(ihid);
10291054
return ret;
@@ -1042,13 +1067,18 @@ static int i2c_hid_remove(struct i2c_client *client)
10421067
hid = ihid->hid;
10431068
hid_destroy_device(hid);
10441069

1045-
free_irq(client->irq, ihid);
1070+
free_irq(ihid->irq, ihid);
10461071

10471072
if (ihid->bufsize)
10481073
i2c_hid_free_buffers(ihid);
10491074

1075+
if (ihid->desc)
1076+
gpiod_put(ihid->desc);
1077+
10501078
kfree(ihid);
10511079

1080+
acpi_dev_remove_driver_gpios(ACPI_COMPANION(&client->dev));
1081+
10521082
return 0;
10531083
}
10541084

@@ -1060,9 +1090,9 @@ static int i2c_hid_suspend(struct device *dev)
10601090
struct hid_device *hid = ihid->hid;
10611091
int ret = 0;
10621092

1063-
disable_irq(client->irq);
1093+
disable_irq(ihid->irq);
10641094
if (device_may_wakeup(&client->dev))
1065-
enable_irq_wake(client->irq);
1095+
enable_irq_wake(ihid->irq);
10661096

10671097
if (hid->driver && hid->driver->suspend)
10681098
ret = hid->driver->suspend(hid, PMSG_SUSPEND);
@@ -1080,13 +1110,13 @@ static int i2c_hid_resume(struct device *dev)
10801110
struct i2c_hid *ihid = i2c_get_clientdata(client);
10811111
struct hid_device *hid = ihid->hid;
10821112

1083-
enable_irq(client->irq);
1113+
enable_irq(ihid->irq);
10841114
ret = i2c_hid_hwreset(client);
10851115
if (ret)
10861116
return ret;
10871117

10881118
if (device_may_wakeup(&client->dev))
1089-
disable_irq_wake(client->irq);
1119+
disable_irq_wake(ihid->irq);
10901120

10911121
if (hid->driver && hid->driver->reset_resume) {
10921122
ret = hid->driver->reset_resume(hid);
@@ -1101,17 +1131,19 @@ static int i2c_hid_resume(struct device *dev)
11011131
static int i2c_hid_runtime_suspend(struct device *dev)
11021132
{
11031133
struct i2c_client *client = to_i2c_client(dev);
1134+
struct i2c_hid *ihid = i2c_get_clientdata(client);
11041135

11051136
i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
1106-
disable_irq(client->irq);
1137+
disable_irq(ihid->irq);
11071138
return 0;
11081139
}
11091140

11101141
static int i2c_hid_runtime_resume(struct device *dev)
11111142
{
11121143
struct i2c_client *client = to_i2c_client(dev);
1144+
struct i2c_hid *ihid = i2c_get_clientdata(client);
11131145

1114-
enable_irq(client->irq);
1146+
enable_irq(ihid->irq);
11151147
i2c_hid_set_power(client, I2C_HID_PWR_ON);
11161148
return 0;
11171149
}

0 commit comments

Comments
 (0)