als300.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
  4. * Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
  5. *
  6. * TODO
  7. * 4 channel playback for ALS300+
  8. * gameport
  9. * mpu401
  10. * opl3
  11. *
  12. * NOTES
  13. * The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
  14. * the position in the current period, NOT the whole buffer. It is important
  15. * to know which period we are in so we can calculate the correct pointer.
  16. * This is why we always use 2 periods. We can then use a flip-flop variable
  17. * to keep track of what period we are in.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/io.h>
  27. #include <sound/core.h>
  28. #include <sound/control.h>
  29. #include <sound/initval.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/ac97_codec.h>
  33. #include <sound/opl3.h>
  34. /* snd_als300_set_irq_flag */
  35. #define IRQ_DISABLE 0
  36. #define IRQ_ENABLE 1
  37. /* I/O port layout */
  38. #define AC97_ACCESS 0x00
  39. #define AC97_READ 0x04
  40. #define AC97_STATUS 0x06
  41. #define AC97_DATA_AVAIL (1<<6)
  42. #define AC97_BUSY (1<<7)
  43. #define ALS300_IRQ_STATUS 0x07 /* ALS300 Only */
  44. #define IRQ_PLAYBACK (1<<3)
  45. #define IRQ_CAPTURE (1<<2)
  46. #define GCR_DATA 0x08
  47. #define GCR_INDEX 0x0C
  48. #define ALS300P_DRAM_IRQ_STATUS 0x0D /* ALS300+ Only */
  49. #define MPU_IRQ_STATUS 0x0E /* ALS300 Rev. E+, ALS300+ */
  50. #define ALS300P_IRQ_STATUS 0x0F /* ALS300+ Only */
  51. /* General Control Registers */
  52. #define PLAYBACK_START 0x80
  53. #define PLAYBACK_END 0x81
  54. #define PLAYBACK_CONTROL 0x82
  55. #define TRANSFER_START (1<<16)
  56. #define FIFO_PAUSE (1<<17)
  57. #define RECORD_START 0x83
  58. #define RECORD_END 0x84
  59. #define RECORD_CONTROL 0x85
  60. #define DRAM_WRITE_CONTROL 0x8B
  61. #define WRITE_TRANS_START (1<<16)
  62. #define DRAM_MODE_2 (1<<17)
  63. #define MISC_CONTROL 0x8C
  64. #define IRQ_SET_BIT (1<<15)
  65. #define VMUTE_NORMAL (1<<20)
  66. #define MMUTE_NORMAL (1<<21)
  67. #define MUS_VOC_VOL 0x8E
  68. #define PLAYBACK_BLOCK_COUNTER 0x9A
  69. #define RECORD_BLOCK_COUNTER 0x9B
  70. #define DEBUG_PLAY_REC 0
  71. #if DEBUG_PLAY_REC
  72. #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
  73. #else
  74. #define snd_als300_dbgplay(format, args...)
  75. #endif
  76. enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
  77. MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
  78. MODULE_DESCRIPTION("Avance Logic ALS300");
  79. MODULE_LICENSE("GPL");
  80. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
  81. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  82. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  83. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  84. module_param_array(index, int, NULL, 0444);
  85. MODULE_PARM_DESC(index, "Index value for ALS300 sound card.");
  86. module_param_array(id, charp, NULL, 0444);
  87. MODULE_PARM_DESC(id, "ID string for ALS300 sound card.");
  88. module_param_array(enable, bool, NULL, 0444);
  89. MODULE_PARM_DESC(enable, "Enable ALS300 sound card.");
  90. struct snd_als300 {
  91. unsigned long port;
  92. spinlock_t reg_lock;
  93. struct snd_card *card;
  94. struct pci_dev *pci;
  95. struct snd_pcm *pcm;
  96. struct snd_pcm_substream *playback_substream;
  97. struct snd_pcm_substream *capture_substream;
  98. struct snd_ac97 *ac97;
  99. struct snd_opl3 *opl3;
  100. struct resource *res_port;
  101. int irq;
  102. int chip_type; /* ALS300 or ALS300+ */
  103. char revision;
  104. };
  105. struct snd_als300_substream_data {
  106. int period_flipflop;
  107. int control_register;
  108. int block_counter_register;
  109. };
  110. static const struct pci_device_id snd_als300_ids[] = {
  111. { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
  112. { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
  113. { 0, }
  114. };
  115. MODULE_DEVICE_TABLE(pci, snd_als300_ids);
  116. static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
  117. {
  118. outb(reg, port+GCR_INDEX);
  119. return inl(port+GCR_DATA);
  120. }
  121. static inline void snd_als300_gcr_write(unsigned long port,
  122. unsigned short reg, u32 val)
  123. {
  124. outb(reg, port+GCR_INDEX);
  125. outl(val, port+GCR_DATA);
  126. }
  127. /* Enable/Disable Interrupts */
  128. static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
  129. {
  130. u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  131. /* boolean XOR check, since old vs. new hardware have
  132. directly reversed bit setting for ENABLE and DISABLE.
  133. ALS300+ acts like newer versions of ALS300 */
  134. if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
  135. (cmd == IRQ_ENABLE)) == 0)
  136. tmp |= IRQ_SET_BIT;
  137. else
  138. tmp &= ~IRQ_SET_BIT;
  139. snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
  140. }
  141. static int snd_als300_free(struct snd_als300 *chip)
  142. {
  143. snd_als300_set_irq_flag(chip, IRQ_DISABLE);
  144. if (chip->irq >= 0)
  145. free_irq(chip->irq, chip);
  146. pci_release_regions(chip->pci);
  147. pci_disable_device(chip->pci);
  148. kfree(chip);
  149. return 0;
  150. }
  151. static int snd_als300_dev_free(struct snd_device *device)
  152. {
  153. struct snd_als300 *chip = device->device_data;
  154. return snd_als300_free(chip);
  155. }
  156. static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
  157. {
  158. u8 status;
  159. struct snd_als300 *chip = dev_id;
  160. struct snd_als300_substream_data *data;
  161. status = inb(chip->port+ALS300_IRQ_STATUS);
  162. if (!status) /* shared IRQ, for different device?? Exit ASAP! */
  163. return IRQ_NONE;
  164. /* ACK everything ASAP */
  165. outb(status, chip->port+ALS300_IRQ_STATUS);
  166. if (status & IRQ_PLAYBACK) {
  167. if (chip->pcm && chip->playback_substream) {
  168. data = chip->playback_substream->runtime->private_data;
  169. data->period_flipflop ^= 1;
  170. snd_pcm_period_elapsed(chip->playback_substream);
  171. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  172. }
  173. }
  174. if (status & IRQ_CAPTURE) {
  175. if (chip->pcm && chip->capture_substream) {
  176. data = chip->capture_substream->runtime->private_data;
  177. data->period_flipflop ^= 1;
  178. snd_pcm_period_elapsed(chip->capture_substream);
  179. snd_als300_dbgplay("IRQ_CAPTURE\n");
  180. }
  181. }
  182. return IRQ_HANDLED;
  183. }
  184. static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
  185. {
  186. u8 general, mpu, dram;
  187. struct snd_als300 *chip = dev_id;
  188. struct snd_als300_substream_data *data;
  189. general = inb(chip->port+ALS300P_IRQ_STATUS);
  190. mpu = inb(chip->port+MPU_IRQ_STATUS);
  191. dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
  192. /* shared IRQ, for different device?? Exit ASAP! */
  193. if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
  194. return IRQ_NONE;
  195. if (general & IRQ_PLAYBACK) {
  196. if (chip->pcm && chip->playback_substream) {
  197. outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
  198. data = chip->playback_substream->runtime->private_data;
  199. data->period_flipflop ^= 1;
  200. snd_pcm_period_elapsed(chip->playback_substream);
  201. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  202. }
  203. }
  204. if (general & IRQ_CAPTURE) {
  205. if (chip->pcm && chip->capture_substream) {
  206. outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
  207. data = chip->capture_substream->runtime->private_data;
  208. data->period_flipflop ^= 1;
  209. snd_pcm_period_elapsed(chip->capture_substream);
  210. snd_als300_dbgplay("IRQ_CAPTURE\n");
  211. }
  212. }
  213. /* FIXME: Ack other interrupt types. Not important right now as
  214. * those other devices aren't enabled. */
  215. return IRQ_HANDLED;
  216. }
  217. static void snd_als300_remove(struct pci_dev *pci)
  218. {
  219. snd_card_free(pci_get_drvdata(pci));
  220. }
  221. static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
  222. unsigned short reg)
  223. {
  224. int i;
  225. struct snd_als300 *chip = ac97->private_data;
  226. for (i = 0; i < 1000; i++) {
  227. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  228. break;
  229. udelay(10);
  230. }
  231. outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
  232. for (i = 0; i < 1000; i++) {
  233. if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
  234. break;
  235. udelay(10);
  236. }
  237. return inw(chip->port+AC97_READ);
  238. }
  239. static void snd_als300_ac97_write(struct snd_ac97 *ac97,
  240. unsigned short reg, unsigned short val)
  241. {
  242. int i;
  243. struct snd_als300 *chip = ac97->private_data;
  244. for (i = 0; i < 1000; i++) {
  245. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  246. break;
  247. udelay(10);
  248. }
  249. outl((reg << 24) | val, chip->port+AC97_ACCESS);
  250. }
  251. static int snd_als300_ac97(struct snd_als300 *chip)
  252. {
  253. struct snd_ac97_bus *bus;
  254. struct snd_ac97_template ac97;
  255. int err;
  256. static const struct snd_ac97_bus_ops ops = {
  257. .write = snd_als300_ac97_write,
  258. .read = snd_als300_ac97_read,
  259. };
  260. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
  261. return err;
  262. memset(&ac97, 0, sizeof(ac97));
  263. ac97.private_data = chip;
  264. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  265. }
  266. /* hardware definition
  267. *
  268. * In AC97 mode, we always use 48k/16bit/stereo.
  269. * Any request to change data type is ignored by
  270. * the card when it is running outside of legacy
  271. * mode.
  272. */
  273. static const struct snd_pcm_hardware snd_als300_playback_hw =
  274. {
  275. .info = (SNDRV_PCM_INFO_MMAP |
  276. SNDRV_PCM_INFO_INTERLEAVED |
  277. SNDRV_PCM_INFO_PAUSE |
  278. SNDRV_PCM_INFO_MMAP_VALID),
  279. .formats = SNDRV_PCM_FMTBIT_S16,
  280. .rates = SNDRV_PCM_RATE_48000,
  281. .rate_min = 48000,
  282. .rate_max = 48000,
  283. .channels_min = 2,
  284. .channels_max = 2,
  285. .buffer_bytes_max = 64 * 1024,
  286. .period_bytes_min = 64,
  287. .period_bytes_max = 32 * 1024,
  288. .periods_min = 2,
  289. .periods_max = 2,
  290. };
  291. static const struct snd_pcm_hardware snd_als300_capture_hw =
  292. {
  293. .info = (SNDRV_PCM_INFO_MMAP |
  294. SNDRV_PCM_INFO_INTERLEAVED |
  295. SNDRV_PCM_INFO_PAUSE |
  296. SNDRV_PCM_INFO_MMAP_VALID),
  297. .formats = SNDRV_PCM_FMTBIT_S16,
  298. .rates = SNDRV_PCM_RATE_48000,
  299. .rate_min = 48000,
  300. .rate_max = 48000,
  301. .channels_min = 2,
  302. .channels_max = 2,
  303. .buffer_bytes_max = 64 * 1024,
  304. .period_bytes_min = 64,
  305. .period_bytes_max = 32 * 1024,
  306. .periods_min = 2,
  307. .periods_max = 2,
  308. };
  309. static int snd_als300_playback_open(struct snd_pcm_substream *substream)
  310. {
  311. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  312. struct snd_pcm_runtime *runtime = substream->runtime;
  313. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  314. GFP_KERNEL);
  315. if (!data)
  316. return -ENOMEM;
  317. chip->playback_substream = substream;
  318. runtime->hw = snd_als300_playback_hw;
  319. runtime->private_data = data;
  320. data->control_register = PLAYBACK_CONTROL;
  321. data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
  322. return 0;
  323. }
  324. static int snd_als300_playback_close(struct snd_pcm_substream *substream)
  325. {
  326. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  327. struct snd_als300_substream_data *data;
  328. data = substream->runtime->private_data;
  329. kfree(data);
  330. chip->playback_substream = NULL;
  331. return 0;
  332. }
  333. static int snd_als300_capture_open(struct snd_pcm_substream *substream)
  334. {
  335. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  336. struct snd_pcm_runtime *runtime = substream->runtime;
  337. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  338. GFP_KERNEL);
  339. if (!data)
  340. return -ENOMEM;
  341. chip->capture_substream = substream;
  342. runtime->hw = snd_als300_capture_hw;
  343. runtime->private_data = data;
  344. data->control_register = RECORD_CONTROL;
  345. data->block_counter_register = RECORD_BLOCK_COUNTER;
  346. return 0;
  347. }
  348. static int snd_als300_capture_close(struct snd_pcm_substream *substream)
  349. {
  350. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  351. struct snd_als300_substream_data *data;
  352. data = substream->runtime->private_data;
  353. kfree(data);
  354. chip->capture_substream = NULL;
  355. return 0;
  356. }
  357. static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
  358. {
  359. u32 tmp;
  360. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  361. struct snd_pcm_runtime *runtime = substream->runtime;
  362. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  363. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  364. spin_lock_irq(&chip->reg_lock);
  365. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  366. tmp &= ~TRANSFER_START;
  367. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
  368. period_bytes, buffer_bytes);
  369. /* set block size */
  370. tmp &= 0xffff0000;
  371. tmp |= period_bytes - 1;
  372. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
  373. /* set dma area */
  374. snd_als300_gcr_write(chip->port, PLAYBACK_START,
  375. runtime->dma_addr);
  376. snd_als300_gcr_write(chip->port, PLAYBACK_END,
  377. runtime->dma_addr + buffer_bytes - 1);
  378. spin_unlock_irq(&chip->reg_lock);
  379. return 0;
  380. }
  381. static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
  382. {
  383. u32 tmp;
  384. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  385. struct snd_pcm_runtime *runtime = substream->runtime;
  386. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  387. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  388. spin_lock_irq(&chip->reg_lock);
  389. tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
  390. tmp &= ~TRANSFER_START;
  391. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
  392. buffer_bytes);
  393. /* set block size */
  394. tmp &= 0xffff0000;
  395. tmp |= period_bytes - 1;
  396. /* set dma area */
  397. snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
  398. snd_als300_gcr_write(chip->port, RECORD_START,
  399. runtime->dma_addr);
  400. snd_als300_gcr_write(chip->port, RECORD_END,
  401. runtime->dma_addr + buffer_bytes - 1);
  402. spin_unlock_irq(&chip->reg_lock);
  403. return 0;
  404. }
  405. static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
  406. {
  407. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  408. u32 tmp;
  409. struct snd_als300_substream_data *data;
  410. unsigned short reg;
  411. int ret = 0;
  412. data = substream->runtime->private_data;
  413. reg = data->control_register;
  414. spin_lock(&chip->reg_lock);
  415. switch (cmd) {
  416. case SNDRV_PCM_TRIGGER_START:
  417. case SNDRV_PCM_TRIGGER_RESUME:
  418. tmp = snd_als300_gcr_read(chip->port, reg);
  419. data->period_flipflop = 1;
  420. snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
  421. snd_als300_dbgplay("TRIGGER START\n");
  422. break;
  423. case SNDRV_PCM_TRIGGER_STOP:
  424. case SNDRV_PCM_TRIGGER_SUSPEND:
  425. tmp = snd_als300_gcr_read(chip->port, reg);
  426. snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
  427. snd_als300_dbgplay("TRIGGER STOP\n");
  428. break;
  429. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  430. tmp = snd_als300_gcr_read(chip->port, reg);
  431. snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
  432. snd_als300_dbgplay("TRIGGER PAUSE\n");
  433. break;
  434. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  435. tmp = snd_als300_gcr_read(chip->port, reg);
  436. snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
  437. snd_als300_dbgplay("TRIGGER RELEASE\n");
  438. break;
  439. default:
  440. snd_als300_dbgplay("TRIGGER INVALID\n");
  441. ret = -EINVAL;
  442. }
  443. spin_unlock(&chip->reg_lock);
  444. return ret;
  445. }
  446. static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
  447. {
  448. u16 current_ptr;
  449. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  450. struct snd_als300_substream_data *data;
  451. unsigned short period_bytes;
  452. data = substream->runtime->private_data;
  453. period_bytes = snd_pcm_lib_period_bytes(substream);
  454. spin_lock(&chip->reg_lock);
  455. current_ptr = (u16) snd_als300_gcr_read(chip->port,
  456. data->block_counter_register) + 4;
  457. spin_unlock(&chip->reg_lock);
  458. if (current_ptr > period_bytes)
  459. current_ptr = 0;
  460. else
  461. current_ptr = period_bytes - current_ptr;
  462. if (data->period_flipflop == 0)
  463. current_ptr += period_bytes;
  464. snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
  465. return bytes_to_frames(substream->runtime, current_ptr);
  466. }
  467. static const struct snd_pcm_ops snd_als300_playback_ops = {
  468. .open = snd_als300_playback_open,
  469. .close = snd_als300_playback_close,
  470. .prepare = snd_als300_playback_prepare,
  471. .trigger = snd_als300_trigger,
  472. .pointer = snd_als300_pointer,
  473. };
  474. static const struct snd_pcm_ops snd_als300_capture_ops = {
  475. .open = snd_als300_capture_open,
  476. .close = snd_als300_capture_close,
  477. .prepare = snd_als300_capture_prepare,
  478. .trigger = snd_als300_trigger,
  479. .pointer = snd_als300_pointer,
  480. };
  481. static int snd_als300_new_pcm(struct snd_als300 *chip)
  482. {
  483. struct snd_pcm *pcm;
  484. int err;
  485. err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
  486. if (err < 0)
  487. return err;
  488. pcm->private_data = chip;
  489. strcpy(pcm->name, "ALS300");
  490. chip->pcm = pcm;
  491. /* set operators */
  492. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  493. &snd_als300_playback_ops);
  494. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  495. &snd_als300_capture_ops);
  496. /* pre-allocation of buffers */
  497. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
  498. 64*1024, 64*1024);
  499. return 0;
  500. }
  501. static void snd_als300_init(struct snd_als300 *chip)
  502. {
  503. unsigned long flags;
  504. u32 tmp;
  505. spin_lock_irqsave(&chip->reg_lock, flags);
  506. chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
  507. & 0x0000000F;
  508. /* Setup DRAM */
  509. tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
  510. snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
  511. (tmp | DRAM_MODE_2)
  512. & ~WRITE_TRANS_START);
  513. /* Enable IRQ output */
  514. snd_als300_set_irq_flag(chip, IRQ_ENABLE);
  515. /* Unmute hardware devices so their outputs get routed to
  516. * the onboard mixer */
  517. tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  518. snd_als300_gcr_write(chip->port, MISC_CONTROL,
  519. tmp | VMUTE_NORMAL | MMUTE_NORMAL);
  520. /* Reset volumes */
  521. snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
  522. /* Make sure playback transfer is stopped */
  523. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  524. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
  525. tmp & ~TRANSFER_START);
  526. spin_unlock_irqrestore(&chip->reg_lock, flags);
  527. }
  528. static int snd_als300_create(struct snd_card *card,
  529. struct pci_dev *pci, int chip_type,
  530. struct snd_als300 **rchip)
  531. {
  532. struct snd_als300 *chip;
  533. void *irq_handler;
  534. int err;
  535. static const struct snd_device_ops ops = {
  536. .dev_free = snd_als300_dev_free,
  537. };
  538. *rchip = NULL;
  539. if ((err = pci_enable_device(pci)) < 0)
  540. return err;
  541. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(28)) < 0 ||
  542. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(28)) < 0) {
  543. dev_err(card->dev, "error setting 28bit DMA mask\n");
  544. pci_disable_device(pci);
  545. return -ENXIO;
  546. }
  547. pci_set_master(pci);
  548. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  549. if (chip == NULL) {
  550. pci_disable_device(pci);
  551. return -ENOMEM;
  552. }
  553. chip->card = card;
  554. chip->pci = pci;
  555. chip->irq = -1;
  556. chip->chip_type = chip_type;
  557. spin_lock_init(&chip->reg_lock);
  558. if ((err = pci_request_regions(pci, "ALS300")) < 0) {
  559. kfree(chip);
  560. pci_disable_device(pci);
  561. return err;
  562. }
  563. chip->port = pci_resource_start(pci, 0);
  564. if (chip->chip_type == DEVICE_ALS300_PLUS)
  565. irq_handler = snd_als300plus_interrupt;
  566. else
  567. irq_handler = snd_als300_interrupt;
  568. if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
  569. KBUILD_MODNAME, chip)) {
  570. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  571. snd_als300_free(chip);
  572. return -EBUSY;
  573. }
  574. chip->irq = pci->irq;
  575. card->sync_irq = chip->irq;
  576. snd_als300_init(chip);
  577. err = snd_als300_ac97(chip);
  578. if (err < 0) {
  579. dev_err(card->dev, "Could not create ac97\n");
  580. snd_als300_free(chip);
  581. return err;
  582. }
  583. if ((err = snd_als300_new_pcm(chip)) < 0) {
  584. dev_err(card->dev, "Could not create PCM\n");
  585. snd_als300_free(chip);
  586. return err;
  587. }
  588. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  589. chip, &ops)) < 0) {
  590. snd_als300_free(chip);
  591. return err;
  592. }
  593. *rchip = chip;
  594. return 0;
  595. }
  596. #ifdef CONFIG_PM_SLEEP
  597. static int snd_als300_suspend(struct device *dev)
  598. {
  599. struct snd_card *card = dev_get_drvdata(dev);
  600. struct snd_als300 *chip = card->private_data;
  601. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  602. snd_ac97_suspend(chip->ac97);
  603. return 0;
  604. }
  605. static int snd_als300_resume(struct device *dev)
  606. {
  607. struct snd_card *card = dev_get_drvdata(dev);
  608. struct snd_als300 *chip = card->private_data;
  609. snd_als300_init(chip);
  610. snd_ac97_resume(chip->ac97);
  611. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  612. return 0;
  613. }
  614. static SIMPLE_DEV_PM_OPS(snd_als300_pm, snd_als300_suspend, snd_als300_resume);
  615. #define SND_ALS300_PM_OPS &snd_als300_pm
  616. #else
  617. #define SND_ALS300_PM_OPS NULL
  618. #endif
  619. static int snd_als300_probe(struct pci_dev *pci,
  620. const struct pci_device_id *pci_id)
  621. {
  622. static int dev;
  623. struct snd_card *card;
  624. struct snd_als300 *chip;
  625. int err, chip_type;
  626. if (dev >= SNDRV_CARDS)
  627. return -ENODEV;
  628. if (!enable[dev]) {
  629. dev++;
  630. return -ENOENT;
  631. }
  632. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  633. 0, &card);
  634. if (err < 0)
  635. return err;
  636. chip_type = pci_id->driver_data;
  637. if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
  638. snd_card_free(card);
  639. return err;
  640. }
  641. card->private_data = chip;
  642. strcpy(card->driver, "ALS300");
  643. if (chip->chip_type == DEVICE_ALS300_PLUS)
  644. /* don't know much about ALS300+ yet
  645. * print revision number for now */
  646. sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
  647. else
  648. sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
  649. chip->revision - 1);
  650. sprintf(card->longname, "%s at 0x%lx irq %i",
  651. card->shortname, chip->port, chip->irq);
  652. if ((err = snd_card_register(card)) < 0) {
  653. snd_card_free(card);
  654. return err;
  655. }
  656. pci_set_drvdata(pci, card);
  657. dev++;
  658. return 0;
  659. }
  660. static struct pci_driver als300_driver = {
  661. .name = KBUILD_MODNAME,
  662. .id_table = snd_als300_ids,
  663. .probe = snd_als300_probe,
  664. .remove = snd_als300_remove,
  665. .driver = {
  666. .pm = SND_ALS300_PM_OPS,
  667. },
  668. };
  669. module_pci_driver(als300_driver);