topology.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021 Intel Corporation
  4. //
  5. // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6. // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7. //
  8. #include <linux/firmware.h>
  9. #include <linux/uuid.h>
  10. #include <sound/soc.h>
  11. #include <sound/soc-acpi.h>
  12. #include <sound/soc-topology.h>
  13. #include <uapi/sound/intel/avs/tokens.h>
  14. #include "avs.h"
  15. #include "control.h"
  16. #include "topology.h"
  17. #include "utils.h"
  18. /* Get pointer to vendor array at the specified offset. */
  19. #define avs_tplg_vendor_array_at(array, offset) \
  20. ((struct snd_soc_tplg_vendor_array *)((u8 *)array + offset))
  21. /* Get pointer to vendor array that is next in line. */
  22. #define avs_tplg_vendor_array_next(array) \
  23. (avs_tplg_vendor_array_at(array, le32_to_cpu((array)->size)))
  24. /*
  25. * Scan provided block of tuples for the specified token. If found,
  26. * @offset is updated with position at which first matching token is
  27. * located.
  28. *
  29. * Returns 0 on success, -ENOENT if not found and error code otherwise.
  30. */
  31. static int
  32. avs_tplg_vendor_array_lookup(struct snd_soc_tplg_vendor_array *tuples,
  33. u32 block_size, u32 token, u32 *offset)
  34. {
  35. u32 pos = 0;
  36. while (block_size > 0) {
  37. struct snd_soc_tplg_vendor_value_elem *tuple;
  38. u32 tuples_size = le32_to_cpu(tuples->size);
  39. if (tuples_size > block_size)
  40. return -EINVAL;
  41. tuple = tuples->value;
  42. if (le32_to_cpu(tuple->token) == token) {
  43. *offset = pos;
  44. return 0;
  45. }
  46. block_size -= tuples_size;
  47. pos += tuples_size;
  48. tuples = avs_tplg_vendor_array_next(tuples);
  49. }
  50. return -ENOENT;
  51. }
  52. /*
  53. * See avs_tplg_vendor_array_lookup() for description.
  54. *
  55. * Behaves exactly like avs_tplg_vendor_lookup() but starts from the
  56. * next vendor array in line. Useful when searching for the finish line
  57. * of an arbitrary entry in a list of entries where each is composed of
  58. * several vendor tuples and a specific token marks the beginning of
  59. * a new entry block.
  60. */
  61. static int
  62. avs_tplg_vendor_array_lookup_next(struct snd_soc_tplg_vendor_array *tuples,
  63. u32 block_size, u32 token, u32 *offset)
  64. {
  65. u32 tuples_size = le32_to_cpu(tuples->size);
  66. int ret;
  67. if (tuples_size > block_size)
  68. return -EINVAL;
  69. tuples = avs_tplg_vendor_array_next(tuples);
  70. block_size -= tuples_size;
  71. ret = avs_tplg_vendor_array_lookup(tuples, block_size, token, offset);
  72. if (!ret)
  73. *offset += tuples_size;
  74. return ret;
  75. }
  76. /*
  77. * Scan provided block of tuples for the specified token which marks
  78. * the border of an entry block. Behavior is similar to
  79. * avs_tplg_vendor_array_lookup() except 0 is also returned if no
  80. * matching token has been found. In such case, returned @size is
  81. * assigned to @block_size as the entire block belongs to the current
  82. * entry.
  83. *
  84. * Returns 0 on success, error code otherwise.
  85. */
  86. static int
  87. avs_tplg_vendor_entry_size(struct snd_soc_tplg_vendor_array *tuples,
  88. u32 block_size, u32 entry_id_token, u32 *size)
  89. {
  90. int ret;
  91. ret = avs_tplg_vendor_array_lookup_next(tuples, block_size, entry_id_token, size);
  92. if (ret == -ENOENT) {
  93. *size = block_size;
  94. ret = 0;
  95. }
  96. return ret;
  97. }
  98. /*
  99. * Vendor tuple parsing descriptor.
  100. *
  101. * @token: vendor specific token that identifies tuple
  102. * @type: tuple type, one of SND_SOC_TPLG_TUPLE_TYPE_XXX
  103. * @offset: offset of a struct's field to initialize
  104. * @parse: parsing function, extracts and assigns value to object's field
  105. */
  106. struct avs_tplg_token_parser {
  107. enum avs_tplg_token token;
  108. u32 type;
  109. u32 offset;
  110. int (*parse)(struct snd_soc_component *comp, void *elem, void *object, u32 offset);
  111. };
  112. static int
  113. avs_parse_uuid_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  114. {
  115. struct snd_soc_tplg_vendor_uuid_elem *tuple = elem;
  116. guid_t *val = (guid_t *)((u8 *)object + offset);
  117. guid_copy((guid_t *)val, (const guid_t *)&tuple->uuid);
  118. return 0;
  119. }
  120. static int
  121. avs_parse_bool_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  122. {
  123. struct snd_soc_tplg_vendor_value_elem *tuple = elem;
  124. bool *val = (bool *)((u8 *)object + offset);
  125. *val = le32_to_cpu(tuple->value);
  126. return 0;
  127. }
  128. static int
  129. avs_parse_byte_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  130. {
  131. struct snd_soc_tplg_vendor_value_elem *tuple = elem;
  132. u8 *val = ((u8 *)object + offset);
  133. *val = le32_to_cpu(tuple->value);
  134. return 0;
  135. }
  136. static int
  137. avs_parse_short_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  138. {
  139. struct snd_soc_tplg_vendor_value_elem *tuple = elem;
  140. u16 *val = (u16 *)((u8 *)object + offset);
  141. *val = le32_to_cpu(tuple->value);
  142. return 0;
  143. }
  144. static int
  145. avs_parse_word_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  146. {
  147. struct snd_soc_tplg_vendor_value_elem *tuple = elem;
  148. u32 *val = (u32 *)((u8 *)object + offset);
  149. *val = le32_to_cpu(tuple->value);
  150. return 0;
  151. }
  152. static int
  153. avs_parse_string_token(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  154. {
  155. struct snd_soc_tplg_vendor_string_elem *tuple = elem;
  156. char *val = (char *)((u8 *)object + offset);
  157. snprintf(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", tuple->string);
  158. return 0;
  159. }
  160. static int avs_parse_uuid_tokens(struct snd_soc_component *comp, void *object,
  161. const struct avs_tplg_token_parser *parsers, int count,
  162. struct snd_soc_tplg_vendor_array *tuples)
  163. {
  164. struct snd_soc_tplg_vendor_uuid_elem *tuple;
  165. int ret, i, j;
  166. /* Parse element by element. */
  167. for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
  168. tuple = &tuples->uuid[i];
  169. for (j = 0; j < count; j++) {
  170. /* Ignore non-UUID tokens. */
  171. if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID ||
  172. parsers[j].token != le32_to_cpu(tuple->token))
  173. continue;
  174. ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
  175. if (ret)
  176. return ret;
  177. }
  178. }
  179. return 0;
  180. }
  181. static int avs_parse_string_tokens(struct snd_soc_component *comp, void *object,
  182. const struct avs_tplg_token_parser *parsers, int count,
  183. struct snd_soc_tplg_vendor_array *tuples)
  184. {
  185. struct snd_soc_tplg_vendor_string_elem *tuple;
  186. int ret, i, j;
  187. /* Parse element by element. */
  188. for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
  189. tuple = &tuples->string[i];
  190. for (j = 0; j < count; j++) {
  191. /* Ignore non-string tokens. */
  192. if (parsers[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING ||
  193. parsers[j].token != le32_to_cpu(tuple->token))
  194. continue;
  195. ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
  196. if (ret)
  197. return ret;
  198. }
  199. }
  200. return 0;
  201. }
  202. static int avs_parse_word_tokens(struct snd_soc_component *comp, void *object,
  203. const struct avs_tplg_token_parser *parsers, int count,
  204. struct snd_soc_tplg_vendor_array *tuples)
  205. {
  206. struct snd_soc_tplg_vendor_value_elem *tuple;
  207. int ret, i, j;
  208. /* Parse element by element. */
  209. for (i = 0; i < le32_to_cpu(tuples->num_elems); i++) {
  210. tuple = &tuples->value[i];
  211. for (j = 0; j < count; j++) {
  212. /* Ignore non-integer tokens. */
  213. if (!(parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
  214. parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
  215. parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
  216. parsers[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
  217. continue;
  218. if (parsers[j].token != le32_to_cpu(tuple->token))
  219. continue;
  220. ret = parsers[j].parse(comp, tuple, object, parsers[j].offset);
  221. if (ret)
  222. return ret;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int avs_parse_tokens(struct snd_soc_component *comp, void *object,
  228. const struct avs_tplg_token_parser *parsers, size_t count,
  229. struct snd_soc_tplg_vendor_array *tuples, int priv_size)
  230. {
  231. int array_size, ret;
  232. while (priv_size > 0) {
  233. array_size = le32_to_cpu(tuples->size);
  234. if (array_size <= 0) {
  235. dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
  236. return -EINVAL;
  237. }
  238. /* Make sure there is enough data before parsing. */
  239. priv_size -= array_size;
  240. if (priv_size < 0) {
  241. dev_err(comp->dev, "invalid array size 0x%x\n", array_size);
  242. return -EINVAL;
  243. }
  244. switch (le32_to_cpu(tuples->type)) {
  245. case SND_SOC_TPLG_TUPLE_TYPE_UUID:
  246. ret = avs_parse_uuid_tokens(comp, object, parsers, count, tuples);
  247. break;
  248. case SND_SOC_TPLG_TUPLE_TYPE_STRING:
  249. ret = avs_parse_string_tokens(comp, object, parsers, count, tuples);
  250. break;
  251. case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
  252. case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
  253. case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
  254. case SND_SOC_TPLG_TUPLE_TYPE_WORD:
  255. ret = avs_parse_word_tokens(comp, object, parsers, count, tuples);
  256. break;
  257. default:
  258. dev_err(comp->dev, "unknown token type %d\n", tuples->type);
  259. ret = -EINVAL;
  260. }
  261. if (ret) {
  262. dev_err(comp->dev, "parsing %zu tokens of %d type failed: %d\n",
  263. count, tuples->type, ret);
  264. return ret;
  265. }
  266. tuples = avs_tplg_vendor_array_next(tuples);
  267. }
  268. return 0;
  269. }
  270. #define AVS_DEFINE_PTR_PARSER(name, type, member) \
  271. static int \
  272. avs_parse_##name##_ptr(struct snd_soc_component *comp, void *elem, void *object, u32 offset) \
  273. { \
  274. struct snd_soc_tplg_vendor_value_elem *tuple = elem; \
  275. struct avs_soc_component *acomp = to_avs_soc_component(comp); \
  276. type **val = (type **)(object + offset); \
  277. u32 idx; \
  278. \
  279. idx = le32_to_cpu(tuple->value); \
  280. if (idx >= acomp->tplg->num_##member) \
  281. return -EINVAL; \
  282. \
  283. *val = &acomp->tplg->member[idx]; \
  284. \
  285. return 0; \
  286. }
  287. AVS_DEFINE_PTR_PARSER(audio_format, struct avs_audio_format, fmts);
  288. AVS_DEFINE_PTR_PARSER(modcfg_base, struct avs_tplg_modcfg_base, modcfgs_base);
  289. AVS_DEFINE_PTR_PARSER(modcfg_ext, struct avs_tplg_modcfg_ext, modcfgs_ext);
  290. AVS_DEFINE_PTR_PARSER(pplcfg, struct avs_tplg_pplcfg, pplcfgs);
  291. AVS_DEFINE_PTR_PARSER(binding, struct avs_tplg_binding, bindings);
  292. static int
  293. parse_audio_format_bitfield(struct snd_soc_component *comp, void *elem, void *object, u32 offset)
  294. {
  295. struct snd_soc_tplg_vendor_value_elem *velem = elem;
  296. struct avs_audio_format *audio_format = object;
  297. switch (offset) {
  298. case AVS_TKN_AFMT_NUM_CHANNELS_U32:
  299. audio_format->num_channels = le32_to_cpu(velem->value);
  300. break;
  301. case AVS_TKN_AFMT_VALID_BIT_DEPTH_U32:
  302. audio_format->valid_bit_depth = le32_to_cpu(velem->value);
  303. break;
  304. case AVS_TKN_AFMT_SAMPLE_TYPE_U32:
  305. audio_format->sample_type = le32_to_cpu(velem->value);
  306. break;
  307. }
  308. return 0;
  309. }
  310. static int avs_ssp_sprint(char *buf, size_t size, const char *fmt, int port, int tdm)
  311. {
  312. char *needle = strstr(fmt, "%d");
  313. int retsize;
  314. /*
  315. * If there is %d present in fmt string it should be replaced by either
  316. * SSP or SSP:TDM, where SSP and TDM are numbers, all other formatting
  317. * will be ignored.
  318. */
  319. if (needle) {
  320. retsize = scnprintf(buf, min_t(size_t, size, needle - fmt + 1), "%s", fmt);
  321. retsize += scnprintf(buf + retsize, size - retsize, "%d", port);
  322. if (tdm)
  323. retsize += scnprintf(buf + retsize, size - retsize, ":%d", tdm);
  324. retsize += scnprintf(buf + retsize, size - retsize, "%s", needle + 2);
  325. return retsize;
  326. }
  327. return snprintf(buf, size, "%s", fmt);
  328. }
  329. static int parse_link_formatted_string(struct snd_soc_component *comp, void *elem,
  330. void *object, u32 offset)
  331. {
  332. struct snd_soc_tplg_vendor_string_elem *tuple = elem;
  333. struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
  334. char *val = (char *)((u8 *)object + offset);
  335. int ssp_port, tdm_slot;
  336. /*
  337. * Dynamic naming - string formats, e.g.: ssp%d - supported only for
  338. * topologies describing single device e.g.: an I2S codec on SSP0.
  339. */
  340. if (!avs_mach_singular_ssp(mach))
  341. return avs_parse_string_token(comp, elem, object, offset);
  342. ssp_port = avs_mach_ssp_port(mach);
  343. if (!avs_mach_singular_tdm(mach, ssp_port))
  344. return avs_parse_string_token(comp, elem, object, offset);
  345. tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
  346. avs_ssp_sprint(val, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, tuple->string, ssp_port, tdm_slot);
  347. return 0;
  348. }
  349. static int
  350. parse_dictionary_header(struct snd_soc_component *comp,
  351. struct snd_soc_tplg_vendor_array *tuples,
  352. void **dict, u32 *num_entries, size_t entry_size,
  353. u32 num_entries_token)
  354. {
  355. struct snd_soc_tplg_vendor_value_elem *tuple;
  356. /* Dictionary header consists of single tuple - entry count. */
  357. tuple = tuples->value;
  358. if (le32_to_cpu(tuple->token) != num_entries_token) {
  359. dev_err(comp->dev, "invalid dictionary header, expected: %d\n",
  360. num_entries_token);
  361. return -EINVAL;
  362. }
  363. *num_entries = le32_to_cpu(tuple->value);
  364. *dict = devm_kcalloc(comp->card->dev, *num_entries, entry_size, GFP_KERNEL);
  365. if (!*dict)
  366. return -ENOMEM;
  367. return 0;
  368. }
  369. static int
  370. parse_dictionary_entries(struct snd_soc_component *comp,
  371. struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
  372. void *dict, u32 num_entries, size_t entry_size,
  373. u32 entry_id_token,
  374. const struct avs_tplg_token_parser *parsers, size_t num_parsers)
  375. {
  376. void *pos = dict;
  377. int i;
  378. for (i = 0; i < num_entries; i++) {
  379. u32 esize;
  380. int ret;
  381. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  382. entry_id_token, &esize);
  383. if (ret)
  384. return ret;
  385. ret = avs_parse_tokens(comp, pos, parsers, num_parsers, tuples, esize);
  386. if (ret < 0) {
  387. dev_err(comp->dev, "parse entry: %d of type: %d failed: %d\n",
  388. i, entry_id_token, ret);
  389. return ret;
  390. }
  391. pos += entry_size;
  392. block_size -= esize;
  393. tuples = avs_tplg_vendor_array_at(tuples, esize);
  394. }
  395. return 0;
  396. }
  397. static int parse_dictionary(struct snd_soc_component *comp,
  398. struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
  399. void **dict, u32 *num_entries, size_t entry_size,
  400. u32 num_entries_token, u32 entry_id_token,
  401. const struct avs_tplg_token_parser *parsers, size_t num_parsers)
  402. {
  403. int ret;
  404. ret = parse_dictionary_header(comp, tuples, dict, num_entries,
  405. entry_size, num_entries_token);
  406. if (ret)
  407. return ret;
  408. block_size -= le32_to_cpu(tuples->size);
  409. /* With header parsed, move on to parsing entries. */
  410. tuples = avs_tplg_vendor_array_next(tuples);
  411. return parse_dictionary_entries(comp, tuples, block_size, *dict,
  412. *num_entries, entry_size,
  413. entry_id_token, parsers, num_parsers);
  414. }
  415. static const struct avs_tplg_token_parser library_parsers[] = {
  416. {
  417. .token = AVS_TKN_LIBRARY_NAME_STRING,
  418. .type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
  419. .offset = offsetof(struct avs_tplg_library, name),
  420. .parse = avs_parse_string_token,
  421. },
  422. };
  423. static int avs_tplg_parse_libraries(struct snd_soc_component *comp,
  424. struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
  425. {
  426. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  427. struct avs_tplg *tplg = acomp->tplg;
  428. return parse_dictionary(comp, tuples, block_size, (void **)&tplg->libs,
  429. &tplg->num_libs, sizeof(*tplg->libs),
  430. AVS_TKN_MANIFEST_NUM_LIBRARIES_U32,
  431. AVS_TKN_LIBRARY_ID_U32,
  432. library_parsers, ARRAY_SIZE(library_parsers));
  433. }
  434. static const struct avs_tplg_token_parser audio_format_parsers[] = {
  435. {
  436. .token = AVS_TKN_AFMT_SAMPLE_RATE_U32,
  437. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  438. .offset = offsetof(struct avs_audio_format, sampling_freq),
  439. .parse = avs_parse_word_token,
  440. },
  441. {
  442. .token = AVS_TKN_AFMT_BIT_DEPTH_U32,
  443. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  444. .offset = offsetof(struct avs_audio_format, bit_depth),
  445. .parse = avs_parse_word_token,
  446. },
  447. {
  448. .token = AVS_TKN_AFMT_CHANNEL_MAP_U32,
  449. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  450. .offset = offsetof(struct avs_audio_format, channel_map),
  451. .parse = avs_parse_word_token,
  452. },
  453. {
  454. .token = AVS_TKN_AFMT_CHANNEL_CFG_U32,
  455. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  456. .offset = offsetof(struct avs_audio_format, channel_config),
  457. .parse = avs_parse_word_token,
  458. },
  459. {
  460. .token = AVS_TKN_AFMT_INTERLEAVING_U32,
  461. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  462. .offset = offsetof(struct avs_audio_format, interleaving),
  463. .parse = avs_parse_word_token,
  464. },
  465. {
  466. .token = AVS_TKN_AFMT_NUM_CHANNELS_U32,
  467. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  468. .offset = AVS_TKN_AFMT_NUM_CHANNELS_U32,
  469. .parse = parse_audio_format_bitfield,
  470. },
  471. {
  472. .token = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
  473. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  474. .offset = AVS_TKN_AFMT_VALID_BIT_DEPTH_U32,
  475. .parse = parse_audio_format_bitfield,
  476. },
  477. {
  478. .token = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
  479. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  480. .offset = AVS_TKN_AFMT_SAMPLE_TYPE_U32,
  481. .parse = parse_audio_format_bitfield,
  482. },
  483. };
  484. static int avs_tplg_parse_audio_formats(struct snd_soc_component *comp,
  485. struct snd_soc_tplg_vendor_array *tuples,
  486. u32 block_size)
  487. {
  488. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  489. struct avs_tplg *tplg = acomp->tplg;
  490. return parse_dictionary(comp, tuples, block_size, (void **)&tplg->fmts,
  491. &tplg->num_fmts, sizeof(*tplg->fmts),
  492. AVS_TKN_MANIFEST_NUM_AFMTS_U32,
  493. AVS_TKN_AFMT_ID_U32,
  494. audio_format_parsers, ARRAY_SIZE(audio_format_parsers));
  495. }
  496. static const struct avs_tplg_token_parser modcfg_base_parsers[] = {
  497. {
  498. .token = AVS_TKN_MODCFG_BASE_CPC_U32,
  499. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  500. .offset = offsetof(struct avs_tplg_modcfg_base, cpc),
  501. .parse = avs_parse_word_token,
  502. },
  503. {
  504. .token = AVS_TKN_MODCFG_BASE_IBS_U32,
  505. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  506. .offset = offsetof(struct avs_tplg_modcfg_base, ibs),
  507. .parse = avs_parse_word_token,
  508. },
  509. {
  510. .token = AVS_TKN_MODCFG_BASE_OBS_U32,
  511. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  512. .offset = offsetof(struct avs_tplg_modcfg_base, obs),
  513. .parse = avs_parse_word_token,
  514. },
  515. {
  516. .token = AVS_TKN_MODCFG_BASE_PAGES_U32,
  517. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  518. .offset = offsetof(struct avs_tplg_modcfg_base, is_pages),
  519. .parse = avs_parse_word_token,
  520. },
  521. };
  522. static int avs_tplg_parse_modcfgs_base(struct snd_soc_component *comp,
  523. struct snd_soc_tplg_vendor_array *tuples,
  524. u32 block_size)
  525. {
  526. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  527. struct avs_tplg *tplg = acomp->tplg;
  528. return parse_dictionary(comp, tuples, block_size, (void **)&tplg->modcfgs_base,
  529. &tplg->num_modcfgs_base, sizeof(*tplg->modcfgs_base),
  530. AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32,
  531. AVS_TKN_MODCFG_BASE_ID_U32,
  532. modcfg_base_parsers, ARRAY_SIZE(modcfg_base_parsers));
  533. }
  534. static const struct avs_tplg_token_parser modcfg_ext_parsers[] = {
  535. {
  536. .token = AVS_TKN_MODCFG_EXT_TYPE_UUID,
  537. .type = SND_SOC_TPLG_TUPLE_TYPE_UUID,
  538. .offset = offsetof(struct avs_tplg_modcfg_ext, type),
  539. .parse = avs_parse_uuid_token,
  540. },
  541. {
  542. .token = AVS_TKN_MODCFG_CPR_OUT_AFMT_ID_U32,
  543. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  544. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.out_fmt),
  545. .parse = avs_parse_audio_format_ptr,
  546. },
  547. {
  548. .token = AVS_TKN_MODCFG_CPR_FEATURE_MASK_U32,
  549. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  550. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.feature_mask),
  551. .parse = avs_parse_word_token,
  552. },
  553. {
  554. .token = AVS_TKN_MODCFG_CPR_VINDEX_U8,
  555. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  556. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.vindex),
  557. .parse = avs_parse_byte_token,
  558. },
  559. {
  560. .token = AVS_TKN_MODCFG_CPR_DMA_TYPE_U32,
  561. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  562. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_type),
  563. .parse = avs_parse_word_token,
  564. },
  565. {
  566. .token = AVS_TKN_MODCFG_CPR_DMABUFF_SIZE_U32,
  567. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  568. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.dma_buffer_size),
  569. .parse = avs_parse_word_token,
  570. },
  571. {
  572. .token = AVS_TKN_MODCFG_CPR_BLOB_FMT_ID_U32,
  573. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  574. .offset = offsetof(struct avs_tplg_modcfg_ext, copier.blob_fmt),
  575. .parse = avs_parse_audio_format_ptr,
  576. },
  577. {
  578. .token = AVS_TKN_MODCFG_MICSEL_OUT_AFMT_ID_U32,
  579. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  580. .offset = offsetof(struct avs_tplg_modcfg_ext, micsel.out_fmt),
  581. .parse = avs_parse_audio_format_ptr,
  582. },
  583. {
  584. .token = AVS_TKN_MODCFG_INTELWOV_CPC_LP_MODE_U32,
  585. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  586. .offset = offsetof(struct avs_tplg_modcfg_ext, wov.cpc_lp_mode),
  587. .parse = avs_parse_word_token,
  588. },
  589. {
  590. .token = AVS_TKN_MODCFG_SRC_OUT_FREQ_U32,
  591. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  592. .offset = offsetof(struct avs_tplg_modcfg_ext, src.out_freq),
  593. .parse = avs_parse_word_token,
  594. },
  595. {
  596. .token = AVS_TKN_MODCFG_MUX_REF_AFMT_ID_U32,
  597. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  598. .offset = offsetof(struct avs_tplg_modcfg_ext, mux.ref_fmt),
  599. .parse = avs_parse_audio_format_ptr,
  600. },
  601. {
  602. .token = AVS_TKN_MODCFG_MUX_OUT_AFMT_ID_U32,
  603. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  604. .offset = offsetof(struct avs_tplg_modcfg_ext, mux.out_fmt),
  605. .parse = avs_parse_audio_format_ptr,
  606. },
  607. {
  608. .token = AVS_TKN_MODCFG_AEC_REF_AFMT_ID_U32,
  609. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  610. .offset = offsetof(struct avs_tplg_modcfg_ext, aec.ref_fmt),
  611. .parse = avs_parse_audio_format_ptr,
  612. },
  613. {
  614. .token = AVS_TKN_MODCFG_AEC_OUT_AFMT_ID_U32,
  615. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  616. .offset = offsetof(struct avs_tplg_modcfg_ext, aec.out_fmt),
  617. .parse = avs_parse_audio_format_ptr,
  618. },
  619. {
  620. .token = AVS_TKN_MODCFG_AEC_CPC_LP_MODE_U32,
  621. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  622. .offset = offsetof(struct avs_tplg_modcfg_ext, aec.cpc_lp_mode),
  623. .parse = avs_parse_word_token,
  624. },
  625. {
  626. .token = AVS_TKN_MODCFG_ASRC_OUT_FREQ_U32,
  627. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  628. .offset = offsetof(struct avs_tplg_modcfg_ext, asrc.out_freq),
  629. .parse = avs_parse_word_token,
  630. },
  631. {
  632. .token = AVS_TKN_MODCFG_ASRC_MODE_U8,
  633. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  634. .offset = offsetof(struct avs_tplg_modcfg_ext, asrc.mode),
  635. .parse = avs_parse_byte_token,
  636. },
  637. {
  638. .token = AVS_TKN_MODCFG_ASRC_DISABLE_JITTER_U8,
  639. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  640. .offset = offsetof(struct avs_tplg_modcfg_ext, asrc.disable_jitter_buffer),
  641. .parse = avs_parse_byte_token,
  642. },
  643. {
  644. .token = AVS_TKN_MODCFG_UPDOWN_MIX_OUT_CHAN_CFG_U32,
  645. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  646. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.out_channel_config),
  647. .parse = avs_parse_word_token,
  648. },
  649. {
  650. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_SELECT_U32,
  651. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  652. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients_select),
  653. .parse = avs_parse_word_token,
  654. },
  655. {
  656. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_0_S32,
  657. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  658. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[0]),
  659. .parse = avs_parse_word_token,
  660. },
  661. {
  662. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_1_S32,
  663. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  664. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[1]),
  665. .parse = avs_parse_word_token,
  666. },
  667. {
  668. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_2_S32,
  669. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  670. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[2]),
  671. .parse = avs_parse_word_token,
  672. },
  673. {
  674. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_3_S32,
  675. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  676. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[3]),
  677. .parse = avs_parse_word_token,
  678. },
  679. {
  680. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_4_S32,
  681. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  682. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[4]),
  683. .parse = avs_parse_word_token,
  684. },
  685. {
  686. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_5_S32,
  687. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  688. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[5]),
  689. .parse = avs_parse_word_token,
  690. },
  691. {
  692. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_6_S32,
  693. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  694. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[6]),
  695. .parse = avs_parse_word_token,
  696. },
  697. {
  698. .token = AVS_TKN_MODCFG_UPDOWN_MIX_COEFF_7_S32,
  699. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  700. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.coefficients[7]),
  701. .parse = avs_parse_word_token,
  702. },
  703. {
  704. .token = AVS_TKN_MODCFG_UPDOWN_MIX_CHAN_MAP_U32,
  705. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  706. .offset = offsetof(struct avs_tplg_modcfg_ext, updown_mix.channel_map),
  707. .parse = avs_parse_word_token,
  708. },
  709. {
  710. .token = AVS_TKN_MODCFG_EXT_NUM_INPUT_PINS_U16,
  711. .type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
  712. .offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_input_pins),
  713. .parse = avs_parse_short_token,
  714. },
  715. {
  716. .token = AVS_TKN_MODCFG_EXT_NUM_OUTPUT_PINS_U16,
  717. .type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
  718. .offset = offsetof(struct avs_tplg_modcfg_ext, generic.num_output_pins),
  719. .parse = avs_parse_short_token,
  720. },
  721. };
  722. static const struct avs_tplg_token_parser pin_format_parsers[] = {
  723. {
  724. .token = AVS_TKN_PIN_FMT_INDEX_U32,
  725. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  726. .offset = offsetof(struct avs_tplg_pin_format, pin_index),
  727. .parse = avs_parse_word_token,
  728. },
  729. {
  730. .token = AVS_TKN_PIN_FMT_IOBS_U32,
  731. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  732. .offset = offsetof(struct avs_tplg_pin_format, iobs),
  733. .parse = avs_parse_word_token,
  734. },
  735. {
  736. .token = AVS_TKN_PIN_FMT_AFMT_ID_U32,
  737. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  738. .offset = offsetof(struct avs_tplg_pin_format, fmt),
  739. .parse = avs_parse_audio_format_ptr,
  740. },
  741. };
  742. static void
  743. assign_copier_gtw_instance(struct snd_soc_component *comp, struct avs_tplg_modcfg_ext *cfg)
  744. {
  745. struct snd_soc_acpi_mach *mach;
  746. int ssp_port, tdm_slot;
  747. if (!guid_equal(&cfg->type, &AVS_COPIER_MOD_UUID))
  748. return;
  749. /* Only I2S boards assign port instance in ->i2s_link_mask. */
  750. switch (cfg->copier.dma_type) {
  751. case AVS_DMA_I2S_LINK_OUTPUT:
  752. case AVS_DMA_I2S_LINK_INPUT:
  753. break;
  754. default:
  755. return;
  756. }
  757. /* If topology sets value don't overwrite it */
  758. if (cfg->copier.vindex.val)
  759. return;
  760. mach = dev_get_platdata(comp->card->dev);
  761. if (!avs_mach_singular_ssp(mach))
  762. return;
  763. ssp_port = avs_mach_ssp_port(mach);
  764. if (!avs_mach_singular_tdm(mach, ssp_port))
  765. return;
  766. tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
  767. cfg->copier.vindex.i2s.instance = ssp_port;
  768. cfg->copier.vindex.i2s.time_slot = tdm_slot;
  769. }
  770. static int avs_tplg_parse_modcfg_ext(struct snd_soc_component *comp,
  771. struct avs_tplg_modcfg_ext *cfg,
  772. struct snd_soc_tplg_vendor_array *tuples,
  773. u32 block_size)
  774. {
  775. u32 esize;
  776. int ret;
  777. /* See where pin block starts. */
  778. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  779. AVS_TKN_PIN_FMT_INDEX_U32, &esize);
  780. if (ret)
  781. return ret;
  782. ret = avs_parse_tokens(comp, cfg, modcfg_ext_parsers,
  783. ARRAY_SIZE(modcfg_ext_parsers), tuples, esize);
  784. if (ret)
  785. return ret;
  786. /* Update copier gateway based on board's i2s_link_mask. */
  787. assign_copier_gtw_instance(comp, cfg);
  788. block_size -= esize;
  789. /* Parse trailing in/out pin formats if any. */
  790. if (block_size) {
  791. struct avs_tplg_pin_format *pins;
  792. u32 num_pins;
  793. num_pins = cfg->generic.num_input_pins + cfg->generic.num_output_pins;
  794. if (!num_pins)
  795. return -EINVAL;
  796. pins = devm_kcalloc(comp->card->dev, num_pins, sizeof(*pins), GFP_KERNEL);
  797. if (!pins)
  798. return -ENOMEM;
  799. tuples = avs_tplg_vendor_array_at(tuples, esize);
  800. ret = parse_dictionary_entries(comp, tuples, block_size,
  801. pins, num_pins, sizeof(*pins),
  802. AVS_TKN_PIN_FMT_INDEX_U32,
  803. pin_format_parsers,
  804. ARRAY_SIZE(pin_format_parsers));
  805. if (ret)
  806. return ret;
  807. cfg->generic.pin_fmts = pins;
  808. }
  809. return 0;
  810. }
  811. static int avs_tplg_parse_modcfgs_ext(struct snd_soc_component *comp,
  812. struct snd_soc_tplg_vendor_array *tuples,
  813. u32 block_size)
  814. {
  815. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  816. struct avs_tplg *tplg = acomp->tplg;
  817. int ret, i;
  818. ret = parse_dictionary_header(comp, tuples, (void **)&tplg->modcfgs_ext,
  819. &tplg->num_modcfgs_ext,
  820. sizeof(*tplg->modcfgs_ext),
  821. AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32);
  822. if (ret)
  823. return ret;
  824. block_size -= le32_to_cpu(tuples->size);
  825. /* With header parsed, move on to parsing entries. */
  826. tuples = avs_tplg_vendor_array_next(tuples);
  827. for (i = 0; i < tplg->num_modcfgs_ext; i++) {
  828. struct avs_tplg_modcfg_ext *cfg = &tplg->modcfgs_ext[i];
  829. u32 esize;
  830. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  831. AVS_TKN_MODCFG_EXT_ID_U32, &esize);
  832. if (ret)
  833. return ret;
  834. ret = avs_tplg_parse_modcfg_ext(comp, cfg, tuples, esize);
  835. if (ret)
  836. return ret;
  837. block_size -= esize;
  838. tuples = avs_tplg_vendor_array_at(tuples, esize);
  839. }
  840. return 0;
  841. }
  842. static const struct avs_tplg_token_parser pplcfg_parsers[] = {
  843. {
  844. .token = AVS_TKN_PPLCFG_REQ_SIZE_U16,
  845. .type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
  846. .offset = offsetof(struct avs_tplg_pplcfg, req_size),
  847. .parse = avs_parse_short_token,
  848. },
  849. {
  850. .token = AVS_TKN_PPLCFG_PRIORITY_U8,
  851. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  852. .offset = offsetof(struct avs_tplg_pplcfg, priority),
  853. .parse = avs_parse_byte_token,
  854. },
  855. {
  856. .token = AVS_TKN_PPLCFG_LOW_POWER_BOOL,
  857. .type = SND_SOC_TPLG_TUPLE_TYPE_BOOL,
  858. .offset = offsetof(struct avs_tplg_pplcfg, lp),
  859. .parse = avs_parse_bool_token,
  860. },
  861. {
  862. .token = AVS_TKN_PPLCFG_ATTRIBUTES_U16,
  863. .type = SND_SOC_TPLG_TUPLE_TYPE_SHORT,
  864. .offset = offsetof(struct avs_tplg_pplcfg, attributes),
  865. .parse = avs_parse_short_token,
  866. },
  867. {
  868. .token = AVS_TKN_PPLCFG_TRIGGER_U32,
  869. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  870. .offset = offsetof(struct avs_tplg_pplcfg, trigger),
  871. .parse = avs_parse_word_token,
  872. },
  873. };
  874. static int avs_tplg_parse_pplcfgs(struct snd_soc_component *comp,
  875. struct snd_soc_tplg_vendor_array *tuples,
  876. u32 block_size)
  877. {
  878. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  879. struct avs_tplg *tplg = acomp->tplg;
  880. return parse_dictionary(comp, tuples, block_size, (void **)&tplg->pplcfgs,
  881. &tplg->num_pplcfgs, sizeof(*tplg->pplcfgs),
  882. AVS_TKN_MANIFEST_NUM_PPLCFGS_U32,
  883. AVS_TKN_PPLCFG_ID_U32,
  884. pplcfg_parsers, ARRAY_SIZE(pplcfg_parsers));
  885. }
  886. static const struct avs_tplg_token_parser binding_parsers[] = {
  887. {
  888. .token = AVS_TKN_BINDING_TARGET_TPLG_NAME_STRING,
  889. .type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
  890. .offset = offsetof(struct avs_tplg_binding, target_tplg_name),
  891. .parse = parse_link_formatted_string,
  892. },
  893. {
  894. .token = AVS_TKN_BINDING_TARGET_PATH_TMPL_ID_U32,
  895. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  896. .offset = offsetof(struct avs_tplg_binding, target_path_tmpl_id),
  897. .parse = avs_parse_word_token,
  898. },
  899. {
  900. .token = AVS_TKN_BINDING_TARGET_PPL_ID_U32,
  901. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  902. .offset = offsetof(struct avs_tplg_binding, target_ppl_id),
  903. .parse = avs_parse_word_token,
  904. },
  905. {
  906. .token = AVS_TKN_BINDING_TARGET_MOD_ID_U32,
  907. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  908. .offset = offsetof(struct avs_tplg_binding, target_mod_id),
  909. .parse = avs_parse_word_token,
  910. },
  911. {
  912. .token = AVS_TKN_BINDING_TARGET_MOD_PIN_U8,
  913. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  914. .offset = offsetof(struct avs_tplg_binding, target_mod_pin),
  915. .parse = avs_parse_byte_token,
  916. },
  917. {
  918. .token = AVS_TKN_BINDING_MOD_ID_U32,
  919. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  920. .offset = offsetof(struct avs_tplg_binding, mod_id),
  921. .parse = avs_parse_word_token,
  922. },
  923. {
  924. .token = AVS_TKN_BINDING_MOD_PIN_U8,
  925. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  926. .offset = offsetof(struct avs_tplg_binding, mod_pin),
  927. .parse = avs_parse_byte_token,
  928. },
  929. {
  930. .token = AVS_TKN_BINDING_IS_SINK_U8,
  931. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  932. .offset = offsetof(struct avs_tplg_binding, is_sink),
  933. .parse = avs_parse_byte_token,
  934. },
  935. };
  936. static int avs_tplg_parse_bindings(struct snd_soc_component *comp,
  937. struct snd_soc_tplg_vendor_array *tuples,
  938. u32 block_size)
  939. {
  940. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  941. struct avs_tplg *tplg = acomp->tplg;
  942. return parse_dictionary(comp, tuples, block_size, (void **)&tplg->bindings,
  943. &tplg->num_bindings, sizeof(*tplg->bindings),
  944. AVS_TKN_MANIFEST_NUM_BINDINGS_U32,
  945. AVS_TKN_BINDING_ID_U32,
  946. binding_parsers, ARRAY_SIZE(binding_parsers));
  947. }
  948. static const struct avs_tplg_token_parser module_parsers[] = {
  949. {
  950. .token = AVS_TKN_MOD_ID_U32,
  951. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  952. .offset = offsetof(struct avs_tplg_module, id),
  953. .parse = avs_parse_word_token,
  954. },
  955. {
  956. .token = AVS_TKN_MOD_MODCFG_BASE_ID_U32,
  957. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  958. .offset = offsetof(struct avs_tplg_module, cfg_base),
  959. .parse = avs_parse_modcfg_base_ptr,
  960. },
  961. {
  962. .token = AVS_TKN_MOD_IN_AFMT_ID_U32,
  963. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  964. .offset = offsetof(struct avs_tplg_module, in_fmt),
  965. .parse = avs_parse_audio_format_ptr,
  966. },
  967. {
  968. .token = AVS_TKN_MOD_CORE_ID_U8,
  969. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  970. .offset = offsetof(struct avs_tplg_module, core_id),
  971. .parse = avs_parse_byte_token,
  972. },
  973. {
  974. .token = AVS_TKN_MOD_PROC_DOMAIN_U8,
  975. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  976. .offset = offsetof(struct avs_tplg_module, domain),
  977. .parse = avs_parse_byte_token,
  978. },
  979. {
  980. .token = AVS_TKN_MOD_MODCFG_EXT_ID_U32,
  981. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  982. .offset = offsetof(struct avs_tplg_module, cfg_ext),
  983. .parse = avs_parse_modcfg_ext_ptr,
  984. },
  985. {
  986. .token = AVS_TKN_MOD_KCONTROL_ID_U32,
  987. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  988. .offset = offsetof(struct avs_tplg_module, ctl_id),
  989. .parse = avs_parse_byte_token,
  990. },
  991. {
  992. .token = AVS_TKN_MOD_INIT_CONFIG_NUM_IDS_U32,
  993. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  994. .offset = offsetof(struct avs_tplg_module, num_config_ids),
  995. .parse = avs_parse_byte_token,
  996. },
  997. };
  998. static const struct avs_tplg_token_parser init_config_parsers[] = {
  999. {
  1000. .token = AVS_TKN_MOD_INIT_CONFIG_ID_U32,
  1001. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1002. .offset = 0,
  1003. .parse = avs_parse_word_token,
  1004. },
  1005. };
  1006. static struct avs_tplg_module *
  1007. avs_tplg_module_create(struct snd_soc_component *comp, struct avs_tplg_pipeline *owner,
  1008. struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
  1009. {
  1010. struct avs_tplg_module *module;
  1011. u32 esize;
  1012. int ret;
  1013. /* See where config id block starts. */
  1014. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  1015. AVS_TKN_MOD_INIT_CONFIG_ID_U32, &esize);
  1016. if (ret)
  1017. return ERR_PTR(ret);
  1018. module = devm_kzalloc(comp->card->dev, sizeof(*module), GFP_KERNEL);
  1019. if (!module)
  1020. return ERR_PTR(-ENOMEM);
  1021. ret = avs_parse_tokens(comp, module, module_parsers,
  1022. ARRAY_SIZE(module_parsers), tuples, esize);
  1023. if (ret < 0)
  1024. return ERR_PTR(ret);
  1025. block_size -= esize;
  1026. /* Parse trailing config ids if any. */
  1027. if (block_size) {
  1028. u32 num_config_ids = module->num_config_ids;
  1029. u32 *config_ids;
  1030. if (!num_config_ids)
  1031. return ERR_PTR(-EINVAL);
  1032. config_ids = devm_kcalloc(comp->card->dev, num_config_ids, sizeof(*config_ids),
  1033. GFP_KERNEL);
  1034. if (!config_ids)
  1035. return ERR_PTR(-ENOMEM);
  1036. tuples = avs_tplg_vendor_array_at(tuples, esize);
  1037. ret = parse_dictionary_entries(comp, tuples, block_size,
  1038. config_ids, num_config_ids, sizeof(*config_ids),
  1039. AVS_TKN_MOD_INIT_CONFIG_ID_U32,
  1040. init_config_parsers,
  1041. ARRAY_SIZE(init_config_parsers));
  1042. if (ret)
  1043. return ERR_PTR(ret);
  1044. module->config_ids = config_ids;
  1045. }
  1046. module->owner = owner;
  1047. INIT_LIST_HEAD(&module->node);
  1048. return module;
  1049. }
  1050. static const struct avs_tplg_token_parser pipeline_parsers[] = {
  1051. {
  1052. .token = AVS_TKN_PPL_ID_U32,
  1053. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1054. .offset = offsetof(struct avs_tplg_pipeline, id),
  1055. .parse = avs_parse_word_token,
  1056. },
  1057. {
  1058. .token = AVS_TKN_PPL_PPLCFG_ID_U32,
  1059. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1060. .offset = offsetof(struct avs_tplg_pipeline, cfg),
  1061. .parse = avs_parse_pplcfg_ptr,
  1062. },
  1063. {
  1064. .token = AVS_TKN_PPL_NUM_BINDING_IDS_U32,
  1065. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1066. .offset = offsetof(struct avs_tplg_pipeline, num_bindings),
  1067. .parse = avs_parse_word_token,
  1068. },
  1069. };
  1070. static const struct avs_tplg_token_parser bindings_parsers[] = {
  1071. {
  1072. .token = AVS_TKN_PPL_BINDING_ID_U32,
  1073. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1074. .offset = 0, /* to treat pipeline->bindings as dictionary */
  1075. .parse = avs_parse_binding_ptr,
  1076. },
  1077. };
  1078. static struct avs_tplg_pipeline *
  1079. avs_tplg_pipeline_create(struct snd_soc_component *comp, struct avs_tplg_path *owner,
  1080. struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
  1081. {
  1082. struct avs_tplg_pipeline *pipeline;
  1083. u32 modblk_size, offset;
  1084. int ret;
  1085. pipeline = devm_kzalloc(comp->card->dev, sizeof(*pipeline), GFP_KERNEL);
  1086. if (!pipeline)
  1087. return ERR_PTR(-ENOMEM);
  1088. pipeline->owner = owner;
  1089. INIT_LIST_HEAD(&pipeline->mod_list);
  1090. /* Pipeline header MUST be followed by at least one module. */
  1091. ret = avs_tplg_vendor_array_lookup(tuples, block_size,
  1092. AVS_TKN_MOD_ID_U32, &offset);
  1093. if (!ret && !offset)
  1094. ret = -EINVAL;
  1095. if (ret)
  1096. return ERR_PTR(ret);
  1097. /* Process header which precedes module sections. */
  1098. ret = avs_parse_tokens(comp, pipeline, pipeline_parsers,
  1099. ARRAY_SIZE(pipeline_parsers), tuples, offset);
  1100. if (ret < 0)
  1101. return ERR_PTR(ret);
  1102. block_size -= offset;
  1103. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1104. /* Optionally, binding sections follow module ones. */
  1105. ret = avs_tplg_vendor_array_lookup_next(tuples, block_size,
  1106. AVS_TKN_PPL_BINDING_ID_U32, &offset);
  1107. if (ret) {
  1108. if (ret != -ENOENT)
  1109. return ERR_PTR(ret);
  1110. /* Does header information match actual block layout? */
  1111. if (pipeline->num_bindings)
  1112. return ERR_PTR(-EINVAL);
  1113. modblk_size = block_size;
  1114. } else {
  1115. pipeline->bindings = devm_kcalloc(comp->card->dev, pipeline->num_bindings,
  1116. sizeof(*pipeline->bindings), GFP_KERNEL);
  1117. if (!pipeline->bindings)
  1118. return ERR_PTR(-ENOMEM);
  1119. modblk_size = offset;
  1120. }
  1121. block_size -= modblk_size;
  1122. do {
  1123. struct avs_tplg_module *module;
  1124. u32 esize;
  1125. ret = avs_tplg_vendor_entry_size(tuples, modblk_size,
  1126. AVS_TKN_MOD_ID_U32, &esize);
  1127. if (ret)
  1128. return ERR_PTR(ret);
  1129. module = avs_tplg_module_create(comp, pipeline, tuples, esize);
  1130. if (IS_ERR(module)) {
  1131. dev_err(comp->dev, "parse module failed: %ld\n",
  1132. PTR_ERR(module));
  1133. return ERR_CAST(module);
  1134. }
  1135. list_add_tail(&module->node, &pipeline->mod_list);
  1136. modblk_size -= esize;
  1137. tuples = avs_tplg_vendor_array_at(tuples, esize);
  1138. } while (modblk_size > 0);
  1139. /* What's left is optional range of bindings. */
  1140. ret = parse_dictionary_entries(comp, tuples, block_size, pipeline->bindings,
  1141. pipeline->num_bindings, sizeof(*pipeline->bindings),
  1142. AVS_TKN_PPL_BINDING_ID_U32,
  1143. bindings_parsers, ARRAY_SIZE(bindings_parsers));
  1144. if (ret)
  1145. return ERR_PTR(ret);
  1146. return pipeline;
  1147. }
  1148. static const struct avs_tplg_token_parser path_parsers[] = {
  1149. {
  1150. .token = AVS_TKN_PATH_ID_U32,
  1151. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1152. .offset = offsetof(struct avs_tplg_path, id),
  1153. .parse = avs_parse_word_token,
  1154. },
  1155. {
  1156. .token = AVS_TKN_PATH_FE_FMT_ID_U32,
  1157. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1158. .offset = offsetof(struct avs_tplg_path, fe_fmt),
  1159. .parse = avs_parse_audio_format_ptr,
  1160. },
  1161. {
  1162. .token = AVS_TKN_PATH_BE_FMT_ID_U32,
  1163. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1164. .offset = offsetof(struct avs_tplg_path, be_fmt),
  1165. .parse = avs_parse_audio_format_ptr,
  1166. },
  1167. };
  1168. static struct avs_tplg_path *
  1169. avs_tplg_path_create(struct snd_soc_component *comp, struct avs_tplg_path_template *owner,
  1170. struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
  1171. const struct avs_tplg_token_parser *parsers, u32 num_parsers)
  1172. {
  1173. struct avs_tplg_pipeline *pipeline;
  1174. struct avs_tplg_path *path;
  1175. u32 offset;
  1176. int ret;
  1177. path = devm_kzalloc(comp->card->dev, sizeof(*path), GFP_KERNEL);
  1178. if (!path)
  1179. return ERR_PTR(-ENOMEM);
  1180. path->owner = owner;
  1181. INIT_LIST_HEAD(&path->ppl_list);
  1182. INIT_LIST_HEAD(&path->node);
  1183. /* Path header MAY be followed by one or more pipelines. */
  1184. ret = avs_tplg_vendor_array_lookup(tuples, block_size,
  1185. AVS_TKN_PPL_ID_U32, &offset);
  1186. if (ret == -ENOENT)
  1187. offset = block_size;
  1188. else if (ret)
  1189. return ERR_PTR(ret);
  1190. else if (!offset)
  1191. return ERR_PTR(-EINVAL);
  1192. /* Process header which precedes pipeline sections. */
  1193. ret = avs_parse_tokens(comp, path, parsers, num_parsers, tuples, offset);
  1194. if (ret < 0)
  1195. return ERR_PTR(ret);
  1196. block_size -= offset;
  1197. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1198. while (block_size > 0) {
  1199. u32 esize;
  1200. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  1201. AVS_TKN_PPL_ID_U32, &esize);
  1202. if (ret)
  1203. return ERR_PTR(ret);
  1204. pipeline = avs_tplg_pipeline_create(comp, path, tuples, esize);
  1205. if (IS_ERR(pipeline)) {
  1206. dev_err(comp->dev, "parse pipeline failed: %ld\n",
  1207. PTR_ERR(pipeline));
  1208. return ERR_CAST(pipeline);
  1209. }
  1210. list_add_tail(&pipeline->node, &path->ppl_list);
  1211. block_size -= esize;
  1212. tuples = avs_tplg_vendor_array_at(tuples, esize);
  1213. }
  1214. return path;
  1215. }
  1216. static const struct avs_tplg_token_parser path_tmpl_parsers[] = {
  1217. {
  1218. .token = AVS_TKN_PATH_TMPL_ID_U32,
  1219. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1220. .offset = offsetof(struct avs_tplg_path_template, id),
  1221. .parse = avs_parse_word_token,
  1222. },
  1223. };
  1224. static int parse_path_template(struct snd_soc_component *comp,
  1225. struct snd_soc_tplg_vendor_array *tuples, u32 block_size,
  1226. struct avs_tplg_path_template *template,
  1227. const struct avs_tplg_token_parser *tmpl_tokens, u32 num_tmpl_tokens,
  1228. const struct avs_tplg_token_parser *path_tokens, u32 num_path_tokens)
  1229. {
  1230. struct avs_tplg_path *path;
  1231. u32 offset;
  1232. int ret;
  1233. /* Path template header MUST be followed by at least one path variant. */
  1234. ret = avs_tplg_vendor_array_lookup(tuples, block_size,
  1235. AVS_TKN_PATH_ID_U32, &offset);
  1236. if (ret)
  1237. return ret;
  1238. /* Process header which precedes path variants sections. */
  1239. ret = avs_parse_tokens(comp, template, tmpl_tokens, num_tmpl_tokens, tuples, offset);
  1240. if (ret < 0)
  1241. return ret;
  1242. block_size -= offset;
  1243. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1244. do {
  1245. u32 esize;
  1246. ret = avs_tplg_vendor_entry_size(tuples, block_size,
  1247. AVS_TKN_PATH_ID_U32, &esize);
  1248. if (ret)
  1249. return ret;
  1250. path = avs_tplg_path_create(comp, template, tuples, esize, path_tokens,
  1251. num_path_tokens);
  1252. if (IS_ERR(path)) {
  1253. dev_err(comp->dev, "parse path failed: %ld\n", PTR_ERR(path));
  1254. return PTR_ERR(path);
  1255. }
  1256. list_add_tail(&path->node, &template->path_list);
  1257. block_size -= esize;
  1258. tuples = avs_tplg_vendor_array_at(tuples, esize);
  1259. } while (block_size > 0);
  1260. return 0;
  1261. }
  1262. static struct avs_tplg_path_template *
  1263. avs_tplg_path_template_create(struct snd_soc_component *comp, struct avs_tplg *owner,
  1264. struct snd_soc_tplg_vendor_array *tuples, u32 block_size)
  1265. {
  1266. struct avs_tplg_path_template *template;
  1267. int ret;
  1268. template = devm_kzalloc(comp->card->dev, sizeof(*template), GFP_KERNEL);
  1269. if (!template)
  1270. return ERR_PTR(-ENOMEM);
  1271. template->owner = owner; /* Used to access component tplg is assigned to. */
  1272. INIT_LIST_HEAD(&template->path_list);
  1273. INIT_LIST_HEAD(&template->node);
  1274. ret = parse_path_template(comp, tuples, block_size, template, path_tmpl_parsers,
  1275. ARRAY_SIZE(path_tmpl_parsers), path_parsers,
  1276. ARRAY_SIZE(path_parsers));
  1277. if (ret)
  1278. return ERR_PTR(ret);
  1279. return template;
  1280. }
  1281. static const struct avs_tplg_token_parser mod_init_config_parsers[] = {
  1282. {
  1283. .token = AVS_TKN_INIT_CONFIG_ID_U32,
  1284. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1285. .offset = offsetof(struct avs_tplg_init_config, id),
  1286. .parse = avs_parse_word_token,
  1287. },
  1288. {
  1289. .token = AVS_TKN_INIT_CONFIG_PARAM_U8,
  1290. .type = SND_SOC_TPLG_TUPLE_TYPE_BYTE,
  1291. .offset = offsetof(struct avs_tplg_init_config, param),
  1292. .parse = avs_parse_byte_token,
  1293. },
  1294. {
  1295. .token = AVS_TKN_INIT_CONFIG_LENGTH_U32,
  1296. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1297. .offset = offsetof(struct avs_tplg_init_config, length),
  1298. .parse = avs_parse_word_token,
  1299. },
  1300. };
  1301. static int avs_tplg_parse_initial_configs(struct snd_soc_component *comp,
  1302. struct snd_soc_tplg_vendor_array *tuples,
  1303. u32 block_size)
  1304. {
  1305. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  1306. struct avs_tplg *tplg = acomp->tplg;
  1307. int ret, i;
  1308. /* Parse tuple section telling how many init configs there are. */
  1309. ret = parse_dictionary_header(comp, tuples, (void **)&tplg->init_configs,
  1310. &tplg->num_init_configs,
  1311. sizeof(*tplg->init_configs),
  1312. AVS_TKN_MANIFEST_NUM_INIT_CONFIGS_U32);
  1313. if (ret)
  1314. return ret;
  1315. block_size -= le32_to_cpu(tuples->size);
  1316. /* With header parsed, move on to parsing entries. */
  1317. tuples = avs_tplg_vendor_array_next(tuples);
  1318. for (i = 0; i < tplg->num_init_configs && block_size > 0; i++) {
  1319. struct avs_tplg_init_config *config = &tplg->init_configs[i];
  1320. struct snd_soc_tplg_vendor_array *tmp;
  1321. void *init_config_data;
  1322. u32 esize;
  1323. /*
  1324. * Usually to get section length we search for first token of next group of data,
  1325. * but in this case we can't as tuples are followed by raw data.
  1326. */
  1327. tmp = avs_tplg_vendor_array_next(tuples);
  1328. esize = le32_to_cpu(tuples->size) + le32_to_cpu(tmp->size);
  1329. ret = parse_dictionary_entries(comp, tuples, esize, config, 1, sizeof(*config),
  1330. AVS_TKN_INIT_CONFIG_ID_U32,
  1331. mod_init_config_parsers,
  1332. ARRAY_SIZE(mod_init_config_parsers));
  1333. block_size -= esize;
  1334. /* handle raw data section */
  1335. init_config_data = (void *)tuples + esize;
  1336. esize = config->length;
  1337. config->data = devm_kmemdup(comp->card->dev, init_config_data, esize, GFP_KERNEL);
  1338. if (!config->data)
  1339. return -ENOMEM;
  1340. tuples = init_config_data + esize;
  1341. block_size -= esize;
  1342. }
  1343. return 0;
  1344. }
  1345. static int avs_route_load(struct snd_soc_component *comp, int index,
  1346. struct snd_soc_dapm_route *route)
  1347. {
  1348. struct snd_soc_acpi_mach *mach = dev_get_platdata(comp->card->dev);
  1349. size_t len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
  1350. int ssp_port, tdm_slot;
  1351. char *buf;
  1352. /* See parse_link_formatted_string() for dynamic naming when(s). */
  1353. if (!avs_mach_singular_ssp(mach))
  1354. return 0;
  1355. ssp_port = avs_mach_ssp_port(mach);
  1356. if (!avs_mach_singular_tdm(mach, ssp_port))
  1357. return 0;
  1358. tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
  1359. buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
  1360. if (!buf)
  1361. return -ENOMEM;
  1362. avs_ssp_sprint(buf, len, route->source, ssp_port, tdm_slot);
  1363. route->source = buf;
  1364. buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
  1365. if (!buf)
  1366. return -ENOMEM;
  1367. avs_ssp_sprint(buf, len, route->sink, ssp_port, tdm_slot);
  1368. route->sink = buf;
  1369. if (route->control) {
  1370. buf = devm_kzalloc(comp->card->dev, len, GFP_KERNEL);
  1371. if (!buf)
  1372. return -ENOMEM;
  1373. avs_ssp_sprint(buf, len, route->control, ssp_port, tdm_slot);
  1374. route->control = buf;
  1375. }
  1376. return 0;
  1377. }
  1378. static int avs_widget_load(struct snd_soc_component *comp, int index,
  1379. struct snd_soc_dapm_widget *w,
  1380. struct snd_soc_tplg_dapm_widget *dw)
  1381. {
  1382. struct snd_soc_acpi_mach *mach;
  1383. struct avs_tplg_path_template *template;
  1384. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  1385. struct avs_tplg *tplg;
  1386. int ssp_port, tdm_slot;
  1387. if (!le32_to_cpu(dw->priv.size))
  1388. return 0;
  1389. w->no_wname_in_kcontrol_name = true;
  1390. if (w->ignore_suspend && !AVS_S0IX_SUPPORTED) {
  1391. dev_info_once(comp->dev, "Device does not support S0IX, check BIOS settings\n");
  1392. w->ignore_suspend = false;
  1393. }
  1394. tplg = acomp->tplg;
  1395. mach = dev_get_platdata(comp->card->dev);
  1396. if (!avs_mach_singular_ssp(mach))
  1397. goto static_name;
  1398. ssp_port = avs_mach_ssp_port(mach);
  1399. /* See parse_link_formatted_string() for dynamic naming when(s). */
  1400. if (avs_mach_singular_tdm(mach, ssp_port)) {
  1401. /* size is based on possible %d -> SSP:TDM, where SSP and TDM < 10 + '\0' */
  1402. size_t size = strlen(dw->name) + 2;
  1403. char *buf;
  1404. tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
  1405. buf = kmalloc(size, GFP_KERNEL);
  1406. if (!buf)
  1407. return -ENOMEM;
  1408. avs_ssp_sprint(buf, size, dw->name, ssp_port, tdm_slot);
  1409. kfree(w->name);
  1410. /* w->name is freed later by soc_tplg_dapm_widget_create() */
  1411. w->name = buf;
  1412. }
  1413. static_name:
  1414. template = avs_tplg_path_template_create(comp, tplg, dw->priv.array,
  1415. le32_to_cpu(dw->priv.size));
  1416. if (IS_ERR(template)) {
  1417. dev_err(comp->dev, "widget %s load failed: %ld\n", dw->name,
  1418. PTR_ERR(template));
  1419. return PTR_ERR(template);
  1420. }
  1421. w->priv = template; /* link path information to widget */
  1422. list_add_tail(&template->node, &tplg->path_tmpl_list);
  1423. return 0;
  1424. }
  1425. static int avs_widget_ready(struct snd_soc_component *comp, int index,
  1426. struct snd_soc_dapm_widget *w,
  1427. struct snd_soc_tplg_dapm_widget *dw)
  1428. {
  1429. struct avs_tplg_path_template *template = w->priv;
  1430. template->w = w;
  1431. return 0;
  1432. }
  1433. static int avs_dai_load(struct snd_soc_component *comp, int index,
  1434. struct snd_soc_dai_driver *dai_drv, struct snd_soc_tplg_pcm *pcm,
  1435. struct snd_soc_dai *dai)
  1436. {
  1437. u32 fe_subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  1438. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  1439. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX;
  1440. if (pcm) {
  1441. dai_drv->ops = &avs_dai_fe_ops;
  1442. dai_drv->capture.subformats = fe_subformats;
  1443. dai_drv->playback.subformats = fe_subformats;
  1444. }
  1445. return 0;
  1446. }
  1447. static int avs_link_load(struct snd_soc_component *comp, int index, struct snd_soc_dai_link *link,
  1448. struct snd_soc_tplg_link_config *cfg)
  1449. {
  1450. if (link->ignore_suspend && !AVS_S0IX_SUPPORTED) {
  1451. dev_info_once(comp->dev, "Device does not support S0IX, check BIOS settings\n");
  1452. link->ignore_suspend = false;
  1453. }
  1454. if (!link->no_pcm) {
  1455. /* Stream control handled by IPCs. */
  1456. link->nonatomic = true;
  1457. /* Open LINK (BE) pipes last and close them first to prevent xruns. */
  1458. link->trigger[0] = SND_SOC_DPCM_TRIGGER_PRE;
  1459. link->trigger[1] = SND_SOC_DPCM_TRIGGER_PRE;
  1460. } else {
  1461. /* Do not ignore codec capabilities. */
  1462. link->dpcm_merged_format = 1;
  1463. }
  1464. return 0;
  1465. }
  1466. static const struct avs_tplg_token_parser manifest_parsers[] = {
  1467. {
  1468. .token = AVS_TKN_MANIFEST_NAME_STRING,
  1469. .type = SND_SOC_TPLG_TUPLE_TYPE_STRING,
  1470. .offset = offsetof(struct avs_tplg, name),
  1471. .parse = parse_link_formatted_string,
  1472. },
  1473. {
  1474. .token = AVS_TKN_MANIFEST_VERSION_U32,
  1475. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1476. .offset = offsetof(struct avs_tplg, version),
  1477. .parse = avs_parse_word_token,
  1478. },
  1479. };
  1480. static int avs_manifest(struct snd_soc_component *comp, int index,
  1481. struct snd_soc_tplg_manifest *manifest)
  1482. {
  1483. struct snd_soc_tplg_vendor_array *tuples = manifest->priv.array;
  1484. struct avs_soc_component *acomp = to_avs_soc_component(comp);
  1485. size_t remaining = le32_to_cpu(manifest->priv.size);
  1486. bool has_init_config = true;
  1487. u32 offset;
  1488. int ret;
  1489. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1490. AVS_TKN_MANIFEST_NUM_LIBRARIES_U32, &offset);
  1491. /* Manifest MUST begin with a header. */
  1492. if (!ret && !offset)
  1493. ret = -EINVAL;
  1494. if (ret) {
  1495. dev_err(comp->dev, "incorrect manifest format: %d\n", ret);
  1496. return ret;
  1497. }
  1498. /* Process header which precedes any of the dictionaries. */
  1499. ret = avs_parse_tokens(comp, acomp->tplg, manifest_parsers,
  1500. ARRAY_SIZE(manifest_parsers), tuples, offset);
  1501. if (ret < 0)
  1502. return ret;
  1503. remaining -= offset;
  1504. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1505. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1506. AVS_TKN_MANIFEST_NUM_AFMTS_U32, &offset);
  1507. if (ret) {
  1508. dev_err(comp->dev, "audio formats lookup failed: %d\n", ret);
  1509. return ret;
  1510. }
  1511. /* Libraries dictionary. */
  1512. ret = avs_tplg_parse_libraries(comp, tuples, offset);
  1513. if (ret < 0)
  1514. return ret;
  1515. remaining -= offset;
  1516. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1517. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1518. AVS_TKN_MANIFEST_NUM_MODCFGS_BASE_U32, &offset);
  1519. if (ret) {
  1520. dev_err(comp->dev, "modcfgs_base lookup failed: %d\n", ret);
  1521. return ret;
  1522. }
  1523. /* Audio formats dictionary. */
  1524. ret = avs_tplg_parse_audio_formats(comp, tuples, offset);
  1525. if (ret < 0)
  1526. return ret;
  1527. remaining -= offset;
  1528. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1529. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1530. AVS_TKN_MANIFEST_NUM_MODCFGS_EXT_U32, &offset);
  1531. if (ret) {
  1532. dev_err(comp->dev, "modcfgs_ext lookup failed: %d\n", ret);
  1533. return ret;
  1534. }
  1535. /* Module configs-base dictionary. */
  1536. ret = avs_tplg_parse_modcfgs_base(comp, tuples, offset);
  1537. if (ret < 0)
  1538. return ret;
  1539. remaining -= offset;
  1540. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1541. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1542. AVS_TKN_MANIFEST_NUM_PPLCFGS_U32, &offset);
  1543. if (ret) {
  1544. dev_err(comp->dev, "pplcfgs lookup failed: %d\n", ret);
  1545. return ret;
  1546. }
  1547. /* Module configs-ext dictionary. */
  1548. ret = avs_tplg_parse_modcfgs_ext(comp, tuples, offset);
  1549. if (ret < 0)
  1550. return ret;
  1551. remaining -= offset;
  1552. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1553. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1554. AVS_TKN_MANIFEST_NUM_BINDINGS_U32, &offset);
  1555. if (ret) {
  1556. dev_err(comp->dev, "bindings lookup failed: %d\n", ret);
  1557. return ret;
  1558. }
  1559. /* Pipeline configs dictionary. */
  1560. ret = avs_tplg_parse_pplcfgs(comp, tuples, offset);
  1561. if (ret < 0)
  1562. return ret;
  1563. remaining -= offset;
  1564. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1565. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1566. AVS_TKN_MANIFEST_NUM_CONDPATH_TMPLS_U32, &offset);
  1567. if (ret) {
  1568. dev_err(comp->dev, "condpath lookup failed: %d\n", ret);
  1569. return ret;
  1570. }
  1571. /* Bindings dictionary. */
  1572. ret = avs_tplg_parse_bindings(comp, tuples, offset);
  1573. if (ret < 0)
  1574. return ret;
  1575. remaining -= offset;
  1576. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1577. ret = avs_tplg_vendor_array_lookup(tuples, remaining,
  1578. AVS_TKN_MANIFEST_NUM_INIT_CONFIGS_U32, &offset);
  1579. if (ret == -ENOENT) {
  1580. dev_dbg(comp->dev, "init config lookup failed: %d\n", ret);
  1581. has_init_config = false;
  1582. } else if (ret) {
  1583. dev_err(comp->dev, "init config lookup failed: %d\n", ret);
  1584. return ret;
  1585. }
  1586. if (!has_init_config)
  1587. return 0;
  1588. remaining -= offset;
  1589. tuples = avs_tplg_vendor_array_at(tuples, offset);
  1590. /* Initial configs dictionary. */
  1591. ret = avs_tplg_parse_initial_configs(comp, tuples, remaining);
  1592. if (ret < 0)
  1593. return ret;
  1594. return 0;
  1595. }
  1596. #define AVS_CONTROL_OPS_VOLUME 257
  1597. static const struct snd_soc_tplg_kcontrol_ops avs_control_ops[] = {
  1598. {
  1599. .id = AVS_CONTROL_OPS_VOLUME,
  1600. .get = avs_control_volume_get,
  1601. .put = avs_control_volume_put,
  1602. },
  1603. };
  1604. static const struct avs_tplg_token_parser control_parsers[] = {
  1605. {
  1606. .token = AVS_TKN_KCONTROL_ID_U32,
  1607. .type = SND_SOC_TPLG_TUPLE_TYPE_WORD,
  1608. .offset = offsetof(struct avs_control_data, id),
  1609. .parse = avs_parse_word_token,
  1610. },
  1611. };
  1612. static int
  1613. avs_control_load(struct snd_soc_component *comp, int index, struct snd_kcontrol_new *ctmpl,
  1614. struct snd_soc_tplg_ctl_hdr *hdr)
  1615. {
  1616. struct snd_soc_tplg_vendor_array *tuples;
  1617. struct snd_soc_tplg_mixer_control *tmc;
  1618. struct avs_control_data *ctl_data;
  1619. struct soc_mixer_control *mc;
  1620. size_t block_size;
  1621. int ret;
  1622. switch (le32_to_cpu(hdr->type)) {
  1623. case SND_SOC_TPLG_TYPE_MIXER:
  1624. tmc = container_of(hdr, typeof(*tmc), hdr);
  1625. tuples = tmc->priv.array;
  1626. block_size = le32_to_cpu(tmc->priv.size);
  1627. break;
  1628. default:
  1629. return -EINVAL;
  1630. }
  1631. ctl_data = devm_kzalloc(comp->card->dev, sizeof(*ctl_data), GFP_KERNEL);
  1632. if (!ctl_data)
  1633. return -ENOMEM;
  1634. ret = parse_dictionary_entries(comp, tuples, block_size, ctl_data, 1, sizeof(*ctl_data),
  1635. AVS_TKN_KCONTROL_ID_U32, control_parsers,
  1636. ARRAY_SIZE(control_parsers));
  1637. if (ret)
  1638. return ret;
  1639. mc = (struct soc_mixer_control *)ctmpl->private_value;
  1640. mc->dobj.private = ctl_data;
  1641. return 0;
  1642. }
  1643. static const struct snd_soc_tplg_ops avs_tplg_ops = {
  1644. .io_ops = avs_control_ops,
  1645. .io_ops_count = ARRAY_SIZE(avs_control_ops),
  1646. .control_load = avs_control_load,
  1647. .dapm_route_load = avs_route_load,
  1648. .widget_load = avs_widget_load,
  1649. .widget_ready = avs_widget_ready,
  1650. .dai_load = avs_dai_load,
  1651. .link_load = avs_link_load,
  1652. .manifest = avs_manifest,
  1653. };
  1654. struct avs_tplg *avs_tplg_new(struct snd_soc_component *comp)
  1655. {
  1656. struct avs_tplg *tplg;
  1657. tplg = devm_kzalloc(comp->card->dev, sizeof(*tplg), GFP_KERNEL);
  1658. if (!tplg)
  1659. return NULL;
  1660. tplg->comp = comp;
  1661. INIT_LIST_HEAD(&tplg->path_tmpl_list);
  1662. return tplg;
  1663. }
  1664. int avs_load_topology(struct snd_soc_component *comp, const char *filename)
  1665. {
  1666. const struct firmware *fw;
  1667. int ret;
  1668. ret = request_firmware(&fw, filename, comp->dev);
  1669. if (ret < 0) {
  1670. dev_err(comp->dev, "request topology \"%s\" failed: %d\n", filename, ret);
  1671. return ret;
  1672. }
  1673. ret = snd_soc_tplg_component_load(comp, &avs_tplg_ops, fw);
  1674. if (ret < 0)
  1675. dev_err(comp->dev, "load topology \"%s\" failed: %d\n", filename, ret);
  1676. release_firmware(fw);
  1677. return ret;
  1678. }
  1679. int avs_remove_topology(struct snd_soc_component *comp)
  1680. {
  1681. snd_soc_tplg_component_remove(comp);
  1682. return 0;
  1683. }