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/clk-provider.h>
25 #define BRRx_MASK(x) (0x3FF & x)
27 static struct rsnd_mod_ops adg_ops = {
32 struct clk *clk[CLKMAX];
33 struct clk *clkout[CLKOUTMAX];
34 struct clk_onecell_data onecell;
37 int rbga_rate_for_441khz; /* RBGA */
38 int rbgb_rate_for_48khz; /* RBGB */
41 #define for_each_rsnd_clk(pos, adg, i) \
44 ((pos) = adg->clk[i]); \
46 #define for_each_rsnd_clkout(pos, adg, i) \
49 ((pos) = adg->clkout[i]); \
51 #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
53 static u32 rsnd_adg_calculate_rbgx(unsigned long div)
60 for (i = 3; i >= 0; i--) {
62 if (0 == (div % ratio))
63 return (u32)((i << 8) | ((div / ratio) - 1));
69 static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
71 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
72 int id = rsnd_mod_id(ssi_mod);
75 if (rsnd_ssi_is_pin_sharing(io)) {
90 return (0x6 + ws) << 8;
93 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *cmd_mod,
94 struct rsnd_dai_stream *io)
96 struct rsnd_priv *priv = rsnd_mod_to_priv(cmd_mod);
97 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
98 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
99 int id = rsnd_mod_id(cmd_mod);
100 int shift = (id % 2) ? 16 : 0;
103 val = rsnd_adg_ssi_ws_timing_gen2(io);
106 mask = 0xffff << shift;
108 rsnd_mod_bset(adg_mod, CMDOUT_TIMSEL, mask, val);
113 static int rsnd_adg_set_src_timsel_gen2(struct rsnd_mod *src_mod,
114 struct rsnd_dai_stream *io,
117 struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
118 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
119 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
120 int is_play = rsnd_io_is_play(io);
121 int id = rsnd_mod_id(src_mod);
122 int shift = (id % 2) ? 16 : 0;
126 rsnd_mod_confirm_src(src_mod);
128 ws = rsnd_adg_ssi_ws_timing_gen2(io);
130 in = (is_play) ? timsel : ws;
131 out = (is_play) ? ws : timsel;
135 mask = 0xffff << shift;
139 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL0, mask, in);
140 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL0, mask, out);
143 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL1, mask, in);
144 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL1, mask, out);
147 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL2, mask, in);
148 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL2, mask, out);
151 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL3, mask, in);
152 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL3, mask, out);
155 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL4, mask, in);
156 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL4, mask, out);
163 int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *src_mod,
164 struct rsnd_dai_stream *io,
165 unsigned int src_rate,
166 unsigned int dst_rate)
168 struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
169 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
170 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
171 struct device *dev = rsnd_priv_to_dev(priv);
172 int idx, sel, div, step, ret;
174 unsigned int min, diff;
175 unsigned int sel_rate [] = {
176 clk_get_rate(adg->clk[CLKA]), /* 0000: CLKA */
177 clk_get_rate(adg->clk[CLKB]), /* 0001: CLKB */
178 clk_get_rate(adg->clk[CLKC]), /* 0010: CLKC */
179 adg->rbga_rate_for_441khz, /* 0011: RBGA */
180 adg->rbgb_rate_for_48khz, /* 0100: RBGB */
183 rsnd_mod_confirm_src(src_mod);
188 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
195 for (div = 2; div <= 98304; div += step) {
196 diff = abs(src_rate - sel_rate[sel] / div);
198 val = (sel << 8) | idx;
200 en = 1 << (sel + 1); /* fixme */
204 * step of 0_0000 / 0_0001 / 0_1101
207 if ((idx > 2) && (idx % 2))
218 dev_err(dev, "no Input clock\n");
222 ret = rsnd_adg_set_src_timsel_gen2(src_mod, io, val);
224 dev_err(dev, "timsel error\n");
228 rsnd_mod_bset(adg_mod, DIV_EN, en, en);
230 dev_dbg(dev, "convert rate %d <-> %d\n", src_rate, dst_rate);
235 int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *src_mod,
236 struct rsnd_dai_stream *io)
238 u32 val = rsnd_adg_ssi_ws_timing_gen2(io);
240 rsnd_mod_confirm_src(src_mod);
242 return rsnd_adg_set_src_timsel_gen2(src_mod, io, val);
245 static void rsnd_adg_set_ssi_clk(struct rsnd_mod *ssi_mod, u32 val)
247 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
248 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
249 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
250 int id = rsnd_mod_id(ssi_mod);
251 int shift = (id % 4) * 8;
252 u32 mask = 0xFF << shift;
254 rsnd_mod_confirm_ssi(ssi_mod);
259 * SSI 8 is not connected to ADG.
260 * it works with SSI 7
267 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL0, mask, val);
270 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL1, mask, val);
273 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL2, mask, val);
278 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *ssi_mod)
280 rsnd_adg_set_ssi_clk(ssi_mod, 0);
285 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *ssi_mod, unsigned int rate)
287 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
288 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
289 struct device *dev = rsnd_priv_to_dev(priv);
300 dev_dbg(dev, "request clock = %d\n", rate);
303 * find suitable clock from
304 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
307 for_each_rsnd_clk(clk, adg, i) {
308 if (rate == clk_get_rate(clk)) {
315 * find divided clock from BRGA/BRGB
317 if (rate == adg->rbga_rate_for_441khz) {
322 if (rate == adg->rbgb_rate_for_48khz) {
331 rsnd_adg_set_ssi_clk(ssi_mod, data);
333 dev_dbg(dev, "ADG: %s[%d] selects 0x%x for %d\n",
334 rsnd_mod_name(ssi_mod), rsnd_mod_id(ssi_mod),
340 static void rsnd_adg_get_clkin(struct rsnd_priv *priv,
341 struct rsnd_adg *adg)
343 struct device *dev = rsnd_priv_to_dev(priv);
345 static const char * const clk_name[] = {
353 for (i = 0; i < CLKMAX; i++) {
354 clk = devm_clk_get(dev, clk_name[i]);
355 adg->clk[i] = IS_ERR(clk) ? NULL : clk;
358 for_each_rsnd_clk(clk, adg, i) {
359 ret = clk_prepare_enable(clk);
361 dev_warn(dev, "can't use clk %d\n", i);
363 dev_dbg(dev, "clk %d : %p : %ld\n", i, clk, clk_get_rate(clk));
367 static void rsnd_adg_get_clkout(struct rsnd_priv *priv,
368 struct rsnd_adg *adg)
371 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
372 struct device *dev = rsnd_priv_to_dev(priv);
373 struct device_node *np = dev->of_node;
374 u32 ckr, rbgx, rbga, rbgb;
375 u32 rate, req_rate = 0, div;
377 unsigned long req_48kHz_rate, req_441kHz_rate;
379 const char *parent_clk_name = NULL;
380 static const char * const clkout_name[] = {
381 [CLKOUT] = "audio_clkout",
382 [CLKOUT1] = "audio_clkout1",
383 [CLKOUT2] = "audio_clkout2",
384 [CLKOUT3] = "audio_clkout3",
393 of_property_read_u32(np, "#clock-cells", &count);
396 * ADG supports BRRA/BRRB output only
397 * this means all clkout0/1/2/3 will be same rate
399 of_property_read_u32(np, "clock-frequency", &req_rate);
402 if (0 == (req_rate % 44100))
403 req_441kHz_rate = req_rate;
404 if (0 == (req_rate % 48000))
405 req_48kHz_rate = req_rate;
408 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
409 * have 44.1kHz or 48kHz base clocks for now.
411 * SSI itself can divide parent clock by 1/1 - 1/16
413 * rsnd_adg_ssi_clk_try_start()
414 * rsnd_ssi_master_clk_start()
417 rbga = 2; /* default 1/6 */
418 rbgb = 2; /* default 1/6 */
419 adg->rbga_rate_for_441khz = 0;
420 adg->rbgb_rate_for_48khz = 0;
421 for_each_rsnd_clk(clk, adg, i) {
422 rate = clk_get_rate(clk);
424 if (0 == rate) /* not used */
428 if (!adg->rbga_rate_for_441khz && (0 == rate % 44100)) {
431 div = rate / req_441kHz_rate;
432 rbgx = rsnd_adg_calculate_rbgx(div);
433 if (BRRx_MASK(rbgx) == rbgx) {
435 adg->rbga_rate_for_441khz = rate / div;
436 ckr |= brg_table[i] << 20;
438 parent_clk_name = __clk_get_name(clk);
443 if (!adg->rbgb_rate_for_48khz && (0 == rate % 48000)) {
446 div = rate / req_48kHz_rate;
447 rbgx = rsnd_adg_calculate_rbgx(div);
448 if (BRRx_MASK(rbgx) == rbgx) {
450 adg->rbgb_rate_for_48khz = rate / div;
451 ckr |= brg_table[i] << 16;
452 if (req_48kHz_rate) {
453 parent_clk_name = __clk_get_name(clk);
461 * ADG supports BRRA/BRRB output only.
462 * this means all clkout0/1/2/3 will be * same rate
469 clk = clk_register_fixed_rate(dev, clkout_name[CLKOUT],
472 0 : CLK_IS_ROOT, req_rate);
474 adg->clkout[CLKOUT] = clk;
475 of_clk_add_provider(np, of_clk_src_simple_get, clk);
482 for (i = 0; i < CLKOUTMAX; i++) {
483 clk = clk_register_fixed_rate(dev, clkout_name[i],
489 adg->onecell.clks = adg->clkout;
490 adg->onecell.clk_num = CLKOUTMAX;
492 adg->clkout[i] = clk;
494 of_clk_add_provider(np, of_clk_src_onecell_get,
500 rsnd_mod_bset(adg_mod, SSICKR, 0x00FF0000, ckr);
501 rsnd_mod_write(adg_mod, BRRA, rbga);
502 rsnd_mod_write(adg_mod, BRRB, rbgb);
504 for_each_rsnd_clkout(clk, adg, i)
505 dev_dbg(dev, "clkout %d : %p : %ld\n", i, clk, clk_get_rate(clk));
506 dev_dbg(dev, "SSICKR = 0x%08x, BRRA/BRRB = 0x%x/0x%x\n",
510 int rsnd_adg_probe(struct rsnd_priv *priv)
512 struct rsnd_adg *adg;
513 struct device *dev = rsnd_priv_to_dev(priv);
515 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
517 dev_err(dev, "ADG allocate failed\n");
521 rsnd_mod_init(priv, &adg->mod, &adg_ops,
524 rsnd_adg_get_clkin(priv, adg);
525 rsnd_adg_get_clkout(priv, adg);
532 void rsnd_adg_remove(struct rsnd_priv *priv)
534 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
538 for_each_rsnd_clk(clk, adg, i) {
539 clk_disable_unprepare(clk);