Revert "Merge commit 'main-jb-2012.08.03-B4' into t114-0806"
[linux-2.6.git] / drivers / rtc / rtc-ds1307.c
index d827ce5..c293d0c 100644 (file)
@@ -20,7 +20,8 @@
 
 
 
-/* We can't determine type by probing, but if we expect pre-Linux code
+/*
+ * We can't determine type by probing, but if we expect pre-Linux code
  * to have set the chip up as a clock (turning on the oscillator and
  * setting the date and time), Linux can ignore the non-clock features.
  * That's a natural job for a factory or repair bench.
@@ -34,8 +35,10 @@ enum ds_type {
        ds_1388,
        ds_3231,
        m41t00,
+       mcp7941x,
        rx_8025,
-       // rs5c372 too?  different address...
+       last_ds_type /* always last */
+       /* rs5c372 too?  different address... */
 };
 
 
@@ -43,6 +46,7 @@ enum ds_type {
 #define DS1307_REG_SECS                0x00    /* 00-59 */
 #      define DS1307_BIT_CH            0x80
 #      define DS1340_BIT_nEOSC         0x80
+#      define MCP7941X_BIT_ST          0x80
 #define DS1307_REG_MIN         0x01    /* 00-59 */
 #define DS1307_REG_HOUR                0x02    /* 00-23, or 1-12{am,pm} */
 #      define DS1307_BIT_12HR          0x40    /* in REG_HOUR */
@@ -50,12 +54,14 @@ enum ds_type {
 #      define DS1340_BIT_CENTURY_EN    0x80    /* in REG_HOUR */
 #      define DS1340_BIT_CENTURY       0x40    /* in REG_HOUR */
 #define DS1307_REG_WDAY                0x03    /* 01-07 */
+#      define MCP7941X_BIT_VBATEN      0x08
 #define DS1307_REG_MDAY                0x04    /* 01-31 */
 #define DS1307_REG_MONTH       0x05    /* 01-12 */
 #      define DS1337_BIT_CENTURY       0x80    /* in REG_MONTH */
 #define DS1307_REG_YEAR                0x06    /* 00-99 */
 
-/* Other registers (control, status, alarms, trickle charge, NVRAM, etc)
+/*
+ * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  * start at 7, and they differ a LOT. Only control and status matter for
  * basic RTC date and time functionality; be careful using them.
  */
@@ -99,6 +105,8 @@ enum ds_type {
 struct ds1307 {
        u8                      offset; /* register's offset */
        u8                      regs[11];
+       u16                     nvram_offset;
+       struct bin_attribute    *nvram;
        enum ds_type            type;
        unsigned long           flags;
 #define HAS_NVRAM      0               /* bit 0 == sysfs file active */
@@ -106,39 +114,42 @@ struct ds1307 {
        struct i2c_client       *client;
        struct rtc_device       *rtc;
        struct work_struct      work;
-       s32 (*read_block_data)(struct i2c_client *client, u8 command,
+       s32 (*read_block_data)(const struct i2c_client *client, u8 command,
                               u8 length, u8 *values);
-       s32 (*write_block_data)(struct i2c_client *client, u8 command,
+       s32 (*write_block_data)(const struct i2c_client *client, u8 command,
                                u8 length, const u8 *values);
 };
 
 struct chip_desc {
-       unsigned                nvram56:1;
        unsigned                alarm:1;
+       u16                     nvram_offset;
+       u16                     nvram_size;
 };
 
-static const struct chip_desc chips[] = {
-[ds_1307] = {
-       .nvram56        = 1,
-},
-[ds_1337] = {
-       .alarm          = 1,
-},
-[ds_1338] = {
-       .nvram56        = 1,
-},
-[ds_1339] = {
-       .alarm          = 1,
-},
-[ds_1340] = {
-},
-[ds_3231] = {
-       .alarm          = 1,
-},
-[m41t00] = {
-},
-[rx_8025] = {
-}, };
+static const struct chip_desc chips[last_ds_type] = {
+       [ds_1307] = {
+               .nvram_offset   = 8,
+               .nvram_size     = 56,
+       },
+       [ds_1337] = {
+               .alarm          = 1,
+       },
+       [ds_1338] = {
+               .nvram_offset   = 8,
+               .nvram_size     = 56,
+       },
+       [ds_1339] = {
+               .alarm          = 1,
+       },
+       [ds_3231] = {
+               .alarm          = 1,
+       },
+       [mcp7941x] = {
+               /* this is battery backed SRAM */
+               .nvram_offset   = 0x20,
+               .nvram_size     = 0x40,
+       },
+};
 
 static const struct i2c_device_id ds1307_id[] = {
        { "ds1307", ds_1307 },
@@ -149,6 +160,8 @@ static const struct i2c_device_id ds1307_id[] = {
        { "ds1340", ds_1340 },
        { "ds3231", ds_3231 },
        { "m41t00", m41t00 },
+       { "mcp7941x", mcp7941x },
+       { "pt7c4338", ds_1307 },
        { "rx8025", rx_8025 },
        { }
 };
@@ -158,8 +171,8 @@ MODULE_DEVICE_TABLE(i2c, ds1307_id);
 
 #define BLOCK_DATA_MAX_TRIES 10
 
-static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command,
-                                 u8 length, u8 *values)
+static s32 ds1307_read_block_data_once(const struct i2c_client *client,
+                                      u8 command, u8 length, u8 *values)
 {
        s32 i, data;
 
@@ -172,7 +185,7 @@ static s32 ds1307_read_block_data_once(struct i2c_client *client, u8 command,
        return i;
 }
 
-static s32 ds1307_read_block_data(struct i2c_client *client, u8 command,
+static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
                                  u8 length, u8 *values)
 {
        u8 oldvalues[I2C_SMBUS_BLOCK_MAX];
@@ -198,7 +211,7 @@ static s32 ds1307_read_block_data(struct i2c_client *client, u8 command,
        return length;
 }
 
-static s32 ds1307_write_block_data(struct i2c_client *client, u8 command,
+static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
                                   u8 length, const u8 *values)
 {
        u8 currvalues[I2C_SMBUS_BLOCK_MAX];
@@ -364,6 +377,15 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
                buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
                                | DS1340_BIT_CENTURY;
                break;
+       case mcp7941x:
+               /*
+                * these bits were cleared when preparing the date/time
+                * values and need to be set again before writing the
+                * buffer out to the device.
+                */
+               buf[DS1307_REG_SECS] |= MCP7941X_BIT_ST;
+               buf[DS1307_REG_WDAY] |= MCP7941X_BIT_VBATEN;
+               break;
        default:
                break;
        }
@@ -406,7 +428,8 @@ static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
                        ds1307->regs[6], ds1307->regs[7],
                        ds1307->regs[8]);
 
-       /* report alarm time (ALARM1); assume 24 hour and day-of-month modes,
+       /*
+        * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
         * and that all four fields are checked matches
         */
        t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
@@ -434,7 +457,7 @@ static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
 
 static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
 {
-       struct i2c_client       *client = to_i2c_client(dev);
+       struct i2c_client       *client = to_i2c_client(dev);
        struct ds1307           *ds1307 = i2c_get_clientdata(client);
        unsigned char           *buf = ds1307->regs;
        u8                      control, status;
@@ -495,50 +518,27 @@ static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
        return 0;
 }
 
-static int ds1307_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
 {
        struct i2c_client       *client = to_i2c_client(dev);
        struct ds1307           *ds1307 = i2c_get_clientdata(client);
        int                     ret;
 
-       switch (cmd) {
-       case RTC_AIE_OFF:
-               if (!test_bit(HAS_ALARM, &ds1307->flags))
-                       return -ENOTTY;
-
-               ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
-               if (ret < 0)
-                       return ret;
-
-               ret &= ~DS1337_BIT_A1IE;
-
-               ret = i2c_smbus_write_byte_data(client,
-                                               DS1337_REG_CONTROL, ret);
-               if (ret < 0)
-                       return ret;
-
-               break;
-
-       case RTC_AIE_ON:
-               if (!test_bit(HAS_ALARM, &ds1307->flags))
-                       return -ENOTTY;
+       if (!test_bit(HAS_ALARM, &ds1307->flags))
+               return -ENOTTY;
 
-               ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
-               if (ret < 0)
-                       return ret;
+       ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
+       if (ret < 0)
+               return ret;
 
+       if (enabled)
                ret |= DS1337_BIT_A1IE;
+       else
+               ret &= ~DS1337_BIT_A1IE;
 
-               ret = i2c_smbus_write_byte_data(client,
-                                               DS1337_REG_CONTROL, ret);
-               if (ret < 0)
-                       return ret;
-
-               break;
-
-       default:
-               return -ENOIOCTLCMD;
-       }
+       ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
+       if (ret < 0)
+               return ret;
 
        return 0;
 }
@@ -548,13 +548,11 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
        .set_time       = ds1307_set_time,
        .read_alarm     = ds1337_read_alarm,
        .set_alarm      = ds1337_set_alarm,
-       .ioctl          = ds1307_ioctl,
+       .alarm_irq_enable = ds1307_alarm_irq_enable,
 };
 
 /*----------------------------------------------------------------------*/
 
-#define NVRAM_SIZE     56
-
 static ssize_t
 ds1307_nvram_read(struct file *filp, struct kobject *kobj,
                struct bin_attribute *attr,
@@ -567,14 +565,15 @@ ds1307_nvram_read(struct file *filp, struct kobject *kobj,
        client = kobj_to_i2c_client(kobj);
        ds1307 = i2c_get_clientdata(client);
 
-       if (unlikely(off >= NVRAM_SIZE))
+       if (unlikely(off >= ds1307->nvram->size))
                return 0;
-       if ((off + count) > NVRAM_SIZE)
-               count = NVRAM_SIZE - off;
+       if ((off + count) > ds1307->nvram->size)
+               count = ds1307->nvram->size - off;
        if (unlikely(!count))
                return count;
 
-       result = ds1307->read_block_data(client, 8 + off, count, buf);
+       result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
+                                                               count, buf);
        if (result < 0)
                dev_err(&client->dev, "%s error %d\n", "nvram read", result);
        return result;
@@ -592,14 +591,15 @@ ds1307_nvram_write(struct file *filp, struct kobject *kobj,
        client = kobj_to_i2c_client(kobj);
        ds1307 = i2c_get_clientdata(client);
 
-       if (unlikely(off >= NVRAM_SIZE))
+       if (unlikely(off >= ds1307->nvram->size))
                return -EFBIG;
-       if ((off + count) > NVRAM_SIZE)
-               count = NVRAM_SIZE - off;
+       if ((off + count) > ds1307->nvram->size)
+               count = ds1307->nvram->size - off;
        if (unlikely(!count))
                return count;
 
-       result = ds1307->write_block_data(client, 8 + off, count, buf);
+       result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
+                                                               count, buf);
        if (result < 0) {
                dev_err(&client->dev, "%s error %d\n", "nvram write", result);
                return result;
@@ -607,21 +607,8 @@ ds1307_nvram_write(struct file *filp, struct kobject *kobj,
        return count;
 }
 
-static struct bin_attribute nvram = {
-       .attr = {
-               .name   = "nvram",
-               .mode   = S_IRUGO | S_IWUSR,
-       },
-
-       .read   = ds1307_nvram_read,
-       .write  = ds1307_nvram_write,
-       .size   = NVRAM_SIZE,
-};
-
 /*----------------------------------------------------------------------*/
 
-static struct i2c_driver ds1307_driver;
-
 static int __devinit ds1307_probe(struct i2c_client *client,
                                  const struct i2c_device_id *id)
 {
@@ -642,7 +629,8 @@ static int __devinit ds1307_probe(struct i2c_client *client,
            && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
                return -EIO;
 
-       if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
+       ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL);
+       if (!ds1307)
                return -ENOMEM;
 
        i2c_set_clientdata(client, ds1307);
@@ -664,11 +652,6 @@ static int __devinit ds1307_probe(struct i2c_client *client,
        case ds_1337:
        case ds_1339:
        case ds_3231:
-               /* has IRQ? */
-               if (ds1307->client->irq > 0 && chip->alarm) {
-                       INIT_WORK(&ds1307->work, ds1307_work);
-                       want_irq = true;
-               }
                /* get registers that the "rtc" read below won't read... */
                tmp = ds1307->read_block_data(ds1307->client,
                                DS1337_REG_CONTROL, 2, buf);
@@ -682,14 +665,19 @@ static int __devinit ds1307_probe(struct i2c_client *client,
                if (ds1307->regs[0] & DS1337_BIT_nEOSC)
                        ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
 
-               /* Using IRQ?  Disable the square wave and both alarms.
+               /*
+                * Using IRQ?  Disable the square wave and both alarms.
                 * For some variants, be sure alarms can trigger when we're
                 * running on Vbackup (BBSQI/BBSQW)
                 */
-               if (want_irq) {
+               if (ds1307->client->irq > 0 && chip->alarm) {
+                       INIT_WORK(&ds1307->work, ds1307_work);
+
                        ds1307->regs[0] |= DS1337_BIT_INTCN
                                        | bbsqi_bitpos[ds1307->type];
                        ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
+
+                       want_irq = true;
                }
 
                i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
@@ -784,7 +772,8 @@ read_rtc:
                goto exit_free;
        }
 
-       /* minimal sanity checking; some chips (like DS1340) don't
+       /*
+        * minimal sanity checking; some chips (like DS1340) don't
         * specify the extra bits as must-be-zero, but there are
         * still a few values that are clearly out-of-range.
         */
@@ -831,11 +820,24 @@ read_rtc:
                        dev_warn(&client->dev, "SET TIME!\n");
                }
                break;
-       case rx_8025:
-       case ds_1337:
-       case ds_1339:
-       case ds_1388:
-       case ds_3231:
+       case mcp7941x:
+               /* make sure that the backup battery is enabled */
+               if (!(ds1307->regs[DS1307_REG_WDAY] & MCP7941X_BIT_VBATEN)) {
+                       i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
+                                       ds1307->regs[DS1307_REG_WDAY]
+                                       | MCP7941X_BIT_VBATEN);
+               }
+
+               /* clock halted?  turn it on, so clock can tick. */
+               if (!(tmp & MCP7941X_BIT_ST)) {
+                       i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
+                                       MCP7941X_BIT_ST);
+                       dev_warn(&client->dev, "SET TIME!\n");
+                       goto read_rtc;
+               }
+
+               break;
+       default:
                break;
        }
 
@@ -843,7 +845,8 @@ read_rtc:
        switch (ds1307->type) {
        case ds_1340:
        case m41t00:
-               /* NOTE: ignores century bits; fix before deploying
+               /*
+                * NOTE: ignores century bits; fix before deploying
                 * systems that will run through year 2100.
                 */
                break;
@@ -853,7 +856,8 @@ read_rtc:
                if (!(tmp & DS1307_BIT_12HR))
                        break;
 
-               /* Be sure we're in 24 hour mode.  Multi-master systems
+               /*
+                * Be sure we're in 24 hour mode.  Multi-master systems
                 * take note...
                 */
                tmp = bcd2bin(tmp & 0x1f);
@@ -889,16 +893,32 @@ read_rtc:
                dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
        }
 
-       if (chip->nvram56) {
-               err = sysfs_create_bin_file(&client->dev.kobj, &nvram);
-               if (err == 0) {
-                       set_bit(HAS_NVRAM, &ds1307->flags);
-                       dev_info(&client->dev, "56 bytes nvram\n");
+       if (chip->nvram_size) {
+               ds1307->nvram = kzalloc(sizeof(struct bin_attribute),
+                                                       GFP_KERNEL);
+               if (!ds1307->nvram) {
+                       err = -ENOMEM;
+                       goto exit_nvram;
                }
+               ds1307->nvram->attr.name = "nvram";
+               ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
+               sysfs_bin_attr_init(ds1307->nvram);
+               ds1307->nvram->read = ds1307_nvram_read,
+               ds1307->nvram->write = ds1307_nvram_write,
+               ds1307->nvram->size = chip->nvram_size;
+               ds1307->nvram_offset = chip->nvram_offset;
+               err = sysfs_create_bin_file(&client->dev.kobj, ds1307->nvram);
+               if (err) {
+                       kfree(ds1307->nvram);
+                       goto exit_nvram;
+               }
+               set_bit(HAS_NVRAM, &ds1307->flags);
+               dev_info(&client->dev, "%zu bytes nvram\n", ds1307->nvram->size);
        }
 
        return 0;
 
+exit_nvram:
 exit_irq:
        rtc_device_unregister(ds1307->rtc);
 exit_free:
@@ -908,15 +928,17 @@ exit_free:
 
 static int __devexit ds1307_remove(struct i2c_client *client)
 {
-       struct ds1307           *ds1307 = i2c_get_clientdata(client);
+       struct ds1307 *ds1307 = i2c_get_clientdata(client);
 
        if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
                free_irq(client->irq, client);
                cancel_work_sync(&ds1307->work);
        }
 
-       if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
-               sysfs_remove_bin_file(&client->dev.kobj, &nvram);
+       if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags)) {
+               sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
+               kfree(ds1307->nvram);
+       }
 
        rtc_device_unregister(ds1307->rtc);
        kfree(ds1307);
@@ -933,17 +955,7 @@ static struct i2c_driver ds1307_driver = {
        .id_table       = ds1307_id,
 };
 
-static int __init ds1307_init(void)
-{
-       return i2c_add_driver(&ds1307_driver);
-}
-module_init(ds1307_init);
-
-static void __exit ds1307_exit(void)
-{
-       i2c_del_driver(&ds1307_driver);
-}
-module_exit(ds1307_exit);
+module_i2c_driver(ds1307_driver);
 
 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
 MODULE_LICENSE("GPL");