rtc-rs5c372.c 18 KB

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