]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/rtc/rtc-rs5c372.c
rtc: rtc-rs5c372: SMBus conversion/support
[mirror_ubuntu-zesty-kernel.git] / drivers / rtc / rtc-rs5c372.c
CommitLineData
7520b94d 1/*
cb26b572 2 * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs
7520b94d
AZ
3 *
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies
0053dc0d 6 * Copyright (C) 2008 Paul Mundt
7520b94d
AZ
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
0053dc0d 17#define DRV_VERSION "0.6"
7520b94d 18
cb26b572
DB
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 */
7520b94d
AZ
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
cb26b572
DB
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
65enum rtc_type {
66 rtc_undef = 0,
67 rtc_rs5c372a,
68 rtc_rs5c372b,
69 rtc_rv5c386,
70 rtc_rv5c387a,
71};
7520b94d 72
3760f736
JD
73static 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};
80MODULE_DEVICE_TABLE(i2c, rs5c372_id);
81
cb26b572
DB
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 */
87struct 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;
0053dc0d 93 unsigned smbus:1;
cb26b572
DB
94 char buf[17];
95 char *regs;
cb26b572 96};
7520b94d 97
cb26b572
DB
98static 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.
0053dc0d
PM
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.
cb26b572 115 */
0053dc0d
PM
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 }
cb26b572 130 }
7520b94d 131
cb26b572
DB
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]);
7520b94d 139
cb26b572
DB
140 return 0;
141}
c6f24f99 142
cb26b572
DB
143static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
144{
145 unsigned hour;
7520b94d 146
cb26b572
DB
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
158static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour)
7520b94d 159{
cb26b572
DB
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}
7520b94d 171
cb26b572
DB
172static 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);
c6f24f99 176
cb26b572
DB
177 if (status < 0)
178 return status;
7520b94d 179
cb26b572
DB
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]);
7520b94d 183
cb26b572
DB
184 tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07);
185 tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f);
7520b94d
AZ
186
187 /* tm->tm_mon is zero-based */
cb26b572 188 tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1;
7520b94d
AZ
189
190 /* year is 1900 + tm->tm_year */
cb26b572 191 tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100;
7520b94d
AZ
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",
2a4e2b87 195 __func__,
7520b94d
AZ
196 tm->tm_sec, tm->tm_min, tm->tm_hour,
197 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
198
cb26b572
DB
199 /* rtc might need initialization */
200 return rtc_valid_tm(tm);
7520b94d
AZ
201}
202
203static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
204{
cb26b572
DB
205 struct rs5c372 *rs5c = i2c_get_clientdata(client);
206 unsigned char buf[8];
0053dc0d 207 int addr;
7520b94d 208
cb26b572 209 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
7520b94d 210 "mday=%d, mon=%d, year=%d, wday=%d\n",
2a4e2b87 211 __func__,
cb26b572 212 tm->tm_sec, tm->tm_min, tm->tm_hour,
7520b94d
AZ
213 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
214
0053dc0d
PM
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);
7520b94d 223
0053dc0d 224 if (i2c_smbus_write_i2c_block_data(client, addr, sizeof(buf), buf) < 0) {
2a4e2b87 225 dev_err(&client->dev, "%s: write error\n", __func__);
7520b94d
AZ
226 return -EIO;
227 }
228
229 return 0;
230}
231
cb26b572
DB
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
7520b94d
AZ
241static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
242{
c6f24f99 243 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
cb26b572 244 u8 tmp = rs5c372->regs[RS5C372_REG_TRIM];
7520b94d 245
7520b94d 246 if (osc)
c6f24f99 247 *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768;
7520b94d 248
17ad78e5 249 if (trim) {
2a4e2b87 250 dev_dbg(&client->dev, "%s: raw trim=%x\n", __func__, tmp);
cb26b572
DB
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;
17ad78e5 264 }
7520b94d
AZ
265
266 return 0;
267}
cb26b572 268#endif
7520b94d
AZ
269
270static 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
275static 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
cb26b572
DB
280#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
281
282static int
283rs5c_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);
0053dc0d
PM
287 unsigned char buf;
288 int status, addr;
cb26b572 289
0053dc0d 290 buf = rs5c->regs[RS5C_REG_CTRL1];
cb26b572
DB
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
0053dc0d 296 && (buf & RS5C372A_CTRL1_SL1))
cb26b572
DB
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
0053dc0d 314 addr = RS5C_ADDR(RS5C_REG_CTRL1);
cb26b572
DB
315 switch (cmd) {
316 case RTC_AIE_OFF: /* alarm off */
0053dc0d 317 buf &= ~RS5C_CTRL1_AALE;
cb26b572
DB
318 break;
319 case RTC_AIE_ON: /* alarm on */
0053dc0d 320 buf |= RS5C_CTRL1_AALE;
cb26b572
DB
321 break;
322 case RTC_UIE_OFF: /* update off */
0053dc0d 323 buf &= ~RS5C_CTRL1_CT_MASK;
cb26b572
DB
324 break;
325 case RTC_UIE_ON: /* update on */
0053dc0d
PM
326 buf &= ~RS5C_CTRL1_CT_MASK;
327 buf |= RS5C_CTRL1_CT4;
cb26b572
DB
328 break;
329 }
0053dc0d
PM
330
331 if (i2c_smbus_write_byte_data(client, addr, buf) < 0) {
cb26b572
DB
332 printk(KERN_WARNING "%s: can't update alarm\n",
333 rs5c->rtc->name);
334 status = -EIO;
335 } else
0053dc0d
PM
336 rs5c->regs[RS5C_REG_CTRL1] = buf;
337
cb26b572
DB
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
355static 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
383static 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);
0053dc0d
PM
387 int status, addr, i;
388 unsigned char buf[3];
cb26b572
DB
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) {
0053dc0d
PM
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) {
cb26b572
DB
406 pr_debug("%s: can't disable alarm\n", rs5c->rtc->name);
407 return -EIO;
408 }
0053dc0d 409 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
cb26b572
DB
410 }
411
412 /* set alarm */
0053dc0d
PM
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 }
cb26b572
DB
423 }
424
425 /* ... and maybe enable its irq */
426 if (t->enabled) {
0053dc0d
PM
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)
cb26b572
DB
430 printk(KERN_WARNING "%s: can't enable alarm\n",
431 rs5c->rtc->name);
0053dc0d 432 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
cb26b572
DB
433 }
434
435 return 0;
436}
437
438#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
439
7520b94d
AZ
440static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
441{
442 int err, osc, trim;
443
adfb4341
AZ
444 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
445 if (err == 0) {
cb26b572
DB
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);
7520b94d
AZ
449 }
450
451 return 0;
452}
453
cb26b572
DB
454#else
455#define rs5c372_rtc_proc NULL
456#endif
457
ff8371ac 458static const struct rtc_class_ops rs5c372_rtc_ops = {
7520b94d 459 .proc = rs5c372_rtc_proc,
cb26b572 460 .ioctl = rs5c_rtc_ioctl,
7520b94d
AZ
461 .read_time = rs5c372_rtc_read_time,
462 .set_time = rs5c372_rtc_set_time,
cb26b572
DB
463 .read_alarm = rs5c_read_alarm,
464 .set_alarm = rs5c_set_alarm,
7520b94d
AZ
465};
466
cb26b572
DB
467#if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
468
7520b94d
AZ
469static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
82896072 472 int err, trim;
7520b94d 473
82896072
AZ
474 err = rs5c372_get_trim(to_i2c_client(dev), NULL, &trim);
475 if (err)
476 return err;
7520b94d 477
cb26b572 478 return sprintf(buf, "%d\n", trim);
7520b94d
AZ
479}
480static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
481
482static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
82896072 485 int err, osc;
7520b94d 486
82896072
AZ
487 err = rs5c372_get_trim(to_i2c_client(dev), &osc, NULL);
488 if (err)
489 return err;
7520b94d 490
82896072 491 return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000);
7520b94d
AZ
492}
493static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
494
cb26b572 495static int rs5c_sysfs_register(struct device *dev)
7520b94d 496{
cb26b572
DB
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
d815461c
DB
509static 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
cb26b572
DB
515#else
516static int rs5c_sysfs_register(struct device *dev)
517{
518 return 0;
7520b94d 519}
d815461c
DB
520
521static void rs5c_sysfs_unregister(struct device *dev)
522{
523 /* nothing */
524}
cb26b572
DB
525#endif /* SYSFS */
526
527static struct i2c_driver rs5c372_driver;
7520b94d 528
0053dc0d
PM
529static 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
d2653e92
JD
572static int rs5c372_probe(struct i2c_client *client,
573 const struct i2c_device_id *id)
7520b94d
AZ
574{
575 int err = 0;
0053dc0d 576 int smbus_mode = 0;
c6f24f99 577 struct rs5c372 *rs5c372;
cb26b572 578 struct rtc_time tm;
7520b94d 579
2a4e2b87 580 dev_dbg(&client->dev, "%s\n", __func__);
7520b94d 581
0053dc0d
PM
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 }
7520b94d
AZ
597 }
598
c6f24f99 599 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
7520b94d
AZ
600 err = -ENOMEM;
601 goto exit;
602 }
cb26b572 603
cb26b572 604 rs5c372->client = client;
c6f24f99 605 i2c_set_clientdata(client, rs5c372);
3760f736 606 rs5c372->type = id->driver_data;
c6f24f99 607
e2bfe342
PM
608 /* we read registers 0x0f then 0x00-0x0f; skip the first one */
609 rs5c372->regs = &rs5c372->buf[1];
0053dc0d 610 rs5c372->smbus = smbus_mode;
e2bfe342 611
cb26b572
DB
612 err = rs5c_get_regs(rs5c372);
613 if (err < 0)
d815461c 614 goto exit_kfree;
cb26b572 615
cb26b572
DB
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");
d815461c 636 goto exit_kfree;
cb26b572
DB
637 }
638
639 /* if the oscillator lost power and no other software (like
640 * the bootloader) set it up, do it here.
641 */
0053dc0d
PM
642 err = rs5c_oscillator_setup(rs5c372);
643 if (unlikely(err < 0)) {
644 dev_err(&client->dev, "setup error\n");
645 goto exit_kfree;
cb26b572
DB
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
d815461c 662 /* REVISIT use client->irq to register alarm irq ... */
7520b94d 663
c6f24f99
RV
664 rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name,
665 &client->dev, &rs5c372_rtc_ops, THIS_MODULE);
7520b94d 666
c6f24f99
RV
667 if (IS_ERR(rs5c372->rtc)) {
668 err = PTR_ERR(rs5c372->rtc);
d815461c 669 goto exit_kfree;
7520b94d
AZ
670 }
671
cb26b572 672 err = rs5c_sysfs_register(&client->dev);
c6f24f99
RV
673 if (err)
674 goto exit_devreg;
7520b94d
AZ
675
676 return 0;
677
91046a8a 678exit_devreg:
c6f24f99 679 rtc_device_unregister(rs5c372->rtc);
91046a8a 680
7520b94d 681exit_kfree:
c6f24f99 682 kfree(rs5c372);
7520b94d
AZ
683
684exit:
685 return err;
686}
687
d815461c 688static int rs5c372_remove(struct i2c_client *client)
cb26b572 689{
c6f24f99 690 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
7520b94d 691
d815461c
DB
692 rtc_device_unregister(rs5c372->rtc);
693 rs5c_sysfs_unregister(&client->dev);
c6f24f99 694 kfree(rs5c372);
7520b94d
AZ
695 return 0;
696}
697
cb26b572
DB
698static struct i2c_driver rs5c372_driver = {
699 .driver = {
700 .name = "rtc-rs5c372",
701 },
d815461c
DB
702 .probe = rs5c372_probe,
703 .remove = rs5c372_remove,
3760f736 704 .id_table = rs5c372_id,
cb26b572
DB
705};
706
7520b94d
AZ
707static __init int rs5c372_init(void)
708{
709 return i2c_add_driver(&rs5c372_driver);
710}
711
712static __exit void rs5c372_exit(void)
713{
714 i2c_del_driver(&rs5c372_driver);
715}
716
717module_init(rs5c372_init);
718module_exit(rs5c372_exit);
719
720MODULE_AUTHOR(
721 "Pavel Mironchik <pmironchik@optifacio.net>, "
0053dc0d
PM
722 "Alessandro Zummo <a.zummo@towertech.it>, "
723 "Paul Mundt <lethal@linux-sh.org>");
7520b94d
AZ
724MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
725MODULE_LICENSE("GPL");
726MODULE_VERSION(DRV_VERSION);