blob: 41556b2ef21e85831a467d78903a7346dbab123b [file] [log] [blame]
Kuninori Morimotodfc94032013-07-21 21:36:46 -07001/*
2 * Helper routines for R-Car sound ADG.
3 *
4 * Copyright (C) 2013 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
5 *
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
8 * for more details.
9 */
10#include <linux/sh_clk.h>
Kuninori Morimotodfc94032013-07-21 21:36:46 -070011#include "rsnd.h"
12
13#define CLKA 0
14#define CLKB 1
15#define CLKC 2
16#define CLKI 3
17#define CLKMAX 4
18
19struct rsnd_adg {
20 struct clk *clk[CLKMAX];
21
Kuninori Morimoto7808aa32013-12-19 19:27:19 -080022 int rbga_rate_for_441khz_div_6; /* RBGA */
23 int rbgb_rate_for_48khz_div_6; /* RBGB */
Kuninori Morimotoefeb9702013-09-23 23:12:17 -070024 u32 ckr;
Kuninori Morimotodfc94032013-07-21 21:36:46 -070025};
26
27#define for_each_rsnd_clk(pos, adg, i) \
Kuninori Morimoto00463c112014-02-11 17:15:51 -080028 for (i = 0; \
29 (i < CLKMAX) && \
30 ((pos) = adg->clk[i]); \
31 i++)
Kuninori Morimotodfc94032013-07-21 21:36:46 -070032#define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
33
Kuninori Morimoto629509c2014-01-23 18:42:00 -080034
Kuninori Morimoto8467ded2014-03-02 23:43:33 -080035static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -080036{
Kuninori Morimoto8467ded2014-03-02 23:43:33 -080037 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -080038 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
39 int id = rsnd_mod_id(mod);
40 int ws = id;
41
42 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
43 switch (id) {
44 case 1:
45 case 2:
46 ws = 0;
47 break;
48 case 4:
49 ws = 3;
50 break;
51 case 8:
52 ws = 7;
53 break;
54 }
55 }
56
57 return (0x6 + ws) << 8;
58}
59
60static int rsnd_adg_set_src_timsel_gen2(struct rsnd_dai *rdai,
61 struct rsnd_mod *mod,
62 struct rsnd_dai_stream *io,
63 u32 timsel)
64{
65 int is_play = rsnd_dai_is_play(rdai, io);
66 int id = rsnd_mod_id(mod);
67 int shift = (id % 2) ? 16 : 0;
68 u32 mask, ws;
69 u32 in, out;
70
Kuninori Morimoto8467ded2014-03-02 23:43:33 -080071 ws = rsnd_adg_ssi_ws_timing_gen2(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -080072
73 in = (is_play) ? timsel : ws;
74 out = (is_play) ? ws : timsel;
75
76 in = in << shift;
77 out = out << shift;
78 mask = 0xffff << shift;
79
80 switch (id / 2) {
81 case 0:
82 rsnd_mod_bset(mod, SRCIN_TIMSEL0, mask, in);
83 rsnd_mod_bset(mod, SRCOUT_TIMSEL0, mask, out);
84 break;
85 case 1:
86 rsnd_mod_bset(mod, SRCIN_TIMSEL1, mask, in);
87 rsnd_mod_bset(mod, SRCOUT_TIMSEL1, mask, out);
88 break;
89 case 2:
90 rsnd_mod_bset(mod, SRCIN_TIMSEL2, mask, in);
91 rsnd_mod_bset(mod, SRCOUT_TIMSEL2, mask, out);
92 break;
93 case 3:
94 rsnd_mod_bset(mod, SRCIN_TIMSEL3, mask, in);
95 rsnd_mod_bset(mod, SRCOUT_TIMSEL3, mask, out);
96 break;
97 case 4:
98 rsnd_mod_bset(mod, SRCIN_TIMSEL4, mask, in);
99 rsnd_mod_bset(mod, SRCOUT_TIMSEL4, mask, out);
100 break;
101 }
102
103 return 0;
104}
105
106int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *mod,
107 struct rsnd_dai *rdai,
108 struct rsnd_dai_stream *io,
109 unsigned int src_rate,
110 unsigned int dst_rate)
111{
112 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
113 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
114 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800115 int idx, sel, div, step, ret;
116 u32 val, en;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800117 unsigned int min, diff;
118 unsigned int sel_rate [] = {
119 clk_get_rate(adg->clk[CLKA]), /* 0000: CLKA */
120 clk_get_rate(adg->clk[CLKB]), /* 0001: CLKB */
121 clk_get_rate(adg->clk[CLKC]), /* 0010: CLKC */
122 adg->rbga_rate_for_441khz_div_6,/* 0011: RBGA */
123 adg->rbgb_rate_for_48khz_div_6, /* 0100: RBGB */
124 };
125
126 min = ~0;
127 val = 0;
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800128 en = 0;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800129 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
130 idx = 0;
131 step = 2;
132
133 if (!sel_rate[sel])
134 continue;
135
136 for (div = 2; div <= 98304; div += step) {
137 diff = abs(src_rate - sel_rate[sel] / div);
138 if (min > diff) {
139 val = (sel << 8) | idx;
140 min = diff;
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800141 en = 1 << (sel + 1); /* fixme */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800142 }
143
144 /*
145 * step of 0_0000 / 0_0001 / 0_1101
146 * are out of order
147 */
148 if ((idx > 2) && (idx % 2))
149 step *= 2;
150 if (idx == 0x1c) {
151 div += step;
152 step *= 2;
153 }
154 idx++;
155 }
156 }
157
158 if (min == ~0) {
159 dev_err(dev, "no Input clock\n");
160 return -EIO;
161 }
162
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800163 ret = rsnd_adg_set_src_timsel_gen2(rdai, mod, io, val);
164 if (ret < 0) {
165 dev_err(dev, "timsel error\n");
166 return ret;
167 }
168
169 rsnd_mod_bset(mod, DIV_EN, en, en);
170
171 return 0;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800172}
173
174int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *mod,
175 struct rsnd_dai *rdai,
176 struct rsnd_dai_stream *io)
177{
Kuninori Morimoto8467ded2014-03-02 23:43:33 -0800178 u32 val = rsnd_adg_ssi_ws_timing_gen2(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800179
180 return rsnd_adg_set_src_timsel_gen2(rdai, mod, io, val);
181}
182
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800183int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
184 struct rsnd_mod *mod,
185 unsigned int src_rate,
186 unsigned int dst_rate)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800187{
188 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
189 struct device *dev = rsnd_priv_to_dev(priv);
190 int idx, sel, div, shift;
191 u32 mask, val;
192 int id = rsnd_mod_id(mod);
193 unsigned int sel_rate [] = {
194 clk_get_rate(adg->clk[CLKA]), /* 000: CLKA */
195 clk_get_rate(adg->clk[CLKB]), /* 001: CLKB */
196 clk_get_rate(adg->clk[CLKC]), /* 010: CLKC */
197 0, /* 011: MLBCLK (not used) */
198 adg->rbga_rate_for_441khz_div_6,/* 100: RBGA */
199 adg->rbgb_rate_for_48khz_div_6, /* 101: RBGB */
200 };
201
202 /* find div (= 1/128, 1/256, 1/512, 1/1024, 1/2048 */
203 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
204 for (div = 128, idx = 0;
205 div <= 2048;
206 div *= 2, idx++) {
207 if (src_rate == sel_rate[sel] / div) {
208 val = (idx << 4) | sel;
209 goto find_rate;
210 }
211 }
212 }
213 dev_err(dev, "can't find convert src clk\n");
214 return -EINVAL;
215
216find_rate:
217 shift = (id % 4) * 8;
218 mask = 0xFF << shift;
219 val = val << shift;
220
221 dev_dbg(dev, "adg convert src clk = %02x\n", val);
222
223 switch (id / 4) {
224 case 0:
225 rsnd_mod_bset(mod, AUDIO_CLK_SEL3, mask, val);
226 break;
227 case 1:
228 rsnd_mod_bset(mod, AUDIO_CLK_SEL4, mask, val);
229 break;
230 case 2:
231 rsnd_mod_bset(mod, AUDIO_CLK_SEL5, mask, val);
232 break;
233 }
234
235 /*
236 * Gen1 doesn't need dst_rate settings,
237 * since it uses SSI WS pin.
238 * see also rsnd_src_set_route_if_gen1()
239 */
240
241 return 0;
242}
243
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800244static void rsnd_adg_set_ssi_clk(struct rsnd_mod *mod, u32 val)
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700245{
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800246 int id = rsnd_mod_id(mod);
247 int shift = (id % 4) * 8;
248 u32 mask = 0xFF << shift;
249
250 val = val << shift;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700251
252 /*
253 * SSI 8 is not connected to ADG.
254 * it works with SSI 7
255 */
256 if (id == 8)
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800257 return;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700258
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800259 switch (id / 4) {
260 case 0:
261 rsnd_mod_bset(mod, AUDIO_CLK_SEL0, mask, val);
262 break;
263 case 1:
264 rsnd_mod_bset(mod, AUDIO_CLK_SEL1, mask, val);
265 break;
266 case 2:
267 rsnd_mod_bset(mod, AUDIO_CLK_SEL2, mask, val);
268 break;
269 }
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700270}
271
272int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod)
273{
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700274 /*
275 * "mod" = "ssi" here.
276 * we can get "ssi id" from mod
277 */
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800278 rsnd_adg_set_ssi_clk(mod, 0);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700279
280 return 0;
281}
282
283int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate)
284{
285 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
286 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
287 struct device *dev = rsnd_priv_to_dev(priv);
288 struct clk *clk;
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800289 int i;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700290 u32 data;
291 int sel_table[] = {
292 [CLKA] = 0x1,
293 [CLKB] = 0x2,
294 [CLKC] = 0x3,
295 [CLKI] = 0x0,
296 };
297
298 dev_dbg(dev, "request clock = %d\n", rate);
299
300 /*
301 * find suitable clock from
302 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
303 */
304 data = 0;
305 for_each_rsnd_clk(clk, adg, i) {
306 if (rate == clk_get_rate(clk)) {
307 data = sel_table[i];
308 goto found_clock;
309 }
310 }
311
312 /*
313 * find 1/6 clock from BRGA/BRGB
314 */
Kuninori Morimoto7808aa32013-12-19 19:27:19 -0800315 if (rate == adg->rbga_rate_for_441khz_div_6) {
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700316 data = 0x10;
317 goto found_clock;
318 }
319
Kuninori Morimoto7808aa32013-12-19 19:27:19 -0800320 if (rate == adg->rbgb_rate_for_48khz_div_6) {
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700321 data = 0x20;
322 goto found_clock;
323 }
324
325 return -EIO;
326
327found_clock:
328
Kuninori Morimotoefeb9702013-09-23 23:12:17 -0700329 /* see rsnd_adg_ssi_clk_init() */
330 rsnd_mod_bset(mod, SSICKR, 0x00FF0000, adg->ckr);
331 rsnd_mod_write(mod, BRRA, 0x00000002); /* 1/6 */
332 rsnd_mod_write(mod, BRRB, 0x00000002); /* 1/6 */
333
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700334 /*
335 * This "mod" = "ssi" here.
336 * we can get "ssi id" from mod
337 */
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800338 rsnd_adg_set_ssi_clk(mod, data);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700339
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800340 dev_dbg(dev, "ADG: ssi%d selects clk%d = %d",
341 rsnd_mod_id(mod), i, rate);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700342
343 return 0;
344}
345
346static void rsnd_adg_ssi_clk_init(struct rsnd_priv *priv, struct rsnd_adg *adg)
347{
348 struct clk *clk;
349 unsigned long rate;
350 u32 ckr;
351 int i;
352 int brg_table[] = {
353 [CLKA] = 0x0,
354 [CLKB] = 0x1,
355 [CLKC] = 0x4,
356 [CLKI] = 0x2,
357 };
358
359 /*
360 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
361 * have 44.1kHz or 48kHz base clocks for now.
362 *
363 * SSI itself can divide parent clock by 1/1 - 1/16
364 * So, BRGA outputs 44.1kHz base parent clock 1/32,
365 * and, BRGB outputs 48.0kHz base parent clock 1/32 here.
366 * see
367 * rsnd_adg_ssi_clk_try_start()
368 */
369 ckr = 0;
Kuninori Morimoto7808aa32013-12-19 19:27:19 -0800370 adg->rbga_rate_for_441khz_div_6 = 0;
371 adg->rbgb_rate_for_48khz_div_6 = 0;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700372 for_each_rsnd_clk(clk, adg, i) {
373 rate = clk_get_rate(clk);
374
375 if (0 == rate) /* not used */
376 continue;
377
378 /* RBGA */
Kuninori Morimoto7808aa32013-12-19 19:27:19 -0800379 if (!adg->rbga_rate_for_441khz_div_6 && (0 == rate % 44100)) {
380 adg->rbga_rate_for_441khz_div_6 = rate / 6;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700381 ckr |= brg_table[i] << 20;
382 }
383
384 /* RBGB */
Kuninori Morimoto7808aa32013-12-19 19:27:19 -0800385 if (!adg->rbgb_rate_for_48khz_div_6 && (0 == rate % 48000)) {
386 adg->rbgb_rate_for_48khz_div_6 = rate / 6;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700387 ckr |= brg_table[i] << 16;
388 }
389 }
390
Kuninori Morimotoefeb9702013-09-23 23:12:17 -0700391 adg->ckr = ckr;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700392}
393
394int rsnd_adg_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700395 const struct rsnd_of_data *of_data,
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700396 struct rsnd_priv *priv)
397{
398 struct rsnd_adg *adg;
399 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto5e392ea2014-05-08 01:59:00 -0700400 struct clk *clk;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700401 int i;
402
403 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
404 if (!adg) {
405 dev_err(dev, "ADG allocate failed\n");
406 return -ENOMEM;
407 }
408
Kuninori Morimoto468be932014-03-02 23:43:11 -0800409 adg->clk[CLKA] = devm_clk_get(dev, "clk_a");
410 adg->clk[CLKB] = devm_clk_get(dev, "clk_b");
411 adg->clk[CLKC] = devm_clk_get(dev, "clk_c");
412 adg->clk[CLKI] = devm_clk_get(dev, "clk_i");
Kuninori Morimoto8691d072014-02-07 00:53:06 -0800413
Kuninori Morimoto5e392ea2014-05-08 01:59:00 -0700414 for_each_rsnd_clk(clk, adg, i)
415 dev_dbg(dev, "clk %d : %p\n", i, clk);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700416
417 rsnd_adg_ssi_clk_init(priv, adg);
418
419 priv->adg = adg;
420
421 dev_dbg(dev, "adg probed\n");
422
423 return 0;
424}