]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/rtc/rtc-rs5c372.c
rtc: rtc-rs5c372: SMBus conversion/support
[linux-2.6-omap-h63xx.git] / drivers / rtc / rtc-rs5c372.c
1 /*
2  * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs
3  *
4  * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5  * Copyright (C) 2006 Tower Technologies
6  * Copyright (C) 2008 Paul Mundt
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/i2c.h>
14 #include <linux/rtc.h>
15 #include <linux/bcd.h>
16
17 #define DRV_VERSION "0.6"
18
19
20 /*
21  * Ricoh has a family of I2C based RTCs, which differ only slightly from
22  * each other.  Differences center on pinout (e.g. how many interrupts,
23  * output clock, etc) and how the control registers are used.  The '372
24  * is significant only because that's the one this driver first supported.
25  */
26 #define RS5C372_REG_SECS        0
27 #define RS5C372_REG_MINS        1
28 #define RS5C372_REG_HOURS       2
29 #define RS5C372_REG_WDAY        3
30 #define RS5C372_REG_DAY         4
31 #define RS5C372_REG_MONTH       5
32 #define RS5C372_REG_YEAR        6
33 #define RS5C372_REG_TRIM        7
34 #       define RS5C372_TRIM_XSL         0x80
35 #       define RS5C372_TRIM_MASK        0x7F
36
37 #define RS5C_REG_ALARM_A_MIN    8                       /* or ALARM_W */
38 #define RS5C_REG_ALARM_A_HOURS  9
39 #define RS5C_REG_ALARM_A_WDAY   10
40
41 #define RS5C_REG_ALARM_B_MIN    11                      /* or ALARM_D */
42 #define RS5C_REG_ALARM_B_HOURS  12
43 #define RS5C_REG_ALARM_B_WDAY   13                      /* (ALARM_B only) */
44
45 #define RS5C_REG_CTRL1          14
46 #       define RS5C_CTRL1_AALE          (1 << 7)        /* or WALE */
47 #       define RS5C_CTRL1_BALE          (1 << 6)        /* or DALE */
48 #       define RV5C387_CTRL1_24         (1 << 5)
49 #       define RS5C372A_CTRL1_SL1       (1 << 5)
50 #       define RS5C_CTRL1_CT_MASK       (7 << 0)
51 #       define RS5C_CTRL1_CT0           (0 << 0)        /* no periodic irq */
52 #       define RS5C_CTRL1_CT4           (4 << 0)        /* 1 Hz level irq */
53 #define RS5C_REG_CTRL2          15
54 #       define RS5C372_CTRL2_24         (1 << 5)
55 #       define RS5C_CTRL2_XSTP          (1 << 4)
56 #       define RS5C_CTRL2_CTFG          (1 << 2)
57 #       define RS5C_CTRL2_AAFG          (1 << 1)        /* or WAFG */
58 #       define RS5C_CTRL2_BAFG          (1 << 0)        /* or DAFG */
59
60
61 /* to read (style 1) or write registers starting at R */
62 #define RS5C_ADDR(R)            (((R) << 4) | 0)
63
64
65 enum rtc_type {
66         rtc_undef = 0,
67         rtc_rs5c372a,
68         rtc_rs5c372b,
69         rtc_rv5c386,
70         rtc_rv5c387a,
71 };
72
73 static const struct i2c_device_id rs5c372_id[] = {
74         { "rs5c372a", rtc_rs5c372a },
75         { "rs5c372b", rtc_rs5c372b },
76         { "rv5c386", rtc_rv5c386 },
77         { "rv5c387a", rtc_rv5c387a },
78         { }
79 };
80 MODULE_DEVICE_TABLE(i2c, rs5c372_id);
81
82 /* REVISIT:  this assumes that:
83  *  - we're in the 21st century, so it's safe to ignore the century
84  *    bit for rv5c38[67] (REG_MONTH bit 7);
85  *  - we should use ALARM_A not ALARM_B (may be wrong on some boards)
86  */
87 struct rs5c372 {
88         struct i2c_client       *client;
89         struct rtc_device       *rtc;
90         enum rtc_type           type;
91         unsigned                time24:1;
92         unsigned                has_irq:1;
93         unsigned                smbus:1;
94         char                    buf[17];
95         char                    *regs;
96 };
97
98 static int rs5c_get_regs(struct rs5c372 *rs5c)
99 {
100         struct i2c_client       *client = rs5c->client;
101         struct i2c_msg          msgs[] = {
102                 { client->addr, I2C_M_RD, sizeof rs5c->buf, rs5c->buf },
103         };
104
105         /* This implements the third reading method from the datasheet, using
106          * an internal address that's reset after each transaction (by STOP)
107          * to 0x0f ... so we read extra registers, and skip the first one.
108          *
109          * The first method doesn't work with the iop3xx adapter driver, on at
110          * least 80219 chips; this works around that bug.
111          *
112          * The third method on the other hand doesn't work for the SMBus-only
113          * configurations, so we use the the first method there, stripping off
114          * the extra register in the process.
115          */
116         if (rs5c->smbus) {
117                 int addr = RS5C_ADDR(RS5C372_REG_SECS);
118                 int size = sizeof(rs5c->buf) - 1;
119
120                 if (i2c_smbus_read_i2c_block_data(client, addr, size,
121                                                   rs5c->buf + 1) != size) {
122                         dev_warn(&client->dev, "can't read registers\n");
123                         return -EIO;
124                 }
125         } else {
126                 if ((i2c_transfer(client->adapter, msgs, 1)) != 1) {
127                         dev_warn(&client->dev, "can't read registers\n");
128                         return -EIO;
129                 }
130         }
131
132         dev_dbg(&client->dev,
133                 "%02x %02x %02x (%02x) %02x %02x %02x (%02x), "
134                 "%02x %02x %02x, %02x %02x %02x; %02x %02x\n",
135                 rs5c->regs[0],  rs5c->regs[1],  rs5c->regs[2],  rs5c->regs[3],
136                 rs5c->regs[4],  rs5c->regs[5],  rs5c->regs[6],  rs5c->regs[7],
137                 rs5c->regs[8],  rs5c->regs[9],  rs5c->regs[10], rs5c->regs[11],
138                 rs5c->regs[12], rs5c->regs[13], rs5c->regs[14], rs5c->regs[15]);
139
140         return 0;
141 }
142
143 static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
144 {
145         unsigned        hour;
146
147         if (rs5c->time24)
148                 return BCD2BIN(reg & 0x3f);
149
150         hour = BCD2BIN(reg & 0x1f);
151         if (hour == 12)
152                 hour = 0;
153         if (reg & 0x20)
154                 hour += 12;
155         return hour;
156 }
157
158 static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour)
159 {
160         if (rs5c->time24)
161                 return BIN2BCD(hour);
162
163         if (hour > 12)
164                 return 0x20 | BIN2BCD(hour - 12);
165         if (hour == 12)
166                 return 0x20 | BIN2BCD(12);
167         if (hour == 0)
168                 return BIN2BCD(12);
169         return BIN2BCD(hour);
170 }
171
172 static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
173 {
174         struct rs5c372  *rs5c = i2c_get_clientdata(client);
175         int             status = rs5c_get_regs(rs5c);
176
177         if (status < 0)
178                 return status;
179
180         tm->tm_sec = BCD2BIN(rs5c->regs[RS5C372_REG_SECS] & 0x7f);
181         tm->tm_min = BCD2BIN(rs5c->regs[RS5C372_REG_MINS] & 0x7f);
182         tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]);
183
184         tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07);
185         tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f);
186
187         /* tm->tm_mon is zero-based */
188         tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1;
189
190         /* year is 1900 + tm->tm_year */
191         tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100;
192
193         dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
194                 "mday=%d, mon=%d, year=%d, wday=%d\n",
195                 __func__,
196                 tm->tm_sec, tm->tm_min, tm->tm_hour,
197                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
198
199         /* rtc might need initialization */
200         return rtc_valid_tm(tm);
201 }
202
203 static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
204 {
205         struct rs5c372  *rs5c = i2c_get_clientdata(client);
206         unsigned char   buf[8];
207         int             addr;
208
209         dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
210                 "mday=%d, mon=%d, year=%d, wday=%d\n",
211                 __func__,
212                 tm->tm_sec, tm->tm_min, tm->tm_hour,
213                 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
214
215         addr   = RS5C_ADDR(RS5C372_REG_SECS);
216         buf[0] = BIN2BCD(tm->tm_sec);
217         buf[1] = BIN2BCD(tm->tm_min);
218         buf[2] = rs5c_hr2reg(rs5c, tm->tm_hour);
219         buf[3] = BIN2BCD(tm->tm_wday);
220         buf[4] = BIN2BCD(tm->tm_mday);
221         buf[5] = BIN2BCD(tm->tm_mon + 1);
222         buf[6] = BIN2BCD(tm->tm_year - 100);
223
224         if (i2c_smbus_write_i2c_block_data(client, addr, sizeof(buf), buf) < 0) {
225                 dev_err(&client->dev, "%s: write error\n", __func__);
226                 return -EIO;
227         }
228
229         return 0;
230 }
231
232 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
233 #define NEED_TRIM
234 #endif
235
236 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
237 #define NEED_TRIM
238 #endif
239
240 #ifdef  NEED_TRIM
241 static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
242 {
243         struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
244         u8 tmp = rs5c372->regs[RS5C372_REG_TRIM];
245
246         if (osc)
247                 *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768;
248
249         if (trim) {
250                 dev_dbg(&client->dev, "%s: raw trim=%x\n", __func__, tmp);
251                 tmp &= RS5C372_TRIM_MASK;
252                 if (tmp & 0x3e) {
253                         int t = tmp & 0x3f;
254
255                         if (tmp & 0x40)
256                                 t = (~t | (s8)0xc0) + 1;
257                         else
258                                 t = t - 1;
259
260                         tmp = t * 2;
261                 } else
262                         tmp = 0;
263                 *trim = tmp;
264         }
265
266         return 0;
267 }
268 #endif
269
270 static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm)
271 {
272         return rs5c372_get_datetime(to_i2c_client(dev), tm);
273 }
274
275 static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm)
276 {
277         return rs5c372_set_datetime(to_i2c_client(dev), tm);
278 }
279
280 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
281
282 static int
283 rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
284 {
285         struct i2c_client       *client = to_i2c_client(dev);
286         struct rs5c372          *rs5c = i2c_get_clientdata(client);
287         unsigned char           buf;
288         int                     status, addr;
289
290         buf = rs5c->regs[RS5C_REG_CTRL1];
291         switch (cmd) {
292         case RTC_UIE_OFF:
293         case RTC_UIE_ON:
294                 /* some 327a modes use a different IRQ pin for 1Hz irqs */
295                 if (rs5c->type == rtc_rs5c372a
296                                 && (buf & RS5C372A_CTRL1_SL1))
297                         return -ENOIOCTLCMD;
298         case RTC_AIE_OFF:
299         case RTC_AIE_ON:
300                 /* these irq management calls only make sense for chips
301                  * which are wired up to an IRQ.
302                  */
303                 if (!rs5c->has_irq)
304                         return -ENOIOCTLCMD;
305                 break;
306         default:
307                 return -ENOIOCTLCMD;
308         }
309
310         status = rs5c_get_regs(rs5c);
311         if (status < 0)
312                 return status;
313
314         addr = RS5C_ADDR(RS5C_REG_CTRL1);
315         switch (cmd) {
316         case RTC_AIE_OFF:       /* alarm off */
317                 buf &= ~RS5C_CTRL1_AALE;
318                 break;
319         case RTC_AIE_ON:        /* alarm on */
320                 buf |= RS5C_CTRL1_AALE;
321                 break;
322         case RTC_UIE_OFF:       /* update off */
323                 buf &= ~RS5C_CTRL1_CT_MASK;
324                 break;
325         case RTC_UIE_ON:        /* update on */
326                 buf &= ~RS5C_CTRL1_CT_MASK;
327                 buf |= RS5C_CTRL1_CT4;
328                 break;
329         }
330
331         if (i2c_smbus_write_byte_data(client, addr, buf) < 0) {
332                 printk(KERN_WARNING "%s: can't update alarm\n",
333                         rs5c->rtc->name);
334                 status = -EIO;
335         } else
336                 rs5c->regs[RS5C_REG_CTRL1] = buf;
337
338         return status;
339 }
340
341 #else
342 #define rs5c_rtc_ioctl  NULL
343 #endif
344
345
346 /* NOTE:  Since RTC_WKALM_{RD,SET} were originally defined for EFI,
347  * which only exposes a polled programming interface; and since
348  * these calls map directly to those EFI requests; we don't demand
349  * we have an IRQ for this chip when we go through this API.
350  *
351  * The older x86_pc derived RTC_ALM_{READ,SET} calls require irqs
352  * though, managed through RTC_AIE_{ON,OFF} requests.
353  */
354
355 static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
356 {
357         struct i2c_client       *client = to_i2c_client(dev);
358         struct rs5c372          *rs5c = i2c_get_clientdata(client);
359         int                     status;
360
361         status = rs5c_get_regs(rs5c);
362         if (status < 0)
363                 return status;
364
365         /* report alarm time */
366         t->time.tm_sec = 0;
367         t->time.tm_min = BCD2BIN(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f);
368         t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]);
369         t->time.tm_mday = -1;
370         t->time.tm_mon = -1;
371         t->time.tm_year = -1;
372         t->time.tm_wday = -1;
373         t->time.tm_yday = -1;
374         t->time.tm_isdst = -1;
375
376         /* ... and status */
377         t->enabled = !!(rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE);
378         t->pending = !!(rs5c->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_AAFG);
379
380         return 0;
381 }
382
383 static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
384 {
385         struct i2c_client       *client = to_i2c_client(dev);
386         struct rs5c372          *rs5c = i2c_get_clientdata(client);
387         int                     status, addr, i;
388         unsigned char           buf[3];
389
390         /* only handle up to 24 hours in the future, like RTC_ALM_SET */
391         if (t->time.tm_mday != -1
392                         || t->time.tm_mon != -1
393                         || t->time.tm_year != -1)
394                 return -EINVAL;
395
396         /* REVISIT: round up tm_sec */
397
398         /* if needed, disable irq (clears pending status) */
399         status = rs5c_get_regs(rs5c);
400         if (status < 0)
401                 return status;
402         if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) {
403                 addr = RS5C_ADDR(RS5C_REG_CTRL1);
404                 buf[0] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE;
405                 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0) {
406                         pr_debug("%s: can't disable alarm\n", rs5c->rtc->name);
407                         return -EIO;
408                 }
409                 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
410         }
411
412         /* set alarm */
413         buf[0] = BIN2BCD(t->time.tm_min);
414         buf[1] = rs5c_hr2reg(rs5c, t->time.tm_hour);
415         buf[2] = 0x7f;  /* any/all days */
416
417         for (i = 0; i < sizeof(buf); i++) {
418                 addr = RS5C_ADDR(RS5C_REG_ALARM_A_MIN + i);
419                 if (i2c_smbus_write_byte_data(client, addr, buf[i]) < 0) {
420                         pr_debug("%s: can't set alarm time\n", rs5c->rtc->name);
421                         return -EIO;
422                 }
423         }
424
425         /* ... and maybe enable its irq */
426         if (t->enabled) {
427                 addr = RS5C_ADDR(RS5C_REG_CTRL1);
428                 buf[0] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE;
429                 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0)
430                         printk(KERN_WARNING "%s: can't enable alarm\n",
431                                 rs5c->rtc->name);
432                 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
433         }
434
435         return 0;
436 }
437
438 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
439
440 static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
441 {
442         int err, osc, trim;
443
444         err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
445         if (err == 0) {
446                 seq_printf(seq, "crystal\t\t: %d.%03d KHz\n",
447                                 osc / 1000, osc % 1000);
448                 seq_printf(seq, "trim\t\t: %d\n", trim);
449         }
450
451         return 0;
452 }
453
454 #else
455 #define rs5c372_rtc_proc        NULL
456 #endif
457
458 static const struct rtc_class_ops rs5c372_rtc_ops = {
459         .proc           = rs5c372_rtc_proc,
460         .ioctl          = rs5c_rtc_ioctl,
461         .read_time      = rs5c372_rtc_read_time,
462         .set_time       = rs5c372_rtc_set_time,
463         .read_alarm     = rs5c_read_alarm,
464         .set_alarm      = rs5c_set_alarm,
465 };
466
467 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
468
469 static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
470                                 struct device_attribute *attr, char *buf)
471 {
472         int err, trim;
473
474         err = rs5c372_get_trim(to_i2c_client(dev), NULL, &trim);
475         if (err)
476                 return err;
477
478         return sprintf(buf, "%d\n", trim);
479 }
480 static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
481
482 static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
483                                 struct device_attribute *attr, char *buf)
484 {
485         int err, osc;
486
487         err = rs5c372_get_trim(to_i2c_client(dev), &osc, NULL);
488         if (err)
489                 return err;
490
491         return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000);
492 }
493 static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
494
495 static int rs5c_sysfs_register(struct device *dev)
496 {
497         int err;
498
499         err = device_create_file(dev, &dev_attr_trim);
500         if (err)
501                 return err;
502         err = device_create_file(dev, &dev_attr_osc);
503         if (err)
504                 device_remove_file(dev, &dev_attr_trim);
505
506         return err;
507 }
508
509 static void rs5c_sysfs_unregister(struct device *dev)
510 {
511         device_remove_file(dev, &dev_attr_trim);
512         device_remove_file(dev, &dev_attr_osc);
513 }
514
515 #else
516 static int rs5c_sysfs_register(struct device *dev)
517 {
518         return 0;
519 }
520
521 static void rs5c_sysfs_unregister(struct device *dev)
522 {
523         /* nothing */
524 }
525 #endif  /* SYSFS */
526
527 static struct i2c_driver rs5c372_driver;
528
529 static int rs5c_oscillator_setup(struct rs5c372 *rs5c372)
530 {
531         unsigned char buf[2];
532         int addr, i, ret = 0;
533
534         if (!(rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP))
535                 return ret;
536         rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP;
537
538         addr   = RS5C_ADDR(RS5C_REG_CTRL1);
539         buf[0] = rs5c372->regs[RS5C_REG_CTRL1];
540         buf[1] = rs5c372->regs[RS5C_REG_CTRL2];
541
542         /* use 24hr mode */
543         switch (rs5c372->type) {
544         case rtc_rs5c372a:
545         case rtc_rs5c372b:
546                 buf[1] |= RS5C372_CTRL2_24;
547                 rs5c372->time24 = 1;
548                 break;
549         case rtc_rv5c386:
550         case rtc_rv5c387a:
551                 buf[0] |= RV5C387_CTRL1_24;
552                 rs5c372->time24 = 1;
553                 break;
554         default:
555                 /* impossible */
556                 break;
557         }
558
559         for (i = 0; i < sizeof(buf); i++) {
560                 addr = RS5C_ADDR(RS5C_REG_CTRL1 + i);
561                 ret = i2c_smbus_write_byte_data(rs5c372->client, addr, buf[i]);
562                 if (unlikely(ret < 0))
563                         return ret;
564         }
565
566         rs5c372->regs[RS5C_REG_CTRL1] = buf[0];
567         rs5c372->regs[RS5C_REG_CTRL2] = buf[1];
568
569         return 0;
570 }
571
572 static int rs5c372_probe(struct i2c_client *client,
573                          const struct i2c_device_id *id)
574 {
575         int err = 0;
576         int smbus_mode = 0;
577         struct rs5c372 *rs5c372;
578         struct rtc_time tm;
579
580         dev_dbg(&client->dev, "%s\n", __func__);
581
582         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
583                         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK)) {
584                 /*
585                  * If we don't have any master mode adapter, try breaking
586                  * it down in to the barest of capabilities.
587                  */
588                 if (i2c_check_functionality(client->adapter,
589                                 I2C_FUNC_SMBUS_BYTE_DATA |
590                                 I2C_FUNC_SMBUS_I2C_BLOCK))
591                         smbus_mode = 1;
592                 else {
593                         /* Still no good, give up */
594                         err = -ENODEV;
595                         goto exit;
596                 }
597         }
598
599         if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
600                 err = -ENOMEM;
601                 goto exit;
602         }
603
604         rs5c372->client = client;
605         i2c_set_clientdata(client, rs5c372);
606         rs5c372->type = id->driver_data;
607
608         /* we read registers 0x0f then 0x00-0x0f; skip the first one */
609         rs5c372->regs = &rs5c372->buf[1];
610         rs5c372->smbus = smbus_mode;
611
612         err = rs5c_get_regs(rs5c372);
613         if (err < 0)
614                 goto exit_kfree;
615
616         /* clock may be set for am/pm or 24 hr time */
617         switch (rs5c372->type) {
618         case rtc_rs5c372a:
619         case rtc_rs5c372b:
620                 /* alarm uses ALARM_A; and nINTRA on 372a, nINTR on 372b.
621                  * so does periodic irq, except some 327a modes.
622                  */
623                 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24)
624                         rs5c372->time24 = 1;
625                 break;
626         case rtc_rv5c386:
627         case rtc_rv5c387a:
628                 if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24)
629                         rs5c372->time24 = 1;
630                 /* alarm uses ALARM_W; and nINTRB for alarm and periodic
631                  * irq, on both 386 and 387
632                  */
633                 break;
634         default:
635                 dev_err(&client->dev, "unknown RTC type\n");
636                 goto exit_kfree;
637         }
638
639         /* if the oscillator lost power and no other software (like
640          * the bootloader) set it up, do it here.
641          */
642         err = rs5c_oscillator_setup(rs5c372);
643         if (unlikely(err < 0)) {
644                 dev_err(&client->dev, "setup error\n");
645                 goto exit_kfree;
646         }
647
648         if (rs5c372_get_datetime(client, &tm) < 0)
649                 dev_warn(&client->dev, "clock needs to be set\n");
650
651         dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n",
652                         ({ char *s; switch (rs5c372->type) {
653                         case rtc_rs5c372a:      s = "rs5c372a"; break;
654                         case rtc_rs5c372b:      s = "rs5c372b"; break;
655                         case rtc_rv5c386:       s = "rv5c386"; break;
656                         case rtc_rv5c387a:      s = "rv5c387a"; break;
657                         default:                s = "chip"; break;
658                         }; s;}),
659                         rs5c372->time24 ? "24hr" : "am/pm"
660                         );
661
662         /* REVISIT use client->irq to register alarm irq ... */
663
664         rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name,
665                                 &client->dev, &rs5c372_rtc_ops, THIS_MODULE);
666
667         if (IS_ERR(rs5c372->rtc)) {
668                 err = PTR_ERR(rs5c372->rtc);
669                 goto exit_kfree;
670         }
671
672         err = rs5c_sysfs_register(&client->dev);
673         if (err)
674                 goto exit_devreg;
675
676         return 0;
677
678 exit_devreg:
679         rtc_device_unregister(rs5c372->rtc);
680
681 exit_kfree:
682         kfree(rs5c372);
683
684 exit:
685         return err;
686 }
687
688 static int rs5c372_remove(struct i2c_client *client)
689 {
690         struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
691
692         rtc_device_unregister(rs5c372->rtc);
693         rs5c_sysfs_unregister(&client->dev);
694         kfree(rs5c372);
695         return 0;
696 }
697
698 static struct i2c_driver rs5c372_driver = {
699         .driver         = {
700                 .name   = "rtc-rs5c372",
701         },
702         .probe          = rs5c372_probe,
703         .remove         = rs5c372_remove,
704         .id_table       = rs5c372_id,
705 };
706
707 static __init int rs5c372_init(void)
708 {
709         return i2c_add_driver(&rs5c372_driver);
710 }
711
712 static __exit void rs5c372_exit(void)
713 {
714         i2c_del_driver(&rs5c372_driver);
715 }
716
717 module_init(rs5c372_init);
718 module_exit(rs5c372_exit);
719
720 MODULE_AUTHOR(
721                 "Pavel Mironchik <pmironchik@optifacio.net>, "
722                 "Alessandro Zummo <a.zummo@towertech.it>, "
723                 "Paul Mundt <lethal@linux-sh.org>");
724 MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
725 MODULE_LICENSE("GPL");
726 MODULE_VERSION(DRV_VERSION);