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