ALSA: hda/ca0132 - Fix another smatch warning
[linux-2.6-block.git] / sound / pci / hda / patch_ca0132.c
CommitLineData
95c6e9cb
IM
1/*
2 * HD audio interface patch for Creative CA0132 chip
3 *
4 * Copyright (c) 2011, Creative Technology Ltd.
5 *
6 * Based on patch_ca0110.c
7 * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
8 *
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.
13 *
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.
18 *
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
22 */
23
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/pci.h>
28#include <linux/mutex.h>
da155d5b 29#include <linux/module.h>
4aa3bb0c 30#include <linux/firmware.h>
95c6e9cb
IM
31#include <sound/core.h>
32#include "hda_codec.h"
33#include "hda_local.h"
128bc4ba 34#include "hda_auto_parser.h"
5aaca44d 35#include "hda_jack.h"
95c6e9cb 36
bcd109c0
IM
37#include "ca0132_regs.h"
38
ef6b2ead
IM
39/* Enable this to see controls for tuning purpose. */
40/*#define ENABLE_TUNING_CONTROLS*/
41
42#define FLOAT_ZERO 0x00000000
43#define FLOAT_ONE 0x3f800000
44#define FLOAT_TWO 0x40000000
45#define FLOAT_MINUS_5 0xc0a00000
46
47#define UNSOL_TAG_HP 0x10
48#define UNSOL_TAG_AMIC1 0x12
49#define UNSOL_TAG_DSP 0x16
50
4aa3bb0c
IM
51#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
52#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
53
54#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
55#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
56#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
57
58#define MASTERCONTROL 0x80
ef6b2ead
IM
59#define MASTERCONTROL_ALLOC_DMA_CHAN 10
60#define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
4aa3bb0c 61
95c6e9cb
IM
62#define WIDGET_CHIP_CTRL 0x15
63#define WIDGET_DSP_CTRL 0x16
64
4aa3bb0c
IM
65#define MEM_CONNID_MICIN1 3
66#define MEM_CONNID_MICIN2 5
67#define MEM_CONNID_MICOUT1 12
68#define MEM_CONNID_MICOUT2 14
69#define MEM_CONNID_WUH 10
70#define MEM_CONNID_DSP 16
71#define MEM_CONNID_DMIC 100
72
73#define SCP_SET 0
74#define SCP_GET 1
75
01ef7dbf
IM
76#define EFX_FILE "ctefx.bin"
77
78MODULE_FIRMWARE(EFX_FILE);
79
ef6b2ead
IM
80static char *dirstr[2] = { "Playback", "Capture" };
81
82enum {
83 SPEAKER_OUT,
84 HEADPHONE_OUT
85};
86
87enum {
88 DIGITAL_MIC,
89 LINE_MIC_IN
90};
91
92enum {
93#define VNODE_START_NID 0x80
94 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
95 VNID_MIC,
96 VNID_HP_SEL,
97 VNID_AMIC1_SEL,
98 VNID_HP_ASEL,
99 VNID_AMIC1_ASEL,
100 VNODE_END_NID,
101#define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
102
103#define EFFECT_START_NID 0x90
104#define OUT_EFFECT_START_NID EFFECT_START_NID
105 SURROUND = OUT_EFFECT_START_NID,
106 CRYSTALIZER,
107 DIALOG_PLUS,
108 SMART_VOLUME,
109 X_BASS,
110 EQUALIZER,
111 OUT_EFFECT_END_NID,
112#define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
113
114#define IN_EFFECT_START_NID OUT_EFFECT_END_NID
115 ECHO_CANCELLATION = IN_EFFECT_START_NID,
116 VOICE_FOCUS,
117 MIC_SVM,
118 NOISE_REDUCTION,
119 IN_EFFECT_END_NID,
120#define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
121
122 VOICEFX = IN_EFFECT_END_NID,
123 PLAY_ENHANCEMENT,
124 CRYSTAL_VOICE,
125 EFFECT_END_NID
126#define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
127};
128
129/* Effects values size*/
130#define EFFECT_VALS_MAX_COUNT 12
131
132struct ct_effect {
133 char name[44];
134 hda_nid_t nid;
135 int mid; /*effect module ID*/
136 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
137 int direct; /* 0:output; 1:input*/
138 int params; /* number of default non-on/off params */
139 /*effect default values, 1st is on/off. */
140 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
141};
142
143#define EFX_DIR_OUT 0
144#define EFX_DIR_IN 1
145
146static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
147 { .name = "Surround",
148 .nid = SURROUND,
149 .mid = 0x96,
150 .reqs = {0, 1},
151 .direct = EFX_DIR_OUT,
152 .params = 1,
153 .def_vals = {0x3F800000, 0x3F2B851F}
154 },
155 { .name = "Crystalizer",
156 .nid = CRYSTALIZER,
157 .mid = 0x96,
158 .reqs = {7, 8},
159 .direct = EFX_DIR_OUT,
160 .params = 1,
161 .def_vals = {0x3F800000, 0x3F266666}
162 },
163 { .name = "Dialog Plus",
164 .nid = DIALOG_PLUS,
165 .mid = 0x96,
166 .reqs = {2, 3},
167 .direct = EFX_DIR_OUT,
168 .params = 1,
169 .def_vals = {0x00000000, 0x3F000000}
170 },
171 { .name = "Smart Volume",
172 .nid = SMART_VOLUME,
173 .mid = 0x96,
174 .reqs = {4, 5, 6},
175 .direct = EFX_DIR_OUT,
176 .params = 2,
177 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
178 },
179 { .name = "X-Bass",
180 .nid = X_BASS,
181 .mid = 0x96,
182 .reqs = {24, 23, 25},
183 .direct = EFX_DIR_OUT,
184 .params = 2,
185 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
186 },
187 { .name = "Equalizer",
188 .nid = EQUALIZER,
189 .mid = 0x96,
190 .reqs = {9, 10, 11, 12, 13, 14,
191 15, 16, 17, 18, 19, 20},
192 .direct = EFX_DIR_OUT,
193 .params = 11,
194 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
195 0x00000000, 0x00000000, 0x00000000, 0x00000000,
196 0x00000000, 0x00000000, 0x00000000, 0x00000000}
197 },
198 { .name = "Echo Cancellation",
199 .nid = ECHO_CANCELLATION,
200 .mid = 0x95,
201 .reqs = {0, 1, 2, 3},
202 .direct = EFX_DIR_IN,
203 .params = 3,
204 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
205 },
206 { .name = "Voice Focus",
207 .nid = VOICE_FOCUS,
208 .mid = 0x95,
209 .reqs = {6, 7, 8, 9},
210 .direct = EFX_DIR_IN,
211 .params = 3,
212 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
213 },
214 { .name = "Mic SVM",
215 .nid = MIC_SVM,
216 .mid = 0x95,
217 .reqs = {44, 45},
218 .direct = EFX_DIR_IN,
219 .params = 1,
220 .def_vals = {0x00000000, 0x3F3D70A4}
221 },
222 { .name = "Noise Reduction",
223 .nid = NOISE_REDUCTION,
224 .mid = 0x95,
225 .reqs = {4, 5},
226 .direct = EFX_DIR_IN,
227 .params = 1,
228 .def_vals = {0x3F800000, 0x3F000000}
229 },
230 { .name = "VoiceFX",
231 .nid = VOICEFX,
232 .mid = 0x95,
233 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
234 .direct = EFX_DIR_IN,
235 .params = 8,
236 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
237 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
238 0x00000000}
239 }
240};
241
242/* Tuning controls */
243#ifdef ENABLE_TUNING_CONTROLS
244
245enum {
246#define TUNING_CTL_START_NID 0xC0
247 WEDGE_ANGLE = TUNING_CTL_START_NID,
248 SVM_LEVEL,
249 EQUALIZER_BAND_0,
250 EQUALIZER_BAND_1,
251 EQUALIZER_BAND_2,
252 EQUALIZER_BAND_3,
253 EQUALIZER_BAND_4,
254 EQUALIZER_BAND_5,
255 EQUALIZER_BAND_6,
256 EQUALIZER_BAND_7,
257 EQUALIZER_BAND_8,
258 EQUALIZER_BAND_9,
259 TUNING_CTL_END_NID
260#define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
261};
262
263struct ct_tuning_ctl {
264 char name[44];
265 hda_nid_t parent_nid;
266 hda_nid_t nid;
267 int mid; /*effect module ID*/
268 int req; /*effect module request*/
269 int direct; /* 0:output; 1:input*/
270 unsigned int def_val;/*effect default values*/
271};
272
273static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
274 { .name = "Wedge Angle",
275 .parent_nid = VOICE_FOCUS,
276 .nid = WEDGE_ANGLE,
277 .mid = 0x95,
278 .req = 8,
279 .direct = EFX_DIR_IN,
280 .def_val = 0x41F00000
281 },
282 { .name = "SVM Level",
283 .parent_nid = MIC_SVM,
284 .nid = SVM_LEVEL,
285 .mid = 0x95,
286 .req = 45,
287 .direct = EFX_DIR_IN,
288 .def_val = 0x3F3D70A4
289 },
290 { .name = "EQ Band0",
291 .parent_nid = EQUALIZER,
292 .nid = EQUALIZER_BAND_0,
293 .mid = 0x96,
294 .req = 11,
295 .direct = EFX_DIR_OUT,
296 .def_val = 0x00000000
297 },
298 { .name = "EQ Band1",
299 .parent_nid = EQUALIZER,
300 .nid = EQUALIZER_BAND_1,
301 .mid = 0x96,
302 .req = 12,
303 .direct = EFX_DIR_OUT,
304 .def_val = 0x00000000
305 },
306 { .name = "EQ Band2",
307 .parent_nid = EQUALIZER,
308 .nid = EQUALIZER_BAND_2,
309 .mid = 0x96,
310 .req = 13,
311 .direct = EFX_DIR_OUT,
312 .def_val = 0x00000000
313 },
314 { .name = "EQ Band3",
315 .parent_nid = EQUALIZER,
316 .nid = EQUALIZER_BAND_3,
317 .mid = 0x96,
318 .req = 14,
319 .direct = EFX_DIR_OUT,
320 .def_val = 0x00000000
321 },
322 { .name = "EQ Band4",
323 .parent_nid = EQUALIZER,
324 .nid = EQUALIZER_BAND_4,
325 .mid = 0x96,
326 .req = 15,
327 .direct = EFX_DIR_OUT,
328 .def_val = 0x00000000
329 },
330 { .name = "EQ Band5",
331 .parent_nid = EQUALIZER,
332 .nid = EQUALIZER_BAND_5,
333 .mid = 0x96,
334 .req = 16,
335 .direct = EFX_DIR_OUT,
336 .def_val = 0x00000000
337 },
338 { .name = "EQ Band6",
339 .parent_nid = EQUALIZER,
340 .nid = EQUALIZER_BAND_6,
341 .mid = 0x96,
342 .req = 17,
343 .direct = EFX_DIR_OUT,
344 .def_val = 0x00000000
345 },
346 { .name = "EQ Band7",
347 .parent_nid = EQUALIZER,
348 .nid = EQUALIZER_BAND_7,
349 .mid = 0x96,
350 .req = 18,
351 .direct = EFX_DIR_OUT,
352 .def_val = 0x00000000
353 },
354 { .name = "EQ Band8",
355 .parent_nid = EQUALIZER,
356 .nid = EQUALIZER_BAND_8,
357 .mid = 0x96,
358 .req = 19,
359 .direct = EFX_DIR_OUT,
360 .def_val = 0x00000000
361 },
362 { .name = "EQ Band9",
363 .parent_nid = EQUALIZER,
364 .nid = EQUALIZER_BAND_9,
365 .mid = 0x96,
366 .req = 20,
367 .direct = EFX_DIR_OUT,
368 .def_val = 0x00000000
369 }
370};
371#endif
372
373/* Voice FX Presets */
374#define VOICEFX_MAX_PARAM_COUNT 9
375
376struct ct_voicefx {
377 char *name;
378 hda_nid_t nid;
379 int mid;
380 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
381};
382
383struct ct_voicefx_preset {
384 char *name; /*preset name*/
385 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
386};
387
388struct ct_voicefx ca0132_voicefx = {
389 .name = "VoiceFX Capture Switch",
390 .nid = VOICEFX,
391 .mid = 0x95,
392 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
393};
394
395struct ct_voicefx_preset ca0132_voicefx_presets[] = {
396 { .name = "Neutral",
397 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
398 0x44FA0000, 0x3F800000, 0x3F800000,
399 0x3F800000, 0x00000000, 0x00000000 }
400 },
401 { .name = "Female2Male",
402 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
403 0x44FA0000, 0x3F19999A, 0x3F866666,
404 0x3F800000, 0x00000000, 0x00000000 }
405 },
406 { .name = "Male2Female",
407 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
408 0x450AC000, 0x4017AE14, 0x3F6B851F,
409 0x3F800000, 0x00000000, 0x00000000 }
410 },
411 { .name = "ScrappyKid",
412 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
413 0x44FA0000, 0x40400000, 0x3F28F5C3,
414 0x3F800000, 0x00000000, 0x00000000 }
415 },
416 { .name = "Elderly",
417 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
418 0x44E10000, 0x3FB33333, 0x3FB9999A,
419 0x3F800000, 0x3E3A2E43, 0x00000000 }
420 },
421 { .name = "Orc",
422 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
423 0x45098000, 0x3F266666, 0x3FC00000,
424 0x3F800000, 0x00000000, 0x00000000 }
425 },
426 { .name = "Elf",
427 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
428 0x45193000, 0x3F8E147B, 0x3F75C28F,
429 0x3F800000, 0x00000000, 0x00000000 }
430 },
431 { .name = "Dwarf",
432 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
433 0x45007000, 0x3F451EB8, 0x3F7851EC,
434 0x3F800000, 0x00000000, 0x00000000 }
435 },
436 { .name = "AlienBrute",
437 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
438 0x451F6000, 0x3F266666, 0x3FA7D945,
439 0x3F800000, 0x3CF5C28F, 0x00000000 }
440 },
441 { .name = "Robot",
442 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
443 0x44FA0000, 0x3FB2718B, 0x3F800000,
444 0xBC07010E, 0x00000000, 0x00000000 }
445 },
446 { .name = "Marine",
447 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
448 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
449 0x3F0A3D71, 0x00000000, 0x00000000 }
450 },
451 { .name = "Emo",
452 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
453 0x44FA0000, 0x3F800000, 0x3F800000,
454 0x3E4CCCCD, 0x00000000, 0x00000000 }
455 },
456 { .name = "DeepVoice",
457 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
458 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
459 0x3F800000, 0x00000000, 0x00000000 }
460 },
461 { .name = "Munchkin",
462 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
463 0x44FA0000, 0x3F800000, 0x3F1A043C,
464 0x3F800000, 0x00000000, 0x00000000 }
465 }
466};
467
95c6e9cb
IM
468enum hda_cmd_vendor_io {
469 /* for DspIO node */
470 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
471 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
472
473 VENDOR_DSPIO_STATUS = 0xF01,
474 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
475 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
476 VENDOR_DSPIO_DSP_INIT = 0x703,
477 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
478 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
479
480 /* for ChipIO node */
481 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
482 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
483 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
484 VENDOR_CHIPIO_DATA_LOW = 0x300,
485 VENDOR_CHIPIO_DATA_HIGH = 0x400,
486
487 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
488 VENDOR_CHIPIO_STATUS = 0xF01,
489 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
490 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
491
4aa3bb0c
IM
492 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
493 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
494
95c6e9cb 495 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
4aa3bb0c 496 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
95c6e9cb
IM
497
498 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
499 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
500 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
501 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
502 VENDOR_CHIPIO_FLAG_SET = 0x70F,
503 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
4aa3bb0c
IM
504 VENDOR_CHIPIO_PARAM_SET = 0x710,
505 VENDOR_CHIPIO_PARAM_GET = 0xF10,
95c6e9cb
IM
506
507 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
508 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
509 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
510 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
511
4aa3bb0c
IM
512 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
513 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
514 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
515 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
516
517 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
518 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
519 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
520 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
521 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
522 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
523
524 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
95c6e9cb
IM
525};
526
527/*
528 * Control flag IDs
529 */
530enum control_flag_id {
531 /* Connection manager stream setup is bypassed/enabled */
532 CONTROL_FLAG_C_MGR = 0,
533 /* DSP DMA is bypassed/enabled */
534 CONTROL_FLAG_DMA = 1,
535 /* 8051 'idle' mode is disabled/enabled */
536 CONTROL_FLAG_IDLE_ENABLE = 2,
537 /* Tracker for the SPDIF-in path is bypassed/enabled */
538 CONTROL_FLAG_TRACKER = 3,
539 /* DigitalOut to Spdif2Out connection is disabled/enabled */
540 CONTROL_FLAG_SPDIF2OUT = 4,
541 /* Digital Microphone is disabled/enabled */
542 CONTROL_FLAG_DMIC = 5,
543 /* ADC_B rate is 48 kHz/96 kHz */
544 CONTROL_FLAG_ADC_B_96KHZ = 6,
545 /* ADC_C rate is 48 kHz/96 kHz */
546 CONTROL_FLAG_ADC_C_96KHZ = 7,
547 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
548 CONTROL_FLAG_DAC_96KHZ = 8,
549 /* DSP rate is 48 kHz/96 kHz */
550 CONTROL_FLAG_DSP_96KHZ = 9,
551 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
552 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
553 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
554 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
555 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
556 CONTROL_FLAG_DECODE_LOOP = 12,
557 /* De-emphasis filter on DAC-1 disabled/enabled */
558 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
559 /* De-emphasis filter on DAC-2 disabled/enabled */
560 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
561 /* De-emphasis filter on DAC-3 disabled/enabled */
562 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
563 /* High-pass filter on ADC_B disabled/enabled */
564 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
565 /* High-pass filter on ADC_C disabled/enabled */
566 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
567 /* Common mode on Port_A disabled/enabled */
568 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
569 /* Common mode on Port_D disabled/enabled */
570 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
571 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
572 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
573 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
4aa3bb0c 574 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
95c6e9cb
IM
575 /* ASI rate is 48kHz/96kHz */
576 CONTROL_FLAG_ASI_96KHZ = 22,
577 /* DAC power settings able to control attached ports no/yes */
578 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
579 /* Clock Stop OK reporting is disabled/enabled */
580 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
581 /* Number of control flags */
582 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
583};
584
585/*
586 * Control parameter IDs
587 */
4aa3bb0c 588enum control_param_id {
ef6b2ead
IM
589 /* 0: None, 1: Mic1In*/
590 CONTROL_PARAM_VIP_SOURCE = 1,
95c6e9cb
IM
591 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
592 CONTROL_PARAM_SPDIF1_SOURCE = 2,
ef6b2ead
IM
593 /* Port A output stage gain setting to use when 16 Ohm output
594 * impedance is selected*/
595 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
596 /* Port D output stage gain setting to use when 16 Ohm output
597 * impedance is selected*/
598 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
95c6e9cb
IM
599
600 /* Stream Control */
601
602 /* Select stream with the given ID */
603 CONTROL_PARAM_STREAM_ID = 24,
604 /* Source connection point for the selected stream */
605 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
606 /* Destination connection point for the selected stream */
607 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
608 /* Number of audio channels in the selected stream */
609 CONTROL_PARAM_STREAMS_CHANNELS = 27,
610 /*Enable control for the selected stream */
611 CONTROL_PARAM_STREAM_CONTROL = 28,
612
613 /* Connection Point Control */
614
615 /* Select connection point with the given ID */
616 CONTROL_PARAM_CONN_POINT_ID = 29,
617 /* Connection point sample rate */
618 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
619
620 /* Node Control */
621
622 /* Select HDA node with the given ID */
623 CONTROL_PARAM_NODE_ID = 31
624};
625
626/*
627 * Dsp Io Status codes
628 */
629enum hda_vendor_status_dspio {
630 /* Success */
631 VENDOR_STATUS_DSPIO_OK = 0x00,
632 /* Busy, unable to accept new command, the host must retry */
633 VENDOR_STATUS_DSPIO_BUSY = 0x01,
634 /* SCP command queue is full */
635 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
636 /* SCP response queue is empty */
637 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
638};
639
640/*
641 * Chip Io Status codes
642 */
643enum hda_vendor_status_chipio {
644 /* Success */
645 VENDOR_STATUS_CHIPIO_OK = 0x00,
646 /* Busy, unable to accept new command, the host must retry */
647 VENDOR_STATUS_CHIPIO_BUSY = 0x01
648};
649
650/*
651 * CA0132 sample rate
652 */
653enum ca0132_sample_rate {
654 SR_6_000 = 0x00,
655 SR_8_000 = 0x01,
656 SR_9_600 = 0x02,
657 SR_11_025 = 0x03,
658 SR_16_000 = 0x04,
659 SR_22_050 = 0x05,
660 SR_24_000 = 0x06,
661 SR_32_000 = 0x07,
662 SR_44_100 = 0x08,
663 SR_48_000 = 0x09,
664 SR_88_200 = 0x0A,
665 SR_96_000 = 0x0B,
666 SR_144_000 = 0x0C,
667 SR_176_400 = 0x0D,
668 SR_192_000 = 0x0E,
669 SR_384_000 = 0x0F,
670
671 SR_COUNT = 0x10,
672
673 SR_RATE_UNKNOWN = 0x1F
674};
675
01ef7dbf
IM
676enum dsp_download_state {
677 DSP_DOWNLOAD_FAILED = -1,
678 DSP_DOWNLOAD_INIT = 0,
679 DSP_DOWNLOADING = 1,
680 DSP_DOWNLOADED = 2
681};
682
01ef7dbf
IM
683/* retrieve parameters from hda format */
684#define get_hdafmt_chs(fmt) (fmt & 0xf)
685#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
686#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
687#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
688
689/*
690 * CA0132 specific
691 */
692
693struct ca0132_spec {
a7e76271
IM
694 struct snd_kcontrol_new *mixers[5];
695 unsigned int num_mixers;
5aaca44d
IM
696 const struct hda_verb *base_init_verbs;
697 const struct hda_verb *base_exit_verbs;
698 const struct hda_verb *init_verbs[5];
699 unsigned int num_init_verbs; /* exclude base init verbs */
01ef7dbf 700 struct auto_pin_cfg autocfg;
5aaca44d
IM
701
702 /* Nodes configurations */
01ef7dbf
IM
703 struct hda_multi_out multiout;
704 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
705 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
5aaca44d 706 unsigned int num_outputs;
01ef7dbf
IM
707 hda_nid_t input_pins[AUTO_PIN_LAST];
708 hda_nid_t adcs[AUTO_PIN_LAST];
709 hda_nid_t dig_out;
710 hda_nid_t dig_in;
711 unsigned int num_inputs;
a7e76271
IM
712 hda_nid_t shared_mic_nid;
713 hda_nid_t shared_out_nid;
5aaca44d 714 struct hda_pcm pcm_rec[5]; /* PCM information */
01ef7dbf
IM
715
716 /* chip access */
717 struct mutex chipio_mutex; /* chip access mutex */
718 u32 curr_chip_addx;
719
720 /* DSP download related */
721 enum dsp_download_state dsp_state;
722 unsigned int dsp_stream_id;
723 unsigned int wait_scp;
724 unsigned int wait_scp_header;
725 unsigned int wait_num_data;
726 unsigned int scp_resp_header;
727 unsigned int scp_resp_data[4];
728 unsigned int scp_resp_count;
5aaca44d
IM
729
730 /* mixer and effects related */
731 unsigned char dmic_ctl;
732 int cur_out_type;
733 int cur_mic_type;
734 long vnode_lvol[VNODES_COUNT];
735 long vnode_rvol[VNODES_COUNT];
736 long vnode_lswitch[VNODES_COUNT];
737 long vnode_rswitch[VNODES_COUNT];
738 long effects_switch[EFFECTS_COUNT];
739 long voicefx_val;
740 long cur_mic_boost;
44f0c978
IM
741
742#ifdef ENABLE_TUNING_CONTROLS
743 long cur_ctl_vals[TUNING_CTLS_COUNT];
744#endif
01ef7dbf
IM
745};
746
747/*
748 * CA0132 codec access
749 */
750unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
751 unsigned int verb, unsigned int parm, unsigned int *res)
752{
753 unsigned int response;
754 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
755 *res = response;
756
757 return ((response == -1) ? -1 : 0);
758}
759
760static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
761 unsigned short converter_format, unsigned int *res)
762{
763 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
764 converter_format & 0xffff, res);
765}
766
767static int codec_set_converter_stream_channel(struct hda_codec *codec,
768 hda_nid_t nid, unsigned char stream,
769 unsigned char channel, unsigned int *res)
770{
771 unsigned char converter_stream_channel = 0;
772
773 converter_stream_channel = (stream << 4) | (channel & 0x0f);
774 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
775 converter_stream_channel, res);
776}
777
778/* Chip access helper function */
779static int chipio_send(struct hda_codec *codec,
780 unsigned int reg,
781 unsigned int data)
782{
783 unsigned int res;
784 int retry = 50;
785
786 /* send bits of data specified by reg */
787 do {
788 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
789 reg, data);
790 if (res == VENDOR_STATUS_CHIPIO_OK)
791 return 0;
792 } while (--retry);
793 return -EIO;
794}
795
796/*
797 * Write chip address through the vendor widget -- NOT protected by the Mutex!
798 */
799static int chipio_write_address(struct hda_codec *codec,
800 unsigned int chip_addx)
801{
4861af80 802 struct ca0132_spec *spec = codec->spec;
01ef7dbf
IM
803 int res;
804
4861af80
IM
805 if (spec->curr_chip_addx == chip_addx)
806 return 0;
807
01ef7dbf
IM
808 /* send low 16 bits of the address */
809 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
810 chip_addx & 0xffff);
811
812 if (res != -EIO) {
813 /* send high 16 bits of the address */
814 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
815 chip_addx >> 16);
816 }
817
4861af80 818 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
01ef7dbf 819
4861af80 820 return res;
01ef7dbf
IM
821}
822
823/*
824 * Write data through the vendor widget -- NOT protected by the Mutex!
825 */
01ef7dbf
IM
826static int chipio_write_data(struct hda_codec *codec, unsigned int data)
827{
5aaca44d 828 struct ca0132_spec *spec = codec->spec;
01ef7dbf
IM
829 int res;
830
831 /* send low 16 bits of the data */
832 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
833
834 if (res != -EIO) {
835 /* send high 16 bits of the data */
836 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
837 data >> 16);
838 }
839
5aaca44d
IM
840 /*If no error encountered, automatically increment the address
841 as per chip behaviour*/
842 spec->curr_chip_addx = (res != -EIO) ?
843 (spec->curr_chip_addx + 4) : ~0UL;
01ef7dbf
IM
844 return res;
845}
846
d5c21b88
IM
847/*
848 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
849 */
01ef7dbf
IM
850static int chipio_write_data_multiple(struct hda_codec *codec,
851 const u32 *data,
852 unsigned int count)
853{
854 int status = 0;
855
856 if (data == NULL) {
857 snd_printdd(KERN_ERR "chipio_write_data null ptr");
858 return -EINVAL;
859 }
860
861 while ((count-- != 0) && (status == 0))
862 status = chipio_write_data(codec, *data++);
863
864 return status;
865}
866
867
868/*
869 * Read data through the vendor widget -- NOT protected by the Mutex!
870 */
871static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
872{
5aaca44d 873 struct ca0132_spec *spec = codec->spec;
01ef7dbf
IM
874 int res;
875
876 /* post read */
877 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
878
879 if (res != -EIO) {
880 /* read status */
881 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
882 }
883
884 if (res != -EIO) {
885 /* read data */
886 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
887 VENDOR_CHIPIO_HIC_READ_DATA,
888 0);
889 }
890
5aaca44d
IM
891 /*If no error encountered, automatically increment the address
892 as per chip behaviour*/
893 spec->curr_chip_addx = (res != -EIO) ?
894 (spec->curr_chip_addx + 4) : ~0UL;
01ef7dbf
IM
895 return res;
896}
897
898/*
899 * Write given value to the given address through the chip I/O widget.
900 * protected by the Mutex
901 */
902static int chipio_write(struct hda_codec *codec,
903 unsigned int chip_addx, const unsigned int data)
904{
905 struct ca0132_spec *spec = codec->spec;
906 int err;
907
908 mutex_lock(&spec->chipio_mutex);
909
910 /* write the address, and if successful proceed to write data */
911 err = chipio_write_address(codec, chip_addx);
912 if (err < 0)
913 goto exit;
914
915 err = chipio_write_data(codec, data);
916 if (err < 0)
917 goto exit;
918
919exit:
920 mutex_unlock(&spec->chipio_mutex);
921 return err;
922}
923
d5c21b88
IM
924/*
925 * Write multiple values to the given address through the chip I/O widget.
926 * protected by the Mutex
927 */
01ef7dbf
IM
928static int chipio_write_multiple(struct hda_codec *codec,
929 u32 chip_addx,
930 const u32 *data,
931 unsigned int count)
932{
933 struct ca0132_spec *spec = codec->spec;
934 int status;
935
936 mutex_lock(&spec->chipio_mutex);
4861af80 937 status = chipio_write_address(codec, chip_addx);
01ef7dbf
IM
938 if (status < 0)
939 goto error;
940
941 status = chipio_write_data_multiple(codec, data, count);
942error:
943 mutex_unlock(&spec->chipio_mutex);
944
945 return status;
946}
947
948/*
949 * Read the given address through the chip I/O widget
950 * protected by the Mutex
951 */
952static int chipio_read(struct hda_codec *codec,
953 unsigned int chip_addx, unsigned int *data)
954{
955 struct ca0132_spec *spec = codec->spec;
956 int err;
957
958 mutex_lock(&spec->chipio_mutex);
959
960 /* write the address, and if successful proceed to write data */
961 err = chipio_write_address(codec, chip_addx);
962 if (err < 0)
963 goto exit;
964
965 err = chipio_read_data(codec, data);
966 if (err < 0)
967 goto exit;
968
969exit:
970 mutex_unlock(&spec->chipio_mutex);
971 return err;
972}
973
d5c21b88
IM
974/*
975 * Set chip control flags through the chip I/O widget.
976 */
01ef7dbf
IM
977static void chipio_set_control_flag(struct hda_codec *codec,
978 enum control_flag_id flag_id,
979 bool flag_state)
980{
981 unsigned int val;
982 unsigned int flag_bit;
983
984 flag_bit = (flag_state ? 1 : 0);
985 val = (flag_bit << 7) | (flag_id);
986 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
987 VENDOR_CHIPIO_FLAG_SET, val);
988}
989
d5c21b88
IM
990/*
991 * Set chip parameters through the chip I/O widget.
992 */
01ef7dbf
IM
993static void chipio_set_control_param(struct hda_codec *codec,
994 enum control_param_id param_id, int param_val)
995{
996 struct ca0132_spec *spec = codec->spec;
997 int val;
998
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);
1003 } else {
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,
1008 param_id);
1009 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1010 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1011 param_val);
1012 }
1013 mutex_unlock(&spec->chipio_mutex);
1014 }
1015}
1016
d5c21b88
IM
1017/*
1018 * Set sampling rate of the connection point.
1019 */
01ef7dbf
IM
1020static void chipio_set_conn_rate(struct hda_codec *codec,
1021 int connid, enum ca0132_sample_rate rate)
1022{
1023 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1024 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1025 rate);
1026}
1027
d5c21b88
IM
1028/*
1029 * Enable clocks.
1030 */
01ef7dbf
IM
1031static void chipio_enable_clocks(struct hda_codec *codec)
1032{
1033 struct ca0132_spec *spec = codec->spec;
1034
1035 mutex_lock(&spec->chipio_mutex);
1036 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1037 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1038 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1039 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1040 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1041 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1042 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1043 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1044 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1045 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1046 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1047 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1048 mutex_unlock(&spec->chipio_mutex);
1049}
1050
1051/*
1052 * CA0132 DSP IO stuffs
1053 */
1054static int dspio_send(struct hda_codec *codec, unsigned int reg,
1055 unsigned int data)
1056{
b645d796 1057 int res;
01ef7dbf
IM
1058 int retry = 50;
1059
1060 /* send bits of data specified by reg to dsp */
1061 do {
1062 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1063 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1064 return res;
1065 } while (--retry);
1066
1067 return -EIO;
1068}
1069
d5c21b88
IM
1070/*
1071 * Wait for DSP to be ready for commands
1072 */
01ef7dbf
IM
1073static void dspio_write_wait(struct hda_codec *codec)
1074{
4861af80
IM
1075 int status;
1076 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
01ef7dbf 1077
01ef7dbf 1078 do {
4861af80
IM
1079 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1080 VENDOR_DSPIO_STATUS, 0);
1081 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1082 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1083 break;
1084 msleep(1);
1085 } while (time_before(jiffies, timeout));
01ef7dbf
IM
1086}
1087
d5c21b88
IM
1088/*
1089 * Write SCP data to DSP
1090 */
01ef7dbf
IM
1091static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1092{
1093 struct ca0132_spec *spec = codec->spec;
1094 int status;
1095
1096 dspio_write_wait(codec);
1097
1098 mutex_lock(&spec->chipio_mutex);
1099 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1100 scp_data & 0xffff);
1101 if (status < 0)
1102 goto error;
1103
1104 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1105 scp_data >> 16);
1106 if (status < 0)
1107 goto error;
1108
1109 /* OK, now check if the write itself has executed*/
1110 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1111 VENDOR_DSPIO_STATUS, 0);
1112error:
1113 mutex_unlock(&spec->chipio_mutex);
1114
1115 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1116 -EIO : 0;
1117}
1118
d5c21b88
IM
1119/*
1120 * Write multiple SCP data to DSP
1121 */
01ef7dbf
IM
1122static int dspio_write_multiple(struct hda_codec *codec,
1123 unsigned int *buffer, unsigned int size)
1124{
1125 int status = 0;
1126 unsigned int count;
1127
1128 if ((buffer == NULL))
1129 return -EINVAL;
1130
1131 count = 0;
1132 while (count < size) {
1133 status = dspio_write(codec, *buffer++);
1134 if (status != 0)
1135 break;
1136 count++;
1137 }
1138
1139 return status;
1140}
1141
a73d511c
IM
1142static int dspio_read(struct hda_codec *codec, unsigned int *data)
1143{
1144 int status;
1145
1146 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1147 if (status == -EIO)
1148 return status;
1149
1150 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1151 if (status == -EIO ||
1152 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1153 return -EIO;
1154
1155 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1156 VENDOR_DSPIO_SCP_READ_DATA, 0);
1157
1158 return 0;
1159}
1160
1161static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1162 unsigned int *buf_size, unsigned int size_count)
1163{
1164 int status = 0;
1165 unsigned int size = *buf_size;
1166 unsigned int count;
1167 unsigned int skip_count;
1168 unsigned int dummy;
1169
1170 if ((buffer == NULL))
1171 return -1;
1172
1173 count = 0;
1174 while (count < size && count < size_count) {
1175 status = dspio_read(codec, buffer++);
1176 if (status != 0)
1177 break;
1178 count++;
1179 }
1180
1181 skip_count = count;
1182 if (status == 0) {
1183 while (skip_count < size) {
1184 status = dspio_read(codec, &dummy);
1185 if (status != 0)
1186 break;
1187 skip_count++;
1188 }
1189 }
1190 *buf_size = count;
1191
1192 return status;
1193}
1194
d5c21b88
IM
1195/*
1196 * Construct the SCP header using corresponding fields
1197 */
01ef7dbf
IM
1198static inline unsigned int
1199make_scp_header(unsigned int target_id, unsigned int source_id,
1200 unsigned int get_flag, unsigned int req,
1201 unsigned int device_flag, unsigned int resp_flag,
1202 unsigned int error_flag, unsigned int data_size)
1203{
1204 unsigned int header = 0;
1205
1206 header = (data_size & 0x1f) << 27;
1207 header |= (error_flag & 0x01) << 26;
1208 header |= (resp_flag & 0x01) << 25;
1209 header |= (device_flag & 0x01) << 24;
1210 header |= (req & 0x7f) << 17;
1211 header |= (get_flag & 0x01) << 16;
1212 header |= (source_id & 0xff) << 8;
1213 header |= target_id & 0xff;
1214
1215 return header;
1216}
1217
d5c21b88
IM
1218/*
1219 * Extract corresponding fields from SCP header
1220 */
01ef7dbf
IM
1221static inline void
1222extract_scp_header(unsigned int header,
1223 unsigned int *target_id, unsigned int *source_id,
1224 unsigned int *get_flag, unsigned int *req,
1225 unsigned int *device_flag, unsigned int *resp_flag,
1226 unsigned int *error_flag, unsigned int *data_size)
1227{
1228 if (data_size)
1229 *data_size = (header >> 27) & 0x1f;
1230 if (error_flag)
1231 *error_flag = (header >> 26) & 0x01;
1232 if (resp_flag)
1233 *resp_flag = (header >> 25) & 0x01;
1234 if (device_flag)
1235 *device_flag = (header >> 24) & 0x01;
1236 if (req)
1237 *req = (header >> 17) & 0x7f;
1238 if (get_flag)
1239 *get_flag = (header >> 16) & 0x01;
1240 if (source_id)
1241 *source_id = (header >> 8) & 0xff;
1242 if (target_id)
1243 *target_id = header & 0xff;
1244}
1245
1246#define SCP_MAX_DATA_WORDS (16)
1247
1248/* Structure to contain any SCP message */
1249struct scp_msg {
1250 unsigned int hdr;
1251 unsigned int data[SCP_MAX_DATA_WORDS];
1252};
1253
a73d511c
IM
1254static void dspio_clear_response_queue(struct hda_codec *codec)
1255{
1256 unsigned int dummy = 0;
1257 int status = -1;
1258
1259 /* clear all from the response queue */
1260 do {
1261 status = dspio_read(codec, &dummy);
1262 } while (status == 0);
1263}
1264
1265static int dspio_get_response_data(struct hda_codec *codec)
1266{
1267 struct ca0132_spec *spec = codec->spec;
1268 unsigned int data = 0;
1269 unsigned int count;
1270
1271 if (dspio_read(codec, &data) < 0)
1272 return -EIO;
1273
1274 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1275 spec->scp_resp_header = data;
1276 spec->scp_resp_count = data >> 27;
1277 count = spec->wait_num_data;
1278 dspio_read_multiple(codec, spec->scp_resp_data,
1279 &spec->scp_resp_count, count);
1280 return 0;
1281 }
1282
1283 return -EIO;
1284}
1285
d5c21b88
IM
1286/*
1287 * Send SCP message to DSP
1288 */
01ef7dbf
IM
1289static int dspio_send_scp_message(struct hda_codec *codec,
1290 unsigned char *send_buf,
1291 unsigned int send_buf_size,
1292 unsigned char *return_buf,
1293 unsigned int return_buf_size,
1294 unsigned int *bytes_returned)
1295{
1296 struct ca0132_spec *spec = codec->spec;
1297 int retry;
1298 int status = -1;
1299 unsigned int scp_send_size = 0;
1300 unsigned int total_size;
1301 bool waiting_for_resp = false;
1302 unsigned int header;
1303 struct scp_msg *ret_msg;
1304 unsigned int resp_src_id, resp_target_id;
1305 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1306
1307 if (bytes_returned)
1308 *bytes_returned = 0;
1309
1310 /* get scp header from buffer */
1311 header = *((unsigned int *)send_buf);
1312 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1313 &device_flag, NULL, NULL, &data_size);
1314 scp_send_size = data_size + 1;
1315 total_size = (scp_send_size * 4);
1316
1317 if (send_buf_size < total_size)
1318 return -EINVAL;
1319
1320 if (get_flag || device_flag) {
1321 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1322 return -EINVAL;
1323
1324 spec->wait_scp_header = *((unsigned int *)send_buf);
1325
1326 /* swap source id with target id */
1327 resp_target_id = src_id;
1328 resp_src_id = target_id;
1329 spec->wait_scp_header &= 0xffff0000;
1330 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1331 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1332 spec->wait_scp = 1;
1333 waiting_for_resp = true;
1334 }
1335
1336 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1337 scp_send_size);
1338 if (status < 0) {
1339 spec->wait_scp = 0;
1340 return status;
1341 }
1342
1343 if (waiting_for_resp) {
1344 memset(return_buf, 0, return_buf_size);
1345 retry = 50;
1346 do {
1347 msleep(20);
1348 } while (spec->wait_scp && (--retry != 0));
1349 waiting_for_resp = false;
1350 if (retry != 0) {
1351 ret_msg = (struct scp_msg *)return_buf;
1352 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1353 memcpy(&ret_msg->data, spec->scp_resp_data,
1354 spec->wait_num_data);
1355 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1356 status = 0;
1357 } else {
1358 status = -EIO;
1359 }
1360 spec->wait_scp = 0;
1361 }
1362
1363 return status;
1364}
1365
d5c21b88
IM
1366/**
1367 * Prepare and send the SCP message to DSP
1368 * @codec: the HDA codec
1369 * @mod_id: ID of the DSP module to send the command
1370 * @req: ID of request to send to the DSP module
1371 * @dir: SET or GET
1372 * @data: pointer to the data to send with the request, request specific
1373 * @len: length of the data, in bytes
1374 * @reply: point to the buffer to hold data returned for a reply
1375 * @reply_len: length of the reply buffer returned from GET
1376 *
1377 * Returns zero or a negative error code.
1378 */
01ef7dbf
IM
1379static int dspio_scp(struct hda_codec *codec,
1380 int mod_id, int req, int dir, void *data, unsigned int len,
1381 void *reply, unsigned int *reply_len)
1382{
1383 int status = 0;
1384 struct scp_msg scp_send, scp_reply;
1385 unsigned int ret_bytes, send_size, ret_size;
1386 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1387 unsigned int reply_data_size;
1388
1389 memset(&scp_send, 0, sizeof(scp_send));
1390 memset(&scp_reply, 0, sizeof(scp_reply));
1391
1392 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1393 return -EINVAL;
1394
1395 if (dir == SCP_GET && reply == NULL) {
1396 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
1397 return -EINVAL;
1398 }
1399
1400 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1401 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
1402 return -EINVAL;
1403 }
1404
1405 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1406 0, 0, 0, len/sizeof(unsigned int));
1407 if (data != NULL && len > 0) {
1408 len = min((unsigned int)(sizeof(scp_send.data)), len);
1409 memcpy(scp_send.data, data, len);
1410 }
1411
1412 ret_bytes = 0;
1413 send_size = sizeof(unsigned int) + len;
1414 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1415 send_size, (unsigned char *)&scp_reply,
1416 sizeof(scp_reply), &ret_bytes);
1417
1418 if (status < 0) {
1419 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1420 return status;
1421 }
1422
1423 /* extract send and reply headers members */
1424 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1425 NULL, NULL, NULL, NULL, NULL);
1426 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1427 &reply_resp_flag, &reply_error_flag,
1428 &reply_data_size);
1429
1430 if (!send_get_flag)
1431 return 0;
1432
1433 if (reply_resp_flag && !reply_error_flag) {
1434 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1435 / sizeof(unsigned int);
1436
1437 if (*reply_len < ret_size*sizeof(unsigned int)) {
1438 snd_printdd(KERN_ERR "reply too long for buf");
1439 return -EINVAL;
1440 } else if (ret_size != reply_data_size) {
1441 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1442 return -EINVAL;
1443 } else {
1444 *reply_len = ret_size*sizeof(unsigned int);
1445 memcpy(reply, scp_reply.data, *reply_len);
1446 }
1447 } else {
1448 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1449 return -EIO;
1450 }
1451
1452 return status;
1453}
1454
5aaca44d
IM
1455/*
1456 * Set DSP parameters
1457 */
1458static int dspio_set_param(struct hda_codec *codec, int mod_id,
1459 int req, void *data, unsigned int len)
1460{
1461 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1462}
1463
1464static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
1465 int req, unsigned int data)
1466{
1467 return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
1468}
1469
d5c21b88
IM
1470/*
1471 * Allocate a DSP DMA channel via an SCP message
1472 */
01ef7dbf
IM
1473static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1474{
1475 int status = 0;
1476 unsigned int size = sizeof(dma_chan);
1477
1478 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1479 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1480 SCP_GET, NULL, 0, dma_chan, &size);
1481
1482 if (status < 0) {
1483 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1484 return status;
1485 }
1486
1487 if ((*dma_chan + 1) == 0) {
1488 snd_printdd(KERN_INFO "no free dma channels to allocate");
1489 return -EBUSY;
1490 }
1491
1492 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1493 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1494
1495 return status;
1496}
1497
d5c21b88
IM
1498/*
1499 * Free a DSP DMA via an SCP message
1500 */
01ef7dbf
IM
1501static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1502{
1503 int status = 0;
1504 unsigned int dummy = 0;
1505
1506 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1507 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1508
1509 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1510 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1511
1512 if (status < 0) {
1513 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1514 return status;
1515 }
1516
1517 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1518
1519 return status;
1520}
1521
1522/*
d5c21b88 1523 * (Re)start the DSP
01ef7dbf
IM
1524 */
1525static int dsp_set_run_state(struct hda_codec *codec)
1526{
1527 unsigned int dbg_ctrl_reg;
1528 unsigned int halt_state;
1529 int err;
1530
1531 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1532 if (err < 0)
1533 return err;
1534
1535 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1536 DSP_DBGCNTL_STATE_LOBIT;
1537
1538 if (halt_state != 0) {
1539 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1540 DSP_DBGCNTL_SS_MASK);
1541 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1542 dbg_ctrl_reg);
1543 if (err < 0)
1544 return err;
1545
1546 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1547 DSP_DBGCNTL_EXEC_MASK;
1548 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1549 dbg_ctrl_reg);
1550 if (err < 0)
1551 return err;
1552 }
1553
1554 return 0;
1555}
1556
d5c21b88
IM
1557/*
1558 * Reset the DSP
1559 */
01ef7dbf
IM
1560static int dsp_reset(struct hda_codec *codec)
1561{
1562 unsigned int res;
1563 int retry = 20;
1564
1565 snd_printdd("dsp_reset\n");
1566 do {
1567 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1568 retry--;
1569 } while (res == -EIO && retry);
1570
1571 if (!retry) {
1572 snd_printdd("dsp_reset timeout\n");
1573 return -EIO;
1574 }
1575
1576 return 0;
1577}
1578
d5c21b88
IM
1579/*
1580 * Convert chip address to DSP address
1581 */
01ef7dbf
IM
1582static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1583 bool *code, bool *yram)
1584{
1585 *code = *yram = false;
1586
1587 if (UC_RANGE(chip_addx, 1)) {
1588 *code = true;
1589 return UC_OFF(chip_addx);
1590 } else if (X_RANGE_ALL(chip_addx, 1)) {
1591 return X_OFF(chip_addx);
1592 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1593 *yram = true;
1594 return Y_OFF(chip_addx);
1595 }
1596
1597 return (unsigned int)INVALID_CHIP_ADDRESS;
1598}
1599
d5c21b88
IM
1600/*
1601 * Check if the DSP DMA is active
1602 */
01ef7dbf
IM
1603static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1604{
1605 unsigned int dma_chnlstart_reg;
1606
1607 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1608
1609 return ((dma_chnlstart_reg & (1 <<
1610 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1611}
1612
1613static int dsp_dma_setup_common(struct hda_codec *codec,
1614 unsigned int chip_addx,
1615 unsigned int dma_chan,
1616 unsigned int port_map_mask,
1617 bool ovly)
1618{
1619 int status = 0;
1620 unsigned int chnl_prop;
1621 unsigned int dsp_addx;
1622 unsigned int active;
1623 bool code, yram;
1624
1625 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1626
1627 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1628 snd_printdd(KERN_ERR "dma chan num invalid");
1629 return -EINVAL;
1630 }
1631
1632 if (dsp_is_dma_active(codec, dma_chan)) {
1633 snd_printdd(KERN_ERR "dma already active");
1634 return -EBUSY;
1635 }
1636
1637 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1638
1639 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1640 snd_printdd(KERN_ERR "invalid chip addr");
1641 return -ENXIO;
1642 }
1643
1644 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1645 active = 0;
1646
1647 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1648
1649 if (ovly) {
1650 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1651 &chnl_prop);
1652
1653 if (status < 0) {
1654 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1655 return status;
1656 }
1657 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1658 }
1659
1660 if (!code)
1661 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1662 else
1663 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1664
1665 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1666
1667 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1668 if (status < 0) {
1669 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1670 return status;
1671 }
1672 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1673
1674 if (ovly) {
1675 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1676 &active);
1677
1678 if (status < 0) {
1679 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1680 return status;
1681 }
1682 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1683 }
1684
1685 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1686 DSPDMAC_ACTIVE_AAR_MASK;
1687
1688 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1689 if (status < 0) {
1690 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1691 return status;
1692 }
1693
1694 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1695
1696 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1697 port_map_mask);
1698 if (status < 0) {
1699 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1700 return status;
1701 }
1702 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1703
1704 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1705 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1706 if (status < 0) {
1707 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1708 return status;
1709 }
1710 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1711
1712 snd_printdd(
1713 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1714 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1715 chip_addx, dsp_addx, dma_chan,
1716 port_map_mask, chnl_prop, active);
1717
1718 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1719
1720 return 0;
1721}
1722
d5c21b88
IM
1723/*
1724 * Setup the DSP DMA per-transfer-specific registers
1725 */
01ef7dbf
IM
1726static int dsp_dma_setup(struct hda_codec *codec,
1727 unsigned int chip_addx,
1728 unsigned int count,
1729 unsigned int dma_chan)
1730{
1731 int status = 0;
1732 bool code, yram;
1733 unsigned int dsp_addx;
1734 unsigned int addr_field;
1735 unsigned int incr_field;
1736 unsigned int base_cnt;
1737 unsigned int cur_cnt;
1738 unsigned int dma_cfg = 0;
1739 unsigned int adr_ofs = 0;
1740 unsigned int xfr_cnt = 0;
1741 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1742 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1743
1744 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1745
1746 if (count > max_dma_count) {
1747 snd_printdd(KERN_ERR "count too big");
1748 return -EINVAL;
1749 }
1750
1751 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1752 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1753 snd_printdd(KERN_ERR "invalid chip addr");
1754 return -ENXIO;
1755 }
1756
1757 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1758
1759 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1760 incr_field = 0;
1761
1762 if (!code) {
1763 addr_field <<= 1;
1764 if (yram)
1765 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1766
1767 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1768 }
1769
1770 dma_cfg = addr_field + incr_field;
1771 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1772 dma_cfg);
1773 if (status < 0) {
1774 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1775 return status;
1776 }
1777 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1778
1779 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1780 (code ? 0 : 1));
1781
1782 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1783 adr_ofs);
1784 if (status < 0) {
1785 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1786 return status;
1787 }
1788 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1789
1790 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1791
1792 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1793
1794 xfr_cnt = base_cnt | cur_cnt;
1795
1796 status = chipio_write(codec,
1797 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1798 if (status < 0) {
1799 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1800 return status;
1801 }
1802 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1803
1804 snd_printdd(
1805 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1806 "ADROFS=0x%x, XFRCNT=0x%x\n",
1807 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1808
1809 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1810
1811 return 0;
1812}
1813
d5c21b88
IM
1814/*
1815 * Start the DSP DMA
1816 */
01ef7dbf
IM
1817static int dsp_dma_start(struct hda_codec *codec,
1818 unsigned int dma_chan, bool ovly)
1819{
1820 unsigned int reg = 0;
1821 int status = 0;
1822
1823 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1824
1825 if (ovly) {
1826 status = chipio_read(codec,
1827 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1828
1829 if (status < 0) {
1830 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1831 return status;
1832 }
1833 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1834
1835 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1836 DSPDMAC_CHNLSTART_DIS_MASK);
1837 }
1838
1839 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1840 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1841 if (status < 0) {
1842 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1843 return status;
1844 }
1845 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1846
1847 return status;
1848}
1849
d5c21b88
IM
1850/*
1851 * Stop the DSP DMA
1852 */
01ef7dbf
IM
1853static int dsp_dma_stop(struct hda_codec *codec,
1854 unsigned int dma_chan, bool ovly)
1855{
1856 unsigned int reg = 0;
1857 int status = 0;
1858
1859 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1860
1861 if (ovly) {
1862 status = chipio_read(codec,
1863 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1864
1865 if (status < 0) {
1866 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1867 return status;
1868 }
1869 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1870 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1871 DSPDMAC_CHNLSTART_DIS_MASK);
1872 }
1873
1874 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1875 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1876 if (status < 0) {
1877 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1878 return status;
1879 }
1880 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1881
1882 return status;
1883}
1884
d5c21b88
IM
1885/**
1886 * Allocate router ports
1887 *
1888 * @codec: the HDA codec
1889 * @num_chans: number of channels in the stream
1890 * @ports_per_channel: number of ports per channel
1891 * @start_device: start device
1892 * @port_map: pointer to the port list to hold the allocated ports
1893 *
1894 * Returns zero or a negative error code.
1895 */
01ef7dbf
IM
1896static int dsp_allocate_router_ports(struct hda_codec *codec,
1897 unsigned int num_chans,
1898 unsigned int ports_per_channel,
1899 unsigned int start_device,
1900 unsigned int *port_map)
1901{
1902 int status = 0;
1903 int res;
1904 u8 val;
1905
1906 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1907 if (status < 0)
1908 return status;
1909
1910 val = start_device << 6;
1911 val |= (ports_per_channel - 1) << 4;
1912 val |= num_chans - 1;
1913
1914 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1915 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1916 val);
1917
1918 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1919 VENDOR_CHIPIO_PORT_ALLOC_SET,
1920 MEM_CONNID_DSP);
1921
1922 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1923 if (status < 0)
1924 return status;
1925
1926 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1927 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1928
1929 *port_map = res;
1930
1931 return (res < 0) ? res : 0;
1932}
1933
d5c21b88
IM
1934/*
1935 * Free router ports
1936 */
01ef7dbf
IM
1937static int dsp_free_router_ports(struct hda_codec *codec)
1938{
1939 int status = 0;
1940
1941 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1942 if (status < 0)
1943 return status;
1944
1945 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1946 VENDOR_CHIPIO_PORT_FREE_SET,
1947 MEM_CONNID_DSP);
1948
1949 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1950
1951 return status;
1952}
1953
d5c21b88
IM
1954/*
1955 * Allocate DSP ports for the download stream
1956 */
01ef7dbf
IM
1957static int dsp_allocate_ports(struct hda_codec *codec,
1958 unsigned int num_chans,
1959 unsigned int rate_multi, unsigned int *port_map)
1960{
1961 int status;
1962
1963 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1964
1965 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1966 snd_printdd(KERN_ERR "bad rate multiple");
1967 return -EINVAL;
1968 }
1969
1970 status = dsp_allocate_router_ports(codec, num_chans,
1971 rate_multi, 0, port_map);
1972
1973 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1974
1975 return status;
1976}
1977
01ef7dbf
IM
1978static int dsp_allocate_ports_format(struct hda_codec *codec,
1979 const unsigned short fmt,
1980 unsigned int *port_map)
1981{
1982 int status;
1983 unsigned int num_chans;
1984
1985 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1986 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1987 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1988
1989 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1990 snd_printdd(KERN_ERR "bad rate multiple");
1991 return -EINVAL;
1992 }
1993
1994 num_chans = get_hdafmt_chs(fmt) + 1;
1995
1996 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1997
1998 return status;
1999}
2000
d5c21b88
IM
2001/*
2002 * free DSP ports
2003 */
2004static int dsp_free_ports(struct hda_codec *codec)
2005{
2006 int status;
2007
2008 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
2009
2010 status = dsp_free_router_ports(codec);
2011 if (status < 0) {
2012 snd_printdd(KERN_ERR "free router ports fail");
2013 return status;
2014 }
2015 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
2016
2017 return status;
2018}
2019
01ef7dbf
IM
2020/*
2021 * HDA DMA engine stuffs for DSP code download
2022 */
2023struct dma_engine {
2024 struct hda_codec *codec;
2025 unsigned short m_converter_format;
2026 struct snd_dma_buffer *dmab;
2027 unsigned int buf_size;
2028};
2029
2030
2031enum dma_state {
2032 DMA_STATE_STOP = 0,
2033 DMA_STATE_RUN = 1
2034};
2035
2036static int dma_convert_to_hda_format(
e97249dd
IM
2037 unsigned int sample_rate,
2038 unsigned short channels,
01ef7dbf
IM
2039 unsigned short *hda_format)
2040{
2041 unsigned int format_val;
2042
2043 format_val = snd_hda_calc_stream_format(
e97249dd
IM
2044 sample_rate,
2045 channels,
01ef7dbf 2046 SNDRV_PCM_FORMAT_S32_LE,
e97249dd 2047 32, 0);
01ef7dbf
IM
2048
2049 if (hda_format)
2050 *hda_format = (unsigned short)format_val;
2051
2052 return 0;
2053}
2054
d5c21b88
IM
2055/*
2056 * Reset DMA for DSP download
2057 */
01ef7dbf
IM
2058static int dma_reset(struct dma_engine *dma)
2059{
2060 struct hda_codec *codec = dma->codec;
2061 struct ca0132_spec *spec = codec->spec;
2062 int status;
2063
2064 if (dma->dmab)
2065 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2066
2067 status = snd_hda_codec_load_dsp_prepare(codec,
2068 dma->m_converter_format,
2069 dma->buf_size,
2070 dma->dmab);
2071 if (status < 0)
2072 return status;
2073 spec->dsp_stream_id = status;
2074 return 0;
2075}
2076
2077static int dma_set_state(struct dma_engine *dma, enum dma_state state)
95c6e9cb 2078{
01ef7dbf
IM
2079 bool cmd;
2080
2081 snd_printdd("dma_set_state state=%d\n", state);
2082
2083 switch (state) {
2084 case DMA_STATE_STOP:
2085 cmd = false;
2086 break;
2087 case DMA_STATE_RUN:
2088 cmd = true;
2089 break;
2090 default:
c41999a2
DH
2091 return 0;
2092 }
01ef7dbf
IM
2093
2094 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2095 return 0;
95c6e9cb
IM
2096}
2097
01ef7dbf
IM
2098static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2099{
2100 return dma->dmab->bytes;
2101}
95c6e9cb 2102
01ef7dbf
IM
2103static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2104{
2105 return dma->dmab->area;
2106}
95c6e9cb 2107
01ef7dbf
IM
2108static int dma_xfer(struct dma_engine *dma,
2109 const unsigned int *data,
2110 unsigned int count)
2111{
2112 memcpy(dma->dmab->area, data, count);
2113 return 0;
2114}
95c6e9cb 2115
01ef7dbf
IM
2116static void dma_get_converter_format(
2117 struct dma_engine *dma,
2118 unsigned short *format)
2119{
2120 if (format)
2121 *format = dma->m_converter_format;
2122}
95c6e9cb 2123
01ef7dbf 2124static unsigned int dma_get_stream_id(struct dma_engine *dma)
95c6e9cb 2125{
01ef7dbf 2126 struct ca0132_spec *spec = dma->codec->spec;
95c6e9cb 2127
01ef7dbf 2128 return spec->dsp_stream_id;
95c6e9cb
IM
2129}
2130
01ef7dbf
IM
2131struct dsp_image_seg {
2132 u32 magic;
2133 u32 chip_addr;
2134 u32 count;
2135 u32 data[0];
2136};
2137
2138static const u32 g_magic_value = 0x4c46584d;
2139static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2140
2141static bool is_valid(const struct dsp_image_seg *p)
95c6e9cb 2142{
01ef7dbf
IM
2143 return p->magic == g_magic_value;
2144}
95c6e9cb 2145
01ef7dbf
IM
2146static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2147{
2148 return g_chip_addr_magic_value == p->chip_addr;
2149}
95c6e9cb 2150
01ef7dbf
IM
2151static bool is_last(const struct dsp_image_seg *p)
2152{
2153 return p->count == 0;
2154}
95c6e9cb 2155
01ef7dbf
IM
2156static size_t dsp_sizeof(const struct dsp_image_seg *p)
2157{
2158 return sizeof(*p) + p->count*sizeof(u32);
2159}
2160
2161static const struct dsp_image_seg *get_next_seg_ptr(
2162 const struct dsp_image_seg *p)
2163{
2164 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
95c6e9cb
IM
2165}
2166
2167/*
01ef7dbf 2168 * CA0132 chip DSP transfer stuffs. For DSP download.
95c6e9cb 2169 */
01ef7dbf 2170#define INVALID_DMA_CHANNEL (~0UL)
95c6e9cb 2171
d5c21b88
IM
2172/*
2173 * Program a list of address/data pairs via the ChipIO widget.
2174 * The segment data is in the format of successive pairs of words.
2175 * These are repeated as indicated by the segment's count field.
2176 */
01ef7dbf
IM
2177static int dspxfr_hci_write(struct hda_codec *codec,
2178 const struct dsp_image_seg *fls)
95c6e9cb 2179{
01ef7dbf
IM
2180 int status;
2181 const u32 *data;
2182 unsigned int count;
95c6e9cb 2183
01ef7dbf
IM
2184 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2185 snd_printdd(KERN_ERR "hci_write invalid params");
2186 return -EINVAL;
95c6e9cb
IM
2187 }
2188
01ef7dbf
IM
2189 count = fls->count;
2190 data = (u32 *)(fls->data);
2191 while (count >= 2) {
2192 status = chipio_write(codec, data[0], data[1]);
2193 if (status < 0) {
2194 snd_printdd(KERN_ERR "hci_write chipio failed");
2195 return status;
2196 }
2197 count -= 2;
2198 data += 2;
2199 }
2200 return 0;
95c6e9cb
IM
2201}
2202
d5c21b88
IM
2203/**
2204 * Write a block of data into DSP code or data RAM using pre-allocated
2205 * DMA engine.
2206 *
2207 * @codec: the HDA codec
2208 * @fls: pointer to a fast load image
2209 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2210 * no relocation
2211 * @dma_engine: pointer to DMA engine to be used for DSP download
2212 * @dma_chan: The number of DMA channels used for DSP download
2213 * @port_map_mask: port mapping
2214 * @ovly: TRUE if overlay format is required
2215 *
2216 * Returns zero or a negative error code.
2217 */
01ef7dbf
IM
2218static int dspxfr_one_seg(struct hda_codec *codec,
2219 const struct dsp_image_seg *fls,
2220 unsigned int reloc,
2221 struct dma_engine *dma_engine,
2222 unsigned int dma_chan,
2223 unsigned int port_map_mask,
2224 bool ovly)
95c6e9cb 2225{
406261ce 2226 int status = 0;
01ef7dbf
IM
2227 bool comm_dma_setup_done = false;
2228 const unsigned int *data;
2229 unsigned int chip_addx;
2230 unsigned int words_to_write;
2231 unsigned int buffer_size_words;
2232 unsigned char *buffer_addx;
2233 unsigned short hda_format;
2234 unsigned int sample_rate_div;
2235 unsigned int sample_rate_mul;
2236 unsigned int num_chans;
2237 unsigned int hda_frame_size_words;
2238 unsigned int remainder_words;
2239 const u32 *data_remainder;
2240 u32 chip_addx_remainder;
2241 unsigned int run_size_words;
2242 const struct dsp_image_seg *hci_write = NULL;
2243 int retry;
2244
2245 if (fls == NULL)
2246 return -EINVAL;
2247 if (is_hci_prog_list_seg(fls)) {
2248 hci_write = fls;
2249 fls = get_next_seg_ptr(fls);
2250 }
95c6e9cb 2251
01ef7dbf
IM
2252 if (hci_write && (!fls || is_last(fls))) {
2253 snd_printdd("hci_write\n");
2254 return dspxfr_hci_write(codec, hci_write);
2255 }
95c6e9cb 2256
01ef7dbf
IM
2257 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2258 snd_printdd("Invalid Params\n");
2259 return -EINVAL;
95c6e9cb
IM
2260 }
2261
01ef7dbf
IM
2262 data = fls->data;
2263 chip_addx = fls->chip_addr,
2264 words_to_write = fls->count;
2265
2266 if (!words_to_write)
2267 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2268 if (reloc)
2269 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2270
2271 if (!UC_RANGE(chip_addx, words_to_write) &&
2272 !X_RANGE_ALL(chip_addx, words_to_write) &&
2273 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2274 snd_printdd("Invalid chip_addx Params\n");
2275 return -EINVAL;
95c6e9cb
IM
2276 }
2277
01ef7dbf
IM
2278 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2279 sizeof(u32);
2280
2281 buffer_addx = dma_get_buffer_addr(dma_engine);
2282
2283 if (buffer_addx == NULL) {
2284 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
2285 return -EINVAL;
2286 }
2287
2288 dma_get_converter_format(dma_engine, &hda_format);
2289 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2290 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2291 num_chans = get_hdafmt_chs(hda_format) + 1;
2292
2293 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2294 (num_chans * sample_rate_mul / sample_rate_div));
2295
2296 buffer_size_words = min(buffer_size_words,
2297 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2298 65536 : 32768));
2299 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2300 snd_printdd(
2301 "chpadr=0x%08x frmsz=%u nchan=%u "
2302 "rate_mul=%u div=%u bufsz=%u\n",
2303 chip_addx, hda_frame_size_words, num_chans,
2304 sample_rate_mul, sample_rate_div, buffer_size_words);
2305
2306 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2307 (buffer_size_words < hda_frame_size_words)) {
2308 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
2309 return -EINVAL;
2310 }
2311
2312 remainder_words = words_to_write % hda_frame_size_words;
2313 data_remainder = data;
2314 chip_addx_remainder = chip_addx;
2315
2316 data += remainder_words;
2317 chip_addx += remainder_words*sizeof(u32);
2318 words_to_write -= remainder_words;
2319
2320 while (words_to_write != 0) {
2321 run_size_words = min(buffer_size_words, words_to_write);
2322 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2323 words_to_write, run_size_words, remainder_words);
2324 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2325 if (!comm_dma_setup_done) {
2326 status = dsp_dma_stop(codec, dma_chan, ovly);
2327 if (status < 0)
425a7880 2328 return status;
01ef7dbf
IM
2329 status = dsp_dma_setup_common(codec, chip_addx,
2330 dma_chan, port_map_mask, ovly);
2331 if (status < 0)
2332 return status;
2333 comm_dma_setup_done = true;
2334 }
2335
2336 status = dsp_dma_setup(codec, chip_addx,
2337 run_size_words, dma_chan);
2338 if (status < 0)
2339 return status;
2340 status = dsp_dma_start(codec, dma_chan, ovly);
2341 if (status < 0)
2342 return status;
2343 if (!dsp_is_dma_active(codec, dma_chan)) {
2344 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
2345 return -EIO;
2346 }
2347 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2348 if (status < 0)
2349 return status;
2350 if (remainder_words != 0) {
2351 status = chipio_write_multiple(codec,
2352 chip_addx_remainder,
2353 data_remainder,
2354 remainder_words);
2355 remainder_words = 0;
2356 }
2357 if (hci_write) {
2358 status = dspxfr_hci_write(codec, hci_write);
2359 hci_write = NULL;
2360 }
2361 retry = 5000;
2362 while (dsp_is_dma_active(codec, dma_chan)) {
2363 if (--retry <= 0)
2364 break;
2365 }
2366 snd_printdd(KERN_INFO "+++++ DMA complete");
2367 dma_set_state(dma_engine, DMA_STATE_STOP);
2368 dma_reset(dma_engine);
2369
2370 if (status < 0)
2371 return status;
2372
2373 data += run_size_words;
2374 chip_addx += run_size_words*sizeof(u32);
2375 words_to_write -= run_size_words;
2376 }
2377
2378 if (remainder_words != 0) {
2379 status = chipio_write_multiple(codec, chip_addx_remainder,
2380 data_remainder, remainder_words);
2381 }
2382
2383 return status;
95c6e9cb
IM
2384}
2385
d5c21b88
IM
2386/**
2387 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2388 *
2389 * @codec: the HDA codec
2390 * @fls_data: pointer to a fast load image
2391 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2392 * no relocation
e97249dd
IM
2393 * @sample_rate: sampling rate of the stream used for DSP download
2394 * @number_channels: channels of the stream used for DSP download
d5c21b88
IM
2395 * @ovly: TRUE if overlay format is required
2396 *
2397 * Returns zero or a negative error code.
2398 */
01ef7dbf
IM
2399static int dspxfr_image(struct hda_codec *codec,
2400 const struct dsp_image_seg *fls_data,
e97249dd
IM
2401 unsigned int reloc,
2402 unsigned int sample_rate,
2403 unsigned short channels,
01ef7dbf 2404 bool ovly)
95c6e9cb
IM
2405{
2406 struct ca0132_spec *spec = codec->spec;
01ef7dbf
IM
2407 int status;
2408 unsigned short hda_format = 0;
2409 unsigned int response;
2410 unsigned char stream_id = 0;
2411 struct dma_engine *dma_engine;
2412 unsigned int dma_chan;
2413 unsigned int port_map_mask;
2414
2415 if (fls_data == NULL)
2416 return -EINVAL;
2417
2418 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
406261ce
IM
2419 if (!dma_engine) {
2420 status = -ENOMEM;
2421 goto exit;
2422 }
95c6e9cb 2423
01ef7dbf
IM
2424 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2425 if (!dma_engine->dmab) {
2426 status = -ENOMEM;
2427 goto exit;
2428 }
95c6e9cb 2429
01ef7dbf 2430 dma_engine->codec = codec;
e97249dd 2431 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
01ef7dbf
IM
2432 dma_engine->m_converter_format = hda_format;
2433 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2434 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2435
2436 dma_chan = 0;
2437
2438 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2439 hda_format, &response);
2440
2441 if (status < 0) {
2442 snd_printdd(KERN_ERR "set converter format fail");
2443 goto exit;
2444 }
2445
2446 status = snd_hda_codec_load_dsp_prepare(codec,
2447 dma_engine->m_converter_format,
2448 dma_engine->buf_size,
2449 dma_engine->dmab);
2450 if (status < 0)
95c6e9cb 2451 goto exit;
01ef7dbf
IM
2452 spec->dsp_stream_id = status;
2453
2454 if (ovly) {
2455 status = dspio_alloc_dma_chan(codec, &dma_chan);
2456 if (status < 0) {
2457 snd_printdd(KERN_ERR "alloc dmachan fail");
2458 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2459 goto exit;
2460 }
2461 }
95c6e9cb 2462
01ef7dbf
IM
2463 port_map_mask = 0;
2464 status = dsp_allocate_ports_format(codec, hda_format,
2465 &port_map_mask);
2466 if (status < 0) {
2467 snd_printdd(KERN_ERR "alloc ports fail");
2468 goto exit;
2469 }
2470
2471 stream_id = dma_get_stream_id(dma_engine);
2472 status = codec_set_converter_stream_channel(codec,
2473 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2474 if (status < 0) {
2475 snd_printdd(KERN_ERR "set stream chan fail");
2476 goto exit;
2477 }
2478
2479 while ((fls_data != NULL) && !is_last(fls_data)) {
2480 if (!is_valid(fls_data)) {
2481 snd_printdd(KERN_ERR "FLS check fail");
2482 status = -EINVAL;
2483 goto exit;
2484 }
2485 status = dspxfr_one_seg(codec, fls_data, reloc,
2486 dma_engine, dma_chan,
2487 port_map_mask, ovly);
2488 if (status < 0)
2489 break;
2490
2491 if (is_hci_prog_list_seg(fls_data))
2492 fls_data = get_next_seg_ptr(fls_data);
2493
2494 if ((fls_data != NULL) && !is_last(fls_data))
2495 fls_data = get_next_seg_ptr(fls_data);
2496 }
2497
2498 if (port_map_mask != 0)
2499 status = dsp_free_ports(codec);
2500
2501 if (status < 0)
95c6e9cb
IM
2502 goto exit;
2503
01ef7dbf
IM
2504 status = codec_set_converter_stream_channel(codec,
2505 WIDGET_CHIP_CTRL, 0, 0, &response);
2506
95c6e9cb 2507exit:
01ef7dbf
IM
2508 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2509 dspio_free_dma_chan(codec, dma_chan);
2510
2511 if (dma_engine->dmab)
2512 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2513 kfree(dma_engine->dmab);
2514 kfree(dma_engine);
2515
2516 return status;
95c6e9cb
IM
2517}
2518
2519/*
01ef7dbf 2520 * CA0132 DSP download stuffs.
95c6e9cb 2521 */
01ef7dbf 2522static void dspload_post_setup(struct hda_codec *codec)
95c6e9cb 2523{
01ef7dbf 2524 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
95c6e9cb 2525
01ef7dbf
IM
2526 /*set DSP speaker to 2.0 configuration*/
2527 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2528 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
95c6e9cb 2529
01ef7dbf
IM
2530 /*update write pointer*/
2531 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2532}
95c6e9cb 2533
d5c21b88
IM
2534/**
2535 * Download DSP from a DSP Image Fast Load structure. This structure is a
2536 * linear, non-constant sized element array of structures, each of which
2537 * contain the count of the data to be loaded, the data itself, and the
2538 * corresponding starting chip address of the starting data location.
2539 *
2540 * @codec: the HDA codec
2541 * @fls: pointer to a fast load image
2542 * @ovly: TRUE if overlay format is required
2543 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2544 * no relocation
2545 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2546 * @router_chans: number of audio router channels to be allocated (0 means use
2547 * internal defaults; max is 32)
2548 *
2549 * Returns zero or a negative error code.
2550 */
01ef7dbf
IM
2551static int dspload_image(struct hda_codec *codec,
2552 const struct dsp_image_seg *fls,
2553 bool ovly,
2554 unsigned int reloc,
2555 bool autostart,
2556 int router_chans)
2557{
2558 int status = 0;
e97249dd
IM
2559 unsigned int sample_rate;
2560 unsigned short channels;
01ef7dbf
IM
2561
2562 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2563 if (router_chans == 0) {
2564 if (!ovly)
2565 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2566 else
2567 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2568 }
95c6e9cb 2569
e97249dd
IM
2570 sample_rate = 48000;
2571 channels = (unsigned short)router_chans;
01ef7dbf 2572
e97249dd
IM
2573 while (channels > 16) {
2574 sample_rate *= 2;
2575 channels /= 2;
01ef7dbf
IM
2576 }
2577
01ef7dbf
IM
2578 do {
2579 snd_printdd(KERN_INFO "Ready to program DMA");
2580 if (!ovly)
2581 status = dsp_reset(codec);
2582
2583 if (status < 0)
2584 break;
2585
2586 snd_printdd(KERN_INFO "dsp_reset() complete");
e97249dd
IM
2587 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2588 ovly);
01ef7dbf
IM
2589
2590 if (status < 0)
2591 break;
2592
2593 snd_printdd(KERN_INFO "dspxfr_image() complete");
2594 if (autostart && !ovly) {
2595 dspload_post_setup(codec);
2596 status = dsp_set_run_state(codec);
2597 }
2598
2599 snd_printdd(KERN_INFO "LOAD FINISHED");
2600 } while (0);
2601
2602 return status;
2603}
2604
2605static bool dspload_is_loaded(struct hda_codec *codec)
2606{
2607 unsigned int data = 0;
2608 int status = 0;
2609
2610 status = chipio_read(codec, 0x40004, &data);
2611 if ((status < 0) || (data != 1))
2612 return false;
2613
2614 return true;
2615}
2616
2617static bool dspload_wait_loaded(struct hda_codec *codec)
2618{
2619 int retry = 100;
2620
2621 do {
2622 msleep(20);
2623 if (dspload_is_loaded(codec)) {
2624 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2625 return true;
2626 }
2627 } while (--retry);
2628
2629 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2630 return false;
95c6e9cb
IM
2631}
2632
2633/*
825315bc 2634 * PCM stuffs
95c6e9cb 2635 */
825315bc
IM
2636static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2637 u32 stream_tag,
2638 int channel_id, int format)
95c6e9cb 2639{
825315bc
IM
2640 unsigned int oldval, newval;
2641
2642 if (!nid)
2643 return;
2644
2645 snd_printdd(
2646 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2647 "channel=%d, format=0x%x\n",
2648 nid, stream_tag, channel_id, format);
2649
2650 /* update the format-id if changed */
2651 oldval = snd_hda_codec_read(codec, nid, 0,
2652 AC_VERB_GET_STREAM_FORMAT,
2653 0);
2654 if (oldval != format) {
2655 msleep(20);
2656 snd_hda_codec_write(codec, nid, 0,
2657 AC_VERB_SET_STREAM_FORMAT,
2658 format);
2659 }
2660
2661 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2662 newval = (stream_tag << 4) | channel_id;
2663 if (oldval != newval) {
2664 snd_hda_codec_write(codec, nid, 0,
2665 AC_VERB_SET_CHANNEL_STREAMID,
2666 newval);
2667 }
2668}
2669
2670static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2671{
2672 unsigned int val;
2673
2674 if (!nid)
2675 return;
2676
2677 snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
2678
2679 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2680 if (!val)
2681 return;
2682
2683 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2684 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
95c6e9cb
IM
2685}
2686
825315bc
IM
2687/*
2688 * PCM callbacks
2689 */
95c6e9cb
IM
2690static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2691 struct hda_codec *codec,
2692 unsigned int stream_tag,
2693 unsigned int format,
2694 struct snd_pcm_substream *substream)
2695{
2696 struct ca0132_spec *spec = codec->spec;
825315bc
IM
2697
2698 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2699
2700 return 0;
95c6e9cb
IM
2701}
2702
2703static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2704 struct hda_codec *codec,
2705 struct snd_pcm_substream *substream)
2706{
2707 struct ca0132_spec *spec = codec->spec;
825315bc
IM
2708
2709 if (spec->dsp_state == DSP_DOWNLOADING)
2710 return 0;
2711
2712 /*If Playback effects are on, allow stream some time to flush
2713 *effects tail*/
2714 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
2715 msleep(50);
2716
2717 ca0132_cleanup_stream(codec, spec->dacs[0]);
2718
2719 return 0;
95c6e9cb
IM
2720}
2721
2722/*
2723 * Digital out
2724 */
27ebeb0b
TI
2725static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2726 struct hda_codec *codec,
2727 struct snd_pcm_substream *substream)
95c6e9cb
IM
2728{
2729 struct ca0132_spec *spec = codec->spec;
27ebeb0b 2730 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
95c6e9cb
IM
2731}
2732
27ebeb0b 2733static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
95c6e9cb
IM
2734 struct hda_codec *codec,
2735 unsigned int stream_tag,
2736 unsigned int format,
2737 struct snd_pcm_substream *substream)
2738{
2739 struct ca0132_spec *spec = codec->spec;
27ebeb0b
TI
2740 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2741 stream_tag, format, substream);
95c6e9cb
IM
2742}
2743
27ebeb0b 2744static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
95c6e9cb 2745 struct hda_codec *codec,
95c6e9cb
IM
2746 struct snd_pcm_substream *substream)
2747{
2748 struct ca0132_spec *spec = codec->spec;
27ebeb0b 2749 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
95c6e9cb
IM
2750}
2751
27ebeb0b
TI
2752static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2753 struct hda_codec *codec,
2754 struct snd_pcm_substream *substream)
95c6e9cb
IM
2755{
2756 struct ca0132_spec *spec = codec->spec;
27ebeb0b 2757 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
95c6e9cb
IM
2758}
2759
825315bc
IM
2760/*
2761 * Analog capture
2762 */
2763static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2764 struct hda_codec *codec,
2765 unsigned int stream_tag,
2766 unsigned int format,
2767 struct snd_pcm_substream *substream)
2768{
2769 struct ca0132_spec *spec = codec->spec;
2770
2771 ca0132_setup_stream(codec, spec->adcs[substream->number],
2772 stream_tag, 0, format);
2773
2774 return 0;
2775}
2776
2777static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2778 struct hda_codec *codec,
2779 struct snd_pcm_substream *substream)
2780{
2781 struct ca0132_spec *spec = codec->spec;
2782
2783 if (spec->dsp_state == DSP_DOWNLOADING)
2784 return 0;
2785
2786 ca0132_cleanup_stream(codec, hinfo->nid);
2787 return 0;
2788}
2789
e90f29e4
IM
2790/*
2791 * Controls stuffs.
2792 */
2793
2794/*
2795 * Mixer controls helpers.
2796 */
2797#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2798 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2799 .name = xname, \
2800 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2801 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2802 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2803 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2804 .info = ca0132_volume_info, \
2805 .get = ca0132_volume_get, \
2806 .put = ca0132_volume_put, \
2807 .tlv = { .c = ca0132_volume_tlv }, \
2808 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2809
2810#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2811 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2812 .name = xname, \
2813 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2814 .info = snd_hda_mixer_amp_switch_info, \
2815 .get = ca0132_switch_get, \
2816 .put = ca0132_switch_put, \
2817 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2818
2819/* stereo */
2820#define CA0132_CODEC_VOL(xname, nid, dir) \
2821 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2822#define CA0132_CODEC_MUTE(xname, nid, dir) \
2823 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2824
44f0c978
IM
2825/* The followings are for tuning of products */
2826#ifdef ENABLE_TUNING_CONTROLS
2827
2828static unsigned int voice_focus_vals_lookup[] = {
28290x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
28300x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
28310x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
28320x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
28330x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
28340x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
28350x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
28360x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
28370x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
28380x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
28390x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
28400x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
28410x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
28420x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
28430x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
28440x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
28450x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
28460x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
28470x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
28480x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
28490x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
28500x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
28510x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
28520x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
28530x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
28540x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
28550x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
2856};
2857
2858static unsigned int mic_svm_vals_lookup[] = {
28590x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
28600x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
28610x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
28620x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
28630x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
28640x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
28650x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
28660x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
28670x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
28680x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
28690x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
28700x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
28710x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
28720x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
28730x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
28740x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
28750x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
2876};
2877
2878static unsigned int equalizer_vals_lookup[] = {
28790xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
28800xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
28810xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
28820xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
28830x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
28840x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
28850x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
28860x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
28870x41C00000
2888};
2889
2890static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
2891 unsigned int *lookup, int idx)
2892{
2893 int i = 0;
2894
2895 for (i = 0; i < TUNING_CTLS_COUNT; i++)
2896 if (nid == ca0132_tuning_ctls[i].nid)
2897 break;
2898
2899 snd_hda_power_up(codec);
2900 dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
2901 ca0132_tuning_ctls[i].req,
2902 &(lookup[idx]), sizeof(unsigned int));
2903 snd_hda_power_down(codec);
2904
2905 return 1;
2906}
2907
2908static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
2909 struct snd_ctl_elem_value *ucontrol)
2910{
2911 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2912 struct ca0132_spec *spec = codec->spec;
2913 hda_nid_t nid = get_amp_nid(kcontrol);
2914 long *valp = ucontrol->value.integer.value;
2915 int idx = nid - TUNING_CTL_START_NID;
2916
2917 *valp = spec->cur_ctl_vals[idx];
2918 return 0;
2919}
2920
2921static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
2922 struct snd_ctl_elem_info *uinfo)
2923{
2924 int chs = get_amp_channels(kcontrol);
2925 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2926 uinfo->count = chs == 3 ? 2 : 1;
2927 uinfo->value.integer.min = 20;
2928 uinfo->value.integer.max = 180;
2929 uinfo->value.integer.step = 1;
2930
2931 return 0;
2932}
2933
2934static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2936{
2937 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2938 struct ca0132_spec *spec = codec->spec;
2939 hda_nid_t nid = get_amp_nid(kcontrol);
2940 long *valp = ucontrol->value.integer.value;
2941 int idx;
2942
2943 idx = nid - TUNING_CTL_START_NID;
2944 /* any change? */
2945 if (spec->cur_ctl_vals[idx] == *valp)
2946 return 0;
2947
2948 spec->cur_ctl_vals[idx] = *valp;
2949
2950 idx = *valp - 20;
2951 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
2952
2953 return 1;
2954}
2955
2956static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_info *uinfo)
2958{
2959 int chs = get_amp_channels(kcontrol);
2960 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2961 uinfo->count = chs == 3 ? 2 : 1;
2962 uinfo->value.integer.min = 0;
2963 uinfo->value.integer.max = 100;
2964 uinfo->value.integer.step = 1;
2965
2966 return 0;
2967}
2968
2969static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2971{
2972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2973 struct ca0132_spec *spec = codec->spec;
2974 hda_nid_t nid = get_amp_nid(kcontrol);
2975 long *valp = ucontrol->value.integer.value;
2976 int idx;
2977
2978 idx = nid - TUNING_CTL_START_NID;
2979 /* any change? */
2980 if (spec->cur_ctl_vals[idx] == *valp)
2981 return 0;
2982
2983 spec->cur_ctl_vals[idx] = *valp;
2984
2985 idx = *valp;
2986 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
2987
2988 return 0;
2989}
2990
2991static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
2992 struct snd_ctl_elem_info *uinfo)
2993{
2994 int chs = get_amp_channels(kcontrol);
2995 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2996 uinfo->count = chs == 3 ? 2 : 1;
2997 uinfo->value.integer.min = 0;
2998 uinfo->value.integer.max = 48;
2999 uinfo->value.integer.step = 1;
3000
3001 return 0;
3002}
3003
3004static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3005 struct snd_ctl_elem_value *ucontrol)
3006{
3007 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3008 struct ca0132_spec *spec = codec->spec;
3009 hda_nid_t nid = get_amp_nid(kcontrol);
3010 long *valp = ucontrol->value.integer.value;
3011 int idx;
3012
3013 idx = nid - TUNING_CTL_START_NID;
3014 /* any change? */
3015 if (spec->cur_ctl_vals[idx] == *valp)
3016 return 0;
3017
3018 spec->cur_ctl_vals[idx] = *valp;
3019
3020 idx = *valp;
3021 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3022
3023 return 1;
3024}
3025
3026static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3027static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
3028
3029static int add_tuning_control(struct hda_codec *codec,
3030 hda_nid_t pnid, hda_nid_t nid,
3031 const char *name, int dir)
3032{
3033 char namestr[44];
3034 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3035 struct snd_kcontrol_new knew =
3036 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3037
3038 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3039 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3040 knew.tlv.c = 0;
3041 knew.tlv.p = 0;
3042 switch (pnid) {
3043 case VOICE_FOCUS:
3044 knew.info = voice_focus_ctl_info;
3045 knew.get = tuning_ctl_get;
3046 knew.put = voice_focus_ctl_put;
3047 knew.tlv.p = voice_focus_db_scale;
3048 break;
3049 case MIC_SVM:
3050 knew.info = mic_svm_ctl_info;
3051 knew.get = tuning_ctl_get;
3052 knew.put = mic_svm_ctl_put;
3053 break;
3054 case EQUALIZER:
3055 knew.info = equalizer_ctl_info;
3056 knew.get = tuning_ctl_get;
3057 knew.put = equalizer_ctl_put;
3058 knew.tlv.p = eq_db_scale;
3059 break;
3060 default:
3061 return 0;
3062 }
3063 knew.private_value =
3064 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3065 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3066 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3067}
3068
3069static int add_tuning_ctls(struct hda_codec *codec)
3070{
3071 int i;
3072 int err;
3073
3074 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3075 err = add_tuning_control(codec,
3076 ca0132_tuning_ctls[i].parent_nid,
3077 ca0132_tuning_ctls[i].nid,
3078 ca0132_tuning_ctls[i].name,
3079 ca0132_tuning_ctls[i].direct);
3080 if (err < 0)
3081 return err;
3082 }
3083
3084 return 0;
3085}
3086
3087static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3088{
3089 struct ca0132_spec *spec = codec->spec;
3090 int i;
3091
3092 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3093 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3094 /* SVM level defaults to 0.74. */
3095 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3096
3097 /* EQ defaults to 0dB. */
3098 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3099 spec->cur_ctl_vals[i] = 24;
3100}
3101#endif /*ENABLE_TUNING_CONTROLS*/
3102
5aaca44d
IM
3103/*
3104 * Select the active output.
3105 * If autodetect is enabled, output will be selected based on jack detection.
3106 * If jack inserted, headphone will be selected, else built-in speakers
3107 * If autodetect is disabled, output will be selected based on selection.
3108 */
3109static int ca0132_select_out(struct hda_codec *codec)
3110{
3111 struct ca0132_spec *spec = codec->spec;
3112 unsigned int pin_ctl;
3113 int jack_present;
3114 int auto_jack;
3115 unsigned int tmp;
3116 int err;
3117
3118 snd_printdd(KERN_INFO "ca0132_select_out\n");
3119
3120 snd_hda_power_up(codec);
3121
3122 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3123
3124 if (auto_jack)
3125 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
3126 else
3127 jack_present =
3128 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3129
3130 if (jack_present)
3131 spec->cur_out_type = HEADPHONE_OUT;
3132 else
3133 spec->cur_out_type = SPEAKER_OUT;
3134
3135 if (spec->cur_out_type == SPEAKER_OUT) {
3136 snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
3137 /*speaker out config*/
3138 tmp = FLOAT_ONE;
3139 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3140 if (err < 0)
3141 goto exit;
3142 /*enable speaker EQ*/
3143 tmp = FLOAT_ONE;
3144 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3145 if (err < 0)
3146 goto exit;
3147
3148 /* Setup EAPD */
3149 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3150 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3151 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3152 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3153 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3154 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3155 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3156 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3157
3158 /* disable headphone node */
3159 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3160 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
a0c041cb
TI
3161 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3162 pin_ctl & ~PIN_HP);
5aaca44d
IM
3163 /* enable speaker node */
3164 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3165 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
a0c041cb
TI
3166 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3167 pin_ctl | PIN_OUT);
5aaca44d
IM
3168 } else {
3169 snd_printdd(KERN_INFO "ca0132_select_out hp\n");
3170 /*headphone out config*/
3171 tmp = FLOAT_ZERO;
3172 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3173 if (err < 0)
3174 goto exit;
3175 /*disable speaker EQ*/
3176 tmp = FLOAT_ZERO;
3177 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3178 if (err < 0)
3179 goto exit;
3180
3181 /* Setup EAPD */
3182 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3183 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3184 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3185 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3186 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3187 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3188 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3189 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3190
3191 /* disable speaker*/
3192 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3193 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
a0c041cb
TI
3194 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3195 pin_ctl & ~PIN_HP);
5aaca44d
IM
3196 /* enable headphone*/
3197 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3198 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
a0c041cb
TI
3199 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3200 pin_ctl | PIN_HP);
5aaca44d
IM
3201 }
3202
3203exit:
3204 snd_hda_power_down(codec);
3205
3206 return err < 0 ? err : 0;
3207}
3208
3209static void ca0132_set_dmic(struct hda_codec *codec, int enable);
3210static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
3211static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
3212
3213/*
3214 * Select the active VIP source
3215 */
3216static int ca0132_set_vipsource(struct hda_codec *codec, int val)
3217{
3218 struct ca0132_spec *spec = codec->spec;
3219 unsigned int tmp;
3220
3221 if (!dspload_is_loaded(codec))
3222 return 0;
3223
3224 /* if CrystalVoice if off, vipsource should be 0 */
3225 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
3226 (val == 0)) {
3227 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3228 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3229 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3230 if (spec->cur_mic_type == DIGITAL_MIC)
3231 tmp = FLOAT_TWO;
3232 else
3233 tmp = FLOAT_ONE;
3234 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3235 tmp = FLOAT_ZERO;
3236 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3237 } else {
3238 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3239 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3240 if (spec->cur_mic_type == DIGITAL_MIC)
3241 tmp = FLOAT_TWO;
3242 else
3243 tmp = FLOAT_ONE;
3244 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3245 tmp = FLOAT_ONE;
3246 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3247 msleep(20);
3248 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3249 }
3250
3251 return 1;
3252}
3253
3254/*
3255 * Select the active microphone.
3256 * If autodetect is enabled, mic will be selected based on jack detection.
3257 * If jack inserted, ext.mic will be selected, else built-in mic
3258 * If autodetect is disabled, mic will be selected based on selection.
3259 */
3260static int ca0132_select_mic(struct hda_codec *codec)
3261{
3262 struct ca0132_spec *spec = codec->spec;
3263 int jack_present;
3264 int auto_jack;
3265
3266 snd_printdd(KERN_INFO "ca0132_select_mic\n");
3267
3268 snd_hda_power_up(codec);
3269
3270 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3271
3272 if (auto_jack)
3273 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3274 else
3275 jack_present =
3276 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3277
3278 if (jack_present)
3279 spec->cur_mic_type = LINE_MIC_IN;
3280 else
3281 spec->cur_mic_type = DIGITAL_MIC;
3282
3283 if (spec->cur_mic_type == DIGITAL_MIC) {
3284 /* enable digital Mic */
3285 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3286 ca0132_set_dmic(codec, 1);
3287 ca0132_mic_boost_set(codec, 0);
3288 /* set voice focus */
3289 ca0132_effects_set(codec, VOICE_FOCUS,
3290 spec->effects_switch
3291 [VOICE_FOCUS - EFFECT_START_NID]);
3292 } else {
3293 /* disable digital Mic */
3294 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3295 ca0132_set_dmic(codec, 0);
3296 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3297 /* disable voice focus */
3298 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3299 }
3300
3301 snd_hda_power_down(codec);
3302
3303 return 0;
3304}
3305
a7e76271
IM
3306/*
3307 * Check if VNODE settings take effect immediately.
3308 */
3309static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3310 hda_nid_t vnid,
3311 hda_nid_t *shared_nid)
3312{
3313 struct ca0132_spec *spec = codec->spec;
3314 hda_nid_t nid;
3315 bool effective = false;
3316
3317 switch (vnid) {
3318 case VNID_SPK:
3319 nid = spec->shared_out_nid;
3320 effective = true;
3321 break;
3322 case VNID_MIC:
3323 nid = spec->shared_mic_nid;
3324 effective = true;
3325 break;
3326 default:
3327 break;
3328 }
3329
3330 if (effective && shared_nid)
3331 *shared_nid = nid;
3332
3333 return effective;
3334}
3335
3336/*
3337* The following functions are control change helpers.
3338* They return 0 if no changed. Return 1 if changed.
3339*/
3340static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3341{
3342 struct ca0132_spec *spec = codec->spec;
3343 unsigned int tmp;
3344
3345 /* based on CrystalVoice state to enable VoiceFX. */
3346 if (enable) {
3347 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3348 FLOAT_ONE : FLOAT_ZERO;
3349 } else {
3350 tmp = FLOAT_ZERO;
3351 }
3352
3353 dspio_set_uint_param(codec, ca0132_voicefx.mid,
3354 ca0132_voicefx.reqs[0], tmp);
3355
3356 return 1;
3357}
3358
5aaca44d
IM
3359/*
3360 * Set the effects parameters
3361 */
3362static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3363{
3364 struct ca0132_spec *spec = codec->spec;
3365 unsigned int on;
3366 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3367 int err = 0;
3368 int idx = nid - EFFECT_START_NID;
3369
3370 if ((idx < 0) || (idx >= num_fx))
3371 return 0; /* no changed */
3372
3373 /* for out effect, qualify with PE */
3374 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3375 /* if PE if off, turn off out effects. */
3376 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3377 val = 0;
3378 }
3379
3380 /* for in effect, qualify with CrystalVoice */
3381 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3382 /* if CrystalVoice if off, turn off in effects. */
3383 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3384 val = 0;
3385
3386 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3387 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3388 val = 0;
3389 }
3390
3391 snd_printdd(KERN_INFO, "ca0132_effect_set: nid=0x%x, val=%ld\n",
3392 nid, val);
3393
3394 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3395 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
3396 ca0132_effects[idx].reqs[0], on);
3397
3398 if (err < 0)
3399 return 0; /* no changed */
3400
3401 return 1;
3402}
3403
a7e76271
IM
3404/*
3405 * Turn on/off Playback Enhancements
3406 */
3407static int ca0132_pe_switch_set(struct hda_codec *codec)
3408{
3409 struct ca0132_spec *spec = codec->spec;
3410 hda_nid_t nid;
3411 int i, ret = 0;
3412
3413 snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
3414 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3415
3416 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3417 nid = OUT_EFFECT_START_NID;
3418 /* PE affects all out effects */
3419 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3420 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3421
3422 return ret;
3423}
3424
5aaca44d
IM
3425/* Check if Mic1 is streaming, if so, stop streaming */
3426static int stop_mic1(struct hda_codec *codec)
3427{
3428 struct ca0132_spec *spec = codec->spec;
3429 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
3430 AC_VERB_GET_CONV, 0);
3431 if (oldval != 0)
3432 snd_hda_codec_write(codec, spec->adcs[0], 0,
3433 AC_VERB_SET_CHANNEL_STREAMID,
3434 0);
3435 return oldval;
3436}
3437
3438/* Resume Mic1 streaming if it was stopped. */
3439static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
3440{
3441 struct ca0132_spec *spec = codec->spec;
3442 /* Restore the previous stream and channel */
3443 if (oldval != 0)
3444 snd_hda_codec_write(codec, spec->adcs[0], 0,
3445 AC_VERB_SET_CHANNEL_STREAMID,
3446 oldval);
3447}
3448
3449/*
a7e76271 3450 * Turn on/off CrystalVoice
5aaca44d 3451 */
a7e76271
IM
3452static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3453{
3454 struct ca0132_spec *spec = codec->spec;
3455 hda_nid_t nid;
3456 int i, ret = 0;
3457 unsigned int oldval;
3458
3459 snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
3460 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3461
3462 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3463 nid = IN_EFFECT_START_NID;
3464 /* CrystalVoice affects all in effects */
3465 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3466 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3467
3468 /* including VoiceFX */
3469 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3470
3471 /* set correct vipsource */
3472 oldval = stop_mic1(codec);
3473 ret |= ca0132_set_vipsource(codec, 1);
3474 resume_mic1(codec, oldval);
3475 return ret;
3476}
3477
5aaca44d
IM
3478static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3479{
3480 struct ca0132_spec *spec = codec->spec;
3481 int ret = 0;
3482
3483 if (val) /* on */
3484 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3485 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3486 else /* off */
3487 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3488 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3489
3490 return ret;
3491}
3492
a7e76271
IM
3493static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3494 struct snd_ctl_elem_value *ucontrol)
95c6e9cb 3495{
a7e76271
IM
3496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3497 hda_nid_t nid = get_amp_nid(kcontrol);
3498 hda_nid_t shared_nid = 0;
3499 bool effective;
3500 int ret = 0;
95c6e9cb 3501 struct ca0132_spec *spec = codec->spec;
a7e76271 3502 int auto_jack;
95c6e9cb 3503
a7e76271
IM
3504 if (nid == VNID_HP_SEL) {
3505 auto_jack =
3506 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3507 if (!auto_jack)
3508 ca0132_select_out(codec);
3509 return 1;
3510 }
95c6e9cb 3511
a7e76271
IM
3512 if (nid == VNID_AMIC1_SEL) {
3513 auto_jack =
3514 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3515 if (!auto_jack)
3516 ca0132_select_mic(codec);
3517 return 1;
3518 }
95c6e9cb 3519
a7e76271
IM
3520 if (nid == VNID_HP_ASEL) {
3521 ca0132_select_out(codec);
3522 return 1;
3523 }
95c6e9cb 3524
a7e76271
IM
3525 if (nid == VNID_AMIC1_ASEL) {
3526 ca0132_select_mic(codec);
3527 return 1;
95c6e9cb 3528 }
a7e76271
IM
3529
3530 /* if effective conditions, then update hw immediately. */
3531 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3532 if (effective) {
3533 int dir = get_amp_direction(kcontrol);
3534 int ch = get_amp_channels(kcontrol);
3535 unsigned long pval;
3536
3537 mutex_lock(&codec->control_mutex);
3538 pval = kcontrol->private_value;
3539 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3540 0, dir);
3541 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3542 kcontrol->private_value = pval;
3543 mutex_unlock(&codec->control_mutex);
95c6e9cb 3544 }
95c6e9cb 3545
a7e76271 3546 return ret;
95c6e9cb 3547}
a7e76271 3548/* End of control change helpers. */
95c6e9cb 3549
a7e76271
IM
3550static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3551 struct snd_ctl_elem_info *uinfo)
3552{
3553 unsigned int items = sizeof(ca0132_voicefx_presets)
3554 / sizeof(struct ct_voicefx_preset);
3555
3556 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3557 uinfo->count = 1;
3558 uinfo->value.enumerated.items = items;
3559 if (uinfo->value.enumerated.item >= items)
3560 uinfo->value.enumerated.item = items - 1;
3561 strcpy(uinfo->value.enumerated.name,
3562 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3563 return 0;
3564}
95c6e9cb 3565
a7e76271 3566static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
95c6e9cb
IM
3567 struct snd_ctl_elem_value *ucontrol)
3568{
3569 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3570 struct ca0132_spec *spec = codec->spec;
95c6e9cb 3571
a7e76271 3572 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
95c6e9cb
IM
3573 return 0;
3574}
3575
a7e76271 3576static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
95c6e9cb
IM
3577 struct snd_ctl_elem_value *ucontrol)
3578{
3579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3580 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
3581 int i, err = 0;
3582 int sel = ucontrol->value.enumerated.item[0];
3583 unsigned int items = sizeof(ca0132_voicefx_presets)
3584 / sizeof(struct ct_voicefx_preset);
95c6e9cb 3585
a7e76271 3586 if (sel >= items)
95c6e9cb
IM
3587 return 0;
3588
a7e76271
IM
3589 snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
3590 sel, ca0132_voicefx_presets[sel].name);
95c6e9cb 3591
a7e76271
IM
3592 /*
3593 * Idx 0 is default.
3594 * Default needs to qualify with CrystalVoice state.
3595 */
3596 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3597 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
3598 ca0132_voicefx.reqs[i],
3599 ca0132_voicefx_presets[sel].vals[i]);
3600 if (err < 0)
3601 break;
3602 }
95c6e9cb 3603
a7e76271
IM
3604 if (err >= 0) {
3605 spec->voicefx_val = sel;
3606 /* enable voice fx */
3607 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3608 }
95c6e9cb 3609
a7e76271 3610 return 1;
95c6e9cb
IM
3611}
3612
a7e76271
IM
3613static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3614 struct snd_ctl_elem_value *ucontrol)
95c6e9cb
IM
3615{
3616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3617 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
3618 hda_nid_t nid = get_amp_nid(kcontrol);
3619 int ch = get_amp_channels(kcontrol);
95c6e9cb
IM
3620 long *valp = ucontrol->value.integer.value;
3621
a7e76271
IM
3622 /* vnode */
3623 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3624 if (ch & 1) {
3625 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3626 valp++;
3627 }
3628 if (ch & 2) {
3629 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3630 valp++;
3631 }
3632 return 0;
3633 }
3634
3635 /* effects, include PE and CrystalVoice */
3636 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3637 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3638 return 0;
3639 }
3640
3641 /* mic boost */
3642 if (nid == spec->input_pins[0]) {
3643 *valp = spec->cur_mic_boost;
3644 return 0;
3645 }
3646
95c6e9cb
IM
3647 return 0;
3648}
3649
a7e76271
IM
3650static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3651 struct snd_ctl_elem_value *ucontrol)
95c6e9cb
IM
3652{
3653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3654 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
3655 hda_nid_t nid = get_amp_nid(kcontrol);
3656 int ch = get_amp_channels(kcontrol);
95c6e9cb 3657 long *valp = ucontrol->value.integer.value;
a7e76271 3658 int changed = 1;
95c6e9cb 3659
a7e76271
IM
3660 snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
3661 nid, *valp);
95c6e9cb
IM
3662
3663 snd_hda_power_up(codec);
a7e76271
IM
3664 /* vnode */
3665 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3666 if (ch & 1) {
3667 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3668 valp++;
3669 }
3670 if (ch & 2) {
3671 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3672 valp++;
3673 }
3674 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3675 goto exit;
3676 }
95c6e9cb 3677
a7e76271
IM
3678 /* PE */
3679 if (nid == PLAY_ENHANCEMENT) {
3680 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3681 changed = ca0132_pe_switch_set(codec);
b97f6bfd 3682 goto exit;
a7e76271 3683 }
95c6e9cb 3684
a7e76271
IM
3685 /* CrystalVoice */
3686 if (nid == CRYSTAL_VOICE) {
3687 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3688 changed = ca0132_cvoice_switch_set(codec);
b97f6bfd 3689 goto exit;
a7e76271 3690 }
95c6e9cb 3691
a7e76271
IM
3692 /* out and in effects */
3693 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3694 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3695 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3696 changed = ca0132_effects_set(codec, nid, *valp);
3697 goto exit;
3698 }
3699
3700 /* mic boost */
3701 if (nid == spec->input_pins[0]) {
3702 spec->cur_mic_boost = *valp;
3703
3704 /* Mic boost does not apply to Digital Mic */
3705 if (spec->cur_mic_type != DIGITAL_MIC)
3706 changed = ca0132_mic_boost_set(codec, *valp);
3707 goto exit;
3708 }
95c6e9cb 3709
a7e76271 3710exit:
95c6e9cb 3711 snd_hda_power_down(codec);
a7e76271 3712 return changed;
95c6e9cb
IM
3713}
3714
a7e76271
IM
3715/*
3716 * Volume related
3717 */
3718static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3719 struct snd_ctl_elem_info *uinfo)
3720{
3721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3722 struct ca0132_spec *spec = codec->spec;
3723 hda_nid_t nid = get_amp_nid(kcontrol);
3724 int ch = get_amp_channels(kcontrol);
3725 int dir = get_amp_direction(kcontrol);
3726 unsigned long pval;
3727 int err;
3728
3729 switch (nid) {
3730 case VNID_SPK:
3731 /* follow shared_out info */
3732 nid = spec->shared_out_nid;
3733 mutex_lock(&codec->control_mutex);
3734 pval = kcontrol->private_value;
3735 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3736 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3737 kcontrol->private_value = pval;
3738 mutex_unlock(&codec->control_mutex);
3739 break;
3740 case VNID_MIC:
3741 /* follow shared_mic info */
3742 nid = spec->shared_mic_nid;
3743 mutex_lock(&codec->control_mutex);
3744 pval = kcontrol->private_value;
3745 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3746 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3747 kcontrol->private_value = pval;
3748 mutex_unlock(&codec->control_mutex);
3749 break;
3750 default:
3751 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3752 }
3753 return err;
3754}
3755
3756static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
95c6e9cb
IM
3757 struct snd_ctl_elem_value *ucontrol)
3758{
3759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3760 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
3761 hda_nid_t nid = get_amp_nid(kcontrol);
3762 int ch = get_amp_channels(kcontrol);
95c6e9cb
IM
3763 long *valp = ucontrol->value.integer.value;
3764
a7e76271
IM
3765 /* store the left and right volume */
3766 if (ch & 1) {
3767 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3768 valp++;
3769 }
3770 if (ch & 2) {
3771 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3772 valp++;
3773 }
95c6e9cb
IM
3774 return 0;
3775}
3776
a7e76271 3777static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
95c6e9cb
IM
3778 struct snd_ctl_elem_value *ucontrol)
3779{
3780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3781 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
3782 hda_nid_t nid = get_amp_nid(kcontrol);
3783 int ch = get_amp_channels(kcontrol);
95c6e9cb 3784 long *valp = ucontrol->value.integer.value;
a7e76271
IM
3785 hda_nid_t shared_nid = 0;
3786 bool effective;
3787 int changed = 1;
3788
3789 /* store the left and right volume */
3790 if (ch & 1) {
3791 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3792 valp++;
3793 }
3794 if (ch & 2) {
3795 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3796 valp++;
3797 }
95c6e9cb 3798
a7e76271
IM
3799 /* if effective conditions, then update hw immediately. */
3800 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3801 if (effective) {
3802 int dir = get_amp_direction(kcontrol);
3803 unsigned long pval;
3804
3805 snd_hda_power_up(codec);
3806 mutex_lock(&codec->control_mutex);
3807 pval = kcontrol->private_value;
3808 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3809 0, dir);
3810 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3811 kcontrol->private_value = pval;
3812 mutex_unlock(&codec->control_mutex);
3813 snd_hda_power_down(codec);
3814 }
95c6e9cb 3815
a7e76271 3816 return changed;
95c6e9cb
IM
3817}
3818
a7e76271
IM
3819static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3820 unsigned int size, unsigned int __user *tlv)
95c6e9cb 3821{
a7e76271
IM
3822 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3823 struct ca0132_spec *spec = codec->spec;
3824 hda_nid_t nid = get_amp_nid(kcontrol);
3825 int ch = get_amp_channels(kcontrol);
3826 int dir = get_amp_direction(kcontrol);
3827 unsigned long pval;
3828 int err;
3829
3830 switch (nid) {
3831 case VNID_SPK:
3832 /* follow shared_out tlv */
3833 nid = spec->shared_out_nid;
3834 mutex_lock(&codec->control_mutex);
3835 pval = kcontrol->private_value;
3836 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3837 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3838 kcontrol->private_value = pval;
3839 mutex_unlock(&codec->control_mutex);
3840 break;
3841 case VNID_MIC:
3842 /* follow shared_mic tlv */
3843 nid = spec->shared_mic_nid;
3844 mutex_lock(&codec->control_mutex);
3845 pval = kcontrol->private_value;
3846 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3847 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3848 kcontrol->private_value = pval;
3849 mutex_unlock(&codec->control_mutex);
3850 break;
3851 default:
3852 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3853 }
3854 return err;
95c6e9cb
IM
3855}
3856
a7e76271
IM
3857static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3858 const char *pfx, int dir)
95c6e9cb 3859{
a7e76271
IM
3860 char namestr[44];
3861 int type = dir ? HDA_INPUT : HDA_OUTPUT;
95c6e9cb 3862 struct snd_kcontrol_new knew =
a7e76271
IM
3863 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3864 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
95c6e9cb
IM
3865 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3866}
3867
a7e76271 3868static int add_voicefx(struct hda_codec *codec)
95c6e9cb
IM
3869{
3870 struct snd_kcontrol_new knew =
a7e76271
IM
3871 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3872 VOICEFX, 1, 0, HDA_INPUT);
3873 knew.info = ca0132_voicefx_info;
3874 knew.get = ca0132_voicefx_get;
3875 knew.put = ca0132_voicefx_put;
3876 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
95c6e9cb
IM
3877}
3878
a7e76271
IM
3879/*
3880 * When changing Node IDs for Mixer Controls below, make sure to update
3881 * Node IDs in ca0132_config() as well.
3882 */
3883static struct snd_kcontrol_new ca0132_mixer[] = {
3884 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3885 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3886 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3887 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3888 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3889 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3890 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3891 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3892 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3893 0x12, 1, HDA_INPUT),
3894 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3895 VNID_HP_SEL, 1, HDA_OUTPUT),
3896 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3897 VNID_AMIC1_SEL, 1, HDA_INPUT),
3898 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
3899 VNID_HP_ASEL, 1, HDA_OUTPUT),
3900 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
3901 VNID_AMIC1_ASEL, 1, HDA_INPUT),
3902 { } /* end */
3903};
3904
e90f29e4
IM
3905static int ca0132_build_controls(struct hda_codec *codec)
3906{
3907 struct ca0132_spec *spec = codec->spec;
3908 int i, num_fx;
3909 int err = 0;
3910
3911 /* Add Mixer controls */
3912 for (i = 0; i < spec->num_mixers; i++) {
3913 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
3914 if (err < 0)
3915 return err;
3916 }
3917
3918 /* Add in and out effects controls.
3919 * VoiceFX, PE and CrystalVoice are added separately.
3920 */
3921 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3922 for (i = 0; i < num_fx; i++) {
3923 err = add_fx_switch(codec, ca0132_effects[i].nid,
3924 ca0132_effects[i].name,
3925 ca0132_effects[i].direct);
3926 if (err < 0)
3927 return err;
3928 }
3929
3930 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
3931 if (err < 0)
3932 return err;
3933
3934 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
3935 if (err < 0)
3936 return err;
3937
3938 add_voicefx(codec);
3939
3940#ifdef ENABLE_TUNING_CONTROLS
3941 add_tuning_ctls(codec);
3942#endif
3943
3944 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3945 if (err < 0)
3946 return err;
3947
3948 if (spec->dig_out) {
3949 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
3950 spec->dig_out);
3951 if (err < 0)
3952 return err;
3953 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
3954 if (err < 0)
3955 return err;
3956 /* spec->multiout.share_spdif = 1; */
3957 }
3958
3959 if (spec->dig_in) {
3960 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
3961 if (err < 0)
3962 return err;
3963 }
3964 return 0;
3965}
3966
a7e76271 3967/*
e90f29e4 3968 * PCM
a7e76271
IM
3969 */
3970static struct hda_pcm_stream ca0132_pcm_analog_playback = {
3971 .substreams = 1,
3972 .channels_min = 2,
825315bc 3973 .channels_max = 6,
a7e76271 3974 .ops = {
a7e76271
IM
3975 .prepare = ca0132_playback_pcm_prepare,
3976 .cleanup = ca0132_playback_pcm_cleanup
3977 },
3978};
3979
3980static struct hda_pcm_stream ca0132_pcm_analog_capture = {
3981 .substreams = 1,
3982 .channels_min = 2,
3983 .channels_max = 2,
825315bc
IM
3984 .ops = {
3985 .prepare = ca0132_capture_pcm_prepare,
3986 .cleanup = ca0132_capture_pcm_cleanup
3987 },
a7e76271
IM
3988};
3989
3990static struct hda_pcm_stream ca0132_pcm_digital_playback = {
3991 .substreams = 1,
3992 .channels_min = 2,
3993 .channels_max = 2,
3994 .ops = {
3995 .open = ca0132_dig_playback_pcm_open,
3996 .close = ca0132_dig_playback_pcm_close,
3997 .prepare = ca0132_dig_playback_pcm_prepare,
3998 .cleanup = ca0132_dig_playback_pcm_cleanup
3999 },
4000};
4001
4002static struct hda_pcm_stream ca0132_pcm_digital_capture = {
4003 .substreams = 1,
4004 .channels_min = 2,
4005 .channels_max = 2,
4006};
4007
4008static int ca0132_build_pcms(struct hda_codec *codec)
95c6e9cb
IM
4009{
4010 struct ca0132_spec *spec = codec->spec;
a7e76271
IM
4011 struct hda_pcm *info = spec->pcm_rec;
4012
4013 codec->pcm_info = info;
4014 codec->num_pcms = 0;
4015
4016 info->name = "CA0132 Analog";
4017 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
4018 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
4019 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4020 spec->multiout.max_channels;
4021 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
825315bc 4022 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
a7e76271
IM
4023 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
4024 codec->num_pcms++;
4025
825315bc
IM
4026 info++;
4027 info->name = "CA0132 Analog Mic-In2";
4028 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4029 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4030 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
4031 codec->num_pcms++;
4032
4033 info++;
4034 info->name = "CA0132 What U Hear";
4035 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4036 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4037 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
4038 codec->num_pcms++;
4039
a7e76271
IM
4040 if (!spec->dig_out && !spec->dig_in)
4041 return 0;
4042
4043 info++;
4044 info->name = "CA0132 Digital";
4045 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4046 if (spec->dig_out) {
4047 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4048 ca0132_pcm_digital_playback;
4049 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
4050 }
4051 if (spec->dig_in) {
4052 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4053 ca0132_pcm_digital_capture;
4054 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
4055 }
4056 codec->num_pcms++;
95c6e9cb 4057
a7e76271 4058 return 0;
95c6e9cb
IM
4059}
4060
441aa6a0
IM
4061static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
4062{
4063 if (pin) {
a0c041cb 4064 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
441aa6a0
IM
4065 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
4066 snd_hda_codec_write(codec, pin, 0,
4067 AC_VERB_SET_AMP_GAIN_MUTE,
4068 AMP_OUT_UNMUTE);
4069 }
4070 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
4071 snd_hda_codec_write(codec, dac, 0,
4072 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
4073}
4074
4075static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
4076{
4077 if (pin) {
a0c041cb 4078 snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
441aa6a0
IM
4079 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
4080 snd_hda_codec_write(codec, pin, 0,
4081 AC_VERB_SET_AMP_GAIN_MUTE,
4082 AMP_IN_UNMUTE(0));
4083 }
4084 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
4085 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4086 AMP_IN_UNMUTE(0));
4087
4088 /* init to 0 dB and unmute. */
4089 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4090 HDA_AMP_VOLMASK, 0x5a);
4091 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4092 HDA_AMP_MUTE, 0);
4093 }
4094}
4095
a73d511c
IM
4096static void ca0132_init_unsol(struct hda_codec *codec)
4097{
4098 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
4099 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
4100}
4101
5aaca44d
IM
4102static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
4103{
4104 unsigned int caps;
4105
4106 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
4107 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
4108 snd_hda_override_amp_caps(codec, nid, dir, caps);
4109}
4110
4111/*
4112 * Switch between Digital built-in mic and analog mic.
4113 */
4114static void ca0132_set_dmic(struct hda_codec *codec, int enable)
4115{
4116 struct ca0132_spec *spec = codec->spec;
4117 unsigned int tmp;
4118 u8 val;
4119 unsigned int oldval;
4120
4121 snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
4122
4123 oldval = stop_mic1(codec);
4124 ca0132_set_vipsource(codec, 0);
4125 if (enable) {
4126 /* set DMic input as 2-ch */
4127 tmp = FLOAT_TWO;
4128 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4129
4130 val = spec->dmic_ctl;
4131 val |= 0x80;
4132 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4133 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4134
4135 if (!(spec->dmic_ctl & 0x20))
4136 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
4137 } else {
4138 /* set AMic input as mono */
4139 tmp = FLOAT_ONE;
4140 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4141
4142 val = spec->dmic_ctl;
4143 /* clear bit7 and bit5 to disable dmic */
4144 val &= 0x5f;
4145 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4146 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4147
4148 if (!(spec->dmic_ctl & 0x20))
4149 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
4150 }
4151 ca0132_set_vipsource(codec, 1);
4152 resume_mic1(codec, oldval);
4153}
4154
4155/*
4156 * Initialization for Digital Mic.
4157 */
4158static void ca0132_init_dmic(struct hda_codec *codec)
4159{
4160 struct ca0132_spec *spec = codec->spec;
4161 u8 val;
4162
4163 /* Setup Digital Mic here, but don't enable.
4164 * Enable based on jack detect.
4165 */
4166
4167 /* MCLK uses MPIO1, set to enable.
4168 * Bit 2-0: MPIO select
4169 * Bit 3: set to disable
4170 * Bit 7-4: reserved
4171 */
4172 val = 0x01;
4173 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4174 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
4175
4176 /* Data1 uses MPIO3. Data2 not use
4177 * Bit 2-0: Data1 MPIO select
4178 * Bit 3: set disable Data1
4179 * Bit 6-4: Data2 MPIO select
4180 * Bit 7: set disable Data2
4181 */
4182 val = 0x83;
4183 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4184 VENDOR_CHIPIO_DMIC_PIN_SET, val);
4185
4186 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
4187 * Bit 3-0: Channel mask
4188 * Bit 4: set for 48KHz, clear for 32KHz
4189 * Bit 5: mode
4190 * Bit 6: set to select Data2, clear for Data1
4191 * Bit 7: set to enable DMic, clear for AMic
4192 */
4193 val = 0x23;
4194 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
4195 spec->dmic_ctl = val;
4196 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4197 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4198}
4199
4200/*
4201 * Initialization for Analog Mic 2
4202 */
4203static void ca0132_init_analog_mic2(struct hda_codec *codec)
4204{
4205 struct ca0132_spec *spec = codec->spec;
4206
4207 mutex_lock(&spec->chipio_mutex);
4208 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4209 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
4210 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4211 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4212 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4213 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4214 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4215 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
4216 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4217 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4218 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4219 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4220 mutex_unlock(&spec->chipio_mutex);
4221}
4222
4223static void ca0132_refresh_widget_caps(struct hda_codec *codec)
4224{
4225 struct ca0132_spec *spec = codec->spec;
4226 int i;
4227 hda_nid_t nid;
4228
4229 snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
4230 nid = codec->start_nid;
4231 for (i = 0; i < codec->num_nodes; i++, nid++)
4232 codec->wcaps[i] = snd_hda_param_read(codec, nid,
4233 AC_PAR_AUDIO_WIDGET_CAP);
4234
4235 for (i = 0; i < spec->multiout.num_dacs; i++)
4236 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
4237
4238 for (i = 0; i < spec->num_outputs; i++)
4239 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
4240
4241 for (i = 0; i < spec->num_inputs; i++) {
4242 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
4243 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
4244 }
4245}
4246
4247/*
4248 * Setup default parameters for DSP
4249 */
4250static void ca0132_setup_defaults(struct hda_codec *codec)
4251{
4252 unsigned int tmp;
4253 int num_fx;
4254 int idx, i;
4255
4256 if (!dspload_is_loaded(codec))
4257 return;
4258
4259 /* out, in effects + voicefx */
4260 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
4261 for (idx = 0; idx < num_fx; idx++) {
4262 for (i = 0; i <= ca0132_effects[idx].params; i++) {
4263 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
4264 ca0132_effects[idx].reqs[i],
4265 ca0132_effects[idx].def_vals[i]);
4266 }
4267 }
4268
4269 /*remove DSP headroom*/
4270 tmp = FLOAT_ZERO;
4271 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
4272
4273 /*set speaker EQ bypass attenuation*/
4274 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
4275
4276 /* set AMic1 and AMic2 as mono mic */
4277 tmp = FLOAT_ONE;
4278 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4279 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
4280
4281 /* set AMic1 as CrystalVoice input */
4282 tmp = FLOAT_ONE;
4283 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4284
4285 /* set WUH source */
4286 tmp = FLOAT_TWO;
4287 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
4288}
4289
4290/*
4291 * Initialization of flags in chip
4292 */
4293static void ca0132_init_flags(struct hda_codec *codec)
4294{
4295 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4296 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4297 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4298 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4299 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4300 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4301}
4302
4303/*
4304 * Initialization of parameters in chip
4305 */
4306static void ca0132_init_params(struct hda_codec *codec)
4307{
4308 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4309 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4310}
95c6e9cb 4311
e90f29e4
IM
4312static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4313{
4314 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4315 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4316 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4317 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4318 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4319 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4320
406261ce
IM
4321 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4322 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
e90f29e4
IM
4323 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4324}
4325
4326static bool ca0132_download_dsp_images(struct hda_codec *codec)
4327{
4328 bool dsp_loaded = false;
4329 const struct dsp_image_seg *dsp_os_image;
15e4ba66 4330 const struct firmware *fw_entry;
e90f29e4 4331
15e4ba66 4332 if (request_firmware(&fw_entry, EFX_FILE, codec->bus->card->dev) != 0)
e90f29e4
IM
4333 return false;
4334
15e4ba66 4335 dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
e90f29e4
IM
4336 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4337 dsp_loaded = dspload_wait_loaded(codec);
4338
15e4ba66
TI
4339 release_firmware(fw_entry);
4340
4341
e90f29e4
IM
4342 return dsp_loaded;
4343}
4344
4345static void ca0132_download_dsp(struct hda_codec *codec)
4346{
4347 struct ca0132_spec *spec = codec->spec;
4348
4349 spec->dsp_state = DSP_DOWNLOAD_INIT;
4350
4351 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4352 chipio_enable_clocks(codec);
4353 spec->dsp_state = DSP_DOWNLOADING;
4354 if (!ca0132_download_dsp_images(codec))
4355 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4356 else
4357 spec->dsp_state = DSP_DOWNLOADED;
4358 }
4359
4360 if (spec->dsp_state == DSP_DOWNLOADED)
4361 ca0132_set_dsp_msr(codec, true);
4362}
4363
e90f29e4
IM
4364static void ca0132_process_dsp_response(struct hda_codec *codec)
4365{
4366 struct ca0132_spec *spec = codec->spec;
4367
4368 snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
4369 if (spec->wait_scp) {
4370 if (dspio_get_response_data(codec) >= 0)
4371 spec->wait_scp = 0;
4372 }
4373
4374 dspio_clear_response_queue(codec);
4375}
4376
4377static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4378{
4379 snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
4380
4381
4382 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4383 ca0132_process_dsp_response(codec);
4384 } else {
4385 res = snd_hda_jack_get_action(codec,
4386 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4387
4388 snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
4389
4390 switch (res) {
4391 case UNSOL_TAG_HP:
4392 ca0132_select_out(codec);
4393 snd_hda_jack_report_sync(codec);
4394 break;
4395 case UNSOL_TAG_AMIC1:
4396 ca0132_select_mic(codec);
4397 snd_hda_jack_report_sync(codec);
4398 break;
4399 default:
4400 break;
4401 }
4402 }
4403}
4404
5aaca44d
IM
4405/*
4406 * Verbs tables.
4407 */
4408
4409/* Sends before DSP download. */
4410static struct hda_verb ca0132_base_init_verbs[] = {
4411 /*enable ct extension*/
4412 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4413 /*enable DSP node unsol, needed for DSP download*/
4414 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4415 {}
4416};
4417
4418/* Send at exit. */
4419static struct hda_verb ca0132_base_exit_verbs[] = {
4420 /*set afg to D3*/
4421 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4422 /*disable ct extension*/
4423 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4424 {}
4425};
4426
4427/* Other verbs tables. Sends after DSP download. */
4428static struct hda_verb ca0132_init_verbs0[] = {
4429 /* chip init verbs */
4430 {0x15, 0x70D, 0xF0},
4431 {0x15, 0x70E, 0xFE},
4432 {0x15, 0x707, 0x75},
4433 {0x15, 0x707, 0xD3},
4434 {0x15, 0x707, 0x09},
4435 {0x15, 0x707, 0x53},
4436 {0x15, 0x707, 0xD4},
4437 {0x15, 0x707, 0xEF},
4438 {0x15, 0x707, 0x75},
4439 {0x15, 0x707, 0xD3},
4440 {0x15, 0x707, 0x09},
4441 {0x15, 0x707, 0x02},
4442 {0x15, 0x707, 0x37},
4443 {0x15, 0x707, 0x78},
4444 {0x15, 0x53C, 0xCE},
4445 {0x15, 0x575, 0xC9},
4446 {0x15, 0x53D, 0xCE},
4447 {0x15, 0x5B7, 0xC9},
4448 {0x15, 0x70D, 0xE8},
4449 {0x15, 0x70E, 0xFE},
4450 {0x15, 0x707, 0x02},
4451 {0x15, 0x707, 0x68},
4452 {0x15, 0x707, 0x62},
4453 {0x15, 0x53A, 0xCE},
4454 {0x15, 0x546, 0xC9},
4455 {0x15, 0x53B, 0xCE},
4456 {0x15, 0x5E8, 0xC9},
4457 {0x15, 0x717, 0x0D},
4458 {0x15, 0x718, 0x20},
4459 {}
4460};
4461
4462static struct hda_verb ca0132_init_verbs1[] = {
4463 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4464 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4465 /* config EAPD */
4466 {0x0b, 0x78D, 0x00},
4467 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4468 /*{0x10, 0x78D, 0x02},*/
4469 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4470 {}
4471};
4472
95c6e9cb
IM
4473static void ca0132_init_chip(struct hda_codec *codec)
4474{
4475 struct ca0132_spec *spec = codec->spec;
5aaca44d
IM
4476 int num_fx;
4477 int i;
4478 unsigned int on;
95c6e9cb
IM
4479
4480 mutex_init(&spec->chipio_mutex);
5aaca44d
IM
4481
4482 spec->cur_out_type = SPEAKER_OUT;
4483 spec->cur_mic_type = DIGITAL_MIC;
4484 spec->cur_mic_boost = 0;
4485
4486 for (i = 0; i < VNODES_COUNT; i++) {
4487 spec->vnode_lvol[i] = 0x5a;
4488 spec->vnode_rvol[i] = 0x5a;
4489 spec->vnode_lswitch[i] = 0;
4490 spec->vnode_rswitch[i] = 0;
4491 }
4492
4493 /*
4494 * Default states for effects are in ca0132_effects[].
4495 */
4496 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4497 for (i = 0; i < num_fx; i++) {
4498 on = (unsigned int)ca0132_effects[i].reqs[0];
4499 spec->effects_switch[i] = on ? 1 : 0;
4500 }
4501
4502 spec->voicefx_val = 0;
4503 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4504 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4505
44f0c978
IM
4506#ifdef ENABLE_TUNING_CONTROLS
4507 ca0132_init_tuning_defaults(codec);
4508#endif
95c6e9cb
IM
4509}
4510
4511static void ca0132_exit_chip(struct hda_codec *codec)
4512{
4513 /* put any chip cleanup stuffs here. */
5aaca44d
IM
4514
4515 if (dspload_is_loaded(codec))
4516 dsp_reset(codec);
95c6e9cb
IM
4517}
4518
4519static int ca0132_init(struct hda_codec *codec)
4520{
4521 struct ca0132_spec *spec = codec->spec;
4522 struct auto_pin_cfg *cfg = &spec->autocfg;
4523 int i;
4524
5aaca44d
IM
4525 spec->dsp_state = DSP_DOWNLOAD_INIT;
4526 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4527
4528 snd_hda_power_up(codec);
4529
4530 ca0132_init_params(codec);
4531 ca0132_init_flags(codec);
4532 snd_hda_sequence_write(codec, spec->base_init_verbs);
01ef7dbf
IM
4533#ifdef CONFIG_SND_HDA_DSP_LOADER
4534 ca0132_download_dsp(codec);
4535#endif
5aaca44d
IM
4536 ca0132_refresh_widget_caps(codec);
4537 ca0132_setup_defaults(codec);
4538 ca0132_init_analog_mic2(codec);
4539 ca0132_init_dmic(codec);
4540
4541 for (i = 0; i < spec->num_outputs; i++)
4542 init_output(codec, spec->out_pins[i], spec->dacs[0]);
01ef7dbf 4543
95c6e9cb
IM
4544 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
4545
4546 for (i = 0; i < spec->num_inputs; i++)
4547 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4548
4549 init_input(codec, cfg->dig_in_pin, spec->dig_in);
4550
5aaca44d
IM
4551 for (i = 0; i < spec->num_init_verbs; i++)
4552 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4553
a73d511c
IM
4554 ca0132_init_unsol(codec);
4555
5aaca44d
IM
4556 ca0132_select_out(codec);
4557 ca0132_select_mic(codec);
4558
a73d511c
IM
4559 snd_hda_jack_report_sync(codec);
4560
5aaca44d 4561 snd_hda_power_down(codec);
95c6e9cb
IM
4562
4563 return 0;
4564}
4565
95c6e9cb
IM
4566static void ca0132_free(struct hda_codec *codec)
4567{
5aaca44d
IM
4568 struct ca0132_spec *spec = codec->spec;
4569
4570 snd_hda_power_up(codec);
4571 snd_hda_sequence_write(codec, spec->base_exit_verbs);
95c6e9cb 4572 ca0132_exit_chip(codec);
5aaca44d 4573 snd_hda_power_down(codec);
95c6e9cb
IM
4574 kfree(codec->spec);
4575}
4576
4577static struct hda_codec_ops ca0132_patch_ops = {
4578 .build_controls = ca0132_build_controls,
4579 .build_pcms = ca0132_build_pcms,
4580 .init = ca0132_init,
4581 .free = ca0132_free,
a73d511c 4582 .unsol_event = ca0132_unsol_event,
95c6e9cb
IM
4583};
4584
441aa6a0
IM
4585static void ca0132_config(struct hda_codec *codec)
4586{
4587 struct ca0132_spec *spec = codec->spec;
4588 struct auto_pin_cfg *cfg = &spec->autocfg;
4589
4590 spec->dacs[0] = 0x2;
4591 spec->dacs[1] = 0x3;
4592 spec->dacs[2] = 0x4;
4593
4594 spec->multiout.dac_nids = spec->dacs;
4595 spec->multiout.num_dacs = 3;
4596 spec->multiout.max_channels = 2;
4597
4598 spec->num_outputs = 2;
4599 spec->out_pins[0] = 0x0b; /* speaker out */
4600 spec->out_pins[1] = 0x10; /* headphone out */
4601 spec->shared_out_nid = 0x2;
4602
4603 spec->num_inputs = 3;
4604 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4605 spec->adcs[1] = 0x8; /* analog mic2 */
4606 spec->adcs[2] = 0xa; /* what u hear */
4607 spec->shared_mic_nid = 0x7;
4608
4609 spec->input_pins[0] = 0x12;
4610 spec->input_pins[1] = 0x11;
4611 spec->input_pins[2] = 0x13;
4612
4613 /* SPDIF I/O */
4614 spec->dig_out = 0x05;
4615 spec->multiout.dig_out_nid = spec->dig_out;
4616 cfg->dig_out_pins[0] = 0x0c;
4617 cfg->dig_outs = 1;
4618 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
4619 spec->dig_in = 0x09;
4620 cfg->dig_in_pin = 0x0e;
4621 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4622}
4623
95c6e9cb
IM
4624static int patch_ca0132(struct hda_codec *codec)
4625{
4626 struct ca0132_spec *spec;
a73d511c 4627 int err;
95c6e9cb
IM
4628
4629 snd_printdd("patch_ca0132\n");
4630
4631 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4632 if (!spec)
4633 return -ENOMEM;
4634 codec->spec = spec;
4635
a7e76271
IM
4636 spec->num_mixers = 1;
4637 spec->mixers[0] = ca0132_mixer;
4638
5aaca44d
IM
4639 spec->base_init_verbs = ca0132_base_init_verbs;
4640 spec->base_exit_verbs = ca0132_base_exit_verbs;
4641 spec->init_verbs[0] = ca0132_init_verbs0;
4642 spec->init_verbs[1] = ca0132_init_verbs1;
4643 spec->num_init_verbs = 2;
4644
95c6e9cb
IM
4645 ca0132_init_chip(codec);
4646
4647 ca0132_config(codec);
4648
a73d511c
IM
4649 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4650 if (err < 0)
4651 return err;
4652
95c6e9cb
IM
4653 codec->patch_ops = ca0132_patch_ops;
4654
4655 return 0;
4656}
4657
4658/*
4659 * patch entries
4660 */
4661static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4662 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4663 {} /* terminator */
4664};
4665
4666MODULE_ALIAS("snd-hda-codec-id:11020011");
4667
4668MODULE_LICENSE("GPL");
406261ce 4669MODULE_DESCRIPTION("Creative Sound Core3D codec");
95c6e9cb
IM
4670
4671static struct hda_codec_preset_list ca0132_list = {
4672 .preset = snd_hda_preset_ca0132,
4673 .owner = THIS_MODULE,
4674};
4675
4676static int __init patch_ca0132_init(void)
4677{
4678 return snd_hda_add_codec_preset(&ca0132_list);
4679}
4680
4681static void __exit patch_ca0132_exit(void)
4682{
4683 snd_hda_delete_codec_preset(&ca0132_list);
4684}
4685
4686module_init(patch_ca0132_init)
4687module_exit(patch_ca0132_exit)