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