rtc-da9052.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Real time clock driver for DA9052
  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/err.h>
  18. #include <linux/delay.h>
  19. #include <linux/mfd/da9052/da9052.h>
  20. #include <linux/mfd/da9052/reg.h>
  21. #define rtc_err(rtc, fmt, ...) \
  22. dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
  23. #define DA9052_GET_TIME_RETRIES 5
  24. struct da9052_rtc {
  25. struct rtc_device *rtc;
  26. struct da9052 *da9052;
  27. };
  28. static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
  29. {
  30. int ret;
  31. if (enable) {
  32. ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
  33. DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON,
  34. DA9052_ALARM_Y_ALARM_ON);
  35. if (ret != 0)
  36. rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
  37. } else {
  38. ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
  39. DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
  40. if (ret != 0)
  41. rtc_err(rtc, "Write error: %d\n", ret);
  42. }
  43. return ret;
  44. }
  45. static irqreturn_t da9052_rtc_irq(int irq, void *data)
  46. {
  47. struct da9052_rtc *rtc = data;
  48. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  49. return IRQ_HANDLED;
  50. }
  51. static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
  52. {
  53. int ret;
  54. uint8_t v[2][5];
  55. int idx = 1;
  56. int timeout = DA9052_GET_TIME_RETRIES;
  57. ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]);
  58. if (ret) {
  59. rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
  60. return ret;
  61. }
  62. do {
  63. ret = da9052_group_read(rtc->da9052,
  64. DA9052_ALARM_MI_REG, 5, &v[idx][0]);
  65. if (ret) {
  66. rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
  67. return ret;
  68. }
  69. if (memcmp(&v[0][0], &v[1][0], 5) == 0) {
  70. rtc_tm->tm_year = (v[0][4] & DA9052_RTC_YEAR) + 100;
  71. rtc_tm->tm_mon = (v[0][3] & DA9052_RTC_MONTH) - 1;
  72. rtc_tm->tm_mday = v[0][2] & DA9052_RTC_DAY;
  73. rtc_tm->tm_hour = v[0][1] & DA9052_RTC_HOUR;
  74. rtc_tm->tm_min = v[0][0] & DA9052_RTC_MIN;
  75. rtc_tm->tm_sec = 0;
  76. ret = rtc_valid_tm(rtc_tm);
  77. return ret;
  78. }
  79. idx = (1-idx);
  80. msleep(20);
  81. } while (timeout--);
  82. rtc_err(rtc, "Timed out reading alarm time\n");
  83. return -EIO;
  84. }
  85. static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
  86. {
  87. struct da9052 *da9052 = rtc->da9052;
  88. unsigned long alm_time;
  89. int ret;
  90. uint8_t v[3];
  91. ret = rtc_tm_to_time(rtc_tm, &alm_time);
  92. if (ret != 0)
  93. return ret;
  94. if (rtc_tm->tm_sec > 0) {
  95. alm_time += 60 - rtc_tm->tm_sec;
  96. rtc_time_to_tm(alm_time, rtc_tm);
  97. }
  98. BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
  99. rtc_tm->tm_year -= 100;
  100. rtc_tm->tm_mon += 1;
  101. ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
  102. DA9052_RTC_MIN, rtc_tm->tm_min);
  103. if (ret != 0) {
  104. rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
  105. return ret;
  106. }
  107. v[0] = rtc_tm->tm_hour;
  108. v[1] = rtc_tm->tm_mday;
  109. v[2] = rtc_tm->tm_mon;
  110. ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
  111. if (ret < 0)
  112. return ret;
  113. ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
  114. DA9052_RTC_YEAR, rtc_tm->tm_year);
  115. if (ret != 0)
  116. rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
  117. return ret;
  118. }
  119. static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
  120. {
  121. int ret;
  122. ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
  123. if (ret < 0) {
  124. rtc_err(rtc, "Failed to read ALM: %d\n", ret);
  125. return ret;
  126. }
  127. return !!(ret&DA9052_ALARM_Y_ALARM_ON);
  128. }
  129. static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
  130. {
  131. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  132. int ret;
  133. uint8_t v[2][6];
  134. int idx = 1;
  135. int timeout = DA9052_GET_TIME_RETRIES;
  136. ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]);
  137. if (ret) {
  138. rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
  139. return ret;
  140. }
  141. do {
  142. ret = da9052_group_read(rtc->da9052,
  143. DA9052_COUNT_S_REG, 6, &v[idx][0]);
  144. if (ret) {
  145. rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
  146. return ret;
  147. }
  148. if (memcmp(&v[0][0], &v[1][0], 6) == 0) {
  149. rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100;
  150. rtc_tm->tm_mon = (v[0][4] & DA9052_RTC_MONTH) - 1;
  151. rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY;
  152. rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR;
  153. rtc_tm->tm_min = v[0][1] & DA9052_RTC_MIN;
  154. rtc_tm->tm_sec = v[0][0] & DA9052_RTC_SEC;
  155. ret = rtc_valid_tm(rtc_tm);
  156. return ret;
  157. }
  158. idx = (1-idx);
  159. msleep(20);
  160. } while (timeout--);
  161. rtc_err(rtc, "Timed out reading time\n");
  162. return -EIO;
  163. }
  164. static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
  165. {
  166. struct da9052_rtc *rtc;
  167. uint8_t v[6];
  168. int ret;
  169. /* DA9052 only has 6 bits for year - to represent 2000-2063 */
  170. if ((tm->tm_year < 100) || (tm->tm_year > 163))
  171. return -EINVAL;
  172. rtc = dev_get_drvdata(dev);
  173. v[0] = tm->tm_sec;
  174. v[1] = tm->tm_min;
  175. v[2] = tm->tm_hour;
  176. v[3] = tm->tm_mday;
  177. v[4] = tm->tm_mon + 1;
  178. v[5] = tm->tm_year - 100;
  179. ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
  180. if (ret < 0)
  181. rtc_err(rtc, "failed to set RTC time: %d\n", ret);
  182. return ret;
  183. }
  184. static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  185. {
  186. int ret;
  187. struct rtc_time *tm = &alrm->time;
  188. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  189. ret = da9052_read_alarm(rtc, tm);
  190. if (ret < 0) {
  191. rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
  192. return ret;
  193. }
  194. alrm->enabled = da9052_rtc_get_alarm_status(rtc);
  195. return 0;
  196. }
  197. static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  198. {
  199. int ret;
  200. struct rtc_time *tm = &alrm->time;
  201. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  202. /* DA9052 only has 6 bits for year - to represent 2000-2063 */
  203. if ((tm->tm_year < 100) || (tm->tm_year > 163))
  204. return -EINVAL;
  205. ret = da9052_rtc_enable_alarm(rtc, 0);
  206. if (ret < 0)
  207. return ret;
  208. ret = da9052_set_alarm(rtc, tm);
  209. if (ret < 0)
  210. return ret;
  211. ret = da9052_rtc_enable_alarm(rtc, 1);
  212. return ret;
  213. }
  214. static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  215. {
  216. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  217. return da9052_rtc_enable_alarm(rtc, enabled);
  218. }
  219. static const struct rtc_class_ops da9052_rtc_ops = {
  220. .read_time = da9052_rtc_read_time,
  221. .set_time = da9052_rtc_set_time,
  222. .read_alarm = da9052_rtc_read_alarm,
  223. .set_alarm = da9052_rtc_set_alarm,
  224. .alarm_irq_enable = da9052_rtc_alarm_irq_enable,
  225. };
  226. static int da9052_rtc_probe(struct platform_device *pdev)
  227. {
  228. struct da9052_rtc *rtc;
  229. int ret;
  230. rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
  231. if (!rtc)
  232. return -ENOMEM;
  233. rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
  234. platform_set_drvdata(pdev, rtc);
  235. ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
  236. if (ret < 0) {
  237. rtc_err(rtc,
  238. "Failed to setup RTC battery charging: %d\n", ret);
  239. return ret;
  240. }
  241. ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
  242. DA9052_ALARM_Y_TICK_ON, 0);
  243. if (ret != 0)
  244. rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);
  245. device_init_wakeup(&pdev->dev, true);
  246. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  247. &da9052_rtc_ops, THIS_MODULE);
  248. if (IS_ERR(rtc->rtc))
  249. return PTR_ERR(rtc->rtc);
  250. ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
  251. da9052_rtc_irq, rtc);
  252. if (ret != 0) {
  253. rtc_err(rtc, "irq registration failed: %d\n", ret);
  254. return ret;
  255. }
  256. return 0;
  257. }
  258. static struct platform_driver da9052_rtc_driver = {
  259. .probe = da9052_rtc_probe,
  260. .driver = {
  261. .name = "da9052-rtc",
  262. },
  263. };
  264. module_platform_driver(da9052_rtc_driver);
  265. MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
  266. MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
  267. MODULE_LICENSE("GPL");
  268. MODULE_ALIAS("platform:da9052-rtc");