Re: [PATCH 2/3] rtc: add rtc controller support for Sophgo CV1800B SoC

From: jingbao qiu
Date: Tue Nov 28 2023 - 08:23:07 EST


On Tue, Nov 21, 2023 at 6:01 PM Krzysztof Kozlowski
<krzysztof.kozlowski@xxxxxxxxxx> wrote:
>
> On 21/11/2023 10:46, Jingbao Qiu wrote:
> > Implement the RTC driver for CV1800B, which able to provide time and
> > alarm functionality.
> >
> > Signed-off-by: Jingbao Qiu <qiujingbao.dlmu@xxxxxxxxx>
> > ---
> > drivers/rtc/Kconfig | 10 ++
> > drivers/rtc/Makefile | 1 +
> > drivers/rtc/rtc-cv1800b.c | 293 ++++++++++++++++++++++++++++++++++++++
> > 3 files changed, 304 insertions(+)
> > create mode 100644 drivers/rtc/rtc-cv1800b.c
>
> Bindings were not tested, so I assume you did not compile the code
> either. Please confirm that you fixed all warnings pointed out by W=1
> builds, smatch and sparse. All of them.

will test & fix in next version

>
> >
> > diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> > index 3814e0845e77..2089cceea38c 100644
> > --- a/drivers/rtc/Kconfig
> > +++ b/drivers/rtc/Kconfig
> > @@ -1103,6 +1103,16 @@ config RTC_DRV_DS2404
> > This driver can also be built as a module. If so, the module
> > will be called rtc-ds2404.
> >
> > +config RTC_DRV_CV1800B
> > + tristate "Sophgo CV1800B RTC"
> > + depends on ARCH_SOPHGO || COMPILE_TEST
> > + help
> > + If you say yes here you will get support for the
> > + RTC of the Sophgo CV1800B SOC.
> > +
> > + This depend on ARCH_SOPHGO and COMPILE_TEST. Please
> > + first config that.
>
> ...
>
> > +static int cv1800b_rtc_probe(struct platform_device *pdev)
> > +{
> > + struct cv1800b_rtc_priv *rtc;
> > + struct resource *res;
> > + int ret;
> > +
> > + rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
> > + if (!rtc)
> > + return -ENOMEM;
> > +
> > + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > + if (!res) {
> > + ret = -ENODEV;
> > + goto err;
> > + }
> > +
> > + rtc->core_map = devm_ioremap_resource(&pdev->dev, res);
>
> Use helper combining these two calls.

Ok, will use devm_platform_ioremap_resource() to replace it.

>
> > + if (IS_ERR(rtc->core_map)) {
> > + ret = PTR_ERR(rtc->core_map);
> > + goto err;
> > + }
> > +
> > + rtc->irq = platform_get_irq(pdev, 0);
> > + platform_set_drvdata(pdev, rtc);
>
> Your code has random order. First you get IRQ, then you check its value,
> then you go further.

ok

>
> > + if (rtc->irq < 0) {
> > + ret = -EINVAL;
> > + goto err;
> > + }
> > +
> > + ret =
> > + devm_request_irq(&pdev->dev, rtc->irq, cv1800b_rtc_irq_handler,
>
> Wrong wrapping.

ok

>
> > + IRQF_SHARED, "rtc alarm", &pdev->dev);
>
> Why shared?

ok

>
> > + if (ret)
> > + goto err;
> > +
> > + rtc->clk = devm_clk_get(&pdev->dev, NULL);
> > + if (IS_ERR(rtc->clk)) {
> > + dev_err(&pdev->dev, "no clock");
>
> This code is not ready for upstream. There are multiple things wrong here.
>
> First, syntax is return dev_err_probe.
>
> Second, you do not have clocks and you do not allow them! Just open your
> binding.

I'm not fully understanding here, can you elaborate more?
as there is clocks info like this in the dt-bindings:
clocks = <&osc>;

>
> Third, use wrapper - devm_clk_get_enable or something like that.

I will use devm_clk_get_enabled() to replace it.

>
>
> > + ret = PTR_ERR(rtc->clk);
> > + goto err;
> > + }
>
> Blank line.

ok

>
> > + ret = clk_prepare_enable(rtc->clk);
> > + if (ret)
> > + goto err;
>
> Blank line.

ok

>
> > + ret = cv1800b_rtc_softinit(rtc);
> > + if (ret)
> > + goto err;
> > + cv1800b_rtc_alarm_irq_enable(&pdev->dev, 1);
> > + rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
> > + if (IS_ERR(rtc->rtc_dev)) {
> > + ret = PTR_ERR(rtc->rtc_dev);
> > + goto err;
> > + }
> > + rtc->rtc_dev->range_max = U32_MAX;
> > + rtc->rtc_dev->ops = &cv800b_rtc_ops;
> > +
> > + return rtc_register_device(rtc->rtc_dev);

I find the commet of devm_rtc_device_register wirte
“This function is deprecated, use devm_rtc_allocate_device and
rtc_register_device instead”
but all of code about this, they all use devm_rtc_device_register
function. So which one do you suggest I use?

> > +err:
> > + return dev_err_probe(&pdev->dev, ret, "Failed to init cv1800b rtc\n");
>
> Drop, just return.

ok

>
> Best regards,
> Krzysztof
>

Best regards,
Jingbao Qiu