src.c 13 KB

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