rtc-88pm80x.c 9.9 KB

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