1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Cherry Trail ACPI INT33FE pseudo device driver
7 * Some Intel Cherry Trail based device which ship with Windows 10, have
8 * this weird INT33FE ACPI device with a CRS table with 4 I2cSerialBusV2
9 * resources, for 4 different chips attached to various i2c busses:
10 * 1. The Whiskey Cove pmic, which is also described by the INT34D3 ACPI device
11 * 2. Maxim MAX17047 Fuel Gauge Controller
12 * 3. FUSB302 USB Type-C Controller
13 * 4. PI3USB30532 USB switch
15 * So this driver is a stub / pseudo driver whose only purpose is to
16 * instantiate i2c-clients for chips 2 - 4, so that standard i2c drivers
17 * for these chips can bind to the them.
20 #include <linux/acpi.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/usb/pd.h>
30 #define EXPECTED_PTYPE 4
34 INT33FE_NODE_MAX17047,
35 INT33FE_NODE_PI3USB30532,
36 INT33FE_NODE_DISPLAYPORT,
37 INT33FE_NODE_ROLE_SWITCH,
38 INT33FE_NODE_USB_CONNECTOR,
42 struct cht_int33fe_data {
43 struct i2c_client *max17047;
44 struct i2c_client *fusb302;
45 struct i2c_client *pi3usb30532;
47 struct fwnode_handle *dp;
48 struct fwnode_handle *mux;
51 static const struct software_node nodes[];
53 static const struct software_node_ref_args pi3usb30532_ref = {
54 &nodes[INT33FE_NODE_PI3USB30532]
57 static const struct software_node_ref_args dp_ref = {
58 &nodes[INT33FE_NODE_DISPLAYPORT]
61 static struct software_node_ref_args mux_ref;
63 static const struct software_node_reference usb_connector_refs[] = {
64 { "orientation-switch", 1, &pi3usb30532_ref},
65 { "mode-switch", 1, &pi3usb30532_ref},
66 { "displayport", 1, &dp_ref},
70 static const struct software_node_reference fusb302_refs[] = {
71 { "usb-role-switch", 1, &mux_ref},
76 * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
77 * the max17047 both through the INT33FE ACPI device (it is right there
78 * in the resources table) as well as through a separate MAX17047 device.
80 * These helpers are used to work around this by checking if an i2c-client
81 * for the max17047 has already been registered.
83 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
85 struct i2c_client **max17047 = data;
86 struct acpi_device *adev;
89 adev = ACPI_COMPANION(dev);
93 hid = acpi_device_hid(adev);
95 /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
96 if (strcmp(hid, "MAX17047"))
99 *max17047 = to_i2c_client(dev);
103 static const char * const max17047_suppliers[] = { "bq24190-charger" };
105 static const struct property_entry max17047_props[] = {
106 PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
110 static const struct property_entry fusb302_props[] = {
111 PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
115 #define PDO_FIXED_FLAGS \
116 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
118 static const u32 src_pdo[] = {
119 PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
122 static const u32 snk_pdo[] = {
123 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
124 PDO_VAR(5000, 12000, 3000),
127 static const struct property_entry usb_connector_props[] = {
128 PROPERTY_ENTRY_STRING("data-role", "dual"),
129 PROPERTY_ENTRY_STRING("power-role", "dual"),
130 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
131 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
132 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
133 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
137 static const struct software_node nodes[] = {
138 { "fusb302", NULL, fusb302_props, fusb302_refs },
139 { "max17047", NULL, max17047_props },
142 { "usb-role-switch" },
143 { "connector", &nodes[0], usb_connector_props, usb_connector_refs },
147 static int cht_int33fe_setup_mux(struct cht_int33fe_data *data)
149 struct fwnode_handle *fwnode;
153 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_ROLE_SWITCH]);
157 /* First finding the platform device */
158 p = bus_find_device_by_name(&platform_bus_type, NULL,
159 "intel_xhci_usb_sw");
161 return -EPROBE_DEFER;
163 /* Then the mux child device */
164 dev = device_find_child_by_name(p, "intel_xhci_usb_sw-role-switch");
167 return -EPROBE_DEFER;
169 /* If there already is a node for the mux, using that one. */
171 fwnode_remove_software_node(fwnode);
173 dev->fwnode = fwnode;
175 data->mux = fwnode_handle_get(dev->fwnode);
177 mux_ref.node = to_software_node(data->mux);
182 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
184 struct fwnode_handle *fwnode;
185 struct pci_dev *pdev;
187 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_DISPLAYPORT]);
191 /* First let's find the GPU PCI device */
192 pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
193 if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
198 /* Then the DP child device node */
199 data->dp = device_get_named_child_node(&pdev->dev, "DD02");
204 fwnode->secondary = ERR_PTR(-ENODEV);
205 data->dp->secondary = fwnode;
210 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
212 software_node_unregister_nodes(nodes);
215 fwnode_handle_put(data->mux);
221 data->dp->secondary = NULL;
222 fwnode_handle_put(data->dp);
227 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
231 ret = software_node_register_nodes(nodes);
235 /* The devices that are not created in this driver need extra steps. */
238 * There is no ACPI device node for the USB role mux, so we need to find
239 * the mux device and assign our node directly to it. That means we
240 * depend on the mux driver. This function will return -PROBE_DEFER
241 * until the mux device is registered.
243 ret = cht_int33fe_setup_mux(data);
245 goto err_remove_nodes;
248 * The DP connector does have ACPI device node. In this case we can just
249 * find that ACPI node and assign our node as the secondary node to it.
251 ret = cht_int33fe_setup_dp(data);
253 goto err_remove_nodes;
258 cht_int33fe_remove_nodes(data);
264 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
266 struct i2c_client *max17047 = NULL;
267 struct i2c_board_info board_info;
268 struct fwnode_handle *fwnode;
271 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_MAX17047]);
275 i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
277 /* Pre-existing i2c-client for the max17047, add device-props */
278 fwnode->secondary = ERR_PTR(-ENODEV);
279 max17047->dev.fwnode->secondary = fwnode;
280 /* And re-probe to get the new device-props applied. */
281 ret = device_reprobe(&max17047->dev);
283 dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
287 memset(&board_info, 0, sizeof(board_info));
288 strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
289 board_info.dev_name = "max17047";
290 board_info.fwnode = fwnode;
291 data->max17047 = i2c_acpi_new_device(dev, 1, &board_info);
293 return PTR_ERR_OR_ZERO(data->max17047);
296 static int cht_int33fe_probe(struct platform_device *pdev)
298 struct device *dev = &pdev->dev;
299 struct i2c_board_info board_info;
300 struct cht_int33fe_data *data;
301 struct fwnode_handle *fwnode;
302 struct regulator *regulator;
303 unsigned long long ptyp;
308 status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
309 if (ACPI_FAILURE(status)) {
310 dev_err(dev, "Error getting PTYPE\n");
315 * The same ACPI HID is used for different configurations check PTYP
316 * to ensure that we are dealing with the expected config.
318 if (ptyp != EXPECTED_PTYPE)
321 /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
322 if (!acpi_dev_present("INT34D3", "1", 3)) {
323 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
329 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
330 * We check for the bq24292i vbus regulator here, this has 2 purposes:
331 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
332 * max-snk voltage to 12V with another charger-IC is not good.
333 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
334 * regulator-map, which is part of the bq24292i regulator_init_data,
335 * must be registered before the fusb302 is instantiated, otherwise
336 * it will end up with a dummy-regulator.
337 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
338 * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
339 * gets instantiated. We use regulator_get_optional here so that we
340 * don't end up getting a dummy-regulator ourselves.
342 regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
343 if (IS_ERR(regulator)) {
344 ret = PTR_ERR(regulator);
345 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
347 regulator_put(regulator);
349 /* The FUSB302 uses the irq at index 1 and is the only irq user */
350 fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
351 if (fusb302_irq < 0) {
352 if (fusb302_irq != -EPROBE_DEFER)
353 dev_err(dev, "Error getting FUSB302 irq\n");
357 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
361 ret = cht_int33fe_add_nodes(data);
365 /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047 */
366 ret = cht_int33fe_register_max17047(dev, data);
368 goto out_remove_nodes;
370 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_FUSB302]);
373 goto out_unregister_max17047;
376 memset(&board_info, 0, sizeof(board_info));
377 strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
378 board_info.dev_name = "fusb302";
379 board_info.fwnode = fwnode;
380 board_info.irq = fusb302_irq;
382 data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
383 if (IS_ERR(data->fusb302)) {
384 ret = PTR_ERR(data->fusb302);
385 goto out_unregister_max17047;
388 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_PI3USB30532]);
391 goto out_unregister_fusb302;
394 memset(&board_info, 0, sizeof(board_info));
395 board_info.dev_name = "pi3usb30532";
396 board_info.fwnode = fwnode;
397 strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
399 data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
400 if (IS_ERR(data->pi3usb30532)) {
401 ret = PTR_ERR(data->pi3usb30532);
402 goto out_unregister_fusb302;
405 platform_set_drvdata(pdev, data);
409 out_unregister_fusb302:
410 i2c_unregister_device(data->fusb302);
412 out_unregister_max17047:
413 i2c_unregister_device(data->max17047);
416 cht_int33fe_remove_nodes(data);
421 static int cht_int33fe_remove(struct platform_device *pdev)
423 struct cht_int33fe_data *data = platform_get_drvdata(pdev);
425 i2c_unregister_device(data->pi3usb30532);
426 i2c_unregister_device(data->fusb302);
427 i2c_unregister_device(data->max17047);
429 cht_int33fe_remove_nodes(data);
434 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
438 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
440 static struct platform_driver cht_int33fe_driver = {
442 .name = "Intel Cherry Trail ACPI INT33FE driver",
443 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
445 .probe = cht_int33fe_probe,
446 .remove = cht_int33fe_remove,
449 module_platform_driver(cht_int33fe_driver);
451 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
453 MODULE_LICENSE("GPL v2");