hdmi-codec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * ALSA SoC codec for HDMI encoder drivers
  3. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
  4. * Author: Jyri Sarha <jsarha@ti.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/string.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <sound/pcm_drm_eld.h>
  22. #include <sound/hdmi-codec.h>
  23. #include <sound/pcm_iec958.h>
  24. #include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
  25. struct hdmi_codec_priv {
  26. struct hdmi_codec_pdata hcd;
  27. struct snd_soc_dai_driver *daidrv;
  28. struct hdmi_codec_daifmt daifmt[2];
  29. struct mutex current_stream_lock;
  30. struct snd_pcm_substream *current_stream;
  31. struct snd_pcm_hw_constraint_list ratec;
  32. uint8_t eld[MAX_ELD_BYTES];
  33. };
  34. static const struct snd_soc_dapm_widget hdmi_widgets[] = {
  35. SND_SOC_DAPM_OUTPUT("TX"),
  36. };
  37. static const struct snd_soc_dapm_route hdmi_routes[] = {
  38. { "TX", NULL, "Playback" },
  39. };
  40. enum {
  41. DAI_ID_I2S = 0,
  42. DAI_ID_SPDIF,
  43. };
  44. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  45. struct snd_ctl_elem_info *uinfo)
  46. {
  47. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  48. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  49. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  50. uinfo->count = sizeof(hcp->eld);
  51. return 0;
  52. }
  53. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  54. struct snd_ctl_elem_value *ucontrol)
  55. {
  56. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  57. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  58. memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
  59. return 0;
  60. }
  61. static const struct snd_kcontrol_new hdmi_controls[] = {
  62. {
  63. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  64. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  65. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  66. .name = "ELD",
  67. .info = hdmi_eld_ctl_info,
  68. .get = hdmi_eld_ctl_get,
  69. },
  70. };
  71. static int hdmi_codec_new_stream(struct snd_pcm_substream *substream,
  72. struct snd_soc_dai *dai)
  73. {
  74. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  75. int ret = 0;
  76. mutex_lock(&hcp->current_stream_lock);
  77. if (!hcp->current_stream) {
  78. hcp->current_stream = substream;
  79. } else if (hcp->current_stream != substream) {
  80. dev_err(dai->dev, "Only one simultaneous stream supported!\n");
  81. ret = -EINVAL;
  82. }
  83. mutex_unlock(&hcp->current_stream_lock);
  84. return ret;
  85. }
  86. static int hdmi_codec_startup(struct snd_pcm_substream *substream,
  87. struct snd_soc_dai *dai)
  88. {
  89. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  90. int ret = 0;
  91. dev_dbg(dai->dev, "%s()\n", __func__);
  92. ret = hdmi_codec_new_stream(substream, dai);
  93. if (ret)
  94. return ret;
  95. if (hcp->hcd.ops->audio_startup) {
  96. ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
  97. if (ret) {
  98. mutex_lock(&hcp->current_stream_lock);
  99. hcp->current_stream = NULL;
  100. mutex_unlock(&hcp->current_stream_lock);
  101. return ret;
  102. }
  103. }
  104. if (hcp->hcd.ops->get_eld) {
  105. ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
  106. hcp->eld, sizeof(hcp->eld));
  107. if (!ret) {
  108. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  109. hcp->eld);
  110. if (ret)
  111. return ret;
  112. }
  113. }
  114. return 0;
  115. }
  116. static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
  117. struct snd_soc_dai *dai)
  118. {
  119. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  120. dev_dbg(dai->dev, "%s()\n", __func__);
  121. WARN_ON(hcp->current_stream != substream);
  122. hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
  123. mutex_lock(&hcp->current_stream_lock);
  124. hcp->current_stream = NULL;
  125. mutex_unlock(&hcp->current_stream_lock);
  126. }
  127. static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
  128. struct snd_pcm_hw_params *params,
  129. struct snd_soc_dai *dai)
  130. {
  131. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  132. struct hdmi_codec_params hp = {
  133. .iec = {
  134. .status = { 0 },
  135. .subcode = { 0 },
  136. .pad = 0,
  137. .dig_subframe = { 0 },
  138. }
  139. };
  140. int ret;
  141. dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
  142. params_width(params), params_rate(params),
  143. params_channels(params));
  144. if (params_width(params) > 24)
  145. params->msbits = 24;
  146. ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
  147. sizeof(hp.iec.status));
  148. if (ret < 0) {
  149. dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
  150. ret);
  151. return ret;
  152. }
  153. ret = hdmi_codec_new_stream(substream, dai);
  154. if (ret)
  155. return ret;
  156. hdmi_audio_infoframe_init(&hp.cea);
  157. hp.cea.channels = params_channels(params);
  158. hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  159. hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  160. hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  161. hp.sample_width = params_width(params);
  162. hp.sample_rate = params_rate(params);
  163. hp.channels = params_channels(params);
  164. return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
  165. &hcp->daifmt[dai->id], &hp);
  166. }
  167. static int hdmi_codec_set_fmt(struct snd_soc_dai *dai,
  168. unsigned int fmt)
  169. {
  170. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  171. struct hdmi_codec_daifmt cf = { 0 };
  172. int ret = 0;
  173. dev_dbg(dai->dev, "%s()\n", __func__);
  174. if (dai->id == DAI_ID_SPDIF) {
  175. cf.fmt = HDMI_SPDIF;
  176. } else {
  177. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  178. case SND_SOC_DAIFMT_CBM_CFM:
  179. cf.bit_clk_master = 1;
  180. cf.frame_clk_master = 1;
  181. break;
  182. case SND_SOC_DAIFMT_CBS_CFM:
  183. cf.frame_clk_master = 1;
  184. break;
  185. case SND_SOC_DAIFMT_CBM_CFS:
  186. cf.bit_clk_master = 1;
  187. break;
  188. case SND_SOC_DAIFMT_CBS_CFS:
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  194. case SND_SOC_DAIFMT_NB_NF:
  195. break;
  196. case SND_SOC_DAIFMT_NB_IF:
  197. cf.frame_clk_inv = 1;
  198. break;
  199. case SND_SOC_DAIFMT_IB_NF:
  200. cf.bit_clk_inv = 1;
  201. break;
  202. case SND_SOC_DAIFMT_IB_IF:
  203. cf.frame_clk_inv = 1;
  204. cf.bit_clk_inv = 1;
  205. break;
  206. }
  207. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  208. case SND_SOC_DAIFMT_I2S:
  209. cf.fmt = HDMI_I2S;
  210. break;
  211. case SND_SOC_DAIFMT_DSP_A:
  212. cf.fmt = HDMI_DSP_A;
  213. break;
  214. case SND_SOC_DAIFMT_DSP_B:
  215. cf.fmt = HDMI_DSP_B;
  216. break;
  217. case SND_SOC_DAIFMT_RIGHT_J:
  218. cf.fmt = HDMI_RIGHT_J;
  219. break;
  220. case SND_SOC_DAIFMT_LEFT_J:
  221. cf.fmt = HDMI_LEFT_J;
  222. break;
  223. case SND_SOC_DAIFMT_AC97:
  224. cf.fmt = HDMI_AC97;
  225. break;
  226. default:
  227. dev_err(dai->dev, "Invalid DAI interface format\n");
  228. return -EINVAL;
  229. }
  230. }
  231. hcp->daifmt[dai->id] = cf;
  232. return ret;
  233. }
  234. static int hdmi_codec_digital_mute(struct snd_soc_dai *dai, int mute)
  235. {
  236. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  237. dev_dbg(dai->dev, "%s()\n", __func__);
  238. if (hcp->hcd.ops->digital_mute)
  239. return hcp->hcd.ops->digital_mute(dai->dev->parent,
  240. hcp->hcd.data, mute);
  241. return 0;
  242. }
  243. static const struct snd_soc_dai_ops hdmi_dai_ops = {
  244. .startup = hdmi_codec_startup,
  245. .shutdown = hdmi_codec_shutdown,
  246. .hw_params = hdmi_codec_hw_params,
  247. .set_fmt = hdmi_codec_set_fmt,
  248. .digital_mute = hdmi_codec_digital_mute,
  249. };
  250. #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  251. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  252. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  253. SNDRV_PCM_RATE_192000)
  254. #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  255. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  256. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  257. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  258. /*
  259. * This list is only for formats allowed on the I2S bus. So there is
  260. * some formats listed that are not supported by HDMI interface. For
  261. * instance allowing the 32-bit formats enables 24-precision with CPU
  262. * DAIs that do not support 24-bit formats. If the extra formats cause
  263. * problems, we should add the video side driver an option to disable
  264. * them.
  265. */
  266. #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  267. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  268. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  269. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
  270. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE)
  271. static struct snd_soc_dai_driver hdmi_i2s_dai = {
  272. .name = "i2s-hifi",
  273. .id = DAI_ID_I2S,
  274. .playback = {
  275. .stream_name = "Playback",
  276. .channels_min = 2,
  277. .channels_max = 8,
  278. .rates = HDMI_RATES,
  279. .formats = I2S_FORMATS,
  280. .sig_bits = 24,
  281. },
  282. .ops = &hdmi_dai_ops,
  283. };
  284. static const struct snd_soc_dai_driver hdmi_spdif_dai = {
  285. .name = "spdif-hifi",
  286. .id = DAI_ID_SPDIF,
  287. .playback = {
  288. .stream_name = "Playback",
  289. .channels_min = 2,
  290. .channels_max = 2,
  291. .rates = HDMI_RATES,
  292. .formats = SPDIF_FORMATS,
  293. },
  294. .ops = &hdmi_dai_ops,
  295. };
  296. static struct snd_soc_codec_driver hdmi_codec = {
  297. .controls = hdmi_controls,
  298. .num_controls = ARRAY_SIZE(hdmi_controls),
  299. .dapm_widgets = hdmi_widgets,
  300. .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
  301. .dapm_routes = hdmi_routes,
  302. .num_dapm_routes = ARRAY_SIZE(hdmi_routes),
  303. };
  304. static int hdmi_codec_probe(struct platform_device *pdev)
  305. {
  306. struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
  307. struct device *dev = &pdev->dev;
  308. struct hdmi_codec_priv *hcp;
  309. int dai_count, i = 0;
  310. int ret;
  311. dev_dbg(dev, "%s()\n", __func__);
  312. if (!hcd) {
  313. dev_err(dev, "%s: No plalform data\n", __func__);
  314. return -EINVAL;
  315. }
  316. dai_count = hcd->i2s + hcd->spdif;
  317. if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
  318. !hcd->ops->audio_shutdown) {
  319. dev_err(dev, "%s: Invalid parameters\n", __func__);
  320. return -EINVAL;
  321. }
  322. hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
  323. if (!hcp)
  324. return -ENOMEM;
  325. hcp->hcd = *hcd;
  326. mutex_init(&hcp->current_stream_lock);
  327. hcp->daidrv = devm_kzalloc(dev, dai_count * sizeof(*hcp->daidrv),
  328. GFP_KERNEL);
  329. if (!hcp->daidrv)
  330. return -ENOMEM;
  331. if (hcd->i2s) {
  332. hcp->daidrv[i] = hdmi_i2s_dai;
  333. hcp->daidrv[i].playback.channels_max =
  334. hcd->max_i2s_channels;
  335. i++;
  336. }
  337. if (hcd->spdif)
  338. hcp->daidrv[i] = hdmi_spdif_dai;
  339. ret = snd_soc_register_codec(dev, &hdmi_codec, hcp->daidrv,
  340. dai_count);
  341. if (ret) {
  342. dev_err(dev, "%s: snd_soc_register_codec() failed (%d)\n",
  343. __func__, ret);
  344. return ret;
  345. }
  346. dev_set_drvdata(dev, hcp);
  347. return 0;
  348. }
  349. static int hdmi_codec_remove(struct platform_device *pdev)
  350. {
  351. snd_soc_unregister_codec(&pdev->dev);
  352. return 0;
  353. }
  354. static struct platform_driver hdmi_codec_driver = {
  355. .driver = {
  356. .name = HDMI_CODEC_DRV_NAME,
  357. },
  358. .probe = hdmi_codec_probe,
  359. .remove = hdmi_codec_remove,
  360. };
  361. module_platform_driver(hdmi_codec_driver);
  362. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  363. MODULE_DESCRIPTION("HDMI Audio Codec Driver");
  364. MODULE_LICENSE("GPL");
  365. MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);