Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Copyright (C) 2016 MediaTek Inc.
  3 *
  4 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  5 *
  6 * This software is licensed under the terms of the GNU General Public
  7 * License version 2, as published by the Free Software Foundation, and
  8 * may be copied, distributed, and modified under those terms.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 */
 16
 17#include <linux/clk.h>
 18#include <linux/dma-mapping.h>
 19#include <linux/iopoll.h>
 20#include <linux/kernel.h>
 21#include <linux/module.h>
 22#include <linux/of_address.h>
 23#include <linux/of_irq.h>
 24#include <linux/pinctrl/consumer.h>
 25#include <linux/platform_device.h>
 26
 27#include "mtu3.h"
 28#include "mtu3_dr.h"
 
 29
 30/* u2-port0 should be powered on and enabled; */
 31int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
 32{
 33	void __iomem *ibase = ssusb->ippc_base;
 34	u32 value, check_val;
 35	int ret;
 36
 37	check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
 38			SSUSB_REF_RST_B_STS;
 39
 40	ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
 41			(check_val == (value & check_val)), 100, 20000);
 42	if (ret) {
 43		dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
 44		return ret;
 45	}
 46
 47	ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
 48			(value & SSUSB_U2_MAC_SYS_RST_B_STS), 100, 10000);
 49	if (ret) {
 50		dev_err(ssusb->dev, "mac2 clock is not stable\n");
 51		return ret;
 52	}
 53
 54	return 0;
 55}
 56
 57static int ssusb_phy_init(struct ssusb_mtk *ssusb)
 58{
 59	int i;
 60	int ret;
 61
 62	for (i = 0; i < ssusb->num_phys; i++) {
 63		ret = phy_init(ssusb->phys[i]);
 64		if (ret)
 65			goto exit_phy;
 66	}
 67	return 0;
 68
 69exit_phy:
 70	for (; i > 0; i--)
 71		phy_exit(ssusb->phys[i - 1]);
 72
 73	return ret;
 74}
 75
 76static int ssusb_phy_exit(struct ssusb_mtk *ssusb)
 77{
 78	int i;
 79
 80	for (i = 0; i < ssusb->num_phys; i++)
 81		phy_exit(ssusb->phys[i]);
 82
 83	return 0;
 84}
 85
 86static int ssusb_phy_power_on(struct ssusb_mtk *ssusb)
 87{
 88	int i;
 89	int ret;
 90
 91	for (i = 0; i < ssusb->num_phys; i++) {
 92		ret = phy_power_on(ssusb->phys[i]);
 93		if (ret)
 94			goto power_off_phy;
 95	}
 96	return 0;
 97
 98power_off_phy:
 99	for (; i > 0; i--)
100		phy_power_off(ssusb->phys[i - 1]);
101
102	return ret;
103}
104
105static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
106{
107	unsigned int i;
108
109	for (i = 0; i < ssusb->num_phys; i++)
110		phy_power_off(ssusb->phys[i]);
111}
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
114{
115	int ret = 0;
116
117	ret = regulator_enable(ssusb->vusb33);
118	if (ret) {
119		dev_err(ssusb->dev, "failed to enable vusb33\n");
120		goto vusb33_err;
121	}
122
123	ret = clk_prepare_enable(ssusb->sys_clk);
124	if (ret) {
125		dev_err(ssusb->dev, "failed to enable sys_clk\n");
126		goto clk_err;
127	}
128
129	ret = ssusb_phy_init(ssusb);
130	if (ret) {
131		dev_err(ssusb->dev, "failed to init phy\n");
132		goto phy_init_err;
133	}
134
135	ret = ssusb_phy_power_on(ssusb);
136	if (ret) {
137		dev_err(ssusb->dev, "failed to power on phy\n");
138		goto phy_err;
139	}
140
141	return 0;
142
143phy_err:
144	ssusb_phy_exit(ssusb);
145phy_init_err:
146	clk_disable_unprepare(ssusb->sys_clk);
147clk_err:
148	regulator_disable(ssusb->vusb33);
149vusb33_err:
150
151	return ret;
152}
153
154static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
155{
156	clk_disable_unprepare(ssusb->sys_clk);
157	regulator_disable(ssusb->vusb33);
158	ssusb_phy_power_off(ssusb);
159	ssusb_phy_exit(ssusb);
160}
161
162static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
163{
164	/* reset whole ip (xhci & u3d) */
165	mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
166	udelay(1);
167	mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
168}
169
170static int get_iddig_pinctrl(struct ssusb_mtk *ssusb)
171{
172	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
173
174	otg_sx->id_pinctrl = devm_pinctrl_get(ssusb->dev);
175	if (IS_ERR(otg_sx->id_pinctrl)) {
176		dev_err(ssusb->dev, "Cannot find id pinctrl!\n");
177		return PTR_ERR(otg_sx->id_pinctrl);
178	}
179
180	otg_sx->id_float =
181		pinctrl_lookup_state(otg_sx->id_pinctrl, "id_float");
182	if (IS_ERR(otg_sx->id_float)) {
183		dev_err(ssusb->dev, "Cannot find pinctrl id_float!\n");
184		return PTR_ERR(otg_sx->id_float);
185	}
186
187	otg_sx->id_ground =
188		pinctrl_lookup_state(otg_sx->id_pinctrl, "id_ground");
189	if (IS_ERR(otg_sx->id_ground)) {
190		dev_err(ssusb->dev, "Cannot find pinctrl id_ground!\n");
191		return PTR_ERR(otg_sx->id_ground);
192	}
193
194	return 0;
195}
196
197static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
198{
199	struct device_node *node = pdev->dev.of_node;
200	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
201	struct device *dev = &pdev->dev;
202	struct regulator *vbus;
203	struct resource *res;
204	int i;
205	int ret;
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207	ssusb->num_phys = of_count_phandle_with_args(node,
208			"phys", "#phy-cells");
209	if (ssusb->num_phys > 0) {
210		ssusb->phys = devm_kcalloc(dev, ssusb->num_phys,
211					sizeof(*ssusb->phys), GFP_KERNEL);
212		if (!ssusb->phys)
213			return -ENOMEM;
214	} else {
215		ssusb->num_phys = 0;
216	}
217
218	for (i = 0; i < ssusb->num_phys; i++) {
219		ssusb->phys[i] = devm_of_phy_get_by_index(dev, node, i);
220		if (IS_ERR(ssusb->phys[i])) {
221			dev_err(dev, "failed to get phy-%d\n", i);
222			return PTR_ERR(ssusb->phys[i]);
223		}
224	}
225
226	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
227	ssusb->ippc_base = devm_ioremap_resource(dev, res);
228	if (IS_ERR(ssusb->ippc_base)) {
229		dev_err(dev, "failed to map memory for ippc\n");
230		return PTR_ERR(ssusb->ippc_base);
231	}
232
233	ssusb->vusb33 = devm_regulator_get(&pdev->dev, "vusb33");
234	if (IS_ERR(ssusb->vusb33)) {
235		dev_err(dev, "failed to get vusb33\n");
236		return PTR_ERR(ssusb->vusb33);
237	}
238
239	ssusb->sys_clk = devm_clk_get(dev, "sys_ck");
240	if (IS_ERR(ssusb->sys_clk)) {
241		dev_err(dev, "failed to get sys clock\n");
242		return PTR_ERR(ssusb->sys_clk);
243	}
244
245	ssusb->dr_mode = usb_get_dr_mode(dev);
246	if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN) {
247		dev_err(dev, "dr_mode is error\n");
248		return -EINVAL;
249	}
250
251	if (ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
252		return 0;
253
254	/* if host role is supported */
255	ret = ssusb_wakeup_of_property_parse(ssusb, node);
256	if (ret)
 
257		return ret;
 
258
259	if (ssusb->dr_mode != USB_DR_MODE_OTG)
260		return 0;
 
261
262	/* if dual-role mode is supported */
263	vbus = devm_regulator_get(&pdev->dev, "vbus");
264	if (IS_ERR(vbus)) {
265		dev_err(dev, "failed to get vbus\n");
266		return PTR_ERR(vbus);
267	}
268	otg_sx->vbus = vbus;
269
 
 
 
 
270	otg_sx->is_u3_drd = of_property_read_bool(node, "mediatek,usb3-drd");
271	otg_sx->manual_drd_enabled =
272		of_property_read_bool(node, "enable-manual-drd");
 
273
274	if (of_property_read_bool(node, "extcon")) {
275		otg_sx->edev = extcon_get_edev_by_phandle(ssusb->dev, 0);
276		if (IS_ERR(otg_sx->edev)) {
277			dev_err(ssusb->dev, "couldn't get extcon device\n");
278			return -EPROBE_DEFER;
279		}
280		if (otg_sx->manual_drd_enabled) {
281			ret = get_iddig_pinctrl(ssusb);
282			if (ret)
283				return ret;
284		}
285	}
286
287	dev_info(dev, "dr_mode: %d, is_u3_dr: %d\n",
288		ssusb->dr_mode, otg_sx->is_u3_drd);
 
 
289
290	return 0;
291}
292
293static int mtu3_probe(struct platform_device *pdev)
294{
295	struct device_node *node = pdev->dev.of_node;
296	struct device *dev = &pdev->dev;
297	struct ssusb_mtk *ssusb;
298	int ret = -ENOMEM;
299
300	/* all elements are set to ZERO as default value */
301	ssusb = devm_kzalloc(dev, sizeof(*ssusb), GFP_KERNEL);
302	if (!ssusb)
303		return -ENOMEM;
304
305	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
306	if (ret) {
307		dev_err(dev, "No suitable DMA config available\n");
308		return -ENOTSUPP;
309	}
310
311	platform_set_drvdata(pdev, ssusb);
312	ssusb->dev = dev;
313
314	ret = get_ssusb_rscs(pdev, ssusb);
315	if (ret)
316		return ret;
317
 
 
318	/* enable power domain */
319	pm_runtime_enable(dev);
320	pm_runtime_get_sync(dev);
321	device_enable_async_suspend(dev);
322
323	ret = ssusb_rscs_init(ssusb);
324	if (ret)
325		goto comm_init_err;
326
327	ssusb_ip_sw_reset(ssusb);
328
329	if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
330		ssusb->dr_mode = USB_DR_MODE_HOST;
331	else if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
332		ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
333
334	/* default as host */
335	ssusb->is_host = !(ssusb->dr_mode == USB_DR_MODE_PERIPHERAL);
336
337	switch (ssusb->dr_mode) {
338	case USB_DR_MODE_PERIPHERAL:
339		ret = ssusb_gadget_init(ssusb);
340		if (ret) {
341			dev_err(dev, "failed to initialize gadget\n");
342			goto comm_exit;
343		}
344		break;
345	case USB_DR_MODE_HOST:
346		ret = ssusb_host_init(ssusb, node);
347		if (ret) {
348			dev_err(dev, "failed to initialize host\n");
349			goto comm_exit;
350		}
351		break;
352	case USB_DR_MODE_OTG:
353		ret = ssusb_gadget_init(ssusb);
354		if (ret) {
355			dev_err(dev, "failed to initialize gadget\n");
356			goto comm_exit;
357		}
358
359		ret = ssusb_host_init(ssusb, node);
360		if (ret) {
361			dev_err(dev, "failed to initialize host\n");
362			goto gadget_exit;
363		}
364
365		ssusb_otg_switch_init(ssusb);
 
 
 
 
366		break;
367	default:
368		dev_err(dev, "unsupported mode: %d\n", ssusb->dr_mode);
369		ret = -EINVAL;
370		goto comm_exit;
371	}
372
373	return 0;
374
 
 
375gadget_exit:
376	ssusb_gadget_exit(ssusb);
377comm_exit:
378	ssusb_rscs_exit(ssusb);
379comm_init_err:
380	pm_runtime_put_sync(dev);
381	pm_runtime_disable(dev);
 
382
383	return ret;
384}
385
386static int mtu3_remove(struct platform_device *pdev)
387{
388	struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
389
390	switch (ssusb->dr_mode) {
391	case USB_DR_MODE_PERIPHERAL:
392		ssusb_gadget_exit(ssusb);
393		break;
394	case USB_DR_MODE_HOST:
395		ssusb_host_exit(ssusb);
396		break;
397	case USB_DR_MODE_OTG:
398		ssusb_otg_switch_exit(ssusb);
399		ssusb_gadget_exit(ssusb);
400		ssusb_host_exit(ssusb);
401		break;
402	default:
403		return -EINVAL;
404	}
405
406	ssusb_rscs_exit(ssusb);
407	pm_runtime_put_sync(&pdev->dev);
408	pm_runtime_disable(&pdev->dev);
 
409
410	return 0;
411}
412
413/*
414 * when support dual-role mode, we reject suspend when
415 * it works as device mode;
416 */
417static int __maybe_unused mtu3_suspend(struct device *dev)
418{
419	struct platform_device *pdev = to_platform_device(dev);
420	struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
421
422	dev_dbg(dev, "%s\n", __func__);
423
424	/* REVISIT: disconnect it for only device mode? */
425	if (!ssusb->is_host)
426		return 0;
427
428	ssusb_host_disable(ssusb, true);
429	ssusb_phy_power_off(ssusb);
430	clk_disable_unprepare(ssusb->sys_clk);
431	ssusb_wakeup_enable(ssusb);
432
433	return 0;
434}
435
436static int __maybe_unused mtu3_resume(struct device *dev)
437{
438	struct platform_device *pdev = to_platform_device(dev);
439	struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
440
441	dev_dbg(dev, "%s\n", __func__);
442
443	if (!ssusb->is_host)
444		return 0;
445
446	ssusb_wakeup_disable(ssusb);
447	clk_prepare_enable(ssusb->sys_clk);
448	ssusb_phy_power_on(ssusb);
 
 
 
 
 
 
449	ssusb_host_enable(ssusb);
450
451	return 0;
 
 
 
 
 
452}
453
454static const struct dev_pm_ops mtu3_pm_ops = {
455	SET_SYSTEM_SLEEP_PM_OPS(mtu3_suspend, mtu3_resume)
456};
457
458#define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &mtu3_pm_ops : NULL)
459
460#ifdef CONFIG_OF
461
462static const struct of_device_id mtu3_of_match[] = {
463	{.compatible = "mediatek,mt8173-mtu3",},
 
464	{},
465};
466
467MODULE_DEVICE_TABLE(of, mtu3_of_match);
468
469#endif
470
471static struct platform_driver mtu3_driver = {
472	.probe = mtu3_probe,
473	.remove = mtu3_remove,
474	.driver = {
475		.name = MTU3_DRIVER_NAME,
476		.pm = DEV_PM_OPS,
477		.of_match_table = of_match_ptr(mtu3_of_match),
478	},
479};
480module_platform_driver(mtu3_driver);
481
482MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
483MODULE_LICENSE("GPL v2");
484MODULE_DESCRIPTION("MediaTek USB3 DRD Controller Driver");
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 MediaTek Inc.
  4 *
  5 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/clk.h>
  9#include <linux/dma-mapping.h>
 10#include <linux/iopoll.h>
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/of_address.h>
 14#include <linux/of_irq.h>
 
 15#include <linux/platform_device.h>
 16
 17#include "mtu3.h"
 18#include "mtu3_dr.h"
 19#include "mtu3_debug.h"
 20
 21/* u2-port0 should be powered on and enabled; */
 22int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
 23{
 24	void __iomem *ibase = ssusb->ippc_base;
 25	u32 value, check_val;
 26	int ret;
 27
 28	check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
 29			SSUSB_REF_RST_B_STS;
 30
 31	ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
 32			(check_val == (value & check_val)), 100, 20000);
 33	if (ret) {
 34		dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
 35		return ret;
 36	}
 37
 38	ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
 39			(value & SSUSB_U2_MAC_SYS_RST_B_STS), 100, 10000);
 40	if (ret) {
 41		dev_err(ssusb->dev, "mac2 clock is not stable\n");
 42		return ret;
 43	}
 44
 45	return 0;
 46}
 47
 48static int ssusb_phy_init(struct ssusb_mtk *ssusb)
 49{
 50	int i;
 51	int ret;
 52
 53	for (i = 0; i < ssusb->num_phys; i++) {
 54		ret = phy_init(ssusb->phys[i]);
 55		if (ret)
 56			goto exit_phy;
 57	}
 58	return 0;
 59
 60exit_phy:
 61	for (; i > 0; i--)
 62		phy_exit(ssusb->phys[i - 1]);
 63
 64	return ret;
 65}
 66
 67static int ssusb_phy_exit(struct ssusb_mtk *ssusb)
 68{
 69	int i;
 70
 71	for (i = 0; i < ssusb->num_phys; i++)
 72		phy_exit(ssusb->phys[i]);
 73
 74	return 0;
 75}
 76
 77static int ssusb_phy_power_on(struct ssusb_mtk *ssusb)
 78{
 79	int i;
 80	int ret;
 81
 82	for (i = 0; i < ssusb->num_phys; i++) {
 83		ret = phy_power_on(ssusb->phys[i]);
 84		if (ret)
 85			goto power_off_phy;
 86	}
 87	return 0;
 88
 89power_off_phy:
 90	for (; i > 0; i--)
 91		phy_power_off(ssusb->phys[i - 1]);
 92
 93	return ret;
 94}
 95
 96static void ssusb_phy_power_off(struct ssusb_mtk *ssusb)
 97{
 98	unsigned int i;
 99
100	for (i = 0; i < ssusb->num_phys; i++)
101		phy_power_off(ssusb->phys[i]);
102}
103
104static int ssusb_clks_enable(struct ssusb_mtk *ssusb)
105{
106	int ret;
107
108	ret = clk_prepare_enable(ssusb->sys_clk);
109	if (ret) {
110		dev_err(ssusb->dev, "failed to enable sys_clk\n");
111		goto sys_clk_err;
112	}
113
114	ret = clk_prepare_enable(ssusb->ref_clk);
115	if (ret) {
116		dev_err(ssusb->dev, "failed to enable ref_clk\n");
117		goto ref_clk_err;
118	}
119
120	ret = clk_prepare_enable(ssusb->mcu_clk);
121	if (ret) {
122		dev_err(ssusb->dev, "failed to enable mcu_clk\n");
123		goto mcu_clk_err;
124	}
125
126	ret = clk_prepare_enable(ssusb->dma_clk);
127	if (ret) {
128		dev_err(ssusb->dev, "failed to enable dma_clk\n");
129		goto dma_clk_err;
130	}
131
132	return 0;
133
134dma_clk_err:
135	clk_disable_unprepare(ssusb->mcu_clk);
136mcu_clk_err:
137	clk_disable_unprepare(ssusb->ref_clk);
138ref_clk_err:
139	clk_disable_unprepare(ssusb->sys_clk);
140sys_clk_err:
141	return ret;
142}
143
144static void ssusb_clks_disable(struct ssusb_mtk *ssusb)
145{
146	clk_disable_unprepare(ssusb->dma_clk);
147	clk_disable_unprepare(ssusb->mcu_clk);
148	clk_disable_unprepare(ssusb->ref_clk);
149	clk_disable_unprepare(ssusb->sys_clk);
150}
151
152static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
153{
154	int ret = 0;
155
156	ret = regulator_enable(ssusb->vusb33);
157	if (ret) {
158		dev_err(ssusb->dev, "failed to enable vusb33\n");
159		goto vusb33_err;
160	}
161
162	ret = ssusb_clks_enable(ssusb);
163	if (ret)
164		goto clks_err;
 
 
165
166	ret = ssusb_phy_init(ssusb);
167	if (ret) {
168		dev_err(ssusb->dev, "failed to init phy\n");
169		goto phy_init_err;
170	}
171
172	ret = ssusb_phy_power_on(ssusb);
173	if (ret) {
174		dev_err(ssusb->dev, "failed to power on phy\n");
175		goto phy_err;
176	}
177
178	return 0;
179
180phy_err:
181	ssusb_phy_exit(ssusb);
182phy_init_err:
183	ssusb_clks_disable(ssusb);
184clks_err:
185	regulator_disable(ssusb->vusb33);
186vusb33_err:
 
187	return ret;
188}
189
190static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
191{
192	ssusb_clks_disable(ssusb);
193	regulator_disable(ssusb->vusb33);
194	ssusb_phy_power_off(ssusb);
195	ssusb_phy_exit(ssusb);
196}
197
198static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
199{
200	/* reset whole ip (xhci & u3d) */
201	mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
202	udelay(1);
203	mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
 
 
 
 
 
204
205	/*
206	 * device ip may be powered on in firmware/BROM stage before entering
207	 * kernel stage;
208	 * power down device ip, otherwise ip-sleep will fail when working as
209	 * host only mode
210	 */
211	mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212}
213
214static int get_ssusb_rscs(struct platform_device *pdev, struct ssusb_mtk *ssusb)
215{
216	struct device_node *node = pdev->dev.of_node;
217	struct otg_switch_mtk *otg_sx = &ssusb->otg_switch;
218	struct device *dev = &pdev->dev;
 
219	struct resource *res;
220	int i;
221	int ret;
222
223	ssusb->vusb33 = devm_regulator_get(dev, "vusb33");
224	if (IS_ERR(ssusb->vusb33)) {
225		dev_err(dev, "failed to get vusb33\n");
226		return PTR_ERR(ssusb->vusb33);
227	}
228
229	ssusb->sys_clk = devm_clk_get(dev, "sys_ck");
230	if (IS_ERR(ssusb->sys_clk)) {
231		dev_err(dev, "failed to get sys clock\n");
232		return PTR_ERR(ssusb->sys_clk);
233	}
234
235	ssusb->ref_clk = devm_clk_get_optional(dev, "ref_ck");
236	if (IS_ERR(ssusb->ref_clk))
237		return PTR_ERR(ssusb->ref_clk);
238
239	ssusb->mcu_clk = devm_clk_get_optional(dev, "mcu_ck");
240	if (IS_ERR(ssusb->mcu_clk))
241		return PTR_ERR(ssusb->mcu_clk);
242
243	ssusb->dma_clk = devm_clk_get_optional(dev, "dma_ck");
244	if (IS_ERR(ssusb->dma_clk))
245		return PTR_ERR(ssusb->dma_clk);
246
247	ssusb->num_phys = of_count_phandle_with_args(node,
248			"phys", "#phy-cells");
249	if (ssusb->num_phys > 0) {
250		ssusb->phys = devm_kcalloc(dev, ssusb->num_phys,
251					sizeof(*ssusb->phys), GFP_KERNEL);
252		if (!ssusb->phys)
253			return -ENOMEM;
254	} else {
255		ssusb->num_phys = 0;
256	}
257
258	for (i = 0; i < ssusb->num_phys; i++) {
259		ssusb->phys[i] = devm_of_phy_get_by_index(dev, node, i);
260		if (IS_ERR(ssusb->phys[i])) {
261			dev_err(dev, "failed to get phy-%d\n", i);
262			return PTR_ERR(ssusb->phys[i]);
263		}
264	}
265
266	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
267	ssusb->ippc_base = devm_ioremap_resource(dev, res);
268	if (IS_ERR(ssusb->ippc_base))
 
269		return PTR_ERR(ssusb->ippc_base);
 
 
 
 
 
 
 
 
 
 
 
 
 
270
271	ssusb->dr_mode = usb_get_dr_mode(dev);
272	if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN)
273		ssusb->dr_mode = USB_DR_MODE_OTG;
 
 
274
275	if (ssusb->dr_mode == USB_DR_MODE_PERIPHERAL)
276		goto out;
277
278	/* if host role is supported */
279	ret = ssusb_wakeup_of_property_parse(ssusb, node);
280	if (ret) {
281		dev_err(dev, "failed to parse uwk property\n");
282		return ret;
283	}
284
285	/* optional property, ignore the error if it does not exist */
286	of_property_read_u32(node, "mediatek,u3p-dis-msk",
287			     &ssusb->u3p_dis_msk);
288
289	otg_sx->vbus = devm_regulator_get(dev, "vbus");
290	if (IS_ERR(otg_sx->vbus)) {
 
291		dev_err(dev, "failed to get vbus\n");
292		return PTR_ERR(otg_sx->vbus);
293	}
 
294
295	if (ssusb->dr_mode == USB_DR_MODE_HOST)
296		goto out;
297
298	/* if dual-role mode is supported */
299	otg_sx->is_u3_drd = of_property_read_bool(node, "mediatek,usb3-drd");
300	otg_sx->manual_drd_enabled =
301		of_property_read_bool(node, "enable-manual-drd");
302	otg_sx->role_sw_used = of_property_read_bool(node, "usb-role-switch");
303
304	if (!otg_sx->role_sw_used && of_property_read_bool(node, "extcon")) {
305		otg_sx->edev = extcon_get_edev_by_phandle(ssusb->dev, 0);
306		if (IS_ERR(otg_sx->edev)) {
307			dev_err(ssusb->dev, "couldn't get extcon device\n");
308			return PTR_ERR(otg_sx->edev);
 
 
 
 
 
309		}
310	}
311
312out:
313	dev_info(dev, "dr_mode: %d, is_u3_dr: %d, u3p_dis_msk: %x, drd: %s\n",
314		ssusb->dr_mode, otg_sx->is_u3_drd, ssusb->u3p_dis_msk,
315		otg_sx->manual_drd_enabled ? "manual" : "auto");
316
317	return 0;
318}
319
320static int mtu3_probe(struct platform_device *pdev)
321{
322	struct device_node *node = pdev->dev.of_node;
323	struct device *dev = &pdev->dev;
324	struct ssusb_mtk *ssusb;
325	int ret = -ENOMEM;
326
327	/* all elements are set to ZERO as default value */
328	ssusb = devm_kzalloc(dev, sizeof(*ssusb), GFP_KERNEL);
329	if (!ssusb)
330		return -ENOMEM;
331
332	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
333	if (ret) {
334		dev_err(dev, "No suitable DMA config available\n");
335		return -ENOTSUPP;
336	}
337
338	platform_set_drvdata(pdev, ssusb);
339	ssusb->dev = dev;
340
341	ret = get_ssusb_rscs(pdev, ssusb);
342	if (ret)
343		return ret;
344
345	ssusb_debugfs_create_root(ssusb);
346
347	/* enable power domain */
348	pm_runtime_enable(dev);
349	pm_runtime_get_sync(dev);
350	device_enable_async_suspend(dev);
351
352	ret = ssusb_rscs_init(ssusb);
353	if (ret)
354		goto comm_init_err;
355
356	ssusb_ip_sw_reset(ssusb);
357
358	if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
359		ssusb->dr_mode = USB_DR_MODE_HOST;
360	else if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
361		ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
362
363	/* default as host */
364	ssusb->is_host = !(ssusb->dr_mode == USB_DR_MODE_PERIPHERAL);
365
366	switch (ssusb->dr_mode) {
367	case USB_DR_MODE_PERIPHERAL:
368		ret = ssusb_gadget_init(ssusb);
369		if (ret) {
370			dev_err(dev, "failed to initialize gadget\n");
371			goto comm_exit;
372		}
373		break;
374	case USB_DR_MODE_HOST:
375		ret = ssusb_host_init(ssusb, node);
376		if (ret) {
377			dev_err(dev, "failed to initialize host\n");
378			goto comm_exit;
379		}
380		break;
381	case USB_DR_MODE_OTG:
382		ret = ssusb_gadget_init(ssusb);
383		if (ret) {
384			dev_err(dev, "failed to initialize gadget\n");
385			goto comm_exit;
386		}
387
388		ret = ssusb_host_init(ssusb, node);
389		if (ret) {
390			dev_err(dev, "failed to initialize host\n");
391			goto gadget_exit;
392		}
393
394		ret = ssusb_otg_switch_init(ssusb);
395		if (ret) {
396			dev_err(dev, "failed to initialize switch\n");
397			goto host_exit;
398		}
399		break;
400	default:
401		dev_err(dev, "unsupported mode: %d\n", ssusb->dr_mode);
402		ret = -EINVAL;
403		goto comm_exit;
404	}
405
406	return 0;
407
408host_exit:
409	ssusb_host_exit(ssusb);
410gadget_exit:
411	ssusb_gadget_exit(ssusb);
412comm_exit:
413	ssusb_rscs_exit(ssusb);
414comm_init_err:
415	pm_runtime_put_sync(dev);
416	pm_runtime_disable(dev);
417	ssusb_debugfs_remove_root(ssusb);
418
419	return ret;
420}
421
422static int mtu3_remove(struct platform_device *pdev)
423{
424	struct ssusb_mtk *ssusb = platform_get_drvdata(pdev);
425
426	switch (ssusb->dr_mode) {
427	case USB_DR_MODE_PERIPHERAL:
428		ssusb_gadget_exit(ssusb);
429		break;
430	case USB_DR_MODE_HOST:
431		ssusb_host_exit(ssusb);
432		break;
433	case USB_DR_MODE_OTG:
434		ssusb_otg_switch_exit(ssusb);
435		ssusb_gadget_exit(ssusb);
436		ssusb_host_exit(ssusb);
437		break;
438	default:
439		return -EINVAL;
440	}
441
442	ssusb_rscs_exit(ssusb);
443	pm_runtime_put_sync(&pdev->dev);
444	pm_runtime_disable(&pdev->dev);
445	ssusb_debugfs_remove_root(ssusb);
446
447	return 0;
448}
449
450/*
451 * when support dual-role mode, we reject suspend when
452 * it works as device mode;
453 */
454static int __maybe_unused mtu3_suspend(struct device *dev)
455{
456	struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
 
457
458	dev_dbg(dev, "%s\n", __func__);
459
460	/* REVISIT: disconnect it for only device mode? */
461	if (!ssusb->is_host)
462		return 0;
463
464	ssusb_host_disable(ssusb, true);
465	ssusb_phy_power_off(ssusb);
466	ssusb_clks_disable(ssusb);
467	ssusb_wakeup_set(ssusb, true);
468
469	return 0;
470}
471
472static int __maybe_unused mtu3_resume(struct device *dev)
473{
474	struct ssusb_mtk *ssusb = dev_get_drvdata(dev);
475	int ret;
476
477	dev_dbg(dev, "%s\n", __func__);
478
479	if (!ssusb->is_host)
480		return 0;
481
482	ssusb_wakeup_set(ssusb, false);
483	ret = ssusb_clks_enable(ssusb);
484	if (ret)
485		goto clks_err;
486
487	ret = ssusb_phy_power_on(ssusb);
488	if (ret)
489		goto phy_err;
490
491	ssusb_host_enable(ssusb);
492
493	return 0;
494
495phy_err:
496	ssusb_clks_disable(ssusb);
497clks_err:
498	return ret;
499}
500
501static const struct dev_pm_ops mtu3_pm_ops = {
502	SET_SYSTEM_SLEEP_PM_OPS(mtu3_suspend, mtu3_resume)
503};
504
505#define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &mtu3_pm_ops : NULL)
506
507#ifdef CONFIG_OF
508
509static const struct of_device_id mtu3_of_match[] = {
510	{.compatible = "mediatek,mt8173-mtu3",},
511	{.compatible = "mediatek,mtu3",},
512	{},
513};
514
515MODULE_DEVICE_TABLE(of, mtu3_of_match);
516
517#endif
518
519static struct platform_driver mtu3_driver = {
520	.probe = mtu3_probe,
521	.remove = mtu3_remove,
522	.driver = {
523		.name = MTU3_DRIVER_NAME,
524		.pm = DEV_PM_OPS,
525		.of_match_table = of_match_ptr(mtu3_of_match),
526	},
527};
528module_platform_driver(mtu3_driver);
529
530MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
531MODULE_LICENSE("GPL v2");
532MODULE_DESCRIPTION("MediaTek USB3 DRD Controller Driver");