oxygen_lib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * C-Media CMI8788 driver - main driver module
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2.
  9. *
  10. * This driver 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 driver; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <sound/ac97_codec.h>
  25. #include <sound/asoundef.h>
  26. #include <sound/core.h>
  27. #include <sound/info.h>
  28. #include <sound/mpu401.h>
  29. #include <sound/pcm.h>
  30. #include "oxygen.h"
  31. #include "cm9780.h"
  32. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  33. MODULE_DESCRIPTION("C-Media CMI8788 helper library");
  34. MODULE_LICENSE("GPL v2");
  35. #define DRIVER "oxygen"
  36. static inline int oxygen_uart_input_ready(struct oxygen *chip)
  37. {
  38. return !(oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_RX_EMPTY);
  39. }
  40. static void oxygen_read_uart(struct oxygen *chip)
  41. {
  42. if (unlikely(!oxygen_uart_input_ready(chip))) {
  43. /* no data, but read it anyway to clear the interrupt */
  44. oxygen_read8(chip, OXYGEN_MPU401);
  45. return;
  46. }
  47. do {
  48. u8 data = oxygen_read8(chip, OXYGEN_MPU401);
  49. if (data == MPU401_ACK)
  50. continue;
  51. if (chip->uart_input_count >= ARRAY_SIZE(chip->uart_input))
  52. chip->uart_input_count = 0;
  53. chip->uart_input[chip->uart_input_count++] = data;
  54. } while (oxygen_uart_input_ready(chip));
  55. if (chip->model.uart_input)
  56. chip->model.uart_input(chip);
  57. }
  58. static irqreturn_t oxygen_interrupt(int dummy, void *dev_id)
  59. {
  60. struct oxygen *chip = dev_id;
  61. unsigned int status, clear, elapsed_streams, i;
  62. status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS);
  63. if (!status)
  64. return IRQ_NONE;
  65. spin_lock(&chip->reg_lock);
  66. clear = status & (OXYGEN_CHANNEL_A |
  67. OXYGEN_CHANNEL_B |
  68. OXYGEN_CHANNEL_C |
  69. OXYGEN_CHANNEL_SPDIF |
  70. OXYGEN_CHANNEL_MULTICH |
  71. OXYGEN_CHANNEL_AC97 |
  72. OXYGEN_INT_SPDIF_IN_DETECT |
  73. OXYGEN_INT_GPIO |
  74. OXYGEN_INT_AC97);
  75. if (clear) {
  76. if (clear & OXYGEN_INT_SPDIF_IN_DETECT)
  77. chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT;
  78. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
  79. chip->interrupt_mask & ~clear);
  80. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
  81. chip->interrupt_mask);
  82. }
  83. elapsed_streams = status & chip->pcm_running;
  84. spin_unlock(&chip->reg_lock);
  85. for (i = 0; i < PCM_COUNT; ++i)
  86. if ((elapsed_streams & (1 << i)) && chip->streams[i])
  87. snd_pcm_period_elapsed(chip->streams[i]);
  88. if (status & OXYGEN_INT_SPDIF_IN_DETECT) {
  89. spin_lock(&chip->reg_lock);
  90. i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
  91. if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT |
  92. OXYGEN_SPDIF_RATE_INT)) {
  93. /* write the interrupt bit(s) to clear */
  94. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i);
  95. schedule_work(&chip->spdif_input_bits_work);
  96. }
  97. spin_unlock(&chip->reg_lock);
  98. }
  99. if (status & OXYGEN_INT_GPIO)
  100. schedule_work(&chip->gpio_work);
  101. if (status & OXYGEN_INT_MIDI) {
  102. if (chip->midi)
  103. snd_mpu401_uart_interrupt(0, chip->midi->private_data);
  104. else
  105. oxygen_read_uart(chip);
  106. }
  107. if (status & OXYGEN_INT_AC97)
  108. wake_up(&chip->ac97_waitqueue);
  109. return IRQ_HANDLED;
  110. }
  111. static void oxygen_spdif_input_bits_changed(struct work_struct *work)
  112. {
  113. struct oxygen *chip = container_of(work, struct oxygen,
  114. spdif_input_bits_work);
  115. u32 reg;
  116. /*
  117. * This function gets called when there is new activity on the SPDIF
  118. * input, or when we lose lock on the input signal, or when the rate
  119. * changes.
  120. */
  121. msleep(1);
  122. spin_lock_irq(&chip->reg_lock);
  123. reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
  124. if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
  125. OXYGEN_SPDIF_LOCK_STATUS))
  126. == OXYGEN_SPDIF_SENSE_STATUS) {
  127. /*
  128. * If we detect activity on the SPDIF input but cannot lock to
  129. * a signal, the clock bit is likely to be wrong.
  130. */
  131. reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK;
  132. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
  133. spin_unlock_irq(&chip->reg_lock);
  134. msleep(1);
  135. spin_lock_irq(&chip->reg_lock);
  136. reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
  137. if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
  138. OXYGEN_SPDIF_LOCK_STATUS))
  139. == OXYGEN_SPDIF_SENSE_STATUS) {
  140. /* nothing detected with either clock; give up */
  141. if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK)
  142. == OXYGEN_SPDIF_IN_CLOCK_192) {
  143. /*
  144. * Reset clock to <= 96 kHz because this is
  145. * more likely to be received next time.
  146. */
  147. reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK;
  148. reg |= OXYGEN_SPDIF_IN_CLOCK_96;
  149. oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
  150. }
  151. }
  152. }
  153. spin_unlock_irq(&chip->reg_lock);
  154. if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) {
  155. spin_lock_irq(&chip->reg_lock);
  156. chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
  157. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
  158. chip->interrupt_mask);
  159. spin_unlock_irq(&chip->reg_lock);
  160. /*
  161. * We don't actually know that any channel status bits have
  162. * changed, but let's send a notification just to be sure.
  163. */
  164. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  165. &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id);
  166. }
  167. }
  168. static void oxygen_gpio_changed(struct work_struct *work)
  169. {
  170. struct oxygen *chip = container_of(work, struct oxygen, gpio_work);
  171. if (chip->model.gpio_changed)
  172. chip->model.gpio_changed(chip);
  173. }
  174. #ifdef CONFIG_PROC_FS
  175. static void oxygen_proc_read(struct snd_info_entry *entry,
  176. struct snd_info_buffer *buffer)
  177. {
  178. struct oxygen *chip = entry->private_data;
  179. int i, j;
  180. snd_iprintf(buffer, "CMI8788:\n");
  181. for (i = 0; i < OXYGEN_IO_SIZE; i += 0x10) {
  182. snd_iprintf(buffer, "%02x:", i);
  183. for (j = 0; j < 0x10; ++j)
  184. snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
  185. snd_iprintf(buffer, "\n");
  186. }
  187. if (mutex_lock_interruptible(&chip->mutex) < 0)
  188. return;
  189. if (chip->has_ac97_0) {
  190. snd_iprintf(buffer, "\nAC97:\n");
  191. for (i = 0; i < 0x80; i += 0x10) {
  192. snd_iprintf(buffer, "%02x:", i);
  193. for (j = 0; j < 0x10; j += 2)
  194. snd_iprintf(buffer, " %04x",
  195. oxygen_read_ac97(chip, 0, i + j));
  196. snd_iprintf(buffer, "\n");
  197. }
  198. }
  199. if (chip->has_ac97_1) {
  200. snd_iprintf(buffer, "\nAC97 2:\n");
  201. for (i = 0; i < 0x80; i += 0x10) {
  202. snd_iprintf(buffer, "%02x:", i);
  203. for (j = 0; j < 0x10; j += 2)
  204. snd_iprintf(buffer, " %04x",
  205. oxygen_read_ac97(chip, 1, i + j));
  206. snd_iprintf(buffer, "\n");
  207. }
  208. }
  209. mutex_unlock(&chip->mutex);
  210. if (chip->model.dump_registers)
  211. chip->model.dump_registers(chip, buffer);
  212. }
  213. static void oxygen_proc_init(struct oxygen *chip)
  214. {
  215. struct snd_info_entry *entry;
  216. if (!snd_card_proc_new(chip->card, "oxygen", &entry))
  217. snd_info_set_text_ops(entry, chip, oxygen_proc_read);
  218. }
  219. #else
  220. #define oxygen_proc_init(chip)
  221. #endif
  222. static const struct pci_device_id *
  223. oxygen_search_pci_id(struct oxygen *chip, const struct pci_device_id ids[])
  224. {
  225. u16 subdevice;
  226. /*
  227. * Make sure the EEPROM pins are available, i.e., not used for SPI.
  228. * (This function is called before we initialize or use SPI.)
  229. */
  230. oxygen_clear_bits8(chip, OXYGEN_FUNCTION,
  231. OXYGEN_FUNCTION_ENABLE_SPI_4_5);
  232. /*
  233. * Read the subsystem device ID directly from the EEPROM, because the
  234. * chip didn't if the first EEPROM word was overwritten.
  235. */
  236. subdevice = oxygen_read_eeprom(chip, 2);
  237. /* use default ID if EEPROM is missing */
  238. if (subdevice == 0xffff && oxygen_read_eeprom(chip, 1) == 0xffff)
  239. subdevice = 0x8788;
  240. /*
  241. * We use only the subsystem device ID for searching because it is
  242. * unique even without the subsystem vendor ID, which may have been
  243. * overwritten in the EEPROM.
  244. */
  245. for (; ids->vendor; ++ids)
  246. if (ids->subdevice == subdevice &&
  247. ids->driver_data != BROKEN_EEPROM_DRIVER_DATA)
  248. return ids;
  249. return NULL;
  250. }
  251. static void oxygen_restore_eeprom(struct oxygen *chip,
  252. const struct pci_device_id *id)
  253. {
  254. u16 eeprom_id;
  255. eeprom_id = oxygen_read_eeprom(chip, 0);
  256. if (eeprom_id != OXYGEN_EEPROM_ID &&
  257. (eeprom_id != 0xffff || id->subdevice != 0x8788)) {
  258. /*
  259. * This function gets called only when a known card model has
  260. * been detected, i.e., we know there is a valid subsystem
  261. * product ID at index 2 in the EEPROM. Therefore, we have
  262. * been able to deduce the correct subsystem vendor ID, and
  263. * this is enough information to restore the original EEPROM
  264. * contents.
  265. */
  266. oxygen_write_eeprom(chip, 1, id->subvendor);
  267. oxygen_write_eeprom(chip, 0, OXYGEN_EEPROM_ID);
  268. oxygen_set_bits8(chip, OXYGEN_MISC,
  269. OXYGEN_MISC_WRITE_PCI_SUBID);
  270. pci_write_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID,
  271. id->subvendor);
  272. pci_write_config_word(chip->pci, PCI_SUBSYSTEM_ID,
  273. id->subdevice);
  274. oxygen_clear_bits8(chip, OXYGEN_MISC,
  275. OXYGEN_MISC_WRITE_PCI_SUBID);
  276. snd_printk(KERN_INFO "EEPROM ID restored\n");
  277. }
  278. }
  279. static void configure_pcie_bridge(struct pci_dev *pci)
  280. {
  281. enum { PEX811X, PI7C9X110 };
  282. static const struct pci_device_id bridge_ids[] = {
  283. { PCI_VDEVICE(PLX, 0x8111), .driver_data = PEX811X },
  284. { PCI_VDEVICE(PLX, 0x8112), .driver_data = PEX811X },
  285. { PCI_DEVICE(0x12d8, 0xe110), .driver_data = PI7C9X110 },
  286. { }
  287. };
  288. struct pci_dev *bridge;
  289. const struct pci_device_id *id;
  290. u32 tmp;
  291. if (!pci->bus || !pci->bus->self)
  292. return;
  293. bridge = pci->bus->self;
  294. id = pci_match_id(bridge_ids, bridge);
  295. if (!id)
  296. return;
  297. switch (id->driver_data) {
  298. case PEX811X: /* PLX PEX8111/PEX8112 PCIe/PCI bridge */
  299. pci_read_config_dword(bridge, 0x48, &tmp);
  300. tmp |= 1; /* enable blind prefetching */
  301. tmp |= 1 << 11; /* enable beacon generation */
  302. pci_write_config_dword(bridge, 0x48, tmp);
  303. pci_write_config_dword(bridge, 0x84, 0x0c);
  304. pci_read_config_dword(bridge, 0x88, &tmp);
  305. tmp &= ~(7 << 27);
  306. tmp |= 2 << 27; /* set prefetch size to 128 bytes */
  307. pci_write_config_dword(bridge, 0x88, tmp);
  308. break;
  309. case PI7C9X110: /* Pericom PI7C9X110 PCIe/PCI bridge */
  310. pci_read_config_dword(bridge, 0x40, &tmp);
  311. tmp |= 1; /* park the PCI arbiter to the sound chip */
  312. pci_write_config_dword(bridge, 0x40, tmp);
  313. break;
  314. }
  315. }
  316. static void oxygen_init(struct oxygen *chip)
  317. {
  318. unsigned int i;
  319. chip->dac_routing = 1;
  320. for (i = 0; i < 8; ++i)
  321. chip->dac_volume[i] = chip->model.dac_volume_min;
  322. chip->dac_mute = 1;
  323. chip->spdif_playback_enable = 1;
  324. chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
  325. (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
  326. chip->spdif_pcm_bits = chip->spdif_bits;
  327. if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)
  328. chip->revision = 2;
  329. else
  330. chip->revision = 1;
  331. if (chip->revision == 1)
  332. oxygen_set_bits8(chip, OXYGEN_MISC,
  333. OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
  334. i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
  335. chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
  336. chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
  337. oxygen_write8_masked(chip, OXYGEN_FUNCTION,
  338. OXYGEN_FUNCTION_RESET_CODEC |
  339. chip->model.function_flags,
  340. OXYGEN_FUNCTION_RESET_CODEC |
  341. OXYGEN_FUNCTION_2WIRE_SPI_MASK |
  342. OXYGEN_FUNCTION_ENABLE_SPI_4_5);
  343. oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
  344. oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
  345. oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
  346. OXYGEN_PLAY_CHANNELS_2 |
  347. OXYGEN_DMA_A_BURST_8 |
  348. OXYGEN_DMA_MULTICH_BURST_8);
  349. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
  350. oxygen_write8_masked(chip, OXYGEN_MISC,
  351. chip->model.misc_flags,
  352. OXYGEN_MISC_WRITE_PCI_SUBID |
  353. OXYGEN_MISC_REC_C_FROM_SPDIF |
  354. OXYGEN_MISC_REC_B_FROM_AC97 |
  355. OXYGEN_MISC_REC_A_FROM_MULTICH |
  356. OXYGEN_MISC_MIDI);
  357. oxygen_write8(chip, OXYGEN_REC_FORMAT,
  358. (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
  359. (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
  360. (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
  361. oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
  362. (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
  363. (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
  364. oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
  365. oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT,
  366. OXYGEN_RATE_48000 | chip->model.dac_i2s_format |
  367. OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
  368. OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
  369. if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
  370. oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
  371. OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
  372. OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
  373. OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
  374. else
  375. oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
  376. OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
  377. if (chip->model.device_config & (CAPTURE_0_FROM_I2S_2 |
  378. CAPTURE_2_FROM_I2S_2))
  379. oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
  380. OXYGEN_RATE_48000 | chip->model.adc_i2s_format |
  381. OXYGEN_I2S_MCLK_256 | OXYGEN_I2S_BITS_16 |
  382. OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64);
  383. else
  384. oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
  385. OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
  386. oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
  387. OXYGEN_I2S_MASTER | OXYGEN_I2S_MUTE_MCLK);
  388. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  389. OXYGEN_SPDIF_OUT_ENABLE |
  390. OXYGEN_SPDIF_LOOPBACK);
  391. if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
  392. oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
  393. OXYGEN_SPDIF_SENSE_MASK |
  394. OXYGEN_SPDIF_LOCK_MASK |
  395. OXYGEN_SPDIF_RATE_MASK |
  396. OXYGEN_SPDIF_LOCK_PAR |
  397. OXYGEN_SPDIF_IN_CLOCK_96,
  398. OXYGEN_SPDIF_SENSE_MASK |
  399. OXYGEN_SPDIF_LOCK_MASK |
  400. OXYGEN_SPDIF_RATE_MASK |
  401. OXYGEN_SPDIF_SENSE_PAR |
  402. OXYGEN_SPDIF_LOCK_PAR |
  403. OXYGEN_SPDIF_IN_CLOCK_MASK);
  404. else
  405. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  406. OXYGEN_SPDIF_SENSE_MASK |
  407. OXYGEN_SPDIF_LOCK_MASK |
  408. OXYGEN_SPDIF_RATE_MASK);
  409. oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
  410. oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
  411. OXYGEN_2WIRE_LENGTH_8 |
  412. OXYGEN_2WIRE_INTERRUPT_MASK |
  413. OXYGEN_2WIRE_SPEED_STANDARD);
  414. oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
  415. oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
  416. oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
  417. oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
  418. OXYGEN_PLAY_MULTICH_I2S_DAC |
  419. OXYGEN_PLAY_SPDIF_SPDIF |
  420. (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
  421. (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
  422. (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
  423. (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
  424. oxygen_write8(chip, OXYGEN_REC_ROUTING,
  425. OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
  426. OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
  427. OXYGEN_REC_C_ROUTE_SPDIF);
  428. oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
  429. oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
  430. (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
  431. (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
  432. (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
  433. (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
  434. if (chip->has_ac97_0 | chip->has_ac97_1)
  435. oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK,
  436. OXYGEN_AC97_INT_READ_DONE |
  437. OXYGEN_AC97_INT_WRITE_DONE);
  438. else
  439. oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0);
  440. oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
  441. oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
  442. if (!(chip->has_ac97_0 | chip->has_ac97_1))
  443. oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
  444. OXYGEN_AC97_CLOCK_DISABLE);
  445. if (!chip->has_ac97_0) {
  446. oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
  447. OXYGEN_AC97_NO_CODEC_0);
  448. } else {
  449. oxygen_write_ac97(chip, 0, AC97_RESET, 0);
  450. msleep(1);
  451. oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
  452. CM9780_GPIO0IO | CM9780_GPIO1IO);
  453. oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
  454. CM9780_BSTSEL | CM9780_STRO_MIC |
  455. CM9780_MIX2FR | CM9780_PCBSW);
  456. oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
  457. CM9780_RSOE | CM9780_CBOE |
  458. CM9780_SSOE | CM9780_FROE |
  459. CM9780_MIC2MIC | CM9780_LI2LI);
  460. oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
  461. oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
  462. oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
  463. oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
  464. oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
  465. oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
  466. oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
  467. oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
  468. oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
  469. oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
  470. oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS,
  471. CM9780_GPO0);
  472. /* power down unused ADCs and DACs */
  473. oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
  474. AC97_PD_PR0 | AC97_PD_PR1);
  475. oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
  476. AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
  477. }
  478. if (chip->has_ac97_1) {
  479. oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
  480. OXYGEN_AC97_CODEC1_SLOT3 |
  481. OXYGEN_AC97_CODEC1_SLOT4);
  482. oxygen_write_ac97(chip, 1, AC97_RESET, 0);
  483. msleep(1);
  484. oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
  485. oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
  486. oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
  487. oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
  488. oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
  489. oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
  490. oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
  491. oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
  492. oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
  493. oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
  494. oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000);
  495. oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
  496. }
  497. }
  498. static void oxygen_shutdown(struct oxygen *chip)
  499. {
  500. spin_lock_irq(&chip->reg_lock);
  501. chip->interrupt_mask = 0;
  502. chip->pcm_running = 0;
  503. oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
  504. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
  505. spin_unlock_irq(&chip->reg_lock);
  506. }
  507. static void oxygen_card_free(struct snd_card *card)
  508. {
  509. struct oxygen *chip = card->private_data;
  510. oxygen_shutdown(chip);
  511. if (chip->irq >= 0)
  512. free_irq(chip->irq, chip);
  513. flush_scheduled_work();
  514. chip->model.cleanup(chip);
  515. kfree(chip->model_data);
  516. mutex_destroy(&chip->mutex);
  517. pci_release_regions(chip->pci);
  518. pci_disable_device(chip->pci);
  519. }
  520. int oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
  521. struct module *owner,
  522. const struct pci_device_id *ids,
  523. int (*get_model)(struct oxygen *chip,
  524. const struct pci_device_id *id
  525. )
  526. )
  527. {
  528. struct snd_card *card;
  529. struct oxygen *chip;
  530. const struct pci_device_id *pci_id;
  531. int err;
  532. err = snd_card_create(index, id, owner, sizeof(*chip), &card);
  533. if (err < 0)
  534. return err;
  535. chip = card->private_data;
  536. chip->card = card;
  537. chip->pci = pci;
  538. chip->irq = -1;
  539. spin_lock_init(&chip->reg_lock);
  540. mutex_init(&chip->mutex);
  541. INIT_WORK(&chip->spdif_input_bits_work,
  542. oxygen_spdif_input_bits_changed);
  543. INIT_WORK(&chip->gpio_work, oxygen_gpio_changed);
  544. init_waitqueue_head(&chip->ac97_waitqueue);
  545. err = pci_enable_device(pci);
  546. if (err < 0)
  547. goto err_card;
  548. err = pci_request_regions(pci, DRIVER);
  549. if (err < 0) {
  550. snd_printk(KERN_ERR "cannot reserve PCI resources\n");
  551. goto err_pci_enable;
  552. }
  553. if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
  554. pci_resource_len(pci, 0) < OXYGEN_IO_SIZE) {
  555. snd_printk(KERN_ERR "invalid PCI I/O range\n");
  556. err = -ENXIO;
  557. goto err_pci_regions;
  558. }
  559. chip->addr = pci_resource_start(pci, 0);
  560. pci_id = oxygen_search_pci_id(chip, ids);
  561. if (!pci_id) {
  562. err = -ENODEV;
  563. goto err_pci_regions;
  564. }
  565. oxygen_restore_eeprom(chip, pci_id);
  566. err = get_model(chip, pci_id);
  567. if (err < 0)
  568. goto err_pci_regions;
  569. if (chip->model.model_data_size) {
  570. chip->model_data = kzalloc(chip->model.model_data_size,
  571. GFP_KERNEL);
  572. if (!chip->model_data) {
  573. err = -ENOMEM;
  574. goto err_pci_regions;
  575. }
  576. }
  577. pci_set_master(pci);
  578. snd_card_set_dev(card, &pci->dev);
  579. card->private_free = oxygen_card_free;
  580. configure_pcie_bridge(pci);
  581. oxygen_init(chip);
  582. chip->model.init(chip);
  583. err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
  584. DRIVER, chip);
  585. if (err < 0) {
  586. snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq);
  587. goto err_card;
  588. }
  589. chip->irq = pci->irq;
  590. strcpy(card->driver, chip->model.chip);
  591. strcpy(card->shortname, chip->model.shortname);
  592. sprintf(card->longname, "%s (rev %u) at %#lx, irq %i",
  593. chip->model.longname, chip->revision, chip->addr, chip->irq);
  594. strcpy(card->mixername, chip->model.chip);
  595. snd_component_add(card, chip->model.chip);
  596. err = oxygen_pcm_init(chip);
  597. if (err < 0)
  598. goto err_card;
  599. err = oxygen_mixer_init(chip);
  600. if (err < 0)
  601. goto err_card;
  602. if (chip->model.device_config & (MIDI_OUTPUT | MIDI_INPUT)) {
  603. unsigned int info_flags = MPU401_INFO_INTEGRATED;
  604. if (chip->model.device_config & MIDI_OUTPUT)
  605. info_flags |= MPU401_INFO_OUTPUT;
  606. if (chip->model.device_config & MIDI_INPUT)
  607. info_flags |= MPU401_INFO_INPUT;
  608. err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
  609. chip->addr + OXYGEN_MPU401,
  610. info_flags, 0, 0,
  611. &chip->midi);
  612. if (err < 0)
  613. goto err_card;
  614. }
  615. oxygen_proc_init(chip);
  616. spin_lock_irq(&chip->reg_lock);
  617. if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
  618. chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
  619. if (chip->has_ac97_0 | chip->has_ac97_1)
  620. chip->interrupt_mask |= OXYGEN_INT_AC97;
  621. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  622. spin_unlock_irq(&chip->reg_lock);
  623. err = snd_card_register(card);
  624. if (err < 0)
  625. goto err_card;
  626. pci_set_drvdata(pci, card);
  627. return 0;
  628. err_pci_regions:
  629. pci_release_regions(pci);
  630. err_pci_enable:
  631. pci_disable_device(pci);
  632. err_card:
  633. snd_card_free(card);
  634. return err;
  635. }
  636. EXPORT_SYMBOL(oxygen_pci_probe);
  637. void oxygen_pci_remove(struct pci_dev *pci)
  638. {
  639. snd_card_free(pci_get_drvdata(pci));
  640. pci_set_drvdata(pci, NULL);
  641. }
  642. EXPORT_SYMBOL(oxygen_pci_remove);
  643. #ifdef CONFIG_PM
  644. int oxygen_pci_suspend(struct pci_dev *pci, pm_message_t state)
  645. {
  646. struct snd_card *card = pci_get_drvdata(pci);
  647. struct oxygen *chip = card->private_data;
  648. unsigned int i, saved_interrupt_mask;
  649. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  650. for (i = 0; i < PCM_COUNT; ++i)
  651. if (chip->streams[i])
  652. snd_pcm_suspend(chip->streams[i]);
  653. if (chip->model.suspend)
  654. chip->model.suspend(chip);
  655. spin_lock_irq(&chip->reg_lock);
  656. saved_interrupt_mask = chip->interrupt_mask;
  657. chip->interrupt_mask = 0;
  658. oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
  659. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
  660. spin_unlock_irq(&chip->reg_lock);
  661. synchronize_irq(chip->irq);
  662. flush_scheduled_work();
  663. chip->interrupt_mask = saved_interrupt_mask;
  664. pci_disable_device(pci);
  665. pci_save_state(pci);
  666. pci_set_power_state(pci, pci_choose_state(pci, state));
  667. return 0;
  668. }
  669. EXPORT_SYMBOL(oxygen_pci_suspend);
  670. static const u32 registers_to_restore[OXYGEN_IO_SIZE / 32] = {
  671. 0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff,
  672. 0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000
  673. };
  674. static const u32 ac97_registers_to_restore[2][0x40 / 32] = {
  675. { 0x18284fa2, 0x03060000 },
  676. { 0x00007fa6, 0x00200000 }
  677. };
  678. static inline int is_bit_set(const u32 *bitmap, unsigned int bit)
  679. {
  680. return bitmap[bit / 32] & (1 << (bit & 31));
  681. }
  682. static void oxygen_restore_ac97(struct oxygen *chip, unsigned int codec)
  683. {
  684. unsigned int i;
  685. oxygen_write_ac97(chip, codec, AC97_RESET, 0);
  686. msleep(1);
  687. for (i = 1; i < 0x40; ++i)
  688. if (is_bit_set(ac97_registers_to_restore[codec], i))
  689. oxygen_write_ac97(chip, codec, i * 2,
  690. chip->saved_ac97_registers[codec][i]);
  691. }
  692. int oxygen_pci_resume(struct pci_dev *pci)
  693. {
  694. struct snd_card *card = pci_get_drvdata(pci);
  695. struct oxygen *chip = card->private_data;
  696. unsigned int i;
  697. pci_set_power_state(pci, PCI_D0);
  698. pci_restore_state(pci);
  699. if (pci_enable_device(pci) < 0) {
  700. snd_printk(KERN_ERR "cannot reenable device");
  701. snd_card_disconnect(card);
  702. return -EIO;
  703. }
  704. pci_set_master(pci);
  705. oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
  706. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
  707. for (i = 0; i < OXYGEN_IO_SIZE; ++i)
  708. if (is_bit_set(registers_to_restore, i))
  709. oxygen_write8(chip, i, chip->saved_registers._8[i]);
  710. if (chip->has_ac97_0)
  711. oxygen_restore_ac97(chip, 0);
  712. if (chip->has_ac97_1)
  713. oxygen_restore_ac97(chip, 1);
  714. if (chip->model.resume)
  715. chip->model.resume(chip);
  716. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  717. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  718. return 0;
  719. }
  720. EXPORT_SYMBOL(oxygen_pci_resume);
  721. #endif /* CONFIG_PM */
  722. void oxygen_pci_shutdown(struct pci_dev *pci)
  723. {
  724. struct snd_card *card = pci_get_drvdata(pci);
  725. struct oxygen *chip = card->private_data;
  726. oxygen_shutdown(chip);
  727. chip->model.cleanup(chip);
  728. }
  729. EXPORT_SYMBOL(oxygen_pci_shutdown);