1 // SPDX-License-Identifier: GPL-2.0+
3 * Serial multi-instantiate driver, pseudo driver to instantiate multiple
4 * client devices 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/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 #if IS_REACHABLE(CONFIG_I2C)
87 while (smi
->i2c_num
--)
88 i2c_unregister_device(smi
->i2c_devs
[smi
->i2c_num
]);
91 if (IS_REACHABLE(CONFIG_SPI
)) {
92 while (smi
->spi_num
--)
93 spi_unregister_device(smi
->spi_devs
[smi
->spi_num
]);
98 * smi_spi_probe - Instantiate multiple SPI devices from inst array
99 * @pdev: Platform device
100 * @smi: Internal struct for Serial multi instantiate driver
101 * @inst_array: Array of instances to probe
103 * Returns the number of SPI devices instantiate, Zero if none is found or a negative error code.
105 static int smi_spi_probe(struct platform_device
*pdev
, struct smi
*smi
,
106 const struct smi_instance
*inst_array
)
108 struct device
*dev
= &pdev
->dev
;
109 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
110 struct spi_controller
*ctlr
;
111 struct spi_device
*spi_dev
;
115 ret
= acpi_spi_count_resources(adev
);
123 smi
->spi_devs
= devm_kcalloc(dev
, count
, sizeof(*smi
->spi_devs
), GFP_KERNEL
);
127 for (i
= 0; i
< count
&& inst_array
[i
].type
; i
++) {
129 spi_dev
= acpi_spi_device_alloc(NULL
, adev
, i
);
130 if (IS_ERR(spi_dev
)) {
131 ret
= dev_err_probe(dev
, PTR_ERR(spi_dev
), "failed to allocate SPI device %s from ACPI\n",
132 dev_name(&adev
->dev
));
136 ctlr
= spi_dev
->controller
;
138 strscpy(spi_dev
->modalias
, inst_array
[i
].type
);
140 ret
= smi_get_irq(pdev
, adev
, &inst_array
[i
]);
142 spi_dev_put(spi_dev
);
147 snprintf(name
, sizeof(name
), "%s-%s-%s.%d", dev_name(&ctlr
->dev
), dev_name(dev
),
148 inst_array
[i
].type
, i
);
149 spi_dev
->dev
.init_name
= name
;
151 ret
= spi_add_device(spi_dev
);
153 dev_err_probe(&ctlr
->dev
, ret
, "failed to add SPI device %s from ACPI\n",
154 dev_name(&adev
->dev
));
155 spi_dev_put(spi_dev
);
159 dev_dbg(dev
, "SPI device %s using chip select %u", name
,
160 spi_get_chipselect(spi_dev
, 0));
162 smi
->spi_devs
[i
] = spi_dev
;
166 if (smi
->spi_num
< count
) {
167 dev_dbg(dev
, "Error finding driver, idx %d\n", i
);
172 dev_info(dev
, "Instantiated %d SPI devices.\n", smi
->spi_num
);
176 smi_devs_unregister(smi
);
182 * smi_i2c_probe - Instantiate multiple I2C devices from inst array
183 * @pdev: Platform device
184 * @smi: Internal struct for Serial multi instantiate driver
185 * @inst_array: Array of instances to probe
187 * Returns the number of I2C devices instantiate, Zero if none is found or a negative error code.
189 static int smi_i2c_probe(struct platform_device
*pdev
, struct smi
*smi
,
190 const struct smi_instance
*inst_array
)
192 struct i2c_board_info board_info
= {};
193 struct device
*dev
= &pdev
->dev
;
194 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
198 ret
= i2c_acpi_client_count(adev
);
206 smi
->i2c_devs
= devm_kcalloc(dev
, count
, sizeof(*smi
->i2c_devs
), GFP_KERNEL
);
210 for (i
= 0; i
< count
&& inst_array
[i
].type
; i
++) {
211 memset(&board_info
, 0, sizeof(board_info
));
212 strscpy(board_info
.type
, inst_array
[i
].type
);
213 snprintf(name
, sizeof(name
), "%s-%s.%d", dev_name(dev
), inst_array
[i
].type
, i
);
214 board_info
.dev_name
= name
;
216 ret
= smi_get_irq(pdev
, adev
, &inst_array
[i
]);
219 board_info
.irq
= ret
;
221 smi
->i2c_devs
[i
] = i2c_acpi_new_device(dev
, i
, &board_info
);
222 if (IS_ERR(smi
->i2c_devs
[i
])) {
223 ret
= dev_err_probe(dev
, PTR_ERR(smi
->i2c_devs
[i
]),
224 "Error creating i2c-client, idx %d\n", i
);
229 if (smi
->i2c_num
< count
) {
230 dev_dbg(dev
, "Error finding driver, idx %d\n", i
);
235 dev_info(dev
, "Instantiated %d I2C devices.\n", smi
->i2c_num
);
239 smi_devs_unregister(smi
);
244 static int smi_probe(struct platform_device
*pdev
)
246 struct device
*dev
= &pdev
->dev
;
247 const struct smi_node
*node
;
251 node
= device_get_match_data(dev
);
253 dev_dbg(dev
, "Error ACPI match data is missing\n");
257 smi
= devm_kzalloc(dev
, sizeof(*smi
), GFP_KERNEL
);
261 platform_set_drvdata(pdev
, smi
);
263 switch (node
->bus_type
) {
265 if (IS_REACHABLE(CONFIG_I2C
))
266 return smi_i2c_probe(pdev
, smi
, node
->instances
);
270 if (IS_REACHABLE(CONFIG_SPI
))
271 return smi_spi_probe(pdev
, smi
, node
->instances
);
274 case SMI_AUTO_DETECT
:
276 * For backwards-compatibility with the existing nodes I2C
277 * is checked first and if such entries are found ONLY I2C
278 * devices are created. Since some existing nodes that were
279 * already handled by this driver could also contain unrelated
280 * SpiSerialBus nodes that were previously ignored, and this
281 * preserves that behavior.
283 if (IS_REACHABLE(CONFIG_I2C
)) {
284 ret
= smi_i2c_probe(pdev
, smi
, node
->instances
);
289 if (IS_REACHABLE(CONFIG_SPI
))
290 return smi_spi_probe(pdev
, smi
, node
->instances
);
298 static void smi_remove(struct platform_device
*pdev
)
300 struct smi
*smi
= platform_get_drvdata(pdev
);
302 smi_devs_unregister(smi
);
305 static const struct smi_node bsg1160_data
= {
307 { "bmc150_accel", IRQ_RESOURCE_GPIO
, 0 },
315 static const struct smi_node bsg2150_data
= {
317 { "bmc150_accel", IRQ_RESOURCE_GPIO
, 0 },
319 /* The resources describe a 3th client, but it is not really there. */
320 { "bsg2150_dummy_dev" },
326 static const struct smi_node int3515_data
= {
328 { "tps6598x", IRQ_RESOURCE_APIC
, 0 },
329 { "tps6598x", IRQ_RESOURCE_APIC
, 1 },
330 { "tps6598x", IRQ_RESOURCE_APIC
, 2 },
331 { "tps6598x", IRQ_RESOURCE_APIC
, 3 },
337 static const struct smi_node cs35l41_hda
= {
339 { "cs35l41-hda", IRQ_RESOURCE_AUTO
, 0 },
340 { "cs35l41-hda", IRQ_RESOURCE_AUTO
, 0 },
341 { "cs35l41-hda", IRQ_RESOURCE_AUTO
, 0 },
342 { "cs35l41-hda", IRQ_RESOURCE_AUTO
, 0 },
345 .bus_type
= SMI_AUTO_DETECT
,
348 static const struct smi_node cs35l54_hda
= {
350 { "cs35l54-hda", IRQ_RESOURCE_AUTO
, 0 },
351 { "cs35l54-hda", IRQ_RESOURCE_AUTO
, 0 },
352 { "cs35l54-hda", IRQ_RESOURCE_AUTO
, 0 },
353 { "cs35l54-hda", IRQ_RESOURCE_AUTO
, 0 },
354 /* a 5th entry is an alias address, not a real device */
355 { "cs35l54-hda_dummy_dev" },
358 .bus_type
= SMI_AUTO_DETECT
,
361 static const struct smi_node cs35l56_hda
= {
363 { "cs35l56-hda", IRQ_RESOURCE_AUTO
, 0 },
364 { "cs35l56-hda", IRQ_RESOURCE_AUTO
, 0 },
365 { "cs35l56-hda", IRQ_RESOURCE_AUTO
, 0 },
366 { "cs35l56-hda", IRQ_RESOURCE_AUTO
, 0 },
367 /* a 5th entry is an alias address, not a real device */
368 { "cs35l56-hda_dummy_dev" },
371 .bus_type
= SMI_AUTO_DETECT
,
374 static const struct smi_node cs35l57_hda
= {
376 { "cs35l57-hda", IRQ_RESOURCE_AUTO
, 0 },
377 { "cs35l57-hda", IRQ_RESOURCE_AUTO
, 0 },
378 { "cs35l57-hda", IRQ_RESOURCE_AUTO
, 0 },
379 { "cs35l57-hda", IRQ_RESOURCE_AUTO
, 0 },
380 /* a 5th entry is an alias address, not a real device */
381 { "cs35l57-hda_dummy_dev" },
384 .bus_type
= SMI_AUTO_DETECT
,
388 * Note new device-ids must also be added to ignore_serial_bus_ids in
389 * drivers/acpi/scan.c: acpi_device_enumeration_by_parent().
391 static const struct acpi_device_id smi_acpi_ids
[] = {
392 { "BSG1160", (unsigned long)&bsg1160_data
},
393 { "BSG2150", (unsigned long)&bsg2150_data
},
394 { "CSC3551", (unsigned long)&cs35l41_hda
},
395 { "CSC3554", (unsigned long)&cs35l54_hda
},
396 { "CSC3556", (unsigned long)&cs35l56_hda
},
397 { "CSC3557", (unsigned long)&cs35l57_hda
},
398 { "INT3515", (unsigned long)&int3515_data
},
399 /* Non-conforming _HID for Cirrus Logic already released */
400 { "CLSA0100", (unsigned long)&cs35l41_hda
},
401 { "CLSA0101", (unsigned long)&cs35l41_hda
},
404 MODULE_DEVICE_TABLE(acpi
, smi_acpi_ids
);
406 static struct platform_driver smi_driver
= {
408 .name
= "Serial bus multi instantiate pseudo device driver",
409 .acpi_match_table
= smi_acpi_ids
,
412 .remove
= smi_remove
,
414 module_platform_driver(smi_driver
);
416 MODULE_DESCRIPTION("Serial multi instantiate pseudo device driver");
417 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
418 MODULE_LICENSE("GPL");