Merge tag 'pci-v5.10-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-microblaze.git] / drivers / platform / x86 / intel_cht_int33fe_typec.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Cherry Trail ACPI INT33FE pseudo device driver
4  *
5  * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
6  *
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
14  *
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.
18  */
19
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>
28
29 #include "intel_cht_int33fe_common.h"
30
31 /*
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.
35  *
36  * These helpers are used to work around this by checking if an I²C client
37  * for the max17047 has already been registered.
38  */
39 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
40 {
41         struct i2c_client **max17047 = data;
42         struct acpi_device *adev;
43
44         adev = ACPI_COMPANION(dev);
45         if (!adev)
46                 return 0;
47
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))
50                 return 0;
51
52         *max17047 = to_i2c_client(dev);
53         return 1;
54 }
55
56 static const char * const max17047_suppliers[] = { "bq24190-charger" };
57
58 static const struct property_entry max17047_properties[] = {
59         PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
60         { }
61 };
62
63 static const struct software_node max17047_node = {
64         .name = "max17047",
65         .properties = max17047_properties,
66 };
67
68 /*
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
71  * software node.
72  */
73 static struct software_node_ref_args fusb302_mux_refs[] = {
74         { .node = NULL },
75 };
76
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),
80         { }
81 };
82
83 static const struct software_node fusb302_node = {
84         .name = "fusb302",
85         .properties = fusb302_properties,
86 };
87
88 #define PDO_FIXED_FLAGS \
89         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
90
91 static const u32 src_pdo[] = {
92         PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
93 };
94
95 static const u32 snk_pdo[] = {
96         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
97         PDO_VAR(5000, 12000, 3000),
98 };
99
100 static const struct software_node pi3usb30532_node = {
101         .name = "pi3usb30532",
102 };
103
104 static const struct software_node displayport_node = {
105         .name = "displayport",
106 };
107
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),
118         { }
119 };
120
121 static const struct software_node usb_connector_node = {
122         .name = "connector",
123         .parent = &fusb302_node,
124         .properties = usb_connector_properties,
125 };
126
127 static const struct software_node *node_group[] = {
128         &fusb302_node,
129         &max17047_node,
130         &pi3usb30532_node,
131         &displayport_node,
132         &usb_connector_node,
133         NULL
134 };
135
136 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
137 {
138         struct fwnode_handle *fwnode;
139         struct pci_dev *pdev;
140
141         fwnode = software_node_fwnode(&displayport_node);
142         if (!fwnode)
143                 return -ENODEV;
144
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) {
148                 pci_dev_put(pdev);
149                 return -ENODEV;
150         }
151
152         /* Then the DP child device node */
153         data->dp = device_get_named_child_node(&pdev->dev, "DD02");
154         pci_dev_put(pdev);
155         if (!data->dp)
156                 return -ENODEV;
157
158         fwnode->secondary = ERR_PTR(-ENODEV);
159         data->dp->secondary = fwnode;
160
161         return 0;
162 }
163
164 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
165 {
166         software_node_unregister_node_group(node_group);
167
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;
171         }
172
173         if (data->dp) {
174                 data->dp->secondary = NULL;
175                 fwnode_handle_put(data->dp);
176                 data->dp = NULL;
177         }
178 }
179
180 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
181 {
182         const struct software_node *mux_ref_node;
183         int ret;
184
185         /*
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.
190          */
191         mux_ref_node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
192         if (!mux_ref_node)
193                 return -EPROBE_DEFER;
194
195         /*
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.
199          */
200         fusb302_mux_refs[0].node = mux_ref_node;
201
202         ret = software_node_register_node_group(node_group);
203         if (ret)
204                 return ret;
205
206         /* The devices that are not created in this driver need extra steps. */
207
208         /*
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.
211          */
212         ret = cht_int33fe_setup_dp(data);
213         if (ret)
214                 goto err_remove_nodes;
215
216         return 0;
217
218 err_remove_nodes:
219         cht_int33fe_remove_nodes(data);
220
221         return ret;
222 }
223
224 static int
225 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
226 {
227         struct i2c_client *max17047 = NULL;
228         struct i2c_board_info board_info;
229         struct fwnode_handle *fwnode;
230         int ret;
231
232         fwnode = software_node_fwnode(&max17047_node);
233         if (!fwnode)
234                 return -ENODEV;
235
236         i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
237         if (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);
242                 if (ret)
243                         dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
244                 return 0;
245         }
246
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);
252
253         return PTR_ERR_OR_ZERO(data->battery_fg);
254 }
255
256 int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
257 {
258         struct device *dev = data->dev;
259         struct i2c_board_info board_info;
260         struct fwnode_handle *fwnode;
261         struct regulator *regulator;
262         int fusb302_irq;
263         int ret;
264
265         /*
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.
278          */
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;
283         }
284         regulator_put(regulator);
285
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");
291                 return fusb302_irq;
292         }
293
294         ret = cht_int33fe_add_nodes(data);
295         if (ret)
296                 return ret;
297
298         /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047() */
299         ret = cht_int33fe_register_max17047(dev, data);
300         if (ret)
301                 goto out_remove_nodes;
302
303         fwnode = software_node_fwnode(&fusb302_node);
304         if (!fwnode) {
305                 ret = -ENODEV;
306                 goto out_unregister_max17047;
307         }
308
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;
314
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;
319         }
320
321         fwnode = software_node_fwnode(&pi3usb30532_node);
322         if (!fwnode) {
323                 ret = -ENODEV;
324                 goto out_unregister_fusb302;
325         }
326
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);
331
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;
336         }
337
338         return 0;
339
340 out_unregister_fusb302:
341         i2c_unregister_device(data->fusb302);
342
343 out_unregister_max17047:
344         i2c_unregister_device(data->battery_fg);
345
346 out_remove_nodes:
347         cht_int33fe_remove_nodes(data);
348
349         return ret;
350 }
351
352 int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
353 {
354         i2c_unregister_device(data->pi3usb30532);
355         i2c_unregister_device(data->fusb302);
356         i2c_unregister_device(data->battery_fg);
357
358         cht_int33fe_remove_nodes(data);
359
360         return 0;
361 }