oxygen_pcm.c 23 KB

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