fm801.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * The driver for the ForteMedia FM801 based soundcards
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/pci.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <sound/core.h>
  14. #include <sound/pcm.h>
  15. #include <sound/tlv.h>
  16. #include <sound/ac97_codec.h>
  17. #include <sound/mpu401.h>
  18. #include <sound/opl3.h>
  19. #include <sound/initval.h>
  20. #ifdef CONFIG_SND_FM801_TEA575X_BOOL
  21. #include <media/drv-intf/tea575x.h>
  22. #endif
  23. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  24. MODULE_DESCRIPTION("ForteMedia FM801");
  25. MODULE_LICENSE("GPL");
  26. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  27. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  28. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  29. /*
  30. * Enable TEA575x tuner
  31. * 1 = MediaForte 256-PCS
  32. * 2 = MediaForte 256-PCP
  33. * 3 = MediaForte 64-PCR
  34. * 16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
  35. * High 16-bits are video (radio) device number + 1
  36. */
  37. static int tea575x_tuner[SNDRV_CARDS];
  38. static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  39. module_param_array(index, int, NULL, 0444);
  40. MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
  41. module_param_array(id, charp, NULL, 0444);
  42. MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
  43. module_param_array(enable, bool, NULL, 0444);
  44. MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
  45. module_param_array(tea575x_tuner, int, NULL, 0444);
  46. 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).");
  47. module_param_array(radio_nr, int, NULL, 0444);
  48. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  49. #define TUNER_DISABLED (1<<3)
  50. #define TUNER_ONLY (1<<4)
  51. #define TUNER_TYPE_MASK (~TUNER_ONLY & 0xFFFF)
  52. /*
  53. * Direct registers
  54. */
  55. #define fm801_writew(chip,reg,value) outw((value), chip->port + FM801_##reg)
  56. #define fm801_readw(chip,reg) inw(chip->port + FM801_##reg)
  57. #define fm801_writel(chip,reg,value) outl((value), chip->port + FM801_##reg)
  58. #define FM801_PCM_VOL 0x00 /* PCM Output Volume */
  59. #define FM801_FM_VOL 0x02 /* FM Output Volume */
  60. #define FM801_I2S_VOL 0x04 /* I2S Volume */
  61. #define FM801_REC_SRC 0x06 /* Record Source */
  62. #define FM801_PLY_CTRL 0x08 /* Playback Control */
  63. #define FM801_PLY_COUNT 0x0a /* Playback Count */
  64. #define FM801_PLY_BUF1 0x0c /* Playback Bufer I */
  65. #define FM801_PLY_BUF2 0x10 /* Playback Buffer II */
  66. #define FM801_CAP_CTRL 0x14 /* Capture Control */
  67. #define FM801_CAP_COUNT 0x16 /* Capture Count */
  68. #define FM801_CAP_BUF1 0x18 /* Capture Buffer I */
  69. #define FM801_CAP_BUF2 0x1c /* Capture Buffer II */
  70. #define FM801_CODEC_CTRL 0x22 /* Codec Control */
  71. #define FM801_I2S_MODE 0x24 /* I2S Mode Control */
  72. #define FM801_VOLUME 0x26 /* Volume Up/Down/Mute Status */
  73. #define FM801_I2C_CTRL 0x29 /* I2C Control */
  74. #define FM801_AC97_CMD 0x2a /* AC'97 Command */
  75. #define FM801_AC97_DATA 0x2c /* AC'97 Data */
  76. #define FM801_MPU401_DATA 0x30 /* MPU401 Data */
  77. #define FM801_MPU401_CMD 0x31 /* MPU401 Command */
  78. #define FM801_GPIO_CTRL 0x52 /* General Purpose I/O Control */
  79. #define FM801_GEN_CTRL 0x54 /* General Control */
  80. #define FM801_IRQ_MASK 0x56 /* Interrupt Mask */
  81. #define FM801_IRQ_STATUS 0x5a /* Interrupt Status */
  82. #define FM801_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
  83. #define FM801_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
  84. #define FM801_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
  85. #define FM801_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
  86. #define FM801_POWERDOWN 0x70 /* Blocks Power Down Control */
  87. /* codec access */
  88. #define FM801_AC97_READ (1<<7) /* read=1, write=0 */
  89. #define FM801_AC97_VALID (1<<8) /* port valid=1 */
  90. #define FM801_AC97_BUSY (1<<9) /* busy=1 */
  91. #define FM801_AC97_ADDR_SHIFT 10 /* codec id (2bit) */
  92. /* playback and record control register bits */
  93. #define FM801_BUF1_LAST (1<<1)
  94. #define FM801_BUF2_LAST (1<<2)
  95. #define FM801_START (1<<5)
  96. #define FM801_PAUSE (1<<6)
  97. #define FM801_IMMED_STOP (1<<7)
  98. #define FM801_RATE_SHIFT 8
  99. #define FM801_RATE_MASK (15 << FM801_RATE_SHIFT)
  100. #define FM801_CHANNELS_4 (1<<12) /* playback only */
  101. #define FM801_CHANNELS_6 (2<<12) /* playback only */
  102. #define FM801_CHANNELS_6MS (3<<12) /* playback only */
  103. #define FM801_CHANNELS_MASK (3<<12)
  104. #define FM801_16BIT (1<<14)
  105. #define FM801_STEREO (1<<15)
  106. /* IRQ status bits */
  107. #define FM801_IRQ_PLAYBACK (1<<8)
  108. #define FM801_IRQ_CAPTURE (1<<9)
  109. #define FM801_IRQ_VOLUME (1<<14)
  110. #define FM801_IRQ_MPU (1<<15)
  111. /* GPIO control register */
  112. #define FM801_GPIO_GP0 (1<<0) /* read/write */
  113. #define FM801_GPIO_GP1 (1<<1)
  114. #define FM801_GPIO_GP2 (1<<2)
  115. #define FM801_GPIO_GP3 (1<<3)
  116. #define FM801_GPIO_GP(x) (1<<(0+(x)))
  117. #define FM801_GPIO_GD0 (1<<8) /* directions: 1 = input, 0 = output*/
  118. #define FM801_GPIO_GD1 (1<<9)
  119. #define FM801_GPIO_GD2 (1<<10)
  120. #define FM801_GPIO_GD3 (1<<11)
  121. #define FM801_GPIO_GD(x) (1<<(8+(x)))
  122. #define FM801_GPIO_GS0 (1<<12) /* function select: */
  123. #define FM801_GPIO_GS1 (1<<13) /* 1 = GPIO */
  124. #define FM801_GPIO_GS2 (1<<14) /* 0 = other (S/PDIF, VOL) */
  125. #define FM801_GPIO_GS3 (1<<15)
  126. #define FM801_GPIO_GS(x) (1<<(12+(x)))
  127. /**
  128. * struct fm801 - describes FM801 chip
  129. * @dev: device for this chio
  130. * @irq: irq number
  131. * @port: I/O port number
  132. * @multichannel: multichannel support
  133. * @secondary: secondary codec
  134. * @secondary_addr: address of the secondary codec
  135. * @tea575x_tuner: tuner access method & flags
  136. * @ply_ctrl: playback control
  137. * @cap_ctrl: capture control
  138. * @ply_buffer: playback buffer
  139. * @ply_buf: playback buffer index
  140. * @ply_count: playback buffer count
  141. * @ply_size: playback buffer size
  142. * @ply_pos: playback position
  143. * @cap_buffer: capture buffer
  144. * @cap_buf: capture buffer index
  145. * @cap_count: capture buffer count
  146. * @cap_size: capture buffer size
  147. * @cap_pos: capture position
  148. * @ac97_bus: ac97 bus handle
  149. * @ac97: ac97 handle
  150. * @ac97_sec: ac97 secondary handle
  151. * @card: ALSA card
  152. * @pcm: PCM devices
  153. * @rmidi: rmidi device
  154. * @playback_substream: substream for playback
  155. * @capture_substream: substream for capture
  156. * @p_dma_size: playback DMA size
  157. * @c_dma_size: capture DMA size
  158. * @reg_lock: lock
  159. * @proc_entry: /proc entry
  160. * @v4l2_dev: v4l2 device
  161. * @tea: tea575a structure
  162. * @saved_regs: context saved during suspend
  163. */
  164. struct fm801 {
  165. struct device *dev;
  166. int irq;
  167. unsigned long port;
  168. unsigned int multichannel: 1,
  169. secondary: 1;
  170. unsigned char secondary_addr;
  171. unsigned int tea575x_tuner;
  172. unsigned short ply_ctrl;
  173. unsigned short cap_ctrl;
  174. unsigned long ply_buffer;
  175. unsigned int ply_buf;
  176. unsigned int ply_count;
  177. unsigned int ply_size;
  178. unsigned int ply_pos;
  179. unsigned long cap_buffer;
  180. unsigned int cap_buf;
  181. unsigned int cap_count;
  182. unsigned int cap_size;
  183. unsigned int cap_pos;
  184. struct snd_ac97_bus *ac97_bus;
  185. struct snd_ac97 *ac97;
  186. struct snd_ac97 *ac97_sec;
  187. struct snd_card *card;
  188. struct snd_pcm *pcm;
  189. struct snd_rawmidi *rmidi;
  190. struct snd_pcm_substream *playback_substream;
  191. struct snd_pcm_substream *capture_substream;
  192. unsigned int p_dma_size;
  193. unsigned int c_dma_size;
  194. spinlock_t reg_lock;
  195. struct snd_info_entry *proc_entry;
  196. #ifdef CONFIG_SND_FM801_TEA575X_BOOL
  197. struct v4l2_device v4l2_dev;
  198. struct snd_tea575x tea;
  199. #endif
  200. u16 saved_regs[0x20];
  201. };
  202. /*
  203. * IO accessors
  204. */
  205. static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
  206. {
  207. outw(value, chip->port + offset);
  208. }
  209. static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
  210. {
  211. return inw(chip->port + offset);
  212. }
  213. static const struct pci_device_id snd_fm801_ids[] = {
  214. { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* FM801 */
  215. { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, }, /* Gallant Odyssey Sound 4 */
  216. { 0, }
  217. };
  218. MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
  219. /*
  220. * common I/O routines
  221. */
  222. static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
  223. {
  224. unsigned int idx;
  225. for (idx = 0; idx < iterations; idx++) {
  226. if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
  227. return true;
  228. udelay(10);
  229. }
  230. return false;
  231. }
  232. static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
  233. {
  234. unsigned int idx;
  235. for (idx = 0; idx < iterations; idx++) {
  236. if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
  237. return true;
  238. udelay(10);
  239. }
  240. return false;
  241. }
  242. static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
  243. unsigned short mask, unsigned short value)
  244. {
  245. int change;
  246. unsigned long flags;
  247. unsigned short old, new;
  248. spin_lock_irqsave(&chip->reg_lock, flags);
  249. old = fm801_ioread16(chip, reg);
  250. new = (old & ~mask) | value;
  251. change = old != new;
  252. if (change)
  253. fm801_iowrite16(chip, reg, new);
  254. spin_unlock_irqrestore(&chip->reg_lock, flags);
  255. return change;
  256. }
  257. static void snd_fm801_codec_write(struct snd_ac97 *ac97,
  258. unsigned short reg,
  259. unsigned short val)
  260. {
  261. struct fm801 *chip = ac97->private_data;
  262. /*
  263. * Wait until the codec interface is not ready..
  264. */
  265. if (!fm801_ac97_is_ready(chip, 100)) {
  266. dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
  267. return;
  268. }
  269. /* write data and address */
  270. fm801_writew(chip, AC97_DATA, val);
  271. fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
  272. /*
  273. * Wait until the write command is not completed..
  274. */
  275. if (!fm801_ac97_is_ready(chip, 1000))
  276. dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
  277. ac97->num);
  278. }
  279. static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  280. {
  281. struct fm801 *chip = ac97->private_data;
  282. /*
  283. * Wait until the codec interface is not ready..
  284. */
  285. if (!fm801_ac97_is_ready(chip, 100)) {
  286. dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
  287. return 0;
  288. }
  289. /* read command */
  290. fm801_writew(chip, AC97_CMD,
  291. reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
  292. if (!fm801_ac97_is_ready(chip, 100)) {
  293. dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
  294. ac97->num);
  295. return 0;
  296. }
  297. if (!fm801_ac97_is_valid(chip, 1000)) {
  298. dev_err(chip->card->dev,
  299. "AC'97 interface #%d is not valid (2)\n", ac97->num);
  300. return 0;
  301. }
  302. return fm801_readw(chip, AC97_DATA);
  303. }
  304. static const unsigned int rates[] = {
  305. 5500, 8000, 9600, 11025,
  306. 16000, 19200, 22050, 32000,
  307. 38400, 44100, 48000
  308. };
  309. static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  310. .count = ARRAY_SIZE(rates),
  311. .list = rates,
  312. .mask = 0,
  313. };
  314. static const unsigned int channels[] = {
  315. 2, 4, 6
  316. };
  317. static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
  318. .count = ARRAY_SIZE(channels),
  319. .list = channels,
  320. .mask = 0,
  321. };
  322. /*
  323. * Sample rate routines
  324. */
  325. static unsigned short snd_fm801_rate_bits(unsigned int rate)
  326. {
  327. unsigned int idx;
  328. for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
  329. if (rates[idx] == rate)
  330. return idx;
  331. snd_BUG();
  332. return ARRAY_SIZE(rates) - 1;
  333. }
  334. /*
  335. * PCM part
  336. */
  337. static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
  338. int cmd)
  339. {
  340. struct fm801 *chip = snd_pcm_substream_chip(substream);
  341. spin_lock(&chip->reg_lock);
  342. switch (cmd) {
  343. case SNDRV_PCM_TRIGGER_START:
  344. chip->ply_ctrl &= ~(FM801_BUF1_LAST |
  345. FM801_BUF2_LAST |
  346. FM801_PAUSE);
  347. chip->ply_ctrl |= FM801_START |
  348. FM801_IMMED_STOP;
  349. break;
  350. case SNDRV_PCM_TRIGGER_STOP:
  351. chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
  352. break;
  353. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  354. case SNDRV_PCM_TRIGGER_SUSPEND:
  355. chip->ply_ctrl |= FM801_PAUSE;
  356. break;
  357. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  358. case SNDRV_PCM_TRIGGER_RESUME:
  359. chip->ply_ctrl &= ~FM801_PAUSE;
  360. break;
  361. default:
  362. spin_unlock(&chip->reg_lock);
  363. snd_BUG();
  364. return -EINVAL;
  365. }
  366. fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
  367. spin_unlock(&chip->reg_lock);
  368. return 0;
  369. }
  370. static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
  371. int cmd)
  372. {
  373. struct fm801 *chip = snd_pcm_substream_chip(substream);
  374. spin_lock(&chip->reg_lock);
  375. switch (cmd) {
  376. case SNDRV_PCM_TRIGGER_START:
  377. chip->cap_ctrl &= ~(FM801_BUF1_LAST |
  378. FM801_BUF2_LAST |
  379. FM801_PAUSE);
  380. chip->cap_ctrl |= FM801_START |
  381. FM801_IMMED_STOP;
  382. break;
  383. case SNDRV_PCM_TRIGGER_STOP:
  384. chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
  385. break;
  386. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  387. case SNDRV_PCM_TRIGGER_SUSPEND:
  388. chip->cap_ctrl |= FM801_PAUSE;
  389. break;
  390. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  391. case SNDRV_PCM_TRIGGER_RESUME:
  392. chip->cap_ctrl &= ~FM801_PAUSE;
  393. break;
  394. default:
  395. spin_unlock(&chip->reg_lock);
  396. snd_BUG();
  397. return -EINVAL;
  398. }
  399. fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
  400. spin_unlock(&chip->reg_lock);
  401. return 0;
  402. }
  403. static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
  404. {
  405. struct fm801 *chip = snd_pcm_substream_chip(substream);
  406. struct snd_pcm_runtime *runtime = substream->runtime;
  407. chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
  408. chip->ply_count = snd_pcm_lib_period_bytes(substream);
  409. spin_lock_irq(&chip->reg_lock);
  410. chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
  411. FM801_STEREO | FM801_RATE_MASK |
  412. FM801_CHANNELS_MASK);
  413. if (snd_pcm_format_width(runtime->format) == 16)
  414. chip->ply_ctrl |= FM801_16BIT;
  415. if (runtime->channels > 1) {
  416. chip->ply_ctrl |= FM801_STEREO;
  417. if (runtime->channels == 4)
  418. chip->ply_ctrl |= FM801_CHANNELS_4;
  419. else if (runtime->channels == 6)
  420. chip->ply_ctrl |= FM801_CHANNELS_6;
  421. }
  422. chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
  423. chip->ply_buf = 0;
  424. fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
  425. fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
  426. chip->ply_buffer = runtime->dma_addr;
  427. chip->ply_pos = 0;
  428. fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
  429. fm801_writel(chip, PLY_BUF2,
  430. chip->ply_buffer + (chip->ply_count % chip->ply_size));
  431. spin_unlock_irq(&chip->reg_lock);
  432. return 0;
  433. }
  434. static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
  435. {
  436. struct fm801 *chip = snd_pcm_substream_chip(substream);
  437. struct snd_pcm_runtime *runtime = substream->runtime;
  438. chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
  439. chip->cap_count = snd_pcm_lib_period_bytes(substream);
  440. spin_lock_irq(&chip->reg_lock);
  441. chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
  442. FM801_STEREO | FM801_RATE_MASK);
  443. if (snd_pcm_format_width(runtime->format) == 16)
  444. chip->cap_ctrl |= FM801_16BIT;
  445. if (runtime->channels > 1)
  446. chip->cap_ctrl |= FM801_STEREO;
  447. chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
  448. chip->cap_buf = 0;
  449. fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
  450. fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
  451. chip->cap_buffer = runtime->dma_addr;
  452. chip->cap_pos = 0;
  453. fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
  454. fm801_writel(chip, CAP_BUF2,
  455. chip->cap_buffer + (chip->cap_count % chip->cap_size));
  456. spin_unlock_irq(&chip->reg_lock);
  457. return 0;
  458. }
  459. static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
  460. {
  461. struct fm801 *chip = snd_pcm_substream_chip(substream);
  462. size_t ptr;
  463. if (!(chip->ply_ctrl & FM801_START))
  464. return 0;
  465. spin_lock(&chip->reg_lock);
  466. ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
  467. if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
  468. ptr += chip->ply_count;
  469. ptr %= chip->ply_size;
  470. }
  471. spin_unlock(&chip->reg_lock);
  472. return bytes_to_frames(substream->runtime, ptr);
  473. }
  474. static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
  475. {
  476. struct fm801 *chip = snd_pcm_substream_chip(substream);
  477. size_t ptr;
  478. if (!(chip->cap_ctrl & FM801_START))
  479. return 0;
  480. spin_lock(&chip->reg_lock);
  481. ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
  482. if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
  483. ptr += chip->cap_count;
  484. ptr %= chip->cap_size;
  485. }
  486. spin_unlock(&chip->reg_lock);
  487. return bytes_to_frames(substream->runtime, ptr);
  488. }
  489. static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
  490. {
  491. struct fm801 *chip = dev_id;
  492. unsigned short status;
  493. unsigned int tmp;
  494. status = fm801_readw(chip, IRQ_STATUS);
  495. status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
  496. if (! status)
  497. return IRQ_NONE;
  498. /* ack first */
  499. fm801_writew(chip, IRQ_STATUS, status);
  500. if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
  501. spin_lock(&chip->reg_lock);
  502. chip->ply_buf++;
  503. chip->ply_pos += chip->ply_count;
  504. chip->ply_pos %= chip->ply_size;
  505. tmp = chip->ply_pos + chip->ply_count;
  506. tmp %= chip->ply_size;
  507. if (chip->ply_buf & 1)
  508. fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
  509. else
  510. fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
  511. spin_unlock(&chip->reg_lock);
  512. snd_pcm_period_elapsed(chip->playback_substream);
  513. }
  514. if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
  515. spin_lock(&chip->reg_lock);
  516. chip->cap_buf++;
  517. chip->cap_pos += chip->cap_count;
  518. chip->cap_pos %= chip->cap_size;
  519. tmp = chip->cap_pos + chip->cap_count;
  520. tmp %= chip->cap_size;
  521. if (chip->cap_buf & 1)
  522. fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
  523. else
  524. fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
  525. spin_unlock(&chip->reg_lock);
  526. snd_pcm_period_elapsed(chip->capture_substream);
  527. }
  528. if (chip->rmidi && (status & FM801_IRQ_MPU))
  529. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  530. if (status & FM801_IRQ_VOLUME) {
  531. /* TODO */
  532. }
  533. return IRQ_HANDLED;
  534. }
  535. static const struct snd_pcm_hardware snd_fm801_playback =
  536. {
  537. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  538. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  539. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
  540. SNDRV_PCM_INFO_MMAP_VALID),
  541. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  542. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  543. .rate_min = 5500,
  544. .rate_max = 48000,
  545. .channels_min = 1,
  546. .channels_max = 2,
  547. .buffer_bytes_max = (128*1024),
  548. .period_bytes_min = 64,
  549. .period_bytes_max = (128*1024),
  550. .periods_min = 1,
  551. .periods_max = 1024,
  552. .fifo_size = 0,
  553. };
  554. static const struct snd_pcm_hardware snd_fm801_capture =
  555. {
  556. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  557. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  558. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
  559. SNDRV_PCM_INFO_MMAP_VALID),
  560. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  561. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  562. .rate_min = 5500,
  563. .rate_max = 48000,
  564. .channels_min = 1,
  565. .channels_max = 2,
  566. .buffer_bytes_max = (128*1024),
  567. .period_bytes_min = 64,
  568. .period_bytes_max = (128*1024),
  569. .periods_min = 1,
  570. .periods_max = 1024,
  571. .fifo_size = 0,
  572. };
  573. static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
  574. {
  575. struct fm801 *chip = snd_pcm_substream_chip(substream);
  576. struct snd_pcm_runtime *runtime = substream->runtime;
  577. int err;
  578. chip->playback_substream = substream;
  579. runtime->hw = snd_fm801_playback;
  580. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  581. &hw_constraints_rates);
  582. if (chip->multichannel) {
  583. runtime->hw.channels_max = 6;
  584. snd_pcm_hw_constraint_list(runtime, 0,
  585. SNDRV_PCM_HW_PARAM_CHANNELS,
  586. &hw_constraints_channels);
  587. }
  588. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  589. if (err < 0)
  590. return err;
  591. return 0;
  592. }
  593. static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
  594. {
  595. struct fm801 *chip = snd_pcm_substream_chip(substream);
  596. struct snd_pcm_runtime *runtime = substream->runtime;
  597. int err;
  598. chip->capture_substream = substream;
  599. runtime->hw = snd_fm801_capture;
  600. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  601. &hw_constraints_rates);
  602. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  603. if (err < 0)
  604. return err;
  605. return 0;
  606. }
  607. static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
  608. {
  609. struct fm801 *chip = snd_pcm_substream_chip(substream);
  610. chip->playback_substream = NULL;
  611. return 0;
  612. }
  613. static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
  614. {
  615. struct fm801 *chip = snd_pcm_substream_chip(substream);
  616. chip->capture_substream = NULL;
  617. return 0;
  618. }
  619. static const struct snd_pcm_ops snd_fm801_playback_ops = {
  620. .open = snd_fm801_playback_open,
  621. .close = snd_fm801_playback_close,
  622. .prepare = snd_fm801_playback_prepare,
  623. .trigger = snd_fm801_playback_trigger,
  624. .pointer = snd_fm801_playback_pointer,
  625. };
  626. static const struct snd_pcm_ops snd_fm801_capture_ops = {
  627. .open = snd_fm801_capture_open,
  628. .close = snd_fm801_capture_close,
  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. err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm);
  639. if (err < 0)
  640. return err;
  641. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
  642. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
  643. pcm->private_data = chip;
  644. pcm->info_flags = 0;
  645. strcpy(pcm->name, "FM801");
  646. chip->pcm = pcm;
  647. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
  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 const 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. val = ucontrol->value.enumerated.item[0];
  868. if (val > 4)
  869. return -EINVAL;
  870. return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
  871. }
  872. static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
  873. #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
  874. static const struct snd_kcontrol_new snd_fm801_controls[] = {
  875. FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
  876. db_scale_dsp),
  877. FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
  878. FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
  879. db_scale_dsp),
  880. FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
  881. FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
  882. db_scale_dsp),
  883. FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
  884. {
  885. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  886. .name = "Digital Capture Source",
  887. .info = snd_fm801_info_mux,
  888. .get = snd_fm801_get_mux,
  889. .put = snd_fm801_put_mux,
  890. }
  891. };
  892. #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
  893. static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
  894. FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
  895. FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
  896. FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
  897. FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
  898. FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
  899. FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
  900. };
  901. static int snd_fm801_mixer(struct fm801 *chip)
  902. {
  903. struct snd_ac97_template ac97;
  904. unsigned int i;
  905. int err;
  906. static const struct snd_ac97_bus_ops ops = {
  907. .write = snd_fm801_codec_write,
  908. .read = snd_fm801_codec_read,
  909. };
  910. err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
  911. if (err < 0)
  912. return err;
  913. memset(&ac97, 0, sizeof(ac97));
  914. ac97.private_data = chip;
  915. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
  916. if (err < 0)
  917. return err;
  918. if (chip->secondary) {
  919. ac97.num = 1;
  920. ac97.addr = chip->secondary_addr;
  921. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
  922. if (err < 0)
  923. return err;
  924. }
  925. for (i = 0; i < FM801_CONTROLS; i++) {
  926. err = snd_ctl_add(chip->card,
  927. snd_ctl_new1(&snd_fm801_controls[i], chip));
  928. if (err < 0)
  929. return err;
  930. }
  931. if (chip->multichannel) {
  932. for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
  933. err = snd_ctl_add(chip->card,
  934. snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
  935. if (err < 0)
  936. return err;
  937. }
  938. }
  939. return 0;
  940. }
  941. /*
  942. * initialization routines
  943. */
  944. static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
  945. unsigned short reg, unsigned long waits)
  946. {
  947. unsigned long timeout = jiffies + waits;
  948. fm801_writew(chip, AC97_CMD,
  949. reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
  950. udelay(5);
  951. do {
  952. if ((fm801_readw(chip, AC97_CMD) &
  953. (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
  954. return 0;
  955. schedule_timeout_uninterruptible(1);
  956. } while (time_after(timeout, jiffies));
  957. return -EIO;
  958. }
  959. static int reset_codec(struct fm801 *chip)
  960. {
  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. return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
  967. }
  968. static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
  969. {
  970. unsigned short cmdw;
  971. if (chip->multichannel) {
  972. if (chip->secondary_addr) {
  973. wait_for_codec(chip, chip->secondary_addr,
  974. AC97_VENDOR_ID1, msecs_to_jiffies(50));
  975. } else {
  976. /* my card has the secondary codec */
  977. /* at address #3, so the loop is inverted */
  978. int i;
  979. for (i = 3; i > 0; i--) {
  980. if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
  981. msecs_to_jiffies(50))) {
  982. cmdw = fm801_readw(chip, AC97_DATA);
  983. if (cmdw != 0xffff && cmdw != 0) {
  984. chip->secondary = 1;
  985. chip->secondary_addr = i;
  986. break;
  987. }
  988. }
  989. }
  990. }
  991. /* the recovery phase, it seems that probing for non-existing codec might */
  992. /* cause timeout problems */
  993. wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
  994. }
  995. }
  996. static void snd_fm801_chip_init(struct fm801 *chip)
  997. {
  998. unsigned short cmdw;
  999. /* init volume */
  1000. fm801_writew(chip, PCM_VOL, 0x0808);
  1001. fm801_writew(chip, FM_VOL, 0x9f1f);
  1002. fm801_writew(chip, I2S_VOL, 0x8808);
  1003. /* I2S control - I2S mode */
  1004. fm801_writew(chip, I2S_MODE, 0x0003);
  1005. /* interrupt setup */
  1006. cmdw = fm801_readw(chip, IRQ_MASK);
  1007. if (chip->irq < 0)
  1008. cmdw |= 0x00c3; /* mask everything, no PCM nor MPU */
  1009. else
  1010. cmdw &= ~0x0083; /* unmask MPU, PLAYBACK & CAPTURE */
  1011. fm801_writew(chip, IRQ_MASK, cmdw);
  1012. /* interrupt clear */
  1013. fm801_writew(chip, IRQ_STATUS,
  1014. FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
  1015. }
  1016. static void snd_fm801_free(struct snd_card *card)
  1017. {
  1018. struct fm801 *chip = card->private_data;
  1019. unsigned short cmdw;
  1020. /* interrupt setup - mask everything */
  1021. cmdw = fm801_readw(chip, IRQ_MASK);
  1022. cmdw |= 0x00c3;
  1023. fm801_writew(chip, IRQ_MASK, cmdw);
  1024. #ifdef CONFIG_SND_FM801_TEA575X_BOOL
  1025. if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
  1026. snd_tea575x_exit(&chip->tea);
  1027. v4l2_device_unregister(&chip->v4l2_dev);
  1028. }
  1029. #endif
  1030. }
  1031. static int snd_fm801_create(struct snd_card *card,
  1032. struct pci_dev *pci,
  1033. int tea575x_tuner,
  1034. int radio_nr)
  1035. {
  1036. struct fm801 *chip = card->private_data;
  1037. int err;
  1038. err = pcim_enable_device(pci);
  1039. if (err < 0)
  1040. return err;
  1041. spin_lock_init(&chip->reg_lock);
  1042. chip->card = card;
  1043. chip->dev = &pci->dev;
  1044. chip->irq = -1;
  1045. chip->tea575x_tuner = tea575x_tuner;
  1046. err = pci_request_regions(pci, "FM801");
  1047. if (err < 0)
  1048. return err;
  1049. chip->port = pci_resource_start(pci, 0);
  1050. if (pci->revision >= 0xb1) /* FM801-AU */
  1051. chip->multichannel = 1;
  1052. if (!(chip->tea575x_tuner & TUNER_ONLY)) {
  1053. if (reset_codec(chip) < 0) {
  1054. dev_info(chip->card->dev,
  1055. "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
  1056. chip->tea575x_tuner = 3 | TUNER_ONLY;
  1057. } else {
  1058. snd_fm801_chip_multichannel_init(chip);
  1059. }
  1060. }
  1061. if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
  1062. if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
  1063. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  1064. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  1065. return -EBUSY;
  1066. }
  1067. chip->irq = pci->irq;
  1068. card->sync_irq = chip->irq;
  1069. pci_set_master(pci);
  1070. }
  1071. card->private_free = snd_fm801_free;
  1072. snd_fm801_chip_init(chip);
  1073. #ifdef CONFIG_SND_FM801_TEA575X_BOOL
  1074. err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
  1075. if (err < 0)
  1076. return err;
  1077. chip->tea.v4l2_dev = &chip->v4l2_dev;
  1078. chip->tea.radio_nr = radio_nr;
  1079. chip->tea.private_data = chip;
  1080. chip->tea.ops = &snd_fm801_tea_ops;
  1081. sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
  1082. if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
  1083. (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
  1084. if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
  1085. dev_err(card->dev, "TEA575x radio not found\n");
  1086. return -ENODEV;
  1087. }
  1088. } else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
  1089. unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
  1090. /* autodetect tuner connection */
  1091. for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
  1092. chip->tea575x_tuner = tea575x_tuner;
  1093. if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
  1094. dev_info(card->dev,
  1095. "detected TEA575x radio type %s\n",
  1096. get_tea575x_gpio(chip)->name);
  1097. break;
  1098. }
  1099. }
  1100. if (tea575x_tuner == 4) {
  1101. dev_err(card->dev, "TEA575x radio not found\n");
  1102. chip->tea575x_tuner = TUNER_DISABLED;
  1103. }
  1104. chip->tea575x_tuner |= tuner_only;
  1105. }
  1106. if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
  1107. strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
  1108. sizeof(chip->tea.card));
  1109. }
  1110. #endif
  1111. return 0;
  1112. }
  1113. static int __snd_card_fm801_probe(struct pci_dev *pci,
  1114. const struct pci_device_id *pci_id)
  1115. {
  1116. static int dev;
  1117. struct snd_card *card;
  1118. struct fm801 *chip;
  1119. struct snd_opl3 *opl3;
  1120. int err;
  1121. if (dev >= SNDRV_CARDS)
  1122. return -ENODEV;
  1123. if (!enable[dev]) {
  1124. dev++;
  1125. return -ENOENT;
  1126. }
  1127. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  1128. sizeof(*chip), &card);
  1129. if (err < 0)
  1130. return err;
  1131. chip = card->private_data;
  1132. err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev]);
  1133. if (err < 0)
  1134. return err;
  1135. strcpy(card->driver, "FM801");
  1136. strcpy(card->shortname, "ForteMedia FM801-");
  1137. strcat(card->shortname, chip->multichannel ? "AU" : "AS");
  1138. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1139. card->shortname, chip->port, chip->irq);
  1140. if (chip->tea575x_tuner & TUNER_ONLY)
  1141. goto __fm801_tuner_only;
  1142. err = snd_fm801_pcm(chip, 0);
  1143. if (err < 0)
  1144. return err;
  1145. err = snd_fm801_mixer(chip);
  1146. if (err < 0)
  1147. return err;
  1148. err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
  1149. chip->port + FM801_MPU401_DATA,
  1150. MPU401_INFO_INTEGRATED |
  1151. MPU401_INFO_IRQ_HOOK,
  1152. -1, &chip->rmidi);
  1153. if (err < 0)
  1154. return err;
  1155. err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
  1156. chip->port + FM801_OPL3_BANK1,
  1157. OPL3_HW_OPL3_FM801, 1, &opl3);
  1158. if (err < 0)
  1159. return err;
  1160. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  1161. if (err < 0)
  1162. return err;
  1163. __fm801_tuner_only:
  1164. err = snd_card_register(card);
  1165. if (err < 0)
  1166. return err;
  1167. pci_set_drvdata(pci, card);
  1168. dev++;
  1169. return 0;
  1170. }
  1171. static int snd_card_fm801_probe(struct pci_dev *pci,
  1172. const struct pci_device_id *pci_id)
  1173. {
  1174. return snd_card_free_on_error(&pci->dev, __snd_card_fm801_probe(pci, pci_id));
  1175. }
  1176. static const unsigned char saved_regs[] = {
  1177. FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
  1178. FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
  1179. FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
  1180. FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
  1181. };
  1182. static int snd_fm801_suspend(struct device *dev)
  1183. {
  1184. struct snd_card *card = dev_get_drvdata(dev);
  1185. struct fm801 *chip = card->private_data;
  1186. int i;
  1187. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1188. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  1189. chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
  1190. if (chip->tea575x_tuner & TUNER_ONLY) {
  1191. /* FIXME: tea575x suspend */
  1192. } else {
  1193. snd_ac97_suspend(chip->ac97);
  1194. snd_ac97_suspend(chip->ac97_sec);
  1195. }
  1196. return 0;
  1197. }
  1198. static int snd_fm801_resume(struct device *dev)
  1199. {
  1200. struct snd_card *card = dev_get_drvdata(dev);
  1201. struct fm801 *chip = card->private_data;
  1202. int i;
  1203. if (chip->tea575x_tuner & TUNER_ONLY) {
  1204. snd_fm801_chip_init(chip);
  1205. } else {
  1206. reset_codec(chip);
  1207. snd_fm801_chip_multichannel_init(chip);
  1208. snd_fm801_chip_init(chip);
  1209. snd_ac97_resume(chip->ac97);
  1210. snd_ac97_resume(chip->ac97_sec);
  1211. }
  1212. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  1213. fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
  1214. #ifdef CONFIG_SND_FM801_TEA575X_BOOL
  1215. if (!(chip->tea575x_tuner & TUNER_DISABLED))
  1216. snd_tea575x_set_freq(&chip->tea);
  1217. #endif
  1218. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1219. return 0;
  1220. }
  1221. static DEFINE_SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
  1222. static struct pci_driver fm801_driver = {
  1223. .name = KBUILD_MODNAME,
  1224. .id_table = snd_fm801_ids,
  1225. .probe = snd_card_fm801_probe,
  1226. .driver = {
  1227. .pm = &snd_fm801_pm,
  1228. },
  1229. };
  1230. module_pci_driver(fm801_driver);