]> Pileus Git - ~andy/linux/blobdiff - drivers/hid/i2c-hid/i2c-hid.c
HID: i2c-hid: add DT bindings
[~andy/linux] / drivers / hid / i2c-hid / i2c-hid.c
index 2b1799a3b212d73e1bc9100be6092923a4bc8cb2..fc9d92cb3f3951d37224ef3d03e8e0f1987a93cf 100644 (file)
@@ -35,6 +35,7 @@
 #include <linux/hid.h>
 #include <linux/mutex.h>
 #include <linux/acpi.h>
+#include <linux/of.h>
 
 #include <linux/i2c/i2c-hid.h>
 
@@ -108,6 +109,7 @@ static const struct i2c_hid_cmd hid_reset_cmd =             { I2C_HID_CMD(0x01),
 static const struct i2c_hid_cmd hid_get_report_cmd =   { I2C_HID_CMD(0x02) };
 static const struct i2c_hid_cmd hid_set_report_cmd =   { I2C_HID_CMD(0x03) };
 static const struct i2c_hid_cmd hid_set_power_cmd =    { I2C_HID_CMD(0x08) };
+static const struct i2c_hid_cmd hid_no_cmd =           { .length = 0 };
 
 /*
  * These definitions are not used here, but are defined by the spec.
@@ -259,8 +261,11 @@ static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
 {
        struct i2c_hid *ihid = i2c_get_clientdata(client);
        u8 *args = ihid->argsbuf;
+       const struct i2c_hid_cmd * hidcmd = &hid_set_report_cmd;
        int ret;
        u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister);
+       u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister);
+       u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength);
 
        /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */
        u16 size =      2                       /* size */ +
@@ -278,8 +283,18 @@ static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
                reportID = 0x0F;
        }
 
-       args[index++] = dataRegister & 0xFF;
-       args[index++] = dataRegister >> 8;
+       /*
+        * use the data register for feature reports or if the device does not
+        * support the output register
+        */
+       if (reportType == 0x03 || maxOutputLength == 0) {
+               args[index++] = dataRegister & 0xFF;
+               args[index++] = dataRegister >> 8;
+       } else {
+               args[index++] = outputRegister & 0xFF;
+               args[index++] = outputRegister >> 8;
+               hidcmd = &hid_no_cmd;
+       }
 
        args[index++] = size & 0xFF;
        args[index++] = size >> 8;
@@ -289,7 +304,7 @@ static int i2c_hid_set_report(struct i2c_client *client, u8 reportType,
 
        memcpy(&args[index], buf, data_len);
 
-       ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID,
+       ret = __i2c_hid_command(client, hidcmd, reportID,
                reportType, args, args_len, NULL, 0);
        if (ret) {
                dev_err(&client->dev, "failed to set a report to device.\n");
@@ -919,6 +934,42 @@ static inline int i2c_hid_acpi_pdata(struct i2c_client *client,
 }
 #endif
 
+#ifdef CONFIG_OF
+static int i2c_hid_of_probe(struct i2c_client *client,
+               struct i2c_hid_platform_data *pdata)
+{
+       struct device *dev = &client->dev;
+       u32 val;
+       int ret;
+
+       ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
+       if (ret) {
+               dev_err(&client->dev, "HID register address not provided\n");
+               return -ENODEV;
+       }
+       if (val >> 16) {
+               dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
+                       val);
+               return -EINVAL;
+       }
+       pdata->hid_descriptor_address = val;
+
+       return 0;
+}
+
+static const struct of_device_id i2c_hid_of_match[] = {
+       { .compatible = "hid-over-i2c" },
+       {},
+};
+MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
+#else
+static inline int i2c_hid_of_probe(struct i2c_client *client,
+               struct i2c_hid_platform_data *pdata)
+{
+       return -ENODEV;
+}
+#endif
+
 static int i2c_hid_probe(struct i2c_client *client,
                         const struct i2c_device_id *dev_id)
 {
@@ -940,7 +991,11 @@ static int i2c_hid_probe(struct i2c_client *client,
        if (!ihid)
                return -ENOMEM;
 
-       if (!platform_data) {
+       if (client->dev.of_node) {
+               ret = i2c_hid_of_probe(client, &ihid->pdata);
+               if (ret)
+                       goto err;
+       } else if (!platform_data) {
                ret = i2c_hid_acpi_pdata(client, &ihid->pdata);
                if (ret) {
                        dev_err(&client->dev,
@@ -1081,6 +1136,7 @@ static struct i2c_driver i2c_hid_driver = {
                .owner  = THIS_MODULE,
                .pm     = &i2c_hid_pm,
                .acpi_match_table = ACPI_PTR(i2c_hid_acpi_match),
+               .of_match_table = of_match_ptr(i2c_hid_of_match),
        },
 
        .probe          = i2c_hid_probe,