Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / drivers / i2c / chips / tps65010.c
index c0ac01b..6a75782 100644 (file)
@@ -18,9 +18,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-#undef DEBUG
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
@@ -33,6 +31,7 @@
 #include <linux/suspend.h>
 #include <linux/debugfs.h>
 #include <linux/seq_file.h>
+#include <linux/mutex.h>
 
 #include <asm/irq.h>
 #include <asm/mach-types.h>
 /*-------------------------------------------------------------------------*/
 
 #define        DRIVER_VERSION  "2 May 2005"
-#define        DRIVER_NAME     (tps65010_driver.name)
+#define        DRIVER_NAME     (tps65010_driver.driver.name)
 
 MODULE_DESCRIPTION("TPS6501x Power Management Driver");
 MODULE_LICENSE("GPL");
 
-/* only two addresses possible */
-#define        TPS_BASE        0x48
-static unsigned short normal_i2c[] = {
-       TPS_BASE,
-       I2C_CLIENT_END };
-static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
+static unsigned short normal_i2c[] = { 0x48, /* 0x49, */ I2C_CLIENT_END };
 
 I2C_CLIENT_INSMOD;
 
@@ -86,7 +80,7 @@ enum tps_model {
 
 struct tps65010 {
        struct i2c_client       client;
-       struct semaphore        lock;
+       struct mutex            lock;
        int                     irq;
        struct work_struct      work;
        struct dentry           *file;
@@ -102,10 +96,10 @@ struct tps65010 {
        u8                      chgstatus, regstatus, chgconf;
        u8                      nmask1, nmask2;
 
-       /* plus four GPIOs, probably used to switch power */
+       /* not currently tracking GPIO state */
 };
 
-#define        POWER_POLL_DELAY        msecs_to_jiffies(800)
+#define        POWER_POLL_DELAY        msecs_to_jiffies(5000)
 
 /*-------------------------------------------------------------------------*/
 
@@ -135,7 +129,7 @@ static void dbg_regstat(char *buf, size_t len, u8 regstatus)
                (regstatus & TPS_REG_COVER) ? " uncover" : "",
                (regstatus & TPS_REG_UVLO) ? " UVLO" : "",
                (regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "",
-               (regstatus & TPS_REG_PG_LD02) ? " ld01_bad" : "",
+               (regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "",
                (regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "",
                (regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "",
                (regstatus & TPS_REG_PG_CORE) ? " core_bad" : "");
@@ -143,7 +137,7 @@ static void dbg_regstat(char *buf, size_t len, u8 regstatus)
 
 static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig)
 {
-       char *hibit;
+       const char *hibit;
 
        if (por)
                hibit = (chgconfig & TPS_CHARGE_POR)
@@ -223,7 +217,7 @@ static int dbg_show(struct seq_file *s, void *_)
        seq_printf(s, "driver  %s\nversion %s\nchip    %s\n\n",
                        DRIVER_NAME, DRIVER_VERSION, chip);
 
-       down(&tps->lock);
+       mutex_lock(&tps->lock);
 
        /* FIXME how can we tell whether a battery is present?
         * likely involves a charge gauging chip (like BQ26501).
@@ -295,7 +289,7 @@ static int dbg_show(struct seq_file *s, void *_)
        seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2);
 
        for (i = 0; i < 4; i++) {
-               if (value & (1 << (4 +i)))
+               if (value & (1 << (4 + i)))
                        seq_printf(s, "  gpio%d-out %s\n", i + 1,
                                (value & (1 << i)) ? "low" : "hi ");
                else
@@ -305,13 +299,13 @@ static int dbg_show(struct seq_file *s, void *_)
                                (v2 & (1 << (4 + i))) ? "rising" : "falling");
        }
 
-       up(&tps->lock);
+       mutex_unlock(&tps->lock);
        return 0;
 }
 
 static int dbg_tps_open(struct inode *inode, struct file *file)
 {
-       return single_open(file, dbg_show, inode->u.generic_ip);
+       return single_open(file, dbg_show, inode->i_private);
 }
 
 static struct file_operations debug_fops = {
@@ -421,7 +415,7 @@ static void tps65010_work(void *_tps)
 {
        struct tps65010         *tps = _tps;
 
-       down(&tps->lock);
+       mutex_lock(&tps->lock);
 
        tps65010_interrupt(tps);
 
@@ -449,7 +443,7 @@ static void tps65010_work(void *_tps)
        if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
                enable_irq(tps->irq);
 
-       up(&tps->lock);
+       mutex_unlock(&tps->lock);
 }
 
 static irqreturn_t tps65010_irq(int irq, void *_tps, struct pt_regs *regs)
@@ -481,7 +475,7 @@ static int __exit tps65010_detach_client(struct i2c_client *client)
        debugfs_remove(tps->file);
        if (i2c_detach_client(client) == 0)
                kfree(tps);
-       the_tps = 0;
+       the_tps = NULL;
        return 0;
 }
 
@@ -499,22 +493,21 @@ tps65010_probe(struct i2c_adapter *bus, int address, int kind)
 {
        struct tps65010         *tps;
        int                     status;
+       unsigned long           irqflags;
 
        if (the_tps) {
                dev_dbg(&bus->dev, "only one %s for now\n", DRIVER_NAME);
                return 0;
        }
 
-       tps = kmalloc(sizeof *tps, GFP_KERNEL);
+       tps = kzalloc(sizeof *tps, GFP_KERNEL);
        if (!tps)
                return 0;
 
-       memset(tps, 0, sizeof *tps);
-       init_MUTEX(&tps->lock);
+       mutex_init(&tps->lock);
        INIT_WORK(&tps->work, tps65010_work, tps);
        tps->irq = -1;
        tps->client.addr = address;
-       i2c_set_clientdata(&tps->client, tps);
        tps->client.adapter = bus;
        tps->client.driver = &tps65010_driver;
        strlcpy(tps->client.name, DRIVER_NAME, I2C_NAME_SIZE);
@@ -523,11 +516,13 @@ tps65010_probe(struct i2c_adapter *bus, int address, int kind)
        if (status < 0) {
                dev_dbg(&bus->dev, "can't attach %s to device %d, err %d\n",
                                DRIVER_NAME, address, status);
-fail1:
-               kfree(tps);
-               return 0;
+               goto fail1;
        }
 
+       /* the IRQ is active low, but many gpio lines can't support that
+        * so this driver can use falling-edge triggers instead.
+        */
+       irqflags = IRQF_SAMPLE_RANDOM;
 #ifdef CONFIG_ARM
        if (machine_is_omap_h2()) {
                tps->model = TPS65010;
@@ -535,7 +530,7 @@ fail1:
                tps->irq = OMAP_GPIO_IRQ(58);
                omap_request_gpio(58);
                omap_set_gpio_direction(58, 1);
-               omap_set_gpio_edge_ctrl(58, OMAP_GPIO_FALLING_EDGE);
+               irqflags |= IRQF_TRIGGER_FALLING;
        }
        if (machine_is_omap_osk()) {
                tps->model = TPS65010;
@@ -543,21 +538,18 @@ fail1:
                tps->irq = OMAP_GPIO_IRQ(OMAP_MPUIO(1));
                omap_request_gpio(OMAP_MPUIO(1));
                omap_set_gpio_direction(OMAP_MPUIO(1), 1);
-               omap_set_gpio_edge_ctrl(OMAP_MPUIO(1), OMAP_GPIO_FALLING_EDGE);
+               irqflags |= IRQF_TRIGGER_FALLING;
        }
        if (machine_is_omap_h3()) {
                tps->model = TPS65013;
 
                // FIXME set up this board's IRQ ...
        }
-#else
-#define set_irq_type(num,trigger)      do{}while(0)
 #endif
 
        if (tps->irq > 0) {
-               set_irq_type(tps->irq, IRQT_LOW);
                status = request_irq(tps->irq, tps65010_irq,
-                       SA_SAMPLE_RANDOM, DRIVER_NAME, tps);
+                       irqflags, DRIVER_NAME, tps);
                if (status < 0) {
                        dev_dbg(&tps->client.dev, "can't get IRQ %d, err %d\n",
                                        tps->irq, status);
@@ -633,6 +625,9 @@ fail1:
        tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
                                tps, DEBUG_FOPS);
        return 0;
+fail1:
+       kfree(tps);
+       return 0;
 }
 
 static int __init tps65010_scan_bus(struct i2c_adapter *bus)
@@ -643,10 +638,9 @@ static int __init tps65010_scan_bus(struct i2c_adapter *bus)
 }
 
 static struct i2c_driver tps65010_driver = {
-       .owner          = THIS_MODULE,
-       .name           = "tps65010",
-       .id             = 888,          /* FIXME assign "official" value */
-       .flags          = I2C_DF_NOTIFY,
+       .driver = {
+               .name   = "tps65010",
+       },
        .attach_adapter = tps65010_scan_bus,
        .detach_client  = __exit_p(tps65010_detach_client),
 };
@@ -701,7 +695,7 @@ int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
        if ((gpio < GPIO1) || (gpio > GPIO4))
                return -EINVAL;
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
        defgpio = i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO);
 
@@ -726,7 +720,7 @@ int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
                gpio, value ? "high" : "low",
                i2c_smbus_read_byte_data(&the_tps->client, TPS_DEFGPIO));
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
        return status;
 }
 EXPORT_SYMBOL(tps65010_set_gpio_out_value);
@@ -744,20 +738,22 @@ int tps65010_set_led(unsigned led, unsigned mode)
        if (!the_tps)
                return -ENODEV;
 
-       if(led == LED1)
+       if (led == LED1)
                offs = 0;
        else {
                offs = 2;
                led = LED2;
        }
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
-       dev_dbg (&the_tps->client.dev, "led%i_on   0x%02x\n", led,
-               i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_ON + offs));
+       pr_debug("%s: led%i_on   0x%02x\n", DRIVER_NAME, led,
+               i2c_smbus_read_byte_data(&the_tps->client,
+                               TPS_LED1_ON + offs));
 
-       dev_dbg (&the_tps->client.dev, "led%i_per  0x%02x\n", led,
-               i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_PER + offs));
+       pr_debug("%s: led%i_per  0x%02x\n", DRIVER_NAME, led,
+               i2c_smbus_read_byte_data(&the_tps->client,
+                               TPS_LED1_PER + offs));
 
        switch (mode) {
        case OFF:
@@ -773,9 +769,9 @@ int tps65010_set_led(unsigned led, unsigned mode)
                led_per = 0x08 | (1 << 7);
                break;
        default:
-               printk(KERN_ERR "%s: Wrong mode parameter for tps65010_set_led()\n",
+               printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
                       DRIVER_NAME);
-               up(&the_tps->lock);
+               mutex_unlock(&the_tps->lock);
                return -EINVAL;
        }
 
@@ -785,11 +781,11 @@ int tps65010_set_led(unsigned led, unsigned mode)
        if (status != 0) {
                printk(KERN_ERR "%s: Failed to write led%i_on register\n",
                       DRIVER_NAME, led);
-               up(&the_tps->lock);
+               mutex_unlock(&the_tps->lock);
                return status;
        }
 
-       dev_dbg (&the_tps->client.dev, "led%i_on   0x%02x\n", led,
+       pr_debug("%s: led%i_on   0x%02x\n", DRIVER_NAME, led,
                i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_ON + offs));
 
        status = i2c_smbus_write_byte_data(&the_tps->client,
@@ -798,14 +794,15 @@ int tps65010_set_led(unsigned led, unsigned mode)
        if (status != 0) {
                printk(KERN_ERR "%s: Failed to write led%i_per register\n",
                       DRIVER_NAME, led);
-               up(&the_tps->lock);
+               mutex_unlock(&the_tps->lock);
                return status;
        }
 
-       dev_dbg (&the_tps->client.dev, "led%i_per  0x%02x\n", led,
-               i2c_smbus_read_byte_data(&the_tps->client, TPS_LED1_PER + offs));
+       pr_debug("%s: led%i_per  0x%02x\n", DRIVER_NAME, led,
+               i2c_smbus_read_byte_data(&the_tps->client,
+                               TPS_LED1_PER + offs));
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
 
        return status;
 }
@@ -823,7 +820,7 @@ int tps65010_set_vib(unsigned value)
        if (!the_tps)
                return -ENODEV;
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
        vdcdc2 = i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC2);
        vdcdc2 &= ~(1 << 1);
@@ -834,7 +831,7 @@ int tps65010_set_vib(unsigned value)
 
        pr_debug("%s: vibrator %s\n", DRIVER_NAME, value ? "on" : "off");
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
        return status;
 }
 EXPORT_SYMBOL(tps65010_set_vib);
@@ -851,7 +848,7 @@ int tps65010_set_low_pwr(unsigned mode)
        if (!the_tps)
                return -ENODEV;
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
        pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
                mode ? "enable" : "disable",
@@ -874,12 +871,12 @@ int tps65010_set_low_pwr(unsigned mode)
 
        if (status != 0)
                printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
-                      DRIVER_NAME);
+                       DRIVER_NAME);
        else
                pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
                        i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
 
        return status;
 }
@@ -897,22 +894,22 @@ int tps65010_config_vregs1(unsigned value)
        if (!the_tps)
                return -ENODEV;
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
        pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
-                       i2c_smbus_read_byte_data(&the_tps->client, TPS_VREGS1));
+                       i2c_smbus_read_byte_data(&the_tps->client, TPS_VREGS1));
 
        status = i2c_smbus_write_byte_data(&the_tps->client,
                        TPS_VREGS1, value);
 
        if (status != 0)
                printk(KERN_ERR "%s: Failed to write vregs1 register\n",
-                       DRIVER_NAME);
+                       DRIVER_NAME);
        else
                pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
                        i2c_smbus_read_byte_data(&the_tps->client, TPS_VREGS1));
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
 
        return status;
 }
@@ -934,7 +931,7 @@ int tps65013_set_low_pwr(unsigned mode)
        if (!the_tps || the_tps->por)
                return -ENODEV;
 
-       down(&the_tps->lock);
+       mutex_lock(&the_tps->lock);
 
        pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
                DRIVER_NAME,
@@ -962,7 +959,7 @@ int tps65013_set_low_pwr(unsigned mode)
        if (status != 0) {
                printk(KERN_ERR "%s: Failed to write chconfig register\n",
         DRIVER_NAME);
-               up(&the_tps->lock);
+               mutex_unlock(&the_tps->lock);
                return status;
        }
 
@@ -980,7 +977,7 @@ int tps65013_set_low_pwr(unsigned mode)
                pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
                        i2c_smbus_read_byte_data(&the_tps->client, TPS_VDCDC1));
 
-       up(&the_tps->lock);
+       mutex_unlock(&the_tps->lock);
 
        return status;
 }
@@ -1009,7 +1006,7 @@ static int __init tps_init(void)
                msleep(10);
        }
 
-#if defined(CONFIG_ARM)
+#ifdef CONFIG_ARM
        if (machine_is_omap_osk()) {
 
                // FIXME: More should be placed in the initialization code