core.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Renesas R-Car SRU/SCU/SSIU/SSI support
  4. //
  5. // Copyright (C) 2013 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. // Based on fsi.c
  9. // Kuninori Morimoto <morimoto.kuninori@renesas.com>
  10. /*
  11. * Renesas R-Car sound device structure
  12. *
  13. * Gen1
  14. *
  15. * SRU : Sound Routing Unit
  16. * - SRC : Sampling Rate Converter
  17. * - CMD
  18. * - CTU : Channel Count Conversion Unit
  19. * - MIX : Mixer
  20. * - DVC : Digital Volume and Mute Function
  21. * - SSI : Serial Sound Interface
  22. *
  23. * Gen2
  24. *
  25. * SCU : Sampling Rate Converter Unit
  26. * - SRC : Sampling Rate Converter
  27. * - CMD
  28. * - CTU : Channel Count Conversion Unit
  29. * - MIX : Mixer
  30. * - DVC : Digital Volume and Mute Function
  31. * SSIU : Serial Sound Interface Unit
  32. * - SSI : Serial Sound Interface
  33. */
  34. /*
  35. * driver data Image
  36. *
  37. * rsnd_priv
  38. * |
  39. * | ** this depends on Gen1/Gen2
  40. * |
  41. * +- gen
  42. * |
  43. * | ** these depend on data path
  44. * | ** gen and platform data control it
  45. * |
  46. * +- rdai[0]
  47. * | | sru ssiu ssi
  48. * | +- playback -> [mod] -> [mod] -> [mod] -> ...
  49. * | |
  50. * | | sru ssiu ssi
  51. * | +- capture -> [mod] -> [mod] -> [mod] -> ...
  52. * |
  53. * +- rdai[1]
  54. * | | sru ssiu ssi
  55. * | +- playback -> [mod] -> [mod] -> [mod] -> ...
  56. * | |
  57. * | | sru ssiu ssi
  58. * | +- capture -> [mod] -> [mod] -> [mod] -> ...
  59. * ...
  60. * |
  61. * | ** these control ssi
  62. * |
  63. * +- ssi
  64. * | |
  65. * | +- ssi[0]
  66. * | +- ssi[1]
  67. * | +- ssi[2]
  68. * | ...
  69. * |
  70. * | ** these control src
  71. * |
  72. * +- src
  73. * |
  74. * +- src[0]
  75. * +- src[1]
  76. * +- src[2]
  77. * ...
  78. *
  79. *
  80. * for_each_rsnd_dai(xx, priv, xx)
  81. * rdai[0] => rdai[1] => rdai[2] => ...
  82. *
  83. * for_each_rsnd_mod(xx, rdai, xx)
  84. * [mod] => [mod] => [mod] => ...
  85. *
  86. * rsnd_dai_call(xxx, fn )
  87. * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
  88. *
  89. */
  90. /*
  91. * you can enable below define if you don't need
  92. * DAI status debug message when debugging
  93. * see rsnd_dbg_dai_call()
  94. *
  95. * #define RSND_DEBUG_NO_DAI_CALL 1
  96. */
  97. #include <linux/pm_runtime.h>
  98. #include "rsnd.h"
  99. #define RSND_RATES SNDRV_PCM_RATE_8000_192000
  100. #define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
  101. SNDRV_PCM_FMTBIT_S16_LE |\
  102. SNDRV_PCM_FMTBIT_S24_LE)
  103. static const struct of_device_id rsnd_of_match[] = {
  104. { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
  105. { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
  106. { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
  107. {},
  108. };
  109. MODULE_DEVICE_TABLE(of, rsnd_of_match);
  110. /*
  111. * rsnd_mod functions
  112. */
  113. void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
  114. {
  115. if (mod->type != type) {
  116. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  117. struct device *dev = rsnd_priv_to_dev(priv);
  118. dev_warn(dev, "%s[%d] is not your expected module\n",
  119. rsnd_mod_name(mod), rsnd_mod_id(mod));
  120. }
  121. }
  122. struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
  123. struct rsnd_mod *mod)
  124. {
  125. if (!mod || !mod->ops || !mod->ops->dma_req)
  126. return NULL;
  127. return mod->ops->dma_req(io, mod);
  128. }
  129. u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
  130. struct rsnd_mod *mod,
  131. enum rsnd_mod_type type)
  132. {
  133. return &mod->status;
  134. }
  135. int rsnd_mod_init(struct rsnd_priv *priv,
  136. struct rsnd_mod *mod,
  137. struct rsnd_mod_ops *ops,
  138. struct clk *clk,
  139. u32* (*get_status)(struct rsnd_dai_stream *io,
  140. struct rsnd_mod *mod,
  141. enum rsnd_mod_type type),
  142. enum rsnd_mod_type type,
  143. int id)
  144. {
  145. int ret = clk_prepare(clk);
  146. if (ret)
  147. return ret;
  148. mod->id = id;
  149. mod->ops = ops;
  150. mod->type = type;
  151. mod->clk = clk;
  152. mod->priv = priv;
  153. mod->get_status = get_status;
  154. return ret;
  155. }
  156. void rsnd_mod_quit(struct rsnd_mod *mod)
  157. {
  158. clk_unprepare(mod->clk);
  159. mod->clk = NULL;
  160. }
  161. void rsnd_mod_interrupt(struct rsnd_mod *mod,
  162. void (*callback)(struct rsnd_mod *mod,
  163. struct rsnd_dai_stream *io))
  164. {
  165. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  166. struct rsnd_dai_stream *io;
  167. struct rsnd_dai *rdai;
  168. int i;
  169. for_each_rsnd_dai(rdai, priv, i) {
  170. io = &rdai->playback;
  171. if (mod == io->mod[mod->type])
  172. callback(mod, io);
  173. io = &rdai->capture;
  174. if (mod == io->mod[mod->type])
  175. callback(mod, io);
  176. }
  177. }
  178. int rsnd_io_is_working(struct rsnd_dai_stream *io)
  179. {
  180. /* see rsnd_dai_stream_init/quit() */
  181. if (io->substream)
  182. return snd_pcm_running(io->substream);
  183. return 0;
  184. }
  185. int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
  186. struct snd_pcm_hw_params *params)
  187. {
  188. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  189. /*
  190. * params will be added when refine
  191. * see
  192. * __rsnd_soc_hw_rule_rate()
  193. * __rsnd_soc_hw_rule_channels()
  194. */
  195. if (params)
  196. return params_channels(params);
  197. else
  198. return runtime->channels;
  199. }
  200. int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
  201. struct snd_pcm_hw_params *params)
  202. {
  203. int chan = rsnd_runtime_channel_original_with_params(io, params);
  204. struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
  205. if (ctu_mod) {
  206. u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
  207. if (converted_chan)
  208. return converted_chan;
  209. }
  210. return chan;
  211. }
  212. int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
  213. struct snd_pcm_hw_params *params)
  214. {
  215. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  216. int chan = rsnd_io_is_play(io) ?
  217. rsnd_runtime_channel_after_ctu_with_params(io, params) :
  218. rsnd_runtime_channel_original_with_params(io, params);
  219. /* Use Multi SSI */
  220. if (rsnd_runtime_is_ssi_multi(io))
  221. chan /= rsnd_rdai_ssi_lane_get(rdai);
  222. /* TDM Extend Mode needs 8ch */
  223. if (chan == 6)
  224. chan = 8;
  225. return chan;
  226. }
  227. int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
  228. {
  229. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  230. int lane = rsnd_rdai_ssi_lane_get(rdai);
  231. int chan = rsnd_io_is_play(io) ?
  232. rsnd_runtime_channel_after_ctu(io) :
  233. rsnd_runtime_channel_original(io);
  234. return (chan > 2) && (lane > 1);
  235. }
  236. int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
  237. {
  238. return rsnd_runtime_channel_for_ssi(io) >= 6;
  239. }
  240. /*
  241. * ADINR function
  242. */
  243. u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
  244. {
  245. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  246. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  247. struct device *dev = rsnd_priv_to_dev(priv);
  248. switch (snd_pcm_format_width(runtime->format)) {
  249. case 8:
  250. return 16 << 16;
  251. case 16:
  252. return 8 << 16;
  253. case 24:
  254. return 0 << 16;
  255. }
  256. dev_warn(dev, "not supported sample bits\n");
  257. return 0;
  258. }
  259. /*
  260. * DALIGN function
  261. */
  262. u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
  263. {
  264. struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
  265. struct rsnd_mod *target;
  266. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  267. /*
  268. * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
  269. * 31..16 15...0
  270. * HW: [L ch] [R ch]
  271. * SW: [R ch] [L ch]
  272. * We need to care about inversion timing to control
  273. * Playback/Capture correctly.
  274. * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
  275. *
  276. * sL/R : software L/R
  277. * hL/R : hardware L/R
  278. * (*) : conversion timing
  279. *
  280. * Playback
  281. * sL/R (*) hL/R hL/R hL/R hL/R hL/R
  282. * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
  283. *
  284. * Capture
  285. * hL/R hL/R hL/R hL/R hL/R (*) sL/R
  286. * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
  287. */
  288. if (rsnd_io_is_play(io)) {
  289. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  290. target = src ? src : ssiu;
  291. } else {
  292. struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
  293. target = cmd ? cmd : ssiu;
  294. }
  295. /* Non target mod or non 16bit needs normal DALIGN */
  296. if ((snd_pcm_format_width(runtime->format) != 16) ||
  297. (mod != target))
  298. return 0x76543210;
  299. /* Target mod needs inverted DALIGN when 16bit */
  300. else
  301. return 0x67452301;
  302. }
  303. u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
  304. {
  305. enum rsnd_mod_type playback_mods[] = {
  306. RSND_MOD_SRC,
  307. RSND_MOD_CMD,
  308. RSND_MOD_SSIU,
  309. };
  310. enum rsnd_mod_type capture_mods[] = {
  311. RSND_MOD_CMD,
  312. RSND_MOD_SRC,
  313. RSND_MOD_SSIU,
  314. };
  315. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  316. struct rsnd_mod *tmod = NULL;
  317. enum rsnd_mod_type *mods =
  318. rsnd_io_is_play(io) ?
  319. playback_mods : capture_mods;
  320. int i;
  321. /*
  322. * This is needed for 24bit data
  323. * We need to shift 8bit
  324. *
  325. * Linux 24bit data is located as 0x00******
  326. * HW 24bit data is located as 0x******00
  327. *
  328. */
  329. if (snd_pcm_format_width(runtime->format) != 24)
  330. return 0;
  331. for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
  332. tmod = rsnd_io_to_mod(io, mods[i]);
  333. if (tmod)
  334. break;
  335. }
  336. if (tmod != mod)
  337. return 0;
  338. if (rsnd_io_is_play(io))
  339. return (0 << 20) | /* shift to Left */
  340. (8 << 16); /* 8bit */
  341. else
  342. return (1 << 20) | /* shift to Right */
  343. (8 << 16); /* 8bit */
  344. }
  345. /*
  346. * rsnd_dai functions
  347. */
  348. struct rsnd_mod *rsnd_mod_next(int *iterator,
  349. struct rsnd_dai_stream *io,
  350. enum rsnd_mod_type *array,
  351. int array_size)
  352. {
  353. struct rsnd_mod *mod;
  354. enum rsnd_mod_type type;
  355. int max = array ? array_size : RSND_MOD_MAX;
  356. for (; *iterator < max; (*iterator)++) {
  357. type = (array) ? array[*iterator] : *iterator;
  358. mod = rsnd_io_to_mod(io, type);
  359. if (mod)
  360. return mod;
  361. }
  362. return NULL;
  363. }
  364. static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
  365. {
  366. /* CAPTURE */
  367. RSND_MOD_AUDMAPP,
  368. RSND_MOD_AUDMA,
  369. RSND_MOD_DVC,
  370. RSND_MOD_MIX,
  371. RSND_MOD_CTU,
  372. RSND_MOD_CMD,
  373. RSND_MOD_SRC,
  374. RSND_MOD_SSIU,
  375. RSND_MOD_SSIM3,
  376. RSND_MOD_SSIM2,
  377. RSND_MOD_SSIM1,
  378. RSND_MOD_SSIP,
  379. RSND_MOD_SSI,
  380. }, {
  381. /* PLAYBACK */
  382. RSND_MOD_AUDMAPP,
  383. RSND_MOD_AUDMA,
  384. RSND_MOD_SSIM3,
  385. RSND_MOD_SSIM2,
  386. RSND_MOD_SSIM1,
  387. RSND_MOD_SSIP,
  388. RSND_MOD_SSI,
  389. RSND_MOD_SSIU,
  390. RSND_MOD_DVC,
  391. RSND_MOD_MIX,
  392. RSND_MOD_CTU,
  393. RSND_MOD_CMD,
  394. RSND_MOD_SRC,
  395. },
  396. };
  397. static int rsnd_status_update(u32 *status,
  398. int shift, int add, int timing)
  399. {
  400. u32 mask = 0xF << shift;
  401. u8 val = (*status >> shift) & 0xF;
  402. u8 next_val = (val + add) & 0xF;
  403. int func_call = (val == timing);
  404. if (next_val == 0xF) /* underflow case */
  405. func_call = 0;
  406. else
  407. *status = (*status & ~mask) + (next_val << shift);
  408. return func_call;
  409. }
  410. #define rsnd_dai_call(fn, io, param...) \
  411. ({ \
  412. struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
  413. struct rsnd_mod *mod; \
  414. int is_play = rsnd_io_is_play(io); \
  415. int ret = 0, i; \
  416. enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
  417. for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
  418. int tmp = 0; \
  419. u32 *status = mod->get_status(io, mod, types[i]); \
  420. int func_call = rsnd_status_update(status, \
  421. __rsnd_mod_shift_##fn, \
  422. __rsnd_mod_add_##fn, \
  423. __rsnd_mod_call_##fn); \
  424. rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n", \
  425. rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
  426. (func_call && (mod)->ops->fn) ? #fn : ""); \
  427. if (func_call && (mod)->ops->fn) \
  428. tmp = (mod)->ops->fn(mod, io, param); \
  429. if (tmp) \
  430. dev_err(dev, "%s[%d] : %s error %d\n", \
  431. rsnd_mod_name(mod), rsnd_mod_id(mod), \
  432. #fn, tmp); \
  433. ret |= tmp; \
  434. } \
  435. ret; \
  436. })
  437. int rsnd_dai_connect(struct rsnd_mod *mod,
  438. struct rsnd_dai_stream *io,
  439. enum rsnd_mod_type type)
  440. {
  441. struct rsnd_priv *priv;
  442. struct device *dev;
  443. if (!mod)
  444. return -EIO;
  445. if (io->mod[type] == mod)
  446. return 0;
  447. if (io->mod[type])
  448. return -EINVAL;
  449. priv = rsnd_mod_to_priv(mod);
  450. dev = rsnd_priv_to_dev(priv);
  451. io->mod[type] = mod;
  452. dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
  453. rsnd_mod_name(mod), rsnd_mod_id(mod),
  454. rsnd_io_is_play(io) ? "Playback" : "Capture");
  455. return 0;
  456. }
  457. static void rsnd_dai_disconnect(struct rsnd_mod *mod,
  458. struct rsnd_dai_stream *io,
  459. enum rsnd_mod_type type)
  460. {
  461. io->mod[type] = NULL;
  462. }
  463. int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
  464. int max_channels)
  465. {
  466. if (max_channels > 0)
  467. rdai->max_channels = max_channels;
  468. return rdai->max_channels;
  469. }
  470. int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
  471. int ssi_lane)
  472. {
  473. if (ssi_lane > 0)
  474. rdai->ssi_lane = ssi_lane;
  475. return rdai->ssi_lane;
  476. }
  477. int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
  478. {
  479. if (width > 0)
  480. rdai->chan_width = width;
  481. return rdai->chan_width;
  482. }
  483. struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
  484. {
  485. if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
  486. return NULL;
  487. return priv->rdai + id;
  488. }
  489. static struct snd_soc_dai_driver
  490. *rsnd_daidrv_get(struct rsnd_priv *priv, int id)
  491. {
  492. if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
  493. return NULL;
  494. return priv->daidrv + id;
  495. }
  496. #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
  497. static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
  498. {
  499. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  500. return rsnd_rdai_get(priv, dai->id);
  501. }
  502. /*
  503. * rsnd_soc_dai functions
  504. */
  505. void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
  506. {
  507. struct snd_pcm_substream *substream = io->substream;
  508. /*
  509. * this function should be called...
  510. *
  511. * - if rsnd_dai_pointer_update() returns true
  512. * - without spin lock
  513. */
  514. snd_pcm_period_elapsed(substream);
  515. }
  516. static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
  517. struct snd_pcm_substream *substream)
  518. {
  519. io->substream = substream;
  520. }
  521. static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
  522. {
  523. io->substream = NULL;
  524. }
  525. static
  526. struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
  527. {
  528. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  529. return rtd->cpu_dai;
  530. }
  531. static
  532. struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
  533. struct snd_pcm_substream *substream)
  534. {
  535. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  536. return &rdai->playback;
  537. else
  538. return &rdai->capture;
  539. }
  540. static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  541. struct snd_soc_dai *dai)
  542. {
  543. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  544. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  545. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  546. int ret;
  547. unsigned long flags;
  548. spin_lock_irqsave(&priv->lock, flags);
  549. switch (cmd) {
  550. case SNDRV_PCM_TRIGGER_START:
  551. case SNDRV_PCM_TRIGGER_RESUME:
  552. ret = rsnd_dai_call(init, io, priv);
  553. if (ret < 0)
  554. goto dai_trigger_end;
  555. ret = rsnd_dai_call(start, io, priv);
  556. if (ret < 0)
  557. goto dai_trigger_end;
  558. ret = rsnd_dai_call(irq, io, priv, 1);
  559. if (ret < 0)
  560. goto dai_trigger_end;
  561. break;
  562. case SNDRV_PCM_TRIGGER_STOP:
  563. case SNDRV_PCM_TRIGGER_SUSPEND:
  564. ret = rsnd_dai_call(irq, io, priv, 0);
  565. ret |= rsnd_dai_call(stop, io, priv);
  566. ret |= rsnd_dai_call(quit, io, priv);
  567. break;
  568. default:
  569. ret = -EINVAL;
  570. }
  571. dai_trigger_end:
  572. spin_unlock_irqrestore(&priv->lock, flags);
  573. return ret;
  574. }
  575. static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  576. {
  577. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  578. /* set master/slave audio interface */
  579. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  580. case SND_SOC_DAIFMT_CBM_CFM:
  581. rdai->clk_master = 0;
  582. break;
  583. case SND_SOC_DAIFMT_CBS_CFS:
  584. rdai->clk_master = 1; /* codec is slave, cpu is master */
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. /* set format */
  590. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  591. case SND_SOC_DAIFMT_I2S:
  592. rdai->sys_delay = 0;
  593. rdai->data_alignment = 0;
  594. rdai->frm_clk_inv = 0;
  595. break;
  596. case SND_SOC_DAIFMT_LEFT_J:
  597. case SND_SOC_DAIFMT_DSP_B:
  598. rdai->sys_delay = 1;
  599. rdai->data_alignment = 0;
  600. rdai->frm_clk_inv = 1;
  601. break;
  602. case SND_SOC_DAIFMT_RIGHT_J:
  603. rdai->sys_delay = 1;
  604. rdai->data_alignment = 1;
  605. rdai->frm_clk_inv = 1;
  606. break;
  607. case SND_SOC_DAIFMT_DSP_A:
  608. rdai->sys_delay = 0;
  609. rdai->data_alignment = 0;
  610. rdai->frm_clk_inv = 1;
  611. break;
  612. }
  613. /* set clock inversion */
  614. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  615. case SND_SOC_DAIFMT_NB_IF:
  616. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  617. break;
  618. case SND_SOC_DAIFMT_IB_NF:
  619. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  620. break;
  621. case SND_SOC_DAIFMT_IB_IF:
  622. rdai->bit_clk_inv = !rdai->bit_clk_inv;
  623. rdai->frm_clk_inv = !rdai->frm_clk_inv;
  624. break;
  625. case SND_SOC_DAIFMT_NB_NF:
  626. default:
  627. break;
  628. }
  629. return 0;
  630. }
  631. static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
  632. u32 tx_mask, u32 rx_mask,
  633. int slots, int slot_width)
  634. {
  635. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  636. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  637. struct device *dev = rsnd_priv_to_dev(priv);
  638. switch (slot_width) {
  639. case 16:
  640. case 24:
  641. case 32:
  642. break;
  643. default:
  644. dev_err(dev, "unsupported slot width value: %d\n", slot_width);
  645. return -EINVAL;
  646. }
  647. switch (slots) {
  648. case 2:
  649. case 6:
  650. case 8:
  651. /* TDM Extend Mode */
  652. rsnd_rdai_channels_set(rdai, slots);
  653. rsnd_rdai_ssi_lane_set(rdai, 1);
  654. rsnd_rdai_width_set(rdai, slot_width);
  655. break;
  656. default:
  657. dev_err(dev, "unsupported TDM slots (%d)\n", slots);
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static unsigned int rsnd_soc_hw_channels_list[] = {
  663. 2, 6, 8,
  664. };
  665. static unsigned int rsnd_soc_hw_rate_list[] = {
  666. 8000,
  667. 11025,
  668. 16000,
  669. 22050,
  670. 32000,
  671. 44100,
  672. 48000,
  673. 64000,
  674. 88200,
  675. 96000,
  676. 176400,
  677. 192000,
  678. };
  679. static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
  680. unsigned int *list, int list_num,
  681. struct snd_interval *baseline, struct snd_interval *iv)
  682. {
  683. struct snd_interval p;
  684. unsigned int rate;
  685. int i;
  686. snd_interval_any(&p);
  687. p.min = UINT_MAX;
  688. p.max = 0;
  689. for (i = 0; i < list_num; i++) {
  690. if (!snd_interval_test(iv, list[i]))
  691. continue;
  692. rate = rsnd_ssi_clk_query(rdai,
  693. baseline->min, list[i], NULL);
  694. if (rate > 0) {
  695. p.min = min(p.min, list[i]);
  696. p.max = max(p.max, list[i]);
  697. }
  698. rate = rsnd_ssi_clk_query(rdai,
  699. baseline->max, list[i], NULL);
  700. if (rate > 0) {
  701. p.min = min(p.min, list[i]);
  702. p.max = max(p.max, list[i]);
  703. }
  704. }
  705. return snd_interval_refine(iv, &p);
  706. }
  707. static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
  708. struct snd_pcm_hw_rule *rule)
  709. {
  710. struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  711. struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  712. struct snd_interval ic;
  713. struct rsnd_dai_stream *io = rule->private;
  714. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  715. /*
  716. * possible sampling rate limitation is same as
  717. * 2ch if it supports multi ssi
  718. * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
  719. */
  720. ic = *ic_;
  721. ic.min =
  722. ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
  723. return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
  724. ARRAY_SIZE(rsnd_soc_hw_rate_list),
  725. &ic, ir);
  726. }
  727. static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
  728. struct snd_pcm_hw_rule *rule)
  729. {
  730. struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  731. struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  732. struct snd_interval ic;
  733. struct rsnd_dai_stream *io = rule->private;
  734. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  735. /*
  736. * possible sampling rate limitation is same as
  737. * 2ch if it supports multi ssi
  738. * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
  739. */
  740. ic = *ic_;
  741. ic.min =
  742. ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
  743. return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
  744. ARRAY_SIZE(rsnd_soc_hw_channels_list),
  745. ir, &ic);
  746. }
  747. static const struct snd_pcm_hardware rsnd_pcm_hardware = {
  748. .info = SNDRV_PCM_INFO_INTERLEAVED |
  749. SNDRV_PCM_INFO_MMAP |
  750. SNDRV_PCM_INFO_MMAP_VALID,
  751. .buffer_bytes_max = 64 * 1024,
  752. .period_bytes_min = 32,
  753. .period_bytes_max = 8192,
  754. .periods_min = 1,
  755. .periods_max = 32,
  756. .fifo_size = 256,
  757. };
  758. static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
  759. struct snd_soc_dai *dai)
  760. {
  761. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  762. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  763. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  764. struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
  765. struct snd_pcm_runtime *runtime = substream->runtime;
  766. unsigned int max_channels = rsnd_rdai_channels_get(rdai);
  767. int ret;
  768. int i;
  769. rsnd_dai_stream_init(io, substream);
  770. /*
  771. * Channel Limitation
  772. * It depends on Platform design
  773. */
  774. constraint->list = rsnd_soc_hw_channels_list;
  775. constraint->count = 0;
  776. constraint->mask = 0;
  777. for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
  778. if (rsnd_soc_hw_channels_list[i] > max_channels)
  779. break;
  780. constraint->count = i + 1;
  781. }
  782. snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
  783. snd_pcm_hw_constraint_list(runtime, 0,
  784. SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
  785. snd_pcm_hw_constraint_integer(runtime,
  786. SNDRV_PCM_HW_PARAM_PERIODS);
  787. /*
  788. * Sampling Rate / Channel Limitation
  789. * It depends on Clock Master Mode
  790. */
  791. if (rsnd_rdai_is_clk_master(rdai)) {
  792. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  793. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  794. rsnd_soc_hw_rule_rate,
  795. is_play ? &rdai->playback : &rdai->capture,
  796. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  797. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  798. rsnd_soc_hw_rule_channels,
  799. is_play ? &rdai->playback : &rdai->capture,
  800. SNDRV_PCM_HW_PARAM_RATE, -1);
  801. }
  802. /*
  803. * call rsnd_dai_call without spinlock
  804. */
  805. ret = rsnd_dai_call(nolock_start, io, priv);
  806. if (ret < 0)
  807. rsnd_dai_call(nolock_stop, io, priv);
  808. return ret;
  809. }
  810. static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
  811. struct snd_soc_dai *dai)
  812. {
  813. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  814. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  815. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  816. /*
  817. * call rsnd_dai_call without spinlock
  818. */
  819. rsnd_dai_call(nolock_stop, io, priv);
  820. rsnd_dai_stream_quit(io);
  821. }
  822. static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
  823. struct snd_soc_dai *dai)
  824. {
  825. struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
  826. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  827. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  828. return rsnd_dai_call(prepare, io, priv);
  829. }
  830. static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
  831. .startup = rsnd_soc_dai_startup,
  832. .shutdown = rsnd_soc_dai_shutdown,
  833. .trigger = rsnd_soc_dai_trigger,
  834. .set_fmt = rsnd_soc_dai_set_fmt,
  835. .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
  836. .prepare = rsnd_soc_dai_prepare,
  837. };
  838. void rsnd_parse_connect_common(struct rsnd_dai *rdai,
  839. struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
  840. struct device_node *node,
  841. struct device_node *playback,
  842. struct device_node *capture)
  843. {
  844. struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
  845. struct device_node *np;
  846. struct rsnd_mod *mod;
  847. int i;
  848. if (!node)
  849. return;
  850. i = 0;
  851. for_each_child_of_node(node, np) {
  852. mod = mod_get(priv, i);
  853. if (np == playback)
  854. rsnd_dai_connect(mod, &rdai->playback, mod->type);
  855. if (np == capture)
  856. rsnd_dai_connect(mod, &rdai->capture, mod->type);
  857. i++;
  858. }
  859. of_node_put(node);
  860. }
  861. static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
  862. int *is_graph)
  863. {
  864. struct device *dev = rsnd_priv_to_dev(priv);
  865. struct device_node *np = dev->of_node;
  866. struct device_node *dai_node;
  867. struct device_node *ret;
  868. *is_graph = 0;
  869. /*
  870. * parse both previous dai (= rcar_sound,dai), and
  871. * graph dai (= ports/port)
  872. */
  873. dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
  874. if (dai_node) {
  875. ret = dai_node;
  876. goto of_node_compatible;
  877. }
  878. ret = np;
  879. dai_node = of_graph_get_next_endpoint(np, NULL);
  880. if (dai_node)
  881. goto of_node_graph;
  882. return NULL;
  883. of_node_graph:
  884. *is_graph = 1;
  885. of_node_compatible:
  886. of_node_put(dai_node);
  887. return ret;
  888. }
  889. static void __rsnd_dai_probe(struct rsnd_priv *priv,
  890. struct device_node *dai_np,
  891. int dai_i)
  892. {
  893. struct device_node *playback, *capture;
  894. struct rsnd_dai_stream *io_playback;
  895. struct rsnd_dai_stream *io_capture;
  896. struct snd_soc_dai_driver *drv;
  897. struct rsnd_dai *rdai;
  898. struct device *dev = rsnd_priv_to_dev(priv);
  899. int io_i;
  900. rdai = rsnd_rdai_get(priv, dai_i);
  901. drv = rsnd_daidrv_get(priv, dai_i);
  902. io_playback = &rdai->playback;
  903. io_capture = &rdai->capture;
  904. snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
  905. rdai->priv = priv;
  906. drv->name = rdai->name;
  907. drv->ops = &rsnd_soc_dai_ops;
  908. snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
  909. "DAI%d Playback", dai_i);
  910. drv->playback.rates = RSND_RATES;
  911. drv->playback.formats = RSND_FMTS;
  912. drv->playback.channels_min = 2;
  913. drv->playback.channels_max = 8;
  914. drv->playback.stream_name = rdai->playback.name;
  915. snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
  916. "DAI%d Capture", dai_i);
  917. drv->capture.rates = RSND_RATES;
  918. drv->capture.formats = RSND_FMTS;
  919. drv->capture.channels_min = 2;
  920. drv->capture.channels_max = 8;
  921. drv->capture.stream_name = rdai->capture.name;
  922. rdai->playback.rdai = rdai;
  923. rdai->capture.rdai = rdai;
  924. rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
  925. rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
  926. rsnd_rdai_width_set(rdai, 32); /* default 32bit width */
  927. for (io_i = 0;; io_i++) {
  928. playback = of_parse_phandle(dai_np, "playback", io_i);
  929. capture = of_parse_phandle(dai_np, "capture", io_i);
  930. if (!playback && !capture)
  931. break;
  932. rsnd_parse_connect_ssi(rdai, playback, capture);
  933. rsnd_parse_connect_src(rdai, playback, capture);
  934. rsnd_parse_connect_ctu(rdai, playback, capture);
  935. rsnd_parse_connect_mix(rdai, playback, capture);
  936. rsnd_parse_connect_dvc(rdai, playback, capture);
  937. of_node_put(playback);
  938. of_node_put(capture);
  939. }
  940. if (rsnd_ssi_is_pin_sharing(io_capture) ||
  941. rsnd_ssi_is_pin_sharing(io_playback)) {
  942. /* should have symmetric_rates if pin sharing */
  943. drv->symmetric_rates = 1;
  944. }
  945. dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
  946. rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
  947. rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
  948. }
  949. static int rsnd_dai_probe(struct rsnd_priv *priv)
  950. {
  951. struct device_node *dai_node;
  952. struct device_node *dai_np;
  953. struct snd_soc_dai_driver *rdrv;
  954. struct device *dev = rsnd_priv_to_dev(priv);
  955. struct rsnd_dai *rdai;
  956. int nr;
  957. int is_graph;
  958. int dai_i;
  959. dai_node = rsnd_dai_of_node(priv, &is_graph);
  960. if (is_graph)
  961. nr = of_graph_get_endpoint_count(dai_node);
  962. else
  963. nr = of_get_child_count(dai_node);
  964. if (!nr)
  965. return -EINVAL;
  966. rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
  967. rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
  968. if (!rdrv || !rdai)
  969. return -ENOMEM;
  970. priv->rdai_nr = nr;
  971. priv->daidrv = rdrv;
  972. priv->rdai = rdai;
  973. /*
  974. * parse all dai
  975. */
  976. dai_i = 0;
  977. if (is_graph) {
  978. for_each_endpoint_of_node(dai_node, dai_np) {
  979. __rsnd_dai_probe(priv, dai_np, dai_i);
  980. rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
  981. dai_i++;
  982. }
  983. } else {
  984. for_each_child_of_node(dai_node, dai_np)
  985. __rsnd_dai_probe(priv, dai_np, dai_i++);
  986. }
  987. return 0;
  988. }
  989. /*
  990. * pcm ops
  991. */
  992. static int rsnd_hw_params(struct snd_pcm_substream *substream,
  993. struct snd_pcm_hw_params *hw_params)
  994. {
  995. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  996. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  997. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  998. int ret;
  999. ret = rsnd_dai_call(hw_params, io, substream, hw_params);
  1000. if (ret)
  1001. return ret;
  1002. return snd_pcm_lib_malloc_pages(substream,
  1003. params_buffer_bytes(hw_params));
  1004. }
  1005. static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
  1006. {
  1007. struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
  1008. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  1009. struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
  1010. snd_pcm_uframes_t pointer = 0;
  1011. rsnd_dai_call(pointer, io, &pointer);
  1012. return pointer;
  1013. }
  1014. static const struct snd_pcm_ops rsnd_pcm_ops = {
  1015. .ioctl = snd_pcm_lib_ioctl,
  1016. .hw_params = rsnd_hw_params,
  1017. .hw_free = snd_pcm_lib_free_pages,
  1018. .pointer = rsnd_pointer,
  1019. };
  1020. /*
  1021. * snd_kcontrol
  1022. */
  1023. static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
  1024. struct snd_ctl_elem_info *uinfo)
  1025. {
  1026. struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
  1027. if (cfg->texts) {
  1028. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1029. uinfo->count = cfg->size;
  1030. uinfo->value.enumerated.items = cfg->max;
  1031. if (uinfo->value.enumerated.item >= cfg->max)
  1032. uinfo->value.enumerated.item = cfg->max - 1;
  1033. strlcpy(uinfo->value.enumerated.name,
  1034. cfg->texts[uinfo->value.enumerated.item],
  1035. sizeof(uinfo->value.enumerated.name));
  1036. } else {
  1037. uinfo->count = cfg->size;
  1038. uinfo->value.integer.min = 0;
  1039. uinfo->value.integer.max = cfg->max;
  1040. uinfo->type = (cfg->max == 1) ?
  1041. SNDRV_CTL_ELEM_TYPE_BOOLEAN :
  1042. SNDRV_CTL_ELEM_TYPE_INTEGER;
  1043. }
  1044. return 0;
  1045. }
  1046. static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
  1047. struct snd_ctl_elem_value *uc)
  1048. {
  1049. struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
  1050. int i;
  1051. for (i = 0; i < cfg->size; i++)
  1052. if (cfg->texts)
  1053. uc->value.enumerated.item[i] = cfg->val[i];
  1054. else
  1055. uc->value.integer.value[i] = cfg->val[i];
  1056. return 0;
  1057. }
  1058. static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
  1059. struct snd_ctl_elem_value *uc)
  1060. {
  1061. struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
  1062. int i, change = 0;
  1063. if (!cfg->accept(cfg->io))
  1064. return 0;
  1065. for (i = 0; i < cfg->size; i++) {
  1066. if (cfg->texts) {
  1067. change |= (uc->value.enumerated.item[i] != cfg->val[i]);
  1068. cfg->val[i] = uc->value.enumerated.item[i];
  1069. } else {
  1070. change |= (uc->value.integer.value[i] != cfg->val[i]);
  1071. cfg->val[i] = uc->value.integer.value[i];
  1072. }
  1073. }
  1074. if (change && cfg->update)
  1075. cfg->update(cfg->io, cfg->mod);
  1076. return change;
  1077. }
  1078. int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
  1079. {
  1080. return 1;
  1081. }
  1082. int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
  1083. {
  1084. struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
  1085. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  1086. struct device *dev = rsnd_priv_to_dev(priv);
  1087. if (!runtime) {
  1088. dev_warn(dev, "Can't update kctrl when idle\n");
  1089. return 0;
  1090. }
  1091. return 1;
  1092. }
  1093. struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
  1094. {
  1095. cfg->cfg.val = cfg->val;
  1096. return &cfg->cfg;
  1097. }
  1098. struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
  1099. {
  1100. cfg->cfg.val = &cfg->val;
  1101. return &cfg->cfg;
  1102. }
  1103. const char * const volume_ramp_rate[] = {
  1104. "128 dB/1 step", /* 00000 */
  1105. "64 dB/1 step", /* 00001 */
  1106. "32 dB/1 step", /* 00010 */
  1107. "16 dB/1 step", /* 00011 */
  1108. "8 dB/1 step", /* 00100 */
  1109. "4 dB/1 step", /* 00101 */
  1110. "2 dB/1 step", /* 00110 */
  1111. "1 dB/1 step", /* 00111 */
  1112. "0.5 dB/1 step", /* 01000 */
  1113. "0.25 dB/1 step", /* 01001 */
  1114. "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
  1115. "0.125 dB/2 steps", /* 01011 */
  1116. "0.125 dB/4 steps", /* 01100 */
  1117. "0.125 dB/8 steps", /* 01101 */
  1118. "0.125 dB/16 steps", /* 01110 */
  1119. "0.125 dB/32 steps", /* 01111 */
  1120. "0.125 dB/64 steps", /* 10000 */
  1121. "0.125 dB/128 steps", /* 10001 */
  1122. "0.125 dB/256 steps", /* 10010 */
  1123. "0.125 dB/512 steps", /* 10011 */
  1124. "0.125 dB/1024 steps", /* 10100 */
  1125. "0.125 dB/2048 steps", /* 10101 */
  1126. "0.125 dB/4096 steps", /* 10110 */
  1127. "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
  1128. };
  1129. int rsnd_kctrl_new(struct rsnd_mod *mod,
  1130. struct rsnd_dai_stream *io,
  1131. struct snd_soc_pcm_runtime *rtd,
  1132. const unsigned char *name,
  1133. int (*accept)(struct rsnd_dai_stream *io),
  1134. void (*update)(struct rsnd_dai_stream *io,
  1135. struct rsnd_mod *mod),
  1136. struct rsnd_kctrl_cfg *cfg,
  1137. const char * const *texts,
  1138. int size,
  1139. u32 max)
  1140. {
  1141. struct snd_card *card = rtd->card->snd_card;
  1142. struct snd_kcontrol *kctrl;
  1143. struct snd_kcontrol_new knew = {
  1144. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1145. .name = name,
  1146. .info = rsnd_kctrl_info,
  1147. .index = rtd->num,
  1148. .get = rsnd_kctrl_get,
  1149. .put = rsnd_kctrl_put,
  1150. };
  1151. int ret;
  1152. if (size > RSND_MAX_CHANNELS)
  1153. return -EINVAL;
  1154. kctrl = snd_ctl_new1(&knew, cfg);
  1155. if (!kctrl)
  1156. return -ENOMEM;
  1157. ret = snd_ctl_add(card, kctrl);
  1158. if (ret < 0)
  1159. return ret;
  1160. cfg->texts = texts;
  1161. cfg->max = max;
  1162. cfg->size = size;
  1163. cfg->accept = accept;
  1164. cfg->update = update;
  1165. cfg->card = card;
  1166. cfg->kctrl = kctrl;
  1167. cfg->io = io;
  1168. cfg->mod = mod;
  1169. return 0;
  1170. }
  1171. /*
  1172. * snd_soc_component
  1173. */
  1174. #define PREALLOC_BUFFER (32 * 1024)
  1175. #define PREALLOC_BUFFER_MAX (32 * 1024)
  1176. static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
  1177. struct rsnd_dai_stream *io,
  1178. int stream)
  1179. {
  1180. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  1181. struct device *dev = rsnd_priv_to_dev(priv);
  1182. struct snd_pcm_substream *substream;
  1183. int err;
  1184. /*
  1185. * use Audio-DMAC dev if we can use IPMMU
  1186. * see
  1187. * rsnd_dmaen_attach()
  1188. */
  1189. if (io->dmac_dev)
  1190. dev = io->dmac_dev;
  1191. for (substream = rtd->pcm->streams[stream].substream;
  1192. substream;
  1193. substream = substream->next) {
  1194. err = snd_pcm_lib_preallocate_pages(substream,
  1195. SNDRV_DMA_TYPE_DEV,
  1196. dev,
  1197. PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
  1198. if (err < 0)
  1199. return err;
  1200. }
  1201. return 0;
  1202. }
  1203. static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1204. {
  1205. struct snd_soc_dai *dai = rtd->cpu_dai;
  1206. struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
  1207. int ret;
  1208. ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
  1209. if (ret)
  1210. return ret;
  1211. ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
  1212. if (ret)
  1213. return ret;
  1214. ret = rsnd_preallocate_pages(rtd, &rdai->playback,
  1215. SNDRV_PCM_STREAM_PLAYBACK);
  1216. if (ret)
  1217. return ret;
  1218. ret = rsnd_preallocate_pages(rtd, &rdai->capture,
  1219. SNDRV_PCM_STREAM_CAPTURE);
  1220. if (ret)
  1221. return ret;
  1222. return 0;
  1223. }
  1224. static const struct snd_soc_component_driver rsnd_soc_component = {
  1225. .ops = &rsnd_pcm_ops,
  1226. .pcm_new = rsnd_pcm_new,
  1227. .name = "rsnd",
  1228. };
  1229. static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
  1230. struct rsnd_dai_stream *io)
  1231. {
  1232. int ret;
  1233. ret = rsnd_dai_call(probe, io, priv);
  1234. if (ret == -EAGAIN) {
  1235. struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
  1236. struct rsnd_mod *mod;
  1237. int i;
  1238. /*
  1239. * Fallback to PIO mode
  1240. */
  1241. /*
  1242. * call "remove" for SSI/SRC/DVC
  1243. * SSI will be switch to PIO mode if it was DMA mode
  1244. * see
  1245. * rsnd_dma_init()
  1246. * rsnd_ssi_fallback()
  1247. */
  1248. rsnd_dai_call(remove, io, priv);
  1249. /*
  1250. * remove all mod from io
  1251. * and, re connect ssi
  1252. */
  1253. for_each_rsnd_mod(i, mod, io)
  1254. rsnd_dai_disconnect(mod, io, i);
  1255. rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
  1256. /*
  1257. * fallback
  1258. */
  1259. rsnd_dai_call(fallback, io, priv);
  1260. /*
  1261. * retry to "probe".
  1262. * DAI has SSI which is PIO mode only now.
  1263. */
  1264. ret = rsnd_dai_call(probe, io, priv);
  1265. }
  1266. return ret;
  1267. }
  1268. /*
  1269. * rsnd probe
  1270. */
  1271. static int rsnd_probe(struct platform_device *pdev)
  1272. {
  1273. struct rsnd_priv *priv;
  1274. struct device *dev = &pdev->dev;
  1275. struct rsnd_dai *rdai;
  1276. int (*probe_func[])(struct rsnd_priv *priv) = {
  1277. rsnd_gen_probe,
  1278. rsnd_dma_probe,
  1279. rsnd_ssi_probe,
  1280. rsnd_ssiu_probe,
  1281. rsnd_src_probe,
  1282. rsnd_ctu_probe,
  1283. rsnd_mix_probe,
  1284. rsnd_dvc_probe,
  1285. rsnd_cmd_probe,
  1286. rsnd_adg_probe,
  1287. rsnd_dai_probe,
  1288. };
  1289. int ret, i;
  1290. /*
  1291. * init priv data
  1292. */
  1293. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1294. if (!priv)
  1295. return -ENODEV;
  1296. priv->pdev = pdev;
  1297. priv->flags = (unsigned long)of_device_get_match_data(dev);
  1298. spin_lock_init(&priv->lock);
  1299. /*
  1300. * init each module
  1301. */
  1302. for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
  1303. ret = probe_func[i](priv);
  1304. if (ret)
  1305. return ret;
  1306. }
  1307. for_each_rsnd_dai(rdai, priv, i) {
  1308. ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
  1309. if (ret)
  1310. goto exit_snd_probe;
  1311. ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
  1312. if (ret)
  1313. goto exit_snd_probe;
  1314. }
  1315. dev_set_drvdata(dev, priv);
  1316. /*
  1317. * asoc register
  1318. */
  1319. ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
  1320. priv->daidrv, rsnd_rdai_nr(priv));
  1321. if (ret < 0) {
  1322. dev_err(dev, "cannot snd dai register\n");
  1323. goto exit_snd_probe;
  1324. }
  1325. pm_runtime_enable(dev);
  1326. dev_info(dev, "probed\n");
  1327. return ret;
  1328. exit_snd_probe:
  1329. for_each_rsnd_dai(rdai, priv, i) {
  1330. rsnd_dai_call(remove, &rdai->playback, priv);
  1331. rsnd_dai_call(remove, &rdai->capture, priv);
  1332. }
  1333. return ret;
  1334. }
  1335. static int rsnd_remove(struct platform_device *pdev)
  1336. {
  1337. struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
  1338. struct rsnd_dai *rdai;
  1339. void (*remove_func[])(struct rsnd_priv *priv) = {
  1340. rsnd_ssi_remove,
  1341. rsnd_ssiu_remove,
  1342. rsnd_src_remove,
  1343. rsnd_ctu_remove,
  1344. rsnd_mix_remove,
  1345. rsnd_dvc_remove,
  1346. rsnd_cmd_remove,
  1347. rsnd_adg_remove,
  1348. };
  1349. int ret = 0, i;
  1350. snd_soc_disconnect_sync(&pdev->dev);
  1351. pm_runtime_disable(&pdev->dev);
  1352. for_each_rsnd_dai(rdai, priv, i) {
  1353. ret |= rsnd_dai_call(remove, &rdai->playback, priv);
  1354. ret |= rsnd_dai_call(remove, &rdai->capture, priv);
  1355. }
  1356. for (i = 0; i < ARRAY_SIZE(remove_func); i++)
  1357. remove_func[i](priv);
  1358. return ret;
  1359. }
  1360. static int __maybe_unused rsnd_suspend(struct device *dev)
  1361. {
  1362. struct rsnd_priv *priv = dev_get_drvdata(dev);
  1363. rsnd_adg_clk_disable(priv);
  1364. return 0;
  1365. }
  1366. static int __maybe_unused rsnd_resume(struct device *dev)
  1367. {
  1368. struct rsnd_priv *priv = dev_get_drvdata(dev);
  1369. rsnd_adg_clk_enable(priv);
  1370. return 0;
  1371. }
  1372. static const struct dev_pm_ops rsnd_pm_ops = {
  1373. SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
  1374. };
  1375. static struct platform_driver rsnd_driver = {
  1376. .driver = {
  1377. .name = "rcar_sound",
  1378. .pm = &rsnd_pm_ops,
  1379. .of_match_table = rsnd_of_match,
  1380. },
  1381. .probe = rsnd_probe,
  1382. .remove = rsnd_remove,
  1383. };
  1384. module_platform_driver(rsnd_driver);
  1385. MODULE_LICENSE("GPL v2");
  1386. MODULE_DESCRIPTION("Renesas R-Car audio driver");
  1387. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  1388. MODULE_ALIAS("platform:rcar-pcm-audio");