src.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Renesas R-Car SRC support
  4. //
  5. // Copyright (C) 2013 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. /*
  8. * you can enable below define if you don't need
  9. * SSI interrupt status debug message when debugging
  10. * see rsnd_dbg_irq_status()
  11. *
  12. * #define RSND_DEBUG_NO_IRQ_STATUS 1
  13. */
  14. #include "rsnd.h"
  15. #define SRC_NAME "src"
  16. /* SCU_SYSTEM_STATUS0/1 */
  17. #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
  18. struct rsnd_src {
  19. struct rsnd_mod mod;
  20. struct rsnd_mod *dma;
  21. struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
  22. struct rsnd_kctrl_cfg_s sync; /* sync convert */
  23. u32 convert_rate; /* sampling rate convert */
  24. int irq;
  25. };
  26. #define RSND_SRC_NAME_SIZE 16
  27. #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
  28. #define rsnd_src_nr(priv) ((priv)->src_nr)
  29. #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
  30. #define rsnd_mod_to_src(_mod) \
  31. container_of((_mod), struct rsnd_src, mod)
  32. #define for_each_rsnd_src(pos, priv, i) \
  33. for ((i) = 0; \
  34. ((i) < rsnd_src_nr(priv)) && \
  35. ((pos) = (struct rsnd_src *)(priv)->src + i); \
  36. i++)
  37. /*
  38. * image of SRC (Sampling Rate Converter)
  39. *
  40. * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
  41. * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
  42. * 44.1kHz <-> +-----+ +-----+ +-------+
  43. * ...
  44. *
  45. */
  46. static void rsnd_src_activation(struct rsnd_mod *mod)
  47. {
  48. rsnd_mod_write(mod, SRC_SWRSR, 0);
  49. rsnd_mod_write(mod, SRC_SWRSR, 1);
  50. }
  51. static void rsnd_src_halt(struct rsnd_mod *mod)
  52. {
  53. rsnd_mod_write(mod, SRC_SRCIR, 1);
  54. rsnd_mod_write(mod, SRC_SWRSR, 0);
  55. }
  56. static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
  57. struct rsnd_mod *mod)
  58. {
  59. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  60. int is_play = rsnd_io_is_play(io);
  61. return rsnd_dma_request_channel(rsnd_src_of_node(priv),
  62. mod,
  63. is_play ? "rx" : "tx");
  64. }
  65. static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
  66. struct rsnd_mod *mod)
  67. {
  68. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  69. struct rsnd_src *src = rsnd_mod_to_src(mod);
  70. u32 convert_rate;
  71. if (!runtime)
  72. return 0;
  73. if (!rsnd_src_sync_is_enabled(mod))
  74. return src->convert_rate;
  75. convert_rate = src->sync.val;
  76. if (!convert_rate)
  77. convert_rate = src->convert_rate;
  78. if (!convert_rate)
  79. convert_rate = runtime->rate;
  80. return convert_rate;
  81. }
  82. unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
  83. struct rsnd_dai_stream *io,
  84. int is_in)
  85. {
  86. struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
  87. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  88. unsigned int rate = 0;
  89. int is_play = rsnd_io_is_play(io);
  90. /*
  91. * Playback
  92. * runtime_rate -> [SRC] -> convert_rate
  93. *
  94. * Capture
  95. * convert_rate -> [SRC] -> runtime_rate
  96. */
  97. if (is_play == is_in)
  98. return runtime->rate;
  99. /*
  100. * return convert rate if SRC is used,
  101. * otherwise, return runtime->rate as usual
  102. */
  103. if (src_mod)
  104. rate = rsnd_src_convert_rate(io, src_mod);
  105. if (!rate)
  106. rate = runtime->rate;
  107. return rate;
  108. }
  109. static int rsnd_src_hw_params(struct rsnd_mod *mod,
  110. struct rsnd_dai_stream *io,
  111. struct snd_pcm_substream *substream,
  112. struct snd_pcm_hw_params *fe_params)
  113. {
  114. struct rsnd_src *src = rsnd_mod_to_src(mod);
  115. struct snd_soc_pcm_runtime *fe = substream->private_data;
  116. /*
  117. * SRC assumes that it is used under DPCM if user want to use
  118. * sampling rate convert. Then, SRC should be FE.
  119. * And then, this function will be called *after* BE settings.
  120. * this means, each BE already has fixuped hw_params.
  121. * see
  122. * dpcm_fe_dai_hw_params()
  123. * dpcm_be_dai_hw_params()
  124. */
  125. src->convert_rate = 0;
  126. if (fe->dai_link->dynamic) {
  127. int stream = substream->stream;
  128. struct snd_soc_dpcm *dpcm;
  129. struct snd_pcm_hw_params *be_params;
  130. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  131. be_params = &dpcm->hw_params;
  132. if (params_rate(fe_params) != params_rate(be_params))
  133. src->convert_rate = params_rate(be_params);
  134. }
  135. }
  136. return 0;
  137. }
  138. static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
  139. struct rsnd_mod *mod)
  140. {
  141. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  142. struct device *dev = rsnd_priv_to_dev(priv);
  143. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  144. int is_play = rsnd_io_is_play(io);
  145. int use_src = 0;
  146. u32 fin, fout;
  147. u32 ifscr, fsrate, adinr;
  148. u32 cr, route;
  149. u32 bsdsr, bsisr;
  150. u32 i_busif, o_busif, tmp;
  151. uint ratio;
  152. if (!runtime)
  153. return;
  154. fin = rsnd_src_get_in_rate(priv, io);
  155. fout = rsnd_src_get_out_rate(priv, io);
  156. /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
  157. if (fin == fout)
  158. ratio = 0;
  159. else if (fin > fout)
  160. ratio = 100 * fin / fout;
  161. else
  162. ratio = 100 * fout / fin;
  163. if (ratio > 600) {
  164. dev_err(dev, "FSO/FSI ratio error\n");
  165. return;
  166. }
  167. use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
  168. /*
  169. * SRC_ADINR
  170. */
  171. adinr = rsnd_get_adinr_bit(mod, io) |
  172. rsnd_runtime_channel_original(io);
  173. /*
  174. * SRC_IFSCR / SRC_IFSVR
  175. */
  176. ifscr = 0;
  177. fsrate = 0;
  178. if (use_src) {
  179. u64 n;
  180. ifscr = 1;
  181. n = (u64)0x0400000 * fin;
  182. do_div(n, fout);
  183. fsrate = n;
  184. }
  185. /*
  186. * SRC_SRCCR / SRC_ROUTE_MODE0
  187. */
  188. cr = 0x00011110;
  189. route = 0x0;
  190. if (use_src) {
  191. route = 0x1;
  192. if (rsnd_src_sync_is_enabled(mod)) {
  193. cr |= 0x1;
  194. route |= rsnd_io_is_play(io) ?
  195. (0x1 << 24) : (0x1 << 25);
  196. }
  197. }
  198. /*
  199. * SRC_BSDSR / SRC_BSISR
  200. */
  201. switch (rsnd_mod_id(mod)) {
  202. case 5:
  203. case 6:
  204. case 7:
  205. case 8:
  206. bsdsr = 0x02400000; /* 6 - 1/6 */
  207. bsisr = 0x00100060; /* 6 - 1/6 */
  208. break;
  209. default:
  210. bsdsr = 0x01800000; /* 6 - 1/6 */
  211. bsisr = 0x00100060 ;/* 6 - 1/6 */
  212. break;
  213. }
  214. /* BUSIF_MODE */
  215. tmp = rsnd_get_busif_shift(io, mod);
  216. i_busif = ( is_play ? tmp : 0) | 1;
  217. o_busif = (!is_play ? tmp : 0) | 1;
  218. rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
  219. rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
  220. rsnd_mod_write(mod, SRC_ADINR, adinr);
  221. rsnd_mod_write(mod, SRC_IFSCR, ifscr);
  222. rsnd_mod_write(mod, SRC_IFSVR, fsrate);
  223. rsnd_mod_write(mod, SRC_SRCCR, cr);
  224. rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
  225. rsnd_mod_write(mod, SRC_BSISR, bsisr);
  226. rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
  227. rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
  228. rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
  229. rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
  230. rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
  231. }
  232. static int rsnd_src_irq(struct rsnd_mod *mod,
  233. struct rsnd_dai_stream *io,
  234. struct rsnd_priv *priv,
  235. int enable)
  236. {
  237. struct rsnd_src *src = rsnd_mod_to_src(mod);
  238. u32 sys_int_val, int_val, sys_int_mask;
  239. int irq = src->irq;
  240. int id = rsnd_mod_id(mod);
  241. sys_int_val =
  242. sys_int_mask = OUF_SRC(id);
  243. int_val = 0x3300;
  244. /*
  245. * IRQ is not supported on non-DT
  246. * see
  247. * rsnd_src_probe_()
  248. */
  249. if ((irq <= 0) || !enable) {
  250. sys_int_val = 0;
  251. int_val = 0;
  252. }
  253. /*
  254. * WORKAROUND
  255. *
  256. * ignore over flow error when rsnd_src_sync_is_enabled()
  257. */
  258. if (rsnd_src_sync_is_enabled(mod))
  259. sys_int_val = sys_int_val & 0xffff;
  260. rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
  261. rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
  262. rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
  263. return 0;
  264. }
  265. static void rsnd_src_status_clear(struct rsnd_mod *mod)
  266. {
  267. u32 val = OUF_SRC(rsnd_mod_id(mod));
  268. rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
  269. rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
  270. }
  271. static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
  272. {
  273. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  274. struct device *dev = rsnd_priv_to_dev(priv);
  275. u32 val0, val1;
  276. u32 status0, status1;
  277. bool ret = false;
  278. val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
  279. /*
  280. * WORKAROUND
  281. *
  282. * ignore over flow error when rsnd_src_sync_is_enabled()
  283. */
  284. if (rsnd_src_sync_is_enabled(mod))
  285. val0 = val0 & 0xffff;
  286. status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
  287. status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
  288. if ((status0 & val0) || (status1 & val1)) {
  289. rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x, 0x%08x\n",
  290. rsnd_mod_name(mod), rsnd_mod_id(mod),
  291. status0, status1);
  292. ret = true;
  293. }
  294. return ret;
  295. }
  296. static int rsnd_src_start(struct rsnd_mod *mod,
  297. struct rsnd_dai_stream *io,
  298. struct rsnd_priv *priv)
  299. {
  300. u32 val;
  301. /*
  302. * WORKAROUND
  303. *
  304. * Enable SRC output if you want to use sync convert together with DVC
  305. */
  306. val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
  307. 0x01 : 0x11;
  308. rsnd_mod_write(mod, SRC_CTRL, val);
  309. return 0;
  310. }
  311. static int rsnd_src_stop(struct rsnd_mod *mod,
  312. struct rsnd_dai_stream *io,
  313. struct rsnd_priv *priv)
  314. {
  315. rsnd_mod_write(mod, SRC_CTRL, 0);
  316. return 0;
  317. }
  318. static int rsnd_src_init(struct rsnd_mod *mod,
  319. struct rsnd_dai_stream *io,
  320. struct rsnd_priv *priv)
  321. {
  322. struct rsnd_src *src = rsnd_mod_to_src(mod);
  323. /* reset sync convert_rate */
  324. src->sync.val = 0;
  325. rsnd_mod_power_on(mod);
  326. rsnd_src_activation(mod);
  327. rsnd_src_set_convert_rate(io, mod);
  328. rsnd_src_status_clear(mod);
  329. return 0;
  330. }
  331. static int rsnd_src_quit(struct rsnd_mod *mod,
  332. struct rsnd_dai_stream *io,
  333. struct rsnd_priv *priv)
  334. {
  335. struct rsnd_src *src = rsnd_mod_to_src(mod);
  336. rsnd_src_halt(mod);
  337. rsnd_mod_power_off(mod);
  338. /* reset sync convert_rate */
  339. src->sync.val = 0;
  340. return 0;
  341. }
  342. static void __rsnd_src_interrupt(struct rsnd_mod *mod,
  343. struct rsnd_dai_stream *io)
  344. {
  345. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  346. bool stop = false;
  347. spin_lock(&priv->lock);
  348. /* ignore all cases if not working */
  349. if (!rsnd_io_is_working(io))
  350. goto rsnd_src_interrupt_out;
  351. if (rsnd_src_error_occurred(mod))
  352. stop = true;
  353. rsnd_src_status_clear(mod);
  354. rsnd_src_interrupt_out:
  355. spin_unlock(&priv->lock);
  356. if (stop)
  357. snd_pcm_stop_xrun(io->substream);
  358. }
  359. static irqreturn_t rsnd_src_interrupt(int irq, void *data)
  360. {
  361. struct rsnd_mod *mod = data;
  362. rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
  363. return IRQ_HANDLED;
  364. }
  365. static int rsnd_src_probe_(struct rsnd_mod *mod,
  366. struct rsnd_dai_stream *io,
  367. struct rsnd_priv *priv)
  368. {
  369. struct rsnd_src *src = rsnd_mod_to_src(mod);
  370. struct device *dev = rsnd_priv_to_dev(priv);
  371. int irq = src->irq;
  372. int ret;
  373. if (irq > 0) {
  374. /*
  375. * IRQ is not supported on non-DT
  376. * see
  377. * rsnd_src_irq()
  378. */
  379. ret = devm_request_irq(dev, irq,
  380. rsnd_src_interrupt,
  381. IRQF_SHARED,
  382. dev_name(dev), mod);
  383. if (ret)
  384. return ret;
  385. }
  386. ret = rsnd_dma_attach(io, mod, &src->dma);
  387. return ret;
  388. }
  389. static int rsnd_src_pcm_new(struct rsnd_mod *mod,
  390. struct rsnd_dai_stream *io,
  391. struct snd_soc_pcm_runtime *rtd)
  392. {
  393. struct rsnd_src *src = rsnd_mod_to_src(mod);
  394. int ret;
  395. /*
  396. * enable SRC sync convert if possible
  397. */
  398. /*
  399. * It can't use SRC Synchronous convert
  400. * when Capture if it uses CMD
  401. */
  402. if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
  403. return 0;
  404. /*
  405. * enable sync convert
  406. */
  407. ret = rsnd_kctrl_new_s(mod, io, rtd,
  408. rsnd_io_is_play(io) ?
  409. "SRC Out Rate Switch" :
  410. "SRC In Rate Switch",
  411. rsnd_kctrl_accept_anytime,
  412. rsnd_src_set_convert_rate,
  413. &src->sen, 1);
  414. if (ret < 0)
  415. return ret;
  416. ret = rsnd_kctrl_new_s(mod, io, rtd,
  417. rsnd_io_is_play(io) ?
  418. "SRC Out Rate" :
  419. "SRC In Rate",
  420. rsnd_kctrl_accept_runtime,
  421. rsnd_src_set_convert_rate,
  422. &src->sync, 192000);
  423. return ret;
  424. }
  425. static struct rsnd_mod_ops rsnd_src_ops = {
  426. .name = SRC_NAME,
  427. .dma_req = rsnd_src_dma_req,
  428. .probe = rsnd_src_probe_,
  429. .init = rsnd_src_init,
  430. .quit = rsnd_src_quit,
  431. .start = rsnd_src_start,
  432. .stop = rsnd_src_stop,
  433. .irq = rsnd_src_irq,
  434. .hw_params = rsnd_src_hw_params,
  435. .pcm_new = rsnd_src_pcm_new,
  436. };
  437. struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
  438. {
  439. if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
  440. id = 0;
  441. return rsnd_mod_get(rsnd_src_get(priv, id));
  442. }
  443. int rsnd_src_probe(struct rsnd_priv *priv)
  444. {
  445. struct device_node *node;
  446. struct device_node *np;
  447. struct device *dev = rsnd_priv_to_dev(priv);
  448. struct rsnd_src *src;
  449. struct clk *clk;
  450. char name[RSND_SRC_NAME_SIZE];
  451. int i, nr, ret;
  452. /* This driver doesn't support Gen1 at this point */
  453. if (rsnd_is_gen1(priv))
  454. return 0;
  455. node = rsnd_src_of_node(priv);
  456. if (!node)
  457. return 0; /* not used is not error */
  458. nr = of_get_child_count(node);
  459. if (!nr) {
  460. ret = -EINVAL;
  461. goto rsnd_src_probe_done;
  462. }
  463. src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
  464. if (!src) {
  465. ret = -ENOMEM;
  466. goto rsnd_src_probe_done;
  467. }
  468. priv->src_nr = nr;
  469. priv->src = src;
  470. i = 0;
  471. for_each_child_of_node(node, np) {
  472. if (!of_device_is_available(np))
  473. goto skip;
  474. src = rsnd_src_get(priv, i);
  475. snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
  476. SRC_NAME, i);
  477. src->irq = irq_of_parse_and_map(np, 0);
  478. if (!src->irq) {
  479. ret = -EINVAL;
  480. of_node_put(np);
  481. goto rsnd_src_probe_done;
  482. }
  483. clk = devm_clk_get(dev, name);
  484. if (IS_ERR(clk)) {
  485. ret = PTR_ERR(clk);
  486. of_node_put(np);
  487. goto rsnd_src_probe_done;
  488. }
  489. ret = rsnd_mod_init(priv, rsnd_mod_get(src),
  490. &rsnd_src_ops, clk, rsnd_mod_get_status,
  491. RSND_MOD_SRC, i);
  492. if (ret) {
  493. of_node_put(np);
  494. goto rsnd_src_probe_done;
  495. }
  496. skip:
  497. i++;
  498. }
  499. ret = 0;
  500. rsnd_src_probe_done:
  501. of_node_put(node);
  502. return ret;
  503. }
  504. void rsnd_src_remove(struct rsnd_priv *priv)
  505. {
  506. struct rsnd_src *src;
  507. int i;
  508. for_each_rsnd_src(src, priv, i) {
  509. rsnd_mod_quit(rsnd_mod_get(src));
  510. }
  511. }