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