xref: /linux/drivers/rtc/rtc-meson-vrtc.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
16ef35398SNeil Armstrong // SPDX-License-Identifier: GPL-2.0
26ef35398SNeil Armstrong /*
36ef35398SNeil Armstrong  * Copyright (C) 2019 BayLibre, SAS
46ef35398SNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
56ef35398SNeil Armstrong  * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
66ef35398SNeil Armstrong  */
76ef35398SNeil Armstrong #include <linux/module.h>
86ef35398SNeil Armstrong #include <linux/platform_device.h>
96ef35398SNeil Armstrong #include <linux/rtc.h>
106ef35398SNeil Armstrong #include <linux/io.h>
116ef35398SNeil Armstrong #include <linux/of.h>
126ef35398SNeil Armstrong #include <linux/time64.h>
136ef35398SNeil Armstrong 
146ef35398SNeil Armstrong struct meson_vrtc_data {
156ef35398SNeil Armstrong 	void __iomem *io_alarm;
166ef35398SNeil Armstrong 	struct rtc_device *rtc;
176ef35398SNeil Armstrong 	unsigned long alarm_time;
186ef35398SNeil Armstrong 	bool enabled;
196ef35398SNeil Armstrong };
206ef35398SNeil Armstrong 
meson_vrtc_read_time(struct device * dev,struct rtc_time * tm)216ef35398SNeil Armstrong static int meson_vrtc_read_time(struct device *dev, struct rtc_time *tm)
226ef35398SNeil Armstrong {
236ef35398SNeil Armstrong 	struct timespec64 time;
246ef35398SNeil Armstrong 
256ef35398SNeil Armstrong 	dev_dbg(dev, "%s\n", __func__);
26*0e6255faSMartin Blumenstingl 	ktime_get_real_ts64(&time);
276ef35398SNeil Armstrong 	rtc_time64_to_tm(time.tv_sec, tm);
286ef35398SNeil Armstrong 
296ef35398SNeil Armstrong 	return 0;
306ef35398SNeil Armstrong }
316ef35398SNeil Armstrong 
meson_vrtc_set_wakeup_time(struct meson_vrtc_data * vrtc,unsigned long time)326ef35398SNeil Armstrong static void meson_vrtc_set_wakeup_time(struct meson_vrtc_data *vrtc,
336ef35398SNeil Armstrong 				       unsigned long time)
346ef35398SNeil Armstrong {
356ef35398SNeil Armstrong 	writel_relaxed(time, vrtc->io_alarm);
366ef35398SNeil Armstrong }
376ef35398SNeil Armstrong 
meson_vrtc_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)386ef35398SNeil Armstrong static int meson_vrtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
396ef35398SNeil Armstrong {
406ef35398SNeil Armstrong 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
416ef35398SNeil Armstrong 
426ef35398SNeil Armstrong 	dev_dbg(dev, "%s: alarm->enabled=%d\n", __func__, alarm->enabled);
436ef35398SNeil Armstrong 	if (alarm->enabled)
446ef35398SNeil Armstrong 		vrtc->alarm_time = rtc_tm_to_time64(&alarm->time);
456ef35398SNeil Armstrong 	else
466ef35398SNeil Armstrong 		vrtc->alarm_time = 0;
476ef35398SNeil Armstrong 
486ef35398SNeil Armstrong 	return 0;
496ef35398SNeil Armstrong }
506ef35398SNeil Armstrong 
meson_vrtc_alarm_irq_enable(struct device * dev,unsigned int enabled)516ef35398SNeil Armstrong static int meson_vrtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
526ef35398SNeil Armstrong {
536ef35398SNeil Armstrong 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
546ef35398SNeil Armstrong 
556ef35398SNeil Armstrong 	vrtc->enabled = enabled;
566ef35398SNeil Armstrong 	return 0;
576ef35398SNeil Armstrong }
586ef35398SNeil Armstrong 
596ef35398SNeil Armstrong static const struct rtc_class_ops meson_vrtc_ops = {
606ef35398SNeil Armstrong 	.read_time = meson_vrtc_read_time,
616ef35398SNeil Armstrong 	.set_alarm = meson_vrtc_set_alarm,
626ef35398SNeil Armstrong 	.alarm_irq_enable = meson_vrtc_alarm_irq_enable,
636ef35398SNeil Armstrong };
646ef35398SNeil Armstrong 
meson_vrtc_probe(struct platform_device * pdev)656ef35398SNeil Armstrong static int meson_vrtc_probe(struct platform_device *pdev)
666ef35398SNeil Armstrong {
676ef35398SNeil Armstrong 	struct meson_vrtc_data *vrtc;
686ef35398SNeil Armstrong 
696ef35398SNeil Armstrong 	vrtc = devm_kzalloc(&pdev->dev, sizeof(*vrtc), GFP_KERNEL);
706ef35398SNeil Armstrong 	if (!vrtc)
716ef35398SNeil Armstrong 		return -ENOMEM;
726ef35398SNeil Armstrong 
736ef35398SNeil Armstrong 	vrtc->io_alarm = devm_platform_ioremap_resource(pdev, 0);
746ef35398SNeil Armstrong 	if (IS_ERR(vrtc->io_alarm))
756ef35398SNeil Armstrong 		return PTR_ERR(vrtc->io_alarm);
766ef35398SNeil Armstrong 
776ef35398SNeil Armstrong 	device_init_wakeup(&pdev->dev, 1);
786ef35398SNeil Armstrong 
796ef35398SNeil Armstrong 	platform_set_drvdata(pdev, vrtc);
806ef35398SNeil Armstrong 
816ef35398SNeil Armstrong 	vrtc->rtc = devm_rtc_allocate_device(&pdev->dev);
826ef35398SNeil Armstrong 	if (IS_ERR(vrtc->rtc))
836ef35398SNeil Armstrong 		return PTR_ERR(vrtc->rtc);
846ef35398SNeil Armstrong 
856ef35398SNeil Armstrong 	vrtc->rtc->ops = &meson_vrtc_ops;
86fdcfd854SBartosz Golaszewski 	return devm_rtc_register_device(vrtc->rtc);
876ef35398SNeil Armstrong }
886ef35398SNeil Armstrong 
meson_vrtc_suspend(struct device * dev)89b99a3120SArnd Bergmann static int __maybe_unused meson_vrtc_suspend(struct device *dev)
906ef35398SNeil Armstrong {
916ef35398SNeil Armstrong 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
926ef35398SNeil Armstrong 
936ef35398SNeil Armstrong 	dev_dbg(dev, "%s\n", __func__);
946ef35398SNeil Armstrong 	if (vrtc->alarm_time) {
956ef35398SNeil Armstrong 		unsigned long local_time;
966ef35398SNeil Armstrong 		long alarm_secs;
976ef35398SNeil Armstrong 		struct timespec64 time;
986ef35398SNeil Armstrong 
99*0e6255faSMartin Blumenstingl 		ktime_get_real_ts64(&time);
1006ef35398SNeil Armstrong 		local_time = time.tv_sec;
1016ef35398SNeil Armstrong 
1026ef35398SNeil Armstrong 		dev_dbg(dev, "alarm_time = %lus, local_time=%lus\n",
1036ef35398SNeil Armstrong 			vrtc->alarm_time, local_time);
1046ef35398SNeil Armstrong 		alarm_secs = vrtc->alarm_time - local_time;
1056ef35398SNeil Armstrong 		if (alarm_secs > 0) {
1066ef35398SNeil Armstrong 			meson_vrtc_set_wakeup_time(vrtc, alarm_secs);
1076ef35398SNeil Armstrong 			dev_dbg(dev, "system will wakeup in %lds.\n",
1086ef35398SNeil Armstrong 				alarm_secs);
1096ef35398SNeil Armstrong 		} else {
1106ef35398SNeil Armstrong 			dev_err(dev, "alarm time already passed: %lds.\n",
1116ef35398SNeil Armstrong 				alarm_secs);
1126ef35398SNeil Armstrong 		}
1136ef35398SNeil Armstrong 	}
1146ef35398SNeil Armstrong 
1156ef35398SNeil Armstrong 	return 0;
1166ef35398SNeil Armstrong }
1176ef35398SNeil Armstrong 
meson_vrtc_resume(struct device * dev)118b99a3120SArnd Bergmann static int __maybe_unused meson_vrtc_resume(struct device *dev)
1196ef35398SNeil Armstrong {
1206ef35398SNeil Armstrong 	struct meson_vrtc_data *vrtc = dev_get_drvdata(dev);
1216ef35398SNeil Armstrong 
1226ef35398SNeil Armstrong 	dev_dbg(dev, "%s\n", __func__);
1236ef35398SNeil Armstrong 
1246ef35398SNeil Armstrong 	vrtc->alarm_time = 0;
1256ef35398SNeil Armstrong 	meson_vrtc_set_wakeup_time(vrtc, 0);
1266ef35398SNeil Armstrong 	return 0;
1276ef35398SNeil Armstrong }
128b99a3120SArnd Bergmann 
1296ef35398SNeil Armstrong static SIMPLE_DEV_PM_OPS(meson_vrtc_pm_ops,
1306ef35398SNeil Armstrong 			 meson_vrtc_suspend, meson_vrtc_resume);
1316ef35398SNeil Armstrong 
1326ef35398SNeil Armstrong static const struct of_device_id meson_vrtc_dt_match[] = {
1336ef35398SNeil Armstrong 	{ .compatible = "amlogic,meson-vrtc"},
1346ef35398SNeil Armstrong 	{},
1356ef35398SNeil Armstrong };
1366ef35398SNeil Armstrong MODULE_DEVICE_TABLE(of, meson_vrtc_dt_match);
1376ef35398SNeil Armstrong 
1386ef35398SNeil Armstrong static struct platform_driver meson_vrtc_driver = {
1396ef35398SNeil Armstrong 	.probe = meson_vrtc_probe,
1406ef35398SNeil Armstrong 	.driver = {
1416ef35398SNeil Armstrong 		.name = "meson-vrtc",
1426ef35398SNeil Armstrong 		.of_match_table = meson_vrtc_dt_match,
1436ef35398SNeil Armstrong 		.pm = &meson_vrtc_pm_ops,
1446ef35398SNeil Armstrong 	},
1456ef35398SNeil Armstrong };
1466ef35398SNeil Armstrong 
1476ef35398SNeil Armstrong module_platform_driver(meson_vrtc_driver);
1486ef35398SNeil Armstrong 
1496ef35398SNeil Armstrong MODULE_DESCRIPTION("Amlogic Virtual Wakeup RTC Timer driver");
1506ef35398SNeil Armstrong MODULE_LICENSE("GPL");
151