rtc-armada38x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * RTC driver for the Armada 38x Marvell SoCs
  3. *
  4. * Copyright (C) 2015 Marvell
  5. *
  6. * Gregory Clement <gregory.clement@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #define RTC_STATUS 0x0
  22. #define RTC_STATUS_ALARM1 BIT(0)
  23. #define RTC_STATUS_ALARM2 BIT(1)
  24. #define RTC_IRQ1_CONF 0x4
  25. #define RTC_IRQ2_CONF 0x8
  26. #define RTC_IRQ_AL_EN BIT(0)
  27. #define RTC_IRQ_FREQ_EN BIT(1)
  28. #define RTC_IRQ_FREQ_1HZ BIT(2)
  29. #define RTC_CCR 0x18
  30. #define RTC_CCR_MODE BIT(15)
  31. #define RTC_CONF_TEST 0x1C
  32. #define RTC_NOMINAL_TIMING BIT(13)
  33. #define RTC_TIME 0xC
  34. #define RTC_ALARM1 0x10
  35. #define RTC_ALARM2 0x14
  36. /* Armada38x SoC registers */
  37. #define RTC_38X_BRIDGE_TIMING_CTL 0x0
  38. #define RTC_38X_PERIOD_OFFS 0
  39. #define RTC_38X_PERIOD_MASK (0x3FF << RTC_38X_PERIOD_OFFS)
  40. #define RTC_38X_READ_DELAY_OFFS 26
  41. #define RTC_38X_READ_DELAY_MASK (0x1F << RTC_38X_READ_DELAY_OFFS)
  42. /* Armada 7K/8K registers */
  43. #define RTC_8K_BRIDGE_TIMING_CTL0 0x0
  44. #define RTC_8K_WRCLK_PERIOD_OFFS 0
  45. #define RTC_8K_WRCLK_PERIOD_MASK (0xFFFF << RTC_8K_WRCLK_PERIOD_OFFS)
  46. #define RTC_8K_WRCLK_SETUP_OFFS 16
  47. #define RTC_8K_WRCLK_SETUP_MASK (0xFFFF << RTC_8K_WRCLK_SETUP_OFFS)
  48. #define RTC_8K_BRIDGE_TIMING_CTL1 0x4
  49. #define RTC_8K_READ_DELAY_OFFS 0
  50. #define RTC_8K_READ_DELAY_MASK (0xFFFF << RTC_8K_READ_DELAY_OFFS)
  51. #define RTC_8K_ISR 0x10
  52. #define RTC_8K_IMR 0x14
  53. #define RTC_8K_ALARM2 BIT(0)
  54. #define SOC_RTC_INTERRUPT 0x8
  55. #define SOC_RTC_ALARM1 BIT(0)
  56. #define SOC_RTC_ALARM2 BIT(1)
  57. #define SOC_RTC_ALARM1_MASK BIT(2)
  58. #define SOC_RTC_ALARM2_MASK BIT(3)
  59. #define SAMPLE_NR 100
  60. struct value_to_freq {
  61. u32 value;
  62. u8 freq;
  63. };
  64. struct armada38x_rtc {
  65. struct rtc_device *rtc_dev;
  66. void __iomem *regs;
  67. void __iomem *regs_soc;
  68. spinlock_t lock;
  69. int irq;
  70. bool initialized;
  71. struct value_to_freq *val_to_freq;
  72. struct armada38x_rtc_data *data;
  73. };
  74. #define ALARM1 0
  75. #define ALARM2 1
  76. #define ALARM_REG(base, alarm) ((base) + (alarm) * sizeof(u32))
  77. struct armada38x_rtc_data {
  78. /* Initialize the RTC-MBUS bridge timing */
  79. void (*update_mbus_timing)(struct armada38x_rtc *rtc);
  80. u32 (*read_rtc_reg)(struct armada38x_rtc *rtc, u8 rtc_reg);
  81. void (*clear_isr)(struct armada38x_rtc *rtc);
  82. void (*unmask_interrupt)(struct armada38x_rtc *rtc);
  83. u32 alarm;
  84. };
  85. /*
  86. * According to the datasheet, the OS should wait 5us after every
  87. * register write to the RTC hard macro so that the required update
  88. * can occur without holding off the system bus
  89. * According to errata RES-3124064, Write to any RTC register
  90. * may fail. As a workaround, before writing to RTC
  91. * register, issue a dummy write of 0x0 twice to RTC Status
  92. * register.
  93. */
  94. static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
  95. {
  96. writel(0, rtc->regs + RTC_STATUS);
  97. writel(0, rtc->regs + RTC_STATUS);
  98. writel(val, rtc->regs + offset);
  99. udelay(5);
  100. }
  101. /* Update RTC-MBUS bridge timing parameters */
  102. static void rtc_update_38x_mbus_timing_params(struct armada38x_rtc *rtc)
  103. {
  104. u32 reg;
  105. reg = readl(rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
  106. reg &= ~RTC_38X_PERIOD_MASK;
  107. reg |= 0x3FF << RTC_38X_PERIOD_OFFS; /* Maximum value */
  108. reg &= ~RTC_38X_READ_DELAY_MASK;
  109. reg |= 0x1F << RTC_38X_READ_DELAY_OFFS; /* Maximum value */
  110. writel(reg, rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
  111. }
  112. static void rtc_update_8k_mbus_timing_params(struct armada38x_rtc *rtc)
  113. {
  114. u32 reg;
  115. reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
  116. reg &= ~RTC_8K_WRCLK_PERIOD_MASK;
  117. reg |= 0x3FF << RTC_8K_WRCLK_PERIOD_OFFS;
  118. reg &= ~RTC_8K_WRCLK_SETUP_MASK;
  119. reg |= 0x29 << RTC_8K_WRCLK_SETUP_OFFS;
  120. writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
  121. reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
  122. reg &= ~RTC_8K_READ_DELAY_MASK;
  123. reg |= 0x3F << RTC_8K_READ_DELAY_OFFS;
  124. writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
  125. }
  126. static u32 read_rtc_register(struct armada38x_rtc *rtc, u8 rtc_reg)
  127. {
  128. return readl(rtc->regs + rtc_reg);
  129. }
  130. static u32 read_rtc_register_38x_wa(struct armada38x_rtc *rtc, u8 rtc_reg)
  131. {
  132. int i, index_max = 0, max = 0;
  133. for (i = 0; i < SAMPLE_NR; i++) {
  134. rtc->val_to_freq[i].value = readl(rtc->regs + rtc_reg);
  135. rtc->val_to_freq[i].freq = 0;
  136. }
  137. for (i = 0; i < SAMPLE_NR; i++) {
  138. int j = 0;
  139. u32 value = rtc->val_to_freq[i].value;
  140. while (rtc->val_to_freq[j].freq) {
  141. if (rtc->val_to_freq[j].value == value) {
  142. rtc->val_to_freq[j].freq++;
  143. break;
  144. }
  145. j++;
  146. }
  147. if (!rtc->val_to_freq[j].freq) {
  148. rtc->val_to_freq[j].value = value;
  149. rtc->val_to_freq[j].freq = 1;
  150. }
  151. if (rtc->val_to_freq[j].freq > max) {
  152. index_max = j;
  153. max = rtc->val_to_freq[j].freq;
  154. }
  155. /*
  156. * If a value already has half of the sample this is the most
  157. * frequent one and we can stop the research right now
  158. */
  159. if (max > SAMPLE_NR / 2)
  160. break;
  161. }
  162. return rtc->val_to_freq[index_max].value;
  163. }
  164. static void armada38x_clear_isr(struct armada38x_rtc *rtc)
  165. {
  166. u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  167. writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
  168. }
  169. static void armada38x_unmask_interrupt(struct armada38x_rtc *rtc)
  170. {
  171. u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  172. writel(val | SOC_RTC_ALARM1_MASK, rtc->regs_soc + SOC_RTC_INTERRUPT);
  173. }
  174. static void armada8k_clear_isr(struct armada38x_rtc *rtc)
  175. {
  176. writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_ISR);
  177. }
  178. static void armada8k_unmask_interrupt(struct armada38x_rtc *rtc)
  179. {
  180. writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_IMR);
  181. }
  182. static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  183. {
  184. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  185. unsigned long time, flags;
  186. spin_lock_irqsave(&rtc->lock, flags);
  187. time = rtc->data->read_rtc_reg(rtc, RTC_TIME);
  188. spin_unlock_irqrestore(&rtc->lock, flags);
  189. rtc_time64_to_tm(time, tm);
  190. return 0;
  191. }
  192. static void armada38x_rtc_reset(struct armada38x_rtc *rtc)
  193. {
  194. u32 reg;
  195. reg = rtc->data->read_rtc_reg(rtc, RTC_CONF_TEST);
  196. /* If bits [7:0] are non-zero, assume RTC was uninitialized */
  197. if (reg & 0xff) {
  198. rtc_delayed_write(0, rtc, RTC_CONF_TEST);
  199. msleep(500); /* Oscillator startup time */
  200. rtc_delayed_write(0, rtc, RTC_TIME);
  201. rtc_delayed_write(SOC_RTC_ALARM1 | SOC_RTC_ALARM2, rtc,
  202. RTC_STATUS);
  203. rtc_delayed_write(RTC_NOMINAL_TIMING, rtc, RTC_CCR);
  204. }
  205. rtc->initialized = true;
  206. }
  207. static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  208. {
  209. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  210. unsigned long time, flags;
  211. time = rtc_tm_to_time64(tm);
  212. if (!rtc->initialized)
  213. armada38x_rtc_reset(rtc);
  214. spin_lock_irqsave(&rtc->lock, flags);
  215. rtc_delayed_write(time, rtc, RTC_TIME);
  216. spin_unlock_irqrestore(&rtc->lock, flags);
  217. return 0;
  218. }
  219. static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  220. {
  221. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  222. unsigned long time, flags;
  223. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  224. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  225. u32 val;
  226. spin_lock_irqsave(&rtc->lock, flags);
  227. time = rtc->data->read_rtc_reg(rtc, reg);
  228. val = rtc->data->read_rtc_reg(rtc, reg_irq) & RTC_IRQ_AL_EN;
  229. spin_unlock_irqrestore(&rtc->lock, flags);
  230. alrm->enabled = val ? 1 : 0;
  231. rtc_time64_to_tm(time, &alrm->time);
  232. return 0;
  233. }
  234. static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  235. {
  236. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  237. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  238. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  239. unsigned long time, flags;
  240. time = rtc_tm_to_time64(&alrm->time);
  241. spin_lock_irqsave(&rtc->lock, flags);
  242. rtc_delayed_write(time, rtc, reg);
  243. if (alrm->enabled) {
  244. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  245. rtc->data->unmask_interrupt(rtc);
  246. }
  247. spin_unlock_irqrestore(&rtc->lock, flags);
  248. return 0;
  249. }
  250. static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  251. unsigned int enabled)
  252. {
  253. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  254. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  255. unsigned long flags;
  256. spin_lock_irqsave(&rtc->lock, flags);
  257. if (enabled)
  258. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  259. else
  260. rtc_delayed_write(0, rtc, reg_irq);
  261. spin_unlock_irqrestore(&rtc->lock, flags);
  262. return 0;
  263. }
  264. static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  265. {
  266. struct armada38x_rtc *rtc = data;
  267. u32 val;
  268. int event = RTC_IRQF | RTC_AF;
  269. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  270. dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  271. spin_lock(&rtc->lock);
  272. rtc->data->clear_isr(rtc);
  273. val = rtc->data->read_rtc_reg(rtc, reg_irq);
  274. /* disable all the interrupts for alarm*/
  275. rtc_delayed_write(0, rtc, reg_irq);
  276. /* Ack the event */
  277. rtc_delayed_write(1 << rtc->data->alarm, rtc, RTC_STATUS);
  278. spin_unlock(&rtc->lock);
  279. if (val & RTC_IRQ_FREQ_EN) {
  280. if (val & RTC_IRQ_FREQ_1HZ)
  281. event |= RTC_UF;
  282. else
  283. event |= RTC_PF;
  284. }
  285. rtc_update_irq(rtc->rtc_dev, 1, event);
  286. return IRQ_HANDLED;
  287. }
  288. /*
  289. * The information given in the Armada 388 functional spec is complex.
  290. * They give two different formulas for calculating the offset value,
  291. * but when considering "Offset" as an 8-bit signed integer, they both
  292. * reduce down to (we shall rename "Offset" as "val" here):
  293. *
  294. * val = (f_ideal / f_measured - 1) / resolution where f_ideal = 32768
  295. *
  296. * Converting to time, f = 1/t:
  297. * val = (t_measured / t_ideal - 1) / resolution where t_ideal = 1/32768
  298. *
  299. * => t_measured / t_ideal = val * resolution + 1
  300. *
  301. * "offset" in the RTC interface is defined as:
  302. * t = t0 * (1 + offset * 1e-9)
  303. * where t is the desired period, t0 is the measured period with a zero
  304. * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
  305. * offset = (t_ideal / t_measured - 1) / 1e-9
  306. *
  307. * => t_ideal / t_measured = offset * 1e-9 + 1
  308. *
  309. * so:
  310. *
  311. * offset * 1e-9 + 1 = 1 / (val * resolution + 1)
  312. *
  313. * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
  314. * offset = 1e18 / (val * R + 1e9) - 1e9
  315. * val = (1e18 / (offset + 1e9) - 1e9) / R
  316. * with a common transformation:
  317. * f(x) = 1e18 / (x + 1e9) - 1e9
  318. * offset = f(val * R)
  319. * val = f(offset) / R
  320. *
  321. * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
  322. */
  323. static long armada38x_ppb_convert(long ppb)
  324. {
  325. long div = ppb + 1000000000L;
  326. return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
  327. }
  328. static int armada38x_rtc_read_offset(struct device *dev, long *offset)
  329. {
  330. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  331. unsigned long ccr, flags;
  332. long ppb_cor;
  333. spin_lock_irqsave(&rtc->lock, flags);
  334. ccr = rtc->data->read_rtc_reg(rtc, RTC_CCR);
  335. spin_unlock_irqrestore(&rtc->lock, flags);
  336. ppb_cor = (ccr & RTC_CCR_MODE ? 3815 : 954) * (s8)ccr;
  337. /* ppb_cor + 1000000000L can never be zero */
  338. *offset = armada38x_ppb_convert(ppb_cor);
  339. return 0;
  340. }
  341. static int armada38x_rtc_set_offset(struct device *dev, long offset)
  342. {
  343. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  344. unsigned long ccr = 0;
  345. long ppb_cor, off;
  346. /*
  347. * The maximum ppb_cor is -128 * 3815 .. 127 * 3815, but we
  348. * need to clamp the input. This equates to -484270 .. 488558.
  349. * Not only is this to stop out of range "off" but also to
  350. * avoid the division by zero in armada38x_ppb_convert().
  351. */
  352. offset = clamp(offset, -484270L, 488558L);
  353. ppb_cor = armada38x_ppb_convert(offset);
  354. /*
  355. * Use low update mode where possible, which gives a better
  356. * resolution of correction.
  357. */
  358. off = DIV_ROUND_CLOSEST(ppb_cor, 954);
  359. if (off > 127 || off < -128) {
  360. ccr = RTC_CCR_MODE;
  361. off = DIV_ROUND_CLOSEST(ppb_cor, 3815);
  362. }
  363. /*
  364. * Armada 388 requires a bit pattern in bits 14..8 depending on
  365. * the sign bit: { 0, ~S, S, S, S, S, S }
  366. */
  367. ccr |= (off & 0x3fff) ^ 0x2000;
  368. rtc_delayed_write(ccr, rtc, RTC_CCR);
  369. return 0;
  370. }
  371. static const struct rtc_class_ops armada38x_rtc_ops = {
  372. .read_time = armada38x_rtc_read_time,
  373. .set_time = armada38x_rtc_set_time,
  374. .read_alarm = armada38x_rtc_read_alarm,
  375. .set_alarm = armada38x_rtc_set_alarm,
  376. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  377. .read_offset = armada38x_rtc_read_offset,
  378. .set_offset = armada38x_rtc_set_offset,
  379. };
  380. static const struct rtc_class_ops armada38x_rtc_ops_noirq = {
  381. .read_time = armada38x_rtc_read_time,
  382. .set_time = armada38x_rtc_set_time,
  383. .read_alarm = armada38x_rtc_read_alarm,
  384. .read_offset = armada38x_rtc_read_offset,
  385. .set_offset = armada38x_rtc_set_offset,
  386. };
  387. static const struct armada38x_rtc_data armada38x_data = {
  388. .update_mbus_timing = rtc_update_38x_mbus_timing_params,
  389. .read_rtc_reg = read_rtc_register_38x_wa,
  390. .clear_isr = armada38x_clear_isr,
  391. .unmask_interrupt = armada38x_unmask_interrupt,
  392. .alarm = ALARM1,
  393. };
  394. static const struct armada38x_rtc_data armada8k_data = {
  395. .update_mbus_timing = rtc_update_8k_mbus_timing_params,
  396. .read_rtc_reg = read_rtc_register,
  397. .clear_isr = armada8k_clear_isr,
  398. .unmask_interrupt = armada8k_unmask_interrupt,
  399. .alarm = ALARM2,
  400. };
  401. #ifdef CONFIG_OF
  402. static const struct of_device_id armada38x_rtc_of_match_table[] = {
  403. {
  404. .compatible = "marvell,armada-380-rtc",
  405. .data = &armada38x_data,
  406. },
  407. {
  408. .compatible = "marvell,armada-8k-rtc",
  409. .data = &armada8k_data,
  410. },
  411. {}
  412. };
  413. MODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
  414. #endif
  415. static __init int armada38x_rtc_probe(struct platform_device *pdev)
  416. {
  417. struct resource *res;
  418. struct armada38x_rtc *rtc;
  419. const struct of_device_id *match;
  420. int ret;
  421. match = of_match_device(armada38x_rtc_of_match_table, &pdev->dev);
  422. if (!match)
  423. return -ENODEV;
  424. rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  425. GFP_KERNEL);
  426. if (!rtc)
  427. return -ENOMEM;
  428. rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
  429. sizeof(struct value_to_freq), GFP_KERNEL);
  430. if (!rtc->val_to_freq)
  431. return -ENOMEM;
  432. spin_lock_init(&rtc->lock);
  433. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  434. rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  435. if (IS_ERR(rtc->regs))
  436. return PTR_ERR(rtc->regs);
  437. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  438. rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  439. if (IS_ERR(rtc->regs_soc))
  440. return PTR_ERR(rtc->regs_soc);
  441. rtc->irq = platform_get_irq(pdev, 0);
  442. if (rtc->irq < 0) {
  443. dev_err(&pdev->dev, "no irq\n");
  444. return rtc->irq;
  445. }
  446. rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  447. if (IS_ERR(rtc->rtc_dev))
  448. return PTR_ERR(rtc->rtc_dev);
  449. if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  450. 0, pdev->name, rtc) < 0) {
  451. dev_warn(&pdev->dev, "Interrupt not available.\n");
  452. rtc->irq = -1;
  453. }
  454. platform_set_drvdata(pdev, rtc);
  455. if (rtc->irq != -1) {
  456. device_init_wakeup(&pdev->dev, 1);
  457. rtc->rtc_dev->ops = &armada38x_rtc_ops;
  458. } else {
  459. /*
  460. * If there is no interrupt available then we can't
  461. * use the alarm
  462. */
  463. rtc->rtc_dev->ops = &armada38x_rtc_ops_noirq;
  464. }
  465. rtc->data = (struct armada38x_rtc_data *)match->data;
  466. /* Update RTC-MBUS bridge timing parameters */
  467. rtc->data->update_mbus_timing(rtc);
  468. rtc->rtc_dev->range_max = U32_MAX;
  469. ret = rtc_register_device(rtc->rtc_dev);
  470. if (ret)
  471. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  472. return ret;
  473. }
  474. #ifdef CONFIG_PM_SLEEP
  475. static int armada38x_rtc_suspend(struct device *dev)
  476. {
  477. if (device_may_wakeup(dev)) {
  478. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  479. return enable_irq_wake(rtc->irq);
  480. }
  481. return 0;
  482. }
  483. static int armada38x_rtc_resume(struct device *dev)
  484. {
  485. if (device_may_wakeup(dev)) {
  486. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  487. /* Update RTC-MBUS bridge timing parameters */
  488. rtc->data->update_mbus_timing(rtc);
  489. return disable_irq_wake(rtc->irq);
  490. }
  491. return 0;
  492. }
  493. #endif
  494. static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  495. armada38x_rtc_suspend, armada38x_rtc_resume);
  496. static struct platform_driver armada38x_rtc_driver = {
  497. .driver = {
  498. .name = "armada38x-rtc",
  499. .pm = &armada38x_rtc_pm_ops,
  500. .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  501. },
  502. };
  503. module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  504. MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  505. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  506. MODULE_LICENSE("GPL");