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