disable some mediatekl custom warnings
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / mfd / omap-usb-host.c
index 4febc5c7fdeece4a87be9c2dca0eb642357d4cc7..759fae3ca7fb0dba592ab808d036f889bf96f922 100644 (file)
@@ -1,8 +1,9 @@
 /**
  * omap-usb-host.c - The USBHS core driver for OMAP EHCI & OHCI
  *
- * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com
+ * Copyright (C) 2011-2013 Texas Instruments Incorporated - http://www.ti.com
  * Author: Keshava Munegowda <keshava_mgowda@ti.com>
+ * Author: Roger Quadros <rogerq@ti.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2  of
@@ -27,6 +28,9 @@
 #include <linux/platform_device.h>
 #include <linux/platform_data/usb-omap.h>
 #include <linux/pm_runtime.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/err.h>
 
 #include "omap-usb.h"
 
@@ -137,6 +141,49 @@ static inline u8 usbhs_readb(void __iomem *base, u8 reg)
 
 /*-------------------------------------------------------------------------*/
 
+/**
+ * Map 'enum usbhs_omap_port_mode' found in <linux/platform_data/usb-omap.h>
+ * to the device tree binding portN-mode found in
+ * 'Documentation/devicetree/bindings/mfd/omap-usb-host.txt'
+ */
+static const char * const port_modes[] = {
+       [OMAP_USBHS_PORT_MODE_UNUSED]   = "",
+       [OMAP_EHCI_PORT_MODE_PHY]       = "ehci-phy",
+       [OMAP_EHCI_PORT_MODE_TLL]       = "ehci-tll",
+       [OMAP_EHCI_PORT_MODE_HSIC]      = "ehci-hsic",
+       [OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0]   = "ohci-phy-6pin-datse0",
+       [OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM]     = "ohci-phy-6pin-dpdm",
+       [OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0]   = "ohci-phy-3pin-datse0",
+       [OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM]     = "ohci-phy-4pin-dpdm",
+       [OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0]   = "ohci-tll-6pin-datse0",
+       [OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM]     = "ohci-tll-6pin-dpdm",
+       [OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0]   = "ohci-tll-3pin-datse0",
+       [OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM]     = "ohci-tll-4pin-dpdm",
+       [OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0]   = "ohci-tll-2pin-datse0",
+       [OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM]     = "ohci-tll-2pin-dpdm",
+};
+
+/**
+ * omap_usbhs_get_dt_port_mode - Get the 'enum usbhs_omap_port_mode'
+ * from the port mode string.
+ * @mode: The port mode string, usually obtained from device tree.
+ *
+ * The function returns the 'enum usbhs_omap_port_mode' that matches the
+ * provided port mode string as per the port_modes table.
+ * If no match is found it returns -ENODEV
+ */
+static const int omap_usbhs_get_dt_port_mode(const char *mode)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(port_modes); i++) {
+               if (!strcmp(mode, port_modes[i]))
+                       return i;
+       }
+
+       return -ENODEV;
+}
+
 static struct platform_device *omap_usbhs_alloc_child(const char *name,
                        struct resource *res, int num_resources, void *pdata,
                        size_t pdata_size, struct device *dev)
@@ -278,7 +325,7 @@ static int usbhs_runtime_resume(struct device *dev)
 
        dev_dbg(dev, "usbhs_runtime_resume\n");
 
-       omap_tll_enable();
+       omap_tll_enable(pdata);
 
        if (!IS_ERR(omap->ehci_logic_fck))
                clk_enable(omap->ehci_logic_fck);
@@ -353,7 +400,7 @@ static int usbhs_runtime_suspend(struct device *dev)
        if (!IS_ERR(omap->ehci_logic_fck))
                clk_disable(omap->ehci_logic_fck);
 
-       omap_tll_disable();
+       omap_tll_disable(pdata);
 
        return 0;
 }
@@ -430,24 +477,10 @@ static unsigned omap_usbhs_rev2_hostconfig(struct usbhs_hcd_omap *omap,
 static void omap_usbhs_init(struct device *dev)
 {
        struct usbhs_hcd_omap           *omap = dev_get_drvdata(dev);
-       struct usbhs_omap_platform_data *pdata = omap->pdata;
        unsigned                        reg;
 
        dev_dbg(dev, "starting TI HSUSB Controller\n");
 
-       if (pdata->phy_reset) {
-               if (gpio_is_valid(pdata->reset_gpio_port[0]))
-                       gpio_request_one(pdata->reset_gpio_port[0],
-                                        GPIOF_OUT_INIT_LOW, "USB1 PHY reset");
-
-               if (gpio_is_valid(pdata->reset_gpio_port[1]))
-                       gpio_request_one(pdata->reset_gpio_port[1],
-                                        GPIOF_OUT_INIT_LOW, "USB2 PHY reset");
-
-               /* Hold the PHY in RESET for enough time till DIR is high */
-               udelay(10);
-       }
-
        pm_runtime_get_sync(dev);
 
        reg = usbhs_read(omap->uhh_base, OMAP_UHH_HOSTCONFIG);
@@ -476,36 +509,59 @@ static void omap_usbhs_init(struct device *dev)
        dev_dbg(dev, "UHH setup done, uhh_hostconfig=%x\n", reg);
 
        pm_runtime_put_sync(dev);
-       if (pdata->phy_reset) {
-               /* Hold the PHY in RESET for enough time till
-                * PHY is settled and ready
-                */
-               udelay(10);
+}
+
+static int usbhs_omap_get_dt_pdata(struct device *dev,
+                                       struct usbhs_omap_platform_data *pdata)
+{
+       int ret, i;
+       struct device_node *node = dev->of_node;
 
-               if (gpio_is_valid(pdata->reset_gpio_port[0]))
-                       gpio_set_value_cansleep
-                               (pdata->reset_gpio_port[0], 1);
+       ret = of_property_read_u32(node, "num-ports", &pdata->nports);
+       if (ret)
+               pdata->nports = 0;
 
-               if (gpio_is_valid(pdata->reset_gpio_port[1]))
-                       gpio_set_value_cansleep
-                               (pdata->reset_gpio_port[1], 1);
+       if (pdata->nports > OMAP3_HS_USB_PORTS) {
+               dev_warn(dev, "Too many num_ports <%d> in device tree. Max %d\n",
+                               pdata->nports, OMAP3_HS_USB_PORTS);
+               return -ENODEV;
        }
-}
 
-static void omap_usbhs_deinit(struct device *dev)
-{
-       struct usbhs_hcd_omap           *omap = dev_get_drvdata(dev);
-       struct usbhs_omap_platform_data *pdata = omap->pdata;
+       /* get port modes */
+       for (i = 0; i < OMAP3_HS_USB_PORTS; i++) {
+               char prop[11];
+               const char *mode;
 
-       if (pdata->phy_reset) {
-               if (gpio_is_valid(pdata->reset_gpio_port[0]))
-                       gpio_free(pdata->reset_gpio_port[0]);
+               pdata->port_mode[i] = OMAP_USBHS_PORT_MODE_UNUSED;
+
+               snprintf(prop, sizeof(prop), "port%d-mode", i + 1);
+               ret = of_property_read_string(node, prop, &mode);
+               if (ret < 0)
+                       continue;
+
+               ret = omap_usbhs_get_dt_port_mode(mode);
+               if (ret < 0) {
+                       dev_warn(dev, "Invalid port%d-mode \"%s\" in device tree\n",
+                                       i, mode);
+                       return -ENODEV;
+               }
 
-               if (gpio_is_valid(pdata->reset_gpio_port[1]))
-                       gpio_free(pdata->reset_gpio_port[1]);
+               dev_dbg(dev, "port%d-mode: %s -> %d\n", i, mode, ret);
+               pdata->port_mode[i] = ret;
        }
+
+       /* get flags */
+       pdata->single_ulpi_bypass = of_property_read_bool(node,
+                                               "single-ulpi-bypass");
+
+       return 0;
 }
 
+static struct of_device_id usbhs_child_match_table[] = {
+       { .compatible = "ti,omap-ehci", },
+       { .compatible = "ti,omap-ohci", },
+       { }
+};
 
 /**
  * usbhs_omap_probe - initialize TI-based HCDs
@@ -522,26 +578,46 @@ static int usbhs_omap_probe(struct platform_device *pdev)
        int                             i;
        bool                            need_logic_fck;
 
+       if (dev->of_node) {
+               /* For DT boot we populate platform data from OF node */
+               pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+               if (!pdata)
+                       return -ENOMEM;
+
+               ret = usbhs_omap_get_dt_pdata(dev, pdata);
+               if (ret)
+                       return ret;
+
+               dev->platform_data = pdata;
+       }
+
        if (!pdata) {
                dev_err(dev, "Missing platform data\n");
                return -ENODEV;
        }
 
+       if (pdata->nports > OMAP3_HS_USB_PORTS) {
+               dev_info(dev, "Too many num_ports <%d> in platform_data. Max %d\n",
+                               pdata->nports, OMAP3_HS_USB_PORTS);
+               return -ENODEV;
+       }
+
        omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
        if (!omap) {
                dev_err(dev, "Memory allocation failed\n");
                return -ENOMEM;
        }
 
-       res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "uhh");
-       omap->uhh_base = devm_request_and_ioremap(dev, res);
-       if (!omap->uhh_base) {
-               dev_err(dev, "Resource request/ioremap failed\n");
-               return -EADDRNOTAVAIL;
-       }
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       omap->uhh_base = devm_ioremap_resource(dev, res);
+       if (IS_ERR(omap->uhh_base))
+               return PTR_ERR(omap->uhh_base);
 
        omap->pdata = pdata;
 
+       /* Initialize the TLL subsystem */
+       omap_tll_init(pdata);
+
        pm_runtime_enable(dev);
 
        platform_set_drvdata(pdev, omap);
@@ -575,6 +651,7 @@ static int usbhs_omap_probe(struct platform_device *pdev)
                         omap->usbhs_rev, omap->nports);
                        break;
                }
+               pdata->nports = omap->nports;
        }
 
        i = sizeof(struct clk *) * omap->nports;
@@ -700,17 +777,28 @@ static int usbhs_omap_probe(struct platform_device *pdev)
        }
 
        omap_usbhs_init(dev);
-       ret = omap_usbhs_alloc_children(pdev);
-       if (ret) {
-               dev_err(dev, "omap_usbhs_alloc_children failed\n");
-               goto err_alloc;
+
+       if (dev->of_node) {
+               ret = of_platform_populate(dev->of_node,
+                               usbhs_child_match_table, NULL, dev);
+
+               if (ret) {
+                       dev_err(dev, "Failed to create DT children: %d\n", ret);
+                       goto err_alloc;
+               }
+
+       } else {
+               ret = omap_usbhs_alloc_children(pdev);
+               if (ret) {
+                       dev_err(dev, "omap_usbhs_alloc_children failed: %d\n",
+                                               ret);
+                       goto err_alloc;
+               }
        }
 
        return 0;
 
 err_alloc:
-       omap_usbhs_deinit(&pdev->dev);
-
        for (i = 0; i < omap->nports; i++) {
                if (!IS_ERR(omap->utmi_clk[i]))
                        clk_put(omap->utmi_clk[i]);
@@ -744,6 +832,13 @@ err_mem:
        return ret;
 }
 
+static int usbhs_omap_remove_child(struct device *dev, void *data)
+{
+       dev_info(dev, "unregistering\n");
+       platform_device_unregister(to_platform_device(dev));
+       return 0;
+}
+
 /**
  * usbhs_omap_remove - shutdown processing for UHH & TLL HCDs
  * @pdev: USB Host Controller being removed
@@ -755,8 +850,6 @@ static int usbhs_omap_remove(struct platform_device *pdev)
        struct usbhs_hcd_omap *omap = platform_get_drvdata(pdev);
        int i;
 
-       omap_usbhs_deinit(&pdev->dev);
-
        for (i = 0; i < omap->nports; i++) {
                if (!IS_ERR(omap->utmi_clk[i]))
                        clk_put(omap->utmi_clk[i]);
@@ -777,6 +870,8 @@ static int usbhs_omap_remove(struct platform_device *pdev)
 
        pm_runtime_disable(&pdev->dev);
 
+       /* remove children */
+       device_for_each_child(&pdev->dev, NULL, usbhs_omap_remove_child);
        return 0;
 }
 
@@ -785,16 +880,26 @@ static const struct dev_pm_ops usbhsomap_dev_pm_ops = {
        .runtime_resume         = usbhs_runtime_resume,
 };
 
+static const struct of_device_id usbhs_omap_dt_ids[] = {
+       { .compatible = "ti,usbhs-host" },
+       { }
+};
+
+MODULE_DEVICE_TABLE(of, usbhs_omap_dt_ids);
+
+
 static struct platform_driver usbhs_omap_driver = {
        .driver = {
                .name           = (char *)usbhs_driver_name,
                .owner          = THIS_MODULE,
                .pm             = &usbhsomap_dev_pm_ops,
+               .of_match_table = of_match_ptr(usbhs_omap_dt_ids),
        },
        .remove         = usbhs_omap_remove,
 };
 
 MODULE_AUTHOR("Keshava Munegowda <keshava_mgowda@ti.com>");
+MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
 MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
 MODULE_LICENSE("GPL v2");
 MODULE_DESCRIPTION("usb host common core driver for omap EHCI and OHCI");