blob: 5c6a0569c6119b0bd7ab144bf39760d4daa519ab [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 Minett4aa3bb02012-09-20 20:29:15 -070038#define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
39#define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
40
41#define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
42#define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
43#define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
44
45#define MASTERCONTROL 0x80
46#define MASTERCONTROL_ALLOC_DMA_CHAN 9
47
Ian Minett95c6e9c2011-06-15 15:35:17 -070048#define WIDGET_CHIP_CTRL 0x15
49#define WIDGET_DSP_CTRL 0x16
50
Ian Minett4aa3bb02012-09-20 20:29:15 -070051#define MEM_CONNID_MICIN1 3
52#define MEM_CONNID_MICIN2 5
53#define MEM_CONNID_MICOUT1 12
54#define MEM_CONNID_MICOUT2 14
55#define MEM_CONNID_WUH 10
56#define MEM_CONNID_DSP 16
57#define MEM_CONNID_DMIC 100
58
59#define SCP_SET 0
60#define SCP_GET 1
61
Ian Minett01ef7db2012-09-20 20:29:16 -070062#define EFX_FILE "ctefx.bin"
63
64MODULE_FIRMWARE(EFX_FILE);
65
Ian Minett95c6e9c2011-06-15 15:35:17 -070066enum hda_cmd_vendor_io {
67 /* for DspIO node */
68 VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
69 VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
70
71 VENDOR_DSPIO_STATUS = 0xF01,
72 VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
73 VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
74 VENDOR_DSPIO_DSP_INIT = 0x703,
75 VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
76 VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
77
78 /* for ChipIO node */
79 VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
80 VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
81 VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
82 VENDOR_CHIPIO_DATA_LOW = 0x300,
83 VENDOR_CHIPIO_DATA_HIGH = 0x400,
84
85 VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
86 VENDOR_CHIPIO_STATUS = 0xF01,
87 VENDOR_CHIPIO_HIC_POST_READ = 0x702,
88 VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
89
Ian Minett4aa3bb02012-09-20 20:29:15 -070090 VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
91 VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
92
Ian Minett95c6e9c2011-06-15 15:35:17 -070093 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
Ian Minett4aa3bb02012-09-20 20:29:15 -070094 VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
Ian Minett95c6e9c2011-06-15 15:35:17 -070095
96 VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
97 VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
98 VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
99 VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
100 VENDOR_CHIPIO_FLAG_SET = 0x70F,
101 VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
Ian Minett4aa3bb02012-09-20 20:29:15 -0700102 VENDOR_CHIPIO_PARAM_SET = 0x710,
103 VENDOR_CHIPIO_PARAM_GET = 0xF10,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700104
105 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
106 VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
107 VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
108 VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
109
Ian Minett4aa3bb02012-09-20 20:29:15 -0700110 VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
111 VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
112 VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
113 VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
114
115 VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
116 VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
117 VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
118 VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
119 VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
120 VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
121
122 VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
Ian Minett95c6e9c2011-06-15 15:35:17 -0700123};
124
125/*
126 * Control flag IDs
127 */
128enum control_flag_id {
129 /* Connection manager stream setup is bypassed/enabled */
130 CONTROL_FLAG_C_MGR = 0,
131 /* DSP DMA is bypassed/enabled */
132 CONTROL_FLAG_DMA = 1,
133 /* 8051 'idle' mode is disabled/enabled */
134 CONTROL_FLAG_IDLE_ENABLE = 2,
135 /* Tracker for the SPDIF-in path is bypassed/enabled */
136 CONTROL_FLAG_TRACKER = 3,
137 /* DigitalOut to Spdif2Out connection is disabled/enabled */
138 CONTROL_FLAG_SPDIF2OUT = 4,
139 /* Digital Microphone is disabled/enabled */
140 CONTROL_FLAG_DMIC = 5,
141 /* ADC_B rate is 48 kHz/96 kHz */
142 CONTROL_FLAG_ADC_B_96KHZ = 6,
143 /* ADC_C rate is 48 kHz/96 kHz */
144 CONTROL_FLAG_ADC_C_96KHZ = 7,
145 /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
146 CONTROL_FLAG_DAC_96KHZ = 8,
147 /* DSP rate is 48 kHz/96 kHz */
148 CONTROL_FLAG_DSP_96KHZ = 9,
149 /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
150 CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
151 /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
152 CONTROL_FLAG_SRC_RATE_96KHZ = 11,
153 /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
154 CONTROL_FLAG_DECODE_LOOP = 12,
155 /* De-emphasis filter on DAC-1 disabled/enabled */
156 CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
157 /* De-emphasis filter on DAC-2 disabled/enabled */
158 CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
159 /* De-emphasis filter on DAC-3 disabled/enabled */
160 CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
161 /* High-pass filter on ADC_B disabled/enabled */
162 CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
163 /* High-pass filter on ADC_C disabled/enabled */
164 CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
165 /* Common mode on Port_A disabled/enabled */
166 CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
167 /* Common mode on Port_D disabled/enabled */
168 CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
169 /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
170 CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
171 /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700172 CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
Ian Minett95c6e9c2011-06-15 15:35:17 -0700173 /* ASI rate is 48kHz/96kHz */
174 CONTROL_FLAG_ASI_96KHZ = 22,
175 /* DAC power settings able to control attached ports no/yes */
176 CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
177 /* Clock Stop OK reporting is disabled/enabled */
178 CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
179 /* Number of control flags */
180 CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
181};
182
183/*
184 * Control parameter IDs
185 */
Ian Minett4aa3bb02012-09-20 20:29:15 -0700186enum control_param_id {
Ian Minett95c6e9c2011-06-15 15:35:17 -0700187 /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
188 CONTROL_PARAM_SPDIF1_SOURCE = 2,
189
190 /* Stream Control */
191
192 /* Select stream with the given ID */
193 CONTROL_PARAM_STREAM_ID = 24,
194 /* Source connection point for the selected stream */
195 CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
196 /* Destination connection point for the selected stream */
197 CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
198 /* Number of audio channels in the selected stream */
199 CONTROL_PARAM_STREAMS_CHANNELS = 27,
200 /*Enable control for the selected stream */
201 CONTROL_PARAM_STREAM_CONTROL = 28,
202
203 /* Connection Point Control */
204
205 /* Select connection point with the given ID */
206 CONTROL_PARAM_CONN_POINT_ID = 29,
207 /* Connection point sample rate */
208 CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
209
210 /* Node Control */
211
212 /* Select HDA node with the given ID */
213 CONTROL_PARAM_NODE_ID = 31
214};
215
216/*
217 * Dsp Io Status codes
218 */
219enum hda_vendor_status_dspio {
220 /* Success */
221 VENDOR_STATUS_DSPIO_OK = 0x00,
222 /* Busy, unable to accept new command, the host must retry */
223 VENDOR_STATUS_DSPIO_BUSY = 0x01,
224 /* SCP command queue is full */
225 VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
226 /* SCP response queue is empty */
227 VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
228};
229
230/*
231 * Chip Io Status codes
232 */
233enum hda_vendor_status_chipio {
234 /* Success */
235 VENDOR_STATUS_CHIPIO_OK = 0x00,
236 /* Busy, unable to accept new command, the host must retry */
237 VENDOR_STATUS_CHIPIO_BUSY = 0x01
238};
239
240/*
241 * CA0132 sample rate
242 */
243enum ca0132_sample_rate {
244 SR_6_000 = 0x00,
245 SR_8_000 = 0x01,
246 SR_9_600 = 0x02,
247 SR_11_025 = 0x03,
248 SR_16_000 = 0x04,
249 SR_22_050 = 0x05,
250 SR_24_000 = 0x06,
251 SR_32_000 = 0x07,
252 SR_44_100 = 0x08,
253 SR_48_000 = 0x09,
254 SR_88_200 = 0x0A,
255 SR_96_000 = 0x0B,
256 SR_144_000 = 0x0C,
257 SR_176_400 = 0x0D,
258 SR_192_000 = 0x0E,
259 SR_384_000 = 0x0F,
260
261 SR_COUNT = 0x10,
262
263 SR_RATE_UNKNOWN = 0x1F
264};
265
266/*
267 * Scp Helper function
268 */
269enum get_set {
270 IS_SET = 0,
271 IS_GET = 1,
272};
273
274/*
275 * Duplicated from ca0110 codec
276 */
277
278static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
279{
280 if (pin) {
Takashi Iwaicdd03ce2012-04-20 12:34:50 +0200281 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
Ian Minett95c6e9c2011-06-15 15:35:17 -0700282 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
283 snd_hda_codec_write(codec, pin, 0,
284 AC_VERB_SET_AMP_GAIN_MUTE,
285 AMP_OUT_UNMUTE);
286 }
Takashi Iwai8e13fc12012-08-08 17:26:54 +0200287 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
Ian Minett95c6e9c2011-06-15 15:35:17 -0700288 snd_hda_codec_write(codec, dac, 0,
289 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
290}
291
292static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
293{
294 if (pin) {
Takashi Iwai47408602012-04-20 13:06:53 +0200295 snd_hda_set_pin_ctl(codec, pin, PIN_IN |
296 snd_hda_get_default_vref(codec, pin));
Ian Minett95c6e9c2011-06-15 15:35:17 -0700297 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
298 snd_hda_codec_write(codec, pin, 0,
299 AC_VERB_SET_AMP_GAIN_MUTE,
300 AMP_IN_UNMUTE(0));
301 }
Takashi Iwai8e13fc12012-08-08 17:26:54 +0200302 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP))
Ian Minett95c6e9c2011-06-15 15:35:17 -0700303 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
304 AMP_IN_UNMUTE(0));
305}
306
307static char *dirstr[2] = { "Playback", "Capture" };
308
309static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
310 int chan, int dir)
311{
312 char namestr[44];
313 int type = dir ? HDA_INPUT : HDA_OUTPUT;
314 struct snd_kcontrol_new knew =
315 HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
David Henningssonc41999a2012-08-20 11:17:00 +0200316 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_MUTE) == 0) {
317 snd_printdd("Skipping '%s %s Switch' (no mute on node 0x%x)\n", pfx, dirstr[dir], nid);
318 return 0;
319 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700320 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
321 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
322}
323
324static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
325 int chan, int dir)
326{
327 char namestr[44];
328 int type = dir ? HDA_INPUT : HDA_OUTPUT;
329 struct snd_kcontrol_new knew =
330 HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
David Henningssonc41999a2012-08-20 11:17:00 +0200331 if ((query_amp_caps(codec, nid, type) & AC_AMPCAP_NUM_STEPS) == 0) {
332 snd_printdd("Skipping '%s %s Volume' (no amp on node 0x%x)\n", pfx, dirstr[dir], nid);
333 return 0;
334 }
Ian Minett95c6e9c2011-06-15 15:35:17 -0700335 sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
336 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
337}
338
339#define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
340#define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
341#define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
342#define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
343#define add_mono_switch(codec, nid, pfx, chan) \
344 _add_switch(codec, nid, pfx, chan, 0)
345#define add_mono_volume(codec, nid, pfx, chan) \
346 _add_volume(codec, nid, pfx, chan, 0)
347#define add_in_mono_switch(codec, nid, pfx, chan) \
348 _add_switch(codec, nid, pfx, chan, 1)
349#define add_in_mono_volume(codec, nid, pfx, chan) \
350 _add_volume(codec, nid, pfx, chan, 1)
351
Ian Minett01ef7db2012-09-20 20:29:16 -0700352enum dsp_download_state {
353 DSP_DOWNLOAD_FAILED = -1,
354 DSP_DOWNLOAD_INIT = 0,
355 DSP_DOWNLOADING = 1,
356 DSP_DOWNLOADED = 2
357};
358
Ian Minett01ef7db2012-09-20 20:29:16 -0700359/* retrieve parameters from hda format */
360#define get_hdafmt_chs(fmt) (fmt & 0xf)
361#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
362#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
363#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700364
365/*
366 * CA0132 specific
367 */
368
369struct ca0132_spec {
370 struct auto_pin_cfg autocfg;
371 struct hda_multi_out multiout;
372 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
373 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
374 hda_nid_t hp_dac;
375 hda_nid_t input_pins[AUTO_PIN_LAST];
376 hda_nid_t adcs[AUTO_PIN_LAST];
377 hda_nid_t dig_out;
378 hda_nid_t dig_in;
379 unsigned int num_inputs;
380 long curr_hp_switch;
381 long curr_hp_volume[2];
382 long curr_speaker_switch;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700383 const char *input_labels[AUTO_PIN_LAST];
384 struct hda_pcm pcm_rec[2]; /* PCM information */
Ian Minett01ef7db2012-09-20 20:29:16 -0700385
386 /* chip access */
387 struct mutex chipio_mutex; /* chip access mutex */
388 u32 curr_chip_addx;
389
390 /* DSP download related */
391 enum dsp_download_state dsp_state;
392 unsigned int dsp_stream_id;
393 unsigned int wait_scp;
394 unsigned int wait_scp_header;
395 unsigned int wait_num_data;
396 unsigned int scp_resp_header;
397 unsigned int scp_resp_data[4];
398 unsigned int scp_resp_count;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700399};
400
Ian Minett01ef7db2012-09-20 20:29:16 -0700401/*
402 * CA0132 codec access
403 */
404unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
405 unsigned int verb, unsigned int parm, unsigned int *res)
406{
407 unsigned int response;
408 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
409 *res = response;
410
411 return ((response == -1) ? -1 : 0);
412}
413
414static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
415 unsigned short converter_format, unsigned int *res)
416{
417 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
418 converter_format & 0xffff, res);
419}
420
421static int codec_set_converter_stream_channel(struct hda_codec *codec,
422 hda_nid_t nid, unsigned char stream,
423 unsigned char channel, unsigned int *res)
424{
425 unsigned char converter_stream_channel = 0;
426
427 converter_stream_channel = (stream << 4) | (channel & 0x0f);
428 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
429 converter_stream_channel, res);
430}
431
Ian Minett95c6e9c2011-06-15 15:35:17 -0700432/* Chip access helper function */
433static int chipio_send(struct hda_codec *codec,
434 unsigned int reg,
435 unsigned int data)
436{
437 unsigned int res;
438 int retry = 50;
439
440 /* send bits of data specified by reg */
441 do {
442 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
443 reg, data);
444 if (res == VENDOR_STATUS_CHIPIO_OK)
445 return 0;
446 } while (--retry);
447 return -EIO;
448}
449
450/*
451 * Write chip address through the vendor widget -- NOT protected by the Mutex!
452 */
453static int chipio_write_address(struct hda_codec *codec,
454 unsigned int chip_addx)
455{
Ian Minett4861af82012-09-20 20:29:20 -0700456 struct ca0132_spec *spec = codec->spec;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700457 int res;
458
Ian Minett4861af82012-09-20 20:29:20 -0700459 if (spec->curr_chip_addx == chip_addx)
460 return 0;
461
Ian Minett95c6e9c2011-06-15 15:35:17 -0700462 /* send low 16 bits of the address */
463 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
464 chip_addx & 0xffff);
465
466 if (res != -EIO) {
467 /* send high 16 bits of the address */
468 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
469 chip_addx >> 16);
470 }
471
Ian Minett4861af82012-09-20 20:29:20 -0700472 spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
473
Ian Minett95c6e9c2011-06-15 15:35:17 -0700474 return res;
475}
476
477/*
478 * Write data through the vendor widget -- NOT protected by the Mutex!
479 */
Ian Minett95c6e9c2011-06-15 15:35:17 -0700480static int chipio_write_data(struct hda_codec *codec, unsigned int data)
481{
482 int res;
483
484 /* send low 16 bits of the data */
485 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
486
487 if (res != -EIO) {
488 /* send high 16 bits of the data */
489 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
490 data >> 16);
491 }
492
493 return res;
494}
495
Ian Minettd5c21b82012-09-20 20:29:18 -0700496/*
497 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
498 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700499static int chipio_write_data_multiple(struct hda_codec *codec,
500 const u32 *data,
501 unsigned int count)
502{
503 int status = 0;
504
505 if (data == NULL) {
506 snd_printdd(KERN_ERR "chipio_write_data null ptr");
507 return -EINVAL;
508 }
509
510 while ((count-- != 0) && (status == 0))
511 status = chipio_write_data(codec, *data++);
512
513 return status;
514}
515
516
Ian Minett95c6e9c2011-06-15 15:35:17 -0700517/*
518 * Read data through the vendor widget -- NOT protected by the Mutex!
519 */
520static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
521{
522 int res;
523
524 /* post read */
525 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
526
527 if (res != -EIO) {
528 /* read status */
529 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
530 }
531
532 if (res != -EIO) {
533 /* read data */
534 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
535 VENDOR_CHIPIO_HIC_READ_DATA,
536 0);
537 }
538
539 return res;
540}
541
542/*
543 * Write given value to the given address through the chip I/O widget.
544 * protected by the Mutex
545 */
546static int chipio_write(struct hda_codec *codec,
547 unsigned int chip_addx, const unsigned int data)
548{
549 struct ca0132_spec *spec = codec->spec;
550 int err;
551
552 mutex_lock(&spec->chipio_mutex);
553
554 /* write the address, and if successful proceed to write data */
555 err = chipio_write_address(codec, chip_addx);
556 if (err < 0)
557 goto exit;
558
559 err = chipio_write_data(codec, data);
560 if (err < 0)
561 goto exit;
562
563exit:
564 mutex_unlock(&spec->chipio_mutex);
565 return err;
566}
567
Ian Minettd5c21b82012-09-20 20:29:18 -0700568/*
569 * Write multiple values to the given address through the chip I/O widget.
570 * protected by the Mutex
571 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700572static int chipio_write_multiple(struct hda_codec *codec,
573 u32 chip_addx,
574 const u32 *data,
575 unsigned int count)
576{
577 struct ca0132_spec *spec = codec->spec;
578 int status;
579
580 mutex_lock(&spec->chipio_mutex);
Ian Minett4861af82012-09-20 20:29:20 -0700581 status = chipio_write_address(codec, chip_addx);
Ian Minett01ef7db2012-09-20 20:29:16 -0700582 if (status < 0)
583 goto error;
584
585 status = chipio_write_data_multiple(codec, data, count);
586error:
587 mutex_unlock(&spec->chipio_mutex);
588
589 return status;
590}
591
Ian Minett95c6e9c2011-06-15 15:35:17 -0700592/*
593 * Read the given address through the chip I/O widget
594 * protected by the Mutex
595 */
596static int chipio_read(struct hda_codec *codec,
597 unsigned int chip_addx, unsigned int *data)
598{
599 struct ca0132_spec *spec = codec->spec;
600 int err;
601
602 mutex_lock(&spec->chipio_mutex);
603
604 /* write the address, and if successful proceed to write data */
605 err = chipio_write_address(codec, chip_addx);
606 if (err < 0)
607 goto exit;
608
609 err = chipio_read_data(codec, data);
610 if (err < 0)
611 goto exit;
612
613exit:
614 mutex_unlock(&spec->chipio_mutex);
615 return err;
616}
617
Ian Minettd5c21b82012-09-20 20:29:18 -0700618/*
619 * Set chip control flags through the chip I/O widget.
620 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700621static void chipio_set_control_flag(struct hda_codec *codec,
622 enum control_flag_id flag_id,
623 bool flag_state)
624{
625 unsigned int val;
626 unsigned int flag_bit;
627
628 flag_bit = (flag_state ? 1 : 0);
629 val = (flag_bit << 7) | (flag_id);
630 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
631 VENDOR_CHIPIO_FLAG_SET, val);
632}
633
Ian Minettd5c21b82012-09-20 20:29:18 -0700634/*
635 * Set chip parameters through the chip I/O widget.
636 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700637static void chipio_set_control_param(struct hda_codec *codec,
638 enum control_param_id param_id, int param_val)
639{
640 struct ca0132_spec *spec = codec->spec;
641 int val;
642
643 if ((param_id < 32) && (param_val < 8)) {
644 val = (param_val << 5) | (param_id);
645 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
646 VENDOR_CHIPIO_PARAM_SET, val);
647 } else {
648 mutex_lock(&spec->chipio_mutex);
649 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
650 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
651 VENDOR_CHIPIO_PARAM_EX_ID_SET,
652 param_id);
653 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
654 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
655 param_val);
656 }
657 mutex_unlock(&spec->chipio_mutex);
658 }
659}
660
Ian Minettd5c21b82012-09-20 20:29:18 -0700661/*
662 * Set sampling rate of the connection point.
663 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700664static void chipio_set_conn_rate(struct hda_codec *codec,
665 int connid, enum ca0132_sample_rate rate)
666{
667 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
668 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
669 rate);
670}
671
Ian Minettd5c21b82012-09-20 20:29:18 -0700672/*
673 * Enable clocks.
674 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700675static void chipio_enable_clocks(struct hda_codec *codec)
676{
677 struct ca0132_spec *spec = codec->spec;
678
679 mutex_lock(&spec->chipio_mutex);
680 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
681 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
682 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
683 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
684 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
685 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
686 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
687 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
688 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
689 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
690 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
691 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
692 mutex_unlock(&spec->chipio_mutex);
693}
694
695/*
696 * CA0132 DSP IO stuffs
697 */
698static int dspio_send(struct hda_codec *codec, unsigned int reg,
699 unsigned int data)
700{
701 unsigned int res;
702 int retry = 50;
703
704 /* send bits of data specified by reg to dsp */
705 do {
706 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
707 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
708 return res;
709 } while (--retry);
710
711 return -EIO;
712}
713
Ian Minettd5c21b82012-09-20 20:29:18 -0700714/*
715 * Wait for DSP to be ready for commands
716 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700717static void dspio_write_wait(struct hda_codec *codec)
718{
Ian Minett4861af82012-09-20 20:29:20 -0700719 int status;
720 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Ian Minett01ef7db2012-09-20 20:29:16 -0700721
Ian Minett01ef7db2012-09-20 20:29:16 -0700722 do {
Ian Minett4861af82012-09-20 20:29:20 -0700723 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
724 VENDOR_DSPIO_STATUS, 0);
725 if ((status == VENDOR_STATUS_DSPIO_OK) ||
726 (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
727 break;
728 msleep(1);
729 } while (time_before(jiffies, timeout));
Ian Minett01ef7db2012-09-20 20:29:16 -0700730}
731
Ian Minettd5c21b82012-09-20 20:29:18 -0700732/*
733 * Write SCP data to DSP
734 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700735static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
736{
737 struct ca0132_spec *spec = codec->spec;
738 int status;
739
740 dspio_write_wait(codec);
741
742 mutex_lock(&spec->chipio_mutex);
743 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
744 scp_data & 0xffff);
745 if (status < 0)
746 goto error;
747
748 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
749 scp_data >> 16);
750 if (status < 0)
751 goto error;
752
753 /* OK, now check if the write itself has executed*/
754 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
755 VENDOR_DSPIO_STATUS, 0);
756error:
757 mutex_unlock(&spec->chipio_mutex);
758
759 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
760 -EIO : 0;
761}
762
Ian Minettd5c21b82012-09-20 20:29:18 -0700763/*
764 * Write multiple SCP data to DSP
765 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700766static int dspio_write_multiple(struct hda_codec *codec,
767 unsigned int *buffer, unsigned int size)
768{
769 int status = 0;
770 unsigned int count;
771
772 if ((buffer == NULL))
773 return -EINVAL;
774
775 count = 0;
776 while (count < size) {
777 status = dspio_write(codec, *buffer++);
778 if (status != 0)
779 break;
780 count++;
781 }
782
783 return status;
784}
785
Ian Minettd5c21b82012-09-20 20:29:18 -0700786/*
787 * Construct the SCP header using corresponding fields
788 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700789static inline unsigned int
790make_scp_header(unsigned int target_id, unsigned int source_id,
791 unsigned int get_flag, unsigned int req,
792 unsigned int device_flag, unsigned int resp_flag,
793 unsigned int error_flag, unsigned int data_size)
794{
795 unsigned int header = 0;
796
797 header = (data_size & 0x1f) << 27;
798 header |= (error_flag & 0x01) << 26;
799 header |= (resp_flag & 0x01) << 25;
800 header |= (device_flag & 0x01) << 24;
801 header |= (req & 0x7f) << 17;
802 header |= (get_flag & 0x01) << 16;
803 header |= (source_id & 0xff) << 8;
804 header |= target_id & 0xff;
805
806 return header;
807}
808
Ian Minettd5c21b82012-09-20 20:29:18 -0700809/*
810 * Extract corresponding fields from SCP header
811 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700812static inline void
813extract_scp_header(unsigned int header,
814 unsigned int *target_id, unsigned int *source_id,
815 unsigned int *get_flag, unsigned int *req,
816 unsigned int *device_flag, unsigned int *resp_flag,
817 unsigned int *error_flag, unsigned int *data_size)
818{
819 if (data_size)
820 *data_size = (header >> 27) & 0x1f;
821 if (error_flag)
822 *error_flag = (header >> 26) & 0x01;
823 if (resp_flag)
824 *resp_flag = (header >> 25) & 0x01;
825 if (device_flag)
826 *device_flag = (header >> 24) & 0x01;
827 if (req)
828 *req = (header >> 17) & 0x7f;
829 if (get_flag)
830 *get_flag = (header >> 16) & 0x01;
831 if (source_id)
832 *source_id = (header >> 8) & 0xff;
833 if (target_id)
834 *target_id = header & 0xff;
835}
836
837#define SCP_MAX_DATA_WORDS (16)
838
839/* Structure to contain any SCP message */
840struct scp_msg {
841 unsigned int hdr;
842 unsigned int data[SCP_MAX_DATA_WORDS];
843};
844
Ian Minettd5c21b82012-09-20 20:29:18 -0700845/*
846 * Send SCP message to DSP
847 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700848static int dspio_send_scp_message(struct hda_codec *codec,
849 unsigned char *send_buf,
850 unsigned int send_buf_size,
851 unsigned char *return_buf,
852 unsigned int return_buf_size,
853 unsigned int *bytes_returned)
854{
855 struct ca0132_spec *spec = codec->spec;
856 int retry;
857 int status = -1;
858 unsigned int scp_send_size = 0;
859 unsigned int total_size;
860 bool waiting_for_resp = false;
861 unsigned int header;
862 struct scp_msg *ret_msg;
863 unsigned int resp_src_id, resp_target_id;
864 unsigned int data_size, src_id, target_id, get_flag, device_flag;
865
866 if (bytes_returned)
867 *bytes_returned = 0;
868
869 /* get scp header from buffer */
870 header = *((unsigned int *)send_buf);
871 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
872 &device_flag, NULL, NULL, &data_size);
873 scp_send_size = data_size + 1;
874 total_size = (scp_send_size * 4);
875
876 if (send_buf_size < total_size)
877 return -EINVAL;
878
879 if (get_flag || device_flag) {
880 if (!return_buf || return_buf_size < 4 || !bytes_returned)
881 return -EINVAL;
882
883 spec->wait_scp_header = *((unsigned int *)send_buf);
884
885 /* swap source id with target id */
886 resp_target_id = src_id;
887 resp_src_id = target_id;
888 spec->wait_scp_header &= 0xffff0000;
889 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
890 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
891 spec->wait_scp = 1;
892 waiting_for_resp = true;
893 }
894
895 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
896 scp_send_size);
897 if (status < 0) {
898 spec->wait_scp = 0;
899 return status;
900 }
901
902 if (waiting_for_resp) {
903 memset(return_buf, 0, return_buf_size);
904 retry = 50;
905 do {
906 msleep(20);
907 } while (spec->wait_scp && (--retry != 0));
908 waiting_for_resp = false;
909 if (retry != 0) {
910 ret_msg = (struct scp_msg *)return_buf;
911 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
912 memcpy(&ret_msg->data, spec->scp_resp_data,
913 spec->wait_num_data);
914 *bytes_returned = (spec->scp_resp_count + 1) * 4;
915 status = 0;
916 } else {
917 status = -EIO;
918 }
919 spec->wait_scp = 0;
920 }
921
922 return status;
923}
924
Ian Minettd5c21b82012-09-20 20:29:18 -0700925/**
926 * Prepare and send the SCP message to DSP
927 * @codec: the HDA codec
928 * @mod_id: ID of the DSP module to send the command
929 * @req: ID of request to send to the DSP module
930 * @dir: SET or GET
931 * @data: pointer to the data to send with the request, request specific
932 * @len: length of the data, in bytes
933 * @reply: point to the buffer to hold data returned for a reply
934 * @reply_len: length of the reply buffer returned from GET
935 *
936 * Returns zero or a negative error code.
937 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700938static int dspio_scp(struct hda_codec *codec,
939 int mod_id, int req, int dir, void *data, unsigned int len,
940 void *reply, unsigned int *reply_len)
941{
942 int status = 0;
943 struct scp_msg scp_send, scp_reply;
944 unsigned int ret_bytes, send_size, ret_size;
945 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
946 unsigned int reply_data_size;
947
948 memset(&scp_send, 0, sizeof(scp_send));
949 memset(&scp_reply, 0, sizeof(scp_reply));
950
951 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
952 return -EINVAL;
953
954 if (dir == SCP_GET && reply == NULL) {
955 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
956 return -EINVAL;
957 }
958
959 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
960 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
961 return -EINVAL;
962 }
963
964 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
965 0, 0, 0, len/sizeof(unsigned int));
966 if (data != NULL && len > 0) {
967 len = min((unsigned int)(sizeof(scp_send.data)), len);
968 memcpy(scp_send.data, data, len);
969 }
970
971 ret_bytes = 0;
972 send_size = sizeof(unsigned int) + len;
973 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
974 send_size, (unsigned char *)&scp_reply,
975 sizeof(scp_reply), &ret_bytes);
976
977 if (status < 0) {
978 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
979 return status;
980 }
981
982 /* extract send and reply headers members */
983 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
984 NULL, NULL, NULL, NULL, NULL);
985 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
986 &reply_resp_flag, &reply_error_flag,
987 &reply_data_size);
988
989 if (!send_get_flag)
990 return 0;
991
992 if (reply_resp_flag && !reply_error_flag) {
993 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
994 / sizeof(unsigned int);
995
996 if (*reply_len < ret_size*sizeof(unsigned int)) {
997 snd_printdd(KERN_ERR "reply too long for buf");
998 return -EINVAL;
999 } else if (ret_size != reply_data_size) {
1000 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1001 return -EINVAL;
1002 } else {
1003 *reply_len = ret_size*sizeof(unsigned int);
1004 memcpy(reply, scp_reply.data, *reply_len);
1005 }
1006 } else {
1007 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1008 return -EIO;
1009 }
1010
1011 return status;
1012}
1013
Ian Minettd5c21b82012-09-20 20:29:18 -07001014/*
1015 * Allocate a DSP DMA channel via an SCP message
1016 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001017static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1018{
1019 int status = 0;
1020 unsigned int size = sizeof(dma_chan);
1021
1022 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1023 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1024 SCP_GET, NULL, 0, dma_chan, &size);
1025
1026 if (status < 0) {
1027 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1028 return status;
1029 }
1030
1031 if ((*dma_chan + 1) == 0) {
1032 snd_printdd(KERN_INFO "no free dma channels to allocate");
1033 return -EBUSY;
1034 }
1035
1036 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1037 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1038
1039 return status;
1040}
1041
Ian Minettd5c21b82012-09-20 20:29:18 -07001042/*
1043 * Free a DSP DMA via an SCP message
1044 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001045static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1046{
1047 int status = 0;
1048 unsigned int dummy = 0;
1049
1050 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1051 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1052
1053 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1054 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1055
1056 if (status < 0) {
1057 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1058 return status;
1059 }
1060
1061 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1062
1063 return status;
1064}
1065
1066/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001067 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001068 */
1069static int dsp_set_run_state(struct hda_codec *codec)
1070{
1071 unsigned int dbg_ctrl_reg;
1072 unsigned int halt_state;
1073 int err;
1074
1075 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1076 if (err < 0)
1077 return err;
1078
1079 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1080 DSP_DBGCNTL_STATE_LOBIT;
1081
1082 if (halt_state != 0) {
1083 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1084 DSP_DBGCNTL_SS_MASK);
1085 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1086 dbg_ctrl_reg);
1087 if (err < 0)
1088 return err;
1089
1090 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1091 DSP_DBGCNTL_EXEC_MASK;
1092 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1093 dbg_ctrl_reg);
1094 if (err < 0)
1095 return err;
1096 }
1097
1098 return 0;
1099}
1100
Ian Minettd5c21b82012-09-20 20:29:18 -07001101/*
1102 * Reset the DSP
1103 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001104static int dsp_reset(struct hda_codec *codec)
1105{
1106 unsigned int res;
1107 int retry = 20;
1108
1109 snd_printdd("dsp_reset\n");
1110 do {
1111 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1112 retry--;
1113 } while (res == -EIO && retry);
1114
1115 if (!retry) {
1116 snd_printdd("dsp_reset timeout\n");
1117 return -EIO;
1118 }
1119
1120 return 0;
1121}
1122
Ian Minettd5c21b82012-09-20 20:29:18 -07001123/*
1124 * Convert chip address to DSP address
1125 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001126static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1127 bool *code, bool *yram)
1128{
1129 *code = *yram = false;
1130
1131 if (UC_RANGE(chip_addx, 1)) {
1132 *code = true;
1133 return UC_OFF(chip_addx);
1134 } else if (X_RANGE_ALL(chip_addx, 1)) {
1135 return X_OFF(chip_addx);
1136 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1137 *yram = true;
1138 return Y_OFF(chip_addx);
1139 }
1140
1141 return (unsigned int)INVALID_CHIP_ADDRESS;
1142}
1143
Ian Minettd5c21b82012-09-20 20:29:18 -07001144/*
1145 * Check if the DSP DMA is active
1146 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001147static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1148{
1149 unsigned int dma_chnlstart_reg;
1150
1151 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1152
1153 return ((dma_chnlstart_reg & (1 <<
1154 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1155}
1156
1157static int dsp_dma_setup_common(struct hda_codec *codec,
1158 unsigned int chip_addx,
1159 unsigned int dma_chan,
1160 unsigned int port_map_mask,
1161 bool ovly)
1162{
1163 int status = 0;
1164 unsigned int chnl_prop;
1165 unsigned int dsp_addx;
1166 unsigned int active;
1167 bool code, yram;
1168
1169 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1170
1171 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1172 snd_printdd(KERN_ERR "dma chan num invalid");
1173 return -EINVAL;
1174 }
1175
1176 if (dsp_is_dma_active(codec, dma_chan)) {
1177 snd_printdd(KERN_ERR "dma already active");
1178 return -EBUSY;
1179 }
1180
1181 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1182
1183 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1184 snd_printdd(KERN_ERR "invalid chip addr");
1185 return -ENXIO;
1186 }
1187
1188 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1189 active = 0;
1190
1191 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1192
1193 if (ovly) {
1194 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1195 &chnl_prop);
1196
1197 if (status < 0) {
1198 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1199 return status;
1200 }
1201 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1202 }
1203
1204 if (!code)
1205 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1206 else
1207 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1208
1209 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1210
1211 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1212 if (status < 0) {
1213 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1214 return status;
1215 }
1216 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1217
1218 if (ovly) {
1219 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1220 &active);
1221
1222 if (status < 0) {
1223 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1224 return status;
1225 }
1226 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1227 }
1228
1229 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1230 DSPDMAC_ACTIVE_AAR_MASK;
1231
1232 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1233 if (status < 0) {
1234 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1235 return status;
1236 }
1237
1238 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1239
1240 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1241 port_map_mask);
1242 if (status < 0) {
1243 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1244 return status;
1245 }
1246 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1247
1248 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1249 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1250 if (status < 0) {
1251 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1252 return status;
1253 }
1254 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1255
1256 snd_printdd(
1257 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1258 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1259 chip_addx, dsp_addx, dma_chan,
1260 port_map_mask, chnl_prop, active);
1261
1262 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1263
1264 return 0;
1265}
1266
Ian Minettd5c21b82012-09-20 20:29:18 -07001267/*
1268 * Setup the DSP DMA per-transfer-specific registers
1269 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001270static int dsp_dma_setup(struct hda_codec *codec,
1271 unsigned int chip_addx,
1272 unsigned int count,
1273 unsigned int dma_chan)
1274{
1275 int status = 0;
1276 bool code, yram;
1277 unsigned int dsp_addx;
1278 unsigned int addr_field;
1279 unsigned int incr_field;
1280 unsigned int base_cnt;
1281 unsigned int cur_cnt;
1282 unsigned int dma_cfg = 0;
1283 unsigned int adr_ofs = 0;
1284 unsigned int xfr_cnt = 0;
1285 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1286 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1287
1288 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1289
1290 if (count > max_dma_count) {
1291 snd_printdd(KERN_ERR "count too big");
1292 return -EINVAL;
1293 }
1294
1295 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1296 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1297 snd_printdd(KERN_ERR "invalid chip addr");
1298 return -ENXIO;
1299 }
1300
1301 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1302
1303 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1304 incr_field = 0;
1305
1306 if (!code) {
1307 addr_field <<= 1;
1308 if (yram)
1309 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1310
1311 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1312 }
1313
1314 dma_cfg = addr_field + incr_field;
1315 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1316 dma_cfg);
1317 if (status < 0) {
1318 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1319 return status;
1320 }
1321 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1322
1323 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1324 (code ? 0 : 1));
1325
1326 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1327 adr_ofs);
1328 if (status < 0) {
1329 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1330 return status;
1331 }
1332 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1333
1334 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1335
1336 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1337
1338 xfr_cnt = base_cnt | cur_cnt;
1339
1340 status = chipio_write(codec,
1341 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1342 if (status < 0) {
1343 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1344 return status;
1345 }
1346 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1347
1348 snd_printdd(
1349 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1350 "ADROFS=0x%x, XFRCNT=0x%x\n",
1351 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1352
1353 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1354
1355 return 0;
1356}
1357
Ian Minettd5c21b82012-09-20 20:29:18 -07001358/*
1359 * Start the DSP DMA
1360 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001361static int dsp_dma_start(struct hda_codec *codec,
1362 unsigned int dma_chan, bool ovly)
1363{
1364 unsigned int reg = 0;
1365 int status = 0;
1366
1367 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1368
1369 if (ovly) {
1370 status = chipio_read(codec,
1371 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1372
1373 if (status < 0) {
1374 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1375 return status;
1376 }
1377 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1378
1379 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1380 DSPDMAC_CHNLSTART_DIS_MASK);
1381 }
1382
1383 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1384 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1385 if (status < 0) {
1386 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1387 return status;
1388 }
1389 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1390
1391 return status;
1392}
1393
Ian Minettd5c21b82012-09-20 20:29:18 -07001394/*
1395 * Stop the DSP DMA
1396 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001397static int dsp_dma_stop(struct hda_codec *codec,
1398 unsigned int dma_chan, bool ovly)
1399{
1400 unsigned int reg = 0;
1401 int status = 0;
1402
1403 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1404
1405 if (ovly) {
1406 status = chipio_read(codec,
1407 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1408
1409 if (status < 0) {
1410 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1411 return status;
1412 }
1413 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1414 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1415 DSPDMAC_CHNLSTART_DIS_MASK);
1416 }
1417
1418 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1419 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1420 if (status < 0) {
1421 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1422 return status;
1423 }
1424 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1425
1426 return status;
1427}
1428
Ian Minettd5c21b82012-09-20 20:29:18 -07001429/**
1430 * Allocate router ports
1431 *
1432 * @codec: the HDA codec
1433 * @num_chans: number of channels in the stream
1434 * @ports_per_channel: number of ports per channel
1435 * @start_device: start device
1436 * @port_map: pointer to the port list to hold the allocated ports
1437 *
1438 * Returns zero or a negative error code.
1439 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001440static int dsp_allocate_router_ports(struct hda_codec *codec,
1441 unsigned int num_chans,
1442 unsigned int ports_per_channel,
1443 unsigned int start_device,
1444 unsigned int *port_map)
1445{
1446 int status = 0;
1447 int res;
1448 u8 val;
1449
1450 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1451 if (status < 0)
1452 return status;
1453
1454 val = start_device << 6;
1455 val |= (ports_per_channel - 1) << 4;
1456 val |= num_chans - 1;
1457
1458 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1459 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1460 val);
1461
1462 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1463 VENDOR_CHIPIO_PORT_ALLOC_SET,
1464 MEM_CONNID_DSP);
1465
1466 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1467 if (status < 0)
1468 return status;
1469
1470 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1471 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1472
1473 *port_map = res;
1474
1475 return (res < 0) ? res : 0;
1476}
1477
Ian Minettd5c21b82012-09-20 20:29:18 -07001478/*
1479 * Free router ports
1480 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001481static int dsp_free_router_ports(struct hda_codec *codec)
1482{
1483 int status = 0;
1484
1485 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1486 if (status < 0)
1487 return status;
1488
1489 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1490 VENDOR_CHIPIO_PORT_FREE_SET,
1491 MEM_CONNID_DSP);
1492
1493 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1494
1495 return status;
1496}
1497
Ian Minettd5c21b82012-09-20 20:29:18 -07001498/*
1499 * Allocate DSP ports for the download stream
1500 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001501static int dsp_allocate_ports(struct hda_codec *codec,
1502 unsigned int num_chans,
1503 unsigned int rate_multi, unsigned int *port_map)
1504{
1505 int status;
1506
1507 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1508
1509 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1510 snd_printdd(KERN_ERR "bad rate multiple");
1511 return -EINVAL;
1512 }
1513
1514 status = dsp_allocate_router_ports(codec, num_chans,
1515 rate_multi, 0, port_map);
1516
1517 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1518
1519 return status;
1520}
1521
Ian Minett01ef7db2012-09-20 20:29:16 -07001522static int dsp_allocate_ports_format(struct hda_codec *codec,
1523 const unsigned short fmt,
1524 unsigned int *port_map)
1525{
1526 int status;
1527 unsigned int num_chans;
1528
1529 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1530 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1531 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1532
1533 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1534 snd_printdd(KERN_ERR "bad rate multiple");
1535 return -EINVAL;
1536 }
1537
1538 num_chans = get_hdafmt_chs(fmt) + 1;
1539
1540 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1541
1542 return status;
1543}
1544
1545/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001546 * free DSP ports
1547 */
1548static int dsp_free_ports(struct hda_codec *codec)
1549{
1550 int status;
1551
1552 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
1553
1554 status = dsp_free_router_ports(codec);
1555 if (status < 0) {
1556 snd_printdd(KERN_ERR "free router ports fail");
1557 return status;
1558 }
1559 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
1560
1561 return status;
1562}
1563
1564/*
Ian Minett01ef7db2012-09-20 20:29:16 -07001565 * HDA DMA engine stuffs for DSP code download
1566 */
1567struct dma_engine {
1568 struct hda_codec *codec;
1569 unsigned short m_converter_format;
1570 struct snd_dma_buffer *dmab;
1571 unsigned int buf_size;
1572};
1573
1574
1575enum dma_state {
1576 DMA_STATE_STOP = 0,
1577 DMA_STATE_RUN = 1
1578};
1579
1580static int dma_convert_to_hda_format(
Ian Minette97249d2012-09-20 20:29:21 -07001581 unsigned int sample_rate,
1582 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07001583 unsigned short *hda_format)
1584{
1585 unsigned int format_val;
1586
1587 format_val = snd_hda_calc_stream_format(
Ian Minette97249d2012-09-20 20:29:21 -07001588 sample_rate,
1589 channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07001590 SNDRV_PCM_FORMAT_S32_LE,
Ian Minette97249d2012-09-20 20:29:21 -07001591 32, 0);
Ian Minett01ef7db2012-09-20 20:29:16 -07001592
1593 if (hda_format)
1594 *hda_format = (unsigned short)format_val;
1595
1596 return 0;
1597}
1598
Ian Minettd5c21b82012-09-20 20:29:18 -07001599/*
1600 * Reset DMA for DSP download
1601 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001602static int dma_reset(struct dma_engine *dma)
1603{
1604 struct hda_codec *codec = dma->codec;
1605 struct ca0132_spec *spec = codec->spec;
1606 int status;
1607
1608 if (dma->dmab)
1609 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
1610
1611 status = snd_hda_codec_load_dsp_prepare(codec,
1612 dma->m_converter_format,
1613 dma->buf_size,
1614 dma->dmab);
1615 if (status < 0)
1616 return status;
1617 spec->dsp_stream_id = status;
1618 return 0;
1619}
1620
1621static int dma_set_state(struct dma_engine *dma, enum dma_state state)
1622{
1623 bool cmd;
1624
1625 snd_printdd("dma_set_state state=%d\n", state);
1626
1627 switch (state) {
1628 case DMA_STATE_STOP:
1629 cmd = false;
1630 break;
1631 case DMA_STATE_RUN:
1632 cmd = true;
1633 break;
1634 default:
1635 return 0;
1636 }
1637
1638 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
1639 return 0;
1640}
1641
1642static unsigned int dma_get_buffer_size(struct dma_engine *dma)
1643{
1644 return dma->dmab->bytes;
1645}
1646
1647static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
1648{
1649 return dma->dmab->area;
1650}
1651
1652static int dma_xfer(struct dma_engine *dma,
1653 const unsigned int *data,
1654 unsigned int count)
1655{
1656 memcpy(dma->dmab->area, data, count);
1657 return 0;
1658}
1659
1660static void dma_get_converter_format(
1661 struct dma_engine *dma,
1662 unsigned short *format)
1663{
1664 if (format)
1665 *format = dma->m_converter_format;
1666}
1667
1668static unsigned int dma_get_stream_id(struct dma_engine *dma)
1669{
1670 struct ca0132_spec *spec = dma->codec->spec;
1671
1672 return spec->dsp_stream_id;
1673}
1674
1675struct dsp_image_seg {
1676 u32 magic;
1677 u32 chip_addr;
1678 u32 count;
1679 u32 data[0];
1680};
1681
1682static const u32 g_magic_value = 0x4c46584d;
1683static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
1684
1685static bool is_valid(const struct dsp_image_seg *p)
1686{
1687 return p->magic == g_magic_value;
1688}
1689
1690static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
1691{
1692 return g_chip_addr_magic_value == p->chip_addr;
1693}
1694
1695static bool is_last(const struct dsp_image_seg *p)
1696{
1697 return p->count == 0;
1698}
1699
1700static size_t dsp_sizeof(const struct dsp_image_seg *p)
1701{
1702 return sizeof(*p) + p->count*sizeof(u32);
1703}
1704
1705static const struct dsp_image_seg *get_next_seg_ptr(
1706 const struct dsp_image_seg *p)
1707{
1708 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
1709}
1710
1711/*
1712 * CA0132 chip DSP transfer stuffs. For DSP download.
1713 */
1714#define INVALID_DMA_CHANNEL (~0UL)
1715
Ian Minettd5c21b82012-09-20 20:29:18 -07001716/*
1717 * Program a list of address/data pairs via the ChipIO widget.
1718 * The segment data is in the format of successive pairs of words.
1719 * These are repeated as indicated by the segment's count field.
1720 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001721static int dspxfr_hci_write(struct hda_codec *codec,
1722 const struct dsp_image_seg *fls)
1723{
1724 int status;
1725 const u32 *data;
1726 unsigned int count;
1727
1728 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
1729 snd_printdd(KERN_ERR "hci_write invalid params");
1730 return -EINVAL;
1731 }
1732
1733 count = fls->count;
1734 data = (u32 *)(fls->data);
1735 while (count >= 2) {
1736 status = chipio_write(codec, data[0], data[1]);
1737 if (status < 0) {
1738 snd_printdd(KERN_ERR "hci_write chipio failed");
1739 return status;
1740 }
1741 count -= 2;
1742 data += 2;
1743 }
1744 return 0;
1745}
1746
Ian Minettd5c21b82012-09-20 20:29:18 -07001747/**
1748 * Write a block of data into DSP code or data RAM using pre-allocated
1749 * DMA engine.
1750 *
1751 * @codec: the HDA codec
1752 * @fls: pointer to a fast load image
1753 * @reloc: Relocation address for loading single-segment overlays, or 0 for
1754 * no relocation
1755 * @dma_engine: pointer to DMA engine to be used for DSP download
1756 * @dma_chan: The number of DMA channels used for DSP download
1757 * @port_map_mask: port mapping
1758 * @ovly: TRUE if overlay format is required
1759 *
1760 * Returns zero or a negative error code.
1761 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001762static int dspxfr_one_seg(struct hda_codec *codec,
1763 const struct dsp_image_seg *fls,
1764 unsigned int reloc,
1765 struct dma_engine *dma_engine,
1766 unsigned int dma_chan,
1767 unsigned int port_map_mask,
1768 bool ovly)
1769{
1770 int status;
1771 bool comm_dma_setup_done = false;
1772 const unsigned int *data;
1773 unsigned int chip_addx;
1774 unsigned int words_to_write;
1775 unsigned int buffer_size_words;
1776 unsigned char *buffer_addx;
1777 unsigned short hda_format;
1778 unsigned int sample_rate_div;
1779 unsigned int sample_rate_mul;
1780 unsigned int num_chans;
1781 unsigned int hda_frame_size_words;
1782 unsigned int remainder_words;
1783 const u32 *data_remainder;
1784 u32 chip_addx_remainder;
1785 unsigned int run_size_words;
1786 const struct dsp_image_seg *hci_write = NULL;
1787 int retry;
1788
1789 if (fls == NULL)
1790 return -EINVAL;
1791 if (is_hci_prog_list_seg(fls)) {
1792 hci_write = fls;
1793 fls = get_next_seg_ptr(fls);
1794 }
1795
1796 if (hci_write && (!fls || is_last(fls))) {
1797 snd_printdd("hci_write\n");
1798 return dspxfr_hci_write(codec, hci_write);
1799 }
1800
1801 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
1802 snd_printdd("Invalid Params\n");
1803 return -EINVAL;
1804 }
1805
1806 data = fls->data;
1807 chip_addx = fls->chip_addr,
1808 words_to_write = fls->count;
1809
1810 if (!words_to_write)
1811 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
1812 if (reloc)
1813 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
1814
1815 if (!UC_RANGE(chip_addx, words_to_write) &&
1816 !X_RANGE_ALL(chip_addx, words_to_write) &&
1817 !Y_RANGE_ALL(chip_addx, words_to_write)) {
1818 snd_printdd("Invalid chip_addx Params\n");
1819 return -EINVAL;
1820 }
1821
1822 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
1823 sizeof(u32);
1824
1825 buffer_addx = dma_get_buffer_addr(dma_engine);
1826
1827 if (buffer_addx == NULL) {
1828 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
1829 return -EINVAL;
1830 }
1831
1832 dma_get_converter_format(dma_engine, &hda_format);
1833 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
1834 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
1835 num_chans = get_hdafmt_chs(hda_format) + 1;
1836
1837 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
1838 (num_chans * sample_rate_mul / sample_rate_div));
1839
1840 buffer_size_words = min(buffer_size_words,
1841 (unsigned int)(UC_RANGE(chip_addx, 1) ?
1842 65536 : 32768));
1843 buffer_size_words -= buffer_size_words % hda_frame_size_words;
1844 snd_printdd(
1845 "chpadr=0x%08x frmsz=%u nchan=%u "
1846 "rate_mul=%u div=%u bufsz=%u\n",
1847 chip_addx, hda_frame_size_words, num_chans,
1848 sample_rate_mul, sample_rate_div, buffer_size_words);
1849
1850 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
1851 (buffer_size_words < hda_frame_size_words)) {
1852 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
1853 return -EINVAL;
1854 }
1855
1856 remainder_words = words_to_write % hda_frame_size_words;
1857 data_remainder = data;
1858 chip_addx_remainder = chip_addx;
1859
1860 data += remainder_words;
1861 chip_addx += remainder_words*sizeof(u32);
1862 words_to_write -= remainder_words;
1863
1864 while (words_to_write != 0) {
1865 run_size_words = min(buffer_size_words, words_to_write);
1866 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
1867 words_to_write, run_size_words, remainder_words);
1868 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
1869 if (!comm_dma_setup_done) {
1870 status = dsp_dma_stop(codec, dma_chan, ovly);
1871 if (status < 0)
1872 return -EIO;
1873 status = dsp_dma_setup_common(codec, chip_addx,
1874 dma_chan, port_map_mask, ovly);
1875 if (status < 0)
1876 return status;
1877 comm_dma_setup_done = true;
1878 }
1879
1880 status = dsp_dma_setup(codec, chip_addx,
1881 run_size_words, dma_chan);
1882 if (status < 0)
1883 return status;
1884 status = dsp_dma_start(codec, dma_chan, ovly);
1885 if (status < 0)
1886 return status;
1887 if (!dsp_is_dma_active(codec, dma_chan)) {
1888 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
1889 return -EIO;
1890 }
1891 status = dma_set_state(dma_engine, DMA_STATE_RUN);
1892 if (status < 0)
1893 return status;
1894 if (remainder_words != 0) {
1895 status = chipio_write_multiple(codec,
1896 chip_addx_remainder,
1897 data_remainder,
1898 remainder_words);
1899 remainder_words = 0;
1900 }
1901 if (hci_write) {
1902 status = dspxfr_hci_write(codec, hci_write);
1903 hci_write = NULL;
1904 }
1905 retry = 5000;
1906 while (dsp_is_dma_active(codec, dma_chan)) {
1907 if (--retry <= 0)
1908 break;
1909 }
1910 snd_printdd(KERN_INFO "+++++ DMA complete");
1911 dma_set_state(dma_engine, DMA_STATE_STOP);
1912 dma_reset(dma_engine);
1913
1914 if (status < 0)
1915 return status;
1916
1917 data += run_size_words;
1918 chip_addx += run_size_words*sizeof(u32);
1919 words_to_write -= run_size_words;
1920 }
1921
1922 if (remainder_words != 0) {
1923 status = chipio_write_multiple(codec, chip_addx_remainder,
1924 data_remainder, remainder_words);
1925 }
1926
1927 return status;
1928}
1929
Ian Minettd5c21b82012-09-20 20:29:18 -07001930/**
1931 * Write the entire DSP image of a DSP code/data overlay to DSP memories
1932 *
1933 * @codec: the HDA codec
1934 * @fls_data: pointer to a fast load image
1935 * @reloc: Relocation address for loading single-segment overlays, or 0 for
1936 * no relocation
Ian Minette97249d2012-09-20 20:29:21 -07001937 * @sample_rate: sampling rate of the stream used for DSP download
1938 * @number_channels: channels of the stream used for DSP download
Ian Minettd5c21b82012-09-20 20:29:18 -07001939 * @ovly: TRUE if overlay format is required
1940 *
1941 * Returns zero or a negative error code.
1942 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001943static int dspxfr_image(struct hda_codec *codec,
1944 const struct dsp_image_seg *fls_data,
Ian Minette97249d2012-09-20 20:29:21 -07001945 unsigned int reloc,
1946 unsigned int sample_rate,
1947 unsigned short channels,
Ian Minett01ef7db2012-09-20 20:29:16 -07001948 bool ovly)
1949{
1950 struct ca0132_spec *spec = codec->spec;
1951 int status;
1952 unsigned short hda_format = 0;
1953 unsigned int response;
1954 unsigned char stream_id = 0;
1955 struct dma_engine *dma_engine;
1956 unsigned int dma_chan;
1957 unsigned int port_map_mask;
1958
1959 if (fls_data == NULL)
1960 return -EINVAL;
1961
1962 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
1963 if (!dma_engine) {
1964 status = -ENOMEM;
1965 goto exit;
1966 }
1967 memset((void *)dma_engine, 0, sizeof(*dma_engine));
1968
1969 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
1970 if (!dma_engine->dmab) {
1971 status = -ENOMEM;
1972 goto exit;
1973 }
1974
1975 dma_engine->codec = codec;
Ian Minette97249d2012-09-20 20:29:21 -07001976 dma_convert_to_hda_format(sample_rate, channels, &hda_format);
Ian Minett01ef7db2012-09-20 20:29:16 -07001977 dma_engine->m_converter_format = hda_format;
1978 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
1979 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
1980
1981 dma_chan = 0;
1982
1983 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
1984 hda_format, &response);
1985
1986 if (status < 0) {
1987 snd_printdd(KERN_ERR "set converter format fail");
1988 goto exit;
1989 }
1990
1991 status = snd_hda_codec_load_dsp_prepare(codec,
1992 dma_engine->m_converter_format,
1993 dma_engine->buf_size,
1994 dma_engine->dmab);
1995 if (status < 0)
1996 goto exit;
1997 spec->dsp_stream_id = status;
1998
1999 if (ovly) {
2000 status = dspio_alloc_dma_chan(codec, &dma_chan);
2001 if (status < 0) {
2002 snd_printdd(KERN_ERR "alloc dmachan fail");
2003 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2004 goto exit;
2005 }
2006 }
2007
2008 port_map_mask = 0;
2009 status = dsp_allocate_ports_format(codec, hda_format,
2010 &port_map_mask);
2011 if (status < 0) {
2012 snd_printdd(KERN_ERR "alloc ports fail");
2013 goto exit;
2014 }
2015
2016 stream_id = dma_get_stream_id(dma_engine);
2017 status = codec_set_converter_stream_channel(codec,
2018 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2019 if (status < 0) {
2020 snd_printdd(KERN_ERR "set stream chan fail");
2021 goto exit;
2022 }
2023
2024 while ((fls_data != NULL) && !is_last(fls_data)) {
2025 if (!is_valid(fls_data)) {
2026 snd_printdd(KERN_ERR "FLS check fail");
2027 status = -EINVAL;
2028 goto exit;
2029 }
2030 status = dspxfr_one_seg(codec, fls_data, reloc,
2031 dma_engine, dma_chan,
2032 port_map_mask, ovly);
2033 if (status < 0)
2034 break;
2035
2036 if (is_hci_prog_list_seg(fls_data))
2037 fls_data = get_next_seg_ptr(fls_data);
2038
2039 if ((fls_data != NULL) && !is_last(fls_data))
2040 fls_data = get_next_seg_ptr(fls_data);
2041 }
2042
2043 if (port_map_mask != 0)
2044 status = dsp_free_ports(codec);
2045
2046 if (status < 0)
2047 goto exit;
2048
2049 status = codec_set_converter_stream_channel(codec,
2050 WIDGET_CHIP_CTRL, 0, 0, &response);
2051
2052exit:
2053 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2054 dspio_free_dma_chan(codec, dma_chan);
2055
2056 if (dma_engine->dmab)
2057 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2058 kfree(dma_engine->dmab);
2059 kfree(dma_engine);
2060
2061 return status;
2062}
2063
2064/*
2065 * CA0132 DSP download stuffs.
2066 */
2067static void dspload_post_setup(struct hda_codec *codec)
2068{
2069 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2070
2071 /*set DSP speaker to 2.0 configuration*/
2072 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2073 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2074
2075 /*update write pointer*/
2076 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2077}
2078
Ian Minettd5c21b82012-09-20 20:29:18 -07002079/**
2080 * Download DSP from a DSP Image Fast Load structure. This structure is a
2081 * linear, non-constant sized element array of structures, each of which
2082 * contain the count of the data to be loaded, the data itself, and the
2083 * corresponding starting chip address of the starting data location.
2084 *
2085 * @codec: the HDA codec
2086 * @fls: pointer to a fast load image
2087 * @ovly: TRUE if overlay format is required
2088 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2089 * no relocation
2090 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2091 * @router_chans: number of audio router channels to be allocated (0 means use
2092 * internal defaults; max is 32)
2093 *
2094 * Returns zero or a negative error code.
2095 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002096static int dspload_image(struct hda_codec *codec,
2097 const struct dsp_image_seg *fls,
2098 bool ovly,
2099 unsigned int reloc,
2100 bool autostart,
2101 int router_chans)
2102{
2103 int status = 0;
Ian Minette97249d2012-09-20 20:29:21 -07002104 unsigned int sample_rate;
2105 unsigned short channels;
Ian Minett01ef7db2012-09-20 20:29:16 -07002106
2107 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2108 if (router_chans == 0) {
2109 if (!ovly)
2110 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2111 else
2112 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2113 }
2114
Ian Minette97249d2012-09-20 20:29:21 -07002115 sample_rate = 48000;
2116 channels = (unsigned short)router_chans;
Ian Minett01ef7db2012-09-20 20:29:16 -07002117
Ian Minette97249d2012-09-20 20:29:21 -07002118 while (channels > 16) {
2119 sample_rate *= 2;
2120 channels /= 2;
Ian Minett01ef7db2012-09-20 20:29:16 -07002121 }
2122
Ian Minett01ef7db2012-09-20 20:29:16 -07002123 do {
2124 snd_printdd(KERN_INFO "Ready to program DMA");
2125 if (!ovly)
2126 status = dsp_reset(codec);
2127
2128 if (status < 0)
2129 break;
2130
2131 snd_printdd(KERN_INFO "dsp_reset() complete");
Ian Minette97249d2012-09-20 20:29:21 -07002132 status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
2133 ovly);
Ian Minett01ef7db2012-09-20 20:29:16 -07002134
2135 if (status < 0)
2136 break;
2137
2138 snd_printdd(KERN_INFO "dspxfr_image() complete");
2139 if (autostart && !ovly) {
2140 dspload_post_setup(codec);
2141 status = dsp_set_run_state(codec);
2142 }
2143
2144 snd_printdd(KERN_INFO "LOAD FINISHED");
2145 } while (0);
2146
2147 return status;
2148}
2149
Ian Minettc3b4eea22012-09-20 20:29:17 -07002150static const struct firmware *fw_efx;
2151
2152static int request_firmware_cached(const struct firmware **firmware_p,
2153 const char *name, struct device *device)
2154{
2155 if (*firmware_p)
2156 return 0; /* already loaded */
2157 return request_firmware(firmware_p, name, device);
2158}
2159
2160static void release_cached_firmware(void)
2161{
2162 if (fw_efx) {
2163 release_firmware(fw_efx);
2164 fw_efx = NULL;
2165 }
2166}
2167
Ian Minett01ef7db2012-09-20 20:29:16 -07002168static bool dspload_is_loaded(struct hda_codec *codec)
2169{
2170 unsigned int data = 0;
2171 int status = 0;
2172
2173 status = chipio_read(codec, 0x40004, &data);
2174 if ((status < 0) || (data != 1))
2175 return false;
2176
2177 return true;
2178}
2179
2180static bool dspload_wait_loaded(struct hda_codec *codec)
2181{
2182 int retry = 100;
2183
2184 do {
2185 msleep(20);
2186 if (dspload_is_loaded(codec)) {
2187 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2188 return true;
2189 }
2190 } while (--retry);
2191
2192 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2193 return false;
2194}
2195
Ian Minett95c6e9c2011-06-15 15:35:17 -07002196/*
Ian Minett95c6e9c2011-06-15 15:35:17 -07002197 * PCM callbacks
2198 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002199static int ca0132_playback_pcm_open(struct hda_pcm_stream *hinfo,
2200 struct hda_codec *codec,
2201 struct snd_pcm_substream *substream)
2202{
2203 struct ca0132_spec *spec = codec->spec;
2204 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2205 hinfo);
2206}
2207
Ian Minett95c6e9c2011-06-15 15:35:17 -07002208static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2209 struct hda_codec *codec,
2210 unsigned int stream_tag,
2211 unsigned int format,
2212 struct snd_pcm_substream *substream)
2213{
2214 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002215 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2216 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002217}
2218
2219static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2220 struct hda_codec *codec,
2221 struct snd_pcm_substream *substream)
2222{
2223 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002224 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002225}
2226
2227/*
2228 * Digital out
2229 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002230static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2231 struct hda_codec *codec,
2232 struct snd_pcm_substream *substream)
2233{
2234 struct ca0132_spec *spec = codec->spec;
2235 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2236}
2237
Ian Minett95c6e9c2011-06-15 15:35:17 -07002238static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2239 struct hda_codec *codec,
2240 unsigned int stream_tag,
2241 unsigned int format,
2242 struct snd_pcm_substream *substream)
2243{
2244 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002245 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2246 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002247}
2248
2249static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2250 struct hda_codec *codec,
2251 struct snd_pcm_substream *substream)
2252{
2253 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002254 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002255}
2256
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002257static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2258 struct hda_codec *codec,
2259 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002260{
2261 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002262 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002263}
2264
2265/*
2266 */
2267static struct hda_pcm_stream ca0132_pcm_analog_playback = {
2268 .substreams = 1,
2269 .channels_min = 2,
2270 .channels_max = 2,
2271 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002272 .open = ca0132_playback_pcm_open,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002273 .prepare = ca0132_playback_pcm_prepare,
2274 .cleanup = ca0132_playback_pcm_cleanup
2275 },
2276};
2277
2278static struct hda_pcm_stream ca0132_pcm_analog_capture = {
2279 .substreams = 1,
2280 .channels_min = 2,
2281 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002282};
2283
2284static struct hda_pcm_stream ca0132_pcm_digital_playback = {
2285 .substreams = 1,
2286 .channels_min = 2,
2287 .channels_max = 2,
2288 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002289 .open = ca0132_dig_playback_pcm_open,
2290 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002291 .prepare = ca0132_dig_playback_pcm_prepare,
2292 .cleanup = ca0132_dig_playback_pcm_cleanup
2293 },
2294};
2295
2296static struct hda_pcm_stream ca0132_pcm_digital_capture = {
2297 .substreams = 1,
2298 .channels_min = 2,
2299 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002300};
2301
2302static int ca0132_build_pcms(struct hda_codec *codec)
2303{
2304 struct ca0132_spec *spec = codec->spec;
2305 struct hda_pcm *info = spec->pcm_rec;
2306
2307 codec->pcm_info = info;
2308 codec->num_pcms = 0;
2309
2310 info->name = "CA0132 Analog";
2311 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
2312 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
2313 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
2314 spec->multiout.max_channels;
2315 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
2316 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
2317 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
2318 codec->num_pcms++;
2319
2320 if (!spec->dig_out && !spec->dig_in)
2321 return 0;
2322
2323 info++;
2324 info->name = "CA0132 Digital";
2325 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2326 if (spec->dig_out) {
2327 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2328 ca0132_pcm_digital_playback;
2329 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
2330 }
2331 if (spec->dig_in) {
2332 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2333 ca0132_pcm_digital_capture;
2334 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
2335 }
2336 codec->num_pcms++;
2337
2338 return 0;
2339}
2340
2341#define REG_CODEC_MUTE 0x18b014
2342#define REG_CODEC_HP_VOL_L 0x18b070
2343#define REG_CODEC_HP_VOL_R 0x18b074
2344
2345static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol)
2347{
2348 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2349 struct ca0132_spec *spec = codec->spec;
2350 long *valp = ucontrol->value.integer.value;
2351
2352 *valp = spec->curr_hp_switch;
2353 return 0;
2354}
2355
2356static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct ca0132_spec *spec = codec->spec;
2361 long *valp = ucontrol->value.integer.value;
2362 unsigned int data;
2363 int err;
2364
2365 /* any change? */
2366 if (spec->curr_hp_switch == *valp)
2367 return 0;
2368
2369 snd_hda_power_up(codec);
2370
2371 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2372 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002373 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002374
2375 /* *valp 0 is mute, 1 is unmute */
2376 data = (data & 0x7f) | (*valp ? 0 : 0x80);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002377 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002378 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002379 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002380
2381 spec->curr_hp_switch = *valp;
2382
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002383 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002384 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002385 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002386}
2387
2388static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390{
2391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2392 struct ca0132_spec *spec = codec->spec;
2393 long *valp = ucontrol->value.integer.value;
2394
2395 *valp = spec->curr_speaker_switch;
2396 return 0;
2397}
2398
2399static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
2401{
2402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2403 struct ca0132_spec *spec = codec->spec;
2404 long *valp = ucontrol->value.integer.value;
2405 unsigned int data;
2406 int err;
2407
2408 /* any change? */
2409 if (spec->curr_speaker_switch == *valp)
2410 return 0;
2411
2412 snd_hda_power_up(codec);
2413
2414 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2415 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002416 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002417
2418 /* *valp 0 is mute, 1 is unmute */
2419 data = (data & 0xef) | (*valp ? 0 : 0x10);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002420 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002421 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002422 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002423
2424 spec->curr_speaker_switch = *valp;
2425
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002426 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002427 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002428 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002429}
2430
2431static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
2433{
2434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2435 struct ca0132_spec *spec = codec->spec;
2436 long *valp = ucontrol->value.integer.value;
2437
2438 *valp++ = spec->curr_hp_volume[0];
2439 *valp = spec->curr_hp_volume[1];
2440 return 0;
2441}
2442
2443static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2445{
2446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2447 struct ca0132_spec *spec = codec->spec;
2448 long *valp = ucontrol->value.integer.value;
2449 long left_vol, right_vol;
2450 unsigned int data;
2451 int val;
2452 int err;
2453
2454 left_vol = *valp++;
2455 right_vol = *valp;
2456
2457 /* any change? */
2458 if ((spec->curr_hp_volume[0] == left_vol) &&
2459 (spec->curr_hp_volume[1] == right_vol))
2460 return 0;
2461
2462 snd_hda_power_up(codec);
2463
2464 err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
2465 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002466 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002467
2468 val = 31 - left_vol;
2469 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002470 err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002471 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002472 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002473
2474 val = 31 - right_vol;
2475 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002476 err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002477 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002478 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002479
2480 spec->curr_hp_volume[0] = left_vol;
2481 spec->curr_hp_volume[1] = right_vol;
2482
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002483 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002484 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002485 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002486}
2487
2488static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
2489{
2490 struct snd_kcontrol_new knew =
2491 HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
2492 nid, 1, 0, HDA_OUTPUT);
2493 knew.get = ca0132_hp_switch_get;
2494 knew.put = ca0132_hp_switch_put;
2495 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2496}
2497
2498static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
2499{
2500 struct snd_kcontrol_new knew =
2501 HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
2502 nid, 3, 0, HDA_OUTPUT);
2503 knew.get = ca0132_hp_volume_get;
2504 knew.put = ca0132_hp_volume_put;
2505 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2506}
2507
2508static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
2509{
2510 struct snd_kcontrol_new knew =
2511 HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
2512 nid, 1, 0, HDA_OUTPUT);
2513 knew.get = ca0132_speaker_switch_get;
2514 knew.put = ca0132_speaker_switch_put;
2515 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2516}
2517
2518static void ca0132_fix_hp_caps(struct hda_codec *codec)
2519{
2520 struct ca0132_spec *spec = codec->spec;
2521 struct auto_pin_cfg *cfg = &spec->autocfg;
2522 unsigned int caps;
2523
2524 /* set mute-capable, 1db step, 32 steps, ofs 6 */
2525 caps = 0x80031f06;
2526 snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
2527}
2528
2529static int ca0132_build_controls(struct hda_codec *codec)
2530{
2531 struct ca0132_spec *spec = codec->spec;
2532 struct auto_pin_cfg *cfg = &spec->autocfg;
2533 int i, err;
2534
2535 if (spec->multiout.num_dacs) {
2536 err = add_speaker_switch(codec, spec->out_pins[0]);
2537 if (err < 0)
2538 return err;
2539 }
2540
2541 if (cfg->hp_outs) {
2542 ca0132_fix_hp_caps(codec);
2543 err = add_hp_switch(codec, cfg->hp_pins[0]);
2544 if (err < 0)
2545 return err;
2546 err = add_hp_volume(codec, cfg->hp_pins[0]);
2547 if (err < 0)
2548 return err;
2549 }
2550
2551 for (i = 0; i < spec->num_inputs; i++) {
2552 const char *label = spec->input_labels[i];
2553
2554 err = add_in_switch(codec, spec->adcs[i], label);
2555 if (err < 0)
2556 return err;
2557 err = add_in_volume(codec, spec->adcs[i], label);
2558 if (err < 0)
2559 return err;
2560 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
2561 /* add Mic-Boost */
2562 err = add_in_mono_volume(codec, spec->input_pins[i],
2563 "Mic Boost", 1);
2564 if (err < 0)
2565 return err;
2566 }
2567 }
2568
2569 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +02002570 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
2571 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002572 if (err < 0)
2573 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002574 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002575 if (err < 0)
2576 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002577 /* spec->multiout.share_spdif = 1; */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002578 }
2579
2580 if (spec->dig_in) {
2581 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
2582 if (err < 0)
2583 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002584 }
2585 return 0;
2586}
2587
2588
2589static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
2590{
2591 /* Set Creative extension */
2592 snd_printdd("SET CREATIVE EXTENSION\n");
2593 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2594 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
2595 enable);
2596 msleep(20);
2597}
2598
2599
2600static void ca0132_config(struct hda_codec *codec)
2601{
2602 struct ca0132_spec *spec = codec->spec;
2603 struct auto_pin_cfg *cfg = &spec->autocfg;
2604
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002605 codec->pcm_format_first = 1;
2606 codec->no_sticky_stream = 1;
2607
Ian Minett95c6e9c2011-06-15 15:35:17 -07002608 /* line-outs */
2609 cfg->line_outs = 1;
2610 cfg->line_out_pins[0] = 0x0b; /* front */
2611 cfg->line_out_type = AUTO_PIN_LINE_OUT;
2612
2613 spec->dacs[0] = 0x02;
2614 spec->out_pins[0] = 0x0b;
2615 spec->multiout.dac_nids = spec->dacs;
2616 spec->multiout.num_dacs = 1;
2617 spec->multiout.max_channels = 2;
2618
2619 /* headphone */
2620 cfg->hp_outs = 1;
2621 cfg->hp_pins[0] = 0x0f;
2622
2623 spec->hp_dac = 0;
2624 spec->multiout.hp_nid = 0;
2625
2626 /* inputs */
2627 cfg->num_inputs = 2; /* Mic-in and line-in */
2628 cfg->inputs[0].pin = 0x12;
2629 cfg->inputs[0].type = AUTO_PIN_MIC;
2630 cfg->inputs[1].pin = 0x11;
2631 cfg->inputs[1].type = AUTO_PIN_LINE_IN;
2632
2633 /* Mic-in */
2634 spec->input_pins[0] = 0x12;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002635 spec->input_labels[0] = "Mic";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002636 spec->adcs[0] = 0x07;
2637
2638 /* Line-In */
2639 spec->input_pins[1] = 0x11;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002640 spec->input_labels[1] = "Line";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002641 spec->adcs[1] = 0x08;
2642 spec->num_inputs = 2;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002643
2644 /* SPDIF I/O */
2645 spec->dig_out = 0x05;
2646 spec->multiout.dig_out_nid = spec->dig_out;
2647 cfg->dig_out_pins[0] = 0x0c;
2648 cfg->dig_outs = 1;
2649 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
2650 spec->dig_in = 0x09;
2651 cfg->dig_in_pin = 0x0e;
2652 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002653}
2654
2655static void ca0132_init_chip(struct hda_codec *codec)
2656{
2657 struct ca0132_spec *spec = codec->spec;
2658
2659 mutex_init(&spec->chipio_mutex);
2660}
2661
2662static void ca0132_exit_chip(struct hda_codec *codec)
2663{
2664 /* put any chip cleanup stuffs here. */
2665}
2666
Ian Minett01ef7db2012-09-20 20:29:16 -07002667static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
2668{
2669 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
2670 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
2671 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
2672 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
2673 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
2674 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
2675
2676 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
2677 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
2678 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
2679}
2680
2681static bool ca0132_download_dsp_images(struct hda_codec *codec)
2682{
2683 bool dsp_loaded = false;
2684 const struct dsp_image_seg *dsp_os_image;
Ian Minett01ef7db2012-09-20 20:29:16 -07002685
Ian Minettc3b4eea22012-09-20 20:29:17 -07002686 if (request_firmware_cached(&fw_efx, EFX_FILE,
2687 codec->bus->card->dev) != 0)
Ian Minett01ef7db2012-09-20 20:29:16 -07002688 return false;
2689
Ian Minettc3b4eea22012-09-20 20:29:17 -07002690 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
Ian Minett01ef7db2012-09-20 20:29:16 -07002691 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
2692 dsp_loaded = dspload_wait_loaded(codec);
2693
Ian Minett01ef7db2012-09-20 20:29:16 -07002694 return dsp_loaded;
2695}
2696
2697static void ca0132_download_dsp(struct hda_codec *codec)
2698{
2699 struct ca0132_spec *spec = codec->spec;
2700
2701 spec->dsp_state = DSP_DOWNLOAD_INIT;
2702
2703 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
2704 chipio_enable_clocks(codec);
2705 spec->dsp_state = DSP_DOWNLOADING;
2706 if (!ca0132_download_dsp_images(codec))
2707 spec->dsp_state = DSP_DOWNLOAD_FAILED;
2708 else
2709 spec->dsp_state = DSP_DOWNLOADED;
2710 }
2711
2712 if (spec->dsp_state == DSP_DOWNLOADED)
2713 ca0132_set_dsp_msr(codec, true);
2714}
2715
Ian Minett95c6e9c2011-06-15 15:35:17 -07002716static int ca0132_init(struct hda_codec *codec)
2717{
2718 struct ca0132_spec *spec = codec->spec;
2719 struct auto_pin_cfg *cfg = &spec->autocfg;
2720 int i;
2721
Ian Minett01ef7db2012-09-20 20:29:16 -07002722#ifdef CONFIG_SND_HDA_DSP_LOADER
2723 ca0132_download_dsp(codec);
2724#endif
2725
Ian Minett95c6e9c2011-06-15 15:35:17 -07002726 for (i = 0; i < spec->multiout.num_dacs; i++) {
2727 init_output(codec, spec->out_pins[i],
2728 spec->multiout.dac_nids[i]);
2729 }
2730 init_output(codec, cfg->hp_pins[0], spec->hp_dac);
2731 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
2732
2733 for (i = 0; i < spec->num_inputs; i++)
2734 init_input(codec, spec->input_pins[i], spec->adcs[i]);
2735
2736 init_input(codec, cfg->dig_in_pin, spec->dig_in);
2737
2738 ca0132_set_ct_ext(codec, 1);
2739
2740 return 0;
2741}
2742
2743
2744static void ca0132_free(struct hda_codec *codec)
2745{
2746 ca0132_set_ct_ext(codec, 0);
2747 ca0132_exit_chip(codec);
2748 kfree(codec->spec);
2749}
2750
2751static struct hda_codec_ops ca0132_patch_ops = {
2752 .build_controls = ca0132_build_controls,
2753 .build_pcms = ca0132_build_pcms,
2754 .init = ca0132_init,
2755 .free = ca0132_free,
2756};
2757
2758
2759
2760static int patch_ca0132(struct hda_codec *codec)
2761{
2762 struct ca0132_spec *spec;
2763
2764 snd_printdd("patch_ca0132\n");
2765
2766 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2767 if (!spec)
2768 return -ENOMEM;
2769 codec->spec = spec;
2770
2771 ca0132_init_chip(codec);
2772
2773 ca0132_config(codec);
2774
2775 codec->patch_ops = ca0132_patch_ops;
2776
2777 return 0;
2778}
2779
2780/*
2781 * patch entries
2782 */
2783static struct hda_codec_preset snd_hda_preset_ca0132[] = {
2784 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
2785 {} /* terminator */
2786};
2787
2788MODULE_ALIAS("snd-hda-codec-id:11020011");
2789
2790MODULE_LICENSE("GPL");
2791MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
2792
2793static struct hda_codec_preset_list ca0132_list = {
2794 .preset = snd_hda_preset_ca0132,
2795 .owner = THIS_MODULE,
2796};
2797
2798static int __init patch_ca0132_init(void)
2799{
2800 return snd_hda_add_codec_preset(&ca0132_list);
2801}
2802
2803static void __exit patch_ca0132_exit(void)
2804{
Ian Minettc3b4eea22012-09-20 20:29:17 -07002805 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07002806 snd_hda_delete_codec_preset(&ca0132_list);
2807}
2808
2809module_init(patch_ca0132_init)
2810module_exit(patch_ca0132_exit)