rtc-cmos: export second NVRAM bank
[safe/jmp/linux-2.6] / drivers / rtc / rtc-x1205.c
index b3fae35..7dcfba1 100644 (file)
 #include <linux/rtc.h>
 #include <linux/delay.h>
 
-#define DRV_VERSION "1.0.7"
-
-/* Addresses to scan: none. This chip is located at
- * 0x6f and uses a two bytes register addressing.
- * Two bytes need to be written to read a single register,
- * while most other chips just require one and take the second
- * one as the data to be written. To prevent corrupting
- * unknown chips, the user must explicitly set the probe parameter.
- */
-
-static unsigned short normal_i2c[] = { I2C_CLIENT_END };
-
-/* Insmod parameters */
-I2C_CLIENT_INSMOD;
+#define DRV_VERSION "1.0.8"
 
 /* offsets into CCR area */
 
@@ -84,6 +71,7 @@ I2C_CLIENT_INSMOD;
 #define X1205_SR_RTCF          0x01    /* Clock failure */
 #define X1205_SR_WEL           0x02    /* Write Enable Latch */
 #define X1205_SR_RWEL          0x04    /* Register Write Enable */
+#define X1205_SR_AL0           0x20    /* Alarm 0 match */
 
 #define X1205_DTR_DTR0         0x01
 #define X1205_DTR_DTR1         0x02
@@ -91,19 +79,9 @@ I2C_CLIENT_INSMOD;
 
 #define X1205_HR_MIL           0x80    /* Set in ccr.hour for 24 hr mode */
 
-/* Prototypes */
-static int x1205_attach(struct i2c_adapter *adapter);
-static int x1205_detach(struct i2c_client *client);
-static int x1205_probe(struct i2c_adapter *adapter, int address, int kind);
+#define X1205_INT_AL0E         0x20    /* Alarm 0 enable */
 
-static struct i2c_driver x1205_driver = {
-       .driver         = {
-               .name   = "x1205",
-       },
-       .id             = I2C_DRIVERID_X1205,
-       .attach_adapter = &x1205_attach,
-       .detach_client  = &x1205_detach,
-};
+static struct i2c_driver x1205_driver;
 
 /*
  * In the routines that deal directly with the x1205 hardware, we use
@@ -114,8 +92,8 @@ static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
                                unsigned char reg_base)
 {
        unsigned char dt_addr[2] = { 0, reg_base };
-
        unsigned char buf[8];
+       int i;
 
        struct i2c_msg msgs[] = {
                { client->addr, 0, 2, dt_addr },        /* setup read ptr */
@@ -123,18 +101,23 @@ static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
        };
 
        /* read date registers */
-       if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
-               dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
+       if (i2c_transfer(client->adapter, &msgs[0], 2) != 2) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
                return -EIO;
        }
 
        dev_dbg(&client->dev,
                "%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
                "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
-               __FUNCTION__,
+               __func__,
                buf[0], buf[1], buf[2], buf[3],
                buf[4], buf[5], buf[6], buf[7]);
 
+       /* Mask out the enable bits if these are alarm registers */
+       if (reg_base < X1205_CCR_BASE)
+               for (i = 0; i <= 4; i++)
+                       buf[i] &= 0x7F;
+
        tm->tm_sec = BCD2BIN(buf[CCR_SEC]);
        tm->tm_min = BCD2BIN(buf[CCR_MIN]);
        tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
@@ -146,7 +129,7 @@ static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
 
        dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
                "mday=%d, mon=%d, year=%d, wday=%d\n",
-               __FUNCTION__,
+               __func__,
                tm->tm_sec, tm->tm_min, tm->tm_hour,
                tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
 
@@ -163,8 +146,8 @@ static int x1205_get_status(struct i2c_client *client, unsigned char *sr)
        };
 
        /* read status register */
-       if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
-               dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
+       if (i2c_transfer(client->adapter, &msgs[0], 2) != 2) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
                return -EIO;
        }
 
@@ -172,10 +155,11 @@ static int x1205_get_status(struct i2c_client *client, unsigned char *sr)
 }
 
 static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
-                               int datetoo, u8 reg_base)
+                       int datetoo, u8 reg_base, unsigned char alm_enable)
 {
-       int i, xfer;
+       int i, xfer, nbytes;
        unsigned char buf[8];
+       unsigned char rdata[10] = { 0, reg_base };
 
        static const unsigned char wel[3] = { 0, X1205_REG_SR,
                                                X1205_SR_WEL };
@@ -187,7 +171,7 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
 
        dev_dbg(&client->dev,
                "%s: secs=%d, mins=%d, hours=%d\n",
-               __FUNCTION__,
+               __func__,
                tm->tm_sec, tm->tm_min, tm->tm_hour);
 
        buf[CCR_SEC] = BIN2BCD(tm->tm_sec);
@@ -200,7 +184,7 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
        if (datetoo) {
                dev_dbg(&client->dev,
                        "%s: mday=%d, mon=%d, year=%d, wday=%d\n",
-                       __FUNCTION__,
+                       __func__,
                        tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
 
                buf[CCR_MDAY] = BIN2BCD(tm->tm_mday);
@@ -214,34 +198,77 @@ static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
                buf[CCR_Y2K] = BIN2BCD(tm->tm_year / 100);
        }
 
+       /* If writing alarm registers, set compare bits on registers 0-4 */
+       if (reg_base < X1205_CCR_BASE)
+               for (i = 0; i <= 4; i++)
+                       buf[i] |= 0x80;
+
        /* this sequence is required to unlock the chip */
        if ((xfer = i2c_master_send(client, wel, 3)) != 3) {
-               dev_err(&client->dev, "%s: wel - %d\n", __FUNCTION__, xfer);
+               dev_err(&client->dev, "%s: wel - %d\n", __func__, xfer);
                return -EIO;
        }
 
        if ((xfer = i2c_master_send(client, rwel, 3)) != 3) {
-               dev_err(&client->dev, "%s: rwel - %d\n", __FUNCTION__, xfer);
+               dev_err(&client->dev, "%s: rwel - %d\n", __func__, xfer);
                return -EIO;
        }
 
+
        /* write register's data */
-       for (i = 0; i < (datetoo ? 8 : 3); i++) {
-               unsigned char rdata[3] = { 0, reg_base + i, buf[i] };
+       if (datetoo)
+               nbytes = 8;
+       else
+               nbytes = 3;
+       for (i = 0; i < nbytes; i++)
+               rdata[2+i] = buf[i];
+
+       xfer = i2c_master_send(client, rdata, nbytes+2);
+       if (xfer != nbytes+2) {
+               dev_err(&client->dev,
+                       "%s: result=%d addr=%02x, data=%02x\n",
+                       __func__,
+                        xfer, rdata[1], rdata[2]);
+               return -EIO;
+       }
+
+       /* If we wrote to the nonvolatile region, wait 10msec for write cycle*/
+       if (reg_base < X1205_CCR_BASE) {
+               unsigned char al0e[3] = { 0, X1205_REG_INT, 0 };
+
+               msleep(10);
+
+               /* ...and set or clear the AL0E bit in the INT register */
 
-               xfer = i2c_master_send(client, rdata, 3);
+               /* Need to set RWEL again as the write has cleared it */
+               xfer = i2c_master_send(client, rwel, 3);
                if (xfer != 3) {
                        dev_err(&client->dev,
-                               "%s: xfer=%d addr=%02x, data=%02x\n",
-                               __FUNCTION__,
-                                xfer, rdata[1], rdata[2]);
+                               "%s: aloe rwel - %d\n",
+                               __func__,
+                               xfer);
+                       return -EIO;
+               }
+
+               if (alm_enable)
+                       al0e[2] = X1205_INT_AL0E;
+
+               xfer = i2c_master_send(client, al0e, 3);
+               if (xfer != 3) {
+                       dev_err(&client->dev,
+                               "%s: al0e - %d\n",
+                               __func__,
+                               xfer);
                        return -EIO;
                }
-       };
+
+               /* and wait 10msec again for this write to complete */
+               msleep(10);
+       }
 
        /* disable further writes */
        if ((xfer = i2c_master_send(client, diswe, 3)) != 3) {
-               dev_err(&client->dev, "%s: diswe - %d\n", __FUNCTION__, xfer);
+               dev_err(&client->dev, "%s: diswe - %d\n", __func__, xfer);
                return -EIO;
        }
 
@@ -255,9 +282,9 @@ static int x1205_fix_osc(struct i2c_client *client)
 
        tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
 
-       if ((err = x1205_set_datetime(client, &tm, 0, X1205_CCR_BASE)) < 0)
-               dev_err(&client->dev,
-                       "unable to restart the oscillator\n");
+       err = x1205_set_datetime(client, &tm, 0, X1205_CCR_BASE, 0);
+       if (err < 0)
+               dev_err(&client->dev, "unable to restart the oscillator\n");
 
        return err;
 }
@@ -273,12 +300,12 @@ static int x1205_get_dtrim(struct i2c_client *client, int *trim)
        };
 
        /* read dtr register */
-       if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
-               dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
+       if (i2c_transfer(client->adapter, &msgs[0], 2) != 2) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
                return -EIO;
        }
 
-       dev_dbg(&client->dev, "%s: raw dtr=%x\n", __FUNCTION__, dtr);
+       dev_dbg(&client->dev, "%s: raw dtr=%x\n", __func__, dtr);
 
        *trim = 0;
 
@@ -305,12 +332,12 @@ static int x1205_get_atrim(struct i2c_client *client, int *trim)
        };
 
        /* read atr register */
-       if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
-               dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
+       if (i2c_transfer(client->adapter, &msgs[0], 2) != 2) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
                return -EIO;
        }
 
-       dev_dbg(&client->dev, "%s: raw atr=%x\n", __FUNCTION__, atr);
+       dev_dbg(&client->dev, "%s: raw atr=%x\n", __func__, atr);
 
        /* atr is a two's complement value on 6 bits,
         * perform sign extension. The formula is
@@ -319,11 +346,11 @@ static int x1205_get_atrim(struct i2c_client *client, int *trim)
        if (atr & 0x20)
                atr |= 0xC0;
 
-       dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __FUNCTION__, atr, atr);
+       dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __func__, atr, atr);
 
        *trim = (atr * 250) + 11000;
 
-       dev_dbg(&client->dev, "%s: real=%d\n", __FUNCTION__, *trim);
+       dev_dbg(&client->dev, "%s: real=%d\n", __func__, *trim);
 
        return 0;
 }
@@ -377,7 +404,7 @@ static int x1205_validate_client(struct i2c_client *client)
                if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) {
                        dev_err(&client->dev,
                                "%s: could not read register %x\n",
-                               __FUNCTION__, probe_zero_pattern[i]);
+                               __func__, probe_zero_pattern[i]);
 
                        return -EIO;
                }
@@ -385,7 +412,7 @@ static int x1205_validate_client(struct i2c_client *client)
                if ((buf & probe_zero_pattern[i+1]) != 0) {
                        dev_err(&client->dev,
                                "%s: register=%02x, zero pattern=%d, value=%x\n",
-                               __FUNCTION__, probe_zero_pattern[i], i, buf);
+                               __func__, probe_zero_pattern[i], i, buf);
 
                        return -ENODEV;
                }
@@ -405,7 +432,7 @@ static int x1205_validate_client(struct i2c_client *client)
                if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) {
                        dev_err(&client->dev,
                                "%s: could not read register %x\n",
-                               __FUNCTION__, probe_limits_pattern[i].reg);
+                               __func__, probe_limits_pattern[i].reg);
 
                        return -EIO;
                }
@@ -416,7 +443,7 @@ static int x1205_validate_client(struct i2c_client *client)
                        value < probe_limits_pattern[i].min) {
                        dev_dbg(&client->dev,
                                "%s: register=%x, lim pattern=%d, value=%d\n",
-                               __FUNCTION__, probe_limits_pattern[i].reg,
+                               __func__, probe_limits_pattern[i].reg,
                                i, value);
 
                        return -ENODEV;
@@ -428,14 +455,33 @@ static int x1205_validate_client(struct i2c_client *client)
 
 static int x1205_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
-       return x1205_get_datetime(to_i2c_client(dev),
-               &alrm->time, X1205_ALM0_BASE);
+       int err;
+       unsigned char intreg, status;
+       static unsigned char int_addr[2] = { 0, X1205_REG_INT };
+       struct i2c_client *client = to_i2c_client(dev);
+       struct i2c_msg msgs[] = {
+               { client->addr, 0, 2, int_addr },        /* setup read ptr */
+               { client->addr, I2C_M_RD, 1, &intreg },  /* read INT register */
+       };
+
+       /* read interrupt register and status register */
+       if (i2c_transfer(client->adapter, &msgs[0], 2) != 2) {
+               dev_err(&client->dev, "%s: read error\n", __func__);
+               return -EIO;
+       }
+       err = x1205_get_status(client, &status);
+       if (err == 0) {
+               alrm->pending = (status & X1205_SR_AL0) ? 1 : 0;
+               alrm->enabled = (intreg & X1205_INT_AL0E) ? 1 : 0;
+               err = x1205_get_datetime(client, &alrm->time, X1205_ALM0_BASE);
+       }
+       return err;
 }
 
 static int x1205_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
        return x1205_set_datetime(to_i2c_client(dev),
-               &alrm->time, 1, X1205_ALM0_BASE);
+               &alrm->time, 1, X1205_ALM0_BASE, alrm->enabled);
 }
 
 static int x1205_rtc_read_time(struct device *dev, struct rtc_time *tm)
@@ -447,7 +493,7 @@ static int x1205_rtc_read_time(struct device *dev, struct rtc_time *tm)
 static int x1205_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
        return x1205_set_datetime(to_i2c_client(dev),
-               tm, 1, X1205_CCR_BASE);
+               tm, 1, X1205_CCR_BASE, 0);
 }
 
 static int x1205_rtc_proc(struct device *dev, struct seq_file *seq)
@@ -497,58 +543,50 @@ static ssize_t x1205_sysfs_show_dtrim(struct device *dev,
 }
 static DEVICE_ATTR(dtrim, S_IRUGO, x1205_sysfs_show_dtrim, NULL);
 
-static int x1205_attach(struct i2c_adapter *adapter)
+static int x1205_sysfs_register(struct device *dev)
 {
-       return i2c_probe(adapter, &addr_data, x1205_probe);
+       int err;
+
+       err = device_create_file(dev, &dev_attr_atrim);
+       if (err)
+               return err;
+
+       err = device_create_file(dev, &dev_attr_dtrim);
+       if (err)
+               device_remove_file(dev, &dev_attr_atrim);
+
+       return err;
 }
 
-static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
+static void x1205_sysfs_unregister(struct device *dev)
+{
+       device_remove_file(dev, &dev_attr_atrim);
+       device_remove_file(dev, &dev_attr_dtrim);
+}
+
+
+static int x1205_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
 {
        int err = 0;
        unsigned char sr;
-       struct i2c_client *client;
        struct rtc_device *rtc;
 
-       dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
+       dev_dbg(&client->dev, "%s\n", __func__);
 
-       if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
-               err = -ENODEV;
-               goto exit;
-       }
-
-       if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
-       }
-
-       /* I2C client */
-       client->addr = address;
-       client->driver = &x1205_driver;
-       client->adapter = adapter;
-
-       strlcpy(client->name, x1205_driver.driver.name, I2C_NAME_SIZE);
-
-       /* Verify the chip is really an X1205 */
-       if (kind < 0) {
-               if (x1205_validate_client(client) < 0) {
-                       err = -ENODEV;
-                       goto exit_kfree;
-               }
-       }
+       if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+               return -ENODEV;
 
-       /* Inform the i2c layer */
-       if ((err = i2c_attach_client(client)))
-               goto exit_kfree;
+       if (x1205_validate_client(client) < 0)
+               return -ENODEV;
 
        dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
 
        rtc = rtc_device_register(x1205_driver.driver.name, &client->dev,
                                &x1205_rtc_ops, THIS_MODULE);
 
-       if (IS_ERR(rtc)) {
-               err = PTR_ERR(rtc);
-               goto exit_detach;
-       }
+       if (IS_ERR(rtc))
+               return PTR_ERR(rtc);
 
        i2c_set_clientdata(client, rtc);
 
@@ -565,45 +603,42 @@ static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
        else
                dev_err(&client->dev, "couldn't read status\n");
 
-       err = device_create_file(&client->dev, &dev_attr_atrim);
-       if (err) goto exit_devreg;
-       err = device_create_file(&client->dev, &dev_attr_dtrim);
-       if (err) goto exit_atrim;
+       err = x1205_sysfs_register(&client->dev);
+       if (err)
+               goto exit_devreg;
 
        return 0;
 
-exit_atrim:
-       device_remove_file(&client->dev, &dev_attr_atrim);
-
 exit_devreg:
        rtc_device_unregister(rtc);
 
-exit_detach:
-       i2c_detach_client(client);
-
-exit_kfree:
-       kfree(client);
-
-exit:
        return err;
 }
 
-static int x1205_detach(struct i2c_client *client)
+static int x1205_remove(struct i2c_client *client)
 {
-       int err;
        struct rtc_device *rtc = i2c_get_clientdata(client);
 
-       if (rtc)
-               rtc_device_unregister(rtc);
-
-       if ((err = i2c_detach_client(client)))
-               return err;
-
-       kfree(client);
-
+       rtc_device_unregister(rtc);
+       x1205_sysfs_unregister(&client->dev);
        return 0;
 }
 
+static const struct i2c_device_id x1205_id[] = {
+       { "x1205", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, x1205_id);
+
+static struct i2c_driver x1205_driver = {
+       .driver         = {
+               .name   = "rtc-x1205",
+       },
+       .probe          = x1205_probe,
+       .remove         = x1205_remove,
+       .id_table       = x1205_id,
+};
+
 static int __init x1205_init(void)
 {
        return i2c_add_driver(&x1205_driver);