atiixp_modem.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA driver for ATI IXP 150/200/250 AC97 modem controllers
  4. *
  5. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/init.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/info.h>
  19. #include <sound/ac97_codec.h>
  20. #include <sound/initval.h>
  21. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  22. MODULE_DESCRIPTION("ATI IXP MC97 controller");
  23. MODULE_LICENSE("GPL");
  24. MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250}}");
  25. static int index = -2; /* Exclude the first card */
  26. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  27. static int ac97_clock = 48000;
  28. module_param(index, int, 0444);
  29. MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
  30. module_param(id, charp, 0444);
  31. MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
  32. module_param(ac97_clock, int, 0444);
  33. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  34. /* just for backward compatibility */
  35. static bool enable;
  36. module_param(enable, bool, 0444);
  37. /*
  38. */
  39. #define ATI_REG_ISR 0x00 /* interrupt source */
  40. #define ATI_REG_ISR_MODEM_IN_XRUN (1U<<0)
  41. #define ATI_REG_ISR_MODEM_IN_STATUS (1U<<1)
  42. #define ATI_REG_ISR_MODEM_OUT1_XRUN (1U<<2)
  43. #define ATI_REG_ISR_MODEM_OUT1_STATUS (1U<<3)
  44. #define ATI_REG_ISR_MODEM_OUT2_XRUN (1U<<4)
  45. #define ATI_REG_ISR_MODEM_OUT2_STATUS (1U<<5)
  46. #define ATI_REG_ISR_MODEM_OUT3_XRUN (1U<<6)
  47. #define ATI_REG_ISR_MODEM_OUT3_STATUS (1U<<7)
  48. #define ATI_REG_ISR_PHYS_INTR (1U<<8)
  49. #define ATI_REG_ISR_PHYS_MISMATCH (1U<<9)
  50. #define ATI_REG_ISR_CODEC0_NOT_READY (1U<<10)
  51. #define ATI_REG_ISR_CODEC1_NOT_READY (1U<<11)
  52. #define ATI_REG_ISR_CODEC2_NOT_READY (1U<<12)
  53. #define ATI_REG_ISR_NEW_FRAME (1U<<13)
  54. #define ATI_REG_ISR_MODEM_GPIO_DATA (1U<<14)
  55. #define ATI_REG_IER 0x04 /* interrupt enable */
  56. #define ATI_REG_IER_MODEM_IN_XRUN_EN (1U<<0)
  57. #define ATI_REG_IER_MODEM_STATUS_EN (1U<<1)
  58. #define ATI_REG_IER_MODEM_OUT1_XRUN_EN (1U<<2)
  59. #define ATI_REG_IER_MODEM_OUT2_XRUN_EN (1U<<4)
  60. #define ATI_REG_IER_MODEM_OUT3_XRUN_EN (1U<<6)
  61. #define ATI_REG_IER_PHYS_INTR_EN (1U<<8)
  62. #define ATI_REG_IER_PHYS_MISMATCH_EN (1U<<9)
  63. #define ATI_REG_IER_CODEC0_INTR_EN (1U<<10)
  64. #define ATI_REG_IER_CODEC1_INTR_EN (1U<<11)
  65. #define ATI_REG_IER_CODEC2_INTR_EN (1U<<12)
  66. #define ATI_REG_IER_NEW_FRAME_EN (1U<<13) /* (RO */
  67. #define ATI_REG_IER_MODEM_GPIO_DATA_EN (1U<<14) /* (WO) modem is running */
  68. #define ATI_REG_IER_MODEM_SET_BUS_BUSY (1U<<15)
  69. #define ATI_REG_CMD 0x08 /* command */
  70. #define ATI_REG_CMD_POWERDOWN (1U<<0)
  71. #define ATI_REG_CMD_MODEM_RECEIVE_EN (1U<<1) /* modem only */
  72. #define ATI_REG_CMD_MODEM_SEND1_EN (1U<<2) /* modem only */
  73. #define ATI_REG_CMD_MODEM_SEND2_EN (1U<<3) /* modem only */
  74. #define ATI_REG_CMD_MODEM_SEND3_EN (1U<<4) /* modem only */
  75. #define ATI_REG_CMD_MODEM_STATUS_MEM (1U<<5) /* modem only */
  76. #define ATI_REG_CMD_MODEM_IN_DMA_EN (1U<<8) /* modem only */
  77. #define ATI_REG_CMD_MODEM_OUT_DMA1_EN (1U<<9) /* modem only */
  78. #define ATI_REG_CMD_MODEM_OUT_DMA2_EN (1U<<10) /* modem only */
  79. #define ATI_REG_CMD_MODEM_OUT_DMA3_EN (1U<<11) /* modem only */
  80. #define ATI_REG_CMD_AUDIO_PRESENT (1U<<20)
  81. #define ATI_REG_CMD_MODEM_GPIO_THRU_DMA (1U<<22) /* modem only */
  82. #define ATI_REG_CMD_LOOPBACK_EN (1U<<23)
  83. #define ATI_REG_CMD_PACKED_DIS (1U<<24)
  84. #define ATI_REG_CMD_BURST_EN (1U<<25)
  85. #define ATI_REG_CMD_PANIC_EN (1U<<26)
  86. #define ATI_REG_CMD_MODEM_PRESENT (1U<<27)
  87. #define ATI_REG_CMD_ACLINK_ACTIVE (1U<<28)
  88. #define ATI_REG_CMD_AC_SOFT_RESET (1U<<29)
  89. #define ATI_REG_CMD_AC_SYNC (1U<<30)
  90. #define ATI_REG_CMD_AC_RESET (1U<<31)
  91. #define ATI_REG_PHYS_OUT_ADDR 0x0c
  92. #define ATI_REG_PHYS_OUT_CODEC_MASK (3U<<0)
  93. #define ATI_REG_PHYS_OUT_RW (1U<<2)
  94. #define ATI_REG_PHYS_OUT_ADDR_EN (1U<<8)
  95. #define ATI_REG_PHYS_OUT_ADDR_SHIFT 9
  96. #define ATI_REG_PHYS_OUT_DATA_SHIFT 16
  97. #define ATI_REG_PHYS_IN_ADDR 0x10
  98. #define ATI_REG_PHYS_IN_READ_FLAG (1U<<8)
  99. #define ATI_REG_PHYS_IN_ADDR_SHIFT 9
  100. #define ATI_REG_PHYS_IN_DATA_SHIFT 16
  101. #define ATI_REG_SLOTREQ 0x14
  102. #define ATI_REG_COUNTER 0x18
  103. #define ATI_REG_COUNTER_SLOT (3U<<0) /* slot # */
  104. #define ATI_REG_COUNTER_BITCLOCK (31U<<8)
  105. #define ATI_REG_IN_FIFO_THRESHOLD 0x1c
  106. #define ATI_REG_MODEM_IN_DMA_LINKPTR 0x20
  107. #define ATI_REG_MODEM_IN_DMA_DT_START 0x24 /* RO */
  108. #define ATI_REG_MODEM_IN_DMA_DT_NEXT 0x28 /* RO */
  109. #define ATI_REG_MODEM_IN_DMA_DT_CUR 0x2c /* RO */
  110. #define ATI_REG_MODEM_IN_DMA_DT_SIZE 0x30
  111. #define ATI_REG_MODEM_OUT_FIFO 0x34 /* output threshold */
  112. #define ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK (0xf<<16)
  113. #define ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT 16
  114. #define ATI_REG_MODEM_OUT_DMA1_LINKPTR 0x38
  115. #define ATI_REG_MODEM_OUT_DMA2_LINKPTR 0x3c
  116. #define ATI_REG_MODEM_OUT_DMA3_LINKPTR 0x40
  117. #define ATI_REG_MODEM_OUT_DMA1_DT_START 0x44
  118. #define ATI_REG_MODEM_OUT_DMA1_DT_NEXT 0x48
  119. #define ATI_REG_MODEM_OUT_DMA1_DT_CUR 0x4c
  120. #define ATI_REG_MODEM_OUT_DMA2_DT_START 0x50
  121. #define ATI_REG_MODEM_OUT_DMA2_DT_NEXT 0x54
  122. #define ATI_REG_MODEM_OUT_DMA2_DT_CUR 0x58
  123. #define ATI_REG_MODEM_OUT_DMA3_DT_START 0x5c
  124. #define ATI_REG_MODEM_OUT_DMA3_DT_NEXT 0x60
  125. #define ATI_REG_MODEM_OUT_DMA3_DT_CUR 0x64
  126. #define ATI_REG_MODEM_OUT_DMA12_DT_SIZE 0x68
  127. #define ATI_REG_MODEM_OUT_DMA3_DT_SIZE 0x6c
  128. #define ATI_REG_MODEM_OUT_FIFO_USED 0x70
  129. #define ATI_REG_MODEM_OUT_GPIO 0x74
  130. #define ATI_REG_MODEM_OUT_GPIO_EN 1
  131. #define ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT 5
  132. #define ATI_REG_MODEM_IN_GPIO 0x78
  133. #define ATI_REG_MODEM_MIRROR 0x7c
  134. #define ATI_REG_AUDIO_MIRROR 0x80
  135. #define ATI_REG_MODEM_FIFO_FLUSH 0x88
  136. #define ATI_REG_MODEM_FIFO_OUT1_FLUSH (1U<<0)
  137. #define ATI_REG_MODEM_FIFO_OUT2_FLUSH (1U<<1)
  138. #define ATI_REG_MODEM_FIFO_OUT3_FLUSH (1U<<2)
  139. #define ATI_REG_MODEM_FIFO_IN_FLUSH (1U<<3)
  140. /* LINKPTR */
  141. #define ATI_REG_LINKPTR_EN (1U<<0)
  142. #define ATI_MAX_DESCRIPTORS 256 /* max number of descriptor packets */
  143. struct atiixp_modem;
  144. /*
  145. * DMA packate descriptor
  146. */
  147. struct atiixp_dma_desc {
  148. __le32 addr; /* DMA buffer address */
  149. u16 status; /* status bits */
  150. u16 size; /* size of the packet in dwords */
  151. __le32 next; /* address of the next packet descriptor */
  152. };
  153. /*
  154. * stream enum
  155. */
  156. enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, NUM_ATI_DMAS }; /* DMAs */
  157. enum { ATI_PCM_OUT, ATI_PCM_IN, NUM_ATI_PCMS }; /* AC97 pcm slots */
  158. enum { ATI_PCMDEV_ANALOG, NUM_ATI_PCMDEVS }; /* pcm devices */
  159. #define NUM_ATI_CODECS 3
  160. /*
  161. * constants and callbacks for each DMA type
  162. */
  163. struct atiixp_dma_ops {
  164. int type; /* ATI_DMA_XXX */
  165. unsigned int llp_offset; /* LINKPTR offset */
  166. unsigned int dt_cur; /* DT_CUR offset */
  167. /* called from open callback */
  168. void (*enable_dma)(struct atiixp_modem *chip, int on);
  169. /* called from trigger (START/STOP) */
  170. void (*enable_transfer)(struct atiixp_modem *chip, int on);
  171. /* called from trigger (STOP only) */
  172. void (*flush_dma)(struct atiixp_modem *chip);
  173. };
  174. /*
  175. * DMA stream
  176. */
  177. struct atiixp_dma {
  178. const struct atiixp_dma_ops *ops;
  179. struct snd_dma_buffer desc_buf;
  180. struct snd_pcm_substream *substream; /* assigned PCM substream */
  181. unsigned int buf_addr, buf_bytes; /* DMA buffer address, bytes */
  182. unsigned int period_bytes, periods;
  183. int opened;
  184. int running;
  185. int pcm_open_flag;
  186. int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
  187. };
  188. /*
  189. * ATI IXP chip
  190. */
  191. struct atiixp_modem {
  192. struct snd_card *card;
  193. struct pci_dev *pci;
  194. struct resource *res; /* memory i/o */
  195. unsigned long addr;
  196. void __iomem *remap_addr;
  197. int irq;
  198. struct snd_ac97_bus *ac97_bus;
  199. struct snd_ac97 *ac97[NUM_ATI_CODECS];
  200. spinlock_t reg_lock;
  201. struct atiixp_dma dmas[NUM_ATI_DMAS];
  202. struct ac97_pcm *pcms[NUM_ATI_PCMS];
  203. struct snd_pcm *pcmdevs[NUM_ATI_PCMDEVS];
  204. int max_channels; /* max. channels for PCM out */
  205. unsigned int codec_not_ready_bits; /* for codec detection */
  206. int spdif_over_aclink; /* passed from the module option */
  207. struct mutex open_mutex; /* playback open mutex */
  208. };
  209. /*
  210. */
  211. static const struct pci_device_id snd_atiixp_ids[] = {
  212. { PCI_VDEVICE(ATI, 0x434d), 0 }, /* SB200 */
  213. { PCI_VDEVICE(ATI, 0x4378), 0 }, /* SB400 */
  214. { 0, }
  215. };
  216. MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
  217. /*
  218. * lowlevel functions
  219. */
  220. /*
  221. * update the bits of the given register.
  222. * return 1 if the bits changed.
  223. */
  224. static int snd_atiixp_update_bits(struct atiixp_modem *chip, unsigned int reg,
  225. unsigned int mask, unsigned int value)
  226. {
  227. void __iomem *addr = chip->remap_addr + reg;
  228. unsigned int data, old_data;
  229. old_data = data = readl(addr);
  230. data &= ~mask;
  231. data |= value;
  232. if (old_data == data)
  233. return 0;
  234. writel(data, addr);
  235. return 1;
  236. }
  237. /*
  238. * macros for easy use
  239. */
  240. #define atiixp_write(chip,reg,value) \
  241. writel(value, chip->remap_addr + ATI_REG_##reg)
  242. #define atiixp_read(chip,reg) \
  243. readl(chip->remap_addr + ATI_REG_##reg)
  244. #define atiixp_update(chip,reg,mask,val) \
  245. snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
  246. /*
  247. * handling DMA packets
  248. *
  249. * we allocate a linear buffer for the DMA, and split it to each packet.
  250. * in a future version, a scatter-gather buffer should be implemented.
  251. */
  252. #define ATI_DESC_LIST_SIZE \
  253. PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(struct atiixp_dma_desc))
  254. /*
  255. * build packets ring for the given buffer size.
  256. *
  257. * IXP handles the buffer descriptors, which are connected as a linked
  258. * list. although we can change the list dynamically, in this version,
  259. * a static RING of buffer descriptors is used.
  260. *
  261. * the ring is built in this function, and is set up to the hardware.
  262. */
  263. static int atiixp_build_dma_packets(struct atiixp_modem *chip,
  264. struct atiixp_dma *dma,
  265. struct snd_pcm_substream *substream,
  266. unsigned int periods,
  267. unsigned int period_bytes)
  268. {
  269. unsigned int i;
  270. u32 addr, desc_addr;
  271. unsigned long flags;
  272. if (periods > ATI_MAX_DESCRIPTORS)
  273. return -ENOMEM;
  274. if (dma->desc_buf.area == NULL) {
  275. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
  276. ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
  277. return -ENOMEM;
  278. dma->period_bytes = dma->periods = 0; /* clear */
  279. }
  280. if (dma->periods == periods && dma->period_bytes == period_bytes)
  281. return 0;
  282. /* reset DMA before changing the descriptor table */
  283. spin_lock_irqsave(&chip->reg_lock, flags);
  284. writel(0, chip->remap_addr + dma->ops->llp_offset);
  285. dma->ops->enable_dma(chip, 0);
  286. dma->ops->enable_dma(chip, 1);
  287. spin_unlock_irqrestore(&chip->reg_lock, flags);
  288. /* fill the entries */
  289. addr = (u32)substream->runtime->dma_addr;
  290. desc_addr = (u32)dma->desc_buf.addr;
  291. for (i = 0; i < periods; i++) {
  292. struct atiixp_dma_desc *desc;
  293. desc = &((struct atiixp_dma_desc *)dma->desc_buf.area)[i];
  294. desc->addr = cpu_to_le32(addr);
  295. desc->status = 0;
  296. desc->size = period_bytes >> 2; /* in dwords */
  297. desc_addr += sizeof(struct atiixp_dma_desc);
  298. if (i == periods - 1)
  299. desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
  300. else
  301. desc->next = cpu_to_le32(desc_addr);
  302. addr += period_bytes;
  303. }
  304. writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
  305. chip->remap_addr + dma->ops->llp_offset);
  306. dma->period_bytes = period_bytes;
  307. dma->periods = periods;
  308. return 0;
  309. }
  310. /*
  311. * remove the ring buffer and release it if assigned
  312. */
  313. static void atiixp_clear_dma_packets(struct atiixp_modem *chip,
  314. struct atiixp_dma *dma,
  315. struct snd_pcm_substream *substream)
  316. {
  317. if (dma->desc_buf.area) {
  318. writel(0, chip->remap_addr + dma->ops->llp_offset);
  319. snd_dma_free_pages(&dma->desc_buf);
  320. dma->desc_buf.area = NULL;
  321. }
  322. }
  323. /*
  324. * AC97 interface
  325. */
  326. static int snd_atiixp_acquire_codec(struct atiixp_modem *chip)
  327. {
  328. int timeout = 1000;
  329. while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
  330. if (! timeout--) {
  331. dev_warn(chip->card->dev, "codec acquire timeout\n");
  332. return -EBUSY;
  333. }
  334. udelay(1);
  335. }
  336. return 0;
  337. }
  338. static unsigned short snd_atiixp_codec_read(struct atiixp_modem *chip,
  339. unsigned short codec,
  340. unsigned short reg)
  341. {
  342. unsigned int data;
  343. int timeout;
  344. if (snd_atiixp_acquire_codec(chip) < 0)
  345. return 0xffff;
  346. data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  347. ATI_REG_PHYS_OUT_ADDR_EN |
  348. ATI_REG_PHYS_OUT_RW |
  349. codec;
  350. atiixp_write(chip, PHYS_OUT_ADDR, data);
  351. if (snd_atiixp_acquire_codec(chip) < 0)
  352. return 0xffff;
  353. timeout = 1000;
  354. do {
  355. data = atiixp_read(chip, PHYS_IN_ADDR);
  356. if (data & ATI_REG_PHYS_IN_READ_FLAG)
  357. return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
  358. udelay(1);
  359. } while (--timeout);
  360. /* time out may happen during reset */
  361. if (reg < 0x7c)
  362. dev_warn(chip->card->dev, "codec read timeout (reg %x)\n", reg);
  363. return 0xffff;
  364. }
  365. static void snd_atiixp_codec_write(struct atiixp_modem *chip,
  366. unsigned short codec,
  367. unsigned short reg, unsigned short val)
  368. {
  369. unsigned int data;
  370. if (snd_atiixp_acquire_codec(chip) < 0)
  371. return;
  372. data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
  373. ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
  374. ATI_REG_PHYS_OUT_ADDR_EN | codec;
  375. atiixp_write(chip, PHYS_OUT_ADDR, data);
  376. }
  377. static unsigned short snd_atiixp_ac97_read(struct snd_ac97 *ac97,
  378. unsigned short reg)
  379. {
  380. struct atiixp_modem *chip = ac97->private_data;
  381. return snd_atiixp_codec_read(chip, ac97->num, reg);
  382. }
  383. static void snd_atiixp_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  384. unsigned short val)
  385. {
  386. struct atiixp_modem *chip = ac97->private_data;
  387. if (reg == AC97_GPIO_STATUS) {
  388. atiixp_write(chip, MODEM_OUT_GPIO,
  389. (val << ATI_REG_MODEM_OUT_GPIO_DATA_SHIFT) | ATI_REG_MODEM_OUT_GPIO_EN);
  390. return;
  391. }
  392. snd_atiixp_codec_write(chip, ac97->num, reg, val);
  393. }
  394. /*
  395. * reset AC link
  396. */
  397. static int snd_atiixp_aclink_reset(struct atiixp_modem *chip)
  398. {
  399. int timeout;
  400. /* reset powerdoewn */
  401. if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
  402. udelay(10);
  403. /* perform a software reset */
  404. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
  405. atiixp_read(chip, CMD);
  406. udelay(10);
  407. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
  408. timeout = 10;
  409. while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
  410. /* do a hard reset */
  411. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  412. ATI_REG_CMD_AC_SYNC);
  413. atiixp_read(chip, CMD);
  414. msleep(1);
  415. atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
  416. if (!--timeout) {
  417. dev_err(chip->card->dev, "codec reset timeout\n");
  418. break;
  419. }
  420. }
  421. /* deassert RESET and assert SYNC to make sure */
  422. atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
  423. ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
  424. return 0;
  425. }
  426. #ifdef CONFIG_PM_SLEEP
  427. static int snd_atiixp_aclink_down(struct atiixp_modem *chip)
  428. {
  429. // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
  430. // return -EBUSY;
  431. atiixp_update(chip, CMD,
  432. ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
  433. ATI_REG_CMD_POWERDOWN);
  434. return 0;
  435. }
  436. #endif
  437. /*
  438. * auto-detection of codecs
  439. *
  440. * the IXP chip can generate interrupts for the non-existing codecs.
  441. * NEW_FRAME interrupt is used to make sure that the interrupt is generated
  442. * even if all three codecs are connected.
  443. */
  444. #define ALL_CODEC_NOT_READY \
  445. (ATI_REG_ISR_CODEC0_NOT_READY |\
  446. ATI_REG_ISR_CODEC1_NOT_READY |\
  447. ATI_REG_ISR_CODEC2_NOT_READY)
  448. #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
  449. static int snd_atiixp_codec_detect(struct atiixp_modem *chip)
  450. {
  451. int timeout;
  452. chip->codec_not_ready_bits = 0;
  453. atiixp_write(chip, IER, CODEC_CHECK_BITS);
  454. /* wait for the interrupts */
  455. timeout = 50;
  456. while (timeout-- > 0) {
  457. msleep(1);
  458. if (chip->codec_not_ready_bits)
  459. break;
  460. }
  461. atiixp_write(chip, IER, 0); /* disable irqs */
  462. if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
  463. dev_err(chip->card->dev, "no codec detected!\n");
  464. return -ENXIO;
  465. }
  466. return 0;
  467. }
  468. /*
  469. * enable DMA and irqs
  470. */
  471. static int snd_atiixp_chip_start(struct atiixp_modem *chip)
  472. {
  473. unsigned int reg;
  474. /* set up spdif, enable burst mode */
  475. reg = atiixp_read(chip, CMD);
  476. reg |= ATI_REG_CMD_BURST_EN;
  477. if(!(reg & ATI_REG_CMD_MODEM_PRESENT))
  478. reg |= ATI_REG_CMD_MODEM_PRESENT;
  479. atiixp_write(chip, CMD, reg);
  480. /* clear all interrupt source */
  481. atiixp_write(chip, ISR, 0xffffffff);
  482. /* enable irqs */
  483. atiixp_write(chip, IER,
  484. ATI_REG_IER_MODEM_STATUS_EN |
  485. ATI_REG_IER_MODEM_IN_XRUN_EN |
  486. ATI_REG_IER_MODEM_OUT1_XRUN_EN);
  487. return 0;
  488. }
  489. /*
  490. * disable DMA and IRQs
  491. */
  492. static int snd_atiixp_chip_stop(struct atiixp_modem *chip)
  493. {
  494. /* clear interrupt source */
  495. atiixp_write(chip, ISR, atiixp_read(chip, ISR));
  496. /* disable irqs */
  497. atiixp_write(chip, IER, 0);
  498. return 0;
  499. }
  500. /*
  501. * PCM section
  502. */
  503. /*
  504. * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
  505. * position. when SG-buffer is implemented, the offset must be calculated
  506. * correctly...
  507. */
  508. static snd_pcm_uframes_t snd_atiixp_pcm_pointer(struct snd_pcm_substream *substream)
  509. {
  510. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  511. struct snd_pcm_runtime *runtime = substream->runtime;
  512. struct atiixp_dma *dma = runtime->private_data;
  513. unsigned int curptr;
  514. int timeout = 1000;
  515. while (timeout--) {
  516. curptr = readl(chip->remap_addr + dma->ops->dt_cur);
  517. if (curptr < dma->buf_addr)
  518. continue;
  519. curptr -= dma->buf_addr;
  520. if (curptr >= dma->buf_bytes)
  521. continue;
  522. return bytes_to_frames(runtime, curptr);
  523. }
  524. dev_dbg(chip->card->dev, "invalid DMA pointer read 0x%x (buf=%x)\n",
  525. readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
  526. return 0;
  527. }
  528. /*
  529. * XRUN detected, and stop the PCM substream
  530. */
  531. static void snd_atiixp_xrun_dma(struct atiixp_modem *chip,
  532. struct atiixp_dma *dma)
  533. {
  534. if (! dma->substream || ! dma->running)
  535. return;
  536. dev_dbg(chip->card->dev, "XRUN detected (DMA %d)\n", dma->ops->type);
  537. snd_pcm_stop_xrun(dma->substream);
  538. }
  539. /*
  540. * the period ack. update the substream.
  541. */
  542. static void snd_atiixp_update_dma(struct atiixp_modem *chip,
  543. struct atiixp_dma *dma)
  544. {
  545. if (! dma->substream || ! dma->running)
  546. return;
  547. snd_pcm_period_elapsed(dma->substream);
  548. }
  549. /* set BUS_BUSY interrupt bit if any DMA is running */
  550. /* call with spinlock held */
  551. static void snd_atiixp_check_bus_busy(struct atiixp_modem *chip)
  552. {
  553. unsigned int bus_busy;
  554. if (atiixp_read(chip, CMD) & (ATI_REG_CMD_MODEM_SEND1_EN |
  555. ATI_REG_CMD_MODEM_RECEIVE_EN))
  556. bus_busy = ATI_REG_IER_MODEM_SET_BUS_BUSY;
  557. else
  558. bus_busy = 0;
  559. atiixp_update(chip, IER, ATI_REG_IER_MODEM_SET_BUS_BUSY, bus_busy);
  560. }
  561. /* common trigger callback
  562. * calling the lowlevel callbacks in it
  563. */
  564. static int snd_atiixp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  565. {
  566. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  567. struct atiixp_dma *dma = substream->runtime->private_data;
  568. int err = 0;
  569. if (snd_BUG_ON(!dma->ops->enable_transfer ||
  570. !dma->ops->flush_dma))
  571. return -EINVAL;
  572. spin_lock(&chip->reg_lock);
  573. switch(cmd) {
  574. case SNDRV_PCM_TRIGGER_START:
  575. dma->ops->enable_transfer(chip, 1);
  576. dma->running = 1;
  577. break;
  578. case SNDRV_PCM_TRIGGER_STOP:
  579. dma->ops->enable_transfer(chip, 0);
  580. dma->running = 0;
  581. break;
  582. default:
  583. err = -EINVAL;
  584. break;
  585. }
  586. if (! err) {
  587. snd_atiixp_check_bus_busy(chip);
  588. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  589. dma->ops->flush_dma(chip);
  590. snd_atiixp_check_bus_busy(chip);
  591. }
  592. }
  593. spin_unlock(&chip->reg_lock);
  594. return err;
  595. }
  596. /*
  597. * lowlevel callbacks for each DMA type
  598. *
  599. * every callback is supposed to be called in chip->reg_lock spinlock
  600. */
  601. /* flush FIFO of analog OUT DMA */
  602. static void atiixp_out_flush_dma(struct atiixp_modem *chip)
  603. {
  604. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_OUT1_FLUSH);
  605. }
  606. /* enable/disable analog OUT DMA */
  607. static void atiixp_out_enable_dma(struct atiixp_modem *chip, int on)
  608. {
  609. unsigned int data;
  610. data = atiixp_read(chip, CMD);
  611. if (on) {
  612. if (data & ATI_REG_CMD_MODEM_OUT_DMA1_EN)
  613. return;
  614. atiixp_out_flush_dma(chip);
  615. data |= ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  616. } else
  617. data &= ~ATI_REG_CMD_MODEM_OUT_DMA1_EN;
  618. atiixp_write(chip, CMD, data);
  619. }
  620. /* start/stop transfer over OUT DMA */
  621. static void atiixp_out_enable_transfer(struct atiixp_modem *chip, int on)
  622. {
  623. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_SEND1_EN,
  624. on ? ATI_REG_CMD_MODEM_SEND1_EN : 0);
  625. }
  626. /* enable/disable analog IN DMA */
  627. static void atiixp_in_enable_dma(struct atiixp_modem *chip, int on)
  628. {
  629. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_IN_DMA_EN,
  630. on ? ATI_REG_CMD_MODEM_IN_DMA_EN : 0);
  631. }
  632. /* start/stop analog IN DMA */
  633. static void atiixp_in_enable_transfer(struct atiixp_modem *chip, int on)
  634. {
  635. if (on) {
  636. unsigned int data = atiixp_read(chip, CMD);
  637. if (! (data & ATI_REG_CMD_MODEM_RECEIVE_EN)) {
  638. data |= ATI_REG_CMD_MODEM_RECEIVE_EN;
  639. atiixp_write(chip, CMD, data);
  640. }
  641. } else
  642. atiixp_update(chip, CMD, ATI_REG_CMD_MODEM_RECEIVE_EN, 0);
  643. }
  644. /* flush FIFO of analog IN DMA */
  645. static void atiixp_in_flush_dma(struct atiixp_modem *chip)
  646. {
  647. atiixp_write(chip, MODEM_FIFO_FLUSH, ATI_REG_MODEM_FIFO_IN_FLUSH);
  648. }
  649. /* set up slots and formats for analog OUT */
  650. static int snd_atiixp_playback_prepare(struct snd_pcm_substream *substream)
  651. {
  652. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  653. unsigned int data;
  654. spin_lock_irq(&chip->reg_lock);
  655. /* set output threshold */
  656. data = atiixp_read(chip, MODEM_OUT_FIFO);
  657. data &= ~ATI_REG_MODEM_OUT1_DMA_THRESHOLD_MASK;
  658. data |= 0x04 << ATI_REG_MODEM_OUT1_DMA_THRESHOLD_SHIFT;
  659. atiixp_write(chip, MODEM_OUT_FIFO, data);
  660. spin_unlock_irq(&chip->reg_lock);
  661. return 0;
  662. }
  663. /* set up slots and formats for analog IN */
  664. static int snd_atiixp_capture_prepare(struct snd_pcm_substream *substream)
  665. {
  666. return 0;
  667. }
  668. /*
  669. * hw_params - allocate the buffer and set up buffer descriptors
  670. */
  671. static int snd_atiixp_pcm_hw_params(struct snd_pcm_substream *substream,
  672. struct snd_pcm_hw_params *hw_params)
  673. {
  674. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  675. struct atiixp_dma *dma = substream->runtime->private_data;
  676. int err;
  677. int i;
  678. dma->buf_addr = substream->runtime->dma_addr;
  679. dma->buf_bytes = params_buffer_bytes(hw_params);
  680. err = atiixp_build_dma_packets(chip, dma, substream,
  681. params_periods(hw_params),
  682. params_period_bytes(hw_params));
  683. if (err < 0)
  684. return err;
  685. /* set up modem rate */
  686. for (i = 0; i < NUM_ATI_CODECS; i++) {
  687. if (! chip->ac97[i])
  688. continue;
  689. snd_ac97_write(chip->ac97[i], AC97_LINE1_RATE, params_rate(hw_params));
  690. snd_ac97_write(chip->ac97[i], AC97_LINE1_LEVEL, 0);
  691. }
  692. return err;
  693. }
  694. static int snd_atiixp_pcm_hw_free(struct snd_pcm_substream *substream)
  695. {
  696. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  697. struct atiixp_dma *dma = substream->runtime->private_data;
  698. atiixp_clear_dma_packets(chip, dma, substream);
  699. return 0;
  700. }
  701. /*
  702. * pcm hardware definition, identical for all DMA types
  703. */
  704. static const struct snd_pcm_hardware snd_atiixp_pcm_hw =
  705. {
  706. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  707. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  708. SNDRV_PCM_INFO_MMAP_VALID),
  709. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  710. .rates = (SNDRV_PCM_RATE_8000 |
  711. SNDRV_PCM_RATE_16000 |
  712. SNDRV_PCM_RATE_KNOT),
  713. .rate_min = 8000,
  714. .rate_max = 16000,
  715. .channels_min = 2,
  716. .channels_max = 2,
  717. .buffer_bytes_max = 256 * 1024,
  718. .period_bytes_min = 32,
  719. .period_bytes_max = 128 * 1024,
  720. .periods_min = 2,
  721. .periods_max = ATI_MAX_DESCRIPTORS,
  722. };
  723. static int snd_atiixp_pcm_open(struct snd_pcm_substream *substream,
  724. struct atiixp_dma *dma, int pcm_type)
  725. {
  726. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  727. struct snd_pcm_runtime *runtime = substream->runtime;
  728. int err;
  729. static const unsigned int rates[] = { 8000, 9600, 12000, 16000 };
  730. static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  731. .count = ARRAY_SIZE(rates),
  732. .list = rates,
  733. .mask = 0,
  734. };
  735. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  736. return -EINVAL;
  737. if (dma->opened)
  738. return -EBUSY;
  739. dma->substream = substream;
  740. runtime->hw = snd_atiixp_pcm_hw;
  741. dma->ac97_pcm_type = pcm_type;
  742. if ((err = snd_pcm_hw_constraint_list(runtime, 0,
  743. SNDRV_PCM_HW_PARAM_RATE,
  744. &hw_constraints_rates)) < 0)
  745. return err;
  746. if ((err = snd_pcm_hw_constraint_integer(runtime,
  747. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  748. return err;
  749. runtime->private_data = dma;
  750. /* enable DMA bits */
  751. spin_lock_irq(&chip->reg_lock);
  752. dma->ops->enable_dma(chip, 1);
  753. spin_unlock_irq(&chip->reg_lock);
  754. dma->opened = 1;
  755. return 0;
  756. }
  757. static int snd_atiixp_pcm_close(struct snd_pcm_substream *substream,
  758. struct atiixp_dma *dma)
  759. {
  760. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  761. /* disable DMA bits */
  762. if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
  763. return -EINVAL;
  764. spin_lock_irq(&chip->reg_lock);
  765. dma->ops->enable_dma(chip, 0);
  766. spin_unlock_irq(&chip->reg_lock);
  767. dma->substream = NULL;
  768. dma->opened = 0;
  769. return 0;
  770. }
  771. /*
  772. */
  773. static int snd_atiixp_playback_open(struct snd_pcm_substream *substream)
  774. {
  775. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  776. int err;
  777. mutex_lock(&chip->open_mutex);
  778. err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
  779. mutex_unlock(&chip->open_mutex);
  780. if (err < 0)
  781. return err;
  782. return 0;
  783. }
  784. static int snd_atiixp_playback_close(struct snd_pcm_substream *substream)
  785. {
  786. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  787. int err;
  788. mutex_lock(&chip->open_mutex);
  789. err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
  790. mutex_unlock(&chip->open_mutex);
  791. return err;
  792. }
  793. static int snd_atiixp_capture_open(struct snd_pcm_substream *substream)
  794. {
  795. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  796. return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
  797. }
  798. static int snd_atiixp_capture_close(struct snd_pcm_substream *substream)
  799. {
  800. struct atiixp_modem *chip = snd_pcm_substream_chip(substream);
  801. return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
  802. }
  803. /* AC97 playback */
  804. static const struct snd_pcm_ops snd_atiixp_playback_ops = {
  805. .open = snd_atiixp_playback_open,
  806. .close = snd_atiixp_playback_close,
  807. .hw_params = snd_atiixp_pcm_hw_params,
  808. .hw_free = snd_atiixp_pcm_hw_free,
  809. .prepare = snd_atiixp_playback_prepare,
  810. .trigger = snd_atiixp_pcm_trigger,
  811. .pointer = snd_atiixp_pcm_pointer,
  812. };
  813. /* AC97 capture */
  814. static const struct snd_pcm_ops snd_atiixp_capture_ops = {
  815. .open = snd_atiixp_capture_open,
  816. .close = snd_atiixp_capture_close,
  817. .hw_params = snd_atiixp_pcm_hw_params,
  818. .hw_free = snd_atiixp_pcm_hw_free,
  819. .prepare = snd_atiixp_capture_prepare,
  820. .trigger = snd_atiixp_pcm_trigger,
  821. .pointer = snd_atiixp_pcm_pointer,
  822. };
  823. static const struct atiixp_dma_ops snd_atiixp_playback_dma_ops = {
  824. .type = ATI_DMA_PLAYBACK,
  825. .llp_offset = ATI_REG_MODEM_OUT_DMA1_LINKPTR,
  826. .dt_cur = ATI_REG_MODEM_OUT_DMA1_DT_CUR,
  827. .enable_dma = atiixp_out_enable_dma,
  828. .enable_transfer = atiixp_out_enable_transfer,
  829. .flush_dma = atiixp_out_flush_dma,
  830. };
  831. static const struct atiixp_dma_ops snd_atiixp_capture_dma_ops = {
  832. .type = ATI_DMA_CAPTURE,
  833. .llp_offset = ATI_REG_MODEM_IN_DMA_LINKPTR,
  834. .dt_cur = ATI_REG_MODEM_IN_DMA_DT_CUR,
  835. .enable_dma = atiixp_in_enable_dma,
  836. .enable_transfer = atiixp_in_enable_transfer,
  837. .flush_dma = atiixp_in_flush_dma,
  838. };
  839. static int snd_atiixp_pcm_new(struct atiixp_modem *chip)
  840. {
  841. struct snd_pcm *pcm;
  842. int err;
  843. /* initialize constants */
  844. chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
  845. chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
  846. /* PCM #0: analog I/O */
  847. err = snd_pcm_new(chip->card, "ATI IXP MC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
  848. if (err < 0)
  849. return err;
  850. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
  851. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
  852. pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
  853. pcm->private_data = chip;
  854. strcpy(pcm->name, "ATI IXP MC97");
  855. chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
  856. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  857. &chip->pci->dev, 64*1024, 128*1024);
  858. return 0;
  859. }
  860. /*
  861. * interrupt handler
  862. */
  863. static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id)
  864. {
  865. struct atiixp_modem *chip = dev_id;
  866. unsigned int status;
  867. status = atiixp_read(chip, ISR);
  868. if (! status)
  869. return IRQ_NONE;
  870. /* process audio DMA */
  871. if (status & ATI_REG_ISR_MODEM_OUT1_XRUN)
  872. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  873. else if (status & ATI_REG_ISR_MODEM_OUT1_STATUS)
  874. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
  875. if (status & ATI_REG_ISR_MODEM_IN_XRUN)
  876. snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  877. else if (status & ATI_REG_ISR_MODEM_IN_STATUS)
  878. snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
  879. /* for codec detection */
  880. if (status & CODEC_CHECK_BITS) {
  881. unsigned int detected;
  882. detected = status & CODEC_CHECK_BITS;
  883. spin_lock(&chip->reg_lock);
  884. chip->codec_not_ready_bits |= detected;
  885. atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
  886. spin_unlock(&chip->reg_lock);
  887. }
  888. /* ack */
  889. atiixp_write(chip, ISR, status);
  890. return IRQ_HANDLED;
  891. }
  892. /*
  893. * ac97 mixer section
  894. */
  895. static int snd_atiixp_mixer_new(struct atiixp_modem *chip, int clock)
  896. {
  897. struct snd_ac97_bus *pbus;
  898. struct snd_ac97_template ac97;
  899. int i, err;
  900. int codec_count;
  901. static const struct snd_ac97_bus_ops ops = {
  902. .write = snd_atiixp_ac97_write,
  903. .read = snd_atiixp_ac97_read,
  904. };
  905. static const unsigned int codec_skip[NUM_ATI_CODECS] = {
  906. ATI_REG_ISR_CODEC0_NOT_READY,
  907. ATI_REG_ISR_CODEC1_NOT_READY,
  908. ATI_REG_ISR_CODEC2_NOT_READY,
  909. };
  910. if (snd_atiixp_codec_detect(chip) < 0)
  911. return -ENXIO;
  912. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  913. return err;
  914. pbus->clock = clock;
  915. chip->ac97_bus = pbus;
  916. codec_count = 0;
  917. for (i = 0; i < NUM_ATI_CODECS; i++) {
  918. if (chip->codec_not_ready_bits & codec_skip[i])
  919. continue;
  920. memset(&ac97, 0, sizeof(ac97));
  921. ac97.private_data = chip;
  922. ac97.pci = chip->pci;
  923. ac97.num = i;
  924. ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
  925. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
  926. chip->ac97[i] = NULL; /* to be sure */
  927. dev_dbg(chip->card->dev,
  928. "codec %d not available for modem\n", i);
  929. continue;
  930. }
  931. codec_count++;
  932. }
  933. if (! codec_count) {
  934. dev_err(chip->card->dev, "no codec available\n");
  935. return -ENODEV;
  936. }
  937. /* snd_ac97_tune_hardware(chip->ac97, ac97_quirks); */
  938. return 0;
  939. }
  940. #ifdef CONFIG_PM_SLEEP
  941. /*
  942. * power management
  943. */
  944. static int snd_atiixp_suspend(struct device *dev)
  945. {
  946. struct snd_card *card = dev_get_drvdata(dev);
  947. struct atiixp_modem *chip = card->private_data;
  948. int i;
  949. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  950. for (i = 0; i < NUM_ATI_CODECS; i++)
  951. snd_ac97_suspend(chip->ac97[i]);
  952. snd_atiixp_aclink_down(chip);
  953. snd_atiixp_chip_stop(chip);
  954. return 0;
  955. }
  956. static int snd_atiixp_resume(struct device *dev)
  957. {
  958. struct snd_card *card = dev_get_drvdata(dev);
  959. struct atiixp_modem *chip = card->private_data;
  960. int i;
  961. snd_atiixp_aclink_reset(chip);
  962. snd_atiixp_chip_start(chip);
  963. for (i = 0; i < NUM_ATI_CODECS; i++)
  964. snd_ac97_resume(chip->ac97[i]);
  965. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  966. return 0;
  967. }
  968. static SIMPLE_DEV_PM_OPS(snd_atiixp_pm, snd_atiixp_suspend, snd_atiixp_resume);
  969. #define SND_ATIIXP_PM_OPS &snd_atiixp_pm
  970. #else
  971. #define SND_ATIIXP_PM_OPS NULL
  972. #endif /* CONFIG_PM_SLEEP */
  973. /*
  974. * proc interface for register dump
  975. */
  976. static void snd_atiixp_proc_read(struct snd_info_entry *entry,
  977. struct snd_info_buffer *buffer)
  978. {
  979. struct atiixp_modem *chip = entry->private_data;
  980. int i;
  981. for (i = 0; i < 256; i += 4)
  982. snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
  983. }
  984. static void snd_atiixp_proc_init(struct atiixp_modem *chip)
  985. {
  986. snd_card_ro_proc_new(chip->card, "atiixp-modem", chip,
  987. snd_atiixp_proc_read);
  988. }
  989. /*
  990. * destructor
  991. */
  992. static int snd_atiixp_free(struct atiixp_modem *chip)
  993. {
  994. if (chip->irq < 0)
  995. goto __hw_end;
  996. snd_atiixp_chip_stop(chip);
  997. __hw_end:
  998. if (chip->irq >= 0)
  999. free_irq(chip->irq, chip);
  1000. iounmap(chip->remap_addr);
  1001. pci_release_regions(chip->pci);
  1002. pci_disable_device(chip->pci);
  1003. kfree(chip);
  1004. return 0;
  1005. }
  1006. static int snd_atiixp_dev_free(struct snd_device *device)
  1007. {
  1008. struct atiixp_modem *chip = device->device_data;
  1009. return snd_atiixp_free(chip);
  1010. }
  1011. /*
  1012. * constructor for chip instance
  1013. */
  1014. static int snd_atiixp_create(struct snd_card *card,
  1015. struct pci_dev *pci,
  1016. struct atiixp_modem **r_chip)
  1017. {
  1018. static const struct snd_device_ops ops = {
  1019. .dev_free = snd_atiixp_dev_free,
  1020. };
  1021. struct atiixp_modem *chip;
  1022. int err;
  1023. if ((err = pci_enable_device(pci)) < 0)
  1024. return err;
  1025. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1026. if (chip == NULL) {
  1027. pci_disable_device(pci);
  1028. return -ENOMEM;
  1029. }
  1030. spin_lock_init(&chip->reg_lock);
  1031. mutex_init(&chip->open_mutex);
  1032. chip->card = card;
  1033. chip->pci = pci;
  1034. chip->irq = -1;
  1035. if ((err = pci_request_regions(pci, "ATI IXP MC97")) < 0) {
  1036. kfree(chip);
  1037. pci_disable_device(pci);
  1038. return err;
  1039. }
  1040. chip->addr = pci_resource_start(pci, 0);
  1041. chip->remap_addr = pci_ioremap_bar(pci, 0);
  1042. if (chip->remap_addr == NULL) {
  1043. dev_err(card->dev, "AC'97 space ioremap problem\n");
  1044. snd_atiixp_free(chip);
  1045. return -EIO;
  1046. }
  1047. if (request_irq(pci->irq, snd_atiixp_interrupt, IRQF_SHARED,
  1048. KBUILD_MODNAME, chip)) {
  1049. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  1050. snd_atiixp_free(chip);
  1051. return -EBUSY;
  1052. }
  1053. chip->irq = pci->irq;
  1054. card->sync_irq = chip->irq;
  1055. pci_set_master(pci);
  1056. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1057. snd_atiixp_free(chip);
  1058. return err;
  1059. }
  1060. *r_chip = chip;
  1061. return 0;
  1062. }
  1063. static int snd_atiixp_probe(struct pci_dev *pci,
  1064. const struct pci_device_id *pci_id)
  1065. {
  1066. struct snd_card *card;
  1067. struct atiixp_modem *chip;
  1068. int err;
  1069. err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
  1070. if (err < 0)
  1071. return err;
  1072. strcpy(card->driver, "ATIIXP-MODEM");
  1073. strcpy(card->shortname, "ATI IXP Modem");
  1074. if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
  1075. goto __error;
  1076. card->private_data = chip;
  1077. if ((err = snd_atiixp_aclink_reset(chip)) < 0)
  1078. goto __error;
  1079. if ((err = snd_atiixp_mixer_new(chip, ac97_clock)) < 0)
  1080. goto __error;
  1081. if ((err = snd_atiixp_pcm_new(chip)) < 0)
  1082. goto __error;
  1083. snd_atiixp_proc_init(chip);
  1084. snd_atiixp_chip_start(chip);
  1085. sprintf(card->longname, "%s rev %x at 0x%lx, irq %i",
  1086. card->shortname, pci->revision, chip->addr, chip->irq);
  1087. if ((err = snd_card_register(card)) < 0)
  1088. goto __error;
  1089. pci_set_drvdata(pci, card);
  1090. return 0;
  1091. __error:
  1092. snd_card_free(card);
  1093. return err;
  1094. }
  1095. static void snd_atiixp_remove(struct pci_dev *pci)
  1096. {
  1097. snd_card_free(pci_get_drvdata(pci));
  1098. }
  1099. static struct pci_driver atiixp_modem_driver = {
  1100. .name = KBUILD_MODNAME,
  1101. .id_table = snd_atiixp_ids,
  1102. .probe = snd_atiixp_probe,
  1103. .remove = snd_atiixp_remove,
  1104. .driver = {
  1105. .pm = SND_ATIIXP_PM_OPS,
  1106. },
  1107. };
  1108. module_pci_driver(atiixp_modem_driver);