oxygen_pcm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. if (channel == PCM_MULTICH) {
  163. err = snd_pcm_hw_constraint_minmax
  164. (runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 0, 8192000);
  165. if (err < 0)
  166. return err;
  167. }
  168. snd_pcm_set_sync(substream);
  169. chip->streams[channel] = substream;
  170. mutex_lock(&chip->mutex);
  171. chip->pcm_active |= 1 << channel;
  172. if (channel == PCM_SPDIF) {
  173. chip->spdif_pcm_bits = chip->spdif_bits;
  174. chip->controls[CONTROL_SPDIF_PCM]->vd[0].access &=
  175. ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  176. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  177. SNDRV_CTL_EVENT_MASK_INFO,
  178. &chip->controls[CONTROL_SPDIF_PCM]->id);
  179. }
  180. mutex_unlock(&chip->mutex);
  181. return 0;
  182. }
  183. static int oxygen_rec_a_open(struct snd_pcm_substream *substream)
  184. {
  185. return oxygen_open(substream, PCM_A);
  186. }
  187. static int oxygen_rec_b_open(struct snd_pcm_substream *substream)
  188. {
  189. return oxygen_open(substream, PCM_B);
  190. }
  191. static int oxygen_rec_c_open(struct snd_pcm_substream *substream)
  192. {
  193. return oxygen_open(substream, PCM_C);
  194. }
  195. static int oxygen_spdif_open(struct snd_pcm_substream *substream)
  196. {
  197. return oxygen_open(substream, PCM_SPDIF);
  198. }
  199. static int oxygen_multich_open(struct snd_pcm_substream *substream)
  200. {
  201. return oxygen_open(substream, PCM_MULTICH);
  202. }
  203. static int oxygen_ac97_open(struct snd_pcm_substream *substream)
  204. {
  205. return oxygen_open(substream, PCM_AC97);
  206. }
  207. static int oxygen_close(struct snd_pcm_substream *substream)
  208. {
  209. struct oxygen *chip = snd_pcm_substream_chip(substream);
  210. unsigned int channel = oxygen_substream_channel(substream);
  211. mutex_lock(&chip->mutex);
  212. chip->pcm_active &= ~(1 << channel);
  213. if (channel == PCM_SPDIF) {
  214. chip->controls[CONTROL_SPDIF_PCM]->vd[0].access |=
  215. SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  216. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
  217. SNDRV_CTL_EVENT_MASK_INFO,
  218. &chip->controls[CONTROL_SPDIF_PCM]->id);
  219. }
  220. if (channel == PCM_SPDIF || channel == PCM_MULTICH)
  221. oxygen_update_spdif_source(chip);
  222. mutex_unlock(&chip->mutex);
  223. chip->streams[channel] = NULL;
  224. return 0;
  225. }
  226. static unsigned int oxygen_format(struct snd_pcm_hw_params *hw_params)
  227. {
  228. if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
  229. return OXYGEN_FORMAT_24;
  230. else
  231. return OXYGEN_FORMAT_16;
  232. }
  233. static unsigned int oxygen_rate(struct snd_pcm_hw_params *hw_params)
  234. {
  235. switch (params_rate(hw_params)) {
  236. case 32000:
  237. return OXYGEN_RATE_32000;
  238. case 44100:
  239. return OXYGEN_RATE_44100;
  240. default: /* 48000 */
  241. return OXYGEN_RATE_48000;
  242. case 64000:
  243. return OXYGEN_RATE_64000;
  244. case 88200:
  245. return OXYGEN_RATE_88200;
  246. case 96000:
  247. return OXYGEN_RATE_96000;
  248. case 176400:
  249. return OXYGEN_RATE_176400;
  250. case 192000:
  251. return OXYGEN_RATE_192000;
  252. }
  253. }
  254. static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
  255. {
  256. if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
  257. return OXYGEN_I2S_BITS_24;
  258. else
  259. return OXYGEN_I2S_BITS_16;
  260. }
  261. static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
  262. {
  263. switch (params_channels(hw_params)) {
  264. default: /* 2 */
  265. return OXYGEN_PLAY_CHANNELS_2;
  266. case 4:
  267. return OXYGEN_PLAY_CHANNELS_4;
  268. case 6:
  269. return OXYGEN_PLAY_CHANNELS_6;
  270. case 8:
  271. return OXYGEN_PLAY_CHANNELS_8;
  272. }
  273. }
  274. static const unsigned int channel_base_registers[PCM_COUNT] = {
  275. [PCM_A] = OXYGEN_DMA_A_ADDRESS,
  276. [PCM_B] = OXYGEN_DMA_B_ADDRESS,
  277. [PCM_C] = OXYGEN_DMA_C_ADDRESS,
  278. [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
  279. [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
  280. [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
  281. };
  282. static int oxygen_hw_params(struct snd_pcm_substream *substream,
  283. struct snd_pcm_hw_params *hw_params)
  284. {
  285. struct oxygen *chip = snd_pcm_substream_chip(substream);
  286. unsigned int channel = oxygen_substream_channel(substream);
  287. int err;
  288. err = snd_pcm_lib_malloc_pages(substream,
  289. params_buffer_bytes(hw_params));
  290. if (err < 0)
  291. return err;
  292. oxygen_write32(chip, channel_base_registers[channel],
  293. (u32)substream->runtime->dma_addr);
  294. if (channel == PCM_MULTICH) {
  295. oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
  296. params_buffer_bytes(hw_params) / 4 - 1);
  297. oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
  298. params_period_bytes(hw_params) / 4 - 1);
  299. } else {
  300. oxygen_write16(chip, channel_base_registers[channel] + 4,
  301. params_buffer_bytes(hw_params) / 4 - 1);
  302. oxygen_write16(chip, channel_base_registers[channel] + 6,
  303. params_period_bytes(hw_params) / 4 - 1);
  304. }
  305. return 0;
  306. }
  307. static u16 get_mclk(struct oxygen *chip, unsigned int channel,
  308. struct snd_pcm_hw_params *params)
  309. {
  310. unsigned int mclks, shift;
  311. if (channel == PCM_MULTICH)
  312. mclks = chip->model.dac_mclks;
  313. else
  314. mclks = chip->model.adc_mclks;
  315. if (params_rate(params) <= 48000)
  316. shift = 0;
  317. else if (params_rate(params) <= 96000)
  318. shift = 2;
  319. else
  320. shift = 4;
  321. return OXYGEN_I2S_MCLK(mclks >> shift);
  322. }
  323. static int oxygen_rec_a_hw_params(struct snd_pcm_substream *substream,
  324. struct snd_pcm_hw_params *hw_params)
  325. {
  326. struct oxygen *chip = snd_pcm_substream_chip(substream);
  327. int err;
  328. err = oxygen_hw_params(substream, hw_params);
  329. if (err < 0)
  330. return err;
  331. spin_lock_irq(&chip->reg_lock);
  332. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  333. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_A_SHIFT,
  334. OXYGEN_REC_FORMAT_A_MASK);
  335. oxygen_write16_masked(chip, OXYGEN_I2S_A_FORMAT,
  336. oxygen_rate(hw_params) |
  337. chip->model.adc_i2s_format |
  338. get_mclk(chip, PCM_A, hw_params) |
  339. oxygen_i2s_bits(hw_params),
  340. OXYGEN_I2S_RATE_MASK |
  341. OXYGEN_I2S_FORMAT_MASK |
  342. OXYGEN_I2S_MCLK_MASK |
  343. OXYGEN_I2S_BITS_MASK);
  344. spin_unlock_irq(&chip->reg_lock);
  345. mutex_lock(&chip->mutex);
  346. chip->model.set_adc_params(chip, hw_params);
  347. mutex_unlock(&chip->mutex);
  348. return 0;
  349. }
  350. static int oxygen_rec_b_hw_params(struct snd_pcm_substream *substream,
  351. struct snd_pcm_hw_params *hw_params)
  352. {
  353. struct oxygen *chip = snd_pcm_substream_chip(substream);
  354. int is_ac97;
  355. int err;
  356. err = oxygen_hw_params(substream, hw_params);
  357. if (err < 0)
  358. return err;
  359. is_ac97 = chip->has_ac97_1 &&
  360. (chip->model.device_config & CAPTURE_2_FROM_AC97_1);
  361. spin_lock_irq(&chip->reg_lock);
  362. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  363. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_B_SHIFT,
  364. OXYGEN_REC_FORMAT_B_MASK);
  365. if (!is_ac97)
  366. oxygen_write16_masked(chip, OXYGEN_I2S_B_FORMAT,
  367. oxygen_rate(hw_params) |
  368. chip->model.adc_i2s_format |
  369. get_mclk(chip, PCM_B, hw_params) |
  370. oxygen_i2s_bits(hw_params),
  371. OXYGEN_I2S_RATE_MASK |
  372. OXYGEN_I2S_FORMAT_MASK |
  373. OXYGEN_I2S_MCLK_MASK |
  374. OXYGEN_I2S_BITS_MASK);
  375. spin_unlock_irq(&chip->reg_lock);
  376. if (!is_ac97) {
  377. mutex_lock(&chip->mutex);
  378. chip->model.set_adc_params(chip, hw_params);
  379. mutex_unlock(&chip->mutex);
  380. }
  381. return 0;
  382. }
  383. static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
  384. struct snd_pcm_hw_params *hw_params)
  385. {
  386. struct oxygen *chip = snd_pcm_substream_chip(substream);
  387. int err;
  388. err = oxygen_hw_params(substream, hw_params);
  389. if (err < 0)
  390. return err;
  391. spin_lock_irq(&chip->reg_lock);
  392. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  393. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
  394. OXYGEN_REC_FORMAT_C_MASK);
  395. spin_unlock_irq(&chip->reg_lock);
  396. return 0;
  397. }
  398. static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
  399. struct snd_pcm_hw_params *hw_params)
  400. {
  401. struct oxygen *chip = snd_pcm_substream_chip(substream);
  402. int err;
  403. err = oxygen_hw_params(substream, hw_params);
  404. if (err < 0)
  405. return err;
  406. mutex_lock(&chip->mutex);
  407. spin_lock_irq(&chip->reg_lock);
  408. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  409. OXYGEN_SPDIF_OUT_ENABLE);
  410. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  411. oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
  412. OXYGEN_SPDIF_FORMAT_MASK);
  413. oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
  414. oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
  415. OXYGEN_SPDIF_OUT_RATE_MASK);
  416. oxygen_update_spdif_source(chip);
  417. spin_unlock_irq(&chip->reg_lock);
  418. mutex_unlock(&chip->mutex);
  419. return 0;
  420. }
  421. static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
  422. struct snd_pcm_hw_params *hw_params)
  423. {
  424. struct oxygen *chip = snd_pcm_substream_chip(substream);
  425. int err;
  426. err = oxygen_hw_params(substream, hw_params);
  427. if (err < 0)
  428. return err;
  429. mutex_lock(&chip->mutex);
  430. spin_lock_irq(&chip->reg_lock);
  431. oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
  432. oxygen_play_channels(hw_params),
  433. OXYGEN_PLAY_CHANNELS_MASK);
  434. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  435. oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
  436. OXYGEN_MULTICH_FORMAT_MASK);
  437. oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
  438. oxygen_rate(hw_params) |
  439. chip->model.dac_i2s_format |
  440. get_mclk(chip, PCM_MULTICH, hw_params) |
  441. oxygen_i2s_bits(hw_params),
  442. OXYGEN_I2S_RATE_MASK |
  443. OXYGEN_I2S_FORMAT_MASK |
  444. OXYGEN_I2S_MCLK_MASK |
  445. OXYGEN_I2S_BITS_MASK);
  446. oxygen_update_spdif_source(chip);
  447. spin_unlock_irq(&chip->reg_lock);
  448. chip->model.set_dac_params(chip, hw_params);
  449. oxygen_update_dac_routing(chip);
  450. mutex_unlock(&chip->mutex);
  451. return 0;
  452. }
  453. static int oxygen_hw_free(struct snd_pcm_substream *substream)
  454. {
  455. struct oxygen *chip = snd_pcm_substream_chip(substream);
  456. unsigned int channel = oxygen_substream_channel(substream);
  457. unsigned int channel_mask = 1 << channel;
  458. spin_lock_irq(&chip->reg_lock);
  459. chip->interrupt_mask &= ~channel_mask;
  460. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  461. oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  462. oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  463. spin_unlock_irq(&chip->reg_lock);
  464. return snd_pcm_lib_free_pages(substream);
  465. }
  466. static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
  467. {
  468. struct oxygen *chip = snd_pcm_substream_chip(substream);
  469. spin_lock_irq(&chip->reg_lock);
  470. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  471. OXYGEN_SPDIF_OUT_ENABLE);
  472. spin_unlock_irq(&chip->reg_lock);
  473. return oxygen_hw_free(substream);
  474. }
  475. static int oxygen_prepare(struct snd_pcm_substream *substream)
  476. {
  477. struct oxygen *chip = snd_pcm_substream_chip(substream);
  478. unsigned int channel = oxygen_substream_channel(substream);
  479. unsigned int channel_mask = 1 << channel;
  480. spin_lock_irq(&chip->reg_lock);
  481. oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  482. oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  483. if (substream->runtime->no_period_wakeup)
  484. chip->interrupt_mask &= ~channel_mask;
  485. else
  486. chip->interrupt_mask |= channel_mask;
  487. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  488. spin_unlock_irq(&chip->reg_lock);
  489. return 0;
  490. }
  491. static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
  492. {
  493. struct oxygen *chip = snd_pcm_substream_chip(substream);
  494. struct snd_pcm_substream *s;
  495. unsigned int mask = 0;
  496. int pausing;
  497. switch (cmd) {
  498. case SNDRV_PCM_TRIGGER_STOP:
  499. case SNDRV_PCM_TRIGGER_START:
  500. case SNDRV_PCM_TRIGGER_SUSPEND:
  501. pausing = 0;
  502. break;
  503. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  504. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  505. pausing = 1;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. snd_pcm_group_for_each_entry(s, substream) {
  511. if (snd_pcm_substream_chip(s) == chip) {
  512. mask |= 1 << oxygen_substream_channel(s);
  513. snd_pcm_trigger_done(s, substream);
  514. }
  515. }
  516. spin_lock(&chip->reg_lock);
  517. if (!pausing) {
  518. if (cmd == SNDRV_PCM_TRIGGER_START)
  519. chip->pcm_running |= mask;
  520. else
  521. chip->pcm_running &= ~mask;
  522. oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
  523. } else {
  524. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
  525. oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  526. else
  527. oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  528. }
  529. spin_unlock(&chip->reg_lock);
  530. return 0;
  531. }
  532. static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
  533. {
  534. struct oxygen *chip = snd_pcm_substream_chip(substream);
  535. struct snd_pcm_runtime *runtime = substream->runtime;
  536. unsigned int channel = oxygen_substream_channel(substream);
  537. u32 curr_addr;
  538. /* no spinlock, this read should be atomic */
  539. curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
  540. return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
  541. }
  542. static struct snd_pcm_ops oxygen_rec_a_ops = {
  543. .open = oxygen_rec_a_open,
  544. .close = oxygen_close,
  545. .ioctl = snd_pcm_lib_ioctl,
  546. .hw_params = oxygen_rec_a_hw_params,
  547. .hw_free = oxygen_hw_free,
  548. .prepare = oxygen_prepare,
  549. .trigger = oxygen_trigger,
  550. .pointer = oxygen_pointer,
  551. };
  552. static struct snd_pcm_ops oxygen_rec_b_ops = {
  553. .open = oxygen_rec_b_open,
  554. .close = oxygen_close,
  555. .ioctl = snd_pcm_lib_ioctl,
  556. .hw_params = oxygen_rec_b_hw_params,
  557. .hw_free = oxygen_hw_free,
  558. .prepare = oxygen_prepare,
  559. .trigger = oxygen_trigger,
  560. .pointer = oxygen_pointer,
  561. };
  562. static struct snd_pcm_ops oxygen_rec_c_ops = {
  563. .open = oxygen_rec_c_open,
  564. .close = oxygen_close,
  565. .ioctl = snd_pcm_lib_ioctl,
  566. .hw_params = oxygen_rec_c_hw_params,
  567. .hw_free = oxygen_hw_free,
  568. .prepare = oxygen_prepare,
  569. .trigger = oxygen_trigger,
  570. .pointer = oxygen_pointer,
  571. };
  572. static struct snd_pcm_ops oxygen_spdif_ops = {
  573. .open = oxygen_spdif_open,
  574. .close = oxygen_close,
  575. .ioctl = snd_pcm_lib_ioctl,
  576. .hw_params = oxygen_spdif_hw_params,
  577. .hw_free = oxygen_spdif_hw_free,
  578. .prepare = oxygen_prepare,
  579. .trigger = oxygen_trigger,
  580. .pointer = oxygen_pointer,
  581. };
  582. static struct snd_pcm_ops oxygen_multich_ops = {
  583. .open = oxygen_multich_open,
  584. .close = oxygen_close,
  585. .ioctl = snd_pcm_lib_ioctl,
  586. .hw_params = oxygen_multich_hw_params,
  587. .hw_free = oxygen_hw_free,
  588. .prepare = oxygen_prepare,
  589. .trigger = oxygen_trigger,
  590. .pointer = oxygen_pointer,
  591. };
  592. static struct snd_pcm_ops oxygen_ac97_ops = {
  593. .open = oxygen_ac97_open,
  594. .close = oxygen_close,
  595. .ioctl = snd_pcm_lib_ioctl,
  596. .hw_params = oxygen_hw_params,
  597. .hw_free = oxygen_hw_free,
  598. .prepare = oxygen_prepare,
  599. .trigger = oxygen_trigger,
  600. .pointer = oxygen_pointer,
  601. };
  602. static void oxygen_pcm_free(struct snd_pcm *pcm)
  603. {
  604. snd_pcm_lib_preallocate_free_for_all(pcm);
  605. }
  606. int oxygen_pcm_init(struct oxygen *chip)
  607. {
  608. struct snd_pcm *pcm;
  609. int outs, ins;
  610. int err;
  611. outs = !!(chip->model.device_config & PLAYBACK_0_TO_I2S);
  612. ins = !!(chip->model.device_config & (CAPTURE_0_FROM_I2S_1 |
  613. CAPTURE_0_FROM_I2S_2));
  614. if (outs | ins) {
  615. err = snd_pcm_new(chip->card, "Multichannel",
  616. 0, 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_multich_ops);
  622. if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
  623. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  624. &oxygen_rec_a_ops);
  625. else if (chip->model.device_config & CAPTURE_0_FROM_I2S_2)
  626. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  627. &oxygen_rec_b_ops);
  628. pcm->private_data = chip;
  629. pcm->private_free = oxygen_pcm_free;
  630. strcpy(pcm->name, "Multichannel");
  631. if (outs)
  632. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  633. SNDRV_DMA_TYPE_DEV,
  634. snd_dma_pci_data(chip->pci),
  635. DEFAULT_BUFFER_BYTES_MULTICH,
  636. BUFFER_BYTES_MAX_MULTICH);
  637. if (ins)
  638. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  639. SNDRV_DMA_TYPE_DEV,
  640. snd_dma_pci_data(chip->pci),
  641. DEFAULT_BUFFER_BYTES,
  642. BUFFER_BYTES_MAX);
  643. }
  644. outs = !!(chip->model.device_config & PLAYBACK_1_TO_SPDIF);
  645. ins = !!(chip->model.device_config & CAPTURE_1_FROM_SPDIF);
  646. if (outs | ins) {
  647. err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
  648. if (err < 0)
  649. return err;
  650. if (outs)
  651. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  652. &oxygen_spdif_ops);
  653. if (ins)
  654. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  655. &oxygen_rec_c_ops);
  656. pcm->private_data = chip;
  657. pcm->private_free = oxygen_pcm_free;
  658. strcpy(pcm->name, "Digital");
  659. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  660. snd_dma_pci_data(chip->pci),
  661. DEFAULT_BUFFER_BYTES,
  662. BUFFER_BYTES_MAX);
  663. }
  664. if (chip->has_ac97_1) {
  665. outs = !!(chip->model.device_config & PLAYBACK_2_TO_AC97_1);
  666. ins = !!(chip->model.device_config & CAPTURE_2_FROM_AC97_1);
  667. } else {
  668. outs = 0;
  669. ins = !!(chip->model.device_config & CAPTURE_2_FROM_I2S_2);
  670. }
  671. if (outs | ins) {
  672. err = snd_pcm_new(chip->card, outs ? "AC97" : "Analog2",
  673. 2, outs, ins, &pcm);
  674. if (err < 0)
  675. return err;
  676. if (outs) {
  677. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  678. &oxygen_ac97_ops);
  679. oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
  680. OXYGEN_REC_B_ROUTE_AC97_1,
  681. OXYGEN_REC_B_ROUTE_MASK);
  682. }
  683. if (ins)
  684. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  685. &oxygen_rec_b_ops);
  686. pcm->private_data = chip;
  687. pcm->private_free = oxygen_pcm_free;
  688. strcpy(pcm->name, outs ? "Front Panel" : "Analog 2");
  689. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  690. snd_dma_pci_data(chip->pci),
  691. DEFAULT_BUFFER_BYTES,
  692. BUFFER_BYTES_MAX);
  693. }
  694. return 0;
  695. }