rtc-ds1307: SMBus compatibility
authorBARRE Sebastien <sbarre@sdelcc.com>
Thu, 8 Jan 2009 02:07:13 +0000 (18:07 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 8 Jan 2009 16:30:59 +0000 (08:30 -0800)
Change i2c access functions to SMBus access functions in order to use the
ds1307 with SMBus adapter.

Signed-off-by: Sebastien Barre <sbarre@sdelcc.com>
Acked-by: David Brownell <david-b@pacbell.net>
Tested-by: David Brownell <david-b@pacbell.net>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
Acked-by: Jean Delvare <khali@linux-fr.org>
Cc: Rodolfo Giometti <giometti@enneenne.com>
Tested-by: Sebastien Barre <sbarre@sdelcc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/rtc/rtc-ds1307.c

index 162330b..fec13e7 100644 (file)
@@ -86,13 +86,11 @@ enum ds_type {
 
 
 struct ds1307 {
-       u8                      reg_addr;
        u8                      regs[11];
        enum ds_type            type;
        unsigned long           flags;
 #define HAS_NVRAM      0               /* bit 0 == sysfs file active */
 #define HAS_ALARM      1               /* bit 1 == irq claimed */
-       struct i2c_msg          msg[2];
        struct i2c_client       *client;
        struct rtc_device       *rtc;
        struct work_struct      work;
@@ -204,13 +202,9 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
        int             tmp;
 
        /* read the RTC date and time registers all at once */
-       ds1307->reg_addr = 0;
-       ds1307->msg[1].flags = I2C_M_RD;
-       ds1307->msg[1].len = 7;
-
-       tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
-                       ds1307->msg, 2);
-       if (tmp != 2) {
+       tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
+               DS1307_REG_SECS, 7, ds1307->regs);
+       if (tmp != 7) {
                dev_err(dev, "%s error %d\n", "read", tmp);
                return -EIO;
        }
@@ -257,7 +251,6 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
                t->tm_hour, t->tm_mday,
                t->tm_mon, t->tm_year, t->tm_wday);
 
-       *buf++ = 0;             /* first register addr */
        buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
        buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
        buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
@@ -282,18 +275,14 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
                break;
        }
 
-       ds1307->msg[1].flags = 0;
-       ds1307->msg[1].len = 8;
-
        dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n",
                "write", buf[0], buf[1], buf[2], buf[3],
                buf[4], buf[5], buf[6]);
 
-       result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent),
-                       &ds1307->msg[1], 1);
-       if (result != 1) {
-               dev_err(dev, "%s error %d\n", "write", tmp);
-               return -EIO;
+       result = i2c_smbus_write_i2c_block_data(ds1307->client, 0, 7, buf);
+       if (result < 0) {
+               dev_err(dev, "%s error %d\n", "write", result);
+               return result;
        }
        return 0;
 }
@@ -308,13 +297,9 @@ static int ds1307_read_alarm(struct device *dev, struct rtc_wkalrm *t)
                return -EINVAL;
 
        /* read all ALARM1, ALARM2, and status registers at once */
-       ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
-       ds1307->msg[1].flags = I2C_M_RD;
-       ds1307->msg[1].len = 9;
-
-       ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
-                       ds1307->msg, 2);
-       if (ret != 2) {
+       ret = i2c_smbus_read_i2c_block_data(client,
+                       DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
+       if (ret != 9) {
                dev_err(dev, "%s error %d\n", "alarm read", ret);
                return -EIO;
        }
@@ -371,13 +356,9 @@ static int ds1307_set_alarm(struct device *dev, struct rtc_wkalrm *t)
                t->enabled, t->pending);
 
        /* read current status of both alarms and the chip */
-       ds1307->reg_addr = DS1339_REG_ALARM1_SECS;
-       ds1307->msg[1].flags = I2C_M_RD;
-       ds1307->msg[1].len = 9;
-
-       ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
-                       ds1307->msg, 2);
-       if (ret != 2) {
+       ret = i2c_smbus_read_i2c_block_data(client,
+                       DS1339_REG_ALARM1_SECS, 9, buf);
+       if (ret != 9) {
                dev_err(dev, "%s error %d\n", "alarm write", ret);
                return -EIO;
        }
@@ -392,7 +373,6 @@ static int ds1307_set_alarm(struct device *dev, struct rtc_wkalrm *t)
                        ds1307->regs[6], control, status);
 
        /* set ALARM1, using 24 hour and day-of-month modes */
-       *buf++ = DS1339_REG_ALARM1_SECS;        /* first register addr */
        buf[0] = bin2bcd(t->time.tm_sec);
        buf[1] = bin2bcd(t->time.tm_min);
        buf[2] = bin2bcd(t->time.tm_hour);
@@ -411,14 +391,11 @@ static int ds1307_set_alarm(struct device *dev, struct rtc_wkalrm *t)
        }
        buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
 
-       ds1307->msg[1].flags = 0;
-       ds1307->msg[1].len = 10;
-
-       ret = i2c_transfer(to_i2c_adapter(client->dev.parent),
-                       &ds1307->msg[1], 1);
-       if (ret != 1) {
+       ret = i2c_smbus_write_i2c_block_data(client,
+                       DS1339_REG_ALARM1_SECS, 9, buf);
+       if (ret < 0) {
                dev_err(dev, "can't set alarm time\n");
-               return -EIO;
+               return ret;
        }
 
        return 0;
@@ -490,7 +467,6 @@ ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
 {
        struct i2c_client       *client;
        struct ds1307           *ds1307;
-       struct i2c_msg          msg[2];
        int                     result;
 
        client = kobj_to_i2c_client(kobj);
@@ -503,24 +479,10 @@ ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
        if (unlikely(!count))
                return count;
 
-       msg[0].addr = client->addr;
-       msg[0].flags = 0;
-       msg[0].len = 1;
-       msg[0].buf = buf;
-
-       buf[0] = 8 + off;
-
-       msg[1].addr = client->addr;
-       msg[1].flags = I2C_M_RD;
-       msg[1].len = count;
-       msg[1].buf = buf;
-
-       result = i2c_transfer(to_i2c_adapter(client->dev.parent), msg, 2);
-       if (result != 2) {
+       result = i2c_smbus_read_i2c_block_data(client, 8 + off, count, buf);
+       if (result < 0)
                dev_err(&client->dev, "%s error %d\n", "nvram read", result);
-               return -EIO;
-       }
-       return count;
+       return result;
 }
 
 static ssize_t
@@ -528,8 +490,7 @@ ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
                char *buf, loff_t off, size_t count)
 {
        struct i2c_client       *client;
-       u8                      buffer[NVRAM_SIZE + 1];
-       int                     ret;
+       int                     result;
 
        client = kobj_to_i2c_client(kobj);
 
@@ -540,11 +501,12 @@ ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
        if (unlikely(!count))
                return count;
 
-       buffer[0] = 8 + off;
-       memcpy(buffer + 1, buf, count);
-
-       ret = i2c_master_send(client, buffer, count + 1);
-       return (ret < 0) ? ret : (ret - 1);
+       result = i2c_smbus_write_i2c_block_data(client, 8 + off, count, buf);
+       if (result < 0) {
+               dev_err(&client->dev, "%s error %d\n", "nvram write", result);
+               return result;
+       }
+       return count;
 }
 
 static struct bin_attribute nvram = {
@@ -571,9 +533,11 @@ static int __devinit ds1307_probe(struct i2c_client *client,
        const struct chip_desc  *chip = &chips[id->driver_data];
        struct i2c_adapter      *adapter = to_i2c_adapter(client->dev.parent);
        int                     want_irq = false;
+       unsigned char           *buf;
 
        if (!i2c_check_functionality(adapter,
-                       I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+                       I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
+                       I2C_FUNC_SMBUS_I2C_BLOCK))
                return -EIO;
 
        if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL)))
@@ -581,18 +545,8 @@ static int __devinit ds1307_probe(struct i2c_client *client,
 
        ds1307->client = client;
        i2c_set_clientdata(client, ds1307);
-
-       ds1307->msg[0].addr = client->addr;
-       ds1307->msg[0].flags = 0;
-       ds1307->msg[0].len = 1;
-       ds1307->msg[0].buf = &ds1307->reg_addr;
-
-       ds1307->msg[1].addr = client->addr;
-       ds1307->msg[1].flags = I2C_M_RD;
-       ds1307->msg[1].len = sizeof(ds1307->regs);
-       ds1307->msg[1].buf = ds1307->regs;
-
        ds1307->type = id->driver_data;
+       buf = ds1307->regs;
 
        switch (ds1307->type) {
        case ds_1337:
@@ -602,21 +556,15 @@ static int __devinit ds1307_probe(struct i2c_client *client,
                        INIT_WORK(&ds1307->work, ds1307_work);
                        want_irq = true;
                }
-
-               ds1307->reg_addr = DS1337_REG_CONTROL;
-               ds1307->msg[1].len = 2;
-
                /* get registers that the "rtc" read below won't read... */
-               tmp = i2c_transfer(adapter, ds1307->msg, 2);
+               tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
+                               DS1337_REG_CONTROL, 2, buf);
                if (tmp != 2) {
                        pr_debug("read error %d\n", tmp);
                        err = -EIO;
                        goto exit_free;
                }
 
-               ds1307->reg_addr = 0;
-               ds1307->msg[1].len = sizeof(ds1307->regs);
-
                /* oscillator off?  turn it on, so clock can tick. */
                if (ds1307->regs[0] & DS1337_BIT_nEOSC)
                        ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
@@ -647,9 +595,8 @@ static int __devinit ds1307_probe(struct i2c_client *client,
 
 read_rtc:
        /* read RTC registers */
-
-       tmp = i2c_transfer(adapter, ds1307->msg, 2);
-       if (tmp != 2) {
+       tmp = i2c_smbus_read_i2c_block_data(ds1307->client, 0, 8, buf);
+       if (tmp != 8) {
                pr_debug("read error %d\n", tmp);
                err = -EIO;
                goto exit_free;