simple-card-utils.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // simple-card-utils.c
  4. //
  5. // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. #include <dt-bindings/sound/audio-graph.h>
  7. #include <linux/cleanup.h>
  8. #include <linux/clk.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_graph.h>
  13. #include <sound/jack.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/simple_card_utils.h>
  16. int simple_util_get_sample_fmt(struct simple_util_data *data)
  17. {
  18. int i;
  19. int val = -EINVAL;
  20. struct {
  21. char *fmt;
  22. u32 val;
  23. } of_sample_fmt_table[] = {
  24. { "s8", SNDRV_PCM_FORMAT_S8},
  25. { "s16_le", SNDRV_PCM_FORMAT_S16_LE},
  26. { "s24_le", SNDRV_PCM_FORMAT_S24_LE},
  27. { "s24_3le", SNDRV_PCM_FORMAT_S24_3LE},
  28. { "s32_le", SNDRV_PCM_FORMAT_S32_LE},
  29. };
  30. for (i = 0; i < ARRAY_SIZE(of_sample_fmt_table); i++) {
  31. if (!strcmp(data->convert_sample_format,
  32. of_sample_fmt_table[i].fmt)) {
  33. val = of_sample_fmt_table[i].val;
  34. break;
  35. }
  36. }
  37. return val;
  38. }
  39. EXPORT_SYMBOL_GPL(simple_util_get_sample_fmt);
  40. static void simple_fixup_sample_fmt(struct simple_util_data *data,
  41. struct snd_pcm_hw_params *params)
  42. {
  43. int val;
  44. struct snd_mask *mask = hw_param_mask(params,
  45. SNDRV_PCM_HW_PARAM_FORMAT);
  46. val = simple_util_get_sample_fmt(data);
  47. if (val >= 0) {
  48. snd_mask_none(mask);
  49. snd_mask_set(mask, val);
  50. }
  51. }
  52. void simple_util_parse_convert(struct device_node *np,
  53. char *prefix,
  54. struct simple_util_data *data)
  55. {
  56. char prop[128];
  57. if (!np)
  58. return;
  59. if (!prefix)
  60. prefix = "";
  61. /* sampling rate convert */
  62. snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
  63. of_property_read_u32(np, prop, &data->convert_rate);
  64. /* channels transfer */
  65. snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
  66. of_property_read_u32(np, prop, &data->convert_channels);
  67. /* convert sample format */
  68. snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-sample-format");
  69. of_property_read_string(np, prop, &data->convert_sample_format);
  70. }
  71. EXPORT_SYMBOL_GPL(simple_util_parse_convert);
  72. /**
  73. * simple_util_is_convert_required() - Query if HW param conversion was requested
  74. * @data: Link data.
  75. *
  76. * Returns true if any HW param conversion was requested for this DAI link with
  77. * any "convert-xxx" properties.
  78. */
  79. bool simple_util_is_convert_required(const struct simple_util_data *data)
  80. {
  81. return data->convert_rate ||
  82. data->convert_channels ||
  83. data->convert_sample_format;
  84. }
  85. EXPORT_SYMBOL_GPL(simple_util_is_convert_required);
  86. int simple_util_parse_daifmt(struct device *dev,
  87. struct device_node *node,
  88. struct device_node *codec,
  89. char *prefix,
  90. unsigned int *retfmt)
  91. {
  92. struct device_node *bitclkmaster = NULL;
  93. struct device_node *framemaster = NULL;
  94. unsigned int daifmt;
  95. daifmt = snd_soc_daifmt_parse_format(node, prefix);
  96. snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
  97. if (!bitclkmaster && !framemaster) {
  98. /*
  99. * No dai-link level and master setting was not found from
  100. * sound node level, revert back to legacy DT parsing and
  101. * take the settings from codec node.
  102. */
  103. dev_dbg(dev, "Revert to legacy daifmt parsing\n");
  104. daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
  105. } else {
  106. daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
  107. ((codec == bitclkmaster) << 4) | (codec == framemaster));
  108. }
  109. of_node_put(bitclkmaster);
  110. of_node_put(framemaster);
  111. *retfmt = daifmt;
  112. return 0;
  113. }
  114. EXPORT_SYMBOL_GPL(simple_util_parse_daifmt);
  115. int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
  116. struct simple_util_dai *dai)
  117. {
  118. int n, i, ret;
  119. u32 *p;
  120. if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
  121. return 0;
  122. n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
  123. if (n % 3) {
  124. dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
  125. return -EINVAL;
  126. }
  127. dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
  128. if (!dai->tdm_width_map)
  129. return -ENOMEM;
  130. u32 *array_values __free(kfree) = kcalloc(n, sizeof(*array_values),
  131. GFP_KERNEL);
  132. if (!array_values)
  133. return -ENOMEM;
  134. ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
  135. if (ret < 0) {
  136. dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
  137. return ret;
  138. }
  139. p = array_values;
  140. for (i = 0; i < n / 3; ++i) {
  141. dai->tdm_width_map[i].sample_bits = *p++;
  142. dai->tdm_width_map[i].slot_width = *p++;
  143. dai->tdm_width_map[i].slot_count = *p++;
  144. }
  145. dai->n_tdm_widths = i;
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(simple_util_parse_tdm_width_map);
  149. int simple_util_set_dailink_name(struct device *dev,
  150. struct snd_soc_dai_link *dai_link,
  151. const char *fmt, ...)
  152. {
  153. va_list ap;
  154. char *name = NULL;
  155. int ret = -ENOMEM;
  156. va_start(ap, fmt);
  157. name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
  158. va_end(ap);
  159. if (name) {
  160. ret = 0;
  161. dai_link->name = name;
  162. dai_link->stream_name = name;
  163. }
  164. return ret;
  165. }
  166. EXPORT_SYMBOL_GPL(simple_util_set_dailink_name);
  167. int simple_util_parse_card_name(struct snd_soc_card *card,
  168. char *prefix)
  169. {
  170. int ret;
  171. if (!prefix)
  172. prefix = "";
  173. /* Parse the card name from DT */
  174. ret = snd_soc_of_parse_card_name(card, "label");
  175. if (ret < 0 || !card->name) {
  176. char prop[128];
  177. snprintf(prop, sizeof(prop), "%sname", prefix);
  178. ret = snd_soc_of_parse_card_name(card, prop);
  179. if (ret < 0)
  180. return ret;
  181. }
  182. if (!card->name && card->dai_link)
  183. card->name = card->dai_link->name;
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(simple_util_parse_card_name);
  187. static int simple_clk_enable(struct simple_util_dai *dai)
  188. {
  189. if (dai)
  190. return clk_prepare_enable(dai->clk);
  191. return 0;
  192. }
  193. static void simple_clk_disable(struct simple_util_dai *dai)
  194. {
  195. if (dai)
  196. clk_disable_unprepare(dai->clk);
  197. }
  198. int simple_util_parse_clk(struct device *dev,
  199. struct device_node *node,
  200. struct simple_util_dai *simple_dai,
  201. struct snd_soc_dai_link_component *dlc)
  202. {
  203. struct clk *clk;
  204. u32 val;
  205. /*
  206. * Parse dai->sysclk come from "clocks = <&xxx>"
  207. * (if system has common clock)
  208. * or "system-clock-frequency = <xxx>"
  209. * or device's module clock.
  210. */
  211. clk = devm_get_clk_from_child(dev, node, NULL);
  212. simple_dai->clk_fixed = of_property_read_bool(
  213. node, "system-clock-fixed");
  214. if (!IS_ERR(clk)) {
  215. simple_dai->sysclk = clk_get_rate(clk);
  216. simple_dai->clk = clk;
  217. } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
  218. simple_dai->sysclk = val;
  219. simple_dai->clk_fixed = true;
  220. } else {
  221. clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
  222. if (!IS_ERR(clk))
  223. simple_dai->sysclk = clk_get_rate(clk);
  224. }
  225. if (of_property_read_bool(node, "system-clock-direction-out"))
  226. simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(simple_util_parse_clk);
  230. static int simple_check_fixed_sysclk(struct device *dev,
  231. struct simple_util_dai *dai,
  232. unsigned int *fixed_sysclk)
  233. {
  234. if (dai->clk_fixed) {
  235. if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
  236. dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
  237. *fixed_sysclk, dai->sysclk);
  238. return -EINVAL;
  239. }
  240. *fixed_sysclk = dai->sysclk;
  241. }
  242. return 0;
  243. }
  244. int simple_util_startup(struct snd_pcm_substream *substream)
  245. {
  246. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  247. struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  248. struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
  249. struct simple_util_dai *dai;
  250. unsigned int fixed_sysclk = 0;
  251. int i1, i2, i;
  252. int ret;
  253. for_each_prop_dai_cpu(props, i1, dai) {
  254. ret = simple_clk_enable(dai);
  255. if (ret)
  256. goto cpu_err;
  257. ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
  258. if (ret)
  259. goto cpu_err;
  260. }
  261. for_each_prop_dai_codec(props, i2, dai) {
  262. ret = simple_clk_enable(dai);
  263. if (ret)
  264. goto codec_err;
  265. ret = simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
  266. if (ret)
  267. goto codec_err;
  268. }
  269. if (fixed_sysclk && props->mclk_fs) {
  270. unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
  271. if (fixed_sysclk % props->mclk_fs) {
  272. dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
  273. fixed_sysclk, props->mclk_fs);
  274. ret = -EINVAL;
  275. goto codec_err;
  276. }
  277. ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
  278. fixed_rate, fixed_rate);
  279. if (ret < 0)
  280. goto codec_err;
  281. }
  282. return 0;
  283. codec_err:
  284. for_each_prop_dai_codec(props, i, dai) {
  285. if (i >= i2)
  286. break;
  287. simple_clk_disable(dai);
  288. }
  289. cpu_err:
  290. for_each_prop_dai_cpu(props, i, dai) {
  291. if (i >= i1)
  292. break;
  293. simple_clk_disable(dai);
  294. }
  295. return ret;
  296. }
  297. EXPORT_SYMBOL_GPL(simple_util_startup);
  298. void simple_util_shutdown(struct snd_pcm_substream *substream)
  299. {
  300. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  301. struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  302. struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
  303. struct simple_util_dai *dai;
  304. int i;
  305. for_each_prop_dai_cpu(props, i, dai) {
  306. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, i);
  307. if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
  308. snd_soc_dai_set_sysclk(cpu_dai,
  309. 0, 0, SND_SOC_CLOCK_OUT);
  310. simple_clk_disable(dai);
  311. }
  312. for_each_prop_dai_codec(props, i, dai) {
  313. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, i);
  314. if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
  315. snd_soc_dai_set_sysclk(codec_dai,
  316. 0, 0, SND_SOC_CLOCK_IN);
  317. simple_clk_disable(dai);
  318. }
  319. }
  320. EXPORT_SYMBOL_GPL(simple_util_shutdown);
  321. static int simple_set_clk_rate(struct device *dev,
  322. struct simple_util_dai *simple_dai,
  323. unsigned long rate)
  324. {
  325. if (!simple_dai)
  326. return 0;
  327. if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
  328. dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
  329. return -EINVAL;
  330. }
  331. if (!simple_dai->clk)
  332. return 0;
  333. if (clk_get_rate(simple_dai->clk) == rate)
  334. return 0;
  335. return clk_set_rate(simple_dai->clk, rate);
  336. }
  337. static int simple_set_tdm(struct snd_soc_dai *dai,
  338. struct simple_util_dai *simple_dai,
  339. struct snd_pcm_hw_params *params)
  340. {
  341. int sample_bits = params_width(params);
  342. int slot_width, slot_count;
  343. int i, ret;
  344. if (!simple_dai || !simple_dai->tdm_width_map)
  345. return 0;
  346. slot_width = simple_dai->slot_width;
  347. slot_count = simple_dai->slots;
  348. if (slot_width == 0)
  349. slot_width = sample_bits;
  350. for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
  351. if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
  352. slot_width = simple_dai->tdm_width_map[i].slot_width;
  353. slot_count = simple_dai->tdm_width_map[i].slot_count;
  354. break;
  355. }
  356. }
  357. ret = snd_soc_dai_set_tdm_slot(dai,
  358. simple_dai->tx_slot_mask,
  359. simple_dai->rx_slot_mask,
  360. slot_count,
  361. slot_width);
  362. if (ret && ret != -ENOTSUPP) {
  363. dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
  364. return ret;
  365. }
  366. return 0;
  367. }
  368. int simple_util_hw_params(struct snd_pcm_substream *substream,
  369. struct snd_pcm_hw_params *params)
  370. {
  371. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  372. struct simple_util_dai *pdai;
  373. struct snd_soc_dai *sdai;
  374. struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  375. struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
  376. unsigned int mclk, mclk_fs = 0;
  377. int i, ret;
  378. if (props->mclk_fs)
  379. mclk_fs = props->mclk_fs;
  380. if (mclk_fs) {
  381. struct snd_soc_component *component;
  382. mclk = params_rate(params) * mclk_fs;
  383. for_each_prop_dai_codec(props, i, pdai) {
  384. ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
  385. if (ret < 0)
  386. return ret;
  387. }
  388. for_each_prop_dai_cpu(props, i, pdai) {
  389. ret = simple_set_clk_rate(rtd->dev, pdai, mclk);
  390. if (ret < 0)
  391. return ret;
  392. }
  393. /* Ensure sysclk is set on all components in case any
  394. * (such as platform components) are missed by calls to
  395. * snd_soc_dai_set_sysclk.
  396. */
  397. for_each_rtd_components(rtd, i, component) {
  398. ret = snd_soc_component_set_sysclk(component, 0, 0,
  399. mclk, SND_SOC_CLOCK_IN);
  400. if (ret && ret != -ENOTSUPP)
  401. return ret;
  402. }
  403. for_each_rtd_codec_dais(rtd, i, sdai) {
  404. ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
  405. if (ret && ret != -ENOTSUPP)
  406. return ret;
  407. }
  408. for_each_rtd_cpu_dais(rtd, i, sdai) {
  409. ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
  410. if (ret && ret != -ENOTSUPP)
  411. return ret;
  412. }
  413. }
  414. for_each_prop_dai_codec(props, i, pdai) {
  415. sdai = snd_soc_rtd_to_codec(rtd, i);
  416. ret = simple_set_tdm(sdai, pdai, params);
  417. if (ret < 0)
  418. return ret;
  419. }
  420. for_each_prop_dai_cpu(props, i, pdai) {
  421. sdai = snd_soc_rtd_to_cpu(rtd, i);
  422. ret = simple_set_tdm(sdai, pdai, params);
  423. if (ret < 0)
  424. return ret;
  425. }
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(simple_util_hw_params);
  429. int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  430. struct snd_pcm_hw_params *params)
  431. {
  432. struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  433. struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
  434. struct simple_util_data *data = &dai_props->adata;
  435. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  436. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  437. if (data->convert_rate)
  438. rate->min =
  439. rate->max = data->convert_rate;
  440. if (data->convert_channels)
  441. channels->min =
  442. channels->max = data->convert_channels;
  443. if (data->convert_sample_format)
  444. simple_fixup_sample_fmt(data, params);
  445. return 0;
  446. }
  447. EXPORT_SYMBOL_GPL(simple_util_be_hw_params_fixup);
  448. static int simple_init_dai(struct snd_soc_dai *dai, struct simple_util_dai *simple_dai)
  449. {
  450. int ret;
  451. if (!simple_dai)
  452. return 0;
  453. if (simple_dai->sysclk) {
  454. ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
  455. simple_dai->clk_direction);
  456. if (ret && ret != -ENOTSUPP) {
  457. dev_err(dai->dev, "simple-card: set_sysclk error\n");
  458. return ret;
  459. }
  460. }
  461. if (simple_dai->slots) {
  462. ret = snd_soc_dai_set_tdm_slot(dai,
  463. simple_dai->tx_slot_mask,
  464. simple_dai->rx_slot_mask,
  465. simple_dai->slots,
  466. simple_dai->slot_width);
  467. if (ret && ret != -ENOTSUPP) {
  468. dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
  469. return ret;
  470. }
  471. }
  472. return 0;
  473. }
  474. static inline int simple_component_is_codec(struct snd_soc_component *component)
  475. {
  476. return component->driver->endianness;
  477. }
  478. static int simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd,
  479. struct simple_dai_props *dai_props)
  480. {
  481. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  482. struct snd_soc_component *component;
  483. struct snd_soc_pcm_stream *c2c_params;
  484. struct snd_pcm_hardware hw;
  485. int i, ret, stream;
  486. /* Do nothing if it already has Codec2Codec settings */
  487. if (dai_link->c2c_params)
  488. return 0;
  489. /* Do nothing if it was DPCM :: BE */
  490. if (dai_link->no_pcm)
  491. return 0;
  492. /* Only Codecs */
  493. for_each_rtd_components(rtd, i, component) {
  494. if (!simple_component_is_codec(component))
  495. return 0;
  496. }
  497. /* Assumes the capabilities are the same for all supported streams */
  498. for_each_pcm_streams(stream) {
  499. ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
  500. if (ret == 0)
  501. break;
  502. }
  503. if (ret < 0) {
  504. dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
  505. return ret;
  506. }
  507. c2c_params = devm_kzalloc(rtd->dev, sizeof(*c2c_params), GFP_KERNEL);
  508. if (!c2c_params)
  509. return -ENOMEM;
  510. c2c_params->formats = hw.formats;
  511. c2c_params->rates = hw.rates;
  512. c2c_params->rate_min = hw.rate_min;
  513. c2c_params->rate_max = hw.rate_max;
  514. c2c_params->channels_min = hw.channels_min;
  515. c2c_params->channels_max = hw.channels_max;
  516. dai_link->c2c_params = c2c_params;
  517. dai_link->num_c2c_params = 1;
  518. return 0;
  519. }
  520. int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd)
  521. {
  522. struct simple_util_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  523. struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
  524. struct simple_util_dai *dai;
  525. int i, ret;
  526. for_each_prop_dai_codec(props, i, dai) {
  527. ret = simple_init_dai(snd_soc_rtd_to_codec(rtd, i), dai);
  528. if (ret < 0)
  529. return ret;
  530. }
  531. for_each_prop_dai_cpu(props, i, dai) {
  532. ret = simple_init_dai(snd_soc_rtd_to_cpu(rtd, i), dai);
  533. if (ret < 0)
  534. return ret;
  535. }
  536. ret = simple_init_for_codec2codec(rtd, props);
  537. if (ret < 0)
  538. return ret;
  539. return 0;
  540. }
  541. EXPORT_SYMBOL_GPL(simple_util_dai_init);
  542. void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
  543. struct snd_soc_dai_link_component *cpus)
  544. {
  545. /*
  546. * Assumes Platform == CPU
  547. *
  548. * Some CPU might be using soc-generic-dmaengine-pcm. This means CPU and Platform
  549. * are different Component, but are sharing same component->dev.
  550. *
  551. * Let's assume Platform is same as CPU if it doesn't identify Platform on DT.
  552. * see
  553. * simple-card.c :: simple_count_noml()
  554. */
  555. if (!platforms->of_node)
  556. snd_soc_dlc_use_cpu_as_platform(platforms, cpus);
  557. }
  558. EXPORT_SYMBOL_GPL(simple_util_canonicalize_platform);
  559. void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
  560. int is_single_links)
  561. {
  562. /*
  563. * In soc_bind_dai_link() will check cpu name after
  564. * of_node matching if dai_link has cpu_dai_name.
  565. * but, it will never match if name was created by
  566. * fmt_single_name() remove cpu_dai_name if cpu_args
  567. * was 0. See:
  568. * fmt_single_name()
  569. * fmt_multiple_name()
  570. */
  571. if (is_single_links)
  572. cpus->dai_name = NULL;
  573. }
  574. EXPORT_SYMBOL_GPL(simple_util_canonicalize_cpu);
  575. void simple_util_clean_reference(struct snd_soc_card *card)
  576. {
  577. struct snd_soc_dai_link *dai_link;
  578. struct snd_soc_dai_link_component *cpu;
  579. struct snd_soc_dai_link_component *codec;
  580. int i, j;
  581. for_each_card_prelinks(card, i, dai_link) {
  582. for_each_link_cpus(dai_link, j, cpu)
  583. of_node_put(cpu->of_node);
  584. for_each_link_codecs(dai_link, j, codec)
  585. of_node_put(codec->of_node);
  586. }
  587. }
  588. EXPORT_SYMBOL_GPL(simple_util_clean_reference);
  589. int simple_util_parse_routing(struct snd_soc_card *card,
  590. char *prefix)
  591. {
  592. struct device_node *node = card->dev->of_node;
  593. char prop[128];
  594. if (!prefix)
  595. prefix = "";
  596. snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
  597. if (!of_property_read_bool(node, prop))
  598. return 0;
  599. return snd_soc_of_parse_audio_routing(card, prop);
  600. }
  601. EXPORT_SYMBOL_GPL(simple_util_parse_routing);
  602. int simple_util_parse_widgets(struct snd_soc_card *card,
  603. char *prefix)
  604. {
  605. struct device_node *node = card->dev->of_node;
  606. char prop[128];
  607. if (!prefix)
  608. prefix = "";
  609. snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
  610. if (of_property_read_bool(node, prop))
  611. return snd_soc_of_parse_audio_simple_widgets(card, prop);
  612. /* no widgets is not error */
  613. return 0;
  614. }
  615. EXPORT_SYMBOL_GPL(simple_util_parse_widgets);
  616. int simple_util_parse_pin_switches(struct snd_soc_card *card,
  617. char *prefix)
  618. {
  619. char prop[128];
  620. if (!prefix)
  621. prefix = "";
  622. snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
  623. return snd_soc_of_parse_pin_switches(card, prop);
  624. }
  625. EXPORT_SYMBOL_GPL(simple_util_parse_pin_switches);
  626. int simple_util_init_jack(struct snd_soc_card *card,
  627. struct simple_util_jack *sjack,
  628. int is_hp, char *prefix,
  629. char *pin)
  630. {
  631. struct device *dev = card->dev;
  632. struct gpio_desc *desc;
  633. char prop[128];
  634. char *pin_name;
  635. char *gpio_name;
  636. int mask;
  637. int error;
  638. if (!prefix)
  639. prefix = "";
  640. if (is_hp) {
  641. snprintf(prop, sizeof(prop), "%shp-det", prefix);
  642. pin_name = pin ? pin : "Headphones";
  643. gpio_name = "Headphone detection";
  644. mask = SND_JACK_HEADPHONE;
  645. } else {
  646. snprintf(prop, sizeof(prop), "%smic-det", prefix);
  647. pin_name = pin ? pin : "Mic Jack";
  648. gpio_name = "Mic detection";
  649. mask = SND_JACK_MICROPHONE;
  650. }
  651. desc = gpiod_get_optional(dev, prop, GPIOD_IN);
  652. error = PTR_ERR_OR_ZERO(desc);
  653. if (error)
  654. return error;
  655. if (desc) {
  656. error = gpiod_set_consumer_name(desc, gpio_name);
  657. if (error)
  658. return error;
  659. sjack->pin.pin = pin_name;
  660. sjack->pin.mask = mask;
  661. sjack->gpio.name = gpio_name;
  662. sjack->gpio.report = mask;
  663. sjack->gpio.desc = desc;
  664. sjack->gpio.debounce_time = 150;
  665. snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack,
  666. &sjack->pin, 1);
  667. snd_soc_jack_add_gpios(&sjack->jack, 1, &sjack->gpio);
  668. }
  669. return 0;
  670. }
  671. EXPORT_SYMBOL_GPL(simple_util_init_jack);
  672. int simple_util_init_aux_jacks(struct simple_util_priv *priv, char *prefix)
  673. {
  674. struct snd_soc_card *card = simple_priv_to_card(priv);
  675. struct snd_soc_component *component;
  676. int found_jack_index = 0;
  677. int type = 0;
  678. int num = 0;
  679. int ret;
  680. if (priv->aux_jacks)
  681. return 0;
  682. for_each_card_auxs(card, component) {
  683. type = snd_soc_component_get_jack_type(component);
  684. if (type > 0)
  685. num++;
  686. }
  687. if (num < 1)
  688. return 0;
  689. priv->aux_jacks = devm_kcalloc(card->dev, num,
  690. sizeof(struct snd_soc_jack), GFP_KERNEL);
  691. if (!priv->aux_jacks)
  692. return -ENOMEM;
  693. for_each_card_auxs(card, component) {
  694. char id[128];
  695. struct snd_soc_jack *jack;
  696. if (found_jack_index >= num)
  697. break;
  698. type = snd_soc_component_get_jack_type(component);
  699. if (type <= 0)
  700. continue;
  701. /* create jack */
  702. jack = &(priv->aux_jacks[found_jack_index++]);
  703. snprintf(id, sizeof(id), "%s-jack", component->name);
  704. ret = snd_soc_card_jack_new(card, id, type, jack);
  705. if (ret)
  706. continue;
  707. (void)snd_soc_component_set_jack(component, jack, NULL);
  708. }
  709. return 0;
  710. }
  711. EXPORT_SYMBOL_GPL(simple_util_init_aux_jacks);
  712. int simple_util_init_priv(struct simple_util_priv *priv,
  713. struct link_info *li)
  714. {
  715. struct snd_soc_card *card = simple_priv_to_card(priv);
  716. struct device *dev = simple_priv_to_dev(priv);
  717. struct snd_soc_dai_link *dai_link;
  718. struct simple_dai_props *dai_props;
  719. struct simple_util_dai *dais;
  720. struct snd_soc_dai_link_component *dlcs;
  721. struct snd_soc_codec_conf *cconf = NULL;
  722. int i, dai_num = 0, dlc_num = 0, cnf_num = 0;
  723. dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
  724. dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL);
  725. if (!dai_props || !dai_link)
  726. return -ENOMEM;
  727. /*
  728. * dais (= CPU+Codec)
  729. * dlcs (= CPU+Codec+Platform)
  730. */
  731. for (i = 0; i < li->link; i++) {
  732. int cc = li->num[i].cpus + li->num[i].codecs;
  733. dai_num += cc;
  734. dlc_num += cc + li->num[i].platforms;
  735. if (!li->num[i].cpus)
  736. cnf_num += li->num[i].codecs;
  737. }
  738. dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
  739. dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
  740. if (!dais || !dlcs)
  741. return -ENOMEM;
  742. if (cnf_num) {
  743. cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
  744. if (!cconf)
  745. return -ENOMEM;
  746. }
  747. dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
  748. li->link, dai_num, cnf_num);
  749. priv->dai_props = dai_props;
  750. priv->dai_link = dai_link;
  751. priv->dais = dais;
  752. priv->dlcs = dlcs;
  753. priv->codec_conf = cconf;
  754. card->dai_link = priv->dai_link;
  755. card->num_links = li->link;
  756. card->codec_conf = cconf;
  757. card->num_configs = cnf_num;
  758. for (i = 0; i < li->link; i++) {
  759. if (li->num[i].cpus) {
  760. /* Normal CPU */
  761. dai_link[i].cpus = dlcs;
  762. dai_props[i].num.cpus =
  763. dai_link[i].num_cpus = li->num[i].cpus;
  764. dai_props[i].cpu_dai = dais;
  765. dlcs += li->num[i].cpus;
  766. dais += li->num[i].cpus;
  767. } else {
  768. /* DPCM Be's CPU = dummy */
  769. dai_link[i].cpus = &snd_soc_dummy_dlc;
  770. dai_props[i].num.cpus =
  771. dai_link[i].num_cpus = 1;
  772. }
  773. if (li->num[i].codecs) {
  774. /* Normal Codec */
  775. dai_link[i].codecs = dlcs;
  776. dai_props[i].num.codecs =
  777. dai_link[i].num_codecs = li->num[i].codecs;
  778. dai_props[i].codec_dai = dais;
  779. dlcs += li->num[i].codecs;
  780. dais += li->num[i].codecs;
  781. if (!li->num[i].cpus) {
  782. /* DPCM Be's Codec */
  783. dai_props[i].codec_conf = cconf;
  784. cconf += li->num[i].codecs;
  785. }
  786. } else {
  787. /* DPCM Fe's Codec = dummy */
  788. dai_link[i].codecs = &snd_soc_dummy_dlc;
  789. dai_props[i].num.codecs =
  790. dai_link[i].num_codecs = 1;
  791. }
  792. if (li->num[i].platforms) {
  793. /* Have Platform */
  794. dai_link[i].platforms = dlcs;
  795. dai_props[i].num.platforms =
  796. dai_link[i].num_platforms = li->num[i].platforms;
  797. dlcs += li->num[i].platforms;
  798. } else {
  799. /* Doesn't have Platform */
  800. dai_link[i].platforms = NULL;
  801. dai_props[i].num.platforms =
  802. dai_link[i].num_platforms = 0;
  803. }
  804. }
  805. return 0;
  806. }
  807. EXPORT_SYMBOL_GPL(simple_util_init_priv);
  808. void simple_util_remove(struct platform_device *pdev)
  809. {
  810. struct snd_soc_card *card = platform_get_drvdata(pdev);
  811. simple_util_clean_reference(card);
  812. }
  813. EXPORT_SYMBOL_GPL(simple_util_remove);
  814. int graph_util_card_probe(struct snd_soc_card *card)
  815. {
  816. struct simple_util_priv *priv = snd_soc_card_get_drvdata(card);
  817. int ret;
  818. ret = simple_util_init_hp(card, &priv->hp_jack, NULL);
  819. if (ret < 0)
  820. return ret;
  821. ret = simple_util_init_mic(card, &priv->mic_jack, NULL);
  822. if (ret < 0)
  823. return ret;
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(graph_util_card_probe);
  827. int graph_util_is_ports0(struct device_node *np)
  828. {
  829. struct device_node *port, *ports, *ports0, *top;
  830. int ret;
  831. /* np is "endpoint" or "port" */
  832. if (of_node_name_eq(np, "endpoint")) {
  833. port = of_get_parent(np);
  834. } else {
  835. port = np;
  836. of_node_get(port);
  837. }
  838. ports = of_get_parent(port);
  839. top = of_get_parent(ports);
  840. ports0 = of_get_child_by_name(top, "ports");
  841. ret = ports0 == ports;
  842. of_node_put(port);
  843. of_node_put(ports);
  844. of_node_put(ports0);
  845. of_node_put(top);
  846. return ret;
  847. }
  848. EXPORT_SYMBOL_GPL(graph_util_is_ports0);
  849. static int graph_get_dai_id(struct device_node *ep)
  850. {
  851. struct device_node *node;
  852. struct device_node *endpoint;
  853. struct of_endpoint info;
  854. int i, id;
  855. int ret;
  856. /* use driver specified DAI ID if exist */
  857. ret = snd_soc_get_dai_id(ep);
  858. if (ret != -ENOTSUPP)
  859. return ret;
  860. /* use endpoint/port reg if exist */
  861. ret = of_graph_parse_endpoint(ep, &info);
  862. if (ret == 0) {
  863. /*
  864. * Because it will count port/endpoint if it doesn't have "reg".
  865. * But, we can't judge whether it has "no reg", or "reg = <0>"
  866. * only of_graph_parse_endpoint().
  867. * We need to check "reg" property
  868. */
  869. if (of_property_present(ep, "reg"))
  870. return info.id;
  871. node = of_get_parent(ep);
  872. ret = of_property_present(node, "reg");
  873. of_node_put(node);
  874. if (ret)
  875. return info.port;
  876. }
  877. node = of_graph_get_port_parent(ep);
  878. /*
  879. * Non HDMI sound case, counting port/endpoint on its DT
  880. * is enough. Let's count it.
  881. */
  882. i = 0;
  883. id = -1;
  884. for_each_endpoint_of_node(node, endpoint) {
  885. if (endpoint == ep)
  886. id = i;
  887. i++;
  888. }
  889. of_node_put(node);
  890. if (id < 0)
  891. return -ENODEV;
  892. return id;
  893. }
  894. int graph_util_parse_dai(struct device *dev, struct device_node *ep,
  895. struct snd_soc_dai_link_component *dlc, int *is_single_link)
  896. {
  897. struct device_node *node;
  898. struct of_phandle_args args = {};
  899. struct snd_soc_dai *dai;
  900. int ret;
  901. if (!ep)
  902. return 0;
  903. node = of_graph_get_port_parent(ep);
  904. /*
  905. * Try to find from DAI node
  906. */
  907. args.np = ep;
  908. dai = snd_soc_get_dai_via_args(&args);
  909. if (dai) {
  910. dlc->of_node = node;
  911. dlc->dai_name = snd_soc_dai_name_get(dai);
  912. dlc->dai_args = snd_soc_copy_dai_args(dev, &args);
  913. if (!dlc->dai_args)
  914. return -ENOMEM;
  915. goto parse_dai_end;
  916. }
  917. /* Get dai->name */
  918. args.np = node;
  919. args.args[0] = graph_get_dai_id(ep);
  920. args.args_count = (of_graph_get_endpoint_count(node) > 1);
  921. /*
  922. * FIXME
  923. *
  924. * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
  925. * If user unbinded CPU or Codec driver, but not for Sound Card,
  926. * dlc->dai_name is keeping unbinded CPU or Codec
  927. * driver's pointer.
  928. *
  929. * If user re-bind CPU or Codec driver again, ALSA SoC will try
  930. * to rebind Card via snd_soc_try_rebind_card(), but because of
  931. * above reason, it might can't bind Sound Card.
  932. * Because Sound Card is pointing to released dai_name pointer.
  933. *
  934. * To avoid this rebind Card issue,
  935. * 1) It needs to alloc memory to keep dai_name eventhough
  936. * CPU or Codec driver was unbinded, or
  937. * 2) user need to rebind Sound Card everytime
  938. * if he unbinded CPU or Codec.
  939. */
  940. ret = snd_soc_get_dlc(&args, dlc);
  941. if (ret < 0) {
  942. of_node_put(node);
  943. return ret;
  944. }
  945. parse_dai_end:
  946. if (is_single_link)
  947. *is_single_link = of_graph_get_endpoint_count(node) == 1;
  948. return 0;
  949. }
  950. EXPORT_SYMBOL_GPL(graph_util_parse_dai);
  951. void graph_util_parse_link_direction(struct device_node *np,
  952. bool *playback_only, bool *capture_only)
  953. {
  954. bool is_playback_only = of_property_read_bool(np, "playback-only");
  955. bool is_capture_only = of_property_read_bool(np, "capture-only");
  956. if (playback_only)
  957. *playback_only = is_playback_only;
  958. if (capture_only)
  959. *capture_only = is_capture_only;
  960. }
  961. EXPORT_SYMBOL_GPL(graph_util_parse_link_direction);
  962. static enum snd_soc_trigger_order
  963. __graph_util_parse_trigger_order(struct simple_util_priv *priv,
  964. struct device_node *np,
  965. const char *prop)
  966. {
  967. u32 val[SND_SOC_TRIGGER_SIZE];
  968. int ret;
  969. ret = of_property_read_u32_array(np, prop, val, SND_SOC_TRIGGER_SIZE);
  970. if (ret == 0) {
  971. struct device *dev = simple_priv_to_dev(priv);
  972. u32 order = (val[0] << 8) +
  973. (val[1] << 4) +
  974. (val[2]);
  975. switch (order) {
  976. case (SND_SOC_TRIGGER_LINK << 8) +
  977. (SND_SOC_TRIGGER_COMPONENT << 4) +
  978. (SND_SOC_TRIGGER_DAI):
  979. return SND_SOC_TRIGGER_ORDER_DEFAULT;
  980. case (SND_SOC_TRIGGER_LINK << 8) +
  981. (SND_SOC_TRIGGER_DAI << 4) +
  982. (SND_SOC_TRIGGER_COMPONENT):
  983. return SND_SOC_TRIGGER_ORDER_LDC;
  984. default:
  985. dev_err(dev, "unsupported trigger order [0x%x]\n", order);
  986. }
  987. }
  988. /* SND_SOC_TRIGGER_ORDER_MAX means error */
  989. return SND_SOC_TRIGGER_ORDER_MAX;
  990. }
  991. void graph_util_parse_trigger_order(struct simple_util_priv *priv,
  992. struct device_node *np,
  993. enum snd_soc_trigger_order *trigger_start,
  994. enum snd_soc_trigger_order *trigger_stop)
  995. {
  996. static enum snd_soc_trigger_order order;
  997. /*
  998. * We can use it like below
  999. *
  1000. * #include <dt-bindings/sound/audio-graph.h>
  1001. *
  1002. * link-trigger-order = <SND_SOC_TRIGGER_LINK
  1003. * SND_SOC_TRIGGER_COMPONENT
  1004. * SND_SOC_TRIGGER_DAI>;
  1005. */
  1006. order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order");
  1007. if (order < SND_SOC_TRIGGER_ORDER_MAX) {
  1008. *trigger_start = order;
  1009. *trigger_stop = order;
  1010. }
  1011. order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order-start");
  1012. if (order < SND_SOC_TRIGGER_ORDER_MAX)
  1013. *trigger_start = order;
  1014. order = __graph_util_parse_trigger_order(priv, np, "link-trigger-order-stop");
  1015. if (order < SND_SOC_TRIGGER_ORDER_MAX)
  1016. *trigger_stop = order;
  1017. return;
  1018. }
  1019. EXPORT_SYMBOL_GPL(graph_util_parse_trigger_order);
  1020. /* Module information */
  1021. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  1022. MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
  1023. MODULE_LICENSE("GPL v2");