oxygen_lib.c 25 KB

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