blob: 8a28fda703a20889571c73fbf562fb582cb3606f [file] [log] [blame]
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001/*
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02002 * Support for audio capture
3 * PCI function #1 of the cx2388x.
4 *
Trent Piepho05b27232007-08-24 01:06:34 -03005 * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02006 * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04007 * (c) 2005 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02008 * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02009 * Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020020 */
21
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020022#include "cx88.h"
23#include "cx88-reg.h"
24
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020025#include <linux/module.h>
26#include <linux/init.h>
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020027#include <linux/delay.h>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020028#include <linux/device.h>
29#include <linux/interrupt.h>
Trent Piephof6210c92007-08-24 01:06:36 -030030#include <linux/vmalloc.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070031#include <linux/dma-mapping.h>
Trent Piepho19453bc2007-08-18 22:54:49 -030032#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Andrew Mortonc24228d2007-05-06 14:51:55 -070034
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/control.h>
39#include <sound/initval.h>
Trent Piephobbaccc02007-09-06 23:02:25 -030040#include <sound/tlv.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020041#include <media/i2c/wm8775.h>
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020042
Mauro Carvalho Chehabdb613712012-10-27 15:25:02 -030043#define dprintk(level, fmt, arg...) do { \
44 if (debug + 1 > level) \
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020045 printk(KERN_DEBUG pr_fmt("%s: alsa: " fmt), \
46 chip->core->name, ##arg); \
47} while (0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020048
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020049/*
50 * Data type declarations - Can be moded to a header file later
51 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020052
Laurent Pinchartfecfede2010-05-11 10:36:31 -030053struct cx88_audio_buffer {
54 unsigned int bpl;
Hans Verkuil5e7045e2014-08-29 04:11:54 -030055 struct cx88_riscmem risc;
Hans Verkuilb2c75ab2014-08-29 05:25:30 -030056 void *vaddr;
57 struct scatterlist *sglist;
58 int sglen;
59 int nr_pages;
Laurent Pinchartfecfede2010-05-11 10:36:31 -030060};
61
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020062struct cx88_audio_dev {
63 struct cx88_core *core;
64 struct cx88_dmaqueue q;
65
66 /* pci i/o */
67 struct pci_dev *pci;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020068
69 /* audio controls */
70 int irq;
71
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010072 struct snd_card *card;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020073
74 spinlock_t reg_lock;
Trent Piepho05b27232007-08-24 01:06:34 -030075 atomic_t count;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020076
77 unsigned int dma_size;
78 unsigned int period_size;
79 unsigned int num_periods;
80
Laurent Pinchartfecfede2010-05-11 10:36:31 -030081 struct cx88_audio_buffer *buf;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020082
Trent Piepho05b27232007-08-24 01:06:34 -030083 struct snd_pcm_substream *substream;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020084};
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020085
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020086/*
87 * Module global static vars
88 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020089
90static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
lawrence rust2e4e98e2010-08-25 09:50:20 -030091static const char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Luis de Bethencourtd275d932015-09-21 13:00:41 -030092static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -020093
94module_param_array(enable, bool, NULL, 0444);
95MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
96
97module_param_array(index, int, NULL, 0444);
98MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
99
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200100/*
101 * Module macros
102 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200103
104MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
105MODULE_AUTHOR("Ricardo Cerqueira");
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -0400106MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200107MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -0300108MODULE_VERSION(CX88_VERSION);
109
Mauro Carvalho Chehabe318e582016-10-18 17:44:04 -0200110MODULE_SUPPORTED_DEVICE("{{Conexant,23881},{{Conexant,23882},{{Conexant,23883}");
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200111static unsigned int debug;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200112module_param(debug, int, 0644);
113MODULE_PARM_DESC(debug, "enable debug messages");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200114
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200115/*
116 * Module specific functions
117 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200118
119/*
120 * BOARD Specific: Sets audio DMA
121 */
122
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200123static int _cx88_start_audio_dma(struct cx88_audio_dev *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200124{
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300125 struct cx88_audio_buffer *buf = chip->buf;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200126 struct cx88_core *core = chip->core;
lawrence rust2e4e98e2010-08-25 09:50:20 -0300127 const struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200128
Trent Piepho59fd8f82007-08-18 22:09:42 -0300129 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
130 cx_clear(MO_AUD_DMACNTRL, 0x11);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200131
132 /* setup fifo + format - out channel */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300133 cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200134
135 /* sets bpl size */
136 cx_write(MO_AUDD_LNGTH, buf->bpl);
137
138 /* reset counter */
Trent Piepho05b27232007-08-24 01:06:34 -0300139 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
140 atomic_set(&chip->count, 0);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200141
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200142 dprintk(1,
143 "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
144 buf->bpl, cx_read(audio_ch->cmds_start + 8) >> 1,
Trent Piepho59fd8f82007-08-18 22:09:42 -0300145 chip->num_periods, buf->bpl * chip->num_periods);
146
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200147 /* Enables corresponding bits at AUD_INT_STAT */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300148 cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
149 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
150
151 /* Clean any pending interrupt bits already set */
152 cx_write(MO_AUD_INTSTAT, ~0);
153
154 /* enable audio irqs */
155 cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200156
157 /* start dma */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200158
159 /* Enables Risc Processor */
160 cx_set(MO_DEV_CNTRL2, (1 << 5));
161 /* audio downstream FIFO and RISC enable */
162 cx_set(MO_AUD_DMACNTRL, 0x11);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200163
164 if (debug)
Trent Piepho59fd8f82007-08-18 22:09:42 -0300165 cx88_sram_channel_dump(chip->core, audio_ch);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200166
167 return 0;
168}
169
170/*
171 * BOARD Specific: Resets audio DMA
172 */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200173static int _cx88_stop_audio_dma(struct cx88_audio_dev *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200174{
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200175 struct cx88_core *core = chip->core;
176
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200177 dprintk(1, "Stopping audio DMA\n");
178
179 /* stop dma */
180 cx_clear(MO_AUD_DMACNTRL, 0x11);
181
182 /* disable irqs */
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300183 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Trent Piepho59fd8f82007-08-18 22:09:42 -0300184 cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
185 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200186
187 if (debug)
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200188 cx88_sram_channel_dump(chip->core,
189 &cx88_sram_channels[SRAM_CH25]);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200190
191 return 0;
192}
193
Trent Piepho05b27232007-08-24 01:06:34 -0300194#define MAX_IRQ_LOOP 50
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200195
196/*
197 * BOARD Specific: IRQ dma bits
198 */
lawrence rust2e4e98e2010-08-25 09:50:20 -0300199static const char *cx88_aud_irqs[32] = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200200 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
201 NULL, /* reserved */
202 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
203 NULL, /* reserved */
204 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
205 NULL, /* reserved */
206 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
207 NULL, /* reserved */
208 "opc_err", "par_err", "rip_err", /* 16-18 */
209 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
210};
211
212/*
213 * BOARD Specific: Threats IRQ audio specific calls
214 */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200215static void cx8801_aud_irq(struct cx88_audio_dev *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200216{
217 struct cx88_core *core = chip->core;
218 u32 status, mask;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200219
220 status = cx_read(MO_AUD_INTSTAT);
221 mask = cx_read(MO_AUD_INTMSK);
Trent Piepho05b27232007-08-24 01:06:34 -0300222 if (0 == (status & mask))
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200223 return;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200224 cx_write(MO_AUD_INTSTAT, status);
225 if (debug > 1 || (status & mask & ~0xff))
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200226 cx88_print_irqbits("irq aud",
Mauro Carvalho Chehab66623a02007-03-29 08:47:04 -0300227 cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
228 status, mask);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200229 /* risc op code error */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300230 if (status & AUD_INT_OPC_ERR) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200231 pr_warn("Audio risc op code error\n");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200232 cx_clear(MO_AUD_DMACNTRL, 0x11);
233 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
234 }
Trent Piepho05b27232007-08-24 01:06:34 -0300235 if (status & AUD_INT_DN_SYNC) {
236 dprintk(1, "Downstream sync error\n");
237 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
238 return;
239 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200240 /* risc1 downstream */
Trent Piepho59fd8f82007-08-18 22:09:42 -0300241 if (status & AUD_INT_DN_RISCI1) {
Trent Piepho05b27232007-08-24 01:06:34 -0300242 atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200243 snd_pcm_period_elapsed(chip->substream);
244 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200245 /* FIXME: Any other status should deserve a special handling? */
246}
247
248/*
249 * BOARD Specific: Handles IRQ calls
250 */
David Howells7d12e782006-10-05 14:55:46 +0100251static irqreturn_t cx8801_irq(int irq, void *dev_id)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200252{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200253 struct cx88_audio_dev *chip = dev_id;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200254 struct cx88_core *core = chip->core;
255 u32 status;
256 int loop, handled = 0;
257
258 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300259 status = cx_read(MO_PCI_INTSTAT) &
260 (core->pci_irqmask | PCI_INT_AUDINT);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200261 if (status == 0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200262 goto out;
Trent Piepho05b27232007-08-24 01:06:34 -0300263 dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
264 loop, MAX_IRQ_LOOP, status);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200265 handled = 1;
266 cx_write(MO_PCI_INTSTAT, status);
267
Trent Piepho5ba862b2007-08-18 07:02:26 -0300268 if (status & core->pci_irqmask)
269 cx88_core_irq(core, status);
Trent Piepho05b27232007-08-24 01:06:34 -0300270 if (status & PCI_INT_AUDINT)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200271 cx8801_aud_irq(chip);
Trent Piepho05b27232007-08-24 01:06:34 -0300272 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200273
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200274 if (loop == MAX_IRQ_LOOP) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200275 pr_err("IRQ loop detected, disabling interrupts\n");
Trent Piepho8ddac9e2007-08-18 06:57:55 -0300276 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200277 }
278
279 out:
280 return IRQ_RETVAL(handled);
281}
282
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300283static int cx88_alsa_dma_init(struct cx88_audio_dev *chip, int nr_pages)
284{
285 struct cx88_audio_buffer *buf = chip->buf;
286 struct page *pg;
287 int i;
288
289 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200290 if (!buf->vaddr) {
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300291 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
292 return -ENOMEM;
293 }
294
Mauro Carvalho Chehabaea629c2018-03-22 14:56:01 -0400295 dprintk(1, "vmalloc is at addr %p, size=%d\n",
296 buf->vaddr, nr_pages << PAGE_SHIFT);
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300297
298 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
299 buf->nr_pages = nr_pages;
300
301 buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200302 if (!buf->sglist)
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300303 goto vzalloc_err;
304
305 sg_init_table(buf->sglist, buf->nr_pages);
306 for (i = 0; i < buf->nr_pages; i++) {
307 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200308 if (!pg)
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300309 goto vmalloc_to_page_err;
310 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
311 }
312 return 0;
313
314vmalloc_to_page_err:
315 vfree(buf->sglist);
316 buf->sglist = NULL;
317vzalloc_err:
318 vfree(buf->vaddr);
319 buf->vaddr = NULL;
320 return -ENOMEM;
321}
322
323static int cx88_alsa_dma_map(struct cx88_audio_dev *dev)
324{
325 struct cx88_audio_buffer *buf = dev->buf;
326
327 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
328 buf->nr_pages, PCI_DMA_FROMDEVICE);
329
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200330 if (buf->sglen == 0) {
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300331 pr_warn("%s: cx88_alsa_map_sg failed\n", __func__);
332 return -ENOMEM;
333 }
334 return 0;
335}
336
337static int cx88_alsa_dma_unmap(struct cx88_audio_dev *dev)
338{
339 struct cx88_audio_buffer *buf = dev->buf;
340
341 if (!buf->sglen)
342 return 0;
343
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200344 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen,
345 PCI_DMA_FROMDEVICE);
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300346 buf->sglen = 0;
347 return 0;
348}
349
350static int cx88_alsa_dma_free(struct cx88_audio_buffer *buf)
351{
352 vfree(buf->sglist);
353 buf->sglist = NULL;
354 vfree(buf->vaddr);
355 buf->vaddr = NULL;
356 return 0;
357}
358
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200359static int dsp_buffer_free(struct cx88_audio_dev *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200360{
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300361 struct cx88_riscmem *risc = &chip->buf->risc;
362
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200363 WARN_ON(!chip->dma_size);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200364
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200365 dprintk(2, "Freeing buffer\n");
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300366 cx88_alsa_dma_unmap(chip);
367 cx88_alsa_dma_free(chip->buf);
Hans Verkuil5e7045e2014-08-29 04:11:54 -0300368 if (risc->cpu)
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200369 pci_free_consistent(chip->pci, risc->size,
370 risc->cpu, risc->dma);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200371 kfree(chip->buf);
372
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300373 chip->buf = NULL;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200374
Trent Piepho05b27232007-08-24 01:06:34 -0300375 return 0;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200376}
377
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200378/*
379 * ALSA PCM Interface
380 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200381
382/*
383 * Digital hardware definition
384 */
Trent Piepho05b27232007-08-24 01:06:34 -0300385#define DEFAULT_FIFO_SIZE 4096
lawrence rust2e4e98e2010-08-25 09:50:20 -0300386static const struct snd_pcm_hardware snd_cx88_digital_hw = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200387 .info = SNDRV_PCM_INFO_MMAP |
388 SNDRV_PCM_INFO_INTERLEAVED |
389 SNDRV_PCM_INFO_BLOCK_TRANSFER |
390 SNDRV_PCM_INFO_MMAP_VALID,
391 .formats = SNDRV_PCM_FMTBIT_S16_LE,
392
393 .rates = SNDRV_PCM_RATE_48000,
394 .rate_min = 48000,
395 .rate_max = 48000,
Trent Piepho5a5b3b52007-08-18 21:01:40 -0300396 .channels_min = 2,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200397 .channels_max = 2,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200398 /*
399 * Analog audio output will be full of clicks and pops if there
400 * are not exactly four lines in the SRAM FIFO buffer.
401 */
402 .period_bytes_min = DEFAULT_FIFO_SIZE / 4,
403 .period_bytes_max = DEFAULT_FIFO_SIZE / 4,
Trent Piepho05b27232007-08-24 01:06:34 -0300404 .periods_min = 1,
405 .periods_max = 1024,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200406 .buffer_bytes_max = (1024 * 1024),
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200407};
408
409/*
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200410 * audio pcm capture open callback
411 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100412static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200413{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200414 struct cx88_audio_dev *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100415 struct snd_pcm_runtime *runtime = substream->runtime;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200416 int err;
417
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300418 if (!chip) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200419 pr_err("BUG: cx88 can't find device struct. Can't proceed with open\n");
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300420 return -ENODEV;
421 }
422
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200423 err = snd_pcm_hw_constraint_pow2(runtime, 0,
424 SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200425 if (err < 0)
426 goto _error;
427
428 chip->substream = substream;
429
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200430 runtime->hw = snd_cx88_digital_hw;
431
Trent Piepho05b27232007-08-24 01:06:34 -0300432 if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
433 unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200434
Trent Piepho05b27232007-08-24 01:06:34 -0300435 bpl &= ~7; /* must be multiple of 8 */
436 runtime->hw.period_bytes_min = bpl;
437 runtime->hw.period_bytes_max = bpl;
438 }
439
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200440 return 0;
441_error:
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200442 dprintk(1, "Error opening PCM!\n");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200443 return err;
444}
445
446/*
447 * audio close callback
448 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100449static int snd_cx88_close(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200450{
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200451 return 0;
452}
453
454/*
455 * hw_params callback
456 */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200457static int snd_cx88_hw_params(struct snd_pcm_substream *substream,
458 struct snd_pcm_hw_params *hw_params)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200459{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200460 struct cx88_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -0300461
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300462 struct cx88_audio_buffer *buf;
Trent Piepho05b27232007-08-24 01:06:34 -0300463 int ret;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200464
465 if (substream->runtime->dma_area) {
466 dsp_buffer_free(chip);
467 substream->runtime->dma_area = NULL;
468 }
469
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200470 chip->period_size = params_period_bytes(hw_params);
471 chip->num_periods = params_periods(hw_params);
472 chip->dma_size = chip->period_size * params_periods(hw_params);
473
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200474 WARN_ON(!chip->dma_size);
475 WARN_ON(chip->num_periods & (chip->num_periods - 1));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200476
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300477 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200478 if (!buf)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200479 return -ENOMEM;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200480
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300481 chip->buf = buf;
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300482 buf->bpl = chip->period_size;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200483
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300484 ret = cx88_alsa_dma_init(chip,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200485 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
Trent Piepho05b27232007-08-24 01:06:34 -0300486 if (ret < 0)
487 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200488
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300489 ret = cx88_alsa_dma_map(chip);
Trent Piepho05b27232007-08-24 01:06:34 -0300490 if (ret < 0)
491 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200492
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300493 ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
Laurent Pinchartfecfede2010-05-11 10:36:31 -0300494 chip->period_size, chip->num_periods, 1);
Trent Piepho05b27232007-08-24 01:06:34 -0300495 if (ret < 0)
496 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200497
Trent Piepho05b27232007-08-24 01:06:34 -0300498 /* Loop back to start of program */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200499 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200500 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
501
Hans Verkuilb2c75ab2014-08-29 05:25:30 -0300502 substream->runtime->dma_area = chip->buf->vaddr;
Trent Piephof6210c92007-08-24 01:06:36 -0300503 substream->runtime->dma_bytes = chip->dma_size;
504 substream->runtime->dma_addr = 0;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200505 return 0;
Trent Piepho05b27232007-08-24 01:06:34 -0300506
507error:
508 kfree(buf);
509 return ret;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200510}
511
512/*
513 * hw free callback
514 */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200515static int snd_cx88_hw_free(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200516{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200517 struct cx88_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200518
519 if (substream->runtime->dma_area) {
520 dsp_buffer_free(chip);
521 substream->runtime->dma_area = NULL;
522 }
523
524 return 0;
525}
526
527/*
528 * prepare callback
529 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100530static int snd_cx88_prepare(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200531{
532 return 0;
533}
534
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200535/*
536 * trigger callback
537 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100538static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200539{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200540 struct cx88_audio_dev *chip = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200541 int err;
542
Trent Piepho05b27232007-08-24 01:06:34 -0300543 /* Local interrupts are already disabled by ALSA */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200544 spin_lock(&chip->reg_lock);
545
546 switch (cmd) {
547 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200548 err = _cx88_start_audio_dma(chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200549 break;
550 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200551 err = _cx88_stop_audio_dma(chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200552 break;
553 default:
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200554 err = -EINVAL;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200555 break;
556 }
557
558 spin_unlock(&chip->reg_lock);
559
560 return err;
561}
562
563/*
564 * pointer callback
565 */
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100566static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200567{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200568 struct cx88_audio_dev *chip = snd_pcm_substream_chip(substream);
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100569 struct snd_pcm_runtime *runtime = substream->runtime;
Trent Piepho05b27232007-08-24 01:06:34 -0300570 u16 count;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200571
Trent Piepho05b27232007-08-24 01:06:34 -0300572 count = atomic_read(&chip->count);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200573
Harvey Harrison32d83ef2008-04-08 23:20:00 -0300574// dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
Trent Piepho05b27232007-08-24 01:06:34 -0300575// count, new, count & (runtime->periods-1),
576// runtime->period_size * (count & (runtime->periods-1)));
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200577 return runtime->period_size * (count & (runtime->periods - 1));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200578}
579
580/*
Trent Piephof6210c92007-08-24 01:06:36 -0300581 * page callback (needed for mmap)
582 */
583static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200584 unsigned long offset)
Trent Piephof6210c92007-08-24 01:06:36 -0300585{
586 void *pageptr = substream->runtime->dma_area + offset;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200587
Trent Piephof6210c92007-08-24 01:06:36 -0300588 return vmalloc_to_page(pageptr);
589}
590
591/*
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200592 * operators
593 */
Julia Lawall5c8d8c02016-09-07 21:44:39 -0300594static const struct snd_pcm_ops snd_cx88_pcm_ops = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200595 .open = snd_cx88_pcm_open,
596 .close = snd_cx88_close,
597 .ioctl = snd_pcm_lib_ioctl,
598 .hw_params = snd_cx88_hw_params,
599 .hw_free = snd_cx88_hw_free,
600 .prepare = snd_cx88_prepare,
601 .trigger = snd_cx88_card_trigger,
602 .pointer = snd_cx88_pointer,
Trent Piephof6210c92007-08-24 01:06:36 -0300603 .page = snd_cx88_page,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200604};
605
606/*
607 * create a PCM device
608 */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200609static int snd_cx88_pcm(struct cx88_audio_dev *chip, int device,
610 const char *name)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200611{
612 int err;
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100613 struct snd_pcm *pcm;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200614
615 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
616 if (err < 0)
617 return err;
618 pcm->private_data = chip;
619 strcpy(pcm->name, name);
620 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
621
622 return 0;
623}
624
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200625/*
626 * CONTROL INTERFACE
627 */
Trent Piepho54ac0052007-09-06 23:02:23 -0300628static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
629 struct snd_ctl_elem_info *info)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200630{
631 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Trent Piepho82896f22007-09-06 23:02:23 -0300632 info->count = 2;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200633 info->value.integer.min = 0;
634 info->value.integer.max = 0x3f;
635
636 return 0;
637}
638
Trent Piepho54ac0052007-09-06 23:02:23 -0300639static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
640 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200641{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200642 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200643 struct cx88_core *core = chip->core;
Trent Piepho82896f22007-09-06 23:02:23 -0300644 int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
645 bal = cx_read(AUD_BAL_CTL);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200646
Trent Piepho82896f22007-09-06 23:02:23 -0300647 value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
648 vol -= (bal & 0x3f);
649 value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200650
651 return 0;
652}
653
Lawrence Rust69518032011-02-06 17:46:12 -0300654static void snd_cx88_wm8775_volume_put(struct snd_kcontrol *kcontrol,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200655 struct snd_ctl_elem_value *value)
Lawrence Rust69518032011-02-06 17:46:12 -0300656{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200657 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Lawrence Rust69518032011-02-06 17:46:12 -0300658 struct cx88_core *core = chip->core;
Mauro Carvalho Chehabaea629c2018-03-22 14:56:01 -0400659 u16 left = value->value.integer.value[0];
660 u16 right = value->value.integer.value[1];
Lawrence Rust69518032011-02-06 17:46:12 -0300661 int v, b;
662
Lawrence Rust69518032011-02-06 17:46:12 -0300663 /* Pass volume & balance onto any WM8775 */
664 if (left >= right) {
665 v = left << 10;
666 b = left ? (0x8000 * right) / left : 0x8000;
667 } else {
668 v = right << 10;
669 b = right ? 0xffff - (0x8000 * left) / right : 0x8000;
670 }
Hans Verkuilbac63982012-06-10 07:39:52 -0300671 wm8775_s_ctrl(core, V4L2_CID_AUDIO_VOLUME, v);
672 wm8775_s_ctrl(core, V4L2_CID_AUDIO_BALANCE, b);
Lawrence Rust69518032011-02-06 17:46:12 -0300673}
674
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200675/* OK - TODO: test it */
Trent Piepho54ac0052007-09-06 23:02:23 -0300676static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
677 struct snd_ctl_elem_value *value)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200678{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200679 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200680 struct cx88_core *core = chip->core;
Clemens Ladischcca80b92010-02-22 06:45:07 -0300681 int left, right, v, b;
Trent Piepho82896f22007-09-06 23:02:23 -0300682 int changed = 0;
683 u32 old;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200684
Hans Verkuil609c4c12013-05-29 10:44:22 -0300685 if (core->sd_wm8775)
Lawrence Rust69518032011-02-06 17:46:12 -0300686 snd_cx88_wm8775_volume_put(kcontrol, value);
687
Clemens Ladischcca80b92010-02-22 06:45:07 -0300688 left = value->value.integer.value[0] & 0x3f;
689 right = value->value.integer.value[1] & 0x3f;
690 b = right - left;
Trent Piepho82896f22007-09-06 23:02:23 -0300691 if (b < 0) {
Lawrence Rust69518032011-02-06 17:46:12 -0300692 v = 0x3f - left;
693 b = (-b) | 0x40;
Trent Piepho82896f22007-09-06 23:02:23 -0300694 } else {
Lawrence Rust69518032011-02-06 17:46:12 -0300695 v = 0x3f - right;
Trent Piepho82896f22007-09-06 23:02:23 -0300696 }
Trent Piepho05b27232007-08-24 01:06:34 -0300697 /* Do we really know this will always be called with IRQs on? */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200698 spin_lock_irq(&chip->reg_lock);
Trent Piepho82896f22007-09-06 23:02:23 -0300699 old = cx_read(AUD_VOL_CTL);
700 if (v != (old & 0x3f)) {
Lawrence Rust69518032011-02-06 17:46:12 -0300701 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v);
702 changed = 1;
Trent Piepho82896f22007-09-06 23:02:23 -0300703 }
Lawrence Rust69518032011-02-06 17:46:12 -0300704 if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) {
705 cx_write(AUD_BAL_CTL, b);
706 changed = 1;
Trent Piepho82896f22007-09-06 23:02:23 -0300707 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200708 spin_unlock_irq(&chip->reg_lock);
709
Trent Piepho82896f22007-09-06 23:02:23 -0300710 return changed;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200711}
712
Trent Piephobbaccc02007-09-06 23:02:25 -0300713static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
714
lawrence rust2e4e98e2010-08-25 09:50:20 -0300715static const struct snd_kcontrol_new snd_cx88_volume = {
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200716 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Trent Piephobbaccc02007-09-06 23:02:25 -0300717 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
718 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Lawrence Rust69518032011-02-06 17:46:12 -0300719 .name = "Analog-TV Volume",
Trent Piepho54ac0052007-09-06 23:02:23 -0300720 .info = snd_cx88_volume_info,
721 .get = snd_cx88_volume_get,
722 .put = snd_cx88_volume_put,
Trent Piephobbaccc02007-09-06 23:02:25 -0300723 .tlv.p = snd_cx88_db_scale,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200724};
725
Trent Piepho54ac0052007-09-06 23:02:23 -0300726static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
727 struct snd_ctl_elem_value *value)
728{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200729 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Trent Piepho54ac0052007-09-06 23:02:23 -0300730 struct cx88_core *core = chip->core;
731 u32 bit = kcontrol->private_value;
732
733 value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
734 return 0;
735}
736
737static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200738 struct snd_ctl_elem_value *value)
Trent Piepho54ac0052007-09-06 23:02:23 -0300739{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200740 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Trent Piepho54ac0052007-09-06 23:02:23 -0300741 struct cx88_core *core = chip->core;
742 u32 bit = kcontrol->private_value;
743 int ret = 0;
744 u32 vol;
745
746 spin_lock_irq(&chip->reg_lock);
747 vol = cx_read(AUD_VOL_CTL);
748 if (value->value.integer.value[0] != !(vol & bit)) {
749 vol ^= bit;
Lawrence Rust69518032011-02-06 17:46:12 -0300750 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol);
751 /* Pass mute onto any WM8775 */
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200752 if (core->sd_wm8775 && ((1 << 6) == bit))
753 wm8775_s_ctrl(core,
754 V4L2_CID_AUDIO_MUTE, 0 != (vol & bit));
Trent Piepho54ac0052007-09-06 23:02:23 -0300755 ret = 1;
756 }
757 spin_unlock_irq(&chip->reg_lock);
758 return ret;
759}
760
lawrence rust2e4e98e2010-08-25 09:50:20 -0300761static const struct snd_kcontrol_new snd_cx88_dac_switch = {
Trent Piepho54ac0052007-09-06 23:02:23 -0300762 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Lawrence Rust69518032011-02-06 17:46:12 -0300763 .name = "Audio-Out Switch",
Trent Piepho54ac0052007-09-06 23:02:23 -0300764 .info = snd_ctl_boolean_mono_info,
765 .get = snd_cx88_switch_get,
766 .put = snd_cx88_switch_put,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200767 .private_value = (1 << 8),
Trent Piepho54ac0052007-09-06 23:02:23 -0300768};
769
lawrence rust2e4e98e2010-08-25 09:50:20 -0300770static const struct snd_kcontrol_new snd_cx88_source_switch = {
Trent Piepho54ac0052007-09-06 23:02:23 -0300771 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Lawrence Rust69518032011-02-06 17:46:12 -0300772 .name = "Analog-TV Switch",
Trent Piepho54ac0052007-09-06 23:02:23 -0300773 .info = snd_ctl_boolean_mono_info,
774 .get = snd_cx88_switch_get,
775 .put = snd_cx88_switch_put,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200776 .private_value = (1 << 6),
Trent Piepho54ac0052007-09-06 23:02:23 -0300777};
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200778
Lawrence Rust69518032011-02-06 17:46:12 -0300779static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200780 struct snd_ctl_elem_value *value)
Lawrence Rust69518032011-02-06 17:46:12 -0300781{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200782 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Lawrence Rust69518032011-02-06 17:46:12 -0300783 struct cx88_core *core = chip->core;
Hans Verkuilbac63982012-06-10 07:39:52 -0300784 s32 val;
Lawrence Rust69518032011-02-06 17:46:12 -0300785
Hans Verkuilbac63982012-06-10 07:39:52 -0300786 val = wm8775_g_ctrl(core, V4L2_CID_AUDIO_LOUDNESS);
787 value->value.integer.value[0] = val ? 1 : 0;
Lawrence Rust69518032011-02-06 17:46:12 -0300788 return 0;
789}
790
791static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200792 struct snd_ctl_elem_value *value)
Lawrence Rust69518032011-02-06 17:46:12 -0300793{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200794 struct cx88_audio_dev *chip = snd_kcontrol_chip(kcontrol);
Lawrence Rust69518032011-02-06 17:46:12 -0300795 struct cx88_core *core = chip->core;
Lawrence Rust69518032011-02-06 17:46:12 -0300796
Hans Verkuilc2c33c32016-07-03 09:00:26 -0300797 wm8775_s_ctrl(core, V4L2_CID_AUDIO_LOUDNESS,
798 value->value.integer.value[0] != 0);
Lawrence Rust69518032011-02-06 17:46:12 -0300799 return 0;
800}
801
Bhumika Goyal9a69ae12017-08-16 05:17:04 -0400802static const struct snd_kcontrol_new snd_cx88_alc_switch = {
Lawrence Rust69518032011-02-06 17:46:12 -0300803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
804 .name = "Line-In ALC Switch",
805 .info = snd_ctl_boolean_mono_info,
806 .get = snd_cx88_alc_get,
807 .put = snd_cx88_alc_put,
808};
809
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200810/*
811 * Basic Flow for Sound Devices
812 */
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200813
814/*
815 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
816 * Only boards with eeprom and byte 1 at eeprom=1 have it
817 */
818
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800819static const struct pci_device_id cx88_audio_pci_tbl[] = {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200820 {0x14f1, 0x8801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
821 {0x14f1, 0x8811, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200822 {0, }
823};
824MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
825
826/*
827 * Chip-specific destructor
828 */
829
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200830static int snd_cx88_free(struct cx88_audio_dev *chip)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200831{
Jeff Garzikf000fd82008-04-22 13:50:34 +0200832 if (chip->irq >= 0)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200833 free_irq(chip->irq, chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200834
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200835 cx88_core_put(chip->core, chip->pci);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200836
837 pci_disable_device(chip->pci);
838 return 0;
839}
840
841/*
842 * Component Destructor
843 */
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200844static void snd_cx88_dev_free(struct snd_card *card)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200845{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200846 struct cx88_audio_dev *chip = card->private_data;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200847
848 snd_cx88_free(chip);
849}
850
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200851/*
852 * Alsa Constructor - Component probe
853 */
854
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -0200855static int devno;
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800856static int snd_cx88_create(struct snd_card *card, struct pci_dev *pci,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200857 struct cx88_audio_dev **rchip,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800858 struct cx88_core **core_ptr)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200859{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200860 struct cx88_audio_dev *chip;
861 struct cx88_core *core;
862 int err;
863 unsigned char pci_lat;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200864
865 *rchip = NULL;
866
867 err = pci_enable_device(pci);
868 if (err < 0)
869 return err;
870
871 pci_set_master(pci);
872
Joe Perchesabf84382010-07-12 17:50:03 -0300873 chip = card->private_data;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200874
875 core = cx88_core_get(pci);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200876 if (!core) {
Duncan Sands31dcbf92006-03-14 12:12:39 -0300877 err = -EINVAL;
Duncan Sands31dcbf92006-03-14 12:12:39 -0300878 return err;
879 }
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200880
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200881 err = pci_set_dma_mask(pci, DMA_BIT_MASK(32));
Christoph Hellwig1a47de62015-11-20 15:57:07 -0800882 if (err) {
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200883 dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n", core->name);
Lawrence Rust69518032011-02-06 17:46:12 -0300884 cx88_core_put(core, pci);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200885 return err;
886 }
887
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200888 /* pci init */
889 chip->card = card;
890 chip->pci = pci;
891 chip->irq = -1;
892 spin_lock_init(&chip->reg_lock);
893
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200894 chip->core = core;
895
896 /* get irq */
897 err = request_irq(chip->pci->irq, cx8801_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -0300898 IRQF_SHARED, chip->core->name, chip);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200899 if (err < 0) {
900 dprintk(0, "%s: can't get IRQ %d\n",
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200901 chip->core->name, chip->pci->irq);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200902 return err;
903 }
904
905 /* print pci info */
Trent Piepho19453bc2007-08-18 22:54:49 -0300906 pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200907
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200908 dprintk(1,
909 "ALSA %s/%i: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
Mauro Carvalho Chehabe318e582016-10-18 17:44:04 -0200910 core->name, devno,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200911 pci_name(pci), pci->revision, pci->irq,
912 pci_lat, (unsigned long long)pci_resource_start(pci, 0));
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200913
914 chip->irq = pci->irq;
915 synchronize_irq(chip->irq);
916
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200917 *rchip = chip;
Lawrence Rust69518032011-02-06 17:46:12 -0300918 *core_ptr = core;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200919
920 return 0;
921}
922
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800923static int cx88_audio_initdev(struct pci_dev *pci,
924 const struct pci_device_id *pci_id)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200925{
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200926 struct snd_card *card;
927 struct cx88_audio_dev *chip;
928 struct cx88_core *core = NULL;
929 int err;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200930
931 if (devno >= SNDRV_CARDS)
932 return (-ENODEV);
933
934 if (!enable[devno]) {
935 ++devno;
936 return (-ENOENT);
937 }
938
Takashi Iwaie7356882014-01-29 14:48:43 +0100939 err = snd_card_new(&pci->dev, index[devno], id[devno], THIS_MODULE,
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200940 sizeof(struct cx88_audio_dev), &card);
Takashi Iwai758021b2009-01-12 15:17:09 +0100941 if (err < 0)
942 return err;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200943
944 card->private_free = snd_cx88_dev_free;
945
Lawrence Rust69518032011-02-06 17:46:12 -0300946 err = snd_cx88_create(card, pci, &chip, &core);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200947 if (err < 0)
Julia Lawalla8782f62008-12-02 19:34:52 -0300948 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200949
950 err = snd_cx88_pcm(chip, 0, "CX88 Digital");
Trent Piepho82896f22007-09-06 23:02:23 -0300951 if (err < 0)
952 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200953
Trent Piepho54ac0052007-09-06 23:02:23 -0300954 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
955 if (err < 0)
956 goto error;
957 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
958 if (err < 0)
959 goto error;
960 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
Trent Piepho82896f22007-09-06 23:02:23 -0300961 if (err < 0)
962 goto error;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200963
Lawrence Rust69518032011-02-06 17:46:12 -0300964 /* If there's a wm8775 then add a Line-In ALC switch */
Hans Verkuil609c4c12013-05-29 10:44:22 -0300965 if (core->sd_wm8775)
Lawrence Rust69518032011-02-06 17:46:12 -0300966 snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip));
967
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200968 strcpy(card->driver, "CX88x");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200969 sprintf(card->shortname, "Conexant CX%x", pci->device);
Greg Kroah-Hartman228aef62006-06-12 15:16:52 -0700970 sprintf(card->longname, "%s at %#llx",
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200971 card->shortname,
972 (unsigned long long)pci_resource_start(pci, 0));
973 strcpy(card->mixername, "CX88");
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200974
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200975 dprintk(0, "%s/%i: ALSA support for cx2388x boards\n",
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200976 card->driver, devno);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200977
978 err = snd_card_register(card);
Trent Piepho82896f22007-09-06 23:02:23 -0300979 if (err < 0)
980 goto error;
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200981 pci_set_drvdata(pci, card);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200982
983 devno++;
984 return 0;
Trent Piepho82896f22007-09-06 23:02:23 -0300985
986error:
987 snd_card_free(card);
988 return err;
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200989}
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200990
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200991/*
992 * ALSA destructor
993 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800994static void cx88_audio_finidev(struct pci_dev *pci)
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200995{
Wei Yongjuncd86e3b2013-11-21 00:38:44 -0300996 struct snd_card *card = pci_get_drvdata(pci);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200997
Wei Yongjuncd86e3b2013-11-21 00:38:44 -0300998 snd_card_free(card);
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -0200999
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001000 devno--;
1001}
1002
1003/*
1004 * PCI driver definition
1005 */
1006
1007static struct pci_driver cx88_audio_pci_driver = {
1008 .name = "cx88_audio",
1009 .id_table = cx88_audio_pci_tbl,
1010 .probe = cx88_audio_initdev,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001011 .remove = cx88_audio_finidev,
Mauro Carvalho Chehabb7f355d2006-01-09 15:32:44 -02001012};
1013
Sachin Kamat59963b92013-09-20 05:32:05 -03001014module_pci_driver(cx88_audio_pci_driver);