rtc-rs5c372.c 17 KB

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