sdca_asoc.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2025 Cirrus Logic, Inc. and
  3. // Cirrus Logic International Semiconductor Ltd.
  4. /*
  5. * The MIPI SDCA specification is available for public downloads at
  6. * https://www.mipi.org/mipi-sdca-v1-0-download
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/bitmap.h>
  10. #include <linux/build_bug.h>
  11. #include <linux/delay.h>
  12. #include <linux/dev_printk.h>
  13. #include <linux/device.h>
  14. #include <linux/minmax.h>
  15. #include <linux/module.h>
  16. #include <linux/overflow.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regmap.h>
  19. #include <linux/soundwire/sdw_registers.h>
  20. #include <linux/string_helpers.h>
  21. #include <linux/types.h>
  22. #include <sound/control.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/sdca.h>
  26. #include <sound/sdca_asoc.h>
  27. #include <sound/sdca_function.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-component.h>
  30. #include <sound/soc-dai.h>
  31. #include <sound/soc-dapm.h>
  32. #include <sound/tlv.h>
  33. static bool exported_control(struct sdca_entity *entity, struct sdca_control *control)
  34. {
  35. switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
  36. case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
  37. return true;
  38. default:
  39. break;
  40. }
  41. return control->layers & (SDCA_ACCESS_LAYER_USER |
  42. SDCA_ACCESS_LAYER_APPLICATION);
  43. }
  44. static bool readonly_control(struct sdca_control *control)
  45. {
  46. return control->has_fixed || control->mode == SDCA_ACCESS_MODE_RO;
  47. }
  48. /**
  49. * sdca_asoc_count_component - count the various component parts
  50. * @dev: Pointer to the device against which allocations will be done.
  51. * @function: Pointer to the Function information.
  52. * @num_widgets: Output integer pointer, will be filled with the
  53. * required number of DAPM widgets for the Function.
  54. * @num_routes: Output integer pointer, will be filled with the
  55. * required number of DAPM routes for the Function.
  56. * @num_controls: Output integer pointer, will be filled with the
  57. * required number of ALSA controls for the Function.
  58. * @num_dais: Output integer pointer, will be filled with the
  59. * required number of ASoC DAIs for the Function.
  60. *
  61. * This function counts various things within the SDCA Function such
  62. * that the calling driver can allocate appropriate space before
  63. * calling the appropriate population functions.
  64. *
  65. * Return: Returns zero on success, and a negative error code on failure.
  66. */
  67. int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *function,
  68. int *num_widgets, int *num_routes, int *num_controls,
  69. int *num_dais)
  70. {
  71. int i, j;
  72. *num_widgets = function->num_entities - 1;
  73. *num_routes = 0;
  74. *num_controls = 0;
  75. *num_dais = 0;
  76. for (i = 0; i < function->num_entities - 1; i++) {
  77. struct sdca_entity *entity = &function->entities[i];
  78. /* Add supply/DAI widget connections */
  79. switch (entity->type) {
  80. case SDCA_ENTITY_TYPE_IT:
  81. case SDCA_ENTITY_TYPE_OT:
  82. *num_routes += !!entity->iot.clock;
  83. *num_routes += !!entity->iot.is_dataport;
  84. *num_controls += !entity->iot.is_dataport;
  85. *num_dais += !!entity->iot.is_dataport;
  86. break;
  87. case SDCA_ENTITY_TYPE_PDE:
  88. *num_routes += entity->pde.num_managed;
  89. break;
  90. default:
  91. break;
  92. }
  93. if (entity->group)
  94. (*num_routes)++;
  95. /* Add primary entity connections from DisCo */
  96. *num_routes += entity->num_sources;
  97. for (j = 0; j < entity->num_controls; j++) {
  98. if (exported_control(entity, &entity->controls[j]))
  99. (*num_controls)++;
  100. }
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL_NS(sdca_asoc_count_component, "SND_SOC_SDCA");
  105. static int ge_put_enum_double(struct snd_kcontrol *kcontrol,
  106. struct snd_ctl_elem_value *ucontrol)
  107. {
  108. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
  109. struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
  110. struct device *dev = component->dev;
  111. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  112. unsigned int *item = ucontrol->value.enumerated.item;
  113. unsigned int reg = e->reg;
  114. int ret;
  115. reg &= ~SDW_SDCA_CTL_CSEL(0x3F);
  116. reg |= SDW_SDCA_CTL_CSEL(SDCA_CTL_GE_DETECTED_MODE);
  117. ret = pm_runtime_resume_and_get(dev);
  118. if (ret < 0) {
  119. dev_err(dev, "failed to resume writing %s: %d\n",
  120. kcontrol->id.name, ret);
  121. return ret;
  122. }
  123. ret = snd_soc_component_read(component, reg);
  124. pm_runtime_put(dev);
  125. if (ret < 0)
  126. return ret;
  127. else if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS)
  128. return -EBUSY;
  129. ret = snd_soc_enum_item_to_val(e, item[0]);
  130. if (ret <= SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS)
  131. return -EINVAL;
  132. return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  133. }
  134. static int entity_early_parse_ge(struct device *dev,
  135. struct sdca_function_data *function,
  136. struct sdca_entity *entity)
  137. {
  138. struct sdca_control_range *range;
  139. struct sdca_control *control;
  140. struct snd_kcontrol_new *kctl;
  141. struct soc_enum *soc_enum;
  142. const char *control_name;
  143. unsigned int *values;
  144. const char **texts;
  145. int i;
  146. control = sdca_selector_find_control(dev, entity, SDCA_CTL_GE_SELECTED_MODE);
  147. if (!control)
  148. return -EINVAL;
  149. if (control->layers != SDCA_ACCESS_LAYER_CLASS)
  150. dev_warn(dev, "%s: unexpected access layer: %x\n",
  151. entity->label, control->layers);
  152. range = sdca_control_find_range(dev, entity, control, SDCA_SELECTED_MODE_NCOLS, 0);
  153. if (!range)
  154. return -EINVAL;
  155. control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
  156. entity->label, control->label);
  157. if (!control_name)
  158. return -ENOMEM;
  159. kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
  160. if (!kctl)
  161. return -ENOMEM;
  162. soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
  163. if (!soc_enum)
  164. return -ENOMEM;
  165. texts = devm_kcalloc(dev, range->rows + 3, sizeof(*texts), GFP_KERNEL);
  166. if (!texts)
  167. return -ENOMEM;
  168. values = devm_kcalloc(dev, range->rows + 3, sizeof(*values), GFP_KERNEL);
  169. if (!values)
  170. return -ENOMEM;
  171. texts[0] = "Jack Unplugged";
  172. texts[1] = "Jack Unknown";
  173. texts[2] = "Detection in Progress";
  174. values[0] = SDCA_DETECTED_MODE_JACK_UNPLUGGED;
  175. values[1] = SDCA_DETECTED_MODE_JACK_UNKNOWN;
  176. values[2] = SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS;
  177. for (i = 0; i < range->rows; i++) {
  178. enum sdca_terminal_type type;
  179. type = sdca_range(range, SDCA_SELECTED_MODE_TERM_TYPE, i);
  180. values[i + 3] = sdca_range(range, SDCA_SELECTED_MODE_INDEX, i);
  181. texts[i + 3] = sdca_find_terminal_name(type);
  182. if (!texts[i + 3]) {
  183. dev_err(dev, "%s: unrecognised terminal type: %#x\n",
  184. entity->label, type);
  185. return -EINVAL;
  186. }
  187. }
  188. soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
  189. soc_enum->items = range->rows + 3;
  190. soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
  191. soc_enum->texts = texts;
  192. soc_enum->values = values;
  193. kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  194. kctl->name = control_name;
  195. kctl->info = snd_soc_info_enum_double;
  196. kctl->get = snd_soc_dapm_get_enum_double;
  197. kctl->put = ge_put_enum_double;
  198. kctl->private_value = (unsigned long)soc_enum;
  199. entity->ge.kctl = kctl;
  200. return 0;
  201. }
  202. static void add_route(struct snd_soc_dapm_route **route, const char *sink,
  203. const char *control, const char *source)
  204. {
  205. (*route)->sink = sink;
  206. (*route)->control = control;
  207. (*route)->source = source;
  208. (*route)++;
  209. }
  210. static int entity_parse_simple(struct device *dev,
  211. struct sdca_function_data *function,
  212. struct sdca_entity *entity,
  213. struct snd_soc_dapm_widget **widget,
  214. struct snd_soc_dapm_route **route,
  215. enum snd_soc_dapm_type id)
  216. {
  217. int i;
  218. (*widget)->id = id;
  219. (*widget)++;
  220. for (i = 0; i < entity->num_sources; i++)
  221. add_route(route, entity->label, NULL, entity->sources[i]->label);
  222. return 0;
  223. }
  224. static int entity_parse_it(struct device *dev,
  225. struct sdca_function_data *function,
  226. struct sdca_entity *entity,
  227. struct snd_soc_dapm_widget **widget,
  228. struct snd_soc_dapm_route **route)
  229. {
  230. int i;
  231. if (entity->iot.is_dataport) {
  232. const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
  233. entity->label, "Playback");
  234. if (!aif_name)
  235. return -ENOMEM;
  236. (*widget)->id = snd_soc_dapm_aif_in;
  237. add_route(route, entity->label, NULL, aif_name);
  238. } else {
  239. (*widget)->id = snd_soc_dapm_mic;
  240. }
  241. if (entity->iot.clock)
  242. add_route(route, entity->label, NULL, entity->iot.clock->label);
  243. for (i = 0; i < entity->num_sources; i++)
  244. add_route(route, entity->label, NULL, entity->sources[i]->label);
  245. (*widget)++;
  246. return 0;
  247. }
  248. static int entity_parse_ot(struct device *dev,
  249. struct sdca_function_data *function,
  250. struct sdca_entity *entity,
  251. struct snd_soc_dapm_widget **widget,
  252. struct snd_soc_dapm_route **route)
  253. {
  254. int i;
  255. if (entity->iot.is_dataport) {
  256. const char *aif_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
  257. entity->label, "Capture");
  258. if (!aif_name)
  259. return -ENOMEM;
  260. (*widget)->id = snd_soc_dapm_aif_out;
  261. add_route(route, aif_name, NULL, entity->label);
  262. } else {
  263. (*widget)->id = snd_soc_dapm_spk;
  264. }
  265. if (entity->iot.clock)
  266. add_route(route, entity->label, NULL, entity->iot.clock->label);
  267. for (i = 0; i < entity->num_sources; i++)
  268. add_route(route, entity->label, NULL, entity->sources[i]->label);
  269. (*widget)++;
  270. return 0;
  271. }
  272. static int entity_pde_event(struct snd_soc_dapm_widget *widget,
  273. struct snd_kcontrol *kctl, int event)
  274. {
  275. struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
  276. struct sdca_entity *entity = widget->priv;
  277. static const int polls = 100;
  278. unsigned int reg, val;
  279. int from, to, i;
  280. int poll_us;
  281. int ret;
  282. if (!component)
  283. return -EIO;
  284. switch (event) {
  285. case SND_SOC_DAPM_POST_PMD:
  286. from = widget->on_val;
  287. to = widget->off_val;
  288. break;
  289. case SND_SOC_DAPM_POST_PMU:
  290. from = widget->off_val;
  291. to = widget->on_val;
  292. break;
  293. default:
  294. return 0;
  295. }
  296. for (i = 0; i < entity->pde.num_max_delay; i++) {
  297. struct sdca_pde_delay *delay = &entity->pde.max_delay[i];
  298. if (delay->from_ps == from && delay->to_ps == to) {
  299. poll_us = delay->us / polls;
  300. break;
  301. }
  302. }
  303. reg = SDW_SDCA_CTL(SDW_SDCA_CTL_FUNC(widget->reg),
  304. SDW_SDCA_CTL_ENT(widget->reg),
  305. SDCA_CTL_PDE_ACTUAL_PS, 0);
  306. for (i = 0; i < polls; i++) {
  307. if (i)
  308. fsleep(poll_us);
  309. ret = regmap_read(component->regmap, reg, &val);
  310. if (ret)
  311. return ret;
  312. else if (val == to)
  313. return 0;
  314. }
  315. dev_err(component->dev, "%s: power transition failed: %x\n",
  316. entity->label, val);
  317. return -ETIMEDOUT;
  318. }
  319. static int entity_parse_pde(struct device *dev,
  320. struct sdca_function_data *function,
  321. struct sdca_entity *entity,
  322. struct snd_soc_dapm_widget **widget,
  323. struct snd_soc_dapm_route **route)
  324. {
  325. unsigned int target = (1 << SDCA_PDE_PS0) | (1 << SDCA_PDE_PS3);
  326. struct sdca_control_range *range;
  327. struct sdca_control *control;
  328. unsigned int mask = 0;
  329. int i;
  330. control = sdca_selector_find_control(dev, entity, SDCA_CTL_PDE_REQUESTED_PS);
  331. if (!control)
  332. return -EINVAL;
  333. /* Power should only be controlled by the driver */
  334. if (control->layers != SDCA_ACCESS_LAYER_CLASS)
  335. dev_warn(dev, "%s: unexpected access layer: %x\n",
  336. entity->label, control->layers);
  337. range = sdca_control_find_range(dev, entity, control, SDCA_REQUESTED_PS_NCOLS, 0);
  338. if (!range)
  339. return -EINVAL;
  340. for (i = 0; i < range->rows; i++)
  341. mask |= 1 << sdca_range(range, SDCA_REQUESTED_PS_STATE, i);
  342. if ((mask & target) != target) {
  343. dev_err(dev, "%s: power control missing states\n", entity->label);
  344. return -EINVAL;
  345. }
  346. (*widget)->id = snd_soc_dapm_supply;
  347. (*widget)->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
  348. (*widget)->mask = GENMASK(control->nbits - 1, 0);
  349. (*widget)->on_val = SDCA_PDE_PS0;
  350. (*widget)->off_val = SDCA_PDE_PS3;
  351. (*widget)->event_flags = SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD;
  352. (*widget)->event = entity_pde_event;
  353. (*widget)->priv = entity;
  354. (*widget)++;
  355. for (i = 0; i < entity->pde.num_managed; i++)
  356. add_route(route, entity->pde.managed[i]->label, NULL, entity->label);
  357. for (i = 0; i < entity->num_sources; i++)
  358. add_route(route, entity->label, NULL, entity->sources[i]->label);
  359. return 0;
  360. }
  361. /* Device selector units are controlled through a group entity */
  362. static int entity_parse_su_device(struct device *dev,
  363. struct sdca_function_data *function,
  364. struct sdca_entity *entity,
  365. struct snd_soc_dapm_widget **widget,
  366. struct snd_soc_dapm_route **route)
  367. {
  368. struct sdca_control_range *range;
  369. int num_routes = 0;
  370. int i, j;
  371. if (!entity->group) {
  372. dev_err(dev, "%s: device selector unit missing group\n", entity->label);
  373. return -EINVAL;
  374. }
  375. range = sdca_selector_find_range(dev, entity->group, SDCA_CTL_GE_SELECTED_MODE,
  376. SDCA_SELECTED_MODE_NCOLS, 0);
  377. if (!range)
  378. return -EINVAL;
  379. (*widget)->id = snd_soc_dapm_mux;
  380. (*widget)->kcontrol_news = entity->group->ge.kctl;
  381. (*widget)->num_kcontrols = 1;
  382. (*widget)++;
  383. for (i = 0; i < entity->group->ge.num_modes; i++) {
  384. struct sdca_ge_mode *mode = &entity->group->ge.modes[i];
  385. for (j = 0; j < mode->num_controls; j++) {
  386. struct sdca_ge_control *affected = &mode->controls[j];
  387. int term;
  388. if (affected->id != entity->id ||
  389. affected->sel != SDCA_CTL_SU_SELECTOR ||
  390. !affected->val)
  391. continue;
  392. if (affected->val - 1 >= entity->num_sources) {
  393. dev_err(dev, "%s: bad control value: %#x\n",
  394. entity->label, affected->val);
  395. return -EINVAL;
  396. }
  397. if (++num_routes > entity->num_sources) {
  398. dev_err(dev, "%s: too many input routes\n", entity->label);
  399. return -EINVAL;
  400. }
  401. term = sdca_range_search(range, SDCA_SELECTED_MODE_INDEX,
  402. mode->val, SDCA_SELECTED_MODE_TERM_TYPE);
  403. if (!term) {
  404. dev_err(dev, "%s: mode not found: %#x\n",
  405. entity->label, mode->val);
  406. return -EINVAL;
  407. }
  408. add_route(route, entity->label, sdca_find_terminal_name(term),
  409. entity->sources[affected->val - 1]->label);
  410. }
  411. }
  412. return 0;
  413. }
  414. /* Class selector units will be exported as an ALSA control */
  415. static int entity_parse_su_class(struct device *dev,
  416. struct sdca_function_data *function,
  417. struct sdca_entity *entity,
  418. struct sdca_control *control,
  419. struct snd_soc_dapm_widget **widget,
  420. struct snd_soc_dapm_route **route)
  421. {
  422. struct snd_kcontrol_new *kctl;
  423. struct soc_enum *soc_enum;
  424. const char **texts;
  425. int i;
  426. kctl = devm_kzalloc(dev, sizeof(*kctl), GFP_KERNEL);
  427. if (!kctl)
  428. return -ENOMEM;
  429. soc_enum = devm_kzalloc(dev, sizeof(*soc_enum), GFP_KERNEL);
  430. if (!soc_enum)
  431. return -ENOMEM;
  432. texts = devm_kcalloc(dev, entity->num_sources + 1, sizeof(*texts), GFP_KERNEL);
  433. if (!texts)
  434. return -ENOMEM;
  435. texts[0] = "No Signal";
  436. for (i = 0; i < entity->num_sources; i++)
  437. texts[i + 1] = entity->sources[i]->label;
  438. soc_enum->reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
  439. soc_enum->items = entity->num_sources + 1;
  440. soc_enum->mask = roundup_pow_of_two(soc_enum->items) - 1;
  441. soc_enum->texts = texts;
  442. kctl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  443. kctl->name = "Route";
  444. kctl->info = snd_soc_info_enum_double;
  445. kctl->get = snd_soc_dapm_get_enum_double;
  446. kctl->put = snd_soc_dapm_put_enum_double;
  447. kctl->private_value = (unsigned long)soc_enum;
  448. (*widget)->id = snd_soc_dapm_mux;
  449. (*widget)->kcontrol_news = kctl;
  450. (*widget)->num_kcontrols = 1;
  451. (*widget)++;
  452. for (i = 0; i < entity->num_sources; i++)
  453. add_route(route, entity->label, texts[i + 1], entity->sources[i]->label);
  454. return 0;
  455. }
  456. static int entity_parse_su(struct device *dev,
  457. struct sdca_function_data *function,
  458. struct sdca_entity *entity,
  459. struct snd_soc_dapm_widget **widget,
  460. struct snd_soc_dapm_route **route)
  461. {
  462. struct sdca_control *control;
  463. if (!entity->num_sources) {
  464. dev_err(dev, "%s: selector with no inputs\n", entity->label);
  465. return -EINVAL;
  466. }
  467. control = sdca_selector_find_control(dev, entity, SDCA_CTL_SU_SELECTOR);
  468. if (!control)
  469. return -EINVAL;
  470. if (control->layers == SDCA_ACCESS_LAYER_DEVICE)
  471. return entity_parse_su_device(dev, function, entity, widget, route);
  472. if (control->layers != SDCA_ACCESS_LAYER_CLASS)
  473. dev_warn(dev, "%s: unexpected access layer: %x\n",
  474. entity->label, control->layers);
  475. return entity_parse_su_class(dev, function, entity, control, widget, route);
  476. }
  477. static int entity_parse_mu(struct device *dev,
  478. struct sdca_function_data *function,
  479. struct sdca_entity *entity,
  480. struct snd_soc_dapm_widget **widget,
  481. struct snd_soc_dapm_route **route)
  482. {
  483. struct sdca_control *control;
  484. struct snd_kcontrol_new *kctl;
  485. int i;
  486. if (!entity->num_sources) {
  487. dev_err(dev, "%s: selector 1 or more inputs\n", entity->label);
  488. return -EINVAL;
  489. }
  490. control = sdca_selector_find_control(dev, entity, SDCA_CTL_MU_MIXER);
  491. if (!control)
  492. return -EINVAL;
  493. /* MU control should be through DAPM */
  494. if (control->layers != SDCA_ACCESS_LAYER_CLASS)
  495. dev_warn(dev, "%s: unexpected access layer: %x\n",
  496. entity->label, control->layers);
  497. kctl = devm_kcalloc(dev, entity->num_sources, sizeof(*kctl), GFP_KERNEL);
  498. if (!kctl)
  499. return -ENOMEM;
  500. for (i = 0; i < entity->num_sources; i++) {
  501. const char *control_name;
  502. struct soc_mixer_control *mc;
  503. control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %d",
  504. control->label, i + 1);
  505. if (!control_name)
  506. return -ENOMEM;
  507. mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
  508. if (!mc)
  509. return -ENOMEM;
  510. mc->reg = SND_SOC_NOPM;
  511. mc->rreg = SND_SOC_NOPM;
  512. mc->invert = 1; // Ensure default is connected
  513. mc->min = 0;
  514. mc->max = 1;
  515. kctl[i].name = control_name;
  516. kctl[i].private_value = (unsigned long)mc;
  517. kctl[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  518. kctl[i].info = snd_soc_info_volsw;
  519. kctl[i].get = snd_soc_dapm_get_volsw;
  520. kctl[i].put = snd_soc_dapm_put_volsw;
  521. }
  522. (*widget)->id = snd_soc_dapm_mixer;
  523. (*widget)->kcontrol_news = kctl;
  524. (*widget)->num_kcontrols = entity->num_sources;
  525. (*widget)++;
  526. for (i = 0; i < entity->num_sources; i++)
  527. add_route(route, entity->label, kctl[i].name, entity->sources[i]->label);
  528. return 0;
  529. }
  530. static int entity_cs_event(struct snd_soc_dapm_widget *widget,
  531. struct snd_kcontrol *kctl, int event)
  532. {
  533. struct snd_soc_component *component = snd_soc_dapm_to_component(widget->dapm);
  534. struct sdca_entity *entity = widget->priv;
  535. if (!component)
  536. return -EIO;
  537. if (entity->cs.max_delay)
  538. fsleep(entity->cs.max_delay);
  539. return 0;
  540. }
  541. static int entity_parse_cs(struct device *dev,
  542. struct sdca_function_data *function,
  543. struct sdca_entity *entity,
  544. struct snd_soc_dapm_widget **widget,
  545. struct snd_soc_dapm_route **route)
  546. {
  547. int i;
  548. (*widget)->id = snd_soc_dapm_supply;
  549. (*widget)->subseq = 1; /* Ensure these run after PDEs */
  550. (*widget)->event_flags = SND_SOC_DAPM_POST_PMU;
  551. (*widget)->event = entity_cs_event;
  552. (*widget)->priv = entity;
  553. (*widget)++;
  554. for (i = 0; i < entity->num_sources; i++)
  555. add_route(route, entity->label, NULL, entity->sources[i]->label);
  556. return 0;
  557. }
  558. /**
  559. * sdca_asoc_populate_dapm - fill in arrays of DAPM widgets and routes
  560. * @dev: Pointer to the device against which allocations will be done.
  561. * @function: Pointer to the Function information.
  562. * @widget: Array of DAPM widgets to be populated.
  563. * @route: Array of DAPM routes to be populated.
  564. *
  565. * This function populates arrays of DAPM widgets and routes from the
  566. * DisCo information for a particular SDCA Function. Typically,
  567. * snd_soc_asoc_count_component will be used to allocate appropriately
  568. * sized arrays before calling this function.
  569. *
  570. * Return: Returns zero on success, and a negative error code on failure.
  571. */
  572. int sdca_asoc_populate_dapm(struct device *dev, struct sdca_function_data *function,
  573. struct snd_soc_dapm_widget *widget,
  574. struct snd_soc_dapm_route *route)
  575. {
  576. int ret;
  577. int i;
  578. for (i = 0; i < function->num_entities - 1; i++) {
  579. struct sdca_entity *entity = &function->entities[i];
  580. /*
  581. * Some entities need to add controls "early" as they are
  582. * referenced by other entities.
  583. */
  584. switch (entity->type) {
  585. case SDCA_ENTITY_TYPE_GE:
  586. ret = entity_early_parse_ge(dev, function, entity);
  587. if (ret)
  588. return ret;
  589. break;
  590. default:
  591. break;
  592. }
  593. }
  594. for (i = 0; i < function->num_entities - 1; i++) {
  595. struct sdca_entity *entity = &function->entities[i];
  596. widget->name = entity->label;
  597. widget->reg = SND_SOC_NOPM;
  598. switch (entity->type) {
  599. case SDCA_ENTITY_TYPE_IT:
  600. ret = entity_parse_it(dev, function, entity, &widget, &route);
  601. break;
  602. case SDCA_ENTITY_TYPE_OT:
  603. ret = entity_parse_ot(dev, function, entity, &widget, &route);
  604. break;
  605. case SDCA_ENTITY_TYPE_PDE:
  606. ret = entity_parse_pde(dev, function, entity, &widget, &route);
  607. break;
  608. case SDCA_ENTITY_TYPE_SU:
  609. ret = entity_parse_su(dev, function, entity, &widget, &route);
  610. break;
  611. case SDCA_ENTITY_TYPE_MU:
  612. ret = entity_parse_mu(dev, function, entity, &widget, &route);
  613. break;
  614. case SDCA_ENTITY_TYPE_CS:
  615. ret = entity_parse_cs(dev, function, entity, &widget, &route);
  616. break;
  617. case SDCA_ENTITY_TYPE_CX:
  618. /*
  619. * FIXME: For now we will just treat these as a supply,
  620. * meaning all options are enabled.
  621. */
  622. dev_warn(dev, "%s: clock selectors not fully supported yet\n",
  623. entity->label);
  624. ret = entity_parse_simple(dev, function, entity, &widget,
  625. &route, snd_soc_dapm_supply);
  626. break;
  627. case SDCA_ENTITY_TYPE_TG:
  628. ret = entity_parse_simple(dev, function, entity, &widget,
  629. &route, snd_soc_dapm_siggen);
  630. break;
  631. case SDCA_ENTITY_TYPE_GE:
  632. ret = entity_parse_simple(dev, function, entity, &widget,
  633. &route, snd_soc_dapm_supply);
  634. break;
  635. default:
  636. ret = entity_parse_simple(dev, function, entity, &widget,
  637. &route, snd_soc_dapm_pga);
  638. break;
  639. }
  640. if (ret)
  641. return ret;
  642. if (entity->group)
  643. add_route(&route, entity->label, NULL, entity->group->label);
  644. }
  645. return 0;
  646. }
  647. EXPORT_SYMBOL_NS(sdca_asoc_populate_dapm, "SND_SOC_SDCA");
  648. static int control_limit_kctl(struct device *dev,
  649. struct sdca_entity *entity,
  650. struct sdca_control *control,
  651. struct snd_kcontrol_new *kctl)
  652. {
  653. struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
  654. struct sdca_control_range *range;
  655. int min, max, step;
  656. unsigned int *tlv;
  657. if (control->type != SDCA_CTL_DATATYPE_Q7P8DB)
  658. return 0;
  659. /*
  660. * FIXME: For now only handle the simple case of a single linear range
  661. */
  662. range = sdca_control_find_range(dev, entity, control, SDCA_VOLUME_LINEAR_NCOLS, 1);
  663. if (!range)
  664. return -EINVAL;
  665. min = sdca_range(range, SDCA_VOLUME_LINEAR_MIN, 0);
  666. max = sdca_range(range, SDCA_VOLUME_LINEAR_MAX, 0);
  667. step = sdca_range(range, SDCA_VOLUME_LINEAR_STEP, 0);
  668. min = sign_extend32(min, control->nbits - 1);
  669. max = sign_extend32(max, control->nbits - 1);
  670. tlv = devm_kcalloc(dev, 4, sizeof(*tlv), GFP_KERNEL);
  671. if (!tlv)
  672. return -ENOMEM;
  673. tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
  674. tlv[1] = 2 * sizeof(*tlv);
  675. tlv[2] = (min * 100) >> 8;
  676. tlv[3] = (max * 100) >> 8;
  677. step = (step * 100) >> 8;
  678. mc->min = ((int)tlv[2] / step);
  679. mc->max = ((int)tlv[3] / step);
  680. mc->shift = step;
  681. mc->sign_bit = 15;
  682. mc->sdca_q78 = 1;
  683. kctl->tlv.p = tlv;
  684. kctl->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  685. return 0;
  686. }
  687. static int volatile_get_volsw(struct snd_kcontrol *kcontrol,
  688. struct snd_ctl_elem_value *ucontrol)
  689. {
  690. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  691. struct device *dev = component->dev;
  692. int ret;
  693. ret = pm_runtime_resume_and_get(dev);
  694. if (ret < 0) {
  695. dev_err(dev, "failed to resume reading %s: %d\n",
  696. kcontrol->id.name, ret);
  697. return ret;
  698. }
  699. ret = snd_soc_get_volsw(kcontrol, ucontrol);
  700. pm_runtime_put(dev);
  701. return ret;
  702. }
  703. static int volatile_put_volsw(struct snd_kcontrol *kcontrol,
  704. struct snd_ctl_elem_value *ucontrol)
  705. {
  706. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  707. struct device *dev = component->dev;
  708. int ret;
  709. ret = pm_runtime_resume_and_get(dev);
  710. if (ret < 0) {
  711. dev_err(dev, "failed to resume writing %s: %d\n",
  712. kcontrol->id.name, ret);
  713. return ret;
  714. }
  715. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  716. pm_runtime_put(dev);
  717. return ret;
  718. }
  719. static int populate_control(struct device *dev,
  720. struct sdca_function_data *function,
  721. struct sdca_entity *entity,
  722. struct sdca_control *control,
  723. struct snd_kcontrol_new **kctl)
  724. {
  725. const char *control_suffix = "";
  726. const char *control_name;
  727. struct soc_mixer_control *mc;
  728. int index = 0;
  729. int ret;
  730. int cn;
  731. if (!exported_control(entity, control))
  732. return 0;
  733. if (control->type == SDCA_CTL_DATATYPE_ONEBIT)
  734. control_suffix = " Switch";
  735. control_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s%s", entity->label,
  736. control->label, control_suffix);
  737. if (!control_name)
  738. return -ENOMEM;
  739. mc = devm_kzalloc(dev, sizeof(*mc), GFP_KERNEL);
  740. if (!mc)
  741. return -ENOMEM;
  742. for_each_set_bit(cn, (unsigned long *)&control->cn_list,
  743. BITS_PER_TYPE(control->cn_list)) {
  744. switch (index++) {
  745. case 0:
  746. mc->reg = SDW_SDCA_CTL(function->desc->adr, entity->id,
  747. control->sel, cn);
  748. mc->rreg = mc->reg;
  749. break;
  750. case 1:
  751. mc->rreg = SDW_SDCA_CTL(function->desc->adr, entity->id,
  752. control->sel, cn);
  753. break;
  754. default:
  755. dev_err(dev, "%s: %s: only mono/stereo controls supported\n",
  756. entity->label, control->label);
  757. return -EINVAL;
  758. }
  759. }
  760. mc->min = 0;
  761. mc->max = clamp((0x1ull << control->nbits) - 1, 0, type_max(mc->max));
  762. if (SDCA_CTL_TYPE(entity->type, control->sel) == SDCA_CTL_TYPE_S(FU, MUTE))
  763. mc->invert = true;
  764. (*kctl)->name = control_name;
  765. (*kctl)->private_value = (unsigned long)mc;
  766. (*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  767. (*kctl)->info = snd_soc_info_volsw;
  768. if (control->is_volatile) {
  769. (*kctl)->get = volatile_get_volsw;
  770. (*kctl)->put = volatile_put_volsw;
  771. } else {
  772. (*kctl)->get = snd_soc_get_volsw;
  773. (*kctl)->put = snd_soc_put_volsw;
  774. }
  775. if (readonly_control(control))
  776. (*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READ;
  777. else
  778. (*kctl)->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  779. ret = control_limit_kctl(dev, entity, control, *kctl);
  780. if (ret)
  781. return ret;
  782. (*kctl)++;
  783. return 0;
  784. }
  785. static int populate_pin_switch(struct device *dev,
  786. struct sdca_entity *entity,
  787. struct snd_kcontrol_new **kctl)
  788. {
  789. const char *control_name;
  790. control_name = devm_kasprintf(dev, GFP_KERNEL, "%s Switch", entity->label);
  791. if (!control_name)
  792. return -ENOMEM;
  793. (*kctl)->name = control_name;
  794. (*kctl)->private_value = (unsigned long)entity->label;
  795. (*kctl)->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  796. (*kctl)->info = snd_soc_dapm_info_pin_switch;
  797. (*kctl)->get = snd_soc_dapm_get_component_pin_switch;
  798. (*kctl)->put = snd_soc_dapm_put_component_pin_switch;
  799. (*kctl)++;
  800. return 0;
  801. }
  802. /**
  803. * sdca_asoc_populate_controls - fill in an array of ALSA controls for a Function
  804. * @dev: Pointer to the device against which allocations will be done.
  805. * @function: Pointer to the Function information.
  806. * @kctl: Array of ALSA controls to be populated.
  807. *
  808. * This function populates an array of ALSA controls from the DisCo
  809. * information for a particular SDCA Function. Typically,
  810. * snd_soc_asoc_count_component will be used to allocate an
  811. * appropriately sized array before calling this function.
  812. *
  813. * Return: Returns zero on success, and a negative error code on failure.
  814. */
  815. int sdca_asoc_populate_controls(struct device *dev,
  816. struct sdca_function_data *function,
  817. struct snd_kcontrol_new *kctl)
  818. {
  819. int i, j;
  820. int ret;
  821. for (i = 0; i < function->num_entities; i++) {
  822. struct sdca_entity *entity = &function->entities[i];
  823. switch (entity->type) {
  824. case SDCA_ENTITY_TYPE_IT:
  825. case SDCA_ENTITY_TYPE_OT:
  826. if (!entity->iot.is_dataport) {
  827. ret = populate_pin_switch(dev, entity, &kctl);
  828. if (ret)
  829. return ret;
  830. }
  831. break;
  832. default:
  833. break;
  834. }
  835. for (j = 0; j < entity->num_controls; j++) {
  836. ret = populate_control(dev, function, entity,
  837. &entity->controls[j], &kctl);
  838. if (ret)
  839. return ret;
  840. }
  841. }
  842. return 0;
  843. }
  844. EXPORT_SYMBOL_NS(sdca_asoc_populate_controls, "SND_SOC_SDCA");
  845. static unsigned int rate_find_mask(unsigned int rate)
  846. {
  847. switch (rate) {
  848. case 0:
  849. return SNDRV_PCM_RATE_8000_768000;
  850. case 5512:
  851. return SNDRV_PCM_RATE_5512;
  852. case 8000:
  853. return SNDRV_PCM_RATE_8000;
  854. case 11025:
  855. return SNDRV_PCM_RATE_11025;
  856. case 16000:
  857. return SNDRV_PCM_RATE_16000;
  858. case 22050:
  859. return SNDRV_PCM_RATE_22050;
  860. case 32000:
  861. return SNDRV_PCM_RATE_32000;
  862. case 44100:
  863. return SNDRV_PCM_RATE_44100;
  864. case 48000:
  865. return SNDRV_PCM_RATE_48000;
  866. case 64000:
  867. return SNDRV_PCM_RATE_64000;
  868. case 88200:
  869. return SNDRV_PCM_RATE_88200;
  870. case 96000:
  871. return SNDRV_PCM_RATE_96000;
  872. case 176400:
  873. return SNDRV_PCM_RATE_176400;
  874. case 192000:
  875. return SNDRV_PCM_RATE_192000;
  876. case 352800:
  877. return SNDRV_PCM_RATE_352800;
  878. case 384000:
  879. return SNDRV_PCM_RATE_384000;
  880. case 705600:
  881. return SNDRV_PCM_RATE_705600;
  882. case 768000:
  883. return SNDRV_PCM_RATE_768000;
  884. case 12000:
  885. return SNDRV_PCM_RATE_12000;
  886. case 24000:
  887. return SNDRV_PCM_RATE_24000;
  888. case 128000:
  889. return SNDRV_PCM_RATE_128000;
  890. default:
  891. return 0;
  892. }
  893. }
  894. static u64 width_find_mask(unsigned int bits)
  895. {
  896. switch (bits) {
  897. case 0:
  898. return SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
  899. SNDRV_PCM_FMTBIT_S20_LE | SNDRV_PCM_FMTBIT_S24_LE |
  900. SNDRV_PCM_FMTBIT_S32_LE;
  901. case 8:
  902. return SNDRV_PCM_FMTBIT_S8;
  903. case 16:
  904. return SNDRV_PCM_FMTBIT_S16_LE;
  905. case 20:
  906. return SNDRV_PCM_FMTBIT_S20_LE;
  907. case 24:
  908. return SNDRV_PCM_FMTBIT_S24_LE;
  909. case 32:
  910. return SNDRV_PCM_FMTBIT_S32_LE;
  911. default:
  912. return 0;
  913. }
  914. }
  915. static int populate_rate_format(struct device *dev,
  916. struct sdca_function_data *function,
  917. struct sdca_entity *entity,
  918. struct snd_soc_pcm_stream *stream)
  919. {
  920. struct sdca_control_range *range;
  921. unsigned int sample_rate, sample_width;
  922. unsigned int clock_rates = 0;
  923. unsigned int rates = 0;
  924. u64 formats = 0;
  925. int sel, i;
  926. switch (entity->type) {
  927. case SDCA_ENTITY_TYPE_IT:
  928. sel = SDCA_CTL_IT_USAGE;
  929. break;
  930. case SDCA_ENTITY_TYPE_OT:
  931. sel = SDCA_CTL_OT_USAGE;
  932. break;
  933. default:
  934. dev_err(dev, "%s: entity type has no usage control\n",
  935. entity->label);
  936. return -EINVAL;
  937. }
  938. if (entity->iot.clock) {
  939. range = sdca_selector_find_range(dev, entity->iot.clock,
  940. SDCA_CTL_CS_SAMPLERATEINDEX,
  941. SDCA_SAMPLERATEINDEX_NCOLS, 0);
  942. if (!range)
  943. return -EINVAL;
  944. for (i = 0; i < range->rows; i++) {
  945. sample_rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
  946. clock_rates |= rate_find_mask(sample_rate);
  947. }
  948. } else {
  949. clock_rates = UINT_MAX;
  950. }
  951. range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
  952. if (!range)
  953. return -EINVAL;
  954. for (i = 0; i < range->rows; i++) {
  955. sample_rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
  956. sample_rate = rate_find_mask(sample_rate);
  957. if (sample_rate & clock_rates) {
  958. rates |= sample_rate;
  959. sample_width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
  960. formats |= width_find_mask(sample_width);
  961. }
  962. }
  963. stream->formats = formats;
  964. stream->rates = rates;
  965. return 0;
  966. }
  967. /**
  968. * sdca_asoc_populate_dais - fill in an array of DAI drivers for a Function
  969. * @dev: Pointer to the device against which allocations will be done.
  970. * @function: Pointer to the Function information.
  971. * @dais: Array of DAI drivers to be populated.
  972. * @ops: DAI ops to be attached to each of the created DAI drivers.
  973. *
  974. * This function populates an array of ASoC DAI drivers from the DisCo
  975. * information for a particular SDCA Function. Typically,
  976. * snd_soc_asoc_count_component will be used to allocate an
  977. * appropriately sized array before calling this function.
  978. *
  979. * Return: Returns zero on success, and a negative error code on failure.
  980. */
  981. int sdca_asoc_populate_dais(struct device *dev, struct sdca_function_data *function,
  982. struct snd_soc_dai_driver *dais,
  983. const struct snd_soc_dai_ops *ops)
  984. {
  985. int i, j;
  986. int ret;
  987. for (i = 0, j = 0; i < function->num_entities - 1; i++) {
  988. struct sdca_entity *entity = &function->entities[i];
  989. struct snd_soc_pcm_stream *stream;
  990. const char *stream_suffix;
  991. switch (entity->type) {
  992. case SDCA_ENTITY_TYPE_IT:
  993. stream = &dais[j].playback;
  994. stream_suffix = "Playback";
  995. break;
  996. case SDCA_ENTITY_TYPE_OT:
  997. stream = &dais[j].capture;
  998. stream_suffix = "Capture";
  999. break;
  1000. default:
  1001. continue;
  1002. }
  1003. /* Can't check earlier as only terminals have an iot member. */
  1004. if (!entity->iot.is_dataport)
  1005. continue;
  1006. stream->stream_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
  1007. entity->label, stream_suffix);
  1008. if (!stream->stream_name)
  1009. return -ENOMEM;
  1010. /* Channels will be further limited by constraints */
  1011. stream->channels_min = 1;
  1012. stream->channels_max = SDCA_MAX_CHANNEL_COUNT;
  1013. ret = populate_rate_format(dev, function, entity, stream);
  1014. if (ret)
  1015. return ret;
  1016. dais[j].id = i;
  1017. dais[j].name = entity->label;
  1018. dais[j].ops = ops;
  1019. j++;
  1020. }
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL_NS(sdca_asoc_populate_dais, "SND_SOC_SDCA");
  1024. /**
  1025. * sdca_asoc_populate_component - fill in a component driver for a Function
  1026. * @dev: Pointer to the device against which allocations will be done.
  1027. * @function: Pointer to the Function information.
  1028. * @component_drv: Pointer to the component driver to be populated.
  1029. * @dai_drv: Pointer to the DAI driver array to be allocated and populated.
  1030. * @num_dai_drv: Pointer to integer that will be populated with the number of
  1031. * DAI drivers.
  1032. * @ops: DAI ops pointer that will be used for each DAI driver.
  1033. *
  1034. * This function populates a snd_soc_component_driver structure based
  1035. * on the DisCo information for a particular SDCA Function. It does
  1036. * all allocation internally.
  1037. *
  1038. * Return: Returns zero on success, and a negative error code on failure.
  1039. */
  1040. int sdca_asoc_populate_component(struct device *dev,
  1041. struct sdca_function_data *function,
  1042. struct snd_soc_component_driver *component_drv,
  1043. struct snd_soc_dai_driver **dai_drv, int *num_dai_drv,
  1044. const struct snd_soc_dai_ops *ops)
  1045. {
  1046. struct snd_soc_dapm_widget *widgets;
  1047. struct snd_soc_dapm_route *routes;
  1048. struct snd_kcontrol_new *controls;
  1049. struct snd_soc_dai_driver *dais;
  1050. int num_widgets, num_routes, num_controls, num_dais;
  1051. int ret;
  1052. ret = sdca_asoc_count_component(dev, function, &num_widgets, &num_routes,
  1053. &num_controls, &num_dais);
  1054. if (ret)
  1055. return ret;
  1056. widgets = devm_kcalloc(dev, num_widgets, sizeof(*widgets), GFP_KERNEL);
  1057. if (!widgets)
  1058. return -ENOMEM;
  1059. routes = devm_kcalloc(dev, num_routes, sizeof(*routes), GFP_KERNEL);
  1060. if (!routes)
  1061. return -ENOMEM;
  1062. controls = devm_kcalloc(dev, num_controls, sizeof(*controls), GFP_KERNEL);
  1063. if (!controls)
  1064. return -ENOMEM;
  1065. dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
  1066. if (!dais)
  1067. return -ENOMEM;
  1068. ret = sdca_asoc_populate_dapm(dev, function, widgets, routes);
  1069. if (ret)
  1070. return ret;
  1071. ret = sdca_asoc_populate_controls(dev, function, controls);
  1072. if (ret)
  1073. return ret;
  1074. ret = sdca_asoc_populate_dais(dev, function, dais, ops);
  1075. if (ret)
  1076. return ret;
  1077. component_drv->dapm_widgets = widgets;
  1078. component_drv->num_dapm_widgets = num_widgets;
  1079. component_drv->dapm_routes = routes;
  1080. component_drv->num_dapm_routes = num_routes;
  1081. component_drv->controls = controls;
  1082. component_drv->num_controls = num_controls;
  1083. *dai_drv = dais;
  1084. *num_dai_drv = num_dais;
  1085. return 0;
  1086. }
  1087. EXPORT_SYMBOL_NS(sdca_asoc_populate_component, "SND_SOC_SDCA");
  1088. /**
  1089. * sdca_asoc_set_constraints - constrain channels available on a DAI
  1090. * @dev: Pointer to the device, used for error messages.
  1091. * @regmap: Pointer to the Function register map.
  1092. * @function: Pointer to the Function information.
  1093. * @substream: Pointer to the PCM substream.
  1094. * @dai: Pointer to the ASoC DAI.
  1095. *
  1096. * Typically called from startup().
  1097. *
  1098. * Return: Returns zero on success, and a negative error code on failure.
  1099. */
  1100. int sdca_asoc_set_constraints(struct device *dev, struct regmap *regmap,
  1101. struct sdca_function_data *function,
  1102. struct snd_pcm_substream *substream,
  1103. struct snd_soc_dai *dai)
  1104. {
  1105. static const unsigned int channel_list[] = {
  1106. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  1107. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  1108. };
  1109. struct sdca_entity *entity = &function->entities[dai->id];
  1110. struct snd_pcm_hw_constraint_list *constraint;
  1111. struct sdca_control_range *range;
  1112. struct sdca_control *control;
  1113. unsigned int channel_mask = 0;
  1114. int i, ret;
  1115. static_assert(ARRAY_SIZE(channel_list) == SDCA_MAX_CHANNEL_COUNT);
  1116. static_assert(sizeof(channel_mask) * BITS_PER_BYTE >= SDCA_MAX_CHANNEL_COUNT);
  1117. if (entity->type != SDCA_ENTITY_TYPE_IT)
  1118. return 0;
  1119. control = sdca_selector_find_control(dev, entity, SDCA_CTL_IT_CLUSTERINDEX);
  1120. if (!control)
  1121. return -EINVAL;
  1122. range = sdca_control_find_range(dev, entity, control, SDCA_CLUSTER_NCOLS, 0);
  1123. if (!range)
  1124. return -EINVAL;
  1125. for (i = 0; i < range->rows; i++) {
  1126. int clusterid = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
  1127. struct sdca_cluster *cluster;
  1128. cluster = sdca_id_find_cluster(dev, function, clusterid);
  1129. if (!cluster)
  1130. return -ENODEV;
  1131. channel_mask |= (1 << (cluster->num_channels - 1));
  1132. }
  1133. dev_dbg(dev, "%s: set channel constraint mask: %#x\n",
  1134. entity->label, channel_mask);
  1135. constraint = kzalloc_obj(*constraint);
  1136. if (!constraint)
  1137. return -ENOMEM;
  1138. constraint->count = ARRAY_SIZE(channel_list);
  1139. constraint->list = channel_list;
  1140. constraint->mask = channel_mask;
  1141. ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
  1142. SNDRV_PCM_HW_PARAM_CHANNELS,
  1143. constraint);
  1144. if (ret) {
  1145. dev_err(dev, "%s: failed to add constraint: %d\n", entity->label, ret);
  1146. kfree(constraint);
  1147. return ret;
  1148. }
  1149. dai->priv = constraint;
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL_NS(sdca_asoc_set_constraints, "SND_SOC_SDCA");
  1153. /**
  1154. * sdca_asoc_free_constraints - free constraint allocations
  1155. * @substream: Pointer to the PCM substream.
  1156. * @dai: Pointer to the ASoC DAI.
  1157. *
  1158. * Typically called from shutdown().
  1159. */
  1160. void sdca_asoc_free_constraints(struct snd_pcm_substream *substream,
  1161. struct snd_soc_dai *dai)
  1162. {
  1163. struct snd_pcm_hw_constraint_list *constraint = dai->priv;
  1164. kfree(constraint);
  1165. }
  1166. EXPORT_SYMBOL_NS(sdca_asoc_free_constraints, "SND_SOC_SDCA");
  1167. /**
  1168. * sdca_asoc_get_port - return SoundWire port for a DAI
  1169. * @dev: Pointer to the device, used for error messages.
  1170. * @regmap: Pointer to the Function register map.
  1171. * @function: Pointer to the Function information.
  1172. * @dai: Pointer to the ASoC DAI.
  1173. *
  1174. * Typically called from hw_params().
  1175. *
  1176. * Return: Returns a positive port number on success, and a negative error
  1177. * code on failure.
  1178. */
  1179. int sdca_asoc_get_port(struct device *dev, struct regmap *regmap,
  1180. struct sdca_function_data *function,
  1181. struct snd_soc_dai *dai)
  1182. {
  1183. struct sdca_entity *entity = &function->entities[dai->id];
  1184. struct sdca_control_range *range;
  1185. unsigned int reg, val;
  1186. int sel = -EINVAL;
  1187. int i, ret;
  1188. switch (entity->type) {
  1189. case SDCA_ENTITY_TYPE_IT:
  1190. sel = SDCA_CTL_IT_DATAPORT_SELECTOR;
  1191. break;
  1192. case SDCA_ENTITY_TYPE_OT:
  1193. sel = SDCA_CTL_OT_DATAPORT_SELECTOR;
  1194. break;
  1195. default:
  1196. break;
  1197. }
  1198. if (sel < 0 || !entity->iot.is_dataport) {
  1199. dev_err(dev, "%s: port number only available for dataports\n",
  1200. entity->label);
  1201. return -EINVAL;
  1202. }
  1203. range = sdca_selector_find_range(dev, entity, sel, SDCA_DATAPORT_SELECTOR_NCOLS,
  1204. SDCA_DATAPORT_SELECTOR_NROWS);
  1205. if (!range)
  1206. return -EINVAL;
  1207. reg = SDW_SDCA_CTL(function->desc->adr, entity->id, sel, 0);
  1208. ret = regmap_read(regmap, reg, &val);
  1209. if (ret) {
  1210. dev_err(dev, "%s: failed to read dataport selector: %d\n",
  1211. entity->label, ret);
  1212. return ret;
  1213. }
  1214. for (i = 0; i < range->rows; i++) {
  1215. static const u8 port_mask = 0xF;
  1216. sel = sdca_range(range, val & port_mask, i);
  1217. /*
  1218. * FIXME: Currently only a single dataport is supported, so
  1219. * return the first one found, technically up to 4 dataports
  1220. * could be linked, but this is not yet supported.
  1221. */
  1222. if (sel != 0xFF)
  1223. return sel;
  1224. val >>= hweight8(port_mask);
  1225. }
  1226. dev_err(dev, "%s: no dataport found\n", entity->label);
  1227. return -ENODEV;
  1228. }
  1229. EXPORT_SYMBOL_NS(sdca_asoc_get_port, "SND_SOC_SDCA");
  1230. static int set_cluster(struct device *dev, struct regmap *regmap,
  1231. struct sdca_function_data *function,
  1232. struct sdca_entity *entity, unsigned int channels)
  1233. {
  1234. int sel = SDCA_CTL_IT_CLUSTERINDEX;
  1235. struct sdca_control_range *range;
  1236. int i, ret;
  1237. range = sdca_selector_find_range(dev, entity, sel, SDCA_CLUSTER_NCOLS, 0);
  1238. if (!range)
  1239. return -EINVAL;
  1240. for (i = 0; i < range->rows; i++) {
  1241. int cluster_id = sdca_range(range, SDCA_CLUSTER_CLUSTERID, i);
  1242. struct sdca_cluster *cluster;
  1243. cluster = sdca_id_find_cluster(dev, function, cluster_id);
  1244. if (!cluster)
  1245. return -ENODEV;
  1246. if (cluster->num_channels == channels) {
  1247. int index = sdca_range(range, SDCA_CLUSTER_BYTEINDEX, i);
  1248. unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
  1249. entity->id, sel, 0);
  1250. ret = regmap_update_bits(regmap, reg, 0xFF, index);
  1251. if (ret) {
  1252. dev_err(dev, "%s: failed to write cluster index: %d\n",
  1253. entity->label, ret);
  1254. return ret;
  1255. }
  1256. dev_dbg(dev, "%s: set cluster to %d (%d channels)\n",
  1257. entity->label, index, channels);
  1258. return 0;
  1259. }
  1260. }
  1261. dev_err(dev, "%s: no cluster for %d channels\n", entity->label, channels);
  1262. return -EINVAL;
  1263. }
  1264. static int set_clock(struct device *dev, struct regmap *regmap,
  1265. struct sdca_function_data *function,
  1266. struct sdca_entity *entity, int target_rate)
  1267. {
  1268. int sel = SDCA_CTL_CS_SAMPLERATEINDEX;
  1269. struct sdca_control_range *range;
  1270. int i, ret;
  1271. range = sdca_selector_find_range(dev, entity, sel, SDCA_SAMPLERATEINDEX_NCOLS, 0);
  1272. if (!range)
  1273. return -EINVAL;
  1274. for (i = 0; i < range->rows; i++) {
  1275. unsigned int rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
  1276. if (rate == target_rate) {
  1277. unsigned int index = sdca_range(range,
  1278. SDCA_SAMPLERATEINDEX_INDEX,
  1279. i);
  1280. unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
  1281. entity->id, sel, 0);
  1282. ret = regmap_update_bits(regmap, reg, 0xFF, index);
  1283. if (ret) {
  1284. dev_err(dev, "%s: failed to write clock rate: %d\n",
  1285. entity->label, ret);
  1286. return ret;
  1287. }
  1288. dev_dbg(dev, "%s: set clock rate to %d (%dHz)\n",
  1289. entity->label, index, rate);
  1290. return 0;
  1291. }
  1292. }
  1293. dev_err(dev, "%s: no clock rate for %dHz\n", entity->label, target_rate);
  1294. return -EINVAL;
  1295. }
  1296. static int set_usage(struct device *dev, struct regmap *regmap,
  1297. struct sdca_function_data *function,
  1298. struct sdca_entity *entity, int sel,
  1299. int target_rate, int target_width)
  1300. {
  1301. struct sdca_control_range *range;
  1302. int i, ret;
  1303. range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
  1304. if (!range)
  1305. return -EINVAL;
  1306. for (i = 0; i < range->rows; i++) {
  1307. unsigned int rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
  1308. unsigned int width = sdca_range(range, SDCA_USAGE_SAMPLE_WIDTH, i);
  1309. if ((!rate || rate == target_rate) && (!width || width == target_width)) {
  1310. unsigned int usage = sdca_range(range, SDCA_USAGE_NUMBER, i);
  1311. unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
  1312. entity->id, sel, 0);
  1313. ret = regmap_update_bits(regmap, reg, 0xFF, usage);
  1314. if (ret) {
  1315. dev_err(dev, "%s: failed to write usage: %d\n",
  1316. entity->label, ret);
  1317. return ret;
  1318. }
  1319. dev_dbg(dev, "%s: set usage to %#x (%dHz, %d bits)\n",
  1320. entity->label, usage, target_rate, target_width);
  1321. return 0;
  1322. }
  1323. }
  1324. dev_err(dev, "%s: no usage for %dHz, %dbits\n",
  1325. entity->label, target_rate, target_width);
  1326. return -EINVAL;
  1327. }
  1328. /**
  1329. * sdca_asoc_hw_params - set SDCA channels, sample rate and bit depth
  1330. * @dev: Pointer to the device, used for error messages.
  1331. * @regmap: Pointer to the Function register map.
  1332. * @function: Pointer to the Function information.
  1333. * @substream: Pointer to the PCM substream.
  1334. * @params: Pointer to the hardware parameters.
  1335. * @dai: Pointer to the ASoC DAI.
  1336. *
  1337. * Typically called from hw_params().
  1338. *
  1339. * Return: Returns zero on success, and a negative error code on failure.
  1340. */
  1341. int sdca_asoc_hw_params(struct device *dev, struct regmap *regmap,
  1342. struct sdca_function_data *function,
  1343. struct snd_pcm_substream *substream,
  1344. struct snd_pcm_hw_params *params,
  1345. struct snd_soc_dai *dai)
  1346. {
  1347. struct sdca_entity *entity = &function->entities[dai->id];
  1348. int channels = params_channels(params);
  1349. int width = params_width(params);
  1350. int rate = params_rate(params);
  1351. int usage_sel;
  1352. int ret;
  1353. switch (entity->type) {
  1354. case SDCA_ENTITY_TYPE_IT:
  1355. ret = set_cluster(dev, regmap, function, entity, channels);
  1356. if (ret)
  1357. return ret;
  1358. usage_sel = SDCA_CTL_IT_USAGE;
  1359. break;
  1360. case SDCA_ENTITY_TYPE_OT:
  1361. usage_sel = SDCA_CTL_OT_USAGE;
  1362. break;
  1363. default:
  1364. dev_err(dev, "%s: hw_params on non-terminal entity\n", entity->label);
  1365. return -EINVAL;
  1366. }
  1367. if (entity->iot.clock) {
  1368. ret = set_clock(dev, regmap, function, entity->iot.clock, rate);
  1369. if (ret)
  1370. return ret;
  1371. }
  1372. ret = set_usage(dev, regmap, function, entity, usage_sel, rate, width);
  1373. if (ret)
  1374. return ret;
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL_NS(sdca_asoc_hw_params, "SND_SOC_SDCA");