Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1 | /* |
| 2 | * intel_hdmi_audio.c - Intel HDMI audio driver |
| 3 | * |
| 4 | * Copyright (C) 2016 Intel Corp |
| 5 | * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com> |
| 6 | * Ramesh Babu K V <ramesh.babu@intel.com> |
| 7 | * Vaibhav Agarwal <vaibhav.agarwal@intel.com> |
| 8 | * Jerome Anand <jerome.anand@intel.com> |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; version 2 of the License. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 21 | * ALSA driver for Intel HDMI audio |
| 22 | */ |
| 23 | |
| 24 | #define pr_fmt(fmt) "had: " fmt |
| 25 | |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/acpi.h> |
| 31 | #include <asm/cacheflush.h> |
| 32 | #include <sound/pcm.h> |
| 33 | #include <sound/core.h> |
| 34 | #include <sound/pcm_params.h> |
| 35 | #include <sound/initval.h> |
| 36 | #include <sound/control.h> |
| 37 | #include <sound/initval.h> |
| 38 | #include "intel_hdmi_audio.h" |
| 39 | |
| 40 | static DEFINE_MUTEX(had_mutex); |
| 41 | |
| 42 | /*standard module options for ALSA. This module supports only one card*/ |
| 43 | static int hdmi_card_index = SNDRV_DEFAULT_IDX1; |
| 44 | static char *hdmi_card_id = SNDRV_DEFAULT_STR1; |
| 45 | static struct snd_intelhad *had_data; |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame] | 46 | static int underrun_count; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 47 | |
| 48 | module_param_named(index, hdmi_card_index, int, 0444); |
| 49 | MODULE_PARM_DESC(index, |
| 50 | "Index value for INTEL Intel HDMI Audio controller."); |
| 51 | module_param_named(id, hdmi_card_id, charp, 0444); |
| 52 | MODULE_PARM_DESC(id, |
| 53 | "ID string for INTEL Intel HDMI Audio controller."); |
| 54 | |
| 55 | /* |
| 56 | * ELD SA bits in the CEA Speaker Allocation data block |
| 57 | */ |
| 58 | static int eld_speaker_allocation_bits[] = { |
| 59 | [0] = FL | FR, |
| 60 | [1] = LFE, |
| 61 | [2] = FC, |
| 62 | [3] = RL | RR, |
| 63 | [4] = RC, |
| 64 | [5] = FLC | FRC, |
| 65 | [6] = RLC | RRC, |
| 66 | /* the following are not defined in ELD yet */ |
| 67 | [7] = 0, |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * This is an ordered list! |
| 72 | * |
| 73 | * The preceding ones have better chances to be selected by |
| 74 | * hdmi_channel_allocation(). |
| 75 | */ |
| 76 | static struct cea_channel_speaker_allocation channel_allocations[] = { |
| 77 | /* channel: 7 6 5 4 3 2 1 0 */ |
| 78 | { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } }, |
| 79 | /* 2.1 */ |
| 80 | { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } }, |
| 81 | /* Dolby Surround */ |
| 82 | { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } }, |
| 83 | /* surround40 */ |
| 84 | { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } }, |
| 85 | /* surround41 */ |
| 86 | { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } }, |
| 87 | /* surround50 */ |
| 88 | { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } }, |
| 89 | /* surround51 */ |
| 90 | { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } }, |
| 91 | /* 6.1 */ |
| 92 | { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } }, |
| 93 | /* surround71 */ |
| 94 | { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } }, |
| 95 | |
| 96 | { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } }, |
| 97 | { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } }, |
| 98 | { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } }, |
| 99 | { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } }, |
| 100 | { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } }, |
| 101 | { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } }, |
| 102 | { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } }, |
| 103 | { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } }, |
| 104 | { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } }, |
| 105 | { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } }, |
| 106 | { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } }, |
| 107 | { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } }, |
| 108 | { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } }, |
| 109 | { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } }, |
| 110 | { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } }, |
| 111 | { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } }, |
| 112 | { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } }, |
| 113 | { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } }, |
| 114 | { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } }, |
| 115 | { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } }, |
| 116 | { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } }, |
| 117 | { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } }, |
| 118 | { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } }, |
| 119 | }; |
| 120 | |
| 121 | static struct channel_map_table map_tables[] = { |
| 122 | { SNDRV_CHMAP_FL, 0x00, FL }, |
| 123 | { SNDRV_CHMAP_FR, 0x01, FR }, |
| 124 | { SNDRV_CHMAP_RL, 0x04, RL }, |
| 125 | { SNDRV_CHMAP_RR, 0x05, RR }, |
| 126 | { SNDRV_CHMAP_LFE, 0x02, LFE }, |
| 127 | { SNDRV_CHMAP_FC, 0x03, FC }, |
| 128 | { SNDRV_CHMAP_RLC, 0x06, RLC }, |
| 129 | { SNDRV_CHMAP_RRC, 0x07, RRC }, |
| 130 | {} /* terminator */ |
| 131 | }; |
| 132 | |
| 133 | /* hardware capability structure */ |
| 134 | static const struct snd_pcm_hardware snd_intel_hadstream = { |
| 135 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 136 | SNDRV_PCM_INFO_DOUBLE | |
| 137 | SNDRV_PCM_INFO_MMAP| |
| 138 | SNDRV_PCM_INFO_MMAP_VALID | |
| 139 | SNDRV_PCM_INFO_BATCH), |
| 140 | .formats = (SNDRV_PCM_FMTBIT_S24 | |
| 141 | SNDRV_PCM_FMTBIT_U24), |
| 142 | .rates = SNDRV_PCM_RATE_32000 | |
| 143 | SNDRV_PCM_RATE_44100 | |
| 144 | SNDRV_PCM_RATE_48000 | |
| 145 | SNDRV_PCM_RATE_88200 | |
| 146 | SNDRV_PCM_RATE_96000 | |
| 147 | SNDRV_PCM_RATE_176400 | |
| 148 | SNDRV_PCM_RATE_192000, |
| 149 | .rate_min = HAD_MIN_RATE, |
| 150 | .rate_max = HAD_MAX_RATE, |
| 151 | .channels_min = HAD_MIN_CHANNEL, |
| 152 | .channels_max = HAD_MAX_CHANNEL, |
| 153 | .buffer_bytes_max = HAD_MAX_BUFFER, |
| 154 | .period_bytes_min = HAD_MIN_PERIOD_BYTES, |
| 155 | .period_bytes_max = HAD_MAX_PERIOD_BYTES, |
| 156 | .periods_min = HAD_MIN_PERIODS, |
| 157 | .periods_max = HAD_MAX_PERIODS, |
| 158 | .fifo_size = HAD_FIFO_SIZE, |
| 159 | }; |
| 160 | |
| 161 | /* Register access functions */ |
| 162 | |
| 163 | int had_get_hwstate(struct snd_intelhad *intelhaddata) |
| 164 | { |
| 165 | /* Check for device presence -SW state */ |
| 166 | if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) { |
| 167 | pr_debug("%s:Device not connected:%d\n", __func__, |
| 168 | intelhaddata->drv_status); |
| 169 | return -ENODEV; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int had_get_caps(enum had_caps_list query, void *caps) |
| 176 | { |
| 177 | int retval; |
| 178 | struct snd_intelhad *intelhaddata = had_data; |
| 179 | |
| 180 | retval = had_get_hwstate(intelhaddata); |
| 181 | if (!retval) |
| 182 | retval = intelhaddata->query_ops.hdmi_audio_get_caps(query, |
| 183 | caps); |
| 184 | |
| 185 | return retval; |
| 186 | } |
| 187 | |
| 188 | int had_set_caps(enum had_caps_list set_element, void *caps) |
| 189 | { |
| 190 | int retval; |
| 191 | struct snd_intelhad *intelhaddata = had_data; |
| 192 | |
| 193 | retval = had_get_hwstate(intelhaddata); |
| 194 | if (!retval) |
| 195 | retval = intelhaddata->query_ops.hdmi_audio_set_caps( |
| 196 | set_element, caps); |
| 197 | |
| 198 | return retval; |
| 199 | } |
| 200 | |
| 201 | int had_read_register(u32 offset, u32 *data) |
| 202 | { |
| 203 | int retval; |
| 204 | struct snd_intelhad *intelhaddata = had_data; |
| 205 | |
| 206 | retval = had_get_hwstate(intelhaddata); |
| 207 | if (!retval) |
| 208 | retval = intelhaddata->reg_ops.hdmi_audio_read_register( |
| 209 | offset + intelhaddata->audio_cfg_offset, data); |
| 210 | |
| 211 | return retval; |
| 212 | } |
| 213 | |
| 214 | int had_write_register(u32 offset, u32 data) |
| 215 | { |
| 216 | int retval; |
| 217 | struct snd_intelhad *intelhaddata = had_data; |
| 218 | |
| 219 | retval = had_get_hwstate(intelhaddata); |
| 220 | if (!retval) |
| 221 | retval = intelhaddata->reg_ops.hdmi_audio_write_register( |
| 222 | offset + intelhaddata->audio_cfg_offset, data); |
| 223 | |
| 224 | return retval; |
| 225 | } |
| 226 | |
| 227 | int had_read_modify(u32 offset, u32 data, u32 mask) |
| 228 | { |
| 229 | int retval; |
| 230 | struct snd_intelhad *intelhaddata = had_data; |
| 231 | |
| 232 | retval = had_get_hwstate(intelhaddata); |
| 233 | if (!retval) |
| 234 | retval = intelhaddata->reg_ops.hdmi_audio_read_modify( |
| 235 | offset + intelhaddata->audio_cfg_offset, |
| 236 | data, mask); |
| 237 | |
| 238 | return retval; |
| 239 | } |
| 240 | /** |
| 241 | * function to read-modify |
| 242 | * AUD_CONFIG register on VLV2.The had_read_modify() function should not |
| 243 | * directly be used on VLV2 for updating AUD_CONFIG register. |
| 244 | * This is because: |
| 245 | * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2 |
| 246 | * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always |
| 247 | * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the |
| 248 | * register. This field should be 1xy binary for configuration with 6 or |
| 249 | * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio) |
| 250 | * causes the "channels" field to be updated as 0xy binary resulting in |
| 251 | * bad audio. The fix is to always write the AUD_CONFIG[6:4] with |
| 252 | * appropriate value when doing read-modify of AUD_CONFIG register. |
| 253 | * |
| 254 | * @substream: the current substream or NULL if no active substream |
| 255 | * @data : data to be written |
| 256 | * @mask : mask |
| 257 | * |
| 258 | */ |
| 259 | static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream, |
| 260 | u32 data, u32 mask) |
| 261 | { |
| 262 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 263 | u8 channels; |
| 264 | |
| 265 | /* |
| 266 | * If substream is NULL, there is no active stream. |
| 267 | * In this case just set channels to 2 |
| 268 | */ |
| 269 | if (substream) |
| 270 | channels = substream->runtime->channels; |
| 271 | else |
| 272 | channels = 2; |
| 273 | cfg_val.cfg_regx_v2.num_ch = channels - 2; |
| 274 | |
| 275 | data = data | cfg_val.cfg_regval; |
| 276 | mask = mask | AUD_CONFIG_CH_MASK_V2; |
| 277 | |
| 278 | pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask); |
| 279 | |
| 280 | return had_read_modify(AUD_CONFIG, data, mask); |
| 281 | } |
| 282 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 283 | static void snd_intelhad_enable_audio_v2(struct snd_pcm_substream *substream, |
| 284 | u8 enable) |
| 285 | { |
| 286 | had_read_modify_aud_config_v2(substream, enable, BIT(0)); |
| 287 | } |
| 288 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 289 | static void snd_intelhad_reset_audio_v2(u8 reset) |
| 290 | { |
| 291 | had_write_register(AUD_HDMI_STATUS_v2, reset); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * initialize audio channel status registers |
| 296 | * This function is called in the prepare callback |
| 297 | */ |
| 298 | static int had_prog_status_reg(struct snd_pcm_substream *substream, |
| 299 | struct snd_intelhad *intelhaddata) |
| 300 | { |
| 301 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 302 | union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0}; |
| 303 | union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0}; |
| 304 | int format; |
| 305 | |
| 306 | pr_debug("Entry %s\n", __func__); |
| 307 | |
| 308 | ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits & |
| 309 | IEC958_AES0_NONAUDIO)>>1; |
| 310 | ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits & |
| 311 | IEC958_AES3_CON_CLOCK)>>4; |
Takashi Iwai | 4812dcc | 2017-01-30 15:58:15 +0100 | [diff] [blame^] | 312 | cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 313 | |
| 314 | switch (substream->runtime->rate) { |
| 315 | case AUD_SAMPLE_RATE_32: |
| 316 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ; |
| 317 | break; |
| 318 | |
| 319 | case AUD_SAMPLE_RATE_44_1: |
| 320 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ; |
| 321 | break; |
| 322 | case AUD_SAMPLE_RATE_48: |
| 323 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ; |
| 324 | break; |
| 325 | case AUD_SAMPLE_RATE_88_2: |
| 326 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ; |
| 327 | break; |
| 328 | case AUD_SAMPLE_RATE_96: |
| 329 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ; |
| 330 | break; |
| 331 | case AUD_SAMPLE_RATE_176_4: |
| 332 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ; |
| 333 | break; |
| 334 | case AUD_SAMPLE_RATE_192: |
| 335 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ; |
| 336 | break; |
| 337 | |
| 338 | default: |
| 339 | /* control should never come here */ |
| 340 | return -EINVAL; |
| 341 | break; |
| 342 | |
| 343 | } |
| 344 | had_write_register(AUD_CH_STATUS_0, ch_stat0.status_0_regval); |
| 345 | |
| 346 | format = substream->runtime->format; |
| 347 | |
| 348 | if (format == SNDRV_PCM_FORMAT_S16_LE) { |
| 349 | ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20; |
| 350 | ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS; |
| 351 | } else if (format == SNDRV_PCM_FORMAT_S24_LE) { |
| 352 | ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24; |
| 353 | ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS; |
| 354 | } else { |
| 355 | ch_stat1.status_1_regx.max_wrd_len = 0; |
| 356 | ch_stat1.status_1_regx.wrd_len = 0; |
| 357 | } |
| 358 | had_write_register(AUD_CH_STATUS_1, ch_stat1.status_1_regval); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * function to initialize audio |
| 364 | * registers and buffer confgiuration registers |
| 365 | * This function is called in the prepare callback |
| 366 | */ |
| 367 | static int snd_intelhad_prog_audio_ctrl_v2(struct snd_pcm_substream *substream, |
| 368 | struct snd_intelhad *intelhaddata) |
| 369 | { |
| 370 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 371 | union aud_buf_config buf_cfg = {.buf_cfgval = 0}; |
| 372 | u8 channels; |
| 373 | |
| 374 | had_prog_status_reg(substream, intelhaddata); |
| 375 | |
| 376 | buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD; |
| 377 | buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD; |
| 378 | buf_cfg.buf_cfg_regx_v2.aud_delay = 0; |
| 379 | had_write_register(AUD_BUF_CONFIG, buf_cfg.buf_cfgval); |
| 380 | |
| 381 | channels = substream->runtime->channels; |
| 382 | cfg_val.cfg_regx_v2.num_ch = channels - 2; |
| 383 | if (channels <= 2) |
| 384 | cfg_val.cfg_regx_v2.layout = LAYOUT0; |
| 385 | else |
| 386 | cfg_val.cfg_regx_v2.layout = LAYOUT1; |
| 387 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 388 | cfg_val.cfg_regx_v2.val_bit = 1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 389 | had_write_register(AUD_CONFIG, cfg_val.cfg_regval); |
| 390 | return 0; |
| 391 | } |
| 392 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 393 | /* |
| 394 | * Compute derived values in channel_allocations[]. |
| 395 | */ |
| 396 | static void init_channel_allocations(void) |
| 397 | { |
| 398 | int i, j; |
| 399 | struct cea_channel_speaker_allocation *p; |
| 400 | |
| 401 | pr_debug("%s: Enter\n", __func__); |
| 402 | |
| 403 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 404 | p = channel_allocations + i; |
| 405 | p->channels = 0; |
| 406 | p->spk_mask = 0; |
| 407 | for (j = 0; j < ARRAY_SIZE(p->speakers); j++) |
| 408 | if (p->speakers[j]) { |
| 409 | p->channels++; |
| 410 | p->spk_mask |= p->speakers[j]; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * The transformation takes two steps: |
| 417 | * |
| 418 | * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask |
| 419 | * spk_mask => (channel_allocations[]) => ai->CA |
| 420 | * |
| 421 | * TODO: it could select the wrong CA from multiple candidates. |
| 422 | */ |
| 423 | static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata, |
| 424 | int channels) |
| 425 | { |
| 426 | int i; |
| 427 | int ca = 0; |
| 428 | int spk_mask = 0; |
| 429 | |
| 430 | /* |
| 431 | * CA defaults to 0 for basic stereo audio |
| 432 | */ |
| 433 | if (channels <= 2) |
| 434 | return 0; |
| 435 | |
| 436 | /* |
| 437 | * expand ELD's speaker allocation mask |
| 438 | * |
| 439 | * ELD tells the speaker mask in a compact(paired) form, |
| 440 | * expand ELD's notions to match the ones used by Audio InfoFrame. |
| 441 | */ |
| 442 | |
| 443 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
| 444 | if (intelhaddata->eeld.speaker_allocation_block & (1 << i)) |
| 445 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 446 | } |
| 447 | |
| 448 | /* search for the first working match in the CA table */ |
| 449 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 450 | if (channels == channel_allocations[i].channels && |
| 451 | (spk_mask & channel_allocations[i].spk_mask) == |
| 452 | channel_allocations[i].spk_mask) { |
| 453 | ca = channel_allocations[i].ca_index; |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels); |
| 459 | |
| 460 | return ca; |
| 461 | } |
| 462 | |
| 463 | /* from speaker bit mask to ALSA API channel position */ |
| 464 | static int spk_to_chmap(int spk) |
| 465 | { |
| 466 | struct channel_map_table *t = map_tables; |
| 467 | |
| 468 | for (; t->map; t++) { |
| 469 | if (t->spk_mask == spk) |
| 470 | return t->map; |
| 471 | } |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata) |
| 476 | { |
| 477 | int i = 0, c = 0; |
| 478 | int spk_mask = 0; |
| 479 | struct snd_pcm_chmap_elem *chmap; |
| 480 | u8 eld_high, eld_high_mask = 0xF0; |
| 481 | u8 high_msb; |
| 482 | |
| 483 | chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); |
| 484 | if (chmap == NULL) { |
| 485 | intelhaddata->chmap->chmap = NULL; |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | had_get_caps(HAD_GET_ELD, &intelhaddata->eeld); |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 490 | had_get_caps(HAD_GET_DP_OUTPUT, &intelhaddata->dp_output); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 491 | |
| 492 | pr_debug("eeld.speaker_allocation_block = %x\n", |
| 493 | intelhaddata->eeld.speaker_allocation_block); |
| 494 | |
| 495 | /* WA: Fix the max channel supported to 8 */ |
| 496 | |
| 497 | /* |
| 498 | * Sink may support more than 8 channels, if eld_high has more than |
| 499 | * one bit set. SOC supports max 8 channels. |
| 500 | * Refer eld_speaker_allocation_bits, for sink speaker allocation |
| 501 | */ |
| 502 | |
| 503 | /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */ |
| 504 | eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask; |
| 505 | if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) { |
| 506 | /* eld_high & (eld_high-1): if more than 1 bit set */ |
| 507 | /* 0x1F: 7 channels */ |
| 508 | for (i = 1; i < 4; i++) { |
| 509 | high_msb = eld_high & (0x80 >> i); |
| 510 | if (high_msb) { |
| 511 | intelhaddata->eeld.speaker_allocation_block &= |
| 512 | high_msb | 0xF; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
| 519 | if (intelhaddata->eeld.speaker_allocation_block & (1 << i)) |
| 520 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 521 | } |
| 522 | |
| 523 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 524 | if (spk_mask == channel_allocations[i].spk_mask) { |
| 525 | for (c = 0; c < channel_allocations[i].channels; c++) { |
| 526 | chmap->map[c] = spk_to_chmap( |
| 527 | channel_allocations[i].speakers[ |
| 528 | (MAX_SPEAKERS - 1)-c]); |
| 529 | } |
| 530 | chmap->channels = channel_allocations[i].channels; |
| 531 | intelhaddata->chmap->chmap = chmap; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | if (i >= ARRAY_SIZE(channel_allocations)) { |
| 536 | intelhaddata->chmap->chmap = NULL; |
| 537 | kfree(chmap); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * ALSA API channel-map control callbacks |
| 543 | */ |
| 544 | static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol, |
| 545 | struct snd_ctl_elem_info *uinfo) |
| 546 | { |
| 547 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 548 | struct snd_intelhad *intelhaddata = info->private_data; |
| 549 | |
| 550 | if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) |
| 551 | return -ENODEV; |
| 552 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 553 | uinfo->count = HAD_MAX_CHANNEL; |
| 554 | uinfo->value.integer.min = 0; |
| 555 | uinfo->value.integer.max = SNDRV_CHMAP_LAST; |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol, |
| 560 | struct snd_ctl_elem_value *ucontrol) |
| 561 | { |
| 562 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 563 | struct snd_intelhad *intelhaddata = info->private_data; |
| 564 | int i = 0; |
| 565 | const struct snd_pcm_chmap_elem *chmap; |
| 566 | |
| 567 | if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) |
| 568 | return -ENODEV; |
| 569 | if (intelhaddata->chmap->chmap == NULL) |
| 570 | return -ENODATA; |
| 571 | chmap = intelhaddata->chmap->chmap; |
| 572 | for (i = 0; i < chmap->channels; i++) { |
| 573 | ucontrol->value.integer.value[i] = chmap->map[i]; |
| 574 | pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]); |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata, |
| 581 | struct snd_pcm *pcm) |
| 582 | { |
| 583 | int err = 0; |
| 584 | |
| 585 | err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 586 | NULL, 0, (unsigned long)intelhaddata, |
| 587 | &intelhaddata->chmap); |
| 588 | if (err < 0) |
| 589 | return err; |
| 590 | |
| 591 | intelhaddata->chmap->private_data = intelhaddata; |
| 592 | intelhaddata->kctl = intelhaddata->chmap->kctl; |
| 593 | intelhaddata->kctl->info = had_chmap_ctl_info; |
| 594 | intelhaddata->kctl->get = had_chmap_ctl_get; |
| 595 | intelhaddata->chmap->chmap = NULL; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | /** |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 600 | * snd_intelhad_prog_dip_v2 - to initialize Data Island Packets registers |
| 601 | * |
| 602 | * @substream:substream for which the prepare function is called |
| 603 | * @intelhaddata:substream private data |
| 604 | * |
| 605 | * This function is called in the prepare callback |
| 606 | */ |
| 607 | static void snd_intelhad_prog_dip_v2(struct snd_pcm_substream *substream, |
| 608 | struct snd_intelhad *intelhaddata) |
| 609 | { |
| 610 | int i; |
| 611 | union aud_ctrl_st ctrl_state = {.ctrl_val = 0}; |
| 612 | union aud_info_frame2 frame2 = {.fr2_val = 0}; |
| 613 | union aud_info_frame3 frame3 = {.fr3_val = 0}; |
| 614 | u8 checksum = 0; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 615 | u32 info_frame; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 616 | int channels; |
| 617 | |
| 618 | channels = substream->runtime->channels; |
| 619 | |
| 620 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 621 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 622 | if (intelhaddata->dp_output) { |
| 623 | info_frame = DP_INFO_FRAME_WORD1; |
| 624 | frame2.fr2_val = 1; |
| 625 | } else { |
| 626 | info_frame = HDMI_INFO_FRAME_WORD1; |
| 627 | frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 628 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 629 | frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation( |
| 630 | intelhaddata, channels); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 631 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 632 | /*Calculte the byte wide checksum for all valid DIP words*/ |
| 633 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 634 | checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 635 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 636 | checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 637 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 638 | checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 639 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 640 | frame2.fr2_regx.chksum = -(checksum); |
| 641 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 642 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 643 | had_write_register(AUD_HDMIW_INFOFR_v2, info_frame); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 644 | had_write_register(AUD_HDMIW_INFOFR_v2, frame2.fr2_val); |
| 645 | had_write_register(AUD_HDMIW_INFOFR_v2, frame3.fr3_val); |
| 646 | |
| 647 | /* program remaining DIP words with zero */ |
| 648 | for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++) |
| 649 | had_write_register(AUD_HDMIW_INFOFR_v2, 0x0); |
| 650 | |
| 651 | ctrl_state.ctrl_regx.dip_freq = 1; |
| 652 | ctrl_state.ctrl_regx.dip_en_sta = 1; |
| 653 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * snd_intelhad_prog_buffer - programs buffer |
| 658 | * address and length registers |
| 659 | * |
| 660 | * @substream:substream for which the prepare function is called |
| 661 | * @intelhaddata:substream private data |
| 662 | * |
| 663 | * This function programs ring buffer address and length into registers. |
| 664 | */ |
| 665 | int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata, |
| 666 | int start, int end) |
| 667 | { |
| 668 | u32 ring_buf_addr, ring_buf_size, period_bytes; |
| 669 | u8 i, num_periods; |
| 670 | struct snd_pcm_substream *substream; |
| 671 | |
| 672 | substream = intelhaddata->stream_info.had_substream; |
| 673 | if (!substream) { |
| 674 | pr_err("substream is NULL\n"); |
| 675 | dump_stack(); |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | ring_buf_addr = substream->runtime->dma_addr; |
| 680 | ring_buf_size = snd_pcm_lib_buffer_bytes(substream); |
| 681 | intelhaddata->stream_info.ring_buf_size = ring_buf_size; |
| 682 | period_bytes = frames_to_bytes(substream->runtime, |
| 683 | substream->runtime->period_size); |
| 684 | num_periods = substream->runtime->periods; |
| 685 | |
| 686 | /* |
| 687 | * buffer addr should be 64 byte aligned, period bytes |
| 688 | * will be used to calculate addr offset |
| 689 | */ |
| 690 | period_bytes &= ~0x3F; |
| 691 | |
| 692 | /* Hardware supports MAX_PERIODS buffers */ |
| 693 | if (end >= HAD_MAX_PERIODS) |
| 694 | return -EINVAL; |
| 695 | |
| 696 | for (i = start; i <= end; i++) { |
| 697 | /* Program the buf registers with addr and len */ |
| 698 | intelhaddata->buf_info[i].buf_addr = ring_buf_addr + |
| 699 | (i * period_bytes); |
| 700 | if (i < num_periods-1) |
| 701 | intelhaddata->buf_info[i].buf_size = period_bytes; |
| 702 | else |
| 703 | intelhaddata->buf_info[i].buf_size = ring_buf_size - |
| 704 | (period_bytes*i); |
| 705 | |
| 706 | had_write_register(AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH), |
| 707 | intelhaddata->buf_info[i].buf_addr | |
| 708 | BIT(0) | BIT(1)); |
| 709 | had_write_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH), |
| 710 | period_bytes); |
| 711 | intelhaddata->buf_info[i].is_valid = true; |
| 712 | } |
| 713 | pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end, |
| 714 | intelhaddata->buf_info[start].buf_addr, |
| 715 | intelhaddata->buf_info[start].buf_size); |
| 716 | intelhaddata->valid_buf_cnt = num_periods; |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | int snd_intelhad_read_len(struct snd_intelhad *intelhaddata) |
| 721 | { |
| 722 | int i, retval = 0; |
| 723 | u32 len[4]; |
| 724 | |
| 725 | for (i = 0; i < 4 ; i++) { |
| 726 | had_read_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH), |
| 727 | &len[i]); |
| 728 | if (!len[i]) |
| 729 | retval++; |
| 730 | } |
| 731 | if (retval != 1) { |
| 732 | for (i = 0; i < 4 ; i++) |
| 733 | pr_debug("buf[%d] size=%d\n", i, len[i]); |
| 734 | } |
| 735 | |
| 736 | return retval; |
| 737 | } |
| 738 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 739 | static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate) |
| 740 | { |
| 741 | u32 maud_val; |
| 742 | |
| 743 | /* Select maud according to DP 1.2 spec*/ |
| 744 | if (link_rate == DP_2_7_GHZ) { |
| 745 | switch (aud_samp_freq) { |
| 746 | case AUD_SAMPLE_RATE_32: |
| 747 | maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL; |
| 748 | break; |
| 749 | |
| 750 | case AUD_SAMPLE_RATE_44_1: |
| 751 | maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL; |
| 752 | break; |
| 753 | |
| 754 | case AUD_SAMPLE_RATE_48: |
| 755 | maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL; |
| 756 | break; |
| 757 | |
| 758 | case AUD_SAMPLE_RATE_88_2: |
| 759 | maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL; |
| 760 | break; |
| 761 | |
| 762 | case AUD_SAMPLE_RATE_96: |
| 763 | maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL; |
| 764 | break; |
| 765 | |
| 766 | case AUD_SAMPLE_RATE_176_4: |
| 767 | maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL; |
| 768 | break; |
| 769 | |
| 770 | case HAD_MAX_RATE: |
| 771 | maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL; |
| 772 | break; |
| 773 | |
| 774 | default: |
| 775 | maud_val = -EINVAL; |
| 776 | break; |
| 777 | } |
| 778 | } else if (link_rate == DP_1_62_GHZ) { |
| 779 | switch (aud_samp_freq) { |
| 780 | case AUD_SAMPLE_RATE_32: |
| 781 | maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL; |
| 782 | break; |
| 783 | |
| 784 | case AUD_SAMPLE_RATE_44_1: |
| 785 | maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL; |
| 786 | break; |
| 787 | |
| 788 | case AUD_SAMPLE_RATE_48: |
| 789 | maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL; |
| 790 | break; |
| 791 | |
| 792 | case AUD_SAMPLE_RATE_88_2: |
| 793 | maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL; |
| 794 | break; |
| 795 | |
| 796 | case AUD_SAMPLE_RATE_96: |
| 797 | maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL; |
| 798 | break; |
| 799 | |
| 800 | case AUD_SAMPLE_RATE_176_4: |
| 801 | maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL; |
| 802 | break; |
| 803 | |
| 804 | case HAD_MAX_RATE: |
| 805 | maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL; |
| 806 | break; |
| 807 | |
| 808 | default: |
| 809 | maud_val = -EINVAL; |
| 810 | break; |
| 811 | } |
| 812 | } else |
| 813 | maud_val = -EINVAL; |
| 814 | |
| 815 | return maud_val; |
| 816 | } |
| 817 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 818 | /** |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 819 | * snd_intelhad_prog_cts_v2 - Program HDMI audio CTS value |
| 820 | * |
| 821 | * @aud_samp_freq: sampling frequency of audio data |
| 822 | * @tmds: sampling frequency of the display data |
| 823 | * @n_param: N value, depends on aud_samp_freq |
| 824 | * @intelhaddata:substream private data |
| 825 | * |
| 826 | * Program CTS register based on the audio and display sampling frequency |
| 827 | */ |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 828 | static void snd_intelhad_prog_cts_v2(u32 aud_samp_freq, u32 tmds, |
| 829 | u32 link_rate, u32 n_param, |
| 830 | struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 831 | { |
| 832 | u32 cts_val; |
| 833 | u64 dividend, divisor; |
| 834 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 835 | if (intelhaddata->dp_output) { |
| 836 | /* Substitute cts_val with Maud according to DP 1.2 spec*/ |
| 837 | cts_val = had_calculate_maud_value(aud_samp_freq, link_rate); |
| 838 | } else { |
| 839 | /* Calculate CTS according to HDMI 1.3a spec*/ |
| 840 | dividend = (u64)tmds * n_param*1000; |
| 841 | divisor = 128 * aud_samp_freq; |
| 842 | cts_val = div64_u64(dividend, divisor); |
| 843 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 844 | pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n", |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 845 | tmds, n_param, cts_val); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 846 | had_write_register(AUD_HDMI_CTS, (BIT(24) | cts_val)); |
| 847 | } |
| 848 | |
| 849 | static int had_calculate_n_value(u32 aud_samp_freq) |
| 850 | { |
| 851 | s32 n_val; |
| 852 | |
| 853 | /* Select N according to HDMI 1.3a spec*/ |
| 854 | switch (aud_samp_freq) { |
| 855 | case AUD_SAMPLE_RATE_32: |
| 856 | n_val = 4096; |
| 857 | break; |
| 858 | |
| 859 | case AUD_SAMPLE_RATE_44_1: |
| 860 | n_val = 6272; |
| 861 | break; |
| 862 | |
| 863 | case AUD_SAMPLE_RATE_48: |
| 864 | n_val = 6144; |
| 865 | break; |
| 866 | |
| 867 | case AUD_SAMPLE_RATE_88_2: |
| 868 | n_val = 12544; |
| 869 | break; |
| 870 | |
| 871 | case AUD_SAMPLE_RATE_96: |
| 872 | n_val = 12288; |
| 873 | break; |
| 874 | |
| 875 | case AUD_SAMPLE_RATE_176_4: |
| 876 | n_val = 25088; |
| 877 | break; |
| 878 | |
| 879 | case HAD_MAX_RATE: |
| 880 | n_val = 24576; |
| 881 | break; |
| 882 | |
| 883 | default: |
| 884 | n_val = -EINVAL; |
| 885 | break; |
| 886 | } |
| 887 | return n_val; |
| 888 | } |
| 889 | |
| 890 | /** |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 891 | * snd_intelhad_prog_n_v2 - Program HDMI audio N value |
| 892 | * |
| 893 | * @aud_samp_freq: sampling frequency of audio data |
| 894 | * @n_param: N value, depends on aud_samp_freq |
| 895 | * @intelhaddata:substream private data |
| 896 | * |
| 897 | * This function is called in the prepare callback. |
| 898 | * It programs based on the audio and display sampling frequency |
| 899 | */ |
| 900 | static int snd_intelhad_prog_n_v2(u32 aud_samp_freq, u32 *n_param, |
| 901 | struct snd_intelhad *intelhaddata) |
| 902 | { |
| 903 | s32 n_val; |
| 904 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 905 | if (intelhaddata->dp_output) { |
| 906 | /* |
| 907 | * According to DP specs, Maud and Naud values hold |
| 908 | * a relationship, which is stated as: |
| 909 | * Maud/Naud = 512 * fs / f_LS_Clk |
| 910 | * where, fs is the sampling frequency of the audio stream |
| 911 | * and Naud is 32768 for Async clock. |
| 912 | */ |
| 913 | |
| 914 | n_val = DP_NAUD_VAL; |
| 915 | } else |
| 916 | n_val = had_calculate_n_value(aud_samp_freq); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 917 | |
| 918 | if (n_val < 0) |
| 919 | return n_val; |
| 920 | |
| 921 | had_write_register(AUD_N_ENABLE, (BIT(24) | n_val)); |
| 922 | *n_param = n_val; |
| 923 | return 0; |
| 924 | } |
| 925 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 926 | static void had_clear_underrun_intr_v2(struct snd_intelhad *intelhaddata) |
| 927 | { |
| 928 | u32 hdmi_status, i = 0; |
| 929 | |
| 930 | /* Handle Underrun interrupt within Audio Unit */ |
| 931 | had_write_register(AUD_CONFIG, 0); |
| 932 | /* Reset buffer pointers */ |
| 933 | had_write_register(AUD_HDMI_STATUS_v2, 1); |
| 934 | had_write_register(AUD_HDMI_STATUS_v2, 0); |
| 935 | /** |
| 936 | * The interrupt status 'sticky' bits might not be cleared by |
| 937 | * setting '1' to that bit once... |
| 938 | */ |
| 939 | do { /* clear bit30, 31 AUD_HDMI_STATUS */ |
| 940 | had_read_register(AUD_HDMI_STATUS_v2, &hdmi_status); |
| 941 | pr_debug("HDMI status =0x%x\n", hdmi_status); |
| 942 | if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) { |
| 943 | i++; |
| 944 | had_write_register(AUD_HDMI_STATUS_v2, hdmi_status); |
| 945 | } else |
| 946 | break; |
| 947 | } while (i < MAX_CNT); |
| 948 | if (i >= MAX_CNT) |
| 949 | pr_err("Unable to clear UNDERRUN bits\n"); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * snd_intelhad_open - stream initializations are done here |
| 954 | * @substream:substream for which the stream function is called |
| 955 | * |
| 956 | * This function is called whenever a PCM stream is opened |
| 957 | */ |
| 958 | static int snd_intelhad_open(struct snd_pcm_substream *substream) |
| 959 | { |
| 960 | struct snd_intelhad *intelhaddata; |
| 961 | struct snd_pcm_runtime *runtime; |
| 962 | struct had_stream_pvt *stream; |
| 963 | struct had_pvt_data *had_stream; |
| 964 | int retval; |
| 965 | |
| 966 | pr_debug("snd_intelhad_open called\n"); |
| 967 | intelhaddata = snd_pcm_substream_chip(substream); |
| 968 | had_stream = intelhaddata->private_data; |
| 969 | runtime = substream->runtime; |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame] | 970 | underrun_count = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 971 | |
| 972 | pm_runtime_get(intelhaddata->dev); |
| 973 | |
| 974 | if (had_get_hwstate(intelhaddata)) { |
| 975 | pr_err("%s: HDMI cable plugged-out\n", __func__); |
| 976 | retval = -ENODEV; |
| 977 | goto exit_put_handle; |
| 978 | } |
| 979 | |
| 980 | /* Check, if device already in use */ |
| 981 | if (runtime->private_data) { |
| 982 | pr_err("Device already in use\n"); |
| 983 | retval = -EBUSY; |
| 984 | goto exit_put_handle; |
| 985 | } |
| 986 | |
| 987 | /* set the runtime hw parameter with local snd_pcm_hardware struct */ |
| 988 | runtime->hw = snd_intel_hadstream; |
| 989 | |
| 990 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 991 | if (!stream) { |
| 992 | retval = -ENOMEM; |
| 993 | goto exit_put_handle; |
| 994 | } |
| 995 | stream->stream_status = STREAM_INIT; |
| 996 | runtime->private_data = stream; |
| 997 | |
| 998 | retval = snd_pcm_hw_constraint_integer(runtime, |
| 999 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 1000 | if (retval < 0) |
| 1001 | goto exit_err; |
| 1002 | |
| 1003 | /* Make sure, that the period size is always aligned |
| 1004 | * 64byte boundary |
| 1005 | */ |
| 1006 | retval = snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 1007 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64); |
| 1008 | if (retval < 0) { |
| 1009 | pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval); |
| 1010 | goto exit_err; |
| 1011 | } |
| 1012 | |
| 1013 | return retval; |
| 1014 | exit_err: |
| 1015 | kfree(stream); |
| 1016 | exit_put_handle: |
| 1017 | pm_runtime_put(intelhaddata->dev); |
| 1018 | runtime->private_data = NULL; |
| 1019 | return retval; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * had_period_elapsed - updates the hardware pointer status |
| 1024 | * @had_substream:substream for which the stream function is called |
| 1025 | * |
| 1026 | */ |
| 1027 | static void had_period_elapsed(void *had_substream) |
| 1028 | { |
| 1029 | struct snd_pcm_substream *substream = had_substream; |
| 1030 | struct had_stream_pvt *stream; |
| 1031 | |
| 1032 | /* pr_debug("had_period_elapsed called\n"); */ |
| 1033 | |
| 1034 | if (!substream || !substream->runtime) |
| 1035 | return; |
| 1036 | stream = substream->runtime->private_data; |
| 1037 | if (!stream) |
| 1038 | return; |
| 1039 | |
| 1040 | if (stream->stream_status != STREAM_RUNNING) |
| 1041 | return; |
| 1042 | snd_pcm_period_elapsed(substream); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * snd_intelhad_init_stream - internal function to initialize stream info |
| 1047 | * @substream:substream for which the stream function is called |
| 1048 | * |
| 1049 | */ |
| 1050 | static int snd_intelhad_init_stream(struct snd_pcm_substream *substream) |
| 1051 | { |
| 1052 | struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream); |
| 1053 | |
| 1054 | pr_debug("snd_intelhad_init_stream called\n"); |
| 1055 | |
| 1056 | pr_debug("setting buffer ptr param\n"); |
| 1057 | intelhaddata->stream_info.period_elapsed = had_period_elapsed; |
| 1058 | intelhaddata->stream_info.had_substream = substream; |
| 1059 | intelhaddata->stream_info.buffer_ptr = 0; |
| 1060 | intelhaddata->stream_info.buffer_rendered = 0; |
| 1061 | intelhaddata->stream_info.sfreq = substream->runtime->rate; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * snd_intelhad_close- to free parameteres when stream is stopped |
| 1067 | * |
| 1068 | * @substream: substream for which the function is called |
| 1069 | * |
| 1070 | * This function is called by ALSA framework when stream is stopped |
| 1071 | */ |
| 1072 | static int snd_intelhad_close(struct snd_pcm_substream *substream) |
| 1073 | { |
| 1074 | struct snd_intelhad *intelhaddata; |
| 1075 | struct snd_pcm_runtime *runtime; |
| 1076 | |
| 1077 | pr_debug("snd_intelhad_close called\n"); |
| 1078 | |
| 1079 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1080 | runtime = substream->runtime; |
| 1081 | |
| 1082 | if (!runtime->private_data) { |
| 1083 | pr_debug("close() might have called after failed open"); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | intelhaddata->stream_info.buffer_rendered = 0; |
| 1088 | intelhaddata->stream_info.buffer_ptr = 0; |
| 1089 | intelhaddata->stream_info.str_id = 0; |
| 1090 | intelhaddata->stream_info.had_substream = NULL; |
| 1091 | |
| 1092 | /* Check if following drv_status modification is required - VA */ |
| 1093 | if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) { |
| 1094 | intelhaddata->drv_status = HAD_DRV_CONNECTED; |
| 1095 | pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n", |
| 1096 | __func__, __LINE__); |
| 1097 | } |
| 1098 | kfree(runtime->private_data); |
| 1099 | runtime->private_data = NULL; |
| 1100 | pm_runtime_put(intelhaddata->dev); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * snd_intelhad_hw_params- to setup the hardware parameters |
| 1106 | * like allocating the buffers |
| 1107 | * |
| 1108 | * @substream: substream for which the function is called |
| 1109 | * @hw_params: hardware parameters |
| 1110 | * |
| 1111 | * This function is called by ALSA framework when hardware params are set |
| 1112 | */ |
| 1113 | static int snd_intelhad_hw_params(struct snd_pcm_substream *substream, |
| 1114 | struct snd_pcm_hw_params *hw_params) |
| 1115 | { |
| 1116 | unsigned long addr; |
| 1117 | int pages, buf_size, retval; |
| 1118 | |
| 1119 | pr_debug("snd_intelhad_hw_params called\n"); |
| 1120 | |
| 1121 | if (!hw_params) |
| 1122 | return -EINVAL; |
| 1123 | |
| 1124 | buf_size = params_buffer_bytes(hw_params); |
| 1125 | retval = snd_pcm_lib_malloc_pages(substream, buf_size); |
| 1126 | if (retval < 0) |
| 1127 | return retval; |
| 1128 | pr_debug("%s:allocated memory = %d\n", __func__, buf_size); |
| 1129 | /* mark the pages as uncached region */ |
| 1130 | addr = (unsigned long) substream->runtime->dma_area; |
| 1131 | pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE; |
| 1132 | retval = set_memory_uc(addr, pages); |
| 1133 | if (retval) { |
| 1134 | pr_err("set_memory_uc failed.Error:%d\n", retval); |
| 1135 | return retval; |
| 1136 | } |
| 1137 | memset(substream->runtime->dma_area, 0, buf_size); |
| 1138 | |
| 1139 | return retval; |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * snd_intelhad_hw_free- to release the resources allocated during |
| 1144 | * hardware params setup |
| 1145 | * |
| 1146 | * @substream: substream for which the function is called |
| 1147 | * |
| 1148 | * This function is called by ALSA framework before close callback. |
| 1149 | * |
| 1150 | */ |
| 1151 | static int snd_intelhad_hw_free(struct snd_pcm_substream *substream) |
| 1152 | { |
| 1153 | unsigned long addr; |
| 1154 | u32 pages; |
| 1155 | |
| 1156 | pr_debug("snd_intelhad_hw_free called\n"); |
| 1157 | |
| 1158 | /* mark back the pages as cached/writeback region before the free */ |
| 1159 | if (substream->runtime->dma_area != NULL) { |
| 1160 | addr = (unsigned long) substream->runtime->dma_area; |
| 1161 | pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / |
| 1162 | PAGE_SIZE; |
| 1163 | set_memory_wb(addr, pages); |
| 1164 | return snd_pcm_lib_free_pages(substream); |
| 1165 | } |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * snd_intelhad_pcm_trigger - stream activities are handled here |
| 1171 | * @substream:substream for which the stream function is called |
| 1172 | * @cmd:the stream commamd thats requested from upper layer |
| 1173 | * This function is called whenever an a stream activity is invoked |
| 1174 | */ |
| 1175 | static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream, |
| 1176 | int cmd) |
| 1177 | { |
| 1178 | int caps, retval = 0; |
| 1179 | unsigned long flag_irq; |
| 1180 | struct snd_intelhad *intelhaddata; |
| 1181 | struct had_stream_pvt *stream; |
| 1182 | struct had_pvt_data *had_stream; |
| 1183 | |
| 1184 | pr_debug("snd_intelhad_pcm_trigger called\n"); |
| 1185 | |
| 1186 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1187 | stream = substream->runtime->private_data; |
| 1188 | had_stream = intelhaddata->private_data; |
| 1189 | |
| 1190 | switch (cmd) { |
| 1191 | case SNDRV_PCM_TRIGGER_START: |
| 1192 | pr_debug("Trigger Start\n"); |
| 1193 | |
| 1194 | /* Disable local INTRs till register prgmng is done */ |
| 1195 | if (had_get_hwstate(intelhaddata)) { |
| 1196 | pr_err("_START: HDMI cable plugged-out\n"); |
| 1197 | retval = -ENODEV; |
| 1198 | break; |
| 1199 | } |
| 1200 | stream->stream_status = STREAM_RUNNING; |
| 1201 | |
| 1202 | had_stream->stream_type = HAD_RUNNING_STREAM; |
| 1203 | |
| 1204 | /* Enable Audio */ |
| 1205 | /* |
| 1206 | * ToDo: Need to enable UNDERRUN interrupts as well |
| 1207 | * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1208 | */ |
| 1209 | caps = HDMI_AUDIO_BUFFER_DONE; |
| 1210 | retval = had_set_caps(HAD_SET_ENABLE_AUDIO_INT, &caps); |
| 1211 | retval = had_set_caps(HAD_SET_ENABLE_AUDIO, NULL); |
| 1212 | intelhaddata->ops->enable_audio(substream, 1); |
| 1213 | |
| 1214 | pr_debug("Processed _Start\n"); |
| 1215 | |
| 1216 | break; |
| 1217 | |
| 1218 | case SNDRV_PCM_TRIGGER_STOP: |
| 1219 | pr_debug("Trigger Stop\n"); |
| 1220 | spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq); |
| 1221 | intelhaddata->stream_info.str_id = 0; |
| 1222 | intelhaddata->curr_buf = 0; |
| 1223 | |
| 1224 | /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/ |
| 1225 | |
| 1226 | had_stream->stream_type = HAD_INIT; |
| 1227 | spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq); |
| 1228 | /* Disable Audio */ |
| 1229 | /* |
| 1230 | * ToDo: Need to disable UNDERRUN interrupts as well |
| 1231 | * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1232 | */ |
| 1233 | caps = HDMI_AUDIO_BUFFER_DONE; |
| 1234 | had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps); |
| 1235 | intelhaddata->ops->enable_audio(substream, 0); |
| 1236 | /* Reset buffer pointers */ |
| 1237 | intelhaddata->ops->reset_audio(1); |
| 1238 | intelhaddata->ops->reset_audio(0); |
| 1239 | stream->stream_status = STREAM_DROPPED; |
| 1240 | had_set_caps(HAD_SET_DISABLE_AUDIO, NULL); |
| 1241 | break; |
| 1242 | |
| 1243 | default: |
| 1244 | retval = -EINVAL; |
| 1245 | } |
| 1246 | return retval; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * snd_intelhad_pcm_prepare- internal preparation before starting a stream |
| 1251 | * |
| 1252 | * @substream: substream for which the function is called |
| 1253 | * |
| 1254 | * This function is called when a stream is started for internal preparation. |
| 1255 | */ |
| 1256 | static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream) |
| 1257 | { |
| 1258 | int retval; |
| 1259 | u32 disp_samp_freq, n_param; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1260 | u32 link_rate = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1261 | struct snd_intelhad *intelhaddata; |
| 1262 | struct snd_pcm_runtime *runtime; |
| 1263 | struct had_pvt_data *had_stream; |
| 1264 | |
| 1265 | pr_debug("snd_intelhad_pcm_prepare called\n"); |
| 1266 | |
| 1267 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1268 | runtime = substream->runtime; |
| 1269 | had_stream = intelhaddata->private_data; |
| 1270 | |
| 1271 | if (had_get_hwstate(intelhaddata)) { |
| 1272 | pr_err("%s: HDMI cable plugged-out\n", __func__); |
| 1273 | retval = -ENODEV; |
| 1274 | goto prep_end; |
| 1275 | } |
| 1276 | |
| 1277 | pr_debug("period_size=%d\n", |
| 1278 | (int)frames_to_bytes(runtime, runtime->period_size)); |
| 1279 | pr_debug("periods=%d\n", runtime->periods); |
| 1280 | pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream)); |
| 1281 | pr_debug("rate=%d\n", runtime->rate); |
| 1282 | pr_debug("channels=%d\n", runtime->channels); |
| 1283 | |
| 1284 | if (intelhaddata->stream_info.str_id) { |
| 1285 | pr_debug("_prepare is called for existing str_id#%d\n", |
| 1286 | intelhaddata->stream_info.str_id); |
| 1287 | retval = snd_intelhad_pcm_trigger(substream, |
| 1288 | SNDRV_PCM_TRIGGER_STOP); |
| 1289 | return retval; |
| 1290 | } |
| 1291 | |
| 1292 | retval = snd_intelhad_init_stream(substream); |
| 1293 | if (retval) |
| 1294 | goto prep_end; |
| 1295 | |
| 1296 | |
| 1297 | /* Get N value in KHz */ |
| 1298 | retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq); |
| 1299 | if (retval) { |
| 1300 | pr_err("querying display sampling freq failed %#x\n", retval); |
| 1301 | goto prep_end; |
| 1302 | } |
| 1303 | |
| 1304 | had_get_caps(HAD_GET_ELD, &intelhaddata->eeld); |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1305 | had_get_caps(HAD_GET_DP_OUTPUT, &intelhaddata->dp_output); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1306 | |
| 1307 | retval = intelhaddata->ops->prog_n(substream->runtime->rate, &n_param, |
| 1308 | intelhaddata); |
| 1309 | if (retval) { |
| 1310 | pr_err("programming N value failed %#x\n", retval); |
| 1311 | goto prep_end; |
| 1312 | } |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1313 | |
| 1314 | if (intelhaddata->dp_output) |
| 1315 | had_get_caps(HAD_GET_LINK_RATE, &link_rate); |
| 1316 | |
| 1317 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1318 | intelhaddata->ops->prog_cts(substream->runtime->rate, |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1319 | disp_samp_freq, link_rate, |
| 1320 | n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1321 | |
| 1322 | intelhaddata->ops->prog_dip(substream, intelhaddata); |
| 1323 | |
| 1324 | retval = intelhaddata->ops->audio_ctrl(substream, intelhaddata); |
| 1325 | |
| 1326 | /* Prog buffer address */ |
| 1327 | retval = snd_intelhad_prog_buffer(intelhaddata, |
| 1328 | HAD_BUF_TYPE_A, HAD_BUF_TYPE_D); |
| 1329 | |
| 1330 | /* |
| 1331 | * Program channel mapping in following order: |
| 1332 | * FL, FR, C, LFE, RL, RR |
| 1333 | */ |
| 1334 | |
| 1335 | had_write_register(AUD_BUF_CH_SWAP, SWAP_LFE_CENTER); |
| 1336 | |
| 1337 | prep_end: |
| 1338 | return retval; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw |
| 1343 | * |
| 1344 | * @substream: substream for which the function is called |
| 1345 | * |
| 1346 | * This function is called by ALSA framework to get the current hw buffer ptr |
| 1347 | * when a period is elapsed |
| 1348 | */ |
| 1349 | static snd_pcm_uframes_t snd_intelhad_pcm_pointer( |
| 1350 | struct snd_pcm_substream *substream) |
| 1351 | { |
| 1352 | struct snd_intelhad *intelhaddata; |
| 1353 | u32 bytes_rendered = 0; |
| 1354 | u32 t; |
| 1355 | int buf_id; |
| 1356 | |
| 1357 | /* pr_debug("snd_intelhad_pcm_pointer called\n"); */ |
| 1358 | |
| 1359 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1360 | |
| 1361 | if (intelhaddata->flag_underrun) { |
| 1362 | intelhaddata->flag_underrun = 0; |
| 1363 | return SNDRV_PCM_POS_XRUN; |
| 1364 | } |
| 1365 | |
| 1366 | /* Use a hw register to calculate sub-period position reports. |
| 1367 | * This makes PulseAudio happier. |
| 1368 | */ |
| 1369 | |
| 1370 | buf_id = intelhaddata->curr_buf % 4; |
| 1371 | had_read_register(AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t); |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame] | 1372 | |
| 1373 | if ((t == 0) || (t == ((u32)-1L))) { |
| 1374 | underrun_count++; |
| 1375 | pr_debug("discovered buffer done for buf %d, count = %d\n", |
| 1376 | buf_id, underrun_count); |
| 1377 | |
| 1378 | if (underrun_count > (HAD_MIN_PERIODS/2)) { |
| 1379 | pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n", |
| 1380 | underrun_count); |
| 1381 | underrun_count = 0; |
| 1382 | return SNDRV_PCM_POS_XRUN; |
| 1383 | } |
| 1384 | } else { |
| 1385 | /* Reset Counter */ |
| 1386 | underrun_count = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1387 | } |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame] | 1388 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1389 | t = intelhaddata->buf_info[buf_id].buf_size - t; |
| 1390 | |
| 1391 | if (intelhaddata->stream_info.buffer_rendered) |
| 1392 | div_u64_rem(intelhaddata->stream_info.buffer_rendered, |
| 1393 | intelhaddata->stream_info.ring_buf_size, |
| 1394 | &(bytes_rendered)); |
| 1395 | |
| 1396 | intelhaddata->stream_info.buffer_ptr = bytes_to_frames( |
| 1397 | substream->runtime, |
| 1398 | bytes_rendered + t); |
| 1399 | return intelhaddata->stream_info.buffer_ptr; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data |
| 1404 | * |
| 1405 | * @substream: substream for which the function is called |
| 1406 | * @vma: struct instance of memory VMM memory area |
| 1407 | * |
| 1408 | * This function is called by OS when a user space component |
| 1409 | * tries to get mmap memory from driver |
| 1410 | */ |
| 1411 | static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream, |
| 1412 | struct vm_area_struct *vma) |
| 1413 | { |
| 1414 | |
| 1415 | pr_debug("snd_intelhad_pcm_mmap called\n"); |
| 1416 | |
| 1417 | pr_debug("entry with prot:%s\n", __func__); |
| 1418 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 1419 | return remap_pfn_range(vma, vma->vm_start, |
| 1420 | substream->dma_buffer.addr >> PAGE_SHIFT, |
| 1421 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 1422 | } |
| 1423 | |
| 1424 | int hdmi_audio_mode_change(struct snd_pcm_substream *substream) |
| 1425 | { |
| 1426 | int retval = 0; |
| 1427 | u32 disp_samp_freq, n_param; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1428 | u32 link_rate = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1429 | struct snd_intelhad *intelhaddata; |
| 1430 | |
| 1431 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1432 | |
| 1433 | /* Disable Audio */ |
| 1434 | intelhaddata->ops->enable_audio(substream, 0); |
| 1435 | |
| 1436 | /* Update CTS value */ |
| 1437 | retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq); |
| 1438 | if (retval) { |
| 1439 | pr_err("querying display sampling freq failed %#x\n", retval); |
| 1440 | goto out; |
| 1441 | } |
| 1442 | |
| 1443 | retval = intelhaddata->ops->prog_n(substream->runtime->rate, &n_param, |
| 1444 | intelhaddata); |
| 1445 | if (retval) { |
| 1446 | pr_err("programming N value failed %#x\n", retval); |
| 1447 | goto out; |
| 1448 | } |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1449 | |
| 1450 | if (intelhaddata->dp_output) |
| 1451 | had_get_caps(HAD_GET_LINK_RATE, &link_rate); |
| 1452 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1453 | intelhaddata->ops->prog_cts(substream->runtime->rate, |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1454 | disp_samp_freq, link_rate, |
| 1455 | n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1456 | |
| 1457 | /* Enable Audio */ |
| 1458 | intelhaddata->ops->enable_audio(substream, 1); |
| 1459 | |
| 1460 | out: |
| 1461 | return retval; |
| 1462 | } |
| 1463 | |
| 1464 | /*PCM operations structure and the calls back for the same */ |
| 1465 | struct snd_pcm_ops snd_intelhad_playback_ops = { |
| 1466 | .open = snd_intelhad_open, |
| 1467 | .close = snd_intelhad_close, |
| 1468 | .ioctl = snd_pcm_lib_ioctl, |
| 1469 | .hw_params = snd_intelhad_hw_params, |
| 1470 | .hw_free = snd_intelhad_hw_free, |
| 1471 | .prepare = snd_intelhad_pcm_prepare, |
| 1472 | .trigger = snd_intelhad_pcm_trigger, |
| 1473 | .pointer = snd_intelhad_pcm_pointer, |
| 1474 | .mmap = snd_intelhad_pcm_mmap, |
| 1475 | }; |
| 1476 | |
| 1477 | /** |
| 1478 | * snd_intelhad_create - to crete alsa card instance |
| 1479 | * |
| 1480 | * @intelhaddata: pointer to internal context |
| 1481 | * @card: pointer to card |
| 1482 | * |
| 1483 | * This function is called when the hdmi cable is plugged in |
| 1484 | */ |
| 1485 | static int snd_intelhad_create( |
| 1486 | struct snd_intelhad *intelhaddata, |
| 1487 | struct snd_card *card) |
| 1488 | { |
| 1489 | int retval; |
| 1490 | static struct snd_device_ops ops = { |
| 1491 | }; |
| 1492 | |
| 1493 | pr_debug("snd_intelhad_create called\n"); |
| 1494 | |
| 1495 | if (!intelhaddata) |
| 1496 | return -EINVAL; |
| 1497 | |
| 1498 | /* ALSA api to register the device */ |
| 1499 | retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops); |
| 1500 | return retval; |
| 1501 | } |
| 1502 | /** |
| 1503 | * snd_intelhad_pcm_free - to free the memory allocated |
| 1504 | * |
| 1505 | * @pcm: pointer to pcm instance |
| 1506 | * This function is called when the device is removed |
| 1507 | */ |
| 1508 | static void snd_intelhad_pcm_free(struct snd_pcm *pcm) |
| 1509 | { |
| 1510 | pr_debug("Freeing PCM preallocated pages\n"); |
| 1511 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 1512 | } |
| 1513 | |
| 1514 | static int had_iec958_info(struct snd_kcontrol *kcontrol, |
| 1515 | struct snd_ctl_elem_info *uinfo) |
| 1516 | { |
| 1517 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 1518 | uinfo->count = 1; |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | static int had_iec958_get(struct snd_kcontrol *kcontrol, |
| 1523 | struct snd_ctl_elem_value *ucontrol) |
| 1524 | { |
| 1525 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1526 | |
| 1527 | ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff; |
| 1528 | ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff; |
| 1529 | ucontrol->value.iec958.status[2] = |
| 1530 | (intelhaddata->aes_bits >> 16) & 0xff; |
| 1531 | ucontrol->value.iec958.status[3] = |
| 1532 | (intelhaddata->aes_bits >> 24) & 0xff; |
| 1533 | return 0; |
| 1534 | } |
| 1535 | static int had_iec958_mask_get(struct snd_kcontrol *kcontrol, |
| 1536 | struct snd_ctl_elem_value *ucontrol) |
| 1537 | { |
| 1538 | ucontrol->value.iec958.status[0] = 0xff; |
| 1539 | ucontrol->value.iec958.status[1] = 0xff; |
| 1540 | ucontrol->value.iec958.status[2] = 0xff; |
| 1541 | ucontrol->value.iec958.status[3] = 0xff; |
| 1542 | return 0; |
| 1543 | } |
| 1544 | static int had_iec958_put(struct snd_kcontrol *kcontrol, |
| 1545 | struct snd_ctl_elem_value *ucontrol) |
| 1546 | { |
| 1547 | unsigned int val; |
| 1548 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1549 | |
| 1550 | pr_debug("entered had_iec958_put\n"); |
| 1551 | val = (ucontrol->value.iec958.status[0] << 0) | |
| 1552 | (ucontrol->value.iec958.status[1] << 8) | |
| 1553 | (ucontrol->value.iec958.status[2] << 16) | |
| 1554 | (ucontrol->value.iec958.status[3] << 24); |
| 1555 | if (intelhaddata->aes_bits != val) { |
| 1556 | intelhaddata->aes_bits = val; |
| 1557 | return 1; |
| 1558 | } |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | static struct snd_kcontrol_new had_control_iec958_mask = { |
| 1563 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1564 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1565 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK), |
| 1566 | .info = had_iec958_info, /* shared */ |
| 1567 | .get = had_iec958_mask_get, |
| 1568 | }; |
| 1569 | |
| 1570 | static struct snd_kcontrol_new had_control_iec958 = { |
| 1571 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1572 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 1573 | .info = had_iec958_info, |
| 1574 | .get = had_iec958_get, |
| 1575 | .put = had_iec958_put |
| 1576 | }; |
| 1577 | |
| 1578 | static struct snd_intel_had_interface had_interface = { |
| 1579 | .name = "hdmi-audio", |
| 1580 | .query = hdmi_audio_query, |
| 1581 | .suspend = hdmi_audio_suspend, |
| 1582 | .resume = hdmi_audio_resume, |
| 1583 | }; |
| 1584 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1585 | static struct had_ops had_ops_v2 = { |
| 1586 | .enable_audio = snd_intelhad_enable_audio_v2, |
| 1587 | .reset_audio = snd_intelhad_reset_audio_v2, |
| 1588 | .prog_n = snd_intelhad_prog_n_v2, |
| 1589 | .prog_cts = snd_intelhad_prog_cts_v2, |
| 1590 | .audio_ctrl = snd_intelhad_prog_audio_ctrl_v2, |
| 1591 | .prog_dip = snd_intelhad_prog_dip_v2, |
| 1592 | .handle_underrun = had_clear_underrun_intr_v2, |
| 1593 | }; |
| 1594 | /** |
| 1595 | * hdmi_audio_probe - to create sound card instance for HDMI audio playabck |
| 1596 | * |
| 1597 | *@haddata: pointer to HAD private data |
| 1598 | *@card_id: card for which probe is called |
| 1599 | * |
| 1600 | * This function is called when the hdmi cable is plugged in. This function |
| 1601 | * creates and registers the sound card with ALSA |
| 1602 | */ |
| 1603 | int hdmi_audio_probe(void *deviceptr) |
| 1604 | { |
| 1605 | int retval; |
| 1606 | struct snd_pcm *pcm; |
| 1607 | struct snd_card *card; |
| 1608 | struct had_callback_ops ops_cb; |
| 1609 | struct snd_intelhad *intelhaddata; |
| 1610 | struct had_pvt_data *had_stream; |
| 1611 | struct platform_device *devptr = deviceptr; |
| 1612 | |
| 1613 | pr_debug("Enter %s\n", __func__); |
| 1614 | |
| 1615 | pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask); |
| 1616 | |
| 1617 | /* allocate memory for saving internal context and working */ |
| 1618 | intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL); |
| 1619 | if (!intelhaddata) |
| 1620 | return -ENOMEM; |
| 1621 | |
| 1622 | had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL); |
| 1623 | if (!had_stream) { |
| 1624 | retval = -ENOMEM; |
| 1625 | goto free_haddata; |
| 1626 | } |
| 1627 | |
| 1628 | had_data = intelhaddata; |
| 1629 | ops_cb.intel_had_event_call_back = had_event_handler; |
| 1630 | |
| 1631 | /* registering with display driver to get access to display APIs */ |
| 1632 | |
| 1633 | retval = mid_hdmi_audio_setup( |
| 1634 | ops_cb.intel_had_event_call_back, |
| 1635 | &(intelhaddata->reg_ops), |
| 1636 | &(intelhaddata->query_ops)); |
| 1637 | if (retval) { |
| 1638 | pr_err("querying display driver APIs failed %#x\n", retval); |
| 1639 | goto free_hadstream; |
| 1640 | } |
| 1641 | mutex_lock(&had_mutex); |
| 1642 | spin_lock_init(&intelhaddata->had_spinlock); |
| 1643 | intelhaddata->drv_status = HAD_DRV_DISCONNECTED; |
| 1644 | pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n", |
| 1645 | __func__, __LINE__); |
| 1646 | |
| 1647 | /* create a card instance with ALSA framework */ |
| 1648 | retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id, |
| 1649 | THIS_MODULE, 0, &card); |
| 1650 | |
| 1651 | if (retval) |
| 1652 | goto unlock_mutex; |
| 1653 | intelhaddata->card = card; |
| 1654 | intelhaddata->card_id = hdmi_card_id; |
| 1655 | intelhaddata->card_index = card->number; |
| 1656 | intelhaddata->private_data = had_stream; |
| 1657 | intelhaddata->flag_underrun = 0; |
| 1658 | intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF; |
| 1659 | strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD)); |
| 1660 | strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD)); |
| 1661 | |
| 1662 | retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS, |
| 1663 | MAX_CAP_STREAMS, &pcm); |
| 1664 | if (retval) |
| 1665 | goto err; |
| 1666 | |
| 1667 | /* setup private data which can be retrieved when required */ |
| 1668 | pcm->private_data = intelhaddata; |
| 1669 | pcm->private_free = snd_intelhad_pcm_free; |
| 1670 | pcm->info_flags = 0; |
| 1671 | strncpy(pcm->name, card->shortname, strlen(card->shortname)); |
| 1672 | /* setup the ops for palyabck */ |
| 1673 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 1674 | &snd_intelhad_playback_ops); |
| 1675 | /* allocate dma pages for ALSA stream operations |
| 1676 | * memory allocated is based on size, not max value |
| 1677 | * thus using same argument for max & size |
| 1678 | */ |
| 1679 | retval = snd_pcm_lib_preallocate_pages_for_all(pcm, |
| 1680 | SNDRV_DMA_TYPE_DEV, NULL, |
| 1681 | HAD_MAX_BUFFER, HAD_MAX_BUFFER); |
| 1682 | |
| 1683 | if (card->dev == NULL) |
| 1684 | pr_debug("card->dev is NULL!!!!! Should not be this case\n"); |
| 1685 | else if (card->dev->dma_mask == NULL) |
| 1686 | pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n"); |
| 1687 | else |
| 1688 | pr_debug("hdmi_audio_probe dma_mask is : %p\n", |
| 1689 | card->dev->dma_mask); |
| 1690 | |
| 1691 | if (retval) |
| 1692 | goto err; |
| 1693 | |
| 1694 | /* internal function call to register device with ALSA */ |
| 1695 | retval = snd_intelhad_create(intelhaddata, card); |
| 1696 | if (retval) |
| 1697 | goto err; |
| 1698 | |
| 1699 | card->private_data = &intelhaddata; |
| 1700 | retval = snd_card_register(card); |
| 1701 | if (retval) |
| 1702 | goto err; |
| 1703 | |
| 1704 | /* IEC958 controls */ |
| 1705 | retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask, |
| 1706 | intelhaddata)); |
| 1707 | if (retval < 0) |
| 1708 | goto err; |
| 1709 | retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958, |
| 1710 | intelhaddata)); |
| 1711 | if (retval < 0) |
| 1712 | goto err; |
| 1713 | |
| 1714 | init_channel_allocations(); |
| 1715 | |
| 1716 | /* Register channel map controls */ |
| 1717 | retval = had_register_chmap_ctls(intelhaddata, pcm); |
| 1718 | if (retval < 0) |
| 1719 | goto err; |
| 1720 | |
| 1721 | intelhaddata->dev = &devptr->dev; |
| 1722 | pm_runtime_set_active(intelhaddata->dev); |
| 1723 | pm_runtime_enable(intelhaddata->dev); |
| 1724 | |
| 1725 | mutex_unlock(&had_mutex); |
| 1726 | retval = mid_hdmi_audio_register(&had_interface, intelhaddata); |
| 1727 | if (retval) { |
| 1728 | pr_err("registering with display driver failed %#x\n", retval); |
| 1729 | snd_card_free(card); |
| 1730 | goto free_hadstream; |
| 1731 | } |
| 1732 | |
| 1733 | intelhaddata->hw_silence = 1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1734 | intelhaddata->ops = &had_ops_v2; |
| 1735 | |
| 1736 | return retval; |
| 1737 | err: |
| 1738 | snd_card_free(card); |
| 1739 | unlock_mutex: |
| 1740 | mutex_unlock(&had_mutex); |
| 1741 | free_hadstream: |
| 1742 | kfree(had_stream); |
| 1743 | pm_runtime_disable(intelhaddata->dev); |
| 1744 | intelhaddata->dev = NULL; |
| 1745 | free_haddata: |
| 1746 | kfree(intelhaddata); |
| 1747 | intelhaddata = NULL; |
| 1748 | pr_err("Error returned from %s api %#x\n", __func__, retval); |
| 1749 | return retval; |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * hdmi_audio_remove - removes the alsa card |
| 1754 | * |
| 1755 | *@haddata: pointer to HAD private data |
| 1756 | * |
| 1757 | * This function is called when the hdmi cable is un-plugged. This function |
| 1758 | * free the sound card. |
| 1759 | */ |
| 1760 | int hdmi_audio_remove(void *pdevptr) |
| 1761 | { |
| 1762 | struct snd_intelhad *intelhaddata = had_data; |
| 1763 | int caps; |
| 1764 | |
| 1765 | pr_debug("Enter %s\n", __func__); |
| 1766 | |
| 1767 | if (!intelhaddata) |
| 1768 | return 0; |
| 1769 | |
| 1770 | if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) { |
| 1771 | caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1772 | had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps); |
| 1773 | had_set_caps(HAD_SET_DISABLE_AUDIO, NULL); |
| 1774 | } |
| 1775 | snd_card_free(intelhaddata->card); |
| 1776 | kfree(intelhaddata->private_data); |
| 1777 | kfree(intelhaddata); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>"); |
| 1782 | MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>"); |
| 1783 | MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>"); |
| 1784 | MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>"); |
| 1785 | MODULE_DESCRIPTION("Intel HDMI Audio driver"); |
| 1786 | MODULE_LICENSE("GPL v2"); |
| 1787 | MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}"); |