Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 2 | /* |
| 3 | * intel_hdmi_audio.c - Intel HDMI audio driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Intel Corp |
| 6 | * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com> |
| 7 | * Ramesh Babu K V <ramesh.babu@intel.com> |
| 8 | * Vaibhav Agarwal <vaibhav.agarwal@intel.com> |
| 9 | * Jerome Anand <jerome.anand@intel.com> |
| 10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 | * |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 12 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 13 | * ALSA driver for Intel HDMI audio |
| 14 | */ |
| 15 | |
Takashi Iwai | 03c3437 | 2017-02-02 16:19:03 +0100 | [diff] [blame] | 16 | #include <linux/types.h> |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/module.h> |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Takashi Iwai | 03c3437 | 2017-02-02 16:19:03 +0100 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Takashi Iwai | 412bbe7d5 | 2017-02-02 22:03:22 +0100 | [diff] [blame] | 23 | #include <linux/dma-mapping.h> |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 24 | #include <linux/delay.h> |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 25 | #include <sound/core.h> |
Takashi Iwai | 03c3437 | 2017-02-02 16:19:03 +0100 | [diff] [blame] | 26 | #include <sound/asoundef.h> |
| 27 | #include <sound/pcm.h> |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 28 | #include <sound/pcm_params.h> |
| 29 | #include <sound/initval.h> |
| 30 | #include <sound/control.h> |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 31 | #include <sound/jack.h> |
Takashi Iwai | 03c3437 | 2017-02-02 16:19:03 +0100 | [diff] [blame] | 32 | #include <drm/drm_edid.h> |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 33 | #include <drm/intel_lpe_audio.h> |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 34 | #include "intel_hdmi_audio.h" |
| 35 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 36 | #define for_each_pipe(card_ctx, pipe) \ |
| 37 | for ((pipe) = 0; (pipe) < (card_ctx)->num_pipes; (pipe)++) |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 38 | #define for_each_port(card_ctx, port) \ |
| 39 | for ((port) = 0; (port) < (card_ctx)->num_ports; (port)++) |
| 40 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 41 | /*standard module options for ALSA. This module supports only one card*/ |
| 42 | static int hdmi_card_index = SNDRV_DEFAULT_IDX1; |
| 43 | static char *hdmi_card_id = SNDRV_DEFAULT_STR1; |
Takashi Iwai | 7229b12 | 2018-02-20 17:33:45 +0100 | [diff] [blame] | 44 | static bool single_port; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 45 | |
| 46 | module_param_named(index, hdmi_card_index, int, 0444); |
| 47 | MODULE_PARM_DESC(index, |
| 48 | "Index value for INTEL Intel HDMI Audio controller."); |
| 49 | module_param_named(id, hdmi_card_id, charp, 0444); |
| 50 | MODULE_PARM_DESC(id, |
| 51 | "ID string for INTEL Intel HDMI Audio controller."); |
Takashi Iwai | 7229b12 | 2018-02-20 17:33:45 +0100 | [diff] [blame] | 52 | module_param(single_port, bool, 0444); |
| 53 | MODULE_PARM_DESC(single_port, |
| 54 | "Single-port mode (for compatibility)"); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * ELD SA bits in the CEA Speaker Allocation data block |
| 58 | */ |
Takashi Iwai | 4a5ddb2 | 2017-02-01 16:45:38 +0100 | [diff] [blame] | 59 | static const int eld_speaker_allocation_bits[] = { |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 60 | [0] = FL | FR, |
| 61 | [1] = LFE, |
| 62 | [2] = FC, |
| 63 | [3] = RL | RR, |
| 64 | [4] = RC, |
| 65 | [5] = FLC | FRC, |
| 66 | [6] = RLC | RRC, |
| 67 | /* the following are not defined in ELD yet */ |
| 68 | [7] = 0, |
| 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * This is an ordered list! |
| 73 | * |
| 74 | * The preceding ones have better chances to be selected by |
| 75 | * hdmi_channel_allocation(). |
| 76 | */ |
| 77 | static struct cea_channel_speaker_allocation channel_allocations[] = { |
| 78 | /* channel: 7 6 5 4 3 2 1 0 */ |
| 79 | { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } }, |
| 80 | /* 2.1 */ |
| 81 | { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } }, |
| 82 | /* Dolby Surround */ |
| 83 | { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } }, |
| 84 | /* surround40 */ |
| 85 | { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } }, |
| 86 | /* surround41 */ |
| 87 | { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } }, |
| 88 | /* surround50 */ |
| 89 | { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } }, |
| 90 | /* surround51 */ |
| 91 | { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } }, |
| 92 | /* 6.1 */ |
| 93 | { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } }, |
| 94 | /* surround71 */ |
| 95 | { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } }, |
| 96 | |
| 97 | { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } }, |
| 98 | { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } }, |
| 99 | { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } }, |
| 100 | { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } }, |
| 101 | { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } }, |
| 102 | { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } }, |
| 103 | { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } }, |
| 104 | { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } }, |
| 105 | { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } }, |
| 106 | { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } }, |
| 107 | { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } }, |
| 108 | { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } }, |
| 109 | { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } }, |
| 110 | { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } }, |
| 111 | { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } }, |
| 112 | { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } }, |
| 113 | { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } }, |
| 114 | { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } }, |
| 115 | { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } }, |
| 116 | { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } }, |
| 117 | { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } }, |
| 118 | { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } }, |
| 119 | { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } }, |
| 120 | }; |
| 121 | |
Takashi Iwai | 4a5ddb2 | 2017-02-01 16:45:38 +0100 | [diff] [blame] | 122 | static const struct channel_map_table map_tables[] = { |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 123 | { SNDRV_CHMAP_FL, 0x00, FL }, |
| 124 | { SNDRV_CHMAP_FR, 0x01, FR }, |
| 125 | { SNDRV_CHMAP_RL, 0x04, RL }, |
| 126 | { SNDRV_CHMAP_RR, 0x05, RR }, |
| 127 | { SNDRV_CHMAP_LFE, 0x02, LFE }, |
| 128 | { SNDRV_CHMAP_FC, 0x03, FC }, |
| 129 | { SNDRV_CHMAP_RLC, 0x06, RLC }, |
| 130 | { SNDRV_CHMAP_RRC, 0x07, RRC }, |
| 131 | {} /* terminator */ |
| 132 | }; |
| 133 | |
| 134 | /* hardware capability structure */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 135 | static const struct snd_pcm_hardware had_pcm_hardware = { |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 136 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
Takashi Iwai | a9ebdd0 | 2017-02-02 21:33:54 +0100 | [diff] [blame] | 137 | SNDRV_PCM_INFO_MMAP | |
Takashi Iwai | e8de985 | 2017-02-07 08:09:12 +0100 | [diff] [blame] | 138 | SNDRV_PCM_INFO_MMAP_VALID | |
| 139 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP), |
Takashi Iwai | 3fe2cf7 | 2017-02-07 13:53:42 +0100 | [diff] [blame] | 140 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | |
| 141 | SNDRV_PCM_FMTBIT_S24_LE | |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 142 | SNDRV_PCM_FMTBIT_S32_LE), |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 143 | .rates = SNDRV_PCM_RATE_32000 | |
| 144 | SNDRV_PCM_RATE_44100 | |
| 145 | SNDRV_PCM_RATE_48000 | |
| 146 | SNDRV_PCM_RATE_88200 | |
| 147 | SNDRV_PCM_RATE_96000 | |
| 148 | SNDRV_PCM_RATE_176400 | |
| 149 | SNDRV_PCM_RATE_192000, |
| 150 | .rate_min = HAD_MIN_RATE, |
| 151 | .rate_max = HAD_MAX_RATE, |
| 152 | .channels_min = HAD_MIN_CHANNEL, |
| 153 | .channels_max = HAD_MAX_CHANNEL, |
| 154 | .buffer_bytes_max = HAD_MAX_BUFFER, |
| 155 | .period_bytes_min = HAD_MIN_PERIOD_BYTES, |
| 156 | .period_bytes_max = HAD_MAX_PERIOD_BYTES, |
| 157 | .periods_min = HAD_MIN_PERIODS, |
| 158 | .periods_max = HAD_MAX_PERIODS, |
| 159 | .fifo_size = HAD_FIFO_SIZE, |
| 160 | }; |
| 161 | |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 162 | /* Get the active PCM substream; |
| 163 | * Call had_substream_put() for unreferecing. |
| 164 | * Don't call this inside had_spinlock, as it takes by itself |
| 165 | */ |
| 166 | static struct snd_pcm_substream * |
| 167 | had_substream_get(struct snd_intelhad *intelhaddata) |
| 168 | { |
| 169 | struct snd_pcm_substream *substream; |
| 170 | unsigned long flags; |
| 171 | |
| 172 | spin_lock_irqsave(&intelhaddata->had_spinlock, flags); |
| 173 | substream = intelhaddata->stream_info.substream; |
| 174 | if (substream) |
| 175 | intelhaddata->stream_info.substream_refcount++; |
| 176 | spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags); |
| 177 | return substream; |
| 178 | } |
| 179 | |
| 180 | /* Unref the active PCM substream; |
| 181 | * Don't call this inside had_spinlock, as it takes by itself |
| 182 | */ |
| 183 | static void had_substream_put(struct snd_intelhad *intelhaddata) |
| 184 | { |
| 185 | unsigned long flags; |
| 186 | |
| 187 | spin_lock_irqsave(&intelhaddata->had_spinlock, flags); |
| 188 | intelhaddata->stream_info.substream_refcount--; |
| 189 | spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags); |
| 190 | } |
| 191 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 192 | static u32 had_config_offset(int pipe) |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 193 | { |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 194 | switch (pipe) { |
| 195 | default: |
| 196 | case 0: |
| 197 | return AUDIO_HDMI_CONFIG_A; |
| 198 | case 1: |
| 199 | return AUDIO_HDMI_CONFIG_B; |
| 200 | case 2: |
| 201 | return AUDIO_HDMI_CONFIG_C; |
| 202 | } |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 205 | /* Register access functions */ |
| 206 | static u32 had_read_register_raw(struct snd_intelhad_card *card_ctx, |
| 207 | int pipe, u32 reg) |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 208 | { |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 209 | return ioread32(card_ctx->mmio_start + had_config_offset(pipe) + reg); |
| 210 | } |
| 211 | |
| 212 | static void had_write_register_raw(struct snd_intelhad_card *card_ctx, |
| 213 | int pipe, u32 reg, u32 val) |
| 214 | { |
| 215 | iowrite32(val, card_ctx->mmio_start + had_config_offset(pipe) + reg); |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Takashi Iwai | 83af57d | 2017-02-03 08:50:06 +0100 | [diff] [blame] | 218 | static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 219 | { |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 220 | if (!ctx->connected) |
| 221 | *val = 0; |
| 222 | else |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 223 | *val = had_read_register_raw(ctx->card_ctx, ctx->pipe, reg); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 224 | } |
| 225 | |
Takashi Iwai | 83af57d | 2017-02-03 08:50:06 +0100 | [diff] [blame] | 226 | static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 227 | { |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 228 | if (ctx->connected) |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 229 | had_write_register_raw(ctx->card_ctx, ctx->pipe, reg, val); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 230 | } |
| 231 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 232 | /* |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 233 | * enable / disable audio configuration |
| 234 | * |
Takashi Iwai | 83af57d | 2017-02-03 08:50:06 +0100 | [diff] [blame] | 235 | * The normal read/modify should not directly be used on VLV2 for |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 236 | * updating AUD_CONFIG register. |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 237 | * This is because: |
| 238 | * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2 |
| 239 | * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always |
| 240 | * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the |
| 241 | * register. This field should be 1xy binary for configuration with 6 or |
| 242 | * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio) |
| 243 | * causes the "channels" field to be updated as 0xy binary resulting in |
| 244 | * bad audio. The fix is to always write the AUD_CONFIG[6:4] with |
| 245 | * appropriate value when doing read-modify of AUD_CONFIG register. |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 246 | */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 247 | static void had_enable_audio(struct snd_intelhad *intelhaddata, |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 248 | bool enable) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 249 | { |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 250 | /* update the cached value */ |
| 251 | intelhaddata->aud_config.regx.aud_en = enable; |
| 252 | had_write_register(intelhaddata, AUD_CONFIG, |
| 253 | intelhaddata->aud_config.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Takashi Iwai | 075a1d4 | 2017-02-07 07:55:27 +0100 | [diff] [blame] | 256 | /* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */ |
| 257 | static void had_ack_irqs(struct snd_intelhad *ctx) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 258 | { |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 259 | u32 status_reg; |
| 260 | |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 261 | if (!ctx->connected) |
| 262 | return; |
Takashi Iwai | 075a1d4 | 2017-02-07 07:55:27 +0100 | [diff] [blame] | 263 | had_read_register(ctx, AUD_HDMI_STATUS, &status_reg); |
| 264 | status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN; |
| 265 | had_write_register(ctx, AUD_HDMI_STATUS, status_reg); |
| 266 | had_read_register(ctx, AUD_HDMI_STATUS, &status_reg); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Takashi Iwai | f4566aa | 2017-02-04 21:39:56 +0100 | [diff] [blame] | 269 | /* Reset buffer pointers */ |
| 270 | static void had_reset_audio(struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 271 | { |
Takashi Iwai | 77531be | 2017-02-07 12:17:23 +0100 | [diff] [blame] | 272 | had_write_register(intelhaddata, AUD_HDMI_STATUS, |
| 273 | AUD_HDMI_STATUSG_MASK_FUNCRST); |
Takashi Iwai | f4566aa | 2017-02-04 21:39:56 +0100 | [diff] [blame] | 274 | had_write_register(intelhaddata, AUD_HDMI_STATUS, 0); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 275 | } |
| 276 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 277 | /* |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 278 | * initialize audio channel status registers |
| 279 | * This function is called in the prepare callback |
| 280 | */ |
| 281 | static int had_prog_status_reg(struct snd_pcm_substream *substream, |
| 282 | struct snd_intelhad *intelhaddata) |
| 283 | { |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 284 | union aud_ch_status_0 ch_stat0 = {.regval = 0}; |
| 285 | union aud_ch_status_1 ch_stat1 = {.regval = 0}; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 286 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 287 | ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits & |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 288 | IEC958_AES0_NONAUDIO) >> 1; |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 289 | ch_stat0.regx.clk_acc = (intelhaddata->aes_bits & |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 290 | IEC958_AES3_CON_CLOCK) >> 4; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 291 | |
| 292 | switch (substream->runtime->rate) { |
| 293 | case AUD_SAMPLE_RATE_32: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 294 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 295 | break; |
| 296 | |
| 297 | case AUD_SAMPLE_RATE_44_1: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 298 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 299 | break; |
| 300 | case AUD_SAMPLE_RATE_48: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 301 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 302 | break; |
| 303 | case AUD_SAMPLE_RATE_88_2: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 304 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 305 | break; |
| 306 | case AUD_SAMPLE_RATE_96: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 307 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 308 | break; |
| 309 | case AUD_SAMPLE_RATE_176_4: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 310 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 311 | break; |
| 312 | case AUD_SAMPLE_RATE_192: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 313 | ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 314 | break; |
| 315 | |
| 316 | default: |
| 317 | /* control should never come here */ |
| 318 | return -EINVAL; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 319 | } |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 320 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 321 | had_write_register(intelhaddata, |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 322 | AUD_CH_STATUS_0, ch_stat0.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 323 | |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 324 | switch (substream->runtime->format) { |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 325 | case SNDRV_PCM_FORMAT_S16_LE: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 326 | ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20; |
| 327 | ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS; |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 328 | break; |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 329 | case SNDRV_PCM_FORMAT_S24_LE: |
| 330 | case SNDRV_PCM_FORMAT_S32_LE: |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 331 | ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24; |
| 332 | ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS; |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 333 | break; |
| 334 | default: |
| 335 | return -EINVAL; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 336 | } |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 337 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 338 | had_write_register(intelhaddata, |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 339 | AUD_CH_STATUS_1, ch_stat1.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Takashi Iwai | 76296ef | 2017-01-30 16:09:11 +0100 | [diff] [blame] | 343 | /* |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 344 | * function to initialize audio |
| 345 | * registers and buffer confgiuration registers |
| 346 | * This function is called in the prepare callback |
| 347 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 348 | static int had_init_audio_ctrl(struct snd_pcm_substream *substream, |
| 349 | struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 350 | { |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 351 | union aud_cfg cfg_val = {.regval = 0}; |
| 352 | union aud_buf_config buf_cfg = {.regval = 0}; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 353 | u8 channels; |
| 354 | |
| 355 | had_prog_status_reg(substream, intelhaddata); |
| 356 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 357 | buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD; |
| 358 | buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD; |
| 359 | buf_cfg.regx.aud_delay = 0; |
| 360 | had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 361 | |
| 362 | channels = substream->runtime->channels; |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 363 | cfg_val.regx.num_ch = channels - 2; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 364 | if (channels <= 2) |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 365 | cfg_val.regx.layout = LAYOUT0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 366 | else |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 367 | cfg_val.regx.layout = LAYOUT1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 368 | |
Takashi Iwai | 3fe2cf7 | 2017-02-07 13:53:42 +0100 | [diff] [blame] | 369 | if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE) |
| 370 | cfg_val.regx.packet_mode = 1; |
| 371 | |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 372 | if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE) |
| 373 | cfg_val.regx.left_align = 1; |
| 374 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 375 | cfg_val.regx.val_bit = 1; |
Takashi Iwai | 83af57d | 2017-02-03 08:50:06 +0100 | [diff] [blame] | 376 | |
| 377 | /* fix up the DP bits */ |
| 378 | if (intelhaddata->dp_output) { |
| 379 | cfg_val.regx.dp_modei = 1; |
| 380 | cfg_val.regx.set = 1; |
| 381 | } |
| 382 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 383 | had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval); |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 384 | intelhaddata->aud_config = cfg_val; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 388 | /* |
| 389 | * Compute derived values in channel_allocations[]. |
| 390 | */ |
| 391 | static void init_channel_allocations(void) |
| 392 | { |
| 393 | int i, j; |
| 394 | struct cea_channel_speaker_allocation *p; |
| 395 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 396 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 397 | p = channel_allocations + i; |
| 398 | p->channels = 0; |
| 399 | p->spk_mask = 0; |
| 400 | for (j = 0; j < ARRAY_SIZE(p->speakers); j++) |
| 401 | if (p->speakers[j]) { |
| 402 | p->channels++; |
| 403 | p->spk_mask |= p->speakers[j]; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * The transformation takes two steps: |
| 410 | * |
| 411 | * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask |
| 412 | * spk_mask => (channel_allocations[]) => ai->CA |
| 413 | * |
| 414 | * TODO: it could select the wrong CA from multiple candidates. |
| 415 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 416 | static int had_channel_allocation(struct snd_intelhad *intelhaddata, |
| 417 | int channels) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 418 | { |
| 419 | int i; |
| 420 | int ca = 0; |
| 421 | int spk_mask = 0; |
| 422 | |
| 423 | /* |
| 424 | * CA defaults to 0 for basic stereo audio |
| 425 | */ |
| 426 | if (channels <= 2) |
| 427 | return 0; |
| 428 | |
| 429 | /* |
| 430 | * expand ELD's speaker allocation mask |
| 431 | * |
| 432 | * ELD tells the speaker mask in a compact(paired) form, |
| 433 | * expand ELD's notions to match the ones used by Audio InfoFrame. |
| 434 | */ |
| 435 | |
| 436 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
Takashi Iwai | df0435d | 2017-02-02 15:37:11 +0100 | [diff] [blame] | 437 | if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i)) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 438 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 439 | } |
| 440 | |
| 441 | /* search for the first working match in the CA table */ |
| 442 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 443 | if (channels == channel_allocations[i].channels && |
| 444 | (spk_mask & channel_allocations[i].spk_mask) == |
| 445 | channel_allocations[i].spk_mask) { |
| 446 | ca = channel_allocations[i].ca_index; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 451 | dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 452 | |
| 453 | return ca; |
| 454 | } |
| 455 | |
| 456 | /* from speaker bit mask to ALSA API channel position */ |
| 457 | static int spk_to_chmap(int spk) |
| 458 | { |
Takashi Iwai | 4a5ddb2 | 2017-02-01 16:45:38 +0100 | [diff] [blame] | 459 | const struct channel_map_table *t = map_tables; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 460 | |
| 461 | for (; t->map; t++) { |
| 462 | if (t->spk_mask == spk) |
| 463 | return t->map; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 468 | static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 469 | { |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 470 | int i, c; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 471 | int spk_mask = 0; |
| 472 | struct snd_pcm_chmap_elem *chmap; |
| 473 | u8 eld_high, eld_high_mask = 0xF0; |
| 474 | u8 high_msb; |
| 475 | |
Takashi Iwai | 1835319 | 2017-02-15 21:42:20 +0100 | [diff] [blame] | 476 | kfree(intelhaddata->chmap->chmap); |
| 477 | intelhaddata->chmap->chmap = NULL; |
| 478 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 479 | chmap = kzalloc(sizeof(*chmap), GFP_KERNEL); |
Takashi Iwai | 1835319 | 2017-02-15 21:42:20 +0100 | [diff] [blame] | 480 | if (!chmap) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 481 | return; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 482 | |
Takashi Iwai | df0435d | 2017-02-02 15:37:11 +0100 | [diff] [blame] | 483 | dev_dbg(intelhaddata->dev, "eld speaker = %x\n", |
| 484 | intelhaddata->eld[DRM_ELD_SPEAKER]); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 485 | |
| 486 | /* WA: Fix the max channel supported to 8 */ |
| 487 | |
| 488 | /* |
| 489 | * Sink may support more than 8 channels, if eld_high has more than |
| 490 | * one bit set. SOC supports max 8 channels. |
| 491 | * Refer eld_speaker_allocation_bits, for sink speaker allocation |
| 492 | */ |
| 493 | |
| 494 | /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */ |
Takashi Iwai | df0435d | 2017-02-02 15:37:11 +0100 | [diff] [blame] | 495 | eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 496 | if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) { |
| 497 | /* eld_high & (eld_high-1): if more than 1 bit set */ |
| 498 | /* 0x1F: 7 channels */ |
| 499 | for (i = 1; i < 4; i++) { |
| 500 | high_msb = eld_high & (0x80 >> i); |
| 501 | if (high_msb) { |
Takashi Iwai | df0435d | 2017-02-02 15:37:11 +0100 | [diff] [blame] | 502 | intelhaddata->eld[DRM_ELD_SPEAKER] &= |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 503 | high_msb | 0xF; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) { |
Takashi Iwai | df0435d | 2017-02-02 15:37:11 +0100 | [diff] [blame] | 510 | if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i)) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 511 | spk_mask |= eld_speaker_allocation_bits[i]; |
| 512 | } |
| 513 | |
| 514 | for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) { |
| 515 | if (spk_mask == channel_allocations[i].spk_mask) { |
| 516 | for (c = 0; c < channel_allocations[i].channels; c++) { |
| 517 | chmap->map[c] = spk_to_chmap( |
| 518 | channel_allocations[i].speakers[ |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 519 | (MAX_SPEAKERS - 1) - c]); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 520 | } |
| 521 | chmap->channels = channel_allocations[i].channels; |
| 522 | intelhaddata->chmap->chmap = chmap; |
| 523 | break; |
| 524 | } |
| 525 | } |
Takashi Iwai | 1835319 | 2017-02-15 21:42:20 +0100 | [diff] [blame] | 526 | if (i >= ARRAY_SIZE(channel_allocations)) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 527 | kfree(chmap); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
| 531 | * ALSA API channel-map control callbacks |
| 532 | */ |
| 533 | static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol, |
| 534 | struct snd_ctl_elem_info *uinfo) |
| 535 | { |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 536 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 537 | uinfo->count = HAD_MAX_CHANNEL; |
| 538 | uinfo->value.integer.min = 0; |
| 539 | uinfo->value.integer.max = SNDRV_CHMAP_LAST; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol, |
| 544 | struct snd_ctl_elem_value *ucontrol) |
| 545 | { |
| 546 | struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); |
| 547 | struct snd_intelhad *intelhaddata = info->private_data; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 548 | int i; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 549 | const struct snd_pcm_chmap_elem *chmap; |
| 550 | |
Takashi Iwai | a72ccfb | 2017-02-15 21:45:06 +0100 | [diff] [blame] | 551 | memset(ucontrol->value.integer.value, 0, |
| 552 | sizeof(long) * HAD_MAX_CHANNEL); |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 553 | mutex_lock(&intelhaddata->mutex); |
| 554 | if (!intelhaddata->chmap->chmap) { |
| 555 | mutex_unlock(&intelhaddata->mutex); |
Takashi Iwai | a72ccfb | 2017-02-15 21:45:06 +0100 | [diff] [blame] | 556 | return 0; |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 559 | chmap = intelhaddata->chmap->chmap; |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 560 | for (i = 0; i < chmap->channels; i++) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 561 | ucontrol->value.integer.value[i] = chmap->map[i]; |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 562 | mutex_unlock(&intelhaddata->mutex); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata, |
| 568 | struct snd_pcm *pcm) |
| 569 | { |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 570 | int err; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 571 | |
| 572 | err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, |
| 573 | NULL, 0, (unsigned long)intelhaddata, |
| 574 | &intelhaddata->chmap); |
| 575 | if (err < 0) |
| 576 | return err; |
| 577 | |
| 578 | intelhaddata->chmap->private_data = intelhaddata; |
Takashi Iwai | e9d65ab | 2017-01-31 16:11:27 +0100 | [diff] [blame] | 579 | intelhaddata->chmap->kctl->info = had_chmap_ctl_info; |
| 580 | intelhaddata->chmap->kctl->get = had_chmap_ctl_get; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 581 | intelhaddata->chmap->chmap = NULL; |
| 582 | return 0; |
| 583 | } |
| 584 | |
Takashi Iwai | 76296ef | 2017-01-30 16:09:11 +0100 | [diff] [blame] | 585 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 586 | * Initialize Data Island Packets registers |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 587 | * This function is called in the prepare callback |
| 588 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 589 | static void had_prog_dip(struct snd_pcm_substream *substream, |
| 590 | struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 591 | { |
| 592 | int i; |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 593 | union aud_ctrl_st ctrl_state = {.regval = 0}; |
| 594 | union aud_info_frame2 frame2 = {.regval = 0}; |
| 595 | union aud_info_frame3 frame3 = {.regval = 0}; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 596 | u8 checksum = 0; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 597 | u32 info_frame; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 598 | int channels; |
Takashi Iwai | 36ed346 | 2017-02-02 17:06:38 +0100 | [diff] [blame] | 599 | int ca; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 600 | |
| 601 | channels = substream->runtime->channels; |
| 602 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 603 | had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 604 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 605 | ca = had_channel_allocation(intelhaddata, channels); |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 606 | if (intelhaddata->dp_output) { |
| 607 | info_frame = DP_INFO_FRAME_WORD1; |
Takashi Iwai | 36ed346 | 2017-02-02 17:06:38 +0100 | [diff] [blame] | 608 | frame2.regval = (substream->runtime->channels - 1) | (ca << 24); |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 609 | } else { |
| 610 | info_frame = HDMI_INFO_FRAME_WORD1; |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 611 | frame2.regx.chnl_cnt = substream->runtime->channels - 1; |
Takashi Iwai | 36ed346 | 2017-02-02 17:06:38 +0100 | [diff] [blame] | 612 | frame3.regx.chnl_alloc = ca; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 613 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 614 | /* Calculte the byte wide checksum for all valid DIP words */ |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 615 | for (i = 0; i < BYTES_PER_WORD; i++) |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 616 | checksum += (info_frame >> (i * 8)) & 0xff; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 617 | for (i = 0; i < BYTES_PER_WORD; i++) |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 618 | checksum += (frame2.regval >> (i * 8)) & 0xff; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 619 | for (i = 0; i < BYTES_PER_WORD; i++) |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 620 | checksum += (frame3.regval >> (i * 8)) & 0xff; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 621 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 622 | frame2.regx.chksum = -(checksum); |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 623 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 624 | |
Takashi Iwai | 4151ee8 | 2017-01-31 18:14:15 +0100 | [diff] [blame] | 625 | had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame); |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 626 | had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval); |
| 627 | had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 628 | |
| 629 | /* program remaining DIP words with zero */ |
| 630 | for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++) |
Takashi Iwai | 4151ee8 | 2017-01-31 18:14:15 +0100 | [diff] [blame] | 631 | had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 632 | |
Takashi Iwai | 7ceba75 | 2017-02-02 15:58:35 +0100 | [diff] [blame] | 633 | ctrl_state.regx.dip_freq = 1; |
| 634 | ctrl_state.regx.dip_en_sta = 1; |
| 635 | had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 636 | } |
| 637 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 638 | static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate) |
| 639 | { |
| 640 | u32 maud_val; |
| 641 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 642 | /* Select maud according to DP 1.2 spec */ |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 643 | if (link_rate == DP_2_7_GHZ) { |
| 644 | switch (aud_samp_freq) { |
| 645 | case AUD_SAMPLE_RATE_32: |
| 646 | maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL; |
| 647 | break; |
| 648 | |
| 649 | case AUD_SAMPLE_RATE_44_1: |
| 650 | maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL; |
| 651 | break; |
| 652 | |
| 653 | case AUD_SAMPLE_RATE_48: |
| 654 | maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL; |
| 655 | break; |
| 656 | |
| 657 | case AUD_SAMPLE_RATE_88_2: |
| 658 | maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL; |
| 659 | break; |
| 660 | |
| 661 | case AUD_SAMPLE_RATE_96: |
| 662 | maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL; |
| 663 | break; |
| 664 | |
| 665 | case AUD_SAMPLE_RATE_176_4: |
| 666 | maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL; |
| 667 | break; |
| 668 | |
| 669 | case HAD_MAX_RATE: |
| 670 | maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL; |
| 671 | break; |
| 672 | |
| 673 | default: |
| 674 | maud_val = -EINVAL; |
| 675 | break; |
| 676 | } |
| 677 | } else if (link_rate == DP_1_62_GHZ) { |
| 678 | switch (aud_samp_freq) { |
| 679 | case AUD_SAMPLE_RATE_32: |
| 680 | maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL; |
| 681 | break; |
| 682 | |
| 683 | case AUD_SAMPLE_RATE_44_1: |
| 684 | maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL; |
| 685 | break; |
| 686 | |
| 687 | case AUD_SAMPLE_RATE_48: |
| 688 | maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL; |
| 689 | break; |
| 690 | |
| 691 | case AUD_SAMPLE_RATE_88_2: |
| 692 | maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL; |
| 693 | break; |
| 694 | |
| 695 | case AUD_SAMPLE_RATE_96: |
| 696 | maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL; |
| 697 | break; |
| 698 | |
| 699 | case AUD_SAMPLE_RATE_176_4: |
| 700 | maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL; |
| 701 | break; |
| 702 | |
| 703 | case HAD_MAX_RATE: |
| 704 | maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL; |
| 705 | break; |
| 706 | |
| 707 | default: |
| 708 | maud_val = -EINVAL; |
| 709 | break; |
| 710 | } |
| 711 | } else |
| 712 | maud_val = -EINVAL; |
| 713 | |
| 714 | return maud_val; |
| 715 | } |
| 716 | |
Takashi Iwai | 76296ef | 2017-01-30 16:09:11 +0100 | [diff] [blame] | 717 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 718 | * Program HDMI audio CTS value |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 719 | * |
| 720 | * @aud_samp_freq: sampling frequency of audio data |
| 721 | * @tmds: sampling frequency of the display data |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 722 | * @link_rate: DP link rate |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 723 | * @n_param: N value, depends on aud_samp_freq |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 724 | * @intelhaddata: substream private data |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 725 | * |
| 726 | * Program CTS register based on the audio and display sampling frequency |
| 727 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 728 | static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate, |
| 729 | u32 n_param, struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 730 | { |
| 731 | u32 cts_val; |
| 732 | u64 dividend, divisor; |
| 733 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 734 | if (intelhaddata->dp_output) { |
| 735 | /* Substitute cts_val with Maud according to DP 1.2 spec*/ |
| 736 | cts_val = had_calculate_maud_value(aud_samp_freq, link_rate); |
| 737 | } else { |
| 738 | /* Calculate CTS according to HDMI 1.3a spec*/ |
| 739 | dividend = (u64)tmds * n_param*1000; |
| 740 | divisor = 128 * aud_samp_freq; |
| 741 | cts_val = div64_u64(dividend, divisor); |
| 742 | } |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 743 | dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n", |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 744 | tmds, n_param, cts_val); |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 745 | had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val)); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static int had_calculate_n_value(u32 aud_samp_freq) |
| 749 | { |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 750 | int n_val; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 751 | |
| 752 | /* Select N according to HDMI 1.3a spec*/ |
| 753 | switch (aud_samp_freq) { |
| 754 | case AUD_SAMPLE_RATE_32: |
| 755 | n_val = 4096; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 756 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 757 | |
| 758 | case AUD_SAMPLE_RATE_44_1: |
| 759 | n_val = 6272; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 760 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 761 | |
| 762 | case AUD_SAMPLE_RATE_48: |
| 763 | n_val = 6144; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 764 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 765 | |
| 766 | case AUD_SAMPLE_RATE_88_2: |
| 767 | n_val = 12544; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 768 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 769 | |
| 770 | case AUD_SAMPLE_RATE_96: |
| 771 | n_val = 12288; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 772 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 773 | |
| 774 | case AUD_SAMPLE_RATE_176_4: |
| 775 | n_val = 25088; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 776 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 777 | |
| 778 | case HAD_MAX_RATE: |
| 779 | n_val = 24576; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 780 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 781 | |
| 782 | default: |
| 783 | n_val = -EINVAL; |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 784 | break; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 785 | } |
| 786 | return n_val; |
| 787 | } |
| 788 | |
Takashi Iwai | 76296ef | 2017-01-30 16:09:11 +0100 | [diff] [blame] | 789 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 790 | * Program HDMI audio N value |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 791 | * |
| 792 | * @aud_samp_freq: sampling frequency of audio data |
| 793 | * @n_param: N value, depends on aud_samp_freq |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 794 | * @intelhaddata: substream private data |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 795 | * |
| 796 | * This function is called in the prepare callback. |
| 797 | * It programs based on the audio and display sampling frequency |
| 798 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 799 | static int had_prog_n(u32 aud_samp_freq, u32 *n_param, |
| 800 | struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 801 | { |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 802 | int n_val; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 803 | |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 804 | if (intelhaddata->dp_output) { |
| 805 | /* |
| 806 | * According to DP specs, Maud and Naud values hold |
| 807 | * a relationship, which is stated as: |
| 808 | * Maud/Naud = 512 * fs / f_LS_Clk |
| 809 | * where, fs is the sampling frequency of the audio stream |
| 810 | * and Naud is 32768 for Async clock. |
| 811 | */ |
| 812 | |
| 813 | n_val = DP_NAUD_VAL; |
| 814 | } else |
| 815 | n_val = had_calculate_n_value(aud_samp_freq); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 816 | |
| 817 | if (n_val < 0) |
| 818 | return n_val; |
| 819 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 820 | had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val)); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 821 | *n_param = n_val; |
| 822 | return 0; |
| 823 | } |
| 824 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 825 | /* |
| 826 | * PCM ring buffer handling |
| 827 | * |
| 828 | * The hardware provides a ring buffer with the fixed 4 buffer descriptors |
| 829 | * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping |
| 830 | * moves at each period elapsed. The below illustrates how it works: |
| 831 | * |
| 832 | * At time=0 |
| 833 | * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1| |
| 834 | * BD | 0 | 1 | 2 | 3 | |
| 835 | * |
| 836 | * At time=1 (period elapsed) |
| 837 | * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1| |
| 838 | * BD | 1 | 2 | 3 | 0 | |
| 839 | * |
| 840 | * At time=2 (second period elapsed) |
| 841 | * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1| |
| 842 | * BD | 2 | 3 | 0 | 1 | |
| 843 | * |
| 844 | * The bd_head field points to the index of the BD to be read. It's also the |
| 845 | * position to be filled at next. The pcm_head and the pcm_filled fields |
| 846 | * point to the indices of the current position and of the next position to |
| 847 | * be filled, respectively. For PCM buffer there are both _head and _filled |
| 848 | * because they may be difference when nperiods > 4. For example, in the |
| 849 | * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5: |
| 850 | * |
| 851 | * pcm_head (=1) --v v-- pcm_filled (=5) |
| 852 | * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1| |
| 853 | * BD | 1 | 2 | 3 | 0 | |
| 854 | * bd_head (=1) --^ ^-- next to fill (= bd_head) |
| 855 | * |
| 856 | * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that |
| 857 | * the hardware skips those BDs in the loop. |
Takashi Iwai | 8d48c01 | 2017-02-07 08:05:46 +0100 | [diff] [blame] | 858 | * |
| 859 | * An exceptional setup is the case with nperiods=1. Since we have to update |
| 860 | * BDs after finishing one BD processing, we'd need at least two BDs, where |
| 861 | * both BDs point to the same content, the same address, the same size of the |
| 862 | * whole PCM buffer. |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 863 | */ |
| 864 | |
| 865 | #define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH) |
| 866 | #define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH) |
| 867 | |
| 868 | /* Set up a buffer descriptor at the "filled" position */ |
| 869 | static void had_prog_bd(struct snd_pcm_substream *substream, |
| 870 | struct snd_intelhad *intelhaddata) |
| 871 | { |
| 872 | int idx = intelhaddata->bd_head; |
| 873 | int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes; |
| 874 | u32 addr = substream->runtime->dma_addr + ofs; |
| 875 | |
Takashi Iwai | e8de985 | 2017-02-07 08:09:12 +0100 | [diff] [blame] | 876 | addr |= AUD_BUF_VALID; |
| 877 | if (!substream->runtime->no_period_wakeup) |
| 878 | addr |= AUD_BUF_INTR_EN; |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 879 | had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr); |
| 880 | had_write_register(intelhaddata, AUD_BUF_LEN(idx), |
| 881 | intelhaddata->period_bytes); |
| 882 | |
| 883 | /* advance the indices to the next */ |
| 884 | intelhaddata->bd_head++; |
| 885 | intelhaddata->bd_head %= intelhaddata->num_bds; |
| 886 | intelhaddata->pcmbuf_filled++; |
| 887 | intelhaddata->pcmbuf_filled %= substream->runtime->periods; |
| 888 | } |
| 889 | |
| 890 | /* invalidate a buffer descriptor with the given index */ |
| 891 | static void had_invalidate_bd(struct snd_intelhad *intelhaddata, |
| 892 | int idx) |
| 893 | { |
| 894 | had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0); |
| 895 | had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0); |
| 896 | } |
| 897 | |
| 898 | /* Initial programming of ring buffer */ |
| 899 | static void had_init_ringbuf(struct snd_pcm_substream *substream, |
| 900 | struct snd_intelhad *intelhaddata) |
| 901 | { |
| 902 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 903 | int i, num_periods; |
| 904 | |
| 905 | num_periods = runtime->periods; |
| 906 | intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS); |
Takashi Iwai | 8d48c01 | 2017-02-07 08:05:46 +0100 | [diff] [blame] | 907 | /* set the minimum 2 BDs for num_periods=1 */ |
| 908 | intelhaddata->num_bds = max(intelhaddata->num_bds, 2U); |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 909 | intelhaddata->period_bytes = |
| 910 | frames_to_bytes(runtime, runtime->period_size); |
| 911 | WARN_ON(intelhaddata->period_bytes & 0x3f); |
| 912 | |
| 913 | intelhaddata->bd_head = 0; |
| 914 | intelhaddata->pcmbuf_head = 0; |
| 915 | intelhaddata->pcmbuf_filled = 0; |
| 916 | |
| 917 | for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) { |
Takashi Iwai | 8d48c01 | 2017-02-07 08:05:46 +0100 | [diff] [blame] | 918 | if (i < intelhaddata->num_bds) |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 919 | had_prog_bd(substream, intelhaddata); |
| 920 | else /* invalidate the rest */ |
| 921 | had_invalidate_bd(intelhaddata, i); |
| 922 | } |
| 923 | |
| 924 | intelhaddata->bd_head = 0; /* reset at head again before starting */ |
| 925 | } |
| 926 | |
| 927 | /* process a bd, advance to the next */ |
| 928 | static void had_advance_ringbuf(struct snd_pcm_substream *substream, |
| 929 | struct snd_intelhad *intelhaddata) |
| 930 | { |
| 931 | int num_periods = substream->runtime->periods; |
| 932 | |
| 933 | /* reprogram the next buffer */ |
| 934 | had_prog_bd(substream, intelhaddata); |
| 935 | |
| 936 | /* proceed to next */ |
| 937 | intelhaddata->pcmbuf_head++; |
| 938 | intelhaddata->pcmbuf_head %= num_periods; |
| 939 | } |
| 940 | |
| 941 | /* process the current BD(s); |
| 942 | * returns the current PCM buffer byte position, or -EPIPE for underrun. |
| 943 | */ |
| 944 | static int had_process_ringbuf(struct snd_pcm_substream *substream, |
| 945 | struct snd_intelhad *intelhaddata) |
| 946 | { |
| 947 | int len, processed; |
| 948 | unsigned long flags; |
| 949 | |
| 950 | processed = 0; |
| 951 | spin_lock_irqsave(&intelhaddata->had_spinlock, flags); |
| 952 | for (;;) { |
| 953 | /* get the remaining bytes on the buffer */ |
| 954 | had_read_register(intelhaddata, |
| 955 | AUD_BUF_LEN(intelhaddata->bd_head), |
| 956 | &len); |
| 957 | if (len < 0 || len > intelhaddata->period_bytes) { |
| 958 | dev_dbg(intelhaddata->dev, "Invalid buf length %d\n", |
| 959 | len); |
| 960 | len = -EPIPE; |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | if (len > 0) /* OK, this is the current buffer */ |
| 965 | break; |
| 966 | |
| 967 | /* len=0 => already empty, check the next buffer */ |
| 968 | if (++processed >= intelhaddata->num_bds) { |
| 969 | len = -EPIPE; /* all empty? - report underrun */ |
| 970 | goto out; |
| 971 | } |
| 972 | had_advance_ringbuf(substream, intelhaddata); |
| 973 | } |
| 974 | |
| 975 | len = intelhaddata->period_bytes - len; |
| 976 | len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head; |
| 977 | out: |
| 978 | spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags); |
| 979 | return len; |
| 980 | } |
| 981 | |
| 982 | /* called from irq handler */ |
| 983 | static void had_process_buffer_done(struct snd_intelhad *intelhaddata) |
| 984 | { |
| 985 | struct snd_pcm_substream *substream; |
| 986 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 987 | substream = had_substream_get(intelhaddata); |
| 988 | if (!substream) |
| 989 | return; /* no stream? - bail out */ |
| 990 | |
Takashi Iwai | be9a2e9 | 2017-02-15 22:05:34 +0100 | [diff] [blame] | 991 | if (!intelhaddata->connected) { |
| 992 | snd_pcm_stop_xrun(substream); |
| 993 | goto out; /* disconnected? - bail out */ |
| 994 | } |
| 995 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 996 | /* process or stop the stream */ |
| 997 | if (had_process_ringbuf(substream, intelhaddata) < 0) |
| 998 | snd_pcm_stop_xrun(substream); |
| 999 | else |
| 1000 | snd_pcm_period_elapsed(substream); |
| 1001 | |
Takashi Iwai | be9a2e9 | 2017-02-15 22:05:34 +0100 | [diff] [blame] | 1002 | out: |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1003 | had_substream_put(intelhaddata); |
| 1004 | } |
| 1005 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1006 | /* |
| 1007 | * The interrupt status 'sticky' bits might not be cleared by |
| 1008 | * setting '1' to that bit once... |
| 1009 | */ |
| 1010 | static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1011 | { |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1012 | int i; |
| 1013 | u32 val; |
| 1014 | |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1015 | for (i = 0; i < 100; i++) { |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1016 | /* clear bit30, 31 AUD_HDMI_STATUS */ |
| 1017 | had_read_register(intelhaddata, AUD_HDMI_STATUS, &val); |
Takashi Iwai | 77531be | 2017-02-07 12:17:23 +0100 | [diff] [blame] | 1018 | if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN)) |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1019 | return; |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1020 | udelay(100); |
| 1021 | cond_resched(); |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1022 | had_write_register(intelhaddata, AUD_HDMI_STATUS, val); |
| 1023 | } |
| 1024 | dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n"); |
| 1025 | } |
| 1026 | |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1027 | /* Perform some reset procedure but only when need_reset is set; |
| 1028 | * this is called from prepare or hw_free callbacks once after trigger STOP |
| 1029 | * or underrun has been processed in order to settle down the h/w state. |
| 1030 | */ |
| 1031 | static void had_do_reset(struct snd_intelhad *intelhaddata) |
| 1032 | { |
Takashi Iwai | 28ed125 | 2017-02-15 22:02:10 +0100 | [diff] [blame] | 1033 | if (!intelhaddata->need_reset || !intelhaddata->connected) |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1034 | return; |
| 1035 | |
| 1036 | /* Reset buffer pointers */ |
| 1037 | had_reset_audio(intelhaddata); |
| 1038 | wait_clear_underrun_bit(intelhaddata); |
| 1039 | intelhaddata->need_reset = false; |
| 1040 | } |
| 1041 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1042 | /* called from irq handler */ |
| 1043 | static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata) |
| 1044 | { |
| 1045 | struct snd_pcm_substream *substream; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1046 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1047 | /* Report UNDERRUN error to above layers */ |
| 1048 | substream = had_substream_get(intelhaddata); |
| 1049 | if (substream) { |
| 1050 | snd_pcm_stop_xrun(substream); |
| 1051 | had_substream_put(intelhaddata); |
| 1052 | } |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1053 | intelhaddata->need_reset = true; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1054 | } |
| 1055 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1056 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1057 | * ALSA PCM open callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1058 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1059 | static int had_pcm_open(struct snd_pcm_substream *substream) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1060 | { |
| 1061 | struct snd_intelhad *intelhaddata; |
| 1062 | struct snd_pcm_runtime *runtime; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1063 | int retval; |
| 1064 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1065 | intelhaddata = snd_pcm_substream_chip(substream); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1066 | runtime = substream->runtime; |
| 1067 | |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1068 | pm_runtime_get_sync(intelhaddata->dev); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1069 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1070 | /* set the runtime hw parameter with local snd_pcm_hardware struct */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1071 | runtime->hw = had_pcm_hardware; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1072 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1073 | retval = snd_pcm_hw_constraint_integer(runtime, |
| 1074 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 1075 | if (retval < 0) |
Takashi Iwai | fa5dfe6 | 2017-02-01 22:03:26 +0100 | [diff] [blame] | 1076 | goto error; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1077 | |
| 1078 | /* Make sure, that the period size is always aligned |
| 1079 | * 64byte boundary |
| 1080 | */ |
| 1081 | retval = snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 1082 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64); |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1083 | if (retval < 0) |
Takashi Iwai | fa5dfe6 | 2017-02-01 22:03:26 +0100 | [diff] [blame] | 1084 | goto error; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1085 | |
Takashi Iwai | 85bd874 | 2017-02-07 13:33:17 +0100 | [diff] [blame] | 1086 | retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); |
| 1087 | if (retval < 0) |
| 1088 | goto error; |
| 1089 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1090 | /* expose PCM substream */ |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1091 | spin_lock_irq(&intelhaddata->had_spinlock); |
| 1092 | intelhaddata->stream_info.substream = substream; |
| 1093 | intelhaddata->stream_info.substream_refcount++; |
| 1094 | spin_unlock_irq(&intelhaddata->had_spinlock); |
| 1095 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1096 | return retval; |
Takashi Iwai | fa5dfe6 | 2017-02-01 22:03:26 +0100 | [diff] [blame] | 1097 | error: |
Takashi Iwai | 3002b9f | 2017-02-13 09:52:44 +0100 | [diff] [blame] | 1098 | pm_runtime_mark_last_busy(intelhaddata->dev); |
| 1099 | pm_runtime_put_autosuspend(intelhaddata->dev); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1100 | return retval; |
| 1101 | } |
| 1102 | |
Takashi Iwai | df76df1 | 2017-01-31 16:04:10 +0100 | [diff] [blame] | 1103 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1104 | * ALSA PCM close callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1105 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1106 | static int had_pcm_close(struct snd_pcm_substream *substream) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1107 | { |
| 1108 | struct snd_intelhad *intelhaddata; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1109 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1110 | intelhaddata = snd_pcm_substream_chip(substream); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1111 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1112 | /* unreference and sync with the pending PCM accesses */ |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1113 | spin_lock_irq(&intelhaddata->had_spinlock); |
| 1114 | intelhaddata->stream_info.substream = NULL; |
| 1115 | intelhaddata->stream_info.substream_refcount--; |
| 1116 | while (intelhaddata->stream_info.substream_refcount > 0) { |
| 1117 | spin_unlock_irq(&intelhaddata->had_spinlock); |
| 1118 | cpu_relax(); |
| 1119 | spin_lock_irq(&intelhaddata->had_spinlock); |
| 1120 | } |
| 1121 | spin_unlock_irq(&intelhaddata->had_spinlock); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1122 | |
Takashi Iwai | 3002b9f | 2017-02-13 09:52:44 +0100 | [diff] [blame] | 1123 | pm_runtime_mark_last_busy(intelhaddata->dev); |
| 1124 | pm_runtime_put_autosuspend(intelhaddata->dev); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1128 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1129 | * ALSA PCM hw_params callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1130 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1131 | static int had_pcm_hw_params(struct snd_pcm_substream *substream, |
| 1132 | struct snd_pcm_hw_params *hw_params) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1133 | { |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1134 | struct snd_intelhad *intelhaddata; |
Takashi Iwai | 87246f7 | 2018-08-08 22:38:36 +0200 | [diff] [blame] | 1135 | int buf_size, retval; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1136 | |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1137 | intelhaddata = snd_pcm_substream_chip(substream); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1138 | buf_size = params_buffer_bytes(hw_params); |
| 1139 | retval = snd_pcm_lib_malloc_pages(substream, buf_size); |
| 1140 | if (retval < 0) |
| 1141 | return retval; |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1142 | dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n", |
| 1143 | __func__, buf_size); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1144 | return retval; |
| 1145 | } |
| 1146 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1147 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1148 | * ALSA PCM hw_free callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1149 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1150 | static int had_pcm_hw_free(struct snd_pcm_substream *substream) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1151 | { |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1152 | struct snd_intelhad *intelhaddata; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1153 | |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1154 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1155 | had_do_reset(intelhaddata); |
| 1156 | |
Takashi Iwai | 87246f7 | 2018-08-08 22:38:36 +0200 | [diff] [blame] | 1157 | return snd_pcm_lib_free_pages(substream); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1158 | } |
| 1159 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1160 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1161 | * ALSA PCM trigger callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1162 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1163 | static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1164 | { |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1165 | int retval = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1166 | struct snd_intelhad *intelhaddata; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1167 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1168 | intelhaddata = snd_pcm_substream_chip(substream); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1169 | |
Takashi Iwai | df42cb4 | 2017-02-12 11:35:44 +0100 | [diff] [blame] | 1170 | spin_lock(&intelhaddata->had_spinlock); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1171 | switch (cmd) { |
| 1172 | case SNDRV_PCM_TRIGGER_START: |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1173 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1174 | case SNDRV_PCM_TRIGGER_RESUME: |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1175 | /* Enable Audio */ |
Takashi Iwai | 075a1d4 | 2017-02-07 07:55:27 +0100 | [diff] [blame] | 1176 | had_ack_irqs(intelhaddata); /* FIXME: do we need this? */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 1177 | had_enable_audio(intelhaddata, true); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1178 | break; |
| 1179 | |
| 1180 | case SNDRV_PCM_TRIGGER_STOP: |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1181 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1182 | /* Disable Audio */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 1183 | had_enable_audio(intelhaddata, false); |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1184 | intelhaddata->need_reset = true; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1185 | break; |
| 1186 | |
| 1187 | default: |
| 1188 | retval = -EINVAL; |
| 1189 | } |
Takashi Iwai | df42cb4 | 2017-02-12 11:35:44 +0100 | [diff] [blame] | 1190 | spin_unlock(&intelhaddata->had_spinlock); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1191 | return retval; |
| 1192 | } |
| 1193 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1194 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1195 | * ALSA PCM prepare callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1196 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1197 | static int had_pcm_prepare(struct snd_pcm_substream *substream) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1198 | { |
| 1199 | int retval; |
| 1200 | u32 disp_samp_freq, n_param; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1201 | u32 link_rate = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1202 | struct snd_intelhad *intelhaddata; |
| 1203 | struct snd_pcm_runtime *runtime; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1204 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1205 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1206 | runtime = substream->runtime; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1207 | |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1208 | dev_dbg(intelhaddata->dev, "period_size=%d\n", |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1209 | (int)frames_to_bytes(runtime, runtime->period_size)); |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1210 | dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods); |
| 1211 | dev_dbg(intelhaddata->dev, "buffer_size=%d\n", |
| 1212 | (int)snd_pcm_lib_buffer_bytes(substream)); |
| 1213 | dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate); |
| 1214 | dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1215 | |
Takashi Iwai | e2acecf | 2017-02-11 08:21:56 +0100 | [diff] [blame] | 1216 | had_do_reset(intelhaddata); |
| 1217 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1218 | /* Get N value in KHz */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1219 | disp_samp_freq = intelhaddata->tmds_clock_speed; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1220 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1221 | retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1222 | if (retval) { |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1223 | dev_err(intelhaddata->dev, |
| 1224 | "programming N value failed %#x\n", retval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1225 | goto prep_end; |
| 1226 | } |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1227 | |
| 1228 | if (intelhaddata->dp_output) |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1229 | link_rate = intelhaddata->link_rate; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1230 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1231 | had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate, |
| 1232 | n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1233 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1234 | had_prog_dip(substream, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1235 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1236 | retval = had_init_audio_ctrl(substream, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1237 | |
| 1238 | /* Prog buffer address */ |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1239 | had_init_ringbuf(substream, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1240 | |
| 1241 | /* |
| 1242 | * Program channel mapping in following order: |
| 1243 | * FL, FR, C, LFE, RL, RR |
| 1244 | */ |
| 1245 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 1246 | had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1247 | |
| 1248 | prep_end: |
| 1249 | return retval; |
| 1250 | } |
| 1251 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1252 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1253 | * ALSA PCM pointer callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1254 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1255 | static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1256 | { |
| 1257 | struct snd_intelhad *intelhaddata; |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1258 | int len; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1259 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1260 | intelhaddata = snd_pcm_substream_chip(substream); |
| 1261 | |
Takashi Iwai | 91b0cb0 | 2017-02-02 17:46:49 +0100 | [diff] [blame] | 1262 | if (!intelhaddata->connected) |
Takashi Iwai | 79f439e | 2017-01-31 16:46:44 +0100 | [diff] [blame] | 1263 | return SNDRV_PCM_POS_XRUN; |
| 1264 | |
Takashi Iwai | e1b239f3 | 2017-02-03 00:01:18 +0100 | [diff] [blame] | 1265 | len = had_process_ringbuf(substream, intelhaddata); |
| 1266 | if (len < 0) |
| 1267 | return SNDRV_PCM_POS_XRUN; |
Takashi Iwai | 8d48c01 | 2017-02-07 08:05:46 +0100 | [diff] [blame] | 1268 | len = bytes_to_frames(substream->runtime, len); |
| 1269 | /* wrapping may happen when periods=1 */ |
| 1270 | len %= substream->runtime->buffer_size; |
| 1271 | return len; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1272 | } |
| 1273 | |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1274 | /* |
Takashi Iwai | 44684f6 | 2017-02-02 17:27:40 +0100 | [diff] [blame] | 1275 | * ALSA PCM mmap callback |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1276 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1277 | static int had_pcm_mmap(struct snd_pcm_substream *substream, |
| 1278 | struct vm_area_struct *vma) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1279 | { |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1280 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 1281 | return remap_pfn_range(vma, vma->vm_start, |
| 1282 | substream->dma_buffer.addr >> PAGE_SHIFT, |
| 1283 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 1284 | } |
| 1285 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1286 | /* |
| 1287 | * ALSA PCM ops |
| 1288 | */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1289 | static const struct snd_pcm_ops had_pcm_ops = { |
| 1290 | .open = had_pcm_open, |
| 1291 | .close = had_pcm_close, |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1292 | .ioctl = snd_pcm_lib_ioctl, |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1293 | .hw_params = had_pcm_hw_params, |
| 1294 | .hw_free = had_pcm_hw_free, |
| 1295 | .prepare = had_pcm_prepare, |
| 1296 | .trigger = had_pcm_trigger, |
| 1297 | .pointer = had_pcm_pointer, |
| 1298 | .mmap = had_pcm_mmap, |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1299 | }; |
| 1300 | |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1301 | /* process mode change of the running stream; called in mutex */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1302 | static int had_process_mode_change(struct snd_intelhad *intelhaddata) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1303 | { |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1304 | struct snd_pcm_substream *substream; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1305 | int retval = 0; |
| 1306 | u32 disp_samp_freq, n_param; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1307 | u32 link_rate = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1308 | |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1309 | substream = had_substream_get(intelhaddata); |
| 1310 | if (!substream) |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1311 | return 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1312 | |
| 1313 | /* Disable Audio */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 1314 | had_enable_audio(intelhaddata, false); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1315 | |
| 1316 | /* Update CTS value */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1317 | disp_samp_freq = intelhaddata->tmds_clock_speed; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1318 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1319 | retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1320 | if (retval) { |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1321 | dev_err(intelhaddata->dev, |
| 1322 | "programming N value failed %#x\n", retval); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1323 | goto out; |
| 1324 | } |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1325 | |
| 1326 | if (intelhaddata->dp_output) |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1327 | link_rate = intelhaddata->link_rate; |
Pierre-Louis Bossart | 964ca80 | 2017-01-31 14:16:52 -0600 | [diff] [blame] | 1328 | |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1329 | had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate, |
| 1330 | n_param, intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1331 | |
| 1332 | /* Enable Audio */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 1333 | had_enable_audio(intelhaddata, true); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1334 | |
| 1335 | out: |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1336 | had_substream_put(intelhaddata); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1337 | return retval; |
| 1338 | } |
| 1339 | |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1340 | /* process hot plug, called from wq with mutex locked */ |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1341 | static void had_process_hot_plug(struct snd_intelhad *intelhaddata) |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1342 | { |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1343 | struct snd_pcm_substream *substream; |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1344 | |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1345 | spin_lock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 91b0cb0 | 2017-02-02 17:46:49 +0100 | [diff] [blame] | 1346 | if (intelhaddata->connected) { |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1347 | dev_dbg(intelhaddata->dev, "Device already connected\n"); |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1348 | spin_unlock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1349 | return; |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1350 | } |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1351 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1352 | /* Disable Audio */ |
| 1353 | had_enable_audio(intelhaddata, false); |
| 1354 | |
Takashi Iwai | 91b0cb0 | 2017-02-02 17:46:49 +0100 | [diff] [blame] | 1355 | intelhaddata->connected = true; |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1356 | dev_dbg(intelhaddata->dev, |
| 1357 | "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n", |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1358 | __func__, __LINE__); |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1359 | spin_unlock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1360 | |
Takashi Iwai | 2d42c03 | 2017-02-15 22:08:21 +0100 | [diff] [blame] | 1361 | had_build_channel_allocation_map(intelhaddata); |
| 1362 | |
| 1363 | /* Report to above ALSA layer */ |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1364 | substream = had_substream_get(intelhaddata); |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1365 | if (substream) { |
Takashi Iwai | 5def901 | 2017-02-15 21:36:38 +0100 | [diff] [blame] | 1366 | snd_pcm_stop_xrun(substream); |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1367 | had_substream_put(intelhaddata); |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 1370 | snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT); |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1373 | /* process hot unplug, called from wq with mutex locked */ |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1374 | static void had_process_hot_unplug(struct snd_intelhad *intelhaddata) |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1375 | { |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1376 | struct snd_pcm_substream *substream; |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1377 | |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1378 | spin_lock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 91b0cb0 | 2017-02-02 17:46:49 +0100 | [diff] [blame] | 1379 | if (!intelhaddata->connected) { |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1380 | dev_dbg(intelhaddata->dev, "Device already disconnected\n"); |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1381 | spin_unlock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 2d42c03 | 2017-02-15 22:08:21 +0100 | [diff] [blame] | 1382 | return; |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1383 | |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1386 | /* Disable Audio */ |
Takashi Iwai | 40ce4b5 | 2017-02-07 16:17:06 +0100 | [diff] [blame] | 1387 | had_enable_audio(intelhaddata, false); |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1388 | |
Takashi Iwai | 91b0cb0 | 2017-02-02 17:46:49 +0100 | [diff] [blame] | 1389 | intelhaddata->connected = false; |
Takashi Iwai | c75b047 | 2017-01-31 15:49:15 +0100 | [diff] [blame] | 1390 | dev_dbg(intelhaddata->dev, |
| 1391 | "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n", |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1392 | __func__, __LINE__); |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1393 | spin_unlock_irq(&intelhaddata->had_spinlock); |
Takashi Iwai | 313d9f2 | 2017-02-02 13:00:12 +0100 | [diff] [blame] | 1394 | |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1395 | kfree(intelhaddata->chmap->chmap); |
| 1396 | intelhaddata->chmap->chmap = NULL; |
Takashi Iwai | 2d42c03 | 2017-02-15 22:08:21 +0100 | [diff] [blame] | 1397 | |
| 1398 | /* Report to above ALSA layer */ |
| 1399 | substream = had_substream_get(intelhaddata); |
| 1400 | if (substream) { |
| 1401 | snd_pcm_stop_xrun(substream); |
| 1402 | had_substream_put(intelhaddata); |
| 1403 | } |
| 1404 | |
| 1405 | snd_jack_report(intelhaddata->jack, 0); |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1408 | /* |
| 1409 | * ALSA iec958 and ELD controls |
| 1410 | */ |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1411 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1412 | static int had_iec958_info(struct snd_kcontrol *kcontrol, |
| 1413 | struct snd_ctl_elem_info *uinfo) |
| 1414 | { |
| 1415 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 1416 | uinfo->count = 1; |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static int had_iec958_get(struct snd_kcontrol *kcontrol, |
| 1421 | struct snd_ctl_elem_value *ucontrol) |
| 1422 | { |
| 1423 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1424 | |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1425 | mutex_lock(&intelhaddata->mutex); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1426 | ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff; |
| 1427 | ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff; |
| 1428 | ucontrol->value.iec958.status[2] = |
| 1429 | (intelhaddata->aes_bits >> 16) & 0xff; |
| 1430 | ucontrol->value.iec958.status[3] = |
| 1431 | (intelhaddata->aes_bits >> 24) & 0xff; |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1432 | mutex_unlock(&intelhaddata->mutex); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1435 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1436 | static int had_iec958_mask_get(struct snd_kcontrol *kcontrol, |
| 1437 | struct snd_ctl_elem_value *ucontrol) |
| 1438 | { |
| 1439 | ucontrol->value.iec958.status[0] = 0xff; |
| 1440 | ucontrol->value.iec958.status[1] = 0xff; |
| 1441 | ucontrol->value.iec958.status[2] = 0xff; |
| 1442 | ucontrol->value.iec958.status[3] = 0xff; |
| 1443 | return 0; |
| 1444 | } |
Takashi Iwai | 372d855 | 2017-01-31 13:57:58 +0100 | [diff] [blame] | 1445 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1446 | static int had_iec958_put(struct snd_kcontrol *kcontrol, |
| 1447 | struct snd_ctl_elem_value *ucontrol) |
| 1448 | { |
| 1449 | unsigned int val; |
| 1450 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1451 | int changed = 0; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1452 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1453 | val = (ucontrol->value.iec958.status[0] << 0) | |
| 1454 | (ucontrol->value.iec958.status[1] << 8) | |
| 1455 | (ucontrol->value.iec958.status[2] << 16) | |
| 1456 | (ucontrol->value.iec958.status[3] << 24); |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1457 | mutex_lock(&intelhaddata->mutex); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1458 | if (intelhaddata->aes_bits != val) { |
| 1459 | intelhaddata->aes_bits = val; |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1460 | changed = 1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1461 | } |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1462 | mutex_unlock(&intelhaddata->mutex); |
| 1463 | return changed; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1464 | } |
| 1465 | |
Takashi Iwai | 4aedb94 | 2017-02-02 16:38:39 +0100 | [diff] [blame] | 1466 | static int had_ctl_eld_info(struct snd_kcontrol *kcontrol, |
| 1467 | struct snd_ctl_elem_info *uinfo) |
| 1468 | { |
| 1469 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 1470 | uinfo->count = HDMI_MAX_ELD_BYTES; |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | static int had_ctl_eld_get(struct snd_kcontrol *kcontrol, |
| 1475 | struct snd_ctl_elem_value *ucontrol) |
| 1476 | { |
| 1477 | struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol); |
| 1478 | |
| 1479 | mutex_lock(&intelhaddata->mutex); |
| 1480 | memcpy(ucontrol->value.bytes.data, intelhaddata->eld, |
| 1481 | HDMI_MAX_ELD_BYTES); |
| 1482 | mutex_unlock(&intelhaddata->mutex); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1486 | static const struct snd_kcontrol_new had_controls[] = { |
Takashi Iwai | 4aedb94 | 2017-02-02 16:38:39 +0100 | [diff] [blame] | 1487 | { |
| 1488 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 1489 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1490 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK), |
| 1491 | .info = had_iec958_info, /* shared */ |
| 1492 | .get = had_iec958_mask_get, |
| 1493 | }, |
| 1494 | { |
| 1495 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1496 | .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), |
| 1497 | .info = had_iec958_info, |
| 1498 | .get = had_iec958_get, |
| 1499 | .put = had_iec958_put, |
| 1500 | }, |
| 1501 | { |
| 1502 | .access = (SNDRV_CTL_ELEM_ACCESS_READ | |
| 1503 | SNDRV_CTL_ELEM_ACCESS_VOLATILE), |
| 1504 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 1505 | .name = "ELD", |
| 1506 | .info = had_ctl_eld_info, |
| 1507 | .get = had_ctl_eld_get, |
| 1508 | }, |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1509 | }; |
| 1510 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1511 | /* |
| 1512 | * audio interrupt handler |
| 1513 | */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1514 | static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id) |
| 1515 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1516 | struct snd_intelhad_card *card_ctx = dev_id; |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1517 | u32 audio_stat[3] = {}; |
| 1518 | int pipe, port; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1519 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1520 | for_each_pipe(card_ctx, pipe) { |
| 1521 | /* use raw register access to ack IRQs even while disconnected */ |
| 1522 | audio_stat[pipe] = had_read_register_raw(card_ctx, pipe, |
| 1523 | AUD_HDMI_STATUS) & |
| 1524 | (HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1525 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1526 | if (audio_stat[pipe]) |
| 1527 | had_write_register_raw(card_ctx, pipe, |
| 1528 | AUD_HDMI_STATUS, audio_stat[pipe]); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1529 | } |
| 1530 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1531 | for_each_port(card_ctx, port) { |
| 1532 | struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port]; |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1533 | int pipe = ctx->pipe; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1534 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1535 | if (pipe < 0) |
| 1536 | continue; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1537 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1538 | if (audio_stat[pipe] & HDMI_AUDIO_BUFFER_DONE) |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1539 | had_process_buffer_done(ctx); |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1540 | if (audio_stat[pipe] & HDMI_AUDIO_UNDERRUN) |
| 1541 | had_process_buffer_underrun(ctx); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | return IRQ_HANDLED; |
| 1545 | } |
| 1546 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1547 | /* |
| 1548 | * monitor plug/unplug notification from i915; just kick off the work |
| 1549 | */ |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1550 | static void notify_audio_lpe(struct platform_device *pdev, int port) |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1551 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1552 | struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev); |
Takashi Iwai | 7229b12 | 2018-02-20 17:33:45 +0100 | [diff] [blame] | 1553 | struct snd_intelhad *ctx; |
| 1554 | |
| 1555 | ctx = &card_ctx->pcm_ctx[single_port ? 0 : port]; |
| 1556 | if (single_port) |
| 1557 | ctx->port = port; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1558 | |
Takashi Iwai | 99b2ab9 | 2017-01-31 16:26:10 +0100 | [diff] [blame] | 1559 | schedule_work(&ctx->hdmi_audio_wq); |
| 1560 | } |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1561 | |
Takashi Iwai | 73997b0 | 2017-02-02 17:38:50 +0100 | [diff] [blame] | 1562 | /* the work to handle monitor hot plug/unplug */ |
Takashi Iwai | 99b2ab9 | 2017-01-31 16:26:10 +0100 | [diff] [blame] | 1563 | static void had_audio_wq(struct work_struct *work) |
| 1564 | { |
| 1565 | struct snd_intelhad *ctx = |
| 1566 | container_of(work, struct snd_intelhad, hdmi_audio_wq); |
| 1567 | struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data; |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1568 | struct intel_hdmi_lpe_audio_port_pdata *ppdata = &pdata->port[ctx->port]; |
Takashi Iwai | 99b2ab9 | 2017-01-31 16:26:10 +0100 | [diff] [blame] | 1569 | |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1570 | pm_runtime_get_sync(ctx->dev); |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1571 | mutex_lock(&ctx->mutex); |
Ville Syrjälä | a8562e4 | 2017-04-27 19:02:27 +0300 | [diff] [blame] | 1572 | if (ppdata->pipe < 0) { |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1573 | dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG : port = %d\n", |
| 1574 | __func__, ctx->port); |
| 1575 | |
Takashi Iwai | 4aedb94 | 2017-02-02 16:38:39 +0100 | [diff] [blame] | 1576 | memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */ |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1577 | |
| 1578 | ctx->dp_output = false; |
| 1579 | ctx->tmds_clock_speed = 0; |
| 1580 | ctx->link_rate = 0; |
| 1581 | |
| 1582 | /* Shut down the stream */ |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1583 | had_process_hot_unplug(ctx); |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1584 | |
| 1585 | ctx->pipe = -1; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1586 | } else { |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1587 | dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n", |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1588 | __func__, ctx->port, ppdata->ls_clock); |
Takashi Iwai | 0e9c67d | 2017-02-01 17:53:19 +0100 | [diff] [blame] | 1589 | |
Ville Syrjälä | a8562e4 | 2017-04-27 19:02:27 +0300 | [diff] [blame] | 1590 | memcpy(ctx->eld, ppdata->eld, sizeof(ctx->eld)); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1591 | |
Ville Syrjälä | a8562e4 | 2017-04-27 19:02:27 +0300 | [diff] [blame] | 1592 | ctx->dp_output = ppdata->dp_output; |
Ville Syrjälä | c98ec5b | 2017-04-27 19:02:24 +0300 | [diff] [blame] | 1593 | if (ctx->dp_output) { |
| 1594 | ctx->tmds_clock_speed = 0; |
Ville Syrjälä | a8562e4 | 2017-04-27 19:02:27 +0300 | [diff] [blame] | 1595 | ctx->link_rate = ppdata->ls_clock; |
Ville Syrjälä | c98ec5b | 2017-04-27 19:02:24 +0300 | [diff] [blame] | 1596 | } else { |
Ville Syrjälä | a8562e4 | 2017-04-27 19:02:27 +0300 | [diff] [blame] | 1597 | ctx->tmds_clock_speed = ppdata->ls_clock; |
Ville Syrjälä | c98ec5b | 2017-04-27 19:02:24 +0300 | [diff] [blame] | 1598 | ctx->link_rate = 0; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1599 | } |
| 1600 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1601 | /* |
| 1602 | * Shut down the stream before we change |
| 1603 | * the pipe assignment for this pcm device |
| 1604 | */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1605 | had_process_hot_plug(ctx); |
| 1606 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1607 | ctx->pipe = ppdata->pipe; |
| 1608 | |
| 1609 | /* Restart the stream if necessary */ |
Takashi Iwai | b556290 | 2017-02-04 22:05:33 +0100 | [diff] [blame] | 1610 | had_process_mode_change(ctx); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1611 | } |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1612 | |
Takashi Iwai | 8f8d1d7 | 2017-02-01 17:24:02 +0100 | [diff] [blame] | 1613 | mutex_unlock(&ctx->mutex); |
Takashi Iwai | 3002b9f | 2017-02-13 09:52:44 +0100 | [diff] [blame] | 1614 | pm_runtime_mark_last_busy(ctx->dev); |
| 1615 | pm_runtime_put_autosuspend(ctx->dev); |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 1619 | * Jack interface |
| 1620 | */ |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1621 | static int had_create_jack(struct snd_intelhad *ctx, |
| 1622 | struct snd_pcm *pcm) |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 1623 | { |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1624 | char hdmi_str[32]; |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 1625 | int err; |
| 1626 | |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1627 | snprintf(hdmi_str, sizeof(hdmi_str), |
| 1628 | "HDMI/DP,pcm=%d", pcm->device); |
| 1629 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1630 | err = snd_jack_new(ctx->card_ctx->card, hdmi_str, |
| 1631 | SND_JACK_AVOUT, &ctx->jack, |
Takashi Iwai | b9bacf2 | 2017-02-14 12:29:38 +0100 | [diff] [blame] | 1632 | true, false); |
| 1633 | if (err < 0) |
| 1634 | return err; |
| 1635 | ctx->jack->private_data = ctx; |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1640 | * PM callbacks |
| 1641 | */ |
| 1642 | |
Ville Syrjälä | 0019457 | 2018-10-24 18:48:25 +0300 | [diff] [blame] | 1643 | static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev) |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1644 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1645 | struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev); |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1646 | |
Ville Syrjälä | 0019457 | 2018-10-24 18:48:25 +0300 | [diff] [blame] | 1647 | snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D3hot); |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1648 | |
Takashi Iwai | 3002b9f | 2017-02-13 09:52:44 +0100 | [diff] [blame] | 1649 | return 0; |
| 1650 | } |
| 1651 | |
Arnd Bergmann | 1df9892 | 2017-02-07 14:38:51 +0100 | [diff] [blame] | 1652 | static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev) |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1653 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1654 | struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev); |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1655 | |
Ville Syrjälä | 0019457 | 2018-10-24 18:48:25 +0300 | [diff] [blame] | 1656 | pm_runtime_mark_last_busy(dev); |
| 1657 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1658 | snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D0); |
Ville Syrjälä | 0019457 | 2018-10-24 18:48:25 +0300 | [diff] [blame] | 1659 | |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1660 | return 0; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* release resources */ |
| 1664 | static void hdmi_lpe_audio_free(struct snd_card *card) |
| 1665 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1666 | struct snd_intelhad_card *card_ctx = card->private_data; |
| 1667 | struct intel_hdmi_lpe_audio_pdata *pdata = card_ctx->dev->platform_data; |
| 1668 | int port; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1669 | |
Ville Syrjälä | 8d5c303 | 2017-04-27 19:02:21 +0300 | [diff] [blame] | 1670 | spin_lock_irq(&pdata->lpe_audio_slock); |
| 1671 | pdata->notify_audio_lpe = NULL; |
| 1672 | spin_unlock_irq(&pdata->lpe_audio_slock); |
Takashi Iwai | 99b2ab9 | 2017-01-31 16:26:10 +0100 | [diff] [blame] | 1673 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1674 | for_each_port(card_ctx, port) { |
| 1675 | struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port]; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1676 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1677 | cancel_work_sync(&ctx->hdmi_audio_wq); |
| 1678 | } |
| 1679 | |
| 1680 | if (card_ctx->mmio_start) |
| 1681 | iounmap(card_ctx->mmio_start); |
| 1682 | if (card_ctx->irq >= 0) |
| 1683 | free_irq(card_ctx->irq, card_ctx); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* |
| 1687 | * hdmi_lpe_audio_probe - start bridge with i915 |
| 1688 | * |
| 1689 | * This function is called when the i915 driver creates the |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1690 | * hdmi-lpe-audio platform device. |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1691 | */ |
| 1692 | static int hdmi_lpe_audio_probe(struct platform_device *pdev) |
| 1693 | { |
| 1694 | struct snd_card *card; |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1695 | struct snd_intelhad_card *card_ctx; |
Takashi Iwai | c77a6ed | 2018-02-28 08:46:00 +0100 | [diff] [blame] | 1696 | struct snd_intelhad *ctx; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1697 | struct snd_pcm *pcm; |
| 1698 | struct intel_hdmi_lpe_audio_pdata *pdata; |
| 1699 | int irq; |
| 1700 | struct resource *res_mmio; |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1701 | int port, ret; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1702 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1703 | pdata = pdev->dev.platform_data; |
| 1704 | if (!pdata) { |
| 1705 | dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__); |
| 1706 | return -EINVAL; |
| 1707 | } |
| 1708 | |
| 1709 | /* get resources */ |
| 1710 | irq = platform_get_irq(pdev, 0); |
| 1711 | if (irq < 0) { |
Gustavo A. R. Silva | 1967158 | 2017-06-30 15:18:41 -0500 | [diff] [blame] | 1712 | dev_err(&pdev->dev, "Could not get irq resource: %d\n", irq); |
| 1713 | return irq; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1717 | if (!res_mmio) { |
| 1718 | dev_err(&pdev->dev, "Could not get IO_MEM resources\n"); |
| 1719 | return -ENXIO; |
| 1720 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1721 | |
Takashi Iwai | 5647aec | 2017-01-31 08:14:34 +0100 | [diff] [blame] | 1722 | /* create a card instance with ALSA framework */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1723 | ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id, |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1724 | THIS_MODULE, sizeof(*card_ctx), &card); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1725 | if (ret) |
| 1726 | return ret; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1727 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1728 | card_ctx = card->private_data; |
| 1729 | card_ctx->dev = &pdev->dev; |
| 1730 | card_ctx->card = card; |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1731 | strcpy(card->driver, INTEL_HAD); |
Takashi Iwai | 873ab03 | 2017-02-07 12:14:04 +0100 | [diff] [blame] | 1732 | strcpy(card->shortname, "Intel HDMI/DP LPE Audio"); |
| 1733 | strcpy(card->longname, "Intel HDMI/DP LPE Audio"); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1734 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1735 | card_ctx->irq = -1; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1736 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1737 | card->private_free = hdmi_lpe_audio_free; |
| 1738 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1739 | platform_set_drvdata(pdev, card_ctx); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1740 | |
Takashi Iwai | c77a6ed | 2018-02-28 08:46:00 +0100 | [diff] [blame] | 1741 | card_ctx->num_pipes = pdata->num_pipes; |
| 1742 | card_ctx->num_ports = single_port ? 1 : pdata->num_ports; |
| 1743 | |
| 1744 | for_each_port(card_ctx, port) { |
| 1745 | ctx = &card_ctx->pcm_ctx[port]; |
| 1746 | ctx->card_ctx = card_ctx; |
| 1747 | ctx->dev = card_ctx->dev; |
| 1748 | ctx->port = single_port ? -1 : port; |
| 1749 | ctx->pipe = -1; |
| 1750 | |
| 1751 | spin_lock_init(&ctx->had_spinlock); |
| 1752 | mutex_init(&ctx->mutex); |
| 1753 | INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq); |
| 1754 | } |
| 1755 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1756 | dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n", |
| 1757 | __func__, (unsigned int)res_mmio->start, |
| 1758 | (unsigned int)res_mmio->end); |
| 1759 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1760 | card_ctx->mmio_start = ioremap_nocache(res_mmio->start, |
| 1761 | (size_t)(resource_size(res_mmio))); |
| 1762 | if (!card_ctx->mmio_start) { |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1763 | dev_err(&pdev->dev, "Could not get ioremap\n"); |
| 1764 | ret = -EACCES; |
| 1765 | goto err; |
| 1766 | } |
| 1767 | |
| 1768 | /* setup interrupt handler */ |
| 1769 | ret = request_irq(irq, display_pipe_interrupt_handler, 0, |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1770 | pdev->name, card_ctx); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1771 | if (ret < 0) { |
| 1772 | dev_err(&pdev->dev, "request_irq failed\n"); |
| 1773 | goto err; |
| 1774 | } |
| 1775 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1776 | card_ctx->irq = irq; |
Takashi Iwai | 412bbe7d5 | 2017-02-02 22:03:22 +0100 | [diff] [blame] | 1777 | |
| 1778 | /* only 32bit addressable */ |
| 1779 | dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 1780 | dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 1781 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1782 | init_channel_allocations(); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1783 | |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1784 | card_ctx->num_pipes = pdata->num_pipes; |
Takashi Iwai | 7229b12 | 2018-02-20 17:33:45 +0100 | [diff] [blame] | 1785 | card_ctx->num_ports = single_port ? 1 : pdata->num_ports; |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1786 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1787 | for_each_port(card_ctx, port) { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1788 | int i; |
| 1789 | |
Takashi Iwai | c77a6ed | 2018-02-28 08:46:00 +0100 | [diff] [blame] | 1790 | ctx = &card_ctx->pcm_ctx[port]; |
Ville Syrjälä | 8a2d6ae | 2017-04-27 19:02:30 +0300 | [diff] [blame] | 1791 | ret = snd_pcm_new(card, INTEL_HAD, port, MAX_PB_STREAMS, |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1792 | MAX_CAP_STREAMS, &pcm); |
| 1793 | if (ret) |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1794 | goto err; |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1795 | |
| 1796 | /* setup private data which can be retrieved when required */ |
| 1797 | pcm->private_data = ctx; |
| 1798 | pcm->info_flags = 0; |
Takashi Iwai | c288248 | 2018-06-27 14:59:00 +0200 | [diff] [blame] | 1799 | strlcpy(pcm->name, card->shortname, strlen(card->shortname)); |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1800 | /* setup the ops for playabck */ |
| 1801 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops); |
| 1802 | |
| 1803 | /* allocate dma pages; |
| 1804 | * try to allocate 600k buffer as default which is large enough |
| 1805 | */ |
| 1806 | snd_pcm_lib_preallocate_pages_for_all(pcm, |
Takashi Iwai | c7c3fec | 2019-02-04 14:24:52 +0100 | [diff] [blame] | 1807 | SNDRV_DMA_TYPE_DEV_UC, |
| 1808 | card->dev, |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1809 | HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER); |
| 1810 | |
| 1811 | /* create controls */ |
| 1812 | for (i = 0; i < ARRAY_SIZE(had_controls); i++) { |
| 1813 | struct snd_kcontrol *kctl; |
| 1814 | |
| 1815 | kctl = snd_ctl_new1(&had_controls[i], ctx); |
| 1816 | if (!kctl) { |
| 1817 | ret = -ENOMEM; |
| 1818 | goto err; |
| 1819 | } |
| 1820 | |
| 1821 | kctl->id.device = pcm->device; |
| 1822 | |
| 1823 | ret = snd_ctl_add(card, kctl); |
| 1824 | if (ret < 0) |
| 1825 | goto err; |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1826 | } |
| 1827 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1828 | /* Register channel map controls */ |
| 1829 | ret = had_register_chmap_ctls(ctx, pcm); |
| 1830 | if (ret < 0) |
| 1831 | goto err; |
Ville Syrjälä | bb4ac5a | 2017-04-27 19:02:28 +0300 | [diff] [blame] | 1832 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1833 | ret = had_create_jack(ctx, pcm); |
Takashi Iwai | 4aedb94 | 2017-02-02 16:38:39 +0100 | [diff] [blame] | 1834 | if (ret < 0) |
| 1835 | goto err; |
| 1836 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1837 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1838 | ret = snd_card_register(card); |
| 1839 | if (ret) |
Takashi Iwai | 36ec0d9 | 2017-01-31 08:47:05 +0100 | [diff] [blame] | 1840 | goto err; |
| 1841 | |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1842 | spin_lock_irq(&pdata->lpe_audio_slock); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1843 | pdata->notify_audio_lpe = notify_audio_lpe; |
Takashi Iwai | bcce775 | 2017-02-01 17:18:20 +0100 | [diff] [blame] | 1844 | spin_unlock_irq(&pdata->lpe_audio_slock); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1845 | |
Takashi Iwai | 3002b9f | 2017-02-13 09:52:44 +0100 | [diff] [blame] | 1846 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1847 | pm_runtime_mark_last_busy(&pdev->dev); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1848 | |
Takashi Iwai | 99b2ab9 | 2017-01-31 16:26:10 +0100 | [diff] [blame] | 1849 | dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__); |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1850 | for_each_port(card_ctx, port) { |
| 1851 | struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port]; |
| 1852 | |
| 1853 | schedule_work(&ctx->hdmi_audio_wq); |
| 1854 | } |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1855 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 1856 | return 0; |
Takashi Iwai | 5647aec | 2017-01-31 08:14:34 +0100 | [diff] [blame] | 1857 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1858 | err: |
| 1859 | snd_card_free(card); |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1860 | return ret; |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1861 | } |
| 1862 | |
Takashi Iwai | 79dda75 | 2017-01-30 17:23:39 +0100 | [diff] [blame] | 1863 | /* |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1864 | * hdmi_lpe_audio_remove - stop bridge with i915 |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1865 | * |
Takashi Iwai | 2e52f5e | 2017-01-31 17:09:13 +0100 | [diff] [blame] | 1866 | * This function is called when the platform device is destroyed. |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1867 | */ |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1868 | static int hdmi_lpe_audio_remove(struct platform_device *pdev) |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1869 | { |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1870 | struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1871 | |
Ville Syrjälä | b4eb0d5 | 2017-04-27 19:02:29 +0300 | [diff] [blame] | 1872 | snd_card_free(card_ctx->card); |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1873 | return 0; |
| 1874 | } |
| 1875 | |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1876 | static const struct dev_pm_ops hdmi_lpe_audio_pm = { |
| 1877 | SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume) |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1878 | }; |
| 1879 | |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1880 | static struct platform_driver hdmi_lpe_audio_driver = { |
| 1881 | .driver = { |
| 1882 | .name = "hdmi-lpe-audio", |
Takashi Iwai | 182cdf2 | 2017-02-02 14:43:39 +0100 | [diff] [blame] | 1883 | .pm = &hdmi_lpe_audio_pm, |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1884 | }, |
| 1885 | .probe = hdmi_lpe_audio_probe, |
| 1886 | .remove = hdmi_lpe_audio_remove, |
Takashi Iwai | da86480 | 2017-01-31 13:52:22 +0100 | [diff] [blame] | 1887 | }; |
| 1888 | |
| 1889 | module_platform_driver(hdmi_lpe_audio_driver); |
| 1890 | MODULE_ALIAS("platform:hdmi_lpe_audio"); |
| 1891 | |
Jerome Anand | 5dab11d | 2017-01-25 04:27:52 +0530 | [diff] [blame] | 1892 | MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>"); |
| 1893 | MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>"); |
| 1894 | MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>"); |
| 1895 | MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>"); |
| 1896 | MODULE_DESCRIPTION("Intel HDMI Audio driver"); |
| 1897 | MODULE_LICENSE("GPL v2"); |
| 1898 | MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}"); |