omap-hdmi.c 9.4 KB

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