Commit | Line | Data |
---|---|---|
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 | ||
78 | MODULE_FIRMWARE(EFX_FILE); | |
79 | ||
ef6b2ead IM |
80 | static char *dirstr[2] = { "Playback", "Capture" }; |
81 | ||
82 | enum { | |
83 | SPEAKER_OUT, | |
84 | HEADPHONE_OUT | |
85 | }; | |
86 | ||
87 | enum { | |
88 | DIGITAL_MIC, | |
89 | LINE_MIC_IN | |
90 | }; | |
91 | ||
92 | enum { | |
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 | ||
132 | struct 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 | ||
146 | static 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 | ||
245 | enum { | |
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 | ||
263 | struct 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 | ||
273 | static 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 | ||
376 | struct 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 | ||
383 | struct ct_voicefx_preset { | |
384 | char *name; /*preset name*/ | |
385 | unsigned int vals[VOICEFX_MAX_PARAM_COUNT]; | |
386 | }; | |
387 | ||
388 | struct 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 | ||
395 | struct 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 |
468 | enum 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 | */ | |
530 | enum 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 | 588 | enum 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 | */ | |
629 | enum 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 | */ | |
643 | enum 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 | */ | |
653 | enum 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 |
676 | enum 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 | ||
693 | struct 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 | */ | |
750 | unsigned 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 | ||
760 | static 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 | ||
767 | static 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 */ | |
779 | static 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 | */ | |
799 | static 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 |
826 | static 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 |
850 | static 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 | */ | |
871 | static 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 | */ | |
902 | static 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 | ||
919 | exit: | |
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 |
928 | static 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); | |
942 | error: | |
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 | */ | |
952 | static 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 | ||
969 | exit: | |
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 |
977 | static 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 |
993 | static 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 |
1020 | static 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 |
1031 | static 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 | */ | |
1054 | static int dspio_send(struct hda_codec *codec, unsigned int reg, | |
1055 | unsigned int data) | |
1056 | { | |
1057 | unsigned int res; | |
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 |
1073 | static 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 |
1091 | static 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); | |
1112 | error: | |
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 |
1122 | static 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 |
1142 | static 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 | ||
1161 | static 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 |
1198 | static inline unsigned int |
1199 | make_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 |
1221 | static inline void |
1222 | extract_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 */ | |
1249 | struct scp_msg { | |
1250 | unsigned int hdr; | |
1251 | unsigned int data[SCP_MAX_DATA_WORDS]; | |
1252 | }; | |
1253 | ||
a73d511c IM |
1254 | static 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 | ||
1265 | static 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 |
1289 | static 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 |
1379 | static 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 | */ | |
1458 | static 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 | ||
1464 | static 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 |
1473 | static 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 |
1501 | static 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 | */ |
1525 | static 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 |
1560 | static 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 |
1582 | static 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 |
1603 | static 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 | ||
1613 | static 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 |
1726 | static 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 |
1817 | static 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, ®); | |
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 |
1853 | static 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, ®); | |
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 |
1896 | static 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 |
1937 | static 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 |
1957 | static 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 |
1978 | static 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 | */ | |
2004 | static 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 | */ | |
2023 | struct 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 | ||
2031 | enum dma_state { | |
2032 | DMA_STATE_STOP = 0, | |
2033 | DMA_STATE_RUN = 1 | |
2034 | }; | |
2035 | ||
2036 | static 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 |
2058 | static 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 | ||
2077 | static 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 |
2098 | static unsigned int dma_get_buffer_size(struct dma_engine *dma) |
2099 | { | |
2100 | return dma->dmab->bytes; | |
2101 | } | |
95c6e9cb | 2102 | |
01ef7dbf IM |
2103 | static unsigned char *dma_get_buffer_addr(struct dma_engine *dma) |
2104 | { | |
2105 | return dma->dmab->area; | |
2106 | } | |
95c6e9cb | 2107 | |
01ef7dbf IM |
2108 | static 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 |
2116 | static 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 | 2124 | static 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 |
2131 | struct dsp_image_seg { |
2132 | u32 magic; | |
2133 | u32 chip_addr; | |
2134 | u32 count; | |
2135 | u32 data[0]; | |
2136 | }; | |
2137 | ||
2138 | static const u32 g_magic_value = 0x4c46584d; | |
2139 | static const u32 g_chip_addr_magic_value = 0xFFFFFF01; | |
2140 | ||
2141 | static 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 |
2146 | static 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 |
2151 | static bool is_last(const struct dsp_image_seg *p) |
2152 | { | |
2153 | return p->count == 0; | |
2154 | } | |
95c6e9cb | 2155 | |
01ef7dbf IM |
2156 | static size_t dsp_sizeof(const struct dsp_image_seg *p) |
2157 | { | |
2158 | return sizeof(*p) + p->count*sizeof(u32); | |
2159 | } | |
2160 | ||
2161 | static 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 |
2177 | static 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 |
2218 | static 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) | |
2328 | return -EIO; | |
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 |
2399 | static 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 | 2507 | exit: |
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 | 2522 | static 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 |
2551 | static 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 | ||
c3b4eea2 IM |
2605 | static const struct firmware *fw_efx; |
2606 | ||
2607 | static int request_firmware_cached(const struct firmware **firmware_p, | |
2608 | const char *name, struct device *device) | |
2609 | { | |
2610 | if (*firmware_p) | |
2611 | return 0; /* already loaded */ | |
2612 | return request_firmware(firmware_p, name, device); | |
2613 | } | |
2614 | ||
2615 | static void release_cached_firmware(void) | |
2616 | { | |
2617 | if (fw_efx) { | |
2618 | release_firmware(fw_efx); | |
2619 | fw_efx = NULL; | |
2620 | } | |
2621 | } | |
2622 | ||
01ef7dbf IM |
2623 | static bool dspload_is_loaded(struct hda_codec *codec) |
2624 | { | |
2625 | unsigned int data = 0; | |
2626 | int status = 0; | |
2627 | ||
2628 | status = chipio_read(codec, 0x40004, &data); | |
2629 | if ((status < 0) || (data != 1)) | |
2630 | return false; | |
2631 | ||
2632 | return true; | |
2633 | } | |
2634 | ||
2635 | static bool dspload_wait_loaded(struct hda_codec *codec) | |
2636 | { | |
2637 | int retry = 100; | |
2638 | ||
2639 | do { | |
2640 | msleep(20); | |
2641 | if (dspload_is_loaded(codec)) { | |
2642 | pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n"); | |
2643 | return true; | |
2644 | } | |
2645 | } while (--retry); | |
2646 | ||
2647 | pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n"); | |
2648 | return false; | |
95c6e9cb IM |
2649 | } |
2650 | ||
2651 | /* | |
825315bc | 2652 | * PCM stuffs |
95c6e9cb | 2653 | */ |
825315bc IM |
2654 | static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid, |
2655 | u32 stream_tag, | |
2656 | int channel_id, int format) | |
95c6e9cb | 2657 | { |
825315bc IM |
2658 | unsigned int oldval, newval; |
2659 | ||
2660 | if (!nid) | |
2661 | return; | |
2662 | ||
2663 | snd_printdd( | |
2664 | "ca0132_setup_stream: NID=0x%x, stream=0x%x, " | |
2665 | "channel=%d, format=0x%x\n", | |
2666 | nid, stream_tag, channel_id, format); | |
2667 | ||
2668 | /* update the format-id if changed */ | |
2669 | oldval = snd_hda_codec_read(codec, nid, 0, | |
2670 | AC_VERB_GET_STREAM_FORMAT, | |
2671 | 0); | |
2672 | if (oldval != format) { | |
2673 | msleep(20); | |
2674 | snd_hda_codec_write(codec, nid, 0, | |
2675 | AC_VERB_SET_STREAM_FORMAT, | |
2676 | format); | |
2677 | } | |
2678 | ||
2679 | oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); | |
2680 | newval = (stream_tag << 4) | channel_id; | |
2681 | if (oldval != newval) { | |
2682 | snd_hda_codec_write(codec, nid, 0, | |
2683 | AC_VERB_SET_CHANNEL_STREAMID, | |
2684 | newval); | |
2685 | } | |
2686 | } | |
2687 | ||
2688 | static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid) | |
2689 | { | |
2690 | unsigned int val; | |
2691 | ||
2692 | if (!nid) | |
2693 | return; | |
2694 | ||
2695 | snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid); | |
2696 | ||
2697 | val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); | |
2698 | if (!val) | |
2699 | return; | |
2700 | ||
2701 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0); | |
2702 | snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); | |
95c6e9cb IM |
2703 | } |
2704 | ||
825315bc IM |
2705 | /* |
2706 | * PCM callbacks | |
2707 | */ | |
95c6e9cb IM |
2708 | static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
2709 | struct hda_codec *codec, | |
2710 | unsigned int stream_tag, | |
2711 | unsigned int format, | |
2712 | struct snd_pcm_substream *substream) | |
2713 | { | |
2714 | struct ca0132_spec *spec = codec->spec; | |
825315bc IM |
2715 | |
2716 | ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format); | |
2717 | ||
2718 | return 0; | |
95c6e9cb IM |
2719 | } |
2720 | ||
2721 | static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, | |
2722 | struct hda_codec *codec, | |
2723 | struct snd_pcm_substream *substream) | |
2724 | { | |
2725 | struct ca0132_spec *spec = codec->spec; | |
825315bc IM |
2726 | |
2727 | if (spec->dsp_state == DSP_DOWNLOADING) | |
2728 | return 0; | |
2729 | ||
2730 | /*If Playback effects are on, allow stream some time to flush | |
2731 | *effects tail*/ | |
2732 | if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) | |
2733 | msleep(50); | |
2734 | ||
2735 | ca0132_cleanup_stream(codec, spec->dacs[0]); | |
2736 | ||
2737 | return 0; | |
95c6e9cb IM |
2738 | } |
2739 | ||
2740 | /* | |
2741 | * Digital out | |
2742 | */ | |
27ebeb0b TI |
2743 | static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo, |
2744 | struct hda_codec *codec, | |
2745 | struct snd_pcm_substream *substream) | |
95c6e9cb IM |
2746 | { |
2747 | struct ca0132_spec *spec = codec->spec; | |
27ebeb0b | 2748 | return snd_hda_multi_out_dig_open(codec, &spec->multiout); |
95c6e9cb IM |
2749 | } |
2750 | ||
27ebeb0b | 2751 | static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo, |
95c6e9cb IM |
2752 | struct hda_codec *codec, |
2753 | unsigned int stream_tag, | |
2754 | unsigned int format, | |
2755 | struct snd_pcm_substream *substream) | |
2756 | { | |
2757 | struct ca0132_spec *spec = codec->spec; | |
27ebeb0b TI |
2758 | return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, |
2759 | stream_tag, format, substream); | |
95c6e9cb IM |
2760 | } |
2761 | ||
27ebeb0b | 2762 | static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, |
95c6e9cb | 2763 | struct hda_codec *codec, |
95c6e9cb IM |
2764 | struct snd_pcm_substream *substream) |
2765 | { | |
2766 | struct ca0132_spec *spec = codec->spec; | |
27ebeb0b | 2767 | return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout); |
95c6e9cb IM |
2768 | } |
2769 | ||
27ebeb0b TI |
2770 | static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo, |
2771 | struct hda_codec *codec, | |
2772 | struct snd_pcm_substream *substream) | |
95c6e9cb IM |
2773 | { |
2774 | struct ca0132_spec *spec = codec->spec; | |
27ebeb0b | 2775 | return snd_hda_multi_out_dig_close(codec, &spec->multiout); |
95c6e9cb IM |
2776 | } |
2777 | ||
825315bc IM |
2778 | /* |
2779 | * Analog capture | |
2780 | */ | |
2781 | static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo, | |
2782 | struct hda_codec *codec, | |
2783 | unsigned int stream_tag, | |
2784 | unsigned int format, | |
2785 | struct snd_pcm_substream *substream) | |
2786 | { | |
2787 | struct ca0132_spec *spec = codec->spec; | |
2788 | ||
2789 | ca0132_setup_stream(codec, spec->adcs[substream->number], | |
2790 | stream_tag, 0, format); | |
2791 | ||
2792 | return 0; | |
2793 | } | |
2794 | ||
2795 | static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, | |
2796 | struct hda_codec *codec, | |
2797 | struct snd_pcm_substream *substream) | |
2798 | { | |
2799 | struct ca0132_spec *spec = codec->spec; | |
2800 | ||
2801 | if (spec->dsp_state == DSP_DOWNLOADING) | |
2802 | return 0; | |
2803 | ||
2804 | ca0132_cleanup_stream(codec, hinfo->nid); | |
2805 | return 0; | |
2806 | } | |
2807 | ||
e90f29e4 IM |
2808 | /* |
2809 | * Controls stuffs. | |
2810 | */ | |
2811 | ||
2812 | /* | |
2813 | * Mixer controls helpers. | |
2814 | */ | |
2815 | #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \ | |
2816 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ | |
2817 | .name = xname, \ | |
2818 | .subdevice = HDA_SUBDEV_AMP_FLAG, \ | |
2819 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \ | |
2820 | SNDRV_CTL_ELEM_ACCESS_TLV_READ | \ | |
2821 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \ | |
2822 | .info = ca0132_volume_info, \ | |
2823 | .get = ca0132_volume_get, \ | |
2824 | .put = ca0132_volume_put, \ | |
2825 | .tlv = { .c = ca0132_volume_tlv }, \ | |
2826 | .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) } | |
2827 | ||
2828 | #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \ | |
2829 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ | |
2830 | .name = xname, \ | |
2831 | .subdevice = HDA_SUBDEV_AMP_FLAG, \ | |
2832 | .info = snd_hda_mixer_amp_switch_info, \ | |
2833 | .get = ca0132_switch_get, \ | |
2834 | .put = ca0132_switch_put, \ | |
2835 | .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) } | |
2836 | ||
2837 | /* stereo */ | |
2838 | #define CA0132_CODEC_VOL(xname, nid, dir) \ | |
2839 | CA0132_CODEC_VOL_MONO(xname, nid, 3, dir) | |
2840 | #define CA0132_CODEC_MUTE(xname, nid, dir) \ | |
2841 | CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir) | |
2842 | ||
44f0c978 IM |
2843 | /* The followings are for tuning of products */ |
2844 | #ifdef ENABLE_TUNING_CONTROLS | |
2845 | ||
2846 | static unsigned int voice_focus_vals_lookup[] = { | |
2847 | 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000, | |
2848 | 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000, | |
2849 | 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000, | |
2850 | 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000, | |
2851 | 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000, | |
2852 | 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000, | |
2853 | 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000, | |
2854 | 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000, | |
2855 | 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000, | |
2856 | 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000, | |
2857 | 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000, | |
2858 | 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000, | |
2859 | 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000, | |
2860 | 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000, | |
2861 | 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000, | |
2862 | 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000, | |
2863 | 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000, | |
2864 | 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000, | |
2865 | 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000, | |
2866 | 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000, | |
2867 | 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000, | |
2868 | 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000, | |
2869 | 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000, | |
2870 | 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000, | |
2871 | 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000, | |
2872 | 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000, | |
2873 | 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000 | |
2874 | }; | |
2875 | ||
2876 | static unsigned int mic_svm_vals_lookup[] = { | |
2877 | 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD, | |
2878 | 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE, | |
2879 | 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B, | |
2880 | 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F, | |
2881 | 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1, | |
2882 | 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333, | |
2883 | 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85, | |
2884 | 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7, | |
2885 | 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14, | |
2886 | 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D, | |
2887 | 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666, | |
2888 | 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F, | |
2889 | 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8, | |
2890 | 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1, | |
2891 | 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A, | |
2892 | 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333, | |
2893 | 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000 | |
2894 | }; | |
2895 | ||
2896 | static unsigned int equalizer_vals_lookup[] = { | |
2897 | 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000, | |
2898 | 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000, | |
2899 | 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000, | |
2900 | 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000, | |
2901 | 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000, | |
2902 | 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000, | |
2903 | 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000, | |
2904 | 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, | |
2905 | 0x41C00000 | |
2906 | }; | |
2907 | ||
2908 | static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid, | |
2909 | unsigned int *lookup, int idx) | |
2910 | { | |
2911 | int i = 0; | |
2912 | ||
2913 | for (i = 0; i < TUNING_CTLS_COUNT; i++) | |
2914 | if (nid == ca0132_tuning_ctls[i].nid) | |
2915 | break; | |
2916 | ||
2917 | snd_hda_power_up(codec); | |
2918 | dspio_set_param(codec, ca0132_tuning_ctls[i].mid, | |
2919 | ca0132_tuning_ctls[i].req, | |
2920 | &(lookup[idx]), sizeof(unsigned int)); | |
2921 | snd_hda_power_down(codec); | |
2922 | ||
2923 | return 1; | |
2924 | } | |
2925 | ||
2926 | static int tuning_ctl_get(struct snd_kcontrol *kcontrol, | |
2927 | struct snd_ctl_elem_value *ucontrol) | |
2928 | { | |
2929 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2930 | struct ca0132_spec *spec = codec->spec; | |
2931 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2932 | long *valp = ucontrol->value.integer.value; | |
2933 | int idx = nid - TUNING_CTL_START_NID; | |
2934 | ||
2935 | *valp = spec->cur_ctl_vals[idx]; | |
2936 | return 0; | |
2937 | } | |
2938 | ||
2939 | static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol, | |
2940 | struct snd_ctl_elem_info *uinfo) | |
2941 | { | |
2942 | int chs = get_amp_channels(kcontrol); | |
2943 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2944 | uinfo->count = chs == 3 ? 2 : 1; | |
2945 | uinfo->value.integer.min = 20; | |
2946 | uinfo->value.integer.max = 180; | |
2947 | uinfo->value.integer.step = 1; | |
2948 | ||
2949 | return 0; | |
2950 | } | |
2951 | ||
2952 | static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol, | |
2953 | struct snd_ctl_elem_value *ucontrol) | |
2954 | { | |
2955 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2956 | struct ca0132_spec *spec = codec->spec; | |
2957 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2958 | long *valp = ucontrol->value.integer.value; | |
2959 | int idx; | |
2960 | ||
2961 | idx = nid - TUNING_CTL_START_NID; | |
2962 | /* any change? */ | |
2963 | if (spec->cur_ctl_vals[idx] == *valp) | |
2964 | return 0; | |
2965 | ||
2966 | spec->cur_ctl_vals[idx] = *valp; | |
2967 | ||
2968 | idx = *valp - 20; | |
2969 | tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx); | |
2970 | ||
2971 | return 1; | |
2972 | } | |
2973 | ||
2974 | static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol, | |
2975 | struct snd_ctl_elem_info *uinfo) | |
2976 | { | |
2977 | int chs = get_amp_channels(kcontrol); | |
2978 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2979 | uinfo->count = chs == 3 ? 2 : 1; | |
2980 | uinfo->value.integer.min = 0; | |
2981 | uinfo->value.integer.max = 100; | |
2982 | uinfo->value.integer.step = 1; | |
2983 | ||
2984 | return 0; | |
2985 | } | |
2986 | ||
2987 | static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol, | |
2988 | struct snd_ctl_elem_value *ucontrol) | |
2989 | { | |
2990 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
2991 | struct ca0132_spec *spec = codec->spec; | |
2992 | hda_nid_t nid = get_amp_nid(kcontrol); | |
2993 | long *valp = ucontrol->value.integer.value; | |
2994 | int idx; | |
2995 | ||
2996 | idx = nid - TUNING_CTL_START_NID; | |
2997 | /* any change? */ | |
2998 | if (spec->cur_ctl_vals[idx] == *valp) | |
2999 | return 0; | |
3000 | ||
3001 | spec->cur_ctl_vals[idx] = *valp; | |
3002 | ||
3003 | idx = *valp; | |
3004 | tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx); | |
3005 | ||
3006 | return 0; | |
3007 | } | |
3008 | ||
3009 | static int equalizer_ctl_info(struct snd_kcontrol *kcontrol, | |
3010 | struct snd_ctl_elem_info *uinfo) | |
3011 | { | |
3012 | int chs = get_amp_channels(kcontrol); | |
3013 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
3014 | uinfo->count = chs == 3 ? 2 : 1; | |
3015 | uinfo->value.integer.min = 0; | |
3016 | uinfo->value.integer.max = 48; | |
3017 | uinfo->value.integer.step = 1; | |
3018 | ||
3019 | return 0; | |
3020 | } | |
3021 | ||
3022 | static int equalizer_ctl_put(struct snd_kcontrol *kcontrol, | |
3023 | struct snd_ctl_elem_value *ucontrol) | |
3024 | { | |
3025 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3026 | struct ca0132_spec *spec = codec->spec; | |
3027 | hda_nid_t nid = get_amp_nid(kcontrol); | |
3028 | long *valp = ucontrol->value.integer.value; | |
3029 | int idx; | |
3030 | ||
3031 | idx = nid - TUNING_CTL_START_NID; | |
3032 | /* any change? */ | |
3033 | if (spec->cur_ctl_vals[idx] == *valp) | |
3034 | return 0; | |
3035 | ||
3036 | spec->cur_ctl_vals[idx] = *valp; | |
3037 | ||
3038 | idx = *valp; | |
3039 | tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx); | |
3040 | ||
3041 | return 1; | |
3042 | } | |
3043 | ||
3044 | static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0); | |
3045 | static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0); | |
3046 | ||
3047 | static int add_tuning_control(struct hda_codec *codec, | |
3048 | hda_nid_t pnid, hda_nid_t nid, | |
3049 | const char *name, int dir) | |
3050 | { | |
3051 | char namestr[44]; | |
3052 | int type = dir ? HDA_INPUT : HDA_OUTPUT; | |
3053 | struct snd_kcontrol_new knew = | |
3054 | HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type); | |
3055 | ||
3056 | knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | | |
3057 | SNDRV_CTL_ELEM_ACCESS_TLV_READ; | |
3058 | knew.tlv.c = 0; | |
3059 | knew.tlv.p = 0; | |
3060 | switch (pnid) { | |
3061 | case VOICE_FOCUS: | |
3062 | knew.info = voice_focus_ctl_info; | |
3063 | knew.get = tuning_ctl_get; | |
3064 | knew.put = voice_focus_ctl_put; | |
3065 | knew.tlv.p = voice_focus_db_scale; | |
3066 | break; | |
3067 | case MIC_SVM: | |
3068 | knew.info = mic_svm_ctl_info; | |
3069 | knew.get = tuning_ctl_get; | |
3070 | knew.put = mic_svm_ctl_put; | |
3071 | break; | |
3072 | case EQUALIZER: | |
3073 | knew.info = equalizer_ctl_info; | |
3074 | knew.get = tuning_ctl_get; | |
3075 | knew.put = equalizer_ctl_put; | |
3076 | knew.tlv.p = eq_db_scale; | |
3077 | break; | |
3078 | default: | |
3079 | return 0; | |
3080 | } | |
3081 | knew.private_value = | |
3082 | HDA_COMPOSE_AMP_VAL(nid, 1, 0, type); | |
3083 | sprintf(namestr, "%s %s Volume", name, dirstr[dir]); | |
3084 | return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec)); | |
3085 | } | |
3086 | ||
3087 | static int add_tuning_ctls(struct hda_codec *codec) | |
3088 | { | |
3089 | int i; | |
3090 | int err; | |
3091 | ||
3092 | for (i = 0; i < TUNING_CTLS_COUNT; i++) { | |
3093 | err = add_tuning_control(codec, | |
3094 | ca0132_tuning_ctls[i].parent_nid, | |
3095 | ca0132_tuning_ctls[i].nid, | |
3096 | ca0132_tuning_ctls[i].name, | |
3097 | ca0132_tuning_ctls[i].direct); | |
3098 | if (err < 0) | |
3099 | return err; | |
3100 | } | |
3101 | ||
3102 | return 0; | |
3103 | } | |
3104 | ||
3105 | static void ca0132_init_tuning_defaults(struct hda_codec *codec) | |
3106 | { | |
3107 | struct ca0132_spec *spec = codec->spec; | |
3108 | int i; | |
3109 | ||
3110 | /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */ | |
3111 | spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10; | |
3112 | /* SVM level defaults to 0.74. */ | |
3113 | spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74; | |
3114 | ||
3115 | /* EQ defaults to 0dB. */ | |
3116 | for (i = 2; i < TUNING_CTLS_COUNT; i++) | |
3117 | spec->cur_ctl_vals[i] = 24; | |
3118 | } | |
3119 | #endif /*ENABLE_TUNING_CONTROLS*/ | |
3120 | ||
5aaca44d IM |
3121 | /* |
3122 | * Select the active output. | |
3123 | * If autodetect is enabled, output will be selected based on jack detection. | |
3124 | * If jack inserted, headphone will be selected, else built-in speakers | |
3125 | * If autodetect is disabled, output will be selected based on selection. | |
3126 | */ | |
3127 | static int ca0132_select_out(struct hda_codec *codec) | |
3128 | { | |
3129 | struct ca0132_spec *spec = codec->spec; | |
3130 | unsigned int pin_ctl; | |
3131 | int jack_present; | |
3132 | int auto_jack; | |
3133 | unsigned int tmp; | |
3134 | int err; | |
3135 | ||
3136 | snd_printdd(KERN_INFO "ca0132_select_out\n"); | |
3137 | ||
3138 | snd_hda_power_up(codec); | |
3139 | ||
3140 | auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID]; | |
3141 | ||
3142 | if (auto_jack) | |
3143 | jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]); | |
3144 | else | |
3145 | jack_present = | |
3146 | spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID]; | |
3147 | ||
3148 | if (jack_present) | |
3149 | spec->cur_out_type = HEADPHONE_OUT; | |
3150 | else | |
3151 | spec->cur_out_type = SPEAKER_OUT; | |
3152 | ||
3153 | if (spec->cur_out_type == SPEAKER_OUT) { | |
3154 | snd_printdd(KERN_INFO "ca0132_select_out speaker\n"); | |
3155 | /*speaker out config*/ | |
3156 | tmp = FLOAT_ONE; | |
3157 | err = dspio_set_uint_param(codec, 0x80, 0x04, tmp); | |
3158 | if (err < 0) | |
3159 | goto exit; | |
3160 | /*enable speaker EQ*/ | |
3161 | tmp = FLOAT_ONE; | |
3162 | err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp); | |
3163 | if (err < 0) | |
3164 | goto exit; | |
3165 | ||
3166 | /* Setup EAPD */ | |
3167 | snd_hda_codec_write(codec, spec->out_pins[1], 0, | |
3168 | VENDOR_CHIPIO_EAPD_SEL_SET, 0x02); | |
3169 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3170 | AC_VERB_SET_EAPD_BTLENABLE, 0x00); | |
3171 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3172 | VENDOR_CHIPIO_EAPD_SEL_SET, 0x00); | |
3173 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3174 | AC_VERB_SET_EAPD_BTLENABLE, 0x02); | |
3175 | ||
3176 | /* disable headphone node */ | |
3177 | pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0, | |
3178 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | |
3179 | snd_hda_codec_write(codec, spec->out_pins[1], 0, | |
3180 | AC_VERB_SET_PIN_WIDGET_CONTROL, | |
3181 | pin_ctl & 0xBF); | |
3182 | /* enable speaker node */ | |
3183 | pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0, | |
3184 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | |
3185 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3186 | AC_VERB_SET_PIN_WIDGET_CONTROL, | |
3187 | pin_ctl | 0x40); | |
3188 | } else { | |
3189 | snd_printdd(KERN_INFO "ca0132_select_out hp\n"); | |
3190 | /*headphone out config*/ | |
3191 | tmp = FLOAT_ZERO; | |
3192 | err = dspio_set_uint_param(codec, 0x80, 0x04, tmp); | |
3193 | if (err < 0) | |
3194 | goto exit; | |
3195 | /*disable speaker EQ*/ | |
3196 | tmp = FLOAT_ZERO; | |
3197 | err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp); | |
3198 | if (err < 0) | |
3199 | goto exit; | |
3200 | ||
3201 | /* Setup EAPD */ | |
3202 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3203 | VENDOR_CHIPIO_EAPD_SEL_SET, 0x00); | |
3204 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3205 | AC_VERB_SET_EAPD_BTLENABLE, 0x00); | |
3206 | snd_hda_codec_write(codec, spec->out_pins[1], 0, | |
3207 | VENDOR_CHIPIO_EAPD_SEL_SET, 0x02); | |
3208 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3209 | AC_VERB_SET_EAPD_BTLENABLE, 0x02); | |
3210 | ||
3211 | /* disable speaker*/ | |
3212 | pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0, | |
3213 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | |
3214 | snd_hda_codec_write(codec, spec->out_pins[0], 0, | |
3215 | AC_VERB_SET_PIN_WIDGET_CONTROL, | |
3216 | pin_ctl & 0xBF); | |
3217 | /* enable headphone*/ | |
3218 | pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0, | |
3219 | AC_VERB_GET_PIN_WIDGET_CONTROL, 0); | |
3220 | snd_hda_codec_write(codec, spec->out_pins[1], 0, | |
3221 | AC_VERB_SET_PIN_WIDGET_CONTROL, | |
3222 | pin_ctl | 0x40); | |
3223 | } | |
3224 | ||
3225 | exit: | |
3226 | snd_hda_power_down(codec); | |
3227 | ||
3228 | return err < 0 ? err : 0; | |
3229 | } | |
3230 | ||
3231 | static void ca0132_set_dmic(struct hda_codec *codec, int enable); | |
3232 | static int ca0132_mic_boost_set(struct hda_codec *codec, long val); | |
3233 | static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val); | |
3234 | ||
3235 | /* | |
3236 | * Select the active VIP source | |
3237 | */ | |
3238 | static int ca0132_set_vipsource(struct hda_codec *codec, int val) | |
3239 | { | |
3240 | struct ca0132_spec *spec = codec->spec; | |
3241 | unsigned int tmp; | |
3242 | ||
3243 | if (!dspload_is_loaded(codec)) | |
3244 | return 0; | |
3245 | ||
3246 | /* if CrystalVoice if off, vipsource should be 0 */ | |
3247 | if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] || | |
3248 | (val == 0)) { | |
3249 | chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0); | |
3250 | chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000); | |
3251 | chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000); | |
3252 | if (spec->cur_mic_type == DIGITAL_MIC) | |
3253 | tmp = FLOAT_TWO; | |
3254 | else | |
3255 | tmp = FLOAT_ONE; | |
3256 | dspio_set_uint_param(codec, 0x80, 0x00, tmp); | |
3257 | tmp = FLOAT_ZERO; | |
3258 | dspio_set_uint_param(codec, 0x80, 0x05, tmp); | |
3259 | } else { | |
3260 | chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000); | |
3261 | chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000); | |
3262 | if (spec->cur_mic_type == DIGITAL_MIC) | |
3263 | tmp = FLOAT_TWO; | |
3264 | else | |
3265 | tmp = FLOAT_ONE; | |
3266 | dspio_set_uint_param(codec, 0x80, 0x00, tmp); | |
3267 | tmp = FLOAT_ONE; | |
3268 | dspio_set_uint_param(codec, 0x80, 0x05, tmp); | |
3269 | msleep(20); | |
3270 | chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val); | |
3271 | } | |
3272 | ||
3273 | return 1; | |
3274 | } | |
3275 | ||
3276 | /* | |
3277 | * Select the active microphone. | |
3278 | * If autodetect is enabled, mic will be selected based on jack detection. | |
3279 | * If jack inserted, ext.mic will be selected, else built-in mic | |
3280 | * If autodetect is disabled, mic will be selected based on selection. | |
3281 | */ | |
3282 | static int ca0132_select_mic(struct hda_codec *codec) | |
3283 | { | |
3284 | struct ca0132_spec *spec = codec->spec; | |
3285 | int jack_present; | |
3286 | int auto_jack; | |
3287 | ||
3288 | snd_printdd(KERN_INFO "ca0132_select_mic\n"); | |
3289 | ||
3290 | snd_hda_power_up(codec); | |
3291 | ||
3292 | auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID]; | |
3293 | ||
3294 | if (auto_jack) | |
3295 | jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]); | |
3296 | else | |
3297 | jack_present = | |
3298 | spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID]; | |
3299 | ||
3300 | if (jack_present) | |
3301 | spec->cur_mic_type = LINE_MIC_IN; | |
3302 | else | |
3303 | spec->cur_mic_type = DIGITAL_MIC; | |
3304 | ||
3305 | if (spec->cur_mic_type == DIGITAL_MIC) { | |
3306 | /* enable digital Mic */ | |
3307 | chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000); | |
3308 | ca0132_set_dmic(codec, 1); | |
3309 | ca0132_mic_boost_set(codec, 0); | |
3310 | /* set voice focus */ | |
3311 | ca0132_effects_set(codec, VOICE_FOCUS, | |
3312 | spec->effects_switch | |
3313 | [VOICE_FOCUS - EFFECT_START_NID]); | |
3314 | } else { | |
3315 | /* disable digital Mic */ | |
3316 | chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000); | |
3317 | ca0132_set_dmic(codec, 0); | |
3318 | ca0132_mic_boost_set(codec, spec->cur_mic_boost); | |
3319 | /* disable voice focus */ | |
3320 | ca0132_effects_set(codec, VOICE_FOCUS, 0); | |
3321 | } | |
3322 | ||
3323 | snd_hda_power_down(codec); | |
3324 | ||
3325 | return 0; | |
3326 | } | |
3327 | ||
a7e76271 IM |
3328 | /* |
3329 | * Check if VNODE settings take effect immediately. | |
3330 | */ | |
3331 | static bool ca0132_is_vnode_effective(struct hda_codec *codec, | |
3332 | hda_nid_t vnid, | |
3333 | hda_nid_t *shared_nid) | |
3334 | { | |
3335 | struct ca0132_spec *spec = codec->spec; | |
3336 | hda_nid_t nid; | |
3337 | bool effective = false; | |
3338 | ||
3339 | switch (vnid) { | |
3340 | case VNID_SPK: | |
3341 | nid = spec->shared_out_nid; | |
3342 | effective = true; | |
3343 | break; | |
3344 | case VNID_MIC: | |
3345 | nid = spec->shared_mic_nid; | |
3346 | effective = true; | |
3347 | break; | |
3348 | default: | |
3349 | break; | |
3350 | } | |
3351 | ||
3352 | if (effective && shared_nid) | |
3353 | *shared_nid = nid; | |
3354 | ||
3355 | return effective; | |
3356 | } | |
3357 | ||
3358 | /* | |
3359 | * The following functions are control change helpers. | |
3360 | * They return 0 if no changed. Return 1 if changed. | |
3361 | */ | |
3362 | static int ca0132_voicefx_set(struct hda_codec *codec, int enable) | |
3363 | { | |
3364 | struct ca0132_spec *spec = codec->spec; | |
3365 | unsigned int tmp; | |
3366 | ||
3367 | /* based on CrystalVoice state to enable VoiceFX. */ | |
3368 | if (enable) { | |
3369 | tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ? | |
3370 | FLOAT_ONE : FLOAT_ZERO; | |
3371 | } else { | |
3372 | tmp = FLOAT_ZERO; | |
3373 | } | |
3374 | ||
3375 | dspio_set_uint_param(codec, ca0132_voicefx.mid, | |
3376 | ca0132_voicefx.reqs[0], tmp); | |
3377 | ||
3378 | return 1; | |
3379 | } | |
3380 | ||
5aaca44d IM |
3381 | /* |
3382 | * Set the effects parameters | |
3383 | */ | |
3384 | static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val) | |
3385 | { | |
3386 | struct ca0132_spec *spec = codec->spec; | |
3387 | unsigned int on; | |
3388 | int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT; | |
3389 | int err = 0; | |
3390 | int idx = nid - EFFECT_START_NID; | |
3391 | ||
3392 | if ((idx < 0) || (idx >= num_fx)) | |
3393 | return 0; /* no changed */ | |
3394 | ||
3395 | /* for out effect, qualify with PE */ | |
3396 | if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) { | |
3397 | /* if PE if off, turn off out effects. */ | |
3398 | if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) | |
3399 | val = 0; | |
3400 | } | |
3401 | ||
3402 | /* for in effect, qualify with CrystalVoice */ | |
3403 | if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) { | |
3404 | /* if CrystalVoice if off, turn off in effects. */ | |
3405 | if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]) | |
3406 | val = 0; | |
3407 | ||
3408 | /* Voice Focus applies to 2-ch Mic, Digital Mic */ | |
3409 | if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC)) | |
3410 | val = 0; | |
3411 | } | |
3412 | ||
3413 | snd_printdd(KERN_INFO, "ca0132_effect_set: nid=0x%x, val=%ld\n", | |
3414 | nid, val); | |
3415 | ||
3416 | on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE; | |
3417 | err = dspio_set_uint_param(codec, ca0132_effects[idx].mid, | |
3418 | ca0132_effects[idx].reqs[0], on); | |
3419 | ||
3420 | if (err < 0) | |
3421 | return 0; /* no changed */ | |
3422 | ||
3423 | return 1; | |
3424 | } | |
3425 | ||
a7e76271 IM |
3426 | /* |
3427 | * Turn on/off Playback Enhancements | |
3428 | */ | |
3429 | static int ca0132_pe_switch_set(struct hda_codec *codec) | |
3430 | { | |
3431 | struct ca0132_spec *spec = codec->spec; | |
3432 | hda_nid_t nid; | |
3433 | int i, ret = 0; | |
3434 | ||
3435 | snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n", | |
3436 | spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]); | |
3437 | ||
3438 | i = OUT_EFFECT_START_NID - EFFECT_START_NID; | |
3439 | nid = OUT_EFFECT_START_NID; | |
3440 | /* PE affects all out effects */ | |
3441 | for (; nid < OUT_EFFECT_END_NID; nid++, i++) | |
3442 | ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]); | |
3443 | ||
3444 | return ret; | |
3445 | } | |
3446 | ||
5aaca44d IM |
3447 | /* Check if Mic1 is streaming, if so, stop streaming */ |
3448 | static int stop_mic1(struct hda_codec *codec) | |
3449 | { | |
3450 | struct ca0132_spec *spec = codec->spec; | |
3451 | unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0, | |
3452 | AC_VERB_GET_CONV, 0); | |
3453 | if (oldval != 0) | |
3454 | snd_hda_codec_write(codec, spec->adcs[0], 0, | |
3455 | AC_VERB_SET_CHANNEL_STREAMID, | |
3456 | 0); | |
3457 | return oldval; | |
3458 | } | |
3459 | ||
3460 | /* Resume Mic1 streaming if it was stopped. */ | |
3461 | static void resume_mic1(struct hda_codec *codec, unsigned int oldval) | |
3462 | { | |
3463 | struct ca0132_spec *spec = codec->spec; | |
3464 | /* Restore the previous stream and channel */ | |
3465 | if (oldval != 0) | |
3466 | snd_hda_codec_write(codec, spec->adcs[0], 0, | |
3467 | AC_VERB_SET_CHANNEL_STREAMID, | |
3468 | oldval); | |
3469 | } | |
3470 | ||
3471 | /* | |
a7e76271 | 3472 | * Turn on/off CrystalVoice |
5aaca44d | 3473 | */ |
a7e76271 IM |
3474 | static int ca0132_cvoice_switch_set(struct hda_codec *codec) |
3475 | { | |
3476 | struct ca0132_spec *spec = codec->spec; | |
3477 | hda_nid_t nid; | |
3478 | int i, ret = 0; | |
3479 | unsigned int oldval; | |
3480 | ||
3481 | snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n", | |
3482 | spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]); | |
3483 | ||
3484 | i = IN_EFFECT_START_NID - EFFECT_START_NID; | |
3485 | nid = IN_EFFECT_START_NID; | |
3486 | /* CrystalVoice affects all in effects */ | |
3487 | for (; nid < IN_EFFECT_END_NID; nid++, i++) | |
3488 | ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]); | |
3489 | ||
3490 | /* including VoiceFX */ | |
3491 | ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0)); | |
3492 | ||
3493 | /* set correct vipsource */ | |
3494 | oldval = stop_mic1(codec); | |
3495 | ret |= ca0132_set_vipsource(codec, 1); | |
3496 | resume_mic1(codec, oldval); | |
3497 | return ret; | |
3498 | } | |
3499 | ||
5aaca44d IM |
3500 | static int ca0132_mic_boost_set(struct hda_codec *codec, long val) |
3501 | { | |
3502 | struct ca0132_spec *spec = codec->spec; | |
3503 | int ret = 0; | |
3504 | ||
3505 | if (val) /* on */ | |
3506 | ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0, | |
3507 | HDA_INPUT, 0, HDA_AMP_VOLMASK, 3); | |
3508 | else /* off */ | |
3509 | ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0, | |
3510 | HDA_INPUT, 0, HDA_AMP_VOLMASK, 0); | |
3511 | ||
3512 | return ret; | |
3513 | } | |
3514 | ||
a7e76271 IM |
3515 | static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol, |
3516 | struct snd_ctl_elem_value *ucontrol) | |
95c6e9cb | 3517 | { |
a7e76271 IM |
3518 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
3519 | hda_nid_t nid = get_amp_nid(kcontrol); | |
3520 | hda_nid_t shared_nid = 0; | |
3521 | bool effective; | |
3522 | int ret = 0; | |
95c6e9cb | 3523 | struct ca0132_spec *spec = codec->spec; |
a7e76271 | 3524 | int auto_jack; |
95c6e9cb | 3525 | |
a7e76271 IM |
3526 | if (nid == VNID_HP_SEL) { |
3527 | auto_jack = | |
3528 | spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID]; | |
3529 | if (!auto_jack) | |
3530 | ca0132_select_out(codec); | |
3531 | return 1; | |
3532 | } | |
95c6e9cb | 3533 | |
a7e76271 IM |
3534 | if (nid == VNID_AMIC1_SEL) { |
3535 | auto_jack = | |
3536 | spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID]; | |
3537 | if (!auto_jack) | |
3538 | ca0132_select_mic(codec); | |
3539 | return 1; | |
3540 | } | |
95c6e9cb | 3541 | |
a7e76271 IM |
3542 | if (nid == VNID_HP_ASEL) { |
3543 | ca0132_select_out(codec); | |
3544 | return 1; | |
3545 | } | |
95c6e9cb | 3546 | |
a7e76271 IM |
3547 | if (nid == VNID_AMIC1_ASEL) { |
3548 | ca0132_select_mic(codec); | |
3549 | return 1; | |
95c6e9cb | 3550 | } |
a7e76271 IM |
3551 | |
3552 | /* if effective conditions, then update hw immediately. */ | |
3553 | effective = ca0132_is_vnode_effective(codec, nid, &shared_nid); | |
3554 | if (effective) { | |
3555 | int dir = get_amp_direction(kcontrol); | |
3556 | int ch = get_amp_channels(kcontrol); | |
3557 | unsigned long pval; | |
3558 | ||
3559 | mutex_lock(&codec->control_mutex); | |
3560 | pval = kcontrol->private_value; | |
3561 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch, | |
3562 | 0, dir); | |
3563 | ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); | |
3564 | kcontrol->private_value = pval; | |
3565 | mutex_unlock(&codec->control_mutex); | |
95c6e9cb | 3566 | } |
95c6e9cb | 3567 | |
a7e76271 | 3568 | return ret; |
95c6e9cb | 3569 | } |
a7e76271 | 3570 | /* End of control change helpers. */ |
95c6e9cb | 3571 | |
a7e76271 IM |
3572 | static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol, |
3573 | struct snd_ctl_elem_info *uinfo) | |
3574 | { | |
3575 | unsigned int items = sizeof(ca0132_voicefx_presets) | |
3576 | / sizeof(struct ct_voicefx_preset); | |
3577 | ||
3578 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
3579 | uinfo->count = 1; | |
3580 | uinfo->value.enumerated.items = items; | |
3581 | if (uinfo->value.enumerated.item >= items) | |
3582 | uinfo->value.enumerated.item = items - 1; | |
3583 | strcpy(uinfo->value.enumerated.name, | |
3584 | ca0132_voicefx_presets[uinfo->value.enumerated.item].name); | |
3585 | return 0; | |
3586 | } | |
95c6e9cb | 3587 | |
a7e76271 | 3588 | static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol, |
95c6e9cb IM |
3589 | struct snd_ctl_elem_value *ucontrol) |
3590 | { | |
3591 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3592 | struct ca0132_spec *spec = codec->spec; | |
95c6e9cb | 3593 | |
a7e76271 | 3594 | ucontrol->value.enumerated.item[0] = spec->voicefx_val; |
95c6e9cb IM |
3595 | return 0; |
3596 | } | |
3597 | ||
a7e76271 | 3598 | static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol, |
95c6e9cb IM |
3599 | struct snd_ctl_elem_value *ucontrol) |
3600 | { | |
3601 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3602 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
3603 | int i, err = 0; |
3604 | int sel = ucontrol->value.enumerated.item[0]; | |
3605 | unsigned int items = sizeof(ca0132_voicefx_presets) | |
3606 | / sizeof(struct ct_voicefx_preset); | |
95c6e9cb | 3607 | |
a7e76271 | 3608 | if (sel >= items) |
95c6e9cb IM |
3609 | return 0; |
3610 | ||
a7e76271 IM |
3611 | snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n", |
3612 | sel, ca0132_voicefx_presets[sel].name); | |
95c6e9cb | 3613 | |
a7e76271 IM |
3614 | /* |
3615 | * Idx 0 is default. | |
3616 | * Default needs to qualify with CrystalVoice state. | |
3617 | */ | |
3618 | for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) { | |
3619 | err = dspio_set_uint_param(codec, ca0132_voicefx.mid, | |
3620 | ca0132_voicefx.reqs[i], | |
3621 | ca0132_voicefx_presets[sel].vals[i]); | |
3622 | if (err < 0) | |
3623 | break; | |
3624 | } | |
95c6e9cb | 3625 | |
a7e76271 IM |
3626 | if (err >= 0) { |
3627 | spec->voicefx_val = sel; | |
3628 | /* enable voice fx */ | |
3629 | ca0132_voicefx_set(codec, (sel ? 1 : 0)); | |
3630 | } | |
95c6e9cb | 3631 | |
a7e76271 | 3632 | return 1; |
95c6e9cb IM |
3633 | } |
3634 | ||
a7e76271 IM |
3635 | static int ca0132_switch_get(struct snd_kcontrol *kcontrol, |
3636 | struct snd_ctl_elem_value *ucontrol) | |
95c6e9cb IM |
3637 | { |
3638 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3639 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
3640 | hda_nid_t nid = get_amp_nid(kcontrol); |
3641 | int ch = get_amp_channels(kcontrol); | |
95c6e9cb IM |
3642 | long *valp = ucontrol->value.integer.value; |
3643 | ||
a7e76271 IM |
3644 | /* vnode */ |
3645 | if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) { | |
3646 | if (ch & 1) { | |
3647 | *valp = spec->vnode_lswitch[nid - VNODE_START_NID]; | |
3648 | valp++; | |
3649 | } | |
3650 | if (ch & 2) { | |
3651 | *valp = spec->vnode_rswitch[nid - VNODE_START_NID]; | |
3652 | valp++; | |
3653 | } | |
3654 | return 0; | |
3655 | } | |
3656 | ||
3657 | /* effects, include PE and CrystalVoice */ | |
3658 | if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) { | |
3659 | *valp = spec->effects_switch[nid - EFFECT_START_NID]; | |
3660 | return 0; | |
3661 | } | |
3662 | ||
3663 | /* mic boost */ | |
3664 | if (nid == spec->input_pins[0]) { | |
3665 | *valp = spec->cur_mic_boost; | |
3666 | return 0; | |
3667 | } | |
3668 | ||
95c6e9cb IM |
3669 | return 0; |
3670 | } | |
3671 | ||
a7e76271 IM |
3672 | static int ca0132_switch_put(struct snd_kcontrol *kcontrol, |
3673 | struct snd_ctl_elem_value *ucontrol) | |
95c6e9cb IM |
3674 | { |
3675 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3676 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
3677 | hda_nid_t nid = get_amp_nid(kcontrol); |
3678 | int ch = get_amp_channels(kcontrol); | |
95c6e9cb | 3679 | long *valp = ucontrol->value.integer.value; |
a7e76271 | 3680 | int changed = 1; |
95c6e9cb | 3681 | |
a7e76271 IM |
3682 | snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n", |
3683 | nid, *valp); | |
95c6e9cb IM |
3684 | |
3685 | snd_hda_power_up(codec); | |
a7e76271 IM |
3686 | /* vnode */ |
3687 | if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) { | |
3688 | if (ch & 1) { | |
3689 | spec->vnode_lswitch[nid - VNODE_START_NID] = *valp; | |
3690 | valp++; | |
3691 | } | |
3692 | if (ch & 2) { | |
3693 | spec->vnode_rswitch[nid - VNODE_START_NID] = *valp; | |
3694 | valp++; | |
3695 | } | |
3696 | changed = ca0132_vnode_switch_set(kcontrol, ucontrol); | |
3697 | goto exit; | |
3698 | } | |
95c6e9cb | 3699 | |
a7e76271 IM |
3700 | /* PE */ |
3701 | if (nid == PLAY_ENHANCEMENT) { | |
3702 | spec->effects_switch[nid - EFFECT_START_NID] = *valp; | |
3703 | changed = ca0132_pe_switch_set(codec); | |
b97f6bfd | 3704 | goto exit; |
a7e76271 | 3705 | } |
95c6e9cb | 3706 | |
a7e76271 IM |
3707 | /* CrystalVoice */ |
3708 | if (nid == CRYSTAL_VOICE) { | |
3709 | spec->effects_switch[nid - EFFECT_START_NID] = *valp; | |
3710 | changed = ca0132_cvoice_switch_set(codec); | |
b97f6bfd | 3711 | goto exit; |
a7e76271 | 3712 | } |
95c6e9cb | 3713 | |
a7e76271 IM |
3714 | /* out and in effects */ |
3715 | if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) || | |
3716 | ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) { | |
3717 | spec->effects_switch[nid - EFFECT_START_NID] = *valp; | |
3718 | changed = ca0132_effects_set(codec, nid, *valp); | |
3719 | goto exit; | |
3720 | } | |
3721 | ||
3722 | /* mic boost */ | |
3723 | if (nid == spec->input_pins[0]) { | |
3724 | spec->cur_mic_boost = *valp; | |
3725 | ||
3726 | /* Mic boost does not apply to Digital Mic */ | |
3727 | if (spec->cur_mic_type != DIGITAL_MIC) | |
3728 | changed = ca0132_mic_boost_set(codec, *valp); | |
3729 | goto exit; | |
3730 | } | |
95c6e9cb | 3731 | |
a7e76271 | 3732 | exit: |
95c6e9cb | 3733 | snd_hda_power_down(codec); |
a7e76271 | 3734 | return changed; |
95c6e9cb IM |
3735 | } |
3736 | ||
a7e76271 IM |
3737 | /* |
3738 | * Volume related | |
3739 | */ | |
3740 | static int ca0132_volume_info(struct snd_kcontrol *kcontrol, | |
3741 | struct snd_ctl_elem_info *uinfo) | |
3742 | { | |
3743 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3744 | struct ca0132_spec *spec = codec->spec; | |
3745 | hda_nid_t nid = get_amp_nid(kcontrol); | |
3746 | int ch = get_amp_channels(kcontrol); | |
3747 | int dir = get_amp_direction(kcontrol); | |
3748 | unsigned long pval; | |
3749 | int err; | |
3750 | ||
3751 | switch (nid) { | |
3752 | case VNID_SPK: | |
3753 | /* follow shared_out info */ | |
3754 | nid = spec->shared_out_nid; | |
3755 | mutex_lock(&codec->control_mutex); | |
3756 | pval = kcontrol->private_value; | |
3757 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir); | |
3758 | err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo); | |
3759 | kcontrol->private_value = pval; | |
3760 | mutex_unlock(&codec->control_mutex); | |
3761 | break; | |
3762 | case VNID_MIC: | |
3763 | /* follow shared_mic info */ | |
3764 | nid = spec->shared_mic_nid; | |
3765 | mutex_lock(&codec->control_mutex); | |
3766 | pval = kcontrol->private_value; | |
3767 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir); | |
3768 | err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo); | |
3769 | kcontrol->private_value = pval; | |
3770 | mutex_unlock(&codec->control_mutex); | |
3771 | break; | |
3772 | default: | |
3773 | err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo); | |
3774 | } | |
3775 | return err; | |
3776 | } | |
3777 | ||
3778 | static int ca0132_volume_get(struct snd_kcontrol *kcontrol, | |
95c6e9cb IM |
3779 | struct snd_ctl_elem_value *ucontrol) |
3780 | { | |
3781 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3782 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
3783 | hda_nid_t nid = get_amp_nid(kcontrol); |
3784 | int ch = get_amp_channels(kcontrol); | |
95c6e9cb IM |
3785 | long *valp = ucontrol->value.integer.value; |
3786 | ||
a7e76271 IM |
3787 | /* store the left and right volume */ |
3788 | if (ch & 1) { | |
3789 | *valp = spec->vnode_lvol[nid - VNODE_START_NID]; | |
3790 | valp++; | |
3791 | } | |
3792 | if (ch & 2) { | |
3793 | *valp = spec->vnode_rvol[nid - VNODE_START_NID]; | |
3794 | valp++; | |
3795 | } | |
95c6e9cb IM |
3796 | return 0; |
3797 | } | |
3798 | ||
a7e76271 | 3799 | static int ca0132_volume_put(struct snd_kcontrol *kcontrol, |
95c6e9cb IM |
3800 | struct snd_ctl_elem_value *ucontrol) |
3801 | { | |
3802 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); | |
3803 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
3804 | hda_nid_t nid = get_amp_nid(kcontrol); |
3805 | int ch = get_amp_channels(kcontrol); | |
95c6e9cb | 3806 | long *valp = ucontrol->value.integer.value; |
a7e76271 IM |
3807 | hda_nid_t shared_nid = 0; |
3808 | bool effective; | |
3809 | int changed = 1; | |
3810 | ||
3811 | /* store the left and right volume */ | |
3812 | if (ch & 1) { | |
3813 | spec->vnode_lvol[nid - VNODE_START_NID] = *valp; | |
3814 | valp++; | |
3815 | } | |
3816 | if (ch & 2) { | |
3817 | spec->vnode_rvol[nid - VNODE_START_NID] = *valp; | |
3818 | valp++; | |
3819 | } | |
95c6e9cb | 3820 | |
a7e76271 IM |
3821 | /* if effective conditions, then update hw immediately. */ |
3822 | effective = ca0132_is_vnode_effective(codec, nid, &shared_nid); | |
3823 | if (effective) { | |
3824 | int dir = get_amp_direction(kcontrol); | |
3825 | unsigned long pval; | |
3826 | ||
3827 | snd_hda_power_up(codec); | |
3828 | mutex_lock(&codec->control_mutex); | |
3829 | pval = kcontrol->private_value; | |
3830 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch, | |
3831 | 0, dir); | |
3832 | changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol); | |
3833 | kcontrol->private_value = pval; | |
3834 | mutex_unlock(&codec->control_mutex); | |
3835 | snd_hda_power_down(codec); | |
3836 | } | |
95c6e9cb | 3837 | |
a7e76271 | 3838 | return changed; |
95c6e9cb IM |
3839 | } |
3840 | ||
a7e76271 IM |
3841 | static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag, |
3842 | unsigned int size, unsigned int __user *tlv) | |
95c6e9cb | 3843 | { |
a7e76271 IM |
3844 | struct hda_codec *codec = snd_kcontrol_chip(kcontrol); |
3845 | struct ca0132_spec *spec = codec->spec; | |
3846 | hda_nid_t nid = get_amp_nid(kcontrol); | |
3847 | int ch = get_amp_channels(kcontrol); | |
3848 | int dir = get_amp_direction(kcontrol); | |
3849 | unsigned long pval; | |
3850 | int err; | |
3851 | ||
3852 | switch (nid) { | |
3853 | case VNID_SPK: | |
3854 | /* follow shared_out tlv */ | |
3855 | nid = spec->shared_out_nid; | |
3856 | mutex_lock(&codec->control_mutex); | |
3857 | pval = kcontrol->private_value; | |
3858 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir); | |
3859 | err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv); | |
3860 | kcontrol->private_value = pval; | |
3861 | mutex_unlock(&codec->control_mutex); | |
3862 | break; | |
3863 | case VNID_MIC: | |
3864 | /* follow shared_mic tlv */ | |
3865 | nid = spec->shared_mic_nid; | |
3866 | mutex_lock(&codec->control_mutex); | |
3867 | pval = kcontrol->private_value; | |
3868 | kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir); | |
3869 | err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv); | |
3870 | kcontrol->private_value = pval; | |
3871 | mutex_unlock(&codec->control_mutex); | |
3872 | break; | |
3873 | default: | |
3874 | err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv); | |
3875 | } | |
3876 | return err; | |
95c6e9cb IM |
3877 | } |
3878 | ||
a7e76271 IM |
3879 | static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid, |
3880 | const char *pfx, int dir) | |
95c6e9cb | 3881 | { |
a7e76271 IM |
3882 | char namestr[44]; |
3883 | int type = dir ? HDA_INPUT : HDA_OUTPUT; | |
95c6e9cb | 3884 | struct snd_kcontrol_new knew = |
a7e76271 IM |
3885 | CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type); |
3886 | sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]); | |
95c6e9cb IM |
3887 | return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec)); |
3888 | } | |
3889 | ||
a7e76271 | 3890 | static int add_voicefx(struct hda_codec *codec) |
95c6e9cb IM |
3891 | { |
3892 | struct snd_kcontrol_new knew = | |
a7e76271 IM |
3893 | HDA_CODEC_MUTE_MONO(ca0132_voicefx.name, |
3894 | VOICEFX, 1, 0, HDA_INPUT); | |
3895 | knew.info = ca0132_voicefx_info; | |
3896 | knew.get = ca0132_voicefx_get; | |
3897 | knew.put = ca0132_voicefx_put; | |
3898 | return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec)); | |
95c6e9cb IM |
3899 | } |
3900 | ||
a7e76271 IM |
3901 | /* |
3902 | * When changing Node IDs for Mixer Controls below, make sure to update | |
3903 | * Node IDs in ca0132_config() as well. | |
3904 | */ | |
3905 | static struct snd_kcontrol_new ca0132_mixer[] = { | |
3906 | CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT), | |
3907 | CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT), | |
3908 | CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT), | |
3909 | CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT), | |
3910 | HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT), | |
3911 | HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT), | |
3912 | HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT), | |
3913 | HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT), | |
3914 | CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch", | |
3915 | 0x12, 1, HDA_INPUT), | |
3916 | CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch", | |
3917 | VNID_HP_SEL, 1, HDA_OUTPUT), | |
3918 | CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch", | |
3919 | VNID_AMIC1_SEL, 1, HDA_INPUT), | |
3920 | CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch", | |
3921 | VNID_HP_ASEL, 1, HDA_OUTPUT), | |
3922 | CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch", | |
3923 | VNID_AMIC1_ASEL, 1, HDA_INPUT), | |
3924 | { } /* end */ | |
3925 | }; | |
3926 | ||
e90f29e4 IM |
3927 | static int ca0132_build_controls(struct hda_codec *codec) |
3928 | { | |
3929 | struct ca0132_spec *spec = codec->spec; | |
3930 | int i, num_fx; | |
3931 | int err = 0; | |
3932 | ||
3933 | /* Add Mixer controls */ | |
3934 | for (i = 0; i < spec->num_mixers; i++) { | |
3935 | err = snd_hda_add_new_ctls(codec, spec->mixers[i]); | |
3936 | if (err < 0) | |
3937 | return err; | |
3938 | } | |
3939 | ||
3940 | /* Add in and out effects controls. | |
3941 | * VoiceFX, PE and CrystalVoice are added separately. | |
3942 | */ | |
3943 | num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT; | |
3944 | for (i = 0; i < num_fx; i++) { | |
3945 | err = add_fx_switch(codec, ca0132_effects[i].nid, | |
3946 | ca0132_effects[i].name, | |
3947 | ca0132_effects[i].direct); | |
3948 | if (err < 0) | |
3949 | return err; | |
3950 | } | |
3951 | ||
3952 | err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0); | |
3953 | if (err < 0) | |
3954 | return err; | |
3955 | ||
3956 | err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1); | |
3957 | if (err < 0) | |
3958 | return err; | |
3959 | ||
3960 | add_voicefx(codec); | |
3961 | ||
3962 | #ifdef ENABLE_TUNING_CONTROLS | |
3963 | add_tuning_ctls(codec); | |
3964 | #endif | |
3965 | ||
3966 | err = snd_hda_jack_add_kctls(codec, &spec->autocfg); | |
3967 | if (err < 0) | |
3968 | return err; | |
3969 | ||
3970 | if (spec->dig_out) { | |
3971 | err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out, | |
3972 | spec->dig_out); | |
3973 | if (err < 0) | |
3974 | return err; | |
3975 | err = snd_hda_create_spdif_share_sw(codec, &spec->multiout); | |
3976 | if (err < 0) | |
3977 | return err; | |
3978 | /* spec->multiout.share_spdif = 1; */ | |
3979 | } | |
3980 | ||
3981 | if (spec->dig_in) { | |
3982 | err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in); | |
3983 | if (err < 0) | |
3984 | return err; | |
3985 | } | |
3986 | return 0; | |
3987 | } | |
3988 | ||
a7e76271 | 3989 | /* |
e90f29e4 | 3990 | * PCM |
a7e76271 IM |
3991 | */ |
3992 | static struct hda_pcm_stream ca0132_pcm_analog_playback = { | |
3993 | .substreams = 1, | |
3994 | .channels_min = 2, | |
825315bc | 3995 | .channels_max = 6, |
a7e76271 | 3996 | .ops = { |
a7e76271 IM |
3997 | .prepare = ca0132_playback_pcm_prepare, |
3998 | .cleanup = ca0132_playback_pcm_cleanup | |
3999 | }, | |
4000 | }; | |
4001 | ||
4002 | static struct hda_pcm_stream ca0132_pcm_analog_capture = { | |
4003 | .substreams = 1, | |
4004 | .channels_min = 2, | |
4005 | .channels_max = 2, | |
825315bc IM |
4006 | .ops = { |
4007 | .prepare = ca0132_capture_pcm_prepare, | |
4008 | .cleanup = ca0132_capture_pcm_cleanup | |
4009 | }, | |
a7e76271 IM |
4010 | }; |
4011 | ||
4012 | static struct hda_pcm_stream ca0132_pcm_digital_playback = { | |
4013 | .substreams = 1, | |
4014 | .channels_min = 2, | |
4015 | .channels_max = 2, | |
4016 | .ops = { | |
4017 | .open = ca0132_dig_playback_pcm_open, | |
4018 | .close = ca0132_dig_playback_pcm_close, | |
4019 | .prepare = ca0132_dig_playback_pcm_prepare, | |
4020 | .cleanup = ca0132_dig_playback_pcm_cleanup | |
4021 | }, | |
4022 | }; | |
4023 | ||
4024 | static struct hda_pcm_stream ca0132_pcm_digital_capture = { | |
4025 | .substreams = 1, | |
4026 | .channels_min = 2, | |
4027 | .channels_max = 2, | |
4028 | }; | |
4029 | ||
4030 | static int ca0132_build_pcms(struct hda_codec *codec) | |
95c6e9cb IM |
4031 | { |
4032 | struct ca0132_spec *spec = codec->spec; | |
a7e76271 IM |
4033 | struct hda_pcm *info = spec->pcm_rec; |
4034 | ||
4035 | codec->pcm_info = info; | |
4036 | codec->num_pcms = 0; | |
4037 | ||
4038 | info->name = "CA0132 Analog"; | |
4039 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback; | |
4040 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0]; | |
4041 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = | |
4042 | spec->multiout.max_channels; | |
4043 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture; | |
825315bc | 4044 | info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1; |
a7e76271 IM |
4045 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0]; |
4046 | codec->num_pcms++; | |
4047 | ||
825315bc IM |
4048 | info++; |
4049 | info->name = "CA0132 Analog Mic-In2"; | |
4050 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture; | |
4051 | info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1; | |
4052 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1]; | |
4053 | codec->num_pcms++; | |
4054 | ||
4055 | info++; | |
4056 | info->name = "CA0132 What U Hear"; | |
4057 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture; | |
4058 | info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1; | |
4059 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2]; | |
4060 | codec->num_pcms++; | |
4061 | ||
a7e76271 IM |
4062 | if (!spec->dig_out && !spec->dig_in) |
4063 | return 0; | |
4064 | ||
4065 | info++; | |
4066 | info->name = "CA0132 Digital"; | |
4067 | info->pcm_type = HDA_PCM_TYPE_SPDIF; | |
4068 | if (spec->dig_out) { | |
4069 | info->stream[SNDRV_PCM_STREAM_PLAYBACK] = | |
4070 | ca0132_pcm_digital_playback; | |
4071 | info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out; | |
4072 | } | |
4073 | if (spec->dig_in) { | |
4074 | info->stream[SNDRV_PCM_STREAM_CAPTURE] = | |
4075 | ca0132_pcm_digital_capture; | |
4076 | info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in; | |
4077 | } | |
4078 | codec->num_pcms++; | |
95c6e9cb | 4079 | |
a7e76271 | 4080 | return 0; |
95c6e9cb IM |
4081 | } |
4082 | ||
441aa6a0 IM |
4083 | static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac) |
4084 | { | |
4085 | if (pin) { | |
4086 | snd_hda_codec_write(codec, pin, 0, | |
4087 | AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP); | |
4088 | if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP) | |
4089 | snd_hda_codec_write(codec, pin, 0, | |
4090 | AC_VERB_SET_AMP_GAIN_MUTE, | |
4091 | AMP_OUT_UNMUTE); | |
4092 | } | |
4093 | if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP)) | |
4094 | snd_hda_codec_write(codec, dac, 0, | |
4095 | AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO); | |
4096 | } | |
4097 | ||
4098 | static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc) | |
4099 | { | |
4100 | if (pin) { | |
4101 | snd_hda_codec_write(codec, pin, 0, | |
4102 | AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80); | |
4103 | if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP) | |
4104 | snd_hda_codec_write(codec, pin, 0, | |
4105 | AC_VERB_SET_AMP_GAIN_MUTE, | |
4106 | AMP_IN_UNMUTE(0)); | |
4107 | } | |
4108 | if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) { | |
4109 | snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE, | |
4110 | AMP_IN_UNMUTE(0)); | |
4111 | ||
4112 | /* init to 0 dB and unmute. */ | |
4113 | snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0, | |
4114 | HDA_AMP_VOLMASK, 0x5a); | |
4115 | snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0, | |
4116 | HDA_AMP_MUTE, 0); | |
4117 | } | |
4118 | } | |
4119 | ||
a73d511c IM |
4120 | static void ca0132_init_unsol(struct hda_codec *codec) |
4121 | { | |
4122 | snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP); | |
4123 | snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1); | |
4124 | } | |
4125 | ||
5aaca44d IM |
4126 | static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir) |
4127 | { | |
4128 | unsigned int caps; | |
4129 | ||
4130 | caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ? | |
4131 | AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); | |
4132 | snd_hda_override_amp_caps(codec, nid, dir, caps); | |
4133 | } | |
4134 | ||
4135 | /* | |
4136 | * Switch between Digital built-in mic and analog mic. | |
4137 | */ | |
4138 | static void ca0132_set_dmic(struct hda_codec *codec, int enable) | |
4139 | { | |
4140 | struct ca0132_spec *spec = codec->spec; | |
4141 | unsigned int tmp; | |
4142 | u8 val; | |
4143 | unsigned int oldval; | |
4144 | ||
4145 | snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable); | |
4146 | ||
4147 | oldval = stop_mic1(codec); | |
4148 | ca0132_set_vipsource(codec, 0); | |
4149 | if (enable) { | |
4150 | /* set DMic input as 2-ch */ | |
4151 | tmp = FLOAT_TWO; | |
4152 | dspio_set_uint_param(codec, 0x80, 0x00, tmp); | |
4153 | ||
4154 | val = spec->dmic_ctl; | |
4155 | val |= 0x80; | |
4156 | snd_hda_codec_write(codec, spec->input_pins[0], 0, | |
4157 | VENDOR_CHIPIO_DMIC_CTL_SET, val); | |
4158 | ||
4159 | if (!(spec->dmic_ctl & 0x20)) | |
4160 | chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1); | |
4161 | } else { | |
4162 | /* set AMic input as mono */ | |
4163 | tmp = FLOAT_ONE; | |
4164 | dspio_set_uint_param(codec, 0x80, 0x00, tmp); | |
4165 | ||
4166 | val = spec->dmic_ctl; | |
4167 | /* clear bit7 and bit5 to disable dmic */ | |
4168 | val &= 0x5f; | |
4169 | snd_hda_codec_write(codec, spec->input_pins[0], 0, | |
4170 | VENDOR_CHIPIO_DMIC_CTL_SET, val); | |
4171 | ||
4172 | if (!(spec->dmic_ctl & 0x20)) | |
4173 | chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0); | |
4174 | } | |
4175 | ca0132_set_vipsource(codec, 1); | |
4176 | resume_mic1(codec, oldval); | |
4177 | } | |
4178 | ||
4179 | /* | |
4180 | * Initialization for Digital Mic. | |
4181 | */ | |
4182 | static void ca0132_init_dmic(struct hda_codec *codec) | |
4183 | { | |
4184 | struct ca0132_spec *spec = codec->spec; | |
4185 | u8 val; | |
4186 | ||
4187 | /* Setup Digital Mic here, but don't enable. | |
4188 | * Enable based on jack detect. | |
4189 | */ | |
4190 | ||
4191 | /* MCLK uses MPIO1, set to enable. | |
4192 | * Bit 2-0: MPIO select | |
4193 | * Bit 3: set to disable | |
4194 | * Bit 7-4: reserved | |
4195 | */ | |
4196 | val = 0x01; | |
4197 | snd_hda_codec_write(codec, spec->input_pins[0], 0, | |
4198 | VENDOR_CHIPIO_DMIC_MCLK_SET, val); | |
4199 | ||
4200 | /* Data1 uses MPIO3. Data2 not use | |
4201 | * Bit 2-0: Data1 MPIO select | |
4202 | * Bit 3: set disable Data1 | |
4203 | * Bit 6-4: Data2 MPIO select | |
4204 | * Bit 7: set disable Data2 | |
4205 | */ | |
4206 | val = 0x83; | |
4207 | snd_hda_codec_write(codec, spec->input_pins[0], 0, | |
4208 | VENDOR_CHIPIO_DMIC_PIN_SET, val); | |
4209 | ||
4210 | /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first. | |
4211 | * Bit 3-0: Channel mask | |
4212 | * Bit 4: set for 48KHz, clear for 32KHz | |
4213 | * Bit 5: mode | |
4214 | * Bit 6: set to select Data2, clear for Data1 | |
4215 | * Bit 7: set to enable DMic, clear for AMic | |
4216 | */ | |
4217 | val = 0x23; | |
4218 | /* keep a copy of dmic ctl val for enable/disable dmic purpuse */ | |
4219 | spec->dmic_ctl = val; | |
4220 | snd_hda_codec_write(codec, spec->input_pins[0], 0, | |
4221 | VENDOR_CHIPIO_DMIC_CTL_SET, val); | |
4222 | } | |
4223 | ||
4224 | /* | |
4225 | * Initialization for Analog Mic 2 | |
4226 | */ | |
4227 | static void ca0132_init_analog_mic2(struct hda_codec *codec) | |
4228 | { | |
4229 | struct ca0132_spec *spec = codec->spec; | |
4230 | ||
4231 | mutex_lock(&spec->chipio_mutex); | |
4232 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4233 | VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20); | |
4234 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4235 | VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19); | |
4236 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4237 | VENDOR_CHIPIO_8051_DATA_WRITE, 0x00); | |
4238 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4239 | VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D); | |
4240 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4241 | VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19); | |
4242 | snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, | |
4243 | VENDOR_CHIPIO_8051_DATA_WRITE, 0x00); | |
4244 | mutex_unlock(&spec->chipio_mutex); | |
4245 | } | |
4246 | ||
4247 | static void ca0132_refresh_widget_caps(struct hda_codec *codec) | |
4248 | { | |
4249 | struct ca0132_spec *spec = codec->spec; | |
4250 | int i; | |
4251 | hda_nid_t nid; | |
4252 | ||
4253 | snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n"); | |
4254 | nid = codec->start_nid; | |
4255 | for (i = 0; i < codec->num_nodes; i++, nid++) | |
4256 | codec->wcaps[i] = snd_hda_param_read(codec, nid, | |
4257 | AC_PAR_AUDIO_WIDGET_CAP); | |
4258 | ||
4259 | for (i = 0; i < spec->multiout.num_dacs; i++) | |
4260 | refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT); | |
4261 | ||
4262 | for (i = 0; i < spec->num_outputs; i++) | |
4263 | refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT); | |
4264 | ||
4265 | for (i = 0; i < spec->num_inputs; i++) { | |
4266 | refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT); | |
4267 | refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT); | |
4268 | } | |
4269 | } | |
4270 | ||
4271 | /* | |
4272 | * Setup default parameters for DSP | |
4273 | */ | |
4274 | static void ca0132_setup_defaults(struct hda_codec *codec) | |
4275 | { | |
4276 | unsigned int tmp; | |
4277 | int num_fx; | |
4278 | int idx, i; | |
4279 | ||
4280 | if (!dspload_is_loaded(codec)) | |
4281 | return; | |
4282 | ||
4283 | /* out, in effects + voicefx */ | |
4284 | num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1; | |
4285 | for (idx = 0; idx < num_fx; idx++) { | |
4286 | for (i = 0; i <= ca0132_effects[idx].params; i++) { | |
4287 | dspio_set_uint_param(codec, ca0132_effects[idx].mid, | |
4288 | ca0132_effects[idx].reqs[i], | |
4289 | ca0132_effects[idx].def_vals[i]); | |
4290 | } | |
4291 | } | |
4292 | ||
4293 | /*remove DSP headroom*/ | |
4294 | tmp = FLOAT_ZERO; | |
4295 | dspio_set_uint_param(codec, 0x96, 0x3C, tmp); | |
4296 | ||
4297 | /*set speaker EQ bypass attenuation*/ | |
4298 | dspio_set_uint_param(codec, 0x8f, 0x01, tmp); | |
4299 | ||
4300 | /* set AMic1 and AMic2 as mono mic */ | |
4301 | tmp = FLOAT_ONE; | |
4302 | dspio_set_uint_param(codec, 0x80, 0x00, tmp); | |
4303 | dspio_set_uint_param(codec, 0x80, 0x01, tmp); | |
4304 | ||
4305 | /* set AMic1 as CrystalVoice input */ | |
4306 | tmp = FLOAT_ONE; | |
4307 | dspio_set_uint_param(codec, 0x80, 0x05, tmp); | |
4308 | ||
4309 | /* set WUH source */ | |
4310 | tmp = FLOAT_TWO; | |
4311 | dspio_set_uint_param(codec, 0x31, 0x00, tmp); | |
4312 | } | |
4313 | ||
4314 | /* | |
4315 | * Initialization of flags in chip | |
4316 | */ | |
4317 | static void ca0132_init_flags(struct hda_codec *codec) | |
4318 | { | |
4319 | chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0); | |
4320 | chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0); | |
4321 | chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0); | |
4322 | chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0); | |
4323 | chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0); | |
4324 | chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1); | |
4325 | } | |
4326 | ||
4327 | /* | |
4328 | * Initialization of parameters in chip | |
4329 | */ | |
4330 | static void ca0132_init_params(struct hda_codec *codec) | |
4331 | { | |
4332 | chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6); | |
4333 | chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6); | |
4334 | } | |
95c6e9cb | 4335 | |
e90f29e4 IM |
4336 | static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k) |
4337 | { | |
4338 | chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k); | |
4339 | chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k); | |
4340 | chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k); | |
4341 | chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k); | |
4342 | chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k); | |
4343 | chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k); | |
4344 | ||
406261ce IM |
4345 | chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000); |
4346 | chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000); | |
e90f29e4 IM |
4347 | chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000); |
4348 | } | |
4349 | ||
4350 | static bool ca0132_download_dsp_images(struct hda_codec *codec) | |
4351 | { | |
4352 | bool dsp_loaded = false; | |
4353 | const struct dsp_image_seg *dsp_os_image; | |
4354 | ||
4355 | if (request_firmware_cached(&fw_efx, EFX_FILE, | |
4356 | codec->bus->card->dev) != 0) | |
4357 | return false; | |
4358 | ||
4359 | dsp_os_image = (struct dsp_image_seg *)(fw_efx->data); | |
4360 | dspload_image(codec, dsp_os_image, 0, 0, true, 0); | |
4361 | dsp_loaded = dspload_wait_loaded(codec); | |
4362 | ||
4363 | return dsp_loaded; | |
4364 | } | |
4365 | ||
4366 | static void ca0132_download_dsp(struct hda_codec *codec) | |
4367 | { | |
4368 | struct ca0132_spec *spec = codec->spec; | |
4369 | ||
4370 | spec->dsp_state = DSP_DOWNLOAD_INIT; | |
4371 | ||
4372 | if (spec->dsp_state == DSP_DOWNLOAD_INIT) { | |
4373 | chipio_enable_clocks(codec); | |
4374 | spec->dsp_state = DSP_DOWNLOADING; | |
4375 | if (!ca0132_download_dsp_images(codec)) | |
4376 | spec->dsp_state = DSP_DOWNLOAD_FAILED; | |
4377 | else | |
4378 | spec->dsp_state = DSP_DOWNLOADED; | |
4379 | } | |
4380 | ||
4381 | if (spec->dsp_state == DSP_DOWNLOADED) | |
4382 | ca0132_set_dsp_msr(codec, true); | |
4383 | } | |
4384 | ||
e90f29e4 IM |
4385 | static void ca0132_process_dsp_response(struct hda_codec *codec) |
4386 | { | |
4387 | struct ca0132_spec *spec = codec->spec; | |
4388 | ||
4389 | snd_printdd(KERN_INFO "ca0132_process_dsp_response\n"); | |
4390 | if (spec->wait_scp) { | |
4391 | if (dspio_get_response_data(codec) >= 0) | |
4392 | spec->wait_scp = 0; | |
4393 | } | |
4394 | ||
4395 | dspio_clear_response_queue(codec); | |
4396 | } | |
4397 | ||
4398 | static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res) | |
4399 | { | |
4400 | snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res); | |
4401 | ||
4402 | ||
4403 | if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) { | |
4404 | ca0132_process_dsp_response(codec); | |
4405 | } else { | |
4406 | res = snd_hda_jack_get_action(codec, | |
4407 | (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f); | |
4408 | ||
4409 | snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res); | |
4410 | ||
4411 | switch (res) { | |
4412 | case UNSOL_TAG_HP: | |
4413 | ca0132_select_out(codec); | |
4414 | snd_hda_jack_report_sync(codec); | |
4415 | break; | |
4416 | case UNSOL_TAG_AMIC1: | |
4417 | ca0132_select_mic(codec); | |
4418 | snd_hda_jack_report_sync(codec); | |
4419 | break; | |
4420 | default: | |
4421 | break; | |
4422 | } | |
4423 | } | |
4424 | } | |
4425 | ||
5aaca44d IM |
4426 | /* |
4427 | * Verbs tables. | |
4428 | */ | |
4429 | ||
4430 | /* Sends before DSP download. */ | |
4431 | static struct hda_verb ca0132_base_init_verbs[] = { | |
4432 | /*enable ct extension*/ | |
4433 | {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1}, | |
4434 | /*enable DSP node unsol, needed for DSP download*/ | |
4435 | {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP}, | |
4436 | {} | |
4437 | }; | |
4438 | ||
4439 | /* Send at exit. */ | |
4440 | static struct hda_verb ca0132_base_exit_verbs[] = { | |
4441 | /*set afg to D3*/ | |
4442 | {0x01, AC_VERB_SET_POWER_STATE, 0x03}, | |
4443 | /*disable ct extension*/ | |
4444 | {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0}, | |
4445 | {} | |
4446 | }; | |
4447 | ||
4448 | /* Other verbs tables. Sends after DSP download. */ | |
4449 | static struct hda_verb ca0132_init_verbs0[] = { | |
4450 | /* chip init verbs */ | |
4451 | {0x15, 0x70D, 0xF0}, | |
4452 | {0x15, 0x70E, 0xFE}, | |
4453 | {0x15, 0x707, 0x75}, | |
4454 | {0x15, 0x707, 0xD3}, | |
4455 | {0x15, 0x707, 0x09}, | |
4456 | {0x15, 0x707, 0x53}, | |
4457 | {0x15, 0x707, 0xD4}, | |
4458 | {0x15, 0x707, 0xEF}, | |
4459 | {0x15, 0x707, 0x75}, | |
4460 | {0x15, 0x707, 0xD3}, | |
4461 | {0x15, 0x707, 0x09}, | |
4462 | {0x15, 0x707, 0x02}, | |
4463 | {0x15, 0x707, 0x37}, | |
4464 | {0x15, 0x707, 0x78}, | |
4465 | {0x15, 0x53C, 0xCE}, | |
4466 | {0x15, 0x575, 0xC9}, | |
4467 | {0x15, 0x53D, 0xCE}, | |
4468 | {0x15, 0x5B7, 0xC9}, | |
4469 | {0x15, 0x70D, 0xE8}, | |
4470 | {0x15, 0x70E, 0xFE}, | |
4471 | {0x15, 0x707, 0x02}, | |
4472 | {0x15, 0x707, 0x68}, | |
4473 | {0x15, 0x707, 0x62}, | |
4474 | {0x15, 0x53A, 0xCE}, | |
4475 | {0x15, 0x546, 0xC9}, | |
4476 | {0x15, 0x53B, 0xCE}, | |
4477 | {0x15, 0x5E8, 0xC9}, | |
4478 | {0x15, 0x717, 0x0D}, | |
4479 | {0x15, 0x718, 0x20}, | |
4480 | {} | |
4481 | }; | |
4482 | ||
4483 | static struct hda_verb ca0132_init_verbs1[] = { | |
4484 | {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP}, | |
4485 | {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1}, | |
4486 | /* config EAPD */ | |
4487 | {0x0b, 0x78D, 0x00}, | |
4488 | /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/ | |
4489 | /*{0x10, 0x78D, 0x02},*/ | |
4490 | /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/ | |
4491 | {} | |
4492 | }; | |
4493 | ||
95c6e9cb IM |
4494 | static void ca0132_init_chip(struct hda_codec *codec) |
4495 | { | |
4496 | struct ca0132_spec *spec = codec->spec; | |
5aaca44d IM |
4497 | int num_fx; |
4498 | int i; | |
4499 | unsigned int on; | |
95c6e9cb IM |
4500 | |
4501 | mutex_init(&spec->chipio_mutex); | |
5aaca44d IM |
4502 | |
4503 | spec->cur_out_type = SPEAKER_OUT; | |
4504 | spec->cur_mic_type = DIGITAL_MIC; | |
4505 | spec->cur_mic_boost = 0; | |
4506 | ||
4507 | for (i = 0; i < VNODES_COUNT; i++) { | |
4508 | spec->vnode_lvol[i] = 0x5a; | |
4509 | spec->vnode_rvol[i] = 0x5a; | |
4510 | spec->vnode_lswitch[i] = 0; | |
4511 | spec->vnode_rswitch[i] = 0; | |
4512 | } | |
4513 | ||
4514 | /* | |
4515 | * Default states for effects are in ca0132_effects[]. | |
4516 | */ | |
4517 | num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT; | |
4518 | for (i = 0; i < num_fx; i++) { | |
4519 | on = (unsigned int)ca0132_effects[i].reqs[0]; | |
4520 | spec->effects_switch[i] = on ? 1 : 0; | |
4521 | } | |
4522 | ||
4523 | spec->voicefx_val = 0; | |
4524 | spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1; | |
4525 | spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0; | |
4526 | ||
44f0c978 IM |
4527 | #ifdef ENABLE_TUNING_CONTROLS |
4528 | ca0132_init_tuning_defaults(codec); | |
4529 | #endif | |
95c6e9cb IM |
4530 | } |
4531 | ||
4532 | static void ca0132_exit_chip(struct hda_codec *codec) | |
4533 | { | |
4534 | /* put any chip cleanup stuffs here. */ | |
5aaca44d IM |
4535 | |
4536 | if (dspload_is_loaded(codec)) | |
4537 | dsp_reset(codec); | |
95c6e9cb IM |
4538 | } |
4539 | ||
4540 | static int ca0132_init(struct hda_codec *codec) | |
4541 | { | |
4542 | struct ca0132_spec *spec = codec->spec; | |
4543 | struct auto_pin_cfg *cfg = &spec->autocfg; | |
4544 | int i; | |
4545 | ||
5aaca44d IM |
4546 | spec->dsp_state = DSP_DOWNLOAD_INIT; |
4547 | spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS; | |
4548 | ||
4549 | snd_hda_power_up(codec); | |
4550 | ||
4551 | ca0132_init_params(codec); | |
4552 | ca0132_init_flags(codec); | |
4553 | snd_hda_sequence_write(codec, spec->base_init_verbs); | |
01ef7dbf IM |
4554 | #ifdef CONFIG_SND_HDA_DSP_LOADER |
4555 | ca0132_download_dsp(codec); | |
4556 | #endif | |
5aaca44d IM |
4557 | ca0132_refresh_widget_caps(codec); |
4558 | ca0132_setup_defaults(codec); | |
4559 | ca0132_init_analog_mic2(codec); | |
4560 | ca0132_init_dmic(codec); | |
4561 | ||
4562 | for (i = 0; i < spec->num_outputs; i++) | |
4563 | init_output(codec, spec->out_pins[i], spec->dacs[0]); | |
01ef7dbf | 4564 | |
95c6e9cb IM |
4565 | init_output(codec, cfg->dig_out_pins[0], spec->dig_out); |
4566 | ||
4567 | for (i = 0; i < spec->num_inputs; i++) | |
4568 | init_input(codec, spec->input_pins[i], spec->adcs[i]); | |
4569 | ||
4570 | init_input(codec, cfg->dig_in_pin, spec->dig_in); | |
4571 | ||
5aaca44d IM |
4572 | for (i = 0; i < spec->num_init_verbs; i++) |
4573 | snd_hda_sequence_write(codec, spec->init_verbs[i]); | |
4574 | ||
a73d511c IM |
4575 | ca0132_init_unsol(codec); |
4576 | ||
5aaca44d IM |
4577 | ca0132_select_out(codec); |
4578 | ca0132_select_mic(codec); | |
4579 | ||
a73d511c IM |
4580 | snd_hda_jack_report_sync(codec); |
4581 | ||
5aaca44d | 4582 | snd_hda_power_down(codec); |
95c6e9cb IM |
4583 | |
4584 | return 0; | |
4585 | } | |
4586 | ||
95c6e9cb IM |
4587 | static void ca0132_free(struct hda_codec *codec) |
4588 | { | |
5aaca44d IM |
4589 | struct ca0132_spec *spec = codec->spec; |
4590 | ||
4591 | snd_hda_power_up(codec); | |
4592 | snd_hda_sequence_write(codec, spec->base_exit_verbs); | |
95c6e9cb | 4593 | ca0132_exit_chip(codec); |
5aaca44d | 4594 | snd_hda_power_down(codec); |
95c6e9cb IM |
4595 | kfree(codec->spec); |
4596 | } | |
4597 | ||
4598 | static struct hda_codec_ops ca0132_patch_ops = { | |
4599 | .build_controls = ca0132_build_controls, | |
4600 | .build_pcms = ca0132_build_pcms, | |
4601 | .init = ca0132_init, | |
4602 | .free = ca0132_free, | |
a73d511c | 4603 | .unsol_event = ca0132_unsol_event, |
95c6e9cb IM |
4604 | }; |
4605 | ||
441aa6a0 IM |
4606 | static void ca0132_config(struct hda_codec *codec) |
4607 | { | |
4608 | struct ca0132_spec *spec = codec->spec; | |
4609 | struct auto_pin_cfg *cfg = &spec->autocfg; | |
4610 | ||
4611 | spec->dacs[0] = 0x2; | |
4612 | spec->dacs[1] = 0x3; | |
4613 | spec->dacs[2] = 0x4; | |
4614 | ||
4615 | spec->multiout.dac_nids = spec->dacs; | |
4616 | spec->multiout.num_dacs = 3; | |
4617 | spec->multiout.max_channels = 2; | |
4618 | ||
4619 | spec->num_outputs = 2; | |
4620 | spec->out_pins[0] = 0x0b; /* speaker out */ | |
4621 | spec->out_pins[1] = 0x10; /* headphone out */ | |
4622 | spec->shared_out_nid = 0x2; | |
4623 | ||
4624 | spec->num_inputs = 3; | |
4625 | spec->adcs[0] = 0x7; /* digital mic / analog mic1 */ | |
4626 | spec->adcs[1] = 0x8; /* analog mic2 */ | |
4627 | spec->adcs[2] = 0xa; /* what u hear */ | |
4628 | spec->shared_mic_nid = 0x7; | |
4629 | ||
4630 | spec->input_pins[0] = 0x12; | |
4631 | spec->input_pins[1] = 0x11; | |
4632 | spec->input_pins[2] = 0x13; | |
4633 | ||
4634 | /* SPDIF I/O */ | |
4635 | spec->dig_out = 0x05; | |
4636 | spec->multiout.dig_out_nid = spec->dig_out; | |
4637 | cfg->dig_out_pins[0] = 0x0c; | |
4638 | cfg->dig_outs = 1; | |
4639 | cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF; | |
4640 | spec->dig_in = 0x09; | |
4641 | cfg->dig_in_pin = 0x0e; | |
4642 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; | |
4643 | } | |
4644 | ||
95c6e9cb IM |
4645 | static int patch_ca0132(struct hda_codec *codec) |
4646 | { | |
4647 | struct ca0132_spec *spec; | |
a73d511c | 4648 | int err; |
95c6e9cb IM |
4649 | |
4650 | snd_printdd("patch_ca0132\n"); | |
4651 | ||
4652 | spec = kzalloc(sizeof(*spec), GFP_KERNEL); | |
4653 | if (!spec) | |
4654 | return -ENOMEM; | |
4655 | codec->spec = spec; | |
4656 | ||
a7e76271 IM |
4657 | spec->num_mixers = 1; |
4658 | spec->mixers[0] = ca0132_mixer; | |
4659 | ||
5aaca44d IM |
4660 | spec->base_init_verbs = ca0132_base_init_verbs; |
4661 | spec->base_exit_verbs = ca0132_base_exit_verbs; | |
4662 | spec->init_verbs[0] = ca0132_init_verbs0; | |
4663 | spec->init_verbs[1] = ca0132_init_verbs1; | |
4664 | spec->num_init_verbs = 2; | |
4665 | ||
95c6e9cb IM |
4666 | ca0132_init_chip(codec); |
4667 | ||
4668 | ca0132_config(codec); | |
4669 | ||
a73d511c IM |
4670 | err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL); |
4671 | if (err < 0) | |
4672 | return err; | |
4673 | ||
95c6e9cb IM |
4674 | codec->patch_ops = ca0132_patch_ops; |
4675 | ||
4676 | return 0; | |
4677 | } | |
4678 | ||
4679 | /* | |
4680 | * patch entries | |
4681 | */ | |
4682 | static struct hda_codec_preset snd_hda_preset_ca0132[] = { | |
4683 | { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 }, | |
4684 | {} /* terminator */ | |
4685 | }; | |
4686 | ||
4687 | MODULE_ALIAS("snd-hda-codec-id:11020011"); | |
4688 | ||
4689 | MODULE_LICENSE("GPL"); | |
406261ce | 4690 | MODULE_DESCRIPTION("Creative Sound Core3D codec"); |
95c6e9cb IM |
4691 | |
4692 | static struct hda_codec_preset_list ca0132_list = { | |
4693 | .preset = snd_hda_preset_ca0132, | |
4694 | .owner = THIS_MODULE, | |
4695 | }; | |
4696 | ||
4697 | static int __init patch_ca0132_init(void) | |
4698 | { | |
4699 | return snd_hda_add_codec_preset(&ca0132_list); | |
4700 | } | |
4701 | ||
4702 | static void __exit patch_ca0132_exit(void) | |
4703 | { | |
c3b4eea2 | 4704 | release_cached_firmware(); |
95c6e9cb IM |
4705 | snd_hda_delete_codec_preset(&ca0132_list); |
4706 | } | |
4707 | ||
4708 | module_init(patch_ca0132_init) | |
4709 | module_exit(patch_ca0132_exit) |