1 // SPDX-License-Identifier: GPL-2.0+
3 * Serial multi-instantiate driver, pseudo driver to instantiate multiple
4 * client devices from a single fwnode.
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/spi/spi.h>
18 #include <linux/types.h>
20 #define IRQ_RESOURCE_TYPE GENMASK(1, 0)
21 #define IRQ_RESOURCE_NONE 0
22 #define IRQ_RESOURCE_GPIO 1
23 #define IRQ_RESOURCE_APIC 2
24 #define IRQ_RESOURCE_AUTO 3
39 enum smi_bus_type bus_type;
40 struct smi_instance instances[];
46 struct i2c_client **i2c_devs;
47 struct spi_device **spi_devs;
50 static int smi_get_irq(struct platform_device *pdev, struct acpi_device *adev,
51 const struct smi_instance *inst)
55 switch (inst->flags & IRQ_RESOURCE_TYPE) {
56 case IRQ_RESOURCE_AUTO:
57 ret = acpi_dev_gpio_irq_get(adev, inst->irq_idx);
59 dev_dbg(&pdev->dev, "Using gpio irq\n");
62 ret = platform_get_irq(pdev, inst->irq_idx);
64 dev_dbg(&pdev->dev, "Using platform irq\n");
68 case IRQ_RESOURCE_GPIO:
69 ret = acpi_dev_gpio_irq_get(adev, inst->irq_idx);
71 case IRQ_RESOURCE_APIC:
72 ret = platform_get_irq(pdev, inst->irq_idx);
78 return dev_err_probe(&pdev->dev, ret, "Error requesting irq at index %d\n",
84 static void smi_devs_unregister(struct smi *smi)
86 while (smi->i2c_num--)
87 i2c_unregister_device(smi->i2c_devs[smi->i2c_num]);
89 while (smi->spi_num--)
90 spi_unregister_device(smi->spi_devs[smi->spi_num]);
94 * smi_spi_probe - Instantiate multiple SPI devices from inst array
95 * @pdev: Platform device
96 * @smi: Internal struct for Serial multi instantiate driver
97 * @inst_array: Array of instances to probe
99 * Returns the number of SPI devices instantiate, Zero if none is found or a negative error code.
101 static int smi_spi_probe(struct platform_device *pdev, struct smi *smi,
102 const struct smi_instance *inst_array)
104 struct device *dev = &pdev->dev;
105 struct acpi_device *adev = ACPI_COMPANION(dev);
106 struct spi_controller *ctlr;
107 struct spi_device *spi_dev;
111 ret = acpi_spi_count_resources(adev);
119 smi->spi_devs = devm_kcalloc(dev, count, sizeof(*smi->spi_devs), GFP_KERNEL);
123 for (i = 0; i < count && inst_array[i].type; i++) {
125 spi_dev = acpi_spi_device_alloc(NULL, adev, i);
126 if (IS_ERR(spi_dev)) {
127 ret = dev_err_probe(dev, PTR_ERR(spi_dev), "failed to allocate SPI device %s from ACPI\n",
128 dev_name(&adev->dev));
132 ctlr = spi_dev->controller;
134 strscpy(spi_dev->modalias, inst_array[i].type, sizeof(spi_dev->modalias));
136 ret = smi_get_irq(pdev, adev, &inst_array[i]);
138 spi_dev_put(spi_dev);
143 snprintf(name, sizeof(name), "%s-%s-%s.%d", dev_name(&ctlr->dev), dev_name(dev),
144 inst_array[i].type, i);
145 spi_dev->dev.init_name = name;
147 ret = spi_add_device(spi_dev);
149 dev_err_probe(&ctlr->dev, ret, "failed to add SPI device %s from ACPI\n",
150 dev_name(&adev->dev));
151 spi_dev_put(spi_dev);
155 dev_dbg(dev, "SPI device %s using chip select %u", name,
156 spi_get_chipselect(spi_dev, 0));
158 smi->spi_devs[i] = spi_dev;
162 if (smi->spi_num < count) {
163 dev_dbg(dev, "Error finding driver, idx %d\n", i);
168 dev_info(dev, "Instantiated %d SPI devices.\n", smi->spi_num);
172 smi_devs_unregister(smi);
178 * smi_i2c_probe - Instantiate multiple I2C devices from inst array
179 * @pdev: Platform device
180 * @smi: Internal struct for Serial multi instantiate driver
181 * @inst_array: Array of instances to probe
183 * Returns the number of I2C devices instantiate, Zero if none is found or a negative error code.
185 static int smi_i2c_probe(struct platform_device *pdev, struct smi *smi,
186 const struct smi_instance *inst_array)
188 struct i2c_board_info board_info = {};
189 struct device *dev = &pdev->dev;
190 struct acpi_device *adev = ACPI_COMPANION(dev);
194 ret = i2c_acpi_client_count(adev);
202 smi->i2c_devs = devm_kcalloc(dev, count, sizeof(*smi->i2c_devs), GFP_KERNEL);
206 for (i = 0; i < count && inst_array[i].type; i++) {
207 memset(&board_info, 0, sizeof(board_info));
208 strscpy(board_info.type, inst_array[i].type, I2C_NAME_SIZE);
209 snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev), inst_array[i].type, i);
210 board_info.dev_name = name;
212 ret = smi_get_irq(pdev, adev, &inst_array[i]);
215 board_info.irq = ret;
217 smi->i2c_devs[i] = i2c_acpi_new_device(dev, i, &board_info);
218 if (IS_ERR(smi->i2c_devs[i])) {
219 ret = dev_err_probe(dev, PTR_ERR(smi->i2c_devs[i]),
220 "Error creating i2c-client, idx %d\n", i);
225 if (smi->i2c_num < count) {
226 dev_dbg(dev, "Error finding driver, idx %d\n", i);
231 dev_info(dev, "Instantiated %d I2C devices.\n", smi->i2c_num);
235 smi_devs_unregister(smi);
240 static int smi_probe(struct platform_device *pdev)
242 struct device *dev = &pdev->dev;
243 const struct smi_node *node;
247 node = device_get_match_data(dev);
249 dev_dbg(dev, "Error ACPI match data is missing\n");
253 smi = devm_kzalloc(dev, sizeof(*smi), GFP_KERNEL);
257 platform_set_drvdata(pdev, smi);
259 switch (node->bus_type) {
261 return smi_i2c_probe(pdev, smi, node->instances);
263 return smi_spi_probe(pdev, smi, node->instances);
264 case SMI_AUTO_DETECT:
266 * For backwards-compatibility with the existing nodes I2C
267 * is checked first and if such entries are found ONLY I2C
268 * devices are created. Since some existing nodes that were
269 * already handled by this driver could also contain unrelated
270 * SpiSerialBus nodes that were previously ignored, and this
271 * preserves that behavior.
273 ret = smi_i2c_probe(pdev, smi, node->instances);
276 return smi_spi_probe(pdev, smi, node->instances);
282 static void smi_remove(struct platform_device *pdev)
284 struct smi *smi = platform_get_drvdata(pdev);
286 smi_devs_unregister(smi);
289 static const struct smi_node bsg1160_data = {
291 { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
299 static const struct smi_node bsg2150_data = {
301 { "bmc150_accel", IRQ_RESOURCE_GPIO, 0 },
303 /* The resources describe a 3th client, but it is not really there. */
304 { "bsg2150_dummy_dev" },
310 static const struct smi_node int3515_data = {
312 { "tps6598x", IRQ_RESOURCE_APIC, 0 },
313 { "tps6598x", IRQ_RESOURCE_APIC, 1 },
314 { "tps6598x", IRQ_RESOURCE_APIC, 2 },
315 { "tps6598x", IRQ_RESOURCE_APIC, 3 },
321 static const struct smi_node cs35l41_hda = {
323 { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
324 { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
325 { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
326 { "cs35l41-hda", IRQ_RESOURCE_AUTO, 0 },
329 .bus_type = SMI_AUTO_DETECT,
332 static const struct smi_node cs35l56_hda = {
334 { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
335 { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
336 { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
337 { "cs35l56-hda", IRQ_RESOURCE_AUTO, 0 },
338 /* a 5th entry is an alias address, not a real device */
339 { "cs35l56-hda_dummy_dev" },
342 .bus_type = SMI_AUTO_DETECT,
346 * Note new device-ids must also be added to ignore_serial_bus_ids in
347 * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
349 static const struct acpi_device_id smi_acpi_ids[] = {
350 { "BSG1160", (unsigned long)&bsg1160_data },
351 { "BSG2150", (unsigned long)&bsg2150_data },
352 { "CSC3551", (unsigned long)&cs35l41_hda },
353 { "CSC3556", (unsigned long)&cs35l56_hda },
354 { "INT3515", (unsigned long)&int3515_data },
355 /* Non-conforming _HID for Cirrus Logic already released */
356 { "CLSA0100", (unsigned long)&cs35l41_hda },
357 { "CLSA0101", (unsigned long)&cs35l41_hda },
360 MODULE_DEVICE_TABLE(acpi, smi_acpi_ids);
362 static struct platform_driver smi_driver = {
364 .name = "Serial bus multi instantiate pseudo device driver",
365 .acpi_match_table = smi_acpi_ids,
368 .remove_new = smi_remove,
370 module_platform_driver(smi_driver);
372 MODULE_DESCRIPTION("Serial multi instantiate pseudo device driver");
374 MODULE_LICENSE("GPL");