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

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

Merge changes from next branch.

File size: 43.2 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 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
671 return err;
672 return 0;
673}
674
675static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
676{
677 struct fm801 *chip = snd_pcm_substream_chip(substream);
678 struct snd_pcm_runtime *runtime = substream->runtime;
679 int err;
680
681 chip->capture_substream = substream;
682 runtime->hw = snd_fm801_capture;
683 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
684 &hw_constraints_rates);
685 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
686 return err;
687 return 0;
688}
689
690static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
691{
692 struct fm801 *chip = snd_pcm_substream_chip(substream);
693
694 chip->playback_substream = NULL;
695 return 0;
696}
697
698static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
699{
700 struct fm801 *chip = snd_pcm_substream_chip(substream);
701
702 chip->capture_substream = NULL;
703 return 0;
704}
705
706static const struct snd_pcm_ops snd_fm801_playback_ops = {
707 .open = snd_fm801_playback_open,
708 .close = snd_fm801_playback_close,
709 .prepare = snd_fm801_playback_prepare,
710 .trigger = snd_fm801_playback_trigger,
711 .pointer = snd_fm801_playback_pointer,
712};
713
714static const struct snd_pcm_ops snd_fm801_capture_ops = {
715 .open = snd_fm801_capture_open,
716 .close = snd_fm801_capture_close,
717 .prepare = snd_fm801_capture_prepare,
718 .trigger = snd_fm801_capture_trigger,
719 .pointer = snd_fm801_capture_pointer,
720};
721
722static int snd_fm801_pcm(struct fm801 *chip, int device)
723{
724 struct pci_dev *pdev = to_pci_dev(chip->dev);
725 struct snd_pcm *pcm;
726 int err;
727
728 if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
729 return err;
730
731 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
732 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
733
734 pcm->private_data = chip;
735 pcm->info_flags = 0;
736 strcpy(pcm->name, "FM801");
737 chip->pcm = pcm;
738
739 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
740 chip->multichannel ? 128*1024 : 64*1024, 128*1024);
741
742 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
743 snd_pcm_alt_chmaps,
744 chip->multichannel ? 6 : 2, 0,
745 NULL);
746}
747
748/*
749 * TEA5757 radio
750 */
751
752#ifdef CONFIG_SND_FM801_TEA575X_BOOL
753
754/* GPIO to TEA575x maps */
755struct snd_fm801_tea575x_gpio {
756 u8 data, clk, wren, most;
757 char *name;
758};
759
760static const struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
761 { .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
762 { .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
763 { .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
764};
765
766#define get_tea575x_gpio(chip) \
767 (&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
768
769static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
770{
771 struct fm801 *chip = tea->private_data;
772 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
773 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
774
775 reg &= ~(FM801_GPIO_GP(gpio.data) |
776 FM801_GPIO_GP(gpio.clk) |
777 FM801_GPIO_GP(gpio.wren));
778
779 reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
780 reg |= (pins & TEA575X_CLK) ? FM801_GPIO_GP(gpio.clk) : 0;
781 /* WRITE_ENABLE is inverted */
782 reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
783
784 fm801_writew(chip, GPIO_CTRL, reg);
785}
786
787static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
788{
789 struct fm801 *chip = tea->private_data;
790 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
791 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
792 u8 ret;
793
794 ret = 0;
795 if (reg & FM801_GPIO_GP(gpio.data))
796 ret |= TEA575X_DATA;
797 if (reg & FM801_GPIO_GP(gpio.most))
798 ret |= TEA575X_MOST;
799 return ret;
800}
801
802static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
803{
804 struct fm801 *chip = tea->private_data;
805 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
806 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
807
808 /* use GPIO lines and set write enable bit */
809 reg |= FM801_GPIO_GS(gpio.data) |
810 FM801_GPIO_GS(gpio.wren) |
811 FM801_GPIO_GS(gpio.clk) |
812 FM801_GPIO_GS(gpio.most);
813 if (output) {
814 /* all of lines are in the write direction */
815 /* clear data and clock lines */
816 reg &= ~(FM801_GPIO_GD(gpio.data) |
817 FM801_GPIO_GD(gpio.wren) |
818 FM801_GPIO_GD(gpio.clk) |
819 FM801_GPIO_GP(gpio.data) |
820 FM801_GPIO_GP(gpio.clk) |
821 FM801_GPIO_GP(gpio.wren));
822 } else {
823 /* use GPIO lines, set data direction to input */
824 reg |= FM801_GPIO_GD(gpio.data) |
825 FM801_GPIO_GD(gpio.most) |
826 FM801_GPIO_GP(gpio.data) |
827 FM801_GPIO_GP(gpio.most) |
828 FM801_GPIO_GP(gpio.wren);
829 /* all of lines are in the write direction, except data */
830 /* clear data, write enable and clock lines */
831 reg &= ~(FM801_GPIO_GD(gpio.wren) |
832 FM801_GPIO_GD(gpio.clk) |
833 FM801_GPIO_GP(gpio.clk));
834 }
835
836 fm801_writew(chip, GPIO_CTRL, reg);
837}
838
839static const struct snd_tea575x_ops snd_fm801_tea_ops = {
840 .set_pins = snd_fm801_tea575x_set_pins,
841 .get_pins = snd_fm801_tea575x_get_pins,
842 .set_direction = snd_fm801_tea575x_set_direction,
843};
844#endif
845
846/*
847 * Mixer routines
848 */
849
850#define FM801_SINGLE(xname, reg, shift, mask, invert) \
851{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
852 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
853 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
854
855static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_info *uinfo)
857{
858 int mask = (kcontrol->private_value >> 16) & 0xff;
859
860 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
861 uinfo->count = 1;
862 uinfo->value.integer.min = 0;
863 uinfo->value.integer.max = mask;
864 return 0;
865}
866
867static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
868 struct snd_ctl_elem_value *ucontrol)
869{
870 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
871 int reg = kcontrol->private_value & 0xff;
872 int shift = (kcontrol->private_value >> 8) & 0xff;
873 int mask = (kcontrol->private_value >> 16) & 0xff;
874 int invert = (kcontrol->private_value >> 24) & 0xff;
875 long *value = ucontrol->value.integer.value;
876
877 value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
878 if (invert)
879 value[0] = mask - value[0];
880 return 0;
881}
882
883static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
884 struct snd_ctl_elem_value *ucontrol)
885{
886 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
887 int reg = kcontrol->private_value & 0xff;
888 int shift = (kcontrol->private_value >> 8) & 0xff;
889 int mask = (kcontrol->private_value >> 16) & 0xff;
890 int invert = (kcontrol->private_value >> 24) & 0xff;
891 unsigned short val;
892
893 val = (ucontrol->value.integer.value[0] & mask);
894 if (invert)
895 val = mask - val;
896 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
897}
898
899#define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
900{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
901 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
902 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
903#define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
904{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
905 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
906 .name = xname, .info = snd_fm801_info_double, \
907 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
908 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
909 .tlv = { .p = (xtlv) } }
910
911static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_info *uinfo)
913{
914 int mask = (kcontrol->private_value >> 16) & 0xff;
915
916 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
917 uinfo->count = 2;
918 uinfo->value.integer.min = 0;
919 uinfo->value.integer.max = mask;
920 return 0;
921}
922
923static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
924 struct snd_ctl_elem_value *ucontrol)
925{
926 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
927 int reg = kcontrol->private_value & 0xff;
928 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
929 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
930 int mask = (kcontrol->private_value >> 16) & 0xff;
931 int invert = (kcontrol->private_value >> 24) & 0xff;
932 long *value = ucontrol->value.integer.value;
933
934 spin_lock_irq(&chip->reg_lock);
935 value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
936 value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
937 spin_unlock_irq(&chip->reg_lock);
938 if (invert) {
939 value[0] = mask - value[0];
940 value[1] = mask - value[1];
941 }
942 return 0;
943}
944
945static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol)
947{
948 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
949 int reg = kcontrol->private_value & 0xff;
950 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
951 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
952 int mask = (kcontrol->private_value >> 16) & 0xff;
953 int invert = (kcontrol->private_value >> 24) & 0xff;
954 unsigned short val1, val2;
955
956 val1 = ucontrol->value.integer.value[0] & mask;
957 val2 = ucontrol->value.integer.value[1] & mask;
958 if (invert) {
959 val1 = mask - val1;
960 val2 = mask - val2;
961 }
962 return snd_fm801_update_bits(chip, reg,
963 (mask << shift_left) | (mask << shift_right),
964 (val1 << shift_left ) | (val2 << shift_right));
965}
966
967static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
968 struct snd_ctl_elem_info *uinfo)
969{
970 static const char * const texts[5] = {
971 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
972 };
973
974 return snd_ctl_enum_info(uinfo, 1, 5, texts);
975}
976
977static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
978 struct snd_ctl_elem_value *ucontrol)
979{
980 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
981 unsigned short val;
982
983 val = fm801_readw(chip, REC_SRC) & 7;
984 if (val > 4)
985 val = 4;
986 ucontrol->value.enumerated.item[0] = val;
987 return 0;
988}
989
990static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
991 struct snd_ctl_elem_value *ucontrol)
992{
993 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
994 unsigned short val;
995
996 if ((val = ucontrol->value.enumerated.item[0]) > 4)
997 return -EINVAL;
998 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
999}
1000
1001static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
1002
1003#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
1004
1005static const struct snd_kcontrol_new snd_fm801_controls[] = {
1006FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1007 db_scale_dsp),
1008FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1009FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1010 db_scale_dsp),
1011FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1012FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1013 db_scale_dsp),
1014FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
1015{
1016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1017 .name = "Digital Capture Source",
1018 .info = snd_fm801_info_mux,
1019 .get = snd_fm801_get_mux,
1020 .put = snd_fm801_put_mux,
1021}
1022};
1023
1024#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
1025
1026static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
1027FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
1028FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
1029FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
1030FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
1031FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
1032FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
1033};
1034
1035static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1036{
1037 struct fm801 *chip = bus->private_data;
1038 chip->ac97_bus = NULL;
1039}
1040
1041static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
1042{
1043 struct fm801 *chip = ac97->private_data;
1044 if (ac97->num == 0) {
1045 chip->ac97 = NULL;
1046 } else {
1047 chip->ac97_sec = NULL;
1048 }
1049}
1050
1051static int snd_fm801_mixer(struct fm801 *chip)
1052{
1053 struct snd_ac97_template ac97;
1054 unsigned int i;
1055 int err;
1056 static const struct snd_ac97_bus_ops ops = {
1057 .write = snd_fm801_codec_write,
1058 .read = snd_fm801_codec_read,
1059 };
1060
1061 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1062 return err;
1063 chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
1064
1065 memset(&ac97, 0, sizeof(ac97));
1066 ac97.private_data = chip;
1067 ac97.private_free = snd_fm801_mixer_free_ac97;
1068 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1069 return err;
1070 if (chip->secondary) {
1071 ac97.num = 1;
1072 ac97.addr = chip->secondary_addr;
1073 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
1074 return err;
1075 }
1076 for (i = 0; i < FM801_CONTROLS; i++) {
1077 err = snd_ctl_add(chip->card,
1078 snd_ctl_new1(&snd_fm801_controls[i], chip));
1079 if (err < 0)
1080 return err;
1081 }
1082 if (chip->multichannel) {
1083 for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1084 err = snd_ctl_add(chip->card,
1085 snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1086 if (err < 0)
1087 return err;
1088 }
1089 }
1090 return 0;
1091}
1092
1093/*
1094 * initialization routines
1095 */
1096
1097static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1098 unsigned short reg, unsigned long waits)
1099{
1100 unsigned long timeout = jiffies + waits;
1101
1102 fm801_writew(chip, AC97_CMD,
1103 reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1104 udelay(5);
1105 do {
1106 if ((fm801_readw(chip, AC97_CMD) &
1107 (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1108 return 0;
1109 schedule_timeout_uninterruptible(1);
1110 } while (time_after(timeout, jiffies));
1111 return -EIO;
1112}
1113
1114static int reset_codec(struct fm801 *chip)
1115{
1116 /* codec cold reset + AC'97 warm reset */
1117 fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1118 fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1119 udelay(100);
1120 fm801_writew(chip, CODEC_CTRL, 0);
1121
1122 return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1123}
1124
1125static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1126{
1127 unsigned short cmdw;
1128
1129 if (chip->multichannel) {
1130 if (chip->secondary_addr) {
1131 wait_for_codec(chip, chip->secondary_addr,
1132 AC97_VENDOR_ID1, msecs_to_jiffies(50));
1133 } else {
1134 /* my card has the secondary codec */
1135 /* at address #3, so the loop is inverted */
1136 int i;
1137 for (i = 3; i > 0; i--) {
1138 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1139 msecs_to_jiffies(50))) {
1140 cmdw = fm801_readw(chip, AC97_DATA);
1141 if (cmdw != 0xffff && cmdw != 0) {
1142 chip->secondary = 1;
1143 chip->secondary_addr = i;
1144 break;
1145 }
1146 }
1147 }
1148 }
1149
1150 /* the recovery phase, it seems that probing for non-existing codec might */
1151 /* cause timeout problems */
1152 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1153 }
1154}
1155
1156static void snd_fm801_chip_init(struct fm801 *chip)
1157{
1158 unsigned short cmdw;
1159
1160 /* init volume */
1161 fm801_writew(chip, PCM_VOL, 0x0808);
1162 fm801_writew(chip, FM_VOL, 0x9f1f);
1163 fm801_writew(chip, I2S_VOL, 0x8808);
1164
1165 /* I2S control - I2S mode */
1166 fm801_writew(chip, I2S_MODE, 0x0003);
1167
1168 /* interrupt setup */
1169 cmdw = fm801_readw(chip, IRQ_MASK);
1170 if (chip->irq < 0)
1171 cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
1172 else
1173 cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
1174 fm801_writew(chip, IRQ_MASK, cmdw);
1175
1176 /* interrupt clear */
1177 fm801_writew(chip, IRQ_STATUS,
1178 FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1179}
1180
1181static int snd_fm801_free(struct fm801 *chip)
1182{
1183 unsigned short cmdw;
1184
1185 if (chip->irq < 0)
1186 goto __end_hw;
1187
1188 /* interrupt setup - mask everything */
1189 cmdw = fm801_readw(chip, IRQ_MASK);
1190 cmdw |= 0x00c3;
1191 fm801_writew(chip, IRQ_MASK, cmdw);
1192
1193 devm_free_irq(chip->dev, chip->irq, chip);
1194
1195 __end_hw:
1196#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1197 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1198 snd_tea575x_exit(&chip->tea);
1199 v4l2_device_unregister(&chip->v4l2_dev);
1200 }
1201#endif
1202 return 0;
1203}
1204
1205static int snd_fm801_dev_free(struct snd_device *device)
1206{
1207 struct fm801 *chip = device->device_data;
1208 return snd_fm801_free(chip);
1209}
1210
1211static int snd_fm801_create(struct snd_card *card,
1212 struct pci_dev *pci,
1213 int tea575x_tuner,
1214 int radio_nr,
1215 struct fm801 **rchip)
1216{
1217 struct fm801 *chip;
1218 int err;
1219 static const struct snd_device_ops ops = {
1220 .dev_free = snd_fm801_dev_free,
1221 };
1222
1223 *rchip = NULL;
1224#ifndef TARGET_OS2
1225 if ((err = pcim_enable_device(pci)) < 0)
1226 return err;
1227 chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
1228#else
1229 if ((err = pci_enable_device(pci)) < 0)
1230 return err;
1231 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1232#endif
1233 if (chip == NULL)
1234 return -ENOMEM;
1235 spin_lock_init(&chip->reg_lock);
1236 chip->card = card;
1237 chip->dev = &pci->dev;
1238 chip->irq = -1;
1239 chip->tea575x_tuner = tea575x_tuner;
1240 if ((err = pci_request_regions(pci, "FM801")) < 0)
1241 return err;
1242 chip->port = pci_resource_start(pci, 0);
1243
1244#ifndef TARGET_OS2
1245 if (pci->revision >= 0xb1) /* FM801-AU */
1246#else
1247 if (snd_pci_revision(pci) >= 0xb1) /* FM801-AU */
1248#endif
1249 chip->multichannel = 1;
1250
1251 if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1252 if (reset_codec(chip) < 0) {
1253 dev_info(chip->card->dev,
1254 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1255 chip->tea575x_tuner = 3 | TUNER_ONLY;
1256 } else {
1257 snd_fm801_chip_multichannel_init(chip);
1258 }
1259 }
1260
1261 if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
1262#ifndef TARGET_OS2
1263 if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
1264 IRQF_SHARED, KBUILD_MODNAME, chip)) {
1265#else
1266 if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1267 KBUILD_MODNAME, chip)) {
1268#endif
1269 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1270 snd_fm801_free(chip);
1271 return -EBUSY;
1272 }
1273 chip->irq = pci->irq;
1274 card->sync_irq = chip->irq;
1275 pci_set_master(pci);
1276 }
1277
1278 snd_fm801_chip_init(chip);
1279
1280 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1281 snd_fm801_free(chip);
1282 return err;
1283 }
1284
1285#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1286 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1287 if (err < 0) {
1288 snd_fm801_free(chip);
1289 return err;
1290 }
1291 chip->tea.v4l2_dev = &chip->v4l2_dev;
1292 chip->tea.radio_nr = radio_nr;
1293 chip->tea.private_data = chip;
1294 chip->tea.ops = &snd_fm801_tea_ops;
1295 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1296 if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1297 (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1298 if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1299 dev_err(card->dev, "TEA575x radio not found\n");
1300 snd_fm801_free(chip);
1301 return -ENODEV;
1302 }
1303 } else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1304 unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1305
1306 /* autodetect tuner connection */
1307 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1308 chip->tea575x_tuner = tea575x_tuner;
1309 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1310 dev_info(card->dev,
1311 "detected TEA575x radio type %s\n",
1312 get_tea575x_gpio(chip)->name);
1313 break;
1314 }
1315 }
1316 if (tea575x_tuner == 4) {
1317 dev_err(card->dev, "TEA575x radio not found\n");
1318 chip->tea575x_tuner = TUNER_DISABLED;
1319 }
1320
1321 chip->tea575x_tuner |= tuner_only;
1322 }
1323 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1324 strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1325 sizeof(chip->tea.card));
1326 }
1327#endif
1328
1329 *rchip = chip;
1330 return 0;
1331}
1332
1333static int snd_card_fm801_probe(struct pci_dev *pci,
1334 const struct pci_device_id *pci_id)
1335{
1336 static int dev;
1337 struct snd_card *card;
1338 struct fm801 *chip;
1339 struct snd_opl3 *opl3;
1340 int err;
1341
1342 if (dev >= SNDRV_CARDS)
1343 return -ENODEV;
1344 if (!enable[dev]) {
1345 dev++;
1346 return -ENOENT;
1347 }
1348
1349 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1350 0, &card);
1351 if (err < 0)
1352 return err;
1353 if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
1354 snd_card_free(card);
1355 return err;
1356 }
1357 card->private_data = chip;
1358
1359 strcpy(card->driver, "FM801");
1360 strcpy(card->shortname, "ForteMedia FM801-");
1361 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1362 sprintf(card->longname, "%s at 0x%lx, irq %i",
1363 card->shortname, chip->port, chip->irq);
1364
1365 if (chip->tea575x_tuner & TUNER_ONLY)
1366 goto __fm801_tuner_only;
1367
1368 if ((err = snd_fm801_pcm(chip, 0)) < 0) {
1369 snd_card_free(card);
1370 return err;
1371 }
1372 if ((err = snd_fm801_mixer(chip)) < 0) {
1373 snd_card_free(card);
1374 return err;
1375 }
1376 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1377 chip->port + FM801_MPU401_DATA,
1378 MPU401_INFO_INTEGRATED |
1379 MPU401_INFO_IRQ_HOOK,
1380 -1, &chip->rmidi)) < 0) {
1381 snd_card_free(card);
1382 return err;
1383 }
1384 if ((err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1385 chip->port + FM801_OPL3_BANK1,
1386 OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1387 snd_card_free(card);
1388 return err;
1389 }
1390 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1391 snd_card_free(card);
1392 return err;
1393 }
1394
1395 __fm801_tuner_only:
1396 if ((err = snd_card_register(card)) < 0) {
1397 snd_card_free(card);
1398 return err;
1399 }
1400 pci_set_drvdata(pci, card);
1401 dev++;
1402 return 0;
1403}
1404
1405static void snd_card_fm801_remove(struct pci_dev *pci)
1406{
1407 snd_card_free(pci_get_drvdata(pci));
1408}
1409
1410#ifdef CONFIG_PM_SLEEP
1411static const unsigned char saved_regs[] = {
1412 FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1413 FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1414 FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1415 FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1416};
1417
1418static int snd_fm801_suspend(struct device *dev)
1419{
1420 struct snd_card *card = dev_get_drvdata(dev);
1421 struct fm801 *chip = card->private_data;
1422 int i;
1423
1424 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1425
1426 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1427 chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
1428
1429 if (chip->tea575x_tuner & TUNER_ONLY) {
1430 /* FIXME: tea575x suspend */
1431 } else {
1432 snd_ac97_suspend(chip->ac97);
1433 snd_ac97_suspend(chip->ac97_sec);
1434 }
1435
1436 return 0;
1437}
1438
1439static int snd_fm801_resume(struct device *dev)
1440{
1441 struct snd_card *card = dev_get_drvdata(dev);
1442 struct fm801 *chip = card->private_data;
1443 int i;
1444
1445 if (chip->tea575x_tuner & TUNER_ONLY) {
1446 snd_fm801_chip_init(chip);
1447 } else {
1448 reset_codec(chip);
1449 snd_fm801_chip_multichannel_init(chip);
1450 snd_fm801_chip_init(chip);
1451 snd_ac97_resume(chip->ac97);
1452 snd_ac97_resume(chip->ac97_sec);
1453 }
1454
1455 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1456 fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1457
1458#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1459 if (!(chip->tea575x_tuner & TUNER_DISABLED))
1460 snd_tea575x_set_freq(&chip->tea);
1461#endif
1462
1463 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1464 return 0;
1465}
1466
1467static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1468#define SND_FM801_PM_OPS &snd_fm801_pm
1469#else
1470#define SND_FM801_PM_OPS NULL
1471#endif /* CONFIG_PM_SLEEP */
1472
1473static struct pci_driver fm801_driver = {
1474 .name = KBUILD_MODNAME,
1475 .id_table = snd_fm801_ids,
1476 .probe = snd_card_fm801_probe,
1477 .remove = snd_card_fm801_remove,
1478 .driver = {
1479 .pm = SND_FM801_PM_OPS,
1480 },
1481};
1482
1483module_pci_driver(fm801_driver);
Note: See TracBrowser for help on using the repository browser.