core.c 18 KB

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