1 // SPDX-License-Identifier: GPL-2.0+
3 * I2C multi-instantiate driver, pseudo driver to instantiate multiple
4 * i2c-clients from a single fwnode.
6 * Copyright 2018 Hans de Goede <hdegoede@redhat.com>
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>
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
24 struct i2c_inst_data {
30 struct i2c_multi_inst_data {
32 struct i2c_client *clients[];
35 static int i2c_multi_inst_probe(struct platform_device *pdev)
37 struct i2c_multi_inst_data *multi;
38 const struct i2c_inst_data *inst_data;
39 struct i2c_board_info board_info = {};
40 struct device *dev = &pdev->dev;
41 struct acpi_device *adev;
45 inst_data = device_get_match_data(dev);
47 dev_err(dev, "Error ACPI match data is missing\n");
51 adev = ACPI_COMPANION(dev);
53 /* Count number of clients to instantiate */
54 ret = i2c_acpi_client_count(adev);
58 multi = devm_kmalloc(dev, struct_size(multi, clients, ret), GFP_KERNEL);
62 multi->num_clients = ret;
64 for (i = 0; i < multi->num_clients && inst_data[i].type; i++) {
65 memset(&board_info, 0, sizeof(board_info));
66 strlcpy(board_info.type, inst_data[i].type, I2C_NAME_SIZE);
67 snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev),
68 inst_data[i].type, i);
69 board_info.dev_name = name;
70 switch (inst_data[i].flags & IRQ_RESOURCE_TYPE) {
71 case IRQ_RESOURCE_GPIO:
72 ret = acpi_dev_gpio_irq_get(adev, inst_data[i].irq_idx);
74 dev_err(dev, "Error requesting irq at index %d: %d\n",
75 inst_data[i].irq_idx, ret);
80 case IRQ_RESOURCE_APIC:
81 ret = platform_get_irq(pdev, inst_data[i].irq_idx);
83 dev_dbg(dev, "Error requesting irq at index %d: %d\n",
84 inst_data[i].irq_idx, ret);
93 multi->clients[i] = i2c_acpi_new_device(dev, i, &board_info);
94 if (IS_ERR(multi->clients[i])) {
95 ret = dev_err_probe(dev, PTR_ERR(multi->clients[i]),
96 "Error creating i2c-client, idx %d\n", i);
100 if (i < multi->num_clients) {
101 dev_err(dev, "Error finding driver, idx %d\n", i);
106 platform_set_drvdata(pdev, multi);
111 i2c_unregister_device(multi->clients[i]);
116 static int i2c_multi_inst_remove(struct platform_device *pdev)
118 struct i2c_multi_inst_data *multi = platform_get_drvdata(pdev);
121 for (i = 0; i < multi->num_clients; i++)
122 i2c_unregister_device(multi->clients[i]);
127 static const struct i2c_inst_data bsg1160_data[] = {
128 { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
134 static const struct i2c_inst_data bsg2150_data[] = {
135 { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
137 /* The resources describe a 3th client, but it is not really there. */
138 { "bsg2150_dummy_dev" },
143 * Device with _HID INT3515 (TI PD controllers) has some unresolved interrupt
144 * issues. The most common problem seen is interrupt flood.
146 * There are at least two known causes. Firstly, on some boards, the
147 * I2CSerialBus resource index does not match the Interrupt resource, i.e. they
148 * are not one-to-one mapped like in the array below. Secondly, on some boards
149 * the IRQ line from the PD controller is not actually connected at all. But the
150 * interrupt flood is also seen on some boards where those are not a problem, so
151 * there are some other problems as well.
153 * Because of the issues with the interrupt, the device is disabled for now. If
154 * you wish to debug the issues, uncomment the below, and add an entry for the
155 * INT3515 device to the i2c_multi_instance_ids table.
157 * static const struct i2c_inst_data int3515_data[] = {
158 * { "tps6598x", IRQ_RESOURCE_APIC, 0 },
159 * { "tps6598x", IRQ_RESOURCE_APIC, 1 },
160 * { "tps6598x", IRQ_RESOURCE_APIC, 2 },
161 * { "tps6598x", IRQ_RESOURCE_APIC, 3 },
167 * Note new device-ids must also be added to i2c_multi_instantiate_ids in
168 * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
170 static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = {
171 { "BSG1160", (unsigned long)bsg1160_data },
172 { "BSG2150", (unsigned long)bsg2150_data },
175 MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids);
177 static struct platform_driver i2c_multi_inst_driver = {
179 .name = "I2C multi instantiate pseudo device driver",
180 .acpi_match_table = i2c_multi_inst_acpi_ids,
182 .probe = i2c_multi_inst_probe,
183 .remove = i2c_multi_inst_remove,
185 module_platform_driver(i2c_multi_inst_driver);
187 MODULE_DESCRIPTION("I2C multi instantiate pseudo device driver");
188 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
189 MODULE_LICENSE("GPL");