hdmi-codec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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_device {
  26. struct device *dev;
  27. struct list_head list;
  28. int cnt;
  29. };
  30. #define pos_to_hdmi_device(pos) container_of((pos), struct hdmi_device, list)
  31. LIST_HEAD(hdmi_device_list);
  32. #define DAI_NAME_SIZE 16
  33. struct hdmi_codec_priv {
  34. struct hdmi_codec_pdata hcd;
  35. struct snd_soc_dai_driver *daidrv;
  36. struct hdmi_codec_daifmt daifmt[2];
  37. struct mutex current_stream_lock;
  38. struct snd_pcm_substream *current_stream;
  39. struct snd_pcm_hw_constraint_list ratec;
  40. uint8_t eld[MAX_ELD_BYTES];
  41. };
  42. static const struct snd_soc_dapm_widget hdmi_widgets[] = {
  43. SND_SOC_DAPM_OUTPUT("TX"),
  44. };
  45. static const struct snd_soc_dapm_route hdmi_routes[] = {
  46. { "TX", NULL, "Playback" },
  47. };
  48. enum {
  49. DAI_ID_I2S = 0,
  50. DAI_ID_SPDIF,
  51. };
  52. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  53. struct snd_ctl_elem_info *uinfo)
  54. {
  55. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  56. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  57. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  58. uinfo->count = sizeof(hcp->eld);
  59. return 0;
  60. }
  61. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  62. struct snd_ctl_elem_value *ucontrol)
  63. {
  64. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  65. struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
  66. memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
  67. return 0;
  68. }
  69. static const struct snd_kcontrol_new hdmi_controls[] = {
  70. {
  71. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  72. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  73. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  74. .name = "ELD",
  75. .info = hdmi_eld_ctl_info,
  76. .get = hdmi_eld_ctl_get,
  77. },
  78. };
  79. static int hdmi_codec_new_stream(struct snd_pcm_substream *substream,
  80. struct snd_soc_dai *dai)
  81. {
  82. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  83. int ret = 0;
  84. mutex_lock(&hcp->current_stream_lock);
  85. if (!hcp->current_stream) {
  86. hcp->current_stream = substream;
  87. } else if (hcp->current_stream != substream) {
  88. dev_err(dai->dev, "Only one simultaneous stream supported!\n");
  89. ret = -EINVAL;
  90. }
  91. mutex_unlock(&hcp->current_stream_lock);
  92. return ret;
  93. }
  94. static int hdmi_codec_startup(struct snd_pcm_substream *substream,
  95. struct snd_soc_dai *dai)
  96. {
  97. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  98. int ret = 0;
  99. dev_dbg(dai->dev, "%s()\n", __func__);
  100. ret = hdmi_codec_new_stream(substream, dai);
  101. if (ret)
  102. return ret;
  103. if (hcp->hcd.ops->audio_startup) {
  104. ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
  105. if (ret) {
  106. mutex_lock(&hcp->current_stream_lock);
  107. hcp->current_stream = NULL;
  108. mutex_unlock(&hcp->current_stream_lock);
  109. return ret;
  110. }
  111. }
  112. if (hcp->hcd.ops->get_eld) {
  113. ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
  114. hcp->eld, sizeof(hcp->eld));
  115. if (!ret) {
  116. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  117. hcp->eld);
  118. if (ret)
  119. return ret;
  120. }
  121. }
  122. return 0;
  123. }
  124. static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
  125. struct snd_soc_dai *dai)
  126. {
  127. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  128. dev_dbg(dai->dev, "%s()\n", __func__);
  129. WARN_ON(hcp->current_stream != substream);
  130. hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
  131. mutex_lock(&hcp->current_stream_lock);
  132. hcp->current_stream = NULL;
  133. mutex_unlock(&hcp->current_stream_lock);
  134. }
  135. static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
  136. struct snd_pcm_hw_params *params,
  137. struct snd_soc_dai *dai)
  138. {
  139. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  140. struct hdmi_codec_params hp = {
  141. .iec = {
  142. .status = { 0 },
  143. .subcode = { 0 },
  144. .pad = 0,
  145. .dig_subframe = { 0 },
  146. }
  147. };
  148. int ret;
  149. dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
  150. params_width(params), params_rate(params),
  151. params_channels(params));
  152. if (params_width(params) > 24)
  153. params->msbits = 24;
  154. ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
  155. sizeof(hp.iec.status));
  156. if (ret < 0) {
  157. dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
  158. ret);
  159. return ret;
  160. }
  161. ret = hdmi_codec_new_stream(substream, dai);
  162. if (ret)
  163. return ret;
  164. hdmi_audio_infoframe_init(&hp.cea);
  165. hp.cea.channels = params_channels(params);
  166. hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  167. hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  168. hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  169. hp.sample_width = params_width(params);
  170. hp.sample_rate = params_rate(params);
  171. hp.channels = params_channels(params);
  172. return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
  173. &hcp->daifmt[dai->id], &hp);
  174. }
  175. static int hdmi_codec_set_fmt(struct snd_soc_dai *dai,
  176. unsigned int fmt)
  177. {
  178. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  179. struct hdmi_codec_daifmt cf = { 0 };
  180. int ret = 0;
  181. dev_dbg(dai->dev, "%s()\n", __func__);
  182. if (dai->id == DAI_ID_SPDIF) {
  183. cf.fmt = HDMI_SPDIF;
  184. } else {
  185. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  186. case SND_SOC_DAIFMT_CBM_CFM:
  187. cf.bit_clk_master = 1;
  188. cf.frame_clk_master = 1;
  189. break;
  190. case SND_SOC_DAIFMT_CBS_CFM:
  191. cf.frame_clk_master = 1;
  192. break;
  193. case SND_SOC_DAIFMT_CBM_CFS:
  194. cf.bit_clk_master = 1;
  195. break;
  196. case SND_SOC_DAIFMT_CBS_CFS:
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  202. case SND_SOC_DAIFMT_NB_NF:
  203. break;
  204. case SND_SOC_DAIFMT_NB_IF:
  205. cf.frame_clk_inv = 1;
  206. break;
  207. case SND_SOC_DAIFMT_IB_NF:
  208. cf.bit_clk_inv = 1;
  209. break;
  210. case SND_SOC_DAIFMT_IB_IF:
  211. cf.frame_clk_inv = 1;
  212. cf.bit_clk_inv = 1;
  213. break;
  214. }
  215. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  216. case SND_SOC_DAIFMT_I2S:
  217. cf.fmt = HDMI_I2S;
  218. break;
  219. case SND_SOC_DAIFMT_DSP_A:
  220. cf.fmt = HDMI_DSP_A;
  221. break;
  222. case SND_SOC_DAIFMT_DSP_B:
  223. cf.fmt = HDMI_DSP_B;
  224. break;
  225. case SND_SOC_DAIFMT_RIGHT_J:
  226. cf.fmt = HDMI_RIGHT_J;
  227. break;
  228. case SND_SOC_DAIFMT_LEFT_J:
  229. cf.fmt = HDMI_LEFT_J;
  230. break;
  231. case SND_SOC_DAIFMT_AC97:
  232. cf.fmt = HDMI_AC97;
  233. break;
  234. default:
  235. dev_err(dai->dev, "Invalid DAI interface format\n");
  236. return -EINVAL;
  237. }
  238. }
  239. hcp->daifmt[dai->id] = cf;
  240. return ret;
  241. }
  242. static int hdmi_codec_digital_mute(struct snd_soc_dai *dai, int mute)
  243. {
  244. struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
  245. dev_dbg(dai->dev, "%s()\n", __func__);
  246. if (hcp->hcd.ops->digital_mute)
  247. return hcp->hcd.ops->digital_mute(dai->dev->parent,
  248. hcp->hcd.data, mute);
  249. return 0;
  250. }
  251. static const struct snd_soc_dai_ops hdmi_dai_ops = {
  252. .startup = hdmi_codec_startup,
  253. .shutdown = hdmi_codec_shutdown,
  254. .hw_params = hdmi_codec_hw_params,
  255. .set_fmt = hdmi_codec_set_fmt,
  256. .digital_mute = hdmi_codec_digital_mute,
  257. };
  258. #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  259. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  260. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  261. SNDRV_PCM_RATE_192000)
  262. #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  263. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  264. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  265. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
  266. /*
  267. * This list is only for formats allowed on the I2S bus. So there is
  268. * some formats listed that are not supported by HDMI interface. For
  269. * instance allowing the 32-bit formats enables 24-precision with CPU
  270. * DAIs that do not support 24-bit formats. If the extra formats cause
  271. * problems, we should add the video side driver an option to disable
  272. * them.
  273. */
  274. #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
  275. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
  276. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
  277. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
  278. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE)
  279. static struct snd_soc_dai_driver hdmi_i2s_dai = {
  280. .id = DAI_ID_I2S,
  281. .playback = {
  282. .stream_name = "Playback",
  283. .channels_min = 2,
  284. .channels_max = 8,
  285. .rates = HDMI_RATES,
  286. .formats = I2S_FORMATS,
  287. .sig_bits = 24,
  288. },
  289. .ops = &hdmi_dai_ops,
  290. };
  291. static const struct snd_soc_dai_driver hdmi_spdif_dai = {
  292. .id = DAI_ID_SPDIF,
  293. .playback = {
  294. .stream_name = "Playback",
  295. .channels_min = 2,
  296. .channels_max = 2,
  297. .rates = HDMI_RATES,
  298. .formats = SPDIF_FORMATS,
  299. },
  300. .ops = &hdmi_dai_ops,
  301. };
  302. static char hdmi_dai_name[][DAI_NAME_SIZE] = {
  303. "hdmi-hifi.0",
  304. "hdmi-hifi.1",
  305. "hdmi-hifi.2",
  306. "hdmi-hifi.3",
  307. };
  308. static int hdmi_of_xlate_dai_name(struct snd_soc_component *component,
  309. struct of_phandle_args *args,
  310. const char **dai_name)
  311. {
  312. int id;
  313. if (args->args_count)
  314. id = args->args[0];
  315. else
  316. id = 0;
  317. if (id < ARRAY_SIZE(hdmi_dai_name)) {
  318. *dai_name = hdmi_dai_name[id];
  319. return 0;
  320. }
  321. return -EAGAIN;
  322. }
  323. static struct snd_soc_codec_driver hdmi_codec = {
  324. .component_driver = {
  325. .controls = hdmi_controls,
  326. .num_controls = ARRAY_SIZE(hdmi_controls),
  327. .dapm_widgets = hdmi_widgets,
  328. .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
  329. .dapm_routes = hdmi_routes,
  330. .num_dapm_routes = ARRAY_SIZE(hdmi_routes),
  331. .of_xlate_dai_name = hdmi_of_xlate_dai_name,
  332. },
  333. };
  334. static int hdmi_codec_probe(struct platform_device *pdev)
  335. {
  336. struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
  337. struct device *dev = &pdev->dev;
  338. struct hdmi_codec_priv *hcp;
  339. struct hdmi_device *hd;
  340. struct list_head *pos;
  341. int dai_count, i = 0;
  342. int ret;
  343. dev_dbg(dev, "%s()\n", __func__);
  344. if (!hcd) {
  345. dev_err(dev, "%s: No plalform data\n", __func__);
  346. return -EINVAL;
  347. }
  348. dai_count = hcd->i2s + hcd->spdif;
  349. if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
  350. !hcd->ops->audio_shutdown) {
  351. dev_err(dev, "%s: Invalid parameters\n", __func__);
  352. return -EINVAL;
  353. }
  354. hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
  355. if (!hcp)
  356. return -ENOMEM;
  357. hd = NULL;
  358. list_for_each(pos, &hdmi_device_list) {
  359. struct hdmi_device *tmp = pos_to_hdmi_device(pos);
  360. if (tmp->dev == dev->parent) {
  361. hd = tmp;
  362. break;
  363. }
  364. }
  365. if (!hd) {
  366. hd = devm_kzalloc(dev, sizeof(*hd), GFP_KERNEL);
  367. if (!hd)
  368. return -ENOMEM;
  369. hd->dev = dev->parent;
  370. list_add_tail(&hd->list, &hdmi_device_list);
  371. }
  372. if (hd->cnt >= ARRAY_SIZE(hdmi_dai_name)) {
  373. dev_err(dev, "too many hdmi codec are deteced\n");
  374. return -EINVAL;
  375. }
  376. hcp->hcd = *hcd;
  377. mutex_init(&hcp->current_stream_lock);
  378. hcp->daidrv = devm_kzalloc(dev, dai_count * sizeof(*hcp->daidrv),
  379. GFP_KERNEL);
  380. if (!hcp->daidrv)
  381. return -ENOMEM;
  382. if (hcd->i2s) {
  383. hcp->daidrv[i] = hdmi_i2s_dai;
  384. hcp->daidrv[i].playback.channels_max =
  385. hcd->max_i2s_channels;
  386. hcp->daidrv[i].name = hdmi_dai_name[hd->cnt++];
  387. i++;
  388. }
  389. if (hcd->spdif) {
  390. hcp->daidrv[i] = hdmi_spdif_dai;
  391. hcp->daidrv[i].name = hdmi_dai_name[hd->cnt++];
  392. }
  393. ret = snd_soc_register_codec(dev, &hdmi_codec, hcp->daidrv,
  394. dai_count);
  395. if (ret) {
  396. dev_err(dev, "%s: snd_soc_register_codec() failed (%d)\n",
  397. __func__, ret);
  398. return ret;
  399. }
  400. dev_set_drvdata(dev, hcp);
  401. return 0;
  402. }
  403. static int hdmi_codec_remove(struct platform_device *pdev)
  404. {
  405. snd_soc_unregister_codec(&pdev->dev);
  406. return 0;
  407. }
  408. static struct platform_driver hdmi_codec_driver = {
  409. .driver = {
  410. .name = HDMI_CODEC_DRV_NAME,
  411. },
  412. .probe = hdmi_codec_probe,
  413. .remove = hdmi_codec_remove,
  414. };
  415. module_platform_driver(hdmi_codec_driver);
  416. MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
  417. MODULE_DESCRIPTION("HDMI Audio Codec Driver");
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);