aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
authorJuergen Borleis <jbe@pengutronix.de>2016-02-09 05:57:27 -0500
committerAlexandre Belloni <alexandre.belloni@free-electrons.com>2016-03-14 12:08:17 -0400
commit31d4d33ef431364972f4a38401f660a2ec935cde (patch)
treebbfccf5b20e1ed35931e7d0aff550bb31dbf2099 /drivers/rtc
parent6cc4c8b1e3790c63b2e1b6d9b25d2bda6e406572 (diff)
rtc: pcf85063: fix time/date setting
When setting a new time/date the RTC's clock must be stopped first, in order to write the time/date registers in an atomic manner. So, this change stops the clock first and then writes the time/date registers and the clock control register (to re-enable the clock) in one turn. Signed-off-by: Juergen Borleis <jbe@pengutronix.de> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/rtc-pcf85063.c78
1 files changed, 55 insertions, 23 deletions
diff --git a/drivers/rtc/rtc-pcf85063.c b/drivers/rtc/rtc-pcf85063.c
index e0343e6aeb31..c5db231f14de 100644
--- a/drivers/rtc/rtc-pcf85063.c
+++ b/drivers/rtc/rtc-pcf85063.c
@@ -19,6 +19,7 @@
19#define DRV_VERSION "0.0.1" 19#define DRV_VERSION "0.0.1"
20 20
21#define PCF85063_REG_CTRL1 0x00 /* status */ 21#define PCF85063_REG_CTRL1 0x00 /* status */
22#define PCF85063_REG_CTRL1_STOP BIT(5)
22#define PCF85063_REG_CTRL2 0x01 23#define PCF85063_REG_CTRL2 0x01
23 24
24#define PCF85063_REG_SC 0x04 /* datetime */ 25#define PCF85063_REG_SC 0x04 /* datetime */
@@ -40,6 +41,30 @@ struct pcf85063 {
40 int voltage_low; /* indicates if a low_voltage was detected */ 41 int voltage_low; /* indicates if a low_voltage was detected */
41}; 42};
42 43
44static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
45{
46 s32 ret;
47
48 ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
49 if (ret < 0) {
50 dev_err(&client->dev, "Failing to stop the clock\n");
51 return -EIO;
52 }
53
54 /* stop the clock */
55 ret |= PCF85063_REG_CTRL1_STOP;
56
57 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
58 if (ret < 0) {
59 dev_err(&client->dev, "Failing to stop the clock\n");
60 return -EIO;
61 }
62
63 *ctrl1 = ret;
64
65 return 0;
66}
67
43/* 68/*
44 * In the routines that deal directly with the pcf85063 hardware, we use 69 * In the routines that deal directly with the pcf85063 hardware, we use
45 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 70 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -87,41 +112,48 @@ static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
87 112
88static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm) 113static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
89{ 114{
90 int i = 0, err = 0; 115 int rc;
91 unsigned char buf[11]; 116 u8 regs[8];
92 117
93 /* Control & status */ 118 /*
94 buf[PCF85063_REG_CTRL1] = 0; 119 * to accurately set the time, reset the divider chain and keep it in
95 buf[PCF85063_REG_CTRL2] = 5; 120 * reset state until all time/date registers are written
121 */
122 rc = pcf85063_stop_clock(client, &regs[7]);
123 if (rc != 0)
124 return rc;
96 125
97 /* hours, minutes and seconds */ 126 /* hours, minutes and seconds */
98 buf[PCF85063_REG_SC] = bin2bcd(tm->tm_sec) & 0x7F; 127 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
99 128
100 buf[PCF85063_REG_MN] = bin2bcd(tm->tm_min); 129 regs[1] = bin2bcd(tm->tm_min);
101 buf[PCF85063_REG_HR] = bin2bcd(tm->tm_hour); 130 regs[2] = bin2bcd(tm->tm_hour);
102 131
103 /* Day of month, 1 - 31 */ 132 /* Day of month, 1 - 31 */
104 buf[PCF85063_REG_DM] = bin2bcd(tm->tm_mday); 133 regs[3] = bin2bcd(tm->tm_mday);
105 134
106 /* Day, 0 - 6 */ 135 /* Day, 0 - 6 */
107 buf[PCF85063_REG_DW] = tm->tm_wday & 0x07; 136 regs[4] = tm->tm_wday & 0x07;
108 137
109 /* month, 1 - 12 */ 138 /* month, 1 - 12 */
110 buf[PCF85063_REG_MO] = bin2bcd(tm->tm_mon + 1); 139 regs[5] = bin2bcd(tm->tm_mon + 1);
111 140
112 /* year and century */ 141 /* year and century */
113 buf[PCF85063_REG_YR] = bin2bcd(tm->tm_year % 100); 142 regs[6] = bin2bcd(tm->tm_year % 100);
114 143
115 /* write register's data */ 144 /*
116 for (i = 0; i < sizeof(buf); i++) { 145 * after all time/date registers are written, let the 'address auto
117 unsigned char data[2] = { i, buf[i] }; 146 * increment' feature wrap around and write register CTRL1 to re-enable
118 147 * the clock divider chain again
119 err = i2c_master_send(client, data, sizeof(data)); 148 */
120 if (err != sizeof(data)) { 149 regs[7] &= ~PCF85063_REG_CTRL1_STOP;
121 dev_err(&client->dev, "%s: err=%d addr=%02x, data=%02x\n", 150
122 __func__, err, data[0], data[1]); 151 /* write all registers at once */
123 return -EIO; 152 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
124 } 153 sizeof(regs), regs);
154 if (rc < 0) {
155 dev_err(&client->dev, "date/time register write error\n");
156 return rc;
125 } 157 }
126 158
127 return 0; 159 return 0;