fm801.c 41 KB

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