j721e-evm.c 26 KB

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