File Index
Files and directories
Sign in
FILE

chtwc_int33fe.c

File preview with syntax highlighting.

linux-6.19.14/drivers/platform/x86/intel/chtwc_int33fe.c 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/dmi.h>
 21#include <linux/i2c.h>
 22#include <linux/interrupt.h>
 23#include <linux/pci.h>
 24#include <linux/platform_device.h>
 25#include <linux/property.h>
 26#include <linux/regulator/consumer.h>
 27#include <linux/slab.h>
 28#include <linux/usb/pd.h>
 29
 30struct cht_int33fe_data {
 31	struct i2c_client *battery_fg;
 32	struct i2c_client *fusb302;
 33	struct i2c_client *pi3usb30532;
 34	struct fwnode_handle *dp;
 35};
 36
 37/*
 38 * Grrr, I severely dislike buggy BIOS-es. At least one BIOS enumerates
 39 * the max17047 both through the INT33FE ACPI device (it is right there
 40 * in the resources table) as well as through a separate MAX17047 device.
 41 *
 42 * These helpers are used to work around this by checking if an I²C client
 43 * for the max17047 has already been registered.
 44 */
 45static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
 46{
 47	struct i2c_client **max17047 = data;
 48	struct acpi_device *adev;
 49
 50	adev = ACPI_COMPANION(dev);
 51	if (!adev)
 52		return 0;
 53
 54	/* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
 55	if (!acpi_dev_hid_uid_match(adev, "MAX17047", NULL))
 56		return 0;
 57
 58	*max17047 = to_i2c_client(dev);
 59	return 1;
 60}
 61
 62static const char * const max17047_suppliers[] = { "bq24190-charger" };
 63
 64static const struct property_entry max17047_properties[] = {
 65	PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
 66	{ }
 67};
 68
 69static const struct software_node max17047_node = {
 70	.name = "max17047",
 71	.properties = max17047_properties,
 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 */
 79static struct software_node_ref_args fusb302_mux_refs[] = {
 80	SOFTWARE_NODE_REFERENCE(NULL),
 81};
 82
 83static const struct property_entry fusb302_properties[] = {
 84	PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
 85	PROPERTY_ENTRY_REF_ARRAY("usb-role-switch", fusb302_mux_refs),
 86	{ }
 87};
 88
 89static const struct software_node fusb302_node = {
 90	.name = "fusb302",
 91	.properties = fusb302_properties,
 92};
 93
 94#define PDO_FIXED_FLAGS \
 95	(PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
 96
 97static const u32 src_pdo[] = {
 98	PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
 99};
100
101static const u32 snk_pdo[] = {
102	PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
103	PDO_VAR(5000, 12000, 3000),
104};
105
106static const struct software_node pi3usb30532_node = {
107	.name = "pi3usb30532",
108};
109
110static const struct software_node displayport_node = {
111	.name = "displayport",
112};
113
114static const struct property_entry usb_connector_properties[] = {
115	PROPERTY_ENTRY_STRING("data-role", "dual"),
116	PROPERTY_ENTRY_STRING("power-role", "dual"),
117	PROPERTY_ENTRY_STRING("try-power-role", "sink"),
118	PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
119	PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
120	PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
121	PROPERTY_ENTRY_REF("orientation-switch", &pi3usb30532_node),
122	PROPERTY_ENTRY_REF("mode-switch", &pi3usb30532_node),
123	PROPERTY_ENTRY_REF("displayport", &displayport_node),
124	{ }
125};
126
127static const struct software_node usb_connector_node = {
128	.name = "connector",
129	.parent = &fusb302_node,
130	.properties = usb_connector_properties,
131};
132
133static const struct software_node altmodes_node = {
134	.name = "altmodes",
135	.parent = &usb_connector_node,
136};
137
138static const struct property_entry dp_altmode_properties[] = {
139	PROPERTY_ENTRY_U16("svid", 0xff01),
140	PROPERTY_ENTRY_U32("vdo", 0x0c0086),
141	{ }
142};
143
144static const struct software_node dp_altmode_node = {
145	.name = "displayport-altmode",
146	.parent = &altmodes_node,
147	.properties = dp_altmode_properties,
148};
149
150static const struct software_node *node_group[] = {
151	&fusb302_node,
152	&max17047_node,
153	&pi3usb30532_node,
154	&displayport_node,
155	&usb_connector_node,
156	&altmodes_node,
157	&dp_altmode_node,
158	NULL
159};
160
161static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
162{
163	struct fwnode_handle *fwnode;
164	struct pci_dev *pdev;
165
166	fwnode = software_node_fwnode(&displayport_node);
167	if (!fwnode)
168		return -ENODEV;
169
170	/* First let's find the GPU PCI device */
171	pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
172	if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
173		pci_dev_put(pdev);
174		return -ENODEV;
175	}
176
177	/* Then the DP-2 child device node */
178	data->dp = device_get_named_child_node(&pdev->dev, "DD04");
179	pci_dev_put(pdev);
180	if (!data->dp)
181		return -ENODEV;
182
183	fwnode->secondary = ERR_PTR(-ENODEV);
184	data->dp->secondary = fwnode;
185
186	return 0;
187}
188
189static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
190{
191	software_node_unregister_node_group(node_group);
192
193	if (data->dp) {
194		data->dp->secondary = NULL;
195		fwnode_handle_put(data->dp);
196		data->dp = NULL;
197	}
198}
199
200static void cht_int33fe_put_swnode(void *data)
201{
202	struct fwnode_handle *fwnode = data;
203
204	fwnode_handle_put(fwnode);
205	fusb302_mux_refs[0] = SOFTWARE_NODE_REFERENCE(NULL);
206}
207
208static int cht_int33fe_add_nodes(struct device *dev, struct cht_int33fe_data *data)
209{
210	const struct software_node *mux_ref_node;
211	int ret;
212
213	/*
214	 * There is no ACPI device node for the USB role mux, so we need to wait
215	 * until the mux driver has created software node for the mux device.
216	 * It means we depend on the mux driver. This function will return
217	 * -EPROBE_DEFER until the mux device is registered.
218	 *
219	 * FIXME: the relevant software node exists in intel-xhci-usb-role-switch
220	 * and - if exported - could be used to set up a static reference.
221	 */
222	mux_ref_node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
223	if (!mux_ref_node)
224		return -EPROBE_DEFER;
225
226	ret = devm_add_action_or_reset(dev, cht_int33fe_put_swnode,
227				       software_node_fwnode(mux_ref_node));
228	if (ret)
229		return ret;
230
231	/*
232	 * Update node used in "usb-role-switch" property. Note that we
233	 * rely on software_node_register_node_group() to use the original
234	 * instance of properties instead of copying them.
235	 */
236	fusb302_mux_refs[0] = SOFTWARE_NODE_REFERENCE(mux_ref_node);
237
238	ret = software_node_register_node_group(node_group);
239	if (ret)
240		return ret;
241
242	/* The devices that are not created in this driver need extra steps. */
243
244	/*
245	 * The DP connector does have ACPI device node. In this case we can just
246	 * find that ACPI node and assign our node as the secondary node to it.
247	 */
248	ret = cht_int33fe_setup_dp(data);
249	if (ret)
250		goto err_remove_nodes;
251
252	return 0;
253
254err_remove_nodes:
255	cht_int33fe_remove_nodes(data);
256
257	return ret;
258}
259
260static int
261cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
262{
263	struct i2c_client *max17047 = NULL;
264	struct i2c_board_info board_info;
265	struct fwnode_handle *fwnode;
266	int ret;
267
268	fwnode = software_node_fwnode(&max17047_node);
269	if (!fwnode)
270		return -ENODEV;
271
272	i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
273	if (max17047) {
274		/* Pre-existing I²C client for the max17047, add device properties */
275		set_secondary_fwnode(&max17047->dev, fwnode);
276		/* And re-probe to get the new device properties applied */
277		ret = device_reprobe(&max17047->dev);
278		if (ret)
279			dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
280		return 0;
281	}
282
283	memset(&board_info, 0, sizeof(board_info));
284	strscpy(board_info.type, "max17047");
285	board_info.dev_name = "max17047";
286	board_info.fwnode = fwnode;
287	data->battery_fg = i2c_acpi_new_device(dev, 1, &board_info);
288
289	return PTR_ERR_OR_ZERO(data->battery_fg);
290}
291
292static const struct dmi_system_id cht_int33fe_typec_ids[] = {
293	{
294		/*
295		 * GPD win / GPD pocket mini laptops
296		 *
297		 * This DMI match may not seem unique, but it is. In the 67000+
298		 * DMI decode dumps from linux-hardware.org only 116 have
299		 * board_vendor set to "AMI Corporation" and of those 116 only
300		 * the GPD win's and pocket's board_name is "Default string".
301		 */
302		.matches = {
303			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
304			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Default string"),
305			DMI_EXACT_MATCH(DMI_BOARD_SERIAL, "Default string"),
306			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Default string"),
307		},
308	},
309	{ }
310};
311MODULE_DEVICE_TABLE(dmi, cht_int33fe_typec_ids);
312
313static int cht_int33fe_typec_probe(struct platform_device *pdev)
314{
315	struct i2c_board_info board_info;
316	struct device *dev = &pdev->dev;
317	struct cht_int33fe_data *data;
318	struct fwnode_handle *fwnode;
319	struct regulator *regulator;
320	int fusb302_irq;
321	int ret;
322
323	if (!dmi_check_system(cht_int33fe_typec_ids))
324		return -ENODEV;
325
326	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
327	if (!data)
328		return -ENOMEM;
329
330	/*
331	 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
332	 * We check for the bq24292i vbus regulator here, this has 2 purposes:
333	 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
334	 *    max-snk voltage to 12V with another charger-IC is not good.
335	 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
336	 *    regulator-map, which is part of the bq24292i regulator_init_data,
337	 *    must be registered before the fusb302 is instantiated, otherwise
338	 *    it will end up with a dummy-regulator.
339	 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
340	 * which is defined in i2c-cht-wc.c from where the bq24292i I²C client
341	 * gets instantiated. We use regulator_get_optional here so that we
342	 * don't end up getting a dummy-regulator ourselves.
343	 */
344	regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
345	if (IS_ERR(regulator)) {
346		ret = PTR_ERR(regulator);
347		return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
348	}
349	regulator_put(regulator);
350
351	/* The FUSB302 uses the IRQ at index 1 and is the only IRQ user */
352	fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
353	if (fusb302_irq < 0) {
354		if (fusb302_irq != -EPROBE_DEFER)
355			dev_err(dev, "Error getting FUSB302 irq\n");
356		return fusb302_irq;
357	}
358
359	ret = cht_int33fe_add_nodes(dev, data);
360	if (ret)
361		return ret;
362
363	/* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047() */
364	ret = cht_int33fe_register_max17047(dev, data);
365	if (ret)
366		goto out_remove_nodes;
367
368	fwnode = software_node_fwnode(&fusb302_node);
369	if (!fwnode) {
370		ret = -ENODEV;
371		goto out_unregister_max17047;
372	}
373
374	memset(&board_info, 0, sizeof(board_info));
375	strscpy(board_info.type, "typec_fusb302");
376	board_info.dev_name = "fusb302";
377	board_info.fwnode = fwnode;
378	board_info.irq = fusb302_irq;
379
380	data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
381	if (IS_ERR(data->fusb302)) {
382		ret = PTR_ERR(data->fusb302);
383		goto out_unregister_max17047;
384	}
385
386	fwnode = software_node_fwnode(&pi3usb30532_node);
387	if (!fwnode) {
388		ret = -ENODEV;
389		goto out_unregister_fusb302;
390	}
391
392	memset(&board_info, 0, sizeof(board_info));
393	board_info.dev_name = "pi3usb30532";
394	board_info.fwnode = fwnode;
395	strscpy(board_info.type, "pi3usb30532");
396
397	data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
398	if (IS_ERR(data->pi3usb30532)) {
399		ret = PTR_ERR(data->pi3usb30532);
400		goto out_unregister_fusb302;
401	}
402
403	platform_set_drvdata(pdev, data);
404
405	return 0;
406
407out_unregister_fusb302:
408	i2c_unregister_device(data->fusb302);
409
410out_unregister_max17047:
411	i2c_unregister_device(data->battery_fg);
412
413out_remove_nodes:
414	cht_int33fe_remove_nodes(data);
415
416	return ret;
417}
418
419static void cht_int33fe_typec_remove(struct platform_device *pdev)
420{
421	struct cht_int33fe_data *data = platform_get_drvdata(pdev);
422
423	i2c_unregister_device(data->pi3usb30532);
424	i2c_unregister_device(data->fusb302);
425	i2c_unregister_device(data->battery_fg);
426
427	cht_int33fe_remove_nodes(data);
428}
429
430static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
431	{ "INT33FE", },
432	{ }
433};
434
435static struct platform_driver cht_int33fe_typec_driver = {
436	.driver	= {
437		.name = "Intel Cherry Trail ACPI INT33FE Type-C driver",
438		.acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
439	},
440	.probe = cht_int33fe_typec_probe,
441	.remove = cht_int33fe_typec_remove,
442};
443
444module_platform_driver(cht_int33fe_typec_driver);
445
446MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE Type-C pseudo device driver");
447MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
448MODULE_LICENSE("GPL v2");
~/ Read-only file preview.