Skip to content

Commit

Permalink
rtc: rework rtc_register_device() resource management
Browse files Browse the repository at this point in the history
rtc_register_device() is a managed interface but it doesn't use devres
by itself - instead it marks an rtc_device as "registered" and the devres
callback for devm_rtc_allocate_device() takes care of resource release.

This doesn't correspond with the design behind devres where managed
structures should not be aware of being managed. The correct solution
here is to register a separate devres callback for unregistering the
device.

While at it: rename rtc_register_device() to devm_rtc_register_device()
and add it to the list of managed interfaces in devres.rst. This way we
can avoid any potential confusion of driver developers who may expect
there to exist a corresponding unregister function.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20201109163409.24301-8-brgl@bgdev.pl
  • Loading branch information
Bartosz Golaszewski authored and Alexandre Belloni committed Nov 19, 2020
1 parent 6746bc0 commit fdcfd85
Show file tree
Hide file tree
Showing 114 changed files with 123 additions and 127 deletions.
1 change: 1 addition & 0 deletions Documentation/driver-api/driver-model/devres.rst
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ RESET
RTC
devm_rtc_device_register()
devm_rtc_allocate_device()
devm_rtc_register_device()
devm_rtc_nvmem_register()

SERDEV
Expand Down
2 changes: 1 addition & 1 deletion arch/alpha/kernel/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ alpha_rtc_init(void)
rtc->ops = &remote_rtc_ops;
#endif

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}
device_initcall(alpha_rtc_init);
2 changes: 1 addition & 1 deletion drivers/mfd/menelaus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ static inline void menelaus_rtc_init(struct menelaus_chip *m)
menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
}

err = rtc_register_device(m->rtc);
err = devm_rtc_register_device(m->rtc);
if (err) {
if (alarm) {
menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
Expand Down
19 changes: 9 additions & 10 deletions drivers/rtc/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ static void rtc_device_get_offset(struct rtc_device *rtc)
*
* @rtc: the RTC class device to destroy
*/
static void rtc_device_unregister(struct rtc_device *rtc)
static void devm_rtc_unregister_device(void *data)
{
struct rtc_device *rtc = data;

mutex_lock(&rtc->ops_lock);
/*
* Remove innards of this RTC, then disable it, before
Expand All @@ -339,10 +341,7 @@ static void devm_rtc_release_device(struct device *dev, void *res)
{
struct rtc_device *rtc = *(struct rtc_device **)res;

if (rtc->registered)
rtc_device_unregister(rtc);
else
put_device(&rtc->dev);
put_device(&rtc->dev);
}

struct rtc_device *devm_rtc_allocate_device(struct device *dev)
Expand Down Expand Up @@ -383,7 +382,7 @@ struct rtc_device *devm_rtc_allocate_device(struct device *dev)
}
EXPORT_SYMBOL_GPL(devm_rtc_allocate_device);

int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
int __devm_rtc_register_device(struct module *owner, struct rtc_device *rtc)
{
struct rtc_wkalrm alrm;
int err;
Expand Down Expand Up @@ -413,7 +412,6 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)

rtc_proc_add_device(rtc);

rtc->registered = true;
dev_info(rtc->dev.parent, "registered as %s\n",
dev_name(&rtc->dev));

Expand All @@ -422,9 +420,10 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
rtc_hctosys(rtc);
#endif

return 0;
return devm_add_action_or_reset(rtc->dev.parent,
devm_rtc_unregister_device, rtc);
}
EXPORT_SYMBOL_GPL(__rtc_register_device);
EXPORT_SYMBOL_GPL(__devm_rtc_register_device);

/**
* devm_rtc_device_register - resource managed rtc_device_register()
Expand Down Expand Up @@ -454,7 +453,7 @@ struct rtc_device *devm_rtc_device_register(struct device *dev,

rtc->ops = ops;

err = __rtc_register_device(owner, rtc);
err = __devm_rtc_register_device(owner, rtc);
if (err)
return ERR_PTR(err);

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-88pm80x.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ static int pm80x_rtc_probe(struct platform_device *pdev)
info->rtc_dev->ops = &pm80x_rtc_ops;
info->rtc_dev->range_max = U32_MAX;

ret = rtc_register_device(info->rtc_dev);
ret = devm_rtc_register_device(info->rtc_dev);
if (ret)
goto out_rtc;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-88pm860x.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ static int pm860x_rtc_probe(struct platform_device *pdev)
info->rtc_dev->ops = &pm860x_rtc_ops;
info->rtc_dev->range_max = U32_MAX;

ret = rtc_register_device(info->rtc_dev);
ret = devm_rtc_register_device(info->rtc_dev);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ab-b5ze-s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ static int abb5zes3_probe(struct i2c_client *client,
}
}

ret = rtc_register_device(data->rtc);
ret = devm_rtc_register_device(data->rtc);

err:
if (ret && data->irq)
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ab-eoz9.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ static int abeoz9_probe(struct i2c_client *client,
data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
data->rtc->range_max = RTC_TIMESTAMP_END_2099;

ret = rtc_register_device(data->rtc);
ret = devm_rtc_register_device(data->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ab3100.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static int __init ab3100_rtc_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, rtc);

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}

static struct platform_driver ab3100_rtc_driver = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ab8500.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static int ab8500_rtc_probe(struct platform_device *pdev)
if (err)
return err;

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}

static int ab8500_rtc_remove(struct platform_device *pdev)
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-abx80x.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ static int abx80x_probe(struct i2c_client *client,
return err;
}

return rtc_register_device(priv->rtc);
return devm_rtc_register_device(priv->rtc);
}

static const struct i2c_device_id abx80x_id[] = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ac100.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ static int ac100_rtc_probe(struct platform_device *pdev)
if (ret)
return ret;

return rtc_register_device(chip->rtc);
return devm_rtc_register_device(chip->rtc);
}

static int ac100_rtc_remove(struct platform_device *pdev)
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-armada38x.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ static __init int armada38x_rtc_probe(struct platform_device *pdev)

rtc->rtc_dev->range_max = U32_MAX;

return rtc_register_device(rtc->rtc_dev);
return devm_rtc_register_device(rtc->rtc_dev);
}

#ifdef CONFIG_PM_SLEEP
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-aspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int aspeed_rtc_probe(struct platform_device *pdev)
rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
rtc->rtc_dev->range_max = 38814989399LL; /* 3199-12-31 23:59:59 */

return rtc_register_device(rtc->rtc_dev);
return devm_rtc_register_device(rtc->rtc_dev);
}

static const struct of_device_id aspeed_rtc_match[] = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-at91rm9200.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ static int __init at91_rtc_probe(struct platform_device *pdev)

rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
rtc->range_max = RTC_TIMESTAMP_END_2099;
ret = rtc_register_device(rtc);
ret = devm_rtc_register_device(rtc);
if (ret)
goto err_clk;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-at91sam9.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ static int at91_rtc_probe(struct platform_device *pdev)
dev_warn(&pdev->dev, "%s: SET TIME!\n",
dev_name(&rtc->rtcdev->dev));

return rtc_register_device(rtc->rtcdev);
return devm_rtc_register_device(rtc->rtcdev);

err_clk:
clk_disable_unprepare(rtc->sclk);
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-au1xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int au1xtoy_rtc_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, rtcdev);

return rtc_register_device(rtcdev);
return devm_rtc_register_device(rtcdev);
}

static struct platform_driver au1xrtc_driver = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-bd70528.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ static int bd70528_probe(struct platform_device *pdev)
}
}

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}

static const struct platform_device_id bd718x7_rtc_id[] = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-brcmstb-waketimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ static int brcmstb_waketmr_probe(struct platform_device *pdev)
timer->rtc->ops = &brcmstb_waketmr_ops;
timer->rtc->range_max = U32_MAX;

ret = rtc_register_device(timer->rtc);
ret = devm_rtc_register_device(timer->rtc);
if (ret)
goto err_notifier;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-cadence.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ static int cdns_rtc_probe(struct platform_device *pdev)
writel(0, crtc->regs + CDNS_RTC_HMR);
writel(CDNS_RTC_KRTCR_KRTC, crtc->regs + CDNS_RTC_KRTCR);

ret = rtc_register_device(crtc->rtc_dev);
ret = devm_rtc_register_device(crtc->rtc_dev);
if (ret)
goto err_disable_wakeup;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-cmos.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
cmos_rtc.rtc->ops = &cmos_rtc_ops_no_alarm;
}

retval = rtc_register_device(cmos_rtc.rtc);
retval = devm_rtc_register_device(cmos_rtc.rtc);
if (retval)
goto cleanup2;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-coh901331.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static int __init coh901331_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, rtap);

ret = rtc_register_device(rtap->rtc);
ret = devm_rtc_register_device(rtap->rtc);
if (ret)
goto out_no_rtc;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-cpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static int cpcap_rtc_probe(struct platform_device *pdev)
/* ignore error and continue without wakeup support */
}

return rtc_register_device(rtc->rtc_dev);
return devm_rtc_register_device(rtc->rtc_dev);
}

static const struct of_device_id cpcap_rtc_of_match[] = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-cros-ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ static int cros_ec_rtc_probe(struct platform_device *pdev)
cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
cros_ec_rtc->rtc->range_max = U32_MAX;

ret = rtc_register_device(cros_ec_rtc->rtc);
ret = devm_rtc_register_device(cros_ec_rtc->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-da9052.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ static int da9052_rtc_probe(struct platform_device *pdev)
rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
rtc->rtc->range_max = RTC_TIMESTAMP_END_2063;

ret = rtc_register_device(rtc->rtc);
ret = devm_rtc_register_device(rtc->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-da9063.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ static int da9063_rtc_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
irq_alarm, ret);

return rtc_register_device(rtc->rtc_dev);
return devm_rtc_register_device(rtc->rtc_dev);
}

static struct platform_driver da9063_rtc_driver = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-davinci.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ static int __init davinci_rtc_probe(struct platform_device *pdev)

device_init_wakeup(&pdev->dev, 0);

return rtc_register_device(davinci_rtc->rtc);
return devm_rtc_register_device(davinci_rtc->rtc);
}

static int __exit davinci_rtc_remove(struct platform_device *pdev)
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-digicolor.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static int __init dc_rtc_probe(struct platform_device *pdev)
rtc->rtc_dev->ops = &dc_rtc_ops;
rtc->rtc_dev->range_max = U32_MAX;

return rtc_register_device(rtc->rtc_dev);
return devm_rtc_register_device(rtc->rtc_dev);
}

static const struct of_device_id dc_dt_ids[] = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-dm355evm.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static int dm355evm_rtc_probe(struct platform_device *pdev)
rtc->ops = &dm355evm_rtc_ops;
rtc->range_max = U32_MAX;

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ static int ds1305_probe(struct spi_device *spi)
ds1305->rtc->range_max = RTC_TIMESTAMP_END_2099;

ds1305_nvmem_cfg.priv = ds1305;
status = rtc_register_device(ds1305->rtc);
status = devm_rtc_register_device(ds1305->rtc);
if (status)
return status;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1307.c
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ static int ds1307_probe(struct i2c_client *client,
if (err)
return err;

err = rtc_register_device(ds1307->rtc);
err = devm_rtc_register_device(ds1307->rtc);
if (err)
return err;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1343.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ static int ds1343_probe(struct spi_device *spi)
dev_err(&spi->dev,
"unable to create sysfs entries for rtc ds1343\n");

res = rtc_register_device(priv->rtc);
res = devm_rtc_register_device(priv->rtc);
if (res)
return res;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1347.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static int ds1347_probe(struct spi_device *spi)
rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
rtc->range_max = RTC_TIMESTAMP_END_9999;

return rtc_register_device(rtc);
return devm_rtc_register_device(rtc);
}

static struct spi_driver ds1347_driver = {
Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1374.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static int ds1374_probe(struct i2c_client *client,
ds1374->rtc->ops = &ds1374_rtc_ops;
ds1374->rtc->range_max = U32_MAX;

ret = rtc_register_device(ds1374->rtc);
ret = devm_rtc_register_device(ds1374->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1511.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ static int ds1511_rtc_probe(struct platform_device *pdev)

pdata->rtc->ops = &ds1511_rtc_ops;

ret = rtc_register_device(pdata->rtc);
ret = devm_rtc_register_device(pdata->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1553.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static int ds1553_rtc_probe(struct platform_device *pdev)

pdata->rtc->ops = &ds1553_rtc_ops;

ret = rtc_register_device(pdata->rtc);
ret = devm_rtc_register_device(pdata->rtc);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1672.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int ds1672_probe(struct i2c_client *client,
rtc->ops = &ds1672_rtc_ops;
rtc->range_max = U32_MAX;

err = rtc_register_device(rtc);
err = devm_rtc_register_device(rtc);
if (err)
return err;

Expand Down
2 changes: 1 addition & 1 deletion drivers/rtc/rtc-ds1685.c
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ ds1685_rtc_probe(struct platform_device *pdev)
if (ret)
return ret;

return rtc_register_device(rtc_dev);
return devm_rtc_register_device(rtc_dev);
}

/**
Expand Down
Loading

0 comments on commit fdcfd85

Please sign in to comment.