rtc-spear.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 void tm2bcd(struct rtc_time *tm)
  149. {
  150. tm->tm_sec = bin2bcd(tm->tm_sec);
  151. tm->tm_min = bin2bcd(tm->tm_min);
  152. tm->tm_hour = bin2bcd(tm->tm_hour);
  153. tm->tm_mday = bin2bcd(tm->tm_mday);
  154. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  155. tm->tm_year = bin2bcd(tm->tm_year);
  156. }
  157. static void bcd2tm(struct rtc_time *tm)
  158. {
  159. tm->tm_sec = bcd2bin(tm->tm_sec);
  160. tm->tm_min = bcd2bin(tm->tm_min);
  161. tm->tm_hour = bcd2bin(tm->tm_hour);
  162. tm->tm_mday = bcd2bin(tm->tm_mday);
  163. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  164. /* epoch == 1900 */
  165. tm->tm_year = bcd2bin(tm->tm_year);
  166. }
  167. /*
  168. * spear_rtc_read_time - set the time
  169. * @dev: rtc device in use
  170. * @tm: holds date and time
  171. *
  172. * This function read time and date. On success it will return 0
  173. * otherwise -ve error is returned.
  174. */
  175. static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
  176. {
  177. struct spear_rtc_config *config = dev_get_drvdata(dev);
  178. unsigned int time, date;
  179. /* we don't report wday/yday/isdst ... */
  180. rtc_wait_not_busy(config);
  181. time = readl(config->ioaddr + TIME_REG);
  182. date = readl(config->ioaddr + DATE_REG);
  183. tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  184. tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  185. tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  186. tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  187. tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  188. tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  189. bcd2tm(tm);
  190. return 0;
  191. }
  192. /*
  193. * spear_rtc_set_time - set the time
  194. * @dev: rtc device in use
  195. * @tm: holds date and time
  196. *
  197. * This function set time and date. On success it will return 0
  198. * otherwise -ve error is returned.
  199. */
  200. static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
  201. {
  202. struct spear_rtc_config *config = dev_get_drvdata(dev);
  203. unsigned int time, date;
  204. tm2bcd(tm);
  205. rtc_wait_not_busy(config);
  206. time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
  207. (tm->tm_hour << HOUR_SHIFT);
  208. date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
  209. (tm->tm_year << YEAR_SHIFT);
  210. writel(time, config->ioaddr + TIME_REG);
  211. writel(date, config->ioaddr + DATE_REG);
  212. return is_write_complete(config);
  213. }
  214. /*
  215. * spear_rtc_read_alarm - read the alarm time
  216. * @dev: rtc device in use
  217. * @alm: holds alarm date and time
  218. *
  219. * This function read alarm time and date. On success it will return 0
  220. * otherwise -ve error is returned.
  221. */
  222. static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  223. {
  224. struct spear_rtc_config *config = dev_get_drvdata(dev);
  225. unsigned int time, date;
  226. rtc_wait_not_busy(config);
  227. time = readl(config->ioaddr + ALARM_TIME_REG);
  228. date = readl(config->ioaddr + ALARM_DATE_REG);
  229. alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  230. alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  231. alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  232. alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  233. alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  234. alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  235. bcd2tm(&alm->time);
  236. alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
  237. return 0;
  238. }
  239. /*
  240. * spear_rtc_set_alarm - set the alarm time
  241. * @dev: rtc device in use
  242. * @alm: holds alarm date and time
  243. *
  244. * This function set alarm time and date. On success it will return 0
  245. * otherwise -ve error is returned.
  246. */
  247. static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  248. {
  249. struct spear_rtc_config *config = dev_get_drvdata(dev);
  250. unsigned int time, date;
  251. int err;
  252. tm2bcd(&alm->time);
  253. rtc_wait_not_busy(config);
  254. time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
  255. MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
  256. date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
  257. MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
  258. writel(time, config->ioaddr + ALARM_TIME_REG);
  259. writel(date, config->ioaddr + ALARM_DATE_REG);
  260. err = is_write_complete(config);
  261. if (err < 0)
  262. return err;
  263. if (alm->enabled)
  264. spear_rtc_enable_interrupt(config);
  265. else
  266. spear_rtc_disable_interrupt(config);
  267. return 0;
  268. }
  269. static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
  270. {
  271. struct spear_rtc_config *config = dev_get_drvdata(dev);
  272. int ret = 0;
  273. spear_rtc_clear_interrupt(config);
  274. switch (enabled) {
  275. case 0:
  276. /* alarm off */
  277. spear_rtc_disable_interrupt(config);
  278. break;
  279. case 1:
  280. /* alarm on */
  281. spear_rtc_enable_interrupt(config);
  282. break;
  283. default:
  284. ret = -EINVAL;
  285. break;
  286. }
  287. return ret;
  288. }
  289. static const struct rtc_class_ops spear_rtc_ops = {
  290. .read_time = spear_rtc_read_time,
  291. .set_time = spear_rtc_set_time,
  292. .read_alarm = spear_rtc_read_alarm,
  293. .set_alarm = spear_rtc_set_alarm,
  294. .alarm_irq_enable = spear_alarm_irq_enable,
  295. };
  296. static int spear_rtc_probe(struct platform_device *pdev)
  297. {
  298. struct resource *res;
  299. struct spear_rtc_config *config;
  300. int status = 0;
  301. int irq;
  302. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  303. if (!config)
  304. return -ENOMEM;
  305. /* alarm irqs */
  306. irq = platform_get_irq(pdev, 0);
  307. if (irq < 0) {
  308. dev_err(&pdev->dev, "no update irq?\n");
  309. return irq;
  310. }
  311. status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
  312. config);
  313. if (status) {
  314. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
  315. irq);
  316. return status;
  317. }
  318. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  319. config->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  320. if (IS_ERR(config->ioaddr))
  321. return PTR_ERR(config->ioaddr);
  322. config->clk = devm_clk_get(&pdev->dev, NULL);
  323. if (IS_ERR(config->clk))
  324. return PTR_ERR(config->clk);
  325. status = clk_prepare_enable(config->clk);
  326. if (status < 0)
  327. return status;
  328. spin_lock_init(&config->lock);
  329. platform_set_drvdata(pdev, config);
  330. config->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  331. &spear_rtc_ops, THIS_MODULE);
  332. if (IS_ERR(config->rtc)) {
  333. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  334. PTR_ERR(config->rtc));
  335. status = PTR_ERR(config->rtc);
  336. goto err_disable_clock;
  337. }
  338. config->rtc->uie_unsupported = 1;
  339. if (!device_can_wakeup(&pdev->dev))
  340. device_init_wakeup(&pdev->dev, 1);
  341. return 0;
  342. err_disable_clock:
  343. clk_disable_unprepare(config->clk);
  344. return status;
  345. }
  346. static int spear_rtc_remove(struct platform_device *pdev)
  347. {
  348. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  349. spear_rtc_disable_interrupt(config);
  350. clk_disable_unprepare(config->clk);
  351. device_init_wakeup(&pdev->dev, 0);
  352. return 0;
  353. }
  354. #ifdef CONFIG_PM_SLEEP
  355. static int spear_rtc_suspend(struct device *dev)
  356. {
  357. struct platform_device *pdev = to_platform_device(dev);
  358. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  359. int irq;
  360. irq = platform_get_irq(pdev, 0);
  361. if (device_may_wakeup(&pdev->dev)) {
  362. if (!enable_irq_wake(irq))
  363. config->irq_wake = 1;
  364. } else {
  365. spear_rtc_disable_interrupt(config);
  366. clk_disable(config->clk);
  367. }
  368. return 0;
  369. }
  370. static int spear_rtc_resume(struct device *dev)
  371. {
  372. struct platform_device *pdev = to_platform_device(dev);
  373. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  374. int irq;
  375. irq = platform_get_irq(pdev, 0);
  376. if (device_may_wakeup(&pdev->dev)) {
  377. if (config->irq_wake) {
  378. disable_irq_wake(irq);
  379. config->irq_wake = 0;
  380. }
  381. } else {
  382. clk_enable(config->clk);
  383. spear_rtc_enable_interrupt(config);
  384. }
  385. return 0;
  386. }
  387. #endif
  388. static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
  389. static void spear_rtc_shutdown(struct platform_device *pdev)
  390. {
  391. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  392. spear_rtc_disable_interrupt(config);
  393. clk_disable(config->clk);
  394. }
  395. #ifdef CONFIG_OF
  396. static const struct of_device_id spear_rtc_id_table[] = {
  397. { .compatible = "st,spear600-rtc" },
  398. {}
  399. };
  400. MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
  401. #endif
  402. static struct platform_driver spear_rtc_driver = {
  403. .probe = spear_rtc_probe,
  404. .remove = spear_rtc_remove,
  405. .shutdown = spear_rtc_shutdown,
  406. .driver = {
  407. .name = "rtc-spear",
  408. .pm = &spear_rtc_pm_ops,
  409. .of_match_table = of_match_ptr(spear_rtc_id_table),
  410. },
  411. };
  412. module_platform_driver(spear_rtc_driver);
  413. MODULE_ALIAS("platform:rtc-spear");
  414. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  415. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  416. MODULE_LICENSE("GPL");