rtc-rs5c372.c 18 KB

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