rtc-da9055.c 9.3 KB

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