omap-hdmi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <plat/dma.h>
  36. #include "omap-pcm.h"
  37. #include "omap-hdmi.h"
  38. #define DRV_NAME "omap-hdmi-audio-dai"
  39. struct hdmi_priv {
  40. struct omap_pcm_dma_data dma_params;
  41. struct omap_dss_audio dss_audio;
  42. struct snd_aes_iec958 iec;
  43. struct snd_cea_861_aud_if cea;
  44. struct omap_dss_device *dssdev;
  45. };
  46. static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
  47. struct snd_soc_dai *dai)
  48. {
  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. return err;
  58. return 0;
  59. }
  60. static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
  61. struct snd_soc_dai *dai)
  62. {
  63. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  64. return priv->dssdev->driver->audio_enable(priv->dssdev);
  65. }
  66. static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
  67. struct snd_pcm_hw_params *params,
  68. struct snd_soc_dai *dai)
  69. {
  70. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  71. struct snd_aes_iec958 *iec = &priv->iec;
  72. struct snd_cea_861_aud_if *cea = &priv->cea;
  73. int err = 0;
  74. switch (params_format(params)) {
  75. case SNDRV_PCM_FORMAT_S16_LE:
  76. priv->dma_params.packet_size = 16;
  77. break;
  78. case SNDRV_PCM_FORMAT_S24_LE:
  79. priv->dma_params.packet_size = 32;
  80. break;
  81. default:
  82. dev_err(dai->dev, "format not supported!\n");
  83. return -EINVAL;
  84. }
  85. priv->dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
  86. snd_soc_dai_set_dma_data(dai, substream,
  87. &priv->dma_params);
  88. /*
  89. * fill the IEC-60958 channel status word
  90. */
  91. /* specify IEC-60958-3 (commercial use) */
  92. iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
  93. /* specify that the audio is LPCM*/
  94. iec->status[0] &= ~IEC958_AES0_NONAUDIO;
  95. iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
  96. iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
  97. iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
  98. iec->status[1] = IEC958_AES1_CON_GENERAL;
  99. iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
  100. iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
  101. switch (params_rate(params)) {
  102. case 32000:
  103. iec->status[3] |= IEC958_AES3_CON_FS_32000;
  104. break;
  105. case 44100:
  106. iec->status[3] |= IEC958_AES3_CON_FS_44100;
  107. break;
  108. case 48000:
  109. iec->status[3] |= IEC958_AES3_CON_FS_48000;
  110. break;
  111. case 88200:
  112. iec->status[3] |= IEC958_AES3_CON_FS_88200;
  113. break;
  114. case 96000:
  115. iec->status[3] |= IEC958_AES3_CON_FS_96000;
  116. break;
  117. case 176400:
  118. iec->status[3] |= IEC958_AES3_CON_FS_176400;
  119. break;
  120. case 192000:
  121. iec->status[3] |= IEC958_AES3_CON_FS_192000;
  122. break;
  123. default:
  124. dev_err(dai->dev, "rate not supported!\n");
  125. return -EINVAL;
  126. }
  127. /* specify the clock accuracy */
  128. iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
  129. /*
  130. * specify the word length. The same word length value can mean
  131. * two different lengths. Hence, we need to specify the maximum
  132. * word length as well.
  133. */
  134. switch (params_format(params)) {
  135. case SNDRV_PCM_FORMAT_S16_LE:
  136. iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
  137. iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
  138. break;
  139. case SNDRV_PCM_FORMAT_S24_LE:
  140. iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
  141. iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
  142. break;
  143. default:
  144. dev_err(dai->dev, "format not supported!\n");
  145. return -EINVAL;
  146. }
  147. /*
  148. * Fill the CEA-861 audio infoframe (see spec for details)
  149. */
  150. cea->db1_ct_cc = (params_channels(params) - 1)
  151. & CEA861_AUDIO_INFOFRAME_DB1CC;
  152. cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
  153. cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
  154. cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
  155. cea->db3 = 0; /* not used, all zeros */
  156. /*
  157. * The OMAP HDMI IP requires to use the 8-channel channel code when
  158. * transmitting more than two channels.
  159. */
  160. if (params_channels(params) == 2)
  161. cea->db4_ca = 0x0;
  162. else
  163. cea->db4_ca = 0x13;
  164. cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
  165. /* the expression is trivial but makes clear what we are doing */
  166. cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
  167. priv->dss_audio.iec = iec;
  168. priv->dss_audio.cea = cea;
  169. err = priv->dssdev->driver->audio_config(priv->dssdev,
  170. &priv->dss_audio);
  171. return err;
  172. }
  173. static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  174. struct snd_soc_dai *dai)
  175. {
  176. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  177. int err = 0;
  178. switch (cmd) {
  179. case SNDRV_PCM_TRIGGER_START:
  180. case SNDRV_PCM_TRIGGER_RESUME:
  181. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  182. err = priv->dssdev->driver->audio_start(priv->dssdev);
  183. break;
  184. case SNDRV_PCM_TRIGGER_STOP:
  185. case SNDRV_PCM_TRIGGER_SUSPEND:
  186. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  187. priv->dssdev->driver->audio_stop(priv->dssdev);
  188. break;
  189. default:
  190. err = -EINVAL;
  191. }
  192. return err;
  193. }
  194. static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
  195. struct snd_soc_dai *dai)
  196. {
  197. struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
  198. priv->dssdev->driver->audio_disable(priv->dssdev);
  199. }
  200. static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
  201. .startup = omap_hdmi_dai_startup,
  202. .hw_params = omap_hdmi_dai_hw_params,
  203. .prepare = omap_hdmi_dai_prepare,
  204. .trigger = omap_hdmi_dai_trigger,
  205. .shutdown = omap_hdmi_dai_shutdown,
  206. };
  207. static struct snd_soc_dai_driver omap_hdmi_dai = {
  208. .playback = {
  209. .channels_min = 2,
  210. .channels_max = 2,
  211. .rates = OMAP_HDMI_RATES,
  212. .formats = OMAP_HDMI_FORMATS,
  213. },
  214. .ops = &omap_hdmi_dai_ops,
  215. };
  216. static __devinit int omap_hdmi_probe(struct platform_device *pdev)
  217. {
  218. int ret;
  219. struct resource *hdmi_rsrc;
  220. struct hdmi_priv *hdmi_data;
  221. bool hdmi_dev_found = false;
  222. hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
  223. if (hdmi_data == NULL) {
  224. dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
  225. return -ENOMEM;
  226. }
  227. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  228. if (!hdmi_rsrc) {
  229. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM HDMI\n");
  230. return -ENODEV;
  231. }
  232. hdmi_data->dma_params.port_addr = hdmi_rsrc->start
  233. + OMAP_HDMI_AUDIO_DMA_PORT;
  234. hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  235. if (!hdmi_rsrc) {
  236. dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
  237. return -ENODEV;
  238. }
  239. hdmi_data->dma_params.dma_req = hdmi_rsrc->start;
  240. hdmi_data->dma_params.name = "HDMI playback";
  241. hdmi_data->dma_params.sync_mode = OMAP_DMA_SYNC_PACKET;
  242. /*
  243. * TODO: We assume that there is only one DSS HDMI device. Future
  244. * OMAP implementations may support more than one HDMI devices and
  245. * we should provided separate audio support for all of them.
  246. */
  247. /* Find an HDMI device. */
  248. for_each_dss_dev(hdmi_data->dssdev) {
  249. omap_dss_get_device(hdmi_data->dssdev);
  250. if (!hdmi_data->dssdev->driver) {
  251. omap_dss_put_device(hdmi_data->dssdev);
  252. continue;
  253. }
  254. if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
  255. hdmi_dev_found = true;
  256. break;
  257. }
  258. }
  259. if (!hdmi_dev_found) {
  260. dev_err(&pdev->dev, "no driver for HDMI display found\n");
  261. return -ENODEV;
  262. }
  263. dev_set_drvdata(&pdev->dev, hdmi_data);
  264. ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
  265. return ret;
  266. }
  267. static int __devexit omap_hdmi_remove(struct platform_device *pdev)
  268. {
  269. struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
  270. snd_soc_unregister_dai(&pdev->dev);
  271. if (hdmi_data == NULL) {
  272. dev_err(&pdev->dev, "cannot obtain HDMi data\n");
  273. return -ENODEV;
  274. }
  275. omap_dss_put_device(hdmi_data->dssdev);
  276. return 0;
  277. }
  278. static struct platform_driver hdmi_dai_driver = {
  279. .driver = {
  280. .name = DRV_NAME,
  281. .owner = THIS_MODULE,
  282. },
  283. .probe = omap_hdmi_probe,
  284. .remove = __devexit_p(omap_hdmi_remove),
  285. };
  286. module_platform_driver(hdmi_dai_driver);
  287. MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
  288. MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
  289. MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
  290. MODULE_LICENSE("GPL");
  291. MODULE_ALIAS("platform:" DRV_NAME);