core.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 src
  75. * |
  76. * +- src
  77. * |
  78. * +- src[0]
  79. * +- src[1]
  80. * +- src[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. static struct rsnd_of_data rsnd_of_data_gen1 = {
  100. .flags = RSND_GEN1,
  101. };
  102. static struct rsnd_of_data rsnd_of_data_gen2 = {
  103. .flags = RSND_GEN2,
  104. };
  105. static struct of_device_id rsnd_of_match[] = {
  106. { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
  107. { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, rsnd_of_match);
  111. /*
  112. * rsnd_platform functions
  113. */
  114. #define rsnd_platform_call(priv, dai, func, param...) \
  115. (!(priv->info->func) ? 0 : \
  116. priv->info->func(param))
  117. #define rsnd_is_enable_path(io, name) \
  118. ((io)->info ? (io)->info->name : NULL)
  119. #define rsnd_info_id(priv, io, name) \
  120. ((io)->info->name - priv->info->name##_info)
  121. /*
  122. * rsnd_mod functions
  123. */
  124. char *rsnd_mod_name(struct rsnd_mod *mod)
  125. {
  126. if (!mod || !mod->ops)
  127. return "unknown";
  128. return mod->ops->name;
  129. }
  130. char *rsnd_mod_dma_name(struct rsnd_mod *mod)
  131. {
  132. if (!mod || !mod->ops)
  133. return "unknown";
  134. if (!mod->ops->dma_name)
  135. return mod->ops->name;
  136. return mod->ops->dma_name(mod);
  137. }
  138. void rsnd_mod_init(struct rsnd_priv *priv,
  139. struct rsnd_mod *mod,
  140. struct rsnd_mod_ops *ops,
  141. enum rsnd_mod_type type,
  142. int id)
  143. {
  144. mod->priv = priv;
  145. mod->id = id;
  146. mod->ops = ops;
  147. mod->type = type;
  148. }
  149. /*
  150. * rsnd_dma functions
  151. */
  152. void rsnd_dma_stop(struct rsnd_dma *dma)
  153. {
  154. dmaengine_terminate_all(dma->chan);
  155. }
  156. static void rsnd_dma_complete(void *data)
  157. {
  158. struct rsnd_dma *dma = (struct rsnd_dma *)data;
  159. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  160. struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
  161. /*
  162. * Renesas sound Gen1 needs 1 DMAC,
  163. * Gen2 needs 2 DMAC.
  164. * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
  165. * But, Audio-DMAC-peri-peri doesn't have interrupt,
  166. * and this driver is assuming that here.
  167. *
  168. * If Audio-DMAC-peri-peri has interrpt,
  169. * rsnd_dai_pointer_update() will be called twice,
  170. * ant it will breaks io->byte_pos
  171. */
  172. rsnd_dai_pointer_update(io, io->byte_per_period);
  173. }
  174. void rsnd_dma_start(struct rsnd_dma *dma)
  175. {
  176. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  177. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  178. struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
  179. struct snd_pcm_substream *substream = io->substream;
  180. struct device *dev = rsnd_priv_to_dev(priv);
  181. struct dma_async_tx_descriptor *desc;
  182. desc = dmaengine_prep_dma_cyclic(dma->chan,
  183. (dma->addr) ? dma->addr :
  184. substream->runtime->dma_addr,
  185. snd_pcm_lib_buffer_bytes(substream),
  186. snd_pcm_lib_period_bytes(substream),
  187. dma->dir,
  188. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  189. if (!desc) {
  190. dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
  191. return;
  192. }
  193. desc->callback = rsnd_dma_complete;
  194. desc->callback_param = dma;
  195. if (dmaengine_submit(desc) < 0) {
  196. dev_err(dev, "dmaengine_submit() fail\n");
  197. return;
  198. }
  199. dma_async_issue_pending(dma->chan);
  200. }
  201. int rsnd_dma_available(struct rsnd_dma *dma)
  202. {
  203. return !!dma->chan;
  204. }
  205. #define DMA_NAME_SIZE 16
  206. #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
  207. static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
  208. {
  209. if (mod)
  210. return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
  211. rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
  212. else
  213. return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
  214. }
  215. static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
  216. struct rsnd_mod *mod_to,
  217. char *dma_name)
  218. {
  219. int index = 0;
  220. index = _rsnd_dma_of_name(dma_name + index, mod_from);
  221. *(dma_name + index++) = '_';
  222. index = _rsnd_dma_of_name(dma_name + index, mod_to);
  223. }
  224. static void rsnd_dma_of_path(struct rsnd_dma *dma,
  225. int is_play,
  226. struct rsnd_mod **mod_from,
  227. struct rsnd_mod **mod_to)
  228. {
  229. struct rsnd_mod *this = rsnd_dma_to_mod(dma);
  230. struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
  231. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  232. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  233. struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
  234. struct rsnd_mod *mod[MOD_MAX];
  235. int i, index;
  236. for (i = 0; i < MOD_MAX; i++)
  237. mod[i] = NULL;
  238. /*
  239. * in play case...
  240. *
  241. * src -> dst
  242. *
  243. * mem -> SSI
  244. * mem -> SRC -> SSI
  245. * mem -> SRC -> DVC -> SSI
  246. */
  247. mod[0] = NULL; /* for "mem" */
  248. index = 1;
  249. for (i = 1; i < MOD_MAX; i++) {
  250. if (!src) {
  251. mod[i] = ssi;
  252. } else if (!dvc) {
  253. mod[i] = src;
  254. src = NULL;
  255. } else {
  256. if ((!is_play) && (this == src))
  257. this = dvc;
  258. mod[i] = (is_play) ? src : dvc;
  259. i++;
  260. mod[i] = (is_play) ? dvc : src;
  261. src = NULL;
  262. dvc = NULL;
  263. }
  264. if (mod[i] == this)
  265. index = i;
  266. if (mod[i] == ssi)
  267. break;
  268. }
  269. if (is_play) {
  270. *mod_from = mod[index - 1];
  271. *mod_to = mod[index];
  272. } else {
  273. *mod_from = mod[index];
  274. *mod_to = mod[index - 1];
  275. }
  276. }
  277. int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
  278. int is_play, int id)
  279. {
  280. struct device *dev = rsnd_priv_to_dev(priv);
  281. struct dma_slave_config cfg;
  282. struct rsnd_mod *mod_from;
  283. struct rsnd_mod *mod_to;
  284. char dma_name[DMA_NAME_SIZE];
  285. dma_cap_mask_t mask;
  286. int ret;
  287. if (dma->chan) {
  288. dev_err(dev, "it already has dma channel\n");
  289. return -EIO;
  290. }
  291. dma_cap_zero(mask);
  292. dma_cap_set(DMA_SLAVE, mask);
  293. rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
  294. rsnd_dma_of_name(mod_from, mod_to, dma_name);
  295. cfg.slave_id = id;
  296. cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  297. cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
  298. cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
  299. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  300. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  301. dev_dbg(dev, "dma : %s %pad -> %pad\n",
  302. dma_name, &cfg.src_addr, &cfg.dst_addr);
  303. dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  304. (void *)id, dev,
  305. dma_name);
  306. if (!dma->chan) {
  307. dev_err(dev, "can't get dma channel\n");
  308. return -EIO;
  309. }
  310. ret = dmaengine_slave_config(dma->chan, &cfg);
  311. if (ret < 0)
  312. goto rsnd_dma_init_err;
  313. dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
  314. dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  315. return 0;
  316. rsnd_dma_init_err:
  317. rsnd_dma_quit(priv, dma);
  318. return ret;
  319. }
  320. void rsnd_dma_quit(struct rsnd_priv *priv,
  321. struct rsnd_dma *dma)
  322. {
  323. if (dma->chan)
  324. dma_release_channel(dma->chan);
  325. dma->chan = NULL;
  326. }
  327. /*
  328. * settting function
  329. */
  330. u32 rsnd_get_adinr(struct rsnd_mod *mod)
  331. {
  332. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  333. struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
  334. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  335. struct device *dev = rsnd_priv_to_dev(priv);
  336. u32 adinr = runtime->channels;
  337. switch (runtime->sample_bits) {
  338. case 16:
  339. adinr |= (8 << 16);
  340. break;
  341. case 32:
  342. adinr |= (0 << 16);
  343. break;
  344. default:
  345. dev_warn(dev, "not supported sample bits\n");
  346. return 0;
  347. }
  348. return adinr;
  349. }
  350. /*
  351. * rsnd_dai functions
  352. */
  353. #define __rsnd_mod_call(mod, func, rdai...) \
  354. ({ \
  355. struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
  356. struct device *dev = rsnd_priv_to_dev(priv); \
  357. dev_dbg(dev, "%s [%d] %s\n", \
  358. rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
  359. (mod)->ops->func(mod, rdai); \
  360. })
  361. #define rsnd_mod_call(mod, func, rdai...) \
  362. (!(mod) ? -ENODEV : \
  363. !((mod)->ops->func) ? 0 : \
  364. __rsnd_mod_call(mod, func, rdai))
  365. #define rsnd_dai_call(fn, io, rdai...) \
  366. ({ \
  367. struct rsnd_mod *mod; \
  368. int ret = 0, i; \
  369. for (i = 0; i < RSND_MOD_MAX; i++) { \
  370. mod = (io)->mod[i]; \
  371. if (!mod) \
  372. continue; \
  373. ret = rsnd_mod_call(mod, fn, rdai); \
  374. if (ret < 0) \
  375. break; \
  376. } \
  377. ret; \
  378. })
  379. static int rsnd_dai_connect(struct rsnd_mod *mod,
  380. struct rsnd_dai_stream *io)
  381. {
  382. if (!mod)
  383. return -EIO;
  384. if (io->mod[mod->type]) {
  385. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  386. struct device *dev = rsnd_priv_to_dev(priv);
  387. dev_err(dev, "%s%d is not empty\n",
  388. rsnd_mod_name(mod),
  389. rsnd_mod_id(mod));
  390. return -EIO;
  391. }
  392. io->mod[mod->type] = mod;
  393. mod->io = io;
  394. return 0;
  395. }
  396. int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
  397. {
  398. int id = rdai - priv->rdai;
  399. if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
  400. return -EINVAL;
  401. return id;
  402. }
  403. struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
  404. {
  405. if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
  406. return NULL;
  407. return priv->rdai + id;
  408. }
  409. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  410. {
  411. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  412. return rsnd_dai_get(priv, dai->id);
  413. }
  414. int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
  415. {
  416. return &rdai->playback == io;
  417. }
  418. /*
  419. * rsnd_soc_dai functions
  420. */
  421. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
  422. {
  423. struct snd_pcm_substream *substream = io->substream;
  424. struct snd_pcm_runtime *runtime = substream->runtime;
  425. int pos = io->byte_pos + additional;
  426. pos %= (runtime->periods * io->byte_per_period);
  427. return pos;
  428. }
  429. void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
  430. {
  431. io->byte_pos += byte;
  432. if (io->byte_pos >= io->next_period_byte) {
  433. struct snd_pcm_substream *substream = io->substream;
  434. struct snd_pcm_runtime *runtime = substream->runtime;
  435. io->period_pos++;
  436. io->next_period_byte += io->byte_per_period;
  437. if (io->period_pos >= runtime->periods) {
  438. io->byte_pos = 0;
  439. io->period_pos = 0;
  440. io->next_period_byte = io->byte_per_period;
  441. }
  442. snd_pcm_period_elapsed(substream);
  443. }
  444. }
  445. static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  446. struct snd_pcm_substream *substream)
  447. {
  448. struct snd_pcm_runtime *runtime = substream->runtime;
  449. io->substream = substream;
  450. io->byte_pos = 0;
  451. io->period_pos = 0;
  452. io->byte_per_period = runtime->period_size *
  453. runtime->channels *
  454. samples_to_bytes(runtime, 1);
  455. io->next_period_byte = io->byte_per_period;
  456. return 0;
  457. }
  458. static
  459. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  460. {
  461. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  462. return rtd->cpu_dai;
  463. }
  464. static
  465. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  466. struct snd_pcm_substream *substream)
  467. {
  468. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  469. return &rdai->playback;
  470. else
  471. return &rdai->capture;
  472. }
  473. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  474. struct snd_soc_dai *dai)
  475. {
  476. struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
  477. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  478. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  479. int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
  480. int ret;
  481. unsigned long flags;
  482. rsnd_lock(priv, flags);
  483. switch (cmd) {
  484. case SNDRV_PCM_TRIGGER_START:
  485. ret = rsnd_dai_stream_init(io, substream);
  486. if (ret < 0)
  487. goto dai_trigger_end;
  488. ret = rsnd_platform_call(priv, dai, start, ssi_id);
  489. if (ret < 0)
  490. goto dai_trigger_end;
  491. ret = rsnd_dai_call(init, io, rdai);
  492. if (ret < 0)
  493. goto dai_trigger_end;
  494. ret = rsnd_dai_call(start, io, rdai);
  495. if (ret < 0)
  496. goto dai_trigger_end;
  497. break;
  498. case SNDRV_PCM_TRIGGER_STOP:
  499. ret = rsnd_dai_call(stop, io, rdai);
  500. if (ret < 0)
  501. goto dai_trigger_end;
  502. ret = rsnd_dai_call(quit, io, rdai);
  503. if (ret < 0)
  504. goto dai_trigger_end;
  505. ret = rsnd_platform_call(priv, dai, stop, ssi_id);
  506. if (ret < 0)
  507. goto dai_trigger_end;
  508. break;
  509. default:
  510. ret = -EINVAL;
  511. }
  512. dai_trigger_end:
  513. rsnd_unlock(priv, flags);
  514. return ret;
  515. }
  516. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  517. {
  518. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  519. /* set master/slave audio interface */
  520. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  521. case SND_SOC_DAIFMT_CBM_CFM:
  522. rdai->clk_master = 0;
  523. break;
  524. case SND_SOC_DAIFMT_CBS_CFS:
  525. rdai->clk_master = 1; /* codec is slave, cpu is master */
  526. break;
  527. default:
  528. return -EINVAL;
  529. }
  530. /* set format */
  531. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  532. case SND_SOC_DAIFMT_I2S:
  533. rdai->sys_delay = 0;
  534. rdai->data_alignment = 0;
  535. rdai->frm_clk_inv = 0;
  536. break;
  537. case SND_SOC_DAIFMT_LEFT_J:
  538. rdai->sys_delay = 1;
  539. rdai->data_alignment = 0;
  540. rdai->frm_clk_inv = 1;
  541. break;
  542. case SND_SOC_DAIFMT_RIGHT_J:
  543. rdai->sys_delay = 1;
  544. rdai->data_alignment = 1;
  545. rdai->frm_clk_inv = 1;
  546. break;
  547. }
  548. /* set clock inversion */
  549. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  550. case SND_SOC_DAIFMT_NB_IF:
  551. rdai->bit_clk_inv = rdai->bit_clk_inv;
  552. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  553. break;
  554. case SND_SOC_DAIFMT_IB_NF:
  555. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  556. rdai->frm_clk_inv = rdai->frm_clk_inv;
  557. break;
  558. case SND_SOC_DAIFMT_IB_IF:
  559. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  560. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  561. break;
  562. case SND_SOC_DAIFMT_NB_NF:
  563. default:
  564. break;
  565. }
  566. return 0;
  567. }
  568. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  569. .trigger = rsnd_soc_dai_trigger,
  570. .set_fmt = rsnd_soc_dai_set_fmt,
  571. };
  572. #define rsnd_path_parse(priv, io, type) \
  573. ({ \
  574. struct rsnd_mod *mod; \
  575. int ret = 0; \
  576. int id = -1; \
  577. \
  578. if (rsnd_is_enable_path(io, type)) { \
  579. id = rsnd_info_id(priv, io, type); \
  580. if (id >= 0) { \
  581. mod = rsnd_##type##_mod_get(priv, id); \
  582. ret = rsnd_dai_connect(mod, io); \
  583. } \
  584. } \
  585. ret; \
  586. })
  587. static int rsnd_path_init(struct rsnd_priv *priv,
  588. struct rsnd_dai *rdai,
  589. struct rsnd_dai_stream *io)
  590. {
  591. int ret;
  592. /*
  593. * Gen1 is created by SRU/SSI, and this SRU is base module of
  594. * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
  595. *
  596. * Easy image is..
  597. * Gen1 SRU = Gen2 SCU + SSIU + etc
  598. *
  599. * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
  600. * using fixed path.
  601. */
  602. /* SRC */
  603. ret = rsnd_path_parse(priv, io, src);
  604. if (ret < 0)
  605. return ret;
  606. /* SSI */
  607. ret = rsnd_path_parse(priv, io, ssi);
  608. if (ret < 0)
  609. return ret;
  610. /* DVC */
  611. ret = rsnd_path_parse(priv, io, dvc);
  612. if (ret < 0)
  613. return ret;
  614. return ret;
  615. }
  616. static void rsnd_of_parse_dai(struct platform_device *pdev,
  617. const struct rsnd_of_data *of_data,
  618. struct rsnd_priv *priv)
  619. {
  620. struct device_node *dai_node, *dai_np;
  621. struct device_node *ssi_node, *ssi_np;
  622. struct device_node *src_node, *src_np;
  623. struct device_node *dvc_node, *dvc_np;
  624. struct device_node *playback, *capture;
  625. struct rsnd_dai_platform_info *dai_info;
  626. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  627. struct device *dev = &pdev->dev;
  628. int nr, i;
  629. int dai_i, ssi_i, src_i, dvc_i;
  630. if (!of_data)
  631. return;
  632. dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
  633. if (!dai_node)
  634. return;
  635. nr = of_get_child_count(dai_node);
  636. if (!nr)
  637. return;
  638. dai_info = devm_kzalloc(dev,
  639. sizeof(struct rsnd_dai_platform_info) * nr,
  640. GFP_KERNEL);
  641. if (!dai_info) {
  642. dev_err(dev, "dai info allocation error\n");
  643. return;
  644. }
  645. info->dai_info_nr = nr;
  646. info->dai_info = dai_info;
  647. ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
  648. src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
  649. dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
  650. #define mod_parse(name) \
  651. if (name##_node) { \
  652. struct rsnd_##name##_platform_info *name##_info; \
  653. \
  654. name##_i = 0; \
  655. for_each_child_of_node(name##_node, name##_np) { \
  656. name##_info = info->name##_info + name##_i; \
  657. \
  658. if (name##_np == playback) \
  659. dai_info->playback.name = name##_info; \
  660. if (name##_np == capture) \
  661. dai_info->capture.name = name##_info; \
  662. \
  663. name##_i++; \
  664. } \
  665. }
  666. /*
  667. * parse all dai
  668. */
  669. dai_i = 0;
  670. for_each_child_of_node(dai_node, dai_np) {
  671. dai_info = info->dai_info + dai_i;
  672. for (i = 0;; i++) {
  673. playback = of_parse_phandle(dai_np, "playback", i);
  674. capture = of_parse_phandle(dai_np, "capture", i);
  675. if (!playback && !capture)
  676. break;
  677. mod_parse(ssi);
  678. mod_parse(src);
  679. mod_parse(dvc);
  680. of_node_put(playback);
  681. of_node_put(capture);
  682. }
  683. dai_i++;
  684. }
  685. }
  686. static int rsnd_dai_probe(struct platform_device *pdev,
  687. const struct rsnd_of_data *of_data,
  688. struct rsnd_priv *priv)
  689. {
  690. struct snd_soc_dai_driver *drv;
  691. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  692. struct rsnd_dai *rdai;
  693. struct rsnd_ssi_platform_info *pmod, *cmod;
  694. struct device *dev = rsnd_priv_to_dev(priv);
  695. int dai_nr;
  696. int i;
  697. rsnd_of_parse_dai(pdev, of_data, priv);
  698. dai_nr = info->dai_info_nr;
  699. if (!dai_nr) {
  700. dev_err(dev, "no dai\n");
  701. return -EIO;
  702. }
  703. drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
  704. rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
  705. if (!drv || !rdai) {
  706. dev_err(dev, "dai allocate failed\n");
  707. return -ENOMEM;
  708. }
  709. priv->rdai_nr = dai_nr;
  710. priv->daidrv = drv;
  711. priv->rdai = rdai;
  712. for (i = 0; i < dai_nr; i++) {
  713. rdai[i].info = &info->dai_info[i];
  714. pmod = rdai[i].info->playback.ssi;
  715. cmod = rdai[i].info->capture.ssi;
  716. /*
  717. * init rsnd_dai
  718. */
  719. snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
  720. /*
  721. * init snd_soc_dai_driver
  722. */
  723. drv[i].name = rdai[i].name;
  724. drv[i].ops = &rsnd_soc_dai_ops;
  725. if (pmod) {
  726. drv[i].playback.rates = RSND_RATES;
  727. drv[i].playback.formats = RSND_FMTS;
  728. drv[i].playback.channels_min = 2;
  729. drv[i].playback.channels_max = 2;
  730. rdai[i].playback.info = &info->dai_info[i].playback;
  731. rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
  732. }
  733. if (cmod) {
  734. drv[i].capture.rates = RSND_RATES;
  735. drv[i].capture.formats = RSND_FMTS;
  736. drv[i].capture.channels_min = 2;
  737. drv[i].capture.channels_max = 2;
  738. rdai[i].capture.info = &info->dai_info[i].capture;
  739. rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
  740. }
  741. dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
  742. pmod ? "play" : " -- ",
  743. cmod ? "capture" : " -- ");
  744. }
  745. return 0;
  746. }
  747. /*
  748. * pcm ops
  749. */
  750. static struct snd_pcm_hardware rsnd_pcm_hardware = {
  751. .info = SNDRV_PCM_INFO_INTERLEAVED |
  752. SNDRV_PCM_INFO_MMAP |
  753. SNDRV_PCM_INFO_MMAP_VALID,
  754. .buffer_bytes_max = 64 * 1024,
  755. .period_bytes_min = 32,
  756. .period_bytes_max = 8192,
  757. .periods_min = 1,
  758. .periods_max = 32,
  759. .fifo_size = 256,
  760. };
  761. static int rsnd_pcm_open(struct snd_pcm_substream *substream)
  762. {
  763. struct snd_pcm_runtime *runtime = substream->runtime;
  764. int ret = 0;
  765. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  766. ret = snd_pcm_hw_constraint_integer(runtime,
  767. SNDRV_PCM_HW_PARAM_PERIODS);
  768. return ret;
  769. }
  770. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  771. struct snd_pcm_hw_params *hw_params)
  772. {
  773. return snd_pcm_lib_malloc_pages(substream,
  774. params_buffer_bytes(hw_params));
  775. }
  776. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  777. {
  778. struct snd_pcm_runtime *runtime = substream->runtime;
  779. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  780. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  781. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  782. return bytes_to_frames(runtime, io->byte_pos);
  783. }
  784. static struct snd_pcm_ops rsnd_pcm_ops = {
  785. .open = rsnd_pcm_open,
  786. .ioctl = snd_pcm_lib_ioctl,
  787. .hw_params = rsnd_hw_params,
  788. .hw_free = snd_pcm_lib_free_pages,
  789. .pointer = rsnd_pointer,
  790. };
  791. /*
  792. * snd_soc_platform
  793. */
  794. #define PREALLOC_BUFFER (32 * 1024)
  795. #define PREALLOC_BUFFER_MAX (32 * 1024)
  796. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  797. {
  798. struct snd_soc_dai *dai = rtd->cpu_dai;
  799. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  800. int ret;
  801. ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
  802. if (ret)
  803. return ret;
  804. ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
  805. if (ret)
  806. return ret;
  807. return snd_pcm_lib_preallocate_pages_for_all(
  808. rtd->pcm,
  809. SNDRV_DMA_TYPE_DEV,
  810. rtd->card->snd_card->dev,
  811. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  812. }
  813. static void rsnd_pcm_free(struct snd_pcm *pcm)
  814. {
  815. snd_pcm_lib_preallocate_free_for_all(pcm);
  816. }
  817. static struct snd_soc_platform_driver rsnd_soc_platform = {
  818. .ops = &rsnd_pcm_ops,
  819. .pcm_new = rsnd_pcm_new,
  820. .pcm_free = rsnd_pcm_free,
  821. };
  822. static const struct snd_soc_component_driver rsnd_soc_component = {
  823. .name = "rsnd",
  824. };
  825. /*
  826. * rsnd probe
  827. */
  828. static int rsnd_probe(struct platform_device *pdev)
  829. {
  830. struct rcar_snd_info *info;
  831. struct rsnd_priv *priv;
  832. struct device *dev = &pdev->dev;
  833. struct rsnd_dai *rdai;
  834. const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
  835. const struct rsnd_of_data *of_data;
  836. int (*probe_func[])(struct platform_device *pdev,
  837. const struct rsnd_of_data *of_data,
  838. struct rsnd_priv *priv) = {
  839. rsnd_gen_probe,
  840. rsnd_ssi_probe,
  841. rsnd_src_probe,
  842. rsnd_dvc_probe,
  843. rsnd_adg_probe,
  844. rsnd_dai_probe,
  845. };
  846. int ret, i;
  847. info = NULL;
  848. of_data = NULL;
  849. if (of_id) {
  850. info = devm_kzalloc(&pdev->dev,
  851. sizeof(struct rcar_snd_info), GFP_KERNEL);
  852. of_data = of_id->data;
  853. } else {
  854. info = pdev->dev.platform_data;
  855. }
  856. if (!info) {
  857. dev_err(dev, "driver needs R-Car sound information\n");
  858. return -ENODEV;
  859. }
  860. /*
  861. * init priv data
  862. */
  863. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  864. if (!priv) {
  865. dev_err(dev, "priv allocate failed\n");
  866. return -ENODEV;
  867. }
  868. priv->pdev = pdev;
  869. priv->info = info;
  870. spin_lock_init(&priv->lock);
  871. /*
  872. * init each module
  873. */
  874. for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
  875. ret = probe_func[i](pdev, of_data, priv);
  876. if (ret)
  877. return ret;
  878. }
  879. for_each_rsnd_dai(rdai, priv, i) {
  880. ret = rsnd_dai_call(probe, &rdai->playback, rdai);
  881. if (ret)
  882. goto exit_snd_probe;
  883. ret = rsnd_dai_call(probe, &rdai->capture, rdai);
  884. if (ret)
  885. goto exit_snd_probe;
  886. }
  887. /*
  888. * asoc register
  889. */
  890. ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
  891. if (ret < 0) {
  892. dev_err(dev, "cannot snd soc register\n");
  893. return ret;
  894. }
  895. ret = snd_soc_register_component(dev, &rsnd_soc_component,
  896. priv->daidrv, rsnd_rdai_nr(priv));
  897. if (ret < 0) {
  898. dev_err(dev, "cannot snd dai register\n");
  899. goto exit_snd_soc;
  900. }
  901. dev_set_drvdata(dev, priv);
  902. pm_runtime_enable(dev);
  903. dev_info(dev, "probed\n");
  904. return ret;
  905. exit_snd_soc:
  906. snd_soc_unregister_platform(dev);
  907. exit_snd_probe:
  908. for_each_rsnd_dai(rdai, priv, i) {
  909. rsnd_dai_call(remove, &rdai->playback, rdai);
  910. rsnd_dai_call(remove, &rdai->capture, rdai);
  911. }
  912. return ret;
  913. }
  914. static int rsnd_remove(struct platform_device *pdev)
  915. {
  916. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  917. struct rsnd_dai *rdai;
  918. int ret = 0, i;
  919. pm_runtime_disable(&pdev->dev);
  920. for_each_rsnd_dai(rdai, priv, i) {
  921. ret |= rsnd_dai_call(remove, &rdai->playback, rdai);
  922. ret |= rsnd_dai_call(remove, &rdai->capture, rdai);
  923. }
  924. return ret;
  925. }
  926. static struct platform_driver rsnd_driver = {
  927. .driver = {
  928. .name = "rcar_sound",
  929. .of_match_table = rsnd_of_match,
  930. },
  931. .probe = rsnd_probe,
  932. .remove = rsnd_remove,
  933. };
  934. module_platform_driver(rsnd_driver);
  935. MODULE_LICENSE("GPL");
  936. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  937. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  938. MODULE_ALIAS("platform:rcar-pcm-audio");