oxygen_pcm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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;
  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. unsigned int oxygen_default_i2s_mclk(struct oxygen *chip,
  255. unsigned int channel,
  256. struct snd_pcm_hw_params *hw_params)
  257. {
  258. if (params_rate(hw_params) <= 96000)
  259. return OXYGEN_I2S_MCLK_256;
  260. else
  261. return OXYGEN_I2S_MCLK_128;
  262. }
  263. EXPORT_SYMBOL(oxygen_default_i2s_mclk);
  264. static unsigned int oxygen_i2s_bits(struct snd_pcm_hw_params *hw_params)
  265. {
  266. if (params_format(hw_params) == SNDRV_PCM_FORMAT_S32_LE)
  267. return OXYGEN_I2S_BITS_24;
  268. else
  269. return OXYGEN_I2S_BITS_16;
  270. }
  271. static unsigned int oxygen_play_channels(struct snd_pcm_hw_params *hw_params)
  272. {
  273. switch (params_channels(hw_params)) {
  274. default: /* 2 */
  275. return OXYGEN_PLAY_CHANNELS_2;
  276. case 4:
  277. return OXYGEN_PLAY_CHANNELS_4;
  278. case 6:
  279. return OXYGEN_PLAY_CHANNELS_6;
  280. case 8:
  281. return OXYGEN_PLAY_CHANNELS_8;
  282. }
  283. }
  284. static const unsigned int channel_base_registers[PCM_COUNT] = {
  285. [PCM_A] = OXYGEN_DMA_A_ADDRESS,
  286. [PCM_B] = OXYGEN_DMA_B_ADDRESS,
  287. [PCM_C] = OXYGEN_DMA_C_ADDRESS,
  288. [PCM_SPDIF] = OXYGEN_DMA_SPDIF_ADDRESS,
  289. [PCM_MULTICH] = OXYGEN_DMA_MULTICH_ADDRESS,
  290. [PCM_AC97] = OXYGEN_DMA_AC97_ADDRESS,
  291. };
  292. static int oxygen_hw_params(struct snd_pcm_substream *substream,
  293. struct snd_pcm_hw_params *hw_params)
  294. {
  295. struct oxygen *chip = snd_pcm_substream_chip(substream);
  296. unsigned int channel = oxygen_substream_channel(substream);
  297. int err;
  298. err = snd_pcm_lib_malloc_pages(substream,
  299. params_buffer_bytes(hw_params));
  300. if (err < 0)
  301. return err;
  302. oxygen_write32(chip, channel_base_registers[channel],
  303. (u32)substream->runtime->dma_addr);
  304. if (channel == PCM_MULTICH) {
  305. oxygen_write32(chip, OXYGEN_DMA_MULTICH_COUNT,
  306. params_buffer_bytes(hw_params) / 4 - 1);
  307. oxygen_write32(chip, OXYGEN_DMA_MULTICH_TCOUNT,
  308. params_period_bytes(hw_params) / 4 - 1);
  309. } else {
  310. oxygen_write16(chip, channel_base_registers[channel] + 4,
  311. params_buffer_bytes(hw_params) / 4 - 1);
  312. oxygen_write16(chip, channel_base_registers[channel] + 6,
  313. params_period_bytes(hw_params) / 4 - 1);
  314. }
  315. return 0;
  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.get_i2s_mclk(chip, PCM_A, hw_params) |
  332. chip->model.adc_i2s_format |
  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.get_i2s_mclk(chip, PCM_B,
  363. hw_params) |
  364. chip->model.adc_i2s_format |
  365. oxygen_i2s_bits(hw_params),
  366. OXYGEN_I2S_RATE_MASK |
  367. OXYGEN_I2S_FORMAT_MASK |
  368. OXYGEN_I2S_MCLK_MASK |
  369. OXYGEN_I2S_BITS_MASK);
  370. spin_unlock_irq(&chip->reg_lock);
  371. if (!is_ac97) {
  372. mutex_lock(&chip->mutex);
  373. chip->model.set_adc_params(chip, hw_params);
  374. mutex_unlock(&chip->mutex);
  375. }
  376. return 0;
  377. }
  378. static int oxygen_rec_c_hw_params(struct snd_pcm_substream *substream,
  379. struct snd_pcm_hw_params *hw_params)
  380. {
  381. struct oxygen *chip = snd_pcm_substream_chip(substream);
  382. int err;
  383. err = oxygen_hw_params(substream, hw_params);
  384. if (err < 0)
  385. return err;
  386. spin_lock_irq(&chip->reg_lock);
  387. oxygen_write8_masked(chip, OXYGEN_REC_FORMAT,
  388. oxygen_format(hw_params) << OXYGEN_REC_FORMAT_C_SHIFT,
  389. OXYGEN_REC_FORMAT_C_MASK);
  390. spin_unlock_irq(&chip->reg_lock);
  391. return 0;
  392. }
  393. static int oxygen_spdif_hw_params(struct snd_pcm_substream *substream,
  394. struct snd_pcm_hw_params *hw_params)
  395. {
  396. struct oxygen *chip = snd_pcm_substream_chip(substream);
  397. int err;
  398. err = oxygen_hw_params(substream, hw_params);
  399. if (err < 0)
  400. return err;
  401. mutex_lock(&chip->mutex);
  402. spin_lock_irq(&chip->reg_lock);
  403. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  404. OXYGEN_SPDIF_OUT_ENABLE);
  405. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  406. oxygen_format(hw_params) << OXYGEN_SPDIF_FORMAT_SHIFT,
  407. OXYGEN_SPDIF_FORMAT_MASK);
  408. oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
  409. oxygen_rate(hw_params) << OXYGEN_SPDIF_OUT_RATE_SHIFT,
  410. OXYGEN_SPDIF_OUT_RATE_MASK);
  411. oxygen_update_spdif_source(chip);
  412. spin_unlock_irq(&chip->reg_lock);
  413. mutex_unlock(&chip->mutex);
  414. return 0;
  415. }
  416. static int oxygen_multich_hw_params(struct snd_pcm_substream *substream,
  417. struct snd_pcm_hw_params *hw_params)
  418. {
  419. struct oxygen *chip = snd_pcm_substream_chip(substream);
  420. int err;
  421. err = oxygen_hw_params(substream, hw_params);
  422. if (err < 0)
  423. return err;
  424. mutex_lock(&chip->mutex);
  425. spin_lock_irq(&chip->reg_lock);
  426. oxygen_write8_masked(chip, OXYGEN_PLAY_CHANNELS,
  427. oxygen_play_channels(hw_params),
  428. OXYGEN_PLAY_CHANNELS_MASK);
  429. oxygen_write8_masked(chip, OXYGEN_PLAY_FORMAT,
  430. oxygen_format(hw_params) << OXYGEN_MULTICH_FORMAT_SHIFT,
  431. OXYGEN_MULTICH_FORMAT_MASK);
  432. oxygen_write16_masked(chip, OXYGEN_I2S_MULTICH_FORMAT,
  433. oxygen_rate(hw_params) |
  434. chip->model.dac_i2s_format |
  435. chip->model.get_i2s_mclk(chip, PCM_MULTICH,
  436. hw_params) |
  437. oxygen_i2s_bits(hw_params),
  438. OXYGEN_I2S_RATE_MASK |
  439. OXYGEN_I2S_FORMAT_MASK |
  440. OXYGEN_I2S_MCLK_MASK |
  441. OXYGEN_I2S_BITS_MASK);
  442. oxygen_update_spdif_source(chip);
  443. spin_unlock_irq(&chip->reg_lock);
  444. chip->model.set_dac_params(chip, hw_params);
  445. oxygen_update_dac_routing(chip);
  446. mutex_unlock(&chip->mutex);
  447. return 0;
  448. }
  449. static int oxygen_hw_free(struct snd_pcm_substream *substream)
  450. {
  451. struct oxygen *chip = snd_pcm_substream_chip(substream);
  452. unsigned int channel = oxygen_substream_channel(substream);
  453. unsigned int channel_mask = 1 << channel;
  454. spin_lock_irq(&chip->reg_lock);
  455. chip->interrupt_mask &= ~channel_mask;
  456. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  457. oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  458. oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  459. spin_unlock_irq(&chip->reg_lock);
  460. return snd_pcm_lib_free_pages(substream);
  461. }
  462. static int oxygen_spdif_hw_free(struct snd_pcm_substream *substream)
  463. {
  464. struct oxygen *chip = snd_pcm_substream_chip(substream);
  465. spin_lock_irq(&chip->reg_lock);
  466. oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
  467. OXYGEN_SPDIF_OUT_ENABLE);
  468. spin_unlock_irq(&chip->reg_lock);
  469. return oxygen_hw_free(substream);
  470. }
  471. static int oxygen_prepare(struct snd_pcm_substream *substream)
  472. {
  473. struct oxygen *chip = snd_pcm_substream_chip(substream);
  474. unsigned int channel = oxygen_substream_channel(substream);
  475. unsigned int channel_mask = 1 << channel;
  476. spin_lock_irq(&chip->reg_lock);
  477. oxygen_set_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  478. oxygen_clear_bits8(chip, OXYGEN_DMA_FLUSH, channel_mask);
  479. if (substream->runtime->no_period_wakeup)
  480. chip->interrupt_mask &= ~channel_mask;
  481. else
  482. chip->interrupt_mask |= channel_mask;
  483. oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
  484. spin_unlock_irq(&chip->reg_lock);
  485. return 0;
  486. }
  487. static int oxygen_trigger(struct snd_pcm_substream *substream, int cmd)
  488. {
  489. struct oxygen *chip = snd_pcm_substream_chip(substream);
  490. struct snd_pcm_substream *s;
  491. unsigned int mask = 0;
  492. int pausing;
  493. switch (cmd) {
  494. case SNDRV_PCM_TRIGGER_STOP:
  495. case SNDRV_PCM_TRIGGER_START:
  496. case SNDRV_PCM_TRIGGER_SUSPEND:
  497. pausing = 0;
  498. break;
  499. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  500. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  501. pausing = 1;
  502. break;
  503. default:
  504. return -EINVAL;
  505. }
  506. snd_pcm_group_for_each_entry(s, substream) {
  507. if (snd_pcm_substream_chip(s) == chip) {
  508. mask |= 1 << oxygen_substream_channel(s);
  509. snd_pcm_trigger_done(s, substream);
  510. }
  511. }
  512. spin_lock(&chip->reg_lock);
  513. if (!pausing) {
  514. if (cmd == SNDRV_PCM_TRIGGER_START)
  515. chip->pcm_running |= mask;
  516. else
  517. chip->pcm_running &= ~mask;
  518. oxygen_write8(chip, OXYGEN_DMA_STATUS, chip->pcm_running);
  519. } else {
  520. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
  521. oxygen_set_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  522. else
  523. oxygen_clear_bits8(chip, OXYGEN_DMA_PAUSE, mask);
  524. }
  525. spin_unlock(&chip->reg_lock);
  526. return 0;
  527. }
  528. static snd_pcm_uframes_t oxygen_pointer(struct snd_pcm_substream *substream)
  529. {
  530. struct oxygen *chip = snd_pcm_substream_chip(substream);
  531. struct snd_pcm_runtime *runtime = substream->runtime;
  532. unsigned int channel = oxygen_substream_channel(substream);
  533. u32 curr_addr;
  534. /* no spinlock, this read should be atomic */
  535. curr_addr = oxygen_read32(chip, channel_base_registers[channel]);
  536. return bytes_to_frames(runtime, curr_addr - (u32)runtime->dma_addr);
  537. }
  538. static struct snd_pcm_ops oxygen_rec_a_ops = {
  539. .open = oxygen_rec_a_open,
  540. .close = oxygen_close,
  541. .ioctl = snd_pcm_lib_ioctl,
  542. .hw_params = oxygen_rec_a_hw_params,
  543. .hw_free = oxygen_hw_free,
  544. .prepare = oxygen_prepare,
  545. .trigger = oxygen_trigger,
  546. .pointer = oxygen_pointer,
  547. };
  548. static struct snd_pcm_ops oxygen_rec_b_ops = {
  549. .open = oxygen_rec_b_open,
  550. .close = oxygen_close,
  551. .ioctl = snd_pcm_lib_ioctl,
  552. .hw_params = oxygen_rec_b_hw_params,
  553. .hw_free = oxygen_hw_free,
  554. .prepare = oxygen_prepare,
  555. .trigger = oxygen_trigger,
  556. .pointer = oxygen_pointer,
  557. };
  558. static struct snd_pcm_ops oxygen_rec_c_ops = {
  559. .open = oxygen_rec_c_open,
  560. .close = oxygen_close,
  561. .ioctl = snd_pcm_lib_ioctl,
  562. .hw_params = oxygen_rec_c_hw_params,
  563. .hw_free = oxygen_hw_free,
  564. .prepare = oxygen_prepare,
  565. .trigger = oxygen_trigger,
  566. .pointer = oxygen_pointer,
  567. };
  568. static struct snd_pcm_ops oxygen_spdif_ops = {
  569. .open = oxygen_spdif_open,
  570. .close = oxygen_close,
  571. .ioctl = snd_pcm_lib_ioctl,
  572. .hw_params = oxygen_spdif_hw_params,
  573. .hw_free = oxygen_spdif_hw_free,
  574. .prepare = oxygen_prepare,
  575. .trigger = oxygen_trigger,
  576. .pointer = oxygen_pointer,
  577. };
  578. static struct snd_pcm_ops oxygen_multich_ops = {
  579. .open = oxygen_multich_open,
  580. .close = oxygen_close,
  581. .ioctl = snd_pcm_lib_ioctl,
  582. .hw_params = oxygen_multich_hw_params,
  583. .hw_free = oxygen_hw_free,
  584. .prepare = oxygen_prepare,
  585. .trigger = oxygen_trigger,
  586. .pointer = oxygen_pointer,
  587. };
  588. static struct snd_pcm_ops oxygen_ac97_ops = {
  589. .open = oxygen_ac97_open,
  590. .close = oxygen_close,
  591. .ioctl = snd_pcm_lib_ioctl,
  592. .hw_params = oxygen_hw_params,
  593. .hw_free = oxygen_hw_free,
  594. .prepare = oxygen_prepare,
  595. .trigger = oxygen_trigger,
  596. .pointer = oxygen_pointer,
  597. };
  598. static void oxygen_pcm_free(struct snd_pcm *pcm)
  599. {
  600. snd_pcm_lib_preallocate_free_for_all(pcm);
  601. }
  602. int oxygen_pcm_init(struct oxygen *chip)
  603. {
  604. struct snd_pcm *pcm;
  605. int outs, ins;
  606. int err;
  607. outs = !!(chip->model.device_config & PLAYBACK_0_TO_I2S);
  608. ins = !!(chip->model.device_config & (CAPTURE_0_FROM_I2S_1 |
  609. CAPTURE_0_FROM_I2S_2));
  610. if (outs | ins) {
  611. err = snd_pcm_new(chip->card, "Multichannel",
  612. 0, outs, ins, &pcm);
  613. if (err < 0)
  614. return err;
  615. if (outs)
  616. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  617. &oxygen_multich_ops);
  618. if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
  619. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  620. &oxygen_rec_a_ops);
  621. else if (chip->model.device_config & CAPTURE_0_FROM_I2S_2)
  622. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  623. &oxygen_rec_b_ops);
  624. pcm->private_data = chip;
  625. pcm->private_free = oxygen_pcm_free;
  626. strcpy(pcm->name, "Multichannel");
  627. if (outs)
  628. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  629. SNDRV_DMA_TYPE_DEV,
  630. snd_dma_pci_data(chip->pci),
  631. DEFAULT_BUFFER_BYTES_MULTICH,
  632. BUFFER_BYTES_MAX_MULTICH);
  633. if (ins)
  634. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  635. SNDRV_DMA_TYPE_DEV,
  636. snd_dma_pci_data(chip->pci),
  637. DEFAULT_BUFFER_BYTES,
  638. BUFFER_BYTES_MAX);
  639. }
  640. outs = !!(chip->model.device_config & PLAYBACK_1_TO_SPDIF);
  641. ins = !!(chip->model.device_config & CAPTURE_1_FROM_SPDIF);
  642. if (outs | ins) {
  643. err = snd_pcm_new(chip->card, "Digital", 1, outs, ins, &pcm);
  644. if (err < 0)
  645. return err;
  646. if (outs)
  647. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  648. &oxygen_spdif_ops);
  649. if (ins)
  650. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  651. &oxygen_rec_c_ops);
  652. pcm->private_data = chip;
  653. pcm->private_free = oxygen_pcm_free;
  654. strcpy(pcm->name, "Digital");
  655. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  656. snd_dma_pci_data(chip->pci),
  657. DEFAULT_BUFFER_BYTES,
  658. BUFFER_BYTES_MAX);
  659. }
  660. if (chip->has_ac97_1) {
  661. outs = !!(chip->model.device_config & PLAYBACK_2_TO_AC97_1);
  662. ins = !!(chip->model.device_config & CAPTURE_2_FROM_AC97_1);
  663. } else {
  664. outs = 0;
  665. ins = !!(chip->model.device_config & CAPTURE_2_FROM_I2S_2);
  666. }
  667. if (outs | ins) {
  668. err = snd_pcm_new(chip->card, outs ? "AC97" : "Analog2",
  669. 2, outs, ins, &pcm);
  670. if (err < 0)
  671. return err;
  672. if (outs) {
  673. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  674. &oxygen_ac97_ops);
  675. oxygen_write8_masked(chip, OXYGEN_REC_ROUTING,
  676. OXYGEN_REC_B_ROUTE_AC97_1,
  677. OXYGEN_REC_B_ROUTE_MASK);
  678. }
  679. if (ins)
  680. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  681. &oxygen_rec_b_ops);
  682. pcm->private_data = chip;
  683. pcm->private_free = oxygen_pcm_free;
  684. strcpy(pcm->name, outs ? "Front Panel" : "Analog 2");
  685. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  686. snd_dma_pci_data(chip->pci),
  687. DEFAULT_BUFFER_BYTES,
  688. BUFFER_BYTES_MAX);
  689. }
  690. return 0;
  691. }