at91: at91sam9g20ek modify dual slot evaluation kit
[sfrench/cifs-2.6.git] / sound / pci / hda / patch_nvhdmi.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for NVIDIA HDMI codecs
5  *
6  * Copyright (c) 2008 NVIDIA Corp.  All rights reserved.
7  * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
8  *
9  *
10  *  This driver is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This driver is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include "hda_local.h"
31
32 /* define below to restrict the supported rates and formats */
33 /* #define LIMITED_RATE_FMT_SUPPORT */
34
35 struct nvhdmi_spec {
36         struct hda_multi_out multiout;
37
38         struct hda_pcm pcm_rec;
39 };
40
41 #define Nv_VERB_SET_Channel_Allocation          0xF79
42 #define Nv_VERB_SET_Info_Frame_Checksum         0xF7A
43 #define Nv_VERB_SET_Audio_Protection_On         0xF98
44 #define Nv_VERB_SET_Audio_Protection_Off        0xF99
45
46 #define Nv_Master_Convert_nid   0x04
47 #define Nv_Master_Pin_nid       0x05
48
49 static hda_nid_t nvhdmi_convert_nids[4] = {
50         /*front, rear, clfe, rear_surr */
51         0x6, 0x8, 0xa, 0xc,
52 };
53
54 static struct hda_verb nvhdmi_basic_init[] = {
55         /* set audio protect on */
56         { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
57         /* enable digital output on pin widget */
58         { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
59         { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
60         { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
61         { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
62         { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
63         {} /* terminator */
64 };
65
66 #ifdef LIMITED_RATE_FMT_SUPPORT
67 /* support only the safe format and rate */
68 #define SUPPORTED_RATES         SNDRV_PCM_RATE_48000
69 #define SUPPORTED_MAXBPS        16
70 #define SUPPORTED_FORMATS       SNDRV_PCM_FMTBIT_S16_LE
71 #else
72 /* support all rates and formats */
73 #define SUPPORTED_RATES \
74         (SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
75         SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
76          SNDRV_PCM_RATE_192000)
77 #define SUPPORTED_MAXBPS        24
78 #define SUPPORTED_FORMATS \
79         (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
80 #endif
81
82 /*
83  * Controls
84  */
85 static int nvhdmi_build_controls(struct hda_codec *codec)
86 {
87         struct nvhdmi_spec *spec = codec->spec;
88         int err;
89
90         err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
91         if (err < 0)
92                 return err;
93
94         return 0;
95 }
96
97 static int nvhdmi_init(struct hda_codec *codec)
98 {
99         snd_hda_sequence_write(codec, nvhdmi_basic_init);
100         return 0;
101 }
102
103 /*
104  * Digital out
105  */
106 static int nvhdmi_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
107                                         struct hda_codec *codec,
108                                         struct snd_pcm_substream *substream)
109 {
110         struct nvhdmi_spec *spec = codec->spec;
111         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
112 }
113
114 static int nvhdmi_dig_playback_pcm_close_8ch(struct hda_pcm_stream *hinfo,
115                                         struct hda_codec *codec,
116                                         struct snd_pcm_substream *substream)
117 {
118         struct nvhdmi_spec *spec = codec->spec;
119         int i;
120
121         snd_hda_codec_write(codec, Nv_Master_Convert_nid,
122                         0, AC_VERB_SET_CHANNEL_STREAMID, 0);
123         for (i = 0; i < 4; i++) {
124                 /* set the stream id */
125                 snd_hda_codec_write(codec, nvhdmi_convert_nids[i], 0,
126                                 AC_VERB_SET_CHANNEL_STREAMID, 0);
127                 /* set the stream format */
128                 snd_hda_codec_write(codec, nvhdmi_convert_nids[i], 0,
129                                 AC_VERB_SET_STREAM_FORMAT, 0);
130         }
131
132         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
133 }
134
135 static int nvhdmi_dig_playback_pcm_close_2ch(struct hda_pcm_stream *hinfo,
136                                         struct hda_codec *codec,
137                                         struct snd_pcm_substream *substream)
138 {
139         struct nvhdmi_spec *spec = codec->spec;
140         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
141 }
142
143 static int nvhdmi_dig_playback_pcm_prepare_8ch(struct hda_pcm_stream *hinfo,
144                                         struct hda_codec *codec,
145                                         unsigned int stream_tag,
146                                         unsigned int format,
147                                         struct snd_pcm_substream *substream)
148 {
149         int chs;
150         unsigned int dataDCC1, dataDCC2, chan, chanmask, channel_id;
151         int i;
152
153         mutex_lock(&codec->spdif_mutex);
154
155         chs = substream->runtime->channels;
156         chan = chs ? (chs - 1) : 1;
157
158         switch (chs) {
159         default:
160         case 0:
161         case 2:
162                 chanmask = 0x00;
163                 break;
164         case 4:
165                 chanmask = 0x08;
166                 break;
167         case 6:
168                 chanmask = 0x0b;
169                 break;
170         case 8:
171                 chanmask = 0x13;
172                 break;
173         }
174         dataDCC1 = AC_DIG1_ENABLE | AC_DIG1_COPYRIGHT;
175         dataDCC2 = 0x2;
176
177         /* set the Audio InforFrame Channel Allocation */
178         snd_hda_codec_write(codec, 0x1, 0,
179                         Nv_VERB_SET_Channel_Allocation, chanmask);
180
181         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
182         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
183                 snd_hda_codec_write(codec,
184                                 Nv_Master_Convert_nid,
185                                 0,
186                                 AC_VERB_SET_DIGI_CONVERT_1,
187                                 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
188
189         /* set the stream id */
190         snd_hda_codec_write(codec, Nv_Master_Convert_nid, 0,
191                         AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
192
193         /* set the stream format */
194         snd_hda_codec_write(codec, Nv_Master_Convert_nid, 0,
195                         AC_VERB_SET_STREAM_FORMAT, format);
196
197         /* turn on again (if needed) */
198         /* enable and set the channel status audio/data flag */
199         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE)) {
200                 snd_hda_codec_write(codec,
201                                 Nv_Master_Convert_nid,
202                                 0,
203                                 AC_VERB_SET_DIGI_CONVERT_1,
204                                 codec->spdif_ctls & 0xff);
205                 snd_hda_codec_write(codec,
206                                 Nv_Master_Convert_nid,
207                                 0,
208                                 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
209         }
210
211         for (i = 0; i < 4; i++) {
212                 if (chs == 2)
213                         channel_id = 0;
214                 else
215                         channel_id = i * 2;
216
217                 /* turn off SPDIF once;
218                  *otherwise the IEC958 bits won't be updated
219                  */
220                 if (codec->spdif_status_reset &&
221                 (codec->spdif_ctls & AC_DIG1_ENABLE))
222                         snd_hda_codec_write(codec,
223                                 nvhdmi_convert_nids[i],
224                                 0,
225                                 AC_VERB_SET_DIGI_CONVERT_1,
226                                 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
227                 /* set the stream id */
228                 snd_hda_codec_write(codec,
229                                 nvhdmi_convert_nids[i],
230                                 0,
231                                 AC_VERB_SET_CHANNEL_STREAMID,
232                                 (stream_tag << 4) | channel_id);
233                 /* set the stream format */
234                 snd_hda_codec_write(codec,
235                                 nvhdmi_convert_nids[i],
236                                 0,
237                                 AC_VERB_SET_STREAM_FORMAT,
238                                 format);
239                 /* turn on again (if needed) */
240                 /* enable and set the channel status audio/data flag */
241                 if (codec->spdif_status_reset &&
242                 (codec->spdif_ctls & AC_DIG1_ENABLE)) {
243                         snd_hda_codec_write(codec,
244                                         nvhdmi_convert_nids[i],
245                                         0,
246                                         AC_VERB_SET_DIGI_CONVERT_1,
247                                         codec->spdif_ctls & 0xff);
248                         snd_hda_codec_write(codec,
249                                         nvhdmi_convert_nids[i],
250                                         0,
251                                         AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
252                 }
253         }
254
255         /* set the Audio Info Frame Checksum */
256         snd_hda_codec_write(codec, 0x1, 0,
257                         Nv_VERB_SET_Info_Frame_Checksum,
258                         (0x71 - chan - chanmask));
259
260         mutex_unlock(&codec->spdif_mutex);
261         return 0;
262 }
263
264 static int nvhdmi_dig_playback_pcm_prepare_2ch(struct hda_pcm_stream *hinfo,
265                                         struct hda_codec *codec,
266                                         unsigned int stream_tag,
267                                         unsigned int format,
268                                         struct snd_pcm_substream *substream)
269 {
270         struct nvhdmi_spec *spec = codec->spec;
271         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
272                                         format, substream);
273 }
274
275 static struct hda_pcm_stream nvhdmi_pcm_digital_playback_8ch = {
276         .substreams = 1,
277         .channels_min = 2,
278         .channels_max = 8,
279         .nid = Nv_Master_Convert_nid,
280         .rates = SUPPORTED_RATES,
281         .maxbps = SUPPORTED_MAXBPS,
282         .formats = SUPPORTED_FORMATS,
283         .ops = {
284                 .open = nvhdmi_dig_playback_pcm_open,
285                 .close = nvhdmi_dig_playback_pcm_close_8ch,
286                 .prepare = nvhdmi_dig_playback_pcm_prepare_8ch
287         },
288 };
289
290 static struct hda_pcm_stream nvhdmi_pcm_digital_playback_2ch = {
291         .substreams = 1,
292         .channels_min = 2,
293         .channels_max = 2,
294         .nid = Nv_Master_Convert_nid,
295         .rates = SUPPORTED_RATES,
296         .maxbps = SUPPORTED_MAXBPS,
297         .formats = SUPPORTED_FORMATS,
298         .ops = {
299                 .open = nvhdmi_dig_playback_pcm_open,
300                 .close = nvhdmi_dig_playback_pcm_close_2ch,
301                 .prepare = nvhdmi_dig_playback_pcm_prepare_2ch
302         },
303 };
304
305 static int nvhdmi_build_pcms_8ch(struct hda_codec *codec)
306 {
307         struct nvhdmi_spec *spec = codec->spec;
308         struct hda_pcm *info = &spec->pcm_rec;
309
310         codec->num_pcms = 1;
311         codec->pcm_info = info;
312
313         info->name = "NVIDIA HDMI";
314         info->pcm_type = HDA_PCM_TYPE_HDMI;
315         info->stream[SNDRV_PCM_STREAM_PLAYBACK]
316                                         = nvhdmi_pcm_digital_playback_8ch;
317
318         return 0;
319 }
320
321 static int nvhdmi_build_pcms_2ch(struct hda_codec *codec)
322 {
323         struct nvhdmi_spec *spec = codec->spec;
324         struct hda_pcm *info = &spec->pcm_rec;
325
326         codec->num_pcms = 1;
327         codec->pcm_info = info;
328
329         info->name = "NVIDIA HDMI";
330         info->pcm_type = HDA_PCM_TYPE_HDMI;
331         info->stream[SNDRV_PCM_STREAM_PLAYBACK]
332                                         = nvhdmi_pcm_digital_playback_2ch;
333
334         return 0;
335 }
336
337 static void nvhdmi_free(struct hda_codec *codec)
338 {
339         kfree(codec->spec);
340 }
341
342 static struct hda_codec_ops nvhdmi_patch_ops_8ch = {
343         .build_controls = nvhdmi_build_controls,
344         .build_pcms = nvhdmi_build_pcms_8ch,
345         .init = nvhdmi_init,
346         .free = nvhdmi_free,
347 };
348
349 static struct hda_codec_ops nvhdmi_patch_ops_2ch = {
350         .build_controls = nvhdmi_build_controls,
351         .build_pcms = nvhdmi_build_pcms_2ch,
352         .init = nvhdmi_init,
353         .free = nvhdmi_free,
354 };
355
356 static int patch_nvhdmi_8ch(struct hda_codec *codec)
357 {
358         struct nvhdmi_spec *spec;
359
360         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
361         if (spec == NULL)
362                 return -ENOMEM;
363
364         codec->spec = spec;
365
366         spec->multiout.num_dacs = 0;  /* no analog */
367         spec->multiout.max_channels = 8;
368         spec->multiout.dig_out_nid = Nv_Master_Convert_nid;
369
370         codec->patch_ops = nvhdmi_patch_ops_8ch;
371
372         return 0;
373 }
374
375 static int patch_nvhdmi_2ch(struct hda_codec *codec)
376 {
377         struct nvhdmi_spec *spec;
378
379         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
380         if (spec == NULL)
381                 return -ENOMEM;
382
383         codec->spec = spec;
384
385         spec->multiout.num_dacs = 0;  /* no analog */
386         spec->multiout.max_channels = 2;
387         spec->multiout.dig_out_nid = Nv_Master_Convert_nid;
388
389         codec->patch_ops = nvhdmi_patch_ops_2ch;
390
391         return 0;
392 }
393
394 /*
395  * patch entries
396  */
397 static struct hda_codec_preset snd_hda_preset_nvhdmi[] = {
398         { .id = 0x10de0002, .name = "MCP78 HDMI", .patch = patch_nvhdmi_8ch },
399         { .id = 0x10de0003, .name = "MCP78 HDMI", .patch = patch_nvhdmi_8ch },
400         { .id = 0x10de0006, .name = "MCP78 HDMI", .patch = patch_nvhdmi_8ch },
401         { .id = 0x10de0007, .name = "MCP7A HDMI", .patch = patch_nvhdmi_8ch },
402         { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
403         { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
404         {} /* terminator */
405 };
406
407 MODULE_ALIAS("snd-hda-codec-id:10de0002");
408 MODULE_ALIAS("snd-hda-codec-id:10de0003");
409 MODULE_ALIAS("snd-hda-codec-id:10de0006");
410 MODULE_ALIAS("snd-hda-codec-id:10de0007");
411 MODULE_ALIAS("snd-hda-codec-id:10de0067");
412 MODULE_ALIAS("snd-hda-codec-id:10de8001");
413
414 MODULE_LICENSE("GPL");
415 MODULE_DESCRIPTION("Nvidia HDMI HD-audio codec");
416
417 static struct hda_codec_preset_list nvhdmi_list = {
418         .preset = snd_hda_preset_nvhdmi,
419         .owner = THIS_MODULE,
420 };
421
422 static int __init patch_nvhdmi_init(void)
423 {
424         return snd_hda_add_codec_preset(&nvhdmi_list);
425 }
426
427 static void __exit patch_nvhdmi_exit(void)
428 {
429         snd_hda_delete_codec_preset(&nvhdmi_list);
430 }
431
432 module_init(patch_nvhdmi_init)
433 module_exit(patch_nvhdmi_exit)