i2c: allow attaching IRQ resources to i2c_board_info
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 1 Mar 2017 19:45:51 +0000 (11:45 -0800)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Sat, 1 Apr 2017 16:36:19 +0000 (09:36 -0700)
Simple integer for interrupt number is not expressive enough, as it does
not convey interrupt trigger type that should be used. Let's allow
attaching array of resources to the board info and have i2c core parse
first IRQ resource and set up interrupt trigger as needed.

Reviewed-by: Wolfram Sang <wsa@the-dreams.de>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/i2c/i2c-boardinfo.c
drivers/i2c/i2c-core.c
include/linux/i2c.h

index 0e285c68b2ff20924caa99aba707f9a7599fb868..31186ead5a40717dc491753eb1ebd37f7a1b0e52 100644 (file)
@@ -90,6 +90,18 @@ int i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsig
                        }
                }
 
+               if (info->resources) {
+                       devinfo->board_info.resources =
+                               kmemdup(info->resources,
+                                       info->num_resources *
+                                               sizeof(*info->resources),
+                                       GFP_KERNEL);
+                       if (!devinfo->board_info.resources) {
+                               status = -ENOMEM;
+                               break;
+                       }
+               }
+
                list_add_tail(&devinfo->list, &__i2c_board_list);
        }
 
index bf7892e3b0c898f16b65813fa5c811edd2a15d3a..679a31fcb6d6f4c4cd3e96860ec87c82f2ebacc2 100644 (file)
@@ -1278,6 +1278,32 @@ static void i2c_dev_set_name(struct i2c_adapter *adap,
                     i2c_encode_flags_to_addr(client));
 }
 
+static int i2c_dev_irq_from_resources(const struct resource *resources,
+                                     unsigned int num_resources)
+{
+       struct irq_data *irqd;
+       int i;
+
+       for (i = 0; i < num_resources; i++) {
+               const struct resource *r = &resources[i];
+
+               if (resource_type(r) != IORESOURCE_IRQ)
+                       continue;
+
+               if (r->flags & IORESOURCE_BITS) {
+                       irqd = irq_get_irq_data(r->start);
+                       if (!irqd)
+                               break;
+
+                       irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
+               }
+
+               return r->start;
+       }
+
+       return 0;
+}
+
 /**
  * i2c_new_device - instantiate an i2c device
  * @adap: the adapter managing the device
@@ -1313,7 +1339,11 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 
        client->flags = info->flags;
        client->addr = info->addr;
+
        client->irq = info->irq;
+       if (!client->irq)
+               client->irq = i2c_dev_irq_from_resources(info->resources,
+                                                        info->num_resources);
 
        strlcpy(client->name, info->type, sizeof(client->name));
 
index 6366989d10018362ad21ce1c5fdcc2d7d049f066..c5bd8b8daf977e09ee80a202f7316746e651ea65 100644 (file)
@@ -304,6 +304,8 @@ static inline int i2c_slave_event(struct i2c_client *client,
  * @of_node: pointer to OpenFirmware device node
  * @fwnode: device node supplied by the platform firmware
  * @properties: additional device properties for the device
+ * @resources: resources associated with the device
+ * @num_resources: number of resources in the @resources array
  * @irq: stored in i2c_client.irq
  *
  * I2C doesn't actually support hardware probing, although controllers and
@@ -326,6 +328,8 @@ struct i2c_board_info {
        struct device_node *of_node;
        struct fwnode_handle *fwnode;
        const struct property_entry *properties;
+       const struct resource *resources;
+       unsigned int    num_resources;
        int             irq;
 };