Skip to contentSkip to conversation

rtc: test: remove useless proc info

PATCH New Alexandre Belloni 1 participant 5 messages May 31, 2018 Real time clock (RTC)

lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/5 rtc: test: remove useless proc info

The rtc proc callback is useless for two reasosn:
- the test RTC is often not the first RTC so it will never be used
- all the info is available in the name file of the RTC sys folder

---
 drivers/rtc/rtc-test.c | 11 -----------
 1 file changed, 11 deletions(-)
drivers/rtc/rtc-test.c +0 -11
index a0d1571c4af6..f0eb8e0c5055 100644
@@ -40,23 +40,12 @@ static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
 	return 0;
 }
 
-static int test_rtc_proc(struct device *dev, struct seq_file *seq)
-{
-	struct platform_device *plat_dev = to_platform_device(dev);
-
-	seq_printf(seq, "test\t\t: yes\n");
-	seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
-
-	return 0;
-}
-
 static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
 {
 	return 0;
 }
 
 static const struct rtc_class_ops test_rtc_ops = {
-	.proc = test_rtc_proc,
 	.read_time = test_rtc_read_time,
 	.read_alarm = test_rtc_read_alarm,
 	.set_alarm = test_rtc_set_alarm,
Signature · 1 line
2.17.0
author maintainer
#1 lore raw

PATCH 2/5 rtc: test: allow registering many devices

Use a loop to register RTC devices

---
 drivers/rtc/rtc-test.c | 48 +++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
drivers/rtc/rtc-test.c +24 -24
index f0eb8e0c5055..796c45dee661 100644
@@ -13,7 +13,9 @@
 #include <linux/rtc.h>
 #include <linux/platform_device.h>
 
-static struct platform_device *test0 = NULL, *test1 = NULL;
+#define MAX_RTC_TEST 3
+
+struct platform_device *pdev[MAX_RTC_TEST];
 
 static int test_rtc_read_alarm(struct device *dev,
 	struct rtc_wkalrm *alrm)
@@ -122,47 +124,45 @@ static struct platform_driver test_driver = {
 
 static int __init test_init(void)
 {
-	int err;
+	int i, err;
 
 	if ((err = platform_driver_register(&test_driver)))
 		return err;
 
-	if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
-		err = -ENOMEM;
-		goto exit_driver_unregister;
+	err = -ENOMEM;
+	for (i = 0; i < MAX_RTC_TEST; i++) {
+		pdev[i] = platform_device_alloc("rtc-test", i);
+		if (!pdev[i])
+			goto exit_free_mem;
 	}
 
-	if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
-		err = -ENOMEM;
-		goto exit_put_test0;
+	for (i = 0; i < MAX_RTC_TEST; i++) {
+		err = platform_device_add(pdev[i]);
+		if (err)
+			goto exit_device_del;
 	}
 
-	if ((err = platform_device_add(test0)))
-		goto exit_put_test1;
-
-	if ((err = platform_device_add(test1)))
-		goto exit_del_test0;
-
 	return 0;
 
-exit_del_test0:
-	platform_device_del(test0);
+exit_device_del:
+	for (; i > 0; i--)
+		platform_device_del(pdev[i - 1]);
 
-exit_put_test1:
-	platform_device_put(test1);
+exit_free_mem:
+	for (i = 0; i < MAX_RTC_TEST; i++)
+		platform_device_put(pdev[i]);
 
-exit_put_test0:
-	platform_device_put(test0);
-
-exit_driver_unregister:
 	platform_driver_unregister(&test_driver);
 	return err;
 }
 
 static void __exit test_exit(void)
 {
-	platform_device_unregister(test0);
-	platform_device_unregister(test1);
+	int i;
+
+	for (i = 0; i < MAX_RTC_TEST; i++)
+		platform_device_unregister(pdev[i]);
+
 	platform_driver_unregister(&test_driver);
 }
 
Signature · 1 line
2.17.0
author maintainer
#2 lore raw

PATCH 3/5 rtc: test: store time as an offset to system time

In reply to Alexandre Belloni

Store the time as an offset to system time. As the offset is in second, it is currently always synced with system time.

---
 drivers/rtc/rtc-test.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
drivers/rtc/rtc-test.c +23 -16
index 796c45dee661..041150e53fd7 100644
@@ -15,6 +15,11 @@
 
 #define MAX_RTC_TEST 3
 
+struct rtc_test_data {
+	struct rtc_device *rtc;
+	time64_t offset;
+};
+
 struct platform_device *pdev[MAX_RTC_TEST];
 
 static int test_rtc_read_alarm(struct device *dev,
@@ -29,16 +34,21 @@ static int test_rtc_set_alarm(struct device *dev,
 	return 0;
 }
 
-static int test_rtc_read_time(struct device *dev,
-	struct rtc_time *tm)
+static int test_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
-	rtc_time64_to_tm(ktime_get_real_seconds(), tm);
+	struct rtc_test_data *rtd = dev_get_drvdata(dev);
+
+	rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm);
+
 	return 0;
 }
 
 static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
 {
-	dev_info(dev, "%s, secs = %lld\n", __func__, (long long)secs);
+	struct rtc_test_data *rtd = dev_get_drvdata(dev);
+
+	rtd->offset = secs - ktime_get_real_seconds();
+
 	return 0;
 }
 
@@ -88,21 +98,18 @@ static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
 
 static int test_probe(struct platform_device *plat_dev)
 {
-	int err;
-	struct rtc_device *rtc;
+	struct rtc_test_data *rtd;
 
-	rtc = devm_rtc_device_register(&plat_dev->dev, "test",
-				&test_rtc_ops, THIS_MODULE);
-	if (IS_ERR(rtc)) {
-		return PTR_ERR(rtc);
-	}
+	rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL);
+	if (!rtd)
+		return -ENOMEM;
 
-	err = device_create_file(&plat_dev->dev, &dev_attr_irq);
-	if (err)
-		dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
-			dev_attr_irq.attr.name);
+	platform_set_drvdata(plat_dev, rtd);
 
-	platform_set_drvdata(plat_dev, rtc);
+	rtd->rtc = devm_rtc_device_register(&plat_dev->dev, "test",
+					    &test_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtd->rtc))
+		return PTR_ERR(rtd->rtc);
 
 	return 0;
 }
Signature · 1 line
2.17.0
author maintainer
#3 lore raw

PATCH 4/5 rtc: test: emulate alarms using timers

In reply to Alexandre Belloni

Use timers to emulate alarms. Note that multiple alarms may happen if they are set more than 15 days after the current RTC time.

---
 drivers/rtc/rtc-test.c | 56 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 4 deletions(-)
drivers/rtc/rtc-test.c +52 -4
index 041150e53fd7..975f6cdda73f 100644
@@ -18,19 +18,49 @@
 struct rtc_test_data {
 	struct rtc_device *rtc;
 	time64_t offset;
+	struct timer_list alarm;
+	bool alarm_en;
 };
 
 struct platform_device *pdev[MAX_RTC_TEST];
 
-static int test_rtc_read_alarm(struct device *dev,
-	struct rtc_wkalrm *alrm)
+static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
+	struct rtc_test_data *rtd = dev_get_drvdata(dev);
+	time64_t alarm;
+
+	alarm = (rtd->alarm.expires - jiffies) / HZ;
+	alarm += ktime_get_real_seconds() + rtd->offset;
+
+	rtc_time64_to_tm(alarm, &alrm->time);
+	alrm->enabled = rtd->alarm_en;
+
 	return 0;
 }
 
-static int test_rtc_set_alarm(struct device *dev,
-	struct rtc_wkalrm *alrm)
+static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
+	struct rtc_test_data *rtd = dev_get_drvdata(dev);
+	ktime_t timeout;
+	u64 expires;
+
+	timeout = rtc_tm_to_time64(&alrm->time) - ktime_get_real_seconds();
+	timeout -= rtd->offset;
+
+	del_timer(&rtd->alarm);
+
+	expires = jiffies + timeout * HZ;
+	if (expires > U32_MAX)
+		expires = U32_MAX;
+
+	pr_err("ABE: %s +%d %s\n", __FILE__, __LINE__, __func__);
+	rtd->alarm.expires = expires;
+
+	if (alrm->enabled)
+		add_timer(&rtd->alarm);
+
+	rtd->alarm_en = alrm->enabled;
+
 	return 0;
 }
 
@@ -54,6 +84,14 @@ static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
 
 static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
 {
+	struct rtc_test_data *rtd = dev_get_drvdata(dev);
+
+	rtd->alarm_en = enable;
+	if (enable)
+		add_timer(&rtd->alarm);
+	else
+		del_timer(&rtd->alarm);
+
 	return 0;
 }
 
@@ -65,6 +103,13 @@ static const struct rtc_class_ops test_rtc_ops = {
 	.alarm_irq_enable = test_rtc_alarm_irq_enable,
 };
 
+static void test_rtc_alarm_handler(struct timer_list *t)
+{
+	struct rtc_test_data *rtd = from_timer(rtd, t, alarm);
+
+	rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF);
+}
+
 static ssize_t test_irq_show(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
@@ -111,6 +156,9 @@ static int test_probe(struct platform_device *plat_dev)
 	if (IS_ERR(rtd->rtc))
 		return PTR_ERR(rtd->rtc);
 
+	timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0);
+	rtd->alarm.expires = 0;
+
 	return 0;
 }
 
Signature · 1 line
2.17.0
author maintainer
#4 lore raw

PATCH 5/5 rtc: test: remove irq sysfs file

In reply to Alexandre Belloni

Now that alarms are emulated, remove the irq sysfs file that could be used to send alarms.

---
 drivers/rtc/rtc-test.c | 39 ---------------------------------------
 1 file changed, 39 deletions(-)
drivers/rtc/rtc-test.c +0 -39
index 975f6cdda73f..fbcec4a2e3c9 100644
@@ -110,37 +110,6 @@ static void test_rtc_alarm_handler(struct timer_list *t)
 	rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF);
 }
 
-static ssize_t test_irq_show(struct device *dev,
-				struct device_attribute *attr, char *buf)
-{
-	return sprintf(buf, "%d\n", 42);
-}
-static ssize_t test_irq_store(struct device *dev,
-				struct device_attribute *attr,
-				const char *buf, size_t count)
-{
-	int retval;
-	struct rtc_device *rtc = dev_get_drvdata(dev);
-
-	retval = count;
-	if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
-		rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
-	else if (strncmp(buf, "alarm", 5) == 0) {
-		struct rtc_wkalrm alrm;
-		int err = rtc_read_alarm(rtc, &alrm);
-
-		if (!err && alrm.enabled)
-			rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
-
-	} else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
-		rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
-	else
-		retval = -EINVAL;
-
-	return retval;
-}
-static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
-
 static int test_probe(struct platform_device *plat_dev)
 {
 	struct rtc_test_data *rtd;
@@ -162,16 +131,8 @@ static int test_probe(struct platform_device *plat_dev)
 	return 0;
 }
 
-static int test_remove(struct platform_device *plat_dev)
-{
-	device_remove_file(&plat_dev->dev, &dev_attr_irq);
-
-	return 0;
-}
-
 static struct platform_driver test_driver = {
 	.probe	= test_probe,
-	.remove = test_remove,
 	.driver = {
 		.name = "rtc-test",
 	},
Signature · 1 line
2.17.0