rtc-da9055.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Real time clock driver for DA9055
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/rtc.h>
  17. #include <linux/mfd/da9055/core.h>
  18. #include <linux/mfd/da9055/reg.h>
  19. #include <linux/mfd/da9055/pdata.h>
  20. struct da9055_rtc {
  21. struct rtc_device *rtc;
  22. struct da9055 *da9055;
  23. int alarm_enable;
  24. };
  25. static int da9055_rtc_enable_alarm(struct da9055_rtc *rtc, bool enable)
  26. {
  27. int ret;
  28. if (enable) {
  29. ret = da9055_reg_update(rtc->da9055, DA9055_REG_ALARM_Y,
  30. DA9055_RTC_ALM_EN,
  31. DA9055_RTC_ALM_EN);
  32. if (ret != 0)
  33. dev_err(rtc->da9055->dev, "Failed to enable ALM: %d\n",
  34. ret);
  35. rtc->alarm_enable = 1;
  36. } else {
  37. ret = da9055_reg_update(rtc->da9055, DA9055_REG_ALARM_Y,
  38. DA9055_RTC_ALM_EN, 0);
  39. if (ret != 0)
  40. dev_err(rtc->da9055->dev,
  41. "Failed to disable ALM: %d\n", ret);
  42. rtc->alarm_enable = 0;
  43. }
  44. return ret;
  45. }
  46. static irqreturn_t da9055_rtc_alm_irq(int irq, void *data)
  47. {
  48. struct da9055_rtc *rtc = data;
  49. da9055_rtc_enable_alarm(rtc, 0);
  50. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  51. return IRQ_HANDLED;
  52. }
  53. static int da9055_read_alarm(struct da9055 *da9055, struct rtc_time *rtc_tm)
  54. {
  55. int ret;
  56. uint8_t v[5];
  57. ret = da9055_group_read(da9055, DA9055_REG_ALARM_MI, 5, v);
  58. if (ret != 0) {
  59. dev_err(da9055->dev, "Failed to group read ALM: %d\n", ret);
  60. return ret;
  61. }
  62. rtc_tm->tm_year = (v[4] & DA9055_RTC_ALM_YEAR) + 100;
  63. rtc_tm->tm_mon = (v[3] & DA9055_RTC_ALM_MONTH) - 1;
  64. rtc_tm->tm_mday = v[2] & DA9055_RTC_ALM_DAY;
  65. rtc_tm->tm_hour = v[1] & DA9055_RTC_ALM_HOUR;
  66. rtc_tm->tm_min = v[0] & DA9055_RTC_ALM_MIN;
  67. rtc_tm->tm_sec = 0;
  68. return rtc_valid_tm(rtc_tm);
  69. }
  70. static int da9055_set_alarm(struct da9055 *da9055, struct rtc_time *rtc_tm)
  71. {
  72. int ret;
  73. uint8_t v[2];
  74. rtc_tm->tm_year -= 100;
  75. rtc_tm->tm_mon += 1;
  76. ret = da9055_reg_update(da9055, DA9055_REG_ALARM_MI,
  77. DA9055_RTC_ALM_MIN, rtc_tm->tm_min);
  78. if (ret != 0) {
  79. dev_err(da9055->dev, "Failed to write ALRM MIN: %d\n", ret);
  80. return ret;
  81. }
  82. v[0] = rtc_tm->tm_hour;
  83. v[1] = rtc_tm->tm_mday;
  84. ret = da9055_group_write(da9055, DA9055_REG_ALARM_H, 2, v);
  85. if (ret < 0)
  86. return ret;
  87. ret = da9055_reg_update(da9055, DA9055_REG_ALARM_MO,
  88. DA9055_RTC_ALM_MONTH, rtc_tm->tm_mon);
  89. if (ret < 0)
  90. dev_err(da9055->dev, "Failed to write ALM Month:%d\n", ret);
  91. ret = da9055_reg_update(da9055, DA9055_REG_ALARM_Y,
  92. DA9055_RTC_ALM_YEAR, rtc_tm->tm_year);
  93. if (ret < 0)
  94. dev_err(da9055->dev, "Failed to write ALM Year:%d\n", ret);
  95. return ret;
  96. }
  97. static int da9055_rtc_get_alarm_status(struct da9055 *da9055)
  98. {
  99. int ret;
  100. ret = da9055_reg_read(da9055, DA9055_REG_ALARM_Y);
  101. if (ret < 0) {
  102. dev_err(da9055->dev, "Failed to read ALM: %d\n", ret);
  103. return ret;
  104. }
  105. ret &= DA9055_RTC_ALM_EN;
  106. return (ret > 0) ? 1 : 0;
  107. }
  108. static int da9055_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
  109. {
  110. struct da9055_rtc *rtc = dev_get_drvdata(dev);
  111. uint8_t v[6];
  112. int ret;
  113. ret = da9055_reg_read(rtc->da9055, DA9055_REG_COUNT_S);
  114. if (ret < 0)
  115. return ret;
  116. /*
  117. * Registers are only valid when RTC_READ
  118. * status bit is asserted
  119. */
  120. if (!(ret & DA9055_RTC_READ))
  121. return -EBUSY;
  122. ret = da9055_group_read(rtc->da9055, DA9055_REG_COUNT_S, 6, v);
  123. if (ret < 0) {
  124. dev_err(rtc->da9055->dev, "Failed to read RTC time : %d\n",
  125. ret);
  126. return ret;
  127. }
  128. rtc_tm->tm_year = (v[5] & DA9055_RTC_YEAR) + 100;
  129. rtc_tm->tm_mon = (v[4] & DA9055_RTC_MONTH) - 1;
  130. rtc_tm->tm_mday = v[3] & DA9055_RTC_DAY;
  131. rtc_tm->tm_hour = v[2] & DA9055_RTC_HOUR;
  132. rtc_tm->tm_min = v[1] & DA9055_RTC_MIN;
  133. rtc_tm->tm_sec = v[0] & DA9055_RTC_SEC;
  134. return rtc_valid_tm(rtc_tm);
  135. }
  136. static int da9055_rtc_set_time(struct device *dev, struct rtc_time *tm)
  137. {
  138. struct da9055_rtc *rtc;
  139. uint8_t v[6];
  140. rtc = dev_get_drvdata(dev);
  141. v[0] = tm->tm_sec;
  142. v[1] = tm->tm_min;
  143. v[2] = tm->tm_hour;
  144. v[3] = tm->tm_mday;
  145. v[4] = tm->tm_mon + 1;
  146. v[5] = tm->tm_year - 100;
  147. return da9055_group_write(rtc->da9055, DA9055_REG_COUNT_S, 6, v);
  148. }
  149. static int da9055_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  150. {
  151. int ret;
  152. struct rtc_time *tm = &alrm->time;
  153. struct da9055_rtc *rtc = dev_get_drvdata(dev);
  154. ret = da9055_read_alarm(rtc->da9055, tm);
  155. if (ret)
  156. return ret;
  157. alrm->enabled = da9055_rtc_get_alarm_status(rtc->da9055);
  158. return 0;
  159. }
  160. static int da9055_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  161. {
  162. int ret;
  163. struct rtc_time *tm = &alrm->time;
  164. struct da9055_rtc *rtc = dev_get_drvdata(dev);
  165. ret = da9055_rtc_enable_alarm(rtc, 0);
  166. if (ret < 0)
  167. return ret;
  168. ret = da9055_set_alarm(rtc->da9055, tm);
  169. if (ret)
  170. return ret;
  171. ret = da9055_rtc_enable_alarm(rtc, 1);
  172. return ret;
  173. }
  174. static int da9055_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  175. {
  176. struct da9055_rtc *rtc = dev_get_drvdata(dev);
  177. return da9055_rtc_enable_alarm(rtc, enabled);
  178. }
  179. static const struct rtc_class_ops da9055_rtc_ops = {
  180. .read_time = da9055_rtc_read_time,
  181. .set_time = da9055_rtc_set_time,
  182. .read_alarm = da9055_rtc_read_alarm,
  183. .set_alarm = da9055_rtc_set_alarm,
  184. .alarm_irq_enable = da9055_rtc_alarm_irq_enable,
  185. };
  186. static int da9055_rtc_device_init(struct da9055 *da9055,
  187. struct da9055_pdata *pdata)
  188. {
  189. int ret;
  190. /* Enable RTC and the internal Crystal */
  191. ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
  192. DA9055_RTC_EN, DA9055_RTC_EN);
  193. if (ret < 0)
  194. return ret;
  195. ret = da9055_reg_update(da9055, DA9055_REG_EN_32K,
  196. DA9055_CRYSTAL_EN, DA9055_CRYSTAL_EN);
  197. if (ret < 0)
  198. return ret;
  199. /* Enable RTC in Power Down mode */
  200. ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
  201. DA9055_RTC_MODE_PD, DA9055_RTC_MODE_PD);
  202. if (ret < 0)
  203. return ret;
  204. /* Enable RTC in Reset mode */
  205. if (pdata && pdata->reset_enable) {
  206. ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
  207. DA9055_RTC_MODE_SD,
  208. DA9055_RTC_MODE_SD <<
  209. DA9055_RTC_MODE_SD_SHIFT);
  210. if (ret < 0)
  211. return ret;
  212. }
  213. /* Disable the RTC TICK ALM */
  214. ret = da9055_reg_update(da9055, DA9055_REG_ALARM_MO,
  215. DA9055_RTC_TICK_WAKE_MASK, 0);
  216. if (ret < 0)
  217. return ret;
  218. return 0;
  219. }
  220. static int da9055_rtc_probe(struct platform_device *pdev)
  221. {
  222. struct da9055_rtc *rtc;
  223. struct da9055_pdata *pdata = NULL;
  224. int ret, alm_irq;
  225. rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9055_rtc), GFP_KERNEL);
  226. if (!rtc)
  227. return -ENOMEM;
  228. rtc->da9055 = dev_get_drvdata(pdev->dev.parent);
  229. pdata = dev_get_platdata(rtc->da9055->dev);
  230. platform_set_drvdata(pdev, rtc);
  231. ret = da9055_rtc_device_init(rtc->da9055, pdata);
  232. if (ret < 0)
  233. goto err_rtc;
  234. ret = da9055_reg_read(rtc->da9055, DA9055_REG_ALARM_Y);
  235. if (ret < 0)
  236. goto err_rtc;
  237. if (ret & DA9055_RTC_ALM_EN)
  238. rtc->alarm_enable = 1;
  239. device_init_wakeup(&pdev->dev, 1);
  240. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  241. &da9055_rtc_ops, THIS_MODULE);
  242. if (IS_ERR(rtc->rtc)) {
  243. ret = PTR_ERR(rtc->rtc);
  244. goto err_rtc;
  245. }
  246. alm_irq = platform_get_irq_byname(pdev, "ALM");
  247. if (alm_irq < 0)
  248. return alm_irq;
  249. ret = devm_request_threaded_irq(&pdev->dev, alm_irq, NULL,
  250. da9055_rtc_alm_irq,
  251. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  252. "ALM", rtc);
  253. if (ret != 0)
  254. dev_err(rtc->da9055->dev, "irq registration failed: %d\n", ret);
  255. err_rtc:
  256. return ret;
  257. }
  258. #ifdef CONFIG_PM
  259. /* Turn off the alarm if it should not be a wake source. */
  260. static int da9055_rtc_suspend(struct device *dev)
  261. {
  262. struct platform_device *pdev = to_platform_device(dev);
  263. struct da9055_rtc *rtc = dev_get_drvdata(&pdev->dev);
  264. int ret;
  265. if (!device_may_wakeup(&pdev->dev)) {
  266. /* Disable the ALM IRQ */
  267. ret = da9055_rtc_enable_alarm(rtc, 0);
  268. if (ret < 0)
  269. dev_err(&pdev->dev, "Failed to disable RTC ALM\n");
  270. }
  271. return 0;
  272. }
  273. /* Enable the alarm if it should be enabled (in case it was disabled to
  274. * prevent use as a wake source).
  275. */
  276. static int da9055_rtc_resume(struct device *dev)
  277. {
  278. struct platform_device *pdev = to_platform_device(dev);
  279. struct da9055_rtc *rtc = dev_get_drvdata(&pdev->dev);
  280. int ret;
  281. if (!device_may_wakeup(&pdev->dev)) {
  282. if (rtc->alarm_enable) {
  283. ret = da9055_rtc_enable_alarm(rtc, 1);
  284. if (ret < 0)
  285. dev_err(&pdev->dev,
  286. "Failed to restart RTC ALM\n");
  287. }
  288. }
  289. return 0;
  290. }
  291. /* Unconditionally disable the alarm */
  292. static int da9055_rtc_freeze(struct device *dev)
  293. {
  294. struct platform_device *pdev = to_platform_device(dev);
  295. struct da9055_rtc *rtc = dev_get_drvdata(&pdev->dev);
  296. int ret;
  297. ret = da9055_rtc_enable_alarm(rtc, 0);
  298. if (ret < 0)
  299. dev_err(&pdev->dev, "Failed to freeze RTC ALMs\n");
  300. return 0;
  301. }
  302. #else
  303. #define da9055_rtc_suspend NULL
  304. #define da9055_rtc_resume NULL
  305. #define da9055_rtc_freeze NULL
  306. #endif
  307. static const struct dev_pm_ops da9055_rtc_pm_ops = {
  308. .suspend = da9055_rtc_suspend,
  309. .resume = da9055_rtc_resume,
  310. .freeze = da9055_rtc_freeze,
  311. .thaw = da9055_rtc_resume,
  312. .restore = da9055_rtc_resume,
  313. .poweroff = da9055_rtc_suspend,
  314. };
  315. static struct platform_driver da9055_rtc_driver = {
  316. .probe = da9055_rtc_probe,
  317. .driver = {
  318. .name = "da9055-rtc",
  319. .pm = &da9055_rtc_pm_ops,
  320. },
  321. };
  322. module_platform_driver(da9055_rtc_driver);
  323. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  324. MODULE_DESCRIPTION("RTC driver for Dialog DA9055 PMIC");
  325. MODULE_LICENSE("GPL");
  326. MODULE_ALIAS("platform:da9055-rtc");