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 | |
| 283 | static void snd_intelhad_enable_audio_v1(struct snd_pcm_substream *substream, |
| 284 | u8 enable) |
| 285 | { |
| 286 | had_read_modify(AUD_CONFIG, enable, BIT(0)); |
| 287 | } |
| 288 | |
| 289 | static void snd_intelhad_enable_audio_v2(struct snd_pcm_substream *substream, |
| 290 | u8 enable) |
| 291 | { |
| 292 | had_read_modify_aud_config_v2(substream, enable, BIT(0)); |
| 293 | } |
| 294 | |
| 295 | static void snd_intelhad_reset_audio_v1(u8 reset) |
| 296 | { |
| 297 | had_write_register(AUD_HDMI_STATUS, reset); |
| 298 | } |
| 299 | |
| 300 | static void snd_intelhad_reset_audio_v2(u8 reset) |
| 301 | { |
| 302 | had_write_register(AUD_HDMI_STATUS_v2, reset); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * initialize audio channel status registers |
| 307 | * This function is called in the prepare callback |
| 308 | */ |
| 309 | static int had_prog_status_reg(struct snd_pcm_substream *substream, |
| 310 | struct snd_intelhad *intelhaddata) |
| 311 | { |
| 312 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 313 | union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0}; |
| 314 | union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0}; |
| 315 | int format; |
| 316 | |
| 317 | pr_debug("Entry %s\n", __func__); |
| 318 | |
| 319 | ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits & |
| 320 | IEC958_AES0_NONAUDIO)>>1; |
| 321 | ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits & |
| 322 | IEC958_AES3_CON_CLOCK)>>4; |
| 323 | cfg_val.cfg_regx.val_bit = ch_stat0.status_0_regx.lpcm_id; |
| 324 | |
| 325 | switch (substream->runtime->rate) { |
| 326 | case AUD_SAMPLE_RATE_32: |
| 327 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ; |
| 328 | break; |
| 329 | |
| 330 | case AUD_SAMPLE_RATE_44_1: |
| 331 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ; |
| 332 | break; |
| 333 | case AUD_SAMPLE_RATE_48: |
| 334 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ; |
| 335 | break; |
| 336 | case AUD_SAMPLE_RATE_88_2: |
| 337 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ; |
| 338 | break; |
| 339 | case AUD_SAMPLE_RATE_96: |
| 340 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ; |
| 341 | break; |
| 342 | case AUD_SAMPLE_RATE_176_4: |
| 343 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ; |
| 344 | break; |
| 345 | case AUD_SAMPLE_RATE_192: |
| 346 | ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ; |
| 347 | break; |
| 348 | |
| 349 | default: |
| 350 | /* control should never come here */ |
| 351 | return -EINVAL; |
| 352 | break; |
| 353 | |
| 354 | } |
| 355 | had_write_register(AUD_CH_STATUS_0, ch_stat0.status_0_regval); |
| 356 | |
| 357 | format = substream->runtime->format; |
| 358 | |
| 359 | if (format == SNDRV_PCM_FORMAT_S16_LE) { |
| 360 | ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20; |
| 361 | ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS; |
| 362 | } else if (format == SNDRV_PCM_FORMAT_S24_LE) { |
| 363 | ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24; |
| 364 | ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS; |
| 365 | } else { |
| 366 | ch_stat1.status_1_regx.max_wrd_len = 0; |
| 367 | ch_stat1.status_1_regx.wrd_len = 0; |
| 368 | } |
| 369 | had_write_register(AUD_CH_STATUS_1, ch_stat1.status_1_regval); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * function to initialize audio |
| 375 | * registers and buffer confgiuration registers |
| 376 | * This function is called in the prepare callback |
| 377 | */ |
| 378 | static int snd_intelhad_prog_audio_ctrl_v2(struct snd_pcm_substream *substream, |
| 379 | struct snd_intelhad *intelhaddata) |
| 380 | { |
| 381 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 382 | union aud_buf_config buf_cfg = {.buf_cfgval = 0}; |
| 383 | u8 channels; |
| 384 | |
| 385 | had_prog_status_reg(substream, intelhaddata); |
| 386 | |
| 387 | buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD; |
| 388 | buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD; |
| 389 | buf_cfg.buf_cfg_regx_v2.aud_delay = 0; |
| 390 | had_write_register(AUD_BUF_CONFIG, buf_cfg.buf_cfgval); |
| 391 | |
| 392 | channels = substream->runtime->channels; |
| 393 | cfg_val.cfg_regx_v2.num_ch = channels - 2; |
| 394 | if (channels <= 2) |
| 395 | cfg_val.cfg_regx_v2.layout = LAYOUT0; |
| 396 | else |
| 397 | cfg_val.cfg_regx_v2.layout = LAYOUT1; |
| 398 | |
| 399 | had_write_register(AUD_CONFIG, cfg_val.cfg_regval); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * function to initialize audio |
| 405 | * registers and buffer confgiuration registers |
| 406 | * This function is called in the prepare callback |
| 407 | */ |
| 408 | static int snd_intelhad_prog_audio_ctrl_v1(struct snd_pcm_substream *substream, |
| 409 | struct snd_intelhad *intelhaddata) |
| 410 | { |
| 411 | union aud_cfg cfg_val = {.cfg_regval = 0}; |
| 412 | union aud_buf_config buf_cfg = {.buf_cfgval = 0}; |
| 413 | u8 channels; |
| 414 | |
| 415 | had_prog_status_reg(substream, intelhaddata); |
| 416 | |
| 417 | buf_cfg.buf_cfg_regx.fifo_width = FIFO_THRESHOLD; |
| 418 | buf_cfg.buf_cfg_regx.aud_delay = 0; |
| 419 | had_write_register(AUD_BUF_CONFIG, buf_cfg.buf_cfgval); |
| 420 | |
| 421 | channels = substream->runtime->channels; |
| 422 | |
| 423 | switch (channels) { |
| 424 | case 1: |
| 425 | case 2: |
| 426 | cfg_val.cfg_regx.num_ch = CH_STEREO; |
| 427 | cfg_val.cfg_regx.layout = LAYOUT0; |
| 428 | break; |
| 429 | |
| 430 | case 3: |
| 431 | case 4: |
| 432 | cfg_val.cfg_regx.num_ch = CH_THREE_FOUR; |
| 433 | cfg_val.cfg_regx.layout = LAYOUT1; |
| 434 | break; |
| 435 | |
| 436 | case 5: |
| 437 | case 6: |
| 438 | cfg_val.cfg_regx.num_ch = CH_FIVE_SIX; |
| 439 | cfg_val.cfg_regx.layout = LAYOUT1; |
| 440 | break; |
| 441 | |
| 442 | case 7: |
| 443 | case 8: |
| 444 | cfg_val.cfg_regx.num_ch = CH_SEVEN_EIGHT; |
| 445 | cfg_val.cfg_regx.layout = LAYOUT1; |
| 446 | break; |
| 447 | |
| 448 | } |
| 449 | |
| 450 | had_write_register(AUD_CONFIG, cfg_val.cfg_regval); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Compute derived values in channel_allocations[]. |
| 456 | */ |
| 457 | static void init_channel_allocations(void) |
| 458 | { |
| 459 | int i, j; |
| 460 | struct cea_channel_speaker_allocation *p; |
| 461 | |
| 462 | pr_debug("%s: Enter\n", __func__); |
| 463 | |
| 464 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 465 | p = channel_allocations + i; |
| 466 | p->channels = 0; |
| 467 | p->spk_mask = 0; |
| 468 | for (j = 0; j < ARRAY_SIZE(p->speakers); j++) |
| 469 | if (p->speakers[j]) { |
| 470 | p->channels++; |
| 471 | p->spk_mask |= p->speakers[j]; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * The transformation takes two steps: |
| 478 | * |
| 479 | * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask |
| 480 | * spk_mask => (channel_allocations[]) => ai->CA |
| 481 | * |
| 482 | * TODO: it could select the wrong CA from multiple candidates. |
| 483 | */ |
| 484 | static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata, |
| 485 | int channels) |
| 486 | { |
| 487 | int i; |
| 488 | int ca = 0; |
| 489 | int spk_mask = 0; |
| 490 | |
| 491 | /* |
| 492 | * CA defaults to 0 for basic stereo audio |
| 493 | */ |
| 494 | if (channels <= 2) |
| 495 | return 0; |
| 496 | |
| 497 | /* |
| 498 | * expand ELD's speaker allocation mask |
| 499 | * |
| 500 | * ELD tells the speaker mask in a compact(paired) form, |
| 501 | * expand ELD's notions to match the ones used by Audio InfoFrame. |
| 502 | */ |
| 503 | |
| 504 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
| 505 | if (intelhaddata->eeld.speaker_allocation_block & (1 << i)) |
| 506 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 507 | } |
| 508 | |
| 509 | /* search for the first working match in the CA table */ |
| 510 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 511 | if (channels == channel_allocations[i].channels && |
| 512 | (spk_mask & channel_allocations[i].spk_mask) == |
| 513 | channel_allocations[i].spk_mask) { |
| 514 | ca = channel_allocations[i].ca_index; |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels); |
| 520 | |
| 521 | return ca; |
| 522 | } |
| 523 | |
| 524 | /* from speaker bit mask to ALSA API channel position */ |
| 525 | static int spk_to_chmap(int spk) |
| 526 | { |
| 527 | struct channel_map_table *t = map_tables; |
| 528 | |
| 529 | for (; t->map; t++) { |
| 530 | if (t->spk_mask == spk) |
| 531 | return t->map; |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata) |
| 537 | { |
| 538 | int i = 0, c = 0; |
| 539 | int spk_mask = 0; |
| 540 | struct snd_pcm_chmap_elem *chmap; |
| 541 | u8 eld_high, eld_high_mask = 0xF0; |
| 542 | u8 high_msb; |
| 543 | |
| 544 | chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); |
| 545 | if (chmap == NULL) { |
| 546 | intelhaddata->chmap->chmap = NULL; |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | had_get_caps(HAD_GET_ELD, &intelhaddata->eeld); |
| 551 | |
| 552 | pr_debug("eeld.speaker_allocation_block = %x\n", |
| 553 | intelhaddata->eeld.speaker_allocation_block); |
| 554 | |
| 555 | /* WA: Fix the max channel supported to 8 */ |
| 556 | |
| 557 | /* |
| 558 | * Sink may support more than 8 channels, if eld_high has more than |
| 559 | * one bit set. SOC supports max 8 channels. |
| 560 | * Refer eld_speaker_allocation_bits, for sink speaker allocation |
| 561 | */ |
| 562 | |
| 563 | /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */ |
| 564 | eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask; |
| 565 | if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) { |
| 566 | /* eld_high & (eld_high-1): if more than 1 bit set */ |
| 567 | /* 0x1F: 7 channels */ |
| 568 | for (i = 1; i < 4; i++) { |
| 569 | high_msb = eld_high & (0x80 >> i); |
| 570 | if (high_msb) { |
| 571 | intelhaddata->eeld.speaker_allocation_block &= |
| 572 | high_msb | 0xF; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
| 579 | if (intelhaddata->eeld.speaker_allocation_block & (1 << i)) |
| 580 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 581 | } |
| 582 | |
| 583 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 584 | if (spk_mask == channel_allocations[i].spk_mask) { |
| 585 | for (c = 0; c < channel_allocations[i].channels; c++) { |
| 586 | chmap->map[c] = spk_to_chmap( |
| 587 | channel_allocations[i].speakers[ |
| 588 | (MAX_SPEAKERS - 1)-c]); |
| 589 | } |
| 590 | chmap->channels = channel_allocations[i].channels; |
| 591 | intelhaddata->chmap->chmap = chmap; |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | if (i >= ARRAY_SIZE(channel_allocations)) { |
| 596 | intelhaddata->chmap->chmap = NULL; |
| 597 | kfree(chmap); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * ALSA API channel-map control callbacks |
| 603 | */ |
| 604 | static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol, |
| 605 | struct snd_ctl_elem_info *uinfo) |
| 606 | { |
| 607 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 608 | struct snd_intelhad *intelhaddata = info->private_data; |
| 609 | |
| 610 | if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) |
| 611 | return -ENODEV; |
| 612 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 613 | uinfo->count = HAD_MAX_CHANNEL; |
| 614 | uinfo->value.integer.min = 0; |
| 615 | uinfo->value.integer.max = SNDRV_CHMAP_LAST; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol, |
| 620 | struct snd_ctl_elem_value *ucontrol) |
| 621 | { |
| 622 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 623 | struct snd_intelhad *intelhaddata = info->private_data; |
| 624 | int i = 0; |
| 625 | const struct snd_pcm_chmap_elem *chmap; |
| 626 | |
| 627 | if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) |
| 628 | return -ENODEV; |
| 629 | if (intelhaddata->chmap->chmap == NULL) |
| 630 | return -ENODATA; |
| 631 | chmap = intelhaddata->chmap->chmap; |
| 632 | for (i = 0; i < chmap->channels; i++) { |
| 633 | ucontrol->value.integer.value[i] = chmap->map[i]; |
| 634 | pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]); |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata, |
| 641 | struct snd_pcm *pcm) |
| 642 | { |
| 643 | int err = 0; |
| 644 | |
| 645 | err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 646 | NULL, 0, (unsigned long)intelhaddata, |
| 647 | &intelhaddata->chmap); |
| 648 | if (err < 0) |
| 649 | return err; |
| 650 | |
| 651 | intelhaddata->chmap->private_data = intelhaddata; |
| 652 | intelhaddata->kctl = intelhaddata->chmap->kctl; |
| 653 | intelhaddata->kctl->info = had_chmap_ctl_info; |
| 654 | intelhaddata->kctl->get = had_chmap_ctl_get; |
| 655 | intelhaddata->chmap->chmap = NULL; |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * snd_intelhad_prog_dip_v1 - to initialize Data Island Packets registers |
| 661 | * |
| 662 | * @substream:substream for which the prepare function is called |
| 663 | * @intelhaddata:substream private data |
| 664 | * |
| 665 | * This function is called in the prepare callback |
| 666 | */ |
| 667 | static void snd_intelhad_prog_dip_v1(struct snd_pcm_substream *substream, |
| 668 | struct snd_intelhad *intelhaddata) |
| 669 | { |
| 670 | int i; |
| 671 | union aud_ctrl_st ctrl_state = {.ctrl_val = 0}; |
| 672 | union aud_info_frame2 frame2 = {.fr2_val = 0}; |
| 673 | union aud_info_frame3 frame3 = {.fr3_val = 0}; |
| 674 | u8 checksum = 0; |
| 675 | int channels; |
| 676 | |
| 677 | channels = substream->runtime->channels; |
| 678 | |
| 679 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 680 | |
| 681 | frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1; |
| 682 | |
| 683 | frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation( |
| 684 | intelhaddata, channels); |
| 685 | |
| 686 | /*Calculte the byte wide checksum for all valid DIP words*/ |
| 687 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 688 | checksum += (INFO_FRAME_WORD1 >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 689 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 690 | checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 691 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 692 | checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 693 | |
| 694 | frame2.fr2_regx.chksum = -(checksum); |
| 695 | |
| 696 | had_write_register(AUD_HDMIW_INFOFR, INFO_FRAME_WORD1); |
| 697 | had_write_register(AUD_HDMIW_INFOFR, frame2.fr2_val); |
| 698 | had_write_register(AUD_HDMIW_INFOFR, frame3.fr3_val); |
| 699 | |
| 700 | /* program remaining DIP words with zero */ |
| 701 | for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++) |
| 702 | had_write_register(AUD_HDMIW_INFOFR, 0x0); |
| 703 | |
| 704 | ctrl_state.ctrl_regx.dip_freq = 1; |
| 705 | ctrl_state.ctrl_regx.dip_en_sta = 1; |
| 706 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * snd_intelhad_prog_dip_v2 - to initialize Data Island Packets registers |
| 711 | * |
| 712 | * @substream:substream for which the prepare function is called |
| 713 | * @intelhaddata:substream private data |
| 714 | * |
| 715 | * This function is called in the prepare callback |
| 716 | */ |
| 717 | static void snd_intelhad_prog_dip_v2(struct snd_pcm_substream *substream, |
| 718 | struct snd_intelhad *intelhaddata) |
| 719 | { |
| 720 | int i; |
| 721 | union aud_ctrl_st ctrl_state = {.ctrl_val = 0}; |
| 722 | union aud_info_frame2 frame2 = {.fr2_val = 0}; |
| 723 | union aud_info_frame3 frame3 = {.fr3_val = 0}; |
| 724 | u8 checksum = 0; |
| 725 | int channels; |
| 726 | |
| 727 | channels = substream->runtime->channels; |
| 728 | |
| 729 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 730 | |
| 731 | frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1; |
| 732 | |
| 733 | frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation( |
| 734 | intelhaddata, channels); |
| 735 | |
| 736 | /*Calculte the byte wide checksum for all valid DIP words*/ |
| 737 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 738 | checksum += (INFO_FRAME_WORD1 >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 739 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 740 | checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 741 | for (i = 0; i < BYTES_PER_WORD; i++) |
| 742 | checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0; |
| 743 | |
| 744 | frame2.fr2_regx.chksum = -(checksum); |
| 745 | |
| 746 | had_write_register(AUD_HDMIW_INFOFR_v2, INFO_FRAME_WORD1); |
| 747 | had_write_register(AUD_HDMIW_INFOFR_v2, frame2.fr2_val); |
| 748 | had_write_register(AUD_HDMIW_INFOFR_v2, frame3.fr3_val); |
| 749 | |
| 750 | /* program remaining DIP words with zero */ |
| 751 | for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++) |
| 752 | had_write_register(AUD_HDMIW_INFOFR_v2, 0x0); |
| 753 | |
| 754 | ctrl_state.ctrl_regx.dip_freq = 1; |
| 755 | ctrl_state.ctrl_regx.dip_en_sta = 1; |
| 756 | had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * snd_intelhad_prog_buffer - programs buffer |
| 761 | * address and length registers |
| 762 | * |
| 763 | * @substream:substream for which the prepare function is called |
| 764 | * @intelhaddata:substream private data |
| 765 | * |
| 766 | * This function programs ring buffer address and length into registers. |
| 767 | */ |
| 768 | int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata, |
| 769 | int start, int end) |
| 770 | { |
| 771 | u32 ring_buf_addr, ring_buf_size, period_bytes; |
| 772 | u8 i, num_periods; |
| 773 | struct snd_pcm_substream *substream; |
| 774 | |
| 775 | substream = intelhaddata->stream_info.had_substream; |
| 776 | if (!substream) { |
| 777 | pr_err("substream is NULL\n"); |
| 778 | dump_stack(); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | ring_buf_addr = substream->runtime->dma_addr; |
| 783 | ring_buf_size = snd_pcm_lib_buffer_bytes(substream); |
| 784 | intelhaddata->stream_info.ring_buf_size = ring_buf_size; |
| 785 | period_bytes = frames_to_bytes(substream->runtime, |
| 786 | substream->runtime->period_size); |
| 787 | num_periods = substream->runtime->periods; |
| 788 | |
| 789 | /* |
| 790 | * buffer addr should be 64 byte aligned, period bytes |
| 791 | * will be used to calculate addr offset |
| 792 | */ |
| 793 | period_bytes &= ~0x3F; |
| 794 | |
| 795 | /* Hardware supports MAX_PERIODS buffers */ |
| 796 | if (end >= HAD_MAX_PERIODS) |
| 797 | return -EINVAL; |
| 798 | |
| 799 | for (i = start; i <= end; i++) { |
| 800 | /* Program the buf registers with addr and len */ |
| 801 | intelhaddata->buf_info[i].buf_addr = ring_buf_addr + |
| 802 | (i * period_bytes); |
| 803 | if (i < num_periods-1) |
| 804 | intelhaddata->buf_info[i].buf_size = period_bytes; |
| 805 | else |
| 806 | intelhaddata->buf_info[i].buf_size = ring_buf_size - |
| 807 | (period_bytes*i); |
| 808 | |
| 809 | had_write_register(AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH), |
| 810 | intelhaddata->buf_info[i].buf_addr | |
| 811 | BIT(0) | BIT(1)); |
| 812 | had_write_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH), |
| 813 | period_bytes); |
| 814 | intelhaddata->buf_info[i].is_valid = true; |
| 815 | } |
| 816 | pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end, |
| 817 | intelhaddata->buf_info[start].buf_addr, |
| 818 | intelhaddata->buf_info[start].buf_size); |
| 819 | intelhaddata->valid_buf_cnt = num_periods; |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | int snd_intelhad_read_len(struct snd_intelhad *intelhaddata) |
| 824 | { |
| 825 | int i, retval = 0; |
| 826 | u32 len[4]; |
| 827 | |
| 828 | for (i = 0; i < 4 ; i++) { |
| 829 | had_read_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH), |
| 830 | &len[i]); |
| 831 | if (!len[i]) |
| 832 | retval++; |
| 833 | } |
| 834 | if (retval != 1) { |
| 835 | for (i = 0; i < 4 ; i++) |
| 836 | pr_debug("buf[%d] size=%d\n", i, len[i]); |
| 837 | } |
| 838 | |
| 839 | return retval; |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * snd_intelhad_prog_cts_v1 - Program HDMI audio CTS value |
| 844 | * |
| 845 | * @aud_samp_freq: sampling frequency of audio data |
| 846 | * @tmds: sampling frequency of the display data |
| 847 | * @n_param: N value, depends on aud_samp_freq |
| 848 | * @intelhaddata:substream private data |
| 849 | * |
| 850 | * Program CTS register based on the audio and display sampling frequency |
| 851 | */ |
| 852 | static void snd_intelhad_prog_cts_v1(u32 aud_samp_freq, u32 tmds, u32 n_param, |
| 853 | struct snd_intelhad *intelhaddata) |
| 854 | { |
| 855 | u32 cts_val; |
| 856 | u64 dividend, divisor; |
| 857 | |
| 858 | /* Calculate CTS according to HDMI 1.3a spec*/ |
| 859 | dividend = (u64)tmds * n_param*1000; |
| 860 | divisor = 128 * aud_samp_freq; |
| 861 | cts_val = div64_u64(dividend, divisor); |
| 862 | pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n", |
| 863 | tmds, n_param, cts_val); |
| 864 | had_write_register(AUD_HDMI_CTS, (BIT(20) | cts_val)); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * snd_intelhad_prog_cts_v2 - Program HDMI audio CTS value |
| 869 | * |
| 870 | * @aud_samp_freq: sampling frequency of audio data |
| 871 | * @tmds: sampling frequency of the display data |
| 872 | * @n_param: N value, depends on aud_samp_freq |
| 873 | * @intelhaddata:substream private data |
| 874 | * |
| 875 | * Program CTS register based on the audio and display sampling frequency |
| 876 | */ |
| 877 | static void snd_intelhad_prog_cts_v2(u32 aud_samp_freq, u32 tmds, u32 n_param, |
| 878 | struct snd_intelhad *intelhaddata) |
| 879 | { |
| 880 | u32 cts_val; |
| 881 | u64 dividend, divisor; |
| 882 | |
| 883 | /* Calculate CTS according to HDMI 1.3a spec*/ |
| 884 | dividend = (u64)tmds * n_param*1000; |
| 885 | divisor = 128 * aud_samp_freq; |
| 886 | cts_val = div64_u64(dividend, divisor); |
| 887 | pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n", |
| 888 | tmds, n_param, cts_val); |
| 889 | had_write_register(AUD_HDMI_CTS, (BIT(24) | cts_val)); |
| 890 | } |
| 891 | |
| 892 | static int had_calculate_n_value(u32 aud_samp_freq) |
| 893 | { |
| 894 | s32 n_val; |
| 895 | |
| 896 | /* Select N according to HDMI 1.3a spec*/ |
| 897 | switch (aud_samp_freq) { |
| 898 | case AUD_SAMPLE_RATE_32: |
| 899 | n_val = 4096; |
| 900 | break; |
| 901 | |
| 902 | case AUD_SAMPLE_RATE_44_1: |
| 903 | n_val = 6272; |
| 904 | break; |
| 905 | |
| 906 | case AUD_SAMPLE_RATE_48: |
| 907 | n_val = 6144; |
| 908 | break; |
| 909 | |
| 910 | case AUD_SAMPLE_RATE_88_2: |
| 911 | n_val = 12544; |
| 912 | break; |
| 913 | |
| 914 | case AUD_SAMPLE_RATE_96: |
| 915 | n_val = 12288; |
| 916 | break; |
| 917 | |
| 918 | case AUD_SAMPLE_RATE_176_4: |
| 919 | n_val = 25088; |
| 920 | break; |
| 921 | |
| 922 | case HAD_MAX_RATE: |
| 923 | n_val = 24576; |
| 924 | break; |
| 925 | |
| 926 | default: |
| 927 | n_val = -EINVAL; |
| 928 | break; |
| 929 | } |
| 930 | return n_val; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * snd_intelhad_prog_n_v1 - Program HDMI audio N value |
| 935 | * |
| 936 | * @aud_samp_freq: sampling frequency of audio data |
| 937 | * @n_param: N value, depends on aud_samp_freq |
| 938 | * @intelhaddata:substream private data |
| 939 | * |
| 940 | * This function is called in the prepare callback. |
| 941 | * It programs based on the audio and display sampling frequency |
| 942 | */ |
| 943 | static int snd_intelhad_prog_n_v1(u32 aud_samp_freq, u32 *n_param, |
| 944 | struct snd_intelhad *intelhaddata) |
| 945 | { |
| 946 | s32 n_val; |
| 947 | |
| 948 | n_val = had_calculate_n_value(aud_samp_freq); |
| 949 | |
| 950 | if (n_val < 0) |
| 951 | return n_val; |
| 952 | |
| 953 | had_write_register(AUD_N_ENABLE, (BIT(20) | n_val)); |
| 954 | *n_param = n_val; |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * snd_intelhad_prog_n_v2 - Program HDMI audio N value |
| 960 | * |
| 961 | * @aud_samp_freq: sampling frequency of audio data |
| 962 | * @n_param: N value, depends on aud_samp_freq |
| 963 | * @intelhaddata:substream private data |
| 964 | * |
| 965 | * This function is called in the prepare callback. |
| 966 | * It programs based on the audio and display sampling frequency |
| 967 | */ |
| 968 | static int snd_intelhad_prog_n_v2(u32 aud_samp_freq, u32 *n_param, |
| 969 | struct snd_intelhad *intelhaddata) |
| 970 | { |
| 971 | s32 n_val; |
| 972 | |
| 973 | n_val = had_calculate_n_value(aud_samp_freq); |
| 974 | |
| 975 | if (n_val < 0) |
| 976 | return n_val; |
| 977 | |
| 978 | had_write_register(AUD_N_ENABLE, (BIT(24) | n_val)); |
| 979 | *n_param = n_val; |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static void had_clear_underrun_intr_v1(struct snd_intelhad *intelhaddata) |
| 984 | { |
| 985 | u32 hdmi_status, i = 0; |
| 986 | |
| 987 | /* Handle Underrun interrupt within Audio Unit */ |
| 988 | had_write_register(AUD_CONFIG, 0); |
| 989 | /* Reset buffer pointers */ |
| 990 | had_write_register(AUD_HDMI_STATUS, 1); |
| 991 | had_write_register(AUD_HDMI_STATUS, 0); |
| 992 | /** |
| 993 | * The interrupt status 'sticky' bits might not be cleared by |
| 994 | * setting '1' to that bit once... |
| 995 | */ |
| 996 | do { /* clear bit30, 31 AUD_HDMI_STATUS */ |
| 997 | had_read_register(AUD_HDMI_STATUS, &hdmi_status); |
| 998 | pr_debug("HDMI status =0x%x\n", hdmi_status); |
| 999 | if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) { |
| 1000 | i++; |
| 1001 | hdmi_status &= (AUD_CONFIG_MASK_SRDBG | |
| 1002 | AUD_CONFIG_MASK_FUNCRST); |
| 1003 | hdmi_status |= ~AUD_CONFIG_MASK_UNDERRUN; |
| 1004 | had_write_register(AUD_HDMI_STATUS, hdmi_status); |
| 1005 | } else |
| 1006 | break; |
| 1007 | } while (i < MAX_CNT); |
| 1008 | if (i >= MAX_CNT) |
| 1009 | pr_err("Unable to clear UNDERRUN bits\n"); |
| 1010 | } |
| 1011 | |
| 1012 | static void had_clear_underrun_intr_v2(struct snd_intelhad *intelhaddata) |
| 1013 | { |
| 1014 | u32 hdmi_status, i = 0; |
| 1015 | |
| 1016 | /* Handle Underrun interrupt within Audio Unit */ |
| 1017 | had_write_register(AUD_CONFIG, 0); |
| 1018 | /* Reset buffer pointers */ |
| 1019 | had_write_register(AUD_HDMI_STATUS_v2, 1); |
| 1020 | had_write_register(AUD_HDMI_STATUS_v2, 0); |
| 1021 | /** |
| 1022 | * The interrupt status 'sticky' bits might not be cleared by |
| 1023 | * setting '1' to that bit once... |
| 1024 | */ |
| 1025 | do { /* clear bit30, 31 AUD_HDMI_STATUS */ |
| 1026 | had_read_register(AUD_HDMI_STATUS_v2, &hdmi_status); |
| 1027 | pr_debug("HDMI status =0x%x\n", hdmi_status); |
| 1028 | if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) { |
| 1029 | i++; |
| 1030 | had_write_register(AUD_HDMI_STATUS_v2, hdmi_status); |
| 1031 | } else |
| 1032 | break; |
| 1033 | } while (i < MAX_CNT); |
| 1034 | if (i >= MAX_CNT) |
| 1035 | pr_err("Unable to clear UNDERRUN bits\n"); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * snd_intelhad_open - stream initializations are done here |
| 1040 | * @substream:substream for which the stream function is called |
| 1041 | * |
| 1042 | * This function is called whenever a PCM stream is opened |
| 1043 | */ |
| 1044 | static int snd_intelhad_open(struct snd_pcm_substream *substream) |
| 1045 | { |
| 1046 | struct snd_intelhad *intelhaddata; |
| 1047 | struct snd_pcm_runtime *runtime; |
| 1048 | struct had_stream_pvt *stream; |
| 1049 | struct had_pvt_data *had_stream; |
| 1050 | int retval; |
| 1051 | |
| 1052 | pr_debug("snd_intelhad_open called\n"); |
| 1053 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1054 | had_stream = intelhaddata->private_data; |
| 1055 | runtime = substream->runtime; |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame^] | 1056 | underrun_count = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1057 | |
| 1058 | pm_runtime_get(intelhaddata->dev); |
| 1059 | |
| 1060 | if (had_get_hwstate(intelhaddata)) { |
| 1061 | pr_err("%s: HDMI cable plugged-out\n", __func__); |
| 1062 | retval = -ENODEV; |
| 1063 | goto exit_put_handle; |
| 1064 | } |
| 1065 | |
| 1066 | /* Check, if device already in use */ |
| 1067 | if (runtime->private_data) { |
| 1068 | pr_err("Device already in use\n"); |
| 1069 | retval = -EBUSY; |
| 1070 | goto exit_put_handle; |
| 1071 | } |
| 1072 | |
| 1073 | /* set the runtime hw parameter with local snd_pcm_hardware struct */ |
| 1074 | runtime->hw = snd_intel_hadstream; |
| 1075 | |
| 1076 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 1077 | if (!stream) { |
| 1078 | retval = -ENOMEM; |
| 1079 | goto exit_put_handle; |
| 1080 | } |
| 1081 | stream->stream_status = STREAM_INIT; |
| 1082 | runtime->private_data = stream; |
| 1083 | |
| 1084 | retval = snd_pcm_hw_constraint_integer(runtime, |
| 1085 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 1086 | if (retval < 0) |
| 1087 | goto exit_err; |
| 1088 | |
| 1089 | /* Make sure, that the period size is always aligned |
| 1090 | * 64byte boundary |
| 1091 | */ |
| 1092 | retval = snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 1093 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64); |
| 1094 | if (retval < 0) { |
| 1095 | pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval); |
| 1096 | goto exit_err; |
| 1097 | } |
| 1098 | |
| 1099 | return retval; |
| 1100 | exit_err: |
| 1101 | kfree(stream); |
| 1102 | exit_put_handle: |
| 1103 | pm_runtime_put(intelhaddata->dev); |
| 1104 | runtime->private_data = NULL; |
| 1105 | return retval; |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * had_period_elapsed - updates the hardware pointer status |
| 1110 | * @had_substream:substream for which the stream function is called |
| 1111 | * |
| 1112 | */ |
| 1113 | static void had_period_elapsed(void *had_substream) |
| 1114 | { |
| 1115 | struct snd_pcm_substream *substream = had_substream; |
| 1116 | struct had_stream_pvt *stream; |
| 1117 | |
| 1118 | /* pr_debug("had_period_elapsed called\n"); */ |
| 1119 | |
| 1120 | if (!substream || !substream->runtime) |
| 1121 | return; |
| 1122 | stream = substream->runtime->private_data; |
| 1123 | if (!stream) |
| 1124 | return; |
| 1125 | |
| 1126 | if (stream->stream_status != STREAM_RUNNING) |
| 1127 | return; |
| 1128 | snd_pcm_period_elapsed(substream); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * snd_intelhad_init_stream - internal function to initialize stream info |
| 1133 | * @substream:substream for which the stream function is called |
| 1134 | * |
| 1135 | */ |
| 1136 | static int snd_intelhad_init_stream(struct snd_pcm_substream *substream) |
| 1137 | { |
| 1138 | struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream); |
| 1139 | |
| 1140 | pr_debug("snd_intelhad_init_stream called\n"); |
| 1141 | |
| 1142 | pr_debug("setting buffer ptr param\n"); |
| 1143 | intelhaddata->stream_info.period_elapsed = had_period_elapsed; |
| 1144 | intelhaddata->stream_info.had_substream = substream; |
| 1145 | intelhaddata->stream_info.buffer_ptr = 0; |
| 1146 | intelhaddata->stream_info.buffer_rendered = 0; |
| 1147 | intelhaddata->stream_info.sfreq = substream->runtime->rate; |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * snd_intelhad_close- to free parameteres when stream is stopped |
| 1153 | * |
| 1154 | * @substream: substream for which the function is called |
| 1155 | * |
| 1156 | * This function is called by ALSA framework when stream is stopped |
| 1157 | */ |
| 1158 | static int snd_intelhad_close(struct snd_pcm_substream *substream) |
| 1159 | { |
| 1160 | struct snd_intelhad *intelhaddata; |
| 1161 | struct snd_pcm_runtime *runtime; |
| 1162 | |
| 1163 | pr_debug("snd_intelhad_close called\n"); |
| 1164 | |
| 1165 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1166 | runtime = substream->runtime; |
| 1167 | |
| 1168 | if (!runtime->private_data) { |
| 1169 | pr_debug("close() might have called after failed open"); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | intelhaddata->stream_info.buffer_rendered = 0; |
| 1174 | intelhaddata->stream_info.buffer_ptr = 0; |
| 1175 | intelhaddata->stream_info.str_id = 0; |
| 1176 | intelhaddata->stream_info.had_substream = NULL; |
| 1177 | |
| 1178 | /* Check if following drv_status modification is required - VA */ |
| 1179 | if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) { |
| 1180 | intelhaddata->drv_status = HAD_DRV_CONNECTED; |
| 1181 | pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n", |
| 1182 | __func__, __LINE__); |
| 1183 | } |
| 1184 | kfree(runtime->private_data); |
| 1185 | runtime->private_data = NULL; |
| 1186 | pm_runtime_put(intelhaddata->dev); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * snd_intelhad_hw_params- to setup the hardware parameters |
| 1192 | * like allocating the buffers |
| 1193 | * |
| 1194 | * @substream: substream for which the function is called |
| 1195 | * @hw_params: hardware parameters |
| 1196 | * |
| 1197 | * This function is called by ALSA framework when hardware params are set |
| 1198 | */ |
| 1199 | static int snd_intelhad_hw_params(struct snd_pcm_substream *substream, |
| 1200 | struct snd_pcm_hw_params *hw_params) |
| 1201 | { |
| 1202 | unsigned long addr; |
| 1203 | int pages, buf_size, retval; |
| 1204 | |
| 1205 | pr_debug("snd_intelhad_hw_params called\n"); |
| 1206 | |
| 1207 | if (!hw_params) |
| 1208 | return -EINVAL; |
| 1209 | |
| 1210 | buf_size = params_buffer_bytes(hw_params); |
| 1211 | retval = snd_pcm_lib_malloc_pages(substream, buf_size); |
| 1212 | if (retval < 0) |
| 1213 | return retval; |
| 1214 | pr_debug("%s:allocated memory = %d\n", __func__, buf_size); |
| 1215 | /* mark the pages as uncached region */ |
| 1216 | addr = (unsigned long) substream->runtime->dma_area; |
| 1217 | pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE; |
| 1218 | retval = set_memory_uc(addr, pages); |
| 1219 | if (retval) { |
| 1220 | pr_err("set_memory_uc failed.Error:%d\n", retval); |
| 1221 | return retval; |
| 1222 | } |
| 1223 | memset(substream->runtime->dma_area, 0, buf_size); |
| 1224 | |
| 1225 | return retval; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * snd_intelhad_hw_free- to release the resources allocated during |
| 1230 | * hardware params setup |
| 1231 | * |
| 1232 | * @substream: substream for which the function is called |
| 1233 | * |
| 1234 | * This function is called by ALSA framework before close callback. |
| 1235 | * |
| 1236 | */ |
| 1237 | static int snd_intelhad_hw_free(struct snd_pcm_substream *substream) |
| 1238 | { |
| 1239 | unsigned long addr; |
| 1240 | u32 pages; |
| 1241 | |
| 1242 | pr_debug("snd_intelhad_hw_free called\n"); |
| 1243 | |
| 1244 | /* mark back the pages as cached/writeback region before the free */ |
| 1245 | if (substream->runtime->dma_area != NULL) { |
| 1246 | addr = (unsigned long) substream->runtime->dma_area; |
| 1247 | pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / |
| 1248 | PAGE_SIZE; |
| 1249 | set_memory_wb(addr, pages); |
| 1250 | return snd_pcm_lib_free_pages(substream); |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * snd_intelhad_pcm_trigger - stream activities are handled here |
| 1257 | * @substream:substream for which the stream function is called |
| 1258 | * @cmd:the stream commamd thats requested from upper layer |
| 1259 | * This function is called whenever an a stream activity is invoked |
| 1260 | */ |
| 1261 | static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream, |
| 1262 | int cmd) |
| 1263 | { |
| 1264 | int caps, retval = 0; |
| 1265 | unsigned long flag_irq; |
| 1266 | struct snd_intelhad *intelhaddata; |
| 1267 | struct had_stream_pvt *stream; |
| 1268 | struct had_pvt_data *had_stream; |
| 1269 | |
| 1270 | pr_debug("snd_intelhad_pcm_trigger called\n"); |
| 1271 | |
| 1272 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1273 | stream = substream->runtime->private_data; |
| 1274 | had_stream = intelhaddata->private_data; |
| 1275 | |
| 1276 | switch (cmd) { |
| 1277 | case SNDRV_PCM_TRIGGER_START: |
| 1278 | pr_debug("Trigger Start\n"); |
| 1279 | |
| 1280 | /* Disable local INTRs till register prgmng is done */ |
| 1281 | if (had_get_hwstate(intelhaddata)) { |
| 1282 | pr_err("_START: HDMI cable plugged-out\n"); |
| 1283 | retval = -ENODEV; |
| 1284 | break; |
| 1285 | } |
| 1286 | stream->stream_status = STREAM_RUNNING; |
| 1287 | |
| 1288 | had_stream->stream_type = HAD_RUNNING_STREAM; |
| 1289 | |
| 1290 | /* Enable Audio */ |
| 1291 | /* |
| 1292 | * ToDo: Need to enable UNDERRUN interrupts as well |
| 1293 | * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1294 | */ |
| 1295 | caps = HDMI_AUDIO_BUFFER_DONE; |
| 1296 | retval = had_set_caps(HAD_SET_ENABLE_AUDIO_INT, &caps); |
| 1297 | retval = had_set_caps(HAD_SET_ENABLE_AUDIO, NULL); |
| 1298 | intelhaddata->ops->enable_audio(substream, 1); |
| 1299 | |
| 1300 | pr_debug("Processed _Start\n"); |
| 1301 | |
| 1302 | break; |
| 1303 | |
| 1304 | case SNDRV_PCM_TRIGGER_STOP: |
| 1305 | pr_debug("Trigger Stop\n"); |
| 1306 | spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq); |
| 1307 | intelhaddata->stream_info.str_id = 0; |
| 1308 | intelhaddata->curr_buf = 0; |
| 1309 | |
| 1310 | /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/ |
| 1311 | |
| 1312 | had_stream->stream_type = HAD_INIT; |
| 1313 | spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq); |
| 1314 | /* Disable Audio */ |
| 1315 | /* |
| 1316 | * ToDo: Need to disable UNDERRUN interrupts as well |
| 1317 | * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1318 | */ |
| 1319 | caps = HDMI_AUDIO_BUFFER_DONE; |
| 1320 | had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps); |
| 1321 | intelhaddata->ops->enable_audio(substream, 0); |
| 1322 | /* Reset buffer pointers */ |
| 1323 | intelhaddata->ops->reset_audio(1); |
| 1324 | intelhaddata->ops->reset_audio(0); |
| 1325 | stream->stream_status = STREAM_DROPPED; |
| 1326 | had_set_caps(HAD_SET_DISABLE_AUDIO, NULL); |
| 1327 | break; |
| 1328 | |
| 1329 | default: |
| 1330 | retval = -EINVAL; |
| 1331 | } |
| 1332 | return retval; |
| 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * snd_intelhad_pcm_prepare- internal preparation before starting a stream |
| 1337 | * |
| 1338 | * @substream: substream for which the function is called |
| 1339 | * |
| 1340 | * This function is called when a stream is started for internal preparation. |
| 1341 | */ |
| 1342 | static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream) |
| 1343 | { |
| 1344 | int retval; |
| 1345 | u32 disp_samp_freq, n_param; |
| 1346 | struct snd_intelhad *intelhaddata; |
| 1347 | struct snd_pcm_runtime *runtime; |
| 1348 | struct had_pvt_data *had_stream; |
| 1349 | |
| 1350 | pr_debug("snd_intelhad_pcm_prepare called\n"); |
| 1351 | |
| 1352 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1353 | runtime = substream->runtime; |
| 1354 | had_stream = intelhaddata->private_data; |
| 1355 | |
| 1356 | if (had_get_hwstate(intelhaddata)) { |
| 1357 | pr_err("%s: HDMI cable plugged-out\n", __func__); |
| 1358 | retval = -ENODEV; |
| 1359 | goto prep_end; |
| 1360 | } |
| 1361 | |
| 1362 | pr_debug("period_size=%d\n", |
| 1363 | (int)frames_to_bytes(runtime, runtime->period_size)); |
| 1364 | pr_debug("periods=%d\n", runtime->periods); |
| 1365 | pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream)); |
| 1366 | pr_debug("rate=%d\n", runtime->rate); |
| 1367 | pr_debug("channels=%d\n", runtime->channels); |
| 1368 | |
| 1369 | if (intelhaddata->stream_info.str_id) { |
| 1370 | pr_debug("_prepare is called for existing str_id#%d\n", |
| 1371 | intelhaddata->stream_info.str_id); |
| 1372 | retval = snd_intelhad_pcm_trigger(substream, |
| 1373 | SNDRV_PCM_TRIGGER_STOP); |
| 1374 | return retval; |
| 1375 | } |
| 1376 | |
| 1377 | retval = snd_intelhad_init_stream(substream); |
| 1378 | if (retval) |
| 1379 | goto prep_end; |
| 1380 | |
| 1381 | |
| 1382 | /* Get N value in KHz */ |
| 1383 | retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq); |
| 1384 | if (retval) { |
| 1385 | pr_err("querying display sampling freq failed %#x\n", retval); |
| 1386 | goto prep_end; |
| 1387 | } |
| 1388 | |
| 1389 | had_get_caps(HAD_GET_ELD, &intelhaddata->eeld); |
| 1390 | |
| 1391 | retval = intelhaddata->ops->prog_n(substream->runtime->rate, &n_param, |
| 1392 | intelhaddata); |
| 1393 | if (retval) { |
| 1394 | pr_err("programming N value failed %#x\n", retval); |
| 1395 | goto prep_end; |
| 1396 | } |
| 1397 | intelhaddata->ops->prog_cts(substream->runtime->rate, |
| 1398 | disp_samp_freq, n_param, intelhaddata); |
| 1399 | |
| 1400 | intelhaddata->ops->prog_dip(substream, intelhaddata); |
| 1401 | |
| 1402 | retval = intelhaddata->ops->audio_ctrl(substream, intelhaddata); |
| 1403 | |
| 1404 | /* Prog buffer address */ |
| 1405 | retval = snd_intelhad_prog_buffer(intelhaddata, |
| 1406 | HAD_BUF_TYPE_A, HAD_BUF_TYPE_D); |
| 1407 | |
| 1408 | /* |
| 1409 | * Program channel mapping in following order: |
| 1410 | * FL, FR, C, LFE, RL, RR |
| 1411 | */ |
| 1412 | |
| 1413 | had_write_register(AUD_BUF_CH_SWAP, SWAP_LFE_CENTER); |
| 1414 | |
| 1415 | prep_end: |
| 1416 | return retval; |
| 1417 | } |
| 1418 | |
| 1419 | /** |
| 1420 | * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw |
| 1421 | * |
| 1422 | * @substream: substream for which the function is called |
| 1423 | * |
| 1424 | * This function is called by ALSA framework to get the current hw buffer ptr |
| 1425 | * when a period is elapsed |
| 1426 | */ |
| 1427 | static snd_pcm_uframes_t snd_intelhad_pcm_pointer( |
| 1428 | struct snd_pcm_substream *substream) |
| 1429 | { |
| 1430 | struct snd_intelhad *intelhaddata; |
| 1431 | u32 bytes_rendered = 0; |
| 1432 | u32 t; |
| 1433 | int buf_id; |
| 1434 | |
| 1435 | /* pr_debug("snd_intelhad_pcm_pointer called\n"); */ |
| 1436 | |
| 1437 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1438 | |
| 1439 | if (intelhaddata->flag_underrun) { |
| 1440 | intelhaddata->flag_underrun = 0; |
| 1441 | return SNDRV_PCM_POS_XRUN; |
| 1442 | } |
| 1443 | |
| 1444 | /* Use a hw register to calculate sub-period position reports. |
| 1445 | * This makes PulseAudio happier. |
| 1446 | */ |
| 1447 | |
| 1448 | buf_id = intelhaddata->curr_buf % 4; |
| 1449 | 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^] | 1450 | |
| 1451 | if ((t == 0) || (t == ((u32)-1L))) { |
| 1452 | underrun_count++; |
| 1453 | pr_debug("discovered buffer done for buf %d, count = %d\n", |
| 1454 | buf_id, underrun_count); |
| 1455 | |
| 1456 | if (underrun_count > (HAD_MIN_PERIODS/2)) { |
| 1457 | pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n", |
| 1458 | underrun_count); |
| 1459 | underrun_count = 0; |
| 1460 | return SNDRV_PCM_POS_XRUN; |
| 1461 | } |
| 1462 | } else { |
| 1463 | /* Reset Counter */ |
| 1464 | underrun_count = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1465 | } |
Jerome Anand | 232892f | 2017-01-25 04:27:53 +0530 | [diff] [blame^] | 1466 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1467 | t = intelhaddata->buf_info[buf_id].buf_size - t; |
| 1468 | |
| 1469 | if (intelhaddata->stream_info.buffer_rendered) |
| 1470 | div_u64_rem(intelhaddata->stream_info.buffer_rendered, |
| 1471 | intelhaddata->stream_info.ring_buf_size, |
| 1472 | &(bytes_rendered)); |
| 1473 | |
| 1474 | intelhaddata->stream_info.buffer_ptr = bytes_to_frames( |
| 1475 | substream->runtime, |
| 1476 | bytes_rendered + t); |
| 1477 | return intelhaddata->stream_info.buffer_ptr; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data |
| 1482 | * |
| 1483 | * @substream: substream for which the function is called |
| 1484 | * @vma: struct instance of memory VMM memory area |
| 1485 | * |
| 1486 | * This function is called by OS when a user space component |
| 1487 | * tries to get mmap memory from driver |
| 1488 | */ |
| 1489 | static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream, |
| 1490 | struct vm_area_struct *vma) |
| 1491 | { |
| 1492 | |
| 1493 | pr_debug("snd_intelhad_pcm_mmap called\n"); |
| 1494 | |
| 1495 | pr_debug("entry with prot:%s\n", __func__); |
| 1496 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 1497 | return remap_pfn_range(vma, vma->vm_start, |
| 1498 | substream->dma_buffer.addr >> PAGE_SHIFT, |
| 1499 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 1500 | } |
| 1501 | |
| 1502 | int hdmi_audio_mode_change(struct snd_pcm_substream *substream) |
| 1503 | { |
| 1504 | int retval = 0; |
| 1505 | u32 disp_samp_freq, n_param; |
| 1506 | struct snd_intelhad *intelhaddata; |
| 1507 | |
| 1508 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1509 | |
| 1510 | /* Disable Audio */ |
| 1511 | intelhaddata->ops->enable_audio(substream, 0); |
| 1512 | |
| 1513 | /* Update CTS value */ |
| 1514 | retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq); |
| 1515 | if (retval) { |
| 1516 | pr_err("querying display sampling freq failed %#x\n", retval); |
| 1517 | goto out; |
| 1518 | } |
| 1519 | |
| 1520 | retval = intelhaddata->ops->prog_n(substream->runtime->rate, &n_param, |
| 1521 | intelhaddata); |
| 1522 | if (retval) { |
| 1523 | pr_err("programming N value failed %#x\n", retval); |
| 1524 | goto out; |
| 1525 | } |
| 1526 | intelhaddata->ops->prog_cts(substream->runtime->rate, |
| 1527 | disp_samp_freq, n_param, intelhaddata); |
| 1528 | |
| 1529 | /* Enable Audio */ |
| 1530 | intelhaddata->ops->enable_audio(substream, 1); |
| 1531 | |
| 1532 | out: |
| 1533 | return retval; |
| 1534 | } |
| 1535 | |
| 1536 | /*PCM operations structure and the calls back for the same */ |
| 1537 | struct snd_pcm_ops snd_intelhad_playback_ops = { |
| 1538 | .open = snd_intelhad_open, |
| 1539 | .close = snd_intelhad_close, |
| 1540 | .ioctl = snd_pcm_lib_ioctl, |
| 1541 | .hw_params = snd_intelhad_hw_params, |
| 1542 | .hw_free = snd_intelhad_hw_free, |
| 1543 | .prepare = snd_intelhad_pcm_prepare, |
| 1544 | .trigger = snd_intelhad_pcm_trigger, |
| 1545 | .pointer = snd_intelhad_pcm_pointer, |
| 1546 | .mmap = snd_intelhad_pcm_mmap, |
| 1547 | }; |
| 1548 | |
| 1549 | /** |
| 1550 | * snd_intelhad_create - to crete alsa card instance |
| 1551 | * |
| 1552 | * @intelhaddata: pointer to internal context |
| 1553 | * @card: pointer to card |
| 1554 | * |
| 1555 | * This function is called when the hdmi cable is plugged in |
| 1556 | */ |
| 1557 | static int snd_intelhad_create( |
| 1558 | struct snd_intelhad *intelhaddata, |
| 1559 | struct snd_card *card) |
| 1560 | { |
| 1561 | int retval; |
| 1562 | static struct snd_device_ops ops = { |
| 1563 | }; |
| 1564 | |
| 1565 | pr_debug("snd_intelhad_create called\n"); |
| 1566 | |
| 1567 | if (!intelhaddata) |
| 1568 | return -EINVAL; |
| 1569 | |
| 1570 | /* ALSA api to register the device */ |
| 1571 | retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops); |
| 1572 | return retval; |
| 1573 | } |
| 1574 | /** |
| 1575 | * snd_intelhad_pcm_free - to free the memory allocated |
| 1576 | * |
| 1577 | * @pcm: pointer to pcm instance |
| 1578 | * This function is called when the device is removed |
| 1579 | */ |
| 1580 | static void snd_intelhad_pcm_free(struct snd_pcm *pcm) |
| 1581 | { |
| 1582 | pr_debug("Freeing PCM preallocated pages\n"); |
| 1583 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 1584 | } |
| 1585 | |
| 1586 | static int had_iec958_info(struct snd_kcontrol *kcontrol, |
| 1587 | struct snd_ctl_elem_info *uinfo) |
| 1588 | { |
| 1589 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 1590 | uinfo->count = 1; |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | static int had_iec958_get(struct snd_kcontrol *kcontrol, |
| 1595 | struct snd_ctl_elem_value *ucontrol) |
| 1596 | { |
| 1597 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1598 | |
| 1599 | ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff; |
| 1600 | ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff; |
| 1601 | ucontrol->value.iec958.status[2] = |
| 1602 | (intelhaddata->aes_bits >> 16) & 0xff; |
| 1603 | ucontrol->value.iec958.status[3] = |
| 1604 | (intelhaddata->aes_bits >> 24) & 0xff; |
| 1605 | return 0; |
| 1606 | } |
| 1607 | static int had_iec958_mask_get(struct snd_kcontrol *kcontrol, |
| 1608 | struct snd_ctl_elem_value *ucontrol) |
| 1609 | { |
| 1610 | ucontrol->value.iec958.status[0] = 0xff; |
| 1611 | ucontrol->value.iec958.status[1] = 0xff; |
| 1612 | ucontrol->value.iec958.status[2] = 0xff; |
| 1613 | ucontrol->value.iec958.status[3] = 0xff; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | static int had_iec958_put(struct snd_kcontrol *kcontrol, |
| 1617 | struct snd_ctl_elem_value *ucontrol) |
| 1618 | { |
| 1619 | unsigned int val; |
| 1620 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1621 | |
| 1622 | pr_debug("entered had_iec958_put\n"); |
| 1623 | val = (ucontrol->value.iec958.status[0] << 0) | |
| 1624 | (ucontrol->value.iec958.status[1] << 8) | |
| 1625 | (ucontrol->value.iec958.status[2] << 16) | |
| 1626 | (ucontrol->value.iec958.status[3] << 24); |
| 1627 | if (intelhaddata->aes_bits != val) { |
| 1628 | intelhaddata->aes_bits = val; |
| 1629 | return 1; |
| 1630 | } |
| 1631 | return 1; |
| 1632 | } |
| 1633 | |
| 1634 | static struct snd_kcontrol_new had_control_iec958_mask = { |
| 1635 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1636 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1637 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK), |
| 1638 | .info = had_iec958_info, /* shared */ |
| 1639 | .get = had_iec958_mask_get, |
| 1640 | }; |
| 1641 | |
| 1642 | static struct snd_kcontrol_new had_control_iec958 = { |
| 1643 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1644 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 1645 | .info = had_iec958_info, |
| 1646 | .get = had_iec958_get, |
| 1647 | .put = had_iec958_put |
| 1648 | }; |
| 1649 | |
| 1650 | static struct snd_intel_had_interface had_interface = { |
| 1651 | .name = "hdmi-audio", |
| 1652 | .query = hdmi_audio_query, |
| 1653 | .suspend = hdmi_audio_suspend, |
| 1654 | .resume = hdmi_audio_resume, |
| 1655 | }; |
| 1656 | |
| 1657 | static struct had_ops had_ops_v1 = { |
| 1658 | .enable_audio = snd_intelhad_enable_audio_v1, |
| 1659 | .reset_audio = snd_intelhad_reset_audio_v1, |
| 1660 | .prog_n = snd_intelhad_prog_n_v1, |
| 1661 | .prog_cts = snd_intelhad_prog_cts_v1, |
| 1662 | .audio_ctrl = snd_intelhad_prog_audio_ctrl_v1, |
| 1663 | .prog_dip = snd_intelhad_prog_dip_v1, |
| 1664 | .handle_underrun = had_clear_underrun_intr_v1, |
| 1665 | }; |
| 1666 | |
| 1667 | static struct had_ops had_ops_v2 = { |
| 1668 | .enable_audio = snd_intelhad_enable_audio_v2, |
| 1669 | .reset_audio = snd_intelhad_reset_audio_v2, |
| 1670 | .prog_n = snd_intelhad_prog_n_v2, |
| 1671 | .prog_cts = snd_intelhad_prog_cts_v2, |
| 1672 | .audio_ctrl = snd_intelhad_prog_audio_ctrl_v2, |
| 1673 | .prog_dip = snd_intelhad_prog_dip_v2, |
| 1674 | .handle_underrun = had_clear_underrun_intr_v2, |
| 1675 | }; |
| 1676 | /** |
| 1677 | * hdmi_audio_probe - to create sound card instance for HDMI audio playabck |
| 1678 | * |
| 1679 | *@haddata: pointer to HAD private data |
| 1680 | *@card_id: card for which probe is called |
| 1681 | * |
| 1682 | * This function is called when the hdmi cable is plugged in. This function |
| 1683 | * creates and registers the sound card with ALSA |
| 1684 | */ |
| 1685 | int hdmi_audio_probe(void *deviceptr) |
| 1686 | { |
| 1687 | int retval; |
| 1688 | struct snd_pcm *pcm; |
| 1689 | struct snd_card *card; |
| 1690 | struct had_callback_ops ops_cb; |
| 1691 | struct snd_intelhad *intelhaddata; |
| 1692 | struct had_pvt_data *had_stream; |
| 1693 | struct platform_device *devptr = deviceptr; |
| 1694 | |
| 1695 | pr_debug("Enter %s\n", __func__); |
| 1696 | |
| 1697 | pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask); |
| 1698 | |
| 1699 | /* allocate memory for saving internal context and working */ |
| 1700 | intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL); |
| 1701 | if (!intelhaddata) |
| 1702 | return -ENOMEM; |
| 1703 | |
| 1704 | had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL); |
| 1705 | if (!had_stream) { |
| 1706 | retval = -ENOMEM; |
| 1707 | goto free_haddata; |
| 1708 | } |
| 1709 | |
| 1710 | had_data = intelhaddata; |
| 1711 | ops_cb.intel_had_event_call_back = had_event_handler; |
| 1712 | |
| 1713 | /* registering with display driver to get access to display APIs */ |
| 1714 | |
| 1715 | retval = mid_hdmi_audio_setup( |
| 1716 | ops_cb.intel_had_event_call_back, |
| 1717 | &(intelhaddata->reg_ops), |
| 1718 | &(intelhaddata->query_ops)); |
| 1719 | if (retval) { |
| 1720 | pr_err("querying display driver APIs failed %#x\n", retval); |
| 1721 | goto free_hadstream; |
| 1722 | } |
| 1723 | mutex_lock(&had_mutex); |
| 1724 | spin_lock_init(&intelhaddata->had_spinlock); |
| 1725 | intelhaddata->drv_status = HAD_DRV_DISCONNECTED; |
| 1726 | pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n", |
| 1727 | __func__, __LINE__); |
| 1728 | |
| 1729 | /* create a card instance with ALSA framework */ |
| 1730 | retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id, |
| 1731 | THIS_MODULE, 0, &card); |
| 1732 | |
| 1733 | if (retval) |
| 1734 | goto unlock_mutex; |
| 1735 | intelhaddata->card = card; |
| 1736 | intelhaddata->card_id = hdmi_card_id; |
| 1737 | intelhaddata->card_index = card->number; |
| 1738 | intelhaddata->private_data = had_stream; |
| 1739 | intelhaddata->flag_underrun = 0; |
| 1740 | intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF; |
| 1741 | strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD)); |
| 1742 | strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD)); |
| 1743 | |
| 1744 | retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS, |
| 1745 | MAX_CAP_STREAMS, &pcm); |
| 1746 | if (retval) |
| 1747 | goto err; |
| 1748 | |
| 1749 | /* setup private data which can be retrieved when required */ |
| 1750 | pcm->private_data = intelhaddata; |
| 1751 | pcm->private_free = snd_intelhad_pcm_free; |
| 1752 | pcm->info_flags = 0; |
| 1753 | strncpy(pcm->name, card->shortname, strlen(card->shortname)); |
| 1754 | /* setup the ops for palyabck */ |
| 1755 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 1756 | &snd_intelhad_playback_ops); |
| 1757 | /* allocate dma pages for ALSA stream operations |
| 1758 | * memory allocated is based on size, not max value |
| 1759 | * thus using same argument for max & size |
| 1760 | */ |
| 1761 | retval = snd_pcm_lib_preallocate_pages_for_all(pcm, |
| 1762 | SNDRV_DMA_TYPE_DEV, NULL, |
| 1763 | HAD_MAX_BUFFER, HAD_MAX_BUFFER); |
| 1764 | |
| 1765 | if (card->dev == NULL) |
| 1766 | pr_debug("card->dev is NULL!!!!! Should not be this case\n"); |
| 1767 | else if (card->dev->dma_mask == NULL) |
| 1768 | pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n"); |
| 1769 | else |
| 1770 | pr_debug("hdmi_audio_probe dma_mask is : %p\n", |
| 1771 | card->dev->dma_mask); |
| 1772 | |
| 1773 | if (retval) |
| 1774 | goto err; |
| 1775 | |
| 1776 | /* internal function call to register device with ALSA */ |
| 1777 | retval = snd_intelhad_create(intelhaddata, card); |
| 1778 | if (retval) |
| 1779 | goto err; |
| 1780 | |
| 1781 | card->private_data = &intelhaddata; |
| 1782 | retval = snd_card_register(card); |
| 1783 | if (retval) |
| 1784 | goto err; |
| 1785 | |
| 1786 | /* IEC958 controls */ |
| 1787 | retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask, |
| 1788 | intelhaddata)); |
| 1789 | if (retval < 0) |
| 1790 | goto err; |
| 1791 | retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958, |
| 1792 | intelhaddata)); |
| 1793 | if (retval < 0) |
| 1794 | goto err; |
| 1795 | |
| 1796 | init_channel_allocations(); |
| 1797 | |
| 1798 | /* Register channel map controls */ |
| 1799 | retval = had_register_chmap_ctls(intelhaddata, pcm); |
| 1800 | if (retval < 0) |
| 1801 | goto err; |
| 1802 | |
| 1803 | intelhaddata->dev = &devptr->dev; |
| 1804 | pm_runtime_set_active(intelhaddata->dev); |
| 1805 | pm_runtime_enable(intelhaddata->dev); |
| 1806 | |
| 1807 | mutex_unlock(&had_mutex); |
| 1808 | retval = mid_hdmi_audio_register(&had_interface, intelhaddata); |
| 1809 | if (retval) { |
| 1810 | pr_err("registering with display driver failed %#x\n", retval); |
| 1811 | snd_card_free(card); |
| 1812 | goto free_hadstream; |
| 1813 | } |
| 1814 | |
| 1815 | intelhaddata->hw_silence = 1; |
| 1816 | had_ops_v1 = had_ops_v1; /* unused */ |
| 1817 | intelhaddata->ops = &had_ops_v2; |
| 1818 | |
| 1819 | return retval; |
| 1820 | err: |
| 1821 | snd_card_free(card); |
| 1822 | unlock_mutex: |
| 1823 | mutex_unlock(&had_mutex); |
| 1824 | free_hadstream: |
| 1825 | kfree(had_stream); |
| 1826 | pm_runtime_disable(intelhaddata->dev); |
| 1827 | intelhaddata->dev = NULL; |
| 1828 | free_haddata: |
| 1829 | kfree(intelhaddata); |
| 1830 | intelhaddata = NULL; |
| 1831 | pr_err("Error returned from %s api %#x\n", __func__, retval); |
| 1832 | return retval; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * hdmi_audio_remove - removes the alsa card |
| 1837 | * |
| 1838 | *@haddata: pointer to HAD private data |
| 1839 | * |
| 1840 | * This function is called when the hdmi cable is un-plugged. This function |
| 1841 | * free the sound card. |
| 1842 | */ |
| 1843 | int hdmi_audio_remove(void *pdevptr) |
| 1844 | { |
| 1845 | struct snd_intelhad *intelhaddata = had_data; |
| 1846 | int caps; |
| 1847 | |
| 1848 | pr_debug("Enter %s\n", __func__); |
| 1849 | |
| 1850 | if (!intelhaddata) |
| 1851 | return 0; |
| 1852 | |
| 1853 | if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) { |
| 1854 | caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE; |
| 1855 | had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps); |
| 1856 | had_set_caps(HAD_SET_DISABLE_AUDIO, NULL); |
| 1857 | } |
| 1858 | snd_card_free(intelhaddata->card); |
| 1859 | kfree(intelhaddata->private_data); |
| 1860 | kfree(intelhaddata); |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>"); |
| 1865 | MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>"); |
| 1866 | MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>"); |
| 1867 | MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>"); |
| 1868 | MODULE_DESCRIPTION("Intel HDMI Audio driver"); |
| 1869 | MODULE_LICENSE("GPL v2"); |
| 1870 | MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}"); |