2 * HD audio interface patch for Creative Sound Core3D 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/pci.h>
28 #include <linux/module.h>
29 #include <linux/firmware.h>
30 #include <sound/core.h>
31 #include <sound/tlv.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
36 #include "ca0132_regs.h"
38 /* Enable this to see more debug messages. */
39 /*#define ENABLE_CA0132_DEBUG*/
40 /* Enable this to see controls for tuning purpose. */
41 /*#define ENABLE_TUNING_CONTROLS*/
43 #define FLOAT_ZERO 0x00000000
44 #define FLOAT_ONE 0x3f800000
45 #define FLOAT_TWO 0x40000000
46 #define FLOAT_MINUS_5 0xc0a00000
48 #define UNSOL_TAG_HP 0x10
49 #define UNSOL_TAG_AMIC1 0x12
50 #define UNSOL_TAG_DSP 0x16
52 #define SUCCEEDED(_x) (((int)(_x)) >= 0)
53 #define FAILED(_x) (((int)(_x)) < 0)
55 #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
56 #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
58 #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
59 #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
60 #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
62 #define MASTERCONTROL 0x80
63 #define MASTERCONTROL_ALLOC_DMA_CHAN 10
64 #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
66 #define WIDGET_CHIP_CTRL 0x15
67 #define WIDGET_DSP_CTRL 0x16
69 #define MEM_CONNID_MICIN1 3
70 #define MEM_CONNID_MICIN2 5
71 #define MEM_CONNID_MICOUT1 12
72 #define MEM_CONNID_MICOUT2 14
73 #define MEM_CONNID_WUH 10
74 #define MEM_CONNID_DSP 16
75 #define MEM_CONNID_DMIC 100
80 #define SPEQ_FILE "ctspeq.bin"
81 #define EFX_FILE "ctefx.bin"
83 MODULE_FIRMWARE(SPEQ_FILE);
84 MODULE_FIRMWARE(EFX_FILE);
86 /* Debug message controls */
87 #ifdef ENABLE_CA0132_DEBUG
90 { if (!(x)) snd_printdd(KERN_ERR "CTASSERT failed.\n"); }
92 #define CA0132_LOG(fmt, args...) snd_printdd(fmt, ##args)
93 #define CA0132_DSP_LOG(msg) snd_printdd(KERN_INFO "[%s]\n", msg)
94 #define FAIL_MSG(n, s) fail_debug_out(n, s)
96 static int fail_debug_out(int status, const char *s)
98 snd_printdd(KERN_ERR "[%s]\n", s);
103 #define CTASSERT(x) do { } while (0)
104 #define CA0132_LOG(fmt, args...) do { } while (0)
105 #define CA0132_DSP_LOG(msg) do { } while (0)
106 #define FAIL_MSG(n, s) fail_debug_out(n, s)
108 static int fail_debug_out(int status, const char *s)
114 static char *dirstr[2] = { "Playback", "Capture" };
127 #define VNODE_START_NID 0x80
128 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
135 #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
137 #define EFFECT_START_NID 0x90
138 #define OUT_EFFECT_START_NID EFFECT_START_NID
139 SURROUND = OUT_EFFECT_START_NID,
146 #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
148 #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
149 ECHO_CANCELLATION = IN_EFFECT_START_NID,
154 #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
156 VOICEFX = IN_EFFECT_END_NID,
160 #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
163 /* Effects values size*/
164 #define EFFECT_VALS_MAX_COUNT 12
166 #define DSP_CAPTURE_INIT_LATENCY 0
167 #define DSP_CRYSTAL_VOICE_LATENCY 124
168 #define DSP_PLAYBACK_INIT_LATENCY 13
169 #define DSP_PLAY_ENHANCEMENT_LATENCY 30
170 #define DSP_SPEAKER_OUT_LATENCY 7
175 int mid; /*effect module ID*/
176 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
177 int direct; /* 0:output; 1:input*/
178 int params; /* number of default non-on/off params */
179 /*effect default values, 1st is on/off. */
180 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
183 static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
190 {0x3F800000, 0x3F2B851F}
198 {0x3F800000, 0x3F266666}
206 {0x00000000, 0x3F000000}
214 {0x3F800000, 0x3F3D70A4, 0x00000000}
222 {0x3F800000, 0x42A00000, 0x3F000000}
227 {9, 10, 11, 12, 13, 14,
228 15, 16, 17, 18, 19, 20},
231 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
232 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
233 0x00000000, 0x00000000}
235 { "Echo Cancellation",
241 {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
249 {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
257 {0x00000000, 0x3F3D70A4}
265 {0x3F800000, 0x3F000000}
270 {10, 11, 12, 13, 14, 15, 16, 17, 18},
273 {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000, 0x3F800000,
274 0x3F800000, 0x3F800000, 0x00000000, 0x00000000}
279 /* Tuning controls */
280 #ifdef ENABLE_TUNING_CONTROLS
283 #define TUNING_CTL_START_NID 0xC0
284 WEDGE_ANGLE = TUNING_CTL_START_NID,
297 #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
300 struct ct_tuning_ctl {
302 hda_nid_t parent_nid;
304 int mid; /*effect module ID*/
305 int req; /*effect module request*/
306 int direct; /* 0:output; 1:input*/
307 unsigned int def_val;/*effect default values*/
310 static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
410 /* Voice FX Presets */
411 #define VOICEFX_MAX_PARAM_COUNT 9
417 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
420 struct ct_voicefx_preset {
421 char *name; /*preset name*/
422 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
425 struct ct_voicefx ca0132_voicefx = {
426 "VoiceFX Capture Switch",
429 {10, 11, 12, 13, 14, 15, 16, 17, 18}
432 struct ct_voicefx_preset ca0132_voicefx_presets[] = {
434 { 0x00000000, 0x43C80000, 0x44AF0000,
435 0x44FA0000, 0x3F800000, 0x3F800000,
436 0x3F800000, 0x00000000, 0x00000000 }
439 { 0x3F800000, 0x43C80000, 0x44AF0000,
440 0x44FA0000, 0x3F19999A, 0x3F866666,
441 0x3F800000, 0x00000000, 0x00000000 }
444 { 0x3F800000, 0x43C80000, 0x44AF0000,
445 0x450AC000, 0x4017AE14, 0x3F6B851F,
446 0x3F800000, 0x00000000, 0x00000000 }
449 { 0x3F800000, 0x43C80000, 0x44AF0000,
450 0x44FA0000, 0x40400000, 0x3F28F5C3,
451 0x3F800000, 0x00000000, 0x00000000 }
454 { 0x3F800000, 0x44324000, 0x44BB8000,
455 0x44E10000, 0x3FB33333, 0x3FB9999A,
456 0x3F800000, 0x3E3A2E43, 0x00000000 }
459 { 0x3F800000, 0x43EA0000, 0x44A52000,
460 0x45098000, 0x3F266666, 0x3FC00000,
461 0x3F800000, 0x00000000, 0x00000000 }
464 { 0x3F800000, 0x43C70000, 0x44AE6000,
465 0x45193000, 0x3F8E147B, 0x3F75C28F,
466 0x3F800000, 0x00000000, 0x00000000 }
469 { 0x3F800000, 0x43930000, 0x44BEE000,
470 0x45007000, 0x3F451EB8, 0x3F7851EC,
471 0x3F800000, 0x00000000, 0x00000000 }
474 { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
475 0x451F6000, 0x3F266666, 0x3FA7D945,
476 0x3F800000, 0x3CF5C28F, 0x00000000 }
479 { 0x3F800000, 0x43C80000, 0x44AF0000,
480 0x44FA0000, 0x3FB2718B, 0x3F800000,
481 0xBC07010E, 0x00000000, 0x00000000 }
484 { 0x3F800000, 0x43C20000, 0x44906000,
485 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
486 0x3F0A3D71, 0x00000000, 0x00000000 }
489 { 0x3F800000, 0x43C80000, 0x44AF0000,
490 0x44FA0000, 0x3F800000, 0x3F800000,
491 0x3E4CCCCD, 0x00000000, 0x00000000 }
494 { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
495 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
496 0x3F800000, 0x00000000, 0x00000000 }
499 { 0x3F800000, 0x43C80000, 0x44AF0000,
500 0x44FA0000, 0x3F800000, 0x3F1A043C,
501 0x3F800000, 0x00000000, 0x00000000 }
505 enum hda_cmd_vendor_io {
507 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
508 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
510 VENDOR_DSPIO_STATUS = 0xF01,
511 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
512 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
513 VENDOR_DSPIO_DSP_INIT = 0x703,
514 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
515 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
517 /* for ChipIO node */
518 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
519 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
520 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
521 VENDOR_CHIPIO_DATA_LOW = 0x300,
522 VENDOR_CHIPIO_DATA_HIGH = 0x400,
524 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
525 VENDOR_CHIPIO_STATUS = 0xF01,
526 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
527 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
529 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
530 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
532 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
533 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
535 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
536 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
537 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
538 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
539 VENDOR_CHIPIO_FLAG_SET = 0x70F,
540 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
541 VENDOR_CHIPIO_PARAM_SET = 0x710,
542 VENDOR_CHIPIO_PARAM_GET = 0xF10,
544 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
545 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
546 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
547 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
549 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
550 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
551 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
552 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
554 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
555 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
556 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
557 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
558 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
559 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
561 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
564 enum control_flag_id {
565 /* Connection manager stream setup is bypassed/enabled */
566 CONTROL_FLAG_C_MGR = 0,
567 /* DSP DMA is bypassed/enabled */
568 CONTROL_FLAG_DMA = 1,
569 /* 8051 'idle' mode is disabled/enabled */
570 CONTROL_FLAG_IDLE_ENABLE = 2,
571 /* Tracker for the SPDIF-in path is bypassed/enabled */
572 CONTROL_FLAG_TRACKER = 3,
573 /* DigitalOut to Spdif2Out connection is disabled/enabled */
574 CONTROL_FLAG_SPDIF2OUT = 4,
575 /* Digital Microphone is disabled/enabled */
576 CONTROL_FLAG_DMIC = 5,
577 /* ADC_B rate is 48 kHz/96 kHz */
578 CONTROL_FLAG_ADC_B_96KHZ = 6,
579 /* ADC_C rate is 48 kHz/96 kHz */
580 CONTROL_FLAG_ADC_C_96KHZ = 7,
581 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
582 CONTROL_FLAG_DAC_96KHZ = 8,
583 /* DSP rate is 48 kHz/96 kHz */
584 CONTROL_FLAG_DSP_96KHZ = 9,
585 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
586 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
587 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
588 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
589 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
590 CONTROL_FLAG_DECODE_LOOP = 12,
591 /* De-emphasis filter on DAC-1 disabled/enabled */
592 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
593 /* De-emphasis filter on DAC-2 disabled/enabled */
594 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
595 /* De-emphasis filter on DAC-3 disabled/enabled */
596 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
597 /* High-pass filter on ADC_B disabled/enabled */
598 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
599 /* High-pass filter on ADC_C disabled/enabled */
600 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
601 /* Common mode on Port_A disabled/enabled */
602 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
603 /* Common mode on Port_D disabled/enabled */
604 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
605 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
606 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
607 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
608 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
609 /* ASI rate is 48kHz/96kHz */
610 CONTROL_FLAG_ASI_96KHZ = 22,
611 /* DAC power settings able to control attached ports no/yes */
612 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
613 /* Clock Stop OK reporting is disabled/enabled */
614 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
615 /* Number of control flags */
616 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
619 enum control_param_id {
620 /* 0: None, 1: Mic1In*/
621 CONTROL_PARAM_VIP_SOURCE = 1,
622 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
623 CONTROL_PARAM_SPDIF1_SOURCE = 2,
624 /* Port A output stage gain setting to use when 16 Ohm output
625 * impedance is selected*/
626 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
627 /* Port D output stage gain setting to use when 16 Ohm output
628 * impedance is selected*/
629 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
633 /* Select stream with the given ID */
634 CONTROL_PARAM_STREAM_ID = 24,
635 /* Source connection point for the selected stream */
636 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
637 /* Destination connection point for the selected stream */
638 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
639 /* Number of audio channels in the selected stream */
640 CONTROL_PARAM_STREAMS_CHANNELS = 27,
641 /*Enable control for the selected stream */
642 CONTROL_PARAM_STREAM_CONTROL = 28,
644 /* Connection Point Control */
646 /* Select connection point with the given ID */
647 CONTROL_PARAM_CONN_POINT_ID = 29,
648 /* Connection point sample rate */
649 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
653 /* Select HDA node with the given ID */
654 CONTROL_PARAM_NODE_ID = 31
657 enum hda_vendor_status_dspio {
659 VENDOR_STATUS_DSPIO_OK = 0x00,
660 /* Busy, unable to accept new command, the host must retry */
661 VENDOR_STATUS_DSPIO_BUSY = 0x01,
662 /* SCP command queue is full */
663 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
664 /* SCP response queue is empty */
665 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
668 enum hda_vendor_status_chipio {
670 VENDOR_STATUS_CHIPIO_OK = 0x00,
671 /* Busy, unable to accept new command, the host must retry */
672 VENDOR_STATUS_CHIPIO_BUSY = 0x01
675 enum ca0132_sample_rate {
695 SR_RATE_UNKNOWN = 0x1F
698 enum dsp_download_state {
699 DSP_DOWNLOAD_FAILED = -1,
700 DSP_DOWNLOAD_INIT = 0,
705 struct hda_stream_format {
706 unsigned int sample_rate;
707 unsigned short valid_bits_per_sample;
708 unsigned short container_size;
709 unsigned short number_channels;
712 /* retrieve parameters from hda format */
713 #define get_hdafmt_chs(fmt) (fmt & 0xf)
714 #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
715 #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
716 #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
719 struct snd_kcontrol_new *mixers[5];
720 unsigned int num_mixers;
721 const struct hda_verb *base_init_verbs;
722 const struct hda_verb *base_exit_verbs;
723 const struct hda_verb *init_verbs[5];
724 unsigned int num_init_verbs; /* exclude base init verbs */
725 struct auto_pin_cfg autocfg;
727 /* Nodes configurations */
728 struct hda_multi_out multiout;
729 hda_nid_t out_pins[5];
731 unsigned int num_outputs;
732 hda_nid_t input_pins[5];
734 unsigned int num_inputs;
735 hda_nid_t shared_mic_nid;
736 hda_nid_t shared_out_nid;
737 struct hda_pcm pcm_rec[5]; /* PCM information */
740 struct mutex chipio_mutex; /* chip access mutex */
743 /* DSP download related */
744 enum dsp_download_state dsp_state;
745 unsigned int dsp_stream_id;
746 unsigned int wait_scp;
747 unsigned int wait_scp_header;
748 unsigned int wait_num_data;
749 unsigned int scp_resp_header;
750 unsigned int scp_resp_data[4];
751 unsigned int scp_resp_count;
753 /* mixer and effects related */
754 unsigned char dmic_ctl;
757 long vnode_lvol[VNODES_COUNT];
758 long vnode_rvol[VNODES_COUNT];
759 long vnode_lswitch[VNODES_COUNT];
760 long vnode_rswitch[VNODES_COUNT];
761 long effects_switch[EFFECTS_COUNT];
765 struct hda_codec *codec;
766 struct delayed_work unsol_hp_work;
768 #ifdef ENABLE_TUNING_CONTROLS
769 long cur_ctl_vals[TUNING_CTLS_COUNT];
774 * CA0132 codec access
776 unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
777 unsigned int verb, unsigned int parm, unsigned int *res)
779 unsigned int response;
780 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
783 return ((response == -1) ? -1 : 0);
786 static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
787 unsigned short converter_format, unsigned int *res)
789 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
790 converter_format & 0xffff, res);
793 static int codec_set_converter_stream_channel(struct hda_codec *codec,
794 hda_nid_t nid, unsigned char stream,
795 unsigned char channel, unsigned int *res)
797 unsigned char converter_stream_channel = 0;
799 converter_stream_channel = (stream << 4) | (channel & 0x0f);
800 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
801 converter_stream_channel, res);
805 * CA0132 chip access stuffs
807 static int chipio_send(struct hda_codec *codec,
812 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
814 /* send bits of data specified by reg */
816 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
818 if (res == VENDOR_STATUS_CHIPIO_OK)
821 } while (time_before(jiffies, timeout));
827 static int chipio_write_addx(struct hda_codec *codec, u32 chip_addx)
829 struct ca0132_spec *spec = codec->spec;
832 if (spec->curr_chip_addx == chip_addx)
835 /* send low 16 bits of the address */
836 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
839 if (SUCCEEDED(status)) {
840 /* send high 16 bits of the address */
841 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
845 CTASSERT(SUCCEEDED(status));
846 spec->curr_chip_addx = SUCCEEDED(status) ? chip_addx : ~0UL;
851 static int chipio_write_data(struct hda_codec *codec, u32 data)
853 struct ca0132_spec *spec = codec->spec;
856 /* send low 16 bits of the data */
857 status = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
859 if (SUCCEEDED(status)) {
860 /* send high 16 bits of the data */
861 status = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
865 CTASSERT(SUCCEEDED(status));
866 spec->curr_chip_addx = SUCCEEDED(status) ?
867 (spec->curr_chip_addx + 4) : ~0UL;
872 static int chipio_write_data_multiple(struct hda_codec *codec,
879 return FAIL_MSG(-1, "chipio_write_data null ptr");
881 while ((count-- != 0) && (status == 0))
882 status = chipio_write_data(codec, *data++);
887 static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
889 struct ca0132_spec *spec = codec->spec;
893 status = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
895 if (SUCCEEDED(status)) {
897 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
900 if (SUCCEEDED(status)) {
902 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
903 VENDOR_CHIPIO_HIC_READ_DATA, 0);
906 CTASSERT(SUCCEEDED(status));
907 spec->curr_chip_addx = SUCCEEDED(status) ?
908 (spec->curr_chip_addx + 4) : ~0UL;
913 static int chipio_write(struct hda_codec *codec, u32 chip_addx,
916 struct ca0132_spec *spec = codec->spec;
919 mutex_lock(&spec->chipio_mutex);
920 status = chipio_write_addx(codec, chip_addx);
921 if (SUCCEEDED(status))
922 status = chipio_write_data(codec, data);
923 mutex_unlock(&spec->chipio_mutex);
928 static int chipio_write_multiple(struct hda_codec *codec,
933 struct ca0132_spec *spec = codec->spec;
936 mutex_lock(&spec->chipio_mutex);
937 status = chipio_write_addx(codec, chip_addx);
938 if (SUCCEEDED(status))
939 status = chipio_write_data_multiple(codec, data, count);
940 mutex_unlock(&spec->chipio_mutex);
945 static int chipio_read(struct hda_codec *codec, u32 chip_addx,
948 struct ca0132_spec *spec = codec->spec;
951 mutex_lock(&spec->chipio_mutex);
952 status = chipio_write_addx(codec, chip_addx);
953 if (SUCCEEDED(status))
954 status = chipio_read_data(codec, data);
955 mutex_unlock(&spec->chipio_mutex);
960 static void chipio_set_control_flag(struct hda_codec *codec,
961 enum control_flag_id flag_id,
965 unsigned int flag_bit;
967 flag_bit = (flag_state ? 1 : 0);
968 val = (flag_bit << 7) | (flag_id);
969 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
970 VENDOR_CHIPIO_FLAG_SET, val);
973 static void chipio_set_control_param(struct hda_codec *codec,
974 enum control_param_id param_id, int param_val)
976 struct ca0132_spec *spec = codec->spec;
979 if ((param_id < 32) && (param_val < 8)) {
980 val = (param_val << 5) | (param_id);
981 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
982 VENDOR_CHIPIO_PARAM_SET, val);
984 mutex_lock(&spec->chipio_mutex);
985 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
986 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
987 VENDOR_CHIPIO_PARAM_EX_ID_SET,
989 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
990 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
993 CA0132_LOG("set_control_param:FAIL! id=0x%x, val=%d\n",
994 param_id, param_val);
995 mutex_unlock(&spec->chipio_mutex);
999 static void chipio_set_conn_rate(struct hda_codec *codec,
1000 int connid, enum ca0132_sample_rate rate)
1002 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1003 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1007 static void chipio_enable_clocks(struct hda_codec *codec)
1009 struct ca0132_spec *spec = codec->spec;
1011 mutex_lock(&spec->chipio_mutex);
1012 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1013 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1014 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1015 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1016 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1017 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1018 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1019 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1020 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1021 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1022 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1023 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1024 mutex_unlock(&spec->chipio_mutex);
1029 * CA0132 DSP IO stuffs
1031 static int dspio_send(struct hda_codec *codec, unsigned int reg,
1035 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1037 /* send bits of data specified by reg to dsp */
1039 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1040 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1043 } while (time_before(jiffies, timeout));
1049 static void dspio_write_wait(struct hda_codec *codec)
1052 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1055 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1056 VENDOR_DSPIO_STATUS, 0);
1057 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1058 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1061 } while (time_before(jiffies, timeout));
1064 static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1066 struct ca0132_spec *spec = codec->spec;
1069 dspio_write_wait(codec);
1071 mutex_lock(&spec->chipio_mutex);
1072 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1074 if (SUCCEEDED(status))
1075 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1077 mutex_unlock(&spec->chipio_mutex);
1079 /* OK, now check if the write itself has executed*/
1080 if (SUCCEEDED(status))
1081 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1082 VENDOR_DSPIO_STATUS, 0);
1084 if (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL)
1085 CA0132_LOG("dspio_write: SCP_COMMAND_QUEUE_FULL!!!\n\n");
1087 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1091 static int dspio_write_multiple(struct hda_codec *codec,
1092 unsigned int *buffer, unsigned int size)
1097 if ((buffer == NULL))
1101 while (count < size) {
1102 status = dspio_write(codec, *buffer++);
1111 static int dspio_read(struct hda_codec *codec, unsigned int *data)
1115 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1116 if (SUCCEEDED(status))
1117 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1119 if (FAILED(status) ||
1120 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1123 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1124 VENDOR_DSPIO_SCP_READ_DATA, 0);
1129 static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1130 unsigned int *buf_size, unsigned int size_count)
1133 unsigned int size = *buf_size;
1135 unsigned int skip_count;
1138 if ((buffer == NULL))
1142 while (count < size && count < size_count) {
1143 status = dspio_read(codec, buffer++);
1150 if (SUCCEEDED(status)) {
1151 while (skip_count < size) {
1152 status = dspio_read(codec, &dummy);
1163 static inline unsigned int
1164 make_scp_header(unsigned int target_id, unsigned int source_id,
1165 unsigned int get_flag, unsigned int req,
1166 unsigned int device_flag, unsigned int resp_flag,
1167 unsigned int error_flag, unsigned int data_size)
1169 unsigned int header = 0;
1171 header = (data_size & 0x1f) << 27;
1172 header |= (error_flag & 0x01) << 26;
1173 header |= (resp_flag & 0x01) << 25;
1174 header |= (device_flag & 0x01) << 24;
1175 header |= (req & 0x7f) << 17;
1176 header |= (get_flag & 0x01) << 16;
1177 header |= (source_id & 0xff) << 8;
1178 header |= target_id & 0xff;
1184 extract_scp_header(unsigned int header,
1185 unsigned int *target_id, unsigned int *source_id,
1186 unsigned int *get_flag, unsigned int *req,
1187 unsigned int *device_flag, unsigned int *resp_flag,
1188 unsigned int *error_flag, unsigned int *data_size)
1191 *data_size = (header >> 27) & 0x1f;
1193 *error_flag = (header >> 26) & 0x01;
1195 *resp_flag = (header >> 25) & 0x01;
1197 *device_flag = (header >> 24) & 0x01;
1199 *req = (header >> 17) & 0x7f;
1201 *get_flag = (header >> 16) & 0x01;
1203 *source_id = (header >> 8) & 0xff;
1205 *target_id = header & 0xff;
1208 #define SCP_MAX_DATA_WORDS (16)
1210 /* Structure to contain any SCP message */
1213 unsigned int data[SCP_MAX_DATA_WORDS];
1216 static void dspio_clear_response_queue(struct hda_codec *codec)
1218 unsigned int dummy = 0;
1221 /* clear all from the response queue */
1223 status = dspio_read(codec, &dummy);
1224 } while (status == 0);
1227 static int dspio_get_response_data(struct hda_codec *codec)
1229 struct ca0132_spec *spec = codec->spec;
1230 unsigned int data = 0;
1233 if (FAILED(dspio_read(codec, &data)))
1236 CA0132_LOG("dspio_get_response_data: 0x%08x\n", data);
1237 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1238 spec->scp_resp_header = data;
1239 spec->scp_resp_count = data >> 27;
1240 count = spec->wait_num_data;
1241 dspio_read_multiple(codec, spec->scp_resp_data,
1242 &spec->scp_resp_count, count);
1249 static int dspio_send_scp_message(struct hda_codec *codec,
1250 unsigned char *send_buf,
1251 unsigned int send_buf_size,
1252 unsigned char *return_buf,
1253 unsigned int return_buf_size,
1254 unsigned int *bytes_returned)
1256 struct ca0132_spec *spec = codec->spec;
1259 unsigned int scp_send_size = 0;
1260 unsigned int total_size;
1261 bool waiting_for_resp = false;
1262 unsigned int header;
1263 struct scp_msg *ret_msg;
1264 unsigned int resp_src_id, resp_target_id;
1265 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1268 *bytes_returned = 0;
1270 /* get scp header from buffer */
1271 header = *((unsigned int *)send_buf);
1272 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1273 &device_flag, NULL, NULL, &data_size);
1274 scp_send_size = data_size + 1;
1275 total_size = (scp_send_size * 4);
1277 CTASSERT(send_buf_size >= total_size);
1278 if (send_buf_size < total_size)
1281 if (get_flag || device_flag) {
1282 if (!return_buf || return_buf_size < 4 || !bytes_returned) {
1286 spec->wait_scp_header = *((unsigned int *)send_buf);
1288 /* swap source id with target id */
1289 resp_target_id = src_id;
1290 resp_src_id = target_id;
1291 spec->wait_scp_header &= 0xffff0000;
1292 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1293 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1295 waiting_for_resp = true;
1298 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1300 if (FAILED(status)) {
1306 if (waiting_for_resp) {
1307 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1308 memset(return_buf, 0, return_buf_size);
1311 } while (spec->wait_scp && time_before(jiffies, timeout));
1312 waiting_for_resp = false;
1313 if (!spec->wait_scp) {
1314 ret_msg = (struct scp_msg *)return_buf;
1315 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1316 memcpy(&ret_msg->data, spec->scp_resp_data,
1317 spec->wait_num_data);
1318 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1321 CTASSERT(*bytes_returned == 0);
1330 static int dspio_scp(struct hda_codec *codec,
1331 int mod_id, int req, int dir, void *data, unsigned int len,
1332 void *reply, unsigned int *reply_len)
1335 struct scp_msg scp_send, scp_reply;
1336 unsigned int ret_bytes, send_size, ret_size;
1337 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1338 unsigned int reply_data_size;
1340 memset(&scp_send, 0, sizeof(scp_send));
1341 memset(&scp_reply, 0, sizeof(scp_reply));
1343 CTASSERT((len == 0 || data != NULL) && len <= SCP_MAX_DATA_WORDS);
1344 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1347 CTASSERT(dir != SCP_GET || reply != NULL);
1348 if (dir == SCP_GET && reply == NULL)
1349 return FAIL_MSG(-1, "dspio_scp get but has no buffer");
1351 CTASSERT((reply == NULL) || (reply_len != NULL && (*reply_len > 0)));
1352 if (reply != NULL && (reply_len == NULL || (*reply_len == 0)))
1353 return FAIL_MSG(-1, "dspio_scp bad resp buf len parms");
1355 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1356 0, 0, 0, len/sizeof(unsigned int));
1357 if (data != NULL && len > 0) {
1358 len = min((unsigned int)(sizeof(scp_send.data)), len);
1359 memcpy(scp_send.data, data, len);
1363 send_size = sizeof(unsigned int) + len;
1364 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1365 send_size, (unsigned char *)&scp_reply,
1366 sizeof(scp_reply), &ret_bytes);
1369 #ifdef ENABLE_CA0132_DEBUG
1371 unsigned int x, *p, i;
1372 unsigned int source_id;
1373 unsigned int target_id;
1375 unsigned int get_flag;
1376 unsigned int resp_flag;
1377 unsigned int device_flag;
1378 unsigned int error_flag;
1379 unsigned int data_size;
1381 memcpy(&x, &scp_send.hdr, sizeof(unsigned int));
1382 extract_scp_header(x, &target_id, &source_id,
1383 &get_flag, &req, &device_flag, &resp_flag,
1384 &error_flag, &data_size);
1387 "ScpDispatch ----- REQ: "
1404 for (x = 0; x < data_size; x++)
1405 snd_printdd("0x%08x ", scp_send.data[x].ui);
1410 if (ret_bytes >= sizeof(scp_reply.hdr)) {
1411 memcpy(&x, &scp_reply.hdr, sizeof(unsigned int));
1412 extract_scp_header(x, &target_id, &source_id,
1413 &get_flag, &req, &device_flag,
1414 &resp_flag, &error_flag, &data_size);
1434 for (x = 0; x < data_size; x++)
1435 snd_printdd("0x%08x ", scp_reply.data[x].ui);
1437 snd_printdd("REP: (too short,nbytes=%d) [", ret_bytes);
1439 for (p = (unsigned int *)&scp_reply, i = 0;
1440 i < ret_bytes; p++, i++) {
1441 snd_printdd("0x%04x ", *p);
1450 return FAIL_MSG(status, "dspio_scp: send scp msg failed");
1452 /* extract send and reply headers members */
1453 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1454 NULL, NULL, NULL, NULL, NULL);
1455 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1456 &reply_resp_flag, &reply_error_flag,
1462 if (reply_resp_flag && !reply_error_flag) {
1463 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1464 / sizeof(unsigned int);
1466 CTASSERT(dir == SCP_GET && reply != NULL && reply_len != NULL);
1468 if (*reply_len < ret_size*sizeof(unsigned int)) {
1469 status = FAIL_MSG(-1, "reply too long for buf");
1470 } else if (ret_size != reply_data_size) {
1471 status = FAIL_MSG(-1, "RetLen and HdrLen .NE.");
1473 *reply_len = ret_size*sizeof(unsigned int);
1474 memcpy(reply, scp_reply.data, *reply_len);
1477 status = FAIL_MSG(-1, "reply ill-formed or errflag set");
1483 static int dspio_set_param(struct hda_codec *codec, int mod_id,
1484 int req, void *data, unsigned int len)
1486 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1489 static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1492 unsigned int size = sizeof(dma_chan);
1494 CA0132_DSP_LOG(" dspio_alloc_dma_chan() -- begin");
1495 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1496 SCP_GET, NULL, 0, dma_chan, &size);
1498 if (FAILED(status)) {
1499 CA0132_DSP_LOG("dspio_alloc_dma_chan: SCP Failed");
1503 if ((*dma_chan + 1) == 0) {
1504 CA0132_DSP_LOG("no free dma channels to allocate");
1508 CA0132_LOG("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1509 CA0132_DSP_LOG(" dspio_alloc_dma_chan() -- complete");
1514 static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1517 unsigned int dummy = 0;
1519 CA0132_DSP_LOG(" dspio_free_dma_chan() -- begin");
1520 CA0132_LOG("dspio_free_dma_chan: chan=%d\n", dma_chan);
1522 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1523 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1525 if (FAILED(status)) {
1526 CA0132_DSP_LOG("dspio_free_dma_chan: SCP Failed");
1530 CA0132_DSP_LOG(" dspio_free_dma_chan() -- complete");
1536 * CA0132 DSP access stuffs
1538 static int dsp_set_run_state(struct hda_codec *codec)
1540 unsigned int dbg_ctrl_reg;
1541 unsigned int halt_state;
1544 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1548 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1549 DSP_DBGCNTL_STATE_LOBIT;
1551 if (halt_state != 0) {
1552 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1553 DSP_DBGCNTL_SS_MASK);
1554 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1559 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1560 DSP_DBGCNTL_EXEC_MASK;
1561 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1570 static int dsp_reset(struct hda_codec *codec)
1575 CA0132_LOG("dsp_reset\n");
1577 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1579 } while (res == -1 && retry);
1582 CA0132_LOG("dsp_reset timeout\n");
1589 static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1590 bool *code, bool *yram)
1592 *code = *yram = false;
1594 if (UC_RANGE(chip_addx, 1)) {
1596 return UC_OFF(chip_addx);
1597 } else if (X_RANGE_ALL(chip_addx, 1)) {
1598 return X_OFF(chip_addx);
1599 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1601 return Y_OFF(chip_addx);
1604 return (unsigned int)INVALID_CHIP_ADDRESS;
1607 static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1609 unsigned int dma_chnlstart_reg;
1611 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1613 return ((dma_chnlstart_reg & (1 <<
1614 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1617 static int dsp_dma_setup_common(struct hda_codec *codec,
1618 unsigned int chip_addx,
1619 unsigned int dma_chan,
1620 unsigned int port_map_mask,
1624 unsigned int chnl_prop;
1625 unsigned int dsp_addx;
1626 unsigned int active;
1629 CA0132_DSP_LOG("-- dsp_dma_setup_common() -- Begin ---------");
1631 CTASSERT(dma_chan < DSPDMAC_DMA_CFG_CHANNEL_COUNT);
1632 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT)
1633 return FAIL_MSG(-1, "dma chan num invalid");
1635 if (dsp_is_dma_active(codec, dma_chan))
1636 return FAIL_MSG(-1, "dma already active");
1638 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1640 if (dsp_addx == INVALID_CHIP_ADDRESS)
1641 return FAIL_MSG(-1, "invalid chip addr");
1643 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1646 CA0132_DSP_LOG(" dsp_dma_setup_common() start reg pgm");
1649 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1653 return FAIL_MSG(-1, "read CHNLPROP Reg fail");
1655 CA0132_DSP_LOG(" dsp_dma_setup_common() Read CHNLPROP");
1659 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1661 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1663 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1665 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1667 return FAIL_MSG(-1, "write CHNLPROP Reg fail");
1669 CA0132_DSP_LOG(" dsp_dma_setup_common() Write CHNLPROP");
1672 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1676 return FAIL_MSG(-1, "read ACTIVE Reg fail");
1678 CA0132_DSP_LOG(" dsp_dma_setup_common() Read ACTIVE");
1681 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1682 DSPDMAC_ACTIVE_AAR_MASK;
1684 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1686 return FAIL_MSG(-1, "write ACTIVE Reg fail");
1688 CA0132_DSP_LOG(" dsp_dma_setup_common() Write ACTIVE");
1690 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1693 return FAIL_MSG(-1, "write AUDCHSEL Reg fail");
1695 CA0132_DSP_LOG(" dsp_dma_setup_common() Write AUDCHSEL");
1697 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1698 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1700 return FAIL_MSG(-1, "write IRQCNT Reg fail");
1702 CA0132_DSP_LOG(" dsp_dma_setup_common() Write IRQCNT");
1705 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1706 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1707 chip_addx, dsp_addx, dma_chan,
1708 port_map_mask, chnl_prop, active);
1710 CA0132_DSP_LOG("-- dsp_dma_setup_common() -- Complete ---------");
1715 static int dsp_dma_setup(struct hda_codec *codec,
1716 unsigned int chip_addx,
1718 unsigned int dma_chan)
1722 unsigned int dsp_addx;
1723 unsigned int addr_field;
1724 unsigned int incr_field;
1725 unsigned int base_cnt;
1726 unsigned int cur_cnt;
1727 unsigned int dma_cfg = 0;
1728 unsigned int adr_ofs = 0;
1729 unsigned int xfr_cnt = 0;
1730 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1731 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1733 CA0132_DSP_LOG("-- dsp_dma_setup() -- Begin ---------");
1735 CTASSERT(count <= max_dma_count);
1736 if (count > max_dma_count)
1737 return FAIL_MSG(-1, "count too big");
1739 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1740 if (dsp_addx == INVALID_CHIP_ADDRESS)
1741 return FAIL_MSG(-1, "invalid chip addr");
1743 CA0132_DSP_LOG(" dsp_dma_setup() start reg pgm");
1745 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1751 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1753 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1756 CTASSERT((addr_field & DSPDMAC_DMACFG_DBADR_MASK) == addr_field);
1757 dma_cfg = addr_field + incr_field;
1758 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1761 return FAIL_MSG(-1, "write DMACFG Reg fail");
1763 CA0132_DSP_LOG(" dsp_dma_setup() Write DMACFG");
1765 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1767 CTASSERT((adr_ofs & DSPDMAC_DSPADROFS_BOFS_MASK) == adr_ofs);
1769 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1772 return FAIL_MSG(-1, "write DSPADROFS Reg fail");
1774 CA0132_DSP_LOG(" dsp_dma_setup() Write DSPADROFS");
1776 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1777 CTASSERT((base_cnt & DSPDMAC_XFRCNT_BCNT_MASK) == base_cnt);
1779 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1780 CTASSERT((cur_cnt & DSPDMAC_XFRCNT_CCNT_MASK) == cur_cnt);
1782 xfr_cnt = base_cnt | cur_cnt;
1784 status = chipio_write(codec,
1785 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1787 return FAIL_MSG(-1, "write XFRCNT Reg fail");
1789 CA0132_DSP_LOG(" dsp_dma_setup() Write XFRCNT");
1792 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1793 "ADROFS=0x%x, XFRCNT=0x%x\n",
1794 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1796 CA0132_DSP_LOG("-- dsp_dma_setup() -- Complete ---------");
1801 static int dsp_dma_start(struct hda_codec *codec,
1802 unsigned int dma_chan, bool ovly)
1804 unsigned int reg = 0;
1807 CA0132_DSP_LOG("-- dsp_dma_start() -- Begin ---------");
1810 status = chipio_read(codec,
1811 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
1814 return FAIL_MSG(-1, "read CHNLSTART reg fail");
1816 CA0132_DSP_LOG("-- dsp_dma_start() Read CHNLSTART");
1818 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1819 DSPDMAC_CHNLSTART_DIS_MASK);
1822 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1823 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1825 return FAIL_MSG(-1, "write CHNLSTART reg fail");
1827 CA0132_DSP_LOG("-- dsp_dma_start() -- Complete ---------");
1832 static int dsp_dma_stop(struct hda_codec *codec,
1833 unsigned int dma_chan, bool ovly)
1835 unsigned int reg = 0;
1838 CA0132_DSP_LOG("-- dsp_dma_stop() -- Begin ---------");
1841 status = chipio_read(codec,
1842 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
1845 return FAIL_MSG(-1, "read CHNLSTART reg fail");
1847 CA0132_DSP_LOG("-- dsp_dma_stop() Read CHNLSTART");
1848 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1849 DSPDMAC_CHNLSTART_DIS_MASK);
1852 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1853 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1855 return FAIL_MSG(-1, "write CHNLSTART reg fail");
1857 CA0132_DSP_LOG("-- dsp_dma_stop() -- Complete ---------");
1862 static int dsp_allocate_router_ports(struct hda_codec *codec,
1863 unsigned int num_chans,
1864 unsigned int ports_per_channel,
1865 unsigned int start_device,
1866 unsigned int *port_map)
1872 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1876 val = start_device << 6;
1877 val |= (ports_per_channel - 1) << 4;
1878 val |= num_chans - 1;
1880 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1881 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1884 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1885 VENDOR_CHIPIO_PORT_ALLOC_SET,
1888 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1892 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1893 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1897 return (res < 0) ? -1 : 0;
1900 static int dsp_free_router_ports(struct hda_codec *codec)
1904 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1908 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1909 VENDOR_CHIPIO_PORT_FREE_SET,
1912 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1917 static int dsp_allocate_ports(struct hda_codec *codec,
1918 unsigned int num_chans,
1919 unsigned int rate_multi, unsigned int *port_map)
1923 CA0132_DSP_LOG(" dsp_allocate_ports() -- begin");
1925 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4))
1926 return FAIL_MSG(status, "bad rate multiple");
1928 status = dsp_allocate_router_ports(codec, num_chans,
1929 rate_multi, 0, port_map);
1931 CA0132_DSP_LOG(" dsp_allocate_ports() -- complete");
1936 static int dsp_free_ports(struct hda_codec *codec)
1940 CA0132_DSP_LOG(" dsp_free_ports() -- begin");
1942 status = dsp_free_router_ports(codec);
1944 return FAIL_MSG(-1, "free router ports fail");
1946 CA0132_DSP_LOG(" dsp_free_ports() -- complete");
1951 static int dsp_allocate_ports_format(struct hda_codec *codec,
1952 const unsigned short fmt,
1953 unsigned int *port_map)
1956 unsigned int num_chans;
1958 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1959 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1960 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1962 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4))
1963 return FAIL_MSG(-1, "bad rate multiple");
1965 num_chans = get_hdafmt_chs(fmt) + 1;
1967 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1973 * HDA DMA engine stuffs for DSP code download
1976 struct hda_codec *codec;
1977 unsigned short m_converter_format;
1978 struct snd_dma_buffer *dmab;
1979 unsigned int buf_size;
1987 static int dma_convert_to_hda_format(
1988 struct hda_stream_format *stream_format,
1989 unsigned short *hda_format)
1991 unsigned int format_val;
1993 format_val = snd_hda_calc_stream_format(
1994 stream_format->sample_rate,
1995 stream_format->number_channels,
1996 SNDRV_PCM_FORMAT_S32_LE,
1997 stream_format->container_size, 0);
2000 *hda_format = (unsigned short)format_val;
2005 static int dma_reset(struct dma_engine *dma)
2007 struct hda_codec *codec = dma->codec;
2008 struct ca0132_spec *spec = codec->spec;
2011 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2013 status = snd_hda_codec_load_dsp_prepare(codec,
2014 dma->m_converter_format,
2019 spec->dsp_stream_id = status;
2024 static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2028 CA0132_LOG("dma_set_state state=%d\n", state);
2031 case DMA_STATE_STOP:
2041 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2045 static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2047 return dma->dmab->bytes;
2050 static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2052 return dma->dmab->area;
2055 static int dma_xfer(struct dma_engine *dma,
2056 const unsigned int *data,
2059 memcpy(dma->dmab->area, data, count);
2063 static void dma_get_converter_format(
2064 struct dma_engine *dma,
2065 unsigned short *format)
2068 *format = dma->m_converter_format;
2071 static unsigned int dma_get_stream_id(struct dma_engine *dma)
2073 struct ca0132_spec *spec = dma->codec->spec;
2075 return spec->dsp_stream_id;
2080 * CA0132 chip DSP image segment stuffs
2082 struct dsp_image_seg {
2089 static const u32 g_magic_value = 0x4c46584d;
2090 static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2092 static bool is_valid(const struct dsp_image_seg *p)
2094 return p->magic == g_magic_value;
2097 static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2099 return g_chip_addr_magic_value == p->chip_addr;
2102 static bool is_last(const struct dsp_image_seg *p)
2104 return p->count == 0;
2107 static size_t dsp_sizeof(const struct dsp_image_seg *p)
2109 return sizeof(*p) + p->count*sizeof(u32);
2112 static const struct dsp_image_seg *get_next_seg_ptr(
2113 const struct dsp_image_seg *p)
2115 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2119 * CA0132 chip DSP trannsfer stuffs. For DSP download.
2121 #define INVALID_DMA_CHANNEL (~0UL)
2123 static int dspxfr_hci_write(struct hda_codec *codec,
2124 const struct dsp_image_seg *fls)
2130 CTASSERT(fls != NULL && fls->chip_addr == g_chip_addr_magic_value);
2131 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value)
2132 return FAIL_MSG(-1, "hci_write invalid params");
2135 data = (u32 *)(fls->data);
2136 while (SUCCEEDED(status) && count >= 2) {
2137 status = chipio_write(codec, data[0], data[1]);
2139 status = FAIL_MSG(status, "hci_write chipio failed");
2146 static int dspxfr_one_seg(struct hda_codec *codec,
2147 const struct dsp_image_seg *fls,
2149 struct dma_engine *dma_engine,
2150 unsigned int dma_chan,
2151 unsigned int port_map_mask,
2155 bool comm_dma_setup_done = false;
2156 const unsigned int *data;
2157 unsigned int chip_addx;
2158 unsigned int words_to_write;
2159 unsigned int buffer_size_words;
2160 unsigned char *buffer_addx;
2161 unsigned short hda_format;
2162 unsigned int sample_rate_div;
2163 unsigned int sample_rate_mul;
2164 unsigned int num_chans;
2165 unsigned int hda_frame_size_words;
2166 unsigned int remainder_words;
2167 const u32 *data_remainder;
2168 u32 chip_addx_remainder;
2169 unsigned int run_size_words;
2170 const struct dsp_image_seg *hci_write = NULL;
2171 unsigned long timeout;
2174 CTASSERT(fls != NULL);
2177 if (is_hci_prog_list_seg(fls)) {
2179 fls = get_next_seg_ptr(fls);
2182 if (hci_write && (!fls || is_last(fls))) {
2183 CA0132_LOG("hci_write\n");
2184 return dspxfr_hci_write(codec, hci_write);
2187 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2188 CA0132_LOG("Invalid Params\n");
2193 chip_addx = fls->chip_addr,
2194 words_to_write = fls->count;
2196 if (!words_to_write)
2197 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2199 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2201 if (!UC_RANGE(chip_addx, words_to_write) &&
2202 !X_RANGE_ALL(chip_addx, words_to_write) &&
2203 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2204 CA0132_LOG("Invalid chip_addx Params\n");
2208 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2211 buffer_addx = dma_get_buffer_addr(dma_engine);
2213 if (buffer_addx == NULL)
2214 status = FAIL_MSG(-1, "dma_engine buffer NULL");
2216 dma_get_converter_format(dma_engine, &hda_format);
2217 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2218 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2219 num_chans = get_hdafmt_chs(hda_format) + 1;
2221 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2222 (num_chans * sample_rate_mul / sample_rate_div));
2224 buffer_size_words = min(buffer_size_words,
2225 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2227 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2229 "chpadr=0x%08x frmsz=%u nchan=%u "
2230 "rate_mul=%u div=%u bufsz=%u\n",
2231 chip_addx, hda_frame_size_words, num_chans,
2232 sample_rate_mul, sample_rate_div, buffer_size_words);
2234 CTASSERT(SUCCEEDED(status));
2235 CTASSERT(buffer_addx != NULL);
2236 CTASSERT(buffer_size_words >= hda_frame_size_words);
2237 CTASSERT(hda_frame_size_words > 0);
2239 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2240 (buffer_size_words < hda_frame_size_words)) {
2241 status = FAIL_MSG(-1, "dspxfr_one_seg:failed");
2247 remainder_words = words_to_write % hda_frame_size_words;
2248 data_remainder = data;
2249 chip_addx_remainder = chip_addx;
2251 data += remainder_words;
2252 chip_addx += remainder_words*sizeof(u32);
2253 words_to_write -= remainder_words;
2255 while ((words_to_write != 0) && SUCCEEDED(status)) {
2256 run_size_words = min(buffer_size_words, words_to_write);
2257 CA0132_LOG("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2258 words_to_write, run_size_words, remainder_words);
2259 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2260 if (!comm_dma_setup_done && SUCCEEDED(status)) {
2261 status = dsp_dma_stop(codec, dma_chan, ovly);
2262 if (SUCCEEDED(status))
2263 status = dsp_dma_setup_common(codec, chip_addx,
2264 dma_chan, port_map_mask, ovly);
2265 comm_dma_setup_done = true;
2268 if (SUCCEEDED(status))
2269 status = dsp_dma_setup(codec, chip_addx,
2270 run_size_words, dma_chan);
2271 if (SUCCEEDED(status))
2272 status = dsp_dma_start(codec, dma_chan, ovly);
2273 if (SUCCEEDED(status) && !dsp_is_dma_active(codec, dma_chan))
2274 status = FAIL_MSG(-1, "dspxfr:DMA did not start");
2275 if (SUCCEEDED(status))
2276 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2277 if (SUCCEEDED(status)) {
2278 if (remainder_words != 0) {
2279 status = chipio_write_multiple(codec,
2280 chip_addx_remainder,
2283 remainder_words = 0;
2286 status = dspxfr_hci_write(codec, hci_write);
2290 timeout = jiffies + msecs_to_jiffies(2000);
2292 dma_active = dsp_is_dma_active(codec, dma_chan);
2296 } while (time_before(jiffies, timeout));
2299 CA0132_DSP_LOG("+++++ DMA complete");
2300 dma_set_state(dma_engine, DMA_STATE_STOP);
2301 status = dma_reset(dma_engine);
2306 CTASSERT(run_size_words <= words_to_write);
2307 data += run_size_words;
2308 chip_addx += run_size_words*sizeof(u32);
2309 words_to_write -= run_size_words;
2312 if (SUCCEEDED(status) && (remainder_words != 0)) {
2313 status = chipio_write_multiple(codec, chip_addx_remainder,
2314 data_remainder, remainder_words);
2320 static int dspxfr_image(struct hda_codec *codec,
2321 const struct dsp_image_seg *fls_data,
2322 unsigned int reloc, struct hda_stream_format *format,
2325 struct ca0132_spec *spec = codec->spec;
2327 unsigned short hda_format = 0;
2328 unsigned int response;
2329 unsigned char stream_id = 0;
2330 struct dma_engine *dma_engine;
2331 unsigned int dma_chan;
2332 unsigned int port_map_mask;
2334 CTASSERT(fls_data != NULL);
2335 if (fls_data == NULL)
2338 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
2344 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2345 if (!dma_engine->dmab) {
2347 goto free_dma_engine;
2350 dma_engine->codec = codec;
2351 dma_convert_to_hda_format(format, &hda_format);
2352 dma_engine->m_converter_format = hda_format;
2353 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2354 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2358 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2359 hda_format, &response);
2361 if (FAILED(status)) {
2362 status = FAIL_MSG(status, "set converter format fail");
2363 goto free_dma_engine_and_dmab;
2366 status = snd_hda_codec_load_dsp_prepare(codec,
2367 dma_engine->m_converter_format,
2368 dma_engine->buf_size,
2372 spec->dsp_stream_id = status;
2375 status = dspio_alloc_dma_chan(codec, &dma_chan);
2376 if (FAILED(status)) {
2377 status = FAIL_MSG(status, "alloc dmachan fail");
2378 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2384 status = dsp_allocate_ports_format(codec, hda_format,
2386 if (FAILED(status)) {
2387 status = FAIL_MSG(status, "alloc parts fail");
2391 stream_id = dma_get_stream_id(dma_engine);
2392 status = codec_set_converter_stream_channel(codec,
2393 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2394 if (FAILED(status)) {
2395 status = FAIL_MSG(status, "set stream chan fail");
2399 while (SUCCEEDED(status) && (fls_data != NULL) &&
2400 !is_last(fls_data)) {
2401 if (!is_valid(fls_data)) {
2402 status = FAIL_MSG(-1, "FLS check fail");
2405 status = dspxfr_one_seg(codec, fls_data, reloc,
2406 dma_engine, dma_chan,
2407 port_map_mask, ovly);
2408 if (is_hci_prog_list_seg(fls_data))
2409 fls_data = get_next_seg_ptr(fls_data);
2411 CTASSERT(fls_data != NULL);
2412 if ((fls_data != NULL) && !is_last(fls_data))
2413 fls_data = get_next_seg_ptr(fls_data);
2415 CTASSERT(fls_data != NULL);
2418 if (port_map_mask != 0)
2419 status = dsp_free_ports(codec);
2424 status = codec_set_converter_stream_channel(codec,
2425 WIDGET_CHIP_CTRL, 0, 0, &response);
2428 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2429 status = dspio_free_dma_chan(codec, dma_chan);
2431 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2432 free_dma_engine_and_dmab:
2433 kfree(dma_engine->dmab);
2441 * CA0132 DSP download stuffs.
2443 static void dspload_post_setup(struct hda_codec *codec)
2445 CA0132_DSP_LOG("---- dspload_post_setup ------");
2447 /*set DSP speaker to 2.0 configuration*/
2448 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2449 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2451 /*update write pointer*/
2452 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2455 static int dspload_image(struct hda_codec *codec,
2456 const struct dsp_image_seg *fls,
2463 struct hda_stream_format stream_format;
2465 CA0132_DSP_LOG("---- dspload_image begin ------");
2466 if (router_chans == 0) {
2468 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2470 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2473 stream_format.sample_rate = 48000;
2474 stream_format.number_channels = (unsigned short)router_chans;
2476 while (stream_format.number_channels > 16) {
2477 stream_format.sample_rate *= 2;
2478 stream_format.number_channels /= 2;
2481 stream_format.container_size = 32;
2482 stream_format.valid_bits_per_sample = 32;
2485 CA0132_DSP_LOG("Ready to program DMA");
2487 status = dsp_reset(codec);
2492 CA0132_DSP_LOG("dsp_reset() complete");
2493 status = dspxfr_image(codec, fls, reloc, &stream_format, ovly);
2498 CA0132_DSP_LOG("dspxfr_image() complete");
2499 if (autostart && !ovly) {
2500 dspload_post_setup(codec);
2501 status = dsp_set_run_state(codec);
2504 CA0132_DSP_LOG("LOAD FINISHED");
2510 static int dspload_get_speakereq_addx(struct hda_codec *codec,
2515 struct { unsigned short y, x; } speakereq_info;
2516 unsigned int size = sizeof(speakereq_info);
2518 CA0132_DSP_LOG("dspload_get_speakereq_addx() -- begin");
2519 status = dspio_scp(codec, MASTERCONTROL,
2520 MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS,
2521 SCP_GET, NULL, 0, &speakereq_info, &size);
2523 if (FAILED(status)) {
2524 CA0132_DSP_LOG("dspload_get_speakereq_addx: SCP Failed");
2528 *x = speakereq_info.x;
2529 *y = speakereq_info.y;
2530 CA0132_LOG("dspload_get_speakereq_addx: X=0x%x Y=0x%x\n", *x, *y);
2532 CA0132_DSP_LOG("dspload_get_speakereq_addx() -- complete");
2537 static const struct firmware *fw_speq;
2538 static const struct firmware *fw_efx;
2540 static int request_firmware_cached(const struct firmware **firmware_p,
2541 const char *name, struct device *device)
2544 return 0; /* already loaded */
2545 return request_firmware(firmware_p, name, device);
2548 static void release_cached_firmware(void)
2551 release_firmware(fw_speq);
2555 release_firmware(fw_efx);
2560 static int dspload_speakereq(struct hda_codec *codec)
2563 const struct dsp_image_seg *image;
2566 CA0132_DSP_LOG("dspload_speakereq() -- begin");
2568 if (request_firmware_cached(&fw_speq, SPEQ_FILE,
2569 codec->bus->card->dev) != 0)
2572 image = (struct dsp_image_seg *)(fw_speq->data);
2574 status = dspload_get_speakereq_addx(codec, &x, &y);
2578 status = dspload_image(codec, image, 1, y, 0, 8);
2581 CA0132_DSP_LOG("dspload_speakereq() -- complete");
2586 static bool dspload_is_loaded(struct hda_codec *codec)
2588 unsigned int data = 0;
2591 status = chipio_read(codec, 0x40004, &data);
2592 if (FAILED(status) || (data != 1))
2598 static bool dspload_wait_loaded(struct hda_codec *codec)
2600 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
2603 if (dspload_is_loaded(codec)) {
2604 pr_info("DOWNLOAD OK :-) DSP IS RUNNING.\n");
2608 } while (time_before(jiffies, timeout));
2610 pr_err("DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2617 static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2619 int channel_id, int format)
2621 unsigned int oldval, newval;
2627 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2628 "channel=%d, format=0x%x\n",
2629 nid, stream_tag, channel_id, format);
2631 /* update the format-id if changed */
2632 oldval = snd_hda_codec_read(codec, nid, 0,
2633 AC_VERB_GET_STREAM_FORMAT, 0);
2634 if (oldval != format) {
2636 snd_hda_codec_write(codec, nid, 0,
2637 AC_VERB_SET_STREAM_FORMAT,
2641 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2642 newval = (stream_tag << 4) | channel_id;
2643 if (oldval != newval) {
2644 snd_hda_codec_write(codec, nid, 0,
2645 AC_VERB_SET_CHANNEL_STREAMID,
2650 static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2657 CA0132_LOG("ca0132_cleanup_stream: NID=0x%x\n", nid);
2659 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2663 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2664 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2670 static unsigned int ca0132_get_playback_latency(struct hda_codec *codec)
2672 struct ca0132_spec *spec = codec->spec;
2673 unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
2675 if (!dspload_is_loaded(codec))
2678 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
2679 if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
2680 (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
2681 latency += DSP_PLAY_ENHANCEMENT_LATENCY;
2684 if (spec->cur_out_type == SPEAKER_OUT)
2685 latency += DSP_SPEAKER_OUT_LATENCY;
2689 static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2690 struct hda_codec *codec,
2691 unsigned int stream_tag,
2692 unsigned int format,
2693 struct snd_pcm_substream *substream)
2695 struct ca0132_spec *spec = codec->spec;
2696 struct snd_pcm_runtime *runtime = substream->runtime;
2697 unsigned int latency = ca0132_get_playback_latency(codec);
2699 if (spec->dsp_state == DSP_DOWNLOADING) {
2700 spec->dsp_stream_id = stream_tag;
2704 runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
2705 runtime->byte_align) / 1000);
2707 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2711 static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2712 struct hda_codec *codec,
2713 struct snd_pcm_substream *substream)
2715 struct ca0132_spec *spec = codec->spec;
2717 if (spec->dsp_state == DSP_DOWNLOADING)
2720 /*Allow stream some time to flush effects tail*/
2723 ca0132_cleanup_stream(codec, spec->dacs[0]);
2731 static unsigned int ca0132_get_capture_latency(struct hda_codec *codec)
2733 struct ca0132_spec *spec = codec->spec;
2734 unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
2736 if (!dspload_is_loaded(codec))
2739 if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
2740 latency += DSP_CRYSTAL_VOICE_LATENCY;
2744 static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2745 struct hda_codec *codec,
2746 unsigned int stream_tag,
2747 unsigned int format,
2748 struct snd_pcm_substream *substream)
2750 struct ca0132_spec *spec = codec->spec;
2751 struct snd_pcm_runtime *runtime = substream->runtime;
2752 unsigned int latency = ca0132_get_capture_latency(codec);
2754 if (spec->dsp_state == DSP_DOWNLOADING)
2757 runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
2758 runtime->byte_align) / 1000);
2760 ca0132_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2764 static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2765 struct hda_codec *codec,
2766 struct snd_pcm_substream *substream)
2768 struct ca0132_spec *spec = codec->spec;
2770 if (spec->dsp_state == DSP_DOWNLOADING)
2773 ca0132_cleanup_stream(codec, hinfo->nid);
2783 * Mixer controls helpers.
2785 #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2786 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2788 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2789 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2790 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2791 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2792 .info = ca0132_volume_info, \
2793 .get = ca0132_volume_get, \
2794 .put = ca0132_volume_put, \
2795 .tlv = { .c = ca0132_volume_tlv }, \
2796 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2798 #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2799 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2801 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2802 .info = snd_hda_mixer_amp_switch_info, \
2803 .get = ca0132_switch_get, \
2804 .put = ca0132_switch_put, \
2805 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2808 #define CA0132_CODEC_VOL(xname, nid, dir) \
2809 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2810 #define CA0132_CODEC_MUTE(xname, nid, dir) \
2811 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2814 /* The followings are for tuning of products */
2815 #ifdef ENABLE_TUNING_CONTROLS
2817 static unsigned int voice_focus_vals_lookup[] = {
2818 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
2819 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
2820 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
2821 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
2822 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
2823 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
2824 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
2825 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
2826 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
2827 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
2828 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
2829 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
2830 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
2831 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
2832 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
2833 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
2834 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
2835 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
2836 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
2837 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
2838 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
2839 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
2840 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
2841 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
2842 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
2843 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
2844 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
2847 static unsigned int mic_svm_vals_lookup[] = {
2848 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
2849 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
2850 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
2851 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
2852 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
2853 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
2854 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
2855 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
2856 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
2857 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
2858 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
2859 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
2860 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
2861 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
2862 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
2863 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
2864 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
2867 static unsigned int equalizer_vals_lookup[] = {
2868 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
2869 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
2870 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
2871 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
2872 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
2873 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
2874 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
2875 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
2879 static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
2880 unsigned int *lookup, int idx)
2884 for (i = 0; i < TUNING_CTLS_COUNT; i++)
2885 if (nid == ca0132_tuning_ctls[i].nid)
2888 snd_hda_power_up(codec);
2889 dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
2890 ca0132_tuning_ctls[i].req,
2891 &(lookup[idx]), sizeof(unsigned int));
2892 snd_hda_power_down(codec);
2897 static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_value *ucontrol)
2900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2901 struct ca0132_spec *spec = codec->spec;
2902 hda_nid_t nid = get_amp_nid(kcontrol);
2903 long *valp = ucontrol->value.integer.value;
2904 int idx = nid - TUNING_CTL_START_NID;
2906 *valp = spec->cur_ctl_vals[idx];
2910 static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_info *uinfo)
2913 int chs = get_amp_channels(kcontrol);
2914 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2915 uinfo->count = chs == 3 ? 2 : 1;
2916 uinfo->value.integer.min = 20;
2917 uinfo->value.integer.max = 180;
2918 uinfo->value.integer.step = 1;
2923 static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2926 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2927 struct ca0132_spec *spec = codec->spec;
2928 hda_nid_t nid = get_amp_nid(kcontrol);
2929 long *valp = ucontrol->value.integer.value;
2932 idx = nid - TUNING_CTL_START_NID;
2934 if (spec->cur_ctl_vals[idx] == *valp)
2937 spec->cur_ctl_vals[idx] = *valp;
2940 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
2945 static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
2946 struct snd_ctl_elem_info *uinfo)
2948 int chs = get_amp_channels(kcontrol);
2949 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2950 uinfo->count = chs == 3 ? 2 : 1;
2951 uinfo->value.integer.min = 0;
2952 uinfo->value.integer.max = 100;
2953 uinfo->value.integer.step = 1;
2958 static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
2959 struct snd_ctl_elem_value *ucontrol)
2961 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2962 struct ca0132_spec *spec = codec->spec;
2963 hda_nid_t nid = get_amp_nid(kcontrol);
2964 long *valp = ucontrol->value.integer.value;
2967 idx = nid - TUNING_CTL_START_NID;
2969 if (spec->cur_ctl_vals[idx] == *valp)
2972 spec->cur_ctl_vals[idx] = *valp;
2975 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
2980 static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_info *uinfo)
2983 int chs = get_amp_channels(kcontrol);
2984 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2985 uinfo->count = chs == 3 ? 2 : 1;
2986 uinfo->value.integer.min = 0;
2987 uinfo->value.integer.max = 48;
2988 uinfo->value.integer.step = 1;
2993 static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2996 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2997 struct ca0132_spec *spec = codec->spec;
2998 hda_nid_t nid = get_amp_nid(kcontrol);
2999 long *valp = ucontrol->value.integer.value;
3002 idx = nid - TUNING_CTL_START_NID;
3004 if (spec->cur_ctl_vals[idx] == *valp)
3007 spec->cur_ctl_vals[idx] = *valp;
3010 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3015 DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3016 DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
3018 static int add_tuning_control(struct hda_codec *codec,
3019 hda_nid_t pnid, hda_nid_t nid,
3020 const char *name, int dir)
3023 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3024 struct snd_kcontrol_new knew =
3025 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3027 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3028 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3033 knew.info = voice_focus_ctl_info;
3034 knew.get = tuning_ctl_get;
3035 knew.put = voice_focus_ctl_put;
3036 knew.tlv.p = voice_focus_db_scale;
3039 knew.info = mic_svm_ctl_info;
3040 knew.get = tuning_ctl_get;
3041 knew.put = mic_svm_ctl_put;
3044 knew.info = equalizer_ctl_info;
3045 knew.get = tuning_ctl_get;
3046 knew.put = equalizer_ctl_put;
3047 knew.tlv.p = eq_db_scale;
3052 knew.private_value =
3053 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3054 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3055 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3058 static int add_tuning_ctls(struct hda_codec *codec)
3063 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3064 err = add_tuning_control(codec,
3065 ca0132_tuning_ctls[i].parent_nid,
3066 ca0132_tuning_ctls[i].nid,
3067 ca0132_tuning_ctls[i].name,
3068 ca0132_tuning_ctls[i].direct);
3076 static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3078 struct ca0132_spec *spec = codec->spec;
3081 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3082 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3083 /* SVM level defaults to 0.74. */
3084 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3086 /* EQ defaults to 0dB. */
3087 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3088 spec->cur_ctl_vals[i] = 24;
3092 static int ca0132_select_out(struct hda_codec *codec)
3094 struct ca0132_spec *spec = codec->spec;
3095 unsigned int pin_ctl;
3101 CA0132_LOG("ca0132_select_out\n");
3103 snd_hda_power_up(codec);
3105 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3108 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
3111 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3114 spec->cur_out_type = HEADPHONE_OUT;
3116 spec->cur_out_type = SPEAKER_OUT;
3118 if (spec->cur_out_type == SPEAKER_OUT) {
3119 CA0132_LOG("ca0132_select_out speaker\n");
3120 /*speaker out config*/
3122 err = dspio_set_param(codec, 0x80, 0x04,
3123 &tmp, sizeof(unsigned int));
3126 /*enable speaker EQ*/
3128 err = dspio_set_param(codec, 0x8f, 0x00,
3129 &tmp, sizeof(unsigned int));
3134 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3135 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3136 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3137 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3138 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3139 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3140 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3141 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3143 /* disable headphone node */
3144 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3145 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3146 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3147 AC_VERB_SET_PIN_WIDGET_CONTROL,
3149 /* enable speaker node */
3150 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3151 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3152 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3153 AC_VERB_SET_PIN_WIDGET_CONTROL,
3156 CA0132_LOG("ca0132_select_out hp\n");
3157 /*headphone out config*/
3159 err = dspio_set_param(codec, 0x80, 0x04,
3160 &tmp, sizeof(unsigned int));
3163 /*disable speaker EQ*/
3165 err = dspio_set_param(codec, 0x8f, 0x00,
3166 &tmp, sizeof(unsigned int));
3171 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3172 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3173 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3174 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3175 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3176 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3177 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3178 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3180 /* disable speaker*/
3181 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3182 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3183 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3184 AC_VERB_SET_PIN_WIDGET_CONTROL,
3186 /* enable headphone*/
3187 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3188 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3189 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3190 AC_VERB_SET_PIN_WIDGET_CONTROL,
3195 snd_hda_power_down(codec);
3197 return err < 0 ? err : 0;
3200 static void ca0132_unsol_hp_delayed(struct work_struct *work)
3202 struct ca0132_spec *spec = container_of(
3203 to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
3204 ca0132_select_out(spec->codec);
3205 snd_hda_jack_report_sync(spec->codec);
3208 static void ca0132_set_dmic(struct hda_codec *codec, int enable);
3209 static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
3210 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
3212 static int ca0132_set_vipsource(struct hda_codec *codec, int val)
3214 struct ca0132_spec *spec = codec->spec;
3217 if (!dspload_is_loaded(codec))
3220 /* if CrystalVoice if off, vipsource should be 0 */
3221 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]
3223 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3224 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3225 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3226 if (spec->cur_mic_type == DIGITAL_MIC)
3230 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
3232 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
3234 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3235 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3236 if (spec->cur_mic_type == DIGITAL_MIC)
3240 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
3242 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
3244 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3250 static int ca0132_select_mic(struct hda_codec *codec)
3252 struct ca0132_spec *spec = codec->spec;
3256 CA0132_LOG("ca0132_select_mic\n");
3258 snd_hda_power_up(codec);
3260 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3263 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3266 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3269 spec->cur_mic_type = LINE_MIC_IN;
3271 spec->cur_mic_type = DIGITAL_MIC;
3273 if (spec->cur_mic_type == DIGITAL_MIC) {
3274 /* enable digital Mic */
3275 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3276 ca0132_set_dmic(codec, 1);
3277 ca0132_mic_boost_set(codec, 0);
3278 /* set voice focus */
3279 ca0132_effects_set(codec, VOICE_FOCUS,
3280 spec->effects_switch
3281 [VOICE_FOCUS - EFFECT_START_NID]);
3283 /* disable digital Mic */
3284 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3285 ca0132_set_dmic(codec, 0);
3286 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3287 /* disable voice focus */
3288 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3291 snd_hda_power_down(codec);
3297 * Check if VNODE settings take effect immediately.
3299 static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3301 hda_nid_t *shared_nid)
3303 struct ca0132_spec *spec = codec->spec;
3305 bool effective = false;
3309 nid = spec->shared_out_nid;
3313 nid = spec->shared_mic_nid;
3320 if (effective && shared_nid)
3327 * The following functions are control change helpers.
3328 * They return 0 if no changed. Return 1 if changed.
3330 static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3332 struct ca0132_spec *spec = codec->spec;
3335 /* based on CrystalVoice state to enable VoiceFX. */
3337 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3338 FLOAT_ONE : FLOAT_ZERO;
3343 dspio_set_param(codec, ca0132_voicefx.mid,
3344 ca0132_voicefx.reqs[0], &tmp, sizeof(unsigned int));
3349 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3351 struct ca0132_spec *spec = codec->spec;
3353 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3355 int idx = nid - EFFECT_START_NID;
3357 if ((idx < 0) || (idx >= num_fx))
3358 return 0; /* no changed */
3360 /* for out effect, qualify with PE */
3361 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3362 /* if PE if off, turn off out effects. */
3363 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3367 /* for in effect, qualify with CrystalVoice */
3368 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3369 /* if CrystalVoice if off, turn off in effects. */
3370 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3373 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3374 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3378 CA0132_LOG("ca0132_effect_set: nid=0x%x, val=%ld\n", nid, val);
3380 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3381 err = dspio_set_param(codec, ca0132_effects[idx].mid,
3382 ca0132_effects[idx].reqs[0],
3383 &on, sizeof(unsigned int));
3385 return 0; /* no changed */
3390 static int ca0132_pe_switch_set(struct hda_codec *codec)
3392 struct ca0132_spec *spec = codec->spec;
3396 CA0132_LOG("ca0132_pe_switch_set: val=%ld\n",
3397 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3399 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3400 nid = OUT_EFFECT_START_NID;
3401 /* PE affects all out effects */
3402 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3403 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3408 static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3410 struct ca0132_spec *spec = codec->spec;
3414 CA0132_LOG("ca0132_cvoice_switch_set: val=%ld\n",
3415 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3417 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3418 nid = IN_EFFECT_START_NID;
3419 /* CrystalVoice affects all in effects */
3420 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3421 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3423 /* including VoiceFX */
3424 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3426 /* set correct vipsource */
3427 ret |= ca0132_set_vipsource(codec, 1);
3431 static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3433 struct ca0132_spec *spec = codec->spec;
3437 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3438 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3440 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3441 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3446 static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3447 struct snd_ctl_elem_value *ucontrol)
3449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3450 hda_nid_t nid = get_amp_nid(kcontrol);
3451 hda_nid_t shared_nid = 0;
3454 struct ca0132_spec *spec = codec->spec;
3457 if (nid == VNID_HP_SEL) {
3459 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3461 ca0132_select_out(codec);
3465 if (nid == VNID_AMIC1_SEL) {
3467 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3469 ca0132_select_mic(codec);
3473 if (nid == VNID_HP_ASEL) {
3474 ca0132_select_out(codec);
3478 if (nid == VNID_AMIC1_ASEL) {
3479 ca0132_select_mic(codec);
3483 /* if effective conditions, then update hw immediately. */
3484 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3486 int dir = get_amp_direction(kcontrol);
3487 int ch = get_amp_channels(kcontrol);
3490 mutex_lock(&codec->control_mutex);
3491 pval = kcontrol->private_value;
3492 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3494 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3495 kcontrol->private_value = pval;
3496 mutex_unlock(&codec->control_mutex);
3501 /* End of control change helpers. */
3503 static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3504 struct snd_ctl_elem_info *uinfo)
3506 unsigned int items = sizeof(ca0132_voicefx_presets)
3507 / sizeof(struct ct_voicefx_preset);
3509 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3511 uinfo->value.enumerated.items = items;
3512 if (uinfo->value.enumerated.item >= items)
3513 uinfo->value.enumerated.item = items - 1;
3514 strcpy(uinfo->value.enumerated.name,
3515 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3519 static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
3520 struct snd_ctl_elem_value *ucontrol)
3522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3523 struct ca0132_spec *spec = codec->spec;
3525 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
3529 static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
3530 struct snd_ctl_elem_value *ucontrol)
3532 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3533 struct ca0132_spec *spec = codec->spec;
3535 int sel = ucontrol->value.enumerated.item[0];
3536 unsigned int items = sizeof(ca0132_voicefx_presets)
3537 / sizeof(struct ct_voicefx_preset);
3542 CA0132_LOG("ca0132_voicefx_put: sel=%d, preset=%s\n",
3543 sel, ca0132_voicefx_presets[sel].name);
3547 * Default needs to qualify with CrystalVoice state.
3549 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3550 err = dspio_set_param(codec, ca0132_voicefx.mid,
3551 ca0132_voicefx.reqs[i],
3552 &(ca0132_voicefx_presets[sel].vals[i]),
3553 sizeof(unsigned int));
3559 spec->voicefx_val = sel;
3560 /* enable voice fx */
3561 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3567 static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3568 struct snd_ctl_elem_value *ucontrol)
3570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3571 struct ca0132_spec *spec = codec->spec;
3572 hda_nid_t nid = get_amp_nid(kcontrol);
3573 int ch = get_amp_channels(kcontrol);
3574 long *valp = ucontrol->value.integer.value;
3577 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3579 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3583 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3589 /* effects, include PE and CrystalVoice */
3590 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3591 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3596 if (nid == spec->input_pins[0]) {
3597 *valp = spec->cur_mic_boost;
3604 static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3605 struct snd_ctl_elem_value *ucontrol)
3607 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3608 struct ca0132_spec *spec = codec->spec;
3609 hda_nid_t nid = get_amp_nid(kcontrol);
3610 int ch = get_amp_channels(kcontrol);
3611 long *valp = ucontrol->value.integer.value;
3614 CA0132_LOG("ca0132_switch_put: nid=0x%x, val=%ld\n", nid, *valp);
3616 snd_hda_power_up(codec);
3618 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3620 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3624 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3627 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3632 if (nid == PLAY_ENHANCEMENT) {
3633 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3634 changed = ca0132_pe_switch_set(codec);
3639 if (nid == CRYSTAL_VOICE) {
3640 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3641 changed = ca0132_cvoice_switch_set(codec);
3645 /* out and in effects */
3646 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3647 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3648 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3649 changed = ca0132_effects_set(codec, nid, *valp);
3654 if (nid == spec->input_pins[0]) {
3655 spec->cur_mic_boost = *valp;
3657 /* Mic boost does not apply to Digital Mic */
3658 if (spec->cur_mic_type != DIGITAL_MIC)
3659 changed = ca0132_mic_boost_set(codec, *valp);
3664 snd_hda_power_down(codec);
3671 static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3672 struct snd_ctl_elem_info *uinfo)
3674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3675 struct ca0132_spec *spec = codec->spec;
3676 hda_nid_t nid = get_amp_nid(kcontrol);
3677 int ch = get_amp_channels(kcontrol);
3678 int dir = get_amp_direction(kcontrol);
3684 /* follow shared_out info */
3685 nid = spec->shared_out_nid;
3686 mutex_lock(&codec->control_mutex);
3687 pval = kcontrol->private_value;
3688 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3689 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3690 kcontrol->private_value = pval;
3691 mutex_unlock(&codec->control_mutex);
3694 /* follow shared_mic info */
3695 nid = spec->shared_mic_nid;
3696 mutex_lock(&codec->control_mutex);
3697 pval = kcontrol->private_value;
3698 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3699 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3700 kcontrol->private_value = pval;
3701 mutex_unlock(&codec->control_mutex);
3704 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3709 static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
3710 struct snd_ctl_elem_value *ucontrol)
3712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3713 struct ca0132_spec *spec = codec->spec;
3714 hda_nid_t nid = get_amp_nid(kcontrol);
3715 int ch = get_amp_channels(kcontrol);
3716 long *valp = ucontrol->value.integer.value;
3718 /* store the left and right volume */
3720 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3724 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3730 static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
3731 struct snd_ctl_elem_value *ucontrol)
3733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3734 struct ca0132_spec *spec = codec->spec;
3735 hda_nid_t nid = get_amp_nid(kcontrol);
3736 int ch = get_amp_channels(kcontrol);
3737 long *valp = ucontrol->value.integer.value;
3738 hda_nid_t shared_nid = 0;
3742 /* store the left and right volume */
3744 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3748 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3752 /* if effective conditions, then update hw immediately. */
3753 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3755 int dir = get_amp_direction(kcontrol);
3758 snd_hda_power_up(codec);
3759 mutex_lock(&codec->control_mutex);
3760 pval = kcontrol->private_value;
3761 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3763 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3764 kcontrol->private_value = pval;
3765 mutex_unlock(&codec->control_mutex);
3766 snd_hda_power_down(codec);
3772 static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3773 unsigned int size, unsigned int __user *tlv)
3775 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3776 struct ca0132_spec *spec = codec->spec;
3777 hda_nid_t nid = get_amp_nid(kcontrol);
3778 int ch = get_amp_channels(kcontrol);
3779 int dir = get_amp_direction(kcontrol);
3785 /* follow shared_out tlv */
3786 nid = spec->shared_out_nid;
3787 mutex_lock(&codec->control_mutex);
3788 pval = kcontrol->private_value;
3789 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3790 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3791 kcontrol->private_value = pval;
3792 mutex_unlock(&codec->control_mutex);
3795 /* follow shared_mic tlv */
3796 nid = spec->shared_mic_nid;
3797 mutex_lock(&codec->control_mutex);
3798 pval = kcontrol->private_value;
3799 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3800 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3801 kcontrol->private_value = pval;
3802 mutex_unlock(&codec->control_mutex);
3805 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3810 static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3811 const char *pfx, int dir)
3814 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3815 struct snd_kcontrol_new knew =
3816 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3817 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
3818 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3821 static int add_voicefx(struct hda_codec *codec)
3823 struct snd_kcontrol_new knew =
3824 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3825 VOICEFX, 1, 0, HDA_INPUT);
3826 knew.info = ca0132_voicefx_info;
3827 knew.get = ca0132_voicefx_get;
3828 knew.put = ca0132_voicefx_put;
3829 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
3833 * When changing Node IDs for Mixer Controls below, make sure to update
3834 * Node IDs in ca0132_config() as well.
3836 static struct snd_kcontrol_new ca0132_mixer[] = {
3837 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3838 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3839 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3840 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3841 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3842 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3843 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3844 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3845 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3846 0x12, 1, HDA_INPUT),
3847 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3848 VNID_HP_SEL, 1, HDA_OUTPUT),
3849 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3850 VNID_AMIC1_SEL, 1, HDA_INPUT),
3851 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
3852 VNID_HP_ASEL, 1, HDA_OUTPUT),
3853 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
3854 VNID_AMIC1_ASEL, 1, HDA_INPUT),
3858 static int ca0132_build_controls(struct hda_codec *codec)
3860 struct ca0132_spec *spec = codec->spec;
3864 /* Add Mixer controls */
3865 for (i = 0; i < spec->num_mixers; i++) {
3866 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
3871 /* Add in and out effects controls.
3872 * VoiceFX, PE and CrystalVoice are added separately.
3874 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3875 for (i = 0; i < num_fx; i++) {
3876 err = add_fx_switch(codec, ca0132_effects[i].nid,
3877 ca0132_effects[i].name,
3878 ca0132_effects[i].direct);
3883 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
3887 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
3893 #ifdef ENABLE_TUNING_CONTROLS
3894 add_tuning_ctls(codec);
3897 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3907 static struct hda_pcm_stream ca0132_pcm_analog_playback = {
3912 .prepare = ca0132_playback_pcm_prepare,
3913 .cleanup = ca0132_playback_pcm_cleanup
3917 static struct hda_pcm_stream ca0132_pcm_analog_capture = {
3922 .prepare = ca0132_capture_pcm_prepare,
3923 .cleanup = ca0132_capture_pcm_cleanup
3927 static int ca0132_build_pcms(struct hda_codec *codec)
3929 struct ca0132_spec *spec = codec->spec;
3930 struct hda_pcm *info = spec->pcm_rec;
3932 codec->pcm_info = info;
3933 codec->num_pcms = 0;
3935 info->name = "CA0132 Analog";
3936 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
3937 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
3938 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3939 spec->multiout.max_channels;
3940 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
3941 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
3942 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
3946 info->name = "CA0132 Analog Mic-In2";
3947 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
3948 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
3949 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
3953 info->name = "CA0132 What U Hear";
3954 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
3955 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
3956 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
3962 static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
3965 snd_hda_codec_write(codec, pin, 0,
3966 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
3967 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3968 snd_hda_codec_write(codec, pin, 0,
3969 AC_VERB_SET_AMP_GAIN_MUTE,
3973 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3977 static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
3980 snd_hda_codec_write(codec, pin, 0,
3981 AC_VERB_SET_PIN_WIDGET_CONTROL,
3983 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
3984 snd_hda_codec_write(codec, pin, 0,
3985 AC_VERB_SET_AMP_GAIN_MUTE,
3989 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3992 /* init to 0 dB and unmute. */
3993 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
3994 HDA_AMP_VOLMASK, 0x5a);
3995 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4000 static void ca0132_init_unsol(struct hda_codec *codec)
4002 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
4003 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
4006 static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
4010 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
4011 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
4012 snd_hda_override_amp_caps(codec, nid, dir, caps);
4015 static void ca0132_set_dmic(struct hda_codec *codec, int enable)
4017 struct ca0132_spec *spec = codec->spec;
4020 unsigned int oldval;
4022 CA0132_LOG("ca0132_set_dmic: enable=%d\n", enable);
4024 /* Check if Mic1 is streaming, if so, stop streaming */
4025 oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
4026 AC_VERB_GET_CONV, 0);
4028 snd_hda_codec_write(codec, spec->adcs[0], 0,
4029 AC_VERB_SET_CHANNEL_STREAMID,
4032 ca0132_set_vipsource(codec, 0);
4034 /* set DMic input as 2-ch */
4036 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4038 val = spec->dmic_ctl;
4040 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4041 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4043 if (!(spec->dmic_ctl & 0x20))
4044 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
4046 /* set AMic input as mono */
4048 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4050 val = spec->dmic_ctl;
4051 /* clear bit7 and bit5 to disable dmic */
4053 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4054 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4056 if (!(spec->dmic_ctl & 0x20))
4057 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
4059 ca0132_set_vipsource(codec, 1);
4061 /* Restore the previous stream and channel */
4063 snd_hda_codec_write(codec, spec->adcs[0], 0,
4064 AC_VERB_SET_CHANNEL_STREAMID,
4068 static void ca0132_init_dmic(struct hda_codec *codec)
4070 struct ca0132_spec *spec = codec->spec;
4073 /* Setup Digital Mic here, but don't enable.
4074 * Enable based on jack detect.
4077 /* MCLK uses MPIO1, set to enable.
4078 * Bit 2-0: MPIO select
4079 * Bit 3: set to disable
4083 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4084 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
4086 /* Data1 uses MPIO3. Data2 not use
4087 * Bit 2-0: Data1 MPIO select
4088 * Bit 3: set disable Data1
4089 * Bit 6-4: Data2 MPIO select
4090 * Bit 7: set disable Data2
4093 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4094 VENDOR_CHIPIO_DMIC_PIN_SET, val);
4096 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
4097 * Bit 3-0: Channel mask
4098 * Bit 4: set for 48KHz, clear for 32KHz
4100 * Bit 6: set to select Data2, clear for Data1
4101 * Bit 7: set to enable DMic, clear for AMic
4104 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
4105 spec->dmic_ctl = val;
4106 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4107 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4110 static void ca0132_init_analog_mic2(struct hda_codec *codec)
4112 struct ca0132_spec *spec = codec->spec;
4114 mutex_lock(&spec->chipio_mutex);
4115 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4116 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
4117 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4118 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4119 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4120 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4121 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4122 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
4123 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4124 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4125 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4126 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4127 mutex_unlock(&spec->chipio_mutex);
4130 static void ca0132_refresh_widget_caps(struct hda_codec *codec)
4132 struct ca0132_spec *spec = codec->spec;
4136 CA0132_LOG("ca0132_refresh_widget_caps.\n");
4137 nid = codec->start_nid;
4138 for (i = 0; i < codec->num_nodes; i++, nid++)
4139 codec->wcaps[i] = snd_hda_param_read(codec, nid,
4140 AC_PAR_AUDIO_WIDGET_CAP);
4142 for (i = 0; i < spec->multiout.num_dacs; i++)
4143 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
4145 for (i = 0; i < spec->num_outputs; i++)
4146 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
4148 for (i = 0; i < spec->num_inputs; i++) {
4149 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
4150 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
4154 static void ca0132_setup_defaults(struct hda_codec *codec)
4160 if (!dspload_is_loaded(codec))
4163 /* out, in effects + voicefx */
4164 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
4165 for (idx = 0; idx < num_fx; idx++) {
4166 for (i = 0; i <= ca0132_effects[idx].params; i++) {
4167 dspio_set_param(codec, ca0132_effects[idx].mid,
4168 ca0132_effects[idx].reqs[i],
4169 &(ca0132_effects[idx].def_vals[i]),
4170 sizeof(unsigned int));
4174 /*remove DSP headroom*/
4176 dspio_set_param(codec, 0x96, 0x3C, &tmp, sizeof(unsigned int));
4178 /*set speaker EQ bypass attenuation*/
4179 dspio_set_param(codec, 0x8f, 0x01, &tmp, sizeof(unsigned int));
4181 /* set AMic1 and AMic2 as mono mic */
4183 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4184 dspio_set_param(codec, 0x80, 0x01, &tmp, sizeof(unsigned int));
4186 /* set AMic1 as CrystalVoice input */
4188 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
4190 /* set WUH source */
4192 dspio_set_param(codec, 0x31, 0x00, &tmp, sizeof(unsigned int));
4195 static void ca0132_init_flags(struct hda_codec *codec)
4197 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4198 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4199 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4200 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4201 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4202 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4205 static void ca0132_init_params(struct hda_codec *codec)
4207 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4208 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4211 static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4213 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4214 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4215 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4216 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4217 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4218 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4220 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4221 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4222 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4225 static bool ca0132_download_dsp_images(struct hda_codec *codec)
4227 bool dsp_loaded = false;
4228 const struct dsp_image_seg *dsp_os_image;
4230 if (request_firmware_cached(&fw_efx, EFX_FILE,
4231 codec->bus->card->dev) != 0)
4234 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
4235 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4236 dsp_loaded = dspload_wait_loaded(codec);
4239 dspload_speakereq(codec);
4244 static void ca0132_download_dsp(struct hda_codec *codec)
4246 struct ca0132_spec *spec = codec->spec;
4248 spec->dsp_state = DSP_DOWNLOAD_INIT;
4250 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4251 chipio_enable_clocks(codec);
4252 spec->dsp_state = DSP_DOWNLOADING;
4253 if (!ca0132_download_dsp_images(codec))
4254 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4256 spec->dsp_state = DSP_DOWNLOADED;
4259 if (spec->dsp_state == DSP_DOWNLOADED)
4260 ca0132_set_dsp_msr(codec, true);
4263 static void ca0132_process_dsp_response(struct hda_codec *codec)
4265 struct ca0132_spec *spec = codec->spec;
4267 CA0132_LOG("ca0132_process_dsp_response\n");
4268 if (spec->wait_scp) {
4269 if (dspio_get_response_data(codec) >= 0)
4273 dspio_clear_response_queue(codec);
4276 static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4278 struct ca0132_spec *spec = codec->spec;
4279 CA0132_LOG("ca0132_unsol_event: 0x%x\n", res);
4282 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4283 ca0132_process_dsp_response(codec);
4285 res = snd_hda_jack_get_action(codec,
4286 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4288 CA0132_LOG("snd_hda_jack_get_action: 0x%x\n", res);
4292 cancel_delayed_work_sync(&spec->unsol_hp_work);
4293 queue_delayed_work(codec->bus->workq,
4294 &spec->unsol_hp_work,
4295 msecs_to_jiffies(250));
4297 case UNSOL_TAG_AMIC1:
4298 ca0132_select_mic(codec);
4299 snd_hda_jack_report_sync(codec);
4311 /* Sends before DSP download. */
4312 static struct hda_verb ca0132_base_init_verbs[] = {
4313 /*enable ct extension*/
4314 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4315 /*enable DSP node unsol, needed for DSP download*/
4316 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4321 static struct hda_verb ca0132_base_exit_verbs[] = {
4323 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4324 /*disable ct extension*/
4325 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4329 /* Other verbs tables. Sends after DSP download. */
4330 static struct hda_verb ca0132_init_verbs0[] = {
4331 /* chip init verbs */
4332 {0x15, 0x70D, 0xF0},
4333 {0x15, 0x70E, 0xFE},
4334 {0x15, 0x707, 0x75},
4335 {0x15, 0x707, 0xD3},
4336 {0x15, 0x707, 0x09},
4337 {0x15, 0x707, 0x53},
4338 {0x15, 0x707, 0xD4},
4339 {0x15, 0x707, 0xEF},
4340 {0x15, 0x707, 0x75},
4341 {0x15, 0x707, 0xD3},
4342 {0x15, 0x707, 0x09},
4343 {0x15, 0x707, 0x02},
4344 {0x15, 0x707, 0x37},
4345 {0x15, 0x707, 0x78},
4346 {0x15, 0x53C, 0xCE},
4347 {0x15, 0x575, 0xC9},
4348 {0x15, 0x53D, 0xCE},
4349 {0x15, 0x5B7, 0xC9},
4350 {0x15, 0x70D, 0xE8},
4351 {0x15, 0x70E, 0xFE},
4352 {0x15, 0x707, 0x02},
4353 {0x15, 0x707, 0x68},
4354 {0x15, 0x707, 0x62},
4355 {0x15, 0x53A, 0xCE},
4356 {0x15, 0x546, 0xC9},
4357 {0x15, 0x53B, 0xCE},
4358 {0x15, 0x5E8, 0xC9},
4359 {0x15, 0x717, 0x0D},
4360 {0x15, 0x718, 0x20},
4364 static struct hda_verb ca0132_init_verbs1[] = {
4365 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4366 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4367 /* Set the default configuration for the mic pin because the
4368 * value set in BIOS will be reset after ct extension is enabled.
4370 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_0, 0xf0},
4371 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_1, 0x10},
4372 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_2, 0xa1},
4373 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_3, 0x03},
4375 {0x0b, 0x78D, 0x00},
4376 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4377 /*{0x10, 0x78D, 0x02},*/
4378 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4382 static void ca0132_init_chip(struct hda_codec *codec)
4384 struct ca0132_spec *spec = codec->spec;
4389 mutex_init(&spec->chipio_mutex);
4391 spec->cur_out_type = SPEAKER_OUT;
4392 spec->cur_mic_type = DIGITAL_MIC;
4393 spec->cur_mic_boost = 0;
4395 for (i = 0; i < VNODES_COUNT; i++) {
4396 spec->vnode_lvol[i] = 0x5a;
4397 spec->vnode_rvol[i] = 0x5a;
4398 spec->vnode_lswitch[i] = 0;
4399 spec->vnode_rswitch[i] = 0;
4403 * Default states for effects are in ca0132_effects[].
4405 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4406 for (i = 0; i < num_fx; i++) {
4407 on = (unsigned int)ca0132_effects[i].reqs[0];
4408 spec->effects_switch[i] = on ? 1 : 0;
4411 spec->voicefx_val = 0;
4412 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4413 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4415 #ifdef ENABLE_TUNING_CONTROLS
4416 ca0132_init_tuning_defaults(codec);
4420 static void ca0132_exit_chip(struct hda_codec *codec)
4422 /* put any chip cleanup stuffs here. */
4424 if (dspload_is_loaded(codec))
4428 static int ca0132_init(struct hda_codec *codec)
4430 struct ca0132_spec *spec = codec->spec;
4433 spec->dsp_state = DSP_DOWNLOAD_INIT;
4434 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4436 snd_hda_power_up(codec);
4438 ca0132_init_params(codec);
4439 ca0132_init_flags(codec);
4440 snd_hda_sequence_write(codec, spec->base_init_verbs);
4441 ca0132_download_dsp(codec);
4442 ca0132_refresh_widget_caps(codec);
4443 ca0132_setup_defaults(codec);
4444 ca0132_init_analog_mic2(codec);
4445 ca0132_init_dmic(codec);
4447 for (i = 0; i < spec->num_outputs; i++)
4448 init_output(codec, spec->out_pins[i], spec->dacs[0]);
4450 for (i = 0; i < spec->num_inputs; i++)
4451 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4453 for (i = 0; i < spec->num_init_verbs; i++)
4454 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4456 ca0132_init_unsol(codec);
4458 ca0132_select_out(codec);
4459 ca0132_select_mic(codec);
4461 snd_hda_jack_report_sync(codec);
4463 snd_hda_power_down(codec);
4468 static void ca0132_free(struct hda_codec *codec)
4470 struct ca0132_spec *spec = codec->spec;
4472 cancel_delayed_work_sync(&spec->unsol_hp_work);
4473 snd_hda_power_up(codec);
4474 snd_hda_sequence_write(codec, spec->base_exit_verbs);
4475 ca0132_exit_chip(codec);
4476 snd_hda_power_down(codec);
4481 static struct hda_codec_ops ca0132_patch_ops = {
4482 .build_controls = ca0132_build_controls,
4483 .build_pcms = ca0132_build_pcms,
4484 .init = ca0132_init,
4485 .free = ca0132_free,
4486 .unsol_event = ca0132_unsol_event,
4489 static void ca0132_config(struct hda_codec *codec)
4491 struct ca0132_spec *spec = codec->spec;
4493 spec->dacs[0] = 0x2;
4494 spec->dacs[1] = 0x3;
4495 spec->dacs[2] = 0x4;
4497 spec->multiout.dac_nids = spec->dacs;
4498 spec->multiout.num_dacs = 3;
4499 spec->multiout.max_channels = 2;
4501 spec->num_outputs = 2;
4502 spec->out_pins[0] = 0x0b; /* speaker out */
4503 spec->out_pins[1] = 0x10; /* headphone out */
4504 spec->shared_out_nid = 0x2;
4506 spec->num_inputs = 3;
4507 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4508 spec->adcs[1] = 0x8; /* analog mic2 */
4509 spec->adcs[2] = 0xa; /* what u hear */
4510 spec->shared_mic_nid = 0x7;
4512 spec->input_pins[0] = 0x12;
4513 spec->input_pins[1] = 0x11;
4514 spec->input_pins[2] = 0x13;
4517 static int patch_ca0132(struct hda_codec *codec)
4519 struct ca0132_spec *spec;
4522 snd_printdd("patch_ca0132\n");
4524 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4528 spec->codec = codec;
4530 spec->num_mixers = 1;
4531 spec->mixers[0] = ca0132_mixer;
4533 spec->base_init_verbs = ca0132_base_init_verbs;
4534 spec->base_exit_verbs = ca0132_base_exit_verbs;
4535 spec->init_verbs[0] = ca0132_init_verbs0;
4536 spec->init_verbs[1] = ca0132_init_verbs1;
4537 spec->num_init_verbs = 2;
4539 INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
4541 ca0132_init_chip(codec);
4543 ca0132_config(codec);
4545 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4549 codec->patch_ops = ca0132_patch_ops;
4558 static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4559 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4563 MODULE_ALIAS("snd-hda-codec-id:11020011");
4565 MODULE_LICENSE("GPL");
4566 MODULE_DESCRIPTION("Creative Sound Core3D codec");
4568 static struct hda_codec_preset_list ca0132_list = {
4569 .preset = snd_hda_preset_ca0132,
4570 .owner = THIS_MODULE,
4573 static int __init patch_ca0132_init(void)
4575 return snd_hda_add_codec_preset(&ca0132_list);
4578 static void __exit patch_ca0132_exit(void)
4580 release_cached_firmware();
4581 snd_hda_delete_codec_preset(&ca0132_list);
4584 module_init(patch_ca0132_init)
4585 module_exit(patch_ca0132_exit)