fm801.c 42 KB

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