rtc-sun6i.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * An RTC driver for Allwinner A31/A23
  3. *
  4. * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * based on rtc-sunxi.c
  7. *
  8. * An RTC driver for Allwinner A10/A20
  9. *
  10. * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/clk-provider.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/fs.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_device.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/rtc.h>
  37. #include <linux/slab.h>
  38. #include <linux/types.h>
  39. /* Control register */
  40. #define SUN6I_LOSC_CTRL 0x0000
  41. #define SUN6I_LOSC_CTRL_KEY (0x16aa << 16)
  42. #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9)
  43. #define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8)
  44. #define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7)
  45. #define SUN6I_LOSC_CTRL_EXT_OSC BIT(0)
  46. #define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7)
  47. #define SUN6I_LOSC_CLK_PRESCAL 0x0008
  48. /* RTC */
  49. #define SUN6I_RTC_YMD 0x0010
  50. #define SUN6I_RTC_HMS 0x0014
  51. /* Alarm 0 (counter) */
  52. #define SUN6I_ALRM_COUNTER 0x0020
  53. #define SUN6I_ALRM_CUR_VAL 0x0024
  54. #define SUN6I_ALRM_EN 0x0028
  55. #define SUN6I_ALRM_EN_CNT_EN BIT(0)
  56. #define SUN6I_ALRM_IRQ_EN 0x002c
  57. #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
  58. #define SUN6I_ALRM_IRQ_STA 0x0030
  59. #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
  60. /* Alarm 1 (wall clock) */
  61. #define SUN6I_ALRM1_EN 0x0044
  62. #define SUN6I_ALRM1_IRQ_EN 0x0048
  63. #define SUN6I_ALRM1_IRQ_STA 0x004c
  64. #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0)
  65. /* Alarm config */
  66. #define SUN6I_ALARM_CONFIG 0x0050
  67. #define SUN6I_ALARM_CONFIG_WAKEUP BIT(0)
  68. #define SUN6I_LOSC_OUT_GATING 0x0060
  69. #define SUN6I_LOSC_OUT_GATING_EN_OFFSET 0
  70. /*
  71. * Get date values
  72. */
  73. #define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f)
  74. #define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8)
  75. #define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16)
  76. #define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22)
  77. /*
  78. * Get time values
  79. */
  80. #define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f)
  81. #define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8)
  82. #define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16)
  83. /*
  84. * Set date values
  85. */
  86. #define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f)
  87. #define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00)
  88. #define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000)
  89. #define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000)
  90. /*
  91. * Set time values
  92. */
  93. #define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f)
  94. #define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00)
  95. #define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000)
  96. /*
  97. * The year parameter passed to the driver is usually an offset relative to
  98. * the year 1900. This macro is used to convert this offset to another one
  99. * relative to the minimum year allowed by the hardware.
  100. *
  101. * The year range is 1970 - 2033. This range is selected to match Allwinner's
  102. * driver, even though it is somewhat limited.
  103. */
  104. #define SUN6I_YEAR_MIN 1970
  105. #define SUN6I_YEAR_MAX 2033
  106. #define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900)
  107. struct sun6i_rtc_dev {
  108. struct rtc_device *rtc;
  109. struct device *dev;
  110. void __iomem *base;
  111. int irq;
  112. unsigned long alarm;
  113. struct clk_hw hw;
  114. struct clk_hw *int_osc;
  115. struct clk *losc;
  116. struct clk *ext_losc;
  117. spinlock_t lock;
  118. };
  119. static struct sun6i_rtc_dev *sun6i_rtc;
  120. static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
  121. unsigned long parent_rate)
  122. {
  123. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  124. u32 val;
  125. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  126. if (val & SUN6I_LOSC_CTRL_EXT_OSC)
  127. return parent_rate;
  128. val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
  129. val &= GENMASK(4, 0);
  130. return parent_rate / (val + 1);
  131. }
  132. static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
  133. {
  134. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  135. return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
  136. }
  137. static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
  138. {
  139. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  140. unsigned long flags;
  141. u32 val;
  142. if (index > 1)
  143. return -EINVAL;
  144. spin_lock_irqsave(&rtc->lock, flags);
  145. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  146. val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
  147. val |= SUN6I_LOSC_CTRL_KEY;
  148. val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
  149. writel(val, rtc->base + SUN6I_LOSC_CTRL);
  150. spin_unlock_irqrestore(&rtc->lock, flags);
  151. return 0;
  152. }
  153. static const struct clk_ops sun6i_rtc_osc_ops = {
  154. .recalc_rate = sun6i_rtc_osc_recalc_rate,
  155. .get_parent = sun6i_rtc_osc_get_parent,
  156. .set_parent = sun6i_rtc_osc_set_parent,
  157. };
  158. static void __init sun6i_rtc_clk_init(struct device_node *node)
  159. {
  160. struct clk_hw_onecell_data *clk_data;
  161. struct sun6i_rtc_dev *rtc;
  162. struct clk_init_data init = {
  163. .ops = &sun6i_rtc_osc_ops,
  164. };
  165. const char *clkout_name = "osc32k-out";
  166. const char *parents[2];
  167. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  168. if (!rtc)
  169. return;
  170. clk_data = kzalloc(sizeof(*clk_data) + (sizeof(*clk_data->hws) * 2),
  171. GFP_KERNEL);
  172. if (!clk_data) {
  173. kfree(rtc);
  174. return;
  175. }
  176. spin_lock_init(&rtc->lock);
  177. rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node));
  178. if (IS_ERR(rtc->base)) {
  179. pr_crit("Can't map RTC registers");
  180. goto err;
  181. }
  182. /* Switch to the external, more precise, oscillator */
  183. writel(SUN6I_LOSC_CTRL_KEY | SUN6I_LOSC_CTRL_EXT_OSC,
  184. rtc->base + SUN6I_LOSC_CTRL);
  185. /* Yes, I know, this is ugly. */
  186. sun6i_rtc = rtc;
  187. /* Deal with old DTs */
  188. if (!of_get_property(node, "clocks", NULL))
  189. goto err;
  190. rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL,
  191. "rtc-int-osc",
  192. NULL, 0,
  193. 667000,
  194. 300000000);
  195. if (IS_ERR(rtc->int_osc)) {
  196. pr_crit("Couldn't register the internal oscillator\n");
  197. return;
  198. }
  199. parents[0] = clk_hw_get_name(rtc->int_osc);
  200. parents[1] = of_clk_get_parent_name(node, 0);
  201. rtc->hw.init = &init;
  202. init.parent_names = parents;
  203. init.num_parents = of_clk_get_parent_count(node) + 1;
  204. of_property_read_string_index(node, "clock-output-names", 0,
  205. &init.name);
  206. rtc->losc = clk_register(NULL, &rtc->hw);
  207. if (IS_ERR(rtc->losc)) {
  208. pr_crit("Couldn't register the LOSC clock\n");
  209. return;
  210. }
  211. of_property_read_string_index(node, "clock-output-names", 1,
  212. &clkout_name);
  213. rtc->ext_losc = clk_register_gate(NULL, clkout_name, rtc->hw.init->name,
  214. 0, rtc->base + SUN6I_LOSC_OUT_GATING,
  215. SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0,
  216. &rtc->lock);
  217. if (IS_ERR(rtc->ext_losc)) {
  218. pr_crit("Couldn't register the LOSC external gate\n");
  219. return;
  220. }
  221. clk_data->num = 2;
  222. clk_data->hws[0] = &rtc->hw;
  223. clk_data->hws[1] = __clk_get_hw(rtc->ext_losc);
  224. of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  225. return;
  226. err:
  227. kfree(clk_data);
  228. }
  229. CLK_OF_DECLARE_DRIVER(sun6i_rtc_clk, "allwinner,sun6i-a31-rtc",
  230. sun6i_rtc_clk_init);
  231. static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
  232. {
  233. struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
  234. irqreturn_t ret = IRQ_NONE;
  235. u32 val;
  236. spin_lock(&chip->lock);
  237. val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  238. if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
  239. val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
  240. writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
  241. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  242. ret = IRQ_HANDLED;
  243. }
  244. spin_unlock(&chip->lock);
  245. return ret;
  246. }
  247. static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
  248. {
  249. u32 alrm_val = 0;
  250. u32 alrm_irq_val = 0;
  251. u32 alrm_wake_val = 0;
  252. unsigned long flags;
  253. if (to) {
  254. alrm_val = SUN6I_ALRM_EN_CNT_EN;
  255. alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
  256. alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
  257. } else {
  258. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  259. chip->base + SUN6I_ALRM_IRQ_STA);
  260. }
  261. spin_lock_irqsave(&chip->lock, flags);
  262. writel(alrm_val, chip->base + SUN6I_ALRM_EN);
  263. writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
  264. writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
  265. spin_unlock_irqrestore(&chip->lock, flags);
  266. }
  267. static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  268. {
  269. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  270. u32 date, time;
  271. /*
  272. * read again in case it changes
  273. */
  274. do {
  275. date = readl(chip->base + SUN6I_RTC_YMD);
  276. time = readl(chip->base + SUN6I_RTC_HMS);
  277. } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
  278. (time != readl(chip->base + SUN6I_RTC_HMS)));
  279. rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
  280. rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
  281. rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
  282. rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
  283. rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date);
  284. rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
  285. rtc_tm->tm_mon -= 1;
  286. /*
  287. * switch from (data_year->min)-relative offset to
  288. * a (1900)-relative one
  289. */
  290. rtc_tm->tm_year += SUN6I_YEAR_OFF;
  291. return 0;
  292. }
  293. static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  294. {
  295. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  296. unsigned long flags;
  297. u32 alrm_st;
  298. u32 alrm_en;
  299. spin_lock_irqsave(&chip->lock, flags);
  300. alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
  301. alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  302. spin_unlock_irqrestore(&chip->lock, flags);
  303. wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
  304. wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
  305. rtc_time_to_tm(chip->alarm, &wkalrm->time);
  306. return 0;
  307. }
  308. static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  309. {
  310. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  311. struct rtc_time *alrm_tm = &wkalrm->time;
  312. struct rtc_time tm_now;
  313. unsigned long time_now = 0;
  314. unsigned long time_set = 0;
  315. unsigned long time_gap = 0;
  316. int ret = 0;
  317. ret = sun6i_rtc_gettime(dev, &tm_now);
  318. if (ret < 0) {
  319. dev_err(dev, "Error in getting time\n");
  320. return -EINVAL;
  321. }
  322. rtc_tm_to_time(alrm_tm, &time_set);
  323. rtc_tm_to_time(&tm_now, &time_now);
  324. if (time_set <= time_now) {
  325. dev_err(dev, "Date to set in the past\n");
  326. return -EINVAL;
  327. }
  328. time_gap = time_set - time_now;
  329. if (time_gap > U32_MAX) {
  330. dev_err(dev, "Date too far in the future\n");
  331. return -EINVAL;
  332. }
  333. sun6i_rtc_setaie(0, chip);
  334. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  335. usleep_range(100, 300);
  336. writel(time_gap, chip->base + SUN6I_ALRM_COUNTER);
  337. chip->alarm = time_set;
  338. sun6i_rtc_setaie(wkalrm->enabled, chip);
  339. return 0;
  340. }
  341. static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
  342. unsigned int mask, unsigned int ms_timeout)
  343. {
  344. const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
  345. u32 reg;
  346. do {
  347. reg = readl(chip->base + offset);
  348. reg &= mask;
  349. if (!reg)
  350. return 0;
  351. } while (time_before(jiffies, timeout));
  352. return -ETIMEDOUT;
  353. }
  354. static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
  355. {
  356. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  357. u32 date = 0;
  358. u32 time = 0;
  359. int year;
  360. year = rtc_tm->tm_year + 1900;
  361. if (year < SUN6I_YEAR_MIN || year > SUN6I_YEAR_MAX) {
  362. dev_err(dev, "rtc only supports year in range %d - %d\n",
  363. SUN6I_YEAR_MIN, SUN6I_YEAR_MAX);
  364. return -EINVAL;
  365. }
  366. rtc_tm->tm_year -= SUN6I_YEAR_OFF;
  367. rtc_tm->tm_mon += 1;
  368. date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
  369. SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
  370. SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
  371. if (is_leap_year(year))
  372. date |= SUN6I_LEAP_SET_VALUE(1);
  373. time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
  374. SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
  375. SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
  376. /* Check whether registers are writable */
  377. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  378. SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
  379. dev_err(dev, "rtc is still busy.\n");
  380. return -EBUSY;
  381. }
  382. writel(time, chip->base + SUN6I_RTC_HMS);
  383. /*
  384. * After writing the RTC HH-MM-SS register, the
  385. * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
  386. * be cleared until the real writing operation is finished
  387. */
  388. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  389. SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
  390. dev_err(dev, "Failed to set rtc time.\n");
  391. return -ETIMEDOUT;
  392. }
  393. writel(date, chip->base + SUN6I_RTC_YMD);
  394. /*
  395. * After writing the RTC YY-MM-DD register, the
  396. * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
  397. * be cleared until the real writing operation is finished
  398. */
  399. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  400. SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
  401. dev_err(dev, "Failed to set rtc time.\n");
  402. return -ETIMEDOUT;
  403. }
  404. return 0;
  405. }
  406. static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  407. {
  408. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  409. if (!enabled)
  410. sun6i_rtc_setaie(enabled, chip);
  411. return 0;
  412. }
  413. static const struct rtc_class_ops sun6i_rtc_ops = {
  414. .read_time = sun6i_rtc_gettime,
  415. .set_time = sun6i_rtc_settime,
  416. .read_alarm = sun6i_rtc_getalarm,
  417. .set_alarm = sun6i_rtc_setalarm,
  418. .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
  419. };
  420. static int sun6i_rtc_probe(struct platform_device *pdev)
  421. {
  422. struct sun6i_rtc_dev *chip = sun6i_rtc;
  423. int ret;
  424. if (!chip)
  425. return -ENODEV;
  426. platform_set_drvdata(pdev, chip);
  427. chip->dev = &pdev->dev;
  428. chip->irq = platform_get_irq(pdev, 0);
  429. if (chip->irq < 0) {
  430. dev_err(&pdev->dev, "No IRQ resource\n");
  431. return chip->irq;
  432. }
  433. ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
  434. 0, dev_name(&pdev->dev), chip);
  435. if (ret) {
  436. dev_err(&pdev->dev, "Could not request IRQ\n");
  437. return ret;
  438. }
  439. /* clear the alarm counter value */
  440. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  441. /* disable counter alarm */
  442. writel(0, chip->base + SUN6I_ALRM_EN);
  443. /* disable counter alarm interrupt */
  444. writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
  445. /* disable week alarm */
  446. writel(0, chip->base + SUN6I_ALRM1_EN);
  447. /* disable week alarm interrupt */
  448. writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
  449. /* clear counter alarm pending interrupts */
  450. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  451. chip->base + SUN6I_ALRM_IRQ_STA);
  452. /* clear week alarm pending interrupts */
  453. writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
  454. chip->base + SUN6I_ALRM1_IRQ_STA);
  455. /* disable alarm wakeup */
  456. writel(0, chip->base + SUN6I_ALARM_CONFIG);
  457. clk_prepare_enable(chip->losc);
  458. chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-sun6i",
  459. &sun6i_rtc_ops, THIS_MODULE);
  460. if (IS_ERR(chip->rtc)) {
  461. dev_err(&pdev->dev, "unable to register device\n");
  462. return PTR_ERR(chip->rtc);
  463. }
  464. dev_info(&pdev->dev, "RTC enabled\n");
  465. return 0;
  466. }
  467. static const struct of_device_id sun6i_rtc_dt_ids[] = {
  468. { .compatible = "allwinner,sun6i-a31-rtc" },
  469. { /* sentinel */ },
  470. };
  471. MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
  472. static struct platform_driver sun6i_rtc_driver = {
  473. .probe = sun6i_rtc_probe,
  474. .driver = {
  475. .name = "sun6i-rtc",
  476. .of_match_table = sun6i_rtc_dt_ids,
  477. },
  478. };
  479. builtin_platform_driver(sun6i_rtc_driver);