core.c 17 KB

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