ac97_pcm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Universal interface for Audio Codec '97
  4. *
  5. * For more details look to AC '97 component specification revision 2.2
  6. * by Intel Corporation (http://developer.intel.com) and to datasheets
  7. * for specific codecs.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/mutex.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/control.h>
  32. #include <sound/ac97_codec.h>
  33. #include <sound/asoundef.h>
  34. #include "ac97_id.h"
  35. #include "ac97_local.h"
  36. /*
  37. * PCM support
  38. */
  39. static unsigned char rate_reg_tables[2][4][9] = {
  40. {
  41. /* standard rates */
  42. {
  43. /* 3&4 front, 7&8 rear, 6&9 center/lfe */
  44. AC97_PCM_FRONT_DAC_RATE, /* slot 3 */
  45. AC97_PCM_FRONT_DAC_RATE, /* slot 4 */
  46. 0xff, /* slot 5 */
  47. AC97_PCM_LFE_DAC_RATE, /* slot 6 */
  48. AC97_PCM_SURR_DAC_RATE, /* slot 7 */
  49. AC97_PCM_SURR_DAC_RATE, /* slot 8 */
  50. AC97_PCM_LFE_DAC_RATE, /* slot 9 */
  51. 0xff, /* slot 10 */
  52. 0xff, /* slot 11 */
  53. },
  54. {
  55. /* 7&8 front, 6&9 rear, 10&11 center/lfe */
  56. 0xff, /* slot 3 */
  57. 0xff, /* slot 4 */
  58. 0xff, /* slot 5 */
  59. AC97_PCM_SURR_DAC_RATE, /* slot 6 */
  60. AC97_PCM_FRONT_DAC_RATE, /* slot 7 */
  61. AC97_PCM_FRONT_DAC_RATE, /* slot 8 */
  62. AC97_PCM_SURR_DAC_RATE, /* slot 9 */
  63. AC97_PCM_LFE_DAC_RATE, /* slot 10 */
  64. AC97_PCM_LFE_DAC_RATE, /* slot 11 */
  65. },
  66. {
  67. /* 6&9 front, 10&11 rear, 3&4 center/lfe */
  68. AC97_PCM_LFE_DAC_RATE, /* slot 3 */
  69. AC97_PCM_LFE_DAC_RATE, /* slot 4 */
  70. 0xff, /* slot 5 */
  71. AC97_PCM_FRONT_DAC_RATE, /* slot 6 */
  72. 0xff, /* slot 7 */
  73. 0xff, /* slot 8 */
  74. AC97_PCM_FRONT_DAC_RATE, /* slot 9 */
  75. AC97_PCM_SURR_DAC_RATE, /* slot 10 */
  76. AC97_PCM_SURR_DAC_RATE, /* slot 11 */
  77. },
  78. {
  79. /* 10&11 front, 3&4 rear, 7&8 center/lfe */
  80. AC97_PCM_SURR_DAC_RATE, /* slot 3 */
  81. AC97_PCM_SURR_DAC_RATE, /* slot 4 */
  82. 0xff, /* slot 5 */
  83. 0xff, /* slot 6 */
  84. AC97_PCM_LFE_DAC_RATE, /* slot 7 */
  85. AC97_PCM_LFE_DAC_RATE, /* slot 8 */
  86. 0xff, /* slot 9 */
  87. AC97_PCM_FRONT_DAC_RATE, /* slot 10 */
  88. AC97_PCM_FRONT_DAC_RATE, /* slot 11 */
  89. },
  90. },
  91. {
  92. /* double rates */
  93. {
  94. /* 3&4 front, 7&8 front (t+1) */
  95. AC97_PCM_FRONT_DAC_RATE, /* slot 3 */
  96. AC97_PCM_FRONT_DAC_RATE, /* slot 4 */
  97. 0xff, /* slot 5 */
  98. 0xff, /* slot 6 */
  99. AC97_PCM_FRONT_DAC_RATE, /* slot 7 */
  100. AC97_PCM_FRONT_DAC_RATE, /* slot 8 */
  101. 0xff, /* slot 9 */
  102. 0xff, /* slot 10 */
  103. 0xff, /* slot 11 */
  104. },
  105. {
  106. /* not specified in the specification */
  107. 0xff, /* slot 3 */
  108. 0xff, /* slot 4 */
  109. 0xff, /* slot 5 */
  110. 0xff, /* slot 6 */
  111. 0xff, /* slot 7 */
  112. 0xff, /* slot 8 */
  113. 0xff, /* slot 9 */
  114. 0xff, /* slot 10 */
  115. 0xff, /* slot 11 */
  116. },
  117. {
  118. 0xff, /* slot 3 */
  119. 0xff, /* slot 4 */
  120. 0xff, /* slot 5 */
  121. 0xff, /* slot 6 */
  122. 0xff, /* slot 7 */
  123. 0xff, /* slot 8 */
  124. 0xff, /* slot 9 */
  125. 0xff, /* slot 10 */
  126. 0xff, /* slot 11 */
  127. },
  128. {
  129. 0xff, /* slot 3 */
  130. 0xff, /* slot 4 */
  131. 0xff, /* slot 5 */
  132. 0xff, /* slot 6 */
  133. 0xff, /* slot 7 */
  134. 0xff, /* slot 8 */
  135. 0xff, /* slot 9 */
  136. 0xff, /* slot 10 */
  137. 0xff, /* slot 11 */
  138. }
  139. }};
  140. /* FIXME: more various mappings for ADC? */
  141. static unsigned char rate_cregs[9] = {
  142. AC97_PCM_LR_ADC_RATE, /* 3 */
  143. AC97_PCM_LR_ADC_RATE, /* 4 */
  144. 0xff, /* 5 */
  145. AC97_PCM_MIC_ADC_RATE, /* 6 */
  146. 0xff, /* 7 */
  147. 0xff, /* 8 */
  148. 0xff, /* 9 */
  149. 0xff, /* 10 */
  150. 0xff, /* 11 */
  151. };
  152. static unsigned char get_slot_reg(struct ac97_pcm *pcm, unsigned short cidx,
  153. unsigned short slot, int dbl)
  154. {
  155. if (slot < 3)
  156. return 0xff;
  157. if (slot > 11)
  158. return 0xff;
  159. if (pcm->spdif)
  160. return AC97_SPDIF; /* pseudo register */
  161. if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK)
  162. return rate_reg_tables[dbl][pcm->r[dbl].rate_table[cidx]][slot - 3];
  163. else
  164. return rate_cregs[slot - 3];
  165. }
  166. static int set_spdif_rate(struct snd_ac97 *ac97, unsigned short rate)
  167. {
  168. unsigned short old, bits, reg, mask;
  169. unsigned int sbits;
  170. if (! (ac97->ext_id & AC97_EI_SPDIF))
  171. return -ENODEV;
  172. /* TODO: double rate support */
  173. if (ac97->flags & AC97_CS_SPDIF) {
  174. switch (rate) {
  175. case 48000: bits = 0; break;
  176. case 44100: bits = 1 << AC97_SC_SPSR_SHIFT; break;
  177. default: /* invalid - disable output */
  178. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  179. return -EINVAL;
  180. }
  181. reg = AC97_CSR_SPDIF;
  182. mask = 1 << AC97_SC_SPSR_SHIFT;
  183. } else {
  184. if (ac97->id == AC97_ID_CM9739 && rate != 48000) {
  185. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  186. return -EINVAL;
  187. }
  188. switch (rate) {
  189. case 44100: bits = AC97_SC_SPSR_44K; break;
  190. case 48000: bits = AC97_SC_SPSR_48K; break;
  191. case 32000: bits = AC97_SC_SPSR_32K; break;
  192. default: /* invalid - disable output */
  193. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  194. return -EINVAL;
  195. }
  196. reg = AC97_SPDIF;
  197. mask = AC97_SC_SPSR_MASK;
  198. }
  199. mutex_lock(&ac97->reg_mutex);
  200. old = snd_ac97_read(ac97, reg) & mask;
  201. if (old != bits) {
  202. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  203. snd_ac97_update_bits_nolock(ac97, reg, mask, bits);
  204. /* update the internal spdif bits */
  205. sbits = ac97->spdif_status;
  206. if (sbits & IEC958_AES0_PROFESSIONAL) {
  207. sbits &= ~IEC958_AES0_PRO_FS;
  208. switch (rate) {
  209. case 44100: sbits |= IEC958_AES0_PRO_FS_44100; break;
  210. case 48000: sbits |= IEC958_AES0_PRO_FS_48000; break;
  211. case 32000: sbits |= IEC958_AES0_PRO_FS_32000; break;
  212. }
  213. } else {
  214. sbits &= ~(IEC958_AES3_CON_FS << 24);
  215. switch (rate) {
  216. case 44100: sbits |= IEC958_AES3_CON_FS_44100<<24; break;
  217. case 48000: sbits |= IEC958_AES3_CON_FS_48000<<24; break;
  218. case 32000: sbits |= IEC958_AES3_CON_FS_32000<<24; break;
  219. }
  220. }
  221. ac97->spdif_status = sbits;
  222. }
  223. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF);
  224. mutex_unlock(&ac97->reg_mutex);
  225. return 0;
  226. }
  227. /**
  228. * snd_ac97_set_rate - change the rate of the given input/output.
  229. * @ac97: the ac97 instance
  230. * @reg: the register to change
  231. * @rate: the sample rate to set
  232. *
  233. * Changes the rate of the given input/output on the codec.
  234. * If the codec doesn't support VAR, the rate must be 48000 (except
  235. * for SPDIF).
  236. *
  237. * The valid registers are AC97_PMC_MIC_ADC_RATE,
  238. * AC97_PCM_FRONT_DAC_RATE, AC97_PCM_LR_ADC_RATE.
  239. * AC97_PCM_SURR_DAC_RATE and AC97_PCM_LFE_DAC_RATE are accepted
  240. * if the codec supports them.
  241. * AC97_SPDIF is accepted as a pseudo register to modify the SPDIF
  242. * status bits.
  243. *
  244. * Returns zero if successful, or a negative error code on failure.
  245. */
  246. int snd_ac97_set_rate(struct snd_ac97 *ac97, int reg, unsigned int rate)
  247. {
  248. int dbl;
  249. unsigned int tmp;
  250. dbl = rate > 48000;
  251. if (dbl) {
  252. if (!(ac97->flags & AC97_DOUBLE_RATE))
  253. return -EINVAL;
  254. if (reg != AC97_PCM_FRONT_DAC_RATE)
  255. return -EINVAL;
  256. }
  257. snd_ac97_update_power(ac97, reg, 1);
  258. switch (reg) {
  259. case AC97_PCM_MIC_ADC_RATE:
  260. if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRM) == 0) /* MIC VRA */
  261. if (rate != 48000)
  262. return -EINVAL;
  263. break;
  264. case AC97_PCM_FRONT_DAC_RATE:
  265. case AC97_PCM_LR_ADC_RATE:
  266. if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRA) == 0) /* VRA */
  267. if (rate != 48000 && rate != 96000)
  268. return -EINVAL;
  269. break;
  270. case AC97_PCM_SURR_DAC_RATE:
  271. if (! (ac97->scaps & AC97_SCAP_SURROUND_DAC))
  272. return -EINVAL;
  273. break;
  274. case AC97_PCM_LFE_DAC_RATE:
  275. if (! (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  276. return -EINVAL;
  277. break;
  278. case AC97_SPDIF:
  279. /* special case */
  280. return set_spdif_rate(ac97, rate);
  281. default:
  282. return -EINVAL;
  283. }
  284. if (dbl)
  285. rate /= 2;
  286. tmp = (rate * ac97->bus->clock) / 48000;
  287. if (tmp > 65535)
  288. return -EINVAL;
  289. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
  290. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  291. AC97_EA_DRA, dbl ? AC97_EA_DRA : 0);
  292. snd_ac97_update(ac97, reg, tmp & 0xffff);
  293. snd_ac97_read(ac97, reg);
  294. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE) {
  295. /* Intel controllers require double rate data to be put in
  296. * slots 7+8
  297. */
  298. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE,
  299. AC97_GP_DRSS_MASK,
  300. dbl ? AC97_GP_DRSS_78 : 0);
  301. snd_ac97_read(ac97, AC97_GENERAL_PURPOSE);
  302. }
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(snd_ac97_set_rate);
  306. static unsigned short get_pslots(struct snd_ac97 *ac97, unsigned char *rate_table, unsigned short *spdif_slots)
  307. {
  308. if (!ac97_is_audio(ac97))
  309. return 0;
  310. if (ac97_is_rev22(ac97) || ac97_can_amap(ac97)) {
  311. unsigned short slots = 0;
  312. if (ac97_is_rev22(ac97)) {
  313. /* Note: it's simply emulation of AMAP behaviour */
  314. u16 es;
  315. es = ac97->regs[AC97_EXTENDED_ID] &= ~AC97_EI_DACS_SLOT_MASK;
  316. switch (ac97->addr) {
  317. case 1:
  318. case 2: es |= (1<<AC97_EI_DACS_SLOT_SHIFT); break;
  319. case 3: es |= (2<<AC97_EI_DACS_SLOT_SHIFT); break;
  320. }
  321. snd_ac97_write_cache(ac97, AC97_EXTENDED_ID, es);
  322. }
  323. switch (ac97->addr) {
  324. case 0:
  325. slots |= (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  326. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  327. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  328. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  329. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  330. if (ac97->ext_id & AC97_EI_SPDIF) {
  331. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  332. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
  333. else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  334. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  335. else
  336. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  337. }
  338. *rate_table = 0;
  339. break;
  340. case 1:
  341. case 2:
  342. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  343. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  344. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  345. if (ac97->ext_id & AC97_EI_SPDIF) {
  346. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  347. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  348. else
  349. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  350. }
  351. *rate_table = 1;
  352. break;
  353. case 3:
  354. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  355. if (ac97->ext_id & AC97_EI_SPDIF)
  356. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  357. *rate_table = 2;
  358. break;
  359. }
  360. return slots;
  361. } else {
  362. unsigned short slots;
  363. slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  364. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  365. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  366. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  367. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  368. if (ac97->ext_id & AC97_EI_SPDIF) {
  369. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  370. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
  371. else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  372. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  373. else
  374. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  375. }
  376. *rate_table = 0;
  377. return slots;
  378. }
  379. }
  380. static unsigned short get_cslots(struct snd_ac97 *ac97)
  381. {
  382. unsigned short slots;
  383. if (!ac97_is_audio(ac97))
  384. return 0;
  385. slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  386. slots |= (1<<AC97_SLOT_MIC);
  387. return slots;
  388. }
  389. static unsigned int get_rates(struct ac97_pcm *pcm, unsigned int cidx, unsigned short slots, int dbl)
  390. {
  391. int i, idx;
  392. unsigned int rates = ~0;
  393. unsigned char reg;
  394. for (i = 3; i < 12; i++) {
  395. if (!(slots & (1 << i)))
  396. continue;
  397. reg = get_slot_reg(pcm, cidx, i, dbl);
  398. switch (reg) {
  399. case AC97_PCM_FRONT_DAC_RATE: idx = AC97_RATES_FRONT_DAC; break;
  400. case AC97_PCM_SURR_DAC_RATE: idx = AC97_RATES_SURR_DAC; break;
  401. case AC97_PCM_LFE_DAC_RATE: idx = AC97_RATES_LFE_DAC; break;
  402. case AC97_PCM_LR_ADC_RATE: idx = AC97_RATES_ADC; break;
  403. case AC97_PCM_MIC_ADC_RATE: idx = AC97_RATES_MIC_ADC; break;
  404. default: idx = AC97_RATES_SPDIF; break;
  405. }
  406. rates &= pcm->r[dbl].codec[cidx]->rates[idx];
  407. }
  408. if (!dbl)
  409. rates &= ~(SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 |
  410. SNDRV_PCM_RATE_96000);
  411. return rates;
  412. }
  413. /**
  414. * snd_ac97_pcm_assign - assign AC97 slots to given PCM streams
  415. * @bus: the ac97 bus instance
  416. * @pcms_count: count of PCMs to be assigned
  417. * @pcms: PCMs to be assigned
  418. *
  419. * It assigns available AC97 slots for given PCMs. If none or only
  420. * some slots are available, pcm->xxx.slots and pcm->xxx.rslots[] members
  421. * are reduced and might be zero.
  422. */
  423. int snd_ac97_pcm_assign(struct snd_ac97_bus *bus,
  424. unsigned short pcms_count,
  425. const struct ac97_pcm *pcms)
  426. {
  427. int i, j, k;
  428. const struct ac97_pcm *pcm;
  429. struct ac97_pcm *rpcms, *rpcm;
  430. unsigned short avail_slots[2][4];
  431. unsigned char rate_table[2][4];
  432. unsigned short tmp, slots;
  433. unsigned short spdif_slots[4];
  434. unsigned int rates;
  435. struct snd_ac97 *codec;
  436. rpcms = kcalloc(pcms_count, sizeof(struct ac97_pcm), GFP_KERNEL);
  437. if (rpcms == NULL)
  438. return -ENOMEM;
  439. memset(avail_slots, 0, sizeof(avail_slots));
  440. memset(rate_table, 0, sizeof(rate_table));
  441. memset(spdif_slots, 0, sizeof(spdif_slots));
  442. for (i = 0; i < 4; i++) {
  443. codec = bus->codec[i];
  444. if (!codec)
  445. continue;
  446. avail_slots[0][i] = get_pslots(codec, &rate_table[0][i], &spdif_slots[i]);
  447. avail_slots[1][i] = get_cslots(codec);
  448. if (!(codec->scaps & AC97_SCAP_INDEP_SDIN)) {
  449. for (j = 0; j < i; j++) {
  450. if (bus->codec[j])
  451. avail_slots[1][i] &= ~avail_slots[1][j];
  452. }
  453. }
  454. }
  455. /* first step - exclusive devices */
  456. for (i = 0; i < pcms_count; i++) {
  457. pcm = &pcms[i];
  458. rpcm = &rpcms[i];
  459. /* low-level driver thinks that it's more clever */
  460. if (pcm->copy_flag) {
  461. *rpcm = *pcm;
  462. continue;
  463. }
  464. rpcm->stream = pcm->stream;
  465. rpcm->exclusive = pcm->exclusive;
  466. rpcm->spdif = pcm->spdif;
  467. rpcm->private_value = pcm->private_value;
  468. rpcm->bus = bus;
  469. rpcm->rates = ~0;
  470. slots = pcm->r[0].slots;
  471. for (j = 0; j < 4 && slots; j++) {
  472. if (!bus->codec[j])
  473. continue;
  474. rates = ~0;
  475. if (pcm->spdif && pcm->stream == 0)
  476. tmp = spdif_slots[j];
  477. else
  478. tmp = avail_slots[pcm->stream][j];
  479. if (pcm->exclusive) {
  480. /* exclusive access */
  481. tmp &= slots;
  482. for (k = 0; k < i; k++) {
  483. if (rpcm->stream == rpcms[k].stream)
  484. tmp &= ~rpcms[k].r[0].rslots[j];
  485. }
  486. } else {
  487. /* non-exclusive access */
  488. tmp &= pcm->r[0].slots;
  489. }
  490. if (tmp) {
  491. rpcm->r[0].rslots[j] = tmp;
  492. rpcm->r[0].codec[j] = bus->codec[j];
  493. rpcm->r[0].rate_table[j] = rate_table[pcm->stream][j];
  494. if (bus->no_vra)
  495. rates = SNDRV_PCM_RATE_48000;
  496. else
  497. rates = get_rates(rpcm, j, tmp, 0);
  498. if (pcm->exclusive)
  499. avail_slots[pcm->stream][j] &= ~tmp;
  500. }
  501. slots &= ~tmp;
  502. rpcm->r[0].slots |= tmp;
  503. rpcm->rates &= rates;
  504. }
  505. /* for double rate, we check the first codec only */
  506. if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  507. bus->codec[0] && (bus->codec[0]->flags & AC97_DOUBLE_RATE) &&
  508. rate_table[pcm->stream][0] == 0) {
  509. tmp = (1<<AC97_SLOT_PCM_LEFT) | (1<<AC97_SLOT_PCM_RIGHT) |
  510. (1<<AC97_SLOT_PCM_LEFT_0) | (1<<AC97_SLOT_PCM_RIGHT_0);
  511. if ((tmp & pcm->r[1].slots) == tmp) {
  512. rpcm->r[1].slots = tmp;
  513. rpcm->r[1].rslots[0] = tmp;
  514. rpcm->r[1].rate_table[0] = 0;
  515. rpcm->r[1].codec[0] = bus->codec[0];
  516. if (pcm->exclusive)
  517. avail_slots[pcm->stream][0] &= ~tmp;
  518. if (bus->no_vra)
  519. rates = SNDRV_PCM_RATE_96000;
  520. else
  521. rates = get_rates(rpcm, 0, tmp, 1);
  522. rpcm->rates |= rates;
  523. }
  524. }
  525. if (rpcm->rates == ~0)
  526. rpcm->rates = 0; /* not used */
  527. }
  528. bus->pcms_count = pcms_count;
  529. bus->pcms = rpcms;
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(snd_ac97_pcm_assign);
  533. /**
  534. * snd_ac97_pcm_open - opens the given AC97 pcm
  535. * @pcm: the ac97 pcm instance
  536. * @rate: rate in Hz, if codec does not support VRA, this value must be 48000Hz
  537. * @cfg: output stream characteristics
  538. * @slots: a subset of allocated slots (snd_ac97_pcm_assign) for this pcm
  539. *
  540. * It locks the specified slots and sets the given rate to AC97 registers.
  541. */
  542. int snd_ac97_pcm_open(struct ac97_pcm *pcm, unsigned int rate,
  543. enum ac97_pcm_cfg cfg, unsigned short slots)
  544. {
  545. struct snd_ac97_bus *bus;
  546. int i, cidx, r, ok_flag;
  547. unsigned int reg_ok[4] = {0,0,0,0};
  548. unsigned char reg;
  549. int err = 0;
  550. r = rate > 48000;
  551. bus = pcm->bus;
  552. if (cfg == AC97_PCM_CFG_SPDIF) {
  553. for (cidx = 0; cidx < 4; cidx++)
  554. if (bus->codec[cidx] && (bus->codec[cidx]->ext_id & AC97_EI_SPDIF)) {
  555. err = set_spdif_rate(bus->codec[cidx], rate);
  556. if (err < 0)
  557. return err;
  558. }
  559. }
  560. spin_lock_irq(&pcm->bus->bus_lock);
  561. for (i = 3; i < 12; i++) {
  562. if (!(slots & (1 << i)))
  563. continue;
  564. ok_flag = 0;
  565. for (cidx = 0; cidx < 4; cidx++) {
  566. if (bus->used_slots[pcm->stream][cidx] & (1 << i)) {
  567. spin_unlock_irq(&pcm->bus->bus_lock);
  568. err = -EBUSY;
  569. goto error;
  570. }
  571. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  572. bus->used_slots[pcm->stream][cidx] |= (1 << i);
  573. ok_flag++;
  574. }
  575. }
  576. if (!ok_flag) {
  577. spin_unlock_irq(&pcm->bus->bus_lock);
  578. snd_printk(KERN_ERR "cannot find configuration for AC97 slot %i\n", i);
  579. err = -EAGAIN;
  580. goto error;
  581. }
  582. }
  583. pcm->cur_dbl = r;
  584. spin_unlock_irq(&pcm->bus->bus_lock);
  585. for (i = 3; i < 12; i++) {
  586. if (!(slots & (1 << i)))
  587. continue;
  588. for (cidx = 0; cidx < 4; cidx++) {
  589. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  590. reg = get_slot_reg(pcm, cidx, i, r);
  591. if (reg == 0xff) {
  592. snd_printk(KERN_ERR "invalid AC97 slot %i?\n", i);
  593. continue;
  594. }
  595. if (reg_ok[cidx] & (1 << (reg - AC97_PCM_FRONT_DAC_RATE)))
  596. continue;
  597. //printk(KERN_DEBUG "setting ac97 reg 0x%x to rate %d\n", reg, rate);
  598. err = snd_ac97_set_rate(pcm->r[r].codec[cidx], reg, rate);
  599. if (err < 0)
  600. snd_printk(KERN_ERR "error in snd_ac97_set_rate: cidx=%d, reg=0x%x, rate=%d, err=%d\n", cidx, reg, rate, err);
  601. else
  602. reg_ok[cidx] |= (1 << (reg - AC97_PCM_FRONT_DAC_RATE));
  603. }
  604. }
  605. }
  606. pcm->aslots = slots;
  607. return 0;
  608. error:
  609. pcm->aslots = slots;
  610. snd_ac97_pcm_close(pcm);
  611. return err;
  612. }
  613. EXPORT_SYMBOL(snd_ac97_pcm_open);
  614. /**
  615. * snd_ac97_pcm_close - closes the given AC97 pcm
  616. * @pcm: the ac97 pcm instance
  617. *
  618. * It frees the locked AC97 slots.
  619. */
  620. int snd_ac97_pcm_close(struct ac97_pcm *pcm)
  621. {
  622. struct snd_ac97_bus *bus;
  623. unsigned short slots = pcm->aslots;
  624. int i, cidx;
  625. #ifdef CONFIG_SND_AC97_POWER_SAVE
  626. int r = pcm->cur_dbl;
  627. for (i = 3; i < 12; i++) {
  628. if (!(slots & (1 << i)))
  629. continue;
  630. for (cidx = 0; cidx < 4; cidx++) {
  631. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  632. int reg = get_slot_reg(pcm, cidx, i, r);
  633. snd_ac97_update_power(pcm->r[r].codec[cidx],
  634. reg, 0);
  635. }
  636. }
  637. }
  638. #endif
  639. bus = pcm->bus;
  640. spin_lock_irq(&pcm->bus->bus_lock);
  641. for (i = 3; i < 12; i++) {
  642. if (!(slots & (1 << i)))
  643. continue;
  644. for (cidx = 0; cidx < 4; cidx++)
  645. bus->used_slots[pcm->stream][cidx] &= ~(1 << i);
  646. }
  647. pcm->aslots = 0;
  648. pcm->cur_dbl = 0;
  649. spin_unlock_irq(&pcm->bus->bus_lock);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL(snd_ac97_pcm_close);
  653. static int double_rate_hw_constraint_rate(struct snd_pcm_hw_params *params,
  654. struct snd_pcm_hw_rule *rule)
  655. {
  656. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  657. if (channels->min > 2) {
  658. static const struct snd_interval single_rates = {
  659. .min = 1,
  660. .max = 48000,
  661. };
  662. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  663. return snd_interval_refine(rate, &single_rates);
  664. }
  665. return 0;
  666. }
  667. static int double_rate_hw_constraint_channels(struct snd_pcm_hw_params *params,
  668. struct snd_pcm_hw_rule *rule)
  669. {
  670. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  671. if (rate->min > 48000) {
  672. static const struct snd_interval double_rate_channels = {
  673. .min = 2,
  674. .max = 2,
  675. };
  676. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  677. return snd_interval_refine(channels, &double_rate_channels);
  678. }
  679. return 0;
  680. }
  681. /**
  682. * snd_ac97_pcm_double_rate_rules - set double rate constraints
  683. * @runtime: the runtime of the ac97 front playback pcm
  684. *
  685. * Installs the hardware constraint rules to prevent using double rates and
  686. * more than two channels at the same time.
  687. */
  688. int snd_ac97_pcm_double_rate_rules(struct snd_pcm_runtime *runtime)
  689. {
  690. int err;
  691. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  692. double_rate_hw_constraint_rate, NULL,
  693. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  694. if (err < 0)
  695. return err;
  696. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  697. double_rate_hw_constraint_channels, NULL,
  698. SNDRV_PCM_HW_PARAM_RATE, -1);
  699. return err;
  700. }
  701. EXPORT_SYMBOL(snd_ac97_pcm_double_rate_rules);