blob: 710dae81fc8e0f868113b29932dc926cef117847 [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 Iwai24f3ced2013-02-04 18:25:51 +010078#ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
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
Takashi Iwaiccd7bd32013-01-16 09:32:54 +0800390static struct ct_voicefx ca0132_voicefx = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800391 .name = "VoiceFX Capture Switch",
392 .nid = VOICEFX,
393 .mid = 0x95,
394 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
395};
396
Takashi Iwaiccd7bd32013-01-16 09:32:54 +0800397static struct ct_voicefx_preset ca0132_voicefx_presets[] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800398 { .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;
Ian Minett6d675302013-02-08 18:31:43 -0800786 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700787
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;
Ian Minett6d675302013-02-08 18:31:43 -0800794 msleep(20);
795 } while (time_before(jiffies, timeout));
796
Ian Minett95c6e9c2011-06-15 15:35:17 -0700797 return -EIO;
798}
799
800/*
801 * Write chip address through the vendor widget -- NOT protected by the Mutex!
802 */
803static int chipio_write_address(struct hda_codec *codec,
804 unsigned int chip_addx)
805{
Ian Minett4861af82012-09-20 20:29:20 -0700806 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700807 int res;
808
Ian Minett4861af82012-09-20 20:29:20 -0700809 if (spec->curr_chip_addx == chip_addx)
810 return 0;
811
Ian Minett95c6e9c2011-06-15 15:35:17 -0700812 /* send low 16 bits of the address */
813 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
814 chip_addx & 0xffff);
815
816 if (res != -EIO) {
817 /* send high 16 bits of the address */
818 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
819 chip_addx >> 16);
820 }
821
Ian Minett4861af82012-09-20 20:29:20 -0700822 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
823
Ian Minett95c6e9c2011-06-15 15:35:17 -0700824 return res;
825}
826
827/*
828 * Write data through the vendor widget -- NOT protected by the Mutex!
829 */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700830static int chipio_write_data(struct hda_codec *codec, unsigned int data)
831{
Ian Minett5aaca442012-12-20 18:53:34 -0800832 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700833 int res;
834
835 /* send low 16 bits of the data */
836 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
837
838 if (res != -EIO) {
839 /* send high 16 bits of the data */
840 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
841 data >> 16);
842 }
843
Ian Minett5aaca442012-12-20 18:53:34 -0800844 /*If no error encountered, automatically increment the address
845 as per chip behaviour*/
846 spec->curr_chip_addx = (res != -EIO) ?
847 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700848 return res;
849}
850
Ian Minettd5c21b82012-09-20 20:29:18 -0700851/*
852 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
853 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700854static int chipio_write_data_multiple(struct hda_codec *codec,
855 const u32 *data,
856 unsigned int count)
857{
858 int status = 0;
859
860 if (data == NULL) {
861 snd_printdd(KERN_ERR "chipio_write_data null ptr");
862 return -EINVAL;
863 }
864
865 while ((count-- != 0) && (status == 0))
866 status = chipio_write_data(codec, *data++);
867
868 return status;
869}
870
871
Ian Minett95c6e9c2011-06-15 15:35:17 -0700872/*
873 * Read data through the vendor widget -- NOT protected by the Mutex!
874 */
875static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
876{
Ian Minett5aaca442012-12-20 18:53:34 -0800877 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700878 int res;
879
880 /* post read */
881 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
882
883 if (res != -EIO) {
884 /* read status */
885 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
886 }
887
888 if (res != -EIO) {
889 /* read data */
890 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
891 VENDOR_CHIPIO_HIC_READ_DATA,
892 0);
893 }
894
Ian Minett5aaca442012-12-20 18:53:34 -0800895 /*If no error encountered, automatically increment the address
896 as per chip behaviour*/
897 spec->curr_chip_addx = (res != -EIO) ?
898 (spec->curr_chip_addx + 4) : ~0UL;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700899 return res;
900}
901
902/*
903 * Write given value to the given address through the chip I/O widget.
904 * protected by the Mutex
905 */
906static int chipio_write(struct hda_codec *codec,
907 unsigned int chip_addx, const unsigned int data)
908{
909 struct ca0132_spec *spec = codec->spec;
910 int err;
911
912 mutex_lock(&spec->chipio_mutex);
913
914 /* write the address, and if successful proceed to write data */
915 err = chipio_write_address(codec, chip_addx);
916 if (err < 0)
917 goto exit;
918
919 err = chipio_write_data(codec, data);
920 if (err < 0)
921 goto exit;
922
923exit:
924 mutex_unlock(&spec->chipio_mutex);
925 return err;
926}
927
Ian Minettd5c21b82012-09-20 20:29:18 -0700928/*
929 * Write multiple values to the given address through the chip I/O widget.
930 * protected by the Mutex
931 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700932static int chipio_write_multiple(struct hda_codec *codec,
933 u32 chip_addx,
934 const u32 *data,
935 unsigned int count)
936{
937 struct ca0132_spec *spec = codec->spec;
938 int status;
939
940 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -0700941 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -0700942 if (status < 0)
943 goto error;
944
945 status = chipio_write_data_multiple(codec, data, count);
946error:
947 mutex_unlock(&spec->chipio_mutex);
948
949 return status;
950}
951
Ian Minett95c6e9c2011-06-15 15:35:17 -0700952/*
953 * Read the given address through the chip I/O widget
954 * protected by the Mutex
955 */
956static int chipio_read(struct hda_codec *codec,
957 unsigned int chip_addx, unsigned int *data)
958{
959 struct ca0132_spec *spec = codec->spec;
960 int err;
961
962 mutex_lock(&spec->chipio_mutex);
963
964 /* write the address, and if successful proceed to write data */
965 err = chipio_write_address(codec, chip_addx);
966 if (err < 0)
967 goto exit;
968
969 err = chipio_read_data(codec, data);
970 if (err < 0)
971 goto exit;
972
973exit:
974 mutex_unlock(&spec->chipio_mutex);
975 return err;
976}
977
Ian Minettd5c21b82012-09-20 20:29:18 -0700978/*
979 * Set chip control flags through the chip I/O widget.
980 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700981static void chipio_set_control_flag(struct hda_codec *codec,
982 enum control_flag_id flag_id,
983 bool flag_state)
984{
985 unsigned int val;
986 unsigned int flag_bit;
987
988 flag_bit = (flag_state ? 1 : 0);
989 val = (flag_bit << 7) | (flag_id);
990 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
991 VENDOR_CHIPIO_FLAG_SET, val);
992}
993
Ian Minettd5c21b82012-09-20 20:29:18 -0700994/*
995 * Set chip parameters through the chip I/O widget.
996 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700997static void chipio_set_control_param(struct hda_codec *codec,
998 enum control_param_id param_id, int param_val)
999{
1000 struct ca0132_spec *spec = codec->spec;
1001 int val;
1002
1003 if ((param_id < 32) && (param_val < 8)) {
1004 val = (param_val << 5) | (param_id);
1005 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1006 VENDOR_CHIPIO_PARAM_SET, val);
1007 } else {
1008 mutex_lock(&spec->chipio_mutex);
1009 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1010 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1011 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1012 param_id);
1013 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1014 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1015 param_val);
1016 }
1017 mutex_unlock(&spec->chipio_mutex);
1018 }
1019}
1020
Ian Minettd5c21b82012-09-20 20:29:18 -07001021/*
1022 * Set sampling rate of the connection point.
1023 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001024static void chipio_set_conn_rate(struct hda_codec *codec,
1025 int connid, enum ca0132_sample_rate rate)
1026{
1027 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1028 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1029 rate);
1030}
1031
Ian Minettd5c21b82012-09-20 20:29:18 -07001032/*
1033 * Enable clocks.
1034 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001035static void chipio_enable_clocks(struct hda_codec *codec)
1036{
1037 struct ca0132_spec *spec = codec->spec;
1038
1039 mutex_lock(&spec->chipio_mutex);
1040 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1041 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1042 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1043 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1044 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1045 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1046 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1047 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1048 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1049 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1050 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1051 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1052 mutex_unlock(&spec->chipio_mutex);
1053}
1054
1055/*
1056 * CA0132 DSP IO stuffs
1057 */
1058static int dspio_send(struct hda_codec *codec, unsigned int reg,
1059 unsigned int data)
1060{
Takashi Iwaib645d792013-01-15 17:39:29 +01001061 int res;
Ian Minett6d675302013-02-08 18:31:43 -08001062 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001063
1064 /* send bits of data specified by reg to dsp */
1065 do {
1066 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1067 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1068 return res;
Ian Minett6d675302013-02-08 18:31:43 -08001069 msleep(20);
1070 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001071
1072 return -EIO;
1073}
1074
Ian Minettd5c21b82012-09-20 20:29:18 -07001075/*
1076 * Wait for DSP to be ready for commands
1077 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001078static void dspio_write_wait(struct hda_codec *codec)
1079{
Ian Minett4861af82012-09-20 20:29:20 -07001080 int status;
1081 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001082
Ian Minett01ef7db2012-09-20 20:29:16 -07001083 do {
Ian Minett4861af82012-09-20 20:29:20 -07001084 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1085 VENDOR_DSPIO_STATUS, 0);
1086 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1087 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1088 break;
1089 msleep(1);
1090 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001091}
1092
Ian Minettd5c21b82012-09-20 20:29:18 -07001093/*
1094 * Write SCP data to DSP
1095 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001096static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1097{
1098 struct ca0132_spec *spec = codec->spec;
1099 int status;
1100
1101 dspio_write_wait(codec);
1102
1103 mutex_lock(&spec->chipio_mutex);
1104 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1105 scp_data & 0xffff);
1106 if (status < 0)
1107 goto error;
1108
1109 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1110 scp_data >> 16);
1111 if (status < 0)
1112 goto error;
1113
1114 /* OK, now check if the write itself has executed*/
1115 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1116 VENDOR_DSPIO_STATUS, 0);
1117error:
1118 mutex_unlock(&spec->chipio_mutex);
1119
1120 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1121 -EIO : 0;
1122}
1123
Ian Minettd5c21b82012-09-20 20:29:18 -07001124/*
1125 * Write multiple SCP data to DSP
1126 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001127static int dspio_write_multiple(struct hda_codec *codec,
1128 unsigned int *buffer, unsigned int size)
1129{
1130 int status = 0;
1131 unsigned int count;
1132
1133 if ((buffer == NULL))
1134 return -EINVAL;
1135
1136 count = 0;
1137 while (count < size) {
1138 status = dspio_write(codec, *buffer++);
1139 if (status != 0)
1140 break;
1141 count++;
1142 }
1143
1144 return status;
1145}
1146
Ian Minetta73d5112012-12-20 18:53:37 -08001147static int dspio_read(struct hda_codec *codec, unsigned int *data)
1148{
1149 int status;
1150
1151 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1152 if (status == -EIO)
1153 return status;
1154
1155 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1156 if (status == -EIO ||
1157 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1158 return -EIO;
1159
1160 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1161 VENDOR_DSPIO_SCP_READ_DATA, 0);
1162
1163 return 0;
1164}
1165
1166static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1167 unsigned int *buf_size, unsigned int size_count)
1168{
1169 int status = 0;
1170 unsigned int size = *buf_size;
1171 unsigned int count;
1172 unsigned int skip_count;
1173 unsigned int dummy;
1174
1175 if ((buffer == NULL))
1176 return -1;
1177
1178 count = 0;
1179 while (count < size && count < size_count) {
1180 status = dspio_read(codec, buffer++);
1181 if (status != 0)
1182 break;
1183 count++;
1184 }
1185
1186 skip_count = count;
1187 if (status == 0) {
1188 while (skip_count < size) {
1189 status = dspio_read(codec, &dummy);
1190 if (status != 0)
1191 break;
1192 skip_count++;
1193 }
1194 }
1195 *buf_size = count;
1196
1197 return status;
1198}
1199
Ian Minettd5c21b82012-09-20 20:29:18 -07001200/*
1201 * Construct the SCP header using corresponding fields
1202 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001203static inline unsigned int
1204make_scp_header(unsigned int target_id, unsigned int source_id,
1205 unsigned int get_flag, unsigned int req,
1206 unsigned int device_flag, unsigned int resp_flag,
1207 unsigned int error_flag, unsigned int data_size)
1208{
1209 unsigned int header = 0;
1210
1211 header = (data_size & 0x1f) << 27;
1212 header |= (error_flag & 0x01) << 26;
1213 header |= (resp_flag & 0x01) << 25;
1214 header |= (device_flag & 0x01) << 24;
1215 header |= (req & 0x7f) << 17;
1216 header |= (get_flag & 0x01) << 16;
1217 header |= (source_id & 0xff) << 8;
1218 header |= target_id & 0xff;
1219
1220 return header;
1221}
1222
Ian Minettd5c21b82012-09-20 20:29:18 -07001223/*
1224 * Extract corresponding fields from SCP header
1225 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001226static inline void
1227extract_scp_header(unsigned int header,
1228 unsigned int *target_id, unsigned int *source_id,
1229 unsigned int *get_flag, unsigned int *req,
1230 unsigned int *device_flag, unsigned int *resp_flag,
1231 unsigned int *error_flag, unsigned int *data_size)
1232{
1233 if (data_size)
1234 *data_size = (header >> 27) & 0x1f;
1235 if (error_flag)
1236 *error_flag = (header >> 26) & 0x01;
1237 if (resp_flag)
1238 *resp_flag = (header >> 25) & 0x01;
1239 if (device_flag)
1240 *device_flag = (header >> 24) & 0x01;
1241 if (req)
1242 *req = (header >> 17) & 0x7f;
1243 if (get_flag)
1244 *get_flag = (header >> 16) & 0x01;
1245 if (source_id)
1246 *source_id = (header >> 8) & 0xff;
1247 if (target_id)
1248 *target_id = header & 0xff;
1249}
1250
1251#define SCP_MAX_DATA_WORDS (16)
1252
1253/* Structure to contain any SCP message */
1254struct scp_msg {
1255 unsigned int hdr;
1256 unsigned int data[SCP_MAX_DATA_WORDS];
1257};
1258
Ian Minetta73d5112012-12-20 18:53:37 -08001259static void dspio_clear_response_queue(struct hda_codec *codec)
1260{
1261 unsigned int dummy = 0;
1262 int status = -1;
1263
1264 /* clear all from the response queue */
1265 do {
1266 status = dspio_read(codec, &dummy);
1267 } while (status == 0);
1268}
1269
1270static int dspio_get_response_data(struct hda_codec *codec)
1271{
1272 struct ca0132_spec *spec = codec->spec;
1273 unsigned int data = 0;
1274 unsigned int count;
1275
1276 if (dspio_read(codec, &data) < 0)
1277 return -EIO;
1278
1279 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1280 spec->scp_resp_header = data;
1281 spec->scp_resp_count = data >> 27;
1282 count = spec->wait_num_data;
1283 dspio_read_multiple(codec, spec->scp_resp_data,
1284 &spec->scp_resp_count, count);
1285 return 0;
1286 }
1287
1288 return -EIO;
1289}
1290
Ian Minettd5c21b82012-09-20 20:29:18 -07001291/*
1292 * Send SCP message to DSP
1293 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001294static int dspio_send_scp_message(struct hda_codec *codec,
1295 unsigned char *send_buf,
1296 unsigned int send_buf_size,
1297 unsigned char *return_buf,
1298 unsigned int return_buf_size,
1299 unsigned int *bytes_returned)
1300{
1301 struct ca0132_spec *spec = codec->spec;
Ian Minett01ef7db2012-09-20 20:29:16 -07001302 int status = -1;
1303 unsigned int scp_send_size = 0;
1304 unsigned int total_size;
1305 bool waiting_for_resp = false;
1306 unsigned int header;
1307 struct scp_msg *ret_msg;
1308 unsigned int resp_src_id, resp_target_id;
1309 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1310
1311 if (bytes_returned)
1312 *bytes_returned = 0;
1313
1314 /* get scp header from buffer */
1315 header = *((unsigned int *)send_buf);
1316 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1317 &device_flag, NULL, NULL, &data_size);
1318 scp_send_size = data_size + 1;
1319 total_size = (scp_send_size * 4);
1320
1321 if (send_buf_size < total_size)
1322 return -EINVAL;
1323
1324 if (get_flag || device_flag) {
1325 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1326 return -EINVAL;
1327
1328 spec->wait_scp_header = *((unsigned int *)send_buf);
1329
1330 /* swap source id with target id */
1331 resp_target_id = src_id;
1332 resp_src_id = target_id;
1333 spec->wait_scp_header &= 0xffff0000;
1334 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1335 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1336 spec->wait_scp = 1;
1337 waiting_for_resp = true;
1338 }
1339
1340 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1341 scp_send_size);
1342 if (status < 0) {
1343 spec->wait_scp = 0;
1344 return status;
1345 }
1346
1347 if (waiting_for_resp) {
Ian Minett6d675302013-02-08 18:31:43 -08001348 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001349 memset(return_buf, 0, return_buf_size);
Ian Minett01ef7db2012-09-20 20:29:16 -07001350 do {
1351 msleep(20);
Ian Minett6d675302013-02-08 18:31:43 -08001352 } while (spec->wait_scp && time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001353 waiting_for_resp = false;
Ian Minett6d675302013-02-08 18:31:43 -08001354 if (!spec->wait_scp) {
Ian Minett01ef7db2012-09-20 20:29:16 -07001355 ret_msg = (struct scp_msg *)return_buf;
1356 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1357 memcpy(&ret_msg->data, spec->scp_resp_data,
1358 spec->wait_num_data);
1359 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1360 status = 0;
1361 } else {
1362 status = -EIO;
1363 }
1364 spec->wait_scp = 0;
1365 }
1366
1367 return status;
1368}
1369
Ian Minettd5c21b82012-09-20 20:29:18 -07001370/**
1371 * Prepare and send the SCP message to DSP
1372 * @codec: the HDA codec
1373 * @mod_id: ID of the DSP module to send the command
1374 * @req: ID of request to send to the DSP module
1375 * @dir: SET or GET
1376 * @data: pointer to the data to send with the request, request specific
1377 * @len: length of the data, in bytes
1378 * @reply: point to the buffer to hold data returned for a reply
1379 * @reply_len: length of the reply buffer returned from GET
1380 *
1381 * Returns zero or a negative error code.
1382 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001383static int dspio_scp(struct hda_codec *codec,
1384 int mod_id, int req, int dir, void *data, unsigned int len,
1385 void *reply, unsigned int *reply_len)
1386{
1387 int status = 0;
1388 struct scp_msg scp_send, scp_reply;
1389 unsigned int ret_bytes, send_size, ret_size;
1390 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1391 unsigned int reply_data_size;
1392
1393 memset(&scp_send, 0, sizeof(scp_send));
1394 memset(&scp_reply, 0, sizeof(scp_reply));
1395
1396 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1397 return -EINVAL;
1398
1399 if (dir == SCP_GET && reply == NULL) {
1400 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
1401 return -EINVAL;
1402 }
1403
1404 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1405 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
1406 return -EINVAL;
1407 }
1408
1409 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1410 0, 0, 0, len/sizeof(unsigned int));
1411 if (data != NULL && len > 0) {
1412 len = min((unsigned int)(sizeof(scp_send.data)), len);
1413 memcpy(scp_send.data, data, len);
1414 }
1415
1416 ret_bytes = 0;
1417 send_size = sizeof(unsigned int) + len;
1418 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1419 send_size, (unsigned char *)&scp_reply,
1420 sizeof(scp_reply), &ret_bytes);
1421
1422 if (status < 0) {
1423 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1424 return status;
1425 }
1426
1427 /* extract send and reply headers members */
1428 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1429 NULL, NULL, NULL, NULL, NULL);
1430 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1431 &reply_resp_flag, &reply_error_flag,
1432 &reply_data_size);
1433
1434 if (!send_get_flag)
1435 return 0;
1436
1437 if (reply_resp_flag && !reply_error_flag) {
1438 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1439 / sizeof(unsigned int);
1440
1441 if (*reply_len < ret_size*sizeof(unsigned int)) {
1442 snd_printdd(KERN_ERR "reply too long for buf");
1443 return -EINVAL;
1444 } else if (ret_size != reply_data_size) {
1445 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1446 return -EINVAL;
1447 } else {
1448 *reply_len = ret_size*sizeof(unsigned int);
1449 memcpy(reply, scp_reply.data, *reply_len);
1450 }
1451 } else {
1452 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1453 return -EIO;
1454 }
1455
1456 return status;
1457}
1458
Ian Minettd5c21b82012-09-20 20:29:18 -07001459/*
Ian Minett5aaca442012-12-20 18:53:34 -08001460 * Set DSP parameters
1461 */
1462static int dspio_set_param(struct hda_codec *codec, int mod_id,
1463 int req, void *data, unsigned int len)
1464{
1465 return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
1466}
1467
1468static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
1469 int req, unsigned int data)
1470{
1471 return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
1472}
1473
1474/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001475 * Allocate a DSP DMA channel via an SCP message
1476 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001477static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1478{
1479 int status = 0;
1480 unsigned int size = sizeof(dma_chan);
1481
1482 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1483 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1484 SCP_GET, NULL, 0, dma_chan, &size);
1485
1486 if (status < 0) {
1487 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1488 return status;
1489 }
1490
1491 if ((*dma_chan + 1) == 0) {
1492 snd_printdd(KERN_INFO "no free dma channels to allocate");
1493 return -EBUSY;
1494 }
1495
1496 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1497 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1498
1499 return status;
1500}
1501
Ian Minettd5c21b82012-09-20 20:29:18 -07001502/*
1503 * Free a DSP DMA via an SCP message
1504 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001505static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1506{
1507 int status = 0;
1508 unsigned int dummy = 0;
1509
1510 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1511 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1512
1513 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1514 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1515
1516 if (status < 0) {
1517 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1518 return status;
1519 }
1520
1521 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1522
1523 return status;
1524}
1525
1526/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001527 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001528 */
1529static int dsp_set_run_state(struct hda_codec *codec)
1530{
1531 unsigned int dbg_ctrl_reg;
1532 unsigned int halt_state;
1533 int err;
1534
1535 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1536 if (err < 0)
1537 return err;
1538
1539 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1540 DSP_DBGCNTL_STATE_LOBIT;
1541
1542 if (halt_state != 0) {
1543 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1544 DSP_DBGCNTL_SS_MASK);
1545 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1546 dbg_ctrl_reg);
1547 if (err < 0)
1548 return err;
1549
1550 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1551 DSP_DBGCNTL_EXEC_MASK;
1552 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1553 dbg_ctrl_reg);
1554 if (err < 0)
1555 return err;
1556 }
1557
1558 return 0;
1559}
1560
Ian Minettd5c21b82012-09-20 20:29:18 -07001561/*
1562 * Reset the DSP
1563 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001564static int dsp_reset(struct hda_codec *codec)
1565{
1566 unsigned int res;
1567 int retry = 20;
1568
1569 snd_printdd("dsp_reset\n");
1570 do {
1571 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1572 retry--;
1573 } while (res == -EIO && retry);
1574
1575 if (!retry) {
1576 snd_printdd("dsp_reset timeout\n");
1577 return -EIO;
1578 }
1579
1580 return 0;
1581}
1582
Ian Minettd5c21b82012-09-20 20:29:18 -07001583/*
1584 * Convert chip address to DSP address
1585 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001586static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1587 bool *code, bool *yram)
1588{
1589 *code = *yram = false;
1590
1591 if (UC_RANGE(chip_addx, 1)) {
1592 *code = true;
1593 return UC_OFF(chip_addx);
1594 } else if (X_RANGE_ALL(chip_addx, 1)) {
1595 return X_OFF(chip_addx);
1596 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1597 *yram = true;
1598 return Y_OFF(chip_addx);
1599 }
1600
1601 return (unsigned int)INVALID_CHIP_ADDRESS;
1602}
1603
Ian Minettd5c21b82012-09-20 20:29:18 -07001604/*
1605 * Check if the DSP DMA is active
1606 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001607static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1608{
1609 unsigned int dma_chnlstart_reg;
1610
1611 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1612
1613 return ((dma_chnlstart_reg & (1 <<
1614 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1615}
1616
1617static int dsp_dma_setup_common(struct hda_codec *codec,
1618 unsigned int chip_addx,
1619 unsigned int dma_chan,
1620 unsigned int port_map_mask,
1621 bool ovly)
1622{
1623 int status = 0;
1624 unsigned int chnl_prop;
1625 unsigned int dsp_addx;
1626 unsigned int active;
1627 bool code, yram;
1628
1629 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1630
1631 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1632 snd_printdd(KERN_ERR "dma chan num invalid");
1633 return -EINVAL;
1634 }
1635
1636 if (dsp_is_dma_active(codec, dma_chan)) {
1637 snd_printdd(KERN_ERR "dma already active");
1638 return -EBUSY;
1639 }
1640
1641 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1642
1643 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1644 snd_printdd(KERN_ERR "invalid chip addr");
1645 return -ENXIO;
1646 }
1647
1648 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1649 active = 0;
1650
1651 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1652
1653 if (ovly) {
1654 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1655 &chnl_prop);
1656
1657 if (status < 0) {
1658 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1659 return status;
1660 }
1661 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1662 }
1663
1664 if (!code)
1665 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1666 else
1667 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1668
1669 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1670
1671 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1672 if (status < 0) {
1673 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1674 return status;
1675 }
1676 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1677
1678 if (ovly) {
1679 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1680 &active);
1681
1682 if (status < 0) {
1683 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1684 return status;
1685 }
1686 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1687 }
1688
1689 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1690 DSPDMAC_ACTIVE_AAR_MASK;
1691
1692 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1693 if (status < 0) {
1694 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1695 return status;
1696 }
1697
1698 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1699
1700 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1701 port_map_mask);
1702 if (status < 0) {
1703 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1704 return status;
1705 }
1706 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1707
1708 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1709 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1710 if (status < 0) {
1711 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1712 return status;
1713 }
1714 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1715
1716 snd_printdd(
1717 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1718 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1719 chip_addx, dsp_addx, dma_chan,
1720 port_map_mask, chnl_prop, active);
1721
1722 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1723
1724 return 0;
1725}
1726
Ian Minettd5c21b82012-09-20 20:29:18 -07001727/*
1728 * Setup the DSP DMA per-transfer-specific registers
1729 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001730static int dsp_dma_setup(struct hda_codec *codec,
1731 unsigned int chip_addx,
1732 unsigned int count,
1733 unsigned int dma_chan)
1734{
1735 int status = 0;
1736 bool code, yram;
1737 unsigned int dsp_addx;
1738 unsigned int addr_field;
1739 unsigned int incr_field;
1740 unsigned int base_cnt;
1741 unsigned int cur_cnt;
1742 unsigned int dma_cfg = 0;
1743 unsigned int adr_ofs = 0;
1744 unsigned int xfr_cnt = 0;
1745 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1746 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1747
1748 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1749
1750 if (count > max_dma_count) {
1751 snd_printdd(KERN_ERR "count too big");
1752 return -EINVAL;
1753 }
1754
1755 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1756 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1757 snd_printdd(KERN_ERR "invalid chip addr");
1758 return -ENXIO;
1759 }
1760
1761 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1762
1763 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1764 incr_field = 0;
1765
1766 if (!code) {
1767 addr_field <<= 1;
1768 if (yram)
1769 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1770
1771 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1772 }
1773
1774 dma_cfg = addr_field + incr_field;
1775 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1776 dma_cfg);
1777 if (status < 0) {
1778 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1779 return status;
1780 }
1781 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1782
1783 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1784 (code ? 0 : 1));
1785
1786 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1787 adr_ofs);
1788 if (status < 0) {
1789 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1790 return status;
1791 }
1792 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1793
1794 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1795
1796 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1797
1798 xfr_cnt = base_cnt | cur_cnt;
1799
1800 status = chipio_write(codec,
1801 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1802 if (status < 0) {
1803 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1804 return status;
1805 }
1806 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1807
1808 snd_printdd(
1809 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1810 "ADROFS=0x%x, XFRCNT=0x%x\n",
1811 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1812
1813 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1814
1815 return 0;
1816}
1817
Ian Minettd5c21b82012-09-20 20:29:18 -07001818/*
1819 * Start the DSP DMA
1820 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001821static int dsp_dma_start(struct hda_codec *codec,
1822 unsigned int dma_chan, bool ovly)
1823{
1824 unsigned int reg = 0;
1825 int status = 0;
1826
1827 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1828
1829 if (ovly) {
1830 status = chipio_read(codec,
1831 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1832
1833 if (status < 0) {
1834 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1835 return status;
1836 }
1837 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1838
1839 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1840 DSPDMAC_CHNLSTART_DIS_MASK);
1841 }
1842
1843 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1844 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1845 if (status < 0) {
1846 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1847 return status;
1848 }
1849 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1850
1851 return status;
1852}
1853
Ian Minettd5c21b82012-09-20 20:29:18 -07001854/*
1855 * Stop the DSP DMA
1856 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001857static int dsp_dma_stop(struct hda_codec *codec,
1858 unsigned int dma_chan, bool ovly)
1859{
1860 unsigned int reg = 0;
1861 int status = 0;
1862
1863 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1864
1865 if (ovly) {
1866 status = chipio_read(codec,
1867 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1868
1869 if (status < 0) {
1870 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1871 return status;
1872 }
1873 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1874 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1875 DSPDMAC_CHNLSTART_DIS_MASK);
1876 }
1877
1878 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1879 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1880 if (status < 0) {
1881 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1882 return status;
1883 }
1884 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1885
1886 return status;
1887}
1888
Ian Minettd5c21b82012-09-20 20:29:18 -07001889/**
1890 * Allocate router ports
1891 *
1892 * @codec: the HDA codec
1893 * @num_chans: number of channels in the stream
1894 * @ports_per_channel: number of ports per channel
1895 * @start_device: start device
1896 * @port_map: pointer to the port list to hold the allocated ports
1897 *
1898 * Returns zero or a negative error code.
1899 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001900static int dsp_allocate_router_ports(struct hda_codec *codec,
1901 unsigned int num_chans,
1902 unsigned int ports_per_channel,
1903 unsigned int start_device,
1904 unsigned int *port_map)
1905{
1906 int status = 0;
1907 int res;
1908 u8 val;
1909
1910 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1911 if (status < 0)
1912 return status;
1913
1914 val = start_device << 6;
1915 val |= (ports_per_channel - 1) << 4;
1916 val |= num_chans - 1;
1917
1918 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1919 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1920 val);
1921
1922 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1923 VENDOR_CHIPIO_PORT_ALLOC_SET,
1924 MEM_CONNID_DSP);
1925
1926 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1927 if (status < 0)
1928 return status;
1929
1930 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1931 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1932
1933 *port_map = res;
1934
1935 return (res < 0) ? res : 0;
1936}
1937
Ian Minettd5c21b82012-09-20 20:29:18 -07001938/*
1939 * Free router ports
1940 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001941static int dsp_free_router_ports(struct hda_codec *codec)
1942{
1943 int status = 0;
1944
1945 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1946 if (status < 0)
1947 return status;
1948
1949 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1950 VENDOR_CHIPIO_PORT_FREE_SET,
1951 MEM_CONNID_DSP);
1952
1953 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1954
1955 return status;
1956}
1957
Ian Minettd5c21b82012-09-20 20:29:18 -07001958/*
1959 * Allocate DSP ports for the download stream
1960 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001961static int dsp_allocate_ports(struct hda_codec *codec,
1962 unsigned int num_chans,
1963 unsigned int rate_multi, unsigned int *port_map)
1964{
1965 int status;
1966
1967 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1968
1969 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1970 snd_printdd(KERN_ERR "bad rate multiple");
1971 return -EINVAL;
1972 }
1973
1974 status = dsp_allocate_router_ports(codec, num_chans,
1975 rate_multi, 0, port_map);
1976
1977 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1978
1979 return status;
1980}
1981
Ian Minett01ef7db2012-09-20 20:29:16 -07001982static int dsp_allocate_ports_format(struct hda_codec *codec,
1983 const unsigned short fmt,
1984 unsigned int *port_map)
1985{
1986 int status;
1987 unsigned int num_chans;
1988
1989 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1990 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1991 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1992
1993 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1994 snd_printdd(KERN_ERR "bad rate multiple");
1995 return -EINVAL;
1996 }
1997
1998 num_chans = get_hdafmt_chs(fmt) + 1;
1999
2000 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2001
2002 return status;
2003}
2004
2005/*
Ian Minettd5c21b82012-09-20 20:29:18 -07002006 * free DSP ports
2007 */
2008static int dsp_free_ports(struct hda_codec *codec)
2009{
2010 int status;
2011
2012 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
2013
2014 status = dsp_free_router_ports(codec);
2015 if (status < 0) {
2016 snd_printdd(KERN_ERR "free router ports fail");
2017 return status;
2018 }
2019 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
2020
2021 return status;
2022}
2023
2024/*
Ian Minett01ef7db2012-09-20 20:29:16 -07002025 * HDA DMA engine stuffs for DSP code download
2026 */
2027struct dma_engine {
2028 struct hda_codec *codec;
2029 unsigned short m_converter_format;
2030 struct snd_dma_buffer *dmab;
2031 unsigned int buf_size;
2032};
2033
2034
2035enum dma_state {
2036 DMA_STATE_STOP = 0,
2037 DMA_STATE_RUN = 1
2038};
2039
2040static int dma_convert_to_hda_format(
Ian Minette97249d2012-09-20 20:29:21 -07002041 unsigned int sample_rate,
2042 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002043 unsigned short *hda_format)
2044{
2045 unsigned int format_val;
2046
2047 format_val = snd_hda_calc_stream_format(
Ian Minette97249d2012-09-20 20:29:21 -07002048 sample_rate,
2049 channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002050 SNDRV_PCM_FORMAT_S32_LE,
Ian Minette97249d2012-09-20 20:29:21 -07002051 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07002052
2053 if (hda_format)
2054 *hda_format = (unsigned short)format_val;
2055
2056 return 0;
2057}
2058
Ian Minettd5c21b82012-09-20 20:29:18 -07002059/*
2060 * Reset DMA for DSP download
2061 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002062static int dma_reset(struct dma_engine *dma)
2063{
2064 struct hda_codec *codec = dma->codec;
2065 struct ca0132_spec *spec = codec->spec;
2066 int status;
2067
2068 if (dma->dmab)
2069 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2070
2071 status = snd_hda_codec_load_dsp_prepare(codec,
2072 dma->m_converter_format,
2073 dma->buf_size,
2074 dma->dmab);
2075 if (status < 0)
2076 return status;
2077 spec->dsp_stream_id = status;
2078 return 0;
2079}
2080
2081static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2082{
2083 bool cmd;
2084
2085 snd_printdd("dma_set_state state=%d\n", state);
2086
2087 switch (state) {
2088 case DMA_STATE_STOP:
2089 cmd = false;
2090 break;
2091 case DMA_STATE_RUN:
2092 cmd = true;
2093 break;
2094 default:
2095 return 0;
2096 }
2097
2098 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2099 return 0;
2100}
2101
2102static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2103{
2104 return dma->dmab->bytes;
2105}
2106
2107static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2108{
2109 return dma->dmab->area;
2110}
2111
2112static int dma_xfer(struct dma_engine *dma,
2113 const unsigned int *data,
2114 unsigned int count)
2115{
2116 memcpy(dma->dmab->area, data, count);
2117 return 0;
2118}
2119
2120static void dma_get_converter_format(
2121 struct dma_engine *dma,
2122 unsigned short *format)
2123{
2124 if (format)
2125 *format = dma->m_converter_format;
2126}
2127
2128static unsigned int dma_get_stream_id(struct dma_engine *dma)
2129{
2130 struct ca0132_spec *spec = dma->codec->spec;
2131
2132 return spec->dsp_stream_id;
2133}
2134
2135struct dsp_image_seg {
2136 u32 magic;
2137 u32 chip_addr;
2138 u32 count;
2139 u32 data[0];
2140};
2141
2142static const u32 g_magic_value = 0x4c46584d;
2143static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2144
2145static bool is_valid(const struct dsp_image_seg *p)
2146{
2147 return p->magic == g_magic_value;
2148}
2149
2150static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2151{
2152 return g_chip_addr_magic_value == p->chip_addr;
2153}
2154
2155static bool is_last(const struct dsp_image_seg *p)
2156{
2157 return p->count == 0;
2158}
2159
2160static size_t dsp_sizeof(const struct dsp_image_seg *p)
2161{
2162 return sizeof(*p) + p->count*sizeof(u32);
2163}
2164
2165static const struct dsp_image_seg *get_next_seg_ptr(
2166 const struct dsp_image_seg *p)
2167{
2168 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2169}
2170
2171/*
2172 * CA0132 chip DSP transfer stuffs. For DSP download.
2173 */
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002174#define INVALID_DMA_CHANNEL (~0U)
Ian Minett01ef7db2012-09-20 20:29:16 -07002175
Ian Minettd5c21b82012-09-20 20:29:18 -07002176/*
2177 * Program a list of address/data pairs via the ChipIO widget.
2178 * The segment data is in the format of successive pairs of words.
2179 * These are repeated as indicated by the segment's count field.
2180 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002181static int dspxfr_hci_write(struct hda_codec *codec,
2182 const struct dsp_image_seg *fls)
2183{
2184 int status;
2185 const u32 *data;
2186 unsigned int count;
2187
2188 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2189 snd_printdd(KERN_ERR "hci_write invalid params");
2190 return -EINVAL;
2191 }
2192
2193 count = fls->count;
2194 data = (u32 *)(fls->data);
2195 while (count >= 2) {
2196 status = chipio_write(codec, data[0], data[1]);
2197 if (status < 0) {
2198 snd_printdd(KERN_ERR "hci_write chipio failed");
2199 return status;
2200 }
2201 count -= 2;
2202 data += 2;
2203 }
2204 return 0;
2205}
2206
Ian Minettd5c21b82012-09-20 20:29:18 -07002207/**
2208 * Write a block of data into DSP code or data RAM using pre-allocated
2209 * DMA engine.
2210 *
2211 * @codec: the HDA codec
2212 * @fls: pointer to a fast load image
2213 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2214 * no relocation
2215 * @dma_engine: pointer to DMA engine to be used for DSP download
2216 * @dma_chan: The number of DMA channels used for DSP download
2217 * @port_map_mask: port mapping
2218 * @ovly: TRUE if overlay format is required
2219 *
2220 * Returns zero or a negative error code.
2221 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002222static int dspxfr_one_seg(struct hda_codec *codec,
2223 const struct dsp_image_seg *fls,
2224 unsigned int reloc,
2225 struct dma_engine *dma_engine,
2226 unsigned int dma_chan,
2227 unsigned int port_map_mask,
2228 bool ovly)
2229{
Ian Minett406261c2012-12-20 18:53:41 -08002230 int status = 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002231 bool comm_dma_setup_done = false;
2232 const unsigned int *data;
2233 unsigned int chip_addx;
2234 unsigned int words_to_write;
2235 unsigned int buffer_size_words;
2236 unsigned char *buffer_addx;
2237 unsigned short hda_format;
2238 unsigned int sample_rate_div;
2239 unsigned int sample_rate_mul;
2240 unsigned int num_chans;
2241 unsigned int hda_frame_size_words;
2242 unsigned int remainder_words;
2243 const u32 *data_remainder;
2244 u32 chip_addx_remainder;
2245 unsigned int run_size_words;
2246 const struct dsp_image_seg *hci_write = NULL;
Ian Minett6d675302013-02-08 18:31:43 -08002247 unsigned long timeout;
2248 bool dma_active;
Ian Minett01ef7db2012-09-20 20:29:16 -07002249
2250 if (fls == NULL)
2251 return -EINVAL;
2252 if (is_hci_prog_list_seg(fls)) {
2253 hci_write = fls;
2254 fls = get_next_seg_ptr(fls);
2255 }
2256
2257 if (hci_write && (!fls || is_last(fls))) {
2258 snd_printdd("hci_write\n");
2259 return dspxfr_hci_write(codec, hci_write);
2260 }
2261
2262 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2263 snd_printdd("Invalid Params\n");
2264 return -EINVAL;
2265 }
2266
2267 data = fls->data;
2268 chip_addx = fls->chip_addr,
2269 words_to_write = fls->count;
2270
2271 if (!words_to_write)
2272 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2273 if (reloc)
2274 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2275
2276 if (!UC_RANGE(chip_addx, words_to_write) &&
2277 !X_RANGE_ALL(chip_addx, words_to_write) &&
2278 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2279 snd_printdd("Invalid chip_addx Params\n");
2280 return -EINVAL;
2281 }
2282
2283 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2284 sizeof(u32);
2285
2286 buffer_addx = dma_get_buffer_addr(dma_engine);
2287
2288 if (buffer_addx == NULL) {
2289 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
2290 return -EINVAL;
2291 }
2292
2293 dma_get_converter_format(dma_engine, &hda_format);
2294 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2295 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2296 num_chans = get_hdafmt_chs(hda_format) + 1;
2297
2298 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2299 (num_chans * sample_rate_mul / sample_rate_div));
2300
2301 buffer_size_words = min(buffer_size_words,
2302 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2303 65536 : 32768));
2304 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2305 snd_printdd(
2306 "chpadr=0x%08x frmsz=%u nchan=%u "
2307 "rate_mul=%u div=%u bufsz=%u\n",
2308 chip_addx, hda_frame_size_words, num_chans,
2309 sample_rate_mul, sample_rate_div, buffer_size_words);
2310
2311 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2312 (buffer_size_words < hda_frame_size_words)) {
2313 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
2314 return -EINVAL;
2315 }
2316
2317 remainder_words = words_to_write % hda_frame_size_words;
2318 data_remainder = data;
2319 chip_addx_remainder = chip_addx;
2320
2321 data += remainder_words;
2322 chip_addx += remainder_words*sizeof(u32);
2323 words_to_write -= remainder_words;
2324
2325 while (words_to_write != 0) {
2326 run_size_words = min(buffer_size_words, words_to_write);
2327 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2328 words_to_write, run_size_words, remainder_words);
2329 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2330 if (!comm_dma_setup_done) {
2331 status = dsp_dma_stop(codec, dma_chan, ovly);
2332 if (status < 0)
Takashi Iwai425a7882013-01-15 17:41:21 +01002333 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002334 status = dsp_dma_setup_common(codec, chip_addx,
2335 dma_chan, port_map_mask, ovly);
2336 if (status < 0)
2337 return status;
2338 comm_dma_setup_done = true;
2339 }
2340
2341 status = dsp_dma_setup(codec, chip_addx,
2342 run_size_words, dma_chan);
2343 if (status < 0)
2344 return status;
2345 status = dsp_dma_start(codec, dma_chan, ovly);
2346 if (status < 0)
2347 return status;
2348 if (!dsp_is_dma_active(codec, dma_chan)) {
2349 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
2350 return -EIO;
2351 }
2352 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2353 if (status < 0)
2354 return status;
2355 if (remainder_words != 0) {
2356 status = chipio_write_multiple(codec,
2357 chip_addx_remainder,
2358 data_remainder,
2359 remainder_words);
2360 remainder_words = 0;
2361 }
2362 if (hci_write) {
2363 status = dspxfr_hci_write(codec, hci_write);
2364 hci_write = NULL;
2365 }
Ian Minett6d675302013-02-08 18:31:43 -08002366
2367 timeout = jiffies + msecs_to_jiffies(2000);
2368 do {
2369 dma_active = dsp_is_dma_active(codec, dma_chan);
2370 if (!dma_active)
Ian Minett01ef7db2012-09-20 20:29:16 -07002371 break;
Ian Minett6d675302013-02-08 18:31:43 -08002372 msleep(20);
2373 } while (time_before(jiffies, timeout));
2374 if (dma_active)
2375 break;
2376
Ian Minett01ef7db2012-09-20 20:29:16 -07002377 snd_printdd(KERN_INFO "+++++ DMA complete");
2378 dma_set_state(dma_engine, DMA_STATE_STOP);
2379 dma_reset(dma_engine);
2380
2381 if (status < 0)
2382 return status;
2383
2384 data += run_size_words;
2385 chip_addx += run_size_words*sizeof(u32);
2386 words_to_write -= run_size_words;
2387 }
2388
2389 if (remainder_words != 0) {
2390 status = chipio_write_multiple(codec, chip_addx_remainder,
2391 data_remainder, remainder_words);
2392 }
2393
2394 return status;
2395}
2396
Ian Minettd5c21b82012-09-20 20:29:18 -07002397/**
2398 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2399 *
2400 * @codec: the HDA codec
2401 * @fls_data: pointer to a fast load image
2402 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2403 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002404 * @sample_rate: sampling rate of the stream used for DSP download
2405 * @number_channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002406 * @ovly: TRUE if overlay format is required
2407 *
2408 * Returns zero or a negative error code.
2409 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002410static int dspxfr_image(struct hda_codec *codec,
2411 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002412 unsigned int reloc,
2413 unsigned int sample_rate,
2414 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002415 bool ovly)
2416{
2417 struct ca0132_spec *spec = codec->spec;
2418 int status;
2419 unsigned short hda_format = 0;
2420 unsigned int response;
2421 unsigned char stream_id = 0;
2422 struct dma_engine *dma_engine;
2423 unsigned int dma_chan;
2424 unsigned int port_map_mask;
2425
2426 if (fls_data == NULL)
2427 return -EINVAL;
2428
2429 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai549e8292013-01-15 17:42:15 +01002430 if (!dma_engine)
2431 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002432
2433 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2434 if (!dma_engine->dmab) {
Takashi Iwai549e8292013-01-15 17:42:15 +01002435 kfree(dma_engine);
2436 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002437 }
2438
2439 dma_engine->codec = codec;
Ian Minette97249d2012-09-20 20:29:21 -07002440 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002441 dma_engine->m_converter_format = hda_format;
2442 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2443 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2444
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002445 dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002446
2447 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2448 hda_format, &response);
2449
2450 if (status < 0) {
2451 snd_printdd(KERN_ERR "set converter format fail");
2452 goto exit;
2453 }
2454
2455 status = snd_hda_codec_load_dsp_prepare(codec,
2456 dma_engine->m_converter_format,
2457 dma_engine->buf_size,
2458 dma_engine->dmab);
2459 if (status < 0)
2460 goto exit;
2461 spec->dsp_stream_id = status;
2462
2463 if (ovly) {
2464 status = dspio_alloc_dma_chan(codec, &dma_chan);
2465 if (status < 0) {
2466 snd_printdd(KERN_ERR "alloc dmachan fail");
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002467 dma_chan = INVALID_DMA_CHANNEL;
Ian Minett01ef7db2012-09-20 20:29:16 -07002468 goto exit;
2469 }
2470 }
2471
2472 port_map_mask = 0;
2473 status = dsp_allocate_ports_format(codec, hda_format,
2474 &port_map_mask);
2475 if (status < 0) {
2476 snd_printdd(KERN_ERR "alloc ports fail");
2477 goto exit;
2478 }
2479
2480 stream_id = dma_get_stream_id(dma_engine);
2481 status = codec_set_converter_stream_channel(codec,
2482 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2483 if (status < 0) {
2484 snd_printdd(KERN_ERR "set stream chan fail");
2485 goto exit;
2486 }
2487
2488 while ((fls_data != NULL) && !is_last(fls_data)) {
2489 if (!is_valid(fls_data)) {
2490 snd_printdd(KERN_ERR "FLS check fail");
2491 status = -EINVAL;
2492 goto exit;
2493 }
2494 status = dspxfr_one_seg(codec, fls_data, reloc,
2495 dma_engine, dma_chan,
2496 port_map_mask, ovly);
2497 if (status < 0)
2498 break;
2499
2500 if (is_hci_prog_list_seg(fls_data))
2501 fls_data = get_next_seg_ptr(fls_data);
2502
2503 if ((fls_data != NULL) && !is_last(fls_data))
2504 fls_data = get_next_seg_ptr(fls_data);
2505 }
2506
2507 if (port_map_mask != 0)
2508 status = dsp_free_ports(codec);
2509
2510 if (status < 0)
2511 goto exit;
2512
2513 status = codec_set_converter_stream_channel(codec,
2514 WIDGET_CHIP_CTRL, 0, 0, &response);
2515
2516exit:
2517 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2518 dspio_free_dma_chan(codec, dma_chan);
2519
2520 if (dma_engine->dmab)
2521 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2522 kfree(dma_engine->dmab);
2523 kfree(dma_engine);
2524
2525 return status;
2526}
2527
2528/*
2529 * CA0132 DSP download stuffs.
2530 */
2531static void dspload_post_setup(struct hda_codec *codec)
2532{
2533 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2534
2535 /*set DSP speaker to 2.0 configuration*/
2536 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2537 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2538
2539 /*update write pointer*/
2540 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2541}
2542
Ian Minettd5c21b82012-09-20 20:29:18 -07002543/**
2544 * Download DSP from a DSP Image Fast Load structure. This structure is a
2545 * linear, non-constant sized element array of structures, each of which
2546 * contain the count of the data to be loaded, the data itself, and the
2547 * corresponding starting chip address of the starting data location.
2548 *
2549 * @codec: the HDA codec
2550 * @fls: pointer to a fast load image
2551 * @ovly: TRUE if overlay format is required
2552 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2553 * no relocation
2554 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2555 * @router_chans: number of audio router channels to be allocated (0 means use
2556 * internal defaults; max is 32)
2557 *
2558 * Returns zero or a negative error code.
2559 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002560static int dspload_image(struct hda_codec *codec,
2561 const struct dsp_image_seg *fls,
2562 bool ovly,
2563 unsigned int reloc,
2564 bool autostart,
2565 int router_chans)
2566{
2567 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07002568 unsigned int sample_rate;
2569 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07002570
2571 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2572 if (router_chans == 0) {
2573 if (!ovly)
2574 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2575 else
2576 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2577 }
2578
Ian Minette97249d2012-09-20 20:29:21 -07002579 sample_rate = 48000;
2580 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07002581
Ian Minette97249d2012-09-20 20:29:21 -07002582 while (channels > 16) {
2583 sample_rate *= 2;
2584 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07002585 }
2586
Ian Minett01ef7db2012-09-20 20:29:16 -07002587 do {
2588 snd_printdd(KERN_INFO "Ready to program DMA");
2589 if (!ovly)
2590 status = dsp_reset(codec);
2591
2592 if (status < 0)
2593 break;
2594
2595 snd_printdd(KERN_INFO "dsp_reset() complete");
Ian Minette97249d2012-09-20 20:29:21 -07002596 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2597 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07002598
2599 if (status < 0)
2600 break;
2601
2602 snd_printdd(KERN_INFO "dspxfr_image() complete");
2603 if (autostart && !ovly) {
2604 dspload_post_setup(codec);
2605 status = dsp_set_run_state(codec);
2606 }
2607
2608 snd_printdd(KERN_INFO "LOAD FINISHED");
2609 } while (0);
2610
2611 return status;
2612}
2613
2614static bool dspload_is_loaded(struct hda_codec *codec)
2615{
2616 unsigned int data = 0;
2617 int status = 0;
2618
2619 status = chipio_read(codec, 0x40004, &data);
2620 if ((status < 0) || (data != 1))
2621 return false;
2622
2623 return true;
2624}
2625
2626static bool dspload_wait_loaded(struct hda_codec *codec)
2627{
Ian Minett6d675302013-02-08 18:31:43 -08002628 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
Ian Minett01ef7db2012-09-20 20:29:16 -07002629
2630 do {
Ian Minett01ef7db2012-09-20 20:29:16 -07002631 if (dspload_is_loaded(codec)) {
2632 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2633 return true;
2634 }
Ian Minett6d675302013-02-08 18:31:43 -08002635 msleep(20);
2636 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07002637
2638 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2639 return false;
2640}
2641
Ian Minett5aaca442012-12-20 18:53:34 -08002642/*
Ian Minett825315b2012-12-20 18:53:36 -08002643 * PCM stuffs
Ian Minett95c6e9c2011-06-15 15:35:17 -07002644 */
Ian Minett825315b2012-12-20 18:53:36 -08002645static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
2646 u32 stream_tag,
2647 int channel_id, int format)
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002648{
Ian Minett825315b2012-12-20 18:53:36 -08002649 unsigned int oldval, newval;
2650
2651 if (!nid)
2652 return;
2653
2654 snd_printdd(
2655 "ca0132_setup_stream: NID=0x%x, stream=0x%x, "
2656 "channel=%d, format=0x%x\n",
2657 nid, stream_tag, channel_id, format);
2658
2659 /* update the format-id if changed */
2660 oldval = snd_hda_codec_read(codec, nid, 0,
2661 AC_VERB_GET_STREAM_FORMAT,
2662 0);
2663 if (oldval != format) {
2664 msleep(20);
2665 snd_hda_codec_write(codec, nid, 0,
2666 AC_VERB_SET_STREAM_FORMAT,
2667 format);
2668 }
2669
2670 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2671 newval = (stream_tag << 4) | channel_id;
2672 if (oldval != newval) {
2673 snd_hda_codec_write(codec, nid, 0,
2674 AC_VERB_SET_CHANNEL_STREAMID,
2675 newval);
2676 }
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002677}
2678
Ian Minett825315b2012-12-20 18:53:36 -08002679static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
2680{
2681 unsigned int val;
2682
2683 if (!nid)
2684 return;
2685
2686 snd_printdd(KERN_INFO "ca0132_cleanup_stream: NID=0x%x\n", nid);
2687
2688 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
2689 if (!val)
2690 return;
2691
2692 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
2693 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2694}
2695
2696/*
2697 * PCM callbacks
2698 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002699static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2700 struct hda_codec *codec,
2701 unsigned int stream_tag,
2702 unsigned int format,
2703 struct snd_pcm_substream *substream)
2704{
2705 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002706
2707 ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
2708
2709 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002710}
2711
2712static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2713 struct hda_codec *codec,
2714 struct snd_pcm_substream *substream)
2715{
2716 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08002717
2718 if (spec->dsp_state == DSP_DOWNLOADING)
2719 return 0;
2720
2721 /*If Playback effects are on, allow stream some time to flush
2722 *effects tail*/
2723 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
2724 msleep(50);
2725
2726 ca0132_cleanup_stream(codec, spec->dacs[0]);
2727
2728 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002729}
2730
2731/*
2732 * Digital out
2733 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002734static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2735 struct hda_codec *codec,
2736 struct snd_pcm_substream *substream)
2737{
2738 struct ca0132_spec *spec = codec->spec;
2739 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2740}
2741
Ian Minett95c6e9c2011-06-15 15:35:17 -07002742static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2743 struct hda_codec *codec,
2744 unsigned int stream_tag,
2745 unsigned int format,
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_prepare(codec, &spec->multiout,
2750 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002751}
2752
2753static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2754 struct hda_codec *codec,
2755 struct snd_pcm_substream *substream)
2756{
2757 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002758 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002759}
2760
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002761static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2762 struct hda_codec *codec,
2763 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002764{
2765 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002766 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002767}
2768
2769/*
Ian Minett825315b2012-12-20 18:53:36 -08002770 * Analog capture
2771 */
2772static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2773 struct hda_codec *codec,
2774 unsigned int stream_tag,
2775 unsigned int format,
2776 struct snd_pcm_substream *substream)
2777{
2778 struct ca0132_spec *spec = codec->spec;
2779
2780 ca0132_setup_stream(codec, spec->adcs[substream->number],
2781 stream_tag, 0, format);
2782
2783 return 0;
2784}
2785
2786static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2787 struct hda_codec *codec,
2788 struct snd_pcm_substream *substream)
2789{
2790 struct ca0132_spec *spec = codec->spec;
2791
2792 if (spec->dsp_state == DSP_DOWNLOADING)
2793 return 0;
2794
2795 ca0132_cleanup_stream(codec, hinfo->nid);
2796 return 0;
2797}
2798
Ian Minette90f29e2012-12-20 18:53:39 -08002799/*
2800 * Controls stuffs.
2801 */
2802
2803/*
2804 * Mixer controls helpers.
2805 */
2806#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2807 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2808 .name = xname, \
2809 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2810 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2811 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2812 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2813 .info = ca0132_volume_info, \
2814 .get = ca0132_volume_get, \
2815 .put = ca0132_volume_put, \
2816 .tlv = { .c = ca0132_volume_tlv }, \
2817 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2818
2819#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2820 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2821 .name = xname, \
2822 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2823 .info = snd_hda_mixer_amp_switch_info, \
2824 .get = ca0132_switch_get, \
2825 .put = ca0132_switch_put, \
2826 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2827
2828/* stereo */
2829#define CA0132_CODEC_VOL(xname, nid, dir) \
2830 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2831#define CA0132_CODEC_MUTE(xname, nid, dir) \
2832 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2833
Ian Minett44f0c972012-12-20 18:53:38 -08002834/* The followings are for tuning of products */
2835#ifdef ENABLE_TUNING_CONTROLS
2836
2837static unsigned int voice_focus_vals_lookup[] = {
28380x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
28390x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
28400x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
28410x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
28420x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
28430x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
28440x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
28450x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
28460x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
28470x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
28480x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
28490x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
28500x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
28510x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
28520x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
28530x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
28540x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
28550x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
28560x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
28570x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
28580x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
28590x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
28600x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
28610x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
28620x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
28630x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
28640x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
2865};
2866
2867static unsigned int mic_svm_vals_lookup[] = {
28680x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
28690x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
28700x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
28710x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
28720x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
28730x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
28740x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
28750x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
28760x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
28770x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
28780x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
28790x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
28800x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
28810x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
28820x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
28830x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
28840x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
2885};
2886
2887static unsigned int equalizer_vals_lookup[] = {
28880xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
28890xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
28900xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
28910xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
28920x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
28930x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
28940x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
28950x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
28960x41C00000
2897};
2898
2899static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
2900 unsigned int *lookup, int idx)
2901{
2902 int i = 0;
2903
2904 for (i = 0; i < TUNING_CTLS_COUNT; i++)
2905 if (nid == ca0132_tuning_ctls[i].nid)
2906 break;
2907
2908 snd_hda_power_up(codec);
2909 dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
2910 ca0132_tuning_ctls[i].req,
2911 &(lookup[idx]), sizeof(unsigned int));
2912 snd_hda_power_down(codec);
2913
2914 return 1;
2915}
2916
2917static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
2918 struct snd_ctl_elem_value *ucontrol)
2919{
2920 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2921 struct ca0132_spec *spec = codec->spec;
2922 hda_nid_t nid = get_amp_nid(kcontrol);
2923 long *valp = ucontrol->value.integer.value;
2924 int idx = nid - TUNING_CTL_START_NID;
2925
2926 *valp = spec->cur_ctl_vals[idx];
2927 return 0;
2928}
2929
2930static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_info *uinfo)
2932{
2933 int chs = get_amp_channels(kcontrol);
2934 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2935 uinfo->count = chs == 3 ? 2 : 1;
2936 uinfo->value.integer.min = 20;
2937 uinfo->value.integer.max = 180;
2938 uinfo->value.integer.step = 1;
2939
2940 return 0;
2941}
2942
2943static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2945{
2946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2947 struct ca0132_spec *spec = codec->spec;
2948 hda_nid_t nid = get_amp_nid(kcontrol);
2949 long *valp = ucontrol->value.integer.value;
2950 int idx;
2951
2952 idx = nid - TUNING_CTL_START_NID;
2953 /* any change? */
2954 if (spec->cur_ctl_vals[idx] == *valp)
2955 return 0;
2956
2957 spec->cur_ctl_vals[idx] = *valp;
2958
2959 idx = *valp - 20;
2960 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
2961
2962 return 1;
2963}
2964
2965static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_info *uinfo)
2967{
2968 int chs = get_amp_channels(kcontrol);
2969 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2970 uinfo->count = chs == 3 ? 2 : 1;
2971 uinfo->value.integer.min = 0;
2972 uinfo->value.integer.max = 100;
2973 uinfo->value.integer.step = 1;
2974
2975 return 0;
2976}
2977
2978static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
2979 struct snd_ctl_elem_value *ucontrol)
2980{
2981 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2982 struct ca0132_spec *spec = codec->spec;
2983 hda_nid_t nid = get_amp_nid(kcontrol);
2984 long *valp = ucontrol->value.integer.value;
2985 int idx;
2986
2987 idx = nid - TUNING_CTL_START_NID;
2988 /* any change? */
2989 if (spec->cur_ctl_vals[idx] == *valp)
2990 return 0;
2991
2992 spec->cur_ctl_vals[idx] = *valp;
2993
2994 idx = *valp;
2995 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
2996
2997 return 0;
2998}
2999
3000static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
3001 struct snd_ctl_elem_info *uinfo)
3002{
3003 int chs = get_amp_channels(kcontrol);
3004 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3005 uinfo->count = chs == 3 ? 2 : 1;
3006 uinfo->value.integer.min = 0;
3007 uinfo->value.integer.max = 48;
3008 uinfo->value.integer.step = 1;
3009
3010 return 0;
3011}
3012
3013static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3014 struct snd_ctl_elem_value *ucontrol)
3015{
3016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3017 struct ca0132_spec *spec = codec->spec;
3018 hda_nid_t nid = get_amp_nid(kcontrol);
3019 long *valp = ucontrol->value.integer.value;
3020 int idx;
3021
3022 idx = nid - TUNING_CTL_START_NID;
3023 /* any change? */
3024 if (spec->cur_ctl_vals[idx] == *valp)
3025 return 0;
3026
3027 spec->cur_ctl_vals[idx] = *valp;
3028
3029 idx = *valp;
3030 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3031
3032 return 1;
3033}
3034
3035static const DECLARE_TLV_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3036static const DECLARE_TLV_DB_SCALE(eq_db_scale, -2400, 100, 0);
3037
3038static int add_tuning_control(struct hda_codec *codec,
3039 hda_nid_t pnid, hda_nid_t nid,
3040 const char *name, int dir)
3041{
3042 char namestr[44];
3043 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3044 struct snd_kcontrol_new knew =
3045 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3046
3047 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3048 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3049 knew.tlv.c = 0;
3050 knew.tlv.p = 0;
3051 switch (pnid) {
3052 case VOICE_FOCUS:
3053 knew.info = voice_focus_ctl_info;
3054 knew.get = tuning_ctl_get;
3055 knew.put = voice_focus_ctl_put;
3056 knew.tlv.p = voice_focus_db_scale;
3057 break;
3058 case MIC_SVM:
3059 knew.info = mic_svm_ctl_info;
3060 knew.get = tuning_ctl_get;
3061 knew.put = mic_svm_ctl_put;
3062 break;
3063 case EQUALIZER:
3064 knew.info = equalizer_ctl_info;
3065 knew.get = tuning_ctl_get;
3066 knew.put = equalizer_ctl_put;
3067 knew.tlv.p = eq_db_scale;
3068 break;
3069 default:
3070 return 0;
3071 }
3072 knew.private_value =
3073 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3074 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3075 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3076}
3077
3078static int add_tuning_ctls(struct hda_codec *codec)
3079{
3080 int i;
3081 int err;
3082
3083 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3084 err = add_tuning_control(codec,
3085 ca0132_tuning_ctls[i].parent_nid,
3086 ca0132_tuning_ctls[i].nid,
3087 ca0132_tuning_ctls[i].name,
3088 ca0132_tuning_ctls[i].direct);
3089 if (err < 0)
3090 return err;
3091 }
3092
3093 return 0;
3094}
3095
3096static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3097{
3098 struct ca0132_spec *spec = codec->spec;
3099 int i;
3100
3101 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3102 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3103 /* SVM level defaults to 0.74. */
3104 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3105
3106 /* EQ defaults to 0dB. */
3107 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3108 spec->cur_ctl_vals[i] = 24;
3109}
3110#endif /*ENABLE_TUNING_CONTROLS*/
3111
Ian Minett825315b2012-12-20 18:53:36 -08003112/*
Ian Minett5aaca442012-12-20 18:53:34 -08003113 * Select the active output.
3114 * If autodetect is enabled, output will be selected based on jack detection.
3115 * If jack inserted, headphone will be selected, else built-in speakers
3116 * If autodetect is disabled, output will be selected based on selection.
3117 */
3118static int ca0132_select_out(struct hda_codec *codec)
3119{
3120 struct ca0132_spec *spec = codec->spec;
3121 unsigned int pin_ctl;
3122 int jack_present;
3123 int auto_jack;
3124 unsigned int tmp;
3125 int err;
3126
3127 snd_printdd(KERN_INFO "ca0132_select_out\n");
3128
3129 snd_hda_power_up(codec);
3130
3131 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3132
3133 if (auto_jack)
3134 jack_present = snd_hda_jack_detect(codec, spec->out_pins[1]);
3135 else
3136 jack_present =
3137 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3138
3139 if (jack_present)
3140 spec->cur_out_type = HEADPHONE_OUT;
3141 else
3142 spec->cur_out_type = SPEAKER_OUT;
3143
3144 if (spec->cur_out_type == SPEAKER_OUT) {
3145 snd_printdd(KERN_INFO "ca0132_select_out speaker\n");
3146 /*speaker out config*/
3147 tmp = FLOAT_ONE;
3148 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3149 if (err < 0)
3150 goto exit;
3151 /*enable speaker EQ*/
3152 tmp = FLOAT_ONE;
3153 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3154 if (err < 0)
3155 goto exit;
3156
3157 /* Setup EAPD */
3158 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3159 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3160 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3161 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3162 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3163 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3164 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3165 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3166
3167 /* disable headphone node */
3168 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3169 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003170 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3171 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003172 /* enable speaker node */
3173 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3174 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003175 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3176 pin_ctl | PIN_OUT);
Ian Minett5aaca442012-12-20 18:53:34 -08003177 } else {
3178 snd_printdd(KERN_INFO "ca0132_select_out hp\n");
3179 /*headphone out config*/
3180 tmp = FLOAT_ZERO;
3181 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3182 if (err < 0)
3183 goto exit;
3184 /*disable speaker EQ*/
3185 tmp = FLOAT_ZERO;
3186 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3187 if (err < 0)
3188 goto exit;
3189
3190 /* Setup EAPD */
3191 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3192 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3193 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3194 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3195 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3196 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3197 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3198 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3199
3200 /* disable speaker*/
3201 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3202 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003203 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3204 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003205 /* enable headphone*/
3206 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3207 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003208 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3209 pin_ctl | PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003210 }
3211
3212exit:
3213 snd_hda_power_down(codec);
3214
3215 return err < 0 ? err : 0;
3216}
3217
3218static void ca0132_set_dmic(struct hda_codec *codec, int enable);
3219static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
3220static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
3221
3222/*
3223 * Select the active VIP source
3224 */
3225static int ca0132_set_vipsource(struct hda_codec *codec, int val)
3226{
3227 struct ca0132_spec *spec = codec->spec;
3228 unsigned int tmp;
3229
3230 if (!dspload_is_loaded(codec))
3231 return 0;
3232
3233 /* if CrystalVoice if off, vipsource should be 0 */
3234 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
3235 (val == 0)) {
3236 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
3237 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
3238 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
3239 if (spec->cur_mic_type == DIGITAL_MIC)
3240 tmp = FLOAT_TWO;
3241 else
3242 tmp = FLOAT_ONE;
3243 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3244 tmp = FLOAT_ZERO;
3245 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3246 } else {
3247 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3248 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3249 if (spec->cur_mic_type == DIGITAL_MIC)
3250 tmp = FLOAT_TWO;
3251 else
3252 tmp = FLOAT_ONE;
3253 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
3254 tmp = FLOAT_ONE;
3255 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
3256 msleep(20);
3257 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
3258 }
3259
3260 return 1;
3261}
3262
3263/*
3264 * Select the active microphone.
3265 * If autodetect is enabled, mic will be selected based on jack detection.
3266 * If jack inserted, ext.mic will be selected, else built-in mic
3267 * If autodetect is disabled, mic will be selected based on selection.
3268 */
3269static int ca0132_select_mic(struct hda_codec *codec)
3270{
3271 struct ca0132_spec *spec = codec->spec;
3272 int jack_present;
3273 int auto_jack;
3274
3275 snd_printdd(KERN_INFO "ca0132_select_mic\n");
3276
3277 snd_hda_power_up(codec);
3278
3279 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3280
3281 if (auto_jack)
3282 jack_present = snd_hda_jack_detect(codec, spec->input_pins[0]);
3283 else
3284 jack_present =
3285 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
3286
3287 if (jack_present)
3288 spec->cur_mic_type = LINE_MIC_IN;
3289 else
3290 spec->cur_mic_type = DIGITAL_MIC;
3291
3292 if (spec->cur_mic_type == DIGITAL_MIC) {
3293 /* enable digital Mic */
3294 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
3295 ca0132_set_dmic(codec, 1);
3296 ca0132_mic_boost_set(codec, 0);
3297 /* set voice focus */
3298 ca0132_effects_set(codec, VOICE_FOCUS,
3299 spec->effects_switch
3300 [VOICE_FOCUS - EFFECT_START_NID]);
3301 } else {
3302 /* disable digital Mic */
3303 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
3304 ca0132_set_dmic(codec, 0);
3305 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
3306 /* disable voice focus */
3307 ca0132_effects_set(codec, VOICE_FOCUS, 0);
3308 }
3309
3310 snd_hda_power_down(codec);
3311
3312 return 0;
3313}
3314
3315/*
Ian Minetta7e76272012-12-20 18:53:35 -08003316 * Check if VNODE settings take effect immediately.
3317 */
3318static bool ca0132_is_vnode_effective(struct hda_codec *codec,
3319 hda_nid_t vnid,
3320 hda_nid_t *shared_nid)
3321{
3322 struct ca0132_spec *spec = codec->spec;
3323 hda_nid_t nid;
Ian Minetta7e76272012-12-20 18:53:35 -08003324
3325 switch (vnid) {
3326 case VNID_SPK:
3327 nid = spec->shared_out_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08003328 break;
3329 case VNID_MIC:
3330 nid = spec->shared_mic_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08003331 break;
3332 default:
Takashi Iwai9a0869f2013-02-07 12:41:40 +01003333 return false;
Ian Minetta7e76272012-12-20 18:53:35 -08003334 }
3335
Takashi Iwai9a0869f2013-02-07 12:41:40 +01003336 if (shared_nid)
Ian Minetta7e76272012-12-20 18:53:35 -08003337 *shared_nid = nid;
3338
Takashi Iwai9a0869f2013-02-07 12:41:40 +01003339 return true;
Ian Minetta7e76272012-12-20 18:53:35 -08003340}
3341
3342/*
3343* The following functions are control change helpers.
3344* They return 0 if no changed. Return 1 if changed.
3345*/
3346static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
3347{
3348 struct ca0132_spec *spec = codec->spec;
3349 unsigned int tmp;
3350
3351 /* based on CrystalVoice state to enable VoiceFX. */
3352 if (enable) {
3353 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
3354 FLOAT_ONE : FLOAT_ZERO;
3355 } else {
3356 tmp = FLOAT_ZERO;
3357 }
3358
3359 dspio_set_uint_param(codec, ca0132_voicefx.mid,
3360 ca0132_voicefx.reqs[0], tmp);
3361
3362 return 1;
3363}
3364
3365/*
Ian Minett5aaca442012-12-20 18:53:34 -08003366 * Set the effects parameters
3367 */
3368static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
3369{
3370 struct ca0132_spec *spec = codec->spec;
3371 unsigned int on;
3372 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3373 int err = 0;
3374 int idx = nid - EFFECT_START_NID;
3375
3376 if ((idx < 0) || (idx >= num_fx))
3377 return 0; /* no changed */
3378
3379 /* for out effect, qualify with PE */
3380 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
3381 /* if PE if off, turn off out effects. */
3382 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3383 val = 0;
3384 }
3385
3386 /* for in effect, qualify with CrystalVoice */
3387 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
3388 /* if CrystalVoice if off, turn off in effects. */
3389 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3390 val = 0;
3391
3392 /* Voice Focus applies to 2-ch Mic, Digital Mic */
3393 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
3394 val = 0;
3395 }
3396
Takashi Iwaidea500c2013-01-15 17:49:56 +01003397 snd_printdd(KERN_INFO "ca0132_effect_set: nid=0x%x, val=%ld\n",
Ian Minett5aaca442012-12-20 18:53:34 -08003398 nid, val);
3399
3400 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
3401 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
3402 ca0132_effects[idx].reqs[0], on);
3403
3404 if (err < 0)
3405 return 0; /* no changed */
3406
3407 return 1;
3408}
3409
Ian Minetta7e76272012-12-20 18:53:35 -08003410/*
3411 * Turn on/off Playback Enhancements
3412 */
3413static int ca0132_pe_switch_set(struct hda_codec *codec)
3414{
3415 struct ca0132_spec *spec = codec->spec;
3416 hda_nid_t nid;
3417 int i, ret = 0;
3418
3419 snd_printdd(KERN_INFO "ca0132_pe_switch_set: val=%ld\n",
3420 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
3421
3422 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
3423 nid = OUT_EFFECT_START_NID;
3424 /* PE affects all out effects */
3425 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
3426 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3427
3428 return ret;
3429}
3430
Ian Minett5aaca442012-12-20 18:53:34 -08003431/* Check if Mic1 is streaming, if so, stop streaming */
3432static int stop_mic1(struct hda_codec *codec)
3433{
3434 struct ca0132_spec *spec = codec->spec;
3435 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
3436 AC_VERB_GET_CONV, 0);
3437 if (oldval != 0)
3438 snd_hda_codec_write(codec, spec->adcs[0], 0,
3439 AC_VERB_SET_CHANNEL_STREAMID,
3440 0);
3441 return oldval;
3442}
3443
3444/* Resume Mic1 streaming if it was stopped. */
3445static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
3446{
3447 struct ca0132_spec *spec = codec->spec;
3448 /* Restore the previous stream and channel */
3449 if (oldval != 0)
3450 snd_hda_codec_write(codec, spec->adcs[0], 0,
3451 AC_VERB_SET_CHANNEL_STREAMID,
3452 oldval);
3453}
3454
3455/*
Ian Minetta7e76272012-12-20 18:53:35 -08003456 * Turn on/off CrystalVoice
Ian Minett5aaca442012-12-20 18:53:34 -08003457 */
Ian Minetta7e76272012-12-20 18:53:35 -08003458static int ca0132_cvoice_switch_set(struct hda_codec *codec)
3459{
3460 struct ca0132_spec *spec = codec->spec;
3461 hda_nid_t nid;
3462 int i, ret = 0;
3463 unsigned int oldval;
3464
3465 snd_printdd(KERN_INFO "ca0132_cvoice_switch_set: val=%ld\n",
3466 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
3467
3468 i = IN_EFFECT_START_NID - EFFECT_START_NID;
3469 nid = IN_EFFECT_START_NID;
3470 /* CrystalVoice affects all in effects */
3471 for (; nid < IN_EFFECT_END_NID; nid++, i++)
3472 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
3473
3474 /* including VoiceFX */
3475 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
3476
3477 /* set correct vipsource */
3478 oldval = stop_mic1(codec);
3479 ret |= ca0132_set_vipsource(codec, 1);
3480 resume_mic1(codec, oldval);
3481 return ret;
3482}
3483
Ian Minett5aaca442012-12-20 18:53:34 -08003484static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
3485{
3486 struct ca0132_spec *spec = codec->spec;
3487 int ret = 0;
3488
3489 if (val) /* on */
3490 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3491 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
3492 else /* off */
3493 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
3494 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
3495
3496 return ret;
3497}
3498
Ian Minetta7e76272012-12-20 18:53:35 -08003499static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
3500 struct snd_ctl_elem_value *ucontrol)
3501{
3502 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3503 hda_nid_t nid = get_amp_nid(kcontrol);
3504 hda_nid_t shared_nid = 0;
3505 bool effective;
3506 int ret = 0;
3507 struct ca0132_spec *spec = codec->spec;
3508 int auto_jack;
3509
3510 if (nid == VNID_HP_SEL) {
3511 auto_jack =
3512 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3513 if (!auto_jack)
3514 ca0132_select_out(codec);
3515 return 1;
3516 }
3517
3518 if (nid == VNID_AMIC1_SEL) {
3519 auto_jack =
3520 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
3521 if (!auto_jack)
3522 ca0132_select_mic(codec);
3523 return 1;
3524 }
3525
3526 if (nid == VNID_HP_ASEL) {
3527 ca0132_select_out(codec);
3528 return 1;
3529 }
3530
3531 if (nid == VNID_AMIC1_ASEL) {
3532 ca0132_select_mic(codec);
3533 return 1;
3534 }
3535
3536 /* if effective conditions, then update hw immediately. */
3537 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3538 if (effective) {
3539 int dir = get_amp_direction(kcontrol);
3540 int ch = get_amp_channels(kcontrol);
3541 unsigned long pval;
3542
3543 mutex_lock(&codec->control_mutex);
3544 pval = kcontrol->private_value;
3545 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3546 0, dir);
3547 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3548 kcontrol->private_value = pval;
3549 mutex_unlock(&codec->control_mutex);
3550 }
3551
3552 return ret;
3553}
3554/* End of control change helpers. */
3555
3556static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
3557 struct snd_ctl_elem_info *uinfo)
3558{
3559 unsigned int items = sizeof(ca0132_voicefx_presets)
3560 / sizeof(struct ct_voicefx_preset);
3561
3562 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3563 uinfo->count = 1;
3564 uinfo->value.enumerated.items = items;
3565 if (uinfo->value.enumerated.item >= items)
3566 uinfo->value.enumerated.item = items - 1;
3567 strcpy(uinfo->value.enumerated.name,
3568 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
3569 return 0;
3570}
3571
3572static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
3573 struct snd_ctl_elem_value *ucontrol)
3574{
3575 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3576 struct ca0132_spec *spec = codec->spec;
3577
3578 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
3579 return 0;
3580}
3581
3582static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
3583 struct snd_ctl_elem_value *ucontrol)
3584{
3585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3586 struct ca0132_spec *spec = codec->spec;
3587 int i, err = 0;
3588 int sel = ucontrol->value.enumerated.item[0];
3589 unsigned int items = sizeof(ca0132_voicefx_presets)
3590 / sizeof(struct ct_voicefx_preset);
3591
3592 if (sel >= items)
3593 return 0;
3594
3595 snd_printdd(KERN_INFO "ca0132_voicefx_put: sel=%d, preset=%s\n",
3596 sel, ca0132_voicefx_presets[sel].name);
3597
3598 /*
3599 * Idx 0 is default.
3600 * Default needs to qualify with CrystalVoice state.
3601 */
3602 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
3603 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
3604 ca0132_voicefx.reqs[i],
3605 ca0132_voicefx_presets[sel].vals[i]);
3606 if (err < 0)
3607 break;
3608 }
3609
3610 if (err >= 0) {
3611 spec->voicefx_val = sel;
3612 /* enable voice fx */
3613 ca0132_voicefx_set(codec, (sel ? 1 : 0));
3614 }
3615
3616 return 1;
3617}
3618
3619static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
3620 struct snd_ctl_elem_value *ucontrol)
3621{
3622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3623 struct ca0132_spec *spec = codec->spec;
3624 hda_nid_t nid = get_amp_nid(kcontrol);
3625 int ch = get_amp_channels(kcontrol);
3626 long *valp = ucontrol->value.integer.value;
3627
3628 /* vnode */
3629 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3630 if (ch & 1) {
3631 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
3632 valp++;
3633 }
3634 if (ch & 2) {
3635 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
3636 valp++;
3637 }
3638 return 0;
3639 }
3640
3641 /* effects, include PE and CrystalVoice */
3642 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
3643 *valp = spec->effects_switch[nid - EFFECT_START_NID];
3644 return 0;
3645 }
3646
3647 /* mic boost */
3648 if (nid == spec->input_pins[0]) {
3649 *valp = spec->cur_mic_boost;
3650 return 0;
3651 }
3652
3653 return 0;
3654}
3655
3656static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
3657 struct snd_ctl_elem_value *ucontrol)
3658{
3659 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3660 struct ca0132_spec *spec = codec->spec;
3661 hda_nid_t nid = get_amp_nid(kcontrol);
3662 int ch = get_amp_channels(kcontrol);
3663 long *valp = ucontrol->value.integer.value;
3664 int changed = 1;
3665
3666 snd_printdd(KERN_INFO "ca0132_switch_put: nid=0x%x, val=%ld\n",
3667 nid, *valp);
3668
3669 snd_hda_power_up(codec);
3670 /* vnode */
3671 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
3672 if (ch & 1) {
3673 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
3674 valp++;
3675 }
3676 if (ch & 2) {
3677 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
3678 valp++;
3679 }
3680 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
3681 goto exit;
3682 }
3683
3684 /* PE */
3685 if (nid == PLAY_ENHANCEMENT) {
3686 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3687 changed = ca0132_pe_switch_set(codec);
3688 goto exit;
3689 }
3690
3691 /* CrystalVoice */
3692 if (nid == CRYSTAL_VOICE) {
3693 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3694 changed = ca0132_cvoice_switch_set(codec);
3695 goto exit;
3696 }
3697
3698 /* out and in effects */
3699 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
3700 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
3701 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
3702 changed = ca0132_effects_set(codec, nid, *valp);
3703 goto exit;
3704 }
3705
3706 /* mic boost */
3707 if (nid == spec->input_pins[0]) {
3708 spec->cur_mic_boost = *valp;
3709
3710 /* Mic boost does not apply to Digital Mic */
3711 if (spec->cur_mic_type != DIGITAL_MIC)
3712 changed = ca0132_mic_boost_set(codec, *valp);
3713 goto exit;
3714 }
3715
3716exit:
3717 snd_hda_power_down(codec);
3718 return changed;
3719}
3720
3721/*
3722 * Volume related
3723 */
3724static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
3725 struct snd_ctl_elem_info *uinfo)
3726{
3727 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3728 struct ca0132_spec *spec = codec->spec;
3729 hda_nid_t nid = get_amp_nid(kcontrol);
3730 int ch = get_amp_channels(kcontrol);
3731 int dir = get_amp_direction(kcontrol);
3732 unsigned long pval;
3733 int err;
3734
3735 switch (nid) {
3736 case VNID_SPK:
3737 /* follow shared_out info */
3738 nid = spec->shared_out_nid;
3739 mutex_lock(&codec->control_mutex);
3740 pval = kcontrol->private_value;
3741 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3742 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3743 kcontrol->private_value = pval;
3744 mutex_unlock(&codec->control_mutex);
3745 break;
3746 case VNID_MIC:
3747 /* follow shared_mic info */
3748 nid = spec->shared_mic_nid;
3749 mutex_lock(&codec->control_mutex);
3750 pval = kcontrol->private_value;
3751 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3752 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3753 kcontrol->private_value = pval;
3754 mutex_unlock(&codec->control_mutex);
3755 break;
3756 default:
3757 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
3758 }
3759 return err;
3760}
3761
3762static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
3763 struct snd_ctl_elem_value *ucontrol)
3764{
3765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3766 struct ca0132_spec *spec = codec->spec;
3767 hda_nid_t nid = get_amp_nid(kcontrol);
3768 int ch = get_amp_channels(kcontrol);
3769 long *valp = ucontrol->value.integer.value;
3770
3771 /* store the left and right volume */
3772 if (ch & 1) {
3773 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
3774 valp++;
3775 }
3776 if (ch & 2) {
3777 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
3778 valp++;
3779 }
3780 return 0;
3781}
3782
3783static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
3784 struct snd_ctl_elem_value *ucontrol)
3785{
3786 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3787 struct ca0132_spec *spec = codec->spec;
3788 hda_nid_t nid = get_amp_nid(kcontrol);
3789 int ch = get_amp_channels(kcontrol);
3790 long *valp = ucontrol->value.integer.value;
3791 hda_nid_t shared_nid = 0;
3792 bool effective;
3793 int changed = 1;
3794
3795 /* store the left and right volume */
3796 if (ch & 1) {
3797 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
3798 valp++;
3799 }
3800 if (ch & 2) {
3801 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
3802 valp++;
3803 }
3804
3805 /* if effective conditions, then update hw immediately. */
3806 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
3807 if (effective) {
3808 int dir = get_amp_direction(kcontrol);
3809 unsigned long pval;
3810
3811 snd_hda_power_up(codec);
3812 mutex_lock(&codec->control_mutex);
3813 pval = kcontrol->private_value;
3814 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
3815 0, dir);
3816 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
3817 kcontrol->private_value = pval;
3818 mutex_unlock(&codec->control_mutex);
3819 snd_hda_power_down(codec);
3820 }
3821
3822 return changed;
3823}
3824
3825static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3826 unsigned int size, unsigned int __user *tlv)
3827{
3828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3829 struct ca0132_spec *spec = codec->spec;
3830 hda_nid_t nid = get_amp_nid(kcontrol);
3831 int ch = get_amp_channels(kcontrol);
3832 int dir = get_amp_direction(kcontrol);
3833 unsigned long pval;
3834 int err;
3835
3836 switch (nid) {
3837 case VNID_SPK:
3838 /* follow shared_out tlv */
3839 nid = spec->shared_out_nid;
3840 mutex_lock(&codec->control_mutex);
3841 pval = kcontrol->private_value;
3842 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3843 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3844 kcontrol->private_value = pval;
3845 mutex_unlock(&codec->control_mutex);
3846 break;
3847 case VNID_MIC:
3848 /* follow shared_mic tlv */
3849 nid = spec->shared_mic_nid;
3850 mutex_lock(&codec->control_mutex);
3851 pval = kcontrol->private_value;
3852 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
3853 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3854 kcontrol->private_value = pval;
3855 mutex_unlock(&codec->control_mutex);
3856 break;
3857 default:
3858 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
3859 }
3860 return err;
3861}
3862
3863static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
3864 const char *pfx, int dir)
3865{
3866 char namestr[44];
3867 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3868 struct snd_kcontrol_new knew =
3869 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
3870 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
3871 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3872}
3873
3874static int add_voicefx(struct hda_codec *codec)
3875{
3876 struct snd_kcontrol_new knew =
3877 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
3878 VOICEFX, 1, 0, HDA_INPUT);
3879 knew.info = ca0132_voicefx_info;
3880 knew.get = ca0132_voicefx_get;
3881 knew.put = ca0132_voicefx_put;
3882 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
3883}
3884
3885/*
3886 * When changing Node IDs for Mixer Controls below, make sure to update
3887 * Node IDs in ca0132_config() as well.
3888 */
3889static struct snd_kcontrol_new ca0132_mixer[] = {
3890 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
3891 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
3892 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
3893 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
3894 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
3895 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
3896 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
3897 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
3898 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
3899 0x12, 1, HDA_INPUT),
3900 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
3901 VNID_HP_SEL, 1, HDA_OUTPUT),
3902 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
3903 VNID_AMIC1_SEL, 1, HDA_INPUT),
3904 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
3905 VNID_HP_ASEL, 1, HDA_OUTPUT),
3906 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
3907 VNID_AMIC1_ASEL, 1, HDA_INPUT),
3908 { } /* end */
3909};
3910
Ian Minette90f29e2012-12-20 18:53:39 -08003911static int ca0132_build_controls(struct hda_codec *codec)
3912{
3913 struct ca0132_spec *spec = codec->spec;
3914 int i, num_fx;
3915 int err = 0;
3916
3917 /* Add Mixer controls */
3918 for (i = 0; i < spec->num_mixers; i++) {
3919 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
3920 if (err < 0)
3921 return err;
3922 }
3923
3924 /* Add in and out effects controls.
3925 * VoiceFX, PE and CrystalVoice are added separately.
3926 */
3927 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
3928 for (i = 0; i < num_fx; i++) {
3929 err = add_fx_switch(codec, ca0132_effects[i].nid,
3930 ca0132_effects[i].name,
3931 ca0132_effects[i].direct);
3932 if (err < 0)
3933 return err;
3934 }
3935
3936 err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
3937 if (err < 0)
3938 return err;
3939
3940 err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
3941 if (err < 0)
3942 return err;
3943
3944 add_voicefx(codec);
3945
3946#ifdef ENABLE_TUNING_CONTROLS
3947 add_tuning_ctls(codec);
3948#endif
3949
3950 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3951 if (err < 0)
3952 return err;
3953
3954 if (spec->dig_out) {
3955 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
3956 spec->dig_out);
3957 if (err < 0)
3958 return err;
3959 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
3960 if (err < 0)
3961 return err;
3962 /* spec->multiout.share_spdif = 1; */
3963 }
3964
3965 if (spec->dig_in) {
3966 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
3967 if (err < 0)
3968 return err;
3969 }
3970 return 0;
3971}
3972
Ian Minett5aaca442012-12-20 18:53:34 -08003973/*
Ian Minette90f29e2012-12-20 18:53:39 -08003974 * PCM
Ian Minett95c6e9c2011-06-15 15:35:17 -07003975 */
3976static struct hda_pcm_stream ca0132_pcm_analog_playback = {
3977 .substreams = 1,
3978 .channels_min = 2,
Ian Minett825315b2012-12-20 18:53:36 -08003979 .channels_max = 6,
Ian Minett95c6e9c2011-06-15 15:35:17 -07003980 .ops = {
3981 .prepare = ca0132_playback_pcm_prepare,
3982 .cleanup = ca0132_playback_pcm_cleanup
3983 },
3984};
3985
3986static struct hda_pcm_stream ca0132_pcm_analog_capture = {
3987 .substreams = 1,
3988 .channels_min = 2,
3989 .channels_max = 2,
Ian Minett825315b2012-12-20 18:53:36 -08003990 .ops = {
3991 .prepare = ca0132_capture_pcm_prepare,
3992 .cleanup = ca0132_capture_pcm_cleanup
3993 },
Ian Minett95c6e9c2011-06-15 15:35:17 -07003994};
3995
3996static struct hda_pcm_stream ca0132_pcm_digital_playback = {
3997 .substreams = 1,
3998 .channels_min = 2,
3999 .channels_max = 2,
4000 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02004001 .open = ca0132_dig_playback_pcm_open,
4002 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004003 .prepare = ca0132_dig_playback_pcm_prepare,
4004 .cleanup = ca0132_dig_playback_pcm_cleanup
4005 },
4006};
4007
4008static struct hda_pcm_stream ca0132_pcm_digital_capture = {
4009 .substreams = 1,
4010 .channels_min = 2,
4011 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004012};
4013
4014static int ca0132_build_pcms(struct hda_codec *codec)
4015{
4016 struct ca0132_spec *spec = codec->spec;
4017 struct hda_pcm *info = spec->pcm_rec;
4018
4019 codec->pcm_info = info;
4020 codec->num_pcms = 0;
4021
4022 info->name = "CA0132 Analog";
4023 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
4024 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
4025 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4026 spec->multiout.max_channels;
4027 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
Ian Minett825315b2012-12-20 18:53:36 -08004028 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004029 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
4030 codec->num_pcms++;
4031
Ian Minett825315b2012-12-20 18:53:36 -08004032 info++;
4033 info->name = "CA0132 Analog Mic-In2";
4034 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4035 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4036 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
4037 codec->num_pcms++;
4038
4039 info++;
4040 info->name = "CA0132 What U Hear";
4041 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
4042 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
4043 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
4044 codec->num_pcms++;
4045
Ian Minett95c6e9c2011-06-15 15:35:17 -07004046 if (!spec->dig_out && !spec->dig_in)
4047 return 0;
4048
4049 info++;
4050 info->name = "CA0132 Digital";
4051 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4052 if (spec->dig_out) {
4053 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4054 ca0132_pcm_digital_playback;
4055 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
4056 }
4057 if (spec->dig_in) {
4058 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4059 ca0132_pcm_digital_capture;
4060 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
4061 }
4062 codec->num_pcms++;
4063
4064 return 0;
4065}
4066
Ian Minett441aa6a2012-12-20 18:53:40 -08004067static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
4068{
4069 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01004070 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
Ian Minett441aa6a2012-12-20 18:53:40 -08004071 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
4072 snd_hda_codec_write(codec, pin, 0,
4073 AC_VERB_SET_AMP_GAIN_MUTE,
4074 AMP_OUT_UNMUTE);
4075 }
4076 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
4077 snd_hda_codec_write(codec, dac, 0,
4078 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
4079}
4080
4081static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
4082{
4083 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01004084 snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
Ian Minett441aa6a2012-12-20 18:53:40 -08004085 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
4086 snd_hda_codec_write(codec, pin, 0,
4087 AC_VERB_SET_AMP_GAIN_MUTE,
4088 AMP_IN_UNMUTE(0));
4089 }
4090 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
4091 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4092 AMP_IN_UNMUTE(0));
4093
4094 /* init to 0 dB and unmute. */
4095 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4096 HDA_AMP_VOLMASK, 0x5a);
4097 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
4098 HDA_AMP_MUTE, 0);
4099 }
4100}
4101
Ian Minetta73d5112012-12-20 18:53:37 -08004102static void ca0132_init_unsol(struct hda_codec *codec)
4103{
4104 snd_hda_jack_detect_enable(codec, UNSOL_TAG_HP, UNSOL_TAG_HP);
4105 snd_hda_jack_detect_enable(codec, UNSOL_TAG_AMIC1, UNSOL_TAG_AMIC1);
4106}
4107
Ian Minett5aaca442012-12-20 18:53:34 -08004108static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
4109{
4110 unsigned int caps;
4111
4112 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
4113 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
4114 snd_hda_override_amp_caps(codec, nid, dir, caps);
4115}
4116
4117/*
4118 * Switch between Digital built-in mic and analog mic.
4119 */
4120static void ca0132_set_dmic(struct hda_codec *codec, int enable)
4121{
4122 struct ca0132_spec *spec = codec->spec;
4123 unsigned int tmp;
4124 u8 val;
4125 unsigned int oldval;
4126
4127 snd_printdd(KERN_INFO "ca0132_set_dmic: enable=%d\n", enable);
4128
4129 oldval = stop_mic1(codec);
4130 ca0132_set_vipsource(codec, 0);
4131 if (enable) {
4132 /* set DMic input as 2-ch */
4133 tmp = FLOAT_TWO;
4134 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4135
4136 val = spec->dmic_ctl;
4137 val |= 0x80;
4138 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4139 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4140
4141 if (!(spec->dmic_ctl & 0x20))
4142 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
4143 } else {
4144 /* set AMic input as mono */
4145 tmp = FLOAT_ONE;
4146 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4147
4148 val = spec->dmic_ctl;
4149 /* clear bit7 and bit5 to disable dmic */
4150 val &= 0x5f;
4151 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4152 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4153
4154 if (!(spec->dmic_ctl & 0x20))
4155 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
4156 }
4157 ca0132_set_vipsource(codec, 1);
4158 resume_mic1(codec, oldval);
4159}
4160
4161/*
4162 * Initialization for Digital Mic.
4163 */
4164static void ca0132_init_dmic(struct hda_codec *codec)
4165{
4166 struct ca0132_spec *spec = codec->spec;
4167 u8 val;
4168
4169 /* Setup Digital Mic here, but don't enable.
4170 * Enable based on jack detect.
4171 */
4172
4173 /* MCLK uses MPIO1, set to enable.
4174 * Bit 2-0: MPIO select
4175 * Bit 3: set to disable
4176 * Bit 7-4: reserved
4177 */
4178 val = 0x01;
4179 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4180 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
4181
4182 /* Data1 uses MPIO3. Data2 not use
4183 * Bit 2-0: Data1 MPIO select
4184 * Bit 3: set disable Data1
4185 * Bit 6-4: Data2 MPIO select
4186 * Bit 7: set disable Data2
4187 */
4188 val = 0x83;
4189 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4190 VENDOR_CHIPIO_DMIC_PIN_SET, val);
4191
4192 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
4193 * Bit 3-0: Channel mask
4194 * Bit 4: set for 48KHz, clear for 32KHz
4195 * Bit 5: mode
4196 * Bit 6: set to select Data2, clear for Data1
4197 * Bit 7: set to enable DMic, clear for AMic
4198 */
4199 val = 0x23;
4200 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
4201 spec->dmic_ctl = val;
4202 snd_hda_codec_write(codec, spec->input_pins[0], 0,
4203 VENDOR_CHIPIO_DMIC_CTL_SET, val);
4204}
4205
4206/*
4207 * Initialization for Analog Mic 2
4208 */
4209static void ca0132_init_analog_mic2(struct hda_codec *codec)
4210{
4211 struct ca0132_spec *spec = codec->spec;
4212
4213 mutex_lock(&spec->chipio_mutex);
4214 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4215 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
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 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4221 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
4222 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4223 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
4224 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
4225 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
4226 mutex_unlock(&spec->chipio_mutex);
4227}
4228
4229static void ca0132_refresh_widget_caps(struct hda_codec *codec)
4230{
4231 struct ca0132_spec *spec = codec->spec;
4232 int i;
4233 hda_nid_t nid;
4234
4235 snd_printdd(KERN_INFO "ca0132_refresh_widget_caps.\n");
4236 nid = codec->start_nid;
4237 for (i = 0; i < codec->num_nodes; i++, nid++)
4238 codec->wcaps[i] = snd_hda_param_read(codec, nid,
4239 AC_PAR_AUDIO_WIDGET_CAP);
4240
4241 for (i = 0; i < spec->multiout.num_dacs; i++)
4242 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
4243
4244 for (i = 0; i < spec->num_outputs; i++)
4245 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
4246
4247 for (i = 0; i < spec->num_inputs; i++) {
4248 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
4249 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
4250 }
4251}
4252
4253/*
4254 * Setup default parameters for DSP
4255 */
4256static void ca0132_setup_defaults(struct hda_codec *codec)
4257{
4258 unsigned int tmp;
4259 int num_fx;
4260 int idx, i;
4261
4262 if (!dspload_is_loaded(codec))
4263 return;
4264
4265 /* out, in effects + voicefx */
4266 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
4267 for (idx = 0; idx < num_fx; idx++) {
4268 for (i = 0; i <= ca0132_effects[idx].params; i++) {
4269 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
4270 ca0132_effects[idx].reqs[i],
4271 ca0132_effects[idx].def_vals[i]);
4272 }
4273 }
4274
4275 /*remove DSP headroom*/
4276 tmp = FLOAT_ZERO;
4277 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
4278
4279 /*set speaker EQ bypass attenuation*/
4280 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
4281
4282 /* set AMic1 and AMic2 as mono mic */
4283 tmp = FLOAT_ONE;
4284 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4285 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
4286
4287 /* set AMic1 as CrystalVoice input */
4288 tmp = FLOAT_ONE;
4289 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4290
4291 /* set WUH source */
4292 tmp = FLOAT_TWO;
4293 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
4294}
4295
4296/*
4297 * Initialization of flags in chip
4298 */
4299static void ca0132_init_flags(struct hda_codec *codec)
4300{
4301 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
4302 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
4303 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
4304 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
4305 chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
4306 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
4307}
4308
4309/*
4310 * Initialization of parameters in chip
4311 */
4312static void ca0132_init_params(struct hda_codec *codec)
4313{
4314 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
4315 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
4316}
Ian Minett95c6e9c2011-06-15 15:35:17 -07004317
Ian Minette90f29e2012-12-20 18:53:39 -08004318static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
4319{
4320 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
4321 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
4322 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
4323 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
4324 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
4325 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
4326
Ian Minett406261c2012-12-20 18:53:41 -08004327 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4328 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
Ian Minette90f29e2012-12-20 18:53:39 -08004329 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
4330}
4331
4332static bool ca0132_download_dsp_images(struct hda_codec *codec)
4333{
4334 bool dsp_loaded = false;
4335 const struct dsp_image_seg *dsp_os_image;
Takashi Iwai15e4ba62013-01-15 17:08:38 +01004336 const struct firmware *fw_entry;
Ian Minette90f29e2012-12-20 18:53:39 -08004337
Takashi Iwai15e4ba62013-01-15 17:08:38 +01004338 if (request_firmware(&fw_entry, EFX_FILE, codec->bus->card->dev) != 0)
Ian Minette90f29e2012-12-20 18:53:39 -08004339 return false;
4340
Takashi Iwai15e4ba62013-01-15 17:08:38 +01004341 dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
Ian Minette90f29e2012-12-20 18:53:39 -08004342 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
4343 dsp_loaded = dspload_wait_loaded(codec);
4344
Takashi Iwai15e4ba62013-01-15 17:08:38 +01004345 release_firmware(fw_entry);
4346
4347
Ian Minette90f29e2012-12-20 18:53:39 -08004348 return dsp_loaded;
4349}
4350
4351static void ca0132_download_dsp(struct hda_codec *codec)
4352{
4353 struct ca0132_spec *spec = codec->spec;
4354
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004355#ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
4356 return; /* NOP */
4357#endif
Ian Minette90f29e2012-12-20 18:53:39 -08004358 spec->dsp_state = DSP_DOWNLOAD_INIT;
4359
4360 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
4361 chipio_enable_clocks(codec);
4362 spec->dsp_state = DSP_DOWNLOADING;
4363 if (!ca0132_download_dsp_images(codec))
4364 spec->dsp_state = DSP_DOWNLOAD_FAILED;
4365 else
4366 spec->dsp_state = DSP_DOWNLOADED;
4367 }
4368
4369 if (spec->dsp_state == DSP_DOWNLOADED)
4370 ca0132_set_dsp_msr(codec, true);
4371}
4372
Ian Minette90f29e2012-12-20 18:53:39 -08004373static void ca0132_process_dsp_response(struct hda_codec *codec)
4374{
4375 struct ca0132_spec *spec = codec->spec;
4376
4377 snd_printdd(KERN_INFO "ca0132_process_dsp_response\n");
4378 if (spec->wait_scp) {
4379 if (dspio_get_response_data(codec) >= 0)
4380 spec->wait_scp = 0;
4381 }
4382
4383 dspio_clear_response_queue(codec);
4384}
4385
4386static void ca0132_unsol_event(struct hda_codec *codec, unsigned int res)
4387{
4388 snd_printdd(KERN_INFO "ca0132_unsol_event: 0x%x\n", res);
4389
4390
4391 if (((res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f) == UNSOL_TAG_DSP) {
4392 ca0132_process_dsp_response(codec);
4393 } else {
4394 res = snd_hda_jack_get_action(codec,
4395 (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x3f);
4396
4397 snd_printdd(KERN_INFO "snd_hda_jack_get_action: 0x%x\n", res);
4398
4399 switch (res) {
4400 case UNSOL_TAG_HP:
4401 ca0132_select_out(codec);
4402 snd_hda_jack_report_sync(codec);
4403 break;
4404 case UNSOL_TAG_AMIC1:
4405 ca0132_select_mic(codec);
4406 snd_hda_jack_report_sync(codec);
4407 break;
4408 default:
4409 break;
4410 }
4411 }
4412}
4413
Ian Minett5aaca442012-12-20 18:53:34 -08004414/*
4415 * Verbs tables.
4416 */
4417
4418/* Sends before DSP download. */
4419static struct hda_verb ca0132_base_init_verbs[] = {
4420 /*enable ct extension*/
4421 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
4422 /*enable DSP node unsol, needed for DSP download*/
4423 {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_DSP},
4424 {}
4425};
4426
4427/* Send at exit. */
4428static struct hda_verb ca0132_base_exit_verbs[] = {
4429 /*set afg to D3*/
4430 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
4431 /*disable ct extension*/
4432 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
4433 {}
4434};
4435
4436/* Other verbs tables. Sends after DSP download. */
4437static struct hda_verb ca0132_init_verbs0[] = {
4438 /* chip init verbs */
4439 {0x15, 0x70D, 0xF0},
4440 {0x15, 0x70E, 0xFE},
4441 {0x15, 0x707, 0x75},
4442 {0x15, 0x707, 0xD3},
4443 {0x15, 0x707, 0x09},
4444 {0x15, 0x707, 0x53},
4445 {0x15, 0x707, 0xD4},
4446 {0x15, 0x707, 0xEF},
4447 {0x15, 0x707, 0x75},
4448 {0x15, 0x707, 0xD3},
4449 {0x15, 0x707, 0x09},
4450 {0x15, 0x707, 0x02},
4451 {0x15, 0x707, 0x37},
4452 {0x15, 0x707, 0x78},
4453 {0x15, 0x53C, 0xCE},
4454 {0x15, 0x575, 0xC9},
4455 {0x15, 0x53D, 0xCE},
4456 {0x15, 0x5B7, 0xC9},
4457 {0x15, 0x70D, 0xE8},
4458 {0x15, 0x70E, 0xFE},
4459 {0x15, 0x707, 0x02},
4460 {0x15, 0x707, 0x68},
4461 {0x15, 0x707, 0x62},
4462 {0x15, 0x53A, 0xCE},
4463 {0x15, 0x546, 0xC9},
4464 {0x15, 0x53B, 0xCE},
4465 {0x15, 0x5E8, 0xC9},
4466 {0x15, 0x717, 0x0D},
4467 {0x15, 0x718, 0x20},
4468 {}
4469};
4470
4471static struct hda_verb ca0132_init_verbs1[] = {
4472 {0x10, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_HP},
4473 {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | UNSOL_TAG_AMIC1},
4474 /* config EAPD */
4475 {0x0b, 0x78D, 0x00},
4476 /*{0x0b, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4477 /*{0x10, 0x78D, 0x02},*/
4478 /*{0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x02},*/
4479 {}
4480};
4481
Ian Minett95c6e9c2011-06-15 15:35:17 -07004482static void ca0132_init_chip(struct hda_codec *codec)
4483{
4484 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08004485 int num_fx;
4486 int i;
4487 unsigned int on;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004488
4489 mutex_init(&spec->chipio_mutex);
Ian Minett5aaca442012-12-20 18:53:34 -08004490
4491 spec->cur_out_type = SPEAKER_OUT;
4492 spec->cur_mic_type = DIGITAL_MIC;
4493 spec->cur_mic_boost = 0;
4494
4495 for (i = 0; i < VNODES_COUNT; i++) {
4496 spec->vnode_lvol[i] = 0x5a;
4497 spec->vnode_rvol[i] = 0x5a;
4498 spec->vnode_lswitch[i] = 0;
4499 spec->vnode_rswitch[i] = 0;
4500 }
4501
4502 /*
4503 * Default states for effects are in ca0132_effects[].
4504 */
4505 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4506 for (i = 0; i < num_fx; i++) {
4507 on = (unsigned int)ca0132_effects[i].reqs[0];
4508 spec->effects_switch[i] = on ? 1 : 0;
4509 }
4510
4511 spec->voicefx_val = 0;
4512 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
4513 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
4514
Ian Minett44f0c972012-12-20 18:53:38 -08004515#ifdef ENABLE_TUNING_CONTROLS
4516 ca0132_init_tuning_defaults(codec);
4517#endif
Ian Minett95c6e9c2011-06-15 15:35:17 -07004518}
4519
4520static void ca0132_exit_chip(struct hda_codec *codec)
4521{
4522 /* put any chip cleanup stuffs here. */
Ian Minett5aaca442012-12-20 18:53:34 -08004523
4524 if (dspload_is_loaded(codec))
4525 dsp_reset(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004526}
4527
4528static int ca0132_init(struct hda_codec *codec)
4529{
4530 struct ca0132_spec *spec = codec->spec;
4531 struct auto_pin_cfg *cfg = &spec->autocfg;
4532 int i;
4533
Ian Minett5aaca442012-12-20 18:53:34 -08004534 spec->dsp_state = DSP_DOWNLOAD_INIT;
4535 spec->curr_chip_addx = (unsigned int)INVALID_CHIP_ADDRESS;
4536
4537 snd_hda_power_up(codec);
4538
4539 ca0132_init_params(codec);
4540 ca0132_init_flags(codec);
4541 snd_hda_sequence_write(codec, spec->base_init_verbs);
Ian Minett01ef7db2012-09-20 20:29:16 -07004542 ca0132_download_dsp(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004543 ca0132_refresh_widget_caps(codec);
4544 ca0132_setup_defaults(codec);
4545 ca0132_init_analog_mic2(codec);
4546 ca0132_init_dmic(codec);
Ian Minett01ef7db2012-09-20 20:29:16 -07004547
Ian Minett5aaca442012-12-20 18:53:34 -08004548 for (i = 0; i < spec->num_outputs; i++)
4549 init_output(codec, spec->out_pins[i], spec->dacs[0]);
4550
Ian Minett95c6e9c2011-06-15 15:35:17 -07004551 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
4552
4553 for (i = 0; i < spec->num_inputs; i++)
4554 init_input(codec, spec->input_pins[i], spec->adcs[i]);
4555
4556 init_input(codec, cfg->dig_in_pin, spec->dig_in);
4557
Ian Minett5aaca442012-12-20 18:53:34 -08004558 for (i = 0; i < spec->num_init_verbs; i++)
4559 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4560
Ian Minetta73d5112012-12-20 18:53:37 -08004561 ca0132_init_unsol(codec);
4562
Ian Minett5aaca442012-12-20 18:53:34 -08004563 ca0132_select_out(codec);
4564 ca0132_select_mic(codec);
4565
Ian Minetta73d5112012-12-20 18:53:37 -08004566 snd_hda_jack_report_sync(codec);
4567
Ian Minett5aaca442012-12-20 18:53:34 -08004568 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004569
4570 return 0;
4571}
4572
Ian Minett95c6e9c2011-06-15 15:35:17 -07004573static void ca0132_free(struct hda_codec *codec)
4574{
Ian Minett5aaca442012-12-20 18:53:34 -08004575 struct ca0132_spec *spec = codec->spec;
4576
4577 snd_hda_power_up(codec);
4578 snd_hda_sequence_write(codec, spec->base_exit_verbs);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004579 ca0132_exit_chip(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004580 snd_hda_power_down(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07004581 kfree(codec->spec);
4582}
4583
4584static struct hda_codec_ops ca0132_patch_ops = {
4585 .build_controls = ca0132_build_controls,
4586 .build_pcms = ca0132_build_pcms,
4587 .init = ca0132_init,
4588 .free = ca0132_free,
Ian Minetta73d5112012-12-20 18:53:37 -08004589 .unsol_event = ca0132_unsol_event,
Ian Minett95c6e9c2011-06-15 15:35:17 -07004590};
4591
Ian Minett441aa6a2012-12-20 18:53:40 -08004592static void ca0132_config(struct hda_codec *codec)
4593{
4594 struct ca0132_spec *spec = codec->spec;
4595 struct auto_pin_cfg *cfg = &spec->autocfg;
4596
4597 spec->dacs[0] = 0x2;
4598 spec->dacs[1] = 0x3;
4599 spec->dacs[2] = 0x4;
4600
4601 spec->multiout.dac_nids = spec->dacs;
4602 spec->multiout.num_dacs = 3;
4603 spec->multiout.max_channels = 2;
4604
4605 spec->num_outputs = 2;
4606 spec->out_pins[0] = 0x0b; /* speaker out */
4607 spec->out_pins[1] = 0x10; /* headphone out */
4608 spec->shared_out_nid = 0x2;
4609
4610 spec->num_inputs = 3;
4611 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
4612 spec->adcs[1] = 0x8; /* analog mic2 */
4613 spec->adcs[2] = 0xa; /* what u hear */
4614 spec->shared_mic_nid = 0x7;
4615
4616 spec->input_pins[0] = 0x12;
4617 spec->input_pins[1] = 0x11;
4618 spec->input_pins[2] = 0x13;
4619
4620 /* SPDIF I/O */
4621 spec->dig_out = 0x05;
4622 spec->multiout.dig_out_nid = spec->dig_out;
4623 cfg->dig_out_pins[0] = 0x0c;
4624 cfg->dig_outs = 1;
4625 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
4626 spec->dig_in = 0x09;
4627 cfg->dig_in_pin = 0x0e;
4628 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4629}
4630
Ian Minett95c6e9c2011-06-15 15:35:17 -07004631static int patch_ca0132(struct hda_codec *codec)
4632{
4633 struct ca0132_spec *spec;
Ian Minetta73d5112012-12-20 18:53:37 -08004634 int err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07004635
4636 snd_printdd("patch_ca0132\n");
4637
4638 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4639 if (!spec)
4640 return -ENOMEM;
4641 codec->spec = spec;
4642
Ian Minetta7e76272012-12-20 18:53:35 -08004643 spec->num_mixers = 1;
4644 spec->mixers[0] = ca0132_mixer;
4645
Ian Minett5aaca442012-12-20 18:53:34 -08004646 spec->base_init_verbs = ca0132_base_init_verbs;
4647 spec->base_exit_verbs = ca0132_base_exit_verbs;
4648 spec->init_verbs[0] = ca0132_init_verbs0;
4649 spec->init_verbs[1] = ca0132_init_verbs1;
4650 spec->num_init_verbs = 2;
4651
Ian Minett95c6e9c2011-06-15 15:35:17 -07004652 ca0132_init_chip(codec);
4653
4654 ca0132_config(codec);
4655
Ian Minetta73d5112012-12-20 18:53:37 -08004656 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
4657 if (err < 0)
4658 return err;
4659
Ian Minett95c6e9c2011-06-15 15:35:17 -07004660 codec->patch_ops = ca0132_patch_ops;
4661
4662 return 0;
4663}
4664
4665/*
4666 * patch entries
4667 */
4668static struct hda_codec_preset snd_hda_preset_ca0132[] = {
4669 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
4670 {} /* terminator */
4671};
4672
4673MODULE_ALIAS("snd-hda-codec-id:11020011");
4674
4675MODULE_LICENSE("GPL");
Ian Minett406261c2012-12-20 18:53:41 -08004676MODULE_DESCRIPTION("Creative Sound Core3D codec");
Ian Minett95c6e9c2011-06-15 15:35:17 -07004677
4678static struct hda_codec_preset_list ca0132_list = {
4679 .preset = snd_hda_preset_ca0132,
4680 .owner = THIS_MODULE,
4681};
4682
4683static int __init patch_ca0132_init(void)
4684{
4685 return snd_hda_add_codec_preset(&ca0132_list);
4686}
4687
4688static void __exit patch_ca0132_exit(void)
4689{
Ian Minett95c6e9c2011-06-15 15:35:17 -07004690 snd_hda_delete_codec_preset(&ca0132_list);
4691}
4692
4693module_init(patch_ca0132_init)
4694module_exit(patch_ca0132_exit)