oxygen_pcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * C-Media CMI8788 driver - PCM code
  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/pci.h>
  20. #include <sound/control.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include "oxygen.h"
  25. static const struct snd_pcm_hardware oxygen_stereo_hardware = {
  26. .info = SNDRV_PCM_INFO_MMAP |
  27. SNDRV_PCM_INFO_MMAP_VALID |
  28. SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_PAUSE |
  30. SNDRV_PCM_INFO_SYNC_START,
  31. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  32. SNDRV_PCM_FMTBIT_S32_LE,
  33. .rates = SNDRV_PCM_RATE_32000 |
  34. SNDRV_PCM_RATE_44100 |
  35. SNDRV_PCM_RATE_48000 |
  36. SNDRV_PCM_RATE_64000 |
  37. SNDRV_PCM_RATE_88200 |
  38. SNDRV_PCM_RATE_96000 |
  39. SNDRV_PCM_RATE_176400 |
  40. SNDRV_PCM_RATE_192000,
  41. .rate_min = 32000,
  42. .rate_max = 192000,
  43. .channels_min = 2,
  44. .channels_max = 2,
  45. .buffer_bytes_max = 256 * 1024,
  46. .period_bytes_min = 128,
  47. .period_bytes_max = 128 * 1024,
  48. .periods_min = 2,
  49. .periods_max = 2048,
  50. };
  51. static const struct snd_pcm_hardware oxygen_multichannel_hardware = {
  52. .info = SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_MMAP_VALID |
  54. SNDRV_PCM_INFO_INTERLEAVED |
  55. SNDRV_PCM_INFO_PAUSE |
  56. SNDRV_PCM_INFO_SYNC_START,
  57. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  58. SNDRV_PCM_FMTBIT_S32_LE,
  59. .rates = SNDRV_PCM_RATE_32000 |
  60. SNDRV_PCM_RATE_44100 |
  61. SNDRV_PCM_RATE_48000 |
  62. SNDRV_PCM_RATE_64000 |
  63. SNDRV_PCM_RATE_88200 |
  64. SNDRV_PCM_RATE_96000 |
  65. SNDRV_PCM_RATE_176400 |
  66. SNDRV_PCM_RATE_192000,
  67. .rate_min = 32000,
  68. .rate_max = 192000,
  69. .channels_min = 2,
  70. .channels_max = 8,
  71. .buffer_bytes_max = 2048 * 1024,
  72. .period_bytes_min = 128,
  73. .period_bytes_max = 256 * 1024,
  74. .periods_min = 2,
  75. .periods_max = 16384,
  76. };
  77. static const struct snd_pcm_hardware oxygen_ac97_hardware = {
  78. .info = SNDRV_PCM_INFO_MMAP |
  79. SNDRV_PCM_INFO_MMAP_VALID |
  80. SNDRV_PCM_INFO_INTERLEAVED |
  81. SNDRV_PCM_INFO_PAUSE |
  82. SNDRV_PCM_INFO_SYNC_START,
  83. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  84. .rates = SNDRV_PCM_RATE_48000,
  85. .rate_min = 48000,
  86. .rate_max = 48000,
  87. .channels_min = 2,
  88. .channels_max = 2,
  89. .buffer_bytes_max = 256 * 1024,
  90. .period_bytes_min = 128,
  91. .period_bytes_max = 128 * 1024,
  92. .periods_min = 2,
  93. .periods_max = 2048,
  94. };
  95. static const struct snd_pcm_hardware *const oxygen_hardware[PCM_COUNT] = {
  96. [PCM_A] = &oxygen_stereo_hardware,
  97. [PCM_B] = &oxygen_stereo_hardware,
  98. [PCM_C] = &oxygen_stereo_hardware,
  99. [PCM_SPDIF] = &oxygen_stereo_hardware,
  100. [PCM_MULTICH] = &oxygen_multichannel_hardware,
  101. [PCM_AC97] = &oxygen_ac97_hardware,
  102. };
  103. static inline unsigned int
  104. oxygen_substream_channel(struct snd_pcm_substream *substream)
  105. {
  106. return (unsigned int)(uintptr_t)substream->runtime->private_data;
  107. }
  108. static int oxygen_open(struct snd_pcm_substream *substream,
  109. unsigned int channel)
  110. {
  111. struct oxygen *chip = snd_pcm_substream_chip(substream);
  112. struct snd_pcm_runtime *runtime = substream->runtime;
  113. int err;
  114. runtime->private_data = (void *)(uintptr_t)channel;
  115. runtime->hw = *oxygen_hardware[channel];
  116. switch (channel) {
  117. case PCM_C:
  118. runtime->hw.rates &= ~(SNDRV_PCM_RATE_32000 |
  119. SNDRV_PCM_RATE_64000);
  120. runtime->hw.rate_min = 44100;
  121. break;
  122. case PCM_MULTICH:
  123. runtime->hw.channels_max = chip->model->dac_channels;
  124. break;
  125. }
  126. if (chip->model->pcm_hardware_filter)
  127. chip->model->pcm_hardware_filter(channel, &runtime->hw);
  128. err = snd_pcm_hw_constraint_step(runtime, 0,
  129. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  130. if (err < 0)
  131. return err;
  132. err = snd_pcm_hw_constraint_step(runtime, 0,
  133. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  134. if (err < 0)
  135. return err;
  136. if (runtime->hw.formats & SNDRV_PCM_FMTBIT_S32_LE) {
  137. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  138. if (err < 0)
  139. return err;
  140. }
  141. if (runtime->hw.channels_max > 2) {
  142. err = snd_pcm_hw_constraint_step(runtime, 0,
  143. SNDRV_PCM_HW_PARAM_CHANNELS,
  144. 2);
  145. if (err < 0)
  146. return err;
  147. }
  148. snd_pcm_set_sync(substream);
  149. chip->streams[channel] = substream;
  150. mutex_lock(&chip->mutex);
  151. chip->pcm_active |= 1 << channel;
  152. if (channel == PCM_SPDIF) {
  153. chip->spdif_pcm_bits = chip->spdif_bits;
  154. chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
  155. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  156. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  157. SNDRV_CTL_EVENT_MASK_INFO,
  158. &chip->controls[CONTROL_SPDIF_PCM]->id);
  159. }
  160. mutex_unlock(&chip->mutex);
  161. return 0;
  162. }
  163. static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
  164. {
  165. return oxygen_open(substream, PCM_A);
  166. }
  167. static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
  168. {
  169. return oxygen_open(substream, PCM_B);
  170. }
  171. static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
  172. {
  173. return oxygen_open(substream, PCM_C);
  174. }
  175. static int oxygen_spdif_open(struct snd_pcm_substream *substream)
  176. {
  177. return oxygen_open(substream, PCM_SPDIF);
  178. }
  179. static int oxygen_multich_open(struct snd_pcm_substream *substream)
  180. {
  181. return oxygen_open(substream, PCM_MULTICH);
  182. }
  183. static int oxygen_ac97_open(struct snd_pcm_substream *substream)
  184. {
  185. return oxygen_open(substream, PCM_AC97);
  186. }
  187. static int oxygen_close(struct snd_pcm_substream *substream)
  188. {
  189. struct oxygen *chip = snd_pcm_substream_chip(substream);
  190. unsigned int channel = oxygen_substream_channel(substream);
  191. mutex_lock(&chip->mutex);
  192. chip->pcm_active &= ~(1 << channel);
  193. if (channel == PCM_SPDIF) {
  194. chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
  195. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  196. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  197. SNDRV_CTL_EVENT_MASK_INFO,
  198. &chip->controls[CONTROL_SPDIF_PCM]->id);
  199. }
  200. if (channel == PCM_SPDIF || channel == PCM_MULTICH)
  201. oxygen_update_spdif_source(chip);
  202. mutex_unlock(&chip->mutex);
  203. chip->streams[channel] = NULL;
  204. return 0;
  205. }
  206. static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
  207. {
  208. if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
  209. return OXYGEN_FORMAT_24;
  210. else
  211. return OXYGEN_FORMAT_16;
  212. }
  213. static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
  214. {
  215. switch (params_rate(hw_params)) {
  216. case 32000:
  217. return OXYGEN_RATE_32000;
  218. case 44100:
  219. return OXYGEN_RATE_44100;
  220. default: /* 48000 */
  221. return OXYGEN_RATE_48000;
  222. case 64000:
  223. return OXYGEN_RATE_64000;
  224. case 88200:
  225. return OXYGEN_RATE_88200;
  226. case 96000:
  227. return OXYGEN_RATE_96000;
  228. case 176400:
  229. return OXYGEN_RATE_176400;
  230. case 192000:
  231. return OXYGEN_RATE_192000;
  232. }
  233. }
  234. static unsigned int oxygen_i2s_mclk(struct snd_pcm_hw_params *hw_params)
  235. {
  236. if (params_rate(hw_params) <= 96000)
  237. return OXYGEN_I2S_MCLK_256;
  238. else
  239. return OXYGEN_I2S_MCLK_128;
  240. }
  241. static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
  242. {
  243. if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
  244. return OXYGEN_I2S_BITS_24;
  245. else
  246. return OXYGEN_I2S_BITS_16;
  247. }
  248. static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
  249. {
  250. switch (params_channels(hw_params)) {
  251. default: /* 2 */
  252. return OXYGEN_PLAY_CHANNELS_2;
  253. case 4:
  254. return OXYGEN_PLAY_CHANNELS_4;
  255. case 6:
  256. return OXYGEN_PLAY_CHANNELS_6;
  257. case 8:
  258. return OXYGEN_PLAY_CHANNELS_8;
  259. }
  260. }
  261. static const unsigned int channel_base_registers[PCM_COUNT] = {
  262. [PCM_A] = OXYGEN_DMA_A_ADDRESS,
  263. [PCM_B] = OXYGEN_DMA_B_ADDRESS,
  264. [PCM_C] = OXYGEN_DMA_C_ADDRESS,
  265. [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
  266. [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
  267. [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
  268. };
  269. static int oxygen_hw_params(struct snd_pcm_substream *substream,
  270. struct snd_pcm_hw_params *hw_params)
  271. {
  272. struct oxygen *chip = snd_pcm_substream_chip(substream);
  273. unsigned int channel = oxygen_substream_channel(substream);
  274. int err;
  275. err = snd_pcm_lib_malloc_pages(substream,
  276. params_buffer_bytes(hw_params));
  277. if (err < 0)
  278. return err;
  279. oxygen_write32(chip, channel_base_registers[channel],
  280. (u32)substream->runtime->dma_addr);
  281. if (channel == PCM_MULTICH) {
  282. oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
  283. params_buffer_bytes(hw_params) / 4 - 1);
  284. oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
  285. params_period_bytes(hw_params) / 4 - 1);
  286. } else {
  287. oxygen_write16(chip, channel_base_registers[channel] + 4,
  288. params_buffer_bytes(hw_params) / 4 - 1);
  289. oxygen_write16(chip, channel_base_registers[channel] + 6,
  290. params_period_bytes(hw_params) / 4 - 1);
  291. }
  292. return 0;
  293. }
  294. static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
  295. struct snd_pcm_hw_params *hw_params)
  296. {
  297. struct oxygen *chip = snd_pcm_substream_chip(substream);
  298. int err;
  299. err = oxygen_hw_params(substream, hw_params);
  300. if (err < 0)
  301. return err;
  302. spin_lock_irq(&chip->reg_lock);
  303. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  304. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
  305. OXYGEN_REC_FORMAT_A_MASK);
  306. oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
  307. oxygen_rate(hw_params) |
  308. oxygen_i2s_mclk(hw_params) |
  309. chip->model->adc_i2s_format |
  310. oxygen_i2s_bits(hw_params),
  311. OXYGEN_I2S_RATE_MASK |
  312. OXYGEN_I2S_FORMAT_MASK |
  313. OXYGEN_I2S_MCLK_MASK |
  314. OXYGEN_I2S_BITS_MASK);
  315. spin_unlock_irq(&chip->reg_lock);
  316. mutex_lock(&chip->mutex);
  317. chip->model->set_adc_params(chip, hw_params);
  318. mutex_unlock(&chip->mutex);
  319. return 0;
  320. }
  321. static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
  322. struct snd_pcm_hw_params *hw_params)
  323. {
  324. struct oxygen *chip = snd_pcm_substream_chip(substream);
  325. int err;
  326. err = oxygen_hw_params(substream, hw_params);
  327. if (err < 0)
  328. return err;
  329. spin_lock_irq(&chip->reg_lock);
  330. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  331. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
  332. OXYGEN_REC_FORMAT_B_MASK);
  333. oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
  334. oxygen_rate(hw_params) |
  335. oxygen_i2s_mclk(hw_params) |
  336. chip->model->adc_i2s_format |
  337. oxygen_i2s_bits(hw_params),
  338. OXYGEN_I2S_RATE_MASK |
  339. OXYGEN_I2S_FORMAT_MASK |
  340. OXYGEN_I2S_MCLK_MASK |
  341. OXYGEN_I2S_BITS_MASK);
  342. spin_unlock_irq(&chip->reg_lock);
  343. mutex_lock(&chip->mutex);
  344. chip->model->set_adc_params(chip, hw_params);
  345. mutex_unlock(&chip->mutex);
  346. return 0;
  347. }
  348. static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
  349. struct snd_pcm_hw_params *hw_params)
  350. {
  351. struct oxygen *chip = snd_pcm_substream_chip(substream);
  352. int err;
  353. err = oxygen_hw_params(substream, hw_params);
  354. if (err < 0)
  355. return err;
  356. spin_lock_irq(&chip->reg_lock);
  357. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  358. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
  359. OXYGEN_REC_FORMAT_C_MASK);
  360. spin_unlock_irq(&chip->reg_lock);
  361. return 0;
  362. }
  363. static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
  364. struct snd_pcm_hw_params *hw_params)
  365. {
  366. struct oxygen *chip = snd_pcm_substream_chip(substream);
  367. int err;
  368. err = oxygen_hw_params(substream, hw_params);
  369. if (err < 0)
  370. return err;
  371. spin_lock_irq(&chip->reg_lock);
  372. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  373. OXYGEN_SPDIF_OUT_ENABLE);
  374. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  375. oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
  376. OXYGEN_SPDIF_FORMAT_MASK);
  377. oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
  378. oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
  379. OXYGEN_SPDIF_OUT_RATE_MASK);
  380. oxygen_update_spdif_source(chip);
  381. spin_unlock_irq(&chip->reg_lock);
  382. return 0;
  383. }
  384. static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
  385. struct snd_pcm_hw_params *hw_params)
  386. {
  387. struct oxygen *chip = snd_pcm_substream_chip(substream);
  388. int err;
  389. err = oxygen_hw_params(substream, hw_params);
  390. if (err < 0)
  391. return err;
  392. spin_lock_irq(&chip->reg_lock);
  393. oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
  394. oxygen_play_channels(hw_params),
  395. OXYGEN_PLAY_CHANNELS_MASK);
  396. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  397. oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
  398. OXYGEN_MULTICH_FORMAT_MASK);
  399. oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
  400. oxygen_rate(hw_params) |
  401. chip->model->dac_i2s_format |
  402. oxygen_i2s_bits(hw_params),
  403. OXYGEN_I2S_RATE_MASK |
  404. OXYGEN_I2S_FORMAT_MASK |
  405. OXYGEN_I2S_BITS_MASK);
  406. oxygen_update_dac_routing(chip);
  407. oxygen_update_spdif_source(chip);
  408. spin_unlock_irq(&chip->reg_lock);
  409. mutex_lock(&chip->mutex);
  410. chip->model->set_dac_params(chip, hw_params);
  411. mutex_unlock(&chip->mutex);
  412. return 0;
  413. }
  414. static int oxygen_hw_free(struct snd_pcm_substream *substream)
  415. {
  416. struct oxygen *chip = snd_pcm_substream_chip(substream);
  417. unsigned int channel = oxygen_substream_channel(substream);
  418. spin_lock_irq(&chip->reg_lock);
  419. chip->interrupt_mask &= ~(1 << channel);
  420. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  421. spin_unlock_irq(&chip->reg_lock);
  422. return snd_pcm_lib_free_pages(substream);
  423. }
  424. static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
  425. {
  426. struct oxygen *chip = snd_pcm_substream_chip(substream);
  427. spin_lock_irq(&chip->reg_lock);
  428. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  429. OXYGEN_SPDIF_OUT_ENABLE);
  430. spin_unlock_irq(&chip->reg_lock);
  431. return oxygen_hw_free(substream);
  432. }
  433. static int oxygen_prepare(struct snd_pcm_substream *substream)
  434. {
  435. struct oxygen *chip = snd_pcm_substream_chip(substream);
  436. unsigned int channel = oxygen_substream_channel(substream);
  437. unsigned int channel_mask = 1 << channel;
  438. spin_lock_irq(&chip->reg_lock);
  439. oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  440. oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  441. chip->interrupt_mask |= channel_mask;
  442. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  443. spin_unlock_irq(&chip->reg_lock);
  444. return 0;
  445. }
  446. static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
  447. {
  448. struct oxygen *chip = snd_pcm_substream_chip(substream);
  449. struct snd_pcm_substream *s;
  450. unsigned int mask = 0;
  451. int pausing;
  452. switch (cmd) {
  453. case SNDRV_PCM_TRIGGER_STOP:
  454. case SNDRV_PCM_TRIGGER_START:
  455. pausing = 0;
  456. break;
  457. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  458. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  459. pausing = 1;
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. snd_pcm_group_for_each_entry(s, substream) {
  465. if (snd_pcm_substream_chip(s) == chip) {
  466. mask |= 1 << oxygen_substream_channel(s);
  467. snd_pcm_trigger_done(s, substream);
  468. }
  469. }
  470. spin_lock(&chip->reg_lock);
  471. if (!pausing) {
  472. if (cmd == SNDRV_PCM_TRIGGER_START)
  473. chip->pcm_running |= mask;
  474. else
  475. chip->pcm_running &= ~mask;
  476. oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
  477. } else {
  478. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
  479. oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  480. else
  481. oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  482. }
  483. spin_unlock(&chip->reg_lock);
  484. return 0;
  485. }
  486. static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
  487. {
  488. struct oxygen *chip = snd_pcm_substream_chip(substream);
  489. struct snd_pcm_runtime *runtime = substream->runtime;
  490. unsigned int channel = oxygen_substream_channel(substream);
  491. u32 curr_addr;
  492. /* no spinlock, this read should be atomic */
  493. curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
  494. return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
  495. }
  496. static struct snd_pcm_ops oxygen_rec_a_ops = {
  497. .open = oxygen_rec_a_open,
  498. .close = oxygen_close,
  499. .ioctl = snd_pcm_lib_ioctl,
  500. .hw_params = oxygen_rec_a_hw_params,
  501. .hw_free = oxygen_hw_free,
  502. .prepare = oxygen_prepare,
  503. .trigger = oxygen_trigger,
  504. .pointer = oxygen_pointer,
  505. };
  506. static struct snd_pcm_ops oxygen_rec_b_ops = {
  507. .open = oxygen_rec_b_open,
  508. .close = oxygen_close,
  509. .ioctl = snd_pcm_lib_ioctl,
  510. .hw_params = oxygen_rec_b_hw_params,
  511. .hw_free = oxygen_hw_free,
  512. .prepare = oxygen_prepare,
  513. .trigger = oxygen_trigger,
  514. .pointer = oxygen_pointer,
  515. };
  516. static struct snd_pcm_ops oxygen_rec_c_ops = {
  517. .open = oxygen_rec_c_open,
  518. .close = oxygen_close,
  519. .ioctl = snd_pcm_lib_ioctl,
  520. .hw_params = oxygen_rec_c_hw_params,
  521. .hw_free = oxygen_hw_free,
  522. .prepare = oxygen_prepare,
  523. .trigger = oxygen_trigger,
  524. .pointer = oxygen_pointer,
  525. };
  526. static struct snd_pcm_ops oxygen_spdif_ops = {
  527. .open = oxygen_spdif_open,
  528. .close = oxygen_close,
  529. .ioctl = snd_pcm_lib_ioctl,
  530. .hw_params = oxygen_spdif_hw_params,
  531. .hw_free = oxygen_spdif_hw_free,
  532. .prepare = oxygen_prepare,
  533. .trigger = oxygen_trigger,
  534. .pointer = oxygen_pointer,
  535. };
  536. static struct snd_pcm_ops oxygen_multich_ops = {
  537. .open = oxygen_multich_open,
  538. .close = oxygen_close,
  539. .ioctl = snd_pcm_lib_ioctl,
  540. .hw_params = oxygen_multich_hw_params,
  541. .hw_free = oxygen_hw_free,
  542. .prepare = oxygen_prepare,
  543. .trigger = oxygen_trigger,
  544. .pointer = oxygen_pointer,
  545. };
  546. static struct snd_pcm_ops oxygen_ac97_ops = {
  547. .open = oxygen_ac97_open,
  548. .close = oxygen_close,
  549. .ioctl = snd_pcm_lib_ioctl,
  550. .hw_params = oxygen_hw_params,
  551. .hw_free = oxygen_hw_free,
  552. .prepare = oxygen_prepare,
  553. .trigger = oxygen_trigger,
  554. .pointer = oxygen_pointer,
  555. };
  556. static void oxygen_pcm_free(struct snd_pcm *pcm)
  557. {
  558. snd_pcm_lib_preallocate_free_for_all(pcm);
  559. }
  560. int __devinit oxygen_pcm_init(struct oxygen *chip)
  561. {
  562. struct snd_pcm *pcm;
  563. int outs, ins;
  564. int err;
  565. outs = 1; /* OXYGEN_CHANNEL_MULTICH is always used */
  566. ins = !!(chip->model->used_channels & (OXYGEN_CHANNEL_A |
  567. OXYGEN_CHANNEL_B));
  568. err = snd_pcm_new(chip->card, "Analog", 0, outs, ins, &pcm);
  569. if (err < 0)
  570. return err;
  571. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &oxygen_multich_ops);
  572. if (chip->model->used_channels & OXYGEN_CHANNEL_A)
  573. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  574. &oxygen_rec_a_ops);
  575. else if (chip->model->used_channels & OXYGEN_CHANNEL_B)
  576. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  577. &oxygen_rec_b_ops);
  578. pcm->private_data = chip;
  579. pcm->private_free = oxygen_pcm_free;
  580. strcpy(pcm->name, "Analog");
  581. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  582. SNDRV_DMA_TYPE_DEV,
  583. snd_dma_pci_data(chip->pci),
  584. 512 * 1024, 2048 * 1024);
  585. if (ins)
  586. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  587. SNDRV_DMA_TYPE_DEV,
  588. snd_dma_pci_data(chip->pci),
  589. 128 * 1024, 256 * 1024);
  590. outs = !!(chip->model->used_channels & OXYGEN_CHANNEL_SPDIF);
  591. ins = !!(chip->model->used_channels & OXYGEN_CHANNEL_C);
  592. if (outs | ins) {
  593. err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
  594. if (err < 0)
  595. return err;
  596. if (outs)
  597. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  598. &oxygen_spdif_ops);
  599. if (ins)
  600. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  601. &oxygen_rec_c_ops);
  602. pcm->private_data = chip;
  603. pcm->private_free = oxygen_pcm_free;
  604. strcpy(pcm->name, "Digital");
  605. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  606. snd_dma_pci_data(chip->pci),
  607. 128 * 1024, 256 * 1024);
  608. }
  609. outs = chip->has_ac97_1 &&
  610. (chip->model->used_channels & OXYGEN_CHANNEL_AC97);
  611. ins = (chip->model->used_channels & (OXYGEN_CHANNEL_A |
  612. OXYGEN_CHANNEL_B))
  613. == (OXYGEN_CHANNEL_A | OXYGEN_CHANNEL_B);
  614. if (outs | ins) {
  615. err = snd_pcm_new(chip->card, ins ? "Analog2" : "AC97",
  616. 2, outs, ins, &pcm);
  617. if (err < 0)
  618. return err;
  619. if (outs)
  620. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  621. &oxygen_ac97_ops);
  622. if (ins)
  623. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  624. &oxygen_rec_b_ops);
  625. pcm->private_data = chip;
  626. pcm->private_free = oxygen_pcm_free;
  627. strcpy(pcm->name, ins ? "Analog 2" : "Front Panel");
  628. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  629. snd_dma_pci_data(chip->pci),
  630. 128 * 1024, 256 * 1024);
  631. }
  632. return 0;
  633. }