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) \
30 ((pos) = adg->clk[i]); \
32 #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
35 static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
37 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
38 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
39 int id = rsnd_mod_id(mod);
42 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
57 return (0x6 + ws) << 8;
60 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *mod,
61 struct rsnd_dai_stream *io)
63 int id = rsnd_mod_id(mod);
64 int shift = (id % 2) ? 16 : 0;
67 val = rsnd_adg_ssi_ws_timing_gen2(io);
70 mask = 0xffff << shift;
72 rsnd_mod_bset(mod, CMDOUT_TIMSEL, mask, val);
77 static int rsnd_adg_set_src_timsel_gen2(struct rsnd_mod *mod,
78 struct rsnd_dai_stream *io,
81 int is_play = rsnd_io_is_play(io);
82 int id = rsnd_mod_id(mod);
83 int shift = (id % 2) ? 16 : 0;
87 ws = rsnd_adg_ssi_ws_timing_gen2(io);
89 in = (is_play) ? timsel : ws;
90 out = (is_play) ? ws : timsel;
94 mask = 0xffff << shift;
98 rsnd_mod_bset(mod, SRCIN_TIMSEL0, mask, in);
99 rsnd_mod_bset(mod, SRCOUT_TIMSEL0, mask, out);
102 rsnd_mod_bset(mod, SRCIN_TIMSEL1, mask, in);
103 rsnd_mod_bset(mod, SRCOUT_TIMSEL1, mask, out);
106 rsnd_mod_bset(mod, SRCIN_TIMSEL2, mask, in);
107 rsnd_mod_bset(mod, SRCOUT_TIMSEL2, mask, out);
110 rsnd_mod_bset(mod, SRCIN_TIMSEL3, mask, in);
111 rsnd_mod_bset(mod, SRCOUT_TIMSEL3, mask, out);
114 rsnd_mod_bset(mod, SRCIN_TIMSEL4, mask, in);
115 rsnd_mod_bset(mod, SRCOUT_TIMSEL4, mask, out);
122 int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *mod,
123 struct rsnd_dai_stream *io,
124 unsigned int src_rate,
125 unsigned int dst_rate)
127 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
128 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
129 struct device *dev = rsnd_priv_to_dev(priv);
130 int idx, sel, div, step, ret;
132 unsigned int min, diff;
133 unsigned int sel_rate [] = {
134 clk_get_rate(adg->clk[CLKA]), /* 0000: CLKA */
135 clk_get_rate(adg->clk[CLKB]), /* 0001: CLKB */
136 clk_get_rate(adg->clk[CLKC]), /* 0010: CLKC */
137 adg->rbga_rate_for_441khz_div_6,/* 0011: RBGA */
138 adg->rbgb_rate_for_48khz_div_6, /* 0100: RBGB */
144 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
151 for (div = 2; div <= 98304; div += step) {
152 diff = abs(src_rate - sel_rate[sel] / div);
154 val = (sel << 8) | idx;
156 en = 1 << (sel + 1); /* fixme */
160 * step of 0_0000 / 0_0001 / 0_1101
163 if ((idx > 2) && (idx % 2))
174 dev_err(dev, "no Input clock\n");
178 ret = rsnd_adg_set_src_timsel_gen2(mod, io, val);
180 dev_err(dev, "timsel error\n");
184 rsnd_mod_bset(mod, DIV_EN, en, en);
186 dev_dbg(dev, "convert rate %d <-> %d\n", src_rate, dst_rate);
191 int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *mod,
192 struct rsnd_dai_stream *io)
194 u32 val = rsnd_adg_ssi_ws_timing_gen2(io);
196 return rsnd_adg_set_src_timsel_gen2(mod, io, val);
199 int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
200 struct rsnd_mod *mod,
201 unsigned int src_rate,
202 unsigned int dst_rate)
204 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
205 struct device *dev = rsnd_priv_to_dev(priv);
206 int idx, sel, div, shift;
208 int id = rsnd_mod_id(mod);
209 unsigned int sel_rate [] = {
210 clk_get_rate(adg->clk[CLKA]), /* 000: CLKA */
211 clk_get_rate(adg->clk[CLKB]), /* 001: CLKB */
212 clk_get_rate(adg->clk[CLKC]), /* 010: CLKC */
213 0, /* 011: MLBCLK (not used) */
214 adg->rbga_rate_for_441khz_div_6,/* 100: RBGA */
215 adg->rbgb_rate_for_48khz_div_6, /* 101: RBGB */
218 /* find div (= 1/128, 1/256, 1/512, 1/1024, 1/2048 */
219 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
220 for (div = 128, idx = 0;
223 if (src_rate == sel_rate[sel] / div) {
224 val = (idx << 4) | sel;
229 dev_err(dev, "can't find convert src clk\n");
233 shift = (id % 4) * 8;
234 mask = 0xFF << shift;
237 dev_dbg(dev, "adg convert src clk = %02x\n", val);
241 rsnd_mod_bset(mod, AUDIO_CLK_SEL3, mask, val);
244 rsnd_mod_bset(mod, AUDIO_CLK_SEL4, mask, val);
247 rsnd_mod_bset(mod, AUDIO_CLK_SEL5, mask, val);
252 * Gen1 doesn't need dst_rate settings,
253 * since it uses SSI WS pin.
254 * see also rsnd_src_set_route_if_gen1()
260 static void rsnd_adg_set_ssi_clk(struct rsnd_mod *mod, u32 val)
262 int id = rsnd_mod_id(mod);
263 int shift = (id % 4) * 8;
264 u32 mask = 0xFF << shift;
269 * SSI 8 is not connected to ADG.
270 * it works with SSI 7
277 rsnd_mod_bset(mod, AUDIO_CLK_SEL0, mask, val);
280 rsnd_mod_bset(mod, AUDIO_CLK_SEL1, mask, val);
283 rsnd_mod_bset(mod, AUDIO_CLK_SEL2, mask, val);
288 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod)
291 * "mod" = "ssi" here.
292 * we can get "ssi id" from mod
294 rsnd_adg_set_ssi_clk(mod, 0);
299 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate)
301 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
302 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
303 struct device *dev = rsnd_priv_to_dev(priv);
314 dev_dbg(dev, "request clock = %d\n", rate);
317 * find suitable clock from
318 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
321 for_each_rsnd_clk(clk, adg, i) {
322 if (rate == clk_get_rate(clk)) {
329 * find 1/6 clock from BRGA/BRGB
331 if (rate == adg->rbga_rate_for_441khz_div_6) {
336 if (rate == adg->rbgb_rate_for_48khz_div_6) {
345 /* see rsnd_adg_ssi_clk_init() */
346 rsnd_mod_bset(mod, SSICKR, 0x00FF0000, adg->ckr);
347 rsnd_mod_write(mod, BRRA, 0x00000002); /* 1/6 */
348 rsnd_mod_write(mod, BRRB, 0x00000002); /* 1/6 */
351 * This "mod" = "ssi" here.
352 * we can get "ssi id" from mod
354 rsnd_adg_set_ssi_clk(mod, data);
356 dev_dbg(dev, "ADG: ssi%d selects clk%d = %d",
357 rsnd_mod_id(mod), i, rate);
362 static void rsnd_adg_ssi_clk_init(struct rsnd_priv *priv, struct rsnd_adg *adg)
376 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
377 * have 44.1kHz or 48kHz base clocks for now.
379 * SSI itself can divide parent clock by 1/1 - 1/16
380 * So, BRGA outputs 44.1kHz base parent clock 1/32,
381 * and, BRGB outputs 48.0kHz base parent clock 1/32 here.
383 * rsnd_adg_ssi_clk_try_start()
386 adg->rbga_rate_for_441khz_div_6 = 0;
387 adg->rbgb_rate_for_48khz_div_6 = 0;
388 for_each_rsnd_clk(clk, adg, i) {
389 rate = clk_get_rate(clk);
391 if (0 == rate) /* not used */
395 if (!adg->rbga_rate_for_441khz_div_6 && (0 == rate % 44100)) {
396 adg->rbga_rate_for_441khz_div_6 = rate / 6;
397 ckr |= brg_table[i] << 20;
401 if (!adg->rbgb_rate_for_48khz_div_6 && (0 == rate % 48000)) {
402 adg->rbgb_rate_for_48khz_div_6 = rate / 6;
403 ckr |= brg_table[i] << 16;
410 int rsnd_adg_probe(struct platform_device *pdev,
411 const struct rsnd_of_data *of_data,
412 struct rsnd_priv *priv)
414 struct rsnd_adg *adg;
415 struct device *dev = rsnd_priv_to_dev(priv);
419 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
421 dev_err(dev, "ADG allocate failed\n");
425 adg->clk[CLKA] = devm_clk_get(dev, "clk_a");
426 adg->clk[CLKB] = devm_clk_get(dev, "clk_b");
427 adg->clk[CLKC] = devm_clk_get(dev, "clk_c");
428 adg->clk[CLKI] = devm_clk_get(dev, "clk_i");
430 for_each_rsnd_clk(clk, adg, i)
431 dev_dbg(dev, "clk %d : %p : %ld\n", i, clk, clk_get_rate(clk));
433 rsnd_adg_ssi_clk_init(priv, adg);