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

source code of linux/sound/pci/fm801.c