]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/intel_cht_int33fe_typec.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux.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 /*
40  * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
41  * the max17047 both through the INT33FE ACPI device (it is right there
42  * in the resources table) as well as through a separate MAX17047 device.
43  *
44  * These helpers are used to work around this by checking if an i2c-client
45  * for the max17047 has already been registered.
46  */
47 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
48 {
49         struct i2c_client **max17047 = data;
50         struct acpi_device *adev;
51         const char *hid;
52
53         adev = ACPI_COMPANION(dev);
54         if (!adev)
55                 return 0;
56
57         hid = acpi_device_hid(adev);
58
59         /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
60         if (strcmp(hid, "MAX17047"))
61                 return 0;
62
63         *max17047 = to_i2c_client(dev);
64         return 1;
65 }
66
67 static const char * const max17047_suppliers[] = { "bq24190-charger" };
68
69 static const struct property_entry max17047_props[] = {
70         PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
71         { }
72 };
73
74 /*
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
77  * software node.
78  */
79 static struct software_node_ref_args fusb302_mux_refs[] = {
80         { .node = NULL },
81 };
82
83 static const struct property_entry fusb302_props[] = {
84         PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
85         PROPERTY_ENTRY_REF_ARRAY("usb-role-switch", fusb302_mux_refs),
86         { }
87 };
88
89 #define PDO_FIXED_FLAGS \
90         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
91
92 static const u32 src_pdo[] = {
93         PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
94 };
95
96 static const u32 snk_pdo[] = {
97         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
98         PDO_VAR(5000, 12000, 3000),
99 };
100
101 static const struct software_node nodes[];
102
103 static const struct property_entry usb_connector_props[] = {
104         PROPERTY_ENTRY_STRING("data-role", "dual"),
105         PROPERTY_ENTRY_STRING("power-role", "dual"),
106         PROPERTY_ENTRY_STRING("try-power-role", "sink"),
107         PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
108         PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
109         PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
110         PROPERTY_ENTRY_REF("orientation-switch",
111                            &nodes[INT33FE_NODE_PI3USB30532]),
112         PROPERTY_ENTRY_REF("mode-switch",
113                            &nodes[INT33FE_NODE_PI3USB30532]),
114         PROPERTY_ENTRY_REF("displayport",
115                            &nodes[INT33FE_NODE_DISPLAYPORT]),
116         { }
117 };
118
119 static const struct software_node nodes[] = {
120         { "fusb302", NULL, fusb302_props },
121         { "max17047", NULL, max17047_props },
122         { "pi3usb30532" },
123         { "displayport" },
124         { "connector", &nodes[0], usb_connector_props },
125         { }
126 };
127
128 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
129 {
130         struct fwnode_handle *fwnode;
131         struct pci_dev *pdev;
132
133         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_DISPLAYPORT]);
134         if (!fwnode)
135                 return -ENODEV;
136
137         /* First let's find the GPU PCI device */
138         pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
139         if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
140                 pci_dev_put(pdev);
141                 return -ENODEV;
142         }
143
144         /* Then the DP child device node */
145         data->dp = device_get_named_child_node(&pdev->dev, "DD02");
146         pci_dev_put(pdev);
147         if (!data->dp)
148                 return -ENODEV;
149
150         fwnode->secondary = ERR_PTR(-ENODEV);
151         data->dp->secondary = fwnode;
152
153         return 0;
154 }
155
156 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
157 {
158         software_node_unregister_nodes(nodes);
159
160         if (fusb302_mux_refs[0].node) {
161                 fwnode_handle_put(
162                         software_node_fwnode(fusb302_mux_refs[0].node));
163                 fusb302_mux_refs[0].node = NULL;
164         }
165
166         if (data->dp) {
167                 data->dp->secondary = NULL;
168                 fwnode_handle_put(data->dp);
169                 data->dp = NULL;
170         }
171 }
172
173 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
174 {
175         const struct software_node *mux_ref_node;
176         int ret;
177
178         /*
179          * There is no ACPI device node for the USB role mux, so we need to wait
180          * until the mux driver has created software node for the mux device.
181          * It means we depend on the mux driver. This function will return
182          * -EPROBE_DEFER until the mux device is registered.
183          */
184         mux_ref_node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
185         if (!mux_ref_node)
186                 return -EPROBE_DEFER;
187
188         /*
189          * Update node used in "usb-role-switch" property. Note that we
190          * rely on software_node_register_nodes() to use the original
191          * instance of properties instead of copying them.
192          */
193         fusb302_mux_refs[0].node = mux_ref_node;
194
195         ret = software_node_register_nodes(nodes);
196         if (ret)
197                 return ret;
198
199         /* The devices that are not created in this driver need extra steps. */
200
201         /*
202          * The DP connector does have ACPI device node. In this case we can just
203          * find that ACPI node and assign our node as the secondary node to it.
204          */
205         ret = cht_int33fe_setup_dp(data);
206         if (ret)
207                 goto err_remove_nodes;
208
209         return 0;
210
211 err_remove_nodes:
212         cht_int33fe_remove_nodes(data);
213
214         return ret;
215 }
216
217 static int
218 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
219 {
220         struct i2c_client *max17047 = NULL;
221         struct i2c_board_info board_info;
222         struct fwnode_handle *fwnode;
223         int ret;
224
225         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_MAX17047]);
226         if (!fwnode)
227                 return -ENODEV;
228
229         i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
230         if (max17047) {
231                 /* Pre-existing i2c-client for the max17047, add device-props */
232                 fwnode->secondary = ERR_PTR(-ENODEV);
233                 max17047->dev.fwnode->secondary = fwnode;
234                 /* And re-probe to get the new device-props applied. */
235                 ret = device_reprobe(&max17047->dev);
236                 if (ret)
237                         dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
238                 return 0;
239         }
240
241         memset(&board_info, 0, sizeof(board_info));
242         strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
243         board_info.dev_name = "max17047";
244         board_info.fwnode = fwnode;
245         data->battery_fg = i2c_acpi_new_device(dev, 1, &board_info);
246
247         return PTR_ERR_OR_ZERO(data->battery_fg);
248 }
249
250 int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
251 {
252         struct device *dev = data->dev;
253         struct i2c_board_info board_info;
254         struct fwnode_handle *fwnode;
255         struct regulator *regulator;
256         int fusb302_irq;
257         int ret;
258
259         /*
260          * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
261          * We check for the bq24292i vbus regulator here, this has 2 purposes:
262          * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
263          *    max-snk voltage to 12V with another charger-IC is not good.
264          * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
265          *    regulator-map, which is part of the bq24292i regulator_init_data,
266          *    must be registered before the fusb302 is instantiated, otherwise
267          *    it will end up with a dummy-regulator.
268          * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
269          * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
270          * gets instantiated. We use regulator_get_optional here so that we
271          * don't end up getting a dummy-regulator ourselves.
272          */
273         regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
274         if (IS_ERR(regulator)) {
275                 ret = PTR_ERR(regulator);
276                 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
277         }
278         regulator_put(regulator);
279
280         /* The FUSB302 uses the irq at index 1 and is the only irq user */
281         fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
282         if (fusb302_irq < 0) {
283                 if (fusb302_irq != -EPROBE_DEFER)
284                         dev_err(dev, "Error getting FUSB302 irq\n");
285                 return fusb302_irq;
286         }
287
288         ret = cht_int33fe_add_nodes(data);
289         if (ret)
290                 return ret;
291
292         /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047 */
293         ret = cht_int33fe_register_max17047(dev, data);
294         if (ret)
295                 goto out_remove_nodes;
296
297         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_FUSB302]);
298         if (!fwnode) {
299                 ret = -ENODEV;
300                 goto out_unregister_max17047;
301         }
302
303         memset(&board_info, 0, sizeof(board_info));
304         strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
305         board_info.dev_name = "fusb302";
306         board_info.fwnode = fwnode;
307         board_info.irq = fusb302_irq;
308
309         data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
310         if (IS_ERR(data->fusb302)) {
311                 ret = PTR_ERR(data->fusb302);
312                 goto out_unregister_max17047;
313         }
314
315         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_PI3USB30532]);
316         if (!fwnode) {
317                 ret = -ENODEV;
318                 goto out_unregister_fusb302;
319         }
320
321         memset(&board_info, 0, sizeof(board_info));
322         board_info.dev_name = "pi3usb30532";
323         board_info.fwnode = fwnode;
324         strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
325
326         data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
327         if (IS_ERR(data->pi3usb30532)) {
328                 ret = PTR_ERR(data->pi3usb30532);
329                 goto out_unregister_fusb302;
330         }
331
332         return 0;
333
334 out_unregister_fusb302:
335         i2c_unregister_device(data->fusb302);
336
337 out_unregister_max17047:
338         i2c_unregister_device(data->battery_fg);
339
340 out_remove_nodes:
341         cht_int33fe_remove_nodes(data);
342
343         return ret;
344 }
345
346 int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
347 {
348         i2c_unregister_device(data->pi3usb30532);
349         i2c_unregister_device(data->fusb302);
350         i2c_unregister_device(data->battery_fg);
351
352         cht_int33fe_remove_nodes(data);
353
354         return 0;
355 }