1 --- a/drivers/rtc/rtc-m41t80.c
2 +++ b/drivers/rtc/rtc-m41t80.c
4 * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
6 * 2006 (c) mycable GmbH
7 + * Copyright (c) 2008 Maciej W. Rozycki
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
12 #define M41T80_REG_DAY 5
13 #define M41T80_REG_MON 6
14 #define M41T80_REG_YEAR 7
15 +#define M41T80_REG_CONTROL 8
16 +#define M41T80_REG_WATCHDOG 9
17 #define M41T80_REG_ALARM_MON 0xa
18 #define M41T80_REG_ALARM_DAY 0xb
19 #define M41T80_REG_ALARM_HOUR 0xc
21 #define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */
22 #define M41T80_FEATURE_SQ_ALT (1 << 4) /* RSx bits are in reg 4 */
24 -#define DRV_VERSION "0.05"
25 +#define DRV_VERSION "0.06"
27 static DEFINE_MUTEX(m41t80_rtc_mutex);
28 static const struct i2c_device_id m41t80_id[] = {
29 @@ -89,31 +92,88 @@ struct m41t80_data {
30 struct rtc_device *rtc;
33 -static int m41t80_get_datetime(struct i2c_client *client,
34 - struct rtc_time *tm)
36 +static int m41t80_write_block_data(struct i2c_client *client,
37 + u8 reg, u8 num, u8 *buf)
39 - u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
40 - struct i2c_msg msgs[] = {
42 - .addr = client->addr,
48 - .addr = client->addr,
50 - .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
51 - .buf = buf + M41T80_REG_SEC,
56 - if (i2c_transfer(client->adapter, msgs, 2) < 0) {
57 - dev_err(&client->dev, "read error\n");
59 + if (i2c_check_functionality(client->adapter,
60 + I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
61 + i = i2c_smbus_write_i2c_block_data(client, reg, num, buf);
63 + for (i = 0; i < num; i++) {
64 + rc = i2c_smbus_write_byte_data(client, reg + i,
76 +static int m41t80_read_block_data(struct i2c_client *client,
77 + u8 reg, u8 num, u8 *buf)
81 + if (i2c_check_functionality(client->adapter,
82 + I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
83 + i = i2c_smbus_read_i2c_block_data(client, reg, num, buf);
85 + for (i = 0; i < num; i++) {
86 + rc = i2c_smbus_read_byte_data(client, reg + i);
98 +static int m41t80_get_datetime(struct i2c_client *client, struct rtc_time *tm)
100 + u8 buf[M41T80_DATETIME_REG_SIZE];
105 + * Time registers are latched by this chip if an I2C block
106 + * transfer is used, but with SMBus-style byte accesses
107 + * this is not the case, so check seconds for a wraparound.
110 + if (m41t80_read_block_data(client, M41T80_REG_SEC,
111 + M41T80_DATETIME_REG_SIZE -
113 + buf + M41T80_REG_SEC) < 0) {
114 + dev_err(&client->dev, "read error\n");
117 + if (i2c_check_functionality(client->adapter,
118 + I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
119 + sec1 = buf[M41T80_REG_SEC];
123 + sec0 = buf[M41T80_REG_SEC];
124 + sec1 = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
126 + dev_err(&client->dev, "read error\n");
130 + sec0 = bcd2bin(sec0 & 0x7f);
131 + sec1 = bcd2bin(sec1 & 0x7f);
132 + } while (sec1 < sec0 && --loops);
134 - tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
136 tm->tm_min = bcd2bin(buf[M41T80_REG_MIN] & 0x7f);
137 tm->tm_hour = bcd2bin(buf[M41T80_REG_HOUR] & 0x3f);
138 tm->tm_mday = bcd2bin(buf[M41T80_REG_DAY] & 0x3f);
139 @@ -128,39 +188,16 @@ static int m41t80_get_datetime(struct i2
140 /* Sets the given date and time to the real time clock. */
141 static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
143 - u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
144 - u8 *buf = &wbuf[1];
145 - u8 dt_addr[1] = { M41T80_REG_SEC };
146 - struct i2c_msg msgs_in[] = {
148 - .addr = client->addr,
154 - .addr = client->addr,
156 - .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
157 - .buf = buf + M41T80_REG_SEC,
160 - struct i2c_msg msgs[] = {
162 - .addr = client->addr,
164 - .len = 1 + M41T80_DATETIME_REG_SIZE,
168 + u8 buf[M41T80_DATETIME_REG_SIZE];
170 /* Read current reg values into buf[1..7] */
171 - if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
172 + if (m41t80_read_block_data(client, M41T80_REG_SEC,
173 + M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
174 + buf + M41T80_REG_SEC) < 0) {
175 dev_err(&client->dev, "read error\n");
179 - wbuf[0] = 0; /* offset into rtc's regs */
180 /* Merge time-data and register flags into buf[0..7] */
181 buf[M41T80_REG_SSEC] = 0;
182 buf[M41T80_REG_SEC] =
183 @@ -178,7 +215,8 @@ static int m41t80_set_datetime(struct i2
184 /* assume 20YY not 19YY */
185 buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year % 100);
187 - if (i2c_transfer(client->adapter, msgs, 1) != 1) {
188 + if (m41t80_write_block_data(client, M41T80_REG_SSEC,
189 + M41T80_DATETIME_REG_SIZE, buf) < 0) {
190 dev_err(&client->dev, "write error\n");
193 @@ -252,34 +290,11 @@ err:
194 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
196 struct i2c_client *client = to_i2c_client(dev);
197 - u8 wbuf[1 + M41T80_ALARM_REG_SIZE];
198 - u8 *buf = &wbuf[1];
199 + u8 buf[M41T80_ALARM_REG_SIZE];
200 u8 *reg = buf - M41T80_REG_ALARM_MON;
201 - u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
202 - struct i2c_msg msgs_in[] = {
204 - .addr = client->addr,
210 - .addr = client->addr,
212 - .len = M41T80_ALARM_REG_SIZE,
216 - struct i2c_msg msgs[] = {
218 - .addr = client->addr,
220 - .len = 1 + M41T80_ALARM_REG_SIZE,
225 - if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
226 + if (m41t80_read_block_data(client, M41T80_REG_ALARM_MON,
227 + M41T80_ALARM_REG_SIZE, buf) < 0) {
228 dev_err(&client->dev, "read error\n");
231 @@ -289,7 +304,6 @@ static int m41t80_rtc_set_alarm(struct d
232 reg[M41T80_REG_ALARM_MIN] = 0;
233 reg[M41T80_REG_ALARM_SEC] = 0;
235 - wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
236 reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
237 bin2bcd(t->time.tm_sec) : 0x80;
238 reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
239 @@ -303,7 +317,8 @@ static int m41t80_rtc_set_alarm(struct d
241 reg[M41T80_REG_ALARM_DAY] |= 0x40;
243 - if (i2c_transfer(client->adapter, msgs, 1) != 1) {
244 + if (m41t80_write_block_data(client, M41T80_REG_ALARM_MON,
245 + M41T80_ALARM_REG_SIZE, buf) < 0) {
246 dev_err(&client->dev, "write error\n");
249 @@ -323,24 +338,10 @@ static int m41t80_rtc_read_alarm(struct
251 struct i2c_client *client = to_i2c_client(dev);
252 u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */
253 - u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
254 u8 *reg = buf - M41T80_REG_ALARM_MON;
255 - struct i2c_msg msgs[] = {
257 - .addr = client->addr,
263 - .addr = client->addr,
265 - .len = M41T80_ALARM_REG_SIZE + 1,
270 - if (i2c_transfer(client->adapter, msgs, 2) < 0) {
271 + if (m41t80_read_block_data(client, M41T80_REG_ALARM_MON,
272 + M41T80_ALARM_REG_SIZE + 1, buf) < 0) {
273 dev_err(&client->dev, "read error\n");
276 @@ -513,26 +514,16 @@ static int boot_flag;
278 static void wdt_ping(void)
280 - unsigned char i2c_data[2];
281 - struct i2c_msg msgs1[1] = {
283 - .addr = save_client->addr,
289 - struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
290 + u8 wdt = 0x80; /* WDS = 1 (0x80) */
292 - i2c_data[0] = 0x09; /* watchdog register */
293 + struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
296 - i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
297 + /* mulitplier = WD_TIMO / 4, resolution = 4s (0x3) */
298 + wdt |= (wdt_margin & 0xfc) | 0x3;
301 - * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
303 - i2c_data[1] = wdt_margin<<2 | 0x82;
304 + /* mulitplier = WD_TIMO, resolution = 1s (0x2) */
305 + wdt |= wdt_margin << 2 | 0x2;
308 * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
309 @@ -541,7 +532,7 @@ static void wdt_ping(void)
310 if (clientdata->features & M41T80_FEATURE_WD)
311 i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
313 - i2c_transfer(save_client->adapter, msgs1, 1);
314 + i2c_smbus_write_byte_data(save_client, M41T80_REG_WATCHDOG, wdt);
318 @@ -551,36 +542,8 @@ static void wdt_ping(void)
320 static void wdt_disable(void)
322 - unsigned char i2c_data[2], i2c_buf[0x10];
323 - struct i2c_msg msgs0[2] = {
325 - .addr = save_client->addr,
331 - .addr = save_client->addr,
337 - struct i2c_msg msgs1[1] = {
339 - .addr = save_client->addr,
346 - i2c_data[0] = 0x09;
347 - i2c_transfer(save_client->adapter, msgs0, 2);
349 - i2c_data[0] = 0x09;
350 - i2c_data[1] = 0x00;
351 - i2c_transfer(save_client->adapter, msgs1, 1);
352 + i2c_smbus_read_byte_data(save_client, M41T80_REG_WATCHDOG);
353 + i2c_smbus_write_byte_data(save_client, M41T80_REG_WATCHDOG, 0);
357 @@ -782,8 +745,8 @@ static int m41t80_probe(struct i2c_clien
359 struct m41t80_data *clientdata = NULL;
361 - if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
362 - | I2C_FUNC_SMBUS_BYTE_DATA)) {
363 + if (!i2c_check_functionality(client->adapter,
364 + I2C_FUNC_SMBUS_BYTE_DATA)) {