omap-hdmi.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * omap-hdmi.c
  3. *
  4. * OMAP ALSA SoC DAI driver for HDMI audio on OMAP4 processors.
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
  6. * Authors: Jorge Candelaria <jorge.candelaria@ti.com>
  7. * Ricardo Neri <ricardo.neri@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/initval.h>
  31. #include <sound/soc.h>
  32. #include <sound/asound.h>
  33. #include <sound/asoundef.h>
  34. #include <video/omapdss.h>
  35. #include "omap-pcm.h"
  36. #include "omap-hdmi.h"
  37. #define DRV_NAME "omap-hdmi-audio-dai"
  38. struct hdmi_priv {
  39. struct omap_pcm_dma_data dma_params;
  40. struct omap_dss_audio dss_audio;
  41. struct snd_aes_iec958 iec;
  42. struct snd_cea_861_aud_if cea;
  43. struct omap_dss_device *dssdev;
  44. };
  45. static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
  46. struct snd_soc_dai *dai)
  47. {
  48. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  49. int err;
  50. /*
  51. * Make sure that the period bytes are multiple of the DMA packet size.
  52. * Largest packet size we use is 32 32-bit words = 128 bytes
  53. */
  54. err = snd_pcm_hw_constraint_step(substream->runtime, 0,
  55. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
  56. if (err < 0) {
  57. dev_err(dai->dev, "could not apply constraint\n");
  58. return err;
  59. }
  60. if (!priv->dssdev->driver->audio_supported(priv->dssdev)) {
  61. dev_err(dai->dev, "audio not supported\n");
  62. return -ENODEV;
  63. }
  64. snd_soc_dai_set_dma_data(dai, substream, &priv->dma_params);
  65. return 0;
  66. }
  67. static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
  68. struct snd_soc_dai *dai)
  69. {
  70. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  71. return priv->dssdev->driver->audio_enable(priv->dssdev);
  72. }
  73. static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct snd_soc_dai *dai)
  76. {
  77. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  78. struct snd_aes_iec958 *iec = &priv->iec;
  79. struct snd_cea_861_aud_if *cea = &priv->cea;
  80. struct omap_pcm_dma_data *dma_data;
  81. int err = 0;
  82. dma_data = snd_soc_dai_get_dma_data(dai, substream);
  83. switch (params_format(params)) {
  84. case SNDRV_PCM_FORMAT_S16_LE:
  85. dma_data->packet_size = 16;
  86. break;
  87. case SNDRV_PCM_FORMAT_S24_LE:
  88. dma_data->packet_size = 32;
  89. break;
  90. default:
  91. dev_err(dai->dev, "format not supported!\n");
  92. return -EINVAL;
  93. }
  94. dma_data->data_type = 32;
  95. /*
  96. * fill the IEC-60958 channel status word
  97. */
  98. /* initialize the word bytes */
  99. memset(iec->status, 0, sizeof(iec->status));
  100. /* specify IEC-60958-3 (commercial use) */
  101. iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
  102. /* specify that the audio is LPCM*/
  103. iec->status[0] &= ~IEC958_AES0_NONAUDIO;
  104. iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  105. iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
  106. iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
  107. iec->status[1] = IEC958_AES1_CON_GENERAL;
  108. iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
  109. iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
  110. switch (params_rate(params)) {
  111. case 32000:
  112. iec->status[3] |= IEC958_AES3_CON_FS_32000;
  113. break;
  114. case 44100:
  115. iec->status[3] |= IEC958_AES3_CON_FS_44100;
  116. break;
  117. case 48000:
  118. iec->status[3] |= IEC958_AES3_CON_FS_48000;
  119. break;
  120. case 88200:
  121. iec->status[3] |= IEC958_AES3_CON_FS_88200;
  122. break;
  123. case 96000:
  124. iec->status[3] |= IEC958_AES3_CON_FS_96000;
  125. break;
  126. case 176400:
  127. iec->status[3] |= IEC958_AES3_CON_FS_176400;
  128. break;
  129. case 192000:
  130. iec->status[3] |= IEC958_AES3_CON_FS_192000;
  131. break;
  132. default:
  133. dev_err(dai->dev, "rate not supported!\n");
  134. return -EINVAL;
  135. }
  136. /* specify the clock accuracy */
  137. iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
  138. /*
  139. * specify the word length. The same word length value can mean
  140. * two different lengths. Hence, we need to specify the maximum
  141. * word length as well.
  142. */
  143. switch (params_format(params)) {
  144. case SNDRV_PCM_FORMAT_S16_LE:
  145. iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
  146. iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
  147. break;
  148. case SNDRV_PCM_FORMAT_S24_LE:
  149. iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
  150. iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
  151. break;
  152. default:
  153. dev_err(dai->dev, "format not supported!\n");
  154. return -EINVAL;
  155. }
  156. /*
  157. * Fill the CEA-861 audio infoframe (see spec for details)
  158. */
  159. cea->db1_ct_cc = (params_channels(params) - 1)
  160. & CEA861_AUDIO_INFOFRAME_DB1CC;
  161. cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
  162. cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
  163. cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
  164. cea->db3 = 0; /* not used, all zeros */
  165. /*
  166. * The OMAP HDMI IP requires to use the 8-channel channel code when
  167. * transmitting more than two channels.
  168. */
  169. if (params_channels(params) == 2)
  170. cea->db4_ca = 0x0;
  171. else
  172. cea->db4_ca = 0x13;
  173. cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
  174. /* the expression is trivial but makes clear what we are doing */
  175. cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
  176. priv->dss_audio.iec = iec;
  177. priv->dss_audio.cea = cea;
  178. err = priv->dssdev->driver->audio_config(priv->dssdev,
  179. &priv->dss_audio);
  180. return err;
  181. }
  182. static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  183. struct snd_soc_dai *dai)
  184. {
  185. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  186. int err = 0;
  187. switch (cmd) {
  188. case SNDRV_PCM_TRIGGER_START:
  189. case SNDRV_PCM_TRIGGER_RESUME:
  190. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  191. err = priv->dssdev->driver->audio_start(priv->dssdev);
  192. break;
  193. case SNDRV_PCM_TRIGGER_STOP:
  194. case SNDRV_PCM_TRIGGER_SUSPEND:
  195. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  196. priv->dssdev->driver->audio_stop(priv->dssdev);
  197. break;
  198. default:
  199. err = -EINVAL;
  200. }
  201. return err;
  202. }
  203. static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
  204. struct snd_soc_dai *dai)
  205. {
  206. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  207. priv->dssdev->driver->audio_disable(priv->dssdev);
  208. }
  209. static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
  210. .startup = omap_hdmi_dai_startup,
  211. .hw_params = omap_hdmi_dai_hw_params,
  212. .prepare = omap_hdmi_dai_prepare,
  213. .trigger = omap_hdmi_dai_trigger,
  214. .shutdown = omap_hdmi_dai_shutdown,
  215. };
  216. static struct snd_soc_dai_driver omap_hdmi_dai = {
  217. .playback = {
  218. .channels_min = 2,
  219. .channels_max = 8,
  220. .rates = OMAP_HDMI_RATES,
  221. .formats = OMAP_HDMI_FORMATS,
  222. },
  223. .ops = &omap_hdmi_dai_ops,
  224. };
  225. static int omap_hdmi_probe(struct platform_device *pdev)
  226. {
  227. int ret;
  228. struct resource *hdmi_rsrc;
  229. struct hdmi_priv *hdmi_data;
  230. bool hdmi_dev_found = false;
  231. hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
  232. if (hdmi_data == NULL) {
  233. dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
  234. return -ENOMEM;
  235. }
  236. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. if (!hdmi_rsrc) {
  238. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM HDMI\n");
  239. return -ENODEV;
  240. }
  241. hdmi_data->dma_params.port_addr = hdmi_rsrc->start
  242. + OMAP_HDMI_AUDIO_DMA_PORT;
  243. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  244. if (!hdmi_rsrc) {
  245. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
  246. return -ENODEV;
  247. }
  248. hdmi_data->dma_params.dma_req = hdmi_rsrc->start;
  249. hdmi_data->dma_params.name = "HDMI playback";
  250. /*
  251. * TODO: We assume that there is only one DSS HDMI device. Future
  252. * OMAP implementations may support more than one HDMI devices and
  253. * we should provided separate audio support for all of them.
  254. */
  255. /* Find an HDMI device. */
  256. for_each_dss_dev(hdmi_data->dssdev) {
  257. omap_dss_get_device(hdmi_data->dssdev);
  258. if (!hdmi_data->dssdev->driver) {
  259. omap_dss_put_device(hdmi_data->dssdev);
  260. continue;
  261. }
  262. if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
  263. hdmi_dev_found = true;
  264. break;
  265. }
  266. }
  267. if (!hdmi_dev_found) {
  268. dev_err(&pdev->dev, "no driver for HDMI display found\n");
  269. return -ENODEV;
  270. }
  271. dev_set_drvdata(&pdev->dev, hdmi_data);
  272. ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
  273. return ret;
  274. }
  275. static int omap_hdmi_remove(struct platform_device *pdev)
  276. {
  277. struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
  278. snd_soc_unregister_dai(&pdev->dev);
  279. if (hdmi_data == NULL) {
  280. dev_err(&pdev->dev, "cannot obtain HDMi data\n");
  281. return -ENODEV;
  282. }
  283. omap_dss_put_device(hdmi_data->dssdev);
  284. return 0;
  285. }
  286. static struct platform_driver hdmi_dai_driver = {
  287. .driver = {
  288. .name = DRV_NAME,
  289. .owner = THIS_MODULE,
  290. },
  291. .probe = omap_hdmi_probe,
  292. .remove = omap_hdmi_remove,
  293. };
  294. module_platform_driver(hdmi_dai_driver);
  295. MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
  296. MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
  297. MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS("platform:" DRV_NAME);