diff mbox

[v3,2/8] MIPS: ranchu: Add Goldfish RTC driver

Message ID 1500656111-9520-3-git-send-email-aleksandar.markovic@rt-rk.com
State Changes Requested
Headers show

Commit Message

Aleksandar Markovic July 21, 2017, 4:53 p.m. UTC
From: Miodrag Dinic <miodrag.dinic@imgtec.com>

Add device driver for a virtual Goldfish RTC clock.

The driver can be built only if CONFIG_MIPS and CONFIG_GOLDFISH are
set. The compatible string used by OS for binding the driver is
defined as "google,goldfish-rtc".

Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
---
 MAINTAINERS                |   1 +
 drivers/rtc/Kconfig        |   8 ++
 drivers/rtc/Makefile       |   1 +
 drivers/rtc/rtc-goldfish.c | 233 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 243 insertions(+)
 create mode 100644 drivers/rtc/rtc-goldfish.c

Comments

kernel test robot July 23, 2017, 11:50 p.m. UTC | #1
Hi Miodrag,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.13-rc1 next-20170721]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Aleksandar-Markovic/MIPS-Add-virtual-Ranchu-board-as-a-generic-based-board/20170724-062318
config: blackfin-allyesconfig (attached as .config)
compiler: bfin-uclinux-gcc (GCC) 6.2.0
reproduce:
        wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=blackfin 

All warnings (new ones prefixed by >>):

   drivers/rtc/rtc-goldfish.c: In function 'goldfish_rtc_set_alarm':
>> drivers/rtc/rtc-goldfish.c:86:21: warning: right shift count >= width of type [-Wshift-count-overflow]
      writel((rtc_alarm >> 32), base + TIMER_ALARM_HIGH);
                        ^~
   drivers/rtc/rtc-goldfish.c: In function 'goldfish_rtc_set_time':
   drivers/rtc/rtc-goldfish.c:167:15: warning: right shift count >= width of type [-Wshift-count-overflow]
      writel((now >> 32), base + TIMER_TIME_HIGH);
                  ^~

vim +86 drivers/rtc/rtc-goldfish.c

    67	
    68	static int goldfish_rtc_set_alarm(struct device *dev,
    69			struct rtc_wkalrm *alrm)
    70	{
    71		struct goldfish_rtc *rtcdrv;
    72		unsigned long rtc_alarm;
    73		u64 rtc_status_reg;
    74		void __iomem *base;
    75		int ret = 0;
    76	
    77		rtcdrv = dev_get_drvdata(dev);
    78		base = rtcdrv->base;
    79	
    80		if (alrm->enabled) {
    81			ret = rtc_tm_to_time(&(alrm->time), &rtc_alarm);
    82			if (ret != 0)
    83				return ret;
    84	
    85			rtc_alarm *= NSEC_PER_SEC;
  > 86			writel((rtc_alarm >> 32), base + TIMER_ALARM_HIGH);
    87			writel(rtc_alarm, base + TIMER_ALARM_LOW);
    88		} else {
    89			/*
    90			 * if this function was called with enabled=0
    91			 * then it could mean that the application is
    92			 * trying to cancel an ongoing alarm
    93			 */
    94			rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
    95			if (rtc_status_reg)
    96				writel(1, base + TIMER_CLEAR_ALARM);
    97		}
    98	
    99		return ret;
   100	}
   101	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
kernel test robot July 24, 2017, 2:14 a.m. UTC | #2
Hi Miodrag,

[auto build test ERROR on linus/master]
[also build test ERROR on next-20170721]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Aleksandar-Markovic/MIPS-Add-virtual-Ranchu-board-as-a-generic-based-board/20170724-062318
config: sparc64-allmodconfig (attached as .config)
compiler: sparc64-linux-gnu-gcc (Debian 6.3.0-18) 6.3.0 20170516
reproduce:
        wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=sparc64 

All errors (new ones prefixed by >>):

   drivers/rtc/rtc-goldfish.c: In function 'goldfish_rtc_read_alarm':
>> drivers/rtc/rtc-goldfish.c:51:18: error: implicit declaration of function 'readl' [-Werror=implicit-function-declaration]
     rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
                     ^~~~~
   drivers/rtc/rtc-goldfish.c: In function 'goldfish_rtc_set_alarm':
>> drivers/rtc/rtc-goldfish.c:86:3: error: implicit declaration of function 'writel' [-Werror=implicit-function-declaration]
      writel((rtc_alarm >> 32), base + TIMER_ALARM_HIGH);
      ^~~~~~
   cc1: some warnings being treated as errors

vim +/readl +51 drivers/rtc/rtc-goldfish.c

    38	
    39	static int goldfish_rtc_read_alarm(struct device *dev,
    40			struct rtc_wkalrm *alrm)
    41	{
    42		u64 rtc_alarm;
    43		u64 rtc_alarm_low;
    44		u64 rtc_alarm_high;
    45		void __iomem *base;
    46		struct goldfish_rtc *rtcdrv;
    47	
    48		rtcdrv = dev_get_drvdata(dev);
    49		base = rtcdrv->base;
    50	
  > 51		rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
    52		rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
    53		rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
    54	
    55		do_div(rtc_alarm, NSEC_PER_SEC);
    56		memset(alrm, 0, sizeof(struct rtc_wkalrm));
    57	
    58		rtc_time_to_tm(rtc_alarm, &(alrm->time));
    59	
    60		if (readl(base + TIMER_ALARM_STATUS))
    61			alrm->enabled = 1;
    62		else
    63			alrm->enabled = 0;
    64	
    65		return 0;
    66	}
    67	
    68	static int goldfish_rtc_set_alarm(struct device *dev,
    69			struct rtc_wkalrm *alrm)
    70	{
    71		struct goldfish_rtc *rtcdrv;
    72		unsigned long rtc_alarm;
    73		u64 rtc_status_reg;
    74		void __iomem *base;
    75		int ret = 0;
    76	
    77		rtcdrv = dev_get_drvdata(dev);
    78		base = rtcdrv->base;
    79	
    80		if (alrm->enabled) {
    81			ret = rtc_tm_to_time(&(alrm->time), &rtc_alarm);
    82			if (ret != 0)
    83				return ret;
    84	
    85			rtc_alarm *= NSEC_PER_SEC;
  > 86			writel((rtc_alarm >> 32), base + TIMER_ALARM_HIGH);
    87			writel(rtc_alarm, base + TIMER_ALARM_LOW);
    88		} else {
    89			/*
    90			 * if this function was called with enabled=0
    91			 * then it could mean that the application is
    92			 * trying to cancel an ongoing alarm
    93			 */
    94			rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
    95			if (rtc_status_reg)
    96				writel(1, base + TIMER_CLEAR_ALARM);
    97		}
    98	
    99		return ret;
   100	}
   101	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Alexandre Belloni Aug. 10, 2017, 8:34 p.m. UTC | #3
Hi,

On 21/07/2017 at 18:53:31 +0200, Aleksandar Markovic wrote:
> From: Miodrag Dinic <miodrag.dinic@imgtec.com>
> 
> Add device driver for a virtual Goldfish RTC clock.
> 
> The driver can be built only if CONFIG_MIPS and CONFIG_GOLDFISH are
> set. The compatible string used by OS for binding the driver is
> defined as "google,goldfish-rtc".
> 
> Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
> Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
> Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
> ---
>  MAINTAINERS                |   1 +
>  drivers/rtc/Kconfig        |   8 ++
>  drivers/rtc/Makefile       |   1 +
>  drivers/rtc/rtc-goldfish.c | 233 +++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 243 insertions(+)
>  create mode 100644 drivers/rtc/rtc-goldfish.c
> 

Do you mind fixing the remaining checkpatch --strict issues, the two
kbuild errors and the warning reported by Julia?

Thanks!
Miodrag Dinic Aug. 11, 2017, 4:46 p.m. UTC | #4
Hi Alexandre,

we have this sorted & plan to submit version 4 of this series next week.

Thank you.

Kind regards,
Miodrag
Aleksandar Markovic Aug. 14, 2017, 12:45 p.m. UTC | #5
Hello, Alexandre,

In addition, if you, and everybody else, don't object, I plan to change the title of this patch form:

"MIPS: ranchu: Add Goldfish RTC driver"

to

"rtc: Add Goldfish RTC driver",

to be more consistent with all commit messages for this kernel source directory.

Regards,
Aleksandar
Alexandre Belloni Aug. 14, 2017, 1:13 p.m. UTC | #6
On 14/08/2017 at 12:45:55 +0000, Aleksandar Markovic wrote:
> Hello, Alexandre,
> 
> In addition, if you, and everybody else, don't object, I plan to change the title of this patch form:
> 
> "MIPS: ranchu: Add Goldfish RTC driver"
> 
> to
> 
> "rtc: Add Goldfish RTC driver",
> 
> to be more consistent with all commit messages for this kernel source directory.
> 

Yes please, I would have changed it anyway.

> Regards,
> Aleksandar
> 
> ________________________________________
> From: Miodrag Dinic
> Sent: Friday, August 11, 2017 9:46 AM
> To: Alexandre Belloni; Aleksandar Markovic
> Cc: linux-mips@linux-mips.org; Goran Ferenc; Aleksandar Markovic; Alessandro Zummo; Bo Hu; David S. Miller; Douglas Leung; Greg Kroah-Hartman; James Hogan; Jin Qian; linux-kernel@vger.kernel.org; linux-rtc@vger.kernel.org; Mauro Carvalho Chehab; Paul Burton; Petar Jovanovic; Raghu Gandham
> Subject: RE: [PATCH v3 2/8] MIPS: ranchu: Add Goldfish RTC driver
> 
> Hi Alexandre,
> 
> we have this sorted & plan to submit version 4 of this series next week.
> 
> Thank you.
> 
> Kind regards,
> Miodrag
> ________________________________________
> From: Alexandre Belloni [alexandre.belloni@free-electrons.com]
> Sent: Thursday, August 10, 2017 10:34 PM
> To: Aleksandar Markovic
> Cc: linux-mips@linux-mips.org; Miodrag Dinic; Goran Ferenc; Aleksandar Markovic; Alessandro Zummo; Bo Hu; David S. Miller; Douglas Leung; Greg Kroah-Hartman; James Hogan; Jin Qian; linux-kernel@vger.kernel.org; linux-rtc@vger.kernel.org; Mauro Carvalho Chehab; Paul Burton; Petar Jovanovic; Raghu Gandham
> Subject: Re: [PATCH v3 2/8] MIPS: ranchu: Add Goldfish RTC driver
> 
> Hi,
> 
> On 21/07/2017 at 18:53:31 +0200, Aleksandar Markovic wrote:
> > From: Miodrag Dinic <miodrag.dinic@imgtec.com>
> >
> > Add device driver for a virtual Goldfish RTC clock.
> >
> > The driver can be built only if CONFIG_MIPS and CONFIG_GOLDFISH are
> > set. The compatible string used by OS for binding the driver is
> > defined as "google,goldfish-rtc".
> >
> > Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
> > Signed-off-by: Goran Ferenc <goran.ferenc@imgtec.com>
> > Signed-off-by: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
> > ---
> >  MAINTAINERS                |   1 +
> >  drivers/rtc/Kconfig        |   8 ++
> >  drivers/rtc/Makefile       |   1 +
> >  drivers/rtc/rtc-goldfish.c | 233 +++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 243 insertions(+)
> >  create mode 100644 drivers/rtc/rtc-goldfish.c
> >
> 
> Do you mind fixing the remaining checkpatch --strict issues, the two
> kbuild errors and the warning reported by Julia?
> 
> Thanks!
> 
> 
> --
> Alexandre Belloni, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com
diff mbox

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 847da3f..768426d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -845,6 +845,7 @@  ANDROID GOLDFISH RTC DRIVER
 M:	Miodrag Dinic <miodrag.dinic@imgtec.com>
 S:	Supported
 F:	Documentation/devicetree/bindings/rtc/google,goldfish-rtc.txt
+F:	drivers/rtc/rtc-goldfish.c
 
 ANDROID ION DRIVER
 M:	Laura Abbott <labbott@redhat.com>
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 72419ac..7cd27d3 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1780,5 +1780,13 @@  config RTC_DRV_HID_SENSOR_TIME
 	  If this driver is compiled as a module, it will be named
 	  rtc-hid-sensor-time.
 
+config RTC_DRV_GOLDFISH
+	tristate "Goldfish Real Time Clock"
+	depends on MIPS && GOLDFISH || COMPILE_TEST
+	help
+	  Say yes to enable RTC driver for the Goldfish based virtual platform.
+
+	  Goldfish is a code name for the virtual platform developed by Google
+	  for Android emulation.
 
 endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index acd366b..d995d49 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -170,3 +170,4 @@  obj-$(CONFIG_RTC_DRV_WM8350)	+= rtc-wm8350.o
 obj-$(CONFIG_RTC_DRV_X1205)	+= rtc-x1205.o
 obj-$(CONFIG_RTC_DRV_XGENE)	+= rtc-xgene.o
 obj-$(CONFIG_RTC_DRV_ZYNQMP)	+= rtc-zynqmp.o
+obj-$(CONFIG_RTC_DRV_GOLDFISH)	+= rtc-goldfish.o
diff --git a/drivers/rtc/rtc-goldfish.c b/drivers/rtc/rtc-goldfish.c
new file mode 100644
index 0000000..aa13062
--- /dev/null
+++ b/drivers/rtc/rtc-goldfish.c
@@ -0,0 +1,233 @@ 
+/* drivers/rtc/rtc-goldfish.c
+ *
+ * Copyright (C) 2007 Google, Inc.
+ * Copyright (C) 2017 Imagination Technologies Ltd.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+
+#define TIMER_TIME_LOW		0x00	/* get low bits of current time  */
+					/*   and update TIMER_TIME_HIGH  */
+#define TIMER_TIME_HIGH	0x04	/* get high bits of time at last */
+					/*   TIMER_TIME_LOW read         */
+#define TIMER_ALARM_LOW	0x08	/* set low bits of alarm and     */
+					/*   activate it                 */
+#define TIMER_ALARM_HIGH	0x0c	/* set high bits of next alarm   */
+#define TIMER_IRQ_ENABLED	0x10
+#define TIMER_CLEAR_ALARM	0x14
+#define TIMER_ALARM_STATUS	0x18
+#define TIMER_CLEAR_INTERRUPT	0x1c
+
+struct goldfish_rtc {
+	void __iomem *base;
+	u32 irq;
+	struct rtc_device *rtc;
+};
+
+static int goldfish_rtc_read_alarm(struct device *dev,
+		struct rtc_wkalrm *alrm)
+{
+	u64 rtc_alarm;
+	u64 rtc_alarm_low;
+	u64 rtc_alarm_high;
+	void __iomem *base;
+	struct goldfish_rtc *rtcdrv;
+
+	rtcdrv = dev_get_drvdata(dev);
+	base = rtcdrv->base;
+
+	rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
+	rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
+	rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
+
+	do_div(rtc_alarm, NSEC_PER_SEC);
+	memset(alrm, 0, sizeof(struct rtc_wkalrm));
+
+	rtc_time_to_tm(rtc_alarm, &(alrm->time));
+
+	if (readl(base + TIMER_ALARM_STATUS))
+		alrm->enabled = 1;
+	else
+		alrm->enabled = 0;
+
+	return 0;
+}
+
+static int goldfish_rtc_set_alarm(struct device *dev,
+		struct rtc_wkalrm *alrm)
+{
+	struct goldfish_rtc *rtcdrv;
+	unsigned long rtc_alarm;
+	u64 rtc_status_reg;
+	void __iomem *base;
+	int ret = 0;
+
+	rtcdrv = dev_get_drvdata(dev);
+	base = rtcdrv->base;
+
+	if (alrm->enabled) {
+		ret = rtc_tm_to_time(&(alrm->time), &rtc_alarm);
+		if (ret != 0)
+			return ret;
+
+		rtc_alarm *= NSEC_PER_SEC;
+		writel((rtc_alarm >> 32), base + TIMER_ALARM_HIGH);
+		writel(rtc_alarm, base + TIMER_ALARM_LOW);
+	} else {
+		/*
+		 * if this function was called with enabled=0
+		 * then it could mean that the application is
+		 * trying to cancel an ongoing alarm
+		 */
+		rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
+		if (rtc_status_reg)
+			writel(1, base + TIMER_CLEAR_ALARM);
+	}
+
+	return ret;
+}
+
+static int goldfish_rtc_alarm_irq_enable(struct device *dev,
+		unsigned int enabled)
+{
+	void __iomem *base;
+	struct goldfish_rtc *rtcdrv;
+
+	rtcdrv = dev_get_drvdata(dev);
+	base = rtcdrv->base;
+
+	if (enabled)
+		writel(1, base + TIMER_IRQ_ENABLED);
+	else
+		writel(0, base + TIMER_IRQ_ENABLED);
+
+	return 0;
+
+}
+
+static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
+{
+	struct goldfish_rtc *rtcdrv = dev_id;
+	void __iomem *base = rtcdrv->base;
+
+	writel(1, base + TIMER_CLEAR_INTERRUPT);
+
+	rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
+
+	return IRQ_HANDLED;
+}
+
+static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct goldfish_rtc *rtcdrv;
+	void __iomem *base;
+	u64 time_high;
+	u64 time_low;
+	u64 time;
+
+	rtcdrv = dev_get_drvdata(dev);
+	base = rtcdrv->base;
+
+	time_low = readl(base + TIMER_TIME_LOW);
+	time_high = readl(base + TIMER_TIME_HIGH);
+	time = (time_high << 32) | time_low;
+
+	do_div(time, NSEC_PER_SEC);
+
+	rtc_time_to_tm(time, tm);
+
+	return 0;
+}
+
+static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct goldfish_rtc *rtcdrv;
+	void __iomem *base;
+	unsigned long now;
+	int ret;
+
+	rtcdrv = dev_get_drvdata(dev);
+	base = rtcdrv->base;
+
+	ret = rtc_tm_to_time(tm, &now);
+	if (ret == 0) {
+		now *= NSEC_PER_SEC;
+		writel((now >> 32), base + TIMER_TIME_HIGH);
+		writel(now, base + TIMER_TIME_LOW);
+	}
+
+	return ret;
+}
+
+static const struct rtc_class_ops goldfish_rtc_ops = {
+	.read_time	= goldfish_rtc_read_time,
+	.set_time	= goldfish_rtc_set_time,
+	.read_alarm	= goldfish_rtc_read_alarm,
+	.set_alarm	= goldfish_rtc_set_alarm,
+	.alarm_irq_enable = goldfish_rtc_alarm_irq_enable
+};
+
+static int goldfish_rtc_probe(struct platform_device *pdev)
+{
+	struct resource *r;
+	struct goldfish_rtc *rtcdrv;
+	int err;
+
+	rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
+	if (rtcdrv == NULL)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, rtcdrv);
+
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (r == NULL)
+		return -ENODEV;
+
+	rtcdrv->base = devm_ioremap_resource(&pdev->dev, r);
+	if (IS_ERR(rtcdrv->base))
+		return -ENODEV;
+
+	rtcdrv->irq = platform_get_irq(pdev, 0);
+	if (rtcdrv->irq < 0)
+		return -ENODEV;
+
+	rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
+					&goldfish_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtcdrv->rtc))
+		return PTR_ERR(rtcdrv->rtc);
+
+	err = devm_request_irq(&pdev->dev, rtcdrv->irq, goldfish_rtc_interrupt,
+		0, pdev->name, rtcdrv);
+	if (err)
+		return err;
+
+	return 0;
+}
+
+static const struct of_device_id goldfish_rtc_of_match[] = {
+	{ .compatible = "google,goldfish-rtc", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
+
+static struct platform_driver goldfish_rtc = {
+	.probe = goldfish_rtc_probe,
+	.driver = {
+		.name = "goldfish_rtc",
+		.of_match_table = goldfish_rtc_of_match,
+	}
+};
+
+module_platform_driver(goldfish_rtc);