rtc-spear.c 14 KB

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