blob: a7b216ea9df3050ab38bc042b79d362c2d53d24c [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 Minettd5c21b82012-09-20 20:29:18 -0700522/*
523 * Write multiple data through the vendor widget -- NOT protected by the Mutex!
524 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700525static int chipio_write_data_multiple(struct hda_codec *codec,
526 const u32 *data,
527 unsigned int count)
528{
529 int status = 0;
530
531 if (data == NULL) {
532 snd_printdd(KERN_ERR "chipio_write_data null ptr");
533 return -EINVAL;
534 }
535
536 while ((count-- != 0) && (status == 0))
537 status = chipio_write_data(codec, *data++);
538
539 return status;
540}
541
542
Ian Minett95c6e9c2011-06-15 15:35:17 -0700543/*
544 * Read data through the vendor widget -- NOT protected by the Mutex!
545 */
546static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
547{
548 int res;
549
550 /* post read */
551 res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
552
553 if (res != -EIO) {
554 /* read status */
555 res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
556 }
557
558 if (res != -EIO) {
559 /* read data */
560 *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
561 VENDOR_CHIPIO_HIC_READ_DATA,
562 0);
563 }
564
565 return res;
566}
567
568/*
569 * Write given value to the given address through the chip I/O widget.
570 * protected by the Mutex
571 */
572static int chipio_write(struct hda_codec *codec,
573 unsigned int chip_addx, const unsigned int data)
574{
575 struct ca0132_spec *spec = codec->spec;
576 int err;
577
578 mutex_lock(&spec->chipio_mutex);
579
580 /* write the address, and if successful proceed to write data */
581 err = chipio_write_address(codec, chip_addx);
582 if (err < 0)
583 goto exit;
584
585 err = chipio_write_data(codec, data);
586 if (err < 0)
587 goto exit;
588
589exit:
590 mutex_unlock(&spec->chipio_mutex);
591 return err;
592}
593
Ian Minettd5c21b82012-09-20 20:29:18 -0700594/*
595 * Write multiple values to the given address through the chip I/O widget.
596 * protected by the Mutex
597 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700598static int chipio_write_multiple(struct hda_codec *codec,
599 u32 chip_addx,
600 const u32 *data,
601 unsigned int count)
602{
603 struct ca0132_spec *spec = codec->spec;
604 int status;
605
606 mutex_lock(&spec->chipio_mutex);
607 status = chipio_write_addx(codec, chip_addx);
608 if (status < 0)
609 goto error;
610
611 status = chipio_write_data_multiple(codec, data, count);
612error:
613 mutex_unlock(&spec->chipio_mutex);
614
615 return status;
616}
617
Ian Minett95c6e9c2011-06-15 15:35:17 -0700618/*
619 * Read the given address through the chip I/O widget
620 * protected by the Mutex
621 */
622static int chipio_read(struct hda_codec *codec,
623 unsigned int chip_addx, unsigned int *data)
624{
625 struct ca0132_spec *spec = codec->spec;
626 int err;
627
628 mutex_lock(&spec->chipio_mutex);
629
630 /* write the address, and if successful proceed to write data */
631 err = chipio_write_address(codec, chip_addx);
632 if (err < 0)
633 goto exit;
634
635 err = chipio_read_data(codec, data);
636 if (err < 0)
637 goto exit;
638
639exit:
640 mutex_unlock(&spec->chipio_mutex);
641 return err;
642}
643
Ian Minettd5c21b82012-09-20 20:29:18 -0700644/*
645 * Set chip control flags through the chip I/O widget.
646 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700647static void chipio_set_control_flag(struct hda_codec *codec,
648 enum control_flag_id flag_id,
649 bool flag_state)
650{
651 unsigned int val;
652 unsigned int flag_bit;
653
654 flag_bit = (flag_state ? 1 : 0);
655 val = (flag_bit << 7) | (flag_id);
656 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
657 VENDOR_CHIPIO_FLAG_SET, val);
658}
659
Ian Minettd5c21b82012-09-20 20:29:18 -0700660/*
661 * Set chip parameters through the chip I/O widget.
662 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700663static void chipio_set_control_param(struct hda_codec *codec,
664 enum control_param_id param_id, int param_val)
665{
666 struct ca0132_spec *spec = codec->spec;
667 int val;
668
669 if ((param_id < 32) && (param_val < 8)) {
670 val = (param_val << 5) | (param_id);
671 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
672 VENDOR_CHIPIO_PARAM_SET, val);
673 } else {
674 mutex_lock(&spec->chipio_mutex);
675 if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
676 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
677 VENDOR_CHIPIO_PARAM_EX_ID_SET,
678 param_id);
679 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
680 VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
681 param_val);
682 }
683 mutex_unlock(&spec->chipio_mutex);
684 }
685}
686
Ian Minettd5c21b82012-09-20 20:29:18 -0700687/*
688 * Set sampling rate of the connection point.
689 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700690static void chipio_set_conn_rate(struct hda_codec *codec,
691 int connid, enum ca0132_sample_rate rate)
692{
693 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
694 chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
695 rate);
696}
697
Ian Minettd5c21b82012-09-20 20:29:18 -0700698/*
699 * Enable clocks.
700 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700701static void chipio_enable_clocks(struct hda_codec *codec)
702{
703 struct ca0132_spec *spec = codec->spec;
704
705 mutex_lock(&spec->chipio_mutex);
706 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
707 VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
708 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
709 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
710 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
711 VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
712 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
713 VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
714 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
715 VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
716 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
717 VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
718 mutex_unlock(&spec->chipio_mutex);
719}
720
721/*
722 * CA0132 DSP IO stuffs
723 */
724static int dspio_send(struct hda_codec *codec, unsigned int reg,
725 unsigned int data)
726{
727 unsigned int res;
728 int retry = 50;
729
730 /* send bits of data specified by reg to dsp */
731 do {
732 res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
733 if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
734 return res;
735 } while (--retry);
736
737 return -EIO;
738}
739
Ian Minettd5c21b82012-09-20 20:29:18 -0700740/*
741 * Wait for DSP to be ready for commands
742 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700743static void dspio_write_wait(struct hda_codec *codec)
744{
745 int cur_val, prv_val;
746 int retry = 50;
747
748 cur_val = 0;
749 do {
750 prv_val = cur_val;
751 msleep(20);
752 dspio_send(codec, VENDOR_DSPIO_SCP_POST_COUNT_QUERY, 1);
753 dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
754 cur_val = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
755 VENDOR_DSPIO_SCP_READ_COUNT, 0);
756 } while (cur_val && (cur_val == prv_val) && --retry);
757}
758
Ian Minettd5c21b82012-09-20 20:29:18 -0700759/*
760 * Write SCP data to DSP
761 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700762static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
763{
764 struct ca0132_spec *spec = codec->spec;
765 int status;
766
767 dspio_write_wait(codec);
768
769 mutex_lock(&spec->chipio_mutex);
770 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
771 scp_data & 0xffff);
772 if (status < 0)
773 goto error;
774
775 status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
776 scp_data >> 16);
777 if (status < 0)
778 goto error;
779
780 /* OK, now check if the write itself has executed*/
781 status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
782 VENDOR_DSPIO_STATUS, 0);
783error:
784 mutex_unlock(&spec->chipio_mutex);
785
786 return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
787 -EIO : 0;
788}
789
Ian Minettd5c21b82012-09-20 20:29:18 -0700790/*
791 * Write multiple SCP data to DSP
792 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700793static int dspio_write_multiple(struct hda_codec *codec,
794 unsigned int *buffer, unsigned int size)
795{
796 int status = 0;
797 unsigned int count;
798
799 if ((buffer == NULL))
800 return -EINVAL;
801
802 count = 0;
803 while (count < size) {
804 status = dspio_write(codec, *buffer++);
805 if (status != 0)
806 break;
807 count++;
808 }
809
810 return status;
811}
812
Ian Minettd5c21b82012-09-20 20:29:18 -0700813/*
814 * Construct the SCP header using corresponding fields
815 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700816static inline unsigned int
817make_scp_header(unsigned int target_id, unsigned int source_id,
818 unsigned int get_flag, unsigned int req,
819 unsigned int device_flag, unsigned int resp_flag,
820 unsigned int error_flag, unsigned int data_size)
821{
822 unsigned int header = 0;
823
824 header = (data_size & 0x1f) << 27;
825 header |= (error_flag & 0x01) << 26;
826 header |= (resp_flag & 0x01) << 25;
827 header |= (device_flag & 0x01) << 24;
828 header |= (req & 0x7f) << 17;
829 header |= (get_flag & 0x01) << 16;
830 header |= (source_id & 0xff) << 8;
831 header |= target_id & 0xff;
832
833 return header;
834}
835
Ian Minettd5c21b82012-09-20 20:29:18 -0700836/*
837 * Extract corresponding fields from SCP header
838 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700839static inline void
840extract_scp_header(unsigned int header,
841 unsigned int *target_id, unsigned int *source_id,
842 unsigned int *get_flag, unsigned int *req,
843 unsigned int *device_flag, unsigned int *resp_flag,
844 unsigned int *error_flag, unsigned int *data_size)
845{
846 if (data_size)
847 *data_size = (header >> 27) & 0x1f;
848 if (error_flag)
849 *error_flag = (header >> 26) & 0x01;
850 if (resp_flag)
851 *resp_flag = (header >> 25) & 0x01;
852 if (device_flag)
853 *device_flag = (header >> 24) & 0x01;
854 if (req)
855 *req = (header >> 17) & 0x7f;
856 if (get_flag)
857 *get_flag = (header >> 16) & 0x01;
858 if (source_id)
859 *source_id = (header >> 8) & 0xff;
860 if (target_id)
861 *target_id = header & 0xff;
862}
863
864#define SCP_MAX_DATA_WORDS (16)
865
866/* Structure to contain any SCP message */
867struct scp_msg {
868 unsigned int hdr;
869 unsigned int data[SCP_MAX_DATA_WORDS];
870};
871
Ian Minettd5c21b82012-09-20 20:29:18 -0700872/*
873 * Send SCP message to DSP
874 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700875static int dspio_send_scp_message(struct hda_codec *codec,
876 unsigned char *send_buf,
877 unsigned int send_buf_size,
878 unsigned char *return_buf,
879 unsigned int return_buf_size,
880 unsigned int *bytes_returned)
881{
882 struct ca0132_spec *spec = codec->spec;
883 int retry;
884 int status = -1;
885 unsigned int scp_send_size = 0;
886 unsigned int total_size;
887 bool waiting_for_resp = false;
888 unsigned int header;
889 struct scp_msg *ret_msg;
890 unsigned int resp_src_id, resp_target_id;
891 unsigned int data_size, src_id, target_id, get_flag, device_flag;
892
893 if (bytes_returned)
894 *bytes_returned = 0;
895
896 /* get scp header from buffer */
897 header = *((unsigned int *)send_buf);
898 extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
899 &device_flag, NULL, NULL, &data_size);
900 scp_send_size = data_size + 1;
901 total_size = (scp_send_size * 4);
902
903 if (send_buf_size < total_size)
904 return -EINVAL;
905
906 if (get_flag || device_flag) {
907 if (!return_buf || return_buf_size < 4 || !bytes_returned)
908 return -EINVAL;
909
910 spec->wait_scp_header = *((unsigned int *)send_buf);
911
912 /* swap source id with target id */
913 resp_target_id = src_id;
914 resp_src_id = target_id;
915 spec->wait_scp_header &= 0xffff0000;
916 spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
917 spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
918 spec->wait_scp = 1;
919 waiting_for_resp = true;
920 }
921
922 status = dspio_write_multiple(codec, (unsigned int *)send_buf,
923 scp_send_size);
924 if (status < 0) {
925 spec->wait_scp = 0;
926 return status;
927 }
928
929 if (waiting_for_resp) {
930 memset(return_buf, 0, return_buf_size);
931 retry = 50;
932 do {
933 msleep(20);
934 } while (spec->wait_scp && (--retry != 0));
935 waiting_for_resp = false;
936 if (retry != 0) {
937 ret_msg = (struct scp_msg *)return_buf;
938 memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
939 memcpy(&ret_msg->data, spec->scp_resp_data,
940 spec->wait_num_data);
941 *bytes_returned = (spec->scp_resp_count + 1) * 4;
942 status = 0;
943 } else {
944 status = -EIO;
945 }
946 spec->wait_scp = 0;
947 }
948
949 return status;
950}
951
Ian Minettd5c21b82012-09-20 20:29:18 -0700952/**
953 * Prepare and send the SCP message to DSP
954 * @codec: the HDA codec
955 * @mod_id: ID of the DSP module to send the command
956 * @req: ID of request to send to the DSP module
957 * @dir: SET or GET
958 * @data: pointer to the data to send with the request, request specific
959 * @len: length of the data, in bytes
960 * @reply: point to the buffer to hold data returned for a reply
961 * @reply_len: length of the reply buffer returned from GET
962 *
963 * Returns zero or a negative error code.
964 */
Ian Minett01ef7db2012-09-20 20:29:16 -0700965static int dspio_scp(struct hda_codec *codec,
966 int mod_id, int req, int dir, void *data, unsigned int len,
967 void *reply, unsigned int *reply_len)
968{
969 int status = 0;
970 struct scp_msg scp_send, scp_reply;
971 unsigned int ret_bytes, send_size, ret_size;
972 unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
973 unsigned int reply_data_size;
974
975 memset(&scp_send, 0, sizeof(scp_send));
976 memset(&scp_reply, 0, sizeof(scp_reply));
977
978 if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
979 return -EINVAL;
980
981 if (dir == SCP_GET && reply == NULL) {
982 snd_printdd(KERN_ERR "dspio_scp get but has no buffer");
983 return -EINVAL;
984 }
985
986 if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
987 snd_printdd(KERN_ERR "dspio_scp bad resp buf len parms");
988 return -EINVAL;
989 }
990
991 scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
992 0, 0, 0, len/sizeof(unsigned int));
993 if (data != NULL && len > 0) {
994 len = min((unsigned int)(sizeof(scp_send.data)), len);
995 memcpy(scp_send.data, data, len);
996 }
997
998 ret_bytes = 0;
999 send_size = sizeof(unsigned int) + len;
1000 status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
1001 send_size, (unsigned char *)&scp_reply,
1002 sizeof(scp_reply), &ret_bytes);
1003
1004 if (status < 0) {
1005 snd_printdd(KERN_ERR "dspio_scp: send scp msg failed");
1006 return status;
1007 }
1008
1009 /* extract send and reply headers members */
1010 extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
1011 NULL, NULL, NULL, NULL, NULL);
1012 extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
1013 &reply_resp_flag, &reply_error_flag,
1014 &reply_data_size);
1015
1016 if (!send_get_flag)
1017 return 0;
1018
1019 if (reply_resp_flag && !reply_error_flag) {
1020 ret_size = (ret_bytes - sizeof(scp_reply.hdr))
1021 / sizeof(unsigned int);
1022
1023 if (*reply_len < ret_size*sizeof(unsigned int)) {
1024 snd_printdd(KERN_ERR "reply too long for buf");
1025 return -EINVAL;
1026 } else if (ret_size != reply_data_size) {
1027 snd_printdd(KERN_ERR "RetLen and HdrLen .NE.");
1028 return -EINVAL;
1029 } else {
1030 *reply_len = ret_size*sizeof(unsigned int);
1031 memcpy(reply, scp_reply.data, *reply_len);
1032 }
1033 } else {
1034 snd_printdd(KERN_ERR "reply ill-formed or errflag set");
1035 return -EIO;
1036 }
1037
1038 return status;
1039}
1040
Ian Minettd5c21b82012-09-20 20:29:18 -07001041/*
1042 * Allocate a DSP DMA channel via an SCP message
1043 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001044static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
1045{
1046 int status = 0;
1047 unsigned int size = sizeof(dma_chan);
1048
1049 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- begin");
1050 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1051 SCP_GET, NULL, 0, dma_chan, &size);
1052
1053 if (status < 0) {
1054 snd_printdd(KERN_INFO "dspio_alloc_dma_chan: SCP Failed");
1055 return status;
1056 }
1057
1058 if ((*dma_chan + 1) == 0) {
1059 snd_printdd(KERN_INFO "no free dma channels to allocate");
1060 return -EBUSY;
1061 }
1062
1063 snd_printdd("dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
1064 snd_printdd(KERN_INFO " dspio_alloc_dma_chan() -- complete");
1065
1066 return status;
1067}
1068
Ian Minettd5c21b82012-09-20 20:29:18 -07001069/*
1070 * Free a DSP DMA via an SCP message
1071 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001072static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
1073{
1074 int status = 0;
1075 unsigned int dummy = 0;
1076
1077 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- begin");
1078 snd_printdd("dspio_free_dma_chan: chan=%d\n", dma_chan);
1079
1080 status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
1081 SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
1082
1083 if (status < 0) {
1084 snd_printdd(KERN_INFO "dspio_free_dma_chan: SCP Failed");
1085 return status;
1086 }
1087
1088 snd_printdd(KERN_INFO " dspio_free_dma_chan() -- complete");
1089
1090 return status;
1091}
1092
1093/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001094 * (Re)start the DSP
Ian Minett01ef7db2012-09-20 20:29:16 -07001095 */
1096static int dsp_set_run_state(struct hda_codec *codec)
1097{
1098 unsigned int dbg_ctrl_reg;
1099 unsigned int halt_state;
1100 int err;
1101
1102 err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
1103 if (err < 0)
1104 return err;
1105
1106 halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
1107 DSP_DBGCNTL_STATE_LOBIT;
1108
1109 if (halt_state != 0) {
1110 dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
1111 DSP_DBGCNTL_SS_MASK);
1112 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1113 dbg_ctrl_reg);
1114 if (err < 0)
1115 return err;
1116
1117 dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
1118 DSP_DBGCNTL_EXEC_MASK;
1119 err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
1120 dbg_ctrl_reg);
1121 if (err < 0)
1122 return err;
1123 }
1124
1125 return 0;
1126}
1127
Ian Minettd5c21b82012-09-20 20:29:18 -07001128/*
1129 * Reset the DSP
1130 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001131static int dsp_reset(struct hda_codec *codec)
1132{
1133 unsigned int res;
1134 int retry = 20;
1135
1136 snd_printdd("dsp_reset\n");
1137 do {
1138 res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
1139 retry--;
1140 } while (res == -EIO && retry);
1141
1142 if (!retry) {
1143 snd_printdd("dsp_reset timeout\n");
1144 return -EIO;
1145 }
1146
1147 return 0;
1148}
1149
Ian Minettd5c21b82012-09-20 20:29:18 -07001150/*
1151 * Convert chip address to DSP address
1152 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001153static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
1154 bool *code, bool *yram)
1155{
1156 *code = *yram = false;
1157
1158 if (UC_RANGE(chip_addx, 1)) {
1159 *code = true;
1160 return UC_OFF(chip_addx);
1161 } else if (X_RANGE_ALL(chip_addx, 1)) {
1162 return X_OFF(chip_addx);
1163 } else if (Y_RANGE_ALL(chip_addx, 1)) {
1164 *yram = true;
1165 return Y_OFF(chip_addx);
1166 }
1167
1168 return (unsigned int)INVALID_CHIP_ADDRESS;
1169}
1170
Ian Minettd5c21b82012-09-20 20:29:18 -07001171/*
1172 * Check if the DSP DMA is active
1173 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001174static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
1175{
1176 unsigned int dma_chnlstart_reg;
1177
1178 chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
1179
1180 return ((dma_chnlstart_reg & (1 <<
1181 (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
1182}
1183
1184static int dsp_dma_setup_common(struct hda_codec *codec,
1185 unsigned int chip_addx,
1186 unsigned int dma_chan,
1187 unsigned int port_map_mask,
1188 bool ovly)
1189{
1190 int status = 0;
1191 unsigned int chnl_prop;
1192 unsigned int dsp_addx;
1193 unsigned int active;
1194 bool code, yram;
1195
1196 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Begin ---------");
1197
1198 if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
1199 snd_printdd(KERN_ERR "dma chan num invalid");
1200 return -EINVAL;
1201 }
1202
1203 if (dsp_is_dma_active(codec, dma_chan)) {
1204 snd_printdd(KERN_ERR "dma already active");
1205 return -EBUSY;
1206 }
1207
1208 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1209
1210 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1211 snd_printdd(KERN_ERR "invalid chip addr");
1212 return -ENXIO;
1213 }
1214
1215 chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
1216 active = 0;
1217
1218 snd_printdd(KERN_INFO " dsp_dma_setup_common() start reg pgm");
1219
1220 if (ovly) {
1221 status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
1222 &chnl_prop);
1223
1224 if (status < 0) {
1225 snd_printdd(KERN_ERR "read CHNLPROP Reg fail");
1226 return status;
1227 }
1228 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read CHNLPROP");
1229 }
1230
1231 if (!code)
1232 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1233 else
1234 chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
1235
1236 chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
1237
1238 status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
1239 if (status < 0) {
1240 snd_printdd(KERN_ERR "write CHNLPROP Reg fail");
1241 return status;
1242 }
1243 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write CHNLPROP");
1244
1245 if (ovly) {
1246 status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
1247 &active);
1248
1249 if (status < 0) {
1250 snd_printdd(KERN_ERR "read ACTIVE Reg fail");
1251 return status;
1252 }
1253 snd_printdd(KERN_INFO "dsp_dma_setup_common() Read ACTIVE");
1254 }
1255
1256 active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
1257 DSPDMAC_ACTIVE_AAR_MASK;
1258
1259 status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
1260 if (status < 0) {
1261 snd_printdd(KERN_ERR "write ACTIVE Reg fail");
1262 return status;
1263 }
1264
1265 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write ACTIVE");
1266
1267 status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
1268 port_map_mask);
1269 if (status < 0) {
1270 snd_printdd(KERN_ERR "write AUDCHSEL Reg fail");
1271 return status;
1272 }
1273 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write AUDCHSEL");
1274
1275 status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
1276 DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
1277 if (status < 0) {
1278 snd_printdd(KERN_ERR "write IRQCNT Reg fail");
1279 return status;
1280 }
1281 snd_printdd(KERN_INFO " dsp_dma_setup_common() Write IRQCNT");
1282
1283 snd_printdd(
1284 "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
1285 "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
1286 chip_addx, dsp_addx, dma_chan,
1287 port_map_mask, chnl_prop, active);
1288
1289 snd_printdd(KERN_INFO "-- dsp_dma_setup_common() -- Complete ------");
1290
1291 return 0;
1292}
1293
Ian Minettd5c21b82012-09-20 20:29:18 -07001294/*
1295 * Setup the DSP DMA per-transfer-specific registers
1296 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001297static int dsp_dma_setup(struct hda_codec *codec,
1298 unsigned int chip_addx,
1299 unsigned int count,
1300 unsigned int dma_chan)
1301{
1302 int status = 0;
1303 bool code, yram;
1304 unsigned int dsp_addx;
1305 unsigned int addr_field;
1306 unsigned int incr_field;
1307 unsigned int base_cnt;
1308 unsigned int cur_cnt;
1309 unsigned int dma_cfg = 0;
1310 unsigned int adr_ofs = 0;
1311 unsigned int xfr_cnt = 0;
1312 const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
1313 DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
1314
1315 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Begin ---------");
1316
1317 if (count > max_dma_count) {
1318 snd_printdd(KERN_ERR "count too big");
1319 return -EINVAL;
1320 }
1321
1322 dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
1323 if (dsp_addx == INVALID_CHIP_ADDRESS) {
1324 snd_printdd(KERN_ERR "invalid chip addr");
1325 return -ENXIO;
1326 }
1327
1328 snd_printdd(KERN_INFO " dsp_dma_setup() start reg pgm");
1329
1330 addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
1331 incr_field = 0;
1332
1333 if (!code) {
1334 addr_field <<= 1;
1335 if (yram)
1336 addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
1337
1338 incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
1339 }
1340
1341 dma_cfg = addr_field + incr_field;
1342 status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
1343 dma_cfg);
1344 if (status < 0) {
1345 snd_printdd(KERN_ERR "write DMACFG Reg fail");
1346 return status;
1347 }
1348 snd_printdd(KERN_INFO " dsp_dma_setup() Write DMACFG");
1349
1350 adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
1351 (code ? 0 : 1));
1352
1353 status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
1354 adr_ofs);
1355 if (status < 0) {
1356 snd_printdd(KERN_ERR "write DSPADROFS Reg fail");
1357 return status;
1358 }
1359 snd_printdd(KERN_INFO " dsp_dma_setup() Write DSPADROFS");
1360
1361 base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
1362
1363 cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
1364
1365 xfr_cnt = base_cnt | cur_cnt;
1366
1367 status = chipio_write(codec,
1368 DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
1369 if (status < 0) {
1370 snd_printdd(KERN_ERR "write XFRCNT Reg fail");
1371 return status;
1372 }
1373 snd_printdd(KERN_INFO " dsp_dma_setup() Write XFRCNT");
1374
1375 snd_printdd(
1376 "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
1377 "ADROFS=0x%x, XFRCNT=0x%x\n",
1378 chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
1379
1380 snd_printdd(KERN_INFO "-- dsp_dma_setup() -- Complete ---------");
1381
1382 return 0;
1383}
1384
Ian Minettd5c21b82012-09-20 20:29:18 -07001385/*
1386 * Start the DSP DMA
1387 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001388static int dsp_dma_start(struct hda_codec *codec,
1389 unsigned int dma_chan, bool ovly)
1390{
1391 unsigned int reg = 0;
1392 int status = 0;
1393
1394 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Begin ---------");
1395
1396 if (ovly) {
1397 status = chipio_read(codec,
1398 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1399
1400 if (status < 0) {
1401 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1402 return status;
1403 }
1404 snd_printdd(KERN_INFO "-- dsp_dma_start() Read CHNLSTART");
1405
1406 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1407 DSPDMAC_CHNLSTART_DIS_MASK);
1408 }
1409
1410 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1411 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
1412 if (status < 0) {
1413 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1414 return status;
1415 }
1416 snd_printdd(KERN_INFO "-- dsp_dma_start() -- Complete ---------");
1417
1418 return status;
1419}
1420
Ian Minettd5c21b82012-09-20 20:29:18 -07001421/*
1422 * Stop the DSP DMA
1423 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001424static int dsp_dma_stop(struct hda_codec *codec,
1425 unsigned int dma_chan, bool ovly)
1426{
1427 unsigned int reg = 0;
1428 int status = 0;
1429
1430 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Begin ---------");
1431
1432 if (ovly) {
1433 status = chipio_read(codec,
1434 DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
1435
1436 if (status < 0) {
1437 snd_printdd(KERN_ERR "read CHNLSTART reg fail");
1438 return status;
1439 }
1440 snd_printdd(KERN_INFO "-- dsp_dma_stop() Read CHNLSTART");
1441 reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
1442 DSPDMAC_CHNLSTART_DIS_MASK);
1443 }
1444
1445 status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
1446 reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
1447 if (status < 0) {
1448 snd_printdd(KERN_ERR "write CHNLSTART reg fail");
1449 return status;
1450 }
1451 snd_printdd(KERN_INFO "-- dsp_dma_stop() -- Complete ---------");
1452
1453 return status;
1454}
1455
Ian Minettd5c21b82012-09-20 20:29:18 -07001456/**
1457 * Allocate router ports
1458 *
1459 * @codec: the HDA codec
1460 * @num_chans: number of channels in the stream
1461 * @ports_per_channel: number of ports per channel
1462 * @start_device: start device
1463 * @port_map: pointer to the port list to hold the allocated ports
1464 *
1465 * Returns zero or a negative error code.
1466 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001467static int dsp_allocate_router_ports(struct hda_codec *codec,
1468 unsigned int num_chans,
1469 unsigned int ports_per_channel,
1470 unsigned int start_device,
1471 unsigned int *port_map)
1472{
1473 int status = 0;
1474 int res;
1475 u8 val;
1476
1477 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1478 if (status < 0)
1479 return status;
1480
1481 val = start_device << 6;
1482 val |= (ports_per_channel - 1) << 4;
1483 val |= num_chans - 1;
1484
1485 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1486 VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
1487 val);
1488
1489 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1490 VENDOR_CHIPIO_PORT_ALLOC_SET,
1491 MEM_CONNID_DSP);
1492
1493 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1494 if (status < 0)
1495 return status;
1496
1497 res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1498 VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
1499
1500 *port_map = res;
1501
1502 return (res < 0) ? res : 0;
1503}
1504
Ian Minettd5c21b82012-09-20 20:29:18 -07001505/*
1506 * Free router ports
1507 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001508static int dsp_free_router_ports(struct hda_codec *codec)
1509{
1510 int status = 0;
1511
1512 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1513 if (status < 0)
1514 return status;
1515
1516 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1517 VENDOR_CHIPIO_PORT_FREE_SET,
1518 MEM_CONNID_DSP);
1519
1520 status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1521
1522 return status;
1523}
1524
Ian Minettd5c21b82012-09-20 20:29:18 -07001525/*
1526 * Allocate DSP ports for the download stream
1527 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001528static int dsp_allocate_ports(struct hda_codec *codec,
1529 unsigned int num_chans,
1530 unsigned int rate_multi, unsigned int *port_map)
1531{
1532 int status;
1533
1534 snd_printdd(KERN_INFO " dsp_allocate_ports() -- begin");
1535
1536 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1537 snd_printdd(KERN_ERR "bad rate multiple");
1538 return -EINVAL;
1539 }
1540
1541 status = dsp_allocate_router_ports(codec, num_chans,
1542 rate_multi, 0, port_map);
1543
1544 snd_printdd(KERN_INFO " dsp_allocate_ports() -- complete");
1545
1546 return status;
1547}
1548
Ian Minett01ef7db2012-09-20 20:29:16 -07001549static int dsp_allocate_ports_format(struct hda_codec *codec,
1550 const unsigned short fmt,
1551 unsigned int *port_map)
1552{
1553 int status;
1554 unsigned int num_chans;
1555
1556 unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
1557 unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
1558 unsigned int rate_multi = sample_rate_mul / sample_rate_div;
1559
1560 if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
1561 snd_printdd(KERN_ERR "bad rate multiple");
1562 return -EINVAL;
1563 }
1564
1565 num_chans = get_hdafmt_chs(fmt) + 1;
1566
1567 status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
1568
1569 return status;
1570}
1571
1572/*
Ian Minettd5c21b82012-09-20 20:29:18 -07001573 * free DSP ports
1574 */
1575static int dsp_free_ports(struct hda_codec *codec)
1576{
1577 int status;
1578
1579 snd_printdd(KERN_INFO " dsp_free_ports() -- begin");
1580
1581 status = dsp_free_router_ports(codec);
1582 if (status < 0) {
1583 snd_printdd(KERN_ERR "free router ports fail");
1584 return status;
1585 }
1586 snd_printdd(KERN_INFO " dsp_free_ports() -- complete");
1587
1588 return status;
1589}
1590
1591/*
Ian Minett01ef7db2012-09-20 20:29:16 -07001592 * HDA DMA engine stuffs for DSP code download
1593 */
1594struct dma_engine {
1595 struct hda_codec *codec;
1596 unsigned short m_converter_format;
1597 struct snd_dma_buffer *dmab;
1598 unsigned int buf_size;
1599};
1600
1601
1602enum dma_state {
1603 DMA_STATE_STOP = 0,
1604 DMA_STATE_RUN = 1
1605};
1606
1607static int dma_convert_to_hda_format(
1608 struct hda_stream_format *stream_format,
1609 unsigned short *hda_format)
1610{
1611 unsigned int format_val;
1612
1613 format_val = snd_hda_calc_stream_format(
1614 stream_format->sample_rate,
1615 stream_format->number_channels,
1616 SNDRV_PCM_FORMAT_S32_LE,
1617 stream_format->container_size, 0);
1618
1619 if (hda_format)
1620 *hda_format = (unsigned short)format_val;
1621
1622 return 0;
1623}
1624
Ian Minettd5c21b82012-09-20 20:29:18 -07001625/*
1626 * Reset DMA for DSP download
1627 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001628static int dma_reset(struct dma_engine *dma)
1629{
1630 struct hda_codec *codec = dma->codec;
1631 struct ca0132_spec *spec = codec->spec;
1632 int status;
1633
1634 if (dma->dmab)
1635 snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
1636
1637 status = snd_hda_codec_load_dsp_prepare(codec,
1638 dma->m_converter_format,
1639 dma->buf_size,
1640 dma->dmab);
1641 if (status < 0)
1642 return status;
1643 spec->dsp_stream_id = status;
1644 return 0;
1645}
1646
1647static int dma_set_state(struct dma_engine *dma, enum dma_state state)
1648{
1649 bool cmd;
1650
1651 snd_printdd("dma_set_state state=%d\n", state);
1652
1653 switch (state) {
1654 case DMA_STATE_STOP:
1655 cmd = false;
1656 break;
1657 case DMA_STATE_RUN:
1658 cmd = true;
1659 break;
1660 default:
1661 return 0;
1662 }
1663
1664 snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
1665 return 0;
1666}
1667
1668static unsigned int dma_get_buffer_size(struct dma_engine *dma)
1669{
1670 return dma->dmab->bytes;
1671}
1672
1673static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
1674{
1675 return dma->dmab->area;
1676}
1677
1678static int dma_xfer(struct dma_engine *dma,
1679 const unsigned int *data,
1680 unsigned int count)
1681{
1682 memcpy(dma->dmab->area, data, count);
1683 return 0;
1684}
1685
1686static void dma_get_converter_format(
1687 struct dma_engine *dma,
1688 unsigned short *format)
1689{
1690 if (format)
1691 *format = dma->m_converter_format;
1692}
1693
1694static unsigned int dma_get_stream_id(struct dma_engine *dma)
1695{
1696 struct ca0132_spec *spec = dma->codec->spec;
1697
1698 return spec->dsp_stream_id;
1699}
1700
1701struct dsp_image_seg {
1702 u32 magic;
1703 u32 chip_addr;
1704 u32 count;
1705 u32 data[0];
1706};
1707
1708static const u32 g_magic_value = 0x4c46584d;
1709static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
1710
1711static bool is_valid(const struct dsp_image_seg *p)
1712{
1713 return p->magic == g_magic_value;
1714}
1715
1716static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
1717{
1718 return g_chip_addr_magic_value == p->chip_addr;
1719}
1720
1721static bool is_last(const struct dsp_image_seg *p)
1722{
1723 return p->count == 0;
1724}
1725
1726static size_t dsp_sizeof(const struct dsp_image_seg *p)
1727{
1728 return sizeof(*p) + p->count*sizeof(u32);
1729}
1730
1731static const struct dsp_image_seg *get_next_seg_ptr(
1732 const struct dsp_image_seg *p)
1733{
1734 return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
1735}
1736
1737/*
1738 * CA0132 chip DSP transfer stuffs. For DSP download.
1739 */
1740#define INVALID_DMA_CHANNEL (~0UL)
1741
Ian Minettd5c21b82012-09-20 20:29:18 -07001742/*
1743 * Program a list of address/data pairs via the ChipIO widget.
1744 * The segment data is in the format of successive pairs of words.
1745 * These are repeated as indicated by the segment's count field.
1746 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001747static int dspxfr_hci_write(struct hda_codec *codec,
1748 const struct dsp_image_seg *fls)
1749{
1750 int status;
1751 const u32 *data;
1752 unsigned int count;
1753
1754 if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
1755 snd_printdd(KERN_ERR "hci_write invalid params");
1756 return -EINVAL;
1757 }
1758
1759 count = fls->count;
1760 data = (u32 *)(fls->data);
1761 while (count >= 2) {
1762 status = chipio_write(codec, data[0], data[1]);
1763 if (status < 0) {
1764 snd_printdd(KERN_ERR "hci_write chipio failed");
1765 return status;
1766 }
1767 count -= 2;
1768 data += 2;
1769 }
1770 return 0;
1771}
1772
Ian Minettd5c21b82012-09-20 20:29:18 -07001773/**
1774 * Write a block of data into DSP code or data RAM using pre-allocated
1775 * DMA engine.
1776 *
1777 * @codec: the HDA codec
1778 * @fls: pointer to a fast load image
1779 * @reloc: Relocation address for loading single-segment overlays, or 0 for
1780 * no relocation
1781 * @dma_engine: pointer to DMA engine to be used for DSP download
1782 * @dma_chan: The number of DMA channels used for DSP download
1783 * @port_map_mask: port mapping
1784 * @ovly: TRUE if overlay format is required
1785 *
1786 * Returns zero or a negative error code.
1787 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001788static int dspxfr_one_seg(struct hda_codec *codec,
1789 const struct dsp_image_seg *fls,
1790 unsigned int reloc,
1791 struct dma_engine *dma_engine,
1792 unsigned int dma_chan,
1793 unsigned int port_map_mask,
1794 bool ovly)
1795{
1796 int status;
1797 bool comm_dma_setup_done = false;
1798 const unsigned int *data;
1799 unsigned int chip_addx;
1800 unsigned int words_to_write;
1801 unsigned int buffer_size_words;
1802 unsigned char *buffer_addx;
1803 unsigned short hda_format;
1804 unsigned int sample_rate_div;
1805 unsigned int sample_rate_mul;
1806 unsigned int num_chans;
1807 unsigned int hda_frame_size_words;
1808 unsigned int remainder_words;
1809 const u32 *data_remainder;
1810 u32 chip_addx_remainder;
1811 unsigned int run_size_words;
1812 const struct dsp_image_seg *hci_write = NULL;
1813 int retry;
1814
1815 if (fls == NULL)
1816 return -EINVAL;
1817 if (is_hci_prog_list_seg(fls)) {
1818 hci_write = fls;
1819 fls = get_next_seg_ptr(fls);
1820 }
1821
1822 if (hci_write && (!fls || is_last(fls))) {
1823 snd_printdd("hci_write\n");
1824 return dspxfr_hci_write(codec, hci_write);
1825 }
1826
1827 if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
1828 snd_printdd("Invalid Params\n");
1829 return -EINVAL;
1830 }
1831
1832 data = fls->data;
1833 chip_addx = fls->chip_addr,
1834 words_to_write = fls->count;
1835
1836 if (!words_to_write)
1837 return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
1838 if (reloc)
1839 chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
1840
1841 if (!UC_RANGE(chip_addx, words_to_write) &&
1842 !X_RANGE_ALL(chip_addx, words_to_write) &&
1843 !Y_RANGE_ALL(chip_addx, words_to_write)) {
1844 snd_printdd("Invalid chip_addx Params\n");
1845 return -EINVAL;
1846 }
1847
1848 buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
1849 sizeof(u32);
1850
1851 buffer_addx = dma_get_buffer_addr(dma_engine);
1852
1853 if (buffer_addx == NULL) {
1854 snd_printdd(KERN_ERR "dma_engine buffer NULL\n");
1855 return -EINVAL;
1856 }
1857
1858 dma_get_converter_format(dma_engine, &hda_format);
1859 sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
1860 sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
1861 num_chans = get_hdafmt_chs(hda_format) + 1;
1862
1863 hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
1864 (num_chans * sample_rate_mul / sample_rate_div));
1865
1866 buffer_size_words = min(buffer_size_words,
1867 (unsigned int)(UC_RANGE(chip_addx, 1) ?
1868 65536 : 32768));
1869 buffer_size_words -= buffer_size_words % hda_frame_size_words;
1870 snd_printdd(
1871 "chpadr=0x%08x frmsz=%u nchan=%u "
1872 "rate_mul=%u div=%u bufsz=%u\n",
1873 chip_addx, hda_frame_size_words, num_chans,
1874 sample_rate_mul, sample_rate_div, buffer_size_words);
1875
1876 if ((buffer_addx == NULL) || (hda_frame_size_words == 0) ||
1877 (buffer_size_words < hda_frame_size_words)) {
1878 snd_printdd(KERN_ERR "dspxfr_one_seg:failed\n");
1879 return -EINVAL;
1880 }
1881
1882 remainder_words = words_to_write % hda_frame_size_words;
1883 data_remainder = data;
1884 chip_addx_remainder = chip_addx;
1885
1886 data += remainder_words;
1887 chip_addx += remainder_words*sizeof(u32);
1888 words_to_write -= remainder_words;
1889
1890 while (words_to_write != 0) {
1891 run_size_words = min(buffer_size_words, words_to_write);
1892 snd_printdd("dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
1893 words_to_write, run_size_words, remainder_words);
1894 dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
1895 if (!comm_dma_setup_done) {
1896 status = dsp_dma_stop(codec, dma_chan, ovly);
1897 if (status < 0)
1898 return -EIO;
1899 status = dsp_dma_setup_common(codec, chip_addx,
1900 dma_chan, port_map_mask, ovly);
1901 if (status < 0)
1902 return status;
1903 comm_dma_setup_done = true;
1904 }
1905
1906 status = dsp_dma_setup(codec, chip_addx,
1907 run_size_words, dma_chan);
1908 if (status < 0)
1909 return status;
1910 status = dsp_dma_start(codec, dma_chan, ovly);
1911 if (status < 0)
1912 return status;
1913 if (!dsp_is_dma_active(codec, dma_chan)) {
1914 snd_printdd(KERN_ERR "dspxfr:DMA did not start");
1915 return -EIO;
1916 }
1917 status = dma_set_state(dma_engine, DMA_STATE_RUN);
1918 if (status < 0)
1919 return status;
1920 if (remainder_words != 0) {
1921 status = chipio_write_multiple(codec,
1922 chip_addx_remainder,
1923 data_remainder,
1924 remainder_words);
1925 remainder_words = 0;
1926 }
1927 if (hci_write) {
1928 status = dspxfr_hci_write(codec, hci_write);
1929 hci_write = NULL;
1930 }
1931 retry = 5000;
1932 while (dsp_is_dma_active(codec, dma_chan)) {
1933 if (--retry <= 0)
1934 break;
1935 }
1936 snd_printdd(KERN_INFO "+++++ DMA complete");
1937 dma_set_state(dma_engine, DMA_STATE_STOP);
1938 dma_reset(dma_engine);
1939
1940 if (status < 0)
1941 return status;
1942
1943 data += run_size_words;
1944 chip_addx += run_size_words*sizeof(u32);
1945 words_to_write -= run_size_words;
1946 }
1947
1948 if (remainder_words != 0) {
1949 status = chipio_write_multiple(codec, chip_addx_remainder,
1950 data_remainder, remainder_words);
1951 }
1952
1953 return status;
1954}
1955
Ian Minettd5c21b82012-09-20 20:29:18 -07001956/**
1957 * Write the entire DSP image of a DSP code/data overlay to DSP memories
1958 *
1959 * @codec: the HDA codec
1960 * @fls_data: pointer to a fast load image
1961 * @reloc: Relocation address for loading single-segment overlays, or 0 for
1962 * no relocation
1963 * @format: format of the stream used for DSP download
1964 * @ovly: TRUE if overlay format is required
1965 *
1966 * Returns zero or a negative error code.
1967 */
Ian Minett01ef7db2012-09-20 20:29:16 -07001968static int dspxfr_image(struct hda_codec *codec,
1969 const struct dsp_image_seg *fls_data,
1970 unsigned int reloc, struct hda_stream_format *format,
1971 bool ovly)
1972{
1973 struct ca0132_spec *spec = codec->spec;
1974 int status;
1975 unsigned short hda_format = 0;
1976 unsigned int response;
1977 unsigned char stream_id = 0;
1978 struct dma_engine *dma_engine;
1979 unsigned int dma_chan;
1980 unsigned int port_map_mask;
1981
1982 if (fls_data == NULL)
1983 return -EINVAL;
1984
1985 dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
1986 if (!dma_engine) {
1987 status = -ENOMEM;
1988 goto exit;
1989 }
1990 memset((void *)dma_engine, 0, sizeof(*dma_engine));
1991
1992 dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
1993 if (!dma_engine->dmab) {
1994 status = -ENOMEM;
1995 goto exit;
1996 }
1997
1998 dma_engine->codec = codec;
1999 dma_convert_to_hda_format(format, &hda_format);
2000 dma_engine->m_converter_format = hda_format;
2001 dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
2002 DSP_DMA_WRITE_BUFLEN_INIT) * 2;
2003
2004 dma_chan = 0;
2005
2006 status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
2007 hda_format, &response);
2008
2009 if (status < 0) {
2010 snd_printdd(KERN_ERR "set converter format fail");
2011 goto exit;
2012 }
2013
2014 status = snd_hda_codec_load_dsp_prepare(codec,
2015 dma_engine->m_converter_format,
2016 dma_engine->buf_size,
2017 dma_engine->dmab);
2018 if (status < 0)
2019 goto exit;
2020 spec->dsp_stream_id = status;
2021
2022 if (ovly) {
2023 status = dspio_alloc_dma_chan(codec, &dma_chan);
2024 if (status < 0) {
2025 snd_printdd(KERN_ERR "alloc dmachan fail");
2026 dma_chan = (unsigned int)INVALID_DMA_CHANNEL;
2027 goto exit;
2028 }
2029 }
2030
2031 port_map_mask = 0;
2032 status = dsp_allocate_ports_format(codec, hda_format,
2033 &port_map_mask);
2034 if (status < 0) {
2035 snd_printdd(KERN_ERR "alloc ports fail");
2036 goto exit;
2037 }
2038
2039 stream_id = dma_get_stream_id(dma_engine);
2040 status = codec_set_converter_stream_channel(codec,
2041 WIDGET_CHIP_CTRL, stream_id, 0, &response);
2042 if (status < 0) {
2043 snd_printdd(KERN_ERR "set stream chan fail");
2044 goto exit;
2045 }
2046
2047 while ((fls_data != NULL) && !is_last(fls_data)) {
2048 if (!is_valid(fls_data)) {
2049 snd_printdd(KERN_ERR "FLS check fail");
2050 status = -EINVAL;
2051 goto exit;
2052 }
2053 status = dspxfr_one_seg(codec, fls_data, reloc,
2054 dma_engine, dma_chan,
2055 port_map_mask, ovly);
2056 if (status < 0)
2057 break;
2058
2059 if (is_hci_prog_list_seg(fls_data))
2060 fls_data = get_next_seg_ptr(fls_data);
2061
2062 if ((fls_data != NULL) && !is_last(fls_data))
2063 fls_data = get_next_seg_ptr(fls_data);
2064 }
2065
2066 if (port_map_mask != 0)
2067 status = dsp_free_ports(codec);
2068
2069 if (status < 0)
2070 goto exit;
2071
2072 status = codec_set_converter_stream_channel(codec,
2073 WIDGET_CHIP_CTRL, 0, 0, &response);
2074
2075exit:
2076 if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
2077 dspio_free_dma_chan(codec, dma_chan);
2078
2079 if (dma_engine->dmab)
2080 snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
2081 kfree(dma_engine->dmab);
2082 kfree(dma_engine);
2083
2084 return status;
2085}
2086
2087/*
2088 * CA0132 DSP download stuffs.
2089 */
2090static void dspload_post_setup(struct hda_codec *codec)
2091{
2092 snd_printdd(KERN_INFO "---- dspload_post_setup ------");
2093
2094 /*set DSP speaker to 2.0 configuration*/
2095 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
2096 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
2097
2098 /*update write pointer*/
2099 chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
2100}
2101
Ian Minettd5c21b82012-09-20 20:29:18 -07002102/**
2103 * Download DSP from a DSP Image Fast Load structure. This structure is a
2104 * linear, non-constant sized element array of structures, each of which
2105 * contain the count of the data to be loaded, the data itself, and the
2106 * corresponding starting chip address of the starting data location.
2107 *
2108 * @codec: the HDA codec
2109 * @fls: pointer to a fast load image
2110 * @ovly: TRUE if overlay format is required
2111 * @reloc: Relocation address for loading single-segment overlays, or 0 for
2112 * no relocation
2113 * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
2114 * @router_chans: number of audio router channels to be allocated (0 means use
2115 * internal defaults; max is 32)
2116 *
2117 * Returns zero or a negative error code.
2118 */
Ian Minett01ef7db2012-09-20 20:29:16 -07002119static int dspload_image(struct hda_codec *codec,
2120 const struct dsp_image_seg *fls,
2121 bool ovly,
2122 unsigned int reloc,
2123 bool autostart,
2124 int router_chans)
2125{
2126 int status = 0;
2127 struct hda_stream_format stream_format;
2128
2129 snd_printdd(KERN_INFO "---- dspload_image begin ------");
2130 if (router_chans == 0) {
2131 if (!ovly)
2132 router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
2133 else
2134 router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
2135 }
2136
2137 stream_format.sample_rate = 48000;
2138 stream_format.number_channels = (unsigned short)router_chans;
2139
2140 while (stream_format.number_channels > 16) {
2141 stream_format.sample_rate *= 2;
2142 stream_format.number_channels /= 2;
2143 }
2144
2145 stream_format.container_size = 32;
2146 stream_format.valid_bits_per_sample = 32;
2147
2148 do {
2149 snd_printdd(KERN_INFO "Ready to program DMA");
2150 if (!ovly)
2151 status = dsp_reset(codec);
2152
2153 if (status < 0)
2154 break;
2155
2156 snd_printdd(KERN_INFO "dsp_reset() complete");
2157 status = dspxfr_image(codec, fls, reloc, &stream_format, ovly);
2158
2159 if (status < 0)
2160 break;
2161
2162 snd_printdd(KERN_INFO "dspxfr_image() complete");
2163 if (autostart && !ovly) {
2164 dspload_post_setup(codec);
2165 status = dsp_set_run_state(codec);
2166 }
2167
2168 snd_printdd(KERN_INFO "LOAD FINISHED");
2169 } while (0);
2170
2171 return status;
2172}
2173
Ian Minettc3b4eea22012-09-20 20:29:17 -07002174static const struct firmware *fw_efx;
2175
2176static int request_firmware_cached(const struct firmware **firmware_p,
2177 const char *name, struct device *device)
2178{
2179 if (*firmware_p)
2180 return 0; /* already loaded */
2181 return request_firmware(firmware_p, name, device);
2182}
2183
2184static void release_cached_firmware(void)
2185{
2186 if (fw_efx) {
2187 release_firmware(fw_efx);
2188 fw_efx = NULL;
2189 }
2190}
2191
Ian Minett01ef7db2012-09-20 20:29:16 -07002192static bool dspload_is_loaded(struct hda_codec *codec)
2193{
2194 unsigned int data = 0;
2195 int status = 0;
2196
2197 status = chipio_read(codec, 0x40004, &data);
2198 if ((status < 0) || (data != 1))
2199 return false;
2200
2201 return true;
2202}
2203
2204static bool dspload_wait_loaded(struct hda_codec *codec)
2205{
2206 int retry = 100;
2207
2208 do {
2209 msleep(20);
2210 if (dspload_is_loaded(codec)) {
2211 pr_info("ca0132 DOWNLOAD OK :-) DSP IS RUNNING.\n");
2212 return true;
2213 }
2214 } while (--retry);
2215
2216 pr_err("ca0132 DOWNLOAD FAILED!!! DSP IS NOT RUNNING.\n");
2217 return false;
2218}
2219
Ian Minett95c6e9c2011-06-15 15:35:17 -07002220/*
Ian Minett95c6e9c2011-06-15 15:35:17 -07002221 * PCM callbacks
2222 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002223static int ca0132_playback_pcm_open(struct hda_pcm_stream *hinfo,
2224 struct hda_codec *codec,
2225 struct snd_pcm_substream *substream)
2226{
2227 struct ca0132_spec *spec = codec->spec;
2228 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2229 hinfo);
2230}
2231
Ian Minett95c6e9c2011-06-15 15:35:17 -07002232static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2233 struct hda_codec *codec,
2234 unsigned int stream_tag,
2235 unsigned int format,
2236 struct snd_pcm_substream *substream)
2237{
2238 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002239 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2240 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002241}
2242
2243static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2244 struct hda_codec *codec,
2245 struct snd_pcm_substream *substream)
2246{
2247 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002248 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002249}
2250
2251/*
2252 * Digital out
2253 */
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002254static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2255 struct hda_codec *codec,
2256 struct snd_pcm_substream *substream)
2257{
2258 struct ca0132_spec *spec = codec->spec;
2259 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2260}
2261
Ian Minett95c6e9c2011-06-15 15:35:17 -07002262static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2263 struct hda_codec *codec,
2264 unsigned int stream_tag,
2265 unsigned int format,
2266 struct snd_pcm_substream *substream)
2267{
2268 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002269 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2270 stream_tag, format, substream);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002271}
2272
2273static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2274 struct hda_codec *codec,
2275 struct snd_pcm_substream *substream)
2276{
2277 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002278 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002279}
2280
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002281static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2282 struct hda_codec *codec,
2283 struct snd_pcm_substream *substream)
Ian Minett95c6e9c2011-06-15 15:35:17 -07002284{
2285 struct ca0132_spec *spec = codec->spec;
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002286 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002287}
2288
2289/*
2290 */
2291static struct hda_pcm_stream ca0132_pcm_analog_playback = {
2292 .substreams = 1,
2293 .channels_min = 2,
2294 .channels_max = 2,
2295 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002296 .open = ca0132_playback_pcm_open,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002297 .prepare = ca0132_playback_pcm_prepare,
2298 .cleanup = ca0132_playback_pcm_cleanup
2299 },
2300};
2301
2302static struct hda_pcm_stream ca0132_pcm_analog_capture = {
2303 .substreams = 1,
2304 .channels_min = 2,
2305 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002306};
2307
2308static struct hda_pcm_stream ca0132_pcm_digital_playback = {
2309 .substreams = 1,
2310 .channels_min = 2,
2311 .channels_max = 2,
2312 .ops = {
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002313 .open = ca0132_dig_playback_pcm_open,
2314 .close = ca0132_dig_playback_pcm_close,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002315 .prepare = ca0132_dig_playback_pcm_prepare,
2316 .cleanup = ca0132_dig_playback_pcm_cleanup
2317 },
2318};
2319
2320static struct hda_pcm_stream ca0132_pcm_digital_capture = {
2321 .substreams = 1,
2322 .channels_min = 2,
2323 .channels_max = 2,
Ian Minett95c6e9c2011-06-15 15:35:17 -07002324};
2325
2326static int ca0132_build_pcms(struct hda_codec *codec)
2327{
2328 struct ca0132_spec *spec = codec->spec;
2329 struct hda_pcm *info = spec->pcm_rec;
2330
2331 codec->pcm_info = info;
2332 codec->num_pcms = 0;
2333
2334 info->name = "CA0132 Analog";
2335 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
2336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
2337 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
2338 spec->multiout.max_channels;
2339 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
2340 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
2341 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
2342 codec->num_pcms++;
2343
2344 if (!spec->dig_out && !spec->dig_in)
2345 return 0;
2346
2347 info++;
2348 info->name = "CA0132 Digital";
2349 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2350 if (spec->dig_out) {
2351 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2352 ca0132_pcm_digital_playback;
2353 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
2354 }
2355 if (spec->dig_in) {
2356 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2357 ca0132_pcm_digital_capture;
2358 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
2359 }
2360 codec->num_pcms++;
2361
2362 return 0;
2363}
2364
2365#define REG_CODEC_MUTE 0x18b014
2366#define REG_CODEC_HP_VOL_L 0x18b070
2367#define REG_CODEC_HP_VOL_R 0x18b074
2368
2369static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2371{
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 struct ca0132_spec *spec = codec->spec;
2374 long *valp = ucontrol->value.integer.value;
2375
2376 *valp = spec->curr_hp_switch;
2377 return 0;
2378}
2379
2380static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 struct ca0132_spec *spec = codec->spec;
2385 long *valp = ucontrol->value.integer.value;
2386 unsigned int data;
2387 int err;
2388
2389 /* any change? */
2390 if (spec->curr_hp_switch == *valp)
2391 return 0;
2392
2393 snd_hda_power_up(codec);
2394
2395 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2396 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002397 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002398
2399 /* *valp 0 is mute, 1 is unmute */
2400 data = (data & 0x7f) | (*valp ? 0 : 0x80);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002401 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002402 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002403 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002404
2405 spec->curr_hp_switch = *valp;
2406
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002407 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002408 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002409 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002410}
2411
2412static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
2414{
2415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2416 struct ca0132_spec *spec = codec->spec;
2417 long *valp = ucontrol->value.integer.value;
2418
2419 *valp = spec->curr_speaker_switch;
2420 return 0;
2421}
2422
2423static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2425{
2426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2427 struct ca0132_spec *spec = codec->spec;
2428 long *valp = ucontrol->value.integer.value;
2429 unsigned int data;
2430 int err;
2431
2432 /* any change? */
2433 if (spec->curr_speaker_switch == *valp)
2434 return 0;
2435
2436 snd_hda_power_up(codec);
2437
2438 err = chipio_read(codec, REG_CODEC_MUTE, &data);
2439 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002440 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002441
2442 /* *valp 0 is mute, 1 is unmute */
2443 data = (data & 0xef) | (*valp ? 0 : 0x10);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002444 err = chipio_write(codec, REG_CODEC_MUTE, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002445 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002446 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002447
2448 spec->curr_speaker_switch = *valp;
2449
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002450 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002451 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002452 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002453}
2454
2455static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
2458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2459 struct ca0132_spec *spec = codec->spec;
2460 long *valp = ucontrol->value.integer.value;
2461
2462 *valp++ = spec->curr_hp_volume[0];
2463 *valp = spec->curr_hp_volume[1];
2464 return 0;
2465}
2466
2467static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
2468 struct snd_ctl_elem_value *ucontrol)
2469{
2470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2471 struct ca0132_spec *spec = codec->spec;
2472 long *valp = ucontrol->value.integer.value;
2473 long left_vol, right_vol;
2474 unsigned int data;
2475 int val;
2476 int err;
2477
2478 left_vol = *valp++;
2479 right_vol = *valp;
2480
2481 /* any change? */
2482 if ((spec->curr_hp_volume[0] == left_vol) &&
2483 (spec->curr_hp_volume[1] == right_vol))
2484 return 0;
2485
2486 snd_hda_power_up(codec);
2487
2488 err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
2489 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002490 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002491
2492 val = 31 - left_vol;
2493 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002494 err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002495 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002496 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002497
2498 val = 31 - right_vol;
2499 data = (data & 0xe0) | val;
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002500 err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002501 if (err < 0)
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002502 goto exit;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002503
2504 spec->curr_hp_volume[0] = left_vol;
2505 spec->curr_hp_volume[1] = right_vol;
2506
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002507 exit:
Ian Minett95c6e9c2011-06-15 15:35:17 -07002508 snd_hda_power_down(codec);
Takashi Iwaib97f6bf2012-02-07 11:00:53 +01002509 return err < 0 ? err : 1;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002510}
2511
2512static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
2513{
2514 struct snd_kcontrol_new knew =
2515 HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
2516 nid, 1, 0, HDA_OUTPUT);
2517 knew.get = ca0132_hp_switch_get;
2518 knew.put = ca0132_hp_switch_put;
2519 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2520}
2521
2522static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
2523{
2524 struct snd_kcontrol_new knew =
2525 HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
2526 nid, 3, 0, HDA_OUTPUT);
2527 knew.get = ca0132_hp_volume_get;
2528 knew.put = ca0132_hp_volume_put;
2529 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2530}
2531
2532static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
2533{
2534 struct snd_kcontrol_new knew =
2535 HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
2536 nid, 1, 0, HDA_OUTPUT);
2537 knew.get = ca0132_speaker_switch_get;
2538 knew.put = ca0132_speaker_switch_put;
2539 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
2540}
2541
2542static void ca0132_fix_hp_caps(struct hda_codec *codec)
2543{
2544 struct ca0132_spec *spec = codec->spec;
2545 struct auto_pin_cfg *cfg = &spec->autocfg;
2546 unsigned int caps;
2547
2548 /* set mute-capable, 1db step, 32 steps, ofs 6 */
2549 caps = 0x80031f06;
2550 snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
2551}
2552
2553static int ca0132_build_controls(struct hda_codec *codec)
2554{
2555 struct ca0132_spec *spec = codec->spec;
2556 struct auto_pin_cfg *cfg = &spec->autocfg;
2557 int i, err;
2558
2559 if (spec->multiout.num_dacs) {
2560 err = add_speaker_switch(codec, spec->out_pins[0]);
2561 if (err < 0)
2562 return err;
2563 }
2564
2565 if (cfg->hp_outs) {
2566 ca0132_fix_hp_caps(codec);
2567 err = add_hp_switch(codec, cfg->hp_pins[0]);
2568 if (err < 0)
2569 return err;
2570 err = add_hp_volume(codec, cfg->hp_pins[0]);
2571 if (err < 0)
2572 return err;
2573 }
2574
2575 for (i = 0; i < spec->num_inputs; i++) {
2576 const char *label = spec->input_labels[i];
2577
2578 err = add_in_switch(codec, spec->adcs[i], label);
2579 if (err < 0)
2580 return err;
2581 err = add_in_volume(codec, spec->adcs[i], label);
2582 if (err < 0)
2583 return err;
2584 if (cfg->inputs[i].type == AUTO_PIN_MIC) {
2585 /* add Mic-Boost */
2586 err = add_in_mono_volume(codec, spec->input_pins[i],
2587 "Mic Boost", 1);
2588 if (err < 0)
2589 return err;
2590 }
2591 }
2592
2593 if (spec->dig_out) {
Takashi Iwaiefb9f462011-06-21 07:44:51 +02002594 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
2595 spec->dig_out);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002596 if (err < 0)
2597 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002598 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
Ian Minett95c6e9c2011-06-15 15:35:17 -07002599 if (err < 0)
2600 return err;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002601 /* spec->multiout.share_spdif = 1; */
Ian Minett95c6e9c2011-06-15 15:35:17 -07002602 }
2603
2604 if (spec->dig_in) {
2605 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
2606 if (err < 0)
2607 return err;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002608 }
2609 return 0;
2610}
2611
2612
2613static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
2614{
2615 /* Set Creative extension */
2616 snd_printdd("SET CREATIVE EXTENSION\n");
2617 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2618 VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
2619 enable);
2620 msleep(20);
2621}
2622
2623
2624static void ca0132_config(struct hda_codec *codec)
2625{
2626 struct ca0132_spec *spec = codec->spec;
2627 struct auto_pin_cfg *cfg = &spec->autocfg;
2628
Takashi Iwai27ebeb02012-08-08 17:20:18 +02002629 codec->pcm_format_first = 1;
2630 codec->no_sticky_stream = 1;
2631
Ian Minett95c6e9c2011-06-15 15:35:17 -07002632 /* line-outs */
2633 cfg->line_outs = 1;
2634 cfg->line_out_pins[0] = 0x0b; /* front */
2635 cfg->line_out_type = AUTO_PIN_LINE_OUT;
2636
2637 spec->dacs[0] = 0x02;
2638 spec->out_pins[0] = 0x0b;
2639 spec->multiout.dac_nids = spec->dacs;
2640 spec->multiout.num_dacs = 1;
2641 spec->multiout.max_channels = 2;
2642
2643 /* headphone */
2644 cfg->hp_outs = 1;
2645 cfg->hp_pins[0] = 0x0f;
2646
2647 spec->hp_dac = 0;
2648 spec->multiout.hp_nid = 0;
2649
2650 /* inputs */
2651 cfg->num_inputs = 2; /* Mic-in and line-in */
2652 cfg->inputs[0].pin = 0x12;
2653 cfg->inputs[0].type = AUTO_PIN_MIC;
2654 cfg->inputs[1].pin = 0x11;
2655 cfg->inputs[1].type = AUTO_PIN_LINE_IN;
2656
2657 /* Mic-in */
2658 spec->input_pins[0] = 0x12;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002659 spec->input_labels[0] = "Mic";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002660 spec->adcs[0] = 0x07;
2661
2662 /* Line-In */
2663 spec->input_pins[1] = 0x11;
Takashi Iwai55cf87f2012-08-08 17:15:55 +02002664 spec->input_labels[1] = "Line";
Ian Minett95c6e9c2011-06-15 15:35:17 -07002665 spec->adcs[1] = 0x08;
2666 spec->num_inputs = 2;
Takashi Iwai8e13fc12012-08-08 17:26:54 +02002667
2668 /* SPDIF I/O */
2669 spec->dig_out = 0x05;
2670 spec->multiout.dig_out_nid = spec->dig_out;
2671 cfg->dig_out_pins[0] = 0x0c;
2672 cfg->dig_outs = 1;
2673 cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
2674 spec->dig_in = 0x09;
2675 cfg->dig_in_pin = 0x0e;
2676 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
Ian Minett95c6e9c2011-06-15 15:35:17 -07002677}
2678
2679static void ca0132_init_chip(struct hda_codec *codec)
2680{
2681 struct ca0132_spec *spec = codec->spec;
2682
2683 mutex_init(&spec->chipio_mutex);
2684}
2685
2686static void ca0132_exit_chip(struct hda_codec *codec)
2687{
2688 /* put any chip cleanup stuffs here. */
2689}
2690
Ian Minett01ef7db2012-09-20 20:29:16 -07002691static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
2692{
2693 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
2694 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
2695 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
2696 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
2697 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
2698 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
2699
2700 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
2701 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
2702 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
2703}
2704
2705static bool ca0132_download_dsp_images(struct hda_codec *codec)
2706{
2707 bool dsp_loaded = false;
2708 const struct dsp_image_seg *dsp_os_image;
Ian Minett01ef7db2012-09-20 20:29:16 -07002709
Ian Minettc3b4eea22012-09-20 20:29:17 -07002710 if (request_firmware_cached(&fw_efx, EFX_FILE,
2711 codec->bus->card->dev) != 0)
Ian Minett01ef7db2012-09-20 20:29:16 -07002712 return false;
2713
Ian Minettc3b4eea22012-09-20 20:29:17 -07002714 dsp_os_image = (struct dsp_image_seg *)(fw_efx->data);
Ian Minett01ef7db2012-09-20 20:29:16 -07002715 dspload_image(codec, dsp_os_image, 0, 0, true, 0);
2716 dsp_loaded = dspload_wait_loaded(codec);
2717
Ian Minett01ef7db2012-09-20 20:29:16 -07002718 return dsp_loaded;
2719}
2720
2721static void ca0132_download_dsp(struct hda_codec *codec)
2722{
2723 struct ca0132_spec *spec = codec->spec;
2724
2725 spec->dsp_state = DSP_DOWNLOAD_INIT;
2726
2727 if (spec->dsp_state == DSP_DOWNLOAD_INIT) {
2728 chipio_enable_clocks(codec);
2729 spec->dsp_state = DSP_DOWNLOADING;
2730 if (!ca0132_download_dsp_images(codec))
2731 spec->dsp_state = DSP_DOWNLOAD_FAILED;
2732 else
2733 spec->dsp_state = DSP_DOWNLOADED;
2734 }
2735
2736 if (spec->dsp_state == DSP_DOWNLOADED)
2737 ca0132_set_dsp_msr(codec, true);
2738}
2739
Ian Minett95c6e9c2011-06-15 15:35:17 -07002740static int ca0132_init(struct hda_codec *codec)
2741{
2742 struct ca0132_spec *spec = codec->spec;
2743 struct auto_pin_cfg *cfg = &spec->autocfg;
2744 int i;
2745
Ian Minett01ef7db2012-09-20 20:29:16 -07002746#ifdef CONFIG_SND_HDA_DSP_LOADER
2747 ca0132_download_dsp(codec);
2748#endif
2749
Ian Minett95c6e9c2011-06-15 15:35:17 -07002750 for (i = 0; i < spec->multiout.num_dacs; i++) {
2751 init_output(codec, spec->out_pins[i],
2752 spec->multiout.dac_nids[i]);
2753 }
2754 init_output(codec, cfg->hp_pins[0], spec->hp_dac);
2755 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
2756
2757 for (i = 0; i < spec->num_inputs; i++)
2758 init_input(codec, spec->input_pins[i], spec->adcs[i]);
2759
2760 init_input(codec, cfg->dig_in_pin, spec->dig_in);
2761
2762 ca0132_set_ct_ext(codec, 1);
2763
2764 return 0;
2765}
2766
2767
2768static void ca0132_free(struct hda_codec *codec)
2769{
2770 ca0132_set_ct_ext(codec, 0);
2771 ca0132_exit_chip(codec);
2772 kfree(codec->spec);
2773}
2774
2775static struct hda_codec_ops ca0132_patch_ops = {
2776 .build_controls = ca0132_build_controls,
2777 .build_pcms = ca0132_build_pcms,
2778 .init = ca0132_init,
2779 .free = ca0132_free,
2780};
2781
2782
2783
2784static int patch_ca0132(struct hda_codec *codec)
2785{
2786 struct ca0132_spec *spec;
2787
2788 snd_printdd("patch_ca0132\n");
2789
2790 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2791 if (!spec)
2792 return -ENOMEM;
2793 codec->spec = spec;
2794
2795 ca0132_init_chip(codec);
2796
2797 ca0132_config(codec);
2798
2799 codec->patch_ops = ca0132_patch_ops;
2800
2801 return 0;
2802}
2803
2804/*
2805 * patch entries
2806 */
2807static struct hda_codec_preset snd_hda_preset_ca0132[] = {
2808 { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
2809 {} /* terminator */
2810};
2811
2812MODULE_ALIAS("snd-hda-codec-id:11020011");
2813
2814MODULE_LICENSE("GPL");
2815MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
2816
2817static struct hda_codec_preset_list ca0132_list = {
2818 .preset = snd_hda_preset_ca0132,
2819 .owner = THIS_MODULE,
2820};
2821
2822static int __init patch_ca0132_init(void)
2823{
2824 return snd_hda_add_codec_preset(&ca0132_list);
2825}
2826
2827static void __exit patch_ca0132_exit(void)
2828{
Ian Minettc3b4eea22012-09-20 20:29:17 -07002829 release_cached_firmware();
Ian Minett95c6e9c2011-06-15 15:35:17 -07002830 snd_hda_delete_codec_preset(&ca0132_list);
2831}
2832
2833module_init(patch_ca0132_init)
2834module_exit(patch_ca0132_exit)