Merge tag 'gfs2-4.20.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2...
[sfrench/cifs-2.6.git] / drivers / platform / x86 / intel_cht_int33fe.c
1 /*
2  * Intel Cherry Trail ACPI INT33FE pseudo device driver
3  *
4  * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Some Intel Cherry Trail based device which ship with Windows 10, have
11  * this weird INT33FE ACPI device with a CRS table with 4 I2cSerialBusV2
12  * resources, for 4 different chips attached to various i2c busses:
13  * 1. The Whiskey Cove pmic, which is also described by the INT34D3 ACPI device
14  * 2. Maxim MAX17047 Fuel Gauge Controller
15  * 3. FUSB302 USB Type-C Controller
16  * 4. PI3USB30532 USB switch
17  *
18  * So this driver is a stub / pseudo driver whose only purpose is to
19  * instantiate i2c-clients for chips 2 - 4, so that standard i2c drivers
20  * for these chips can bind to the them.
21  */
22
23 #include <linux/acpi.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30
31 #define EXPECTED_PTYPE          4
32
33 struct cht_int33fe_data {
34         struct i2c_client *max17047;
35         struct i2c_client *fusb302;
36         struct i2c_client *pi3usb30532;
37         /* Contain a list-head must be per device */
38         struct device_connection connections[3];
39 };
40
41 /*
42  * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
43  * the max17047 both through the INT33FE ACPI device (it is right there
44  * in the resources table) as well as through a separate MAX17047 device.
45  *
46  * These helpers are used to work around this by checking if an i2c-client
47  * for the max17047 has already been registered.
48  */
49 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
50 {
51         struct i2c_client **max17047 = data;
52         struct acpi_device *adev;
53         const char *hid;
54
55         adev = ACPI_COMPANION(dev);
56         if (!adev)
57                 return 0;
58
59         hid = acpi_device_hid(adev);
60
61         /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
62         if (strcmp(hid, "MAX17047"))
63                 return 0;
64
65         *max17047 = to_i2c_client(dev);
66         return 1;
67 }
68
69 static struct i2c_client *cht_int33fe_find_max17047(void)
70 {
71         struct i2c_client *max17047 = NULL;
72
73         i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
74         return max17047;
75 }
76
77 static const char * const max17047_suppliers[] = { "bq24190-charger" };
78
79 static const struct property_entry max17047_props[] = {
80         PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
81         { }
82 };
83
84 static const struct property_entry fusb302_props[] = {
85         PROPERTY_ENTRY_STRING("fcs,extcon-name", "cht_wcove_pwrsrc"),
86         PROPERTY_ENTRY_U32("fcs,max-sink-microvolt", 12000000),
87         PROPERTY_ENTRY_U32("fcs,max-sink-microamp",   3000000),
88         PROPERTY_ENTRY_U32("fcs,max-sink-microwatt", 36000000),
89         { }
90 };
91
92 static int cht_int33fe_probe(struct platform_device *pdev)
93 {
94         struct device *dev = &pdev->dev;
95         struct i2c_board_info board_info;
96         struct cht_int33fe_data *data;
97         struct i2c_client *max17047;
98         struct regulator *regulator;
99         unsigned long long ptyp;
100         acpi_status status;
101         int fusb302_irq;
102         int ret;
103
104         status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
105         if (ACPI_FAILURE(status)) {
106                 dev_err(dev, "Error getting PTYPE\n");
107                 return -ENODEV;
108         }
109
110         /*
111          * The same ACPI HID is used for different configurations check PTYP
112          * to ensure that we are dealing with the expected config.
113          */
114         if (ptyp != EXPECTED_PTYPE)
115                 return -ENODEV;
116
117         /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
118         if (!acpi_dev_present("INT34D3", "1", 3)) {
119                 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
120                         EXPECTED_PTYPE);
121                 return -ENODEV;
122         }
123
124         /*
125          * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
126          * We check for the bq24292i vbus regulator here, this has 2 purposes:
127          * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
128          *    max-snk voltage to 12V with another charger-IC is not good.
129          * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
130          *    regulator-map, which is part of the bq24292i regulator_init_data,
131          *    must be registered before the fusb302 is instantiated, otherwise
132          *    it will end up with a dummy-regulator.
133          * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
134          * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
135          * gets instantiated. We use regulator_get_optional here so that we
136          * don't end up getting a dummy-regulator ourselves.
137          */
138         regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
139         if (IS_ERR(regulator)) {
140                 ret = PTR_ERR(regulator);
141                 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
142         }
143         regulator_put(regulator);
144
145         /* The FUSB302 uses the irq at index 1 and is the only irq user */
146         fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
147         if (fusb302_irq < 0) {
148                 if (fusb302_irq != -EPROBE_DEFER)
149                         dev_err(dev, "Error getting FUSB302 irq\n");
150                 return fusb302_irq;
151         }
152
153         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
154         if (!data)
155                 return -ENOMEM;
156
157         /* Work around BIOS bug, see comment on cht_int33fe_find_max17047 */
158         max17047 = cht_int33fe_find_max17047();
159         if (max17047) {
160                 /* Pre-existing i2c-client for the max17047, add device-props */
161                 ret = device_add_properties(&max17047->dev, max17047_props);
162                 if (ret)
163                         return ret;
164                 /* And re-probe to get the new device-props applied. */
165                 ret = device_reprobe(&max17047->dev);
166                 if (ret)
167                         dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
168         } else {
169                 memset(&board_info, 0, sizeof(board_info));
170                 strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
171                 board_info.dev_name = "max17047";
172                 board_info.properties = max17047_props;
173                 data->max17047 = i2c_acpi_new_device(dev, 1, &board_info);
174                 if (!data->max17047)
175                         return -EPROBE_DEFER; /* Wait for i2c-adapter to load */
176         }
177
178         data->connections[0].endpoint[0] = "i2c-fusb302";
179         data->connections[0].endpoint[1] = "i2c-pi3usb30532";
180         data->connections[0].id = "typec-switch";
181         data->connections[1].endpoint[0] = "i2c-fusb302";
182         data->connections[1].endpoint[1] = "i2c-pi3usb30532";
183         data->connections[1].id = "typec-mux";
184         data->connections[2].endpoint[0] = "i2c-fusb302";
185         data->connections[2].endpoint[1] = "intel_xhci_usb_sw-role-switch";
186         data->connections[2].id = "usb-role-switch";
187
188         device_connection_add(&data->connections[0]);
189         device_connection_add(&data->connections[1]);
190         device_connection_add(&data->connections[2]);
191
192         memset(&board_info, 0, sizeof(board_info));
193         strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
194         board_info.dev_name = "fusb302";
195         board_info.properties = fusb302_props;
196         board_info.irq = fusb302_irq;
197
198         data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
199         if (!data->fusb302)
200                 goto out_unregister_max17047;
201
202         memset(&board_info, 0, sizeof(board_info));
203         board_info.dev_name = "pi3usb30532";
204         strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
205
206         data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
207         if (!data->pi3usb30532)
208                 goto out_unregister_fusb302;
209
210         platform_set_drvdata(pdev, data);
211
212         return 0;
213
214 out_unregister_fusb302:
215         i2c_unregister_device(data->fusb302);
216
217 out_unregister_max17047:
218         if (data->max17047)
219                 i2c_unregister_device(data->max17047);
220
221         device_connection_remove(&data->connections[2]);
222         device_connection_remove(&data->connections[1]);
223         device_connection_remove(&data->connections[0]);
224
225         return -EPROBE_DEFER; /* Wait for the i2c-adapter to load */
226 }
227
228 static int cht_int33fe_remove(struct platform_device *pdev)
229 {
230         struct cht_int33fe_data *data = platform_get_drvdata(pdev);
231
232         i2c_unregister_device(data->pi3usb30532);
233         i2c_unregister_device(data->fusb302);
234         if (data->max17047)
235                 i2c_unregister_device(data->max17047);
236
237         device_connection_remove(&data->connections[2]);
238         device_connection_remove(&data->connections[1]);
239         device_connection_remove(&data->connections[0]);
240
241         return 0;
242 }
243
244 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
245         { "INT33FE", },
246         { }
247 };
248 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
249
250 static struct platform_driver cht_int33fe_driver = {
251         .driver = {
252                 .name = "Intel Cherry Trail ACPI INT33FE driver",
253                 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
254         },
255         .probe = cht_int33fe_probe,
256         .remove = cht_int33fe_remove,
257 };
258
259 module_platform_driver(cht_int33fe_driver);
260
261 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
262 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
263 MODULE_LICENSE("GPL");