topology.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. //
  10. #include <linux/bits.h>
  11. #include <linux/device.h>
  12. #include <linux/errno.h>
  13. #include <linux/firmware.h>
  14. #include <linux/workqueue.h>
  15. #include <sound/tlv.h>
  16. #include <uapi/sound/sof/tokens.h>
  17. #include "sof-priv.h"
  18. #include "sof-audio.h"
  19. #include "ops.h"
  20. static bool disable_function_topology;
  21. module_param(disable_function_topology, bool, 0444);
  22. MODULE_PARM_DESC(disable_function_topology, "Disable function topology loading");
  23. #define COMP_ID_UNASSIGNED 0xffffffff
  24. /*
  25. * Constants used in the computation of linear volume gain
  26. * from dB gain 20th root of 10 in Q1.16 fixed-point notation
  27. */
  28. #define VOL_TWENTIETH_ROOT_OF_TEN 73533
  29. /* 40th root of 10 in Q1.16 fixed-point notation*/
  30. #define VOL_FORTIETH_ROOT_OF_TEN 69419
  31. /* 0.5 dB step value in topology TLV */
  32. #define VOL_HALF_DB_STEP 50
  33. /* TLV data items */
  34. #define TLV_MIN 0
  35. #define TLV_STEP 1
  36. #define TLV_MUTE 2
  37. /**
  38. * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
  39. * token ID.
  40. * @scomp: pointer to SOC component
  41. * @object: target IPC struct to save the parsed values
  42. * @token_id: token ID for the token array to be searched
  43. * @tuples: pointer to the tuples array
  44. * @num_tuples: number of tuples in the tuples array
  45. * @object_size: size of the object
  46. * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
  47. * looks for @token_instance_num of each token in the token array associated
  48. * with the @token_id
  49. */
  50. int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
  51. struct snd_sof_tuple *tuples, int num_tuples,
  52. size_t object_size, int token_instance_num)
  53. {
  54. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  55. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  56. const struct sof_token_info *token_list;
  57. const struct sof_topology_token *tokens;
  58. int i, j;
  59. token_list = tplg_ops ? tplg_ops->token_list : NULL;
  60. /* nothing to do if token_list is NULL */
  61. if (!token_list)
  62. return 0;
  63. if (token_list[token_id].count < 0) {
  64. dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
  65. return -EINVAL;
  66. }
  67. /* No tokens to match */
  68. if (!token_list[token_id].count)
  69. return 0;
  70. tokens = token_list[token_id].tokens;
  71. if (!tokens) {
  72. dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
  73. return -EINVAL;
  74. }
  75. for (i = 0; i < token_list[token_id].count; i++) {
  76. int offset = 0;
  77. int num_tokens_matched = 0;
  78. for (j = 0; j < num_tuples; j++) {
  79. if (tokens[i].token == tuples[j].token) {
  80. switch (tokens[i].type) {
  81. case SND_SOC_TPLG_TUPLE_TYPE_WORD:
  82. {
  83. u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
  84. offset);
  85. *val = tuples[j].value.v;
  86. break;
  87. }
  88. case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
  89. case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
  90. {
  91. u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
  92. offset);
  93. *val = (u16)tuples[j].value.v;
  94. break;
  95. }
  96. case SND_SOC_TPLG_TUPLE_TYPE_STRING:
  97. {
  98. if (!tokens[i].get_token) {
  99. dev_err(scomp->dev,
  100. "get_token not defined for token %d in %s\n",
  101. tokens[i].token, token_list[token_id].name);
  102. return -EINVAL;
  103. }
  104. tokens[i].get_token((void *)tuples[j].value.s, object,
  105. tokens[i].offset + offset);
  106. break;
  107. }
  108. default:
  109. break;
  110. }
  111. num_tokens_matched++;
  112. /* found all required sets of current token. Move to the next one */
  113. if (!(num_tokens_matched % token_instance_num))
  114. break;
  115. /* move to the next object */
  116. offset += object_size;
  117. }
  118. }
  119. }
  120. return 0;
  121. }
  122. static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
  123. {
  124. /* we only support dB scale TLV type at the moment */
  125. if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
  126. return -EINVAL;
  127. /* min value in topology tlv data is multiplied by 100 */
  128. tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
  129. /* volume steps */
  130. tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
  131. TLV_DB_SCALE_MASK);
  132. /* mute ON/OFF */
  133. if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
  134. TLV_DB_SCALE_MUTE) == 0)
  135. tlv[TLV_MUTE] = 0;
  136. else
  137. tlv[TLV_MUTE] = 1;
  138. return 0;
  139. }
  140. /*
  141. * Function to truncate an unsigned 64-bit number
  142. * by x bits and return 32-bit unsigned number. This
  143. * function also takes care of rounding while truncating
  144. */
  145. static inline u32 vol_shift_64(u64 i, u32 x)
  146. {
  147. /* do not truncate more than 32 bits */
  148. if (x > 32)
  149. x = 32;
  150. if (x == 0)
  151. return (u32)i;
  152. return (u32)(((i >> (x - 1)) + 1) >> 1);
  153. }
  154. /*
  155. * Function to compute a ^ exp where,
  156. * a is a fractional number represented by a fixed-point
  157. * integer with a fractional world length of "fwl"
  158. * exp is an integer
  159. * fwl is the fractional word length
  160. * Return value is a fractional number represented by a
  161. * fixed-point integer with a fractional word length of "fwl"
  162. */
  163. static u32 vol_pow32(u32 a, int exp, u32 fwl)
  164. {
  165. int i, iter;
  166. u32 power = 1 << fwl;
  167. u64 numerator;
  168. /* if exponent is 0, return 1 */
  169. if (exp == 0)
  170. return power;
  171. /* determine the number of iterations based on the exponent */
  172. if (exp < 0)
  173. iter = exp * -1;
  174. else
  175. iter = exp;
  176. /* mutiply a "iter" times to compute power */
  177. for (i = 0; i < iter; i++) {
  178. /*
  179. * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
  180. * Truncate product back to fwl fractional bits with rounding
  181. */
  182. power = vol_shift_64((u64)power * a, fwl);
  183. }
  184. if (exp > 0) {
  185. /* if exp is positive, return the result */
  186. return power;
  187. }
  188. /* if exp is negative, return the multiplicative inverse */
  189. numerator = (u64)1 << (fwl << 1);
  190. do_div(numerator, power);
  191. return (u32)numerator;
  192. }
  193. /*
  194. * Function to calculate volume gain from TLV data.
  195. * This function can only handle gain steps that are multiples of 0.5 dB
  196. */
  197. u32 vol_compute_gain(u32 value, int *tlv)
  198. {
  199. int dB_gain;
  200. u32 linear_gain;
  201. int f_step;
  202. /* mute volume */
  203. if (value == 0 && tlv[TLV_MUTE])
  204. return 0;
  205. /*
  206. * compute dB gain from tlv. tlv_step
  207. * in topology is multiplied by 100
  208. */
  209. dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
  210. /*
  211. * compute linear gain represented by fixed-point
  212. * int with VOLUME_FWL fractional bits
  213. */
  214. linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
  215. /* extract the fractional part of volume step */
  216. f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
  217. /* if volume step is an odd multiple of 0.5 dB */
  218. if (f_step == VOL_HALF_DB_STEP && (value & 1))
  219. linear_gain = vol_shift_64((u64)linear_gain *
  220. VOL_FORTIETH_ROOT_OF_TEN,
  221. VOLUME_FWL);
  222. return linear_gain;
  223. }
  224. /*
  225. * Set up volume table for kcontrols from tlv data
  226. * "size" specifies the number of entries in the table
  227. */
  228. static int set_up_volume_table(struct snd_sof_control *scontrol,
  229. int tlv[SOF_TLV_ITEMS], int size)
  230. {
  231. struct snd_soc_component *scomp = scontrol->scomp;
  232. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  233. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  234. if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table)
  235. return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
  236. dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
  237. return -EINVAL;
  238. }
  239. struct sof_dai_types {
  240. const char *name;
  241. enum sof_ipc_dai_type type;
  242. };
  243. static const struct sof_dai_types sof_dais[] = {
  244. {"SSP", SOF_DAI_INTEL_SSP},
  245. {"HDA", SOF_DAI_INTEL_HDA},
  246. {"DMIC", SOF_DAI_INTEL_DMIC},
  247. {"ALH", SOF_DAI_INTEL_ALH},
  248. {"SAI", SOF_DAI_IMX_SAI},
  249. {"ESAI", SOF_DAI_IMX_ESAI},
  250. {"ACPBT", SOF_DAI_AMD_BT},
  251. {"ACPSP", SOF_DAI_AMD_SP},
  252. {"ACPDMIC", SOF_DAI_AMD_DMIC},
  253. {"ACPHS", SOF_DAI_AMD_HS},
  254. {"AFE", SOF_DAI_MEDIATEK_AFE},
  255. {"ACPSP_VIRTUAL", SOF_DAI_AMD_SP_VIRTUAL},
  256. {"ACPHS_VIRTUAL", SOF_DAI_AMD_HS_VIRTUAL},
  257. {"MICFIL", SOF_DAI_IMX_MICFIL},
  258. {"ACP_SDW", SOF_DAI_AMD_SDW},
  259. };
  260. static enum sof_ipc_dai_type find_dai(const char *name)
  261. {
  262. int i;
  263. for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
  264. if (strcmp(name, sof_dais[i].name) == 0)
  265. return sof_dais[i].type;
  266. }
  267. return SOF_DAI_INTEL_NONE;
  268. }
  269. /*
  270. * Supported Frame format types and lookup, add new ones to end of list.
  271. */
  272. struct sof_frame_types {
  273. const char *name;
  274. enum sof_ipc_frame frame;
  275. };
  276. static const struct sof_frame_types sof_frames[] = {
  277. {"s16le", SOF_IPC_FRAME_S16_LE},
  278. {"s24le", SOF_IPC_FRAME_S24_4LE},
  279. {"s32le", SOF_IPC_FRAME_S32_LE},
  280. {"float", SOF_IPC_FRAME_FLOAT},
  281. };
  282. static enum sof_ipc_frame find_format(const char *name)
  283. {
  284. int i;
  285. for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
  286. if (strcmp(name, sof_frames[i].name) == 0)
  287. return sof_frames[i].frame;
  288. }
  289. /* use s32le if nothing is specified */
  290. return SOF_IPC_FRAME_S32_LE;
  291. }
  292. int get_token_u32(void *elem, void *object, u32 offset)
  293. {
  294. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  295. u32 *val = (u32 *)((u8 *)object + offset);
  296. *val = le32_to_cpu(velem->value);
  297. return 0;
  298. }
  299. int get_token_u16(void *elem, void *object, u32 offset)
  300. {
  301. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  302. u16 *val = (u16 *)((u8 *)object + offset);
  303. *val = (u16)le32_to_cpu(velem->value);
  304. return 0;
  305. }
  306. int get_token_uuid(void *elem, void *object, u32 offset)
  307. {
  308. struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
  309. u8 *dst = (u8 *)object + offset;
  310. memcpy(dst, velem->uuid, UUID_SIZE);
  311. return 0;
  312. }
  313. /*
  314. * The string gets from topology will be stored in heap, the owner only
  315. * holds a char* member point to the heap.
  316. */
  317. int get_token_string(void *elem, void *object, u32 offset)
  318. {
  319. /* "dst" here points to the char* member of the owner */
  320. char **dst = (char **)((u8 *)object + offset);
  321. *dst = kstrdup(elem, GFP_KERNEL);
  322. if (!*dst)
  323. return -ENOMEM;
  324. return 0;
  325. };
  326. int get_token_comp_format(void *elem, void *object, u32 offset)
  327. {
  328. u32 *val = (u32 *)((u8 *)object + offset);
  329. *val = find_format((const char *)elem);
  330. return 0;
  331. }
  332. int get_token_dai_type(void *elem, void *object, u32 offset)
  333. {
  334. u32 *val = (u32 *)((u8 *)object + offset);
  335. *val = find_dai((const char *)elem);
  336. return 0;
  337. }
  338. /* PCM */
  339. static const struct sof_topology_token stream_tokens[] = {
  340. {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  341. offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
  342. {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  343. offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
  344. {SOF_TKN_STREAM_PLAYBACK_PAUSE_SUPPORTED, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  345. offsetof(struct snd_sof_pcm, stream[0].pause_supported)},
  346. {SOF_TKN_STREAM_CAPTURE_PAUSE_SUPPORTED, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  347. offsetof(struct snd_sof_pcm, stream[1].pause_supported)},
  348. };
  349. /* Leds */
  350. static const struct sof_topology_token led_tokens[] = {
  351. {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  352. offsetof(struct snd_sof_led_control, use_led)},
  353. {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  354. offsetof(struct snd_sof_led_control, direction)},
  355. };
  356. static const struct sof_topology_token comp_pin_tokens[] = {
  357. {SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  358. offsetof(struct snd_sof_widget, num_input_pins)},
  359. {SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  360. offsetof(struct snd_sof_widget, num_output_pins)},
  361. };
  362. static const struct sof_topology_token comp_input_pin_binding_tokens[] = {
  363. {SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
  364. get_token_string, 0},
  365. };
  366. static const struct sof_topology_token comp_output_pin_binding_tokens[] = {
  367. {SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
  368. get_token_string, 0},
  369. };
  370. /**
  371. * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
  372. * @scomp: pointer to soc component
  373. * @object: target ipc struct for parsed values
  374. * @offset: offset within the object pointer
  375. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  376. * @num_tokens: number of tokens in tokens array
  377. * @array: source pointer to consecutive vendor arrays in topology
  378. *
  379. * This function parses multiple sets of string type tokens in vendor arrays
  380. */
  381. static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
  382. void *object, size_t offset,
  383. const struct sof_topology_token *tokens, int num_tokens,
  384. struct snd_soc_tplg_vendor_array *array)
  385. {
  386. struct snd_soc_tplg_vendor_uuid_elem *elem;
  387. int found = 0;
  388. int i, j;
  389. /* parse element by element */
  390. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  391. elem = &array->uuid[i];
  392. /* search for token */
  393. for (j = 0; j < num_tokens; j++) {
  394. /* match token type */
  395. if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
  396. continue;
  397. /* match token id */
  398. if (tokens[j].token != le32_to_cpu(elem->token))
  399. continue;
  400. /* matched - now load token */
  401. tokens[j].get_token(elem, object,
  402. offset + tokens[j].offset);
  403. found++;
  404. }
  405. }
  406. return found;
  407. }
  408. /**
  409. * sof_copy_tuples - Parse tokens and copy them to the @tuples array
  410. * @sdev: pointer to struct snd_sof_dev
  411. * @array: source pointer to consecutive vendor arrays in topology
  412. * @array_size: size of @array
  413. * @token_id: Token ID associated with a token array
  414. * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
  415. * looks for @token_instance_num of each token in the token array associated
  416. * with the @token_id
  417. * @tuples: tuples array to copy the matched tuples to
  418. * @tuples_size: size of @tuples
  419. * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
  420. *
  421. */
  422. static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
  423. int array_size, u32 token_id, int token_instance_num,
  424. struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
  425. {
  426. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  427. const struct sof_token_info *token_list;
  428. const struct sof_topology_token *tokens;
  429. int found = 0;
  430. int num_tokens, asize;
  431. int i, j;
  432. token_list = tplg_ops ? tplg_ops->token_list : NULL;
  433. /* nothing to do if token_list is NULL */
  434. if (!token_list)
  435. return 0;
  436. if (!tuples || !num_copied_tuples) {
  437. dev_err(sdev->dev, "Invalid tuples array\n");
  438. return -EINVAL;
  439. }
  440. tokens = token_list[token_id].tokens;
  441. num_tokens = token_list[token_id].count;
  442. if (!tokens) {
  443. dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
  444. return -EINVAL;
  445. }
  446. /* check if there's space in the tuples array for new tokens */
  447. if (*num_copied_tuples >= tuples_size) {
  448. dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
  449. token_list[token_id].name);
  450. return -EINVAL;
  451. }
  452. while (array_size > 0 && found < num_tokens * token_instance_num) {
  453. asize = le32_to_cpu(array->size);
  454. /* validate asize */
  455. if (asize < 0) {
  456. dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
  457. return -EINVAL;
  458. }
  459. /* make sure there is enough data before parsing */
  460. array_size -= asize;
  461. if (array_size < 0) {
  462. dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
  463. return -EINVAL;
  464. }
  465. /* parse element by element */
  466. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  467. /* search for token */
  468. for (j = 0; j < num_tokens; j++) {
  469. /* match token type */
  470. if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
  471. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
  472. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
  473. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
  474. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
  475. continue;
  476. if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
  477. struct snd_soc_tplg_vendor_string_elem *elem;
  478. elem = &array->string[i];
  479. /* match token id */
  480. if (tokens[j].token != le32_to_cpu(elem->token))
  481. continue;
  482. tuples[*num_copied_tuples].token = tokens[j].token;
  483. tuples[*num_copied_tuples].value.s =
  484. devm_kasprintf(sdev->dev, GFP_KERNEL,
  485. "%s", elem->string);
  486. if (!tuples[*num_copied_tuples].value.s)
  487. return -ENOMEM;
  488. } else {
  489. struct snd_soc_tplg_vendor_value_elem *elem;
  490. elem = &array->value[i];
  491. /* match token id */
  492. if (tokens[j].token != le32_to_cpu(elem->token))
  493. continue;
  494. tuples[*num_copied_tuples].token = tokens[j].token;
  495. tuples[*num_copied_tuples].value.v =
  496. le32_to_cpu(elem->value);
  497. }
  498. found++;
  499. (*num_copied_tuples)++;
  500. /* stop if there's no space for any more new tuples */
  501. if (*num_copied_tuples == tuples_size)
  502. return 0;
  503. }
  504. /* stop when we've found the required token instances */
  505. if (found == num_tokens * token_instance_num)
  506. return 0;
  507. }
  508. /* next array */
  509. array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
  510. }
  511. return 0;
  512. }
  513. /**
  514. * sof_parse_string_tokens - Parse multiple sets of tokens
  515. * @scomp: pointer to soc component
  516. * @object: target ipc struct for parsed values
  517. * @offset: offset within the object pointer
  518. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  519. * @num_tokens: number of tokens in tokens array
  520. * @array: source pointer to consecutive vendor arrays in topology
  521. *
  522. * This function parses multiple sets of string type tokens in vendor arrays
  523. */
  524. static int sof_parse_string_tokens(struct snd_soc_component *scomp,
  525. void *object, int offset,
  526. const struct sof_topology_token *tokens, int num_tokens,
  527. struct snd_soc_tplg_vendor_array *array)
  528. {
  529. struct snd_soc_tplg_vendor_string_elem *elem;
  530. int found = 0;
  531. int i, j, ret;
  532. /* parse element by element */
  533. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  534. elem = &array->string[i];
  535. /* search for token */
  536. for (j = 0; j < num_tokens; j++) {
  537. /* match token type */
  538. if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
  539. continue;
  540. /* match token id */
  541. if (tokens[j].token != le32_to_cpu(elem->token))
  542. continue;
  543. /* matched - now load token */
  544. ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
  545. if (ret < 0)
  546. return ret;
  547. found++;
  548. }
  549. }
  550. return found;
  551. }
  552. /**
  553. * sof_parse_word_tokens - Parse multiple sets of tokens
  554. * @scomp: pointer to soc component
  555. * @object: target ipc struct for parsed values
  556. * @offset: offset within the object pointer
  557. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  558. * @num_tokens: number of tokens in tokens array
  559. * @array: source pointer to consecutive vendor arrays in topology
  560. *
  561. * This function parses multiple sets of word type tokens in vendor arrays
  562. */
  563. static int sof_parse_word_tokens(struct snd_soc_component *scomp,
  564. void *object, int offset,
  565. const struct sof_topology_token *tokens, int num_tokens,
  566. struct snd_soc_tplg_vendor_array *array)
  567. {
  568. struct snd_soc_tplg_vendor_value_elem *elem;
  569. int found = 0;
  570. int i, j;
  571. /* parse element by element */
  572. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  573. elem = &array->value[i];
  574. /* search for token */
  575. for (j = 0; j < num_tokens; j++) {
  576. /* match token type */
  577. if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
  578. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
  579. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
  580. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
  581. continue;
  582. /* match token id */
  583. if (tokens[j].token != le32_to_cpu(elem->token))
  584. continue;
  585. /* load token */
  586. tokens[j].get_token(elem, object, offset + tokens[j].offset);
  587. found++;
  588. }
  589. }
  590. return found;
  591. }
  592. /**
  593. * sof_parse_token_sets - Parse multiple sets of tokens
  594. * @scomp: pointer to soc component
  595. * @object: target ipc struct for parsed values
  596. * @tokens: token definition array describing what tokens to parse
  597. * @count: number of tokens in definition array
  598. * @array: source pointer to consecutive vendor arrays in topology
  599. * @array_size: total size of @array
  600. * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
  601. * looks for @token_instance_num of each token in the @tokens
  602. * @object_size: offset to next target ipc struct with multiple sets
  603. *
  604. * This function parses multiple sets of tokens in vendor arrays into
  605. * consecutive ipc structs.
  606. */
  607. static int sof_parse_token_sets(struct snd_soc_component *scomp,
  608. void *object, const struct sof_topology_token *tokens,
  609. int count, struct snd_soc_tplg_vendor_array *array,
  610. int array_size, int token_instance_num, size_t object_size)
  611. {
  612. size_t offset = 0;
  613. int found = 0;
  614. int total = 0;
  615. int asize;
  616. int ret;
  617. while (array_size > 0 && total < count * token_instance_num) {
  618. asize = le32_to_cpu(array->size);
  619. /* validate asize */
  620. if (asize < sizeof(*array)) {
  621. dev_err(scomp->dev, "error: invalid array size 0x%x\n",
  622. asize);
  623. return -EINVAL;
  624. }
  625. /* make sure there is enough data before parsing */
  626. array_size -= asize;
  627. if (array_size < 0) {
  628. dev_err(scomp->dev, "error: invalid array size 0x%x\n",
  629. asize);
  630. return -EINVAL;
  631. }
  632. /* call correct parser depending on type */
  633. switch (le32_to_cpu(array->type)) {
  634. case SND_SOC_TPLG_TUPLE_TYPE_UUID:
  635. found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
  636. array);
  637. break;
  638. case SND_SOC_TPLG_TUPLE_TYPE_STRING:
  639. ret = sof_parse_string_tokens(scomp, object, offset, tokens, count,
  640. array);
  641. if (ret < 0) {
  642. dev_err(scomp->dev, "error: no memory to copy string token\n");
  643. return ret;
  644. }
  645. found += ret;
  646. break;
  647. case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
  648. case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
  649. case SND_SOC_TPLG_TUPLE_TYPE_WORD:
  650. case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
  651. found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
  652. array);
  653. break;
  654. default:
  655. dev_err(scomp->dev, "error: unknown token type %d\n",
  656. array->type);
  657. return -EINVAL;
  658. }
  659. /* next array */
  660. array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
  661. + asize);
  662. /* move to next target struct */
  663. if (found >= count) {
  664. offset += object_size;
  665. total += found;
  666. found = 0;
  667. }
  668. }
  669. return 0;
  670. }
  671. /**
  672. * sof_parse_tokens - Parse one set of tokens
  673. * @scomp: pointer to soc component
  674. * @object: target ipc struct for parsed values
  675. * @tokens: token definition array describing what tokens to parse
  676. * @num_tokens: number of tokens in definition array
  677. * @array: source pointer to consecutive vendor arrays in topology
  678. * @array_size: total size of @array
  679. *
  680. * This function parses a single set of tokens in vendor arrays into
  681. * consecutive ipc structs.
  682. */
  683. static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
  684. const struct sof_topology_token *tokens, int num_tokens,
  685. struct snd_soc_tplg_vendor_array *array,
  686. int array_size)
  687. {
  688. /*
  689. * sof_parse_tokens is used when topology contains only a single set of
  690. * identical tuples arrays. So additional parameters to
  691. * sof_parse_token_sets are sets = 1 (only 1 set) and
  692. * object_size = 0 (irrelevant).
  693. */
  694. return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
  695. array_size, 1, 0);
  696. }
  697. /*
  698. * Standard Kcontrols.
  699. */
  700. static int sof_control_load_volume(struct snd_soc_component *scomp,
  701. struct snd_sof_control *scontrol,
  702. struct snd_kcontrol_new *kc,
  703. struct snd_soc_tplg_ctl_hdr *hdr)
  704. {
  705. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  706. struct snd_soc_tplg_mixer_control *mc =
  707. container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
  708. int tlv[SOF_TLV_ITEMS];
  709. unsigned int mask;
  710. int ret;
  711. /* validate topology data */
  712. if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
  713. return -EINVAL;
  714. /*
  715. * If control has more than 2 channels we need to override the info. This is because even if
  716. * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
  717. * pre-defined dapm control types (and related functions) creating the actual control
  718. * restrict the channels only to mono or stereo.
  719. */
  720. if (le32_to_cpu(mc->num_channels) > 2)
  721. kc->info = snd_sof_volume_info;
  722. scontrol->comp_id = sdev->next_comp_id;
  723. scontrol->min_volume_step = le32_to_cpu(mc->min);
  724. scontrol->max_volume_step = le32_to_cpu(mc->max);
  725. scontrol->num_channels = le32_to_cpu(mc->num_channels);
  726. scontrol->max = le32_to_cpu(mc->max);
  727. if (le32_to_cpu(mc->max) == 1)
  728. goto skip;
  729. /* extract tlv data */
  730. if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
  731. dev_err(scomp->dev, "error: invalid TLV data\n");
  732. return -EINVAL;
  733. }
  734. /* set up volume table */
  735. ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
  736. if (ret < 0) {
  737. dev_err(scomp->dev, "error: setting up volume table\n");
  738. return ret;
  739. }
  740. skip:
  741. /* set up possible led control from mixer private data */
  742. ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
  743. ARRAY_SIZE(led_tokens), mc->priv.array,
  744. le32_to_cpu(mc->priv.size));
  745. if (ret != 0) {
  746. dev_err(scomp->dev, "error: parse led tokens failed %d\n",
  747. le32_to_cpu(mc->priv.size));
  748. goto err;
  749. }
  750. if (scontrol->led_ctl.use_led) {
  751. mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
  752. SNDRV_CTL_ELEM_ACCESS_SPK_LED;
  753. scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
  754. scontrol->access |= mask;
  755. kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
  756. kc->access |= mask;
  757. sdev->led_present = true;
  758. }
  759. dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
  760. scontrol->comp_id, scontrol->num_channels);
  761. return 0;
  762. err:
  763. if (le32_to_cpu(mc->max) > 1)
  764. kfree(scontrol->volume_table);
  765. return ret;
  766. }
  767. static int sof_control_load_enum(struct snd_soc_component *scomp,
  768. struct snd_sof_control *scontrol,
  769. struct snd_kcontrol_new *kc,
  770. struct snd_soc_tplg_ctl_hdr *hdr)
  771. {
  772. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  773. struct snd_soc_tplg_enum_control *ec =
  774. container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
  775. /* validate topology data */
  776. if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
  777. return -EINVAL;
  778. scontrol->comp_id = sdev->next_comp_id;
  779. scontrol->num_channels = le32_to_cpu(ec->num_channels);
  780. dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
  781. scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
  782. return 0;
  783. }
  784. static int sof_control_load_bytes(struct snd_soc_component *scomp,
  785. struct snd_sof_control *scontrol,
  786. struct snd_kcontrol_new *kc,
  787. struct snd_soc_tplg_ctl_hdr *hdr)
  788. {
  789. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  790. struct snd_soc_tplg_bytes_control *control =
  791. container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
  792. struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
  793. size_t priv_size = le32_to_cpu(control->priv.size);
  794. scontrol->max_size = sbe->max;
  795. scontrol->comp_id = sdev->next_comp_id;
  796. dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
  797. /* copy the private data */
  798. if (priv_size > 0) {
  799. scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
  800. if (!scontrol->priv)
  801. return -ENOMEM;
  802. scontrol->priv_size = priv_size;
  803. }
  804. return 0;
  805. }
  806. /* external kcontrol init - used for any driver specific init */
  807. static int sof_control_load(struct snd_soc_component *scomp, int index,
  808. struct snd_kcontrol_new *kc,
  809. struct snd_soc_tplg_ctl_hdr *hdr)
  810. {
  811. struct soc_mixer_control *sm;
  812. struct soc_bytes_ext *sbe;
  813. struct soc_enum *se;
  814. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  815. struct snd_soc_dobj *dobj;
  816. struct snd_sof_control *scontrol;
  817. int ret;
  818. dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
  819. hdr->type, hdr->name);
  820. scontrol = kzalloc_obj(*scontrol);
  821. if (!scontrol)
  822. return -ENOMEM;
  823. scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
  824. if (!scontrol->name) {
  825. kfree(scontrol);
  826. return -ENOMEM;
  827. }
  828. scontrol->scomp = scomp;
  829. scontrol->access = kc->access;
  830. scontrol->info_type = le32_to_cpu(hdr->ops.info);
  831. scontrol->index = kc->index;
  832. switch (le32_to_cpu(hdr->ops.info)) {
  833. case SND_SOC_TPLG_CTL_VOLSW:
  834. case SND_SOC_TPLG_CTL_VOLSW_SX:
  835. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  836. sm = (struct soc_mixer_control *)kc->private_value;
  837. dobj = &sm->dobj;
  838. ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
  839. break;
  840. case SND_SOC_TPLG_CTL_BYTES:
  841. sbe = (struct soc_bytes_ext *)kc->private_value;
  842. dobj = &sbe->dobj;
  843. ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
  844. break;
  845. case SND_SOC_TPLG_CTL_ENUM:
  846. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  847. se = (struct soc_enum *)kc->private_value;
  848. dobj = &se->dobj;
  849. ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
  850. break;
  851. case SND_SOC_TPLG_CTL_RANGE:
  852. case SND_SOC_TPLG_CTL_STROBE:
  853. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  854. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  855. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  856. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  857. case SND_SOC_TPLG_DAPM_CTL_PIN:
  858. default:
  859. dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
  860. hdr->ops.get, hdr->ops.put, hdr->ops.info);
  861. kfree(scontrol->name);
  862. kfree(scontrol);
  863. return 0;
  864. }
  865. if (ret < 0) {
  866. kfree(scontrol->name);
  867. kfree(scontrol);
  868. return ret;
  869. }
  870. scontrol->led_ctl.led_value = -1;
  871. dobj->private = scontrol;
  872. list_add(&scontrol->list, &sdev->kcontrol_list);
  873. return 0;
  874. }
  875. static int sof_control_unload(struct snd_soc_component *scomp,
  876. struct snd_soc_dobj *dobj)
  877. {
  878. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  879. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  880. struct snd_sof_control *scontrol = dobj->private;
  881. int ret = 0;
  882. dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
  883. if (tplg_ops && tplg_ops->control_free) {
  884. ret = tplg_ops->control_free(sdev, scontrol);
  885. if (ret < 0)
  886. dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
  887. }
  888. /* free all data before returning in case of error too */
  889. kfree(scontrol->ipc_control_data);
  890. kfree(scontrol->priv);
  891. kfree(scontrol->name);
  892. list_del(&scontrol->list);
  893. kfree(scontrol);
  894. return ret;
  895. }
  896. /*
  897. * DAI Topology
  898. */
  899. static int sof_connect_dai_widget(struct snd_soc_component *scomp,
  900. struct snd_soc_dapm_widget *w,
  901. struct snd_soc_tplg_dapm_widget *tw,
  902. struct snd_sof_dai *dai)
  903. {
  904. struct snd_soc_card *card = scomp->card;
  905. struct snd_soc_pcm_runtime *rtd, *full, *partial;
  906. struct snd_soc_dai *cpu_dai;
  907. int stream;
  908. int i;
  909. if (!w->sname) {
  910. dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
  911. return -EINVAL;
  912. }
  913. if (w->id == snd_soc_dapm_dai_out)
  914. stream = SNDRV_PCM_STREAM_CAPTURE;
  915. else if (w->id == snd_soc_dapm_dai_in)
  916. stream = SNDRV_PCM_STREAM_PLAYBACK;
  917. else
  918. goto end;
  919. full = NULL;
  920. partial = NULL;
  921. list_for_each_entry(rtd, &card->rtd_list, list) {
  922. /* does stream match DAI link ? */
  923. if (rtd->dai_link->stream_name) {
  924. if (!strcmp(rtd->dai_link->stream_name, w->sname)) {
  925. full = rtd;
  926. break;
  927. } else if (strstr(rtd->dai_link->stream_name, w->sname)) {
  928. partial = rtd;
  929. }
  930. }
  931. }
  932. rtd = full ? full : partial;
  933. if (rtd) {
  934. for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
  935. /*
  936. * Please create DAI widget in the right order
  937. * to ensure BE will connect to the right DAI
  938. * widget.
  939. */
  940. if (!snd_soc_dai_get_widget(cpu_dai, stream)) {
  941. snd_soc_dai_set_widget(cpu_dai, stream, w);
  942. break;
  943. }
  944. }
  945. if (i == rtd->dai_link->num_cpus) {
  946. dev_err(scomp->dev, "error: can't find BE for DAI %s\n", w->name);
  947. return -EINVAL;
  948. }
  949. dai->name = rtd->dai_link->name;
  950. dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
  951. w->name, rtd->dai_link->name);
  952. }
  953. end:
  954. /* check we have a connection */
  955. if (!dai->name) {
  956. dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
  957. w->name, w->sname);
  958. return -EINVAL;
  959. }
  960. return 0;
  961. }
  962. static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
  963. struct snd_soc_dapm_widget *w)
  964. {
  965. struct snd_soc_card *card = scomp->card;
  966. struct snd_soc_pcm_runtime *rtd;
  967. const char *sname = w->sname;
  968. struct snd_soc_dai *cpu_dai;
  969. int i, stream;
  970. if (!sname)
  971. return;
  972. if (w->id == snd_soc_dapm_dai_out)
  973. stream = SNDRV_PCM_STREAM_CAPTURE;
  974. else if (w->id == snd_soc_dapm_dai_in)
  975. stream = SNDRV_PCM_STREAM_PLAYBACK;
  976. else
  977. return;
  978. list_for_each_entry(rtd, &card->rtd_list, list) {
  979. /* does stream match DAI link ? */
  980. if (!rtd->dai_link->stream_name ||
  981. !strstr(rtd->dai_link->stream_name, sname))
  982. continue;
  983. for_each_rtd_cpu_dais(rtd, i, cpu_dai)
  984. if (snd_soc_dai_get_widget(cpu_dai, stream) == w) {
  985. snd_soc_dai_set_widget(cpu_dai, stream, NULL);
  986. break;
  987. }
  988. }
  989. }
  990. /* bind PCM ID to host component ID */
  991. static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
  992. int dir)
  993. {
  994. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  995. struct snd_sof_widget *host_widget;
  996. if (sdev->dspless_mode_selected)
  997. return 0;
  998. host_widget = snd_sof_find_swidget_sname(scomp,
  999. spcm->pcm.caps[dir].name,
  1000. dir);
  1001. if (!host_widget) {
  1002. dev_err(scomp->dev, "can't find host comp to bind pcm\n");
  1003. return -EINVAL;
  1004. }
  1005. spcm->stream[dir].comp_id = host_widget->comp_id;
  1006. return 0;
  1007. }
  1008. static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
  1009. {
  1010. int i;
  1011. if (!tuples)
  1012. return -EINVAL;
  1013. for (i = 0; i < num_tuples; i++) {
  1014. if (tuples[i].token == token_id)
  1015. return tuples[i].value.v;
  1016. }
  1017. return -EINVAL;
  1018. }
  1019. static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
  1020. struct snd_soc_tplg_dapm_widget *tw,
  1021. enum sof_tokens *object_token_list, int count)
  1022. {
  1023. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1024. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1025. struct snd_soc_tplg_private *private = &tw->priv;
  1026. const struct sof_token_info *token_list;
  1027. int num_tuples = 0;
  1028. int ret, i;
  1029. token_list = tplg_ops ? tplg_ops->token_list : NULL;
  1030. /* nothing to do if token_list is NULL */
  1031. if (!token_list)
  1032. return 0;
  1033. if (count > 0 && !object_token_list) {
  1034. dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
  1035. return -EINVAL;
  1036. }
  1037. /* calculate max size of tuples array */
  1038. for (i = 0; i < count; i++)
  1039. num_tuples += token_list[object_token_list[i]].count;
  1040. /* allocate memory for tuples array */
  1041. swidget->tuples = kzalloc_objs(*swidget->tuples, num_tuples);
  1042. if (!swidget->tuples)
  1043. return -ENOMEM;
  1044. /* parse token list for widget */
  1045. for (i = 0; i < count; i++) {
  1046. int num_sets = 1;
  1047. if (object_token_list[i] >= SOF_TOKEN_COUNT) {
  1048. dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
  1049. object_token_list[i], swidget->widget->name);
  1050. ret = -EINVAL;
  1051. goto err;
  1052. }
  1053. switch (object_token_list[i]) {
  1054. case SOF_COMP_EXT_TOKENS:
  1055. /* parse and save UUID in swidget */
  1056. ret = sof_parse_tokens(scomp, swidget,
  1057. token_list[object_token_list[i]].tokens,
  1058. token_list[object_token_list[i]].count,
  1059. private->array, le32_to_cpu(private->size));
  1060. if (ret < 0) {
  1061. dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
  1062. token_list[object_token_list[i]].name,
  1063. swidget->widget->name);
  1064. goto err;
  1065. }
  1066. continue;
  1067. case SOF_IN_AUDIO_FORMAT_TOKENS:
  1068. num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS,
  1069. swidget->tuples, swidget->num_tuples);
  1070. if (num_sets < 0) {
  1071. dev_err(sdev->dev, "Invalid input audio format count for %s\n",
  1072. swidget->widget->name);
  1073. ret = num_sets;
  1074. goto err;
  1075. }
  1076. break;
  1077. case SOF_OUT_AUDIO_FORMAT_TOKENS:
  1078. num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS,
  1079. swidget->tuples, swidget->num_tuples);
  1080. if (num_sets < 0) {
  1081. dev_err(sdev->dev, "Invalid output audio format count for %s\n",
  1082. swidget->widget->name);
  1083. ret = num_sets;
  1084. goto err;
  1085. }
  1086. break;
  1087. default:
  1088. break;
  1089. }
  1090. if (num_sets > 1) {
  1091. struct snd_sof_tuple *new_tuples;
  1092. num_tuples += token_list[object_token_list[i]].count * (num_sets - 1);
  1093. new_tuples = krealloc_array(swidget->tuples,
  1094. num_tuples, sizeof(*new_tuples), GFP_KERNEL);
  1095. if (!new_tuples) {
  1096. ret = -ENOMEM;
  1097. goto err;
  1098. }
  1099. swidget->tuples = new_tuples;
  1100. }
  1101. /* copy one set of tuples per token ID into swidget->tuples */
  1102. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1103. object_token_list[i], num_sets, swidget->tuples,
  1104. num_tuples, &swidget->num_tuples);
  1105. if (ret < 0) {
  1106. dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
  1107. token_list[object_token_list[i]].name, swidget->widget->name, ret);
  1108. goto err;
  1109. }
  1110. }
  1111. return 0;
  1112. err:
  1113. kfree(swidget->tuples);
  1114. return ret;
  1115. }
  1116. static void sof_free_pin_binding(struct snd_sof_widget *swidget,
  1117. bool pin_type)
  1118. {
  1119. char **pin_binding;
  1120. u32 num_pins;
  1121. int i;
  1122. if (pin_type == SOF_PIN_TYPE_INPUT) {
  1123. pin_binding = swidget->input_pin_binding;
  1124. num_pins = swidget->num_input_pins;
  1125. } else {
  1126. pin_binding = swidget->output_pin_binding;
  1127. num_pins = swidget->num_output_pins;
  1128. }
  1129. if (pin_binding) {
  1130. for (i = 0; i < num_pins; i++)
  1131. kfree(pin_binding[i]);
  1132. }
  1133. kfree(pin_binding);
  1134. }
  1135. static int sof_parse_pin_binding(struct snd_sof_widget *swidget,
  1136. struct snd_soc_tplg_private *priv, bool pin_type)
  1137. {
  1138. const struct sof_topology_token *pin_binding_token;
  1139. char *pin_binding[SOF_WIDGET_MAX_NUM_PINS];
  1140. int token_count;
  1141. u32 num_pins;
  1142. char **pb;
  1143. int ret;
  1144. int i;
  1145. if (pin_type == SOF_PIN_TYPE_INPUT) {
  1146. num_pins = swidget->num_input_pins;
  1147. pin_binding_token = comp_input_pin_binding_tokens;
  1148. token_count = ARRAY_SIZE(comp_input_pin_binding_tokens);
  1149. } else {
  1150. num_pins = swidget->num_output_pins;
  1151. pin_binding_token = comp_output_pin_binding_tokens;
  1152. token_count = ARRAY_SIZE(comp_output_pin_binding_tokens);
  1153. }
  1154. memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *));
  1155. ret = sof_parse_token_sets(swidget->scomp, pin_binding, pin_binding_token,
  1156. token_count, priv->array, le32_to_cpu(priv->size),
  1157. num_pins, sizeof(char *));
  1158. if (ret < 0)
  1159. goto err;
  1160. /* copy pin binding array to swidget only if it is defined in topology */
  1161. if (pin_binding[0]) {
  1162. pb = kmemdup_array(pin_binding, num_pins, sizeof(char *), GFP_KERNEL);
  1163. if (!pb) {
  1164. ret = -ENOMEM;
  1165. goto err;
  1166. }
  1167. if (pin_type == SOF_PIN_TYPE_INPUT)
  1168. swidget->input_pin_binding = pb;
  1169. else
  1170. swidget->output_pin_binding = pb;
  1171. }
  1172. return 0;
  1173. err:
  1174. for (i = 0; i < num_pins; i++)
  1175. kfree(pin_binding[i]);
  1176. return ret;
  1177. }
  1178. static int get_w_no_wname_in_long_name(void *elem, void *object, u32 offset)
  1179. {
  1180. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  1181. struct snd_soc_dapm_widget *w = object;
  1182. w->no_wname_in_kcontrol_name = !!le32_to_cpu(velem->value);
  1183. return 0;
  1184. }
  1185. static const struct sof_topology_token dapm_widget_tokens[] = {
  1186. {SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
  1187. get_w_no_wname_in_long_name, 0}
  1188. };
  1189. /* external widget init - used for any driver specific init */
  1190. static int sof_widget_ready(struct snd_soc_component *scomp, int index,
  1191. struct snd_soc_dapm_widget *w,
  1192. struct snd_soc_tplg_dapm_widget *tw)
  1193. {
  1194. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1195. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1196. const struct sof_ipc_tplg_widget_ops *widget_ops;
  1197. struct snd_soc_tplg_private *priv = &tw->priv;
  1198. enum sof_tokens *token_list = NULL;
  1199. struct snd_sof_widget *swidget;
  1200. struct snd_sof_dai *dai;
  1201. int token_list_size = 0;
  1202. int ret = 0;
  1203. swidget = kzalloc_obj(*swidget);
  1204. if (!swidget)
  1205. return -ENOMEM;
  1206. swidget->scomp = scomp;
  1207. swidget->widget = w;
  1208. swidget->comp_id = sdev->next_comp_id++;
  1209. swidget->id = w->id;
  1210. swidget->pipeline_id = index;
  1211. swidget->private = NULL;
  1212. mutex_init(&swidget->setup_mutex);
  1213. ida_init(&swidget->output_queue_ida);
  1214. ida_init(&swidget->input_queue_ida);
  1215. ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, ARRAY_SIZE(dapm_widget_tokens),
  1216. priv->array, le32_to_cpu(priv->size));
  1217. if (ret < 0) {
  1218. dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n",
  1219. w->name);
  1220. goto widget_free;
  1221. }
  1222. ret = sof_parse_tokens(scomp, swidget, comp_pin_tokens,
  1223. ARRAY_SIZE(comp_pin_tokens), priv->array,
  1224. le32_to_cpu(priv->size));
  1225. if (ret < 0) {
  1226. dev_err(scomp->dev, "failed to parse component pin tokens for %s\n",
  1227. w->name);
  1228. goto widget_free;
  1229. }
  1230. if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS ||
  1231. swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) {
  1232. dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n",
  1233. swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins);
  1234. ret = -EINVAL;
  1235. goto widget_free;
  1236. }
  1237. if (swidget->num_input_pins > 1) {
  1238. ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT);
  1239. /* on parsing error, pin binding is not allocated, nothing to free. */
  1240. if (ret < 0) {
  1241. dev_err(scomp->dev, "failed to parse input pin binding for %s\n",
  1242. w->name);
  1243. goto widget_free;
  1244. }
  1245. }
  1246. if (swidget->num_output_pins > 1) {
  1247. ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT);
  1248. /* on parsing error, pin binding is not allocated, nothing to free. */
  1249. if (ret < 0) {
  1250. dev_err(scomp->dev, "failed to parse output pin binding for %s\n",
  1251. w->name);
  1252. goto widget_free;
  1253. }
  1254. }
  1255. dev_dbg(scomp->dev,
  1256. "tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n",
  1257. swidget->comp_id, w->name, swidget->id, index,
  1258. swidget->num_input_pins, swidget->num_output_pins,
  1259. strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none");
  1260. widget_ops = tplg_ops ? tplg_ops->widget : NULL;
  1261. if (widget_ops) {
  1262. token_list = widget_ops[w->id].token_list;
  1263. token_list_size = widget_ops[w->id].token_list_size;
  1264. }
  1265. /* handle any special case widgets */
  1266. switch (w->id) {
  1267. case snd_soc_dapm_dai_in:
  1268. case snd_soc_dapm_dai_out:
  1269. dai = kzalloc_obj(*dai);
  1270. if (!dai) {
  1271. ret = -ENOMEM;
  1272. goto widget_free;
  1273. }
  1274. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1275. if (!ret)
  1276. ret = sof_connect_dai_widget(scomp, w, tw, dai);
  1277. if (ret < 0) {
  1278. kfree(dai);
  1279. break;
  1280. }
  1281. list_add(&dai->list, &sdev->dai_list);
  1282. swidget->private = dai;
  1283. break;
  1284. case snd_soc_dapm_effect:
  1285. /* check we have some tokens - we need at least process type */
  1286. if (le32_to_cpu(tw->priv.size) == 0) {
  1287. dev_err(scomp->dev, "error: process tokens not found\n");
  1288. ret = -EINVAL;
  1289. break;
  1290. }
  1291. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1292. break;
  1293. case snd_soc_dapm_pga:
  1294. if (!le32_to_cpu(tw->num_kcontrols)) {
  1295. dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
  1296. tw->num_kcontrols);
  1297. ret = -EINVAL;
  1298. break;
  1299. }
  1300. fallthrough;
  1301. case snd_soc_dapm_mixer:
  1302. case snd_soc_dapm_buffer:
  1303. case snd_soc_dapm_scheduler:
  1304. case snd_soc_dapm_aif_out:
  1305. case snd_soc_dapm_aif_in:
  1306. case snd_soc_dapm_src:
  1307. case snd_soc_dapm_asrc:
  1308. case snd_soc_dapm_siggen:
  1309. case snd_soc_dapm_mux:
  1310. case snd_soc_dapm_demux:
  1311. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1312. break;
  1313. case snd_soc_dapm_switch:
  1314. case snd_soc_dapm_dai_link:
  1315. case snd_soc_dapm_kcontrol:
  1316. default:
  1317. dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
  1318. break;
  1319. }
  1320. /* check token parsing reply */
  1321. if (ret < 0) {
  1322. dev_err(scomp->dev,
  1323. "failed to add widget type %d name : %s stream %s\n",
  1324. swidget->id, tw->name, strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
  1325. ? tw->sname : "none");
  1326. goto widget_free;
  1327. }
  1328. if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
  1329. swidget->core = SOF_DSP_PRIMARY_CORE;
  1330. } else {
  1331. int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
  1332. swidget->num_tuples);
  1333. if (core >= 0)
  1334. swidget->core = core;
  1335. }
  1336. /* bind widget to external event */
  1337. if (tw->event_type) {
  1338. if (widget_ops && widget_ops[w->id].bind_event) {
  1339. ret = widget_ops[w->id].bind_event(scomp, swidget,
  1340. le16_to_cpu(tw->event_type));
  1341. if (ret) {
  1342. dev_err(scomp->dev, "widget event binding failed for %s\n",
  1343. swidget->widget->name);
  1344. goto free;
  1345. }
  1346. }
  1347. }
  1348. /* create and add pipeline for scheduler type widgets */
  1349. if (w->id == snd_soc_dapm_scheduler) {
  1350. struct snd_sof_pipeline *spipe;
  1351. spipe = kzalloc_obj(*spipe);
  1352. if (!spipe) {
  1353. ret = -ENOMEM;
  1354. goto free;
  1355. }
  1356. spipe->pipe_widget = swidget;
  1357. swidget->spipe = spipe;
  1358. list_add(&spipe->list, &sdev->pipeline_list);
  1359. }
  1360. w->dobj.private = swidget;
  1361. list_add(&swidget->list, &sdev->widget_list);
  1362. return ret;
  1363. free:
  1364. kfree(swidget->private);
  1365. kfree(swidget->tuples);
  1366. widget_free:
  1367. kfree(swidget);
  1368. return ret;
  1369. }
  1370. static int sof_route_unload(struct snd_soc_component *scomp,
  1371. struct snd_soc_dobj *dobj)
  1372. {
  1373. struct snd_sof_route *sroute;
  1374. sroute = dobj->private;
  1375. if (!sroute)
  1376. return 0;
  1377. /* free sroute and its private data */
  1378. kfree(sroute->private);
  1379. list_del(&sroute->list);
  1380. kfree(sroute);
  1381. return 0;
  1382. }
  1383. static int sof_widget_unload(struct snd_soc_component *scomp,
  1384. struct snd_soc_dobj *dobj)
  1385. {
  1386. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1387. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1388. const struct sof_ipc_tplg_widget_ops *widget_ops;
  1389. const struct snd_kcontrol_new *kc;
  1390. struct snd_soc_dapm_widget *widget;
  1391. struct snd_sof_control *scontrol;
  1392. struct snd_sof_widget *swidget;
  1393. struct soc_mixer_control *sm;
  1394. struct soc_bytes_ext *sbe;
  1395. struct snd_sof_dai *dai;
  1396. struct soc_enum *se;
  1397. int i;
  1398. swidget = dobj->private;
  1399. if (!swidget)
  1400. return 0;
  1401. widget = swidget->widget;
  1402. switch (swidget->id) {
  1403. case snd_soc_dapm_dai_in:
  1404. case snd_soc_dapm_dai_out:
  1405. dai = swidget->private;
  1406. if (dai)
  1407. list_del(&dai->list);
  1408. sof_disconnect_dai_widget(scomp, widget);
  1409. break;
  1410. case snd_soc_dapm_scheduler:
  1411. {
  1412. struct snd_sof_pipeline *spipe = swidget->spipe;
  1413. list_del(&spipe->list);
  1414. kfree(spipe);
  1415. swidget->spipe = NULL;
  1416. break;
  1417. }
  1418. default:
  1419. break;
  1420. }
  1421. for (i = 0; i < widget->num_kcontrols; i++) {
  1422. kc = &widget->kcontrol_news[i];
  1423. switch (widget->dobj.widget.kcontrol_type[i]) {
  1424. case SND_SOC_TPLG_TYPE_MIXER:
  1425. sm = (struct soc_mixer_control *)kc->private_value;
  1426. scontrol = sm->dobj.private;
  1427. if (sm->max > 1)
  1428. kfree(scontrol->volume_table);
  1429. break;
  1430. case SND_SOC_TPLG_TYPE_ENUM:
  1431. se = (struct soc_enum *)kc->private_value;
  1432. scontrol = se->dobj.private;
  1433. break;
  1434. case SND_SOC_TPLG_TYPE_BYTES:
  1435. sbe = (struct soc_bytes_ext *)kc->private_value;
  1436. scontrol = sbe->dobj.private;
  1437. break;
  1438. default:
  1439. dev_warn(scomp->dev, "unsupported kcontrol_type\n");
  1440. goto out;
  1441. }
  1442. kfree(scontrol->ipc_control_data);
  1443. list_del(&scontrol->list);
  1444. kfree(scontrol->name);
  1445. kfree(scontrol);
  1446. }
  1447. out:
  1448. /* free IPC related data */
  1449. widget_ops = tplg_ops ? tplg_ops->widget : NULL;
  1450. if (widget_ops && widget_ops[swidget->id].ipc_free)
  1451. widget_ops[swidget->id].ipc_free(swidget);
  1452. ida_destroy(&swidget->output_queue_ida);
  1453. ida_destroy(&swidget->input_queue_ida);
  1454. sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT);
  1455. sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT);
  1456. kfree(swidget->tuples);
  1457. /* remove and free swidget object */
  1458. list_del(&swidget->list);
  1459. kfree(swidget);
  1460. return 0;
  1461. }
  1462. /*
  1463. * DAI HW configuration.
  1464. */
  1465. /* FE DAI - used for any driver specific init */
  1466. static int sof_dai_load(struct snd_soc_component *scomp, int index,
  1467. struct snd_soc_dai_driver *dai_drv,
  1468. struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
  1469. {
  1470. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1471. const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
  1472. struct snd_soc_tplg_stream_caps *caps;
  1473. struct snd_soc_tplg_private *private = &pcm->priv;
  1474. struct snd_sof_pcm *spcm;
  1475. int stream;
  1476. int ret;
  1477. /* nothing to do for BEs atm */
  1478. if (!pcm)
  1479. return 0;
  1480. spcm = kzalloc_obj(*spcm);
  1481. if (!spcm)
  1482. return -ENOMEM;
  1483. spcm->scomp = scomp;
  1484. for_each_pcm_streams(stream) {
  1485. spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
  1486. if (pcm->compress)
  1487. snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
  1488. else
  1489. snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
  1490. }
  1491. spcm->pcm = *pcm;
  1492. dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
  1493. /* perform pcm set op */
  1494. if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
  1495. ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
  1496. if (ret < 0) {
  1497. kfree(spcm);
  1498. return ret;
  1499. }
  1500. }
  1501. dai_drv->dobj.private = spcm;
  1502. list_add(&spcm->list, &sdev->pcm_list);
  1503. ret = sof_parse_tokens(scomp, spcm, stream_tokens,
  1504. ARRAY_SIZE(stream_tokens), private->array,
  1505. le32_to_cpu(private->size));
  1506. if (ret) {
  1507. dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
  1508. le32_to_cpu(private->size));
  1509. return ret;
  1510. }
  1511. /* do we need to allocate playback PCM DMA pages */
  1512. if (!spcm->pcm.playback)
  1513. goto capture;
  1514. stream = SNDRV_PCM_STREAM_PLAYBACK;
  1515. caps = &spcm->pcm.caps[stream];
  1516. /* allocate playback page table buffer */
  1517. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
  1518. PAGE_SIZE, &spcm->stream[stream].page_table);
  1519. if (ret < 0) {
  1520. dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
  1521. caps->name, ret);
  1522. return ret;
  1523. }
  1524. /* bind pcm to host comp */
  1525. ret = spcm_bind(scomp, spcm, stream);
  1526. if (ret) {
  1527. dev_err(scomp->dev,
  1528. "error: can't bind pcm to host\n");
  1529. goto free_playback_tables;
  1530. }
  1531. capture:
  1532. stream = SNDRV_PCM_STREAM_CAPTURE;
  1533. /* do we need to allocate capture PCM DMA pages */
  1534. if (!spcm->pcm.capture)
  1535. return ret;
  1536. caps = &spcm->pcm.caps[stream];
  1537. /* allocate capture page table buffer */
  1538. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
  1539. PAGE_SIZE, &spcm->stream[stream].page_table);
  1540. if (ret < 0) {
  1541. dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
  1542. caps->name, ret);
  1543. goto free_playback_tables;
  1544. }
  1545. /* bind pcm to host comp */
  1546. ret = spcm_bind(scomp, spcm, stream);
  1547. if (ret) {
  1548. dev_err(scomp->dev,
  1549. "error: can't bind pcm to host\n");
  1550. snd_dma_free_pages(&spcm->stream[stream].page_table);
  1551. goto free_playback_tables;
  1552. }
  1553. return ret;
  1554. free_playback_tables:
  1555. if (spcm->pcm.playback)
  1556. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
  1557. return ret;
  1558. }
  1559. static int sof_dai_unload(struct snd_soc_component *scomp,
  1560. struct snd_soc_dobj *dobj)
  1561. {
  1562. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1563. const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
  1564. struct snd_sof_pcm *spcm = dobj->private;
  1565. /* free PCM DMA pages */
  1566. if (spcm->pcm.playback)
  1567. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
  1568. if (spcm->pcm.capture)
  1569. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
  1570. /* perform pcm free op */
  1571. if (ipc_pcm_ops && ipc_pcm_ops->pcm_free)
  1572. ipc_pcm_ops->pcm_free(sdev, spcm);
  1573. /* remove from list and free spcm */
  1574. list_del(&spcm->list);
  1575. kfree(spcm);
  1576. return 0;
  1577. }
  1578. static const struct sof_topology_token common_dai_link_tokens[] = {
  1579. {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
  1580. offsetof(struct snd_sof_dai_link, type)},
  1581. };
  1582. /* DAI link - used for any driver specific init */
  1583. static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
  1584. struct snd_soc_tplg_link_config *cfg)
  1585. {
  1586. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1587. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1588. struct snd_soc_tplg_private *private = &cfg->priv;
  1589. const struct sof_token_info *token_list;
  1590. struct snd_sof_dai_link *slink;
  1591. u32 token_id = 0;
  1592. int num_tuples = 0;
  1593. int ret, num_sets;
  1594. if (!link->platforms) {
  1595. dev_err(scomp->dev, "error: no platforms\n");
  1596. return -EINVAL;
  1597. }
  1598. link->platforms->name = dev_name(scomp->dev);
  1599. if (tplg_ops && tplg_ops->link_setup) {
  1600. ret = tplg_ops->link_setup(sdev, link);
  1601. if (ret < 0)
  1602. return ret;
  1603. }
  1604. /* Set nonatomic property for FE dai links as their trigger action involves IPC's */
  1605. if (!link->no_pcm) {
  1606. link->nonatomic = true;
  1607. return 0;
  1608. }
  1609. /* check we have some tokens - we need at least DAI type */
  1610. if (le32_to_cpu(private->size) == 0) {
  1611. dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
  1612. return -EINVAL;
  1613. }
  1614. slink = kzalloc_obj(*slink);
  1615. if (!slink)
  1616. return -ENOMEM;
  1617. slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
  1618. slink->hw_configs = kmemdup_array(cfg->hw_config,
  1619. slink->num_hw_configs, sizeof(*slink->hw_configs),
  1620. GFP_KERNEL);
  1621. if (!slink->hw_configs) {
  1622. kfree(slink);
  1623. return -ENOMEM;
  1624. }
  1625. slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
  1626. slink->link = link;
  1627. dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
  1628. slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
  1629. ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
  1630. ARRAY_SIZE(common_dai_link_tokens),
  1631. private->array, le32_to_cpu(private->size));
  1632. if (ret < 0) {
  1633. dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
  1634. kfree(slink->hw_configs);
  1635. kfree(slink);
  1636. return ret;
  1637. }
  1638. token_list = tplg_ops ? tplg_ops->token_list : NULL;
  1639. if (!token_list)
  1640. goto out;
  1641. /* calculate size of tuples array */
  1642. num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
  1643. num_sets = slink->num_hw_configs;
  1644. switch (slink->type) {
  1645. case SOF_DAI_INTEL_SSP:
  1646. token_id = SOF_SSP_TOKENS;
  1647. num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
  1648. break;
  1649. case SOF_DAI_INTEL_DMIC:
  1650. token_id = SOF_DMIC_TOKENS;
  1651. num_tuples += token_list[SOF_DMIC_TOKENS].count;
  1652. /* Allocate memory for max PDM controllers */
  1653. num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
  1654. break;
  1655. case SOF_DAI_INTEL_HDA:
  1656. token_id = SOF_HDA_TOKENS;
  1657. num_tuples += token_list[SOF_HDA_TOKENS].count;
  1658. break;
  1659. case SOF_DAI_INTEL_ALH:
  1660. token_id = SOF_ALH_TOKENS;
  1661. num_tuples += token_list[SOF_ALH_TOKENS].count;
  1662. break;
  1663. case SOF_DAI_IMX_SAI:
  1664. token_id = SOF_SAI_TOKENS;
  1665. num_tuples += token_list[SOF_SAI_TOKENS].count;
  1666. break;
  1667. case SOF_DAI_IMX_ESAI:
  1668. token_id = SOF_ESAI_TOKENS;
  1669. num_tuples += token_list[SOF_ESAI_TOKENS].count;
  1670. break;
  1671. case SOF_DAI_MEDIATEK_AFE:
  1672. token_id = SOF_AFE_TOKENS;
  1673. num_tuples += token_list[SOF_AFE_TOKENS].count;
  1674. break;
  1675. case SOF_DAI_AMD_DMIC:
  1676. token_id = SOF_ACPDMIC_TOKENS;
  1677. num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
  1678. break;
  1679. case SOF_DAI_AMD_BT:
  1680. case SOF_DAI_AMD_SP:
  1681. case SOF_DAI_AMD_HS:
  1682. case SOF_DAI_AMD_SP_VIRTUAL:
  1683. case SOF_DAI_AMD_HS_VIRTUAL:
  1684. token_id = SOF_ACPI2S_TOKENS;
  1685. num_tuples += token_list[SOF_ACPI2S_TOKENS].count;
  1686. break;
  1687. case SOF_DAI_IMX_MICFIL:
  1688. token_id = SOF_MICFIL_TOKENS;
  1689. num_tuples += token_list[SOF_MICFIL_TOKENS].count;
  1690. break;
  1691. case SOF_DAI_AMD_SDW:
  1692. token_id = SOF_ACP_SDW_TOKENS;
  1693. num_tuples += token_list[SOF_ACP_SDW_TOKENS].count;
  1694. break;
  1695. default:
  1696. break;
  1697. }
  1698. /* allocate memory for tuples array */
  1699. slink->tuples = kzalloc_objs(*slink->tuples, num_tuples);
  1700. if (!slink->tuples) {
  1701. kfree(slink->hw_configs);
  1702. kfree(slink);
  1703. return -ENOMEM;
  1704. }
  1705. if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
  1706. /* parse one set of DAI link tokens */
  1707. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1708. SOF_DAI_LINK_TOKENS, 1, slink->tuples,
  1709. num_tuples, &slink->num_tuples);
  1710. if (ret < 0) {
  1711. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1712. token_list[SOF_DAI_LINK_TOKENS].name, link->name);
  1713. goto err;
  1714. }
  1715. }
  1716. /* nothing more to do if there are no DAI type-specific tokens defined */
  1717. if (!token_id || !token_list[token_id].tokens)
  1718. goto out;
  1719. /* parse "num_sets" sets of DAI-specific tokens */
  1720. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1721. token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
  1722. if (ret < 0) {
  1723. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1724. token_list[token_id].name, link->name);
  1725. goto err;
  1726. }
  1727. /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
  1728. if (token_id == SOF_DMIC_TOKENS) {
  1729. num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
  1730. slink->tuples, slink->num_tuples);
  1731. if (num_sets < 0) {
  1732. dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
  1733. ret = num_sets;
  1734. goto err;
  1735. }
  1736. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1737. SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
  1738. num_tuples, &slink->num_tuples);
  1739. if (ret < 0) {
  1740. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1741. token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
  1742. goto err;
  1743. }
  1744. }
  1745. out:
  1746. link->dobj.private = slink;
  1747. list_add(&slink->list, &sdev->dai_link_list);
  1748. return 0;
  1749. err:
  1750. kfree(slink->tuples);
  1751. kfree(slink->hw_configs);
  1752. kfree(slink);
  1753. return ret;
  1754. }
  1755. static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
  1756. {
  1757. struct snd_sof_dai_link *slink = dobj->private;
  1758. if (!slink)
  1759. return 0;
  1760. slink->link->platforms->name = NULL;
  1761. kfree(slink->tuples);
  1762. list_del(&slink->list);
  1763. kfree(slink->hw_configs);
  1764. kfree(slink);
  1765. dobj->private = NULL;
  1766. return 0;
  1767. }
  1768. /* DAI link - used for any driver specific init */
  1769. static int sof_route_load(struct snd_soc_component *scomp, int index,
  1770. struct snd_soc_dapm_route *route)
  1771. {
  1772. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1773. struct snd_sof_widget *source_swidget, *sink_swidget;
  1774. struct snd_soc_dobj *dobj = &route->dobj;
  1775. struct snd_sof_route *sroute;
  1776. int ret = 0;
  1777. /* allocate memory for sroute and connect */
  1778. sroute = kzalloc_obj(*sroute);
  1779. if (!sroute)
  1780. return -ENOMEM;
  1781. sroute->scomp = scomp;
  1782. dev_dbg(scomp->dev, "sink %s control %s source %s\n",
  1783. route->sink, route->control ? route->control : "none",
  1784. route->source);
  1785. /* source component */
  1786. source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
  1787. if (!source_swidget) {
  1788. dev_err(scomp->dev, "source %s for sink %s is not found\n",
  1789. route->source, route->sink);
  1790. ret = -EINVAL;
  1791. goto err;
  1792. }
  1793. /*
  1794. * Virtual widgets of type output/out_drv may be added in topology
  1795. * for compatibility. These are not handled by the FW.
  1796. * So, don't send routes whose source/sink widget is of such types
  1797. * to the DSP.
  1798. */
  1799. if (source_swidget->id == snd_soc_dapm_out_drv ||
  1800. source_swidget->id == snd_soc_dapm_output)
  1801. goto err;
  1802. /* sink component */
  1803. sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
  1804. if (!sink_swidget) {
  1805. dev_err(scomp->dev, "sink %s for source %s is not found\n",
  1806. route->sink, route->source);
  1807. ret = -EINVAL;
  1808. goto err;
  1809. }
  1810. /*
  1811. * Don't send routes whose sink widget is of type
  1812. * output or out_drv to the DSP
  1813. */
  1814. if (sink_swidget->id == snd_soc_dapm_out_drv ||
  1815. sink_swidget->id == snd_soc_dapm_output)
  1816. goto err;
  1817. sroute->route = route;
  1818. dobj->private = sroute;
  1819. sroute->src_widget = source_swidget;
  1820. sroute->sink_widget = sink_swidget;
  1821. /* add route to route list */
  1822. list_add(&sroute->list, &sdev->route_list);
  1823. return 0;
  1824. err:
  1825. kfree(sroute);
  1826. return ret;
  1827. }
  1828. /**
  1829. * sof_set_widget_pipeline - Set pipeline for a component
  1830. * @sdev: pointer to struct snd_sof_dev
  1831. * @spipe: pointer to struct snd_sof_pipeline
  1832. * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
  1833. *
  1834. * Return: 0 if successful, -EINVAL on error.
  1835. * The function checks if @swidget is associated with any volatile controls. If so, setting
  1836. * the dynamic_pipeline_widget is disallowed.
  1837. */
  1838. static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe,
  1839. struct snd_sof_widget *swidget)
  1840. {
  1841. struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
  1842. struct snd_sof_control *scontrol;
  1843. if (pipe_widget->dynamic_pipeline_widget) {
  1844. /* dynamic widgets cannot have volatile kcontrols */
  1845. list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
  1846. if (scontrol->comp_id == swidget->comp_id &&
  1847. (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
  1848. dev_err(sdev->dev,
  1849. "error: volatile control found for dynamic widget %s\n",
  1850. swidget->widget->name);
  1851. return -EINVAL;
  1852. }
  1853. }
  1854. /* set the pipeline and apply the dynamic_pipeline_widget_flag */
  1855. swidget->spipe = spipe;
  1856. swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
  1857. return 0;
  1858. }
  1859. /* completion - called at completion of firmware loading */
  1860. static int sof_complete(struct snd_soc_component *scomp)
  1861. {
  1862. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1863. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1864. const struct sof_ipc_tplg_widget_ops *widget_ops;
  1865. struct snd_sof_control *scontrol;
  1866. struct snd_sof_pipeline *spipe;
  1867. int ret;
  1868. widget_ops = tplg_ops ? tplg_ops->widget : NULL;
  1869. /* first update all control IPC structures based on the IPC version */
  1870. if (tplg_ops && tplg_ops->control_setup)
  1871. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  1872. ret = tplg_ops->control_setup(sdev, scontrol);
  1873. if (ret < 0) {
  1874. dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
  1875. scontrol->name);
  1876. return ret;
  1877. }
  1878. }
  1879. /* set up the IPC structures for the pipeline widgets */
  1880. list_for_each_entry(spipe, &sdev->pipeline_list, list) {
  1881. struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
  1882. struct snd_sof_widget *swidget;
  1883. pipe_widget->instance_id = -EINVAL;
  1884. /* Update the scheduler widget's IPC structure */
  1885. if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) {
  1886. ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget);
  1887. if (ret < 0) {
  1888. dev_err(sdev->dev, "failed updating IPC struct for %s\n",
  1889. pipe_widget->widget->name);
  1890. return ret;
  1891. }
  1892. }
  1893. /* set the pipeline and update the IPC structure for the non scheduler widgets */
  1894. list_for_each_entry(swidget, &sdev->widget_list, list)
  1895. if (swidget->widget->id != snd_soc_dapm_scheduler &&
  1896. swidget->pipeline_id == pipe_widget->pipeline_id) {
  1897. ret = sof_set_widget_pipeline(sdev, spipe, swidget);
  1898. if (ret < 0)
  1899. return ret;
  1900. if (widget_ops && widget_ops[swidget->id].ipc_setup) {
  1901. ret = widget_ops[swidget->id].ipc_setup(swidget);
  1902. if (ret < 0) {
  1903. dev_err(sdev->dev,
  1904. "failed updating IPC struct for %s\n",
  1905. swidget->widget->name);
  1906. return ret;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. /* verify topology components loading including dynamic pipelines */
  1912. if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
  1913. if (tplg_ops && tplg_ops->set_up_all_pipelines &&
  1914. tplg_ops->tear_down_all_pipelines) {
  1915. ret = tplg_ops->set_up_all_pipelines(sdev, true);
  1916. if (ret < 0) {
  1917. dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
  1918. ret);
  1919. return ret;
  1920. }
  1921. ret = tplg_ops->tear_down_all_pipelines(sdev, true);
  1922. if (ret < 0) {
  1923. dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
  1924. ret);
  1925. return ret;
  1926. }
  1927. }
  1928. }
  1929. /* set up static pipelines */
  1930. if (tplg_ops && tplg_ops->set_up_all_pipelines)
  1931. return tplg_ops->set_up_all_pipelines(sdev, false);
  1932. return 0;
  1933. }
  1934. /* manifest - optional to inform component of manifest */
  1935. static int sof_manifest(struct snd_soc_component *scomp, int index,
  1936. struct snd_soc_tplg_manifest *man)
  1937. {
  1938. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1939. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  1940. if (tplg_ops && tplg_ops->parse_manifest)
  1941. return tplg_ops->parse_manifest(scomp, index, man);
  1942. return 0;
  1943. }
  1944. /* vendor specific kcontrol handlers available for binding */
  1945. static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
  1946. {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
  1947. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
  1948. {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
  1949. {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
  1950. };
  1951. /* vendor specific bytes ext handlers available for binding */
  1952. static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
  1953. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
  1954. {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
  1955. };
  1956. static const struct snd_soc_tplg_ops sof_tplg_ops = {
  1957. /* external kcontrol init - used for any driver specific init */
  1958. .control_load = sof_control_load,
  1959. .control_unload = sof_control_unload,
  1960. /* external kcontrol init - used for any driver specific init */
  1961. .dapm_route_load = sof_route_load,
  1962. .dapm_route_unload = sof_route_unload,
  1963. /* external widget init - used for any driver specific init */
  1964. /* .widget_load is not currently used */
  1965. .widget_ready = sof_widget_ready,
  1966. .widget_unload = sof_widget_unload,
  1967. /* FE DAI - used for any driver specific init */
  1968. .dai_load = sof_dai_load,
  1969. .dai_unload = sof_dai_unload,
  1970. /* DAI link - used for any driver specific init */
  1971. .link_load = sof_link_load,
  1972. .link_unload = sof_link_unload,
  1973. /*
  1974. * No need to set the complete callback. sof_complete will be called explicitly after
  1975. * topology loading is complete.
  1976. */
  1977. /* manifest - optional to inform component of manifest */
  1978. .manifest = sof_manifest,
  1979. /* vendor specific kcontrol handlers available for binding */
  1980. .io_ops = sof_io_ops,
  1981. .io_ops_count = ARRAY_SIZE(sof_io_ops),
  1982. /* vendor specific bytes ext handlers available for binding */
  1983. .bytes_ext_ops = sof_bytes_ext_ops,
  1984. .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
  1985. };
  1986. static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol,
  1987. struct snd_ctl_elem_value *ucontrol)
  1988. {
  1989. return 0;
  1990. }
  1991. static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = {
  1992. {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
  1993. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
  1994. {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
  1995. {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
  1996. };
  1997. static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol,
  1998. unsigned int __user *binary_data,
  1999. unsigned int size)
  2000. {
  2001. return 0;
  2002. }
  2003. static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol,
  2004. const unsigned int __user *binary_data,
  2005. unsigned int size)
  2006. {
  2007. return 0;
  2008. }
  2009. static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = {
  2010. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put},
  2011. {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get},
  2012. };
  2013. /* external widget init - used for any driver specific init */
  2014. static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index,
  2015. struct snd_soc_dapm_widget *w,
  2016. struct snd_soc_tplg_dapm_widget *tw)
  2017. {
  2018. struct snd_soc_tplg_private *priv = &tw->priv;
  2019. int ret;
  2020. /* for snd_soc_dapm_widget.no_wname_in_kcontrol_name */
  2021. ret = sof_parse_tokens(scomp, w, dapm_widget_tokens,
  2022. ARRAY_SIZE(dapm_widget_tokens),
  2023. priv->array, le32_to_cpu(priv->size));
  2024. if (ret < 0) {
  2025. dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n",
  2026. w->name);
  2027. return ret;
  2028. }
  2029. if (WIDGET_IS_DAI(w->id)) {
  2030. static const struct sof_topology_token dai_tokens[] = {
  2031. {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 0}};
  2032. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  2033. struct snd_sof_widget *swidget;
  2034. struct snd_sof_dai *sdai;
  2035. swidget = kzalloc_obj(*swidget);
  2036. if (!swidget)
  2037. return -ENOMEM;
  2038. sdai = kzalloc_obj(*sdai);
  2039. if (!sdai) {
  2040. kfree(swidget);
  2041. return -ENOMEM;
  2042. }
  2043. ret = sof_parse_tokens(scomp, &sdai->type, dai_tokens, ARRAY_SIZE(dai_tokens),
  2044. priv->array, le32_to_cpu(priv->size));
  2045. if (ret < 0) {
  2046. dev_err(scomp->dev, "Failed to parse DAI tokens for %s\n", tw->name);
  2047. kfree(swidget);
  2048. kfree(sdai);
  2049. return ret;
  2050. }
  2051. ret = sof_connect_dai_widget(scomp, w, tw, sdai);
  2052. if (ret) {
  2053. kfree(swidget);
  2054. kfree(sdai);
  2055. return ret;
  2056. }
  2057. swidget->scomp = scomp;
  2058. swidget->widget = w;
  2059. swidget->private = sdai;
  2060. mutex_init(&swidget->setup_mutex);
  2061. w->dobj.private = swidget;
  2062. list_add(&swidget->list, &sdev->widget_list);
  2063. }
  2064. return 0;
  2065. }
  2066. static int sof_dspless_widget_unload(struct snd_soc_component *scomp,
  2067. struct snd_soc_dobj *dobj)
  2068. {
  2069. struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
  2070. if (WIDGET_IS_DAI(w->id)) {
  2071. struct snd_sof_widget *swidget = dobj->private;
  2072. sof_disconnect_dai_widget(scomp, w);
  2073. if (!swidget)
  2074. return 0;
  2075. /* remove and free swidget object */
  2076. list_del(&swidget->list);
  2077. kfree(swidget->private);
  2078. kfree(swidget);
  2079. }
  2080. return 0;
  2081. }
  2082. static int sof_dspless_link_load(struct snd_soc_component *scomp, int index,
  2083. struct snd_soc_dai_link *link,
  2084. struct snd_soc_tplg_link_config *cfg)
  2085. {
  2086. link->platforms->name = dev_name(scomp->dev);
  2087. /* Set nonatomic property for FE dai links for FE-BE compatibility */
  2088. if (!link->no_pcm)
  2089. link->nonatomic = true;
  2090. return 0;
  2091. }
  2092. static const struct snd_soc_tplg_ops sof_dspless_tplg_ops = {
  2093. /* external widget init - used for any driver specific init */
  2094. .widget_ready = sof_dspless_widget_ready,
  2095. .widget_unload = sof_dspless_widget_unload,
  2096. /* FE DAI - used for any driver specific init */
  2097. .dai_load = sof_dai_load,
  2098. .dai_unload = sof_dai_unload,
  2099. /* DAI link - used for any driver specific init */
  2100. .link_load = sof_dspless_link_load,
  2101. /* vendor specific kcontrol handlers available for binding */
  2102. .io_ops = sof_dspless_io_ops,
  2103. .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops),
  2104. /* vendor specific bytes ext handlers available for binding */
  2105. .bytes_ext_ops = sof_dspless_bytes_ext_ops,
  2106. .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops),
  2107. };
  2108. int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
  2109. {
  2110. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  2111. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  2112. const char *tplg_filename_prefix = sof_pdata->tplg_filename_prefix;
  2113. const struct firmware *fw;
  2114. const char **tplg_files;
  2115. int tplg_cnt = 0;
  2116. int ret;
  2117. int i;
  2118. tplg_files = kcalloc(scomp->card->num_links, sizeof(char *), GFP_KERNEL);
  2119. if (!tplg_files)
  2120. return -ENOMEM;
  2121. /* Try to use function topologies if possible */
  2122. if (!sof_pdata->disable_function_topology && !disable_function_topology &&
  2123. sof_pdata->machine && sof_pdata->machine->get_function_tplg_files) {
  2124. /*
  2125. * When the topology name contains 'dummy' word, it means that
  2126. * there is no fallback option to monolithic topology in case
  2127. * any of the function topologies might be missing.
  2128. * In this case we should use best effort to form the card,
  2129. * ignoring functionalities that we are missing a fragment for.
  2130. *
  2131. * Note: monolithic topologies also ignore these possibly
  2132. * missing functions, so the functionality of the card would be
  2133. * identical to the case if there would be a fallback monolithic
  2134. * topology created for the configuration.
  2135. */
  2136. bool no_fallback = strstr(file, "dummy");
  2137. tplg_cnt = sof_pdata->machine->get_function_tplg_files(scomp->card,
  2138. sof_pdata->machine,
  2139. tplg_filename_prefix,
  2140. &tplg_files,
  2141. no_fallback);
  2142. if (tplg_cnt < 0) {
  2143. kfree(tplg_files);
  2144. return tplg_cnt;
  2145. }
  2146. }
  2147. /*
  2148. * The monolithic topology will be used if there is no get_function_tplg_files
  2149. * callback or the callback returns 0.
  2150. */
  2151. if (!tplg_cnt) {
  2152. if (strstr(file, "dummy")) {
  2153. dev_err(scomp->dev,
  2154. "Function topology is required, please upgrade sof-firmware\n");
  2155. return -EINVAL;
  2156. }
  2157. tplg_files[0] = file;
  2158. tplg_cnt = 1;
  2159. dev_info(scomp->dev, "loading topology: %s\n", file);
  2160. } else {
  2161. dev_info(scomp->dev, "Using function topologies instead %s\n", file);
  2162. }
  2163. for (i = 0; i < tplg_cnt; i++) {
  2164. /* Only print the file names if the function topologies are used */
  2165. if (tplg_files[0] != file)
  2166. dev_info(scomp->dev, "loading topology %d: %s\n", i, tplg_files[i]);
  2167. ret = request_firmware(&fw, tplg_files[i], scomp->dev);
  2168. if (ret < 0) {
  2169. /*
  2170. * snd_soc_tplg_component_remove(scomp) will be called
  2171. * if snd_soc_tplg_component_load(scomp) failed and all
  2172. * objects in the scomp will be removed. No need to call
  2173. * snd_soc_tplg_component_remove(scomp) here.
  2174. */
  2175. dev_err(scomp->dev, "tplg request firmware %s failed err: %d\n",
  2176. tplg_files[i], ret);
  2177. goto out;
  2178. }
  2179. if (sdev->dspless_mode_selected)
  2180. ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
  2181. else
  2182. ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
  2183. release_firmware(fw);
  2184. if (ret < 0) {
  2185. dev_err(scomp->dev, "tplg %s component load failed %d\n",
  2186. tplg_files[i], ret);
  2187. goto out;
  2188. }
  2189. }
  2190. /* call sof_complete when topologies are loaded successfully */
  2191. ret = sof_complete(scomp);
  2192. out:
  2193. if (ret >= 0 && sdev->led_present)
  2194. ret = snd_ctl_led_request();
  2195. kfree(tplg_files);
  2196. return ret;
  2197. }
  2198. EXPORT_SYMBOL(snd_sof_load_topology);