forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/platform/x86/i2c-multi-instantiate.c
....@@ -7,21 +7,54 @@
77 */
88
99 #include <linux/acpi.h>
10
+#include <linux/bits.h>
1011 #include <linux/i2c.h>
1112 #include <linux/interrupt.h>
1213 #include <linux/kernel.h>
1314 #include <linux/module.h>
1415 #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
1522
1623 struct i2c_inst_data {
1724 const char *type;
18
- int gpio_irq_idx;
25
+ unsigned int flags;
26
+ int irq_idx;
1927 };
2028
2129 struct i2c_multi_inst_data {
2230 int num_clients;
23
- struct i2c_client *clients[0];
31
+ struct i2c_client *clients[];
2432 };
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
+}
2558
2659 static int i2c_multi_inst_probe(struct platform_device *pdev)
2760 {
....@@ -44,39 +77,57 @@
4477 adev = ACPI_COMPANION(dev);
4578
4679 /* 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;
4883
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);
5285 if (!multi)
5386 return -ENOMEM;
5487
55
- multi->num_clients = i;
88
+ multi->num_clients = ret;
5689
57
- for (i = 0; i < multi->num_clients; i++) {
90
+ for (i = 0; i < multi->num_clients && inst_data[i].type; i++) {
5891 memset(&board_info, 0, sizeof(board_info));
5992 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);
6295 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);
6799 if (ret < 0) {
68100 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);
70102 goto error;
71103 }
72104 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;
73118 }
74119 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);
78124 goto error;
79125 }
126
+ }
127
+ if (i < multi->num_clients) {
128
+ dev_err(dev, "Error finding driver, idx %d\n", i);
129
+ ret = -ENODEV;
130
+ goto error;
80131 }
81132
82133 platform_set_drvdata(pdev, multi);
....@@ -101,11 +152,43 @@
101152 }
102153
103154 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" },
107158 {}
108159 };
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
+ */
109192
110193 /*
111194 * Note new device-ids must also be added to i2c_multi_instantiate_ids in
....@@ -113,6 +196,7 @@
113196 */
114197 static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = {
115198 { "BSG1160", (unsigned long)bsg1160_data },
199
+ { "BSG2150", (unsigned long)bsg2150_data },
116200 { }
117201 };
118202 MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids);