omap-hdmi.c 9.3 KB

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