fm801.c 41 KB

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