rtc-spear.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 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 __devinit 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. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  308. if (!res) {
  309. dev_err(&pdev->dev, "no resource defined\n");
  310. return -EBUSY;
  311. }
  312. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  313. dev_err(&pdev->dev, "rtc region already claimed\n");
  314. return -EBUSY;
  315. }
  316. config = kzalloc(sizeof(*config), GFP_KERNEL);
  317. if (!config) {
  318. dev_err(&pdev->dev, "out of memory\n");
  319. status = -ENOMEM;
  320. goto err_release_region;
  321. }
  322. config->clk = clk_get(&pdev->dev, NULL);
  323. if (IS_ERR(config->clk)) {
  324. status = PTR_ERR(config->clk);
  325. goto err_kfree;
  326. }
  327. status = clk_enable(config->clk);
  328. if (status < 0)
  329. goto err_clk_put;
  330. config->ioaddr = ioremap(res->start, resource_size(res));
  331. if (!config->ioaddr) {
  332. dev_err(&pdev->dev, "ioremap fail\n");
  333. status = -ENOMEM;
  334. goto err_disable_clock;
  335. }
  336. spin_lock_init(&config->lock);
  337. platform_set_drvdata(pdev, config);
  338. config->rtc = rtc_device_register(pdev->name, &pdev->dev,
  339. &spear_rtc_ops, THIS_MODULE);
  340. if (IS_ERR(config->rtc)) {
  341. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  342. PTR_ERR(config->rtc));
  343. status = PTR_ERR(config->rtc);
  344. goto err_iounmap;
  345. }
  346. /* alarm irqs */
  347. irq = platform_get_irq(pdev, 0);
  348. if (irq < 0) {
  349. dev_err(&pdev->dev, "no update irq?\n");
  350. status = irq;
  351. goto err_clear_platdata;
  352. }
  353. status = request_irq(irq, spear_rtc_irq, 0, pdev->name, config);
  354. if (status) {
  355. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
  356. claimed\n", irq);
  357. goto err_clear_platdata;
  358. }
  359. if (!device_can_wakeup(&pdev->dev))
  360. device_init_wakeup(&pdev->dev, 1);
  361. return 0;
  362. err_clear_platdata:
  363. platform_set_drvdata(pdev, NULL);
  364. rtc_device_unregister(config->rtc);
  365. err_iounmap:
  366. iounmap(config->ioaddr);
  367. err_disable_clock:
  368. clk_disable(config->clk);
  369. err_clk_put:
  370. clk_put(config->clk);
  371. err_kfree:
  372. kfree(config);
  373. err_release_region:
  374. release_mem_region(res->start, resource_size(res));
  375. return status;
  376. }
  377. static int __devexit spear_rtc_remove(struct platform_device *pdev)
  378. {
  379. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  380. int irq;
  381. struct resource *res;
  382. /* leave rtc running, but disable irqs */
  383. spear_rtc_disable_interrupt(config);
  384. device_init_wakeup(&pdev->dev, 0);
  385. irq = platform_get_irq(pdev, 0);
  386. if (irq)
  387. free_irq(irq, pdev);
  388. clk_disable(config->clk);
  389. clk_put(config->clk);
  390. iounmap(config->ioaddr);
  391. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  392. if (res)
  393. release_mem_region(res->start, resource_size(res));
  394. platform_set_drvdata(pdev, NULL);
  395. rtc_device_unregister(config->rtc);
  396. kfree(config);
  397. return 0;
  398. }
  399. #ifdef CONFIG_PM
  400. static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  401. {
  402. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  403. int irq;
  404. irq = platform_get_irq(pdev, 0);
  405. if (device_may_wakeup(&pdev->dev)) {
  406. if (!enable_irq_wake(irq))
  407. config->irq_wake = 1;
  408. } else {
  409. spear_rtc_disable_interrupt(config);
  410. clk_disable(config->clk);
  411. }
  412. return 0;
  413. }
  414. static int spear_rtc_resume(struct platform_device *pdev)
  415. {
  416. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  417. int irq;
  418. irq = platform_get_irq(pdev, 0);
  419. if (device_may_wakeup(&pdev->dev)) {
  420. if (config->irq_wake) {
  421. disable_irq_wake(irq);
  422. config->irq_wake = 0;
  423. }
  424. } else {
  425. clk_enable(config->clk);
  426. spear_rtc_enable_interrupt(config);
  427. }
  428. return 0;
  429. }
  430. #else
  431. #define spear_rtc_suspend NULL
  432. #define spear_rtc_resume NULL
  433. #endif
  434. static void spear_rtc_shutdown(struct platform_device *pdev)
  435. {
  436. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  437. spear_rtc_disable_interrupt(config);
  438. clk_disable(config->clk);
  439. }
  440. #ifdef CONFIG_OF
  441. static const struct of_device_id spear_rtc_id_table[] = {
  442. { .compatible = "st,spear600-rtc" },
  443. {}
  444. };
  445. MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
  446. #endif
  447. static struct platform_driver spear_rtc_driver = {
  448. .probe = spear_rtc_probe,
  449. .remove = __devexit_p(spear_rtc_remove),
  450. .suspend = spear_rtc_suspend,
  451. .resume = spear_rtc_resume,
  452. .shutdown = spear_rtc_shutdown,
  453. .driver = {
  454. .name = "rtc-spear",
  455. .of_match_table = of_match_ptr(spear_rtc_id_table),
  456. },
  457. };
  458. module_platform_driver(spear_rtc_driver);
  459. MODULE_ALIAS("platform:rtc-spear");
  460. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  461. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  462. MODULE_LICENSE("GPL");