rtc-amlogic-a4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: (GPL-2.0-only OR MIT)
  2. /*
  3. * Copyright (C) 2024 Amlogic, Inc. All rights reserved
  4. * Author: Yiting Deng <yiting.deng@amlogic.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/delay.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/rtc.h>
  14. #include <linux/time64.h>
  15. /* rtc oscillator rate */
  16. #define OSC_32K 32768
  17. #define OSC_24M 24000000
  18. #define RTC_CTRL (0x0 << 2) /* Control RTC */
  19. #define RTC_ALRM0_EN BIT(0)
  20. #define RTC_OSC_SEL BIT(8)
  21. #define RTC_ENABLE BIT(12)
  22. #define RTC_COUNTER_REG (0x1 << 2) /* Program RTC counter initial value */
  23. #define RTC_ALARM0_REG (0x2 << 2) /* Program RTC alarm0 value */
  24. #define RTC_SEC_ADJUST_REG (0x6 << 2) /* Control second-based timing adjustment */
  25. #define RTC_MATCH_COUNTER GENMASK(18, 0)
  26. #define RTC_SEC_ADJUST_CTRL GENMASK(20, 19)
  27. #define RTC_ADJ_VALID BIT(23)
  28. #define RTC_INT_MASK (0x8 << 2) /* RTC interrupt mask */
  29. #define RTC_ALRM0_IRQ_MSK BIT(0)
  30. #define RTC_INT_CLR (0x9 << 2) /* Clear RTC interrupt */
  31. #define RTC_ALRM0_IRQ_CLR BIT(0)
  32. #define RTC_OSCIN_CTRL0 (0xa << 2) /* Control RTC clk from 24M */
  33. #define RTC_OSCIN_CTRL1 (0xb << 2) /* Control RTC clk from 24M */
  34. #define RTC_OSCIN_IN_EN BIT(31)
  35. #define RTC_OSCIN_OUT_CFG GENMASK(29, 28)
  36. #define RTC_OSCIN_OUT_N0M0 GENMASK(11, 0)
  37. #define RTC_OSCIN_OUT_N1M1 GENMASK(23, 12)
  38. #define RTC_INT_STATUS (0xc << 2) /* RTC interrupt status */
  39. #define RTC_ALRM0_IRQ_STATUS BIT(0)
  40. #define RTC_REAL_TIME (0xd << 2) /* RTC time value */
  41. #define RTC_OSCIN_OUT_32K_N0 0x2dc
  42. #define RTC_OSCIN_OUT_32K_N1 0x2db
  43. #define RTC_OSCIN_OUT_32K_M0 0x1
  44. #define RTC_OSCIN_OUT_32K_M1 0x2
  45. #define RTC_SWALLOW_SECOND 0x2
  46. #define RTC_INSERT_SECOND 0x3
  47. struct aml_rtc_config {
  48. bool gray_stored;
  49. };
  50. struct aml_rtc_data {
  51. struct regmap *map;
  52. struct rtc_device *rtc_dev;
  53. int irq;
  54. struct clk *rtc_clk;
  55. struct clk *sys_clk;
  56. int rtc_enabled;
  57. const struct aml_rtc_config *config;
  58. };
  59. static inline u32 gray_to_binary(u32 gray)
  60. {
  61. u32 bcd = gray;
  62. int size = sizeof(bcd) * 8;
  63. int i;
  64. for (i = 0; (1 << i) < size; i++)
  65. bcd ^= bcd >> (1 << i);
  66. return bcd;
  67. }
  68. static inline u32 binary_to_gray(u32 bcd)
  69. {
  70. return bcd ^ (bcd >> 1);
  71. }
  72. static int aml_rtc_read_time(struct device *dev, struct rtc_time *tm)
  73. {
  74. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  75. u32 time_sec;
  76. /* if RTC disabled, read time failed */
  77. if (!rtc->rtc_enabled)
  78. return -EINVAL;
  79. regmap_read(rtc->map, RTC_REAL_TIME, &time_sec);
  80. if (rtc->config->gray_stored)
  81. time_sec = gray_to_binary(time_sec);
  82. rtc_time64_to_tm(time_sec, tm);
  83. dev_dbg(dev, "%s: read time = %us\n", __func__, time_sec);
  84. return 0;
  85. }
  86. static int aml_rtc_set_time(struct device *dev, struct rtc_time *tm)
  87. {
  88. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  89. u32 time_sec;
  90. /* if RTC disabled, first enable it */
  91. if (!rtc->rtc_enabled) {
  92. regmap_write_bits(rtc->map, RTC_CTRL, RTC_ENABLE, RTC_ENABLE);
  93. usleep_range(100, 200);
  94. rtc->rtc_enabled = regmap_test_bits(rtc->map, RTC_CTRL, RTC_ENABLE);
  95. if (!rtc->rtc_enabled)
  96. return -EINVAL;
  97. }
  98. time_sec = rtc_tm_to_time64(tm);
  99. if (rtc->config->gray_stored)
  100. time_sec = binary_to_gray(time_sec);
  101. regmap_write(rtc->map, RTC_COUNTER_REG, time_sec);
  102. dev_dbg(dev, "%s: set time = %us\n", __func__, time_sec);
  103. return 0;
  104. }
  105. static int aml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  106. {
  107. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  108. time64_t alarm_sec;
  109. /* if RTC disabled, set alarm failed */
  110. if (!rtc->rtc_enabled)
  111. return -EINVAL;
  112. regmap_update_bits(rtc->map, RTC_CTRL,
  113. RTC_ALRM0_EN, RTC_ALRM0_EN);
  114. regmap_update_bits(rtc->map, RTC_INT_MASK,
  115. RTC_ALRM0_IRQ_MSK, 0);
  116. alarm_sec = rtc_tm_to_time64(&alarm->time);
  117. if (rtc->config->gray_stored)
  118. alarm_sec = binary_to_gray(alarm_sec);
  119. regmap_write(rtc->map, RTC_ALARM0_REG, alarm_sec);
  120. dev_dbg(dev, "%s: alarm->enabled=%d alarm_set=%llds\n", __func__,
  121. alarm->enabled, alarm_sec);
  122. return 0;
  123. }
  124. static int aml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  125. {
  126. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  127. u32 alarm_sec;
  128. int alarm_enable;
  129. int alarm_mask;
  130. /* if RTC disabled, read alarm failed */
  131. if (!rtc->rtc_enabled)
  132. return -EINVAL;
  133. regmap_read(rtc->map, RTC_ALARM0_REG, &alarm_sec);
  134. if (rtc->config->gray_stored)
  135. alarm_sec = gray_to_binary(alarm_sec);
  136. rtc_time64_to_tm(alarm_sec, &alarm->time);
  137. alarm_enable = regmap_test_bits(rtc->map, RTC_CTRL, RTC_ALRM0_EN);
  138. alarm_mask = regmap_test_bits(rtc->map, RTC_INT_MASK, RTC_ALRM0_IRQ_MSK);
  139. alarm->enabled = (alarm_enable && !alarm_mask) ? 1 : 0;
  140. dev_dbg(dev, "%s: alarm->enabled=%d alarm=%us\n", __func__,
  141. alarm->enabled, alarm_sec);
  142. return 0;
  143. }
  144. static int aml_rtc_read_offset(struct device *dev, long *offset)
  145. {
  146. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  147. u32 reg_val;
  148. long val;
  149. int sign, match_counter, enable;
  150. /* if RTC disabled, read offset failed */
  151. if (!rtc->rtc_enabled)
  152. return -EINVAL;
  153. regmap_read(rtc->map, RTC_SEC_ADJUST_REG, &reg_val);
  154. enable = FIELD_GET(RTC_ADJ_VALID, reg_val);
  155. if (!enable) {
  156. val = 0;
  157. } else {
  158. sign = FIELD_GET(RTC_SEC_ADJUST_CTRL, reg_val);
  159. match_counter = FIELD_GET(RTC_MATCH_COUNTER, reg_val);
  160. val = 1000000000 / (match_counter + 1);
  161. if (sign == RTC_SWALLOW_SECOND)
  162. val = -val;
  163. }
  164. *offset = val;
  165. return 0;
  166. }
  167. static int aml_rtc_set_offset(struct device *dev, long offset)
  168. {
  169. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  170. int sign = 0;
  171. int match_counter = 0;
  172. int enable = 0;
  173. u32 reg_val;
  174. /* if RTC disabled, set offset failed */
  175. if (!rtc->rtc_enabled)
  176. return -EINVAL;
  177. if (offset) {
  178. enable = 1;
  179. sign = offset < 0 ? RTC_SWALLOW_SECOND : RTC_INSERT_SECOND;
  180. match_counter = 1000000000 / abs(offset) - 1;
  181. if (match_counter < 0 || match_counter > RTC_MATCH_COUNTER)
  182. return -EINVAL;
  183. }
  184. reg_val = FIELD_PREP(RTC_ADJ_VALID, enable) |
  185. FIELD_PREP(RTC_SEC_ADJUST_CTRL, sign) |
  186. FIELD_PREP(RTC_MATCH_COUNTER, match_counter);
  187. regmap_write(rtc->map, RTC_SEC_ADJUST_REG, reg_val);
  188. return 0;
  189. }
  190. static int aml_rtc_alarm_enable(struct device *dev, unsigned int enabled)
  191. {
  192. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  193. if (enabled) {
  194. regmap_update_bits(rtc->map, RTC_CTRL,
  195. RTC_ALRM0_EN, RTC_ALRM0_EN);
  196. regmap_update_bits(rtc->map, RTC_INT_MASK,
  197. RTC_ALRM0_IRQ_MSK, 0);
  198. } else {
  199. regmap_update_bits(rtc->map, RTC_INT_MASK,
  200. RTC_ALRM0_IRQ_MSK, RTC_ALRM0_IRQ_MSK);
  201. regmap_update_bits(rtc->map, RTC_CTRL,
  202. RTC_ALRM0_EN, 0);
  203. }
  204. return 0;
  205. }
  206. static const struct rtc_class_ops aml_rtc_ops = {
  207. .read_time = aml_rtc_read_time,
  208. .set_time = aml_rtc_set_time,
  209. .read_alarm = aml_rtc_read_alarm,
  210. .set_alarm = aml_rtc_set_alarm,
  211. .alarm_irq_enable = aml_rtc_alarm_enable,
  212. .read_offset = aml_rtc_read_offset,
  213. .set_offset = aml_rtc_set_offset,
  214. };
  215. static irqreturn_t aml_rtc_handler(int irq, void *data)
  216. {
  217. struct aml_rtc_data *rtc = (struct aml_rtc_data *)data;
  218. regmap_write(rtc->map, RTC_ALARM0_REG, 0);
  219. regmap_write(rtc->map, RTC_INT_CLR, RTC_ALRM0_IRQ_STATUS);
  220. rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
  221. return IRQ_HANDLED;
  222. }
  223. static void aml_rtc_init(struct aml_rtc_data *rtc)
  224. {
  225. u32 reg_val = 0;
  226. rtc->rtc_enabled = regmap_test_bits(rtc->map, RTC_CTRL, RTC_ENABLE);
  227. if (!rtc->rtc_enabled) {
  228. if (clk_get_rate(rtc->rtc_clk) == OSC_24M) {
  229. /* select 24M oscillator */
  230. regmap_write_bits(rtc->map, RTC_CTRL, RTC_OSC_SEL, RTC_OSC_SEL);
  231. /*
  232. * Set RTC oscillator to freq_out to freq_in/((N0*M0+N1*M1)/(M0+M1))
  233. * Enable clock_in gate of oscillator 24MHz
  234. * Set N0 to 733, N1 to 732
  235. */
  236. reg_val = FIELD_PREP(RTC_OSCIN_IN_EN, 1)
  237. | FIELD_PREP(RTC_OSCIN_OUT_CFG, 1)
  238. | FIELD_PREP(RTC_OSCIN_OUT_N0M0, RTC_OSCIN_OUT_32K_N0)
  239. | FIELD_PREP(RTC_OSCIN_OUT_N1M1, RTC_OSCIN_OUT_32K_N1);
  240. regmap_write_bits(rtc->map, RTC_OSCIN_CTRL0, RTC_OSCIN_IN_EN
  241. | RTC_OSCIN_OUT_CFG | RTC_OSCIN_OUT_N0M0
  242. | RTC_OSCIN_OUT_N1M1, reg_val);
  243. /* Set M0 to 2, M1 to 3, so freq_out = 32768 Hz*/
  244. reg_val = FIELD_PREP(RTC_OSCIN_OUT_N0M0, RTC_OSCIN_OUT_32K_M0)
  245. | FIELD_PREP(RTC_OSCIN_OUT_N1M1, RTC_OSCIN_OUT_32K_M1);
  246. regmap_write_bits(rtc->map, RTC_OSCIN_CTRL1, RTC_OSCIN_OUT_N0M0
  247. | RTC_OSCIN_OUT_N1M1, reg_val);
  248. } else {
  249. /* select 32K oscillator */
  250. regmap_write_bits(rtc->map, RTC_CTRL, RTC_OSC_SEL, 0);
  251. }
  252. }
  253. regmap_write_bits(rtc->map, RTC_INT_MASK,
  254. RTC_ALRM0_IRQ_MSK, RTC_ALRM0_IRQ_MSK);
  255. regmap_write_bits(rtc->map, RTC_CTRL, RTC_ALRM0_EN, 0);
  256. }
  257. static int aml_rtc_probe(struct platform_device *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct aml_rtc_data *rtc;
  261. void __iomem *base;
  262. int ret = 0;
  263. const struct regmap_config aml_rtc_regmap_config = {
  264. .reg_bits = 32,
  265. .val_bits = 32,
  266. .reg_stride = 4,
  267. .max_register = RTC_REAL_TIME,
  268. };
  269. rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
  270. if (!rtc)
  271. return -ENOMEM;
  272. rtc->config = of_device_get_match_data(dev);
  273. if (!rtc->config)
  274. return -ENODEV;
  275. base = devm_platform_ioremap_resource(pdev, 0);
  276. if (IS_ERR(base))
  277. return dev_err_probe(dev, PTR_ERR(base), "resource ioremap failed\n");
  278. rtc->map = devm_regmap_init_mmio(dev, base, &aml_rtc_regmap_config);
  279. if (IS_ERR(rtc->map))
  280. return dev_err_probe(dev, PTR_ERR(rtc->map), "regmap init failed\n");
  281. rtc->irq = platform_get_irq(pdev, 0);
  282. if (rtc->irq < 0)
  283. return rtc->irq;
  284. rtc->rtc_clk = devm_clk_get(dev, "osc");
  285. if (IS_ERR(rtc->rtc_clk))
  286. return dev_err_probe(dev, PTR_ERR(rtc->rtc_clk),
  287. "failed to find rtc clock\n");
  288. if (clk_get_rate(rtc->rtc_clk) != OSC_32K && clk_get_rate(rtc->rtc_clk) != OSC_24M)
  289. return dev_err_probe(dev, -EINVAL, "Invalid clock configuration\n");
  290. rtc->sys_clk = devm_clk_get_enabled(dev, "sys");
  291. if (IS_ERR(rtc->sys_clk))
  292. return dev_err_probe(dev, PTR_ERR(rtc->sys_clk),
  293. "failed to get_enable rtc sys clk\n");
  294. aml_rtc_init(rtc);
  295. devm_device_init_wakeup(dev);
  296. platform_set_drvdata(pdev, rtc);
  297. rtc->rtc_dev = devm_rtc_allocate_device(dev);
  298. if (IS_ERR(rtc->rtc_dev))
  299. return PTR_ERR(rtc->rtc_dev);
  300. ret = devm_request_irq(dev, rtc->irq, aml_rtc_handler,
  301. 0, "aml-rtc alarm", rtc);
  302. if (ret) {
  303. dev_err_probe(dev, ret, "IRQ%d request failed, ret = %d\n",
  304. rtc->irq, ret);
  305. return ret;
  306. }
  307. rtc->rtc_dev->ops = &aml_rtc_ops;
  308. rtc->rtc_dev->range_min = 0;
  309. rtc->rtc_dev->range_max = U32_MAX;
  310. return devm_rtc_register_device(rtc->rtc_dev);
  311. }
  312. #ifdef CONFIG_PM_SLEEP
  313. static int aml_rtc_suspend(struct device *dev)
  314. {
  315. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  316. if (device_may_wakeup(dev))
  317. enable_irq_wake(rtc->irq);
  318. return 0;
  319. }
  320. static int aml_rtc_resume(struct device *dev)
  321. {
  322. struct aml_rtc_data *rtc = dev_get_drvdata(dev);
  323. if (device_may_wakeup(dev))
  324. disable_irq_wake(rtc->irq);
  325. return 0;
  326. }
  327. #endif
  328. static SIMPLE_DEV_PM_OPS(aml_rtc_pm_ops,
  329. aml_rtc_suspend, aml_rtc_resume);
  330. static const struct aml_rtc_config a5_rtc_config = {
  331. };
  332. static const struct aml_rtc_config a4_rtc_config = {
  333. .gray_stored = true,
  334. };
  335. static const struct of_device_id aml_rtc_device_id[] = {
  336. {
  337. .compatible = "amlogic,a4-rtc",
  338. .data = &a4_rtc_config,
  339. },
  340. {
  341. .compatible = "amlogic,a5-rtc",
  342. .data = &a5_rtc_config,
  343. },
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(of, aml_rtc_device_id);
  347. static struct platform_driver aml_rtc_driver = {
  348. .probe = aml_rtc_probe,
  349. .driver = {
  350. .name = "aml-rtc",
  351. .pm = &aml_rtc_pm_ops,
  352. .of_match_table = aml_rtc_device_id,
  353. },
  354. };
  355. module_platform_driver(aml_rtc_driver);
  356. MODULE_DESCRIPTION("Amlogic RTC driver");
  357. MODULE_AUTHOR("Yiting Deng <yiting.deng@amlogic.com>");
  358. MODULE_LICENSE("GPL");