rtc-pm8xxx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * pm8xxx RTC driver
  4. *
  5. * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  6. * Copyright (c) 2023, Linaro Limited
  7. */
  8. #include <linux/efi.h>
  9. #include <linux/of.h>
  10. #include <linux/module.h>
  11. #include <linux/nvmem-consumer.h>
  12. #include <linux/init.h>
  13. #include <linux/rtc.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/pm_wakeirq.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/unaligned.h>
  21. #include <asm/byteorder.h>
  22. /* RTC_CTRL register bit fields */
  23. #define PM8xxx_RTC_ENABLE BIT(7)
  24. #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
  25. #define PM8xxx_RTC_ALARM_ENABLE BIT(7)
  26. #define NUM_8_BIT_RTC_REGS 0x4
  27. /**
  28. * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
  29. * @ctrl: address of control register
  30. * @write: base address of write registers
  31. * @read: base address of read registers
  32. * @alarm_ctrl: address of alarm control register
  33. * @alarm_ctrl2: address of alarm control2 register
  34. * @alarm_rw: base address of alarm read-write registers
  35. * @alarm_en: alarm enable mask
  36. */
  37. struct pm8xxx_rtc_regs {
  38. unsigned int ctrl;
  39. unsigned int write;
  40. unsigned int read;
  41. unsigned int alarm_ctrl;
  42. unsigned int alarm_ctrl2;
  43. unsigned int alarm_rw;
  44. unsigned int alarm_en;
  45. };
  46. struct qcom_uefi_rtc_info {
  47. __le32 offset_gps;
  48. u8 reserved[8];
  49. } __packed;
  50. /**
  51. * struct pm8xxx_rtc - RTC driver internal structure
  52. * @rtc: RTC device
  53. * @regmap: regmap used to access registers
  54. * @allow_set_time: whether the time can be set
  55. * @use_uefi: use UEFI variable as fallback for offset
  56. * @alarm_irq: alarm irq number
  57. * @regs: register description
  58. * @dev: device structure
  59. * @rtc_info: qcom uefi rtc-info structure
  60. * @nvmem_cell: nvmem cell for offset
  61. * @offset: offset from epoch in seconds
  62. * @offset_dirty: offset needs to be stored on shutdown
  63. */
  64. struct pm8xxx_rtc {
  65. struct rtc_device *rtc;
  66. struct regmap *regmap;
  67. bool allow_set_time;
  68. bool use_uefi;
  69. int alarm_irq;
  70. const struct pm8xxx_rtc_regs *regs;
  71. struct device *dev;
  72. struct qcom_uefi_rtc_info rtc_info;
  73. struct nvmem_cell *nvmem_cell;
  74. u32 offset;
  75. bool offset_dirty;
  76. };
  77. #ifdef CONFIG_EFI
  78. MODULE_IMPORT_NS("EFIVAR");
  79. #define QCOM_UEFI_NAME L"RTCInfo"
  80. #define QCOM_UEFI_GUID EFI_GUID(0x882f8c2b, 0x9646, 0x435f, \
  81. 0x8d, 0xe5, 0xf2, 0x08, 0xff, 0x80, 0xc1, 0xbd)
  82. #define QCOM_UEFI_ATTRS (EFI_VARIABLE_NON_VOLATILE | \
  83. EFI_VARIABLE_BOOTSERVICE_ACCESS | \
  84. EFI_VARIABLE_RUNTIME_ACCESS)
  85. static int pm8xxx_rtc_read_uefi_offset(struct pm8xxx_rtc *rtc_dd)
  86. {
  87. struct qcom_uefi_rtc_info *rtc_info = &rtc_dd->rtc_info;
  88. unsigned long size = sizeof(*rtc_info);
  89. struct device *dev = rtc_dd->dev;
  90. efi_status_t status;
  91. u32 offset_gps;
  92. int rc;
  93. rc = efivar_lock();
  94. if (rc)
  95. return rc;
  96. status = efivar_get_variable(QCOM_UEFI_NAME, &QCOM_UEFI_GUID, NULL,
  97. &size, rtc_info);
  98. efivar_unlock();
  99. if (status != EFI_SUCCESS) {
  100. dev_dbg(dev, "failed to read UEFI offset: %lu\n", status);
  101. return efi_status_to_err(status);
  102. }
  103. if (size != sizeof(*rtc_info)) {
  104. dev_dbg(dev, "unexpected UEFI structure size %lu\n", size);
  105. return -EINVAL;
  106. }
  107. dev_dbg(dev, "uefi_rtc_info = %*ph\n", (int)size, rtc_info);
  108. /* Convert from GPS to Unix time offset */
  109. offset_gps = le32_to_cpu(rtc_info->offset_gps);
  110. rtc_dd->offset = offset_gps + (u32)RTC_TIMESTAMP_EPOCH_GPS;
  111. return 0;
  112. }
  113. static int pm8xxx_rtc_write_uefi_offset(struct pm8xxx_rtc *rtc_dd, u32 offset)
  114. {
  115. struct qcom_uefi_rtc_info *rtc_info = &rtc_dd->rtc_info;
  116. unsigned long size = sizeof(*rtc_info);
  117. struct device *dev = rtc_dd->dev;
  118. efi_status_t status;
  119. u32 offset_gps;
  120. /* Convert from Unix to GPS time offset */
  121. offset_gps = offset - (u32)RTC_TIMESTAMP_EPOCH_GPS;
  122. rtc_info->offset_gps = cpu_to_le32(offset_gps);
  123. dev_dbg(dev, "efi_rtc_info = %*ph\n", (int)size, rtc_info);
  124. status = efivar_set_variable(QCOM_UEFI_NAME, &QCOM_UEFI_GUID,
  125. QCOM_UEFI_ATTRS, size, rtc_info);
  126. if (status != EFI_SUCCESS) {
  127. dev_dbg(dev, "failed to write UEFI offset: %lx\n", status);
  128. return efi_status_to_err(status);
  129. }
  130. return 0;
  131. }
  132. #else /* CONFIG_EFI */
  133. static int pm8xxx_rtc_read_uefi_offset(struct pm8xxx_rtc *rtc_dd)
  134. {
  135. return -ENODEV;
  136. }
  137. static int pm8xxx_rtc_write_uefi_offset(struct pm8xxx_rtc *rtc_dd, u32 offset)
  138. {
  139. return -ENODEV;
  140. }
  141. #endif /* CONFIG_EFI */
  142. static int pm8xxx_rtc_read_nvmem_offset(struct pm8xxx_rtc *rtc_dd)
  143. {
  144. size_t len;
  145. void *buf;
  146. int rc;
  147. buf = nvmem_cell_read(rtc_dd->nvmem_cell, &len);
  148. if (IS_ERR(buf)) {
  149. rc = PTR_ERR(buf);
  150. dev_dbg(rtc_dd->dev, "failed to read nvmem offset: %d\n", rc);
  151. return rc;
  152. }
  153. if (len != sizeof(u32)) {
  154. dev_dbg(rtc_dd->dev, "unexpected nvmem cell size %zu\n", len);
  155. kfree(buf);
  156. return -EINVAL;
  157. }
  158. rtc_dd->offset = get_unaligned_le32(buf);
  159. kfree(buf);
  160. return 0;
  161. }
  162. static int pm8xxx_rtc_write_nvmem_offset(struct pm8xxx_rtc *rtc_dd, u32 offset)
  163. {
  164. u8 buf[sizeof(u32)];
  165. int rc;
  166. put_unaligned_le32(offset, buf);
  167. rc = nvmem_cell_write(rtc_dd->nvmem_cell, buf, sizeof(buf));
  168. if (rc < 0) {
  169. dev_dbg(rtc_dd->dev, "failed to write nvmem offset: %d\n", rc);
  170. return rc;
  171. }
  172. return 0;
  173. }
  174. static int pm8xxx_rtc_read_raw(struct pm8xxx_rtc *rtc_dd, u32 *secs)
  175. {
  176. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  177. u8 value[NUM_8_BIT_RTC_REGS];
  178. unsigned int reg;
  179. int rc;
  180. rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
  181. if (rc)
  182. return rc;
  183. /*
  184. * Read the LSB again and check if there has been a carry over.
  185. * If there has, redo the read operation.
  186. */
  187. rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
  188. if (rc < 0)
  189. return rc;
  190. if (reg < value[0]) {
  191. rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value,
  192. sizeof(value));
  193. if (rc)
  194. return rc;
  195. }
  196. *secs = get_unaligned_le32(value);
  197. return 0;
  198. }
  199. static int pm8xxx_rtc_update_offset(struct pm8xxx_rtc *rtc_dd, u32 secs)
  200. {
  201. u32 raw_secs;
  202. u32 offset;
  203. int rc;
  204. if (!rtc_dd->nvmem_cell && !rtc_dd->use_uefi)
  205. return -ENODEV;
  206. rc = pm8xxx_rtc_read_raw(rtc_dd, &raw_secs);
  207. if (rc)
  208. return rc;
  209. offset = secs - raw_secs;
  210. if (offset == rtc_dd->offset)
  211. return 0;
  212. /*
  213. * Reduce flash wear by deferring updates due to clock drift until
  214. * shutdown.
  215. */
  216. if (abs_diff(offset, rtc_dd->offset) < 30) {
  217. rtc_dd->offset_dirty = true;
  218. goto out;
  219. }
  220. if (rtc_dd->nvmem_cell)
  221. rc = pm8xxx_rtc_write_nvmem_offset(rtc_dd, offset);
  222. else
  223. rc = pm8xxx_rtc_write_uefi_offset(rtc_dd, offset);
  224. if (rc)
  225. return rc;
  226. rtc_dd->offset_dirty = false;
  227. out:
  228. rtc_dd->offset = offset;
  229. return 0;
  230. }
  231. /*
  232. * Steps to write the RTC registers.
  233. * 1. Disable alarm if enabled.
  234. * 2. Disable rtc if enabled.
  235. * 3. Write 0x00 to LSB.
  236. * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
  237. * 5. Enable rtc if disabled in step 2.
  238. * 6. Enable alarm if disabled in step 1.
  239. */
  240. static int __pm8xxx_rtc_set_time(struct pm8xxx_rtc *rtc_dd, u32 secs)
  241. {
  242. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  243. u8 value[NUM_8_BIT_RTC_REGS];
  244. bool alarm_enabled;
  245. int rc;
  246. put_unaligned_le32(secs, value);
  247. rc = regmap_update_bits_check(rtc_dd->regmap, regs->alarm_ctrl,
  248. regs->alarm_en, 0, &alarm_enabled);
  249. if (rc)
  250. return rc;
  251. /* Disable RTC */
  252. rc = regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE, 0);
  253. if (rc)
  254. return rc;
  255. /* Write 0 to Byte[0] */
  256. rc = regmap_write(rtc_dd->regmap, regs->write, 0);
  257. if (rc)
  258. return rc;
  259. /* Write Byte[1], Byte[2], Byte[3] */
  260. rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
  261. &value[1], sizeof(value) - 1);
  262. if (rc)
  263. return rc;
  264. /* Write Byte[0] */
  265. rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
  266. if (rc)
  267. return rc;
  268. /* Enable RTC */
  269. rc = regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE,
  270. PM8xxx_RTC_ENABLE);
  271. if (rc)
  272. return rc;
  273. if (alarm_enabled) {
  274. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  275. regs->alarm_en, regs->alarm_en);
  276. if (rc)
  277. return rc;
  278. }
  279. return 0;
  280. }
  281. static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  282. {
  283. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  284. u32 secs;
  285. int rc;
  286. secs = rtc_tm_to_time64(tm);
  287. if (rtc_dd->allow_set_time)
  288. rc = __pm8xxx_rtc_set_time(rtc_dd, secs);
  289. else
  290. rc = pm8xxx_rtc_update_offset(rtc_dd, secs);
  291. if (rc)
  292. return rc;
  293. dev_dbg(dev, "set time: %ptRd %ptRt (%u + %u)\n", tm, tm,
  294. secs - rtc_dd->offset, rtc_dd->offset);
  295. return 0;
  296. }
  297. static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  298. {
  299. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  300. u32 secs;
  301. int rc;
  302. rc = pm8xxx_rtc_read_raw(rtc_dd, &secs);
  303. if (rc)
  304. return rc;
  305. secs += rtc_dd->offset;
  306. rtc_time64_to_tm(secs, tm);
  307. dev_dbg(dev, "read time: %ptRd %ptRt (%u + %u)\n", tm, tm,
  308. secs - rtc_dd->offset, rtc_dd->offset);
  309. return 0;
  310. }
  311. static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  312. {
  313. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  314. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  315. u8 value[NUM_8_BIT_RTC_REGS];
  316. u32 secs;
  317. int rc;
  318. secs = rtc_tm_to_time64(&alarm->time);
  319. secs -= rtc_dd->offset;
  320. put_unaligned_le32(secs, value);
  321. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  322. regs->alarm_en, 0);
  323. if (rc)
  324. return rc;
  325. rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
  326. sizeof(value));
  327. if (rc)
  328. return rc;
  329. if (alarm->enabled) {
  330. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  331. regs->alarm_en, regs->alarm_en);
  332. if (rc)
  333. return rc;
  334. }
  335. dev_dbg(dev, "set alarm: %ptRd %ptRt\n", &alarm->time, &alarm->time);
  336. return 0;
  337. }
  338. static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  339. {
  340. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  341. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  342. u8 value[NUM_8_BIT_RTC_REGS];
  343. unsigned int ctrl_reg;
  344. u32 secs;
  345. int rc;
  346. rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
  347. sizeof(value));
  348. if (rc)
  349. return rc;
  350. secs = get_unaligned_le32(value);
  351. secs += rtc_dd->offset;
  352. rtc_time64_to_tm(secs, &alarm->time);
  353. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  354. if (rc)
  355. return rc;
  356. alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
  357. dev_dbg(dev, "read alarm: %ptRd %ptRt\n", &alarm->time, &alarm->time);
  358. return 0;
  359. }
  360. static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  361. {
  362. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  363. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  364. u8 value[NUM_8_BIT_RTC_REGS] = {0};
  365. unsigned int val;
  366. int rc;
  367. if (enable)
  368. val = regs->alarm_en;
  369. else
  370. val = 0;
  371. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  372. regs->alarm_en, val);
  373. if (rc)
  374. return rc;
  375. /* Clear alarm register */
  376. if (!enable) {
  377. rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
  378. sizeof(value));
  379. if (rc)
  380. return rc;
  381. }
  382. return 0;
  383. }
  384. static const struct rtc_class_ops pm8xxx_rtc_ops = {
  385. .read_time = pm8xxx_rtc_read_time,
  386. .set_time = pm8xxx_rtc_set_time,
  387. .set_alarm = pm8xxx_rtc_set_alarm,
  388. .read_alarm = pm8xxx_rtc_read_alarm,
  389. .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
  390. };
  391. static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
  392. {
  393. struct pm8xxx_rtc *rtc_dd = dev_id;
  394. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  395. int rc;
  396. rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
  397. /* Disable alarm */
  398. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
  399. regs->alarm_en, 0);
  400. if (rc)
  401. return IRQ_NONE;
  402. /* Clear alarm status */
  403. rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl2,
  404. PM8xxx_RTC_ALARM_CLEAR, 0);
  405. if (rc)
  406. return IRQ_NONE;
  407. return IRQ_HANDLED;
  408. }
  409. static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
  410. {
  411. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  412. return regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE,
  413. PM8xxx_RTC_ENABLE);
  414. }
  415. static const struct pm8xxx_rtc_regs pm8921_regs = {
  416. .ctrl = 0x11d,
  417. .write = 0x11f,
  418. .read = 0x123,
  419. .alarm_rw = 0x127,
  420. .alarm_ctrl = 0x11d,
  421. .alarm_ctrl2 = 0x11e,
  422. .alarm_en = BIT(1),
  423. };
  424. static const struct pm8xxx_rtc_regs pm8058_regs = {
  425. .ctrl = 0x1e8,
  426. .write = 0x1ea,
  427. .read = 0x1ee,
  428. .alarm_rw = 0x1f2,
  429. .alarm_ctrl = 0x1e8,
  430. .alarm_ctrl2 = 0x1e9,
  431. .alarm_en = BIT(1),
  432. };
  433. static const struct pm8xxx_rtc_regs pm8941_regs = {
  434. .ctrl = 0x6046,
  435. .write = 0x6040,
  436. .read = 0x6048,
  437. .alarm_rw = 0x6140,
  438. .alarm_ctrl = 0x6146,
  439. .alarm_ctrl2 = 0x6148,
  440. .alarm_en = BIT(7),
  441. };
  442. static const struct pm8xxx_rtc_regs pmk8350_regs = {
  443. .ctrl = 0x6146,
  444. .write = 0x6140,
  445. .read = 0x6148,
  446. .alarm_rw = 0x6240,
  447. .alarm_ctrl = 0x6246,
  448. .alarm_ctrl2 = 0x6248,
  449. .alarm_en = BIT(7),
  450. };
  451. static const struct of_device_id pm8xxx_id_table[] = {
  452. { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
  453. { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
  454. { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
  455. { .compatible = "qcom,pmk8350-rtc", .data = &pmk8350_regs },
  456. { },
  457. };
  458. MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
  459. static int pm8xxx_rtc_probe_offset(struct pm8xxx_rtc *rtc_dd)
  460. {
  461. int rc;
  462. rtc_dd->nvmem_cell = devm_nvmem_cell_get(rtc_dd->dev, "offset");
  463. if (IS_ERR(rtc_dd->nvmem_cell)) {
  464. rc = PTR_ERR(rtc_dd->nvmem_cell);
  465. if (rc != -ENOENT)
  466. return rc;
  467. rtc_dd->nvmem_cell = NULL;
  468. } else {
  469. return pm8xxx_rtc_read_nvmem_offset(rtc_dd);
  470. }
  471. /* Use UEFI storage as fallback if available */
  472. rtc_dd->use_uefi = of_property_read_bool(rtc_dd->dev->of_node,
  473. "qcom,uefi-rtc-info");
  474. if (!rtc_dd->use_uefi)
  475. return 0;
  476. if (!efivar_is_available()) {
  477. if (IS_ENABLED(CONFIG_EFI))
  478. return -EPROBE_DEFER;
  479. dev_warn(rtc_dd->dev, "efivars not available\n");
  480. rtc_dd->use_uefi = false;
  481. }
  482. return pm8xxx_rtc_read_uefi_offset(rtc_dd);
  483. }
  484. static int pm8xxx_rtc_probe(struct platform_device *pdev)
  485. {
  486. const struct of_device_id *match;
  487. struct pm8xxx_rtc *rtc_dd;
  488. int rc;
  489. match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
  490. if (!match)
  491. return -ENXIO;
  492. rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
  493. if (rtc_dd == NULL)
  494. return -ENOMEM;
  495. rtc_dd->regs = match->data;
  496. rtc_dd->dev = &pdev->dev;
  497. rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  498. if (!rtc_dd->regmap)
  499. return -ENXIO;
  500. if (!of_property_read_bool(pdev->dev.of_node, "qcom,no-alarm")) {
  501. rtc_dd->alarm_irq = platform_get_irq(pdev, 0);
  502. if (rtc_dd->alarm_irq < 0)
  503. return -ENXIO;
  504. }
  505. rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
  506. "allow-set-time");
  507. if (!rtc_dd->allow_set_time) {
  508. rc = pm8xxx_rtc_probe_offset(rtc_dd);
  509. if (rc)
  510. return rc;
  511. }
  512. rc = pm8xxx_rtc_enable(rtc_dd);
  513. if (rc)
  514. return rc;
  515. platform_set_drvdata(pdev, rtc_dd);
  516. rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
  517. if (IS_ERR(rtc_dd->rtc))
  518. return PTR_ERR(rtc_dd->rtc);
  519. rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
  520. rtc_dd->rtc->range_max = U32_MAX;
  521. if (rtc_dd->alarm_irq) {
  522. rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->alarm_irq,
  523. pm8xxx_alarm_trigger,
  524. IRQF_TRIGGER_RISING,
  525. "pm8xxx_rtc_alarm", rtc_dd);
  526. if (rc < 0)
  527. return rc;
  528. rc = devm_pm_set_wake_irq(&pdev->dev, rtc_dd->alarm_irq);
  529. if (rc)
  530. return rc;
  531. devm_device_init_wakeup(&pdev->dev);
  532. } else {
  533. clear_bit(RTC_FEATURE_ALARM, rtc_dd->rtc->features);
  534. }
  535. return devm_rtc_register_device(rtc_dd->rtc);
  536. }
  537. static void pm8xxx_shutdown(struct platform_device *pdev)
  538. {
  539. struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev);
  540. if (rtc_dd->offset_dirty) {
  541. if (rtc_dd->nvmem_cell)
  542. pm8xxx_rtc_write_nvmem_offset(rtc_dd, rtc_dd->offset);
  543. else
  544. pm8xxx_rtc_write_uefi_offset(rtc_dd, rtc_dd->offset);
  545. }
  546. }
  547. static struct platform_driver pm8xxx_rtc_driver = {
  548. .probe = pm8xxx_rtc_probe,
  549. .shutdown = pm8xxx_shutdown,
  550. .driver = {
  551. .name = "rtc-pm8xxx",
  552. .of_match_table = pm8xxx_id_table,
  553. },
  554. };
  555. module_platform_driver(pm8xxx_rtc_driver);
  556. MODULE_DESCRIPTION("PMIC8xxx RTC driver");
  557. MODULE_LICENSE("GPL v2");
  558. MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");
  559. MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");