blob: 0d668f471620cc81283142cc16cc0d1159cb84f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Sound Core PDAudioCF soundcards
3 *
4 * PCM part
5 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02006 * Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/delay.h>
25#include <sound/core.h>
26#include <sound/asoundef.h>
27#include "pdaudiocf.h"
28
29
30/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * clear the SRAM contents
32 */
Takashi Iwaidb131542005-11-17 15:07:38 +010033static int pdacf_pcm_clear_sram(struct snd_pdacf *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 int max_loop = 64 * 1024;
36
37 while (inw(chip->port + PDAUDIOCF_REG_RDP) != inw(chip->port + PDAUDIOCF_REG_WDP)) {
38 if (max_loop-- < 0)
39 return -EIO;
40 inw(chip->port + PDAUDIOCF_REG_MD);
41 }
42 return 0;
43}
44
45/*
46 * pdacf_pcm_trigger - trigger callback for capture
47 */
Takashi Iwaidb131542005-11-17 15:07:38 +010048static int pdacf_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Takashi Iwaidb131542005-11-17 15:07:38 +010050 struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
51 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int inc, ret = 0, rate;
53 unsigned short mask, val, tmp;
54
55 if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
56 return -EBUSY;
57
58 switch (cmd) {
59 case SNDRV_PCM_TRIGGER_START:
60 chip->pcm_hwptr = 0;
61 chip->pcm_tdone = 0;
62 /* fall thru */
63 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
64 case SNDRV_PCM_TRIGGER_RESUME:
65 mask = 0;
66 val = PDAUDIOCF_RECORD;
67 inc = 1;
68 rate = snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_STAT|AK4117_CHECK_NO_RATE);
69 break;
70 case SNDRV_PCM_TRIGGER_STOP:
71 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
72 case SNDRV_PCM_TRIGGER_SUSPEND:
73 mask = PDAUDIOCF_RECORD;
74 val = 0;
75 inc = -1;
76 rate = 0;
77 break;
78 default:
79 return -EINVAL;
80 }
81 spin_lock(&chip->reg_lock);
82 chip->pcm_running += inc;
83 tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
84 if (chip->pcm_running) {
85 if ((chip->ak4117->rcs0 & AK4117_UNLCK) || runtime->rate != rate) {
86 chip->pcm_running -= inc;
87 ret = -EIO;
88 goto __end;
89 }
90 }
91 tmp &= ~mask;
92 tmp |= val;
93 pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, tmp);
94 __end:
95 spin_unlock(&chip->reg_lock);
96 snd_ak4117_check_rate_and_errors(chip->ak4117, AK4117_CHECK_NO_RATE);
97 return ret;
98}
99
100/*
101 * pdacf_pcm_hw_params - hw_params callback for playback and capture
102 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100103static int pdacf_pcm_hw_params(struct snd_pcm_substream *subs,
104 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Clemens Ladischd20fb5d2009-12-18 09:29:49 +0100106 return snd_pcm_lib_alloc_vmalloc_32_buffer
107 (subs, params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/*
111 * pdacf_pcm_hw_free - hw_free callback for playback and capture
112 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100113static int pdacf_pcm_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Clemens Ladischd20fb5d2009-12-18 09:29:49 +0100115 return snd_pcm_lib_free_vmalloc_buffer(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
119 * pdacf_pcm_prepare - prepare callback for playback and capture
120 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100121static int pdacf_pcm_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwaidb131542005-11-17 15:07:38 +0100123 struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
124 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u16 val, nval, aval;
126
127 if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
128 return -EBUSY;
129
130 chip->pcm_channels = runtime->channels;
131
132 chip->pcm_little = snd_pcm_format_little_endian(runtime->format) > 0;
133#ifdef SNDRV_LITTLE_ENDIAN
134 chip->pcm_swab = snd_pcm_format_big_endian(runtime->format) > 0;
135#else
136 chip->pcm_swab = chip->pcm_little;
137#endif
138
139 if (snd_pcm_format_unsigned(runtime->format))
140 chip->pcm_xor = 0x80008000;
141
142 if (pdacf_pcm_clear_sram(chip) < 0)
143 return -EIO;
144
145 val = nval = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
146 nval &= ~(PDAUDIOCF_DATAFMT0|PDAUDIOCF_DATAFMT1);
147 switch (runtime->format) {
148 case SNDRV_PCM_FORMAT_S16_LE:
149 case SNDRV_PCM_FORMAT_S16_BE:
150 break;
151 default: /* 24-bit */
152 nval |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1;
153 break;
154 }
155 aval = 0;
156 chip->pcm_sample = 4;
157 switch (runtime->format) {
158 case SNDRV_PCM_FORMAT_S16_LE:
159 case SNDRV_PCM_FORMAT_S16_BE:
160 aval = AK4117_DIF_16R;
161 chip->pcm_frame = 2;
162 chip->pcm_sample = 2;
163 break;
164 case SNDRV_PCM_FORMAT_S24_3LE:
165 case SNDRV_PCM_FORMAT_S24_3BE:
166 chip->pcm_sample = 3;
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100167 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 default: /* 24-bit */
169 aval = AK4117_DIF_24R;
170 chip->pcm_frame = 3;
171 chip->pcm_xor &= 0xffff0000;
172 break;
173 }
174
175 if (val != nval) {
176 snd_ak4117_reg_write(chip->ak4117, AK4117_REG_IO, AK4117_DIF2|AK4117_DIF1|AK4117_DIF0, aval);
177 pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, nval);
178 }
179
180 val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);
181 val &= ~(PDAUDIOCF_IRQLVLEN1);
182 val |= PDAUDIOCF_IRQLVLEN0;
183 pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);
184
185 chip->pcm_size = runtime->buffer_size;
186 chip->pcm_period = runtime->period_size;
187 chip->pcm_area = runtime->dma_area;
188
189 return 0;
190}
191
192
193/*
194 * capture hw information
195 */
196
Takashi Iwaidb131542005-11-17 15:07:38 +0100197static struct snd_pcm_hardware pdacf_pcm_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
199 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
Takashi Iwai2008f132009-04-28 12:25:59 +0200200 SNDRV_PCM_INFO_MMAP_VALID |
201 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
203 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
204 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
205 .rates = SNDRV_PCM_RATE_32000 |
206 SNDRV_PCM_RATE_44100 |
207 SNDRV_PCM_RATE_48000 |
208 SNDRV_PCM_RATE_88200 |
209 SNDRV_PCM_RATE_96000 |
210 SNDRV_PCM_RATE_176400 |
211 SNDRV_PCM_RATE_192000,
212 .rate_min = 32000,
213 .rate_max = 192000,
214 .channels_min = 1,
215 .channels_max = 2,
216 .buffer_bytes_max = (512*1024),
217 .period_bytes_min = 8*1024,
218 .period_bytes_max = (64*1024),
219 .periods_min = 2,
220 .periods_max = 128,
221 .fifo_size = 0,
222};
223
224
225/*
226 * pdacf_pcm_capture_open - open callback for capture
227 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100228static int pdacf_pcm_capture_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Takashi Iwaidb131542005-11-17 15:07:38 +0100230 struct snd_pcm_runtime *runtime = subs->runtime;
231 struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (chip->chip_status & PDAUDIOCF_STAT_IS_STALE)
234 return -EBUSY;
235
236 runtime->hw = pdacf_pcm_capture_hw;
237 runtime->private_data = chip;
238 chip->pcm_substream = subs;
239
240 return 0;
241}
242
243/*
244 * pdacf_pcm_capture_close - close callback for capture
245 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100246static int pdacf_pcm_capture_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Takashi Iwaidb131542005-11-17 15:07:38 +0100248 struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (!chip)
251 return -EINVAL;
252 pdacf_reinit(chip, 0);
253 chip->pcm_substream = NULL;
254 return 0;
255}
256
257
258/*
259 * pdacf_pcm_capture_pointer - pointer callback for capture
260 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100261static snd_pcm_uframes_t pdacf_pcm_capture_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwaidb131542005-11-17 15:07:38 +0100263 struct snd_pdacf *chip = snd_pcm_substream_chip(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return chip->pcm_hwptr;
265}
266
267/*
268 * operators for PCM capture
269 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100270static struct snd_pcm_ops pdacf_pcm_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .open = pdacf_pcm_capture_open,
272 .close = pdacf_pcm_capture_close,
273 .ioctl = snd_pcm_lib_ioctl,
274 .hw_params = pdacf_pcm_hw_params,
275 .hw_free = pdacf_pcm_hw_free,
276 .prepare = pdacf_pcm_prepare,
277 .trigger = pdacf_pcm_trigger,
278 .pointer = pdacf_pcm_capture_pointer,
Clemens Ladischd20fb5d2009-12-18 09:29:49 +0100279 .page = snd_pcm_lib_get_vmalloc_page,
Takashi Iwaic32d9772010-01-18 14:58:57 +0100280 .mmap = snd_pcm_lib_mmap_vmalloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
283
284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * snd_pdacf_pcm_new - create and initialize a pcm
286 */
Takashi Iwaidb131542005-11-17 15:07:38 +0100287int snd_pdacf_pcm_new(struct snd_pdacf *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Takashi Iwaidb131542005-11-17 15:07:38 +0100289 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int err;
291
292 err = snd_pcm_new(chip->card, "PDAudioCF", 0, 0, 1, &pcm);
293 if (err < 0)
294 return err;
295
296 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pdacf_pcm_capture_ops);
297
298 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 pcm->info_flags = 0;
300 strcpy(pcm->name, chip->card->shortname);
301 chip->pcm = pcm;
302
303 err = snd_ak4117_build(chip->ak4117, pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
304 if (err < 0)
305 return err;
306
307 return 0;
308}