als300.c 20 KB

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