Re: [RFC 2/2] usb: dwc3: Refactor PHY logic to support Multiport Controller

Pavan Kondeti
2022-05-23T07:46:37Z

On Thu, May 19, 2022 at 06:04:55PM +0530, Harsh Agarwal wrote:
> Currently the USB driver supports only single port controller
> which works with 2 PHYs at max ie HS and SS.
>
> But some devices have "multiport" controller where a single
> controller supports multiple ports and each port have their own
> PHYs. Refactor PHY logic to support the same.
>
> This implementation is compatible with existing glue drivers.
>
> Signed-off-by: Harsh Agarwal <quic_harshq@quicinc.com>

<snip>

> +static struct usb_phy *dwc3_core_get_phy_by_handle_with_node(struct device *dev,
> + const char *phandle, u8 index, struct device_node *lookup_node)
> +{
> + struct device_node *node;
> + struct usb_phy *phy;
> +
> + node = of_parse_phandle(lookup_node, phandle, index);
> + if (!node) {
> + dev_err(dev, "failed to get %s phandle in %pOF node\n", phandle,
> + dev->of_node);
> + return ERR_PTR(-ENODEV);
> + }
> + phy = devm_usb_get_phy_by_node(dev, node, NULL);
> + of_node_put(node);
> + return phy;
> +}
> +

we have devm_of_phy_get() API that takes both device and device_node (could be
other than device-of_node). This API you have to use for the generic PHY, so
we can have a similar wrapper with devm_of_usb_get_phy_by_phandle() which
takes device and device_node as arguments.

> +static int dwc3_extract_num_phys(struct dwc3 *dwc)
> +{
> + struct device_node *ports, *port;
> +
> + /* Find if any "multiport" child is present inside DWC3*/
> + for_each_available_child_of_node(dwc->dev->of_node, ports) {
> + if (!strcmp(ports->name, "multiport"))
> + break;
> + }
> + if (!ports) {
> + dwc->num_hsphy = 1;
> + dwc->num_ssphy = 1;
> + } else {
> + for_each_available_child_of_node(ports, port) {
> + dwc->num_hsphy += 1;
> + dwc->num_ssphy += 1;
> + }
> + }
> + dev_info(dwc->dev, "Num of HS and SS PHY are %u %u\n", dwc->num_hsphy, dwc->num_ssphy);
> +
> + dwc->usb2_phy = devm_kzalloc(dwc->dev,
> + sizeof(*dwc->usb2_phy) * dwc->num_hsphy, GFP_KERNEL);
> + if (!dwc->usb2_phy)
> + return -ENOMEM;
> +
> + dwc->usb3_phy = devm_kzalloc(dwc->dev,
> + sizeof(*dwc->usb3_phy) * dwc->num_ssphy, GFP_KERNEL);
> + if (!dwc->usb3_phy)
> + return -ENOMEM;
> +
> + return 0;
> +}
> +
> static int dwc3_core_get_phy(struct dwc3 *dwc)
> {
> struct device *dev = dwc->dev;
> struct device_node *node = dev->of_node;
> - int ret;
> + struct device_node *ports, *port;
>
> - if (node) {
> - dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
> - dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
> - } else {
> - dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
> - dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
> - }
> + int ret, i = 0;
>
> - if (IS_ERR(dwc->usb2_phy)) {
> - ret = PTR_ERR(dwc->usb2_phy);
> - if (ret == -ENXIO || ret == -ENODEV)
> - dwc->usb2_phy = NULL;
> - else
> - return dev_err_probe(dev, ret, "no usb2 phy configured\n");
> + ret = dwc3_extract_num_phys(dwc);
> + if (ret) {
> + dev_err(dwc->dev, "Unable to extract number of PHYs\n");
> + return ret;
> }
>
> - if (IS_ERR(dwc->usb3_phy)) {
> - ret = PTR_ERR(dwc->usb3_phy);
> - if (ret == -ENXIO || ret == -ENODEV)
> - dwc->usb3_phy = NULL;
> - else
> - return dev_err_probe(dev, ret, "no usb3 phy configured\n");
> + /* Find if any "multiport" child is present inside DWC3*/
> + for_each_available_child_of_node(node, ports) {
> + if (!strcmp(ports->name, "multiport"))
> + break;
> }
>
> - dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
> - if (IS_ERR(dwc->usb2_generic_phy)) {
> - ret = PTR_ERR(dwc->usb2_generic_phy);
> - if (ret == -ENOSYS || ret == -ENODEV)
> - dwc->usb2_generic_phy = NULL;
> - else
> - return dev_err_probe(dev, ret, "no usb2 phy configured\n");
> - }
> + if (!ports) {
> + if (node) {
> + dwc->usb2_phy[0] = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
> + dwc->usb3_phy[0] = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
> + } else {
> + dwc->usb2_phy[0] = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
> + dwc->usb3_phy[0] = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
> + }
>
> - dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
> - if (IS_ERR(dwc->usb3_generic_phy)) {
> - ret = PTR_ERR(dwc->usb3_generic_phy);
> - if (ret == -ENOSYS || ret == -ENODEV)
> - dwc->usb3_generic_phy = NULL;
> - else
> - return dev_err_probe(dev, ret, "no usb3 phy configured\n");
> + if (IS_ERR(dwc->usb2_phy[0])) {
> + ret = PTR_ERR(dwc->usb2_phy[0]);
> + if (ret == -ENXIO || ret == -ENODEV)
> + dwc->usb2_phy[0] = NULL;
> + else
> + return dev_err_probe(dev, ret, "no usb2 phy configured\n");
> + }
> +
> + if (IS_ERR(dwc->usb3_phy[0])) {
> + ret = PTR_ERR(dwc->usb3_phy[0]);
> + if (ret == -ENXIO || ret == -ENODEV)
> + dwc->usb3_phy[0] = NULL;
> + else
> + return dev_err_probe(dev, ret, "no usb3 phy configured\n");
> + }
> +
> + dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
> + if (IS_ERR(dwc->usb2_generic_phy)) {
> + ret = PTR_ERR(dwc->usb2_generic_phy);
> + if (ret == -ENOSYS || ret == -ENODEV)
> + dwc->usb2_generic_phy = NULL;
> + else
> + return dev_err_probe(dev, ret, "no usb2 phy configured\n");
> + }

Like I said above, devm_of_phy_get() is what needs to be used if we want to
re-use the same block of code that works for existing and multiport case.

Thanks,
Pavan