usb: renesas_usbhs: Use dev_of_node macro instead of open coded
[platform/kernel/linux-starfive.git] / drivers / usb / renesas_usbhs / common.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * Renesas USB driver
4  *
5  * Copyright (C) 2011 Renesas Solutions Corp.
6  * Copyright (C) 2019 Renesas Electronics Corporation
7  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8  */
9 #include <linux/clk.h>
10 #include <linux/err.h>
11 #include <linux/gpio.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_gpio.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include "common.h"
21 #include "rcar2.h"
22 #include "rcar3.h"
23 #include "rza.h"
24
25 /*
26  *              image of renesas_usbhs
27  *
28  * ex) gadget case
29
30  * mod.c
31  * mod_gadget.c
32  * mod_host.c           pipe.c          fifo.c
33  *
34  *                      +-------+       +-----------+
35  *                      | pipe0 |------>| fifo pio  |
36  * +------------+       +-------+       +-----------+
37  * | mod_gadget |=====> | pipe1 |--+
38  * +------------+       +-------+  |    +-----------+
39  *                      | pipe2 |  |  +-| fifo dma0 |
40  * +------------+       +-------+  |  | +-----------+
41  * | mod_host   |       | pipe3 |<-|--+
42  * +------------+       +-------+  |    +-----------+
43  *                      | ....  |  +--->| fifo dma1 |
44  *                      | ....  |       +-----------+
45  */
46
47 /*
48  * platform call back
49  *
50  * renesas usb support platform callback function.
51  * Below macro call it.
52  * if platform doesn't have callback, it return 0 (no error)
53  */
54 #define usbhs_platform_call(priv, func, args...)\
55         (!(priv) ? -ENODEV :                    \
56          !((priv)->pfunc.func) ? 0 :            \
57          (priv)->pfunc.func(args))
58
59 /*
60  *              common functions
61  */
62 u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
63 {
64         return ioread16(priv->base + reg);
65 }
66
67 void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
68 {
69         iowrite16(data, priv->base + reg);
70 }
71
72 void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
73 {
74         u16 val = usbhs_read(priv, reg);
75
76         val &= ~mask;
77         val |= data & mask;
78
79         usbhs_write(priv, reg, val);
80 }
81
82 struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
83 {
84         return dev_get_drvdata(&pdev->dev);
85 }
86
87 /*
88  *              syscfg functions
89  */
90 static void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
91 {
92         usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
93 }
94
95 void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
96 {
97         u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
98         u16 val  = DCFM | DRPD | HSE | USBE;
99
100         /*
101          * if enable
102          *
103          * - select Host mode
104          * - D+ Line/D- Line Pull-down
105          */
106         usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
107 }
108
109 void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
110 {
111         u16 mask = DCFM | DRPD | DPRPU | HSE | USBE;
112         u16 val  = HSE | USBE;
113
114         /* CNEN bit is required for function operation */
115         if (usbhs_get_dparam(priv, has_cnen)) {
116                 mask |= CNEN;
117                 val  |= CNEN;
118         }
119
120         /*
121          * if enable
122          *
123          * - select Function mode
124          * - D+ Line Pull-up is disabled
125          *      When D+ Line Pull-up is enabled,
126          *      calling usbhs_sys_function_pullup(,1)
127          */
128         usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
129 }
130
131 void usbhs_sys_function_pullup(struct usbhs_priv *priv, int enable)
132 {
133         usbhs_bset(priv, SYSCFG, DPRPU, enable ? DPRPU : 0);
134 }
135
136 void usbhs_sys_set_test_mode(struct usbhs_priv *priv, u16 mode)
137 {
138         usbhs_write(priv, TESTMODE, mode);
139 }
140
141 /*
142  *              frame functions
143  */
144 int usbhs_frame_get_num(struct usbhs_priv *priv)
145 {
146         return usbhs_read(priv, FRMNUM) & FRNM_MASK;
147 }
148
149 /*
150  *              usb request functions
151  */
152 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
153 {
154         u16 val;
155
156         val = usbhs_read(priv, USBREQ);
157         req->bRequest           = (val >> 8) & 0xFF;
158         req->bRequestType       = (val >> 0) & 0xFF;
159
160         req->wValue     = usbhs_read(priv, USBVAL);
161         req->wIndex     = usbhs_read(priv, USBINDX);
162         req->wLength    = usbhs_read(priv, USBLENG);
163 }
164
165 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
166 {
167         usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
168         usbhs_write(priv, USBVAL,  req->wValue);
169         usbhs_write(priv, USBINDX, req->wIndex);
170         usbhs_write(priv, USBLENG, req->wLength);
171
172         usbhs_bset(priv, DCPCTR, SUREQ, SUREQ);
173 }
174
175 /*
176  *              bus/vbus functions
177  */
178 void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
179 {
180         u16 status = usbhs_read(priv, DVSTCTR) & (USBRST | UACT);
181
182         if (status != USBRST) {
183                 struct device *dev = usbhs_priv_to_dev(priv);
184                 dev_err(dev, "usbhs should be reset\n");
185         }
186
187         usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
188 }
189
190 void usbhs_bus_send_reset(struct usbhs_priv *priv)
191 {
192         usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
193 }
194
195 int usbhs_bus_get_speed(struct usbhs_priv *priv)
196 {
197         u16 dvstctr = usbhs_read(priv, DVSTCTR);
198
199         switch (RHST & dvstctr) {
200         case RHST_LOW_SPEED:
201                 return USB_SPEED_LOW;
202         case RHST_FULL_SPEED:
203                 return USB_SPEED_FULL;
204         case RHST_HIGH_SPEED:
205                 return USB_SPEED_HIGH;
206         }
207
208         return USB_SPEED_UNKNOWN;
209 }
210
211 int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
212 {
213         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
214
215         return usbhs_platform_call(priv, set_vbus, pdev, enable);
216 }
217
218 static void usbhsc_bus_init(struct usbhs_priv *priv)
219 {
220         usbhs_write(priv, DVSTCTR, 0);
221
222         usbhs_vbus_ctrl(priv, 0);
223 }
224
225 /*
226  *              device configuration
227  */
228 int usbhs_set_device_config(struct usbhs_priv *priv, int devnum,
229                            u16 upphub, u16 hubport, u16 speed)
230 {
231         struct device *dev = usbhs_priv_to_dev(priv);
232         u16 usbspd = 0;
233         u32 reg = DEVADD0 + (2 * devnum);
234
235         if (devnum > 10) {
236                 dev_err(dev, "cannot set speed to unknown device %d\n", devnum);
237                 return -EIO;
238         }
239
240         if (upphub > 0xA) {
241                 dev_err(dev, "unsupported hub number %d\n", upphub);
242                 return -EIO;
243         }
244
245         switch (speed) {
246         case USB_SPEED_LOW:
247                 usbspd = USBSPD_SPEED_LOW;
248                 break;
249         case USB_SPEED_FULL:
250                 usbspd = USBSPD_SPEED_FULL;
251                 break;
252         case USB_SPEED_HIGH:
253                 usbspd = USBSPD_SPEED_HIGH;
254                 break;
255         default:
256                 dev_err(dev, "unsupported speed %d\n", speed);
257                 return -EIO;
258         }
259
260         usbhs_write(priv, reg,  UPPHUB(upphub)  |
261                                 HUBPORT(hubport)|
262                                 USBSPD(usbspd));
263
264         return 0;
265 }
266
267 /*
268  *              interrupt functions
269  */
270 void usbhs_xxxsts_clear(struct usbhs_priv *priv, u16 sts_reg, u16 bit)
271 {
272         u16 pipe_mask = (u16)GENMASK(usbhs_get_dparam(priv, pipe_size), 0);
273
274         usbhs_write(priv, sts_reg, ~(1 << bit) & pipe_mask);
275 }
276
277 /*
278  *              local functions
279  */
280 static void usbhsc_set_buswait(struct usbhs_priv *priv)
281 {
282         int wait = usbhs_get_dparam(priv, buswait_bwait);
283
284         /* set bus wait if platform have */
285         if (wait)
286                 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
287 }
288
289 static bool usbhsc_is_multi_clks(struct usbhs_priv *priv)
290 {
291         return priv->dparam.multi_clks;
292 }
293
294 static int usbhsc_clk_get(struct device *dev, struct usbhs_priv *priv)
295 {
296         if (!usbhsc_is_multi_clks(priv))
297                 return 0;
298
299         /* The first clock should exist */
300         priv->clks[0] = of_clk_get(dev_of_node(dev), 0);
301         if (IS_ERR(priv->clks[0]))
302                 return PTR_ERR(priv->clks[0]);
303
304         /*
305          * To backward compatibility with old DT, this driver checks the return
306          * value if it's -ENOENT or not.
307          */
308         priv->clks[1] = of_clk_get(dev_of_node(dev), 1);
309         if (PTR_ERR(priv->clks[1]) == -ENOENT)
310                 priv->clks[1] = NULL;
311         else if (IS_ERR(priv->clks[1]))
312                 return PTR_ERR(priv->clks[1]);
313
314         return 0;
315 }
316
317 static void usbhsc_clk_put(struct usbhs_priv *priv)
318 {
319         int i;
320
321         if (!usbhsc_is_multi_clks(priv))
322                 return;
323
324         for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
325                 clk_put(priv->clks[i]);
326 }
327
328 static int usbhsc_clk_prepare_enable(struct usbhs_priv *priv)
329 {
330         int i, ret;
331
332         if (!usbhsc_is_multi_clks(priv))
333                 return 0;
334
335         for (i = 0; i < ARRAY_SIZE(priv->clks); i++) {
336                 ret = clk_prepare_enable(priv->clks[i]);
337                 if (ret) {
338                         while (--i >= 0)
339                                 clk_disable_unprepare(priv->clks[i]);
340                         return ret;
341                 }
342         }
343
344         return ret;
345 }
346
347 static void usbhsc_clk_disable_unprepare(struct usbhs_priv *priv)
348 {
349         int i;
350
351         if (!usbhsc_is_multi_clks(priv))
352                 return;
353
354         for (i = 0; i < ARRAY_SIZE(priv->clks); i++)
355                 clk_disable_unprepare(priv->clks[i]);
356 }
357
358 /*
359  *              platform default param
360  */
361
362 /* commonly used on old SH-Mobile SoCs */
363 static struct renesas_usbhs_driver_pipe_config usbhsc_default_pipe[] = {
364         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
365         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, false),
366         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x18, false),
367         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x28, true),
368         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x38, true),
369         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
370         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
371         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
372         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
373         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x07, false),
374 };
375
376 /* commonly used on newer SH-Mobile and R-Car SoCs */
377 static struct renesas_usbhs_driver_pipe_config usbhsc_new_pipe[] = {
378         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_CONTROL, 64, 0x00, false),
379         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x08, true),
380         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_ISOC, 1024, 0x28, true),
381         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x48, true),
382         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x58, true),
383         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x68, true),
384         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x04, false),
385         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x05, false),
386         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_INT, 64, 0x06, false),
387         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x78, true),
388         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x88, true),
389         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0x98, true),
390         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xa8, true),
391         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xb8, true),
392         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xc8, true),
393         RENESAS_USBHS_PIPE(USB_ENDPOINT_XFER_BULK, 512, 0xd8, true),
394 };
395
396 /*
397  *              power control
398  */
399 static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
400 {
401         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
402         struct device *dev = usbhs_priv_to_dev(priv);
403
404         if (enable) {
405                 /* enable PM */
406                 pm_runtime_get_sync(dev);
407
408                 /* enable clks */
409                 if (usbhsc_clk_prepare_enable(priv))
410                         return;
411
412                 /* enable platform power */
413                 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
414
415                 /* USB on */
416                 usbhs_sys_clock_ctrl(priv, enable);
417         } else {
418                 /* USB off */
419                 usbhs_sys_clock_ctrl(priv, enable);
420
421                 /* disable platform power */
422                 usbhs_platform_call(priv, power_ctrl, pdev, priv->base, enable);
423
424                 /* disable clks */
425                 usbhsc_clk_disable_unprepare(priv);
426
427                 /* disable PM */
428                 pm_runtime_put_sync(dev);
429         }
430 }
431
432 /*
433  *              hotplug
434  */
435 static void usbhsc_hotplug(struct usbhs_priv *priv)
436 {
437         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
438         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
439         int id;
440         int enable;
441         int cable;
442         int ret;
443
444         /*
445          * get vbus status from platform
446          */
447         enable = usbhs_mod_info_call(priv, get_vbus, pdev);
448
449         /*
450          * get id from platform
451          */
452         id = usbhs_platform_call(priv, get_id, pdev);
453
454         if (enable && !mod) {
455                 if (priv->edev) {
456                         cable = extcon_get_state(priv->edev, EXTCON_USB_HOST);
457                         if ((cable > 0 && id != USBHS_HOST) ||
458                             (!cable && id != USBHS_GADGET)) {
459                                 dev_info(&pdev->dev,
460                                          "USB cable plugged in doesn't match the selected role!\n");
461                                 return;
462                         }
463                 }
464
465                 ret = usbhs_mod_change(priv, id);
466                 if (ret < 0)
467                         return;
468
469                 dev_dbg(&pdev->dev, "%s enable\n", __func__);
470
471                 /* power on */
472                 if (usbhs_get_dparam(priv, runtime_pwctrl))
473                         usbhsc_power_ctrl(priv, enable);
474
475                 /* bus init */
476                 usbhsc_set_buswait(priv);
477                 usbhsc_bus_init(priv);
478
479                 /* module start */
480                 usbhs_mod_call(priv, start, priv);
481
482         } else if (!enable && mod) {
483                 dev_dbg(&pdev->dev, "%s disable\n", __func__);
484
485                 /* module stop */
486                 usbhs_mod_call(priv, stop, priv);
487
488                 /* bus init */
489                 usbhsc_bus_init(priv);
490
491                 /* power off */
492                 if (usbhs_get_dparam(priv, runtime_pwctrl))
493                         usbhsc_power_ctrl(priv, enable);
494
495                 usbhs_mod_change(priv, -1);
496
497                 /* reset phy for next connection */
498                 usbhs_platform_call(priv, phy_reset, pdev);
499         }
500 }
501
502 /*
503  *              notify hotplug
504  */
505 static void usbhsc_notify_hotplug(struct work_struct *work)
506 {
507         struct usbhs_priv *priv = container_of(work,
508                                                struct usbhs_priv,
509                                                notify_hotplug_work.work);
510         usbhsc_hotplug(priv);
511 }
512
513 int usbhsc_schedule_notify_hotplug(struct platform_device *pdev)
514 {
515         struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
516         int delay = usbhs_get_dparam(priv, detection_delay);
517
518         /*
519          * This functions will be called in interrupt.
520          * To make sure safety context,
521          * use workqueue for usbhs_notify_hotplug
522          */
523         schedule_delayed_work(&priv->notify_hotplug_work,
524                               msecs_to_jiffies(delay));
525         return 0;
526 }
527
528 static const struct usbhs_of_data rcar_gen2_data = {
529         .platform_callback = &usbhs_rcar2_ops,
530         .param = {
531                 .has_usb_dmac = 1,
532                 .pipe_configs = usbhsc_new_pipe,
533                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
534         }
535 };
536
537 static const struct usbhs_of_data rcar_gen3_data = {
538         .platform_callback = &usbhs_rcar3_ops,
539         .param = {
540                 .has_usb_dmac = 1,
541                 .multi_clks = 1,
542                 .pipe_configs = usbhsc_new_pipe,
543                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
544         }
545 };
546
547 static const struct usbhs_of_data rcar_gen3_with_pll_data = {
548         .platform_callback = &usbhs_rcar3_with_pll_ops,
549         .param = {
550                 .has_usb_dmac = 1,
551                 .multi_clks = 1,
552                 .pipe_configs = usbhsc_new_pipe,
553                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
554         }
555 };
556
557 static const struct usbhs_of_data rza1_data = {
558         .platform_callback = &usbhs_rza1_ops,
559         .param = {
560                 .pipe_configs = usbhsc_new_pipe,
561                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
562         }
563 };
564
565 static const struct usbhs_of_data rza2_data = {
566         .platform_callback = &usbhs_rza2_ops,
567         .param = {
568                 .has_cnen = 1,
569                 .cfifo_byte_addr = 1,
570                 .pipe_configs = usbhsc_new_pipe,
571                 .pipe_size = ARRAY_SIZE(usbhsc_new_pipe),
572         }
573 };
574
575 /*
576  *              platform functions
577  */
578 static const struct of_device_id usbhs_of_match[] = {
579         {
580                 .compatible = "renesas,usbhs-r8a774c0",
581                 .data = &rcar_gen3_with_pll_data,
582         },
583         {
584                 .compatible = "renesas,usbhs-r8a7790",
585                 .data = &rcar_gen2_data,
586         },
587         {
588                 .compatible = "renesas,usbhs-r8a7791",
589                 .data = &rcar_gen2_data,
590         },
591         {
592                 .compatible = "renesas,usbhs-r8a7794",
593                 .data = &rcar_gen2_data,
594         },
595         {
596                 .compatible = "renesas,usbhs-r8a7795",
597                 .data = &rcar_gen3_data,
598         },
599         {
600                 .compatible = "renesas,usbhs-r8a7796",
601                 .data = &rcar_gen3_data,
602         },
603         {
604                 .compatible = "renesas,usbhs-r8a77990",
605                 .data = &rcar_gen3_with_pll_data,
606         },
607         {
608                 .compatible = "renesas,usbhs-r8a77995",
609                 .data = &rcar_gen3_with_pll_data,
610         },
611         {
612                 .compatible = "renesas,rcar-gen2-usbhs",
613                 .data = &rcar_gen2_data,
614         },
615         {
616                 .compatible = "renesas,rcar-gen3-usbhs",
617                 .data = &rcar_gen3_data,
618         },
619         {
620                 .compatible = "renesas,rza1-usbhs",
621                 .data = &rza1_data,
622         },
623         {
624                 .compatible = "renesas,rza2-usbhs",
625                 .data = &rza2_data,
626         },
627         { },
628 };
629 MODULE_DEVICE_TABLE(of, usbhs_of_match);
630
631 static struct renesas_usbhs_platform_info *usbhs_parse_dt(struct device *dev)
632 {
633         struct renesas_usbhs_platform_info *info;
634         struct renesas_usbhs_driver_param *dparam;
635         const struct usbhs_of_data *data;
636         u32 tmp;
637         int gpio;
638
639         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
640         if (!info)
641                 return NULL;
642
643         data = of_device_get_match_data(dev);
644         if (!data)
645                 return NULL;
646
647         dparam = &info->driver_param;
648         *dparam = data->param;
649         info->platform_callback = *data->platform_callback;
650
651         if (!of_property_read_u32(dev_of_node(dev), "renesas,buswait", &tmp))
652                 dparam->buswait_bwait = tmp;
653         gpio = of_get_named_gpio_flags(dev_of_node(dev), "renesas,enable-gpio",
654                                        0, NULL);
655         if (gpio > 0)
656                 dparam->enable_gpio = gpio;
657
658         return info;
659 }
660
661 static int usbhs_probe(struct platform_device *pdev)
662 {
663         struct renesas_usbhs_platform_info *info = renesas_usbhs_get_info(pdev);
664         struct usbhs_priv *priv;
665         struct resource *res, *irq_res;
666         int ret;
667
668         /* check device node */
669         if (dev_of_node(&pdev->dev))
670                 info = pdev->dev.platform_data = usbhs_parse_dt(&pdev->dev);
671
672         /* check platform information */
673         if (!info) {
674                 dev_err(&pdev->dev, "no platform information\n");
675                 return -EINVAL;
676         }
677
678         /* platform data */
679         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
680         if (!irq_res) {
681                 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
682                 return -ENODEV;
683         }
684
685         /* usb private data */
686         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
687         if (!priv)
688                 return -ENOMEM;
689
690         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
691         priv->base = devm_ioremap_resource(&pdev->dev, res);
692         if (IS_ERR(priv->base))
693                 return PTR_ERR(priv->base);
694
695         if (of_property_read_bool(dev_of_node(&pdev->dev), "extcon")) {
696                 priv->edev = extcon_get_edev_by_phandle(&pdev->dev, 0);
697                 if (IS_ERR(priv->edev))
698                         return PTR_ERR(priv->edev);
699         }
700
701         priv->rsts = devm_reset_control_array_get_optional_shared(&pdev->dev);
702         if (IS_ERR(priv->rsts))
703                 return PTR_ERR(priv->rsts);
704
705         /*
706          * care platform info
707          */
708
709         priv->dparam = info->driver_param;
710
711         if (!info->platform_callback.get_id) {
712                 dev_err(&pdev->dev, "no platform callbacks");
713                 return -EINVAL;
714         }
715         priv->pfunc = info->platform_callback;
716
717         /* set default param if platform doesn't have */
718         if (!priv->dparam.pipe_configs) {
719                 priv->dparam.pipe_configs = usbhsc_default_pipe;
720                 priv->dparam.pipe_size = ARRAY_SIZE(usbhsc_default_pipe);
721         }
722         if (!priv->dparam.pio_dma_border)
723                 priv->dparam.pio_dma_border = 64; /* 64byte */
724
725         /* FIXME */
726         /* runtime power control ? */
727         if (priv->pfunc.get_vbus)
728                 usbhs_get_dparam(priv, runtime_pwctrl) = 1;
729
730         /*
731          * priv settings
732          */
733         priv->irq       = irq_res->start;
734         if (irq_res->flags & IORESOURCE_IRQ_SHAREABLE)
735                 priv->irqflags = IRQF_SHARED;
736         priv->pdev      = pdev;
737         INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
738         spin_lock_init(usbhs_priv_to_lock(priv));
739
740         /* call pipe and module init */
741         ret = usbhs_pipe_probe(priv);
742         if (ret < 0)
743                 return ret;
744
745         ret = usbhs_fifo_probe(priv);
746         if (ret < 0)
747                 goto probe_end_pipe_exit;
748
749         ret = usbhs_mod_probe(priv);
750         if (ret < 0)
751                 goto probe_end_fifo_exit;
752
753         /* dev_set_drvdata should be called after usbhs_mod_init */
754         platform_set_drvdata(pdev, priv);
755
756         ret = reset_control_deassert(priv->rsts);
757         if (ret)
758                 goto probe_fail_rst;
759
760         ret = usbhsc_clk_get(&pdev->dev, priv);
761         if (ret)
762                 goto probe_fail_clks;
763
764         /*
765          * deviece reset here because
766          * USB device might be used in boot loader.
767          */
768         usbhs_sys_clock_ctrl(priv, 0);
769
770         /* check GPIO determining if USB function should be enabled */
771         if (priv->dparam.enable_gpio) {
772                 gpio_request_one(priv->dparam.enable_gpio, GPIOF_IN, NULL);
773                 ret = !gpio_get_value(priv->dparam.enable_gpio);
774                 gpio_free(priv->dparam.enable_gpio);
775                 if (ret) {
776                         dev_warn(&pdev->dev,
777                                  "USB function not selected (GPIO %d)\n",
778                                  priv->dparam.enable_gpio);
779                         ret = -ENOTSUPP;
780                         goto probe_end_mod_exit;
781                 }
782         }
783
784         /*
785          * platform call
786          *
787          * USB phy setup might depend on CPU/Board.
788          * If platform has its callback functions,
789          * call it here.
790          */
791         ret = usbhs_platform_call(priv, hardware_init, pdev);
792         if (ret < 0) {
793                 dev_err(&pdev->dev, "platform init failed.\n");
794                 goto probe_end_mod_exit;
795         }
796
797         /* reset phy for connection */
798         usbhs_platform_call(priv, phy_reset, pdev);
799
800         /* power control */
801         pm_runtime_enable(&pdev->dev);
802         if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
803                 usbhsc_power_ctrl(priv, 1);
804                 usbhs_mod_autonomy_mode(priv);
805         } else {
806                 usbhs_mod_non_autonomy_mode(priv);
807         }
808
809         /*
810          * manual call notify_hotplug for cold plug
811          */
812         usbhsc_schedule_notify_hotplug(pdev);
813
814         dev_info(&pdev->dev, "probed\n");
815
816         return ret;
817
818 probe_end_mod_exit:
819         usbhsc_clk_put(priv);
820 probe_fail_clks:
821         reset_control_assert(priv->rsts);
822 probe_fail_rst:
823         usbhs_mod_remove(priv);
824 probe_end_fifo_exit:
825         usbhs_fifo_remove(priv);
826 probe_end_pipe_exit:
827         usbhs_pipe_remove(priv);
828
829         dev_info(&pdev->dev, "probe failed (%d)\n", ret);
830
831         return ret;
832 }
833
834 static int usbhs_remove(struct platform_device *pdev)
835 {
836         struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
837
838         dev_dbg(&pdev->dev, "usb remove\n");
839
840         /* power off */
841         if (!usbhs_get_dparam(priv, runtime_pwctrl))
842                 usbhsc_power_ctrl(priv, 0);
843
844         pm_runtime_disable(&pdev->dev);
845
846         usbhs_platform_call(priv, hardware_exit, pdev);
847         usbhsc_clk_put(priv);
848         reset_control_assert(priv->rsts);
849         usbhs_mod_remove(priv);
850         usbhs_fifo_remove(priv);
851         usbhs_pipe_remove(priv);
852
853         return 0;
854 }
855
856 static __maybe_unused int usbhsc_suspend(struct device *dev)
857 {
858         struct usbhs_priv *priv = dev_get_drvdata(dev);
859         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
860
861         if (mod) {
862                 usbhs_mod_call(priv, stop, priv);
863                 usbhs_mod_change(priv, -1);
864         }
865
866         if (mod || !usbhs_get_dparam(priv, runtime_pwctrl))
867                 usbhsc_power_ctrl(priv, 0);
868
869         return 0;
870 }
871
872 static __maybe_unused int usbhsc_resume(struct device *dev)
873 {
874         struct usbhs_priv *priv = dev_get_drvdata(dev);
875         struct platform_device *pdev = usbhs_priv_to_pdev(priv);
876
877         if (!usbhs_get_dparam(priv, runtime_pwctrl)) {
878                 usbhsc_power_ctrl(priv, 1);
879                 usbhs_mod_autonomy_mode(priv);
880         }
881
882         usbhs_platform_call(priv, phy_reset, pdev);
883
884         usbhsc_schedule_notify_hotplug(pdev);
885
886         return 0;
887 }
888
889 static SIMPLE_DEV_PM_OPS(usbhsc_pm_ops, usbhsc_suspend, usbhsc_resume);
890
891 static struct platform_driver renesas_usbhs_driver = {
892         .driver         = {
893                 .name   = "renesas_usbhs",
894                 .pm     = &usbhsc_pm_ops,
895                 .of_match_table = of_match_ptr(usbhs_of_match),
896         },
897         .probe          = usbhs_probe,
898         .remove         = usbhs_remove,
899 };
900
901 module_platform_driver(renesas_usbhs_driver);
902
903 MODULE_LICENSE("GPL");
904 MODULE_DESCRIPTION("Renesas USB driver");
905 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");