core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Renesas R-Car SRU/SCU/SSIU/SSI support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * Based on fsi.c
  8. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. /*
  15. * Renesas R-Car sound device structure
  16. *
  17. * Gen1
  18. *
  19. * SRU : Sound Routing Unit
  20. * - SRC : Sampling Rate Converter
  21. * - CMD
  22. * - CTU : Channel Count Conversion Unit
  23. * - MIX : Mixer
  24. * - DVC : Digital Volume and Mute Function
  25. * - SSI : Serial Sound Interface
  26. *
  27. * Gen2
  28. *
  29. * SCU : Sampling Rate Converter Unit
  30. * - SRC : Sampling Rate Converter
  31. * - CMD
  32. * - CTU : Channel Count Conversion Unit
  33. * - MIX : Mixer
  34. * - DVC : Digital Volume and Mute Function
  35. * SSIU : Serial Sound Interface Unit
  36. * - SSI : Serial Sound Interface
  37. */
  38. /*
  39. * driver data Image
  40. *
  41. * rsnd_priv
  42. * |
  43. * | ** this depends on Gen1/Gen2
  44. * |
  45. * +- gen
  46. * |
  47. * | ** these depend on data path
  48. * | ** gen and platform data control it
  49. * |
  50. * +- rdai[0]
  51. * | | sru ssiu ssi
  52. * | +- playback -> [mod] -> [mod] -> [mod] -> ...
  53. * | |
  54. * | | sru ssiu ssi
  55. * | +- capture -> [mod] -> [mod] -> [mod] -> ...
  56. * |
  57. * +- rdai[1]
  58. * | | sru ssiu ssi
  59. * | +- playback -> [mod] -> [mod] -> [mod] -> ...
  60. * | |
  61. * | | sru ssiu ssi
  62. * | +- capture -> [mod] -> [mod] -> [mod] -> ...
  63. * ...
  64. * |
  65. * | ** these control ssi
  66. * |
  67. * +- ssi
  68. * | |
  69. * | +- ssi[0]
  70. * | +- ssi[1]
  71. * | +- ssi[2]
  72. * | ...
  73. * |
  74. * | ** these control scu
  75. * |
  76. * +- scu
  77. * |
  78. * +- scu[0]
  79. * +- scu[1]
  80. * +- scu[2]
  81. * ...
  82. *
  83. *
  84. * for_each_rsnd_dai(xx, priv, xx)
  85. * rdai[0] => rdai[1] => rdai[2] => ...
  86. *
  87. * for_each_rsnd_mod(xx, rdai, xx)
  88. * [mod] => [mod] => [mod] => ...
  89. *
  90. * rsnd_dai_call(xxx, fn )
  91. * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
  92. *
  93. */
  94. #include <linux/pm_runtime.h>
  95. #include "rsnd.h"
  96. #define RSND_RATES SNDRV_PCM_RATE_8000_96000
  97. #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  98. /*
  99. * rsnd_platform functions
  100. */
  101. #define rsnd_platform_call(priv, dai, func, param...) \
  102. (!(priv->info->func) ? -ENODEV : \
  103. priv->info->func(param))
  104. /*
  105. * rsnd_mod functions
  106. */
  107. char *rsnd_mod_name(struct rsnd_mod *mod)
  108. {
  109. if (!mod || !mod->ops)
  110. return "unknown";
  111. return mod->ops->name;
  112. }
  113. void rsnd_mod_init(struct rsnd_priv *priv,
  114. struct rsnd_mod *mod,
  115. struct rsnd_mod_ops *ops,
  116. int id)
  117. {
  118. mod->priv = priv;
  119. mod->id = id;
  120. mod->ops = ops;
  121. INIT_LIST_HEAD(&mod->list);
  122. }
  123. /*
  124. * rsnd_dai functions
  125. */
  126. #define rsnd_dai_call(rdai, io, fn) \
  127. ({ \
  128. struct rsnd_mod *mod, *n; \
  129. int ret = 0; \
  130. for_each_rsnd_mod(mod, n, io) { \
  131. ret = rsnd_mod_call(mod, fn, rdai, io); \
  132. if (ret < 0) \
  133. break; \
  134. } \
  135. ret; \
  136. })
  137. int rsnd_dai_connect(struct rsnd_dai *rdai,
  138. struct rsnd_mod *mod,
  139. struct rsnd_dai_stream *io)
  140. {
  141. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  142. struct device *dev = rsnd_priv_to_dev(priv);
  143. if (!mod) {
  144. dev_err(dev, "NULL mod\n");
  145. return -EIO;
  146. }
  147. if (!list_empty(&mod->list)) {
  148. dev_err(dev, "%s%d is not empty\n",
  149. rsnd_mod_name(mod),
  150. rsnd_mod_id(mod));
  151. return -EIO;
  152. }
  153. list_add_tail(&mod->list, &io->head);
  154. return 0;
  155. }
  156. int rsnd_dai_disconnect(struct rsnd_mod *mod)
  157. {
  158. list_del_init(&mod->list);
  159. return 0;
  160. }
  161. struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
  162. {
  163. return priv->rdai + id;
  164. }
  165. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  166. {
  167. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  168. return rsnd_dai_get(priv, dai->id);
  169. }
  170. int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
  171. {
  172. return &rdai->playback == io;
  173. }
  174. /*
  175. * rsnd_soc_dai functions
  176. */
  177. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
  178. {
  179. struct snd_pcm_substream *substream = io->substream;
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. int pos = io->byte_pos + additional;
  182. pos %= (runtime->periods * io->byte_per_period);
  183. return pos;
  184. }
  185. void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
  186. {
  187. io->byte_pos += byte;
  188. if (io->byte_pos >= io->next_period_byte) {
  189. struct snd_pcm_substream *substream = io->substream;
  190. struct snd_pcm_runtime *runtime = substream->runtime;
  191. io->period_pos++;
  192. io->next_period_byte += io->byte_per_period;
  193. if (io->period_pos >= runtime->periods) {
  194. io->byte_pos = 0;
  195. io->period_pos = 0;
  196. io->next_period_byte = io->byte_per_period;
  197. }
  198. snd_pcm_period_elapsed(substream);
  199. }
  200. }
  201. static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  202. struct snd_pcm_substream *substream)
  203. {
  204. struct snd_pcm_runtime *runtime = substream->runtime;
  205. if (!list_empty(&io->head))
  206. return -EIO;
  207. INIT_LIST_HEAD(&io->head);
  208. io->substream = substream;
  209. io->byte_pos = 0;
  210. io->period_pos = 0;
  211. io->byte_per_period = runtime->period_size *
  212. runtime->channels *
  213. samples_to_bytes(runtime, 1);
  214. io->next_period_byte = io->byte_per_period;
  215. return 0;
  216. }
  217. static
  218. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  219. {
  220. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  221. return rtd->cpu_dai;
  222. }
  223. static
  224. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  225. struct snd_pcm_substream *substream)
  226. {
  227. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  228. return &rdai->playback;
  229. else
  230. return &rdai->capture;
  231. }
  232. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  233. struct snd_soc_dai *dai)
  234. {
  235. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  236. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  237. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  238. struct rsnd_dai_platform_info *info = rsnd_dai_get_platform_info(rdai);
  239. int ssi_id = rsnd_dai_is_play(rdai, io) ? info->ssi_id_playback :
  240. info->ssi_id_capture;
  241. int ret;
  242. unsigned long flags;
  243. rsnd_lock(priv, flags);
  244. switch (cmd) {
  245. case SNDRV_PCM_TRIGGER_START:
  246. ret = rsnd_dai_stream_init(io, substream);
  247. if (ret < 0)
  248. goto dai_trigger_end;
  249. ret = rsnd_platform_call(priv, dai, start, ssi_id);
  250. if (ret < 0)
  251. goto dai_trigger_end;
  252. ret = rsnd_dai_call(rdai, io, init);
  253. if (ret < 0)
  254. goto dai_trigger_end;
  255. ret = rsnd_dai_call(rdai, io, start);
  256. if (ret < 0)
  257. goto dai_trigger_end;
  258. break;
  259. case SNDRV_PCM_TRIGGER_STOP:
  260. ret = rsnd_dai_call(rdai, io, stop);
  261. if (ret < 0)
  262. goto dai_trigger_end;
  263. ret = rsnd_dai_call(rdai, io, quit);
  264. if (ret < 0)
  265. goto dai_trigger_end;
  266. ret = rsnd_platform_call(priv, dai, stop, ssi_id);
  267. if (ret < 0)
  268. goto dai_trigger_end;
  269. break;
  270. default:
  271. ret = -EINVAL;
  272. }
  273. dai_trigger_end:
  274. rsnd_unlock(priv, flags);
  275. return ret;
  276. }
  277. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  278. {
  279. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  280. /* set master/slave audio interface */
  281. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  282. case SND_SOC_DAIFMT_CBM_CFM:
  283. rdai->clk_master = 1;
  284. break;
  285. case SND_SOC_DAIFMT_CBS_CFS:
  286. rdai->clk_master = 0;
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. /* set clock inversion */
  292. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  293. case SND_SOC_DAIFMT_NB_IF:
  294. rdai->bit_clk_inv = 0;
  295. rdai->frm_clk_inv = 1;
  296. break;
  297. case SND_SOC_DAIFMT_IB_NF:
  298. rdai->bit_clk_inv = 1;
  299. rdai->frm_clk_inv = 0;
  300. break;
  301. case SND_SOC_DAIFMT_IB_IF:
  302. rdai->bit_clk_inv = 1;
  303. rdai->frm_clk_inv = 1;
  304. break;
  305. case SND_SOC_DAIFMT_NB_NF:
  306. default:
  307. rdai->bit_clk_inv = 0;
  308. rdai->frm_clk_inv = 0;
  309. break;
  310. }
  311. /* set format */
  312. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  313. case SND_SOC_DAIFMT_I2S:
  314. rdai->sys_delay = 0;
  315. rdai->data_alignment = 0;
  316. break;
  317. case SND_SOC_DAIFMT_LEFT_J:
  318. rdai->sys_delay = 1;
  319. rdai->data_alignment = 0;
  320. break;
  321. case SND_SOC_DAIFMT_RIGHT_J:
  322. rdai->sys_delay = 1;
  323. rdai->data_alignment = 1;
  324. break;
  325. }
  326. return 0;
  327. }
  328. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  329. .trigger = rsnd_soc_dai_trigger,
  330. .set_fmt = rsnd_soc_dai_set_fmt,
  331. };
  332. static int rsnd_dai_probe(struct platform_device *pdev,
  333. struct rcar_snd_info *info,
  334. struct rsnd_priv *priv)
  335. {
  336. struct snd_soc_dai_driver *drv;
  337. struct rsnd_dai *rdai;
  338. struct device *dev = rsnd_priv_to_dev(priv);
  339. struct rsnd_dai_platform_info *dai_info;
  340. int dai_nr = info->dai_info_nr;
  341. int i, pid, cid;
  342. drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
  343. rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
  344. if (!drv || !rdai) {
  345. dev_err(dev, "dai allocate failed\n");
  346. return -ENOMEM;
  347. }
  348. for (i = 0; i < dai_nr; i++) {
  349. dai_info = &info->dai_info[i];
  350. pid = dai_info->ssi_id_playback;
  351. cid = dai_info->ssi_id_capture;
  352. /*
  353. * init rsnd_dai
  354. */
  355. INIT_LIST_HEAD(&rdai[i].playback.head);
  356. INIT_LIST_HEAD(&rdai[i].capture.head);
  357. rdai[i].info = dai_info;
  358. snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
  359. /*
  360. * init snd_soc_dai_driver
  361. */
  362. drv[i].name = rdai[i].name;
  363. drv[i].ops = &rsnd_soc_dai_ops;
  364. if (pid >= 0) {
  365. drv[i].playback.rates = RSND_RATES;
  366. drv[i].playback.formats = RSND_FMTS;
  367. drv[i].playback.channels_min = 2;
  368. drv[i].playback.channels_max = 2;
  369. }
  370. if (cid >= 0) {
  371. drv[i].capture.rates = RSND_RATES;
  372. drv[i].capture.formats = RSND_FMTS;
  373. drv[i].capture.channels_min = 2;
  374. drv[i].capture.channels_max = 2;
  375. }
  376. dev_dbg(dev, "%s (%d, %d) probed", rdai[i].name, pid, cid);
  377. }
  378. priv->dai_nr = dai_nr;
  379. priv->daidrv = drv;
  380. priv->rdai = rdai;
  381. return 0;
  382. }
  383. static void rsnd_dai_remove(struct platform_device *pdev,
  384. struct rsnd_priv *priv)
  385. {
  386. }
  387. /*
  388. * pcm ops
  389. */
  390. static struct snd_pcm_hardware rsnd_pcm_hardware = {
  391. .info = SNDRV_PCM_INFO_INTERLEAVED |
  392. SNDRV_PCM_INFO_MMAP |
  393. SNDRV_PCM_INFO_MMAP_VALID |
  394. SNDRV_PCM_INFO_PAUSE,
  395. .formats = RSND_FMTS,
  396. .rates = RSND_RATES,
  397. .rate_min = 8000,
  398. .rate_max = 192000,
  399. .channels_min = 2,
  400. .channels_max = 2,
  401. .buffer_bytes_max = 64 * 1024,
  402. .period_bytes_min = 32,
  403. .period_bytes_max = 8192,
  404. .periods_min = 1,
  405. .periods_max = 32,
  406. .fifo_size = 256,
  407. };
  408. static int rsnd_pcm_open(struct snd_pcm_substream *substream)
  409. {
  410. struct snd_pcm_runtime *runtime = substream->runtime;
  411. int ret = 0;
  412. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  413. ret = snd_pcm_hw_constraint_integer(runtime,
  414. SNDRV_PCM_HW_PARAM_PERIODS);
  415. return ret;
  416. }
  417. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  418. struct snd_pcm_hw_params *hw_params)
  419. {
  420. return snd_pcm_lib_malloc_pages(substream,
  421. params_buffer_bytes(hw_params));
  422. }
  423. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  424. {
  425. struct snd_pcm_runtime *runtime = substream->runtime;
  426. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  427. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  428. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  429. return bytes_to_frames(runtime, io->byte_pos);
  430. }
  431. static struct snd_pcm_ops rsnd_pcm_ops = {
  432. .open = rsnd_pcm_open,
  433. .ioctl = snd_pcm_lib_ioctl,
  434. .hw_params = rsnd_hw_params,
  435. .hw_free = snd_pcm_lib_free_pages,
  436. .pointer = rsnd_pointer,
  437. };
  438. /*
  439. * snd_soc_platform
  440. */
  441. #define PREALLOC_BUFFER (32 * 1024)
  442. #define PREALLOC_BUFFER_MAX (32 * 1024)
  443. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  444. {
  445. return snd_pcm_lib_preallocate_pages_for_all(
  446. rtd->pcm,
  447. SNDRV_DMA_TYPE_DEV,
  448. rtd->card->snd_card->dev,
  449. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  450. }
  451. static void rsnd_pcm_free(struct snd_pcm *pcm)
  452. {
  453. snd_pcm_lib_preallocate_free_for_all(pcm);
  454. }
  455. static struct snd_soc_platform_driver rsnd_soc_platform = {
  456. .ops = &rsnd_pcm_ops,
  457. .pcm_new = rsnd_pcm_new,
  458. .pcm_free = rsnd_pcm_free,
  459. };
  460. static const struct snd_soc_component_driver rsnd_soc_component = {
  461. .name = "rsnd",
  462. };
  463. /*
  464. * rsnd probe
  465. */
  466. static int rsnd_probe(struct platform_device *pdev)
  467. {
  468. struct rcar_snd_info *info;
  469. struct rsnd_priv *priv;
  470. struct device *dev = &pdev->dev;
  471. int ret;
  472. info = pdev->dev.platform_data;
  473. if (!info) {
  474. dev_err(dev, "driver needs R-Car sound information\n");
  475. return -ENODEV;
  476. }
  477. /*
  478. * init priv data
  479. */
  480. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  481. if (!priv) {
  482. dev_err(dev, "priv allocate failed\n");
  483. return -ENODEV;
  484. }
  485. priv->dev = dev;
  486. priv->info = info;
  487. spin_lock_init(&priv->lock);
  488. /*
  489. * init each module
  490. */
  491. ret = rsnd_dai_probe(pdev, info, priv);
  492. if (ret < 0)
  493. return ret;
  494. /*
  495. * asoc register
  496. */
  497. ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
  498. if (ret < 0) {
  499. dev_err(dev, "cannot snd soc register\n");
  500. return ret;
  501. }
  502. ret = snd_soc_register_component(dev, &rsnd_soc_component,
  503. priv->daidrv, rsnd_dai_nr(priv));
  504. if (ret < 0) {
  505. dev_err(dev, "cannot snd dai register\n");
  506. goto exit_snd_soc;
  507. }
  508. dev_set_drvdata(dev, priv);
  509. pm_runtime_enable(dev);
  510. dev_info(dev, "probed\n");
  511. return ret;
  512. exit_snd_soc:
  513. snd_soc_unregister_platform(dev);
  514. return ret;
  515. }
  516. static int rsnd_remove(struct platform_device *pdev)
  517. {
  518. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  519. pm_runtime_disable(&pdev->dev);
  520. /*
  521. * remove each module
  522. */
  523. rsnd_dai_remove(pdev, priv);
  524. return 0;
  525. }
  526. static struct platform_driver rsnd_driver = {
  527. .driver = {
  528. .name = "rcar_sound",
  529. },
  530. .probe = rsnd_probe,
  531. .remove = rsnd_remove,
  532. };
  533. module_platform_driver(rsnd_driver);
  534. MODULE_LICENSE("GPL");
  535. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  536. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  537. MODULE_ALIAS("platform:rcar-pcm-audio");