Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/**
  3 * Copyright (C) 2016 Linaro Ltd
  4 */
  5#include <linux/module.h>
  6#include <linux/ulpi/driver.h>
  7#include <linux/ulpi/regs.h>
  8#include <linux/clk.h>
  9#include <linux/regulator/consumer.h>
 10#include <linux/of_device.h>
 11#include <linux/phy/phy.h>
 12#include <linux/reset.h>
 13#include <linux/extcon.h>
 14#include <linux/notifier.h>
 15
 16#define ULPI_PWR_CLK_MNG_REG		0x88
 17# define ULPI_PWR_OTG_COMP_DISABLE	BIT(0)
 18
 19#define ULPI_MISC_A			0x96
 20# define ULPI_MISC_A_VBUSVLDEXTSEL	BIT(1)
 21# define ULPI_MISC_A_VBUSVLDEXT		BIT(0)
 22
 23
 24struct ulpi_seq {
 25	u8 addr;
 26	u8 val;
 27};
 28
 29struct qcom_usb_hs_phy {
 30	struct ulpi *ulpi;
 31	struct phy *phy;
 32	struct clk *ref_clk;
 33	struct clk *sleep_clk;
 34	struct regulator *v1p8;
 35	struct regulator *v3p3;
 36	struct reset_control *reset;
 37	struct ulpi_seq *init_seq;
 38	struct extcon_dev *vbus_edev;
 39	struct notifier_block vbus_notify;
 40};
 41
 42static int qcom_usb_hs_phy_set_mode(struct phy *phy,
 43				    enum phy_mode mode, int submode)
 44{
 45	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
 46	u8 addr;
 47	int ret;
 48
 49	if (!uphy->vbus_edev) {
 50		u8 val = 0;
 51
 52		switch (mode) {
 53		case PHY_MODE_USB_OTG:
 54		case PHY_MODE_USB_HOST:
 55			val |= ULPI_INT_IDGRD;
 56			fallthrough;
 57		case PHY_MODE_USB_DEVICE:
 58			val |= ULPI_INT_SESS_VALID;
 59		default:
 60			break;
 61		}
 62
 63		ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_RISE, val);
 64		if (ret)
 65			return ret;
 66		ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_FALL, val);
 67	} else {
 68		switch (mode) {
 69		case PHY_MODE_USB_OTG:
 70		case PHY_MODE_USB_DEVICE:
 71			addr = ULPI_SET(ULPI_MISC_A);
 72			break;
 73		case PHY_MODE_USB_HOST:
 74			addr = ULPI_CLR(ULPI_MISC_A);
 75			break;
 76		default:
 77			return -EINVAL;
 78		}
 79
 80		ret = ulpi_write(uphy->ulpi, ULPI_SET(ULPI_PWR_CLK_MNG_REG),
 81				 ULPI_PWR_OTG_COMP_DISABLE);
 82		if (ret)
 83			return ret;
 84		ret = ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXTSEL);
 85	}
 86
 87	return ret;
 88}
 89
 90static int
 91qcom_usb_hs_phy_vbus_notifier(struct notifier_block *nb, unsigned long event,
 92			      void *ptr)
 93{
 94	struct qcom_usb_hs_phy *uphy;
 95	u8 addr;
 96
 97	uphy = container_of(nb, struct qcom_usb_hs_phy, vbus_notify);
 98
 99	if (event)
100		addr = ULPI_SET(ULPI_MISC_A);
101	else
102		addr = ULPI_CLR(ULPI_MISC_A);
103
104	return ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXT);
105}
106
107static int qcom_usb_hs_phy_power_on(struct phy *phy)
108{
109	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
110	struct ulpi *ulpi = uphy->ulpi;
111	const struct ulpi_seq *seq;
112	int ret, state;
113
114	ret = clk_prepare_enable(uphy->ref_clk);
115	if (ret)
116		return ret;
117
118	ret = clk_prepare_enable(uphy->sleep_clk);
119	if (ret)
120		goto err_sleep;
121
122	ret = regulator_set_load(uphy->v1p8, 50000);
123	if (ret < 0)
124		goto err_1p8;
125
126	ret = regulator_enable(uphy->v1p8);
127	if (ret)
128		goto err_1p8;
129
130	ret = regulator_set_voltage_triplet(uphy->v3p3, 3050000, 3300000,
131					    3300000);
132	if (ret)
133		goto err_3p3;
134
135	ret = regulator_set_load(uphy->v3p3, 50000);
136	if (ret < 0)
137		goto err_3p3;
138
139	ret = regulator_enable(uphy->v3p3);
140	if (ret)
141		goto err_3p3;
142
143	for (seq = uphy->init_seq; seq->addr; seq++) {
144		ret = ulpi_write(ulpi, ULPI_EXT_VENDOR_SPECIFIC + seq->addr,
145				 seq->val);
146		if (ret)
147			goto err_ulpi;
148	}
149
150	if (uphy->reset) {
151		ret = reset_control_reset(uphy->reset);
152		if (ret)
153			goto err_ulpi;
154	}
155
156	if (uphy->vbus_edev) {
157		state = extcon_get_state(uphy->vbus_edev, EXTCON_USB);
158		/* setup initial state */
159		qcom_usb_hs_phy_vbus_notifier(&uphy->vbus_notify, state,
160					      uphy->vbus_edev);
161		ret = extcon_register_notifier(uphy->vbus_edev, EXTCON_USB,
162					       &uphy->vbus_notify);
163		if (ret)
164			goto err_ulpi;
165	}
166
167	return 0;
168err_ulpi:
169	regulator_disable(uphy->v3p3);
170err_3p3:
171	regulator_disable(uphy->v1p8);
172err_1p8:
173	clk_disable_unprepare(uphy->sleep_clk);
174err_sleep:
175	clk_disable_unprepare(uphy->ref_clk);
176	return ret;
177}
178
179static int qcom_usb_hs_phy_power_off(struct phy *phy)
180{
181	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
182
183	if (uphy->vbus_edev)
184		extcon_unregister_notifier(uphy->vbus_edev, EXTCON_USB,
185					   &uphy->vbus_notify);
186	regulator_disable(uphy->v3p3);
187	regulator_disable(uphy->v1p8);
188	clk_disable_unprepare(uphy->sleep_clk);
189	clk_disable_unprepare(uphy->ref_clk);
190
191	return 0;
192}
193
194static const struct phy_ops qcom_usb_hs_phy_ops = {
195	.power_on = qcom_usb_hs_phy_power_on,
196	.power_off = qcom_usb_hs_phy_power_off,
197	.set_mode = qcom_usb_hs_phy_set_mode,
198	.owner = THIS_MODULE,
199};
200
201static int qcom_usb_hs_phy_probe(struct ulpi *ulpi)
202{
203	struct qcom_usb_hs_phy *uphy;
204	struct phy_provider *p;
205	struct clk *clk;
206	struct regulator *reg;
207	struct reset_control *reset;
208	int size;
209	int ret;
210
211	uphy = devm_kzalloc(&ulpi->dev, sizeof(*uphy), GFP_KERNEL);
212	if (!uphy)
213		return -ENOMEM;
214	ulpi_set_drvdata(ulpi, uphy);
215	uphy->ulpi = ulpi;
216
217	size = of_property_count_u8_elems(ulpi->dev.of_node, "qcom,init-seq");
218	if (size < 0)
219		size = 0;
220	uphy->init_seq = devm_kmalloc_array(&ulpi->dev, (size / 2) + 1,
221					   sizeof(*uphy->init_seq), GFP_KERNEL);
222	if (!uphy->init_seq)
223		return -ENOMEM;
224	ret = of_property_read_u8_array(ulpi->dev.of_node, "qcom,init-seq",
225					(u8 *)uphy->init_seq, size);
226	if (ret && size)
227		return ret;
228	/* NUL terminate */
229	uphy->init_seq[size / 2].addr = uphy->init_seq[size / 2].val = 0;
230
231	uphy->ref_clk = clk = devm_clk_get(&ulpi->dev, "ref");
232	if (IS_ERR(clk))
233		return PTR_ERR(clk);
234
235	uphy->sleep_clk = clk = devm_clk_get(&ulpi->dev, "sleep");
236	if (IS_ERR(clk))
237		return PTR_ERR(clk);
238
239	uphy->v1p8 = reg = devm_regulator_get(&ulpi->dev, "v1p8");
240	if (IS_ERR(reg))
241		return PTR_ERR(reg);
242
243	uphy->v3p3 = reg = devm_regulator_get(&ulpi->dev, "v3p3");
244	if (IS_ERR(reg))
245		return PTR_ERR(reg);
246
247	uphy->reset = reset = devm_reset_control_get(&ulpi->dev, "por");
248	if (IS_ERR(reset)) {
249		if (PTR_ERR(reset) == -EPROBE_DEFER)
250			return PTR_ERR(reset);
251		uphy->reset = NULL;
252	}
253
254	uphy->phy = devm_phy_create(&ulpi->dev, ulpi->dev.of_node,
255				    &qcom_usb_hs_phy_ops);
256	if (IS_ERR(uphy->phy))
257		return PTR_ERR(uphy->phy);
258
259	uphy->vbus_edev = extcon_get_edev_by_phandle(&ulpi->dev, 0);
260	if (IS_ERR(uphy->vbus_edev)) {
261		if (PTR_ERR(uphy->vbus_edev) != -ENODEV)
262			return PTR_ERR(uphy->vbus_edev);
263		uphy->vbus_edev = NULL;
264	}
265
266	uphy->vbus_notify.notifier_call = qcom_usb_hs_phy_vbus_notifier;
267	phy_set_drvdata(uphy->phy, uphy);
268
269	p = devm_of_phy_provider_register(&ulpi->dev, of_phy_simple_xlate);
270	return PTR_ERR_OR_ZERO(p);
271}
272
273static const struct of_device_id qcom_usb_hs_phy_match[] = {
274	{ .compatible = "qcom,usb-hs-phy", },
275	{ }
276};
277MODULE_DEVICE_TABLE(of, qcom_usb_hs_phy_match);
278
279static struct ulpi_driver qcom_usb_hs_phy_driver = {
280	.probe = qcom_usb_hs_phy_probe,
281	.driver = {
282		.name = "qcom_usb_hs_phy",
283		.of_match_table = qcom_usb_hs_phy_match,
284	},
285};
286module_ulpi_driver(qcom_usb_hs_phy_driver);
287
288MODULE_DESCRIPTION("Qualcomm USB HS phy");
289MODULE_LICENSE("GPL v2");