1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Renesas R-Car Gen3 for USB2.0 PHY driver
4  *
5  * Copyright (C) 2015-2017 Renesas Electronics Corporation
6  *
7  * This is based on the phy-rcar-gen2 driver:
8  * Copyright (C) 2014 Renesas Solutions Corp.
9  * Copyright (C) 2014 Cogent Embedded, Inc.
10  */
11 
12 #include <linux/extcon-provider.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/usb/of.h>
25 #include <linux/workqueue.h>
26 
27 /******* USB2.0 Host registers (original offset is +0x200) *******/
28 #define USB2_INT_ENABLE		0x000
29 #define USB2_USBCTR		0x00c
30 #define USB2_SPD_RSM_TIMSET	0x10c
31 #define USB2_OC_TIMSET		0x110
32 #define USB2_COMMCTRL		0x600
33 #define USB2_OBINTSTA		0x604
34 #define USB2_OBINTEN		0x608
35 #define USB2_VBCTRL		0x60c
36 #define USB2_LINECTRL1		0x610
37 #define USB2_ADPCTRL		0x630
38 
39 /* INT_ENABLE */
40 #define USB2_INT_ENABLE_UCOM_INTEN	BIT(3)
41 #define USB2_INT_ENABLE_USBH_INTB_EN	BIT(2)	/* For EHCI */
42 #define USB2_INT_ENABLE_USBH_INTA_EN	BIT(1)	/* For OHCI */
43 
44 /* USBCTR */
45 #define USB2_USBCTR_DIRPD	BIT(2)
46 #define USB2_USBCTR_PLL_RST	BIT(1)
47 
48 /* SPD_RSM_TIMSET */
49 #define USB2_SPD_RSM_TIMSET_INIT	0x014e029b
50 
51 /* OC_TIMSET */
52 #define USB2_OC_TIMSET_INIT		0x000209ab
53 
54 /* COMMCTRL */
55 #define USB2_COMMCTRL_OTG_PERI		BIT(31)	/* 1 = Peripheral mode */
56 
57 /* OBINTSTA and OBINTEN */
58 #define USB2_OBINT_SESSVLDCHG		BIT(12)
59 #define USB2_OBINT_IDDIGCHG		BIT(11)
60 #define USB2_OBINT_BITS			(USB2_OBINT_SESSVLDCHG | \
61 					 USB2_OBINT_IDDIGCHG)
62 
63 /* VBCTRL */
64 #define USB2_VBCTRL_OCCLREN		BIT(16)
65 #define USB2_VBCTRL_DRVVBUSSEL		BIT(8)
66 
67 /* LINECTRL1 */
68 #define USB2_LINECTRL1_DPRPD_EN		BIT(19)
69 #define USB2_LINECTRL1_DP_RPD		BIT(18)
70 #define USB2_LINECTRL1_DMRPD_EN		BIT(17)
71 #define USB2_LINECTRL1_DM_RPD		BIT(16)
72 #define USB2_LINECTRL1_OPMODE_NODRV	BIT(6)
73 
74 /* ADPCTRL */
75 #define USB2_ADPCTRL_OTGSESSVLD		BIT(20)
76 #define USB2_ADPCTRL_IDDIG		BIT(19)
77 #define USB2_ADPCTRL_IDPULLUP		BIT(5)	/* 1 = ID sampling is enabled */
78 #define USB2_ADPCTRL_DRVVBUS		BIT(4)
79 
80 #define NUM_OF_PHYS			4
81 enum rcar_gen3_phy_index {
82 	PHY_INDEX_BOTH_HC,
83 	PHY_INDEX_OHCI,
84 	PHY_INDEX_EHCI,
85 	PHY_INDEX_HSUSB
86 };
87 
88 static const u32 rcar_gen3_int_enable[NUM_OF_PHYS] = {
89 	USB2_INT_ENABLE_USBH_INTB_EN | USB2_INT_ENABLE_USBH_INTA_EN,
90 	USB2_INT_ENABLE_USBH_INTA_EN,
91 	USB2_INT_ENABLE_USBH_INTB_EN,
92 	0
93 };
94 
95 struct rcar_gen3_phy {
96 	struct phy *phy;
97 	struct rcar_gen3_chan *ch;
98 	u32 int_enable_bits;
99 	bool initialized;
100 	bool otg_initialized;
101 	bool powered;
102 };
103 
104 struct rcar_gen3_chan {
105 	void __iomem *base;
106 	struct device *dev;	/* platform_device's device */
107 	struct extcon_dev *extcon;
108 	struct rcar_gen3_phy rphys[NUM_OF_PHYS];
109 	struct regulator *vbus;
110 	struct work_struct work;
111 	struct mutex lock;	/* protects rphys[...].powered */
112 	enum usb_dr_mode dr_mode;
113 	bool extcon_host;
114 	bool is_otg_channel;
115 	bool uses_otg_pins;
116 };
117 
118 /*
119  * Combination about is_otg_channel and uses_otg_pins:
120  *
121  * Parameters				|| Behaviors
122  * is_otg_channel	| uses_otg_pins	|| irqs		| role sysfs
123  * ---------------------+---------------++--------------+------------
124  * true			| true		|| enabled	| enabled
125  * true                 | false		|| disabled	| enabled
126  * false                | any		|| disabled	| disabled
127  */
128 
129 static void rcar_gen3_phy_usb2_work(struct work_struct *work)
130 {
131 	struct rcar_gen3_chan *ch = container_of(work, struct rcar_gen3_chan,
132 						 work);
133 
134 	if (ch->extcon_host) {
135 		extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, true);
136 		extcon_set_state_sync(ch->extcon, EXTCON_USB, false);
137 	} else {
138 		extcon_set_state_sync(ch->extcon, EXTCON_USB_HOST, false);
139 		extcon_set_state_sync(ch->extcon, EXTCON_USB, true);
140 	}
141 }
142 
143 static void rcar_gen3_set_host_mode(struct rcar_gen3_chan *ch, int host)
144 {
145 	void __iomem *usb2_base = ch->base;
146 	u32 val = readl(usb2_base + USB2_COMMCTRL);
147 
148 	dev_vdbg(ch->dev, "%s: %08x, %d\n", __func__, val, host);
149 	if (host)
150 		val &= ~USB2_COMMCTRL_OTG_PERI;
151 	else
152 		val |= USB2_COMMCTRL_OTG_PERI;
153 	writel(val, usb2_base + USB2_COMMCTRL);
154 }
155 
156 static void rcar_gen3_set_linectrl(struct rcar_gen3_chan *ch, int dp, int dm)
157 {
158 	void __iomem *usb2_base = ch->base;
159 	u32 val = readl(usb2_base + USB2_LINECTRL1);
160 
161 	dev_vdbg(ch->dev, "%s: %08x, %d, %d\n", __func__, val, dp, dm);
162 	val &= ~(USB2_LINECTRL1_DP_RPD | USB2_LINECTRL1_DM_RPD);
163 	if (dp)
164 		val |= USB2_LINECTRL1_DP_RPD;
165 	if (dm)
166 		val |= USB2_LINECTRL1_DM_RPD;
167 	writel(val, usb2_base + USB2_LINECTRL1);
168 }
169 
170 static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus)
171 {
172 	void __iomem *usb2_base = ch->base;
173 	u32 val = readl(usb2_base + USB2_ADPCTRL);
174 
175 	dev_vdbg(ch->dev, "%s: %08x, %d\n", __func__, val, vbus);
176 	if (vbus)
177 		val |= USB2_ADPCTRL_DRVVBUS;
178 	else
179 		val &= ~USB2_ADPCTRL_DRVVBUS;
180 	writel(val, usb2_base + USB2_ADPCTRL);
181 }
182 
183 static void rcar_gen3_control_otg_irq(struct rcar_gen3_chan *ch, int enable)
184 {
185 	void __iomem *usb2_base = ch->base;
186 	u32 val = readl(usb2_base + USB2_OBINTEN);
187 
188 	if (ch->uses_otg_pins && enable)
189 		val |= USB2_OBINT_BITS;
190 	else
191 		val &= ~USB2_OBINT_BITS;
192 	writel(val, usb2_base + USB2_OBINTEN);
193 }
194 
195 static void rcar_gen3_init_for_host(struct rcar_gen3_chan *ch)
196 {
197 	rcar_gen3_set_linectrl(ch, 1, 1);
198 	rcar_gen3_set_host_mode(ch, 1);
199 	rcar_gen3_enable_vbus_ctrl(ch, 1);
200 
201 	ch->extcon_host = true;
202 	schedule_work(&ch->work);
203 }
204 
205 static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch)
206 {
207 	rcar_gen3_set_linectrl(ch, 0, 1);
208 	rcar_gen3_set_host_mode(ch, 0);
209 	rcar_gen3_enable_vbus_ctrl(ch, 0);
210 
211 	ch->extcon_host = false;
212 	schedule_work(&ch->work);
213 }
214 
215 static void rcar_gen3_init_for_b_host(struct rcar_gen3_chan *ch)
216 {
217 	void __iomem *usb2_base = ch->base;
218 	u32 val;
219 
220 	val = readl(usb2_base + USB2_LINECTRL1);
221 	writel(val | USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
222 
223 	rcar_gen3_set_linectrl(ch, 1, 1);
224 	rcar_gen3_set_host_mode(ch, 1);
225 	rcar_gen3_enable_vbus_ctrl(ch, 0);
226 
227 	val = readl(usb2_base + USB2_LINECTRL1);
228 	writel(val & ~USB2_LINECTRL1_OPMODE_NODRV, usb2_base + USB2_LINECTRL1);
229 }
230 
231 static void rcar_gen3_init_for_a_peri(struct rcar_gen3_chan *ch)
232 {
233 	rcar_gen3_set_linectrl(ch, 0, 1);
234 	rcar_gen3_set_host_mode(ch, 0);
235 	rcar_gen3_enable_vbus_ctrl(ch, 1);
236 }
237 
238 static void rcar_gen3_init_from_a_peri_to_a_host(struct rcar_gen3_chan *ch)
239 {
240 	rcar_gen3_control_otg_irq(ch, 0);
241 
242 	rcar_gen3_enable_vbus_ctrl(ch, 1);
243 	rcar_gen3_init_for_host(ch);
244 
245 	rcar_gen3_control_otg_irq(ch, 1);
246 }
247 
248 static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch)
249 {
250 	if (!ch->uses_otg_pins)
251 		return (ch->dr_mode == USB_DR_MODE_HOST) ? false : true;
252 
253 	return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG);
254 }
255 
256 static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch)
257 {
258 	if (!rcar_gen3_check_id(ch))
259 		rcar_gen3_init_for_host(ch);
260 	else
261 		rcar_gen3_init_for_peri(ch);
262 }
263 
264 static bool rcar_gen3_is_host(struct rcar_gen3_chan *ch)
265 {
266 	return !(readl(ch->base + USB2_COMMCTRL) & USB2_COMMCTRL_OTG_PERI);
267 }
268 
269 static enum phy_mode rcar_gen3_get_phy_mode(struct rcar_gen3_chan *ch)
270 {
271 	if (rcar_gen3_is_host(ch))
272 		return PHY_MODE_USB_HOST;
273 
274 	return PHY_MODE_USB_DEVICE;
275 }
276 
277 static bool rcar_gen3_is_any_rphy_initialized(struct rcar_gen3_chan *ch)
278 {
279 	int i;
280 
281 	for (i = 0; i < NUM_OF_PHYS; i++) {
282 		if (ch->rphys[i].initialized)
283 			return true;
284 	}
285 
286 	return false;
287 }
288 
289 static bool rcar_gen3_needs_init_otg(struct rcar_gen3_chan *ch)
290 {
291 	int i;
292 
293 	for (i = 0; i < NUM_OF_PHYS; i++) {
294 		if (ch->rphys[i].otg_initialized)
295 			return false;
296 	}
297 
298 	return true;
299 }
300 
301 static bool rcar_gen3_are_all_rphys_power_off(struct rcar_gen3_chan *ch)
302 {
303 	int i;
304 
305 	for (i = 0; i < NUM_OF_PHYS; i++) {
306 		if (ch->rphys[i].powered)
307 			return false;
308 	}
309 
310 	return true;
311 }
312 
313 static ssize_t role_store(struct device *dev, struct device_attribute *attr,
314 			  const char *buf, size_t count)
315 {
316 	struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
317 	bool is_b_device;
318 	enum phy_mode cur_mode, new_mode;
319 
320 	if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch))
321 		return -EIO;
322 
323 	if (!strncmp(buf, "host", strlen("host")))
324 		new_mode = PHY_MODE_USB_HOST;
325 	else if (!strncmp(buf, "peripheral", strlen("peripheral")))
326 		new_mode = PHY_MODE_USB_DEVICE;
327 	else
328 		return -EINVAL;
329 
330 	/* is_b_device: true is B-Device. false is A-Device. */
331 	is_b_device = rcar_gen3_check_id(ch);
332 	cur_mode = rcar_gen3_get_phy_mode(ch);
333 
334 	/* If current and new mode is the same, this returns the error */
335 	if (cur_mode == new_mode)
336 		return -EINVAL;
337 
338 	if (new_mode == PHY_MODE_USB_HOST) { /* And is_host must be false */
339 		if (!is_b_device)	/* A-Peripheral */
340 			rcar_gen3_init_from_a_peri_to_a_host(ch);
341 		else			/* B-Peripheral */
342 			rcar_gen3_init_for_b_host(ch);
343 	} else {			/* And is_host must be true */
344 		if (!is_b_device)	/* A-Host */
345 			rcar_gen3_init_for_a_peri(ch);
346 		else			/* B-Host */
347 			rcar_gen3_init_for_peri(ch);
348 	}
349 
350 	return count;
351 }
352 
353 static ssize_t role_show(struct device *dev, struct device_attribute *attr,
354 			 char *buf)
355 {
356 	struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
357 
358 	if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch))
359 		return -EIO;
360 
361 	return sprintf(buf, "%s\n", rcar_gen3_is_host(ch) ? "host" :
362 							    "peripheral");
363 }
364 static DEVICE_ATTR_RW(role);
365 
366 static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
367 {
368 	void __iomem *usb2_base = ch->base;
369 	u32 val;
370 
371 	/* Should not use functions of read-modify-write a register */
372 	val = readl(usb2_base + USB2_LINECTRL1);
373 	val = (val & ~USB2_LINECTRL1_DP_RPD) | USB2_LINECTRL1_DPRPD_EN |
374 	      USB2_LINECTRL1_DMRPD_EN | USB2_LINECTRL1_DM_RPD;
375 	writel(val, usb2_base + USB2_LINECTRL1);
376 
377 	val = readl(usb2_base + USB2_VBCTRL);
378 	val &= ~USB2_VBCTRL_OCCLREN;
379 	writel(val | USB2_VBCTRL_DRVVBUSSEL, usb2_base + USB2_VBCTRL);
380 	val = readl(usb2_base + USB2_ADPCTRL);
381 	writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL);
382 
383 	msleep(20);
384 
385 	writel(0xffffffff, usb2_base + USB2_OBINTSTA);
386 	writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTEN);
387 
388 	rcar_gen3_device_recognition(ch);
389 }
390 
391 static int rcar_gen3_phy_usb2_init(struct phy *p)
392 {
393 	struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
394 	struct rcar_gen3_chan *channel = rphy->ch;
395 	void __iomem *usb2_base = channel->base;
396 	u32 val;
397 
398 	/* Initialize USB2 part */
399 	val = readl(usb2_base + USB2_INT_ENABLE);
400 	val |= USB2_INT_ENABLE_UCOM_INTEN | rphy->int_enable_bits;
401 	writel(val, usb2_base + USB2_INT_ENABLE);
402 	writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET);
403 	writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET);
404 
405 	/* Initialize otg part */
406 	if (channel->is_otg_channel) {
407 		if (rcar_gen3_needs_init_otg(channel))
408 			rcar_gen3_init_otg(channel);
409 		rphy->otg_initialized = true;
410 	}
411 
412 	rphy->initialized = true;
413 
414 	return 0;
415 }
416 
417 static int rcar_gen3_phy_usb2_exit(struct phy *p)
418 {
419 	struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
420 	struct rcar_gen3_chan *channel = rphy->ch;
421 	void __iomem *usb2_base = channel->base;
422 	u32 val;
423 
424 	rphy->initialized = false;
425 
426 	if (channel->is_otg_channel)
427 		rphy->otg_initialized = false;
428 
429 	val = readl(usb2_base + USB2_INT_ENABLE);
430 	val &= ~rphy->int_enable_bits;
431 	if (!rcar_gen3_is_any_rphy_initialized(channel))
432 		val &= ~USB2_INT_ENABLE_UCOM_INTEN;
433 	writel(val, usb2_base + USB2_INT_ENABLE);
434 
435 	return 0;
436 }
437 
438 static int rcar_gen3_phy_usb2_power_on(struct phy *p)
439 {
440 	struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
441 	struct rcar_gen3_chan *channel = rphy->ch;
442 	void __iomem *usb2_base = channel->base;
443 	u32 val;
444 	int ret = 0;
445 
446 	mutex_lock(&channel->lock);
447 	if (!rcar_gen3_are_all_rphys_power_off(channel))
448 		goto out;
449 
450 	if (channel->vbus) {
451 		ret = regulator_enable(channel->vbus);
452 		if (ret)
453 			goto out;
454 	}
455 
456 	val = readl(usb2_base + USB2_USBCTR);
457 	val |= USB2_USBCTR_PLL_RST;
458 	writel(val, usb2_base + USB2_USBCTR);
459 	val &= ~USB2_USBCTR_PLL_RST;
460 	writel(val, usb2_base + USB2_USBCTR);
461 
462 out:
463 	/* The powered flag should be set for any other phys anyway */
464 	rphy->powered = true;
465 	mutex_unlock(&channel->lock);
466 
467 	return 0;
468 }
469 
470 static int rcar_gen3_phy_usb2_power_off(struct phy *p)
471 {
472 	struct rcar_gen3_phy *rphy = phy_get_drvdata(p);
473 	struct rcar_gen3_chan *channel = rphy->ch;
474 	int ret = 0;
475 
476 	mutex_lock(&channel->lock);
477 	rphy->powered = false;
478 
479 	if (!rcar_gen3_are_all_rphys_power_off(channel))
480 		goto out;
481 
482 	if (channel->vbus)
483 		ret = regulator_disable(channel->vbus);
484 
485 out:
486 	mutex_unlock(&channel->lock);
487 
488 	return ret;
489 }
490 
491 static const struct phy_ops rcar_gen3_phy_usb2_ops = {
492 	.init		= rcar_gen3_phy_usb2_init,
493 	.exit		= rcar_gen3_phy_usb2_exit,
494 	.power_on	= rcar_gen3_phy_usb2_power_on,
495 	.power_off	= rcar_gen3_phy_usb2_power_off,
496 	.owner		= THIS_MODULE,
497 };
498 
499 static const struct phy_ops rz_g1c_phy_usb2_ops = {
500 	.init		= rcar_gen3_phy_usb2_init,
501 	.exit		= rcar_gen3_phy_usb2_exit,
502 	.owner		= THIS_MODULE,
503 };
504 
505 static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch)
506 {
507 	struct rcar_gen3_chan *ch = _ch;
508 	void __iomem *usb2_base = ch->base;
509 	u32 status = readl(usb2_base + USB2_OBINTSTA);
510 	irqreturn_t ret = IRQ_NONE;
511 
512 	if (status & USB2_OBINT_BITS) {
513 		dev_vdbg(ch->dev, "%s: %08x\n", __func__, status);
514 		writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTSTA);
515 		rcar_gen3_device_recognition(ch);
516 		ret = IRQ_HANDLED;
517 	}
518 
519 	return ret;
520 }
521 
522 static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = {
523 	{
524 		.compatible = "renesas,usb2-phy-r8a77470",
525 		.data = &rz_g1c_phy_usb2_ops,
526 	},
527 	{
528 		.compatible = "renesas,usb2-phy-r8a7795",
529 		.data = &rcar_gen3_phy_usb2_ops,
530 	},
531 	{
532 		.compatible = "renesas,usb2-phy-r8a7796",
533 		.data = &rcar_gen3_phy_usb2_ops,
534 	},
535 	{
536 		.compatible = "renesas,usb2-phy-r8a77965",
537 		.data = &rcar_gen3_phy_usb2_ops,
538 	},
539 	{
540 		.compatible = "renesas,rcar-gen3-usb2-phy",
541 		.data = &rcar_gen3_phy_usb2_ops,
542 	},
543 	{ /* sentinel */ },
544 };
545 MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table);
546 
547 static const unsigned int rcar_gen3_phy_cable[] = {
548 	EXTCON_USB,
549 	EXTCON_USB_HOST,
550 	EXTCON_NONE,
551 };
552 
553 static struct phy *rcar_gen3_phy_usb2_xlate(struct device *dev,
554 					    struct of_phandle_args *args)
555 {
556 	struct rcar_gen3_chan *ch = dev_get_drvdata(dev);
557 
558 	if (args->args_count == 0)	/* For old version dts */
559 		return ch->rphys[PHY_INDEX_BOTH_HC].phy;
560 	else if (args->args_count > 1)	/* Prevent invalid args count */
561 		return ERR_PTR(-ENODEV);
562 
563 	if (args->args[0] >= NUM_OF_PHYS)
564 		return ERR_PTR(-ENODEV);
565 
566 	return ch->rphys[args->args[0]].phy;
567 }
568 
569 static enum usb_dr_mode rcar_gen3_get_dr_mode(struct device_node *np)
570 {
571 	enum usb_dr_mode candidate = USB_DR_MODE_UNKNOWN;
572 	int i;
573 
574 	/*
575 	 * If one of device nodes has other dr_mode except UNKNOWN,
576 	 * this function returns UNKNOWN. To achieve backward compatibility,
577 	 * this loop starts the index as 0.
578 	 */
579 	for (i = 0; i < NUM_OF_PHYS; i++) {
580 		enum usb_dr_mode mode = of_usb_get_dr_mode_by_phy(np, i);
581 
582 		if (mode != USB_DR_MODE_UNKNOWN) {
583 			if (candidate == USB_DR_MODE_UNKNOWN)
584 				candidate = mode;
585 			else if (candidate != mode)
586 				return USB_DR_MODE_UNKNOWN;
587 		}
588 	}
589 
590 	return candidate;
591 }
592 
593 static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
594 {
595 	struct device *dev = &pdev->dev;
596 	struct rcar_gen3_chan *channel;
597 	struct phy_provider *provider;
598 	struct resource *res;
599 	const struct phy_ops *phy_usb2_ops;
600 	int irq, ret = 0, i;
601 
602 	if (!dev->of_node) {
603 		dev_err(dev, "This driver needs device tree\n");
604 		return -EINVAL;
605 	}
606 
607 	channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
608 	if (!channel)
609 		return -ENOMEM;
610 
611 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
612 	channel->base = devm_ioremap_resource(dev, res);
613 	if (IS_ERR(channel->base))
614 		return PTR_ERR(channel->base);
615 
616 	/* call request_irq for OTG */
617 	irq = platform_get_irq(pdev, 0);
618 	if (irq >= 0) {
619 		INIT_WORK(&channel->work, rcar_gen3_phy_usb2_work);
620 		irq = devm_request_irq(dev, irq, rcar_gen3_phy_usb2_irq,
621 				       IRQF_SHARED, dev_name(dev), channel);
622 		if (irq < 0)
623 			dev_err(dev, "No irq handler (%d)\n", irq);
624 	}
625 
626 	channel->dr_mode = rcar_gen3_get_dr_mode(dev->of_node);
627 	if (channel->dr_mode != USB_DR_MODE_UNKNOWN) {
628 		int ret;
629 
630 		channel->is_otg_channel = true;
631 		channel->uses_otg_pins = !of_property_read_bool(dev->of_node,
632 							"renesas,no-otg-pins");
633 		channel->extcon = devm_extcon_dev_allocate(dev,
634 							rcar_gen3_phy_cable);
635 		if (IS_ERR(channel->extcon))
636 			return PTR_ERR(channel->extcon);
637 
638 		ret = devm_extcon_dev_register(dev, channel->extcon);
639 		if (ret < 0) {
640 			dev_err(dev, "Failed to register extcon\n");
641 			return ret;
642 		}
643 	}
644 
645 	/*
646 	 * devm_phy_create() will call pm_runtime_enable(&phy->dev);
647 	 * And then, phy-core will manage runtime pm for this device.
648 	 */
649 	pm_runtime_enable(dev);
650 	phy_usb2_ops = of_device_get_match_data(dev);
651 	if (!phy_usb2_ops)
652 		return -EINVAL;
653 
654 	mutex_init(&channel->lock);
655 	for (i = 0; i < NUM_OF_PHYS; i++) {
656 		channel->rphys[i].phy = devm_phy_create(dev, NULL,
657 							phy_usb2_ops);
658 		if (IS_ERR(channel->rphys[i].phy)) {
659 			dev_err(dev, "Failed to create USB2 PHY\n");
660 			ret = PTR_ERR(channel->rphys[i].phy);
661 			goto error;
662 		}
663 		channel->rphys[i].ch = channel;
664 		channel->rphys[i].int_enable_bits = rcar_gen3_int_enable[i];
665 		phy_set_drvdata(channel->rphys[i].phy, &channel->rphys[i]);
666 	}
667 
668 	channel->vbus = devm_regulator_get_optional(dev, "vbus");
669 	if (IS_ERR(channel->vbus)) {
670 		if (PTR_ERR(channel->vbus) == -EPROBE_DEFER) {
671 			ret = PTR_ERR(channel->vbus);
672 			goto error;
673 		}
674 		channel->vbus = NULL;
675 	}
676 
677 	platform_set_drvdata(pdev, channel);
678 	channel->dev = dev;
679 
680 	provider = devm_of_phy_provider_register(dev, rcar_gen3_phy_usb2_xlate);
681 	if (IS_ERR(provider)) {
682 		dev_err(dev, "Failed to register PHY provider\n");
683 		ret = PTR_ERR(provider);
684 		goto error;
685 	} else if (channel->is_otg_channel) {
686 		int ret;
687 
688 		ret = device_create_file(dev, &dev_attr_role);
689 		if (ret < 0)
690 			goto error;
691 	}
692 
693 	return 0;
694 
695 error:
696 	pm_runtime_disable(dev);
697 
698 	return ret;
699 }
700 
701 static int rcar_gen3_phy_usb2_remove(struct platform_device *pdev)
702 {
703 	struct rcar_gen3_chan *channel = platform_get_drvdata(pdev);
704 
705 	if (channel->is_otg_channel)
706 		device_remove_file(&pdev->dev, &dev_attr_role);
707 
708 	pm_runtime_disable(&pdev->dev);
709 
710 	return 0;
711 };
712 
713 static struct platform_driver rcar_gen3_phy_usb2_driver = {
714 	.driver = {
715 		.name		= "phy_rcar_gen3_usb2",
716 		.of_match_table	= rcar_gen3_phy_usb2_match_table,
717 	},
718 	.probe	= rcar_gen3_phy_usb2_probe,
719 	.remove = rcar_gen3_phy_usb2_remove,
720 };
721 module_platform_driver(rcar_gen3_phy_usb2_driver);
722 
723 MODULE_LICENSE("GPL v2");
724 MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
725 MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
726