Blob Blame History Raw
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
Date: Wed, 28 Feb 2018 21:58:02 +0100
Subject: rtc: nvmem: allow registering the nvmem device before the rtc

Git-commit: ac75779b72fd52741d7446ad27cf6d58c006c643
Patch-mainline: v4.17-rc1
References: fate#326530,fate#326531,fate#326535,fate#326538,fate#326539

Switch the parent of the nvmem device to the parent of the rtc device so it
can be registered before the RTC.

This is a small change in the ABI as the nvmem moves out of the
/sys/class/rtc/rtcX folder to be under the parent device folder (that is
where the previous nvram files where registered).

However, it is still available under its correct location,
/sys/bus/nvmem/devices which is the one that should be used by userspace
applications.

The other benefit is that the nvmem device can stay registered even if the
rtc registration fails. Or it is possible to not register the rtc if the
nvmem registration failed.

Finally, it makes a lot of sense for devices that actually have different
i2c or spi addresses for the RTC and the EEPROM. That is basically how it
would end up when using MFD or even completely separate devices.

Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Mian Yousaf Kaukab <yousaf.kaukab@suse.com>
---
 drivers/rtc/class.c |    5 +++--
 drivers/rtc/nvmem.c |    2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -290,8 +290,6 @@ EXPORT_SYMBOL_GPL(rtc_device_register);
  */
 void rtc_device_unregister(struct rtc_device *rtc)
 {
-	rtc_nvmem_unregister(rtc);
-
 	mutex_lock(&rtc->ops_lock);
 	/*
 	 * Remove innards of this RTC, then disable it, before
@@ -309,6 +307,7 @@ static void devm_rtc_device_release(stru
 {
 	struct rtc_device *rtc = *(struct rtc_device **)res;
 
+	rtc_nvmem_unregister(rtc);
 	rtc_device_unregister(rtc);
 }
 
@@ -379,6 +378,8 @@ static void devm_rtc_release_device(stru
 {
 	struct rtc_device *rtc = *(struct rtc_device **)res;
 
+	rtc_nvmem_unregister(rtc);
+
 	if (rtc->registered)
 		rtc_device_unregister(rtc);
 	else
--- a/drivers/rtc/nvmem.c
+++ b/drivers/rtc/nvmem.c
@@ -93,7 +93,7 @@ int rtc_nvmem_register(struct rtc_device
 	if (!nvmem_config)
 		return -ENODEV;
 
-	nvmem_config->dev = &rtc->dev;
+	nvmem_config->dev = rtc->dev.parent;
 	nvmem_config->owner = rtc->owner;
 	rtc->nvmem = nvmem_register(nvmem_config);
 	if (IS_ERR_OR_NULL(rtc->nvmem))