oxygen_pcm.c 21 KB

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