Commit | Line | Data |
---|---|---|
07539c1d | 1 | /* |
ba9c949f | 2 | * Renesas R-Car SRC support |
07539c1d KM |
3 | * |
4 | * Copyright (C) 2013 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | #include "rsnd.h" | |
12 | ||
8aefda50 KM |
13 | #define SRC_NAME "src" |
14 | ||
cfcefe01 KM |
15 | /* SRCx_STATUS */ |
16 | #define OUF_SRCO ((1 << 12) | (1 << 13)) | |
17 | #define OUF_SRCI ((1 << 9) | (1 << 8)) | |
18 | ||
19 | /* SCU_SYSTEM_STATUS0/1 */ | |
20 | #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id)) | |
21 | ||
ba9c949f | 22 | struct rsnd_src { |
07539c1d | 23 | struct rsnd_mod mod; |
940e9479 | 24 | struct rsnd_mod *dma; |
43cb6954 KM |
25 | struct rsnd_kctrl_cfg_s sen; /* sync convert enable */ |
26 | struct rsnd_kctrl_cfg_s sync; /* sync convert */ | |
3b7843ff | 27 | u32 convert_rate; /* sampling rate convert */ |
cfcefe01 | 28 | int err; |
adf6a681 | 29 | int irq; |
07539c1d KM |
30 | }; |
31 | ||
ba9c949f | 32 | #define RSND_SRC_NAME_SIZE 16 |
374a5281 | 33 | |
adf6a681 | 34 | #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id) |
232c00b6 | 35 | #define rsnd_src_to_dma(src) ((src)->dma) |
da599fd3 | 36 | #define rsnd_src_nr(priv) ((priv)->src_nr) |
43cb6954 | 37 | #define rsnd_enable_sync_convert(src) ((src)->sen.val) |
82e76ed3 | 38 | |
ba9c949f KM |
39 | #define rsnd_mod_to_src(_mod) \ |
40 | container_of((_mod), struct rsnd_src, mod) | |
39cf3c40 | 41 | |
ba9c949f | 42 | #define for_each_rsnd_src(pos, priv, i) \ |
39cf3c40 | 43 | for ((i) = 0; \ |
ba9c949f KM |
44 | ((i) < rsnd_src_nr(priv)) && \ |
45 | ((pos) = (struct rsnd_src *)(priv)->src + i); \ | |
39cf3c40 KM |
46 | i++) |
47 | ||
48 | ||
ef749400 KM |
49 | /* |
50 | * image of SRC (Sampling Rate Converter) | |
51 | * | |
52 | * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ | |
53 | * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | | |
54 | * 44.1kHz <-> +-----+ +-----+ +-------+ | |
55 | * ... | |
56 | * | |
57 | */ | |
374a5281 | 58 | |
c926b746 | 59 | /* |
ba9c949f | 60 | * src.c is caring... |
c926b746 KM |
61 | * |
62 | * Gen1 | |
63 | * | |
64 | * [mem] -> [SRU] -> [SSI] | |
65 | * |--------| | |
66 | * | |
67 | * Gen2 | |
68 | * | |
ba9c949f | 69 | * [mem] -> [SRC] -> [SSIU] -> [SSI] |
c926b746 KM |
70 | * |-----------------| |
71 | */ | |
72 | ||
98efeeae | 73 | static void rsnd_src_activation(struct rsnd_mod *mod) |
379febfd KM |
74 | { |
75 | rsnd_mod_write(mod, SRC_SWRSR, 0); | |
76 | rsnd_mod_write(mod, SRC_SWRSR, 1); | |
77 | } | |
78 | ||
475a361a KM |
79 | static void rsnd_src_halt(struct rsnd_mod *mod) |
80 | { | |
81 | rsnd_mod_write(mod, SRC_SRCIR, 1); | |
82 | rsnd_mod_write(mod, SRC_SWRSR, 0); | |
83 | } | |
84 | ||
9b99e9a7 KM |
85 | static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io, |
86 | struct rsnd_mod *mod) | |
72adc61f KM |
87 | { |
88 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
72adc61f KM |
89 | int is_play = rsnd_io_is_play(io); |
90 | ||
91 | return rsnd_dma_request_channel(rsnd_src_of_node(priv), | |
92 | mod, | |
93 | is_play ? "rx" : "tx"); | |
94 | } | |
95 | ||
88c61cff KM |
96 | static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io, |
97 | struct rsnd_src *src) | |
43cb6954 | 98 | { |
43cb6954 KM |
99 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
100 | u32 convert_rate; | |
101 | ||
102 | if (!runtime) | |
103 | return 0; | |
104 | ||
105 | if (!rsnd_enable_sync_convert(src)) | |
106 | return src->convert_rate; | |
107 | ||
108 | convert_rate = src->sync.val; | |
109 | ||
110 | if (!convert_rate) | |
111 | convert_rate = src->convert_rate; | |
112 | ||
113 | if (!convert_rate) | |
114 | convert_rate = runtime->rate; | |
115 | ||
116 | return convert_rate; | |
117 | } | |
118 | ||
ba9c949f | 119 | unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv, |
374e5426 | 120 | struct rsnd_dai_stream *io, |
1b7b08ef KM |
121 | struct snd_pcm_runtime *runtime) |
122 | { | |
b1eac430 | 123 | struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io); |
ba9c949f | 124 | struct rsnd_src *src; |
b1eac430 | 125 | unsigned int rate = 0; |
1b7b08ef | 126 | |
b1eac430 KM |
127 | if (src_mod) { |
128 | src = rsnd_mod_to_src(src_mod); | |
129 | ||
130 | /* | |
131 | * return convert rate if SRC is used, | |
132 | * otherwise, return runtime->rate as usual | |
133 | */ | |
88c61cff | 134 | rate = rsnd_src_convert_rate(io, src); |
b1eac430 | 135 | } |
1b7b08ef | 136 | |
1b7b08ef KM |
137 | if (!rate) |
138 | rate = runtime->rate; | |
139 | ||
140 | return rate; | |
141 | } | |
142 | ||
3b7843ff | 143 | static int rsnd_src_hw_params(struct rsnd_mod *mod, |
2c0fac19 | 144 | struct rsnd_dai_stream *io, |
3b7843ff KM |
145 | struct snd_pcm_substream *substream, |
146 | struct snd_pcm_hw_params *fe_params) | |
147 | { | |
148 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
149 | struct snd_soc_pcm_runtime *fe = substream->private_data; | |
150 | ||
3b7843ff KM |
151 | /* |
152 | * SRC assumes that it is used under DPCM if user want to use | |
153 | * sampling rate convert. Then, SRC should be FE. | |
154 | * And then, this function will be called *after* BE settings. | |
155 | * this means, each BE already has fixuped hw_params. | |
156 | * see | |
157 | * dpcm_fe_dai_hw_params() | |
158 | * dpcm_be_dai_hw_params() | |
159 | */ | |
160 | if (fe->dai_link->dynamic) { | |
161 | int stream = substream->stream; | |
162 | struct snd_soc_dpcm *dpcm; | |
163 | struct snd_pcm_hw_params *be_params; | |
164 | ||
165 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { | |
166 | be_params = &dpcm->hw_params; | |
167 | ||
168 | if (params_rate(fe_params) != params_rate(be_params)) | |
169 | src->convert_rate = params_rate(be_params); | |
170 | } | |
171 | } | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
75916f65 KM |
176 | static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io, |
177 | struct rsnd_mod *mod) | |
1b7b08ef | 178 | { |
75916f65 KM |
179 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
180 | struct device *dev = rsnd_priv_to_dev(priv); | |
181 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
ba9c949f | 182 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
75916f65 KM |
183 | u32 convert_rate = rsnd_src_convert_rate(io, src); |
184 | u32 ifscr, fsrate, adinr; | |
185 | u32 cr, route; | |
186 | u32 bsdsr, bsisr; | |
187 | uint ratio; | |
1b7b08ef | 188 | |
75916f65 KM |
189 | if (!runtime) |
190 | return; | |
43cb6954 | 191 | |
75916f65 KM |
192 | /* 6 - 1/6 are very enough ratio for SRC_BSDSR */ |
193 | if (!convert_rate) | |
194 | ratio = 0; | |
195 | else if (convert_rate > runtime->rate) | |
196 | ratio = 100 * convert_rate / runtime->rate; | |
197 | else | |
198 | ratio = 100 * runtime->rate / convert_rate; | |
1b7b08ef | 199 | |
75916f65 KM |
200 | if (ratio > 600) { |
201 | dev_err(dev, "FSO/FSI ratio error\n"); | |
202 | return; | |
203 | } | |
1b7b08ef | 204 | |
75916f65 KM |
205 | /* |
206 | * SRC_ADINR | |
207 | */ | |
c45f7263 KM |
208 | adinr = rsnd_get_adinr_bit(mod, io) | |
209 | rsnd_get_adinr_chan(mod, io); | |
1b7b08ef | 210 | |
75916f65 KM |
211 | /* |
212 | * SRC_IFSCR / SRC_IFSVR | |
213 | */ | |
214 | ifscr = 0; | |
215 | fsrate = 0; | |
216 | if (convert_rate) { | |
217 | ifscr = 1; | |
218 | fsrate = 0x0400000 / convert_rate * runtime->rate; | |
219 | } | |
cfcefe01 | 220 | |
75916f65 KM |
221 | /* |
222 | * SRC_SRCCR / SRC_ROUTE_MODE0 | |
223 | */ | |
224 | cr = 0x00011110; | |
225 | route = 0x0; | |
226 | if (convert_rate) { | |
227 | route = 0x1; | |
3b7843ff | 228 | |
75916f65 KM |
229 | if (rsnd_enable_sync_convert(src)) { |
230 | cr |= 0x1; | |
231 | route |= rsnd_io_is_play(io) ? | |
232 | (0x1 << 24) : (0x1 << 25); | |
233 | } | |
234 | } | |
43cb6954 | 235 | |
75916f65 KM |
236 | /* |
237 | * SRC_BSDSR / SRC_BSISR | |
238 | */ | |
239 | switch (rsnd_mod_id(mod)) { | |
240 | case 5: | |
241 | case 6: | |
242 | case 7: | |
243 | case 8: | |
244 | bsdsr = 0x02400000; /* 6 - 1/6 */ | |
245 | bsisr = 0x00100060; /* 6 - 1/6 */ | |
246 | break; | |
247 | default: | |
248 | bsdsr = 0x01800000; /* 6 - 1/6 */ | |
249 | bsisr = 0x00100060 ;/* 6 - 1/6 */ | |
250 | break; | |
251 | } | |
1b7b08ef | 252 | |
75916f65 KM |
253 | rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */ |
254 | rsnd_mod_write(mod, SRC_ADINR, adinr); | |
255 | rsnd_mod_write(mod, SRC_IFSCR, ifscr); | |
256 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); | |
257 | rsnd_mod_write(mod, SRC_SRCCR, cr); | |
258 | rsnd_mod_write(mod, SRC_BSDSR, bsdsr); | |
259 | rsnd_mod_write(mod, SRC_BSISR, bsisr); | |
260 | rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */ | |
1b7b08ef | 261 | |
75916f65 | 262 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, route); |
bf4e8d7c KM |
263 | rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1); |
264 | rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1); | |
75916f65 | 265 | rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io)); |
1b7b08ef | 266 | |
75916f65 KM |
267 | if (convert_rate) |
268 | rsnd_adg_set_convert_clk_gen2(mod, io, | |
269 | runtime->rate, | |
270 | convert_rate); | |
271 | else | |
272 | rsnd_adg_set_convert_timing_gen2(mod, io); | |
1b7b08ef KM |
273 | } |
274 | ||
75916f65 KM |
275 | #define rsnd_src_irq_enable(mod) rsnd_src_irq_ctrol(mod, 1) |
276 | #define rsnd_src_irq_disable(mod) rsnd_src_irq_ctrol(mod, 0) | |
277 | static void rsnd_src_irq_ctrol(struct rsnd_mod *mod, int enable) | |
cfcefe01 KM |
278 | { |
279 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
280 | u32 sys_int_val, int_val, sys_int_mask; | |
adf6a681 | 281 | int irq = src->irq; |
cfcefe01 KM |
282 | int id = rsnd_mod_id(mod); |
283 | ||
284 | sys_int_val = | |
285 | sys_int_mask = OUF_SRC(id); | |
286 | int_val = 0x3300; | |
287 | ||
288 | /* | |
289 | * IRQ is not supported on non-DT | |
290 | * see | |
75916f65 | 291 | * rsnd_src_probe_() |
cfcefe01 KM |
292 | */ |
293 | if ((irq <= 0) || !enable) { | |
294 | sys_int_val = 0; | |
295 | int_val = 0; | |
296 | } | |
297 | ||
1a1bf58a KM |
298 | /* |
299 | * WORKAROUND | |
300 | * | |
301 | * ignore over flow error when rsnd_enable_sync_convert() | |
302 | */ | |
303 | if (rsnd_enable_sync_convert(src)) | |
304 | sys_int_val = sys_int_val & 0xffff; | |
305 | ||
cfcefe01 KM |
306 | rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val); |
307 | rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val); | |
308 | rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val); | |
309 | } | |
310 | ||
8cc225f7 | 311 | static void rsnd_src_status_clear(struct rsnd_mod *mod) |
cfcefe01 KM |
312 | { |
313 | u32 val = OUF_SRC(rsnd_mod_id(mod)); | |
314 | ||
315 | rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val); | |
316 | rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val); | |
317 | } | |
318 | ||
8cc225f7 | 319 | static bool rsnd_src_record_error(struct rsnd_mod *mod) |
cfcefe01 | 320 | { |
1a1bf58a KM |
321 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
322 | u32 val0, val1; | |
cfcefe01 KM |
323 | bool ret = false; |
324 | ||
1a1bf58a KM |
325 | val0 = val1 = OUF_SRC(rsnd_mod_id(mod)); |
326 | ||
327 | /* | |
328 | * WORKAROUND | |
329 | * | |
330 | * ignore over flow error when rsnd_enable_sync_convert() | |
331 | */ | |
332 | if (rsnd_enable_sync_convert(src)) | |
333 | val0 = val0 & 0xffff; | |
334 | ||
335 | if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) || | |
336 | (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) { | |
cfcefe01 KM |
337 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
338 | ||
339 | src->err++; | |
340 | ret = true; | |
341 | } | |
342 | ||
cfcefe01 KM |
343 | return ret; |
344 | } | |
345 | ||
75916f65 KM |
346 | static int rsnd_src_start(struct rsnd_mod *mod, |
347 | struct rsnd_dai_stream *io, | |
348 | struct rsnd_priv *priv) | |
cfcefe01 | 349 | { |
1a1bf58a KM |
350 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
351 | u32 val; | |
352 | ||
353 | /* | |
354 | * WORKAROUND | |
355 | * | |
356 | * Enable SRC output if you want to use sync convert together with DVC | |
357 | */ | |
358 | val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ? | |
359 | 0x01 : 0x11; | |
cfcefe01 KM |
360 | |
361 | rsnd_mod_write(mod, SRC_CTRL, val); | |
362 | ||
cfcefe01 KM |
363 | return 0; |
364 | } | |
365 | ||
75916f65 KM |
366 | static int rsnd_src_stop(struct rsnd_mod *mod, |
367 | struct rsnd_dai_stream *io, | |
368 | struct rsnd_priv *priv) | |
cfcefe01 | 369 | { |
b761bf27 KM |
370 | /* |
371 | * stop SRC output only | |
75916f65 | 372 | * see rsnd_src_quit |
b761bf27 KM |
373 | */ |
374 | rsnd_mod_write(mod, SRC_CTRL, 0x01); | |
cfcefe01 | 375 | |
75916f65 KM |
376 | return 0; |
377 | } | |
378 | ||
379 | static int rsnd_src_init(struct rsnd_mod *mod, | |
380 | struct rsnd_dai_stream *io, | |
381 | struct rsnd_priv *priv) | |
382 | { | |
383 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
cfcefe01 | 384 | |
75916f65 KM |
385 | rsnd_mod_power_on(mod); |
386 | ||
98efeeae | 387 | rsnd_src_activation(mod); |
75916f65 KM |
388 | |
389 | rsnd_src_set_convert_rate(io, mod); | |
390 | ||
8cc225f7 | 391 | rsnd_src_status_clear(mod); |
75916f65 KM |
392 | |
393 | rsnd_src_irq_enable(mod); | |
394 | ||
395 | src->err = 0; | |
396 | ||
397 | /* reset sync convert_rate */ | |
398 | src->sync.val = 0; | |
399 | ||
400 | return 0; | |
cfcefe01 KM |
401 | } |
402 | ||
75916f65 KM |
403 | static int rsnd_src_quit(struct rsnd_mod *mod, |
404 | struct rsnd_dai_stream *io, | |
405 | struct rsnd_priv *priv) | |
b761bf27 | 406 | { |
75916f65 KM |
407 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
408 | struct device *dev = rsnd_priv_to_dev(priv); | |
409 | ||
410 | rsnd_src_irq_disable(mod); | |
411 | ||
b761bf27 KM |
412 | /* stop both out/in */ |
413 | rsnd_mod_write(mod, SRC_CTRL, 0); | |
414 | ||
475a361a KM |
415 | rsnd_src_halt(mod); |
416 | ||
75916f65 KM |
417 | rsnd_mod_power_off(mod); |
418 | ||
419 | if (src->err) | |
420 | dev_warn(dev, "%s[%d] under/over flow err = %d\n", | |
421 | rsnd_mod_name(mod), rsnd_mod_id(mod), src->err); | |
422 | ||
423 | src->convert_rate = 0; | |
424 | ||
425 | /* reset sync convert_rate */ | |
426 | src->sync.val = 0; | |
427 | ||
428 | return 0; | |
b761bf27 KM |
429 | } |
430 | ||
75916f65 KM |
431 | static void __rsnd_src_interrupt(struct rsnd_mod *mod, |
432 | struct rsnd_dai_stream *io) | |
cfcefe01 | 433 | { |
02299d98 | 434 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
69e32a58 KM |
435 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
436 | struct device *dev = rsnd_priv_to_dev(priv); | |
02299d98 KM |
437 | |
438 | spin_lock(&priv->lock); | |
cfcefe01 | 439 | |
02299d98 | 440 | /* ignore all cases if not working */ |
d5bbe7de | 441 | if (!rsnd_io_is_working(io)) |
75916f65 | 442 | goto rsnd_src_interrupt_out; |
cfcefe01 | 443 | |
8cc225f7 | 444 | if (rsnd_src_record_error(mod)) { |
cfcefe01 | 445 | |
cfcefe01 KM |
446 | dev_dbg(dev, "%s[%d] restart\n", |
447 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
639b231f | 448 | |
75916f65 KM |
449 | rsnd_src_stop(mod, io, priv); |
450 | rsnd_src_start(mod, io, priv); | |
69e32a58 KM |
451 | } |
452 | ||
453 | if (src->err > 1024) { | |
75916f65 | 454 | rsnd_src_irq_disable(mod); |
69e32a58 KM |
455 | |
456 | dev_warn(dev, "no more %s[%d] restart\n", | |
457 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
cfcefe01 | 458 | } |
88c61cff | 459 | |
8cc225f7 | 460 | rsnd_src_status_clear(mod); |
75916f65 | 461 | rsnd_src_interrupt_out: |
8cc225f7 | 462 | |
02299d98 | 463 | spin_unlock(&priv->lock); |
88c61cff KM |
464 | } |
465 | ||
75916f65 | 466 | static irqreturn_t rsnd_src_interrupt(int irq, void *data) |
88c61cff KM |
467 | { |
468 | struct rsnd_mod *mod = data; | |
469 | ||
75916f65 | 470 | rsnd_mod_interrupt(mod, __rsnd_src_interrupt); |
cfcefe01 KM |
471 | |
472 | return IRQ_HANDLED; | |
473 | } | |
474 | ||
75916f65 KM |
475 | static int rsnd_src_probe_(struct rsnd_mod *mod, |
476 | struct rsnd_dai_stream *io, | |
477 | struct rsnd_priv *priv) | |
76c6fb5c | 478 | { |
ba9c949f | 479 | struct rsnd_src *src = rsnd_mod_to_src(mod); |
76c6fb5c | 480 | struct device *dev = rsnd_priv_to_dev(priv); |
adf6a681 | 481 | int irq = src->irq; |
76c6fb5c | 482 | int ret; |
76c6fb5c | 483 | |
cfcefe01 KM |
484 | if (irq > 0) { |
485 | /* | |
486 | * IRQ is not supported on non-DT | |
487 | * see | |
75916f65 | 488 | * rsnd_src_irq_enable() |
cfcefe01 KM |
489 | */ |
490 | ret = devm_request_irq(dev, irq, | |
75916f65 | 491 | rsnd_src_interrupt, |
cfcefe01 KM |
492 | IRQF_SHARED, |
493 | dev_name(dev), mod); | |
494 | if (ret) | |
b543b52a | 495 | return ret; |
cfcefe01 KM |
496 | } |
497 | ||
adf6a681 | 498 | src->dma = rsnd_dma_attach(io, mod, 0); |
232c00b6 KM |
499 | if (IS_ERR(src->dma)) |
500 | return PTR_ERR(src->dma); | |
8aefda50 | 501 | |
76c6fb5c KM |
502 | return ret; |
503 | } | |
504 | ||
75916f65 | 505 | static int rsnd_src_pcm_new(struct rsnd_mod *mod, |
2c0fac19 | 506 | struct rsnd_dai_stream *io, |
43cb6954 KM |
507 | struct snd_soc_pcm_runtime *rtd) |
508 | { | |
43cb6954 KM |
509 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
510 | struct rsnd_src *src = rsnd_mod_to_src(mod); | |
511 | int ret; | |
512 | ||
513 | /* | |
514 | * enable SRC sync convert if possible | |
515 | */ | |
516 | ||
43cb6954 KM |
517 | /* |
518 | * SRC sync convert needs clock master | |
519 | */ | |
520 | if (!rsnd_rdai_is_clk_master(rdai)) | |
521 | return 0; | |
522 | ||
43cb6954 KM |
523 | /* |
524 | * enable sync convert | |
525 | */ | |
b65a7ccc | 526 | ret = rsnd_kctrl_new_s(mod, io, rtd, |
43cb6954 KM |
527 | rsnd_io_is_play(io) ? |
528 | "SRC Out Rate Switch" : | |
529 | "SRC In Rate Switch", | |
75916f65 | 530 | rsnd_src_set_convert_rate, |
43cb6954 KM |
531 | &src->sen, 1); |
532 | if (ret < 0) | |
533 | return ret; | |
534 | ||
b65a7ccc | 535 | ret = rsnd_kctrl_new_s(mod, io, rtd, |
43cb6954 KM |
536 | rsnd_io_is_play(io) ? |
537 | "SRC Out Rate" : | |
538 | "SRC In Rate", | |
75916f65 | 539 | rsnd_src_set_convert_rate, |
43cb6954 KM |
540 | &src->sync, 192000); |
541 | ||
542 | return ret; | |
543 | } | |
544 | ||
75916f65 | 545 | static struct rsnd_mod_ops rsnd_src_ops = { |
8aefda50 | 546 | .name = SRC_NAME, |
72adc61f | 547 | .dma_req = rsnd_src_dma_req, |
75916f65 KM |
548 | .probe = rsnd_src_probe_, |
549 | .init = rsnd_src_init, | |
550 | .quit = rsnd_src_quit, | |
551 | .start = rsnd_src_start, | |
552 | .stop = rsnd_src_stop, | |
3b7843ff | 553 | .hw_params = rsnd_src_hw_params, |
75916f65 | 554 | .pcm_new = rsnd_src_pcm_new, |
629509c5 KM |
555 | }; |
556 | ||
ba9c949f | 557 | struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id) |
07539c1d | 558 | { |
ba9c949f | 559 | if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv))) |
8b14719b | 560 | id = 0; |
07539c1d | 561 | |
adf6a681 | 562 | return rsnd_mod_get(rsnd_src_get(priv, id)); |
90e8e50f KM |
563 | } |
564 | ||
2ea6b074 | 565 | int rsnd_src_probe(struct rsnd_priv *priv) |
07539c1d | 566 | { |
adf6a681 KM |
567 | struct device_node *node; |
568 | struct device_node *np; | |
07539c1d | 569 | struct device *dev = rsnd_priv_to_dev(priv); |
ba9c949f | 570 | struct rsnd_src *src; |
ef749400 | 571 | struct clk *clk; |
ba9c949f | 572 | char name[RSND_SRC_NAME_SIZE]; |
2f78dd7f | 573 | int i, nr, ret; |
07539c1d | 574 | |
e8e7b7bd KM |
575 | /* This driver doesn't support Gen1 at this point */ |
576 | if (rsnd_is_gen1(priv)) | |
577 | return 0; | |
033e7ed8 | 578 | |
adf6a681 KM |
579 | node = rsnd_src_of_node(priv); |
580 | if (!node) | |
581 | return 0; /* not used is not error */ | |
90e8e50f | 582 | |
adf6a681 KM |
583 | nr = of_get_child_count(node); |
584 | if (!nr) { | |
585 | ret = -EINVAL; | |
586 | goto rsnd_src_probe_done; | |
587 | } | |
389933d9 | 588 | |
ba9c949f | 589 | src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL); |
adf6a681 KM |
590 | if (!src) { |
591 | ret = -ENOMEM; | |
592 | goto rsnd_src_probe_done; | |
593 | } | |
07539c1d | 594 | |
ba9c949f KM |
595 | priv->src_nr = nr; |
596 | priv->src = src; | |
07539c1d | 597 | |
adf6a681 KM |
598 | i = 0; |
599 | for_each_child_of_node(node, np) { | |
600 | src = rsnd_src_get(priv, i); | |
601 | ||
8aefda50 KM |
602 | snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d", |
603 | SRC_NAME, i); | |
ef749400 | 604 | |
adf6a681 KM |
605 | src->irq = irq_of_parse_and_map(np, 0); |
606 | if (!src->irq) { | |
607 | ret = -EINVAL; | |
608 | goto rsnd_src_probe_done; | |
609 | } | |
ef749400 | 610 | |
adf6a681 KM |
611 | clk = devm_clk_get(dev, name); |
612 | if (IS_ERR(clk)) { | |
613 | ret = PTR_ERR(clk); | |
614 | goto rsnd_src_probe_done; | |
615 | } | |
07539c1d | 616 | |
e8e7b7bd | 617 | ret = rsnd_mod_init(priv, rsnd_mod_get(src), |
75916f65 | 618 | &rsnd_src_ops, clk, RSND_MOD_SRC, i); |
2f78dd7f | 619 | if (ret) |
adf6a681 KM |
620 | goto rsnd_src_probe_done; |
621 | ||
622 | i++; | |
374a5281 | 623 | } |
07539c1d | 624 | |
adf6a681 KM |
625 | ret = 0; |
626 | ||
627 | rsnd_src_probe_done: | |
628 | of_node_put(node); | |
629 | ||
630 | return ret; | |
07539c1d | 631 | } |
2f78dd7f | 632 | |
2ea6b074 | 633 | void rsnd_src_remove(struct rsnd_priv *priv) |
2f78dd7f KM |
634 | { |
635 | struct rsnd_src *src; | |
636 | int i; | |
637 | ||
638 | for_each_rsnd_src(src, priv, i) { | |
b76e218a | 639 | rsnd_mod_quit(rsnd_mod_get(src)); |
2f78dd7f KM |
640 | } |
641 | } |