blob: 529f5f4f4c9cda9c31f1cb69fc6bd7c920443b34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The driver for the ForteMedia FM801 based soundcards
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andy Shevchenkoe0a5d822006-07-04 12:05:14 +02005 * Support FM only card by Andy Shevchenko <andy@smile.org.ua>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
Andy Shevchenko215dacc2014-04-29 10:56:03 +030026#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
28#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/pcm.h>
Takashi Iwai666c70f2006-08-23 12:32:06 +020032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/ac97_codec.h>
34#include <sound/mpu401.h>
35#include <sound/opl3.h>
36#include <sound/initval.h>
37
Adrian Bunkefce4bb2006-06-29 13:22:29 +020038#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Ondrej Zary59b56452013-07-28 16:01:43 -030039#include <media/tea575x.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#endif
41
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020042MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043MODULE_DESCRIPTION("ForteMedia FM801");
44MODULE_LICENSE("GPL");
45MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
46 "{Genius,SoundMaker Live 5.1}}");
47
48static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
49static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103050static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Enable TEA575x tuner
53 * 1 = MediaForte 256-PCS
Ondrej Zaryd7ba8582011-05-10 23:24:15 +020054 * 2 = MediaForte 256-PCP
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * 3 = MediaForte 64-PCR
Ondrej Zaryfb716c02009-11-27 18:18:33 +010056 * 16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * High 16-bits are video (radio) device number + 1
58 */
Takashi Iwai6581f4e2006-05-17 17:14:51 +020059static int tea575x_tuner[SNDRV_CARDS];
Hans Verkuild4ecc832012-02-27 05:30:13 -030060static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62module_param_array(index, int, NULL, 0444);
63MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
64module_param_array(id, charp, NULL, 0444);
65MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
66module_param_array(enable, bool, NULL, 0444);
67MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
68module_param_array(tea575x_tuner, int, NULL, 0444);
Ondrej Zaryd7ba8582011-05-10 23:24:15 +020069MODULE_PARM_DESC(tea575x_tuner, "TEA575x tuner access method (0 = auto, 1 = SF256-PCS, 2=SF256-PCP, 3=SF64-PCR, 8=disable, +16=tuner-only).");
Hans Verkuild4ecc832012-02-27 05:30:13 -030070module_param_array(radio_nr, int, NULL, 0444);
71MODULE_PARM_DESC(radio_nr, "Radio device numbers");
72
Ondrej Zaryfb716c02009-11-27 18:18:33 +010073
Ben Hutchingsc37279b2011-09-22 14:39:52 +010074#define TUNER_DISABLED (1<<3)
Ondrej Zaryfb716c02009-11-27 18:18:33 +010075#define TUNER_ONLY (1<<4)
76#define TUNER_TYPE_MASK (~TUNER_ONLY & 0xFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Direct registers
80 */
81
Andy Shevchenko215dacc2014-04-29 10:56:03 +030082#define fm801_writew(chip,reg,value) outw((value), chip->port + FM801_##reg)
83#define fm801_readw(chip,reg) inw(chip->port + FM801_##reg)
84
85#define fm801_writel(chip,reg,value) outl((value), chip->port + FM801_##reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#define FM801_PCM_VOL 0x00 /* PCM Output Volume */
88#define FM801_FM_VOL 0x02 /* FM Output Volume */
89#define FM801_I2S_VOL 0x04 /* I2S Volume */
90#define FM801_REC_SRC 0x06 /* Record Source */
91#define FM801_PLY_CTRL 0x08 /* Playback Control */
92#define FM801_PLY_COUNT 0x0a /* Playback Count */
93#define FM801_PLY_BUF1 0x0c /* Playback Bufer I */
94#define FM801_PLY_BUF2 0x10 /* Playback Buffer II */
95#define FM801_CAP_CTRL 0x14 /* Capture Control */
96#define FM801_CAP_COUNT 0x16 /* Capture Count */
97#define FM801_CAP_BUF1 0x18 /* Capture Buffer I */
98#define FM801_CAP_BUF2 0x1c /* Capture Buffer II */
99#define FM801_CODEC_CTRL 0x22 /* Codec Control */
100#define FM801_I2S_MODE 0x24 /* I2S Mode Control */
101#define FM801_VOLUME 0x26 /* Volume Up/Down/Mute Status */
102#define FM801_I2C_CTRL 0x29 /* I2C Control */
103#define FM801_AC97_CMD 0x2a /* AC'97 Command */
104#define FM801_AC97_DATA 0x2c /* AC'97 Data */
105#define FM801_MPU401_DATA 0x30 /* MPU401 Data */
106#define FM801_MPU401_CMD 0x31 /* MPU401 Command */
107#define FM801_GPIO_CTRL 0x52 /* General Purpose I/O Control */
108#define FM801_GEN_CTRL 0x54 /* General Control */
109#define FM801_IRQ_MASK 0x56 /* Interrupt Mask */
110#define FM801_IRQ_STATUS 0x5a /* Interrupt Status */
111#define FM801_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
112#define FM801_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
113#define FM801_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
114#define FM801_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
115#define FM801_POWERDOWN 0x70 /* Blocks Power Down Control */
116
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100117/* codec access */
118#define FM801_AC97_READ (1<<7) /* read=1, write=0 */
119#define FM801_AC97_VALID (1<<8) /* port valid=1 */
120#define FM801_AC97_BUSY (1<<9) /* busy=1 */
121#define FM801_AC97_ADDR_SHIFT 10 /* codec id (2bit) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* playback and record control register bits */
124#define FM801_BUF1_LAST (1<<1)
125#define FM801_BUF2_LAST (1<<2)
126#define FM801_START (1<<5)
127#define FM801_PAUSE (1<<6)
128#define FM801_IMMED_STOP (1<<7)
129#define FM801_RATE_SHIFT 8
130#define FM801_RATE_MASK (15 << FM801_RATE_SHIFT)
131#define FM801_CHANNELS_4 (1<<12) /* playback only */
132#define FM801_CHANNELS_6 (2<<12) /* playback only */
133#define FM801_CHANNELS_6MS (3<<12) /* playback only */
134#define FM801_CHANNELS_MASK (3<<12)
135#define FM801_16BIT (1<<14)
136#define FM801_STEREO (1<<15)
137
138/* IRQ status bits */
139#define FM801_IRQ_PLAYBACK (1<<8)
140#define FM801_IRQ_CAPTURE (1<<9)
141#define FM801_IRQ_VOLUME (1<<14)
142#define FM801_IRQ_MPU (1<<15)
143
144/* GPIO control register */
145#define FM801_GPIO_GP0 (1<<0) /* read/write */
146#define FM801_GPIO_GP1 (1<<1)
147#define FM801_GPIO_GP2 (1<<2)
148#define FM801_GPIO_GP3 (1<<3)
149#define FM801_GPIO_GP(x) (1<<(0+(x)))
150#define FM801_GPIO_GD0 (1<<8) /* directions: 1 = input, 0 = output*/
151#define FM801_GPIO_GD1 (1<<9)
152#define FM801_GPIO_GD2 (1<<10)
153#define FM801_GPIO_GD3 (1<<11)
154#define FM801_GPIO_GD(x) (1<<(8+(x)))
155#define FM801_GPIO_GS0 (1<<12) /* function select: */
156#define FM801_GPIO_GS1 (1<<13) /* 1 = GPIO */
157#define FM801_GPIO_GS2 (1<<14) /* 0 = other (S/PDIF, VOL) */
158#define FM801_GPIO_GS3 (1<<15)
159#define FM801_GPIO_GS(x) (1<<(12+(x)))
160
Andy Shevchenko052c2332014-05-19 14:07:28 +0300161/**
162 * struct fm801 - describes FM801 chip
163 * @port: I/O port number
164 * @multichannel: multichannel support
165 * @secondary: secondary codec
166 * @secondary_addr: address of the secondary codec
167 * @tea575x_tuner: tuner access method & flags
168 * @ply_ctrl: playback control
169 * @cap_ctrl: capture control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Takashi Iwaia5f22152005-11-17 15:04:28 +0100171struct fm801 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 int irq;
173
Andy Shevchenko052c2332014-05-19 14:07:28 +0300174 unsigned long port;
175 unsigned int multichannel: 1,
176 secondary: 1;
177 unsigned char secondary_addr;
178 unsigned int tea575x_tuner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Andy Shevchenko052c2332014-05-19 14:07:28 +0300180 unsigned short ply_ctrl;
181 unsigned short cap_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 unsigned long ply_buffer;
184 unsigned int ply_buf;
185 unsigned int ply_count;
186 unsigned int ply_size;
187 unsigned int ply_pos;
188
189 unsigned long cap_buffer;
190 unsigned int cap_buf;
191 unsigned int cap_count;
192 unsigned int cap_size;
193 unsigned int cap_pos;
194
Takashi Iwaia5f22152005-11-17 15:04:28 +0100195 struct snd_ac97_bus *ac97_bus;
196 struct snd_ac97 *ac97;
197 struct snd_ac97 *ac97_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 struct pci_dev *pci;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100200 struct snd_card *card;
201 struct snd_pcm *pcm;
202 struct snd_rawmidi *rmidi;
203 struct snd_pcm_substream *playback_substream;
204 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned int p_dma_size;
206 unsigned int c_dma_size;
207
208 spinlock_t reg_lock;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100209 struct snd_info_entry *proc_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ondrej Zaryfdb62b52011-05-14 22:51:01 +0200211#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Hans Verkuild4ecc832012-02-27 05:30:13 -0300212 struct v4l2_device v4l2_dev;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100213 struct snd_tea575x tea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100215
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200216#ifdef CONFIG_PM_SLEEP
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100217 u16 saved_regs[0x20];
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200221static DEFINE_PCI_DEVICE_TABLE(snd_fm801_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
Sergey Vlasov26be8652005-04-11 14:17:19 +0200223 { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* Gallant Odyssey Sound 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 { 0, }
225};
226
227MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
228
229/*
230 * common I/O routines
231 */
232
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300233static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
234{
235 unsigned int idx;
236
237 for (idx = 0; idx < iterations; idx++) {
238 if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
239 return true;
240 udelay(10);
241 }
242 return false;
243}
244
245static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
246{
247 unsigned int idx;
248
249 for (idx = 0; idx < iterations; idx++) {
250 if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
251 return true;
252 udelay(10);
253 }
254 return false;
255}
256
Takashi Iwaia5f22152005-11-17 15:04:28 +0100257static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned short mask, unsigned short value)
259{
260 int change;
261 unsigned long flags;
262 unsigned short old, new;
263
264 spin_lock_irqsave(&chip->reg_lock, flags);
265 old = inw(chip->port + reg);
266 new = (old & ~mask) | value;
267 change = old != new;
268 if (change)
269 outw(new, chip->port + reg);
270 spin_unlock_irqrestore(&chip->reg_lock, flags);
271 return change;
272}
273
Takashi Iwaia5f22152005-11-17 15:04:28 +0100274static void snd_fm801_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned short reg,
276 unsigned short val)
277{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100278 struct fm801 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /*
281 * Wait until the codec interface is not ready..
282 */
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300283 if (!fm801_ac97_is_ready(chip, 100)) {
284 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
285 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* write data and address */
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300289 fm801_writew(chip, AC97_DATA, val);
290 fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /*
292 * Wait until the write command is not completed..
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300293 */
294 if (!fm801_ac97_is_ready(chip, 1000))
295 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
296 ac97->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Takashi Iwaia5f22152005-11-17 15:04:28 +0100299static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100301 struct fm801 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /*
304 * Wait until the codec interface is not ready..
305 */
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300306 if (!fm801_ac97_is_ready(chip, 100)) {
307 dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* read command */
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300312 fm801_writew(chip, AC97_CMD,
313 reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300314 if (!fm801_ac97_is_ready(chip, 100)) {
315 dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
316 ac97->num);
317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andy Shevchenko02fd1a72014-04-29 10:56:04 +0300320 if (!fm801_ac97_is_valid(chip, 1000)) {
321 dev_err(chip->card->dev,
322 "AC'97 interface #%d is not valid (2)\n", ac97->num);
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300326 return fm801_readw(chip, AC97_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329static unsigned int rates[] = {
330 5500, 8000, 9600, 11025,
331 16000, 19200, 22050, 32000,
332 38400, 44100, 48000
333};
334
Takashi Iwaia5f22152005-11-17 15:04:28 +0100335static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .count = ARRAY_SIZE(rates),
337 .list = rates,
338 .mask = 0,
339};
340
341static unsigned int channels[] = {
342 2, 4, 6
343};
344
Takashi Iwaia5f22152005-11-17 15:04:28 +0100345static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
Tobias Klauser5e4968e2006-08-16 12:56:16 +0200346 .count = ARRAY_SIZE(channels),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .list = channels,
348 .mask = 0,
349};
350
351/*
352 * Sample rate routines
353 */
354
355static unsigned short snd_fm801_rate_bits(unsigned int rate)
356{
357 unsigned int idx;
358
359 for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
360 if (rates[idx] == rate)
361 return idx;
362 snd_BUG();
363 return ARRAY_SIZE(rates) - 1;
364}
365
366/*
367 * PCM part
368 */
369
Takashi Iwaia5f22152005-11-17 15:04:28 +0100370static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int cmd)
372{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100373 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 spin_lock(&chip->reg_lock);
376 switch (cmd) {
377 case SNDRV_PCM_TRIGGER_START:
378 chip->ply_ctrl &= ~(FM801_BUF1_LAST |
379 FM801_BUF2_LAST |
380 FM801_PAUSE);
381 chip->ply_ctrl |= FM801_START |
382 FM801_IMMED_STOP;
383 break;
384 case SNDRV_PCM_TRIGGER_STOP:
385 chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
386 break;
387 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100388 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 chip->ply_ctrl |= FM801_PAUSE;
390 break;
391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100392 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 chip->ply_ctrl &= ~FM801_PAUSE;
394 break;
395 default:
396 spin_unlock(&chip->reg_lock);
397 snd_BUG();
398 return -EINVAL;
399 }
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300400 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 spin_unlock(&chip->reg_lock);
402 return 0;
403}
404
Takashi Iwaia5f22152005-11-17 15:04:28 +0100405static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int cmd)
407{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100408 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock(&chip->reg_lock);
411 switch (cmd) {
412 case SNDRV_PCM_TRIGGER_START:
413 chip->cap_ctrl &= ~(FM801_BUF1_LAST |
414 FM801_BUF2_LAST |
415 FM801_PAUSE);
416 chip->cap_ctrl |= FM801_START |
417 FM801_IMMED_STOP;
418 break;
419 case SNDRV_PCM_TRIGGER_STOP:
420 chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
421 break;
422 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100423 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 chip->cap_ctrl |= FM801_PAUSE;
425 break;
426 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100427 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 chip->cap_ctrl &= ~FM801_PAUSE;
429 break;
430 default:
431 spin_unlock(&chip->reg_lock);
432 snd_BUG();
433 return -EINVAL;
434 }
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300435 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 spin_unlock(&chip->reg_lock);
437 return 0;
438}
439
Takashi Iwaia5f22152005-11-17 15:04:28 +0100440static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
444}
445
Takashi Iwaia5f22152005-11-17 15:04:28 +0100446static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 return snd_pcm_lib_free_pages(substream);
449}
450
Takashi Iwaia5f22152005-11-17 15:04:28 +0100451static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100453 struct fm801 *chip = snd_pcm_substream_chip(substream);
454 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
457 chip->ply_count = snd_pcm_lib_period_bytes(substream);
458 spin_lock_irq(&chip->reg_lock);
459 chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
460 FM801_STEREO | FM801_RATE_MASK |
461 FM801_CHANNELS_MASK);
462 if (snd_pcm_format_width(runtime->format) == 16)
463 chip->ply_ctrl |= FM801_16BIT;
464 if (runtime->channels > 1) {
465 chip->ply_ctrl |= FM801_STEREO;
466 if (runtime->channels == 4)
467 chip->ply_ctrl |= FM801_CHANNELS_4;
468 else if (runtime->channels == 6)
469 chip->ply_ctrl |= FM801_CHANNELS_6;
470 }
471 chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
472 chip->ply_buf = 0;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300473 fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
474 fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 chip->ply_buffer = runtime->dma_addr;
476 chip->ply_pos = 0;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300477 fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
478 fm801_writel(chip, PLY_BUF2,
479 chip->ply_buffer + (chip->ply_count % chip->ply_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 spin_unlock_irq(&chip->reg_lock);
481 return 0;
482}
483
Takashi Iwaia5f22152005-11-17 15:04:28 +0100484static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100486 struct fm801 *chip = snd_pcm_substream_chip(substream);
487 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
490 chip->cap_count = snd_pcm_lib_period_bytes(substream);
491 spin_lock_irq(&chip->reg_lock);
492 chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
493 FM801_STEREO | FM801_RATE_MASK);
494 if (snd_pcm_format_width(runtime->format) == 16)
495 chip->cap_ctrl |= FM801_16BIT;
496 if (runtime->channels > 1)
497 chip->cap_ctrl |= FM801_STEREO;
498 chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
499 chip->cap_buf = 0;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300500 fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
501 fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 chip->cap_buffer = runtime->dma_addr;
503 chip->cap_pos = 0;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300504 fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
505 fm801_writel(chip, CAP_BUF2,
506 chip->cap_buffer + (chip->cap_count % chip->cap_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_unlock_irq(&chip->reg_lock);
508 return 0;
509}
510
Takashi Iwaia5f22152005-11-17 15:04:28 +0100511static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100513 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 size_t ptr;
515
516 if (!(chip->ply_ctrl & FM801_START))
517 return 0;
518 spin_lock(&chip->reg_lock);
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300519 ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
520 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ptr += chip->ply_count;
522 ptr %= chip->ply_size;
523 }
524 spin_unlock(&chip->reg_lock);
525 return bytes_to_frames(substream->runtime, ptr);
526}
527
Takashi Iwaia5f22152005-11-17 15:04:28 +0100528static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100530 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 size_t ptr;
532
533 if (!(chip->cap_ctrl & FM801_START))
534 return 0;
535 spin_lock(&chip->reg_lock);
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300536 ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
537 if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ptr += chip->cap_count;
539 ptr %= chip->cap_size;
540 }
541 spin_unlock(&chip->reg_lock);
542 return bytes_to_frames(substream->runtime, ptr);
543}
544
David Howells7d12e782006-10-05 14:55:46 +0100545static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100547 struct fm801 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 unsigned short status;
549 unsigned int tmp;
550
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300551 status = fm801_readw(chip, IRQ_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
553 if (! status)
554 return IRQ_NONE;
555 /* ack first */
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300556 fm801_writew(chip, IRQ_STATUS, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
558 spin_lock(&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;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300564 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 spin_unlock(&chip->reg_lock);
569 snd_pcm_period_elapsed(chip->playback_substream);
570 }
571 if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
572 spin_lock(&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;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300578 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 spin_unlock(&chip->reg_lock);
583 snd_pcm_period_elapsed(chip->capture_substream);
584 }
585 if (chip->rmidi && (status & FM801_IRQ_MPU))
David Howells7d12e782006-10-05 14:55:46 +0100586 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (status & FM801_IRQ_VOLUME)
588 ;/* TODO */
589
590 return IRQ_HANDLED;
591}
592
Takashi Iwaia5f22152005-11-17 15:04:28 +0100593static struct snd_pcm_hardware snd_fm801_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
596 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100597 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 SNDRV_PCM_INFO_MMAP_VALID),
599 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
600 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
601 .rate_min = 5500,
602 .rate_max = 48000,
603 .channels_min = 1,
604 .channels_max = 2,
605 .buffer_bytes_max = (128*1024),
606 .period_bytes_min = 64,
607 .period_bytes_max = (128*1024),
608 .periods_min = 1,
609 .periods_max = 1024,
610 .fifo_size = 0,
611};
612
Takashi Iwaia5f22152005-11-17 15:04:28 +0100613static struct snd_pcm_hardware snd_fm801_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
616 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwaib1e9ed22005-11-17 16:14:33 +0100617 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 SNDRV_PCM_INFO_MMAP_VALID),
619 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
620 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
621 .rate_min = 5500,
622 .rate_max = 48000,
623 .channels_min = 1,
624 .channels_max = 2,
625 .buffer_bytes_max = (128*1024),
626 .period_bytes_min = 64,
627 .period_bytes_max = (128*1024),
628 .periods_min = 1,
629 .periods_max = 1024,
630 .fifo_size = 0,
631};
632
Takashi Iwaia5f22152005-11-17 15:04:28 +0100633static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100635 struct fm801 *chip = snd_pcm_substream_chip(substream);
636 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int err;
638
639 chip->playback_substream = substream;
640 runtime->hw = snd_fm801_playback;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100641 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
642 &hw_constraints_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (chip->multichannel) {
644 runtime->hw.channels_max = 6;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100645 snd_pcm_hw_constraint_list(runtime, 0,
646 SNDRV_PCM_HW_PARAM_CHANNELS,
647 &hw_constraints_channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
650 return err;
651 return 0;
652}
653
Takashi Iwaia5f22152005-11-17 15:04:28 +0100654static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100656 struct fm801 *chip = snd_pcm_substream_chip(substream);
657 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int err;
659
660 chip->capture_substream = substream;
661 runtime->hw = snd_fm801_capture;
Takashi Iwaia5f22152005-11-17 15:04:28 +0100662 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
663 &hw_constraints_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
665 return err;
666 return 0;
667}
668
Takashi Iwaia5f22152005-11-17 15:04:28 +0100669static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100671 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 chip->playback_substream = NULL;
674 return 0;
675}
676
Takashi Iwaia5f22152005-11-17 15:04:28 +0100677static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100679 struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 chip->capture_substream = NULL;
682 return 0;
683}
684
Takashi Iwaia5f22152005-11-17 15:04:28 +0100685static struct snd_pcm_ops snd_fm801_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .open = snd_fm801_playback_open,
687 .close = snd_fm801_playback_close,
688 .ioctl = snd_pcm_lib_ioctl,
689 .hw_params = snd_fm801_hw_params,
690 .hw_free = snd_fm801_hw_free,
691 .prepare = snd_fm801_playback_prepare,
692 .trigger = snd_fm801_playback_trigger,
693 .pointer = snd_fm801_playback_pointer,
694};
695
Takashi Iwaia5f22152005-11-17 15:04:28 +0100696static struct snd_pcm_ops snd_fm801_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 .open = snd_fm801_capture_open,
698 .close = snd_fm801_capture_close,
699 .ioctl = snd_pcm_lib_ioctl,
700 .hw_params = snd_fm801_hw_params,
701 .hw_free = snd_fm801_hw_free,
702 .prepare = snd_fm801_capture_prepare,
703 .trigger = snd_fm801_capture_trigger,
704 .pointer = snd_fm801_capture_pointer,
705};
706
Bill Pembertone23e7a12012-12-06 12:35:10 -0500707static int snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100709 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int err;
711
712 if (rpcm)
713 *rpcm = NULL;
714 if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
715 return err;
716
717 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
718 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
719
720 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 pcm->info_flags = 0;
722 strcpy(pcm->name, "FM801");
723 chip->pcm = pcm;
724
725 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
726 snd_dma_pci_data(chip->pci),
727 chip->multichannel ? 128*1024 : 64*1024, 128*1024);
728
Takashi Iwaie36e3b82012-08-03 18:01:40 +0200729 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
730 snd_pcm_alt_chmaps,
731 chip->multichannel ? 6 : 2, 0,
732 NULL);
733 if (err < 0)
734 return err;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (rpcm)
737 *rpcm = pcm;
738 return 0;
739}
740
741/*
742 * TEA5757 radio
743 */
744
Ondrej Zaryfdb62b52011-05-14 22:51:01 +0200745#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Ondrej Zary938a1562011-05-09 23:39:51 +0200747/* GPIO to TEA575x maps */
748struct snd_fm801_tea575x_gpio {
749 u8 data, clk, wren, most;
Ondrej Zaryd7ba8582011-05-10 23:24:15 +0200750 char *name;
Ondrej Zary938a1562011-05-09 23:39:51 +0200751};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Ondrej Zary938a1562011-05-09 23:39:51 +0200753static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
Ondrej Zaryd7ba8582011-05-10 23:24:15 +0200754 { .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
755 { .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
756 { .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
Ondrej Zary938a1562011-05-09 23:39:51 +0200757};
758
Takashi Iwai8e699d22011-09-22 16:54:23 +0200759#define get_tea575x_gpio(chip) \
760 (&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
761
Ondrej Zary938a1562011-05-09 23:39:51 +0200762static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100764 struct fm801 *chip = tea->private_data;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300765 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
Takashi Iwai8e699d22011-09-22 16:54:23 +0200766 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Ondrej Zary938a1562011-05-09 23:39:51 +0200768 reg &= ~(FM801_GPIO_GP(gpio.data) |
769 FM801_GPIO_GP(gpio.clk) |
770 FM801_GPIO_GP(gpio.wren));
771
772 reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
773 reg |= (pins & TEA575X_CLK) ? FM801_GPIO_GP(gpio.clk) : 0;
774 /* WRITE_ENABLE is inverted */
775 reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
776
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300777 fm801_writew(chip, GPIO_CTRL, reg);
Ondrej Zary938a1562011-05-09 23:39:51 +0200778}
779
780static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
781{
782 struct fm801 *chip = tea->private_data;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300783 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
Takashi Iwai8e699d22011-09-22 16:54:23 +0200784 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
Dan Carpentereffded72012-11-14 11:23:54 +0300785 u8 ret;
Ondrej Zary938a1562011-05-09 23:39:51 +0200786
Dan Carpentereffded72012-11-14 11:23:54 +0300787 ret = 0;
788 if (reg & FM801_GPIO_GP(gpio.data))
789 ret |= TEA575X_DATA;
790 if (reg & FM801_GPIO_GP(gpio.most))
791 ret |= TEA575X_MOST;
792 return ret;
Ondrej Zary938a1562011-05-09 23:39:51 +0200793}
794
795static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
796{
797 struct fm801 *chip = tea->private_data;
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300798 unsigned short reg = fm801_readw(chip, GPIO_CTRL);
Takashi Iwai8e699d22011-09-22 16:54:23 +0200799 struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
Ondrej Zary938a1562011-05-09 23:39:51 +0200800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* use GPIO lines and set write enable bit */
Ondrej Zary938a1562011-05-09 23:39:51 +0200802 reg |= FM801_GPIO_GS(gpio.data) |
803 FM801_GPIO_GS(gpio.wren) |
804 FM801_GPIO_GS(gpio.clk) |
805 FM801_GPIO_GS(gpio.most);
806 if (output) {
807 /* all of lines are in the write direction */
808 /* clear data and clock lines */
809 reg &= ~(FM801_GPIO_GD(gpio.data) |
810 FM801_GPIO_GD(gpio.wren) |
811 FM801_GPIO_GD(gpio.clk) |
812 FM801_GPIO_GP(gpio.data) |
813 FM801_GPIO_GP(gpio.clk) |
814 FM801_GPIO_GP(gpio.wren));
815 } else {
816 /* use GPIO lines, set data direction to input */
817 reg |= FM801_GPIO_GD(gpio.data) |
818 FM801_GPIO_GD(gpio.most) |
819 FM801_GPIO_GP(gpio.data) |
820 FM801_GPIO_GP(gpio.most) |
821 FM801_GPIO_GP(gpio.wren);
822 /* all of lines are in the write direction, except data */
823 /* clear data, write enable and clock lines */
824 reg &= ~(FM801_GPIO_GD(gpio.wren) |
825 FM801_GPIO_GD(gpio.clk) |
826 FM801_GPIO_GP(gpio.clk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300829 fm801_writew(chip, GPIO_CTRL, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Ondrej Zary938a1562011-05-09 23:39:51 +0200832static struct snd_tea575x_ops snd_fm801_tea_ops = {
833 .set_pins = snd_fm801_tea575x_set_pins,
834 .get_pins = snd_fm801_tea575x_get_pins,
835 .set_direction = snd_fm801_tea575x_set_direction,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836};
837#endif
838
839/*
840 * Mixer routines
841 */
842
843#define FM801_SINGLE(xname, reg, shift, mask, invert) \
844{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
845 .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
846 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
847
Takashi Iwaia5f22152005-11-17 15:04:28 +0100848static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
849 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 int mask = (kcontrol->private_value >> 16) & 0xff;
852
853 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
854 uinfo->count = 1;
855 uinfo->value.integer.min = 0;
856 uinfo->value.integer.max = mask;
857 return 0;
858}
859
Takashi Iwaia5f22152005-11-17 15:04:28 +0100860static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
861 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100863 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int reg = kcontrol->private_value & 0xff;
865 int shift = (kcontrol->private_value >> 8) & 0xff;
866 int mask = (kcontrol->private_value >> 16) & 0xff;
867 int invert = (kcontrol->private_value >> 24) & 0xff;
868
869 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
870 if (invert)
871 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
872 return 0;
873}
874
Takashi Iwaia5f22152005-11-17 15:04:28 +0100875static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
876 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100878 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int reg = kcontrol->private_value & 0xff;
880 int shift = (kcontrol->private_value >> 8) & 0xff;
881 int mask = (kcontrol->private_value >> 16) & 0xff;
882 int invert = (kcontrol->private_value >> 24) & 0xff;
883 unsigned short val;
884
885 val = (ucontrol->value.integer.value[0] & mask);
886 if (invert)
887 val = mask - val;
888 return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
889}
890
891#define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
892{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
893 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
894 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
Takashi Iwai666c70f2006-08-23 12:32:06 +0200895#define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
896{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
897 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
898 .name = xname, .info = snd_fm801_info_double, \
899 .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
900 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
901 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Takashi Iwaia5f22152005-11-17 15:04:28 +0100903static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 int mask = (kcontrol->private_value >> 16) & 0xff;
907
908 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
909 uinfo->count = 2;
910 uinfo->value.integer.min = 0;
911 uinfo->value.integer.max = mask;
912 return 0;
913}
914
Takashi Iwaia5f22152005-11-17 15:04:28 +0100915static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
916 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100918 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int reg = kcontrol->private_value & 0xff;
920 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
921 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
922 int mask = (kcontrol->private_value >> 16) & 0xff;
923 int invert = (kcontrol->private_value >> 24) & 0xff;
924
925 spin_lock_irq(&chip->reg_lock);
926 ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
927 ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
928 spin_unlock_irq(&chip->reg_lock);
929 if (invert) {
930 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
931 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
932 }
933 return 0;
934}
935
Takashi Iwaia5f22152005-11-17 15:04:28 +0100936static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
937 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100939 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int reg = kcontrol->private_value & 0xff;
941 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
942 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
943 int mask = (kcontrol->private_value >> 16) & 0xff;
944 int invert = (kcontrol->private_value >> 24) & 0xff;
945 unsigned short val1, val2;
946
947 val1 = ucontrol->value.integer.value[0] & mask;
948 val2 = ucontrol->value.integer.value[1] & mask;
949 if (invert) {
950 val1 = mask - val1;
951 val2 = mask - val2;
952 }
953 return snd_fm801_update_bits(chip, reg,
954 (mask << shift_left) | (mask << shift_right),
955 (val1 << shift_left ) | (val2 << shift_right));
956}
957
Takashi Iwaia5f22152005-11-17 15:04:28 +0100958static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
959 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 static char *texts[5] = {
962 "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
963 };
964
965 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
966 uinfo->count = 1;
967 uinfo->value.enumerated.items = 5;
968 if (uinfo->value.enumerated.item > 4)
969 uinfo->value.enumerated.item = 4;
970 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
971 return 0;
972}
973
Takashi Iwaia5f22152005-11-17 15:04:28 +0100974static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
975 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100977 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned short val;
979
Andy Shevchenko215dacc2014-04-29 10:56:03 +0300980 val = fm801_readw(chip, REC_SRC) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (val > 4)
982 val = 4;
983 ucontrol->value.enumerated.item[0] = val;
984 return 0;
985}
986
Takashi Iwaia5f22152005-11-17 15:04:28 +0100987static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
988 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Takashi Iwaia5f22152005-11-17 15:04:28 +0100990 struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned short val;
992
993 if ((val = ucontrol->value.enumerated.item[0]) > 4)
994 return -EINVAL;
995 return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
996}
997
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100998static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
Takashi Iwai666c70f2006-08-23 12:32:06 +0200999
Takashi Iwaia5f22152005-11-17 15:04:28 +01001000#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Bill Pembertone23e7a12012-12-06 12:35:10 -05001002static struct snd_kcontrol_new snd_fm801_controls[] = {
Takashi Iwai666c70f2006-08-23 12:32:06 +02001003FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1004 db_scale_dsp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
Takashi Iwai666c70f2006-08-23 12:32:06 +02001006FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1007 db_scale_dsp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
Takashi Iwai666c70f2006-08-23 12:32:06 +02001009FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1010 db_scale_dsp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
1012{
1013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1014 .name = "Digital Capture Source",
1015 .info = snd_fm801_info_mux,
1016 .get = snd_fm801_get_mux,
1017 .put = snd_fm801_put_mux,
1018}
1019};
1020
Takashi Iwaia5f22152005-11-17 15:04:28 +01001021#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Bill Pembertone23e7a12012-12-06 12:35:10 -05001023static struct snd_kcontrol_new snd_fm801_controls_multi[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
1025FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001026FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
1027FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
1028FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
1029FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
Takashi Iwaia5f22152005-11-17 15:04:28 +01001032static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwaia5f22152005-11-17 15:04:28 +01001034 struct fm801 *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 chip->ac97_bus = NULL;
1036}
1037
Takashi Iwaia5f22152005-11-17 15:04:28 +01001038static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Takashi Iwaia5f22152005-11-17 15:04:28 +01001040 struct fm801 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (ac97->num == 0) {
1042 chip->ac97 = NULL;
1043 } else {
1044 chip->ac97_sec = NULL;
1045 }
1046}
1047
Bill Pembertone23e7a12012-12-06 12:35:10 -05001048static int snd_fm801_mixer(struct fm801 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwaia5f22152005-11-17 15:04:28 +01001050 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned int i;
1052 int err;
Takashi Iwaia5f22152005-11-17 15:04:28 +01001053 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 .write = snd_fm801_codec_write,
1055 .read = snd_fm801_codec_read,
1056 };
1057
1058 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1059 return err;
1060 chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
1061
1062 memset(&ac97, 0, sizeof(ac97));
1063 ac97.private_data = chip;
1064 ac97.private_free = snd_fm801_mixer_free_ac97;
1065 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1066 return err;
1067 if (chip->secondary) {
1068 ac97.num = 1;
1069 ac97.addr = chip->secondary_addr;
1070 if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
1071 return err;
1072 }
1073 for (i = 0; i < FM801_CONTROLS; i++)
1074 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
1075 if (chip->multichannel) {
1076 for (i = 0; i < FM801_CONTROLS_MULTI; i++)
1077 snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1078 }
1079 return 0;
1080}
1081
1082/*
1083 * initialization routines
1084 */
1085
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001086static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1087 unsigned short reg, unsigned long waits)
1088{
1089 unsigned long timeout = jiffies + waits;
1090
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001091 fm801_writew(chip, AC97_CMD,
1092 reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001093 udelay(5);
1094 do {
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001095 if ((fm801_readw(chip, AC97_CMD) &
1096 (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001097 return 0;
1098 schedule_timeout_uninterruptible(1);
1099 } while (time_after(timeout, jiffies));
1100 return -EIO;
1101}
1102
1103static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1104{
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001105 unsigned short cmdw;
1106
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001107 if (chip->tea575x_tuner & TUNER_ONLY)
Andy Shevchenkoe0a5d822006-07-04 12:05:14 +02001108 goto __ac97_ok;
1109
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001110 /* codec cold reset + AC'97 warm reset */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001111 fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1112 fm801_readw(chip, CODEC_CTRL); /* flush posting data */
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001113 udelay(100);
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001114 fm801_writew(chip, CODEC_CTRL, 0);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001115
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001116 if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
1117 if (!resume) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001118 dev_info(chip->card->dev,
1119 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001120 chip->tea575x_tuner = 3 | TUNER_ONLY;
1121 goto __ac97_ok;
1122 }
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001123
1124 if (chip->multichannel) {
1125 if (chip->secondary_addr) {
1126 wait_for_codec(chip, chip->secondary_addr,
1127 AC97_VENDOR_ID1, msecs_to_jiffies(50));
1128 } else {
1129 /* my card has the secondary codec */
1130 /* at address #3, so the loop is inverted */
Harvey Harrison58e43342008-02-28 11:55:07 +01001131 int i;
1132 for (i = 3; i > 0; i--) {
1133 if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001134 msecs_to_jiffies(50))) {
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001135 cmdw = fm801_readw(chip, AC97_DATA);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001136 if (cmdw != 0xffff && cmdw != 0) {
1137 chip->secondary = 1;
Harvey Harrison58e43342008-02-28 11:55:07 +01001138 chip->secondary_addr = i;
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001139 break;
1140 }
1141 }
1142 }
1143 }
1144
1145 /* the recovery phase, it seems that probing for non-existing codec might */
1146 /* cause timeout problems */
1147 wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1148 }
1149
Jaroslav Kysela6bbe13e2006-07-04 13:39:55 +02001150 __ac97_ok:
1151
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001152 /* init volume */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001153 fm801_writew(chip, PCM_VOL, 0x0808);
1154 fm801_writew(chip, FM_VOL, 0x9f1f);
1155 fm801_writew(chip, I2S_VOL, 0x8808);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001156
1157 /* I2S control - I2S mode */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001158 fm801_writew(chip, I2S_MODE, 0x0003);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001159
Jaroslav Kysela6bbe13e2006-07-04 13:39:55 +02001160 /* interrupt setup */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001161 cmdw = fm801_readw(chip, IRQ_MASK);
Jaroslav Kysela6bbe13e2006-07-04 13:39:55 +02001162 if (chip->irq < 0)
1163 cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
1164 else
1165 cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001166 fm801_writew(chip, IRQ_MASK, cmdw);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001167
1168 /* interrupt clear */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001169 fm801_writew(chip, IRQ_STATUS,
1170 FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001171
1172 return 0;
1173}
1174
1175
Takashi Iwaia5f22152005-11-17 15:04:28 +01001176static int snd_fm801_free(struct fm801 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 unsigned short cmdw;
1179
1180 if (chip->irq < 0)
1181 goto __end_hw;
1182
1183 /* interrupt setup - mask everything */
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001184 cmdw = fm801_readw(chip, IRQ_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 cmdw |= 0x00c3;
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001186 fm801_writew(chip, IRQ_MASK, cmdw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 __end_hw:
Ondrej Zaryfdb62b52011-05-14 22:51:01 +02001189#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Hans Verkuild4ecc832012-02-27 05:30:13 -03001190 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
Ben Hutchingsc37279b2011-09-22 14:39:52 +01001191 snd_tea575x_exit(&chip->tea);
Hans Verkuild4ecc832012-02-27 05:30:13 -03001192 v4l2_device_unregister(&chip->v4l2_dev);
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#endif
1195 if (chip->irq >= 0)
Takashi Iwaia5f22152005-11-17 15:04:28 +01001196 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 pci_release_regions(chip->pci);
1198 pci_disable_device(chip->pci);
1199
1200 kfree(chip);
1201 return 0;
1202}
1203
Takashi Iwaia5f22152005-11-17 15:04:28 +01001204static int snd_fm801_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Takashi Iwaia5f22152005-11-17 15:04:28 +01001206 struct fm801 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return snd_fm801_free(chip);
1208}
1209
Bill Pembertone23e7a12012-12-06 12:35:10 -05001210static int snd_fm801_create(struct snd_card *card,
1211 struct pci_dev *pci,
1212 int tea575x_tuner,
1213 int radio_nr,
1214 struct fm801 **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Takashi Iwaia5f22152005-11-17 15:04:28 +01001216 struct fm801 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int err;
Takashi Iwaia5f22152005-11-17 15:04:28 +01001218 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 .dev_free = snd_fm801_dev_free,
1220 };
1221
1222 *rchip = NULL;
1223 if ((err = pci_enable_device(pci)) < 0)
1224 return err;
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001225 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (chip == NULL) {
1227 pci_disable_device(pci);
1228 return -ENOMEM;
1229 }
1230 spin_lock_init(&chip->reg_lock);
1231 chip->card = card;
1232 chip->pci = pci;
1233 chip->irq = -1;
Jaroslav Kysela6bbe13e2006-07-04 13:39:55 +02001234 chip->tea575x_tuner = tea575x_tuner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if ((err = pci_request_regions(pci, "FM801")) < 0) {
1236 kfree(chip);
1237 pci_disable_device(pci);
1238 return err;
1239 }
1240 chip->port = pci_resource_start(pci, 0);
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001241 if ((tea575x_tuner & TUNER_ONLY) == 0) {
Takashi Iwai437a5a42006-11-21 12:14:23 +01001242 if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02001243 KBUILD_MODNAME, chip)) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001244 dev_err(card->dev, "unable to grab IRQ %d\n", chip->irq);
Jaroslav Kysela6bbe13e2006-07-04 13:39:55 +02001245 snd_fm801_free(chip);
1246 return -EBUSY;
1247 }
1248 chip->irq = pci->irq;
1249 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Auke Kok44c10132007-06-08 15:46:36 -07001252 if (pci->revision >= 0xb1) /* FM801-AU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 chip->multichannel = 1;
1254
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001255 snd_fm801_chip_init(chip, 0);
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001256 /* init might set tuner access method */
1257 tea575x_tuner = chip->tea575x_tuner;
1258
1259 if (chip->irq >= 0 && (tea575x_tuner & TUNER_ONLY)) {
1260 pci_clear_master(pci);
1261 free_irq(chip->irq, chip);
1262 chip->irq = -1;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1266 snd_fm801_free(chip);
1267 return err;
1268 }
1269
Ondrej Zaryfdb62b52011-05-14 22:51:01 +02001270#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Hans Verkuild4ecc832012-02-27 05:30:13 -03001271 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1272 if (err < 0) {
1273 snd_fm801_free(chip);
1274 return err;
1275 }
1276 chip->tea.v4l2_dev = &chip->v4l2_dev;
1277 chip->tea.radio_nr = radio_nr;
Ondrej Zaryd7ba8582011-05-10 23:24:15 +02001278 chip->tea.private_data = chip;
1279 chip->tea.ops = &snd_fm801_tea_ops;
Ondrej Zary10ca7202011-05-12 22:18:22 +02001280 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001281 if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1282 (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
Hans de Goede5daf53a2012-05-19 07:57:03 -03001283 if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001284 dev_err(card->dev, "TEA575x radio not found\n");
Hans Verkuild4ecc832012-02-27 05:30:13 -03001285 snd_fm801_free(chip);
Dan Carpenter96760012011-05-26 11:47:35 +03001286 return -ENODEV;
1287 }
1288 } else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
Ondrej Zaryd7ba8582011-05-10 23:24:15 +02001289 /* autodetect tuner connection */
1290 for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1291 chip->tea575x_tuner = tea575x_tuner;
Hans de Goede5daf53a2012-05-19 07:57:03 -03001292 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001293 dev_info(card->dev,
1294 "detected TEA575x radio type %s\n",
Takashi Iwai8e699d22011-09-22 16:54:23 +02001295 get_tea575x_gpio(chip)->name);
Ondrej Zaryd7ba8582011-05-10 23:24:15 +02001296 break;
1297 }
1298 }
Dan Carpenter96760012011-05-26 11:47:35 +03001299 if (tea575x_tuner == 4) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001300 dev_err(card->dev, "TEA575x radio not found\n");
Ben Hutchingsc37279b2011-09-22 14:39:52 +01001301 chip->tea575x_tuner = TUNER_DISABLED;
Dan Carpenter96760012011-05-26 11:47:35 +03001302 }
1303 }
Ben Hutchingsc37279b2011-09-22 14:39:52 +01001304 if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
Takashi Iwai8e699d22011-09-22 16:54:23 +02001305 strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
Ben Hutchingsc37279b2011-09-22 14:39:52 +01001306 sizeof(chip->tea.card));
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308#endif
1309
1310 *rchip = chip;
1311 return 0;
1312}
1313
Bill Pembertone23e7a12012-12-06 12:35:10 -05001314static int snd_card_fm801_probe(struct pci_dev *pci,
1315 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 static int dev;
Takashi Iwaia5f22152005-11-17 15:04:28 +01001318 struct snd_card *card;
1319 struct fm801 *chip;
1320 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int err;
1322
1323 if (dev >= SNDRV_CARDS)
1324 return -ENODEV;
1325 if (!enable[dev]) {
1326 dev++;
1327 return -ENOENT;
1328 }
1329
Takashi Iwai60c57722014-01-29 14:20:19 +01001330 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1331 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01001332 if (err < 0)
1333 return err;
Hans Verkuild4ecc832012-02-27 05:30:13 -03001334 if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 snd_card_free(card);
1336 return err;
1337 }
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001338 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 strcpy(card->driver, "FM801");
1341 strcpy(card->shortname, "ForteMedia FM801-");
1342 strcat(card->shortname, chip->multichannel ? "AU" : "AS");
1343 sprintf(card->longname, "%s at 0x%lx, irq %i",
1344 card->shortname, chip->port, chip->irq);
1345
Ondrej Zaryfb716c02009-11-27 18:18:33 +01001346 if (chip->tea575x_tuner & TUNER_ONLY)
Andy Shevchenkoe0a5d822006-07-04 12:05:14 +02001347 goto __fm801_tuner_only;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
1350 snd_card_free(card);
1351 return err;
1352 }
1353 if ((err = snd_fm801_mixer(chip)) < 0) {
1354 snd_card_free(card);
1355 return err;
1356 }
1357 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001358 chip->port + FM801_MPU401_DATA,
Clemens Ladischdba8b462011-09-13 11:24:41 +02001359 MPU401_INFO_INTEGRATED |
1360 MPU401_INFO_IRQ_HOOK,
1361 -1, &chip->rmidi)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 snd_card_free(card);
1363 return err;
1364 }
Andy Shevchenko215dacc2014-04-29 10:56:03 +03001365 if ((err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1366 chip->port + FM801_OPL3_BANK1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
1368 snd_card_free(card);
1369 return err;
1370 }
1371 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1372 snd_card_free(card);
1373 return err;
1374 }
1375
Andy Shevchenkoe0a5d822006-07-04 12:05:14 +02001376 __fm801_tuner_only:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if ((err = snd_card_register(card)) < 0) {
1378 snd_card_free(card);
1379 return err;
1380 }
1381 pci_set_drvdata(pci, card);
1382 dev++;
1383 return 0;
1384}
1385
Bill Pembertone23e7a12012-12-06 12:35:10 -05001386static void snd_card_fm801_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001391#ifdef CONFIG_PM_SLEEP
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001392static unsigned char saved_regs[] = {
1393 FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1394 FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1395 FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1396 FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1397};
1398
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001399static int snd_fm801_suspend(struct device *dev)
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001400{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001401 struct pci_dev *pci = to_pci_dev(dev);
1402 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001403 struct fm801 *chip = card->private_data;
1404 int i;
1405
1406 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1407 snd_pcm_suspend_all(chip->pcm);
1408 snd_ac97_suspend(chip->ac97);
1409 snd_ac97_suspend(chip->ac97_sec);
1410 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1411 chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1412 /* FIXME: tea575x suspend */
1413
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001414 pci_disable_device(pci);
1415 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001416 pci_set_power_state(pci, PCI_D3hot);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001417 return 0;
1418}
1419
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001420static int snd_fm801_resume(struct device *dev)
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001421{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001422 struct pci_dev *pci = to_pci_dev(dev);
1423 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001424 struct fm801 *chip = card->private_data;
1425 int i;
1426
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001427 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02001428 pci_restore_state(pci);
1429 if (pci_enable_device(pci) < 0) {
Takashi Iwai9c7f9ab2014-02-25 14:31:35 +01001430 dev_err(dev, "pci_enable_device failed, disabling device\n");
Takashi Iwai30b35392006-10-11 18:52:53 +02001431 snd_card_disconnect(card);
1432 return -EIO;
1433 }
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001434 pci_set_master(pci);
1435
1436 snd_fm801_chip_init(chip, 1);
1437 snd_ac97_resume(chip->ac97);
1438 snd_ac97_resume(chip->ac97_sec);
1439 for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1440 outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1441
1442 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1443 return 0;
1444}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001445
1446static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1447#define SND_FM801_PM_OPS &snd_fm801_pm
1448#else
1449#define SND_FM801_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001450#endif /* CONFIG_PM_SLEEP */
Takashi Iwaib1e9ed22005-11-17 16:14:33 +01001451
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001452static struct pci_driver fm801_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02001453 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 .id_table = snd_fm801_ids,
1455 .probe = snd_card_fm801_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05001456 .remove = snd_card_fm801_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001457 .driver = {
1458 .pm = SND_FM801_PM_OPS,
1459 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460};
1461
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001462module_pci_driver(fm801_driver);