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