blob: 4d8a7edfd308ab1c8cb201dc904f7aef77275017 [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
359struct hda_stream_format {
360 unsigned int sample_rate;
361 unsigned short valid_bits_per_sample;
362 unsigned short container_size;
363 unsigned short number_channels;
364};
365
366/* retrieve parameters from hda format */
367#define get_hdafmt_chs(fmt) (fmt & 0xf)
368#define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
369#define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
370#define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
Ian Minett95c6e9c2011-06-15 15:35:17 -0700371
372/*
373 * CA0132 specific
374 */
375
376struct ca0132_spec {
377 struct auto_pin_cfg autocfg;
378 struct hda_multi_out multiout;
379 hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
380 hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
381 hda_nid_t hp_dac;
382 hda_nid_t input_pins[AUTO_PIN_LAST];
383 hda_nid_t adcs[AUTO_PIN_LAST];
384 hda_nid_t dig_out;
385 hda_nid_t dig_in;
386 unsigned int num_inputs;
387 long curr_hp_switch;
388 long curr_hp_volume[2];
389 long curr_speaker_switch;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700390 const char *input_labels[AUTO_PIN_LAST];
391 struct hda_pcm pcm_rec[2]; /* PCM information */
Ian Minett01ef7db2012-09-20 20:29:16 -0700392
393 /* chip access */
394 struct mutex chipio_mutex; /* chip access mutex */
395 u32 curr_chip_addx;
396
397 /* DSP download related */
398 enum dsp_download_state dsp_state;
399 unsigned int dsp_stream_id;
400 unsigned int wait_scp;
401 unsigned int wait_scp_header;
402 unsigned int wait_num_data;
403 unsigned int scp_resp_header;
404 unsigned int scp_resp_data[4];
405 unsigned int scp_resp_count;
Ian Minett95c6e9c2011-06-15 15:35:17 -0700406};
407
Ian Minett01ef7db2012-09-20 20:29:16 -0700408/*
409 * CA0132 codec access
410 */
411unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
412 unsigned int verb, unsigned int parm, unsigned int *res)
413{
414 unsigned int response;
415 response = snd_hda_codec_read(codec, nid, 0, verb, parm);
416 *res = response;
417
418 return ((response == -1) ? -1 : 0);
419}
420
421static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
422 unsigned short converter_format, unsigned int *res)
423{
424 return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
425 converter_format & 0xffff, res);
426}
427
428static int codec_set_converter_stream_channel(struct hda_codec *codec,
429 hda_nid_t nid, unsigned char stream,
430 unsigned char channel, unsigned int *res)
431{
432 unsigned char converter_stream_channel = 0;
433
434 converter_stream_channel = (stream << 4) | (channel & 0x0f);
435 return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
436 converter_stream_channel, res);
437}
438
Ian Minett95c6e9c2011-06-15 15:35:17 -0700439/* Chip access helper function */
440static int chipio_send(struct hda_codec *codec,
441 unsigned int reg,
442 unsigned int data)
443{
444 unsigned int res;
445 int retry = 50;
446
447 /* send bits of data specified by reg */
448 do {
449 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
450 reg, data);
451 if (res == VENDOR_STATUS_CHIPIO_OK)
452 return 0;
453 } while (--retry);
454 return -EIO;
455}
456
457/*
458 * Write chip address through the vendor widget -- NOT protected by the Mutex!
459 */
460static int chipio_write_address(struct hda_codec *codec,
461 unsigned int chip_addx)
462{
463 int res;
464
465 /* send low 16 bits of the address */
466 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
467 chip_addx & 0xffff);
468
469 if (res != -EIO) {
470 /* send high 16 bits of the address */
471 res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
472 chip_addx >> 16);
473 }
474
475 return res;
476}
477
Ian Minett01ef7db2012-09-20 20:29:16 -0700478static int chipio_write_addx(struct hda_codec *codec, u32 chip_addx)
479{
480 struct ca0132_spec *spec = codec->spec;
481 int status;
482
483 if (spec->curr_chip_addx == chip_addx)
484 return 0;
485
486 /* send low 16 bits of the address */
487 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
488 chip_addx & 0xffff);
489
490 if (status < 0)
491 return status;
492
493 /* send high 16 bits of the address */
494 status = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
495 chip_addx >> 16);
496
497 spec->curr_chip_addx = (status < 0) ? ~0UL : chip_addx;
498
499 return status;
500}
501
Ian Minett95c6e9c2011-06-15 15:35:17 -0700502/*
503 * Write data through the vendor widget -- NOT protected by the Mutex!
504 */
505
506static int chipio_write_data(struct hda_codec *codec, unsigned int data)
507{
508 int res;
509
510 /* send low 16 bits of the data */
511 res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
512
513 if (res != -EIO) {
514 /* send high 16 bits of the data */
515 res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
516 data >> 16);
517 }
518
519 return res;
520}
521
Ian Minett01ef7db2012-09-20 20:29:16 -0700522static int chipio_write_data_multiple(struct hda_codec *codec,
523 const u32 *data,
524 unsigned int count)
525{
526 int status = 0;
527
528 if (data == NULL) {
529 snd_printdd(KERN_ERR "chipio_write_data null ptr");
530 return -EINVAL;
531 }
532
533 while ((count-- != 0) && (status == 0))
534 status = chipio_write_data(codec, *data++);
535
536 return status;
537}
538
539
Ian Minett95c6e9c2011-06-15 15:35:17 -0700540/*
541 * Read data through the vendor widget -- NOT protected by the Mutex!
542 */
543static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
544{
545 int res;
546
547 /* post read */
548 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
549
550 if (res != -EIO) {
551 /* read status */
552 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
553 }
554
555 if (res != -EIO) {
556 /* read data */
557 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
558 VENDOR_CHIPIO_HIC_READ_DATA,
559 0);
560 }
561
562 return res;
563}
564
565/*
566 * Write given value to the given address through the chip I/O widget.
567 * protected by the Mutex
568 */
569static int chipio_write(struct hda_codec *codec,
570 unsigned int chip_addx, const unsigned int data)
571{
572 struct ca0132_spec *spec = codec->spec;
573 int err;
574
575 mutex_lock(&spec->chipio_mutex);
576
577 /* write the address, and if successful proceed to write data */
578 err = chipio_write_address(codec, chip_addx);
579 if (err < 0)
580 goto exit;
581
582 err = chipio_write_data(codec, data);
583 if (err < 0)
584 goto exit;
585
586exit:
587 mutex_unlock(&spec->chipio_mutex);
588 return err;
589}
590
Ian Minett01ef7db2012-09-20 20:29:16 -0700591static int chipio_write_multiple(struct hda_codec *codec,
592 u32 chip_addx,
593 const u32 *data,
594 unsigned int count)
595{
596 struct ca0132_spec *spec = codec->spec;
597 int status;
598
599 mutex_lock(&spec->chipio_mutex);
600 status = chipio_write_addx(codec, chip_addx);
601 if (status < 0)
602 goto error;
603
604 status = chipio_write_data_multiple(codec, data, count);
605error:
606 mutex_unlock(&spec->chipio_mutex);
607
608 return status;
609}
610
Ian Minett95c6e9c2011-06-15 15:35:17 -0700611/*
612 * Read the given address through the chip I/O widget
613 * protected by the Mutex
614 */
615static int chipio_read(struct hda_codec *codec,
616 unsigned int chip_addx, unsigned int *data)
617{
618 struct ca0132_spec *spec = codec->spec;
619 int err;
620
621 mutex_lock(&spec->chipio_mutex);
622
623 /* write the address, and if successful proceed to write data */
624 err = chipio_write_address(codec, chip_addx);
625 if (err < 0)
626 goto exit;
627
628 err = chipio_read_data(codec, data);
629 if (err < 0)
630 goto exit;
631
632exit:
633 mutex_unlock(&spec->chipio_mutex);
634 return err;
635}
636
Ian Minett01ef7db2012-09-20 20:29:16 -0700637static void chipio_set_control_flag(struct hda_codec *codec,
638 enum control_flag_id flag_id,
639 bool flag_state)
640{
641 unsigned int val;
642 unsigned int flag_bit;
643
644 flag_bit = (flag_state ? 1 : 0);
645 val = (flag_bit << 7) | (flag_id);
646 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
647 VENDOR_CHIPIO_FLAG_SET, val);
648}
649
650static void chipio_set_control_param(struct hda_codec *codec,
651 enum control_param_id param_id, int param_val)
652{
653 struct ca0132_spec *spec = codec->spec;
654 int val;
655
656 if ((param_id < 32) && (param_val < 8)) {
657 val = (param_val << 5) | (param_id);
658 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
659 VENDOR_CHIPIO_PARAM_SET, val);
660 } else {
661 mutex_lock(&spec->chipio_mutex);
662 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
663 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
664 VENDOR_CHIPIO_PARAM_EX_ID_SET,
665 param_id);
666 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
667 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
668 param_val);
669 }
670 mutex_unlock(&spec->chipio_mutex);
671 }
672}
673
674static void chipio_set_conn_rate(struct hda_codec *codec,
675 int connid, enum ca0132_sample_rate rate)
676{
677 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
678 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
679 rate);
680}
681
682static void chipio_enable_clocks(struct hda_codec *codec)
683{
684 struct ca0132_spec *spec = codec->spec;
685
686 mutex_lock(&spec->chipio_mutex);
687 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
688 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
689 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
690 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
691 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
692 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
693 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
694 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
695 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
696 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
697 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
698 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
699 mutex_unlock(&spec->chipio_mutex);
700}
701
702/*
703 * CA0132 DSP IO stuffs
704 */
705static int dspio_send(struct hda_codec *codec, unsigned int reg,
706 unsigned int data)
707{
708 unsigned int res;
709 int retry = 50;
710
711 /* send bits of data specified by reg to dsp */
712 do {
713 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
714 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
715 return res;
716 } while (--retry);
717
718 return -EIO;
719}
720
721static void dspio_write_wait(struct hda_codec *codec)
722{
723 int cur_val, prv_val;
724 int retry = 50;
725
726 cur_val = 0;
727 do {
728 prv_val = cur_val;
729 msleep(20);
730 dspio_send(codec, VENDOR_DSPIO_SCP_POST_COUNT_QUERY, 1);
731 dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
732 cur_val = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
733 VENDOR_DSPIO_SCP_READ_COUNT, 0);
734 } while (cur_val && (cur_val == prv_val) && --retry);
735}
736
737static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
738{
739 struct ca0132_spec *spec = codec->spec;
740 int status;
741
742 dspio_write_wait(codec);
743
744 mutex_lock(&spec->chipio_mutex);
745 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
746 scp_data & 0xffff);
747 if (status < 0)
748 goto error;
749
750 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
751 scp_data >> 16);
752 if (status < 0)
753 goto error;
754
755 /* OK, now check if the write itself has executed*/
756 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
757 VENDOR_DSPIO_STATUS, 0);
758error:
759 mutex_unlock(&spec->chipio_mutex);
760
761 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
762 -EIO : 0;
763}
764
765static int dspio_write_multiple(struct hda_codec *codec,
766 unsigned int *buffer, unsigned int size)
767{
768 int status = 0;
769 unsigned int count;
770
771 if ((buffer == NULL))
772 return -EINVAL;
773
774 count = 0;
775 while (count < size) {
776 status = dspio_write(codec, *buffer++);
777 if (status != 0)
778 break;
779 count++;
780 }
781
782 return status;
783}
784
785static inline unsigned int
786make_scp_header(unsigned int target_id, unsigned int source_id,
787 unsigned int get_flag, unsigned int req,
788 unsigned int device_flag, unsigned int resp_flag,
789 unsigned int error_flag, unsigned int data_size)
790{
791 unsigned int header = 0;
792
793 header = (data_size & 0x1f) << 27;
794 header |= (error_flag & 0x01) << 26;
795 header |= (resp_flag & 0x01) << 25;
796 header |= (device_flag & 0x01) << 24;
797 header |= (req & 0x7f) << 17;
798 header |= (get_flag & 0x01) << 16;
799 header |= (source_id & 0xff) << 8;
800 header |= target_id & 0xff;
801
802 return header;
803}
804
805static inline void
806extract_scp_header(unsigned int header,
807 unsigned int *target_id, unsigned int *source_id,
808 unsigned int *get_flag, unsigned int *req,
809 unsigned int *device_flag, unsigned int *resp_flag,
810 unsigned int *error_flag, unsigned int *data_size)
811{
812 if (data_size)
813 *data_size = (header >> 27) & 0x1f;
814 if (error_flag)
815 *error_flag = (header >> 26) & 0x01;
816 if (resp_flag)
817 *resp_flag = (header >> 25) & 0x01;
818 if (device_flag)
819 *device_flag = (header >> 24) & 0x01;
820 if (req)
821 *req = (header >> 17) & 0x7f;
822 if (get_flag)
823 *get_flag = (header >> 16) & 0x01;
824 if (source_id)
825 *source_id = (header >> 8) & 0xff;
826 if (target_id)
827 *target_id = header & 0xff;
828}
829
830#define SCP_MAX_DATA_WORDS (16)
831
832/* Structure to contain any SCP message */
833struct scp_msg {
834 unsigned int hdr;
835 unsigned int data[SCP_MAX_DATA_WORDS];
836};
837
838static int dspio_send_scp_message(struct hda_codec *codec,
839 unsigned char *send_buf,
840 unsigned int send_buf_size,
841 unsigned char *return_buf,
842 unsigned int return_buf_size,
843 unsigned int *bytes_returned)
844{
845 struct ca0132_spec *spec = codec->spec;
846 int retry;
847 int status = -1;
848 unsigned int scp_send_size = 0;
849 unsigned int total_size;
850 bool waiting_for_resp = false;
851 unsigned int header;
852 struct scp_msg *ret_msg;
853 unsigned int resp_src_id, resp_target_id;
854 unsigned int data_size, src_id, target_id, get_flag, device_flag;
855
856 if (bytes_returned)
857 *bytes_returned = 0;
858
859 /* get scp header from buffer */
860 header = *((unsigned int *)send_buf);
861 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
862 &device_flag, NULL, NULL, &data_size);
863 scp_send_size = data_size + 1;
864 total_size = (scp_send_size * 4);
865
866 if (send_buf_size < total_size)
867 return -EINVAL;
868
869 if (get_flag || device_flag) {
870 if (!return_buf || return_buf_size < 4 || !bytes_returned)
871 return -EINVAL;
872
873 spec->wait_scp_header = *((unsigned int *)send_buf);
874
875 /* swap source id with target id */
876 resp_target_id = src_id;
877 resp_src_id = target_id;
878 spec->wait_scp_header &= 0xffff0000;
879 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
880 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
881 spec->wait_scp = 1;
882 waiting_for_resp = true;
883 }
884
885 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
886 scp_send_size);
887 if (status < 0) {
888 spec->wait_scp = 0;
889 return status;
890 }
891
892 if (waiting_for_resp) {
893 memset(return_buf, 0, return_buf_size);
894 retry = 50;
895 do {
896 msleep(20);
897 } while (spec->wait_scp && (--retry != 0));
898 waiting_for_resp = false;
899 if (retry != 0) {
900 ret_msg = (struct scp_msg *)return_buf;
901 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
902 memcpy(&ret_msg->data, spec->scp_resp_data,
903 spec->wait_num_data);
904 *bytes_returned = (spec->scp_resp_count + 1) * 4;
905 status = 0;
906 } else {
907 status = -EIO;
908 }
909 spec->wait_scp = 0;
910 }
911
912 return status;
913}
914
915static int dspio_scp(struct hda_codec *codec,
916 int mod_id, int req, int dir, void *data, unsigned int len,
917 void *reply, unsigned int *reply_len)
918{
919 int status = 0;
920 struct scp_msg scp_send, scp_reply;
921 unsigned int ret_bytes, send_size, ret_size;
922 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
923 unsigned int reply_data_size;
924
925 memset(&scp_send, 0, sizeof(scp_send));
926 memset(&scp_reply, 0, sizeof(scp_reply));
927
928 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
929 return -EINVAL;
930
931 if (dir == SCP_GET && reply == NULL) {
932 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
933 return -EINVAL;
934 }
935
936 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
937 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
938 return -EINVAL;
939 }
940
941 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
942 0, 0, 0, len/sizeof(unsigned int));
943 if (data != NULL && len > 0) {
944 len = min((unsigned int)(sizeof(scp_send.data)), len);
945 memcpy(scp_send.data, data, len);
946 }
947
948 ret_bytes = 0;
949 send_size = sizeof(unsigned int) + len;
950 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
951 send_size, (unsigned char *)&scp_reply,
952 sizeof(scp_reply), &ret_bytes);
953
954 if (status < 0) {
955 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
956 return status;
957 }
958
959 /* extract send and reply headers members */
960 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
961 NULL, NULL, NULL, NULL, NULL);
962 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
963 &reply_resp_flag, &reply_error_flag,
964 &reply_data_size);
965
966 if (!send_get_flag)
967 return 0;
968
969 if (reply_resp_flag && !reply_error_flag) {
970 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
971 / sizeof(unsigned int);
972
973 if (*reply_len < ret_size*sizeof(unsigned int)) {
974 snd_printdd(KERN_ERR "reply too long for buf");
975 return -EINVAL;
976 } else if (ret_size != reply_data_size) {
977 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
978 return -EINVAL;
979 } else {
980 *reply_len = ret_size*sizeof(unsigned int);
981 memcpy(reply, scp_reply.data, *reply_len);
982 }
983 } else {
984 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
985 return -EIO;
986 }
987
988 return status;
989}
990
991static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
992{
993 int status = 0;
994 unsigned int size = sizeof(dma_chan);
995
996 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
997 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
998 SCP_GET, NULL, 0, dma_chan, &size);
999
1000 if (status < 0) {
1001 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1002 return status;
1003 }
1004
1005 if ((*dma_chan + 1) == 0) {
1006 snd_printdd(KERN_INFO "no free dma channels to allocate");
1007 return -EBUSY;
1008 }
1009
1010 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1011 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1012
1013 return status;
1014}
1015
1016static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1017{
1018 int status = 0;
1019 unsigned int dummy = 0;
1020
1021 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1022 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1023
1024 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1025 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1026
1027 if (status < 0) {
1028 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1029 return status;
1030 }
1031
1032 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1033
1034 return status;
1035}
1036
1037/*
1038 * CA0132 DSP access stuffs
1039 */
1040static int dsp_set_run_state(struct hda_codec *codec)
1041{
1042 unsigned int dbg_ctrl_reg;
1043 unsigned int halt_state;
1044 int err;
1045
1046 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1047 if (err < 0)
1048 return err;
1049
1050 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1051 DSP_DBGCNTL_STATE_LOBIT;
1052
1053 if (halt_state != 0) {
1054 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1055 DSP_DBGCNTL_SS_MASK);
1056 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1057 dbg_ctrl_reg);
1058 if (err < 0)
1059 return err;
1060
1061 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1062 DSP_DBGCNTL_EXEC_MASK;
1063 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1064 dbg_ctrl_reg);
1065 if (err < 0)
1066 return err;
1067 }
1068
1069 return 0;
1070}
1071
1072static int dsp_reset(struct hda_codec *codec)
1073{
1074 unsigned int res;
1075 int retry = 20;
1076
1077 snd_printdd("dsp_reset\n");
1078 do {
1079 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1080 retry--;
1081 } while (res == -EIO && retry);
1082
1083 if (!retry) {
1084 snd_printdd("dsp_reset timeout\n");
1085 return -EIO;
1086 }
1087
1088 return 0;
1089}
1090
1091static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1092 bool *code, bool *yram)
1093{
1094 *code = *yram = false;
1095
1096 if (UC_RANGE(chip_addx, 1)) {
1097 *code = true;
1098 return UC_OFF(chip_addx);
1099 } else if (X_RANGE_ALL(chip_addx, 1)) {
1100 return X_OFF(chip_addx);
1101 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1102 *yram = true;
1103 return Y_OFF(chip_addx);
1104 }
1105
1106 return (unsigned int)INVALID_CHIP_ADDRESS;
1107}
1108
1109static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1110{
1111 unsigned int dma_chnlstart_reg;
1112
1113 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1114
1115 return ((dma_chnlstart_reg & (1 <<
1116 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1117}
1118
1119static int dsp_dma_setup_common(struct hda_codec *codec,
1120 unsigned int chip_addx,
1121 unsigned int dma_chan,
1122 unsigned int port_map_mask,
1123 bool ovly)
1124{
1125 int status = 0;
1126 unsigned int chnl_prop;
1127 unsigned int dsp_addx;
1128 unsigned int active;
1129 bool code, yram;
1130
1131 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1132
1133 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1134 snd_printdd(KERN_ERR "dma chan num invalid");
1135 return -EINVAL;
1136 }
1137
1138 if (dsp_is_dma_active(codec, dma_chan)) {
1139 snd_printdd(KERN_ERR "dma already active");
1140 return -EBUSY;
1141 }
1142
1143 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1144
1145 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1146 snd_printdd(KERN_ERR "invalid chip addr");
1147 return -ENXIO;
1148 }
1149
1150 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1151 active = 0;
1152
1153 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1154
1155 if (ovly) {
1156 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1157 &chnl_prop);
1158
1159 if (status < 0) {
1160 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1161 return status;
1162 }
1163 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1164 }
1165
1166 if (!code)
1167 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1168 else
1169 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1170
1171 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1172
1173 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1174 if (status < 0) {
1175 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1176 return status;
1177 }
1178 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1179
1180 if (ovly) {
1181 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1182 &active);
1183
1184 if (status < 0) {
1185 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1186 return status;
1187 }
1188 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1189 }
1190
1191 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1192 DSPDMAC_ACTIVE_AAR_MASK;
1193
1194 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1195 if (status < 0) {
1196 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1197 return status;
1198 }
1199
1200 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1201
1202 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1203 port_map_mask);
1204 if (status < 0) {
1205 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1206 return status;
1207 }
1208 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1209
1210 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1211 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1212 if (status < 0) {
1213 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1214 return status;
1215 }
1216 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1217
1218 snd_printdd(
1219 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1220 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1221 chip_addx, dsp_addx, dma_chan,
1222 port_map_mask, chnl_prop, active);
1223
1224 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1225
1226 return 0;
1227}
1228
1229static int dsp_dma_setup(struct hda_codec *codec,
1230 unsigned int chip_addx,
1231 unsigned int count,
1232 unsigned int dma_chan)
1233{
1234 int status = 0;
1235 bool code, yram;
1236 unsigned int dsp_addx;
1237 unsigned int addr_field;
1238 unsigned int incr_field;
1239 unsigned int base_cnt;
1240 unsigned int cur_cnt;
1241 unsigned int dma_cfg = 0;
1242 unsigned int adr_ofs = 0;
1243 unsigned int xfr_cnt = 0;
1244 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1245 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1246
1247 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1248
1249 if (count > max_dma_count) {
1250 snd_printdd(KERN_ERR "count too big");
1251 return -EINVAL;
1252 }
1253
1254 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1255 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1256 snd_printdd(KERN_ERR "invalid chip addr");
1257 return -ENXIO;
1258 }
1259
1260 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1261
1262 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1263 incr_field = 0;
1264
1265 if (!code) {
1266 addr_field <<= 1;
1267 if (yram)
1268 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1269
1270 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1271 }
1272
1273 dma_cfg = addr_field + incr_field;
1274 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1275 dma_cfg);
1276 if (status < 0) {
1277 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1278 return status;
1279 }
1280 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1281
1282 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1283 (code ? 0 : 1));
1284
1285 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1286 adr_ofs);
1287 if (status < 0) {
1288 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1289 return status;
1290 }
1291 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1292
1293 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1294
1295 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1296
1297 xfr_cnt = base_cnt | cur_cnt;
1298
1299 status = chipio_write(codec,
1300 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1301 if (status < 0) {
1302 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1303 return status;
1304 }
1305 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1306
1307 snd_printdd(
1308 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1309 "ADROFS=0x%x, XFRCNT=0x%x\n",
1310 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1311
1312 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1313
1314 return 0;
1315}
1316
1317static int dsp_dma_start(struct hda_codec *codec,
1318 unsigned int dma_chan, bool ovly)
1319{
1320 unsigned int reg = 0;
1321 int status = 0;
1322
1323 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1324
1325 if (ovly) {
1326 status = chipio_read(codec,
1327 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1328
1329 if (status < 0) {
1330 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1331 return status;
1332 }
1333 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1334
1335 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1336 DSPDMAC_CHNLSTART_DIS_MASK);
1337 }
1338
1339 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1340 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1341 if (status < 0) {
1342 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1343 return status;
1344 }
1345 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1346
1347 return status;
1348}
1349
1350static int dsp_dma_stop(struct hda_codec *codec,
1351 unsigned int dma_chan, bool ovly)
1352{
1353 unsigned int reg = 0;
1354 int status = 0;
1355
1356 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1357
1358 if (ovly) {
1359 status = chipio_read(codec,
1360 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1361
1362 if (status < 0) {
1363 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1364 return status;
1365 }
1366 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1367 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1368 DSPDMAC_CHNLSTART_DIS_MASK);
1369 }
1370
1371 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1372 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1373 if (status < 0) {
1374 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1375 return status;
1376 }
1377 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1378
1379 return status;
1380}
1381
1382static int dsp_allocate_router_ports(struct hda_codec *codec,
1383 unsigned int num_chans,
1384 unsigned int ports_per_channel,
1385 unsigned int start_device,
1386 unsigned int *port_map)
1387{
1388 int status = 0;
1389 int res;
1390 u8 val;
1391
1392 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1393 if (status < 0)
1394 return status;
1395
1396 val = start_device << 6;
1397 val |= (ports_per_channel - 1) << 4;
1398 val |= num_chans - 1;
1399
1400 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1401 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1402 val);
1403
1404 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1405 VENDOR_CHIPIO_PORT_ALLOC_SET,
1406 MEM_CONNID_DSP);
1407
1408 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1409 if (status < 0)
1410 return status;
1411
1412 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1413 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1414
1415 *port_map = res;
1416
1417 return (res < 0) ? res : 0;
1418}
1419
1420static int dsp_free_router_ports(struct hda_codec *codec)
1421{
1422 int status = 0;
1423
1424 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1425 if (status < 0)
1426 return status;
1427
1428 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1429 VENDOR_CHIPIO_PORT_FREE_SET,
1430 MEM_CONNID_DSP);
1431
1432 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1433
1434 return status;
1435}
1436
1437static int dsp_allocate_ports(struct hda_codec *codec,
1438 unsigned int num_chans,
1439 unsigned int rate_multi, unsigned int *port_map)
1440{
1441 int status;
1442
1443 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1444
1445 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1446 snd_printdd(KERN_ERR "bad rate multiple");
1447 return -EINVAL;
1448 }
1449
1450 status = dsp_allocate_router_ports(codec, num_chans,
1451 rate_multi, 0, port_map);
1452
1453 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1454
1455 return status;
1456}
1457
1458static int dsp_free_ports(struct hda_codec *codec)
1459{
1460 int status;
1461
1462 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
1463
1464 status = dsp_free_router_ports(codec);
1465 if (status < 0) {
1466 snd_printdd(KERN_ERR "free router ports fail");
1467 return status;
1468 }
1469 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
1470
1471 return status;
1472}
1473
1474static int dsp_allocate_ports_format(struct hda_codec *codec,
1475 const unsigned short fmt,
1476 unsigned int *port_map)
1477{
1478 int status;
1479 unsigned int num_chans;
1480
1481 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1482 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1483 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1484
1485 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1486 snd_printdd(KERN_ERR "bad rate multiple");
1487 return -EINVAL;
1488 }
1489
1490 num_chans = get_hdafmt_chs(fmt) + 1;
1491
1492 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1493
1494 return status;
1495}
1496
1497/*
1498 * HDA DMA engine stuffs for DSP code download
1499 */
1500struct dma_engine {
1501 struct hda_codec *codec;
1502 unsigned short m_converter_format;
1503 struct snd_dma_buffer *dmab;
1504 unsigned int buf_size;
1505};
1506
1507
1508enum dma_state {
1509 DMA_STATE_STOP = 0,
1510 DMA_STATE_RUN = 1
1511};
1512
1513static int dma_convert_to_hda_format(
1514 struct hda_stream_format *stream_format,
1515 unsigned short *hda_format)
1516{
1517 unsigned int format_val;
1518
1519 format_val = snd_hda_calc_stream_format(
1520 stream_format->sample_rate,
1521 stream_format->number_channels,
1522 SNDRV_PCM_FORMAT_S32_LE,
1523 stream_format->container_size, 0);
1524
1525 if (hda_format)
1526 *hda_format = (unsigned short)format_val;
1527
1528 return 0;
1529}
1530
1531static int dma_reset(struct dma_engine *dma)
1532{
1533 struct hda_codec *codec = dma->codec;
1534 struct ca0132_spec *spec = codec->spec;
1535 int status;
1536
1537 if (dma->dmab)
1538 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
1539
1540 status = snd_hda_codec_load_dsp_prepare(codec,
1541 dma->m_converter_format,
1542 dma->buf_size,
1543 dma->dmab);
1544 if (status < 0)
1545 return status;
1546 spec->dsp_stream_id = status;
1547 return 0;
1548}
1549
1550static int dma_set_state(struct dma_engine *dma, enum dma_state state)
1551{
1552 bool cmd;
1553
1554 snd_printdd("dma_set_state state=%d\n", state);
1555
1556 switch (state) {
1557 case DMA_STATE_STOP:
1558 cmd = false;
1559 break;
1560 case DMA_STATE_RUN:
1561 cmd = true;
1562 break;
1563 default:
1564 return 0;
1565 }
1566
1567 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
1568 return 0;
1569}
1570
1571static unsigned int dma_get_buffer_size(struct dma_engine *dma)
1572{
1573 return dma->dmab->bytes;
1574}
1575
1576static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
1577{
1578 return dma->dmab->area;
1579}
1580
1581static int dma_xfer(struct dma_engine *dma,
1582 const unsigned int *data,
1583 unsigned int count)
1584{
1585 memcpy(dma->dmab->area, data, count);
1586 return 0;
1587}
1588
1589static void dma_get_converter_format(
1590 struct dma_engine *dma,
1591 unsigned short *format)
1592{
1593 if (format)
1594 *format = dma->m_converter_format;
1595}
1596
1597static unsigned int dma_get_stream_id(struct dma_engine *dma)
1598{
1599 struct ca0132_spec *spec = dma->codec->spec;
1600
1601 return spec->dsp_stream_id;
1602}
1603
1604struct dsp_image_seg {
1605 u32 magic;
1606 u32 chip_addr;
1607 u32 count;
1608 u32 data[0];
1609};
1610
1611static const u32 g_magic_value = 0x4c46584d;
1612static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
1613
1614static bool is_valid(const struct dsp_image_seg *p)
1615{
1616 return p->magic == g_magic_value;
1617}
1618
1619static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
1620{
1621 return g_chip_addr_magic_value == p->chip_addr;
1622}
1623
1624static bool is_last(const struct dsp_image_seg *p)
1625{
1626 return p->count == 0;
1627}
1628
1629static size_t dsp_sizeof(const struct dsp_image_seg *p)
1630{
1631 return sizeof(*p) + p->count*sizeof(u32);
1632}
1633
1634static const struct dsp_image_seg *get_next_seg_ptr(
1635 const struct dsp_image_seg *p)
1636{
1637 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
1638}
1639
1640/*
1641 * CA0132 chip DSP transfer stuffs. For DSP download.
1642 */
1643#define INVALID_DMA_CHANNEL (~0UL)
1644
1645static int dspxfr_hci_write(struct hda_codec *codec,
1646 const struct dsp_image_seg *fls)
1647{
1648 int status;
1649 const u32 *data;
1650 unsigned int count;
1651
1652 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
1653 snd_printdd(KERN_ERR "hci_write invalid params");
1654 return -EINVAL;
1655 }
1656
1657 count = fls->count;
1658 data = (u32 *)(fls->data);
1659 while (count >= 2) {
1660 status = chipio_write(codec, data[0], data[1]);
1661 if (status < 0) {
1662 snd_printdd(KERN_ERR "hci_write chipio failed");
1663 return status;
1664 }
1665 count -= 2;
1666 data += 2;
1667 }
1668 return 0;
1669}
1670
1671static int dspxfr_one_seg(struct hda_codec *codec,
1672 const struct dsp_image_seg *fls,
1673 unsigned int reloc,
1674 struct dma_engine *dma_engine,
1675 unsigned int dma_chan,
1676 unsigned int port_map_mask,
1677 bool ovly)
1678{
1679 int status;
1680 bool comm_dma_setup_done = false;
1681 const unsigned int *data;
1682 unsigned int chip_addx;
1683 unsigned int words_to_write;
1684 unsigned int buffer_size_words;
1685 unsigned char *buffer_addx;
1686 unsigned short hda_format;
1687 unsigned int sample_rate_div;
1688 unsigned int sample_rate_mul;
1689 unsigned int num_chans;
1690 unsigned int hda_frame_size_words;
1691 unsigned int remainder_words;
1692 const u32 *data_remainder;
1693 u32 chip_addx_remainder;
1694 unsigned int run_size_words;
1695 const struct dsp_image_seg *hci_write = NULL;
1696 int retry;
1697
1698 if (fls == NULL)
1699 return -EINVAL;
1700 if (is_hci_prog_list_seg(fls)) {
1701 hci_write = fls;
1702 fls = get_next_seg_ptr(fls);
1703 }
1704
1705 if (hci_write && (!fls || is_last(fls))) {
1706 snd_printdd("hci_write\n");
1707 return dspxfr_hci_write(codec, hci_write);
1708 }
1709
1710 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
1711 snd_printdd("Invalid Params\n");
1712 return -EINVAL;
1713 }
1714
1715 data = fls->data;
1716 chip_addx = fls->chip_addr,
1717 words_to_write = fls->count;
1718
1719 if (!words_to_write)
1720 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
1721 if (reloc)
1722 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
1723
1724 if (!UC_RANGE(chip_addx, words_to_write) &&
1725 !X_RANGE_ALL(chip_addx, words_to_write) &&
1726 !Y_RANGE_ALL(chip_addx, words_to_write)) {
1727 snd_printdd("Invalid chip_addx Params\n");
1728 return -EINVAL;
1729 }
1730
1731 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
1732 sizeof(u32);
1733
1734 buffer_addx = dma_get_buffer_addr(dma_engine);
1735
1736 if (buffer_addx == NULL) {
1737 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
1738 return -EINVAL;
1739 }
1740
1741 dma_get_converter_format(dma_engine, &hda_format);
1742 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
1743 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
1744 num_chans = get_hdafmt_chs(hda_format) + 1;
1745
1746 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
1747 (num_chans * sample_rate_mul / sample_rate_div));
1748
1749 buffer_size_words = min(buffer_size_words,
1750 (unsigned int)(UC_RANGE(chip_addx, 1) ?
1751 65536 : 32768));
1752 buffer_size_words -= buffer_size_words % hda_frame_size_words;
1753 snd_printdd(
1754 "chpadr=0x%08x frmsz=%u nchan=%u "
1755 "rate_mul=%u div=%u bufsz=%u\n",
1756 chip_addx, hda_frame_size_words, num_chans,
1757 sample_rate_mul, sample_rate_div, buffer_size_words);
1758
1759 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
1760 (buffer_size_words < hda_frame_size_words)) {
1761 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
1762 return -EINVAL;
1763 }
1764
1765 remainder_words = words_to_write % hda_frame_size_words;
1766 data_remainder = data;
1767 chip_addx_remainder = chip_addx;
1768
1769 data += remainder_words;
1770 chip_addx += remainder_words*sizeof(u32);
1771 words_to_write -= remainder_words;
1772
1773 while (words_to_write != 0) {
1774 run_size_words = min(buffer_size_words, words_to_write);
1775 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
1776 words_to_write, run_size_words, remainder_words);
1777 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
1778 if (!comm_dma_setup_done) {
1779 status = dsp_dma_stop(codec, dma_chan, ovly);
1780 if (status < 0)
1781 return -EIO;
1782 status = dsp_dma_setup_common(codec, chip_addx,
1783 dma_chan, port_map_mask, ovly);
1784 if (status < 0)
1785 return status;
1786 comm_dma_setup_done = true;
1787 }
1788
1789 status = dsp_dma_setup(codec, chip_addx,
1790 run_size_words, dma_chan);
1791 if (status < 0)
1792 return status;
1793 status = dsp_dma_start(codec, dma_chan, ovly);
1794 if (status < 0)
1795 return status;
1796 if (!dsp_is_dma_active(codec, dma_chan)) {
1797 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
1798 return -EIO;
1799 }
1800 status = dma_set_state(dma_engine, DMA_STATE_RUN);
1801 if (status < 0)
1802 return status;
1803 if (remainder_words != 0) {
1804 status = chipio_write_multiple(codec,
1805 chip_addx_remainder,
1806 data_remainder,
1807 remainder_words);
1808 remainder_words = 0;
1809 }
1810 if (hci_write) {
1811 status = dspxfr_hci_write(codec, hci_write);
1812 hci_write = NULL;
1813 }
1814 retry = 5000;
1815 while (dsp_is_dma_active(codec, dma_chan)) {
1816 if (--retry <= 0)
1817 break;
1818 }
1819 snd_printdd(KERN_INFO "+++++ DMA complete");
1820 dma_set_state(dma_engine, DMA_STATE_STOP);
1821 dma_reset(dma_engine);
1822
1823 if (status < 0)
1824 return status;
1825
1826 data += run_size_words;
1827 chip_addx += run_size_words*sizeof(u32);
1828 words_to_write -= run_size_words;
1829 }
1830
1831 if (remainder_words != 0) {
1832 status = chipio_write_multiple(codec, chip_addx_remainder,
1833 data_remainder, remainder_words);
1834 }
1835
1836 return status;
1837}
1838
1839static int dspxfr_image(struct hda_codec *codec,
1840 const struct dsp_image_seg *fls_data,
1841 unsigned int reloc, struct hda_stream_format *format,
1842 bool ovly)
1843{
1844 struct ca0132_spec *spec = codec->spec;
1845 int status;
1846 unsigned short hda_format = 0;
1847 unsigned int response;
1848 unsigned char stream_id = 0;
1849 struct dma_engine *dma_engine;
1850 unsigned int dma_chan;
1851 unsigned int port_map_mask;
1852
1853 if (fls_data == NULL)
1854 return -EINVAL;
1855
1856 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
1857 if (!dma_engine) {
1858 status = -ENOMEM;
1859 goto exit;
1860 }
1861 memset((void *)dma_engine, 0, sizeof(*dma_engine));
1862
1863 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
1864 if (!dma_engine->dmab) {
1865 status = -ENOMEM;
1866 goto exit;
1867 }
1868
1869 dma_engine->codec = codec;
1870 dma_convert_to_hda_format(format, &hda_format);
1871 dma_engine->m_converter_format = hda_format;
1872 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
1873 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
1874
1875 dma_chan = 0;
1876
1877 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
1878 hda_format, &response);
1879
1880 if (status < 0) {
1881 snd_printdd(KERN_ERR "set converter format fail");
1882 goto exit;
1883 }
1884
1885 status = snd_hda_codec_load_dsp_prepare(codec,
1886 dma_engine->m_converter_format,
1887 dma_engine->buf_size,
1888 dma_engine->dmab);
1889 if (status < 0)
1890 goto exit;
1891 spec->dsp_stream_id = status;
1892
1893 if (ovly) {
1894 status = dspio_alloc_dma_chan(codec, &dma_chan);
1895 if (status < 0) {
1896 snd_printdd(KERN_ERR "alloc dmachan fail");
1897 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
1898 goto exit;
1899 }
1900 }
1901
1902 port_map_mask = 0;
1903 status = dsp_allocate_ports_format(codec, hda_format,
1904 &port_map_mask);
1905 if (status < 0) {
1906 snd_printdd(KERN_ERR "alloc ports fail");
1907 goto exit;
1908 }
1909
1910 stream_id = dma_get_stream_id(dma_engine);
1911 status = codec_set_converter_stream_channel(codec,
1912 WIDGET_CHIP_CTRL, stream_id, 0, &response);
1913 if (status < 0) {
1914 snd_printdd(KERN_ERR "set stream chan fail");
1915 goto exit;
1916 }
1917
1918 while ((fls_data != NULL) && !is_last(fls_data)) {
1919 if (!is_valid(fls_data)) {
1920 snd_printdd(KERN_ERR "FLS check fail");
1921 status = -EINVAL;
1922 goto exit;
1923 }
1924 status = dspxfr_one_seg(codec, fls_data, reloc,
1925 dma_engine, dma_chan,
1926 port_map_mask, ovly);
1927 if (status < 0)
1928 break;
1929
1930 if (is_hci_prog_list_seg(fls_data))
1931 fls_data = get_next_seg_ptr(fls_data);
1932
1933 if ((fls_data != NULL) && !is_last(fls_data))
1934 fls_data = get_next_seg_ptr(fls_data);
1935 }
1936
1937 if (port_map_mask != 0)
1938 status = dsp_free_ports(codec);
1939
1940 if (status < 0)
1941 goto exit;
1942
1943 status = codec_set_converter_stream_channel(codec,
1944 WIDGET_CHIP_CTRL, 0, 0, &response);
1945
1946exit:
1947 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
1948 dspio_free_dma_chan(codec, dma_chan);
1949
1950 if (dma_engine->dmab)
1951 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
1952 kfree(dma_engine->dmab);
1953 kfree(dma_engine);
1954
1955 return status;
1956}
1957
1958/*
1959 * CA0132 DSP download stuffs.
1960 */
1961static void dspload_post_setup(struct hda_codec *codec)
1962{
1963 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
1964
1965 /*set DSP speaker to 2.0 configuration*/
1966 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
1967 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
1968
1969 /*update write pointer*/
1970 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
1971}
1972
1973static int dspload_image(struct hda_codec *codec,
1974 const struct dsp_image_seg *fls,
1975 bool ovly,
1976 unsigned int reloc,
1977 bool autostart,
1978 int router_chans)
1979{
1980 int status = 0;
1981 struct hda_stream_format stream_format;
1982
1983 snd_printdd(KERN_INFO "---- dspload_image begin ------");
1984 if (router_chans == 0) {
1985 if (!ovly)
1986 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
1987 else
1988 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
1989 }
1990
1991 stream_format.sample_rate = 48000;
1992 stream_format.number_channels = (unsigned short)router_chans;
1993
1994 while (stream_format.number_channels > 16) {
1995 stream_format.sample_rate *= 2;
1996 stream_format.number_channels /= 2;
1997 }
1998
1999 stream_format.container_size = 32;
2000 stream_format.valid_bits_per_sample = 32;
2001
2002 do {
2003 snd_printdd(KERN_INFO "Ready to program DMA");
2004 if (!ovly)
2005 status = dsp_reset(codec);
2006
2007 if (status < 0)
2008 break;
2009
2010 snd_printdd(KERN_INFO "dsp_reset() complete");
2011 status = dspxfr_image(codec, fls, reloc, &stream_format, ovly);
2012
2013 if (status < 0)
2014 break;
2015
2016 snd_printdd(KERN_INFO "dspxfr_image() complete");
2017 if (autostart && !ovly) {
2018 dspload_post_setup(codec);
2019 status = dsp_set_run_state(codec);
2020 }
2021
2022 snd_printdd(KERN_INFO "LOAD FINISHED");
2023 } while (0);
2024
2025 return status;
2026}
2027
Ian Minettc3b4eea22012-09-20 20:29:17 -07002028static const struct firmware *fw_efx;
2029
2030static int request_firmware_cached(const struct firmware **firmware_p,
2031 const char *name, struct device *device)
2032{
2033 if (*firmware_p)
2034 return 0; /* already loaded */
2035 return request_firmware(firmware_p, name, device);
2036}
2037
2038static void release_cached_firmware(void)
2039{
2040 if (fw_efx) {
2041 release_firmware(fw_efx);
2042 fw_efx = NULL;
2043 }
2044}
2045
Ian Minett01ef7db2012-09-20 20:29:16 -07002046static bool dspload_is_loaded(struct hda_codec *codec)
2047{
2048 unsigned int data = 0;
2049 int status = 0;
2050
2051 status = chipio_read(codec, 0x40004, &data);
2052 if ((status < 0) || (data != 1))
2053 return false;
2054
2055 return true;
2056}
2057
2058static bool dspload_wait_loaded(struct hda_codec *codec)
2059{
2060 int retry = 100;
2061
2062 do {
2063 msleep(20);
2064 if (dspload_is_loaded(codec)) {
2065 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2066 return true;
2067 }
2068 } while (--retry);
2069
2070 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2071 return false;
2072}
2073
Ian Minett95c6e9c2011-06-15 15:35:17 -07002074/*
Ian Minett95c6e9c2011-06-15 15:35:17 -07002075 * PCM callbacks
2076 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002077static int ca0132_playback_pcm_open(struct hda_pcm_stream *hinfo,
2078 struct hda_codec *codec,
2079 struct snd_pcm_substream *substream)
2080{
2081 struct ca0132_spec *spec = codec->spec;
2082 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2083 hinfo);
2084}
2085
Ian Minett95c6e9c2011-06-15 15:35:17 -07002086static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2087 struct hda_codec *codec,
2088 unsigned int stream_tag,
2089 unsigned int format,
2090 struct snd_pcm_substream *substream)
2091{
2092 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002093 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2094 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002095}
2096
2097static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2098 struct hda_codec *codec,
2099 struct snd_pcm_substream *substream)
2100{
2101 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002102 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002103}
2104
2105/*
2106 * Digital out
2107 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002108static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2109 struct hda_codec *codec,
2110 struct snd_pcm_substream *substream)
2111{
2112 struct ca0132_spec *spec = codec->spec;
2113 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2114}
2115
Ian Minett95c6e9c2011-06-15 15:35:17 -07002116static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2117 struct hda_codec *codec,
2118 unsigned int stream_tag,
2119 unsigned int format,
2120 struct snd_pcm_substream *substream)
2121{
2122 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002123 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2124 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002125}
2126
2127static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2128 struct hda_codec *codec,
2129 struct snd_pcm_substream *substream)
2130{
2131 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002132 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002133}
2134
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002135static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2136 struct hda_codec *codec,
2137 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002138{
2139 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002140 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002141}
2142
2143/*
2144 */
2145static struct hda_pcm_stream ca0132_pcm_analog_playback = {
2146 .substreams = 1,
2147 .channels_min = 2,
2148 .channels_max = 2,
2149 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002150 .open = ca0132_playback_pcm_open,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002151 .prepare = ca0132_playback_pcm_prepare,
2152 .cleanup = ca0132_playback_pcm_cleanup
2153 },
2154};
2155
2156static struct hda_pcm_stream ca0132_pcm_analog_capture = {
2157 .substreams = 1,
2158 .channels_min = 2,
2159 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002160};
2161
2162static struct hda_pcm_stream ca0132_pcm_digital_playback = {
2163 .substreams = 1,
2164 .channels_min = 2,
2165 .channels_max = 2,
2166 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002167 .open = ca0132_dig_playback_pcm_open,
2168 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002169 .prepare = ca0132_dig_playback_pcm_prepare,
2170 .cleanup = ca0132_dig_playback_pcm_cleanup
2171 },
2172};
2173
2174static struct hda_pcm_stream ca0132_pcm_digital_capture = {
2175 .substreams = 1,
2176 .channels_min = 2,
2177 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002178};
2179
2180static int ca0132_build_pcms(struct hda_codec *codec)
2181{
2182 struct ca0132_spec *spec = codec->spec;
2183 struct hda_pcm *info = spec->pcm_rec;
2184
2185 codec->pcm_info = info;
2186 codec->num_pcms = 0;
2187
2188 info->name = "CA0132 Analog";
2189 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
2190 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
2191 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
2192 spec->multiout.max_channels;
2193 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
2194 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
2195 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
2196 codec->num_pcms++;
2197
2198 if (!spec->dig_out && !spec->dig_in)
2199 return 0;
2200
2201 info++;
2202 info->name = "CA0132 Digital";
2203 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2204 if (spec->dig_out) {
2205 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2206 ca0132_pcm_digital_playback;
2207 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
2208 }
2209 if (spec->dig_in) {
2210 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2211 ca0132_pcm_digital_capture;
2212 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
2213 }
2214 codec->num_pcms++;
2215
2216 return 0;
2217}
2218
2219#define REG_CODEC_MUTE 0x18b014
2220#define REG_CODEC_HP_VOL_L 0x18b070
2221#define REG_CODEC_HP_VOL_R 0x18b074
2222
2223static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
2224 struct snd_ctl_elem_value *ucontrol)
2225{
2226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2227 struct ca0132_spec *spec = codec->spec;
2228 long *valp = ucontrol->value.integer.value;
2229
2230 *valp = spec->curr_hp_switch;
2231 return 0;
2232}
2233
2234static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol)
2236{
2237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2238 struct ca0132_spec *spec = codec->spec;
2239 long *valp = ucontrol->value.integer.value;
2240 unsigned int data;
2241 int err;
2242
2243 /* any change? */
2244 if (spec->curr_hp_switch == *valp)
2245 return 0;
2246
2247 snd_hda_power_up(codec);
2248
2249 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2250 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002251 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002252
2253 /* *valp 0 is mute, 1 is unmute */
2254 data = (data & 0x7f) | (*valp ? 0 : 0x80);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002255 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002256 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002257 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002258
2259 spec->curr_hp_switch = *valp;
2260
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002261 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002262 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002263 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002264}
2265
2266static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
2268{
2269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2270 struct ca0132_spec *spec = codec->spec;
2271 long *valp = ucontrol->value.integer.value;
2272
2273 *valp = spec->curr_speaker_switch;
2274 return 0;
2275}
2276
2277static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2281 struct ca0132_spec *spec = codec->spec;
2282 long *valp = ucontrol->value.integer.value;
2283 unsigned int data;
2284 int err;
2285
2286 /* any change? */
2287 if (spec->curr_speaker_switch == *valp)
2288 return 0;
2289
2290 snd_hda_power_up(codec);
2291
2292 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2293 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002294 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002295
2296 /* *valp 0 is mute, 1 is unmute */
2297 data = (data & 0xef) | (*valp ? 0 : 0x10);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002298 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002299 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002300 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002301
2302 spec->curr_speaker_switch = *valp;
2303
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002304 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002305 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002306 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002307}
2308
2309static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
2310 struct snd_ctl_elem_value *ucontrol)
2311{
2312 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2313 struct ca0132_spec *spec = codec->spec;
2314 long *valp = ucontrol->value.integer.value;
2315
2316 *valp++ = spec->curr_hp_volume[0];
2317 *valp = spec->curr_hp_volume[1];
2318 return 0;
2319}
2320
2321static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
2322 struct snd_ctl_elem_value *ucontrol)
2323{
2324 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2325 struct ca0132_spec *spec = codec->spec;
2326 long *valp = ucontrol->value.integer.value;
2327 long left_vol, right_vol;
2328 unsigned int data;
2329 int val;
2330 int err;
2331
2332 left_vol = *valp++;
2333 right_vol = *valp;
2334
2335 /* any change? */
2336 if ((spec->curr_hp_volume[0] == left_vol) &&
2337 (spec->curr_hp_volume[1] == right_vol))
2338 return 0;
2339
2340 snd_hda_power_up(codec);
2341
2342 err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
2343 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002344 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002345
2346 val = 31 - left_vol;
2347 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002348 err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002349 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002350 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002351
2352 val = 31 - right_vol;
2353 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002354 err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002355 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002356 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002357
2358 spec->curr_hp_volume[0] = left_vol;
2359 spec->curr_hp_volume[1] = right_vol;
2360
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002361 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002362 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002363 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002364}
2365
2366static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
2367{
2368 struct snd_kcontrol_new knew =
2369 HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
2370 nid, 1, 0, HDA_OUTPUT);
2371 knew.get = ca0132_hp_switch_get;
2372 knew.put = ca0132_hp_switch_put;
2373 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2374}
2375
2376static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
2377{
2378 struct snd_kcontrol_new knew =
2379 HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
2380 nid, 3, 0, HDA_OUTPUT);
2381 knew.get = ca0132_hp_volume_get;
2382 knew.put = ca0132_hp_volume_put;
2383 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2384}
2385
2386static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
2387{
2388 struct snd_kcontrol_new knew =
2389 HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
2390 nid, 1, 0, HDA_OUTPUT);
2391 knew.get = ca0132_speaker_switch_get;
2392 knew.put = ca0132_speaker_switch_put;
2393 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2394}
2395
2396static void ca0132_fix_hp_caps(struct hda_codec *codec)
2397{
2398 struct ca0132_spec *spec = codec->spec;
2399 struct auto_pin_cfg *cfg = &spec->autocfg;
2400 unsigned int caps;
2401
2402 /* set mute-capable, 1db step, 32 steps, ofs 6 */
2403 caps = 0x80031f06;
2404 snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
2405}
2406
2407static int ca0132_build_controls(struct hda_codec *codec)
2408{
2409 struct ca0132_spec *spec = codec->spec;
2410 struct auto_pin_cfg *cfg = &spec->autocfg;
2411 int i, err;
2412
2413 if (spec->multiout.num_dacs) {
2414 err = add_speaker_switch(codec, spec->out_pins[0]);
2415 if (err < 0)
2416 return err;
2417 }
2418
2419 if (cfg->hp_outs) {
2420 ca0132_fix_hp_caps(codec);
2421 err = add_hp_switch(codec, cfg->hp_pins[0]);
2422 if (err < 0)
2423 return err;
2424 err = add_hp_volume(codec, cfg->hp_pins[0]);
2425 if (err < 0)
2426 return err;
2427 }
2428
2429 for (i = 0; i < spec->num_inputs; i++) {
2430 const char *label = spec->input_labels[i];
2431
2432 err = add_in_switch(codec, spec->adcs[i], label);
2433 if (err < 0)
2434 return err;
2435 err = add_in_volume(codec, spec->adcs[i], label);
2436 if (err < 0)
2437 return err;
2438 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
2439 /* add Mic-Boost */
2440 err = add_in_mono_volume(codec, spec->input_pins[i],
2441 "Mic Boost", 1);
2442 if (err < 0)
2443 return err;
2444 }
2445 }
2446
2447 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +02002448 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
2449 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002450 if (err < 0)
2451 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002452 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002453 if (err < 0)
2454 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002455 /* spec->multiout.share_spdif = 1; */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002456 }
2457
2458 if (spec->dig_in) {
2459 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
2460 if (err < 0)
2461 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002462 }
2463 return 0;
2464}
2465
2466
2467static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
2468{
2469 /* Set Creative extension */
2470 snd_printdd("SET CREATIVE EXTENSION\n");
2471 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2472 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
2473 enable);
2474 msleep(20);
2475}
2476
2477
2478static void ca0132_config(struct hda_codec *codec)
2479{
2480 struct ca0132_spec *spec = codec->spec;
2481 struct auto_pin_cfg *cfg = &spec->autocfg;
2482
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002483 codec->pcm_format_first = 1;
2484 codec->no_sticky_stream = 1;
2485
Ian Minett95c6e9c2011-06-15 15:35:17 -07002486 /* line-outs */
2487 cfg->line_outs = 1;
2488 cfg->line_out_pins[0] = 0x0b; /* front */
2489 cfg->line_out_type = AUTO_PIN_LINE_OUT;
2490
2491 spec->dacs[0] = 0x02;
2492 spec->out_pins[0] = 0x0b;
2493 spec->multiout.dac_nids = spec->dacs;
2494 spec->multiout.num_dacs = 1;
2495 spec->multiout.max_channels = 2;
2496
2497 /* headphone */
2498 cfg->hp_outs = 1;
2499 cfg->hp_pins[0] = 0x0f;
2500
2501 spec->hp_dac = 0;
2502 spec->multiout.hp_nid = 0;
2503
2504 /* inputs */
2505 cfg->num_inputs = 2; /* Mic-in and line-in */
2506 cfg->inputs[0].pin = 0x12;
2507 cfg->inputs[0].type = AUTO_PIN_MIC;
2508 cfg->inputs[1].pin = 0x11;
2509 cfg->inputs[1].type = AUTO_PIN_LINE_IN;
2510
2511 /* Mic-in */
2512 spec->input_pins[0] = 0x12;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002513 spec->input_labels[0] = "Mic";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002514 spec->adcs[0] = 0x07;
2515
2516 /* Line-In */
2517 spec->input_pins[1] = 0x11;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002518 spec->input_labels[1] = "Line";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002519 spec->adcs[1] = 0x08;
2520 spec->num_inputs = 2;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002521
2522 /* SPDIF I/O */
2523 spec->dig_out = 0x05;
2524 spec->multiout.dig_out_nid = spec->dig_out;
2525 cfg->dig_out_pins[0] = 0x0c;
2526 cfg->dig_outs = 1;
2527 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
2528 spec->dig_in = 0x09;
2529 cfg->dig_in_pin = 0x0e;
2530 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002531}
2532
2533static void ca0132_init_chip(struct hda_codec *codec)
2534{
2535 struct ca0132_spec *spec = codec->spec;
2536
2537 mutex_init(&spec->chipio_mutex);
2538}
2539
2540static void ca0132_exit_chip(struct hda_codec *codec)
2541{
2542 /* put any chip cleanup stuffs here. */
2543}
2544
Ian Minett01ef7db2012-09-20 20:29:16 -07002545static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
2546{
2547 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
2548 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
2549 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
2550 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
2551 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
2552 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
2553
2554 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
2555 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
2556 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
2557}
2558
2559static bool ca0132_download_dsp_images(struct hda_codec *codec)
2560{
2561 bool dsp_loaded = false;
2562 const struct dsp_image_seg *dsp_os_image;
Ian Minett01ef7db2012-09-20 20:29:16 -07002563
Ian Minettc3b4eea22012-09-20 20:29:17 -07002564 if (request_firmware_cached(&fw_efx, EFX_FILE,
2565 codec->bus->card->dev) != 0)
Ian Minett01ef7db2012-09-20 20:29:16 -07002566 return false;
2567
Ian Minettc3b4eea22012-09-20 20:29:17 -07002568 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
Ian Minett01ef7db2012-09-20 20:29:16 -07002569 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
2570 dsp_loaded = dspload_wait_loaded(codec);
2571
Ian Minett01ef7db2012-09-20 20:29:16 -07002572 return dsp_loaded;
2573}
2574
2575static void ca0132_download_dsp(struct hda_codec *codec)
2576{
2577 struct ca0132_spec *spec = codec->spec;
2578
2579 spec->dsp_state = DSP_DOWNLOAD_INIT;
2580
2581 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
2582 chipio_enable_clocks(codec);
2583 spec->dsp_state = DSP_DOWNLOADING;
2584 if (!ca0132_download_dsp_images(codec))
2585 spec->dsp_state = DSP_DOWNLOAD_FAILED;
2586 else
2587 spec->dsp_state = DSP_DOWNLOADED;
2588 }
2589
2590 if (spec->dsp_state == DSP_DOWNLOADED)
2591 ca0132_set_dsp_msr(codec, true);
2592}
2593
Ian Minett95c6e9c2011-06-15 15:35:17 -07002594static int ca0132_init(struct hda_codec *codec)
2595{
2596 struct ca0132_spec *spec = codec->spec;
2597 struct auto_pin_cfg *cfg = &spec->autocfg;
2598 int i;
2599
Ian Minett01ef7db2012-09-20 20:29:16 -07002600#ifdef CONFIG_SND_HDA_DSP_LOADER
2601 ca0132_download_dsp(codec);
2602#endif
2603
Ian Minett95c6e9c2011-06-15 15:35:17 -07002604 for (i = 0; i < spec->multiout.num_dacs; i++) {
2605 init_output(codec, spec->out_pins[i],
2606 spec->multiout.dac_nids[i]);
2607 }
2608 init_output(codec, cfg->hp_pins[0], spec->hp_dac);
2609 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
2610
2611 for (i = 0; i < spec->num_inputs; i++)
2612 init_input(codec, spec->input_pins[i], spec->adcs[i]);
2613
2614 init_input(codec, cfg->dig_in_pin, spec->dig_in);
2615
2616 ca0132_set_ct_ext(codec, 1);
2617
2618 return 0;
2619}
2620
2621
2622static void ca0132_free(struct hda_codec *codec)
2623{
2624 ca0132_set_ct_ext(codec, 0);
2625 ca0132_exit_chip(codec);
2626 kfree(codec->spec);
2627}
2628
2629static struct hda_codec_ops ca0132_patch_ops = {
2630 .build_controls = ca0132_build_controls,
2631 .build_pcms = ca0132_build_pcms,
2632 .init = ca0132_init,
2633 .free = ca0132_free,
2634};
2635
2636
2637
2638static int patch_ca0132(struct hda_codec *codec)
2639{
2640 struct ca0132_spec *spec;
2641
2642 snd_printdd("patch_ca0132\n");
2643
2644 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2645 if (!spec)
2646 return -ENOMEM;
2647 codec->spec = spec;
2648
2649 ca0132_init_chip(codec);
2650
2651 ca0132_config(codec);
2652
2653 codec->patch_ops = ca0132_patch_ops;
2654
2655 return 0;
2656}
2657
2658/*
2659 * patch entries
2660 */
2661static struct hda_codec_preset snd_hda_preset_ca0132[] = {
2662 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
2663 {} /* terminator */
2664};
2665
2666MODULE_ALIAS("snd-hda-codec-id:11020011");
2667
2668MODULE_LICENSE("GPL");
2669MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
2670
2671static struct hda_codec_preset_list ca0132_list = {
2672 .preset = snd_hda_preset_ca0132,
2673 .owner = THIS_MODULE,
2674};
2675
2676static int __init patch_ca0132_init(void)
2677{
2678 return snd_hda_add_codec_preset(&ca0132_list);
2679}
2680
2681static void __exit patch_ca0132_exit(void)
2682{
Ian Minettc3b4eea22012-09-20 20:29:17 -07002683 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07002684 snd_hda_delete_codec_preset(&ca0132_list);
2685}
2686
2687module_init(patch_ca0132_init)
2688module_exit(patch_ca0132_exit)