rtc-88pm80x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Real Time Clock driver for Marvell 88PM80x PMIC
  3. *
  4. * Copyright (c) 2012 Marvell International Ltd.
  5. * Wenzeng Chen<wzch@marvell.com>
  6. * Qiao Zhou <zhouqiao@marvell.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file "COPYING" in the main directory of this
  10. * archive for more details.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/regmap.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/88pm80x.h>
  27. #include <linux/rtc.h>
  28. #define PM800_RTC_COUNTER1 (0xD1)
  29. #define PM800_RTC_COUNTER2 (0xD2)
  30. #define PM800_RTC_COUNTER3 (0xD3)
  31. #define PM800_RTC_COUNTER4 (0xD4)
  32. #define PM800_RTC_EXPIRE1_1 (0xD5)
  33. #define PM800_RTC_EXPIRE1_2 (0xD6)
  34. #define PM800_RTC_EXPIRE1_3 (0xD7)
  35. #define PM800_RTC_EXPIRE1_4 (0xD8)
  36. #define PM800_RTC_TRIM1 (0xD9)
  37. #define PM800_RTC_TRIM2 (0xDA)
  38. #define PM800_RTC_TRIM3 (0xDB)
  39. #define PM800_RTC_TRIM4 (0xDC)
  40. #define PM800_RTC_EXPIRE2_1 (0xDD)
  41. #define PM800_RTC_EXPIRE2_2 (0xDE)
  42. #define PM800_RTC_EXPIRE2_3 (0xDF)
  43. #define PM800_RTC_EXPIRE2_4 (0xE0)
  44. #define PM800_POWER_DOWN_LOG1 (0xE5)
  45. #define PM800_POWER_DOWN_LOG2 (0xE6)
  46. struct pm80x_rtc_info {
  47. struct pm80x_chip *chip;
  48. struct regmap *map;
  49. struct rtc_device *rtc_dev;
  50. struct device *dev;
  51. struct delayed_work calib_work;
  52. int irq;
  53. int vrtc;
  54. };
  55. static irqreturn_t rtc_update_handler(int irq, void *data)
  56. {
  57. struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
  58. int mask;
  59. mask = PM800_ALARM | PM800_ALARM_WAKEUP;
  60. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
  61. mask);
  62. rtc_update_irq(info->rtc_dev, 1, RTC_AF);
  63. return IRQ_HANDLED;
  64. }
  65. static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  66. {
  67. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  68. if (enabled)
  69. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  70. PM800_ALARM1_EN, PM800_ALARM1_EN);
  71. else
  72. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  73. PM800_ALARM1_EN, 0);
  74. return 0;
  75. }
  76. /*
  77. * Calculate the next alarm time given the requested alarm time mask
  78. * and the current time.
  79. */
  80. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  81. struct rtc_time *alrm)
  82. {
  83. unsigned long next_time;
  84. unsigned long now_time;
  85. next->tm_year = now->tm_year;
  86. next->tm_mon = now->tm_mon;
  87. next->tm_mday = now->tm_mday;
  88. next->tm_hour = alrm->tm_hour;
  89. next->tm_min = alrm->tm_min;
  90. next->tm_sec = alrm->tm_sec;
  91. rtc_tm_to_time(now, &now_time);
  92. rtc_tm_to_time(next, &next_time);
  93. if (next_time < now_time) {
  94. /* Advance one day */
  95. next_time += 60 * 60 * 24;
  96. rtc_time_to_tm(next_time, next);
  97. }
  98. }
  99. static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  100. {
  101. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  102. unsigned char buf[4];
  103. unsigned long ticks, base, data;
  104. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  105. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  106. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  107. /* load 32-bit read-only counter */
  108. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  109. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  110. ticks = base + data;
  111. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  112. base, data, ticks);
  113. rtc_time_to_tm(ticks, tm);
  114. return 0;
  115. }
  116. static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  117. {
  118. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  119. unsigned char buf[4];
  120. unsigned long ticks, base, data;
  121. if ((tm->tm_year < 70) || (tm->tm_year > 138)) {
  122. dev_dbg(info->dev,
  123. "Set time %d out of range. Please set time between 1970 to 2038.\n",
  124. 1900 + tm->tm_year);
  125. return -EINVAL;
  126. }
  127. rtc_tm_to_time(tm, &ticks);
  128. /* load 32-bit read-only counter */
  129. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  130. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  131. base = ticks - data;
  132. dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  133. base, data, ticks);
  134. buf[0] = base & 0xFF;
  135. buf[1] = (base >> 8) & 0xFF;
  136. buf[2] = (base >> 16) & 0xFF;
  137. buf[3] = (base >> 24) & 0xFF;
  138. regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  139. return 0;
  140. }
  141. static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  142. {
  143. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  144. unsigned char buf[4];
  145. unsigned long ticks, base, data;
  146. int ret;
  147. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  148. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  149. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  150. regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  151. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  152. ticks = base + data;
  153. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  154. base, data, ticks);
  155. rtc_time_to_tm(ticks, &alrm->time);
  156. regmap_read(info->map, PM800_RTC_CONTROL, &ret);
  157. alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
  158. alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
  159. return 0;
  160. }
  161. static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  162. {
  163. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  164. struct rtc_time now_tm, alarm_tm;
  165. unsigned long ticks, base, data;
  166. unsigned char buf[4];
  167. int mask;
  168. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
  169. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  170. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  171. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  172. /* load 32-bit read-only counter */
  173. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  174. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  175. ticks = base + data;
  176. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  177. base, data, ticks);
  178. rtc_time_to_tm(ticks, &now_tm);
  179. dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
  180. rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
  181. /* get new ticks for alarm in 24 hours */
  182. rtc_tm_to_time(&alarm_tm, &ticks);
  183. dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
  184. data = ticks - base;
  185. buf[0] = data & 0xff;
  186. buf[1] = (data >> 8) & 0xff;
  187. buf[2] = (data >> 16) & 0xff;
  188. buf[3] = (data >> 24) & 0xff;
  189. regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  190. if (alrm->enabled) {
  191. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  192. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
  193. } else {
  194. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  195. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
  196. PM800_ALARM | PM800_ALARM_WAKEUP);
  197. }
  198. return 0;
  199. }
  200. static const struct rtc_class_ops pm80x_rtc_ops = {
  201. .read_time = pm80x_rtc_read_time,
  202. .set_time = pm80x_rtc_set_time,
  203. .read_alarm = pm80x_rtc_read_alarm,
  204. .set_alarm = pm80x_rtc_set_alarm,
  205. .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
  206. };
  207. #ifdef CONFIG_PM_SLEEP
  208. static int pm80x_rtc_suspend(struct device *dev)
  209. {
  210. return pm80x_dev_suspend(dev);
  211. }
  212. static int pm80x_rtc_resume(struct device *dev)
  213. {
  214. return pm80x_dev_resume(dev);
  215. }
  216. #endif
  217. static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
  218. static int pm80x_rtc_probe(struct platform_device *pdev)
  219. {
  220. struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  221. struct pm80x_platform_data *pm80x_pdata =
  222. dev_get_platdata(pdev->dev.parent);
  223. struct pm80x_rtc_pdata *pdata = NULL;
  224. struct pm80x_rtc_info *info;
  225. struct rtc_time tm;
  226. unsigned long ticks = 0;
  227. int ret;
  228. pdata = dev_get_platdata(&pdev->dev);
  229. if (pdata == NULL)
  230. dev_warn(&pdev->dev, "No platform data!\n");
  231. info =
  232. devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
  233. if (!info)
  234. return -ENOMEM;
  235. info->irq = platform_get_irq(pdev, 0);
  236. if (info->irq < 0) {
  237. dev_err(&pdev->dev, "No IRQ resource!\n");
  238. ret = -EINVAL;
  239. goto out;
  240. }
  241. info->chip = chip;
  242. info->map = chip->regmap;
  243. if (!info->map) {
  244. dev_err(&pdev->dev, "no regmap!\n");
  245. ret = -EINVAL;
  246. goto out;
  247. }
  248. info->dev = &pdev->dev;
  249. dev_set_drvdata(&pdev->dev, info);
  250. ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
  251. IRQF_ONESHOT, "rtc", info);
  252. if (ret < 0) {
  253. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  254. info->irq, ret);
  255. goto out;
  256. }
  257. ret = pm80x_rtc_read_time(&pdev->dev, &tm);
  258. if (ret < 0) {
  259. dev_err(&pdev->dev, "Failed to read initial time.\n");
  260. goto out_rtc;
  261. }
  262. if ((tm.tm_year < 70) || (tm.tm_year > 138)) {
  263. tm.tm_year = 70;
  264. tm.tm_mon = 0;
  265. tm.tm_mday = 1;
  266. tm.tm_hour = 0;
  267. tm.tm_min = 0;
  268. tm.tm_sec = 0;
  269. ret = pm80x_rtc_set_time(&pdev->dev, &tm);
  270. if (ret < 0) {
  271. dev_err(&pdev->dev, "Failed to set initial time.\n");
  272. goto out_rtc;
  273. }
  274. }
  275. rtc_tm_to_time(&tm, &ticks);
  276. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "88pm80x-rtc",
  277. &pm80x_rtc_ops, THIS_MODULE);
  278. if (IS_ERR(info->rtc_dev)) {
  279. ret = PTR_ERR(info->rtc_dev);
  280. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  281. goto out_rtc;
  282. }
  283. /*
  284. * enable internal XO instead of internal 3.25MHz clock since it can
  285. * free running in PMIC power-down state.
  286. */
  287. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
  288. PM800_RTC1_USE_XO);
  289. if (pm80x_pdata) {
  290. pdata = pm80x_pdata->rtc;
  291. if (pdata)
  292. info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
  293. }
  294. device_init_wakeup(&pdev->dev, 1);
  295. return 0;
  296. out_rtc:
  297. pm80x_free_irq(chip, info->irq, info);
  298. out:
  299. return ret;
  300. }
  301. static int pm80x_rtc_remove(struct platform_device *pdev)
  302. {
  303. struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
  304. pm80x_free_irq(info->chip, info->irq, info);
  305. return 0;
  306. }
  307. static struct platform_driver pm80x_rtc_driver = {
  308. .driver = {
  309. .name = "88pm80x-rtc",
  310. .pm = &pm80x_rtc_pm_ops,
  311. },
  312. .probe = pm80x_rtc_probe,
  313. .remove = pm80x_rtc_remove,
  314. };
  315. module_platform_driver(pm80x_rtc_driver);
  316. MODULE_LICENSE("GPL");
  317. MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
  318. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  319. MODULE_ALIAS("platform:88pm80x-rtc");