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
47 #define FLOAT_60 0x42700000;
49 #define UNSOL_TAG_HP 0x10
50 #define UNSOL_TAG_AMIC1 0x12
51 #define UNSOL_TAG_DSP 0x16
53 #define SUCCEEDED(_x) (((int)(_x)) >= 0)
54 #define FAILED(_x) (((int)(_x)) < 0)
56 #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
57 #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
59 #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
60 #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
61 #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
63 #define MASTERCONTROL 0x80
64 #define MASTERCONTROL_ALLOC_DMA_CHAN 10
65 #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
67 #define WIDGET_CHIP_CTRL 0x15
68 #define WIDGET_DSP_CTRL 0x16
70 #define MEM_CONNID_MICIN1 3
71 #define MEM_CONNID_MICIN2 5
72 #define MEM_CONNID_MICOUT1 12
73 #define MEM_CONNID_MICOUT2 14
74 #define MEM_CONNID_WUH 10
75 #define MEM_CONNID_DSP 16
76 #define MEM_CONNID_DMIC 100
81 #define SPEQ_FILE "ctspeq.bin"
82 #define EFX_FILE "ctefx.bin"
84 MODULE_FIRMWARE(SPEQ_FILE);
85 MODULE_FIRMWARE(EFX_FILE);
87 /* Debug message controls */
88 #ifdef ENABLE_CA0132_DEBUG
91 { if (!(x)) snd_printdd(KERN_ERR "CTASSERT failed.\n"); }
93 #define CA0132_LOG(fmt, args...) snd_printdd(fmt, ##args)
94 #define CA0132_DSP_LOG(msg) snd_printdd(KERN_INFO "[%s]\n", msg)
95 #define FAIL_MSG(n, s) fail_debug_out(n, s)
97 static int fail_debug_out(int status, const char *s)
99 snd_printdd(KERN_ERR "[%s]\n", s);
104 #define CTASSERT(x) do { } while (0)
105 #define CA0132_LOG(fmt, args...) do { } while (0)
106 #define CA0132_DSP_LOG(msg) do { } while (0)
107 #define FAIL_MSG(n, s) fail_debug_out(n, s)
109 static int fail_debug_out(int status, const char *s)
115 static char *dirstr[2] = { "Playback", "Capture" };
128 #define VNODE_START_NID 0x80
129 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
136 #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
138 #define EFFECT_START_NID 0x90
139 #define OUT_EFFECT_START_NID EFFECT_START_NID
140 SURROUND = OUT_EFFECT_START_NID,
147 #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
149 #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
150 ECHO_CANCELLATION = IN_EFFECT_START_NID,
156 #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
158 VOICEFX = IN_EFFECT_END_NID,
162 #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
165 /* Effects values size*/
166 #define EFFECT_VALS_MAX_COUNT 12
168 #define DSP_CAPTURE_INIT_LATENCY 0
169 #define DSP_CRYSTAL_VOICE_LATENCY 124
170 #define DSP_PLAYBACK_INIT_LATENCY 13
171 #define DSP_PLAY_ENHANCEMENT_LATENCY 30
172 #define DSP_SPEAKER_OUT_LATENCY 7
177 int mid; /*effect module ID*/
178 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
179 int direct; /* 0:output; 1:input*/
180 int params; /* number of default non-on/off params */
181 /*effect default values, 1st is on/off. */
182 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
185 static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
192 {0x3F800000, 0x3F2B851F}
200 {0x3F800000, 0x3F266666}
208 {0x00000000, 0x3F000000}
216 {0x3F800000, 0x3F3D70A4, 0x00000000}
224 {0x3F800000, 0x42A00000, 0x3F000000}
229 {9, 10, 11, 12, 13, 14,
230 15, 16, 17, 18, 19, 20},
233 {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
234 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
235 0x00000000, 0x00000000}
237 { "Echo Cancellation",
243 {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
251 {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
259 {0x00000000, 0x3F3D70A4}
267 {0x3F800000, 0x3F000000}
269 { "Key-Click Reduction",
280 {10, 11, 12, 13, 14, 15, 16, 17, 18},
283 {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000, 0x3F800000,
284 0x3F800000, 0x3F800000, 0x00000000, 0x00000000}
289 /* Tuning controls */
290 #ifdef ENABLE_TUNING_CONTROLS
293 #define TUNING_CTL_START_NID 0xC0
294 WEDGE_ANGLE = TUNING_CTL_START_NID,
308 #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
311 struct ct_tuning_ctl {
313 hda_nid_t parent_nid;
315 int mid; /*effect module ID*/
316 int req; /*effect module request*/
317 int direct; /* 0:output; 1:input*/
318 unsigned int def_val;/*effect default values*/
321 static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
429 /* Voice FX Presets */
430 #define VOICEFX_MAX_PARAM_COUNT 9
436 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
439 struct ct_voicefx_preset {
440 char *name; /*preset name*/
441 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
444 struct ct_voicefx ca0132_voicefx = {
445 "VoiceFX Capture Switch",
448 {10, 11, 12, 13, 14, 15, 16, 17, 18}
451 struct ct_voicefx_preset ca0132_voicefx_presets[] = {
453 { 0x00000000, 0x43C80000, 0x44AF0000,
454 0x44FA0000, 0x3F800000, 0x3F800000,
455 0x3F800000, 0x00000000, 0x00000000 }
458 { 0x3F800000, 0x43C80000, 0x44AF0000,
459 0x44FA0000, 0x3F19999A, 0x3F866666,
460 0x3F800000, 0x00000000, 0x00000000 }
463 { 0x3F800000, 0x43C80000, 0x44AF0000,
464 0x450AC000, 0x4017AE14, 0x3F6B851F,
465 0x3F800000, 0x00000000, 0x00000000 }
468 { 0x3F800000, 0x43C80000, 0x44AF0000,
469 0x44FA0000, 0x40400000, 0x3F28F5C3,
470 0x3F800000, 0x00000000, 0x00000000 }
473 { 0x3F800000, 0x44324000, 0x44BB8000,
474 0x44E10000, 0x3FB33333, 0x3FB9999A,
475 0x3F800000, 0x3E3A2E43, 0x00000000 }
478 { 0x3F800000, 0x43EA0000, 0x44A52000,
479 0x45098000, 0x3F266666, 0x3FC00000,
480 0x3F800000, 0x00000000, 0x00000000 }
483 { 0x3F800000, 0x43C70000, 0x44AE6000,
484 0x45193000, 0x3F8E147B, 0x3F75C28F,
485 0x3F800000, 0x00000000, 0x00000000 }
488 { 0x3F800000, 0x43930000, 0x44BEE000,
489 0x45007000, 0x3F451EB8, 0x3F7851EC,
490 0x3F800000, 0x00000000, 0x00000000 }
493 { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
494 0x451F6000, 0x3F266666, 0x3FA7D945,
495 0x3F800000, 0x3CF5C28F, 0x00000000 }
498 { 0x3F800000, 0x43C80000, 0x44AF0000,
499 0x44FA0000, 0x3FB2718B, 0x3F800000,
500 0xBC07010E, 0x00000000, 0x00000000 }
503 { 0x3F800000, 0x43C20000, 0x44906000,
504 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
505 0x3F0A3D71, 0x00000000, 0x00000000 }
508 { 0x3F800000, 0x43C80000, 0x44AF0000,
509 0x44FA0000, 0x3F800000, 0x3F800000,
510 0x3E4CCCCD, 0x00000000, 0x00000000 }
513 { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
514 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
515 0x3F800000, 0x00000000, 0x00000000 }
518 { 0x3F800000, 0x43C80000, 0x44AF0000,
519 0x44FA0000, 0x3F800000, 0x3F1A043C,
520 0x3F800000, 0x00000000, 0x00000000 }
524 enum hda_cmd_vendor_io {
526 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
527 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
529 VENDOR_DSPIO_STATUS = 0xF01,
530 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
531 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
532 VENDOR_DSPIO_DSP_INIT = 0x703,
533 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
534 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
536 /* for ChipIO node */
537 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
538 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
539 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
540 VENDOR_CHIPIO_DATA_LOW = 0x300,
541 VENDOR_CHIPIO_DATA_HIGH = 0x400,
543 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
544 VENDOR_CHIPIO_STATUS = 0xF01,
545 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
546 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
548 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
549 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
551 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
552 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
554 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
555 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
556 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
557 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
558 VENDOR_CHIPIO_FLAG_SET = 0x70F,
559 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
560 VENDOR_CHIPIO_PARAM_SET = 0x710,
561 VENDOR_CHIPIO_PARAM_GET = 0xF10,
563 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
564 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
565 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
566 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
568 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
569 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
570 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
571 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
573 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
574 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
575 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
576 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
577 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
578 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
580 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
583 enum control_flag_id {
584 /* Connection manager stream setup is bypassed/enabled */
585 CONTROL_FLAG_C_MGR = 0,
586 /* DSP DMA is bypassed/enabled */
587 CONTROL_FLAG_DMA = 1,
588 /* 8051 'idle' mode is disabled/enabled */
589 CONTROL_FLAG_IDLE_ENABLE = 2,
590 /* Tracker for the SPDIF-in path is bypassed/enabled */
591 CONTROL_FLAG_TRACKER = 3,
592 /* DigitalOut to Spdif2Out connection is disabled/enabled */
593 CONTROL_FLAG_SPDIF2OUT = 4,
594 /* Digital Microphone is disabled/enabled */
595 CONTROL_FLAG_DMIC = 5,
596 /* ADC_B rate is 48 kHz/96 kHz */
597 CONTROL_FLAG_ADC_B_96KHZ = 6,
598 /* ADC_C rate is 48 kHz/96 kHz */
599 CONTROL_FLAG_ADC_C_96KHZ = 7,
600 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
601 CONTROL_FLAG_DAC_96KHZ = 8,
602 /* DSP rate is 48 kHz/96 kHz */
603 CONTROL_FLAG_DSP_96KHZ = 9,
604 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
605 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
606 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
607 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
608 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
609 CONTROL_FLAG_DECODE_LOOP = 12,
610 /* De-emphasis filter on DAC-1 disabled/enabled */
611 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
612 /* De-emphasis filter on DAC-2 disabled/enabled */
613 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
614 /* De-emphasis filter on DAC-3 disabled/enabled */
615 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
616 /* High-pass filter on ADC_B disabled/enabled */
617 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
618 /* High-pass filter on ADC_C disabled/enabled */
619 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
620 /* Common mode on Port_A disabled/enabled */
621 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
622 /* Common mode on Port_D disabled/enabled */
623 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
624 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
625 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
626 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
627 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
628 /* ASI rate is 48kHz/96kHz */
629 CONTROL_FLAG_ASI_96KHZ = 22,
630 /* DAC power settings able to control attached ports no/yes */
631 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
632 /* Clock Stop OK reporting is disabled/enabled */
633 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
634 /* Number of control flags */
635 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
638 enum control_param_id {
639 /* 0: None, 1: Mic1In*/
640 CONTROL_PARAM_VIP_SOURCE = 1,
641 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
642 CONTROL_PARAM_SPDIF1_SOURCE = 2,
643 /* Port A output stage gain setting to use when 16 Ohm output
644 * impedance is selected*/
645 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
646 /* Port D output stage gain setting to use when 16 Ohm output
647 * impedance is selected*/
648 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
652 /* Select stream with the given ID */
653 CONTROL_PARAM_STREAM_ID = 24,
654 /* Source connection point for the selected stream */
655 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
656 /* Destination connection point for the selected stream */
657 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
658 /* Number of audio channels in the selected stream */
659 CONTROL_PARAM_STREAMS_CHANNELS = 27,
660 /*Enable control for the selected stream */
661 CONTROL_PARAM_STREAM_CONTROL = 28,
663 /* Connection Point Control */
665 /* Select connection point with the given ID */
666 CONTROL_PARAM_CONN_POINT_ID = 29,
667 /* Connection point sample rate */
668 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
672 /* Select HDA node with the given ID */
673 CONTROL_PARAM_NODE_ID = 31
676 enum hda_vendor_status_dspio {
678 VENDOR_STATUS_DSPIO_OK = 0x00,
679 /* Busy, unable to accept new command, the host must retry */
680 VENDOR_STATUS_DSPIO_BUSY = 0x01,
681 /* SCP command queue is full */
682 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
683 /* SCP response queue is empty */
684 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
687 enum hda_vendor_status_chipio {
689 VENDOR_STATUS_CHIPIO_OK = 0x00,
690 /* Busy, unable to accept new command, the host must retry */
691 VENDOR_STATUS_CHIPIO_BUSY = 0x01
694 enum ca0132_sample_rate {
714 SR_RATE_UNKNOWN = 0x1F
717 enum dsp_download_state {
718 DSP_DOWNLOAD_FAILED = -1,
719 DSP_DOWNLOAD_INIT = 0,
724 struct hda_stream_format {
725 unsigned int sample_rate;
726 unsigned short valid_bits_per_sample;
727 unsigned short container_size;
728 unsigned short number_channels;
731 /* retrieve parameters from hda format */
732 #define get_hdafmt_chs(fmt) (fmt & 0xf)
733 #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
734 #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
735 #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
738 struct snd_kcontrol_new *mixers[5];
739 unsigned int num_mixers;
740 const struct hda_verb *base_init_verbs;
741 const struct hda_verb *base_exit_verbs;
742 const struct hda_verb *init_verbs[5];
743 unsigned int num_init_verbs; /* exclude base init verbs */
744 struct auto_pin_cfg autocfg;
746 /* Nodes configurations */
747 struct hda_multi_out multiout;
748 hda_nid_t out_pins[5];
750 unsigned int num_outputs;
751 hda_nid_t input_pins[5];
753 unsigned int num_inputs;
754 hda_nid_t shared_mic_nid;
755 hda_nid_t shared_out_nid;
756 struct hda_pcm pcm_rec[5]; /* PCM information */
759 struct mutex chipio_mutex; /* chip access mutex */
762 /* DSP download related */
763 enum dsp_download_state dsp_state;
764 unsigned int dsp_stream_id;
765 unsigned int wait_scp;
766 unsigned int wait_scp_header;
767 unsigned int wait_num_data;
768 unsigned int scp_resp_header;
769 unsigned int scp_resp_data[4];
770 unsigned int scp_resp_count;
772 /* mixer and effects related */
773 unsigned char dmic_ctl;
776 long vnode_lvol[VNODES_COUNT];
777 long vnode_rvol[VNODES_COUNT];
778 long vnode_lswitch[VNODES_COUNT];
779 long vnode_rswitch[VNODES_COUNT];
780 long effects_switch[EFFECTS_COUNT];
784 struct hda_codec *codec;
785 struct delayed_work unsol_hp_work;
787 #ifdef ENABLE_TUNING_CONTROLS
788 long cur_ctl_vals[TUNING_CTLS_COUNT];
793 * CA0132 codec access
795 unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
796 unsigned int verb, unsigned int parm, unsigned int *res)
798 unsigned int response;
799 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
802 return ((response == -1) ? -1 : 0);
805 static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
806 unsigned short converter_format, unsigned int *res)
808 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
809 converter_format & 0xffff, res);
812 static int codec_set_converter_stream_channel(struct hda_codec *codec,
813 hda_nid_t nid, unsigned char stream,
814 unsigned char channel, unsigned int *res)
816 unsigned char converter_stream_channel = 0;
818 converter_stream_channel = (stream << 4) | (channel & 0x0f);
819 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
820 converter_stream_channel, res);
824 * CA0132 chip access stuffs
826 static int chipio_send(struct hda_codec *codec,
831 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
833 /* send bits of data specified by reg */
835 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
837 if (res == VENDOR_STATUS_CHIPIO_OK)
840 } while (time_before(jiffies, timeout));
843 printk(KERN_ERR "CA0132 chipio timed out %x %x\n", reg, data);
847 static int chipio_write_addx(struct hda_codec *codec, u32 chip_addx)
849 struct ca0132_spec *spec = codec->spec;
852 if (spec->curr_chip_addx == chip_addx)
855 /* send low 16 bits of the address */
856 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
859 if (SUCCEEDED(status)) {
860 /* send high 16 bits of the address */
861 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
865 CTASSERT(SUCCEEDED(status));
866 spec->curr_chip_addx = SUCCEEDED(status) ? chip_addx : ~0UL;
871 static int chipio_write_data(struct hda_codec *codec, u32 data)
873 struct ca0132_spec *spec = codec->spec;
876 /* send low 16 bits of the data */
877 status = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
879 if (SUCCEEDED(status)) {
880 /* send high 16 bits of the data */
881 status = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
885 CTASSERT(SUCCEEDED(status));
886 spec->curr_chip_addx = SUCCEEDED(status) ?
887 (spec->curr_chip_addx + 4) : ~0UL;
892 static int chipio_write_data_multiple(struct hda_codec *codec,
899 return FAIL_MSG(-1, "chipio_write_data null ptr");
901 while ((count-- != 0) && (status == 0))
902 status = chipio_write_data(codec, *data++);
907 static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
909 struct ca0132_spec *spec = codec->spec;
913 status = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
915 if (SUCCEEDED(status)) {
917 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
920 if (SUCCEEDED(status)) {
922 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
923 VENDOR_CHIPIO_HIC_READ_DATA, 0);
926 CTASSERT(SUCCEEDED(status));
927 spec->curr_chip_addx = SUCCEEDED(status) ?
928 (spec->curr_chip_addx + 4) : ~0UL;
933 static int chipio_write(struct hda_codec *codec, u32 chip_addx,
936 struct ca0132_spec *spec = codec->spec;
939 mutex_lock(&spec->chipio_mutex);
940 status = chipio_write_addx(codec, chip_addx);
941 if (SUCCEEDED(status))
942 status = chipio_write_data(codec, data);
943 mutex_unlock(&spec->chipio_mutex);
948 static int chipio_write_multiple(struct hda_codec *codec,
953 struct ca0132_spec *spec = codec->spec;
956 mutex_lock(&spec->chipio_mutex);
957 status = chipio_write_addx(codec, chip_addx);
958 if (SUCCEEDED(status))
959 status = chipio_write_data_multiple(codec, data, count);
960 mutex_unlock(&spec->chipio_mutex);
965 static int chipio_read(struct hda_codec *codec, u32 chip_addx,
968 struct ca0132_spec *spec = codec->spec;
971 mutex_lock(&spec->chipio_mutex);
972 status = chipio_write_addx(codec, chip_addx);
973 if (SUCCEEDED(status))
974 status = chipio_read_data(codec, data);
975 mutex_unlock(&spec->chipio_mutex);
980 static void chipio_set_control_flag(struct hda_codec *codec,
981 enum control_flag_id flag_id,
985 unsigned int flag_bit;
987 flag_bit = (flag_state ? 1 : 0);
988 val = (flag_bit << 7) | (flag_id);
989 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
990 VENDOR_CHIPIO_FLAG_SET, val);
993 static void chipio_set_control_param(struct hda_codec *codec,
994 enum control_param_id param_id, int param_val)
996 struct ca0132_spec *spec = codec->spec;
999 if ((param_id < 32) && (param_val < 8)) {
1000 val = (param_val << 5) | (param_id);
1001 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1002 VENDOR_CHIPIO_PARAM_SET, val);
1004 mutex_lock(&spec->chipio_mutex);
1005 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1006 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1007 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1009 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1010 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1013 CA0132_LOG("set_control_param:FAIL! id=0x%x, val=%d\n",
1014 param_id, param_val);
1015 mutex_unlock(&spec->chipio_mutex);
1019 static void chipio_set_conn_rate(struct hda_codec *codec,
1020 int connid, enum ca0132_sample_rate rate)
1022 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1023 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1027 static void chipio_enable_clocks(struct hda_codec *codec)
1029 struct ca0132_spec *spec = codec->spec;
1031 mutex_lock(&spec->chipio_mutex);
1032 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1033 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1034 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1035 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1036 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1037 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1038 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1039 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1040 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1041 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1042 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1043 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1044 mutex_unlock(&spec->chipio_mutex);
1049 * CA0132 DSP IO stuffs
1051 static int dspio_send(struct hda_codec *codec, unsigned int reg,
1055 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1057 /* send bits of data specified by reg to dsp */
1059 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1060 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1063 } while (time_before(jiffies, timeout));
1069 static void dspio_write_wait(struct hda_codec *codec)
1072 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1075 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1076 VENDOR_DSPIO_STATUS, 0);
1077 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1078 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1081 } while (time_before(jiffies, timeout));
1084 static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1086 struct ca0132_spec *spec = codec->spec;
1089 dspio_write_wait(codec);
1091 mutex_lock(&spec->chipio_mutex);
1092 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1094 if (SUCCEEDED(status))
1095 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1097 mutex_unlock(&spec->chipio_mutex);
1099 /* OK, now check if the write itself has executed*/
1100 if (SUCCEEDED(status))
1101 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1102 VENDOR_DSPIO_STATUS, 0);
1104 if (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL)
1105 CA0132_LOG("dspio_write: SCP_COMMAND_QUEUE_FULL!!!\n\n");
1107 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1111 static int dspio_write_multiple(struct hda_codec *codec,
1112 unsigned int *buffer, unsigned int size)
1117 if ((buffer == NULL))
1121 while (count < size) {
1122 status = dspio_write(codec, *buffer++);
1131 static int dspio_read(struct hda_codec *codec, unsigned int *data)
1135 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1136 if (SUCCEEDED(status))
1137 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1139 if (FAILED(status) ||
1140 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1143 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1144 VENDOR_DSPIO_SCP_READ_DATA, 0);
1149 static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1150 unsigned int *buf_size, unsigned int size_count)
1153 unsigned int size = *buf_size;
1155 unsigned int skip_count;
1158 if ((buffer == NULL))
1162 while (count < size && count < size_count) {
1163 status = dspio_read(codec, buffer++);
1170 if (SUCCEEDED(status)) {
1171 while (skip_count < size) {
1172 status = dspio_read(codec, &dummy);
1183 static inline unsigned int
1184 make_scp_header(unsigned int target_id, unsigned int source_id,
1185 unsigned int get_flag, unsigned int req,
1186 unsigned int device_flag, unsigned int resp_flag,
1187 unsigned int error_flag, unsigned int data_size)
1189 unsigned int header = 0;
1191 header = (data_size & 0x1f) << 27;
1192 header |= (error_flag & 0x01) << 26;
1193 header |= (resp_flag & 0x01) << 25;
1194 header |= (device_flag & 0x01) << 24;
1195 header |= (req & 0x7f) << 17;
1196 header |= (get_flag & 0x01) << 16;
1197 header |= (source_id & 0xff) << 8;
1198 header |= target_id & 0xff;
1204 extract_scp_header(unsigned int header,
1205 unsigned int *target_id, unsigned int *source_id,
1206 unsigned int *get_flag, unsigned int *req,
1207 unsigned int *device_flag, unsigned int *resp_flag,
1208 unsigned int *error_flag, unsigned int *data_size)
1211 *data_size = (header >> 27) & 0x1f;
1213 *error_flag = (header >> 26) & 0x01;
1215 *resp_flag = (header >> 25) & 0x01;
1217 *device_flag = (header >> 24) & 0x01;
1219 *req = (header >> 17) & 0x7f;
1221 *get_flag = (header >> 16) & 0x01;
1223 *source_id = (header >> 8) & 0xff;
1225 *target_id = header & 0xff;
1228 #define SCP_MAX_DATA_WORDS (16)
1230 /* Structure to contain any SCP message */
1233 unsigned int data[SCP_MAX_DATA_WORDS];
1236 static void dspio_clear_response_queue(struct hda_codec *codec)
1238 unsigned int dummy = 0;
1241 /* clear all from the response queue */
1243 status = dspio_read(codec, &dummy);
1244 } while (status == 0);
1247 static int dspio_get_response_data(struct hda_codec *codec)
1249 struct ca0132_spec *spec = codec->spec;
1250 unsigned int data = 0;
1253 if (FAILED(dspio_read(codec, &data)))
1256 CA0132_LOG("dspio_get_response_data: 0x%08x\n", data);
1257 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1258 spec->scp_resp_header = data;
1259 spec->scp_resp_count = data >> 27;
1260 count = spec->wait_num_data;
1261 dspio_read_multiple(codec, spec->scp_resp_data,
1262 &spec->scp_resp_count, count);
1269 static int dspio_send_scp_message(struct hda_codec *codec,
1270 unsigned char *send_buf,
1271 unsigned int send_buf_size,
1272 unsigned char *return_buf,
1273 unsigned int return_buf_size,
1274 unsigned int *bytes_returned)
1276 struct ca0132_spec *spec = codec->spec;
1278 unsigned int scp_send_size = 0;
1279 unsigned int total_size;
1280 bool waiting_for_resp = false;
1281 unsigned int header;
1282 struct scp_msg *ret_msg;
1283 unsigned int resp_src_id, resp_target_id;
1284 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1287 *bytes_returned = 0;
1289 /* get scp header from buffer */
1290 header = *((unsigned int *)send_buf);
1291 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1292 &device_flag, NULL, NULL, &data_size);
1293 scp_send_size = data_size + 1;
1294 total_size = (scp_send_size * 4);
1296 CTASSERT(send_buf_size >= total_size);
1297 if (send_buf_size < total_size)
1300 if (get_flag || device_flag) {
1301 if (!return_buf || return_buf_size < 4 || !bytes_returned) {
1305 spec->wait_scp_header = *((unsigned int *)send_buf);
1307 /* swap source id with target id */
1308 resp_target_id = src_id;
1309 resp_src_id = target_id;
1310 spec->wait_scp_header &= 0xffff0000;
1311 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1312 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1314 waiting_for_resp = true;
1317 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1319 if (FAILED(status)) {
1325 if (waiting_for_resp) {
1326 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1327 memset(return_buf, 0, return_buf_size);
1330 } while (spec->wait_scp && time_before(jiffies, timeout));
1331 waiting_for_resp = false;
1332 if (!spec->wait_scp) {
1333 ret_msg = (struct scp_msg *)return_buf;
1334 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1335 memcpy(&ret_msg->data, spec->scp_resp_data,
1336 spec->wait_num_data);
1337 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1340 CTASSERT(*bytes_returned == 0);
1349 static int dspio_scp(struct hda_codec *codec,
1350 int mod_id, int req, int dir, void *data, unsigned int len,
1351 void *reply, unsigned int *reply_len)
1354 struct scp_msg scp_send, scp_reply;
1355 unsigned int ret_bytes, send_size, ret_size;
1356 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1357 unsigned int reply_data_size;
1359 memset(&scp_send, 0, sizeof(scp_send));
1360 memset(&scp_reply, 0, sizeof(scp_reply));
1362 CTASSERT((len == 0 || data != NULL) && len <= SCP_MAX_DATA_WORDS);
1363 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1366 CTASSERT(dir != SCP_GET || reply != NULL);
1367 if (dir == SCP_GET && reply == NULL)
1368 return FAIL_MSG(-1, "dspio_scp get but has no buffer");
1370 CTASSERT((reply == NULL) || (reply_len != NULL && (*reply_len > 0)));
1371 if (reply != NULL && (reply_len == NULL || (*reply_len == 0)))
1372 return FAIL_MSG(-1, "dspio_scp bad resp buf len parms");
1374 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1375 0, 0, 0, len/sizeof(unsigned int));
1376 if (data != NULL && len > 0) {
1377 len = min((unsigned int)(sizeof(scp_send.data)), len);
1378 memcpy(scp_send.data, data, len);
1382 send_size = sizeof(unsigned int) + len;
1383 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1384 send_size, (unsigned char *)&scp_reply,
1385 sizeof(scp_reply), &ret_bytes);
1388 #ifdef ENABLE_CA0132_DEBUG
1390 unsigned int x, *p, i;
1391 unsigned int source_id;
1392 unsigned int target_id;
1394 unsigned int get_flag;
1395 unsigned int resp_flag;
1396 unsigned int device_flag;
1397 unsigned int error_flag;
1398 unsigned int data_size;
1400 memcpy(&x, &scp_send.hdr, sizeof(unsigned int));
1401 extract_scp_header(x, &target_id, &source_id,
1402 &get_flag, &req, &device_flag, &resp_flag,
1403 &error_flag, &data_size);
1406 "ScpDispatch ----- REQ: "
1423 for (x = 0; x < data_size; x++)
1424 snd_printdd("0x%08x ", scp_send.data[x].ui);
1429 if (ret_bytes >= sizeof(scp_reply.hdr)) {
1430 memcpy(&x, &scp_reply.hdr, sizeof(unsigned int));
1431 extract_scp_header(x, &target_id, &source_id,
1432 &get_flag, &req, &device_flag,
1433 &resp_flag, &error_flag, &data_size);
1453 for (x = 0; x < data_size; x++)
1454 snd_printdd("0x%08x ", scp_reply.data[x].ui);
1456 snd_printdd("REP: (too short,nbytes=%d) [", ret_bytes);
1458 for (p = (unsigned int *)&scp_reply, i = 0;
1459 i < ret_bytes; p++, i++) {
1460 snd_printdd("0x%04x ", *p);
1469 return FAIL_MSG(status, "dspio_scp: send scp msg failed");
1471 /* extract send and reply headers members */
1472 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1473 NULL, NULL, NULL, NULL, NULL);
1474 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1475 &reply_resp_flag, &reply_error_flag,
1481 if (reply_resp_flag && !reply_error_flag) {
1482 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1483 / sizeof(unsigned int);
1485 CTASSERT(dir == SCP_GET && reply != NULL && reply_len != NULL);
1487 if (*reply_len < ret_size*sizeof(unsigned int)) {
1488 status = FAIL_MSG(-1, "reply too long for buf");
1489 } else if (ret_size != reply_data_size) {
1490 status = FAIL_MSG(-1, "RetLen and HdrLen .NE.");
1492 *reply_len = ret_size*sizeof(unsigned int);
1493 memcpy(reply, scp_reply.data, *reply_len);
1496 status = FAIL_MSG(-1, "reply ill-formed or errflag set");
1502 static int dspio_set_param(struct hda_codec *codec, int mod_id,
1503 int req, void *data, unsigned int len)
1505 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1508 static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1511 unsigned int size = sizeof(dma_chan);
1513 CA0132_DSP_LOG(" dspio_alloc_dma_chan() -- begin");
1514 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1515 SCP_GET, NULL, 0, dma_chan, &size);
1517 if (FAILED(status)) {
1518 CA0132_DSP_LOG("dspio_alloc_dma_chan: SCP Failed");
1522 if ((*dma_chan + 1) == 0) {
1523 CA0132_DSP_LOG("no free dma channels to allocate");
1527 CA0132_LOG("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1528 CA0132_DSP_LOG(" dspio_alloc_dma_chan() -- complete");
1533 static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1536 unsigned int dummy = 0;
1538 CA0132_DSP_LOG(" dspio_free_dma_chan() -- begin");
1539 CA0132_LOG("dspio_free_dma_chan: chan=%d\n", dma_chan);
1541 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1542 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1544 if (FAILED(status)) {
1545 CA0132_DSP_LOG("dspio_free_dma_chan: SCP Failed");
1549 CA0132_DSP_LOG(" dspio_free_dma_chan() -- complete");
1555 * CA0132 DSP access stuffs
1557 static int dsp_set_run_state(struct hda_codec *codec)
1559 unsigned int dbg_ctrl_reg;
1560 unsigned int halt_state;
1563 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1567 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1568 DSP_DBGCNTL_STATE_LOBIT;
1570 if (halt_state != 0) {
1571 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1572 DSP_DBGCNTL_SS_MASK);
1573 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1578 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1579 DSP_DBGCNTL_EXEC_MASK;
1580 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1589 static int dsp_reset(struct hda_codec *codec)
1594 CA0132_LOG("dsp_reset\n");
1596 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1598 } while (res == -1 && retry);
1601 CA0132_LOG("dsp_reset timeout\n");
1608 static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1609 bool *code, bool *yram)
1611 *code = *yram = false;
1613 if (UC_RANGE(chip_addx, 1)) {
1615 return UC_OFF(chip_addx);
1616 } else if (X_RANGE_ALL(chip_addx, 1)) {
1617 return X_OFF(chip_addx);
1618 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1620 return Y_OFF(chip_addx);
1623 return (unsigned int)INVALID_CHIP_ADDRESS;
1626 static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1628 unsigned int dma_chnlstart_reg;
1630 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1632 return ((dma_chnlstart_reg & (1 <<
1633 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1636 static int dsp_dma_setup_common(struct hda_codec *codec,
1637 unsigned int chip_addx,
1638 unsigned int dma_chan,
1639 unsigned int port_map_mask,
1643 unsigned int chnl_prop;
1644 unsigned int dsp_addx;
1645 unsigned int active;
1648 CA0132_DSP_LOG("-- dsp_dma_setup_common() -- Begin ---------");
1650 CTASSERT(dma_chan < DSPDMAC_DMA_CFG_CHANNEL_COUNT);
1651 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT)
1652 return FAIL_MSG(-1, "dma chan num invalid");
1654 if (dsp_is_dma_active(codec, dma_chan))
1655 return FAIL_MSG(-1, "dma already active");
1657 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1659 if (dsp_addx == INVALID_CHIP_ADDRESS)
1660 return FAIL_MSG(-1, "invalid chip addr");
1662 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1665 CA0132_DSP_LOG(" dsp_dma_setup_common() start reg pgm");
1668 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1672 return FAIL_MSG(-1, "read CHNLPROP Reg fail");
1674 CA0132_DSP_LOG(" dsp_dma_setup_common() Read CHNLPROP");
1678 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1680 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1682 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1684 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1686 return FAIL_MSG(-1, "write CHNLPROP Reg fail");
1688 CA0132_DSP_LOG(" dsp_dma_setup_common() Write CHNLPROP");
1691 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1695 return FAIL_MSG(-1, "read ACTIVE Reg fail");
1697 CA0132_DSP_LOG(" dsp_dma_setup_common() Read ACTIVE");
1700 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1701 DSPDMAC_ACTIVE_AAR_MASK;
1703 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1705 return FAIL_MSG(-1, "write ACTIVE Reg fail");
1707 CA0132_DSP_LOG(" dsp_dma_setup_common() Write ACTIVE");
1709 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1712 return FAIL_MSG(-1, "write AUDCHSEL Reg fail");
1714 CA0132_DSP_LOG(" dsp_dma_setup_common() Write AUDCHSEL");
1716 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1717 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1719 return FAIL_MSG(-1, "write IRQCNT Reg fail");
1721 CA0132_DSP_LOG(" dsp_dma_setup_common() Write IRQCNT");
1724 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1725 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1726 chip_addx, dsp_addx, dma_chan,
1727 port_map_mask, chnl_prop, active);
1729 CA0132_DSP_LOG("-- dsp_dma_setup_common() -- Complete ---------");
1734 static int dsp_dma_setup(struct hda_codec *codec,
1735 unsigned int chip_addx,
1737 unsigned int dma_chan)
1741 unsigned int dsp_addx;
1742 unsigned int addr_field;
1743 unsigned int incr_field;
1744 unsigned int base_cnt;
1745 unsigned int cur_cnt;
1746 unsigned int dma_cfg = 0;
1747 unsigned int adr_ofs = 0;
1748 unsigned int xfr_cnt = 0;
1749 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1750 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1752 CA0132_DSP_LOG("-- dsp_dma_setup() -- Begin ---------");
1754 CTASSERT(count <= max_dma_count);
1755 if (count > max_dma_count)
1756 return FAIL_MSG(-1, "count too big");
1758 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1759 if (dsp_addx == INVALID_CHIP_ADDRESS)
1760 return FAIL_MSG(-1, "invalid chip addr");
1762 CA0132_DSP_LOG(" dsp_dma_setup() start reg pgm");
1764 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1770 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1772 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1775 CTASSERT((addr_field & DSPDMAC_DMACFG_DBADR_MASK) == addr_field);
1776 dma_cfg = addr_field + incr_field;
1777 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1780 return FAIL_MSG(-1, "write DMACFG Reg fail");
1782 CA0132_DSP_LOG(" dsp_dma_setup() Write DMACFG");
1784 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1786 CTASSERT((adr_ofs & DSPDMAC_DSPADROFS_BOFS_MASK) == adr_ofs);
1788 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1791 return FAIL_MSG(-1, "write DSPADROFS Reg fail");
1793 CA0132_DSP_LOG(" dsp_dma_setup() Write DSPADROFS");
1795 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1796 CTASSERT((base_cnt & DSPDMAC_XFRCNT_BCNT_MASK) == base_cnt);
1798 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1799 CTASSERT((cur_cnt & DSPDMAC_XFRCNT_CCNT_MASK) == cur_cnt);
1801 xfr_cnt = base_cnt | cur_cnt;
1803 status = chipio_write(codec,
1804 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1806 return FAIL_MSG(-1, "write XFRCNT Reg fail");
1808 CA0132_DSP_LOG(" dsp_dma_setup() Write XFRCNT");
1811 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1812 "ADROFS=0x%x, XFRCNT=0x%x\n",
1813 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1815 CA0132_DSP_LOG("-- dsp_dma_setup() -- Complete ---------");
1820 static int dsp_dma_start(struct hda_codec *codec,
1821 unsigned int dma_chan, bool ovly)
1823 unsigned int reg = 0;
1826 CA0132_DSP_LOG("-- dsp_dma_start() -- Begin ---------");
1829 status = chipio_read(codec,
1830 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
1833 return FAIL_MSG(-1, "read CHNLSTART reg fail");
1835 CA0132_DSP_LOG("-- dsp_dma_start() Read CHNLSTART");
1837 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1838 DSPDMAC_CHNLSTART_DIS_MASK);
1841 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1842 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1844 return FAIL_MSG(-1, "write CHNLSTART reg fail");
1846 CA0132_DSP_LOG("-- dsp_dma_start() -- Complete ---------");
1851 static int dsp_dma_stop(struct hda_codec *codec,
1852 unsigned int dma_chan, bool ovly)
1854 unsigned int reg = 0;
1857 CA0132_DSP_LOG("-- dsp_dma_stop() -- Begin ---------");
1860 status = chipio_read(codec,
1861 DSPDMAC_CHNLSTART_INST_OFFSET, ®);
1864 return FAIL_MSG(-1, "read CHNLSTART reg fail");
1866 CA0132_DSP_LOG("-- dsp_dma_stop() Read CHNLSTART");
1867 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1868 DSPDMAC_CHNLSTART_DIS_MASK);
1871 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1872 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1874 return FAIL_MSG(-1, "write CHNLSTART reg fail");
1876 CA0132_DSP_LOG("-- dsp_dma_stop() -- Complete ---------");
1881 static int dsp_allocate_router_ports(struct hda_codec *codec,
1882 unsigned int num_chans,
1883 unsigned int ports_per_channel,
1884 unsigned int start_device,
1885 unsigned int *port_map)
1891 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1895 val = start_device << 6;
1896 val |= (ports_per_channel - 1) << 4;
1897 val |= num_chans - 1;
1899 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1900 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1903 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1904 VENDOR_CHIPIO_PORT_ALLOC_SET,
1907 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1911 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1912 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1916 return (res < 0) ? -1 : 0;
1919 static int dsp_free_router_ports(struct hda_codec *codec)
1923 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1927 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1928 VENDOR_CHIPIO_PORT_FREE_SET,
1931 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1936 static int dsp_allocate_ports(struct hda_codec *codec,
1937 unsigned int num_chans,
1938 unsigned int rate_multi, unsigned int *port_map)
1942 CA0132_DSP_LOG(" dsp_allocate_ports() -- begin");
1944 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4))
1945 return FAIL_MSG(status, "bad rate multiple");
1947 status = dsp_allocate_router_ports(codec, num_chans,
1948 rate_multi, 0, port_map);
1950 CA0132_DSP_LOG(" dsp_allocate_ports() -- complete");
1955 static int dsp_free_ports(struct hda_codec *codec)
1959 CA0132_DSP_LOG(" dsp_free_ports() -- begin");
1961 status = dsp_free_router_ports(codec);
1963 return FAIL_MSG(-1, "free router ports fail");
1965 CA0132_DSP_LOG(" dsp_free_ports() -- complete");
1970 static int dsp_allocate_ports_format(struct hda_codec *codec,
1971 const unsigned short fmt,
1972 unsigned int *port_map)
1975 unsigned int num_chans;
1977 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1978 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1979 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1981 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4))
1982 return FAIL_MSG(-1, "bad rate multiple");
1984 num_chans = get_hdafmt_chs(fmt) + 1;
1986 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1992 * HDA DMA engine stuffs for DSP code download
1995 struct hda_codec *codec;
1996 unsigned short m_converter_format;
1997 struct snd_dma_buffer *dmab;
1998 unsigned int buf_size;
2006 static int dma_convert_to_hda_format(
2007 struct hda_stream_format *stream_format,
2008 unsigned short *hda_format)
2010 unsigned int format_val;
2012 format_val = snd_hda_calc_stream_format(
2013 stream_format->sample_rate,
2014 stream_format->number_channels,
2015 SNDRV_PCM_FORMAT_S32_LE,
2016 stream_format->container_size, 0);
2019 *hda_format = (unsigned short)format_val;
2024 static int dma_reset(struct dma_engine *dma)
2026 struct hda_codec *codec = dma->codec;
2027 struct ca0132_spec *spec = codec->spec;
2030 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2032 status = snd_hda_codec_load_dsp_prepare(codec,
2033 dma->m_converter_format,
2038 spec->dsp_stream_id = status;
2043 static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2047 CA0132_LOG("dma_set_state state=%d\n", state);
2050 case DMA_STATE_STOP:
2060 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2064 static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2066 return dma->dmab->bytes;
2069 static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2071 return dma->dmab->area;
2074 static int dma_xfer(struct dma_engine *dma,
2075 const unsigned int *data,
2078 memcpy(dma->dmab->area, data, count);
2082 static void dma_get_converter_format(
2083 struct dma_engine *dma,
2084 unsigned short *format)
2087 *format = dma->m_converter_format;
2090 static unsigned int dma_get_stream_id(struct dma_engine *dma)
2092 struct ca0132_spec *spec = dma->codec->spec;
2094 return spec->dsp_stream_id;
2099 * CA0132 chip DSP image segment stuffs
2101 struct dsp_image_seg {
2108 static const u32 g_magic_value = 0x4c46584d;
2109 static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2111 static bool is_valid(const struct dsp_image_seg *p)
2113 return p->magic == g_magic_value;
2116 static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2118 return g_chip_addr_magic_value == p->chip_addr;
2121 static bool is_last(const struct dsp_image_seg *p)
2123 return p->count == 0;
2126 static size_t dsp_sizeof(const struct dsp_image_seg *p)
2128 return sizeof(*p) + p->count*sizeof(u32);
2131 static const struct dsp_image_seg *get_next_seg_ptr(
2132 const struct dsp_image_seg *p)
2134 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2138 * CA0132 chip DSP trannsfer stuffs. For DSP download.
2140 #define INVALID_DMA_CHANNEL (~0UL)
2142 static int dspxfr_hci_write(struct hda_codec *codec,
2143 const struct dsp_image_seg *fls)
2149 CTASSERT(fls != NULL && fls->chip_addr == g_chip_addr_magic_value);
2150 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value)
2151 return FAIL_MSG(-1, "hci_write invalid params");
2154 data = (u32 *)(fls->data);
2155 while (SUCCEEDED(status) && count >= 2) {
2156 status = chipio_write(codec, data[0], data[1]);
2158 status = FAIL_MSG(status, "hci_write chipio failed");
2165 static int dspxfr_one_seg(struct hda_codec *codec,
2166 const struct dsp_image_seg *fls,
2168 struct dma_engine *dma_engine,
2169 unsigned int dma_chan,
2170 unsigned int port_map_mask,
2174 bool comm_dma_setup_done = false;
2175 const unsigned int *data;
2176 unsigned int chip_addx;
2177 unsigned int words_to_write;
2178 unsigned int buffer_size_words;
2179 unsigned char *buffer_addx;
2180 unsigned short hda_format;
2181 unsigned int sample_rate_div;
2182 unsigned int sample_rate_mul;
2183 unsigned int num_chans;
2184 unsigned int hda_frame_size_words;
2185 unsigned int remainder_words;
2186 const u32 *data_remainder;
2187 u32 chip_addx_remainder;
2188 unsigned int run_size_words;
2189 const struct dsp_image_seg *hci_write = NULL;
2190 unsigned long timeout;
2193 CTASSERT(fls != NULL);
2196 if (is_hci_prog_list_seg(fls)) {
2198 fls = get_next_seg_ptr(fls);
2201 if (hci_write && (!fls || is_last(fls))) {
2202 CA0132_LOG("hci_write\n");
2203 return dspxfr_hci_write(codec, hci_write);
2206 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2207 CA0132_LOG("Invalid Params\n");
2212 chip_addx = fls->chip_addr,
2213 words_to_write = fls->count;
2215 if (!words_to_write)
2216 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2218 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2220 if (!UC_RANGE(chip_addx, words_to_write) &&
2221 !X_RANGE_ALL(chip_addx, words_to_write) &&
2222 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2223 CA0132_LOG("Invalid chip_addx Params\n");
2227 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2230 buffer_addx = dma_get_buffer_addr(dma_engine);
2232 if (buffer_addx == NULL)
2233 status = FAIL_MSG(-1, "dma_engine buffer NULL");
2235 dma_get_converter_format(dma_engine, &hda_format);
2236 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2237 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2238 num_chans = get_hdafmt_chs(hda_format) + 1;
2240 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2241 (num_chans * sample_rate_mul / sample_rate_div));
2243 if (hda_frame_size_words == 0) {
2244 snd_printdd(KERN_ERR "frmsz zero\n");
2248 buffer_size_words = min(buffer_size_words,
2249 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2251 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2253 "chpadr=0x%08x frmsz=%u nchan=%u "
2254 "rate_mul=%u div=%u bufsz=%u\n",
2255 chip_addx, hda_frame_size_words, num_chans,
2256 sample_rate_mul, sample_rate_div, buffer_size_words);
2258 CTASSERT(SUCCEEDED(status));
2259 CTASSERT(buffer_addx != NULL);
2260 CTASSERT(buffer_size_words >= hda_frame_size_words);
2262 if (buffer_addx == NULL || buffer_size_words < hda_frame_size_words) {
2263 status = FAIL_MSG(-1, "dspxfr_one_seg:failed");
2269 remainder_words = words_to_write % hda_frame_size_words;
2270 data_remainder = data;
2271 chip_addx_remainder = chip_addx;
2273 data += remainder_words;
2274 chip_addx += remainder_words*sizeof(u32);
2275 words_to_write -= remainder_words;
2277 while ((words_to_write != 0) && SUCCEEDED(status)) {
2278 run_size_words = min(buffer_size_words, words_to_write);
2279 CA0132_LOG("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2280 words_to_write, run_size_words, remainder_words);
2281 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2282 if (!comm_dma_setup_done && SUCCEEDED(status)) {
2283 status = dsp_dma_stop(codec, dma_chan, ovly);
2284 if (SUCCEEDED(status))
2285 status = dsp_dma_setup_common(codec, chip_addx,
2286 dma_chan, port_map_mask, ovly);
2287 comm_dma_setup_done = true;
2290 if (SUCCEEDED(status))
2291 status = dsp_dma_setup(codec, chip_addx,
2292 run_size_words, dma_chan);
2293 if (SUCCEEDED(status))
2294 status = dsp_dma_start(codec, dma_chan, ovly);
2295 if (SUCCEEDED(status) && !dsp_is_dma_active(codec, dma_chan))
2296 status = FAIL_MSG(-1, "dspxfr:DMA did not start");
2297 if (SUCCEEDED(status))
2298 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2299 if (SUCCEEDED(status)) {
2300 if (remainder_words != 0) {
2301 status = chipio_write_multiple(codec,
2302 chip_addx_remainder,
2305 remainder_words = 0;
2308 status = dspxfr_hci_write(codec, hci_write);
2312 timeout = jiffies + msecs_to_jiffies(2000);
2314 dma_active = dsp_is_dma_active(codec, dma_chan);
2318 } while (time_before(jiffies, timeout));
2321 CA0132_DSP_LOG("+++++ DMA complete");
2322 dma_set_state(dma_engine, DMA_STATE_STOP);
2323 status = dma_reset(dma_engine);
2328 CTASSERT(run_size_words <= words_to_write);
2329 data += run_size_words;
2330 chip_addx += run_size_words*sizeof(u32);
2331 words_to_write -= run_size_words;
2334 if (SUCCEEDED(status) && (remainder_words != 0)) {
2335 status = chipio_write_multiple(codec, chip_addx_remainder,
2336 data_remainder, remainder_words);
2342 static int dspxfr_image(struct hda_codec *codec,
2343 const struct dsp_image_seg *fls_data,
2344 unsigned int reloc, struct hda_stream_format *format,
2347 struct ca0132_spec *spec = codec->spec;
2349 unsigned short hda_format = 0;
2350 unsigned int response;
2351 unsigned char stream_id = 0;
2352 struct dma_engine *dma_engine;
2353 unsigned int dma_chan;
2354 unsigned int port_map_mask;
2356 CTASSERT(fls_data != NULL);
2357 if (fls_data == NULL)
2360 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
2366 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2367 if (!dma_engine->dmab) {
2369 goto free_dma_engine;
2372 dma_engine->codec = codec;
2373 dma_convert_to_hda_format(format, &hda_format);
2374 dma_engine->m_converter_format = hda_format;
2375 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2376 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2380 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2381 hda_format, &response);
2383 if (FAILED(status)) {
2384 status = FAIL_MSG(status, "set converter format fail");
2385 goto free_dma_engine_and_dmab;
2388 status = snd_hda_codec_load_dsp_prepare(codec,
2389 dma_engine->m_converter_format,
2390 dma_engine->buf_size,
2394 spec->dsp_stream_id = status;
2397 status = dspio_alloc_dma_chan(codec, &dma_chan);
2398 if (FAILED(status)) {
2399 status = FAIL_MSG(status, "alloc dmachan fail");
2400 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2406 status = dsp_allocate_ports_format(codec, hda_format,
2408 if (FAILED(status)) {
2409 status = FAIL_MSG(status, "alloc parts fail");
2413 stream_id = dma_get_stream_id(dma_engine);
2414 status = codec_set_converter_stream_channel(codec,
2415 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2416 if (FAILED(status)) {
2417 status = FAIL_MSG(status, "set stream chan fail");
2421 while (SUCCEEDED(status) && (fls_data != NULL) &&
2422 !is_last(fls_data)) {
2423 if (!is_valid(fls_data)) {
2424 status = FAIL_MSG(-1, "FLS check fail");
2427 status = dspxfr_one_seg(codec, fls_data, reloc,
2428 dma_engine, dma_chan,
2429 port_map_mask, ovly);
2430 if (is_hci_prog_list_seg(fls_data))
2431 fls_data = get_next_seg_ptr(fls_data);
2433 CTASSERT(fls_data != NULL);
2434 if ((fls_data != NULL) && !is_last(fls_data))
2435 fls_data = get_next_seg_ptr(fls_data);
2437 CTASSERT(fls_data != NULL);
2440 if (port_map_mask != 0)
2441 status = dsp_free_ports(codec);
2446 status = codec_set_converter_stream_channel(codec,
2447 WIDGET_CHIP_CTRL, 0, 0, &response);
2450 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2451 status = dspio_free_dma_chan(codec, dma_chan);
2453 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2454 free_dma_engine_and_dmab:
2455 kfree(dma_engine->dmab);
2463 * CA0132 DSP download stuffs.
2465 static void dspload_post_setup(struct hda_codec *codec)
2467 CA0132_DSP_LOG("---- dspload_post_setup ------");
2469 /*set DSP speaker to 2.0 configuration*/
2470 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2471 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2473 /*update write pointer*/
2474 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2477 static int dspload_image(struct hda_codec *codec,
2478 const struct dsp_image_seg *fls,
2485 struct hda_stream_format stream_format;
2487 CA0132_DSP_LOG("---- dspload_image begin ------");
2488 if (router_chans == 0) {
2490 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2492 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2495 stream_format.sample_rate = 48000;
2496 stream_format.number_channels = (unsigned short)router_chans;
2498 while (stream_format.number_channels > 16) {
2499 stream_format.sample_rate *= 2;
2500 stream_format.number_channels /= 2;
2503 stream_format.container_size = 32;
2504 stream_format.valid_bits_per_sample = 32;
2507 CA0132_DSP_LOG("Ready to program DMA");
2509 status = dsp_reset(codec);
2514 CA0132_DSP_LOG("dsp_reset() complete");
2515 status = dspxfr_image(codec, fls, reloc, &stream_format, ovly);
2520 CA0132_DSP_LOG("dspxfr_image() complete");
2521 if (autostart && !ovly) {
2522 dspload_post_setup(codec);
2523 status = dsp_set_run_state(codec);
2526 CA0132_DSP_LOG("LOAD FINISHED");
2532 static int dspload_get_speakereq_addx(struct hda_codec *codec,
2537 struct { unsigned short y, x; } speakereq_info;
2538 unsigned int size = sizeof(speakereq_info);
2540 CA0132_DSP_LOG("dspload_get_speakereq_addx() -- begin");
2541 status = dspio_scp(codec, MASTERCONTROL,
2542 MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS,
2543 SCP_GET, NULL, 0, &speakereq_info, &size);
2545 if (FAILED(status)) {
2546 CA0132_DSP_LOG("dspload_get_speakereq_addx: SCP Failed");
2550 *x = speakereq_info.x;
2551 *y = speakereq_info.y;
2552 CA0132_LOG("dspload_get_speakereq_addx: X=0x%x Y=0x%x\n", *x, *y);
2554 CA0132_DSP_LOG("dspload_get_speakereq_addx() -- complete");
2559 static const struct firmware *fw_speq;
2560 static const struct firmware *fw_efx;
2562 static int request_firmware_cached(const struct firmware **firmware_p,
2563 const char *name, struct device *device)
2566 return 0; /* already loaded */
2567 return request_firmware(firmware_p, name, device);
2570 static void release_cached_firmware(void)
2573 release_firmware(fw_speq);
2577 release_firmware(fw_efx);
2582 static int dspload_speakereq(struct hda_codec *codec)
2585 const struct dsp_image_seg *image;
2588 CA0132_DSP_LOG("dspload_speakereq() -- begin");
2590 if (request_firmware_cached(&fw_speq, SPEQ_FILE,
2591 codec->bus->card->dev) != 0)
2594 image = (struct dsp_image_seg *)(fw_speq->data);
2596 status = dspload_get_speakereq_addx(codec, &x, &y);
2600 status = dspload_image(codec, image, 1, y, 0, 8);
2603 CA0132_DSP_LOG("dspload_speakereq() -- complete");
2608 static bool dspload_is_loaded(struct hda_codec *codec)
2610 unsigned int data = 0;
2613 status = chipio_read(codec, 0x40004, &data);
2614 if (FAILED(status) || (data != 1))
2620 static bool dspload_wait_loaded(struct hda_codec *codec)
2622 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
2625 if (dspload_is_loaded(codec)) {
2626 pr_info("DOWNLOAD OK :-) DSP IS RUNNING.\n");
2630 } while (time_before(jiffies, timeout));
2632 pr_err("DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2639 static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2641 int channel_id, int format)
2643 unsigned int oldval, newval;
2649 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2650 "channel=%d, format=0x%x\n",
2651 nid, stream_tag, channel_id, format);
2653 /* update the format-id if changed */
2654 oldval = snd_hda_codec_read(codec, nid, 0,
2655 AC_VERB_GET_STREAM_FORMAT, 0);
2656 if (oldval != format) {
2658 snd_hda_codec_write(codec, nid, 0,
2659 AC_VERB_SET_STREAM_FORMAT,
2663 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2664 newval = (stream_tag << 4) | channel_id;
2665 if (oldval != newval) {
2666 snd_hda_codec_write(codec, nid, 0,
2667 AC_VERB_SET_CHANNEL_STREAMID,
2672 static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2679 CA0132_LOG("ca0132_cleanup_stream: NID=0x%x\n", nid);
2681 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2685 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2686 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2692 static unsigned int ca0132_get_playback_latency(struct hda_codec *codec)
2694 struct ca0132_spec *spec = codec->spec;
2695 unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
2697 if (!dspload_is_loaded(codec))
2700 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
2701 if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
2702 (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
2703 latency += DSP_PLAY_ENHANCEMENT_LATENCY;
2706 if (spec->cur_out_type == SPEAKER_OUT)
2707 latency += DSP_SPEAKER_OUT_LATENCY;
2711 /* Toggles the audio format to serial and back to parallel. Needed to make sure
2712 * the codec does what we tell it. */
2713 static void ca0132_toggle_dac_format(struct hda_codec *codec)
2716 chipio_read(codec, 0x18B008, &aicr);
2718 chipio_write(codec, 0x18B008, aicr);
2720 chipio_write(codec, 0x18B008, aicr);
2723 static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2724 struct hda_codec *codec,
2725 unsigned int stream_tag,
2726 unsigned int format,
2727 struct snd_pcm_substream *substream)
2729 struct ca0132_spec *spec = codec->spec;
2730 struct snd_pcm_runtime *runtime = substream->runtime;
2731 unsigned int latency = ca0132_get_playback_latency(codec);
2733 if (spec->dsp_state == DSP_DOWNLOADING) {
2734 spec->dsp_stream_id = stream_tag;
2738 runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
2739 runtime->byte_align) / 1000);
2741 ca0132_toggle_dac_format(codec);
2743 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2748 static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2749 struct hda_codec *codec,
2750 struct snd_pcm_substream *substream)
2752 struct ca0132_spec *spec = codec->spec;
2754 if (spec->dsp_state == DSP_DOWNLOADING)
2757 /*Allow stream some time to flush effects tail*/
2760 ca0132_cleanup_stream(codec, spec->dacs[0]);
2768 static unsigned int ca0132_get_capture_latency(struct hda_codec *codec)
2770 struct ca0132_spec *spec = codec->spec;
2771 unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
2773 if (!dspload_is_loaded(codec))
2776 if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
2777 latency += DSP_CRYSTAL_VOICE_LATENCY;
2781 static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2782 struct hda_codec *codec,
2783 unsigned int stream_tag,
2784 unsigned int format,
2785 struct snd_pcm_substream *substream)
2787 struct ca0132_spec *spec = codec->spec;
2788 struct snd_pcm_runtime *runtime = substream->runtime;
2789 unsigned int latency = ca0132_get_capture_latency(codec);
2791 if (spec->dsp_state == DSP_DOWNLOADING)
2794 runtime->delay = bytes_to_frames(runtime, (latency * runtime->rate *
2795 runtime->byte_align) / 1000);
2797 ca0132_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2801 static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2802 struct hda_codec *codec,
2803 struct snd_pcm_substream *substream)
2805 struct ca0132_spec *spec = codec->spec;
2807 if (spec->dsp_state == DSP_DOWNLOADING)
2810 ca0132_cleanup_stream(codec, hinfo->nid);
2820 * Mixer controls helpers.
2822 #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2823 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2825 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2826 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2827 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2828 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2829 .info = ca0132_volume_info, \
2830 .get = ca0132_volume_get, \
2831 .put = ca0132_volume_put, \
2832 .tlv = { .c = ca0132_volume_tlv }, \
2833 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2835 #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2836 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2838 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2839 .info = snd_hda_mixer_amp_switch_info, \
2840 .get = ca0132_switch_get, \
2841 .put = ca0132_switch_put, \
2842 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2845 #define CA0132_CODEC_VOL(xname, nid, dir) \
2846 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2847 #define CA0132_CODEC_MUTE(xname, nid, dir) \
2848 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2851 /* The followings are for tuning of products */
2852 #ifdef ENABLE_TUNING_CONTROLS
2854 static unsigned int voice_focus_vals_lookup[] = {
2855 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
2856 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
2857 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
2858 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
2859 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
2860 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
2861 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
2862 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
2863 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
2864 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
2865 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
2866 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
2867 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
2868 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
2869 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
2870 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
2871 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
2872 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
2873 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
2874 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
2875 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
2876 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
2877 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
2878 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
2879 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
2880 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
2881 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
2884 static unsigned int mic_svm_vals_lookup[] = {
2885 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
2886 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
2887 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
2888 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
2889 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
2890 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
2891 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
2892 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
2893 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
2894 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
2895 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
2896 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
2897 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
2898 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
2899 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
2900 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
2901 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
2904 static unsigned int equalizer_vals_lookup[] = {
2905 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
2906 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
2907 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
2908 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
2909 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
2910 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
2911 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
2912 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
2916 static unsigned int aec_vals_lookup[] = {
2917 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
2918 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
2919 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
2920 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
2921 0x41C00000, 0x41C80000, 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000,
2922 0x41F00000, 0x41F80000, 0x42000000, 0x42040000, 0x42080000, 0x420C0000,
2923 0x42100000, 0x42140000, 0x42180000, 0x421C0000, 0x42200000, 0x42240000,
2924 0x42280000, 0x422C0000, 0x42300000, 0x42340000, 0x42380000, 0x423C0000,
2925 0x42400000, 0x42440000, 0x42480000, 0x424C0000, 0x42500000, 0x42540000,
2926 0x42580000, 0x425C0000, 0x42600000, 0x42640000, 0x42680000, 0x426C0000,
2930 static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
2931 unsigned int *lookup, int idx)
2935 for (i = 0; i < TUNING_CTLS_COUNT; i++)
2936 if (nid == ca0132_tuning_ctls[i].nid)
2939 snd_hda_power_up(codec);
2940 dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
2941 ca0132_tuning_ctls[i].req,
2942 &(lookup[idx]), sizeof(unsigned int));
2943 snd_hda_power_down(codec);
2948 static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2952 struct ca0132_spec *spec = codec->spec;
2953 hda_nid_t nid = get_amp_nid(kcontrol);
2954 long *valp = ucontrol->value.integer.value;
2955 int idx = nid - TUNING_CTL_START_NID;
2957 *valp = spec->cur_ctl_vals[idx];
2961 static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_info *uinfo)
2964 int chs = get_amp_channels(kcontrol);
2965 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2966 uinfo->count = chs == 3 ? 2 : 1;
2967 uinfo->value.integer.min = 20;
2968 uinfo->value.integer.max = 180;
2969 uinfo->value.integer.step = 1;
2974 static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 struct ca0132_spec *spec = codec->spec;
2979 hda_nid_t nid = get_amp_nid(kcontrol);
2980 long *valp = ucontrol->value.integer.value;
2983 idx = nid - TUNING_CTL_START_NID;
2985 if (spec->cur_ctl_vals[idx] == *valp)
2988 spec->cur_ctl_vals[idx] = *valp;
2991 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
2996 static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_info *uinfo)
2999 int chs = get_amp_channels(kcontrol);
3000 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3001 uinfo->count = chs == 3 ? 2 : 1;
3002 uinfo->value.integer.min = 0;
3003 uinfo->value.integer.max = 100;
3004 uinfo->value.integer.step = 1;
3009 static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
3012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3013 struct ca0132_spec *spec = codec->spec;
3014 hda_nid_t nid = get_amp_nid(kcontrol);
3015 long *valp = ucontrol->value.integer.value;
3018 idx = nid - TUNING_CTL_START_NID;
3020 if (spec->cur_ctl_vals[idx] == *valp)
3023 spec->cur_ctl_vals[idx] = *valp;
3026 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
3031 static int aec_ctl_info(struct snd_kcontrol *kcontrol,
3032 struct snd_ctl_elem_info *uinfo)
3034 int chs = get_amp_channels(kcontrol);
3035 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3036 uinfo->count = chs == 3 ? 2 : 1;
3037 uinfo->value.integer.min = 0;
3038 uinfo->value.integer.max = 60;
3039 uinfo->value.integer.step = 1;
3044 static int aec_ctl_put(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3048 struct ca0132_spec *spec = codec->spec;
3049 hda_nid_t nid = get_amp_nid(kcontrol);
3050 long *valp = ucontrol->value.integer.value;
3053 idx = nid - TUNING_CTL_START_NID;
3055 if (spec->cur_ctl_vals[idx] == *valp)
3058 spec->cur_ctl_vals[idx] = *valp;
3061 tuning_ctl_set(codec, nid, aec_vals_lookup, idx);
3066 static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_info *uinfo)
3069 int chs = get_amp_channels(kcontrol);
3070 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3071 uinfo->count = chs == 3 ? 2 : 1;
3072 uinfo->value.integer.min = 0;
3073 uinfo->value.integer.max = 48;
3074 uinfo->value.integer.step = 1;
3079 static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
3082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3083 struct ca0132_spec *spec = codec->spec;
3084 hda_nid_t nid = get_amp_nid(kcontrol);
3085 long *valp = ucontrol->value.integer.value;
3088 idx = nid - TUNING_CTL_START_NID;
3090 if (spec->cur_ctl_vals[idx] == *valp)
3093 spec->cur_ctl_vals[idx] = *valp;
3096 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3101 DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3102 DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
3104 static int add_tuning_control(struct hda_codec *codec,
3105 hda_nid_t pnid, hda_nid_t nid,
3106 const char *name, int dir)
3109 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3110 struct snd_kcontrol_new knew =
3111 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3113 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3114 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3119 knew.info = voice_focus_ctl_info;
3120 knew.get = tuning_ctl_get;
3121 knew.put = voice_focus_ctl_put;
3122 knew.tlv.p = voice_focus_db_scale;
3125 knew.info = mic_svm_ctl_info;
3126 knew.get = tuning_ctl_get;
3127 knew.put = mic_svm_ctl_put;
3130 knew.info = equalizer_ctl_info;
3131 knew.get = tuning_ctl_get;
3132 knew.put = equalizer_ctl_put;
3133 knew.tlv.p = eq_db_scale;
3135 case ECHO_CANCELLATION:
3136 knew.info = aec_ctl_info;
3137 knew.get = tuning_ctl_get;
3138 knew.put = aec_ctl_put;
3143 knew.private_value =
3144 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3145 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3146 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3149 static int add_tuning_ctls(struct hda_codec *codec)
3154 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3155 err = add_tuning_control(codec,
3156 ca0132_tuning_ctls[i].parent_nid,
3157 ca0132_tuning_ctls[i].nid,
3158 ca0132_tuning_ctls[i].name,
3159 ca0132_tuning_ctls[i].direct);
3167 static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3169 struct ca0132_spec *spec = codec->spec;
3172 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3173 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3174 /* SVM level defaults to 0.74. */
3175 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3177 /* EQ defaults to 0dB. */
3178 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3179 spec->cur_ctl_vals[i] = 24;
3183 static int ca0132_select_out(struct hda_codec *codec)
3185 struct ca0132_spec *spec = codec->spec;
3186 unsigned int pin_ctl;
3192 CA0132_LOG("ca0132_select_out\n");
3194 snd_hda_power_up(codec);
3196 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3199 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
3202 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3205 spec->cur_out_type = HEADPHONE_OUT;
3207 spec->cur_out_type = SPEAKER_OUT;
3209 if (spec->cur_out_type == SPEAKER_OUT) {
3210 CA0132_LOG("ca0132_select_out speaker\n");
3211 /*speaker out config*/
3213 err = dspio_set_param(codec, 0x80, 0x04,
3214 &tmp, sizeof(unsigned int));
3217 /*enable speaker EQ*/
3219 err = dspio_set_param(codec, 0x8f, 0x00,
3220 &tmp, sizeof(unsigned int));
3225 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3226 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3227 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3228 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3229 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3230 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3231 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3232 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3234 /* disable headphone node */
3235 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3236 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3237 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3238 AC_VERB_SET_PIN_WIDGET_CONTROL,
3240 /* enable speaker node */
3241 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3242 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3243 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3244 AC_VERB_SET_PIN_WIDGET_CONTROL,
3247 CA0132_LOG("ca0132_select_out hp\n");
3248 /*headphone out config*/
3250 err = dspio_set_param(codec, 0x80, 0x04,
3251 &tmp, sizeof(unsigned int));
3254 /*disable speaker EQ*/
3256 err = dspio_set_param(codec, 0x8f, 0x00,
3257 &tmp, sizeof(unsigned int));
3262 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3263 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3264 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3265 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3266 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3267 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3268 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3269 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3271 /* disable speaker*/
3272 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3273 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3274 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3275 AC_VERB_SET_PIN_WIDGET_CONTROL,
3277 /* enable headphone*/
3278 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3279 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3280 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3281 AC_VERB_SET_PIN_WIDGET_CONTROL,
3286 snd_hda_power_down(codec);
3288 return err < 0 ? err : 0;
3291 static void ca0132_unsol_hp_delayed(struct work_struct *work)
3293 struct ca0132_spec *spec = container_of(
3294 to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
3295 ca0132_select_out(spec->codec);
3296 snd_hda_jack_report_sync(spec->codec);
3299 static void ca0132_set_dmic(struct hda_codec *codec, int enable);
3300 static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
3301 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
3303 static int ca0132_set_vipsource(struct hda_codec *codec, int val)
3305 struct ca0132_spec *spec = codec->spec;
3308 if (!dspload_is_loaded(codec))
3311 /* if CrystalVoice if off, vipsource should be 0 */
3312 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]
3314 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3315 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3316 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3317 if (spec->cur_mic_type == DIGITAL_MIC)
3321 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
3323 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
3325 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3326 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3327 if (spec->cur_mic_type == DIGITAL_MIC)
3331 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
3333 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
3335 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3341 static int ca0132_select_mic(struct hda_codec *codec)
3343 struct ca0132_spec *spec = codec->spec;
3347 CA0132_LOG("ca0132_select_mic\n");
3349 snd_hda_power_up(codec);
3351 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3354 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3357 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3360 spec->cur_mic_type = LINE_MIC_IN;
3362 spec->cur_mic_type = DIGITAL_MIC;
3364 if (spec->cur_mic_type == DIGITAL_MIC) {
3365 /* enable digital Mic */
3366 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3367 ca0132_set_dmic(codec, 1);
3368 ca0132_mic_boost_set(codec, 0);
3369 /* set voice focus */
3370 ca0132_effects_set(codec, VOICE_FOCUS,
3371 spec->effects_switch
3372 [VOICE_FOCUS - EFFECT_START_NID]);
3374 /* disable digital Mic */
3375 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3376 ca0132_set_dmic(codec, 0);
3377 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3378 /* disable voice focus */
3379 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3382 snd_hda_power_down(codec);
3388 * Check if VNODE settings take effect immediately.
3390 static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3392 hda_nid_t *shared_nid)
3394 struct ca0132_spec *spec = codec->spec;
3396 bool effective = false;
3400 nid = spec->shared_out_nid;
3404 nid = spec->shared_mic_nid;
3411 if (effective && shared_nid)
3418 * The following functions are control change helpers.
3419 * They return 0 if no changed. Return 1 if changed.
3421 static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3423 struct ca0132_spec *spec = codec->spec;
3426 /* based on CrystalVoice state to enable VoiceFX. */
3428 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3429 FLOAT_ONE : FLOAT_ZERO;
3434 dspio_set_param(codec, ca0132_voicefx.mid,
3435 ca0132_voicefx.reqs[0], &tmp, sizeof(unsigned int));
3440 static void ca0132_update_latency(struct hda_codec *codec,
3441 unsigned int direction)
3443 struct ca0132_spec *spec = codec->spec;
3444 unsigned int latency;
3445 struct snd_pcm_substream *substr;
3446 struct snd_pcm_runtime *runtime;
3448 if (spec->dsp_state == DSP_DOWNLOADING)
3451 latency = (direction == SNDRV_PCM_STREAM_PLAYBACK)
3452 ? ca0132_get_playback_latency(codec)
3453 : ca0132_get_capture_latency(codec);
3455 substr = codec->pcm_info->pcm->streams[direction].substream;
3457 runtime = substr->runtime;
3458 /* update latency only when runtime is setup */
3459 if (runtime && runtime->frame_bits)
3460 runtime->delay = bytes_to_frames(runtime,
3461 (latency * runtime->rate *
3462 runtime->byte_align) / 1000);
3463 substr = substr->next;
3467 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3469 struct ca0132_spec *spec = codec->spec;
3471 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3473 int idx = nid - EFFECT_START_NID;
3475 if ((idx < 0) || (idx >= num_fx))
3476 return 0; /* no changed */
3478 /* for out effect, qualify with PE */
3479 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3480 /* if PE if off, turn off out effects. */
3481 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3485 /* for in effect, qualify with CrystalVoice */
3486 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3487 /* if CrystalVoice if off, turn off in effects. */
3488 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3491 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3492 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3496 CA0132_LOG("ca0132_effect_set: nid=0x%x, val=%ld\n", nid, val);
3498 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3499 err = dspio_set_param(codec, ca0132_effects[idx].mid,
3500 ca0132_effects[idx].reqs[0],
3501 &on, sizeof(unsigned int));
3503 ca0132_update_latency(codec, SNDRV_PCM_STREAM_PLAYBACK);
3504 ca0132_update_latency(codec, SNDRV_PCM_STREAM_CAPTURE);
3507 return 0; /* no changed */
3512 static int ca0132_pe_switch_set(struct hda_codec *codec)
3514 struct ca0132_spec *spec = codec->spec;
3518 CA0132_LOG("ca0132_pe_switch_set: val=%ld\n",
3519 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3521 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3522 nid = OUT_EFFECT_START_NID;
3523 /* PE affects all out effects */
3524 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3525 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3530 /* Check if Mic1 is streaming, if so, stop streaming */
3531 static int stop_mic1(struct hda_codec *codec)
3533 struct ca0132_spec *spec = codec->spec;
3534 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
3535 AC_VERB_GET_CONV, 0);
3537 snd_hda_codec_write(codec, spec->adcs[0], 0,
3538 AC_VERB_SET_CHANNEL_STREAMID,
3543 /* Resume Mic1 streaming if it was stopped. */
3544 static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
3546 struct ca0132_spec *spec = codec->spec;
3547 /* Restore the previous stream and channel */
3549 snd_hda_codec_write(codec, spec->adcs[0], 0,
3550 AC_VERB_SET_CHANNEL_STREAMID,
3554 static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3556 struct ca0132_spec *spec = codec->spec;
3559 unsigned int oldval;
3561 CA0132_LOG("ca0132_cvoice_switch_set: val=%ld\n",
3562 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3564 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3565 nid = IN_EFFECT_START_NID;
3566 /* CrystalVoice affects all in effects */
3567 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3568 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3570 /* including VoiceFX */
3571 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3573 /* set correct vipsource */
3574 oldval = stop_mic1(codec);
3575 ret |= ca0132_set_vipsource(codec, 1);
3576 resume_mic1(codec, oldval);
3580 static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3582 struct ca0132_spec *spec = codec->spec;
3586 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3587 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3589 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3590 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3595 static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3596 struct snd_ctl_elem_value *ucontrol)
3598 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3599 hda_nid_t nid = get_amp_nid(kcontrol);
3600 hda_nid_t shared_nid = 0;
3603 struct ca0132_spec *spec = codec->spec;
3606 if (nid == VNID_HP_SEL) {
3608 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3610 ca0132_select_out(codec);
3614 if (nid == VNID_AMIC1_SEL) {
3616 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3618 ca0132_select_mic(codec);
3622 if (nid == VNID_HP_ASEL) {
3623 ca0132_select_out(codec);
3627 if (nid == VNID_AMIC1_ASEL) {
3628 ca0132_select_mic(codec);
3632 /* if effective conditions, then update hw immediately. */
3633 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3635 int dir = get_amp_direction(kcontrol);
3636 int ch = get_amp_channels(kcontrol);
3639 mutex_lock(&codec->control_mutex);
3640 pval = kcontrol->private_value;
3641 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3643 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3644 kcontrol->private_value = pval;
3645 mutex_unlock(&codec->control_mutex);
3650 /* End of control change helpers. */
3652 static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3653 struct snd_ctl_elem_info *uinfo)
3655 unsigned int items = sizeof(ca0132_voicefx_presets)
3656 / sizeof(struct ct_voicefx_preset);
3658 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3660 uinfo->value.enumerated.items = items;
3661 if (uinfo->value.enumerated.item >= items)
3662 uinfo->value.enumerated.item = items - 1;
3663 strcpy(uinfo->value.enumerated.name,
3664 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3668 static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
3669 struct snd_ctl_elem_value *ucontrol)
3671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3672 struct ca0132_spec *spec = codec->spec;
3674 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
3678 static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
3679 struct snd_ctl_elem_value *ucontrol)
3681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3682 struct ca0132_spec *spec = codec->spec;
3684 int sel = ucontrol->value.enumerated.item[0];
3685 unsigned int items = sizeof(ca0132_voicefx_presets)
3686 / sizeof(struct ct_voicefx_preset);
3691 CA0132_LOG("ca0132_voicefx_put: sel=%d, preset=%s\n",
3692 sel, ca0132_voicefx_presets[sel].name);
3696 * Default needs to qualify with CrystalVoice state.
3698 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3699 err = dspio_set_param(codec, ca0132_voicefx.mid,
3700 ca0132_voicefx.reqs[i],
3701 &(ca0132_voicefx_presets[sel].vals[i]),
3702 sizeof(unsigned int));
3708 spec->voicefx_val = sel;
3709 /* enable voice fx */
3710 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3716 static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3717 struct snd_ctl_elem_value *ucontrol)
3719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3720 struct ca0132_spec *spec = codec->spec;
3721 hda_nid_t nid = get_amp_nid(kcontrol);
3722 int ch = get_amp_channels(kcontrol);
3723 long *valp = ucontrol->value.integer.value;
3726 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3728 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3732 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3738 /* effects, include PE and CrystalVoice */
3739 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3740 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3745 if (nid == spec->input_pins[0]) {
3746 *valp = spec->cur_mic_boost;
3753 static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3754 struct snd_ctl_elem_value *ucontrol)
3756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3757 struct ca0132_spec *spec = codec->spec;
3758 hda_nid_t nid = get_amp_nid(kcontrol);
3759 int ch = get_amp_channels(kcontrol);
3760 long *valp = ucontrol->value.integer.value;
3763 CA0132_LOG("ca0132_switch_put: nid=0x%x, val=%ld\n", nid, *valp);
3765 snd_hda_power_up(codec);
3767 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3769 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3773 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3776 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3781 if (nid == PLAY_ENHANCEMENT) {
3782 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3783 changed = ca0132_pe_switch_set(codec);
3788 if (nid == CRYSTAL_VOICE) {
3789 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3790 changed = ca0132_cvoice_switch_set(codec);
3794 /* out and in effects */
3795 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3796 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3797 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3798 changed = ca0132_effects_set(codec, nid, *valp);
3803 if (nid == spec->input_pins[0]) {
3804 spec->cur_mic_boost = *valp;
3806 /* Mic boost does not apply to Digital Mic */
3807 if (spec->cur_mic_type != DIGITAL_MIC)
3808 changed = ca0132_mic_boost_set(codec, *valp);
3813 snd_hda_power_down(codec);
3820 static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3821 struct snd_ctl_elem_info *uinfo)
3823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3824 struct ca0132_spec *spec = codec->spec;
3825 hda_nid_t nid = get_amp_nid(kcontrol);
3826 int ch = get_amp_channels(kcontrol);
3827 int dir = get_amp_direction(kcontrol);
3833 /* follow shared_out info */
3834 nid = spec->shared_out_nid;
3835 mutex_lock(&codec->control_mutex);
3836 pval = kcontrol->private_value;
3837 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3838 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3839 kcontrol->private_value = pval;
3840 mutex_unlock(&codec->control_mutex);
3843 /* follow shared_mic info */
3844 nid = spec->shared_mic_nid;
3845 mutex_lock(&codec->control_mutex);
3846 pval = kcontrol->private_value;
3847 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3848 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3849 kcontrol->private_value = pval;
3850 mutex_unlock(&codec->control_mutex);
3853 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3858 static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
3859 struct snd_ctl_elem_value *ucontrol)
3861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3862 struct ca0132_spec *spec = codec->spec;
3863 hda_nid_t nid = get_amp_nid(kcontrol);
3864 int ch = get_amp_channels(kcontrol);
3865 long *valp = ucontrol->value.integer.value;
3867 /* store the left and right volume */
3869 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3873 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3879 static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
3880 struct snd_ctl_elem_value *ucontrol)
3882 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3883 struct ca0132_spec *spec = codec->spec;
3884 hda_nid_t nid = get_amp_nid(kcontrol);
3885 int ch = get_amp_channels(kcontrol);
3886 long *valp = ucontrol->value.integer.value;
3887 hda_nid_t shared_nid = 0;
3891 /* store the left and right volume */
3893 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3897 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3901 /* if effective conditions, then update hw immediately. */
3902 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3904 int dir = get_amp_direction(kcontrol);
3907 snd_hda_power_up(codec);
3908 mutex_lock(&codec->control_mutex);
3909 pval = kcontrol->private_value;
3910 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3912 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3913 kcontrol->private_value = pval;
3914 mutex_unlock(&codec->control_mutex);
3915 snd_hda_power_down(codec);
3921 static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3922 unsigned int size, unsigned int __user *tlv)
3924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3925 struct ca0132_spec *spec = codec->spec;
3926 hda_nid_t nid = get_amp_nid(kcontrol);
3927 int ch = get_amp_channels(kcontrol);
3928 int dir = get_amp_direction(kcontrol);
3934 /* follow shared_out tlv */
3935 nid = spec->shared_out_nid;
3936 mutex_lock(&codec->control_mutex);
3937 pval = kcontrol->private_value;
3938 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3939 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3940 kcontrol->private_value = pval;
3941 mutex_unlock(&codec->control_mutex);
3944 /* follow shared_mic tlv */
3945 nid = spec->shared_mic_nid;
3946 mutex_lock(&codec->control_mutex);
3947 pval = kcontrol->private_value;
3948 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3949 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3950 kcontrol->private_value = pval;
3951 mutex_unlock(&codec->control_mutex);
3954 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3959 static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3960 const char *pfx, int dir)
3963 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3964 struct snd_kcontrol_new knew =
3965 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3966 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
3967 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3970 static int add_voicefx(struct hda_codec *codec)
3972 struct snd_kcontrol_new knew =
3973 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3974 VOICEFX, 1, 0, HDA_INPUT);
3975 knew.info = ca0132_voicefx_info;
3976 knew.get = ca0132_voicefx_get;
3977 knew.put = ca0132_voicefx_put;
3978 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
3982 * When changing Node IDs for Mixer Controls below, make sure to update
3983 * Node IDs in ca0132_config() as well.
3985 static struct snd_kcontrol_new ca0132_mixer[] = {
3986 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3987 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3988 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3989 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3990 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3991 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3992 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3993 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3994 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3995 0x12, 1, HDA_INPUT),
3996 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3997 VNID_HP_SEL, 1, HDA_OUTPUT),
3998 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3999 VNID_AMIC1_SEL, 1, HDA_INPUT),
4000 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
4001 VNID_HP_ASEL, 1, HDA_OUTPUT),
4002 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
4003 VNID_AMIC1_ASEL, 1, HDA_INPUT),
4007 static int ca0132_build_controls(struct hda_codec *codec)
4009 struct ca0132_spec *spec = codec->spec;
4013 /* Add Mixer controls */
4014 for (i = 0; i < spec->num_mixers; i++) {
4015 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
4020 /* Add in and out effects controls.
4021 * VoiceFX, PE and CrystalVoice are added separately.
4023 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4024 for (i = 0; i < num_fx; i++) {
4025 err = add_fx_switch(codec, ca0132_effects[i].nid,
4026 ca0132_effects[i].name,
4027 ca0132_effects[i].direct);
4032 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
4036 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
4042 #ifdef ENABLE_TUNING_CONTROLS
4043 add_tuning_ctls(codec);
4046 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4056 static struct hda_pcm_stream ca0132_pcm_analog_playback = {
4061 .prepare = ca0132_playback_pcm_prepare,
4062 .cleanup = ca0132_playback_pcm_cleanup
4066 static struct hda_pcm_stream ca0132_pcm_analog_capture = {
4071 .prepare = ca0132_capture_pcm_prepare,
4072 .cleanup = ca0132_capture_pcm_cleanup
4076 static int ca0132_build_pcms(struct hda_codec *codec)
4078 struct ca0132_spec *spec = codec->spec;
4079 struct hda_pcm *info = spec->pcm_rec;
4081 codec->pcm_info = info;
4082 codec->num_pcms = 0;
4084 info->name = "CA0132 Analog";
4085 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
4086 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
4087 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4088 spec->multiout.max_channels;
4089 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4090 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4091 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
4095 info->name = "CA0132 Analog Mic-In2";
4096 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4097 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4098 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
4102 info->name = "CA0132 What U Hear";
4103 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4104 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4105 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
4111 static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
4114 snd_hda_codec_write(codec, pin, 0,
4115 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
4116 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
4117 snd_hda_codec_write(codec, pin, 0,
4118 AC_VERB_SET_AMP_GAIN_MUTE,
4122 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4126 static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
4129 snd_hda_codec_write(codec, pin, 0,
4130 AC_VERB_SET_PIN_WIDGET_CONTROL,
4132 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
4133 snd_hda_codec_write(codec, pin, 0,
4134 AC_VERB_SET_AMP_GAIN_MUTE,
4138 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4141 /* init to 0 dB and unmute. */
4142 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4143 HDA_AMP_VOLMASK, 0x5a);
4144 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4149 static void ca0132_init_unsol(struct hda_codec *codec)
4151 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
4152 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
4155 static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
4159 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
4160 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
4161 snd_hda_override_amp_caps(codec, nid, dir, caps);
4164 static void ca0132_set_dmic_gain(struct hda_codec *codec, unsigned int gain)
4166 unsigned int mic_gain_reg;
4167 chipio_read(codec, 0x18B098, &mic_gain_reg);
4168 mic_gain_reg = (mic_gain_reg & ~0x1F) | gain;
4169 chipio_write(codec, 0x18B098, mic_gain_reg);
4171 chipio_read(codec, 0x18B09C, &mic_gain_reg);
4172 mic_gain_reg = (mic_gain_reg & ~0x1F) | gain;
4173 chipio_write(codec, 0x18B09C, mic_gain_reg);
4176 static void ca0132_set_dmic(struct hda_codec *codec, int enable)
4178 struct ca0132_spec *spec = codec->spec;
4181 unsigned int oldval;
4183 CA0132_LOG("ca0132_set_dmic: enable=%d\n", enable);
4185 oldval = stop_mic1(codec);
4186 ca0132_set_vipsource(codec, 0);
4188 /* set DMic input as 2-ch */
4190 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4192 val = spec->dmic_ctl;
4194 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4195 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4197 if (!(spec->dmic_ctl & 0x20))
4198 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
4200 ca0132_set_dmic_gain(codec, 0x14);
4202 /* set AMic input as mono */
4204 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4206 val = spec->dmic_ctl;
4207 /* clear bit7 and bit5 to disable dmic */
4209 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4210 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4212 if (!(spec->dmic_ctl & 0x20))
4213 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
4215 ca0132_set_dmic_gain(codec, 0x0);
4217 ca0132_set_vipsource(codec, 1);
4218 resume_mic1(codec, oldval);
4221 static void ca0132_init_dmic(struct hda_codec *codec)
4223 struct ca0132_spec *spec = codec->spec;
4226 /* Setup Digital Mic here, but don't enable.
4227 * Enable based on jack detect.
4230 /* MCLK uses MPIO1, set to enable.
4231 * Bit 2-0: MPIO select
4232 * Bit 3: set to disable
4236 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4237 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
4239 /* Data1 uses MPIO3. Data2 not use
4240 * Bit 2-0: Data1 MPIO select
4241 * Bit 3: set disable Data1
4242 * Bit 6-4: Data2 MPIO select
4243 * Bit 7: set disable Data2
4246 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4247 VENDOR_CHIPIO_DMIC_PIN_SET, val);
4249 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
4250 * Bit 3-0: Channel mask
4251 * Bit 4: set for 48KHz, clear for 32KHz
4253 * Bit 6: set to select Data2, clear for Data1
4254 * Bit 7: set to enable DMic, clear for AMic
4257 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
4258 spec->dmic_ctl = val;
4259 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4260 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4263 static void ca0132_init_analog_mic2(struct hda_codec *codec)
4265 struct ca0132_spec *spec = codec->spec;
4267 mutex_lock(&spec->chipio_mutex);
4268 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4269 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
4270 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4271 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4272 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4273 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4274 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4275 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
4276 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4277 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4278 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4279 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4280 mutex_unlock(&spec->chipio_mutex);
4283 static void ca0132_refresh_widget_caps(struct hda_codec *codec)
4285 struct ca0132_spec *spec = codec->spec;
4289 CA0132_LOG("ca0132_refresh_widget_caps.\n");
4290 nid = codec->start_nid;
4291 for (i = 0; i < codec->num_nodes; i++, nid++)
4292 codec->wcaps[i] = snd_hda_param_read(codec, nid,
4293 AC_PAR_AUDIO_WIDGET_CAP);
4295 for (i = 0; i < spec->multiout.num_dacs; i++)
4296 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
4298 for (i = 0; i < spec->num_outputs; i++)
4299 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
4301 for (i = 0; i < spec->num_inputs; i++) {
4302 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
4303 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
4307 static void ca0132_setup_defaults(struct hda_codec *codec)
4313 if (!dspload_is_loaded(codec))
4316 /* out, in effects + voicefx */
4317 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
4318 for (idx = 0; idx < num_fx; idx++) {
4319 for (i = 0; i <= ca0132_effects[idx].params; i++) {
4320 dspio_set_param(codec, ca0132_effects[idx].mid,
4321 ca0132_effects[idx].reqs[i],
4322 &(ca0132_effects[idx].def_vals[i]),
4323 sizeof(unsigned int));
4327 /*remove DSP headroom*/
4329 dspio_set_param(codec, 0x96, 0x3C, &tmp, sizeof(unsigned int));
4331 /*set speaker EQ bypass attenuation*/
4332 dspio_set_param(codec, 0x8f, 0x01, &tmp, sizeof(unsigned int));
4334 /* set AMic1 and AMic2 as mono mic */
4336 dspio_set_param(codec, 0x80, 0x00, &tmp, sizeof(unsigned int));
4337 dspio_set_param(codec, 0x80, 0x01, &tmp, sizeof(unsigned int));
4339 /* set AMic1 as CrystalVoice input */
4341 dspio_set_param(codec, 0x80, 0x05, &tmp, sizeof(unsigned int));
4343 /* set WUH source */
4345 dspio_set_param(codec, 0x31, 0x00, &tmp, sizeof(unsigned int));
4349 dspio_set_param(codec, 0x86, 0x05, &tmp, sizeof(unsigned int));
4352 static void ca0132_init_flags(struct hda_codec *codec)
4354 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4355 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4356 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4357 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4358 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4359 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4362 static void ca0132_init_params(struct hda_codec *codec)
4364 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4365 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4368 static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4370 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4371 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4372 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4373 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4374 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4375 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4377 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4378 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4379 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4382 static bool ca0132_download_dsp_images(struct hda_codec *codec)
4384 bool dsp_loaded = false;
4385 const struct dsp_image_seg *dsp_os_image;
4387 if (request_firmware_cached(&fw_efx, EFX_FILE,
4388 codec->bus->card->dev) != 0)
4391 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
4392 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4393 dsp_loaded = dspload_wait_loaded(codec);
4396 dspload_speakereq(codec);
4401 static void ca0132_download_dsp(struct hda_codec *codec)
4403 struct ca0132_spec *spec = codec->spec;
4405 spec->dsp_state = DSP_DOWNLOAD_INIT;
4407 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4408 chipio_enable_clocks(codec);
4409 spec->dsp_state = DSP_DOWNLOADING;
4410 if (!ca0132_download_dsp_images(codec))
4411 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4413 spec->dsp_state = DSP_DOWNLOADED;
4416 if (spec->dsp_state == DSP_DOWNLOADED)
4417 ca0132_set_dsp_msr(codec, true);
4420 static void ca0132_process_dsp_response(struct hda_codec *codec)
4422 struct ca0132_spec *spec = codec->spec;
4424 CA0132_LOG("ca0132_process_dsp_response\n");
4425 if (spec->wait_scp) {
4426 if (dspio_get_response_data(codec) >= 0)
4430 dspio_clear_response_queue(codec);
4433 static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4435 struct ca0132_spec *spec = codec->spec;
4436 CA0132_LOG("ca0132_unsol_event: 0x%x\n", res);
4439 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4440 ca0132_process_dsp_response(codec);
4442 res = snd_hda_jack_get_action(codec,
4443 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4445 CA0132_LOG("snd_hda_jack_get_action: 0x%x\n", res);
4449 cancel_delayed_work_sync(&spec->unsol_hp_work);
4450 queue_delayed_work(codec->bus->workq,
4451 &spec->unsol_hp_work,
4452 msecs_to_jiffies(250));
4454 case UNSOL_TAG_AMIC1:
4455 ca0132_select_mic(codec);
4456 snd_hda_jack_report_sync(codec);
4468 /* Sends before DSP download. */
4469 static struct hda_verb ca0132_base_init_verbs[] = {
4470 /*enable ct extension*/
4471 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4472 /*enable DSP node unsol, needed for DSP download*/
4473 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4478 static struct hda_verb ca0132_base_exit_verbs[] = {
4480 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4481 /*disable ct extension*/
4482 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4486 /* Other verbs tables. Sends after DSP download. */
4487 static struct hda_verb ca0132_init_verbs0[] = {
4488 /* chip init verbs */
4489 {0x15, 0x70D, 0xF0},
4490 {0x15, 0x70E, 0xFE},
4491 {0x15, 0x707, 0x75},
4492 {0x15, 0x707, 0xD3},
4493 {0x15, 0x707, 0x09},
4494 {0x15, 0x707, 0x53},
4495 {0x15, 0x707, 0xD4},
4496 {0x15, 0x707, 0xEF},
4497 {0x15, 0x707, 0x75},
4498 {0x15, 0x707, 0xD3},
4499 {0x15, 0x707, 0x09},
4500 {0x15, 0x707, 0x02},
4501 {0x15, 0x707, 0x37},
4502 {0x15, 0x707, 0x78},
4503 {0x15, 0x53C, 0xCE},
4504 {0x15, 0x575, 0xC9},
4505 {0x15, 0x53D, 0xCE},
4506 {0x15, 0x5B7, 0xC9},
4507 {0x15, 0x70D, 0xE8},
4508 {0x15, 0x70E, 0xFE},
4509 {0x15, 0x707, 0x02},
4510 {0x15, 0x707, 0x68},
4511 {0x15, 0x707, 0x62},
4512 {0x15, 0x53A, 0xCE},
4513 {0x15, 0x546, 0xC9},
4514 {0x15, 0x53B, 0xCE},
4515 {0x15, 0x5E8, 0xC9},
4516 {0x15, 0x717, 0x0D},
4517 {0x15, 0x718, 0x20},
4521 static struct hda_verb ca0132_init_verbs1[] = {
4522 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4523 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4524 /* Set the default configuration for the mic pin because the
4525 * value set in BIOS will be reset after ct extension is enabled.
4527 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_0, 0xf0},
4528 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_1, 0x10},
4529 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_2, 0xa1},
4530 {0x12, AC_VERB_SET_CONFIG_DEFAULT_BYTES_3, 0x03},
4532 {0x0b, 0x78D, 0x00},
4533 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4534 /*{0x10, 0x78D, 0x02},*/
4535 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4539 static void ca0132_init_chip(struct hda_codec *codec)
4541 struct ca0132_spec *spec = codec->spec;
4546 mutex_init(&spec->chipio_mutex);
4548 spec->cur_out_type = SPEAKER_OUT;
4549 spec->cur_mic_type = DIGITAL_MIC;
4550 spec->cur_mic_boost = 0;
4552 for (i = 0; i < VNODES_COUNT; i++) {
4553 spec->vnode_lvol[i] = 0x5a;
4554 spec->vnode_rvol[i] = 0x5a;
4555 spec->vnode_lswitch[i] = 0;
4556 spec->vnode_rswitch[i] = 0;
4560 * Default states for effects are in ca0132_effects[].
4562 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4563 for (i = 0; i < num_fx; i++) {
4564 on = (unsigned int)ca0132_effects[i].reqs[0];
4565 spec->effects_switch[i] = on ? 1 : 0;
4568 spec->voicefx_val = 0;
4569 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4570 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4572 #ifdef ENABLE_TUNING_CONTROLS
4573 ca0132_init_tuning_defaults(codec);
4577 static void ca0132_exit_chip(struct hda_codec *codec)
4579 /* put any chip cleanup stuffs here. */
4581 if (dspload_is_loaded(codec))
4585 static int xdata_read(struct hda_codec *codec, u16 addr)
4587 struct ca0132_spec *spec = codec->spec;
4590 mutex_lock(&spec->chipio_mutex);
4591 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4592 VENDOR_CHIPIO_8051_ADDRESS_LOW, addr & 0xFF);
4593 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4594 VENDOR_CHIPIO_8051_ADDRESS_HIGH,
4595 (addr >> 8) & 0xFF);
4596 data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
4597 VENDOR_CHIPIO_8051_DATA_READ, 0);
4598 mutex_unlock(&spec->chipio_mutex);
4603 static int xdata_write(struct hda_codec *codec, u16 addr, u8 value)
4605 struct ca0132_spec *spec = codec->spec;
4607 mutex_lock(&spec->chipio_mutex);
4608 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4609 VENDOR_CHIPIO_8051_ADDRESS_LOW, addr & 0xFF);
4610 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4611 VENDOR_CHIPIO_8051_ADDRESS_HIGH,
4612 (addr >> 8) & 0xFF);
4613 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4614 VENDOR_CHIPIO_8051_DATA_WRITE, value);
4615 mutex_unlock(&spec->chipio_mutex);
4620 static int idata_write(struct hda_codec *codec, u16 addr, u8 value)
4622 struct ca0132_spec *spec = codec->spec;
4624 mutex_lock(&spec->chipio_mutex);
4625 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4626 VENDOR_CHIPIO_8051_ADDRESS_LOW, addr & 0xFF);
4627 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4628 VENDOR_CHIPIO_8051_ADDRESS_HIGH,
4629 (addr >> 8) & 0xFF);
4630 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4632 mutex_unlock(&spec->chipio_mutex);
4637 static int pmu_read(struct hda_codec *codec, u8 addr)
4639 struct ca0132_spec *spec = codec->spec;
4642 mutex_lock(&spec->chipio_mutex);
4643 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4644 VENDOR_CHIPIO_8051_ADDRESS_LOW, addr);
4645 data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
4646 VENDOR_CHIPIO_PLL_PMU_READ, 0);
4647 mutex_unlock(&spec->chipio_mutex);
4652 static int pmu_write(struct hda_codec *codec, u8 addr, u8 value)
4654 struct ca0132_spec *spec = codec->spec;
4656 mutex_lock(&spec->chipio_mutex);
4657 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4658 VENDOR_CHIPIO_8051_ADDRESS_LOW, addr);
4659 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4660 VENDOR_CHIPIO_PLL_PMU_WRITE, value);
4661 mutex_unlock(&spec->chipio_mutex);
4666 static int ca0132_init_codec(struct hda_codec *codec)
4668 u8 codecDevices[] = {
4669 1, 2, 3, 6, 8, 10, 11
4671 bool codec_ready = true;
4675 int codecStateAddr = 0x18BE;
4681 for (i = 0; i < ARRAY_SIZE(codecDevices); i++) {
4682 addr = codecStateAddr + 13 * codecDevices[i] + 8;
4683 data = xdata_read(codec, addr);
4684 if ((data > 0) && (data & 0x4))
4685 codec_ready = false;
4689 snd_hda_codec_write(codec, 1, 0,
4690 AC_VERB_SET_POWER_STATE, 4);
4692 data = pmu_read(codec, 0xF);
4693 pmu_write(codec, 0xF, data & ~0x88);
4695 pmu_write(codec, 0xF, data | 0x88);
4697 xdata_write(codec, 0xB6, 0x1);
4698 xdata_write(codec, 0x1F07, 0xF9);
4699 xdata_write(codec, 0x1F08, 0x72);
4700 idata_write(codec, 0x7D, 0x40);
4703 data = xdata_read(codec, 0xB6);
4705 } while (data != 0 && --tryCount);
4707 snd_hda_codec_write(codec, 1, 0,
4708 AC_VERB_SET_POWER_STATE, 0);
4711 } while (!codec_ready && --retryCount);
4716 static int ca0132_init(struct hda_codec *codec)
4718 struct ca0132_spec *spec = codec->spec;
4721 spec->dsp_state = DSP_DOWNLOAD_INIT;
4722 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4724 snd_hda_power_up(codec);
4726 snd_hda_sequence_write(codec, spec->base_init_verbs);
4727 ca0132_init_codec(codec);
4728 ca0132_init_params(codec);
4729 ca0132_init_flags(codec);
4730 ca0132_download_dsp(codec);
4731 ca0132_refresh_widget_caps(codec);
4732 ca0132_setup_defaults(codec);
4733 ca0132_pe_switch_set(codec);
4734 ca0132_cvoice_switch_set(codec);
4735 ca0132_init_analog_mic2(codec);
4736 ca0132_init_dmic(codec);
4738 for (i = 0; i < spec->num_outputs; i++)
4739 init_output(codec, spec->out_pins[i], spec->dacs[0]);
4741 for (i = 0; i < spec->num_inputs; i++)
4742 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4744 for (i = 0; i < spec->num_init_verbs; i++)
4745 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4747 ca0132_init_unsol(codec);
4749 ca0132_select_out(codec);
4750 ca0132_select_mic(codec);
4752 snd_hda_jack_report_sync(codec);
4754 snd_hda_power_down(codec);
4759 static void ca0132_free(struct hda_codec *codec)
4761 struct ca0132_spec *spec = codec->spec;
4763 cancel_delayed_work_sync(&spec->unsol_hp_work);
4764 snd_hda_power_up(codec);
4765 snd_hda_sequence_write(codec, spec->base_exit_verbs);
4766 ca0132_exit_chip(codec);
4767 snd_hda_power_down(codec);
4772 static struct hda_codec_ops ca0132_patch_ops = {
4773 .build_controls = ca0132_build_controls,
4774 .build_pcms = ca0132_build_pcms,
4775 .init = ca0132_init,
4776 .free = ca0132_free,
4777 .unsol_event = ca0132_unsol_event,
4780 static void ca0132_config(struct hda_codec *codec)
4782 struct ca0132_spec *spec = codec->spec;
4784 spec->dacs[0] = 0x2;
4785 spec->dacs[1] = 0x3;
4786 spec->dacs[2] = 0x4;
4788 spec->multiout.dac_nids = spec->dacs;
4789 spec->multiout.num_dacs = 3;
4790 spec->multiout.max_channels = 2;
4792 spec->num_outputs = 2;
4793 spec->out_pins[0] = 0x0b; /* speaker out */
4794 spec->out_pins[1] = 0x10; /* headphone out */
4795 spec->shared_out_nid = 0x2;
4797 spec->num_inputs = 3;
4798 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4799 spec->adcs[1] = 0x8; /* analog mic2 */
4800 spec->adcs[2] = 0xa; /* what u hear */
4801 spec->shared_mic_nid = 0x7;
4803 spec->input_pins[0] = 0x12;
4804 spec->input_pins[1] = 0x11;
4805 spec->input_pins[2] = 0x13;
4808 static int patch_ca0132(struct hda_codec *codec)
4810 struct ca0132_spec *spec;
4813 snd_printdd("patch_ca0132\n");
4815 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4819 spec->codec = codec;
4821 spec->num_mixers = 1;
4822 spec->mixers[0] = ca0132_mixer;
4824 spec->base_init_verbs = ca0132_base_init_verbs;
4825 spec->base_exit_verbs = ca0132_base_exit_verbs;
4826 spec->init_verbs[0] = ca0132_init_verbs0;
4827 spec->init_verbs[1] = ca0132_init_verbs1;
4828 spec->num_init_verbs = 2;
4830 INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
4832 ca0132_init_chip(codec);
4834 ca0132_config(codec);
4836 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4840 codec->patch_ops = ca0132_patch_ops;
4849 static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4850 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4854 MODULE_ALIAS("snd-hda-codec-id:11020011");
4856 MODULE_LICENSE("GPL");
4857 MODULE_DESCRIPTION("Creative Sound Core3D codec");
4859 static struct hda_codec_preset_list ca0132_list = {
4860 .preset = snd_hda_preset_ca0132,
4861 .owner = THIS_MODULE,
4864 static int __init patch_ca0132_init(void)
4866 return snd_hda_add_codec_preset(&ca0132_list);
4869 static void __exit patch_ca0132_exit(void)
4871 release_cached_firmware();
4872 snd_hda_delete_codec_preset(&ca0132_list);
4875 module_init(patch_ca0132_init)
4876 module_exit(patch_ca0132_exit)