blob: 1946ce8baf2e92fbb97bbdc3a14c2af349fe0080 [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 */
Kuninori Morimoto2a46db42015-09-10 07:04:45 +000010#include <linux/clk-provider.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
Kuninori Morimoto2a46db42015-09-10 07:04:45 +000019#define CLKOUT 0
20#define CLKOUT1 1
21#define CLKOUT2 2
22#define CLKOUT3 3
23#define CLKOUTMAX 4
24
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +000025#define BRRx_MASK(x) (0x3FF & x)
26
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +000027static struct rsnd_mod_ops adg_ops = {
28 .name = "adg",
29};
30
Kuninori Morimotodfc94032013-07-21 21:36:46 -070031struct rsnd_adg {
32 struct clk *clk[CLKMAX];
Kuninori Morimoto2a46db42015-09-10 07:04:45 +000033 struct clk *clkout[CLKOUTMAX];
34 struct clk_onecell_data onecell;
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +000035 struct rsnd_mod mod;
Kuninori Morimotodfc94032013-07-21 21:36:46 -070036
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +000037 int rbga_rate_for_441khz; /* RBGA */
38 int rbgb_rate_for_48khz; /* RBGB */
Kuninori Morimotodfc94032013-07-21 21:36:46 -070039};
40
41#define for_each_rsnd_clk(pos, adg, i) \
Kuninori Morimoto00463c112014-02-11 17:15:51 -080042 for (i = 0; \
43 (i < CLKMAX) && \
44 ((pos) = adg->clk[i]); \
45 i++)
Kuninori Morimoto2a46db42015-09-10 07:04:45 +000046#define for_each_rsnd_clkout(pos, adg, i) \
47 for (i = 0; \
48 (i < CLKOUTMAX) && \
49 ((pos) = adg->clkout[i]); \
50 i++)
Kuninori Morimotodfc94032013-07-21 21:36:46 -070051#define rsnd_priv_to_adg(priv) ((struct rsnd_adg *)(priv)->adg)
52
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +000053static u32 rsnd_adg_calculate_rbgx(unsigned long div)
54{
55 int i, ratio;
56
57 if (!div)
58 return 0;
59
60 for (i = 3; i >= 0; i--) {
61 ratio = 2 << (i * 2);
62 if (0 == (div % ratio))
63 return (u32)((i << 8) | ((div / ratio) - 1));
64 }
65
66 return ~0;
67}
Kuninori Morimoto629509c2014-01-23 18:42:00 -080068
Kuninori Morimoto8467ded2014-03-02 23:43:33 -080069static u32 rsnd_adg_ssi_ws_timing_gen2(struct rsnd_dai_stream *io)
Kuninori Morimoto629509c2014-01-23 18:42:00 -080070{
Kuninori Morimoto8467ded2014-03-02 23:43:33 -080071 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -080072 int id = rsnd_mod_id(mod);
73 int ws = id;
74
Kuninori Morimotob415b4d2015-10-22 03:15:46 +000075 if (rsnd_ssi_is_pin_sharing(io)) {
Kuninori Morimoto629509c2014-01-23 18:42:00 -080076 switch (id) {
77 case 1:
78 case 2:
79 ws = 0;
80 break;
81 case 4:
82 ws = 3;
83 break;
84 case 8:
85 ws = 7;
86 break;
87 }
88 }
89
90 return (0x6 + ws) << 8;
91}
92
Kuninori Morimotof708d942015-01-15 08:07:19 +000093int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *mod,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070094 struct rsnd_dai_stream *io)
95{
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +000096 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
97 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
98 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -070099 int id = rsnd_mod_id(mod);
100 int shift = (id % 2) ? 16 : 0;
101 u32 mask, val;
102
103 val = rsnd_adg_ssi_ws_timing_gen2(io);
104
105 val = val << shift;
106 mask = 0xffff << shift;
107
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000108 rsnd_mod_bset(adg_mod, CMDOUT_TIMSEL, mask, val);
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700109
110 return 0;
111}
112
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000113static int rsnd_adg_set_src_timsel_gen2(struct rsnd_mod *src_mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800114 struct rsnd_dai_stream *io,
115 u32 timsel)
116{
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000117 struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000118 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
119 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimoto985a4f62015-01-15 08:06:49 +0000120 int is_play = rsnd_io_is_play(io);
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000121 int id = rsnd_mod_id(src_mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800122 int shift = (id % 2) ? 16 : 0;
123 u32 mask, ws;
124 u32 in, out;
125
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000126 rsnd_mod_confirm_src(src_mod);
127
Kuninori Morimoto8467ded2014-03-02 23:43:33 -0800128 ws = rsnd_adg_ssi_ws_timing_gen2(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800129
130 in = (is_play) ? timsel : ws;
131 out = (is_play) ? ws : timsel;
132
133 in = in << shift;
134 out = out << shift;
135 mask = 0xffff << shift;
136
137 switch (id / 2) {
138 case 0:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000139 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL0, mask, in);
140 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL0, mask, out);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800141 break;
142 case 1:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000143 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL1, mask, in);
144 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL1, mask, out);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800145 break;
146 case 2:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000147 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL2, mask, in);
148 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL2, mask, out);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800149 break;
150 case 3:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000151 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL3, mask, in);
152 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL3, mask, out);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800153 break;
154 case 4:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000155 rsnd_mod_bset(adg_mod, SRCIN_TIMSEL4, mask, in);
156 rsnd_mod_bset(adg_mod, SRCOUT_TIMSEL4, mask, out);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800157 break;
158 }
159
160 return 0;
161}
162
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000163int rsnd_adg_set_convert_clk_gen2(struct rsnd_mod *src_mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800164 struct rsnd_dai_stream *io,
165 unsigned int src_rate,
166 unsigned int dst_rate)
167{
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000168 struct rsnd_priv *priv = rsnd_mod_to_priv(src_mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800169 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000170 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800171 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800172 int idx, sel, div, step, ret;
173 u32 val, en;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800174 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 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000179 adg->rbga_rate_for_441khz, /* 0011: RBGA */
180 adg->rbgb_rate_for_48khz, /* 0100: RBGB */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800181 };
182
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000183 rsnd_mod_confirm_src(src_mod);
184
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800185 min = ~0;
186 val = 0;
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800187 en = 0;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800188 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
189 idx = 0;
190 step = 2;
191
192 if (!sel_rate[sel])
193 continue;
194
195 for (div = 2; div <= 98304; div += step) {
196 diff = abs(src_rate - sel_rate[sel] / div);
197 if (min > diff) {
198 val = (sel << 8) | idx;
199 min = diff;
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800200 en = 1 << (sel + 1); /* fixme */
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800201 }
202
203 /*
204 * step of 0_0000 / 0_0001 / 0_1101
205 * are out of order
206 */
207 if ((idx > 2) && (idx % 2))
208 step *= 2;
209 if (idx == 0x1c) {
210 div += step;
211 step *= 2;
212 }
213 idx++;
214 }
215 }
216
217 if (min == ~0) {
218 dev_err(dev, "no Input clock\n");
219 return -EIO;
220 }
221
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000222 ret = rsnd_adg_set_src_timsel_gen2(src_mod, io, val);
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800223 if (ret < 0) {
224 dev_err(dev, "timsel error\n");
225 return ret;
226 }
227
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000228 rsnd_mod_bset(adg_mod, DIV_EN, en, en);
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800229
Kuninori Morimotod2c4b802015-03-19 04:14:45 +0000230 dev_dbg(dev, "convert rate %d <-> %d\n", src_rate, dst_rate);
231
Kuninori Morimotoee2c8282014-02-11 21:04:12 -0800232 return 0;
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800233}
234
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000235int rsnd_adg_set_convert_timing_gen2(struct rsnd_mod *src_mod,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800236 struct rsnd_dai_stream *io)
237{
Kuninori Morimoto8467ded2014-03-02 23:43:33 -0800238 u32 val = rsnd_adg_ssi_ws_timing_gen2(io);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800239
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000240 rsnd_mod_confirm_src(src_mod);
241
242 return rsnd_adg_set_src_timsel_gen2(src_mod, io, val);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800243}
244
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800245int rsnd_adg_set_convert_clk_gen1(struct rsnd_priv *priv,
246 struct rsnd_mod *mod,
247 unsigned int src_rate,
248 unsigned int dst_rate)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800249{
250 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000251 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800252 struct device *dev = rsnd_priv_to_dev(priv);
253 int idx, sel, div, shift;
254 u32 mask, val;
255 int id = rsnd_mod_id(mod);
256 unsigned int sel_rate [] = {
257 clk_get_rate(adg->clk[CLKA]), /* 000: CLKA */
258 clk_get_rate(adg->clk[CLKB]), /* 001: CLKB */
259 clk_get_rate(adg->clk[CLKC]), /* 010: CLKC */
260 0, /* 011: MLBCLK (not used) */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000261 adg->rbga_rate_for_441khz, /* 100: RBGA */
262 adg->rbgb_rate_for_48khz, /* 101: RBGB */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800263 };
264
265 /* find div (= 1/128, 1/256, 1/512, 1/1024, 1/2048 */
266 for (sel = 0; sel < ARRAY_SIZE(sel_rate); sel++) {
267 for (div = 128, idx = 0;
268 div <= 2048;
269 div *= 2, idx++) {
270 if (src_rate == sel_rate[sel] / div) {
271 val = (idx << 4) | sel;
272 goto find_rate;
273 }
274 }
275 }
276 dev_err(dev, "can't find convert src clk\n");
277 return -EINVAL;
278
279find_rate:
280 shift = (id % 4) * 8;
281 mask = 0xFF << shift;
282 val = val << shift;
283
284 dev_dbg(dev, "adg convert src clk = %02x\n", val);
285
286 switch (id / 4) {
287 case 0:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000288 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL3, mask, val);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800289 break;
290 case 1:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000291 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL4, mask, val);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800292 break;
293 case 2:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000294 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL5, mask, val);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800295 break;
296 }
297
298 /*
299 * Gen1 doesn't need dst_rate settings,
300 * since it uses SSI WS pin.
301 * see also rsnd_src_set_route_if_gen1()
302 */
303
304 return 0;
305}
306
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000307static void rsnd_adg_set_ssi_clk(struct rsnd_mod *ssi_mod, u32 val)
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700308{
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000309 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000310 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
311 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000312 int id = rsnd_mod_id(ssi_mod);
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800313 int shift = (id % 4) * 8;
314 u32 mask = 0xFF << shift;
315
Kuninori Morimotof1df1222015-09-10 07:03:08 +0000316 rsnd_mod_confirm_ssi(ssi_mod);
317
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800318 val = val << shift;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700319
320 /*
321 * SSI 8 is not connected to ADG.
322 * it works with SSI 7
323 */
324 if (id == 8)
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800325 return;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700326
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800327 switch (id / 4) {
328 case 0:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000329 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL0, mask, val);
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800330 break;
331 case 1:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000332 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL1, mask, val);
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800333 break;
334 case 2:
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000335 rsnd_mod_bset(adg_mod, AUDIO_CLK_SEL2, mask, val);
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800336 break;
337 }
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700338}
339
340int rsnd_adg_ssi_clk_stop(struct rsnd_mod *mod)
341{
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700342 /*
343 * "mod" = "ssi" here.
344 * we can get "ssi id" from mod
345 */
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800346 rsnd_adg_set_ssi_clk(mod, 0);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700347
348 return 0;
349}
350
351int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *mod, unsigned int rate)
352{
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
354 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
355 struct device *dev = rsnd_priv_to_dev(priv);
356 struct clk *clk;
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800357 int i;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700358 u32 data;
359 int sel_table[] = {
360 [CLKA] = 0x1,
361 [CLKB] = 0x2,
362 [CLKC] = 0x3,
363 [CLKI] = 0x0,
364 };
365
366 dev_dbg(dev, "request clock = %d\n", rate);
367
368 /*
369 * find suitable clock from
370 * AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC/AUDIO_CLKI.
371 */
372 data = 0;
373 for_each_rsnd_clk(clk, adg, i) {
374 if (rate == clk_get_rate(clk)) {
375 data = sel_table[i];
376 goto found_clock;
377 }
378 }
379
380 /*
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000381 * find divided clock from BRGA/BRGB
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700382 */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000383 if (rate == adg->rbga_rate_for_441khz) {
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700384 data = 0x10;
385 goto found_clock;
386 }
387
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000388 if (rate == adg->rbgb_rate_for_48khz) {
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700389 data = 0x20;
390 goto found_clock;
391 }
392
393 return -EIO;
394
395found_clock:
396
397 /*
398 * This "mod" = "ssi" here.
399 * we can get "ssi id" from mod
400 */
Kuninori Morimotoe3378532013-12-19 19:26:31 -0800401 rsnd_adg_set_ssi_clk(mod, data);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700402
Kuninori Morimoto3af6c3a2015-09-10 07:04:06 +0000403 dev_dbg(dev, "ADG: %s[%d] selects 0x%x for %d\n",
404 rsnd_mod_name(mod), rsnd_mod_id(mod),
405 data, rate);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700406
407 return 0;
408}
409
Kuninori Morimoto248e88c2015-09-10 07:04:24 +0000410static void rsnd_adg_get_clkin(struct rsnd_priv *priv,
411 struct rsnd_adg *adg)
412{
413 struct device *dev = rsnd_priv_to_dev(priv);
414 struct clk *clk;
415 static const char * const clk_name[] = {
416 [CLKA] = "clk_a",
417 [CLKB] = "clk_b",
418 [CLKC] = "clk_c",
419 [CLKI] = "clk_i",
420 };
Kuninori Morimoto68a55022015-11-05 08:51:15 +0000421 int i, ret;
Kuninori Morimoto248e88c2015-09-10 07:04:24 +0000422
423 for (i = 0; i < CLKMAX; i++) {
424 clk = devm_clk_get(dev, clk_name[i]);
425 adg->clk[i] = IS_ERR(clk) ? NULL : clk;
426 }
427
Kuninori Morimoto68a55022015-11-05 08:51:15 +0000428 for_each_rsnd_clk(clk, adg, i) {
429 ret = clk_prepare_enable(clk);
430 if (ret < 0)
431 dev_warn(dev, "can't use clk %d\n", i);
432
Kuninori Morimoto248e88c2015-09-10 07:04:24 +0000433 dev_dbg(dev, "clk %d : %p : %ld\n", i, clk, clk_get_rate(clk));
Kuninori Morimoto68a55022015-11-05 08:51:15 +0000434 }
Kuninori Morimoto248e88c2015-09-10 07:04:24 +0000435}
436
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000437static void rsnd_adg_get_clkout(struct rsnd_priv *priv,
438 struct rsnd_adg *adg)
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700439{
440 struct clk *clk;
Kuninori Morimoto5c6901d2015-09-10 07:03:25 +0000441 struct rsnd_mod *adg_mod = rsnd_mod_get(adg);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000442 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000443 struct device_node *np = dev->of_node;
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000444 u32 ckr, rbgx, rbga, rbgb;
Geert Uytterhoeven8b274182015-10-28 16:03:48 +0100445 u32 rate, req_rate = 0, div;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000446 uint32_t count = 0;
447 unsigned long req_48kHz_rate, req_441kHz_rate;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700448 int i;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000449 const char *parent_clk_name = NULL;
450 static const char * const clkout_name[] = {
451 [CLKOUT] = "audio_clkout",
452 [CLKOUT1] = "audio_clkout1",
453 [CLKOUT2] = "audio_clkout2",
454 [CLKOUT3] = "audio_clkout3",
455 };
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700456 int brg_table[] = {
457 [CLKA] = 0x0,
458 [CLKB] = 0x1,
459 [CLKC] = 0x4,
460 [CLKI] = 0x2,
461 };
462
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000463 of_property_read_u32(np, "#clock-cells", &count);
464
465 /*
466 * ADG supports BRRA/BRRB output only
467 * this means all clkout0/1/2/3 will be same rate
468 */
469 of_property_read_u32(np, "clock-frequency", &req_rate);
470 req_48kHz_rate = 0;
471 req_441kHz_rate = 0;
472 if (0 == (req_rate % 44100))
473 req_441kHz_rate = req_rate;
474 if (0 == (req_rate % 48000))
475 req_48kHz_rate = req_rate;
476
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700477 /*
478 * This driver is assuming that AUDIO_CLKA/AUDIO_CLKB/AUDIO_CLKC
479 * have 44.1kHz or 48kHz base clocks for now.
480 *
481 * SSI itself can divide parent clock by 1/1 - 1/16
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700482 * see
483 * rsnd_adg_ssi_clk_try_start()
Kuninori Morimoto5c6901d2015-09-10 07:03:25 +0000484 * rsnd_ssi_master_clk_start()
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700485 */
486 ckr = 0;
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000487 rbga = 2; /* default 1/6 */
488 rbgb = 2; /* default 1/6 */
489 adg->rbga_rate_for_441khz = 0;
490 adg->rbgb_rate_for_48khz = 0;
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700491 for_each_rsnd_clk(clk, adg, i) {
492 rate = clk_get_rate(clk);
493
494 if (0 == rate) /* not used */
495 continue;
496
497 /* RBGA */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000498 if (!adg->rbga_rate_for_441khz && (0 == rate % 44100)) {
499 div = 6;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000500 if (req_441kHz_rate)
501 div = rate / req_441kHz_rate;
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000502 rbgx = rsnd_adg_calculate_rbgx(div);
503 if (BRRx_MASK(rbgx) == rbgx) {
504 rbga = rbgx;
505 adg->rbga_rate_for_441khz = rate / div;
506 ckr |= brg_table[i] << 20;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000507 if (req_441kHz_rate)
508 parent_clk_name = __clk_get_name(clk);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000509 }
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700510 }
511
512 /* RBGB */
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000513 if (!adg->rbgb_rate_for_48khz && (0 == rate % 48000)) {
514 div = 6;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000515 if (req_48kHz_rate)
516 div = rate / req_48kHz_rate;
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000517 rbgx = rsnd_adg_calculate_rbgx(div);
518 if (BRRx_MASK(rbgx) == rbgx) {
519 rbgb = rbgx;
520 adg->rbgb_rate_for_48khz = rate / div;
521 ckr |= brg_table[i] << 16;
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000522 if (req_48kHz_rate) {
523 parent_clk_name = __clk_get_name(clk);
524 ckr |= 0x80000000;
525 }
526 }
527 }
528 }
529
530 /*
531 * ADG supports BRRA/BRRB output only.
532 * this means all clkout0/1/2/3 will be * same rate
533 */
534
535 /*
536 * for clkout
537 */
538 if (!count) {
Kuninori Morimoto462c30b2015-09-15 02:44:37 +0000539 clk = clk_register_fixed_rate(dev, clkout_name[CLKOUT],
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000540 parent_clk_name,
541 (parent_clk_name) ?
542 0 : CLK_IS_ROOT, req_rate);
543 if (!IS_ERR(clk)) {
544 adg->clkout[CLKOUT] = clk;
545 of_clk_add_provider(np, of_clk_src_simple_get, clk);
546 }
547 }
548 /*
549 * for clkout0/1/2/3
550 */
551 else {
552 for (i = 0; i < CLKOUTMAX; i++) {
553 clk = clk_register_fixed_rate(dev, clkout_name[i],
554 parent_clk_name,
555 (parent_clk_name) ?
556 0 : CLK_IS_ROOT,
557 req_rate);
558 if (!IS_ERR(clk)) {
559 adg->onecell.clks = adg->clkout;
560 adg->onecell.clk_num = CLKOUTMAX;
561
562 adg->clkout[i] = clk;
563
564 of_clk_add_provider(np, of_clk_src_onecell_get,
565 &adg->onecell);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000566 }
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700567 }
568 }
569
Kuninori Morimoto5c6901d2015-09-10 07:03:25 +0000570 rsnd_mod_bset(adg_mod, SSICKR, 0x00FF0000, ckr);
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000571 rsnd_mod_write(adg_mod, BRRA, rbga);
572 rsnd_mod_write(adg_mod, BRRB, rbgb);
573
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000574 for_each_rsnd_clkout(clk, adg, i)
575 dev_dbg(dev, "clkout %d : %p : %ld\n", i, clk, clk_get_rate(clk));
Kuninori Morimotoeae6fff2015-09-10 07:03:48 +0000576 dev_dbg(dev, "SSICKR = 0x%08x, BRRA/BRRB = 0x%x/0x%x\n",
577 ckr, rbga, rbgb);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700578}
579
580int rsnd_adg_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700581 const struct rsnd_of_data *of_data,
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700582 struct rsnd_priv *priv)
583{
584 struct rsnd_adg *adg;
585 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700586
587 adg = devm_kzalloc(dev, sizeof(*adg), GFP_KERNEL);
588 if (!adg) {
589 dev_err(dev, "ADG allocate failed\n");
590 return -ENOMEM;
591 }
592
Kuninori Morimoto1665a9e2015-09-10 07:02:39 +0000593 /*
594 * ADG is special module.
595 * Use ADG mod without rsnd_mod_init() to make debug easy
596 * for rsnd_write/rsnd_read
597 */
598 adg->mod.ops = &adg_ops;
599 adg->mod.priv = priv;
600
Kuninori Morimoto248e88c2015-09-10 07:04:24 +0000601 rsnd_adg_get_clkin(priv, adg);
Kuninori Morimoto2a46db42015-09-10 07:04:45 +0000602 rsnd_adg_get_clkout(priv, adg);
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700603
604 priv->adg = adg;
605
Kuninori Morimotodfc94032013-07-21 21:36:46 -0700606 return 0;
607}
Kuninori Morimoto68a55022015-11-05 08:51:15 +0000608
609void rsnd_adg_remove(struct platform_device *pdev,
610 struct rsnd_priv *priv)
611{
612 struct rsnd_adg *adg = rsnd_priv_to_adg(priv);
613 struct clk *clk;
614 int i;
615
616 for_each_rsnd_clk(clk, adg, i) {
617 clk_disable_unprepare(clk);
618 }
619}