als300.c 23 KB

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