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/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/usb/pd.h>
29 #include "intel_cht_int33fe_common.h"
32 * Grrr, I severely dislike buggy BIOS-es. At least one BIOS enumerates
33 * the max17047 both through the INT33FE ACPI device (it is right there
34 * in the resources table) as well as through a separate MAX17047 device.
36 * These helpers are used to work around this by checking if an I²C client
37 * for the max17047 has already been registered.
39 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
41 struct i2c_client **max17047 = data;
42 struct acpi_device *adev;
44 adev = ACPI_COMPANION(dev);
48 /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
49 if (!acpi_dev_hid_uid_match(adev, "MAX17047", NULL))
52 *max17047 = to_i2c_client(dev);
56 static const char * const max17047_suppliers[] = { "bq24190-charger" };
58 static const struct property_entry max17047_properties[] = {
59 PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
63 static const struct software_node max17047_node = {
65 .properties = max17047_properties,
69 * We are not using inline property here because those are constant,
70 * and we need to adjust this one at runtime to point to real
73 static struct software_node_ref_args fusb302_mux_refs[] = {
77 static const struct property_entry fusb302_properties[] = {
78 PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
79 PROPERTY_ENTRY_REF_ARRAY("usb-role-switch", fusb302_mux_refs),
83 static const struct software_node fusb302_node = {
85 .properties = fusb302_properties,
88 #define PDO_FIXED_FLAGS \
89 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
91 static const u32 src_pdo[] = {
92 PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
95 static const u32 snk_pdo[] = {
96 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
97 PDO_VAR(5000, 12000, 3000),
100 static const struct software_node pi3usb30532_node = {
101 .name = "pi3usb30532",
104 static const struct software_node displayport_node = {
105 .name = "displayport",
108 static const struct property_entry usb_connector_properties[] = {
109 PROPERTY_ENTRY_STRING("data-role", "dual"),
110 PROPERTY_ENTRY_STRING("power-role", "dual"),
111 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
112 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
113 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
114 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
115 PROPERTY_ENTRY_REF("orientation-switch", &pi3usb30532_node),
116 PROPERTY_ENTRY_REF("mode-switch", &pi3usb30532_node),
117 PROPERTY_ENTRY_REF("displayport", &displayport_node),
121 static const struct software_node usb_connector_node = {
123 .parent = &fusb302_node,
124 .properties = usb_connector_properties,
127 static const struct software_node *node_group[] = {
136 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
138 struct fwnode_handle *fwnode;
139 struct pci_dev *pdev;
141 fwnode = software_node_fwnode(&displayport_node);
145 /* First let's find the GPU PCI device */
146 pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
147 if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
152 /* Then the DP child device node */
153 data->dp = device_get_named_child_node(&pdev->dev, "DD02");
158 fwnode->secondary = ERR_PTR(-ENODEV);
159 data->dp->secondary = fwnode;
164 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
166 software_node_unregister_node_group(node_group);
168 if (fusb302_mux_refs[0].node) {
169 fwnode_handle_put(software_node_fwnode(fusb302_mux_refs[0].node));
170 fusb302_mux_refs[0].node = NULL;
174 data->dp->secondary = NULL;
175 fwnode_handle_put(data->dp);
180 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
182 const struct software_node *mux_ref_node;
186 * There is no ACPI device node for the USB role mux, so we need to wait
187 * until the mux driver has created software node for the mux device.
188 * It means we depend on the mux driver. This function will return
189 * -EPROBE_DEFER until the mux device is registered.
191 mux_ref_node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
193 return -EPROBE_DEFER;
196 * Update node used in "usb-role-switch" property. Note that we
197 * rely on software_node_register_nodes() to use the original
198 * instance of properties instead of copying them.
200 fusb302_mux_refs[0].node = mux_ref_node;
202 ret = software_node_register_node_group(node_group);
206 /* The devices that are not created in this driver need extra steps. */
209 * The DP connector does have ACPI device node. In this case we can just
210 * find that ACPI node and assign our node as the secondary node to it.
212 ret = cht_int33fe_setup_dp(data);
214 goto err_remove_nodes;
219 cht_int33fe_remove_nodes(data);
225 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
227 struct i2c_client *max17047 = NULL;
228 struct i2c_board_info board_info;
229 struct fwnode_handle *fwnode;
232 fwnode = software_node_fwnode(&max17047_node);
236 i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
238 /* Pre-existing I²C client for the max17047, add device properties */
239 set_secondary_fwnode(&max17047->dev, fwnode);
240 /* And re-probe to get the new device properties applied */
241 ret = device_reprobe(&max17047->dev);
243 dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
247 memset(&board_info, 0, sizeof(board_info));
248 strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
249 board_info.dev_name = "max17047";
250 board_info.fwnode = fwnode;
251 data->battery_fg = i2c_acpi_new_device(dev, 1, &board_info);
253 return PTR_ERR_OR_ZERO(data->battery_fg);
256 int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
258 struct device *dev = data->dev;
259 struct i2c_board_info board_info;
260 struct fwnode_handle *fwnode;
261 struct regulator *regulator;
266 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
267 * We check for the bq24292i vbus regulator here, this has 2 purposes:
268 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
269 * max-snk voltage to 12V with another charger-IC is not good.
270 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
271 * regulator-map, which is part of the bq24292i regulator_init_data,
272 * must be registered before the fusb302 is instantiated, otherwise
273 * it will end up with a dummy-regulator.
274 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
275 * which is defined in i2c-cht-wc.c from where the bq24292i I²C client
276 * gets instantiated. We use regulator_get_optional here so that we
277 * don't end up getting a dummy-regulator ourselves.
279 regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
280 if (IS_ERR(regulator)) {
281 ret = PTR_ERR(regulator);
282 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
284 regulator_put(regulator);
286 /* The FUSB302 uses the IRQ at index 1 and is the only IRQ user */
287 fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
288 if (fusb302_irq < 0) {
289 if (fusb302_irq != -EPROBE_DEFER)
290 dev_err(dev, "Error getting FUSB302 irq\n");
294 ret = cht_int33fe_add_nodes(data);
298 /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047() */
299 ret = cht_int33fe_register_max17047(dev, data);
301 goto out_remove_nodes;
303 fwnode = software_node_fwnode(&fusb302_node);
306 goto out_unregister_max17047;
309 memset(&board_info, 0, sizeof(board_info));
310 strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
311 board_info.dev_name = "fusb302";
312 board_info.fwnode = fwnode;
313 board_info.irq = fusb302_irq;
315 data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
316 if (IS_ERR(data->fusb302)) {
317 ret = PTR_ERR(data->fusb302);
318 goto out_unregister_max17047;
321 fwnode = software_node_fwnode(&pi3usb30532_node);
324 goto out_unregister_fusb302;
327 memset(&board_info, 0, sizeof(board_info));
328 board_info.dev_name = "pi3usb30532";
329 board_info.fwnode = fwnode;
330 strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
332 data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
333 if (IS_ERR(data->pi3usb30532)) {
334 ret = PTR_ERR(data->pi3usb30532);
335 goto out_unregister_fusb302;
340 out_unregister_fusb302:
341 i2c_unregister_device(data->fusb302);
343 out_unregister_max17047:
344 i2c_unregister_device(data->battery_fg);
347 cht_int33fe_remove_nodes(data);
352 int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
354 i2c_unregister_device(data->pi3usb30532);
355 i2c_unregister_device(data->fusb302);
356 i2c_unregister_device(data->battery_fg);
358 cht_int33fe_remove_nodes(data);