omap-hdmi.c 9.1 KB

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