blob: 38ac07b8a4ec25f0840d578358cbb2ba5239a142 [file] [log] [blame]
Ian Minett95c6e9c2011-06-15 15:35:17 -07001/*
2 * HD audio interface patch for Creative CA0132 chip
3 *
4 * Copyright (c) 2011, Creative Technology Ltd.
5 *
6 * Based on patch_ca0110.c
7 * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
8 *
9 * This driver is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This driver is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/slab.h>
27#include <linux/pci.h>
28#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040029#include <linux/module.h>
Ian Minett4aa3bb02012-09-20 20:29:15 -070030#include <linux/firmware.h>
Ian Minett95c6e9c2011-06-15 15:35:17 -070031#include <sound/core.h>
32#include "hda_codec.h"
33#include "hda_local.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020034#include "hda_auto_parser.h"
Ian Minett95c6e9c2011-06-15 15:35:17 -070035
Ian Minettbcd109c2012-09-20 20:29:14 -070036#include "ca0132_regs.h"
37
Ian Minettef6b2ea2012-12-20 18:53:33 -080038/* Enable this to see controls for tuning purpose. */
39/*#define ENABLE_TUNING_CONTROLS*/
40
41#define FLOAT_ZERO 0x00000000
42#define FLOAT_ONE 0x3f800000
43#define FLOAT_TWO 0x40000000
44#define FLOAT_MINUS_5 0xc0a00000
45
46#define UNSOL_TAG_HP 0x10
47#define UNSOL_TAG_AMIC1 0x12
48#define UNSOL_TAG_DSP 0x16
49
Ian Minett4aa3bb02012-09-20 20:29:15 -070050#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
51#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
52
53#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
54#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
55#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
56
57#define MASTERCONTROL 0x80
Ian Minettef6b2ea2012-12-20 18:53:33 -080058#define MASTERCONTROL_ALLOC_DMA_CHAN 10
59#define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
Ian Minett4aa3bb02012-09-20 20:29:15 -070060
Ian Minett95c6e9c2011-06-15 15:35:17 -070061#define WIDGET_CHIP_CTRL 0x15
62#define WIDGET_DSP_CTRL 0x16
63
Ian Minett4aa3bb02012-09-20 20:29:15 -070064#define MEM_CONNID_MICIN1 3
65#define MEM_CONNID_MICIN2 5
66#define MEM_CONNID_MICOUT1 12
67#define MEM_CONNID_MICOUT2 14
68#define MEM_CONNID_WUH 10
69#define MEM_CONNID_DSP 16
70#define MEM_CONNID_DMIC 100
71
72#define SCP_SET 0
73#define SCP_GET 1
74
Ian Minett01ef7db2012-09-20 20:29:16 -070075#define EFX_FILE "ctefx.bin"
76
77MODULE_FIRMWARE(EFX_FILE);
78
Ian Minettef6b2ea2012-12-20 18:53:33 -080079static char *dirstr[2] = { "Playback", "Capture" };
80
81enum {
82 SPEAKER_OUT,
83 HEADPHONE_OUT
84};
85
86enum {
87 DIGITAL_MIC,
88 LINE_MIC_IN
89};
90
91enum {
92#define VNODE_START_NID 0x80
93 VNID_SPK = VNODE_START_NID, /* Speaker vnid */
94 VNID_MIC,
95 VNID_HP_SEL,
96 VNID_AMIC1_SEL,
97 VNID_HP_ASEL,
98 VNID_AMIC1_ASEL,
99 VNODE_END_NID,
100#define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
101
102#define EFFECT_START_NID 0x90
103#define OUT_EFFECT_START_NID EFFECT_START_NID
104 SURROUND = OUT_EFFECT_START_NID,
105 CRYSTALIZER,
106 DIALOG_PLUS,
107 SMART_VOLUME,
108 X_BASS,
109 EQUALIZER,
110 OUT_EFFECT_END_NID,
111#define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
112
113#define IN_EFFECT_START_NID OUT_EFFECT_END_NID
114 ECHO_CANCELLATION = IN_EFFECT_START_NID,
115 VOICE_FOCUS,
116 MIC_SVM,
117 NOISE_REDUCTION,
118 IN_EFFECT_END_NID,
119#define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
120
121 VOICEFX = IN_EFFECT_END_NID,
122 PLAY_ENHANCEMENT,
123 CRYSTAL_VOICE,
124 EFFECT_END_NID
125#define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
126};
127
128/* Effects values size*/
129#define EFFECT_VALS_MAX_COUNT 12
130
131struct ct_effect {
132 char name[44];
133 hda_nid_t nid;
134 int mid; /*effect module ID*/
135 int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
136 int direct; /* 0:output; 1:input*/
137 int params; /* number of default non-on/off params */
138 /*effect default values, 1st is on/off. */
139 unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
140};
141
142#define EFX_DIR_OUT 0
143#define EFX_DIR_IN 1
144
145static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
146 { .name = "Surround",
147 .nid = SURROUND,
148 .mid = 0x96,
149 .reqs = {0, 1},
150 .direct = EFX_DIR_OUT,
151 .params = 1,
152 .def_vals = {0x3F800000, 0x3F2B851F}
153 },
154 { .name = "Crystalizer",
155 .nid = CRYSTALIZER,
156 .mid = 0x96,
157 .reqs = {7, 8},
158 .direct = EFX_DIR_OUT,
159 .params = 1,
160 .def_vals = {0x3F800000, 0x3F266666}
161 },
162 { .name = "Dialog Plus",
163 .nid = DIALOG_PLUS,
164 .mid = 0x96,
165 .reqs = {2, 3},
166 .direct = EFX_DIR_OUT,
167 .params = 1,
168 .def_vals = {0x00000000, 0x3F000000}
169 },
170 { .name = "Smart Volume",
171 .nid = SMART_VOLUME,
172 .mid = 0x96,
173 .reqs = {4, 5, 6},
174 .direct = EFX_DIR_OUT,
175 .params = 2,
176 .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
177 },
178 { .name = "X-Bass",
179 .nid = X_BASS,
180 .mid = 0x96,
181 .reqs = {24, 23, 25},
182 .direct = EFX_DIR_OUT,
183 .params = 2,
184 .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
185 },
186 { .name = "Equalizer",
187 .nid = EQUALIZER,
188 .mid = 0x96,
189 .reqs = {9, 10, 11, 12, 13, 14,
190 15, 16, 17, 18, 19, 20},
191 .direct = EFX_DIR_OUT,
192 .params = 11,
193 .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
194 0x00000000, 0x00000000, 0x00000000, 0x00000000,
195 0x00000000, 0x00000000, 0x00000000, 0x00000000}
196 },
197 { .name = "Echo Cancellation",
198 .nid = ECHO_CANCELLATION,
199 .mid = 0x95,
200 .reqs = {0, 1, 2, 3},
201 .direct = EFX_DIR_IN,
202 .params = 3,
203 .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
204 },
205 { .name = "Voice Focus",
206 .nid = VOICE_FOCUS,
207 .mid = 0x95,
208 .reqs = {6, 7, 8, 9},
209 .direct = EFX_DIR_IN,
210 .params = 3,
211 .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
212 },
213 { .name = "Mic SVM",
214 .nid = MIC_SVM,
215 .mid = 0x95,
216 .reqs = {44, 45},
217 .direct = EFX_DIR_IN,
218 .params = 1,
219 .def_vals = {0x00000000, 0x3F3D70A4}
220 },
221 { .name = "Noise Reduction",
222 .nid = NOISE_REDUCTION,
223 .mid = 0x95,
224 .reqs = {4, 5},
225 .direct = EFX_DIR_IN,
226 .params = 1,
227 .def_vals = {0x3F800000, 0x3F000000}
228 },
229 { .name = "VoiceFX",
230 .nid = VOICEFX,
231 .mid = 0x95,
232 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
233 .direct = EFX_DIR_IN,
234 .params = 8,
235 .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
236 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
237 0x00000000}
238 }
239};
240
241/* Tuning controls */
242#ifdef ENABLE_TUNING_CONTROLS
243
244enum {
245#define TUNING_CTL_START_NID 0xC0
246 WEDGE_ANGLE = TUNING_CTL_START_NID,
247 SVM_LEVEL,
248 EQUALIZER_BAND_0,
249 EQUALIZER_BAND_1,
250 EQUALIZER_BAND_2,
251 EQUALIZER_BAND_3,
252 EQUALIZER_BAND_4,
253 EQUALIZER_BAND_5,
254 EQUALIZER_BAND_6,
255 EQUALIZER_BAND_7,
256 EQUALIZER_BAND_8,
257 EQUALIZER_BAND_9,
258 TUNING_CTL_END_NID
259#define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
260};
261
262struct ct_tuning_ctl {
263 char name[44];
264 hda_nid_t parent_nid;
265 hda_nid_t nid;
266 int mid; /*effect module ID*/
267 int req; /*effect module request*/
268 int direct; /* 0:output; 1:input*/
269 unsigned int def_val;/*effect default values*/
270};
271
272static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
273 { .name = "Wedge Angle",
274 .parent_nid = VOICE_FOCUS,
275 .nid = WEDGE_ANGLE,
276 .mid = 0x95,
277 .req = 8,
278 .direct = EFX_DIR_IN,
279 .def_val = 0x41F00000
280 },
281 { .name = "SVM Level",
282 .parent_nid = MIC_SVM,
283 .nid = SVM_LEVEL,
284 .mid = 0x95,
285 .req = 45,
286 .direct = EFX_DIR_IN,
287 .def_val = 0x3F3D70A4
288 },
289 { .name = "EQ Band0",
290 .parent_nid = EQUALIZER,
291 .nid = EQUALIZER_BAND_0,
292 .mid = 0x96,
293 .req = 11,
294 .direct = EFX_DIR_OUT,
295 .def_val = 0x00000000
296 },
297 { .name = "EQ Band1",
298 .parent_nid = EQUALIZER,
299 .nid = EQUALIZER_BAND_1,
300 .mid = 0x96,
301 .req = 12,
302 .direct = EFX_DIR_OUT,
303 .def_val = 0x00000000
304 },
305 { .name = "EQ Band2",
306 .parent_nid = EQUALIZER,
307 .nid = EQUALIZER_BAND_2,
308 .mid = 0x96,
309 .req = 13,
310 .direct = EFX_DIR_OUT,
311 .def_val = 0x00000000
312 },
313 { .name = "EQ Band3",
314 .parent_nid = EQUALIZER,
315 .nid = EQUALIZER_BAND_3,
316 .mid = 0x96,
317 .req = 14,
318 .direct = EFX_DIR_OUT,
319 .def_val = 0x00000000
320 },
321 { .name = "EQ Band4",
322 .parent_nid = EQUALIZER,
323 .nid = EQUALIZER_BAND_4,
324 .mid = 0x96,
325 .req = 15,
326 .direct = EFX_DIR_OUT,
327 .def_val = 0x00000000
328 },
329 { .name = "EQ Band5",
330 .parent_nid = EQUALIZER,
331 .nid = EQUALIZER_BAND_5,
332 .mid = 0x96,
333 .req = 16,
334 .direct = EFX_DIR_OUT,
335 .def_val = 0x00000000
336 },
337 { .name = "EQ Band6",
338 .parent_nid = EQUALIZER,
339 .nid = EQUALIZER_BAND_6,
340 .mid = 0x96,
341 .req = 17,
342 .direct = EFX_DIR_OUT,
343 .def_val = 0x00000000
344 },
345 { .name = "EQ Band7",
346 .parent_nid = EQUALIZER,
347 .nid = EQUALIZER_BAND_7,
348 .mid = 0x96,
349 .req = 18,
350 .direct = EFX_DIR_OUT,
351 .def_val = 0x00000000
352 },
353 { .name = "EQ Band8",
354 .parent_nid = EQUALIZER,
355 .nid = EQUALIZER_BAND_8,
356 .mid = 0x96,
357 .req = 19,
358 .direct = EFX_DIR_OUT,
359 .def_val = 0x00000000
360 },
361 { .name = "EQ Band9",
362 .parent_nid = EQUALIZER,
363 .nid = EQUALIZER_BAND_9,
364 .mid = 0x96,
365 .req = 20,
366 .direct = EFX_DIR_OUT,
367 .def_val = 0x00000000
368 }
369};
370#endif
371
372/* Voice FX Presets */
373#define VOICEFX_MAX_PARAM_COUNT 9
374
375struct ct_voicefx {
376 char *name;
377 hda_nid_t nid;
378 int mid;
379 int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
380};
381
382struct ct_voicefx_preset {
383 char *name; /*preset name*/
384 unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
385};
386
387struct ct_voicefx ca0132_voicefx = {
388 .name = "VoiceFX Capture Switch",
389 .nid = VOICEFX,
390 .mid = 0x95,
391 .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
392};
393
394struct ct_voicefx_preset ca0132_voicefx_presets[] = {
395 { .name = "Neutral",
396 .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
397 0x44FA0000, 0x3F800000, 0x3F800000,
398 0x3F800000, 0x00000000, 0x00000000 }
399 },
400 { .name = "Female2Male",
401 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
402 0x44FA0000, 0x3F19999A, 0x3F866666,
403 0x3F800000, 0x00000000, 0x00000000 }
404 },
405 { .name = "Male2Female",
406 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
407 0x450AC000, 0x4017AE14, 0x3F6B851F,
408 0x3F800000, 0x00000000, 0x00000000 }
409 },
410 { .name = "ScrappyKid",
411 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
412 0x44FA0000, 0x40400000, 0x3F28F5C3,
413 0x3F800000, 0x00000000, 0x00000000 }
414 },
415 { .name = "Elderly",
416 .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
417 0x44E10000, 0x3FB33333, 0x3FB9999A,
418 0x3F800000, 0x3E3A2E43, 0x00000000 }
419 },
420 { .name = "Orc",
421 .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
422 0x45098000, 0x3F266666, 0x3FC00000,
423 0x3F800000, 0x00000000, 0x00000000 }
424 },
425 { .name = "Elf",
426 .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
427 0x45193000, 0x3F8E147B, 0x3F75C28F,
428 0x3F800000, 0x00000000, 0x00000000 }
429 },
430 { .name = "Dwarf",
431 .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
432 0x45007000, 0x3F451EB8, 0x3F7851EC,
433 0x3F800000, 0x00000000, 0x00000000 }
434 },
435 { .name = "AlienBrute",
436 .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
437 0x451F6000, 0x3F266666, 0x3FA7D945,
438 0x3F800000, 0x3CF5C28F, 0x00000000 }
439 },
440 { .name = "Robot",
441 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
442 0x44FA0000, 0x3FB2718B, 0x3F800000,
443 0xBC07010E, 0x00000000, 0x00000000 }
444 },
445 { .name = "Marine",
446 .vals = { 0x3F800000, 0x43C20000, 0x44906000,
447 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
448 0x3F0A3D71, 0x00000000, 0x00000000 }
449 },
450 { .name = "Emo",
451 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
452 0x44FA0000, 0x3F800000, 0x3F800000,
453 0x3E4CCCCD, 0x00000000, 0x00000000 }
454 },
455 { .name = "DeepVoice",
456 .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
457 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
458 0x3F800000, 0x00000000, 0x00000000 }
459 },
460 { .name = "Munchkin",
461 .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
462 0x44FA0000, 0x3F800000, 0x3F1A043C,
463 0x3F800000, 0x00000000, 0x00000000 }
464 }
465};
466
Ian Minett95c6e9c2011-06-15 15:35:17 -0700467enum hda_cmd_vendor_io {
468 /* for DspIO node */
469 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
470 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
471
472 VENDOR_DSPIO_STATUS = 0xF01,
473 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
474 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
475 VENDOR_DSPIO_DSP_INIT = 0x703,
476 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
477 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
478
479 /* for ChipIO node */
480 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
481 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
482 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
483 VENDOR_CHIPIO_DATA_LOW = 0x300,
484 VENDOR_CHIPIO_DATA_HIGH = 0x400,
485
486 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
487 VENDOR_CHIPIO_STATUS = 0xF01,
488 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
489 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
490
Ian Minett4aa3bb02012-09-20 20:29:15 -0700491 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
492 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
493
Ian Minett95c6e9c2011-06-15 15:35:17 -0700494 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700495 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700496
497 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
498 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
499 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
500 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
501 VENDOR_CHIPIO_FLAG_SET = 0x70F,
502 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700503 VENDOR_CHIPIO_PARAM_SET = 0x710,
504 VENDOR_CHIPIO_PARAM_GET = 0xF10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700505
506 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
507 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
508 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
509 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
510
Ian Minett4aa3bb02012-09-20 20:29:15 -0700511 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
512 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
513 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
514 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
515
516 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
517 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
518 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
519 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
520 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
521 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
522
523 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
Ian Minett95c6e9c2011-06-15 15:35:17 -0700524};
525
526/*
527 * Control flag IDs
528 */
529enum control_flag_id {
530 /* Connection manager stream setup is bypassed/enabled */
531 CONTROL_FLAG_C_MGR = 0,
532 /* DSP DMA is bypassed/enabled */
533 CONTROL_FLAG_DMA = 1,
534 /* 8051 'idle' mode is disabled/enabled */
535 CONTROL_FLAG_IDLE_ENABLE = 2,
536 /* Tracker for the SPDIF-in path is bypassed/enabled */
537 CONTROL_FLAG_TRACKER = 3,
538 /* DigitalOut to Spdif2Out connection is disabled/enabled */
539 CONTROL_FLAG_SPDIF2OUT = 4,
540 /* Digital Microphone is disabled/enabled */
541 CONTROL_FLAG_DMIC = 5,
542 /* ADC_B rate is 48 kHz/96 kHz */
543 CONTROL_FLAG_ADC_B_96KHZ = 6,
544 /* ADC_C rate is 48 kHz/96 kHz */
545 CONTROL_FLAG_ADC_C_96KHZ = 7,
546 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
547 CONTROL_FLAG_DAC_96KHZ = 8,
548 /* DSP rate is 48 kHz/96 kHz */
549 CONTROL_FLAG_DSP_96KHZ = 9,
550 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
551 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
552 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
553 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
554 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
555 CONTROL_FLAG_DECODE_LOOP = 12,
556 /* De-emphasis filter on DAC-1 disabled/enabled */
557 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
558 /* De-emphasis filter on DAC-2 disabled/enabled */
559 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
560 /* De-emphasis filter on DAC-3 disabled/enabled */
561 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
562 /* High-pass filter on ADC_B disabled/enabled */
563 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
564 /* High-pass filter on ADC_C disabled/enabled */
565 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
566 /* Common mode on Port_A disabled/enabled */
567 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
568 /* Common mode on Port_D disabled/enabled */
569 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
570 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
571 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
572 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700573 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700574 /* ASI rate is 48kHz/96kHz */
575 CONTROL_FLAG_ASI_96KHZ = 22,
576 /* DAC power settings able to control attached ports no/yes */
577 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
578 /* Clock Stop OK reporting is disabled/enabled */
579 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
580 /* Number of control flags */
581 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
582};
583
584/*
585 * Control parameter IDs
586 */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700587enum control_param_id {
Ian Minettef6b2ea2012-12-20 18:53:33 -0800588 /* 0: None, 1: Mic1In*/
589 CONTROL_PARAM_VIP_SOURCE = 1,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700590 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
591 CONTROL_PARAM_SPDIF1_SOURCE = 2,
Ian Minettef6b2ea2012-12-20 18:53:33 -0800592 /* Port A output stage gain setting to use when 16 Ohm output
593 * impedance is selected*/
594 CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
595 /* Port D output stage gain setting to use when 16 Ohm output
596 * impedance is selected*/
597 CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700598
599 /* Stream Control */
600
601 /* Select stream with the given ID */
602 CONTROL_PARAM_STREAM_ID = 24,
603 /* Source connection point for the selected stream */
604 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
605 /* Destination connection point for the selected stream */
606 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
607 /* Number of audio channels in the selected stream */
608 CONTROL_PARAM_STREAMS_CHANNELS = 27,
609 /*Enable control for the selected stream */
610 CONTROL_PARAM_STREAM_CONTROL = 28,
611
612 /* Connection Point Control */
613
614 /* Select connection point with the given ID */
615 CONTROL_PARAM_CONN_POINT_ID = 29,
616 /* Connection point sample rate */
617 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
618
619 /* Node Control */
620
621 /* Select HDA node with the given ID */
622 CONTROL_PARAM_NODE_ID = 31
623};
624
625/*
626 * Dsp Io Status codes
627 */
628enum hda_vendor_status_dspio {
629 /* Success */
630 VENDOR_STATUS_DSPIO_OK = 0x00,
631 /* Busy, unable to accept new command, the host must retry */
632 VENDOR_STATUS_DSPIO_BUSY = 0x01,
633 /* SCP command queue is full */
634 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
635 /* SCP response queue is empty */
636 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
637};
638
639/*
640 * Chip Io Status codes
641 */
642enum hda_vendor_status_chipio {
643 /* Success */
644 VENDOR_STATUS_CHIPIO_OK = 0x00,
645 /* Busy, unable to accept new command, the host must retry */
646 VENDOR_STATUS_CHIPIO_BUSY = 0x01
647};
648
649/*
650 * CA0132 sample rate
651 */
652enum ca0132_sample_rate {
653 SR_6_000 = 0x00,
654 SR_8_000 = 0x01,
655 SR_9_600 = 0x02,
656 SR_11_025 = 0x03,
657 SR_16_000 = 0x04,
658 SR_22_050 = 0x05,
659 SR_24_000 = 0x06,
660 SR_32_000 = 0x07,
661 SR_44_100 = 0x08,
662 SR_48_000 = 0x09,
663 SR_88_200 = 0x0A,
664 SR_96_000 = 0x0B,
665 SR_144_000 = 0x0C,
666 SR_176_400 = 0x0D,
667 SR_192_000 = 0x0E,
668 SR_384_000 = 0x0F,
669
670 SR_COUNT = 0x10,
671
672 SR_RATE_UNKNOWN = 0x1F
673};
674
675/*
676 * Scp Helper function
677 */
678enum get_set {
679 IS_SET = 0,
680 IS_GET = 1,
681};
682
683/*
684 * Duplicated from ca0110 codec
685 */
686
687static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
688{
689 if (pin) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200690 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700691 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
692 snd_hda_codec_write(codec, pin, 0,
693 AC_VERB_SET_AMP_GAIN_MUTE,
694 AMP_OUT_UNMUTE);
695 }
Takashi Iwai8e13fc12012-08-08 17:26:54 +0200696 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
Ian Minett95c6e9c2011-06-15 15:35:17 -0700697 snd_hda_codec_write(codec, dac, 0,
698 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
699}
700
701static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
702{
703 if (pin) {
Takashi Iwai47408602012-04-20 13:06:53 +0200704 snd_hda_set_pin_ctl(codec, pin, PIN_IN |
705 snd_hda_get_default_vref(codec, pin));
Ian Minett95c6e9c2011-06-15 15:35:17 -0700706 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
707 snd_hda_codec_write(codec, pin, 0,
708 AC_VERB_SET_AMP_GAIN_MUTE,
709 AMP_IN_UNMUTE(0));
710 }
Takashi Iwai8e13fc12012-08-08 17:26:54 +0200711 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP))
Ian Minett95c6e9c2011-06-15 15:35:17 -0700712 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
713 AMP_IN_UNMUTE(0));
714}
715
Ian Minett95c6e9c2011-06-15 15:35:17 -0700716static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
717 int chan, int dir)
718{
719 char namestr[44];
720 int type = dir ? HDA_INPUT : HDA_OUTPUT;
721 struct snd_kcontrol_new knew =
722 HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
David Henningssonc41999a2012-08-20 11:17:00 +0200723 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_MUTE) == 0) {
724 snd_printdd("Skipping '%s %s Switch' (no mute on node 0x%x)\n", pfx, dirstr[dir], nid);
725 return 0;
726 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700727 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
728 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
729}
730
731static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
732 int chan, int dir)
733{
734 char namestr[44];
735 int type = dir ? HDA_INPUT : HDA_OUTPUT;
736 struct snd_kcontrol_new knew =
737 HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
David Henningssonc41999a2012-08-20 11:17:00 +0200738 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_NUM_STEPS) == 0) {
739 snd_printdd("Skipping '%s %s Volume' (no amp on node 0x%x)\n", pfx, dirstr[dir], nid);
740 return 0;
741 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700742 sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
743 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
744}
745
746#define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
747#define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
748#define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
749#define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
750#define add_mono_switch(codec, nid, pfx, chan) \
751 _add_switch(codec, nid, pfx, chan, 0)
752#define add_mono_volume(codec, nid, pfx, chan) \
753 _add_volume(codec, nid, pfx, chan, 0)
754#define add_in_mono_switch(codec, nid, pfx, chan) \
755 _add_switch(codec, nid, pfx, chan, 1)
756#define add_in_mono_volume(codec, nid, pfx, chan) \
757 _add_volume(codec, nid, pfx, chan, 1)
758
Ian Minett01ef7db2012-09-20 20:29:16 -0700759enum dsp_download_state {
760 DSP_DOWNLOAD_FAILED = -1,
761 DSP_DOWNLOAD_INIT = 0,
762 DSP_DOWNLOADING = 1,
763 DSP_DOWNLOADED = 2
764};
765
Ian Minett01ef7db2012-09-20 20:29:16 -0700766/* retrieve parameters from hda format */
767#define get_hdafmt_chs(fmt) (fmt & 0xf)
768#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
769#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
770#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700771
772/*
773 * CA0132 specific
774 */
775
776struct ca0132_spec {
777 struct auto_pin_cfg autocfg;
778 struct hda_multi_out multiout;
779 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
780 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
781 hda_nid_t hp_dac;
782 hda_nid_t input_pins[AUTO_PIN_LAST];
783 hda_nid_t adcs[AUTO_PIN_LAST];
784 hda_nid_t dig_out;
785 hda_nid_t dig_in;
786 unsigned int num_inputs;
787 long curr_hp_switch;
788 long curr_hp_volume[2];
789 long curr_speaker_switch;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700790 const char *input_labels[AUTO_PIN_LAST];
791 struct hda_pcm pcm_rec[2]; /* PCM information */
Ian Minett01ef7db2012-09-20 20:29:16 -0700792
793 /* chip access */
794 struct mutex chipio_mutex; /* chip access mutex */
795 u32 curr_chip_addx;
796
797 /* DSP download related */
798 enum dsp_download_state dsp_state;
799 unsigned int dsp_stream_id;
800 unsigned int wait_scp;
801 unsigned int wait_scp_header;
802 unsigned int wait_num_data;
803 unsigned int scp_resp_header;
804 unsigned int scp_resp_data[4];
805 unsigned int scp_resp_count;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700806};
807
Ian Minett01ef7db2012-09-20 20:29:16 -0700808/*
809 * CA0132 codec access
810 */
811unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
812 unsigned int verb, unsigned int parm, unsigned int *res)
813{
814 unsigned int response;
815 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
816 *res = response;
817
818 return ((response == -1) ? -1 : 0);
819}
820
821static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
822 unsigned short converter_format, unsigned int *res)
823{
824 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
825 converter_format & 0xffff, res);
826}
827
828static int codec_set_converter_stream_channel(struct hda_codec *codec,
829 hda_nid_t nid, unsigned char stream,
830 unsigned char channel, unsigned int *res)
831{
832 unsigned char converter_stream_channel = 0;
833
834 converter_stream_channel = (stream << 4) | (channel & 0x0f);
835 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
836 converter_stream_channel, res);
837}
838
Ian Minett95c6e9c2011-06-15 15:35:17 -0700839/* Chip access helper function */
840static int chipio_send(struct hda_codec *codec,
841 unsigned int reg,
842 unsigned int data)
843{
844 unsigned int res;
845 int retry = 50;
846
847 /* send bits of data specified by reg */
848 do {
849 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
850 reg, data);
851 if (res == VENDOR_STATUS_CHIPIO_OK)
852 return 0;
853 } while (--retry);
854 return -EIO;
855}
856
857/*
858 * Write chip address through the vendor widget -- NOT protected by the Mutex!
859 */
860static int chipio_write_address(struct hda_codec *codec,
861 unsigned int chip_addx)
862{
Ian Minett4861af82012-09-20 20:29:20 -0700863 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700864 int res;
865
Ian Minett4861af82012-09-20 20:29:20 -0700866 if (spec->curr_chip_addx == chip_addx)
867 return 0;
868
Ian Minett95c6e9c2011-06-15 15:35:17 -0700869 /* send low 16 bits of the address */
870 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
871 chip_addx & 0xffff);
872
873 if (res != -EIO) {
874 /* send high 16 bits of the address */
875 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
876 chip_addx >> 16);
877 }
878
Ian Minett4861af82012-09-20 20:29:20 -0700879 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
880
Ian Minett95c6e9c2011-06-15 15:35:17 -0700881 return res;
882}
883
884/*
885 * Write data through the vendor widget -- NOT protected by the Mutex!
886 */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700887static int chipio_write_data(struct hda_codec *codec, unsigned int data)
888{
889 int res;
890
891 /* send low 16 bits of the data */
892 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
893
894 if (res != -EIO) {
895 /* send high 16 bits of the data */
896 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
897 data >> 16);
898 }
899
900 return res;
901}
902
Ian Minettd5c21b82012-09-20 20:29:18 -0700903/*
904 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
905 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700906static int chipio_write_data_multiple(struct hda_codec *codec,
907 const u32 *data,
908 unsigned int count)
909{
910 int status = 0;
911
912 if (data == NULL) {
913 snd_printdd(KERN_ERR "chipio_write_data null ptr");
914 return -EINVAL;
915 }
916
917 while ((count-- != 0) && (status == 0))
918 status = chipio_write_data(codec, *data++);
919
920 return status;
921}
922
923
Ian Minett95c6e9c2011-06-15 15:35:17 -0700924/*
925 * Read data through the vendor widget -- NOT protected by the Mutex!
926 */
927static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
928{
929 int res;
930
931 /* post read */
932 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
933
934 if (res != -EIO) {
935 /* read status */
936 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
937 }
938
939 if (res != -EIO) {
940 /* read data */
941 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
942 VENDOR_CHIPIO_HIC_READ_DATA,
943 0);
944 }
945
946 return res;
947}
948
949/*
950 * Write given value to the given address through the chip I/O widget.
951 * protected by the Mutex
952 */
953static int chipio_write(struct hda_codec *codec,
954 unsigned int chip_addx, const unsigned int data)
955{
956 struct ca0132_spec *spec = codec->spec;
957 int err;
958
959 mutex_lock(&spec->chipio_mutex);
960
961 /* write the address, and if successful proceed to write data */
962 err = chipio_write_address(codec, chip_addx);
963 if (err < 0)
964 goto exit;
965
966 err = chipio_write_data(codec, data);
967 if (err < 0)
968 goto exit;
969
970exit:
971 mutex_unlock(&spec->chipio_mutex);
972 return err;
973}
974
Ian Minettd5c21b82012-09-20 20:29:18 -0700975/*
976 * Write multiple values to the given address through the chip I/O widget.
977 * protected by the Mutex
978 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700979static int chipio_write_multiple(struct hda_codec *codec,
980 u32 chip_addx,
981 const u32 *data,
982 unsigned int count)
983{
984 struct ca0132_spec *spec = codec->spec;
985 int status;
986
987 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -0700988 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -0700989 if (status < 0)
990 goto error;
991
992 status = chipio_write_data_multiple(codec, data, count);
993error:
994 mutex_unlock(&spec->chipio_mutex);
995
996 return status;
997}
998
Ian Minett95c6e9c2011-06-15 15:35:17 -0700999/*
1000 * Read the given address through the chip I/O widget
1001 * protected by the Mutex
1002 */
1003static int chipio_read(struct hda_codec *codec,
1004 unsigned int chip_addx, unsigned int *data)
1005{
1006 struct ca0132_spec *spec = codec->spec;
1007 int err;
1008
1009 mutex_lock(&spec->chipio_mutex);
1010
1011 /* write the address, and if successful proceed to write data */
1012 err = chipio_write_address(codec, chip_addx);
1013 if (err < 0)
1014 goto exit;
1015
1016 err = chipio_read_data(codec, data);
1017 if (err < 0)
1018 goto exit;
1019
1020exit:
1021 mutex_unlock(&spec->chipio_mutex);
1022 return err;
1023}
1024
Ian Minettd5c21b82012-09-20 20:29:18 -07001025/*
1026 * Set chip control flags through the chip I/O widget.
1027 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001028static void chipio_set_control_flag(struct hda_codec *codec,
1029 enum control_flag_id flag_id,
1030 bool flag_state)
1031{
1032 unsigned int val;
1033 unsigned int flag_bit;
1034
1035 flag_bit = (flag_state ? 1 : 0);
1036 val = (flag_bit << 7) | (flag_id);
1037 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1038 VENDOR_CHIPIO_FLAG_SET, val);
1039}
1040
Ian Minettd5c21b82012-09-20 20:29:18 -07001041/*
1042 * Set chip parameters through the chip I/O widget.
1043 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001044static void chipio_set_control_param(struct hda_codec *codec,
1045 enum control_param_id param_id, int param_val)
1046{
1047 struct ca0132_spec *spec = codec->spec;
1048 int val;
1049
1050 if ((param_id < 32) && (param_val < 8)) {
1051 val = (param_val << 5) | (param_id);
1052 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1053 VENDOR_CHIPIO_PARAM_SET, val);
1054 } else {
1055 mutex_lock(&spec->chipio_mutex);
1056 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1057 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1058 VENDOR_CHIPIO_PARAM_EX_ID_SET,
1059 param_id);
1060 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1061 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1062 param_val);
1063 }
1064 mutex_unlock(&spec->chipio_mutex);
1065 }
1066}
1067
Ian Minettd5c21b82012-09-20 20:29:18 -07001068/*
1069 * Set sampling rate of the connection point.
1070 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001071static void chipio_set_conn_rate(struct hda_codec *codec,
1072 int connid, enum ca0132_sample_rate rate)
1073{
1074 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1075 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1076 rate);
1077}
1078
Ian Minettd5c21b82012-09-20 20:29:18 -07001079/*
1080 * Enable clocks.
1081 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001082static void chipio_enable_clocks(struct hda_codec *codec)
1083{
1084 struct ca0132_spec *spec = codec->spec;
1085
1086 mutex_lock(&spec->chipio_mutex);
1087 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1088 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
1089 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1090 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1091 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1092 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
1093 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1094 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
1095 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1096 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
1097 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1098 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
1099 mutex_unlock(&spec->chipio_mutex);
1100}
1101
1102/*
1103 * CA0132 DSP IO stuffs
1104 */
1105static int dspio_send(struct hda_codec *codec, unsigned int reg,
1106 unsigned int data)
1107{
1108 unsigned int res;
1109 int retry = 50;
1110
1111 /* send bits of data specified by reg to dsp */
1112 do {
1113 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
1114 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
1115 return res;
1116 } while (--retry);
1117
1118 return -EIO;
1119}
1120
Ian Minettd5c21b82012-09-20 20:29:18 -07001121/*
1122 * Wait for DSP to be ready for commands
1123 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001124static void dspio_write_wait(struct hda_codec *codec)
1125{
Ian Minett4861af82012-09-20 20:29:20 -07001126 int status;
1127 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -07001128
Ian Minett01ef7db2012-09-20 20:29:16 -07001129 do {
Ian Minett4861af82012-09-20 20:29:20 -07001130 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1131 VENDOR_DSPIO_STATUS, 0);
1132 if ((status == VENDOR_STATUS_DSPIO_OK) ||
1133 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
1134 break;
1135 msleep(1);
1136 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -07001137}
1138
Ian Minettd5c21b82012-09-20 20:29:18 -07001139/*
1140 * Write SCP data to DSP
1141 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001142static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
1143{
1144 struct ca0132_spec *spec = codec->spec;
1145 int status;
1146
1147 dspio_write_wait(codec);
1148
1149 mutex_lock(&spec->chipio_mutex);
1150 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
1151 scp_data & 0xffff);
1152 if (status < 0)
1153 goto error;
1154
1155 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
1156 scp_data >> 16);
1157 if (status < 0)
1158 goto error;
1159
1160 /* OK, now check if the write itself has executed*/
1161 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
1162 VENDOR_DSPIO_STATUS, 0);
1163error:
1164 mutex_unlock(&spec->chipio_mutex);
1165
1166 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
1167 -EIO : 0;
1168}
1169
Ian Minettd5c21b82012-09-20 20:29:18 -07001170/*
1171 * Write multiple SCP data to DSP
1172 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001173static int dspio_write_multiple(struct hda_codec *codec,
1174 unsigned int *buffer, unsigned int size)
1175{
1176 int status = 0;
1177 unsigned int count;
1178
1179 if ((buffer == NULL))
1180 return -EINVAL;
1181
1182 count = 0;
1183 while (count < size) {
1184 status = dspio_write(codec, *buffer++);
1185 if (status != 0)
1186 break;
1187 count++;
1188 }
1189
1190 return status;
1191}
1192
Ian Minettd5c21b82012-09-20 20:29:18 -07001193/*
1194 * Construct the SCP header using corresponding fields
1195 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001196static inline unsigned int
1197make_scp_header(unsigned int target_id, unsigned int source_id,
1198 unsigned int get_flag, unsigned int req,
1199 unsigned int device_flag, unsigned int resp_flag,
1200 unsigned int error_flag, unsigned int data_size)
1201{
1202 unsigned int header = 0;
1203
1204 header = (data_size & 0x1f) << 27;
1205 header |= (error_flag & 0x01) << 26;
1206 header |= (resp_flag & 0x01) << 25;
1207 header |= (device_flag & 0x01) << 24;
1208 header |= (req & 0x7f) << 17;
1209 header |= (get_flag & 0x01) << 16;
1210 header |= (source_id & 0xff) << 8;
1211 header |= target_id & 0xff;
1212
1213 return header;
1214}
1215
Ian Minettd5c21b82012-09-20 20:29:18 -07001216/*
1217 * Extract corresponding fields from SCP header
1218 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001219static inline void
1220extract_scp_header(unsigned int header,
1221 unsigned int *target_id, unsigned int *source_id,
1222 unsigned int *get_flag, unsigned int *req,
1223 unsigned int *device_flag, unsigned int *resp_flag,
1224 unsigned int *error_flag, unsigned int *data_size)
1225{
1226 if (data_size)
1227 *data_size = (header >> 27) & 0x1f;
1228 if (error_flag)
1229 *error_flag = (header >> 26) & 0x01;
1230 if (resp_flag)
1231 *resp_flag = (header >> 25) & 0x01;
1232 if (device_flag)
1233 *device_flag = (header >> 24) & 0x01;
1234 if (req)
1235 *req = (header >> 17) & 0x7f;
1236 if (get_flag)
1237 *get_flag = (header >> 16) & 0x01;
1238 if (source_id)
1239 *source_id = (header >> 8) & 0xff;
1240 if (target_id)
1241 *target_id = header & 0xff;
1242}
1243
1244#define SCP_MAX_DATA_WORDS (16)
1245
1246/* Structure to contain any SCP message */
1247struct scp_msg {
1248 unsigned int hdr;
1249 unsigned int data[SCP_MAX_DATA_WORDS];
1250};
1251
Ian Minettd5c21b82012-09-20 20:29:18 -07001252/*
1253 * Send SCP message to DSP
1254 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001255static int dspio_send_scp_message(struct hda_codec *codec,
1256 unsigned char *send_buf,
1257 unsigned int send_buf_size,
1258 unsigned char *return_buf,
1259 unsigned int return_buf_size,
1260 unsigned int *bytes_returned)
1261{
1262 struct ca0132_spec *spec = codec->spec;
1263 int retry;
1264 int status = -1;
1265 unsigned int scp_send_size = 0;
1266 unsigned int total_size;
1267 bool waiting_for_resp = false;
1268 unsigned int header;
1269 struct scp_msg *ret_msg;
1270 unsigned int resp_src_id, resp_target_id;
1271 unsigned int data_size, src_id, target_id, get_flag, device_flag;
1272
1273 if (bytes_returned)
1274 *bytes_returned = 0;
1275
1276 /* get scp header from buffer */
1277 header = *((unsigned int *)send_buf);
1278 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
1279 &device_flag, NULL, NULL, &data_size);
1280 scp_send_size = data_size + 1;
1281 total_size = (scp_send_size * 4);
1282
1283 if (send_buf_size < total_size)
1284 return -EINVAL;
1285
1286 if (get_flag || device_flag) {
1287 if (!return_buf || return_buf_size < 4 || !bytes_returned)
1288 return -EINVAL;
1289
1290 spec->wait_scp_header = *((unsigned int *)send_buf);
1291
1292 /* swap source id with target id */
1293 resp_target_id = src_id;
1294 resp_src_id = target_id;
1295 spec->wait_scp_header &= 0xffff0000;
1296 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
1297 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
1298 spec->wait_scp = 1;
1299 waiting_for_resp = true;
1300 }
1301
1302 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
1303 scp_send_size);
1304 if (status < 0) {
1305 spec->wait_scp = 0;
1306 return status;
1307 }
1308
1309 if (waiting_for_resp) {
1310 memset(return_buf, 0, return_buf_size);
1311 retry = 50;
1312 do {
1313 msleep(20);
1314 } while (spec->wait_scp && (--retry != 0));
1315 waiting_for_resp = false;
1316 if (retry != 0) {
1317 ret_msg = (struct scp_msg *)return_buf;
1318 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
1319 memcpy(&ret_msg->data, spec->scp_resp_data,
1320 spec->wait_num_data);
1321 *bytes_returned = (spec->scp_resp_count + 1) * 4;
1322 status = 0;
1323 } else {
1324 status = -EIO;
1325 }
1326 spec->wait_scp = 0;
1327 }
1328
1329 return status;
1330}
1331
Ian Minettd5c21b82012-09-20 20:29:18 -07001332/**
1333 * Prepare and send the SCP message to DSP
1334 * @codec: the HDA codec
1335 * @mod_id: ID of the DSP module to send the command
1336 * @req: ID of request to send to the DSP module
1337 * @dir: SET or GET
1338 * @data: pointer to the data to send with the request, request specific
1339 * @len: length of the data, in bytes
1340 * @reply: point to the buffer to hold data returned for a reply
1341 * @reply_len: length of the reply buffer returned from GET
1342 *
1343 * Returns zero or a negative error code.
1344 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001345static int dspio_scp(struct hda_codec *codec,
1346 int mod_id, int req, int dir, void *data, unsigned int len,
1347 void *reply, unsigned int *reply_len)
1348{
1349 int status = 0;
1350 struct scp_msg scp_send, scp_reply;
1351 unsigned int ret_bytes, send_size, ret_size;
1352 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
1353 unsigned int reply_data_size;
1354
1355 memset(&scp_send, 0, sizeof(scp_send));
1356 memset(&scp_reply, 0, sizeof(scp_reply));
1357
1358 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
1359 return -EINVAL;
1360
1361 if (dir == SCP_GET && reply == NULL) {
1362 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
1363 return -EINVAL;
1364 }
1365
1366 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
1367 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
1368 return -EINVAL;
1369 }
1370
1371 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
1372 0, 0, 0, len/sizeof(unsigned int));
1373 if (data != NULL && len > 0) {
1374 len = min((unsigned int)(sizeof(scp_send.data)), len);
1375 memcpy(scp_send.data, data, len);
1376 }
1377
1378 ret_bytes = 0;
1379 send_size = sizeof(unsigned int) + len;
1380 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1381 send_size, (unsigned char *)&scp_reply,
1382 sizeof(scp_reply), &ret_bytes);
1383
1384 if (status < 0) {
1385 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1386 return status;
1387 }
1388
1389 /* extract send and reply headers members */
1390 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1391 NULL, NULL, NULL, NULL, NULL);
1392 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1393 &reply_resp_flag, &reply_error_flag,
1394 &reply_data_size);
1395
1396 if (!send_get_flag)
1397 return 0;
1398
1399 if (reply_resp_flag && !reply_error_flag) {
1400 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1401 / sizeof(unsigned int);
1402
1403 if (*reply_len < ret_size*sizeof(unsigned int)) {
1404 snd_printdd(KERN_ERR "reply too long for buf");
1405 return -EINVAL;
1406 } else if (ret_size != reply_data_size) {
1407 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1408 return -EINVAL;
1409 } else {
1410 *reply_len = ret_size*sizeof(unsigned int);
1411 memcpy(reply, scp_reply.data, *reply_len);
1412 }
1413 } else {
1414 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1415 return -EIO;
1416 }
1417
1418 return status;
1419}
1420
Ian Minettd5c21b82012-09-20 20:29:18 -07001421/*
1422 * Allocate a DSP DMA channel via an SCP message
1423 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001424static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1425{
1426 int status = 0;
1427 unsigned int size = sizeof(dma_chan);
1428
1429 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1430 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1431 SCP_GET, NULL, 0, dma_chan, &size);
1432
1433 if (status < 0) {
1434 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1435 return status;
1436 }
1437
1438 if ((*dma_chan + 1) == 0) {
1439 snd_printdd(KERN_INFO "no free dma channels to allocate");
1440 return -EBUSY;
1441 }
1442
1443 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1444 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1445
1446 return status;
1447}
1448
Ian Minettd5c21b82012-09-20 20:29:18 -07001449/*
1450 * Free a DSP DMA via an SCP message
1451 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001452static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1453{
1454 int status = 0;
1455 unsigned int dummy = 0;
1456
1457 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1458 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1459
1460 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1461 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1462
1463 if (status < 0) {
1464 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1465 return status;
1466 }
1467
1468 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1469
1470 return status;
1471}
1472
1473/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001474 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001475 */
1476static int dsp_set_run_state(struct hda_codec *codec)
1477{
1478 unsigned int dbg_ctrl_reg;
1479 unsigned int halt_state;
1480 int err;
1481
1482 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1483 if (err < 0)
1484 return err;
1485
1486 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1487 DSP_DBGCNTL_STATE_LOBIT;
1488
1489 if (halt_state != 0) {
1490 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1491 DSP_DBGCNTL_SS_MASK);
1492 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1493 dbg_ctrl_reg);
1494 if (err < 0)
1495 return err;
1496
1497 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1498 DSP_DBGCNTL_EXEC_MASK;
1499 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1500 dbg_ctrl_reg);
1501 if (err < 0)
1502 return err;
1503 }
1504
1505 return 0;
1506}
1507
Ian Minettd5c21b82012-09-20 20:29:18 -07001508/*
1509 * Reset the DSP
1510 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001511static int dsp_reset(struct hda_codec *codec)
1512{
1513 unsigned int res;
1514 int retry = 20;
1515
1516 snd_printdd("dsp_reset\n");
1517 do {
1518 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1519 retry--;
1520 } while (res == -EIO && retry);
1521
1522 if (!retry) {
1523 snd_printdd("dsp_reset timeout\n");
1524 return -EIO;
1525 }
1526
1527 return 0;
1528}
1529
Ian Minettd5c21b82012-09-20 20:29:18 -07001530/*
1531 * Convert chip address to DSP address
1532 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001533static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1534 bool *code, bool *yram)
1535{
1536 *code = *yram = false;
1537
1538 if (UC_RANGE(chip_addx, 1)) {
1539 *code = true;
1540 return UC_OFF(chip_addx);
1541 } else if (X_RANGE_ALL(chip_addx, 1)) {
1542 return X_OFF(chip_addx);
1543 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1544 *yram = true;
1545 return Y_OFF(chip_addx);
1546 }
1547
1548 return (unsigned int)INVALID_CHIP_ADDRESS;
1549}
1550
Ian Minettd5c21b82012-09-20 20:29:18 -07001551/*
1552 * Check if the DSP DMA is active
1553 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001554static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1555{
1556 unsigned int dma_chnlstart_reg;
1557
1558 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1559
1560 return ((dma_chnlstart_reg & (1 <<
1561 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1562}
1563
1564static int dsp_dma_setup_common(struct hda_codec *codec,
1565 unsigned int chip_addx,
1566 unsigned int dma_chan,
1567 unsigned int port_map_mask,
1568 bool ovly)
1569{
1570 int status = 0;
1571 unsigned int chnl_prop;
1572 unsigned int dsp_addx;
1573 unsigned int active;
1574 bool code, yram;
1575
1576 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1577
1578 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1579 snd_printdd(KERN_ERR "dma chan num invalid");
1580 return -EINVAL;
1581 }
1582
1583 if (dsp_is_dma_active(codec, dma_chan)) {
1584 snd_printdd(KERN_ERR "dma already active");
1585 return -EBUSY;
1586 }
1587
1588 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1589
1590 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1591 snd_printdd(KERN_ERR "invalid chip addr");
1592 return -ENXIO;
1593 }
1594
1595 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1596 active = 0;
1597
1598 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1599
1600 if (ovly) {
1601 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1602 &chnl_prop);
1603
1604 if (status < 0) {
1605 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1606 return status;
1607 }
1608 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1609 }
1610
1611 if (!code)
1612 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1613 else
1614 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1615
1616 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1617
1618 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1619 if (status < 0) {
1620 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1621 return status;
1622 }
1623 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1624
1625 if (ovly) {
1626 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1627 &active);
1628
1629 if (status < 0) {
1630 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1631 return status;
1632 }
1633 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1634 }
1635
1636 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1637 DSPDMAC_ACTIVE_AAR_MASK;
1638
1639 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1640 if (status < 0) {
1641 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1642 return status;
1643 }
1644
1645 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1646
1647 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1648 port_map_mask);
1649 if (status < 0) {
1650 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1651 return status;
1652 }
1653 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1654
1655 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1656 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1657 if (status < 0) {
1658 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1659 return status;
1660 }
1661 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1662
1663 snd_printdd(
1664 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1665 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1666 chip_addx, dsp_addx, dma_chan,
1667 port_map_mask, chnl_prop, active);
1668
1669 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1670
1671 return 0;
1672}
1673
Ian Minettd5c21b82012-09-20 20:29:18 -07001674/*
1675 * Setup the DSP DMA per-transfer-specific registers
1676 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001677static int dsp_dma_setup(struct hda_codec *codec,
1678 unsigned int chip_addx,
1679 unsigned int count,
1680 unsigned int dma_chan)
1681{
1682 int status = 0;
1683 bool code, yram;
1684 unsigned int dsp_addx;
1685 unsigned int addr_field;
1686 unsigned int incr_field;
1687 unsigned int base_cnt;
1688 unsigned int cur_cnt;
1689 unsigned int dma_cfg = 0;
1690 unsigned int adr_ofs = 0;
1691 unsigned int xfr_cnt = 0;
1692 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1693 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1694
1695 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1696
1697 if (count > max_dma_count) {
1698 snd_printdd(KERN_ERR "count too big");
1699 return -EINVAL;
1700 }
1701
1702 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1703 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1704 snd_printdd(KERN_ERR "invalid chip addr");
1705 return -ENXIO;
1706 }
1707
1708 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1709
1710 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1711 incr_field = 0;
1712
1713 if (!code) {
1714 addr_field <<= 1;
1715 if (yram)
1716 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1717
1718 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1719 }
1720
1721 dma_cfg = addr_field + incr_field;
1722 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1723 dma_cfg);
1724 if (status < 0) {
1725 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1726 return status;
1727 }
1728 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1729
1730 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1731 (code ? 0 : 1));
1732
1733 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1734 adr_ofs);
1735 if (status < 0) {
1736 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1737 return status;
1738 }
1739 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1740
1741 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1742
1743 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1744
1745 xfr_cnt = base_cnt | cur_cnt;
1746
1747 status = chipio_write(codec,
1748 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1749 if (status < 0) {
1750 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1751 return status;
1752 }
1753 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1754
1755 snd_printdd(
1756 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1757 "ADROFS=0x%x, XFRCNT=0x%x\n",
1758 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1759
1760 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1761
1762 return 0;
1763}
1764
Ian Minettd5c21b82012-09-20 20:29:18 -07001765/*
1766 * Start the DSP DMA
1767 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001768static int dsp_dma_start(struct hda_codec *codec,
1769 unsigned int dma_chan, bool ovly)
1770{
1771 unsigned int reg = 0;
1772 int status = 0;
1773
1774 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1775
1776 if (ovly) {
1777 status = chipio_read(codec,
1778 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1779
1780 if (status < 0) {
1781 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1782 return status;
1783 }
1784 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1785
1786 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1787 DSPDMAC_CHNLSTART_DIS_MASK);
1788 }
1789
1790 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1791 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1792 if (status < 0) {
1793 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1794 return status;
1795 }
1796 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1797
1798 return status;
1799}
1800
Ian Minettd5c21b82012-09-20 20:29:18 -07001801/*
1802 * Stop the DSP DMA
1803 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001804static int dsp_dma_stop(struct hda_codec *codec,
1805 unsigned int dma_chan, bool ovly)
1806{
1807 unsigned int reg = 0;
1808 int status = 0;
1809
1810 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1811
1812 if (ovly) {
1813 status = chipio_read(codec,
1814 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1815
1816 if (status < 0) {
1817 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1818 return status;
1819 }
1820 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1821 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1822 DSPDMAC_CHNLSTART_DIS_MASK);
1823 }
1824
1825 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1826 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1827 if (status < 0) {
1828 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1829 return status;
1830 }
1831 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1832
1833 return status;
1834}
1835
Ian Minettd5c21b82012-09-20 20:29:18 -07001836/**
1837 * Allocate router ports
1838 *
1839 * @codec: the HDA codec
1840 * @num_chans: number of channels in the stream
1841 * @ports_per_channel: number of ports per channel
1842 * @start_device: start device
1843 * @port_map: pointer to the port list to hold the allocated ports
1844 *
1845 * Returns zero or a negative error code.
1846 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001847static int dsp_allocate_router_ports(struct hda_codec *codec,
1848 unsigned int num_chans,
1849 unsigned int ports_per_channel,
1850 unsigned int start_device,
1851 unsigned int *port_map)
1852{
1853 int status = 0;
1854 int res;
1855 u8 val;
1856
1857 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1858 if (status < 0)
1859 return status;
1860
1861 val = start_device << 6;
1862 val |= (ports_per_channel - 1) << 4;
1863 val |= num_chans - 1;
1864
1865 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1866 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1867 val);
1868
1869 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1870 VENDOR_CHIPIO_PORT_ALLOC_SET,
1871 MEM_CONNID_DSP);
1872
1873 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1874 if (status < 0)
1875 return status;
1876
1877 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1878 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1879
1880 *port_map = res;
1881
1882 return (res < 0) ? res : 0;
1883}
1884
Ian Minettd5c21b82012-09-20 20:29:18 -07001885/*
1886 * Free router ports
1887 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001888static int dsp_free_router_ports(struct hda_codec *codec)
1889{
1890 int status = 0;
1891
1892 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1893 if (status < 0)
1894 return status;
1895
1896 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1897 VENDOR_CHIPIO_PORT_FREE_SET,
1898 MEM_CONNID_DSP);
1899
1900 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1901
1902 return status;
1903}
1904
Ian Minettd5c21b82012-09-20 20:29:18 -07001905/*
1906 * Allocate DSP ports for the download stream
1907 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001908static int dsp_allocate_ports(struct hda_codec *codec,
1909 unsigned int num_chans,
1910 unsigned int rate_multi, unsigned int *port_map)
1911{
1912 int status;
1913
1914 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1915
1916 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1917 snd_printdd(KERN_ERR "bad rate multiple");
1918 return -EINVAL;
1919 }
1920
1921 status = dsp_allocate_router_ports(codec, num_chans,
1922 rate_multi, 0, port_map);
1923
1924 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1925
1926 return status;
1927}
1928
Ian Minett01ef7db2012-09-20 20:29:16 -07001929static int dsp_allocate_ports_format(struct hda_codec *codec,
1930 const unsigned short fmt,
1931 unsigned int *port_map)
1932{
1933 int status;
1934 unsigned int num_chans;
1935
1936 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1937 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1938 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1939
1940 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1941 snd_printdd(KERN_ERR "bad rate multiple");
1942 return -EINVAL;
1943 }
1944
1945 num_chans = get_hdafmt_chs(fmt) + 1;
1946
1947 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1948
1949 return status;
1950}
1951
1952/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001953 * free DSP ports
1954 */
1955static int dsp_free_ports(struct hda_codec *codec)
1956{
1957 int status;
1958
1959 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
1960
1961 status = dsp_free_router_ports(codec);
1962 if (status < 0) {
1963 snd_printdd(KERN_ERR "free router ports fail");
1964 return status;
1965 }
1966 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
1967
1968 return status;
1969}
1970
1971/*
Ian Minett01ef7db2012-09-20 20:29:16 -07001972 * HDA DMA engine stuffs for DSP code download
1973 */
1974struct dma_engine {
1975 struct hda_codec *codec;
1976 unsigned short m_converter_format;
1977 struct snd_dma_buffer *dmab;
1978 unsigned int buf_size;
1979};
1980
1981
1982enum dma_state {
1983 DMA_STATE_STOP = 0,
1984 DMA_STATE_RUN = 1
1985};
1986
1987static int dma_convert_to_hda_format(
Ian Minette97249d2012-09-20 20:29:21 -07001988 unsigned int sample_rate,
1989 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07001990 unsigned short *hda_format)
1991{
1992 unsigned int format_val;
1993
1994 format_val = snd_hda_calc_stream_format(
Ian Minette97249d2012-09-20 20:29:21 -07001995 sample_rate,
1996 channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07001997 SNDRV_PCM_FORMAT_S32_LE,
Ian Minette97249d2012-09-20 20:29:21 -07001998 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07001999
2000 if (hda_format)
2001 *hda_format = (unsigned short)format_val;
2002
2003 return 0;
2004}
2005
Ian Minettd5c21b82012-09-20 20:29:18 -07002006/*
2007 * Reset DMA for DSP download
2008 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002009static int dma_reset(struct dma_engine *dma)
2010{
2011 struct hda_codec *codec = dma->codec;
2012 struct ca0132_spec *spec = codec->spec;
2013 int status;
2014
2015 if (dma->dmab)
2016 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
2017
2018 status = snd_hda_codec_load_dsp_prepare(codec,
2019 dma->m_converter_format,
2020 dma->buf_size,
2021 dma->dmab);
2022 if (status < 0)
2023 return status;
2024 spec->dsp_stream_id = status;
2025 return 0;
2026}
2027
2028static int dma_set_state(struct dma_engine *dma, enum dma_state state)
2029{
2030 bool cmd;
2031
2032 snd_printdd("dma_set_state state=%d\n", state);
2033
2034 switch (state) {
2035 case DMA_STATE_STOP:
2036 cmd = false;
2037 break;
2038 case DMA_STATE_RUN:
2039 cmd = true;
2040 break;
2041 default:
2042 return 0;
2043 }
2044
2045 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
2046 return 0;
2047}
2048
2049static unsigned int dma_get_buffer_size(struct dma_engine *dma)
2050{
2051 return dma->dmab->bytes;
2052}
2053
2054static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
2055{
2056 return dma->dmab->area;
2057}
2058
2059static int dma_xfer(struct dma_engine *dma,
2060 const unsigned int *data,
2061 unsigned int count)
2062{
2063 memcpy(dma->dmab->area, data, count);
2064 return 0;
2065}
2066
2067static void dma_get_converter_format(
2068 struct dma_engine *dma,
2069 unsigned short *format)
2070{
2071 if (format)
2072 *format = dma->m_converter_format;
2073}
2074
2075static unsigned int dma_get_stream_id(struct dma_engine *dma)
2076{
2077 struct ca0132_spec *spec = dma->codec->spec;
2078
2079 return spec->dsp_stream_id;
2080}
2081
2082struct dsp_image_seg {
2083 u32 magic;
2084 u32 chip_addr;
2085 u32 count;
2086 u32 data[0];
2087};
2088
2089static const u32 g_magic_value = 0x4c46584d;
2090static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
2091
2092static bool is_valid(const struct dsp_image_seg *p)
2093{
2094 return p->magic == g_magic_value;
2095}
2096
2097static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
2098{
2099 return g_chip_addr_magic_value == p->chip_addr;
2100}
2101
2102static bool is_last(const struct dsp_image_seg *p)
2103{
2104 return p->count == 0;
2105}
2106
2107static size_t dsp_sizeof(const struct dsp_image_seg *p)
2108{
2109 return sizeof(*p) + p->count*sizeof(u32);
2110}
2111
2112static const struct dsp_image_seg *get_next_seg_ptr(
2113 const struct dsp_image_seg *p)
2114{
2115 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
2116}
2117
2118/*
2119 * CA0132 chip DSP transfer stuffs. For DSP download.
2120 */
2121#define INVALID_DMA_CHANNEL (~0UL)
2122
Ian Minettd5c21b82012-09-20 20:29:18 -07002123/*
2124 * Program a list of address/data pairs via the ChipIO widget.
2125 * The segment data is in the format of successive pairs of words.
2126 * These are repeated as indicated by the segment's count field.
2127 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002128static int dspxfr_hci_write(struct hda_codec *codec,
2129 const struct dsp_image_seg *fls)
2130{
2131 int status;
2132 const u32 *data;
2133 unsigned int count;
2134
2135 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
2136 snd_printdd(KERN_ERR "hci_write invalid params");
2137 return -EINVAL;
2138 }
2139
2140 count = fls->count;
2141 data = (u32 *)(fls->data);
2142 while (count >= 2) {
2143 status = chipio_write(codec, data[0], data[1]);
2144 if (status < 0) {
2145 snd_printdd(KERN_ERR "hci_write chipio failed");
2146 return status;
2147 }
2148 count -= 2;
2149 data += 2;
2150 }
2151 return 0;
2152}
2153
Ian Minettd5c21b82012-09-20 20:29:18 -07002154/**
2155 * Write a block of data into DSP code or data RAM using pre-allocated
2156 * DMA engine.
2157 *
2158 * @codec: the HDA codec
2159 * @fls: pointer to a fast load image
2160 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2161 * no relocation
2162 * @dma_engine: pointer to DMA engine to be used for DSP download
2163 * @dma_chan: The number of DMA channels used for DSP download
2164 * @port_map_mask: port mapping
2165 * @ovly: TRUE if overlay format is required
2166 *
2167 * Returns zero or a negative error code.
2168 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002169static int dspxfr_one_seg(struct hda_codec *codec,
2170 const struct dsp_image_seg *fls,
2171 unsigned int reloc,
2172 struct dma_engine *dma_engine,
2173 unsigned int dma_chan,
2174 unsigned int port_map_mask,
2175 bool ovly)
2176{
2177 int status;
2178 bool comm_dma_setup_done = false;
2179 const unsigned int *data;
2180 unsigned int chip_addx;
2181 unsigned int words_to_write;
2182 unsigned int buffer_size_words;
2183 unsigned char *buffer_addx;
2184 unsigned short hda_format;
2185 unsigned int sample_rate_div;
2186 unsigned int sample_rate_mul;
2187 unsigned int num_chans;
2188 unsigned int hda_frame_size_words;
2189 unsigned int remainder_words;
2190 const u32 *data_remainder;
2191 u32 chip_addx_remainder;
2192 unsigned int run_size_words;
2193 const struct dsp_image_seg *hci_write = NULL;
2194 int retry;
2195
2196 if (fls == NULL)
2197 return -EINVAL;
2198 if (is_hci_prog_list_seg(fls)) {
2199 hci_write = fls;
2200 fls = get_next_seg_ptr(fls);
2201 }
2202
2203 if (hci_write && (!fls || is_last(fls))) {
2204 snd_printdd("hci_write\n");
2205 return dspxfr_hci_write(codec, hci_write);
2206 }
2207
2208 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
2209 snd_printdd("Invalid Params\n");
2210 return -EINVAL;
2211 }
2212
2213 data = fls->data;
2214 chip_addx = fls->chip_addr,
2215 words_to_write = fls->count;
2216
2217 if (!words_to_write)
2218 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
2219 if (reloc)
2220 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
2221
2222 if (!UC_RANGE(chip_addx, words_to_write) &&
2223 !X_RANGE_ALL(chip_addx, words_to_write) &&
2224 !Y_RANGE_ALL(chip_addx, words_to_write)) {
2225 snd_printdd("Invalid chip_addx Params\n");
2226 return -EINVAL;
2227 }
2228
2229 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
2230 sizeof(u32);
2231
2232 buffer_addx = dma_get_buffer_addr(dma_engine);
2233
2234 if (buffer_addx == NULL) {
2235 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
2236 return -EINVAL;
2237 }
2238
2239 dma_get_converter_format(dma_engine, &hda_format);
2240 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
2241 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
2242 num_chans = get_hdafmt_chs(hda_format) + 1;
2243
2244 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
2245 (num_chans * sample_rate_mul / sample_rate_div));
2246
2247 buffer_size_words = min(buffer_size_words,
2248 (unsigned int)(UC_RANGE(chip_addx, 1) ?
2249 65536 : 32768));
2250 buffer_size_words -= buffer_size_words % hda_frame_size_words;
2251 snd_printdd(
2252 "chpadr=0x%08x frmsz=%u nchan=%u "
2253 "rate_mul=%u div=%u bufsz=%u\n",
2254 chip_addx, hda_frame_size_words, num_chans,
2255 sample_rate_mul, sample_rate_div, buffer_size_words);
2256
2257 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
2258 (buffer_size_words < hda_frame_size_words)) {
2259 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
2260 return -EINVAL;
2261 }
2262
2263 remainder_words = words_to_write % hda_frame_size_words;
2264 data_remainder = data;
2265 chip_addx_remainder = chip_addx;
2266
2267 data += remainder_words;
2268 chip_addx += remainder_words*sizeof(u32);
2269 words_to_write -= remainder_words;
2270
2271 while (words_to_write != 0) {
2272 run_size_words = min(buffer_size_words, words_to_write);
2273 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
2274 words_to_write, run_size_words, remainder_words);
2275 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
2276 if (!comm_dma_setup_done) {
2277 status = dsp_dma_stop(codec, dma_chan, ovly);
2278 if (status < 0)
2279 return -EIO;
2280 status = dsp_dma_setup_common(codec, chip_addx,
2281 dma_chan, port_map_mask, ovly);
2282 if (status < 0)
2283 return status;
2284 comm_dma_setup_done = true;
2285 }
2286
2287 status = dsp_dma_setup(codec, chip_addx,
2288 run_size_words, dma_chan);
2289 if (status < 0)
2290 return status;
2291 status = dsp_dma_start(codec, dma_chan, ovly);
2292 if (status < 0)
2293 return status;
2294 if (!dsp_is_dma_active(codec, dma_chan)) {
2295 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
2296 return -EIO;
2297 }
2298 status = dma_set_state(dma_engine, DMA_STATE_RUN);
2299 if (status < 0)
2300 return status;
2301 if (remainder_words != 0) {
2302 status = chipio_write_multiple(codec,
2303 chip_addx_remainder,
2304 data_remainder,
2305 remainder_words);
2306 remainder_words = 0;
2307 }
2308 if (hci_write) {
2309 status = dspxfr_hci_write(codec, hci_write);
2310 hci_write = NULL;
2311 }
2312 retry = 5000;
2313 while (dsp_is_dma_active(codec, dma_chan)) {
2314 if (--retry <= 0)
2315 break;
2316 }
2317 snd_printdd(KERN_INFO "+++++ DMA complete");
2318 dma_set_state(dma_engine, DMA_STATE_STOP);
2319 dma_reset(dma_engine);
2320
2321 if (status < 0)
2322 return status;
2323
2324 data += run_size_words;
2325 chip_addx += run_size_words*sizeof(u32);
2326 words_to_write -= run_size_words;
2327 }
2328
2329 if (remainder_words != 0) {
2330 status = chipio_write_multiple(codec, chip_addx_remainder,
2331 data_remainder, remainder_words);
2332 }
2333
2334 return status;
2335}
2336
Ian Minettd5c21b82012-09-20 20:29:18 -07002337/**
2338 * Write the entire DSP image of a DSP code/data overlay to DSP memories
2339 *
2340 * @codec: the HDA codec
2341 * @fls_data: pointer to a fast load image
2342 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2343 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07002344 * @sample_rate: sampling rate of the stream used for DSP download
2345 * @number_channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07002346 * @ovly: TRUE if overlay format is required
2347 *
2348 * Returns zero or a negative error code.
2349 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002350static int dspxfr_image(struct hda_codec *codec,
2351 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07002352 unsigned int reloc,
2353 unsigned int sample_rate,
2354 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07002355 bool ovly)
2356{
2357 struct ca0132_spec *spec = codec->spec;
2358 int status;
2359 unsigned short hda_format = 0;
2360 unsigned int response;
2361 unsigned char stream_id = 0;
2362 struct dma_engine *dma_engine;
2363 unsigned int dma_chan;
2364 unsigned int port_map_mask;
2365
2366 if (fls_data == NULL)
2367 return -EINVAL;
2368
2369 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
Takashi Iwai063bca02012-09-21 13:44:34 +02002370 if (!dma_engine)
2371 return -ENOMEM;
Ian Minett01ef7db2012-09-20 20:29:16 -07002372
2373 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
2374 if (!dma_engine->dmab) {
2375 status = -ENOMEM;
2376 goto exit;
2377 }
2378
2379 dma_engine->codec = codec;
Ian Minette97249d2012-09-20 20:29:21 -07002380 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07002381 dma_engine->m_converter_format = hda_format;
2382 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2383 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2384
2385 dma_chan = 0;
2386
2387 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2388 hda_format, &response);
2389
2390 if (status < 0) {
2391 snd_printdd(KERN_ERR "set converter format fail");
2392 goto exit;
2393 }
2394
2395 status = snd_hda_codec_load_dsp_prepare(codec,
2396 dma_engine->m_converter_format,
2397 dma_engine->buf_size,
2398 dma_engine->dmab);
2399 if (status < 0)
2400 goto exit;
2401 spec->dsp_stream_id = status;
2402
2403 if (ovly) {
2404 status = dspio_alloc_dma_chan(codec, &dma_chan);
2405 if (status < 0) {
2406 snd_printdd(KERN_ERR "alloc dmachan fail");
2407 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2408 goto exit;
2409 }
2410 }
2411
2412 port_map_mask = 0;
2413 status = dsp_allocate_ports_format(codec, hda_format,
2414 &port_map_mask);
2415 if (status < 0) {
2416 snd_printdd(KERN_ERR "alloc ports fail");
2417 goto exit;
2418 }
2419
2420 stream_id = dma_get_stream_id(dma_engine);
2421 status = codec_set_converter_stream_channel(codec,
2422 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2423 if (status < 0) {
2424 snd_printdd(KERN_ERR "set stream chan fail");
2425 goto exit;
2426 }
2427
2428 while ((fls_data != NULL) && !is_last(fls_data)) {
2429 if (!is_valid(fls_data)) {
2430 snd_printdd(KERN_ERR "FLS check fail");
2431 status = -EINVAL;
2432 goto exit;
2433 }
2434 status = dspxfr_one_seg(codec, fls_data, reloc,
2435 dma_engine, dma_chan,
2436 port_map_mask, ovly);
2437 if (status < 0)
2438 break;
2439
2440 if (is_hci_prog_list_seg(fls_data))
2441 fls_data = get_next_seg_ptr(fls_data);
2442
2443 if ((fls_data != NULL) && !is_last(fls_data))
2444 fls_data = get_next_seg_ptr(fls_data);
2445 }
2446
2447 if (port_map_mask != 0)
2448 status = dsp_free_ports(codec);
2449
2450 if (status < 0)
2451 goto exit;
2452
2453 status = codec_set_converter_stream_channel(codec,
2454 WIDGET_CHIP_CTRL, 0, 0, &response);
2455
2456exit:
2457 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2458 dspio_free_dma_chan(codec, dma_chan);
2459
2460 if (dma_engine->dmab)
2461 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2462 kfree(dma_engine->dmab);
2463 kfree(dma_engine);
2464
2465 return status;
2466}
2467
2468/*
2469 * CA0132 DSP download stuffs.
2470 */
2471static void dspload_post_setup(struct hda_codec *codec)
2472{
2473 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2474
2475 /*set DSP speaker to 2.0 configuration*/
2476 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2477 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2478
2479 /*update write pointer*/
2480 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2481}
2482
Ian Minettd5c21b82012-09-20 20:29:18 -07002483/**
2484 * Download DSP from a DSP Image Fast Load structure. This structure is a
2485 * linear, non-constant sized element array of structures, each of which
2486 * contain the count of the data to be loaded, the data itself, and the
2487 * corresponding starting chip address of the starting data location.
2488 *
2489 * @codec: the HDA codec
2490 * @fls: pointer to a fast load image
2491 * @ovly: TRUE if overlay format is required
2492 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2493 * no relocation
2494 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2495 * @router_chans: number of audio router channels to be allocated (0 means use
2496 * internal defaults; max is 32)
2497 *
2498 * Returns zero or a negative error code.
2499 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002500static int dspload_image(struct hda_codec *codec,
2501 const struct dsp_image_seg *fls,
2502 bool ovly,
2503 unsigned int reloc,
2504 bool autostart,
2505 int router_chans)
2506{
2507 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07002508 unsigned int sample_rate;
2509 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07002510
2511 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2512 if (router_chans == 0) {
2513 if (!ovly)
2514 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2515 else
2516 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2517 }
2518
Ian Minette97249d2012-09-20 20:29:21 -07002519 sample_rate = 48000;
2520 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07002521
Ian Minette97249d2012-09-20 20:29:21 -07002522 while (channels > 16) {
2523 sample_rate *= 2;
2524 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07002525 }
2526
Ian Minett01ef7db2012-09-20 20:29:16 -07002527 do {
2528 snd_printdd(KERN_INFO "Ready to program DMA");
2529 if (!ovly)
2530 status = dsp_reset(codec);
2531
2532 if (status < 0)
2533 break;
2534
2535 snd_printdd(KERN_INFO "dsp_reset() complete");
Ian Minette97249d2012-09-20 20:29:21 -07002536 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2537 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07002538
2539 if (status < 0)
2540 break;
2541
2542 snd_printdd(KERN_INFO "dspxfr_image() complete");
2543 if (autostart && !ovly) {
2544 dspload_post_setup(codec);
2545 status = dsp_set_run_state(codec);
2546 }
2547
2548 snd_printdd(KERN_INFO "LOAD FINISHED");
2549 } while (0);
2550
2551 return status;
2552}
2553
Ian Minettc3b4eea22012-09-20 20:29:17 -07002554static const struct firmware *fw_efx;
2555
2556static int request_firmware_cached(const struct firmware **firmware_p,
2557 const char *name, struct device *device)
2558{
2559 if (*firmware_p)
2560 return 0; /* already loaded */
2561 return request_firmware(firmware_p, name, device);
2562}
2563
2564static void release_cached_firmware(void)
2565{
2566 if (fw_efx) {
2567 release_firmware(fw_efx);
2568 fw_efx = NULL;
2569 }
2570}
2571
Ian Minett01ef7db2012-09-20 20:29:16 -07002572static bool dspload_is_loaded(struct hda_codec *codec)
2573{
2574 unsigned int data = 0;
2575 int status = 0;
2576
2577 status = chipio_read(codec, 0x40004, &data);
2578 if ((status < 0) || (data != 1))
2579 return false;
2580
2581 return true;
2582}
2583
2584static bool dspload_wait_loaded(struct hda_codec *codec)
2585{
2586 int retry = 100;
2587
2588 do {
2589 msleep(20);
2590 if (dspload_is_loaded(codec)) {
2591 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2592 return true;
2593 }
2594 } while (--retry);
2595
2596 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2597 return false;
2598}
2599
Ian Minettef6b2ea2012-12-20 18:53:33 -08002600
2601/*
2602 * Mixer controls helpers.
2603 */
2604#define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
2605 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2606 .name = xname, \
2607 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2608 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2609 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2610 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
2611 .info = ca0132_volume_info, \
2612 .get = ca0132_volume_get, \
2613 .put = ca0132_volume_put, \
2614 .tlv = { .c = ca0132_volume_tlv }, \
2615 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2616
2617#define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
2618 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2619 .name = xname, \
2620 .subdevice = HDA_SUBDEV_AMP_FLAG, \
2621 .info = snd_hda_mixer_amp_switch_info, \
2622 .get = ca0132_switch_get, \
2623 .put = ca0132_switch_put, \
2624 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
2625
2626/* stereo */
2627#define CA0132_CODEC_VOL(xname, nid, dir) \
2628 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
2629#define CA0132_CODEC_MUTE(xname, nid, dir) \
2630 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
2631
Ian Minett95c6e9c2011-06-15 15:35:17 -07002632/*
Ian Minett95c6e9c2011-06-15 15:35:17 -07002633 * PCM callbacks
2634 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002635static int ca0132_playback_pcm_open(struct hda_pcm_stream *hinfo,
2636 struct hda_codec *codec,
2637 struct snd_pcm_substream *substream)
2638{
2639 struct ca0132_spec *spec = codec->spec;
2640 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2641 hinfo);
2642}
2643
Ian Minett95c6e9c2011-06-15 15:35:17 -07002644static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2645 struct hda_codec *codec,
2646 unsigned int stream_tag,
2647 unsigned int format,
2648 struct snd_pcm_substream *substream)
2649{
2650 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002651 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2652 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002653}
2654
2655static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2656 struct hda_codec *codec,
2657 struct snd_pcm_substream *substream)
2658{
2659 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002660 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002661}
2662
2663/*
2664 * Digital out
2665 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002666static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2667 struct hda_codec *codec,
2668 struct snd_pcm_substream *substream)
2669{
2670 struct ca0132_spec *spec = codec->spec;
2671 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2672}
2673
Ian Minett95c6e9c2011-06-15 15:35:17 -07002674static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2675 struct hda_codec *codec,
2676 unsigned int stream_tag,
2677 unsigned int format,
2678 struct snd_pcm_substream *substream)
2679{
2680 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002681 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2682 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002683}
2684
2685static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2686 struct hda_codec *codec,
2687 struct snd_pcm_substream *substream)
2688{
2689 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002690 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002691}
2692
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002693static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2694 struct hda_codec *codec,
2695 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002696{
2697 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002698 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002699}
2700
2701/*
2702 */
2703static struct hda_pcm_stream ca0132_pcm_analog_playback = {
2704 .substreams = 1,
2705 .channels_min = 2,
2706 .channels_max = 2,
2707 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002708 .open = ca0132_playback_pcm_open,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002709 .prepare = ca0132_playback_pcm_prepare,
2710 .cleanup = ca0132_playback_pcm_cleanup
2711 },
2712};
2713
2714static struct hda_pcm_stream ca0132_pcm_analog_capture = {
2715 .substreams = 1,
2716 .channels_min = 2,
2717 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002718};
2719
2720static struct hda_pcm_stream ca0132_pcm_digital_playback = {
2721 .substreams = 1,
2722 .channels_min = 2,
2723 .channels_max = 2,
2724 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002725 .open = ca0132_dig_playback_pcm_open,
2726 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002727 .prepare = ca0132_dig_playback_pcm_prepare,
2728 .cleanup = ca0132_dig_playback_pcm_cleanup
2729 },
2730};
2731
2732static struct hda_pcm_stream ca0132_pcm_digital_capture = {
2733 .substreams = 1,
2734 .channels_min = 2,
2735 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002736};
2737
2738static int ca0132_build_pcms(struct hda_codec *codec)
2739{
2740 struct ca0132_spec *spec = codec->spec;
2741 struct hda_pcm *info = spec->pcm_rec;
2742
2743 codec->pcm_info = info;
2744 codec->num_pcms = 0;
2745
2746 info->name = "CA0132 Analog";
2747 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
2748 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
2749 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
2750 spec->multiout.max_channels;
2751 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
2752 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
2753 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
2754 codec->num_pcms++;
2755
2756 if (!spec->dig_out && !spec->dig_in)
2757 return 0;
2758
2759 info++;
2760 info->name = "CA0132 Digital";
2761 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2762 if (spec->dig_out) {
2763 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2764 ca0132_pcm_digital_playback;
2765 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
2766 }
2767 if (spec->dig_in) {
2768 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2769 ca0132_pcm_digital_capture;
2770 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
2771 }
2772 codec->num_pcms++;
2773
2774 return 0;
2775}
2776
2777#define REG_CODEC_MUTE 0x18b014
2778#define REG_CODEC_HP_VOL_L 0x18b070
2779#define REG_CODEC_HP_VOL_R 0x18b074
2780
2781static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2783{
2784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2785 struct ca0132_spec *spec = codec->spec;
2786 long *valp = ucontrol->value.integer.value;
2787
2788 *valp = spec->curr_hp_switch;
2789 return 0;
2790}
2791
2792static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_value *ucontrol)
2794{
2795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2796 struct ca0132_spec *spec = codec->spec;
2797 long *valp = ucontrol->value.integer.value;
2798 unsigned int data;
2799 int err;
2800
2801 /* any change? */
2802 if (spec->curr_hp_switch == *valp)
2803 return 0;
2804
2805 snd_hda_power_up(codec);
2806
2807 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2808 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002809 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002810
2811 /* *valp 0 is mute, 1 is unmute */
2812 data = (data & 0x7f) | (*valp ? 0 : 0x80);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002813 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002814 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002815 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002816
2817 spec->curr_hp_switch = *valp;
2818
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002819 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002820 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002821 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002822}
2823
2824static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
2825 struct snd_ctl_elem_value *ucontrol)
2826{
2827 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2828 struct ca0132_spec *spec = codec->spec;
2829 long *valp = ucontrol->value.integer.value;
2830
2831 *valp = spec->curr_speaker_switch;
2832 return 0;
2833}
2834
2835static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
2837{
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 struct ca0132_spec *spec = codec->spec;
2840 long *valp = ucontrol->value.integer.value;
2841 unsigned int data;
2842 int err;
2843
2844 /* any change? */
2845 if (spec->curr_speaker_switch == *valp)
2846 return 0;
2847
2848 snd_hda_power_up(codec);
2849
2850 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2851 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002852 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002853
2854 /* *valp 0 is mute, 1 is unmute */
2855 data = (data & 0xef) | (*valp ? 0 : 0x10);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002856 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002857 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002858 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002859
2860 spec->curr_speaker_switch = *valp;
2861
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002862 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002863 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002864 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002865}
2866
2867static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2869{
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2871 struct ca0132_spec *spec = codec->spec;
2872 long *valp = ucontrol->value.integer.value;
2873
2874 *valp++ = spec->curr_hp_volume[0];
2875 *valp = spec->curr_hp_volume[1];
2876 return 0;
2877}
2878
2879static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
2882 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2883 struct ca0132_spec *spec = codec->spec;
2884 long *valp = ucontrol->value.integer.value;
2885 long left_vol, right_vol;
2886 unsigned int data;
2887 int val;
2888 int err;
2889
2890 left_vol = *valp++;
2891 right_vol = *valp;
2892
2893 /* any change? */
2894 if ((spec->curr_hp_volume[0] == left_vol) &&
2895 (spec->curr_hp_volume[1] == right_vol))
2896 return 0;
2897
2898 snd_hda_power_up(codec);
2899
2900 err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
2901 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002902 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002903
2904 val = 31 - left_vol;
2905 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002906 err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002907 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002908 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002909
2910 val = 31 - right_vol;
2911 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002912 err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002913 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002914 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002915
2916 spec->curr_hp_volume[0] = left_vol;
2917 spec->curr_hp_volume[1] = right_vol;
2918
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002919 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002920 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002921 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002922}
2923
2924static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
2925{
2926 struct snd_kcontrol_new knew =
2927 HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
2928 nid, 1, 0, HDA_OUTPUT);
2929 knew.get = ca0132_hp_switch_get;
2930 knew.put = ca0132_hp_switch_put;
2931 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2932}
2933
2934static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
2935{
2936 struct snd_kcontrol_new knew =
2937 HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
2938 nid, 3, 0, HDA_OUTPUT);
2939 knew.get = ca0132_hp_volume_get;
2940 knew.put = ca0132_hp_volume_put;
2941 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2942}
2943
2944static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
2945{
2946 struct snd_kcontrol_new knew =
2947 HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
2948 nid, 1, 0, HDA_OUTPUT);
2949 knew.get = ca0132_speaker_switch_get;
2950 knew.put = ca0132_speaker_switch_put;
2951 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2952}
2953
2954static void ca0132_fix_hp_caps(struct hda_codec *codec)
2955{
2956 struct ca0132_spec *spec = codec->spec;
2957 struct auto_pin_cfg *cfg = &spec->autocfg;
2958 unsigned int caps;
2959
2960 /* set mute-capable, 1db step, 32 steps, ofs 6 */
2961 caps = 0x80031f06;
2962 snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
2963}
2964
2965static int ca0132_build_controls(struct hda_codec *codec)
2966{
2967 struct ca0132_spec *spec = codec->spec;
2968 struct auto_pin_cfg *cfg = &spec->autocfg;
2969 int i, err;
2970
2971 if (spec->multiout.num_dacs) {
2972 err = add_speaker_switch(codec, spec->out_pins[0]);
2973 if (err < 0)
2974 return err;
2975 }
2976
2977 if (cfg->hp_outs) {
2978 ca0132_fix_hp_caps(codec);
2979 err = add_hp_switch(codec, cfg->hp_pins[0]);
2980 if (err < 0)
2981 return err;
2982 err = add_hp_volume(codec, cfg->hp_pins[0]);
2983 if (err < 0)
2984 return err;
2985 }
2986
2987 for (i = 0; i < spec->num_inputs; i++) {
2988 const char *label = spec->input_labels[i];
2989
2990 err = add_in_switch(codec, spec->adcs[i], label);
2991 if (err < 0)
2992 return err;
2993 err = add_in_volume(codec, spec->adcs[i], label);
2994 if (err < 0)
2995 return err;
2996 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
2997 /* add Mic-Boost */
2998 err = add_in_mono_volume(codec, spec->input_pins[i],
2999 "Mic Boost", 1);
3000 if (err < 0)
3001 return err;
3002 }
3003 }
3004
3005 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +02003006 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
3007 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003008 if (err < 0)
3009 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02003010 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07003011 if (err < 0)
3012 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02003013 /* spec->multiout.share_spdif = 1; */
Ian Minett95c6e9c2011-06-15 15:35:17 -07003014 }
3015
3016 if (spec->dig_in) {
3017 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
3018 if (err < 0)
3019 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003020 }
3021 return 0;
3022}
3023
3024
3025static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
3026{
3027 /* Set Creative extension */
3028 snd_printdd("SET CREATIVE EXTENSION\n");
3029 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
3030 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
3031 enable);
3032 msleep(20);
3033}
3034
3035
3036static void ca0132_config(struct hda_codec *codec)
3037{
3038 struct ca0132_spec *spec = codec->spec;
3039 struct auto_pin_cfg *cfg = &spec->autocfg;
3040
Takashi Iwai27ebeb02012-08-08 17:20:18 +02003041 codec->pcm_format_first = 1;
3042 codec->no_sticky_stream = 1;
3043
Ian Minett95c6e9c2011-06-15 15:35:17 -07003044 /* line-outs */
3045 cfg->line_outs = 1;
3046 cfg->line_out_pins[0] = 0x0b; /* front */
3047 cfg->line_out_type = AUTO_PIN_LINE_OUT;
3048
3049 spec->dacs[0] = 0x02;
3050 spec->out_pins[0] = 0x0b;
3051 spec->multiout.dac_nids = spec->dacs;
3052 spec->multiout.num_dacs = 1;
3053 spec->multiout.max_channels = 2;
3054
3055 /* headphone */
3056 cfg->hp_outs = 1;
3057 cfg->hp_pins[0] = 0x0f;
3058
3059 spec->hp_dac = 0;
3060 spec->multiout.hp_nid = 0;
3061
3062 /* inputs */
3063 cfg->num_inputs = 2; /* Mic-in and line-in */
3064 cfg->inputs[0].pin = 0x12;
3065 cfg->inputs[0].type = AUTO_PIN_MIC;
3066 cfg->inputs[1].pin = 0x11;
3067 cfg->inputs[1].type = AUTO_PIN_LINE_IN;
3068
3069 /* Mic-in */
3070 spec->input_pins[0] = 0x12;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02003071 spec->input_labels[0] = "Mic";
Ian Minett95c6e9c2011-06-15 15:35:17 -07003072 spec->adcs[0] = 0x07;
3073
3074 /* Line-In */
3075 spec->input_pins[1] = 0x11;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02003076 spec->input_labels[1] = "Line";
Ian Minett95c6e9c2011-06-15 15:35:17 -07003077 spec->adcs[1] = 0x08;
3078 spec->num_inputs = 2;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02003079
3080 /* SPDIF I/O */
3081 spec->dig_out = 0x05;
3082 spec->multiout.dig_out_nid = spec->dig_out;
3083 cfg->dig_out_pins[0] = 0x0c;
3084 cfg->dig_outs = 1;
3085 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
3086 spec->dig_in = 0x09;
3087 cfg->dig_in_pin = 0x0e;
3088 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07003089}
3090
3091static void ca0132_init_chip(struct hda_codec *codec)
3092{
3093 struct ca0132_spec *spec = codec->spec;
3094
3095 mutex_init(&spec->chipio_mutex);
3096}
3097
3098static void ca0132_exit_chip(struct hda_codec *codec)
3099{
3100 /* put any chip cleanup stuffs here. */
3101}
3102
Ian Minett01ef7db2012-09-20 20:29:16 -07003103static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
3104{
3105 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
3106 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
3107 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
3108 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
3109 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
3110 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
3111
3112 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
3113 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
3114 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
3115}
3116
3117static bool ca0132_download_dsp_images(struct hda_codec *codec)
3118{
3119 bool dsp_loaded = false;
3120 const struct dsp_image_seg *dsp_os_image;
Ian Minett01ef7db2012-09-20 20:29:16 -07003121
Ian Minettc3b4eea22012-09-20 20:29:17 -07003122 if (request_firmware_cached(&fw_efx, EFX_FILE,
3123 codec->bus->card->dev) != 0)
Ian Minett01ef7db2012-09-20 20:29:16 -07003124 return false;
3125
Ian Minettc3b4eea22012-09-20 20:29:17 -07003126 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
Ian Minett01ef7db2012-09-20 20:29:16 -07003127 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
3128 dsp_loaded = dspload_wait_loaded(codec);
3129
Ian Minett01ef7db2012-09-20 20:29:16 -07003130 return dsp_loaded;
3131}
3132
3133static void ca0132_download_dsp(struct hda_codec *codec)
3134{
3135 struct ca0132_spec *spec = codec->spec;
3136
3137 spec->dsp_state = DSP_DOWNLOAD_INIT;
3138
3139 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
3140 chipio_enable_clocks(codec);
3141 spec->dsp_state = DSP_DOWNLOADING;
3142 if (!ca0132_download_dsp_images(codec))
3143 spec->dsp_state = DSP_DOWNLOAD_FAILED;
3144 else
3145 spec->dsp_state = DSP_DOWNLOADED;
3146 }
3147
3148 if (spec->dsp_state == DSP_DOWNLOADED)
3149 ca0132_set_dsp_msr(codec, true);
3150}
3151
Ian Minett95c6e9c2011-06-15 15:35:17 -07003152static int ca0132_init(struct hda_codec *codec)
3153{
3154 struct ca0132_spec *spec = codec->spec;
3155 struct auto_pin_cfg *cfg = &spec->autocfg;
3156 int i;
3157
Ian Minett01ef7db2012-09-20 20:29:16 -07003158#ifdef CONFIG_SND_HDA_DSP_LOADER
3159 ca0132_download_dsp(codec);
3160#endif
3161
Ian Minett95c6e9c2011-06-15 15:35:17 -07003162 for (i = 0; i < spec->multiout.num_dacs; i++) {
3163 init_output(codec, spec->out_pins[i],
3164 spec->multiout.dac_nids[i]);
3165 }
3166 init_output(codec, cfg->hp_pins[0], spec->hp_dac);
3167 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
3168
3169 for (i = 0; i < spec->num_inputs; i++)
3170 init_input(codec, spec->input_pins[i], spec->adcs[i]);
3171
3172 init_input(codec, cfg->dig_in_pin, spec->dig_in);
3173
3174 ca0132_set_ct_ext(codec, 1);
3175
3176 return 0;
3177}
3178
3179
3180static void ca0132_free(struct hda_codec *codec)
3181{
3182 ca0132_set_ct_ext(codec, 0);
3183 ca0132_exit_chip(codec);
3184 kfree(codec->spec);
3185}
3186
3187static struct hda_codec_ops ca0132_patch_ops = {
3188 .build_controls = ca0132_build_controls,
3189 .build_pcms = ca0132_build_pcms,
3190 .init = ca0132_init,
3191 .free = ca0132_free,
3192};
3193
3194
3195
3196static int patch_ca0132(struct hda_codec *codec)
3197{
3198 struct ca0132_spec *spec;
3199
3200 snd_printdd("patch_ca0132\n");
3201
3202 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3203 if (!spec)
3204 return -ENOMEM;
3205 codec->spec = spec;
3206
3207 ca0132_init_chip(codec);
3208
3209 ca0132_config(codec);
3210
3211 codec->patch_ops = ca0132_patch_ops;
3212
3213 return 0;
3214}
3215
3216/*
3217 * patch entries
3218 */
3219static struct hda_codec_preset snd_hda_preset_ca0132[] = {
3220 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
3221 {} /* terminator */
3222};
3223
3224MODULE_ALIAS("snd-hda-codec-id:11020011");
3225
3226MODULE_LICENSE("GPL");
3227MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
3228
3229static struct hda_codec_preset_list ca0132_list = {
3230 .preset = snd_hda_preset_ca0132,
3231 .owner = THIS_MODULE,
3232};
3233
3234static int __init patch_ca0132_init(void)
3235{
3236 return snd_hda_add_codec_preset(&ca0132_list);
3237}
3238
3239static void __exit patch_ca0132_exit(void)
3240{
Ian Minettc3b4eea22012-09-20 20:29:17 -07003241 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07003242 snd_hda_delete_codec_preset(&ca0132_list);
3243}
3244
3245module_init(patch_ca0132_init)
3246module_exit(patch_ca0132_exit)