rtc-armada38x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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_time_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. int ret = 0;
  211. unsigned long time, flags;
  212. ret = rtc_tm_to_time(tm, &time);
  213. if (ret)
  214. goto out;
  215. if (!rtc->initialized)
  216. armada38x_rtc_reset(rtc);
  217. spin_lock_irqsave(&rtc->lock, flags);
  218. rtc_delayed_write(time, rtc, RTC_TIME);
  219. spin_unlock_irqrestore(&rtc->lock, flags);
  220. out:
  221. return ret;
  222. }
  223. static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  224. {
  225. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  226. unsigned long time, flags;
  227. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  228. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  229. u32 val;
  230. spin_lock_irqsave(&rtc->lock, flags);
  231. time = rtc->data->read_rtc_reg(rtc, reg);
  232. val = rtc->data->read_rtc_reg(rtc, reg_irq) & RTC_IRQ_AL_EN;
  233. spin_unlock_irqrestore(&rtc->lock, flags);
  234. alrm->enabled = val ? 1 : 0;
  235. rtc_time_to_tm(time, &alrm->time);
  236. return 0;
  237. }
  238. static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  239. {
  240. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  241. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  242. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  243. unsigned long time, flags;
  244. int ret = 0;
  245. ret = rtc_tm_to_time(&alrm->time, &time);
  246. if (ret)
  247. goto out;
  248. spin_lock_irqsave(&rtc->lock, flags);
  249. rtc_delayed_write(time, rtc, reg);
  250. if (alrm->enabled) {
  251. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  252. rtc->data->unmask_interrupt(rtc);
  253. }
  254. spin_unlock_irqrestore(&rtc->lock, flags);
  255. out:
  256. return ret;
  257. }
  258. static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  259. unsigned int enabled)
  260. {
  261. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  262. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  263. unsigned long flags;
  264. spin_lock_irqsave(&rtc->lock, flags);
  265. if (enabled)
  266. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  267. else
  268. rtc_delayed_write(0, rtc, reg_irq);
  269. spin_unlock_irqrestore(&rtc->lock, flags);
  270. return 0;
  271. }
  272. static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  273. {
  274. struct armada38x_rtc *rtc = data;
  275. u32 val;
  276. int event = RTC_IRQF | RTC_AF;
  277. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  278. dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  279. spin_lock(&rtc->lock);
  280. rtc->data->clear_isr(rtc);
  281. val = rtc->data->read_rtc_reg(rtc, reg_irq);
  282. /* disable all the interrupts for alarm*/
  283. rtc_delayed_write(0, rtc, reg_irq);
  284. /* Ack the event */
  285. rtc_delayed_write(1 << rtc->data->alarm, rtc, RTC_STATUS);
  286. spin_unlock(&rtc->lock);
  287. if (val & RTC_IRQ_FREQ_EN) {
  288. if (val & RTC_IRQ_FREQ_1HZ)
  289. event |= RTC_UF;
  290. else
  291. event |= RTC_PF;
  292. }
  293. rtc_update_irq(rtc->rtc_dev, 1, event);
  294. return IRQ_HANDLED;
  295. }
  296. /*
  297. * The information given in the Armada 388 functional spec is complex.
  298. * They give two different formulas for calculating the offset value,
  299. * but when considering "Offset" as an 8-bit signed integer, they both
  300. * reduce down to (we shall rename "Offset" as "val" here):
  301. *
  302. * val = (f_ideal / f_measured - 1) / resolution where f_ideal = 32768
  303. *
  304. * Converting to time, f = 1/t:
  305. * val = (t_measured / t_ideal - 1) / resolution where t_ideal = 1/32768
  306. *
  307. * => t_measured / t_ideal = val * resolution + 1
  308. *
  309. * "offset" in the RTC interface is defined as:
  310. * t = t0 * (1 + offset * 1e-9)
  311. * where t is the desired period, t0 is the measured period with a zero
  312. * offset, which is t_measured above. With t0 = t_measured and t = t_ideal,
  313. * offset = (t_ideal / t_measured - 1) / 1e-9
  314. *
  315. * => t_ideal / t_measured = offset * 1e-9 + 1
  316. *
  317. * so:
  318. *
  319. * offset * 1e-9 + 1 = 1 / (val * resolution + 1)
  320. *
  321. * We want "resolution" to be an integer, so resolution = R * 1e-9, giving
  322. * offset = 1e18 / (val * R + 1e9) - 1e9
  323. * val = (1e18 / (offset + 1e9) - 1e9) / R
  324. * with a common transformation:
  325. * f(x) = 1e18 / (x + 1e9) - 1e9
  326. * offset = f(val * R)
  327. * val = f(offset) / R
  328. *
  329. * Armada 38x supports two modes, fine mode (954ppb) and coarse mode (3815ppb).
  330. */
  331. static long armada38x_ppb_convert(long ppb)
  332. {
  333. long div = ppb + 1000000000L;
  334. return div_s64(1000000000000000000LL + div / 2, div) - 1000000000L;
  335. }
  336. static int armada38x_rtc_read_offset(struct device *dev, long *offset)
  337. {
  338. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  339. unsigned long ccr, flags;
  340. long ppb_cor;
  341. spin_lock_irqsave(&rtc->lock, flags);
  342. ccr = rtc->data->read_rtc_reg(rtc, RTC_CCR);
  343. spin_unlock_irqrestore(&rtc->lock, flags);
  344. ppb_cor = (ccr & RTC_CCR_MODE ? 3815 : 954) * (s8)ccr;
  345. /* ppb_cor + 1000000000L can never be zero */
  346. *offset = armada38x_ppb_convert(ppb_cor);
  347. return 0;
  348. }
  349. static int armada38x_rtc_set_offset(struct device *dev, long offset)
  350. {
  351. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  352. unsigned long ccr = 0;
  353. long ppb_cor, off;
  354. /*
  355. * The maximum ppb_cor is -128 * 3815 .. 127 * 3815, but we
  356. * need to clamp the input. This equates to -484270 .. 488558.
  357. * Not only is this to stop out of range "off" but also to
  358. * avoid the division by zero in armada38x_ppb_convert().
  359. */
  360. offset = clamp(offset, -484270L, 488558L);
  361. ppb_cor = armada38x_ppb_convert(offset);
  362. /*
  363. * Use low update mode where possible, which gives a better
  364. * resolution of correction.
  365. */
  366. off = DIV_ROUND_CLOSEST(ppb_cor, 954);
  367. if (off > 127 || off < -128) {
  368. ccr = RTC_CCR_MODE;
  369. off = DIV_ROUND_CLOSEST(ppb_cor, 3815);
  370. }
  371. /*
  372. * Armada 388 requires a bit pattern in bits 14..8 depending on
  373. * the sign bit: { 0, ~S, S, S, S, S, S }
  374. */
  375. ccr |= (off & 0x3fff) ^ 0x2000;
  376. rtc_delayed_write(ccr, rtc, RTC_CCR);
  377. return 0;
  378. }
  379. static const struct rtc_class_ops armada38x_rtc_ops = {
  380. .read_time = armada38x_rtc_read_time,
  381. .set_time = armada38x_rtc_set_time,
  382. .read_alarm = armada38x_rtc_read_alarm,
  383. .set_alarm = armada38x_rtc_set_alarm,
  384. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  385. .read_offset = armada38x_rtc_read_offset,
  386. .set_offset = armada38x_rtc_set_offset,
  387. };
  388. static const struct rtc_class_ops armada38x_rtc_ops_noirq = {
  389. .read_time = armada38x_rtc_read_time,
  390. .set_time = armada38x_rtc_set_time,
  391. .read_alarm = armada38x_rtc_read_alarm,
  392. .read_offset = armada38x_rtc_read_offset,
  393. .set_offset = armada38x_rtc_set_offset,
  394. };
  395. static const struct armada38x_rtc_data armada38x_data = {
  396. .update_mbus_timing = rtc_update_38x_mbus_timing_params,
  397. .read_rtc_reg = read_rtc_register_38x_wa,
  398. .clear_isr = armada38x_clear_isr,
  399. .unmask_interrupt = armada38x_unmask_interrupt,
  400. .alarm = ALARM1,
  401. };
  402. static const struct armada38x_rtc_data armada8k_data = {
  403. .update_mbus_timing = rtc_update_8k_mbus_timing_params,
  404. .read_rtc_reg = read_rtc_register,
  405. .clear_isr = armada8k_clear_isr,
  406. .unmask_interrupt = armada8k_unmask_interrupt,
  407. .alarm = ALARM2,
  408. };
  409. #ifdef CONFIG_OF
  410. static const struct of_device_id armada38x_rtc_of_match_table[] = {
  411. {
  412. .compatible = "marvell,armada-380-rtc",
  413. .data = &armada38x_data,
  414. },
  415. {
  416. .compatible = "marvell,armada-8k-rtc",
  417. .data = &armada8k_data,
  418. },
  419. {}
  420. };
  421. MODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
  422. #endif
  423. static __init int armada38x_rtc_probe(struct platform_device *pdev)
  424. {
  425. struct resource *res;
  426. struct armada38x_rtc *rtc;
  427. const struct of_device_id *match;
  428. int ret;
  429. match = of_match_device(armada38x_rtc_of_match_table, &pdev->dev);
  430. if (!match)
  431. return -ENODEV;
  432. rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  433. GFP_KERNEL);
  434. if (!rtc)
  435. return -ENOMEM;
  436. rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
  437. sizeof(struct value_to_freq), GFP_KERNEL);
  438. if (!rtc->val_to_freq)
  439. return -ENOMEM;
  440. spin_lock_init(&rtc->lock);
  441. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  442. rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  443. if (IS_ERR(rtc->regs))
  444. return PTR_ERR(rtc->regs);
  445. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  446. rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  447. if (IS_ERR(rtc->regs_soc))
  448. return PTR_ERR(rtc->regs_soc);
  449. rtc->irq = platform_get_irq(pdev, 0);
  450. if (rtc->irq < 0) {
  451. dev_err(&pdev->dev, "no irq\n");
  452. return rtc->irq;
  453. }
  454. rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  455. if (IS_ERR(rtc->rtc_dev))
  456. return PTR_ERR(rtc->rtc_dev);
  457. if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  458. 0, pdev->name, rtc) < 0) {
  459. dev_warn(&pdev->dev, "Interrupt not available.\n");
  460. rtc->irq = -1;
  461. }
  462. platform_set_drvdata(pdev, rtc);
  463. if (rtc->irq != -1) {
  464. device_init_wakeup(&pdev->dev, 1);
  465. rtc->rtc_dev->ops = &armada38x_rtc_ops;
  466. } else {
  467. /*
  468. * If there is no interrupt available then we can't
  469. * use the alarm
  470. */
  471. rtc->rtc_dev->ops = &armada38x_rtc_ops_noirq;
  472. }
  473. rtc->data = (struct armada38x_rtc_data *)match->data;
  474. /* Update RTC-MBUS bridge timing parameters */
  475. rtc->data->update_mbus_timing(rtc);
  476. ret = rtc_register_device(rtc->rtc_dev);
  477. if (ret)
  478. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  479. return ret;
  480. }
  481. #ifdef CONFIG_PM_SLEEP
  482. static int armada38x_rtc_suspend(struct device *dev)
  483. {
  484. if (device_may_wakeup(dev)) {
  485. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  486. return enable_irq_wake(rtc->irq);
  487. }
  488. return 0;
  489. }
  490. static int armada38x_rtc_resume(struct device *dev)
  491. {
  492. if (device_may_wakeup(dev)) {
  493. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  494. /* Update RTC-MBUS bridge timing parameters */
  495. rtc->data->update_mbus_timing(rtc);
  496. return disable_irq_wake(rtc->irq);
  497. }
  498. return 0;
  499. }
  500. #endif
  501. static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  502. armada38x_rtc_suspend, armada38x_rtc_resume);
  503. static struct platform_driver armada38x_rtc_driver = {
  504. .driver = {
  505. .name = "armada38x-rtc",
  506. .pm = &armada38x_rtc_pm_ops,
  507. .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  508. },
  509. };
  510. module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  511. MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  512. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  513. MODULE_LICENSE("GPL");