ramips: register gpio buttons on the Fonera 2.0N board
[openwrt.git] / target / linux / ar7 / files / drivers / vlynq / vlynq.c
index 77a3d21..f4b7b0f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006, 2007 Eugene Konev
+ * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@
 #include <linux/platform_device.h>
 #include <linux/interrupt.h>
 #include <linux/device.h>
+#include <linux/delay.h>
 #include <linux/io.h>
 
 #include <linux/vlynq.h>
@@ -39,6 +40,8 @@
 #define VLYNQ_CTRL_INT2CFG             0x00000080
 #define VLYNQ_CTRL_RESET               0x00000001
 
+#define VLYNQ_CTRL_CLOCK_MASK          (0x7 << 16)
+
 #define VLYNQ_INT_OFFSET               0x00000014
 #define VLYNQ_REMOTE_OFFSET            0x00000080
 
@@ -68,7 +71,7 @@ struct vlynq_regs {
        u32 autonego;
        u32 unused[6];
        u32 int_device[8];
-} __attribute__ ((packed));
+};
 
 #define vlynq_reg_read(reg) readl(&(reg))
 #define vlynq_reg_write(reg, val)  writel(val, &(reg))
@@ -113,6 +116,24 @@ int vlynq_linked(struct vlynq_device *dev)
        return 0;
 }
 
+static void vlynq_reset(struct vlynq_device *dev)
+{
+       vlynq_reg_write(dev->local->control,
+                       vlynq_reg_read(dev->local->control) |
+                       VLYNQ_CTRL_RESET);
+
+       /* Wait for the devices to finish resetting */
+       msleep(5);
+
+       /* Remove reset bit */
+       vlynq_reg_write(dev->local->control,
+                       vlynq_reg_read(dev->local->control) &
+                       ~VLYNQ_CTRL_RESET);
+
+       /* Give some time for the devices to settle */
+       msleep(5);
+}
+
 static void vlynq_irq_unmask(unsigned int irq)
 {
        u32 val;
@@ -301,22 +322,18 @@ static int vlynq_device_match(struct device *dev,
 {
        struct vlynq_device *vdev = to_vlynq_device(dev);
        struct vlynq_driver *vdrv = to_vlynq_driver(drv);
-       struct plat_vlynq_ops *ops = dev->platform_data;
        struct vlynq_device_id *ids = vdrv->id_table;
-       u32 id = 0;
-       int result;
 
        while (ids->id) {
-               vdev->divisor = ids->divisor;
-               result = __vlynq_enable_device(vdev);
-               if (result == 0) {
-                       id = vlynq_reg_read(vdev->remote->chip);
-                       ops->off(vdev);
-                       if (ids->id == id) {
-                               vlynq_set_drvdata(vdev, ids);
-                               return 1;
-                       }
+               if (ids->id == vdev->dev_id) {
+                       vdev->divisor = ids->divisor;
+                       vlynq_set_drvdata(vdev, ids);
+                       printk(KERN_INFO "Driver found for VLYNQ " \
+                               "device: %08x\n", vdev->dev_id);
+                       return 1;
                }
+               printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver" \
+                       " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
                ids++;
        }
        return 0;
@@ -360,46 +377,133 @@ void vlynq_unregister_driver(struct vlynq_driver *driver)
 }
 EXPORT_SYMBOL(vlynq_unregister_driver);
 
-static int __vlynq_enable_device(struct vlynq_device *dev)
+static int __vlynq_try_remote(struct vlynq_device *dev)
 {
-       int i, result;
-       struct plat_vlynq_ops *ops = dev->dev.platform_data;
+       int i;
 
-       result = ops->on(dev);
-       if (result)
-               return result;
+       vlynq_reset(dev);
+       for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
+                       i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
+               dev->dev_id ? i++ : i--) {
+
+               if (!vlynq_linked(dev))
+                       break;
+
+               vlynq_reg_write(dev->remote->control,
+                               (vlynq_reg_read(dev->remote->control) &
+                               ~VLYNQ_CTRL_CLOCK_MASK) |
+                               VLYNQ_CTRL_CLOCK_INT |
+                               VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1));
+               vlynq_reg_write(dev->local->control,
+                               ((vlynq_reg_read(dev->local->control)
+                               & ~(VLYNQ_CTRL_CLOCK_INT |
+                               VLYNQ_CTRL_CLOCK_MASK)) |
+                               VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1)));
+
+               if (vlynq_linked(dev)) {
+                       printk(KERN_DEBUG
+                               "%s: using remote clock divisor %d\n",
+                               dev->dev.bus_id, i - vlynq_rdiv1 + 1);
+                       dev->divisor = i;
+                       return 0;
+               } else {
+                       vlynq_reset(dev);
+               }
+       }
+
+       return -ENODEV;
+}
+
+static int __vlynq_try_local(struct vlynq_device *dev)
+{
+       int i;
+       
+       vlynq_reset(dev);
+
+       for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
+                       i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
+               dev->dev_id ? i++ : i--) {
+
+               vlynq_reg_write(dev->local->control,
+                               (vlynq_reg_read(dev->local->control) &
+                               ~VLYNQ_CTRL_CLOCK_MASK) |
+                               VLYNQ_CTRL_CLOCK_INT |
+                               VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1));
+
+               if (vlynq_linked(dev)) {
+                       printk(KERN_DEBUG
+                               "%s: using local clock divisor %d\n",
+                               dev->dev.bus_id, i - vlynq_ldiv1 + 1);
+                       dev->divisor = i;
+                       return 0;
+               } else {
+                       vlynq_reset(dev);
+               }
+       }
+
+       return -ENODEV;
+}
+
+static int __vlynq_try_external(struct vlynq_device *dev)
+{
+       vlynq_reset(dev);
+       if (!vlynq_linked(dev))
+               return -ENODEV;
+
+       vlynq_reg_write(dev->remote->control,
+                       (vlynq_reg_read(dev->remote->control) &
+                       ~VLYNQ_CTRL_CLOCK_INT));
+
+       vlynq_reg_write(dev->local->control,
+                       (vlynq_reg_read(dev->local->control) &
+                       ~VLYNQ_CTRL_CLOCK_INT));
 
-       vlynq_reg_write(dev->local->control, 0);
-       vlynq_reg_write(dev->remote->control, 0);
        if (vlynq_linked(dev)) {
                printk(KERN_DEBUG "%s: using external clock\n",
                        dev->dev.bus_id);
+                       dev->divisor = vlynq_div_external;
                return 0;
        }
+       
+       return -ENODEV;
+}
+
+static int __vlynq_enable_device(struct vlynq_device *dev)
+{
+       int result;
+       struct plat_vlynq_ops *ops = dev->dev.platform_data;
+
+       result = ops->on(dev);
+       if (result)
+               return result;
 
        switch (dev->divisor) {
+       case vlynq_div_external:
        case vlynq_div_auto:
-               /* Only try locally supplied clock, others cause problems */
-               vlynq_reg_write(dev->remote->control, 0);
-               for (i = vlynq_ldiv2; i <= vlynq_ldiv8; i++) {
-                       vlynq_reg_write(dev->local->control,
-                                       VLYNQ_CTRL_CLOCK_INT |
-                                       VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1));
-                       if (vlynq_linked(dev)) {
-                               printk(KERN_DEBUG
-                                      "%s: using local clock divisor %d\n",
-                                      dev->dev.bus_id, i - vlynq_ldiv1 + 1);
-                               dev->divisor = i;
+               /* When the device is brought from reset it should have clock
+               generation negotiated by hardware.
+               Check which device is generating clocks and perform setup
+               accordingly */
+               if (vlynq_linked(dev) && vlynq_reg_read(dev->remote->control) &
+                  VLYNQ_CTRL_CLOCK_INT) {
+                       if (!__vlynq_try_remote(dev) ||
+                               !__vlynq_try_local(dev)  ||
+                               !__vlynq_try_external(dev))
+                               return 0;
+               } else {
+                       if (!__vlynq_try_external(dev) ||
+                               !__vlynq_try_local(dev)    ||
+                               !__vlynq_try_remote(dev))
                                return 0;
-                       }
                }
+               break;
        case vlynq_ldiv1: case vlynq_ldiv2: case vlynq_ldiv3: case vlynq_ldiv4:
        case vlynq_ldiv5: case vlynq_ldiv6: case vlynq_ldiv7: case vlynq_ldiv8:
-               vlynq_reg_write(dev->remote->control, 0);
                vlynq_reg_write(dev->local->control,
                                VLYNQ_CTRL_CLOCK_INT |
                                VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
                                                     vlynq_ldiv1));
+               vlynq_reg_write(dev->remote->control, 0);
                if (vlynq_linked(dev)) {
                        printk(KERN_DEBUG
                               "%s: using local clock divisor %d\n",
@@ -601,6 +705,16 @@ static int vlynq_probe(struct platform_device *pdev)
               dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
               (void *)dev->mem_start);
 
+       dev->dev_id = 0;
+       dev->divisor = vlynq_div_auto;
+       result = __vlynq_enable_device(dev);
+       if (result == 0) {
+               dev->dev_id = vlynq_reg_read(dev->remote->chip);
+               ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
+       }
+       if (dev->dev_id)
+               printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
+
        return 0;
 
 fail_register:
@@ -625,7 +739,7 @@ static int vlynq_remove(struct platform_device *pdev)
        return 0;
 }
 
-static struct platform_driver vlynq_driver = {
+static struct platform_driver vlynq_platform_driver = {
        .driver.name = "vlynq",
        .probe = vlynq_probe,
        .remove = __devexit_p(vlynq_remove),
@@ -647,7 +761,7 @@ static int __devinit vlynq_init(void)
        if (res)
                goto fail_bus;
 
-       res = platform_driver_register(&vlynq_driver);
+       res = platform_driver_register(&vlynq_platform_driver);
        if (res)
                goto fail_platform;
 
@@ -661,7 +775,7 @@ fail_bus:
 
 static void __devexit vlynq_exit(void)
 {
-       platform_driver_unregister(&vlynq_driver);
+       platform_driver_unregister(&vlynq_platform_driver);
        bus_unregister(&vlynq_bus_type);
 }
 
This page took 0.035347 seconds and 4 git commands to generate.