mfd: Program twl4030 remap_sleep correctly
[safe/jmp/linux-2.6] / drivers / mfd / ucb1x00-core.c
index e335d54..252b741 100644 (file)
  *  Note that all locks are private to this file.  Nothing else may
  *  touch them.
  */
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/errno.h>
 #include <linux/interrupt.h>
 #include <linux/device.h>
+#include <linux/mutex.h>
+#include <linux/mfd/ucb1x00.h>
+#include <linux/gpio.h>
 
-#include <asm/dma.h>
-#include <asm/hardware.h>
-#include <asm/irq.h>
+#include <mach/dma.h>
+#include <mach/hardware.h>
 
-#include "ucb1x00.h"
-
-static DECLARE_MUTEX(ucb1x00_sem);
+static DEFINE_MUTEX(ucb1x00_mutex);
 static LIST_HEAD(ucb1x00_drivers);
 static LIST_HEAD(ucb1x00_devices);
 
@@ -108,6 +108,60 @@ unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
        return ucb1x00_reg_read(ucb, UCB_IO_DATA);
 }
 
+static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
+{
+       struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
+       unsigned long flags;
+
+       spin_lock_irqsave(&ucb->io_lock, flags);
+       if (value)
+               ucb->io_out |= 1 << offset;
+       else
+               ucb->io_out &= ~(1 << offset);
+
+       ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
+       spin_unlock_irqrestore(&ucb->io_lock, flags);
+}
+
+static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
+{
+       struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
+       return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset);
+}
+
+static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
+{
+       struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
+       unsigned long flags;
+
+       spin_lock_irqsave(&ucb->io_lock, flags);
+       ucb->io_dir &= ~(1 << offset);
+       ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
+       spin_unlock_irqrestore(&ucb->io_lock, flags);
+
+       return 0;
+}
+
+static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
+               , int value)
+{
+       struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
+       unsigned long flags;
+
+       spin_lock_irqsave(&ucb->io_lock, flags);
+       ucb->io_dir |= (1 << offset);
+       ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
+
+       if (value)
+               ucb->io_out |= 1 << offset;
+       else
+               ucb->io_out &= ~(1 << offset);
+       ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
+       spin_unlock_irqrestore(&ucb->io_lock, flags);
+
+       return 0;
+}
+
 /*
  * UCB1300 data sheet says we must:
  *  1. enable ADC      => 5us (including reference startup time)
@@ -204,7 +258,7 @@ void ucb1x00_adc_disable(struct ucb1x00 *ucb)
  * SIBCLK to talk to the chip.  We leave the clock running until
  * we have finished processing all interrupts from the chip.
  */
-static irqreturn_t ucb1x00_irq(int irqnr, void *devid, struct pt_regs *regs)
+static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
 {
        struct ucb1x00 *ucb = devid;
        struct ucb1x00_irq *irq;
@@ -420,8 +474,10 @@ static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
        unsigned long mask;
 
        mask = probe_irq_on();
-       if (!mask)
+       if (!mask) {
+               probe_irq_off(mask);
                return NO_IRQ;
+       }
 
        /*
         * Enable the ADC interrupt.
@@ -457,7 +513,7 @@ static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
        return probe_irq_off(mask);
 }
 
-static void ucb1x00_release(struct class_device *dev)
+static void ucb1x00_release(struct device *dev)
 {
        struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
        kfree(ucb);
@@ -465,7 +521,7 @@ static void ucb1x00_release(struct class_device *dev)
 
 static struct class ucb1x00_class = {
        .name           = "ucb1x00",
-       .release        = ucb1x00_release,
+       .dev_release    = ucb1x00_release,
 };
 
 static int ucb1x00_probe(struct mcp *mcp)
@@ -474,25 +530,25 @@ static int ucb1x00_probe(struct mcp *mcp)
        struct ucb1x00_driver *drv;
        unsigned int id;
        int ret = -ENODEV;
+       int temp;
 
        mcp_enable(mcp);
        id = mcp_reg_read(mcp, UCB_ID);
 
-       if (id != UCB_ID_1200 && id != UCB_ID_1300) {
+       if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
                printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
                goto err_disable;
        }
 
-       ucb = kmalloc(sizeof(struct ucb1x00), GFP_KERNEL);
+       ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
        ret = -ENOMEM;
        if (!ucb)
                goto err_disable;
 
-       memset(ucb, 0, sizeof(struct ucb1x00));
 
-       ucb->cdev.class = &ucb1x00_class;
-       ucb->cdev.dev = &mcp->attached_device;
-       strlcpy(ucb->cdev.class_id, "ucb1x00", sizeof(ucb->cdev.class_id));
+       ucb->dev.class = &ucb1x00_class;
+       ucb->dev.parent = &mcp->attached_device;
+       dev_set_name(&ucb->dev, "ucb1x00");
 
        spin_lock_init(&ucb->lock);
        spin_lock_init(&ucb->io_lock);
@@ -507,31 +563,51 @@ static int ucb1x00_probe(struct mcp *mcp)
                goto err_free;
        }
 
-       ret = request_irq(ucb->irq, ucb1x00_irq, 0, "UCB1x00", ucb);
+       ucb->gpio.base = -1;
+       if (mcp->gpio_base != 0) {
+               ucb->gpio.label = dev_name(&ucb->dev);
+               ucb->gpio.base = mcp->gpio_base;
+               ucb->gpio.ngpio = 10;
+               ucb->gpio.set = ucb1x00_gpio_set;
+               ucb->gpio.get = ucb1x00_gpio_get;
+               ucb->gpio.direction_input = ucb1x00_gpio_direction_input;
+               ucb->gpio.direction_output = ucb1x00_gpio_direction_output;
+               ret = gpiochip_add(&ucb->gpio);
+               if (ret)
+                       goto err_free;
+       } else
+               dev_info(&ucb->dev, "gpio_base not set so no gpiolib support");
+
+       ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING,
+                         "UCB1x00", ucb);
        if (ret) {
                printk(KERN_ERR "ucb1x00: unable to grab irq%d: %d\n",
                        ucb->irq, ret);
-               goto err_free;
+               goto err_gpio;
        }
 
-       set_irq_type(ucb->irq, IRQT_RISING);
        mcp_set_drvdata(mcp, ucb);
 
-       ret = class_device_register(&ucb->cdev);
+       ret = device_register(&ucb->dev);
        if (ret)
                goto err_irq;
 
+
        INIT_LIST_HEAD(&ucb->devs);
-       down(&ucb1x00_sem);
+       mutex_lock(&ucb1x00_mutex);
        list_add(&ucb->node, &ucb1x00_devices);
        list_for_each_entry(drv, &ucb1x00_drivers, node) {
                ucb1x00_add_dev(ucb, drv);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
+
        goto out;
 
  err_irq:
        free_irq(ucb->irq, ucb);
+ err_gpio:
+       if (ucb->gpio.base != -1)
+               temp = gpiochip_remove(&ucb->gpio);
  err_free:
        kfree(ucb);
  err_disable:
@@ -544,17 +620,24 @@ static void ucb1x00_remove(struct mcp *mcp)
 {
        struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
        struct list_head *l, *n;
+       int ret;
 
-       down(&ucb1x00_sem);
+       mutex_lock(&ucb1x00_mutex);
        list_del(&ucb->node);
        list_for_each_safe(l, n, &ucb->devs) {
                struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node);
                ucb1x00_remove_dev(dev);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
+
+       if (ucb->gpio.base != -1) {
+               ret = gpiochip_remove(&ucb->gpio);
+               if (ret)
+                       dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret);
+       }
 
        free_irq(ucb->irq, ucb);
-       class_device_unregister(&ucb->cdev);
+       device_unregister(&ucb->dev);
 }
 
 int ucb1x00_register_driver(struct ucb1x00_driver *drv)
@@ -562,12 +645,12 @@ int ucb1x00_register_driver(struct ucb1x00_driver *drv)
        struct ucb1x00 *ucb;
 
        INIT_LIST_HEAD(&drv->devs);
-       down(&ucb1x00_sem);
+       mutex_lock(&ucb1x00_mutex);
        list_add(&drv->node, &ucb1x00_drivers);
        list_for_each_entry(ucb, &ucb1x00_devices, node) {
                ucb1x00_add_dev(ucb, drv);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
        return 0;
 }
 
@@ -575,13 +658,13 @@ void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
 {
        struct list_head *n, *l;
 
-       down(&ucb1x00_sem);
+       mutex_lock(&ucb1x00_mutex);
        list_del(&drv->node);
        list_for_each_safe(l, n, &drv->devs) {
                struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node);
                ucb1x00_remove_dev(dev);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
 }
 
 static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
@@ -589,12 +672,12 @@ static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
        struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
        struct ucb1x00_dev *dev;
 
-       down(&ucb1x00_sem);
+       mutex_lock(&ucb1x00_mutex);
        list_for_each_entry(dev, &ucb->devs, dev_node) {
                if (dev->drv->suspend)
                        dev->drv->suspend(dev, state);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
        return 0;
 }
 
@@ -603,12 +686,13 @@ static int ucb1x00_resume(struct mcp *mcp)
        struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
        struct ucb1x00_dev *dev;
 
-       down(&ucb1x00_sem);
+       ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
+       mutex_lock(&ucb1x00_mutex);
        list_for_each_entry(dev, &ucb->devs, dev_node) {
                if (dev->drv->resume)
                        dev->drv->resume(dev);
        }
-       up(&ucb1x00_sem);
+       mutex_unlock(&ucb1x00_mutex);
        return 0;
 }