zynqmp_dp_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP DisplayPort Subsystem Driver - Audio support
  4. *
  5. * Copyright (C) 2015 - 2024 Xilinx, Inc.
  6. *
  7. * Authors:
  8. * - Hyun Woo Kwon <hyun.kwon@xilinx.com>
  9. * - Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/mutex.h>
  14. #include <linux/pm_runtime.h>
  15. #include <sound/asoundef.h>
  16. #include <sound/core.h>
  17. #include <sound/dmaengine_pcm.h>
  18. #include <sound/initval.h>
  19. #include <sound/pcm.h>
  20. #include <sound/soc.h>
  21. #include <sound/tlv.h>
  22. #include "zynqmp_disp_regs.h"
  23. #include "zynqmp_dp.h"
  24. #include "zynqmp_dpsub.h"
  25. #define ZYNQMP_DISP_AUD_SMPL_RATE_TO_CLK 512
  26. #define ZYNQMP_NUM_PCMS 2
  27. struct zynqmp_dpsub_audio {
  28. void __iomem *base;
  29. struct snd_soc_card card;
  30. const char *dai_name;
  31. const char *link_names[ZYNQMP_NUM_PCMS];
  32. const char *pcm_names[ZYNQMP_NUM_PCMS];
  33. struct snd_soc_dai_driver dai_driver;
  34. struct snd_dmaengine_pcm_config pcm_configs[2];
  35. struct snd_soc_dai_link links[ZYNQMP_NUM_PCMS];
  36. struct {
  37. struct snd_soc_dai_link_component cpu;
  38. struct snd_soc_dai_link_component platform;
  39. } components[ZYNQMP_NUM_PCMS];
  40. /*
  41. * Protects:
  42. * - enabled_streams
  43. * - volumes
  44. * - current_rate
  45. */
  46. struct mutex enable_lock;
  47. u32 enabled_streams;
  48. u32 current_rate;
  49. u16 volumes[2];
  50. };
  51. static const struct snd_pcm_hardware zynqmp_dp_pcm_hw = {
  52. .info = SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_MMAP_VALID |
  54. SNDRV_PCM_INFO_INTERLEAVED |
  55. SNDRV_PCM_INFO_PAUSE |
  56. SNDRV_PCM_INFO_RESUME |
  57. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  58. .buffer_bytes_max = 128 * 1024,
  59. .period_bytes_min = 256,
  60. .period_bytes_max = 1024 * 1024,
  61. .periods_min = 2,
  62. .periods_max = 256,
  63. };
  64. static int zynqmp_dp_startup(struct snd_pcm_substream *substream)
  65. {
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  68. 256);
  69. return 0;
  70. }
  71. static const struct snd_soc_ops zynqmp_dp_ops = {
  72. .startup = zynqmp_dp_startup,
  73. };
  74. static void zynqmp_dp_audio_write(struct zynqmp_dpsub_audio *audio, int reg,
  75. u32 val)
  76. {
  77. writel(val, audio->base + reg);
  78. }
  79. static int dp_dai_hw_params(struct snd_pcm_substream *substream,
  80. struct snd_pcm_hw_params *params,
  81. struct snd_soc_dai *socdai)
  82. {
  83. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  84. struct zynqmp_dpsub *dpsub =
  85. snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  86. struct zynqmp_dpsub_audio *audio = dpsub->audio;
  87. int ret;
  88. u32 sample_rate;
  89. struct snd_aes_iec958 iec = { 0 };
  90. unsigned long rate;
  91. sample_rate = params_rate(params);
  92. if (sample_rate != 48000 && sample_rate != 44100)
  93. return -EINVAL;
  94. guard(mutex)(&audio->enable_lock);
  95. if (audio->enabled_streams && audio->current_rate != sample_rate) {
  96. dev_err(dpsub->dev,
  97. "Can't change rate while playback enabled\n");
  98. return -EINVAL;
  99. }
  100. if (audio->enabled_streams > 0) {
  101. /* Nothing to do */
  102. audio->enabled_streams++;
  103. return 0;
  104. }
  105. audio->current_rate = sample_rate;
  106. /* Note: clock rate can only be changed if the clock is disabled */
  107. ret = clk_set_rate(dpsub->aud_clk,
  108. sample_rate * ZYNQMP_DISP_AUD_SMPL_RATE_TO_CLK);
  109. if (ret) {
  110. dev_err(dpsub->dev, "can't set aud_clk to %u err:%d\n",
  111. sample_rate * ZYNQMP_DISP_AUD_SMPL_RATE_TO_CLK, ret);
  112. return ret;
  113. }
  114. clk_prepare_enable(dpsub->aud_clk);
  115. rate = clk_get_rate(dpsub->aud_clk);
  116. /* Ignore some offset +- 10 */
  117. if (abs(sample_rate * ZYNQMP_DISP_AUD_SMPL_RATE_TO_CLK - rate) > 10) {
  118. dev_err(dpsub->dev, "aud_clk offset is higher: %ld\n",
  119. sample_rate * ZYNQMP_DISP_AUD_SMPL_RATE_TO_CLK - rate);
  120. clk_disable_unprepare(dpsub->aud_clk);
  121. return -EINVAL;
  122. }
  123. pm_runtime_get_sync(dpsub->dev);
  124. zynqmp_dp_audio_write(audio, ZYNQMP_DISP_AUD_MIXER_VOLUME,
  125. audio->volumes[0] | (audio->volumes[1] << 16));
  126. /* Clear the audio soft reset register as it's an non-reset flop. */
  127. zynqmp_dp_audio_write(audio, ZYNQMP_DISP_AUD_SOFT_RESET, 0);
  128. /* Only 2 channel audio is supported now */
  129. zynqmp_dp_audio_set_channels(dpsub->dp, 2);
  130. zynqmp_dp_audio_write_n_m(dpsub->dp);
  131. /* Channel status */
  132. if (sample_rate == 48000)
  133. iec.status[3] = IEC958_AES3_CON_FS_48000;
  134. else
  135. iec.status[3] = IEC958_AES3_CON_FS_44100;
  136. for (unsigned int i = 0; i < AES_IEC958_STATUS_SIZE / 4; ++i) {
  137. u32 v;
  138. v = (iec.status[(i * 4) + 0] << 0) |
  139. (iec.status[(i * 4) + 1] << 8) |
  140. (iec.status[(i * 4) + 2] << 16) |
  141. (iec.status[(i * 4) + 3] << 24);
  142. zynqmp_dp_audio_write(audio, ZYNQMP_DISP_AUD_CH_STATUS(i), v);
  143. }
  144. zynqmp_dp_audio_enable(dpsub->dp);
  145. audio->enabled_streams++;
  146. return 0;
  147. }
  148. static int dp_dai_hw_free(struct snd_pcm_substream *substream,
  149. struct snd_soc_dai *socdai)
  150. {
  151. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  152. struct zynqmp_dpsub *dpsub =
  153. snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
  154. struct zynqmp_dpsub_audio *audio = dpsub->audio;
  155. guard(mutex)(&audio->enable_lock);
  156. /* Nothing to do */
  157. if (audio->enabled_streams > 1) {
  158. audio->enabled_streams--;
  159. return 0;
  160. }
  161. pm_runtime_put(dpsub->dev);
  162. zynqmp_dp_audio_disable(dpsub->dp);
  163. /*
  164. * Reset doesn't work. If we assert reset between audio stop and start,
  165. * the audio won't start anymore. Probably we are missing writing
  166. * some audio related registers. A/B buf?
  167. */
  168. /*
  169. zynqmp_disp_audio_write(audio, ZYNQMP_DISP_AUD_SOFT_RESET,
  170. ZYNQMP_DISP_AUD_SOFT_RESET_AUD_SRST);
  171. */
  172. clk_disable_unprepare(dpsub->aud_clk);
  173. audio->current_rate = 0;
  174. audio->enabled_streams--;
  175. return 0;
  176. }
  177. static const struct snd_soc_dai_ops zynqmp_dp_dai_ops = {
  178. .hw_params = dp_dai_hw_params,
  179. .hw_free = dp_dai_hw_free,
  180. };
  181. /*
  182. * Min = 10 * log10(0x1 / 0x2000) = -39.13
  183. * Max = 10 * log10(0xffffff / 0x2000) = 9.03
  184. */
  185. static const DECLARE_TLV_DB_RANGE(zynqmp_dp_tlv,
  186. 0x0, 0x0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, -3913, 1),
  187. 0x1, 0x2000, TLV_DB_LINEAR_ITEM(-3913, 0),
  188. 0x2000, 0xffff, TLV_DB_LINEAR_ITEM(0, 903),
  189. );
  190. static const struct snd_kcontrol_new zynqmp_dp_snd_controls[] = {
  191. SOC_SINGLE_TLV("Input0 Playback Volume", 0,
  192. 0, 0xffff, 0, zynqmp_dp_tlv),
  193. SOC_SINGLE_TLV("Input1 Playback Volume", 1,
  194. 0, 0xffff, 0, zynqmp_dp_tlv),
  195. };
  196. /*
  197. * Note: these read & write functions only support two "registers", 0 and 1,
  198. * for volume 0 and 1. In other words, these are not real register read/write
  199. * functions.
  200. *
  201. * This is done to support caching the volume value for the case where the
  202. * hardware is not enabled, and also to support locking as volumes 0 and 1
  203. * are in the same register.
  204. */
  205. static unsigned int zynqmp_dp_dai_read(struct snd_soc_component *component,
  206. unsigned int reg)
  207. {
  208. struct zynqmp_dpsub *dpsub = dev_get_drvdata(component->dev);
  209. struct zynqmp_dpsub_audio *audio = dpsub->audio;
  210. return audio->volumes[reg];
  211. }
  212. static int zynqmp_dp_dai_write(struct snd_soc_component *component,
  213. unsigned int reg, unsigned int val)
  214. {
  215. struct zynqmp_dpsub *dpsub = dev_get_drvdata(component->dev);
  216. struct zynqmp_dpsub_audio *audio = dpsub->audio;
  217. guard(mutex)(&audio->enable_lock);
  218. audio->volumes[reg] = val;
  219. if (audio->enabled_streams)
  220. zynqmp_dp_audio_write(audio, ZYNQMP_DISP_AUD_MIXER_VOLUME,
  221. audio->volumes[0] |
  222. (audio->volumes[1] << 16));
  223. return 0;
  224. }
  225. static const struct snd_soc_component_driver zynqmp_dp_component_driver = {
  226. .idle_bias_on = 1,
  227. .use_pmdown_time = 1,
  228. .endianness = 1,
  229. .controls = zynqmp_dp_snd_controls,
  230. .num_controls = ARRAY_SIZE(zynqmp_dp_snd_controls),
  231. .read = zynqmp_dp_dai_read,
  232. .write = zynqmp_dp_dai_write,
  233. };
  234. int zynqmp_audio_init(struct zynqmp_dpsub *dpsub)
  235. {
  236. struct platform_device *pdev = to_platform_device(dpsub->dev);
  237. struct device *dev = dpsub->dev;
  238. struct zynqmp_dpsub_audio *audio;
  239. struct snd_soc_card *card;
  240. void *dev_data;
  241. int ret;
  242. if (!dpsub->aud_clk)
  243. return 0;
  244. audio = devm_kzalloc(dev, sizeof(*audio), GFP_KERNEL);
  245. if (!audio)
  246. return -ENOMEM;
  247. dpsub->audio = audio;
  248. mutex_init(&audio->enable_lock);
  249. /* 0x2000 is the zero level, no change */
  250. audio->volumes[0] = 0x2000;
  251. audio->volumes[1] = 0x2000;
  252. audio->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  253. "%s-dai", dev_name(dev));
  254. if (!audio->dai_name)
  255. return -ENOMEM;
  256. for (unsigned int i = 0; i < ZYNQMP_NUM_PCMS; ++i) {
  257. audio->link_names[i] = devm_kasprintf(dev, GFP_KERNEL,
  258. "%s-dp-%u", dev_name(dev), i);
  259. audio->pcm_names[i] = devm_kasprintf(dev, GFP_KERNEL,
  260. "%s-pcm-%u", dev_name(dev), i);
  261. if (!audio->link_names[i] || !audio->pcm_names[i])
  262. return -ENOMEM;
  263. }
  264. audio->base = devm_platform_ioremap_resource_byname(pdev, "aud");
  265. if (IS_ERR(audio->base))
  266. return PTR_ERR(audio->base);
  267. /* Create CPU DAI */
  268. audio->dai_driver = (struct snd_soc_dai_driver) {
  269. .name = audio->dai_name,
  270. .ops = &zynqmp_dp_dai_ops,
  271. .playback = {
  272. .channels_min = 2,
  273. .channels_max = 2,
  274. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  275. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  276. },
  277. };
  278. ret = devm_snd_soc_register_component(dev, &zynqmp_dp_component_driver,
  279. &audio->dai_driver, 1);
  280. if (ret) {
  281. dev_err(dev, "Failed to register CPU DAI\n");
  282. return ret;
  283. }
  284. /* Create PCMs */
  285. for (unsigned int i = 0; i < ZYNQMP_NUM_PCMS; ++i) {
  286. struct snd_dmaengine_pcm_config *pcm_config =
  287. &audio->pcm_configs[i];
  288. *pcm_config = (struct snd_dmaengine_pcm_config){
  289. .name = audio->pcm_names[i],
  290. .pcm_hardware = &zynqmp_dp_pcm_hw,
  291. .prealloc_buffer_size = 64 * 1024,
  292. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] =
  293. i == 0 ? "aud0" : "aud1",
  294. };
  295. ret = devm_snd_dmaengine_pcm_register(dev, pcm_config, 0);
  296. if (ret) {
  297. dev_err(dev, "Failed to register PCM %u\n", i);
  298. return ret;
  299. }
  300. }
  301. /* Create card */
  302. card = &audio->card;
  303. card->name = "DisplayPort";
  304. card->long_name = "DisplayPort Monitor";
  305. card->driver_name = "zynqmp_dpsub";
  306. card->dev = dev;
  307. card->owner = THIS_MODULE;
  308. card->num_links = ZYNQMP_NUM_PCMS;
  309. card->dai_link = audio->links;
  310. for (unsigned int i = 0; i < ZYNQMP_NUM_PCMS; ++i) {
  311. struct snd_soc_dai_link *link = &card->dai_link[i];
  312. link->ops = &zynqmp_dp_ops;
  313. link->name = audio->link_names[i];
  314. link->stream_name = audio->link_names[i];
  315. link->cpus = &audio->components[i].cpu;
  316. link->num_cpus = 1;
  317. link->cpus[0].dai_name = audio->dai_name;
  318. link->codecs = &snd_soc_dummy_dlc;
  319. link->num_codecs = 1;
  320. link->platforms = &audio->components[i].platform;
  321. link->num_platforms = 1;
  322. link->platforms[0].name = audio->pcm_names[i];
  323. }
  324. /*
  325. * HACK: devm_snd_soc_register_card() overwrites current drvdata
  326. * so we need to hack it back.
  327. */
  328. dev_data = dev_get_drvdata(dev);
  329. ret = devm_snd_soc_register_card(dev, card);
  330. dev_set_drvdata(dev, dev_data);
  331. if (ret) {
  332. /*
  333. * As older dtbs may not have the audio channel dmas defined,
  334. * instead of returning an error here we'll continue and just
  335. * mark the audio as disabled.
  336. */
  337. dev_err(dev, "Failed to register sound card, disabling audio support\n");
  338. devm_kfree(dev, audio);
  339. dpsub->audio = NULL;
  340. return 0;
  341. }
  342. return 0;
  343. }
  344. void zynqmp_audio_uninit(struct zynqmp_dpsub *dpsub)
  345. {
  346. struct zynqmp_dpsub_audio *audio = dpsub->audio;
  347. if (!audio)
  348. return;
  349. if (!dpsub->aud_clk)
  350. return;
  351. mutex_destroy(&audio->enable_lock);
  352. }