1*fcaf2036SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
213082398SMarek Vasut /*
313082398SMarek Vasut * Copyright 2012 DENX Software Engineering, GmbH
413082398SMarek Vasut *
513082398SMarek Vasut * Pulled from code:
613082398SMarek Vasut * Portions copyright (C) 2003 Russell King, PXA MMCI Driver
713082398SMarek Vasut * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver
813082398SMarek Vasut *
913082398SMarek Vasut * Copyright 2008 Embedded Alley Solutions, Inc.
1013082398SMarek Vasut * Copyright 2009-2011 Freescale Semiconductor, Inc.
1113082398SMarek Vasut */
1213082398SMarek Vasut
1313082398SMarek Vasut #include <linux/kernel.h>
1413082398SMarek Vasut #include <linux/init.h>
1513082398SMarek Vasut #include <linux/clk.h>
1613082398SMarek Vasut #include <linux/module.h>
1713082398SMarek Vasut #include <linux/device.h>
1813082398SMarek Vasut #include <linux/io.h>
1913082398SMarek Vasut #include <linux/spi/mxs-spi.h>
2013082398SMarek Vasut
mxs_ssp_set_clk_rate(struct mxs_ssp * ssp,unsigned int rate)2113082398SMarek Vasut void mxs_ssp_set_clk_rate(struct mxs_ssp *ssp, unsigned int rate)
2213082398SMarek Vasut {
2313082398SMarek Vasut unsigned int ssp_clk, ssp_sck;
2413082398SMarek Vasut u32 clock_divide, clock_rate;
2513082398SMarek Vasut u32 val;
2613082398SMarek Vasut
2713082398SMarek Vasut ssp_clk = clk_get_rate(ssp->clk);
2813082398SMarek Vasut
2913082398SMarek Vasut for (clock_divide = 2; clock_divide <= 254; clock_divide += 2) {
3013082398SMarek Vasut clock_rate = DIV_ROUND_UP(ssp_clk, rate * clock_divide);
3113082398SMarek Vasut clock_rate = (clock_rate > 0) ? clock_rate - 1 : 0;
3213082398SMarek Vasut if (clock_rate <= 255)
3313082398SMarek Vasut break;
3413082398SMarek Vasut }
3513082398SMarek Vasut
3613082398SMarek Vasut if (clock_divide > 254) {
3713082398SMarek Vasut dev_err(ssp->dev,
3813082398SMarek Vasut "%s: cannot set clock to %d\n", __func__, rate);
3913082398SMarek Vasut return;
4013082398SMarek Vasut }
4113082398SMarek Vasut
4213082398SMarek Vasut ssp_sck = ssp_clk / clock_divide / (1 + clock_rate);
4313082398SMarek Vasut
4413082398SMarek Vasut val = readl(ssp->base + HW_SSP_TIMING(ssp));
4513082398SMarek Vasut val &= ~(BM_SSP_TIMING_CLOCK_DIVIDE | BM_SSP_TIMING_CLOCK_RATE);
4613082398SMarek Vasut val |= BF_SSP(clock_divide, TIMING_CLOCK_DIVIDE);
4713082398SMarek Vasut val |= BF_SSP(clock_rate, TIMING_CLOCK_RATE);
4813082398SMarek Vasut writel(val, ssp->base + HW_SSP_TIMING(ssp));
4913082398SMarek Vasut
5013082398SMarek Vasut ssp->clk_rate = ssp_sck;
5113082398SMarek Vasut
5213082398SMarek Vasut dev_dbg(ssp->dev,
5313082398SMarek Vasut "%s: clock_divide %d, clock_rate %d, ssp_clk %d, rate_actual %d, rate_requested %d\n",
5413082398SMarek Vasut __func__, clock_divide, clock_rate, ssp_clk, ssp_sck, rate);
5513082398SMarek Vasut }
5613082398SMarek Vasut EXPORT_SYMBOL_GPL(mxs_ssp_set_clk_rate);
57