| .. | .. |
|---|
| 7 | 7 | */ |
|---|
| 8 | 8 | |
|---|
| 9 | 9 | #include <linux/acpi.h> |
|---|
| 10 | +#include <linux/bits.h> |
|---|
| 10 | 11 | #include <linux/i2c.h> |
|---|
| 11 | 12 | #include <linux/interrupt.h> |
|---|
| 12 | 13 | #include <linux/kernel.h> |
|---|
| 13 | 14 | #include <linux/module.h> |
|---|
| 14 | 15 | #include <linux/platform_device.h> |
|---|
| 16 | +#include <linux/types.h> |
|---|
| 17 | + |
|---|
| 18 | +#define IRQ_RESOURCE_TYPE GENMASK(1, 0) |
|---|
| 19 | +#define IRQ_RESOURCE_NONE 0 |
|---|
| 20 | +#define IRQ_RESOURCE_GPIO 1 |
|---|
| 21 | +#define IRQ_RESOURCE_APIC 2 |
|---|
| 15 | 22 | |
|---|
| 16 | 23 | struct i2c_inst_data { |
|---|
| 17 | 24 | const char *type; |
|---|
| 18 | | - int gpio_irq_idx; |
|---|
| 25 | + unsigned int flags; |
|---|
| 26 | + int irq_idx; |
|---|
| 19 | 27 | }; |
|---|
| 20 | 28 | |
|---|
| 21 | 29 | struct i2c_multi_inst_data { |
|---|
| 22 | 30 | int num_clients; |
|---|
| 23 | | - struct i2c_client *clients[0]; |
|---|
| 31 | + struct i2c_client *clients[]; |
|---|
| 24 | 32 | }; |
|---|
| 33 | + |
|---|
| 34 | +static int i2c_multi_inst_count(struct acpi_resource *ares, void *data) |
|---|
| 35 | +{ |
|---|
| 36 | + struct acpi_resource_i2c_serialbus *sb; |
|---|
| 37 | + int *count = data; |
|---|
| 38 | + |
|---|
| 39 | + if (i2c_acpi_get_i2c_resource(ares, &sb)) |
|---|
| 40 | + *count = *count + 1; |
|---|
| 41 | + |
|---|
| 42 | + return 1; |
|---|
| 43 | +} |
|---|
| 44 | + |
|---|
| 45 | +static int i2c_multi_inst_count_resources(struct acpi_device *adev) |
|---|
| 46 | +{ |
|---|
| 47 | + LIST_HEAD(r); |
|---|
| 48 | + int count = 0; |
|---|
| 49 | + int ret; |
|---|
| 50 | + |
|---|
| 51 | + ret = acpi_dev_get_resources(adev, &r, i2c_multi_inst_count, &count); |
|---|
| 52 | + if (ret < 0) |
|---|
| 53 | + return ret; |
|---|
| 54 | + |
|---|
| 55 | + acpi_dev_free_resource_list(&r); |
|---|
| 56 | + return count; |
|---|
| 57 | +} |
|---|
| 25 | 58 | |
|---|
| 26 | 59 | static int i2c_multi_inst_probe(struct platform_device *pdev) |
|---|
| 27 | 60 | { |
|---|
| .. | .. |
|---|
| 44 | 77 | adev = ACPI_COMPANION(dev); |
|---|
| 45 | 78 | |
|---|
| 46 | 79 | /* Count number of clients to instantiate */ |
|---|
| 47 | | - for (i = 0; inst_data[i].type; i++) {} |
|---|
| 80 | + ret = i2c_multi_inst_count_resources(adev); |
|---|
| 81 | + if (ret < 0) |
|---|
| 82 | + return ret; |
|---|
| 48 | 83 | |
|---|
| 49 | | - multi = devm_kmalloc(dev, |
|---|
| 50 | | - offsetof(struct i2c_multi_inst_data, clients[i]), |
|---|
| 51 | | - GFP_KERNEL); |
|---|
| 84 | + multi = devm_kmalloc(dev, struct_size(multi, clients, ret), GFP_KERNEL); |
|---|
| 52 | 85 | if (!multi) |
|---|
| 53 | 86 | return -ENOMEM; |
|---|
| 54 | 87 | |
|---|
| 55 | | - multi->num_clients = i; |
|---|
| 88 | + multi->num_clients = ret; |
|---|
| 56 | 89 | |
|---|
| 57 | | - for (i = 0; i < multi->num_clients; i++) { |
|---|
| 90 | + for (i = 0; i < multi->num_clients && inst_data[i].type; i++) { |
|---|
| 58 | 91 | memset(&board_info, 0, sizeof(board_info)); |
|---|
| 59 | 92 | strlcpy(board_info.type, inst_data[i].type, I2C_NAME_SIZE); |
|---|
| 60 | | - snprintf(name, sizeof(name), "%s-%s", match->id, |
|---|
| 61 | | - inst_data[i].type); |
|---|
| 93 | + snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev), |
|---|
| 94 | + inst_data[i].type, i); |
|---|
| 62 | 95 | board_info.dev_name = name; |
|---|
| 63 | | - board_info.irq = 0; |
|---|
| 64 | | - if (inst_data[i].gpio_irq_idx != -1) { |
|---|
| 65 | | - ret = acpi_dev_gpio_irq_get(adev, |
|---|
| 66 | | - inst_data[i].gpio_irq_idx); |
|---|
| 96 | + switch (inst_data[i].flags & IRQ_RESOURCE_TYPE) { |
|---|
| 97 | + case IRQ_RESOURCE_GPIO: |
|---|
| 98 | + ret = acpi_dev_gpio_irq_get(adev, inst_data[i].irq_idx); |
|---|
| 67 | 99 | if (ret < 0) { |
|---|
| 68 | 100 | dev_err(dev, "Error requesting irq at index %d: %d\n", |
|---|
| 69 | | - inst_data[i].gpio_irq_idx, ret); |
|---|
| 101 | + inst_data[i].irq_idx, ret); |
|---|
| 70 | 102 | goto error; |
|---|
| 71 | 103 | } |
|---|
| 72 | 104 | board_info.irq = ret; |
|---|
| 105 | + break; |
|---|
| 106 | + case IRQ_RESOURCE_APIC: |
|---|
| 107 | + ret = platform_get_irq(pdev, inst_data[i].irq_idx); |
|---|
| 108 | + if (ret < 0) { |
|---|
| 109 | + dev_dbg(dev, "Error requesting irq at index %d: %d\n", |
|---|
| 110 | + inst_data[i].irq_idx, ret); |
|---|
| 111 | + goto error; |
|---|
| 112 | + } |
|---|
| 113 | + board_info.irq = ret; |
|---|
| 114 | + break; |
|---|
| 115 | + default: |
|---|
| 116 | + board_info.irq = 0; |
|---|
| 117 | + break; |
|---|
| 73 | 118 | } |
|---|
| 74 | 119 | multi->clients[i] = i2c_acpi_new_device(dev, i, &board_info); |
|---|
| 75 | | - if (!multi->clients[i]) { |
|---|
| 76 | | - dev_err(dev, "Error creating i2c-client, idx %d\n", i); |
|---|
| 77 | | - ret = -ENODEV; |
|---|
| 120 | + if (IS_ERR(multi->clients[i])) { |
|---|
| 121 | + ret = PTR_ERR(multi->clients[i]); |
|---|
| 122 | + if (ret != -EPROBE_DEFER) |
|---|
| 123 | + dev_err(dev, "Error creating i2c-client, idx %d\n", i); |
|---|
| 78 | 124 | goto error; |
|---|
| 79 | 125 | } |
|---|
| 126 | + } |
|---|
| 127 | + if (i < multi->num_clients) { |
|---|
| 128 | + dev_err(dev, "Error finding driver, idx %d\n", i); |
|---|
| 129 | + ret = -ENODEV; |
|---|
| 130 | + goto error; |
|---|
| 80 | 131 | } |
|---|
| 81 | 132 | |
|---|
| 82 | 133 | platform_set_drvdata(pdev, multi); |
|---|
| .. | .. |
|---|
| 101 | 152 | } |
|---|
| 102 | 153 | |
|---|
| 103 | 154 | static const struct i2c_inst_data bsg1160_data[] = { |
|---|
| 104 | | - { "bmc150_accel", 0 }, |
|---|
| 105 | | - { "bmc150_magn", -1 }, |
|---|
| 106 | | - { "bmg160", -1 }, |
|---|
| 155 | + { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 }, |
|---|
| 156 | + { "bmc150_magn" }, |
|---|
| 157 | + { "bmg160" }, |
|---|
| 107 | 158 | {} |
|---|
| 108 | 159 | }; |
|---|
| 160 | + |
|---|
| 161 | +static const struct i2c_inst_data bsg2150_data[] = { |
|---|
| 162 | + { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 }, |
|---|
| 163 | + { "bmc150_magn" }, |
|---|
| 164 | + /* The resources describe a 3th client, but it is not really there. */ |
|---|
| 165 | + { "bsg2150_dummy_dev" }, |
|---|
| 166 | + {} |
|---|
| 167 | +}; |
|---|
| 168 | + |
|---|
| 169 | +/* |
|---|
| 170 | + * Device with _HID INT3515 (TI PD controllers) has some unresolved interrupt |
|---|
| 171 | + * issues. The most common problem seen is interrupt flood. |
|---|
| 172 | + * |
|---|
| 173 | + * There are at least two known causes. Firstly, on some boards, the |
|---|
| 174 | + * I2CSerialBus resource index does not match the Interrupt resource, i.e. they |
|---|
| 175 | + * are not one-to-one mapped like in the array below. Secondly, on some boards |
|---|
| 176 | + * the IRQ line from the PD controller is not actually connected at all. But the |
|---|
| 177 | + * interrupt flood is also seen on some boards where those are not a problem, so |
|---|
| 178 | + * there are some other problems as well. |
|---|
| 179 | + * |
|---|
| 180 | + * Because of the issues with the interrupt, the device is disabled for now. If |
|---|
| 181 | + * you wish to debug the issues, uncomment the below, and add an entry for the |
|---|
| 182 | + * INT3515 device to the i2c_multi_instance_ids table. |
|---|
| 183 | + * |
|---|
| 184 | + * static const struct i2c_inst_data int3515_data[] = { |
|---|
| 185 | + * { "tps6598x", IRQ_RESOURCE_APIC, 0 }, |
|---|
| 186 | + * { "tps6598x", IRQ_RESOURCE_APIC, 1 }, |
|---|
| 187 | + * { "tps6598x", IRQ_RESOURCE_APIC, 2 }, |
|---|
| 188 | + * { "tps6598x", IRQ_RESOURCE_APIC, 3 }, |
|---|
| 189 | + * { } |
|---|
| 190 | + * }; |
|---|
| 191 | + */ |
|---|
| 109 | 192 | |
|---|
| 110 | 193 | /* |
|---|
| 111 | 194 | * Note new device-ids must also be added to i2c_multi_instantiate_ids in |
|---|
| .. | .. |
|---|
| 113 | 196 | */ |
|---|
| 114 | 197 | static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = { |
|---|
| 115 | 198 | { "BSG1160", (unsigned long)bsg1160_data }, |
|---|
| 199 | + { "BSG2150", (unsigned long)bsg2150_data }, |
|---|
| 116 | 200 | { } |
|---|
| 117 | 201 | }; |
|---|
| 118 | 202 | MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids); |
|---|