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