core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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_dai functions
  106. */
  107. struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
  108. {
  109. return priv->rdai + id;
  110. }
  111. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  112. {
  113. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  114. return rsnd_dai_get(priv, dai->id);
  115. }
  116. int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
  117. {
  118. return &rdai->playback == io;
  119. }
  120. /*
  121. * rsnd_soc_dai functions
  122. */
  123. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
  124. {
  125. struct snd_pcm_substream *substream = io->substream;
  126. struct snd_pcm_runtime *runtime = substream->runtime;
  127. int pos = io->byte_pos + additional;
  128. pos %= (runtime->periods * io->byte_per_period);
  129. return pos;
  130. }
  131. void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
  132. {
  133. io->byte_pos += byte;
  134. if (io->byte_pos >= io->next_period_byte) {
  135. struct snd_pcm_substream *substream = io->substream;
  136. struct snd_pcm_runtime *runtime = substream->runtime;
  137. io->period_pos++;
  138. io->next_period_byte += io->byte_per_period;
  139. if (io->period_pos >= runtime->periods) {
  140. io->byte_pos = 0;
  141. io->period_pos = 0;
  142. io->next_period_byte = io->byte_per_period;
  143. }
  144. snd_pcm_period_elapsed(substream);
  145. }
  146. }
  147. static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  148. struct snd_pcm_substream *substream)
  149. {
  150. struct snd_pcm_runtime *runtime = substream->runtime;
  151. if (!list_empty(&io->head))
  152. return -EIO;
  153. INIT_LIST_HEAD(&io->head);
  154. io->substream = substream;
  155. io->byte_pos = 0;
  156. io->period_pos = 0;
  157. io->byte_per_period = runtime->period_size *
  158. runtime->channels *
  159. samples_to_bytes(runtime, 1);
  160. io->next_period_byte = io->byte_per_period;
  161. return 0;
  162. }
  163. static
  164. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  165. {
  166. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  167. return rtd->cpu_dai;
  168. }
  169. static
  170. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  171. struct snd_pcm_substream *substream)
  172. {
  173. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. return &rdai->playback;
  175. else
  176. return &rdai->capture;
  177. }
  178. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  179. struct snd_soc_dai *dai)
  180. {
  181. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  182. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  183. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  184. struct rsnd_dai_platform_info *info = rsnd_dai_get_platform_info(rdai);
  185. int ssi_id = rsnd_dai_is_play(rdai, io) ? info->ssi_id_playback :
  186. info->ssi_id_capture;
  187. int ret;
  188. unsigned long flags;
  189. rsnd_lock(priv, flags);
  190. switch (cmd) {
  191. case SNDRV_PCM_TRIGGER_START:
  192. ret = rsnd_dai_stream_init(io, substream);
  193. if (ret < 0)
  194. goto dai_trigger_end;
  195. ret = rsnd_platform_call(priv, dai, start, ssi_id);
  196. if (ret < 0)
  197. goto dai_trigger_end;
  198. break;
  199. case SNDRV_PCM_TRIGGER_STOP:
  200. ret = rsnd_platform_call(priv, dai, stop, ssi_id);
  201. if (ret < 0)
  202. goto dai_trigger_end;
  203. break;
  204. default:
  205. ret = -EINVAL;
  206. }
  207. dai_trigger_end:
  208. rsnd_unlock(priv, flags);
  209. return ret;
  210. }
  211. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  212. {
  213. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  214. /* set master/slave audio interface */
  215. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  216. case SND_SOC_DAIFMT_CBM_CFM:
  217. rdai->clk_master = 1;
  218. break;
  219. case SND_SOC_DAIFMT_CBS_CFS:
  220. rdai->clk_master = 0;
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. /* set clock inversion */
  226. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  227. case SND_SOC_DAIFMT_NB_IF:
  228. rdai->bit_clk_inv = 0;
  229. rdai->frm_clk_inv = 1;
  230. break;
  231. case SND_SOC_DAIFMT_IB_NF:
  232. rdai->bit_clk_inv = 1;
  233. rdai->frm_clk_inv = 0;
  234. break;
  235. case SND_SOC_DAIFMT_IB_IF:
  236. rdai->bit_clk_inv = 1;
  237. rdai->frm_clk_inv = 1;
  238. break;
  239. case SND_SOC_DAIFMT_NB_NF:
  240. default:
  241. rdai->bit_clk_inv = 0;
  242. rdai->frm_clk_inv = 0;
  243. break;
  244. }
  245. /* set format */
  246. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  247. case SND_SOC_DAIFMT_I2S:
  248. rdai->sys_delay = 0;
  249. rdai->data_alignment = 0;
  250. break;
  251. case SND_SOC_DAIFMT_LEFT_J:
  252. rdai->sys_delay = 1;
  253. rdai->data_alignment = 0;
  254. break;
  255. case SND_SOC_DAIFMT_RIGHT_J:
  256. rdai->sys_delay = 1;
  257. rdai->data_alignment = 1;
  258. break;
  259. }
  260. return 0;
  261. }
  262. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  263. .trigger = rsnd_soc_dai_trigger,
  264. .set_fmt = rsnd_soc_dai_set_fmt,
  265. };
  266. static int rsnd_dai_probe(struct platform_device *pdev,
  267. struct rcar_snd_info *info,
  268. struct rsnd_priv *priv)
  269. {
  270. struct snd_soc_dai_driver *drv;
  271. struct rsnd_dai *rdai;
  272. struct device *dev = rsnd_priv_to_dev(priv);
  273. struct rsnd_dai_platform_info *dai_info;
  274. int dai_nr = info->dai_info_nr;
  275. int i, pid, cid;
  276. drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
  277. rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
  278. if (!drv || !rdai) {
  279. dev_err(dev, "dai allocate failed\n");
  280. return -ENOMEM;
  281. }
  282. for (i = 0; i < dai_nr; i++) {
  283. dai_info = &info->dai_info[i];
  284. pid = dai_info->ssi_id_playback;
  285. cid = dai_info->ssi_id_capture;
  286. /*
  287. * init rsnd_dai
  288. */
  289. INIT_LIST_HEAD(&rdai[i].playback.head);
  290. INIT_LIST_HEAD(&rdai[i].capture.head);
  291. rdai[i].info = dai_info;
  292. snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
  293. /*
  294. * init snd_soc_dai_driver
  295. */
  296. drv[i].name = rdai[i].name;
  297. drv[i].ops = &rsnd_soc_dai_ops;
  298. if (pid >= 0) {
  299. drv[i].playback.rates = RSND_RATES;
  300. drv[i].playback.formats = RSND_FMTS;
  301. drv[i].playback.channels_min = 2;
  302. drv[i].playback.channels_max = 2;
  303. }
  304. if (cid >= 0) {
  305. drv[i].capture.rates = RSND_RATES;
  306. drv[i].capture.formats = RSND_FMTS;
  307. drv[i].capture.channels_min = 2;
  308. drv[i].capture.channels_max = 2;
  309. }
  310. dev_dbg(dev, "%s (%d, %d) probed", rdai[i].name, pid, cid);
  311. }
  312. priv->dai_nr = dai_nr;
  313. priv->daidrv = drv;
  314. priv->rdai = rdai;
  315. return 0;
  316. }
  317. static void rsnd_dai_remove(struct platform_device *pdev,
  318. struct rsnd_priv *priv)
  319. {
  320. }
  321. /*
  322. * pcm ops
  323. */
  324. static struct snd_pcm_hardware rsnd_pcm_hardware = {
  325. .info = SNDRV_PCM_INFO_INTERLEAVED |
  326. SNDRV_PCM_INFO_MMAP |
  327. SNDRV_PCM_INFO_MMAP_VALID |
  328. SNDRV_PCM_INFO_PAUSE,
  329. .formats = RSND_FMTS,
  330. .rates = RSND_RATES,
  331. .rate_min = 8000,
  332. .rate_max = 192000,
  333. .channels_min = 2,
  334. .channels_max = 2,
  335. .buffer_bytes_max = 64 * 1024,
  336. .period_bytes_min = 32,
  337. .period_bytes_max = 8192,
  338. .periods_min = 1,
  339. .periods_max = 32,
  340. .fifo_size = 256,
  341. };
  342. static int rsnd_pcm_open(struct snd_pcm_substream *substream)
  343. {
  344. struct snd_pcm_runtime *runtime = substream->runtime;
  345. int ret = 0;
  346. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  347. ret = snd_pcm_hw_constraint_integer(runtime,
  348. SNDRV_PCM_HW_PARAM_PERIODS);
  349. return ret;
  350. }
  351. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  352. struct snd_pcm_hw_params *hw_params)
  353. {
  354. return snd_pcm_lib_malloc_pages(substream,
  355. params_buffer_bytes(hw_params));
  356. }
  357. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  358. {
  359. struct snd_pcm_runtime *runtime = substream->runtime;
  360. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  361. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  362. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  363. return bytes_to_frames(runtime, io->byte_pos);
  364. }
  365. static struct snd_pcm_ops rsnd_pcm_ops = {
  366. .open = rsnd_pcm_open,
  367. .ioctl = snd_pcm_lib_ioctl,
  368. .hw_params = rsnd_hw_params,
  369. .hw_free = snd_pcm_lib_free_pages,
  370. .pointer = rsnd_pointer,
  371. };
  372. /*
  373. * snd_soc_platform
  374. */
  375. #define PREALLOC_BUFFER (32 * 1024)
  376. #define PREALLOC_BUFFER_MAX (32 * 1024)
  377. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  378. {
  379. return snd_pcm_lib_preallocate_pages_for_all(
  380. rtd->pcm,
  381. SNDRV_DMA_TYPE_DEV,
  382. rtd->card->snd_card->dev,
  383. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  384. }
  385. static void rsnd_pcm_free(struct snd_pcm *pcm)
  386. {
  387. snd_pcm_lib_preallocate_free_for_all(pcm);
  388. }
  389. static struct snd_soc_platform_driver rsnd_soc_platform = {
  390. .ops = &rsnd_pcm_ops,
  391. .pcm_new = rsnd_pcm_new,
  392. .pcm_free = rsnd_pcm_free,
  393. };
  394. static const struct snd_soc_component_driver rsnd_soc_component = {
  395. .name = "rsnd",
  396. };
  397. /*
  398. * rsnd probe
  399. */
  400. static int rsnd_probe(struct platform_device *pdev)
  401. {
  402. struct rcar_snd_info *info;
  403. struct rsnd_priv *priv;
  404. struct device *dev = &pdev->dev;
  405. int ret;
  406. info = pdev->dev.platform_data;
  407. if (!info) {
  408. dev_err(dev, "driver needs R-Car sound information\n");
  409. return -ENODEV;
  410. }
  411. /*
  412. * init priv data
  413. */
  414. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  415. if (!priv) {
  416. dev_err(dev, "priv allocate failed\n");
  417. return -ENODEV;
  418. }
  419. priv->dev = dev;
  420. priv->info = info;
  421. spin_lock_init(&priv->lock);
  422. /*
  423. * init each module
  424. */
  425. ret = rsnd_dai_probe(pdev, info, priv);
  426. if (ret < 0)
  427. return ret;
  428. /*
  429. * asoc register
  430. */
  431. ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
  432. if (ret < 0) {
  433. dev_err(dev, "cannot snd soc register\n");
  434. return ret;
  435. }
  436. ret = snd_soc_register_component(dev, &rsnd_soc_component,
  437. priv->daidrv, rsnd_dai_nr(priv));
  438. if (ret < 0) {
  439. dev_err(dev, "cannot snd dai register\n");
  440. goto exit_snd_soc;
  441. }
  442. dev_set_drvdata(dev, priv);
  443. pm_runtime_enable(dev);
  444. dev_info(dev, "probed\n");
  445. return ret;
  446. exit_snd_soc:
  447. snd_soc_unregister_platform(dev);
  448. return ret;
  449. }
  450. static int rsnd_remove(struct platform_device *pdev)
  451. {
  452. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  453. pm_runtime_disable(&pdev->dev);
  454. /*
  455. * remove each module
  456. */
  457. rsnd_dai_remove(pdev, priv);
  458. return 0;
  459. }
  460. static struct platform_driver rsnd_driver = {
  461. .driver = {
  462. .name = "rcar_sound",
  463. },
  464. .probe = rsnd_probe,
  465. .remove = rsnd_remove,
  466. };
  467. module_platform_driver(rsnd_driver);
  468. MODULE_LICENSE("GPL");
  469. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  470. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  471. MODULE_ALIAS("platform:rcar-pcm-audio");