fm801.c 41 KB

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