4fbdff48a4b52e9d288e51a30eb06ea809ade981
[linux-2.6-microblaze.git] / drivers / platform / x86 / intel_cht_int33fe.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/acpi.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/usb/pd.h>
29
30 #define EXPECTED_PTYPE          4
31
32 enum {
33         INT33FE_NODE_FUSB302,
34         INT33FE_NODE_MAX17047,
35         INT33FE_NODE_PI3USB30532,
36         INT33FE_NODE_DISPLAYPORT,
37         INT33FE_NODE_ROLE_SWITCH,
38         INT33FE_NODE_USB_CONNECTOR,
39         INT33FE_NODE_MAX,
40 };
41
42 struct cht_int33fe_data {
43         struct i2c_client *max17047;
44         struct i2c_client *fusb302;
45         struct i2c_client *pi3usb30532;
46
47         struct fwnode_handle *dp;
48         struct fwnode_handle *mux;
49 };
50
51 static const struct software_node nodes[];
52
53 static const struct software_node_ref_args pi3usb30532_ref = {
54         &nodes[INT33FE_NODE_PI3USB30532]
55 };
56
57 static const struct software_node_ref_args dp_ref = {
58         &nodes[INT33FE_NODE_DISPLAYPORT]
59 };
60
61 static struct software_node_ref_args mux_ref;
62
63 static const struct software_node_reference usb_connector_refs[] = {
64         { "orientation-switch", 1, &pi3usb30532_ref},
65         { "mode-switch", 1, &pi3usb30532_ref},
66         { "displayport", 1, &dp_ref},
67         { }
68 };
69
70 static const struct software_node_reference fusb302_refs[] = {
71         { "usb-role-switch", 1, &mux_ref},
72         { }
73 };
74
75 /*
76  * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
77  * the max17047 both through the INT33FE ACPI device (it is right there
78  * in the resources table) as well as through a separate MAX17047 device.
79  *
80  * These helpers are used to work around this by checking if an i2c-client
81  * for the max17047 has already been registered.
82  */
83 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
84 {
85         struct i2c_client **max17047 = data;
86         struct acpi_device *adev;
87         const char *hid;
88
89         adev = ACPI_COMPANION(dev);
90         if (!adev)
91                 return 0;
92
93         hid = acpi_device_hid(adev);
94
95         /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
96         if (strcmp(hid, "MAX17047"))
97                 return 0;
98
99         *max17047 = to_i2c_client(dev);
100         return 1;
101 }
102
103 static const char * const max17047_suppliers[] = { "bq24190-charger" };
104
105 static const struct property_entry max17047_props[] = {
106         PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
107         { }
108 };
109
110 static const struct property_entry fusb302_props[] = {
111         PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
112         { }
113 };
114
115 #define PDO_FIXED_FLAGS \
116         (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
117
118 static const u32 src_pdo[] = {
119         PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
120 };
121
122 static const u32 snk_pdo[] = {
123         PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
124         PDO_VAR(5000, 12000, 3000),
125 };
126
127 static const struct property_entry usb_connector_props[] = {
128         PROPERTY_ENTRY_STRING("data-role", "dual"),
129         PROPERTY_ENTRY_STRING("power-role", "dual"),
130         PROPERTY_ENTRY_STRING("try-power-role", "sink"),
131         PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
132         PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
133         PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
134         { }
135 };
136
137 static const struct software_node nodes[] = {
138         { "fusb302", NULL, fusb302_props, fusb302_refs },
139         { "max17047", NULL, max17047_props },
140         { "pi3usb30532" },
141         { "displayport" },
142         { "usb-role-switch" },
143         { "connector", &nodes[0], usb_connector_props, usb_connector_refs },
144         { }
145 };
146
147 static int cht_int33fe_setup_mux(struct cht_int33fe_data *data)
148 {
149         struct fwnode_handle *fwnode;
150         struct device *dev;
151         struct device *p;
152
153         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_ROLE_SWITCH]);
154         if (!fwnode)
155                 return -ENODEV;
156
157         /* First finding the platform device */
158         p = bus_find_device_by_name(&platform_bus_type, NULL,
159                                     "intel_xhci_usb_sw");
160         if (!p)
161                 return -EPROBE_DEFER;
162
163         /* Then the mux child device */
164         dev = device_find_child_by_name(p, "intel_xhci_usb_sw-role-switch");
165         put_device(p);
166         if (!dev)
167                 return -EPROBE_DEFER;
168
169         /* If there already is a node for the mux, using that one. */
170         if (dev->fwnode)
171                 fwnode_remove_software_node(fwnode);
172         else
173                 dev->fwnode = fwnode;
174
175         data->mux = fwnode_handle_get(dev->fwnode);
176         put_device(dev);
177         mux_ref.node = to_software_node(data->mux);
178
179         return 0;
180 }
181
182 static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
183 {
184         struct fwnode_handle *fwnode;
185         struct pci_dev *pdev;
186
187         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_DISPLAYPORT]);
188         if (!fwnode)
189                 return -ENODEV;
190
191         /* First let's find the GPU PCI device */
192         pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
193         if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
194                 pci_dev_put(pdev);
195                 return -ENODEV;
196         }
197
198         /* Then the DP child device node */
199         data->dp = device_get_named_child_node(&pdev->dev, "DD02");
200         pci_dev_put(pdev);
201         if (!data->dp)
202                 return -ENODEV;
203
204         fwnode->secondary = ERR_PTR(-ENODEV);
205         data->dp->secondary = fwnode;
206
207         return 0;
208 }
209
210 static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
211 {
212         software_node_unregister_nodes(nodes);
213
214         if (data->mux) {
215                 fwnode_handle_put(data->mux);
216                 mux_ref.node = NULL;
217                 data->mux = NULL;
218         }
219
220         if (data->dp) {
221                 data->dp->secondary = NULL;
222                 fwnode_handle_put(data->dp);
223                 data->dp = NULL;
224         }
225 }
226
227 static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
228 {
229         int ret;
230
231         ret = software_node_register_nodes(nodes);
232         if (ret)
233                 return ret;
234
235         /* The devices that are not created in this driver need extra steps. */
236
237         /*
238          * There is no ACPI device node for the USB role mux, so we need to find
239          * the mux device and assign our node directly to it. That means we
240          * depend on the mux driver. This function will return -PROBE_DEFER
241          * until the mux device is registered.
242          */
243         ret = cht_int33fe_setup_mux(data);
244         if (ret)
245                 goto err_remove_nodes;
246
247         /*
248          * The DP connector does have ACPI device node. In this case we can just
249          * find that ACPI node and assign our node as the secondary node to it.
250          */
251         ret = cht_int33fe_setup_dp(data);
252         if (ret)
253                 goto err_remove_nodes;
254
255         return 0;
256
257 err_remove_nodes:
258         cht_int33fe_remove_nodes(data);
259
260         return ret;
261 }
262
263 static int
264 cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
265 {
266         struct i2c_client *max17047 = NULL;
267         struct i2c_board_info board_info;
268         struct fwnode_handle *fwnode;
269         int ret;
270
271         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_MAX17047]);
272         if (!fwnode)
273                 return -ENODEV;
274
275         i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
276         if (max17047) {
277                 /* Pre-existing i2c-client for the max17047, add device-props */
278                 fwnode->secondary = ERR_PTR(-ENODEV);
279                 max17047->dev.fwnode->secondary = fwnode;
280                 /* And re-probe to get the new device-props applied. */
281                 ret = device_reprobe(&max17047->dev);
282                 if (ret)
283                         dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
284                 return 0;
285         }
286
287         memset(&board_info, 0, sizeof(board_info));
288         strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
289         board_info.dev_name = "max17047";
290         board_info.fwnode = fwnode;
291         data->max17047 = i2c_acpi_new_device(dev, 1, &board_info);
292
293         return PTR_ERR_OR_ZERO(data->max17047);
294 }
295
296 static int cht_int33fe_probe(struct platform_device *pdev)
297 {
298         struct device *dev = &pdev->dev;
299         struct i2c_board_info board_info;
300         struct cht_int33fe_data *data;
301         struct fwnode_handle *fwnode;
302         struct regulator *regulator;
303         unsigned long long ptyp;
304         acpi_status status;
305         int fusb302_irq;
306         int ret;
307
308         status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
309         if (ACPI_FAILURE(status)) {
310                 dev_err(dev, "Error getting PTYPE\n");
311                 return -ENODEV;
312         }
313
314         /*
315          * The same ACPI HID is used for different configurations check PTYP
316          * to ensure that we are dealing with the expected config.
317          */
318         if (ptyp != EXPECTED_PTYPE)
319                 return -ENODEV;
320
321         /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
322         if (!acpi_dev_present("INT34D3", "1", 3)) {
323                 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
324                         EXPECTED_PTYPE);
325                 return -ENODEV;
326         }
327
328         /*
329          * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
330          * We check for the bq24292i vbus regulator here, this has 2 purposes:
331          * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
332          *    max-snk voltage to 12V with another charger-IC is not good.
333          * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
334          *    regulator-map, which is part of the bq24292i regulator_init_data,
335          *    must be registered before the fusb302 is instantiated, otherwise
336          *    it will end up with a dummy-regulator.
337          * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
338          * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
339          * gets instantiated. We use regulator_get_optional here so that we
340          * don't end up getting a dummy-regulator ourselves.
341          */
342         regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
343         if (IS_ERR(regulator)) {
344                 ret = PTR_ERR(regulator);
345                 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
346         }
347         regulator_put(regulator);
348
349         /* The FUSB302 uses the irq at index 1 and is the only irq user */
350         fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
351         if (fusb302_irq < 0) {
352                 if (fusb302_irq != -EPROBE_DEFER)
353                         dev_err(dev, "Error getting FUSB302 irq\n");
354                 return fusb302_irq;
355         }
356
357         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
358         if (!data)
359                 return -ENOMEM;
360
361         ret = cht_int33fe_add_nodes(data);
362         if (ret)
363                 return ret;
364
365         /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047 */
366         ret = cht_int33fe_register_max17047(dev, data);
367         if (ret)
368                 goto out_remove_nodes;
369
370         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_FUSB302]);
371         if (!fwnode) {
372                 ret = -ENODEV;
373                 goto out_unregister_max17047;
374         }
375
376         memset(&board_info, 0, sizeof(board_info));
377         strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
378         board_info.dev_name = "fusb302";
379         board_info.fwnode = fwnode;
380         board_info.irq = fusb302_irq;
381
382         data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
383         if (IS_ERR(data->fusb302)) {
384                 ret = PTR_ERR(data->fusb302);
385                 goto out_unregister_max17047;
386         }
387
388         fwnode = software_node_fwnode(&nodes[INT33FE_NODE_PI3USB30532]);
389         if (!fwnode) {
390                 ret = -ENODEV;
391                 goto out_unregister_fusb302;
392         }
393
394         memset(&board_info, 0, sizeof(board_info));
395         board_info.dev_name = "pi3usb30532";
396         board_info.fwnode = fwnode;
397         strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
398
399         data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
400         if (IS_ERR(data->pi3usb30532)) {
401                 ret = PTR_ERR(data->pi3usb30532);
402                 goto out_unregister_fusb302;
403         }
404
405         platform_set_drvdata(pdev, data);
406
407         return 0;
408
409 out_unregister_fusb302:
410         i2c_unregister_device(data->fusb302);
411
412 out_unregister_max17047:
413         i2c_unregister_device(data->max17047);
414
415 out_remove_nodes:
416         cht_int33fe_remove_nodes(data);
417
418         return ret;
419 }
420
421 static int cht_int33fe_remove(struct platform_device *pdev)
422 {
423         struct cht_int33fe_data *data = platform_get_drvdata(pdev);
424
425         i2c_unregister_device(data->pi3usb30532);
426         i2c_unregister_device(data->fusb302);
427         i2c_unregister_device(data->max17047);
428
429         cht_int33fe_remove_nodes(data);
430
431         return 0;
432 }
433
434 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
435         { "INT33FE", },
436         { }
437 };
438 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
439
440 static struct platform_driver cht_int33fe_driver = {
441         .driver = {
442                 .name = "Intel Cherry Trail ACPI INT33FE driver",
443                 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
444         },
445         .probe = cht_int33fe_probe,
446         .remove = cht_int33fe_remove,
447 };
448
449 module_platform_driver(cht_int33fe_driver);
450
451 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
452 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
453 MODULE_LICENSE("GPL v2");