rtc-vt8500.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * drivers/rtc/rtc-vt8500.c
  3. *
  4. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  5. *
  6. * Based on rtc-pxa.c
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/rtc.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/bcd.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. /*
  27. * Register definitions
  28. */
  29. #define VT8500_RTC_TS 0x00 /* Time set */
  30. #define VT8500_RTC_DS 0x04 /* Date set */
  31. #define VT8500_RTC_AS 0x08 /* Alarm set */
  32. #define VT8500_RTC_CR 0x0c /* Control */
  33. #define VT8500_RTC_TR 0x10 /* Time read */
  34. #define VT8500_RTC_DR 0x14 /* Date read */
  35. #define VT8500_RTC_WS 0x18 /* Write status */
  36. #define VT8500_RTC_CL 0x20 /* Calibration */
  37. #define VT8500_RTC_IS 0x24 /* Interrupt status */
  38. #define VT8500_RTC_ST 0x28 /* Status */
  39. #define INVALID_TIME_BIT (1 << 31)
  40. #define DATE_CENTURY_S 19
  41. #define DATE_YEAR_S 11
  42. #define DATE_YEAR_MASK (0xff << DATE_YEAR_S)
  43. #define DATE_MONTH_S 6
  44. #define DATE_MONTH_MASK (0x1f << DATE_MONTH_S)
  45. #define DATE_DAY_MASK 0x3f
  46. #define TIME_DOW_S 20
  47. #define TIME_DOW_MASK (0x07 << TIME_DOW_S)
  48. #define TIME_HOUR_S 14
  49. #define TIME_HOUR_MASK (0x3f << TIME_HOUR_S)
  50. #define TIME_MIN_S 7
  51. #define TIME_MIN_MASK (0x7f << TIME_MIN_S)
  52. #define TIME_SEC_MASK 0x7f
  53. #define ALARM_DAY_S 20
  54. #define ALARM_DAY_MASK (0x3f << ALARM_DAY_S)
  55. #define ALARM_DAY_BIT (1 << 29)
  56. #define ALARM_HOUR_BIT (1 << 28)
  57. #define ALARM_MIN_BIT (1 << 27)
  58. #define ALARM_SEC_BIT (1 << 26)
  59. #define ALARM_ENABLE_MASK (ALARM_DAY_BIT \
  60. | ALARM_HOUR_BIT \
  61. | ALARM_MIN_BIT \
  62. | ALARM_SEC_BIT)
  63. #define VT8500_RTC_CR_ENABLE (1 << 0) /* Enable RTC */
  64. #define VT8500_RTC_CR_12H (1 << 1) /* 12h time format */
  65. #define VT8500_RTC_CR_SM_ENABLE (1 << 2) /* Enable periodic irqs */
  66. #define VT8500_RTC_CR_SM_SEC (1 << 3) /* 0: 1Hz/60, 1: 1Hz */
  67. #define VT8500_RTC_CR_CALIB (1 << 4) /* Enable calibration */
  68. #define VT8500_RTC_IS_ALARM (1 << 0) /* Alarm interrupt status */
  69. struct vt8500_rtc {
  70. void __iomem *regbase;
  71. int irq_alarm;
  72. struct rtc_device *rtc;
  73. spinlock_t lock; /* Protects this structure */
  74. };
  75. static irqreturn_t vt8500_rtc_irq(int irq, void *dev_id)
  76. {
  77. struct vt8500_rtc *vt8500_rtc = dev_id;
  78. u32 isr;
  79. unsigned long events = 0;
  80. spin_lock(&vt8500_rtc->lock);
  81. /* clear interrupt sources */
  82. isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
  83. writel(isr, vt8500_rtc->regbase + VT8500_RTC_IS);
  84. spin_unlock(&vt8500_rtc->lock);
  85. if (isr & VT8500_RTC_IS_ALARM)
  86. events |= RTC_AF | RTC_IRQF;
  87. rtc_update_irq(vt8500_rtc->rtc, 1, events);
  88. return IRQ_HANDLED;
  89. }
  90. static int vt8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
  93. u32 date, time;
  94. date = readl(vt8500_rtc->regbase + VT8500_RTC_DR);
  95. time = readl(vt8500_rtc->regbase + VT8500_RTC_TR);
  96. tm->tm_sec = bcd2bin(time & TIME_SEC_MASK);
  97. tm->tm_min = bcd2bin((time & TIME_MIN_MASK) >> TIME_MIN_S);
  98. tm->tm_hour = bcd2bin((time & TIME_HOUR_MASK) >> TIME_HOUR_S);
  99. tm->tm_mday = bcd2bin(date & DATE_DAY_MASK);
  100. tm->tm_mon = bcd2bin((date & DATE_MONTH_MASK) >> DATE_MONTH_S) - 1;
  101. tm->tm_year = bcd2bin((date & DATE_YEAR_MASK) >> DATE_YEAR_S)
  102. + ((date >> DATE_CENTURY_S) & 1 ? 200 : 100);
  103. tm->tm_wday = (time & TIME_DOW_MASK) >> TIME_DOW_S;
  104. return 0;
  105. }
  106. static int vt8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
  107. {
  108. struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
  109. if (tm->tm_year < 100) {
  110. dev_warn(dev, "Only years 2000-2199 are supported by the "
  111. "hardware!\n");
  112. return -EINVAL;
  113. }
  114. writel((bin2bcd(tm->tm_year % 100) << DATE_YEAR_S)
  115. | (bin2bcd(tm->tm_mon + 1) << DATE_MONTH_S)
  116. | (bin2bcd(tm->tm_mday))
  117. | ((tm->tm_year >= 200) << DATE_CENTURY_S),
  118. vt8500_rtc->regbase + VT8500_RTC_DS);
  119. writel((bin2bcd(tm->tm_wday) << TIME_DOW_S)
  120. | (bin2bcd(tm->tm_hour) << TIME_HOUR_S)
  121. | (bin2bcd(tm->tm_min) << TIME_MIN_S)
  122. | (bin2bcd(tm->tm_sec)),
  123. vt8500_rtc->regbase + VT8500_RTC_TS);
  124. return 0;
  125. }
  126. static int vt8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  127. {
  128. struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
  129. u32 isr, alarm;
  130. alarm = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
  131. isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
  132. alrm->time.tm_mday = bcd2bin((alarm & ALARM_DAY_MASK) >> ALARM_DAY_S);
  133. alrm->time.tm_hour = bcd2bin((alarm & TIME_HOUR_MASK) >> TIME_HOUR_S);
  134. alrm->time.tm_min = bcd2bin((alarm & TIME_MIN_MASK) >> TIME_MIN_S);
  135. alrm->time.tm_sec = bcd2bin((alarm & TIME_SEC_MASK));
  136. alrm->enabled = (alarm & ALARM_ENABLE_MASK) ? 1 : 0;
  137. alrm->pending = (isr & VT8500_RTC_IS_ALARM) ? 1 : 0;
  138. return rtc_valid_tm(&alrm->time);
  139. }
  140. static int vt8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  141. {
  142. struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
  143. writel((alrm->enabled ? ALARM_ENABLE_MASK : 0)
  144. | (bin2bcd(alrm->time.tm_mday) << ALARM_DAY_S)
  145. | (bin2bcd(alrm->time.tm_hour) << TIME_HOUR_S)
  146. | (bin2bcd(alrm->time.tm_min) << TIME_MIN_S)
  147. | (bin2bcd(alrm->time.tm_sec)),
  148. vt8500_rtc->regbase + VT8500_RTC_AS);
  149. return 0;
  150. }
  151. static int vt8500_alarm_irq_enable(struct device *dev, unsigned int enabled)
  152. {
  153. struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
  154. unsigned long tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
  155. if (enabled)
  156. tmp |= ALARM_ENABLE_MASK;
  157. else
  158. tmp &= ~ALARM_ENABLE_MASK;
  159. writel(tmp, vt8500_rtc->regbase + VT8500_RTC_AS);
  160. return 0;
  161. }
  162. static const struct rtc_class_ops vt8500_rtc_ops = {
  163. .read_time = vt8500_rtc_read_time,
  164. .set_time = vt8500_rtc_set_time,
  165. .read_alarm = vt8500_rtc_read_alarm,
  166. .set_alarm = vt8500_rtc_set_alarm,
  167. .alarm_irq_enable = vt8500_alarm_irq_enable,
  168. };
  169. static int vt8500_rtc_probe(struct platform_device *pdev)
  170. {
  171. struct vt8500_rtc *vt8500_rtc;
  172. struct resource *res;
  173. int ret;
  174. vt8500_rtc = devm_kzalloc(&pdev->dev,
  175. sizeof(struct vt8500_rtc), GFP_KERNEL);
  176. if (!vt8500_rtc)
  177. return -ENOMEM;
  178. spin_lock_init(&vt8500_rtc->lock);
  179. platform_set_drvdata(pdev, vt8500_rtc);
  180. vt8500_rtc->irq_alarm = platform_get_irq(pdev, 0);
  181. if (vt8500_rtc->irq_alarm < 0) {
  182. dev_err(&pdev->dev, "No alarm IRQ resource defined\n");
  183. return vt8500_rtc->irq_alarm;
  184. }
  185. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  186. vt8500_rtc->regbase = devm_ioremap_resource(&pdev->dev, res);
  187. if (IS_ERR(vt8500_rtc->regbase))
  188. return PTR_ERR(vt8500_rtc->regbase);
  189. /* Enable RTC and set it to 24-hour mode */
  190. writel(VT8500_RTC_CR_ENABLE,
  191. vt8500_rtc->regbase + VT8500_RTC_CR);
  192. vt8500_rtc->rtc = devm_rtc_device_register(&pdev->dev, "vt8500-rtc",
  193. &vt8500_rtc_ops, THIS_MODULE);
  194. if (IS_ERR(vt8500_rtc->rtc)) {
  195. ret = PTR_ERR(vt8500_rtc->rtc);
  196. dev_err(&pdev->dev,
  197. "Failed to register RTC device -> %d\n", ret);
  198. goto err_return;
  199. }
  200. ret = devm_request_irq(&pdev->dev, vt8500_rtc->irq_alarm,
  201. vt8500_rtc_irq, 0, "rtc alarm", vt8500_rtc);
  202. if (ret < 0) {
  203. dev_err(&pdev->dev, "can't get irq %i, err %d\n",
  204. vt8500_rtc->irq_alarm, ret);
  205. goto err_return;
  206. }
  207. return 0;
  208. err_return:
  209. return ret;
  210. }
  211. static int vt8500_rtc_remove(struct platform_device *pdev)
  212. {
  213. struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev);
  214. /* Disable alarm matching */
  215. writel(0, vt8500_rtc->regbase + VT8500_RTC_IS);
  216. return 0;
  217. }
  218. static const struct of_device_id wmt_dt_ids[] = {
  219. { .compatible = "via,vt8500-rtc", },
  220. {}
  221. };
  222. MODULE_DEVICE_TABLE(of, wmt_dt_ids);
  223. static struct platform_driver vt8500_rtc_driver = {
  224. .probe = vt8500_rtc_probe,
  225. .remove = vt8500_rtc_remove,
  226. .driver = {
  227. .name = "vt8500-rtc",
  228. .of_match_table = wmt_dt_ids,
  229. },
  230. };
  231. module_platform_driver(vt8500_rtc_driver);
  232. MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
  233. MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
  234. MODULE_LICENSE("GPL v2");
  235. MODULE_ALIAS("platform:vt8500-rtc");