blob: b5bddb05c0647959ba8b8899282a29302f99b5d4 [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>
Ian Minett95c6e9c2011-06-15 15:35:17 -070027#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Ian Minett4aa3bb02012-09-20 20:29:15 -070029#include <linux/firmware.h>
Jérémy Lefaurea9291f42017-10-12 22:36:31 -040030#include <linux/kernel.h>
Connor McAdamsaa317042018-05-08 13:20:03 -040031#include <linux/types.h>
32#include <linux/io.h>
33#include <linux/pci.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070034#include <sound/core.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050035#include <sound/hda_codec.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070036#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020037#include "hda_auto_parser.h"
Ian Minett5aaca442012-12-20 18:53:34 -080038#include "hda_jack.h"
Ian Minett95c6e9c2011-06-15 15:35:17 -070039
Ian Minettbcd109c2012-09-20 20:29:14 -070040#include "ca0132_regs.h"
41
Ian Minettef6b2ea2012-12-20 18:53:33 -080042/* Enable this to see controls for tuning purpose. */
43/*#define ENABLE_TUNING_CONTROLS*/
44
Takashi Sakamoto8e142e92018-05-02 22:48:16 +090045#ifdef ENABLE_TUNING_CONTROLS
46#include <sound/tlv.h>
47#endif
48
Ian Minettef6b2ea2012-12-20 18:53:33 -080049#define FLOAT_ZERO 0x00000000
50#define FLOAT_ONE 0x3f800000
51#define FLOAT_TWO 0x40000000
Connor McAdams38ba69f2018-05-08 13:20:07 -040052#define FLOAT_THREE 0x40400000
Connor McAdams7cb9d942018-05-08 13:20:10 -040053#define FLOAT_EIGHT 0x41000000
Ian Minettef6b2ea2012-12-20 18:53:33 -080054#define FLOAT_MINUS_5 0xc0a00000
55
Ian Minettef6b2ea2012-12-20 18:53:33 -080056#define UNSOL_TAG_DSP 0x16
57
Ian Minett4aa3bb02012-09-20 20:29:15 -070058#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
59#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
60
61#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
62#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
63#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
64
65#define MASTERCONTROL 0x80
Ian Minettef6b2ea2012-12-20 18:53:33 -080066#define MASTERCONTROL_ALLOC_DMA_CHAN 10
67#define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
Ian Minett4aa3bb02012-09-20 20:29:15 -070068
Ian Minett95c6e9c2011-06-15 15:35:17 -070069#define WIDGET_CHIP_CTRL 0x15
70#define WIDGET_DSP_CTRL 0x16
71
Ian Minett4aa3bb02012-09-20 20:29:15 -070072#define MEM_CONNID_MICIN1 3
73#define MEM_CONNID_MICIN2 5
74#define MEM_CONNID_MICOUT1 12
75#define MEM_CONNID_MICOUT2 14
76#define MEM_CONNID_WUH 10
77#define MEM_CONNID_DSP 16
78#define MEM_CONNID_DMIC 100
79
80#define SCP_SET 0
81#define SCP_GET 1
82
Ian Minett01ef7db2012-09-20 20:29:16 -070083#define EFX_FILE "ctefx.bin"
Connor McAdams8a19bce2018-05-08 13:20:01 -040084#define SBZ_EFX_FILE "ctefx-sbz.bin"
85#define R3DI_EFX_FILE "ctefx-r3di.bin"
Ian Minett01ef7db2012-09-20 20:29:16 -070086
Takashi Iwai24f3ced2013-02-04 18:25:51 +010087#ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
Ian Minett01ef7db2012-09-20 20:29:16 -070088MODULE_FIRMWARE(EFX_FILE);
Connor McAdams8a19bce2018-05-08 13:20:01 -040089MODULE_FIRMWARE(SBZ_EFX_FILE);
90MODULE_FIRMWARE(R3DI_EFX_FILE);
Takashi Iwai7a527ed2013-01-15 17:44:20 +010091#endif
Ian Minett01ef7db2012-09-20 20:29:16 -070092
Takashi Sakamoto3a03f832018-05-15 22:12:58 +090093static const char *const dirstr[2] = { "Playback", "Capture" };
Ian Minettef6b2ea2012-12-20 18:53:33 -080094
Connor McAdams7cb9d942018-05-08 13:20:10 -040095#define NUM_OF_OUTPUTS 3
Ian Minettef6b2ea2012-12-20 18:53:33 -080096enum {
97 SPEAKER_OUT,
Connor McAdams7cb9d942018-05-08 13:20:10 -040098 HEADPHONE_OUT,
99 SURROUND_OUT
Ian Minettef6b2ea2012-12-20 18:53:33 -0800100};
101
102enum {
103 DIGITAL_MIC,
104 LINE_MIC_IN
105};
106
Connor McAdams7cb9d942018-05-08 13:20:10 -0400107/* Strings for Input Source Enum Control */
Takashi Sakamoto3a03f832018-05-15 22:12:58 +0900108static const char *const in_src_str[3] = {"Rear Mic", "Line", "Front Mic" };
Connor McAdams7cb9d942018-05-08 13:20:10 -0400109#define IN_SRC_NUM_OF_INPUTS 3
110enum {
111 REAR_MIC,
112 REAR_LINE_IN,
113 FRONT_MIC,
114};
115
Ian Minettef6b2ea2012-12-20 18:53:33 -0800116enum {
117#define VNODE_START_NID 0x80
118 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
119 VNID_MIC,
120 VNID_HP_SEL,
121 VNID_AMIC1_SEL,
122 VNID_HP_ASEL,
123 VNID_AMIC1_ASEL,
124 VNODE_END_NID,
125#define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
126
127#define EFFECT_START_NID 0x90
128#define OUT_EFFECT_START_NID EFFECT_START_NID
129 SURROUND = OUT_EFFECT_START_NID,
130 CRYSTALIZER,
131 DIALOG_PLUS,
132 SMART_VOLUME,
133 X_BASS,
134 EQUALIZER,
135 OUT_EFFECT_END_NID,
136#define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
137
138#define IN_EFFECT_START_NID OUT_EFFECT_END_NID
139 ECHO_CANCELLATION = IN_EFFECT_START_NID,
140 VOICE_FOCUS,
141 MIC_SVM,
142 NOISE_REDUCTION,
143 IN_EFFECT_END_NID,
144#define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
145
146 VOICEFX = IN_EFFECT_END_NID,
147 PLAY_ENHANCEMENT,
148 CRYSTAL_VOICE,
Connor McAdams7cb9d942018-05-08 13:20:10 -0400149 EFFECT_END_NID,
150 OUTPUT_SOURCE_ENUM,
Connor McAdams47cdf762018-05-08 13:20:13 -0400151 INPUT_SOURCE_ENUM,
152 XBASS_XOVER,
153 EQ_PRESET_ENUM,
154 SMART_VOLUME_ENUM,
155 MIC_BOOST_ENUM
Ian Minettef6b2ea2012-12-20 18:53:33 -0800156#define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
157};
158
159/* Effects values size*/
160#define EFFECT_VALS_MAX_COUNT 12
161
Connor McAdams47cdf762018-05-08 13:20:13 -0400162/*
163 * Default values for the effect slider controls, they are in order of their
164 * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
165 * X-bass.
166 */
167static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
168/* Amount of effect level sliders for ca0132_alt controls. */
169#define EFFECT_LEVEL_SLIDERS 5
170
Dylan Reide8412ca2013-04-04 13:55:09 -0700171/* Latency introduced by DSP blocks in milliseconds. */
172#define DSP_CAPTURE_INIT_LATENCY 0
173#define DSP_CRYSTAL_VOICE_LATENCY 124
174#define DSP_PLAYBACK_INIT_LATENCY 13
175#define DSP_PLAY_ENHANCEMENT_LATENCY 30
176#define DSP_SPEAKER_OUT_LATENCY 7
177
Ian Minettef6b2ea2012-12-20 18:53:33 -0800178struct ct_effect {
Takashi Iwai975cc022013-06-28 11:56:49 +0200179 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minettef6b2ea2012-12-20 18:53:33 -0800180 hda_nid_t nid;
181 int mid; /*effect module ID*/
182 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
183 int direct; /* 0:output; 1:input*/
184 int params; /* number of default non-on/off params */
185 /*effect default values, 1st is on/off. */
186 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
187};
188
189#define EFX_DIR_OUT 0
190#define EFX_DIR_IN 1
191
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900192static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800193 { .name = "Surround",
194 .nid = SURROUND,
195 .mid = 0x96,
196 .reqs = {0, 1},
197 .direct = EFX_DIR_OUT,
198 .params = 1,
199 .def_vals = {0x3F800000, 0x3F2B851F}
200 },
201 { .name = "Crystalizer",
202 .nid = CRYSTALIZER,
203 .mid = 0x96,
204 .reqs = {7, 8},
205 .direct = EFX_DIR_OUT,
206 .params = 1,
207 .def_vals = {0x3F800000, 0x3F266666}
208 },
209 { .name = "Dialog Plus",
210 .nid = DIALOG_PLUS,
211 .mid = 0x96,
212 .reqs = {2, 3},
213 .direct = EFX_DIR_OUT,
214 .params = 1,
215 .def_vals = {0x00000000, 0x3F000000}
216 },
217 { .name = "Smart Volume",
218 .nid = SMART_VOLUME,
219 .mid = 0x96,
220 .reqs = {4, 5, 6},
221 .direct = EFX_DIR_OUT,
222 .params = 2,
223 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
224 },
225 { .name = "X-Bass",
226 .nid = X_BASS,
227 .mid = 0x96,
228 .reqs = {24, 23, 25},
229 .direct = EFX_DIR_OUT,
230 .params = 2,
231 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
232 },
233 { .name = "Equalizer",
234 .nid = EQUALIZER,
235 .mid = 0x96,
236 .reqs = {9, 10, 11, 12, 13, 14,
237 15, 16, 17, 18, 19, 20},
238 .direct = EFX_DIR_OUT,
239 .params = 11,
240 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
241 0x00000000, 0x00000000, 0x00000000, 0x00000000,
242 0x00000000, 0x00000000, 0x00000000, 0x00000000}
243 },
244 { .name = "Echo Cancellation",
245 .nid = ECHO_CANCELLATION,
246 .mid = 0x95,
247 .reqs = {0, 1, 2, 3},
248 .direct = EFX_DIR_IN,
249 .params = 3,
250 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
251 },
252 { .name = "Voice Focus",
253 .nid = VOICE_FOCUS,
254 .mid = 0x95,
255 .reqs = {6, 7, 8, 9},
256 .direct = EFX_DIR_IN,
257 .params = 3,
258 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
259 },
260 { .name = "Mic SVM",
261 .nid = MIC_SVM,
262 .mid = 0x95,
263 .reqs = {44, 45},
264 .direct = EFX_DIR_IN,
265 .params = 1,
266 .def_vals = {0x00000000, 0x3F3D70A4}
267 },
268 { .name = "Noise Reduction",
269 .nid = NOISE_REDUCTION,
270 .mid = 0x95,
271 .reqs = {4, 5},
272 .direct = EFX_DIR_IN,
273 .params = 1,
274 .def_vals = {0x3F800000, 0x3F000000}
275 },
276 { .name = "VoiceFX",
277 .nid = VOICEFX,
278 .mid = 0x95,
279 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
280 .direct = EFX_DIR_IN,
281 .params = 8,
282 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
283 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
284 0x00000000}
285 }
286};
287
288/* Tuning controls */
289#ifdef ENABLE_TUNING_CONTROLS
290
291enum {
292#define TUNING_CTL_START_NID 0xC0
293 WEDGE_ANGLE = TUNING_CTL_START_NID,
294 SVM_LEVEL,
295 EQUALIZER_BAND_0,
296 EQUALIZER_BAND_1,
297 EQUALIZER_BAND_2,
298 EQUALIZER_BAND_3,
299 EQUALIZER_BAND_4,
300 EQUALIZER_BAND_5,
301 EQUALIZER_BAND_6,
302 EQUALIZER_BAND_7,
303 EQUALIZER_BAND_8,
304 EQUALIZER_BAND_9,
305 TUNING_CTL_END_NID
306#define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
307};
308
309struct ct_tuning_ctl {
Takashi Iwai975cc022013-06-28 11:56:49 +0200310 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minettef6b2ea2012-12-20 18:53:33 -0800311 hda_nid_t parent_nid;
312 hda_nid_t nid;
313 int mid; /*effect module ID*/
314 int req; /*effect module request*/
315 int direct; /* 0:output; 1:input*/
316 unsigned int def_val;/*effect default values*/
317};
318
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900319static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800320 { .name = "Wedge Angle",
321 .parent_nid = VOICE_FOCUS,
322 .nid = WEDGE_ANGLE,
323 .mid = 0x95,
324 .req = 8,
325 .direct = EFX_DIR_IN,
326 .def_val = 0x41F00000
327 },
328 { .name = "SVM Level",
329 .parent_nid = MIC_SVM,
330 .nid = SVM_LEVEL,
331 .mid = 0x95,
332 .req = 45,
333 .direct = EFX_DIR_IN,
334 .def_val = 0x3F3D70A4
335 },
336 { .name = "EQ Band0",
337 .parent_nid = EQUALIZER,
338 .nid = EQUALIZER_BAND_0,
339 .mid = 0x96,
340 .req = 11,
341 .direct = EFX_DIR_OUT,
342 .def_val = 0x00000000
343 },
344 { .name = "EQ Band1",
345 .parent_nid = EQUALIZER,
346 .nid = EQUALIZER_BAND_1,
347 .mid = 0x96,
348 .req = 12,
349 .direct = EFX_DIR_OUT,
350 .def_val = 0x00000000
351 },
352 { .name = "EQ Band2",
353 .parent_nid = EQUALIZER,
354 .nid = EQUALIZER_BAND_2,
355 .mid = 0x96,
356 .req = 13,
357 .direct = EFX_DIR_OUT,
358 .def_val = 0x00000000
359 },
360 { .name = "EQ Band3",
361 .parent_nid = EQUALIZER,
362 .nid = EQUALIZER_BAND_3,
363 .mid = 0x96,
364 .req = 14,
365 .direct = EFX_DIR_OUT,
366 .def_val = 0x00000000
367 },
368 { .name = "EQ Band4",
369 .parent_nid = EQUALIZER,
370 .nid = EQUALIZER_BAND_4,
371 .mid = 0x96,
372 .req = 15,
373 .direct = EFX_DIR_OUT,
374 .def_val = 0x00000000
375 },
376 { .name = "EQ Band5",
377 .parent_nid = EQUALIZER,
378 .nid = EQUALIZER_BAND_5,
379 .mid = 0x96,
380 .req = 16,
381 .direct = EFX_DIR_OUT,
382 .def_val = 0x00000000
383 },
384 { .name = "EQ Band6",
385 .parent_nid = EQUALIZER,
386 .nid = EQUALIZER_BAND_6,
387 .mid = 0x96,
388 .req = 17,
389 .direct = EFX_DIR_OUT,
390 .def_val = 0x00000000
391 },
392 { .name = "EQ Band7",
393 .parent_nid = EQUALIZER,
394 .nid = EQUALIZER_BAND_7,
395 .mid = 0x96,
396 .req = 18,
397 .direct = EFX_DIR_OUT,
398 .def_val = 0x00000000
399 },
400 { .name = "EQ Band8",
401 .parent_nid = EQUALIZER,
402 .nid = EQUALIZER_BAND_8,
403 .mid = 0x96,
404 .req = 19,
405 .direct = EFX_DIR_OUT,
406 .def_val = 0x00000000
407 },
408 { .name = "EQ Band9",
409 .parent_nid = EQUALIZER,
410 .nid = EQUALIZER_BAND_9,
411 .mid = 0x96,
412 .req = 20,
413 .direct = EFX_DIR_OUT,
414 .def_val = 0x00000000
415 }
416};
417#endif
418
419/* Voice FX Presets */
420#define VOICEFX_MAX_PARAM_COUNT 9
421
422struct ct_voicefx {
423 char *name;
424 hda_nid_t nid;
425 int mid;
426 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
427};
428
429struct ct_voicefx_preset {
430 char *name; /*preset name*/
431 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
432};
433
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900434static const struct ct_voicefx ca0132_voicefx = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800435 .name = "VoiceFX Capture Switch",
436 .nid = VOICEFX,
437 .mid = 0x95,
438 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
439};
440
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900441static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800442 { .name = "Neutral",
443 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
444 0x44FA0000, 0x3F800000, 0x3F800000,
445 0x3F800000, 0x00000000, 0x00000000 }
446 },
447 { .name = "Female2Male",
448 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
449 0x44FA0000, 0x3F19999A, 0x3F866666,
450 0x3F800000, 0x00000000, 0x00000000 }
451 },
452 { .name = "Male2Female",
453 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
454 0x450AC000, 0x4017AE14, 0x3F6B851F,
455 0x3F800000, 0x00000000, 0x00000000 }
456 },
457 { .name = "ScrappyKid",
458 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
459 0x44FA0000, 0x40400000, 0x3F28F5C3,
460 0x3F800000, 0x00000000, 0x00000000 }
461 },
462 { .name = "Elderly",
463 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
464 0x44E10000, 0x3FB33333, 0x3FB9999A,
465 0x3F800000, 0x3E3A2E43, 0x00000000 }
466 },
467 { .name = "Orc",
468 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
469 0x45098000, 0x3F266666, 0x3FC00000,
470 0x3F800000, 0x00000000, 0x00000000 }
471 },
472 { .name = "Elf",
473 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
474 0x45193000, 0x3F8E147B, 0x3F75C28F,
475 0x3F800000, 0x00000000, 0x00000000 }
476 },
477 { .name = "Dwarf",
478 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
479 0x45007000, 0x3F451EB8, 0x3F7851EC,
480 0x3F800000, 0x00000000, 0x00000000 }
481 },
482 { .name = "AlienBrute",
483 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
484 0x451F6000, 0x3F266666, 0x3FA7D945,
485 0x3F800000, 0x3CF5C28F, 0x00000000 }
486 },
487 { .name = "Robot",
488 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
489 0x44FA0000, 0x3FB2718B, 0x3F800000,
490 0xBC07010E, 0x00000000, 0x00000000 }
491 },
492 { .name = "Marine",
493 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
494 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
495 0x3F0A3D71, 0x00000000, 0x00000000 }
496 },
497 { .name = "Emo",
498 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
499 0x44FA0000, 0x3F800000, 0x3F800000,
500 0x3E4CCCCD, 0x00000000, 0x00000000 }
501 },
502 { .name = "DeepVoice",
503 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
504 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
505 0x3F800000, 0x00000000, 0x00000000 }
506 },
507 { .name = "Munchkin",
508 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
509 0x44FA0000, 0x3F800000, 0x3F1A043C,
510 0x3F800000, 0x00000000, 0x00000000 }
511 }
512};
513
Connor McAdams47cdf762018-05-08 13:20:13 -0400514/* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
515
516#define EQ_PRESET_MAX_PARAM_COUNT 11
517
518struct ct_eq {
519 char *name;
520 hda_nid_t nid;
521 int mid;
522 int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
523};
524
525struct ct_eq_preset {
526 char *name; /*preset name*/
527 unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
528};
529
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900530static const struct ct_eq ca0132_alt_eq_enum = {
Connor McAdams47cdf762018-05-08 13:20:13 -0400531 .name = "FX: Equalizer Preset Switch",
532 .nid = EQ_PRESET_ENUM,
533 .mid = 0x96,
534 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
535};
536
537
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900538static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
Connor McAdams47cdf762018-05-08 13:20:13 -0400539 { .name = "Flat",
540 .vals = { 0x00000000, 0x00000000, 0x00000000,
541 0x00000000, 0x00000000, 0x00000000,
542 0x00000000, 0x00000000, 0x00000000,
543 0x00000000, 0x00000000 }
544 },
545 { .name = "Acoustic",
546 .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
547 0x40000000, 0x00000000, 0x00000000,
548 0x00000000, 0x00000000, 0x40000000,
549 0x40000000, 0x40000000 }
550 },
551 { .name = "Classical",
552 .vals = { 0x00000000, 0x00000000, 0x40C00000,
553 0x40C00000, 0x40466666, 0x00000000,
554 0x00000000, 0x00000000, 0x00000000,
555 0x40466666, 0x40466666 }
556 },
557 { .name = "Country",
558 .vals = { 0x00000000, 0xBF99999A, 0x00000000,
559 0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
560 0x00000000, 0x00000000, 0x40000000,
561 0x40466666, 0x40800000 }
562 },
563 { .name = "Dance",
564 .vals = { 0x00000000, 0xBF99999A, 0x40000000,
565 0x40466666, 0x40866666, 0xBF99999A,
566 0xBF99999A, 0x00000000, 0x00000000,
567 0x40800000, 0x40800000 }
568 },
569 { .name = "Jazz",
570 .vals = { 0x00000000, 0x00000000, 0x00000000,
571 0x3F8CCCCD, 0x40800000, 0x40800000,
572 0x40800000, 0x00000000, 0x3F8CCCCD,
573 0x40466666, 0x40466666 }
574 },
575 { .name = "New Age",
576 .vals = { 0x00000000, 0x00000000, 0x40000000,
577 0x40000000, 0x00000000, 0x00000000,
578 0x00000000, 0x3F8CCCCD, 0x40000000,
579 0x40000000, 0x40000000 }
580 },
581 { .name = "Pop",
582 .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
583 0x40000000, 0x40000000, 0x00000000,
584 0xBF99999A, 0xBF99999A, 0x00000000,
585 0x40466666, 0x40C00000 }
586 },
587 { .name = "Rock",
588 .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
589 0x3F8CCCCD, 0x40000000, 0xBF99999A,
590 0xBF99999A, 0x00000000, 0x00000000,
591 0x40800000, 0x40800000 }
592 },
593 { .name = "Vocal",
594 .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
595 0xBF99999A, 0x00000000, 0x40466666,
596 0x40800000, 0x40466666, 0x00000000,
597 0x00000000, 0x3F8CCCCD }
598 }
599};
600
Connor McAdams7cb9d942018-05-08 13:20:10 -0400601/* DSP command sequences for ca0132_alt_select_out */
602#define ALT_OUT_SET_MAX_COMMANDS 9 /* Max number of commands in sequence */
603struct ca0132_alt_out_set {
604 char *name; /*preset name*/
605 unsigned char commands;
606 unsigned int mids[ALT_OUT_SET_MAX_COMMANDS];
607 unsigned int reqs[ALT_OUT_SET_MAX_COMMANDS];
608 unsigned int vals[ALT_OUT_SET_MAX_COMMANDS];
609};
610
611static const struct ca0132_alt_out_set alt_out_presets[] = {
612 { .name = "Line Out",
613 .commands = 7,
614 .mids = { 0x96, 0x96, 0x96, 0x8F,
615 0x96, 0x96, 0x96 },
616 .reqs = { 0x19, 0x17, 0x18, 0x01,
617 0x1F, 0x15, 0x3A },
618 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
619 0x00000000, 0x00000000, 0x00000000,
620 0x00000000 }
621 },
622 { .name = "Headphone",
623 .commands = 7,
624 .mids = { 0x96, 0x96, 0x96, 0x8F,
625 0x96, 0x96, 0x96 },
626 .reqs = { 0x19, 0x17, 0x18, 0x01,
627 0x1F, 0x15, 0x3A },
628 .vals = { 0x3F000000, 0x42A00000, 0x00000000,
629 0x00000000, 0x00000000, 0x00000000,
630 0x00000000 }
631 },
632 { .name = "Surround",
633 .commands = 8,
634 .mids = { 0x96, 0x8F, 0x96, 0x96,
635 0x96, 0x96, 0x96, 0x96 },
636 .reqs = { 0x18, 0x01, 0x1F, 0x15,
637 0x3A, 0x1A, 0x1B, 0x1C },
638 .vals = { 0x00000000, 0x00000000, 0x00000000,
639 0x00000000, 0x00000000, 0x00000000,
640 0x00000000, 0x00000000 }
641 }
642};
643
Connor McAdams017310f2018-05-08 13:20:11 -0400644/*
645 * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
646 * and I don't know what the third req is, but it's always zero. I assume it's
647 * some sort of update or set command to tell the DSP there's new volume info.
648 */
649#define DSP_VOL_OUT 0
650#define DSP_VOL_IN 1
651
652struct ct_dsp_volume_ctl {
653 hda_nid_t vnid;
654 int mid; /* module ID*/
655 unsigned int reqs[3]; /* scp req ID */
656};
657
Takashi Sakamoto862154b2018-05-15 22:13:00 +0900658static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
Connor McAdams017310f2018-05-08 13:20:11 -0400659 { .vnid = VNID_SPK,
660 .mid = 0x32,
661 .reqs = {3, 4, 2}
662 },
663 { .vnid = VNID_MIC,
664 .mid = 0x37,
665 .reqs = {2, 3, 1}
666 }
667};
668
Ian Minett95c6e9c2011-06-15 15:35:17 -0700669enum hda_cmd_vendor_io {
670 /* for DspIO node */
671 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
672 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
673
674 VENDOR_DSPIO_STATUS = 0xF01,
675 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
676 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
677 VENDOR_DSPIO_DSP_INIT = 0x703,
678 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
679 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
680
681 /* for ChipIO node */
682 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
683 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
684 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
685 VENDOR_CHIPIO_DATA_LOW = 0x300,
686 VENDOR_CHIPIO_DATA_HIGH = 0x400,
687
Connor McAdamsbf85a912018-09-18 14:33:29 -0400688 VENDOR_CHIPIO_8051_WRITE_DIRECT = 0x500,
689 VENDOR_CHIPIO_8051_READ_DIRECT = 0xD00,
690
Ian Minett95c6e9c2011-06-15 15:35:17 -0700691 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
692 VENDOR_CHIPIO_STATUS = 0xF01,
693 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
694 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
695
Ian Minett4aa3bb02012-09-20 20:29:15 -0700696 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
697 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
Connor McAdamsbf85a912018-09-18 14:33:29 -0400698 VENDOR_CHIPIO_8051_PMEM_READ = 0xF08,
699 VENDOR_CHIPIO_8051_IRAM_WRITE = 0x709,
700 VENDOR_CHIPIO_8051_IRAM_READ = 0xF09,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700701
Ian Minett95c6e9c2011-06-15 15:35:17 -0700702 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700703 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700704
705 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
706 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
707 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
708 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
709 VENDOR_CHIPIO_FLAG_SET = 0x70F,
710 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700711 VENDOR_CHIPIO_PARAM_SET = 0x710,
712 VENDOR_CHIPIO_PARAM_GET = 0xF10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700713
714 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
715 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
716 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
717 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
718
Ian Minett4aa3bb02012-09-20 20:29:15 -0700719 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
720 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
721 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
722 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
723
724 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
725 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
726 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
727 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
728 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
729 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
730
731 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
Ian Minett95c6e9c2011-06-15 15:35:17 -0700732};
733
734/*
735 * Control flag IDs
736 */
737enum control_flag_id {
738 /* Connection manager stream setup is bypassed/enabled */
739 CONTROL_FLAG_C_MGR = 0,
740 /* DSP DMA is bypassed/enabled */
741 CONTROL_FLAG_DMA = 1,
742 /* 8051 'idle' mode is disabled/enabled */
743 CONTROL_FLAG_IDLE_ENABLE = 2,
744 /* Tracker for the SPDIF-in path is bypassed/enabled */
745 CONTROL_FLAG_TRACKER = 3,
746 /* DigitalOut to Spdif2Out connection is disabled/enabled */
747 CONTROL_FLAG_SPDIF2OUT = 4,
748 /* Digital Microphone is disabled/enabled */
749 CONTROL_FLAG_DMIC = 5,
750 /* ADC_B rate is 48 kHz/96 kHz */
751 CONTROL_FLAG_ADC_B_96KHZ = 6,
752 /* ADC_C rate is 48 kHz/96 kHz */
753 CONTROL_FLAG_ADC_C_96KHZ = 7,
754 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
755 CONTROL_FLAG_DAC_96KHZ = 8,
756 /* DSP rate is 48 kHz/96 kHz */
757 CONTROL_FLAG_DSP_96KHZ = 9,
758 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
759 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
760 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
761 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
762 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
763 CONTROL_FLAG_DECODE_LOOP = 12,
764 /* De-emphasis filter on DAC-1 disabled/enabled */
765 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
766 /* De-emphasis filter on DAC-2 disabled/enabled */
767 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
768 /* De-emphasis filter on DAC-3 disabled/enabled */
769 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
770 /* High-pass filter on ADC_B disabled/enabled */
771 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
772 /* High-pass filter on ADC_C disabled/enabled */
773 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
774 /* Common mode on Port_A disabled/enabled */
775 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
776 /* Common mode on Port_D disabled/enabled */
777 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
778 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
779 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
780 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700781 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700782 /* ASI rate is 48kHz/96kHz */
783 CONTROL_FLAG_ASI_96KHZ = 22,
784 /* DAC power settings able to control attached ports no/yes */
785 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
786 /* Clock Stop OK reporting is disabled/enabled */
787 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
788 /* Number of control flags */
789 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
790};
791
792/*
793 * Control parameter IDs
794 */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700795enum control_param_id {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800796 /* 0: None, 1: Mic1In*/
797 CONTROL_PARAM_VIP_SOURCE = 1,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700798 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
799 CONTROL_PARAM_SPDIF1_SOURCE = 2,
Ian Minettef6b2ea2012-12-20 18:53:33 -0800800 /* Port A output stage gain setting to use when 16 Ohm output
801 * impedance is selected*/
802 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
803 /* Port D output stage gain setting to use when 16 Ohm output
804 * impedance is selected*/
805 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700806
Connor McAdamsbf85a912018-09-18 14:33:29 -0400807 /*
808 * This control param name was found in the 8051 memory, and makes
809 * sense given the fact the AE-5 uses it and has the ASI flag set.
810 */
811 CONTROL_PARAM_ASI = 23,
812
Ian Minett95c6e9c2011-06-15 15:35:17 -0700813 /* Stream Control */
814
815 /* Select stream with the given ID */
816 CONTROL_PARAM_STREAM_ID = 24,
817 /* Source connection point for the selected stream */
818 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
819 /* Destination connection point for the selected stream */
820 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
821 /* Number of audio channels in the selected stream */
822 CONTROL_PARAM_STREAMS_CHANNELS = 27,
823 /*Enable control for the selected stream */
824 CONTROL_PARAM_STREAM_CONTROL = 28,
825
826 /* Connection Point Control */
827
828 /* Select connection point with the given ID */
829 CONTROL_PARAM_CONN_POINT_ID = 29,
830 /* Connection point sample rate */
831 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
832
833 /* Node Control */
834
835 /* Select HDA node with the given ID */
836 CONTROL_PARAM_NODE_ID = 31
837};
838
839/*
840 * Dsp Io Status codes
841 */
842enum hda_vendor_status_dspio {
843 /* Success */
844 VENDOR_STATUS_DSPIO_OK = 0x00,
845 /* Busy, unable to accept new command, the host must retry */
846 VENDOR_STATUS_DSPIO_BUSY = 0x01,
847 /* SCP command queue is full */
848 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
849 /* SCP response queue is empty */
850 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
851};
852
853/*
854 * Chip Io Status codes
855 */
856enum hda_vendor_status_chipio {
857 /* Success */
858 VENDOR_STATUS_CHIPIO_OK = 0x00,
859 /* Busy, unable to accept new command, the host must retry */
860 VENDOR_STATUS_CHIPIO_BUSY = 0x01
861};
862
863/*
864 * CA0132 sample rate
865 */
866enum ca0132_sample_rate {
867 SR_6_000 = 0x00,
868 SR_8_000 = 0x01,
869 SR_9_600 = 0x02,
870 SR_11_025 = 0x03,
871 SR_16_000 = 0x04,
872 SR_22_050 = 0x05,
873 SR_24_000 = 0x06,
874 SR_32_000 = 0x07,
875 SR_44_100 = 0x08,
876 SR_48_000 = 0x09,
877 SR_88_200 = 0x0A,
878 SR_96_000 = 0x0B,
879 SR_144_000 = 0x0C,
880 SR_176_400 = 0x0D,
881 SR_192_000 = 0x0E,
882 SR_384_000 = 0x0F,
883
884 SR_COUNT = 0x10,
885
886 SR_RATE_UNKNOWN = 0x1F
887};
888
Ian Minett01ef7db2012-09-20 20:29:16 -0700889enum dsp_download_state {
890 DSP_DOWNLOAD_FAILED = -1,
891 DSP_DOWNLOAD_INIT = 0,
892 DSP_DOWNLOADING = 1,
893 DSP_DOWNLOADED = 2
894};
895
Ian Minett01ef7db2012-09-20 20:29:16 -0700896/* retrieve parameters from hda format */
897#define get_hdafmt_chs(fmt) (fmt & 0xf)
898#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
899#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
900#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700901
902/*
903 * CA0132 specific
904 */
905
906struct ca0132_spec {
Takashi Sakamotob0eaa072018-05-15 22:12:57 +0900907 const struct snd_kcontrol_new *mixers[5];
Ian Minetta7e76272012-12-20 18:53:35 -0800908 unsigned int num_mixers;
Ian Minett5aaca442012-12-20 18:53:34 -0800909 const struct hda_verb *base_init_verbs;
910 const struct hda_verb *base_exit_verbs;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200911 const struct hda_verb *chip_init_verbs;
Connor McAdamse42c7c72018-08-08 13:34:18 -0400912 const struct hda_verb *desktop_init_verbs;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200913 struct hda_verb *spec_init_verbs;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700914 struct auto_pin_cfg autocfg;
Ian Minett5aaca442012-12-20 18:53:34 -0800915
916 /* Nodes configurations */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700917 struct hda_multi_out multiout;
918 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
919 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
Ian Minett5aaca442012-12-20 18:53:34 -0800920 unsigned int num_outputs;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700921 hda_nid_t input_pins[AUTO_PIN_LAST];
922 hda_nid_t adcs[AUTO_PIN_LAST];
923 hda_nid_t dig_out;
924 hda_nid_t dig_in;
925 unsigned int num_inputs;
Ian Minetta7e76272012-12-20 18:53:35 -0800926 hda_nid_t shared_mic_nid;
927 hda_nid_t shared_out_nid;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200928 hda_nid_t unsol_tag_hp;
Connor McAdams63177af2018-05-08 13:20:02 -0400929 hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200930 hda_nid_t unsol_tag_amic1;
Ian Minett01ef7db2012-09-20 20:29:16 -0700931
932 /* chip access */
933 struct mutex chipio_mutex; /* chip access mutex */
934 u32 curr_chip_addx;
935
936 /* DSP download related */
937 enum dsp_download_state dsp_state;
938 unsigned int dsp_stream_id;
939 unsigned int wait_scp;
940 unsigned int wait_scp_header;
941 unsigned int wait_num_data;
942 unsigned int scp_resp_header;
943 unsigned int scp_resp_data[4];
944 unsigned int scp_resp_count;
Connor McAdams8a19bce2018-05-08 13:20:01 -0400945 bool alt_firmware_present;
Connor McAdams38ba69f2018-05-08 13:20:07 -0400946 bool startup_check_entered;
Connor McAdamse93ac302018-05-08 13:20:05 -0400947 bool dsp_reload;
Ian Minett5aaca442012-12-20 18:53:34 -0800948
949 /* mixer and effects related */
950 unsigned char dmic_ctl;
951 int cur_out_type;
952 int cur_mic_type;
953 long vnode_lvol[VNODES_COUNT];
954 long vnode_rvol[VNODES_COUNT];
955 long vnode_lswitch[VNODES_COUNT];
956 long vnode_rswitch[VNODES_COUNT];
957 long effects_switch[EFFECTS_COUNT];
958 long voicefx_val;
959 long cur_mic_boost;
Connor McAdams7cb9d942018-05-08 13:20:10 -0400960 /* ca0132_alt control related values */
961 unsigned char in_enum_val;
962 unsigned char out_enum_val;
Connor McAdams47cdf762018-05-08 13:20:13 -0400963 unsigned char mic_boost_enum_val;
964 unsigned char smart_volume_setting;
965 long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
966 long xbass_xover_freq;
967 long eq_preset_val;
968 unsigned int tlv[4];
969 struct hda_vmaster_mute_hook vmaster_mute;
970
Ian Minett44f0c972012-12-20 18:53:38 -0800971
Chih-Chung Chang993884f2013-03-25 10:39:23 -0700972 struct hda_codec *codec;
973 struct delayed_work unsol_hp_work;
Gabriele Martinod5c016b2015-05-18 21:15:13 +0200974 int quirk;
Chih-Chung Chang993884f2013-03-25 10:39:23 -0700975
Ian Minett44f0c972012-12-20 18:53:38 -0800976#ifdef ENABLE_TUNING_CONTROLS
977 long cur_ctl_vals[TUNING_CTLS_COUNT];
978#endif
Connor McAdamsaa317042018-05-08 13:20:03 -0400979 /*
Connor McAdams08eca6b2018-08-08 13:34:17 -0400980 * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
981 * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
982 * things.
Connor McAdamsaa317042018-05-08 13:20:03 -0400983 */
Connor McAdams08eca6b2018-08-08 13:34:17 -0400984 bool use_pci_mmio;
Connor McAdamsaa317042018-05-08 13:20:03 -0400985 void __iomem *mem_base;
Connor McAdams009b8f92018-05-08 13:20:06 -0400986
987 /*
988 * Whether or not to use the alt functions like alt_select_out,
989 * alt_select_in, etc. Only used on desktop codecs for now, because of
990 * surround sound support.
991 */
992 bool use_alt_functions;
Connor McAdams47cdf762018-05-08 13:20:13 -0400993
994 /*
995 * Whether or not to use alt controls: volume effect sliders, EQ
996 * presets, smart volume presets, and new control names with FX prefix.
997 * Renames PlayEnhancement and CrystalVoice too.
998 */
999 bool use_alt_controls;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001000};
1001
Ian Minett01ef7db2012-09-20 20:29:16 -07001002/*
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001003 * CA0132 quirks table
1004 */
1005enum {
1006 QUIRK_NONE,
1007 QUIRK_ALIENWARE,
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04001008 QUIRK_ALIENWARE_M17XR4,
Connor McAdams8a19bce2018-05-08 13:20:01 -04001009 QUIRK_SBZ,
1010 QUIRK_R3DI,
Connor McAdams8f8c5232018-08-08 13:34:15 -04001011 QUIRK_R3D,
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001012};
1013
Takashi Iwaife14f392015-08-10 16:53:32 +02001014static const struct hda_pintbl alienware_pincfgs[] = {
1015 { 0x0b, 0x90170110 }, /* Builtin Speaker */
1016 { 0x0c, 0x411111f0 }, /* N/A */
1017 { 0x0d, 0x411111f0 }, /* N/A */
1018 { 0x0e, 0x411111f0 }, /* N/A */
1019 { 0x0f, 0x0321101f }, /* HP */
1020 { 0x10, 0x411111f0 }, /* Headset? disabled for now */
1021 { 0x11, 0x03a11021 }, /* Mic */
1022 { 0x12, 0xd5a30140 }, /* Builtin Mic */
1023 { 0x13, 0x411111f0 }, /* N/A */
1024 { 0x18, 0x411111f0 }, /* N/A */
1025 {}
1026};
1027
Connor McAdams63177af2018-05-08 13:20:02 -04001028/* Sound Blaster Z pin configs taken from Windows Driver */
1029static const struct hda_pintbl sbz_pincfgs[] = {
1030 { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1031 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1032 { 0x0d, 0x014510f0 }, /* Digital Out */
1033 { 0x0e, 0x01c510f0 }, /* SPDIF In */
1034 { 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
1035 { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1036 { 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
1037 { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1038 { 0x13, 0x908700f0 }, /* What U Hear In*/
1039 { 0x18, 0x50d000f0 }, /* N/A */
1040 {}
1041};
1042
Connor McAdams7f73df92018-08-08 13:34:16 -04001043/* Recon3D pin configs taken from Windows Driver */
1044static const struct hda_pintbl r3d_pincfgs[] = {
1045 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1046 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1047 { 0x0d, 0x014510f0 }, /* Digital Out */
1048 { 0x0e, 0x01c520f0 }, /* SPDIF In */
1049 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1050 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1051 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1052 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1053 { 0x13, 0x908700f0 }, /* What U Hear In*/
1054 { 0x18, 0x50d000f0 }, /* N/A */
1055 {}
1056};
1057
Connor McAdams63177af2018-05-08 13:20:02 -04001058/* Recon3D integrated pin configs taken from Windows Driver */
1059static const struct hda_pintbl r3di_pincfgs[] = {
1060 { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1061 { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1062 { 0x0d, 0x014510f0 }, /* Digital Out */
1063 { 0x0e, 0x41c520f0 }, /* SPDIF In */
1064 { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1065 { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1066 { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1067 { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1068 { 0x13, 0x908700f0 }, /* What U Hear In*/
1069 { 0x18, 0x500000f0 }, /* N/A */
1070 {}
1071};
1072
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001073static const struct snd_pci_quirk ca0132_quirks[] = {
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04001074 SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
Gabriele Martino5328e1e2015-12-09 17:05:58 +01001075 SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
1076 SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
Sven Hahneb5337cf2016-11-25 14:16:43 +01001077 SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
Connor McAdams8a19bce2018-05-08 13:20:01 -04001078 SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
1079 SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
1080 SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
Alastair Bridgewaterdad59262018-07-11 18:09:45 -04001081 SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
Alastair Bridgewaterc5a59d22018-07-11 18:09:46 -04001082 SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
Connor McAdams8f8c5232018-08-08 13:34:15 -04001083 SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
Gabriele Martinod5c016b2015-05-18 21:15:13 +02001084 {}
1085};
1086
1087/*
Ian Minett01ef7db2012-09-20 20:29:16 -07001088 * CA0132 codec access
1089 */
Sachin Kamat399ae722013-09-13 15:14:22 +05301090static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
Ian Minett01ef7db2012-09-20 20:29:16 -07001091 unsigned int verb, unsigned int parm, unsigned int *res)
1092{
1093 unsigned int response;
1094 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
1095 *res = response;
1096
1097 return ((response == -1) ? -1 : 0);
1098}
1099
1100static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
1101 unsigned short converter_format, unsigned int *res)
1102{
1103 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
1104 converter_format & 0xffff, res);
1105}
1106
1107static int codec_set_converter_stream_channel(struct hda_codec *codec,
1108 hda_nid_t nid, unsigned char stream,
1109 unsigned char channel, unsigned int *res)
1110{
1111 unsigned char converter_stream_channel = 0;
1112
1113 converter_stream_channel = (stream << 4) | (channel & 0x0f);
1114 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
1115 converter_stream_channel, res);
1116}
1117
Ian Minett95c6e9c2011-06-15 15:35:17 -07001118/* Chip access helper function */
1119static int chipio_send(struct hda_codec *codec,
1120 unsigned int reg,
1121 unsigned int data)
1122{
1123 unsigned int res;
Ian Minett6d675302013-02-08 18:31:43 -08001124 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett95c6e9c2011-06-15 15:35:17 -07001125
1126 /* send bits of data specified by reg */
1127 do {
1128 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1129 reg, data);
1130 if (res == VENDOR_STATUS_CHIPIO_OK)
1131 return 0;
Ian Minett6d675302013-02-08 18:31:43 -08001132 msleep(20);
1133 } while (time_before(jiffies, timeout));
1134
Ian Minett95c6e9c2011-06-15 15:35:17 -07001135 return -EIO;
1136}
1137
1138/*
1139 * Write chip address through the vendor widget -- NOT protected by the Mutex!
1140 */
1141static int chipio_write_address(struct hda_codec *codec,
1142 unsigned int chip_addx)
1143{
Ian Minett4861af82012-09-20 20:29:20 -07001144 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001145 int res;
1146
Ian Minett4861af82012-09-20 20:29:20 -07001147 if (spec->curr_chip_addx == chip_addx)
1148 return 0;
1149
Ian Minett95c6e9c2011-06-15 15:35:17 -07001150 /* send low 16 bits of the address */
1151 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
1152 chip_addx & 0xffff);
1153
1154 if (res != -EIO) {
1155 /* send high 16 bits of the address */
1156 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
1157 chip_addx >> 16);
1158 }
1159
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001160 spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
Ian Minett4861af82012-09-20 20:29:20 -07001161
Ian Minett95c6e9c2011-06-15 15:35:17 -07001162 return res;
1163}
1164
1165/*
1166 * Write data through the vendor widget -- NOT protected by the Mutex!
1167 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07001168static int chipio_write_data(struct hda_codec *codec, unsigned int data)
1169{
Ian Minett5aaca442012-12-20 18:53:34 -08001170 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001171 int res;
1172
1173 /* send low 16 bits of the data */
1174 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
1175
1176 if (res != -EIO) {
1177 /* send high 16 bits of the data */
1178 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
1179 data >> 16);
1180 }
1181
Ian Minett5aaca442012-12-20 18:53:34 -08001182 /*If no error encountered, automatically increment the address
1183 as per chip behaviour*/
1184 spec->curr_chip_addx = (res != -EIO) ?
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001185 (spec->curr_chip_addx + 4) : ~0U;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001186 return res;
1187}
1188
Ian Minettd5c21b82012-09-20 20:29:18 -07001189/*
1190 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
1191 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001192static int chipio_write_data_multiple(struct hda_codec *codec,
1193 const u32 *data,
1194 unsigned int count)
1195{
1196 int status = 0;
1197
1198 if (data == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001199 codec_dbg(codec, "chipio_write_data null ptr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001200 return -EINVAL;
1201 }
1202
1203 while ((count-- != 0) && (status == 0))
1204 status = chipio_write_data(codec, *data++);
1205
1206 return status;
1207}
1208
1209
Ian Minett95c6e9c2011-06-15 15:35:17 -07001210/*
1211 * Read data through the vendor widget -- NOT protected by the Mutex!
1212 */
1213static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
1214{
Ian Minett5aaca442012-12-20 18:53:34 -08001215 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001216 int res;
1217
1218 /* post read */
1219 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
1220
1221 if (res != -EIO) {
1222 /* read status */
1223 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1224 }
1225
1226 if (res != -EIO) {
1227 /* read data */
1228 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1229 VENDOR_CHIPIO_HIC_READ_DATA,
1230 0);
1231 }
1232
Ian Minett5aaca442012-12-20 18:53:34 -08001233 /*If no error encountered, automatically increment the address
1234 as per chip behaviour*/
1235 spec->curr_chip_addx = (res != -EIO) ?
Matthias Kaehlcked1600402017-03-31 18:00:04 -07001236 (spec->curr_chip_addx + 4) : ~0U;
Ian Minett95c6e9c2011-06-15 15:35:17 -07001237 return res;
1238}
1239
1240/*
1241 * Write given value to the given address through the chip I/O widget.
1242 * protected by the Mutex
1243 */
1244static int chipio_write(struct hda_codec *codec,
1245 unsigned int chip_addx, const unsigned int data)
1246{
1247 struct ca0132_spec *spec = codec->spec;
1248 int err;
1249
1250 mutex_lock(&spec->chipio_mutex);
1251
1252 /* write the address, and if successful proceed to write data */
1253 err = chipio_write_address(codec, chip_addx);
1254 if (err < 0)
1255 goto exit;
1256
1257 err = chipio_write_data(codec, data);
1258 if (err < 0)
1259 goto exit;
1260
1261exit:
1262 mutex_unlock(&spec->chipio_mutex);
1263 return err;
1264}
1265
Ian Minettd5c21b82012-09-20 20:29:18 -07001266/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04001267 * Write given value to the given address through the chip I/O widget.
1268 * not protected by the Mutex
1269 */
1270static int chipio_write_no_mutex(struct hda_codec *codec,
1271 unsigned int chip_addx, const unsigned int data)
1272{
1273 int err;
1274
1275
1276 /* write the address, and if successful proceed to write data */
1277 err = chipio_write_address(codec, chip_addx);
1278 if (err < 0)
1279 goto exit;
1280
1281 err = chipio_write_data(codec, data);
1282 if (err < 0)
1283 goto exit;
1284
1285exit:
1286 return err;
1287}
1288
1289/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001290 * Write multiple values to the given address through the chip I/O widget.
1291 * protected by the Mutex
1292 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001293static int chipio_write_multiple(struct hda_codec *codec,
1294 u32 chip_addx,
1295 const u32 *data,
1296 unsigned int count)
1297{
1298 struct ca0132_spec *spec = codec->spec;
1299 int status;
1300
1301 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -07001302 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -07001303 if (status < 0)
1304 goto error;
1305
1306 status = chipio_write_data_multiple(codec, data, count);
1307error:
1308 mutex_unlock(&spec->chipio_mutex);
1309
1310 return status;
1311}
1312
Ian Minett95c6e9c2011-06-15 15:35:17 -07001313/*
1314 * Read the given address through the chip I/O widget
1315 * protected by the Mutex
1316 */
1317static int chipio_read(struct hda_codec *codec,
1318 unsigned int chip_addx, unsigned int *data)
1319{
1320 struct ca0132_spec *spec = codec->spec;
1321 int err;
1322
1323 mutex_lock(&spec->chipio_mutex);
1324
1325 /* write the address, and if successful proceed to write data */
1326 err = chipio_write_address(codec, chip_addx);
1327 if (err < 0)
1328 goto exit;
1329
1330 err = chipio_read_data(codec, data);
1331 if (err < 0)
1332 goto exit;
1333
1334exit:
1335 mutex_unlock(&spec->chipio_mutex);
1336 return err;
1337}
1338
Ian Minettd5c21b82012-09-20 20:29:18 -07001339/*
1340 * Set chip control flags through the chip I/O widget.
1341 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001342static void chipio_set_control_flag(struct hda_codec *codec,
1343 enum control_flag_id flag_id,
1344 bool flag_state)
1345{
1346 unsigned int val;
1347 unsigned int flag_bit;
1348
1349 flag_bit = (flag_state ? 1 : 0);
1350 val = (flag_bit << 7) | (flag_id);
1351 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1352 VENDOR_CHIPIO_FLAG_SET, val);
1353}
1354
Ian Minettd5c21b82012-09-20 20:29:18 -07001355/*
1356 * Set chip parameters through the chip I/O widget.
1357 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001358static void chipio_set_control_param(struct hda_codec *codec,
1359 enum control_param_id param_id, int param_val)
1360{
1361 struct ca0132_spec *spec = codec->spec;
1362 int val;
1363
1364 if ((param_id < 32) && (param_val < 8)) {
1365 val = (param_val << 5) | (param_id);
1366 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1367 VENDOR_CHIPIO_PARAM_SET, val);
1368 } else {
1369 mutex_lock(&spec->chipio_mutex);
1370 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1371 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1372 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1373 param_id);
1374 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1375 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1376 param_val);
1377 }
1378 mutex_unlock(&spec->chipio_mutex);
1379 }
1380}
1381
Ian Minettd5c21b82012-09-20 20:29:18 -07001382/*
Connor McAdams009b8f92018-05-08 13:20:06 -04001383 * Set chip parameters through the chip I/O widget. NO MUTEX.
1384 */
1385static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
1386 enum control_param_id param_id, int param_val)
1387{
1388 int val;
1389
1390 if ((param_id < 32) && (param_val < 8)) {
1391 val = (param_val << 5) | (param_id);
1392 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1393 VENDOR_CHIPIO_PARAM_SET, val);
1394 } else {
1395 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1396 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1397 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1398 param_id);
1399 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1400 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1401 param_val);
1402 }
1403 }
1404}
Connor McAdams38ba69f2018-05-08 13:20:07 -04001405/*
1406 * Connect stream to a source point, and then connect
1407 * that source point to a destination point.
1408 */
1409static void chipio_set_stream_source_dest(struct hda_codec *codec,
1410 int streamid, int source_point, int dest_point)
1411{
1412 chipio_set_control_param_no_mutex(codec,
1413 CONTROL_PARAM_STREAM_ID, streamid);
1414 chipio_set_control_param_no_mutex(codec,
1415 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
1416 chipio_set_control_param_no_mutex(codec,
1417 CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
1418}
1419
1420/*
1421 * Set number of channels in the selected stream.
1422 */
1423static void chipio_set_stream_channels(struct hda_codec *codec,
1424 int streamid, unsigned int channels)
1425{
1426 chipio_set_control_param_no_mutex(codec,
1427 CONTROL_PARAM_STREAM_ID, streamid);
1428 chipio_set_control_param_no_mutex(codec,
1429 CONTROL_PARAM_STREAMS_CHANNELS, channels);
1430}
Connor McAdams009b8f92018-05-08 13:20:06 -04001431
1432/*
1433 * Enable/Disable audio stream.
1434 */
1435static void chipio_set_stream_control(struct hda_codec *codec,
1436 int streamid, int enable)
1437{
1438 chipio_set_control_param_no_mutex(codec,
1439 CONTROL_PARAM_STREAM_ID, streamid);
1440 chipio_set_control_param_no_mutex(codec,
1441 CONTROL_PARAM_STREAM_CONTROL, enable);
1442}
1443
Connor McAdams38ba69f2018-05-08 13:20:07 -04001444
1445/*
1446 * Set sampling rate of the connection point. NO MUTEX.
1447 */
1448static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
1449 int connid, enum ca0132_sample_rate rate)
1450{
1451 chipio_set_control_param_no_mutex(codec,
1452 CONTROL_PARAM_CONN_POINT_ID, connid);
1453 chipio_set_control_param_no_mutex(codec,
1454 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
1455}
1456
Connor McAdams009b8f92018-05-08 13:20:06 -04001457/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001458 * Set sampling rate of the connection point.
1459 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001460static void chipio_set_conn_rate(struct hda_codec *codec,
1461 int connid, enum ca0132_sample_rate rate)
1462{
1463 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1464 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1465 rate);
1466}
1467
Ian Minettd5c21b82012-09-20 20:29:18 -07001468/*
1469 * Enable clocks.
1470 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001471static void chipio_enable_clocks(struct hda_codec *codec)
1472{
1473 struct ca0132_spec *spec = codec->spec;
1474
1475 mutex_lock(&spec->chipio_mutex);
1476 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1477 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1478 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1479 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1480 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1481 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1482 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1483 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1484 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1485 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1486 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1487 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1488 mutex_unlock(&spec->chipio_mutex);
1489}
1490
1491/*
1492 * CA0132 DSP IO stuffs
1493 */
1494static int dspio_send(struct hda_codec *codec, unsigned int reg,
1495 unsigned int data)
1496{
Takashi Iwaib645d792013-01-15 17:39:29 +01001497 int res;
Ian Minett6d675302013-02-08 18:31:43 -08001498 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001499
1500 /* send bits of data specified by reg to dsp */
1501 do {
1502 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1503 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1504 return res;
Ian Minett6d675302013-02-08 18:31:43 -08001505 msleep(20);
1506 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001507
1508 return -EIO;
1509}
1510
Ian Minettd5c21b82012-09-20 20:29:18 -07001511/*
1512 * Wait for DSP to be ready for commands
1513 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001514static void dspio_write_wait(struct hda_codec *codec)
1515{
Ian Minett4861af82012-09-20 20:29:20 -07001516 int status;
1517 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001518
Ian Minett01ef7db2012-09-20 20:29:16 -07001519 do {
Ian Minett4861af82012-09-20 20:29:20 -07001520 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1521 VENDOR_DSPIO_STATUS, 0);
1522 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1523 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1524 break;
1525 msleep(1);
1526 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001527}
1528
Ian Minettd5c21b82012-09-20 20:29:18 -07001529/*
1530 * Write SCP data to DSP
1531 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001532static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1533{
1534 struct ca0132_spec *spec = codec->spec;
1535 int status;
1536
1537 dspio_write_wait(codec);
1538
1539 mutex_lock(&spec->chipio_mutex);
1540 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1541 scp_data & 0xffff);
1542 if (status < 0)
1543 goto error;
1544
1545 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1546 scp_data >> 16);
1547 if (status < 0)
1548 goto error;
1549
1550 /* OK, now check if the write itself has executed*/
1551 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1552 VENDOR_DSPIO_STATUS, 0);
1553error:
1554 mutex_unlock(&spec->chipio_mutex);
1555
1556 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1557 -EIO : 0;
1558}
1559
Ian Minettd5c21b82012-09-20 20:29:18 -07001560/*
1561 * Write multiple SCP data to DSP
1562 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001563static int dspio_write_multiple(struct hda_codec *codec,
1564 unsigned int *buffer, unsigned int size)
1565{
1566 int status = 0;
1567 unsigned int count;
1568
Matthias Kaehlckea16fbb82017-03-15 15:41:23 -07001569 if (buffer == NULL)
Ian Minett01ef7db2012-09-20 20:29:16 -07001570 return -EINVAL;
1571
1572 count = 0;
1573 while (count < size) {
1574 status = dspio_write(codec, *buffer++);
1575 if (status != 0)
1576 break;
1577 count++;
1578 }
1579
1580 return status;
1581}
1582
Ian Minetta73d5112012-12-20 18:53:37 -08001583static int dspio_read(struct hda_codec *codec, unsigned int *data)
1584{
1585 int status;
1586
1587 status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
1588 if (status == -EIO)
1589 return status;
1590
1591 status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
1592 if (status == -EIO ||
1593 status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
1594 return -EIO;
1595
1596 *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1597 VENDOR_DSPIO_SCP_READ_DATA, 0);
1598
1599 return 0;
1600}
1601
1602static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
1603 unsigned int *buf_size, unsigned int size_count)
1604{
1605 int status = 0;
1606 unsigned int size = *buf_size;
1607 unsigned int count;
1608 unsigned int skip_count;
1609 unsigned int dummy;
1610
Matthias Kaehlckea16fbb82017-03-15 15:41:23 -07001611 if (buffer == NULL)
Ian Minetta73d5112012-12-20 18:53:37 -08001612 return -1;
1613
1614 count = 0;
1615 while (count < size && count < size_count) {
1616 status = dspio_read(codec, buffer++);
1617 if (status != 0)
1618 break;
1619 count++;
1620 }
1621
1622 skip_count = count;
1623 if (status == 0) {
1624 while (skip_count < size) {
1625 status = dspio_read(codec, &dummy);
1626 if (status != 0)
1627 break;
1628 skip_count++;
1629 }
1630 }
1631 *buf_size = count;
1632
1633 return status;
1634}
1635
Ian Minettd5c21b82012-09-20 20:29:18 -07001636/*
1637 * Construct the SCP header using corresponding fields
1638 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001639static inline unsigned int
1640make_scp_header(unsigned int target_id, unsigned int source_id,
1641 unsigned int get_flag, unsigned int req,
1642 unsigned int device_flag, unsigned int resp_flag,
1643 unsigned int error_flag, unsigned int data_size)
1644{
1645 unsigned int header = 0;
1646
1647 header = (data_size & 0x1f) << 27;
1648 header |= (error_flag & 0x01) << 26;
1649 header |= (resp_flag & 0x01) << 25;
1650 header |= (device_flag & 0x01) << 24;
1651 header |= (req & 0x7f) << 17;
1652 header |= (get_flag & 0x01) << 16;
1653 header |= (source_id & 0xff) << 8;
1654 header |= target_id & 0xff;
1655
1656 return header;
1657}
1658
Ian Minettd5c21b82012-09-20 20:29:18 -07001659/*
1660 * Extract corresponding fields from SCP header
1661 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001662static inline void
1663extract_scp_header(unsigned int header,
1664 unsigned int *target_id, unsigned int *source_id,
1665 unsigned int *get_flag, unsigned int *req,
1666 unsigned int *device_flag, unsigned int *resp_flag,
1667 unsigned int *error_flag, unsigned int *data_size)
1668{
1669 if (data_size)
1670 *data_size = (header >> 27) & 0x1f;
1671 if (error_flag)
1672 *error_flag = (header >> 26) & 0x01;
1673 if (resp_flag)
1674 *resp_flag = (header >> 25) & 0x01;
1675 if (device_flag)
1676 *device_flag = (header >> 24) & 0x01;
1677 if (req)
1678 *req = (header >> 17) & 0x7f;
1679 if (get_flag)
1680 *get_flag = (header >> 16) & 0x01;
1681 if (source_id)
1682 *source_id = (header >> 8) & 0xff;
1683 if (target_id)
1684 *target_id = header & 0xff;
1685}
1686
1687#define SCP_MAX_DATA_WORDS (16)
1688
1689/* Structure to contain any SCP message */
1690struct scp_msg {
1691 unsigned int hdr;
1692 unsigned int data[SCP_MAX_DATA_WORDS];
1693};
1694
Ian Minetta73d5112012-12-20 18:53:37 -08001695static void dspio_clear_response_queue(struct hda_codec *codec)
1696{
1697 unsigned int dummy = 0;
1698 int status = -1;
1699
1700 /* clear all from the response queue */
1701 do {
1702 status = dspio_read(codec, &dummy);
1703 } while (status == 0);
1704}
1705
1706static int dspio_get_response_data(struct hda_codec *codec)
1707{
1708 struct ca0132_spec *spec = codec->spec;
1709 unsigned int data = 0;
1710 unsigned int count;
1711
1712 if (dspio_read(codec, &data) < 0)
1713 return -EIO;
1714
1715 if ((data & 0x00ffffff) == spec->wait_scp_header) {
1716 spec->scp_resp_header = data;
1717 spec->scp_resp_count = data >> 27;
1718 count = spec->wait_num_data;
1719 dspio_read_multiple(codec, spec->scp_resp_data,
1720 &spec->scp_resp_count, count);
1721 return 0;
1722 }
1723
1724 return -EIO;
1725}
1726
Ian Minettd5c21b82012-09-20 20:29:18 -07001727/*
1728 * Send SCP message to DSP
1729 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001730static int dspio_send_scp_message(struct hda_codec *codec,
1731 unsigned char *send_buf,
1732 unsigned int send_buf_size,
1733 unsigned char *return_buf,
1734 unsigned int return_buf_size,
1735 unsigned int *bytes_returned)
1736{
1737 struct ca0132_spec *spec = codec->spec;
Ian Minett01ef7db2012-09-20 20:29:16 -07001738 int status = -1;
1739 unsigned int scp_send_size = 0;
1740 unsigned int total_size;
1741 bool waiting_for_resp = false;
1742 unsigned int header;
1743 struct scp_msg *ret_msg;
1744 unsigned int resp_src_id, resp_target_id;
1745 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1746
1747 if (bytes_returned)
1748 *bytes_returned = 0;
1749
1750 /* get scp header from buffer */
1751 header = *((unsigned int *)send_buf);
1752 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1753 &device_flag, NULL, NULL, &data_size);
1754 scp_send_size = data_size + 1;
1755 total_size = (scp_send_size * 4);
1756
1757 if (send_buf_size < total_size)
1758 return -EINVAL;
1759
1760 if (get_flag || device_flag) {
1761 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1762 return -EINVAL;
1763
1764 spec->wait_scp_header = *((unsigned int *)send_buf);
1765
1766 /* swap source id with target id */
1767 resp_target_id = src_id;
1768 resp_src_id = target_id;
1769 spec->wait_scp_header &= 0xffff0000;
1770 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1771 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1772 spec->wait_scp = 1;
1773 waiting_for_resp = true;
1774 }
1775
1776 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1777 scp_send_size);
1778 if (status < 0) {
1779 spec->wait_scp = 0;
1780 return status;
1781 }
1782
1783 if (waiting_for_resp) {
Ian Minett6d675302013-02-08 18:31:43 -08001784 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001785 memset(return_buf, 0, return_buf_size);
Ian Minett01ef7db2012-09-20 20:29:16 -07001786 do {
1787 msleep(20);
Ian Minett6d675302013-02-08 18:31:43 -08001788 } while (spec->wait_scp && time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001789 waiting_for_resp = false;
Ian Minett6d675302013-02-08 18:31:43 -08001790 if (!spec->wait_scp) {
Ian Minett01ef7db2012-09-20 20:29:16 -07001791 ret_msg = (struct scp_msg *)return_buf;
1792 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1793 memcpy(&ret_msg->data, spec->scp_resp_data,
1794 spec->wait_num_data);
1795 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1796 status = 0;
1797 } else {
1798 status = -EIO;
1799 }
1800 spec->wait_scp = 0;
1801 }
1802
1803 return status;
1804}
1805
Ian Minettd5c21b82012-09-20 20:29:18 -07001806/**
1807 * Prepare and send the SCP message to DSP
1808 * @codec: the HDA codec
1809 * @mod_id: ID of the DSP module to send the command
1810 * @req: ID of request to send to the DSP module
1811 * @dir: SET or GET
1812 * @data: pointer to the data to send with the request, request specific
1813 * @len: length of the data, in bytes
1814 * @reply: point to the buffer to hold data returned for a reply
1815 * @reply_len: length of the reply buffer returned from GET
1816 *
1817 * Returns zero or a negative error code.
1818 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001819static int dspio_scp(struct hda_codec *codec,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001820 int mod_id, int src_id, int req, int dir, const void *data,
1821 unsigned int len, void *reply, unsigned int *reply_len)
Ian Minett01ef7db2012-09-20 20:29:16 -07001822{
1823 int status = 0;
1824 struct scp_msg scp_send, scp_reply;
1825 unsigned int ret_bytes, send_size, ret_size;
1826 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1827 unsigned int reply_data_size;
1828
1829 memset(&scp_send, 0, sizeof(scp_send));
1830 memset(&scp_reply, 0, sizeof(scp_reply));
1831
1832 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1833 return -EINVAL;
1834
1835 if (dir == SCP_GET && reply == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001836 codec_dbg(codec, "dspio_scp get but has no buffer\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001837 return -EINVAL;
1838 }
1839
1840 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001841 codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001842 return -EINVAL;
1843 }
1844
Connor McAdams447fd8e2018-05-08 13:20:09 -04001845 scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
Ian Minett01ef7db2012-09-20 20:29:16 -07001846 0, 0, 0, len/sizeof(unsigned int));
1847 if (data != NULL && len > 0) {
1848 len = min((unsigned int)(sizeof(scp_send.data)), len);
1849 memcpy(scp_send.data, data, len);
1850 }
1851
1852 ret_bytes = 0;
1853 send_size = sizeof(unsigned int) + len;
1854 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1855 send_size, (unsigned char *)&scp_reply,
1856 sizeof(scp_reply), &ret_bytes);
1857
1858 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001859 codec_dbg(codec, "dspio_scp: send scp msg failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001860 return status;
1861 }
1862
1863 /* extract send and reply headers members */
1864 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1865 NULL, NULL, NULL, NULL, NULL);
1866 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1867 &reply_resp_flag, &reply_error_flag,
1868 &reply_data_size);
1869
1870 if (!send_get_flag)
1871 return 0;
1872
1873 if (reply_resp_flag && !reply_error_flag) {
1874 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1875 / sizeof(unsigned int);
1876
1877 if (*reply_len < ret_size*sizeof(unsigned int)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001878 codec_dbg(codec, "reply too long for buf\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001879 return -EINVAL;
1880 } else if (ret_size != reply_data_size) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001881 codec_dbg(codec, "RetLen and HdrLen .NE.\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001882 return -EINVAL;
Arnd Bergmann46a049d2017-01-11 14:39:44 +01001883 } else if (!reply) {
1884 codec_dbg(codec, "NULL reply\n");
1885 return -EINVAL;
Ian Minett01ef7db2012-09-20 20:29:16 -07001886 } else {
1887 *reply_len = ret_size*sizeof(unsigned int);
1888 memcpy(reply, scp_reply.data, *reply_len);
1889 }
1890 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001891 codec_dbg(codec, "reply ill-formed or errflag set\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001892 return -EIO;
1893 }
1894
1895 return status;
1896}
1897
Ian Minettd5c21b82012-09-20 20:29:18 -07001898/*
Ian Minett5aaca442012-12-20 18:53:34 -08001899 * Set DSP parameters
1900 */
1901static int dspio_set_param(struct hda_codec *codec, int mod_id,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001902 int src_id, int req, const void *data, unsigned int len)
Ian Minett5aaca442012-12-20 18:53:34 -08001903{
Connor McAdams447fd8e2018-05-08 13:20:09 -04001904 return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
1905 NULL);
Ian Minett5aaca442012-12-20 18:53:34 -08001906}
1907
1908static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
Connor McAdams447fd8e2018-05-08 13:20:09 -04001909 int req, const unsigned int data)
Ian Minett5aaca442012-12-20 18:53:34 -08001910{
Connor McAdams447fd8e2018-05-08 13:20:09 -04001911 return dspio_set_param(codec, mod_id, 0x20, req, &data,
1912 sizeof(unsigned int));
1913}
1914
1915static int dspio_set_uint_param_no_source(struct hda_codec *codec, int mod_id,
1916 int req, const unsigned int data)
1917{
1918 return dspio_set_param(codec, mod_id, 0x00, req, &data,
1919 sizeof(unsigned int));
Ian Minett5aaca442012-12-20 18:53:34 -08001920}
1921
1922/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001923 * Allocate a DSP DMA channel via an SCP message
1924 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001925static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1926{
1927 int status = 0;
1928 unsigned int size = sizeof(dma_chan);
1929
Takashi Iwai4e76a882014-02-25 12:21:03 +01001930 codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
Connor McAdams447fd8e2018-05-08 13:20:09 -04001931 status = dspio_scp(codec, MASTERCONTROL, 0x20,
1932 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
1933 dma_chan, &size);
Ian Minett01ef7db2012-09-20 20:29:16 -07001934
1935 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001936 codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001937 return status;
1938 }
1939
1940 if ((*dma_chan + 1) == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001941 codec_dbg(codec, "no free dma channels to allocate\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001942 return -EBUSY;
1943 }
1944
Takashi Iwai4e76a882014-02-25 12:21:03 +01001945 codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1946 codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001947
1948 return status;
1949}
1950
Ian Minettd5c21b82012-09-20 20:29:18 -07001951/*
1952 * Free a DSP DMA via an SCP message
1953 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001954static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1955{
1956 int status = 0;
1957 unsigned int dummy = 0;
1958
Takashi Iwai4e76a882014-02-25 12:21:03 +01001959 codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
1960 codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
Ian Minett01ef7db2012-09-20 20:29:16 -07001961
Connor McAdams447fd8e2018-05-08 13:20:09 -04001962 status = dspio_scp(codec, MASTERCONTROL, 0x20,
1963 MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
1964 sizeof(dma_chan), NULL, &dummy);
Ian Minett01ef7db2012-09-20 20:29:16 -07001965
1966 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001967 codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001968 return status;
1969 }
1970
Takashi Iwai4e76a882014-02-25 12:21:03 +01001971 codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07001972
1973 return status;
1974}
1975
1976/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001977 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001978 */
1979static int dsp_set_run_state(struct hda_codec *codec)
1980{
1981 unsigned int dbg_ctrl_reg;
1982 unsigned int halt_state;
1983 int err;
1984
1985 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1986 if (err < 0)
1987 return err;
1988
1989 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1990 DSP_DBGCNTL_STATE_LOBIT;
1991
1992 if (halt_state != 0) {
1993 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1994 DSP_DBGCNTL_SS_MASK);
1995 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1996 dbg_ctrl_reg);
1997 if (err < 0)
1998 return err;
1999
2000 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
2001 DSP_DBGCNTL_EXEC_MASK;
2002 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2003 dbg_ctrl_reg);
2004 if (err < 0)
2005 return err;
2006 }
2007
2008 return 0;
2009}
2010
Ian Minettd5c21b82012-09-20 20:29:18 -07002011/*
2012 * Reset the DSP
2013 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002014static int dsp_reset(struct hda_codec *codec)
2015{
2016 unsigned int res;
2017 int retry = 20;
2018
Takashi Iwai4e76a882014-02-25 12:21:03 +01002019 codec_dbg(codec, "dsp_reset\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002020 do {
2021 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
2022 retry--;
2023 } while (res == -EIO && retry);
2024
2025 if (!retry) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002026 codec_dbg(codec, "dsp_reset timeout\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002027 return -EIO;
2028 }
2029
2030 return 0;
2031}
2032
Ian Minettd5c21b82012-09-20 20:29:18 -07002033/*
2034 * Convert chip address to DSP address
2035 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002036static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
2037 bool *code, bool *yram)
2038{
2039 *code = *yram = false;
2040
2041 if (UC_RANGE(chip_addx, 1)) {
2042 *code = true;
2043 return UC_OFF(chip_addx);
2044 } else if (X_RANGE_ALL(chip_addx, 1)) {
2045 return X_OFF(chip_addx);
2046 } else if (Y_RANGE_ALL(chip_addx, 1)) {
2047 *yram = true;
2048 return Y_OFF(chip_addx);
2049 }
2050
Takashi Iwai4a8b89f2013-02-12 10:15:15 +01002051 return INVALID_CHIP_ADDRESS;
Ian Minett01ef7db2012-09-20 20:29:16 -07002052}
2053
Ian Minettd5c21b82012-09-20 20:29:18 -07002054/*
2055 * Check if the DSP DMA is active
2056 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002057static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
2058{
2059 unsigned int dma_chnlstart_reg;
2060
2061 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
2062
2063 return ((dma_chnlstart_reg & (1 <<
2064 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
2065}
2066
2067static int dsp_dma_setup_common(struct hda_codec *codec,
2068 unsigned int chip_addx,
2069 unsigned int dma_chan,
2070 unsigned int port_map_mask,
2071 bool ovly)
2072{
2073 int status = 0;
2074 unsigned int chnl_prop;
2075 unsigned int dsp_addx;
2076 unsigned int active;
2077 bool code, yram;
2078
Takashi Iwai4e76a882014-02-25 12:21:03 +01002079 codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002080
2081 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002082 codec_dbg(codec, "dma chan num invalid\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002083 return -EINVAL;
2084 }
2085
2086 if (dsp_is_dma_active(codec, dma_chan)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002087 codec_dbg(codec, "dma already active\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002088 return -EBUSY;
2089 }
2090
2091 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2092
2093 if (dsp_addx == INVALID_CHIP_ADDRESS) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002094 codec_dbg(codec, "invalid chip addr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002095 return -ENXIO;
2096 }
2097
2098 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
2099 active = 0;
2100
Takashi Iwai4e76a882014-02-25 12:21:03 +01002101 codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002102
2103 if (ovly) {
2104 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
2105 &chnl_prop);
2106
2107 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002108 codec_dbg(codec, "read CHNLPROP Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002109 return status;
2110 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002111 codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002112 }
2113
2114 if (!code)
2115 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2116 else
2117 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2118
2119 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
2120
2121 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
2122 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002123 codec_dbg(codec, "write CHNLPROP Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002124 return status;
2125 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002126 codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002127
2128 if (ovly) {
2129 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
2130 &active);
2131
2132 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002133 codec_dbg(codec, "read ACTIVE Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002134 return status;
2135 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002136 codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002137 }
2138
2139 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
2140 DSPDMAC_ACTIVE_AAR_MASK;
2141
2142 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
2143 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002144 codec_dbg(codec, "write ACTIVE Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002145 return status;
2146 }
2147
Takashi Iwai4e76a882014-02-25 12:21:03 +01002148 codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002149
2150 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
2151 port_map_mask);
2152 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002153 codec_dbg(codec, "write AUDCHSEL Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002154 return status;
2155 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002156 codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002157
2158 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
2159 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
2160 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002161 codec_dbg(codec, "write IRQCNT Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002162 return status;
2163 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002164 codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002165
Takashi Iwai4e76a882014-02-25 12:21:03 +01002166 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002167 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
2168 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
2169 chip_addx, dsp_addx, dma_chan,
2170 port_map_mask, chnl_prop, active);
2171
Takashi Iwai4e76a882014-02-25 12:21:03 +01002172 codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002173
2174 return 0;
2175}
2176
Ian Minettd5c21b82012-09-20 20:29:18 -07002177/*
2178 * Setup the DSP DMA per-transfer-specific registers
2179 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002180static int dsp_dma_setup(struct hda_codec *codec,
2181 unsigned int chip_addx,
2182 unsigned int count,
2183 unsigned int dma_chan)
2184{
2185 int status = 0;
2186 bool code, yram;
2187 unsigned int dsp_addx;
2188 unsigned int addr_field;
2189 unsigned int incr_field;
2190 unsigned int base_cnt;
2191 unsigned int cur_cnt;
2192 unsigned int dma_cfg = 0;
2193 unsigned int adr_ofs = 0;
2194 unsigned int xfr_cnt = 0;
2195 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
2196 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
2197
Takashi Iwai4e76a882014-02-25 12:21:03 +01002198 codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002199
2200 if (count > max_dma_count) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002201 codec_dbg(codec, "count too big\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002202 return -EINVAL;
2203 }
2204
2205 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2206 if (dsp_addx == INVALID_CHIP_ADDRESS) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002207 codec_dbg(codec, "invalid chip addr\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002208 return -ENXIO;
2209 }
2210
Takashi Iwai4e76a882014-02-25 12:21:03 +01002211 codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002212
2213 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
2214 incr_field = 0;
2215
2216 if (!code) {
2217 addr_field <<= 1;
2218 if (yram)
2219 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
2220
2221 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
2222 }
2223
2224 dma_cfg = addr_field + incr_field;
2225 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
2226 dma_cfg);
2227 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002228 codec_dbg(codec, "write DMACFG Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002229 return status;
2230 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002231 codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002232
2233 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
2234 (code ? 0 : 1));
2235
2236 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
2237 adr_ofs);
2238 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002239 codec_dbg(codec, "write DSPADROFS Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002240 return status;
2241 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002242 codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002243
2244 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
2245
2246 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
2247
2248 xfr_cnt = base_cnt | cur_cnt;
2249
2250 status = chipio_write(codec,
2251 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
2252 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002253 codec_dbg(codec, "write XFRCNT Reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002254 return status;
2255 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002256 codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002257
Takashi Iwai4e76a882014-02-25 12:21:03 +01002258 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002259 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
2260 "ADROFS=0x%x, XFRCNT=0x%x\n",
2261 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
2262
Takashi Iwai4e76a882014-02-25 12:21:03 +01002263 codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002264
2265 return 0;
2266}
2267
Ian Minettd5c21b82012-09-20 20:29:18 -07002268/*
2269 * Start the DSP DMA
2270 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002271static int dsp_dma_start(struct hda_codec *codec,
2272 unsigned int dma_chan, bool ovly)
2273{
2274 unsigned int reg = 0;
2275 int status = 0;
2276
Takashi Iwai4e76a882014-02-25 12:21:03 +01002277 codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002278
2279 if (ovly) {
2280 status = chipio_read(codec,
2281 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2282
2283 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002284 codec_dbg(codec, "read CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002285 return status;
2286 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002287 codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002288
2289 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2290 DSPDMAC_CHNLSTART_DIS_MASK);
2291 }
2292
2293 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2294 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
2295 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002296 codec_dbg(codec, "write CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002297 return status;
2298 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002299 codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002300
2301 return status;
2302}
2303
Ian Minettd5c21b82012-09-20 20:29:18 -07002304/*
2305 * Stop the DSP DMA
2306 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002307static int dsp_dma_stop(struct hda_codec *codec,
2308 unsigned int dma_chan, bool ovly)
2309{
2310 unsigned int reg = 0;
2311 int status = 0;
2312
Takashi Iwai4e76a882014-02-25 12:21:03 +01002313 codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002314
2315 if (ovly) {
2316 status = chipio_read(codec,
2317 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2318
2319 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002320 codec_dbg(codec, "read CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002321 return status;
2322 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002323 codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002324 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2325 DSPDMAC_CHNLSTART_DIS_MASK);
2326 }
2327
2328 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2329 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
2330 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002331 codec_dbg(codec, "write CHNLSTART reg fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002332 return status;
2333 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002334 codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002335
2336 return status;
2337}
2338
Ian Minettd5c21b82012-09-20 20:29:18 -07002339/**
2340 * Allocate router ports
2341 *
2342 * @codec: the HDA codec
2343 * @num_chans: number of channels in the stream
2344 * @ports_per_channel: number of ports per channel
2345 * @start_device: start device
2346 * @port_map: pointer to the port list to hold the allocated ports
2347 *
2348 * Returns zero or a negative error code.
2349 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002350static int dsp_allocate_router_ports(struct hda_codec *codec,
2351 unsigned int num_chans,
2352 unsigned int ports_per_channel,
2353 unsigned int start_device,
2354 unsigned int *port_map)
2355{
2356 int status = 0;
2357 int res;
2358 u8 val;
2359
2360 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2361 if (status < 0)
2362 return status;
2363
2364 val = start_device << 6;
2365 val |= (ports_per_channel - 1) << 4;
2366 val |= num_chans - 1;
2367
2368 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2369 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
2370 val);
2371
2372 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2373 VENDOR_CHIPIO_PORT_ALLOC_SET,
2374 MEM_CONNID_DSP);
2375
2376 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2377 if (status < 0)
2378 return status;
2379
2380 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
2381 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
2382
2383 *port_map = res;
2384
2385 return (res < 0) ? res : 0;
2386}
2387
Ian Minettd5c21b82012-09-20 20:29:18 -07002388/*
2389 * Free router ports
2390 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002391static int dsp_free_router_ports(struct hda_codec *codec)
2392{
2393 int status = 0;
2394
2395 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2396 if (status < 0)
2397 return status;
2398
2399 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2400 VENDOR_CHIPIO_PORT_FREE_SET,
2401 MEM_CONNID_DSP);
2402
2403 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2404
2405 return status;
2406}
2407
Ian Minettd5c21b82012-09-20 20:29:18 -07002408/*
2409 * Allocate DSP ports for the download stream
2410 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002411static int dsp_allocate_ports(struct hda_codec *codec,
2412 unsigned int num_chans,
2413 unsigned int rate_multi, unsigned int *port_map)
2414{
2415 int status;
2416
Takashi Iwai4e76a882014-02-25 12:21:03 +01002417 codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002418
2419 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002420 codec_dbg(codec, "bad rate multiple\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002421 return -EINVAL;
2422 }
2423
2424 status = dsp_allocate_router_ports(codec, num_chans,
2425 rate_multi, 0, port_map);
2426
Takashi Iwai4e76a882014-02-25 12:21:03 +01002427 codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002428
2429 return status;
2430}
2431
Ian Minett01ef7db2012-09-20 20:29:16 -07002432static int dsp_allocate_ports_format(struct hda_codec *codec,
2433 const unsigned short fmt,
2434 unsigned int *port_map)
2435{
2436 int status;
2437 unsigned int num_chans;
2438
2439 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2440 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2441 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2442
2443 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002444 codec_dbg(codec, "bad rate multiple\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002445 return -EINVAL;
2446 }
2447
2448 num_chans = get_hdafmt_chs(fmt) + 1;
2449
2450 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2451
2452 return status;
2453}
2454
2455/*
Ian Minettd5c21b82012-09-20 20:29:18 -07002456 * free DSP ports
2457 */
2458static int dsp_free_ports(struct hda_codec *codec)
2459{
2460 int status;
2461
Takashi Iwai4e76a882014-02-25 12:21:03 +01002462 codec_dbg(codec, " dsp_free_ports() -- begin\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002463
2464 status = dsp_free_router_ports(codec);
2465 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002466 codec_dbg(codec, "free router ports fail\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002467 return status;
2468 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01002469 codec_dbg(codec, " dsp_free_ports() -- complete\n");
Ian Minettd5c21b82012-09-20 20:29:18 -07002470
2471 return status;
2472}
2473
2474/*
Ian Minett01ef7db2012-09-20 20:29:16 -07002475 * HDA DMA engine stuffs for DSP code download
2476 */
2477struct dma_engine {
2478 struct hda_codec *codec;
2479 unsigned short m_converter_format;
2480 struct snd_dma_buffer *dmab;
2481 unsigned int buf_size;
2482};
2483
2484
2485enum dma_state {
2486 DMA_STATE_STOP = 0,
2487 DMA_STATE_RUN = 1
2488};
2489
Takashi Iwai6194b992014-06-06 18:12:16 +02002490static int dma_convert_to_hda_format(struct hda_codec *codec,
Ian Minette97249d2012-09-20 20:29:21 -07002491 unsigned int sample_rate,
2492 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002493 unsigned short *hda_format)
2494{
2495 unsigned int format_val;
2496
Takashi Iwaib7d023e2015-04-16 08:19:06 +02002497 format_val = snd_hdac_calc_stream_format(sample_rate,
2498 channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07002499
2500 if (hda_format)
2501 *hda_format = (unsigned short)format_val;
2502
2503 return 0;
2504}
2505
Ian Minettd5c21b82012-09-20 20:29:18 -07002506/*
2507 * Reset DMA for DSP download
2508 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002509static int dma_reset(struct dma_engine *dma)
2510{
2511 struct hda_codec *codec = dma->codec;
2512 struct ca0132_spec *spec = codec->spec;
2513 int status;
2514
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002515 if (dma->dmab->area)
Ian Minett01ef7db2012-09-20 20:29:16 -07002516 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2517
2518 status = snd_hda_codec_load_dsp_prepare(codec,
2519 dma->m_converter_format,
2520 dma->buf_size,
2521 dma->dmab);
2522 if (status < 0)
2523 return status;
2524 spec->dsp_stream_id = status;
2525 return 0;
2526}
2527
2528static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2529{
2530 bool cmd;
2531
Ian Minett01ef7db2012-09-20 20:29:16 -07002532 switch (state) {
2533 case DMA_STATE_STOP:
2534 cmd = false;
2535 break;
2536 case DMA_STATE_RUN:
2537 cmd = true;
2538 break;
2539 default:
2540 return 0;
2541 }
2542
2543 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2544 return 0;
2545}
2546
2547static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2548{
2549 return dma->dmab->bytes;
2550}
2551
2552static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2553{
2554 return dma->dmab->area;
2555}
2556
2557static int dma_xfer(struct dma_engine *dma,
2558 const unsigned int *data,
2559 unsigned int count)
2560{
2561 memcpy(dma->dmab->area, data, count);
2562 return 0;
2563}
2564
2565static void dma_get_converter_format(
2566 struct dma_engine *dma,
2567 unsigned short *format)
2568{
2569 if (format)
2570 *format = dma->m_converter_format;
2571}
2572
2573static unsigned int dma_get_stream_id(struct dma_engine *dma)
2574{
2575 struct ca0132_spec *spec = dma->codec->spec;
2576
2577 return spec->dsp_stream_id;
2578}
2579
2580struct dsp_image_seg {
2581 u32 magic;
2582 u32 chip_addr;
2583 u32 count;
2584 u32 data[0];
2585};
2586
2587static const u32 g_magic_value = 0x4c46584d;
2588static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2589
2590static bool is_valid(const struct dsp_image_seg *p)
2591{
2592 return p->magic == g_magic_value;
2593}
2594
2595static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2596{
2597 return g_chip_addr_magic_value == p->chip_addr;
2598}
2599
2600static bool is_last(const struct dsp_image_seg *p)
2601{
2602 return p->count == 0;
2603}
2604
2605static size_t dsp_sizeof(const struct dsp_image_seg *p)
2606{
2607 return sizeof(*p) + p->count*sizeof(u32);
2608}
2609
2610static const struct dsp_image_seg *get_next_seg_ptr(
2611 const struct dsp_image_seg *p)
2612{
2613 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2614}
2615
2616/*
2617 * CA0132 chip DSP transfer stuffs. For DSP download.
2618 */
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002619#define INVALID_DMA_CHANNEL (~0U)
Ian Minett01ef7db2012-09-20 20:29:16 -07002620
Ian Minettd5c21b82012-09-20 20:29:18 -07002621/*
2622 * Program a list of address/data pairs via the ChipIO widget.
2623 * The segment data is in the format of successive pairs of words.
2624 * These are repeated as indicated by the segment's count field.
2625 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002626static int dspxfr_hci_write(struct hda_codec *codec,
2627 const struct dsp_image_seg *fls)
2628{
2629 int status;
2630 const u32 *data;
2631 unsigned int count;
2632
2633 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002634 codec_dbg(codec, "hci_write invalid params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002635 return -EINVAL;
2636 }
2637
2638 count = fls->count;
2639 data = (u32 *)(fls->data);
2640 while (count >= 2) {
2641 status = chipio_write(codec, data[0], data[1]);
2642 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002643 codec_dbg(codec, "hci_write chipio failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002644 return status;
2645 }
2646 count -= 2;
2647 data += 2;
2648 }
2649 return 0;
2650}
2651
Ian Minettd5c21b82012-09-20 20:29:18 -07002652/**
2653 * Write a block of data into DSP code or data RAM using pre-allocated
2654 * DMA engine.
2655 *
2656 * @codec: the HDA codec
2657 * @fls: pointer to a fast load image
2658 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2659 * no relocation
2660 * @dma_engine: pointer to DMA engine to be used for DSP download
2661 * @dma_chan: The number of DMA channels used for DSP download
2662 * @port_map_mask: port mapping
2663 * @ovly: TRUE if overlay format is required
2664 *
2665 * Returns zero or a negative error code.
2666 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002667static int dspxfr_one_seg(struct hda_codec *codec,
2668 const struct dsp_image_seg *fls,
2669 unsigned int reloc,
2670 struct dma_engine *dma_engine,
2671 unsigned int dma_chan,
2672 unsigned int port_map_mask,
2673 bool ovly)
2674{
Ian Minett406261c2012-12-20 18:53:41 -08002675 int status = 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002676 bool comm_dma_setup_done = false;
2677 const unsigned int *data;
2678 unsigned int chip_addx;
2679 unsigned int words_to_write;
2680 unsigned int buffer_size_words;
2681 unsigned char *buffer_addx;
2682 unsigned short hda_format;
2683 unsigned int sample_rate_div;
2684 unsigned int sample_rate_mul;
2685 unsigned int num_chans;
2686 unsigned int hda_frame_size_words;
2687 unsigned int remainder_words;
2688 const u32 *data_remainder;
2689 u32 chip_addx_remainder;
2690 unsigned int run_size_words;
2691 const struct dsp_image_seg *hci_write = NULL;
Ian Minett6d675302013-02-08 18:31:43 -08002692 unsigned long timeout;
2693 bool dma_active;
Ian Minett01ef7db2012-09-20 20:29:16 -07002694
2695 if (fls == NULL)
2696 return -EINVAL;
2697 if (is_hci_prog_list_seg(fls)) {
2698 hci_write = fls;
2699 fls = get_next_seg_ptr(fls);
2700 }
2701
2702 if (hci_write && (!fls || is_last(fls))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002703 codec_dbg(codec, "hci_write\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002704 return dspxfr_hci_write(codec, hci_write);
2705 }
2706
2707 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002708 codec_dbg(codec, "Invalid Params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002709 return -EINVAL;
2710 }
2711
2712 data = fls->data;
2713 chip_addx = fls->chip_addr,
2714 words_to_write = fls->count;
2715
2716 if (!words_to_write)
2717 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2718 if (reloc)
2719 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2720
2721 if (!UC_RANGE(chip_addx, words_to_write) &&
2722 !X_RANGE_ALL(chip_addx, words_to_write) &&
2723 !Y_RANGE_ALL(chip_addx, words_to_write)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002724 codec_dbg(codec, "Invalid chip_addx Params\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002725 return -EINVAL;
2726 }
2727
2728 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2729 sizeof(u32);
2730
2731 buffer_addx = dma_get_buffer_addr(dma_engine);
2732
2733 if (buffer_addx == NULL) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002734 codec_dbg(codec, "dma_engine buffer NULL\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002735 return -EINVAL;
2736 }
2737
2738 dma_get_converter_format(dma_engine, &hda_format);
2739 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2740 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2741 num_chans = get_hdafmt_chs(hda_format) + 1;
2742
2743 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2744 (num_chans * sample_rate_mul / sample_rate_div));
2745
Xi Wang3bc085a2013-03-07 00:13:51 -05002746 if (hda_frame_size_words == 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002747 codec_dbg(codec, "frmsz zero\n");
Xi Wang3bc085a2013-03-07 00:13:51 -05002748 return -EINVAL;
2749 }
2750
Ian Minett01ef7db2012-09-20 20:29:16 -07002751 buffer_size_words = min(buffer_size_words,
2752 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2753 65536 : 32768));
2754 buffer_size_words -= buffer_size_words % hda_frame_size_words;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002755 codec_dbg(codec,
Ian Minett01ef7db2012-09-20 20:29:16 -07002756 "chpadr=0x%08x frmsz=%u nchan=%u "
2757 "rate_mul=%u div=%u bufsz=%u\n",
2758 chip_addx, hda_frame_size_words, num_chans,
2759 sample_rate_mul, sample_rate_div, buffer_size_words);
2760
Xi Wang3bc085a2013-03-07 00:13:51 -05002761 if (buffer_size_words < hda_frame_size_words) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002762 codec_dbg(codec, "dspxfr_one_seg:failed\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002763 return -EINVAL;
2764 }
2765
2766 remainder_words = words_to_write % hda_frame_size_words;
2767 data_remainder = data;
2768 chip_addx_remainder = chip_addx;
2769
2770 data += remainder_words;
2771 chip_addx += remainder_words*sizeof(u32);
2772 words_to_write -= remainder_words;
2773
2774 while (words_to_write != 0) {
2775 run_size_words = min(buffer_size_words, words_to_write);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002776 codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
Ian Minett01ef7db2012-09-20 20:29:16 -07002777 words_to_write, run_size_words, remainder_words);
2778 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2779 if (!comm_dma_setup_done) {
2780 status = dsp_dma_stop(codec, dma_chan, ovly);
2781 if (status < 0)
Takashi Iwai425a7882013-01-15 17:41:21 +01002782 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002783 status = dsp_dma_setup_common(codec, chip_addx,
2784 dma_chan, port_map_mask, ovly);
2785 if (status < 0)
2786 return status;
2787 comm_dma_setup_done = true;
2788 }
2789
2790 status = dsp_dma_setup(codec, chip_addx,
2791 run_size_words, dma_chan);
2792 if (status < 0)
2793 return status;
2794 status = dsp_dma_start(codec, dma_chan, ovly);
2795 if (status < 0)
2796 return status;
2797 if (!dsp_is_dma_active(codec, dma_chan)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002798 codec_dbg(codec, "dspxfr:DMA did not start\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002799 return -EIO;
2800 }
2801 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2802 if (status < 0)
2803 return status;
2804 if (remainder_words != 0) {
2805 status = chipio_write_multiple(codec,
2806 chip_addx_remainder,
2807 data_remainder,
2808 remainder_words);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002809 if (status < 0)
2810 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002811 remainder_words = 0;
2812 }
2813 if (hci_write) {
2814 status = dspxfr_hci_write(codec, hci_write);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002815 if (status < 0)
2816 return status;
Ian Minett01ef7db2012-09-20 20:29:16 -07002817 hci_write = NULL;
2818 }
Ian Minett6d675302013-02-08 18:31:43 -08002819
2820 timeout = jiffies + msecs_to_jiffies(2000);
2821 do {
2822 dma_active = dsp_is_dma_active(codec, dma_chan);
2823 if (!dma_active)
Ian Minett01ef7db2012-09-20 20:29:16 -07002824 break;
Ian Minett6d675302013-02-08 18:31:43 -08002825 msleep(20);
2826 } while (time_before(jiffies, timeout));
2827 if (dma_active)
2828 break;
2829
Takashi Iwai4e76a882014-02-25 12:21:03 +01002830 codec_dbg(codec, "+++++ DMA complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002831 dma_set_state(dma_engine, DMA_STATE_STOP);
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002832 status = dma_reset(dma_engine);
Ian Minett01ef7db2012-09-20 20:29:16 -07002833
2834 if (status < 0)
2835 return status;
2836
2837 data += run_size_words;
2838 chip_addx += run_size_words*sizeof(u32);
2839 words_to_write -= run_size_words;
2840 }
2841
2842 if (remainder_words != 0) {
2843 status = chipio_write_multiple(codec, chip_addx_remainder,
2844 data_remainder, remainder_words);
2845 }
2846
2847 return status;
2848}
2849
Ian Minettd5c21b82012-09-20 20:29:18 -07002850/**
2851 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2852 *
2853 * @codec: the HDA codec
2854 * @fls_data: pointer to a fast load image
2855 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2856 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002857 * @sample_rate: sampling rate of the stream used for DSP download
Takashi Iwaie60b2c72014-11-10 16:47:26 +01002858 * @channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002859 * @ovly: TRUE if overlay format is required
2860 *
2861 * Returns zero or a negative error code.
2862 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002863static int dspxfr_image(struct hda_codec *codec,
2864 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002865 unsigned int reloc,
2866 unsigned int sample_rate,
2867 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002868 bool ovly)
2869{
2870 struct ca0132_spec *spec = codec->spec;
2871 int status;
2872 unsigned short hda_format = 0;
2873 unsigned int response;
2874 unsigned char stream_id = 0;
2875 struct dma_engine *dma_engine;
2876 unsigned int dma_chan;
2877 unsigned int port_map_mask;
2878
2879 if (fls_data == NULL)
2880 return -EINVAL;
2881
2882 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai549e8292013-01-15 17:42:15 +01002883 if (!dma_engine)
2884 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002885
2886 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2887 if (!dma_engine->dmab) {
Takashi Iwai549e8292013-01-15 17:42:15 +01002888 kfree(dma_engine);
2889 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002890 }
2891
2892 dma_engine->codec = codec;
Takashi Iwai6194b992014-06-06 18:12:16 +02002893 dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002894 dma_engine->m_converter_format = hda_format;
2895 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2896 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2897
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002898 dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
Ian Minett01ef7db2012-09-20 20:29:16 -07002899
2900 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2901 hda_format, &response);
2902
2903 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002904 codec_dbg(codec, "set converter format fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002905 goto exit;
2906 }
2907
2908 status = snd_hda_codec_load_dsp_prepare(codec,
2909 dma_engine->m_converter_format,
2910 dma_engine->buf_size,
2911 dma_engine->dmab);
2912 if (status < 0)
2913 goto exit;
2914 spec->dsp_stream_id = status;
2915
2916 if (ovly) {
2917 status = dspio_alloc_dma_chan(codec, &dma_chan);
2918 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002919 codec_dbg(codec, "alloc dmachan fail\n");
Takashi Iwai8ae3124b2013-01-15 17:43:09 +01002920 dma_chan = INVALID_DMA_CHANNEL;
Ian Minett01ef7db2012-09-20 20:29:16 -07002921 goto exit;
2922 }
2923 }
2924
2925 port_map_mask = 0;
2926 status = dsp_allocate_ports_format(codec, hda_format,
2927 &port_map_mask);
2928 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002929 codec_dbg(codec, "alloc ports fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002930 goto exit;
2931 }
2932
2933 stream_id = dma_get_stream_id(dma_engine);
2934 status = codec_set_converter_stream_channel(codec,
2935 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2936 if (status < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002937 codec_dbg(codec, "set stream chan fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002938 goto exit;
2939 }
2940
2941 while ((fls_data != NULL) && !is_last(fls_data)) {
2942 if (!is_valid(fls_data)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002943 codec_dbg(codec, "FLS check fail\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07002944 status = -EINVAL;
2945 goto exit;
2946 }
2947 status = dspxfr_one_seg(codec, fls_data, reloc,
2948 dma_engine, dma_chan,
2949 port_map_mask, ovly);
2950 if (status < 0)
2951 break;
2952
2953 if (is_hci_prog_list_seg(fls_data))
2954 fls_data = get_next_seg_ptr(fls_data);
2955
2956 if ((fls_data != NULL) && !is_last(fls_data))
2957 fls_data = get_next_seg_ptr(fls_data);
2958 }
2959
2960 if (port_map_mask != 0)
2961 status = dsp_free_ports(codec);
2962
2963 if (status < 0)
2964 goto exit;
2965
2966 status = codec_set_converter_stream_channel(codec,
2967 WIDGET_CHIP_CTRL, 0, 0, &response);
2968
2969exit:
2970 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2971 dspio_free_dma_chan(codec, dma_chan);
2972
Takashi Iwaib3667bd2013-02-10 11:58:40 +01002973 if (dma_engine->dmab->area)
Ian Minett01ef7db2012-09-20 20:29:16 -07002974 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2975 kfree(dma_engine->dmab);
2976 kfree(dma_engine);
2977
2978 return status;
2979}
2980
2981/*
2982 * CA0132 DSP download stuffs.
2983 */
2984static void dspload_post_setup(struct hda_codec *codec)
2985{
Connor McAdams009b8f92018-05-08 13:20:06 -04002986 struct ca0132_spec *spec = codec->spec;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002987 codec_dbg(codec, "---- dspload_post_setup ------\n");
Connor McAdams009b8f92018-05-08 13:20:06 -04002988 if (!spec->use_alt_functions) {
2989 /*set DSP speaker to 2.0 configuration*/
2990 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2991 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
Ian Minett01ef7db2012-09-20 20:29:16 -07002992
Connor McAdams009b8f92018-05-08 13:20:06 -04002993 /*update write pointer*/
2994 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2995 }
Ian Minett01ef7db2012-09-20 20:29:16 -07002996}
2997
Ian Minettd5c21b82012-09-20 20:29:18 -07002998/**
Takashi Iwaie60b2c72014-11-10 16:47:26 +01002999 * dspload_image - Download DSP from a DSP Image Fast Load structure.
Ian Minettd5c21b82012-09-20 20:29:18 -07003000 *
3001 * @codec: the HDA codec
3002 * @fls: pointer to a fast load image
3003 * @ovly: TRUE if overlay format is required
3004 * @reloc: Relocation address for loading single-segment overlays, or 0 for
3005 * no relocation
3006 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
3007 * @router_chans: number of audio router channels to be allocated (0 means use
3008 * internal defaults; max is 32)
3009 *
Takashi Iwaie60b2c72014-11-10 16:47:26 +01003010 * Download DSP from a DSP Image Fast Load structure. This structure is a
3011 * linear, non-constant sized element array of structures, each of which
3012 * contain the count of the data to be loaded, the data itself, and the
3013 * corresponding starting chip address of the starting data location.
Ian Minettd5c21b82012-09-20 20:29:18 -07003014 * Returns zero or a negative error code.
3015 */
Ian Minett01ef7db2012-09-20 20:29:16 -07003016static int dspload_image(struct hda_codec *codec,
3017 const struct dsp_image_seg *fls,
3018 bool ovly,
3019 unsigned int reloc,
3020 bool autostart,
3021 int router_chans)
3022{
3023 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07003024 unsigned int sample_rate;
3025 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07003026
Takashi Iwai4e76a882014-02-25 12:21:03 +01003027 codec_dbg(codec, "---- dspload_image begin ------\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003028 if (router_chans == 0) {
3029 if (!ovly)
3030 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
3031 else
3032 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
3033 }
3034
Ian Minette97249d2012-09-20 20:29:21 -07003035 sample_rate = 48000;
3036 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07003037
Ian Minette97249d2012-09-20 20:29:21 -07003038 while (channels > 16) {
3039 sample_rate *= 2;
3040 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07003041 }
3042
Ian Minett01ef7db2012-09-20 20:29:16 -07003043 do {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003044 codec_dbg(codec, "Ready to program DMA\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003045 if (!ovly)
3046 status = dsp_reset(codec);
3047
3048 if (status < 0)
3049 break;
3050
Takashi Iwai4e76a882014-02-25 12:21:03 +01003051 codec_dbg(codec, "dsp_reset() complete\n");
Ian Minette97249d2012-09-20 20:29:21 -07003052 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
3053 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07003054
3055 if (status < 0)
3056 break;
3057
Takashi Iwai4e76a882014-02-25 12:21:03 +01003058 codec_dbg(codec, "dspxfr_image() complete\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003059 if (autostart && !ovly) {
3060 dspload_post_setup(codec);
3061 status = dsp_set_run_state(codec);
3062 }
3063
Takashi Iwai4e76a882014-02-25 12:21:03 +01003064 codec_dbg(codec, "LOAD FINISHED\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003065 } while (0);
3066
3067 return status;
3068}
3069
Takashi Iwaif6644172013-02-11 14:18:29 +01003070#ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07003071static bool dspload_is_loaded(struct hda_codec *codec)
3072{
3073 unsigned int data = 0;
3074 int status = 0;
3075
3076 status = chipio_read(codec, 0x40004, &data);
3077 if ((status < 0) || (data != 1))
3078 return false;
3079
3080 return true;
3081}
Takashi Iwaif6644172013-02-11 14:18:29 +01003082#else
3083#define dspload_is_loaded(codec) false
3084#endif
Ian Minett01ef7db2012-09-20 20:29:16 -07003085
3086static bool dspload_wait_loaded(struct hda_codec *codec)
3087{
Ian Minett6d675302013-02-08 18:31:43 -08003088 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
Ian Minett01ef7db2012-09-20 20:29:16 -07003089
3090 do {
Ian Minett01ef7db2012-09-20 20:29:16 -07003091 if (dspload_is_loaded(codec)) {
Takashi Iwaid9684bb2015-10-26 16:54:16 +01003092 codec_info(codec, "ca0132 DSP downloaded and running\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003093 return true;
3094 }
Ian Minett6d675302013-02-08 18:31:43 -08003095 msleep(20);
3096 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07003097
Takashi Iwaid9684bb2015-10-26 16:54:16 +01003098 codec_err(codec, "ca0132 failed to download DSP\n");
Ian Minett01ef7db2012-09-20 20:29:16 -07003099 return false;
3100}
3101
Ian Minett5aaca442012-12-20 18:53:34 -08003102/*
Connor McAdamse93ac302018-05-08 13:20:05 -04003103 * Setup GPIO for the other variants of Core3D.
3104 */
3105
3106/*
Connor McAdamsa62e4732018-08-08 13:34:12 -04003107 * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
3108 * the mmio address 0x320 is used to set GPIO pins. The format for the data
3109 * The first eight bits are just the number of the pin. So far, I've only seen
3110 * this number go to 7.
3111 */
3112static void ca0132_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
3113 bool enable)
3114{
3115 struct ca0132_spec *spec = codec->spec;
3116 unsigned short gpio_data;
3117
3118 gpio_data = gpio_pin & 0xF;
3119 gpio_data |= ((enable << 8) & 0x100);
3120
3121 writew(gpio_data, spec->mem_base + 0x320);
3122}
3123
3124/*
Connor McAdamse93ac302018-05-08 13:20:05 -04003125 * Sets up the GPIO pins so that they are discoverable. If this isn't done,
3126 * the card shows as having no GPIO pins.
3127 */
3128static void ca0132_gpio_init(struct hda_codec *codec)
3129{
3130 struct ca0132_spec *spec = codec->spec;
3131
3132 switch (spec->quirk) {
3133 case QUIRK_SBZ:
3134 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3135 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
3136 snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
3137 break;
3138 case QUIRK_R3DI:
3139 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3140 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
3141 break;
3142 }
3143
3144}
3145
3146/* Sets the GPIO for audio output. */
3147static void ca0132_gpio_setup(struct hda_codec *codec)
3148{
3149 struct ca0132_spec *spec = codec->spec;
3150
3151 switch (spec->quirk) {
3152 case QUIRK_SBZ:
3153 snd_hda_codec_write(codec, 0x01, 0,
3154 AC_VERB_SET_GPIO_DIRECTION, 0x07);
3155 snd_hda_codec_write(codec, 0x01, 0,
3156 AC_VERB_SET_GPIO_MASK, 0x07);
3157 snd_hda_codec_write(codec, 0x01, 0,
3158 AC_VERB_SET_GPIO_DATA, 0x04);
3159 snd_hda_codec_write(codec, 0x01, 0,
3160 AC_VERB_SET_GPIO_DATA, 0x06);
3161 break;
3162 case QUIRK_R3DI:
3163 snd_hda_codec_write(codec, 0x01, 0,
3164 AC_VERB_SET_GPIO_DIRECTION, 0x1E);
3165 snd_hda_codec_write(codec, 0x01, 0,
3166 AC_VERB_SET_GPIO_MASK, 0x1F);
3167 snd_hda_codec_write(codec, 0x01, 0,
3168 AC_VERB_SET_GPIO_DATA, 0x0C);
3169 break;
3170 }
3171}
3172
3173/*
Connor McAdams7e6ed622018-05-08 13:20:08 -04003174 * GPIO control functions for the Recon3D integrated.
3175 */
3176
3177enum r3di_gpio_bit {
3178 /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
3179 R3DI_MIC_SELECT_BIT = 1,
3180 /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
3181 R3DI_OUT_SELECT_BIT = 2,
3182 /*
3183 * I dunno what this actually does, but it stays on until the dsp
3184 * is downloaded.
3185 */
3186 R3DI_GPIO_DSP_DOWNLOADING = 3,
3187 /*
3188 * Same as above, no clue what it does, but it comes on after the dsp
3189 * is downloaded.
3190 */
3191 R3DI_GPIO_DSP_DOWNLOADED = 4
3192};
3193
3194enum r3di_mic_select {
3195 /* Set GPIO bit 1 to 0 for rear mic */
3196 R3DI_REAR_MIC = 0,
3197 /* Set GPIO bit 1 to 1 for front microphone*/
3198 R3DI_FRONT_MIC = 1
3199};
3200
3201enum r3di_out_select {
3202 /* Set GPIO bit 2 to 0 for headphone */
3203 R3DI_HEADPHONE_OUT = 0,
3204 /* Set GPIO bit 2 to 1 for speaker */
3205 R3DI_LINE_OUT = 1
3206};
3207enum r3di_dsp_status {
3208 /* Set GPIO bit 3 to 1 until DSP is downloaded */
3209 R3DI_DSP_DOWNLOADING = 0,
3210 /* Set GPIO bit 4 to 1 once DSP is downloaded */
3211 R3DI_DSP_DOWNLOADED = 1
3212};
3213
Connor McAdams7cb9d942018-05-08 13:20:10 -04003214
3215static void r3di_gpio_mic_set(struct hda_codec *codec,
3216 enum r3di_mic_select cur_mic)
3217{
3218 unsigned int cur_gpio;
3219
3220 /* Get the current GPIO Data setup */
3221 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3222
3223 switch (cur_mic) {
3224 case R3DI_REAR_MIC:
3225 cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
3226 break;
3227 case R3DI_FRONT_MIC:
3228 cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
3229 break;
3230 }
3231 snd_hda_codec_write(codec, codec->core.afg, 0,
3232 AC_VERB_SET_GPIO_DATA, cur_gpio);
3233}
3234
3235static void r3di_gpio_out_set(struct hda_codec *codec,
3236 enum r3di_out_select cur_out)
3237{
3238 unsigned int cur_gpio;
3239
3240 /* Get the current GPIO Data setup */
3241 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3242
3243 switch (cur_out) {
3244 case R3DI_HEADPHONE_OUT:
3245 cur_gpio &= ~(1 << R3DI_OUT_SELECT_BIT);
3246 break;
3247 case R3DI_LINE_OUT:
3248 cur_gpio |= (1 << R3DI_OUT_SELECT_BIT);
3249 break;
3250 }
3251 snd_hda_codec_write(codec, codec->core.afg, 0,
3252 AC_VERB_SET_GPIO_DATA, cur_gpio);
3253}
3254
Connor McAdams7e6ed622018-05-08 13:20:08 -04003255static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
3256 enum r3di_dsp_status dsp_status)
3257{
3258 unsigned int cur_gpio;
3259
3260 /* Get the current GPIO Data setup */
3261 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3262
3263 switch (dsp_status) {
3264 case R3DI_DSP_DOWNLOADING:
3265 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
3266 snd_hda_codec_write(codec, codec->core.afg, 0,
3267 AC_VERB_SET_GPIO_DATA, cur_gpio);
3268 break;
3269 case R3DI_DSP_DOWNLOADED:
3270 /* Set DOWNLOADING bit to 0. */
3271 cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
3272
3273 snd_hda_codec_write(codec, codec->core.afg, 0,
3274 AC_VERB_SET_GPIO_DATA, cur_gpio);
3275
3276 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
3277 break;
3278 }
3279
3280 snd_hda_codec_write(codec, codec->core.afg, 0,
3281 AC_VERB_SET_GPIO_DATA, cur_gpio);
3282}
3283
3284/*
Ian Minett825315b2012-12-20 18:53:36 -08003285 * PCM callbacks
3286 */
Ian Minett95c6e9c2011-06-15 15:35:17 -07003287static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3288 struct hda_codec *codec,
3289 unsigned int stream_tag,
3290 unsigned int format,
3291 struct snd_pcm_substream *substream)
3292{
3293 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08003294
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003295 snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
Ian Minett825315b2012-12-20 18:53:36 -08003296
3297 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003298}
3299
3300static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3301 struct hda_codec *codec,
3302 struct snd_pcm_substream *substream)
3303{
3304 struct ca0132_spec *spec = codec->spec;
Ian Minett825315b2012-12-20 18:53:36 -08003305
3306 if (spec->dsp_state == DSP_DOWNLOADING)
3307 return 0;
3308
3309 /*If Playback effects are on, allow stream some time to flush
3310 *effects tail*/
3311 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3312 msleep(50);
3313
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003314 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
Ian Minett825315b2012-12-20 18:53:36 -08003315
3316 return 0;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003317}
3318
Dylan Reide8412ca2013-04-04 13:55:09 -07003319static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
3320 struct hda_codec *codec,
3321 struct snd_pcm_substream *substream)
3322{
3323 struct ca0132_spec *spec = codec->spec;
3324 unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
3325 struct snd_pcm_runtime *runtime = substream->runtime;
3326
3327 if (spec->dsp_state != DSP_DOWNLOADED)
3328 return 0;
3329
3330 /* Add latency if playback enhancement and either effect is enabled. */
3331 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
3332 if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
3333 (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
3334 latency += DSP_PLAY_ENHANCEMENT_LATENCY;
3335 }
3336
3337 /* Applying Speaker EQ adds latency as well. */
3338 if (spec->cur_out_type == SPEAKER_OUT)
3339 latency += DSP_SPEAKER_OUT_LATENCY;
3340
3341 return (latency * runtime->rate) / 1000;
3342}
3343
Ian Minett95c6e9c2011-06-15 15:35:17 -07003344/*
3345 * Digital out
3346 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003347static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3348 struct hda_codec *codec,
3349 struct snd_pcm_substream *substream)
3350{
3351 struct ca0132_spec *spec = codec->spec;
3352 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3353}
3354
Ian Minett95c6e9c2011-06-15 15:35:17 -07003355static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3356 struct hda_codec *codec,
3357 unsigned int stream_tag,
3358 unsigned int format,
3359 struct snd_pcm_substream *substream)
3360{
3361 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003362 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3363 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003364}
3365
3366static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3367 struct hda_codec *codec,
3368 struct snd_pcm_substream *substream)
3369{
3370 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003371 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003372}
3373
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003374static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3375 struct hda_codec *codec,
3376 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07003377{
3378 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003379 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003380}
3381
3382/*
Ian Minett825315b2012-12-20 18:53:36 -08003383 * Analog capture
3384 */
3385static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3386 struct hda_codec *codec,
3387 unsigned int stream_tag,
3388 unsigned int format,
3389 struct snd_pcm_substream *substream)
3390{
Hsin-Yu Chao13c12db2014-02-19 14:30:35 +08003391 snd_hda_codec_setup_stream(codec, hinfo->nid,
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003392 stream_tag, 0, format);
Ian Minett825315b2012-12-20 18:53:36 -08003393
3394 return 0;
3395}
3396
3397static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3398 struct hda_codec *codec,
3399 struct snd_pcm_substream *substream)
3400{
3401 struct ca0132_spec *spec = codec->spec;
3402
3403 if (spec->dsp_state == DSP_DOWNLOADING)
3404 return 0;
3405
Hsin-Yu Chao28fba952014-02-19 14:27:07 +08003406 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
Ian Minett825315b2012-12-20 18:53:36 -08003407 return 0;
3408}
3409
Dylan Reide8412ca2013-04-04 13:55:09 -07003410static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
3411 struct hda_codec *codec,
3412 struct snd_pcm_substream *substream)
3413{
3414 struct ca0132_spec *spec = codec->spec;
3415 unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
3416 struct snd_pcm_runtime *runtime = substream->runtime;
3417
3418 if (spec->dsp_state != DSP_DOWNLOADED)
3419 return 0;
3420
3421 if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
3422 latency += DSP_CRYSTAL_VOICE_LATENCY;
3423
3424 return (latency * runtime->rate) / 1000;
3425}
3426
Ian Minette90f29e2012-12-20 18:53:39 -08003427/*
3428 * Controls stuffs.
3429 */
3430
3431/*
3432 * Mixer controls helpers.
3433 */
3434#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
3435 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3436 .name = xname, \
3437 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3438 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3439 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
3440 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
3441 .info = ca0132_volume_info, \
3442 .get = ca0132_volume_get, \
3443 .put = ca0132_volume_put, \
3444 .tlv = { .c = ca0132_volume_tlv }, \
3445 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3446
Connor McAdams017310f2018-05-08 13:20:11 -04003447/*
3448 * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
3449 * volume put, which is used for setting the DSP volume. This was done because
3450 * the ca0132 functions were taking too much time and causing lag.
3451 */
3452#define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
3453 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3454 .name = xname, \
3455 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3456 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
3457 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
3458 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
3459 .info = snd_hda_mixer_amp_volume_info, \
3460 .get = snd_hda_mixer_amp_volume_get, \
3461 .put = ca0132_alt_volume_put, \
3462 .tlv = { .c = snd_hda_mixer_amp_tlv }, \
3463 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3464
Ian Minette90f29e2012-12-20 18:53:39 -08003465#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
3466 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
3467 .name = xname, \
3468 .subdevice = HDA_SUBDEV_AMP_FLAG, \
3469 .info = snd_hda_mixer_amp_switch_info, \
3470 .get = ca0132_switch_get, \
3471 .put = ca0132_switch_put, \
3472 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
3473
3474/* stereo */
3475#define CA0132_CODEC_VOL(xname, nid, dir) \
3476 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
Connor McAdams017310f2018-05-08 13:20:11 -04003477#define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
3478 CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
Ian Minette90f29e2012-12-20 18:53:39 -08003479#define CA0132_CODEC_MUTE(xname, nid, dir) \
3480 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
3481
Connor McAdams017310f2018-05-08 13:20:11 -04003482/* lookup tables */
3483/*
3484 * Lookup table with decibel values for the DSP. When volume is changed in
3485 * Windows, the DSP is also sent the dB value in floating point. In Windows,
3486 * these values have decimal points, probably because the Windows driver
3487 * actually uses floating point. We can't here, so I made a lookup table of
3488 * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
3489 * DAC's, and 9 is the maximum.
3490 */
3491static const unsigned int float_vol_db_lookup[] = {
34920xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
34930xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
34940xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
34950xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
34960xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
34970xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
34980xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
34990xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
35000xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
35010xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
35020xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
35030xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
35040xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
35050xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
35060xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
35070x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
35080x40C00000, 0x40E00000, 0x41000000, 0x41100000
3509};
3510
Connor McAdams47cdf762018-05-08 13:20:13 -04003511/*
3512 * This table counts from float 0 to 1 in increments of .01, which is
3513 * useful for a few different sliders.
3514 */
3515static const unsigned int float_zero_to_one_lookup[] = {
35160x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
35170x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
35180x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
35190x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
35200x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
35210x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
35220x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
35230x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
35240x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
35250x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
35260x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
35270x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
35280x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
35290x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
35300x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
35310x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
35320x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
3533};
3534
3535/*
3536 * This table counts from float 10 to 1000, which is the range of the x-bass
3537 * crossover slider in Windows.
3538 */
3539static const unsigned int float_xbass_xover_lookup[] = {
35400x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
35410x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
35420x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
35430x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
35440x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
35450x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
35460x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
35470x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
35480x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
35490x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
35500x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
35510x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
35520x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
35530x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
35540x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
35550x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
35560x44728000, 0x44750000, 0x44778000, 0x447A0000
3557};
3558
Masahiro Yamada4091fb92017-02-27 14:29:56 -08003559/* The following are for tuning of products */
Ian Minett44f0c972012-12-20 18:53:38 -08003560#ifdef ENABLE_TUNING_CONTROLS
3561
3562static unsigned int voice_focus_vals_lookup[] = {
35630x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
35640x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
35650x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
35660x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
35670x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
35680x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
35690x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
35700x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
35710x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
35720x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
35730x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
35740x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
35750x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
35760x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
35770x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
35780x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
35790x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
35800x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
35810x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
35820x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
35830x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
35840x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
35850x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
35860x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
35870x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
35880x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
35890x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
3590};
3591
3592static unsigned int mic_svm_vals_lookup[] = {
35930x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
35940x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
35950x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
35960x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
35970x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
35980x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
35990x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
36000x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
36010x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
36020x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
36030x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
36040x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
36050x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
36060x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
36070x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
36080x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
36090x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
3610};
3611
3612static unsigned int equalizer_vals_lookup[] = {
36130xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
36140xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
36150xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
36160xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
36170x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
36180x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
36190x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
36200x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
36210x41C00000
3622};
3623
3624static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
3625 unsigned int *lookup, int idx)
3626{
3627 int i = 0;
3628
3629 for (i = 0; i < TUNING_CTLS_COUNT; i++)
3630 if (nid == ca0132_tuning_ctls[i].nid)
3631 break;
3632
3633 snd_hda_power_up(codec);
Connor McAdams447fd8e2018-05-08 13:20:09 -04003634 dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
Ian Minett44f0c972012-12-20 18:53:38 -08003635 ca0132_tuning_ctls[i].req,
3636 &(lookup[idx]), sizeof(unsigned int));
3637 snd_hda_power_down(codec);
3638
3639 return 1;
3640}
3641
3642static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
3643 struct snd_ctl_elem_value *ucontrol)
3644{
3645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3646 struct ca0132_spec *spec = codec->spec;
3647 hda_nid_t nid = get_amp_nid(kcontrol);
3648 long *valp = ucontrol->value.integer.value;
3649 int idx = nid - TUNING_CTL_START_NID;
3650
3651 *valp = spec->cur_ctl_vals[idx];
3652 return 0;
3653}
3654
3655static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
3656 struct snd_ctl_elem_info *uinfo)
3657{
3658 int chs = get_amp_channels(kcontrol);
3659 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3660 uinfo->count = chs == 3 ? 2 : 1;
3661 uinfo->value.integer.min = 20;
3662 uinfo->value.integer.max = 180;
3663 uinfo->value.integer.step = 1;
3664
3665 return 0;
3666}
3667
3668static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
3669 struct snd_ctl_elem_value *ucontrol)
3670{
3671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3672 struct ca0132_spec *spec = codec->spec;
3673 hda_nid_t nid = get_amp_nid(kcontrol);
3674 long *valp = ucontrol->value.integer.value;
3675 int idx;
3676
3677 idx = nid - TUNING_CTL_START_NID;
3678 /* any change? */
3679 if (spec->cur_ctl_vals[idx] == *valp)
3680 return 0;
3681
3682 spec->cur_ctl_vals[idx] = *valp;
3683
3684 idx = *valp - 20;
3685 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
3686
3687 return 1;
3688}
3689
3690static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
3691 struct snd_ctl_elem_info *uinfo)
3692{
3693 int chs = get_amp_channels(kcontrol);
3694 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3695 uinfo->count = chs == 3 ? 2 : 1;
3696 uinfo->value.integer.min = 0;
3697 uinfo->value.integer.max = 100;
3698 uinfo->value.integer.step = 1;
3699
3700 return 0;
3701}
3702
3703static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
3704 struct snd_ctl_elem_value *ucontrol)
3705{
3706 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3707 struct ca0132_spec *spec = codec->spec;
3708 hda_nid_t nid = get_amp_nid(kcontrol);
3709 long *valp = ucontrol->value.integer.value;
3710 int idx;
3711
3712 idx = nid - TUNING_CTL_START_NID;
3713 /* any change? */
3714 if (spec->cur_ctl_vals[idx] == *valp)
3715 return 0;
3716
3717 spec->cur_ctl_vals[idx] = *valp;
3718
3719 idx = *valp;
3720 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
3721
3722 return 0;
3723}
3724
3725static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
3726 struct snd_ctl_elem_info *uinfo)
3727{
3728 int chs = get_amp_channels(kcontrol);
3729 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3730 uinfo->count = chs == 3 ? 2 : 1;
3731 uinfo->value.integer.min = 0;
3732 uinfo->value.integer.max = 48;
3733 uinfo->value.integer.step = 1;
3734
3735 return 0;
3736}
3737
3738static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
3739 struct snd_ctl_elem_value *ucontrol)
3740{
3741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3742 struct ca0132_spec *spec = codec->spec;
3743 hda_nid_t nid = get_amp_nid(kcontrol);
3744 long *valp = ucontrol->value.integer.value;
3745 int idx;
3746
3747 idx = nid - TUNING_CTL_START_NID;
3748 /* any change? */
3749 if (spec->cur_ctl_vals[idx] == *valp)
3750 return 0;
3751
3752 spec->cur_ctl_vals[idx] = *valp;
3753
3754 idx = *valp;
3755 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
3756
3757 return 1;
3758}
3759
Takashi Sakamoto8e142e92018-05-02 22:48:16 +09003760static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
3761static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
Ian Minett44f0c972012-12-20 18:53:38 -08003762
3763static int add_tuning_control(struct hda_codec *codec,
3764 hda_nid_t pnid, hda_nid_t nid,
3765 const char *name, int dir)
3766{
Takashi Iwai975cc022013-06-28 11:56:49 +02003767 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minett44f0c972012-12-20 18:53:38 -08003768 int type = dir ? HDA_INPUT : HDA_OUTPUT;
3769 struct snd_kcontrol_new knew =
3770 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
3771
3772 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3773 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
3774 knew.tlv.c = 0;
3775 knew.tlv.p = 0;
3776 switch (pnid) {
3777 case VOICE_FOCUS:
3778 knew.info = voice_focus_ctl_info;
3779 knew.get = tuning_ctl_get;
3780 knew.put = voice_focus_ctl_put;
3781 knew.tlv.p = voice_focus_db_scale;
3782 break;
3783 case MIC_SVM:
3784 knew.info = mic_svm_ctl_info;
3785 knew.get = tuning_ctl_get;
3786 knew.put = mic_svm_ctl_put;
3787 break;
3788 case EQUALIZER:
3789 knew.info = equalizer_ctl_info;
3790 knew.get = tuning_ctl_get;
3791 knew.put = equalizer_ctl_put;
3792 knew.tlv.p = eq_db_scale;
3793 break;
3794 default:
3795 return 0;
3796 }
3797 knew.private_value =
3798 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
3799 sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
3800 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
3801}
3802
3803static int add_tuning_ctls(struct hda_codec *codec)
3804{
3805 int i;
3806 int err;
3807
3808 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
3809 err = add_tuning_control(codec,
3810 ca0132_tuning_ctls[i].parent_nid,
3811 ca0132_tuning_ctls[i].nid,
3812 ca0132_tuning_ctls[i].name,
3813 ca0132_tuning_ctls[i].direct);
3814 if (err < 0)
3815 return err;
3816 }
3817
3818 return 0;
3819}
3820
3821static void ca0132_init_tuning_defaults(struct hda_codec *codec)
3822{
3823 struct ca0132_spec *spec = codec->spec;
3824 int i;
3825
3826 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
3827 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
3828 /* SVM level defaults to 0.74. */
3829 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
3830
3831 /* EQ defaults to 0dB. */
3832 for (i = 2; i < TUNING_CTLS_COUNT; i++)
3833 spec->cur_ctl_vals[i] = 24;
3834}
3835#endif /*ENABLE_TUNING_CONTROLS*/
3836
Ian Minett825315b2012-12-20 18:53:36 -08003837/*
Ian Minett5aaca442012-12-20 18:53:34 -08003838 * Select the active output.
3839 * If autodetect is enabled, output will be selected based on jack detection.
3840 * If jack inserted, headphone will be selected, else built-in speakers
3841 * If autodetect is disabled, output will be selected based on selection.
3842 */
3843static int ca0132_select_out(struct hda_codec *codec)
3844{
3845 struct ca0132_spec *spec = codec->spec;
3846 unsigned int pin_ctl;
3847 int jack_present;
3848 int auto_jack;
3849 unsigned int tmp;
3850 int err;
3851
Takashi Iwai4e76a882014-02-25 12:21:03 +01003852 codec_dbg(codec, "ca0132_select_out\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003853
Takashi Iwai664c7152015-04-08 11:43:14 +02003854 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08003855
3856 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3857
3858 if (auto_jack)
Takashi Iwaife14f392015-08-10 16:53:32 +02003859 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
Ian Minett5aaca442012-12-20 18:53:34 -08003860 else
3861 jack_present =
3862 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
3863
3864 if (jack_present)
3865 spec->cur_out_type = HEADPHONE_OUT;
3866 else
3867 spec->cur_out_type = SPEAKER_OUT;
3868
3869 if (spec->cur_out_type == SPEAKER_OUT) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003870 codec_dbg(codec, "ca0132_select_out speaker\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003871 /*speaker out config*/
3872 tmp = FLOAT_ONE;
3873 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3874 if (err < 0)
3875 goto exit;
3876 /*enable speaker EQ*/
3877 tmp = FLOAT_ONE;
3878 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3879 if (err < 0)
3880 goto exit;
3881
3882 /* Setup EAPD */
3883 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3884 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3885 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3886 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3887 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3888 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3889 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3890 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3891
3892 /* disable headphone node */
3893 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3894 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003895 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3896 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003897 /* enable speaker node */
3898 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
Connor McAdams8a19bce2018-05-08 13:20:01 -04003899 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003900 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3901 pin_ctl | PIN_OUT);
Ian Minett5aaca442012-12-20 18:53:34 -08003902 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003903 codec_dbg(codec, "ca0132_select_out hp\n");
Ian Minett5aaca442012-12-20 18:53:34 -08003904 /*headphone out config*/
3905 tmp = FLOAT_ZERO;
3906 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
3907 if (err < 0)
3908 goto exit;
3909 /*disable speaker EQ*/
3910 tmp = FLOAT_ZERO;
3911 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
3912 if (err < 0)
3913 goto exit;
3914
3915 /* Setup EAPD */
3916 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3917 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
3918 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3919 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
3920 snd_hda_codec_write(codec, spec->out_pins[1], 0,
3921 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
3922 snd_hda_codec_write(codec, spec->out_pins[0], 0,
3923 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3924
3925 /* disable speaker*/
3926 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
3927 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003928 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
3929 pin_ctl & ~PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003930 /* enable headphone*/
3931 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
3932 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
Takashi Iwaia0c041c2013-01-15 17:13:31 +01003933 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
3934 pin_ctl | PIN_HP);
Ian Minett5aaca442012-12-20 18:53:34 -08003935 }
3936
3937exit:
Takashi Iwai664c7152015-04-08 11:43:14 +02003938 snd_hda_power_down_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08003939
3940 return err < 0 ? err : 0;
3941}
3942
Connor McAdams7cb9d942018-05-08 13:20:10 -04003943/*
3944 * This function behaves similarly to the ca0132_select_out funciton above,
3945 * except with a few differences. It adds the ability to select the current
3946 * output with an enumerated control "output source" if the auto detect
3947 * mute switch is set to off. If the auto detect mute switch is enabled, it
3948 * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
3949 * It also adds the ability to auto-detect the front headphone port. The only
3950 * way to select surround is to disable auto detect, and set Surround with the
3951 * enumerated control.
3952 */
3953static int ca0132_alt_select_out(struct hda_codec *codec)
3954{
3955 struct ca0132_spec *spec = codec->spec;
3956 unsigned int pin_ctl;
3957 int jack_present;
3958 int auto_jack;
3959 unsigned int i;
3960 unsigned int tmp;
3961 int err;
3962 /* Default Headphone is rear headphone */
3963 hda_nid_t headphone_nid = spec->out_pins[1];
3964
3965 codec_dbg(codec, "%s\n", __func__);
3966
3967 snd_hda_power_up_pm(codec);
3968
3969 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
3970
3971 /*
3972 * If headphone rear or front is plugged in, set to headphone.
3973 * If neither is plugged in, set to rear line out. Only if
3974 * hp/speaker auto detect is enabled.
3975 */
3976 if (auto_jack) {
3977 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
3978 snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
3979
3980 if (jack_present)
3981 spec->cur_out_type = HEADPHONE_OUT;
3982 else
3983 spec->cur_out_type = SPEAKER_OUT;
3984 } else
3985 spec->cur_out_type = spec->out_enum_val;
3986
3987 /* Begin DSP output switch */
3988 tmp = FLOAT_ONE;
3989 err = dspio_set_uint_param(codec, 0x96, 0x3A, tmp);
3990 if (err < 0)
3991 goto exit;
3992
3993 switch (spec->cur_out_type) {
3994 case SPEAKER_OUT:
3995 codec_dbg(codec, "%s speaker\n", __func__);
3996 /*speaker out config*/
3997 switch (spec->quirk) {
3998 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04003999 ca0132_mmio_gpio_set(codec, 7, false);
4000 ca0132_mmio_gpio_set(codec, 4, true);
4001 ca0132_mmio_gpio_set(codec, 1, true);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004002 chipio_set_control_param(codec, 0x0D, 0x18);
4003 break;
4004 case QUIRK_R3DI:
4005 chipio_set_control_param(codec, 0x0D, 0x24);
4006 r3di_gpio_out_set(codec, R3DI_LINE_OUT);
4007 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004008 case QUIRK_R3D:
4009 chipio_set_control_param(codec, 0x0D, 0x24);
4010 ca0132_mmio_gpio_set(codec, 1, true);
4011 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004012 }
4013
4014 /* disable headphone node */
4015 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4016 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4017 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4018 pin_ctl & ~PIN_HP);
4019 /* enable line-out node */
4020 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4021 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4022 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4023 pin_ctl | PIN_OUT);
4024 /* Enable EAPD */
4025 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4026 AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4027
4028 /* If PlayEnhancement is enabled, set different source */
4029 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4030 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4031 else
4032 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
4033 break;
4034 case HEADPHONE_OUT:
4035 codec_dbg(codec, "%s hp\n", __func__);
4036 /* Headphone out config*/
4037 switch (spec->quirk) {
4038 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004039 ca0132_mmio_gpio_set(codec, 7, true);
4040 ca0132_mmio_gpio_set(codec, 4, true);
4041 ca0132_mmio_gpio_set(codec, 1, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004042 chipio_set_control_param(codec, 0x0D, 0x12);
4043 break;
4044 case QUIRK_R3DI:
4045 chipio_set_control_param(codec, 0x0D, 0x21);
4046 r3di_gpio_out_set(codec, R3DI_HEADPHONE_OUT);
4047 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004048 case QUIRK_R3D:
4049 chipio_set_control_param(codec, 0x0D, 0x21);
4050 ca0132_mmio_gpio_set(codec, 0x1, false);
4051 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004052 }
4053
4054 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4055 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4056
4057 /* disable speaker*/
4058 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4059 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4060 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4061 pin_ctl & ~PIN_HP);
4062
4063 /* enable headphone, either front or rear */
4064
4065 if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
4066 headphone_nid = spec->out_pins[2];
4067 else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
4068 headphone_nid = spec->out_pins[1];
4069
4070 pin_ctl = snd_hda_codec_read(codec, headphone_nid, 0,
4071 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4072 snd_hda_set_pin_ctl(codec, headphone_nid,
4073 pin_ctl | PIN_HP);
4074
4075 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4076 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4077 else
4078 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
4079 break;
4080 case SURROUND_OUT:
4081 codec_dbg(codec, "%s surround\n", __func__);
4082 /* Surround out config*/
4083 switch (spec->quirk) {
4084 case QUIRK_SBZ:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004085 ca0132_mmio_gpio_set(codec, 7, false);
4086 ca0132_mmio_gpio_set(codec, 4, true);
4087 ca0132_mmio_gpio_set(codec, 1, true);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004088 chipio_set_control_param(codec, 0x0D, 0x18);
4089 break;
4090 case QUIRK_R3DI:
4091 chipio_set_control_param(codec, 0x0D, 0x24);
4092 r3di_gpio_out_set(codec, R3DI_LINE_OUT);
4093 break;
Connor McAdams42aa3a12018-08-08 13:34:20 -04004094 case QUIRK_R3D:
4095 ca0132_mmio_gpio_set(codec, 1, true);
4096 chipio_set_control_param(codec, 0x0D, 0x24);
4097 break;
Connor McAdams7cb9d942018-05-08 13:20:10 -04004098 }
4099 /* enable line out node */
4100 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4101 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4102 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4103 pin_ctl | PIN_OUT);
4104 /* Disable headphone out */
4105 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4106 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4107 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4108 pin_ctl & ~PIN_HP);
4109 /* Enable EAPD on line out */
4110 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4111 AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4112 /* enable center/lfe out node */
4113 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[2], 0,
4114 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4115 snd_hda_set_pin_ctl(codec, spec->out_pins[2],
4116 pin_ctl | PIN_OUT);
4117 /* Now set rear surround node as out. */
4118 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[3], 0,
4119 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4120 snd_hda_set_pin_ctl(codec, spec->out_pins[3],
4121 pin_ctl | PIN_OUT);
4122
4123 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4124 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4125 else
4126 dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
4127 break;
4128 }
4129
4130 /* run through the output dsp commands for line-out */
4131 for (i = 0; i < alt_out_presets[spec->cur_out_type].commands; i++) {
4132 err = dspio_set_uint_param(codec,
4133 alt_out_presets[spec->cur_out_type].mids[i],
4134 alt_out_presets[spec->cur_out_type].reqs[i],
4135 alt_out_presets[spec->cur_out_type].vals[i]);
4136
4137 if (err < 0)
4138 goto exit;
4139 }
4140
4141exit:
4142 snd_hda_power_down_pm(codec);
4143
4144 return err < 0 ? err : 0;
4145}
4146
Chih-Chung Chang993884f2013-03-25 10:39:23 -07004147static void ca0132_unsol_hp_delayed(struct work_struct *work)
4148{
4149 struct ca0132_spec *spec = container_of(
4150 to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02004151 struct hda_jack_tbl *jack;
4152
Connor McAdams7cb9d942018-05-08 13:20:10 -04004153 if (spec->use_alt_functions)
4154 ca0132_alt_select_out(spec->codec);
4155 else
4156 ca0132_select_out(spec->codec);
4157
Gabriele Martinod5c016b2015-05-18 21:15:13 +02004158 jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02004159 if (jack) {
4160 jack->block_report = 0;
4161 snd_hda_jack_report_sync(spec->codec);
4162 }
Chih-Chung Chang993884f2013-03-25 10:39:23 -07004163}
4164
Ian Minett5aaca442012-12-20 18:53:34 -08004165static void ca0132_set_dmic(struct hda_codec *codec, int enable);
4166static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
4167static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
Connor McAdamse0026d02018-05-08 13:20:12 -04004168static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
4169static int stop_mic1(struct hda_codec *codec);
4170static int ca0132_cvoice_switch_set(struct hda_codec *codec);
Connor McAdams47cdf762018-05-08 13:20:13 -04004171static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
Ian Minett5aaca442012-12-20 18:53:34 -08004172
4173/*
4174 * Select the active VIP source
4175 */
4176static int ca0132_set_vipsource(struct hda_codec *codec, int val)
4177{
4178 struct ca0132_spec *spec = codec->spec;
4179 unsigned int tmp;
4180
Dylan Reide8f1bd52013-03-14 17:27:45 -07004181 if (spec->dsp_state != DSP_DOWNLOADED)
Ian Minett5aaca442012-12-20 18:53:34 -08004182 return 0;
4183
4184 /* if CrystalVoice if off, vipsource should be 0 */
4185 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4186 (val == 0)) {
4187 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4188 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4189 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4190 if (spec->cur_mic_type == DIGITAL_MIC)
4191 tmp = FLOAT_TWO;
4192 else
4193 tmp = FLOAT_ONE;
4194 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4195 tmp = FLOAT_ZERO;
4196 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4197 } else {
4198 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4199 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4200 if (spec->cur_mic_type == DIGITAL_MIC)
4201 tmp = FLOAT_TWO;
4202 else
4203 tmp = FLOAT_ONE;
4204 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4205 tmp = FLOAT_ONE;
4206 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4207 msleep(20);
4208 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4209 }
4210
4211 return 1;
4212}
4213
Connor McAdamse0026d02018-05-08 13:20:12 -04004214static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
4215{
4216 struct ca0132_spec *spec = codec->spec;
4217 unsigned int tmp;
4218
4219 if (spec->dsp_state != DSP_DOWNLOADED)
4220 return 0;
4221
4222 codec_dbg(codec, "%s\n", __func__);
4223
4224 chipio_set_stream_control(codec, 0x03, 0);
4225 chipio_set_stream_control(codec, 0x04, 0);
4226
4227 /* if CrystalVoice is off, vipsource should be 0 */
4228 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4229 (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
4230 codec_dbg(codec, "%s: off.", __func__);
4231 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4232
4233 tmp = FLOAT_ZERO;
4234 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4235
4236 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4237 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4238 if (spec->quirk == QUIRK_R3DI)
4239 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4240
4241
4242 if (spec->in_enum_val == REAR_LINE_IN)
4243 tmp = FLOAT_ZERO;
4244 else {
4245 if (spec->quirk == QUIRK_SBZ)
4246 tmp = FLOAT_THREE;
4247 else
4248 tmp = FLOAT_ONE;
4249 }
4250
4251 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4252
4253 } else {
4254 codec_dbg(codec, "%s: on.", __func__);
4255 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4256 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4257 if (spec->quirk == QUIRK_R3DI)
4258 chipio_set_conn_rate(codec, 0x0F, SR_16_000);
4259
4260 if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
4261 tmp = FLOAT_TWO;
4262 else
4263 tmp = FLOAT_ONE;
4264 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4265
4266 tmp = FLOAT_ONE;
4267 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4268
4269 msleep(20);
4270 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4271 }
4272
4273 chipio_set_stream_control(codec, 0x03, 1);
4274 chipio_set_stream_control(codec, 0x04, 1);
4275
4276 return 1;
4277}
4278
Ian Minett5aaca442012-12-20 18:53:34 -08004279/*
4280 * Select the active microphone.
4281 * If autodetect is enabled, mic will be selected based on jack detection.
4282 * If jack inserted, ext.mic will be selected, else built-in mic
4283 * If autodetect is disabled, mic will be selected based on selection.
4284 */
4285static int ca0132_select_mic(struct hda_codec *codec)
4286{
4287 struct ca0132_spec *spec = codec->spec;
4288 int jack_present;
4289 int auto_jack;
4290
Takashi Iwai4e76a882014-02-25 12:21:03 +01004291 codec_dbg(codec, "ca0132_select_mic\n");
Ian Minett5aaca442012-12-20 18:53:34 -08004292
Takashi Iwai664c7152015-04-08 11:43:14 +02004293 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004294
4295 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
4296
4297 if (auto_jack)
Takashi Iwaife14f392015-08-10 16:53:32 +02004298 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
Ian Minett5aaca442012-12-20 18:53:34 -08004299 else
4300 jack_present =
4301 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
4302
4303 if (jack_present)
4304 spec->cur_mic_type = LINE_MIC_IN;
4305 else
4306 spec->cur_mic_type = DIGITAL_MIC;
4307
4308 if (spec->cur_mic_type == DIGITAL_MIC) {
4309 /* enable digital Mic */
4310 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
4311 ca0132_set_dmic(codec, 1);
4312 ca0132_mic_boost_set(codec, 0);
4313 /* set voice focus */
4314 ca0132_effects_set(codec, VOICE_FOCUS,
4315 spec->effects_switch
4316 [VOICE_FOCUS - EFFECT_START_NID]);
4317 } else {
4318 /* disable digital Mic */
4319 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
4320 ca0132_set_dmic(codec, 0);
4321 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
4322 /* disable voice focus */
4323 ca0132_effects_set(codec, VOICE_FOCUS, 0);
4324 }
4325
Takashi Iwai664c7152015-04-08 11:43:14 +02004326 snd_hda_power_down_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08004327
4328 return 0;
4329}
4330
4331/*
Connor McAdams7cb9d942018-05-08 13:20:10 -04004332 * Select the active input.
4333 * Mic detection isn't used, because it's kind of pointless on the SBZ.
4334 * The front mic has no jack-detection, so the only way to switch to it
4335 * is to do it manually in alsamixer.
4336 */
4337static int ca0132_alt_select_in(struct hda_codec *codec)
4338{
4339 struct ca0132_spec *spec = codec->spec;
4340 unsigned int tmp;
4341
4342 codec_dbg(codec, "%s\n", __func__);
4343
4344 snd_hda_power_up_pm(codec);
4345
4346 chipio_set_stream_control(codec, 0x03, 0);
4347 chipio_set_stream_control(codec, 0x04, 0);
4348
4349 spec->cur_mic_type = spec->in_enum_val;
4350
4351 switch (spec->cur_mic_type) {
4352 case REAR_MIC:
4353 switch (spec->quirk) {
4354 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004355 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004356 ca0132_mmio_gpio_set(codec, 0, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004357 tmp = FLOAT_THREE;
4358 break;
4359 case QUIRK_R3DI:
4360 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
4361 tmp = FLOAT_ONE;
4362 break;
4363 default:
4364 tmp = FLOAT_ONE;
4365 break;
4366 }
4367
4368 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4369 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4370 if (spec->quirk == QUIRK_R3DI)
4371 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4372
4373 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4374
4375 chipio_set_stream_control(codec, 0x03, 1);
4376 chipio_set_stream_control(codec, 0x04, 1);
4377
4378 if (spec->quirk == QUIRK_SBZ) {
4379 chipio_write(codec, 0x18B098, 0x0000000C);
4380 chipio_write(codec, 0x18B09C, 0x0000000C);
4381 }
Connor McAdams47cdf762018-05-08 13:20:13 -04004382 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004383 break;
4384 case REAR_LINE_IN:
4385 ca0132_mic_boost_set(codec, 0);
4386 switch (spec->quirk) {
4387 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004388 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004389 ca0132_mmio_gpio_set(codec, 0, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004390 break;
4391 case QUIRK_R3DI:
4392 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
4393 break;
4394 }
4395
4396 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4397 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4398 if (spec->quirk == QUIRK_R3DI)
4399 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4400
4401 tmp = FLOAT_ZERO;
4402 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4403
4404 if (spec->quirk == QUIRK_SBZ) {
4405 chipio_write(codec, 0x18B098, 0x00000000);
4406 chipio_write(codec, 0x18B09C, 0x00000000);
4407 }
4408
4409 chipio_set_stream_control(codec, 0x03, 1);
4410 chipio_set_stream_control(codec, 0x04, 1);
4411 break;
4412 case FRONT_MIC:
4413 switch (spec->quirk) {
4414 case QUIRK_SBZ:
Connor McAdams42aa3a12018-08-08 13:34:20 -04004415 case QUIRK_R3D:
Connor McAdamsa62e4732018-08-08 13:34:12 -04004416 ca0132_mmio_gpio_set(codec, 0, true);
4417 ca0132_mmio_gpio_set(codec, 5, false);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004418 tmp = FLOAT_THREE;
4419 break;
4420 case QUIRK_R3DI:
4421 r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
4422 tmp = FLOAT_ONE;
4423 break;
4424 default:
4425 tmp = FLOAT_ONE;
4426 break;
4427 }
4428
4429 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4430 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4431 if (spec->quirk == QUIRK_R3DI)
4432 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4433
4434 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4435
4436 chipio_set_stream_control(codec, 0x03, 1);
4437 chipio_set_stream_control(codec, 0x04, 1);
4438
4439 if (spec->quirk == QUIRK_SBZ) {
4440 chipio_write(codec, 0x18B098, 0x0000000C);
4441 chipio_write(codec, 0x18B09C, 0x000000CC);
4442 }
Connor McAdams47cdf762018-05-08 13:20:13 -04004443 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004444 break;
4445 }
Connor McAdamse0026d02018-05-08 13:20:12 -04004446 ca0132_cvoice_switch_set(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04004447
4448 snd_hda_power_down_pm(codec);
4449 return 0;
4450
4451}
4452
4453/*
Ian Minetta7e76272012-12-20 18:53:35 -08004454 * Check if VNODE settings take effect immediately.
4455 */
4456static bool ca0132_is_vnode_effective(struct hda_codec *codec,
4457 hda_nid_t vnid,
4458 hda_nid_t *shared_nid)
4459{
4460 struct ca0132_spec *spec = codec->spec;
4461 hda_nid_t nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004462
4463 switch (vnid) {
4464 case VNID_SPK:
4465 nid = spec->shared_out_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004466 break;
4467 case VNID_MIC:
4468 nid = spec->shared_mic_nid;
Ian Minetta7e76272012-12-20 18:53:35 -08004469 break;
4470 default:
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004471 return false;
Ian Minetta7e76272012-12-20 18:53:35 -08004472 }
4473
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004474 if (shared_nid)
Ian Minetta7e76272012-12-20 18:53:35 -08004475 *shared_nid = nid;
4476
Takashi Iwai9a0869f2013-02-07 12:41:40 +01004477 return true;
Ian Minetta7e76272012-12-20 18:53:35 -08004478}
4479
4480/*
4481* The following functions are control change helpers.
4482* They return 0 if no changed. Return 1 if changed.
4483*/
4484static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
4485{
4486 struct ca0132_spec *spec = codec->spec;
4487 unsigned int tmp;
4488
4489 /* based on CrystalVoice state to enable VoiceFX. */
4490 if (enable) {
4491 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
4492 FLOAT_ONE : FLOAT_ZERO;
4493 } else {
4494 tmp = FLOAT_ZERO;
4495 }
4496
4497 dspio_set_uint_param(codec, ca0132_voicefx.mid,
4498 ca0132_voicefx.reqs[0], tmp);
4499
4500 return 1;
4501}
4502
4503/*
Ian Minett5aaca442012-12-20 18:53:34 -08004504 * Set the effects parameters
4505 */
4506static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
4507{
4508 struct ca0132_spec *spec = codec->spec;
Connor McAdams009b8f92018-05-08 13:20:06 -04004509 unsigned int on, tmp;
Ian Minett5aaca442012-12-20 18:53:34 -08004510 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
4511 int err = 0;
4512 int idx = nid - EFFECT_START_NID;
4513
4514 if ((idx < 0) || (idx >= num_fx))
4515 return 0; /* no changed */
4516
4517 /* for out effect, qualify with PE */
4518 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
4519 /* if PE if off, turn off out effects. */
4520 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
4521 val = 0;
4522 }
4523
4524 /* for in effect, qualify with CrystalVoice */
4525 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
4526 /* if CrystalVoice if off, turn off in effects. */
4527 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
4528 val = 0;
4529
4530 /* Voice Focus applies to 2-ch Mic, Digital Mic */
4531 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
4532 val = 0;
Connor McAdams009b8f92018-05-08 13:20:06 -04004533
4534 /* If Voice Focus on SBZ, set to two channel. */
Connor McAdams7cb9d942018-05-08 13:20:10 -04004535 if ((nid == VOICE_FOCUS) && (spec->quirk == QUIRK_SBZ)
4536 && (spec->cur_mic_type != REAR_LINE_IN)) {
Connor McAdams009b8f92018-05-08 13:20:06 -04004537 if (spec->effects_switch[CRYSTAL_VOICE -
4538 EFFECT_START_NID]) {
4539
4540 if (spec->effects_switch[VOICE_FOCUS -
4541 EFFECT_START_NID]) {
4542 tmp = FLOAT_TWO;
4543 val = 1;
4544 } else
4545 tmp = FLOAT_ONE;
4546
4547 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4548 }
4549 }
4550 /*
4551 * For SBZ noise reduction, there's an extra command
4552 * to module ID 0x47. No clue why.
4553 */
Connor McAdams7cb9d942018-05-08 13:20:10 -04004554 if ((nid == NOISE_REDUCTION) && (spec->quirk == QUIRK_SBZ)
4555 && (spec->cur_mic_type != REAR_LINE_IN)) {
Connor McAdams009b8f92018-05-08 13:20:06 -04004556 if (spec->effects_switch[CRYSTAL_VOICE -
4557 EFFECT_START_NID]) {
4558 if (spec->effects_switch[NOISE_REDUCTION -
4559 EFFECT_START_NID])
4560 tmp = FLOAT_ONE;
4561 else
4562 tmp = FLOAT_ZERO;
4563 } else
4564 tmp = FLOAT_ZERO;
4565
4566 dspio_set_uint_param(codec, 0x47, 0x00, tmp);
4567 }
Connor McAdams7cb9d942018-05-08 13:20:10 -04004568
4569 /* If rear line in disable effects. */
4570 if (spec->use_alt_functions &&
4571 spec->in_enum_val == REAR_LINE_IN)
4572 val = 0;
Ian Minett5aaca442012-12-20 18:53:34 -08004573 }
4574
Takashi Iwai4e76a882014-02-25 12:21:03 +01004575 codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
Ian Minett5aaca442012-12-20 18:53:34 -08004576 nid, val);
4577
4578 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
4579 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
4580 ca0132_effects[idx].reqs[0], on);
4581
4582 if (err < 0)
4583 return 0; /* no changed */
4584
4585 return 1;
4586}
4587
Ian Minetta7e76272012-12-20 18:53:35 -08004588/*
4589 * Turn on/off Playback Enhancements
4590 */
4591static int ca0132_pe_switch_set(struct hda_codec *codec)
4592{
4593 struct ca0132_spec *spec = codec->spec;
4594 hda_nid_t nid;
4595 int i, ret = 0;
4596
Takashi Iwai4e76a882014-02-25 12:21:03 +01004597 codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08004598 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
4599
Connor McAdams7cb9d942018-05-08 13:20:10 -04004600 if (spec->use_alt_functions)
4601 ca0132_alt_select_out(codec);
4602
Ian Minetta7e76272012-12-20 18:53:35 -08004603 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
4604 nid = OUT_EFFECT_START_NID;
4605 /* PE affects all out effects */
4606 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
4607 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
4608
4609 return ret;
4610}
4611
Ian Minett5aaca442012-12-20 18:53:34 -08004612/* Check if Mic1 is streaming, if so, stop streaming */
4613static int stop_mic1(struct hda_codec *codec)
4614{
4615 struct ca0132_spec *spec = codec->spec;
4616 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
4617 AC_VERB_GET_CONV, 0);
4618 if (oldval != 0)
4619 snd_hda_codec_write(codec, spec->adcs[0], 0,
4620 AC_VERB_SET_CHANNEL_STREAMID,
4621 0);
4622 return oldval;
4623}
4624
4625/* Resume Mic1 streaming if it was stopped. */
4626static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
4627{
4628 struct ca0132_spec *spec = codec->spec;
4629 /* Restore the previous stream and channel */
4630 if (oldval != 0)
4631 snd_hda_codec_write(codec, spec->adcs[0], 0,
4632 AC_VERB_SET_CHANNEL_STREAMID,
4633 oldval);
4634}
4635
4636/*
Ian Minetta7e76272012-12-20 18:53:35 -08004637 * Turn on/off CrystalVoice
Ian Minett5aaca442012-12-20 18:53:34 -08004638 */
Ian Minetta7e76272012-12-20 18:53:35 -08004639static int ca0132_cvoice_switch_set(struct hda_codec *codec)
4640{
4641 struct ca0132_spec *spec = codec->spec;
4642 hda_nid_t nid;
4643 int i, ret = 0;
4644 unsigned int oldval;
4645
Takashi Iwai4e76a882014-02-25 12:21:03 +01004646 codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08004647 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
4648
4649 i = IN_EFFECT_START_NID - EFFECT_START_NID;
4650 nid = IN_EFFECT_START_NID;
4651 /* CrystalVoice affects all in effects */
4652 for (; nid < IN_EFFECT_END_NID; nid++, i++)
4653 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
4654
4655 /* including VoiceFX */
4656 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
4657
4658 /* set correct vipsource */
4659 oldval = stop_mic1(codec);
Connor McAdamse0026d02018-05-08 13:20:12 -04004660 if (spec->use_alt_functions)
4661 ret |= ca0132_alt_set_vipsource(codec, 1);
4662 else
4663 ret |= ca0132_set_vipsource(codec, 1);
Ian Minetta7e76272012-12-20 18:53:35 -08004664 resume_mic1(codec, oldval);
4665 return ret;
4666}
4667
Ian Minett5aaca442012-12-20 18:53:34 -08004668static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
4669{
4670 struct ca0132_spec *spec = codec->spec;
4671 int ret = 0;
4672
4673 if (val) /* on */
4674 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4675 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
4676 else /* off */
4677 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4678 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
4679
4680 return ret;
4681}
4682
Connor McAdams47cdf762018-05-08 13:20:13 -04004683static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
4684{
4685 struct ca0132_spec *spec = codec->spec;
4686 int ret = 0;
4687
4688 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
4689 HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
4690 return ret;
4691}
4692
Ian Minetta7e76272012-12-20 18:53:35 -08004693static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
4694 struct snd_ctl_elem_value *ucontrol)
4695{
4696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4697 hda_nid_t nid = get_amp_nid(kcontrol);
4698 hda_nid_t shared_nid = 0;
4699 bool effective;
4700 int ret = 0;
4701 struct ca0132_spec *spec = codec->spec;
4702 int auto_jack;
4703
4704 if (nid == VNID_HP_SEL) {
4705 auto_jack =
4706 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
Connor McAdams7cb9d942018-05-08 13:20:10 -04004707 if (!auto_jack) {
4708 if (spec->use_alt_functions)
4709 ca0132_alt_select_out(codec);
4710 else
4711 ca0132_select_out(codec);
4712 }
Ian Minetta7e76272012-12-20 18:53:35 -08004713 return 1;
4714 }
4715
4716 if (nid == VNID_AMIC1_SEL) {
4717 auto_jack =
4718 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
4719 if (!auto_jack)
4720 ca0132_select_mic(codec);
4721 return 1;
4722 }
4723
4724 if (nid == VNID_HP_ASEL) {
Connor McAdams7cb9d942018-05-08 13:20:10 -04004725 if (spec->use_alt_functions)
4726 ca0132_alt_select_out(codec);
4727 else
4728 ca0132_select_out(codec);
Ian Minetta7e76272012-12-20 18:53:35 -08004729 return 1;
4730 }
4731
4732 if (nid == VNID_AMIC1_ASEL) {
4733 ca0132_select_mic(codec);
4734 return 1;
4735 }
4736
4737 /* if effective conditions, then update hw immediately. */
4738 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
4739 if (effective) {
4740 int dir = get_amp_direction(kcontrol);
4741 int ch = get_amp_channels(kcontrol);
4742 unsigned long pval;
4743
4744 mutex_lock(&codec->control_mutex);
4745 pval = kcontrol->private_value;
4746 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
4747 0, dir);
4748 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
4749 kcontrol->private_value = pval;
4750 mutex_unlock(&codec->control_mutex);
4751 }
4752
4753 return ret;
4754}
4755/* End of control change helpers. */
Connor McAdams47cdf762018-05-08 13:20:13 -04004756/*
4757 * Below I've added controls to mess with the effect levels, I've only enabled
4758 * them on the Sound Blaster Z, but they would probably also work on the
4759 * Chromebook. I figured they were probably tuned specifically for it, and left
4760 * out for a reason.
4761 */
4762
4763/* Sets DSP effect level from the sliders above the controls */
4764static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
4765 const unsigned int *lookup, int idx)
4766{
4767 int i = 0;
4768 unsigned int y;
4769 /*
4770 * For X_BASS, req 2 is actually crossover freq instead of
4771 * effect level
4772 */
4773 if (nid == X_BASS)
4774 y = 2;
4775 else
4776 y = 1;
4777
4778 snd_hda_power_up(codec);
4779 if (nid == XBASS_XOVER) {
4780 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
4781 if (ca0132_effects[i].nid == X_BASS)
4782 break;
4783
4784 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
4785 ca0132_effects[i].reqs[1],
4786 &(lookup[idx - 1]), sizeof(unsigned int));
4787 } else {
4788 /* Find the actual effect structure */
4789 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
4790 if (nid == ca0132_effects[i].nid)
4791 break;
4792
4793 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
4794 ca0132_effects[i].reqs[y],
4795 &(lookup[idx]), sizeof(unsigned int));
4796 }
4797
4798 snd_hda_power_down(codec);
4799
4800 return 0;
4801}
4802
4803static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
4804 struct snd_ctl_elem_value *ucontrol)
4805{
4806 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4807 struct ca0132_spec *spec = codec->spec;
4808 long *valp = ucontrol->value.integer.value;
4809
4810 *valp = spec->xbass_xover_freq;
4811 return 0;
4812}
4813
4814static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
4815 struct snd_ctl_elem_value *ucontrol)
4816{
4817 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4818 struct ca0132_spec *spec = codec->spec;
4819 hda_nid_t nid = get_amp_nid(kcontrol);
4820 long *valp = ucontrol->value.integer.value;
4821 int idx = nid - OUT_EFFECT_START_NID;
4822
4823 *valp = spec->fx_ctl_val[idx];
4824 return 0;
4825}
4826
4827/*
4828 * The X-bass crossover starts at 10hz, so the min is 1. The
4829 * frequency is set in multiples of 10.
4830 */
4831static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
4832 struct snd_ctl_elem_info *uinfo)
4833{
4834 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4835 uinfo->count = 1;
4836 uinfo->value.integer.min = 1;
4837 uinfo->value.integer.max = 100;
4838 uinfo->value.integer.step = 1;
4839
4840 return 0;
4841}
4842
4843static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
4844 struct snd_ctl_elem_info *uinfo)
4845{
4846 int chs = get_amp_channels(kcontrol);
4847
4848 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4849 uinfo->count = chs == 3 ? 2 : 1;
4850 uinfo->value.integer.min = 0;
4851 uinfo->value.integer.max = 100;
4852 uinfo->value.integer.step = 1;
4853
4854 return 0;
4855}
4856
4857static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
4858 struct snd_ctl_elem_value *ucontrol)
4859{
4860 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4861 struct ca0132_spec *spec = codec->spec;
4862 hda_nid_t nid = get_amp_nid(kcontrol);
4863 long *valp = ucontrol->value.integer.value;
4864 int idx;
4865
4866 /* any change? */
4867 if (spec->xbass_xover_freq == *valp)
4868 return 0;
4869
4870 spec->xbass_xover_freq = *valp;
4871
4872 idx = *valp;
4873 ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
4874
4875 return 0;
4876}
4877
4878static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
4879 struct snd_ctl_elem_value *ucontrol)
4880{
4881 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4882 struct ca0132_spec *spec = codec->spec;
4883 hda_nid_t nid = get_amp_nid(kcontrol);
4884 long *valp = ucontrol->value.integer.value;
4885 int idx;
4886
4887 idx = nid - EFFECT_START_NID;
4888 /* any change? */
4889 if (spec->fx_ctl_val[idx] == *valp)
4890 return 0;
4891
4892 spec->fx_ctl_val[idx] = *valp;
4893
4894 idx = *valp;
4895 ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
4896
4897 return 0;
4898}
4899
4900
4901/*
4902 * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
4903 * only has off or full 30 dB, and didn't like making a volume slider that has
4904 * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
4905 */
4906#define MIC_BOOST_NUM_OF_STEPS 4
4907#define MIC_BOOST_ENUM_MAX_STRLEN 10
4908
4909static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
4910 struct snd_ctl_elem_info *uinfo)
4911{
4912 char *sfx = "dB";
4913 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
4914
4915 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4916 uinfo->count = 1;
4917 uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
4918 if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
4919 uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
4920 sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
4921 strcpy(uinfo->value.enumerated.name, namestr);
4922 return 0;
4923}
4924
4925static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
4926 struct snd_ctl_elem_value *ucontrol)
4927{
4928 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4929 struct ca0132_spec *spec = codec->spec;
4930
4931 ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
4932 return 0;
4933}
4934
4935static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
4936 struct snd_ctl_elem_value *ucontrol)
4937{
4938 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4939 struct ca0132_spec *spec = codec->spec;
4940 int sel = ucontrol->value.enumerated.item[0];
4941 unsigned int items = MIC_BOOST_NUM_OF_STEPS;
4942
4943 if (sel >= items)
4944 return 0;
4945
4946 codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
4947 sel);
4948
4949 spec->mic_boost_enum_val = sel;
4950
4951 if (spec->in_enum_val != REAR_LINE_IN)
4952 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
4953
4954 return 1;
4955}
4956
Ian Minetta7e76272012-12-20 18:53:35 -08004957
Connor McAdams7cb9d942018-05-08 13:20:10 -04004958/*
4959 * Input Select Control for alternative ca0132 codecs. This exists because
4960 * front microphone has no auto-detect, and we need a way to set the rear
4961 * as line-in
4962 */
4963static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
4964 struct snd_ctl_elem_info *uinfo)
4965{
4966 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4967 uinfo->count = 1;
4968 uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
4969 if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
4970 uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
4971 strcpy(uinfo->value.enumerated.name,
4972 in_src_str[uinfo->value.enumerated.item]);
4973 return 0;
4974}
4975
4976static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
4977 struct snd_ctl_elem_value *ucontrol)
4978{
4979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4980 struct ca0132_spec *spec = codec->spec;
4981
4982 ucontrol->value.enumerated.item[0] = spec->in_enum_val;
4983 return 0;
4984}
4985
4986static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
4987 struct snd_ctl_elem_value *ucontrol)
4988{
4989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4990 struct ca0132_spec *spec = codec->spec;
4991 int sel = ucontrol->value.enumerated.item[0];
4992 unsigned int items = IN_SRC_NUM_OF_INPUTS;
4993
4994 if (sel >= items)
4995 return 0;
4996
4997 codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
4998 sel, in_src_str[sel]);
4999
5000 spec->in_enum_val = sel;
5001
5002 ca0132_alt_select_in(codec);
5003
5004 return 1;
5005}
5006
5007/* Sound Blaster Z Output Select Control */
5008static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
5009 struct snd_ctl_elem_info *uinfo)
5010{
5011 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5012 uinfo->count = 1;
5013 uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
5014 if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
5015 uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
5016 strcpy(uinfo->value.enumerated.name,
5017 alt_out_presets[uinfo->value.enumerated.item].name);
5018 return 0;
5019}
5020
5021static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
5022 struct snd_ctl_elem_value *ucontrol)
5023{
5024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5025 struct ca0132_spec *spec = codec->spec;
5026
5027 ucontrol->value.enumerated.item[0] = spec->out_enum_val;
5028 return 0;
5029}
5030
5031static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
5032 struct snd_ctl_elem_value *ucontrol)
5033{
5034 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5035 struct ca0132_spec *spec = codec->spec;
5036 int sel = ucontrol->value.enumerated.item[0];
5037 unsigned int items = NUM_OF_OUTPUTS;
5038 unsigned int auto_jack;
5039
5040 if (sel >= items)
5041 return 0;
5042
5043 codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
5044 sel, alt_out_presets[sel].name);
5045
5046 spec->out_enum_val = sel;
5047
5048 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
5049
5050 if (!auto_jack)
5051 ca0132_alt_select_out(codec);
5052
5053 return 1;
5054}
5055
Connor McAdams47cdf762018-05-08 13:20:13 -04005056/*
5057 * Smart Volume output setting control. Three different settings, Normal,
5058 * which takes the value from the smart volume slider. The two others, loud
5059 * and night, disregard the slider value and have uneditable values.
5060 */
5061#define NUM_OF_SVM_SETTINGS 3
Takashi Sakamoto3a03f832018-05-15 22:12:58 +09005062static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
Connor McAdams47cdf762018-05-08 13:20:13 -04005063
5064static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
5065 struct snd_ctl_elem_info *uinfo)
5066{
5067 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5068 uinfo->count = 1;
5069 uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
5070 if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
5071 uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
5072 strcpy(uinfo->value.enumerated.name,
5073 out_svm_set_enum_str[uinfo->value.enumerated.item]);
5074 return 0;
5075}
5076
5077static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
5078 struct snd_ctl_elem_value *ucontrol)
5079{
5080 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5081 struct ca0132_spec *spec = codec->spec;
5082
5083 ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
5084 return 0;
5085}
5086
5087static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
5088 struct snd_ctl_elem_value *ucontrol)
5089{
5090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5091 struct ca0132_spec *spec = codec->spec;
5092 int sel = ucontrol->value.enumerated.item[0];
5093 unsigned int items = NUM_OF_SVM_SETTINGS;
5094 unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
5095 unsigned int tmp;
5096
5097 if (sel >= items)
5098 return 0;
5099
5100 codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
5101 sel, out_svm_set_enum_str[sel]);
5102
5103 spec->smart_volume_setting = sel;
5104
5105 switch (sel) {
5106 case 0:
5107 tmp = FLOAT_ZERO;
5108 break;
5109 case 1:
5110 tmp = FLOAT_ONE;
5111 break;
5112 case 2:
5113 tmp = FLOAT_TWO;
5114 break;
5115 default:
5116 tmp = FLOAT_ZERO;
5117 break;
5118 }
5119 /* Req 2 is the Smart Volume Setting req. */
5120 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
5121 ca0132_effects[idx].reqs[2], tmp);
5122 return 1;
5123}
5124
5125/* Sound Blaster Z EQ preset controls */
5126static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
5127 struct snd_ctl_elem_info *uinfo)
5128{
Fengguang Wuc5f13d72018-05-15 03:02:14 +08005129 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
Connor McAdams47cdf762018-05-08 13:20:13 -04005130
5131 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5132 uinfo->count = 1;
5133 uinfo->value.enumerated.items = items;
5134 if (uinfo->value.enumerated.item >= items)
5135 uinfo->value.enumerated.item = items - 1;
5136 strcpy(uinfo->value.enumerated.name,
5137 ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
5138 return 0;
5139}
5140
5141static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
5142 struct snd_ctl_elem_value *ucontrol)
5143{
5144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5145 struct ca0132_spec *spec = codec->spec;
5146
5147 ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
5148 return 0;
5149}
5150
5151static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
5152 struct snd_ctl_elem_value *ucontrol)
5153{
5154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5155 struct ca0132_spec *spec = codec->spec;
5156 int i, err = 0;
5157 int sel = ucontrol->value.enumerated.item[0];
Fengguang Wuc5f13d72018-05-15 03:02:14 +08005158 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
Connor McAdams47cdf762018-05-08 13:20:13 -04005159
5160 if (sel >= items)
5161 return 0;
5162
5163 codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
5164 ca0132_alt_eq_presets[sel].name);
5165 /*
5166 * Idx 0 is default.
5167 * Default needs to qualify with CrystalVoice state.
5168 */
5169 for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
5170 err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
5171 ca0132_alt_eq_enum.reqs[i],
5172 ca0132_alt_eq_presets[sel].vals[i]);
5173 if (err < 0)
5174 break;
5175 }
5176
5177 if (err >= 0)
5178 spec->eq_preset_val = sel;
5179
5180 return 1;
5181}
5182
Ian Minetta7e76272012-12-20 18:53:35 -08005183static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
5184 struct snd_ctl_elem_info *uinfo)
5185{
Jérémy Lefaurea9291f42017-10-12 22:36:31 -04005186 unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
Ian Minetta7e76272012-12-20 18:53:35 -08005187
5188 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5189 uinfo->count = 1;
5190 uinfo->value.enumerated.items = items;
5191 if (uinfo->value.enumerated.item >= items)
5192 uinfo->value.enumerated.item = items - 1;
5193 strcpy(uinfo->value.enumerated.name,
5194 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
5195 return 0;
5196}
5197
5198static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
5199 struct snd_ctl_elem_value *ucontrol)
5200{
5201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5202 struct ca0132_spec *spec = codec->spec;
5203
5204 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
5205 return 0;
5206}
5207
5208static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
5209 struct snd_ctl_elem_value *ucontrol)
5210{
5211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5212 struct ca0132_spec *spec = codec->spec;
5213 int i, err = 0;
5214 int sel = ucontrol->value.enumerated.item[0];
Ian Minetta7e76272012-12-20 18:53:35 -08005215
Jérémy Lefaurea9291f42017-10-12 22:36:31 -04005216 if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
Ian Minetta7e76272012-12-20 18:53:35 -08005217 return 0;
5218
Takashi Iwai4e76a882014-02-25 12:21:03 +01005219 codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
Ian Minetta7e76272012-12-20 18:53:35 -08005220 sel, ca0132_voicefx_presets[sel].name);
5221
5222 /*
5223 * Idx 0 is default.
5224 * Default needs to qualify with CrystalVoice state.
5225 */
5226 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
5227 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
5228 ca0132_voicefx.reqs[i],
5229 ca0132_voicefx_presets[sel].vals[i]);
5230 if (err < 0)
5231 break;
5232 }
5233
5234 if (err >= 0) {
5235 spec->voicefx_val = sel;
5236 /* enable voice fx */
5237 ca0132_voicefx_set(codec, (sel ? 1 : 0));
5238 }
5239
5240 return 1;
5241}
5242
5243static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
5244 struct snd_ctl_elem_value *ucontrol)
5245{
5246 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5247 struct ca0132_spec *spec = codec->spec;
5248 hda_nid_t nid = get_amp_nid(kcontrol);
5249 int ch = get_amp_channels(kcontrol);
5250 long *valp = ucontrol->value.integer.value;
5251
5252 /* vnode */
5253 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
5254 if (ch & 1) {
5255 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
5256 valp++;
5257 }
5258 if (ch & 2) {
5259 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
5260 valp++;
5261 }
5262 return 0;
5263 }
5264
5265 /* effects, include PE and CrystalVoice */
5266 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
5267 *valp = spec->effects_switch[nid - EFFECT_START_NID];
5268 return 0;
5269 }
5270
5271 /* mic boost */
5272 if (nid == spec->input_pins[0]) {
5273 *valp = spec->cur_mic_boost;
5274 return 0;
5275 }
5276
5277 return 0;
5278}
5279
5280static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
5281 struct snd_ctl_elem_value *ucontrol)
5282{
5283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5284 struct ca0132_spec *spec = codec->spec;
5285 hda_nid_t nid = get_amp_nid(kcontrol);
5286 int ch = get_amp_channels(kcontrol);
5287 long *valp = ucontrol->value.integer.value;
5288 int changed = 1;
5289
Takashi Iwai4e76a882014-02-25 12:21:03 +01005290 codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
Ian Minetta7e76272012-12-20 18:53:35 -08005291 nid, *valp);
5292
5293 snd_hda_power_up(codec);
5294 /* vnode */
5295 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
5296 if (ch & 1) {
5297 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
5298 valp++;
5299 }
5300 if (ch & 2) {
5301 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
5302 valp++;
5303 }
5304 changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
5305 goto exit;
5306 }
5307
5308 /* PE */
5309 if (nid == PLAY_ENHANCEMENT) {
5310 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5311 changed = ca0132_pe_switch_set(codec);
5312 goto exit;
5313 }
5314
5315 /* CrystalVoice */
5316 if (nid == CRYSTAL_VOICE) {
5317 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5318 changed = ca0132_cvoice_switch_set(codec);
5319 goto exit;
5320 }
5321
5322 /* out and in effects */
5323 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
5324 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
5325 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
5326 changed = ca0132_effects_set(codec, nid, *valp);
5327 goto exit;
5328 }
5329
5330 /* mic boost */
5331 if (nid == spec->input_pins[0]) {
5332 spec->cur_mic_boost = *valp;
Connor McAdams7cb9d942018-05-08 13:20:10 -04005333 if (spec->use_alt_functions) {
5334 if (spec->in_enum_val != REAR_LINE_IN)
5335 changed = ca0132_mic_boost_set(codec, *valp);
5336 } else {
5337 /* Mic boost does not apply to Digital Mic */
5338 if (spec->cur_mic_type != DIGITAL_MIC)
5339 changed = ca0132_mic_boost_set(codec, *valp);
5340 }
Ian Minetta7e76272012-12-20 18:53:35 -08005341
Ian Minetta7e76272012-12-20 18:53:35 -08005342 goto exit;
5343 }
5344
5345exit:
5346 snd_hda_power_down(codec);
5347 return changed;
5348}
5349
5350/*
5351 * Volume related
5352 */
Connor McAdams017310f2018-05-08 13:20:11 -04005353/*
5354 * Sets the internal DSP decibel level to match the DAC for output, and the
5355 * ADC for input. Currently only the SBZ sets dsp capture volume level, and
5356 * all alternative codecs set DSP playback volume.
5357 */
5358static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
5359{
5360 struct ca0132_spec *spec = codec->spec;
5361 unsigned int dsp_dir;
5362 unsigned int lookup_val;
5363
5364 if (nid == VNID_SPK)
5365 dsp_dir = DSP_VOL_OUT;
5366 else
5367 dsp_dir = DSP_VOL_IN;
5368
5369 lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
5370
5371 dspio_set_uint_param(codec,
5372 ca0132_alt_vol_ctls[dsp_dir].mid,
5373 ca0132_alt_vol_ctls[dsp_dir].reqs[0],
5374 float_vol_db_lookup[lookup_val]);
5375
5376 lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
5377
5378 dspio_set_uint_param(codec,
5379 ca0132_alt_vol_ctls[dsp_dir].mid,
5380 ca0132_alt_vol_ctls[dsp_dir].reqs[1],
5381 float_vol_db_lookup[lookup_val]);
5382
5383 dspio_set_uint_param(codec,
5384 ca0132_alt_vol_ctls[dsp_dir].mid,
5385 ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
5386}
5387
Ian Minetta7e76272012-12-20 18:53:35 -08005388static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
5389 struct snd_ctl_elem_info *uinfo)
5390{
5391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5392 struct ca0132_spec *spec = codec->spec;
5393 hda_nid_t nid = get_amp_nid(kcontrol);
5394 int ch = get_amp_channels(kcontrol);
5395 int dir = get_amp_direction(kcontrol);
5396 unsigned long pval;
5397 int err;
5398
5399 switch (nid) {
5400 case VNID_SPK:
5401 /* follow shared_out info */
5402 nid = spec->shared_out_nid;
5403 mutex_lock(&codec->control_mutex);
5404 pval = kcontrol->private_value;
5405 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5406 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5407 kcontrol->private_value = pval;
5408 mutex_unlock(&codec->control_mutex);
5409 break;
5410 case VNID_MIC:
5411 /* follow shared_mic info */
5412 nid = spec->shared_mic_nid;
5413 mutex_lock(&codec->control_mutex);
5414 pval = kcontrol->private_value;
5415 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5416 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5417 kcontrol->private_value = pval;
5418 mutex_unlock(&codec->control_mutex);
5419 break;
5420 default:
5421 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
5422 }
5423 return err;
5424}
5425
5426static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
5427 struct snd_ctl_elem_value *ucontrol)
5428{
5429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5430 struct ca0132_spec *spec = codec->spec;
5431 hda_nid_t nid = get_amp_nid(kcontrol);
5432 int ch = get_amp_channels(kcontrol);
5433 long *valp = ucontrol->value.integer.value;
5434
5435 /* store the left and right volume */
5436 if (ch & 1) {
5437 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
5438 valp++;
5439 }
5440 if (ch & 2) {
5441 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
5442 valp++;
5443 }
5444 return 0;
5445}
5446
5447static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
5448 struct snd_ctl_elem_value *ucontrol)
5449{
5450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5451 struct ca0132_spec *spec = codec->spec;
5452 hda_nid_t nid = get_amp_nid(kcontrol);
5453 int ch = get_amp_channels(kcontrol);
5454 long *valp = ucontrol->value.integer.value;
5455 hda_nid_t shared_nid = 0;
5456 bool effective;
5457 int changed = 1;
5458
5459 /* store the left and right volume */
5460 if (ch & 1) {
5461 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
5462 valp++;
5463 }
5464 if (ch & 2) {
5465 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
5466 valp++;
5467 }
5468
5469 /* if effective conditions, then update hw immediately. */
5470 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
5471 if (effective) {
5472 int dir = get_amp_direction(kcontrol);
5473 unsigned long pval;
5474
5475 snd_hda_power_up(codec);
5476 mutex_lock(&codec->control_mutex);
5477 pval = kcontrol->private_value;
5478 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
5479 0, dir);
5480 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
5481 kcontrol->private_value = pval;
5482 mutex_unlock(&codec->control_mutex);
5483 snd_hda_power_down(codec);
5484 }
5485
5486 return changed;
5487}
5488
Connor McAdams017310f2018-05-08 13:20:11 -04005489/*
5490 * This function is the same as the one above, because using an if statement
5491 * inside of the above volume control for the DSP volume would cause too much
5492 * lag. This is a lot more smooth.
5493 */
5494static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
5495 struct snd_ctl_elem_value *ucontrol)
5496{
5497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5498 struct ca0132_spec *spec = codec->spec;
5499 hda_nid_t nid = get_amp_nid(kcontrol);
5500 int ch = get_amp_channels(kcontrol);
5501 long *valp = ucontrol->value.integer.value;
5502 hda_nid_t vnid = 0;
5503 int changed = 1;
5504
5505 switch (nid) {
5506 case 0x02:
5507 vnid = VNID_SPK;
5508 break;
5509 case 0x07:
5510 vnid = VNID_MIC;
5511 break;
5512 }
5513
5514 /* store the left and right volume */
5515 if (ch & 1) {
5516 spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
5517 valp++;
5518 }
5519 if (ch & 2) {
5520 spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
5521 valp++;
5522 }
5523
5524 snd_hda_power_up(codec);
5525 ca0132_alt_dsp_volume_put(codec, vnid);
5526 mutex_lock(&codec->control_mutex);
5527 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
5528 mutex_unlock(&codec->control_mutex);
5529 snd_hda_power_down(codec);
5530
5531 return changed;
5532}
5533
Ian Minetta7e76272012-12-20 18:53:35 -08005534static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
5535 unsigned int size, unsigned int __user *tlv)
5536{
5537 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5538 struct ca0132_spec *spec = codec->spec;
5539 hda_nid_t nid = get_amp_nid(kcontrol);
5540 int ch = get_amp_channels(kcontrol);
5541 int dir = get_amp_direction(kcontrol);
5542 unsigned long pval;
5543 int err;
5544
5545 switch (nid) {
5546 case VNID_SPK:
5547 /* follow shared_out tlv */
5548 nid = spec->shared_out_nid;
5549 mutex_lock(&codec->control_mutex);
5550 pval = kcontrol->private_value;
5551 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5552 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5553 kcontrol->private_value = pval;
5554 mutex_unlock(&codec->control_mutex);
5555 break;
5556 case VNID_MIC:
5557 /* follow shared_mic tlv */
5558 nid = spec->shared_mic_nid;
5559 mutex_lock(&codec->control_mutex);
5560 pval = kcontrol->private_value;
5561 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
5562 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5563 kcontrol->private_value = pval;
5564 mutex_unlock(&codec->control_mutex);
5565 break;
5566 default:
5567 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
5568 }
5569 return err;
5570}
5571
Connor McAdams47cdf762018-05-08 13:20:13 -04005572/* Add volume slider control for effect level */
5573static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
5574 const char *pfx, int dir)
5575{
Connor McAdams47cdf762018-05-08 13:20:13 -04005576 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5577 int type = dir ? HDA_INPUT : HDA_OUTPUT;
5578 struct snd_kcontrol_new knew =
5579 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
5580
Takashi Sakamoto0cc1aa72018-05-15 22:12:59 +09005581 sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
Connor McAdams47cdf762018-05-08 13:20:13 -04005582
Takashi Iwaibb86124c2018-07-25 23:00:49 +02005583 knew.tlv.c = NULL;
Connor McAdams47cdf762018-05-08 13:20:13 -04005584
5585 switch (nid) {
5586 case XBASS_XOVER:
5587 knew.info = ca0132_alt_xbass_xover_slider_info;
5588 knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
5589 knew.put = ca0132_alt_xbass_xover_slider_put;
5590 break;
5591 default:
5592 knew.info = ca0132_alt_effect_slider_info;
5593 knew.get = ca0132_alt_slider_ctl_get;
5594 knew.put = ca0132_alt_effect_slider_put;
5595 knew.private_value =
5596 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
5597 break;
5598 }
5599
5600 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
5601}
5602
5603/*
5604 * Added FX: prefix for the alternative codecs, because otherwise the surround
5605 * effect would conflict with the Surround sound volume control. Also seems more
5606 * clear as to what the switches do. Left alone for others.
5607 */
Ian Minetta7e76272012-12-20 18:53:35 -08005608static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
5609 const char *pfx, int dir)
5610{
Connor McAdams47cdf762018-05-08 13:20:13 -04005611 struct ca0132_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02005612 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Ian Minetta7e76272012-12-20 18:53:35 -08005613 int type = dir ? HDA_INPUT : HDA_OUTPUT;
5614 struct snd_kcontrol_new knew =
5615 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
Connor McAdams47cdf762018-05-08 13:20:13 -04005616 /* If using alt_controls, add FX: prefix. But, don't add FX:
5617 * prefix to OutFX or InFX enable controls.
5618 */
5619 if ((spec->use_alt_controls) && (nid <= IN_EFFECT_END_NID))
Takashi Sakamoto0cc1aa72018-05-15 22:12:59 +09005620 sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
Connor McAdams47cdf762018-05-08 13:20:13 -04005621 else
5622 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
5623
Ian Minetta7e76272012-12-20 18:53:35 -08005624 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
5625}
5626
5627static int add_voicefx(struct hda_codec *codec)
5628{
5629 struct snd_kcontrol_new knew =
5630 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
5631 VOICEFX, 1, 0, HDA_INPUT);
5632 knew.info = ca0132_voicefx_info;
5633 knew.get = ca0132_voicefx_get;
5634 knew.put = ca0132_voicefx_put;
5635 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
5636}
5637
Connor McAdams47cdf762018-05-08 13:20:13 -04005638/* Create the EQ Preset control */
5639static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
5640{
5641 struct snd_kcontrol_new knew =
5642 HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
5643 EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
5644 knew.info = ca0132_alt_eq_preset_info;
5645 knew.get = ca0132_alt_eq_preset_get;
5646 knew.put = ca0132_alt_eq_preset_put;
5647 return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
5648 snd_ctl_new1(&knew, codec));
5649}
5650
5651/*
5652 * Add enumerated control for the three different settings of the smart volume
5653 * output effect. Normal just uses the slider value, and loud and night are
5654 * their own things that ignore that value.
5655 */
5656static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
5657{
5658 struct snd_kcontrol_new knew =
5659 HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
5660 SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
5661 knew.info = ca0132_alt_svm_setting_info;
5662 knew.get = ca0132_alt_svm_setting_get;
5663 knew.put = ca0132_alt_svm_setting_put;
5664 return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
5665 snd_ctl_new1(&knew, codec));
5666
5667}
5668
Ian Minetta7e76272012-12-20 18:53:35 -08005669/*
Connor McAdams7cb9d942018-05-08 13:20:10 -04005670 * Create an Output Select enumerated control for codecs with surround
5671 * out capabilities.
5672 */
5673static int ca0132_alt_add_output_enum(struct hda_codec *codec)
5674{
5675 struct snd_kcontrol_new knew =
5676 HDA_CODEC_MUTE_MONO("Output Select",
5677 OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
5678 knew.info = ca0132_alt_output_select_get_info;
5679 knew.get = ca0132_alt_output_select_get;
5680 knew.put = ca0132_alt_output_select_put;
5681 return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
5682 snd_ctl_new1(&knew, codec));
5683}
5684
5685/*
5686 * Create an Input Source enumerated control for the alternate ca0132 codecs
5687 * because the front microphone has no auto-detect, and Line-in has to be set
5688 * somehow.
5689 */
5690static int ca0132_alt_add_input_enum(struct hda_codec *codec)
5691{
5692 struct snd_kcontrol_new knew =
5693 HDA_CODEC_MUTE_MONO("Input Source",
5694 INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
5695 knew.info = ca0132_alt_input_source_info;
5696 knew.get = ca0132_alt_input_source_get;
5697 knew.put = ca0132_alt_input_source_put;
5698 return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
5699 snd_ctl_new1(&knew, codec));
5700}
5701
5702/*
Connor McAdams47cdf762018-05-08 13:20:13 -04005703 * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
5704 * more control than the original mic boost, which is either full 30dB or off.
5705 */
5706static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
5707{
5708 struct snd_kcontrol_new knew =
5709 HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
5710 MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
5711 knew.info = ca0132_alt_mic_boost_info;
5712 knew.get = ca0132_alt_mic_boost_get;
5713 knew.put = ca0132_alt_mic_boost_put;
5714 return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
5715 snd_ctl_new1(&knew, codec));
5716
5717}
5718
5719/*
5720 * Need to create slave controls for the alternate codecs that have surround
5721 * capabilities.
5722 */
5723static const char * const ca0132_alt_slave_pfxs[] = {
5724 "Front", "Surround", "Center", "LFE", NULL,
5725};
5726
5727/*
5728 * Also need special channel map, because the default one is incorrect.
5729 * I think this has to do with the pin for rear surround being 0x11,
5730 * and the center/lfe being 0x10. Usually the pin order is the opposite.
5731 */
Colin Ian King9c4a6652018-06-21 19:34:57 +01005732static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
Connor McAdams47cdf762018-05-08 13:20:13 -04005733 { .channels = 2,
5734 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
5735 { .channels = 4,
5736 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
5737 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
5738 { .channels = 6,
5739 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
5740 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
5741 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
5742 { }
5743};
5744
5745/* Add the correct chmap for streams with 6 channels. */
5746static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
5747{
5748 int err = 0;
5749 struct hda_pcm *pcm;
5750
5751 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
5752 struct hda_pcm_stream *hinfo =
5753 &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
5754 struct snd_pcm_chmap *chmap;
5755 const struct snd_pcm_chmap_elem *elem;
5756
5757 elem = ca0132_alt_chmaps;
5758 if (hinfo->channels_max == 6) {
5759 err = snd_pcm_add_chmap_ctls(pcm->pcm,
5760 SNDRV_PCM_STREAM_PLAYBACK,
5761 elem, hinfo->channels_max, 0, &chmap);
5762 if (err < 0)
5763 codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
5764 }
5765 }
5766}
5767
5768/*
Ian Minetta7e76272012-12-20 18:53:35 -08005769 * When changing Node IDs for Mixer Controls below, make sure to update
5770 * Node IDs in ca0132_config() as well.
5771 */
Takashi Sakamotob0eaa072018-05-15 22:12:57 +09005772static const struct snd_kcontrol_new ca0132_mixer[] = {
Ian Minetta7e76272012-12-20 18:53:35 -08005773 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
5774 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
5775 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
5776 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5777 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
5778 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
5779 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5780 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5781 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
5782 0x12, 1, HDA_INPUT),
5783 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
5784 VNID_HP_SEL, 1, HDA_OUTPUT),
5785 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
5786 VNID_AMIC1_SEL, 1, HDA_INPUT),
5787 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5788 VNID_HP_ASEL, 1, HDA_OUTPUT),
5789 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
5790 VNID_AMIC1_ASEL, 1, HDA_INPUT),
5791 { } /* end */
5792};
5793
Connor McAdams017310f2018-05-08 13:20:11 -04005794/*
Connor McAdamse25e3442018-08-08 13:34:21 -04005795 * Desktop specific control mixer. Removes auto-detect for mic, and adds
5796 * surround controls. Also sets both the Front Playback and Capture Volume
5797 * controls to alt so they set the DSP's decibel level.
Connor McAdams017310f2018-05-08 13:20:11 -04005798 */
Connor McAdamse25e3442018-08-08 13:34:21 -04005799static const struct snd_kcontrol_new desktop_mixer[] = {
Connor McAdams017310f2018-05-08 13:20:11 -04005800 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
5801 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
Connor McAdams47cdf762018-05-08 13:20:13 -04005802 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
5803 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
5804 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
5805 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
5806 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
5807 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
Connor McAdams017310f2018-05-08 13:20:11 -04005808 CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
5809 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5810 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5811 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5812 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5813 VNID_HP_ASEL, 1, HDA_OUTPUT),
5814 { } /* end */
5815};
5816
5817/*
5818 * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
5819 * because it doesn't set decibel levels for the DSP for capture.
5820 */
Takashi Sakamotob0eaa072018-05-15 22:12:57 +09005821static const struct snd_kcontrol_new r3di_mixer[] = {
Connor McAdams017310f2018-05-08 13:20:11 -04005822 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
5823 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
Connor McAdams47cdf762018-05-08 13:20:13 -04005824 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
5825 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
5826 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
5827 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
5828 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
5829 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
Connor McAdams017310f2018-05-08 13:20:11 -04005830 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
5831 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
5832 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
5833 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
5834 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
5835 VNID_HP_ASEL, 1, HDA_OUTPUT),
5836 { } /* end */
5837};
5838
Ian Minette90f29e2012-12-20 18:53:39 -08005839static int ca0132_build_controls(struct hda_codec *codec)
5840{
5841 struct ca0132_spec *spec = codec->spec;
Connor McAdams47cdf762018-05-08 13:20:13 -04005842 int i, num_fx, num_sliders;
Ian Minette90f29e2012-12-20 18:53:39 -08005843 int err = 0;
5844
5845 /* Add Mixer controls */
5846 for (i = 0; i < spec->num_mixers; i++) {
5847 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
5848 if (err < 0)
5849 return err;
5850 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005851 /* Setup vmaster with surround slaves for desktop ca0132 devices */
5852 if (spec->use_alt_functions) {
5853 snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
5854 spec->tlv);
5855 snd_hda_add_vmaster(codec, "Master Playback Volume",
5856 spec->tlv, ca0132_alt_slave_pfxs,
5857 "Playback Volume");
5858 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5859 NULL, ca0132_alt_slave_pfxs,
5860 "Playback Switch",
5861 true, &spec->vmaster_mute.sw_kctl);
5862
5863 }
Ian Minette90f29e2012-12-20 18:53:39 -08005864
5865 /* Add in and out effects controls.
5866 * VoiceFX, PE and CrystalVoice are added separately.
5867 */
5868 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
5869 for (i = 0; i < num_fx; i++) {
Connor McAdamse25e3442018-08-08 13:34:21 -04005870 /* SBZ and R3D break if Echo Cancellation is used. */
5871 if (spec->quirk == QUIRK_SBZ || spec->quirk == QUIRK_R3D) {
Connor McAdams47cdf762018-05-08 13:20:13 -04005872 if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
5873 OUT_EFFECTS_COUNT))
5874 continue;
5875 }
5876
Ian Minette90f29e2012-12-20 18:53:39 -08005877 err = add_fx_switch(codec, ca0132_effects[i].nid,
5878 ca0132_effects[i].name,
5879 ca0132_effects[i].direct);
5880 if (err < 0)
5881 return err;
5882 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005883 /*
5884 * If codec has use_alt_controls set to true, add effect level sliders,
5885 * EQ presets, and Smart Volume presets. Also, change names to add FX
5886 * prefix, and change PlayEnhancement and CrystalVoice to match.
5887 */
5888 if (spec->use_alt_controls) {
5889 ca0132_alt_add_svm_enum(codec);
5890 add_ca0132_alt_eq_presets(codec);
5891 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
5892 "Enable OutFX", 0);
5893 if (err < 0)
5894 return err;
Ian Minette90f29e2012-12-20 18:53:39 -08005895
Connor McAdams47cdf762018-05-08 13:20:13 -04005896 err = add_fx_switch(codec, CRYSTAL_VOICE,
5897 "Enable InFX", 1);
5898 if (err < 0)
5899 return err;
Ian Minette90f29e2012-12-20 18:53:39 -08005900
Connor McAdams47cdf762018-05-08 13:20:13 -04005901 num_sliders = OUT_EFFECTS_COUNT - 1;
5902 for (i = 0; i < num_sliders; i++) {
5903 err = ca0132_alt_add_effect_slider(codec,
5904 ca0132_effects[i].nid,
5905 ca0132_effects[i].name,
5906 ca0132_effects[i].direct);
5907 if (err < 0)
5908 return err;
5909 }
Ian Minette90f29e2012-12-20 18:53:39 -08005910
Connor McAdams47cdf762018-05-08 13:20:13 -04005911 err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
5912 "X-Bass Crossover", EFX_DIR_OUT);
5913
5914 if (err < 0)
5915 return err;
5916 } else {
5917 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
5918 "PlayEnhancement", 0);
5919 if (err < 0)
5920 return err;
5921
5922 err = add_fx_switch(codec, CRYSTAL_VOICE,
5923 "CrystalVoice", 1);
5924 if (err < 0)
5925 return err;
5926 }
Ian Minette90f29e2012-12-20 18:53:39 -08005927 add_voicefx(codec);
5928
Connor McAdams7cb9d942018-05-08 13:20:10 -04005929 /*
5930 * If the codec uses alt_functions, you need the enumerated controls
5931 * to select the new outputs and inputs, plus add the new mic boost
5932 * setting control.
5933 */
5934 if (spec->use_alt_functions) {
5935 ca0132_alt_add_output_enum(codec);
5936 ca0132_alt_add_input_enum(codec);
Connor McAdams47cdf762018-05-08 13:20:13 -04005937 ca0132_alt_add_mic_boost_enum(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04005938 }
Ian Minette90f29e2012-12-20 18:53:39 -08005939#ifdef ENABLE_TUNING_CONTROLS
5940 add_tuning_ctls(codec);
5941#endif
5942
5943 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5944 if (err < 0)
5945 return err;
5946
5947 if (spec->dig_out) {
5948 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
5949 spec->dig_out);
5950 if (err < 0)
5951 return err;
5952 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
5953 if (err < 0)
5954 return err;
5955 /* spec->multiout.share_spdif = 1; */
5956 }
5957
5958 if (spec->dig_in) {
5959 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
5960 if (err < 0)
5961 return err;
5962 }
Connor McAdams47cdf762018-05-08 13:20:13 -04005963
5964 if (spec->use_alt_functions)
5965 ca0132_alt_add_chmap_ctls(codec);
5966
Ian Minette90f29e2012-12-20 18:53:39 -08005967 return 0;
5968}
5969
Ian Minett5aaca442012-12-20 18:53:34 -08005970/*
Ian Minette90f29e2012-12-20 18:53:39 -08005971 * PCM
Ian Minett95c6e9c2011-06-15 15:35:17 -07005972 */
Julia Lawall071f1342016-09-11 15:05:43 +02005973static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07005974 .substreams = 1,
5975 .channels_min = 2,
Ian Minett825315b2012-12-20 18:53:36 -08005976 .channels_max = 6,
Ian Minett95c6e9c2011-06-15 15:35:17 -07005977 .ops = {
5978 .prepare = ca0132_playback_pcm_prepare,
Dylan Reide8412ca2013-04-04 13:55:09 -07005979 .cleanup = ca0132_playback_pcm_cleanup,
5980 .get_delay = ca0132_playback_pcm_delay,
Ian Minett95c6e9c2011-06-15 15:35:17 -07005981 },
5982};
5983
Julia Lawall071f1342016-09-11 15:05:43 +02005984static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07005985 .substreams = 1,
5986 .channels_min = 2,
5987 .channels_max = 2,
Ian Minett825315b2012-12-20 18:53:36 -08005988 .ops = {
5989 .prepare = ca0132_capture_pcm_prepare,
Dylan Reide8412ca2013-04-04 13:55:09 -07005990 .cleanup = ca0132_capture_pcm_cleanup,
5991 .get_delay = ca0132_capture_pcm_delay,
Ian Minett825315b2012-12-20 18:53:36 -08005992 },
Ian Minett95c6e9c2011-06-15 15:35:17 -07005993};
5994
Julia Lawall071f1342016-09-11 15:05:43 +02005995static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07005996 .substreams = 1,
5997 .channels_min = 2,
5998 .channels_max = 2,
5999 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02006000 .open = ca0132_dig_playback_pcm_open,
6001 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07006002 .prepare = ca0132_dig_playback_pcm_prepare,
6003 .cleanup = ca0132_dig_playback_pcm_cleanup
6004 },
6005};
6006
Julia Lawall071f1342016-09-11 15:05:43 +02006007static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07006008 .substreams = 1,
6009 .channels_min = 2,
6010 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07006011};
6012
6013static int ca0132_build_pcms(struct hda_codec *codec)
6014{
6015 struct ca0132_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006016 struct hda_pcm *info;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006017
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006018 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
6019 if (!info)
6020 return -ENOMEM;
Connor McAdams47cdf762018-05-08 13:20:13 -04006021 if (spec->use_alt_functions) {
6022 info->own_chmap = true;
6023 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
6024 = ca0132_alt_chmaps;
6025 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07006026 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
6027 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
6028 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
6029 spec->multiout.max_channels;
6030 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
Ian Minett825315b2012-12-20 18:53:36 -08006031 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006032 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
Ian Minett95c6e9c2011-06-15 15:35:17 -07006033
Connor McAdams009b8f92018-05-08 13:20:06 -04006034 /* With the DSP enabled, desktops don't use this ADC. */
Alastair Bridgewater5f8ddc62018-06-15 21:56:19 -04006035 if (!spec->use_alt_functions) {
Connor McAdams009b8f92018-05-08 13:20:06 -04006036 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
6037 if (!info)
6038 return -ENOMEM;
6039 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
6040 ca0132_pcm_analog_capture;
6041 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
6042 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
6043 }
Ian Minett825315b2012-12-20 18:53:36 -08006044
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006045 info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
6046 if (!info)
6047 return -ENOMEM;
Ian Minett825315b2012-12-20 18:53:36 -08006048 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
6049 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
6050 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
Ian Minett825315b2012-12-20 18:53:36 -08006051
Ian Minett95c6e9c2011-06-15 15:35:17 -07006052 if (!spec->dig_out && !spec->dig_in)
6053 return 0;
6054
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01006055 info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
6056 if (!info)
6057 return -ENOMEM;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006058 info->pcm_type = HDA_PCM_TYPE_SPDIF;
6059 if (spec->dig_out) {
6060 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
6061 ca0132_pcm_digital_playback;
6062 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
6063 }
6064 if (spec->dig_in) {
6065 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
6066 ca0132_pcm_digital_capture;
6067 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
6068 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07006069
6070 return 0;
6071}
6072
Ian Minett441aa6a2012-12-20 18:53:40 -08006073static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
6074{
6075 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01006076 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
Ian Minett441aa6a2012-12-20 18:53:40 -08006077 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
6078 snd_hda_codec_write(codec, pin, 0,
6079 AC_VERB_SET_AMP_GAIN_MUTE,
6080 AMP_OUT_UNMUTE);
6081 }
6082 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
6083 snd_hda_codec_write(codec, dac, 0,
6084 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
6085}
6086
6087static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
6088{
6089 if (pin) {
Takashi Iwaia0c041c2013-01-15 17:13:31 +01006090 snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
Ian Minett441aa6a2012-12-20 18:53:40 -08006091 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
6092 snd_hda_codec_write(codec, pin, 0,
6093 AC_VERB_SET_AMP_GAIN_MUTE,
6094 AMP_IN_UNMUTE(0));
6095 }
6096 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
6097 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
6098 AMP_IN_UNMUTE(0));
6099
6100 /* init to 0 dB and unmute. */
6101 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
6102 HDA_AMP_VOLMASK, 0x5a);
6103 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
6104 HDA_AMP_MUTE, 0);
6105 }
6106}
6107
Ian Minett5aaca442012-12-20 18:53:34 -08006108static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
6109{
6110 unsigned int caps;
6111
6112 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
6113 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
6114 snd_hda_override_amp_caps(codec, nid, dir, caps);
6115}
6116
6117/*
6118 * Switch between Digital built-in mic and analog mic.
6119 */
6120static void ca0132_set_dmic(struct hda_codec *codec, int enable)
6121{
6122 struct ca0132_spec *spec = codec->spec;
6123 unsigned int tmp;
6124 u8 val;
6125 unsigned int oldval;
6126
Takashi Iwai4e76a882014-02-25 12:21:03 +01006127 codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
Ian Minett5aaca442012-12-20 18:53:34 -08006128
6129 oldval = stop_mic1(codec);
6130 ca0132_set_vipsource(codec, 0);
6131 if (enable) {
6132 /* set DMic input as 2-ch */
6133 tmp = FLOAT_TWO;
6134 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6135
6136 val = spec->dmic_ctl;
6137 val |= 0x80;
6138 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6139 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6140
6141 if (!(spec->dmic_ctl & 0x20))
6142 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
6143 } else {
6144 /* set AMic input as mono */
6145 tmp = FLOAT_ONE;
6146 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6147
6148 val = spec->dmic_ctl;
6149 /* clear bit7 and bit5 to disable dmic */
6150 val &= 0x5f;
6151 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6152 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6153
6154 if (!(spec->dmic_ctl & 0x20))
6155 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
6156 }
6157 ca0132_set_vipsource(codec, 1);
6158 resume_mic1(codec, oldval);
6159}
6160
6161/*
6162 * Initialization for Digital Mic.
6163 */
6164static void ca0132_init_dmic(struct hda_codec *codec)
6165{
6166 struct ca0132_spec *spec = codec->spec;
6167 u8 val;
6168
6169 /* Setup Digital Mic here, but don't enable.
6170 * Enable based on jack detect.
6171 */
6172
6173 /* MCLK uses MPIO1, set to enable.
6174 * Bit 2-0: MPIO select
6175 * Bit 3: set to disable
6176 * Bit 7-4: reserved
6177 */
6178 val = 0x01;
6179 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6180 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
6181
6182 /* Data1 uses MPIO3. Data2 not use
6183 * Bit 2-0: Data1 MPIO select
6184 * Bit 3: set disable Data1
6185 * Bit 6-4: Data2 MPIO select
6186 * Bit 7: set disable Data2
6187 */
6188 val = 0x83;
6189 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6190 VENDOR_CHIPIO_DMIC_PIN_SET, val);
6191
6192 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
6193 * Bit 3-0: Channel mask
6194 * Bit 4: set for 48KHz, clear for 32KHz
6195 * Bit 5: mode
6196 * Bit 6: set to select Data2, clear for Data1
6197 * Bit 7: set to enable DMic, clear for AMic
6198 */
Alastair Bridgewatera57a46b2018-06-15 21:56:20 -04006199 if (spec->quirk == QUIRK_ALIENWARE_M17XR4)
6200 val = 0x33;
6201 else
6202 val = 0x23;
Ian Minett5aaca442012-12-20 18:53:34 -08006203 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
6204 spec->dmic_ctl = val;
6205 snd_hda_codec_write(codec, spec->input_pins[0], 0,
6206 VENDOR_CHIPIO_DMIC_CTL_SET, val);
6207}
6208
6209/*
6210 * Initialization for Analog Mic 2
6211 */
6212static void ca0132_init_analog_mic2(struct hda_codec *codec)
6213{
6214 struct ca0132_spec *spec = codec->spec;
6215
6216 mutex_lock(&spec->chipio_mutex);
6217 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6218 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
6219 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6220 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
6221 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6222 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
6223 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6224 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
6225 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6226 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
6227 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
6228 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
6229 mutex_unlock(&spec->chipio_mutex);
6230}
6231
6232static void ca0132_refresh_widget_caps(struct hda_codec *codec)
6233{
6234 struct ca0132_spec *spec = codec->spec;
6235 int i;
Ian Minett5aaca442012-12-20 18:53:34 -08006236
Takashi Iwai4e76a882014-02-25 12:21:03 +01006237 codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
Takashi Iwai7639a062015-03-03 10:07:24 +01006238 snd_hda_codec_update_widgets(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08006239
6240 for (i = 0; i < spec->multiout.num_dacs; i++)
6241 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
6242
6243 for (i = 0; i < spec->num_outputs; i++)
6244 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
6245
6246 for (i = 0; i < spec->num_inputs; i++) {
6247 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
6248 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
6249 }
6250}
6251
6252/*
Connor McAdamsc986f502018-08-08 13:34:19 -04006253 * Recon3D r3d_setup_defaults sub functions.
Connor McAdams7e6ed622018-05-08 13:20:08 -04006254 */
6255
Connor McAdamsc986f502018-08-08 13:34:19 -04006256static void r3d_dsp_scp_startup(struct hda_codec *codec)
Connor McAdams447fd8e2018-05-08 13:20:09 -04006257{
6258 unsigned int tmp;
6259
6260 tmp = 0x00000000;
6261 dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
6262
6263 tmp = 0x00000001;
6264 dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
6265
6266 tmp = 0x00000004;
6267 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6268
6269 tmp = 0x00000005;
6270 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6271
6272 tmp = 0x00000000;
6273 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6274
6275}
6276
Connor McAdamsc986f502018-08-08 13:34:19 -04006277static void r3d_dsp_initial_mic_setup(struct hda_codec *codec)
Connor McAdams7e6ed622018-05-08 13:20:08 -04006278{
6279 unsigned int tmp;
6280
6281 /* Mic 1 Setup */
6282 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6283 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6284 /* This ConnPointID is unique to Recon3Di. Haven't seen it elsewhere */
6285 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
6286 tmp = FLOAT_ONE;
6287 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6288
6289 /* Mic 2 Setup, even though it isn't connected on SBZ */
6290 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
6291 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
6292 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
6293 tmp = FLOAT_ZERO;
6294 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6295}
6296
6297/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04006298 * Initialize Sound Blaster Z analog microphones.
6299 */
6300static void sbz_init_analog_mics(struct hda_codec *codec)
6301{
6302 unsigned int tmp;
6303
6304 /* Mic 1 Setup */
6305 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6306 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6307 tmp = FLOAT_THREE;
6308 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6309
6310 /* Mic 2 Setup, even though it isn't connected on SBZ */
6311 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
6312 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
6313 tmp = FLOAT_ZERO;
6314 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6315
6316}
6317
6318/*
6319 * Sets the source of stream 0x14 to connpointID 0x48, and the destination
6320 * connpointID to 0x91. If this isn't done, the destination is 0x71, and
6321 * you get no sound. I'm guessing this has to do with the Sound Blaster Z
6322 * having an updated DAC, which changes the destination to that DAC.
6323 */
6324static void sbz_connect_streams(struct hda_codec *codec)
6325{
6326 struct ca0132_spec *spec = codec->spec;
6327
6328 mutex_lock(&spec->chipio_mutex);
6329
6330 codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
6331
6332 chipio_set_stream_channels(codec, 0x0C, 6);
6333 chipio_set_stream_control(codec, 0x0C, 1);
6334
6335 /* This value is 0x43 for 96khz, and 0x83 for 192khz. */
6336 chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
6337
6338 /* Setup stream 0x14 with it's source and destination points */
6339 chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
6340 chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
6341 chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
6342 chipio_set_stream_channels(codec, 0x14, 2);
6343 chipio_set_stream_control(codec, 0x14, 1);
6344
6345 codec_dbg(codec, "Connect Streams exited, mutex released.\n");
6346
6347 mutex_unlock(&spec->chipio_mutex);
6348
6349}
6350
6351/*
6352 * Write data through ChipIO to setup proper stream destinations.
6353 * Not sure how it exactly works, but it seems to direct data
6354 * to different destinations. Example is f8 to c0, e0 to c0.
6355 * All I know is, if you don't set these, you get no sound.
6356 */
6357static void sbz_chipio_startup_data(struct hda_codec *codec)
6358{
6359 struct ca0132_spec *spec = codec->spec;
6360
6361 mutex_lock(&spec->chipio_mutex);
6362 codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
6363
6364 /* These control audio output */
6365 chipio_write_no_mutex(codec, 0x190060, 0x0001f8c0);
6366 chipio_write_no_mutex(codec, 0x190064, 0x0001f9c1);
6367 chipio_write_no_mutex(codec, 0x190068, 0x0001fac6);
6368 chipio_write_no_mutex(codec, 0x19006c, 0x0001fbc7);
6369 /* Signal to update I think */
6370 chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
6371
6372 chipio_set_stream_channels(codec, 0x0C, 6);
6373 chipio_set_stream_control(codec, 0x0C, 1);
6374 /* No clue what these control */
6375 chipio_write_no_mutex(codec, 0x190030, 0x0001e0c0);
6376 chipio_write_no_mutex(codec, 0x190034, 0x0001e1c1);
6377 chipio_write_no_mutex(codec, 0x190038, 0x0001e4c2);
6378 chipio_write_no_mutex(codec, 0x19003c, 0x0001e5c3);
6379 chipio_write_no_mutex(codec, 0x190040, 0x0001e2c4);
6380 chipio_write_no_mutex(codec, 0x190044, 0x0001e3c5);
6381 chipio_write_no_mutex(codec, 0x190048, 0x0001e8c6);
6382 chipio_write_no_mutex(codec, 0x19004c, 0x0001e9c7);
6383 chipio_write_no_mutex(codec, 0x190050, 0x0001ecc8);
6384 chipio_write_no_mutex(codec, 0x190054, 0x0001edc9);
6385 chipio_write_no_mutex(codec, 0x190058, 0x0001eaca);
6386 chipio_write_no_mutex(codec, 0x19005c, 0x0001ebcb);
6387
6388 chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
6389
6390 codec_dbg(codec, "Startup Data exited, mutex released.\n");
6391 mutex_unlock(&spec->chipio_mutex);
6392}
6393
Connor McAdams447fd8e2018-05-08 13:20:09 -04006394/*
6395 * Sound Blaster Z uses these after DSP is loaded. Weird SCP commands
6396 * without a 0x20 source like normal.
6397 */
6398static void sbz_dsp_scp_startup(struct hda_codec *codec)
6399{
6400 unsigned int tmp;
6401
6402 tmp = 0x00000003;
6403 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6404
6405 tmp = 0x00000000;
6406 dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
6407
6408 tmp = 0x00000001;
6409 dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
6410
6411 tmp = 0x00000004;
6412 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6413
6414 tmp = 0x00000005;
6415 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6416
6417 tmp = 0x00000000;
6418 dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
6419
6420}
6421
Connor McAdams38ba69f2018-05-08 13:20:07 -04006422static void sbz_dsp_initial_mic_setup(struct hda_codec *codec)
6423{
6424 unsigned int tmp;
6425
6426 chipio_set_stream_control(codec, 0x03, 0);
6427 chipio_set_stream_control(codec, 0x04, 0);
6428
6429 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6430 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
6431
6432 tmp = FLOAT_THREE;
6433 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6434
6435 chipio_set_stream_control(codec, 0x03, 1);
6436 chipio_set_stream_control(codec, 0x04, 1);
6437
6438 chipio_write(codec, 0x18b098, 0x0000000c);
6439 chipio_write(codec, 0x18b09C, 0x0000000c);
6440}
6441
6442/*
Ian Minett5aaca442012-12-20 18:53:34 -08006443 * Setup default parameters for DSP
6444 */
6445static void ca0132_setup_defaults(struct hda_codec *codec)
6446{
Dylan Reide8f1bd52013-03-14 17:27:45 -07006447 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08006448 unsigned int tmp;
6449 int num_fx;
6450 int idx, i;
6451
Dylan Reide8f1bd52013-03-14 17:27:45 -07006452 if (spec->dsp_state != DSP_DOWNLOADED)
Ian Minett5aaca442012-12-20 18:53:34 -08006453 return;
6454
6455 /* out, in effects + voicefx */
6456 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6457 for (idx = 0; idx < num_fx; idx++) {
6458 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6459 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
6460 ca0132_effects[idx].reqs[i],
6461 ca0132_effects[idx].def_vals[i]);
6462 }
6463 }
6464
6465 /*remove DSP headroom*/
6466 tmp = FLOAT_ZERO;
6467 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6468
6469 /*set speaker EQ bypass attenuation*/
6470 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
6471
6472 /* set AMic1 and AMic2 as mono mic */
6473 tmp = FLOAT_ONE;
6474 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
6475 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
6476
6477 /* set AMic1 as CrystalVoice input */
6478 tmp = FLOAT_ONE;
6479 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
6480
6481 /* set WUH source */
6482 tmp = FLOAT_TWO;
6483 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6484}
6485
6486/*
Connor McAdamsc986f502018-08-08 13:34:19 -04006487 * Setup default parameters for Recon3D/Recon3Di DSP.
Connor McAdams7e6ed622018-05-08 13:20:08 -04006488 */
6489
Connor McAdamsc986f502018-08-08 13:34:19 -04006490static void r3d_setup_defaults(struct hda_codec *codec)
Connor McAdams7e6ed622018-05-08 13:20:08 -04006491{
6492 struct ca0132_spec *spec = codec->spec;
6493 unsigned int tmp;
6494 int num_fx;
6495 int idx, i;
6496
6497 if (spec->dsp_state != DSP_DOWNLOADED)
6498 return;
6499
Connor McAdamsc986f502018-08-08 13:34:19 -04006500 r3d_dsp_scp_startup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006501
Connor McAdamsc986f502018-08-08 13:34:19 -04006502 r3d_dsp_initial_mic_setup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006503
6504 /*remove DSP headroom*/
6505 tmp = FLOAT_ZERO;
6506 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6507
6508 /* set WUH source */
6509 tmp = FLOAT_TWO;
6510 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6511 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6512
6513 /* Set speaker source? */
6514 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
6515
Connor McAdamsc986f502018-08-08 13:34:19 -04006516 if (spec->quirk == QUIRK_R3DI)
6517 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
Connor McAdams7e6ed622018-05-08 13:20:08 -04006518
6519 /* Setup effect defaults */
6520 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6521 for (idx = 0; idx < num_fx; idx++) {
6522 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6523 dspio_set_uint_param(codec,
6524 ca0132_effects[idx].mid,
6525 ca0132_effects[idx].reqs[i],
6526 ca0132_effects[idx].def_vals[i]);
6527 }
6528 }
Connor McAdams7e6ed622018-05-08 13:20:08 -04006529}
6530
6531/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04006532 * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
6533 * than the Chromebook setup.
6534 */
6535static void sbz_setup_defaults(struct hda_codec *codec)
6536{
6537 struct ca0132_spec *spec = codec->spec;
6538 unsigned int tmp, stream_format;
6539 int num_fx;
6540 int idx, i;
6541
6542 if (spec->dsp_state != DSP_DOWNLOADED)
6543 return;
6544
Connor McAdams447fd8e2018-05-08 13:20:09 -04006545 sbz_dsp_scp_startup(codec);
Connor McAdams38ba69f2018-05-08 13:20:07 -04006546
6547 sbz_init_analog_mics(codec);
6548
6549 sbz_connect_streams(codec);
6550
6551 sbz_chipio_startup_data(codec);
6552
6553 chipio_set_stream_control(codec, 0x03, 1);
6554 chipio_set_stream_control(codec, 0x04, 1);
6555
6556 /*
6557 * Sets internal input loopback to off, used to have a switch to
6558 * enable input loopback, but turned out to be way too buggy.
6559 */
6560 tmp = FLOAT_ONE;
6561 dspio_set_uint_param(codec, 0x37, 0x08, tmp);
6562 dspio_set_uint_param(codec, 0x37, 0x10, tmp);
6563
6564 /*remove DSP headroom*/
6565 tmp = FLOAT_ZERO;
6566 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
6567
6568 /* set WUH source */
6569 tmp = FLOAT_TWO;
6570 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
6571 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6572
6573 /* Set speaker source? */
6574 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
6575
6576 sbz_dsp_initial_mic_setup(codec);
6577
6578
6579 /* out, in effects + voicefx */
6580 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
6581 for (idx = 0; idx < num_fx; idx++) {
6582 for (i = 0; i <= ca0132_effects[idx].params; i++) {
6583 dspio_set_uint_param(codec,
6584 ca0132_effects[idx].mid,
6585 ca0132_effects[idx].reqs[i],
6586 ca0132_effects[idx].def_vals[i]);
6587 }
6588 }
6589
6590 /*
6591 * Have to make a stream to bind the sound output to, otherwise
6592 * you'll get dead audio. Before I did this, it would bind to an
6593 * audio input, and would never work
6594 */
6595 stream_format = snd_hdac_calc_stream_format(48000, 2,
6596 SNDRV_PCM_FORMAT_S32_LE, 32, 0);
6597
6598 snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
6599 0, stream_format);
6600
6601 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
6602
6603 snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
6604 0, stream_format);
6605
6606 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
6607}
6608
6609/*
Ian Minett5aaca442012-12-20 18:53:34 -08006610 * Initialization of flags in chip
6611 */
6612static void ca0132_init_flags(struct hda_codec *codec)
6613{
Connor McAdams009b8f92018-05-08 13:20:06 -04006614 struct ca0132_spec *spec = codec->spec;
6615
6616 if (spec->use_alt_functions) {
6617 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
6618 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
6619 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
6620 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
6621 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
6622 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
6623 chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
6624 chipio_set_control_flag(codec,
6625 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
6626 chipio_set_control_flag(codec,
6627 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
6628 } else {
6629 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
6630 chipio_set_control_flag(codec,
6631 CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
6632 chipio_set_control_flag(codec,
6633 CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
6634 chipio_set_control_flag(codec,
6635 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
6636 chipio_set_control_flag(codec,
6637 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
6638 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
6639 }
Ian Minett5aaca442012-12-20 18:53:34 -08006640}
6641
6642/*
6643 * Initialization of parameters in chip
6644 */
6645static void ca0132_init_params(struct hda_codec *codec)
6646{
Connor McAdams009b8f92018-05-08 13:20:06 -04006647 struct ca0132_spec *spec = codec->spec;
6648
6649 if (spec->use_alt_functions) {
6650 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6651 chipio_set_conn_rate(codec, 0x0B, SR_48_000);
6652 chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
6653 chipio_set_control_param(codec, 0, 0);
6654 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
6655 }
6656
Ian Minett5aaca442012-12-20 18:53:34 -08006657 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
6658 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
6659}
Ian Minett95c6e9c2011-06-15 15:35:17 -07006660
Ian Minette90f29e2012-12-20 18:53:39 -08006661static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
6662{
6663 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
6664 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
6665 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
6666 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
6667 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
6668 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
6669
Ian Minett406261c2012-12-20 18:53:41 -08006670 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
6671 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
Ian Minette90f29e2012-12-20 18:53:39 -08006672 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
6673}
6674
6675static bool ca0132_download_dsp_images(struct hda_codec *codec)
6676{
6677 bool dsp_loaded = false;
Connor McAdams8a19bce2018-05-08 13:20:01 -04006678 struct ca0132_spec *spec = codec->spec;
Ian Minette90f29e2012-12-20 18:53:39 -08006679 const struct dsp_image_seg *dsp_os_image;
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006680 const struct firmware *fw_entry;
Connor McAdams8a19bce2018-05-08 13:20:01 -04006681 /*
6682 * Alternate firmwares for different variants. The Recon3Di apparently
6683 * can use the default firmware, but I'll leave the option in case
6684 * it needs it again.
6685 */
6686 switch (spec->quirk) {
6687 case QUIRK_SBZ:
6688 if (request_firmware(&fw_entry, SBZ_EFX_FILE,
6689 codec->card->dev) != 0) {
6690 codec_dbg(codec, "SBZ alt firmware not detected. ");
6691 spec->alt_firmware_present = false;
6692 } else {
6693 codec_dbg(codec, "Sound Blaster Z firmware selected.");
6694 spec->alt_firmware_present = true;
6695 }
6696 break;
6697 case QUIRK_R3DI:
6698 if (request_firmware(&fw_entry, R3DI_EFX_FILE,
6699 codec->card->dev) != 0) {
6700 codec_dbg(codec, "Recon3Di alt firmware not detected.");
6701 spec->alt_firmware_present = false;
6702 } else {
6703 codec_dbg(codec, "Recon3Di firmware selected.");
6704 spec->alt_firmware_present = true;
6705 }
6706 break;
6707 default:
6708 spec->alt_firmware_present = false;
6709 break;
6710 }
6711 /*
6712 * Use default ctefx.bin if no alt firmware is detected, or if none
6713 * exists for your particular codec.
6714 */
6715 if (!spec->alt_firmware_present) {
6716 codec_dbg(codec, "Default firmware selected.");
6717 if (request_firmware(&fw_entry, EFX_FILE,
6718 codec->card->dev) != 0)
6719 return false;
6720 }
Ian Minette90f29e2012-12-20 18:53:39 -08006721
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006722 dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
Dylan Reidd1d28502013-03-14 17:27:44 -07006723 if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
Takashi Iwaid9684bb2015-10-26 16:54:16 +01006724 codec_err(codec, "ca0132 DSP load image failed\n");
Dylan Reidd1d28502013-03-14 17:27:44 -07006725 goto exit_download;
6726 }
6727
Ian Minette90f29e2012-12-20 18:53:39 -08006728 dsp_loaded = dspload_wait_loaded(codec);
6729
Dylan Reidd1d28502013-03-14 17:27:44 -07006730exit_download:
Takashi Iwai15e4ba62013-01-15 17:08:38 +01006731 release_firmware(fw_entry);
6732
Ian Minette90f29e2012-12-20 18:53:39 -08006733 return dsp_loaded;
6734}
6735
6736static void ca0132_download_dsp(struct hda_codec *codec)
6737{
6738 struct ca0132_spec *spec = codec->spec;
6739
Takashi Iwai9a0869f2013-02-07 12:41:40 +01006740#ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
6741 return; /* NOP */
6742#endif
Ian Minette90f29e2012-12-20 18:53:39 -08006743
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02006744 if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
6745 return; /* don't retry failures */
6746
Dylan Reidb714a712013-03-14 17:27:46 -07006747 chipio_enable_clocks(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04006748 if (spec->dsp_state != DSP_DOWNLOADED) {
6749 spec->dsp_state = DSP_DOWNLOADING;
6750
6751 if (!ca0132_download_dsp_images(codec))
6752 spec->dsp_state = DSP_DOWNLOAD_FAILED;
6753 else
6754 spec->dsp_state = DSP_DOWNLOADED;
6755 }
Ian Minette90f29e2012-12-20 18:53:39 -08006756
Connor McAdams009b8f92018-05-08 13:20:06 -04006757 /* For codecs using alt functions, this is already done earlier */
6758 if (spec->dsp_state == DSP_DOWNLOADED && (!spec->use_alt_functions))
Ian Minette90f29e2012-12-20 18:53:39 -08006759 ca0132_set_dsp_msr(codec, true);
6760}
6761
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006762static void ca0132_process_dsp_response(struct hda_codec *codec,
6763 struct hda_jack_callback *callback)
Ian Minette90f29e2012-12-20 18:53:39 -08006764{
6765 struct ca0132_spec *spec = codec->spec;
6766
Takashi Iwai4e76a882014-02-25 12:21:03 +01006767 codec_dbg(codec, "ca0132_process_dsp_response\n");
Ian Minette90f29e2012-12-20 18:53:39 -08006768 if (spec->wait_scp) {
6769 if (dspio_get_response_data(codec) >= 0)
6770 spec->wait_scp = 0;
6771 }
6772
6773 dspio_clear_response_queue(codec);
6774}
6775
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006776static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
Ian Minette90f29e2012-12-20 18:53:39 -08006777{
Chih-Chung Chang993884f2013-03-25 10:39:23 -07006778 struct ca0132_spec *spec = codec->spec;
Takashi Iwai2ebab402016-02-09 10:23:52 +01006779 struct hda_jack_tbl *tbl;
Ian Minette90f29e2012-12-20 18:53:39 -08006780
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006781 /* Delay enabling the HP amp, to let the mic-detection
6782 * state machine run.
6783 */
6784 cancel_delayed_work_sync(&spec->unsol_hp_work);
Takashi Iwai2f35c632015-02-27 22:43:26 +01006785 schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
Takashi Iwai2ebab402016-02-09 10:23:52 +01006786 tbl = snd_hda_jack_tbl_get(codec, cb->nid);
6787 if (tbl)
6788 tbl->block_report = 1;
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006789}
Ian Minette90f29e2012-12-20 18:53:39 -08006790
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006791static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
6792{
Connor McAdamsa1b7f012018-08-08 13:34:14 -04006793 struct ca0132_spec *spec = codec->spec;
6794
6795 if (spec->use_alt_functions)
6796 ca0132_alt_select_in(codec);
6797 else
6798 ca0132_select_mic(codec);
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006799}
6800
6801static void ca0132_init_unsol(struct hda_codec *codec)
6802{
Gabriele Martinod5c016b2015-05-18 21:15:13 +02006803 struct ca0132_spec *spec = codec->spec;
6804 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
6805 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
Takashi Iwaif8fb1172014-09-11 15:53:26 +02006806 amic_callback);
6807 snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
6808 ca0132_process_dsp_response);
Connor McAdams63177af2018-05-08 13:20:02 -04006809 /* Front headphone jack detection */
Connor McAdams009b8f92018-05-08 13:20:06 -04006810 if (spec->use_alt_functions)
Connor McAdams63177af2018-05-08 13:20:02 -04006811 snd_hda_jack_detect_enable_callback(codec,
6812 spec->unsol_tag_front_hp, hp_callback);
Ian Minette90f29e2012-12-20 18:53:39 -08006813}
6814
Ian Minett5aaca442012-12-20 18:53:34 -08006815/*
6816 * Verbs tables.
6817 */
6818
6819/* Sends before DSP download. */
6820static struct hda_verb ca0132_base_init_verbs[] = {
6821 /*enable ct extension*/
6822 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
Ian Minett5aaca442012-12-20 18:53:34 -08006823 {}
6824};
6825
6826/* Send at exit. */
6827static struct hda_verb ca0132_base_exit_verbs[] = {
6828 /*set afg to D3*/
6829 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
6830 /*disable ct extension*/
6831 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
6832 {}
6833};
6834
Connor McAdams8a19bce2018-05-08 13:20:01 -04006835/* Other verbs tables. Sends after DSP download. */
Connor McAdamse93ac302018-05-08 13:20:05 -04006836
Ian Minett5aaca442012-12-20 18:53:34 -08006837static struct hda_verb ca0132_init_verbs0[] = {
6838 /* chip init verbs */
6839 {0x15, 0x70D, 0xF0},
6840 {0x15, 0x70E, 0xFE},
6841 {0x15, 0x707, 0x75},
6842 {0x15, 0x707, 0xD3},
6843 {0x15, 0x707, 0x09},
6844 {0x15, 0x707, 0x53},
6845 {0x15, 0x707, 0xD4},
6846 {0x15, 0x707, 0xEF},
6847 {0x15, 0x707, 0x75},
6848 {0x15, 0x707, 0xD3},
6849 {0x15, 0x707, 0x09},
6850 {0x15, 0x707, 0x02},
6851 {0x15, 0x707, 0x37},
6852 {0x15, 0x707, 0x78},
6853 {0x15, 0x53C, 0xCE},
6854 {0x15, 0x575, 0xC9},
6855 {0x15, 0x53D, 0xCE},
6856 {0x15, 0x5B7, 0xC9},
6857 {0x15, 0x70D, 0xE8},
6858 {0x15, 0x70E, 0xFE},
6859 {0x15, 0x707, 0x02},
6860 {0x15, 0x707, 0x68},
6861 {0x15, 0x707, 0x62},
6862 {0x15, 0x53A, 0xCE},
6863 {0x15, 0x546, 0xC9},
6864 {0x15, 0x53B, 0xCE},
6865 {0x15, 0x5E8, 0xC9},
Connor McAdamse93ac302018-05-08 13:20:05 -04006866 {}
6867};
6868
Connor McAdamse42c7c72018-08-08 13:34:18 -04006869/* Extra init verbs for desktop cards. */
6870static struct hda_verb ca0132_init_verbs1[] = {
Connor McAdamse93ac302018-05-08 13:20:05 -04006871 {0x15, 0x70D, 0x20},
6872 {0x15, 0x70E, 0x19},
6873 {0x15, 0x707, 0x00},
6874 {0x15, 0x539, 0xCE},
6875 {0x15, 0x546, 0xC9},
6876 {0x15, 0x70D, 0xB7},
6877 {0x15, 0x70E, 0x09},
6878 {0x15, 0x707, 0x10},
6879 {0x15, 0x70D, 0xAF},
6880 {0x15, 0x70E, 0x09},
6881 {0x15, 0x707, 0x01},
6882 {0x15, 0x707, 0x05},
6883 {0x15, 0x70D, 0x73},
6884 {0x15, 0x70E, 0x09},
6885 {0x15, 0x707, 0x14},
6886 {0x15, 0x6FF, 0xC4},
Ian Minett5aaca442012-12-20 18:53:34 -08006887 {}
6888};
6889
Ian Minett95c6e9c2011-06-15 15:35:17 -07006890static void ca0132_init_chip(struct hda_codec *codec)
6891{
6892 struct ca0132_spec *spec = codec->spec;
Ian Minett5aaca442012-12-20 18:53:34 -08006893 int num_fx;
6894 int i;
6895 unsigned int on;
Ian Minett95c6e9c2011-06-15 15:35:17 -07006896
6897 mutex_init(&spec->chipio_mutex);
Ian Minett5aaca442012-12-20 18:53:34 -08006898
6899 spec->cur_out_type = SPEAKER_OUT;
Connor McAdams7cb9d942018-05-08 13:20:10 -04006900 if (!spec->use_alt_functions)
6901 spec->cur_mic_type = DIGITAL_MIC;
6902 else
6903 spec->cur_mic_type = REAR_MIC;
6904
Ian Minett5aaca442012-12-20 18:53:34 -08006905 spec->cur_mic_boost = 0;
6906
6907 for (i = 0; i < VNODES_COUNT; i++) {
6908 spec->vnode_lvol[i] = 0x5a;
6909 spec->vnode_rvol[i] = 0x5a;
6910 spec->vnode_lswitch[i] = 0;
6911 spec->vnode_rswitch[i] = 0;
6912 }
6913
6914 /*
6915 * Default states for effects are in ca0132_effects[].
6916 */
6917 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
6918 for (i = 0; i < num_fx; i++) {
6919 on = (unsigned int)ca0132_effects[i].reqs[0];
6920 spec->effects_switch[i] = on ? 1 : 0;
6921 }
Connor McAdams47cdf762018-05-08 13:20:13 -04006922 /*
6923 * Sets defaults for the effect slider controls, only for alternative
6924 * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
6925 */
6926 if (spec->use_alt_controls) {
6927 spec->xbass_xover_freq = 8;
6928 for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
6929 spec->fx_ctl_val[i] = effect_slider_defaults[i];
6930 }
Ian Minett5aaca442012-12-20 18:53:34 -08006931
6932 spec->voicefx_val = 0;
6933 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
6934 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
6935
Ian Minett44f0c972012-12-20 18:53:38 -08006936#ifdef ENABLE_TUNING_CONTROLS
6937 ca0132_init_tuning_defaults(codec);
6938#endif
Ian Minett95c6e9c2011-06-15 15:35:17 -07006939}
6940
Connor McAdams2e48b2b2018-05-08 13:20:04 -04006941/*
6942 * Recon3Di exit specific commands.
6943 */
6944/* prevents popping noise on shutdown */
6945static void r3di_gpio_shutdown(struct hda_codec *codec)
6946{
6947 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
6948}
6949
6950/*
6951 * Sound Blaster Z exit specific commands.
6952 */
6953static void sbz_region2_exit(struct hda_codec *codec)
6954{
6955 struct ca0132_spec *spec = codec->spec;
6956 unsigned int i;
6957
6958 for (i = 0; i < 4; i++)
6959 writeb(0x0, spec->mem_base + 0x100);
6960 for (i = 0; i < 8; i++)
6961 writeb(0xb3, spec->mem_base + 0x304);
Connor McAdamsa62e4732018-08-08 13:34:12 -04006962
6963 ca0132_mmio_gpio_set(codec, 0, false);
6964 ca0132_mmio_gpio_set(codec, 1, false);
6965 ca0132_mmio_gpio_set(codec, 4, true);
6966 ca0132_mmio_gpio_set(codec, 5, false);
6967 ca0132_mmio_gpio_set(codec, 7, false);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04006968}
6969
6970static void sbz_set_pin_ctl_default(struct hda_codec *codec)
6971{
6972 hda_nid_t pins[5] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
6973 unsigned int i;
6974
6975 snd_hda_codec_write(codec, 0x11, 0,
6976 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
6977
6978 for (i = 0; i < 5; i++)
6979 snd_hda_codec_write(codec, pins[i], 0,
6980 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
6981}
6982
Connor McAdams2f295f92018-08-08 13:34:22 -04006983static void ca0132_clear_unsolicited(struct hda_codec *codec)
Connor McAdams2e48b2b2018-05-08 13:20:04 -04006984{
6985 hda_nid_t pins[7] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
6986 unsigned int i;
6987
6988 for (i = 0; i < 7; i++) {
6989 snd_hda_codec_write(codec, pins[i], 0,
6990 AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
6991 }
6992}
6993
6994/* On shutdown, sends commands in sets of three */
6995static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
6996 int mask, int data)
6997{
6998 if (dir >= 0)
6999 snd_hda_codec_write(codec, 0x01, 0,
7000 AC_VERB_SET_GPIO_DIRECTION, dir);
7001 if (mask >= 0)
7002 snd_hda_codec_write(codec, 0x01, 0,
7003 AC_VERB_SET_GPIO_MASK, mask);
7004
7005 if (data >= 0)
7006 snd_hda_codec_write(codec, 0x01, 0,
7007 AC_VERB_SET_GPIO_DATA, data);
7008}
7009
7010static void sbz_exit_chip(struct hda_codec *codec)
7011{
Connor McAdams009b8f92018-05-08 13:20:06 -04007012 chipio_set_stream_control(codec, 0x03, 0);
7013 chipio_set_stream_control(codec, 0x04, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007014
7015 /* Mess with GPIO */
7016 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
7017 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
7018 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
7019
Connor McAdams009b8f92018-05-08 13:20:06 -04007020 chipio_set_stream_control(codec, 0x14, 0);
7021 chipio_set_stream_control(codec, 0x0C, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007022
7023 chipio_set_conn_rate(codec, 0x41, SR_192_000);
7024 chipio_set_conn_rate(codec, 0x91, SR_192_000);
7025
7026 chipio_write(codec, 0x18a020, 0x00000083);
7027
7028 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
7029 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
7030 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
7031
Connor McAdams009b8f92018-05-08 13:20:06 -04007032 chipio_set_stream_control(codec, 0x0C, 0);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007033
7034 chipio_set_control_param(codec, 0x0D, 0x24);
7035
Connor McAdams2f295f92018-08-08 13:34:22 -04007036 ca0132_clear_unsolicited(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007037 sbz_set_pin_ctl_default(codec);
7038
7039 snd_hda_codec_write(codec, 0x0B, 0,
7040 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
7041
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007042 sbz_region2_exit(codec);
7043}
7044
Connor McAdams2f295f92018-08-08 13:34:22 -04007045static void r3d_exit_chip(struct hda_codec *codec)
7046{
7047 ca0132_clear_unsolicited(codec);
7048 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
7049 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
7050}
7051
Ian Minett95c6e9c2011-06-15 15:35:17 -07007052static void ca0132_exit_chip(struct hda_codec *codec)
7053{
7054 /* put any chip cleanup stuffs here. */
Ian Minett5aaca442012-12-20 18:53:34 -08007055
7056 if (dspload_is_loaded(codec))
7057 dsp_reset(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007058}
7059
Connor McAdamse93ac302018-05-08 13:20:05 -04007060/*
Connor McAdams38ba69f2018-05-08 13:20:07 -04007061 * This fixes a problem that was hard to reproduce. Very rarely, I would
7062 * boot up, and there would be no sound, but the DSP indicated it had loaded
7063 * properly. I did a few memory dumps to see if anything was different, and
7064 * there were a few areas of memory uninitialized with a1a2a3a4. This function
7065 * checks if those areas are uninitialized, and if they are, it'll attempt to
7066 * reload the card 3 times. Usually it fixes by the second.
7067 */
7068static void sbz_dsp_startup_check(struct hda_codec *codec)
7069{
7070 struct ca0132_spec *spec = codec->spec;
7071 unsigned int dsp_data_check[4];
7072 unsigned int cur_address = 0x390;
7073 unsigned int i;
7074 unsigned int failure = 0;
7075 unsigned int reload = 3;
7076
7077 if (spec->startup_check_entered)
7078 return;
7079
7080 spec->startup_check_entered = true;
7081
7082 for (i = 0; i < 4; i++) {
7083 chipio_read(codec, cur_address, &dsp_data_check[i]);
7084 cur_address += 0x4;
7085 }
7086 for (i = 0; i < 4; i++) {
7087 if (dsp_data_check[i] == 0xa1a2a3a4)
7088 failure = 1;
7089 }
7090
7091 codec_dbg(codec, "Startup Check: %d ", failure);
7092 if (failure)
7093 codec_info(codec, "DSP not initialized properly. Attempting to fix.");
7094 /*
7095 * While the failure condition is true, and we haven't reached our
7096 * three reload limit, continue trying to reload the driver and
7097 * fix the issue.
7098 */
7099 while (failure && (reload != 0)) {
7100 codec_info(codec, "Reloading... Tries left: %d", reload);
7101 sbz_exit_chip(codec);
7102 spec->dsp_state = DSP_DOWNLOAD_INIT;
7103 codec->patch_ops.init(codec);
7104 failure = 0;
7105 for (i = 0; i < 4; i++) {
7106 chipio_read(codec, cur_address, &dsp_data_check[i]);
7107 cur_address += 0x4;
7108 }
7109 for (i = 0; i < 4; i++) {
7110 if (dsp_data_check[i] == 0xa1a2a3a4)
7111 failure = 1;
7112 }
7113 reload--;
7114 }
7115
7116 if (!failure && reload < 3)
7117 codec_info(codec, "DSP fixed.");
7118
7119 if (!failure)
7120 return;
7121
7122 codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
7123}
7124
7125/*
Connor McAdamse93ac302018-05-08 13:20:05 -04007126 * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
7127 * extra precision for decibel values. If you had the dB value in floating point
7128 * you would take the value after the decimal point, multiply by 64, and divide
7129 * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
7130 * implement fixed point or floating point dB volumes. For now, I'll set them
7131 * to 0 just incase a value has lingered from a boot into Windows.
7132 */
7133static void ca0132_alt_vol_setup(struct hda_codec *codec)
7134{
7135 snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
7136 snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
7137 snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
7138 snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
7139 snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
7140 snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
7141 snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
7142 snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
7143}
7144
7145/*
7146 * Extra commands that don't really fit anywhere else.
7147 */
7148static void sbz_pre_dsp_setup(struct hda_codec *codec)
7149{
7150 struct ca0132_spec *spec = codec->spec;
7151
7152 writel(0x00820680, spec->mem_base + 0x01C);
7153 writel(0x00820680, spec->mem_base + 0x01C);
7154
Connor McAdamse93ac302018-05-08 13:20:05 -04007155 chipio_write(codec, 0x18b0a4, 0x000000c2);
7156
7157 snd_hda_codec_write(codec, 0x11, 0,
7158 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
7159}
7160
Connor McAdamse42c7c72018-08-08 13:34:18 -04007161static void r3d_pre_dsp_setup(struct hda_codec *codec)
7162{
Connor McAdamse42c7c72018-08-08 13:34:18 -04007163 chipio_write(codec, 0x18b0a4, 0x000000c2);
7164
7165 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7166 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
7167 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7168 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
7169 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7170 VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
7171
7172 snd_hda_codec_write(codec, 0x11, 0,
7173 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
7174}
7175
Connor McAdamse93ac302018-05-08 13:20:05 -04007176static void r3di_pre_dsp_setup(struct hda_codec *codec)
7177{
7178 chipio_write(codec, 0x18b0a4, 0x000000c2);
7179
7180 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7181 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
7182 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7183 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
7184 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7185 VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
7186
7187 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7188 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
7189 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7190 VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
7191 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7192 VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
7193 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7194 VENDOR_CHIPIO_8051_DATA_WRITE, 0x40);
7195
7196 snd_hda_codec_write(codec, 0x11, 0,
7197 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
7198}
7199
Connor McAdamse93ac302018-05-08 13:20:05 -04007200/*
7201 * These are sent before the DSP is downloaded. Not sure
7202 * what they do, or if they're necessary. Could possibly
7203 * be removed. Figure they're better to leave in.
7204 */
Connor McAdamse42c7c72018-08-08 13:34:18 -04007205static void ca0132_mmio_init(struct hda_codec *codec)
Connor McAdamse93ac302018-05-08 13:20:05 -04007206{
7207 struct ca0132_spec *spec = codec->spec;
7208
7209 writel(0x00000000, spec->mem_base + 0x400);
7210 writel(0x00000000, spec->mem_base + 0x408);
7211 writel(0x00000000, spec->mem_base + 0x40C);
7212 writel(0x00880680, spec->mem_base + 0x01C);
7213 writel(0x00000083, spec->mem_base + 0xC0C);
7214 writel(0x00000030, spec->mem_base + 0xC00);
7215 writel(0x00000000, spec->mem_base + 0xC04);
7216 writel(0x00000003, spec->mem_base + 0xC0C);
7217 writel(0x00000003, spec->mem_base + 0xC0C);
7218 writel(0x00000003, spec->mem_base + 0xC0C);
7219 writel(0x00000003, spec->mem_base + 0xC0C);
7220 writel(0x000000C1, spec->mem_base + 0xC08);
7221 writel(0x000000F1, spec->mem_base + 0xC08);
7222 writel(0x00000001, spec->mem_base + 0xC08);
7223 writel(0x000000C7, spec->mem_base + 0xC08);
7224 writel(0x000000C1, spec->mem_base + 0xC08);
7225 writel(0x00000080, spec->mem_base + 0xC04);
7226}
7227
7228/*
7229 * Extra init functions for alternative ca0132 codecs. Done
7230 * here so they don't clutter up the main ca0132_init function
7231 * anymore than they have to.
7232 */
7233static void ca0132_alt_init(struct hda_codec *codec)
7234{
7235 struct ca0132_spec *spec = codec->spec;
7236
7237 ca0132_alt_vol_setup(codec);
7238
7239 switch (spec->quirk) {
7240 case QUIRK_SBZ:
7241 codec_dbg(codec, "SBZ alt_init");
7242 ca0132_gpio_init(codec);
7243 sbz_pre_dsp_setup(codec);
7244 snd_hda_sequence_write(codec, spec->chip_init_verbs);
Connor McAdamse42c7c72018-08-08 13:34:18 -04007245 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
Connor McAdamse93ac302018-05-08 13:20:05 -04007246 break;
7247 case QUIRK_R3DI:
7248 codec_dbg(codec, "R3DI alt_init");
7249 ca0132_gpio_init(codec);
7250 ca0132_gpio_setup(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007251 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
Connor McAdamse93ac302018-05-08 13:20:05 -04007252 r3di_pre_dsp_setup(codec);
7253 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7254 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
7255 break;
Connor McAdamse42c7c72018-08-08 13:34:18 -04007256 case QUIRK_R3D:
7257 r3d_pre_dsp_setup(codec);
7258 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7259 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
7260 break;
Connor McAdamse93ac302018-05-08 13:20:05 -04007261 }
7262}
7263
Ian Minett95c6e9c2011-06-15 15:35:17 -07007264static int ca0132_init(struct hda_codec *codec)
7265{
7266 struct ca0132_spec *spec = codec->spec;
7267 struct auto_pin_cfg *cfg = &spec->autocfg;
7268 int i;
Connor McAdamse93ac302018-05-08 13:20:05 -04007269 bool dsp_loaded;
7270
7271 /*
7272 * If the DSP is already downloaded, and init has been entered again,
7273 * there's only two reasons for it. One, the codec has awaken from a
7274 * suspended state, and in that case dspload_is_loaded will return
7275 * false, and the init will be ran again. The other reason it gets
7276 * re entered is on startup for some reason it triggers a suspend and
7277 * resume state. In this case, it will check if the DSP is downloaded,
7278 * and not run the init function again. For codecs using alt_functions,
7279 * it will check if the DSP is loaded properly.
7280 */
7281 if (spec->dsp_state == DSP_DOWNLOADED) {
7282 dsp_loaded = dspload_is_loaded(codec);
7283 if (!dsp_loaded) {
7284 spec->dsp_reload = true;
7285 spec->dsp_state = DSP_DOWNLOAD_INIT;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007286 } else {
7287 if (spec->quirk == QUIRK_SBZ)
7288 sbz_dsp_startup_check(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007289 return 0;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007290 }
Connor McAdamse93ac302018-05-08 13:20:05 -04007291 }
Ian Minett95c6e9c2011-06-15 15:35:17 -07007292
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02007293 if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
7294 spec->dsp_state = DSP_DOWNLOAD_INIT;
Takashi Iwai4a8b89f2013-02-12 10:15:15 +01007295 spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
Ian Minett5aaca442012-12-20 18:53:34 -08007296
Connor McAdamse42c7c72018-08-08 13:34:18 -04007297 if (spec->use_pci_mmio)
7298 ca0132_mmio_init(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007299
Takashi Iwai664c7152015-04-08 11:43:14 +02007300 snd_hda_power_up_pm(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08007301
Takashi Iwaif8fb1172014-09-11 15:53:26 +02007302 ca0132_init_unsol(codec);
Ian Minett5aaca442012-12-20 18:53:34 -08007303 ca0132_init_params(codec);
7304 ca0132_init_flags(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007305
Ian Minett5aaca442012-12-20 18:53:34 -08007306 snd_hda_sequence_write(codec, spec->base_init_verbs);
Connor McAdamse93ac302018-05-08 13:20:05 -04007307
Alastair Bridgewater365c7f22018-06-15 21:56:17 -04007308 if (spec->use_alt_functions)
Connor McAdamse93ac302018-05-08 13:20:05 -04007309 ca0132_alt_init(codec);
7310
Ian Minett01ef7db2012-09-20 20:29:16 -07007311 ca0132_download_dsp(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007312
Ian Minett5aaca442012-12-20 18:53:34 -08007313 ca0132_refresh_widget_caps(codec);
Connor McAdamse93ac302018-05-08 13:20:05 -04007314
Connor McAdams7e6ed622018-05-08 13:20:08 -04007315 switch (spec->quirk) {
7316 case QUIRK_R3DI:
Connor McAdamsc986f502018-08-08 13:34:19 -04007317 case QUIRK_R3D:
7318 r3d_setup_defaults(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007319 break;
Alastair Bridgewater126b75e2018-06-15 21:56:18 -04007320 case QUIRK_SBZ:
Connor McAdamsd97420d2018-08-08 13:34:13 -04007321 sbz_setup_defaults(codec);
Alastair Bridgewater126b75e2018-06-15 21:56:18 -04007322 break;
7323 default:
Connor McAdams38ba69f2018-05-08 13:20:07 -04007324 ca0132_setup_defaults(codec);
7325 ca0132_init_analog_mic2(codec);
7326 ca0132_init_dmic(codec);
Connor McAdams7e6ed622018-05-08 13:20:08 -04007327 break;
Connor McAdams38ba69f2018-05-08 13:20:07 -04007328 }
Ian Minett01ef7db2012-09-20 20:29:16 -07007329
Ian Minett5aaca442012-12-20 18:53:34 -08007330 for (i = 0; i < spec->num_outputs; i++)
7331 init_output(codec, spec->out_pins[i], spec->dacs[0]);
7332
Ian Minett95c6e9c2011-06-15 15:35:17 -07007333 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
7334
7335 for (i = 0; i < spec->num_inputs; i++)
7336 init_input(codec, spec->input_pins[i], spec->adcs[i]);
7337
7338 init_input(codec, cfg->dig_in_pin, spec->dig_in);
7339
Connor McAdams009b8f92018-05-08 13:20:06 -04007340 if (!spec->use_alt_functions) {
Connor McAdamse93ac302018-05-08 13:20:05 -04007341 snd_hda_sequence_write(codec, spec->chip_init_verbs);
7342 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7343 VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
7344 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
7345 VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
7346 }
7347
Connor McAdams7cb9d942018-05-08 13:20:10 -04007348 if (spec->quirk == QUIRK_SBZ)
Connor McAdamse93ac302018-05-08 13:20:05 -04007349 ca0132_gpio_setup(codec);
7350
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007351 snd_hda_sequence_write(codec, spec->spec_init_verbs);
Connor McAdamsd97420d2018-08-08 13:34:13 -04007352 if (spec->use_alt_functions) {
Connor McAdams7cb9d942018-05-08 13:20:10 -04007353 ca0132_alt_select_out(codec);
7354 ca0132_alt_select_in(codec);
Connor McAdamsd97420d2018-08-08 13:34:13 -04007355 } else {
Connor McAdams7cb9d942018-05-08 13:20:10 -04007356 ca0132_select_out(codec);
7357 ca0132_select_mic(codec);
Connor McAdams7cb9d942018-05-08 13:20:10 -04007358 }
Ian Minett5aaca442012-12-20 18:53:34 -08007359
Ian Minetta73d5112012-12-20 18:53:37 -08007360 snd_hda_jack_report_sync(codec);
7361
Connor McAdamse93ac302018-05-08 13:20:05 -04007362 /*
7363 * Re set the PlayEnhancement switch on a resume event, because the
7364 * controls will not be reloaded.
7365 */
7366 if (spec->dsp_reload) {
7367 spec->dsp_reload = false;
7368 ca0132_pe_switch_set(codec);
7369 }
7370
Takashi Iwai664c7152015-04-08 11:43:14 +02007371 snd_hda_power_down_pm(codec);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007372
7373 return 0;
7374}
7375
Ian Minett95c6e9c2011-06-15 15:35:17 -07007376static void ca0132_free(struct hda_codec *codec)
7377{
Ian Minett5aaca442012-12-20 18:53:34 -08007378 struct ca0132_spec *spec = codec->spec;
7379
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007380 cancel_delayed_work_sync(&spec->unsol_hp_work);
Ian Minett5aaca442012-12-20 18:53:34 -08007381 snd_hda_power_up(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007382 switch (spec->quirk) {
7383 case QUIRK_SBZ:
7384 sbz_exit_chip(codec);
7385 break;
Connor McAdams2f295f92018-08-08 13:34:22 -04007386 case QUIRK_R3D:
7387 r3d_exit_chip(codec);
7388 break;
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007389 case QUIRK_R3DI:
7390 r3di_gpio_shutdown(codec);
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007391 break;
7392 }
Connor McAdams2f295f92018-08-08 13:34:22 -04007393
7394 snd_hda_sequence_write(codec, spec->base_exit_verbs);
7395 ca0132_exit_chip(codec);
7396
Ian Minett5aaca442012-12-20 18:53:34 -08007397 snd_hda_power_down(codec);
Connor McAdamsaa317042018-05-08 13:20:03 -04007398 if (spec->mem_base)
7399 iounmap(spec->mem_base);
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007400 kfree(spec->spec_init_verbs);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007401 kfree(codec->spec);
7402}
7403
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007404static void ca0132_reboot_notify(struct hda_codec *codec)
7405{
7406 codec->patch_ops.free(codec);
7407}
7408
Julia Lawall071f1342016-09-11 15:05:43 +02007409static const struct hda_codec_ops ca0132_patch_ops = {
Ian Minett95c6e9c2011-06-15 15:35:17 -07007410 .build_controls = ca0132_build_controls,
7411 .build_pcms = ca0132_build_pcms,
7412 .init = ca0132_init,
7413 .free = ca0132_free,
Takashi Iwaif8fb1172014-09-11 15:53:26 +02007414 .unsol_event = snd_hda_jack_unsol_event,
Connor McAdams2e48b2b2018-05-08 13:20:04 -04007415 .reboot_notify = ca0132_reboot_notify,
Ian Minett95c6e9c2011-06-15 15:35:17 -07007416};
7417
Ian Minett441aa6a2012-12-20 18:53:40 -08007418static void ca0132_config(struct hda_codec *codec)
7419{
7420 struct ca0132_spec *spec = codec->spec;
Ian Minett441aa6a2012-12-20 18:53:40 -08007421
7422 spec->dacs[0] = 0x2;
7423 spec->dacs[1] = 0x3;
7424 spec->dacs[2] = 0x4;
7425
7426 spec->multiout.dac_nids = spec->dacs;
7427 spec->multiout.num_dacs = 3;
Ian Minett441aa6a2012-12-20 18:53:40 -08007428
Connor McAdams009b8f92018-05-08 13:20:06 -04007429 if (!spec->use_alt_functions)
Connor McAdams63177af2018-05-08 13:20:02 -04007430 spec->multiout.max_channels = 2;
7431 else
7432 spec->multiout.max_channels = 6;
7433
7434 switch (spec->quirk) {
7435 case QUIRK_ALIENWARE:
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007436 codec_dbg(codec, "ca0132_config: QUIRK_ALIENWARE applied.\n");
Takashi Iwaife14f392015-08-10 16:53:32 +02007437 snd_hda_apply_pincfgs(codec, alienware_pincfgs);
7438
7439 spec->num_outputs = 2;
7440 spec->out_pins[0] = 0x0b; /* speaker out */
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007441 spec->out_pins[1] = 0x0f;
Takashi Iwaife14f392015-08-10 16:53:32 +02007442 spec->shared_out_nid = 0x2;
7443 spec->unsol_tag_hp = 0x0f;
7444
7445 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
7446 spec->adcs[1] = 0x8; /* analog mic2 */
7447 spec->adcs[2] = 0xa; /* what u hear */
7448
7449 spec->num_inputs = 3;
7450 spec->input_pins[0] = 0x12;
7451 spec->input_pins[1] = 0x11;
7452 spec->input_pins[2] = 0x13;
7453 spec->shared_mic_nid = 0x7;
7454 spec->unsol_tag_amic1 = 0x11;
Connor McAdams63177af2018-05-08 13:20:02 -04007455 break;
7456 case QUIRK_SBZ:
Connor McAdams7f73df92018-08-08 13:34:16 -04007457 case QUIRK_R3D:
7458 if (spec->quirk == QUIRK_SBZ) {
7459 codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
7460 snd_hda_apply_pincfgs(codec, sbz_pincfgs);
7461 }
7462 if (spec->quirk == QUIRK_R3D) {
7463 codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
7464 snd_hda_apply_pincfgs(codec, r3d_pincfgs);
7465 }
Connor McAdams63177af2018-05-08 13:20:02 -04007466
7467 spec->num_outputs = 2;
7468 spec->out_pins[0] = 0x0B; /* Line out */
7469 spec->out_pins[1] = 0x0F; /* Rear headphone out */
7470 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
7471 spec->out_pins[3] = 0x11; /* Rear surround */
7472 spec->shared_out_nid = 0x2;
7473 spec->unsol_tag_hp = spec->out_pins[1];
7474 spec->unsol_tag_front_hp = spec->out_pins[2];
7475
7476 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
7477 spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
7478 spec->adcs[2] = 0xa; /* what u hear */
7479
7480 spec->num_inputs = 2;
7481 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
7482 spec->input_pins[1] = 0x13; /* What U Hear */
7483 spec->shared_mic_nid = 0x7;
7484 spec->unsol_tag_amic1 = spec->input_pins[0];
7485
7486 /* SPDIF I/O */
7487 spec->dig_out = 0x05;
7488 spec->multiout.dig_out_nid = spec->dig_out;
Connor McAdams63177af2018-05-08 13:20:02 -04007489 spec->dig_in = 0x09;
Connor McAdams63177af2018-05-08 13:20:02 -04007490 break;
7491 case QUIRK_R3DI:
7492 codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
7493 snd_hda_apply_pincfgs(codec, r3di_pincfgs);
7494
7495 spec->num_outputs = 2;
7496 spec->out_pins[0] = 0x0B; /* Line out */
7497 spec->out_pins[1] = 0x0F; /* Rear headphone out */
7498 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
7499 spec->out_pins[3] = 0x11; /* Rear surround */
7500 spec->shared_out_nid = 0x2;
7501 spec->unsol_tag_hp = spec->out_pins[1];
7502 spec->unsol_tag_front_hp = spec->out_pins[2];
7503
7504 spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
7505 spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
7506 spec->adcs[2] = 0x0a; /* what u hear */
7507
7508 spec->num_inputs = 2;
7509 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
7510 spec->input_pins[1] = 0x13; /* What U Hear */
7511 spec->shared_mic_nid = 0x7;
7512 spec->unsol_tag_amic1 = spec->input_pins[0];
7513
7514 /* SPDIF I/O */
7515 spec->dig_out = 0x05;
7516 spec->multiout.dig_out_nid = spec->dig_out;
Connor McAdams63177af2018-05-08 13:20:02 -04007517 break;
7518 default:
Takashi Iwaife14f392015-08-10 16:53:32 +02007519 spec->num_outputs = 2;
7520 spec->out_pins[0] = 0x0b; /* speaker out */
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007521 spec->out_pins[1] = 0x10; /* headphone out */
Takashi Iwaife14f392015-08-10 16:53:32 +02007522 spec->shared_out_nid = 0x2;
7523 spec->unsol_tag_hp = spec->out_pins[1];
7524
7525 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
7526 spec->adcs[1] = 0x8; /* analog mic2 */
7527 spec->adcs[2] = 0xa; /* what u hear */
7528
7529 spec->num_inputs = 3;
7530 spec->input_pins[0] = 0x12;
7531 spec->input_pins[1] = 0x11;
7532 spec->input_pins[2] = 0x13;
7533 spec->shared_mic_nid = 0x7;
7534 spec->unsol_tag_amic1 = spec->input_pins[0];
7535
7536 /* SPDIF I/O */
7537 spec->dig_out = 0x05;
7538 spec->multiout.dig_out_nid = spec->dig_out;
Takashi Iwaife14f392015-08-10 16:53:32 +02007539 spec->dig_in = 0x09;
Connor McAdams63177af2018-05-08 13:20:02 -04007540 break;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007541 }
Ian Minett441aa6a2012-12-20 18:53:40 -08007542}
7543
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007544static int ca0132_prepare_verbs(struct hda_codec *codec)
7545{
7546/* Verbs + terminator (an empty element) */
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007547#define NUM_SPEC_VERBS 2
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007548 struct ca0132_spec *spec = codec->spec;
7549
7550 spec->chip_init_verbs = ca0132_init_verbs0;
Connor McAdamse42c7c72018-08-08 13:34:18 -04007551 if (spec->quirk == QUIRK_SBZ || spec->quirk == QUIRK_R3D)
7552 spec->desktop_init_verbs = ca0132_init_verbs1;
Kees Cook6396bb22018-06-12 14:03:40 -07007553 spec->spec_init_verbs = kcalloc(NUM_SPEC_VERBS,
7554 sizeof(struct hda_verb),
7555 GFP_KERNEL);
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007556 if (!spec->spec_init_verbs)
7557 return -ENOMEM;
7558
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007559 /* config EAPD */
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007560 spec->spec_init_verbs[0].nid = 0x0b;
7561 spec->spec_init_verbs[0].param = 0x78D;
7562 spec->spec_init_verbs[0].verb = 0x00;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007563
7564 /* Previously commented configuration */
7565 /*
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007566 spec->spec_init_verbs[2].nid = 0x0b;
7567 spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
7568 spec->spec_init_verbs[2].verb = 0x02;
7569
7570 spec->spec_init_verbs[3].nid = 0x10;
7571 spec->spec_init_verbs[3].param = 0x78D;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007572 spec->spec_init_verbs[3].verb = 0x02;
7573
7574 spec->spec_init_verbs[4].nid = 0x10;
Alastair Bridgewatera3d90d62018-06-15 21:56:16 -04007575 spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007576 spec->spec_init_verbs[4].verb = 0x02;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007577 */
7578
7579 /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
7580 return 0;
7581}
7582
Ian Minett95c6e9c2011-06-15 15:35:17 -07007583static int patch_ca0132(struct hda_codec *codec)
7584{
7585 struct ca0132_spec *spec;
Ian Minetta73d5112012-12-20 18:53:37 -08007586 int err;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007587 const struct snd_pci_quirk *quirk;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007588
Takashi Iwai4e76a882014-02-25 12:21:03 +01007589 codec_dbg(codec, "patch_ca0132\n");
Ian Minett95c6e9c2011-06-15 15:35:17 -07007590
7591 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
7592 if (!spec)
7593 return -ENOMEM;
7594 codec->spec = spec;
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007595 spec->codec = codec;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007596
Takashi Iwai225068a2015-05-29 10:42:14 +02007597 codec->patch_ops = ca0132_patch_ops;
7598 codec->pcm_format_first = 1;
7599 codec->no_sticky_stream = 1;
7600
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007601 /* Detect codec quirk */
7602 quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
7603 if (quirk)
7604 spec->quirk = quirk->value;
7605 else
7606 spec->quirk = QUIRK_NONE;
7607
Takashi Iwaie24aa0a2014-08-10 13:30:08 +02007608 spec->dsp_state = DSP_DOWNLOAD_INIT;
Ian Minetta7e76272012-12-20 18:53:35 -08007609 spec->num_mixers = 1;
Connor McAdams017310f2018-05-08 13:20:11 -04007610
7611 /* Set which mixers each quirk uses. */
7612 switch (spec->quirk) {
7613 case QUIRK_SBZ:
Connor McAdamse25e3442018-08-08 13:34:21 -04007614 spec->mixers[0] = desktop_mixer;
Connor McAdams017310f2018-05-08 13:20:11 -04007615 snd_hda_codec_set_name(codec, "Sound Blaster Z");
7616 break;
Connor McAdamse25e3442018-08-08 13:34:21 -04007617 case QUIRK_R3D:
7618 spec->mixers[0] = desktop_mixer;
7619 snd_hda_codec_set_name(codec, "Recon3D");
7620 break;
Connor McAdams017310f2018-05-08 13:20:11 -04007621 case QUIRK_R3DI:
7622 spec->mixers[0] = r3di_mixer;
7623 snd_hda_codec_set_name(codec, "Recon3Di");
7624 break;
7625 default:
7626 spec->mixers[0] = ca0132_mixer;
7627 break;
7628 }
Ian Minetta7e76272012-12-20 18:53:35 -08007629
Connor McAdams08eca6b2018-08-08 13:34:17 -04007630 /* Setup whether or not to use alt functions/controls/pci_mmio */
Connor McAdams009b8f92018-05-08 13:20:06 -04007631 switch (spec->quirk) {
7632 case QUIRK_SBZ:
Connor McAdamse42c7c72018-08-08 13:34:18 -04007633 case QUIRK_R3D:
Connor McAdams08eca6b2018-08-08 13:34:17 -04007634 spec->use_alt_controls = true;
7635 spec->use_alt_functions = true;
7636 spec->use_pci_mmio = true;
7637 break;
Connor McAdams009b8f92018-05-08 13:20:06 -04007638 case QUIRK_R3DI:
Connor McAdams47cdf762018-05-08 13:20:13 -04007639 spec->use_alt_controls = true;
Connor McAdams009b8f92018-05-08 13:20:06 -04007640 spec->use_alt_functions = true;
Connor McAdams08eca6b2018-08-08 13:34:17 -04007641 spec->use_pci_mmio = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007642 break;
7643 default:
Connor McAdams47cdf762018-05-08 13:20:13 -04007644 spec->use_alt_controls = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007645 spec->use_alt_functions = false;
Connor McAdams08eca6b2018-08-08 13:34:17 -04007646 spec->use_pci_mmio = false;
Connor McAdams009b8f92018-05-08 13:20:06 -04007647 break;
7648 }
7649
Connor McAdams08eca6b2018-08-08 13:34:17 -04007650 if (spec->use_pci_mmio) {
7651 spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
7652 if (spec->mem_base == NULL) {
7653 codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
7654 spec->quirk = QUIRK_NONE;
7655 }
7656 }
7657
Ian Minett5aaca442012-12-20 18:53:34 -08007658 spec->base_init_verbs = ca0132_base_init_verbs;
7659 spec->base_exit_verbs = ca0132_base_exit_verbs;
Ian Minett5aaca442012-12-20 18:53:34 -08007660
Chih-Chung Chang993884f2013-03-25 10:39:23 -07007661 INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
7662
Ian Minett95c6e9c2011-06-15 15:35:17 -07007663 ca0132_init_chip(codec);
7664
7665 ca0132_config(codec);
7666
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007667 err = ca0132_prepare_verbs(codec);
7668 if (err < 0)
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007669 goto error;
Gabriele Martinod5c016b2015-05-18 21:15:13 +02007670
Ian Minetta73d5112012-12-20 18:53:37 -08007671 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
7672 if (err < 0)
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007673 goto error;
Ian Minetta73d5112012-12-20 18:53:37 -08007674
Ian Minett95c6e9c2011-06-15 15:35:17 -07007675 return 0;
Takashi Iwaicc91cea2017-09-04 17:38:36 +02007676
7677 error:
7678 ca0132_free(codec);
7679 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07007680}
7681
7682/*
7683 * patch entries
7684 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007685static struct hda_device_id snd_hda_id_ca0132[] = {
7686 HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
Ian Minett95c6e9c2011-06-15 15:35:17 -07007687 {} /* terminator */
7688};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007689MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
Ian Minett95c6e9c2011-06-15 15:35:17 -07007690
7691MODULE_LICENSE("GPL");
Ian Minett406261c2012-12-20 18:53:41 -08007692MODULE_DESCRIPTION("Creative Sound Core3D codec");
Ian Minett95c6e9c2011-06-15 15:35:17 -07007693
Takashi Iwaid8a766a2015-02-17 15:25:37 +01007694static struct hda_codec_driver ca0132_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02007695 .id = snd_hda_id_ca0132,
Ian Minett95c6e9c2011-06-15 15:35:17 -07007696};
7697
Takashi Iwaid8a766a2015-02-17 15:25:37 +01007698module_hda_codec_driver(ca0132_driver);