oxygen_pcm.c 20 KB

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