rtc-da9063.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* rtc-da9063.c - Real time clock device driver for DA9063
  2. * Copyright (C) 2013-14 Dialog Semiconductor Ltd.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/da9063/registers.h>
  24. #include <linux/mfd/da9063/core.h>
  25. #define YEARS_TO_DA9063(year) ((year) - 100)
  26. #define MONTHS_TO_DA9063(month) ((month) + 1)
  27. #define YEARS_FROM_DA9063(year) ((year) + 100)
  28. #define MONTHS_FROM_DA9063(month) ((month) - 1)
  29. #define RTC_ALARM_DATA_LEN (DA9063_AD_REG_ALARM_Y - DA9063_AD_REG_ALARM_MI + 1)
  30. #define RTC_DATA_LEN (DA9063_REG_COUNT_Y - DA9063_REG_COUNT_S + 1)
  31. #define RTC_SEC 0
  32. #define RTC_MIN 1
  33. #define RTC_HOUR 2
  34. #define RTC_DAY 3
  35. #define RTC_MONTH 4
  36. #define RTC_YEAR 5
  37. struct da9063_rtc {
  38. struct rtc_device *rtc_dev;
  39. struct da9063 *hw;
  40. struct rtc_time alarm_time;
  41. bool rtc_sync;
  42. int alarm_year;
  43. int alarm_start;
  44. int alarm_len;
  45. int data_start;
  46. };
  47. static void da9063_data_to_tm(u8 *data, struct rtc_time *tm)
  48. {
  49. tm->tm_sec = data[RTC_SEC] & DA9063_COUNT_SEC_MASK;
  50. tm->tm_min = data[RTC_MIN] & DA9063_COUNT_MIN_MASK;
  51. tm->tm_hour = data[RTC_HOUR] & DA9063_COUNT_HOUR_MASK;
  52. tm->tm_mday = data[RTC_DAY] & DA9063_COUNT_DAY_MASK;
  53. tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] &
  54. DA9063_COUNT_MONTH_MASK);
  55. tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] &
  56. DA9063_COUNT_YEAR_MASK);
  57. }
  58. static void da9063_tm_to_data(struct rtc_time *tm, u8 *data)
  59. {
  60. data[RTC_SEC] &= ~DA9063_COUNT_SEC_MASK;
  61. data[RTC_SEC] |= tm->tm_sec & DA9063_COUNT_SEC_MASK;
  62. data[RTC_MIN] &= ~DA9063_COUNT_MIN_MASK;
  63. data[RTC_MIN] |= tm->tm_min & DA9063_COUNT_MIN_MASK;
  64. data[RTC_HOUR] &= ~DA9063_COUNT_HOUR_MASK;
  65. data[RTC_HOUR] |= tm->tm_hour & DA9063_COUNT_HOUR_MASK;
  66. data[RTC_DAY] &= ~DA9063_COUNT_DAY_MASK;
  67. data[RTC_DAY] |= tm->tm_mday & DA9063_COUNT_DAY_MASK;
  68. data[RTC_MONTH] &= ~DA9063_COUNT_MONTH_MASK;
  69. data[RTC_MONTH] |= MONTHS_TO_DA9063(tm->tm_mon) &
  70. DA9063_COUNT_MONTH_MASK;
  71. data[RTC_YEAR] &= ~DA9063_COUNT_YEAR_MASK;
  72. data[RTC_YEAR] |= YEARS_TO_DA9063(tm->tm_year) &
  73. DA9063_COUNT_YEAR_MASK;
  74. }
  75. static int da9063_rtc_stop_alarm(struct device *dev)
  76. {
  77. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  78. return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
  79. DA9063_ALARM_ON, 0);
  80. }
  81. static int da9063_rtc_start_alarm(struct device *dev)
  82. {
  83. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  84. return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
  85. DA9063_ALARM_ON, DA9063_ALARM_ON);
  86. }
  87. static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
  88. {
  89. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  90. unsigned long tm_secs;
  91. unsigned long al_secs;
  92. u8 data[RTC_DATA_LEN];
  93. int ret;
  94. ret = regmap_bulk_read(rtc->hw->regmap, DA9063_REG_COUNT_S,
  95. data, RTC_DATA_LEN);
  96. if (ret < 0) {
  97. dev_err(dev, "Failed to read RTC time data: %d\n", ret);
  98. return ret;
  99. }
  100. if (!(data[RTC_SEC] & DA9063_RTC_READ)) {
  101. dev_dbg(dev, "RTC not yet ready to be read by the host\n");
  102. return -EINVAL;
  103. }
  104. da9063_data_to_tm(data, tm);
  105. rtc_tm_to_time(tm, &tm_secs);
  106. rtc_tm_to_time(&rtc->alarm_time, &al_secs);
  107. /* handle the rtc synchronisation delay */
  108. if (rtc->rtc_sync == true && al_secs - tm_secs == 1)
  109. memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time));
  110. else
  111. rtc->rtc_sync = false;
  112. return rtc_valid_tm(tm);
  113. }
  114. static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
  115. {
  116. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  117. u8 data[RTC_DATA_LEN];
  118. int ret;
  119. da9063_tm_to_data(tm, data);
  120. ret = regmap_bulk_write(rtc->hw->regmap, DA9063_REG_COUNT_S,
  121. data, RTC_DATA_LEN);
  122. if (ret < 0)
  123. dev_err(dev, "Failed to set RTC time data: %d\n", ret);
  124. return ret;
  125. }
  126. static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  127. {
  128. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  129. u8 data[RTC_DATA_LEN];
  130. int ret;
  131. unsigned int val;
  132. data[RTC_SEC] = 0;
  133. ret = regmap_bulk_read(rtc->hw->regmap, rtc->alarm_start,
  134. &data[rtc->data_start], rtc->alarm_len);
  135. if (ret < 0)
  136. return ret;
  137. da9063_data_to_tm(data, &alrm->time);
  138. alrm->enabled = !!(data[RTC_YEAR] & DA9063_ALARM_ON);
  139. ret = regmap_read(rtc->hw->regmap, DA9063_REG_EVENT_A, &val);
  140. if (ret < 0)
  141. return ret;
  142. if (val & (DA9063_E_ALARM))
  143. alrm->pending = 1;
  144. else
  145. alrm->pending = 0;
  146. return 0;
  147. }
  148. static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  149. {
  150. struct da9063_rtc *rtc = dev_get_drvdata(dev);
  151. u8 data[RTC_DATA_LEN];
  152. int ret;
  153. da9063_tm_to_data(&alrm->time, data);
  154. ret = da9063_rtc_stop_alarm(dev);
  155. if (ret < 0) {
  156. dev_err(dev, "Failed to stop alarm: %d\n", ret);
  157. return ret;
  158. }
  159. ret = regmap_bulk_write(rtc->hw->regmap, rtc->alarm_start,
  160. &data[rtc->data_start], rtc->alarm_len);
  161. if (ret < 0) {
  162. dev_err(dev, "Failed to write alarm: %d\n", ret);
  163. return ret;
  164. }
  165. da9063_data_to_tm(data, &rtc->alarm_time);
  166. if (alrm->enabled) {
  167. ret = da9063_rtc_start_alarm(dev);
  168. if (ret < 0) {
  169. dev_err(dev, "Failed to start alarm: %d\n", ret);
  170. return ret;
  171. }
  172. }
  173. return ret;
  174. }
  175. static int da9063_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  176. {
  177. if (enabled)
  178. return da9063_rtc_start_alarm(dev);
  179. else
  180. return da9063_rtc_stop_alarm(dev);
  181. }
  182. static irqreturn_t da9063_alarm_event(int irq, void *data)
  183. {
  184. struct da9063_rtc *rtc = data;
  185. regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
  186. DA9063_ALARM_ON, 0);
  187. rtc->rtc_sync = true;
  188. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  189. return IRQ_HANDLED;
  190. }
  191. static const struct rtc_class_ops da9063_rtc_ops = {
  192. .read_time = da9063_rtc_read_time,
  193. .set_time = da9063_rtc_set_time,
  194. .read_alarm = da9063_rtc_read_alarm,
  195. .set_alarm = da9063_rtc_set_alarm,
  196. .alarm_irq_enable = da9063_rtc_alarm_irq_enable,
  197. };
  198. static int da9063_rtc_probe(struct platform_device *pdev)
  199. {
  200. struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
  201. struct da9063_rtc *rtc;
  202. int irq_alarm;
  203. u8 data[RTC_DATA_LEN];
  204. int ret;
  205. ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_E,
  206. DA9063_RTC_EN, DA9063_RTC_EN);
  207. if (ret < 0) {
  208. dev_err(&pdev->dev, "Failed to enable RTC\n");
  209. goto err;
  210. }
  211. ret = regmap_update_bits(da9063->regmap, DA9063_REG_EN_32K,
  212. DA9063_CRYSTAL, DA9063_CRYSTAL);
  213. if (ret < 0) {
  214. dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
  215. goto err;
  216. }
  217. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  218. if (!rtc)
  219. return -ENOMEM;
  220. if (da9063->variant_code == PMIC_DA9063_AD) {
  221. rtc->alarm_year = DA9063_AD_REG_ALARM_Y;
  222. rtc->alarm_start = DA9063_AD_REG_ALARM_MI;
  223. rtc->alarm_len = RTC_ALARM_DATA_LEN;
  224. rtc->data_start = RTC_MIN;
  225. } else {
  226. rtc->alarm_year = DA9063_BB_REG_ALARM_Y;
  227. rtc->alarm_start = DA9063_BB_REG_ALARM_S;
  228. rtc->alarm_len = RTC_DATA_LEN;
  229. rtc->data_start = RTC_SEC;
  230. }
  231. ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
  232. DA9063_ALARM_STATUS_TICK | DA9063_ALARM_STATUS_ALARM,
  233. 0);
  234. if (ret < 0) {
  235. dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
  236. goto err;
  237. }
  238. ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
  239. DA9063_ALARM_STATUS_ALARM,
  240. DA9063_ALARM_STATUS_ALARM);
  241. if (ret < 0) {
  242. dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
  243. goto err;
  244. }
  245. ret = regmap_update_bits(da9063->regmap, rtc->alarm_year,
  246. DA9063_TICK_ON, 0);
  247. if (ret < 0) {
  248. dev_err(&pdev->dev, "Failed to disable TICKs\n");
  249. goto err;
  250. }
  251. data[RTC_SEC] = 0;
  252. ret = regmap_bulk_read(da9063->regmap, rtc->alarm_start,
  253. &data[rtc->data_start], rtc->alarm_len);
  254. if (ret < 0) {
  255. dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
  256. ret);
  257. goto err;
  258. }
  259. platform_set_drvdata(pdev, rtc);
  260. irq_alarm = platform_get_irq_byname(pdev, "ALARM");
  261. ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
  262. da9063_alarm_event,
  263. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  264. "ALARM", rtc);
  265. if (ret) {
  266. dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
  267. irq_alarm, ret);
  268. goto err;
  269. }
  270. rtc->hw = da9063;
  271. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
  272. &da9063_rtc_ops, THIS_MODULE);
  273. if (IS_ERR(rtc->rtc_dev))
  274. return PTR_ERR(rtc->rtc_dev);
  275. da9063_data_to_tm(data, &rtc->alarm_time);
  276. rtc->rtc_sync = false;
  277. err:
  278. return ret;
  279. }
  280. static struct platform_driver da9063_rtc_driver = {
  281. .probe = da9063_rtc_probe,
  282. .driver = {
  283. .name = DA9063_DRVNAME_RTC,
  284. },
  285. };
  286. module_platform_driver(da9063_rtc_driver);
  287. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  288. MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
  289. MODULE_LICENSE("GPL v2");
  290. MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);