2d097fc2dd465b4879f5ba4465000a8d4e3c6364
[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 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
14  *
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.
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/regulator/consumer.h>
25 #include <linux/slab.h>
26 #include <linux/usb/pd.h>
27
28 #include "intel_cht_int33fe_common.h"
29
30 enum {
31         INT33FE_NODE_FUSB302,
32         INT33FE_NODE_MAX17047,
33         INT33FE_NODE_PI3USB30532,
34         INT33FE_NODE_DISPLAYPORT,
35         INT33FE_NODE_USB_CONNECTOR,
36         INT33FE_NODE_MAX,
37 };
38
39 static const struct software_node nodes[];
40
41 static const struct software_node_ref_args pi3usb30532_ref = {
42         &nodes[INT33FE_NODE_PI3USB30532]
43 };
44
45 static const struct software_node_ref_args dp_ref = {
46         &nodes[INT33FE_NODE_DISPLAYPORT]
47 };
48
49 static struct software_node_ref_args mux_ref;
50
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},
55         { }
56 };
57
58 static const struct software_node_reference fusb302_refs[] = {
59         { "usb-role-switch", 1, &mux_ref},
60         { }
61 };
62
63 /*
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.
67  *
68  * These helpers are used to work around this by checking if an i2c-client
69  * for the max17047 has already been registered.
70  */
71 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
72 {
73         struct i2c_client **max17047 = data;
74         struct acpi_device *adev;
75         const char *hid;
76
77         adev = ACPI_COMPANION(dev);
78         if (!adev)
79                 return 0;
80
81         hid = acpi_device_hid(adev);
82
83         /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
84         if (strcmp(hid, "MAX17047"))
85                 return 0;
86
87         *max17047 = to_i2c_client(dev);
88         return 1;
89 }
90
91 static const char * const max17047_suppliers[] = { "bq24190-charger" };
92
93 static const struct property_entry max17047_props[] = {
94         PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
95         { }
96 };
97
98 static const struct property_entry fusb302_props[] = {
99         PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
100         { }
101 };
102
103 #define PDO_FIXED_FLAGS \
104         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
105
106 static const u32 src_pdo[] = {
107         PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
108 };
109
110 static const u32 snk_pdo[] = {
111         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
112         PDO_VAR(5000, 12000, 3000),
113 };
114
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),
122         { }
123 };
124
125 static const struct software_node nodes[] = {
126         { "fusb302", NULL, fusb302_props, fusb302_refs },
127         { "max17047", NULL, max17047_props },
128         { "pi3usb30532" },
129         { "displayport" },
130         { "connector", &nodes[0], usb_connector_props, usb_connector_refs },
131         { }
132 };
133
134 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
135 {
136         struct fwnode_handle *fwnode;
137         struct pci_dev *pdev;
138
139         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_DISPLAYPORT]);
140         if (!fwnode)
141                 return -ENODEV;
142
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) {
146                 pci_dev_put(pdev);
147                 return -ENODEV;
148         }
149
150         /* Then the DP child device node */
151         data->dp = device_get_named_child_node(&pdev->dev, "DD02");
152         pci_dev_put(pdev);
153         if (!data->dp)
154                 return -ENODEV;
155
156         fwnode->secondary = ERR_PTR(-ENODEV);
157         data->dp->secondary = fwnode;
158
159         return 0;
160 }
161
162 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
163 {
164         software_node_unregister_nodes(nodes);
165
166         if (mux_ref.node) {
167                 fwnode_handle_put(software_node_fwnode(mux_ref.node));
168                 mux_ref.node = NULL;
169         }
170
171         if (data->dp) {
172                 data->dp->secondary = NULL;
173                 fwnode_handle_put(data->dp);
174                 data->dp = NULL;
175         }
176 }
177
178 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
179 {
180         int ret;
181
182         ret = software_node_register_nodes(nodes);
183         if (ret)
184                 return ret;
185
186         /* The devices that are not created in this driver need extra steps. */
187
188         /*
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.
193          */
194         mux_ref.node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
195         if (!mux_ref.node) {
196                 ret = -EPROBE_DEFER;
197                 goto err_remove_nodes;
198         }
199
200         /*
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.
203          */
204         ret = cht_int33fe_setup_dp(data);
205         if (ret)
206                 goto err_remove_nodes;
207
208         return 0;
209
210 err_remove_nodes:
211         cht_int33fe_remove_nodes(data);
212
213         return ret;
214 }
215
216 static int
217 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
218 {
219         struct i2c_client *max17047 = NULL;
220         struct i2c_board_info board_info;
221         struct fwnode_handle *fwnode;
222         int ret;
223
224         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_MAX17047]);
225         if (!fwnode)
226                 return -ENODEV;
227
228         i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
229         if (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);
235                 if (ret)
236                         dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
237                 return 0;
238         }
239
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);
245
246         return PTR_ERR_OR_ZERO(data->battery_fg);
247 }
248
249 int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
250 {
251         struct device *dev = data->dev;
252         struct i2c_board_info board_info;
253         struct fwnode_handle *fwnode;
254         struct regulator *regulator;
255         int fusb302_irq;
256         int ret;
257
258         /*
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.
271          */
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;
276         }
277         regulator_put(regulator);
278
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");
284                 return fusb302_irq;
285         }
286
287         ret = cht_int33fe_add_nodes(data);
288         if (ret)
289                 return ret;
290
291         /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047 */
292         ret = cht_int33fe_register_max17047(dev, data);
293         if (ret)
294                 goto out_remove_nodes;
295
296         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_FUSB302]);
297         if (!fwnode) {
298                 ret = -ENODEV;
299                 goto out_unregister_max17047;
300         }
301
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;
307
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;
312         }
313
314         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_PI3USB30532]);
315         if (!fwnode) {
316                 ret = -ENODEV;
317                 goto out_unregister_fusb302;
318         }
319
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);
324
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;
329         }
330
331         return 0;
332
333 out_unregister_fusb302:
334         i2c_unregister_device(data->fusb302);
335
336 out_unregister_max17047:
337         i2c_unregister_device(data->battery_fg);
338
339 out_remove_nodes:
340         cht_int33fe_remove_nodes(data);
341
342         return ret;
343 }
344
345 int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
346 {
347         i2c_unregister_device(data->pi3usb30532);
348         i2c_unregister_device(data->fusb302);
349         i2c_unregister_device(data->battery_fg);
350
351         cht_int33fe_remove_nodes(data);
352
353         return 0;
354 }