perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / usb / renesas_usbhs / common.c
bloba3e1290d682d364dd095328dff810c38059b5104
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3 * Renesas USB driver
5 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 */
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/gpio.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include "common.h"
20 #include "rcar2.h"
21 #include "rcar3.h"
22 #include "rza.h"
25 * image of renesas_usbhs
27 * ex) gadget case
29 * mod.c
30 * mod_gadget.c
31 * mod_host.c pipe.c fifo.c
33 * +-------+ +-----------+
34 * | pipe0 |------>| fifo pio |
35 * +------------+ +-------+ +-----------+
36 * | mod_gadget |=====> | pipe1 |--+
37 * +------------+ +-------+ | +-----------+
38 * | pipe2 | | +-| fifo dma0 |
39 * +------------+ +-------+ | | +-----------+
40 * | mod_host | | pipe3 |<-|--+
41 * +------------+ +-------+ | +-----------+
42 * | .... | +--->| fifo dma1 |
43 * | .... | +-----------+
47 #define USBHSF_RUNTIME_PWCTRL (1 << 0)
49 /* status */
50 #define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
51 #define usbhsc_flags_set(p, b) ((p)->flags |= (b))
52 #define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53 #define usbhsc_flags_has(p, b) ((p)->flags & (b))
56 * platform call back
58 * renesas usb support platform callback function.
59 * Below macro call it.
60 * if platform doesn't have callback, it return 0 (no error)
62 #define usbhs_platform_call(priv, func, args...)\
63 (!(priv) ? -ENODEV : \
64 !((priv)->pfunc.func) ? 0 : \
65 (priv)->pfunc.func(args))
68 * common functions
70 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
72 return ioread16(priv->base + reg);
75 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
77 iowrite16(data, priv->base + reg);
80 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
82 u16 val = usbhs_read(priv, reg);
84 val &= ~mask;
85 val |= data & mask;
87 usbhs_write(priv, reg, val);
90 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
92 return dev_get_drvdata(&pdev->dev);
96 * syscfg functions
98 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
100 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
103 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
105 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
106 u16 val = DCFM | DRPD | HSE | USBE;
107 int has_otg = usbhs_get_dparam(priv, has_otg);
109 if (has_otg)
110 usbhs_bset(priv, DVSTCTR, (EXTLP | PWEN), (EXTLP | PWEN));
113 * if enable
115 * - select Host mode
116 * - D+ Line/D- Line Pull-down
118 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
121 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
123 u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
124 u16 val = HSE | USBE;
127 * if enable
129 * - select Function mode
130 * - D+ Line Pull-up is disabled
131 * When D+ Line Pull-up is enabled,
132 * calling usbhs_sys_function_pullup(,1)
134 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
137 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
139 usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
142 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
144 usbhs_write(priv, TESTMODE, mode);
148 * frame functions
150 int usbhs_frame_get_num(struct usbhs_priv *priv)
152 return usbhs_read(priv, FRMNUM) & FRNM_MASK;
156 * usb request functions
158 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
160 u16 val;
162 val = usbhs_read(priv, USBREQ);
163 req->bRequest = (val >> 8) & 0xFF;
164 req->bRequestType = (val >> 0) & 0xFF;
166 req->wValue = usbhs_read(priv, USBVAL);
167 req->wIndex = usbhs_read(priv, USBINDX);
168 req->wLength = usbhs_read(priv, USBLENG);
171 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
173 usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType);
174 usbhs_write(priv, USBVAL, req->wValue);
175 usbhs_write(priv, USBINDX, req->wIndex);
176 usbhs_write(priv, USBLENG, req->wLength);
178 usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
182 * bus/vbus functions
184 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
186 u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
188 if (status != USBRST) {
189 struct device *dev = usbhs_priv_to_dev(priv);
190 dev_err(dev, "usbhs should be reset\n");
193 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
196 void usbhs_bus_send_reset(struct usbhs_priv *priv)
198 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
201 int usbhs_bus_get_speed(struct usbhs_priv *priv)
203 u16 dvstctr = usbhs_read(priv, DVSTCTR);
205 switch (RHST & dvstctr) {
206 case RHST_LOW_SPEED:
207 return USB_SPEED_LOW;
208 case RHST_FULL_SPEED:
209 return USB_SPEED_FULL;
210 case RHST_HIGH_SPEED:
211 return USB_SPEED_HIGH;
214 return USB_SPEED_UNKNOWN;
217 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
219 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
221 return usbhs_platform_call(priv, set_vbus, pdev, enable);
224 static void usbhsc_bus_init(struct usbhs_priv *priv)
226 usbhs_write(priv, DVSTCTR, 0);
228 usbhs_vbus_ctrl(priv, 0);
232 * device configuration
234 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
235 u16 upphub, u16 hubport, u16 speed)
237 struct device *dev = usbhs_priv_to_dev(priv);
238 u16 usbspd = 0;
239 u32 reg = DEVADD0 + (2 * devnum);
241 if (devnum > 10) {
242 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
243 return -EIO;
246 if (upphub > 0xA) {
247 dev_err(dev, "unsupported hub number %d\n", upphub);
248 return -EIO;
251 switch (speed) {
252 case USB_SPEED_LOW:
253 usbspd = USBSPD_SPEED_LOW;
254 break;
255 case USB_SPEED_FULL:
256 usbspd = USBSPD_SPEED_FULL;
257 break;
258 case USB_SPEED_HIGH:
259 usbspd = USBSPD_SPEED_HIGH;
260 break;
261 default:
262 dev_err(dev, "unsupported speed %d\n", speed);
263 return -EIO;
266 usbhs_write(priv, reg, UPPHUB(upphub) |
267 HUBPORT(hubport)|
268 USBSPD(usbspd));
270 return 0;
274 * interrupt functions
276 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
278 u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
280 usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
284 * local functions
286 static void usbhsc_set_buswait(struct usbhs_priv *priv)
288 int wait = usbhs_get_dparam(priv, buswait_bwait);
290 /* set bus wait if platform have */
291 if (wait)
292 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
295 static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
297 if (priv->dparam.type == USBHS_TYPE_RCAR_GEN3 ||
298 priv->dparam.type == USBHS_TYPE_RCAR_GEN3_WITH_PLL)
299 return true;
301 return false;
304 static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
306 if (!usbhsc_is_multi_clks(priv))
307 return 0;
309 /* The first clock should exist */
310 priv->clks[0] = of_clk_get(dev->of_node, 0);
311 if (IS_ERR(priv->clks[0]))
312 return PTR_ERR(priv->clks[0]);
315 * To backward compatibility with old DT, this driver checks the return
316 * value if it's -ENOENT or not.
318 priv->clks[1] = of_clk_get(dev->of_node, 1);
319 if (PTR_ERR(priv->clks[1]) == -ENOENT)
320 priv->clks[1] = NULL;
321 else if (IS_ERR(priv->clks[1]))
322 return PTR_ERR(priv->clks[1]);
324 return 0;
327 static void usbhsc_clk_put(struct usbhs_priv *priv)
329 int i;
331 if (!usbhsc_is_multi_clks(priv))
332 return;
334 for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
335 clk_put(priv->clks[i]);
338 static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
340 int i, ret;
342 if (!usbhsc_is_multi_clks(priv))
343 return 0;
345 for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
346 ret = clk_prepare_enable(priv->clks[i]);
347 if (ret) {
348 while (--i >= 0)
349 clk_disable_unprepare(priv->clks[i]);
350 return ret;
354 return ret;
357 static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
359 int i;
361 if (!usbhsc_is_multi_clks(priv))
362 return;
364 for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
365 clk_disable_unprepare(priv->clks[i]);
369 * platform default param
372 /* commonly used on old SH-Mobile SoCs */
373 static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
374 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
375 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
376 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
377 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
378 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
379 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
380 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
381 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
382 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
383 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
386 /* commonly used on newer SH-Mobile and R-Car SoCs */
387 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
388 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
389 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
390 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
391 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
392 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
393 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
394 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
395 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
396 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
397 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
398 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
399 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
400 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
401 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
402 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
403 RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
407 * power control
409 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
411 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
412 struct device *dev = usbhs_priv_to_dev(priv);
414 if (enable) {
415 /* enable PM */
416 pm_runtime_get_sync(dev);
418 /* enable clks */
419 if (usbhsc_clk_prepare_enable(priv))
420 return;
422 /* enable platform power */
423 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
425 /* USB on */
426 usbhs_sys_clock_ctrl(priv, enable);
427 } else {
428 /* USB off */
429 usbhs_sys_clock_ctrl(priv, enable);
431 /* disable platform power */
432 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
434 /* disable clks */
435 usbhsc_clk_disable_unprepare(priv);
437 /* disable PM */
438 pm_runtime_put_sync(dev);
443 * hotplug
445 static void usbhsc_hotplug(struct usbhs_priv *priv)
447 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
448 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
449 int id;
450 int enable;
451 int cable;
452 int ret;
455 * get vbus status from platform
457 enable = usbhs_platform_call(priv, get_vbus, pdev);
460 * get id from platform
462 id = usbhs_platform_call(priv, get_id, pdev);
464 if (enable && !mod) {
465 if (priv->edev) {
466 cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
467 if ((cable > 0 && id != USBHS_HOST) ||
468 (!cable && id != USBHS_GADGET)) {
469 dev_info(&pdev->dev,
470 "USB cable plugged in doesn't match the selected role!\n");
471 return;
475 ret = usbhs_mod_change(priv, id);
476 if (ret < 0)
477 return;
479 dev_dbg(&pdev->dev, "%s enable\n", __func__);
481 /* power on */
482 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
483 usbhsc_power_ctrl(priv, enable);
485 /* bus init */
486 usbhsc_set_buswait(priv);
487 usbhsc_bus_init(priv);
489 /* module start */
490 usbhs_mod_call(priv, start, priv);
492 } else if (!enable && mod) {
493 dev_dbg(&pdev->dev, "%s disable\n", __func__);
495 /* module stop */
496 usbhs_mod_call(priv, stop, priv);
498 /* bus init */
499 usbhsc_bus_init(priv);
501 /* power off */
502 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
503 usbhsc_power_ctrl(priv, enable);
505 usbhs_mod_change(priv, -1);
507 /* reset phy for next connection */
508 usbhs_platform_call(priv, phy_reset, pdev);
513 * notify hotplug
515 static void usbhsc_notify_hotplug(struct work_struct *work)
517 struct usbhs_priv *priv = container_of(work,
518 struct usbhs_priv,
519 notify_hotplug_work.work);
520 usbhsc_hotplug(priv);
523 static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
525 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
526 int delay = usbhs_get_dparam(priv, detection_delay);
529 * This functions will be called in interrupt.
530 * To make sure safety context,
531 * use workqueue for usbhs_notify_hotplug
533 schedule_delayed_work(&priv->notify_hotplug_work,
534 msecs_to_jiffies(delay));
535 return 0;
539 * platform functions
541 static const struct of_device_id usbhs_of_match[] = {
543 .compatible = "renesas,usbhs-r8a7790",
544 .data = (void *)USBHS_TYPE_RCAR_GEN2,
547 .compatible = "renesas,usbhs-r8a7791",
548 .data = (void *)USBHS_TYPE_RCAR_GEN2,
551 .compatible = "renesas,usbhs-r8a7794",
552 .data = (void *)USBHS_TYPE_RCAR_GEN2,
555 .compatible = "renesas,usbhs-r8a7795",
556 .data = (void *)USBHS_TYPE_RCAR_GEN3,
559 .compatible = "renesas,usbhs-r8a7796",
560 .data = (void *)USBHS_TYPE_RCAR_GEN3,
563 .compatible = "renesas,usbhs-r8a77990",
564 .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
567 .compatible = "renesas,usbhs-r8a77995",
568 .data = (void *)USBHS_TYPE_RCAR_GEN3_WITH_PLL,
571 .compatible = "renesas,rcar-gen2-usbhs",
572 .data = (void *)USBHS_TYPE_RCAR_GEN2,
575 .compatible = "renesas,rcar-gen3-usbhs",
576 .data = (void *)USBHS_TYPE_RCAR_GEN3,
579 .compatible = "renesas,rza1-usbhs",
580 .data = (void *)USBHS_TYPE_RZA1,
582 { },
584 MODULE_DEVICE_TABLE(of, usbhs_of_match);
586 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
588 struct renesas_usbhs_platform_info *info;
589 struct renesas_usbhs_driver_param *dparam;
590 u32 tmp;
591 int gpio;
593 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
594 if (!info)
595 return NULL;
597 dparam = &info->driver_param;
598 dparam->type = (uintptr_t)of_device_get_match_data(dev);
599 if (!of_property_read_u32(dev->of_node, "renesas,buswait", &tmp))
600 dparam->buswait_bwait = tmp;
601 gpio = of_get_named_gpio_flags(dev->of_node, "renesas,enable-gpio", 0,
602 NULL);
603 if (gpio > 0)
604 dparam->enable_gpio = gpio;
606 if (dparam->type == USBHS_TYPE_RCAR_GEN2 ||
607 dparam->type == USBHS_TYPE_RCAR_GEN3 ||
608 dparam->type == USBHS_TYPE_RCAR_GEN3_WITH_PLL) {
609 dparam->has_usb_dmac = 1;
610 dparam->pipe_configs = usbhsc_new_pipe;
611 dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
614 if (dparam->type == USBHS_TYPE_RZA1) {
615 dparam->pipe_configs = usbhsc_new_pipe;
616 dparam->pipe_size = ARRAY_SIZE(usbhsc_new_pipe);
619 return info;
622 static int usbhs_probe(struct platform_device *pdev)
624 struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
625 struct renesas_usbhs_driver_callback *dfunc;
626 struct usbhs_priv *priv;
627 struct resource *res, *irq_res;
628 int ret;
630 /* check device node */
631 if (pdev->dev.of_node)
632 info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
634 /* check platform information */
635 if (!info) {
636 dev_err(&pdev->dev, "no platform information\n");
637 return -EINVAL;
640 /* platform data */
641 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
642 if (!irq_res) {
643 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
644 return -ENODEV;
647 /* usb private data */
648 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
649 if (!priv)
650 return -ENOMEM;
652 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653 priv->base = devm_ioremap_resource(&pdev->dev, res);
654 if (IS_ERR(priv->base))
655 return PTR_ERR(priv->base);
657 if (of_property_read_bool(pdev->dev.of_node, "extcon")) {
658 priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
659 if (IS_ERR(priv->edev))
660 return PTR_ERR(priv->edev);
663 priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
664 if (IS_ERR(priv->rsts))
665 return PTR_ERR(priv->rsts);
668 * care platform info
671 memcpy(&priv->dparam,
672 &info->driver_param,
673 sizeof(struct renesas_usbhs_driver_param));
675 switch (priv->dparam.type) {
676 case USBHS_TYPE_RCAR_GEN2:
677 priv->pfunc = usbhs_rcar2_ops;
678 break;
679 case USBHS_TYPE_RCAR_GEN3:
680 priv->pfunc = usbhs_rcar3_ops;
681 break;
682 case USBHS_TYPE_RCAR_GEN3_WITH_PLL:
683 priv->pfunc = usbhs_rcar3_with_pll_ops;
684 break;
685 case USBHS_TYPE_RZA1:
686 priv->pfunc = usbhs_rza1_ops;
687 break;
688 default:
689 if (!info->platform_callback.get_id) {
690 dev_err(&pdev->dev, "no platform callbacks");
691 return -EINVAL;
693 memcpy(&priv->pfunc,
694 &info->platform_callback,
695 sizeof(struct renesas_usbhs_platform_callback));
696 break;
699 /* set driver callback functions for platform */
700 dfunc = &info->driver_callback;
701 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
703 /* set default param if platform doesn't have */
704 if (!priv->dparam.pipe_configs) {
705 priv->dparam.pipe_configs = usbhsc_default_pipe;
706 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
708 if (!priv->dparam.pio_dma_border)
709 priv->dparam.pio_dma_border = 64; /* 64byte */
711 /* FIXME */
712 /* runtime power control ? */
713 if (priv->pfunc.get_vbus)
714 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
717 * priv settings
719 priv->irq = irq_res->start;
720 if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
721 priv->irqflags = IRQF_SHARED;
722 priv->pdev = pdev;
723 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
724 spin_lock_init(usbhs_priv_to_lock(priv));
726 /* call pipe and module init */
727 ret = usbhs_pipe_probe(priv);
728 if (ret < 0)
729 return ret;
731 ret = usbhs_fifo_probe(priv);
732 if (ret < 0)
733 goto probe_end_pipe_exit;
735 ret = usbhs_mod_probe(priv);
736 if (ret < 0)
737 goto probe_end_fifo_exit;
739 /* dev_set_drvdata should be called after usbhs_mod_init */
740 platform_set_drvdata(pdev, priv);
742 ret = reset_control_deassert(priv->rsts);
743 if (ret)
744 goto probe_fail_rst;
746 ret = usbhsc_clk_get(&pdev->dev, priv);
747 if (ret)
748 goto probe_fail_clks;
751 * deviece reset here because
752 * USB device might be used in boot loader.
754 usbhs_sys_clock_ctrl(priv, 0);
756 /* check GPIO determining if USB function should be enabled */
757 if (priv->dparam.enable_gpio) {
758 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
759 ret = !gpio_get_value(priv->dparam.enable_gpio);
760 gpio_free(priv->dparam.enable_gpio);
761 if (ret) {
762 dev_warn(&pdev->dev,
763 "USB function not selected (GPIO %d)\n",
764 priv->dparam.enable_gpio);
765 ret = -ENOTSUPP;
766 goto probe_end_mod_exit;
771 * platform call
773 * USB phy setup might depend on CPU/Board.
774 * If platform has its callback functions,
775 * call it here.
777 ret = usbhs_platform_call(priv, hardware_init, pdev);
778 if (ret < 0) {
779 dev_err(&pdev->dev, "platform init failed.\n");
780 goto probe_end_mod_exit;
783 /* reset phy for connection */
784 usbhs_platform_call(priv, phy_reset, pdev);
786 /* power control */
787 pm_runtime_enable(&pdev->dev);
788 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
789 usbhsc_power_ctrl(priv, 1);
790 usbhs_mod_autonomy_mode(priv);
794 * manual call notify_hotplug for cold plug
796 usbhsc_drvcllbck_notify_hotplug(pdev);
798 dev_info(&pdev->dev, "probed\n");
800 return ret;
802 probe_end_mod_exit:
803 usbhsc_clk_put(priv);
804 probe_fail_clks:
805 reset_control_assert(priv->rsts);
806 probe_fail_rst:
807 usbhs_mod_remove(priv);
808 probe_end_fifo_exit:
809 usbhs_fifo_remove(priv);
810 probe_end_pipe_exit:
811 usbhs_pipe_remove(priv);
813 dev_info(&pdev->dev, "probe failed (%d)\n", ret);
815 return ret;
818 static int usbhs_remove(struct platform_device *pdev)
820 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
821 struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
822 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
824 dev_dbg(&pdev->dev, "usb remove\n");
826 dfunc->notify_hotplug = NULL;
828 /* power off */
829 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
830 usbhsc_power_ctrl(priv, 0);
832 pm_runtime_disable(&pdev->dev);
834 usbhs_platform_call(priv, hardware_exit, pdev);
835 usbhsc_clk_put(priv);
836 reset_control_assert(priv->rsts);
837 usbhs_mod_remove(priv);
838 usbhs_fifo_remove(priv);
839 usbhs_pipe_remove(priv);
841 return 0;
844 static int usbhsc_suspend(struct device *dev)
846 struct usbhs_priv *priv = dev_get_drvdata(dev);
847 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
849 if (mod) {
850 usbhs_mod_call(priv, stop, priv);
851 usbhs_mod_change(priv, -1);
854 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
855 usbhsc_power_ctrl(priv, 0);
857 return 0;
860 static int usbhsc_resume(struct device *dev)
862 struct usbhs_priv *priv = dev_get_drvdata(dev);
863 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
865 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
866 usbhsc_power_ctrl(priv, 1);
867 usbhs_mod_autonomy_mode(priv);
870 usbhs_platform_call(priv, phy_reset, pdev);
872 usbhsc_drvcllbck_notify_hotplug(pdev);
874 return 0;
877 static int usbhsc_runtime_nop(struct device *dev)
879 /* Runtime PM callback shared between ->runtime_suspend()
880 * and ->runtime_resume(). Simply returns success.
882 * This driver re-initializes all registers after
883 * pm_runtime_get_sync() anyway so there is no need
884 * to save and restore registers here.
886 return 0;
889 static const struct dev_pm_ops usbhsc_pm_ops = {
890 .suspend = usbhsc_suspend,
891 .resume = usbhsc_resume,
892 .runtime_suspend = usbhsc_runtime_nop,
893 .runtime_resume = usbhsc_runtime_nop,
896 static struct platform_driver renesas_usbhs_driver = {
897 .driver = {
898 .name = "renesas_usbhs",
899 .pm = &usbhsc_pm_ops,
900 .of_match_table = of_match_ptr(usbhs_of_match),
902 .probe = usbhs_probe,
903 .remove = usbhs_remove,
906 module_platform_driver(renesas_usbhs_driver);
908 MODULE_LICENSE("GPL");
909 MODULE_DESCRIPTION("Renesas USB driver");
910 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");