162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * API for creating and destroying USB onboard hub platform devices
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2022, Google LLC
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci#include <linux/export.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/list.h>
1262306a36Sopenharmony_ci#include <linux/of.h>
1362306a36Sopenharmony_ci#include <linux/of_platform.h>
1462306a36Sopenharmony_ci#include <linux/platform_device.h>
1562306a36Sopenharmony_ci#include <linux/usb.h>
1662306a36Sopenharmony_ci#include <linux/usb/hcd.h>
1762306a36Sopenharmony_ci#include <linux/usb/of.h>
1862306a36Sopenharmony_ci#include <linux/usb/onboard_hub.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include "onboard_usb_hub.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct pdev_list_entry {
2362306a36Sopenharmony_ci	struct platform_device *pdev;
2462306a36Sopenharmony_ci	struct list_head node;
2562306a36Sopenharmony_ci};
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic bool of_is_onboard_usb_hub(const struct device_node *np)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	return !!of_match_node(onboard_hub_match, np);
3062306a36Sopenharmony_ci}
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/**
3362306a36Sopenharmony_ci * onboard_hub_create_pdevs -- create platform devices for onboard USB hubs
3462306a36Sopenharmony_ci * @parent_hub	: parent hub to scan for connected onboard hubs
3562306a36Sopenharmony_ci * @pdev_list	: list of onboard hub platform devices owned by the parent hub
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * Creates a platform device for each supported onboard hub that is connected to
3862306a36Sopenharmony_ci * the given parent hub. The platform device is in charge of initializing the
3962306a36Sopenharmony_ci * hub (enable regulators, take the hub out of reset, ...) and can optionally
4062306a36Sopenharmony_ci * control whether the hub remains powered during system suspend or not.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * To keep track of the platform devices they are added to a list that is owned
4362306a36Sopenharmony_ci * by the parent hub.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * Some background about the logic in this function, which can be a bit hard
4662306a36Sopenharmony_ci * to follow:
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * Root hubs don't have dedicated device tree nodes, but use the node of their
4962306a36Sopenharmony_ci * HCD. The primary and secondary HCD are usually represented by a single DT
5062306a36Sopenharmony_ci * node. That means the root hubs of the primary and secondary HCD share the
5162306a36Sopenharmony_ci * same device tree node (the HCD node). As a result this function can be called
5262306a36Sopenharmony_ci * twice with the same DT node for root hubs. We only want to create a single
5362306a36Sopenharmony_ci * platform device for each physical onboard hub, hence for root hubs the loop
5462306a36Sopenharmony_ci * is only executed for the root hub of the primary HCD. Since the function
5562306a36Sopenharmony_ci * scans through all child nodes it still creates pdevs for onboard hubs
5662306a36Sopenharmony_ci * connected to the root hub of the secondary HCD if needed.
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * Further there must be only one platform device for onboard hubs with a peer
5962306a36Sopenharmony_ci * hub (the hub is a single physical device). To achieve this two measures are
6062306a36Sopenharmony_ci * taken: pdevs for onboard hubs with a peer are only created when the function
6162306a36Sopenharmony_ci * is called on behalf of the parent hub that is connected to the primary HCD
6262306a36Sopenharmony_ci * (directly or through other hubs). For onboard hubs connected to root hubs
6362306a36Sopenharmony_ci * the function processes the nodes of both peers. A platform device is only
6462306a36Sopenharmony_ci * created if the peer hub doesn't have one already.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_civoid onboard_hub_create_pdevs(struct usb_device *parent_hub, struct list_head *pdev_list)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	int i;
6962306a36Sopenharmony_ci	struct usb_hcd *hcd = bus_to_hcd(parent_hub->bus);
7062306a36Sopenharmony_ci	struct device_node *np, *npc;
7162306a36Sopenharmony_ci	struct platform_device *pdev;
7262306a36Sopenharmony_ci	struct pdev_list_entry *pdle;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (!parent_hub->dev.of_node)
7562306a36Sopenharmony_ci		return;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	if (!parent_hub->parent && !usb_hcd_is_primary_hcd(hcd))
7862306a36Sopenharmony_ci		return;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	for (i = 1; i <= parent_hub->maxchild; i++) {
8162306a36Sopenharmony_ci		np = usb_of_get_device_node(parent_hub, i);
8262306a36Sopenharmony_ci		if (!np)
8362306a36Sopenharmony_ci			continue;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci		if (!of_is_onboard_usb_hub(np))
8662306a36Sopenharmony_ci			goto node_put;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		npc = of_parse_phandle(np, "peer-hub", 0);
8962306a36Sopenharmony_ci		if (npc) {
9062306a36Sopenharmony_ci			if (!usb_hcd_is_primary_hcd(hcd)) {
9162306a36Sopenharmony_ci				of_node_put(npc);
9262306a36Sopenharmony_ci				goto node_put;
9362306a36Sopenharmony_ci			}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci			pdev = of_find_device_by_node(npc);
9662306a36Sopenharmony_ci			of_node_put(npc);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci			if (pdev) {
9962306a36Sopenharmony_ci				put_device(&pdev->dev);
10062306a36Sopenharmony_ci				goto node_put;
10162306a36Sopenharmony_ci			}
10262306a36Sopenharmony_ci		}
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci		pdev = of_platform_device_create(np, NULL, &parent_hub->dev);
10562306a36Sopenharmony_ci		if (!pdev) {
10662306a36Sopenharmony_ci			dev_err(&parent_hub->dev,
10762306a36Sopenharmony_ci				"failed to create platform device for onboard hub '%pOF'\n", np);
10862306a36Sopenharmony_ci			goto node_put;
10962306a36Sopenharmony_ci		}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci		pdle = kzalloc(sizeof(*pdle), GFP_KERNEL);
11262306a36Sopenharmony_ci		if (!pdle) {
11362306a36Sopenharmony_ci			of_platform_device_destroy(&pdev->dev, NULL);
11462306a36Sopenharmony_ci			goto node_put;
11562306a36Sopenharmony_ci		}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci		pdle->pdev = pdev;
11862306a36Sopenharmony_ci		list_add(&pdle->node, pdev_list);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cinode_put:
12162306a36Sopenharmony_ci		of_node_put(np);
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(onboard_hub_create_pdevs);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/**
12762306a36Sopenharmony_ci * onboard_hub_destroy_pdevs -- free resources of onboard hub platform devices
12862306a36Sopenharmony_ci * @pdev_list	: list of onboard hub platform devices
12962306a36Sopenharmony_ci *
13062306a36Sopenharmony_ci * Destroys the platform devices in the given list and frees the memory associated
13162306a36Sopenharmony_ci * with the list entry.
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_civoid onboard_hub_destroy_pdevs(struct list_head *pdev_list)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct pdev_list_entry *pdle, *tmp;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	list_for_each_entry_safe(pdle, tmp, pdev_list, node) {
13862306a36Sopenharmony_ci		list_del(&pdle->node);
13962306a36Sopenharmony_ci		of_platform_device_destroy(&pdle->pdev->dev, NULL);
14062306a36Sopenharmony_ci		kfree(pdle);
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(onboard_hub_destroy_pdevs);
144