1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * omap-hdmi-audio.c -- OMAP4+ DSS HDMI audio support library 4 * 5 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com 6 * 7 * Author: Jyri Sarha <jsarha@ti.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/err.h> 13 #include <linux/string.h> 14 #include <linux/platform_device.h> 15 #include <sound/soc.h> 16 #include <sound/pcm_params.h> 17 #include <sound/dmaengine_pcm.h> 18 #include <uapi/sound/asound.h> 19 #include <sound/asoundef.h> 20 #include <sound/omap-hdmi-audio.h> 21 22 #include "sdma-pcm.h" 23 24 #define DRV_NAME "omap-hdmi-audio" 25 26 struct hdmi_audio_data { 27 struct snd_soc_card *card; 28 29 const struct omap_hdmi_audio_ops *ops; 30 struct device *dssdev; 31 struct snd_dmaengine_dai_dma_data dma_data; 32 struct omap_dss_audio dss_audio; 33 struct snd_aes_iec958 iec; 34 struct snd_cea_861_aud_if cea; 35 36 struct mutex current_stream_lock; 37 struct snd_pcm_substream *current_stream; 38 }; 39 40 static 41 struct hdmi_audio_data *card_drvdata_substream(struct snd_pcm_substream *ss) 42 { 43 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(ss); 44 45 return snd_soc_card_get_drvdata(rtd->card); 46 } 47 48 static void hdmi_dai_abort(struct device *dev) 49 { 50 struct hdmi_audio_data *ad = dev_get_drvdata(dev); 51 52 guard(mutex)(&ad->current_stream_lock); 53 if (ad->current_stream && ad->current_stream->runtime && 54 snd_pcm_running(ad->current_stream)) { 55 dev_err(dev, "HDMI display disabled, aborting playback\n"); 56 snd_pcm_stream_lock_irq(ad->current_stream); 57 snd_pcm_stop(ad->current_stream, SNDRV_PCM_STATE_DISCONNECTED); 58 snd_pcm_stream_unlock_irq(ad->current_stream); 59 } 60 } 61 62 static int hdmi_dai_startup(struct snd_pcm_substream *substream, 63 struct snd_soc_dai *dai) 64 { 65 struct hdmi_audio_data *ad = card_drvdata_substream(substream); 66 int ret; 67 /* 68 * Make sure that the period bytes are multiple of the DMA packet size. 69 * Largest packet size we use is 32 32-bit words = 128 bytes 70 */ 71 ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 72 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128); 73 if (ret < 0) { 74 dev_err(dai->dev, "Could not apply period constraint: %d\n", 75 ret); 76 return ret; 77 } 78 ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 79 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 128); 80 if (ret < 0) { 81 dev_err(dai->dev, "Could not apply buffer constraint: %d\n", 82 ret); 83 return ret; 84 } 85 86 snd_soc_dai_set_dma_data(dai, substream, &ad->dma_data); 87 88 scoped_guard(mutex, &ad->current_stream_lock) 89 ad->current_stream = substream; 90 91 ret = ad->ops->audio_startup(ad->dssdev, hdmi_dai_abort); 92 93 if (ret) { 94 scoped_guard(mutex, &ad->current_stream_lock) 95 ad->current_stream = NULL; 96 } 97 98 return ret; 99 } 100 101 static int hdmi_dai_hw_params(struct snd_pcm_substream *substream, 102 struct snd_pcm_hw_params *params, 103 struct snd_soc_dai *dai) 104 { 105 struct hdmi_audio_data *ad = card_drvdata_substream(substream); 106 struct snd_aes_iec958 *iec = &ad->iec; 107 struct snd_cea_861_aud_if *cea = &ad->cea; 108 109 WARN_ON(ad->current_stream != substream); 110 111 switch (params_format(params)) { 112 case SNDRV_PCM_FORMAT_S16_LE: 113 ad->dma_data.maxburst = 16; 114 break; 115 case SNDRV_PCM_FORMAT_S24_LE: 116 ad->dma_data.maxburst = 32; 117 break; 118 default: 119 dev_err(dai->dev, "format not supported!\n"); 120 return -EINVAL; 121 } 122 123 ad->dss_audio.iec = iec; 124 ad->dss_audio.cea = cea; 125 /* 126 * fill the IEC-60958 channel status word 127 */ 128 /* initialize the word bytes */ 129 memset(iec->status, 0, sizeof(iec->status)); 130 131 /* specify IEC-60958-3 (commercial use) */ 132 iec->status[0] &= ~IEC958_AES0_PROFESSIONAL; 133 134 /* specify that the audio is LPCM*/ 135 iec->status[0] &= ~IEC958_AES0_NONAUDIO; 136 137 iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT; 138 139 iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE; 140 141 iec->status[1] = IEC958_AES1_CON_GENERAL; 142 143 iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC; 144 145 iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC; 146 147 switch (params_rate(params)) { 148 case 32000: 149 iec->status[3] |= IEC958_AES3_CON_FS_32000; 150 break; 151 case 44100: 152 iec->status[3] |= IEC958_AES3_CON_FS_44100; 153 break; 154 case 48000: 155 iec->status[3] |= IEC958_AES3_CON_FS_48000; 156 break; 157 case 88200: 158 iec->status[3] |= IEC958_AES3_CON_FS_88200; 159 break; 160 case 96000: 161 iec->status[3] |= IEC958_AES3_CON_FS_96000; 162 break; 163 case 176400: 164 iec->status[3] |= IEC958_AES3_CON_FS_176400; 165 break; 166 case 192000: 167 iec->status[3] |= IEC958_AES3_CON_FS_192000; 168 break; 169 default: 170 dev_err(dai->dev, "rate not supported!\n"); 171 return -EINVAL; 172 } 173 174 /* specify the clock accuracy */ 175 iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM; 176 177 /* 178 * specify the word length. The same word length value can mean 179 * two different lengths. Hence, we need to specify the maximum 180 * word length as well. 181 */ 182 switch (params_format(params)) { 183 case SNDRV_PCM_FORMAT_S16_LE: 184 iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16; 185 iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24; 186 break; 187 case SNDRV_PCM_FORMAT_S24_LE: 188 iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20; 189 iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24; 190 break; 191 default: 192 dev_err(dai->dev, "format not supported!\n"); 193 return -EINVAL; 194 } 195 196 /* 197 * Fill the CEA-861 audio infoframe (see spec for details) 198 */ 199 200 cea->db1_ct_cc = (params_channels(params) - 1) 201 & CEA861_AUDIO_INFOFRAME_DB1CC; 202 cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM; 203 204 cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM; 205 cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM; 206 207 cea->db3 = 0; /* not used, all zeros */ 208 209 if (params_channels(params) == 2) 210 cea->db4_ca = 0x0; 211 else if (params_channels(params) == 6) 212 cea->db4_ca = 0xb; 213 else 214 cea->db4_ca = 0x13; 215 216 if (cea->db4_ca == 0x00) 217 cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PERMITTED; 218 else 219 cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED; 220 221 /* the expression is trivial but makes clear what we are doing */ 222 cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV); 223 224 return ad->ops->audio_config(ad->dssdev, &ad->dss_audio); 225 } 226 227 static int hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd, 228 struct snd_soc_dai *dai) 229 { 230 struct hdmi_audio_data *ad = card_drvdata_substream(substream); 231 int err = 0; 232 233 WARN_ON(ad->current_stream != substream); 234 235 switch (cmd) { 236 case SNDRV_PCM_TRIGGER_START: 237 case SNDRV_PCM_TRIGGER_RESUME: 238 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 239 err = ad->ops->audio_start(ad->dssdev); 240 break; 241 case SNDRV_PCM_TRIGGER_STOP: 242 case SNDRV_PCM_TRIGGER_SUSPEND: 243 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 244 ad->ops->audio_stop(ad->dssdev); 245 break; 246 default: 247 err = -EINVAL; 248 } 249 return err; 250 } 251 252 static void hdmi_dai_shutdown(struct snd_pcm_substream *substream, 253 struct snd_soc_dai *dai) 254 { 255 struct hdmi_audio_data *ad = card_drvdata_substream(substream); 256 257 WARN_ON(ad->current_stream != substream); 258 259 ad->ops->audio_shutdown(ad->dssdev); 260 261 scoped_guard(mutex, &ad->current_stream_lock) 262 ad->current_stream = NULL; 263 } 264 265 static const struct snd_soc_dai_ops hdmi_dai_ops = { 266 .startup = hdmi_dai_startup, 267 .hw_params = hdmi_dai_hw_params, 268 .trigger = hdmi_dai_trigger, 269 .shutdown = hdmi_dai_shutdown, 270 }; 271 272 static const struct snd_soc_component_driver omap_hdmi_component = { 273 .name = "omapdss_hdmi", 274 .legacy_dai_naming = 1, 275 }; 276 277 static struct snd_soc_dai_driver omap5_hdmi_dai = { 278 .name = "omap5-hdmi-dai", 279 .playback = { 280 .channels_min = 2, 281 .channels_max = 8, 282 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | 283 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | 284 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | 285 SNDRV_PCM_RATE_192000), 286 .formats = SNDRV_PCM_FMTBIT_S16_LE, 287 }, 288 .ops = &hdmi_dai_ops, 289 }; 290 291 static struct snd_soc_dai_driver omap4_hdmi_dai = { 292 .name = "omap4-hdmi-dai", 293 .playback = { 294 .channels_min = 2, 295 .channels_max = 8, 296 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | 297 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | 298 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | 299 SNDRV_PCM_RATE_192000), 300 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 301 }, 302 .ops = &hdmi_dai_ops, 303 }; 304 305 static int omap_hdmi_audio_probe(struct platform_device *pdev) 306 { 307 struct omap_hdmi_audio_pdata *ha = pdev->dev.platform_data; 308 struct device *dev = &pdev->dev; 309 struct hdmi_audio_data *ad; 310 struct snd_soc_dai_driver *dai_drv; 311 struct snd_soc_card *card; 312 struct snd_soc_dai_link_component *compnent; 313 int ret; 314 315 if (!ha) { 316 dev_err(dev, "No platform data\n"); 317 return -EINVAL; 318 } 319 320 ad = devm_kzalloc(dev, sizeof(*ad), GFP_KERNEL); 321 if (!ad) 322 return -ENOMEM; 323 ad->dssdev = ha->dev; 324 ad->ops = ha->ops; 325 ad->dma_data.addr = ha->audio_dma_addr; 326 ad->dma_data.filter_data = "audio_tx"; 327 ad->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 328 mutex_init(&ad->current_stream_lock); 329 330 switch (ha->version) { 331 case 4: 332 dai_drv = &omap4_hdmi_dai; 333 break; 334 case 5: 335 dai_drv = &omap5_hdmi_dai; 336 break; 337 default: 338 return -EINVAL; 339 } 340 ret = devm_snd_soc_register_component(ad->dssdev, &omap_hdmi_component, 341 dai_drv, 1); 342 if (ret) 343 return ret; 344 345 ret = sdma_pcm_platform_register(ad->dssdev, "audio_tx", NULL); 346 if (ret) 347 return ret; 348 349 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 350 if (!card) 351 return -ENOMEM; 352 353 card->name = "HDMI"; 354 card->owner = THIS_MODULE; 355 card->dai_link = 356 devm_kzalloc(dev, sizeof(*(card->dai_link)), GFP_KERNEL); 357 if (!card->dai_link) 358 return -ENOMEM; 359 360 compnent = devm_kzalloc(dev, 2 * sizeof(*compnent), GFP_KERNEL); 361 if (!compnent) 362 return -ENOMEM; 363 card->dai_link->cpus = &compnent[0]; 364 card->dai_link->num_cpus = 1; 365 card->dai_link->codecs = &snd_soc_dummy_dlc; 366 card->dai_link->num_codecs = 1; 367 card->dai_link->platforms = &compnent[1]; 368 card->dai_link->num_platforms = 1; 369 370 card->dai_link->name = card->name; 371 card->dai_link->stream_name = card->name; 372 card->dai_link->cpus->dai_name = dev_name(ad->dssdev); 373 card->dai_link->platforms->name = dev_name(ad->dssdev); 374 card->num_links = 1; 375 card->dev = dev; 376 377 ret = devm_snd_soc_register_card(dev, card); 378 if (ret) { 379 dev_err(dev, "snd_soc_register_card failed (%d)\n", ret); 380 return ret; 381 } 382 383 ad->card = card; 384 snd_soc_card_set_drvdata(card, ad); 385 386 dev_set_drvdata(dev, ad); 387 388 return 0; 389 } 390 391 static struct platform_driver hdmi_audio_driver = { 392 .driver = { 393 .name = DRV_NAME, 394 }, 395 .probe = omap_hdmi_audio_probe, 396 }; 397 398 module_platform_driver(hdmi_audio_driver); 399 400 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>"); 401 MODULE_DESCRIPTION("OMAP HDMI Audio Driver"); 402 MODULE_LICENSE("GPL"); 403 MODULE_ALIAS("platform:" DRV_NAME); 404