sibyte: add 3.3 support
[openwrt/svn-archive/archive.git] / target / linux / sibyte / patches-3.3 / 103-m41t80_smbus.patch
1 --- a/drivers/rtc/rtc-m41t80.c
2 +++ b/drivers/rtc/rtc-m41t80.c
3 @@ -6,6 +6,7 @@
4 * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
5 *
6 * 2006 (c) mycable GmbH
7 + * Copyright (c) 2008 Maciej W. Rozycki
8 *
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
11 @@ -38,6 +39,8 @@
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
20 @@ -66,7 +69,7 @@
21 #define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */
22 #define M41T80_FEATURE_SQ_ALT (1 << 4) /* RSx bits are in reg 4 */
23
24 -#define DRV_VERSION "0.05"
25 +#define DRV_VERSION "0.06"
26
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;
31 };
32
33 -static int m41t80_get_datetime(struct i2c_client *client,
34 - struct rtc_time *tm)
35 +
36 +static int m41t80_write_block_data(struct i2c_client *client,
37 + u8 reg, u8 num, u8 *buf)
38 {
39 - u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
40 - struct i2c_msg msgs[] = {
41 - {
42 - .addr = client->addr,
43 - .flags = 0,
44 - .len = 1,
45 - .buf = dt_addr,
46 - },
47 - {
48 - .addr = client->addr,
49 - .flags = I2C_M_RD,
50 - .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
51 - .buf = buf + M41T80_REG_SEC,
52 - },
53 - };
54 + int i, rc;
55
56 - if (i2c_transfer(client->adapter, msgs, 2) < 0) {
57 - dev_err(&client->dev, "read error\n");
58 - return -EIO;
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);
62 + } else {
63 + for (i = 0; i < num; i++) {
64 + rc = i2c_smbus_write_byte_data(client, reg + i,
65 + buf[i]);
66 + if (rc < 0) {
67 + i = rc;
68 + goto out;
69 + }
70 + }
71 }
72 +out:
73 + return i;
74 +}
75 +
76 +static int m41t80_read_block_data(struct i2c_client *client,
77 + u8 reg, u8 num, u8 *buf)
78 +{
79 + int i, rc;
80 +
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);
84 + } else {
85 + for (i = 0; i < num; i++) {
86 + rc = i2c_smbus_read_byte_data(client, reg + i);
87 + if (rc < 0) {
88 + i = rc;
89 + goto out;
90 + }
91 + buf[i] = rc;
92 + }
93 + }
94 +out:
95 + return i;
96 +}
97 +
98 +static int m41t80_get_datetime(struct i2c_client *client, struct rtc_time *tm)
99 +{
100 + u8 buf[M41T80_DATETIME_REG_SIZE];
101 + int loops = 2;
102 + int sec0, sec1;
103 +
104 + /*
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.
108 + */
109 + do {
110 + if (m41t80_read_block_data(client, M41T80_REG_SEC,
111 + M41T80_DATETIME_REG_SIZE -
112 + M41T80_REG_SEC,
113 + buf + M41T80_REG_SEC) < 0) {
114 + dev_err(&client->dev, "read error\n");
115 + return -EIO;
116 + }
117 + if (i2c_check_functionality(client->adapter,
118 + I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
119 + sec1 = buf[M41T80_REG_SEC];
120 + break;
121 + }
122 +
123 + sec0 = buf[M41T80_REG_SEC];
124 + sec1 = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
125 + if (sec1 < 0) {
126 + dev_err(&client->dev, "read error\n");
127 + return -EIO;
128 + }
129 +
130 + sec0 = bcd2bin(sec0 & 0x7f);
131 + sec1 = bcd2bin(sec1 & 0x7f);
132 + } while (sec1 < sec0 && --loops);
133
134 - tm->tm_sec = bcd2bin(buf[M41T80_REG_SEC] & 0x7f);
135 + tm->tm_sec = sec1;
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)
142 {
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[] = {
147 - {
148 - .addr = client->addr,
149 - .flags = 0,
150 - .len = 1,
151 - .buf = dt_addr,
152 - },
153 - {
154 - .addr = client->addr,
155 - .flags = I2C_M_RD,
156 - .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
157 - .buf = buf + M41T80_REG_SEC,
158 - },
159 - };
160 - struct i2c_msg msgs[] = {
161 - {
162 - .addr = client->addr,
163 - .flags = 0,
164 - .len = 1 + M41T80_DATETIME_REG_SIZE,
165 - .buf = wbuf,
166 - },
167 - };
168 + u8 buf[M41T80_DATETIME_REG_SIZE];
169
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");
176 return -EIO;
177 }
178
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);
186
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");
191 return -EIO;
192 }
193 @@ -238,34 +276,11 @@ err:
194 static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
195 {
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[] = {
203 - {
204 - .addr = client->addr,
205 - .flags = 0,
206 - .len = 1,
207 - .buf = dt_addr,
208 - },
209 - {
210 - .addr = client->addr,
211 - .flags = I2C_M_RD,
212 - .len = M41T80_ALARM_REG_SIZE,
213 - .buf = buf,
214 - },
215 - };
216 - struct i2c_msg msgs[] = {
217 - {
218 - .addr = client->addr,
219 - .flags = 0,
220 - .len = 1 + M41T80_ALARM_REG_SIZE,
221 - .buf = wbuf,
222 - },
223 - };
224
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");
229 return -EIO;
230 }
231 @@ -275,7 +290,6 @@ static int m41t80_rtc_set_alarm(struct d
232 reg[M41T80_REG_ALARM_MIN] = 0;
233 reg[M41T80_REG_ALARM_SEC] = 0;
234
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 @@ -289,7 +303,8 @@ static int m41t80_rtc_set_alarm(struct d
240 else
241 reg[M41T80_REG_ALARM_DAY] |= 0x40;
242
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");
247 return -EIO;
248 }
249 @@ -309,24 +324,10 @@ static int m41t80_rtc_read_alarm(struct
250 {
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[] = {
256 - {
257 - .addr = client->addr,
258 - .flags = 0,
259 - .len = 1,
260 - .buf = dt_addr,
261 - },
262 - {
263 - .addr = client->addr,
264 - .flags = I2C_M_RD,
265 - .len = M41T80_ALARM_REG_SIZE + 1,
266 - .buf = buf,
267 - },
268 - };
269
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");
274 return -EIO;
275 }
276 @@ -508,26 +509,16 @@ static int boot_flag;
277 */
278 static void wdt_ping(void)
279 {
280 - unsigned char i2c_data[2];
281 - struct i2c_msg msgs1[1] = {
282 - {
283 - .addr = save_client->addr,
284 - .flags = 0,
285 - .len = 2,
286 - .buf = i2c_data,
287 - },
288 - };
289 - struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
290 + u8 wdt = 0x80; /* WDS = 1 (0x80) */
291
292 - i2c_data[0] = 0x09; /* watchdog register */
293 + struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
294
295 if (wdt_margin > 31)
296 - i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
297 + /* mulitplier = WD_TIMO / 4, resolution = 4s (0x3) */
298 + wdt |= (wdt_margin & 0xfc) | 0x3;
299 else
300 - /*
301 - * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
302 - */
303 - i2c_data[1] = wdt_margin<<2 | 0x82;
304 + /* mulitplier = WD_TIMO, resolution = 1s (0x2) */
305 + wdt |= wdt_margin << 2 | 0x2;
306
307 /*
308 * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
309 @@ -536,7 +527,7 @@ static void wdt_ping(void)
310 if (clientdata->features & M41T80_FEATURE_WD)
311 i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
312
313 - i2c_transfer(save_client->adapter, msgs1, 1);
314 + i2c_smbus_write_byte_data(save_client, M41T80_REG_WATCHDOG, wdt);
315 }
316
317 /**
318 @@ -546,36 +537,8 @@ static void wdt_ping(void)
319 */
320 static void wdt_disable(void)
321 {
322 - unsigned char i2c_data[2], i2c_buf[0x10];
323 - struct i2c_msg msgs0[2] = {
324 - {
325 - .addr = save_client->addr,
326 - .flags = 0,
327 - .len = 1,
328 - .buf = i2c_data,
329 - },
330 - {
331 - .addr = save_client->addr,
332 - .flags = I2C_M_RD,
333 - .len = 1,
334 - .buf = i2c_buf,
335 - },
336 - };
337 - struct i2c_msg msgs1[1] = {
338 - {
339 - .addr = save_client->addr,
340 - .flags = 0,
341 - .len = 2,
342 - .buf = i2c_data,
343 - },
344 - };
345 -
346 - i2c_data[0] = 0x09;
347 - i2c_transfer(save_client->adapter, msgs0, 2);
348 -
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);
354 }
355
356 /**
357 @@ -777,8 +740,8 @@ static int m41t80_probe(struct i2c_clien
358 struct rtc_time tm;
359 struct m41t80_data *clientdata = NULL;
360
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)) {
365 rc = -ENODEV;
366 goto exit;
367 }