core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 <linux/shdma-base.h>
  96. #include "rsnd.h"
  97. #define RSND_RATES SNDRV_PCM_RATE_8000_96000
  98. #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
  99. /*
  100. * rsnd_platform functions
  101. */
  102. #define rsnd_platform_call(priv, dai, func, param...) \
  103. (!(priv->info->func) ? 0 : \
  104. priv->info->func(param))
  105. /*
  106. * rsnd_mod functions
  107. */
  108. char *rsnd_mod_name(struct rsnd_mod *mod)
  109. {
  110. if (!mod || !mod->ops)
  111. return "unknown";
  112. return mod->ops->name;
  113. }
  114. void rsnd_mod_init(struct rsnd_priv *priv,
  115. struct rsnd_mod *mod,
  116. struct rsnd_mod_ops *ops,
  117. int id)
  118. {
  119. mod->priv = priv;
  120. mod->id = id;
  121. mod->ops = ops;
  122. INIT_LIST_HEAD(&mod->list);
  123. }
  124. /*
  125. * rsnd_dma functions
  126. */
  127. static void rsnd_dma_continue(struct rsnd_dma *dma)
  128. {
  129. /* push next A or B plane */
  130. dma->submit_loop = 1;
  131. schedule_work(&dma->work);
  132. }
  133. void rsnd_dma_start(struct rsnd_dma *dma)
  134. {
  135. /* push both A and B plane*/
  136. dma->submit_loop = 2;
  137. schedule_work(&dma->work);
  138. }
  139. void rsnd_dma_stop(struct rsnd_dma *dma)
  140. {
  141. dma->submit_loop = 0;
  142. cancel_work_sync(&dma->work);
  143. dmaengine_terminate_all(dma->chan);
  144. }
  145. static void rsnd_dma_complete(void *data)
  146. {
  147. struct rsnd_dma *dma = (struct rsnd_dma *)data;
  148. struct rsnd_priv *priv = dma->priv;
  149. unsigned long flags;
  150. rsnd_lock(priv, flags);
  151. dma->complete(dma);
  152. if (dma->submit_loop)
  153. rsnd_dma_continue(dma);
  154. rsnd_unlock(priv, flags);
  155. }
  156. static void rsnd_dma_do_work(struct work_struct *work)
  157. {
  158. struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
  159. struct rsnd_priv *priv = dma->priv;
  160. struct device *dev = rsnd_priv_to_dev(priv);
  161. struct dma_async_tx_descriptor *desc;
  162. dma_addr_t buf;
  163. size_t len;
  164. int i;
  165. for (i = 0; i < dma->submit_loop; i++) {
  166. if (dma->inquiry(dma, &buf, &len) < 0)
  167. return;
  168. desc = dmaengine_prep_slave_single(
  169. dma->chan, buf, len, dma->dir,
  170. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  171. if (!desc) {
  172. dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
  173. return;
  174. }
  175. desc->callback = rsnd_dma_complete;
  176. desc->callback_param = dma;
  177. if (dmaengine_submit(desc) < 0) {
  178. dev_err(dev, "dmaengine_submit() fail\n");
  179. return;
  180. }
  181. dma_async_issue_pending(dma->chan);
  182. }
  183. }
  184. int rsnd_dma_available(struct rsnd_dma *dma)
  185. {
  186. return !!dma->chan;
  187. }
  188. int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
  189. int is_play, int id,
  190. int (*inquiry)(struct rsnd_dma *dma,
  191. dma_addr_t *buf, int *len),
  192. int (*complete)(struct rsnd_dma *dma))
  193. {
  194. struct device *dev = rsnd_priv_to_dev(priv);
  195. struct dma_slave_config cfg;
  196. dma_cap_mask_t mask;
  197. int ret;
  198. if (dma->chan) {
  199. dev_err(dev, "it already has dma channel\n");
  200. return -EIO;
  201. }
  202. dma_cap_zero(mask);
  203. dma_cap_set(DMA_SLAVE, mask);
  204. dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  205. (void *)id, dev,
  206. is_play ? "tx" : "rx");
  207. if (!dma->chan) {
  208. dev_err(dev, "can't get dma channel\n");
  209. return -EIO;
  210. }
  211. cfg.slave_id = id;
  212. cfg.dst_addr = 0; /* use default addr when playback */
  213. cfg.src_addr = 0; /* use default addr when capture */
  214. cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  215. ret = dmaengine_slave_config(dma->chan, &cfg);
  216. if (ret < 0)
  217. goto rsnd_dma_init_err;
  218. dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  219. dma->priv = priv;
  220. dma->inquiry = inquiry;
  221. dma->complete = complete;
  222. INIT_WORK(&dma->work, rsnd_dma_do_work);
  223. return 0;
  224. rsnd_dma_init_err:
  225. rsnd_dma_quit(priv, dma);
  226. return ret;
  227. }
  228. void rsnd_dma_quit(struct rsnd_priv *priv,
  229. struct rsnd_dma *dma)
  230. {
  231. if (dma->chan)
  232. dma_release_channel(dma->chan);
  233. dma->chan = NULL;
  234. }
  235. /*
  236. * rsnd_dai functions
  237. */
  238. #define rsnd_dai_call(rdai, io, fn) \
  239. ({ \
  240. struct rsnd_mod *mod, *n; \
  241. int ret = 0; \
  242. for_each_rsnd_mod(mod, n, io) { \
  243. ret = rsnd_mod_call(mod, fn, rdai, io); \
  244. if (ret < 0) \
  245. break; \
  246. } \
  247. ret; \
  248. })
  249. int rsnd_dai_connect(struct rsnd_dai *rdai,
  250. struct rsnd_mod *mod,
  251. struct rsnd_dai_stream *io)
  252. {
  253. if (!mod)
  254. return -EIO;
  255. if (!list_empty(&mod->list)) {
  256. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  257. struct device *dev = rsnd_priv_to_dev(priv);
  258. dev_err(dev, "%s%d is not empty\n",
  259. rsnd_mod_name(mod),
  260. rsnd_mod_id(mod));
  261. return -EIO;
  262. }
  263. list_add_tail(&mod->list, &io->head);
  264. return 0;
  265. }
  266. int rsnd_dai_disconnect(struct rsnd_mod *mod)
  267. {
  268. list_del_init(&mod->list);
  269. return 0;
  270. }
  271. int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
  272. {
  273. int id = rdai - priv->rdai;
  274. if ((id < 0) || (id >= rsnd_dai_nr(priv)))
  275. return -EINVAL;
  276. return id;
  277. }
  278. struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
  279. {
  280. if ((id < 0) || (id >= rsnd_dai_nr(priv)))
  281. return NULL;
  282. return priv->rdai + id;
  283. }
  284. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  285. {
  286. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  287. return rsnd_dai_get(priv, dai->id);
  288. }
  289. int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
  290. {
  291. return &rdai->playback == io;
  292. }
  293. /*
  294. * rsnd_soc_dai functions
  295. */
  296. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
  297. {
  298. struct snd_pcm_substream *substream = io->substream;
  299. struct snd_pcm_runtime *runtime = substream->runtime;
  300. int pos = io->byte_pos + additional;
  301. pos %= (runtime->periods * io->byte_per_period);
  302. return pos;
  303. }
  304. void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
  305. {
  306. io->byte_pos += byte;
  307. if (io->byte_pos >= io->next_period_byte) {
  308. struct snd_pcm_substream *substream = io->substream;
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. io->period_pos++;
  311. io->next_period_byte += io->byte_per_period;
  312. if (io->period_pos >= runtime->periods) {
  313. io->byte_pos = 0;
  314. io->period_pos = 0;
  315. io->next_period_byte = io->byte_per_period;
  316. }
  317. snd_pcm_period_elapsed(substream);
  318. }
  319. }
  320. static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  321. struct snd_pcm_substream *substream)
  322. {
  323. struct snd_pcm_runtime *runtime = substream->runtime;
  324. if (!list_empty(&io->head))
  325. return -EIO;
  326. INIT_LIST_HEAD(&io->head);
  327. io->substream = substream;
  328. io->byte_pos = 0;
  329. io->period_pos = 0;
  330. io->byte_per_period = runtime->period_size *
  331. runtime->channels *
  332. samples_to_bytes(runtime, 1);
  333. io->next_period_byte = io->byte_per_period;
  334. return 0;
  335. }
  336. static
  337. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  338. {
  339. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  340. return rtd->cpu_dai;
  341. }
  342. static
  343. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  344. struct snd_pcm_substream *substream)
  345. {
  346. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  347. return &rdai->playback;
  348. else
  349. return &rdai->capture;
  350. }
  351. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  352. struct snd_soc_dai *dai)
  353. {
  354. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  355. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  356. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  357. struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv,
  358. rsnd_dai_id(priv, rdai),
  359. rsnd_dai_is_play(rdai, io));
  360. int ssi_id = rsnd_mod_id(mod);
  361. int ret;
  362. unsigned long flags;
  363. rsnd_lock(priv, flags);
  364. switch (cmd) {
  365. case SNDRV_PCM_TRIGGER_START:
  366. ret = rsnd_dai_stream_init(io, substream);
  367. if (ret < 0)
  368. goto dai_trigger_end;
  369. ret = rsnd_platform_call(priv, dai, start, ssi_id);
  370. if (ret < 0)
  371. goto dai_trigger_end;
  372. ret = rsnd_gen_path_init(priv, rdai, io);
  373. if (ret < 0)
  374. goto dai_trigger_end;
  375. ret = rsnd_dai_call(rdai, io, init);
  376. if (ret < 0)
  377. goto dai_trigger_end;
  378. ret = rsnd_dai_call(rdai, io, start);
  379. if (ret < 0)
  380. goto dai_trigger_end;
  381. break;
  382. case SNDRV_PCM_TRIGGER_STOP:
  383. ret = rsnd_dai_call(rdai, io, stop);
  384. if (ret < 0)
  385. goto dai_trigger_end;
  386. ret = rsnd_dai_call(rdai, io, quit);
  387. if (ret < 0)
  388. goto dai_trigger_end;
  389. ret = rsnd_gen_path_exit(priv, rdai, io);
  390. if (ret < 0)
  391. goto dai_trigger_end;
  392. ret = rsnd_platform_call(priv, dai, stop, ssi_id);
  393. if (ret < 0)
  394. goto dai_trigger_end;
  395. break;
  396. default:
  397. ret = -EINVAL;
  398. }
  399. dai_trigger_end:
  400. rsnd_unlock(priv, flags);
  401. return ret;
  402. }
  403. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  404. {
  405. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  406. /* set master/slave audio interface */
  407. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  408. case SND_SOC_DAIFMT_CBM_CFM:
  409. rdai->clk_master = 1;
  410. break;
  411. case SND_SOC_DAIFMT_CBS_CFS:
  412. rdai->clk_master = 0;
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. /* set clock inversion */
  418. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  419. case SND_SOC_DAIFMT_NB_IF:
  420. rdai->bit_clk_inv = 0;
  421. rdai->frm_clk_inv = 1;
  422. break;
  423. case SND_SOC_DAIFMT_IB_NF:
  424. rdai->bit_clk_inv = 1;
  425. rdai->frm_clk_inv = 0;
  426. break;
  427. case SND_SOC_DAIFMT_IB_IF:
  428. rdai->bit_clk_inv = 1;
  429. rdai->frm_clk_inv = 1;
  430. break;
  431. case SND_SOC_DAIFMT_NB_NF:
  432. default:
  433. rdai->bit_clk_inv = 0;
  434. rdai->frm_clk_inv = 0;
  435. break;
  436. }
  437. /* set format */
  438. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  439. case SND_SOC_DAIFMT_I2S:
  440. rdai->sys_delay = 0;
  441. rdai->data_alignment = 0;
  442. break;
  443. case SND_SOC_DAIFMT_LEFT_J:
  444. rdai->sys_delay = 1;
  445. rdai->data_alignment = 0;
  446. break;
  447. case SND_SOC_DAIFMT_RIGHT_J:
  448. rdai->sys_delay = 1;
  449. rdai->data_alignment = 1;
  450. break;
  451. }
  452. return 0;
  453. }
  454. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  455. .trigger = rsnd_soc_dai_trigger,
  456. .set_fmt = rsnd_soc_dai_set_fmt,
  457. };
  458. static int rsnd_dai_probe(struct platform_device *pdev,
  459. struct rcar_snd_info *info,
  460. struct rsnd_priv *priv)
  461. {
  462. struct snd_soc_dai_driver *drv;
  463. struct rsnd_dai *rdai;
  464. struct rsnd_mod *pmod, *cmod;
  465. struct device *dev = rsnd_priv_to_dev(priv);
  466. int dai_nr;
  467. int i;
  468. /* get max dai nr */
  469. for (dai_nr = 0; dai_nr < 32; dai_nr++) {
  470. pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1);
  471. cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0);
  472. if (!pmod && !cmod)
  473. break;
  474. }
  475. if (!dai_nr) {
  476. dev_err(dev, "no dai\n");
  477. return -EIO;
  478. }
  479. drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
  480. rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
  481. if (!drv || !rdai) {
  482. dev_err(dev, "dai allocate failed\n");
  483. return -ENOMEM;
  484. }
  485. for (i = 0; i < dai_nr; i++) {
  486. pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1);
  487. cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0);
  488. /*
  489. * init rsnd_dai
  490. */
  491. INIT_LIST_HEAD(&rdai[i].playback.head);
  492. INIT_LIST_HEAD(&rdai[i].capture.head);
  493. snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
  494. /*
  495. * init snd_soc_dai_driver
  496. */
  497. drv[i].name = rdai[i].name;
  498. drv[i].ops = &rsnd_soc_dai_ops;
  499. if (pmod) {
  500. drv[i].playback.rates = RSND_RATES;
  501. drv[i].playback.formats = RSND_FMTS;
  502. drv[i].playback.channels_min = 2;
  503. drv[i].playback.channels_max = 2;
  504. }
  505. if (cmod) {
  506. drv[i].capture.rates = RSND_RATES;
  507. drv[i].capture.formats = RSND_FMTS;
  508. drv[i].capture.channels_min = 2;
  509. drv[i].capture.channels_max = 2;
  510. }
  511. dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
  512. pmod ? "play" : " -- ",
  513. cmod ? "capture" : " -- ");
  514. }
  515. priv->dai_nr = dai_nr;
  516. priv->daidrv = drv;
  517. priv->rdai = rdai;
  518. return 0;
  519. }
  520. static void rsnd_dai_remove(struct platform_device *pdev,
  521. struct rsnd_priv *priv)
  522. {
  523. }
  524. /*
  525. * pcm ops
  526. */
  527. static struct snd_pcm_hardware rsnd_pcm_hardware = {
  528. .info = SNDRV_PCM_INFO_INTERLEAVED |
  529. SNDRV_PCM_INFO_MMAP |
  530. SNDRV_PCM_INFO_MMAP_VALID |
  531. SNDRV_PCM_INFO_PAUSE,
  532. .formats = RSND_FMTS,
  533. .rates = RSND_RATES,
  534. .rate_min = 8000,
  535. .rate_max = 192000,
  536. .channels_min = 2,
  537. .channels_max = 2,
  538. .buffer_bytes_max = 64 * 1024,
  539. .period_bytes_min = 32,
  540. .period_bytes_max = 8192,
  541. .periods_min = 1,
  542. .periods_max = 32,
  543. .fifo_size = 256,
  544. };
  545. static int rsnd_pcm_open(struct snd_pcm_substream *substream)
  546. {
  547. struct snd_pcm_runtime *runtime = substream->runtime;
  548. int ret = 0;
  549. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  550. ret = snd_pcm_hw_constraint_integer(runtime,
  551. SNDRV_PCM_HW_PARAM_PERIODS);
  552. return ret;
  553. }
  554. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  555. struct snd_pcm_hw_params *hw_params)
  556. {
  557. return snd_pcm_lib_malloc_pages(substream,
  558. params_buffer_bytes(hw_params));
  559. }
  560. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  561. {
  562. struct snd_pcm_runtime *runtime = substream->runtime;
  563. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  564. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  565. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  566. return bytes_to_frames(runtime, io->byte_pos);
  567. }
  568. static struct snd_pcm_ops rsnd_pcm_ops = {
  569. .open = rsnd_pcm_open,
  570. .ioctl = snd_pcm_lib_ioctl,
  571. .hw_params = rsnd_hw_params,
  572. .hw_free = snd_pcm_lib_free_pages,
  573. .pointer = rsnd_pointer,
  574. };
  575. /*
  576. * snd_soc_platform
  577. */
  578. #define PREALLOC_BUFFER (32 * 1024)
  579. #define PREALLOC_BUFFER_MAX (32 * 1024)
  580. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  581. {
  582. return snd_pcm_lib_preallocate_pages_for_all(
  583. rtd->pcm,
  584. SNDRV_DMA_TYPE_DEV,
  585. rtd->card->snd_card->dev,
  586. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  587. }
  588. static void rsnd_pcm_free(struct snd_pcm *pcm)
  589. {
  590. snd_pcm_lib_preallocate_free_for_all(pcm);
  591. }
  592. static struct snd_soc_platform_driver rsnd_soc_platform = {
  593. .ops = &rsnd_pcm_ops,
  594. .pcm_new = rsnd_pcm_new,
  595. .pcm_free = rsnd_pcm_free,
  596. };
  597. static const struct snd_soc_component_driver rsnd_soc_component = {
  598. .name = "rsnd",
  599. };
  600. /*
  601. * rsnd probe
  602. */
  603. static int rsnd_probe(struct platform_device *pdev)
  604. {
  605. struct rcar_snd_info *info;
  606. struct rsnd_priv *priv;
  607. struct device *dev = &pdev->dev;
  608. int ret;
  609. info = pdev->dev.platform_data;
  610. if (!info) {
  611. dev_err(dev, "driver needs R-Car sound information\n");
  612. return -ENODEV;
  613. }
  614. /*
  615. * init priv data
  616. */
  617. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  618. if (!priv) {
  619. dev_err(dev, "priv allocate failed\n");
  620. return -ENODEV;
  621. }
  622. priv->dev = dev;
  623. priv->info = info;
  624. spin_lock_init(&priv->lock);
  625. /*
  626. * init each module
  627. */
  628. ret = rsnd_gen_probe(pdev, info, priv);
  629. if (ret < 0)
  630. return ret;
  631. ret = rsnd_scu_probe(pdev, info, priv);
  632. if (ret < 0)
  633. return ret;
  634. ret = rsnd_adg_probe(pdev, info, priv);
  635. if (ret < 0)
  636. return ret;
  637. ret = rsnd_ssi_probe(pdev, info, priv);
  638. if (ret < 0)
  639. return ret;
  640. ret = rsnd_dai_probe(pdev, info, priv);
  641. if (ret < 0)
  642. return ret;
  643. /*
  644. * asoc register
  645. */
  646. ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
  647. if (ret < 0) {
  648. dev_err(dev, "cannot snd soc register\n");
  649. return ret;
  650. }
  651. ret = snd_soc_register_component(dev, &rsnd_soc_component,
  652. priv->daidrv, rsnd_dai_nr(priv));
  653. if (ret < 0) {
  654. dev_err(dev, "cannot snd dai register\n");
  655. goto exit_snd_soc;
  656. }
  657. dev_set_drvdata(dev, priv);
  658. pm_runtime_enable(dev);
  659. dev_info(dev, "probed\n");
  660. return ret;
  661. exit_snd_soc:
  662. snd_soc_unregister_platform(dev);
  663. return ret;
  664. }
  665. static int rsnd_remove(struct platform_device *pdev)
  666. {
  667. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  668. pm_runtime_disable(&pdev->dev);
  669. /*
  670. * remove each module
  671. */
  672. rsnd_ssi_remove(pdev, priv);
  673. rsnd_adg_remove(pdev, priv);
  674. rsnd_scu_remove(pdev, priv);
  675. rsnd_dai_remove(pdev, priv);
  676. rsnd_gen_remove(pdev, priv);
  677. return 0;
  678. }
  679. static struct platform_driver rsnd_driver = {
  680. .driver = {
  681. .name = "rcar_sound",
  682. },
  683. .probe = rsnd_probe,
  684. .remove = rsnd_remove,
  685. };
  686. module_platform_driver(rsnd_driver);
  687. MODULE_LICENSE("GPL");
  688. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  689. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  690. MODULE_ALIAS("platform:rcar-pcm-audio");