rtc-88pm80x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. int irq;
  52. };
  53. static irqreturn_t rtc_update_handler(int irq, void *data)
  54. {
  55. struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
  56. int mask;
  57. mask = PM800_ALARM | PM800_ALARM_WAKEUP;
  58. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
  59. mask);
  60. rtc_update_irq(info->rtc_dev, 1, RTC_AF);
  61. return IRQ_HANDLED;
  62. }
  63. static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  64. {
  65. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  66. if (enabled)
  67. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  68. PM800_ALARM1_EN, PM800_ALARM1_EN);
  69. else
  70. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  71. PM800_ALARM1_EN, 0);
  72. return 0;
  73. }
  74. /*
  75. * Calculate the next alarm time given the requested alarm time mask
  76. * and the current time.
  77. */
  78. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  79. struct rtc_time *alrm)
  80. {
  81. unsigned long next_time;
  82. unsigned long now_time;
  83. next->tm_year = now->tm_year;
  84. next->tm_mon = now->tm_mon;
  85. next->tm_mday = now->tm_mday;
  86. next->tm_hour = alrm->tm_hour;
  87. next->tm_min = alrm->tm_min;
  88. next->tm_sec = alrm->tm_sec;
  89. now_time = rtc_tm_to_time64(now);
  90. next_time = rtc_tm_to_time64(next);
  91. if (next_time < now_time) {
  92. /* Advance one day */
  93. next_time += 60 * 60 * 24;
  94. rtc_time64_to_tm(next_time, next);
  95. }
  96. }
  97. static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  98. {
  99. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  100. unsigned char buf[4];
  101. unsigned long ticks, base, data;
  102. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  103. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  104. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  105. /* load 32-bit read-only counter */
  106. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  107. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  108. ticks = base + data;
  109. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  110. base, data, ticks);
  111. rtc_time64_to_tm(ticks, tm);
  112. return 0;
  113. }
  114. static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  115. {
  116. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  117. unsigned char buf[4];
  118. unsigned long ticks, base, data;
  119. ticks = rtc_tm_to_time64(tm);
  120. /* load 32-bit read-only counter */
  121. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  122. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  123. base = ticks - data;
  124. dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  125. base, data, ticks);
  126. buf[0] = base & 0xFF;
  127. buf[1] = (base >> 8) & 0xFF;
  128. buf[2] = (base >> 16) & 0xFF;
  129. buf[3] = (base >> 24) & 0xFF;
  130. regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  131. return 0;
  132. }
  133. static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  134. {
  135. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  136. unsigned char buf[4];
  137. unsigned long ticks, base, data;
  138. int ret;
  139. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  140. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  141. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  142. regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  143. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  144. ticks = base + data;
  145. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  146. base, data, ticks);
  147. rtc_time64_to_tm(ticks, &alrm->time);
  148. regmap_read(info->map, PM800_RTC_CONTROL, &ret);
  149. alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
  150. alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
  151. return 0;
  152. }
  153. static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  154. {
  155. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  156. struct rtc_time now_tm, alarm_tm;
  157. unsigned long ticks, base, data;
  158. unsigned char buf[4];
  159. int mask;
  160. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
  161. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  162. base = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  163. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  164. /* load 32-bit read-only counter */
  165. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  166. data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  167. ticks = base + data;
  168. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  169. base, data, ticks);
  170. rtc_time64_to_tm(ticks, &now_tm);
  171. dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
  172. rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
  173. /* get new ticks for alarm in 24 hours */
  174. ticks = rtc_tm_to_time64(&alarm_tm);
  175. dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
  176. data = ticks - base;
  177. buf[0] = data & 0xff;
  178. buf[1] = (data >> 8) & 0xff;
  179. buf[2] = (data >> 16) & 0xff;
  180. buf[3] = (data >> 24) & 0xff;
  181. regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  182. if (alrm->enabled) {
  183. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  184. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
  185. } else {
  186. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  187. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
  188. PM800_ALARM | PM800_ALARM_WAKEUP);
  189. }
  190. return 0;
  191. }
  192. static const struct rtc_class_ops pm80x_rtc_ops = {
  193. .read_time = pm80x_rtc_read_time,
  194. .set_time = pm80x_rtc_set_time,
  195. .read_alarm = pm80x_rtc_read_alarm,
  196. .set_alarm = pm80x_rtc_set_alarm,
  197. .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
  198. };
  199. #ifdef CONFIG_PM_SLEEP
  200. static int pm80x_rtc_suspend(struct device *dev)
  201. {
  202. return pm80x_dev_suspend(dev);
  203. }
  204. static int pm80x_rtc_resume(struct device *dev)
  205. {
  206. return pm80x_dev_resume(dev);
  207. }
  208. #endif
  209. static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
  210. static int pm80x_rtc_probe(struct platform_device *pdev)
  211. {
  212. struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  213. struct pm80x_rtc_pdata *pdata = dev_get_platdata(&pdev->dev);
  214. struct pm80x_rtc_info *info;
  215. struct device_node *node = pdev->dev.of_node;
  216. int ret;
  217. if (!pdata && !node) {
  218. dev_err(&pdev->dev,
  219. "pm80x-rtc requires platform data or of_node\n");
  220. return -EINVAL;
  221. }
  222. if (!pdata) {
  223. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  224. if (!pdata) {
  225. dev_err(&pdev->dev, "failed to allocate memory\n");
  226. return -ENOMEM;
  227. }
  228. }
  229. info =
  230. devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
  231. if (!info)
  232. return -ENOMEM;
  233. info->irq = platform_get_irq(pdev, 0);
  234. if (info->irq < 0) {
  235. dev_err(&pdev->dev, "No IRQ resource!\n");
  236. ret = -EINVAL;
  237. goto out;
  238. }
  239. info->chip = chip;
  240. info->map = chip->regmap;
  241. if (!info->map) {
  242. dev_err(&pdev->dev, "no regmap!\n");
  243. ret = -EINVAL;
  244. goto out;
  245. }
  246. info->dev = &pdev->dev;
  247. dev_set_drvdata(&pdev->dev, info);
  248. info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  249. if (IS_ERR(info->rtc_dev))
  250. return PTR_ERR(info->rtc_dev);
  251. ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
  252. IRQF_ONESHOT, "rtc", info);
  253. if (ret < 0) {
  254. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  255. info->irq, ret);
  256. goto out;
  257. }
  258. info->rtc_dev->ops = &pm80x_rtc_ops;
  259. info->rtc_dev->range_max = U32_MAX;
  260. ret = rtc_register_device(info->rtc_dev);
  261. if (ret) {
  262. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  263. goto out_rtc;
  264. }
  265. /*
  266. * enable internal XO instead of internal 3.25MHz clock since it can
  267. * free running in PMIC power-down state.
  268. */
  269. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
  270. PM800_RTC1_USE_XO);
  271. /* remember whether this power up is caused by PMIC RTC or not */
  272. info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
  273. device_init_wakeup(&pdev->dev, 1);
  274. return 0;
  275. out_rtc:
  276. pm80x_free_irq(chip, info->irq, info);
  277. out:
  278. return ret;
  279. }
  280. static int pm80x_rtc_remove(struct platform_device *pdev)
  281. {
  282. struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
  283. pm80x_free_irq(info->chip, info->irq, info);
  284. return 0;
  285. }
  286. static struct platform_driver pm80x_rtc_driver = {
  287. .driver = {
  288. .name = "88pm80x-rtc",
  289. .pm = &pm80x_rtc_pm_ops,
  290. },
  291. .probe = pm80x_rtc_probe,
  292. .remove = pm80x_rtc_remove,
  293. };
  294. module_platform_driver(pm80x_rtc_driver);
  295. MODULE_LICENSE("GPL");
  296. MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
  297. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  298. MODULE_ALIAS("platform:88pm80x-rtc");