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 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/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/usb/pd.h>
28 #include "intel_cht_int33fe_common.h"
32 INT33FE_NODE_MAX17047,
33 INT33FE_NODE_PI3USB30532,
34 INT33FE_NODE_DISPLAYPORT,
35 INT33FE_NODE_USB_CONNECTOR,
39 static const struct software_node nodes[];
41 static const struct software_node_ref_args pi3usb30532_ref = {
42 &nodes[INT33FE_NODE_PI3USB30532]
45 static const struct software_node_ref_args dp_ref = {
46 &nodes[INT33FE_NODE_DISPLAYPORT]
49 static struct software_node_ref_args mux_ref;
51 static const struct software_node_reference usb_connector_refs[] = {
52 { "orientation-switch", 1, &pi3usb30532_ref},
53 { "mode-switch", 1, &pi3usb30532_ref},
54 { "displayport", 1, &dp_ref},
58 static const struct software_node_reference fusb302_refs[] = {
59 { "usb-role-switch", 1, &mux_ref},
64 * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
65 * the max17047 both through the INT33FE ACPI device (it is right there
66 * in the resources table) as well as through a separate MAX17047 device.
68 * These helpers are used to work around this by checking if an i2c-client
69 * for the max17047 has already been registered.
71 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
73 struct i2c_client **max17047 = data;
74 struct acpi_device *adev;
77 adev = ACPI_COMPANION(dev);
81 hid = acpi_device_hid(adev);
83 /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
84 if (strcmp(hid, "MAX17047"))
87 *max17047 = to_i2c_client(dev);
91 static const char * const max17047_suppliers[] = { "bq24190-charger" };
93 static const struct property_entry max17047_props[] = {
94 PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
98 static const struct property_entry fusb302_props[] = {
99 PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
103 #define PDO_FIXED_FLAGS \
104 (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
106 static const u32 src_pdo[] = {
107 PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
110 static const u32 snk_pdo[] = {
111 PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
112 PDO_VAR(5000, 12000, 3000),
115 static const struct property_entry usb_connector_props[] = {
116 PROPERTY_ENTRY_STRING("data-role", "dual"),
117 PROPERTY_ENTRY_STRING("power-role", "dual"),
118 PROPERTY_ENTRY_STRING("try-power-role", "sink"),
119 PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
120 PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
121 PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
125 static const struct software_node nodes[] = {
126 { "fusb302", NULL, fusb302_props, fusb302_refs },
127 { "max17047", NULL, max17047_props },
130 { "connector", &nodes[0], usb_connector_props, usb_connector_refs },
134 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
136 struct fwnode_handle *fwnode;
137 struct pci_dev *pdev;
139 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_DISPLAYPORT]);
143 /* First let's find the GPU PCI device */
144 pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
145 if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
150 /* Then the DP child device node */
151 data->dp = device_get_named_child_node(&pdev->dev, "DD02");
156 fwnode->secondary = ERR_PTR(-ENODEV);
157 data->dp->secondary = fwnode;
162 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
164 software_node_unregister_nodes(nodes);
167 fwnode_handle_put(software_node_fwnode(mux_ref.node));
172 data->dp->secondary = NULL;
173 fwnode_handle_put(data->dp);
178 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
182 ret = software_node_register_nodes(nodes);
186 /* The devices that are not created in this driver need extra steps. */
189 * There is no ACPI device node for the USB role mux, so we need to wait
190 * until the mux driver has created software node for the mux device.
191 * It means we depend on the mux driver. This function will return
192 * -EPROBE_DEFER until the mux device is registered.
194 mux_ref.node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
197 goto err_remove_nodes;
201 * The DP connector does have ACPI device node. In this case we can just
202 * find that ACPI node and assign our node as the secondary node to it.
204 ret = cht_int33fe_setup_dp(data);
206 goto err_remove_nodes;
211 cht_int33fe_remove_nodes(data);
217 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
219 struct i2c_client *max17047 = NULL;
220 struct i2c_board_info board_info;
221 struct fwnode_handle *fwnode;
224 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_MAX17047]);
228 i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
230 /* Pre-existing i2c-client for the max17047, add device-props */
231 fwnode->secondary = ERR_PTR(-ENODEV);
232 max17047->dev.fwnode->secondary = fwnode;
233 /* And re-probe to get the new device-props applied. */
234 ret = device_reprobe(&max17047->dev);
236 dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
240 memset(&board_info, 0, sizeof(board_info));
241 strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
242 board_info.dev_name = "max17047";
243 board_info.fwnode = fwnode;
244 data->battery_fg = i2c_acpi_new_device(dev, 1, &board_info);
246 return PTR_ERR_OR_ZERO(data->battery_fg);
249 int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
251 struct device *dev = data->dev;
252 struct i2c_board_info board_info;
253 struct fwnode_handle *fwnode;
254 struct regulator *regulator;
259 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
260 * We check for the bq24292i vbus regulator here, this has 2 purposes:
261 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
262 * max-snk voltage to 12V with another charger-IC is not good.
263 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
264 * regulator-map, which is part of the bq24292i regulator_init_data,
265 * must be registered before the fusb302 is instantiated, otherwise
266 * it will end up with a dummy-regulator.
267 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
268 * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
269 * gets instantiated. We use regulator_get_optional here so that we
270 * don't end up getting a dummy-regulator ourselves.
272 regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
273 if (IS_ERR(regulator)) {
274 ret = PTR_ERR(regulator);
275 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
277 regulator_put(regulator);
279 /* The FUSB302 uses the irq at index 1 and is the only irq user */
280 fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
281 if (fusb302_irq < 0) {
282 if (fusb302_irq != -EPROBE_DEFER)
283 dev_err(dev, "Error getting FUSB302 irq\n");
287 ret = cht_int33fe_add_nodes(data);
291 /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047 */
292 ret = cht_int33fe_register_max17047(dev, data);
294 goto out_remove_nodes;
296 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_FUSB302]);
299 goto out_unregister_max17047;
302 memset(&board_info, 0, sizeof(board_info));
303 strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
304 board_info.dev_name = "fusb302";
305 board_info.fwnode = fwnode;
306 board_info.irq = fusb302_irq;
308 data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
309 if (IS_ERR(data->fusb302)) {
310 ret = PTR_ERR(data->fusb302);
311 goto out_unregister_max17047;
314 fwnode = software_node_fwnode(&nodes[INT33FE_NODE_PI3USB30532]);
317 goto out_unregister_fusb302;
320 memset(&board_info, 0, sizeof(board_info));
321 board_info.dev_name = "pi3usb30532";
322 board_info.fwnode = fwnode;
323 strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
325 data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
326 if (IS_ERR(data->pi3usb30532)) {
327 ret = PTR_ERR(data->pi3usb30532);
328 goto out_unregister_fusb302;
333 out_unregister_fusb302:
334 i2c_unregister_device(data->fusb302);
336 out_unregister_max17047:
337 i2c_unregister_device(data->battery_fg);
340 cht_int33fe_remove_nodes(data);
345 int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
347 i2c_unregister_device(data->pi3usb30532);
348 i2c_unregister_device(data->fusb302);
349 i2c_unregister_device(data->battery_fg);
351 cht_int33fe_remove_nodes(data);