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

Last change on this file since 703 was 703, checked in by David Azarewicz, 4 years ago

Merge changes from next branch.

File size: 43.3 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 void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1040{
1041 struct fm801 *chip = bus->private_data;
1042 chip->ac97_bus = NULL;
1043}
1044
1045static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
1046{
1047 struct fm801 *chip = ac97->private_data;
1048 if (ac97->num == 0) {
1049 chip->ac97 = NULL;
1050 } else {
1051 chip->ac97_sec = NULL;
1052 }
1053}
1054
1055static int snd_fm801_mixer(struct fm801 *chip)
1056{
1057 struct snd_ac97_template ac97;
1058 unsigned int i;
1059 int err;
1060 static const struct snd_ac97_bus_ops ops = {
1061 .write = snd_fm801_codec_write,
1062 .read = snd_fm801_codec_read,
1063 };
1064
1065 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1066 if (err < 0)
1067 return err;
1068 chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
1069
1070 memset(&ac97, 0, sizeof(ac97));
1071 ac97.private_data = chip;
1072 ac97.private_free = snd_fm801_mixer_free_ac97;
1073 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1074 if (err < 0)
1075 return err;
1076 if (chip->secondary) {
1077 ac97.num = 1;
1078 ac97.addr = chip->secondary_addr;
1079 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
1080 if (err < 0)
1081 return err;
1082 }
1083 for (i = 0; i < FM801_CONTROLS; i++) {
1084 err = snd_ctl_add(chip->card,
1085 snd_ctl_new1(&snd_fm801_controls[i], chip));
1086 if (err < 0)
1087 return err;
1088 }
1089 if (chip->multichannel) {
1090 for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1091 err = snd_ctl_add(chip->card,
1092 snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1093 if (err < 0)
1094 return err;
1095 }
1096 }
1097 return 0;
1098}
1099
1100/*
1101 * initialization routines
1102 */
1103
1104static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1105 unsigned short reg, unsigned long waits)
1106{
1107 unsigned long timeout = jiffies + waits;
1108
1109 fm801_writew(chip, AC97_CMD,
1110 reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1111 udelay(5);
1112 do {
1113 if ((fm801_readw(chip, AC97_CMD) &
1114 (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1115 return 0;
1116 schedule_timeout_uninterruptible(1);
1117 } while (time_after(timeout, jiffies));
1118 return -EIO;
1119}
1120
1121static int reset_codec(struct fm801 *chip)
1122{
1123 /* codec cold reset + AC'97 warm reset */
1124 fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1125 fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1126 udelay(100);
1127 fm801_writew(chip, CODEC_CTRL, 0);
1128
1129 return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1130}
1131
1132static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1133{
1134 unsigned short cmdw;
1135
1136 if (chip->multichannel) {
1137 if (chip->secondary_addr) {
1138 wait_for_codec(chip, chip->secondary_addr,
1139 AC97_VENDOR_ID1, msecs_to_jiffies(50));
1140 } else {
1141 /* my card has the secondary codec */
1142 /* at address #3, so the loop is inverted */
1143 int i;
1144 for (i = 3; i > 0; i--) {
1145 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1146 msecs_to_jiffies(50))) {
1147 cmdw = fm801_readw(chip, AC97_DATA);
1148 if (cmdw != 0xffff && cmdw != 0) {
1149 chip->secondary = 1;
1150 chip->secondary_addr = i;
1151 break;
1152 }
1153 }
1154 }
1155 }
1156
1157 /* the recovery phase, it seems that probing for non-existing codec might */
1158 /* cause timeout problems */
1159 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1160 }
1161}
1162
1163static void snd_fm801_chip_init(struct fm801 *chip)
1164{
1165 unsigned short cmdw;
1166
1167 /* init volume */
1168 fm801_writew(chip, PCM_VOL, 0x0808);
1169 fm801_writew(chip, FM_VOL, 0x9f1f);
1170 fm801_writew(chip, I2S_VOL, 0x8808);
1171
1172 /* I2S control - I2S mode */
1173 fm801_writew(chip, I2S_MODE, 0x0003);
1174
1175 /* interrupt setup */
1176 cmdw = fm801_readw(chip, IRQ_MASK);
1177 if (chip->irq < 0)
1178 cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
1179 else
1180 cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
1181 fm801_writew(chip, IRQ_MASK, cmdw);
1182
1183 /* interrupt clear */
1184 fm801_writew(chip, IRQ_STATUS,
1185 FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1186}
1187
1188static int snd_fm801_free(struct fm801 *chip)
1189{
1190 unsigned short cmdw;
1191
1192 if (chip->irq < 0)
1193 goto __end_hw;
1194
1195 /* interrupt setup - mask everything */
1196 cmdw = fm801_readw(chip, IRQ_MASK);
1197 cmdw |= 0x00c3;
1198 fm801_writew(chip, IRQ_MASK, cmdw);
1199
1200 devm_free_irq(chip->dev, chip->irq, chip);
1201
1202 __end_hw:
1203#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1204 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1205 snd_tea575x_exit(&chip->tea);
1206 v4l2_device_unregister(&chip->v4l2_dev);
1207 }
1208#endif
1209 return 0;
1210}
1211
1212static int snd_fm801_dev_free(struct snd_device *device)
1213{
1214 struct fm801 *chip = device->device_data;
1215 return snd_fm801_free(chip);
1216}
1217
1218static int snd_fm801_create(struct snd_card *card,
1219 struct pci_dev *pci,
1220 int tea575x_tuner,
1221 int radio_nr,
1222 struct fm801 **rchip)
1223{
1224 struct fm801 *chip;
1225 int err;
1226 static const struct snd_device_ops ops = {
1227 .dev_free = snd_fm801_dev_free,
1228 };
1229
1230 *rchip = NULL;
1231#ifndef TARGET_OS2
1232 err = pcim_enable_device(pci);
1233 if (err < 0)
1234 return err;
1235 chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
1236#else
1237 if ((err = pci_enable_device(pci)) < 0)
1238 return err;
1239 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1240#endif
1241 if (chip == NULL)
1242 return -ENOMEM;
1243 spin_lock_init(&chip->reg_lock);
1244 chip->card = card;
1245 chip->dev = &pci->dev;
1246 chip->irq = -1;
1247 chip->tea575x_tuner = tea575x_tuner;
1248 err = pci_request_regions(pci, "FM801");
1249 if (err < 0)
1250 return err;
1251 chip->port = pci_resource_start(pci, 0);
1252
1253#ifndef TARGET_OS2
1254 if (pci->revision >= 0xb1) /* FM801-AU */
1255#else
1256 if (snd_pci_revision(pci) >= 0xb1) /* FM801-AU */
1257#endif
1258 chip->multichannel = 1;
1259
1260 if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1261 if (reset_codec(chip) < 0) {
1262 dev_info(chip->card->dev,
1263 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1264 chip->tea575x_tuner = 3 | TUNER_ONLY;
1265 } else {
1266 snd_fm801_chip_multichannel_init(chip);
1267 }
1268 }
1269
1270 if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
1271#ifndef TARGET_OS2
1272 if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
1273 IRQF_SHARED, KBUILD_MODNAME, chip)) {
1274#else
1275 if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1276 KBUILD_MODNAME, chip)) {
1277#endif
1278 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1279 snd_fm801_free(chip);
1280 return -EBUSY;
1281 }
1282 chip->irq = pci->irq;
1283 card->sync_irq = chip->irq;
1284 pci_set_master(pci);
1285 }
1286
1287 snd_fm801_chip_init(chip);
1288
1289 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1290 if (err < 0) {
1291 snd_fm801_free(chip);
1292 return err;
1293 }
1294
1295#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1296 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1297 if (err < 0) {
1298 snd_fm801_free(chip);
1299 return err;
1300 }
1301 chip->tea.v4l2_dev = &chip->v4l2_dev;
1302 chip->tea.radio_nr = radio_nr;
1303 chip->tea.private_data = chip;
1304 chip->tea.ops = &snd_fm801_tea_ops;
1305 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1306 if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1307 (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1308 if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1309 dev_err(card->dev, "TEA575x radio not found\n");
1310 snd_fm801_free(chip);
1311 return -ENODEV;
1312 }
1313 } else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1314 unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1315
1316 /* autodetect tuner connection */
1317 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1318 chip->tea575x_tuner = tea575x_tuner;
1319 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1320 dev_info(card->dev,
1321 "detected TEA575x radio type %s\n",
1322 get_tea575x_gpio(chip)->name);
1323 break;
1324 }
1325 }
1326 if (tea575x_tuner == 4) {
1327 dev_err(card->dev, "TEA575x radio not found\n");
1328 chip->tea575x_tuner = TUNER_DISABLED;
1329 }
1330
1331 chip->tea575x_tuner |= tuner_only;
1332 }
1333 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1334 strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1335 sizeof(chip->tea.card));
1336 }
1337#endif
1338
1339 *rchip = chip;
1340 return 0;
1341}
1342
1343static int snd_card_fm801_probe(struct pci_dev *pci,
1344 const struct pci_device_id *pci_id)
1345{
1346 static int dev;
1347 struct snd_card *card;
1348 struct fm801 *chip;
1349 struct snd_opl3 *opl3;
1350 int err;
1351
1352 if (dev >= SNDRV_CARDS)
1353 return -ENODEV;
1354 if (!enable[dev]) {
1355 dev++;
1356 return -ENOENT;
1357 }
1358
1359 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1360 0, &card);
1361 if (err < 0)
1362 return err;
1363 err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip);
1364 if (err < 0) {
1365 snd_card_free(card);
1366 return err;
1367 }
1368 card->private_data = chip;
1369
1370 strcpy(card->driver, "FM801");
1371 strcpy(card->shortname, "ForteMedia FM801-");
1372 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1373 sprintf(card->longname, "%s at 0x%lx, irq %i",
1374 card->shortname, chip->port, chip->irq);
1375
1376 if (chip->tea575x_tuner & TUNER_ONLY)
1377 goto __fm801_tuner_only;
1378
1379 err = snd_fm801_pcm(chip, 0);
1380 if (err < 0) {
1381 snd_card_free(card);
1382 return err;
1383 }
1384 err = snd_fm801_mixer(chip);
1385 if (err < 0) {
1386 snd_card_free(card);
1387 return err;
1388 }
1389 err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1390 chip->port + FM801_MPU401_DATA,
1391 MPU401_INFO_INTEGRATED |
1392 MPU401_INFO_IRQ_HOOK,
1393 -1, &chip->rmidi);
1394 if (err < 0) {
1395 snd_card_free(card);
1396 return err;
1397 }
1398 err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1399 chip->port + FM801_OPL3_BANK1,
1400 OPL3_HW_OPL3_FM801, 1, &opl3);
1401 if (err < 0) {
1402 snd_card_free(card);
1403 return err;
1404 }
1405 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
1406 if (err < 0) {
1407 snd_card_free(card);
1408 return err;
1409 }
1410
1411 __fm801_tuner_only:
1412 err = snd_card_register(card);
1413 if (err < 0) {
1414 snd_card_free(card);
1415 return err;
1416 }
1417 pci_set_drvdata(pci, card);
1418 dev++;
1419 return 0;
1420}
1421
1422static void snd_card_fm801_remove(struct pci_dev *pci)
1423{
1424 snd_card_free(pci_get_drvdata(pci));
1425}
1426
1427#ifdef CONFIG_PM_SLEEP
1428static const unsigned char saved_regs[] = {
1429 FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1430 FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1431 FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1432 FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1433};
1434
1435static int snd_fm801_suspend(struct device *dev)
1436{
1437 struct snd_card *card = dev_get_drvdata(dev);
1438 struct fm801 *chip = card->private_data;
1439 int i;
1440
1441 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1442
1443 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1444 chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
1445
1446 if (chip->tea575x_tuner & TUNER_ONLY) {
1447 /* FIXME: tea575x suspend */
1448 } else {
1449 snd_ac97_suspend(chip->ac97);
1450 snd_ac97_suspend(chip->ac97_sec);
1451 }
1452
1453 return 0;
1454}
1455
1456static int snd_fm801_resume(struct device *dev)
1457{
1458 struct snd_card *card = dev_get_drvdata(dev);
1459 struct fm801 *chip = card->private_data;
1460 int i;
1461
1462 if (chip->tea575x_tuner & TUNER_ONLY) {
1463 snd_fm801_chip_init(chip);
1464 } else {
1465 reset_codec(chip);
1466 snd_fm801_chip_multichannel_init(chip);
1467 snd_fm801_chip_init(chip);
1468 snd_ac97_resume(chip->ac97);
1469 snd_ac97_resume(chip->ac97_sec);
1470 }
1471
1472 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1473 fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1474
1475#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1476 if (!(chip->tea575x_tuner & TUNER_DISABLED))
1477 snd_tea575x_set_freq(&chip->tea);
1478#endif
1479
1480 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1481 return 0;
1482}
1483
1484static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1485#define SND_FM801_PM_OPS &snd_fm801_pm
1486#else
1487#define SND_FM801_PM_OPS NULL
1488#endif /* CONFIG_PM_SLEEP */
1489
1490static struct pci_driver fm801_driver = {
1491 .name = KBUILD_MODNAME,
1492 .id_table = snd_fm801_ids,
1493 .probe = snd_card_fm801_probe,
1494 .remove = snd_card_fm801_remove,
1495 .driver = {
1496 .pm = SND_FM801_PM_OPS,
1497 },
1498};
1499
1500module_pci_driver(fm801_driver);
Note: See TracBrowser for help on using the repository browser.