source: GPL/trunk/alsa-kernel/pci/fm801.c@ 717

Last change on this file since 717 was 717, checked in by David Azarewicz, 3 years ago

Merge changes from next branch.

File size: 42.0 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * The driver for the ForteMedia FM801 based soundcards
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
6
7#include <linux/delay.h>
8#include <linux/init.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/pci.h>
12#include <linux/slab.h>
13#include <linux/module.h>
14#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/tlv.h>
17#include <sound/ac97_codec.h>
18#include <sound/mpu401.h>
19#include <sound/opl3.h>
20#include <sound/initval.h>
21
22#ifdef TARGET_OS2
23#define KBUILD_MODNAME "fm801"
24#endif
25
26#ifdef CONFIG_SND_FM801_TEA575X_BOOL
27#include <media/drv-intf/tea575x.h>
28#endif
29
30MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
31MODULE_DESCRIPTION("ForteMedia FM801");
32MODULE_LICENSE("GPL");
33
34static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
35static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
36static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
37/*
38 * Enable TEA575x tuner
39 * 1 = MediaForte 256-PCS
40 * 2 = MediaForte 256-PCP
41 * 3 = MediaForte 64-PCR
42 * 16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
43 * High 16-bits are video (radio) device number + 1
44 */
45static int tea575x_tuner[SNDRV_CARDS];
46#ifndef TARGET_OS2
47static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
48#else
49static int radio_nr[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
50#endif
51
52module_param_array(index, int, NULL, 0444);
53MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
54module_param_array(id, charp, NULL, 0444);
55MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
56module_param_array(enable, bool, NULL, 0444);
57MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
58module_param_array(tea575x_tuner, int, NULL, 0444);
59MODULE_PARM_DESC(tea575x_tuner, "TEA575x tuner access method (0 = auto, 1 = SF256-PCS, 2=SF256-PCP, 3=SF64-PCR, 8=disable, +16=tuner-only).");
60module_param_array(radio_nr, int, NULL, 0444);
61MODULE_PARM_DESC(radio_nr, "Radio device numbers");
62
63
64#define TUNER_DISABLED (1<<3)
65#define TUNER_ONLY (1<<4)
66#define TUNER_TYPE_MASK (~TUNER_ONLY & 0xFFFF)
67
68/*
69 * Direct registers
70 */
71
72#define fm801_writew(chip,reg,value) outw((value), chip->port + FM801_##reg)
73#define fm801_readw(chip,reg) inw(chip->port + FM801_##reg)
74
75#define fm801_writel(chip,reg,value) outl((value), chip->port + FM801_##reg)
76
77#define FM801_PCM_VOL 0x00 /* PCM Output Volume */
78#define FM801_FM_VOL 0x02 /* FM Output Volume */
79#define FM801_I2S_VOL 0x04 /* I2S Volume */
80#define FM801_REC_SRC 0x06 /* Record Source */
81#define FM801_PLY_CTRL 0x08 /* Playback Control */
82#define FM801_PLY_COUNT 0x0a /* Playback Count */
83#define FM801_PLY_BUF1 0x0c /* Playback Bufer I */
84#define FM801_PLY_BUF2 0x10 /* Playback Buffer II */
85#define FM801_CAP_CTRL 0x14 /* Capture Control */
86#define FM801_CAP_COUNT 0x16 /* Capture Count */
87#define FM801_CAP_BUF1 0x18 /* Capture Buffer I */
88#define FM801_CAP_BUF2 0x1c /* Capture Buffer II */
89#define FM801_CODEC_CTRL 0x22 /* Codec Control */
90#define FM801_I2S_MODE 0x24 /* I2S Mode Control */
91#define FM801_VOLUME 0x26 /* Volume Up/Down/Mute Status */
92#define FM801_I2C_CTRL 0x29 /* I2C Control */
93#define FM801_AC97_CMD 0x2a /* AC'97 Command */
94#define FM801_AC97_DATA 0x2c /* AC'97 Data */
95#define FM801_MPU401_DATA 0x30 /* MPU401 Data */
96#define FM801_MPU401_CMD 0x31 /* MPU401 Command */
97#define FM801_GPIO_CTRL 0x52 /* General Purpose I/O Control */
98#define FM801_GEN_CTRL 0x54 /* General Control */
99#define FM801_IRQ_MASK 0x56 /* Interrupt Mask */
100#define FM801_IRQ_STATUS 0x5a /* Interrupt Status */
101#define FM801_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
102#define FM801_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
103#define FM801_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
104#define FM801_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
105#define FM801_POWERDOWN 0x70 /* Blocks Power Down Control */
106
107/* codec access */
108#define FM801_AC97_READ (1<<7) /* read=1, write=0 */
109#define FM801_AC97_VALID (1<<8) /* port valid=1 */
110#define FM801_AC97_BUSY (1<<9) /* busy=1 */
111#define FM801_AC97_ADDR_SHIFT 10 /* codec id (2bit) */
112
113/* playback and record control register bits */
114#define FM801_BUF1_LAST (1<<1)
115#define FM801_BUF2_LAST (1<<2)
116#define FM801_START (1<<5)
117#define FM801_PAUSE (1<<6)
118#define FM801_IMMED_STOP (1<<7)
119#define FM801_RATE_SHIFT 8
120#define FM801_RATE_MASK (15 << FM801_RATE_SHIFT)
121#define FM801_CHANNELS_4 (1<<12) /* playback only */
122#define FM801_CHANNELS_6 (2<<12) /* playback only */
123#define FM801_CHANNELS_6MS (3<<12) /* playback only */
124#define FM801_CHANNELS_MASK (3<<12)
125#define FM801_16BIT (1<<14)
126#define FM801_STEREO (1<<15)
127
128/* IRQ status bits */
129#define FM801_IRQ_PLAYBACK (1<<8)
130#define FM801_IRQ_CAPTURE (1<<9)
131#define FM801_IRQ_VOLUME (1<<14)
132#define FM801_IRQ_MPU (1<<15)
133
134/* GPIO control register */
135#define FM801_GPIO_GP0 (1<<0) /* read/write */
136#define FM801_GPIO_GP1 (1<<1)
137#define FM801_GPIO_GP2 (1<<2)
138#define FM801_GPIO_GP3 (1<<3)
139#define FM801_GPIO_GP(x) (1<<(0+(x)))
140#define FM801_GPIO_GD0 (1<<8) /* directions: 1 = input, 0 = output*/
141#define FM801_GPIO_GD1 (1<<9)
142#define FM801_GPIO_GD2 (1<<10)
143#define FM801_GPIO_GD3 (1<<11)
144#define FM801_GPIO_GD(x) (1<<(8+(x)))
145#define FM801_GPIO_GS0 (1<<12) /* function select: */
146#define FM801_GPIO_GS1 (1<<13) /* 1 = GPIO */
147#define FM801_GPIO_GS2 (1<<14) /* 0 = other (S/PDIF, VOL) */
148#define FM801_GPIO_GS3 (1<<15)
149#define FM801_GPIO_GS(x) (1<<(12+(x)))
150
151/**
152 * struct fm801 - describes FM801 chip
153 * @dev: device for this chio
154 * @irq: irq number
155 * @port: I/O port number
156 * @multichannel: multichannel support
157 * @secondary: secondary codec
158 * @secondary_addr: address of the secondary codec
159 * @tea575x_tuner: tuner access method & flags
160 * @ply_ctrl: playback control
161 * @cap_ctrl: capture control
162 * @ply_buffer: playback buffer
163 * @ply_buf: playback buffer index
164 * @ply_count: playback buffer count
165 * @ply_size: playback buffer size
166 * @ply_pos: playback position
167 * @cap_buffer: capture buffer
168 * @cap_buf: capture buffer index
169 * @cap_count: capture buffer count
170 * @cap_size: capture buffer size
171 * @cap_pos: capture position
172 * @ac97_bus: ac97 bus handle
173 * @ac97: ac97 handle
174 * @ac97_sec: ac97 secondary handle
175 * @card: ALSA card
176 * @pcm: PCM devices
177 * @rmidi: rmidi device
178 * @playback_substream: substream for playback
179 * @capture_substream: substream for capture
180 * @p_dma_size: playback DMA size
181 * @c_dma_size: capture DMA size
182 * @reg_lock: lock
183 * @proc_entry: /proc entry
184 * @v4l2_dev: v4l2 device
185 * @tea: tea575a structure
186 * @saved_regs: context saved during suspend
187 */
188struct fm801 {
189 struct device *dev;
190 int irq;
191
192 unsigned long port;
193 unsigned int multichannel: 1,
194 secondary: 1;
195 unsigned char secondary_addr;
196 unsigned int tea575x_tuner;
197
198 unsigned short ply_ctrl;
199 unsigned short cap_ctrl;
200
201 unsigned long ply_buffer;
202 unsigned int ply_buf;
203 unsigned int ply_count;
204 unsigned int ply_size;
205 unsigned int ply_pos;
206
207 unsigned long cap_buffer;
208 unsigned int cap_buf;
209 unsigned int cap_count;
210 unsigned int cap_size;
211 unsigned int cap_pos;
212
213 struct snd_ac97_bus *ac97_bus;
214 struct snd_ac97 *ac97;
215 struct snd_ac97 *ac97_sec;
216
217 struct snd_card *card;
218 struct snd_pcm *pcm;
219 struct snd_rawmidi *rmidi;
220 struct snd_pcm_substream *playback_substream;
221 struct snd_pcm_substream *capture_substream;
222 unsigned int p_dma_size;
223 unsigned int c_dma_size;
224
225 spinlock_t reg_lock;
226 struct snd_info_entry *proc_entry;
227
228#ifdef CONFIG_SND_FM801_TEA575X_BOOL
229 struct v4l2_device v4l2_dev;
230 struct snd_tea575x tea;
231#endif
232
233#ifdef CONFIG_PM_SLEEP
234 u16 saved_regs[0x20];
235#endif
236};
237
238/*
239 * IO accessors
240 */
241
242static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
243{
244 outw(value, chip->port + offset);
245}
246
247static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
248{
249 return inw(chip->port + offset);
250}
251
252static const struct pci_device_id snd_fm801_ids[] = {
253 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
254 { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* Gallant Odyssey Sound 4 */
255 { 0, }
256};
257
258MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
259
260/*
261 * common I/O routines
262 */
263
264static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
265{
266 unsigned int idx;
267
268 for (idx = 0; idx < iterations; idx++) {
269 if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
270 return true;
271 udelay(10);
272 }
273 return false;
274}
275
276static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
277{
278 unsigned int idx;
279
280 for (idx = 0; idx < iterations; idx++) {
281 if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
282 return true;
283 udelay(10);
284 }
285 return false;
286}
287
288static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
289 unsigned short mask, unsigned short value)
290{
291 int change;
292 unsigned long flags;
293 unsigned short old, new;
294
295 spin_lock_irqsave(&chip->reg_lock, flags);
296 old = fm801_ioread16(chip, reg);
297 new = (old & ~mask) | value;
298 change = old != new;
299 if (change)
300 fm801_iowrite16(chip, reg, new);
301 spin_unlock_irqrestore(&chip->reg_lock, flags);
302 return change;
303}
304
305static void snd_fm801_codec_write(struct snd_ac97 *ac97,
306 unsigned short reg,
307 unsigned short val)
308{
309 struct fm801 *chip = ac97->private_data;
310
311 /*
312 * Wait until the codec interface is not ready..
313 */
314 if (!fm801_ac97_is_ready(chip, 100)) {
315 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
316 return;
317 }
318
319 /* write data and address */
320 fm801_writew(chip, AC97_DATA, val);
321 fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
322 /*
323 * Wait until the write command is not completed..
324 */
325 if (!fm801_ac97_is_ready(chip, 1000))
326 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
327 ac97->num);
328}
329
330static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
331{
332 struct fm801 *chip = ac97->private_data;
333
334 /*
335 * Wait until the codec interface is not ready..
336 */
337 if (!fm801_ac97_is_ready(chip, 100)) {
338 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
339 return 0;
340 }
341
342 /* read command */
343 fm801_writew(chip, AC97_CMD,
344 reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
345 if (!fm801_ac97_is_ready(chip, 100)) {
346 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
347 ac97->num);
348 return 0;
349 }
350
351 if (!fm801_ac97_is_valid(chip, 1000)) {
352 dev_err(chip->card->dev,
353 "AC'97 interface #%d is not valid (2)\n", ac97->num);
354 return 0;
355 }
356
357 return fm801_readw(chip, AC97_DATA);
358}
359
360static const unsigned int rates[] = {
361 5500, 8000, 9600, 11025,
362 16000, 19200, 22050, 32000,
363 38400, 44100, 48000
364};
365
366static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
367 .count = ARRAY_SIZE(rates),
368 .list = rates,
369 .mask = 0,
370};
371
372static const unsigned int channels[] = {
373 2, 4, 6
374};
375
376static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
377 .count = ARRAY_SIZE(channels),
378 .list = channels,
379 .mask = 0,
380};
381
382/*
383 * Sample rate routines
384 */
385
386static unsigned short snd_fm801_rate_bits(unsigned int rate)
387{
388 unsigned int idx;
389
390 for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
391 if (rates[idx] == rate)
392 return idx;
393 snd_BUG();
394 return ARRAY_SIZE(rates) - 1;
395}
396
397/*
398 * PCM part
399 */
400
401static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
402 int cmd)
403{
404 struct fm801 *chip = snd_pcm_substream_chip(substream);
405
406 spin_lock(&chip->reg_lock);
407 switch (cmd) {
408 case SNDRV_PCM_TRIGGER_START:
409 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
410 FM801_BUF2_LAST |
411 FM801_PAUSE);
412 chip->ply_ctrl |= FM801_START |
413 FM801_IMMED_STOP;
414 break;
415 case SNDRV_PCM_TRIGGER_STOP:
416 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
417 break;
418 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
419 case SNDRV_PCM_TRIGGER_SUSPEND:
420 chip->ply_ctrl |= FM801_PAUSE;
421 break;
422 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
423 case SNDRV_PCM_TRIGGER_RESUME:
424 chip->ply_ctrl &= ~FM801_PAUSE;
425 break;
426 default:
427 spin_unlock(&chip->reg_lock);
428 snd_BUG();
429 return -EINVAL;
430 }
431 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
432 spin_unlock(&chip->reg_lock);
433 return 0;
434}
435
436static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
437 int cmd)
438{
439 struct fm801 *chip = snd_pcm_substream_chip(substream);
440
441 spin_lock(&chip->reg_lock);
442 switch (cmd) {
443 case SNDRV_PCM_TRIGGER_START:
444 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
445 FM801_BUF2_LAST |
446 FM801_PAUSE);
447 chip->cap_ctrl |= FM801_START |
448 FM801_IMMED_STOP;
449 break;
450 case SNDRV_PCM_TRIGGER_STOP:
451 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
452 break;
453 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
454 case SNDRV_PCM_TRIGGER_SUSPEND:
455 chip->cap_ctrl |= FM801_PAUSE;
456 break;
457 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
458 case SNDRV_PCM_TRIGGER_RESUME:
459 chip->cap_ctrl &= ~FM801_PAUSE;
460 break;
461 default:
462 spin_unlock(&chip->reg_lock);
463 snd_BUG();
464 return -EINVAL;
465 }
466 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
467 spin_unlock(&chip->reg_lock);
468 return 0;
469}
470
471static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
472{
473 struct fm801 *chip = snd_pcm_substream_chip(substream);
474 struct snd_pcm_runtime *runtime = substream->runtime;
475
476 chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
477 chip->ply_count = snd_pcm_lib_period_bytes(substream);
478 spin_lock_irq(&chip->reg_lock);
479 chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
480 FM801_STEREO | FM801_RATE_MASK |
481 FM801_CHANNELS_MASK);
482 if (snd_pcm_format_width(runtime->format) == 16)
483 chip->ply_ctrl |= FM801_16BIT;
484 if (runtime->channels > 1) {
485 chip->ply_ctrl |= FM801_STEREO;
486 if (runtime->channels == 4)
487 chip->ply_ctrl |= FM801_CHANNELS_4;
488 else if (runtime->channels == 6)
489 chip->ply_ctrl |= FM801_CHANNELS_6;
490 }
491 chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
492 chip->ply_buf = 0;
493 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
494 fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
495 chip->ply_buffer = runtime->dma_addr;
496 chip->ply_pos = 0;
497 fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
498 fm801_writel(chip, PLY_BUF2,
499 chip->ply_buffer + (chip->ply_count % chip->ply_size));
500 spin_unlock_irq(&chip->reg_lock);
501 return 0;
502}
503
504static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
505{
506 struct fm801 *chip = snd_pcm_substream_chip(substream);
507 struct snd_pcm_runtime *runtime = substream->runtime;
508
509 chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
510 chip->cap_count = snd_pcm_lib_period_bytes(substream);
511 spin_lock_irq(&chip->reg_lock);
512 chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
513 FM801_STEREO | FM801_RATE_MASK);
514 if (snd_pcm_format_width(runtime->format) == 16)
515 chip->cap_ctrl |= FM801_16BIT;
516 if (runtime->channels > 1)
517 chip->cap_ctrl |= FM801_STEREO;
518 chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
519 chip->cap_buf = 0;
520 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
521 fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
522 chip->cap_buffer = runtime->dma_addr;
523 chip->cap_pos = 0;
524 fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
525 fm801_writel(chip, CAP_BUF2,
526 chip->cap_buffer + (chip->cap_count % chip->cap_size));
527 spin_unlock_irq(&chip->reg_lock);
528 return 0;
529}
530
531static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
532{
533 struct fm801 *chip = snd_pcm_substream_chip(substream);
534 size_t ptr;
535
536 if (!(chip->ply_ctrl & FM801_START))
537 return 0;
538 spin_lock(&chip->reg_lock);
539 ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
540 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
541 ptr += chip->ply_count;
542 ptr %= chip->ply_size;
543 }
544 spin_unlock(&chip->reg_lock);
545 return bytes_to_frames(substream->runtime, ptr);
546}
547
548static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
549{
550 struct fm801 *chip = snd_pcm_substream_chip(substream);
551 size_t ptr;
552
553 if (!(chip->cap_ctrl & FM801_START))
554 return 0;
555 spin_lock(&chip->reg_lock);
556 ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
557 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
558 ptr += chip->cap_count;
559 ptr %= chip->cap_size;
560 }
561 spin_unlock(&chip->reg_lock);
562 return bytes_to_frames(substream->runtime, ptr);
563}
564
565static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
566{
567 struct fm801 *chip = dev_id;
568 unsigned short status;
569 unsigned int tmp;
570
571 status = fm801_readw(chip, IRQ_STATUS);
572 status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
573 if (! status)
574 return IRQ_NONE;
575 /* ack first */
576 fm801_writew(chip, IRQ_STATUS, status);
577 if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
578 spin_lock(&chip->reg_lock);
579 chip->ply_buf++;
580 chip->ply_pos += chip->ply_count;
581 chip->ply_pos %= chip->ply_size;
582 tmp = chip->ply_pos + chip->ply_count;
583 tmp %= chip->ply_size;
584 if (chip->ply_buf & 1)
585 fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
586 else
587 fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
588 spin_unlock(&chip->reg_lock);
589 snd_pcm_period_elapsed(chip->playback_substream);
590 }
591 if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
592 spin_lock(&chip->reg_lock);
593 chip->cap_buf++;
594 chip->cap_pos += chip->cap_count;
595 chip->cap_pos %= chip->cap_size;
596 tmp = chip->cap_pos + chip->cap_count;
597 tmp %= chip->cap_size;
598 if (chip->cap_buf & 1)
599 fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
600 else
601 fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
602 spin_unlock(&chip->reg_lock);
603 snd_pcm_period_elapsed(chip->capture_substream);
604 }
605 if (chip->rmidi && (status & FM801_IRQ_MPU))
606 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
607 if (status & FM801_IRQ_VOLUME) {
608 /* TODO */
609 }
610
611 return IRQ_HANDLED;
612}
613
614static const struct snd_pcm_hardware snd_fm801_playback =
615{
616 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
617 SNDRV_PCM_INFO_BLOCK_TRANSFER |
618 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
619 SNDRV_PCM_INFO_MMAP_VALID),
620 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
621 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
622 .rate_min = 5500,
623 .rate_max = 48000,
624 .channels_min = 1,
625 .channels_max = 2,
626 .buffer_bytes_max = (128*1024),
627 .period_bytes_min = 64,
628 .period_bytes_max = (128*1024),
629 .periods_min = 1,
630 .periods_max = 1024,
631 .fifo_size = 0,
632};
633
634static const struct snd_pcm_hardware snd_fm801_capture =
635{
636 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
637 SNDRV_PCM_INFO_BLOCK_TRANSFER |
638 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
639 SNDRV_PCM_INFO_MMAP_VALID),
640 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
641 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
642 .rate_min = 5500,
643 .rate_max = 48000,
644 .channels_min = 1,
645 .channels_max = 2,
646 .buffer_bytes_max = (128*1024),
647 .period_bytes_min = 64,
648 .period_bytes_max = (128*1024),
649 .periods_min = 1,
650 .periods_max = 1024,
651 .fifo_size = 0,
652};
653
654static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
655{
656 struct fm801 *chip = snd_pcm_substream_chip(substream);
657 struct snd_pcm_runtime *runtime = substream->runtime;
658 int err;
659
660 chip->playback_substream = substream;
661 runtime->hw = snd_fm801_playback;
662 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
663 &hw_constraints_rates);
664 if (chip->multichannel) {
665 runtime->hw.channels_max = 6;
666 snd_pcm_hw_constraint_list(runtime, 0,
667 SNDRV_PCM_HW_PARAM_CHANNELS,
668 &hw_constraints_channels);
669 }
670 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
671 if (err < 0)
672 return err;
673 return 0;
674}
675
676static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
677{
678 struct fm801 *chip = snd_pcm_substream_chip(substream);
679 struct snd_pcm_runtime *runtime = substream->runtime;
680 int err;
681
682 chip->capture_substream = substream;
683 runtime->hw = snd_fm801_capture;
684 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
685 &hw_constraints_rates);
686 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
687 if (err < 0)
688 return err;
689 return 0;
690}
691
692static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
693{
694 struct fm801 *chip = snd_pcm_substream_chip(substream);
695
696 chip->playback_substream = NULL;
697 return 0;
698}
699
700static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
701{
702 struct fm801 *chip = snd_pcm_substream_chip(substream);
703
704 chip->capture_substream = NULL;
705 return 0;
706}
707
708static const struct snd_pcm_ops snd_fm801_playback_ops = {
709 .open = snd_fm801_playback_open,
710 .close = snd_fm801_playback_close,
711 .prepare = snd_fm801_playback_prepare,
712 .trigger = snd_fm801_playback_trigger,
713 .pointer = snd_fm801_playback_pointer,
714};
715
716static const struct snd_pcm_ops snd_fm801_capture_ops = {
717 .open = snd_fm801_capture_open,
718 .close = snd_fm801_capture_close,
719 .prepare = snd_fm801_capture_prepare,
720 .trigger = snd_fm801_capture_trigger,
721 .pointer = snd_fm801_capture_pointer,
722};
723
724static int snd_fm801_pcm(struct fm801 *chip, int device)
725{
726 struct pci_dev *pdev = to_pci_dev(chip->dev);
727 struct snd_pcm *pcm;
728 int err;
729
730 err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm);
731 if (err < 0)
732 return err;
733
734 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
735 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
736
737 pcm->private_data = chip;
738 pcm->info_flags = 0;
739 strcpy(pcm->name, "FM801");
740 chip->pcm = pcm;
741
742 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
743 chip->multichannel ? 128*1024 : 64*1024, 128*1024);
744
745 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
746 snd_pcm_alt_chmaps,
747 chip->multichannel ? 6 : 2, 0,
748 NULL);
749}
750
751/*
752 * TEA5757 radio
753 */
754
755#ifdef CONFIG_SND_FM801_TEA575X_BOOL
756
757/* GPIO to TEA575x maps */
758struct snd_fm801_tea575x_gpio {
759 u8 data, clk, wren, most;
760 char *name;
761};
762
763static const struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
764 { .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
765 { .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
766 { .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
767};
768
769#define get_tea575x_gpio(chip) \
770 (&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
771
772static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
773{
774 struct fm801 *chip = tea->private_data;
775 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
776 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
777
778 reg &= ~(FM801_GPIO_GP(gpio.data) |
779 FM801_GPIO_GP(gpio.clk) |
780 FM801_GPIO_GP(gpio.wren));
781
782 reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
783 reg |= (pins & TEA575X_CLK) ? FM801_GPIO_GP(gpio.clk) : 0;
784 /* WRITE_ENABLE is inverted */
785 reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
786
787 fm801_writew(chip, GPIO_CTRL, reg);
788}
789
790static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
791{
792 struct fm801 *chip = tea->private_data;
793 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
794 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
795 u8 ret;
796
797 ret = 0;
798 if (reg & FM801_GPIO_GP(gpio.data))
799 ret |= TEA575X_DATA;
800 if (reg & FM801_GPIO_GP(gpio.most))
801 ret |= TEA575X_MOST;
802 return ret;
803}
804
805static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
806{
807 struct fm801 *chip = tea->private_data;
808 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
809 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
810
811 /* use GPIO lines and set write enable bit */
812 reg |= FM801_GPIO_GS(gpio.data) |
813 FM801_GPIO_GS(gpio.wren) |
814 FM801_GPIO_GS(gpio.clk) |
815 FM801_GPIO_GS(gpio.most);
816 if (output) {
817 /* all of lines are in the write direction */
818 /* clear data and clock lines */
819 reg &= ~(FM801_GPIO_GD(gpio.data) |
820 FM801_GPIO_GD(gpio.wren) |
821 FM801_GPIO_GD(gpio.clk) |
822 FM801_GPIO_GP(gpio.data) |
823 FM801_GPIO_GP(gpio.clk) |
824 FM801_GPIO_GP(gpio.wren));
825 } else {
826 /* use GPIO lines, set data direction to input */
827 reg |= FM801_GPIO_GD(gpio.data) |
828 FM801_GPIO_GD(gpio.most) |
829 FM801_GPIO_GP(gpio.data) |
830 FM801_GPIO_GP(gpio.most) |
831 FM801_GPIO_GP(gpio.wren);
832 /* all of lines are in the write direction, except data */
833 /* clear data, write enable and clock lines */
834 reg &= ~(FM801_GPIO_GD(gpio.wren) |
835 FM801_GPIO_GD(gpio.clk) |
836 FM801_GPIO_GP(gpio.clk));
837 }
838
839 fm801_writew(chip, GPIO_CTRL, reg);
840}
841
842static const struct snd_tea575x_ops snd_fm801_tea_ops = {
843 .set_pins = snd_fm801_tea575x_set_pins,
844 .get_pins = snd_fm801_tea575x_get_pins,
845 .set_direction = snd_fm801_tea575x_set_direction,
846};
847#endif
848
849/*
850 * Mixer routines
851 */
852
853#define FM801_SINGLE(xname, reg, shift, mask, invert) \
854{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
855 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
856 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
857
858static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
859 struct snd_ctl_elem_info *uinfo)
860{
861 int mask = (kcontrol->private_value >> 16) & 0xff;
862
863 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
864 uinfo->count = 1;
865 uinfo->value.integer.min = 0;
866 uinfo->value.integer.max = mask;
867 return 0;
868}
869
870static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
871 struct snd_ctl_elem_value *ucontrol)
872{
873 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
874 int reg = kcontrol->private_value & 0xff;
875 int shift = (kcontrol->private_value >> 8) & 0xff;
876 int mask = (kcontrol->private_value >> 16) & 0xff;
877 int invert = (kcontrol->private_value >> 24) & 0xff;
878 long *value = ucontrol->value.integer.value;
879
880 value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
881 if (invert)
882 value[0] = mask - value[0];
883 return 0;
884}
885
886static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
887 struct snd_ctl_elem_value *ucontrol)
888{
889 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
890 int reg = kcontrol->private_value & 0xff;
891 int shift = (kcontrol->private_value >> 8) & 0xff;
892 int mask = (kcontrol->private_value >> 16) & 0xff;
893 int invert = (kcontrol->private_value >> 24) & 0xff;
894 unsigned short val;
895
896 val = (ucontrol->value.integer.value[0] & mask);
897 if (invert)
898 val = mask - val;
899 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
900}
901
902#define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
903{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
904 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
905 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
906#define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
907{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
908 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
909 .name = xname, .info = snd_fm801_info_double, \
910 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
911 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
912 .tlv = { .p = (xtlv) } }
913
914static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
915 struct snd_ctl_elem_info *uinfo)
916{
917 int mask = (kcontrol->private_value >> 16) & 0xff;
918
919 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
920 uinfo->count = 2;
921 uinfo->value.integer.min = 0;
922 uinfo->value.integer.max = mask;
923 return 0;
924}
925
926static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
927 struct snd_ctl_elem_value *ucontrol)
928{
929 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
930 int reg = kcontrol->private_value & 0xff;
931 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
932 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
933 int mask = (kcontrol->private_value >> 16) & 0xff;
934 int invert = (kcontrol->private_value >> 24) & 0xff;
935 long *value = ucontrol->value.integer.value;
936
937 spin_lock_irq(&chip->reg_lock);
938 value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
939 value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
940 spin_unlock_irq(&chip->reg_lock);
941 if (invert) {
942 value[0] = mask - value[0];
943 value[1] = mask - value[1];
944 }
945 return 0;
946}
947
948static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
949 struct snd_ctl_elem_value *ucontrol)
950{
951 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
952 int reg = kcontrol->private_value & 0xff;
953 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
954 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
955 int mask = (kcontrol->private_value >> 16) & 0xff;
956 int invert = (kcontrol->private_value >> 24) & 0xff;
957 unsigned short val1, val2;
958
959 val1 = ucontrol->value.integer.value[0] & mask;
960 val2 = ucontrol->value.integer.value[1] & mask;
961 if (invert) {
962 val1 = mask - val1;
963 val2 = mask - val2;
964 }
965 return snd_fm801_update_bits(chip, reg,
966 (mask << shift_left) | (mask << shift_right),
967 (val1 << shift_left ) | (val2 << shift_right));
968}
969
970static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
971 struct snd_ctl_elem_info *uinfo)
972{
973 static const char * const texts[5] = {
974 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
975 };
976
977 return snd_ctl_enum_info(uinfo, 1, 5, texts);
978}
979
980static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
981 struct snd_ctl_elem_value *ucontrol)
982{
983 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
984 unsigned short val;
985
986 val = fm801_readw(chip, REC_SRC) & 7;
987 if (val > 4)
988 val = 4;
989 ucontrol->value.enumerated.item[0] = val;
990 return 0;
991}
992
993static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
994 struct snd_ctl_elem_value *ucontrol)
995{
996 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
997 unsigned short val;
998
999 val = ucontrol->value.enumerated.item[0];
1000 if (val > 4)
1001 return -EINVAL;
1002 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
1003}
1004
1005static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
1006
1007#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
1008
1009static const struct snd_kcontrol_new snd_fm801_controls[] = {
1010FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1011 db_scale_dsp),
1012FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1013FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1014 db_scale_dsp),
1015FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1016FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1017 db_scale_dsp),
1018FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
1019{
1020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1021 .name = "Digital Capture Source",
1022 .info = snd_fm801_info_mux,
1023 .get = snd_fm801_get_mux,
1024 .put = snd_fm801_put_mux,
1025}
1026};
1027
1028#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
1029
1030static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
1031FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
1032FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
1033FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
1034FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
1035FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
1036FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
1037};
1038
1039static int snd_fm801_mixer(struct fm801 *chip)
1040{
1041 struct snd_ac97_template ac97;
1042 unsigned int i;
1043 int err;
1044 static const struct snd_ac97_bus_ops ops = {
1045 .write = snd_fm801_codec_write,
1046 .read = snd_fm801_codec_read,
1047 };
1048
1049 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1050 if (err < 0)
1051 return err;
1052
1053 memset(&ac97, 0, sizeof(ac97));
1054 ac97.private_data = chip;
1055 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1056 if (err < 0)
1057 return err;
1058 if (chip->secondary) {
1059 ac97.num = 1;
1060 ac97.addr = chip->secondary_addr;
1061 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
1062 if (err < 0)
1063 return err;
1064 }
1065 for (i = 0; i < FM801_CONTROLS; i++) {
1066 err = snd_ctl_add(chip->card,
1067 snd_ctl_new1(&snd_fm801_controls[i], chip));
1068 if (err < 0)
1069 return err;
1070 }
1071 if (chip->multichannel) {
1072 for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1073 err = snd_ctl_add(chip->card,
1074 snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1075 if (err < 0)
1076 return err;
1077 }
1078 }
1079 return 0;
1080}
1081
1082/*
1083 * initialization routines
1084 */
1085
1086static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1087 unsigned short reg, unsigned long waits)
1088{
1089 unsigned long timeout = jiffies + waits;
1090
1091 fm801_writew(chip, AC97_CMD,
1092 reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1093 udelay(5);
1094 do {
1095 if ((fm801_readw(chip, AC97_CMD) &
1096 (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1097 return 0;
1098 schedule_timeout_uninterruptible(1);
1099 } while (time_after(timeout, jiffies));
1100 return -EIO;
1101}
1102
1103static int reset_codec(struct fm801 *chip)
1104{
1105 /* codec cold reset + AC'97 warm reset */
1106 fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1107 fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1108 udelay(100);
1109 fm801_writew(chip, CODEC_CTRL, 0);
1110
1111 return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1112}
1113
1114static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1115{
1116 unsigned short cmdw;
1117
1118 if (chip->multichannel) {
1119 if (chip->secondary_addr) {
1120 wait_for_codec(chip, chip->secondary_addr,
1121 AC97_VENDOR_ID1, msecs_to_jiffies(50));
1122 } else {
1123 /* my card has the secondary codec */
1124 /* at address #3, so the loop is inverted */
1125 int i;
1126 for (i = 3; i > 0; i--) {
1127 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1128 msecs_to_jiffies(50))) {
1129 cmdw = fm801_readw(chip, AC97_DATA);
1130 if (cmdw != 0xffff && cmdw != 0) {
1131 chip->secondary = 1;
1132 chip->secondary_addr = i;
1133 break;
1134 }
1135 }
1136 }
1137 }
1138
1139 /* the recovery phase, it seems that probing for non-existing codec might */
1140 /* cause timeout problems */
1141 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1142 }
1143}
1144
1145static void snd_fm801_chip_init(struct fm801 *chip)
1146{
1147 unsigned short cmdw;
1148
1149 /* init volume */
1150 fm801_writew(chip, PCM_VOL, 0x0808);
1151 fm801_writew(chip, FM_VOL, 0x9f1f);
1152 fm801_writew(chip, I2S_VOL, 0x8808);
1153
1154 /* I2S control - I2S mode */
1155 fm801_writew(chip, I2S_MODE, 0x0003);
1156
1157 /* interrupt setup */
1158 cmdw = fm801_readw(chip, IRQ_MASK);
1159 if (chip->irq < 0)
1160 cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
1161 else
1162 cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
1163 fm801_writew(chip, IRQ_MASK, cmdw);
1164
1165 /* interrupt clear */
1166 fm801_writew(chip, IRQ_STATUS,
1167 FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1168}
1169
1170static void snd_fm801_free(struct snd_card *card)
1171{
1172 struct fm801 *chip = card->private_data;
1173 unsigned short cmdw;
1174
1175 /* interrupt setup - mask everything */
1176 cmdw = fm801_readw(chip, IRQ_MASK);
1177 cmdw |= 0x00c3;
1178 fm801_writew(chip, IRQ_MASK, cmdw);
1179
1180#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1181 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1182 snd_tea575x_exit(&chip->tea);
1183 v4l2_device_unregister(&chip->v4l2_dev);
1184 }
1185#endif
1186}
1187
1188
1189static int snd_fm801_create(struct snd_card *card,
1190 struct pci_dev *pci,
1191 int tea575x_tuner,
1192 int radio_nr)
1193{
1194 struct fm801 *chip = card->private_data;
1195 int err;
1196
1197#ifndef TARGET_OS2
1198 err = pcim_enable_device(pci);
1199 if (err < 0)
1200 return err;
1201#else
1202 if ((err = pci_enable_device(pci)) < 0)
1203 return err;
1204#endif
1205 spin_lock_init(&chip->reg_lock);
1206 chip->card = card;
1207 chip->dev = &pci->dev;
1208 chip->irq = -1;
1209 chip->tea575x_tuner = tea575x_tuner;
1210 err = pci_request_regions(pci, "FM801");
1211 if (err < 0)
1212 return err;
1213 chip->port = pci_resource_start(pci, 0);
1214
1215#ifndef TARGET_OS2
1216 if (pci->revision >= 0xb1) /* FM801-AU */
1217#else
1218 if (snd_pci_revision(pci) >= 0xb1) /* FM801-AU */
1219#endif
1220 chip->multichannel = 1;
1221
1222 if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1223 if (reset_codec(chip) < 0) {
1224 dev_info(chip->card->dev,
1225 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1226 chip->tea575x_tuner = 3 | TUNER_ONLY;
1227 } else {
1228 snd_fm801_chip_multichannel_init(chip);
1229 }
1230 }
1231
1232 if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
1233#ifndef TARGET_OS2
1234 if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
1235 IRQF_SHARED, KBUILD_MODNAME, chip)) {
1236#else
1237 if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1238 KBUILD_MODNAME, chip)) {
1239#endif
1240 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1241 return -EBUSY;
1242 }
1243 chip->irq = pci->irq;
1244 card->sync_irq = chip->irq;
1245 pci_set_master(pci);
1246 }
1247
1248 card->private_free = snd_fm801_free;
1249 snd_fm801_chip_init(chip);
1250
1251#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1252 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1253 if (err < 0)
1254 return err;
1255 chip->tea.v4l2_dev = &chip->v4l2_dev;
1256 chip->tea.radio_nr = radio_nr;
1257 chip->tea.private_data = chip;
1258 chip->tea.ops = &snd_fm801_tea_ops;
1259 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1260 if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1261 (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1262 if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1263 dev_err(card->dev, "TEA575x radio not found\n");
1264 return -ENODEV;
1265 }
1266 } else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1267 unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1268
1269 /* autodetect tuner connection */
1270 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1271 chip->tea575x_tuner = tea575x_tuner;
1272 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1273 dev_info(card->dev,
1274 "detected TEA575x radio type %s\n",
1275 get_tea575x_gpio(chip)->name);
1276 break;
1277 }
1278 }
1279 if (tea575x_tuner == 4) {
1280 dev_err(card->dev, "TEA575x radio not found\n");
1281 chip->tea575x_tuner = TUNER_DISABLED;
1282 }
1283
1284 chip->tea575x_tuner |= tuner_only;
1285 }
1286 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1287 strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1288 sizeof(chip->tea.card));
1289 }
1290#endif
1291 return 0;
1292}
1293
1294static int __snd_card_fm801_probe(struct pci_dev *pci,
1295 const struct pci_device_id *pci_id)
1296{
1297 static int dev;
1298 struct snd_card *card;
1299 struct fm801 *chip;
1300 struct snd_opl3 *opl3;
1301 int err;
1302
1303 if (dev >= SNDRV_CARDS)
1304 return -ENODEV;
1305 if (!enable[dev]) {
1306 dev++;
1307 return -ENOENT;
1308 }
1309
1310 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1311 sizeof(*chip), &card);
1312 if (err < 0)
1313 return err;
1314 chip = card->private_data;
1315 err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev]);
1316 if (err < 0)
1317 return err;
1318
1319 strcpy(card->driver, "FM801");
1320 strcpy(card->shortname, "ForteMedia FM801-");
1321 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1322 sprintf(card->longname, "%s at 0x%lx, irq %i",
1323 card->shortname, chip->port, chip->irq);
1324
1325 if (chip->tea575x_tuner & TUNER_ONLY)
1326 goto __fm801_tuner_only;
1327
1328 err = snd_fm801_pcm(chip, 0);
1329 if (err < 0)
1330 return err;
1331 err = snd_fm801_mixer(chip);
1332 if (err < 0)
1333 return err;
1334 err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1335 chip->port + FM801_MPU401_DATA,
1336 MPU401_INFO_INTEGRATED |
1337 MPU401_INFO_IRQ_HOOK,
1338 -1, &chip->rmidi);
1339 if (err < 0)
1340 return err;
1341 err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1342 chip->port + FM801_OPL3_BANK1,
1343 OPL3_HW_OPL3_FM801, 1, &opl3);
1344 if (err < 0)
1345 return err;
1346 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
1347 if (err < 0)
1348 return err;
1349
1350 __fm801_tuner_only:
1351 err = snd_card_register(card);
1352 if (err < 0)
1353 return err;
1354 pci_set_drvdata(pci, card);
1355 dev++;
1356 return 0;
1357}
1358
1359static int snd_card_fm801_probe(struct pci_dev *pci,
1360 const struct pci_device_id *pci_id)
1361{
1362 return snd_card_free_on_error(&pci->dev, __snd_card_fm801_probe(pci, pci_id));
1363}
1364
1365#ifdef CONFIG_PM_SLEEP
1366static const unsigned char saved_regs[] = {
1367 FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1368 FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1369 FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1370 FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1371};
1372
1373static int snd_fm801_suspend(struct device *dev)
1374{
1375 struct snd_card *card = dev_get_drvdata(dev);
1376 struct fm801 *chip = card->private_data;
1377 int i;
1378
1379 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1380
1381 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1382 chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
1383
1384 if (chip->tea575x_tuner & TUNER_ONLY) {
1385 /* FIXME: tea575x suspend */
1386 } else {
1387 snd_ac97_suspend(chip->ac97);
1388 snd_ac97_suspend(chip->ac97_sec);
1389 }
1390
1391 return 0;
1392}
1393
1394static int snd_fm801_resume(struct device *dev)
1395{
1396 struct snd_card *card = dev_get_drvdata(dev);
1397 struct fm801 *chip = card->private_data;
1398 int i;
1399
1400 if (chip->tea575x_tuner & TUNER_ONLY) {
1401 snd_fm801_chip_init(chip);
1402 } else {
1403 reset_codec(chip);
1404 snd_fm801_chip_multichannel_init(chip);
1405 snd_fm801_chip_init(chip);
1406 snd_ac97_resume(chip->ac97);
1407 snd_ac97_resume(chip->ac97_sec);
1408 }
1409
1410 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1411 fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1412
1413#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1414 if (!(chip->tea575x_tuner & TUNER_DISABLED))
1415 snd_tea575x_set_freq(&chip->tea);
1416#endif
1417
1418 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1419 return 0;
1420}
1421
1422static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1423#define SND_FM801_PM_OPS &snd_fm801_pm
1424#else
1425#define SND_FM801_PM_OPS NULL
1426#endif /* CONFIG_PM_SLEEP */
1427
1428static struct pci_driver fm801_driver = {
1429 .name = KBUILD_MODNAME,
1430 .id_table = snd_fm801_ids,
1431 .probe = snd_card_fm801_probe,
1432 .driver = {
1433 .pm = SND_FM801_PM_OPS,
1434 },
1435};
1436
1437module_pci_driver(fm801_driver);
Note: See TracBrowser for help on using the repository browser.