1 // SPDX-License-Identifier: GPL-2.0
3 // Helper routines for R-Car sound ADG.
5 // Copyright (C) 2013 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 #include <linux/clk-provider.h>
22 #define BRRx_MASK(x) (0x3FF & x)
24 static struct rsnd_mod_ops adg_ops = {
29 struct clk *clk[CLKMAX];
30 struct clk *clkout[CLKOUTMAX];
31 struct clk_onecell_data onecell;
39 int rbga_rate_for_441khz; /* RBGA */
40 int rbgb_rate_for_48khz; /* RBGB */
43 #define LRCLK_ASYNC (1 << 0)
44 #define AUDIO_OUT_48 (1 << 1)
46 #define for_each_rsnd_clk(pos, adg, i) \
49 ((pos) = adg->clk[i]); \
51 #define for_each_rsnd_clkout(pos, adg, i) \
54 ((pos) = adg->clkout[i]); \
56 #define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
58 static const char * const clk_name[] = {
65 static u32 rsnd_adg_calculate_rbgx(unsigned long div)
72 for (i = 3; i >= 0; i--) {
74 if (0 == (div % ratio))
75 return (u32)((i << 8) | ((div / ratio) - 1));
81 static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
83 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
84 int id = rsnd_mod_id(ssi_mod);
87 if (rsnd_ssi_is_pin_sharing(io)) {
103 return (0x6 + ws) << 8;
106 static void __rsnd_adg_get_timesel_ratio(struct rsnd_priv *priv,
107 struct rsnd_dai_stream *io,
108 unsigned int target_rate,
109 unsigned int *target_val,
110 unsigned int *target_en)
112 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
113 struct device *dev = rsnd_priv_to_dev(priv);
114 int idx, sel, div, step;
115 unsigned int val, en;
116 unsigned int min, diff;
117 unsigned int sel_rate[] = {
118 adg->clk_rate[CLKA], /* 0000: CLKA */
119 adg->clk_rate[CLKB], /* 0001: CLKB */
120 adg->clk_rate[CLKC], /* 0010: CLKC */
121 adg->rbga_rate_for_441khz, /* 0011: RBGA */
122 adg->rbgb_rate_for_48khz, /* 0100: RBGB */
128 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
135 for (div = 2; div <= 98304; div += step) {
136 diff = abs(target_rate - sel_rate[sel] / div);
138 val = (sel << 8) | idx;
140 en = 1 << (sel + 1); /* fixme */
144 * step of 0_0000 / 0_0001 / 0_1101
147 if ((idx > 2) && (idx % 2))
158 dev_err(dev, "no Input clock\n");
167 static void rsnd_adg_get_timesel_ratio(struct rsnd_priv *priv,
168 struct rsnd_dai_stream *io,
169 unsigned int in_rate,
170 unsigned int out_rate,
171 u32 *in, u32 *out, u32 *en)
173 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
174 unsigned int target_rate;
180 /* default = SSI WS */
182 _out = rsnd_adg_ssi_ws_timing_gen2(io);
187 if (runtime->rate != in_rate) {
188 target_rate = out_rate;
190 } else if (runtime->rate != out_rate) {
191 target_rate = in_rate;
196 __rsnd_adg_get_timesel_ratio(priv, io,
208 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *cmd_mod,
209 struct rsnd_dai_stream *io)
211 struct rsnd_priv *priv = rsnd_mod_to_priv(cmd_mod);
212 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
213 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
214 int id = rsnd_mod_id(cmd_mod);
215 int shift = (id % 2) ? 16 : 0;
218 rsnd_adg_get_timesel_ratio(priv, io,
219 rsnd_src_get_in_rate(priv, io),
220 rsnd_src_get_out_rate(priv, io),
224 mask = 0x0f1f << shift;
226 rsnd_mod_bset(adg_mod, CMDOUT_TIMSEL, mask, val);
231 int rsnd_adg_set_src_timesel_gen2(struct rsnd_mod *src_mod,
232 struct rsnd_dai_stream *io,
233 unsigned int in_rate,
234 unsigned int out_rate)
236 struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
237 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
238 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
241 int id = rsnd_mod_id(src_mod);
242 int shift = (id % 2) ? 16 : 0;
244 rsnd_mod_confirm_src(src_mod);
246 rsnd_adg_get_timesel_ratio(priv, io,
252 mask = 0x0f1f << shift;
254 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL(id / 2), mask, in);
255 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL(id / 2), mask, out);
258 rsnd_mod_bset(adg_mod, DIV_EN, en, en);
263 static void rsnd_adg_set_ssi_clk(struct rsnd_mod *ssi_mod, u32 val)
265 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
266 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
267 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
268 struct device *dev = rsnd_priv_to_dev(priv);
269 int id = rsnd_mod_id(ssi_mod);
270 int shift = (id % 4) * 8;
271 u32 mask = 0xFF << shift;
273 rsnd_mod_confirm_ssi(ssi_mod);
278 * SSI 8 is not connected to ADG.
279 * it works with SSI 7
284 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL(id / 4), mask, val);
286 dev_dbg(dev, "AUDIO_CLK_SEL is 0x%x\n", val);
289 int rsnd_adg_clk_query(struct rsnd_priv *priv, unsigned int rate)
291 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
302 * find suitable clock from
303 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
305 for_each_rsnd_clk(clk, adg, i) {
306 if (rate == adg->clk_rate[i])
311 * find divided clock from BRGA/BRGB
313 if (rate == adg->rbga_rate_for_441khz)
316 if (rate == adg->rbgb_rate_for_48khz)
322 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *ssi_mod)
324 rsnd_adg_set_ssi_clk(ssi_mod, 0);
329 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *ssi_mod, unsigned int rate)
331 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
332 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
333 struct device *dev = rsnd_priv_to_dev(priv);
334 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
338 data = rsnd_adg_clk_query(priv, rate);
342 rsnd_adg_set_ssi_clk(ssi_mod, data);
344 if (rsnd_flags_has(adg, LRCLK_ASYNC)) {
345 if (rsnd_flags_has(adg, AUDIO_OUT_48))
348 if (0 == (rate % 8000))
352 rsnd_mod_bset(adg_mod, BRGCKR, 0x80770000, adg->ckr | ckr);
353 rsnd_mod_write(adg_mod, BRRA, adg->rbga);
354 rsnd_mod_write(adg_mod, BRRB, adg->rbgb);
356 dev_dbg(dev, "CLKOUT is based on BRG%c (= %dHz)\n",
358 (ckr) ? adg->rbgb_rate_for_48khz :
359 adg->rbga_rate_for_441khz);
364 void rsnd_adg_clk_control(struct rsnd_priv *priv, int enable)
366 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
367 struct device *dev = rsnd_priv_to_dev(priv);
371 for_each_rsnd_clk(clk, adg, i) {
374 ret = clk_prepare_enable(clk);
377 * We shouldn't use clk_get_rate() under
378 * atomic context. Let's keep it when
379 * rsnd_adg_clk_enable() was called
381 adg->clk_rate[i] = clk_get_rate(adg->clk[i]);
383 clk_disable_unprepare(clk);
387 dev_warn(dev, "can't use clk %d\n", i);
391 static void rsnd_adg_get_clkin(struct rsnd_priv *priv,
392 struct rsnd_adg *adg)
394 struct device *dev = rsnd_priv_to_dev(priv);
398 for (i = 0; i < CLKMAX; i++) {
399 clk = devm_clk_get(dev, clk_name[i]);
400 adg->clk[i] = IS_ERR(clk) ? NULL : clk;
404 static void rsnd_adg_get_clkout(struct rsnd_priv *priv,
405 struct rsnd_adg *adg)
408 struct device *dev = rsnd_priv_to_dev(priv);
409 struct device_node *np = dev->of_node;
410 struct property *prop;
411 u32 ckr, rbgx, rbga, rbgb;
414 u32 req_rate[REQ_SIZE] = {};
416 unsigned long req_48kHz_rate, req_441kHz_rate;
418 const char *parent_clk_name = NULL;
419 static const char * const clkout_name[] = {
420 [CLKOUT] = "audio_clkout",
421 [CLKOUT1] = "audio_clkout1",
422 [CLKOUT2] = "audio_clkout2",
423 [CLKOUT3] = "audio_clkout3",
433 rbga = 2; /* default 1/6 */
434 rbgb = 2; /* default 1/6 */
437 * ADG supports BRRA/BRRB output only
438 * this means all clkout0/1/2/3 will be same rate
440 prop = of_find_property(np, "clock-frequency", NULL);
442 goto rsnd_adg_get_clkout_end;
444 req_size = prop->length / sizeof(u32);
445 if (req_size > REQ_SIZE) {
447 "too many clock-frequency, use top %d\n", REQ_SIZE);
451 of_property_read_u32_array(np, "clock-frequency", req_rate, req_size);
454 for (i = 0; i < req_size; i++) {
455 if (0 == (req_rate[i] % 44100))
456 req_441kHz_rate = req_rate[i];
457 if (0 == (req_rate[i] % 48000))
458 req_48kHz_rate = req_rate[i];
461 if (req_rate[0] % 48000 == 0)
462 rsnd_flags_set(adg, AUDIO_OUT_48);
464 if (of_get_property(np, "clkout-lr-asynchronous", NULL))
465 rsnd_flags_set(adg, LRCLK_ASYNC);
468 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
469 * have 44.1kHz or 48kHz base clocks for now.
471 * SSI itself can divide parent clock by 1/1 - 1/16
473 * rsnd_adg_ssi_clk_try_start()
474 * rsnd_ssi_master_clk_start()
476 adg->rbga_rate_for_441khz = 0;
477 adg->rbgb_rate_for_48khz = 0;
478 for_each_rsnd_clk(clk, adg, i) {
479 rate = clk_get_rate(clk);
481 if (0 == rate) /* not used */
485 if (!adg->rbga_rate_for_441khz && (0 == rate % 44100)) {
488 div = rate / req_441kHz_rate;
489 rbgx = rsnd_adg_calculate_rbgx(div);
490 if (BRRx_MASK(rbgx) == rbgx) {
492 adg->rbga_rate_for_441khz = rate / div;
493 ckr |= brg_table[i] << 20;
494 if (req_441kHz_rate &&
495 !rsnd_flags_has(adg, AUDIO_OUT_48))
496 parent_clk_name = __clk_get_name(clk);
501 if (!adg->rbgb_rate_for_48khz && (0 == rate % 48000)) {
504 div = rate / req_48kHz_rate;
505 rbgx = rsnd_adg_calculate_rbgx(div);
506 if (BRRx_MASK(rbgx) == rbgx) {
508 adg->rbgb_rate_for_48khz = rate / div;
509 ckr |= brg_table[i] << 16;
510 if (req_48kHz_rate &&
511 rsnd_flags_has(adg, AUDIO_OUT_48))
512 parent_clk_name = __clk_get_name(clk);
518 * ADG supports BRRA/BRRB output only.
519 * this means all clkout0/1/2/3 will be * same rate
522 of_property_read_u32(np, "#clock-cells", &count);
527 clk = clk_register_fixed_rate(dev, clkout_name[CLKOUT],
528 parent_clk_name, 0, req_rate[0]);
530 adg->clkout[CLKOUT] = clk;
531 of_clk_add_provider(np, of_clk_src_simple_get, clk);
538 for (i = 0; i < CLKOUTMAX; i++) {
539 clk = clk_register_fixed_rate(dev, clkout_name[i],
543 adg->clkout[i] = clk;
545 adg->onecell.clks = adg->clkout;
546 adg->onecell.clk_num = CLKOUTMAX;
547 of_clk_add_provider(np, of_clk_src_onecell_get,
551 rsnd_adg_get_clkout_end:
558 static void rsnd_adg_clk_dbg_info(struct rsnd_priv *priv, struct rsnd_adg *adg)
560 struct device *dev = rsnd_priv_to_dev(priv);
564 for_each_rsnd_clk(clk, adg, i)
565 dev_dbg(dev, "%s : %pa : %ld\n",
566 clk_name[i], clk, clk_get_rate(clk));
568 dev_dbg(dev, "BRGCKR = 0x%08x, BRRA/BRRB = 0x%x/0x%x\n",
569 adg->ckr, adg->rbga, adg->rbgb);
570 dev_dbg(dev, "BRGA (for 44100 base) = %d\n", adg->rbga_rate_for_441khz);
571 dev_dbg(dev, "BRGB (for 48000 base) = %d\n", adg->rbgb_rate_for_48khz);
574 * Actual CLKOUT will be exchanged in rsnd_adg_ssi_clk_try_start()
575 * by BRGCKR::BRGCKR_31
577 for_each_rsnd_clkout(clk, adg, i)
578 dev_dbg(dev, "clkout %d : %pa : %ld\n", i,
579 clk, clk_get_rate(clk));
582 #define rsnd_adg_clk_dbg_info(priv, adg)
585 int rsnd_adg_probe(struct rsnd_priv *priv)
587 struct rsnd_adg *adg;
588 struct device *dev = rsnd_priv_to_dev(priv);
591 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
595 ret = rsnd_mod_init(priv, &adg->mod, &adg_ops,
600 rsnd_adg_get_clkin(priv, adg);
601 rsnd_adg_get_clkout(priv, adg);
602 rsnd_adg_clk_dbg_info(priv, adg);
606 rsnd_adg_clk_enable(priv);
611 void rsnd_adg_remove(struct rsnd_priv *priv)
613 struct device *dev = rsnd_priv_to_dev(priv);
614 struct device_node *np = dev->of_node;
615 struct rsnd_adg *adg = priv->adg;
619 for_each_rsnd_clkout(clk, adg, i)
621 clk_unregister_fixed_rate(adg->clkout[i]);
623 of_clk_del_provider(np);
625 rsnd_adg_clk_disable(priv);