Skip to content

Commit 46e001c

Browse files
committed
Add devm_dma_request_chan() to simplify probe
Merge series from Bence Csókás <[email protected]>: The probe function of the atmel-quadspi driver got quite convoluted, especially since the addition of SAMA7G5 support, that was forward-ported from an older vendor kernel. To alleivate this - and similar problems in the future - an effort was made to migrate as many functions as possible, to their devm_ managed counterparts. Patch 1/2 adds the new `devm_dma_request_chan()` function. Patch 2/2 then uses this APIs to simplify the probe() function.
2 parents ac4c064 + 2555691 commit 46e001c

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

drivers/dma/dmaengine.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,36 @@ void dma_release_channel(struct dma_chan *chan)
926926
}
927927
EXPORT_SYMBOL_GPL(dma_release_channel);
928928

929+
static void dmaenginem_release_channel(void *chan)
930+
{
931+
dma_release_channel(chan);
932+
}
933+
934+
/**
935+
* devm_dma_request_chan - try to allocate an exclusive slave channel
936+
* @dev: pointer to client device structure
937+
* @name: slave channel name
938+
*
939+
* Returns pointer to appropriate DMA channel on success or an error pointer.
940+
*
941+
* The operation is managed and will be undone on driver detach.
942+
*/
943+
944+
struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name)
945+
{
946+
struct dma_chan *chan = dma_request_chan(dev, name);
947+
int ret = 0;
948+
949+
if (!IS_ERR(chan))
950+
ret = devm_add_action_or_reset(dev, dmaenginem_release_channel, chan);
951+
952+
if (ret)
953+
return ERR_PTR(ret);
954+
955+
return chan;
956+
}
957+
EXPORT_SYMBOL_GPL(devm_dma_request_chan);
958+
929959
/**
930960
* dmaengine_get - register interest in dma_channels
931961
*/

drivers/spi/atmel-quadspi.c

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,18 +1285,21 @@ static int atmel_qspi_dma_init(struct spi_controller *ctrl)
12851285
struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
12861286
int ret;
12871287

1288-
aq->rx_chan = dma_request_chan(&aq->pdev->dev, "rx");
1288+
aq->rx_chan = devm_dma_request_chan(&aq->pdev->dev, "rx");
12891289
if (IS_ERR(aq->rx_chan)) {
12901290
ret = dev_err_probe(&aq->pdev->dev, PTR_ERR(aq->rx_chan),
12911291
"RX DMA channel is not available\n");
1292-
goto null_rx_chan;
1292+
aq->rx_chan = NULL;
1293+
return ret;
12931294
}
12941295

1295-
aq->tx_chan = dma_request_chan(&aq->pdev->dev, "tx");
1296+
aq->tx_chan = devm_dma_request_chan(&aq->pdev->dev, "tx");
12961297
if (IS_ERR(aq->tx_chan)) {
12971298
ret = dev_err_probe(&aq->pdev->dev, PTR_ERR(aq->tx_chan),
12981299
"TX DMA channel is not available\n");
1299-
goto release_rx_chan;
1300+
aq->rx_chan = NULL;
1301+
aq->tx_chan = NULL;
1302+
return ret;
13001303
}
13011304

13021305
ctrl->dma_rx = aq->rx_chan;
@@ -1307,21 +1310,6 @@ static int atmel_qspi_dma_init(struct spi_controller *ctrl)
13071310
dma_chan_name(aq->tx_chan), dma_chan_name(aq->rx_chan));
13081311

13091312
return 0;
1310-
1311-
release_rx_chan:
1312-
dma_release_channel(aq->rx_chan);
1313-
aq->tx_chan = NULL;
1314-
null_rx_chan:
1315-
aq->rx_chan = NULL;
1316-
return ret;
1317-
}
1318-
1319-
static void atmel_qspi_dma_release(struct atmel_qspi *aq)
1320-
{
1321-
if (aq->rx_chan)
1322-
dma_release_channel(aq->rx_chan);
1323-
if (aq->tx_chan)
1324-
dma_release_channel(aq->tx_chan);
13251313
}
13261314

13271315
static const struct atmel_qspi_ops atmel_qspi_ops = {
@@ -1426,14 +1414,13 @@ static int atmel_qspi_probe(struct platform_device *pdev)
14261414

14271415
/* Request the IRQ */
14281416
irq = platform_get_irq(pdev, 0);
1429-
if (irq < 0) {
1430-
err = irq;
1431-
goto dma_release;
1432-
}
1417+
if (irq < 0)
1418+
return irq;
1419+
14331420
err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt,
14341421
0, dev_name(&pdev->dev), aq);
14351422
if (err)
1436-
goto dma_release;
1423+
return err;
14371424

14381425
pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
14391426
pm_runtime_use_autosuspend(&pdev->dev);
@@ -1442,22 +1429,16 @@ static int atmel_qspi_probe(struct platform_device *pdev)
14421429

14431430
err = atmel_qspi_init(aq);
14441431
if (err)
1445-
goto dma_release;
1432+
return err;
14461433

14471434
err = spi_register_controller(ctrl);
14481435
if (err)
1449-
goto dma_release;
1436+
return err;
14501437

14511438
pm_runtime_mark_last_busy(&pdev->dev);
14521439
pm_runtime_put_autosuspend(&pdev->dev);
14531440

14541441
return 0;
1455-
1456-
dma_release:
1457-
if (aq->caps->has_dma)
1458-
atmel_qspi_dma_release(aq);
1459-
1460-
return err;
14611442
}
14621443

14631444
static int atmel_qspi_sama7g5_suspend(struct atmel_qspi *aq)
@@ -1507,9 +1488,6 @@ static void atmel_qspi_remove(struct platform_device *pdev)
15071488

15081489
ret = pm_runtime_get_sync(&pdev->dev);
15091490
if (ret >= 0) {
1510-
if (aq->caps->has_dma)
1511-
atmel_qspi_dma_release(aq);
1512-
15131491
if (aq->caps->has_gclk) {
15141492
ret = atmel_qspi_sama7g5_suspend(aq);
15151493
if (ret)

include/linux/dmaengine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
15241524

15251525
struct dma_chan *dma_request_chan(struct device *dev, const char *name);
15261526
struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
1527+
struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name);
15271528

15281529
void dma_release_channel(struct dma_chan *chan);
15291530
int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
@@ -1560,6 +1561,12 @@ static inline struct dma_chan *dma_request_chan_by_mask(
15601561
{
15611562
return ERR_PTR(-ENODEV);
15621563
}
1564+
1565+
static inline struct dma_chan *devm_dma_request_chan(struct device *dev, const char *name)
1566+
{
1567+
return ERR_PTR(-ENODEV);
1568+
}
1569+
15631570
static inline void dma_release_channel(struct dma_chan *chan)
15641571
{
15651572
}

0 commit comments

Comments
 (0)