rtc-pm8xxx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  3. */
  4. #include <linux/of.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/rtc.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm.h>
  10. #include <linux/pm_wakeirq.h>
  11. #include <linux/regmap.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/suspend.h>
  15. /* RTC Register offsets from RTC CTRL REG */
  16. #define PM8XXX_ALARM_CTRL_OFFSET 0x01
  17. #define PM8XXX_RTC_WRITE_OFFSET 0x02
  18. #define PM8XXX_RTC_READ_OFFSET 0x06
  19. #define PM8XXX_ALARM_RW_OFFSET 0x0A
  20. /* RTC_CTRL register bit fields */
  21. #define PM8xxx_RTC_ENABLE BIT(7)
  22. #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
  23. #define PM8xxx_RTC_ALARM_ENABLE BIT(7)
  24. #define NUM_8_BIT_RTC_REGS 0x4
  25. /**
  26. * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
  27. * @ctrl: base address of control register
  28. * @write: base address of write register
  29. * @read: base address of read register
  30. * @alarm_ctrl: base address of alarm control register
  31. * @alarm_ctrl2: base address of alarm control2 register
  32. * @alarm_rw: base address of alarm read-write register
  33. * @alarm_en: alarm enable mask
  34. */
  35. struct pm8xxx_rtc_regs {
  36. unsigned int ctrl;
  37. unsigned int write;
  38. unsigned int read;
  39. unsigned int alarm_ctrl;
  40. unsigned int alarm_ctrl2;
  41. unsigned int alarm_rw;
  42. unsigned int alarm_en;
  43. };
  44. /**
  45. * struct pm8xxx_rtc - rtc driver internal structure
  46. * @rtc: rtc device for this driver.
  47. * @regmap: regmap used to access RTC registers
  48. * @allow_set_time: indicates whether writing to the RTC is allowed
  49. * @rtc_alarm_irq: rtc alarm irq number.
  50. * @regs: rtc registers description.
  51. * @rtc_dev: device structure.
  52. * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
  53. * @deepsleep_support: deepsleep_support used to check the status of enablement
  54. */
  55. struct pm8xxx_rtc {
  56. struct rtc_device *rtc;
  57. struct regmap *regmap;
  58. bool allow_set_time;
  59. int rtc_alarm_irq;
  60. const struct pm8xxx_rtc_regs *regs;
  61. struct device *rtc_dev;
  62. spinlock_t ctrl_reg_lock;
  63. bool deepsleep_support;
  64. };
  65. /*
  66. * Steps to write the RTC registers.
  67. * 1. Disable alarm if enabled.
  68. * 2. Disable rtc if enabled.
  69. * 3. Write 0x00 to LSB.
  70. * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
  71. * 5. Enable rtc if disabled in step 2.
  72. * 6. Enable alarm if disabled in step 1.
  73. */
  74. static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  75. {
  76. int rc, i;
  77. unsigned long secs, irq_flags;
  78. u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0;
  79. unsigned int ctrl_reg, rtc_ctrl_reg;
  80. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  81. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  82. if (!rtc_dd->allow_set_time)
  83. return -ENODEV;
  84. secs = rtc_tm_to_time64(tm);
  85. dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
  86. for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
  87. value[i] = secs & 0xFF;
  88. secs >>= 8;
  89. }
  90. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  91. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  92. if (rc)
  93. goto rtc_rw_fail;
  94. if (ctrl_reg & regs->alarm_en) {
  95. alarm_enabled = 1;
  96. ctrl_reg &= ~regs->alarm_en;
  97. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  98. if (rc) {
  99. dev_err(dev, "Write to RTC Alarm control register failed\n");
  100. goto rtc_rw_fail;
  101. }
  102. }
  103. /* Disable RTC H/w before writing on RTC register */
  104. rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg);
  105. if (rc)
  106. goto rtc_rw_fail;
  107. if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) {
  108. rtc_disabled = 1;
  109. rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE;
  110. rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
  111. if (rc) {
  112. dev_err(dev, "Write to RTC control register failed\n");
  113. goto rtc_rw_fail;
  114. }
  115. }
  116. /* Write 0 to Byte[0] */
  117. rc = regmap_write(rtc_dd->regmap, regs->write, 0);
  118. if (rc) {
  119. dev_err(dev, "Write to RTC write data register failed\n");
  120. goto rtc_rw_fail;
  121. }
  122. /* Write Byte[1], Byte[2], Byte[3] */
  123. rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
  124. &value[1], sizeof(value) - 1);
  125. if (rc) {
  126. dev_err(dev, "Write to RTC write data register failed\n");
  127. goto rtc_rw_fail;
  128. }
  129. /* Write Byte[0] */
  130. rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
  131. if (rc) {
  132. dev_err(dev, "Write to RTC write data register failed\n");
  133. goto rtc_rw_fail;
  134. }
  135. /* Enable RTC H/w after writing on RTC register */
  136. if (rtc_disabled) {
  137. rtc_ctrl_reg |= PM8xxx_RTC_ENABLE;
  138. rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
  139. if (rc) {
  140. dev_err(dev, "Write to RTC control register failed\n");
  141. goto rtc_rw_fail;
  142. }
  143. }
  144. if (alarm_enabled) {
  145. ctrl_reg |= regs->alarm_en;
  146. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  147. if (rc) {
  148. dev_err(dev, "Write to RTC Alarm control register failed\n");
  149. goto rtc_rw_fail;
  150. }
  151. }
  152. rtc_rw_fail:
  153. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  154. return rc;
  155. }
  156. static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  157. {
  158. int rc;
  159. u8 value[NUM_8_BIT_RTC_REGS];
  160. unsigned long secs;
  161. unsigned int reg;
  162. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  163. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  164. rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
  165. if (rc) {
  166. dev_err(dev, "RTC read data register failed\n");
  167. return rc;
  168. }
  169. /*
  170. * Read the LSB again and check if there has been a carry over.
  171. * If there is, redo the read operation.
  172. */
  173. rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
  174. if (rc < 0) {
  175. dev_err(dev, "RTC read data register failed\n");
  176. return rc;
  177. }
  178. if (unlikely(reg < value[0])) {
  179. rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
  180. value, sizeof(value));
  181. if (rc) {
  182. dev_err(dev, "RTC read data register failed\n");
  183. return rc;
  184. }
  185. }
  186. secs = value[0] | (value[1] << 8) | (value[2] << 16) |
  187. ((unsigned long)value[3] << 24);
  188. rtc_time64_to_tm(secs, tm);
  189. dev_dbg(dev, "secs = %lu, h:m:s == %ptRt, y-m-d = %ptRdr\n", secs, tm, tm);
  190. return 0;
  191. }
  192. static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  193. {
  194. int rc, i;
  195. u8 value[NUM_8_BIT_RTC_REGS];
  196. unsigned long secs, irq_flags;
  197. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  198. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  199. secs = rtc_tm_to_time64(&alarm->time);
  200. for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
  201. value[i] = secs & 0xFF;
  202. secs >>= 8;
  203. }
  204. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  205. regs->alarm_en, 0);
  206. if (rc)
  207. return rc;
  208. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  209. rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
  210. sizeof(value));
  211. if (rc) {
  212. dev_err(dev, "Write to RTC ALARM register failed\n");
  213. goto rtc_rw_fail;
  214. }
  215. if (alarm->enabled) {
  216. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  217. regs->alarm_en, regs->alarm_en);
  218. if (rc)
  219. goto rtc_rw_fail;
  220. }
  221. dev_dbg(dev, "Alarm Set for h:m:s=%ptRt, y-m-d=%ptRdr\n",
  222. &alarm->time, &alarm->time);
  223. rtc_rw_fail:
  224. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  225. return rc;
  226. }
  227. static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  228. {
  229. int rc;
  230. unsigned int ctrl_reg;
  231. u8 value[NUM_8_BIT_RTC_REGS];
  232. unsigned long secs;
  233. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  234. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  235. rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
  236. sizeof(value));
  237. if (rc) {
  238. dev_err(dev, "RTC alarm time read failed\n");
  239. return rc;
  240. }
  241. secs = value[0] | (value[1] << 8) | (value[2] << 16) |
  242. ((unsigned long)value[3] << 24);
  243. rtc_time64_to_tm(secs, &alarm->time);
  244. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  245. if (rc) {
  246. dev_err(dev, "Read from RTC alarm control register failed\n");
  247. return rc;
  248. }
  249. alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
  250. dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n",
  251. &alarm->time, &alarm->time);
  252. return 0;
  253. }
  254. static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  255. {
  256. int rc;
  257. unsigned long irq_flags;
  258. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  259. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  260. unsigned int ctrl_reg;
  261. u8 value[NUM_8_BIT_RTC_REGS] = {0};
  262. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  263. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  264. if (rc)
  265. goto rtc_rw_fail;
  266. if (enable)
  267. ctrl_reg |= regs->alarm_en;
  268. else
  269. ctrl_reg &= ~regs->alarm_en;
  270. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  271. if (rc) {
  272. dev_err(dev, "Write to RTC control register failed\n");
  273. goto rtc_rw_fail;
  274. }
  275. /* Clear Alarm register */
  276. if (!enable) {
  277. rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
  278. sizeof(value));
  279. if (rc) {
  280. dev_err(dev, "Clear RTC ALARM register failed\n");
  281. goto rtc_rw_fail;
  282. }
  283. }
  284. rtc_rw_fail:
  285. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  286. return rc;
  287. }
  288. static const struct rtc_class_ops pm8xxx_rtc_ops = {
  289. .read_time = pm8xxx_rtc_read_time,
  290. .set_time = pm8xxx_rtc_set_time,
  291. .set_alarm = pm8xxx_rtc_set_alarm,
  292. .read_alarm = pm8xxx_rtc_read_alarm,
  293. .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
  294. };
  295. static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
  296. {
  297. struct pm8xxx_rtc *rtc_dd = dev_id;
  298. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  299. unsigned int ctrl_reg;
  300. int rc;
  301. rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
  302. spin_lock(&rtc_dd->ctrl_reg_lock);
  303. /* Clear the alarm enable bit */
  304. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  305. if (rc) {
  306. spin_unlock(&rtc_dd->ctrl_reg_lock);
  307. goto rtc_alarm_handled;
  308. }
  309. ctrl_reg &= ~regs->alarm_en;
  310. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  311. if (rc) {
  312. spin_unlock(&rtc_dd->ctrl_reg_lock);
  313. dev_err(rtc_dd->rtc_dev,
  314. "Write to alarm control register failed\n");
  315. goto rtc_alarm_handled;
  316. }
  317. spin_unlock(&rtc_dd->ctrl_reg_lock);
  318. /* Clear RTC alarm register */
  319. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
  320. if (rc) {
  321. dev_err(rtc_dd->rtc_dev,
  322. "RTC Alarm control2 register read failed\n");
  323. goto rtc_alarm_handled;
  324. }
  325. ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
  326. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
  327. if (rc)
  328. dev_err(rtc_dd->rtc_dev,
  329. "Write to RTC Alarm control2 register failed\n");
  330. rtc_alarm_handled:
  331. return IRQ_HANDLED;
  332. }
  333. static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
  334. {
  335. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  336. unsigned int ctrl_reg;
  337. int rc;
  338. /* Check if the RTC is on, else turn it on */
  339. rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
  340. if (rc)
  341. return rc;
  342. if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
  343. ctrl_reg |= PM8xxx_RTC_ENABLE;
  344. rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
  345. if (rc)
  346. return rc;
  347. }
  348. return 0;
  349. }
  350. static const struct pm8xxx_rtc_regs pm8921_regs = {
  351. .ctrl = 0x11d,
  352. .write = 0x11f,
  353. .read = 0x123,
  354. .alarm_rw = 0x127,
  355. .alarm_ctrl = 0x11d,
  356. .alarm_ctrl2 = 0x11e,
  357. .alarm_en = BIT(1),
  358. };
  359. static const struct pm8xxx_rtc_regs pm8058_regs = {
  360. .ctrl = 0x1e8,
  361. .write = 0x1ea,
  362. .read = 0x1ee,
  363. .alarm_rw = 0x1f2,
  364. .alarm_ctrl = 0x1e8,
  365. .alarm_ctrl2 = 0x1e9,
  366. .alarm_en = BIT(1),
  367. };
  368. static const struct pm8xxx_rtc_regs pm8941_regs = {
  369. .ctrl = 0x6046,
  370. .write = 0x6040,
  371. .read = 0x6048,
  372. .alarm_rw = 0x6140,
  373. .alarm_ctrl = 0x6146,
  374. .alarm_ctrl2 = 0x6148,
  375. .alarm_en = BIT(7),
  376. };
  377. static const struct pm8xxx_rtc_regs pmk8350_regs = {
  378. .ctrl = 0x6146,
  379. .write = 0x6140,
  380. .read = 0x6148,
  381. .alarm_rw = 0x6240,
  382. .alarm_ctrl = 0x6246,
  383. .alarm_ctrl2 = 0x6248,
  384. .alarm_en = BIT(7),
  385. };
  386. /*
  387. * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
  388. */
  389. static const struct of_device_id pm8xxx_id_table[] = {
  390. { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
  391. { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs },
  392. { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
  393. { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
  394. { .compatible = "qcom,pmk8350-rtc", .data = &pmk8350_regs },
  395. { },
  396. };
  397. MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
  398. static int pm8xxx_rtc_probe(struct platform_device *pdev)
  399. {
  400. int rc;
  401. struct pm8xxx_rtc *rtc_dd;
  402. const struct of_device_id *match;
  403. match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
  404. if (!match)
  405. return -ENXIO;
  406. rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
  407. if (rtc_dd == NULL)
  408. return -ENOMEM;
  409. /* Initialise spinlock to protect RTC control register */
  410. spin_lock_init(&rtc_dd->ctrl_reg_lock);
  411. rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  412. if (!rtc_dd->regmap) {
  413. dev_err(&pdev->dev, "Parent regmap unavailable.\n");
  414. return -ENXIO;
  415. }
  416. rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
  417. if (rtc_dd->rtc_alarm_irq < 0)
  418. return -ENXIO;
  419. rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
  420. "allow-set-time");
  421. rtc_dd->regs = match->data;
  422. rtc_dd->rtc_dev = &pdev->dev;
  423. rc = pm8xxx_rtc_enable(rtc_dd);
  424. if (rc)
  425. return rc;
  426. platform_set_drvdata(pdev, rtc_dd);
  427. device_init_wakeup(&pdev->dev, 1);
  428. /* Register the RTC device */
  429. rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
  430. if (IS_ERR(rtc_dd->rtc))
  431. return PTR_ERR(rtc_dd->rtc);
  432. rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
  433. rtc_dd->rtc->range_max = U32_MAX;
  434. /* Request the alarm IRQ */
  435. rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
  436. pm8xxx_alarm_trigger,
  437. IRQF_TRIGGER_RISING,
  438. "pm8xxx_rtc_alarm", rtc_dd);
  439. if (rc < 0) {
  440. dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
  441. return rc;
  442. }
  443. rc = devm_rtc_register_device(rtc_dd->rtc);
  444. if (rc)
  445. return rc;
  446. if (!of_property_read_bool(pdev->dev.of_node, "qcom,disable-alarm-wakeup")) {
  447. rc = dev_pm_set_wake_irq(&pdev->dev, rtc_dd->rtc_alarm_irq);
  448. if (rc)
  449. return rc;
  450. }
  451. if (of_property_read_bool(pdev->dev.of_node, "qcom,support-deepsleep"))
  452. rtc_dd->deepsleep_support = true;
  453. return 0;
  454. }
  455. static int pm8xxx_rtc_restore(struct device *dev)
  456. {
  457. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  458. int rc;
  459. /* Request the alarm IRQ */
  460. rc = devm_request_any_context_irq(rtc_dd->rtc_dev,
  461. rtc_dd->rtc_alarm_irq,
  462. pm8xxx_alarm_trigger,
  463. IRQF_TRIGGER_RISING,
  464. "pm8xxx_rtc_alarm", rtc_dd);
  465. if (rc < 0) {
  466. dev_err(rtc_dd->rtc_dev, "Request IRQ failed (%d)\n", rc);
  467. return rc;
  468. }
  469. return pm8xxx_rtc_enable(rtc_dd);
  470. }
  471. static int pm8xxx_rtc_freeze(struct device *dev)
  472. {
  473. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  474. devm_free_irq(rtc_dd->rtc_dev, rtc_dd->rtc_alarm_irq, rtc_dd);
  475. return 0;
  476. }
  477. #if IS_ENABLED(CONFIG_RTC_AUTO_PWRON)
  478. static struct rtc_wkalrm pwron_alarm;
  479. void pmic_rtc_setalarm(struct rtc_wkalrm *alm)
  480. {
  481. memcpy(&pwron_alarm, alm, sizeof(struct rtc_wkalrm));
  482. }
  483. EXPORT_SYMBOL(pmic_rtc_setalarm);
  484. static void pm8xxx_rtc_shutdown(struct platform_device *pdev)
  485. {
  486. struct rtc_wkalrm alarm;
  487. int rc = 0;
  488. if (pdev) {
  489. pm8xxx_rtc_set_alarm(&pdev->dev, &pwron_alarm);
  490. rc = pm8xxx_rtc_read_alarm(&pdev->dev, &alarm);
  491. if (!rc) {
  492. pr_info("%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__,
  493. alarm.time.tm_year + 1900, alarm.time.tm_mon + 1, alarm.time.tm_mday,
  494. alarm.time.tm_hour, alarm.time.tm_min, alarm.time.tm_sec);
  495. }
  496. } else
  497. pr_err("%s: spmi device not found\n", __func__);
  498. }
  499. #endif
  500. #if IS_ENABLED(CONFIG_SEC_SAPA_SHIPMODE)
  501. static struct rtc_wkalrm sapa_shipmode_alarm;
  502. void sapa_shipmode_setalarm(struct rtc_wkalrm *alm)
  503. {
  504. memcpy(&sapa_shipmode_alarm, alm, sizeof(struct rtc_wkalrm));
  505. }
  506. EXPORT_SYMBOL(sapa_shipmode_setalarm);
  507. // TODO: After Shutdown implementation is completed, add sec-sapa-shipmode logic to Shutdown function
  508. #endif
  509. static int pm8xxx_remove(struct platform_device *pdev)
  510. {
  511. dev_pm_clear_wake_irq(&pdev->dev);
  512. return 0;
  513. }
  514. static int pm8xxx_rtc_resume(struct device *dev)
  515. {
  516. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  517. if (rtc_dd->deepsleep_support && (pm_suspend_target_state == PM_SUSPEND_MEM))
  518. return pm8xxx_rtc_restore(dev);
  519. return 0;
  520. }
  521. static int pm8xxx_rtc_suspend(struct device *dev)
  522. {
  523. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  524. if (rtc_dd->deepsleep_support && (pm_suspend_target_state == PM_SUSPEND_MEM))
  525. return pm8xxx_rtc_freeze(dev);
  526. return 0;
  527. }
  528. static const struct dev_pm_ops pm8xxx_rtc_pm_ops = {
  529. .freeze = pm8xxx_rtc_freeze,
  530. .restore = pm8xxx_rtc_restore,
  531. .suspend = pm8xxx_rtc_suspend,
  532. .resume = pm8xxx_rtc_resume,
  533. };
  534. static struct platform_driver pm8xxx_rtc_driver = {
  535. .probe = pm8xxx_rtc_probe,
  536. #if IS_ENABLED(CONFIG_RTC_AUTO_PWRON)
  537. .shutdown = pm8xxx_rtc_shutdown,
  538. #endif
  539. .remove = pm8xxx_remove,
  540. .driver = {
  541. .name = "rtc-pm8xxx",
  542. .of_match_table = pm8xxx_id_table,
  543. .pm = &pm8xxx_rtc_pm_ops,
  544. },
  545. };
  546. module_platform_driver(pm8xxx_rtc_driver);
  547. MODULE_ALIAS("platform:rtc-pm8xxx");
  548. MODULE_DESCRIPTION("PMIC8xxx RTC driver");
  549. MODULE_LICENSE("GPL v2");
  550. MODULE_AUTHOR("Anirudh Ghayal <[email protected]>");