topology.c 69 KB

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