oxygen_pcm.c 22 KB

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