fm801.c 42 KB

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