Skip to content

Commit

Permalink
spi: Use devm_clk_get_*() helper function to
Browse files Browse the repository at this point in the history
Merge series from Li Zetao <lizetao1@huawei.com>:

Commit 7ef9651 ("clk: Provide new devm_clk helpers for prepared
and enabled clocks") provides a new helper function for prepared and
enabled clocks when a driver keeps a clock prepared (or enabled) during
the whole lifetime of the driver. So where drivers get clocks and enable
them immediately, it can be combined into a single function
devm_clk_get_*(). Moreover, the unprepare and disable function
has been registered to devm_clk_state, and before devm_clk_state is
released, the clocks will be unprepareed and disable, so it is unnecessary
to unprepare and disable clocks explicitly when remove drivers or in the
error handling path.
  • Loading branch information
Mark Brown committed Sep 11, 2023
2 parents fffae3a + d6c612a commit fd811b6
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 413 deletions.
22 changes: 3 additions & 19 deletions drivers/spi/spi-ar934x.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,21 @@ static int ar934x_spi_probe(struct platform_device *pdev)
struct ar934x_spi *sp;
void __iomem *base;
struct clk *clk;
int ret;

base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(base))
return PTR_ERR(base);

clk = devm_clk_get(&pdev->dev, NULL);
clk = devm_clk_get_enabled(&pdev->dev, NULL);
if (IS_ERR(clk)) {
dev_err(&pdev->dev, "failed to get clock\n");
return PTR_ERR(clk);
}

ret = clk_prepare_enable(clk);
if (ret)
return ret;

ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*sp));
if (!ctlr) {
dev_info(&pdev->dev, "failed to allocate spi controller\n");
ret = -ENOMEM;
goto err_clk_disable;
return -ENOMEM;
}

/* disable flash mapping and expose spi controller registers */
Expand All @@ -212,25 +206,15 @@ static int ar934x_spi_probe(struct platform_device *pdev)
sp->clk_freq = clk_get_rate(clk);
sp->ctlr = ctlr;

ret = spi_register_controller(ctlr);
if (!ret)
return 0;

err_clk_disable:
clk_disable_unprepare(clk);
return ret;
return spi_register_controller(ctlr);
}

static void ar934x_spi_remove(struct platform_device *pdev)
{
struct spi_controller *ctlr;
struct ar934x_spi *sp;

ctlr = dev_get_drvdata(&pdev->dev);
sp = spi_controller_get_devdata(ctlr);

spi_unregister_controller(ctlr);
clk_disable_unprepare(sp->clk);
}

static struct platform_driver ar934x_spi_driver = {
Expand Down
23 changes: 3 additions & 20 deletions drivers/spi/spi-armada-3700.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,18 +865,12 @@ static int a3700_spi_probe(struct platform_device *pdev)

init_completion(&spi->done);

spi->clk = devm_clk_get(dev, NULL);
spi->clk = devm_clk_get_prepared(dev, NULL);
if (IS_ERR(spi->clk)) {
dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
goto error;
}

ret = clk_prepare(spi->clk);
if (ret) {
dev_err(dev, "could not prepare clk: %d\n", ret);
goto error;
}

host->max_speed_hz = min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ,
clk_get_rate(spi->clk));
host->min_speed_hz = DIV_ROUND_UP(clk_get_rate(spi->clk),
Expand All @@ -888,40 +882,29 @@ static int a3700_spi_probe(struct platform_device *pdev)
dev_name(dev), host);
if (ret) {
dev_err(dev, "could not request IRQ: %d\n", ret);
goto error_clk;
goto error;
}

ret = devm_spi_register_controller(dev, host);
if (ret) {
dev_err(dev, "Failed to register host\n");
goto error_clk;
goto error;
}

return 0;

error_clk:
clk_unprepare(spi->clk);
error:
spi_controller_put(host);
out:
return ret;
}

static void a3700_spi_remove(struct platform_device *pdev)
{
struct spi_controller *host = platform_get_drvdata(pdev);
struct a3700_spi *spi = spi_controller_get_devdata(host);

clk_unprepare(spi->clk);
}

static struct platform_driver a3700_spi_driver = {
.driver = {
.name = DRIVER_NAME,
.of_match_table = of_match_ptr(a3700_spi_dt_ids),
},
.probe = a3700_spi_probe,
.remove_new = a3700_spi_remove,
};

module_platform_driver(a3700_spi_driver);
Expand Down
16 changes: 2 additions & 14 deletions drivers/spi/spi-aspeed-smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ static int aspeed_spi_probe(struct platform_device *pdev)
aspi->ahb_window_size = resource_size(res);
aspi->ahb_base_phy = res->start;

aspi->clk = devm_clk_get(&pdev->dev, NULL);
aspi->clk = devm_clk_get_enabled(&pdev->dev, NULL);
if (IS_ERR(aspi->clk)) {
dev_err(dev, "missing clock\n");
return PTR_ERR(aspi->clk);
Expand All @@ -760,12 +760,6 @@ static int aspeed_spi_probe(struct platform_device *pdev)
return -EINVAL;
}

ret = clk_prepare_enable(aspi->clk);
if (ret) {
dev_err(dev, "can not enable the clock\n");
return ret;
}

/* IRQ is for DMA, which the driver doesn't support yet */

ctlr->mode_bits = SPI_RX_DUAL | SPI_TX_DUAL | data->mode_bits;
Expand All @@ -777,14 +771,9 @@ static int aspeed_spi_probe(struct platform_device *pdev)
ctlr->dev.of_node = dev->of_node;

ret = devm_spi_register_controller(dev, ctlr);
if (ret) {
if (ret)
dev_err(&pdev->dev, "spi_register_controller failed\n");
goto disable_clk;
}
return 0;

disable_clk:
clk_disable_unprepare(aspi->clk);
return ret;
}

Expand All @@ -793,7 +782,6 @@ static void aspeed_spi_remove(struct platform_device *pdev)
struct aspeed_spi *aspi = platform_get_drvdata(pdev);

aspeed_spi_enable(aspi, false);
clk_disable_unprepare(aspi->clk);
}

/*
Expand Down
11 changes: 2 additions & 9 deletions drivers/spi/spi-ath79.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,16 @@ static int ath79_spi_probe(struct platform_device *pdev)
goto err_put_host;
}

sp->clk = devm_clk_get(&pdev->dev, "ahb");
sp->clk = devm_clk_get_enabled(&pdev->dev, "ahb");
if (IS_ERR(sp->clk)) {
ret = PTR_ERR(sp->clk);
goto err_put_host;
}

ret = clk_prepare_enable(sp->clk);
if (ret)
goto err_put_host;

rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
if (!rate) {
ret = -EINVAL;
goto err_clk_disable;
goto err_put_host;
}

sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
Expand All @@ -229,8 +225,6 @@ static int ath79_spi_probe(struct platform_device *pdev)

err_disable:
ath79_spi_disable(sp);
err_clk_disable:
clk_disable_unprepare(sp->clk);
err_put_host:
spi_controller_put(host);

Expand All @@ -243,7 +237,6 @@ static void ath79_spi_remove(struct platform_device *pdev)

spi_bitbang_stop(&sp->bitbang);
ath79_spi_disable(sp);
clk_disable_unprepare(sp->clk);
spi_controller_put(sp->bitbang.master);
}

Expand Down
25 changes: 5 additions & 20 deletions drivers/spi/spi-axi-spi-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,30 +485,22 @@ static int spi_engine_probe(struct platform_device *pdev)

spin_lock_init(&spi_engine->lock);

spi_engine->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
spi_engine->clk = devm_clk_get_enabled(&pdev->dev, "s_axi_aclk");
if (IS_ERR(spi_engine->clk)) {
ret = PTR_ERR(spi_engine->clk);
goto err_put_host;
}

spi_engine->ref_clk = devm_clk_get(&pdev->dev, "spi_clk");
spi_engine->ref_clk = devm_clk_get_enabled(&pdev->dev, "spi_clk");
if (IS_ERR(spi_engine->ref_clk)) {
ret = PTR_ERR(spi_engine->ref_clk);
goto err_put_host;
}

ret = clk_prepare_enable(spi_engine->clk);
if (ret)
goto err_put_host;

ret = clk_prepare_enable(spi_engine->ref_clk);
if (ret)
goto err_clk_disable;

spi_engine->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(spi_engine->base)) {
ret = PTR_ERR(spi_engine->base);
goto err_ref_clk_disable;
goto err_put_host;
}

version = readl(spi_engine->base + SPI_ENGINE_REG_VERSION);
Expand All @@ -518,7 +510,7 @@ static int spi_engine_probe(struct platform_device *pdev)
SPI_ENGINE_VERSION_MINOR(version),
SPI_ENGINE_VERSION_PATCH(version));
ret = -ENODEV;
goto err_ref_clk_disable;
goto err_put_host;
}

writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_RESET);
Expand All @@ -527,7 +519,7 @@ static int spi_engine_probe(struct platform_device *pdev)

ret = request_irq(irq, spi_engine_irq, 0, pdev->name, host);
if (ret)
goto err_ref_clk_disable;
goto err_put_host;

host->dev.of_node = pdev->dev.of_node;
host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_3WIRE;
Expand All @@ -545,10 +537,6 @@ static int spi_engine_probe(struct platform_device *pdev)
return 0;
err_free_irq:
free_irq(irq, host);
err_ref_clk_disable:
clk_disable_unprepare(spi_engine->ref_clk);
err_clk_disable:
clk_disable_unprepare(spi_engine->clk);
err_put_host:
spi_controller_put(host);
return ret;
Expand All @@ -569,9 +557,6 @@ static void spi_engine_remove(struct platform_device *pdev)
writel_relaxed(0xff, spi_engine->base + SPI_ENGINE_REG_INT_PENDING);
writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_INT_ENABLE);
writel_relaxed(0x01, spi_engine->base + SPI_ENGINE_REG_RESET);

clk_disable_unprepare(spi_engine->ref_clk);
clk_disable_unprepare(spi_engine->clk);
}

static const struct of_device_id spi_engine_match_table[] = {
Expand Down
11 changes: 2 additions & 9 deletions drivers/spi/spi-bcm2835.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ static int bcm2835_spi_probe(struct platform_device *pdev)
if (IS_ERR(bs->regs))
return PTR_ERR(bs->regs);

bs->clk = devm_clk_get(&pdev->dev, NULL);
bs->clk = devm_clk_get_enabled(&pdev->dev, NULL);
if (IS_ERR(bs->clk))
return dev_err_probe(&pdev->dev, PTR_ERR(bs->clk),
"could not get clk\n");
Expand All @@ -1363,14 +1363,11 @@ static int bcm2835_spi_probe(struct platform_device *pdev)
if (bs->irq < 0)
return bs->irq;

err = clk_prepare_enable(bs->clk);
if (err)
return err;
bs->clk_hz = clk_get_rate(bs->clk);

err = bcm2835_dma_init(ctlr, &pdev->dev, bs);
if (err)
goto out_clk_disable;
return err;

/* initialise the hardware with the default polarities */
bcm2835_wr(bs, BCM2835_SPI_CS,
Expand All @@ -1396,8 +1393,6 @@ static int bcm2835_spi_probe(struct platform_device *pdev)

out_dma_release:
bcm2835_dma_release(ctlr, bs);
out_clk_disable:
clk_disable_unprepare(bs->clk);
return err;
}

Expand All @@ -1415,8 +1410,6 @@ static void bcm2835_spi_remove(struct platform_device *pdev)
/* Clear FIFOs, and disable the HW block */
bcm2835_wr(bs, BCM2835_SPI_CS,
BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX);

clk_disable_unprepare(bs->clk);
}

static const struct of_device_id bcm2835_spi_match[] = {
Expand Down
23 changes: 4 additions & 19 deletions drivers/spi/spi-bcm2835aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ static int bcm2835aux_spi_probe(struct platform_device *pdev)
if (IS_ERR(bs->regs))
return PTR_ERR(bs->regs);

bs->clk = devm_clk_get(&pdev->dev, NULL);
bs->clk = devm_clk_get_enabled(&pdev->dev, NULL);
if (IS_ERR(bs->clk)) {
err = PTR_ERR(bs->clk);
dev_err(&pdev->dev, "could not get clk: %d\n", err);
Expand All @@ -523,19 +523,11 @@ static int bcm2835aux_spi_probe(struct platform_device *pdev)
if (bs->irq < 0)
return bs->irq;

/* this also enables the HW block */
err = clk_prepare_enable(bs->clk);
if (err) {
dev_err(&pdev->dev, "could not prepare clock: %d\n", err);
return err;
}

/* just checking if the clock returns a sane value */
clk_hz = clk_get_rate(bs->clk);
if (!clk_hz) {
dev_err(&pdev->dev, "clock returns 0 Hz\n");
err = -ENODEV;
goto out_clk_disable;
return -ENODEV;
}

/* reset SPI-HW block */
Expand All @@ -547,22 +539,18 @@ static int bcm2835aux_spi_probe(struct platform_device *pdev)
dev_name(&pdev->dev), host);
if (err) {
dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
goto out_clk_disable;
return err;
}

err = spi_register_controller(host);
if (err) {
dev_err(&pdev->dev, "could not register SPI host: %d\n", err);
goto out_clk_disable;
return err;
}

bcm2835aux_debugfs_create(bs, dev_name(&pdev->dev));

return 0;

out_clk_disable:
clk_disable_unprepare(bs->clk);
return err;
}

static void bcm2835aux_spi_remove(struct platform_device *pdev)
Expand All @@ -575,9 +563,6 @@ static void bcm2835aux_spi_remove(struct platform_device *pdev)
spi_unregister_controller(host);

bcm2835aux_spi_reset_hw(bs);

/* disable the HW block by releasing the clock */
clk_disable_unprepare(bs->clk);
}

static const struct of_device_id bcm2835aux_spi_match[] = {
Expand Down
Loading

0 comments on commit fd811b6

Please sign in to comment.