USB: Modify EHCI & OHCI clk and gpio.
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / usbdev_rk30.c
index 69bc695969b1cf4132b386497e8a9e0e12bb1410..6fb727f8a56329e53eb75600f948caf8e1e0b429 100755 (executable)
@@ -1,31 +1,10 @@
-#include <linux/kernel.h>
-#include <linux/platform_device.h>
-#include <linux/delay.h>
-#include <linux/dma-mapping.h>
-#include <linux/clk.h>
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/of.h>
-#include <linux/err.h>
-#include <linux/io.h>
-#include <linux/of_gpio.h>
-#include <linux/of_device.h>
-#include <linux/gpio.h>
-#include <linux/wakelock.h>
-#include <linux/workqueue.h>
-#include <linux/interrupt.h>
-#include <linux/irq.h>
-#include "usbdev_grf_regs.h"
+
 #include "usbdev_rk.h"
+#include "usbdev_grf_regs.h"
 #include "dwc_otg_regs.h"
 
 static struct dwc_otg_control_usb *control_usb;
 
-static int usb_get_chip_id(void)
-{
-       return control_usb->chip_id;
-}
-
 #ifdef CONFIG_USB20_OTG
 
 static void usb20otg_hw_init(void)
@@ -42,7 +21,9 @@ static void usb20otg_hw_init(void)
 
        /* other haredware init,include:
         * DRV_VBUS GPIO init */
-       gpio_direction_output(control_usb->otg_gpios->gpio, 0);
+       if(gpio_get_value(control_usb->otg_gpios->gpio)){
+               gpio_set_value(control_usb->otg_gpios->gpio, 0);
+       }
 }
 
 static void usb20otg_phy_suspend(void* pdata, int suspend)
@@ -71,15 +52,15 @@ static void usb20otg_clock_init(void* pdata)
        struct dwc_otg_platform_data *usbpdata=pdata;
        struct clk* ahbclk,*phyclk;
 
-       ahbclk = devm_clk_get(usbpdata->dev, "hclk_otg0");
+       ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb0");
        if (IS_ERR(ahbclk)) {
-               dev_err(usbpdata->dev, "Failed to get hclk_otg0\n");
+               dev_err(usbpdata->dev, "Failed to get hclk_usb0\n");
                return;
        }
 
-       phyclk = devm_clk_get(usbpdata->dev, "otgphy0");
+       phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy0");
        if (IS_ERR(phyclk)) {
-               dev_err(usbpdata->dev, "Failed to get otgphy0\n");
+               dev_err(usbpdata->dev, "Failed to get clk_usbphy0\n");
                return;
        }
 
@@ -117,6 +98,15 @@ static int usb20otg_get_status(int id)
                        // id in grf
                        ret = control_usb->grf_soc_status0_rk3188->otg_iddig;
                        break;
+               case USB_CHIP_ID:
+                       ret = control_usb->chip_id;
+                       break;
+               case USB_REMOTE_WAKEUP:
+                       ret = control_usb->remote_wakeup;
+                       break;
+               case USB_IRQ_WAKEUP:
+                       ret = control_usb->usb_irq_wakeup;
+                       break;
                default:
                        break;
        }
@@ -162,11 +152,11 @@ struct dwc_otg_platform_data usb20otg_pdata_rk3188 = {
        .clock_init = usb20otg_clock_init,
        .clock_enable = usb20otg_clock_enable,
        .get_status = usb20otg_get_status,
-       .get_chip_id = usb_get_chip_id,
        .power_enable = usb20otg_power_enable,
 #ifdef CONFIG_RK_USB_UART
        .dwc_otg_uart_mode = dwc_otg_uart_mode,
 #endif
+    .bc_detect_cb=usb20otg_battery_charger_detect_cb,
 };
 
 #endif
@@ -178,8 +168,9 @@ static void usb20host_hw_init(void)
 
        /* other haredware init,include:
         * DRV_VBUS GPIO init */
-       gpio_direction_output(control_usb->host_gpios->gpio, 1);
-
+       if(!gpio_get_value(control_usb->host_gpios->gpio)){
+               gpio_set_value(control_usb->host_gpios->gpio, 1);
+       }
 }
 
 static void usb20host_phy_suspend(void* pdata, int suspend)
@@ -208,15 +199,15 @@ static void usb20host_clock_init(void* pdata)
        struct dwc_otg_platform_data *usbpdata=pdata;
        struct clk* ahbclk,*phyclk;
 
-       ahbclk = devm_clk_get(usbpdata->dev, "hclk_otg1");
+       ahbclk = devm_clk_get(usbpdata->dev, "hclk_usb1");
        if (IS_ERR(ahbclk)) {
-               dev_err(usbpdata->dev, "Failed to get hclk_otg1\n");
+               dev_err(usbpdata->dev, "Failed to get hclk_usb1\n");
                return;
        }
 
-       phyclk = devm_clk_get(usbpdata->dev, "otgphy1");
+       phyclk = devm_clk_get(usbpdata->dev, "clk_usbphy1");
        if (IS_ERR(phyclk)) {
-               dev_err(usbpdata->dev, "Failed to get otgphy1\n");
+               dev_err(usbpdata->dev, "Failed to get clk_usbphy1\n");
                return;
        }
 
@@ -254,6 +245,15 @@ static int usb20host_get_status(int id)
                        // id in grf
                        ret = control_usb->grf_soc_status0_rk3188->uhost_iddig;
                        break;
+               case USB_CHIP_ID:
+                       ret = control_usb->chip_id;
+                       break;
+               case USB_REMOTE_WAKEUP:
+                       ret = control_usb->remote_wakeup;
+                       break;
+               case USB_IRQ_WAKEUP:
+                       ret = control_usb->usb_irq_wakeup;
+                       break;
                default:
                        break;
        }
@@ -281,7 +281,6 @@ struct dwc_otg_platform_data usb20host_pdata_rk3188 = {
        .clock_init = usb20host_clock_init,
        .clock_enable = usb20host_clock_enable,
        .get_status = usb20host_get_status,
-       .get_chip_id = usb_get_chip_id,
        .power_enable = usb20host_power_enable,
 };
 #endif
@@ -313,23 +312,23 @@ static void rk_hsic_clock_init(void* pdata)
         * to uart mode, otg phy 480MHz clk will be closed automatically
         */
        struct rkehci_platform_data *usbpdata=pdata;
-       struct clk *ahbclk, *phyclk480m_hsic, *phyclk12m_hsic, *phyclk_otgphy1;
+       struct clk *ahbclk, *phyclk480m_hsic, *phyclk12m_hsic, *phyclk_usbphy1;
 
-       phyclk480m_hsic = devm_clk_get(usbpdata->dev, "hsicphy480m");
+       phyclk480m_hsic = devm_clk_get(usbpdata->dev, "hsicphy_480m");
        if (IS_ERR(phyclk480m_hsic)) {
-               dev_err(usbpdata->dev, "Failed to get hsicphy480m\n");
+               dev_err(usbpdata->dev, "Failed to get hsicphy_480m\n");
                return;
        }
 
-       phyclk12m_hsic = devm_clk_get(usbpdata->dev, "hsicphy12m");
+       phyclk12m_hsic = devm_clk_get(usbpdata->dev, "hsicphy_12m");
        if (IS_ERR(phyclk12m_hsic)) {
-               dev_err(usbpdata->dev, "Failed to get hsicphy12m\n");
+               dev_err(usbpdata->dev, "Failed to get hsicphy_12m\n");
                return;
        }
 
-       phyclk_otgphy1 = devm_clk_get(usbpdata->dev, "hsic_otgphy1");
-       if (IS_ERR(phyclk_otgphy1)) {
-               dev_err(usbpdata->dev, "Failed to get hsic_otgphy1\n");
+       phyclk_usbphy1 = devm_clk_get(usbpdata->dev, "hsic_usbphy1");
+       if (IS_ERR(phyclk_usbphy1)) {
+               dev_err(usbpdata->dev, "Failed to get hsic_usbphy1\n");
                return;
        }
 
@@ -339,7 +338,7 @@ static void rk_hsic_clock_init(void* pdata)
                return;
        }
 
-       clk_set_parent(phyclk480m_hsic, phyclk_otgphy1);
+       clk_set_parent(phyclk480m_hsic, phyclk_usbphy1);
        
        usbpdata->hclk_hsic = ahbclk;
        usbpdata->hsic_phy_480m = phyclk480m_hsic;
@@ -379,18 +378,15 @@ struct rkehci_platform_data rkhsic_pdata_rk3188 = {
        .clock_init = rk_hsic_clock_init,
        .clock_enable = rk_hsic_clock_enable,
        .soft_reset = rk_hsic_soft_reset,
-       .get_chip_id = usb_get_chip_id,
 };
 #endif
 
-#ifdef CONFIG_RK_USB_DETECT_BY_OTG_BVALID
 #define WAKE_LOCK_TIMEOUT (HZ * 10)
 
 inline static void do_wakeup(struct work_struct *work)
 {
 //     rk28_send_wakeup_key();
 }
-#endif
 
 /********** handler for bvalid irq **********/
 static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
@@ -403,10 +399,10 @@ static irqreturn_t bvalid_irq_handler(int irq, void *dev_id)
        dwc_otg_uart_mode(NULL, PHY_USB_MODE);
 #endif
 
-#ifdef CONFIG_RK_USB_DETECT_BY_OTG_BVALID
-       wake_lock_timeout(&control_usb->usb_wakelock, WAKE_LOCK_TIMEOUT);
-       schedule_delayed_work(&control_usb->usb_det_wakeup_work, HZ/10);
-#endif
+       if(control_usb->usb_irq_wakeup){
+               wake_lock_timeout(&control_usb->usb_wakelock, WAKE_LOCK_TIMEOUT);
+               schedule_delayed_work(&control_usb->usb_det_wakeup_work, HZ/10);
+       }
 
        return IRQ_HANDLED;
 }
@@ -417,14 +413,14 @@ static int otg_irq_detect_init(struct platform_device *pdev)
        int ret = 0;
        int irq = 0;
 
-#ifdef CONFIG_RK_USB_DETECT_BY_OTG_BVALID
-       wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND, "usb_detect");
-       INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
-#endif
+       if(control_usb->usb_irq_wakeup){
+               wake_lock_init(&control_usb->usb_wakelock, WAKE_LOCK_SUSPEND, "usb_detect");
+               INIT_DELAYED_WORK(&control_usb->usb_det_wakeup_work, do_wakeup);
+       }
 
-       irq = platform_get_irq_byname(pdev, "bvalid");
+       irq = platform_get_irq_byname(pdev, "otg_bvalid");
        if (irq > 0) {
-               ret = request_irq(irq, bvalid_irq_handler, 0, "bvalid", NULL);
+               ret = request_irq(irq, bvalid_irq_handler, 0, "otg_bvalid", NULL);
                if(ret < 0){
                        dev_err(&pdev->dev, "request_irq %d failed!\n", irq);
                        return ret;
@@ -433,9 +429,9 @@ static int otg_irq_detect_init(struct platform_device *pdev)
                /* clear & enable bvalid irq */
                control_usb->grf_uoc0_base->CON3 = (3 << 30) | (3 << 14);
 
-#ifdef CONFIG_RK_USB_DETECT_BY_OTG_BVALID
-               enable_irq_wake(irq);
-#endif
+               if(control_usb->usb_irq_wakeup){
+                       enable_irq_wake(irq);
+               }
        }
 
        return ret;
@@ -518,6 +514,7 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
        int ret = 0;
 
        control_usb = devm_kzalloc(&pdev->dev, sizeof(*control_usb),GFP_KERNEL);
+
        if (!control_usb) {
                dev_err(&pdev->dev, "unable to alloc memory for control usb\n");
                ret =  -ENOMEM;
@@ -525,6 +522,10 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
        }
 
        control_usb->chip_id = RK3188_USB_CTLR;
+       control_usb->remote_wakeup = of_property_read_bool(np,
+               "rockchip,remote_wakeup");
+       control_usb->usb_irq_wakeup = of_property_read_bool(np,
+               "rockchip,usb_irq_wakeup");
 
        hclk_usb_peri = devm_clk_get(&pdev->dev, "hclk_usb_peri");
        if (IS_ERR(hclk_usb_peri)) {
@@ -542,7 +543,13 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
                goto err2;
        }
 
+       /* init host gpio */
        control_usb->host_gpios = devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
+       if(!control_usb->host_gpios){
+               dev_err(&pdev->dev, "unable to alloc memory for host_gpios\n");
+               ret =  -ENOMEM;
+               goto err2;
+       }
 
        gpio =  of_get_named_gpio(np, "gpios", 0);
        if(!gpio_is_valid(gpio)){
@@ -550,7 +557,9 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
                ret = -EINVAL;
                goto err2;
        }
+
        control_usb->host_gpios->gpio = gpio;
+
        err = devm_gpio_request(&pdev->dev, gpio, "host_drv_gpio");
        if (err) {
                dev_err(&pdev->dev,
@@ -559,8 +568,15 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
                ret = err;
                goto err2;
        }
+       gpio_direction_output(control_usb->host_gpios->gpio, 1);
 
+       /* init otg gpio */
        control_usb->otg_gpios = devm_kzalloc(&pdev->dev, sizeof(struct gpio), GFP_KERNEL);
+       if(!control_usb->otg_gpios){
+               dev_err(&pdev->dev, "unable to alloc memory for otg_gpios\n");
+               ret =  -ENOMEM;
+               goto err2;
+       }
 
        gpio =  of_get_named_gpio(np, "gpios", 1);
        if(!gpio_is_valid(gpio)){
@@ -577,6 +593,7 @@ static int dwc_otg_control_usb_probe(struct platform_device *pdev)
                ret = err;
                goto err2;
        }
+       gpio_direction_output(control_usb->otg_gpios->gpio, 0);
 
        ret = otg_irq_detect_init(pdev);
        if (ret < 0)