Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / macintosh / smu.c
index e837827..00ef468 100644 (file)
@@ -19,7 +19,6 @@
  *    the userland interface
  */
 
-#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/device.h>
@@ -35,6 +34,7 @@
 #include <linux/delay.h>
 #include <linux/sysdev.h>
 #include <linux/poll.h>
+#include <linux/mutex.h>
 
 #include <asm/byteorder.h>
 #include <asm/io.h>
@@ -53,7 +53,7 @@
 #undef DEBUG_SMU
 
 #ifdef DEBUG_SMU
-#define DPRINTK(fmt, args...) do { udbg_printf(KERN_DEBUG fmt , ##args); } while (0)
+#define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
 #else
 #define DPRINTK(fmt, args...) do { } while (0)
 #endif
@@ -75,9 +75,11 @@ struct smu_device {
        struct of_device        *of_dev;
        int                     doorbell;       /* doorbell gpio */
        u32 __iomem             *db_buf;        /* doorbell buffer */
-       int                     db_irq;
+       struct device_node      *db_node;
+       unsigned int            db_irq;
        int                     msg;
-       int                     msg_irq;
+       struct device_node      *msg_node;
+       unsigned int            msg_irq;
        struct smu_cmd_buf      *cmd_buf;       /* command buffer virtual */
        u32                     cmd_buf_abs;    /* command buffer absolute */
        struct list_head        cmd_list;
@@ -92,7 +94,10 @@ struct smu_device {
  * for now, just hard code that
  */
 static struct smu_device       *smu;
-static DECLARE_MUTEX(smu_part_access);
+static DEFINE_MUTEX(smu_part_access);
+static int smu_irq_inited;
+
+static void smu_i2c_retry(unsigned long data);
 
 /*
  * SMU driver low level stuff
@@ -255,6 +260,10 @@ int smu_queue_cmd(struct smu_cmd *cmd)
                smu_start_cmd();
        spin_unlock_irqrestore(&smu->lock, flags);
 
+       /* Workaround for early calls when irq isn't available */
+       if (!smu_irq_inited || smu->db_irq == NO_IRQ)
+               smu_spinwait_cmd(cmd);
+
        return 0;
 }
 EXPORT_SYMBOL(smu_queue_cmd);
@@ -469,7 +478,6 @@ int __init smu_init (void)
        smu->of_node = np;
        smu->db_irq = NO_IRQ;
        smu->msg_irq = NO_IRQ;
-       init_timer(&smu->i2c_timer);
 
        /* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a
         * 32 bits value safely
@@ -477,14 +485,15 @@ int __init smu_init (void)
        smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
        smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);
 
-       np = of_find_node_by_name(NULL, "smu-doorbell");
-       if (np == NULL) {
+       smu->db_node = of_find_node_by_name(NULL, "smu-doorbell");
+       if (smu->db_node == NULL) {
                printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");
                goto fail;
        }
-       data = (u32 *)get_property(np, "reg", NULL);
+       data = (u32 *)get_property(smu->db_node, "reg", NULL);
        if (data == NULL) {
-               of_node_put(np);
+               of_node_put(smu->db_node);
+               smu->db_node = NULL;
                printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
                goto fail;
        }
@@ -496,27 +505,21 @@ int __init smu_init (void)
        smu->doorbell = *data;
        if (smu->doorbell < 0x50)
                smu->doorbell += 0x50;
-       if (np->n_intrs > 0)
-               smu->db_irq = np->intrs[0].line;
-
-       of_node_put(np);
 
        /* Now look for the smu-interrupt GPIO */
        do {
-               np = of_find_node_by_name(NULL, "smu-interrupt");
-               if (np == NULL)
+               smu->msg_node = of_find_node_by_name(NULL, "smu-interrupt");
+               if (smu->msg_node == NULL)
                        break;
-               data = (u32 *)get_property(np, "reg", NULL);
+               data = (u32 *)get_property(smu->msg_node, "reg", NULL);
                if (data == NULL) {
-                       of_node_put(np);
+                       of_node_put(smu->msg_node);
+                       smu->msg_node = NULL;
                        break;
                }
                smu->msg = *data;
                if (smu->msg < 0x50)
                        smu->msg += 0x50;
-               if (np->n_intrs > 0)
-                       smu->msg_irq = np->intrs[0].line;
-               of_node_put(np);
        } while(0);
 
        /* Doorbell buffer is currently hard-coded, I didn't find a proper
@@ -544,13 +547,30 @@ static int smu_late_init(void)
        if (!smu)
                return 0;
 
+       init_timer(&smu->i2c_timer);
+       smu->i2c_timer.function = smu_i2c_retry;
+       smu->i2c_timer.data = (unsigned long)smu;
+
+       if (smu->db_node) {
+               smu->db_irq = irq_of_parse_and_map(smu->db_node, 0);
+               if (smu->db_irq == NO_IRQ)
+                       printk(KERN_ERR "smu: failed to map irq for node %s\n",
+                              smu->db_node->full_name);
+       }
+       if (smu->msg_node) {
+               smu->msg_irq = irq_of_parse_and_map(smu->msg_node, 0);
+               if (smu->msg_irq == NO_IRQ)
+                       printk(KERN_ERR "smu: failed to map irq for node %s\n",
+                              smu->msg_node->full_name);
+       }
+
        /*
         * Try to request the interrupts
         */
 
        if (smu->db_irq != NO_IRQ) {
                if (request_irq(smu->db_irq, smu_db_intr,
-                               SA_SHIRQ, "SMU doorbell", smu) < 0) {
+                               IRQF_SHARED, "SMU doorbell", smu) < 0) {
                        printk(KERN_WARNING "SMU: can't "
                               "request interrupt %d\n",
                               smu->db_irq);
@@ -560,7 +580,7 @@ static int smu_late_init(void)
 
        if (smu->msg_irq != NO_IRQ) {
                if (request_irq(smu->msg_irq, smu_msg_intr,
-                               SA_SHIRQ, "SMU message", smu) < 0) {
+                               IRQF_SHARED, "SMU message", smu) < 0) {
                        printk(KERN_WARNING "SMU: can't "
                               "request interrupt %d\n",
                               smu->msg_irq);
@@ -568,9 +588,13 @@ static int smu_late_init(void)
                }
        }
 
+       smu_irq_inited = 1;
        return 0;
 }
-arch_initcall(smu_late_init);
+/* This has to be before arch_initcall as the low i2c stuff relies on the
+ * above having been done before we reach arch_initcalls
+ */
+core_initcall(smu_late_init);
 
 /*
  * sysfs visibility
@@ -580,20 +604,10 @@ static void smu_expose_childs(void *unused)
 {
        struct device_node *np;
 
-       for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;) {
-               if (device_is_compatible(np, "smu-i2c")) {
-                       char name[32];
-                       u32 *reg = (u32 *)get_property(np, "reg", NULL);
-
-                       if (reg == NULL)
-                               continue;
-                       sprintf(name, "smu-i2c-%02x", *reg);
-                       of_platform_device_create(np, name, &smu->of_dev->dev);
-               }
+       for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
                if (device_is_compatible(np, "smu-sensors"))
-                       of_platform_device_create(np, "smu-sensors", &smu->of_dev->dev);
-       }
-
+                       of_platform_device_create(np, "smu-sensors",
+                                                 &smu->of_dev->dev);
 }
 
 static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
@@ -631,8 +645,6 @@ static struct of_platform_driver smu_of_platform_driver =
 
 static int __init smu_init_sysfs(void)
 {
-       int rc;
-
        /*
         * Due to sysfs bogosity, a sysdev is not a real device, so
         * we should in fact create both if we want sysdev semantics
@@ -641,7 +653,7 @@ static int __init smu_init_sysfs(void)
         * I'm a bit too far from figuring out how that works with those
         * new chipsets, but that will come back and bite us
         */
-       rc = of_register_driver(&smu_of_platform_driver);
+       of_register_driver(&smu_of_platform_driver);
        return 0;
 }
 
@@ -712,13 +724,13 @@ static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
 
 static void smu_i2c_retry(unsigned long data)
 {
-       struct smu_i2c_cmd      *cmd = (struct smu_i2c_cmd *)data;
+       struct smu_i2c_cmd      *cmd = smu->cmd_i2c_cur;
 
        DPRINTK("SMU: i2c failure, requeuing...\n");
 
        /* requeue command simply by resetting reply_len */
        cmd->pdata[0] = 0xff;
-       cmd->scmd.reply_len = 0x10;
+       cmd->scmd.reply_len = sizeof(cmd->pdata);
        smu_queue_cmd(&cmd->scmd);
 }
 
@@ -747,10 +759,13 @@ static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
         */
        if (fail && --cmd->retries > 0) {
                DPRINTK("SMU: i2c failure, starting timer...\n");
-               smu->i2c_timer.function = smu_i2c_retry;
-               smu->i2c_timer.data = (unsigned long)cmd;
-               smu->i2c_timer.expires = jiffies + msecs_to_jiffies(5);
-               add_timer(&smu->i2c_timer);
+               BUG_ON(cmd != smu->cmd_i2c_cur);
+               if (!smu_irq_inited) {
+                       mdelay(5);
+                       smu_i2c_retry(0);
+                       return;
+               }
+               mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
                return;
        }
 
@@ -764,7 +779,7 @@ static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
 
        /* Ok, initial command complete, now poll status */
        scmd->reply_buf = cmd->pdata;
-       scmd->reply_len = 0x10;
+       scmd->reply_len = sizeof(cmd->pdata);
        scmd->data_buf = cmd->pdata;
        scmd->data_len = 1;
        cmd->pdata[0] = 0;
@@ -786,7 +801,7 @@ int smu_queue_i2c(struct smu_i2c_cmd *cmd)
        cmd->scmd.done = smu_i2c_low_completion;
        cmd->scmd.misc = cmd;
        cmd->scmd.reply_buf = cmd->pdata;
-       cmd->scmd.reply_len = 0x10;
+       cmd->scmd.reply_len = sizeof(cmd->pdata);
        cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
        cmd->scmd.status = 1;
        cmd->stage = 0;
@@ -909,10 +924,13 @@ static struct smu_sdbp_header *smu_create_sdb_partition(int id)
        struct property *prop;
 
        /* First query the partition info */
+       DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
        smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
                         smu_done_complete, &comp,
                         SMU_CMD_PARTITION_LATEST, id);
        wait_for_completion(&comp);
+       DPRINTK("SMU: done, status: %d, reply_len: %d\n",
+               cmd.cmd.status, cmd.cmd.reply_len);
 
        /* Partition doesn't exist (or other error) */
        if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
@@ -975,22 +993,25 @@ struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
 
        sprintf(pname, "sdb-partition-%02x", id);
 
+       DPRINTK("smu_get_sdb_partition(%02x)\n", id);
+
        if (interruptible) {
                int rc;
-               rc = down_interruptible(&smu_part_access);
+               rc = mutex_lock_interruptible(&smu_part_access);
                if (rc)
                        return ERR_PTR(rc);
        } else
-               down(&smu_part_access);
+               mutex_lock(&smu_part_access);
 
        part = (struct smu_sdbp_header *)get_property(smu->of_node,
                                                      pname, size);
        if (part == NULL) {
+               DPRINTK("trying to extract from SMU ...\n");
                part = smu_create_sdb_partition(id);
                if (part != NULL && size)
                        *size = part->len << 2;
        }
-       up(&smu_part_access);
+       mutex_unlock(&smu_part_access);
        return part;
 }