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

Last change on this file since 777 was 777, checked in by David Azarewicz, 5 months ago

Merge from uniaud32-exp branch

File size: 41.7 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 u16 saved_regs[0x20];
234};
235
236/*
237 * IO accessors
238 */
239
240static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
241{
242 outw(value, chip->port + offset);
243}
244
245static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
246{
247 return inw(chip->port + offset);
248}
249
250static const struct pci_device_id snd_fm801_ids[] = {
251 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
252 { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* Gallant Odyssey Sound 4 */
253 { 0, }
254};
255
256MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
257
258/*
259 * common I/O routines
260 */
261
262static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
263{
264 unsigned int idx;
265
266 for (idx = 0; idx < iterations; idx++) {
267 if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
268 return true;
269 udelay(10);
270 }
271 return false;
272}
273
274static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
275{
276 unsigned int idx;
277
278 for (idx = 0; idx < iterations; idx++) {
279 if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
280 return true;
281 udelay(10);
282 }
283 return false;
284}
285
286static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
287 unsigned short mask, unsigned short value)
288{
289 int change;
290 unsigned long flags;
291 unsigned short old, new;
292
293 spin_lock_irqsave(&chip->reg_lock, flags);
294 old = fm801_ioread16(chip, reg);
295 new = (old & ~mask) | value;
296 change = old != new;
297 if (change)
298 fm801_iowrite16(chip, reg, new);
299 spin_unlock_irqrestore(&chip->reg_lock, flags);
300 return change;
301}
302
303static void snd_fm801_codec_write(struct snd_ac97 *ac97,
304 unsigned short reg,
305 unsigned short val)
306{
307 struct fm801 *chip = ac97->private_data;
308
309 /*
310 * Wait until the codec interface is not ready..
311 */
312 if (!fm801_ac97_is_ready(chip, 100)) {
313 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
314 return;
315 }
316
317 /* write data and address */
318 fm801_writew(chip, AC97_DATA, val);
319 fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
320 /*
321 * Wait until the write command is not completed..
322 */
323 if (!fm801_ac97_is_ready(chip, 1000))
324 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
325 ac97->num);
326}
327
328static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
329{
330 struct fm801 *chip = ac97->private_data;
331
332 /*
333 * Wait until the codec interface is not ready..
334 */
335 if (!fm801_ac97_is_ready(chip, 100)) {
336 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
337 return 0;
338 }
339
340 /* read command */
341 fm801_writew(chip, AC97_CMD,
342 reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
343 if (!fm801_ac97_is_ready(chip, 100)) {
344 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
345 ac97->num);
346 return 0;
347 }
348
349 if (!fm801_ac97_is_valid(chip, 1000)) {
350 dev_err(chip->card->dev,
351 "AC'97 interface #%d is not valid (2)\n", ac97->num);
352 return 0;
353 }
354
355 return fm801_readw(chip, AC97_DATA);
356}
357
358static const unsigned int rates[] = {
359 5500, 8000, 9600, 11025,
360 16000, 19200, 22050, 32000,
361 38400, 44100, 48000
362};
363
364static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
365 .count = ARRAY_SIZE(rates),
366 .list = rates,
367 .mask = 0,
368};
369
370static const unsigned int channels[] = {
371 2, 4, 6
372};
373
374static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
375 .count = ARRAY_SIZE(channels),
376 .list = channels,
377 .mask = 0,
378};
379
380/*
381 * Sample rate routines
382 */
383
384static unsigned short snd_fm801_rate_bits(unsigned int rate)
385{
386 unsigned int idx;
387
388 for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
389 if (rates[idx] == rate)
390 return idx;
391 snd_BUG();
392 return ARRAY_SIZE(rates) - 1;
393}
394
395/*
396 * PCM part
397 */
398
399static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
400 int cmd)
401{
402 struct fm801 *chip = snd_pcm_substream_chip(substream);
403
404 spin_lock(&chip->reg_lock);
405 switch (cmd) {
406 case SNDRV_PCM_TRIGGER_START:
407 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
408 FM801_BUF2_LAST |
409 FM801_PAUSE);
410 chip->ply_ctrl |= FM801_START |
411 FM801_IMMED_STOP;
412 break;
413 case SNDRV_PCM_TRIGGER_STOP:
414 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
415 break;
416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417 case SNDRV_PCM_TRIGGER_SUSPEND:
418 chip->ply_ctrl |= FM801_PAUSE;
419 break;
420 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
421 case SNDRV_PCM_TRIGGER_RESUME:
422 chip->ply_ctrl &= ~FM801_PAUSE;
423 break;
424 default:
425 spin_unlock(&chip->reg_lock);
426 snd_BUG();
427 return -EINVAL;
428 }
429 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
430 spin_unlock(&chip->reg_lock);
431 return 0;
432}
433
434static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
435 int cmd)
436{
437 struct fm801 *chip = snd_pcm_substream_chip(substream);
438
439 spin_lock(&chip->reg_lock);
440 switch (cmd) {
441 case SNDRV_PCM_TRIGGER_START:
442 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
443 FM801_BUF2_LAST |
444 FM801_PAUSE);
445 chip->cap_ctrl |= FM801_START |
446 FM801_IMMED_STOP;
447 break;
448 case SNDRV_PCM_TRIGGER_STOP:
449 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
450 break;
451 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
452 case SNDRV_PCM_TRIGGER_SUSPEND:
453 chip->cap_ctrl |= FM801_PAUSE;
454 break;
455 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
456 case SNDRV_PCM_TRIGGER_RESUME:
457 chip->cap_ctrl &= ~FM801_PAUSE;
458 break;
459 default:
460 spin_unlock(&chip->reg_lock);
461 snd_BUG();
462 return -EINVAL;
463 }
464 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
465 spin_unlock(&chip->reg_lock);
466 return 0;
467}
468
469static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
470{
471 struct fm801 *chip = snd_pcm_substream_chip(substream);
472 struct snd_pcm_runtime *runtime = substream->runtime;
473
474 chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
475 chip->ply_count = snd_pcm_lib_period_bytes(substream);
476 spin_lock_irq(&chip->reg_lock);
477 chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
478 FM801_STEREO | FM801_RATE_MASK |
479 FM801_CHANNELS_MASK);
480 if (snd_pcm_format_width(runtime->format) == 16)
481 chip->ply_ctrl |= FM801_16BIT;
482 if (runtime->channels > 1) {
483 chip->ply_ctrl |= FM801_STEREO;
484 if (runtime->channels == 4)
485 chip->ply_ctrl |= FM801_CHANNELS_4;
486 else if (runtime->channels == 6)
487 chip->ply_ctrl |= FM801_CHANNELS_6;
488 }
489 chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
490 chip->ply_buf = 0;
491 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
492 fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
493 chip->ply_buffer = runtime->dma_addr;
494 chip->ply_pos = 0;
495 fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
496 fm801_writel(chip, PLY_BUF2,
497 chip->ply_buffer + (chip->ply_count % chip->ply_size));
498 spin_unlock_irq(&chip->reg_lock);
499 return 0;
500}
501
502static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
503{
504 struct fm801 *chip = snd_pcm_substream_chip(substream);
505 struct snd_pcm_runtime *runtime = substream->runtime;
506
507 chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
508 chip->cap_count = snd_pcm_lib_period_bytes(substream);
509 spin_lock_irq(&chip->reg_lock);
510 chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
511 FM801_STEREO | FM801_RATE_MASK);
512 if (snd_pcm_format_width(runtime->format) == 16)
513 chip->cap_ctrl |= FM801_16BIT;
514 if (runtime->channels > 1)
515 chip->cap_ctrl |= FM801_STEREO;
516 chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
517 chip->cap_buf = 0;
518 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
519 fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
520 chip->cap_buffer = runtime->dma_addr;
521 chip->cap_pos = 0;
522 fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
523 fm801_writel(chip, CAP_BUF2,
524 chip->cap_buffer + (chip->cap_count % chip->cap_size));
525 spin_unlock_irq(&chip->reg_lock);
526 return 0;
527}
528
529static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
530{
531 struct fm801 *chip = snd_pcm_substream_chip(substream);
532 size_t ptr;
533
534 if (!(chip->ply_ctrl & FM801_START))
535 return 0;
536 spin_lock(&chip->reg_lock);
537 ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
538 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
539 ptr += chip->ply_count;
540 ptr %= chip->ply_size;
541 }
542 spin_unlock(&chip->reg_lock);
543 return bytes_to_frames(substream->runtime, ptr);
544}
545
546static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
547{
548 struct fm801 *chip = snd_pcm_substream_chip(substream);
549 size_t ptr;
550
551 if (!(chip->cap_ctrl & FM801_START))
552 return 0;
553 spin_lock(&chip->reg_lock);
554 ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
555 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
556 ptr += chip->cap_count;
557 ptr %= chip->cap_size;
558 }
559 spin_unlock(&chip->reg_lock);
560 return bytes_to_frames(substream->runtime, ptr);
561}
562
563static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
564{
565 struct fm801 *chip = dev_id;
566 unsigned short status;
567 unsigned int tmp;
568
569 status = fm801_readw(chip, IRQ_STATUS);
570 status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
571 if (! status)
572 return IRQ_NONE;
573 /* ack first */
574 fm801_writew(chip, IRQ_STATUS, status);
575 if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
576 spin_lock(&chip->reg_lock);
577 chip->ply_buf++;
578 chip->ply_pos += chip->ply_count;
579 chip->ply_pos %= chip->ply_size;
580 tmp = chip->ply_pos + chip->ply_count;
581 tmp %= chip->ply_size;
582 if (chip->ply_buf & 1)
583 fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
584 else
585 fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
586 spin_unlock(&chip->reg_lock);
587 snd_pcm_period_elapsed(chip->playback_substream);
588 }
589 if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
590 spin_lock(&chip->reg_lock);
591 chip->cap_buf++;
592 chip->cap_pos += chip->cap_count;
593 chip->cap_pos %= chip->cap_size;
594 tmp = chip->cap_pos + chip->cap_count;
595 tmp %= chip->cap_size;
596 if (chip->cap_buf & 1)
597 fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
598 else
599 fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
600 spin_unlock(&chip->reg_lock);
601 snd_pcm_period_elapsed(chip->capture_substream);
602 }
603 if (chip->rmidi && (status & FM801_IRQ_MPU))
604 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
605 if (status & FM801_IRQ_VOLUME) {
606 /* TODO */
607 }
608
609 return IRQ_HANDLED;
610}
611
612static const struct snd_pcm_hardware snd_fm801_playback =
613{
614 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
615 SNDRV_PCM_INFO_BLOCK_TRANSFER |
616 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
617 SNDRV_PCM_INFO_MMAP_VALID),
618 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
619 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
620 .rate_min = 5500,
621 .rate_max = 48000,
622 .channels_min = 1,
623 .channels_max = 2,
624 .buffer_bytes_max = (128*1024),
625 .period_bytes_min = 64,
626 .period_bytes_max = (128*1024),
627 .periods_min = 1,
628 .periods_max = 1024,
629 .fifo_size = 0,
630};
631
632static const struct snd_pcm_hardware snd_fm801_capture =
633{
634 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
635 SNDRV_PCM_INFO_BLOCK_TRANSFER |
636 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
637 SNDRV_PCM_INFO_MMAP_VALID),
638 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
639 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
640 .rate_min = 5500,
641 .rate_max = 48000,
642 .channels_min = 1,
643 .channels_max = 2,
644 .buffer_bytes_max = (128*1024),
645 .period_bytes_min = 64,
646 .period_bytes_max = (128*1024),
647 .periods_min = 1,
648 .periods_max = 1024,
649 .fifo_size = 0,
650};
651
652static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
653{
654 struct fm801 *chip = snd_pcm_substream_chip(substream);
655 struct snd_pcm_runtime *runtime = substream->runtime;
656 int err;
657
658 chip->playback_substream = substream;
659 runtime->hw = snd_fm801_playback;
660 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
661 &hw_constraints_rates);
662 if (chip->multichannel) {
663 runtime->hw.channels_max = 6;
664 snd_pcm_hw_constraint_list(runtime, 0,
665 SNDRV_PCM_HW_PARAM_CHANNELS,
666 &hw_constraints_channels);
667 }
668 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
669 if (err < 0)
670 return err;
671 return 0;
672}
673
674static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
675{
676 struct fm801 *chip = snd_pcm_substream_chip(substream);
677 struct snd_pcm_runtime *runtime = substream->runtime;
678 int err;
679
680 chip->capture_substream = substream;
681 runtime->hw = snd_fm801_capture;
682 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
683 &hw_constraints_rates);
684 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
685 if (err < 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 err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm);
729 if (err < 0)
730 return err;
731
732 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
733 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
734
735 pcm->private_data = chip;
736 pcm->info_flags = 0;
737 strcpy(pcm->name, "FM801");
738 chip->pcm = pcm;
739
740 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
741 chip->multichannel ? 128*1024 : 64*1024, 128*1024);
742
743 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
744 snd_pcm_alt_chmaps,
745 chip->multichannel ? 6 : 2, 0,
746 NULL);
747}
748
749/*
750 * TEA5757 radio
751 */
752
753#ifdef CONFIG_SND_FM801_TEA575X_BOOL
754
755/* GPIO to TEA575x maps */
756struct snd_fm801_tea575x_gpio {
757 u8 data, clk, wren, most;
758 char *name;
759};
760
761static const struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
762 { .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
763 { .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
764 { .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
765};
766
767#define get_tea575x_gpio(chip) \
768 (&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
769
770static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
771{
772 struct fm801 *chip = tea->private_data;
773 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
774 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
775
776 reg &= ~(FM801_GPIO_GP(gpio.data) |
777 FM801_GPIO_GP(gpio.clk) |
778 FM801_GPIO_GP(gpio.wren));
779
780 reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
781 reg |= (pins & TEA575X_CLK) ? FM801_GPIO_GP(gpio.clk) : 0;
782 /* WRITE_ENABLE is inverted */
783 reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
784
785 fm801_writew(chip, GPIO_CTRL, reg);
786}
787
788static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
789{
790 struct fm801 *chip = tea->private_data;
791 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
792 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
793 u8 ret;
794
795 ret = 0;
796 if (reg & FM801_GPIO_GP(gpio.data))
797 ret |= TEA575X_DATA;
798 if (reg & FM801_GPIO_GP(gpio.most))
799 ret |= TEA575X_MOST;
800 return ret;
801}
802
803static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
804{
805 struct fm801 *chip = tea->private_data;
806 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
807 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
808
809 /* use GPIO lines and set write enable bit */
810 reg |= FM801_GPIO_GS(gpio.data) |
811 FM801_GPIO_GS(gpio.wren) |
812 FM801_GPIO_GS(gpio.clk) |
813 FM801_GPIO_GS(gpio.most);
814 if (output) {
815 /* all of lines are in the write direction */
816 /* clear data and clock lines */
817 reg &= ~(FM801_GPIO_GD(gpio.data) |
818 FM801_GPIO_GD(gpio.wren) |
819 FM801_GPIO_GD(gpio.clk) |
820 FM801_GPIO_GP(gpio.data) |
821 FM801_GPIO_GP(gpio.clk) |
822 FM801_GPIO_GP(gpio.wren));
823 } else {
824 /* use GPIO lines, set data direction to input */
825 reg |= FM801_GPIO_GD(gpio.data) |
826 FM801_GPIO_GD(gpio.most) |
827 FM801_GPIO_GP(gpio.data) |
828 FM801_GPIO_GP(gpio.most) |
829 FM801_GPIO_GP(gpio.wren);
830 /* all of lines are in the write direction, except data */
831 /* clear data, write enable and clock lines */
832 reg &= ~(FM801_GPIO_GD(gpio.wren) |
833 FM801_GPIO_GD(gpio.clk) |
834 FM801_GPIO_GP(gpio.clk));
835 }
836
837 fm801_writew(chip, GPIO_CTRL, reg);
838}
839
840static const struct snd_tea575x_ops snd_fm801_tea_ops = {
841 .set_pins = snd_fm801_tea575x_set_pins,
842 .get_pins = snd_fm801_tea575x_get_pins,
843 .set_direction = snd_fm801_tea575x_set_direction,
844};
845#endif
846
847/*
848 * Mixer routines
849 */
850
851#define FM801_SINGLE(xname, reg, shift, mask, invert) \
852{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
853 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
854 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
855
856static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
857 struct snd_ctl_elem_info *uinfo)
858{
859 int mask = (kcontrol->private_value >> 16) & 0xff;
860
861 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
862 uinfo->count = 1;
863 uinfo->value.integer.min = 0;
864 uinfo->value.integer.max = mask;
865 return 0;
866}
867
868static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
869 struct snd_ctl_elem_value *ucontrol)
870{
871 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
872 int reg = kcontrol->private_value & 0xff;
873 int shift = (kcontrol->private_value >> 8) & 0xff;
874 int mask = (kcontrol->private_value >> 16) & 0xff;
875 int invert = (kcontrol->private_value >> 24) & 0xff;
876 long *value = ucontrol->value.integer.value;
877
878 value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
879 if (invert)
880 value[0] = mask - value[0];
881 return 0;
882}
883
884static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
885 struct snd_ctl_elem_value *ucontrol)
886{
887 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
888 int reg = kcontrol->private_value & 0xff;
889 int shift = (kcontrol->private_value >> 8) & 0xff;
890 int mask = (kcontrol->private_value >> 16) & 0xff;
891 int invert = (kcontrol->private_value >> 24) & 0xff;
892 unsigned short val;
893
894 val = (ucontrol->value.integer.value[0] & mask);
895 if (invert)
896 val = mask - val;
897 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
898}
899
900#define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
901{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
902 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
903 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
904#define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
905{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
906 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
907 .name = xname, .info = snd_fm801_info_double, \
908 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
909 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
910 .tlv = { .p = (xtlv) } }
911
912static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
913 struct snd_ctl_elem_info *uinfo)
914{
915 int mask = (kcontrol->private_value >> 16) & 0xff;
916
917 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
918 uinfo->count = 2;
919 uinfo->value.integer.min = 0;
920 uinfo->value.integer.max = mask;
921 return 0;
922}
923
924static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
925 struct snd_ctl_elem_value *ucontrol)
926{
927 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
928 int reg = kcontrol->private_value & 0xff;
929 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
930 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
931 int mask = (kcontrol->private_value >> 16) & 0xff;
932 int invert = (kcontrol->private_value >> 24) & 0xff;
933 long *value = ucontrol->value.integer.value;
934
935 spin_lock_irq(&chip->reg_lock);
936 value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
937 value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
938 spin_unlock_irq(&chip->reg_lock);
939 if (invert) {
940 value[0] = mask - value[0];
941 value[1] = mask - value[1];
942 }
943 return 0;
944}
945
946static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_value *ucontrol)
948{
949 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
950 int reg = kcontrol->private_value & 0xff;
951 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
952 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
953 int mask = (kcontrol->private_value >> 16) & 0xff;
954 int invert = (kcontrol->private_value >> 24) & 0xff;
955 unsigned short val1, val2;
956
957 val1 = ucontrol->value.integer.value[0] & mask;
958 val2 = ucontrol->value.integer.value[1] & mask;
959 if (invert) {
960 val1 = mask - val1;
961 val2 = mask - val2;
962 }
963 return snd_fm801_update_bits(chip, reg,
964 (mask << shift_left) | (mask << shift_right),
965 (val1 << shift_left ) | (val2 << shift_right));
966}
967
968static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_info *uinfo)
970{
971 static const char * const texts[5] = {
972 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
973 };
974
975 return snd_ctl_enum_info(uinfo, 1, 5, texts);
976}
977
978static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
979 struct snd_ctl_elem_value *ucontrol)
980{
981 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
982 unsigned short val;
983
984 val = fm801_readw(chip, REC_SRC) & 7;
985 if (val > 4)
986 val = 4;
987 ucontrol->value.enumerated.item[0] = val;
988 return 0;
989}
990
991static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_value *ucontrol)
993{
994 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
995 unsigned short val;
996
997 val = ucontrol->value.enumerated.item[0];
998 if (val > 4)
999 return -EINVAL;
1000 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
1001}
1002
1003static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
1004
1005#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
1006
1007static const struct snd_kcontrol_new snd_fm801_controls[] = {
1008FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1009 db_scale_dsp),
1010FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1011FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1012 db_scale_dsp),
1013FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1014FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1015 db_scale_dsp),
1016FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
1017{
1018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019 .name = "Digital Capture Source",
1020 .info = snd_fm801_info_mux,
1021 .get = snd_fm801_get_mux,
1022 .put = snd_fm801_put_mux,
1023}
1024};
1025
1026#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
1027
1028static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
1029FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
1030FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
1031FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
1032FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
1033FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
1034FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
1035};
1036
1037static int snd_fm801_mixer(struct fm801 *chip)
1038{
1039 struct snd_ac97_template ac97;
1040 unsigned int i;
1041 int err;
1042 static const struct snd_ac97_bus_ops ops = {
1043 .write = snd_fm801_codec_write,
1044 .read = snd_fm801_codec_read,
1045 };
1046
1047 err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1048 if (err < 0)
1049 return err;
1050
1051 memset(&ac97, 0, sizeof(ac97));
1052 ac97.private_data = chip;
1053 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1054 if (err < 0)
1055 return err;
1056 if (chip->secondary) {
1057 ac97.num = 1;
1058 ac97.addr = chip->secondary_addr;
1059 err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
1060 if (err < 0)
1061 return err;
1062 }
1063 for (i = 0; i < FM801_CONTROLS; i++) {
1064 err = snd_ctl_add(chip->card,
1065 snd_ctl_new1(&snd_fm801_controls[i], chip));
1066 if (err < 0)
1067 return err;
1068 }
1069 if (chip->multichannel) {
1070 for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1071 err = snd_ctl_add(chip->card,
1072 snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1073 if (err < 0)
1074 return err;
1075 }
1076 }
1077 return 0;
1078}
1079
1080/*
1081 * initialization routines
1082 */
1083
1084static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1085 unsigned short reg, unsigned long waits)
1086{
1087 unsigned long timeout = jiffies + waits;
1088
1089 fm801_writew(chip, AC97_CMD,
1090 reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1091 udelay(5);
1092 do {
1093 if ((fm801_readw(chip, AC97_CMD) &
1094 (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1095 return 0;
1096 schedule_timeout_uninterruptible(1);
1097 } while (time_after(timeout, jiffies));
1098 return -EIO;
1099}
1100
1101static int reset_codec(struct fm801 *chip)
1102{
1103 /* codec cold reset + AC'97 warm reset */
1104 fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1105 fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1106 udelay(100);
1107 fm801_writew(chip, CODEC_CTRL, 0);
1108
1109 return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1110}
1111
1112static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1113{
1114 unsigned short cmdw;
1115
1116 if (chip->multichannel) {
1117 if (chip->secondary_addr) {
1118 wait_for_codec(chip, chip->secondary_addr,
1119 AC97_VENDOR_ID1, msecs_to_jiffies(50));
1120 } else {
1121 /* my card has the secondary codec */
1122 /* at address #3, so the loop is inverted */
1123 int i;
1124 for (i = 3; i > 0; i--) {
1125 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1126 msecs_to_jiffies(50))) {
1127 cmdw = fm801_readw(chip, AC97_DATA);
1128 if (cmdw != 0xffff && cmdw != 0) {
1129 chip->secondary = 1;
1130 chip->secondary_addr = i;
1131 break;
1132 }
1133 }
1134 }
1135 }
1136
1137 /* the recovery phase, it seems that probing for non-existing codec might */
1138 /* cause timeout problems */
1139 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1140 }
1141}
1142
1143static void snd_fm801_chip_init(struct fm801 *chip)
1144{
1145 unsigned short cmdw;
1146
1147 /* init volume */
1148 fm801_writew(chip, PCM_VOL, 0x0808);
1149 fm801_writew(chip, FM_VOL, 0x9f1f);
1150 fm801_writew(chip, I2S_VOL, 0x8808);
1151
1152 /* I2S control - I2S mode */
1153 fm801_writew(chip, I2S_MODE, 0x0003);
1154
1155 /* interrupt setup */
1156 cmdw = fm801_readw(chip, IRQ_MASK);
1157 if (chip->irq < 0)
1158 cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
1159 else
1160 cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
1161 fm801_writew(chip, IRQ_MASK, cmdw);
1162
1163 /* interrupt clear */
1164 fm801_writew(chip, IRQ_STATUS,
1165 FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1166}
1167
1168static void snd_fm801_free(struct snd_card *card)
1169{
1170 struct fm801 *chip = card->private_data;
1171 unsigned short cmdw;
1172
1173 /* interrupt setup - mask everything */
1174 cmdw = fm801_readw(chip, IRQ_MASK);
1175 cmdw |= 0x00c3;
1176 fm801_writew(chip, IRQ_MASK, cmdw);
1177
1178#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1179 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1180 snd_tea575x_exit(&chip->tea);
1181 v4l2_device_unregister(&chip->v4l2_dev);
1182 }
1183#endif
1184}
1185
1186static int snd_fm801_create(struct snd_card *card,
1187 struct pci_dev *pci,
1188 int tea575x_tuner,
1189 int radio_nr)
1190{
1191 struct fm801 *chip = card->private_data;
1192 int err;
1193
1194 err = pcim_enable_device(pci);
1195 if (err < 0)
1196 return err;
1197 spin_lock_init(&chip->reg_lock);
1198 chip->card = card;
1199 chip->dev = &pci->dev;
1200 chip->irq = -1;
1201 chip->tea575x_tuner = tea575x_tuner;
1202 err = pci_request_regions(pci, "FM801");
1203 if (err < 0)
1204 return err;
1205 chip->port = pci_resource_start(pci, 0);
1206
1207#ifndef TARGET_OS2
1208 if (pci->revision >= 0xb1) /* FM801-AU */
1209#else
1210 if (snd_pci_revision(pci) >= 0xb1) /* FM801-AU */
1211#endif
1212 chip->multichannel = 1;
1213
1214 if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1215 if (reset_codec(chip) < 0) {
1216 dev_info(chip->card->dev,
1217 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1218 chip->tea575x_tuner = 3 | TUNER_ONLY;
1219 } else {
1220 snd_fm801_chip_multichannel_init(chip);
1221 }
1222 }
1223
1224 if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
1225 if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
1226 IRQF_SHARED, KBUILD_MODNAME, chip)) {
1227 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1228 return -EBUSY;
1229 }
1230 chip->irq = pci->irq;
1231 card->sync_irq = chip->irq;
1232 pci_set_master(pci);
1233 }
1234
1235 card->private_free = snd_fm801_free;
1236 snd_fm801_chip_init(chip);
1237
1238#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1239 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1240 if (err < 0)
1241 return err;
1242 chip->tea.v4l2_dev = &chip->v4l2_dev;
1243 chip->tea.radio_nr = radio_nr;
1244 chip->tea.private_data = chip;
1245 chip->tea.ops = &snd_fm801_tea_ops;
1246 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1247 if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1248 (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1249 if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1250 dev_err(card->dev, "TEA575x radio not found\n");
1251 return -ENODEV;
1252 }
1253 } else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1254 unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1255
1256 /* autodetect tuner connection */
1257 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1258 chip->tea575x_tuner = tea575x_tuner;
1259 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1260 dev_info(card->dev,
1261 "detected TEA575x radio type %s\n",
1262 get_tea575x_gpio(chip)->name);
1263 break;
1264 }
1265 }
1266 if (tea575x_tuner == 4) {
1267 dev_err(card->dev, "TEA575x radio not found\n");
1268 chip->tea575x_tuner = TUNER_DISABLED;
1269 }
1270
1271 chip->tea575x_tuner |= tuner_only;
1272 }
1273 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1274 strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1275 sizeof(chip->tea.card));
1276 }
1277#endif
1278 return 0;
1279}
1280
1281static int __snd_card_fm801_probe(struct pci_dev *pci,
1282 const struct pci_device_id *pci_id)
1283{
1284 static int dev;
1285 struct snd_card *card;
1286 struct fm801 *chip;
1287 struct snd_opl3 *opl3;
1288 int err;
1289
1290 if (dev >= SNDRV_CARDS)
1291 return -ENODEV;
1292 if (!enable[dev]) {
1293 dev++;
1294 return -ENOENT;
1295 }
1296
1297 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1298 sizeof(*chip), &card);
1299 if (err < 0)
1300 return err;
1301 chip = card->private_data;
1302 err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev]);
1303 if (err < 0)
1304 return err;
1305
1306 strcpy(card->driver, "FM801");
1307 strcpy(card->shortname, "ForteMedia FM801-");
1308 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1309 sprintf(card->longname, "%s at 0x%lx, irq %i",
1310 card->shortname, chip->port, chip->irq);
1311
1312 if (chip->tea575x_tuner & TUNER_ONLY)
1313 goto __fm801_tuner_only;
1314
1315 err = snd_fm801_pcm(chip, 0);
1316 if (err < 0)
1317 return err;
1318 err = snd_fm801_mixer(chip);
1319 if (err < 0)
1320 return err;
1321 err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1322 chip->port + FM801_MPU401_DATA,
1323 MPU401_INFO_INTEGRATED |
1324 MPU401_INFO_IRQ_HOOK,
1325 -1, &chip->rmidi);
1326 if (err < 0)
1327 return err;
1328 err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1329 chip->port + FM801_OPL3_BANK1,
1330 OPL3_HW_OPL3_FM801, 1, &opl3);
1331 if (err < 0)
1332 return err;
1333 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
1334 if (err < 0)
1335 return err;
1336
1337 __fm801_tuner_only:
1338 err = snd_card_register(card);
1339 if (err < 0)
1340 return err;
1341 pci_set_drvdata(pci, card);
1342 dev++;
1343 return 0;
1344}
1345
1346static int snd_card_fm801_probe(struct pci_dev *pci,
1347 const struct pci_device_id *pci_id)
1348{
1349 return snd_card_free_on_error(&pci->dev, __snd_card_fm801_probe(pci, pci_id));
1350}
1351
1352static const unsigned char saved_regs[] = {
1353 FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1354 FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1355 FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1356 FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1357};
1358
1359static int snd_fm801_suspend(struct device *dev)
1360{
1361 struct snd_card *card = dev_get_drvdata(dev);
1362 struct fm801 *chip = card->private_data;
1363 int i;
1364
1365 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1366
1367 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1368 chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
1369
1370 if (chip->tea575x_tuner & TUNER_ONLY) {
1371 /* FIXME: tea575x suspend */
1372 } else {
1373 snd_ac97_suspend(chip->ac97);
1374 snd_ac97_suspend(chip->ac97_sec);
1375 }
1376
1377 return 0;
1378}
1379
1380static int snd_fm801_resume(struct device *dev)
1381{
1382 struct snd_card *card = dev_get_drvdata(dev);
1383 struct fm801 *chip = card->private_data;
1384 int i;
1385
1386 if (chip->tea575x_tuner & TUNER_ONLY) {
1387 snd_fm801_chip_init(chip);
1388 } else {
1389 reset_codec(chip);
1390 snd_fm801_chip_multichannel_init(chip);
1391 snd_fm801_chip_init(chip);
1392 snd_ac97_resume(chip->ac97);
1393 snd_ac97_resume(chip->ac97_sec);
1394 }
1395
1396 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1397 fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1398
1399#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1400 if (!(chip->tea575x_tuner & TUNER_DISABLED))
1401 snd_tea575x_set_freq(&chip->tea);
1402#endif
1403
1404 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1405 return 0;
1406}
1407
1408static DEFINE_SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1409
1410static struct pci_driver fm801_driver = {
1411 .name = KBUILD_MODNAME,
1412 .id_table = snd_fm801_ids,
1413 .probe = snd_card_fm801_probe,
1414 .driver = {
1415 .pm = &snd_fm801_pm,
1416 },
1417};
1418
1419module_pci_driver(fm801_driver);
Note: See TracBrowser for help on using the repository browser.