1 /**
2  * Copyright (C) 2016 Linaro Ltd
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/module.h>
9 #include <linux/ulpi/driver.h>
10 #include <linux/ulpi/regs.h>
11 #include <linux/clk.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/of_device.h>
14 #include <linux/phy/phy.h>
15 #include <linux/reset.h>
16 #include <linux/extcon.h>
17 #include <linux/notifier.h>
18 
19 #define ULPI_PWR_CLK_MNG_REG		0x88
20 # define ULPI_PWR_OTG_COMP_DISABLE	BIT(0)
21 
22 #define ULPI_MISC_A			0x96
23 # define ULPI_MISC_A_VBUSVLDEXTSEL	BIT(1)
24 # define ULPI_MISC_A_VBUSVLDEXT		BIT(0)
25 
26 
27 struct ulpi_seq {
28 	u8 addr;
29 	u8 val;
30 };
31 
32 struct qcom_usb_hs_phy {
33 	struct ulpi *ulpi;
34 	struct phy *phy;
35 	struct clk *ref_clk;
36 	struct clk *sleep_clk;
37 	struct regulator *v1p8;
38 	struct regulator *v3p3;
39 	struct reset_control *reset;
40 	struct ulpi_seq *init_seq;
41 	struct extcon_dev *vbus_edev;
42 	struct notifier_block vbus_notify;
43 };
44 
45 static int qcom_usb_hs_phy_set_mode(struct phy *phy,
46 				    enum phy_mode mode, int submode)
47 {
48 	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
49 	u8 addr;
50 	int ret;
51 
52 	if (!uphy->vbus_edev) {
53 		u8 val = 0;
54 
55 		switch (mode) {
56 		case PHY_MODE_USB_OTG:
57 		case PHY_MODE_USB_HOST:
58 			val |= ULPI_INT_IDGRD;
59 			/* fall through */
60 		case PHY_MODE_USB_DEVICE:
61 			val |= ULPI_INT_SESS_VALID;
62 		default:
63 			break;
64 		}
65 
66 		ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_RISE, val);
67 		if (ret)
68 			return ret;
69 		ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_FALL, val);
70 	} else {
71 		switch (mode) {
72 		case PHY_MODE_USB_OTG:
73 		case PHY_MODE_USB_DEVICE:
74 			addr = ULPI_SET(ULPI_MISC_A);
75 			break;
76 		case PHY_MODE_USB_HOST:
77 			addr = ULPI_CLR(ULPI_MISC_A);
78 			break;
79 		default:
80 			return -EINVAL;
81 		}
82 
83 		ret = ulpi_write(uphy->ulpi, ULPI_SET(ULPI_PWR_CLK_MNG_REG),
84 				 ULPI_PWR_OTG_COMP_DISABLE);
85 		if (ret)
86 			return ret;
87 		ret = ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXTSEL);
88 	}
89 
90 	return ret;
91 }
92 
93 static int
94 qcom_usb_hs_phy_vbus_notifier(struct notifier_block *nb, unsigned long event,
95 			      void *ptr)
96 {
97 	struct qcom_usb_hs_phy *uphy;
98 	u8 addr;
99 
100 	uphy = container_of(nb, struct qcom_usb_hs_phy, vbus_notify);
101 
102 	if (event)
103 		addr = ULPI_SET(ULPI_MISC_A);
104 	else
105 		addr = ULPI_CLR(ULPI_MISC_A);
106 
107 	return ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXT);
108 }
109 
110 static int qcom_usb_hs_phy_power_on(struct phy *phy)
111 {
112 	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
113 	struct ulpi *ulpi = uphy->ulpi;
114 	const struct ulpi_seq *seq;
115 	int ret, state;
116 
117 	ret = clk_prepare_enable(uphy->ref_clk);
118 	if (ret)
119 		return ret;
120 
121 	ret = clk_prepare_enable(uphy->sleep_clk);
122 	if (ret)
123 		goto err_sleep;
124 
125 	ret = regulator_set_load(uphy->v1p8, 50000);
126 	if (ret < 0)
127 		goto err_1p8;
128 
129 	ret = regulator_enable(uphy->v1p8);
130 	if (ret)
131 		goto err_1p8;
132 
133 	ret = regulator_set_voltage_triplet(uphy->v3p3, 3050000, 3300000,
134 					    3300000);
135 	if (ret)
136 		goto err_3p3;
137 
138 	ret = regulator_set_load(uphy->v3p3, 50000);
139 	if (ret < 0)
140 		goto err_3p3;
141 
142 	ret = regulator_enable(uphy->v3p3);
143 	if (ret)
144 		goto err_3p3;
145 
146 	for (seq = uphy->init_seq; seq->addr; seq++) {
147 		ret = ulpi_write(ulpi, ULPI_EXT_VENDOR_SPECIFIC + seq->addr,
148 				 seq->val);
149 		if (ret)
150 			goto err_ulpi;
151 	}
152 
153 	if (uphy->reset) {
154 		ret = reset_control_reset(uphy->reset);
155 		if (ret)
156 			goto err_ulpi;
157 	}
158 
159 	if (uphy->vbus_edev) {
160 		state = extcon_get_state(uphy->vbus_edev, EXTCON_USB);
161 		/* setup initial state */
162 		qcom_usb_hs_phy_vbus_notifier(&uphy->vbus_notify, state,
163 					      uphy->vbus_edev);
164 		ret = devm_extcon_register_notifier(&ulpi->dev, uphy->vbus_edev,
165 				EXTCON_USB, &uphy->vbus_notify);
166 		if (ret)
167 			goto err_ulpi;
168 	}
169 
170 	return 0;
171 err_ulpi:
172 	regulator_disable(uphy->v3p3);
173 err_3p3:
174 	regulator_disable(uphy->v1p8);
175 err_1p8:
176 	clk_disable_unprepare(uphy->sleep_clk);
177 err_sleep:
178 	clk_disable_unprepare(uphy->ref_clk);
179 	return ret;
180 }
181 
182 static int qcom_usb_hs_phy_power_off(struct phy *phy)
183 {
184 	struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
185 
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 
194 static 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 
201 static 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 
273 static const struct of_device_id qcom_usb_hs_phy_match[] = {
274 	{ .compatible = "qcom,usb-hs-phy", },
275 	{ }
276 };
277 MODULE_DEVICE_TABLE(of, qcom_usb_hs_phy_match);
278 
279 static 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 };
286 module_ulpi_driver(qcom_usb_hs_phy_driver);
287 
288 MODULE_DESCRIPTION("Qualcomm USB HS phy");
289 MODULE_LICENSE("GPL v2");
290