rtc-pm8xxx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/of.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/rtc.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. /* RTC Register offsets from RTC CTRL REG */
  22. #define PM8XXX_ALARM_CTRL_OFFSET 0x01
  23. #define PM8XXX_RTC_WRITE_OFFSET 0x02
  24. #define PM8XXX_RTC_READ_OFFSET 0x06
  25. #define PM8XXX_ALARM_RW_OFFSET 0x0A
  26. /* RTC_CTRL register bit fields */
  27. #define PM8xxx_RTC_ENABLE BIT(7)
  28. #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
  29. #define NUM_8_BIT_RTC_REGS 0x4
  30. /**
  31. * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
  32. * @ctrl: base address of control register
  33. * @write: base address of write register
  34. * @read: base address of read register
  35. * @alarm_ctrl: base address of alarm control register
  36. * @alarm_ctrl2: base address of alarm control2 register
  37. * @alarm_rw: base address of alarm read-write register
  38. * @alarm_en: alarm enable mask
  39. */
  40. struct pm8xxx_rtc_regs {
  41. unsigned int ctrl;
  42. unsigned int write;
  43. unsigned int read;
  44. unsigned int alarm_ctrl;
  45. unsigned int alarm_ctrl2;
  46. unsigned int alarm_rw;
  47. unsigned int alarm_en;
  48. };
  49. /**
  50. * struct pm8xxx_rtc - rtc driver internal structure
  51. * @rtc: rtc device for this driver.
  52. * @regmap: regmap used to access RTC registers
  53. * @allow_set_time: indicates whether writing to the RTC is allowed
  54. * @rtc_alarm_irq: rtc alarm irq number.
  55. * @ctrl_reg: rtc control register.
  56. * @rtc_dev: device structure.
  57. * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
  58. */
  59. struct pm8xxx_rtc {
  60. struct rtc_device *rtc;
  61. struct regmap *regmap;
  62. bool allow_set_time;
  63. int rtc_alarm_irq;
  64. const struct pm8xxx_rtc_regs *regs;
  65. struct device *rtc_dev;
  66. spinlock_t ctrl_reg_lock;
  67. };
  68. /*
  69. * Steps to write the RTC registers.
  70. * 1. Disable alarm if enabled.
  71. * 2. Write 0x00 to LSB.
  72. * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0].
  73. * 4. Enable alarm if disabled in step 1.
  74. */
  75. static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  76. {
  77. int rc, i;
  78. unsigned long secs, irq_flags;
  79. u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0;
  80. unsigned int ctrl_reg;
  81. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  82. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  83. if (!rtc_dd->allow_set_time)
  84. return -EACCES;
  85. rtc_tm_to_time(tm, &secs);
  86. for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
  87. value[i] = secs & 0xFF;
  88. secs >>= 8;
  89. }
  90. dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
  91. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  92. rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
  93. if (rc)
  94. goto rtc_rw_fail;
  95. if (ctrl_reg & regs->alarm_en) {
  96. alarm_enabled = 1;
  97. ctrl_reg &= ~regs->alarm_en;
  98. rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
  99. if (rc) {
  100. dev_err(dev, "Write to RTC control register failed\n");
  101. goto rtc_rw_fail;
  102. }
  103. }
  104. /* Write 0 to Byte[0] */
  105. rc = regmap_write(rtc_dd->regmap, regs->write, 0);
  106. if (rc) {
  107. dev_err(dev, "Write to RTC write data register failed\n");
  108. goto rtc_rw_fail;
  109. }
  110. /* Write Byte[1], Byte[2], Byte[3] */
  111. rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
  112. &value[1], sizeof(value) - 1);
  113. if (rc) {
  114. dev_err(dev, "Write to RTC write data register failed\n");
  115. goto rtc_rw_fail;
  116. }
  117. /* Write Byte[0] */
  118. rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
  119. if (rc) {
  120. dev_err(dev, "Write to RTC write data register failed\n");
  121. goto rtc_rw_fail;
  122. }
  123. if (alarm_enabled) {
  124. ctrl_reg |= regs->alarm_en;
  125. rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
  126. if (rc) {
  127. dev_err(dev, "Write to RTC control register failed\n");
  128. goto rtc_rw_fail;
  129. }
  130. }
  131. rtc_rw_fail:
  132. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  133. return rc;
  134. }
  135. static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  136. {
  137. int rc;
  138. u8 value[NUM_8_BIT_RTC_REGS];
  139. unsigned long secs;
  140. unsigned int reg;
  141. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  142. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  143. rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
  144. if (rc) {
  145. dev_err(dev, "RTC read data register failed\n");
  146. return rc;
  147. }
  148. /*
  149. * Read the LSB again and check if there has been a carry over.
  150. * If there is, redo the read operation.
  151. */
  152. rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
  153. if (rc < 0) {
  154. dev_err(dev, "RTC read data register failed\n");
  155. return rc;
  156. }
  157. if (unlikely(reg < value[0])) {
  158. rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
  159. value, sizeof(value));
  160. if (rc) {
  161. dev_err(dev, "RTC read data register failed\n");
  162. return rc;
  163. }
  164. }
  165. secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
  166. rtc_time_to_tm(secs, tm);
  167. rc = rtc_valid_tm(tm);
  168. if (rc < 0) {
  169. dev_err(dev, "Invalid time read from RTC\n");
  170. return rc;
  171. }
  172. dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
  173. secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
  174. tm->tm_mday, tm->tm_mon, tm->tm_year);
  175. return 0;
  176. }
  177. static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  178. {
  179. int rc, i;
  180. u8 value[NUM_8_BIT_RTC_REGS];
  181. unsigned int ctrl_reg;
  182. unsigned long secs, irq_flags;
  183. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  184. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  185. rtc_tm_to_time(&alarm->time, &secs);
  186. for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
  187. value[i] = secs & 0xFF;
  188. secs >>= 8;
  189. }
  190. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  191. rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
  192. sizeof(value));
  193. if (rc) {
  194. dev_err(dev, "Write to RTC ALARM register failed\n");
  195. goto rtc_rw_fail;
  196. }
  197. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  198. if (rc)
  199. goto rtc_rw_fail;
  200. if (alarm->enabled)
  201. ctrl_reg |= regs->alarm_en;
  202. else
  203. ctrl_reg &= ~regs->alarm_en;
  204. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  205. if (rc) {
  206. dev_err(dev, "Write to RTC alarm control register failed\n");
  207. goto rtc_rw_fail;
  208. }
  209. dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
  210. alarm->time.tm_hour, alarm->time.tm_min,
  211. alarm->time.tm_sec, alarm->time.tm_mday,
  212. alarm->time.tm_mon, alarm->time.tm_year);
  213. rtc_rw_fail:
  214. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  215. return rc;
  216. }
  217. static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  218. {
  219. int rc;
  220. u8 value[NUM_8_BIT_RTC_REGS];
  221. unsigned long secs;
  222. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  223. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  224. rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
  225. sizeof(value));
  226. if (rc) {
  227. dev_err(dev, "RTC alarm time read failed\n");
  228. return rc;
  229. }
  230. secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
  231. rtc_time_to_tm(secs, &alarm->time);
  232. rc = rtc_valid_tm(&alarm->time);
  233. if (rc < 0) {
  234. dev_err(dev, "Invalid alarm time read from RTC\n");
  235. return rc;
  236. }
  237. dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
  238. alarm->time.tm_hour, alarm->time.tm_min,
  239. alarm->time.tm_sec, alarm->time.tm_mday,
  240. alarm->time.tm_mon, alarm->time.tm_year);
  241. return 0;
  242. }
  243. static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  244. {
  245. int rc;
  246. unsigned long irq_flags;
  247. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  248. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  249. unsigned int ctrl_reg;
  250. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  251. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  252. if (rc)
  253. goto rtc_rw_fail;
  254. if (enable)
  255. ctrl_reg |= regs->alarm_en;
  256. else
  257. ctrl_reg &= ~regs->alarm_en;
  258. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  259. if (rc) {
  260. dev_err(dev, "Write to RTC control register failed\n");
  261. goto rtc_rw_fail;
  262. }
  263. rtc_rw_fail:
  264. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  265. return rc;
  266. }
  267. static const struct rtc_class_ops pm8xxx_rtc_ops = {
  268. .read_time = pm8xxx_rtc_read_time,
  269. .set_time = pm8xxx_rtc_set_time,
  270. .set_alarm = pm8xxx_rtc_set_alarm,
  271. .read_alarm = pm8xxx_rtc_read_alarm,
  272. .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
  273. };
  274. static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
  275. {
  276. struct pm8xxx_rtc *rtc_dd = dev_id;
  277. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  278. unsigned int ctrl_reg;
  279. int rc;
  280. unsigned long irq_flags;
  281. rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
  282. spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
  283. /* Clear the alarm enable bit */
  284. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
  285. if (rc) {
  286. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  287. goto rtc_alarm_handled;
  288. }
  289. ctrl_reg &= ~regs->alarm_en;
  290. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
  291. if (rc) {
  292. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  293. dev_err(rtc_dd->rtc_dev,
  294. "Write to alarm control register failed\n");
  295. goto rtc_alarm_handled;
  296. }
  297. spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
  298. /* Clear RTC alarm register */
  299. rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
  300. if (rc) {
  301. dev_err(rtc_dd->rtc_dev,
  302. "RTC Alarm control2 register read failed\n");
  303. goto rtc_alarm_handled;
  304. }
  305. ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
  306. rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
  307. if (rc)
  308. dev_err(rtc_dd->rtc_dev,
  309. "Write to RTC Alarm control2 register failed\n");
  310. rtc_alarm_handled:
  311. return IRQ_HANDLED;
  312. }
  313. static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
  314. {
  315. const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
  316. unsigned int ctrl_reg;
  317. int rc;
  318. /* Check if the RTC is on, else turn it on */
  319. rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
  320. if (rc)
  321. return rc;
  322. if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
  323. ctrl_reg |= PM8xxx_RTC_ENABLE;
  324. rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
  325. if (rc)
  326. return rc;
  327. }
  328. return 0;
  329. }
  330. static const struct pm8xxx_rtc_regs pm8921_regs = {
  331. .ctrl = 0x11d,
  332. .write = 0x11f,
  333. .read = 0x123,
  334. .alarm_rw = 0x127,
  335. .alarm_ctrl = 0x11d,
  336. .alarm_ctrl2 = 0x11e,
  337. .alarm_en = BIT(1),
  338. };
  339. static const struct pm8xxx_rtc_regs pm8058_regs = {
  340. .ctrl = 0x1e8,
  341. .write = 0x1ea,
  342. .read = 0x1ee,
  343. .alarm_rw = 0x1f2,
  344. .alarm_ctrl = 0x1e8,
  345. .alarm_ctrl2 = 0x1e9,
  346. .alarm_en = BIT(1),
  347. };
  348. static const struct pm8xxx_rtc_regs pm8941_regs = {
  349. .ctrl = 0x6046,
  350. .write = 0x6040,
  351. .read = 0x6048,
  352. .alarm_rw = 0x6140,
  353. .alarm_ctrl = 0x6146,
  354. .alarm_ctrl2 = 0x6148,
  355. .alarm_en = BIT(7),
  356. };
  357. /*
  358. * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
  359. */
  360. static const struct of_device_id pm8xxx_id_table[] = {
  361. { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
  362. { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
  363. { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
  364. { },
  365. };
  366. MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
  367. static int pm8xxx_rtc_probe(struct platform_device *pdev)
  368. {
  369. int rc;
  370. struct pm8xxx_rtc *rtc_dd;
  371. const struct of_device_id *match;
  372. match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
  373. if (!match)
  374. return -ENXIO;
  375. rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
  376. if (rtc_dd == NULL)
  377. return -ENOMEM;
  378. /* Initialise spinlock to protect RTC control register */
  379. spin_lock_init(&rtc_dd->ctrl_reg_lock);
  380. rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  381. if (!rtc_dd->regmap) {
  382. dev_err(&pdev->dev, "Parent regmap unavailable.\n");
  383. return -ENXIO;
  384. }
  385. rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
  386. if (rtc_dd->rtc_alarm_irq < 0) {
  387. dev_err(&pdev->dev, "Alarm IRQ resource absent!\n");
  388. return -ENXIO;
  389. }
  390. rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
  391. "allow-set-time");
  392. rtc_dd->regs = match->data;
  393. rtc_dd->rtc_dev = &pdev->dev;
  394. rc = pm8xxx_rtc_enable(rtc_dd);
  395. if (rc)
  396. return rc;
  397. platform_set_drvdata(pdev, rtc_dd);
  398. device_init_wakeup(&pdev->dev, 1);
  399. /* Register the RTC device */
  400. rtc_dd->rtc = devm_rtc_device_register(&pdev->dev, "pm8xxx_rtc",
  401. &pm8xxx_rtc_ops, THIS_MODULE);
  402. if (IS_ERR(rtc_dd->rtc)) {
  403. dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
  404. __func__, PTR_ERR(rtc_dd->rtc));
  405. return PTR_ERR(rtc_dd->rtc);
  406. }
  407. /* Request the alarm IRQ */
  408. rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
  409. pm8xxx_alarm_trigger,
  410. IRQF_TRIGGER_RISING,
  411. "pm8xxx_rtc_alarm", rtc_dd);
  412. if (rc < 0) {
  413. dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
  414. return rc;
  415. }
  416. dev_dbg(&pdev->dev, "Probe success !!\n");
  417. return 0;
  418. }
  419. #ifdef CONFIG_PM_SLEEP
  420. static int pm8xxx_rtc_resume(struct device *dev)
  421. {
  422. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  423. if (device_may_wakeup(dev))
  424. disable_irq_wake(rtc_dd->rtc_alarm_irq);
  425. return 0;
  426. }
  427. static int pm8xxx_rtc_suspend(struct device *dev)
  428. {
  429. struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
  430. if (device_may_wakeup(dev))
  431. enable_irq_wake(rtc_dd->rtc_alarm_irq);
  432. return 0;
  433. }
  434. #endif
  435. static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops,
  436. pm8xxx_rtc_suspend,
  437. pm8xxx_rtc_resume);
  438. static struct platform_driver pm8xxx_rtc_driver = {
  439. .probe = pm8xxx_rtc_probe,
  440. .driver = {
  441. .name = "rtc-pm8xxx",
  442. .pm = &pm8xxx_rtc_pm_ops,
  443. .of_match_table = pm8xxx_id_table,
  444. },
  445. };
  446. module_platform_driver(pm8xxx_rtc_driver);
  447. MODULE_ALIAS("platform:rtc-pm8xxx");
  448. MODULE_DESCRIPTION("PMIC8xxx RTC driver");
  449. MODULE_LICENSE("GPL v2");
  450. MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");