usb: dwc_otg_310: support vbus controlled by both gpio and pmic
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk3368.c
1 #ifdef CONFIG_ARM64
2 #include "usbdev_rk.h"
3 #include "dwc_otg_regs.h"
4
5 static struct dwc_otg_control_usb *control_usb;
6
7 static u32 uoc_read(u32 reg)
8 {
9         unsigned int val;
10
11         regmap_read(control_usb->grf, reg, &val);
12         return val;
13 }
14
15 static void uoc_write(u32 value, u32 reg)
16 {
17         regmap_write(control_usb->grf, reg, value);
18 }
19
20 #ifdef CONFIG_USB20_OTG
21 static void usb20otg_hw_init(void)
22 {
23         /* Turn off differential receiver in suspend mode */
24         uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x798);
25
26         /* Set disconnect detection trigger point to 625mv */
27         uoc_write(UOC_HIWORD_UPDATE(0x9, 0xf, 11), 0x79c);
28
29         /* other haredware init,include:
30          * DRV_VBUS GPIO init */
31         if (gpio_is_valid(control_usb->otg_gpios->gpio))
32                 gpio_set_value(control_usb->otg_gpios->gpio, 0);
33 }
34
35 static void usb20otg_phy_suspend(void *pdata, int suspend)
36 {
37         struct dwc_otg_platform_data *usbpdata = pdata;
38         if (suspend) {
39                 /* enable soft control */
40                 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x700);
41                 usbpdata->phy_status = 1;
42         } else {
43                 /* exit suspend */
44                 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x700);
45                 usbpdata->phy_status = 0;
46         }
47 }
48
49 static void usb20otg_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
50 {
51         struct dwc_otg_platform_data *usbpdata = pdata;
52         struct reset_control *rst_otg_h, *rst_otg_p, *rst_otg_c;
53
54         rst_otg_h = devm_reset_control_get(usbpdata->dev, "otg_ahb");
55         rst_otg_p = devm_reset_control_get(usbpdata->dev, "otg_phy");
56         rst_otg_c = devm_reset_control_get(usbpdata->dev, "otg_controller");
57         if (IS_ERR(rst_otg_h) || IS_ERR(rst_otg_p) || IS_ERR(rst_otg_c)) {
58                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
59                 return;
60         }
61
62         switch (rst_type) {
63         case RST_POR:
64                 /* PHY reset */
65                 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x700);
66                 reset_control_assert(rst_otg_p);
67                 udelay(15);
68                 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x700);
69                 udelay(1500);
70                 reset_control_deassert(rst_otg_p);
71                 udelay(2);
72
73                 /* Controller reset */
74                 reset_control_assert(rst_otg_c);
75                 reset_control_assert(rst_otg_h);
76
77                 udelay(2);
78
79                 reset_control_deassert(rst_otg_c);
80                 reset_control_deassert(rst_otg_h);
81                 break;
82         case RST_CHN_HALT:
83                 /* PHY reset */
84                 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x700);
85                 reset_control_assert(rst_otg_p);
86                 udelay(15);
87                 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x700);
88                 udelay(1500);
89                 reset_control_deassert(rst_otg_p);
90                 udelay(2);
91                 break;
92         default:
93                 break;
94         }
95 }
96
97 static void usb20otg_clock_init(void *pdata)
98 {
99         struct dwc_otg_platform_data *usbpdata = pdata;
100         struct clk *ahbclk, *phyclk;
101
102         ahbclk = devm_clk_get(usbpdata->dev, "otg");
103         if (IS_ERR(ahbclk)) {
104                 dev_err(usbpdata->dev, "Failed to get otg clk\n");
105                 return;
106         }
107
108         phyclk = devm_clk_get(usbpdata->dev, "sclk_otgphy0");
109         if (IS_ERR(phyclk)) {
110                 dev_err(usbpdata->dev, "Failed to get sclk_otgphy0\n");
111                 return;
112         }
113
114         usbpdata->phyclk = phyclk;
115         usbpdata->ahbclk = ahbclk;
116 }
117
118 static void usb20otg_clock_enable(void *pdata, int enable)
119 {
120         struct dwc_otg_platform_data *usbpdata = pdata;
121
122         if (enable) {
123                 clk_prepare_enable(usbpdata->ahbclk);
124                 clk_prepare_enable(usbpdata->phyclk);
125         } else {
126                 clk_disable_unprepare(usbpdata->ahbclk);
127                 clk_disable_unprepare(usbpdata->phyclk);
128         }
129 }
130
131 static int usb20otg_get_status(int id)
132 {
133         int ret = -1;
134         u32 soc_status15 = uoc_read(control_usb->grf_otg_st_offset);
135
136         switch (id) {
137         case USB_STATUS_BVABLID:
138                 /* bvalid in grf */
139                 ret = soc_status15 & (0x1 << 23);
140                 break;
141         case USB_STATUS_DPDM:
142                 /* dpdm in grf */
143                 ret = soc_status15 & (0x3 << 24);
144                 break;
145         case USB_STATUS_ID:
146                 /* id in grf */
147                 ret = soc_status15 & (0x1 << 26);
148                 break;
149         case USB_CHIP_ID:
150                 ret = control_usb->chip_id;
151                 break;
152         case USB_REMOTE_WAKEUP:
153                 ret = control_usb->remote_wakeup;
154                 break;
155         case USB_IRQ_WAKEUP:
156                 ret = control_usb->usb_irq_wakeup;
157                 break;
158         default:
159                 break;
160         }
161         return ret;
162 }
163
164 #ifdef CONFIG_RK_USB_UART
165 /**
166  *  dwc_otg_uart_enabled - check if a usb-uart bypass func is enabled in DT
167  *
168  *  Returns true if the status property of node "usb_uart" is set to "okay"
169  *  or "ok", if this property is absent it will use the default status "ok"
170  *  0 otherwise
171  */
172 static bool dwc_otg_uart_enabled(void)
173 {
174         return false;
175 }
176
177 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
178 {
179 }
180 #else
181 static void dwc_otg_uart_mode(void *pdata, int enter_usb_uart_mode)
182 {
183 }
184 #endif
185
186 static void usb20otg_power_enable(int enable)
187 {
188         if (0 == enable) {
189                 /* disable otg_drv power */
190                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
191                         gpio_set_value(control_usb->otg_gpios->gpio, 0);
192
193                 rk_battery_charger_detect_cb(USB_OTG_POWER_OFF);
194         } else if (1 == enable) {
195                 /* enable otg_drv power */
196                 if (gpio_is_valid(control_usb->otg_gpios->gpio))
197                         gpio_set_value(control_usb->otg_gpios->gpio, 1);
198
199                 if (!usb20otg_get_status(USB_STATUS_BVABLID))
200                         rk_battery_charger_detect_cb(USB_OTG_POWER_ON);
201         }
202 }
203
204 struct dwc_otg_platform_data usb20otg_pdata_rk3368 = {
205         .phyclk = NULL,
206         .ahbclk = NULL,
207         .busclk = NULL,
208         .phy_status = 0,
209         .hw_init = usb20otg_hw_init,
210         .phy_suspend = usb20otg_phy_suspend,
211         .soft_reset = usb20otg_soft_reset,
212         .clock_init = usb20otg_clock_init,
213         .clock_enable = usb20otg_clock_enable,
214         .get_status = usb20otg_get_status,
215         .power_enable = usb20otg_power_enable,
216         .dwc_otg_uart_mode = dwc_otg_uart_mode,
217         .bc_detect_cb = rk_battery_charger_detect_cb,
218 };
219 #endif
220
221 #ifdef CONFIG_USB_EHCI_RK
222 static void usb20ehci_hw_init(void)
223 {
224         /* Turn off differential receiver in suspend mode */
225         uoc_write(UOC_HIWORD_UPDATE(0, 1, 2), 0x7b8);
226         /* Set disconnect detection trigger point to 625mv */
227         uoc_write(UOC_HIWORD_UPDATE(0x9, 0xf, 11), 0x7bc);
228
229         /* other haredware init,include:
230          * DRV_VBUS GPIO init */
231         if (gpio_is_valid(control_usb->host_gpios->gpio)) {
232                 if (!gpio_get_value(control_usb->host_gpios->gpio))
233                         gpio_set_value(control_usb->host_gpios->gpio, 1);
234         }
235 }
236
237 static void usb20ehci_phy_suspend(void *pdata, int suspend)
238 {
239         struct rkehci_platform_data *usbpdata = pdata;
240
241         if (suspend) {
242                 /* enable soft control */
243                 uoc_write(UOC_HIWORD_UPDATE(0x1d5, 0x1ff, 0), 0x728);
244                 usbpdata->phy_status = 1;
245         } else {
246                 /* exit suspend */
247                 uoc_write(UOC_HIWORD_UPDATE(0x0, 0x1, 0), 0x728);
248                 usbpdata->phy_status = 0;
249         }
250 }
251
252 static void usb20ehci_soft_reset(void *pdata, enum rkusb_rst_flag rst_type)
253 {
254         struct rkehci_platform_data *usbpdata = pdata;
255         struct reset_control *rst_host_h, *rst_host_p, *rst_host_c;
256
257         rst_host_h = devm_reset_control_get(usbpdata->dev, "host_ahb");
258         rst_host_p = devm_reset_control_get(usbpdata->dev, "host_phy");
259         rst_host_c = devm_reset_control_get(usbpdata->dev, "host_controller");
260         if (IS_ERR(rst_host_h) || IS_ERR(rst_host_p) || IS_ERR(rst_host_c)) {
261                 dev_err(usbpdata->dev, "Fail to get reset control from dts\n");
262                 return;
263         }
264
265         switch (rst_type) {
266         case RST_POR:
267                 /* PHY reset */
268                 uoc_write(UOC_HIWORD_UPDATE(0x1, 0x3, 0), 0x728);
269                 reset_control_assert(rst_host_p);
270                 udelay(15);
271                 uoc_write(UOC_HIWORD_UPDATE(0x2, 0x3, 0), 0x728);
272
273                 udelay(1500);
274                 reset_control_deassert(rst_host_p);
275
276                 /* Controller reset */
277                 reset_control_assert(rst_host_c);
278                 reset_control_assert(rst_host_h);
279
280                 udelay(5);
281
282                 reset_control_deassert(rst_host_c);
283                 reset_control_deassert(rst_host_h);
284                 break;
285
286         default:
287                 break;
288         }
289 }
290
291 static void usb20ehci_clock_init(void *pdata)
292 {
293 }
294
295 static void usb20ehci_clock_enable(void *pdata, int enable)
296 {
297 }
298
299 static int usb20ehci_get_status(int id)
300 {
301         /* For HOST port in rk336x can not get any info from GRF */
302         return -ENOENT;
303 }
304
305 struct rkehci_platform_data usb20ehci_pdata_rk3368 = {
306         .phyclk = NULL,
307         .ahbclk = NULL,
308         .phy_status = 0,
309         .hw_init = usb20ehci_hw_init,
310         .phy_suspend = usb20ehci_phy_suspend,
311         .soft_reset = usb20ehci_soft_reset,
312         .clock_init = usb20ehci_clock_init,
313         .clock_enable = usb20ehci_clock_enable,
314         .get_status = usb20ehci_get_status,
315 };
316 #endif
317
318 struct dwc_otg_platform_data usb20ohci_pdata_rk3368;
319
320 #ifdef CONFIG_OF
321 static const struct of_device_id rk_usb_control_id_table[] = {
322         {
323          .compatible = "rockchip,rk3368-usb-control",
324          },
325         {},
326 };
327 #endif
328 /*********************************************************************
329                         rk3126 usb detections
330 *********************************************************************/
331
332 #define WAKE_LOCK_TIMEOUT (HZ * 10)
333 static inline void do_wakeup(struct work_struct *work)
334 {
335         /* wake up the system */
336         rk_send_wakeup_key();
337 }
338
339 static void usb_battery_charger_detect_work(struct work_struct *work)
340 {
341         rk_battery_charger_detect_cb(usb_battery_charger_detect(1));
342 }
343
344 /********** handler for bvalid irq **********/
345 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
346 {
347         /* clear irq */
348         uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x6a0);
349 #ifdef CONFIG_RK_USB_UART
350         /* usb otg dp/dm switch to usb phy */
351         dwc_otg_uart_mode(NULL, PHY_USB_MODE);
352 #endif
353
354         if (control_usb->usb_irq_wakeup) {
355                 wake_lock_timeout(&control_usb->usb_wakelock,
356                                   WAKE_LOCK_TIMEOUT);
357                 schedule_delayed_work(&control_usb->usb_det_wakeup_work,
358                                       HZ / 10);
359         }
360
361         schedule_delayed_work(&control_usb->usb_charger_det_work, HZ / 10);
362
363         return IRQ_HANDLED;
364 }
365
366 /************* register usb detection irqs **************/
367 static int otg_irq_detect_init(struct platform_device *pdev)
368 {
369         int ret = 0;
370         int irq = 0;
371
372         wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND,
373                        "usb_detect");
374         INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
375
376         /*register otg_bvalid irq */
377         irq = platform_get_irq_byname(pdev, "otg_bvalid");
378         if ((irq > 0) && control_usb->usb_irq_wakeup) {
379                 ret = request_irq(irq, bvalid_irq_handler,
380                                   0, "otg_bvalid", NULL);
381                 if (ret < 0) {
382                         dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
383                 } else {
384                         /* enable bvalid irq  */
385                         uoc_write(UOC_HIWORD_UPDATE(0x1, 0x1, 3), 0x680);
386                 }
387         }
388
389         return 0;
390 }
391
392 /********** end of usb detections **********/
393 #ifdef CONFIG_OF
394 static const struct of_device_id dwc_otg_control_usb_id_table[] = {
395         {
396          .compatible = "rockchip,rk3368-dwc-control-usb",
397          },
398         {},
399 };
400 #endif
401 static int dwc_otg_control_usb_probe(struct platform_device *pdev)
402 {
403         struct device *dev = &pdev->dev;
404         struct device_node *np = dev->of_node;
405         struct clk *hclk_usb_peri;
406         struct regmap *grf;
407         int gpio, ret = 0;
408         u32 offset;
409
410         control_usb = devm_kzalloc(dev, sizeof(*control_usb), GFP_KERNEL);
411         if (!control_usb) {
412                 dev_err(&pdev->dev, "Unable to alloc memory for control usb\n");
413                 return -ENOMEM;
414         }
415
416         /* Init regmap GRF */
417         grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
418         if (IS_ERR(grf)) {
419                 dev_err(&pdev->dev, "Missing rockchip,grf property\n");
420                 return PTR_ERR(grf);
421         }
422         control_usb->grf = grf;
423
424         /* get the reg offset of GRF_SOC_STATUS for USB2.0 OTG */
425         if (of_property_read_u32(np, "grf-offset", &offset)) {
426                 dev_err(&pdev->dev, "missing reg property in node %s\n",
427                         np->name);
428                 return -EINVAL;
429         }
430         control_usb->grf_otg_st_offset = offset;
431
432         /* Init Vbus-drv GPIOs */
433         control_usb->host_gpios =
434             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
435         if (!control_usb->host_gpios) {
436                 dev_err(&pdev->dev, "Unable to alloc memory for host_gpios\n");
437                 return -ENOMEM;
438         }
439
440         gpio = of_get_named_gpio(np, "host_drv_gpio", 0);
441         control_usb->host_gpios->gpio = gpio;
442
443         if (gpio_is_valid(gpio)) {
444                 if (devm_gpio_request(&pdev->dev, gpio, "usb_host_drv")) {
445                         dev_err(&pdev->dev,
446                                 "Failed to request GPIO%d for host_drv\n",
447                                 gpio);
448                         return -EINVAL;
449                 }
450                 gpio_direction_output(control_usb->host_gpios->gpio, 1);
451         }
452
453         control_usb->otg_gpios =
454             devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
455         if (!control_usb->otg_gpios) {
456                 dev_err(&pdev->dev, "Unable to alloc memory for otg_gpios\n");
457                 return -ENOMEM;
458         }
459
460         gpio = of_get_named_gpio(np, "otg_drv_gpio", 0);
461         control_usb->otg_gpios->gpio = gpio;
462
463         if (gpio_is_valid(gpio)) {
464                 if (devm_gpio_request(&pdev->dev, gpio, "usb_otg_drv")) {
465                         dev_err(&pdev->dev,
466                                 "failed to request GPIO%d for otg_drv\n", gpio);
467                         return -EINVAL;
468                 }
469                 gpio_direction_output(control_usb->otg_gpios->gpio, 0);
470         }
471
472
473         control_usb->remote_wakeup = of_property_read_bool(np,
474                                                            "rockchip,remote_wakeup");
475         control_usb->usb_irq_wakeup = of_property_read_bool(np,
476                                                             "rockchip,usb_irq_wakeup");
477
478         /* Init hclk_usb_peri */
479         hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
480         if (IS_ERR(hclk_usb_peri)) {
481                 dev_info(&pdev->dev, "no hclk_usb_peri clk specified\n");
482                 hclk_usb_peri = NULL;
483         }
484         control_usb->hclk_usb_peri = hclk_usb_peri;
485         clk_prepare_enable(hclk_usb_peri);
486
487 #ifdef CONFIG_USB20_OTG
488         INIT_DELAYED_WORK(&control_usb->usb_charger_det_work,
489                           usb_battery_charger_detect_work);
490
491         if (usb20otg_get_status(USB_STATUS_BVABLID))
492                 schedule_delayed_work(&control_usb->usb_charger_det_work,
493                                       HZ / 10);
494 #endif
495
496         ret = otg_irq_detect_init(pdev);
497         if (ret < 0)
498                 goto err;
499
500         return 0;
501
502 err:
503         clk_disable_unprepare(hclk_usb_peri);
504         return ret;
505 }
506
507 static int dwc_otg_control_usb_remove(struct platform_device *pdev)
508 {
509         clk_disable_unprepare(control_usb->hclk_usb_peri);
510         return 0;
511 }
512
513 static struct platform_driver dwc_otg_control_usb_driver = {
514         .probe = dwc_otg_control_usb_probe,
515         .remove = dwc_otg_control_usb_remove,
516         .driver = {
517                    .name = "rk3368-dwc-control-usb",
518                    .owner = THIS_MODULE,
519                    .of_match_table = of_match_ptr(dwc_otg_control_usb_id_table),
520                    },
521 };
522
523 static int __init dwc_otg_control_usb_init(void)
524 {
525         int retval = 0;
526
527         retval |= platform_driver_register(&dwc_otg_control_usb_driver);
528         return retval;
529 }
530
531 subsys_initcall(dwc_otg_control_usb_init);
532
533 static void __exit dwc_otg_control_usb_exit(void)
534 {
535         platform_driver_unregister(&dwc_otg_control_usb_driver);
536 }
537
538 module_exit(dwc_otg_control_usb_exit);
539 MODULE_ALIAS("platform: dwc_control_usb");
540 MODULE_AUTHOR("RockChip Inc.");
541 MODULE_DESCRIPTION("RockChip Control Module USB Driver");
542 MODULE_LICENSE("GPL v2");
543 #endif