blob: 57ddf8a34178dcb5c9884c9bff61b4c9a1cf5a90 [file] [log] [blame]
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001/*
2 *
3 * Support for audio capture
4 * PCI function #1 of the cx2388x.
5 *
Trent Piepho05b27232007-08-24 01:06:34 -03006 * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02007 * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03008 * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02009 * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020010 * Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/device.h>
30#include <linux/interrupt.h>
Trent Piephof6210c92007-08-24 01:06:36 -030031#include <linux/vmalloc.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070032#include <linux/dma-mapping.h>
Trent Piepho19453bc2007-08-18 22:54:49 -030033#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070035
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020036#include <asm/delay.h>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020037#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/control.h>
41#include <sound/initval.h>
Trent Piephobbaccc02007-09-06 23:02:25 -030042#include <sound/tlv.h>
Lawrence Rust69518032011-02-06 17:46:12 -030043#include <media/wm8775.h>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020044
45#include "cx88.h"
46#include "cx88-reg.h"
47
Mauro Carvalho Chehabdb613712012-10-27 15:25:02 -030048#define dprintk(level, fmt, arg...) do { \
49 if (debug + 1 > level) \
50 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg);\
51} while(0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020052
Mauro Carvalho Chehabdb613712012-10-27 15:25:02 -030053#define dprintk_core(level, fmt, arg...) do { \
54 if (debug + 1 > level) \
55 printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg);\
56} while(0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020057
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020058/****************************************************************************
59 Data type declarations - Can be moded to a header file later
60 ****************************************************************************/
61
Laurent Pinchartfecfede2010-05-11 10:36:31 -030062struct cx88_audio_buffer {
63 unsigned int bpl;
Hans Verkuil5e7045e2014-08-29 04:11:54 -030064 struct cx88_riscmem risc;
Hans Verkuilb2c75ab2014-08-29 05:25:30 -030065 void *vaddr;
66 struct scatterlist *sglist;
67 int sglen;
68 int nr_pages;
Laurent Pinchartfecfede2010-05-11 10:36:31 -030069};
70
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020071struct cx88_audio_dev {
72 struct cx88_core *core;
73 struct cx88_dmaqueue q;
74
75 /* pci i/o */
76 struct pci_dev *pci;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020077
78 /* audio controls */
79 int irq;
80
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010081 struct snd_card *card;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020082
83 spinlock_t reg_lock;
Trent Piepho05b27232007-08-24 01:06:34 -030084 atomic_t count;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020085
86 unsigned int dma_size;
87 unsigned int period_size;
88 unsigned int num_periods;
89
Laurent Pinchartfecfede2010-05-11 10:36:31 -030090 struct cx88_audio_buffer *buf;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020091
Trent Piepho05b27232007-08-24 01:06:34 -030092 struct snd_pcm_substream *substream;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020093};
94typedef struct cx88_audio_dev snd_cx88_card_t;
95
96
97
98/****************************************************************************
99 Module global static vars
100 ****************************************************************************/
101
102static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
lawrence rust2e4e98e2010-08-25 09:50:20 -0300103static const char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Luis de Bethencourtd275d932015-09-21 13:00:41 -0300104static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200105
106module_param_array(enable, bool, NULL, 0444);
107MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
108
109module_param_array(index, int, NULL, 0444);
110MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
111
112
113/****************************************************************************
114 Module macros
115 ****************************************************************************/
116
117MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
118MODULE_AUTHOR("Ricardo Cerqueira");
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -0300119MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200120MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300121MODULE_VERSION(CX88_VERSION);
122
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200123MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
124 "{{Conexant,23882},"
125 "{{Conexant,23883}");
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200126static unsigned int debug;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200127module_param(debug,int,0644);
128MODULE_PARM_DESC(debug,"enable debug messages");
129
130/****************************************************************************
131 Module specific funtions
132 ****************************************************************************/
133
134/*
135 * BOARD Specific: Sets audio DMA
136 */
137
Mauro Carvalho Chehabbe8a82d2006-02-07 06:49:14 -0200138static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200139{
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300140 struct cx88_audio_buffer *buf = chip->buf;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200141 struct cx88_core *core=chip->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300142 const struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200143
Trent Piepho59fd8f82007-08-18 22:09:42 -0300144 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
145 cx_clear(MO_AUD_DMACNTRL, 0x11);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200146
147 /* setup fifo + format - out channel */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300148 cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200149
150 /* sets bpl size */
151 cx_write(MO_AUDD_LNGTH, buf->bpl);
152
153 /* reset counter */
Trent Piepho05b27232007-08-24 01:06:34 -0300154 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
155 atomic_set(&chip->count, 0);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200156
Trent Piepho05b27232007-08-24 01:06:34 -0300157 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
158 "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
Trent Piepho59fd8f82007-08-18 22:09:42 -0300159 chip->num_periods, buf->bpl * chip->num_periods);
160
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200161 /* Enables corresponding bits at AUD_INT_STAT */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300162 cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
163 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
164
165 /* Clean any pending interrupt bits already set */
166 cx_write(MO_AUD_INTSTAT, ~0);
167
168 /* enable audio irqs */
169 cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200170
171 /* start dma */
172 cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
173 cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
174
175 if (debug)
Trent Piepho59fd8f82007-08-18 22:09:42 -0300176 cx88_sram_channel_dump(chip->core, audio_ch);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200177
178 return 0;
179}
180
181/*
182 * BOARD Specific: Resets audio DMA
183 */
Mauro Carvalho Chehabbe8a82d2006-02-07 06:49:14 -0200184static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200185{
186 struct cx88_core *core=chip->core;
187 dprintk(1, "Stopping audio DMA\n");
188
189 /* stop dma */
190 cx_clear(MO_AUD_DMACNTRL, 0x11);
191
192 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300193 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Trent Piepho59fd8f82007-08-18 22:09:42 -0300194 cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
195 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200196
197 if (debug)
198 cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
199
200 return 0;
201}
202
Trent Piepho05b27232007-08-24 01:06:34 -0300203#define MAX_IRQ_LOOP 50
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200204
205/*
206 * BOARD Specific: IRQ dma bits
207 */
lawrence rust2e4e98e2010-08-25 09:50:20 -0300208static const char *cx88_aud_irqs[32] = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200209 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
210 NULL, /* reserved */
211 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
212 NULL, /* reserved */
213 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
214 NULL, /* reserved */
215 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
216 NULL, /* reserved */
217 "opc_err", "par_err", "rip_err", /* 16-18 */
218 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
219};
220
221/*
222 * BOARD Specific: Threats IRQ audio specific calls
223 */
224static void cx8801_aud_irq(snd_cx88_card_t *chip)
225{
226 struct cx88_core *core = chip->core;
227 u32 status, mask;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200228
229 status = cx_read(MO_AUD_INTSTAT);
230 mask = cx_read(MO_AUD_INTMSK);
Trent Piepho05b27232007-08-24 01:06:34 -0300231 if (0 == (status & mask))
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200232 return;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200233 cx_write(MO_AUD_INTSTAT, status);
234 if (debug > 1 || (status & mask & ~0xff))
235 cx88_print_irqbits(core->name, "irq aud",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -0300236 cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
237 status, mask);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200238 /* risc op code error */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300239 if (status & AUD_INT_OPC_ERR) {
Trent Piepho05b27232007-08-24 01:06:34 -0300240 printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200241 cx_clear(MO_AUD_DMACNTRL, 0x11);
242 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
243 }
Trent Piepho05b27232007-08-24 01:06:34 -0300244 if (status & AUD_INT_DN_SYNC) {
245 dprintk(1, "Downstream sync error\n");
246 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
247 return;
248 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200249 /* risc1 downstream */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300250 if (status & AUD_INT_DN_RISCI1) {
Trent Piepho05b27232007-08-24 01:06:34 -0300251 atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200252 snd_pcm_period_elapsed(chip->substream);
253 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200254 /* FIXME: Any other status should deserve a special handling? */
255}
256
257/*
258 * BOARD Specific: Handles IRQ calls
259 */
David Howells7d12e782006-10-05 14:55:46 +0100260static irqreturn_t cx8801_irq(int irq, void *dev_id)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200261{
262 snd_cx88_card_t *chip = dev_id;
263 struct cx88_core *core = chip->core;
264 u32 status;
265 int loop, handled = 0;
266
267 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300268 status = cx_read(MO_PCI_INTSTAT) &
269 (core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200270 if (0 == status)
271 goto out;
Trent Piepho05b27232007-08-24 01:06:34 -0300272 dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
273 loop, MAX_IRQ_LOOP, status);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200274 handled = 1;
275 cx_write(MO_PCI_INTSTAT, status);
276
Trent Piepho5ba862b2007-08-18 07:02:26 -0300277 if (status & core->pci_irqmask)
278 cx88_core_irq(core, status);
Trent Piepho05b27232007-08-24 01:06:34 -0300279 if (status & PCI_INT_AUDINT)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200280 cx8801_aud_irq(chip);
Trent Piepho05b27232007-08-24 01:06:34 -0300281 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200282
283 if (MAX_IRQ_LOOP == loop) {
Trent Piepho05b27232007-08-24 01:06:34 -0300284 printk(KERN_ERR
285 "%s/1: IRQ loop detected, disabling interrupts\n",
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200286 core->name);
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300287 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200288 }
289
290 out:
291 return IRQ_RETVAL(handled);
292}
293
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300294static int cx88_alsa_dma_init(struct cx88_audio_dev *chip, int nr_pages)
295{
296 struct cx88_audio_buffer *buf = chip->buf;
297 struct page *pg;
298 int i;
299
300 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
301 if (NULL == buf->vaddr) {
302 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
303 return -ENOMEM;
304 }
305
306 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
307 (unsigned long)buf->vaddr,
308 nr_pages << PAGE_SHIFT);
309
310 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
311 buf->nr_pages = nr_pages;
312
313 buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
314 if (NULL == buf->sglist)
315 goto vzalloc_err;
316
317 sg_init_table(buf->sglist, buf->nr_pages);
318 for (i = 0; i < buf->nr_pages; i++) {
319 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
320 if (NULL == pg)
321 goto vmalloc_to_page_err;
322 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
323 }
324 return 0;
325
326vmalloc_to_page_err:
327 vfree(buf->sglist);
328 buf->sglist = NULL;
329vzalloc_err:
330 vfree(buf->vaddr);
331 buf->vaddr = NULL;
332 return -ENOMEM;
333}
334
335static int cx88_alsa_dma_map(struct cx88_audio_dev *dev)
336{
337 struct cx88_audio_buffer *buf = dev->buf;
338
339 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
340 buf->nr_pages, PCI_DMA_FROMDEVICE);
341
342 if (0 == buf->sglen) {
343 pr_warn("%s: cx88_alsa_map_sg failed\n", __func__);
344 return -ENOMEM;
345 }
346 return 0;
347}
348
349static int cx88_alsa_dma_unmap(struct cx88_audio_dev *dev)
350{
351 struct cx88_audio_buffer *buf = dev->buf;
352
353 if (!buf->sglen)
354 return 0;
355
356 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
357 buf->sglen = 0;
358 return 0;
359}
360
361static int cx88_alsa_dma_free(struct cx88_audio_buffer *buf)
362{
363 vfree(buf->sglist);
364 buf->sglist = NULL;
365 vfree(buf->vaddr);
366 buf->vaddr = NULL;
367 return 0;
368}
369
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200370
371static int dsp_buffer_free(snd_cx88_card_t *chip)
372{
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300373 struct cx88_riscmem *risc = &chip->buf->risc;
374
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200375 BUG_ON(!chip->dma_size);
376
377 dprintk(2,"Freeing buffer\n");
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300378 cx88_alsa_dma_unmap(chip);
379 cx88_alsa_dma_free(chip->buf);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300380 if (risc->cpu)
381 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200382 kfree(chip->buf);
383
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300384 chip->buf = NULL;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200385
Trent Piepho05b27232007-08-24 01:06:34 -0300386 return 0;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200387}
388
389/****************************************************************************
390 ALSA PCM Interface
391 ****************************************************************************/
392
393/*
394 * Digital hardware definition
395 */
Trent Piepho05b27232007-08-24 01:06:34 -0300396#define DEFAULT_FIFO_SIZE 4096
lawrence rust2e4e98e2010-08-25 09:50:20 -0300397static const struct snd_pcm_hardware snd_cx88_digital_hw = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200398 .info = SNDRV_PCM_INFO_MMAP |
399 SNDRV_PCM_INFO_INTERLEAVED |
400 SNDRV_PCM_INFO_BLOCK_TRANSFER |
401 SNDRV_PCM_INFO_MMAP_VALID,
402 .formats = SNDRV_PCM_FMTBIT_S16_LE,
403
404 .rates = SNDRV_PCM_RATE_48000,
405 .rate_min = 48000,
406 .rate_max = 48000,
Trent Piepho5a5b3b52007-08-18 21:01:40 -0300407 .channels_min = 2,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200408 .channels_max = 2,
Trent Piepho05b27232007-08-24 01:06:34 -0300409 /* Analog audio output will be full of clicks and pops if there
410 are not exactly four lines in the SRAM FIFO buffer. */
411 .period_bytes_min = DEFAULT_FIFO_SIZE/4,
412 .period_bytes_max = DEFAULT_FIFO_SIZE/4,
413 .periods_min = 1,
414 .periods_max = 1024,
415 .buffer_bytes_max = (1024*1024),
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200416};
417
418/*
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200419 * audio pcm capture open callback
420 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100421static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200422{
423 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100424 struct snd_pcm_runtime *runtime = substream->runtime;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200425 int err;
426
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300427 if (!chip) {
428 printk(KERN_ERR "BUG: cx88 can't find device struct."
429 " Can't proceed with open\n");
430 return -ENODEV;
431 }
432
Trent Piepho05b27232007-08-24 01:06:34 -0300433 err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200434 if (err < 0)
435 goto _error;
436
437 chip->substream = substream;
438
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200439 runtime->hw = snd_cx88_digital_hw;
440
Trent Piepho05b27232007-08-24 01:06:34 -0300441 if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
442 unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
443 bpl &= ~7; /* must be multiple of 8 */
444 runtime->hw.period_bytes_min = bpl;
445 runtime->hw.period_bytes_max = bpl;
446 }
447
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200448 return 0;
449_error:
450 dprintk(1,"Error opening PCM!\n");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200451 return err;
452}
453
454/*
455 * audio close callback
456 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100457static int snd_cx88_close(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200458{
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200459 return 0;
460}
461
462/*
463 * hw_params callback
464 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100465static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
466 struct snd_pcm_hw_params * hw_params)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200467{
468 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300469
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300470 struct cx88_audio_buffer *buf;
Trent Piepho05b27232007-08-24 01:06:34 -0300471 int ret;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200472
473 if (substream->runtime->dma_area) {
474 dsp_buffer_free(chip);
475 substream->runtime->dma_area = NULL;
476 }
477
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200478 chip->period_size = params_period_bytes(hw_params);
479 chip->num_periods = params_periods(hw_params);
480 chip->dma_size = chip->period_size * params_periods(hw_params);
481
482 BUG_ON(!chip->dma_size);
Trent Piepho05b27232007-08-24 01:06:34 -0300483 BUG_ON(chip->num_periods & (chip->num_periods-1));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200484
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300485 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200486 if (NULL == buf)
487 return -ENOMEM;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200488
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300489 chip->buf = buf;
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300490 buf->bpl = chip->period_size;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200491
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300492 ret = cx88_alsa_dma_init(chip,
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300493 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
Trent Piepho05b27232007-08-24 01:06:34 -0300494 if (ret < 0)
495 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200496
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300497 ret = cx88_alsa_dma_map(chip);
Trent Piepho05b27232007-08-24 01:06:34 -0300498 if (ret < 0)
499 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200500
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300501 ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300502 chip->period_size, chip->num_periods, 1);
Trent Piepho05b27232007-08-24 01:06:34 -0300503 if (ret < 0)
504 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200505
Trent Piepho05b27232007-08-24 01:06:34 -0300506 /* Loop back to start of program */
507 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200508 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
509
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300510 substream->runtime->dma_area = chip->buf->vaddr;
Trent Piephof6210c92007-08-24 01:06:36 -0300511 substream->runtime->dma_bytes = chip->dma_size;
512 substream->runtime->dma_addr = 0;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200513 return 0;
Trent Piepho05b27232007-08-24 01:06:34 -0300514
515error:
516 kfree(buf);
517 return ret;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200518}
519
520/*
521 * hw free callback
522 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100523static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200524{
525
526 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
527
528 if (substream->runtime->dma_area) {
529 dsp_buffer_free(chip);
530 substream->runtime->dma_area = NULL;
531 }
532
533 return 0;
534}
535
536/*
537 * prepare callback
538 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100539static int snd_cx88_prepare(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200540{
541 return 0;
542}
543
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200544/*
545 * trigger callback
546 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100547static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200548{
549 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
550 int err;
551
Trent Piepho05b27232007-08-24 01:06:34 -0300552 /* Local interrupts are already disabled by ALSA */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200553 spin_lock(&chip->reg_lock);
554
555 switch (cmd) {
556 case SNDRV_PCM_TRIGGER_START:
557 err=_cx88_start_audio_dma(chip);
558 break;
559 case SNDRV_PCM_TRIGGER_STOP:
560 err=_cx88_stop_audio_dma(chip);
561 break;
562 default:
563 err=-EINVAL;
564 break;
565 }
566
567 spin_unlock(&chip->reg_lock);
568
569 return err;
570}
571
572/*
573 * pointer callback
574 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100575static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200576{
577 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100578 struct snd_pcm_runtime *runtime = substream->runtime;
Trent Piepho05b27232007-08-24 01:06:34 -0300579 u16 count;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200580
Trent Piepho05b27232007-08-24 01:06:34 -0300581 count = atomic_read(&chip->count);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200582
Harvey Harrison32d83ef2008-04-08 23:20:00 -0300583// dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
Trent Piepho05b27232007-08-24 01:06:34 -0300584// count, new, count & (runtime->periods-1),
585// runtime->period_size * (count & (runtime->periods-1)));
586 return runtime->period_size * (count & (runtime->periods-1));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200587}
588
589/*
Trent Piephof6210c92007-08-24 01:06:36 -0300590 * page callback (needed for mmap)
591 */
592static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
593 unsigned long offset)
594{
595 void *pageptr = substream->runtime->dma_area + offset;
596 return vmalloc_to_page(pageptr);
597}
598
599/*
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200600 * operators
601 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100602static struct snd_pcm_ops snd_cx88_pcm_ops = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200603 .open = snd_cx88_pcm_open,
604 .close = snd_cx88_close,
605 .ioctl = snd_pcm_lib_ioctl,
606 .hw_params = snd_cx88_hw_params,
607 .hw_free = snd_cx88_hw_free,
608 .prepare = snd_cx88_prepare,
609 .trigger = snd_cx88_card_trigger,
610 .pointer = snd_cx88_pointer,
Trent Piephof6210c92007-08-24 01:06:36 -0300611 .page = snd_cx88_page,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200612};
613
614/*
615 * create a PCM device
616 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800617static int snd_cx88_pcm(snd_cx88_card_t *chip, int device, const char *name)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200618{
619 int err;
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100620 struct snd_pcm *pcm;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200621
622 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
623 if (err < 0)
624 return err;
625 pcm->private_data = chip;
626 strcpy(pcm->name, name);
627 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
628
629 return 0;
630}
631
632/****************************************************************************
633 CONTROL INTERFACE
634 ****************************************************************************/
Trent Piepho54ac0052007-09-06 23:02:23 -0300635static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
636 struct snd_ctl_elem_info *info)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200637{
638 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Trent Piepho82896f22007-09-06 23:02:23 -0300639 info->count = 2;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200640 info->value.integer.min = 0;
641 info->value.integer.max = 0x3f;
642
643 return 0;
644}
645
Trent Piepho54ac0052007-09-06 23:02:23 -0300646static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200648{
649 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
650 struct cx88_core *core=chip->core;
Trent Piepho82896f22007-09-06 23:02:23 -0300651 int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
652 bal = cx_read(AUD_BAL_CTL);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200653
Trent Piepho82896f22007-09-06 23:02:23 -0300654 value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
655 vol -= (bal & 0x3f);
656 value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200657
658 return 0;
659}
660
Lawrence Rust69518032011-02-06 17:46:12 -0300661static void snd_cx88_wm8775_volume_put(struct snd_kcontrol *kcontrol,
662 struct snd_ctl_elem_value *value)
663{
664 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
665 struct cx88_core *core = chip->core;
Lawrence Rust69518032011-02-06 17:46:12 -0300666 int left = value->value.integer.value[0];
667 int right = value->value.integer.value[1];
668 int v, b;
669
Lawrence Rust69518032011-02-06 17:46:12 -0300670 /* Pass volume & balance onto any WM8775 */
671 if (left >= right) {
672 v = left << 10;
673 b = left ? (0x8000 * right) / left : 0x8000;
674 } else {
675 v = right << 10;
676 b = right ? 0xffff - (0x8000 * left) / right : 0x8000;
677 }
Hans Verkuilbac63982012-06-10 07:39:52 -0300678 wm8775_s_ctrl(core, V4L2_CID_AUDIO_VOLUME, v);
679 wm8775_s_ctrl(core, V4L2_CID_AUDIO_BALANCE, b);
Lawrence Rust69518032011-02-06 17:46:12 -0300680}
681
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200682/* OK - TODO: test it */
Trent Piepho54ac0052007-09-06 23:02:23 -0300683static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
684 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200685{
686 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
687 struct cx88_core *core=chip->core;
Clemens Ladischcca80b92010-02-22 06:45:07 -0300688 int left, right, v, b;
Trent Piepho82896f22007-09-06 23:02:23 -0300689 int changed = 0;
690 u32 old;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200691
Hans Verkuil609c4c12013-05-29 10:44:22 -0300692 if (core->sd_wm8775)
Lawrence Rust69518032011-02-06 17:46:12 -0300693 snd_cx88_wm8775_volume_put(kcontrol, value);
694
Clemens Ladischcca80b92010-02-22 06:45:07 -0300695 left = value->value.integer.value[0] & 0x3f;
696 right = value->value.integer.value[1] & 0x3f;
697 b = right - left;
Trent Piepho82896f22007-09-06 23:02:23 -0300698 if (b < 0) {
Lawrence Rust69518032011-02-06 17:46:12 -0300699 v = 0x3f - left;
700 b = (-b) | 0x40;
Trent Piepho82896f22007-09-06 23:02:23 -0300701 } else {
Lawrence Rust69518032011-02-06 17:46:12 -0300702 v = 0x3f - right;
Trent Piepho82896f22007-09-06 23:02:23 -0300703 }
Trent Piepho05b27232007-08-24 01:06:34 -0300704 /* Do we really know this will always be called with IRQs on? */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200705 spin_lock_irq(&chip->reg_lock);
Trent Piepho82896f22007-09-06 23:02:23 -0300706 old = cx_read(AUD_VOL_CTL);
707 if (v != (old & 0x3f)) {
Lawrence Rust69518032011-02-06 17:46:12 -0300708 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v);
709 changed = 1;
Trent Piepho82896f22007-09-06 23:02:23 -0300710 }
Lawrence Rust69518032011-02-06 17:46:12 -0300711 if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) {
712 cx_write(AUD_BAL_CTL, b);
713 changed = 1;
Trent Piepho82896f22007-09-06 23:02:23 -0300714 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200715 spin_unlock_irq(&chip->reg_lock);
716
Trent Piepho82896f22007-09-06 23:02:23 -0300717 return changed;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200718}
719
Trent Piephobbaccc02007-09-06 23:02:25 -0300720static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
721
lawrence rust2e4e98e2010-08-25 09:50:20 -0300722static const struct snd_kcontrol_new snd_cx88_volume = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Trent Piephobbaccc02007-09-06 23:02:25 -0300724 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
725 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Lawrence Rust69518032011-02-06 17:46:12 -0300726 .name = "Analog-TV Volume",
Trent Piepho54ac0052007-09-06 23:02:23 -0300727 .info = snd_cx88_volume_info,
728 .get = snd_cx88_volume_get,
729 .put = snd_cx88_volume_put,
Trent Piephobbaccc02007-09-06 23:02:25 -0300730 .tlv.p = snd_cx88_db_scale,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200731};
732
Trent Piepho54ac0052007-09-06 23:02:23 -0300733static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
734 struct snd_ctl_elem_value *value)
735{
736 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
737 struct cx88_core *core = chip->core;
738 u32 bit = kcontrol->private_value;
739
740 value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
741 return 0;
742}
743
744static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_value *value)
746{
747 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
748 struct cx88_core *core = chip->core;
749 u32 bit = kcontrol->private_value;
750 int ret = 0;
751 u32 vol;
752
753 spin_lock_irq(&chip->reg_lock);
754 vol = cx_read(AUD_VOL_CTL);
755 if (value->value.integer.value[0] != !(vol & bit)) {
756 vol ^= bit;
Lawrence Rust69518032011-02-06 17:46:12 -0300757 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol);
758 /* Pass mute onto any WM8775 */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300759 if (core->sd_wm8775 && ((1<<6) == bit))
Hans Verkuilbac63982012-06-10 07:39:52 -0300760 wm8775_s_ctrl(core, V4L2_CID_AUDIO_MUTE, 0 != (vol & bit));
Trent Piepho54ac0052007-09-06 23:02:23 -0300761 ret = 1;
762 }
763 spin_unlock_irq(&chip->reg_lock);
764 return ret;
765}
766
lawrence rust2e4e98e2010-08-25 09:50:20 -0300767static const struct snd_kcontrol_new snd_cx88_dac_switch = {
Trent Piepho54ac0052007-09-06 23:02:23 -0300768 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Lawrence Rust69518032011-02-06 17:46:12 -0300769 .name = "Audio-Out Switch",
Trent Piepho54ac0052007-09-06 23:02:23 -0300770 .info = snd_ctl_boolean_mono_info,
771 .get = snd_cx88_switch_get,
772 .put = snd_cx88_switch_put,
773 .private_value = (1<<8),
774};
775
lawrence rust2e4e98e2010-08-25 09:50:20 -0300776static const struct snd_kcontrol_new snd_cx88_source_switch = {
Trent Piepho54ac0052007-09-06 23:02:23 -0300777 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Lawrence Rust69518032011-02-06 17:46:12 -0300778 .name = "Analog-TV Switch",
Trent Piepho54ac0052007-09-06 23:02:23 -0300779 .info = snd_ctl_boolean_mono_info,
780 .get = snd_cx88_switch_get,
781 .put = snd_cx88_switch_put,
782 .private_value = (1<<6),
783};
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200784
Lawrence Rust69518032011-02-06 17:46:12 -0300785static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol,
786 struct snd_ctl_elem_value *value)
787{
788 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
789 struct cx88_core *core = chip->core;
Hans Verkuilbac63982012-06-10 07:39:52 -0300790 s32 val;
Lawrence Rust69518032011-02-06 17:46:12 -0300791
Hans Verkuilbac63982012-06-10 07:39:52 -0300792 val = wm8775_g_ctrl(core, V4L2_CID_AUDIO_LOUDNESS);
793 value->value.integer.value[0] = val ? 1 : 0;
Lawrence Rust69518032011-02-06 17:46:12 -0300794 return 0;
795}
796
797static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
798 struct snd_ctl_elem_value *value)
799{
800 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
801 struct cx88_core *core = chip->core;
802 struct v4l2_control client_ctl;
803
804 memset(&client_ctl, 0, sizeof(client_ctl));
805 client_ctl.value = 0 != value->value.integer.value[0];
806 client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
807 call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
808
809 return 0;
810}
811
812static struct snd_kcontrol_new snd_cx88_alc_switch = {
813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
814 .name = "Line-In ALC Switch",
815 .info = snd_ctl_boolean_mono_info,
816 .get = snd_cx88_alc_get,
817 .put = snd_cx88_alc_put,
818};
819
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200820/****************************************************************************
821 Basic Flow for Sound Devices
822 ****************************************************************************/
823
824/*
825 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
826 * Only boards with eeprom and byte 1 at eeprom=1 have it
827 */
828
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800829static const struct pci_device_id cx88_audio_pci_tbl[] = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200830 {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
831 {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
832 {0, }
833};
834MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
835
836/*
837 * Chip-specific destructor
838 */
839
840static int snd_cx88_free(snd_cx88_card_t *chip)
841{
842
Jeff Garzikf000fd82008-04-22 13:50:34 +0200843 if (chip->irq >= 0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200844 free_irq(chip->irq, chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200845
846 cx88_core_put(chip->core,chip->pci);
847
848 pci_disable_device(chip->pci);
849 return 0;
850}
851
852/*
853 * Component Destructor
854 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100855static void snd_cx88_dev_free(struct snd_card * card)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200856{
857 snd_cx88_card_t *chip = card->private_data;
858
859 snd_cx88_free(chip);
860}
861
862
863/*
864 * Alsa Constructor - Component probe
865 */
866
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200867static int devno;
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800868static int snd_cx88_create(struct snd_card *card, struct pci_dev *pci,
869 snd_cx88_card_t **rchip,
870 struct cx88_core **core_ptr)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200871{
872 snd_cx88_card_t *chip;
873 struct cx88_core *core;
874 int err;
Trent Piepho19453bc2007-08-18 22:54:49 -0300875 unsigned char pci_lat;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200876
877 *rchip = NULL;
878
879 err = pci_enable_device(pci);
880 if (err < 0)
881 return err;
882
883 pci_set_master(pci);
884
Joe Perchesabf84382010-07-12 17:50:03 -0300885 chip = card->private_data;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200886
887 core = cx88_core_get(pci);
Duncan Sands31dcbf92006-03-14 12:12:39 -0300888 if (NULL == core) {
889 err = -EINVAL;
Duncan Sands31dcbf92006-03-14 12:12:39 -0300890 return err;
891 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200892
Yang Hongyang284901a2009-04-06 19:01:15 -0700893 if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200894 dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
895 err = -EIO;
Lawrence Rust69518032011-02-06 17:46:12 -0300896 cx88_core_put(core, pci);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200897 return err;
898 }
899
900
901 /* pci init */
902 chip->card = card;
903 chip->pci = pci;
904 chip->irq = -1;
905 spin_lock_init(&chip->reg_lock);
906
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200907 chip->core = core;
908
909 /* get irq */
910 err = request_irq(chip->pci->irq, cx8801_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -0300911 IRQF_SHARED, chip->core->name, chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200912 if (err < 0) {
913 dprintk(0, "%s: can't get IRQ %d\n",
914 chip->core->name, chip->pci->irq);
915 return err;
916 }
917
918 /* print pci info */
Trent Piepho19453bc2007-08-18 22:54:49 -0300919 pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200920
921 dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700922 "latency: %d, mmio: 0x%llx\n", core->name, devno,
Trent Piepho19453bc2007-08-18 22:54:49 -0300923 pci_name(pci), pci->revision, pci->irq,
924 pci_lat, (unsigned long long)pci_resource_start(pci,0));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200925
926 chip->irq = pci->irq;
927 synchronize_irq(chip->irq);
928
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200929 *rchip = chip;
Lawrence Rust69518032011-02-06 17:46:12 -0300930 *core_ptr = core;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200931
932 return 0;
933}
934
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800935static int cx88_audio_initdev(struct pci_dev *pci,
936 const struct pci_device_id *pci_id)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200937{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100938 struct snd_card *card;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200939 snd_cx88_card_t *chip;
Hans Verkuild6972cc2011-03-06 09:31:00 -0300940 struct cx88_core *core = NULL;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200941 int err;
942
943 if (devno >= SNDRV_CARDS)
944 return (-ENODEV);
945
946 if (!enable[devno]) {
947 ++devno;
948 return (-ENOENT);
949 }
950
Takashi Iwaie7356882014-01-29 14:48:43 +0100951 err = snd_card_new(&pci->dev, index[devno], id[devno], THIS_MODULE,
952 sizeof(snd_cx88_card_t), &card);
Takashi Iwai758021b2009-01-12 15:17:09 +0100953 if (err < 0)
954 return err;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200955
956 card->private_free = snd_cx88_dev_free;
957
Lawrence Rust69518032011-02-06 17:46:12 -0300958 err = snd_cx88_create(card, pci, &chip, &core);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200959 if (err < 0)
Julia Lawalla8782f62008-12-02 19:34:52 -0300960 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200961
962 err = snd_cx88_pcm(chip, 0, "CX88 Digital");
Trent Piepho82896f22007-09-06 23:02:23 -0300963 if (err < 0)
964 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200965
Trent Piepho54ac0052007-09-06 23:02:23 -0300966 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
967 if (err < 0)
968 goto error;
969 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
970 if (err < 0)
971 goto error;
972 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
Trent Piepho82896f22007-09-06 23:02:23 -0300973 if (err < 0)
974 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200975
Lawrence Rust69518032011-02-06 17:46:12 -0300976 /* If there's a wm8775 then add a Line-In ALC switch */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300977 if (core->sd_wm8775)
Lawrence Rust69518032011-02-06 17:46:12 -0300978 snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip));
979
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200980 strcpy (card->driver, "CX88x");
981 sprintf(card->shortname, "Conexant CX%x", pci->device);
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700982 sprintf(card->longname, "%s at %#llx",
983 card->shortname,(unsigned long long)pci_resource_start(pci, 0));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200984 strcpy (card->mixername, "CX88");
985
986 dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
987 card->driver,devno);
988
989 err = snd_card_register(card);
Trent Piepho82896f22007-09-06 23:02:23 -0300990 if (err < 0)
991 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200992 pci_set_drvdata(pci,card);
993
994 devno++;
995 return 0;
Trent Piepho82896f22007-09-06 23:02:23 -0300996
997error:
998 snd_card_free(card);
999 return err;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001000}
1001/*
1002 * ALSA destructor
1003 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001004static void cx88_audio_finidev(struct pci_dev *pci)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001005{
Wei Yongjuncd86e3b2013-11-21 00:38:44 -03001006 struct snd_card *card = pci_get_drvdata(pci);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001007
Wei Yongjuncd86e3b2013-11-21 00:38:44 -03001008 snd_card_free(card);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001009
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001010 devno--;
1011}
1012
1013/*
1014 * PCI driver definition
1015 */
1016
1017static struct pci_driver cx88_audio_pci_driver = {
1018 .name = "cx88_audio",
1019 .id_table = cx88_audio_pci_tbl,
1020 .probe = cx88_audio_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001021 .remove = cx88_audio_finidev,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001022};
1023
Sachin Kamat59963b92013-09-20 05:32:05 -03001024module_pci_driver(cx88_audio_pci_driver);