1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Cherry Trail ACPI INT33FE pseudo device driver
5 * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
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 I²C buses:
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 I²C clients for chips 2 - 4, so that standard I²C drivers
17 * for these chips can bind to the them.
20 #include <linux/dmi.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/property.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/usb/pd.h>
30 struct cht_int33fe_data
{
31 struct i2c_client
*battery_fg
;
32 struct i2c_client
*fusb302
;
33 struct i2c_client
*pi3usb30532
;
34 struct fwnode_handle
*dp
;
38 * Grrr, I severely dislike buggy BIOS-es. At least one BIOS enumerates
39 * the max17047 both through the INT33FE ACPI device (it is right there
40 * in the resources table) as well as through a separate MAX17047 device.
42 * These helpers are used to work around this by checking if an I²C client
43 * for the max17047 has already been registered.
45 static int cht_int33fe_check_for_max17047(struct device
*dev
, void *data
)
47 struct i2c_client
**max17047
= data
;
48 struct acpi_device
*adev
;
50 adev
= ACPI_COMPANION(dev
);
54 /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
55 if (!acpi_dev_hid_uid_match(adev
, "MAX17047", NULL
))
58 *max17047
= to_i2c_client(dev
);
62 static const char * const max17047_suppliers
[] = { "bq24190-charger" };
64 static const struct property_entry max17047_properties
[] = {
65 PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers
),
69 static const struct software_node max17047_node
= {
71 .properties
= max17047_properties
,
75 * We are not using inline property here because those are constant,
76 * and we need to adjust this one at runtime to point to real
79 static struct software_node_ref_args fusb302_mux_refs
[] = {
83 static const struct property_entry fusb302_properties
[] = {
84 PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
85 PROPERTY_ENTRY_REF_ARRAY("usb-role-switch", fusb302_mux_refs
),
89 static const struct software_node fusb302_node
= {
91 .properties
= fusb302_properties
,
94 #define PDO_FIXED_FLAGS \
95 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
97 static const u32 src_pdo
[] = {
98 PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS
),
101 static const u32 snk_pdo
[] = {
102 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS
),
103 PDO_VAR(5000, 12000, 3000),
106 static const struct software_node pi3usb30532_node
= {
107 .name
= "pi3usb30532",
110 static const struct software_node displayport_node
= {
111 .name
= "displayport",
114 static const struct property_entry usb_connector_properties
[] = {
115 PROPERTY_ENTRY_STRING("data-role", "dual"),
116 PROPERTY_ENTRY_STRING("power-role", "dual"),
117 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
118 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo
),
119 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo
),
120 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
121 PROPERTY_ENTRY_REF("orientation-switch", &pi3usb30532_node
),
122 PROPERTY_ENTRY_REF("mode-switch", &pi3usb30532_node
),
123 PROPERTY_ENTRY_REF("displayport", &displayport_node
),
127 static const struct software_node usb_connector_node
= {
129 .parent
= &fusb302_node
,
130 .properties
= usb_connector_properties
,
133 static const struct software_node altmodes_node
= {
135 .parent
= &usb_connector_node
,
138 static const struct property_entry dp_altmode_properties
[] = {
139 PROPERTY_ENTRY_U16("svid", 0xff01),
140 PROPERTY_ENTRY_U32("vdo", 0x0c0086),
144 static const struct software_node dp_altmode_node
= {
145 .name
= "displayport-altmode",
146 .parent
= &altmodes_node
,
147 .properties
= dp_altmode_properties
,
150 static const struct software_node
*node_group
[] = {
161 static int cht_int33fe_setup_dp(struct cht_int33fe_data
*data
)
163 struct fwnode_handle
*fwnode
;
164 struct pci_dev
*pdev
;
166 fwnode
= software_node_fwnode(&displayport_node
);
170 /* First let's find the GPU PCI device */
171 pdev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
172 if (!pdev
|| pdev
->vendor
!= PCI_VENDOR_ID_INTEL
) {
177 /* Then the DP-2 child device node */
178 data
->dp
= device_get_named_child_node(&pdev
->dev
, "DD04");
183 fwnode
->secondary
= ERR_PTR(-ENODEV
);
184 data
->dp
->secondary
= fwnode
;
189 static void cht_int33fe_remove_nodes(struct cht_int33fe_data
*data
)
191 software_node_unregister_node_group(node_group
);
193 if (fusb302_mux_refs
[0].node
) {
194 fwnode_handle_put(software_node_fwnode(fusb302_mux_refs
[0].node
));
195 fusb302_mux_refs
[0].node
= NULL
;
199 data
->dp
->secondary
= NULL
;
200 fwnode_handle_put(data
->dp
);
205 static int cht_int33fe_add_nodes(struct cht_int33fe_data
*data
)
207 const struct software_node
*mux_ref_node
;
211 * There is no ACPI device node for the USB role mux, so we need to wait
212 * until the mux driver has created software node for the mux device.
213 * It means we depend on the mux driver. This function will return
214 * -EPROBE_DEFER until the mux device is registered.
216 mux_ref_node
= software_node_find_by_name(NULL
, "intel-xhci-usb-sw");
218 return -EPROBE_DEFER
;
221 * Update node used in "usb-role-switch" property. Note that we
222 * rely on software_node_register_node_group() to use the original
223 * instance of properties instead of copying them.
225 fusb302_mux_refs
[0].node
= mux_ref_node
;
227 ret
= software_node_register_node_group(node_group
);
231 /* The devices that are not created in this driver need extra steps. */
234 * The DP connector does have ACPI device node. In this case we can just
235 * find that ACPI node and assign our node as the secondary node to it.
237 ret
= cht_int33fe_setup_dp(data
);
239 goto err_remove_nodes
;
244 cht_int33fe_remove_nodes(data
);
250 cht_int33fe_register_max17047(struct device
*dev
, struct cht_int33fe_data
*data
)
252 struct i2c_client
*max17047
= NULL
;
253 struct i2c_board_info board_info
;
254 struct fwnode_handle
*fwnode
;
257 fwnode
= software_node_fwnode(&max17047_node
);
261 i2c_for_each_dev(&max17047
, cht_int33fe_check_for_max17047
);
263 /* Pre-existing I²C client for the max17047, add device properties */
264 set_secondary_fwnode(&max17047
->dev
, fwnode
);
265 /* And re-probe to get the new device properties applied */
266 ret
= device_reprobe(&max17047
->dev
);
268 dev_warn(dev
, "Reprobing max17047 error: %d\n", ret
);
272 memset(&board_info
, 0, sizeof(board_info
));
273 strscpy(board_info
.type
, "max17047");
274 board_info
.dev_name
= "max17047";
275 board_info
.fwnode
= fwnode
;
276 data
->battery_fg
= i2c_acpi_new_device(dev
, 1, &board_info
);
278 return PTR_ERR_OR_ZERO(data
->battery_fg
);
281 static const struct dmi_system_id cht_int33fe_typec_ids
[] = {
284 * GPD win / GPD pocket mini laptops
286 * This DMI match may not seem unique, but it is. In the 67000+
287 * DMI decode dumps from linux-hardware.org only 116 have
288 * board_vendor set to "AMI Corporation" and of those 116 only
289 * the GPD win's and pocket's board_name is "Default string".
292 DMI_EXACT_MATCH(DMI_BOARD_VENDOR
, "AMI Corporation"),
293 DMI_EXACT_MATCH(DMI_BOARD_NAME
, "Default string"),
294 DMI_EXACT_MATCH(DMI_BOARD_SERIAL
, "Default string"),
295 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "Default string"),
300 MODULE_DEVICE_TABLE(dmi
, cht_int33fe_typec_ids
);
302 static int cht_int33fe_typec_probe(struct platform_device
*pdev
)
304 struct i2c_board_info board_info
;
305 struct device
*dev
= &pdev
->dev
;
306 struct cht_int33fe_data
*data
;
307 struct fwnode_handle
*fwnode
;
308 struct regulator
*regulator
;
312 if (!dmi_check_system(cht_int33fe_typec_ids
))
315 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
320 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
321 * We check for the bq24292i vbus regulator here, this has 2 purposes:
322 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
323 * max-snk voltage to 12V with another charger-IC is not good.
324 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
325 * regulator-map, which is part of the bq24292i regulator_init_data,
326 * must be registered before the fusb302 is instantiated, otherwise
327 * it will end up with a dummy-regulator.
328 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
329 * which is defined in i2c-cht-wc.c from where the bq24292i I²C client
330 * gets instantiated. We use regulator_get_optional here so that we
331 * don't end up getting a dummy-regulator ourselves.
333 regulator
= regulator_get_optional(dev
, "cht_wc_usb_typec_vbus");
334 if (IS_ERR(regulator
)) {
335 ret
= PTR_ERR(regulator
);
336 return (ret
== -ENODEV
) ? -EPROBE_DEFER
: ret
;
338 regulator_put(regulator
);
340 /* The FUSB302 uses the IRQ at index 1 and is the only IRQ user */
341 fusb302_irq
= acpi_dev_gpio_irq_get(ACPI_COMPANION(dev
), 1);
342 if (fusb302_irq
< 0) {
343 if (fusb302_irq
!= -EPROBE_DEFER
)
344 dev_err(dev
, "Error getting FUSB302 irq\n");
348 ret
= cht_int33fe_add_nodes(data
);
352 /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047() */
353 ret
= cht_int33fe_register_max17047(dev
, data
);
355 goto out_remove_nodes
;
357 fwnode
= software_node_fwnode(&fusb302_node
);
360 goto out_unregister_max17047
;
363 memset(&board_info
, 0, sizeof(board_info
));
364 strscpy(board_info
.type
, "typec_fusb302");
365 board_info
.dev_name
= "fusb302";
366 board_info
.fwnode
= fwnode
;
367 board_info
.irq
= fusb302_irq
;
369 data
->fusb302
= i2c_acpi_new_device(dev
, 2, &board_info
);
370 if (IS_ERR(data
->fusb302
)) {
371 ret
= PTR_ERR(data
->fusb302
);
372 goto out_unregister_max17047
;
375 fwnode
= software_node_fwnode(&pi3usb30532_node
);
378 goto out_unregister_fusb302
;
381 memset(&board_info
, 0, sizeof(board_info
));
382 board_info
.dev_name
= "pi3usb30532";
383 board_info
.fwnode
= fwnode
;
384 strscpy(board_info
.type
, "pi3usb30532");
386 data
->pi3usb30532
= i2c_acpi_new_device(dev
, 3, &board_info
);
387 if (IS_ERR(data
->pi3usb30532
)) {
388 ret
= PTR_ERR(data
->pi3usb30532
);
389 goto out_unregister_fusb302
;
392 platform_set_drvdata(pdev
, data
);
396 out_unregister_fusb302
:
397 i2c_unregister_device(data
->fusb302
);
399 out_unregister_max17047
:
400 i2c_unregister_device(data
->battery_fg
);
403 cht_int33fe_remove_nodes(data
);
408 static void cht_int33fe_typec_remove(struct platform_device
*pdev
)
410 struct cht_int33fe_data
*data
= platform_get_drvdata(pdev
);
412 i2c_unregister_device(data
->pi3usb30532
);
413 i2c_unregister_device(data
->fusb302
);
414 i2c_unregister_device(data
->battery_fg
);
416 cht_int33fe_remove_nodes(data
);
419 static const struct acpi_device_id cht_int33fe_acpi_ids
[] = {
424 static struct platform_driver cht_int33fe_typec_driver
= {
426 .name
= "Intel Cherry Trail ACPI INT33FE Type-C driver",
427 .acpi_match_table
= ACPI_PTR(cht_int33fe_acpi_ids
),
429 .probe
= cht_int33fe_typec_probe
,
430 .remove
= cht_int33fe_typec_remove
,
433 module_platform_driver(cht_int33fe_typec_driver
);
435 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE Type-C pseudo device driver");
436 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
437 MODULE_LICENSE("GPL v2");