src.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. u32 fin, fout;
  159. u32 ifscr, fsrate, adinr;
  160. u32 cr, route;
  161. u32 bsdsr, bsisr;
  162. uint ratio;
  163. if (!runtime)
  164. return;
  165. fin = rsnd_src_get_in_rate(priv, io);
  166. fout = rsnd_src_get_out_rate(priv, io);
  167. /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
  168. if (fin == fout)
  169. ratio = 0;
  170. else if (fin > fout)
  171. ratio = 100 * fin / fout;
  172. else
  173. ratio = 100 * fout / fin;
  174. if (ratio > 600) {
  175. dev_err(dev, "FSO/FSI ratio error\n");
  176. return;
  177. }
  178. /*
  179. * SRC_ADINR
  180. */
  181. adinr = rsnd_get_adinr_bit(mod, io) |
  182. rsnd_runtime_channel_original(io);
  183. /*
  184. * SRC_IFSCR / SRC_IFSVR
  185. */
  186. ifscr = 0;
  187. fsrate = 0;
  188. if (fin != fout) {
  189. u64 n;
  190. ifscr = 1;
  191. n = (u64)0x0400000 * fin;
  192. do_div(n, fout);
  193. fsrate = n;
  194. }
  195. /*
  196. * SRC_SRCCR / SRC_ROUTE_MODE0
  197. */
  198. cr = 0x00011110;
  199. route = 0x0;
  200. if (fin != fout) {
  201. route = 0x1;
  202. if (rsnd_src_sync_is_enabled(mod)) {
  203. cr |= 0x1;
  204. route |= rsnd_io_is_play(io) ?
  205. (0x1 << 24) : (0x1 << 25);
  206. }
  207. }
  208. /*
  209. * SRC_BSDSR / SRC_BSISR
  210. */
  211. switch (rsnd_mod_id(mod)) {
  212. case 5:
  213. case 6:
  214. case 7:
  215. case 8:
  216. bsdsr = 0x02400000; /* 6 - 1/6 */
  217. bsisr = 0x00100060; /* 6 - 1/6 */
  218. break;
  219. default:
  220. bsdsr = 0x01800000; /* 6 - 1/6 */
  221. bsisr = 0x00100060 ;/* 6 - 1/6 */
  222. break;
  223. }
  224. rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
  225. rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
  226. rsnd_mod_write(mod, SRC_ADINR, adinr);
  227. rsnd_mod_write(mod, SRC_IFSCR, ifscr);
  228. rsnd_mod_write(mod, SRC_IFSVR, fsrate);
  229. rsnd_mod_write(mod, SRC_SRCCR, cr);
  230. rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
  231. rsnd_mod_write(mod, SRC_BSISR, bsisr);
  232. rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
  233. rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
  234. rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
  235. rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
  236. rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
  237. }
  238. static int rsnd_src_irq(struct rsnd_mod *mod,
  239. struct rsnd_dai_stream *io,
  240. struct rsnd_priv *priv,
  241. int enable)
  242. {
  243. struct rsnd_src *src = rsnd_mod_to_src(mod);
  244. u32 sys_int_val, int_val, sys_int_mask;
  245. int irq = src->irq;
  246. int id = rsnd_mod_id(mod);
  247. sys_int_val =
  248. sys_int_mask = OUF_SRC(id);
  249. int_val = 0x3300;
  250. /*
  251. * IRQ is not supported on non-DT
  252. * see
  253. * rsnd_src_probe_()
  254. */
  255. if ((irq <= 0) || !enable) {
  256. sys_int_val = 0;
  257. int_val = 0;
  258. }
  259. /*
  260. * WORKAROUND
  261. *
  262. * ignore over flow error when rsnd_src_sync_is_enabled()
  263. */
  264. if (rsnd_src_sync_is_enabled(mod))
  265. sys_int_val = sys_int_val & 0xffff;
  266. rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
  267. rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
  268. rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
  269. return 0;
  270. }
  271. static void rsnd_src_status_clear(struct rsnd_mod *mod)
  272. {
  273. u32 val = OUF_SRC(rsnd_mod_id(mod));
  274. rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
  275. rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
  276. }
  277. static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
  278. {
  279. u32 val0, val1;
  280. bool ret = false;
  281. val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
  282. /*
  283. * WORKAROUND
  284. *
  285. * ignore over flow error when rsnd_src_sync_is_enabled()
  286. */
  287. if (rsnd_src_sync_is_enabled(mod))
  288. val0 = val0 & 0xffff;
  289. if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
  290. (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1))
  291. ret = true;
  292. return ret;
  293. }
  294. static int rsnd_src_start(struct rsnd_mod *mod,
  295. struct rsnd_dai_stream *io,
  296. struct rsnd_priv *priv)
  297. {
  298. u32 val;
  299. /*
  300. * WORKAROUND
  301. *
  302. * Enable SRC output if you want to use sync convert together with DVC
  303. */
  304. val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
  305. 0x01 : 0x11;
  306. rsnd_mod_write(mod, SRC_CTRL, val);
  307. return 0;
  308. }
  309. static int rsnd_src_stop(struct rsnd_mod *mod,
  310. struct rsnd_dai_stream *io,
  311. struct rsnd_priv *priv)
  312. {
  313. rsnd_mod_write(mod, SRC_CTRL, 0);
  314. return 0;
  315. }
  316. static int rsnd_src_init(struct rsnd_mod *mod,
  317. struct rsnd_dai_stream *io,
  318. struct rsnd_priv *priv)
  319. {
  320. struct rsnd_src *src = rsnd_mod_to_src(mod);
  321. rsnd_mod_power_on(mod);
  322. rsnd_src_activation(mod);
  323. rsnd_src_set_convert_rate(io, mod);
  324. rsnd_src_status_clear(mod);
  325. /* reset sync convert_rate */
  326. src->sync.val = 0;
  327. return 0;
  328. }
  329. static int rsnd_src_quit(struct rsnd_mod *mod,
  330. struct rsnd_dai_stream *io,
  331. struct rsnd_priv *priv)
  332. {
  333. struct rsnd_src *src = rsnd_mod_to_src(mod);
  334. rsnd_src_halt(mod);
  335. rsnd_mod_power_off(mod);
  336. src->convert_rate = 0;
  337. /* reset sync convert_rate */
  338. src->sync.val = 0;
  339. return 0;
  340. }
  341. static void __rsnd_src_interrupt(struct rsnd_mod *mod,
  342. struct rsnd_dai_stream *io)
  343. {
  344. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  345. bool stop = false;
  346. spin_lock(&priv->lock);
  347. /* ignore all cases if not working */
  348. if (!rsnd_io_is_working(io))
  349. goto rsnd_src_interrupt_out;
  350. if (rsnd_src_error_occurred(mod))
  351. stop = true;
  352. rsnd_src_status_clear(mod);
  353. rsnd_src_interrupt_out:
  354. spin_unlock(&priv->lock);
  355. if (stop)
  356. snd_pcm_stop_xrun(io->substream);
  357. }
  358. static irqreturn_t rsnd_src_interrupt(int irq, void *data)
  359. {
  360. struct rsnd_mod *mod = data;
  361. rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
  362. return IRQ_HANDLED;
  363. }
  364. static int rsnd_src_probe_(struct rsnd_mod *mod,
  365. struct rsnd_dai_stream *io,
  366. struct rsnd_priv *priv)
  367. {
  368. struct rsnd_src *src = rsnd_mod_to_src(mod);
  369. struct device *dev = rsnd_priv_to_dev(priv);
  370. int irq = src->irq;
  371. int ret;
  372. if (irq > 0) {
  373. /*
  374. * IRQ is not supported on non-DT
  375. * see
  376. * rsnd_src_irq()
  377. */
  378. ret = devm_request_irq(dev, irq,
  379. rsnd_src_interrupt,
  380. IRQF_SHARED,
  381. dev_name(dev), mod);
  382. if (ret)
  383. return ret;
  384. }
  385. ret = rsnd_dma_attach(io, mod, &src->dma, 0);
  386. return ret;
  387. }
  388. static int rsnd_src_pcm_new(struct rsnd_mod *mod,
  389. struct rsnd_dai_stream *io,
  390. struct snd_soc_pcm_runtime *rtd)
  391. {
  392. struct rsnd_src *src = rsnd_mod_to_src(mod);
  393. int ret;
  394. /*
  395. * enable SRC sync convert if possible
  396. */
  397. /*
  398. * It can't use SRC Synchronous convert
  399. * when Capture if it uses CMD
  400. */
  401. if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
  402. return 0;
  403. /*
  404. * enable sync convert
  405. */
  406. ret = rsnd_kctrl_new_s(mod, io, rtd,
  407. rsnd_io_is_play(io) ?
  408. "SRC Out Rate Switch" :
  409. "SRC In Rate Switch",
  410. rsnd_src_set_convert_rate,
  411. &src->sen, 1);
  412. if (ret < 0)
  413. return ret;
  414. ret = rsnd_kctrl_new_s(mod, io, rtd,
  415. rsnd_io_is_play(io) ?
  416. "SRC Out Rate" :
  417. "SRC In Rate",
  418. rsnd_src_set_convert_rate,
  419. &src->sync, 192000);
  420. return ret;
  421. }
  422. static struct rsnd_mod_ops rsnd_src_ops = {
  423. .name = SRC_NAME,
  424. .dma_req = rsnd_src_dma_req,
  425. .probe = rsnd_src_probe_,
  426. .init = rsnd_src_init,
  427. .quit = rsnd_src_quit,
  428. .start = rsnd_src_start,
  429. .stop = rsnd_src_stop,
  430. .irq = rsnd_src_irq,
  431. .hw_params = rsnd_src_hw_params,
  432. .pcm_new = rsnd_src_pcm_new,
  433. };
  434. struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
  435. {
  436. if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
  437. id = 0;
  438. return rsnd_mod_get(rsnd_src_get(priv, id));
  439. }
  440. int rsnd_src_probe(struct rsnd_priv *priv)
  441. {
  442. struct device_node *node;
  443. struct device_node *np;
  444. struct device *dev = rsnd_priv_to_dev(priv);
  445. struct rsnd_src *src;
  446. struct clk *clk;
  447. char name[RSND_SRC_NAME_SIZE];
  448. int i, nr, ret;
  449. /* This driver doesn't support Gen1 at this point */
  450. if (rsnd_is_gen1(priv))
  451. return 0;
  452. node = rsnd_src_of_node(priv);
  453. if (!node)
  454. return 0; /* not used is not error */
  455. nr = of_get_child_count(node);
  456. if (!nr) {
  457. ret = -EINVAL;
  458. goto rsnd_src_probe_done;
  459. }
  460. src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
  461. if (!src) {
  462. ret = -ENOMEM;
  463. goto rsnd_src_probe_done;
  464. }
  465. priv->src_nr = nr;
  466. priv->src = src;
  467. i = 0;
  468. for_each_child_of_node(node, np) {
  469. if (!of_device_is_available(np))
  470. goto skip;
  471. src = rsnd_src_get(priv, i);
  472. snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
  473. SRC_NAME, i);
  474. src->irq = irq_of_parse_and_map(np, 0);
  475. if (!src->irq) {
  476. ret = -EINVAL;
  477. goto rsnd_src_probe_done;
  478. }
  479. clk = devm_clk_get(dev, name);
  480. if (IS_ERR(clk)) {
  481. ret = PTR_ERR(clk);
  482. goto rsnd_src_probe_done;
  483. }
  484. ret = rsnd_mod_init(priv, rsnd_mod_get(src),
  485. &rsnd_src_ops, clk, rsnd_mod_get_status,
  486. RSND_MOD_SRC, i);
  487. if (ret)
  488. goto rsnd_src_probe_done;
  489. skip:
  490. i++;
  491. }
  492. ret = 0;
  493. rsnd_src_probe_done:
  494. of_node_put(node);
  495. return ret;
  496. }
  497. void rsnd_src_remove(struct rsnd_priv *priv)
  498. {
  499. struct rsnd_src *src;
  500. int i;
  501. for_each_rsnd_src(src, priv, i) {
  502. rsnd_mod_quit(rsnd_mod_get(src));
  503. }
  504. }