Commit 2784366c authored by Vincent Donnefort's avatar Vincent Donnefort Committed by Linus Torvalds

drivers/rtc/rtc-pcf8563.c: introduce read|write_block_data

This functions allow to factorize I2C I/O operations.
Signed-off-by: default avatarVincent Donnefort <vdonnefort@gmail.com>
Cc: Simon Guinot <simon.guinot@sequanux.org>
Cc: Jason Cooper <jason@lakedaemon.net>
Cc: Andrew Lunn <andrew@lunn.ch>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent da167ad7
...@@ -69,35 +69,66 @@ struct pcf8563 { ...@@ -69,35 +69,66 @@ struct pcf8563 {
int voltage_low; /* incicates if a low_voltage was detected */ int voltage_low; /* incicates if a low_voltage was detected */
}; };
/* static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg,
* In the routines that deal directly with the pcf8563 hardware, we use unsigned char length, unsigned char *buf)
* rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
*/
static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
{ {
struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
unsigned char buf[13] = { PCF8563_REG_ST1 };
struct i2c_msg msgs[] = { struct i2c_msg msgs[] = {
{/* setup read ptr */ {/* setup read ptr */
.addr = client->addr, .addr = client->addr,
.len = 1, .len = 1,
.buf = buf .buf = &reg,
}, },
{/* read status + date */ {
.addr = client->addr, .addr = client->addr,
.flags = I2C_M_RD, .flags = I2C_M_RD,
.len = 13, .len = length,
.buf = buf .buf = buf
}, },
}; };
/* read registers */
if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
dev_err(&client->dev, "%s: read error\n", __func__); dev_err(&client->dev, "%s: read error\n", __func__);
return -EIO; return -EIO;
} }
return 0;
}
static int pcf8563_write_block_data(struct i2c_client *client,
unsigned char reg, unsigned char length,
unsigned char *buf)
{
int i, err;
for (i = 0; i < length; i++) {
unsigned char data[2] = { reg + i, buf[i] };
err = i2c_master_send(client, data, sizeof(data));
if (err != sizeof(data)) {
dev_err(&client->dev,
"%s: err=%d addr=%02x, data=%02x\n",
__func__, err, data[0], data[1]);
return -EIO;
}
}
return 0;
}
/*
* In the routines that deal directly with the pcf8563 hardware, we use
* rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
*/
static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
{
struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
unsigned char buf[9];
int err;
err = pcf8563_read_block_data(client, PCF8563_REG_ST1, 9, buf);
if (err)
return err;
if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) { if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) {
pcf8563->voltage_low = 1; pcf8563->voltage_low = 1;
dev_info(&client->dev, dev_info(&client->dev,
...@@ -144,7 +175,7 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) ...@@ -144,7 +175,7 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
{ {
struct pcf8563 *pcf8563 = i2c_get_clientdata(client); struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
int i, err; int err;
unsigned char buf[9]; unsigned char buf[9];
dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
...@@ -170,19 +201,10 @@ static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) ...@@ -170,19 +201,10 @@ static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
buf[PCF8563_REG_DW] = tm->tm_wday & 0x07; buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
/* write register's data */ err = pcf8563_write_block_data(client, PCF8563_REG_SC,
for (i = 0; i < 7; i++) { 9 - PCF8563_REG_SC, buf + PCF8563_REG_SC);
unsigned char data[2] = { PCF8563_REG_SC + i, if (err)
buf[PCF8563_REG_SC + i] }; return err;
err = i2c_master_send(client, data, sizeof(data));
if (err != sizeof(data)) {
dev_err(&client->dev,
"%s: err=%d addr=%02x, data=%02x\n",
__func__, err, data[0], data[1]);
return -EIO;
}
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment