2 * HD audio interface patch for Creative CA0132 chip
4 * Copyright (c) 2011, Creative Technology Ltd.
6 * Based on patch_ca0110.c
7 * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <linux/firmware.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
33 #include <linux/pci.h>
35 #include <sound/core.h>
36 #include <sound/hda_codec.h>
37 #include "hda_local.h"
38 #include "hda_auto_parser.h"
41 #include "ca0132_regs.h"
43 /* Enable this to see controls for tuning purpose. */
44 /*#define ENABLE_TUNING_CONTROLS*/
46 #ifdef ENABLE_TUNING_CONTROLS
47 #include <sound/tlv.h>
50 #define FLOAT_ZERO 0x00000000
51 #define FLOAT_ONE 0x3f800000
52 #define FLOAT_TWO 0x40000000
53 #define FLOAT_THREE 0x40400000
54 #define FLOAT_EIGHT 0x41000000
55 #define FLOAT_MINUS_5 0xc0a00000
57 #define UNSOL_TAG_DSP 0x16
59 #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
60 #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
62 #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
63 #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
64 #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
66 #define MASTERCONTROL 0x80
67 #define MASTERCONTROL_ALLOC_DMA_CHAN 10
68 #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
70 #define WIDGET_CHIP_CTRL 0x15
71 #define WIDGET_DSP_CTRL 0x16
73 #define MEM_CONNID_MICIN1 3
74 #define MEM_CONNID_MICIN2 5
75 #define MEM_CONNID_MICOUT1 12
76 #define MEM_CONNID_MICOUT2 14
77 #define MEM_CONNID_WUH 10
78 #define MEM_CONNID_DSP 16
79 #define MEM_CONNID_DMIC 100
84 #define EFX_FILE "ctefx.bin"
85 #define DESKTOP_EFX_FILE "ctefx-desktop.bin"
86 #define R3DI_EFX_FILE "ctefx-r3di.bin"
88 #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
89 MODULE_FIRMWARE(EFX_FILE);
90 MODULE_FIRMWARE(DESKTOP_EFX_FILE);
91 MODULE_FIRMWARE(R3DI_EFX_FILE);
94 static const char *const dirstr[2] = { "Playback", "Capture" };
96 #define NUM_OF_OUTPUTS 3
108 /* Strings for Input Source Enum Control */
109 static const char *const in_src_str[3] = {"Rear Mic", "Line", "Front Mic" };
110 #define IN_SRC_NUM_OF_INPUTS 3
118 #define VNODE_START_NID 0x80
119 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
126 #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
128 #define EFFECT_START_NID 0x90
129 #define OUT_EFFECT_START_NID EFFECT_START_NID
130 SURROUND = OUT_EFFECT_START_NID,
137 #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
139 #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
140 ECHO_CANCELLATION = IN_EFFECT_START_NID,
145 #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
147 VOICEFX = IN_EFFECT_END_NID,
157 AE5_HEADPHONE_GAIN_ENUM,
158 AE5_SOUND_FILTER_ENUM,
160 #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
163 /* Effects values size*/
164 #define EFFECT_VALS_MAX_COUNT 12
167 * Default values for the effect slider controls, they are in order of their
168 * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
171 static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
172 /* Amount of effect level sliders for ca0132_alt controls. */
173 #define EFFECT_LEVEL_SLIDERS 5
175 /* Latency introduced by DSP blocks in milliseconds. */
176 #define DSP_CAPTURE_INIT_LATENCY 0
177 #define DSP_CRYSTAL_VOICE_LATENCY 124
178 #define DSP_PLAYBACK_INIT_LATENCY 13
179 #define DSP_PLAY_ENHANCEMENT_LATENCY 30
180 #define DSP_SPEAKER_OUT_LATENCY 7
183 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
185 int mid; /*effect module ID*/
186 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
187 int direct; /* 0:output; 1:input*/
188 int params; /* number of default non-on/off params */
189 /*effect default values, 1st is on/off. */
190 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
193 #define EFX_DIR_OUT 0
196 static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
197 { .name = "Surround",
201 .direct = EFX_DIR_OUT,
203 .def_vals = {0x3F800000, 0x3F2B851F}
205 { .name = "Crystalizer",
209 .direct = EFX_DIR_OUT,
211 .def_vals = {0x3F800000, 0x3F266666}
213 { .name = "Dialog Plus",
217 .direct = EFX_DIR_OUT,
219 .def_vals = {0x00000000, 0x3F000000}
221 { .name = "Smart Volume",
225 .direct = EFX_DIR_OUT,
227 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
232 .reqs = {24, 23, 25},
233 .direct = EFX_DIR_OUT,
235 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
237 { .name = "Equalizer",
240 .reqs = {9, 10, 11, 12, 13, 14,
241 15, 16, 17, 18, 19, 20},
242 .direct = EFX_DIR_OUT,
244 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
245 0x00000000, 0x00000000, 0x00000000, 0x00000000,
246 0x00000000, 0x00000000, 0x00000000, 0x00000000}
248 { .name = "Echo Cancellation",
249 .nid = ECHO_CANCELLATION,
251 .reqs = {0, 1, 2, 3},
252 .direct = EFX_DIR_IN,
254 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
256 { .name = "Voice Focus",
259 .reqs = {6, 7, 8, 9},
260 .direct = EFX_DIR_IN,
262 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
268 .direct = EFX_DIR_IN,
270 .def_vals = {0x00000000, 0x3F3D70A4}
272 { .name = "Noise Reduction",
273 .nid = NOISE_REDUCTION,
276 .direct = EFX_DIR_IN,
278 .def_vals = {0x3F800000, 0x3F000000}
283 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
284 .direct = EFX_DIR_IN,
286 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
287 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
292 /* Tuning controls */
293 #ifdef ENABLE_TUNING_CONTROLS
296 #define TUNING_CTL_START_NID 0xC0
297 WEDGE_ANGLE = TUNING_CTL_START_NID,
310 #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
313 struct ct_tuning_ctl {
314 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
315 hda_nid_t parent_nid;
317 int mid; /*effect module ID*/
318 int req; /*effect module request*/
319 int direct; /* 0:output; 1:input*/
320 unsigned int def_val;/*effect default values*/
323 static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
324 { .name = "Wedge Angle",
325 .parent_nid = VOICE_FOCUS,
329 .direct = EFX_DIR_IN,
330 .def_val = 0x41F00000
332 { .name = "SVM Level",
333 .parent_nid = MIC_SVM,
337 .direct = EFX_DIR_IN,
338 .def_val = 0x3F3D70A4
340 { .name = "EQ Band0",
341 .parent_nid = EQUALIZER,
342 .nid = EQUALIZER_BAND_0,
345 .direct = EFX_DIR_OUT,
346 .def_val = 0x00000000
348 { .name = "EQ Band1",
349 .parent_nid = EQUALIZER,
350 .nid = EQUALIZER_BAND_1,
353 .direct = EFX_DIR_OUT,
354 .def_val = 0x00000000
356 { .name = "EQ Band2",
357 .parent_nid = EQUALIZER,
358 .nid = EQUALIZER_BAND_2,
361 .direct = EFX_DIR_OUT,
362 .def_val = 0x00000000
364 { .name = "EQ Band3",
365 .parent_nid = EQUALIZER,
366 .nid = EQUALIZER_BAND_3,
369 .direct = EFX_DIR_OUT,
370 .def_val = 0x00000000
372 { .name = "EQ Band4",
373 .parent_nid = EQUALIZER,
374 .nid = EQUALIZER_BAND_4,
377 .direct = EFX_DIR_OUT,
378 .def_val = 0x00000000
380 { .name = "EQ Band5",
381 .parent_nid = EQUALIZER,
382 .nid = EQUALIZER_BAND_5,
385 .direct = EFX_DIR_OUT,
386 .def_val = 0x00000000
388 { .name = "EQ Band6",
389 .parent_nid = EQUALIZER,
390 .nid = EQUALIZER_BAND_6,
393 .direct = EFX_DIR_OUT,
394 .def_val = 0x00000000
396 { .name = "EQ Band7",
397 .parent_nid = EQUALIZER,
398 .nid = EQUALIZER_BAND_7,
401 .direct = EFX_DIR_OUT,
402 .def_val = 0x00000000
404 { .name = "EQ Band8",
405 .parent_nid = EQUALIZER,
406 .nid = EQUALIZER_BAND_8,
409 .direct = EFX_DIR_OUT,
410 .def_val = 0x00000000
412 { .name = "EQ Band9",
413 .parent_nid = EQUALIZER,
414 .nid = EQUALIZER_BAND_9,
417 .direct = EFX_DIR_OUT,
418 .def_val = 0x00000000
423 /* Voice FX Presets */
424 #define VOICEFX_MAX_PARAM_COUNT 9
430 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
433 struct ct_voicefx_preset {
434 char *name; /*preset name*/
435 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
438 static const struct ct_voicefx ca0132_voicefx = {
439 .name = "VoiceFX Capture Switch",
442 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
445 static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
447 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
448 0x44FA0000, 0x3F800000, 0x3F800000,
449 0x3F800000, 0x00000000, 0x00000000 }
451 { .name = "Female2Male",
452 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
453 0x44FA0000, 0x3F19999A, 0x3F866666,
454 0x3F800000, 0x00000000, 0x00000000 }
456 { .name = "Male2Female",
457 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
458 0x450AC000, 0x4017AE14, 0x3F6B851F,
459 0x3F800000, 0x00000000, 0x00000000 }
461 { .name = "ScrappyKid",
462 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
463 0x44FA0000, 0x40400000, 0x3F28F5C3,
464 0x3F800000, 0x00000000, 0x00000000 }
467 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
468 0x44E10000, 0x3FB33333, 0x3FB9999A,
469 0x3F800000, 0x3E3A2E43, 0x00000000 }
472 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
473 0x45098000, 0x3F266666, 0x3FC00000,
474 0x3F800000, 0x00000000, 0x00000000 }
477 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
478 0x45193000, 0x3F8E147B, 0x3F75C28F,
479 0x3F800000, 0x00000000, 0x00000000 }
482 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
483 0x45007000, 0x3F451EB8, 0x3F7851EC,
484 0x3F800000, 0x00000000, 0x00000000 }
486 { .name = "AlienBrute",
487 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
488 0x451F6000, 0x3F266666, 0x3FA7D945,
489 0x3F800000, 0x3CF5C28F, 0x00000000 }
492 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
493 0x44FA0000, 0x3FB2718B, 0x3F800000,
494 0xBC07010E, 0x00000000, 0x00000000 }
497 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
498 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
499 0x3F0A3D71, 0x00000000, 0x00000000 }
502 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
503 0x44FA0000, 0x3F800000, 0x3F800000,
504 0x3E4CCCCD, 0x00000000, 0x00000000 }
506 { .name = "DeepVoice",
507 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
508 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
509 0x3F800000, 0x00000000, 0x00000000 }
511 { .name = "Munchkin",
512 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
513 0x44FA0000, 0x3F800000, 0x3F1A043C,
514 0x3F800000, 0x00000000, 0x00000000 }
518 /* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
520 #define EQ_PRESET_MAX_PARAM_COUNT 11
526 int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
529 struct ct_eq_preset {
530 char *name; /*preset name*/
531 unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
534 static const struct ct_eq ca0132_alt_eq_enum = {
535 .name = "FX: Equalizer Preset Switch",
536 .nid = EQ_PRESET_ENUM,
538 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
542 static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
544 .vals = { 0x00000000, 0x00000000, 0x00000000,
545 0x00000000, 0x00000000, 0x00000000,
546 0x00000000, 0x00000000, 0x00000000,
547 0x00000000, 0x00000000 }
549 { .name = "Acoustic",
550 .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
551 0x40000000, 0x00000000, 0x00000000,
552 0x00000000, 0x00000000, 0x40000000,
553 0x40000000, 0x40000000 }
555 { .name = "Classical",
556 .vals = { 0x00000000, 0x00000000, 0x40C00000,
557 0x40C00000, 0x40466666, 0x00000000,
558 0x00000000, 0x00000000, 0x00000000,
559 0x40466666, 0x40466666 }
562 .vals = { 0x00000000, 0xBF99999A, 0x00000000,
563 0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
564 0x00000000, 0x00000000, 0x40000000,
565 0x40466666, 0x40800000 }
568 .vals = { 0x00000000, 0xBF99999A, 0x40000000,
569 0x40466666, 0x40866666, 0xBF99999A,
570 0xBF99999A, 0x00000000, 0x00000000,
571 0x40800000, 0x40800000 }
574 .vals = { 0x00000000, 0x00000000, 0x00000000,
575 0x3F8CCCCD, 0x40800000, 0x40800000,
576 0x40800000, 0x00000000, 0x3F8CCCCD,
577 0x40466666, 0x40466666 }
580 .vals = { 0x00000000, 0x00000000, 0x40000000,
581 0x40000000, 0x00000000, 0x00000000,
582 0x00000000, 0x3F8CCCCD, 0x40000000,
583 0x40000000, 0x40000000 }
586 .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
587 0x40000000, 0x40000000, 0x00000000,
588 0xBF99999A, 0xBF99999A, 0x00000000,
589 0x40466666, 0x40C00000 }
592 .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
593 0x3F8CCCCD, 0x40000000, 0xBF99999A,
594 0xBF99999A, 0x00000000, 0x00000000,
595 0x40800000, 0x40800000 }
598 .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
599 0xBF99999A, 0x00000000, 0x40466666,
600 0x40800000, 0x40466666, 0x00000000,
601 0x00000000, 0x3F8CCCCD }
605 /* DSP command sequences for ca0132_alt_select_out */
606 #define ALT_OUT_SET_MAX_COMMANDS 9 /* Max number of commands in sequence */
607 struct ca0132_alt_out_set {
608 char *name; /*preset name*/
609 unsigned char commands;
610 unsigned int mids[ALT_OUT_SET_MAX_COMMANDS];
611 unsigned int reqs[ALT_OUT_SET_MAX_COMMANDS];
612 unsigned int vals[ALT_OUT_SET_MAX_COMMANDS];
615 static const struct ca0132_alt_out_set alt_out_presets[] = {
616 { .name = "Line Out",
618 .mids = { 0x96, 0x96, 0x96, 0x8F,
620 .reqs = { 0x19, 0x17, 0x18, 0x01,
622 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
623 0x00000000, 0x00000000, 0x00000000,
626 { .name = "Headphone",
628 .mids = { 0x96, 0x96, 0x96, 0x8F,
630 .reqs = { 0x19, 0x17, 0x18, 0x01,
632 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
633 0x00000000, 0x00000000, 0x00000000,
636 { .name = "Surround",
638 .mids = { 0x96, 0x8F, 0x96, 0x96,
639 0x96, 0x96, 0x96, 0x96 },
640 .reqs = { 0x18, 0x01, 0x1F, 0x15,
641 0x3A, 0x1A, 0x1B, 0x1C },
642 .vals = { 0x00000000, 0x00000000, 0x00000000,
643 0x00000000, 0x00000000, 0x00000000,
644 0x00000000, 0x00000000 }
649 * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
650 * and I don't know what the third req is, but it's always zero. I assume it's
651 * some sort of update or set command to tell the DSP there's new volume info.
653 #define DSP_VOL_OUT 0
656 struct ct_dsp_volume_ctl {
658 int mid; /* module ID*/
659 unsigned int reqs[3]; /* scp req ID */
662 static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
673 /* Values for ca0113_mmio_command_set for selecting output. */
674 #define AE5_CA0113_OUT_SET_COMMANDS 6
675 struct ae5_ca0113_output_set {
676 unsigned int group[AE5_CA0113_OUT_SET_COMMANDS];
677 unsigned int target[AE5_CA0113_OUT_SET_COMMANDS];
678 unsigned int vals[AE5_CA0113_OUT_SET_COMMANDS];
681 static const struct ae5_ca0113_output_set ae5_ca0113_output_presets[] = {
682 { .group = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
683 .target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
684 .vals = { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f }
686 { .group = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
687 .target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
688 .vals = { 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00 }
690 { .group = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
691 .target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
692 .vals = { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f }
696 /* ae5 ca0113 command sequences to set headphone gain levels. */
697 #define AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS 4
698 struct ae5_headphone_gain_set {
700 unsigned int vals[AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS];
703 static const struct ae5_headphone_gain_set ae5_headphone_gain_presets[] = {
704 { .name = "Low (16-31",
705 .vals = { 0xff, 0x2c, 0xf5, 0x32 }
707 { .name = "Medium (32-149",
708 .vals = { 0x38, 0xa8, 0x3e, 0x4c }
710 { .name = "High (150-600",
711 .vals = { 0xff, 0xff, 0xff, 0x7f }
715 struct ae5_filter_set {
720 static const struct ae5_filter_set ae5_filter_presets[] = {
721 { .name = "Slow Roll Off",
724 { .name = "Minimum Phase",
727 { .name = "Fast Roll Off",
732 enum hda_cmd_vendor_io {
734 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
735 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
737 VENDOR_DSPIO_STATUS = 0xF01,
738 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
739 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
740 VENDOR_DSPIO_DSP_INIT = 0x703,
741 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
742 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
744 /* for ChipIO node */
745 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
746 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
747 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
748 VENDOR_CHIPIO_DATA_LOW = 0x300,
749 VENDOR_CHIPIO_DATA_HIGH = 0x400,
751 VENDOR_CHIPIO_8051_WRITE_DIRECT = 0x500,
752 VENDOR_CHIPIO_8051_READ_DIRECT = 0xD00,
754 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
755 VENDOR_CHIPIO_STATUS = 0xF01,
756 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
757 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
759 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
760 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
761 VENDOR_CHIPIO_8051_PMEM_READ = 0xF08,
762 VENDOR_CHIPIO_8051_IRAM_WRITE = 0x709,
763 VENDOR_CHIPIO_8051_IRAM_READ = 0xF09,
765 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
766 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
768 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
769 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
770 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
771 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
772 VENDOR_CHIPIO_FLAG_SET = 0x70F,
773 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
774 VENDOR_CHIPIO_PARAM_SET = 0x710,
775 VENDOR_CHIPIO_PARAM_GET = 0xF10,
777 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
778 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
779 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
780 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
782 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
783 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
784 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
785 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
787 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
788 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
789 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
790 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
791 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
792 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
794 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
800 enum control_flag_id {
801 /* Connection manager stream setup is bypassed/enabled */
802 CONTROL_FLAG_C_MGR = 0,
803 /* DSP DMA is bypassed/enabled */
804 CONTROL_FLAG_DMA = 1,
805 /* 8051 'idle' mode is disabled/enabled */
806 CONTROL_FLAG_IDLE_ENABLE = 2,
807 /* Tracker for the SPDIF-in path is bypassed/enabled */
808 CONTROL_FLAG_TRACKER = 3,
809 /* DigitalOut to Spdif2Out connection is disabled/enabled */
810 CONTROL_FLAG_SPDIF2OUT = 4,
811 /* Digital Microphone is disabled/enabled */
812 CONTROL_FLAG_DMIC = 5,
813 /* ADC_B rate is 48 kHz/96 kHz */
814 CONTROL_FLAG_ADC_B_96KHZ = 6,
815 /* ADC_C rate is 48 kHz/96 kHz */
816 CONTROL_FLAG_ADC_C_96KHZ = 7,
817 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
818 CONTROL_FLAG_DAC_96KHZ = 8,
819 /* DSP rate is 48 kHz/96 kHz */
820 CONTROL_FLAG_DSP_96KHZ = 9,
821 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
822 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
823 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
824 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
825 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
826 CONTROL_FLAG_DECODE_LOOP = 12,
827 /* De-emphasis filter on DAC-1 disabled/enabled */
828 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
829 /* De-emphasis filter on DAC-2 disabled/enabled */
830 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
831 /* De-emphasis filter on DAC-3 disabled/enabled */
832 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
833 /* High-pass filter on ADC_B disabled/enabled */
834 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
835 /* High-pass filter on ADC_C disabled/enabled */
836 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
837 /* Common mode on Port_A disabled/enabled */
838 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
839 /* Common mode on Port_D disabled/enabled */
840 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
841 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
842 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
843 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
844 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
845 /* ASI rate is 48kHz/96kHz */
846 CONTROL_FLAG_ASI_96KHZ = 22,
847 /* DAC power settings able to control attached ports no/yes */
848 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
849 /* Clock Stop OK reporting is disabled/enabled */
850 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
851 /* Number of control flags */
852 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
856 * Control parameter IDs
858 enum control_param_id {
859 /* 0: None, 1: Mic1In*/
860 CONTROL_PARAM_VIP_SOURCE = 1,
861 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
862 CONTROL_PARAM_SPDIF1_SOURCE = 2,
863 /* Port A output stage gain setting to use when 16 Ohm output
864 * impedance is selected*/
865 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
866 /* Port D output stage gain setting to use when 16 Ohm output
867 * impedance is selected*/
868 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
871 * This control param name was found in the 8051 memory, and makes
872 * sense given the fact the AE-5 uses it and has the ASI flag set.
874 CONTROL_PARAM_ASI = 23,
878 /* Select stream with the given ID */
879 CONTROL_PARAM_STREAM_ID = 24,
880 /* Source connection point for the selected stream */
881 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
882 /* Destination connection point for the selected stream */
883 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
884 /* Number of audio channels in the selected stream */
885 CONTROL_PARAM_STREAMS_CHANNELS = 27,
886 /*Enable control for the selected stream */
887 CONTROL_PARAM_STREAM_CONTROL = 28,
889 /* Connection Point Control */
891 /* Select connection point with the given ID */
892 CONTROL_PARAM_CONN_POINT_ID = 29,
893 /* Connection point sample rate */
894 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
898 /* Select HDA node with the given ID */
899 CONTROL_PARAM_NODE_ID = 31
903 * Dsp Io Status codes
905 enum hda_vendor_status_dspio {
907 VENDOR_STATUS_DSPIO_OK = 0x00,
908 /* Busy, unable to accept new command, the host must retry */
909 VENDOR_STATUS_DSPIO_BUSY = 0x01,
910 /* SCP command queue is full */
911 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
912 /* SCP response queue is empty */
913 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
917 * Chip Io Status codes
919 enum hda_vendor_status_chipio {
921 VENDOR_STATUS_CHIPIO_OK = 0x00,
922 /* Busy, unable to accept new command, the host must retry */
923 VENDOR_STATUS_CHIPIO_BUSY = 0x01
929 enum ca0132_sample_rate {
949 SR_RATE_UNKNOWN = 0x1F
952 enum dsp_download_state {
953 DSP_DOWNLOAD_FAILED = -1,
954 DSP_DOWNLOAD_INIT = 0,
959 /* retrieve parameters from hda format */
960 #define get_hdafmt_chs(fmt) (fmt & 0xf)
961 #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
962 #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
963 #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
970 const struct snd_kcontrol_new *mixers[5];
971 unsigned int num_mixers;
972 const struct hda_verb *base_init_verbs;
973 const struct hda_verb *base_exit_verbs;
974 const struct hda_verb *chip_init_verbs;
975 const struct hda_verb *desktop_init_verbs;
976 struct hda_verb *spec_init_verbs;
977 struct auto_pin_cfg autocfg;
979 /* Nodes configurations */
980 struct hda_multi_out multiout;
981 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
982 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
983 unsigned int num_outputs;
984 hda_nid_t input_pins[AUTO_PIN_LAST];
985 hda_nid_t adcs[AUTO_PIN_LAST];
988 unsigned int num_inputs;
989 hda_nid_t shared_mic_nid;
990 hda_nid_t shared_out_nid;
991 hda_nid_t unsol_tag_hp;
992 hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
993 hda_nid_t unsol_tag_amic1;
996 struct mutex chipio_mutex; /* chip access mutex */
999 /* DSP download related */
1000 enum dsp_download_state dsp_state;
1001 unsigned int dsp_stream_id;
1002 unsigned int wait_scp;
1003 unsigned int wait_scp_header;
1004 unsigned int wait_num_data;
1005 unsigned int scp_resp_header;
1006 unsigned int scp_resp_data[4];
1007 unsigned int scp_resp_count;
1008 bool alt_firmware_present;
1009 bool startup_check_entered;
1012 /* mixer and effects related */
1013 unsigned char dmic_ctl;
1016 long vnode_lvol[VNODES_COUNT];
1017 long vnode_rvol[VNODES_COUNT];
1018 long vnode_lswitch[VNODES_COUNT];
1019 long vnode_rswitch[VNODES_COUNT];
1020 long effects_switch[EFFECTS_COUNT];
1023 /* ca0132_alt control related values */
1024 unsigned char in_enum_val;
1025 unsigned char out_enum_val;
1026 unsigned char mic_boost_enum_val;
1027 unsigned char smart_volume_setting;
1028 long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
1029 long xbass_xover_freq;
1031 unsigned int tlv[4];
1032 struct hda_vmaster_mute_hook vmaster_mute;
1033 /* AE-5 Control values */
1034 unsigned char ae5_headphone_gain_val;
1035 unsigned char ae5_filter_val;
1036 /* ZxR Control Values */
1037 unsigned char zxr_gain_set;
1039 struct hda_codec *codec;
1040 struct delayed_work unsol_hp_work;
1043 #ifdef ENABLE_TUNING_CONTROLS
1044 long cur_ctl_vals[TUNING_CTLS_COUNT];
1047 * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
1048 * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
1052 void __iomem *mem_base;
1055 * Whether or not to use the alt functions like alt_select_out,
1056 * alt_select_in, etc. Only used on desktop codecs for now, because of
1057 * surround sound support.
1059 bool use_alt_functions;
1062 * Whether or not to use alt controls: volume effect sliders, EQ
1063 * presets, smart volume presets, and new control names with FX prefix.
1064 * Renames PlayEnhancement and CrystalVoice too.
1066 bool use_alt_controls;
1070 * CA0132 quirks table
1075 QUIRK_ALIENWARE_M17XR4,
1084 static const struct hda_pintbl alienware_pincfgs[] = {
1085 { 0x0b, 0x90170110 }, /* Builtin Speaker */
1086 { 0x0c, 0x411111f0 }, /* N/A */
1087 { 0x0d, 0x411111f0 }, /* N/A */
1088 { 0x0e, 0x411111f0 }, /* N/A */
1089 { 0x0f, 0x0321101f }, /* HP */
1090 { 0x10, 0x411111f0 }, /* Headset? disabled for now */
1091 { 0x11, 0x03a11021 }, /* Mic */
1092 { 0x12, 0xd5a30140 }, /* Builtin Mic */
1093 { 0x13, 0x411111f0 }, /* N/A */
1094 { 0x18, 0x411111f0 }, /* N/A */
1098 /* Sound Blaster Z pin configs taken from Windows Driver */
1099 static const struct hda_pintbl sbz_pincfgs[] = {
1100 { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1101 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1102 { 0x0d, 0x014510f0 }, /* Digital Out */
1103 { 0x0e, 0x01c510f0 }, /* SPDIF In */
1104 { 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
1105 { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1106 { 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
1107 { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1108 { 0x13, 0x908700f0 }, /* What U Hear In*/
1109 { 0x18, 0x50d000f0 }, /* N/A */
1113 /* Sound Blaster ZxR pin configs taken from Windows Driver */
1114 static const struct hda_pintbl zxr_pincfgs[] = {
1115 { 0x0b, 0x01047110 }, /* Port G -- Lineout FRONT L/R */
1116 { 0x0c, 0x414510f0 }, /* SPDIF Out 1 - Disabled*/
1117 { 0x0d, 0x014510f0 }, /* Digital Out */
1118 { 0x0e, 0x41c520f0 }, /* SPDIF In - Disabled*/
1119 { 0x0f, 0x0122711f }, /* Port A -- BackPanel HP */
1120 { 0x10, 0x01017111 }, /* Port D -- Center/LFE */
1121 { 0x11, 0x01017114 }, /* Port B -- LineMicIn2 / Rear L/R */
1122 { 0x12, 0x01a271f0 }, /* Port C -- LineIn1 */
1123 { 0x13, 0x908700f0 }, /* What U Hear In*/
1124 { 0x18, 0x50d000f0 }, /* N/A */
1128 /* Recon3D pin configs taken from Windows Driver */
1129 static const struct hda_pintbl r3d_pincfgs[] = {
1130 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1131 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1132 { 0x0d, 0x014510f0 }, /* Digital Out */
1133 { 0x0e, 0x01c520f0 }, /* SPDIF In */
1134 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1135 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1136 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1137 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1138 { 0x13, 0x908700f0 }, /* What U Hear In*/
1139 { 0x18, 0x50d000f0 }, /* N/A */
1143 /* Sound Blaster AE-5 pin configs taken from Windows Driver */
1144 static const struct hda_pintbl ae5_pincfgs[] = {
1145 { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1146 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1147 { 0x0d, 0x014510f0 }, /* Digital Out */
1148 { 0x0e, 0x01c510f0 }, /* SPDIF In */
1149 { 0x0f, 0x01017114 }, /* Port A -- Rear L/R. */
1150 { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1151 { 0x11, 0x01a170ff }, /* Port B -- LineMicIn2 / Rear Headphone */
1152 { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1153 { 0x13, 0x908700f0 }, /* What U Hear In*/
1154 { 0x18, 0x50d000f0 }, /* N/A */
1158 /* Recon3D integrated pin configs taken from Windows Driver */
1159 static const struct hda_pintbl r3di_pincfgs[] = {
1160 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1161 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1162 { 0x0d, 0x014510f0 }, /* Digital Out */
1163 { 0x0e, 0x41c520f0 }, /* SPDIF In */
1164 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1165 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1166 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1167 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1168 { 0x13, 0x908700f0 }, /* What U Hear In*/
1169 { 0x18, 0x500000f0 }, /* N/A */
1173 static const struct snd_pci_quirk ca0132_quirks[] = {
1174 SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
1175 SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
1176 SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
1177 SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
1178 SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
1179 SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
1180 SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
1181 SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
1182 SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
1183 SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
1184 SND_PCI_QUIRK(0x1102, 0x0051, "Sound Blaster AE-5", QUIRK_AE5),
1189 * CA0132 codec access
1191 static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
1192 unsigned int verb, unsigned int parm, unsigned int *res)
1194 unsigned int response;
1195 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
1198 return ((response == -1) ? -1 : 0);
1201 static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
1202 unsigned short converter_format, unsigned int *res)
1204 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
1205 converter_format & 0xffff, res);
1208 static int codec_set_converter_stream_channel(struct hda_codec *codec,
1209 hda_nid_t nid, unsigned char stream,
1210 unsigned char channel, unsigned int *res)
1212 unsigned char converter_stream_channel = 0;
1214 converter_stream_channel = (stream << 4) | (channel & 0x0f);
1215 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
1216 converter_stream_channel, res);
1219 /* Chip access helper function */
1220 static int chipio_send(struct hda_codec *codec,
1225 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1227 /* send bits of data specified by reg */
1229 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1231 if (res == VENDOR_STATUS_CHIPIO_OK)
1234 } while (time_before(jiffies, timeout));
1240 * Write chip address through the vendor widget -- NOT protected by the Mutex!
1242 static int chipio_write_address(struct hda_codec *codec,
1243 unsigned int chip_addx)
1245 struct ca0132_spec *spec = codec->spec;
1248 if (spec->curr_chip_addx == chip_addx)
1251 /* send low 16 bits of the address */
1252 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
1253 chip_addx & 0xffff);
1256 /* send high 16 bits of the address */
1257 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
1261 spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
1267 * Write data through the vendor widget -- NOT protected by the Mutex!
1269 static int chipio_write_data(struct hda_codec *codec, unsigned int data)
1271 struct ca0132_spec *spec = codec->spec;
1274 /* send low 16 bits of the data */
1275 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
1278 /* send high 16 bits of the data */
1279 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
1283 /*If no error encountered, automatically increment the address
1284 as per chip behaviour*/
1285 spec->curr_chip_addx = (res != -EIO) ?
1286 (spec->curr_chip_addx + 4) : ~0U;
1291 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
1293 static int chipio_write_data_multiple(struct hda_codec *codec,
1300 codec_dbg(codec, "chipio_write_data null ptr\n");
1304 while ((count-- != 0) && (status == 0))
1305 status = chipio_write_data(codec, *data++);
1312 * Read data through the vendor widget -- NOT protected by the Mutex!
1314 static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
1316 struct ca0132_spec *spec = codec->spec;
1320 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
1324 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1329 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1330 VENDOR_CHIPIO_HIC_READ_DATA,
1334 /*If no error encountered, automatically increment the address
1335 as per chip behaviour*/
1336 spec->curr_chip_addx = (res != -EIO) ?
1337 (spec->curr_chip_addx + 4) : ~0U;
1342 * Write given value to the given address through the chip I/O widget.
1343 * protected by the Mutex
1345 static int chipio_write(struct hda_codec *codec,
1346 unsigned int chip_addx, const unsigned int data)
1348 struct ca0132_spec *spec = codec->spec;
1351 mutex_lock(&spec->chipio_mutex);
1353 /* write the address, and if successful proceed to write data */
1354 err = chipio_write_address(codec, chip_addx);
1358 err = chipio_write_data(codec, data);
1363 mutex_unlock(&spec->chipio_mutex);
1368 * Write given value to the given address through the chip I/O widget.
1369 * not protected by the Mutex
1371 static int chipio_write_no_mutex(struct hda_codec *codec,
1372 unsigned int chip_addx, const unsigned int data)
1377 /* write the address, and if successful proceed to write data */
1378 err = chipio_write_address(codec, chip_addx);
1382 err = chipio_write_data(codec, data);
1391 * Write multiple values to the given address through the chip I/O widget.
1392 * protected by the Mutex
1394 static int chipio_write_multiple(struct hda_codec *codec,
1399 struct ca0132_spec *spec = codec->spec;
1402 mutex_lock(&spec->chipio_mutex);
1403 status = chipio_write_address(codec, chip_addx);
1407 status = chipio_write_data_multiple(codec, data, count);
1409 mutex_unlock(&spec->chipio_mutex);
1415 * Read the given address through the chip I/O widget
1416 * protected by the Mutex
1418 static int chipio_read(struct hda_codec *codec,
1419 unsigned int chip_addx, unsigned int *data)
1421 struct ca0132_spec *spec = codec->spec;
1424 mutex_lock(&spec->chipio_mutex);
1426 /* write the address, and if successful proceed to write data */
1427 err = chipio_write_address(codec, chip_addx);
1431 err = chipio_read_data(codec, data);
1436 mutex_unlock(&spec->chipio_mutex);
1441 * Set chip control flags through the chip I/O widget.
1443 static void chipio_set_control_flag(struct hda_codec *codec,
1444 enum control_flag_id flag_id,
1448 unsigned int flag_bit;
1450 flag_bit = (flag_state ? 1 : 0);
1451 val = (flag_bit << 7) | (flag_id);
1452 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1453 VENDOR_CHIPIO_FLAG_SET, val);
1457 * Set chip parameters through the chip I/O widget.
1459 static void chipio_set_control_param(struct hda_codec *codec,
1460 enum control_param_id param_id, int param_val)
1462 struct ca0132_spec *spec = codec->spec;
1465 if ((param_id < 32) && (param_val < 8)) {
1466 val = (param_val << 5) | (param_id);
1467 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1468 VENDOR_CHIPIO_PARAM_SET, val);
1470 mutex_lock(&spec->chipio_mutex);
1471 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1472 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1473 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1475 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1476 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1479 mutex_unlock(&spec->chipio_mutex);
1484 * Set chip parameters through the chip I/O widget. NO MUTEX.
1486 static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
1487 enum control_param_id param_id, int param_val)
1491 if ((param_id < 32) && (param_val < 8)) {
1492 val = (param_val << 5) | (param_id);
1493 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1494 VENDOR_CHIPIO_PARAM_SET, val);
1496 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1497 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1498 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1500 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1501 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1507 * Connect stream to a source point, and then connect
1508 * that source point to a destination point.
1510 static void chipio_set_stream_source_dest(struct hda_codec *codec,
1511 int streamid, int source_point, int dest_point)
1513 chipio_set_control_param_no_mutex(codec,
1514 CONTROL_PARAM_STREAM_ID, streamid);
1515 chipio_set_control_param_no_mutex(codec,
1516 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
1517 chipio_set_control_param_no_mutex(codec,
1518 CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
1522 * Set number of channels in the selected stream.
1524 static void chipio_set_stream_channels(struct hda_codec *codec,
1525 int streamid, unsigned int channels)
1527 chipio_set_control_param_no_mutex(codec,
1528 CONTROL_PARAM_STREAM_ID, streamid);
1529 chipio_set_control_param_no_mutex(codec,
1530 CONTROL_PARAM_STREAMS_CHANNELS, channels);
1534 * Enable/Disable audio stream.
1536 static void chipio_set_stream_control(struct hda_codec *codec,
1537 int streamid, int enable)
1539 chipio_set_control_param_no_mutex(codec,
1540 CONTROL_PARAM_STREAM_ID, streamid);
1541 chipio_set_control_param_no_mutex(codec,
1542 CONTROL_PARAM_STREAM_CONTROL, enable);
1547 * Set sampling rate of the connection point. NO MUTEX.
1549 static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
1550 int connid, enum ca0132_sample_rate rate)
1552 chipio_set_control_param_no_mutex(codec,
1553 CONTROL_PARAM_CONN_POINT_ID, connid);
1554 chipio_set_control_param_no_mutex(codec,
1555 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
1559 * Set sampling rate of the connection point.
1561 static void chipio_set_conn_rate(struct hda_codec *codec,
1562 int connid, enum ca0132_sample_rate rate)
1564 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1565 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1570 * Writes to the 8051's internal address space directly instead of indirectly,
1571 * giving access to the special function registers located at addresses
1574 static void chipio_8051_write_direct(struct hda_codec *codec,
1575 unsigned int addr, unsigned int data)
1579 verb = VENDOR_CHIPIO_8051_WRITE_DIRECT | data;
1580 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, verb, addr);
1586 static void chipio_enable_clocks(struct hda_codec *codec)
1588 struct ca0132_spec *spec = codec->spec;
1590 mutex_lock(&spec->chipio_mutex);
1591 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1592 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1593 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1594 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1595 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1596 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1597 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1598 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1599 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1600 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1601 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1602 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1603 mutex_unlock(&spec->chipio_mutex);
1607 * CA0132 DSP IO stuffs
1609 static int dspio_send(struct hda_codec *codec, unsigned int reg,
1613 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1615 /* send bits of data specified by reg to dsp */
1617 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1618 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1621 } while (time_before(jiffies, timeout));
1627 * Wait for DSP to be ready for commands
1629 static void dspio_write_wait(struct hda_codec *codec)
1632 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1635 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1636 VENDOR_DSPIO_STATUS, 0);
1637 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1638 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1641 } while (time_before(jiffies, timeout));
1645 * Write SCP data to DSP
1647 static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1649 struct ca0132_spec *spec = codec->spec;
1652 dspio_write_wait(codec);
1654 mutex_lock(&spec->chipio_mutex);
1655 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1660 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1665 /* OK, now check if the write itself has executed*/
1666 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1667 VENDOR_DSPIO_STATUS, 0);
1669 mutex_unlock(&spec->chipio_mutex);
1671 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1676 * Write multiple SCP data to DSP
1678 static int dspio_write_multiple(struct hda_codec *codec,
1679 unsigned int *buffer, unsigned int size)
1688 while (count < size) {
1689 status = dspio_write(codec, *buffer++);
1698 static int dspio_read(struct hda_codec *codec, unsigned int *data)
1702 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1706 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1707 if (status == -EIO ||
1708 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1711 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1712 VENDOR_DSPIO_SCP_READ_DATA, 0);
1717 static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1718 unsigned int *buf_size, unsigned int size_count)
1721 unsigned int size = *buf_size;
1723 unsigned int skip_count;
1730 while (count < size && count < size_count) {
1731 status = dspio_read(codec, buffer++);
1739 while (skip_count < size) {
1740 status = dspio_read(codec, &dummy);
1752 * Construct the SCP header using corresponding fields
1754 static inline unsigned int
1755 make_scp_header(unsigned int target_id, unsigned int source_id,
1756 unsigned int get_flag, unsigned int req,
1757 unsigned int device_flag, unsigned int resp_flag,
1758 unsigned int error_flag, unsigned int data_size)
1760 unsigned int header = 0;
1762 header = (data_size & 0x1f) << 27;
1763 header |= (error_flag & 0x01) << 26;
1764 header |= (resp_flag & 0x01) << 25;
1765 header |= (device_flag & 0x01) << 24;
1766 header |= (req & 0x7f) << 17;
1767 header |= (get_flag & 0x01) << 16;
1768 header |= (source_id & 0xff) << 8;
1769 header |= target_id & 0xff;
1775 * Extract corresponding fields from SCP header
1778 extract_scp_header(unsigned int header,
1779 unsigned int *target_id, unsigned int *source_id,
1780 unsigned int *get_flag, unsigned int *req,
1781 unsigned int *device_flag, unsigned int *resp_flag,
1782 unsigned int *error_flag, unsigned int *data_size)
1785 *data_size = (header >> 27) & 0x1f;
1787 *error_flag = (header >> 26) & 0x01;
1789 *resp_flag = (header >> 25) & 0x01;
1791 *device_flag = (header >> 24) & 0x01;
1793 *req = (header >> 17) & 0x7f;
1795 *get_flag = (header >> 16) & 0x01;
1797 *source_id = (header >> 8) & 0xff;
1799 *target_id = header & 0xff;
1802 #define SCP_MAX_DATA_WORDS (16)
1804 /* Structure to contain any SCP message */
1807 unsigned int data[SCP_MAX_DATA_WORDS];
1810 static void dspio_clear_response_queue(struct hda_codec *codec)
1812 unsigned int dummy = 0;
1815 /* clear all from the response queue */
1817 status = dspio_read(codec, &dummy);
1818 } while (status == 0);
1821 static int dspio_get_response_data(struct hda_codec *codec)
1823 struct ca0132_spec *spec = codec->spec;
1824 unsigned int data = 0;
1827 if (dspio_read(codec, &data) < 0)
1830 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1831 spec->scp_resp_header = data;
1832 spec->scp_resp_count = data >> 27;
1833 count = spec->wait_num_data;
1834 dspio_read_multiple(codec, spec->scp_resp_data,
1835 &spec->scp_resp_count, count);
1843 * Send SCP message to DSP
1845 static int dspio_send_scp_message(struct hda_codec *codec,
1846 unsigned char *send_buf,
1847 unsigned int send_buf_size,
1848 unsigned char *return_buf,
1849 unsigned int return_buf_size,
1850 unsigned int *bytes_returned)
1852 struct ca0132_spec *spec = codec->spec;
1854 unsigned int scp_send_size = 0;
1855 unsigned int total_size;
1856 bool waiting_for_resp = false;
1857 unsigned int header;
1858 struct scp_msg *ret_msg;
1859 unsigned int resp_src_id, resp_target_id;
1860 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1863 *bytes_returned = 0;
1865 /* get scp header from buffer */
1866 header = *((unsigned int *)send_buf);
1867 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1868 &device_flag, NULL, NULL, &data_size);
1869 scp_send_size = data_size + 1;
1870 total_size = (scp_send_size * 4);
1872 if (send_buf_size < total_size)
1875 if (get_flag || device_flag) {
1876 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1879 spec->wait_scp_header = *((unsigned int *)send_buf);
1881 /* swap source id with target id */
1882 resp_target_id = src_id;
1883 resp_src_id = target_id;
1884 spec->wait_scp_header &= 0xffff0000;
1885 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1886 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1888 waiting_for_resp = true;
1891 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1898 if (waiting_for_resp) {
1899 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1900 memset(return_buf, 0, return_buf_size);
1903 } while (spec->wait_scp && time_before(jiffies, timeout));
1904 waiting_for_resp = false;
1905 if (!spec->wait_scp) {
1906 ret_msg = (struct scp_msg *)return_buf;
1907 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1908 memcpy(&ret_msg->data, spec->scp_resp_data,
1909 spec->wait_num_data);
1910 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1922 * Prepare and send the SCP message to DSP
1923 * @codec: the HDA codec
1924 * @mod_id: ID of the DSP module to send the command
1925 * @req: ID of request to send to the DSP module
1927 * @data: pointer to the data to send with the request, request specific
1928 * @len: length of the data, in bytes
1929 * @reply: point to the buffer to hold data returned for a reply
1930 * @reply_len: length of the reply buffer returned from GET
1932 * Returns zero or a negative error code.
1934 static int dspio_scp(struct hda_codec *codec,
1935 int mod_id, int src_id, int req, int dir, const void *data,
1936 unsigned int len, void *reply, unsigned int *reply_len)
1939 struct scp_msg scp_send, scp_reply;
1940 unsigned int ret_bytes, send_size, ret_size;
1941 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1942 unsigned int reply_data_size;
1944 memset(&scp_send, 0, sizeof(scp_send));
1945 memset(&scp_reply, 0, sizeof(scp_reply));
1947 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1950 if (dir == SCP_GET && reply == NULL) {
1951 codec_dbg(codec, "dspio_scp get but has no buffer\n");
1955 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1956 codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
1960 scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
1961 0, 0, 0, len/sizeof(unsigned int));
1962 if (data != NULL && len > 0) {
1963 len = min((unsigned int)(sizeof(scp_send.data)), len);
1964 memcpy(scp_send.data, data, len);
1968 send_size = sizeof(unsigned int) + len;
1969 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1970 send_size, (unsigned char *)&scp_reply,
1971 sizeof(scp_reply), &ret_bytes);
1974 codec_dbg(codec, "dspio_scp: send scp msg failed\n");
1978 /* extract send and reply headers members */
1979 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1980 NULL, NULL, NULL, NULL, NULL);
1981 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1982 &reply_resp_flag, &reply_error_flag,
1988 if (reply_resp_flag && !reply_error_flag) {
1989 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1990 / sizeof(unsigned int);
1992 if (*reply_len < ret_size*sizeof(unsigned int)) {
1993 codec_dbg(codec, "reply too long for buf\n");
1995 } else if (ret_size != reply_data_size) {
1996 codec_dbg(codec, "RetLen and HdrLen .NE.\n");
1998 } else if (!reply) {
1999 codec_dbg(codec, "NULL reply\n");
2002 *reply_len = ret_size*sizeof(unsigned int);
2003 memcpy(reply, scp_reply.data, *reply_len);
2006 codec_dbg(codec, "reply ill-formed or errflag set\n");
2014 * Set DSP parameters
2016 static int dspio_set_param(struct hda_codec *codec, int mod_id,
2017 int src_id, int req, const void *data, unsigned int len)
2019 return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
2023 static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
2024 int req, const unsigned int data)
2026 return dspio_set_param(codec, mod_id, 0x20, req, &data,
2027 sizeof(unsigned int));
2030 static int dspio_set_uint_param_no_source(struct hda_codec *codec, int mod_id,
2031 int req, const unsigned int data)
2033 return dspio_set_param(codec, mod_id, 0x00, req, &data,
2034 sizeof(unsigned int));
2038 * Allocate a DSP DMA channel via an SCP message
2040 static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
2043 unsigned int size = sizeof(dma_chan);
2045 codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
2046 status = dspio_scp(codec, MASTERCONTROL, 0x20,
2047 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
2051 codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
2055 if ((*dma_chan + 1) == 0) {
2056 codec_dbg(codec, "no free dma channels to allocate\n");
2060 codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
2061 codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
2067 * Free a DSP DMA via an SCP message
2069 static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
2072 unsigned int dummy = 0;
2074 codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
2075 codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
2077 status = dspio_scp(codec, MASTERCONTROL, 0x20,
2078 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
2079 sizeof(dma_chan), NULL, &dummy);
2082 codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
2086 codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
2094 static int dsp_set_run_state(struct hda_codec *codec)
2096 unsigned int dbg_ctrl_reg;
2097 unsigned int halt_state;
2100 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
2104 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
2105 DSP_DBGCNTL_STATE_LOBIT;
2107 if (halt_state != 0) {
2108 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
2109 DSP_DBGCNTL_SS_MASK);
2110 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2115 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
2116 DSP_DBGCNTL_EXEC_MASK;
2117 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2129 static int dsp_reset(struct hda_codec *codec)
2134 codec_dbg(codec, "dsp_reset\n");
2136 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
2138 } while (res == -EIO && retry);
2141 codec_dbg(codec, "dsp_reset timeout\n");
2149 * Convert chip address to DSP address
2151 static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
2152 bool *code, bool *yram)
2154 *code = *yram = false;
2156 if (UC_RANGE(chip_addx, 1)) {
2158 return UC_OFF(chip_addx);
2159 } else if (X_RANGE_ALL(chip_addx, 1)) {
2160 return X_OFF(chip_addx);
2161 } else if (Y_RANGE_ALL(chip_addx, 1)) {
2163 return Y_OFF(chip_addx);
2166 return INVALID_CHIP_ADDRESS;
2170 * Check if the DSP DMA is active
2172 static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
2174 unsigned int dma_chnlstart_reg;
2176 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
2178 return ((dma_chnlstart_reg & (1 <<
2179 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
2182 static int dsp_dma_setup_common(struct hda_codec *codec,
2183 unsigned int chip_addx,
2184 unsigned int dma_chan,
2185 unsigned int port_map_mask,
2189 unsigned int chnl_prop;
2190 unsigned int dsp_addx;
2191 unsigned int active;
2194 codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
2196 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
2197 codec_dbg(codec, "dma chan num invalid\n");
2201 if (dsp_is_dma_active(codec, dma_chan)) {
2202 codec_dbg(codec, "dma already active\n");
2206 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2208 if (dsp_addx == INVALID_CHIP_ADDRESS) {
2209 codec_dbg(codec, "invalid chip addr\n");
2213 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
2216 codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
2219 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
2223 codec_dbg(codec, "read CHNLPROP Reg fail\n");
2226 codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
2230 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2232 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2234 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
2236 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
2238 codec_dbg(codec, "write CHNLPROP Reg fail\n");
2241 codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
2244 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
2248 codec_dbg(codec, "read ACTIVE Reg fail\n");
2251 codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
2254 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
2255 DSPDMAC_ACTIVE_AAR_MASK;
2257 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
2259 codec_dbg(codec, "write ACTIVE Reg fail\n");
2263 codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
2265 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
2268 codec_dbg(codec, "write AUDCHSEL Reg fail\n");
2271 codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
2273 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
2274 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
2276 codec_dbg(codec, "write IRQCNT Reg fail\n");
2279 codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
2282 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
2283 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
2284 chip_addx, dsp_addx, dma_chan,
2285 port_map_mask, chnl_prop, active);
2287 codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
2293 * Setup the DSP DMA per-transfer-specific registers
2295 static int dsp_dma_setup(struct hda_codec *codec,
2296 unsigned int chip_addx,
2298 unsigned int dma_chan)
2302 unsigned int dsp_addx;
2303 unsigned int addr_field;
2304 unsigned int incr_field;
2305 unsigned int base_cnt;
2306 unsigned int cur_cnt;
2307 unsigned int dma_cfg = 0;
2308 unsigned int adr_ofs = 0;
2309 unsigned int xfr_cnt = 0;
2310 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
2311 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
2313 codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
2315 if (count > max_dma_count) {
2316 codec_dbg(codec, "count too big\n");
2320 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2321 if (dsp_addx == INVALID_CHIP_ADDRESS) {
2322 codec_dbg(codec, "invalid chip addr\n");
2326 codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
2328 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
2334 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
2336 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
2339 dma_cfg = addr_field + incr_field;
2340 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
2343 codec_dbg(codec, "write DMACFG Reg fail\n");
2346 codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
2348 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
2351 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
2354 codec_dbg(codec, "write DSPADROFS Reg fail\n");
2357 codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
2359 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
2361 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
2363 xfr_cnt = base_cnt | cur_cnt;
2365 status = chipio_write(codec,
2366 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
2368 codec_dbg(codec, "write XFRCNT Reg fail\n");
2371 codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
2374 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
2375 "ADROFS=0x%x, XFRCNT=0x%x\n",
2376 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
2378 codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
2386 static int dsp_dma_start(struct hda_codec *codec,
2387 unsigned int dma_chan, bool ovly)
2389 unsigned int reg = 0;
2392 codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
2395 status = chipio_read(codec,
2396 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
2399 codec_dbg(codec, "read CHNLSTART reg fail\n");
2402 codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
2404 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2405 DSPDMAC_CHNLSTART_DIS_MASK);
2408 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2409 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
2411 codec_dbg(codec, "write CHNLSTART reg fail\n");
2414 codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
2422 static int dsp_dma_stop(struct hda_codec *codec,
2423 unsigned int dma_chan, bool ovly)
2425 unsigned int reg = 0;
2428 codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
2431 status = chipio_read(codec,
2432 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
2435 codec_dbg(codec, "read CHNLSTART reg fail\n");
2438 codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
2439 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2440 DSPDMAC_CHNLSTART_DIS_MASK);
2443 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2444 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
2446 codec_dbg(codec, "write CHNLSTART reg fail\n");
2449 codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
2455 * Allocate router ports
2457 * @codec: the HDA codec
2458 * @num_chans: number of channels in the stream
2459 * @ports_per_channel: number of ports per channel
2460 * @start_device: start device
2461 * @port_map: pointer to the port list to hold the allocated ports
2463 * Returns zero or a negative error code.
2465 static int dsp_allocate_router_ports(struct hda_codec *codec,
2466 unsigned int num_chans,
2467 unsigned int ports_per_channel,
2468 unsigned int start_device,
2469 unsigned int *port_map)
2475 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2479 val = start_device << 6;
2480 val |= (ports_per_channel - 1) << 4;
2481 val |= num_chans - 1;
2483 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2484 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
2487 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2488 VENDOR_CHIPIO_PORT_ALLOC_SET,
2491 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2495 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
2496 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
2500 return (res < 0) ? res : 0;
2506 static int dsp_free_router_ports(struct hda_codec *codec)
2510 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2514 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2515 VENDOR_CHIPIO_PORT_FREE_SET,
2518 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2524 * Allocate DSP ports for the download stream
2526 static int dsp_allocate_ports(struct hda_codec *codec,
2527 unsigned int num_chans,
2528 unsigned int rate_multi, unsigned int *port_map)
2532 codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
2534 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2535 codec_dbg(codec, "bad rate multiple\n");
2539 status = dsp_allocate_router_ports(codec, num_chans,
2540 rate_multi, 0, port_map);
2542 codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
2547 static int dsp_allocate_ports_format(struct hda_codec *codec,
2548 const unsigned short fmt,
2549 unsigned int *port_map)
2552 unsigned int num_chans;
2554 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2555 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2556 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2558 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2559 codec_dbg(codec, "bad rate multiple\n");
2563 num_chans = get_hdafmt_chs(fmt) + 1;
2565 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2573 static int dsp_free_ports(struct hda_codec *codec)
2577 codec_dbg(codec, " dsp_free_ports() -- begin\n");
2579 status = dsp_free_router_ports(codec);
2581 codec_dbg(codec, "free router ports fail\n");
2584 codec_dbg(codec, " dsp_free_ports() -- complete\n");
2590 * HDA DMA engine stuffs for DSP code download
2593 struct hda_codec *codec;
2594 unsigned short m_converter_format;
2595 struct snd_dma_buffer *dmab;
2596 unsigned int buf_size;
2605 static int dma_convert_to_hda_format(struct hda_codec *codec,
2606 unsigned int sample_rate,
2607 unsigned short channels,
2608 unsigned short *hda_format)
2610 unsigned int format_val;
2612 format_val = snd_hdac_calc_stream_format(sample_rate,
2613 channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
2616 *hda_format = (unsigned short)format_val;
2622 * Reset DMA for DSP download
2624 static int dma_reset(struct dma_engine *dma)
2626 struct hda_codec *codec = dma->codec;
2627 struct ca0132_spec *spec = codec->spec;
2630 if (dma->dmab->area)
2631 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2633 status = snd_hda_codec_load_dsp_prepare(codec,
2634 dma->m_converter_format,
2639 spec->dsp_stream_id = status;
2643 static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2648 case DMA_STATE_STOP:
2658 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2662 static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2664 return dma->dmab->bytes;
2667 static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2669 return dma->dmab->area;
2672 static int dma_xfer(struct dma_engine *dma,
2673 const unsigned int *data,
2676 memcpy(dma->dmab->area, data, count);
2680 static void dma_get_converter_format(
2681 struct dma_engine *dma,
2682 unsigned short *format)
2685 *format = dma->m_converter_format;
2688 static unsigned int dma_get_stream_id(struct dma_engine *dma)
2690 struct ca0132_spec *spec = dma->codec->spec;
2692 return spec->dsp_stream_id;
2695 struct dsp_image_seg {
2702 static const u32 g_magic_value = 0x4c46584d;
2703 static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2705 static bool is_valid(const struct dsp_image_seg *p)
2707 return p->magic == g_magic_value;
2710 static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2712 return g_chip_addr_magic_value == p->chip_addr;
2715 static bool is_last(const struct dsp_image_seg *p)
2717 return p->count == 0;
2720 static size_t dsp_sizeof(const struct dsp_image_seg *p)
2722 return sizeof(*p) + p->count*sizeof(u32);
2725 static const struct dsp_image_seg *get_next_seg_ptr(
2726 const struct dsp_image_seg *p)
2728 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2732 * CA0132 chip DSP transfer stuffs. For DSP download.
2734 #define INVALID_DMA_CHANNEL (~0U)
2737 * Program a list of address/data pairs via the ChipIO widget.
2738 * The segment data is in the format of successive pairs of words.
2739 * These are repeated as indicated by the segment's count field.
2741 static int dspxfr_hci_write(struct hda_codec *codec,
2742 const struct dsp_image_seg *fls)
2748 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2749 codec_dbg(codec, "hci_write invalid params\n");
2754 data = (u32 *)(fls->data);
2755 while (count >= 2) {
2756 status = chipio_write(codec, data[0], data[1]);
2758 codec_dbg(codec, "hci_write chipio failed\n");
2768 * Write a block of data into DSP code or data RAM using pre-allocated
2771 * @codec: the HDA codec
2772 * @fls: pointer to a fast load image
2773 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2775 * @dma_engine: pointer to DMA engine to be used for DSP download
2776 * @dma_chan: The number of DMA channels used for DSP download
2777 * @port_map_mask: port mapping
2778 * @ovly: TRUE if overlay format is required
2780 * Returns zero or a negative error code.
2782 static int dspxfr_one_seg(struct hda_codec *codec,
2783 const struct dsp_image_seg *fls,
2785 struct dma_engine *dma_engine,
2786 unsigned int dma_chan,
2787 unsigned int port_map_mask,
2791 bool comm_dma_setup_done = false;
2792 const unsigned int *data;
2793 unsigned int chip_addx;
2794 unsigned int words_to_write;
2795 unsigned int buffer_size_words;
2796 unsigned char *buffer_addx;
2797 unsigned short hda_format;
2798 unsigned int sample_rate_div;
2799 unsigned int sample_rate_mul;
2800 unsigned int num_chans;
2801 unsigned int hda_frame_size_words;
2802 unsigned int remainder_words;
2803 const u32 *data_remainder;
2804 u32 chip_addx_remainder;
2805 unsigned int run_size_words;
2806 const struct dsp_image_seg *hci_write = NULL;
2807 unsigned long timeout;
2812 if (is_hci_prog_list_seg(fls)) {
2814 fls = get_next_seg_ptr(fls);
2817 if (hci_write && (!fls || is_last(fls))) {
2818 codec_dbg(codec, "hci_write\n");
2819 return dspxfr_hci_write(codec, hci_write);
2822 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2823 codec_dbg(codec, "Invalid Params\n");
2828 chip_addx = fls->chip_addr,
2829 words_to_write = fls->count;
2831 if (!words_to_write)
2832 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2834 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2836 if (!UC_RANGE(chip_addx, words_to_write) &&
2837 !X_RANGE_ALL(chip_addx, words_to_write) &&
2838 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2839 codec_dbg(codec, "Invalid chip_addx Params\n");
2843 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2846 buffer_addx = dma_get_buffer_addr(dma_engine);
2848 if (buffer_addx == NULL) {
2849 codec_dbg(codec, "dma_engine buffer NULL\n");
2853 dma_get_converter_format(dma_engine, &hda_format);
2854 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2855 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2856 num_chans = get_hdafmt_chs(hda_format) + 1;
2858 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2859 (num_chans * sample_rate_mul / sample_rate_div));
2861 if (hda_frame_size_words == 0) {
2862 codec_dbg(codec, "frmsz zero\n");
2866 buffer_size_words = min(buffer_size_words,
2867 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2869 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2871 "chpadr=0x%08x frmsz=%u nchan=%u "
2872 "rate_mul=%u div=%u bufsz=%u\n",
2873 chip_addx, hda_frame_size_words, num_chans,
2874 sample_rate_mul, sample_rate_div, buffer_size_words);
2876 if (buffer_size_words < hda_frame_size_words) {
2877 codec_dbg(codec, "dspxfr_one_seg:failed\n");
2881 remainder_words = words_to_write % hda_frame_size_words;
2882 data_remainder = data;
2883 chip_addx_remainder = chip_addx;
2885 data += remainder_words;
2886 chip_addx += remainder_words*sizeof(u32);
2887 words_to_write -= remainder_words;
2889 while (words_to_write != 0) {
2890 run_size_words = min(buffer_size_words, words_to_write);
2891 codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2892 words_to_write, run_size_words, remainder_words);
2893 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2894 if (!comm_dma_setup_done) {
2895 status = dsp_dma_stop(codec, dma_chan, ovly);
2898 status = dsp_dma_setup_common(codec, chip_addx,
2899 dma_chan, port_map_mask, ovly);
2902 comm_dma_setup_done = true;
2905 status = dsp_dma_setup(codec, chip_addx,
2906 run_size_words, dma_chan);
2909 status = dsp_dma_start(codec, dma_chan, ovly);
2912 if (!dsp_is_dma_active(codec, dma_chan)) {
2913 codec_dbg(codec, "dspxfr:DMA did not start\n");
2916 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2919 if (remainder_words != 0) {
2920 status = chipio_write_multiple(codec,
2921 chip_addx_remainder,
2926 remainder_words = 0;
2929 status = dspxfr_hci_write(codec, hci_write);
2935 timeout = jiffies + msecs_to_jiffies(2000);
2937 dma_active = dsp_is_dma_active(codec, dma_chan);
2941 } while (time_before(jiffies, timeout));
2945 codec_dbg(codec, "+++++ DMA complete\n");
2946 dma_set_state(dma_engine, DMA_STATE_STOP);
2947 status = dma_reset(dma_engine);
2952 data += run_size_words;
2953 chip_addx += run_size_words*sizeof(u32);
2954 words_to_write -= run_size_words;
2957 if (remainder_words != 0) {
2958 status = chipio_write_multiple(codec, chip_addx_remainder,
2959 data_remainder, remainder_words);
2966 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2968 * @codec: the HDA codec
2969 * @fls_data: pointer to a fast load image
2970 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2972 * @sample_rate: sampling rate of the stream used for DSP download
2973 * @channels: channels of the stream used for DSP download
2974 * @ovly: TRUE if overlay format is required
2976 * Returns zero or a negative error code.
2978 static int dspxfr_image(struct hda_codec *codec,
2979 const struct dsp_image_seg *fls_data,
2981 unsigned int sample_rate,
2982 unsigned short channels,
2985 struct ca0132_spec *spec = codec->spec;
2987 unsigned short hda_format = 0;
2988 unsigned int response;
2989 unsigned char stream_id = 0;
2990 struct dma_engine *dma_engine;
2991 unsigned int dma_chan;
2992 unsigned int port_map_mask;
2994 if (fls_data == NULL)
2997 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
3001 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
3002 if (!dma_engine->dmab) {
3007 dma_engine->codec = codec;
3008 dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
3009 dma_engine->m_converter_format = hda_format;
3010 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
3011 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
3013 dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
3015 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
3016 hda_format, &response);
3019 codec_dbg(codec, "set converter format fail\n");
3023 status = snd_hda_codec_load_dsp_prepare(codec,
3024 dma_engine->m_converter_format,
3025 dma_engine->buf_size,
3029 spec->dsp_stream_id = status;
3032 status = dspio_alloc_dma_chan(codec, &dma_chan);
3034 codec_dbg(codec, "alloc dmachan fail\n");
3035 dma_chan = INVALID_DMA_CHANNEL;
3041 status = dsp_allocate_ports_format(codec, hda_format,
3044 codec_dbg(codec, "alloc ports fail\n");
3048 stream_id = dma_get_stream_id(dma_engine);
3049 status = codec_set_converter_stream_channel(codec,
3050 WIDGET_CHIP_CTRL, stream_id, 0, &response);
3052 codec_dbg(codec, "set stream chan fail\n");
3056 while ((fls_data != NULL) && !is_last(fls_data)) {
3057 if (!is_valid(fls_data)) {
3058 codec_dbg(codec, "FLS check fail\n");
3062 status = dspxfr_one_seg(codec, fls_data, reloc,
3063 dma_engine, dma_chan,
3064 port_map_mask, ovly);
3068 if (is_hci_prog_list_seg(fls_data))
3069 fls_data = get_next_seg_ptr(fls_data);
3071 if ((fls_data != NULL) && !is_last(fls_data))
3072 fls_data = get_next_seg_ptr(fls_data);
3075 if (port_map_mask != 0)
3076 status = dsp_free_ports(codec);
3081 status = codec_set_converter_stream_channel(codec,
3082 WIDGET_CHIP_CTRL, 0, 0, &response);
3085 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
3086 dspio_free_dma_chan(codec, dma_chan);
3088 if (dma_engine->dmab->area)
3089 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
3090 kfree(dma_engine->dmab);
3097 * CA0132 DSP download stuffs.
3099 static void dspload_post_setup(struct hda_codec *codec)
3101 struct ca0132_spec *spec = codec->spec;
3102 codec_dbg(codec, "---- dspload_post_setup ------\n");
3103 if (!spec->use_alt_functions) {
3104 /*set DSP speaker to 2.0 configuration*/
3105 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
3106 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
3108 /*update write pointer*/
3109 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
3114 * dspload_image - Download DSP from a DSP Image Fast Load structure.
3116 * @codec: the HDA codec
3117 * @fls: pointer to a fast load image
3118 * @ovly: TRUE if overlay format is required
3119 * @reloc: Relocation address for loading single-segment overlays, or 0 for
3121 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
3122 * @router_chans: number of audio router channels to be allocated (0 means use
3123 * internal defaults; max is 32)
3125 * Download DSP from a DSP Image Fast Load structure. This structure is a
3126 * linear, non-constant sized element array of structures, each of which
3127 * contain the count of the data to be loaded, the data itself, and the
3128 * corresponding starting chip address of the starting data location.
3129 * Returns zero or a negative error code.
3131 static int dspload_image(struct hda_codec *codec,
3132 const struct dsp_image_seg *fls,
3139 unsigned int sample_rate;
3140 unsigned short channels;
3142 codec_dbg(codec, "---- dspload_image begin ------\n");
3143 if (router_chans == 0) {
3145 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
3147 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
3150 sample_rate = 48000;
3151 channels = (unsigned short)router_chans;
3153 while (channels > 16) {
3159 codec_dbg(codec, "Ready to program DMA\n");
3161 status = dsp_reset(codec);
3166 codec_dbg(codec, "dsp_reset() complete\n");
3167 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
3173 codec_dbg(codec, "dspxfr_image() complete\n");
3174 if (autostart && !ovly) {
3175 dspload_post_setup(codec);
3176 status = dsp_set_run_state(codec);
3179 codec_dbg(codec, "LOAD FINISHED\n");
3185 #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
3186 static bool dspload_is_loaded(struct hda_codec *codec)
3188 unsigned int data = 0;
3191 status = chipio_read(codec, 0x40004, &data);
3192 if ((status < 0) || (data != 1))
3198 #define dspload_is_loaded(codec) false
3201 static bool dspload_wait_loaded(struct hda_codec *codec)
3203 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
3206 if (dspload_is_loaded(codec)) {
3207 codec_info(codec, "ca0132 DSP downloaded and running\n");
3211 } while (time_before(jiffies, timeout));
3213 codec_err(codec, "ca0132 failed to download DSP\n");
3218 * ca0113 related functions. The ca0113 acts as the HDA bus for the pci-e
3219 * based cards, and has a second mmio region, region2, that's used for special
3224 * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
3225 * the mmio address 0x320 is used to set GPIO pins. The format for the data
3226 * The first eight bits are just the number of the pin. So far, I've only seen
3227 * this number go to 7.
3228 * AE-5 note: The AE-5 seems to use pins 2 and 3 to somehow set the color value
3229 * of the on-card LED. It seems to use pin 2 for data, then toggles 3 to on and
3230 * then off to send that bit.
3232 static void ca0113_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
3235 struct ca0132_spec *spec = codec->spec;
3236 unsigned short gpio_data;
3238 gpio_data = gpio_pin & 0xF;
3239 gpio_data |= ((enable << 8) & 0x100);
3241 writew(gpio_data, spec->mem_base + 0x320);
3245 * Special pci region2 commands that are only used by the AE-5. They follow
3246 * a set format, and require reads at certain points to seemingly 'clear'
3247 * the response data. My first tests didn't do these reads, and would cause
3248 * the card to get locked up until the memory was read. These commands
3249 * seem to work with three distinct values that I've taken to calling group,
3250 * target-id, and value.
3252 static void ca0113_mmio_command_set(struct hda_codec *codec, unsigned int group,
3253 unsigned int target, unsigned int value)
3255 struct ca0132_spec *spec = codec->spec;
3256 unsigned int write_val;
3258 writel(0x0000007e, spec->mem_base + 0x210);
3259 readl(spec->mem_base + 0x210);
3260 writel(0x0000005a, spec->mem_base + 0x210);
3261 readl(spec->mem_base + 0x210);
3262 readl(spec->mem_base + 0x210);
3264 writel(0x00800005, spec->mem_base + 0x20c);
3265 writel(group, spec->mem_base + 0x804);
3267 writel(0x00800005, spec->mem_base + 0x20c);
3268 write_val = (target & 0xff);
3269 write_val |= (value << 8);
3272 writel(write_val, spec->mem_base + 0x204);
3274 * Need delay here or else it goes too fast and works inconsistently.
3278 readl(spec->mem_base + 0x860);
3279 readl(spec->mem_base + 0x854);
3280 readl(spec->mem_base + 0x840);
3282 writel(0x00800004, spec->mem_base + 0x20c);
3283 writel(0x00000000, spec->mem_base + 0x210);
3284 readl(spec->mem_base + 0x210);
3285 readl(spec->mem_base + 0x210);
3289 * This second type of command is used for setting the sound filter type.
3291 static void ca0113_mmio_command_set_type2(struct hda_codec *codec,
3292 unsigned int group, unsigned int target, unsigned int value)
3294 struct ca0132_spec *spec = codec->spec;
3295 unsigned int write_val;
3297 writel(0x0000007e, spec->mem_base + 0x210);
3298 readl(spec->mem_base + 0x210);
3299 writel(0x0000005a, spec->mem_base + 0x210);
3300 readl(spec->mem_base + 0x210);
3301 readl(spec->mem_base + 0x210);
3303 writel(0x00800003, spec->mem_base + 0x20c);
3304 writel(group, spec->mem_base + 0x804);
3306 writel(0x00800005, spec->mem_base + 0x20c);
3307 write_val = (target & 0xff);
3308 write_val |= (value << 8);
3311 writel(write_val, spec->mem_base + 0x204);
3313 readl(spec->mem_base + 0x860);
3314 readl(spec->mem_base + 0x854);
3315 readl(spec->mem_base + 0x840);
3317 writel(0x00800004, spec->mem_base + 0x20c);
3318 writel(0x00000000, spec->mem_base + 0x210);
3319 readl(spec->mem_base + 0x210);
3320 readl(spec->mem_base + 0x210);
3324 * Setup GPIO for the other variants of Core3D.
3328 * Sets up the GPIO pins so that they are discoverable. If this isn't done,
3329 * the card shows as having no GPIO pins.
3331 static void ca0132_gpio_init(struct hda_codec *codec)
3333 struct ca0132_spec *spec = codec->spec;
3335 switch (spec->quirk) {
3338 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3339 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
3340 snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
3343 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3344 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
3350 /* Sets the GPIO for audio output. */
3351 static void ca0132_gpio_setup(struct hda_codec *codec)
3353 struct ca0132_spec *spec = codec->spec;
3355 switch (spec->quirk) {
3357 snd_hda_codec_write(codec, 0x01, 0,
3358 AC_VERB_SET_GPIO_DIRECTION, 0x07);
3359 snd_hda_codec_write(codec, 0x01, 0,
3360 AC_VERB_SET_GPIO_MASK, 0x07);
3361 snd_hda_codec_write(codec, 0x01, 0,
3362 AC_VERB_SET_GPIO_DATA, 0x04);
3363 snd_hda_codec_write(codec, 0x01, 0,
3364 AC_VERB_SET_GPIO_DATA, 0x06);
3367 snd_hda_codec_write(codec, 0x01, 0,
3368 AC_VERB_SET_GPIO_DIRECTION, 0x1E);
3369 snd_hda_codec_write(codec, 0x01, 0,
3370 AC_VERB_SET_GPIO_MASK, 0x1F);
3371 snd_hda_codec_write(codec, 0x01, 0,
3372 AC_VERB_SET_GPIO_DATA, 0x0C);
3378 * GPIO control functions for the Recon3D integrated.
3381 enum r3di_gpio_bit {
3382 /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
3383 R3DI_MIC_SELECT_BIT = 1,
3384 /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
3385 R3DI_OUT_SELECT_BIT = 2,
3387 * I dunno what this actually does, but it stays on until the dsp
3390 R3DI_GPIO_DSP_DOWNLOADING = 3,
3392 * Same as above, no clue what it does, but it comes on after the dsp
3395 R3DI_GPIO_DSP_DOWNLOADED = 4
3398 enum r3di_mic_select {
3399 /* Set GPIO bit 1 to 0 for rear mic */
3401 /* Set GPIO bit 1 to 1 for front microphone*/
3405 enum r3di_out_select {
3406 /* Set GPIO bit 2 to 0 for headphone */
3407 R3DI_HEADPHONE_OUT = 0,
3408 /* Set GPIO bit 2 to 1 for speaker */