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

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

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