core.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 "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. static const struct of_device_id rsnd_of_match[] = {
  99. { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
  100. { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
  101. { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 }, /* gen2 compatible */
  102. {},
  103. };
  104. MODULE_DEVICE_TABLE(of, rsnd_of_match);
  105. /*
  106. * rsnd_mod functions
  107. */
  108. #ifdef DEBUG
  109. void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
  110. {
  111. if (mod->type != type) {
  112. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  113. struct device *dev = rsnd_priv_to_dev(priv);
  114. dev_warn(dev, "%s[%d] is not your expected module\n",
  115. rsnd_mod_name(mod), rsnd_mod_id(mod));
  116. }
  117. }
  118. #endif
  119. char *rsnd_mod_name(struct rsnd_mod *mod)
  120. {
  121. if (!mod || !mod->ops)
  122. return "unknown";
  123. return mod->ops->name;
  124. }
  125. struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
  126. struct rsnd_mod *mod)
  127. {
  128. if (!mod || !mod->ops || !mod->ops->dma_req)
  129. return NULL;
  130. return mod->ops->dma_req(io, mod);
  131. }
  132. u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
  133. struct rsnd_mod *mod,
  134. enum rsnd_mod_type type)
  135. {
  136. return &mod->status;
  137. }
  138. int rsnd_mod_init(struct rsnd_priv *priv,
  139. struct rsnd_mod *mod,
  140. struct rsnd_mod_ops *ops,
  141. struct clk *clk,
  142. u32* (*get_status)(struct rsnd_dai_stream *io,
  143. struct rsnd_mod *mod,
  144. enum rsnd_mod_type type),
  145. enum rsnd_mod_type type,
  146. int id)
  147. {
  148. int ret = clk_prepare(clk);
  149. if (ret)
  150. return ret;
  151. mod->id = id;
  152. mod->ops = ops;
  153. mod->type = type;
  154. mod->clk = clk;
  155. mod->priv = priv;
  156. mod->get_status = get_status;
  157. return ret;
  158. }
  159. void rsnd_mod_quit(struct rsnd_mod *mod)
  160. {
  161. if (mod->clk)
  162. clk_unprepare(mod->clk);
  163. mod->clk = NULL;
  164. }
  165. void rsnd_mod_interrupt(struct rsnd_mod *mod,
  166. void (*callback)(struct rsnd_mod *mod,
  167. struct rsnd_dai_stream *io))
  168. {
  169. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  170. struct rsnd_dai_stream *io;
  171. struct rsnd_dai *rdai;
  172. int i;
  173. for_each_rsnd_dai(rdai, priv, i) {
  174. io = &rdai->playback;
  175. if (mod == io->mod[mod->type])
  176. callback(mod, io);
  177. io = &rdai->capture;
  178. if (mod == io->mod[mod->type])
  179. callback(mod, io);
  180. }
  181. }
  182. int rsnd_io_is_working(struct rsnd_dai_stream *io)
  183. {
  184. /* see rsnd_dai_stream_init/quit() */
  185. return !!io->substream;
  186. }
  187. void rsnd_set_slot(struct rsnd_dai *rdai,
  188. int slots, int num)
  189. {
  190. rdai->slots = slots;
  191. rdai->slots_num = num;
  192. }
  193. int rsnd_get_slot(struct rsnd_dai_stream *io)
  194. {
  195. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  196. return rdai->slots;
  197. }
  198. int rsnd_get_slot_num(struct rsnd_dai_stream *io)
  199. {
  200. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  201. return rdai->slots_num;
  202. }
  203. int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
  204. {
  205. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  206. return runtime->channels;
  207. }
  208. int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
  209. {
  210. int chan = rsnd_runtime_channel_original(io);
  211. struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
  212. if (ctu_mod) {
  213. u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
  214. if (converted_chan)
  215. return converted_chan;
  216. }
  217. return chan;
  218. }
  219. int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
  220. {
  221. int chan = rsnd_io_is_play(io) ?
  222. rsnd_runtime_channel_after_ctu(io) :
  223. rsnd_runtime_channel_original(io);
  224. /* Use Multi SSI */
  225. if (rsnd_runtime_is_ssi_multi(io))
  226. chan /= rsnd_get_slot_num(io);
  227. /* TDM Extend Mode needs 8ch */
  228. if (chan == 6)
  229. chan = 8;
  230. return chan;
  231. }
  232. int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
  233. {
  234. int slots = rsnd_get_slot_num(io);
  235. int chan = rsnd_io_is_play(io) ?
  236. rsnd_runtime_channel_after_ctu(io) :
  237. rsnd_runtime_channel_original(io);
  238. return (chan >= 6) && (slots > 1);
  239. }
  240. int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
  241. {
  242. return rsnd_runtime_channel_for_ssi(io) >= 6;
  243. }
  244. /*
  245. * ADINR function
  246. */
  247. u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
  248. {
  249. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  250. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  251. struct device *dev = rsnd_priv_to_dev(priv);
  252. switch (runtime->sample_bits) {
  253. case 16:
  254. return 8 << 16;
  255. case 32:
  256. return 0 << 16;
  257. }
  258. dev_warn(dev, "not supported sample bits\n");
  259. return 0;
  260. }
  261. /*
  262. * DALIGN function
  263. */
  264. u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
  265. {
  266. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  267. struct rsnd_mod *target;
  268. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  269. u32 val = 0x76543210;
  270. u32 mask = ~0;
  271. if (rsnd_io_is_play(io)) {
  272. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  273. target = src ? src : ssi;
  274. } else {
  275. struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
  276. target = cmd ? cmd : ssi;
  277. }
  278. mask <<= runtime->channels * 4;
  279. val = val & mask;
  280. switch (runtime->sample_bits) {
  281. case 16:
  282. val |= 0x67452301 & ~mask;
  283. break;
  284. case 32:
  285. val |= 0x76543210 & ~mask;
  286. break;
  287. }
  288. /*
  289. * exchange channeles on SRC if possible,
  290. * otherwise, R/L volume settings on DVC
  291. * changes inverted channels
  292. */
  293. if (mod == target)
  294. return val;
  295. else
  296. return 0x76543210;
  297. }
  298. /*
  299. * rsnd_dai functions
  300. */
  301. #define rsnd_mod_call(idx, io, func, param...) \
  302. ({ \
  303. struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
  304. struct rsnd_mod *mod = (io)->mod[idx]; \
  305. struct device *dev = rsnd_priv_to_dev(priv); \
  306. u32 *status = mod->get_status(io, mod, idx); \
  307. u32 mask = 0xF << __rsnd_mod_shift_##func; \
  308. u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
  309. u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
  310. int ret = 0; \
  311. int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
  312. if (add == 0xF) \
  313. call = 0; \
  314. else \
  315. *status = (*status & ~mask) + \
  316. (add << __rsnd_mod_shift_##func); \
  317. dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
  318. rsnd_mod_name(mod), rsnd_mod_id(mod), \
  319. *status, call ? #func : ""); \
  320. if (call) \
  321. ret = (mod)->ops->func(mod, io, param); \
  322. if (ret) \
  323. dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n", \
  324. rsnd_mod_name(mod), rsnd_mod_id(mod), ret); \
  325. ret; \
  326. })
  327. static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
  328. {
  329. /* CAPTURE */
  330. RSND_MOD_AUDMAPP,
  331. RSND_MOD_AUDMA,
  332. RSND_MOD_DVC,
  333. RSND_MOD_MIX,
  334. RSND_MOD_CTU,
  335. RSND_MOD_CMD,
  336. RSND_MOD_SRC,
  337. RSND_MOD_SSIU,
  338. RSND_MOD_SSIM3,
  339. RSND_MOD_SSIM2,
  340. RSND_MOD_SSIM1,
  341. RSND_MOD_SSIP,
  342. RSND_MOD_SSI,
  343. }, {
  344. /* PLAYBACK */
  345. RSND_MOD_AUDMAPP,
  346. RSND_MOD_AUDMA,
  347. RSND_MOD_SSIM3,
  348. RSND_MOD_SSIM2,
  349. RSND_MOD_SSIM1,
  350. RSND_MOD_SSIP,
  351. RSND_MOD_SSI,
  352. RSND_MOD_SSIU,
  353. RSND_MOD_DVC,
  354. RSND_MOD_MIX,
  355. RSND_MOD_CTU,
  356. RSND_MOD_CMD,
  357. RSND_MOD_SRC,
  358. },
  359. };
  360. #define rsnd_dai_call(fn, io, param...) \
  361. ({ \
  362. struct rsnd_mod *mod; \
  363. int type, is_play = rsnd_io_is_play(io); \
  364. int ret = 0, i; \
  365. for (i = 0; i < RSND_MOD_MAX; i++) { \
  366. type = rsnd_mod_sequence[is_play][i]; \
  367. mod = (io)->mod[type]; \
  368. if (!mod) \
  369. continue; \
  370. ret |= rsnd_mod_call(type, io, fn, param); \
  371. } \
  372. ret; \
  373. })
  374. int rsnd_dai_connect(struct rsnd_mod *mod,
  375. struct rsnd_dai_stream *io,
  376. enum rsnd_mod_type type)
  377. {
  378. struct rsnd_priv *priv;
  379. struct device *dev;
  380. if (!mod)
  381. return -EIO;
  382. if (io->mod[type] == mod)
  383. return 0;
  384. if (io->mod[type])
  385. return -EINVAL;
  386. priv = rsnd_mod_to_priv(mod);
  387. dev = rsnd_priv_to_dev(priv);
  388. io->mod[type] = mod;
  389. dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
  390. rsnd_mod_name(mod), rsnd_mod_id(mod),
  391. rsnd_io_is_play(io) ? "Playback" : "Capture");
  392. return 0;
  393. }
  394. static void rsnd_dai_disconnect(struct rsnd_mod *mod,
  395. struct rsnd_dai_stream *io,
  396. enum rsnd_mod_type type)
  397. {
  398. io->mod[type] = NULL;
  399. }
  400. struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
  401. {
  402. if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
  403. return NULL;
  404. return priv->rdai + id;
  405. }
  406. #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
  407. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  408. {
  409. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  410. return rsnd_rdai_get(priv, dai->id);
  411. }
  412. /*
  413. * rsnd_soc_dai functions
  414. */
  415. int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
  416. {
  417. struct snd_pcm_substream *substream = io->substream;
  418. struct snd_pcm_runtime *runtime = substream->runtime;
  419. int pos = io->byte_pos + additional;
  420. pos %= (runtime->periods * io->byte_per_period);
  421. return pos;
  422. }
  423. bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
  424. {
  425. io->byte_pos += byte;
  426. if (io->byte_pos >= io->next_period_byte) {
  427. struct snd_pcm_substream *substream = io->substream;
  428. struct snd_pcm_runtime *runtime = substream->runtime;
  429. io->period_pos++;
  430. io->next_period_byte += io->byte_per_period;
  431. if (io->period_pos >= runtime->periods) {
  432. io->byte_pos = 0;
  433. io->period_pos = 0;
  434. io->next_period_byte = io->byte_per_period;
  435. }
  436. return true;
  437. }
  438. return false;
  439. }
  440. void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
  441. {
  442. struct snd_pcm_substream *substream = io->substream;
  443. /*
  444. * this function should be called...
  445. *
  446. * - if rsnd_dai_pointer_update() returns true
  447. * - without spin lock
  448. */
  449. snd_pcm_period_elapsed(substream);
  450. }
  451. static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  452. struct snd_pcm_substream *substream)
  453. {
  454. struct snd_pcm_runtime *runtime = substream->runtime;
  455. io->substream = substream;
  456. io->byte_pos = 0;
  457. io->period_pos = 0;
  458. io->byte_per_period = runtime->period_size *
  459. runtime->channels *
  460. samples_to_bytes(runtime, 1);
  461. io->next_period_byte = io->byte_per_period;
  462. }
  463. static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
  464. {
  465. io->substream = NULL;
  466. }
  467. static
  468. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  469. {
  470. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  471. return rtd->cpu_dai;
  472. }
  473. static
  474. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  475. struct snd_pcm_substream *substream)
  476. {
  477. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  478. return &rdai->playback;
  479. else
  480. return &rdai->capture;
  481. }
  482. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  483. struct snd_soc_dai *dai)
  484. {
  485. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  486. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  487. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  488. int ret;
  489. unsigned long flags;
  490. spin_lock_irqsave(&priv->lock, flags);
  491. switch (cmd) {
  492. case SNDRV_PCM_TRIGGER_START:
  493. case SNDRV_PCM_TRIGGER_RESUME:
  494. rsnd_dai_stream_init(io, substream);
  495. ret = rsnd_dai_call(init, io, priv);
  496. if (ret < 0)
  497. goto dai_trigger_end;
  498. ret = rsnd_dai_call(start, io, priv);
  499. if (ret < 0)
  500. goto dai_trigger_end;
  501. ret = rsnd_dai_call(irq, io, priv, 1);
  502. if (ret < 0)
  503. goto dai_trigger_end;
  504. break;
  505. case SNDRV_PCM_TRIGGER_STOP:
  506. case SNDRV_PCM_TRIGGER_SUSPEND:
  507. ret = rsnd_dai_call(irq, io, priv, 0);
  508. ret |= rsnd_dai_call(stop, io, priv);
  509. ret |= rsnd_dai_call(quit, io, priv);
  510. rsnd_dai_stream_quit(io);
  511. break;
  512. default:
  513. ret = -EINVAL;
  514. }
  515. dai_trigger_end:
  516. spin_unlock_irqrestore(&priv->lock, flags);
  517. return ret;
  518. }
  519. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  520. {
  521. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  522. /* set master/slave audio interface */
  523. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  524. case SND_SOC_DAIFMT_CBM_CFM:
  525. rdai->clk_master = 0;
  526. break;
  527. case SND_SOC_DAIFMT_CBS_CFS:
  528. rdai->clk_master = 1; /* codec is slave, cpu is master */
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. /* set format */
  534. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  535. case SND_SOC_DAIFMT_I2S:
  536. rdai->sys_delay = 0;
  537. rdai->data_alignment = 0;
  538. rdai->frm_clk_inv = 0;
  539. break;
  540. case SND_SOC_DAIFMT_LEFT_J:
  541. rdai->sys_delay = 1;
  542. rdai->data_alignment = 0;
  543. rdai->frm_clk_inv = 1;
  544. break;
  545. case SND_SOC_DAIFMT_RIGHT_J:
  546. rdai->sys_delay = 1;
  547. rdai->data_alignment = 1;
  548. rdai->frm_clk_inv = 1;
  549. break;
  550. }
  551. /* set clock inversion */
  552. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  553. case SND_SOC_DAIFMT_NB_IF:
  554. rdai->bit_clk_inv = rdai->bit_clk_inv;
  555. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  556. break;
  557. case SND_SOC_DAIFMT_IB_NF:
  558. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  559. rdai->frm_clk_inv = rdai->frm_clk_inv;
  560. break;
  561. case SND_SOC_DAIFMT_IB_IF:
  562. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  563. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  564. break;
  565. case SND_SOC_DAIFMT_NB_NF:
  566. default:
  567. break;
  568. }
  569. return 0;
  570. }
  571. static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
  572. u32 tx_mask, u32 rx_mask,
  573. int slots, int slot_width)
  574. {
  575. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  576. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  577. struct device *dev = rsnd_priv_to_dev(priv);
  578. switch (slots) {
  579. case 6:
  580. /* TDM Extend Mode */
  581. rsnd_set_slot(rdai, slots, 1);
  582. break;
  583. default:
  584. dev_err(dev, "unsupported TDM slots (%d)\n", slots);
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  590. .trigger = rsnd_soc_dai_trigger,
  591. .set_fmt = rsnd_soc_dai_set_fmt,
  592. .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
  593. };
  594. void rsnd_parse_connect_common(struct rsnd_dai *rdai,
  595. struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
  596. struct device_node *node,
  597. struct device_node *playback,
  598. struct device_node *capture)
  599. {
  600. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  601. struct device_node *np;
  602. struct rsnd_mod *mod;
  603. int i;
  604. if (!node)
  605. return;
  606. i = 0;
  607. for_each_child_of_node(node, np) {
  608. mod = mod_get(priv, i);
  609. if (np == playback)
  610. rsnd_dai_connect(mod, &rdai->playback, mod->type);
  611. if (np == capture)
  612. rsnd_dai_connect(mod, &rdai->capture, mod->type);
  613. i++;
  614. }
  615. of_node_put(node);
  616. }
  617. static int rsnd_dai_probe(struct rsnd_priv *priv)
  618. {
  619. struct device_node *dai_node;
  620. struct device_node *dai_np;
  621. struct device_node *playback, *capture;
  622. struct rsnd_dai_stream *io_playback;
  623. struct rsnd_dai_stream *io_capture;
  624. struct snd_soc_dai_driver *rdrv, *drv;
  625. struct rsnd_dai *rdai;
  626. struct device *dev = rsnd_priv_to_dev(priv);
  627. int nr, dai_i, io_i;
  628. int ret;
  629. dai_node = rsnd_dai_of_node(priv);
  630. nr = of_get_child_count(dai_node);
  631. if (!nr) {
  632. ret = -EINVAL;
  633. goto rsnd_dai_probe_done;
  634. }
  635. rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
  636. rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
  637. if (!rdrv || !rdai) {
  638. ret = -ENOMEM;
  639. goto rsnd_dai_probe_done;
  640. }
  641. priv->rdai_nr = nr;
  642. priv->daidrv = rdrv;
  643. priv->rdai = rdai;
  644. /*
  645. * parse all dai
  646. */
  647. dai_i = 0;
  648. for_each_child_of_node(dai_node, dai_np) {
  649. rdai = rsnd_rdai_get(priv, dai_i);
  650. drv = rdrv + dai_i;
  651. io_playback = &rdai->playback;
  652. io_capture = &rdai->capture;
  653. snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
  654. rdai->priv = priv;
  655. drv->name = rdai->name;
  656. drv->ops = &rsnd_soc_dai_ops;
  657. snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
  658. "DAI%d Playback", dai_i);
  659. drv->playback.rates = RSND_RATES;
  660. drv->playback.formats = RSND_FMTS;
  661. drv->playback.channels_min = 2;
  662. drv->playback.channels_max = 6;
  663. drv->playback.stream_name = rdai->playback.name;
  664. snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
  665. "DAI%d Capture", dai_i);
  666. drv->capture.rates = RSND_RATES;
  667. drv->capture.formats = RSND_FMTS;
  668. drv->capture.channels_min = 2;
  669. drv->capture.channels_max = 6;
  670. drv->capture.stream_name = rdai->capture.name;
  671. rdai->playback.rdai = rdai;
  672. rdai->capture.rdai = rdai;
  673. rsnd_set_slot(rdai, 2, 1); /* default */
  674. for (io_i = 0;; io_i++) {
  675. playback = of_parse_phandle(dai_np, "playback", io_i);
  676. capture = of_parse_phandle(dai_np, "capture", io_i);
  677. if (!playback && !capture)
  678. break;
  679. rsnd_parse_connect_ssi(rdai, playback, capture);
  680. rsnd_parse_connect_src(rdai, playback, capture);
  681. rsnd_parse_connect_ctu(rdai, playback, capture);
  682. rsnd_parse_connect_mix(rdai, playback, capture);
  683. rsnd_parse_connect_dvc(rdai, playback, capture);
  684. of_node_put(playback);
  685. of_node_put(capture);
  686. }
  687. dai_i++;
  688. dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
  689. rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
  690. rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
  691. }
  692. ret = 0;
  693. rsnd_dai_probe_done:
  694. of_node_put(dai_node);
  695. return ret;
  696. }
  697. /*
  698. * pcm ops
  699. */
  700. static struct snd_pcm_hardware rsnd_pcm_hardware = {
  701. .info = SNDRV_PCM_INFO_INTERLEAVED |
  702. SNDRV_PCM_INFO_MMAP |
  703. SNDRV_PCM_INFO_MMAP_VALID,
  704. .buffer_bytes_max = 64 * 1024,
  705. .period_bytes_min = 32,
  706. .period_bytes_max = 8192,
  707. .periods_min = 1,
  708. .periods_max = 32,
  709. .fifo_size = 256,
  710. };
  711. static int rsnd_pcm_open(struct snd_pcm_substream *substream)
  712. {
  713. struct snd_pcm_runtime *runtime = substream->runtime;
  714. int ret = 0;
  715. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  716. ret = snd_pcm_hw_constraint_integer(runtime,
  717. SNDRV_PCM_HW_PARAM_PERIODS);
  718. return ret;
  719. }
  720. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  721. struct snd_pcm_hw_params *hw_params)
  722. {
  723. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  724. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  725. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  726. int ret;
  727. ret = rsnd_dai_call(hw_params, io, substream, hw_params);
  728. if (ret)
  729. return ret;
  730. return snd_pcm_lib_malloc_pages(substream,
  731. params_buffer_bytes(hw_params));
  732. }
  733. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  734. {
  735. struct snd_pcm_runtime *runtime = substream->runtime;
  736. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  737. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  738. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  739. return bytes_to_frames(runtime, io->byte_pos);
  740. }
  741. static struct snd_pcm_ops rsnd_pcm_ops = {
  742. .open = rsnd_pcm_open,
  743. .ioctl = snd_pcm_lib_ioctl,
  744. .hw_params = rsnd_hw_params,
  745. .hw_free = snd_pcm_lib_free_pages,
  746. .pointer = rsnd_pointer,
  747. };
  748. /*
  749. * snd_kcontrol
  750. */
  751. #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
  752. static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
  753. struct snd_ctl_elem_info *uinfo)
  754. {
  755. struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
  756. if (cfg->texts) {
  757. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  758. uinfo->count = cfg->size;
  759. uinfo->value.enumerated.items = cfg->max;
  760. if (uinfo->value.enumerated.item >= cfg->max)
  761. uinfo->value.enumerated.item = cfg->max - 1;
  762. strlcpy(uinfo->value.enumerated.name,
  763. cfg->texts[uinfo->value.enumerated.item],
  764. sizeof(uinfo->value.enumerated.name));
  765. } else {
  766. uinfo->count = cfg->size;
  767. uinfo->value.integer.min = 0;
  768. uinfo->value.integer.max = cfg->max;
  769. uinfo->type = (cfg->max == 1) ?
  770. SNDRV_CTL_ELEM_TYPE_BOOLEAN :
  771. SNDRV_CTL_ELEM_TYPE_INTEGER;
  772. }
  773. return 0;
  774. }
  775. static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
  776. struct snd_ctl_elem_value *uc)
  777. {
  778. struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
  779. int i;
  780. for (i = 0; i < cfg->size; i++)
  781. if (cfg->texts)
  782. uc->value.enumerated.item[i] = cfg->val[i];
  783. else
  784. uc->value.integer.value[i] = cfg->val[i];
  785. return 0;
  786. }
  787. static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
  788. struct snd_ctl_elem_value *uc)
  789. {
  790. struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
  791. struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
  792. int i, change = 0;
  793. for (i = 0; i < cfg->size; i++) {
  794. if (cfg->texts) {
  795. change |= (uc->value.enumerated.item[i] != cfg->val[i]);
  796. cfg->val[i] = uc->value.enumerated.item[i];
  797. } else {
  798. change |= (uc->value.integer.value[i] != cfg->val[i]);
  799. cfg->val[i] = uc->value.integer.value[i];
  800. }
  801. }
  802. if (change && cfg->update)
  803. cfg->update(cfg->io, mod);
  804. return change;
  805. }
  806. static int __rsnd_kctrl_new(struct rsnd_mod *mod,
  807. struct rsnd_dai_stream *io,
  808. struct snd_soc_pcm_runtime *rtd,
  809. const unsigned char *name,
  810. struct rsnd_kctrl_cfg *cfg,
  811. void (*update)(struct rsnd_dai_stream *io,
  812. struct rsnd_mod *mod))
  813. {
  814. struct snd_card *card = rtd->card->snd_card;
  815. struct snd_kcontrol *kctrl;
  816. struct snd_kcontrol_new knew = {
  817. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  818. .name = name,
  819. .info = rsnd_kctrl_info,
  820. .index = rtd->num,
  821. .get = rsnd_kctrl_get,
  822. .put = rsnd_kctrl_put,
  823. .private_value = (unsigned long)cfg,
  824. };
  825. int ret;
  826. kctrl = snd_ctl_new1(&knew, mod);
  827. if (!kctrl)
  828. return -ENOMEM;
  829. ret = snd_ctl_add(card, kctrl);
  830. if (ret < 0)
  831. return ret;
  832. cfg->update = update;
  833. cfg->card = card;
  834. cfg->kctrl = kctrl;
  835. cfg->io = io;
  836. return 0;
  837. }
  838. void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
  839. {
  840. snd_ctl_remove(cfg->card, cfg->kctrl);
  841. }
  842. int rsnd_kctrl_new_m(struct rsnd_mod *mod,
  843. struct rsnd_dai_stream *io,
  844. struct snd_soc_pcm_runtime *rtd,
  845. const unsigned char *name,
  846. void (*update)(struct rsnd_dai_stream *io,
  847. struct rsnd_mod *mod),
  848. struct rsnd_kctrl_cfg_m *_cfg,
  849. int ch_size,
  850. u32 max)
  851. {
  852. if (ch_size > RSND_MAX_CHANNELS)
  853. return -EINVAL;
  854. _cfg->cfg.max = max;
  855. _cfg->cfg.size = ch_size;
  856. _cfg->cfg.val = _cfg->val;
  857. return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
  858. }
  859. int rsnd_kctrl_new_s(struct rsnd_mod *mod,
  860. struct rsnd_dai_stream *io,
  861. struct snd_soc_pcm_runtime *rtd,
  862. const unsigned char *name,
  863. void (*update)(struct rsnd_dai_stream *io,
  864. struct rsnd_mod *mod),
  865. struct rsnd_kctrl_cfg_s *_cfg,
  866. u32 max)
  867. {
  868. _cfg->cfg.max = max;
  869. _cfg->cfg.size = 1;
  870. _cfg->cfg.val = &_cfg->val;
  871. return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
  872. }
  873. int rsnd_kctrl_new_e(struct rsnd_mod *mod,
  874. struct rsnd_dai_stream *io,
  875. struct snd_soc_pcm_runtime *rtd,
  876. const unsigned char *name,
  877. struct rsnd_kctrl_cfg_s *_cfg,
  878. void (*update)(struct rsnd_dai_stream *io,
  879. struct rsnd_mod *mod),
  880. const char * const *texts,
  881. u32 max)
  882. {
  883. _cfg->cfg.max = max;
  884. _cfg->cfg.size = 1;
  885. _cfg->cfg.val = &_cfg->val;
  886. _cfg->cfg.texts = texts;
  887. return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
  888. }
  889. /*
  890. * snd_soc_platform
  891. */
  892. #define PREALLOC_BUFFER (32 * 1024)
  893. #define PREALLOC_BUFFER_MAX (32 * 1024)
  894. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  895. {
  896. struct snd_soc_dai *dai = rtd->cpu_dai;
  897. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  898. int ret;
  899. ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
  900. if (ret)
  901. return ret;
  902. ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
  903. if (ret)
  904. return ret;
  905. return snd_pcm_lib_preallocate_pages_for_all(
  906. rtd->pcm,
  907. SNDRV_DMA_TYPE_DEV,
  908. rtd->card->snd_card->dev,
  909. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  910. }
  911. static struct snd_soc_platform_driver rsnd_soc_platform = {
  912. .ops = &rsnd_pcm_ops,
  913. .pcm_new = rsnd_pcm_new,
  914. };
  915. static const struct snd_soc_component_driver rsnd_soc_component = {
  916. .name = "rsnd",
  917. };
  918. static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
  919. struct rsnd_dai_stream *io)
  920. {
  921. int ret;
  922. ret = rsnd_dai_call(probe, io, priv);
  923. if (ret == -EAGAIN) {
  924. struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
  925. int i;
  926. /*
  927. * Fallback to PIO mode
  928. */
  929. /*
  930. * call "remove" for SSI/SRC/DVC
  931. * SSI will be switch to PIO mode if it was DMA mode
  932. * see
  933. * rsnd_dma_init()
  934. * rsnd_ssi_fallback()
  935. */
  936. rsnd_dai_call(remove, io, priv);
  937. /*
  938. * remove all mod from io
  939. * and, re connect ssi
  940. */
  941. for (i = 0; i < RSND_MOD_MAX; i++)
  942. rsnd_dai_disconnect((io)->mod[i], io, i);
  943. rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
  944. /*
  945. * fallback
  946. */
  947. rsnd_dai_call(fallback, io, priv);
  948. /*
  949. * retry to "probe".
  950. * DAI has SSI which is PIO mode only now.
  951. */
  952. ret = rsnd_dai_call(probe, io, priv);
  953. }
  954. return ret;
  955. }
  956. /*
  957. * rsnd probe
  958. */
  959. static int rsnd_probe(struct platform_device *pdev)
  960. {
  961. struct rsnd_priv *priv;
  962. struct device *dev = &pdev->dev;
  963. struct rsnd_dai *rdai;
  964. int (*probe_func[])(struct rsnd_priv *priv) = {
  965. rsnd_gen_probe,
  966. rsnd_dma_probe,
  967. rsnd_ssi_probe,
  968. rsnd_ssiu_probe,
  969. rsnd_src_probe,
  970. rsnd_ctu_probe,
  971. rsnd_mix_probe,
  972. rsnd_dvc_probe,
  973. rsnd_cmd_probe,
  974. rsnd_adg_probe,
  975. rsnd_dai_probe,
  976. };
  977. int ret, i;
  978. /*
  979. * init priv data
  980. */
  981. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  982. if (!priv) {
  983. dev_err(dev, "priv allocate failed\n");
  984. return -ENODEV;
  985. }
  986. priv->pdev = pdev;
  987. priv->flags = (unsigned long)of_device_get_match_data(dev);
  988. spin_lock_init(&priv->lock);
  989. /*
  990. * init each module
  991. */
  992. for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
  993. ret = probe_func[i](priv);
  994. if (ret)
  995. return ret;
  996. }
  997. for_each_rsnd_dai(rdai, priv, i) {
  998. ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
  999. if (ret)
  1000. goto exit_snd_probe;
  1001. ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
  1002. if (ret)
  1003. goto exit_snd_probe;
  1004. }
  1005. dev_set_drvdata(dev, priv);
  1006. /*
  1007. * asoc register
  1008. */
  1009. ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
  1010. if (ret < 0) {
  1011. dev_err(dev, "cannot snd soc register\n");
  1012. return ret;
  1013. }
  1014. ret = snd_soc_register_component(dev, &rsnd_soc_component,
  1015. priv->daidrv, rsnd_rdai_nr(priv));
  1016. if (ret < 0) {
  1017. dev_err(dev, "cannot snd dai register\n");
  1018. goto exit_snd_soc;
  1019. }
  1020. pm_runtime_enable(dev);
  1021. dev_info(dev, "probed\n");
  1022. return ret;
  1023. exit_snd_soc:
  1024. snd_soc_unregister_platform(dev);
  1025. exit_snd_probe:
  1026. for_each_rsnd_dai(rdai, priv, i) {
  1027. rsnd_dai_call(remove, &rdai->playback, priv);
  1028. rsnd_dai_call(remove, &rdai->capture, priv);
  1029. }
  1030. return ret;
  1031. }
  1032. static int rsnd_remove(struct platform_device *pdev)
  1033. {
  1034. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  1035. struct rsnd_dai *rdai;
  1036. void (*remove_func[])(struct rsnd_priv *priv) = {
  1037. rsnd_ssi_remove,
  1038. rsnd_ssiu_remove,
  1039. rsnd_src_remove,
  1040. rsnd_ctu_remove,
  1041. rsnd_mix_remove,
  1042. rsnd_dvc_remove,
  1043. rsnd_cmd_remove,
  1044. rsnd_adg_remove,
  1045. };
  1046. int ret = 0, i;
  1047. pm_runtime_disable(&pdev->dev);
  1048. for_each_rsnd_dai(rdai, priv, i) {
  1049. ret |= rsnd_dai_call(remove, &rdai->playback, priv);
  1050. ret |= rsnd_dai_call(remove, &rdai->capture, priv);
  1051. }
  1052. for (i = 0; i < ARRAY_SIZE(remove_func); i++)
  1053. remove_func[i](priv);
  1054. snd_soc_unregister_component(&pdev->dev);
  1055. snd_soc_unregister_platform(&pdev->dev);
  1056. return ret;
  1057. }
  1058. static struct platform_driver rsnd_driver = {
  1059. .driver = {
  1060. .name = "rcar_sound",
  1061. .of_match_table = rsnd_of_match,
  1062. },
  1063. .probe = rsnd_probe,
  1064. .remove = rsnd_remove,
  1065. };
  1066. module_platform_driver(rsnd_driver);
  1067. MODULE_LICENSE("GPL");
  1068. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  1069. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  1070. MODULE_ALIAS("platform:rcar-pcm-audio");