Linux Audio

Check our new training course

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