Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / i2c-multi-instantiate.c
blobb457b0babde390a9b3108103f81c9817d64ca834
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.
6 * Copyright 2018 Hans de Goede <hdegoede@redhat.com>
7 */
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 {
25 const char *type;
26 unsigned int flags;
27 int irq_idx;
30 struct i2c_multi_inst_data {
31 int num_clients;
32 struct i2c_client *clients[];
35 static int i2c_multi_inst_count(struct acpi_resource *ares, void *data)
37 struct acpi_resource_i2c_serialbus *sb;
38 int *count = data;
40 if (i2c_acpi_get_i2c_resource(ares, &sb))
41 *count = *count + 1;
43 return 1;
46 static int i2c_multi_inst_count_resources(struct acpi_device *adev)
48 LIST_HEAD(r);
49 int count = 0;
50 int ret;
52 ret = acpi_dev_get_resources(adev, &r, i2c_multi_inst_count, &count);
53 if (ret < 0)
54 return ret;
56 acpi_dev_free_resource_list(&r);
57 return count;
60 static int i2c_multi_inst_probe(struct platform_device *pdev)
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;
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;
76 adev = ACPI_COMPANION(dev);
78 /* Count number of clients to instantiate */
79 ret = i2c_multi_inst_count_resources(adev);
80 if (ret < 0)
81 return ret;
83 multi = devm_kmalloc(dev, struct_size(multi, clients, ret), GFP_KERNEL);
84 if (!multi)
85 return -ENOMEM;
87 multi->num_clients = ret;
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;
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;
112 board_info.irq = ret;
113 break;
114 default:
115 board_info.irq = 0;
116 break;
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;
125 if (i < multi->num_clients) {
126 dev_err(dev, "Error finding driver, idx %d\n", i);
127 ret = -ENODEV;
128 goto error;
131 platform_set_drvdata(pdev, multi);
132 return 0;
134 error:
135 while (--i >= 0)
136 i2c_unregister_device(multi->clients[i]);
138 return ret;
141 static int i2c_multi_inst_remove(struct platform_device *pdev)
143 struct i2c_multi_inst_data *multi = platform_get_drvdata(pdev);
144 int i;
146 for (i = 0; i < multi->num_clients; i++)
147 i2c_unregister_device(multi->clients[i]);
149 return 0;
152 static const struct i2c_inst_data bsg1160_data[] = {
153 { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
154 { "bmc150_magn" },
155 { "bmg160" },
159 static 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" },
167 static const struct i2c_inst_data int3515_data[] = {
168 { "tps6598x", IRQ_RESOURCE_APIC, 0 },
169 { "tps6598x", IRQ_RESOURCE_APIC, 1 },
170 { "tps6598x", IRQ_RESOURCE_APIC, 2 },
171 { "tps6598x", IRQ_RESOURCE_APIC, 3 },
176 * Note new device-ids must also be added to i2c_multi_instantiate_ids in
177 * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
179 static const struct acpi_device_id i2c_multi_inst_acpi_ids[] = {
180 { "BSG1160", (unsigned long)bsg1160_data },
181 { "BSG2150", (unsigned long)bsg2150_data },
182 { "INT3515", (unsigned long)int3515_data },
185 MODULE_DEVICE_TABLE(acpi, i2c_multi_inst_acpi_ids);
187 static struct platform_driver i2c_multi_inst_driver = {
188 .driver = {
189 .name = "I2C multi instantiate pseudo device driver",
190 .acpi_match_table = i2c_multi_inst_acpi_ids,
192 .probe = i2c_multi_inst_probe,
193 .remove = i2c_multi_inst_remove,
195 module_platform_driver(i2c_multi_inst_driver);
197 MODULE_DESCRIPTION("I2C multi instantiate pseudo device driver");
198 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
199 MODULE_LICENSE("GPL");