soc-topology.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // soc-topology.c -- ALSA SoC Topology
  4. //
  5. // Copyright (C) 2012 Texas Instruments Inc.
  6. // Copyright (C) 2015 Intel Corporation.
  7. //
  8. // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. // K, Mythri P <mythri.p.k@intel.com>
  10. // Prusty, Subhransu S <subhransu.s.prusty@intel.com>
  11. // B, Jayachandran <jayachandran.b@intel.com>
  12. // Abdullah, Omair M <omair.m.abdullah@intel.com>
  13. // Jin, Yao <yao.jin@intel.com>
  14. // Lin, Mengdong <mengdong.lin@intel.com>
  15. //
  16. // Add support to read audio firmware topology alongside firmware text. The
  17. // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
  18. // equalizers, firmware, coefficients etc.
  19. //
  20. // This file only manages the core ALSA and ASoC components, all other bespoke
  21. // firmware topology data is passed to component drivers for bespoke handling.
  22. #include <linux/kernel.h>
  23. #include <linux/export.h>
  24. #include <linux/list.h>
  25. #include <linux/firmware.h>
  26. #include <linux/slab.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/soc-topology.h>
  30. #include <sound/tlv.h>
  31. #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
  32. /*
  33. * We make several passes over the data (since it wont necessarily be ordered)
  34. * and process objects in the following order. This guarantees the component
  35. * drivers will be ready with any vendor data before the mixers and DAPM objects
  36. * are loaded (that may make use of the vendor data).
  37. */
  38. #define SOC_TPLG_PASS_MANIFEST 0
  39. #define SOC_TPLG_PASS_VENDOR 1
  40. #define SOC_TPLG_PASS_CONTROL 2
  41. #define SOC_TPLG_PASS_WIDGET 3
  42. #define SOC_TPLG_PASS_PCM_DAI 4
  43. #define SOC_TPLG_PASS_GRAPH 5
  44. #define SOC_TPLG_PASS_BE_DAI 6
  45. #define SOC_TPLG_PASS_LINK 7
  46. #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
  47. #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
  48. /* topology context */
  49. struct soc_tplg {
  50. const struct firmware *fw;
  51. /* runtime FW parsing */
  52. const u8 *pos; /* read position */
  53. const u8 *hdr_pos; /* header position */
  54. unsigned int pass; /* pass number */
  55. /* component caller */
  56. struct device *dev;
  57. struct snd_soc_component *comp;
  58. u32 index; /* current block index */
  59. /* vendor specific kcontrol operations */
  60. const struct snd_soc_tplg_kcontrol_ops *io_ops;
  61. int io_ops_count;
  62. /* vendor specific bytes ext handlers, for TLV bytes controls */
  63. const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
  64. int bytes_ext_ops_count;
  65. /* optional fw loading callbacks to component drivers */
  66. const struct snd_soc_tplg_ops *ops;
  67. };
  68. /* check we dont overflow the data for this control chunk */
  69. static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
  70. unsigned int count, size_t bytes, const char *elem_type)
  71. {
  72. const u8 *end = tplg->pos + elem_size * count;
  73. if (end > tplg->fw->data + tplg->fw->size) {
  74. dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
  75. elem_type);
  76. return -EINVAL;
  77. }
  78. /* check there is enough room in chunk for control.
  79. extra bytes at the end of control are for vendor data here */
  80. if (elem_size * count > bytes) {
  81. dev_err(tplg->dev,
  82. "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
  83. elem_type, count, elem_size, bytes);
  84. return -EINVAL;
  85. }
  86. return 0;
  87. }
  88. static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
  89. {
  90. const u8 *end = tplg->hdr_pos;
  91. if (end >= tplg->fw->data + tplg->fw->size)
  92. return true;
  93. return false;
  94. }
  95. static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
  96. {
  97. return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
  98. }
  99. static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
  100. {
  101. return (unsigned long)(tplg->pos - tplg->fw->data);
  102. }
  103. /* mapping of Kcontrol types and associated operations. */
  104. static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
  105. {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
  106. snd_soc_put_volsw, snd_soc_info_volsw},
  107. {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
  108. snd_soc_put_volsw_sx, NULL},
  109. {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
  110. snd_soc_put_enum_double, snd_soc_info_enum_double},
  111. {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
  112. snd_soc_put_enum_double, NULL},
  113. {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
  114. snd_soc_bytes_put, snd_soc_bytes_info},
  115. {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
  116. snd_soc_put_volsw_range, snd_soc_info_volsw_range},
  117. {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
  118. snd_soc_put_xr_sx, snd_soc_info_xr_sx},
  119. {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
  120. snd_soc_put_strobe, NULL},
  121. {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
  122. snd_soc_dapm_put_volsw, snd_soc_info_volsw},
  123. {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
  124. snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
  125. {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
  126. snd_soc_dapm_put_enum_double, NULL},
  127. {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
  128. snd_soc_dapm_put_enum_double, NULL},
  129. {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
  130. snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
  131. };
  132. struct soc_tplg_map {
  133. int uid;
  134. int kid;
  135. };
  136. /* mapping of widget types from UAPI IDs to kernel IDs */
  137. static const struct soc_tplg_map dapm_map[] = {
  138. {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
  139. {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
  140. {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
  141. {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
  142. {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
  143. {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
  144. {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
  145. {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
  146. {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
  147. {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
  148. {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
  149. {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
  150. {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
  151. {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
  152. {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
  153. {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
  154. {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
  155. {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
  156. {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
  157. {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
  158. {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
  159. {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
  160. {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
  161. {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
  162. };
  163. static int tplg_chan_get_reg(struct soc_tplg *tplg,
  164. struct snd_soc_tplg_channel *chan, int map)
  165. {
  166. int i;
  167. for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
  168. if (le32_to_cpu(chan[i].id) == map)
  169. return le32_to_cpu(chan[i].reg);
  170. }
  171. return -EINVAL;
  172. }
  173. static int tplg_chan_get_shift(struct soc_tplg *tplg,
  174. struct snd_soc_tplg_channel *chan, int map)
  175. {
  176. int i;
  177. for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
  178. if (le32_to_cpu(chan[i].id) == map)
  179. return le32_to_cpu(chan[i].shift);
  180. }
  181. return -EINVAL;
  182. }
  183. static int get_widget_id(int tplg_type)
  184. {
  185. int i;
  186. for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
  187. if (tplg_type == dapm_map[i].uid)
  188. return dapm_map[i].kid;
  189. }
  190. return -EINVAL;
  191. }
  192. static inline void soc_bind_err(struct soc_tplg *tplg,
  193. struct snd_soc_tplg_ctl_hdr *hdr, int index)
  194. {
  195. dev_err(tplg->dev,
  196. "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
  197. hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
  198. soc_tplg_get_offset(tplg));
  199. }
  200. static inline void soc_control_err(struct soc_tplg *tplg,
  201. struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
  202. {
  203. dev_err(tplg->dev,
  204. "ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
  205. name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
  206. soc_tplg_get_offset(tplg));
  207. }
  208. /* pass vendor data to component driver for processing */
  209. static int soc_tplg_vendor_load(struct soc_tplg *tplg,
  210. struct snd_soc_tplg_hdr *hdr)
  211. {
  212. int ret = 0;
  213. if (tplg->ops && tplg->ops->vendor_load)
  214. ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
  215. else {
  216. dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
  217. hdr->vendor_type);
  218. return -EINVAL;
  219. }
  220. if (ret < 0)
  221. dev_err(tplg->dev,
  222. "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
  223. soc_tplg_get_hdr_offset(tplg),
  224. soc_tplg_get_hdr_offset(tplg),
  225. hdr->type, hdr->vendor_type);
  226. return ret;
  227. }
  228. /* optionally pass new dynamic widget to component driver. This is mainly for
  229. * external widgets where we can assign private data/ops */
  230. static int soc_tplg_widget_load(struct soc_tplg *tplg,
  231. struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
  232. {
  233. if (tplg->ops && tplg->ops->widget_load)
  234. return tplg->ops->widget_load(tplg->comp, tplg->index, w,
  235. tplg_w);
  236. return 0;
  237. }
  238. /* optionally pass new dynamic widget to component driver. This is mainly for
  239. * external widgets where we can assign private data/ops */
  240. static int soc_tplg_widget_ready(struct soc_tplg *tplg,
  241. struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
  242. {
  243. if (tplg->ops && tplg->ops->widget_ready)
  244. return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
  245. tplg_w);
  246. return 0;
  247. }
  248. /* pass DAI configurations to component driver for extra initialization */
  249. static int soc_tplg_dai_load(struct soc_tplg *tplg,
  250. struct snd_soc_dai_driver *dai_drv,
  251. struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
  252. {
  253. if (tplg->ops && tplg->ops->dai_load)
  254. return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
  255. pcm, dai);
  256. return 0;
  257. }
  258. /* pass link configurations to component driver for extra initialization */
  259. static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
  260. struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
  261. {
  262. if (tplg->ops && tplg->ops->link_load)
  263. return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
  264. return 0;
  265. }
  266. /* tell the component driver that all firmware has been loaded in this request */
  267. static int soc_tplg_complete(struct soc_tplg *tplg)
  268. {
  269. if (tplg->ops && tplg->ops->complete)
  270. return tplg->ops->complete(tplg->comp);
  271. return 0;
  272. }
  273. /* add a dynamic kcontrol */
  274. static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
  275. const struct snd_kcontrol_new *control_new, const char *prefix,
  276. void *data, struct snd_kcontrol **kcontrol)
  277. {
  278. int err;
  279. *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
  280. if (*kcontrol == NULL) {
  281. dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
  282. control_new->name);
  283. return -ENOMEM;
  284. }
  285. err = snd_ctl_add(card, *kcontrol);
  286. if (err < 0) {
  287. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  288. control_new->name, err);
  289. return err;
  290. }
  291. return 0;
  292. }
  293. /* add a dynamic kcontrol for component driver */
  294. static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
  295. struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
  296. {
  297. struct snd_soc_component *comp = tplg->comp;
  298. return soc_tplg_add_dcontrol(comp->card->snd_card,
  299. tplg->dev, k, comp->name_prefix, comp, kcontrol);
  300. }
  301. /* remove kcontrol */
  302. static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
  303. int pass)
  304. {
  305. struct snd_card *card = comp->card->snd_card;
  306. if (pass != SOC_TPLG_PASS_CONTROL)
  307. return;
  308. if (dobj->unload)
  309. dobj->unload(comp, dobj);
  310. snd_ctl_remove(card, dobj->control.kcontrol);
  311. list_del(&dobj->list);
  312. }
  313. /* remove a route */
  314. static void soc_tplg_remove_route(struct snd_soc_component *comp,
  315. struct snd_soc_dobj *dobj, int pass)
  316. {
  317. if (pass != SOC_TPLG_PASS_GRAPH)
  318. return;
  319. if (dobj->unload)
  320. dobj->unload(comp, dobj);
  321. list_del(&dobj->list);
  322. }
  323. /* remove a widget and it's kcontrols - routes must be removed first */
  324. static void soc_tplg_remove_widget(struct snd_soc_component *comp,
  325. struct snd_soc_dobj *dobj, int pass)
  326. {
  327. struct snd_card *card = comp->card->snd_card;
  328. struct snd_soc_dapm_widget *w =
  329. container_of(dobj, struct snd_soc_dapm_widget, dobj);
  330. int i;
  331. if (pass != SOC_TPLG_PASS_WIDGET)
  332. return;
  333. if (dobj->unload)
  334. dobj->unload(comp, dobj);
  335. if (w->kcontrols)
  336. for (i = 0; i < w->num_kcontrols; i++)
  337. snd_ctl_remove(card, w->kcontrols[i]);
  338. list_del(&dobj->list);
  339. /* widget w is freed by soc-dapm.c */
  340. }
  341. /* remove DAI configurations */
  342. static void soc_tplg_remove_dai(struct snd_soc_component *comp,
  343. struct snd_soc_dobj *dobj, int pass)
  344. {
  345. struct snd_soc_dai_driver *dai_drv =
  346. container_of(dobj, struct snd_soc_dai_driver, dobj);
  347. struct snd_soc_dai *dai, *_dai;
  348. if (pass != SOC_TPLG_PASS_PCM_DAI)
  349. return;
  350. if (dobj->unload)
  351. dobj->unload(comp, dobj);
  352. for_each_component_dais_safe(comp, dai, _dai)
  353. if (dai->driver == dai_drv)
  354. snd_soc_unregister_dai(dai);
  355. list_del(&dobj->list);
  356. }
  357. /* remove link configurations */
  358. static void soc_tplg_remove_link(struct snd_soc_component *comp,
  359. struct snd_soc_dobj *dobj, int pass)
  360. {
  361. struct snd_soc_dai_link *link =
  362. container_of(dobj, struct snd_soc_dai_link, dobj);
  363. if (pass != SOC_TPLG_PASS_PCM_DAI)
  364. return;
  365. if (dobj->unload)
  366. dobj->unload(comp, dobj);
  367. list_del(&dobj->list);
  368. snd_soc_remove_pcm_runtime(comp->card,
  369. snd_soc_get_pcm_runtime(comp->card, link));
  370. }
  371. /* unload dai link */
  372. static void remove_backend_link(struct snd_soc_component *comp,
  373. struct snd_soc_dobj *dobj, int pass)
  374. {
  375. if (pass != SOC_TPLG_PASS_LINK)
  376. return;
  377. if (dobj->unload)
  378. dobj->unload(comp, dobj);
  379. /*
  380. * We don't free the link here as what soc_tplg_remove_link() do since BE
  381. * links are not allocated by topology.
  382. * We however need to reset the dobj type to its initial values
  383. */
  384. dobj->type = SND_SOC_DOBJ_NONE;
  385. list_del(&dobj->list);
  386. }
  387. /* bind a kcontrol to it's IO handlers */
  388. static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
  389. struct snd_kcontrol_new *k,
  390. const struct soc_tplg *tplg)
  391. {
  392. const struct snd_soc_tplg_kcontrol_ops *ops;
  393. const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
  394. int num_ops, i;
  395. if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
  396. && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
  397. && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
  398. || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
  399. && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  400. struct soc_bytes_ext *sbe;
  401. struct snd_soc_tplg_bytes_control *be;
  402. sbe = (struct soc_bytes_ext *)k->private_value;
  403. be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
  404. /* TLV bytes controls need standard kcontrol info handler,
  405. * TLV callback and extended put/get handlers.
  406. */
  407. k->info = snd_soc_bytes_info_ext;
  408. k->tlv.c = snd_soc_bytes_tlv_callback;
  409. /*
  410. * When a topology-based implementation abuses the
  411. * control interface and uses bytes_ext controls of
  412. * more than 512 bytes, we need to disable the size
  413. * checks, otherwise accesses to such controls will
  414. * return an -EINVAL error and prevent the card from
  415. * being configured.
  416. */
  417. if (sbe->max > 512)
  418. k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
  419. ext_ops = tplg->bytes_ext_ops;
  420. num_ops = tplg->bytes_ext_ops_count;
  421. for (i = 0; i < num_ops; i++) {
  422. if (!sbe->put &&
  423. ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
  424. sbe->put = ext_ops[i].put;
  425. if (!sbe->get &&
  426. ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
  427. sbe->get = ext_ops[i].get;
  428. }
  429. if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
  430. return -EINVAL;
  431. if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
  432. return -EINVAL;
  433. return 0;
  434. }
  435. /* try and map vendor specific kcontrol handlers first */
  436. ops = tplg->io_ops;
  437. num_ops = tplg->io_ops_count;
  438. for (i = 0; i < num_ops; i++) {
  439. if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
  440. k->put = ops[i].put;
  441. if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
  442. k->get = ops[i].get;
  443. if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
  444. k->info = ops[i].info;
  445. }
  446. /* vendor specific handlers found ? */
  447. if (k->put && k->get && k->info)
  448. return 0;
  449. /* none found so try standard kcontrol handlers */
  450. ops = io_ops;
  451. num_ops = ARRAY_SIZE(io_ops);
  452. for (i = 0; i < num_ops; i++) {
  453. if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
  454. k->put = ops[i].put;
  455. if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
  456. k->get = ops[i].get;
  457. if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
  458. k->info = ops[i].info;
  459. }
  460. /* standard handlers found ? */
  461. if (k->put && k->get && k->info)
  462. return 0;
  463. /* nothing to bind */
  464. return -EINVAL;
  465. }
  466. /* bind a widgets to it's evnt handlers */
  467. int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
  468. const struct snd_soc_tplg_widget_events *events,
  469. int num_events, u16 event_type)
  470. {
  471. int i;
  472. w->event = NULL;
  473. for (i = 0; i < num_events; i++) {
  474. if (event_type == events[i].type) {
  475. /* found - so assign event */
  476. w->event = events[i].event_handler;
  477. return 0;
  478. }
  479. }
  480. /* not found */
  481. return -EINVAL;
  482. }
  483. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
  484. /* optionally pass new dynamic kcontrol to component driver. */
  485. static int soc_tplg_control_load(struct soc_tplg *tplg,
  486. struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
  487. {
  488. int ret = 0;
  489. if (tplg->ops && tplg->ops->control_load)
  490. ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
  491. if (ret)
  492. dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
  493. return ret;
  494. }
  495. static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
  496. struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
  497. {
  498. unsigned int item_len = 2 * sizeof(unsigned int);
  499. unsigned int *p;
  500. p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
  501. if (!p)
  502. return -ENOMEM;
  503. p[0] = SNDRV_CTL_TLVT_DB_SCALE;
  504. p[1] = item_len;
  505. p[2] = le32_to_cpu(scale->min);
  506. p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
  507. | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
  508. kc->tlv.p = (void *)p;
  509. return 0;
  510. }
  511. static int soc_tplg_create_tlv(struct soc_tplg *tplg,
  512. struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
  513. {
  514. struct snd_soc_tplg_ctl_tlv *tplg_tlv;
  515. u32 access = le32_to_cpu(tc->access);
  516. if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
  517. return 0;
  518. if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
  519. tplg_tlv = &tc->tlv;
  520. switch (le32_to_cpu(tplg_tlv->type)) {
  521. case SNDRV_CTL_TLVT_DB_SCALE:
  522. return soc_tplg_create_tlv_db_scale(tplg, kc,
  523. &tplg_tlv->scale);
  524. /* TODO: add support for other TLV types */
  525. default:
  526. dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
  527. tplg_tlv->type);
  528. return -EINVAL;
  529. }
  530. }
  531. return 0;
  532. }
  533. static int soc_tplg_control_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
  534. {
  535. struct snd_soc_tplg_mixer_control *mc;
  536. struct soc_mixer_control *sm;
  537. int err;
  538. mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
  539. /* validate kcontrol */
  540. if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  541. return -EINVAL;
  542. sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
  543. if (!sm)
  544. return -ENOMEM;
  545. tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + le32_to_cpu(mc->priv.size);
  546. dev_dbg(tplg->dev, "ASoC: adding mixer kcontrol %s with access 0x%x\n",
  547. mc->hdr.name, mc->hdr.access);
  548. kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
  549. if (!kc->name)
  550. return -ENOMEM;
  551. kc->private_value = (long)sm;
  552. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  553. kc->access = le32_to_cpu(mc->hdr.access);
  554. /* we only support FL/FR channel mapping atm */
  555. sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
  556. sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
  557. sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
  558. sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
  559. sm->max = le32_to_cpu(mc->max);
  560. sm->min = le32_to_cpu(mc->min);
  561. sm->invert = le32_to_cpu(mc->invert);
  562. sm->platform_max = le32_to_cpu(mc->platform_max);
  563. /* map io handlers */
  564. err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
  565. if (err) {
  566. soc_control_err(tplg, &mc->hdr, mc->hdr.name);
  567. return err;
  568. }
  569. /* create any TLV data */
  570. err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
  571. if (err < 0) {
  572. dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
  573. return err;
  574. }
  575. /* pass control to driver for optional further init */
  576. return soc_tplg_control_load(tplg, kc, &mc->hdr);
  577. }
  578. static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
  579. struct snd_soc_tplg_enum_control *ec)
  580. {
  581. int i, ret;
  582. if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
  583. return -EINVAL;
  584. se->dobj.control.dtexts =
  585. devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
  586. if (se->dobj.control.dtexts == NULL)
  587. return -ENOMEM;
  588. for (i = 0; i < le32_to_cpu(ec->items); i++) {
  589. if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  590. SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
  591. ret = -EINVAL;
  592. goto err;
  593. }
  594. se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
  595. if (!se->dobj.control.dtexts[i]) {
  596. ret = -ENOMEM;
  597. goto err;
  598. }
  599. }
  600. se->items = le32_to_cpu(ec->items);
  601. se->texts = (const char * const *)se->dobj.control.dtexts;
  602. return 0;
  603. err:
  604. return ret;
  605. }
  606. static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
  607. struct snd_soc_tplg_enum_control *ec)
  608. {
  609. int i;
  610. /*
  611. * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
  612. * values instead of using ARRAY_SIZE(ec->values) due to the fact that
  613. * it is oversized for its purpose. Additionally it is done so because
  614. * it is defined in UAPI header where it can't be easily changed.
  615. */
  616. if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
  617. return -EINVAL;
  618. se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
  619. sizeof(*se->dobj.control.dvalues),
  620. GFP_KERNEL);
  621. if (!se->dobj.control.dvalues)
  622. return -ENOMEM;
  623. /* convert from little-endian */
  624. for (i = 0; i < le32_to_cpu(ec->items); i++) {
  625. se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
  626. }
  627. se->items = le32_to_cpu(ec->items);
  628. se->values = (const unsigned int *)se->dobj.control.dvalues;
  629. return 0;
  630. }
  631. static int soc_tplg_control_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
  632. {
  633. struct snd_soc_tplg_enum_control *ec;
  634. struct soc_enum *se;
  635. int err;
  636. ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
  637. /* validate kcontrol */
  638. if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  639. return -EINVAL;
  640. se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
  641. if (!se)
  642. return -ENOMEM;
  643. tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + le32_to_cpu(ec->priv.size));
  644. dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", ec->hdr.name, ec->items);
  645. kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
  646. if (!kc->name)
  647. return -ENOMEM;
  648. kc->private_value = (long)se;
  649. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  650. kc->access = le32_to_cpu(ec->hdr.access);
  651. /* we only support FL/FR channel mapping atm */
  652. se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
  653. se->shift_l = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
  654. se->shift_r = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
  655. se->mask = le32_to_cpu(ec->mask);
  656. switch (le32_to_cpu(ec->hdr.ops.info)) {
  657. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  658. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  659. err = soc_tplg_denum_create_values(tplg, se, ec);
  660. if (err < 0) {
  661. dev_err(tplg->dev, "ASoC: could not create values for %s\n", ec->hdr.name);
  662. return err;
  663. }
  664. fallthrough;
  665. case SND_SOC_TPLG_CTL_ENUM:
  666. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  667. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  668. err = soc_tplg_denum_create_texts(tplg, se, ec);
  669. if (err < 0) {
  670. dev_err(tplg->dev, "ASoC: could not create texts for %s\n", ec->hdr.name);
  671. return err;
  672. }
  673. break;
  674. default:
  675. dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
  676. ec->hdr.ops.info, ec->hdr.name);
  677. return -EINVAL;
  678. }
  679. /* map io handlers */
  680. err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
  681. if (err) {
  682. soc_control_err(tplg, &ec->hdr, ec->hdr.name);
  683. return err;
  684. }
  685. /* pass control to driver for optional further init */
  686. return soc_tplg_control_load(tplg, kc, &ec->hdr);
  687. }
  688. static int soc_tplg_control_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
  689. {
  690. struct snd_soc_tplg_bytes_control *be;
  691. struct soc_bytes_ext *sbe;
  692. int err;
  693. be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
  694. /* validate kcontrol */
  695. if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  696. return -EINVAL;
  697. sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
  698. if (!sbe)
  699. return -ENOMEM;
  700. tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + le32_to_cpu(be->priv.size));
  701. dev_dbg(tplg->dev, "ASoC: adding bytes kcontrol %s with access 0x%x\n",
  702. be->hdr.name, be->hdr.access);
  703. kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
  704. if (!kc->name)
  705. return -ENOMEM;
  706. kc->private_value = (long)sbe;
  707. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  708. kc->access = le32_to_cpu(be->hdr.access);
  709. sbe->max = le32_to_cpu(be->max);
  710. /* map standard io handlers and check for external handlers */
  711. err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
  712. if (err) {
  713. soc_control_err(tplg, &be->hdr, be->hdr.name);
  714. return err;
  715. }
  716. /* pass control to driver for optional further init */
  717. return soc_tplg_control_load(tplg, kc, &be->hdr);
  718. }
  719. static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
  720. {
  721. struct snd_kcontrol_new kc = {0};
  722. struct soc_bytes_ext *sbe;
  723. int ret;
  724. if (soc_tplg_check_elem_count(tplg,
  725. sizeof(struct snd_soc_tplg_bytes_control),
  726. 1, size, "mixer bytes"))
  727. return -EINVAL;
  728. ret = soc_tplg_control_dbytes_create(tplg, &kc);
  729. if (ret)
  730. return ret;
  731. /* register dynamic object */
  732. sbe = (struct soc_bytes_ext *)kc.private_value;
  733. INIT_LIST_HEAD(&sbe->dobj.list);
  734. sbe->dobj.type = SND_SOC_DOBJ_BYTES;
  735. sbe->dobj.index = tplg->index;
  736. if (tplg->ops)
  737. sbe->dobj.unload = tplg->ops->control_unload;
  738. /* create control directly */
  739. ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
  740. if (ret < 0)
  741. return ret;
  742. list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
  743. return ret;
  744. }
  745. static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
  746. {
  747. struct snd_kcontrol_new kc = {0};
  748. struct soc_mixer_control *sm;
  749. int ret;
  750. if (soc_tplg_check_elem_count(tplg,
  751. sizeof(struct snd_soc_tplg_mixer_control),
  752. 1, size, "mixers"))
  753. return -EINVAL;
  754. ret = soc_tplg_control_dmixer_create(tplg, &kc);
  755. if (ret)
  756. return ret;
  757. /* register dynamic object */
  758. sm = (struct soc_mixer_control *)kc.private_value;
  759. INIT_LIST_HEAD(&sm->dobj.list);
  760. sm->dobj.type = SND_SOC_DOBJ_MIXER;
  761. sm->dobj.index = tplg->index;
  762. if (tplg->ops)
  763. sm->dobj.unload = tplg->ops->control_unload;
  764. /* create control directly */
  765. ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
  766. if (ret < 0)
  767. return ret;
  768. list_add(&sm->dobj.list, &tplg->comp->dobj_list);
  769. return ret;
  770. }
  771. static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
  772. {
  773. struct snd_kcontrol_new kc = {0};
  774. struct soc_enum *se;
  775. int ret;
  776. if (soc_tplg_check_elem_count(tplg,
  777. sizeof(struct snd_soc_tplg_enum_control),
  778. 1, size, "enums"))
  779. return -EINVAL;
  780. ret = soc_tplg_control_denum_create(tplg, &kc);
  781. if (ret)
  782. return ret;
  783. /* register dynamic object */
  784. se = (struct soc_enum *)kc.private_value;
  785. INIT_LIST_HEAD(&se->dobj.list);
  786. se->dobj.type = SND_SOC_DOBJ_ENUM;
  787. se->dobj.index = tplg->index;
  788. if (tplg->ops)
  789. se->dobj.unload = tplg->ops->control_unload;
  790. /* create control directly */
  791. ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
  792. if (ret < 0)
  793. return ret;
  794. list_add(&se->dobj.list, &tplg->comp->dobj_list);
  795. return ret;
  796. }
  797. static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
  798. struct snd_soc_tplg_hdr *hdr)
  799. {
  800. int ret;
  801. int i;
  802. dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
  803. soc_tplg_get_offset(tplg));
  804. for (i = 0; i < le32_to_cpu(hdr->count); i++) {
  805. struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
  806. if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
  807. dev_err(tplg->dev, "ASoC: invalid control size\n");
  808. return -EINVAL;
  809. }
  810. switch (le32_to_cpu(control_hdr->ops.info)) {
  811. case SND_SOC_TPLG_CTL_VOLSW:
  812. case SND_SOC_TPLG_CTL_STROBE:
  813. case SND_SOC_TPLG_CTL_VOLSW_SX:
  814. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  815. case SND_SOC_TPLG_CTL_RANGE:
  816. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  817. case SND_SOC_TPLG_DAPM_CTL_PIN:
  818. ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
  819. break;
  820. case SND_SOC_TPLG_CTL_ENUM:
  821. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  822. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  823. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  824. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  825. ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
  826. break;
  827. case SND_SOC_TPLG_CTL_BYTES:
  828. ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
  829. break;
  830. default:
  831. soc_bind_err(tplg, control_hdr, i);
  832. return -EINVAL;
  833. }
  834. if (ret < 0) {
  835. dev_err(tplg->dev, "ASoC: invalid control\n");
  836. return ret;
  837. }
  838. }
  839. return 0;
  840. }
  841. /* optionally pass new dynamic kcontrol to component driver. */
  842. static int soc_tplg_add_route(struct soc_tplg *tplg,
  843. struct snd_soc_dapm_route *route)
  844. {
  845. if (tplg->ops && tplg->ops->dapm_route_load)
  846. return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
  847. route);
  848. return 0;
  849. }
  850. static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
  851. struct snd_soc_tplg_hdr *hdr)
  852. {
  853. struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
  854. const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
  855. struct snd_soc_tplg_dapm_graph_elem *elem;
  856. struct snd_soc_dapm_route *route;
  857. int count, i;
  858. int ret = 0;
  859. count = le32_to_cpu(hdr->count);
  860. if (soc_tplg_check_elem_count(tplg,
  861. sizeof(struct snd_soc_tplg_dapm_graph_elem),
  862. count, le32_to_cpu(hdr->payload_size), "graph"))
  863. return -EINVAL;
  864. dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
  865. hdr->index);
  866. for (i = 0; i < count; i++) {
  867. route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
  868. if (!route)
  869. return -ENOMEM;
  870. elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
  871. tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
  872. /* validate routes */
  873. if ((strnlen(elem->source, maxlen) == maxlen) ||
  874. (strnlen(elem->sink, maxlen) == maxlen) ||
  875. (strnlen(elem->control, maxlen) == maxlen)) {
  876. ret = -EINVAL;
  877. break;
  878. }
  879. route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL);
  880. route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL);
  881. if (!route->source || !route->sink) {
  882. ret = -ENOMEM;
  883. break;
  884. }
  885. if (strnlen(elem->control, maxlen) != 0) {
  886. route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL);
  887. if (!route->control) {
  888. ret = -ENOMEM;
  889. break;
  890. }
  891. }
  892. /* add route dobj to dobj_list */
  893. route->dobj.type = SND_SOC_DOBJ_GRAPH;
  894. if (tplg->ops)
  895. route->dobj.unload = tplg->ops->dapm_route_unload;
  896. route->dobj.index = tplg->index;
  897. list_add(&route->dobj.list, &tplg->comp->dobj_list);
  898. ret = soc_tplg_add_route(tplg, route);
  899. if (ret < 0) {
  900. dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
  901. break;
  902. }
  903. ret = snd_soc_dapm_add_routes(dapm, route, 1);
  904. if (ret) {
  905. if (!dapm->card->disable_route_checks) {
  906. dev_err(tplg->dev, "ASoC: dapm_add_routes failed: %d\n", ret);
  907. break;
  908. }
  909. dev_info(tplg->dev,
  910. "ASoC: disable_route_checks set, ignoring dapm_add_routes errors\n");
  911. }
  912. }
  913. return ret;
  914. }
  915. static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
  916. struct snd_soc_tplg_dapm_widget *w)
  917. {
  918. struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
  919. struct snd_soc_dapm_widget template, *widget;
  920. struct snd_soc_tplg_ctl_hdr *control_hdr;
  921. struct snd_soc_card *card = tplg->comp->card;
  922. unsigned int *kcontrol_type = NULL;
  923. struct snd_kcontrol_new *kc;
  924. int mixer_count = 0;
  925. int bytes_count = 0;
  926. int enum_count = 0;
  927. int ret = 0;
  928. int i;
  929. if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  930. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  931. return -EINVAL;
  932. if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  933. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  934. return -EINVAL;
  935. dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
  936. w->name, w->id);
  937. memset(&template, 0, sizeof(template));
  938. /* map user to kernel widget ID */
  939. template.id = get_widget_id(le32_to_cpu(w->id));
  940. if ((int)template.id < 0)
  941. return template.id;
  942. /* strings are allocated here, but used and freed by the widget */
  943. template.name = kstrdup(w->name, GFP_KERNEL);
  944. if (!template.name)
  945. return -ENOMEM;
  946. template.sname = kstrdup(w->sname, GFP_KERNEL);
  947. if (!template.sname) {
  948. ret = -ENOMEM;
  949. goto err;
  950. }
  951. template.reg = le32_to_cpu(w->reg);
  952. template.shift = le32_to_cpu(w->shift);
  953. template.mask = le32_to_cpu(w->mask);
  954. template.subseq = le32_to_cpu(w->subseq);
  955. template.on_val = w->invert ? 0 : 1;
  956. template.off_val = w->invert ? 1 : 0;
  957. template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
  958. template.event_flags = le16_to_cpu(w->event_flags);
  959. template.dobj.index = tplg->index;
  960. tplg->pos +=
  961. (sizeof(struct snd_soc_tplg_dapm_widget) +
  962. le32_to_cpu(w->priv.size));
  963. if (w->num_kcontrols == 0) {
  964. template.num_kcontrols = 0;
  965. goto widget;
  966. }
  967. template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
  968. kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
  969. if (!kc) {
  970. ret = -ENOMEM;
  971. goto hdr_err;
  972. }
  973. kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
  974. GFP_KERNEL);
  975. if (!kcontrol_type) {
  976. ret = -ENOMEM;
  977. goto hdr_err;
  978. }
  979. for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
  980. control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
  981. switch (le32_to_cpu(control_hdr->ops.info)) {
  982. case SND_SOC_TPLG_CTL_VOLSW:
  983. case SND_SOC_TPLG_CTL_STROBE:
  984. case SND_SOC_TPLG_CTL_VOLSW_SX:
  985. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  986. case SND_SOC_TPLG_CTL_RANGE:
  987. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  988. /* volume mixer */
  989. kc[i].index = mixer_count;
  990. kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
  991. mixer_count++;
  992. ret = soc_tplg_control_dmixer_create(tplg, &kc[i]);
  993. if (ret < 0)
  994. goto hdr_err;
  995. break;
  996. case SND_SOC_TPLG_CTL_ENUM:
  997. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  998. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  999. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  1000. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  1001. /* enumerated mixer */
  1002. kc[i].index = enum_count;
  1003. kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
  1004. enum_count++;
  1005. ret = soc_tplg_control_denum_create(tplg, &kc[i]);
  1006. if (ret < 0)
  1007. goto hdr_err;
  1008. break;
  1009. case SND_SOC_TPLG_CTL_BYTES:
  1010. /* bytes control */
  1011. kc[i].index = bytes_count;
  1012. kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
  1013. bytes_count++;
  1014. ret = soc_tplg_control_dbytes_create(tplg, &kc[i]);
  1015. if (ret < 0)
  1016. goto hdr_err;
  1017. break;
  1018. default:
  1019. dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
  1020. control_hdr->ops.get, control_hdr->ops.put,
  1021. le32_to_cpu(control_hdr->ops.info));
  1022. ret = -EINVAL;
  1023. goto hdr_err;
  1024. }
  1025. }
  1026. template.kcontrol_news = kc;
  1027. dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
  1028. w->name, mixer_count, enum_count, bytes_count);
  1029. widget:
  1030. ret = soc_tplg_widget_load(tplg, &template, w);
  1031. if (ret < 0)
  1032. goto hdr_err;
  1033. /* card dapm mutex is held by the core if we are loading topology
  1034. * data during sound card init. */
  1035. if (snd_soc_card_is_instantiated(card))
  1036. widget = snd_soc_dapm_new_control(dapm, &template);
  1037. else
  1038. widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
  1039. if (IS_ERR(widget)) {
  1040. ret = PTR_ERR(widget);
  1041. goto hdr_err;
  1042. }
  1043. widget->dobj.type = SND_SOC_DOBJ_WIDGET;
  1044. widget->dobj.widget.kcontrol_type = kcontrol_type;
  1045. if (tplg->ops)
  1046. widget->dobj.unload = tplg->ops->widget_unload;
  1047. widget->dobj.index = tplg->index;
  1048. list_add(&widget->dobj.list, &tplg->comp->dobj_list);
  1049. ret = soc_tplg_widget_ready(tplg, widget, w);
  1050. if (ret < 0)
  1051. goto ready_err;
  1052. kfree(template.sname);
  1053. kfree(template.name);
  1054. return 0;
  1055. ready_err:
  1056. soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
  1057. snd_soc_dapm_free_widget(widget);
  1058. hdr_err:
  1059. kfree(template.sname);
  1060. err:
  1061. kfree(template.name);
  1062. return ret;
  1063. }
  1064. static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
  1065. struct snd_soc_tplg_hdr *hdr)
  1066. {
  1067. int count, i;
  1068. count = le32_to_cpu(hdr->count);
  1069. dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
  1070. for (i = 0; i < count; i++) {
  1071. struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
  1072. int ret;
  1073. /*
  1074. * check if widget itself fits within topology file
  1075. * use sizeof instead of widget->size, as we can't be sure
  1076. * it is set properly yet (file may end before it is present)
  1077. */
  1078. if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
  1079. dev_err(tplg->dev, "ASoC: invalid widget data size\n");
  1080. return -EINVAL;
  1081. }
  1082. /* check if widget has proper size */
  1083. if (le32_to_cpu(widget->size) != sizeof(*widget)) {
  1084. dev_err(tplg->dev, "ASoC: invalid widget size\n");
  1085. return -EINVAL;
  1086. }
  1087. /* check if widget private data fits within topology file */
  1088. if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
  1089. dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
  1090. return -EINVAL;
  1091. }
  1092. ret = soc_tplg_dapm_widget_create(tplg, widget);
  1093. if (ret < 0) {
  1094. dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
  1095. widget->name);
  1096. return ret;
  1097. }
  1098. }
  1099. return 0;
  1100. }
  1101. static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
  1102. {
  1103. struct snd_soc_card *card = tplg->comp->card;
  1104. int ret;
  1105. /* Card might not have been registered at this point.
  1106. * If so, just return success.
  1107. */
  1108. if (!snd_soc_card_is_instantiated(card)) {
  1109. dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
  1110. return 0;
  1111. }
  1112. ret = snd_soc_dapm_new_widgets(card);
  1113. if (ret < 0)
  1114. dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
  1115. return ret;
  1116. }
  1117. static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
  1118. struct snd_soc_tplg_stream_caps *caps)
  1119. {
  1120. stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
  1121. if (!stream->stream_name)
  1122. return -ENOMEM;
  1123. stream->channels_min = le32_to_cpu(caps->channels_min);
  1124. stream->channels_max = le32_to_cpu(caps->channels_max);
  1125. stream->rates = le32_to_cpu(caps->rates);
  1126. stream->rate_min = le32_to_cpu(caps->rate_min);
  1127. stream->rate_max = le32_to_cpu(caps->rate_max);
  1128. stream->formats = le64_to_cpu(caps->formats);
  1129. stream->sig_bits = le32_to_cpu(caps->sig_bits);
  1130. return 0;
  1131. }
  1132. static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
  1133. unsigned int flag_mask, unsigned int flags)
  1134. {
  1135. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
  1136. dai_drv->symmetric_rate =
  1137. (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
  1138. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
  1139. dai_drv->symmetric_channels =
  1140. (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
  1141. 1 : 0;
  1142. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
  1143. dai_drv->symmetric_sample_bits =
  1144. (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
  1145. 1 : 0;
  1146. }
  1147. static const struct snd_soc_dai_ops tplg_dai_ops = {
  1148. .compress_new = snd_soc_new_compress,
  1149. };
  1150. static int soc_tplg_dai_create(struct soc_tplg *tplg,
  1151. struct snd_soc_tplg_pcm *pcm)
  1152. {
  1153. struct snd_soc_dai_driver *dai_drv;
  1154. struct snd_soc_pcm_stream *stream;
  1155. struct snd_soc_tplg_stream_caps *caps;
  1156. struct snd_soc_dai *dai;
  1157. struct snd_soc_dapm_context *dapm =
  1158. snd_soc_component_get_dapm(tplg->comp);
  1159. int ret;
  1160. dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  1161. if (dai_drv == NULL)
  1162. return -ENOMEM;
  1163. if (strlen(pcm->dai_name)) {
  1164. dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
  1165. if (!dai_drv->name) {
  1166. ret = -ENOMEM;
  1167. goto err;
  1168. }
  1169. }
  1170. dai_drv->id = le32_to_cpu(pcm->dai_id);
  1171. if (pcm->playback) {
  1172. stream = &dai_drv->playback;
  1173. caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1174. ret = set_stream_info(tplg, stream, caps);
  1175. if (ret < 0)
  1176. goto err;
  1177. }
  1178. if (pcm->capture) {
  1179. stream = &dai_drv->capture;
  1180. caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1181. ret = set_stream_info(tplg, stream, caps);
  1182. if (ret < 0)
  1183. goto err;
  1184. }
  1185. if (pcm->compress)
  1186. dai_drv->ops = &tplg_dai_ops;
  1187. /* pass control to component driver for optional further init */
  1188. ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
  1189. if (ret < 0) {
  1190. dev_err(tplg->dev, "ASoC: DAI loading failed\n");
  1191. goto err;
  1192. }
  1193. dai_drv->dobj.index = tplg->index;
  1194. dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
  1195. if (tplg->ops)
  1196. dai_drv->dobj.unload = tplg->ops->dai_unload;
  1197. list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
  1198. /* register the DAI to the component */
  1199. dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
  1200. if (!dai)
  1201. return -ENOMEM;
  1202. /* Create the DAI widgets here */
  1203. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  1204. if (ret != 0) {
  1205. dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
  1206. snd_soc_unregister_dai(dai);
  1207. return ret;
  1208. }
  1209. return 0;
  1210. err:
  1211. return ret;
  1212. }
  1213. static void set_link_flags(struct snd_soc_dai_link *link,
  1214. unsigned int flag_mask, unsigned int flags)
  1215. {
  1216. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
  1217. link->symmetric_rate =
  1218. (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
  1219. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
  1220. link->symmetric_channels =
  1221. (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
  1222. 1 : 0;
  1223. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
  1224. link->symmetric_sample_bits =
  1225. (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
  1226. 1 : 0;
  1227. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
  1228. link->ignore_suspend =
  1229. (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
  1230. 1 : 0;
  1231. }
  1232. /* create the FE DAI link */
  1233. static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
  1234. struct snd_soc_tplg_pcm *pcm)
  1235. {
  1236. struct snd_soc_dai_link *link;
  1237. struct snd_soc_dai_link_component *dlc;
  1238. int ret;
  1239. /* link + cpu + codec + platform */
  1240. link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
  1241. if (link == NULL)
  1242. return -ENOMEM;
  1243. dlc = (struct snd_soc_dai_link_component *)(link + 1);
  1244. link->cpus = &dlc[0];
  1245. link->num_cpus = 1;
  1246. link->dobj.index = tplg->index;
  1247. link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
  1248. if (tplg->ops)
  1249. link->dobj.unload = tplg->ops->link_unload;
  1250. if (strlen(pcm->pcm_name)) {
  1251. link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
  1252. link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
  1253. if (!link->name || !link->stream_name) {
  1254. ret = -ENOMEM;
  1255. goto err;
  1256. }
  1257. }
  1258. link->id = le32_to_cpu(pcm->pcm_id);
  1259. if (strlen(pcm->dai_name)) {
  1260. link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
  1261. if (!link->cpus->dai_name) {
  1262. ret = -ENOMEM;
  1263. goto err;
  1264. }
  1265. }
  1266. /*
  1267. * Many topology are assuming link has Codec / Platform, and
  1268. * these might be overwritten at soc_tplg_dai_link_load().
  1269. * Don't use &snd_soc_dummy_dlc here.
  1270. */
  1271. link->codecs = &dlc[1]; /* Don't use &snd_soc_dummy_dlc here */
  1272. link->codecs->name = "snd-soc-dummy";
  1273. link->codecs->dai_name = "snd-soc-dummy-dai";
  1274. link->num_codecs = 1;
  1275. link->platforms = &dlc[2]; /* Don't use &snd_soc_dummy_dlc here */
  1276. link->platforms->name = "snd-soc-dummy";
  1277. link->num_platforms = 1;
  1278. /* enable DPCM */
  1279. link->dynamic = 1;
  1280. link->ignore_pmdown_time = 1;
  1281. link->dpcm_playback = le32_to_cpu(pcm->playback);
  1282. link->dpcm_capture = le32_to_cpu(pcm->capture);
  1283. if (pcm->flag_mask)
  1284. set_link_flags(link,
  1285. le32_to_cpu(pcm->flag_mask),
  1286. le32_to_cpu(pcm->flags));
  1287. /* pass control to component driver for optional further init */
  1288. ret = soc_tplg_dai_link_load(tplg, link, NULL);
  1289. if (ret < 0) {
  1290. dev_err(tplg->dev, "ASoC: FE link loading failed\n");
  1291. goto err;
  1292. }
  1293. ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
  1294. if (ret < 0) {
  1295. if (ret != -EPROBE_DEFER)
  1296. dev_err(tplg->dev, "ASoC: adding FE link failed\n");
  1297. goto err;
  1298. }
  1299. list_add(&link->dobj.list, &tplg->comp->dobj_list);
  1300. return 0;
  1301. err:
  1302. return ret;
  1303. }
  1304. /* create a FE DAI and DAI link from the PCM object */
  1305. static int soc_tplg_pcm_create(struct soc_tplg *tplg,
  1306. struct snd_soc_tplg_pcm *pcm)
  1307. {
  1308. int ret;
  1309. ret = soc_tplg_dai_create(tplg, pcm);
  1310. if (ret < 0)
  1311. return ret;
  1312. return soc_tplg_fe_link_create(tplg, pcm);
  1313. }
  1314. static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
  1315. struct snd_soc_tplg_hdr *hdr)
  1316. {
  1317. struct snd_soc_tplg_pcm *pcm;
  1318. int count;
  1319. int size;
  1320. int i;
  1321. int ret;
  1322. count = le32_to_cpu(hdr->count);
  1323. /* check the element size and count */
  1324. pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
  1325. size = le32_to_cpu(pcm->size);
  1326. if (size > sizeof(struct snd_soc_tplg_pcm)) {
  1327. dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
  1328. size);
  1329. return -EINVAL;
  1330. }
  1331. if (soc_tplg_check_elem_count(tplg,
  1332. size, count,
  1333. le32_to_cpu(hdr->payload_size),
  1334. "PCM DAI"))
  1335. return -EINVAL;
  1336. for (i = 0; i < count; i++) {
  1337. pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
  1338. size = le32_to_cpu(pcm->size);
  1339. /* check ABI version by size, create a new version of pcm
  1340. * if abi not match.
  1341. */
  1342. if (size != sizeof(*pcm))
  1343. return -EINVAL;
  1344. /* create the FE DAIs and DAI links */
  1345. ret = soc_tplg_pcm_create(tplg, pcm);
  1346. if (ret < 0)
  1347. return ret;
  1348. /* offset by version-specific struct size and
  1349. * real priv data size
  1350. */
  1351. tplg->pos += size + le32_to_cpu(pcm->priv.size);
  1352. }
  1353. dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
  1354. return 0;
  1355. }
  1356. /**
  1357. * set_link_hw_format - Set the HW audio format of the physical DAI link.
  1358. * @link: &snd_soc_dai_link which should be updated
  1359. * @cfg: physical link configs.
  1360. *
  1361. * Topology context contains a list of supported HW formats (configs) and
  1362. * a default format ID for the physical link. This function will use this
  1363. * default ID to choose the HW format to set the link's DAI format for init.
  1364. */
  1365. static void set_link_hw_format(struct snd_soc_dai_link *link,
  1366. struct snd_soc_tplg_link_config *cfg)
  1367. {
  1368. struct snd_soc_tplg_hw_config *hw_config;
  1369. unsigned char bclk_provider, fsync_provider;
  1370. unsigned char invert_bclk, invert_fsync;
  1371. int i;
  1372. for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
  1373. hw_config = &cfg->hw_config[i];
  1374. if (hw_config->id != cfg->default_hw_config_id)
  1375. continue;
  1376. link->dai_fmt = le32_to_cpu(hw_config->fmt) &
  1377. SND_SOC_DAIFMT_FORMAT_MASK;
  1378. /* clock gating */
  1379. switch (hw_config->clock_gated) {
  1380. case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
  1381. link->dai_fmt |= SND_SOC_DAIFMT_GATED;
  1382. break;
  1383. case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
  1384. link->dai_fmt |= SND_SOC_DAIFMT_CONT;
  1385. break;
  1386. default:
  1387. /* ignore the value */
  1388. break;
  1389. }
  1390. /* clock signal polarity */
  1391. invert_bclk = hw_config->invert_bclk;
  1392. invert_fsync = hw_config->invert_fsync;
  1393. if (!invert_bclk && !invert_fsync)
  1394. link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
  1395. else if (!invert_bclk && invert_fsync)
  1396. link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
  1397. else if (invert_bclk && !invert_fsync)
  1398. link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
  1399. else
  1400. link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
  1401. /* clock masters */
  1402. bclk_provider = (hw_config->bclk_provider ==
  1403. SND_SOC_TPLG_BCLK_CP);
  1404. fsync_provider = (hw_config->fsync_provider ==
  1405. SND_SOC_TPLG_FSYNC_CP);
  1406. if (bclk_provider && fsync_provider)
  1407. link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
  1408. else if (!bclk_provider && fsync_provider)
  1409. link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
  1410. else if (bclk_provider && !fsync_provider)
  1411. link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
  1412. else
  1413. link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
  1414. }
  1415. }
  1416. /**
  1417. * snd_soc_find_dai_link - Find a DAI link
  1418. *
  1419. * @card: soc card
  1420. * @id: DAI link ID to match
  1421. * @name: DAI link name to match, optional
  1422. * @stream_name: DAI link stream name to match, optional
  1423. *
  1424. * This function will search all existing DAI links of the soc card to
  1425. * find the link of the same ID. Since DAI links may not have their
  1426. * unique ID, so name and stream name should also match if being
  1427. * specified.
  1428. *
  1429. * Return: pointer of DAI link, or NULL if not found.
  1430. */
  1431. static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
  1432. int id, const char *name,
  1433. const char *stream_name)
  1434. {
  1435. struct snd_soc_pcm_runtime *rtd;
  1436. for_each_card_rtds(card, rtd) {
  1437. struct snd_soc_dai_link *link = rtd->dai_link;
  1438. if (link->id != id)
  1439. continue;
  1440. if (name && (!link->name || !strstr(link->name, name)))
  1441. continue;
  1442. if (stream_name && (!link->stream_name ||
  1443. !strstr(link->stream_name, stream_name)))
  1444. continue;
  1445. return link;
  1446. }
  1447. return NULL;
  1448. }
  1449. /* Find and configure an existing physical DAI link */
  1450. static int soc_tplg_link_config(struct soc_tplg *tplg,
  1451. struct snd_soc_tplg_link_config *cfg)
  1452. {
  1453. struct snd_soc_dai_link *link;
  1454. const char *name, *stream_name;
  1455. size_t len;
  1456. int ret;
  1457. len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1458. if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1459. return -EINVAL;
  1460. else if (len)
  1461. name = cfg->name;
  1462. else
  1463. name = NULL;
  1464. len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1465. if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1466. return -EINVAL;
  1467. else if (len)
  1468. stream_name = cfg->stream_name;
  1469. else
  1470. stream_name = NULL;
  1471. link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
  1472. name, stream_name);
  1473. if (!link) {
  1474. dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
  1475. name, cfg->id);
  1476. return -EINVAL;
  1477. }
  1478. /* hw format */
  1479. if (cfg->num_hw_configs)
  1480. set_link_hw_format(link, cfg);
  1481. /* flags */
  1482. if (cfg->flag_mask)
  1483. set_link_flags(link,
  1484. le32_to_cpu(cfg->flag_mask),
  1485. le32_to_cpu(cfg->flags));
  1486. /* pass control to component driver for optional further init */
  1487. ret = soc_tplg_dai_link_load(tplg, link, cfg);
  1488. if (ret < 0) {
  1489. dev_err(tplg->dev, "ASoC: physical link loading failed\n");
  1490. return ret;
  1491. }
  1492. /* for unloading it in snd_soc_tplg_component_remove */
  1493. link->dobj.index = tplg->index;
  1494. link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
  1495. if (tplg->ops)
  1496. link->dobj.unload = tplg->ops->link_unload;
  1497. list_add(&link->dobj.list, &tplg->comp->dobj_list);
  1498. return 0;
  1499. }
  1500. /* Load physical link config elements from the topology context */
  1501. static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
  1502. struct snd_soc_tplg_hdr *hdr)
  1503. {
  1504. struct snd_soc_tplg_link_config *link;
  1505. int count;
  1506. int size;
  1507. int i, ret;
  1508. count = le32_to_cpu(hdr->count);
  1509. /* check the element size and count */
  1510. link = (struct snd_soc_tplg_link_config *)tplg->pos;
  1511. size = le32_to_cpu(link->size);
  1512. if (size > sizeof(struct snd_soc_tplg_link_config)) {
  1513. dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
  1514. size);
  1515. return -EINVAL;
  1516. }
  1517. if (soc_tplg_check_elem_count(tplg, size, count,
  1518. le32_to_cpu(hdr->payload_size),
  1519. "physical link config"))
  1520. return -EINVAL;
  1521. /* config physical DAI links */
  1522. for (i = 0; i < count; i++) {
  1523. link = (struct snd_soc_tplg_link_config *)tplg->pos;
  1524. size = le32_to_cpu(link->size);
  1525. if (size != sizeof(*link))
  1526. return -EINVAL;
  1527. ret = soc_tplg_link_config(tplg, link);
  1528. if (ret < 0)
  1529. return ret;
  1530. /* offset by version-specific struct size and
  1531. * real priv data size
  1532. */
  1533. tplg->pos += size + le32_to_cpu(link->priv.size);
  1534. }
  1535. return 0;
  1536. }
  1537. /**
  1538. * soc_tplg_dai_config - Find and configure an existing physical DAI.
  1539. * @tplg: topology context
  1540. * @d: physical DAI configs.
  1541. *
  1542. * The physical dai should already be registered by the platform driver.
  1543. * The platform driver should specify the DAI name and ID for matching.
  1544. */
  1545. static int soc_tplg_dai_config(struct soc_tplg *tplg,
  1546. struct snd_soc_tplg_dai *d)
  1547. {
  1548. struct snd_soc_dai_link_component dai_component;
  1549. struct snd_soc_dai *dai;
  1550. struct snd_soc_dai_driver *dai_drv;
  1551. struct snd_soc_pcm_stream *stream;
  1552. struct snd_soc_tplg_stream_caps *caps;
  1553. int ret;
  1554. memset(&dai_component, 0, sizeof(dai_component));
  1555. dai_component.dai_name = d->dai_name;
  1556. dai = snd_soc_find_dai(&dai_component);
  1557. if (!dai) {
  1558. dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
  1559. d->dai_name);
  1560. return -EINVAL;
  1561. }
  1562. if (le32_to_cpu(d->dai_id) != dai->id) {
  1563. dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
  1564. d->dai_name);
  1565. return -EINVAL;
  1566. }
  1567. dai_drv = dai->driver;
  1568. if (!dai_drv)
  1569. return -EINVAL;
  1570. if (d->playback) {
  1571. stream = &dai_drv->playback;
  1572. caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1573. ret = set_stream_info(tplg, stream, caps);
  1574. if (ret < 0)
  1575. return ret;
  1576. }
  1577. if (d->capture) {
  1578. stream = &dai_drv->capture;
  1579. caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1580. ret = set_stream_info(tplg, stream, caps);
  1581. if (ret < 0)
  1582. return ret;
  1583. }
  1584. if (d->flag_mask)
  1585. set_dai_flags(dai_drv,
  1586. le32_to_cpu(d->flag_mask),
  1587. le32_to_cpu(d->flags));
  1588. /* pass control to component driver for optional further init */
  1589. ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
  1590. if (ret < 0) {
  1591. dev_err(tplg->dev, "ASoC: DAI loading failed\n");
  1592. return ret;
  1593. }
  1594. return 0;
  1595. }
  1596. /* load physical DAI elements */
  1597. static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
  1598. struct snd_soc_tplg_hdr *hdr)
  1599. {
  1600. int count;
  1601. int i;
  1602. count = le32_to_cpu(hdr->count);
  1603. /* config the existing BE DAIs */
  1604. for (i = 0; i < count; i++) {
  1605. struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
  1606. int ret;
  1607. if (le32_to_cpu(dai->size) != sizeof(*dai)) {
  1608. dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
  1609. return -EINVAL;
  1610. }
  1611. ret = soc_tplg_dai_config(tplg, dai);
  1612. if (ret < 0) {
  1613. dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
  1614. return ret;
  1615. }
  1616. tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
  1617. }
  1618. dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
  1619. return 0;
  1620. }
  1621. static int soc_tplg_manifest_load(struct soc_tplg *tplg,
  1622. struct snd_soc_tplg_hdr *hdr)
  1623. {
  1624. struct snd_soc_tplg_manifest *manifest;
  1625. int ret = 0;
  1626. manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
  1627. /* check ABI version by size, create a new manifest if abi not match */
  1628. if (le32_to_cpu(manifest->size) != sizeof(*manifest))
  1629. return -EINVAL;
  1630. /* pass control to component driver for optional further init */
  1631. if (tplg->ops && tplg->ops->manifest)
  1632. ret = tplg->ops->manifest(tplg->comp, tplg->index, manifest);
  1633. return ret;
  1634. }
  1635. /* validate header magic, size and type */
  1636. static int soc_tplg_valid_header(struct soc_tplg *tplg,
  1637. struct snd_soc_tplg_hdr *hdr)
  1638. {
  1639. if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
  1640. dev_err(tplg->dev,
  1641. "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
  1642. le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
  1643. tplg->fw->size);
  1644. return -EINVAL;
  1645. }
  1646. if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
  1647. dev_err(tplg->dev,
  1648. "ASoC: invalid header of type %d at offset %ld payload_size %d\n",
  1649. le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
  1650. hdr->payload_size);
  1651. return -EINVAL;
  1652. }
  1653. /* big endian firmware objects not supported atm */
  1654. if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
  1655. dev_err(tplg->dev,
  1656. "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
  1657. tplg->pass, hdr->magic,
  1658. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1659. return -EINVAL;
  1660. }
  1661. if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
  1662. dev_err(tplg->dev,
  1663. "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
  1664. tplg->pass, hdr->magic,
  1665. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1666. return -EINVAL;
  1667. }
  1668. /* Support ABI from version 4 */
  1669. if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
  1670. le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
  1671. dev_err(tplg->dev,
  1672. "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
  1673. tplg->pass, hdr->abi,
  1674. SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
  1675. tplg->fw->size);
  1676. return -EINVAL;
  1677. }
  1678. if (hdr->payload_size == 0) {
  1679. dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
  1680. soc_tplg_get_hdr_offset(tplg));
  1681. return -EINVAL;
  1682. }
  1683. return 0;
  1684. }
  1685. /* check header type and call appropriate handler */
  1686. static int soc_tplg_load_header(struct soc_tplg *tplg,
  1687. struct snd_soc_tplg_hdr *hdr)
  1688. {
  1689. int (*elem_load)(struct soc_tplg *tplg,
  1690. struct snd_soc_tplg_hdr *hdr);
  1691. unsigned int hdr_pass;
  1692. tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
  1693. tplg->index = le32_to_cpu(hdr->index);
  1694. switch (le32_to_cpu(hdr->type)) {
  1695. case SND_SOC_TPLG_TYPE_MIXER:
  1696. case SND_SOC_TPLG_TYPE_ENUM:
  1697. case SND_SOC_TPLG_TYPE_BYTES:
  1698. hdr_pass = SOC_TPLG_PASS_CONTROL;
  1699. elem_load = soc_tplg_kcontrol_elems_load;
  1700. break;
  1701. case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
  1702. hdr_pass = SOC_TPLG_PASS_GRAPH;
  1703. elem_load = soc_tplg_dapm_graph_elems_load;
  1704. break;
  1705. case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
  1706. hdr_pass = SOC_TPLG_PASS_WIDGET;
  1707. elem_load = soc_tplg_dapm_widget_elems_load;
  1708. break;
  1709. case SND_SOC_TPLG_TYPE_PCM:
  1710. hdr_pass = SOC_TPLG_PASS_PCM_DAI;
  1711. elem_load = soc_tplg_pcm_elems_load;
  1712. break;
  1713. case SND_SOC_TPLG_TYPE_DAI:
  1714. hdr_pass = SOC_TPLG_PASS_BE_DAI;
  1715. elem_load = soc_tplg_dai_elems_load;
  1716. break;
  1717. case SND_SOC_TPLG_TYPE_DAI_LINK:
  1718. case SND_SOC_TPLG_TYPE_BACKEND_LINK:
  1719. /* physical link configurations */
  1720. hdr_pass = SOC_TPLG_PASS_LINK;
  1721. elem_load = soc_tplg_link_elems_load;
  1722. break;
  1723. case SND_SOC_TPLG_TYPE_MANIFEST:
  1724. hdr_pass = SOC_TPLG_PASS_MANIFEST;
  1725. elem_load = soc_tplg_manifest_load;
  1726. break;
  1727. default:
  1728. /* bespoke vendor data object */
  1729. hdr_pass = SOC_TPLG_PASS_VENDOR;
  1730. elem_load = soc_tplg_vendor_load;
  1731. break;
  1732. }
  1733. if (tplg->pass == hdr_pass) {
  1734. dev_dbg(tplg->dev,
  1735. "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
  1736. hdr->payload_size, hdr->type, hdr->version,
  1737. hdr->vendor_type, tplg->pass);
  1738. return elem_load(tplg, hdr);
  1739. }
  1740. return 0;
  1741. }
  1742. /* process the topology file headers */
  1743. static int soc_tplg_process_headers(struct soc_tplg *tplg)
  1744. {
  1745. int ret;
  1746. /* process the header types from start to end */
  1747. for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
  1748. struct snd_soc_tplg_hdr *hdr;
  1749. tplg->hdr_pos = tplg->fw->data;
  1750. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  1751. while (!soc_tplg_is_eof(tplg)) {
  1752. /* make sure header is valid before loading */
  1753. ret = soc_tplg_valid_header(tplg, hdr);
  1754. if (ret < 0)
  1755. return ret;
  1756. /* load the header object */
  1757. ret = soc_tplg_load_header(tplg, hdr);
  1758. if (ret < 0) {
  1759. if (ret != -EPROBE_DEFER) {
  1760. dev_err(tplg->dev,
  1761. "ASoC: topology: could not load header: %d\n",
  1762. ret);
  1763. }
  1764. return ret;
  1765. }
  1766. /* goto next header */
  1767. tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
  1768. sizeof(struct snd_soc_tplg_hdr);
  1769. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  1770. }
  1771. }
  1772. /* signal DAPM we are complete */
  1773. ret = soc_tplg_dapm_complete(tplg);
  1774. return ret;
  1775. }
  1776. static int soc_tplg_load(struct soc_tplg *tplg)
  1777. {
  1778. int ret;
  1779. ret = soc_tplg_process_headers(tplg);
  1780. if (ret == 0)
  1781. return soc_tplg_complete(tplg);
  1782. return ret;
  1783. }
  1784. /* load audio component topology from "firmware" file */
  1785. int snd_soc_tplg_component_load(struct snd_soc_component *comp,
  1786. const struct snd_soc_tplg_ops *ops, const struct firmware *fw)
  1787. {
  1788. struct soc_tplg tplg;
  1789. int ret;
  1790. /*
  1791. * check if we have sane parameters:
  1792. * comp - needs to exist to keep and reference data while parsing
  1793. * comp->card - used for setting card related parameters
  1794. * comp->card->dev - used for resource management and prints
  1795. * fw - we need it, as it is the very thing we parse
  1796. */
  1797. if (!comp || !comp->card || !comp->card->dev || !fw)
  1798. return -EINVAL;
  1799. /* setup parsing context */
  1800. memset(&tplg, 0, sizeof(tplg));
  1801. tplg.fw = fw;
  1802. tplg.dev = comp->card->dev;
  1803. tplg.comp = comp;
  1804. if (ops) {
  1805. tplg.ops = ops;
  1806. tplg.io_ops = ops->io_ops;
  1807. tplg.io_ops_count = ops->io_ops_count;
  1808. tplg.bytes_ext_ops = ops->bytes_ext_ops;
  1809. tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
  1810. }
  1811. ret = soc_tplg_load(&tplg);
  1812. /* free the created components if fail to load topology */
  1813. if (ret)
  1814. snd_soc_tplg_component_remove(comp);
  1815. return ret;
  1816. }
  1817. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
  1818. /* remove dynamic controls from the component driver */
  1819. int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
  1820. {
  1821. struct snd_soc_dobj *dobj, *next_dobj;
  1822. int pass;
  1823. /* process the header types from end to start */
  1824. for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
  1825. /* remove mixer controls */
  1826. list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
  1827. list) {
  1828. switch (dobj->type) {
  1829. case SND_SOC_DOBJ_BYTES:
  1830. case SND_SOC_DOBJ_ENUM:
  1831. case SND_SOC_DOBJ_MIXER:
  1832. soc_tplg_remove_kcontrol(comp, dobj, pass);
  1833. break;
  1834. case SND_SOC_DOBJ_GRAPH:
  1835. soc_tplg_remove_route(comp, dobj, pass);
  1836. break;
  1837. case SND_SOC_DOBJ_WIDGET:
  1838. soc_tplg_remove_widget(comp, dobj, pass);
  1839. break;
  1840. case SND_SOC_DOBJ_PCM:
  1841. soc_tplg_remove_dai(comp, dobj, pass);
  1842. break;
  1843. case SND_SOC_DOBJ_DAI_LINK:
  1844. soc_tplg_remove_link(comp, dobj, pass);
  1845. break;
  1846. case SND_SOC_DOBJ_BACKEND_LINK:
  1847. /*
  1848. * call link_unload ops if extra
  1849. * deinitialization is needed.
  1850. */
  1851. remove_backend_link(comp, dobj, pass);
  1852. break;
  1853. default:
  1854. dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
  1855. dobj->type);
  1856. break;
  1857. }
  1858. }
  1859. }
  1860. /* let caller know if FW can be freed when no objects are left */
  1861. return !list_empty(&comp->dobj_list);
  1862. }
  1863. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);