EEPROM: 93cx6: Header file cleanup
[safe/jmp/linux-2.6] / drivers / rtc / rtc-ds1307.c
index 1c975e6..c4ec5c1 100644 (file)
@@ -3,6 +3,7 @@
  *
  *  Copyright (C) 2005 James Chapman (ds1337 core)
  *  Copyright (C) 2006 David Brownell
+ *  Copyright (C) 2009 Matthias Fuchs (rx8025 support)
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -30,7 +31,10 @@ enum ds_type {
        ds_1338,
        ds_1339,
        ds_1340,
+       ds_1388,
+       ds_3231,
        m41t00,
+       rx_8025,
        // rs5c372 too?  different address...
 };
 
@@ -64,6 +68,7 @@ enum ds_type {
 #define DS1337_REG_CONTROL     0x0e
 #      define DS1337_BIT_nEOSC         0x80
 #      define DS1339_BIT_BBSQI         0x20
+#      define DS3231_BIT_BBSQW         0x40 /* same as BBSQI */
 #      define DS1337_BIT_RS2           0x10
 #      define DS1337_BIT_RS1           0x08
 #      define DS1337_BIT_INTCN         0x04
@@ -83,9 +88,16 @@ enum ds_type {
 #define DS1339_REG_ALARM1_SECS 0x07
 #define DS1339_REG_TRICKLE     0x10
 
+#define RX8025_REG_CTRL1       0x0e
+#      define RX8025_BIT_2412          0x20
+#define RX8025_REG_CTRL2       0x0f
+#      define RX8025_BIT_PON           0x10
+#      define RX8025_BIT_VDET          0x40
+#      define RX8025_BIT_XST           0x20
 
 
 struct ds1307 {
+       u8                      offset; /* register's offset */
        u8                      regs[11];
        enum ds_type            type;
        unsigned long           flags;
@@ -120,7 +132,12 @@ static const struct chip_desc chips[] = {
 },
 [ds_1340] = {
 },
+[ds_3231] = {
+       .alarm          = 1,
+},
 [m41t00] = {
+},
+[rx_8025] = {
 }, };
 
 static const struct i2c_device_id ds1307_id[] = {
@@ -128,8 +145,11 @@ static const struct i2c_device_id ds1307_id[] = {
        { "ds1337", ds_1337 },
        { "ds1338", ds_1338 },
        { "ds1339", ds_1339 },
+       { "ds1388", ds_1388 },
        { "ds1340", ds_1340 },
+       { "ds3231", ds_3231 },
        { "m41t00", m41t00 },
+       { "rx8025", rx_8025 },
        { }
 };
 MODULE_DEVICE_TABLE(i2c, ds1307_id);
@@ -247,12 +267,7 @@ static void ds1307_work(struct work_struct *work)
                control &= ~DS1337_BIT_A1IE;
                i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
 
-               /* rtc_update_irq() assumes that it is called
-                * from IRQ-disabled context.
-                */
-               local_irq_disable();
                rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
-               local_irq_enable();
        }
 
 out:
@@ -280,7 +295,7 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
 
        /* read the RTC date and time registers all at once */
        tmp = ds1307->read_block_data(ds1307->client,
-               DS1307_REG_SECS, 7, ds1307->regs);
+               ds1307->offset, 7, ds1307->regs);
        if (tmp != 7) {
                dev_err(dev, "%s error %d\n", "read", tmp);
                return -EIO;
@@ -342,6 +357,7 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
        switch (ds1307->type) {
        case ds_1337:
        case ds_1339:
+       case ds_3231:
                buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
                break;
        case ds_1340:
@@ -356,7 +372,8 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
                "write", buf[0], buf[1], buf[2], buf[3],
                buf[4], buf[5], buf[6]);
 
-       result = ds1307->write_block_data(ds1307->client, 0, 7, buf);
+       result = ds1307->write_block_data(ds1307->client,
+               ds1307->offset, 7, buf);
        if (result < 0) {
                dev_err(dev, "%s error %d\n", "write", result);
                return result;
@@ -613,6 +630,11 @@ static int __devinit ds1307_probe(struct i2c_client *client,
        struct i2c_adapter      *adapter = to_i2c_adapter(client->dev.parent);
        int                     want_irq = false;
        unsigned char           *buf;
+       static const int        bbsqi_bitpos[] = {
+               [ds_1337] = 0,
+               [ds_1339] = DS1339_BIT_BBSQI,
+               [ds_3231] = DS3231_BIT_BBSQW,
+       };
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
            && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
@@ -621,9 +643,12 @@ static int __devinit ds1307_probe(struct i2c_client *client,
        if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
                return -ENOMEM;
 
-       ds1307->client = client;
        i2c_set_clientdata(client, ds1307);
-       ds1307->type = id->driver_data;
+
+       ds1307->client  = client;
+       ds1307->type    = id->driver_data;
+       ds1307->offset  = 0;
+
        buf = ds1307->regs;
        if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
                ds1307->read_block_data = i2c_smbus_read_i2c_block_data;
@@ -636,6 +661,7 @@ static int __devinit ds1307_probe(struct i2c_client *client,
        switch (ds1307->type) {
        case ds_1337:
        case ds_1339:
+       case ds_3231:
                /* has IRQ? */
                if (ds1307->client->irq > 0 && chip->alarm) {
                        INIT_WORK(&ds1307->work, ds1307_work);
@@ -655,12 +681,12 @@ static int __devinit ds1307_probe(struct i2c_client *client,
                        ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
 
                /* Using IRQ?  Disable the square wave and both alarms.
-                * For ds1339, be sure alarms can trigger when we're
-                * running on Vbackup (BBSQI); we assume ds1337 will
-                * ignore that bit
+                * For some variants, be sure alarms can trigger when we're
+                * running on Vbackup (BBSQI/BBSQW)
                 */
                if (want_irq) {
-                       ds1307->regs[0] |= DS1337_BIT_INTCN | DS1339_BIT_BBSQI;
+                       ds1307->regs[0] |= DS1337_BIT_INTCN
+                                       | bbsqi_bitpos[ds1307->type];
                        ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
                }
 
@@ -674,6 +700,75 @@ static int __devinit ds1307_probe(struct i2c_client *client,
                        dev_warn(&client->dev, "SET TIME!\n");
                }
                break;
+
+       case rx_8025:
+               tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
+                               RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
+               if (tmp != 2) {
+                       pr_debug("read error %d\n", tmp);
+                       err = -EIO;
+                       goto exit_free;
+               }
+
+               /* oscillator off?  turn it on, so clock can tick. */
+               if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
+                       ds1307->regs[1] |= RX8025_BIT_XST;
+                       i2c_smbus_write_byte_data(client,
+                                                 RX8025_REG_CTRL2 << 4 | 0x08,
+                                                 ds1307->regs[1]);
+                       dev_warn(&client->dev,
+                                "oscillator stop detected - SET TIME!\n");
+               }
+
+               if (ds1307->regs[1] & RX8025_BIT_PON) {
+                       ds1307->regs[1] &= ~RX8025_BIT_PON;
+                       i2c_smbus_write_byte_data(client,
+                                                 RX8025_REG_CTRL2 << 4 | 0x08,
+                                                 ds1307->regs[1]);
+                       dev_warn(&client->dev, "power-on detected\n");
+               }
+
+               if (ds1307->regs[1] & RX8025_BIT_VDET) {
+                       ds1307->regs[1] &= ~RX8025_BIT_VDET;
+                       i2c_smbus_write_byte_data(client,
+                                                 RX8025_REG_CTRL2 << 4 | 0x08,
+                                                 ds1307->regs[1]);
+                       dev_warn(&client->dev, "voltage drop detected\n");
+               }
+
+               /* make sure we are running in 24hour mode */
+               if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
+                       u8 hour;
+
+                       /* switch to 24 hour mode */
+                       i2c_smbus_write_byte_data(client,
+                                                 RX8025_REG_CTRL1 << 4 | 0x08,
+                                                 ds1307->regs[0] |
+                                                 RX8025_BIT_2412);
+
+                       tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
+                                       RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
+                       if (tmp != 2) {
+                               pr_debug("read error %d\n", tmp);
+                               err = -EIO;
+                               goto exit_free;
+                       }
+
+                       /* correct hour */
+                       hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
+                       if (hour == 12)
+                               hour = 0;
+                       if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
+                               hour += 12;
+
+                       i2c_smbus_write_byte_data(client,
+                                                 DS1307_REG_HOUR << 4 | 0x08,
+                                                 hour);
+               }
+               break;
+       case ds_1388:
+               ds1307->offset = 1; /* Seconds starts at 1 */
+               break;
        default:
                break;
        }
@@ -734,8 +829,11 @@ 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:
                break;
        }
 
@@ -747,6 +845,8 @@ read_rtc:
                 * systems that will run through year 2100.
                 */
                break;
+       case rx_8025:
+               break;
        default:
                if (!(tmp & DS1307_BIT_12HR))
                        break;
@@ -774,13 +874,15 @@ read_rtc:
        }
 
        if (want_irq) {
-               err = request_irq(client->irq, ds1307_irq, 0,
+               err = request_irq(client->irq, ds1307_irq, IRQF_SHARED,
                          ds1307->rtc->name, client);
                if (err) {
                        dev_err(&client->dev,
                                "unable to request IRQ!\n");
                        goto exit_irq;
                }
+
+               device_set_wakeup_capable(&client->dev, 1);
                set_bit(HAS_ALARM, &ds1307->flags);
                dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
        }
@@ -796,8 +898,7 @@ read_rtc:
        return 0;
 
 exit_irq:
-       if (ds1307->rtc)
-               rtc_device_unregister(ds1307->rtc);
+       rtc_device_unregister(ds1307->rtc);
 exit_free:
        kfree(ds1307);
        return err;