axg-card.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/module.h>
  6. #include <linux/of_platform.h>
  7. #include <sound/soc.h>
  8. #include <sound/soc-dai.h>
  9. #include "axg-tdm.h"
  10. struct axg_card {
  11. struct snd_soc_card card;
  12. void **link_data;
  13. };
  14. struct axg_dai_link_tdm_mask {
  15. u32 tx;
  16. u32 rx;
  17. };
  18. struct axg_dai_link_tdm_data {
  19. unsigned int mclk_fs;
  20. unsigned int slots;
  21. unsigned int slot_width;
  22. u32 *tx_mask;
  23. u32 *rx_mask;
  24. struct axg_dai_link_tdm_mask *codec_masks;
  25. };
  26. #define PREFIX "amlogic,"
  27. static int axg_card_reallocate_links(struct axg_card *priv,
  28. unsigned int num_links)
  29. {
  30. struct snd_soc_dai_link *links;
  31. void **ldata;
  32. links = krealloc(priv->card.dai_link,
  33. num_links * sizeof(*priv->card.dai_link),
  34. GFP_KERNEL | __GFP_ZERO);
  35. ldata = krealloc(priv->link_data,
  36. num_links * sizeof(*priv->link_data),
  37. GFP_KERNEL | __GFP_ZERO);
  38. if (!links || !ldata) {
  39. dev_err(priv->card.dev, "failed to allocate links\n");
  40. return -ENOMEM;
  41. }
  42. priv->card.dai_link = links;
  43. priv->link_data = ldata;
  44. priv->card.num_links = num_links;
  45. return 0;
  46. }
  47. static int axg_card_parse_dai(struct snd_soc_card *card,
  48. struct device_node *node,
  49. struct device_node **dai_of_node,
  50. const char **dai_name)
  51. {
  52. struct of_phandle_args args;
  53. int ret;
  54. if (!dai_name || !dai_of_node || !node)
  55. return -EINVAL;
  56. ret = of_parse_phandle_with_args(node, "sound-dai",
  57. "#sound-dai-cells", 0, &args);
  58. if (ret) {
  59. if (ret != -EPROBE_DEFER)
  60. dev_err(card->dev, "can't parse dai %d\n", ret);
  61. return ret;
  62. }
  63. *dai_of_node = args.np;
  64. return snd_soc_get_dai_name(&args, dai_name);
  65. }
  66. static int axg_card_set_link_name(struct snd_soc_card *card,
  67. struct snd_soc_dai_link *link,
  68. const char *prefix)
  69. {
  70. char *name = devm_kasprintf(card->dev, GFP_KERNEL, "%s.%s",
  71. prefix, link->cpu_of_node->full_name);
  72. if (!name)
  73. return -ENOMEM;
  74. link->name = name;
  75. link->stream_name = name;
  76. return 0;
  77. }
  78. static void axg_card_clean_references(struct axg_card *priv)
  79. {
  80. struct snd_soc_card *card = &priv->card;
  81. struct snd_soc_dai_link *link;
  82. int i, j;
  83. if (card->dai_link) {
  84. for (i = 0; i < card->num_links; i++) {
  85. link = &card->dai_link[i];
  86. of_node_put(link->cpu_of_node);
  87. for (j = 0; j < link->num_codecs; j++)
  88. of_node_put(link->codecs[j].of_node);
  89. }
  90. }
  91. if (card->aux_dev) {
  92. for (i = 0; i < card->num_aux_devs; i++)
  93. of_node_put(card->aux_dev[i].codec_of_node);
  94. }
  95. kfree(card->dai_link);
  96. kfree(priv->link_data);
  97. }
  98. static int axg_card_add_aux_devices(struct snd_soc_card *card)
  99. {
  100. struct device_node *node = card->dev->of_node;
  101. struct snd_soc_aux_dev *aux;
  102. int num, i;
  103. num = of_count_phandle_with_args(node, "audio-aux-devs", NULL);
  104. if (num == -ENOENT) {
  105. /*
  106. * It is ok to have no auxiliary devices but for this card it
  107. * is a strange situtation. Let's warn the about it.
  108. */
  109. dev_warn(card->dev, "card has no auxiliary devices\n");
  110. return 0;
  111. } else if (num < 0) {
  112. dev_err(card->dev, "error getting auxiliary devices: %d\n",
  113. num);
  114. return num;
  115. }
  116. aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
  117. if (!aux)
  118. return -ENOMEM;
  119. card->aux_dev = aux;
  120. card->num_aux_devs = num;
  121. for (i = 0; i < card->num_aux_devs; i++, aux++) {
  122. aux->codec_of_node =
  123. of_parse_phandle(node, "audio-aux-devs", i);
  124. if (!aux->codec_of_node)
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
  130. struct snd_pcm_hw_params *params)
  131. {
  132. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  133. struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
  134. struct axg_dai_link_tdm_data *be =
  135. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  136. struct snd_soc_dai *codec_dai;
  137. unsigned int mclk;
  138. int ret, i;
  139. if (be->mclk_fs) {
  140. mclk = params_rate(params) * be->mclk_fs;
  141. for (i = 0; i < rtd->num_codecs; i++) {
  142. codec_dai = rtd->codec_dais[i];
  143. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  144. SND_SOC_CLOCK_IN);
  145. if (ret && ret != -ENOTSUPP)
  146. return ret;
  147. }
  148. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk,
  149. SND_SOC_CLOCK_OUT);
  150. if (ret && ret != -ENOTSUPP)
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. static const struct snd_soc_ops axg_card_tdm_be_ops = {
  156. .hw_params = axg_card_tdm_be_hw_params,
  157. };
  158. static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
  159. {
  160. struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
  161. struct axg_dai_link_tdm_data *be =
  162. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  163. struct snd_soc_dai *codec_dai;
  164. int ret, i;
  165. for (i = 0; i < rtd->num_codecs; i++) {
  166. codec_dai = rtd->codec_dais[i];
  167. ret = snd_soc_dai_set_tdm_slot(codec_dai,
  168. be->codec_masks[i].tx,
  169. be->codec_masks[i].rx,
  170. be->slots, be->slot_width);
  171. if (ret && ret != -ENOTSUPP) {
  172. dev_err(codec_dai->dev,
  173. "setting tdm link slots failed\n");
  174. return ret;
  175. }
  176. }
  177. ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, be->tx_mask, be->rx_mask,
  178. be->slots, be->slot_width);
  179. if (ret) {
  180. dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
  186. {
  187. struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
  188. struct axg_dai_link_tdm_data *be =
  189. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  190. int ret;
  191. /* The loopback rx_mask is the pad tx_mask */
  192. ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, NULL, be->tx_mask,
  193. be->slots, be->slot_width);
  194. if (ret) {
  195. dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
  196. return ret;
  197. }
  198. return 0;
  199. }
  200. static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
  201. int *index)
  202. {
  203. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  204. struct snd_soc_dai_link *pad = &card->dai_link[*index];
  205. struct snd_soc_dai_link *lb;
  206. int ret;
  207. /* extend links */
  208. ret = axg_card_reallocate_links(priv, card->num_links + 1);
  209. if (ret)
  210. return ret;
  211. lb = &card->dai_link[*index + 1];
  212. lb->name = kasprintf(GFP_KERNEL, "%s-lb", pad->name);
  213. if (!lb->name)
  214. return -ENOMEM;
  215. lb->stream_name = lb->name;
  216. lb->cpu_of_node = pad->cpu_of_node;
  217. lb->cpu_dai_name = "TDM Loopback";
  218. lb->codec_name = "snd-soc-dummy";
  219. lb->codec_dai_name = "snd-soc-dummy-dai";
  220. lb->dpcm_capture = 1;
  221. lb->no_pcm = 1;
  222. lb->ops = &axg_card_tdm_be_ops;
  223. lb->init = axg_card_tdm_dai_lb_init;
  224. /* Provide the same link data to the loopback */
  225. priv->link_data[*index + 1] = priv->link_data[*index];
  226. /*
  227. * axg_card_clean_references() will iterate over this link,
  228. * make sure the node count is balanced
  229. */
  230. of_node_get(lb->cpu_of_node);
  231. /* Let add_links continue where it should */
  232. *index += 1;
  233. return 0;
  234. }
  235. static unsigned int axg_card_parse_daifmt(struct device_node *node,
  236. struct device_node *cpu_node)
  237. {
  238. struct device_node *bitclkmaster = NULL;
  239. struct device_node *framemaster = NULL;
  240. unsigned int daifmt;
  241. daifmt = snd_soc_of_parse_daifmt(node, PREFIX,
  242. &bitclkmaster, &framemaster);
  243. daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  244. /* If no master is provided, default to cpu master */
  245. if (!bitclkmaster || bitclkmaster == cpu_node) {
  246. daifmt |= (!framemaster || framemaster == cpu_node) ?
  247. SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBS_CFM;
  248. } else {
  249. daifmt |= (!framemaster || framemaster == cpu_node) ?
  250. SND_SOC_DAIFMT_CBM_CFS : SND_SOC_DAIFMT_CBM_CFM;
  251. }
  252. of_node_put(bitclkmaster);
  253. of_node_put(framemaster);
  254. return daifmt;
  255. }
  256. static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
  257. struct snd_soc_dai_link *link,
  258. struct device_node *node,
  259. struct axg_dai_link_tdm_data *be)
  260. {
  261. char propname[32];
  262. u32 tx, rx;
  263. int i;
  264. be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  265. sizeof(*be->tx_mask), GFP_KERNEL);
  266. be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  267. sizeof(*be->rx_mask), GFP_KERNEL);
  268. if (!be->tx_mask || !be->rx_mask)
  269. return -ENOMEM;
  270. for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
  271. snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
  272. snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
  273. tx = max(tx, be->tx_mask[i]);
  274. }
  275. /* Disable playback is the interface has no tx slots */
  276. if (!tx)
  277. link->dpcm_playback = 0;
  278. for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
  279. snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
  280. snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
  281. rx = max(rx, be->rx_mask[i]);
  282. }
  283. /* Disable capture is the interface has no rx slots */
  284. if (!rx)
  285. link->dpcm_capture = 0;
  286. /* ... but the interface should at least have one of them */
  287. if (!tx && !rx) {
  288. dev_err(card->dev, "tdm link has no cpu slots\n");
  289. return -EINVAL;
  290. }
  291. of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
  292. if (!be->slots) {
  293. /*
  294. * If the slot number is not provided, set it such as it
  295. * accommodates the largest mask
  296. */
  297. be->slots = fls(max(tx, rx));
  298. } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
  299. /*
  300. * Error if the slots can't accommodate the largest mask or
  301. * if it is just too big
  302. */
  303. dev_err(card->dev, "bad slot number\n");
  304. return -EINVAL;
  305. }
  306. of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
  307. return 0;
  308. }
  309. static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
  310. struct snd_soc_dai_link *link,
  311. struct device_node *node,
  312. struct axg_dai_link_tdm_data *be)
  313. {
  314. struct axg_dai_link_tdm_mask *codec_mask;
  315. struct device_node *np;
  316. codec_mask = devm_kcalloc(card->dev, link->num_codecs,
  317. sizeof(*codec_mask), GFP_KERNEL);
  318. if (!codec_mask)
  319. return -ENOMEM;
  320. be->codec_masks = codec_mask;
  321. for_each_child_of_node(node, np) {
  322. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
  323. &codec_mask->rx);
  324. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
  325. &codec_mask->tx);
  326. codec_mask++;
  327. }
  328. return 0;
  329. }
  330. static int axg_card_parse_tdm(struct snd_soc_card *card,
  331. struct device_node *node,
  332. int *index)
  333. {
  334. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  335. struct snd_soc_dai_link *link = &card->dai_link[*index];
  336. struct axg_dai_link_tdm_data *be;
  337. int ret;
  338. /* Allocate tdm link parameters */
  339. be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
  340. if (!be)
  341. return -ENOMEM;
  342. priv->link_data[*index] = be;
  343. /* Setup tdm link */
  344. link->ops = &axg_card_tdm_be_ops;
  345. link->init = axg_card_tdm_dai_init;
  346. link->dai_fmt = axg_card_parse_daifmt(node, link->cpu_of_node);
  347. of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
  348. ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
  349. if (ret) {
  350. dev_err(card->dev, "error parsing tdm link slots\n");
  351. return ret;
  352. }
  353. ret = axg_card_parse_codecs_masks(card, link, node, be);
  354. if (ret)
  355. return ret;
  356. /* Add loopback if the pad dai has playback */
  357. if (link->dpcm_playback) {
  358. ret = axg_card_add_tdm_loopback(card, index);
  359. if (ret)
  360. return ret;
  361. }
  362. return 0;
  363. }
  364. static int axg_card_set_be_link(struct snd_soc_card *card,
  365. struct snd_soc_dai_link *link,
  366. struct device_node *node)
  367. {
  368. struct snd_soc_dai_link_component *codec;
  369. struct device_node *np;
  370. int ret, num_codecs;
  371. link->no_pcm = 1;
  372. link->dpcm_playback = 1;
  373. link->dpcm_capture = 1;
  374. num_codecs = of_get_child_count(node);
  375. if (!num_codecs) {
  376. dev_err(card->dev, "be link %s has no codec\n",
  377. node->full_name);
  378. return -EINVAL;
  379. }
  380. codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL);
  381. if (!codec)
  382. return -ENOMEM;
  383. link->codecs = codec;
  384. link->num_codecs = num_codecs;
  385. for_each_child_of_node(node, np) {
  386. ret = axg_card_parse_dai(card, np, &codec->of_node,
  387. &codec->dai_name);
  388. if (ret) {
  389. of_node_put(np);
  390. return ret;
  391. }
  392. codec++;
  393. }
  394. ret = axg_card_set_link_name(card, link, "be");
  395. if (ret)
  396. dev_err(card->dev, "error setting %s link name\n", np->name);
  397. return ret;
  398. }
  399. static int axg_card_set_fe_link(struct snd_soc_card *card,
  400. struct snd_soc_dai_link *link,
  401. bool is_playback)
  402. {
  403. link->dynamic = 1;
  404. link->dpcm_merged_format = 1;
  405. link->dpcm_merged_chan = 1;
  406. link->dpcm_merged_rate = 1;
  407. link->codec_dai_name = "snd-soc-dummy-dai";
  408. link->codec_name = "snd-soc-dummy";
  409. if (is_playback)
  410. link->dpcm_playback = 1;
  411. else
  412. link->dpcm_capture = 1;
  413. return axg_card_set_link_name(card, link, "fe");
  414. }
  415. static int axg_card_cpu_is_capture_fe(struct device_node *np)
  416. {
  417. return of_device_is_compatible(np, PREFIX "axg-toddr");
  418. }
  419. static int axg_card_cpu_is_playback_fe(struct device_node *np)
  420. {
  421. return of_device_is_compatible(np, PREFIX "axg-frddr");
  422. }
  423. static int axg_card_cpu_is_tdm_iface(struct device_node *np)
  424. {
  425. return of_device_is_compatible(np, PREFIX "axg-tdm-iface");
  426. }
  427. static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
  428. int *index)
  429. {
  430. struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
  431. int ret;
  432. ret = axg_card_parse_dai(card, np, &dai_link->cpu_of_node,
  433. &dai_link->cpu_dai_name);
  434. if (ret)
  435. return ret;
  436. if (axg_card_cpu_is_playback_fe(dai_link->cpu_of_node))
  437. ret = axg_card_set_fe_link(card, dai_link, true);
  438. else if (axg_card_cpu_is_capture_fe(dai_link->cpu_of_node))
  439. ret = axg_card_set_fe_link(card, dai_link, false);
  440. else
  441. ret = axg_card_set_be_link(card, dai_link, np);
  442. if (ret)
  443. return ret;
  444. if (axg_card_cpu_is_tdm_iface(dai_link->cpu_of_node))
  445. ret = axg_card_parse_tdm(card, np, index);
  446. return ret;
  447. }
  448. static int axg_card_add_links(struct snd_soc_card *card)
  449. {
  450. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  451. struct device_node *node = card->dev->of_node;
  452. struct device_node *np;
  453. int num, i, ret;
  454. num = of_get_child_count(node);
  455. if (!num) {
  456. dev_err(card->dev, "card has no links\n");
  457. return -EINVAL;
  458. }
  459. ret = axg_card_reallocate_links(priv, num);
  460. if (ret)
  461. return ret;
  462. i = 0;
  463. for_each_child_of_node(node, np) {
  464. ret = axg_card_add_link(card, np, &i);
  465. if (ret) {
  466. of_node_put(np);
  467. return ret;
  468. }
  469. i++;
  470. }
  471. return 0;
  472. }
  473. static int axg_card_parse_of_optional(struct snd_soc_card *card,
  474. const char *propname,
  475. int (*func)(struct snd_soc_card *c,
  476. const char *p))
  477. {
  478. /* If property is not provided, don't fail ... */
  479. if (!of_property_read_bool(card->dev->of_node, propname))
  480. return 0;
  481. /* ... but do fail if it is provided and the parsing fails */
  482. return func(card, propname);
  483. }
  484. static const struct of_device_id axg_card_of_match[] = {
  485. { .compatible = "amlogic,axg-sound-card", },
  486. {}
  487. };
  488. MODULE_DEVICE_TABLE(of, axg_card_of_match);
  489. static int axg_card_probe(struct platform_device *pdev)
  490. {
  491. struct device *dev = &pdev->dev;
  492. struct axg_card *priv;
  493. int ret;
  494. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  495. if (!priv)
  496. return -ENOMEM;
  497. platform_set_drvdata(pdev, priv);
  498. snd_soc_card_set_drvdata(&priv->card, priv);
  499. priv->card.owner = THIS_MODULE;
  500. priv->card.dev = dev;
  501. ret = snd_soc_of_parse_card_name(&priv->card, "model");
  502. if (ret < 0)
  503. return ret;
  504. ret = axg_card_parse_of_optional(&priv->card, "audio-routing",
  505. snd_soc_of_parse_audio_routing);
  506. if (ret) {
  507. dev_err(dev, "error while parsing routing\n");
  508. return ret;
  509. }
  510. ret = axg_card_parse_of_optional(&priv->card, "audio-widgets",
  511. snd_soc_of_parse_audio_simple_widgets);
  512. if (ret) {
  513. dev_err(dev, "error while parsing widgets\n");
  514. return ret;
  515. }
  516. ret = axg_card_add_links(&priv->card);
  517. if (ret)
  518. goto out_err;
  519. ret = axg_card_add_aux_devices(&priv->card);
  520. if (ret)
  521. goto out_err;
  522. ret = devm_snd_soc_register_card(dev, &priv->card);
  523. if (ret)
  524. goto out_err;
  525. return 0;
  526. out_err:
  527. axg_card_clean_references(priv);
  528. return ret;
  529. }
  530. static int axg_card_remove(struct platform_device *pdev)
  531. {
  532. struct axg_card *priv = platform_get_drvdata(pdev);
  533. axg_card_clean_references(priv);
  534. return 0;
  535. }
  536. static struct platform_driver axg_card_pdrv = {
  537. .probe = axg_card_probe,
  538. .remove = axg_card_remove,
  539. .driver = {
  540. .name = "axg-sound-card",
  541. .of_match_table = axg_card_of_match,
  542. },
  543. };
  544. module_platform_driver(axg_card_pdrv);
  545. MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
  546. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  547. MODULE_LICENSE("GPL v2");