topology.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <[email protected]>
  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. #define COMP_ID_UNASSIGNED 0xffffffff
  21. /*
  22. * Constants used in the computation of linear volume gain
  23. * from dB gain 20th root of 10 in Q1.16 fixed-point notation
  24. */
  25. #define VOL_TWENTIETH_ROOT_OF_TEN 73533
  26. /* 40th root of 10 in Q1.16 fixed-point notation*/
  27. #define VOL_FORTIETH_ROOT_OF_TEN 69419
  28. /* 0.5 dB step value in topology TLV */
  29. #define VOL_HALF_DB_STEP 50
  30. /* TLV data items */
  31. #define TLV_MIN 0
  32. #define TLV_STEP 1
  33. #define TLV_MUTE 2
  34. /**
  35. * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
  36. * token ID.
  37. * @scomp: pointer to SOC component
  38. * @object: target IPC struct to save the parsed values
  39. * @token_id: token ID for the token array to be searched
  40. * @tuples: pointer to the tuples array
  41. * @num_tuples: number of tuples in the tuples array
  42. * @object_size: size of the object
  43. * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
  44. * looks for @token_instance_num of each token in the token array associated
  45. * with the @token_id
  46. */
  47. int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
  48. struct snd_sof_tuple *tuples, int num_tuples,
  49. size_t object_size, int token_instance_num)
  50. {
  51. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  52. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  53. const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
  54. const struct sof_topology_token *tokens;
  55. int i, j;
  56. if (token_list[token_id].count < 0) {
  57. dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
  58. return -EINVAL;
  59. }
  60. /* No tokens to match */
  61. if (!token_list[token_id].count)
  62. return 0;
  63. tokens = token_list[token_id].tokens;
  64. if (!tokens) {
  65. dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
  66. return -EINVAL;
  67. }
  68. for (i = 0; i < token_list[token_id].count; i++) {
  69. int offset = 0;
  70. int num_tokens_matched = 0;
  71. for (j = 0; j < num_tuples; j++) {
  72. if (tokens[i].token == tuples[j].token) {
  73. switch (tokens[i].type) {
  74. case SND_SOC_TPLG_TUPLE_TYPE_WORD:
  75. {
  76. u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
  77. offset);
  78. *val = tuples[j].value.v;
  79. break;
  80. }
  81. case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
  82. case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
  83. {
  84. u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
  85. offset);
  86. *val = (u16)tuples[j].value.v;
  87. break;
  88. }
  89. case SND_SOC_TPLG_TUPLE_TYPE_STRING:
  90. {
  91. if (!tokens[i].get_token) {
  92. dev_err(scomp->dev,
  93. "get_token not defined for token %d in %s\n",
  94. tokens[i].token, token_list[token_id].name);
  95. return -EINVAL;
  96. }
  97. tokens[i].get_token((void *)tuples[j].value.s, object,
  98. tokens[i].offset + offset);
  99. break;
  100. }
  101. default:
  102. break;
  103. }
  104. num_tokens_matched++;
  105. /* found all required sets of current token. Move to the next one */
  106. if (!(num_tokens_matched % token_instance_num))
  107. break;
  108. /* move to the next object */
  109. offset += object_size;
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115. static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
  116. {
  117. /* we only support dB scale TLV type at the moment */
  118. if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
  119. return -EINVAL;
  120. /* min value in topology tlv data is multiplied by 100 */
  121. tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
  122. /* volume steps */
  123. tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
  124. TLV_DB_SCALE_MASK);
  125. /* mute ON/OFF */
  126. if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
  127. TLV_DB_SCALE_MUTE) == 0)
  128. tlv[TLV_MUTE] = 0;
  129. else
  130. tlv[TLV_MUTE] = 1;
  131. return 0;
  132. }
  133. /*
  134. * Function to truncate an unsigned 64-bit number
  135. * by x bits and return 32-bit unsigned number. This
  136. * function also takes care of rounding while truncating
  137. */
  138. static inline u32 vol_shift_64(u64 i, u32 x)
  139. {
  140. /* do not truncate more than 32 bits */
  141. if (x > 32)
  142. x = 32;
  143. if (x == 0)
  144. return (u32)i;
  145. return (u32)(((i >> (x - 1)) + 1) >> 1);
  146. }
  147. /*
  148. * Function to compute a ^ exp where,
  149. * a is a fractional number represented by a fixed-point
  150. * integer with a fractional world length of "fwl"
  151. * exp is an integer
  152. * fwl is the fractional word length
  153. * Return value is a fractional number represented by a
  154. * fixed-point integer with a fractional word length of "fwl"
  155. */
  156. static u32 vol_pow32(u32 a, int exp, u32 fwl)
  157. {
  158. int i, iter;
  159. u32 power = 1 << fwl;
  160. u64 numerator;
  161. /* if exponent is 0, return 1 */
  162. if (exp == 0)
  163. return power;
  164. /* determine the number of iterations based on the exponent */
  165. if (exp < 0)
  166. iter = exp * -1;
  167. else
  168. iter = exp;
  169. /* mutiply a "iter" times to compute power */
  170. for (i = 0; i < iter; i++) {
  171. /*
  172. * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
  173. * Truncate product back to fwl fractional bits with rounding
  174. */
  175. power = vol_shift_64((u64)power * a, fwl);
  176. }
  177. if (exp > 0) {
  178. /* if exp is positive, return the result */
  179. return power;
  180. }
  181. /* if exp is negative, return the multiplicative inverse */
  182. numerator = (u64)1 << (fwl << 1);
  183. do_div(numerator, power);
  184. return (u32)numerator;
  185. }
  186. /*
  187. * Function to calculate volume gain from TLV data.
  188. * This function can only handle gain steps that are multiples of 0.5 dB
  189. */
  190. u32 vol_compute_gain(u32 value, int *tlv)
  191. {
  192. int dB_gain;
  193. u32 linear_gain;
  194. int f_step;
  195. /* mute volume */
  196. if (value == 0 && tlv[TLV_MUTE])
  197. return 0;
  198. /*
  199. * compute dB gain from tlv. tlv_step
  200. * in topology is multiplied by 100
  201. */
  202. dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
  203. /*
  204. * compute linear gain represented by fixed-point
  205. * int with VOLUME_FWL fractional bits
  206. */
  207. linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
  208. /* extract the fractional part of volume step */
  209. f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
  210. /* if volume step is an odd multiple of 0.5 dB */
  211. if (f_step == VOL_HALF_DB_STEP && (value & 1))
  212. linear_gain = vol_shift_64((u64)linear_gain *
  213. VOL_FORTIETH_ROOT_OF_TEN,
  214. VOLUME_FWL);
  215. return linear_gain;
  216. }
  217. /*
  218. * Set up volume table for kcontrols from tlv data
  219. * "size" specifies the number of entries in the table
  220. */
  221. static int set_up_volume_table(struct snd_sof_control *scontrol,
  222. int tlv[SOF_TLV_ITEMS], int size)
  223. {
  224. struct snd_soc_component *scomp = scontrol->scomp;
  225. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  226. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  227. if (tplg_ops->control->set_up_volume_table)
  228. return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
  229. dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
  230. return -EINVAL;
  231. }
  232. struct sof_dai_types {
  233. const char *name;
  234. enum sof_ipc_dai_type type;
  235. };
  236. static const struct sof_dai_types sof_dais[] = {
  237. {"SSP", SOF_DAI_INTEL_SSP},
  238. {"HDA", SOF_DAI_INTEL_HDA},
  239. {"DMIC", SOF_DAI_INTEL_DMIC},
  240. {"ALH", SOF_DAI_INTEL_ALH},
  241. {"SAI", SOF_DAI_IMX_SAI},
  242. {"ESAI", SOF_DAI_IMX_ESAI},
  243. {"ACP", SOF_DAI_AMD_BT},
  244. {"ACPSP", SOF_DAI_AMD_SP},
  245. {"ACPDMIC", SOF_DAI_AMD_DMIC},
  246. {"ACPHS", SOF_DAI_AMD_HS},
  247. {"AFE", SOF_DAI_MEDIATEK_AFE},
  248. };
  249. static enum sof_ipc_dai_type find_dai(const char *name)
  250. {
  251. int i;
  252. for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
  253. if (strcmp(name, sof_dais[i].name) == 0)
  254. return sof_dais[i].type;
  255. }
  256. return SOF_DAI_INTEL_NONE;
  257. }
  258. /*
  259. * Supported Frame format types and lookup, add new ones to end of list.
  260. */
  261. struct sof_frame_types {
  262. const char *name;
  263. enum sof_ipc_frame frame;
  264. };
  265. static const struct sof_frame_types sof_frames[] = {
  266. {"s16le", SOF_IPC_FRAME_S16_LE},
  267. {"s24le", SOF_IPC_FRAME_S24_4LE},
  268. {"s32le", SOF_IPC_FRAME_S32_LE},
  269. {"float", SOF_IPC_FRAME_FLOAT},
  270. };
  271. static enum sof_ipc_frame find_format(const char *name)
  272. {
  273. int i;
  274. for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
  275. if (strcmp(name, sof_frames[i].name) == 0)
  276. return sof_frames[i].frame;
  277. }
  278. /* use s32le if nothing is specified */
  279. return SOF_IPC_FRAME_S32_LE;
  280. }
  281. int get_token_u32(void *elem, void *object, u32 offset)
  282. {
  283. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  284. u32 *val = (u32 *)((u8 *)object + offset);
  285. *val = le32_to_cpu(velem->value);
  286. return 0;
  287. }
  288. int get_token_u16(void *elem, void *object, u32 offset)
  289. {
  290. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  291. u16 *val = (u16 *)((u8 *)object + offset);
  292. *val = (u16)le32_to_cpu(velem->value);
  293. return 0;
  294. }
  295. int get_token_uuid(void *elem, void *object, u32 offset)
  296. {
  297. struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
  298. u8 *dst = (u8 *)object + offset;
  299. memcpy(dst, velem->uuid, UUID_SIZE);
  300. return 0;
  301. }
  302. int get_token_comp_format(void *elem, void *object, u32 offset)
  303. {
  304. u32 *val = (u32 *)((u8 *)object + offset);
  305. *val = find_format((const char *)elem);
  306. return 0;
  307. }
  308. int get_token_dai_type(void *elem, void *object, u32 offset)
  309. {
  310. u32 *val = (u32 *)((u8 *)object + offset);
  311. *val = find_dai((const char *)elem);
  312. return 0;
  313. }
  314. /* PCM */
  315. static const struct sof_topology_token stream_tokens[] = {
  316. {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  317. offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
  318. {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
  319. offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
  320. };
  321. /* Leds */
  322. static const struct sof_topology_token led_tokens[] = {
  323. {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  324. offsetof(struct snd_sof_led_control, use_led)},
  325. {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
  326. offsetof(struct snd_sof_led_control, direction)},
  327. };
  328. /**
  329. * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
  330. * @scomp: pointer to soc component
  331. * @object: target ipc struct for parsed values
  332. * @offset: offset within the object pointer
  333. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  334. * @num_tokens: number of tokens in tokens array
  335. * @array: source pointer to consecutive vendor arrays in topology
  336. *
  337. * This function parses multiple sets of string type tokens in vendor arrays
  338. */
  339. static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
  340. void *object, size_t offset,
  341. const struct sof_topology_token *tokens, int num_tokens,
  342. struct snd_soc_tplg_vendor_array *array)
  343. {
  344. struct snd_soc_tplg_vendor_uuid_elem *elem;
  345. int found = 0;
  346. int i, j;
  347. /* parse element by element */
  348. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  349. elem = &array->uuid[i];
  350. /* search for token */
  351. for (j = 0; j < num_tokens; j++) {
  352. /* match token type */
  353. if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
  354. continue;
  355. /* match token id */
  356. if (tokens[j].token != le32_to_cpu(elem->token))
  357. continue;
  358. /* matched - now load token */
  359. tokens[j].get_token(elem, object,
  360. offset + tokens[j].offset);
  361. found++;
  362. }
  363. }
  364. return found;
  365. }
  366. /**
  367. * sof_copy_tuples - Parse tokens and copy them to the @tuples array
  368. * @sdev: pointer to struct snd_sof_dev
  369. * @array: source pointer to consecutive vendor arrays in topology
  370. * @array_size: size of @array
  371. * @token_id: Token ID associated with a token array
  372. * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
  373. * looks for @token_instance_num of each token in the token array associated
  374. * with the @token_id
  375. * @tuples: tuples array to copy the matched tuples to
  376. * @tuples_size: size of @tuples
  377. * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
  378. *
  379. */
  380. static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
  381. int array_size, u32 token_id, int token_instance_num,
  382. struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
  383. {
  384. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  385. const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
  386. const struct sof_topology_token *tokens;
  387. int found = 0;
  388. int num_tokens, asize;
  389. int i, j;
  390. /* nothing to do if token_list is NULL */
  391. if (!token_list)
  392. return 0;
  393. if (!tuples || !num_copied_tuples) {
  394. dev_err(sdev->dev, "Invalid tuples array\n");
  395. return -EINVAL;
  396. }
  397. tokens = token_list[token_id].tokens;
  398. num_tokens = token_list[token_id].count;
  399. if (!tokens) {
  400. dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
  401. return -EINVAL;
  402. }
  403. /* check if there's space in the tuples array for new tokens */
  404. if (*num_copied_tuples >= tuples_size) {
  405. dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
  406. token_list[token_id].name);
  407. return -EINVAL;
  408. }
  409. while (array_size > 0 && found < num_tokens * token_instance_num) {
  410. asize = le32_to_cpu(array->size);
  411. /* validate asize */
  412. if (asize < 0) {
  413. dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
  414. return -EINVAL;
  415. }
  416. /* make sure there is enough data before parsing */
  417. array_size -= asize;
  418. if (array_size < 0) {
  419. dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
  420. return -EINVAL;
  421. }
  422. /* parse element by element */
  423. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  424. /* search for token */
  425. for (j = 0; j < num_tokens; j++) {
  426. /* match token type */
  427. if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
  428. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
  429. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
  430. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
  431. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
  432. continue;
  433. if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
  434. struct snd_soc_tplg_vendor_string_elem *elem;
  435. elem = &array->string[i];
  436. /* match token id */
  437. if (tokens[j].token != le32_to_cpu(elem->token))
  438. continue;
  439. tuples[*num_copied_tuples].token = tokens[j].token;
  440. tuples[*num_copied_tuples].value.s = elem->string;
  441. } else {
  442. struct snd_soc_tplg_vendor_value_elem *elem;
  443. elem = &array->value[i];
  444. /* match token id */
  445. if (tokens[j].token != le32_to_cpu(elem->token))
  446. continue;
  447. tuples[*num_copied_tuples].token = tokens[j].token;
  448. tuples[*num_copied_tuples].value.v =
  449. le32_to_cpu(elem->value);
  450. }
  451. found++;
  452. (*num_copied_tuples)++;
  453. /* stop if there's no space for any more new tuples */
  454. if (*num_copied_tuples == tuples_size)
  455. return 0;
  456. }
  457. /* stop when we've found the required token instances */
  458. if (found == num_tokens * token_instance_num)
  459. return 0;
  460. }
  461. /* next array */
  462. array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
  463. }
  464. return 0;
  465. }
  466. /**
  467. * sof_parse_string_tokens - Parse multiple sets of tokens
  468. * @scomp: pointer to soc component
  469. * @object: target ipc struct for parsed values
  470. * @offset: offset within the object pointer
  471. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  472. * @num_tokens: number of tokens in tokens array
  473. * @array: source pointer to consecutive vendor arrays in topology
  474. *
  475. * This function parses multiple sets of string type tokens in vendor arrays
  476. */
  477. static int sof_parse_string_tokens(struct snd_soc_component *scomp,
  478. void *object, int offset,
  479. const struct sof_topology_token *tokens, int num_tokens,
  480. struct snd_soc_tplg_vendor_array *array)
  481. {
  482. struct snd_soc_tplg_vendor_string_elem *elem;
  483. int found = 0;
  484. int i, j;
  485. /* parse element by element */
  486. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  487. elem = &array->string[i];
  488. /* search for token */
  489. for (j = 0; j < num_tokens; j++) {
  490. /* match token type */
  491. if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
  492. continue;
  493. /* match token id */
  494. if (tokens[j].token != le32_to_cpu(elem->token))
  495. continue;
  496. /* matched - now load token */
  497. tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
  498. found++;
  499. }
  500. }
  501. return found;
  502. }
  503. /**
  504. * sof_parse_word_tokens - Parse multiple sets of tokens
  505. * @scomp: pointer to soc component
  506. * @object: target ipc struct for parsed values
  507. * @offset: offset within the object pointer
  508. * @tokens: array of struct sof_topology_token containing the tokens to be matched
  509. * @num_tokens: number of tokens in tokens array
  510. * @array: source pointer to consecutive vendor arrays in topology
  511. *
  512. * This function parses multiple sets of word type tokens in vendor arrays
  513. */
  514. static int sof_parse_word_tokens(struct snd_soc_component *scomp,
  515. void *object, int offset,
  516. const struct sof_topology_token *tokens, int num_tokens,
  517. struct snd_soc_tplg_vendor_array *array)
  518. {
  519. struct snd_soc_tplg_vendor_value_elem *elem;
  520. int found = 0;
  521. int i, j;
  522. /* parse element by element */
  523. for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
  524. elem = &array->value[i];
  525. /* search for token */
  526. for (j = 0; j < num_tokens; j++) {
  527. /* match token type */
  528. if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
  529. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
  530. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
  531. tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
  532. continue;
  533. /* match token id */
  534. if (tokens[j].token != le32_to_cpu(elem->token))
  535. continue;
  536. /* load token */
  537. tokens[j].get_token(elem, object, offset + tokens[j].offset);
  538. found++;
  539. }
  540. }
  541. return found;
  542. }
  543. /**
  544. * sof_parse_token_sets - Parse multiple sets of tokens
  545. * @scomp: pointer to soc component
  546. * @object: target ipc struct for parsed values
  547. * @tokens: token definition array describing what tokens to parse
  548. * @count: number of tokens in definition array
  549. * @array: source pointer to consecutive vendor arrays in topology
  550. * @array_size: total size of @array
  551. * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
  552. * looks for @token_instance_num of each token in the @tokens
  553. * @object_size: offset to next target ipc struct with multiple sets
  554. *
  555. * This function parses multiple sets of tokens in vendor arrays into
  556. * consecutive ipc structs.
  557. */
  558. static int sof_parse_token_sets(struct snd_soc_component *scomp,
  559. void *object, const struct sof_topology_token *tokens,
  560. int count, struct snd_soc_tplg_vendor_array *array,
  561. int array_size, int token_instance_num, size_t object_size)
  562. {
  563. size_t offset = 0;
  564. int found = 0;
  565. int total = 0;
  566. int asize;
  567. while (array_size > 0 && total < count * token_instance_num) {
  568. asize = le32_to_cpu(array->size);
  569. /* validate asize */
  570. if (asize < 0) { /* FIXME: A zero-size array makes no sense */
  571. dev_err(scomp->dev, "error: invalid array size 0x%x\n",
  572. asize);
  573. return -EINVAL;
  574. }
  575. /* make sure there is enough data before parsing */
  576. array_size -= asize;
  577. if (array_size < 0) {
  578. dev_err(scomp->dev, "error: invalid array size 0x%x\n",
  579. asize);
  580. return -EINVAL;
  581. }
  582. /* call correct parser depending on type */
  583. switch (le32_to_cpu(array->type)) {
  584. case SND_SOC_TPLG_TUPLE_TYPE_UUID:
  585. found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
  586. array);
  587. break;
  588. case SND_SOC_TPLG_TUPLE_TYPE_STRING:
  589. found += sof_parse_string_tokens(scomp, object, offset, tokens, count,
  590. array);
  591. break;
  592. case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
  593. case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
  594. case SND_SOC_TPLG_TUPLE_TYPE_WORD:
  595. case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
  596. found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
  597. array);
  598. break;
  599. default:
  600. dev_err(scomp->dev, "error: unknown token type %d\n",
  601. array->type);
  602. return -EINVAL;
  603. }
  604. /* next array */
  605. array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
  606. + asize);
  607. /* move to next target struct */
  608. if (found >= count) {
  609. offset += object_size;
  610. total += found;
  611. found = 0;
  612. }
  613. }
  614. return 0;
  615. }
  616. /**
  617. * sof_parse_tokens - Parse one set of tokens
  618. * @scomp: pointer to soc component
  619. * @object: target ipc struct for parsed values
  620. * @tokens: token definition array describing what tokens to parse
  621. * @num_tokens: number of tokens in definition array
  622. * @array: source pointer to consecutive vendor arrays in topology
  623. * @array_size: total size of @array
  624. *
  625. * This function parses a single set of tokens in vendor arrays into
  626. * consecutive ipc structs.
  627. */
  628. static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
  629. const struct sof_topology_token *tokens, int num_tokens,
  630. struct snd_soc_tplg_vendor_array *array,
  631. int array_size)
  632. {
  633. /*
  634. * sof_parse_tokens is used when topology contains only a single set of
  635. * identical tuples arrays. So additional parameters to
  636. * sof_parse_token_sets are sets = 1 (only 1 set) and
  637. * object_size = 0 (irrelevant).
  638. */
  639. return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
  640. array_size, 1, 0);
  641. }
  642. /*
  643. * Standard Kcontrols.
  644. */
  645. static int sof_control_load_volume(struct snd_soc_component *scomp,
  646. struct snd_sof_control *scontrol,
  647. struct snd_kcontrol_new *kc,
  648. struct snd_soc_tplg_ctl_hdr *hdr)
  649. {
  650. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  651. struct snd_soc_tplg_mixer_control *mc =
  652. container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
  653. int tlv[SOF_TLV_ITEMS];
  654. unsigned int mask;
  655. int ret;
  656. /* validate topology data */
  657. if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
  658. return -EINVAL;
  659. /*
  660. * If control has more than 2 channels we need to override the info. This is because even if
  661. * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
  662. * pre-defined dapm control types (and related functions) creating the actual control
  663. * restrict the channels only to mono or stereo.
  664. */
  665. if (le32_to_cpu(mc->num_channels) > 2)
  666. kc->info = snd_sof_volume_info;
  667. scontrol->comp_id = sdev->next_comp_id;
  668. scontrol->min_volume_step = le32_to_cpu(mc->min);
  669. scontrol->max_volume_step = le32_to_cpu(mc->max);
  670. scontrol->num_channels = le32_to_cpu(mc->num_channels);
  671. scontrol->max = le32_to_cpu(mc->max);
  672. if (le32_to_cpu(mc->max) == 1)
  673. goto skip;
  674. /* extract tlv data */
  675. if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
  676. dev_err(scomp->dev, "error: invalid TLV data\n");
  677. return -EINVAL;
  678. }
  679. /* set up volume table */
  680. ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
  681. if (ret < 0) {
  682. dev_err(scomp->dev, "error: setting up volume table\n");
  683. return ret;
  684. }
  685. skip:
  686. /* set up possible led control from mixer private data */
  687. ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
  688. ARRAY_SIZE(led_tokens), mc->priv.array,
  689. le32_to_cpu(mc->priv.size));
  690. if (ret != 0) {
  691. dev_err(scomp->dev, "error: parse led tokens failed %d\n",
  692. le32_to_cpu(mc->priv.size));
  693. goto err;
  694. }
  695. if (scontrol->led_ctl.use_led) {
  696. mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
  697. SNDRV_CTL_ELEM_ACCESS_SPK_LED;
  698. scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
  699. scontrol->access |= mask;
  700. kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
  701. kc->access |= mask;
  702. sdev->led_present = true;
  703. }
  704. dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
  705. scontrol->comp_id, scontrol->num_channels);
  706. return 0;
  707. err:
  708. if (le32_to_cpu(mc->max) > 1)
  709. kfree(scontrol->volume_table);
  710. return ret;
  711. }
  712. static int sof_control_load_enum(struct snd_soc_component *scomp,
  713. struct snd_sof_control *scontrol,
  714. struct snd_kcontrol_new *kc,
  715. struct snd_soc_tplg_ctl_hdr *hdr)
  716. {
  717. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  718. struct snd_soc_tplg_enum_control *ec =
  719. container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
  720. /* validate topology data */
  721. if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
  722. return -EINVAL;
  723. scontrol->comp_id = sdev->next_comp_id;
  724. scontrol->num_channels = le32_to_cpu(ec->num_channels);
  725. dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
  726. scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
  727. return 0;
  728. }
  729. static int sof_control_load_bytes(struct snd_soc_component *scomp,
  730. struct snd_sof_control *scontrol,
  731. struct snd_kcontrol_new *kc,
  732. struct snd_soc_tplg_ctl_hdr *hdr)
  733. {
  734. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  735. struct snd_soc_tplg_bytes_control *control =
  736. container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
  737. struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
  738. size_t priv_size = le32_to_cpu(control->priv.size);
  739. scontrol->max_size = sbe->max;
  740. scontrol->comp_id = sdev->next_comp_id;
  741. dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
  742. /* copy the private data */
  743. if (priv_size > 0) {
  744. scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
  745. if (!scontrol->priv)
  746. return -ENOMEM;
  747. scontrol->priv_size = priv_size;
  748. }
  749. return 0;
  750. }
  751. /* external kcontrol init - used for any driver specific init */
  752. static int sof_control_load(struct snd_soc_component *scomp, int index,
  753. struct snd_kcontrol_new *kc,
  754. struct snd_soc_tplg_ctl_hdr *hdr)
  755. {
  756. struct soc_mixer_control *sm;
  757. struct soc_bytes_ext *sbe;
  758. struct soc_enum *se;
  759. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  760. struct snd_soc_dobj *dobj;
  761. struct snd_sof_control *scontrol;
  762. int ret;
  763. dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
  764. hdr->type, hdr->name);
  765. scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
  766. if (!scontrol)
  767. return -ENOMEM;
  768. scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
  769. if (!scontrol->name) {
  770. kfree(scontrol);
  771. return -ENOMEM;
  772. }
  773. scontrol->scomp = scomp;
  774. scontrol->access = kc->access;
  775. scontrol->info_type = le32_to_cpu(hdr->ops.info);
  776. scontrol->index = kc->index;
  777. switch (le32_to_cpu(hdr->ops.info)) {
  778. case SND_SOC_TPLG_CTL_VOLSW:
  779. case SND_SOC_TPLG_CTL_VOLSW_SX:
  780. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  781. sm = (struct soc_mixer_control *)kc->private_value;
  782. dobj = &sm->dobj;
  783. ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
  784. break;
  785. case SND_SOC_TPLG_CTL_BYTES:
  786. sbe = (struct soc_bytes_ext *)kc->private_value;
  787. dobj = &sbe->dobj;
  788. ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
  789. break;
  790. case SND_SOC_TPLG_CTL_ENUM:
  791. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  792. se = (struct soc_enum *)kc->private_value;
  793. dobj = &se->dobj;
  794. ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
  795. break;
  796. case SND_SOC_TPLG_CTL_RANGE:
  797. case SND_SOC_TPLG_CTL_STROBE:
  798. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  799. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  800. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  801. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  802. case SND_SOC_TPLG_DAPM_CTL_PIN:
  803. default:
  804. dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
  805. hdr->ops.get, hdr->ops.put, hdr->ops.info);
  806. kfree(scontrol->name);
  807. kfree(scontrol);
  808. return 0;
  809. }
  810. if (ret < 0) {
  811. kfree(scontrol->name);
  812. kfree(scontrol);
  813. return ret;
  814. }
  815. scontrol->led_ctl.led_value = -1;
  816. dobj->private = scontrol;
  817. list_add(&scontrol->list, &sdev->kcontrol_list);
  818. return 0;
  819. }
  820. static int sof_control_unload(struct snd_soc_component *scomp,
  821. struct snd_soc_dobj *dobj)
  822. {
  823. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  824. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  825. struct snd_sof_control *scontrol = dobj->private;
  826. int ret = 0;
  827. dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
  828. if (ipc_tplg_ops->control_free) {
  829. ret = ipc_tplg_ops->control_free(sdev, scontrol);
  830. if (ret < 0)
  831. dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
  832. }
  833. /* free all data before returning in case of error too */
  834. kfree(scontrol->ipc_control_data);
  835. kfree(scontrol->priv);
  836. kfree(scontrol->name);
  837. list_del(&scontrol->list);
  838. kfree(scontrol);
  839. return ret;
  840. }
  841. /*
  842. * DAI Topology
  843. */
  844. static int sof_connect_dai_widget(struct snd_soc_component *scomp,
  845. struct snd_soc_dapm_widget *w,
  846. struct snd_soc_tplg_dapm_widget *tw,
  847. struct snd_sof_dai *dai)
  848. {
  849. struct snd_soc_card *card = scomp->card;
  850. struct snd_soc_pcm_runtime *rtd;
  851. struct snd_soc_dai *cpu_dai;
  852. int i;
  853. if (!w->sname) {
  854. dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
  855. return -EINVAL;
  856. }
  857. list_for_each_entry(rtd, &card->rtd_list, list) {
  858. /* does stream match DAI link ? */
  859. if (!rtd->dai_link->stream_name ||
  860. strcmp(w->sname, rtd->dai_link->stream_name))
  861. continue;
  862. switch (w->id) {
  863. case snd_soc_dapm_dai_out:
  864. for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
  865. /*
  866. * Please create DAI widget in the right order
  867. * to ensure BE will connect to the right DAI
  868. * widget.
  869. */
  870. if (!cpu_dai->capture_widget) {
  871. cpu_dai->capture_widget = w;
  872. break;
  873. }
  874. }
  875. if (i == rtd->dai_link->num_cpus) {
  876. dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
  877. w->name);
  878. return -EINVAL;
  879. }
  880. dai->name = rtd->dai_link->name;
  881. dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
  882. w->name, rtd->dai_link->name);
  883. break;
  884. case snd_soc_dapm_dai_in:
  885. for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
  886. /*
  887. * Please create DAI widget in the right order
  888. * to ensure BE will connect to the right DAI
  889. * widget.
  890. */
  891. if (!cpu_dai->playback_widget) {
  892. cpu_dai->playback_widget = w;
  893. break;
  894. }
  895. }
  896. if (i == rtd->dai_link->num_cpus) {
  897. dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
  898. w->name);
  899. return -EINVAL;
  900. }
  901. dai->name = rtd->dai_link->name;
  902. dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
  903. w->name, rtd->dai_link->name);
  904. break;
  905. default:
  906. break;
  907. }
  908. }
  909. /* check we have a connection */
  910. if (!dai->name) {
  911. dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
  912. w->name, w->sname);
  913. return -EINVAL;
  914. }
  915. return 0;
  916. }
  917. static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
  918. struct snd_soc_dapm_widget *w)
  919. {
  920. struct snd_soc_card *card = scomp->card;
  921. struct snd_soc_pcm_runtime *rtd;
  922. const char *sname = w->sname;
  923. struct snd_soc_dai *cpu_dai;
  924. int i;
  925. if (!sname)
  926. return;
  927. list_for_each_entry(rtd, &card->rtd_list, list) {
  928. /* does stream match DAI link ? */
  929. if (!rtd->dai_link->stream_name ||
  930. strcmp(sname, rtd->dai_link->stream_name))
  931. continue;
  932. switch (w->id) {
  933. case snd_soc_dapm_dai_out:
  934. for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
  935. if (cpu_dai->capture_widget == w) {
  936. cpu_dai->capture_widget = NULL;
  937. break;
  938. }
  939. }
  940. break;
  941. case snd_soc_dapm_dai_in:
  942. for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
  943. if (cpu_dai->playback_widget == w) {
  944. cpu_dai->playback_widget = NULL;
  945. break;
  946. }
  947. }
  948. break;
  949. default:
  950. break;
  951. }
  952. }
  953. }
  954. /* bind PCM ID to host component ID */
  955. static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
  956. int dir)
  957. {
  958. struct snd_sof_widget *host_widget;
  959. host_widget = snd_sof_find_swidget_sname(scomp,
  960. spcm->pcm.caps[dir].name,
  961. dir);
  962. if (!host_widget) {
  963. dev_err(scomp->dev, "can't find host comp to bind pcm\n");
  964. return -EINVAL;
  965. }
  966. spcm->stream[dir].comp_id = host_widget->comp_id;
  967. return 0;
  968. }
  969. static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
  970. {
  971. int i;
  972. if (!tuples)
  973. return -EINVAL;
  974. for (i = 0; i < num_tuples; i++) {
  975. if (tuples[i].token == token_id)
  976. return tuples[i].value.v;
  977. }
  978. return -EINVAL;
  979. }
  980. static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
  981. struct snd_soc_tplg_dapm_widget *tw,
  982. enum sof_tokens *object_token_list, int count)
  983. {
  984. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  985. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  986. const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
  987. struct snd_soc_tplg_private *private = &tw->priv;
  988. int num_tuples = 0;
  989. int ret, i;
  990. if (count > 0 && !object_token_list) {
  991. dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
  992. return -EINVAL;
  993. }
  994. /* calculate max size of tuples array */
  995. for (i = 0; i < count; i++)
  996. num_tuples += token_list[object_token_list[i]].count;
  997. /* allocate memory for tuples array */
  998. swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
  999. if (!swidget->tuples)
  1000. return -ENOMEM;
  1001. /* parse token list for widget */
  1002. for (i = 0; i < count; i++) {
  1003. int num_sets = 1;
  1004. if (object_token_list[i] >= SOF_TOKEN_COUNT) {
  1005. dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
  1006. object_token_list[i], swidget->widget->name);
  1007. ret = -EINVAL;
  1008. goto err;
  1009. }
  1010. switch (object_token_list[i]) {
  1011. case SOF_COMP_EXT_TOKENS:
  1012. /* parse and save UUID in swidget */
  1013. ret = sof_parse_tokens(scomp, swidget,
  1014. token_list[object_token_list[i]].tokens,
  1015. token_list[object_token_list[i]].count,
  1016. private->array, le32_to_cpu(private->size));
  1017. if (ret < 0) {
  1018. dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
  1019. token_list[object_token_list[i]].name,
  1020. swidget->widget->name);
  1021. goto err;
  1022. }
  1023. continue;
  1024. case SOF_IN_AUDIO_FORMAT_TOKENS:
  1025. case SOF_OUT_AUDIO_FORMAT_TOKENS:
  1026. case SOF_COPIER_GATEWAY_CFG_TOKENS:
  1027. case SOF_AUDIO_FORMAT_BUFFER_SIZE_TOKENS:
  1028. num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_AUDIO_FORMATS,
  1029. swidget->tuples, swidget->num_tuples);
  1030. if (num_sets < 0) {
  1031. dev_err(sdev->dev, "Invalid audio format count for %s\n",
  1032. swidget->widget->name);
  1033. ret = num_sets;
  1034. goto err;
  1035. }
  1036. if (num_sets > 1) {
  1037. struct snd_sof_tuple *new_tuples;
  1038. num_tuples += token_list[object_token_list[i]].count * num_sets;
  1039. new_tuples = krealloc(swidget->tuples,
  1040. sizeof(*new_tuples) * num_tuples, GFP_KERNEL);
  1041. if (!new_tuples) {
  1042. ret = -ENOMEM;
  1043. goto err;
  1044. }
  1045. swidget->tuples = new_tuples;
  1046. }
  1047. break;
  1048. default:
  1049. break;
  1050. }
  1051. /* copy one set of tuples per token ID into swidget->tuples */
  1052. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1053. object_token_list[i], num_sets, swidget->tuples,
  1054. num_tuples, &swidget->num_tuples);
  1055. if (ret < 0) {
  1056. dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
  1057. token_list[object_token_list[i]].name, swidget->widget->name, ret);
  1058. goto err;
  1059. }
  1060. }
  1061. return 0;
  1062. err:
  1063. kfree(swidget->tuples);
  1064. return ret;
  1065. }
  1066. /* external widget init - used for any driver specific init */
  1067. static int sof_widget_ready(struct snd_soc_component *scomp, int index,
  1068. struct snd_soc_dapm_widget *w,
  1069. struct snd_soc_tplg_dapm_widget *tw)
  1070. {
  1071. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1072. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  1073. const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
  1074. struct snd_sof_widget *swidget;
  1075. struct snd_sof_dai *dai;
  1076. enum sof_tokens *token_list;
  1077. int token_list_size;
  1078. int ret = 0;
  1079. swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
  1080. if (!swidget)
  1081. return -ENOMEM;
  1082. swidget->scomp = scomp;
  1083. swidget->widget = w;
  1084. swidget->comp_id = sdev->next_comp_id++;
  1085. swidget->complete = 0;
  1086. swidget->id = w->id;
  1087. swidget->pipeline_id = index;
  1088. swidget->private = NULL;
  1089. dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
  1090. swidget->comp_id, index, swidget->id, tw->name,
  1091. strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
  1092. ? tw->sname : "none");
  1093. token_list = widget_ops[w->id].token_list;
  1094. token_list_size = widget_ops[w->id].token_list_size;
  1095. /* handle any special case widgets */
  1096. switch (w->id) {
  1097. case snd_soc_dapm_dai_in:
  1098. case snd_soc_dapm_dai_out:
  1099. dai = kzalloc(sizeof(*dai), GFP_KERNEL);
  1100. if (!dai) {
  1101. kfree(swidget);
  1102. return -ENOMEM;
  1103. }
  1104. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1105. if (!ret)
  1106. ret = sof_connect_dai_widget(scomp, w, tw, dai);
  1107. if (ret < 0) {
  1108. kfree(dai);
  1109. break;
  1110. }
  1111. list_add(&dai->list, &sdev->dai_list);
  1112. swidget->private = dai;
  1113. break;
  1114. case snd_soc_dapm_effect:
  1115. /* check we have some tokens - we need at least process type */
  1116. if (le32_to_cpu(tw->priv.size) == 0) {
  1117. dev_err(scomp->dev, "error: process tokens not found\n");
  1118. ret = -EINVAL;
  1119. break;
  1120. }
  1121. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1122. break;
  1123. case snd_soc_dapm_pga:
  1124. if (!le32_to_cpu(tw->num_kcontrols)) {
  1125. dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
  1126. tw->num_kcontrols);
  1127. ret = -EINVAL;
  1128. break;
  1129. }
  1130. fallthrough;
  1131. case snd_soc_dapm_mixer:
  1132. case snd_soc_dapm_buffer:
  1133. case snd_soc_dapm_scheduler:
  1134. case snd_soc_dapm_aif_out:
  1135. case snd_soc_dapm_aif_in:
  1136. case snd_soc_dapm_src:
  1137. case snd_soc_dapm_asrc:
  1138. case snd_soc_dapm_siggen:
  1139. case snd_soc_dapm_mux:
  1140. case snd_soc_dapm_demux:
  1141. ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
  1142. break;
  1143. case snd_soc_dapm_switch:
  1144. case snd_soc_dapm_dai_link:
  1145. case snd_soc_dapm_kcontrol:
  1146. default:
  1147. dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
  1148. break;
  1149. }
  1150. /* check token parsing reply */
  1151. if (ret < 0) {
  1152. dev_err(scomp->dev,
  1153. "error: failed to add widget id %d type %d name : %s stream %s\n",
  1154. tw->shift, swidget->id, tw->name,
  1155. strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
  1156. ? tw->sname : "none");
  1157. kfree(swidget);
  1158. return ret;
  1159. }
  1160. if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
  1161. swidget->core = SOF_DSP_PRIMARY_CORE;
  1162. } else {
  1163. int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
  1164. swidget->num_tuples);
  1165. if (core >= 0)
  1166. swidget->core = core;
  1167. }
  1168. /* bind widget to external event */
  1169. if (tw->event_type) {
  1170. if (widget_ops[w->id].bind_event) {
  1171. ret = widget_ops[w->id].bind_event(scomp, swidget,
  1172. le16_to_cpu(tw->event_type));
  1173. if (ret) {
  1174. dev_err(scomp->dev, "widget event binding failed for %s\n",
  1175. swidget->widget->name);
  1176. kfree(swidget->private);
  1177. kfree(swidget->tuples);
  1178. kfree(swidget);
  1179. return ret;
  1180. }
  1181. }
  1182. }
  1183. w->dobj.private = swidget;
  1184. list_add(&swidget->list, &sdev->widget_list);
  1185. return ret;
  1186. }
  1187. static int sof_route_unload(struct snd_soc_component *scomp,
  1188. struct snd_soc_dobj *dobj)
  1189. {
  1190. struct snd_sof_route *sroute;
  1191. sroute = dobj->private;
  1192. if (!sroute)
  1193. return 0;
  1194. /* free sroute and its private data */
  1195. kfree(sroute->private);
  1196. list_del(&sroute->list);
  1197. kfree(sroute);
  1198. return 0;
  1199. }
  1200. static int sof_widget_unload(struct snd_soc_component *scomp,
  1201. struct snd_soc_dobj *dobj)
  1202. {
  1203. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1204. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  1205. const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
  1206. const struct snd_kcontrol_new *kc;
  1207. struct snd_soc_dapm_widget *widget;
  1208. struct snd_sof_control *scontrol;
  1209. struct snd_sof_widget *swidget;
  1210. struct soc_mixer_control *sm;
  1211. struct soc_bytes_ext *sbe;
  1212. struct snd_sof_dai *dai;
  1213. struct soc_enum *se;
  1214. int i;
  1215. swidget = dobj->private;
  1216. if (!swidget)
  1217. return 0;
  1218. widget = swidget->widget;
  1219. switch (swidget->id) {
  1220. case snd_soc_dapm_dai_in:
  1221. case snd_soc_dapm_dai_out:
  1222. dai = swidget->private;
  1223. if (dai)
  1224. list_del(&dai->list);
  1225. sof_disconnect_dai_widget(scomp, widget);
  1226. break;
  1227. default:
  1228. break;
  1229. }
  1230. for (i = 0; i < widget->num_kcontrols; i++) {
  1231. kc = &widget->kcontrol_news[i];
  1232. switch (widget->dobj.widget.kcontrol_type[i]) {
  1233. case SND_SOC_TPLG_TYPE_MIXER:
  1234. sm = (struct soc_mixer_control *)kc->private_value;
  1235. scontrol = sm->dobj.private;
  1236. if (sm->max > 1)
  1237. kfree(scontrol->volume_table);
  1238. break;
  1239. case SND_SOC_TPLG_TYPE_ENUM:
  1240. se = (struct soc_enum *)kc->private_value;
  1241. scontrol = se->dobj.private;
  1242. break;
  1243. case SND_SOC_TPLG_TYPE_BYTES:
  1244. sbe = (struct soc_bytes_ext *)kc->private_value;
  1245. scontrol = sbe->dobj.private;
  1246. break;
  1247. default:
  1248. dev_warn(scomp->dev, "unsupported kcontrol_type\n");
  1249. goto out;
  1250. }
  1251. kfree(scontrol->ipc_control_data);
  1252. list_del(&scontrol->list);
  1253. kfree(scontrol->name);
  1254. kfree(scontrol);
  1255. }
  1256. out:
  1257. /* free IPC related data */
  1258. if (widget_ops[swidget->id].ipc_free)
  1259. widget_ops[swidget->id].ipc_free(swidget);
  1260. kfree(swidget->tuples);
  1261. /* remove and free swidget object */
  1262. list_del(&swidget->list);
  1263. kfree(swidget);
  1264. return 0;
  1265. }
  1266. /*
  1267. * DAI HW configuration.
  1268. */
  1269. /* FE DAI - used for any driver specific init */
  1270. static int sof_dai_load(struct snd_soc_component *scomp, int index,
  1271. struct snd_soc_dai_driver *dai_drv,
  1272. struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
  1273. {
  1274. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1275. struct snd_soc_tplg_stream_caps *caps;
  1276. struct snd_soc_tplg_private *private = &pcm->priv;
  1277. struct snd_sof_pcm *spcm;
  1278. int stream;
  1279. int ret;
  1280. /* nothing to do for BEs atm */
  1281. if (!pcm)
  1282. return 0;
  1283. spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
  1284. if (!spcm)
  1285. return -ENOMEM;
  1286. spcm->scomp = scomp;
  1287. for_each_pcm_streams(stream) {
  1288. spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
  1289. if (pcm->compress)
  1290. snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
  1291. else
  1292. snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
  1293. }
  1294. spcm->pcm = *pcm;
  1295. dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
  1296. dai_drv->dobj.private = spcm;
  1297. list_add(&spcm->list, &sdev->pcm_list);
  1298. ret = sof_parse_tokens(scomp, spcm, stream_tokens,
  1299. ARRAY_SIZE(stream_tokens), private->array,
  1300. le32_to_cpu(private->size));
  1301. if (ret) {
  1302. dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
  1303. le32_to_cpu(private->size));
  1304. return ret;
  1305. }
  1306. /* do we need to allocate playback PCM DMA pages */
  1307. if (!spcm->pcm.playback)
  1308. goto capture;
  1309. stream = SNDRV_PCM_STREAM_PLAYBACK;
  1310. caps = &spcm->pcm.caps[stream];
  1311. /* allocate playback page table buffer */
  1312. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
  1313. PAGE_SIZE, &spcm->stream[stream].page_table);
  1314. if (ret < 0) {
  1315. dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
  1316. caps->name, ret);
  1317. return ret;
  1318. }
  1319. /* bind pcm to host comp */
  1320. ret = spcm_bind(scomp, spcm, stream);
  1321. if (ret) {
  1322. dev_err(scomp->dev,
  1323. "error: can't bind pcm to host\n");
  1324. goto free_playback_tables;
  1325. }
  1326. capture:
  1327. stream = SNDRV_PCM_STREAM_CAPTURE;
  1328. /* do we need to allocate capture PCM DMA pages */
  1329. if (!spcm->pcm.capture)
  1330. return ret;
  1331. caps = &spcm->pcm.caps[stream];
  1332. /* allocate capture page table buffer */
  1333. ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
  1334. PAGE_SIZE, &spcm->stream[stream].page_table);
  1335. if (ret < 0) {
  1336. dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
  1337. caps->name, ret);
  1338. goto free_playback_tables;
  1339. }
  1340. /* bind pcm to host comp */
  1341. ret = spcm_bind(scomp, spcm, stream);
  1342. if (ret) {
  1343. dev_err(scomp->dev,
  1344. "error: can't bind pcm to host\n");
  1345. snd_dma_free_pages(&spcm->stream[stream].page_table);
  1346. goto free_playback_tables;
  1347. }
  1348. return ret;
  1349. free_playback_tables:
  1350. if (spcm->pcm.playback)
  1351. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
  1352. return ret;
  1353. }
  1354. static int sof_dai_unload(struct snd_soc_component *scomp,
  1355. struct snd_soc_dobj *dobj)
  1356. {
  1357. struct snd_sof_pcm *spcm = dobj->private;
  1358. /* free PCM DMA pages */
  1359. if (spcm->pcm.playback)
  1360. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
  1361. if (spcm->pcm.capture)
  1362. snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
  1363. /* remove from list and free spcm */
  1364. list_del(&spcm->list);
  1365. kfree(spcm);
  1366. return 0;
  1367. }
  1368. static const struct sof_topology_token common_dai_link_tokens[] = {
  1369. {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
  1370. offsetof(struct snd_sof_dai_link, type)},
  1371. };
  1372. /* DAI link - used for any driver specific init */
  1373. static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
  1374. struct snd_soc_tplg_link_config *cfg)
  1375. {
  1376. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1377. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  1378. const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
  1379. struct snd_soc_tplg_private *private = &cfg->priv;
  1380. struct snd_sof_dai_link *slink;
  1381. u32 token_id = 0;
  1382. int num_tuples = 0;
  1383. int ret, num_sets;
  1384. if (!link->platforms) {
  1385. dev_err(scomp->dev, "error: no platforms\n");
  1386. return -EINVAL;
  1387. }
  1388. link->platforms->name = dev_name(scomp->dev);
  1389. /*
  1390. * Set nonatomic property for FE dai links as their trigger action
  1391. * involves IPC's.
  1392. */
  1393. if (!link->no_pcm) {
  1394. link->nonatomic = true;
  1395. /*
  1396. * set default trigger order for all links. Exceptions to
  1397. * the rule will be handled in sof_pcm_dai_link_fixup()
  1398. * For playback, the sequence is the following: start FE,
  1399. * start BE, stop BE, stop FE; for Capture the sequence is
  1400. * inverted start BE, start FE, stop FE, stop BE
  1401. */
  1402. link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
  1403. SND_SOC_DPCM_TRIGGER_PRE;
  1404. link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
  1405. SND_SOC_DPCM_TRIGGER_POST;
  1406. /* nothing more to do for FE dai links */
  1407. return 0;
  1408. }
  1409. /* check we have some tokens - we need at least DAI type */
  1410. if (le32_to_cpu(private->size) == 0) {
  1411. dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
  1412. return -EINVAL;
  1413. }
  1414. slink = kzalloc(sizeof(*slink), GFP_KERNEL);
  1415. if (!slink)
  1416. return -ENOMEM;
  1417. slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
  1418. slink->hw_configs = kmemdup(cfg->hw_config,
  1419. sizeof(*slink->hw_configs) * slink->num_hw_configs,
  1420. GFP_KERNEL);
  1421. if (!slink->hw_configs) {
  1422. kfree(slink);
  1423. return -ENOMEM;
  1424. }
  1425. slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
  1426. slink->link = link;
  1427. dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
  1428. slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
  1429. ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
  1430. ARRAY_SIZE(common_dai_link_tokens),
  1431. private->array, le32_to_cpu(private->size));
  1432. if (ret < 0) {
  1433. dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
  1434. kfree(slink->hw_configs);
  1435. kfree(slink);
  1436. return ret;
  1437. }
  1438. if (!token_list)
  1439. goto out;
  1440. /* calculate size of tuples array */
  1441. num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
  1442. num_sets = slink->num_hw_configs;
  1443. switch (slink->type) {
  1444. case SOF_DAI_INTEL_SSP:
  1445. token_id = SOF_SSP_TOKENS;
  1446. num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
  1447. break;
  1448. case SOF_DAI_INTEL_DMIC:
  1449. token_id = SOF_DMIC_TOKENS;
  1450. num_tuples += token_list[SOF_DMIC_TOKENS].count;
  1451. /* Allocate memory for max PDM controllers */
  1452. num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
  1453. break;
  1454. case SOF_DAI_INTEL_HDA:
  1455. token_id = SOF_HDA_TOKENS;
  1456. num_tuples += token_list[SOF_HDA_TOKENS].count;
  1457. break;
  1458. case SOF_DAI_INTEL_ALH:
  1459. token_id = SOF_ALH_TOKENS;
  1460. num_tuples += token_list[SOF_ALH_TOKENS].count;
  1461. break;
  1462. case SOF_DAI_IMX_SAI:
  1463. token_id = SOF_SAI_TOKENS;
  1464. num_tuples += token_list[SOF_SAI_TOKENS].count;
  1465. break;
  1466. case SOF_DAI_IMX_ESAI:
  1467. token_id = SOF_ESAI_TOKENS;
  1468. num_tuples += token_list[SOF_ESAI_TOKENS].count;
  1469. break;
  1470. case SOF_DAI_MEDIATEK_AFE:
  1471. token_id = SOF_AFE_TOKENS;
  1472. num_tuples += token_list[SOF_AFE_TOKENS].count;
  1473. break;
  1474. case SOF_DAI_AMD_DMIC:
  1475. token_id = SOF_ACPDMIC_TOKENS;
  1476. num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
  1477. break;
  1478. default:
  1479. break;
  1480. }
  1481. /* allocate memory for tuples array */
  1482. slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
  1483. if (!slink->tuples) {
  1484. kfree(slink->hw_configs);
  1485. kfree(slink);
  1486. return -ENOMEM;
  1487. }
  1488. if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
  1489. /* parse one set of DAI link tokens */
  1490. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1491. SOF_DAI_LINK_TOKENS, 1, slink->tuples,
  1492. num_tuples, &slink->num_tuples);
  1493. if (ret < 0) {
  1494. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1495. token_list[SOF_DAI_LINK_TOKENS].name, link->name);
  1496. goto err;
  1497. }
  1498. }
  1499. /* nothing more to do if there are no DAI type-specific tokens defined */
  1500. if (!token_id || !token_list[token_id].tokens)
  1501. goto out;
  1502. /* parse "num_sets" sets of DAI-specific tokens */
  1503. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1504. token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
  1505. if (ret < 0) {
  1506. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1507. token_list[token_id].name, link->name);
  1508. goto err;
  1509. }
  1510. /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
  1511. if (token_id == SOF_DMIC_TOKENS) {
  1512. num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
  1513. slink->tuples, slink->num_tuples);
  1514. if (num_sets < 0) {
  1515. dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
  1516. ret = num_sets;
  1517. goto err;
  1518. }
  1519. ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
  1520. SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
  1521. num_tuples, &slink->num_tuples);
  1522. if (ret < 0) {
  1523. dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
  1524. token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
  1525. goto err;
  1526. }
  1527. }
  1528. out:
  1529. link->dobj.private = slink;
  1530. list_add(&slink->list, &sdev->dai_link_list);
  1531. return 0;
  1532. err:
  1533. kfree(slink->tuples);
  1534. kfree(slink->hw_configs);
  1535. kfree(slink);
  1536. return ret;
  1537. }
  1538. static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
  1539. {
  1540. struct snd_sof_dai_link *slink = dobj->private;
  1541. if (!slink)
  1542. return 0;
  1543. kfree(slink->tuples);
  1544. list_del(&slink->list);
  1545. kfree(slink->hw_configs);
  1546. kfree(slink);
  1547. dobj->private = NULL;
  1548. return 0;
  1549. }
  1550. /* DAI link - used for any driver specific init */
  1551. static int sof_route_load(struct snd_soc_component *scomp, int index,
  1552. struct snd_soc_dapm_route *route)
  1553. {
  1554. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1555. struct snd_sof_widget *source_swidget, *sink_swidget;
  1556. struct snd_soc_dobj *dobj = &route->dobj;
  1557. struct snd_sof_route *sroute;
  1558. int ret = 0;
  1559. /* allocate memory for sroute and connect */
  1560. sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
  1561. if (!sroute)
  1562. return -ENOMEM;
  1563. sroute->scomp = scomp;
  1564. dev_dbg(scomp->dev, "sink %s control %s source %s\n",
  1565. route->sink, route->control ? route->control : "none",
  1566. route->source);
  1567. /* source component */
  1568. source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
  1569. if (!source_swidget) {
  1570. dev_err(scomp->dev, "error: source %s not found\n",
  1571. route->source);
  1572. ret = -EINVAL;
  1573. goto err;
  1574. }
  1575. /*
  1576. * Virtual widgets of type output/out_drv may be added in topology
  1577. * for compatibility. These are not handled by the FW.
  1578. * So, don't send routes whose source/sink widget is of such types
  1579. * to the DSP.
  1580. */
  1581. if (source_swidget->id == snd_soc_dapm_out_drv ||
  1582. source_swidget->id == snd_soc_dapm_output)
  1583. goto err;
  1584. /* sink component */
  1585. sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
  1586. if (!sink_swidget) {
  1587. dev_err(scomp->dev, "error: sink %s not found\n",
  1588. route->sink);
  1589. ret = -EINVAL;
  1590. goto err;
  1591. }
  1592. /*
  1593. * Don't send routes whose sink widget is of type
  1594. * output or out_drv to the DSP
  1595. */
  1596. if (sink_swidget->id == snd_soc_dapm_out_drv ||
  1597. sink_swidget->id == snd_soc_dapm_output)
  1598. goto err;
  1599. sroute->route = route;
  1600. dobj->private = sroute;
  1601. sroute->src_widget = source_swidget;
  1602. sroute->sink_widget = sink_swidget;
  1603. /* add route to route list */
  1604. list_add(&sroute->list, &sdev->route_list);
  1605. return 0;
  1606. err:
  1607. kfree(sroute);
  1608. return ret;
  1609. }
  1610. /**
  1611. * sof_set_pipe_widget - Set pipe_widget for a component
  1612. * @sdev: pointer to struct snd_sof_dev
  1613. * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
  1614. * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
  1615. *
  1616. * Return: 0 if successful, -EINVAL on error.
  1617. * The function checks if @swidget is associated with any volatile controls. If so, setting
  1618. * the dynamic_pipeline_widget is disallowed.
  1619. */
  1620. static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
  1621. struct snd_sof_widget *swidget)
  1622. {
  1623. struct snd_sof_control *scontrol;
  1624. if (pipe_widget->dynamic_pipeline_widget) {
  1625. /* dynamic widgets cannot have volatile kcontrols */
  1626. list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
  1627. if (scontrol->comp_id == swidget->comp_id &&
  1628. (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
  1629. dev_err(sdev->dev,
  1630. "error: volatile control found for dynamic widget %s\n",
  1631. swidget->widget->name);
  1632. return -EINVAL;
  1633. }
  1634. }
  1635. /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
  1636. swidget->pipe_widget = pipe_widget;
  1637. swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
  1638. return 0;
  1639. }
  1640. /* completion - called at completion of firmware loading */
  1641. static int sof_complete(struct snd_soc_component *scomp)
  1642. {
  1643. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1644. struct snd_sof_widget *swidget, *comp_swidget;
  1645. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  1646. const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
  1647. struct snd_sof_control *scontrol;
  1648. int ret;
  1649. /* first update all control IPC structures based on the IPC version */
  1650. if (ipc_tplg_ops->control_setup)
  1651. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  1652. ret = ipc_tplg_ops->control_setup(sdev, scontrol);
  1653. if (ret < 0) {
  1654. dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
  1655. scontrol->name);
  1656. return ret;
  1657. }
  1658. }
  1659. /*
  1660. * then update all widget IPC structures. If any of the ipc_setup callbacks fail, the
  1661. * topology will be removed and all widgets will be unloaded resulting in freeing all
  1662. * associated memories.
  1663. */
  1664. list_for_each_entry(swidget, &sdev->widget_list, list) {
  1665. if (widget_ops[swidget->id].ipc_setup) {
  1666. ret = widget_ops[swidget->id].ipc_setup(swidget);
  1667. if (ret < 0) {
  1668. dev_err(sdev->dev, "failed updating IPC struct for %s\n",
  1669. swidget->widget->name);
  1670. return ret;
  1671. }
  1672. }
  1673. }
  1674. /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
  1675. list_for_each_entry(swidget, &sdev->widget_list, list) {
  1676. switch (swidget->id) {
  1677. case snd_soc_dapm_scheduler:
  1678. /*
  1679. * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
  1680. * for all widgets that have the same pipeline ID as the scheduler widget
  1681. */
  1682. list_for_each_entry(comp_swidget, &sdev->widget_list, list)
  1683. if (comp_swidget->pipeline_id == swidget->pipeline_id) {
  1684. ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
  1685. if (ret < 0)
  1686. return ret;
  1687. }
  1688. break;
  1689. default:
  1690. break;
  1691. }
  1692. }
  1693. /* verify topology components loading including dynamic pipelines */
  1694. if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
  1695. if (ipc_tplg_ops->set_up_all_pipelines && ipc_tplg_ops->tear_down_all_pipelines) {
  1696. ret = ipc_tplg_ops->set_up_all_pipelines(sdev, true);
  1697. if (ret < 0) {
  1698. dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
  1699. ret);
  1700. return ret;
  1701. }
  1702. ret = ipc_tplg_ops->tear_down_all_pipelines(sdev, true);
  1703. if (ret < 0) {
  1704. dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
  1705. ret);
  1706. return ret;
  1707. }
  1708. }
  1709. }
  1710. /* set up static pipelines */
  1711. if (ipc_tplg_ops->set_up_all_pipelines)
  1712. return ipc_tplg_ops->set_up_all_pipelines(sdev, false);
  1713. return 0;
  1714. }
  1715. /* manifest - optional to inform component of manifest */
  1716. static int sof_manifest(struct snd_soc_component *scomp, int index,
  1717. struct snd_soc_tplg_manifest *man)
  1718. {
  1719. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1720. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  1721. if (ipc_tplg_ops->parse_manifest)
  1722. return ipc_tplg_ops->parse_manifest(scomp, index, man);
  1723. return 0;
  1724. }
  1725. /* vendor specific kcontrol handlers available for binding */
  1726. static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
  1727. {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
  1728. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
  1729. {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
  1730. {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
  1731. };
  1732. /* vendor specific bytes ext handlers available for binding */
  1733. static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
  1734. {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
  1735. {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
  1736. };
  1737. static struct snd_soc_tplg_ops sof_tplg_ops = {
  1738. /* external kcontrol init - used for any driver specific init */
  1739. .control_load = sof_control_load,
  1740. .control_unload = sof_control_unload,
  1741. /* external kcontrol init - used for any driver specific init */
  1742. .dapm_route_load = sof_route_load,
  1743. .dapm_route_unload = sof_route_unload,
  1744. /* external widget init - used for any driver specific init */
  1745. /* .widget_load is not currently used */
  1746. .widget_ready = sof_widget_ready,
  1747. .widget_unload = sof_widget_unload,
  1748. /* FE DAI - used for any driver specific init */
  1749. .dai_load = sof_dai_load,
  1750. .dai_unload = sof_dai_unload,
  1751. /* DAI link - used for any driver specific init */
  1752. .link_load = sof_link_load,
  1753. .link_unload = sof_link_unload,
  1754. /* completion - called at completion of firmware loading */
  1755. .complete = sof_complete,
  1756. /* manifest - optional to inform component of manifest */
  1757. .manifest = sof_manifest,
  1758. /* vendor specific kcontrol handlers available for binding */
  1759. .io_ops = sof_io_ops,
  1760. .io_ops_count = ARRAY_SIZE(sof_io_ops),
  1761. /* vendor specific bytes ext handlers available for binding */
  1762. .bytes_ext_ops = sof_bytes_ext_ops,
  1763. .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
  1764. };
  1765. int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
  1766. {
  1767. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  1768. const struct firmware *fw;
  1769. int ret;
  1770. dev_dbg(scomp->dev, "loading topology:%s\n", file);
  1771. ret = request_firmware(&fw, file, scomp->dev);
  1772. if (ret < 0) {
  1773. dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
  1774. file, ret);
  1775. dev_err(scomp->dev,
  1776. "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
  1777. return ret;
  1778. }
  1779. ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
  1780. if (ret < 0) {
  1781. dev_err(scomp->dev, "error: tplg component load failed %d\n",
  1782. ret);
  1783. ret = -EINVAL;
  1784. }
  1785. release_firmware(fw);
  1786. if (ret >= 0 && sdev->led_present)
  1787. ret = snd_ctl_led_request();
  1788. return ret;
  1789. }
  1790. EXPORT_SYMBOL(snd_sof_load_topology);