rtc-da9052.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/mfd/da9052/da9052.h>
  18. #include <linux/mfd/da9052/reg.h>
  19. #define rtc_err(da9052, fmt, ...) \
  20. dev_err(da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
  21. struct da9052_rtc {
  22. struct rtc_device *rtc;
  23. struct da9052 *da9052;
  24. int irq;
  25. };
  26. static int da9052_rtc_enable_alarm(struct da9052 *da9052, bool enable)
  27. {
  28. int ret;
  29. if (enable) {
  30. ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
  31. DA9052_ALARM_Y_ALARM_ON,
  32. DA9052_ALARM_Y_ALARM_ON);
  33. if (ret != 0)
  34. rtc_err(da9052, "Failed to enable ALM: %d\n", ret);
  35. } else {
  36. ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
  37. DA9052_ALARM_Y_ALARM_ON, 0);
  38. if (ret != 0)
  39. rtc_err(da9052, "Write error: %d\n", ret);
  40. }
  41. return ret;
  42. }
  43. static irqreturn_t da9052_rtc_irq(int irq, void *data)
  44. {
  45. struct da9052_rtc *rtc = data;
  46. int ret;
  47. ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_MI_REG);
  48. if (ret < 0) {
  49. rtc_err(rtc->da9052, "Read error: %d\n", ret);
  50. return IRQ_NONE;
  51. }
  52. if (ret & DA9052_ALARMMI_ALARMTYPE) {
  53. da9052_rtc_enable_alarm(rtc->da9052, 0);
  54. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  55. } else
  56. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_PF);
  57. return IRQ_HANDLED;
  58. }
  59. static int da9052_read_alarm(struct da9052 *da9052, struct rtc_time *rtc_tm)
  60. {
  61. int ret;
  62. uint8_t v[5];
  63. ret = da9052_group_read(da9052, DA9052_ALARM_MI_REG, 5, v);
  64. if (ret != 0) {
  65. rtc_err(da9052, "Failed to group read ALM: %d\n", ret);
  66. return ret;
  67. }
  68. rtc_tm->tm_year = (v[4] & DA9052_RTC_YEAR) + 100;
  69. rtc_tm->tm_mon = (v[3] & DA9052_RTC_MONTH) - 1;
  70. rtc_tm->tm_mday = v[2] & DA9052_RTC_DAY;
  71. rtc_tm->tm_hour = v[1] & DA9052_RTC_HOUR;
  72. rtc_tm->tm_min = v[0] & DA9052_RTC_MIN;
  73. ret = rtc_valid_tm(rtc_tm);
  74. if (ret != 0)
  75. return ret;
  76. return ret;
  77. }
  78. static int da9052_set_alarm(struct da9052 *da9052, struct rtc_time *rtc_tm)
  79. {
  80. int ret;
  81. uint8_t v[3];
  82. rtc_tm->tm_year -= 100;
  83. rtc_tm->tm_mon += 1;
  84. ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
  85. DA9052_RTC_MIN, rtc_tm->tm_min);
  86. if (ret != 0) {
  87. rtc_err(da9052, "Failed to write ALRM MIN: %d\n", ret);
  88. return ret;
  89. }
  90. v[0] = rtc_tm->tm_hour;
  91. v[1] = rtc_tm->tm_mday;
  92. v[2] = rtc_tm->tm_mon;
  93. ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
  94. if (ret < 0)
  95. return ret;
  96. ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
  97. DA9052_RTC_YEAR, rtc_tm->tm_year);
  98. if (ret != 0)
  99. rtc_err(da9052, "Failed to write ALRM YEAR: %d\n", ret);
  100. return ret;
  101. }
  102. static int da9052_rtc_get_alarm_status(struct da9052 *da9052)
  103. {
  104. int ret;
  105. ret = da9052_reg_read(da9052, DA9052_ALARM_Y_REG);
  106. if (ret < 0) {
  107. rtc_err(da9052, "Failed to read ALM: %d\n", ret);
  108. return ret;
  109. }
  110. ret &= DA9052_ALARM_Y_ALARM_ON;
  111. return (ret > 0) ? 1 : 0;
  112. }
  113. static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
  114. {
  115. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  116. uint8_t v[6];
  117. int ret;
  118. ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
  119. if (ret < 0) {
  120. rtc_err(rtc->da9052, "Failed to read RTC time : %d\n", ret);
  121. return ret;
  122. }
  123. rtc_tm->tm_year = (v[5] & DA9052_RTC_YEAR) + 100;
  124. rtc_tm->tm_mon = (v[4] & DA9052_RTC_MONTH) - 1;
  125. rtc_tm->tm_mday = v[3] & DA9052_RTC_DAY;
  126. rtc_tm->tm_hour = v[2] & DA9052_RTC_HOUR;
  127. rtc_tm->tm_min = v[1] & DA9052_RTC_MIN;
  128. rtc_tm->tm_sec = v[0] & DA9052_RTC_SEC;
  129. ret = rtc_valid_tm(rtc_tm);
  130. if (ret != 0) {
  131. rtc_err(rtc->da9052, "rtc_valid_tm failed: %d\n", ret);
  132. return ret;
  133. }
  134. return 0;
  135. }
  136. static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
  137. {
  138. struct da9052_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 da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
  148. }
  149. static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  150. {
  151. int ret;
  152. struct rtc_time *tm = &alrm->time;
  153. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  154. ret = da9052_read_alarm(rtc->da9052, tm);
  155. if (ret)
  156. return ret;
  157. alrm->enabled = da9052_rtc_get_alarm_status(rtc->da9052);
  158. return 0;
  159. }
  160. static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  161. {
  162. int ret;
  163. struct rtc_time *tm = &alrm->time;
  164. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  165. ret = da9052_rtc_enable_alarm(rtc->da9052, 0);
  166. if (ret < 0)
  167. return ret;
  168. ret = da9052_set_alarm(rtc->da9052, tm);
  169. if (ret)
  170. return ret;
  171. ret = da9052_rtc_enable_alarm(rtc->da9052, 1);
  172. return ret;
  173. }
  174. static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  175. {
  176. struct da9052_rtc *rtc = dev_get_drvdata(dev);
  177. return da9052_rtc_enable_alarm(rtc->da9052, enabled);
  178. }
  179. static const struct rtc_class_ops da9052_rtc_ops = {
  180. .read_time = da9052_rtc_read_time,
  181. .set_time = da9052_rtc_set_time,
  182. .read_alarm = da9052_rtc_read_alarm,
  183. .set_alarm = da9052_rtc_set_alarm,
  184. .alarm_irq_enable = da9052_rtc_alarm_irq_enable,
  185. };
  186. static int __devinit da9052_rtc_probe(struct platform_device *pdev)
  187. {
  188. struct da9052_rtc *rtc;
  189. int ret;
  190. rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
  191. if (!rtc)
  192. return -ENOMEM;
  193. rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
  194. platform_set_drvdata(pdev, rtc);
  195. rtc->irq = platform_get_irq_byname(pdev, "ALM");
  196. ret = request_threaded_irq(rtc->irq, NULL, da9052_rtc_irq,
  197. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  198. "ALM", rtc);
  199. if (ret != 0) {
  200. rtc_err(rtc->da9052, "irq registration failed: %d\n", ret);
  201. goto err_mem;
  202. }
  203. rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
  204. &da9052_rtc_ops, THIS_MODULE);
  205. if (IS_ERR(rtc->rtc)) {
  206. ret = PTR_ERR(rtc->rtc);
  207. goto err_free_irq;
  208. }
  209. return 0;
  210. err_free_irq:
  211. free_irq(rtc->irq, rtc);
  212. err_mem:
  213. devm_kfree(&pdev->dev, rtc);
  214. return ret;
  215. }
  216. static int __devexit da9052_rtc_remove(struct platform_device *pdev)
  217. {
  218. struct da9052_rtc *rtc = pdev->dev.platform_data;
  219. rtc_device_unregister(rtc->rtc);
  220. free_irq(rtc->irq, rtc);
  221. platform_set_drvdata(pdev, NULL);
  222. devm_kfree(&pdev->dev, rtc);
  223. return 0;
  224. }
  225. static struct platform_driver da9052_rtc_driver = {
  226. .probe = da9052_rtc_probe,
  227. .remove = __devexit_p(da9052_rtc_remove),
  228. .driver = {
  229. .name = "da9052-rtc",
  230. .owner = THIS_MODULE,
  231. },
  232. };
  233. module_platform_driver(da9052_rtc_driver);
  234. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  235. MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
  236. MODULE_LICENSE("GPL");
  237. MODULE_ALIAS("platform:da9052-rtc");