rtc-ds3232.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * RTC client/driver for the Maxim/Dallas DS3232/DS3234 Real-Time Clock
  3. *
  4. * Copyright (C) 2009-2011 Freescale Semiconductor.
  5. * Author: Jack Lan <jack.lan@freescale.com>
  6. * Copyright (C) 2008 MIMOMax Wireless Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/rtc.h>
  20. #include <linux/bcd.h>
  21. #include <linux/slab.h>
  22. #include <linux/regmap.h>
  23. #define DS3232_REG_SECONDS 0x00
  24. #define DS3232_REG_MINUTES 0x01
  25. #define DS3232_REG_HOURS 0x02
  26. #define DS3232_REG_AMPM 0x02
  27. #define DS3232_REG_DAY 0x03
  28. #define DS3232_REG_DATE 0x04
  29. #define DS3232_REG_MONTH 0x05
  30. #define DS3232_REG_CENTURY 0x05
  31. #define DS3232_REG_YEAR 0x06
  32. #define DS3232_REG_ALARM1 0x07 /* Alarm 1 BASE */
  33. #define DS3232_REG_ALARM2 0x0B /* Alarm 2 BASE */
  34. #define DS3232_REG_CR 0x0E /* Control register */
  35. # define DS3232_REG_CR_nEOSC 0x80
  36. # define DS3232_REG_CR_INTCN 0x04
  37. # define DS3232_REG_CR_A2IE 0x02
  38. # define DS3232_REG_CR_A1IE 0x01
  39. #define DS3232_REG_SR 0x0F /* control/status register */
  40. # define DS3232_REG_SR_OSF 0x80
  41. # define DS3232_REG_SR_BSY 0x04
  42. # define DS3232_REG_SR_A2F 0x02
  43. # define DS3232_REG_SR_A1F 0x01
  44. struct ds3232 {
  45. struct device *dev;
  46. struct regmap *regmap;
  47. int irq;
  48. struct rtc_device *rtc;
  49. bool suspended;
  50. };
  51. static int ds3232_check_rtc_status(struct device *dev)
  52. {
  53. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  54. int ret = 0;
  55. int control, stat;
  56. ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat);
  57. if (ret)
  58. return ret;
  59. if (stat & DS3232_REG_SR_OSF)
  60. dev_warn(dev,
  61. "oscillator discontinuity flagged, "
  62. "time unreliable\n");
  63. stat &= ~(DS3232_REG_SR_OSF | DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  64. ret = regmap_write(ds3232->regmap, DS3232_REG_SR, stat);
  65. if (ret)
  66. return ret;
  67. /* If the alarm is pending, clear it before requesting
  68. * the interrupt, so an interrupt event isn't reported
  69. * before everything is initialized.
  70. */
  71. ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control);
  72. if (ret)
  73. return ret;
  74. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  75. control |= DS3232_REG_CR_INTCN;
  76. return regmap_write(ds3232->regmap, DS3232_REG_CR, control);
  77. }
  78. static int ds3232_read_time(struct device *dev, struct rtc_time *time)
  79. {
  80. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  81. int ret;
  82. u8 buf[7];
  83. unsigned int year, month, day, hour, minute, second;
  84. unsigned int week, twelve_hr, am_pm;
  85. unsigned int century, add_century = 0;
  86. ret = regmap_bulk_read(ds3232->regmap, DS3232_REG_SECONDS, buf, 7);
  87. if (ret)
  88. return ret;
  89. second = buf[0];
  90. minute = buf[1];
  91. hour = buf[2];
  92. week = buf[3];
  93. day = buf[4];
  94. month = buf[5];
  95. year = buf[6];
  96. /* Extract additional information for AM/PM and century */
  97. twelve_hr = hour & 0x40;
  98. am_pm = hour & 0x20;
  99. century = month & 0x80;
  100. /* Write to rtc_time structure */
  101. time->tm_sec = bcd2bin(second);
  102. time->tm_min = bcd2bin(minute);
  103. if (twelve_hr) {
  104. /* Convert to 24 hr */
  105. if (am_pm)
  106. time->tm_hour = bcd2bin(hour & 0x1F) + 12;
  107. else
  108. time->tm_hour = bcd2bin(hour & 0x1F);
  109. } else {
  110. time->tm_hour = bcd2bin(hour);
  111. }
  112. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  113. time->tm_wday = bcd2bin(week) - 1;
  114. time->tm_mday = bcd2bin(day);
  115. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  116. time->tm_mon = bcd2bin(month & 0x7F) - 1;
  117. if (century)
  118. add_century = 100;
  119. time->tm_year = bcd2bin(year) + add_century;
  120. return rtc_valid_tm(time);
  121. }
  122. static int ds3232_set_time(struct device *dev, struct rtc_time *time)
  123. {
  124. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  125. u8 buf[7];
  126. /* Extract time from rtc_time and load into ds3232*/
  127. buf[0] = bin2bcd(time->tm_sec);
  128. buf[1] = bin2bcd(time->tm_min);
  129. buf[2] = bin2bcd(time->tm_hour);
  130. /* Day of the week in linux range is 0~6 while 1~7 in RTC chip */
  131. buf[3] = bin2bcd(time->tm_wday + 1);
  132. buf[4] = bin2bcd(time->tm_mday); /* Date */
  133. /* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */
  134. buf[5] = bin2bcd(time->tm_mon + 1);
  135. if (time->tm_year >= 100) {
  136. buf[5] |= 0x80;
  137. buf[6] = bin2bcd(time->tm_year - 100);
  138. } else {
  139. buf[6] = bin2bcd(time->tm_year);
  140. }
  141. return regmap_bulk_write(ds3232->regmap, DS3232_REG_SECONDS, buf, 7);
  142. }
  143. /*
  144. * DS3232 has two alarm, we only use alarm1
  145. * According to linux specification, only support one-shot alarm
  146. * no periodic alarm mode
  147. */
  148. static int ds3232_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  149. {
  150. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  151. int control, stat;
  152. int ret;
  153. u8 buf[4];
  154. ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat);
  155. if (ret)
  156. goto out;
  157. ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control);
  158. if (ret)
  159. goto out;
  160. ret = regmap_bulk_read(ds3232->regmap, DS3232_REG_ALARM1, buf, 4);
  161. if (ret)
  162. goto out;
  163. alarm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
  164. alarm->time.tm_min = bcd2bin(buf[1] & 0x7F);
  165. alarm->time.tm_hour = bcd2bin(buf[2] & 0x7F);
  166. alarm->time.tm_mday = bcd2bin(buf[3] & 0x7F);
  167. alarm->enabled = !!(control & DS3232_REG_CR_A1IE);
  168. alarm->pending = !!(stat & DS3232_REG_SR_A1F);
  169. ret = 0;
  170. out:
  171. return ret;
  172. }
  173. /*
  174. * linux rtc-module does not support wday alarm
  175. * and only 24h time mode supported indeed
  176. */
  177. static int ds3232_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  178. {
  179. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  180. int control, stat;
  181. int ret;
  182. u8 buf[4];
  183. if (ds3232->irq <= 0)
  184. return -EINVAL;
  185. buf[0] = bin2bcd(alarm->time.tm_sec);
  186. buf[1] = bin2bcd(alarm->time.tm_min);
  187. buf[2] = bin2bcd(alarm->time.tm_hour);
  188. buf[3] = bin2bcd(alarm->time.tm_mday);
  189. /* clear alarm interrupt enable bit */
  190. ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control);
  191. if (ret)
  192. goto out;
  193. control &= ~(DS3232_REG_CR_A1IE | DS3232_REG_CR_A2IE);
  194. ret = regmap_write(ds3232->regmap, DS3232_REG_CR, control);
  195. if (ret)
  196. goto out;
  197. /* clear any pending alarm flag */
  198. ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat);
  199. if (ret)
  200. goto out;
  201. stat &= ~(DS3232_REG_SR_A1F | DS3232_REG_SR_A2F);
  202. ret = regmap_write(ds3232->regmap, DS3232_REG_SR, stat);
  203. if (ret)
  204. goto out;
  205. ret = regmap_bulk_write(ds3232->regmap, DS3232_REG_ALARM1, buf, 4);
  206. if (ret)
  207. goto out;
  208. if (alarm->enabled) {
  209. control |= DS3232_REG_CR_A1IE;
  210. ret = regmap_write(ds3232->regmap, DS3232_REG_CR, control);
  211. }
  212. out:
  213. return ret;
  214. }
  215. static int ds3232_update_alarm(struct device *dev, unsigned int enabled)
  216. {
  217. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  218. int control;
  219. int ret;
  220. ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control);
  221. if (ret)
  222. return ret;
  223. if (enabled)
  224. /* enable alarm1 interrupt */
  225. control |= DS3232_REG_CR_A1IE;
  226. else
  227. /* disable alarm1 interrupt */
  228. control &= ~(DS3232_REG_CR_A1IE);
  229. ret = regmap_write(ds3232->regmap, DS3232_REG_CR, control);
  230. return ret;
  231. }
  232. static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)
  233. {
  234. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  235. if (ds3232->irq <= 0)
  236. return -EINVAL;
  237. return ds3232_update_alarm(dev, enabled);
  238. }
  239. static irqreturn_t ds3232_irq(int irq, void *dev_id)
  240. {
  241. struct device *dev = dev_id;
  242. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  243. struct mutex *lock = &ds3232->rtc->ops_lock;
  244. int ret;
  245. int stat, control;
  246. mutex_lock(lock);
  247. ret = regmap_read(ds3232->regmap, DS3232_REG_SR, &stat);
  248. if (ret)
  249. goto unlock;
  250. if (stat & DS3232_REG_SR_A1F) {
  251. ret = regmap_read(ds3232->regmap, DS3232_REG_CR, &control);
  252. if (ret) {
  253. dev_warn(ds3232->dev,
  254. "Read Control Register error %d\n", ret);
  255. } else {
  256. /* disable alarm1 interrupt */
  257. control &= ~(DS3232_REG_CR_A1IE);
  258. ret = regmap_write(ds3232->regmap, DS3232_REG_CR,
  259. control);
  260. if (ret) {
  261. dev_warn(ds3232->dev,
  262. "Write Control Register error %d\n",
  263. ret);
  264. goto unlock;
  265. }
  266. /* clear the alarm pend flag */
  267. stat &= ~DS3232_REG_SR_A1F;
  268. ret = regmap_write(ds3232->regmap, DS3232_REG_SR, stat);
  269. if (ret) {
  270. dev_warn(ds3232->dev,
  271. "Write Status Register error %d\n",
  272. ret);
  273. goto unlock;
  274. }
  275. rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF);
  276. }
  277. }
  278. unlock:
  279. mutex_unlock(lock);
  280. return IRQ_HANDLED;
  281. }
  282. static const struct rtc_class_ops ds3232_rtc_ops = {
  283. .read_time = ds3232_read_time,
  284. .set_time = ds3232_set_time,
  285. .read_alarm = ds3232_read_alarm,
  286. .set_alarm = ds3232_set_alarm,
  287. .alarm_irq_enable = ds3232_alarm_irq_enable,
  288. };
  289. static int ds3232_probe(struct device *dev, struct regmap *regmap, int irq,
  290. const char *name)
  291. {
  292. struct ds3232 *ds3232;
  293. int ret;
  294. ds3232 = devm_kzalloc(dev, sizeof(*ds3232), GFP_KERNEL);
  295. if (!ds3232)
  296. return -ENOMEM;
  297. ds3232->regmap = regmap;
  298. ds3232->irq = irq;
  299. ds3232->dev = dev;
  300. dev_set_drvdata(dev, ds3232);
  301. ret = ds3232_check_rtc_status(dev);
  302. if (ret)
  303. return ret;
  304. ds3232->rtc = devm_rtc_device_register(dev, name, &ds3232_rtc_ops,
  305. THIS_MODULE);
  306. if (IS_ERR(ds3232->rtc))
  307. return PTR_ERR(ds3232->rtc);
  308. if (ds3232->irq > 0) {
  309. ret = devm_request_threaded_irq(dev, ds3232->irq, NULL,
  310. ds3232_irq,
  311. IRQF_SHARED | IRQF_ONESHOT,
  312. name, dev);
  313. if (ret) {
  314. ds3232->irq = 0;
  315. dev_err(dev, "unable to request IRQ\n");
  316. } else
  317. device_init_wakeup(dev, 1);
  318. }
  319. return 0;
  320. }
  321. #ifdef CONFIG_PM_SLEEP
  322. static int ds3232_suspend(struct device *dev)
  323. {
  324. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  325. if (device_may_wakeup(dev)) {
  326. if (enable_irq_wake(ds3232->irq))
  327. dev_warn_once(dev, "Cannot set wakeup source\n");
  328. }
  329. return 0;
  330. }
  331. static int ds3232_resume(struct device *dev)
  332. {
  333. struct ds3232 *ds3232 = dev_get_drvdata(dev);
  334. if (device_may_wakeup(dev))
  335. disable_irq_wake(ds3232->irq);
  336. return 0;
  337. }
  338. #endif
  339. static const struct dev_pm_ops ds3232_pm_ops = {
  340. SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend, ds3232_resume)
  341. };
  342. #if IS_ENABLED(CONFIG_I2C)
  343. static int ds3232_i2c_probe(struct i2c_client *client,
  344. const struct i2c_device_id *id)
  345. {
  346. struct regmap *regmap;
  347. static const struct regmap_config config = {
  348. .reg_bits = 8,
  349. .val_bits = 8,
  350. };
  351. regmap = devm_regmap_init_i2c(client, &config);
  352. if (IS_ERR(regmap)) {
  353. dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
  354. __func__, PTR_ERR(regmap));
  355. return PTR_ERR(regmap);
  356. }
  357. return ds3232_probe(&client->dev, regmap, client->irq, client->name);
  358. }
  359. static const struct i2c_device_id ds3232_id[] = {
  360. { "ds3232", 0 },
  361. { }
  362. };
  363. MODULE_DEVICE_TABLE(i2c, ds3232_id);
  364. static struct i2c_driver ds3232_driver = {
  365. .driver = {
  366. .name = "rtc-ds3232",
  367. .pm = &ds3232_pm_ops,
  368. },
  369. .probe = ds3232_i2c_probe,
  370. .id_table = ds3232_id,
  371. };
  372. static int ds3232_register_driver(void)
  373. {
  374. return i2c_add_driver(&ds3232_driver);
  375. }
  376. static void ds3232_unregister_driver(void)
  377. {
  378. i2c_del_driver(&ds3232_driver);
  379. }
  380. #else
  381. static int ds3232_register_driver(void)
  382. {
  383. return 0;
  384. }
  385. static void ds3232_unregister_driver(void)
  386. {
  387. }
  388. #endif
  389. #if IS_ENABLED(CONFIG_SPI_MASTER)
  390. static int ds3234_probe(struct spi_device *spi)
  391. {
  392. int res;
  393. unsigned int tmp;
  394. static const struct regmap_config config = {
  395. .reg_bits = 8,
  396. .val_bits = 8,
  397. .write_flag_mask = 0x80,
  398. };
  399. struct regmap *regmap;
  400. regmap = devm_regmap_init_spi(spi, &config);
  401. if (IS_ERR(regmap)) {
  402. dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
  403. __func__, PTR_ERR(regmap));
  404. return PTR_ERR(regmap);
  405. }
  406. spi->mode = SPI_MODE_3;
  407. spi->bits_per_word = 8;
  408. spi_setup(spi);
  409. res = regmap_read(regmap, DS3232_REG_SECONDS, &tmp);
  410. if (res)
  411. return res;
  412. /* Control settings
  413. *
  414. * CONTROL_REG
  415. * BIT 7 6 5 4 3 2 1 0
  416. * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
  417. *
  418. * 0 0 0 1 1 1 0 0
  419. *
  420. * CONTROL_STAT_REG
  421. * BIT 7 6 5 4 3 2 1 0
  422. * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
  423. *
  424. * 1 0 0 0 1 0 0 0
  425. */
  426. res = regmap_read(regmap, DS3232_REG_CR, &tmp);
  427. if (res)
  428. return res;
  429. res = regmap_write(regmap, DS3232_REG_CR, tmp & 0x1c);
  430. if (res)
  431. return res;
  432. res = regmap_read(regmap, DS3232_REG_SR, &tmp);
  433. if (res)
  434. return res;
  435. res = regmap_write(regmap, DS3232_REG_SR, tmp & 0x88);
  436. if (res)
  437. return res;
  438. /* Print our settings */
  439. res = regmap_read(regmap, DS3232_REG_CR, &tmp);
  440. if (res)
  441. return res;
  442. dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp);
  443. res = regmap_read(regmap, DS3232_REG_SR, &tmp);
  444. if (res)
  445. return res;
  446. dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp);
  447. return ds3232_probe(&spi->dev, regmap, spi->irq, "ds3234");
  448. }
  449. static struct spi_driver ds3234_driver = {
  450. .driver = {
  451. .name = "ds3234",
  452. },
  453. .probe = ds3234_probe,
  454. };
  455. static int ds3234_register_driver(void)
  456. {
  457. return spi_register_driver(&ds3234_driver);
  458. }
  459. static void ds3234_unregister_driver(void)
  460. {
  461. spi_unregister_driver(&ds3234_driver);
  462. }
  463. #else
  464. static int ds3234_register_driver(void)
  465. {
  466. return 0;
  467. }
  468. static void ds3234_unregister_driver(void)
  469. {
  470. }
  471. #endif
  472. static int __init ds323x_init(void)
  473. {
  474. int ret;
  475. ret = ds3232_register_driver();
  476. if (ret) {
  477. pr_err("Failed to register ds3232 driver: %d\n", ret);
  478. return ret;
  479. }
  480. ret = ds3234_register_driver();
  481. if (ret) {
  482. pr_err("Failed to register ds3234 driver: %d\n", ret);
  483. ds3232_unregister_driver();
  484. }
  485. return ret;
  486. }
  487. module_init(ds323x_init)
  488. static void __exit ds323x_exit(void)
  489. {
  490. ds3234_unregister_driver();
  491. ds3232_unregister_driver();
  492. }
  493. module_exit(ds323x_exit)
  494. MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");
  495. MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
  496. MODULE_DESCRIPTION("Maxim/Dallas DS3232/DS3234 RTC Driver");
  497. MODULE_LICENSE("GPL");
  498. MODULE_ALIAS("spi:ds3234");