rtc-spear.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * drivers/rtc/rtc-spear.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Rajeev Kumar<rajeev-dlh.kumar@st.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. /* RTC registers */
  24. #define TIME_REG 0x00
  25. #define DATE_REG 0x04
  26. #define ALARM_TIME_REG 0x08
  27. #define ALARM_DATE_REG 0x0C
  28. #define CTRL_REG 0x10
  29. #define STATUS_REG 0x14
  30. /* TIME_REG & ALARM_TIME_REG */
  31. #define SECONDS_UNITS (0xf<<0) /* seconds units position */
  32. #define SECONDS_TENS (0x7<<4) /* seconds tens position */
  33. #define MINUTES_UNITS (0xf<<8) /* minutes units position */
  34. #define MINUTES_TENS (0x7<<12) /* minutes tens position */
  35. #define HOURS_UNITS (0xf<<16) /* hours units position */
  36. #define HOURS_TENS (0x3<<20) /* hours tens position */
  37. /* DATE_REG & ALARM_DATE_REG */
  38. #define DAYS_UNITS (0xf<<0) /* days units position */
  39. #define DAYS_TENS (0x3<<4) /* days tens position */
  40. #define MONTHS_UNITS (0xf<<8) /* months units position */
  41. #define MONTHS_TENS (0x1<<12) /* months tens position */
  42. #define YEARS_UNITS (0xf<<16) /* years units position */
  43. #define YEARS_TENS (0xf<<20) /* years tens position */
  44. #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
  45. #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
  46. /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
  47. #define SECOND_SHIFT 0x00 /* seconds units */
  48. #define MINUTE_SHIFT 0x08 /* minutes units position */
  49. #define HOUR_SHIFT 0x10 /* hours units position */
  50. #define MDAY_SHIFT 0x00 /* Month day shift */
  51. #define MONTH_SHIFT 0x08 /* Month shift */
  52. #define YEAR_SHIFT 0x10 /* Year shift */
  53. #define SECOND_MASK 0x7F
  54. #define MIN_MASK 0x7F
  55. #define HOUR_MASK 0x3F
  56. #define DAY_MASK 0x3F
  57. #define MONTH_MASK 0x7F
  58. #define YEAR_MASK 0xFFFF
  59. /* date reg equal to time reg, for debug only */
  60. #define TIME_BYP (1<<9)
  61. #define INT_ENABLE (1<<31) /* interrupt enable */
  62. /* STATUS_REG */
  63. #define CLK_UNCONNECTED (1<<0)
  64. #define PEND_WR_TIME (1<<2)
  65. #define PEND_WR_DATE (1<<3)
  66. #define LOST_WR_TIME (1<<4)
  67. #define LOST_WR_DATE (1<<5)
  68. #define RTC_INT_MASK (1<<31)
  69. #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
  70. #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
  71. struct spear_rtc_config {
  72. struct rtc_device *rtc;
  73. struct clk *clk;
  74. spinlock_t lock;
  75. void __iomem *ioaddr;
  76. unsigned int irq_wake;
  77. };
  78. static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
  79. {
  80. unsigned int val;
  81. unsigned long flags;
  82. spin_lock_irqsave(&config->lock, flags);
  83. val = readl(config->ioaddr + STATUS_REG);
  84. val |= RTC_INT_MASK;
  85. writel(val, config->ioaddr + STATUS_REG);
  86. spin_unlock_irqrestore(&config->lock, flags);
  87. }
  88. static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
  89. {
  90. unsigned int val;
  91. val = readl(config->ioaddr + CTRL_REG);
  92. if (!(val & INT_ENABLE)) {
  93. spear_rtc_clear_interrupt(config);
  94. val |= INT_ENABLE;
  95. writel(val, config->ioaddr + CTRL_REG);
  96. }
  97. }
  98. static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
  99. {
  100. unsigned int val;
  101. val = readl(config->ioaddr + CTRL_REG);
  102. if (val & INT_ENABLE) {
  103. val &= ~INT_ENABLE;
  104. writel(val, config->ioaddr + CTRL_REG);
  105. }
  106. }
  107. static inline int is_write_complete(struct spear_rtc_config *config)
  108. {
  109. int ret = 0;
  110. unsigned long flags;
  111. spin_lock_irqsave(&config->lock, flags);
  112. if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
  113. ret = -EIO;
  114. spin_unlock_irqrestore(&config->lock, flags);
  115. return ret;
  116. }
  117. static void rtc_wait_not_busy(struct spear_rtc_config *config)
  118. {
  119. int status, count = 0;
  120. unsigned long flags;
  121. /* Assuming BUSY may stay active for 80 msec) */
  122. for (count = 0; count < 80; count++) {
  123. spin_lock_irqsave(&config->lock, flags);
  124. status = readl(config->ioaddr + STATUS_REG);
  125. spin_unlock_irqrestore(&config->lock, flags);
  126. if ((status & STATUS_BUSY) == 0)
  127. break;
  128. /* check status busy, after each msec */
  129. msleep(1);
  130. }
  131. }
  132. static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
  133. {
  134. struct spear_rtc_config *config = dev_id;
  135. unsigned long flags, events = 0;
  136. unsigned int irq_data;
  137. spin_lock_irqsave(&config->lock, flags);
  138. irq_data = readl(config->ioaddr + STATUS_REG);
  139. spin_unlock_irqrestore(&config->lock, flags);
  140. if ((irq_data & RTC_INT_MASK)) {
  141. spear_rtc_clear_interrupt(config);
  142. events = RTC_IRQF | RTC_AF;
  143. rtc_update_irq(config->rtc, 1, events);
  144. return IRQ_HANDLED;
  145. } else
  146. return IRQ_NONE;
  147. }
  148. static int tm2bcd(struct rtc_time *tm)
  149. {
  150. if (rtc_valid_tm(tm) != 0)
  151. return -EINVAL;
  152. tm->tm_sec = bin2bcd(tm->tm_sec);
  153. tm->tm_min = bin2bcd(tm->tm_min);
  154. tm->tm_hour = bin2bcd(tm->tm_hour);
  155. tm->tm_mday = bin2bcd(tm->tm_mday);
  156. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  157. tm->tm_year = bin2bcd(tm->tm_year);
  158. return 0;
  159. }
  160. static void bcd2tm(struct rtc_time *tm)
  161. {
  162. tm->tm_sec = bcd2bin(tm->tm_sec);
  163. tm->tm_min = bcd2bin(tm->tm_min);
  164. tm->tm_hour = bcd2bin(tm->tm_hour);
  165. tm->tm_mday = bcd2bin(tm->tm_mday);
  166. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  167. /* epoch == 1900 */
  168. tm->tm_year = bcd2bin(tm->tm_year);
  169. }
  170. /*
  171. * spear_rtc_read_time - set the time
  172. * @dev: rtc device in use
  173. * @tm: holds date and time
  174. *
  175. * This function read time and date. On success it will return 0
  176. * otherwise -ve error is returned.
  177. */
  178. static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
  179. {
  180. struct spear_rtc_config *config = dev_get_drvdata(dev);
  181. unsigned int time, date;
  182. /* we don't report wday/yday/isdst ... */
  183. rtc_wait_not_busy(config);
  184. time = readl(config->ioaddr + TIME_REG);
  185. date = readl(config->ioaddr + DATE_REG);
  186. tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  187. tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  188. tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  189. tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  190. tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  191. tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  192. bcd2tm(tm);
  193. return 0;
  194. }
  195. /*
  196. * spear_rtc_set_time - set the time
  197. * @dev: rtc device in use
  198. * @tm: holds date and time
  199. *
  200. * This function set time and date. On success it will return 0
  201. * otherwise -ve error is returned.
  202. */
  203. static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
  204. {
  205. struct spear_rtc_config *config = dev_get_drvdata(dev);
  206. unsigned int time, date;
  207. if (tm2bcd(tm) < 0)
  208. return -EINVAL;
  209. rtc_wait_not_busy(config);
  210. time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
  211. (tm->tm_hour << HOUR_SHIFT);
  212. date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
  213. (tm->tm_year << YEAR_SHIFT);
  214. writel(time, config->ioaddr + TIME_REG);
  215. writel(date, config->ioaddr + DATE_REG);
  216. return is_write_complete(config);
  217. }
  218. /*
  219. * spear_rtc_read_alarm - read the alarm time
  220. * @dev: rtc device in use
  221. * @alm: holds alarm date and time
  222. *
  223. * This function read alarm time and date. On success it will return 0
  224. * otherwise -ve error is returned.
  225. */
  226. static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  227. {
  228. struct spear_rtc_config *config = dev_get_drvdata(dev);
  229. unsigned int time, date;
  230. rtc_wait_not_busy(config);
  231. time = readl(config->ioaddr + ALARM_TIME_REG);
  232. date = readl(config->ioaddr + ALARM_DATE_REG);
  233. alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  234. alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  235. alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  236. alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  237. alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  238. alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  239. bcd2tm(&alm->time);
  240. alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
  241. return 0;
  242. }
  243. /*
  244. * spear_rtc_set_alarm - set the alarm time
  245. * @dev: rtc device in use
  246. * @alm: holds alarm date and time
  247. *
  248. * This function set alarm time and date. On success it will return 0
  249. * otherwise -ve error is returned.
  250. */
  251. static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  252. {
  253. struct spear_rtc_config *config = dev_get_drvdata(dev);
  254. unsigned int time, date;
  255. int err;
  256. if (tm2bcd(&alm->time) < 0)
  257. return -EINVAL;
  258. rtc_wait_not_busy(config);
  259. time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
  260. MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
  261. date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
  262. MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
  263. writel(time, config->ioaddr + ALARM_TIME_REG);
  264. writel(date, config->ioaddr + ALARM_DATE_REG);
  265. err = is_write_complete(config);
  266. if (err < 0)
  267. return err;
  268. if (alm->enabled)
  269. spear_rtc_enable_interrupt(config);
  270. else
  271. spear_rtc_disable_interrupt(config);
  272. return 0;
  273. }
  274. static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
  275. {
  276. struct spear_rtc_config *config = dev_get_drvdata(dev);
  277. int ret = 0;
  278. spear_rtc_clear_interrupt(config);
  279. switch (enabled) {
  280. case 0:
  281. /* alarm off */
  282. spear_rtc_disable_interrupt(config);
  283. break;
  284. case 1:
  285. /* alarm on */
  286. spear_rtc_enable_interrupt(config);
  287. break;
  288. default:
  289. ret = -EINVAL;
  290. break;
  291. }
  292. return ret;
  293. }
  294. static const struct rtc_class_ops spear_rtc_ops = {
  295. .read_time = spear_rtc_read_time,
  296. .set_time = spear_rtc_set_time,
  297. .read_alarm = spear_rtc_read_alarm,
  298. .set_alarm = spear_rtc_set_alarm,
  299. .alarm_irq_enable = spear_alarm_irq_enable,
  300. };
  301. static int spear_rtc_probe(struct platform_device *pdev)
  302. {
  303. struct resource *res;
  304. struct spear_rtc_config *config;
  305. int status = 0;
  306. int irq;
  307. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  308. if (!config)
  309. return -ENOMEM;
  310. /* alarm irqs */
  311. irq = platform_get_irq(pdev, 0);
  312. if (irq < 0) {
  313. dev_err(&pdev->dev, "no update irq?\n");
  314. return irq;
  315. }
  316. status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
  317. config);
  318. if (status) {
  319. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
  320. irq);
  321. return status;
  322. }
  323. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  324. config->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  325. if (IS_ERR(config->ioaddr))
  326. return PTR_ERR(config->ioaddr);
  327. config->clk = devm_clk_get(&pdev->dev, NULL);
  328. if (IS_ERR(config->clk))
  329. return PTR_ERR(config->clk);
  330. status = clk_prepare_enable(config->clk);
  331. if (status < 0)
  332. return status;
  333. spin_lock_init(&config->lock);
  334. platform_set_drvdata(pdev, config);
  335. config->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  336. &spear_rtc_ops, THIS_MODULE);
  337. if (IS_ERR(config->rtc)) {
  338. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  339. PTR_ERR(config->rtc));
  340. status = PTR_ERR(config->rtc);
  341. goto err_disable_clock;
  342. }
  343. config->rtc->uie_unsupported = 1;
  344. if (!device_can_wakeup(&pdev->dev))
  345. device_init_wakeup(&pdev->dev, 1);
  346. return 0;
  347. err_disable_clock:
  348. clk_disable_unprepare(config->clk);
  349. return status;
  350. }
  351. static int spear_rtc_remove(struct platform_device *pdev)
  352. {
  353. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  354. spear_rtc_disable_interrupt(config);
  355. clk_disable_unprepare(config->clk);
  356. device_init_wakeup(&pdev->dev, 0);
  357. return 0;
  358. }
  359. #ifdef CONFIG_PM_SLEEP
  360. static int spear_rtc_suspend(struct device *dev)
  361. {
  362. struct platform_device *pdev = to_platform_device(dev);
  363. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  364. int irq;
  365. irq = platform_get_irq(pdev, 0);
  366. if (device_may_wakeup(&pdev->dev)) {
  367. if (!enable_irq_wake(irq))
  368. config->irq_wake = 1;
  369. } else {
  370. spear_rtc_disable_interrupt(config);
  371. clk_disable(config->clk);
  372. }
  373. return 0;
  374. }
  375. static int spear_rtc_resume(struct device *dev)
  376. {
  377. struct platform_device *pdev = to_platform_device(dev);
  378. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  379. int irq;
  380. irq = platform_get_irq(pdev, 0);
  381. if (device_may_wakeup(&pdev->dev)) {
  382. if (config->irq_wake) {
  383. disable_irq_wake(irq);
  384. config->irq_wake = 0;
  385. }
  386. } else {
  387. clk_enable(config->clk);
  388. spear_rtc_enable_interrupt(config);
  389. }
  390. return 0;
  391. }
  392. #endif
  393. static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
  394. static void spear_rtc_shutdown(struct platform_device *pdev)
  395. {
  396. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  397. spear_rtc_disable_interrupt(config);
  398. clk_disable(config->clk);
  399. }
  400. #ifdef CONFIG_OF
  401. static const struct of_device_id spear_rtc_id_table[] = {
  402. { .compatible = "st,spear600-rtc" },
  403. {}
  404. };
  405. MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
  406. #endif
  407. static struct platform_driver spear_rtc_driver = {
  408. .probe = spear_rtc_probe,
  409. .remove = spear_rtc_remove,
  410. .shutdown = spear_rtc_shutdown,
  411. .driver = {
  412. .name = "rtc-spear",
  413. .pm = &spear_rtc_pm_ops,
  414. .of_match_table = of_match_ptr(spear_rtc_id_table),
  415. },
  416. };
  417. module_platform_driver(spear_rtc_driver);
  418. MODULE_ALIAS("platform:rtc-spear");
  419. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  420. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  421. MODULE_LICENSE("GPL");