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