fm801.c 41 KB

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