ramips: ramips_esw: convert it to be a platform driver
[openwrt/svn-archive/archive.git] / target / linux / ramips / files / drivers / net / ramips_esw.c
index e8041b48dfde08d4c38f3e62186811346add78ca..87479c65443e7f10621d2f074581624d444cc2f0 100644 (file)
@@ -1,5 +1,7 @@
-#include <rt305x.h>
+#include <linux/ioport.h>
+
 #include <rt305x_regs.h>
 #include <rt305x_regs.h>
+#include <rt305x_esw_platform.h>
 
 #define GPIO_PRUPOSE           0x60
 #define GPIO_MDIO_BIT          (1<<7)
 
 #define GPIO_PRUPOSE           0x60
 #define GPIO_MDIO_BIT          (1<<7)
 
 struct rt305x_esw {
        void __iomem *base;
 
 struct rt305x_esw {
        void __iomem *base;
+       struct rt305x_esw_platform_data *pdata;
 };
 
 };
 
-static struct rt305x_esw rt305x_esw;
-
 static inline void
 ramips_esw_wr(struct rt305x_esw *esw, u32 val, unsigned reg)
 {
 static inline void
 ramips_esw_wr(struct rt305x_esw *esw, u32 val, unsigned reg)
 {
@@ -121,15 +122,80 @@ rt305x_esw_hw_init(struct rt305x_esw *esw)
 }
 
 static int
 }
 
 static int
-rt305x_esw_init(void)
+rt305x_esw_probe(struct platform_device *pdev)
 {
 {
+       struct rt305x_esw_platform_data *pdata;
        struct rt305x_esw *esw;
        struct rt305x_esw *esw;
+       struct resource *res;
+       int err;
+
+       pdata = pdev->dev.platform_data;
+       if (!pdata)
+               return -EINVAL;
 
 
-       esw = &rt305x_esw;
-       esw->base = ioremap_nocache(RT305X_SWITCH_BASE, PAGE_SIZE);
-       if(!esw->base)
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if (!res) {
+               dev_err(&pdev->dev, "no memory resource found\n");
                return -ENOMEM;
                return -ENOMEM;
+       }
+
+       esw = kzalloc(sizeof (struct rt305x_esw), GFP_KERNEL);
+       if (!esw) {
+               dev_err(&pdev->dev, "no memory for private data\n");
+               return -ENOMEM;
+       }
 
 
+       esw->base = ioremap(res->start, resource_size(res));
+       if (!esw->base) {
+               dev_err(&pdev->dev, "ioremap failed\n");
+               err = -ENOMEM;
+               goto free_esw;
+       }
+
+       platform_set_drvdata(pdev, esw);
+
+       esw->pdata = pdata;
        rt305x_esw_hw_init(esw);
        rt305x_esw_hw_init(esw);
+
        return 0;
        return 0;
+
+free_esw:
+       kfree(esw);
+       return err;
+}
+
+static int
+rt305x_esw_remove(struct platform_device *pdev)
+{
+       struct rt305x_esw *esw;
+
+       esw = platform_get_drvdata(pdev);
+       if (esw) {
+               platform_set_drvdata(pdev, NULL);
+               iounmap(esw->base);
+               kfree(esw);
+       }
+
+       return 0;
+}
+
+static struct platform_driver rt305x_esw_driver = {
+       .probe = rt305x_esw_probe,
+       .remove = rt305x_esw_remove,
+       .driver = {
+               .name = "rt305x-esw",
+               .owner = THIS_MODULE,
+       },
+};
+
+static int __init
+rt305x_esw_init(void)
+{
+       return platform_driver_register(&rt305x_esw_driver);
+}
+
+static void __exit
+rt305x_esw_exit(void)
+{
+       platform_driver_unregister(&rt305x_esw_driver);
 }
 }