Message ID | 20221121065144.3667658-4-ping.bai@nxp.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Add nxp bbnsm module support | expand |
On 21/11/2022 14:51:43+0800, Jacky Bai wrote: > The BBNSM module includes a real time counter with alarm. > Add a RTC driver for this function. > > Signed-off-by: Jacky Bai <ping.bai@nxp.com> > Reviewed-by: Peng Fan <peng.fan@nxp.com> > --- > drivers/rtc/Kconfig | 12 +++ > drivers/rtc/Makefile | 1 + > drivers/rtc/rtc-bbnsm.c | 223 ++++++++++++++++++++++++++++++++++++++++ I'd prefer that filename to include imx or mxc if this is only available on those SoCs. > diff --git a/drivers/rtc/rtc-bbnsm.c b/drivers/rtc/rtc-bbnsm.c > new file mode 100644 > index 000000000000..4157b238ed9a > --- /dev/null > +++ b/drivers/rtc/rtc-bbnsm.c > @@ -0,0 +1,223 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +// > +// Copyright 2022 NXP. > + > +#include <linux/init.h> > +#include <linux/io.h> > +#include <linux/kernel.h> > +#include <linux/module.h> > +#include <linux/of.h> > +#include <linux/platform_device.h> > +#include <linux/pm_wakeirq.h> > +#include <linux/rtc.h> > +#include <linux/mfd/syscon.h> > +#include <linux/regmap.h> This list should be sorted alphabetically > + > +#define BBNSM_CTRL 0x8 > +#define BBNSM_INT_EN 0x10 > +#define BBNSM_EVENTS 0x14 > +#define BBNSM_RTC_LS 0x40 > +#define BBNSM_RTC_MS 0x44 > +#define BBNSM_TA 0x50 > + > +#define RTC_EN 0x2 > +#define RTC_EN_MSK 0x3 > +#define TA_EN (0x2 << 2) > +#define TA_DIS (0x1 << 2) > +#define TA_EN_MSK (0x3 << 2) > +#define RTC_INT_EN 0x2 > +#define TA_INT_EN (0x2 << 2) > + > +#define BBNSM_EVENT_TA TA_EN > + I'm not clear why this define is needed > +static irqreturn_t bbnsm_rtc_irq_handler(int irq, void *dev_id) > +{ > + struct device *dev = dev_id; > + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); > + u32 val; > + u32 event = 0; You can rework this function to remove this variable because it is either 0 or RTC_AF | RTC_IRQF > + > + regmap_read(bbnsm->regmap, BBNSM_EVENTS, &val); > + if (val & BBNSM_EVENT_TA) { > + event |= (RTC_AF | RTC_IRQF); > + bbnsm_rtc_alarm_irq_enable(dev, false); > + /* clear the alarm event */ > + regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, TA_EN_MSK, BBNSM_EVENT_TA); > + rtc_update_irq(bbnsm->rtc, 1, event); > + } > + > + return event ? IRQ_HANDLED : IRQ_NONE; > +} > + > +static int bbnsm_rtc_probe(struct platform_device *pdev) > +{ > + struct bbnsm_rtc *bbnsm; > + int ret; > + > + bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL); > + if (!bbnsm) > + return -ENOMEM; > + > + bbnsm->rtc = devm_rtc_allocate_device(&pdev->dev); > + > + bbnsm->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap"); > + if (IS_ERR(bbnsm->regmap)) { > + dev_err(&pdev->dev, "bbnsm get regmap failed\n"); > + return PTR_ERR(bbnsm->regmap); > + } > + > + bbnsm->irq = platform_get_irq(pdev, 0); > + if (bbnsm->irq < 0) > + return bbnsm->irq; > + > + platform_set_drvdata(pdev, bbnsm); > + > + /* clear all the pending events */ > + regmap_write(bbnsm->regmap, BBNSM_EVENTS, 0x7A); > + > + /* Enable the Real-Time counter */ > + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, RTC_EN_MSK, RTC_EN); > + Please don't do that, this removes an important piece of information. Instead, let .set_time enable it and check it in .read_time as if this is not set, you now you are out of PoR and the time is invalid > + device_init_wakeup(&pdev->dev, true); > + ret = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq); > + if (ret) > + dev_err(&pdev->dev, "Failed to enable the irq wakeup\n"); dev_err but the function is not failing. Maybe just dev_warn? Also, is this message really necessary? > + > + ret = devm_request_irq(&pdev->dev, bbnsm->irq, bbnsm_rtc_irq_handler, > + IRQF_SHARED, "rtc alarm", &pdev->dev); > + if (ret) { > + dev_err(&pdev->dev, "failed to request irq %d: %d\n", > + bbnsm->irq, ret); > + return ret; > + } > + > + bbnsm->rtc->ops = &bbnsm_rtc_ops; > + bbnsm->rtc->range_max = U32_MAX; > + > + return devm_rtc_register_device(bbnsm->rtc); > +} > + > +static const struct of_device_id bbnsm_dt_ids[] = { > + { .compatible = "nxp,bbnsm-rtc", }, > + { /* sentinel */ }, > +}; > +MODULE_DEVICE_TABLE(of, bbnsm_dt_ids); > + > +static struct platform_driver bbnsm_rtc_driver = { > + .driver = { > + .name = "bbnsm_rtc", > + .of_match_table = bbnsm_dt_ids, > + }, > + .probe = bbnsm_rtc_probe, > +}; > +module_platform_driver(bbnsm_rtc_driver); > + > +MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>"); > +MODULE_DESCRIPTION("NXP BBNSM RTC Driver"); > +MODULE_LICENSE("GPL"); > -- > 2.37.1 >
> Subject: Re: [PATCH 3/4] rtc: bbnsm: Add the bbnsm rtc support > > On 21/11/2022 14:51:43+0800, Jacky Bai wrote: > > The BBNSM module includes a real time counter with alarm. > > Add a RTC driver for this function. > > > > Signed-off-by: Jacky Bai <ping.bai@nxp.com> > > Reviewed-by: Peng Fan <peng.fan@nxp.com> > > --- > > drivers/rtc/Kconfig | 12 +++ > > drivers/rtc/Makefile | 1 + > > drivers/rtc/rtc-bbnsm.c | 223 > > ++++++++++++++++++++++++++++++++++++++++ > > I'd prefer that filename to include imx or mxc if this is only available on those > SoCs. > For now, it is used on i.MX SoC, not sure if it will be used on NXP other SoC. I will update the file name to include 'imx' in v2. > > diff --git a/drivers/rtc/rtc-bbnsm.c b/drivers/rtc/rtc-bbnsm.c new > > file mode 100644 index 000000000000..4157b238ed9a > > --- /dev/null > > +++ b/drivers/rtc/rtc-bbnsm.c > > @@ -0,0 +1,223 @@ > > +// SPDX-License-Identifier: GPL-2.0+ > > +// > > +// Copyright 2022 NXP. > > + > > +#include <linux/init.h> > > +#include <linux/io.h> > > +#include <linux/kernel.h> > > +#include <linux/module.h> > > +#include <linux/of.h> > > +#include <linux/platform_device.h> > > +#include <linux/pm_wakeirq.h> > > +#include <linux/rtc.h> > > +#include <linux/mfd/syscon.h> > > +#include <linux/regmap.h> > > This list should be sorted alphabetically > Thx, will fix it in V2. > > > + > > +#define BBNSM_CTRL 0x8 > > +#define BBNSM_INT_EN 0x10 > > +#define BBNSM_EVENTS 0x14 > > +#define BBNSM_RTC_LS 0x40 > > +#define BBNSM_RTC_MS 0x44 > > +#define BBNSM_TA 0x50 > > + > > +#define RTC_EN 0x2 > > +#define RTC_EN_MSK 0x3 > > +#define TA_EN (0x2 << 2) > > +#define TA_DIS (0x1 << 2) > > +#define TA_EN_MSK (0x3 << 2) > > +#define RTC_INT_EN 0x2 > > +#define TA_INT_EN (0x2 << 2) > > + > > +#define BBNSM_EVENT_TA TA_EN > > + > > I'm not clear why this define is needed This define is for RTC alarm event status check, as it use the same bitfield offset as TA_EN, I just did the above define. It seems introduce some unnecessary confusion. > > > +static irqreturn_t bbnsm_rtc_irq_handler(int irq, void *dev_id) { > > + struct device *dev = dev_id; > > + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); > > + u32 val; > > + u32 event = 0; > > You can rework this function to remove this variable because it is either 0 or > RTC_AF | RTC_IRQF > Ok, will refine in V2. > > + > > + regmap_read(bbnsm->regmap, BBNSM_EVENTS, &val); > > + if (val & BBNSM_EVENT_TA) { > > + event |= (RTC_AF | RTC_IRQF); > > + bbnsm_rtc_alarm_irq_enable(dev, false); > > + /* clear the alarm event */ > > + regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, TA_EN_MSK, > BBNSM_EVENT_TA); > > + rtc_update_irq(bbnsm->rtc, 1, event); > > + } > > + > > + return event ? IRQ_HANDLED : IRQ_NONE; } > > + > > +static int bbnsm_rtc_probe(struct platform_device *pdev) { > > + struct bbnsm_rtc *bbnsm; > > + int ret; > > + > > + bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL); > > + if (!bbnsm) > > + return -ENOMEM; > > + > > + bbnsm->rtc = devm_rtc_allocate_device(&pdev->dev); > > + > > + bbnsm->regmap = > syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap"); > > + if (IS_ERR(bbnsm->regmap)) { > > + dev_err(&pdev->dev, "bbnsm get regmap failed\n"); > > + return PTR_ERR(bbnsm->regmap); > > + } > > + > > + bbnsm->irq = platform_get_irq(pdev, 0); > > + if (bbnsm->irq < 0) > > + return bbnsm->irq; > > + > > + platform_set_drvdata(pdev, bbnsm); > > + > > + /* clear all the pending events */ > > + regmap_write(bbnsm->regmap, BBNSM_EVENTS, 0x7A); > > + > > + /* Enable the Real-Time counter */ > > + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, RTC_EN_MSK, > RTC_EN); > > + > > Please don't do that, this removes an important piece of information. > Instead, let .set_time enable it and check it in .read_time as if this is not set, > you now you are out of PoR and the time is invalid > Will remove it. set_time has the enable operation. I will add enable check in read_time callback. > > + device_init_wakeup(&pdev->dev, true); > > + ret = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq); > > + if (ret) > > + dev_err(&pdev->dev, "Failed to enable the irq wakeup\n"); > > dev_err but the function is not failing. Maybe just dev_warn? Also, is this > message really necessary? > Not too necessary, I can drop the above log print. BR > > + > > + ret = devm_request_irq(&pdev->dev, bbnsm->irq, > bbnsm_rtc_irq_handler, > > + IRQF_SHARED, "rtc alarm", &pdev->dev); > > + if (ret) { > > + dev_err(&pdev->dev, "failed to request irq %d: %d\n", > > + bbnsm->irq, ret); > > + return ret; > > + } > > + > > + bbnsm->rtc->ops = &bbnsm_rtc_ops; > > + bbnsm->rtc->range_max = U32_MAX; > > + > > + return devm_rtc_register_device(bbnsm->rtc); > > +} > > + > > +static const struct of_device_id bbnsm_dt_ids[] = { > > + { .compatible = "nxp,bbnsm-rtc", }, > > + { /* sentinel */ }, > > +}; > > +MODULE_DEVICE_TABLE(of, bbnsm_dt_ids); > > + > > +static struct platform_driver bbnsm_rtc_driver = { > > + .driver = { > > + .name = "bbnsm_rtc", > > + .of_match_table = bbnsm_dt_ids, > > + }, > > + .probe = bbnsm_rtc_probe, > > +}; > > +module_platform_driver(bbnsm_rtc_driver); > > + > > +MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>"); > > +MODULE_DESCRIPTION("NXP BBNSM RTC Driver"); > MODULE_LICENSE("GPL"); > > -- > > 2.37.1 > > > > --
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index ab9a1f814119..0c8534a49c78 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1786,6 +1786,18 @@ config RTC_DRV_SNVS This driver can also be built as a module, if so, the module will be called "rtc-snvs". +config RTC_DRV_BBNSM + tristate "NXP BBNSM RTC support" + select REGMAP_MMIO + depends on ARCH_MXC || COMPILE_TEST + depends on HAS_IOMEM + depends on OF + help + If you say yes here you get support for the NXP BBNSM RTC module. + + This driver can also be built as a module, if so, the module + will be called "rtc-bbnsm". + config RTC_DRV_IMX_SC depends on IMX_SCU depends on HAVE_ARM_SMCCC diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index d3c042dcbc73..43bd29b2f42f 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_RTC_DRV_ASPEED) += rtc-aspeed.o obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o obj-$(CONFIG_RTC_DRV_AT91SAM9) += rtc-at91sam9.o obj-$(CONFIG_RTC_DRV_AU1XXX) += rtc-au1xxx.o +obj-$(CONFIG_RTC_DRV_BBNSM) += rtc-bbnsm.o obj-$(CONFIG_RTC_DRV_BD70528) += rtc-bd70528.o obj-$(CONFIG_RTC_DRV_BQ32K) += rtc-bq32k.o obj-$(CONFIG_RTC_DRV_BQ4802) += rtc-bq4802.o diff --git a/drivers/rtc/rtc-bbnsm.c b/drivers/rtc/rtc-bbnsm.c new file mode 100644 index 000000000000..4157b238ed9a --- /dev/null +++ b/drivers/rtc/rtc-bbnsm.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0+ +// +// Copyright 2022 NXP. + +#include <linux/init.h> +#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/platform_device.h> +#include <linux/pm_wakeirq.h> +#include <linux/rtc.h> +#include <linux/mfd/syscon.h> +#include <linux/regmap.h> + +#define BBNSM_CTRL 0x8 +#define BBNSM_INT_EN 0x10 +#define BBNSM_EVENTS 0x14 +#define BBNSM_RTC_LS 0x40 +#define BBNSM_RTC_MS 0x44 +#define BBNSM_TA 0x50 + +#define RTC_EN 0x2 +#define RTC_EN_MSK 0x3 +#define TA_EN (0x2 << 2) +#define TA_DIS (0x1 << 2) +#define TA_EN_MSK (0x3 << 2) +#define RTC_INT_EN 0x2 +#define TA_INT_EN (0x2 << 2) + +#define BBNSM_EVENT_TA TA_EN + +#define CNTR_TO_SECS_SH 15 + +struct bbnsm_rtc { + struct rtc_device *rtc; + struct regmap *regmap; + int irq; + struct clk *clk; +}; + +static u32 bbnsm_read_counter(struct bbnsm_rtc *bbnsm) +{ + u32 rtc_msb, rtc_lsb; + unsigned int timeout = 100; + u32 time; + u32 tmp = 0; + + do { + time = tmp; + /* read the msb */ + regmap_read(bbnsm->regmap, BBNSM_RTC_MS, &rtc_msb); + /* read the lsb */ + regmap_read(bbnsm->regmap, BBNSM_RTC_LS, &rtc_lsb); + /* convert to seconds */ + tmp = (rtc_msb << 17) | (rtc_lsb >> 15); + } while (tmp != time && --timeout); + + return time; +} + +static int bbnsm_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + unsigned long time; + + time = bbnsm_read_counter(bbnsm); + rtc_time64_to_tm(time, tm); + + return 0; +} + +static int bbnsm_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + unsigned long time = rtc_tm_to_time64(tm); + + /* disable the RTC first */ + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, RTC_EN_MSK, 0); + + /* write the 32bit sec time to 47 bit timer counter, leaving 15 LSBs blank */ + regmap_write(bbnsm->regmap, BBNSM_RTC_LS, time << CNTR_TO_SECS_SH); + regmap_write(bbnsm->regmap, BBNSM_RTC_MS, time >> (32 - CNTR_TO_SECS_SH)); + + /* Enable the RTC again */ + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, RTC_EN_MSK, RTC_EN); + + return 0; +} + +static int bbnsm_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + u32 bbnsm_events, bbnsm_ta; + + regmap_read(bbnsm->regmap, BBNSM_TA, &bbnsm_ta); + rtc_time64_to_tm(bbnsm_ta, &alrm->time); + + regmap_read(bbnsm->regmap, BBNSM_EVENTS, &bbnsm_events); + alrm->pending = (bbnsm_events & BBNSM_EVENT_TA) ? 1 : 0; + + return 0; +} + +static int bbnsm_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) +{ + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + + /* enable the alarm event */ + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, TA_EN_MSK, enable ? TA_EN : TA_DIS); + /* enable the alarm interrupt */ + regmap_update_bits(bbnsm->regmap, BBNSM_INT_EN, TA_EN_MSK, enable ? TA_EN : TA_DIS); + + return 0; +} + +static int bbnsm_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + unsigned long time = rtc_tm_to_time64(&alrm->time); + + /* disable the alarm */ + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, TA_EN, TA_EN); + + /* write the seconds to TA */ + regmap_write(bbnsm->regmap, BBNSM_TA, time); + + return bbnsm_rtc_alarm_irq_enable(dev, alrm->enabled); +} + +static const struct rtc_class_ops bbnsm_rtc_ops = { + .read_time = bbnsm_rtc_read_time, + .set_time = bbnsm_rtc_set_time, + .read_alarm = bbnsm_rtc_read_alarm, + .set_alarm = bbnsm_rtc_set_alarm, + .alarm_irq_enable = bbnsm_rtc_alarm_irq_enable, +}; + +static irqreturn_t bbnsm_rtc_irq_handler(int irq, void *dev_id) +{ + struct device *dev = dev_id; + struct bbnsm_rtc *bbnsm = dev_get_drvdata(dev); + u32 val; + u32 event = 0; + + regmap_read(bbnsm->regmap, BBNSM_EVENTS, &val); + if (val & BBNSM_EVENT_TA) { + event |= (RTC_AF | RTC_IRQF); + bbnsm_rtc_alarm_irq_enable(dev, false); + /* clear the alarm event */ + regmap_write_bits(bbnsm->regmap, BBNSM_EVENTS, TA_EN_MSK, BBNSM_EVENT_TA); + rtc_update_irq(bbnsm->rtc, 1, event); + } + + return event ? IRQ_HANDLED : IRQ_NONE; +} + +static int bbnsm_rtc_probe(struct platform_device *pdev) +{ + struct bbnsm_rtc *bbnsm; + int ret; + + bbnsm = devm_kzalloc(&pdev->dev, sizeof(*bbnsm), GFP_KERNEL); + if (!bbnsm) + return -ENOMEM; + + bbnsm->rtc = devm_rtc_allocate_device(&pdev->dev); + + bbnsm->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap"); + if (IS_ERR(bbnsm->regmap)) { + dev_err(&pdev->dev, "bbnsm get regmap failed\n"); + return PTR_ERR(bbnsm->regmap); + } + + bbnsm->irq = platform_get_irq(pdev, 0); + if (bbnsm->irq < 0) + return bbnsm->irq; + + platform_set_drvdata(pdev, bbnsm); + + /* clear all the pending events */ + regmap_write(bbnsm->regmap, BBNSM_EVENTS, 0x7A); + + /* Enable the Real-Time counter */ + regmap_update_bits(bbnsm->regmap, BBNSM_CTRL, RTC_EN_MSK, RTC_EN); + + device_init_wakeup(&pdev->dev, true); + ret = dev_pm_set_wake_irq(&pdev->dev, bbnsm->irq); + if (ret) + dev_err(&pdev->dev, "Failed to enable the irq wakeup\n"); + + ret = devm_request_irq(&pdev->dev, bbnsm->irq, bbnsm_rtc_irq_handler, + IRQF_SHARED, "rtc alarm", &pdev->dev); + if (ret) { + dev_err(&pdev->dev, "failed to request irq %d: %d\n", + bbnsm->irq, ret); + return ret; + } + + bbnsm->rtc->ops = &bbnsm_rtc_ops; + bbnsm->rtc->range_max = U32_MAX; + + return devm_rtc_register_device(bbnsm->rtc); +} + +static const struct of_device_id bbnsm_dt_ids[] = { + { .compatible = "nxp,bbnsm-rtc", }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, bbnsm_dt_ids); + +static struct platform_driver bbnsm_rtc_driver = { + .driver = { + .name = "bbnsm_rtc", + .of_match_table = bbnsm_dt_ids, + }, + .probe = bbnsm_rtc_probe, +}; +module_platform_driver(bbnsm_rtc_driver); + +MODULE_AUTHOR("Jacky Bai <ping.bai@nxp.com>"); +MODULE_DESCRIPTION("NXP BBNSM RTC Driver"); +MODULE_LICENSE("GPL");