axg-card.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. struct snd_soc_dai_link_component *codec;
  83. int i, j;
  84. if (card->dai_link) {
  85. for_each_card_prelinks(card, i, link) {
  86. of_node_put(link->cpu_of_node);
  87. for_each_link_codecs(link, j, codec)
  88. of_node_put(codec->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_each_rtd_codec_dai(rtd, i, codec_dai) {
  142. ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
  143. SND_SOC_CLOCK_IN);
  144. if (ret && ret != -ENOTSUPP)
  145. return ret;
  146. }
  147. ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0, mclk,
  148. SND_SOC_CLOCK_OUT);
  149. if (ret && ret != -ENOTSUPP)
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static const struct snd_soc_ops axg_card_tdm_be_ops = {
  155. .hw_params = axg_card_tdm_be_hw_params,
  156. };
  157. static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
  158. {
  159. struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
  160. struct axg_dai_link_tdm_data *be =
  161. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  162. struct snd_soc_dai *codec_dai;
  163. int ret, i;
  164. for_each_rtd_codec_dai(rtd, i, codec_dai) {
  165. ret = snd_soc_dai_set_tdm_slot(codec_dai,
  166. be->codec_masks[i].tx,
  167. be->codec_masks[i].rx,
  168. be->slots, be->slot_width);
  169. if (ret && ret != -ENOTSUPP) {
  170. dev_err(codec_dai->dev,
  171. "setting tdm link slots failed\n");
  172. return ret;
  173. }
  174. }
  175. ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, be->tx_mask, be->rx_mask,
  176. be->slots, be->slot_width);
  177. if (ret) {
  178. dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
  184. {
  185. struct axg_card *priv = snd_soc_card_get_drvdata(rtd->card);
  186. struct axg_dai_link_tdm_data *be =
  187. (struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
  188. int ret;
  189. /* The loopback rx_mask is the pad tx_mask */
  190. ret = axg_tdm_set_tdm_slots(rtd->cpu_dai, NULL, be->tx_mask,
  191. be->slots, be->slot_width);
  192. if (ret) {
  193. dev_err(rtd->cpu_dai->dev, "setting tdm link slots failed\n");
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
  199. int *index)
  200. {
  201. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  202. struct snd_soc_dai_link *pad = &card->dai_link[*index];
  203. struct snd_soc_dai_link *lb;
  204. int ret;
  205. /* extend links */
  206. ret = axg_card_reallocate_links(priv, card->num_links + 1);
  207. if (ret)
  208. return ret;
  209. lb = &card->dai_link[*index + 1];
  210. lb->name = kasprintf(GFP_KERNEL, "%s-lb", pad->name);
  211. if (!lb->name)
  212. return -ENOMEM;
  213. lb->stream_name = lb->name;
  214. lb->cpu_of_node = pad->cpu_of_node;
  215. lb->cpu_dai_name = "TDM Loopback";
  216. lb->codec_name = "snd-soc-dummy";
  217. lb->codec_dai_name = "snd-soc-dummy-dai";
  218. lb->dpcm_capture = 1;
  219. lb->no_pcm = 1;
  220. lb->ops = &axg_card_tdm_be_ops;
  221. lb->init = axg_card_tdm_dai_lb_init;
  222. /* Provide the same link data to the loopback */
  223. priv->link_data[*index + 1] = priv->link_data[*index];
  224. /*
  225. * axg_card_clean_references() will iterate over this link,
  226. * make sure the node count is balanced
  227. */
  228. of_node_get(lb->cpu_of_node);
  229. /* Let add_links continue where it should */
  230. *index += 1;
  231. return 0;
  232. }
  233. static unsigned int axg_card_parse_daifmt(struct device_node *node,
  234. struct device_node *cpu_node)
  235. {
  236. struct device_node *bitclkmaster = NULL;
  237. struct device_node *framemaster = NULL;
  238. unsigned int daifmt;
  239. daifmt = snd_soc_of_parse_daifmt(node, PREFIX,
  240. &bitclkmaster, &framemaster);
  241. daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  242. /* If no master is provided, default to cpu master */
  243. if (!bitclkmaster || bitclkmaster == cpu_node) {
  244. daifmt |= (!framemaster || framemaster == cpu_node) ?
  245. SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBS_CFM;
  246. } else {
  247. daifmt |= (!framemaster || framemaster == cpu_node) ?
  248. SND_SOC_DAIFMT_CBM_CFS : SND_SOC_DAIFMT_CBM_CFM;
  249. }
  250. of_node_put(bitclkmaster);
  251. of_node_put(framemaster);
  252. return daifmt;
  253. }
  254. static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
  255. struct snd_soc_dai_link *link,
  256. struct device_node *node,
  257. struct axg_dai_link_tdm_data *be)
  258. {
  259. char propname[32];
  260. u32 tx, rx;
  261. int i;
  262. be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  263. sizeof(*be->tx_mask), GFP_KERNEL);
  264. be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
  265. sizeof(*be->rx_mask), GFP_KERNEL);
  266. if (!be->tx_mask || !be->rx_mask)
  267. return -ENOMEM;
  268. for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
  269. snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
  270. snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
  271. tx = max(tx, be->tx_mask[i]);
  272. }
  273. /* Disable playback is the interface has no tx slots */
  274. if (!tx)
  275. link->dpcm_playback = 0;
  276. for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
  277. snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
  278. snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
  279. rx = max(rx, be->rx_mask[i]);
  280. }
  281. /* Disable capture is the interface has no rx slots */
  282. if (!rx)
  283. link->dpcm_capture = 0;
  284. /* ... but the interface should at least have one of them */
  285. if (!tx && !rx) {
  286. dev_err(card->dev, "tdm link has no cpu slots\n");
  287. return -EINVAL;
  288. }
  289. of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
  290. if (!be->slots) {
  291. /*
  292. * If the slot number is not provided, set it such as it
  293. * accommodates the largest mask
  294. */
  295. be->slots = fls(max(tx, rx));
  296. } else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
  297. /*
  298. * Error if the slots can't accommodate the largest mask or
  299. * if it is just too big
  300. */
  301. dev_err(card->dev, "bad slot number\n");
  302. return -EINVAL;
  303. }
  304. of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
  305. return 0;
  306. }
  307. static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
  308. struct snd_soc_dai_link *link,
  309. struct device_node *node,
  310. struct axg_dai_link_tdm_data *be)
  311. {
  312. struct axg_dai_link_tdm_mask *codec_mask;
  313. struct device_node *np;
  314. codec_mask = devm_kcalloc(card->dev, link->num_codecs,
  315. sizeof(*codec_mask), GFP_KERNEL);
  316. if (!codec_mask)
  317. return -ENOMEM;
  318. be->codec_masks = codec_mask;
  319. for_each_child_of_node(node, np) {
  320. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
  321. &codec_mask->rx);
  322. snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
  323. &codec_mask->tx);
  324. codec_mask++;
  325. }
  326. return 0;
  327. }
  328. static int axg_card_parse_tdm(struct snd_soc_card *card,
  329. struct device_node *node,
  330. int *index)
  331. {
  332. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  333. struct snd_soc_dai_link *link = &card->dai_link[*index];
  334. struct axg_dai_link_tdm_data *be;
  335. int ret;
  336. /* Allocate tdm link parameters */
  337. be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
  338. if (!be)
  339. return -ENOMEM;
  340. priv->link_data[*index] = be;
  341. /* Setup tdm link */
  342. link->ops = &axg_card_tdm_be_ops;
  343. link->init = axg_card_tdm_dai_init;
  344. link->dai_fmt = axg_card_parse_daifmt(node, link->cpu_of_node);
  345. of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
  346. ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
  347. if (ret) {
  348. dev_err(card->dev, "error parsing tdm link slots\n");
  349. return ret;
  350. }
  351. ret = axg_card_parse_codecs_masks(card, link, node, be);
  352. if (ret)
  353. return ret;
  354. /* Add loopback if the pad dai has playback */
  355. if (link->dpcm_playback) {
  356. ret = axg_card_add_tdm_loopback(card, index);
  357. if (ret)
  358. return ret;
  359. }
  360. return 0;
  361. }
  362. static int axg_card_set_be_link(struct snd_soc_card *card,
  363. struct snd_soc_dai_link *link,
  364. struct device_node *node)
  365. {
  366. struct snd_soc_dai_link_component *codec;
  367. struct device_node *np;
  368. int ret, num_codecs;
  369. link->no_pcm = 1;
  370. link->dpcm_playback = 1;
  371. link->dpcm_capture = 1;
  372. num_codecs = of_get_child_count(node);
  373. if (!num_codecs) {
  374. dev_err(card->dev, "be link %s has no codec\n",
  375. node->full_name);
  376. return -EINVAL;
  377. }
  378. codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL);
  379. if (!codec)
  380. return -ENOMEM;
  381. link->codecs = codec;
  382. link->num_codecs = num_codecs;
  383. for_each_child_of_node(node, np) {
  384. ret = axg_card_parse_dai(card, np, &codec->of_node,
  385. &codec->dai_name);
  386. if (ret) {
  387. of_node_put(np);
  388. return ret;
  389. }
  390. codec++;
  391. }
  392. ret = axg_card_set_link_name(card, link, "be");
  393. if (ret)
  394. dev_err(card->dev, "error setting %pOFn link name\n", np);
  395. return ret;
  396. }
  397. static int axg_card_set_fe_link(struct snd_soc_card *card,
  398. struct snd_soc_dai_link *link,
  399. bool is_playback)
  400. {
  401. link->dynamic = 1;
  402. link->dpcm_merged_format = 1;
  403. link->dpcm_merged_chan = 1;
  404. link->dpcm_merged_rate = 1;
  405. link->codec_dai_name = "snd-soc-dummy-dai";
  406. link->codec_name = "snd-soc-dummy";
  407. if (is_playback)
  408. link->dpcm_playback = 1;
  409. else
  410. link->dpcm_capture = 1;
  411. return axg_card_set_link_name(card, link, "fe");
  412. }
  413. static int axg_card_cpu_is_capture_fe(struct device_node *np)
  414. {
  415. return of_device_is_compatible(np, PREFIX "axg-toddr");
  416. }
  417. static int axg_card_cpu_is_playback_fe(struct device_node *np)
  418. {
  419. return of_device_is_compatible(np, PREFIX "axg-frddr");
  420. }
  421. static int axg_card_cpu_is_tdm_iface(struct device_node *np)
  422. {
  423. return of_device_is_compatible(np, PREFIX "axg-tdm-iface");
  424. }
  425. static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
  426. int *index)
  427. {
  428. struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
  429. int ret;
  430. ret = axg_card_parse_dai(card, np, &dai_link->cpu_of_node,
  431. &dai_link->cpu_dai_name);
  432. if (ret)
  433. return ret;
  434. if (axg_card_cpu_is_playback_fe(dai_link->cpu_of_node))
  435. ret = axg_card_set_fe_link(card, dai_link, true);
  436. else if (axg_card_cpu_is_capture_fe(dai_link->cpu_of_node))
  437. ret = axg_card_set_fe_link(card, dai_link, false);
  438. else
  439. ret = axg_card_set_be_link(card, dai_link, np);
  440. if (ret)
  441. return ret;
  442. if (axg_card_cpu_is_tdm_iface(dai_link->cpu_of_node))
  443. ret = axg_card_parse_tdm(card, np, index);
  444. return ret;
  445. }
  446. static int axg_card_add_links(struct snd_soc_card *card)
  447. {
  448. struct axg_card *priv = snd_soc_card_get_drvdata(card);
  449. struct device_node *node = card->dev->of_node;
  450. struct device_node *np;
  451. int num, i, ret;
  452. num = of_get_child_count(node);
  453. if (!num) {
  454. dev_err(card->dev, "card has no links\n");
  455. return -EINVAL;
  456. }
  457. ret = axg_card_reallocate_links(priv, num);
  458. if (ret)
  459. return ret;
  460. i = 0;
  461. for_each_child_of_node(node, np) {
  462. ret = axg_card_add_link(card, np, &i);
  463. if (ret) {
  464. of_node_put(np);
  465. return ret;
  466. }
  467. i++;
  468. }
  469. return 0;
  470. }
  471. static int axg_card_parse_of_optional(struct snd_soc_card *card,
  472. const char *propname,
  473. int (*func)(struct snd_soc_card *c,
  474. const char *p))
  475. {
  476. /* If property is not provided, don't fail ... */
  477. if (!of_property_read_bool(card->dev->of_node, propname))
  478. return 0;
  479. /* ... but do fail if it is provided and the parsing fails */
  480. return func(card, propname);
  481. }
  482. static const struct of_device_id axg_card_of_match[] = {
  483. { .compatible = "amlogic,axg-sound-card", },
  484. {}
  485. };
  486. MODULE_DEVICE_TABLE(of, axg_card_of_match);
  487. static int axg_card_probe(struct platform_device *pdev)
  488. {
  489. struct device *dev = &pdev->dev;
  490. struct axg_card *priv;
  491. int ret;
  492. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  493. if (!priv)
  494. return -ENOMEM;
  495. platform_set_drvdata(pdev, priv);
  496. snd_soc_card_set_drvdata(&priv->card, priv);
  497. priv->card.owner = THIS_MODULE;
  498. priv->card.dev = dev;
  499. ret = snd_soc_of_parse_card_name(&priv->card, "model");
  500. if (ret < 0)
  501. return ret;
  502. ret = axg_card_parse_of_optional(&priv->card, "audio-routing",
  503. snd_soc_of_parse_audio_routing);
  504. if (ret) {
  505. dev_err(dev, "error while parsing routing\n");
  506. return ret;
  507. }
  508. ret = axg_card_parse_of_optional(&priv->card, "audio-widgets",
  509. snd_soc_of_parse_audio_simple_widgets);
  510. if (ret) {
  511. dev_err(dev, "error while parsing widgets\n");
  512. return ret;
  513. }
  514. ret = axg_card_add_links(&priv->card);
  515. if (ret)
  516. goto out_err;
  517. ret = axg_card_add_aux_devices(&priv->card);
  518. if (ret)
  519. goto out_err;
  520. ret = devm_snd_soc_register_card(dev, &priv->card);
  521. if (ret)
  522. goto out_err;
  523. return 0;
  524. out_err:
  525. axg_card_clean_references(priv);
  526. return ret;
  527. }
  528. static int axg_card_remove(struct platform_device *pdev)
  529. {
  530. struct axg_card *priv = platform_get_drvdata(pdev);
  531. axg_card_clean_references(priv);
  532. return 0;
  533. }
  534. static struct platform_driver axg_card_pdrv = {
  535. .probe = axg_card_probe,
  536. .remove = axg_card_remove,
  537. .driver = {
  538. .name = "axg-sound-card",
  539. .of_match_table = axg_card_of_match,
  540. },
  541. };
  542. module_platform_driver(axg_card_pdrv);
  543. MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
  544. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  545. MODULE_LICENSE("GPL v2");