core.c 37 KB

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