Merge tag 'asoc-3.8' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound...
[pandora-kernel.git] / sound / pci / pcxhr / pcxhr_hwdep.c
1 /*
2  * Driver for Digigram pcxhr compatible soundcards
3  *
4  * hwdep device manager
5  *
6  * Copyright (c) 2004 by Digigram <alsa@digigram.com>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/interrupt.h>
24 #include <linux/vmalloc.h>
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
27 #include <linux/module.h>
28 #include <asm/io.h>
29 #include <sound/core.h>
30 #include <sound/hwdep.h>
31 #include "pcxhr.h"
32 #include "pcxhr_mixer.h"
33 #include "pcxhr_hwdep.h"
34 #include "pcxhr_core.h"
35 #include "pcxhr_mix22.h"
36
37
38 static int pcxhr_sub_init(struct pcxhr_mgr *mgr);
39 /*
40  * get basic information and init pcxhr card
41  */
42 static int pcxhr_init_board(struct pcxhr_mgr *mgr)
43 {
44         int err;
45         struct pcxhr_rmh rmh;
46         int card_streams;
47
48         /* calc the number of all streams used */
49         if (mgr->mono_capture)
50                 card_streams = mgr->capture_chips * 2;
51         else
52                 card_streams = mgr->capture_chips;
53         card_streams += mgr->playback_chips * PCXHR_PLAYBACK_STREAMS;
54
55         /* enable interrupts */
56         pcxhr_enable_dsp(mgr);
57
58         pcxhr_init_rmh(&rmh, CMD_SUPPORTED);
59         err = pcxhr_send_msg(mgr, &rmh);
60         if (err)
61                 return err;
62         /* test 4, 8 or 12 phys out */
63         if ((rmh.stat[0] & MASK_FIRST_FIELD) < mgr->playback_chips * 2)
64                 return -EINVAL;
65         /* test 4, 8 or 2 phys in */
66         if (((rmh.stat[0] >> (2 * FIELD_SIZE)) & MASK_FIRST_FIELD) <
67             mgr->capture_chips * 2)
68                 return -EINVAL;
69         /* test max nb substream per board */
70         if ((rmh.stat[1] & 0x5F) < card_streams)
71                 return -EINVAL;
72         /* test max nb substream per pipe */
73         if (((rmh.stat[1] >> 7) & 0x5F) < PCXHR_PLAYBACK_STREAMS)
74                 return -EINVAL;
75         snd_printdd("supported formats : playback=%x capture=%x\n",
76                     rmh.stat[2], rmh.stat[3]);
77
78         pcxhr_init_rmh(&rmh, CMD_VERSION);
79         /* firmware num for DSP */
80         rmh.cmd[0] |= mgr->firmware_num;
81         /* transfer granularity in samples (should be multiple of 48) */
82         rmh.cmd[1] = (1<<23) + mgr->granularity;
83         rmh.cmd_len = 2;
84         err = pcxhr_send_msg(mgr, &rmh);
85         if (err)
86                 return err;
87         snd_printdd("PCXHR DSP version is %d.%d.%d\n", (rmh.stat[0]>>16)&0xff,
88                     (rmh.stat[0]>>8)&0xff, rmh.stat[0]&0xff);
89         mgr->dsp_version = rmh.stat[0];
90
91         if (mgr->is_hr_stereo)
92                 err = hr222_sub_init(mgr);
93         else
94                 err = pcxhr_sub_init(mgr);
95         return err;
96 }
97
98 static int pcxhr_sub_init(struct pcxhr_mgr *mgr)
99 {
100         int err;
101         struct pcxhr_rmh rmh;
102
103         /* get options */
104         pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
105         rmh.cmd[0] |= IO_NUM_REG_STATUS;
106         rmh.cmd[1]  = REG_STATUS_OPTIONS;
107         rmh.cmd_len = 2;
108         err = pcxhr_send_msg(mgr, &rmh);
109         if (err)
110                 return err;
111
112         if ((rmh.stat[1] & REG_STATUS_OPT_DAUGHTER_MASK) ==
113             REG_STATUS_OPT_ANALOG_BOARD)
114                 mgr->board_has_analog = 1;      /* analog addon board found */
115
116         /* unmute inputs */
117         err = pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS,
118                                           REG_CONT_UNMUTE_INPUTS, NULL);
119         if (err)
120                 return err;
121         /* unmute outputs (a write to IO_NUM_REG_MUTE_OUT mutes!) */
122         pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_READ);
123         rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
124         if (DSP_EXT_CMD_SET(mgr)) {
125                 rmh.cmd[1]  = 1;        /* unmute digital plugs */
126                 rmh.cmd_len = 2;
127         }
128         err = pcxhr_send_msg(mgr, &rmh);
129         return err;
130 }
131
132 void pcxhr_reset_board(struct pcxhr_mgr *mgr)
133 {
134         struct pcxhr_rmh rmh;
135
136         if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_MAIN_INDEX)) {
137                 /* mute outputs */
138             if (!mgr->is_hr_stereo) {
139                 /* a read to IO_NUM_REG_MUTE_OUT register unmutes! */
140                 pcxhr_init_rmh(&rmh, CMD_ACCESS_IO_WRITE);
141                 rmh.cmd[0] |= IO_NUM_REG_MUTE_OUT;
142                 pcxhr_send_msg(mgr, &rmh);
143                 /* mute inputs */
144                 pcxhr_write_io_num_reg_cont(mgr, REG_CONT_UNMUTE_INPUTS,
145                                             0, NULL);
146             }
147                 /* stereo cards mute with reset of dsp */
148         }
149         /* reset pcxhr dsp */
150         if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_DSP_EPRM_INDEX))
151                 pcxhr_reset_dsp(mgr);
152         /* reset second xilinx */
153         if (mgr->dsp_loaded & (1 << PCXHR_FIRMWARE_XLX_COM_INDEX)) {
154                 pcxhr_reset_xilinx_com(mgr);
155                 mgr->dsp_loaded = 1;
156         }
157         return;
158 }
159
160
161 /*
162  *  allocate a playback/capture pipe (pcmp0/pcmc0)
163  */
164 static int pcxhr_dsp_allocate_pipe(struct pcxhr_mgr *mgr,
165                                    struct pcxhr_pipe *pipe,
166                                    int is_capture, int pin)
167 {
168         int stream_count, audio_count;
169         int err;
170         struct pcxhr_rmh rmh;
171
172         if (is_capture) {
173                 stream_count = 1;
174                 if (mgr->mono_capture)
175                         audio_count = 1;
176                 else
177                         audio_count = 2;
178         } else {
179                 stream_count = PCXHR_PLAYBACK_STREAMS;
180                 audio_count = 2;        /* always stereo */
181         }
182         snd_printdd("snd_add_ref_pipe pin(%d) pcm%c0\n",
183                     pin, is_capture ? 'c' : 'p');
184         pipe->is_capture = is_capture;
185         pipe->first_audio = pin;
186         /* define pipe (P_PCM_ONLY_MASK (0x020000) is not necessary) */
187         pcxhr_init_rmh(&rmh, CMD_RES_PIPE);
188         pcxhr_set_pipe_cmd_params(&rmh, is_capture, pin,
189                                   audio_count, stream_count);
190         rmh.cmd[1] |= 0x020000; /* add P_PCM_ONLY_MASK */
191         if (DSP_EXT_CMD_SET(mgr)) {
192                 /* add channel mask to command */
193           rmh.cmd[rmh.cmd_len++] = (audio_count == 1) ? 0x01 : 0x03;
194         }
195         err = pcxhr_send_msg(mgr, &rmh);
196         if (err < 0) {
197                 snd_printk(KERN_ERR "error pipe allocation "
198                            "(CMD_RES_PIPE) err=%x!\n", err);
199                 return err;
200         }
201         pipe->status = PCXHR_PIPE_DEFINED;
202
203         return 0;
204 }
205
206 /*
207  *  free playback/capture pipe (pcmp0/pcmc0)
208  */
209 #if 0
210 static int pcxhr_dsp_free_pipe( struct pcxhr_mgr *mgr, struct pcxhr_pipe *pipe)
211 {
212         struct pcxhr_rmh rmh;
213         int capture_mask = 0;
214         int playback_mask = 0;
215         int err = 0;
216
217         if (pipe->is_capture)
218                 capture_mask  = (1 << pipe->first_audio);
219         else
220                 playback_mask = (1 << pipe->first_audio);
221
222         /* stop one pipe */
223         err = pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 0);
224         if (err < 0)
225                 snd_printk(KERN_ERR "error stopping pipe!\n");
226         /* release the pipe */
227         pcxhr_init_rmh(&rmh, CMD_FREE_PIPE);
228         pcxhr_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->first_audio,
229                                   0, 0);
230         err = pcxhr_send_msg(mgr, &rmh);
231         if (err < 0)
232                 snd_printk(KERN_ERR "error pipe release "
233                            "(CMD_FREE_PIPE) err(%x)\n", err);
234         pipe->status = PCXHR_PIPE_UNDEFINED;
235         return err;
236 }
237 #endif
238
239
240 static int pcxhr_config_pipes(struct pcxhr_mgr *mgr)
241 {
242         int err, i, j;
243         struct snd_pcxhr *chip;
244         struct pcxhr_pipe *pipe;
245
246         /* allocate the pipes on the dsp */
247         for (i = 0; i < mgr->num_cards; i++) {
248                 chip = mgr->chip[i];
249                 if (chip->nb_streams_play) {
250                         pipe = &chip->playback_pipe;
251                         err = pcxhr_dsp_allocate_pipe( mgr, pipe, 0, i*2);
252                         if (err)
253                                 return err;
254                         for(j = 0; j < chip->nb_streams_play; j++)
255                                 chip->playback_stream[j].pipe = pipe;
256                 }
257                 for (j = 0; j < chip->nb_streams_capt; j++) {
258                         pipe = &chip->capture_pipe[j];
259                         err = pcxhr_dsp_allocate_pipe(mgr, pipe, 1, i*2 + j);
260                         if (err)
261                                 return err;
262                         chip->capture_stream[j].pipe = pipe;
263                 }
264         }
265         return 0;
266 }
267
268 static int pcxhr_start_pipes(struct pcxhr_mgr *mgr)
269 {
270         int i, j;
271         struct snd_pcxhr *chip;
272         int playback_mask = 0;
273         int capture_mask = 0;
274
275         /* start all the pipes on the dsp */
276         for (i = 0; i < mgr->num_cards; i++) {
277                 chip = mgr->chip[i];
278                 if (chip->nb_streams_play)
279                         playback_mask |= 1 << chip->playback_pipe.first_audio;
280                 for (j = 0; j < chip->nb_streams_capt; j++)
281                         capture_mask |= 1 << chip->capture_pipe[j].first_audio;
282         }
283         return pcxhr_set_pipe_state(mgr, playback_mask, capture_mask, 1);
284 }
285
286
287 static int pcxhr_dsp_load(struct pcxhr_mgr *mgr, int index,
288                           const struct firmware *dsp)
289 {
290         int err, card_index;
291
292         snd_printdd("loading dsp [%d] size = %Zd\n", index, dsp->size);
293
294         switch (index) {
295         case PCXHR_FIRMWARE_XLX_INT_INDEX:
296                 pcxhr_reset_xilinx_com(mgr);
297                 return pcxhr_load_xilinx_binary(mgr, dsp, 0);
298
299         case PCXHR_FIRMWARE_XLX_COM_INDEX:
300                 pcxhr_reset_xilinx_com(mgr);
301                 return pcxhr_load_xilinx_binary(mgr, dsp, 1);
302
303         case PCXHR_FIRMWARE_DSP_EPRM_INDEX:
304                 pcxhr_reset_dsp(mgr);
305                 return pcxhr_load_eeprom_binary(mgr, dsp);
306
307         case PCXHR_FIRMWARE_DSP_BOOT_INDEX:
308                 return pcxhr_load_boot_binary(mgr, dsp);
309
310         case PCXHR_FIRMWARE_DSP_MAIN_INDEX:
311                 err = pcxhr_load_dsp_binary(mgr, dsp);
312                 if (err)
313                         return err;
314                 break;  /* continue with first init */
315         default:
316                 snd_printk(KERN_ERR "wrong file index\n");
317                 return -EFAULT;
318         } /* end of switch file index*/
319
320         /* first communication with embedded */
321         err = pcxhr_init_board(mgr);
322         if (err < 0) {
323                 snd_printk(KERN_ERR "pcxhr could not be set up\n");
324                 return err;
325         }
326         err = pcxhr_config_pipes(mgr);
327         if (err < 0) {
328                 snd_printk(KERN_ERR "pcxhr pipes could not be set up\n");
329                 return err;
330         }
331         /* create devices and mixer in accordance with HW options*/
332         for (card_index = 0; card_index < mgr->num_cards; card_index++) {
333                 struct snd_pcxhr *chip = mgr->chip[card_index];
334
335                 if ((err = pcxhr_create_pcm(chip)) < 0)
336                         return err;
337
338                 if (card_index == 0) {
339                         if ((err = pcxhr_create_mixer(chip->mgr)) < 0)
340                                 return err;
341                 }
342                 if ((err = snd_card_register(chip->card)) < 0)
343                         return err;
344         }
345         err = pcxhr_start_pipes(mgr);
346         if (err < 0) {
347                 snd_printk(KERN_ERR "pcxhr pipes could not be started\n");
348                 return err;
349         }
350         snd_printdd("pcxhr firmware downloaded and successfully set up\n");
351
352         return 0;
353 }
354
355 /*
356  * fw loader entry
357  */
358 int pcxhr_setup_firmware(struct pcxhr_mgr *mgr)
359 {
360         static char *fw_files[][5] = {
361         [0] = { "xlxint.dat", "xlxc882hr.dat",
362                 "dspe882.e56", "dspb882hr.b56", "dspd882.d56" },
363         [1] = { "xlxint.dat", "xlxc882e.dat",
364                 "dspe882.e56", "dspb882e.b56", "dspd882.d56" },
365         [2] = { "xlxint.dat", "xlxc1222hr.dat",
366                 "dspe882.e56", "dspb1222hr.b56", "dspd1222.d56" },
367         [3] = { "xlxint.dat", "xlxc1222e.dat",
368                 "dspe882.e56", "dspb1222e.b56", "dspd1222.d56" },
369         [4] = { NULL, "xlxc222.dat",
370                 "dspe924.e56", "dspb924.b56", "dspd222.d56" },
371         [5] = { NULL, "xlxc924.dat",
372                 "dspe924.e56", "dspb924.b56", "dspd222.d56" },
373         };
374         char path[32];
375
376         const struct firmware *fw_entry;
377         int i, err;
378         int fw_set = mgr->fw_file_set;
379
380         for (i = 0; i < 5; i++) {
381                 if (!fw_files[fw_set][i])
382                         continue;
383                 sprintf(path, "pcxhr/%s", fw_files[fw_set][i]);
384                 if (request_firmware(&fw_entry, path, &mgr->pci->dev)) {
385                         snd_printk(KERN_ERR "pcxhr: can't load firmware %s\n",
386                                    path);
387                         return -ENOENT;
388                 }
389                 /* fake hwdep dsp record */
390                 err = pcxhr_dsp_load(mgr, i, fw_entry);
391                 release_firmware(fw_entry);
392                 if (err < 0)
393                         return err;
394                 mgr->dsp_loaded |= 1 << i;
395         }
396         return 0;
397 }
398
399 MODULE_FIRMWARE("pcxhr/xlxint.dat");
400 MODULE_FIRMWARE("pcxhr/xlxc882hr.dat");
401 MODULE_FIRMWARE("pcxhr/xlxc882e.dat");
402 MODULE_FIRMWARE("pcxhr/dspe882.e56");
403 MODULE_FIRMWARE("pcxhr/dspb882hr.b56");
404 MODULE_FIRMWARE("pcxhr/dspb882e.b56");
405 MODULE_FIRMWARE("pcxhr/dspd882.d56");
406
407 MODULE_FIRMWARE("pcxhr/xlxc1222hr.dat");
408 MODULE_FIRMWARE("pcxhr/xlxc1222e.dat");
409 MODULE_FIRMWARE("pcxhr/dspb1222hr.b56");
410 MODULE_FIRMWARE("pcxhr/dspb1222e.b56");
411 MODULE_FIRMWARE("pcxhr/dspd1222.d56");
412
413 MODULE_FIRMWARE("pcxhr/xlxc222.dat");
414 MODULE_FIRMWARE("pcxhr/xlxc924.dat");
415 MODULE_FIRMWARE("pcxhr/dspe924.e56");
416 MODULE_FIRMWARE("pcxhr/dspb924.b56");
417 MODULE_FIRMWARE("pcxhr/dspd222.d56");