rtc-msm6242.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Oki MSM6242 RTC Driver
  3. *
  4. * Copyright 2009 Geert Uytterhoeven
  5. *
  6. * Based on the A2000 TOD code in arch/m68k/amiga/config.c
  7. * Copyright (C) 1993 Hamish Macdonald
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rtc.h>
  16. #include <linux/slab.h>
  17. enum {
  18. MSM6242_SECOND1 = 0x0, /* 1-second digit register */
  19. MSM6242_SECOND10 = 0x1, /* 10-second digit register */
  20. MSM6242_MINUTE1 = 0x2, /* 1-minute digit register */
  21. MSM6242_MINUTE10 = 0x3, /* 10-minute digit register */
  22. MSM6242_HOUR1 = 0x4, /* 1-hour digit register */
  23. MSM6242_HOUR10 = 0x5, /* PM/AM, 10-hour digit register */
  24. MSM6242_DAY1 = 0x6, /* 1-day digit register */
  25. MSM6242_DAY10 = 0x7, /* 10-day digit register */
  26. MSM6242_MONTH1 = 0x8, /* 1-month digit register */
  27. MSM6242_MONTH10 = 0x9, /* 10-month digit register */
  28. MSM6242_YEAR1 = 0xa, /* 1-year digit register */
  29. MSM6242_YEAR10 = 0xb, /* 10-year digit register */
  30. MSM6242_WEEK = 0xc, /* Week register */
  31. MSM6242_CD = 0xd, /* Control Register D */
  32. MSM6242_CE = 0xe, /* Control Register E */
  33. MSM6242_CF = 0xf, /* Control Register F */
  34. };
  35. #define MSM6242_HOUR10_AM (0 << 2)
  36. #define MSM6242_HOUR10_PM (1 << 2)
  37. #define MSM6242_HOUR10_HR_MASK (3 << 0)
  38. #define MSM6242_WEEK_SUNDAY 0
  39. #define MSM6242_WEEK_MONDAY 1
  40. #define MSM6242_WEEK_TUESDAY 2
  41. #define MSM6242_WEEK_WEDNESDAY 3
  42. #define MSM6242_WEEK_THURSDAY 4
  43. #define MSM6242_WEEK_FRIDAY 5
  44. #define MSM6242_WEEK_SATURDAY 6
  45. #define MSM6242_CD_30_S_ADJ (1 << 3) /* 30-second adjustment */
  46. #define MSM6242_CD_IRQ_FLAG (1 << 2)
  47. #define MSM6242_CD_BUSY (1 << 1)
  48. #define MSM6242_CD_HOLD (1 << 0)
  49. #define MSM6242_CE_T_MASK (3 << 2)
  50. #define MSM6242_CE_T_64HZ (0 << 2) /* period 1/64 second */
  51. #define MSM6242_CE_T_1HZ (1 << 2) /* period 1 second */
  52. #define MSM6242_CE_T_1MINUTE (2 << 2) /* period 1 minute */
  53. #define MSM6242_CE_T_1HOUR (3 << 2) /* period 1 hour */
  54. #define MSM6242_CE_ITRPT_STND (1 << 1)
  55. #define MSM6242_CE_MASK (1 << 0) /* STD.P output control */
  56. #define MSM6242_CF_TEST (1 << 3)
  57. #define MSM6242_CF_12H (0 << 2)
  58. #define MSM6242_CF_24H (1 << 2)
  59. #define MSM6242_CF_STOP (1 << 1)
  60. #define MSM6242_CF_REST (1 << 0) /* reset */
  61. struct msm6242_priv {
  62. u32 __iomem *regs;
  63. struct rtc_device *rtc;
  64. };
  65. static inline unsigned int msm6242_read(struct msm6242_priv *priv,
  66. unsigned int reg)
  67. {
  68. return __raw_readl(&priv->regs[reg]) & 0xf;
  69. }
  70. static inline void msm6242_write(struct msm6242_priv *priv, unsigned int val,
  71. unsigned int reg)
  72. {
  73. __raw_writel(val, &priv->regs[reg]);
  74. }
  75. static inline void msm6242_set(struct msm6242_priv *priv, unsigned int val,
  76. unsigned int reg)
  77. {
  78. msm6242_write(priv, msm6242_read(priv, reg) | val, reg);
  79. }
  80. static inline void msm6242_clear(struct msm6242_priv *priv, unsigned int val,
  81. unsigned int reg)
  82. {
  83. msm6242_write(priv, msm6242_read(priv, reg) & ~val, reg);
  84. }
  85. static void msm6242_lock(struct msm6242_priv *priv)
  86. {
  87. int cnt = 5;
  88. msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD);
  89. while ((msm6242_read(priv, MSM6242_CD) & MSM6242_CD_BUSY) && cnt) {
  90. msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD);
  91. udelay(70);
  92. msm6242_set(priv, MSM6242_CD_HOLD, MSM6242_CD);
  93. cnt--;
  94. }
  95. if (!cnt)
  96. pr_warn("timed out waiting for RTC (0x%x)\n",
  97. msm6242_read(priv, MSM6242_CD));
  98. }
  99. static void msm6242_unlock(struct msm6242_priv *priv)
  100. {
  101. msm6242_clear(priv, MSM6242_CD_HOLD, MSM6242_CD);
  102. }
  103. static int msm6242_read_time(struct device *dev, struct rtc_time *tm)
  104. {
  105. struct msm6242_priv *priv = dev_get_drvdata(dev);
  106. msm6242_lock(priv);
  107. tm->tm_sec = msm6242_read(priv, MSM6242_SECOND10) * 10 +
  108. msm6242_read(priv, MSM6242_SECOND1);
  109. tm->tm_min = msm6242_read(priv, MSM6242_MINUTE10) * 10 +
  110. msm6242_read(priv, MSM6242_MINUTE1);
  111. tm->tm_hour = (msm6242_read(priv, MSM6242_HOUR10) &
  112. MSM6242_HOUR10_HR_MASK) * 10 +
  113. msm6242_read(priv, MSM6242_HOUR1);
  114. tm->tm_mday = msm6242_read(priv, MSM6242_DAY10) * 10 +
  115. msm6242_read(priv, MSM6242_DAY1);
  116. tm->tm_wday = msm6242_read(priv, MSM6242_WEEK);
  117. tm->tm_mon = msm6242_read(priv, MSM6242_MONTH10) * 10 +
  118. msm6242_read(priv, MSM6242_MONTH1) - 1;
  119. tm->tm_year = msm6242_read(priv, MSM6242_YEAR10) * 10 +
  120. msm6242_read(priv, MSM6242_YEAR1);
  121. if (tm->tm_year <= 69)
  122. tm->tm_year += 100;
  123. if (!(msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)) {
  124. unsigned int pm = msm6242_read(priv, MSM6242_HOUR10) &
  125. MSM6242_HOUR10_PM;
  126. if (!pm && tm->tm_hour == 12)
  127. tm->tm_hour = 0;
  128. else if (pm && tm->tm_hour != 12)
  129. tm->tm_hour += 12;
  130. }
  131. msm6242_unlock(priv);
  132. return 0;
  133. }
  134. static int msm6242_set_time(struct device *dev, struct rtc_time *tm)
  135. {
  136. struct msm6242_priv *priv = dev_get_drvdata(dev);
  137. msm6242_lock(priv);
  138. msm6242_write(priv, tm->tm_sec / 10, MSM6242_SECOND10);
  139. msm6242_write(priv, tm->tm_sec % 10, MSM6242_SECOND1);
  140. msm6242_write(priv, tm->tm_min / 10, MSM6242_MINUTE10);
  141. msm6242_write(priv, tm->tm_min % 10, MSM6242_MINUTE1);
  142. if (msm6242_read(priv, MSM6242_CF) & MSM6242_CF_24H)
  143. msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
  144. else if (tm->tm_hour >= 12)
  145. msm6242_write(priv, MSM6242_HOUR10_PM + (tm->tm_hour - 12) / 10,
  146. MSM6242_HOUR10);
  147. else
  148. msm6242_write(priv, tm->tm_hour / 10, MSM6242_HOUR10);
  149. msm6242_write(priv, tm->tm_hour % 10, MSM6242_HOUR1);
  150. msm6242_write(priv, tm->tm_mday / 10, MSM6242_DAY10);
  151. msm6242_write(priv, tm->tm_mday % 10, MSM6242_DAY1);
  152. if (tm->tm_wday != -1)
  153. msm6242_write(priv, tm->tm_wday, MSM6242_WEEK);
  154. msm6242_write(priv, (tm->tm_mon + 1) / 10, MSM6242_MONTH10);
  155. msm6242_write(priv, (tm->tm_mon + 1) % 10, MSM6242_MONTH1);
  156. if (tm->tm_year >= 100)
  157. tm->tm_year -= 100;
  158. msm6242_write(priv, tm->tm_year / 10, MSM6242_YEAR10);
  159. msm6242_write(priv, tm->tm_year % 10, MSM6242_YEAR1);
  160. msm6242_unlock(priv);
  161. return 0;
  162. }
  163. static const struct rtc_class_ops msm6242_rtc_ops = {
  164. .read_time = msm6242_read_time,
  165. .set_time = msm6242_set_time,
  166. };
  167. static int __init msm6242_rtc_probe(struct platform_device *pdev)
  168. {
  169. struct resource *res;
  170. struct msm6242_priv *priv;
  171. struct rtc_device *rtc;
  172. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. if (!res)
  174. return -ENODEV;
  175. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  176. if (!priv)
  177. return -ENOMEM;
  178. priv->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  179. if (!priv->regs)
  180. return -ENOMEM;
  181. platform_set_drvdata(pdev, priv);
  182. rtc = devm_rtc_device_register(&pdev->dev, "rtc-msm6242",
  183. &msm6242_rtc_ops, THIS_MODULE);
  184. if (IS_ERR(rtc))
  185. return PTR_ERR(rtc);
  186. priv->rtc = rtc;
  187. return 0;
  188. }
  189. static struct platform_driver msm6242_rtc_driver = {
  190. .driver = {
  191. .name = "rtc-msm6242",
  192. },
  193. };
  194. module_platform_driver_probe(msm6242_rtc_driver, msm6242_rtc_probe);
  195. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("Oki MSM6242 RTC driver");
  198. MODULE_ALIAS("platform:rtc-msm6242");