j721e-evm.c 26 KB

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