j721e-evm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
  4. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/soc.h>
  14. #include "davinci-mcasp.h"
  15. /*
  16. * Maximum number of configuration entries for prefixes:
  17. * CPB: 2 (mcasp10 + codec)
  18. * IVI: 3 (mcasp0 + 2x codec)
  19. */
  20. #define J721E_CODEC_CONF_COUNT 5
  21. enum j721e_audio_domain_id {
  22. J721E_AUDIO_DOMAIN_CPB = 0,
  23. J721E_AUDIO_DOMAIN_IVI,
  24. J721E_AUDIO_DOMAIN_LAST,
  25. };
  26. #define J721E_CLK_PARENT_48000 0
  27. #define J721E_CLK_PARENT_44100 1
  28. #define J721E_MAX_CLK_HSDIV 128
  29. #define PCM1368A_MAX_SYSCLK 36864000
  30. #define J721E_DAI_FMT (SND_SOC_DAIFMT_RIGHT_J | \
  31. SND_SOC_DAIFMT_NB_NF | \
  32. SND_SOC_DAIFMT_CBS_CFS)
  33. enum j721e_board_type {
  34. J721E_BOARD_CPB = 1,
  35. J721E_BOARD_CPB_IVI,
  36. };
  37. struct j721e_audio_match_data {
  38. enum j721e_board_type board_type;
  39. int num_links;
  40. unsigned int pll_rates[2];
  41. };
  42. static unsigned int ratios_for_pcm3168a[] = {
  43. 256,
  44. 512,
  45. 768,
  46. };
  47. struct j721e_audio_clocks {
  48. struct clk *target;
  49. struct clk *parent[2];
  50. };
  51. struct j721e_audio_domain {
  52. struct j721e_audio_clocks codec;
  53. struct j721e_audio_clocks mcasp;
  54. int parent_clk_id;
  55. int active;
  56. unsigned int active_link;
  57. unsigned int rate;
  58. };
  59. struct j721e_priv {
  60. struct device *dev;
  61. struct snd_soc_card card;
  62. struct snd_soc_dai_link *dai_links;
  63. struct snd_soc_codec_conf codec_conf[J721E_CODEC_CONF_COUNT];
  64. struct snd_interval rate_range;
  65. const struct j721e_audio_match_data *match_data;
  66. u32 pll_rates[2];
  67. unsigned int hsdiv_rates[2];
  68. struct j721e_audio_domain audio_domains[J721E_AUDIO_DOMAIN_LAST];
  69. struct mutex mutex;
  70. };
  71. static const struct snd_soc_dapm_widget j721e_cpb_dapm_widgets[] = {
  72. SND_SOC_DAPM_HP("CPB Stereo HP 1", NULL),
  73. SND_SOC_DAPM_HP("CPB Stereo HP 2", NULL),
  74. SND_SOC_DAPM_HP("CPB Stereo HP 3", NULL),
  75. SND_SOC_DAPM_LINE("CPB Line Out", NULL),
  76. SND_SOC_DAPM_MIC("CPB Stereo Mic 1", NULL),
  77. SND_SOC_DAPM_MIC("CPB Stereo Mic 2", NULL),
  78. SND_SOC_DAPM_LINE("CPB Line In", NULL),
  79. };
  80. static const struct snd_soc_dapm_route j721e_cpb_dapm_routes[] = {
  81. {"CPB Stereo HP 1", NULL, "codec-1 AOUT1L"},
  82. {"CPB Stereo HP 1", NULL, "codec-1 AOUT1R"},
  83. {"CPB Stereo HP 2", NULL, "codec-1 AOUT2L"},
  84. {"CPB Stereo HP 2", NULL, "codec-1 AOUT2R"},
  85. {"CPB Stereo HP 3", NULL, "codec-1 AOUT3L"},
  86. {"CPB Stereo HP 3", NULL, "codec-1 AOUT3R"},
  87. {"CPB Line Out", NULL, "codec-1 AOUT4L"},
  88. {"CPB Line Out", NULL, "codec-1 AOUT4R"},
  89. {"codec-1 AIN1L", NULL, "CPB Stereo Mic 1"},
  90. {"codec-1 AIN1R", NULL, "CPB Stereo Mic 1"},
  91. {"codec-1 AIN2L", NULL, "CPB Stereo Mic 2"},
  92. {"codec-1 AIN2R", NULL, "CPB Stereo Mic 2"},
  93. {"codec-1 AIN3L", NULL, "CPB Line In"},
  94. {"codec-1 AIN3R", NULL, "CPB Line In"},
  95. };
  96. static const struct snd_soc_dapm_widget j721e_ivi_codec_a_dapm_widgets[] = {
  97. SND_SOC_DAPM_LINE("IVI A Line Out 1", NULL),
  98. SND_SOC_DAPM_LINE("IVI A Line Out 2", NULL),
  99. SND_SOC_DAPM_LINE("IVI A Line Out 3", NULL),
  100. SND_SOC_DAPM_LINE("IVI A Line Out 4", NULL),
  101. SND_SOC_DAPM_MIC("IVI A Stereo Mic 1", NULL),
  102. SND_SOC_DAPM_MIC("IVI A Stereo Mic 2", NULL),
  103. SND_SOC_DAPM_LINE("IVI A Line In", NULL),
  104. };
  105. static const struct snd_soc_dapm_route j721e_codec_a_dapm_routes[] = {
  106. {"IVI A Line Out 1", NULL, "codec-a AOUT1L"},
  107. {"IVI A Line Out 1", NULL, "codec-a AOUT1R"},
  108. {"IVI A Line Out 2", NULL, "codec-a AOUT2L"},
  109. {"IVI A Line Out 2", NULL, "codec-a AOUT2R"},
  110. {"IVI A Line Out 3", NULL, "codec-a AOUT3L"},
  111. {"IVI A Line Out 3", NULL, "codec-a AOUT3R"},
  112. {"IVI A Line Out 4", NULL, "codec-a AOUT4L"},
  113. {"IVI A Line Out 4", NULL, "codec-a AOUT4R"},
  114. {"codec-a AIN1L", NULL, "IVI A Stereo Mic 1"},
  115. {"codec-a AIN1R", NULL, "IVI A Stereo Mic 1"},
  116. {"codec-a AIN2L", NULL, "IVI A Stereo Mic 2"},
  117. {"codec-a AIN2R", NULL, "IVI A Stereo Mic 2"},
  118. {"codec-a AIN3L", NULL, "IVI A Line In"},
  119. {"codec-a AIN3R", NULL, "IVI A Line In"},
  120. };
  121. static const struct snd_soc_dapm_widget j721e_ivi_codec_b_dapm_widgets[] = {
  122. SND_SOC_DAPM_LINE("IVI B Line Out 1", NULL),
  123. SND_SOC_DAPM_LINE("IVI B Line Out 2", NULL),
  124. SND_SOC_DAPM_LINE("IVI B Line Out 3", NULL),
  125. SND_SOC_DAPM_LINE("IVI B Line Out 4", NULL),
  126. SND_SOC_DAPM_MIC("IVI B Stereo Mic 1", NULL),
  127. SND_SOC_DAPM_MIC("IVI B Stereo Mic 2", NULL),
  128. SND_SOC_DAPM_LINE("IVI B Line In", NULL),
  129. };
  130. static const struct snd_soc_dapm_route j721e_codec_b_dapm_routes[] = {
  131. {"IVI B Line Out 1", NULL, "codec-b AOUT1L"},
  132. {"IVI B Line Out 1", NULL, "codec-b AOUT1R"},
  133. {"IVI B Line Out 2", NULL, "codec-b AOUT2L"},
  134. {"IVI B Line Out 2", NULL, "codec-b AOUT2R"},
  135. {"IVI B Line Out 3", NULL, "codec-b AOUT3L"},
  136. {"IVI B Line Out 3", NULL, "codec-b AOUT3R"},
  137. {"IVI B Line Out 4", NULL, "codec-b AOUT4L"},
  138. {"IVI B Line Out 4", NULL, "codec-b AOUT4R"},
  139. {"codec-b AIN1L", NULL, "IVI B Stereo Mic 1"},
  140. {"codec-b AIN1R", NULL, "IVI B Stereo Mic 1"},
  141. {"codec-b AIN2L", NULL, "IVI B Stereo Mic 2"},
  142. {"codec-b AIN2R", NULL, "IVI B Stereo Mic 2"},
  143. {"codec-b AIN3L", NULL, "IVI B Line In"},
  144. {"codec-b AIN3R", NULL, "IVI B Line In"},
  145. };
  146. static int j721e_configure_refclk(struct j721e_priv *priv,
  147. unsigned int audio_domain, unsigned int rate)
  148. {
  149. struct j721e_audio_domain *domain = &priv->audio_domains[audio_domain];
  150. unsigned int scki;
  151. int ret = -EINVAL;
  152. int i, clk_id;
  153. if (!(rate % 8000) && priv->pll_rates[J721E_CLK_PARENT_48000])
  154. clk_id = J721E_CLK_PARENT_48000;
  155. else if (!(rate % 11025) && priv->pll_rates[J721E_CLK_PARENT_44100])
  156. clk_id = J721E_CLK_PARENT_44100;
  157. else if (!(rate % 11025) && priv->pll_rates[J721E_CLK_PARENT_48000])
  158. clk_id = J721E_CLK_PARENT_48000;
  159. else
  160. return ret;
  161. for (i = 0; i < ARRAY_SIZE(ratios_for_pcm3168a); i++) {
  162. scki = ratios_for_pcm3168a[i] * rate;
  163. if (priv->pll_rates[clk_id] / scki <= J721E_MAX_CLK_HSDIV) {
  164. ret = 0;
  165. break;
  166. }
  167. }
  168. if (ret) {
  169. dev_err(priv->dev, "No valid clock configuration for %u Hz\n",
  170. rate);
  171. return ret;
  172. }
  173. if (domain->parent_clk_id == -1 || priv->hsdiv_rates[domain->parent_clk_id] != scki) {
  174. dev_dbg(priv->dev,
  175. "domain%u configuration for %u Hz: %s, %dxFS (SCKI: %u Hz)\n",
  176. audio_domain, rate,
  177. clk_id == J721E_CLK_PARENT_48000 ? "PLL4" : "PLL15",
  178. ratios_for_pcm3168a[i], scki);
  179. if (domain->parent_clk_id != clk_id) {
  180. ret = clk_set_parent(domain->codec.target,
  181. domain->codec.parent[clk_id]);
  182. if (ret)
  183. return ret;
  184. ret = clk_set_parent(domain->mcasp.target,
  185. domain->mcasp.parent[clk_id]);
  186. if (ret)
  187. return ret;
  188. domain->parent_clk_id = clk_id;
  189. }
  190. ret = clk_set_rate(domain->codec.target, scki);
  191. if (ret) {
  192. dev_err(priv->dev, "codec set rate failed for %u Hz\n",
  193. scki);
  194. return ret;
  195. }
  196. ret = clk_set_rate(domain->mcasp.target, scki);
  197. if (!ret) {
  198. priv->hsdiv_rates[domain->parent_clk_id] = scki;
  199. } else {
  200. dev_err(priv->dev, "mcasp set rate failed for %u Hz\n",
  201. scki);
  202. return ret;
  203. }
  204. }
  205. return ret;
  206. }
  207. static int j721e_rule_rate(struct snd_pcm_hw_params *params,
  208. struct snd_pcm_hw_rule *rule)
  209. {
  210. struct snd_interval *t = rule->private;
  211. return snd_interval_refine(hw_param_interval(params, rule->var), t);
  212. }
  213. static int j721e_audio_startup(struct snd_pcm_substream *substream)
  214. {
  215. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  216. struct j721e_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  217. unsigned int domain_id = rtd->dai_link->id;
  218. struct j721e_audio_domain *domain = &priv->audio_domains[domain_id];
  219. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  220. struct snd_soc_dai *codec_dai;
  221. unsigned int active_rate;
  222. int ret = 0;
  223. int i;
  224. mutex_lock(&priv->mutex);
  225. domain->active++;
  226. for (i = 0; i < J721E_AUDIO_DOMAIN_LAST; i++) {
  227. active_rate = priv->audio_domains[i].rate;
  228. if (active_rate)
  229. break;
  230. }
  231. if (active_rate)
  232. ret = snd_pcm_hw_constraint_single(substream->runtime,
  233. SNDRV_PCM_HW_PARAM_RATE,
  234. active_rate);
  235. else
  236. ret = snd_pcm_hw_rule_add(substream->runtime, 0,
  237. SNDRV_PCM_HW_PARAM_RATE,
  238. j721e_rule_rate, &priv->rate_range,
  239. SNDRV_PCM_HW_PARAM_RATE, -1);
  240. if (ret)
  241. goto out;
  242. /* Reset TDM slots to 32 */
  243. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 32);
  244. if (ret && ret != -ENOTSUPP)
  245. goto out;
  246. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  247. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 0x3, 2, 32);
  248. if (ret && ret != -ENOTSUPP)
  249. goto out;
  250. }
  251. if (ret == -ENOTSUPP)
  252. ret = 0;
  253. out:
  254. if (ret)
  255. domain->active--;
  256. mutex_unlock(&priv->mutex);
  257. return ret;
  258. }
  259. static int j721e_audio_hw_params(struct snd_pcm_substream *substream,
  260. struct snd_pcm_hw_params *params)
  261. {
  262. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  263. struct snd_soc_card *card = rtd->card;
  264. struct j721e_priv *priv = snd_soc_card_get_drvdata(card);
  265. unsigned int domain_id = rtd->dai_link->id;
  266. struct j721e_audio_domain *domain = &priv->audio_domains[domain_id];
  267. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  268. struct snd_soc_dai *codec_dai;
  269. unsigned int sysclk_rate;
  270. int slot_width = 32;
  271. int ret;
  272. int i;
  273. mutex_lock(&priv->mutex);
  274. if (domain->rate && domain->rate != params_rate(params)) {
  275. ret = -EINVAL;
  276. goto out;
  277. }
  278. if (params_width(params) == 16)
  279. slot_width = 16;
  280. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, slot_width);
  281. if (ret && ret != -ENOTSUPP)
  282. goto out;
  283. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  284. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 0x3, 2,
  285. slot_width);
  286. if (ret && ret != -ENOTSUPP)
  287. goto out;
  288. }
  289. ret = j721e_configure_refclk(priv, domain_id, params_rate(params));
  290. if (ret)
  291. goto out;
  292. sysclk_rate = priv->hsdiv_rates[domain->parent_clk_id];
  293. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  294. ret = snd_soc_dai_set_sysclk(codec_dai, 0, sysclk_rate,
  295. SND_SOC_CLOCK_IN);
  296. if (ret && ret != -ENOTSUPP) {
  297. dev_err(priv->dev,
  298. "codec set_sysclk failed for %u Hz\n",
  299. sysclk_rate);
  300. goto out;
  301. }
  302. }
  303. ret = snd_soc_dai_set_sysclk(cpu_dai, MCASP_CLK_HCLK_AUXCLK,
  304. sysclk_rate, SND_SOC_CLOCK_IN);
  305. if (ret && ret != -ENOTSUPP) {
  306. dev_err(priv->dev, "mcasp set_sysclk failed for %u Hz\n",
  307. sysclk_rate);
  308. } else {
  309. domain->rate = params_rate(params);
  310. ret = 0;
  311. }
  312. out:
  313. mutex_unlock(&priv->mutex);
  314. return ret;
  315. }
  316. static void j721e_audio_shutdown(struct snd_pcm_substream *substream)
  317. {
  318. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  319. struct j721e_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  320. unsigned int domain_id = rtd->dai_link->id;
  321. struct j721e_audio_domain *domain = &priv->audio_domains[domain_id];
  322. mutex_lock(&priv->mutex);
  323. domain->active--;
  324. if (!domain->active) {
  325. domain->rate = 0;
  326. domain->active_link = 0;
  327. }
  328. mutex_unlock(&priv->mutex);
  329. }
  330. static const struct snd_soc_ops j721e_audio_ops = {
  331. .startup = j721e_audio_startup,
  332. .hw_params = j721e_audio_hw_params,
  333. .shutdown = j721e_audio_shutdown,
  334. };
  335. static int j721e_audio_init(struct snd_soc_pcm_runtime *rtd)
  336. {
  337. struct j721e_priv *priv = snd_soc_card_get_drvdata(rtd->card);
  338. unsigned int domain_id = rtd->dai_link->id;
  339. struct j721e_audio_domain *domain = &priv->audio_domains[domain_id];
  340. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  341. struct snd_soc_dai *codec_dai;
  342. unsigned int sysclk_rate;
  343. int i, ret;
  344. /* Set up initial clock configuration */
  345. ret = j721e_configure_refclk(priv, domain_id, 48000);
  346. if (ret)
  347. return ret;
  348. sysclk_rate = priv->hsdiv_rates[domain->parent_clk_id];
  349. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  350. ret = snd_soc_dai_set_sysclk(codec_dai, 0, sysclk_rate,
  351. SND_SOC_CLOCK_IN);
  352. if (ret && ret != -ENOTSUPP)
  353. return ret;
  354. }
  355. ret = snd_soc_dai_set_sysclk(cpu_dai, MCASP_CLK_HCLK_AUXCLK,
  356. sysclk_rate, SND_SOC_CLOCK_IN);
  357. if (ret && ret != -ENOTSUPP)
  358. return ret;
  359. /* Set initial tdm slots */
  360. ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 32);
  361. if (ret && ret != -ENOTSUPP)
  362. return ret;
  363. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  364. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 0x3, 2, 32);
  365. if (ret && ret != -ENOTSUPP)
  366. return ret;
  367. }
  368. return 0;
  369. }
  370. static int j721e_audio_init_ivi(struct snd_soc_pcm_runtime *rtd)
  371. {
  372. struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
  373. snd_soc_dapm_new_controls(dapm, j721e_ivi_codec_a_dapm_widgets,
  374. ARRAY_SIZE(j721e_ivi_codec_a_dapm_widgets));
  375. snd_soc_dapm_add_routes(dapm, j721e_codec_a_dapm_routes,
  376. ARRAY_SIZE(j721e_codec_a_dapm_routes));
  377. snd_soc_dapm_new_controls(dapm, j721e_ivi_codec_b_dapm_widgets,
  378. ARRAY_SIZE(j721e_ivi_codec_b_dapm_widgets));
  379. snd_soc_dapm_add_routes(dapm, j721e_codec_b_dapm_routes,
  380. ARRAY_SIZE(j721e_codec_b_dapm_routes));
  381. return j721e_audio_init(rtd);
  382. }
  383. static int j721e_get_clocks(struct device *dev,
  384. struct j721e_audio_clocks *clocks, char *prefix)
  385. {
  386. struct clk *parent;
  387. char *clk_name;
  388. int ret;
  389. clocks->target = devm_clk_get(dev, prefix);
  390. if (IS_ERR(clocks->target))
  391. return dev_err_probe(dev, PTR_ERR(clocks->target),
  392. "failed to acquire %s\n", prefix);
  393. clk_name = kasprintf(GFP_KERNEL, "%s-48000", prefix);
  394. if (clk_name) {
  395. parent = devm_clk_get(dev, clk_name);
  396. kfree(clk_name);
  397. if (IS_ERR(parent)) {
  398. ret = PTR_ERR(parent);
  399. if (ret == -EPROBE_DEFER)
  400. return ret;
  401. dev_dbg(dev, "no 48KHz parent for %s: %d\n", prefix, ret);
  402. parent = NULL;
  403. }
  404. clocks->parent[J721E_CLK_PARENT_48000] = parent;
  405. } else {
  406. return -ENOMEM;
  407. }
  408. clk_name = kasprintf(GFP_KERNEL, "%s-44100", prefix);
  409. if (clk_name) {
  410. parent = devm_clk_get(dev, clk_name);
  411. kfree(clk_name);
  412. if (IS_ERR(parent)) {
  413. ret = PTR_ERR(parent);
  414. if (ret == -EPROBE_DEFER)
  415. return ret;
  416. dev_dbg(dev, "no 44.1KHz parent for %s: %d\n", prefix, ret);
  417. parent = NULL;
  418. }
  419. clocks->parent[J721E_CLK_PARENT_44100] = parent;
  420. } else {
  421. return -ENOMEM;
  422. }
  423. if (!clocks->parent[J721E_CLK_PARENT_44100] &&
  424. !clocks->parent[J721E_CLK_PARENT_48000]) {
  425. dev_err(dev, "At least one parent clock is needed for %s\n",
  426. prefix);
  427. return -EINVAL;
  428. }
  429. return 0;
  430. }
  431. static const struct j721e_audio_match_data j721e_cpb_data = {
  432. .board_type = J721E_BOARD_CPB,
  433. .num_links = 2, /* CPB pcm3168a */
  434. .pll_rates = {
  435. [J721E_CLK_PARENT_44100] = 1083801600, /* PLL15 */
  436. [J721E_CLK_PARENT_48000] = 1179648000, /* PLL4 */
  437. },
  438. };
  439. static const struct j721e_audio_match_data j721e_cpb_ivi_data = {
  440. .board_type = J721E_BOARD_CPB_IVI,
  441. .num_links = 4, /* CPB pcm3168a + 2x pcm3168a on IVI */
  442. .pll_rates = {
  443. [J721E_CLK_PARENT_44100] = 1083801600, /* PLL15 */
  444. [J721E_CLK_PARENT_48000] = 1179648000, /* PLL4 */
  445. },
  446. };
  447. static const struct j721e_audio_match_data j7200_cpb_data = {
  448. .board_type = J721E_BOARD_CPB,
  449. .num_links = 2, /* CPB pcm3168a */
  450. .pll_rates = {
  451. [J721E_CLK_PARENT_48000] = 2359296000u, /* PLL4 */
  452. },
  453. };
  454. static const struct of_device_id j721e_audio_of_match[] = {
  455. {
  456. .compatible = "ti,j721e-cpb-audio",
  457. .data = &j721e_cpb_data,
  458. }, {
  459. .compatible = "ti,j721e-cpb-ivi-audio",
  460. .data = &j721e_cpb_ivi_data,
  461. }, {
  462. .compatible = "ti,j7200-cpb-audio",
  463. .data = &j7200_cpb_data,
  464. },
  465. { },
  466. };
  467. MODULE_DEVICE_TABLE(of, j721e_audio_of_match);
  468. static int j721e_calculate_rate_range(struct j721e_priv *priv)
  469. {
  470. const struct j721e_audio_match_data *match_data = priv->match_data;
  471. struct j721e_audio_clocks *domain_clocks;
  472. unsigned int min_rate, max_rate, pll_rate;
  473. struct clk *pll;
  474. domain_clocks = &priv->audio_domains[J721E_AUDIO_DOMAIN_CPB].mcasp;
  475. pll = clk_get_parent(domain_clocks->parent[J721E_CLK_PARENT_44100]);
  476. if (IS_ERR_OR_NULL(pll)) {
  477. priv->pll_rates[J721E_CLK_PARENT_44100] =
  478. match_data->pll_rates[J721E_CLK_PARENT_44100];
  479. } else {
  480. priv->pll_rates[J721E_CLK_PARENT_44100] = clk_get_rate(pll);
  481. clk_put(pll);
  482. }
  483. pll = clk_get_parent(domain_clocks->parent[J721E_CLK_PARENT_48000]);
  484. if (IS_ERR_OR_NULL(pll)) {
  485. priv->pll_rates[J721E_CLK_PARENT_48000] =
  486. match_data->pll_rates[J721E_CLK_PARENT_48000];
  487. } else {
  488. priv->pll_rates[J721E_CLK_PARENT_48000] = clk_get_rate(pll);
  489. clk_put(pll);
  490. }
  491. if (!priv->pll_rates[J721E_CLK_PARENT_44100] &&
  492. !priv->pll_rates[J721E_CLK_PARENT_48000]) {
  493. dev_err(priv->dev, "At least one PLL is needed\n");
  494. return -EINVAL;
  495. }
  496. if (priv->pll_rates[J721E_CLK_PARENT_44100])
  497. pll_rate = priv->pll_rates[J721E_CLK_PARENT_44100];
  498. else
  499. pll_rate = priv->pll_rates[J721E_CLK_PARENT_48000];
  500. min_rate = pll_rate / J721E_MAX_CLK_HSDIV;
  501. min_rate /= ratios_for_pcm3168a[ARRAY_SIZE(ratios_for_pcm3168a) - 1];
  502. if (priv->pll_rates[J721E_CLK_PARENT_48000])
  503. pll_rate = priv->pll_rates[J721E_CLK_PARENT_48000];
  504. else
  505. pll_rate = priv->pll_rates[J721E_CLK_PARENT_44100];
  506. if (pll_rate > PCM1368A_MAX_SYSCLK)
  507. pll_rate = PCM1368A_MAX_SYSCLK;
  508. max_rate = pll_rate / ratios_for_pcm3168a[0];
  509. snd_interval_any(&priv->rate_range);
  510. priv->rate_range.min = min_rate;
  511. priv->rate_range.max = max_rate;
  512. return 0;
  513. }
  514. static int j721e_soc_probe_cpb(struct j721e_priv *priv, int *link_idx,
  515. int *conf_idx)
  516. {
  517. struct device_node *node = priv->dev->of_node;
  518. struct snd_soc_dai_link_component *compnent;
  519. struct device_node *dai_node, *codec_node;
  520. struct j721e_audio_domain *domain;
  521. int comp_count, comp_idx;
  522. int ret;
  523. dai_node = of_parse_phandle(node, "ti,cpb-mcasp", 0);
  524. if (!dai_node) {
  525. dev_err(priv->dev, "CPB McASP node is not provided\n");
  526. return -EINVAL;
  527. }
  528. codec_node = of_parse_phandle(node, "ti,cpb-codec", 0);
  529. if (!codec_node) {
  530. dev_err(priv->dev, "CPB codec node is not provided\n");
  531. ret = -EINVAL;
  532. goto put_dai_node;
  533. }
  534. domain = &priv->audio_domains[J721E_AUDIO_DOMAIN_CPB];
  535. ret = j721e_get_clocks(priv->dev, &domain->codec, "cpb-codec-scki");
  536. if (ret)
  537. goto put_codec_node;
  538. ret = j721e_get_clocks(priv->dev, &domain->mcasp, "cpb-mcasp-auxclk");
  539. if (ret)
  540. goto put_codec_node;
  541. /*
  542. * Common Processor Board, two links
  543. * Link 1: McASP10 -> pcm3168a_1 DAC
  544. * Link 2: McASP10 <- pcm3168a_1 ADC
  545. */
  546. comp_count = 6;
  547. compnent = devm_kcalloc(priv->dev, comp_count, sizeof(*compnent),
  548. GFP_KERNEL);
  549. if (!compnent) {
  550. ret = -ENOMEM;
  551. goto put_codec_node;
  552. }
  553. comp_idx = 0;
  554. priv->dai_links[*link_idx].cpus = &compnent[comp_idx++];
  555. priv->dai_links[*link_idx].num_cpus = 1;
  556. priv->dai_links[*link_idx].codecs = &compnent[comp_idx++];
  557. priv->dai_links[*link_idx].num_codecs = 1;
  558. priv->dai_links[*link_idx].platforms = &compnent[comp_idx++];
  559. priv->dai_links[*link_idx].num_platforms = 1;
  560. priv->dai_links[*link_idx].name = "CPB PCM3168A Playback";
  561. priv->dai_links[*link_idx].stream_name = "CPB PCM3168A Analog";
  562. priv->dai_links[*link_idx].cpus->of_node = dai_node;
  563. priv->dai_links[*link_idx].platforms->of_node = dai_node;
  564. priv->dai_links[*link_idx].codecs->of_node = codec_node;
  565. priv->dai_links[*link_idx].codecs->dai_name = "pcm3168a-dac";
  566. priv->dai_links[*link_idx].playback_only = 1;
  567. priv->dai_links[*link_idx].id = J721E_AUDIO_DOMAIN_CPB;
  568. priv->dai_links[*link_idx].dai_fmt = J721E_DAI_FMT;
  569. priv->dai_links[*link_idx].init = j721e_audio_init;
  570. priv->dai_links[*link_idx].ops = &j721e_audio_ops;
  571. (*link_idx)++;
  572. priv->dai_links[*link_idx].cpus = &compnent[comp_idx++];
  573. priv->dai_links[*link_idx].num_cpus = 1;
  574. priv->dai_links[*link_idx].codecs = &compnent[comp_idx++];
  575. priv->dai_links[*link_idx].num_codecs = 1;
  576. priv->dai_links[*link_idx].platforms = &compnent[comp_idx++];
  577. priv->dai_links[*link_idx].num_platforms = 1;
  578. priv->dai_links[*link_idx].name = "CPB PCM3168A Capture";
  579. priv->dai_links[*link_idx].stream_name = "CPB PCM3168A Analog";
  580. priv->dai_links[*link_idx].cpus->of_node = dai_node;
  581. priv->dai_links[*link_idx].platforms->of_node = dai_node;
  582. priv->dai_links[*link_idx].codecs->of_node = codec_node;
  583. priv->dai_links[*link_idx].codecs->dai_name = "pcm3168a-adc";
  584. priv->dai_links[*link_idx].capture_only = 1;
  585. priv->dai_links[*link_idx].id = J721E_AUDIO_DOMAIN_CPB;
  586. priv->dai_links[*link_idx].dai_fmt = J721E_DAI_FMT;
  587. priv->dai_links[*link_idx].init = j721e_audio_init;
  588. priv->dai_links[*link_idx].ops = &j721e_audio_ops;
  589. (*link_idx)++;
  590. priv->codec_conf[*conf_idx].dlc.of_node = codec_node;
  591. priv->codec_conf[*conf_idx].name_prefix = "codec-1";
  592. (*conf_idx)++;
  593. priv->codec_conf[*conf_idx].dlc.of_node = dai_node;
  594. priv->codec_conf[*conf_idx].name_prefix = "McASP10";
  595. (*conf_idx)++;
  596. return 0;
  597. put_codec_node:
  598. of_node_put(codec_node);
  599. put_dai_node:
  600. of_node_put(dai_node);
  601. return ret;
  602. }
  603. static int j721e_soc_probe_ivi(struct j721e_priv *priv, int *link_idx,
  604. int *conf_idx)
  605. {
  606. struct device_node *node = priv->dev->of_node;
  607. struct snd_soc_dai_link_component *compnent;
  608. struct device_node *dai_node, *codeca_node, *codecb_node;
  609. struct j721e_audio_domain *domain;
  610. int comp_count, comp_idx;
  611. int ret;
  612. if (priv->match_data->board_type != J721E_BOARD_CPB_IVI)
  613. return 0;
  614. dai_node = of_parse_phandle(node, "ti,ivi-mcasp", 0);
  615. if (!dai_node) {
  616. dev_err(priv->dev, "IVI McASP node is not provided\n");
  617. return -EINVAL;
  618. }
  619. codeca_node = of_parse_phandle(node, "ti,ivi-codec-a", 0);
  620. if (!codeca_node) {
  621. dev_err(priv->dev, "IVI codec-a node is not provided\n");
  622. ret = -EINVAL;
  623. goto put_dai_node;
  624. }
  625. codecb_node = of_parse_phandle(node, "ti,ivi-codec-b", 0);
  626. if (!codecb_node) {
  627. dev_warn(priv->dev, "IVI codec-b node is not provided\n");
  628. ret = 0;
  629. goto put_codeca_node;
  630. }
  631. domain = &priv->audio_domains[J721E_AUDIO_DOMAIN_IVI];
  632. ret = j721e_get_clocks(priv->dev, &domain->codec, "ivi-codec-scki");
  633. if (ret)
  634. goto put_codecb_node;
  635. ret = j721e_get_clocks(priv->dev, &domain->mcasp, "ivi-mcasp-auxclk");
  636. if (ret)
  637. goto put_codecb_node;
  638. /*
  639. * IVI extension, two links
  640. * Link 1: McASP0 -> pcm3168a_a DAC
  641. * \> pcm3168a_b DAC
  642. * Link 2: McASP0 <- pcm3168a_a ADC
  643. * \ pcm3168a_b ADC
  644. */
  645. comp_count = 8;
  646. compnent = devm_kcalloc(priv->dev, comp_count, sizeof(*compnent),
  647. GFP_KERNEL);
  648. if (!compnent) {
  649. ret = -ENOMEM;
  650. goto put_codecb_node;
  651. }
  652. comp_idx = 0;
  653. priv->dai_links[*link_idx].cpus = &compnent[comp_idx++];
  654. priv->dai_links[*link_idx].num_cpus = 1;
  655. priv->dai_links[*link_idx].platforms = &compnent[comp_idx++];
  656. priv->dai_links[*link_idx].num_platforms = 1;
  657. priv->dai_links[*link_idx].codecs = &compnent[comp_idx];
  658. priv->dai_links[*link_idx].num_codecs = 2;
  659. comp_idx += 2;
  660. priv->dai_links[*link_idx].name = "IVI 2xPCM3168A Playback";
  661. priv->dai_links[*link_idx].stream_name = "IVI 2xPCM3168A Analog";
  662. priv->dai_links[*link_idx].cpus->of_node = dai_node;
  663. priv->dai_links[*link_idx].platforms->of_node = dai_node;
  664. priv->dai_links[*link_idx].codecs[0].of_node = codeca_node;
  665. priv->dai_links[*link_idx].codecs[0].dai_name = "pcm3168a-dac";
  666. priv->dai_links[*link_idx].codecs[1].of_node = codecb_node;
  667. priv->dai_links[*link_idx].codecs[1].dai_name = "pcm3168a-dac";
  668. priv->dai_links[*link_idx].playback_only = 1;
  669. priv->dai_links[*link_idx].id = J721E_AUDIO_DOMAIN_IVI;
  670. priv->dai_links[*link_idx].dai_fmt = J721E_DAI_FMT;
  671. priv->dai_links[*link_idx].init = j721e_audio_init_ivi;
  672. priv->dai_links[*link_idx].ops = &j721e_audio_ops;
  673. (*link_idx)++;
  674. priv->dai_links[*link_idx].cpus = &compnent[comp_idx++];
  675. priv->dai_links[*link_idx].num_cpus = 1;
  676. priv->dai_links[*link_idx].platforms = &compnent[comp_idx++];
  677. priv->dai_links[*link_idx].num_platforms = 1;
  678. priv->dai_links[*link_idx].codecs = &compnent[comp_idx];
  679. priv->dai_links[*link_idx].num_codecs = 2;
  680. priv->dai_links[*link_idx].name = "IVI 2xPCM3168A Capture";
  681. priv->dai_links[*link_idx].stream_name = "IVI 2xPCM3168A Analog";
  682. priv->dai_links[*link_idx].cpus->of_node = dai_node;
  683. priv->dai_links[*link_idx].platforms->of_node = dai_node;
  684. priv->dai_links[*link_idx].codecs[0].of_node = codeca_node;
  685. priv->dai_links[*link_idx].codecs[0].dai_name = "pcm3168a-adc";
  686. priv->dai_links[*link_idx].codecs[1].of_node = codecb_node;
  687. priv->dai_links[*link_idx].codecs[1].dai_name = "pcm3168a-adc";
  688. priv->dai_links[*link_idx].capture_only = 1;
  689. priv->dai_links[*link_idx].id = J721E_AUDIO_DOMAIN_IVI;
  690. priv->dai_links[*link_idx].dai_fmt = J721E_DAI_FMT;
  691. priv->dai_links[*link_idx].init = j721e_audio_init;
  692. priv->dai_links[*link_idx].ops = &j721e_audio_ops;
  693. (*link_idx)++;
  694. priv->codec_conf[*conf_idx].dlc.of_node = codeca_node;
  695. priv->codec_conf[*conf_idx].name_prefix = "codec-a";
  696. (*conf_idx)++;
  697. priv->codec_conf[*conf_idx].dlc.of_node = codecb_node;
  698. priv->codec_conf[*conf_idx].name_prefix = "codec-b";
  699. (*conf_idx)++;
  700. priv->codec_conf[*conf_idx].dlc.of_node = dai_node;
  701. priv->codec_conf[*conf_idx].name_prefix = "McASP0";
  702. (*conf_idx)++;
  703. return 0;
  704. put_codecb_node:
  705. of_node_put(codecb_node);
  706. put_codeca_node:
  707. of_node_put(codeca_node);
  708. put_dai_node:
  709. of_node_put(dai_node);
  710. return ret;
  711. }
  712. static int j721e_soc_probe(struct platform_device *pdev)
  713. {
  714. struct device_node *node = pdev->dev.of_node;
  715. struct snd_soc_card *card;
  716. const struct of_device_id *match;
  717. struct j721e_priv *priv;
  718. int link_cnt, conf_cnt, ret, i;
  719. if (!node) {
  720. dev_err(&pdev->dev, "of node is missing.\n");
  721. return -ENODEV;
  722. }
  723. match = of_match_node(j721e_audio_of_match, node);
  724. if (!match) {
  725. dev_err(&pdev->dev, "No compatible match found\n");
  726. return -ENODEV;
  727. }
  728. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  729. if (!priv)
  730. return -ENOMEM;
  731. priv->match_data = match->data;
  732. priv->dai_links = devm_kcalloc(&pdev->dev, priv->match_data->num_links,
  733. sizeof(*priv->dai_links), GFP_KERNEL);
  734. if (!priv->dai_links)
  735. return -ENOMEM;
  736. for (i = 0; i < J721E_AUDIO_DOMAIN_LAST; i++)
  737. priv->audio_domains[i].parent_clk_id = -1;
  738. priv->dev = &pdev->dev;
  739. card = &priv->card;
  740. card->dev = &pdev->dev;
  741. card->owner = THIS_MODULE;
  742. card->dapm_widgets = j721e_cpb_dapm_widgets;
  743. card->num_dapm_widgets = ARRAY_SIZE(j721e_cpb_dapm_widgets);
  744. card->dapm_routes = j721e_cpb_dapm_routes;
  745. card->num_dapm_routes = ARRAY_SIZE(j721e_cpb_dapm_routes);
  746. card->fully_routed = 1;
  747. if (snd_soc_of_parse_card_name(card, "model")) {
  748. dev_err(&pdev->dev, "Card name is not provided\n");
  749. return -ENODEV;
  750. }
  751. link_cnt = 0;
  752. conf_cnt = 0;
  753. ret = j721e_soc_probe_cpb(priv, &link_cnt, &conf_cnt);
  754. if (ret)
  755. return ret;
  756. ret = j721e_soc_probe_ivi(priv, &link_cnt, &conf_cnt);
  757. if (ret)
  758. return ret;
  759. card->dai_link = priv->dai_links;
  760. card->num_links = link_cnt;
  761. card->codec_conf = priv->codec_conf;
  762. card->num_configs = conf_cnt;
  763. ret = j721e_calculate_rate_range(priv);
  764. if (ret)
  765. return ret;
  766. snd_soc_card_set_drvdata(card, priv);
  767. mutex_init(&priv->mutex);
  768. ret = devm_snd_soc_register_card(&pdev->dev, card);
  769. if (ret)
  770. dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
  771. ret);
  772. return ret;
  773. }
  774. static struct platform_driver j721e_soc_driver = {
  775. .driver = {
  776. .name = "j721e-audio",
  777. .pm = &snd_soc_pm_ops,
  778. .of_match_table = j721e_audio_of_match,
  779. },
  780. .probe = j721e_soc_probe,
  781. };
  782. module_platform_driver(j721e_soc_driver);
  783. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  784. MODULE_DESCRIPTION("ASoC machine driver for j721e Common Processor Board");
  785. MODULE_LICENSE("GPL v2");