rtc-rs5c372.c 19 KB

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