xref: /openbmc/linux/drivers/usb/core/phy.c (revision 976e3645923bdd2fe7893aae33fd7a21098bfb28)
107dbff0dSMartin Blumenstingl // SPDX-License-Identifier: GPL-2.0+
207dbff0dSMartin Blumenstingl /*
307dbff0dSMartin Blumenstingl  * A wrapper for multiple PHYs which passes all phy_* function calls to
407dbff0dSMartin Blumenstingl  * multiple (actual) PHY devices. This is comes handy when initializing
507dbff0dSMartin Blumenstingl  * all PHYs on a HCD and to keep them all in the same state.
607dbff0dSMartin Blumenstingl  *
707dbff0dSMartin Blumenstingl  * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
807dbff0dSMartin Blumenstingl  */
907dbff0dSMartin Blumenstingl 
1007dbff0dSMartin Blumenstingl #include <linux/device.h>
1107dbff0dSMartin Blumenstingl #include <linux/list.h>
1207dbff0dSMartin Blumenstingl #include <linux/phy/phy.h>
1307dbff0dSMartin Blumenstingl #include <linux/of.h>
1407dbff0dSMartin Blumenstingl 
1507dbff0dSMartin Blumenstingl #include "phy.h"
1607dbff0dSMartin Blumenstingl 
1707dbff0dSMartin Blumenstingl struct usb_phy_roothub {
1807dbff0dSMartin Blumenstingl 	struct phy		*phy;
1907dbff0dSMartin Blumenstingl 	struct list_head	list;
2007dbff0dSMartin Blumenstingl };
2107dbff0dSMartin Blumenstingl 
usb_phy_roothub_add_phy(struct device * dev,int index,struct list_head * list)2207dbff0dSMartin Blumenstingl static int usb_phy_roothub_add_phy(struct device *dev, int index,
2307dbff0dSMartin Blumenstingl 				   struct list_head *list)
2407dbff0dSMartin Blumenstingl {
2507dbff0dSMartin Blumenstingl 	struct usb_phy_roothub *roothub_entry;
260a6ab90cSChunfeng Yun 	struct phy *phy;
2707dbff0dSMartin Blumenstingl 
280a6ab90cSChunfeng Yun 	phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
290a6ab90cSChunfeng Yun 	if (IS_ERR(phy)) {
300a6ab90cSChunfeng Yun 		if (PTR_ERR(phy) == -ENODEV)
3107dbff0dSMartin Blumenstingl 			return 0;
3207dbff0dSMartin Blumenstingl 		else
3307dbff0dSMartin Blumenstingl 			return PTR_ERR(phy);
3407dbff0dSMartin Blumenstingl 	}
3507dbff0dSMartin Blumenstingl 
3663cb03f5SMartin Blumenstingl 	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
3763cb03f5SMartin Blumenstingl 	if (!roothub_entry)
3863cb03f5SMartin Blumenstingl 		return -ENOMEM;
3963cb03f5SMartin Blumenstingl 
4063cb03f5SMartin Blumenstingl 	INIT_LIST_HEAD(&roothub_entry->list);
4107dbff0dSMartin Blumenstingl 
4207dbff0dSMartin Blumenstingl 	roothub_entry->phy = phy;
4307dbff0dSMartin Blumenstingl 
4407dbff0dSMartin Blumenstingl 	list_add_tail(&roothub_entry->list, list);
4507dbff0dSMartin Blumenstingl 
4607dbff0dSMartin Blumenstingl 	return 0;
4707dbff0dSMartin Blumenstingl }
4807dbff0dSMartin Blumenstingl 
usb_phy_roothub_alloc(struct device * dev)4963cb03f5SMartin Blumenstingl struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
5007dbff0dSMartin Blumenstingl {
5107dbff0dSMartin Blumenstingl 	struct usb_phy_roothub *phy_roothub;
5207dbff0dSMartin Blumenstingl 	int i, num_phys, err;
5307dbff0dSMartin Blumenstingl 
54fec94445SMartin Blumenstingl 	if (!IS_ENABLED(CONFIG_GENERIC_PHY))
55fec94445SMartin Blumenstingl 		return NULL;
56fec94445SMartin Blumenstingl 
5707dbff0dSMartin Blumenstingl 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
5807dbff0dSMartin Blumenstingl 					      "#phy-cells");
5907dbff0dSMartin Blumenstingl 	if (num_phys <= 0)
6007dbff0dSMartin Blumenstingl 		return NULL;
6107dbff0dSMartin Blumenstingl 
6263cb03f5SMartin Blumenstingl 	phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
6363cb03f5SMartin Blumenstingl 	if (!phy_roothub)
6463cb03f5SMartin Blumenstingl 		return ERR_PTR(-ENOMEM);
6563cb03f5SMartin Blumenstingl 
6663cb03f5SMartin Blumenstingl 	INIT_LIST_HEAD(&phy_roothub->list);
6707dbff0dSMartin Blumenstingl 
6807dbff0dSMartin Blumenstingl 	for (i = 0; i < num_phys; i++) {
6907dbff0dSMartin Blumenstingl 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
7007dbff0dSMartin Blumenstingl 		if (err)
7163cb03f5SMartin Blumenstingl 			return ERR_PTR(err);
7207dbff0dSMartin Blumenstingl 	}
7307dbff0dSMartin Blumenstingl 
7463cb03f5SMartin Blumenstingl 	return phy_roothub;
7563cb03f5SMartin Blumenstingl }
7663cb03f5SMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
7763cb03f5SMartin Blumenstingl 
usb_phy_roothub_init(struct usb_phy_roothub * phy_roothub)7863cb03f5SMartin Blumenstingl int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
7963cb03f5SMartin Blumenstingl {
8063cb03f5SMartin Blumenstingl 	struct usb_phy_roothub *roothub_entry;
8163cb03f5SMartin Blumenstingl 	struct list_head *head;
8263cb03f5SMartin Blumenstingl 	int err;
8363cb03f5SMartin Blumenstingl 
8463cb03f5SMartin Blumenstingl 	if (!phy_roothub)
8563cb03f5SMartin Blumenstingl 		return 0;
8663cb03f5SMartin Blumenstingl 
8707dbff0dSMartin Blumenstingl 	head = &phy_roothub->list;
8807dbff0dSMartin Blumenstingl 
8907dbff0dSMartin Blumenstingl 	list_for_each_entry(roothub_entry, head, list) {
9007dbff0dSMartin Blumenstingl 		err = phy_init(roothub_entry->phy);
9107dbff0dSMartin Blumenstingl 		if (err)
9207dbff0dSMartin Blumenstingl 			goto err_exit_phys;
9307dbff0dSMartin Blumenstingl 	}
9407dbff0dSMartin Blumenstingl 
9563cb03f5SMartin Blumenstingl 	return 0;
9607dbff0dSMartin Blumenstingl 
9707dbff0dSMartin Blumenstingl err_exit_phys:
9807dbff0dSMartin Blumenstingl 	list_for_each_entry_continue_reverse(roothub_entry, head, list)
9907dbff0dSMartin Blumenstingl 		phy_exit(roothub_entry->phy);
10007dbff0dSMartin Blumenstingl 
10163cb03f5SMartin Blumenstingl 	return err;
10207dbff0dSMartin Blumenstingl }
10307dbff0dSMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
10407dbff0dSMartin Blumenstingl 
usb_phy_roothub_exit(struct usb_phy_roothub * phy_roothub)10507dbff0dSMartin Blumenstingl int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
10607dbff0dSMartin Blumenstingl {
10707dbff0dSMartin Blumenstingl 	struct usb_phy_roothub *roothub_entry;
10807dbff0dSMartin Blumenstingl 	struct list_head *head;
10907dbff0dSMartin Blumenstingl 	int err, ret = 0;
11007dbff0dSMartin Blumenstingl 
11107dbff0dSMartin Blumenstingl 	if (!phy_roothub)
11207dbff0dSMartin Blumenstingl 		return 0;
11307dbff0dSMartin Blumenstingl 
11407dbff0dSMartin Blumenstingl 	head = &phy_roothub->list;
11507dbff0dSMartin Blumenstingl 
11607dbff0dSMartin Blumenstingl 	list_for_each_entry(roothub_entry, head, list) {
11707dbff0dSMartin Blumenstingl 		err = phy_exit(roothub_entry->phy);
11807dbff0dSMartin Blumenstingl 		if (err)
119dd40438fSMartin Blumenstingl 			ret = err;
12007dbff0dSMartin Blumenstingl 	}
12107dbff0dSMartin Blumenstingl 
12207dbff0dSMartin Blumenstingl 	return ret;
12307dbff0dSMartin Blumenstingl }
12407dbff0dSMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
12507dbff0dSMartin Blumenstingl 
usb_phy_roothub_set_mode(struct usb_phy_roothub * phy_roothub,enum phy_mode mode)126b97a3134SMiquel Raynal int usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub,
127b97a3134SMiquel Raynal 			     enum phy_mode mode)
128b97a3134SMiquel Raynal {
129b97a3134SMiquel Raynal 	struct usb_phy_roothub *roothub_entry;
130b97a3134SMiquel Raynal 	struct list_head *head;
131b97a3134SMiquel Raynal 	int err;
132b97a3134SMiquel Raynal 
133b97a3134SMiquel Raynal 	if (!phy_roothub)
134b97a3134SMiquel Raynal 		return 0;
135b97a3134SMiquel Raynal 
136b97a3134SMiquel Raynal 	head = &phy_roothub->list;
137b97a3134SMiquel Raynal 
138b97a3134SMiquel Raynal 	list_for_each_entry(roothub_entry, head, list) {
139b97a3134SMiquel Raynal 		err = phy_set_mode(roothub_entry->phy, mode);
140b97a3134SMiquel Raynal 		if (err)
141b97a3134SMiquel Raynal 			goto err_out;
142b97a3134SMiquel Raynal 	}
143b97a3134SMiquel Raynal 
144b97a3134SMiquel Raynal 	return 0;
145b97a3134SMiquel Raynal 
146b97a3134SMiquel Raynal err_out:
147b97a3134SMiquel Raynal 	list_for_each_entry_continue_reverse(roothub_entry, head, list)
148b97a3134SMiquel Raynal 		phy_power_off(roothub_entry->phy);
149b97a3134SMiquel Raynal 
150b97a3134SMiquel Raynal 	return err;
151b97a3134SMiquel Raynal }
152b97a3134SMiquel Raynal EXPORT_SYMBOL_GPL(usb_phy_roothub_set_mode);
153b97a3134SMiquel Raynal 
usb_phy_roothub_calibrate(struct usb_phy_roothub * phy_roothub)154*34c7ed72SMarek Szyprowski int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub)
155*34c7ed72SMarek Szyprowski {
156*34c7ed72SMarek Szyprowski 	struct usb_phy_roothub *roothub_entry;
157*34c7ed72SMarek Szyprowski 	struct list_head *head;
158*34c7ed72SMarek Szyprowski 	int err;
159*34c7ed72SMarek Szyprowski 
160*34c7ed72SMarek Szyprowski 	if (!phy_roothub)
161*34c7ed72SMarek Szyprowski 		return 0;
162*34c7ed72SMarek Szyprowski 
163*34c7ed72SMarek Szyprowski 	head = &phy_roothub->list;
164*34c7ed72SMarek Szyprowski 
165*34c7ed72SMarek Szyprowski 	list_for_each_entry(roothub_entry, head, list) {
166*34c7ed72SMarek Szyprowski 		err = phy_calibrate(roothub_entry->phy);
167*34c7ed72SMarek Szyprowski 		if (err)
168*34c7ed72SMarek Szyprowski 			return err;
169*34c7ed72SMarek Szyprowski 	}
170*34c7ed72SMarek Szyprowski 
171*34c7ed72SMarek Szyprowski 	return 0;
172*34c7ed72SMarek Szyprowski }
173*34c7ed72SMarek Szyprowski EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate);
174*34c7ed72SMarek Szyprowski 
usb_phy_roothub_power_on(struct usb_phy_roothub * phy_roothub)17507dbff0dSMartin Blumenstingl int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
17607dbff0dSMartin Blumenstingl {
17707dbff0dSMartin Blumenstingl 	struct usb_phy_roothub *roothub_entry;
17807dbff0dSMartin Blumenstingl 	struct list_head *head;
17907dbff0dSMartin Blumenstingl 	int err;
18007dbff0dSMartin Blumenstingl 
18107dbff0dSMartin Blumenstingl 	if (!phy_roothub)
18207dbff0dSMartin Blumenstingl 		return 0;
18307dbff0dSMartin Blumenstingl 
18407dbff0dSMartin Blumenstingl 	head = &phy_roothub->list;
18507dbff0dSMartin Blumenstingl 
18607dbff0dSMartin Blumenstingl 	list_for_each_entry(roothub_entry, head, list) {
18707dbff0dSMartin Blumenstingl 		err = phy_power_on(roothub_entry->phy);
18807dbff0dSMartin Blumenstingl 		if (err)
18907dbff0dSMartin Blumenstingl 			goto err_out;
19007dbff0dSMartin Blumenstingl 	}
19107dbff0dSMartin Blumenstingl 
19207dbff0dSMartin Blumenstingl 	return 0;
19307dbff0dSMartin Blumenstingl 
19407dbff0dSMartin Blumenstingl err_out:
19507dbff0dSMartin Blumenstingl 	list_for_each_entry_continue_reverse(roothub_entry, head, list)
19607dbff0dSMartin Blumenstingl 		phy_power_off(roothub_entry->phy);
19707dbff0dSMartin Blumenstingl 
19807dbff0dSMartin Blumenstingl 	return err;
19907dbff0dSMartin Blumenstingl }
20007dbff0dSMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
20107dbff0dSMartin Blumenstingl 
usb_phy_roothub_power_off(struct usb_phy_roothub * phy_roothub)20207dbff0dSMartin Blumenstingl void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
20307dbff0dSMartin Blumenstingl {
20407dbff0dSMartin Blumenstingl 	struct usb_phy_roothub *roothub_entry;
20507dbff0dSMartin Blumenstingl 
20607dbff0dSMartin Blumenstingl 	if (!phy_roothub)
20707dbff0dSMartin Blumenstingl 		return;
20807dbff0dSMartin Blumenstingl 
20907dbff0dSMartin Blumenstingl 	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
21007dbff0dSMartin Blumenstingl 		phy_power_off(roothub_entry->phy);
21107dbff0dSMartin Blumenstingl }
21207dbff0dSMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
213f0e36d47SMartin Blumenstingl 
usb_phy_roothub_suspend(struct device * controller_dev,struct usb_phy_roothub * phy_roothub)214f0e36d47SMartin Blumenstingl int usb_phy_roothub_suspend(struct device *controller_dev,
215f0e36d47SMartin Blumenstingl 			    struct usb_phy_roothub *phy_roothub)
216f0e36d47SMartin Blumenstingl {
217f0e36d47SMartin Blumenstingl 	usb_phy_roothub_power_off(phy_roothub);
218f0e36d47SMartin Blumenstingl 
219f0e36d47SMartin Blumenstingl 	/* keep the PHYs initialized so the device can wake up the system */
220f0e36d47SMartin Blumenstingl 	if (device_may_wakeup(controller_dev))
221f0e36d47SMartin Blumenstingl 		return 0;
222f0e36d47SMartin Blumenstingl 
223f0e36d47SMartin Blumenstingl 	return usb_phy_roothub_exit(phy_roothub);
224f0e36d47SMartin Blumenstingl }
225f0e36d47SMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
226f0e36d47SMartin Blumenstingl 
usb_phy_roothub_resume(struct device * controller_dev,struct usb_phy_roothub * phy_roothub)227f0e36d47SMartin Blumenstingl int usb_phy_roothub_resume(struct device *controller_dev,
228f0e36d47SMartin Blumenstingl 			   struct usb_phy_roothub *phy_roothub)
229f0e36d47SMartin Blumenstingl {
230f0e36d47SMartin Blumenstingl 	int err;
231f0e36d47SMartin Blumenstingl 
232f0e36d47SMartin Blumenstingl 	/* if the device can't wake up the system _exit was called */
233f0e36d47SMartin Blumenstingl 	if (!device_may_wakeup(controller_dev)) {
234f0e36d47SMartin Blumenstingl 		err = usb_phy_roothub_init(phy_roothub);
235f0e36d47SMartin Blumenstingl 		if (err)
236f0e36d47SMartin Blumenstingl 			return err;
237f0e36d47SMartin Blumenstingl 	}
238f0e36d47SMartin Blumenstingl 
239f0e36d47SMartin Blumenstingl 	err = usb_phy_roothub_power_on(phy_roothub);
240f0e36d47SMartin Blumenstingl 
241f0e36d47SMartin Blumenstingl 	/* undo _init if _power_on failed */
242f0e36d47SMartin Blumenstingl 	if (err && !device_may_wakeup(controller_dev))
243f0e36d47SMartin Blumenstingl 		usb_phy_roothub_exit(phy_roothub);
244f0e36d47SMartin Blumenstingl 
245f0e36d47SMartin Blumenstingl 	return err;
246f0e36d47SMartin Blumenstingl }
247f0e36d47SMartin Blumenstingl EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);
248