ctatc.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctatc.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of the device resource management
  12. * object.
  13. *
  14. * @Author Liu Chun
  15. * @Date Mar 28 2008
  16. */
  17. #include "ctatc.h"
  18. #include "ctpcm.h"
  19. #include "ctmixer.h"
  20. #include "ctsrc.h"
  21. #include "ctamixer.h"
  22. #include "ctdaio.h"
  23. #include "cttimer.h"
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <sound/pcm.h>
  27. #include <sound/control.h>
  28. #include <sound/asoundef.h>
  29. #define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
  30. #define MAX_MULTI_CHN 8
  31. #define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
  32. | IEC958_AES0_CON_NOT_COPYRIGHT) \
  33. | ((IEC958_AES1_CON_MIXER \
  34. | IEC958_AES1_CON_ORIGINAL) << 8) \
  35. | (0x10 << 16) \
  36. | ((IEC958_AES3_CON_FS_48000) << 24))
  37. static struct snd_pci_quirk subsys_20k1_list[] = {
  38. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
  39. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
  40. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
  41. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
  42. SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
  43. "UAA", CTUAA),
  44. { } /* terminator */
  45. };
  46. static struct snd_pci_quirk subsys_20k2_list[] = {
  47. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
  48. "SB0760", CTSB0760),
  49. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB1270,
  50. "SB1270", CTSB1270),
  51. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
  52. "SB0880", CTSB0880),
  53. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
  54. "SB0880", CTSB0880),
  55. SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
  56. "SB0880", CTSB0880),
  57. SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
  58. PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
  59. CTHENDRIX),
  60. { } /* terminator */
  61. };
  62. static const char *ct_subsys_name[NUM_CTCARDS] = {
  63. /* 20k1 models */
  64. [CTSB055X] = "SB055x",
  65. [CTSB073X] = "SB073x",
  66. [CTUAA] = "UAA",
  67. [CT20K1_UNKNOWN] = "Unknown",
  68. /* 20k2 models */
  69. [CTSB0760] = "SB076x",
  70. [CTHENDRIX] = "Hendrix",
  71. [CTSB0880] = "SB0880",
  72. [CTSB1270] = "SB1270",
  73. [CT20K2_UNKNOWN] = "Unknown",
  74. };
  75. static struct {
  76. int (*create)(struct ct_atc *atc,
  77. enum CTALSADEVS device, const char *device_name);
  78. int (*destroy)(void *alsa_dev);
  79. const char *public_name;
  80. } alsa_dev_funcs[NUM_CTALSADEVS] = {
  81. [FRONT] = { .create = ct_alsa_pcm_create,
  82. .destroy = NULL,
  83. .public_name = "Front/WaveIn"},
  84. [SURROUND] = { .create = ct_alsa_pcm_create,
  85. .destroy = NULL,
  86. .public_name = "Surround"},
  87. [CLFE] = { .create = ct_alsa_pcm_create,
  88. .destroy = NULL,
  89. .public_name = "Center/LFE"},
  90. [SIDE] = { .create = ct_alsa_pcm_create,
  91. .destroy = NULL,
  92. .public_name = "Side"},
  93. [IEC958] = { .create = ct_alsa_pcm_create,
  94. .destroy = NULL,
  95. .public_name = "IEC958 Non-audio"},
  96. [MIXER] = { .create = ct_alsa_mix_create,
  97. .destroy = NULL,
  98. .public_name = "Mixer"}
  99. };
  100. typedef int (*create_t)(struct hw *, void **);
  101. typedef int (*destroy_t)(void *);
  102. static struct {
  103. int (*create)(struct hw *hw, void **rmgr);
  104. int (*destroy)(void *mgr);
  105. } rsc_mgr_funcs[NUM_RSCTYP] = {
  106. [SRC] = { .create = (create_t)src_mgr_create,
  107. .destroy = (destroy_t)src_mgr_destroy },
  108. [SRCIMP] = { .create = (create_t)srcimp_mgr_create,
  109. .destroy = (destroy_t)srcimp_mgr_destroy },
  110. [AMIXER] = { .create = (create_t)amixer_mgr_create,
  111. .destroy = (destroy_t)amixer_mgr_destroy },
  112. [SUM] = { .create = (create_t)sum_mgr_create,
  113. .destroy = (destroy_t)sum_mgr_destroy },
  114. [DAIO] = { .create = (create_t)daio_mgr_create,
  115. .destroy = (destroy_t)daio_mgr_destroy }
  116. };
  117. static int
  118. atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
  119. /* *
  120. * Only mono and interleaved modes are supported now.
  121. * Always allocates a contiguous channel block.
  122. * */
  123. static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  124. {
  125. struct snd_pcm_runtime *runtime;
  126. struct ct_vm *vm;
  127. if (!apcm->substream)
  128. return 0;
  129. runtime = apcm->substream->runtime;
  130. vm = atc->vm;
  131. apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
  132. if (!apcm->vm_block)
  133. return -ENOENT;
  134. return 0;
  135. }
  136. static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  137. {
  138. struct ct_vm *vm;
  139. if (!apcm->vm_block)
  140. return;
  141. vm = atc->vm;
  142. vm->unmap(vm, apcm->vm_block);
  143. apcm->vm_block = NULL;
  144. }
  145. static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
  146. {
  147. return atc->vm->get_ptp_phys(atc->vm, index);
  148. }
  149. static unsigned int convert_format(snd_pcm_format_t snd_format,
  150. struct snd_card *card)
  151. {
  152. switch (snd_format) {
  153. case SNDRV_PCM_FORMAT_U8:
  154. return SRC_SF_U8;
  155. case SNDRV_PCM_FORMAT_S16_LE:
  156. return SRC_SF_S16;
  157. case SNDRV_PCM_FORMAT_S24_3LE:
  158. return SRC_SF_S24;
  159. case SNDRV_PCM_FORMAT_S32_LE:
  160. return SRC_SF_S32;
  161. case SNDRV_PCM_FORMAT_FLOAT_LE:
  162. return SRC_SF_F32;
  163. default:
  164. dev_err(card->dev, "not recognized snd format is %d\n",
  165. snd_format);
  166. return SRC_SF_S16;
  167. }
  168. }
  169. static unsigned int
  170. atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
  171. {
  172. unsigned int pitch;
  173. int b;
  174. /* get pitch and convert to fixed-point 8.24 format. */
  175. pitch = (input_rate / output_rate) << 24;
  176. input_rate %= output_rate;
  177. input_rate /= 100;
  178. output_rate /= 100;
  179. for (b = 31; ((b >= 0) && !(input_rate >> b)); )
  180. b--;
  181. if (b >= 0) {
  182. input_rate <<= (31 - b);
  183. input_rate /= output_rate;
  184. b = 24 - (31 - b);
  185. if (b >= 0)
  186. input_rate <<= b;
  187. else
  188. input_rate >>= -b;
  189. pitch |= input_rate;
  190. }
  191. return pitch;
  192. }
  193. static int select_rom(unsigned int pitch)
  194. {
  195. if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
  196. /* 0.26 <= pitch <= 1.72 */
  197. return 1;
  198. } else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
  199. /* pitch == 1.8375 */
  200. return 2;
  201. } else if (pitch == 0x02000000) {
  202. /* pitch == 2 */
  203. return 3;
  204. } else if (pitch <= 0x08000000) {
  205. /* 0 <= pitch <= 8 */
  206. return 0;
  207. } else {
  208. return -ENOENT;
  209. }
  210. }
  211. static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  212. {
  213. struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
  214. struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
  215. struct src_desc desc = {0};
  216. struct amixer_desc mix_dsc = {0};
  217. struct src *src;
  218. struct amixer *amixer;
  219. int err;
  220. int n_amixer = apcm->substream->runtime->channels, i = 0;
  221. int device = apcm->substream->pcm->device;
  222. unsigned int pitch;
  223. /* first release old resources */
  224. atc_pcm_release_resources(atc, apcm);
  225. /* Get SRC resource */
  226. desc.multi = apcm->substream->runtime->channels;
  227. desc.msr = atc->msr;
  228. desc.mode = MEMRD;
  229. err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
  230. if (err)
  231. goto error1;
  232. pitch = atc_get_pitch(apcm->substream->runtime->rate,
  233. (atc->rsr * atc->msr));
  234. src = apcm->src;
  235. src->ops->set_pitch(src, pitch);
  236. src->ops->set_rom(src, select_rom(pitch));
  237. src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
  238. atc->card));
  239. src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
  240. /* Get AMIXER resource */
  241. n_amixer = (n_amixer < 2) ? 2 : n_amixer;
  242. apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
  243. if (!apcm->amixers) {
  244. err = -ENOMEM;
  245. goto error1;
  246. }
  247. mix_dsc.msr = atc->msr;
  248. for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
  249. err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
  250. (struct amixer **)&apcm->amixers[i]);
  251. if (err)
  252. goto error1;
  253. apcm->n_amixer++;
  254. }
  255. /* Set up device virtual mem map */
  256. err = ct_map_audio_buffer(atc, apcm);
  257. if (err < 0)
  258. goto error1;
  259. /* Connect resources */
  260. src = apcm->src;
  261. for (i = 0; i < n_amixer; i++) {
  262. amixer = apcm->amixers[i];
  263. mutex_lock(&atc->atc_mutex);
  264. amixer->ops->setup(amixer, &src->rsc,
  265. INIT_VOL, atc->pcm[i+device*2]);
  266. mutex_unlock(&atc->atc_mutex);
  267. src = src->ops->next_interleave(src);
  268. if (!src)
  269. src = apcm->src;
  270. }
  271. ct_timer_prepare(apcm->timer);
  272. return 0;
  273. error1:
  274. atc_pcm_release_resources(atc, apcm);
  275. return err;
  276. }
  277. static int
  278. atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  279. {
  280. struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
  281. struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
  282. struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
  283. struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
  284. struct srcimp *srcimp;
  285. int i;
  286. if (apcm->srcimps) {
  287. for (i = 0; i < apcm->n_srcimp; i++) {
  288. srcimp = apcm->srcimps[i];
  289. srcimp->ops->unmap(srcimp);
  290. srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
  291. apcm->srcimps[i] = NULL;
  292. }
  293. kfree(apcm->srcimps);
  294. apcm->srcimps = NULL;
  295. }
  296. if (apcm->srccs) {
  297. for (i = 0; i < apcm->n_srcc; i++) {
  298. src_mgr->put_src(src_mgr, apcm->srccs[i]);
  299. apcm->srccs[i] = NULL;
  300. }
  301. kfree(apcm->srccs);
  302. apcm->srccs = NULL;
  303. }
  304. if (apcm->amixers) {
  305. for (i = 0; i < apcm->n_amixer; i++) {
  306. amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
  307. apcm->amixers[i] = NULL;
  308. }
  309. kfree(apcm->amixers);
  310. apcm->amixers = NULL;
  311. }
  312. if (apcm->mono) {
  313. sum_mgr->put_sum(sum_mgr, apcm->mono);
  314. apcm->mono = NULL;
  315. }
  316. if (apcm->src) {
  317. src_mgr->put_src(src_mgr, apcm->src);
  318. apcm->src = NULL;
  319. }
  320. if (apcm->vm_block) {
  321. /* Undo device virtual mem map */
  322. ct_unmap_audio_buffer(atc, apcm);
  323. apcm->vm_block = NULL;
  324. }
  325. return 0;
  326. }
  327. static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  328. {
  329. unsigned int max_cisz;
  330. struct src *src = apcm->src;
  331. if (apcm->started)
  332. return 0;
  333. apcm->started = 1;
  334. max_cisz = src->multi * src->rsc.msr;
  335. max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
  336. src->ops->set_sa(src, apcm->vm_block->addr);
  337. src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
  338. src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
  339. src->ops->set_cisz(src, max_cisz);
  340. src->ops->set_bm(src, 1);
  341. src->ops->set_state(src, SRC_STATE_INIT);
  342. src->ops->commit_write(src);
  343. ct_timer_start(apcm->timer);
  344. return 0;
  345. }
  346. static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  347. {
  348. struct src *src;
  349. int i;
  350. ct_timer_stop(apcm->timer);
  351. src = apcm->src;
  352. src->ops->set_bm(src, 0);
  353. src->ops->set_state(src, SRC_STATE_OFF);
  354. src->ops->commit_write(src);
  355. if (apcm->srccs) {
  356. for (i = 0; i < apcm->n_srcc; i++) {
  357. src = apcm->srccs[i];
  358. src->ops->set_bm(src, 0);
  359. src->ops->set_state(src, SRC_STATE_OFF);
  360. src->ops->commit_write(src);
  361. }
  362. }
  363. apcm->started = 0;
  364. return 0;
  365. }
  366. static int
  367. atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  368. {
  369. struct src *src = apcm->src;
  370. u32 size, max_cisz;
  371. int position;
  372. if (!src)
  373. return 0;
  374. position = src->ops->get_ca(src);
  375. if (position < apcm->vm_block->addr) {
  376. dev_dbg(atc->card->dev,
  377. "bad ca - ca=0x%08x, vba=0x%08x, vbs=0x%08x\n",
  378. position, apcm->vm_block->addr, apcm->vm_block->size);
  379. position = apcm->vm_block->addr;
  380. }
  381. size = apcm->vm_block->size;
  382. max_cisz = src->multi * src->rsc.msr;
  383. max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
  384. return (position + size - max_cisz - apcm->vm_block->addr) % size;
  385. }
  386. struct src_node_conf_t {
  387. unsigned int pitch;
  388. unsigned int msr:8;
  389. unsigned int mix_msr:8;
  390. unsigned int imp_msr:8;
  391. unsigned int vo:1;
  392. };
  393. static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
  394. struct src_node_conf_t *conf, int *n_srcc)
  395. {
  396. unsigned int pitch;
  397. /* get pitch and convert to fixed-point 8.24 format. */
  398. pitch = atc_get_pitch((atc->rsr * atc->msr),
  399. apcm->substream->runtime->rate);
  400. *n_srcc = 0;
  401. if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
  402. *n_srcc = apcm->substream->runtime->channels;
  403. conf[0].pitch = pitch;
  404. conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
  405. conf[0].vo = 1;
  406. } else if (2 <= atc->msr) {
  407. if (0x8000000 < pitch) {
  408. /* Need two-stage SRCs, SRCIMPs and
  409. * AMIXERs for converting format */
  410. conf[0].pitch = (atc->msr << 24);
  411. conf[0].msr = conf[0].mix_msr = 1;
  412. conf[0].imp_msr = atc->msr;
  413. conf[0].vo = 0;
  414. conf[1].pitch = atc_get_pitch(atc->rsr,
  415. apcm->substream->runtime->rate);
  416. conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
  417. conf[1].vo = 1;
  418. *n_srcc = apcm->substream->runtime->channels * 2;
  419. } else if (0x1000000 < pitch) {
  420. /* Need one-stage SRCs, SRCIMPs and
  421. * AMIXERs for converting format */
  422. conf[0].pitch = pitch;
  423. conf[0].msr = conf[0].mix_msr
  424. = conf[0].imp_msr = atc->msr;
  425. conf[0].vo = 1;
  426. *n_srcc = apcm->substream->runtime->channels;
  427. }
  428. }
  429. }
  430. static int
  431. atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  432. {
  433. struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
  434. struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
  435. struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
  436. struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
  437. struct src_desc src_dsc = {0};
  438. struct src *src;
  439. struct srcimp_desc srcimp_dsc = {0};
  440. struct srcimp *srcimp;
  441. struct amixer_desc mix_dsc = {0};
  442. struct sum_desc sum_dsc = {0};
  443. unsigned int pitch;
  444. int multi, err, i;
  445. int n_srcimp, n_amixer, n_srcc, n_sum;
  446. struct src_node_conf_t src_node_conf[2] = {{0} };
  447. /* first release old resources */
  448. atc_pcm_release_resources(atc, apcm);
  449. /* The numbers of converting SRCs and SRCIMPs should be determined
  450. * by pitch value. */
  451. multi = apcm->substream->runtime->channels;
  452. /* get pitch and convert to fixed-point 8.24 format. */
  453. pitch = atc_get_pitch((atc->rsr * atc->msr),
  454. apcm->substream->runtime->rate);
  455. setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
  456. n_sum = (1 == multi) ? 1 : 0;
  457. n_amixer = n_sum * 2 + n_srcc;
  458. n_srcimp = n_srcc;
  459. if ((multi > 1) && (0x8000000 >= pitch)) {
  460. /* Need extra AMIXERs and SRCIMPs for special treatment
  461. * of interleaved recording of conjugate channels */
  462. n_amixer += multi * atc->msr;
  463. n_srcimp += multi * atc->msr;
  464. } else {
  465. n_srcimp += multi;
  466. }
  467. if (n_srcc) {
  468. apcm->srccs = kzalloc(sizeof(void *)*n_srcc, GFP_KERNEL);
  469. if (!apcm->srccs)
  470. return -ENOMEM;
  471. }
  472. if (n_amixer) {
  473. apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
  474. if (!apcm->amixers) {
  475. err = -ENOMEM;
  476. goto error1;
  477. }
  478. }
  479. apcm->srcimps = kzalloc(sizeof(void *)*n_srcimp, GFP_KERNEL);
  480. if (!apcm->srcimps) {
  481. err = -ENOMEM;
  482. goto error1;
  483. }
  484. /* Allocate SRCs for sample rate conversion if needed */
  485. src_dsc.multi = 1;
  486. src_dsc.mode = ARCRW;
  487. for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
  488. src_dsc.msr = src_node_conf[i/multi].msr;
  489. err = src_mgr->get_src(src_mgr, &src_dsc,
  490. (struct src **)&apcm->srccs[i]);
  491. if (err)
  492. goto error1;
  493. src = apcm->srccs[i];
  494. pitch = src_node_conf[i/multi].pitch;
  495. src->ops->set_pitch(src, pitch);
  496. src->ops->set_rom(src, select_rom(pitch));
  497. src->ops->set_vo(src, src_node_conf[i/multi].vo);
  498. apcm->n_srcc++;
  499. }
  500. /* Allocate AMIXERs for routing SRCs of conversion if needed */
  501. for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
  502. if (i < (n_sum*2))
  503. mix_dsc.msr = atc->msr;
  504. else if (i < (n_sum*2+n_srcc))
  505. mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
  506. else
  507. mix_dsc.msr = 1;
  508. err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
  509. (struct amixer **)&apcm->amixers[i]);
  510. if (err)
  511. goto error1;
  512. apcm->n_amixer++;
  513. }
  514. /* Allocate a SUM resource to mix all input channels together */
  515. sum_dsc.msr = atc->msr;
  516. err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
  517. if (err)
  518. goto error1;
  519. pitch = atc_get_pitch((atc->rsr * atc->msr),
  520. apcm->substream->runtime->rate);
  521. /* Allocate SRCIMP resources */
  522. for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
  523. if (i < (n_srcc))
  524. srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
  525. else if (1 == multi)
  526. srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
  527. else
  528. srcimp_dsc.msr = 1;
  529. err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
  530. if (err)
  531. goto error1;
  532. apcm->srcimps[i] = srcimp;
  533. apcm->n_srcimp++;
  534. }
  535. /* Allocate a SRC for writing data to host memory */
  536. src_dsc.multi = apcm->substream->runtime->channels;
  537. src_dsc.msr = 1;
  538. src_dsc.mode = MEMWR;
  539. err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
  540. if (err)
  541. goto error1;
  542. src = apcm->src;
  543. src->ops->set_pitch(src, pitch);
  544. /* Set up device virtual mem map */
  545. err = ct_map_audio_buffer(atc, apcm);
  546. if (err < 0)
  547. goto error1;
  548. return 0;
  549. error1:
  550. atc_pcm_release_resources(atc, apcm);
  551. return err;
  552. }
  553. static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  554. {
  555. struct src *src;
  556. struct amixer *amixer;
  557. struct srcimp *srcimp;
  558. struct ct_mixer *mixer = atc->mixer;
  559. struct sum *mono;
  560. struct rsc *out_ports[8] = {NULL};
  561. int err, i, j, n_sum, multi;
  562. unsigned int pitch;
  563. int mix_base = 0, imp_base = 0;
  564. atc_pcm_release_resources(atc, apcm);
  565. /* Get needed resources. */
  566. err = atc_pcm_capture_get_resources(atc, apcm);
  567. if (err)
  568. return err;
  569. /* Connect resources */
  570. mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
  571. &out_ports[0], &out_ports[1]);
  572. multi = apcm->substream->runtime->channels;
  573. if (1 == multi) {
  574. mono = apcm->mono;
  575. for (i = 0; i < 2; i++) {
  576. amixer = apcm->amixers[i];
  577. amixer->ops->setup(amixer, out_ports[i],
  578. MONO_SUM_SCALE, mono);
  579. }
  580. out_ports[0] = &mono->rsc;
  581. n_sum = 1;
  582. mix_base = n_sum * 2;
  583. }
  584. for (i = 0; i < apcm->n_srcc; i++) {
  585. src = apcm->srccs[i];
  586. srcimp = apcm->srcimps[imp_base+i];
  587. amixer = apcm->amixers[mix_base+i];
  588. srcimp->ops->map(srcimp, src, out_ports[i%multi]);
  589. amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
  590. out_ports[i%multi] = &amixer->rsc;
  591. }
  592. pitch = atc_get_pitch((atc->rsr * atc->msr),
  593. apcm->substream->runtime->rate);
  594. if ((multi > 1) && (pitch <= 0x8000000)) {
  595. /* Special connection for interleaved
  596. * recording with conjugate channels */
  597. for (i = 0; i < multi; i++) {
  598. out_ports[i]->ops->master(out_ports[i]);
  599. for (j = 0; j < atc->msr; j++) {
  600. amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
  601. amixer->ops->set_input(amixer, out_ports[i]);
  602. amixer->ops->set_scale(amixer, INIT_VOL);
  603. amixer->ops->set_sum(amixer, NULL);
  604. amixer->ops->commit_raw_write(amixer);
  605. out_ports[i]->ops->next_conj(out_ports[i]);
  606. srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
  607. srcimp->ops->map(srcimp, apcm->src,
  608. &amixer->rsc);
  609. }
  610. }
  611. } else {
  612. for (i = 0; i < multi; i++) {
  613. srcimp = apcm->srcimps[apcm->n_srcc+i];
  614. srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
  615. }
  616. }
  617. ct_timer_prepare(apcm->timer);
  618. return 0;
  619. }
  620. static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  621. {
  622. struct src *src;
  623. struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
  624. int i, multi;
  625. if (apcm->started)
  626. return 0;
  627. apcm->started = 1;
  628. multi = apcm->substream->runtime->channels;
  629. /* Set up converting SRCs */
  630. for (i = 0; i < apcm->n_srcc; i++) {
  631. src = apcm->srccs[i];
  632. src->ops->set_pm(src, ((i%multi) != (multi-1)));
  633. src_mgr->src_disable(src_mgr, src);
  634. }
  635. /* Set up recording SRC */
  636. src = apcm->src;
  637. src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
  638. atc->card));
  639. src->ops->set_sa(src, apcm->vm_block->addr);
  640. src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
  641. src->ops->set_ca(src, apcm->vm_block->addr);
  642. src_mgr->src_disable(src_mgr, src);
  643. /* Disable relevant SRCs firstly */
  644. src_mgr->commit_write(src_mgr);
  645. /* Enable SRCs respectively */
  646. for (i = 0; i < apcm->n_srcc; i++) {
  647. src = apcm->srccs[i];
  648. src->ops->set_state(src, SRC_STATE_RUN);
  649. src->ops->commit_write(src);
  650. src_mgr->src_enable_s(src_mgr, src);
  651. }
  652. src = apcm->src;
  653. src->ops->set_bm(src, 1);
  654. src->ops->set_state(src, SRC_STATE_RUN);
  655. src->ops->commit_write(src);
  656. src_mgr->src_enable_s(src_mgr, src);
  657. /* Enable relevant SRCs synchronously */
  658. src_mgr->commit_write(src_mgr);
  659. ct_timer_start(apcm->timer);
  660. return 0;
  661. }
  662. static int
  663. atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  664. {
  665. struct src *src = apcm->src;
  666. if (!src)
  667. return 0;
  668. return src->ops->get_ca(src) - apcm->vm_block->addr;
  669. }
  670. static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
  671. struct ct_atc_pcm *apcm)
  672. {
  673. struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
  674. struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
  675. struct src_desc desc = {0};
  676. struct amixer_desc mix_dsc = {0};
  677. struct src *src;
  678. int err;
  679. int n_amixer = apcm->substream->runtime->channels, i;
  680. unsigned int pitch, rsr = atc->pll_rate;
  681. /* first release old resources */
  682. atc_pcm_release_resources(atc, apcm);
  683. /* Get SRC resource */
  684. desc.multi = apcm->substream->runtime->channels;
  685. desc.msr = 1;
  686. while (apcm->substream->runtime->rate > (rsr * desc.msr))
  687. desc.msr <<= 1;
  688. desc.mode = MEMRD;
  689. err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
  690. if (err)
  691. goto error1;
  692. pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
  693. src = apcm->src;
  694. src->ops->set_pitch(src, pitch);
  695. src->ops->set_rom(src, select_rom(pitch));
  696. src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
  697. atc->card));
  698. src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
  699. src->ops->set_bp(src, 1);
  700. /* Get AMIXER resource */
  701. n_amixer = (n_amixer < 2) ? 2 : n_amixer;
  702. apcm->amixers = kzalloc(sizeof(void *)*n_amixer, GFP_KERNEL);
  703. if (!apcm->amixers) {
  704. err = -ENOMEM;
  705. goto error1;
  706. }
  707. mix_dsc.msr = desc.msr;
  708. for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
  709. err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
  710. (struct amixer **)&apcm->amixers[i]);
  711. if (err)
  712. goto error1;
  713. apcm->n_amixer++;
  714. }
  715. /* Set up device virtual mem map */
  716. err = ct_map_audio_buffer(atc, apcm);
  717. if (err < 0)
  718. goto error1;
  719. return 0;
  720. error1:
  721. atc_pcm_release_resources(atc, apcm);
  722. return err;
  723. }
  724. static int atc_pll_init(struct ct_atc *atc, int rate)
  725. {
  726. struct hw *hw = atc->hw;
  727. int err;
  728. err = hw->pll_init(hw, rate);
  729. atc->pll_rate = err ? 0 : rate;
  730. return err;
  731. }
  732. static int
  733. spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  734. {
  735. struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
  736. unsigned int rate = apcm->substream->runtime->rate;
  737. unsigned int status;
  738. int err = 0;
  739. unsigned char iec958_con_fs;
  740. switch (rate) {
  741. case 48000:
  742. iec958_con_fs = IEC958_AES3_CON_FS_48000;
  743. break;
  744. case 44100:
  745. iec958_con_fs = IEC958_AES3_CON_FS_44100;
  746. break;
  747. case 32000:
  748. iec958_con_fs = IEC958_AES3_CON_FS_32000;
  749. break;
  750. default:
  751. return -ENOENT;
  752. }
  753. mutex_lock(&atc->atc_mutex);
  754. dao->ops->get_spos(dao, &status);
  755. if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
  756. status &= ~(IEC958_AES3_CON_FS << 24);
  757. status |= (iec958_con_fs << 24);
  758. dao->ops->set_spos(dao, status);
  759. dao->ops->commit_write(dao);
  760. }
  761. if ((rate != atc->pll_rate) && (32000 != rate))
  762. err = atc_pll_init(atc, rate);
  763. mutex_unlock(&atc->atc_mutex);
  764. return err;
  765. }
  766. static int
  767. spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
  768. {
  769. struct src *src;
  770. struct amixer *amixer;
  771. struct dao *dao;
  772. int err;
  773. int i;
  774. atc_pcm_release_resources(atc, apcm);
  775. /* Configure SPDIFOO and PLL to passthrough mode;
  776. * determine pll_rate. */
  777. err = spdif_passthru_playback_setup(atc, apcm);
  778. if (err)
  779. return err;
  780. /* Get needed resources. */
  781. err = spdif_passthru_playback_get_resources(atc, apcm);
  782. if (err)
  783. return err;
  784. /* Connect resources */
  785. src = apcm->src;
  786. for (i = 0; i < apcm->n_amixer; i++) {
  787. amixer = apcm->amixers[i];
  788. amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
  789. src = src->ops->next_interleave(src);
  790. if (!src)
  791. src = apcm->src;
  792. }
  793. /* Connect to SPDIFOO */
  794. mutex_lock(&atc->atc_mutex);
  795. dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
  796. amixer = apcm->amixers[0];
  797. dao->ops->set_left_input(dao, &amixer->rsc);
  798. amixer = apcm->amixers[1];
  799. dao->ops->set_right_input(dao, &amixer->rsc);
  800. mutex_unlock(&atc->atc_mutex);
  801. ct_timer_prepare(apcm->timer);
  802. return 0;
  803. }
  804. static int atc_select_line_in(struct ct_atc *atc)
  805. {
  806. struct hw *hw = atc->hw;
  807. struct ct_mixer *mixer = atc->mixer;
  808. struct src *src;
  809. if (hw->is_adc_source_selected(hw, ADC_LINEIN))
  810. return 0;
  811. mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
  812. mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
  813. hw->select_adc_source(hw, ADC_LINEIN);
  814. src = atc->srcs[2];
  815. mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
  816. src = atc->srcs[3];
  817. mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
  818. return 0;
  819. }
  820. static int atc_select_mic_in(struct ct_atc *atc)
  821. {
  822. struct hw *hw = atc->hw;
  823. struct ct_mixer *mixer = atc->mixer;
  824. struct src *src;
  825. if (hw->is_adc_source_selected(hw, ADC_MICIN))
  826. return 0;
  827. mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
  828. mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
  829. hw->select_adc_source(hw, ADC_MICIN);
  830. src = atc->srcs[2];
  831. mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
  832. src = atc->srcs[3];
  833. mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
  834. return 0;
  835. }
  836. static struct capabilities atc_capabilities(struct ct_atc *atc)
  837. {
  838. struct hw *hw = atc->hw;
  839. return hw->capabilities(hw);
  840. }
  841. static int atc_output_switch_get(struct ct_atc *atc)
  842. {
  843. struct hw *hw = atc->hw;
  844. return hw->output_switch_get(hw);
  845. }
  846. static int atc_output_switch_put(struct ct_atc *atc, int position)
  847. {
  848. struct hw *hw = atc->hw;
  849. return hw->output_switch_put(hw, position);
  850. }
  851. static int atc_mic_source_switch_get(struct ct_atc *atc)
  852. {
  853. struct hw *hw = atc->hw;
  854. return hw->mic_source_switch_get(hw);
  855. }
  856. static int atc_mic_source_switch_put(struct ct_atc *atc, int position)
  857. {
  858. struct hw *hw = atc->hw;
  859. return hw->mic_source_switch_put(hw, position);
  860. }
  861. static int atc_select_digit_io(struct ct_atc *atc)
  862. {
  863. struct hw *hw = atc->hw;
  864. if (hw->is_adc_source_selected(hw, ADC_NONE))
  865. return 0;
  866. hw->select_adc_source(hw, ADC_NONE);
  867. return 0;
  868. }
  869. static int atc_daio_unmute(struct ct_atc *atc, unsigned char state, int type)
  870. {
  871. struct daio_mgr *daio_mgr = atc->rsc_mgrs[DAIO];
  872. if (state)
  873. daio_mgr->daio_enable(daio_mgr, atc->daios[type]);
  874. else
  875. daio_mgr->daio_disable(daio_mgr, atc->daios[type]);
  876. daio_mgr->commit_write(daio_mgr);
  877. return 0;
  878. }
  879. static int
  880. atc_dao_get_status(struct ct_atc *atc, unsigned int *status, int type)
  881. {
  882. struct dao *dao = container_of(atc->daios[type], struct dao, daio);
  883. return dao->ops->get_spos(dao, status);
  884. }
  885. static int
  886. atc_dao_set_status(struct ct_atc *atc, unsigned int status, int type)
  887. {
  888. struct dao *dao = container_of(atc->daios[type], struct dao, daio);
  889. dao->ops->set_spos(dao, status);
  890. dao->ops->commit_write(dao);
  891. return 0;
  892. }
  893. static int atc_line_front_unmute(struct ct_atc *atc, unsigned char state)
  894. {
  895. return atc_daio_unmute(atc, state, LINEO1);
  896. }
  897. static int atc_line_surround_unmute(struct ct_atc *atc, unsigned char state)
  898. {
  899. return atc_daio_unmute(atc, state, LINEO2);
  900. }
  901. static int atc_line_clfe_unmute(struct ct_atc *atc, unsigned char state)
  902. {
  903. return atc_daio_unmute(atc, state, LINEO3);
  904. }
  905. static int atc_line_rear_unmute(struct ct_atc *atc, unsigned char state)
  906. {
  907. return atc_daio_unmute(atc, state, LINEO4);
  908. }
  909. static int atc_line_in_unmute(struct ct_atc *atc, unsigned char state)
  910. {
  911. return atc_daio_unmute(atc, state, LINEIM);
  912. }
  913. static int atc_mic_unmute(struct ct_atc *atc, unsigned char state)
  914. {
  915. return atc_daio_unmute(atc, state, MIC);
  916. }
  917. static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)
  918. {
  919. return atc_daio_unmute(atc, state, SPDIFOO);
  920. }
  921. static int atc_spdif_in_unmute(struct ct_atc *atc, unsigned char state)
  922. {
  923. return atc_daio_unmute(atc, state, SPDIFIO);
  924. }
  925. static int atc_spdif_out_get_status(struct ct_atc *atc, unsigned int *status)
  926. {
  927. return atc_dao_get_status(atc, status, SPDIFOO);
  928. }
  929. static int atc_spdif_out_set_status(struct ct_atc *atc, unsigned int status)
  930. {
  931. return atc_dao_set_status(atc, status, SPDIFOO);
  932. }
  933. static int atc_spdif_out_passthru(struct ct_atc *atc, unsigned char state)
  934. {
  935. struct dao_desc da_dsc = {0};
  936. struct dao *dao;
  937. int err;
  938. struct ct_mixer *mixer = atc->mixer;
  939. struct rsc *rscs[2] = {NULL};
  940. unsigned int spos = 0;
  941. mutex_lock(&atc->atc_mutex);
  942. dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
  943. da_dsc.msr = state ? 1 : atc->msr;
  944. da_dsc.passthru = state ? 1 : 0;
  945. err = dao->ops->reinit(dao, &da_dsc);
  946. if (state) {
  947. spos = IEC958_DEFAULT_CON;
  948. } else {
  949. mixer->get_output_ports(mixer, MIX_SPDIF_OUT,
  950. &rscs[0], &rscs[1]);
  951. dao->ops->set_left_input(dao, rscs[0]);
  952. dao->ops->set_right_input(dao, rscs[1]);
  953. /* Restore PLL to atc->rsr if needed. */
  954. if (atc->pll_rate != atc->rsr)
  955. err = atc_pll_init(atc, atc->rsr);
  956. }
  957. dao->ops->set_spos(dao, spos);
  958. dao->ops->commit_write(dao);
  959. mutex_unlock(&atc->atc_mutex);
  960. return err;
  961. }
  962. static int atc_release_resources(struct ct_atc *atc)
  963. {
  964. int i;
  965. struct daio_mgr *daio_mgr = NULL;
  966. struct dao *dao = NULL;
  967. struct daio *daio = NULL;
  968. struct sum_mgr *sum_mgr = NULL;
  969. struct src_mgr *src_mgr = NULL;
  970. struct srcimp_mgr *srcimp_mgr = NULL;
  971. struct srcimp *srcimp = NULL;
  972. struct ct_mixer *mixer = NULL;
  973. /* disconnect internal mixer objects */
  974. if (atc->mixer) {
  975. mixer = atc->mixer;
  976. mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
  977. mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
  978. mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
  979. mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
  980. mixer->set_input_left(mixer, MIX_SPDIF_IN, NULL);
  981. mixer->set_input_right(mixer, MIX_SPDIF_IN, NULL);
  982. }
  983. if (atc->daios) {
  984. daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
  985. for (i = 0; i < atc->n_daio; i++) {
  986. daio = atc->daios[i];
  987. if (daio->type < LINEIM) {
  988. dao = container_of(daio, struct dao, daio);
  989. dao->ops->clear_left_input(dao);
  990. dao->ops->clear_right_input(dao);
  991. }
  992. daio_mgr->put_daio(daio_mgr, daio);
  993. }
  994. kfree(atc->daios);
  995. atc->daios = NULL;
  996. }
  997. if (atc->pcm) {
  998. sum_mgr = atc->rsc_mgrs[SUM];
  999. for (i = 0; i < atc->n_pcm; i++)
  1000. sum_mgr->put_sum(sum_mgr, atc->pcm[i]);
  1001. kfree(atc->pcm);
  1002. atc->pcm = NULL;
  1003. }
  1004. if (atc->srcs) {
  1005. src_mgr = atc->rsc_mgrs[SRC];
  1006. for (i = 0; i < atc->n_src; i++)
  1007. src_mgr->put_src(src_mgr, atc->srcs[i]);
  1008. kfree(atc->srcs);
  1009. atc->srcs = NULL;
  1010. }
  1011. if (atc->srcimps) {
  1012. srcimp_mgr = atc->rsc_mgrs[SRCIMP];
  1013. for (i = 0; i < atc->n_srcimp; i++) {
  1014. srcimp = atc->srcimps[i];
  1015. srcimp->ops->unmap(srcimp);
  1016. srcimp_mgr->put_srcimp(srcimp_mgr, atc->srcimps[i]);
  1017. }
  1018. kfree(atc->srcimps);
  1019. atc->srcimps = NULL;
  1020. }
  1021. return 0;
  1022. }
  1023. static int ct_atc_destroy(struct ct_atc *atc)
  1024. {
  1025. int i = 0;
  1026. if (!atc)
  1027. return 0;
  1028. if (atc->timer) {
  1029. ct_timer_free(atc->timer);
  1030. atc->timer = NULL;
  1031. }
  1032. atc_release_resources(atc);
  1033. /* Destroy internal mixer objects */
  1034. if (atc->mixer)
  1035. ct_mixer_destroy(atc->mixer);
  1036. for (i = 0; i < NUM_RSCTYP; i++) {
  1037. if (rsc_mgr_funcs[i].destroy && atc->rsc_mgrs[i])
  1038. rsc_mgr_funcs[i].destroy(atc->rsc_mgrs[i]);
  1039. }
  1040. if (atc->hw)
  1041. destroy_hw_obj(atc->hw);
  1042. /* Destroy device virtual memory manager object */
  1043. if (atc->vm) {
  1044. ct_vm_destroy(atc->vm);
  1045. atc->vm = NULL;
  1046. }
  1047. kfree(atc);
  1048. return 0;
  1049. }
  1050. static int atc_dev_free(struct snd_device *dev)
  1051. {
  1052. struct ct_atc *atc = dev->device_data;
  1053. return ct_atc_destroy(atc);
  1054. }
  1055. static int atc_identify_card(struct ct_atc *atc, unsigned int ssid)
  1056. {
  1057. const struct snd_pci_quirk *p;
  1058. const struct snd_pci_quirk *list;
  1059. u16 vendor_id, device_id;
  1060. switch (atc->chip_type) {
  1061. case ATC20K1:
  1062. atc->chip_name = "20K1";
  1063. list = subsys_20k1_list;
  1064. break;
  1065. case ATC20K2:
  1066. atc->chip_name = "20K2";
  1067. list = subsys_20k2_list;
  1068. break;
  1069. default:
  1070. return -ENOENT;
  1071. }
  1072. if (ssid) {
  1073. vendor_id = ssid >> 16;
  1074. device_id = ssid & 0xffff;
  1075. } else {
  1076. vendor_id = atc->pci->subsystem_vendor;
  1077. device_id = atc->pci->subsystem_device;
  1078. }
  1079. p = snd_pci_quirk_lookup_id(vendor_id, device_id, list);
  1080. if (p) {
  1081. if (p->value < 0) {
  1082. dev_err(atc->card->dev,
  1083. "Device %04x:%04x is black-listed\n",
  1084. vendor_id, device_id);
  1085. return -ENOENT;
  1086. }
  1087. atc->model = p->value;
  1088. } else {
  1089. if (atc->chip_type == ATC20K1)
  1090. atc->model = CT20K1_UNKNOWN;
  1091. else
  1092. atc->model = CT20K2_UNKNOWN;
  1093. }
  1094. atc->model_name = ct_subsys_name[atc->model];
  1095. dev_info(atc->card->dev, "chip %s model %s (%04x:%04x) is found\n",
  1096. atc->chip_name, atc->model_name,
  1097. vendor_id, device_id);
  1098. return 0;
  1099. }
  1100. int ct_atc_create_alsa_devs(struct ct_atc *atc)
  1101. {
  1102. enum CTALSADEVS i;
  1103. int err;
  1104. alsa_dev_funcs[MIXER].public_name = atc->chip_name;
  1105. for (i = 0; i < NUM_CTALSADEVS; i++) {
  1106. if (!alsa_dev_funcs[i].create)
  1107. continue;
  1108. err = alsa_dev_funcs[i].create(atc, i,
  1109. alsa_dev_funcs[i].public_name);
  1110. if (err) {
  1111. dev_err(atc->card->dev,
  1112. "Creating alsa device %d failed!\n", i);
  1113. return err;
  1114. }
  1115. }
  1116. return 0;
  1117. }
  1118. static int atc_create_hw_devs(struct ct_atc *atc)
  1119. {
  1120. struct hw *hw;
  1121. struct card_conf info = {0};
  1122. int i, err;
  1123. err = create_hw_obj(atc->pci, atc->chip_type, atc->model, &hw);
  1124. if (err) {
  1125. dev_err(atc->card->dev, "Failed to create hw obj!!!\n");
  1126. return err;
  1127. }
  1128. hw->card = atc->card;
  1129. atc->hw = hw;
  1130. /* Initialize card hardware. */
  1131. info.rsr = atc->rsr;
  1132. info.msr = atc->msr;
  1133. info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
  1134. err = hw->card_init(hw, &info);
  1135. if (err < 0)
  1136. return err;
  1137. for (i = 0; i < NUM_RSCTYP; i++) {
  1138. if (!rsc_mgr_funcs[i].create)
  1139. continue;
  1140. err = rsc_mgr_funcs[i].create(atc->hw, &atc->rsc_mgrs[i]);
  1141. if (err) {
  1142. dev_err(atc->card->dev,
  1143. "Failed to create rsc_mgr %d!!!\n", i);
  1144. return err;
  1145. }
  1146. }
  1147. return 0;
  1148. }
  1149. static int atc_get_resources(struct ct_atc *atc)
  1150. {
  1151. struct daio_desc da_desc = {0};
  1152. struct daio_mgr *daio_mgr;
  1153. struct src_desc src_dsc = {0};
  1154. struct src_mgr *src_mgr;
  1155. struct srcimp_desc srcimp_dsc = {0};
  1156. struct srcimp_mgr *srcimp_mgr;
  1157. struct sum_desc sum_dsc = {0};
  1158. struct sum_mgr *sum_mgr;
  1159. int err, i, num_srcs, num_daios;
  1160. num_daios = ((atc->model == CTSB1270) ? 8 : 7);
  1161. num_srcs = ((atc->model == CTSB1270) ? 6 : 4);
  1162. atc->daios = kzalloc(sizeof(void *)*num_daios, GFP_KERNEL);
  1163. if (!atc->daios)
  1164. return -ENOMEM;
  1165. atc->srcs = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
  1166. if (!atc->srcs)
  1167. return -ENOMEM;
  1168. atc->srcimps = kzalloc(sizeof(void *)*num_srcs, GFP_KERNEL);
  1169. if (!atc->srcimps)
  1170. return -ENOMEM;
  1171. atc->pcm = kzalloc(sizeof(void *)*(2*4), GFP_KERNEL);
  1172. if (!atc->pcm)
  1173. return -ENOMEM;
  1174. daio_mgr = (struct daio_mgr *)atc->rsc_mgrs[DAIO];
  1175. da_desc.msr = atc->msr;
  1176. for (i = 0, atc->n_daio = 0; i < num_daios; i++) {
  1177. da_desc.type = (atc->model != CTSB073X) ? i :
  1178. ((i == SPDIFIO) ? SPDIFI1 : i);
  1179. err = daio_mgr->get_daio(daio_mgr, &da_desc,
  1180. (struct daio **)&atc->daios[i]);
  1181. if (err) {
  1182. dev_err(atc->card->dev,
  1183. "Failed to get DAIO resource %d!!!\n",
  1184. i);
  1185. return err;
  1186. }
  1187. atc->n_daio++;
  1188. }
  1189. src_mgr = atc->rsc_mgrs[SRC];
  1190. src_dsc.multi = 1;
  1191. src_dsc.msr = atc->msr;
  1192. src_dsc.mode = ARCRW;
  1193. for (i = 0, atc->n_src = 0; i < num_srcs; i++) {
  1194. err = src_mgr->get_src(src_mgr, &src_dsc,
  1195. (struct src **)&atc->srcs[i]);
  1196. if (err)
  1197. return err;
  1198. atc->n_src++;
  1199. }
  1200. srcimp_mgr = atc->rsc_mgrs[SRCIMP];
  1201. srcimp_dsc.msr = 8;
  1202. for (i = 0, atc->n_srcimp = 0; i < num_srcs; i++) {
  1203. err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc,
  1204. (struct srcimp **)&atc->srcimps[i]);
  1205. if (err)
  1206. return err;
  1207. atc->n_srcimp++;
  1208. }
  1209. sum_mgr = atc->rsc_mgrs[SUM];
  1210. sum_dsc.msr = atc->msr;
  1211. for (i = 0, atc->n_pcm = 0; i < (2*4); i++) {
  1212. err = sum_mgr->get_sum(sum_mgr, &sum_dsc,
  1213. (struct sum **)&atc->pcm[i]);
  1214. if (err)
  1215. return err;
  1216. atc->n_pcm++;
  1217. }
  1218. return 0;
  1219. }
  1220. static void
  1221. atc_connect_dai(struct src_mgr *src_mgr, struct dai *dai,
  1222. struct src **srcs, struct srcimp **srcimps)
  1223. {
  1224. struct rsc *rscs[2] = {NULL};
  1225. struct src *src;
  1226. struct srcimp *srcimp;
  1227. int i = 0;
  1228. rscs[0] = &dai->daio.rscl;
  1229. rscs[1] = &dai->daio.rscr;
  1230. for (i = 0; i < 2; i++) {
  1231. src = srcs[i];
  1232. srcimp = srcimps[i];
  1233. srcimp->ops->map(srcimp, src, rscs[i]);
  1234. src_mgr->src_disable(src_mgr, src);
  1235. }
  1236. src_mgr->commit_write(src_mgr); /* Actually disable SRCs */
  1237. src = srcs[0];
  1238. src->ops->set_pm(src, 1);
  1239. for (i = 0; i < 2; i++) {
  1240. src = srcs[i];
  1241. src->ops->set_state(src, SRC_STATE_RUN);
  1242. src->ops->commit_write(src);
  1243. src_mgr->src_enable_s(src_mgr, src);
  1244. }
  1245. dai->ops->set_srt_srcl(dai, &(srcs[0]->rsc));
  1246. dai->ops->set_srt_srcr(dai, &(srcs[1]->rsc));
  1247. dai->ops->set_enb_src(dai, 1);
  1248. dai->ops->set_enb_srt(dai, 1);
  1249. dai->ops->commit_write(dai);
  1250. src_mgr->commit_write(src_mgr); /* Synchronously enable SRCs */
  1251. }
  1252. static void atc_connect_resources(struct ct_atc *atc)
  1253. {
  1254. struct dai *dai;
  1255. struct dao *dao;
  1256. struct src *src;
  1257. struct sum *sum;
  1258. struct ct_mixer *mixer;
  1259. struct rsc *rscs[2] = {NULL};
  1260. int i, j;
  1261. mixer = atc->mixer;
  1262. for (i = MIX_WAVE_FRONT, j = LINEO1; i <= MIX_SPDIF_OUT; i++, j++) {
  1263. mixer->get_output_ports(mixer, i, &rscs[0], &rscs[1]);
  1264. dao = container_of(atc->daios[j], struct dao, daio);
  1265. dao->ops->set_left_input(dao, rscs[0]);
  1266. dao->ops->set_right_input(dao, rscs[1]);
  1267. }
  1268. dai = container_of(atc->daios[LINEIM], struct dai, daio);
  1269. atc_connect_dai(atc->rsc_mgrs[SRC], dai,
  1270. (struct src **)&atc->srcs[2],
  1271. (struct srcimp **)&atc->srcimps[2]);
  1272. src = atc->srcs[2];
  1273. mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
  1274. src = atc->srcs[3];
  1275. mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
  1276. if (atc->model == CTSB1270) {
  1277. /* Titanium HD has a dedicated ADC for the Mic. */
  1278. dai = container_of(atc->daios[MIC], struct dai, daio);
  1279. atc_connect_dai(atc->rsc_mgrs[SRC], dai,
  1280. (struct src **)&atc->srcs[4],
  1281. (struct srcimp **)&atc->srcimps[4]);
  1282. src = atc->srcs[4];
  1283. mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
  1284. src = atc->srcs[5];
  1285. mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
  1286. }
  1287. dai = container_of(atc->daios[SPDIFIO], struct dai, daio);
  1288. atc_connect_dai(atc->rsc_mgrs[SRC], dai,
  1289. (struct src **)&atc->srcs[0],
  1290. (struct srcimp **)&atc->srcimps[0]);
  1291. src = atc->srcs[0];
  1292. mixer->set_input_left(mixer, MIX_SPDIF_IN, &src->rsc);
  1293. src = atc->srcs[1];
  1294. mixer->set_input_right(mixer, MIX_SPDIF_IN, &src->rsc);
  1295. for (i = MIX_PCMI_FRONT, j = 0; i <= MIX_PCMI_SURROUND; i++, j += 2) {
  1296. sum = atc->pcm[j];
  1297. mixer->set_input_left(mixer, i, &sum->rsc);
  1298. sum = atc->pcm[j+1];
  1299. mixer->set_input_right(mixer, i, &sum->rsc);
  1300. }
  1301. }
  1302. #ifdef CONFIG_PM_SLEEP
  1303. static int atc_suspend(struct ct_atc *atc)
  1304. {
  1305. int i;
  1306. struct hw *hw = atc->hw;
  1307. snd_power_change_state(atc->card, SNDRV_CTL_POWER_D3hot);
  1308. for (i = FRONT; i < NUM_PCMS; i++) {
  1309. if (!atc->pcms[i])
  1310. continue;
  1311. snd_pcm_suspend_all(atc->pcms[i]);
  1312. }
  1313. atc_release_resources(atc);
  1314. hw->suspend(hw);
  1315. return 0;
  1316. }
  1317. static int atc_hw_resume(struct ct_atc *atc)
  1318. {
  1319. struct hw *hw = atc->hw;
  1320. struct card_conf info = {0};
  1321. /* Re-initialize card hardware. */
  1322. info.rsr = atc->rsr;
  1323. info.msr = atc->msr;
  1324. info.vm_pgt_phys = atc_get_ptp_phys(atc, 0);
  1325. return hw->resume(hw, &info);
  1326. }
  1327. static int atc_resources_resume(struct ct_atc *atc)
  1328. {
  1329. struct ct_mixer *mixer;
  1330. int err = 0;
  1331. /* Get resources */
  1332. err = atc_get_resources(atc);
  1333. if (err < 0) {
  1334. atc_release_resources(atc);
  1335. return err;
  1336. }
  1337. /* Build topology */
  1338. atc_connect_resources(atc);
  1339. mixer = atc->mixer;
  1340. mixer->resume(mixer);
  1341. return 0;
  1342. }
  1343. static int atc_resume(struct ct_atc *atc)
  1344. {
  1345. int err = 0;
  1346. /* Do hardware resume. */
  1347. err = atc_hw_resume(atc);
  1348. if (err < 0) {
  1349. dev_err(atc->card->dev,
  1350. "pci_enable_device failed, disabling device\n");
  1351. snd_card_disconnect(atc->card);
  1352. return err;
  1353. }
  1354. err = atc_resources_resume(atc);
  1355. if (err < 0)
  1356. return err;
  1357. snd_power_change_state(atc->card, SNDRV_CTL_POWER_D0);
  1358. return 0;
  1359. }
  1360. #endif
  1361. static struct ct_atc atc_preset = {
  1362. .map_audio_buffer = ct_map_audio_buffer,
  1363. .unmap_audio_buffer = ct_unmap_audio_buffer,
  1364. .pcm_playback_prepare = atc_pcm_playback_prepare,
  1365. .pcm_release_resources = atc_pcm_release_resources,
  1366. .pcm_playback_start = atc_pcm_playback_start,
  1367. .pcm_playback_stop = atc_pcm_stop,
  1368. .pcm_playback_position = atc_pcm_playback_position,
  1369. .pcm_capture_prepare = atc_pcm_capture_prepare,
  1370. .pcm_capture_start = atc_pcm_capture_start,
  1371. .pcm_capture_stop = atc_pcm_stop,
  1372. .pcm_capture_position = atc_pcm_capture_position,
  1373. .spdif_passthru_playback_prepare = spdif_passthru_playback_prepare,
  1374. .get_ptp_phys = atc_get_ptp_phys,
  1375. .select_line_in = atc_select_line_in,
  1376. .select_mic_in = atc_select_mic_in,
  1377. .select_digit_io = atc_select_digit_io,
  1378. .line_front_unmute = atc_line_front_unmute,
  1379. .line_surround_unmute = atc_line_surround_unmute,
  1380. .line_clfe_unmute = atc_line_clfe_unmute,
  1381. .line_rear_unmute = atc_line_rear_unmute,
  1382. .line_in_unmute = atc_line_in_unmute,
  1383. .mic_unmute = atc_mic_unmute,
  1384. .spdif_out_unmute = atc_spdif_out_unmute,
  1385. .spdif_in_unmute = atc_spdif_in_unmute,
  1386. .spdif_out_get_status = atc_spdif_out_get_status,
  1387. .spdif_out_set_status = atc_spdif_out_set_status,
  1388. .spdif_out_passthru = atc_spdif_out_passthru,
  1389. .capabilities = atc_capabilities,
  1390. .output_switch_get = atc_output_switch_get,
  1391. .output_switch_put = atc_output_switch_put,
  1392. .mic_source_switch_get = atc_mic_source_switch_get,
  1393. .mic_source_switch_put = atc_mic_source_switch_put,
  1394. #ifdef CONFIG_PM_SLEEP
  1395. .suspend = atc_suspend,
  1396. .resume = atc_resume,
  1397. #endif
  1398. };
  1399. /**
  1400. * ct_atc_create - create and initialize a hardware manager
  1401. * @card: corresponding alsa card object
  1402. * @pci: corresponding kernel pci device object
  1403. * @ratc: return created object address in it
  1404. *
  1405. * Creates and initializes a hardware manager.
  1406. *
  1407. * Creates kmallocated ct_atc structure. Initializes hardware.
  1408. * Returns 0 if succeeds, or negative error code if fails.
  1409. */
  1410. int ct_atc_create(struct snd_card *card, struct pci_dev *pci,
  1411. unsigned int rsr, unsigned int msr,
  1412. int chip_type, unsigned int ssid,
  1413. struct ct_atc **ratc)
  1414. {
  1415. struct ct_atc *atc;
  1416. static struct snd_device_ops ops = {
  1417. .dev_free = atc_dev_free,
  1418. };
  1419. int err;
  1420. *ratc = NULL;
  1421. atc = kzalloc(sizeof(*atc), GFP_KERNEL);
  1422. if (!atc)
  1423. return -ENOMEM;
  1424. /* Set operations */
  1425. *atc = atc_preset;
  1426. atc->card = card;
  1427. atc->pci = pci;
  1428. atc->rsr = rsr;
  1429. atc->msr = msr;
  1430. atc->chip_type = chip_type;
  1431. mutex_init(&atc->atc_mutex);
  1432. /* Find card model */
  1433. err = atc_identify_card(atc, ssid);
  1434. if (err < 0) {
  1435. dev_err(card->dev, "ctatc: Card not recognised\n");
  1436. goto error1;
  1437. }
  1438. /* Set up device virtual memory management object */
  1439. err = ct_vm_create(&atc->vm, pci);
  1440. if (err < 0)
  1441. goto error1;
  1442. /* Create all atc hw devices */
  1443. err = atc_create_hw_devs(atc);
  1444. if (err < 0)
  1445. goto error1;
  1446. err = ct_mixer_create(atc, (struct ct_mixer **)&atc->mixer);
  1447. if (err) {
  1448. dev_err(card->dev, "Failed to create mixer obj!!!\n");
  1449. goto error1;
  1450. }
  1451. /* Get resources */
  1452. err = atc_get_resources(atc);
  1453. if (err < 0)
  1454. goto error1;
  1455. /* Build topology */
  1456. atc_connect_resources(atc);
  1457. atc->timer = ct_timer_new(atc);
  1458. if (!atc->timer) {
  1459. err = -ENOMEM;
  1460. goto error1;
  1461. }
  1462. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, atc, &ops);
  1463. if (err < 0)
  1464. goto error1;
  1465. *ratc = atc;
  1466. return 0;
  1467. error1:
  1468. ct_atc_destroy(atc);
  1469. dev_err(card->dev, "Something wrong!!!\n");
  1470. return err;
  1471. }