oxygen_pcm.c 22 KB

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