Skip to content

Commit bd911ec

Browse files
jbrun3tstorulf
authored andcommitted
mmc: meson-gx: rework clock init function
Thanks to devm, carrying the clock structure around after init is not necessary. Rework the function to remove these from the controller host data. Finally, set initial mmc clock rate before enabling it, simplifying the exit condition. Reviewed-by: Kevin Hilman <[email protected]> Signed-off-by: Jerome Brunet <[email protected]> Signed-off-by: Ulf Hansson <[email protected]>
1 parent f89f55d commit bd911ec

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

drivers/mmc/host/meson-gx-mmc.c

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242

4343
#define SD_EMMC_CLOCK 0x0
4444
#define CLK_DIV_MASK GENMASK(5, 0)
45-
#define CLK_DIV_MAX 63
4645
#define CLK_SRC_MASK GENMASK(7, 6)
47-
#define CLK_SRC_XTAL 0 /* external crystal */
48-
#define CLK_SRC_PLL 1 /* FCLK_DIV2 */
4946
#define CLK_CORE_PHASE_MASK GENMASK(9, 8)
5047
#define CLK_TX_PHASE_MASK GENMASK(11, 10)
5148
#define CLK_RX_PHASE_MASK GENMASK(13, 12)
@@ -137,13 +134,9 @@ struct meson_host {
137134
spinlock_t lock;
138135
void __iomem *regs;
139136
struct clk *core_clk;
140-
struct clk_mux mux;
141-
struct clk *mux_clk;
137+
struct clk *mmc_clk;
142138
unsigned long req_rate;
143139

144-
struct clk_divider cfg_div;
145-
struct clk *cfg_div_clk;
146-
147140
unsigned int bounce_buf_size;
148141
void *bounce_buf;
149142
dma_addr_t bounce_dma_addr;
@@ -291,15 +284,15 @@ static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
291284
return 0;
292285
}
293286

294-
ret = clk_set_rate(host->cfg_div_clk, clk_rate);
287+
ret = clk_set_rate(host->mmc_clk, clk_rate);
295288
if (ret) {
296289
dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
297290
clk_rate, ret);
298291
return ret;
299292
}
300293

301294
host->req_rate = clk_rate;
302-
mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
295+
mmc->actual_clock = clk_get_rate(host->mmc_clk);
303296

304297
dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock);
305298
if (clk_rate != mmc->actual_clock)
@@ -321,10 +314,13 @@ static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
321314
static int meson_mmc_clk_init(struct meson_host *host)
322315
{
323316
struct clk_init_data init;
317+
struct clk_mux *mux;
318+
struct clk_divider *div;
319+
struct clk *clk;
324320
char clk_name[32];
325321
int i, ret = 0;
326322
const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
327-
const char *clk_div_parents[1];
323+
const char *clk_parent[1];
328324
u32 clk_reg;
329325

330326
/* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
@@ -353,55 +349,57 @@ static int meson_mmc_clk_init(struct meson_host *host)
353349
}
354350

355351
/* create the mux */
352+
mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL);
353+
if (!mux)
354+
return -ENOMEM;
355+
356356
snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
357357
init.name = clk_name;
358358
init.ops = &clk_mux_ops;
359359
init.flags = 0;
360360
init.parent_names = mux_parent_names;
361361
init.num_parents = MUX_CLK_NUM_PARENTS;
362-
host->mux.reg = host->regs + SD_EMMC_CLOCK;
363-
host->mux.shift = __bf_shf(CLK_SRC_MASK);
364-
host->mux.mask = CLK_SRC_MASK >> host->mux.shift;
365-
host->mux.flags = 0;
366-
host->mux.table = NULL;
367-
host->mux.hw.init = &init;
368362

369-
host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
370-
if (WARN_ON(IS_ERR(host->mux_clk)))
371-
return PTR_ERR(host->mux_clk);
363+
mux->reg = host->regs + SD_EMMC_CLOCK;
364+
mux->shift = __bf_shf(CLK_SRC_MASK);
365+
mux->mask = CLK_SRC_MASK >> mux->shift;
366+
mux->hw.init = &init;
367+
368+
clk = devm_clk_register(host->dev, &mux->hw);
369+
if (WARN_ON(IS_ERR(clk)))
370+
return PTR_ERR(clk);
372371

373372
/* create the divider */
373+
div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL);
374+
if (!div)
375+
return -ENOMEM;
376+
374377
snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
375378
init.name = clk_name;
376379
init.ops = &clk_divider_ops;
377380
init.flags = CLK_SET_RATE_PARENT;
378-
clk_div_parents[0] = __clk_get_name(host->mux_clk);
379-
init.parent_names = clk_div_parents;
380-
init.num_parents = ARRAY_SIZE(clk_div_parents);
381-
382-
host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
383-
host->cfg_div.shift = __bf_shf(CLK_DIV_MASK);
384-
host->cfg_div.width = __builtin_popcountl(CLK_DIV_MASK);
385-
host->cfg_div.hw.init = &init;
386-
host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
387-
CLK_DIVIDER_ROUND_CLOSEST;
388-
389-
host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
390-
if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
391-
return PTR_ERR(host->cfg_div_clk);
392-
393-
ret = clk_prepare_enable(host->cfg_div_clk);
394-
if (ret)
395-
return ret;
381+
clk_parent[0] = __clk_get_name(clk);
382+
init.parent_names = clk_parent;
383+
init.num_parents = 1;
384+
385+
div->reg = host->regs + SD_EMMC_CLOCK;
386+
div->shift = __bf_shf(CLK_DIV_MASK);
387+
div->width = __builtin_popcountl(CLK_DIV_MASK);
388+
div->hw.init = &init;
389+
div->flags = (CLK_DIVIDER_ONE_BASED |
390+
CLK_DIVIDER_ROUND_CLOSEST);
396391

397-
/* Get the nearest minimum clock to 400KHz */
398-
host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
392+
host->mmc_clk = devm_clk_register(host->dev, &div->hw);
393+
if (WARN_ON(PTR_ERR_OR_ZERO(host->mmc_clk)))
394+
return PTR_ERR(host->mmc_clk);
399395

400-
ret = meson_mmc_clk_set(host, host->mmc->f_min);
396+
/* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
397+
host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000);
398+
ret = clk_set_rate(host->mmc_clk, host->mmc->f_min);
401399
if (ret)
402-
clk_disable_unprepare(host->cfg_div_clk);
400+
return ret;
403401

404-
return ret;
402+
return clk_prepare_enable(host->mmc_clk);
405403
}
406404

407405
static void meson_mmc_set_tuning_params(struct mmc_host *mmc)
@@ -949,7 +947,7 @@ static int meson_mmc_probe(struct platform_device *pdev)
949947
meson_mmc_irq_thread, IRQF_SHARED,
950948
NULL, host);
951949
if (ret)
952-
goto err_div_clk;
950+
goto err_init_clk;
953951

954952
mmc->caps |= MMC_CAP_CMD23;
955953
mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
@@ -965,7 +963,7 @@ static int meson_mmc_probe(struct platform_device *pdev)
965963
if (host->bounce_buf == NULL) {
966964
dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
967965
ret = -ENOMEM;
968-
goto err_div_clk;
966+
goto err_init_clk;
969967
}
970968

971969
host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
@@ -984,8 +982,8 @@ static int meson_mmc_probe(struct platform_device *pdev)
984982
err_bounce_buf:
985983
dma_free_coherent(host->dev, host->bounce_buf_size,
986984
host->bounce_buf, host->bounce_dma_addr);
987-
err_div_clk:
988-
clk_disable_unprepare(host->cfg_div_clk);
985+
err_init_clk:
986+
clk_disable_unprepare(host->mmc_clk);
989987
err_core_clk:
990988
clk_disable_unprepare(host->core_clk);
991989
free_host:
@@ -1007,7 +1005,7 @@ static int meson_mmc_remove(struct platform_device *pdev)
10071005
dma_free_coherent(host->dev, host->bounce_buf_size,
10081006
host->bounce_buf, host->bounce_dma_addr);
10091007

1010-
clk_disable_unprepare(host->cfg_div_clk);
1008+
clk_disable_unprepare(host->mmc_clk);
10111009
clk_disable_unprepare(host->core_clk);
10121010

10131011
mmc_free_host(host->mmc);

0 commit comments

Comments
 (0)