2 * Helper routines for R-Car sound ADG.
4 * Copyright (C) 2013 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/sh_clk.h>
20 struct clk *clk[CLKMAX];
22 int rbga_rate_for_441khz_div_6; /* RBGA */
23 int rbgb_rate_for_48khz_div_6; /* RBGB */
27 #define for_each_rsnd_clk(pos, adg, i) \
28 for (i = 0, (pos) = adg->clk[i]; \
30 i++, (pos) = adg->clk[i])
31 #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
33 static int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
35 unsigned int src_rate,
36 unsigned int dst_rate)
38 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
39 struct device *dev = rsnd_priv_to_dev(priv);
40 int idx, sel, div, shift;
42 int id = rsnd_mod_id(mod);
43 unsigned int sel_rate [] = {
44 clk_get_rate(adg->clk[CLKA]), /* 000: CLKA */
45 clk_get_rate(adg->clk[CLKB]), /* 001: CLKB */
46 clk_get_rate(adg->clk[CLKC]), /* 010: CLKC */
47 0, /* 011: MLBCLK (not used) */
48 adg->rbga_rate_for_441khz_div_6,/* 100: RBGA */
49 adg->rbgb_rate_for_48khz_div_6, /* 101: RBGB */
52 /* find div (= 1/128, 1/256, 1/512, 1/1024, 1/2048 */
53 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
54 for (div = 128, idx = 0;
57 if (src_rate == sel_rate[sel] / div) {
58 val = (idx << 4) | sel;
63 dev_err(dev, "can't find convert src clk\n");
71 dev_dbg(dev, "adg convert src clk = %02x\n", val);
75 rsnd_mod_bset(mod, AUDIO_CLK_SEL3, mask, val);
78 rsnd_mod_bset(mod, AUDIO_CLK_SEL4, mask, val);
81 rsnd_mod_bset(mod, AUDIO_CLK_SEL5, mask, val);
86 * Gen1 doesn't need dst_rate settings,
87 * since it uses SSI WS pin.
88 * see also rsnd_src_set_route_if_gen1()
94 int rsnd_adg_set_convert_clk(struct rsnd_priv *priv,
96 unsigned int src_rate,
97 unsigned int dst_rate)
99 if (rsnd_is_gen1(priv))
100 return rsnd_adg_set_convert_clk_gen1(priv, mod,
106 static void rsnd_adg_set_ssi_clk(struct rsnd_mod *mod, u32 val)
108 int id = rsnd_mod_id(mod);
109 int shift = (id % 4) * 8;
110 u32 mask = 0xFF << shift;
115 * SSI 8 is not connected to ADG.
116 * it works with SSI 7
123 rsnd_mod_bset(mod, AUDIO_CLK_SEL0, mask, val);
126 rsnd_mod_bset(mod, AUDIO_CLK_SEL1, mask, val);
129 rsnd_mod_bset(mod, AUDIO_CLK_SEL2, mask, val);
134 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod)
137 * "mod" = "ssi" here.
138 * we can get "ssi id" from mod
140 rsnd_adg_set_ssi_clk(mod, 0);
145 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate)
147 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
148 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
149 struct device *dev = rsnd_priv_to_dev(priv);
160 dev_dbg(dev, "request clock = %d\n", rate);
163 * find suitable clock from
164 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
167 for_each_rsnd_clk(clk, adg, i) {
168 if (rate == clk_get_rate(clk)) {
175 * find 1/6 clock from BRGA/BRGB
177 if (rate == adg->rbga_rate_for_441khz_div_6) {
182 if (rate == adg->rbgb_rate_for_48khz_div_6) {
191 /* see rsnd_adg_ssi_clk_init() */
192 rsnd_mod_bset(mod, SSICKR, 0x00FF0000, adg->ckr);
193 rsnd_mod_write(mod, BRRA, 0x00000002); /* 1/6 */
194 rsnd_mod_write(mod, BRRB, 0x00000002); /* 1/6 */
197 * This "mod" = "ssi" here.
198 * we can get "ssi id" from mod
200 rsnd_adg_set_ssi_clk(mod, data);
202 dev_dbg(dev, "ADG: ssi%d selects clk%d = %d",
203 rsnd_mod_id(mod), i, rate);
208 static void rsnd_adg_ssi_clk_init(struct rsnd_priv *priv, struct rsnd_adg *adg)
222 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
223 * have 44.1kHz or 48kHz base clocks for now.
225 * SSI itself can divide parent clock by 1/1 - 1/16
226 * So, BRGA outputs 44.1kHz base parent clock 1/32,
227 * and, BRGB outputs 48.0kHz base parent clock 1/32 here.
229 * rsnd_adg_ssi_clk_try_start()
232 adg->rbga_rate_for_441khz_div_6 = 0;
233 adg->rbgb_rate_for_48khz_div_6 = 0;
234 for_each_rsnd_clk(clk, adg, i) {
235 rate = clk_get_rate(clk);
237 if (0 == rate) /* not used */
241 if (!adg->rbga_rate_for_441khz_div_6 && (0 == rate % 44100)) {
242 adg->rbga_rate_for_441khz_div_6 = rate / 6;
243 ckr |= brg_table[i] << 20;
247 if (!adg->rbgb_rate_for_48khz_div_6 && (0 == rate % 48000)) {
248 adg->rbgb_rate_for_48khz_div_6 = rate / 6;
249 ckr |= brg_table[i] << 16;
256 int rsnd_adg_probe(struct platform_device *pdev,
257 struct rcar_snd_info *info,
258 struct rsnd_priv *priv)
260 struct rsnd_adg *adg;
261 struct device *dev = rsnd_priv_to_dev(priv);
265 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
267 dev_err(dev, "ADG allocate failed\n");
271 adg->clk[CLKA] = clk_get(NULL, "audio_clk_a");
272 adg->clk[CLKB] = clk_get(NULL, "audio_clk_b");
273 adg->clk[CLKC] = clk_get(NULL, "audio_clk_c");
274 adg->clk[CLKI] = clk_get(NULL, "audio_clk_internal");
275 for_each_rsnd_clk(clk, adg, i) {
277 dev_err(dev, "Audio clock failed\n");
282 rsnd_adg_ssi_clk_init(priv, adg);
286 dev_dbg(dev, "adg probed\n");
291 void rsnd_adg_remove(struct platform_device *pdev,
292 struct rsnd_priv *priv)
294 struct rsnd_adg *adg = priv->adg;
298 for_each_rsnd_clk(clk, adg, i)