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