fm801.c 41 KB

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