cthw20k2.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  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 cthw20k2.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of hardware access method for 20k2.
  12. *
  13. * @Author Liu Chun
  14. * @Date May 14 2008
  15. *
  16. */
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/pci.h>
  20. #include <linux/io.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include "cthw20k2.h"
  26. #include "ct20k2reg.h"
  27. struct hw20k2 {
  28. struct hw hw;
  29. /* for i2c */
  30. unsigned char dev_id;
  31. unsigned char addr_size;
  32. unsigned char data_size;
  33. int mic_source;
  34. };
  35. static u32 hw_read_20kx(struct hw *hw, u32 reg);
  36. static void hw_write_20kx(struct hw *hw, u32 reg, u32 data);
  37. /*
  38. * Type definition block.
  39. * The layout of control structures can be directly applied on 20k2 chip.
  40. */
  41. /*
  42. * SRC control block definitions.
  43. */
  44. /* SRC resource control block */
  45. #define SRCCTL_STATE 0x00000007
  46. #define SRCCTL_BM 0x00000008
  47. #define SRCCTL_RSR 0x00000030
  48. #define SRCCTL_SF 0x000001C0
  49. #define SRCCTL_WR 0x00000200
  50. #define SRCCTL_PM 0x00000400
  51. #define SRCCTL_ROM 0x00001800
  52. #define SRCCTL_VO 0x00002000
  53. #define SRCCTL_ST 0x00004000
  54. #define SRCCTL_IE 0x00008000
  55. #define SRCCTL_ILSZ 0x000F0000
  56. #define SRCCTL_BP 0x00100000
  57. #define SRCCCR_CISZ 0x000007FF
  58. #define SRCCCR_CWA 0x001FF800
  59. #define SRCCCR_D 0x00200000
  60. #define SRCCCR_RS 0x01C00000
  61. #define SRCCCR_NAL 0x3E000000
  62. #define SRCCCR_RA 0xC0000000
  63. #define SRCCA_CA 0x0FFFFFFF
  64. #define SRCCA_RS 0xE0000000
  65. #define SRCSA_SA 0x0FFFFFFF
  66. #define SRCLA_LA 0x0FFFFFFF
  67. /* Mixer Parameter Ring ram Low and Hight register.
  68. * Fixed-point value in 8.24 format for parameter channel */
  69. #define MPRLH_PITCH 0xFFFFFFFF
  70. /* SRC resource register dirty flags */
  71. union src_dirty {
  72. struct {
  73. u16 ctl:1;
  74. u16 ccr:1;
  75. u16 sa:1;
  76. u16 la:1;
  77. u16 ca:1;
  78. u16 mpr:1;
  79. u16 czbfs:1; /* Clear Z-Buffers */
  80. u16 rsv:9;
  81. } bf;
  82. u16 data;
  83. };
  84. struct src_rsc_ctrl_blk {
  85. unsigned int ctl;
  86. unsigned int ccr;
  87. unsigned int ca;
  88. unsigned int sa;
  89. unsigned int la;
  90. unsigned int mpr;
  91. union src_dirty dirty;
  92. };
  93. /* SRC manager control block */
  94. union src_mgr_dirty {
  95. struct {
  96. u16 enb0:1;
  97. u16 enb1:1;
  98. u16 enb2:1;
  99. u16 enb3:1;
  100. u16 enb4:1;
  101. u16 enb5:1;
  102. u16 enb6:1;
  103. u16 enb7:1;
  104. u16 enbsa:1;
  105. u16 rsv:7;
  106. } bf;
  107. u16 data;
  108. };
  109. struct src_mgr_ctrl_blk {
  110. unsigned int enbsa;
  111. unsigned int enb[8];
  112. union src_mgr_dirty dirty;
  113. };
  114. /* SRCIMP manager control block */
  115. #define SRCAIM_ARC 0x00000FFF
  116. #define SRCAIM_NXT 0x00FF0000
  117. #define SRCAIM_SRC 0xFF000000
  118. struct srcimap {
  119. unsigned int srcaim;
  120. unsigned int idx;
  121. };
  122. /* SRCIMP manager register dirty flags */
  123. union srcimp_mgr_dirty {
  124. struct {
  125. u16 srcimap:1;
  126. u16 rsv:15;
  127. } bf;
  128. u16 data;
  129. };
  130. struct srcimp_mgr_ctrl_blk {
  131. struct srcimap srcimap;
  132. union srcimp_mgr_dirty dirty;
  133. };
  134. /*
  135. * Function implementation block.
  136. */
  137. static int src_get_rsc_ctrl_blk(void **rblk)
  138. {
  139. struct src_rsc_ctrl_blk *blk;
  140. *rblk = NULL;
  141. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  142. if (!blk)
  143. return -ENOMEM;
  144. *rblk = blk;
  145. return 0;
  146. }
  147. static int src_put_rsc_ctrl_blk(void *blk)
  148. {
  149. kfree(blk);
  150. return 0;
  151. }
  152. static int src_set_state(void *blk, unsigned int state)
  153. {
  154. struct src_rsc_ctrl_blk *ctl = blk;
  155. set_field(&ctl->ctl, SRCCTL_STATE, state);
  156. ctl->dirty.bf.ctl = 1;
  157. return 0;
  158. }
  159. static int src_set_bm(void *blk, unsigned int bm)
  160. {
  161. struct src_rsc_ctrl_blk *ctl = blk;
  162. set_field(&ctl->ctl, SRCCTL_BM, bm);
  163. ctl->dirty.bf.ctl = 1;
  164. return 0;
  165. }
  166. static int src_set_rsr(void *blk, unsigned int rsr)
  167. {
  168. struct src_rsc_ctrl_blk *ctl = blk;
  169. set_field(&ctl->ctl, SRCCTL_RSR, rsr);
  170. ctl->dirty.bf.ctl = 1;
  171. return 0;
  172. }
  173. static int src_set_sf(void *blk, unsigned int sf)
  174. {
  175. struct src_rsc_ctrl_blk *ctl = blk;
  176. set_field(&ctl->ctl, SRCCTL_SF, sf);
  177. ctl->dirty.bf.ctl = 1;
  178. return 0;
  179. }
  180. static int src_set_wr(void *blk, unsigned int wr)
  181. {
  182. struct src_rsc_ctrl_blk *ctl = blk;
  183. set_field(&ctl->ctl, SRCCTL_WR, wr);
  184. ctl->dirty.bf.ctl = 1;
  185. return 0;
  186. }
  187. static int src_set_pm(void *blk, unsigned int pm)
  188. {
  189. struct src_rsc_ctrl_blk *ctl = blk;
  190. set_field(&ctl->ctl, SRCCTL_PM, pm);
  191. ctl->dirty.bf.ctl = 1;
  192. return 0;
  193. }
  194. static int src_set_rom(void *blk, unsigned int rom)
  195. {
  196. struct src_rsc_ctrl_blk *ctl = blk;
  197. set_field(&ctl->ctl, SRCCTL_ROM, rom);
  198. ctl->dirty.bf.ctl = 1;
  199. return 0;
  200. }
  201. static int src_set_vo(void *blk, unsigned int vo)
  202. {
  203. struct src_rsc_ctrl_blk *ctl = blk;
  204. set_field(&ctl->ctl, SRCCTL_VO, vo);
  205. ctl->dirty.bf.ctl = 1;
  206. return 0;
  207. }
  208. static int src_set_st(void *blk, unsigned int st)
  209. {
  210. struct src_rsc_ctrl_blk *ctl = blk;
  211. set_field(&ctl->ctl, SRCCTL_ST, st);
  212. ctl->dirty.bf.ctl = 1;
  213. return 0;
  214. }
  215. static int src_set_ie(void *blk, unsigned int ie)
  216. {
  217. struct src_rsc_ctrl_blk *ctl = blk;
  218. set_field(&ctl->ctl, SRCCTL_IE, ie);
  219. ctl->dirty.bf.ctl = 1;
  220. return 0;
  221. }
  222. static int src_set_ilsz(void *blk, unsigned int ilsz)
  223. {
  224. struct src_rsc_ctrl_blk *ctl = blk;
  225. set_field(&ctl->ctl, SRCCTL_ILSZ, ilsz);
  226. ctl->dirty.bf.ctl = 1;
  227. return 0;
  228. }
  229. static int src_set_bp(void *blk, unsigned int bp)
  230. {
  231. struct src_rsc_ctrl_blk *ctl = blk;
  232. set_field(&ctl->ctl, SRCCTL_BP, bp);
  233. ctl->dirty.bf.ctl = 1;
  234. return 0;
  235. }
  236. static int src_set_cisz(void *blk, unsigned int cisz)
  237. {
  238. struct src_rsc_ctrl_blk *ctl = blk;
  239. set_field(&ctl->ccr, SRCCCR_CISZ, cisz);
  240. ctl->dirty.bf.ccr = 1;
  241. return 0;
  242. }
  243. static int src_set_ca(void *blk, unsigned int ca)
  244. {
  245. struct src_rsc_ctrl_blk *ctl = blk;
  246. set_field(&ctl->ca, SRCCA_CA, ca);
  247. ctl->dirty.bf.ca = 1;
  248. return 0;
  249. }
  250. static int src_set_sa(void *blk, unsigned int sa)
  251. {
  252. struct src_rsc_ctrl_blk *ctl = blk;
  253. set_field(&ctl->sa, SRCSA_SA, sa);
  254. ctl->dirty.bf.sa = 1;
  255. return 0;
  256. }
  257. static int src_set_la(void *blk, unsigned int la)
  258. {
  259. struct src_rsc_ctrl_blk *ctl = blk;
  260. set_field(&ctl->la, SRCLA_LA, la);
  261. ctl->dirty.bf.la = 1;
  262. return 0;
  263. }
  264. static int src_set_pitch(void *blk, unsigned int pitch)
  265. {
  266. struct src_rsc_ctrl_blk *ctl = blk;
  267. set_field(&ctl->mpr, MPRLH_PITCH, pitch);
  268. ctl->dirty.bf.mpr = 1;
  269. return 0;
  270. }
  271. static int src_set_clear_zbufs(void *blk, unsigned int clear)
  272. {
  273. ((struct src_rsc_ctrl_blk *)blk)->dirty.bf.czbfs = (clear ? 1 : 0);
  274. return 0;
  275. }
  276. static int src_set_dirty(void *blk, unsigned int flags)
  277. {
  278. ((struct src_rsc_ctrl_blk *)blk)->dirty.data = (flags & 0xffff);
  279. return 0;
  280. }
  281. static int src_set_dirty_all(void *blk)
  282. {
  283. ((struct src_rsc_ctrl_blk *)blk)->dirty.data = ~(0x0);
  284. return 0;
  285. }
  286. #define AR_SLOT_SIZE 4096
  287. #define AR_SLOT_BLOCK_SIZE 16
  288. #define AR_PTS_PITCH 6
  289. #define AR_PARAM_SRC_OFFSET 0x60
  290. static unsigned int src_param_pitch_mixer(unsigned int src_idx)
  291. {
  292. return ((src_idx << 4) + AR_PTS_PITCH + AR_SLOT_SIZE
  293. - AR_PARAM_SRC_OFFSET) % AR_SLOT_SIZE;
  294. }
  295. static int src_commit_write(struct hw *hw, unsigned int idx, void *blk)
  296. {
  297. struct src_rsc_ctrl_blk *ctl = blk;
  298. int i;
  299. if (ctl->dirty.bf.czbfs) {
  300. /* Clear Z-Buffer registers */
  301. for (i = 0; i < 8; i++)
  302. hw_write_20kx(hw, SRC_UPZ+idx*0x100+i*0x4, 0);
  303. for (i = 0; i < 4; i++)
  304. hw_write_20kx(hw, SRC_DN0Z+idx*0x100+i*0x4, 0);
  305. for (i = 0; i < 8; i++)
  306. hw_write_20kx(hw, SRC_DN1Z+idx*0x100+i*0x4, 0);
  307. ctl->dirty.bf.czbfs = 0;
  308. }
  309. if (ctl->dirty.bf.mpr) {
  310. /* Take the parameter mixer resource in the same group as that
  311. * the idx src is in for simplicity. Unlike src, all conjugate
  312. * parameter mixer resources must be programmed for
  313. * corresponding conjugate src resources. */
  314. unsigned int pm_idx = src_param_pitch_mixer(idx);
  315. hw_write_20kx(hw, MIXER_PRING_LO_HI+4*pm_idx, ctl->mpr);
  316. hw_write_20kx(hw, MIXER_PMOPLO+8*pm_idx, 0x3);
  317. hw_write_20kx(hw, MIXER_PMOPHI+8*pm_idx, 0x0);
  318. ctl->dirty.bf.mpr = 0;
  319. }
  320. if (ctl->dirty.bf.sa) {
  321. hw_write_20kx(hw, SRC_SA+idx*0x100, ctl->sa);
  322. ctl->dirty.bf.sa = 0;
  323. }
  324. if (ctl->dirty.bf.la) {
  325. hw_write_20kx(hw, SRC_LA+idx*0x100, ctl->la);
  326. ctl->dirty.bf.la = 0;
  327. }
  328. if (ctl->dirty.bf.ca) {
  329. hw_write_20kx(hw, SRC_CA+idx*0x100, ctl->ca);
  330. ctl->dirty.bf.ca = 0;
  331. }
  332. /* Write srccf register */
  333. hw_write_20kx(hw, SRC_CF+idx*0x100, 0x0);
  334. if (ctl->dirty.bf.ccr) {
  335. hw_write_20kx(hw, SRC_CCR+idx*0x100, ctl->ccr);
  336. ctl->dirty.bf.ccr = 0;
  337. }
  338. if (ctl->dirty.bf.ctl) {
  339. hw_write_20kx(hw, SRC_CTL+idx*0x100, ctl->ctl);
  340. ctl->dirty.bf.ctl = 0;
  341. }
  342. return 0;
  343. }
  344. static int src_get_ca(struct hw *hw, unsigned int idx, void *blk)
  345. {
  346. struct src_rsc_ctrl_blk *ctl = blk;
  347. ctl->ca = hw_read_20kx(hw, SRC_CA+idx*0x100);
  348. ctl->dirty.bf.ca = 0;
  349. return get_field(ctl->ca, SRCCA_CA);
  350. }
  351. static unsigned int src_get_dirty(void *blk)
  352. {
  353. return ((struct src_rsc_ctrl_blk *)blk)->dirty.data;
  354. }
  355. static unsigned int src_dirty_conj_mask(void)
  356. {
  357. return 0x20;
  358. }
  359. static int src_mgr_enbs_src(void *blk, unsigned int idx)
  360. {
  361. ((struct src_mgr_ctrl_blk *)blk)->enbsa |= (0x1 << ((idx%128)/4));
  362. ((struct src_mgr_ctrl_blk *)blk)->dirty.bf.enbsa = 1;
  363. ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] |= (0x1 << (idx%32));
  364. return 0;
  365. }
  366. static int src_mgr_enb_src(void *blk, unsigned int idx)
  367. {
  368. ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] |= (0x1 << (idx%32));
  369. ((struct src_mgr_ctrl_blk *)blk)->dirty.data |= (0x1 << (idx/32));
  370. return 0;
  371. }
  372. static int src_mgr_dsb_src(void *blk, unsigned int idx)
  373. {
  374. ((struct src_mgr_ctrl_blk *)blk)->enb[idx/32] &= ~(0x1 << (idx%32));
  375. ((struct src_mgr_ctrl_blk *)blk)->dirty.data |= (0x1 << (idx/32));
  376. return 0;
  377. }
  378. static int src_mgr_commit_write(struct hw *hw, void *blk)
  379. {
  380. struct src_mgr_ctrl_blk *ctl = blk;
  381. int i;
  382. unsigned int ret;
  383. if (ctl->dirty.bf.enbsa) {
  384. do {
  385. ret = hw_read_20kx(hw, SRC_ENBSTAT);
  386. } while (ret & 0x1);
  387. hw_write_20kx(hw, SRC_ENBSA, ctl->enbsa);
  388. ctl->dirty.bf.enbsa = 0;
  389. }
  390. for (i = 0; i < 8; i++) {
  391. if ((ctl->dirty.data & (0x1 << i))) {
  392. hw_write_20kx(hw, SRC_ENB+(i*0x100), ctl->enb[i]);
  393. ctl->dirty.data &= ~(0x1 << i);
  394. }
  395. }
  396. return 0;
  397. }
  398. static int src_mgr_get_ctrl_blk(void **rblk)
  399. {
  400. struct src_mgr_ctrl_blk *blk;
  401. *rblk = NULL;
  402. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  403. if (!blk)
  404. return -ENOMEM;
  405. *rblk = blk;
  406. return 0;
  407. }
  408. static int src_mgr_put_ctrl_blk(void *blk)
  409. {
  410. kfree(blk);
  411. return 0;
  412. }
  413. static int srcimp_mgr_get_ctrl_blk(void **rblk)
  414. {
  415. struct srcimp_mgr_ctrl_blk *blk;
  416. *rblk = NULL;
  417. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  418. if (!blk)
  419. return -ENOMEM;
  420. *rblk = blk;
  421. return 0;
  422. }
  423. static int srcimp_mgr_put_ctrl_blk(void *blk)
  424. {
  425. kfree(blk);
  426. return 0;
  427. }
  428. static int srcimp_mgr_set_imaparc(void *blk, unsigned int slot)
  429. {
  430. struct srcimp_mgr_ctrl_blk *ctl = blk;
  431. set_field(&ctl->srcimap.srcaim, SRCAIM_ARC, slot);
  432. ctl->dirty.bf.srcimap = 1;
  433. return 0;
  434. }
  435. static int srcimp_mgr_set_imapuser(void *blk, unsigned int user)
  436. {
  437. struct srcimp_mgr_ctrl_blk *ctl = blk;
  438. set_field(&ctl->srcimap.srcaim, SRCAIM_SRC, user);
  439. ctl->dirty.bf.srcimap = 1;
  440. return 0;
  441. }
  442. static int srcimp_mgr_set_imapnxt(void *blk, unsigned int next)
  443. {
  444. struct srcimp_mgr_ctrl_blk *ctl = blk;
  445. set_field(&ctl->srcimap.srcaim, SRCAIM_NXT, next);
  446. ctl->dirty.bf.srcimap = 1;
  447. return 0;
  448. }
  449. static int srcimp_mgr_set_imapaddr(void *blk, unsigned int addr)
  450. {
  451. ((struct srcimp_mgr_ctrl_blk *)blk)->srcimap.idx = addr;
  452. ((struct srcimp_mgr_ctrl_blk *)blk)->dirty.bf.srcimap = 1;
  453. return 0;
  454. }
  455. static int srcimp_mgr_commit_write(struct hw *hw, void *blk)
  456. {
  457. struct srcimp_mgr_ctrl_blk *ctl = blk;
  458. if (ctl->dirty.bf.srcimap) {
  459. hw_write_20kx(hw, SRC_IMAP+ctl->srcimap.idx*0x100,
  460. ctl->srcimap.srcaim);
  461. ctl->dirty.bf.srcimap = 0;
  462. }
  463. return 0;
  464. }
  465. /*
  466. * AMIXER control block definitions.
  467. */
  468. #define AMOPLO_M 0x00000003
  469. #define AMOPLO_IV 0x00000004
  470. #define AMOPLO_X 0x0003FFF0
  471. #define AMOPLO_Y 0xFFFC0000
  472. #define AMOPHI_SADR 0x000000FF
  473. #define AMOPHI_SE 0x80000000
  474. /* AMIXER resource register dirty flags */
  475. union amixer_dirty {
  476. struct {
  477. u16 amoplo:1;
  478. u16 amophi:1;
  479. u16 rsv:14;
  480. } bf;
  481. u16 data;
  482. };
  483. /* AMIXER resource control block */
  484. struct amixer_rsc_ctrl_blk {
  485. unsigned int amoplo;
  486. unsigned int amophi;
  487. union amixer_dirty dirty;
  488. };
  489. static int amixer_set_mode(void *blk, unsigned int mode)
  490. {
  491. struct amixer_rsc_ctrl_blk *ctl = blk;
  492. set_field(&ctl->amoplo, AMOPLO_M, mode);
  493. ctl->dirty.bf.amoplo = 1;
  494. return 0;
  495. }
  496. static int amixer_set_iv(void *blk, unsigned int iv)
  497. {
  498. struct amixer_rsc_ctrl_blk *ctl = blk;
  499. set_field(&ctl->amoplo, AMOPLO_IV, iv);
  500. ctl->dirty.bf.amoplo = 1;
  501. return 0;
  502. }
  503. static int amixer_set_x(void *blk, unsigned int x)
  504. {
  505. struct amixer_rsc_ctrl_blk *ctl = blk;
  506. set_field(&ctl->amoplo, AMOPLO_X, x);
  507. ctl->dirty.bf.amoplo = 1;
  508. return 0;
  509. }
  510. static int amixer_set_y(void *blk, unsigned int y)
  511. {
  512. struct amixer_rsc_ctrl_blk *ctl = blk;
  513. set_field(&ctl->amoplo, AMOPLO_Y, y);
  514. ctl->dirty.bf.amoplo = 1;
  515. return 0;
  516. }
  517. static int amixer_set_sadr(void *blk, unsigned int sadr)
  518. {
  519. struct amixer_rsc_ctrl_blk *ctl = blk;
  520. set_field(&ctl->amophi, AMOPHI_SADR, sadr);
  521. ctl->dirty.bf.amophi = 1;
  522. return 0;
  523. }
  524. static int amixer_set_se(void *blk, unsigned int se)
  525. {
  526. struct amixer_rsc_ctrl_blk *ctl = blk;
  527. set_field(&ctl->amophi, AMOPHI_SE, se);
  528. ctl->dirty.bf.amophi = 1;
  529. return 0;
  530. }
  531. static int amixer_set_dirty(void *blk, unsigned int flags)
  532. {
  533. ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data = (flags & 0xffff);
  534. return 0;
  535. }
  536. static int amixer_set_dirty_all(void *blk)
  537. {
  538. ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data = ~(0x0);
  539. return 0;
  540. }
  541. static int amixer_commit_write(struct hw *hw, unsigned int idx, void *blk)
  542. {
  543. struct amixer_rsc_ctrl_blk *ctl = blk;
  544. if (ctl->dirty.bf.amoplo || ctl->dirty.bf.amophi) {
  545. hw_write_20kx(hw, MIXER_AMOPLO+idx*8, ctl->amoplo);
  546. ctl->dirty.bf.amoplo = 0;
  547. hw_write_20kx(hw, MIXER_AMOPHI+idx*8, ctl->amophi);
  548. ctl->dirty.bf.amophi = 0;
  549. }
  550. return 0;
  551. }
  552. static int amixer_get_y(void *blk)
  553. {
  554. struct amixer_rsc_ctrl_blk *ctl = blk;
  555. return get_field(ctl->amoplo, AMOPLO_Y);
  556. }
  557. static unsigned int amixer_get_dirty(void *blk)
  558. {
  559. return ((struct amixer_rsc_ctrl_blk *)blk)->dirty.data;
  560. }
  561. static int amixer_rsc_get_ctrl_blk(void **rblk)
  562. {
  563. struct amixer_rsc_ctrl_blk *blk;
  564. *rblk = NULL;
  565. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  566. if (!blk)
  567. return -ENOMEM;
  568. *rblk = blk;
  569. return 0;
  570. }
  571. static int amixer_rsc_put_ctrl_blk(void *blk)
  572. {
  573. kfree(blk);
  574. return 0;
  575. }
  576. static int amixer_mgr_get_ctrl_blk(void **rblk)
  577. {
  578. *rblk = NULL;
  579. return 0;
  580. }
  581. static int amixer_mgr_put_ctrl_blk(void *blk)
  582. {
  583. return 0;
  584. }
  585. /*
  586. * DAIO control block definitions.
  587. */
  588. /* Receiver Sample Rate Tracker Control register */
  589. #define SRTCTL_SRCO 0x000000FF
  590. #define SRTCTL_SRCM 0x0000FF00
  591. #define SRTCTL_RSR 0x00030000
  592. #define SRTCTL_DRAT 0x00300000
  593. #define SRTCTL_EC 0x01000000
  594. #define SRTCTL_ET 0x10000000
  595. /* DAIO Receiver register dirty flags */
  596. union dai_dirty {
  597. struct {
  598. u16 srt:1;
  599. u16 rsv:15;
  600. } bf;
  601. u16 data;
  602. };
  603. /* DAIO Receiver control block */
  604. struct dai_ctrl_blk {
  605. unsigned int srt;
  606. union dai_dirty dirty;
  607. };
  608. /* Audio Input Mapper RAM */
  609. #define AIM_ARC 0x00000FFF
  610. #define AIM_NXT 0x007F0000
  611. struct daoimap {
  612. unsigned int aim;
  613. unsigned int idx;
  614. };
  615. /* Audio Transmitter Control and Status register */
  616. #define ATXCTL_EN 0x00000001
  617. #define ATXCTL_MODE 0x00000010
  618. #define ATXCTL_CD 0x00000020
  619. #define ATXCTL_RAW 0x00000100
  620. #define ATXCTL_MT 0x00000200
  621. #define ATXCTL_NUC 0x00003000
  622. #define ATXCTL_BEN 0x00010000
  623. #define ATXCTL_BMUX 0x00700000
  624. #define ATXCTL_B24 0x01000000
  625. #define ATXCTL_CPF 0x02000000
  626. #define ATXCTL_RIV 0x10000000
  627. #define ATXCTL_LIV 0x20000000
  628. #define ATXCTL_RSAT 0x40000000
  629. #define ATXCTL_LSAT 0x80000000
  630. /* XDIF Transmitter register dirty flags */
  631. union dao_dirty {
  632. struct {
  633. u16 atxcsl:1;
  634. u16 rsv:15;
  635. } bf;
  636. u16 data;
  637. };
  638. /* XDIF Transmitter control block */
  639. struct dao_ctrl_blk {
  640. /* XDIF Transmitter Channel Status Low Register */
  641. unsigned int atxcsl;
  642. union dao_dirty dirty;
  643. };
  644. /* Audio Receiver Control register */
  645. #define ARXCTL_EN 0x00000001
  646. /* DAIO manager register dirty flags */
  647. union daio_mgr_dirty {
  648. struct {
  649. u32 atxctl:8;
  650. u32 arxctl:8;
  651. u32 daoimap:1;
  652. u32 rsv:15;
  653. } bf;
  654. u32 data;
  655. };
  656. /* DAIO manager control block */
  657. struct daio_mgr_ctrl_blk {
  658. struct daoimap daoimap;
  659. unsigned int txctl[8];
  660. unsigned int rxctl[8];
  661. union daio_mgr_dirty dirty;
  662. };
  663. static int dai_srt_set_srco(void *blk, unsigned int src)
  664. {
  665. struct dai_ctrl_blk *ctl = blk;
  666. set_field(&ctl->srt, SRTCTL_SRCO, src);
  667. ctl->dirty.bf.srt = 1;
  668. return 0;
  669. }
  670. static int dai_srt_set_srcm(void *blk, unsigned int src)
  671. {
  672. struct dai_ctrl_blk *ctl = blk;
  673. set_field(&ctl->srt, SRTCTL_SRCM, src);
  674. ctl->dirty.bf.srt = 1;
  675. return 0;
  676. }
  677. static int dai_srt_set_rsr(void *blk, unsigned int rsr)
  678. {
  679. struct dai_ctrl_blk *ctl = blk;
  680. set_field(&ctl->srt, SRTCTL_RSR, rsr);
  681. ctl->dirty.bf.srt = 1;
  682. return 0;
  683. }
  684. static int dai_srt_set_drat(void *blk, unsigned int drat)
  685. {
  686. struct dai_ctrl_blk *ctl = blk;
  687. set_field(&ctl->srt, SRTCTL_DRAT, drat);
  688. ctl->dirty.bf.srt = 1;
  689. return 0;
  690. }
  691. static int dai_srt_set_ec(void *blk, unsigned int ec)
  692. {
  693. struct dai_ctrl_blk *ctl = blk;
  694. set_field(&ctl->srt, SRTCTL_EC, ec ? 1 : 0);
  695. ctl->dirty.bf.srt = 1;
  696. return 0;
  697. }
  698. static int dai_srt_set_et(void *blk, unsigned int et)
  699. {
  700. struct dai_ctrl_blk *ctl = blk;
  701. set_field(&ctl->srt, SRTCTL_ET, et ? 1 : 0);
  702. ctl->dirty.bf.srt = 1;
  703. return 0;
  704. }
  705. static int dai_commit_write(struct hw *hw, unsigned int idx, void *blk)
  706. {
  707. struct dai_ctrl_blk *ctl = blk;
  708. if (ctl->dirty.bf.srt) {
  709. hw_write_20kx(hw, AUDIO_IO_RX_SRT_CTL+0x40*idx, ctl->srt);
  710. ctl->dirty.bf.srt = 0;
  711. }
  712. return 0;
  713. }
  714. static int dai_get_ctrl_blk(void **rblk)
  715. {
  716. struct dai_ctrl_blk *blk;
  717. *rblk = NULL;
  718. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  719. if (!blk)
  720. return -ENOMEM;
  721. *rblk = blk;
  722. return 0;
  723. }
  724. static int dai_put_ctrl_blk(void *blk)
  725. {
  726. kfree(blk);
  727. return 0;
  728. }
  729. static int dao_set_spos(void *blk, unsigned int spos)
  730. {
  731. ((struct dao_ctrl_blk *)blk)->atxcsl = spos;
  732. ((struct dao_ctrl_blk *)blk)->dirty.bf.atxcsl = 1;
  733. return 0;
  734. }
  735. static int dao_commit_write(struct hw *hw, unsigned int idx, void *blk)
  736. {
  737. struct dao_ctrl_blk *ctl = blk;
  738. if (ctl->dirty.bf.atxcsl) {
  739. if (idx < 4) {
  740. /* S/PDIF SPOSx */
  741. hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_L+0x40*idx,
  742. ctl->atxcsl);
  743. }
  744. ctl->dirty.bf.atxcsl = 0;
  745. }
  746. return 0;
  747. }
  748. static int dao_get_spos(void *blk, unsigned int *spos)
  749. {
  750. *spos = ((struct dao_ctrl_blk *)blk)->atxcsl;
  751. return 0;
  752. }
  753. static int dao_get_ctrl_blk(void **rblk)
  754. {
  755. struct dao_ctrl_blk *blk;
  756. *rblk = NULL;
  757. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  758. if (!blk)
  759. return -ENOMEM;
  760. *rblk = blk;
  761. return 0;
  762. }
  763. static int dao_put_ctrl_blk(void *blk)
  764. {
  765. kfree(blk);
  766. return 0;
  767. }
  768. static int daio_mgr_enb_dai(void *blk, unsigned int idx)
  769. {
  770. struct daio_mgr_ctrl_blk *ctl = blk;
  771. set_field(&ctl->rxctl[idx], ARXCTL_EN, 1);
  772. ctl->dirty.bf.arxctl |= (0x1 << idx);
  773. return 0;
  774. }
  775. static int daio_mgr_dsb_dai(void *blk, unsigned int idx)
  776. {
  777. struct daio_mgr_ctrl_blk *ctl = blk;
  778. set_field(&ctl->rxctl[idx], ARXCTL_EN, 0);
  779. ctl->dirty.bf.arxctl |= (0x1 << idx);
  780. return 0;
  781. }
  782. static int daio_mgr_enb_dao(void *blk, unsigned int idx)
  783. {
  784. struct daio_mgr_ctrl_blk *ctl = blk;
  785. set_field(&ctl->txctl[idx], ATXCTL_EN, 1);
  786. ctl->dirty.bf.atxctl |= (0x1 << idx);
  787. return 0;
  788. }
  789. static int daio_mgr_dsb_dao(void *blk, unsigned int idx)
  790. {
  791. struct daio_mgr_ctrl_blk *ctl = blk;
  792. set_field(&ctl->txctl[idx], ATXCTL_EN, 0);
  793. ctl->dirty.bf.atxctl |= (0x1 << idx);
  794. return 0;
  795. }
  796. static int daio_mgr_dao_init(void *blk, unsigned int idx, unsigned int conf)
  797. {
  798. struct daio_mgr_ctrl_blk *ctl = blk;
  799. if (idx < 4) {
  800. /* S/PDIF output */
  801. switch ((conf & 0xf)) {
  802. case 1:
  803. set_field(&ctl->txctl[idx], ATXCTL_NUC, 0);
  804. break;
  805. case 2:
  806. set_field(&ctl->txctl[idx], ATXCTL_NUC, 1);
  807. break;
  808. case 4:
  809. set_field(&ctl->txctl[idx], ATXCTL_NUC, 2);
  810. break;
  811. case 8:
  812. set_field(&ctl->txctl[idx], ATXCTL_NUC, 3);
  813. break;
  814. default:
  815. break;
  816. }
  817. /* CDIF */
  818. set_field(&ctl->txctl[idx], ATXCTL_CD, (!(conf & 0x7)));
  819. /* Non-audio */
  820. set_field(&ctl->txctl[idx], ATXCTL_LIV, (conf >> 4) & 0x1);
  821. /* Non-audio */
  822. set_field(&ctl->txctl[idx], ATXCTL_RIV, (conf >> 4) & 0x1);
  823. set_field(&ctl->txctl[idx], ATXCTL_RAW,
  824. ((conf >> 3) & 0x1) ? 0 : 0);
  825. ctl->dirty.bf.atxctl |= (0x1 << idx);
  826. } else {
  827. /* I2S output */
  828. /*idx %= 4; */
  829. }
  830. return 0;
  831. }
  832. static int daio_mgr_set_imaparc(void *blk, unsigned int slot)
  833. {
  834. struct daio_mgr_ctrl_blk *ctl = blk;
  835. set_field(&ctl->daoimap.aim, AIM_ARC, slot);
  836. ctl->dirty.bf.daoimap = 1;
  837. return 0;
  838. }
  839. static int daio_mgr_set_imapnxt(void *blk, unsigned int next)
  840. {
  841. struct daio_mgr_ctrl_blk *ctl = blk;
  842. set_field(&ctl->daoimap.aim, AIM_NXT, next);
  843. ctl->dirty.bf.daoimap = 1;
  844. return 0;
  845. }
  846. static int daio_mgr_set_imapaddr(void *blk, unsigned int addr)
  847. {
  848. ((struct daio_mgr_ctrl_blk *)blk)->daoimap.idx = addr;
  849. ((struct daio_mgr_ctrl_blk *)blk)->dirty.bf.daoimap = 1;
  850. return 0;
  851. }
  852. static int daio_mgr_commit_write(struct hw *hw, void *blk)
  853. {
  854. struct daio_mgr_ctrl_blk *ctl = blk;
  855. unsigned int data;
  856. int i;
  857. for (i = 0; i < 8; i++) {
  858. if ((ctl->dirty.bf.atxctl & (0x1 << i))) {
  859. data = ctl->txctl[i];
  860. hw_write_20kx(hw, (AUDIO_IO_TX_CTL+(0x40*i)), data);
  861. ctl->dirty.bf.atxctl &= ~(0x1 << i);
  862. mdelay(1);
  863. }
  864. if ((ctl->dirty.bf.arxctl & (0x1 << i))) {
  865. data = ctl->rxctl[i];
  866. hw_write_20kx(hw, (AUDIO_IO_RX_CTL+(0x40*i)), data);
  867. ctl->dirty.bf.arxctl &= ~(0x1 << i);
  868. mdelay(1);
  869. }
  870. }
  871. if (ctl->dirty.bf.daoimap) {
  872. hw_write_20kx(hw, AUDIO_IO_AIM+ctl->daoimap.idx*4,
  873. ctl->daoimap.aim);
  874. ctl->dirty.bf.daoimap = 0;
  875. }
  876. return 0;
  877. }
  878. static int daio_mgr_get_ctrl_blk(struct hw *hw, void **rblk)
  879. {
  880. struct daio_mgr_ctrl_blk *blk;
  881. int i;
  882. *rblk = NULL;
  883. blk = kzalloc(sizeof(*blk), GFP_KERNEL);
  884. if (!blk)
  885. return -ENOMEM;
  886. for (i = 0; i < 8; i++) {
  887. blk->txctl[i] = hw_read_20kx(hw, AUDIO_IO_TX_CTL+(0x40*i));
  888. blk->rxctl[i] = hw_read_20kx(hw, AUDIO_IO_RX_CTL+(0x40*i));
  889. }
  890. *rblk = blk;
  891. return 0;
  892. }
  893. static int daio_mgr_put_ctrl_blk(void *blk)
  894. {
  895. kfree(blk);
  896. return 0;
  897. }
  898. /* Timer interrupt */
  899. static int set_timer_irq(struct hw *hw, int enable)
  900. {
  901. hw_write_20kx(hw, GIE, enable ? IT_INT : 0);
  902. return 0;
  903. }
  904. static int set_timer_tick(struct hw *hw, unsigned int ticks)
  905. {
  906. if (ticks)
  907. ticks |= TIMR_IE | TIMR_IP;
  908. hw_write_20kx(hw, TIMR, ticks);
  909. return 0;
  910. }
  911. static unsigned int get_wc(struct hw *hw)
  912. {
  913. return hw_read_20kx(hw, WC);
  914. }
  915. /* Card hardware initialization block */
  916. struct dac_conf {
  917. unsigned int msr; /* master sample rate in rsrs */
  918. };
  919. struct adc_conf {
  920. unsigned int msr; /* master sample rate in rsrs */
  921. unsigned char input; /* the input source of ADC */
  922. unsigned char mic20db; /* boost mic by 20db if input is microphone */
  923. };
  924. struct daio_conf {
  925. unsigned int msr; /* master sample rate in rsrs */
  926. };
  927. struct trn_conf {
  928. unsigned long vm_pgt_phys;
  929. };
  930. static int hw_daio_init(struct hw *hw, const struct daio_conf *info)
  931. {
  932. u32 data;
  933. int i;
  934. /* Program I2S with proper sample rate and enable the correct I2S
  935. * channel. ED(0/8/16/24): Enable all I2S/I2X master clock output */
  936. if (1 == info->msr) {
  937. hw_write_20kx(hw, AUDIO_IO_MCLK, 0x01010101);
  938. hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x01010101);
  939. hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
  940. } else if (2 == info->msr) {
  941. if (hw->model != CTSB1270) {
  942. hw_write_20kx(hw, AUDIO_IO_MCLK, 0x11111111);
  943. } else {
  944. /* PCM4220 on Titanium HD is different. */
  945. hw_write_20kx(hw, AUDIO_IO_MCLK, 0x11011111);
  946. }
  947. /* Specify all playing 96khz
  948. * EA [0] - Enabled
  949. * RTA [4:5] - 96kHz
  950. * EB [8] - Enabled
  951. * RTB [12:13] - 96kHz
  952. * EC [16] - Enabled
  953. * RTC [20:21] - 96kHz
  954. * ED [24] - Enabled
  955. * RTD [28:29] - 96kHz */
  956. hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x11111111);
  957. hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
  958. } else if ((4 == info->msr) && (hw->model == CTSB1270)) {
  959. hw_write_20kx(hw, AUDIO_IO_MCLK, 0x21011111);
  960. hw_write_20kx(hw, AUDIO_IO_TX_BLRCLK, 0x21212121);
  961. hw_write_20kx(hw, AUDIO_IO_RX_BLRCLK, 0);
  962. } else {
  963. dev_alert(hw->card->dev,
  964. "ERROR!!! Invalid sampling rate!!!\n");
  965. return -EINVAL;
  966. }
  967. for (i = 0; i < 8; i++) {
  968. if (i <= 3) {
  969. /* This comment looks wrong since loop is over 4 */
  970. /* channels and emu20k2 supports 4 spdif IOs. */
  971. /* 1st 3 channels are SPDIFs (SB0960) */
  972. if (i == 3)
  973. data = 0x1001001;
  974. else
  975. data = 0x1000001;
  976. hw_write_20kx(hw, (AUDIO_IO_TX_CTL+(0x40*i)), data);
  977. hw_write_20kx(hw, (AUDIO_IO_RX_CTL+(0x40*i)), data);
  978. /* Initialize the SPDIF Out Channel status registers.
  979. * The value specified here is based on the typical
  980. * values provided in the specification, namely: Clock
  981. * Accuracy of 1000ppm, Sample Rate of 48KHz,
  982. * unspecified source number, Generation status = 1,
  983. * Category code = 0x12 (Digital Signal Mixer),
  984. * Mode = 0, Emph = 0, Copy Permitted, AN = 0
  985. * (indicating that we're transmitting digital audio,
  986. * and the Professional Use bit is 0. */
  987. hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_L+(0x40*i),
  988. 0x02109204); /* Default to 48kHz */
  989. hw_write_20kx(hw, AUDIO_IO_TX_CSTAT_H+(0x40*i), 0x0B);
  990. } else {
  991. /* Again, loop is over 4 channels not 5. */
  992. /* Next 5 channels are I2S (SB0960) */
  993. data = 0x11;
  994. hw_write_20kx(hw, AUDIO_IO_RX_CTL+(0x40*i), data);
  995. if (2 == info->msr) {
  996. /* Four channels per sample period */
  997. data |= 0x1000;
  998. } else if (4 == info->msr) {
  999. /* FIXME: check this against the chip spec */
  1000. data |= 0x2000;
  1001. }
  1002. hw_write_20kx(hw, AUDIO_IO_TX_CTL+(0x40*i), data);
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. /* TRANSPORT operations */
  1008. static int hw_trn_init(struct hw *hw, const struct trn_conf *info)
  1009. {
  1010. u32 vmctl, data;
  1011. u32 ptp_phys_low, ptp_phys_high;
  1012. int i;
  1013. /* Set up device page table */
  1014. if ((~0UL) == info->vm_pgt_phys) {
  1015. dev_alert(hw->card->dev,
  1016. "Wrong device page table page address!!!\n");
  1017. return -1;
  1018. }
  1019. vmctl = 0x80000C0F; /* 32-bit, 4k-size page */
  1020. ptp_phys_low = (u32)info->vm_pgt_phys;
  1021. ptp_phys_high = upper_32_bits(info->vm_pgt_phys);
  1022. if (sizeof(void *) == 8) /* 64bit address */
  1023. vmctl |= (3 << 8);
  1024. /* Write page table physical address to all PTPAL registers */
  1025. for (i = 0; i < 64; i++) {
  1026. hw_write_20kx(hw, VMEM_PTPAL+(16*i), ptp_phys_low);
  1027. hw_write_20kx(hw, VMEM_PTPAH+(16*i), ptp_phys_high);
  1028. }
  1029. /* Enable virtual memory transfer */
  1030. hw_write_20kx(hw, VMEM_CTL, vmctl);
  1031. /* Enable transport bus master and queueing of request */
  1032. hw_write_20kx(hw, TRANSPORT_CTL, 0x03);
  1033. hw_write_20kx(hw, TRANSPORT_INT, 0x200c01);
  1034. /* Enable transport ring */
  1035. data = hw_read_20kx(hw, TRANSPORT_ENB);
  1036. hw_write_20kx(hw, TRANSPORT_ENB, (data | 0x03));
  1037. return 0;
  1038. }
  1039. /* Card initialization */
  1040. #define GCTL_AIE 0x00000001
  1041. #define GCTL_UAA 0x00000002
  1042. #define GCTL_DPC 0x00000004
  1043. #define GCTL_DBP 0x00000008
  1044. #define GCTL_ABP 0x00000010
  1045. #define GCTL_TBP 0x00000020
  1046. #define GCTL_SBP 0x00000040
  1047. #define GCTL_FBP 0x00000080
  1048. #define GCTL_ME 0x00000100
  1049. #define GCTL_AID 0x00001000
  1050. #define PLLCTL_SRC 0x00000007
  1051. #define PLLCTL_SPE 0x00000008
  1052. #define PLLCTL_RD 0x000000F0
  1053. #define PLLCTL_FD 0x0001FF00
  1054. #define PLLCTL_OD 0x00060000
  1055. #define PLLCTL_B 0x00080000
  1056. #define PLLCTL_AS 0x00100000
  1057. #define PLLCTL_LF 0x03E00000
  1058. #define PLLCTL_SPS 0x1C000000
  1059. #define PLLCTL_AD 0x60000000
  1060. #define PLLSTAT_CCS 0x00000007
  1061. #define PLLSTAT_SPL 0x00000008
  1062. #define PLLSTAT_CRD 0x000000F0
  1063. #define PLLSTAT_CFD 0x0001FF00
  1064. #define PLLSTAT_SL 0x00020000
  1065. #define PLLSTAT_FAS 0x00040000
  1066. #define PLLSTAT_B 0x00080000
  1067. #define PLLSTAT_PD 0x00100000
  1068. #define PLLSTAT_OCA 0x00200000
  1069. #define PLLSTAT_NCA 0x00400000
  1070. static int hw_pll_init(struct hw *hw, unsigned int rsr)
  1071. {
  1072. unsigned int pllenb;
  1073. unsigned int pllctl;
  1074. unsigned int pllstat;
  1075. int i;
  1076. pllenb = 0xB;
  1077. hw_write_20kx(hw, PLL_ENB, pllenb);
  1078. pllctl = 0x20C00000;
  1079. set_field(&pllctl, PLLCTL_B, 0);
  1080. set_field(&pllctl, PLLCTL_FD, 48000 == rsr ? 16 - 4 : 147 - 4);
  1081. set_field(&pllctl, PLLCTL_RD, 48000 == rsr ? 1 - 1 : 10 - 1);
  1082. hw_write_20kx(hw, PLL_CTL, pllctl);
  1083. mdelay(40);
  1084. pllctl = hw_read_20kx(hw, PLL_CTL);
  1085. set_field(&pllctl, PLLCTL_FD, 48000 == rsr ? 16 - 2 : 147 - 2);
  1086. hw_write_20kx(hw, PLL_CTL, pllctl);
  1087. mdelay(40);
  1088. for (i = 0; i < 1000; i++) {
  1089. pllstat = hw_read_20kx(hw, PLL_STAT);
  1090. if (get_field(pllstat, PLLSTAT_PD))
  1091. continue;
  1092. if (get_field(pllstat, PLLSTAT_B) !=
  1093. get_field(pllctl, PLLCTL_B))
  1094. continue;
  1095. if (get_field(pllstat, PLLSTAT_CCS) !=
  1096. get_field(pllctl, PLLCTL_SRC))
  1097. continue;
  1098. if (get_field(pllstat, PLLSTAT_CRD) !=
  1099. get_field(pllctl, PLLCTL_RD))
  1100. continue;
  1101. if (get_field(pllstat, PLLSTAT_CFD) !=
  1102. get_field(pllctl, PLLCTL_FD))
  1103. continue;
  1104. break;
  1105. }
  1106. if (i >= 1000) {
  1107. dev_alert(hw->card->dev,
  1108. "PLL initialization failed!!!\n");
  1109. return -EBUSY;
  1110. }
  1111. return 0;
  1112. }
  1113. static int hw_auto_init(struct hw *hw)
  1114. {
  1115. unsigned int gctl;
  1116. int i;
  1117. gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
  1118. set_field(&gctl, GCTL_AIE, 0);
  1119. hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
  1120. set_field(&gctl, GCTL_AIE, 1);
  1121. hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
  1122. mdelay(10);
  1123. for (i = 0; i < 400000; i++) {
  1124. gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
  1125. if (get_field(gctl, GCTL_AID))
  1126. break;
  1127. }
  1128. if (!get_field(gctl, GCTL_AID)) {
  1129. dev_alert(hw->card->dev, "Card Auto-init failed!!!\n");
  1130. return -EBUSY;
  1131. }
  1132. return 0;
  1133. }
  1134. /* DAC operations */
  1135. #define CS4382_MC1 0x1
  1136. #define CS4382_MC2 0x2
  1137. #define CS4382_MC3 0x3
  1138. #define CS4382_FC 0x4
  1139. #define CS4382_IC 0x5
  1140. #define CS4382_XC1 0x6
  1141. #define CS4382_VCA1 0x7
  1142. #define CS4382_VCB1 0x8
  1143. #define CS4382_XC2 0x9
  1144. #define CS4382_VCA2 0xA
  1145. #define CS4382_VCB2 0xB
  1146. #define CS4382_XC3 0xC
  1147. #define CS4382_VCA3 0xD
  1148. #define CS4382_VCB3 0xE
  1149. #define CS4382_XC4 0xF
  1150. #define CS4382_VCA4 0x10
  1151. #define CS4382_VCB4 0x11
  1152. #define CS4382_CREV 0x12
  1153. /* I2C status */
  1154. #define STATE_LOCKED 0x00
  1155. #define STATE_UNLOCKED 0xAA
  1156. #define DATA_READY 0x800000 /* Used with I2C_IF_STATUS */
  1157. #define DATA_ABORT 0x10000 /* Used with I2C_IF_STATUS */
  1158. #define I2C_STATUS_DCM 0x00000001
  1159. #define I2C_STATUS_BC 0x00000006
  1160. #define I2C_STATUS_APD 0x00000008
  1161. #define I2C_STATUS_AB 0x00010000
  1162. #define I2C_STATUS_DR 0x00800000
  1163. #define I2C_ADDRESS_PTAD 0x0000FFFF
  1164. #define I2C_ADDRESS_SLAD 0x007F0000
  1165. struct regs_cs4382 {
  1166. u32 mode_control_1;
  1167. u32 mode_control_2;
  1168. u32 mode_control_3;
  1169. u32 filter_control;
  1170. u32 invert_control;
  1171. u32 mix_control_P1;
  1172. u32 vol_control_A1;
  1173. u32 vol_control_B1;
  1174. u32 mix_control_P2;
  1175. u32 vol_control_A2;
  1176. u32 vol_control_B2;
  1177. u32 mix_control_P3;
  1178. u32 vol_control_A3;
  1179. u32 vol_control_B3;
  1180. u32 mix_control_P4;
  1181. u32 vol_control_A4;
  1182. u32 vol_control_B4;
  1183. };
  1184. static int hw20k2_i2c_unlock_full_access(struct hw *hw)
  1185. {
  1186. u8 UnlockKeySequence_FLASH_FULLACCESS_MODE[2] = {0xB3, 0xD4};
  1187. /* Send keys for forced BIOS mode */
  1188. hw_write_20kx(hw, I2C_IF_WLOCK,
  1189. UnlockKeySequence_FLASH_FULLACCESS_MODE[0]);
  1190. hw_write_20kx(hw, I2C_IF_WLOCK,
  1191. UnlockKeySequence_FLASH_FULLACCESS_MODE[1]);
  1192. /* Check whether the chip is unlocked */
  1193. if (hw_read_20kx(hw, I2C_IF_WLOCK) == STATE_UNLOCKED)
  1194. return 0;
  1195. return -1;
  1196. }
  1197. static int hw20k2_i2c_lock_chip(struct hw *hw)
  1198. {
  1199. /* Write twice */
  1200. hw_write_20kx(hw, I2C_IF_WLOCK, STATE_LOCKED);
  1201. hw_write_20kx(hw, I2C_IF_WLOCK, STATE_LOCKED);
  1202. if (hw_read_20kx(hw, I2C_IF_WLOCK) == STATE_LOCKED)
  1203. return 0;
  1204. return -1;
  1205. }
  1206. static int hw20k2_i2c_init(struct hw *hw, u8 dev_id, u8 addr_size, u8 data_size)
  1207. {
  1208. struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
  1209. int err;
  1210. unsigned int i2c_status;
  1211. unsigned int i2c_addr;
  1212. err = hw20k2_i2c_unlock_full_access(hw);
  1213. if (err < 0)
  1214. return err;
  1215. hw20k2->addr_size = addr_size;
  1216. hw20k2->data_size = data_size;
  1217. hw20k2->dev_id = dev_id;
  1218. i2c_addr = 0;
  1219. set_field(&i2c_addr, I2C_ADDRESS_SLAD, dev_id);
  1220. hw_write_20kx(hw, I2C_IF_ADDRESS, i2c_addr);
  1221. i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
  1222. set_field(&i2c_status, I2C_STATUS_DCM, 1); /* Direct control mode */
  1223. hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
  1224. return 0;
  1225. }
  1226. static int hw20k2_i2c_uninit(struct hw *hw)
  1227. {
  1228. unsigned int i2c_status;
  1229. unsigned int i2c_addr;
  1230. i2c_addr = 0;
  1231. set_field(&i2c_addr, I2C_ADDRESS_SLAD, 0x57); /* I2C id */
  1232. hw_write_20kx(hw, I2C_IF_ADDRESS, i2c_addr);
  1233. i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
  1234. set_field(&i2c_status, I2C_STATUS_DCM, 0); /* I2C mode */
  1235. hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
  1236. return hw20k2_i2c_lock_chip(hw);
  1237. }
  1238. static int hw20k2_i2c_wait_data_ready(struct hw *hw)
  1239. {
  1240. int i = 0x400000;
  1241. unsigned int ret;
  1242. do {
  1243. ret = hw_read_20kx(hw, I2C_IF_STATUS);
  1244. } while ((!(ret & DATA_READY)) && --i);
  1245. return i;
  1246. }
  1247. static int hw20k2_i2c_read(struct hw *hw, u16 addr, u32 *datap)
  1248. {
  1249. struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
  1250. unsigned int i2c_status;
  1251. i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
  1252. set_field(&i2c_status, I2C_STATUS_BC,
  1253. (4 == hw20k2->addr_size) ? 0 : hw20k2->addr_size);
  1254. hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
  1255. if (!hw20k2_i2c_wait_data_ready(hw))
  1256. return -1;
  1257. hw_write_20kx(hw, I2C_IF_WDATA, addr);
  1258. if (!hw20k2_i2c_wait_data_ready(hw))
  1259. return -1;
  1260. /* Force a read operation */
  1261. hw_write_20kx(hw, I2C_IF_RDATA, 0);
  1262. if (!hw20k2_i2c_wait_data_ready(hw))
  1263. return -1;
  1264. *datap = hw_read_20kx(hw, I2C_IF_RDATA);
  1265. return 0;
  1266. }
  1267. static int hw20k2_i2c_write(struct hw *hw, u16 addr, u32 data)
  1268. {
  1269. struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
  1270. unsigned int i2c_data = (data << (hw20k2->addr_size * 8)) | addr;
  1271. unsigned int i2c_status;
  1272. i2c_status = hw_read_20kx(hw, I2C_IF_STATUS);
  1273. set_field(&i2c_status, I2C_STATUS_BC,
  1274. (4 == (hw20k2->addr_size + hw20k2->data_size)) ?
  1275. 0 : (hw20k2->addr_size + hw20k2->data_size));
  1276. hw_write_20kx(hw, I2C_IF_STATUS, i2c_status);
  1277. hw20k2_i2c_wait_data_ready(hw);
  1278. /* Dummy write to trigger the write operation */
  1279. hw_write_20kx(hw, I2C_IF_WDATA, 0);
  1280. hw20k2_i2c_wait_data_ready(hw);
  1281. /* This is the real data */
  1282. hw_write_20kx(hw, I2C_IF_WDATA, i2c_data);
  1283. hw20k2_i2c_wait_data_ready(hw);
  1284. return 0;
  1285. }
  1286. static void hw_dac_stop(struct hw *hw)
  1287. {
  1288. u32 data;
  1289. data = hw_read_20kx(hw, GPIO_DATA);
  1290. data &= 0xFFFFFFFD;
  1291. hw_write_20kx(hw, GPIO_DATA, data);
  1292. mdelay(10);
  1293. }
  1294. static void hw_dac_start(struct hw *hw)
  1295. {
  1296. u32 data;
  1297. data = hw_read_20kx(hw, GPIO_DATA);
  1298. data |= 0x2;
  1299. hw_write_20kx(hw, GPIO_DATA, data);
  1300. mdelay(50);
  1301. }
  1302. static void hw_dac_reset(struct hw *hw)
  1303. {
  1304. hw_dac_stop(hw);
  1305. hw_dac_start(hw);
  1306. }
  1307. static int hw_dac_init(struct hw *hw, const struct dac_conf *info)
  1308. {
  1309. int err;
  1310. u32 data;
  1311. int i;
  1312. struct regs_cs4382 cs_read = {0};
  1313. struct regs_cs4382 cs_def = {
  1314. .mode_control_1 = 0x00000001, /* Mode Control 1 */
  1315. .mode_control_2 = 0x00000000, /* Mode Control 2 */
  1316. .mode_control_3 = 0x00000084, /* Mode Control 3 */
  1317. .filter_control = 0x00000000, /* Filter Control */
  1318. .invert_control = 0x00000000, /* Invert Control */
  1319. .mix_control_P1 = 0x00000024, /* Mixing Control Pair 1 */
  1320. .vol_control_A1 = 0x00000000, /* Vol Control A1 */
  1321. .vol_control_B1 = 0x00000000, /* Vol Control B1 */
  1322. .mix_control_P2 = 0x00000024, /* Mixing Control Pair 2 */
  1323. .vol_control_A2 = 0x00000000, /* Vol Control A2 */
  1324. .vol_control_B2 = 0x00000000, /* Vol Control B2 */
  1325. .mix_control_P3 = 0x00000024, /* Mixing Control Pair 3 */
  1326. .vol_control_A3 = 0x00000000, /* Vol Control A3 */
  1327. .vol_control_B3 = 0x00000000, /* Vol Control B3 */
  1328. .mix_control_P4 = 0x00000024, /* Mixing Control Pair 4 */
  1329. .vol_control_A4 = 0x00000000, /* Vol Control A4 */
  1330. .vol_control_B4 = 0x00000000 /* Vol Control B4 */
  1331. };
  1332. if (hw->model == CTSB1270) {
  1333. hw_dac_stop(hw);
  1334. data = hw_read_20kx(hw, GPIO_DATA);
  1335. data &= ~0x0600;
  1336. if (1 == info->msr)
  1337. data |= 0x0000; /* Single Speed Mode 0-50kHz */
  1338. else if (2 == info->msr)
  1339. data |= 0x0200; /* Double Speed Mode 50-100kHz */
  1340. else
  1341. data |= 0x0600; /* Quad Speed Mode 100-200kHz */
  1342. hw_write_20kx(hw, GPIO_DATA, data);
  1343. hw_dac_start(hw);
  1344. return 0;
  1345. }
  1346. /* Set DAC reset bit as output */
  1347. data = hw_read_20kx(hw, GPIO_CTRL);
  1348. data |= 0x02;
  1349. hw_write_20kx(hw, GPIO_CTRL, data);
  1350. err = hw20k2_i2c_init(hw, 0x18, 1, 1);
  1351. if (err < 0)
  1352. goto End;
  1353. for (i = 0; i < 2; i++) {
  1354. /* Reset DAC twice just in-case the chip
  1355. * didn't initialized properly */
  1356. hw_dac_reset(hw);
  1357. hw_dac_reset(hw);
  1358. if (hw20k2_i2c_read(hw, CS4382_MC1, &cs_read.mode_control_1))
  1359. continue;
  1360. if (hw20k2_i2c_read(hw, CS4382_MC2, &cs_read.mode_control_2))
  1361. continue;
  1362. if (hw20k2_i2c_read(hw, CS4382_MC3, &cs_read.mode_control_3))
  1363. continue;
  1364. if (hw20k2_i2c_read(hw, CS4382_FC, &cs_read.filter_control))
  1365. continue;
  1366. if (hw20k2_i2c_read(hw, CS4382_IC, &cs_read.invert_control))
  1367. continue;
  1368. if (hw20k2_i2c_read(hw, CS4382_XC1, &cs_read.mix_control_P1))
  1369. continue;
  1370. if (hw20k2_i2c_read(hw, CS4382_VCA1, &cs_read.vol_control_A1))
  1371. continue;
  1372. if (hw20k2_i2c_read(hw, CS4382_VCB1, &cs_read.vol_control_B1))
  1373. continue;
  1374. if (hw20k2_i2c_read(hw, CS4382_XC2, &cs_read.mix_control_P2))
  1375. continue;
  1376. if (hw20k2_i2c_read(hw, CS4382_VCA2, &cs_read.vol_control_A2))
  1377. continue;
  1378. if (hw20k2_i2c_read(hw, CS4382_VCB2, &cs_read.vol_control_B2))
  1379. continue;
  1380. if (hw20k2_i2c_read(hw, CS4382_XC3, &cs_read.mix_control_P3))
  1381. continue;
  1382. if (hw20k2_i2c_read(hw, CS4382_VCA3, &cs_read.vol_control_A3))
  1383. continue;
  1384. if (hw20k2_i2c_read(hw, CS4382_VCB3, &cs_read.vol_control_B3))
  1385. continue;
  1386. if (hw20k2_i2c_read(hw, CS4382_XC4, &cs_read.mix_control_P4))
  1387. continue;
  1388. if (hw20k2_i2c_read(hw, CS4382_VCA4, &cs_read.vol_control_A4))
  1389. continue;
  1390. if (hw20k2_i2c_read(hw, CS4382_VCB4, &cs_read.vol_control_B4))
  1391. continue;
  1392. if (memcmp(&cs_read, &cs_def, sizeof(cs_read)))
  1393. continue;
  1394. else
  1395. break;
  1396. }
  1397. if (i >= 2)
  1398. goto End;
  1399. /* Note: Every I2C write must have some delay.
  1400. * This is not a requirement but the delay works here... */
  1401. hw20k2_i2c_write(hw, CS4382_MC1, 0x80);
  1402. hw20k2_i2c_write(hw, CS4382_MC2, 0x10);
  1403. if (1 == info->msr) {
  1404. hw20k2_i2c_write(hw, CS4382_XC1, 0x24);
  1405. hw20k2_i2c_write(hw, CS4382_XC2, 0x24);
  1406. hw20k2_i2c_write(hw, CS4382_XC3, 0x24);
  1407. hw20k2_i2c_write(hw, CS4382_XC4, 0x24);
  1408. } else if (2 == info->msr) {
  1409. hw20k2_i2c_write(hw, CS4382_XC1, 0x25);
  1410. hw20k2_i2c_write(hw, CS4382_XC2, 0x25);
  1411. hw20k2_i2c_write(hw, CS4382_XC3, 0x25);
  1412. hw20k2_i2c_write(hw, CS4382_XC4, 0x25);
  1413. } else {
  1414. hw20k2_i2c_write(hw, CS4382_XC1, 0x26);
  1415. hw20k2_i2c_write(hw, CS4382_XC2, 0x26);
  1416. hw20k2_i2c_write(hw, CS4382_XC3, 0x26);
  1417. hw20k2_i2c_write(hw, CS4382_XC4, 0x26);
  1418. }
  1419. return 0;
  1420. End:
  1421. hw20k2_i2c_uninit(hw);
  1422. return -1;
  1423. }
  1424. /* ADC operations */
  1425. #define MAKE_WM8775_ADDR(addr, data) (u32)(((addr<<1)&0xFE)|((data>>8)&0x1))
  1426. #define MAKE_WM8775_DATA(data) (u32)(data&0xFF)
  1427. #define WM8775_IC 0x0B
  1428. #define WM8775_MMC 0x0C
  1429. #define WM8775_AADCL 0x0E
  1430. #define WM8775_AADCR 0x0F
  1431. #define WM8775_ADCMC 0x15
  1432. #define WM8775_RESET 0x17
  1433. static int hw_is_adc_input_selected(struct hw *hw, enum ADCSRC type)
  1434. {
  1435. u32 data;
  1436. if (hw->model == CTSB1270) {
  1437. /* Titanium HD has two ADC chips, one for line in and one */
  1438. /* for MIC. We don't need to switch the ADC input. */
  1439. return 1;
  1440. }
  1441. data = hw_read_20kx(hw, GPIO_DATA);
  1442. switch (type) {
  1443. case ADC_MICIN:
  1444. data = (data & (0x1 << 14)) ? 1 : 0;
  1445. break;
  1446. case ADC_LINEIN:
  1447. data = (data & (0x1 << 14)) ? 0 : 1;
  1448. break;
  1449. default:
  1450. data = 0;
  1451. }
  1452. return data;
  1453. }
  1454. #define MIC_BOOST_0DB 0xCF
  1455. #define MIC_BOOST_STEPS_PER_DB 2
  1456. static void hw_wm8775_input_select(struct hw *hw, u8 input, s8 gain_in_db)
  1457. {
  1458. u32 adcmc, gain;
  1459. if (input > 3)
  1460. input = 3;
  1461. adcmc = ((u32)1 << input) | 0x100; /* Link L+R gain... */
  1462. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_ADCMC, adcmc),
  1463. MAKE_WM8775_DATA(adcmc));
  1464. if (gain_in_db < -103)
  1465. gain_in_db = -103;
  1466. if (gain_in_db > 24)
  1467. gain_in_db = 24;
  1468. gain = gain_in_db * MIC_BOOST_STEPS_PER_DB + MIC_BOOST_0DB;
  1469. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_AADCL, gain),
  1470. MAKE_WM8775_DATA(gain));
  1471. /* ...so there should be no need for the following. */
  1472. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_AADCR, gain),
  1473. MAKE_WM8775_DATA(gain));
  1474. }
  1475. static int hw_adc_input_select(struct hw *hw, enum ADCSRC type)
  1476. {
  1477. u32 data;
  1478. data = hw_read_20kx(hw, GPIO_DATA);
  1479. switch (type) {
  1480. case ADC_MICIN:
  1481. data |= (0x1 << 14);
  1482. hw_write_20kx(hw, GPIO_DATA, data);
  1483. hw_wm8775_input_select(hw, 0, 20); /* Mic, 20dB */
  1484. break;
  1485. case ADC_LINEIN:
  1486. data &= ~(0x1 << 14);
  1487. hw_write_20kx(hw, GPIO_DATA, data);
  1488. hw_wm8775_input_select(hw, 1, 0); /* Line-in, 0dB */
  1489. break;
  1490. default:
  1491. break;
  1492. }
  1493. return 0;
  1494. }
  1495. static int hw_adc_init(struct hw *hw, const struct adc_conf *info)
  1496. {
  1497. int err;
  1498. u32 data, ctl;
  1499. /* Set ADC reset bit as output */
  1500. data = hw_read_20kx(hw, GPIO_CTRL);
  1501. data |= (0x1 << 15);
  1502. hw_write_20kx(hw, GPIO_CTRL, data);
  1503. /* Initialize I2C */
  1504. err = hw20k2_i2c_init(hw, 0x1A, 1, 1);
  1505. if (err < 0) {
  1506. dev_alert(hw->card->dev, "Failure to acquire I2C!!!\n");
  1507. goto error;
  1508. }
  1509. /* Reset the ADC (reset is active low). */
  1510. data = hw_read_20kx(hw, GPIO_DATA);
  1511. data &= ~(0x1 << 15);
  1512. hw_write_20kx(hw, GPIO_DATA, data);
  1513. if (hw->model == CTSB1270) {
  1514. /* Set up the PCM4220 ADC on Titanium HD */
  1515. data &= ~0x0C;
  1516. if (1 == info->msr)
  1517. data |= 0x00; /* Single Speed Mode 32-50kHz */
  1518. else if (2 == info->msr)
  1519. data |= 0x08; /* Double Speed Mode 50-108kHz */
  1520. else
  1521. data |= 0x04; /* Quad Speed Mode 108kHz-216kHz */
  1522. hw_write_20kx(hw, GPIO_DATA, data);
  1523. }
  1524. mdelay(10);
  1525. /* Return the ADC to normal operation. */
  1526. data |= (0x1 << 15);
  1527. hw_write_20kx(hw, GPIO_DATA, data);
  1528. mdelay(50);
  1529. /* I2C write to register offset 0x0B to set ADC LRCLK polarity */
  1530. /* invert bit, interface format to I2S, word length to 24-bit, */
  1531. /* enable ADC high pass filter. Fixes bug 5323? */
  1532. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_IC, 0x26),
  1533. MAKE_WM8775_DATA(0x26));
  1534. /* Set the master mode (256fs) */
  1535. if (1 == info->msr) {
  1536. /* slave mode, 128x oversampling 256fs */
  1537. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_MMC, 0x02),
  1538. MAKE_WM8775_DATA(0x02));
  1539. } else if ((2 == info->msr) || (4 == info->msr)) {
  1540. /* slave mode, 64x oversampling, 256fs */
  1541. hw20k2_i2c_write(hw, MAKE_WM8775_ADDR(WM8775_MMC, 0x0A),
  1542. MAKE_WM8775_DATA(0x0A));
  1543. } else {
  1544. dev_alert(hw->card->dev,
  1545. "Invalid master sampling rate (msr %d)!!!\n",
  1546. info->msr);
  1547. err = -EINVAL;
  1548. goto error;
  1549. }
  1550. if (hw->model != CTSB1270) {
  1551. /* Configure GPIO bit 14 change to line-in/mic-in */
  1552. ctl = hw_read_20kx(hw, GPIO_CTRL);
  1553. ctl |= 0x1 << 14;
  1554. hw_write_20kx(hw, GPIO_CTRL, ctl);
  1555. hw_adc_input_select(hw, ADC_LINEIN);
  1556. } else {
  1557. hw_wm8775_input_select(hw, 0, 0);
  1558. }
  1559. return 0;
  1560. error:
  1561. hw20k2_i2c_uninit(hw);
  1562. return err;
  1563. }
  1564. static struct capabilities hw_capabilities(struct hw *hw)
  1565. {
  1566. struct capabilities cap;
  1567. cap.digit_io_switch = 0;
  1568. cap.dedicated_mic = hw->model == CTSB1270;
  1569. cap.output_switch = hw->model == CTSB1270;
  1570. cap.mic_source_switch = hw->model == CTSB1270;
  1571. return cap;
  1572. }
  1573. static int hw_output_switch_get(struct hw *hw)
  1574. {
  1575. u32 data = hw_read_20kx(hw, GPIO_EXT_DATA);
  1576. switch (data & 0x30) {
  1577. case 0x00:
  1578. return 0;
  1579. case 0x10:
  1580. return 1;
  1581. case 0x20:
  1582. return 2;
  1583. default:
  1584. return 3;
  1585. }
  1586. }
  1587. static int hw_output_switch_put(struct hw *hw, int position)
  1588. {
  1589. u32 data;
  1590. if (position == hw_output_switch_get(hw))
  1591. return 0;
  1592. /* Mute line and headphones (intended for anti-pop). */
  1593. data = hw_read_20kx(hw, GPIO_DATA);
  1594. data |= (0x03 << 11);
  1595. hw_write_20kx(hw, GPIO_DATA, data);
  1596. data = hw_read_20kx(hw, GPIO_EXT_DATA) & ~0x30;
  1597. switch (position) {
  1598. case 0:
  1599. break;
  1600. case 1:
  1601. data |= 0x10;
  1602. break;
  1603. default:
  1604. data |= 0x20;
  1605. }
  1606. hw_write_20kx(hw, GPIO_EXT_DATA, data);
  1607. /* Unmute line and headphones. */
  1608. data = hw_read_20kx(hw, GPIO_DATA);
  1609. data &= ~(0x03 << 11);
  1610. hw_write_20kx(hw, GPIO_DATA, data);
  1611. return 1;
  1612. }
  1613. static int hw_mic_source_switch_get(struct hw *hw)
  1614. {
  1615. struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
  1616. return hw20k2->mic_source;
  1617. }
  1618. static int hw_mic_source_switch_put(struct hw *hw, int position)
  1619. {
  1620. struct hw20k2 *hw20k2 = (struct hw20k2 *)hw;
  1621. if (position == hw20k2->mic_source)
  1622. return 0;
  1623. switch (position) {
  1624. case 0:
  1625. hw_wm8775_input_select(hw, 0, 0); /* Mic, 0dB */
  1626. break;
  1627. case 1:
  1628. hw_wm8775_input_select(hw, 1, 0); /* FP Mic, 0dB */
  1629. break;
  1630. case 2:
  1631. hw_wm8775_input_select(hw, 3, 0); /* Aux Ext, 0dB */
  1632. break;
  1633. default:
  1634. return 0;
  1635. }
  1636. hw20k2->mic_source = position;
  1637. return 1;
  1638. }
  1639. static irqreturn_t ct_20k2_interrupt(int irq, void *dev_id)
  1640. {
  1641. struct hw *hw = dev_id;
  1642. unsigned int status;
  1643. status = hw_read_20kx(hw, GIP);
  1644. if (!status)
  1645. return IRQ_NONE;
  1646. if (hw->irq_callback)
  1647. hw->irq_callback(hw->irq_callback_data, status);
  1648. hw_write_20kx(hw, GIP, status);
  1649. return IRQ_HANDLED;
  1650. }
  1651. static int hw_card_start(struct hw *hw)
  1652. {
  1653. int err = 0;
  1654. struct pci_dev *pci = hw->pci;
  1655. unsigned int gctl;
  1656. const unsigned int dma_bits = BITS_PER_LONG;
  1657. err = pci_enable_device(pci);
  1658. if (err < 0)
  1659. return err;
  1660. /* Set DMA transfer mask */
  1661. if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(dma_bits))) {
  1662. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(dma_bits));
  1663. } else {
  1664. dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
  1665. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
  1666. }
  1667. if (!hw->io_base) {
  1668. err = pci_request_regions(pci, "XFi");
  1669. if (err < 0)
  1670. goto error1;
  1671. hw->io_base = pci_resource_start(hw->pci, 2);
  1672. hw->mem_base = ioremap(hw->io_base,
  1673. pci_resource_len(hw->pci, 2));
  1674. if (!hw->mem_base) {
  1675. err = -ENOENT;
  1676. goto error2;
  1677. }
  1678. }
  1679. /* Switch to 20k2 mode from UAA mode. */
  1680. gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
  1681. set_field(&gctl, GCTL_UAA, 0);
  1682. hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
  1683. if (hw->irq < 0) {
  1684. err = request_irq(pci->irq, ct_20k2_interrupt, IRQF_SHARED,
  1685. KBUILD_MODNAME, hw);
  1686. if (err < 0) {
  1687. dev_err(hw->card->dev,
  1688. "XFi: Cannot get irq %d\n", pci->irq);
  1689. goto error2;
  1690. }
  1691. hw->irq = pci->irq;
  1692. }
  1693. pci_set_master(pci);
  1694. return 0;
  1695. /*error3:
  1696. iounmap((void *)hw->mem_base);
  1697. hw->mem_base = (unsigned long)NULL;*/
  1698. error2:
  1699. pci_release_regions(pci);
  1700. hw->io_base = 0;
  1701. error1:
  1702. pci_disable_device(pci);
  1703. return err;
  1704. }
  1705. static int hw_card_stop(struct hw *hw)
  1706. {
  1707. unsigned int data;
  1708. /* disable transport bus master and queueing of request */
  1709. hw_write_20kx(hw, TRANSPORT_CTL, 0x00);
  1710. /* disable pll */
  1711. data = hw_read_20kx(hw, PLL_ENB);
  1712. hw_write_20kx(hw, PLL_ENB, (data & (~0x07)));
  1713. /* TODO: Disable interrupt and so on... */
  1714. return 0;
  1715. }
  1716. static int hw_card_shutdown(struct hw *hw)
  1717. {
  1718. if (hw->irq >= 0)
  1719. free_irq(hw->irq, hw);
  1720. hw->irq = -1;
  1721. iounmap(hw->mem_base);
  1722. hw->mem_base = NULL;
  1723. if (hw->io_base)
  1724. pci_release_regions(hw->pci);
  1725. hw->io_base = 0;
  1726. pci_disable_device(hw->pci);
  1727. return 0;
  1728. }
  1729. static int hw_card_init(struct hw *hw, struct card_conf *info)
  1730. {
  1731. int err;
  1732. unsigned int gctl;
  1733. u32 data = 0;
  1734. struct dac_conf dac_info = {0};
  1735. struct adc_conf adc_info = {0};
  1736. struct daio_conf daio_info = {0};
  1737. struct trn_conf trn_info = {0};
  1738. /* Get PCI io port/memory base address and
  1739. * do 20kx core switch if needed. */
  1740. err = hw_card_start(hw);
  1741. if (err)
  1742. return err;
  1743. /* PLL init */
  1744. err = hw_pll_init(hw, info->rsr);
  1745. if (err < 0)
  1746. return err;
  1747. /* kick off auto-init */
  1748. err = hw_auto_init(hw);
  1749. if (err < 0)
  1750. return err;
  1751. gctl = hw_read_20kx(hw, GLOBAL_CNTL_GCTL);
  1752. set_field(&gctl, GCTL_DBP, 1);
  1753. set_field(&gctl, GCTL_TBP, 1);
  1754. set_field(&gctl, GCTL_FBP, 1);
  1755. set_field(&gctl, GCTL_DPC, 0);
  1756. hw_write_20kx(hw, GLOBAL_CNTL_GCTL, gctl);
  1757. /* Reset all global pending interrupts */
  1758. hw_write_20kx(hw, GIE, 0);
  1759. /* Reset all SRC pending interrupts */
  1760. hw_write_20kx(hw, SRC_IP, 0);
  1761. if (hw->model != CTSB1270) {
  1762. /* TODO: detect the card ID and configure GPIO accordingly. */
  1763. /* Configures GPIO (0xD802 0x98028) */
  1764. /*hw_write_20kx(hw, GPIO_CTRL, 0x7F07);*/
  1765. /* Configures GPIO (SB0880) */
  1766. /*hw_write_20kx(hw, GPIO_CTRL, 0xFF07);*/
  1767. hw_write_20kx(hw, GPIO_CTRL, 0xD802);
  1768. } else {
  1769. hw_write_20kx(hw, GPIO_CTRL, 0x9E5F);
  1770. }
  1771. /* Enable audio ring */
  1772. hw_write_20kx(hw, MIXER_AR_ENABLE, 0x01);
  1773. trn_info.vm_pgt_phys = info->vm_pgt_phys;
  1774. err = hw_trn_init(hw, &trn_info);
  1775. if (err < 0)
  1776. return err;
  1777. daio_info.msr = info->msr;
  1778. err = hw_daio_init(hw, &daio_info);
  1779. if (err < 0)
  1780. return err;
  1781. dac_info.msr = info->msr;
  1782. err = hw_dac_init(hw, &dac_info);
  1783. if (err < 0)
  1784. return err;
  1785. adc_info.msr = info->msr;
  1786. adc_info.input = ADC_LINEIN;
  1787. adc_info.mic20db = 0;
  1788. err = hw_adc_init(hw, &adc_info);
  1789. if (err < 0)
  1790. return err;
  1791. data = hw_read_20kx(hw, SRC_MCTL);
  1792. data |= 0x1; /* Enables input from the audio ring */
  1793. hw_write_20kx(hw, SRC_MCTL, data);
  1794. return 0;
  1795. }
  1796. #ifdef CONFIG_PM_SLEEP
  1797. static int hw_suspend(struct hw *hw)
  1798. {
  1799. hw_card_stop(hw);
  1800. return 0;
  1801. }
  1802. static int hw_resume(struct hw *hw, struct card_conf *info)
  1803. {
  1804. /* Re-initialize card hardware. */
  1805. return hw_card_init(hw, info);
  1806. }
  1807. #endif
  1808. static u32 hw_read_20kx(struct hw *hw, u32 reg)
  1809. {
  1810. return readl(hw->mem_base + reg);
  1811. }
  1812. static void hw_write_20kx(struct hw *hw, u32 reg, u32 data)
  1813. {
  1814. writel(data, hw->mem_base + reg);
  1815. }
  1816. static const struct hw ct20k2_preset = {
  1817. .irq = -1,
  1818. .card_init = hw_card_init,
  1819. .card_stop = hw_card_stop,
  1820. .pll_init = hw_pll_init,
  1821. .is_adc_source_selected = hw_is_adc_input_selected,
  1822. .select_adc_source = hw_adc_input_select,
  1823. .capabilities = hw_capabilities,
  1824. .output_switch_get = hw_output_switch_get,
  1825. .output_switch_put = hw_output_switch_put,
  1826. .mic_source_switch_get = hw_mic_source_switch_get,
  1827. .mic_source_switch_put = hw_mic_source_switch_put,
  1828. #ifdef CONFIG_PM_SLEEP
  1829. .suspend = hw_suspend,
  1830. .resume = hw_resume,
  1831. #endif
  1832. .src_rsc_get_ctrl_blk = src_get_rsc_ctrl_blk,
  1833. .src_rsc_put_ctrl_blk = src_put_rsc_ctrl_blk,
  1834. .src_mgr_get_ctrl_blk = src_mgr_get_ctrl_blk,
  1835. .src_mgr_put_ctrl_blk = src_mgr_put_ctrl_blk,
  1836. .src_set_state = src_set_state,
  1837. .src_set_bm = src_set_bm,
  1838. .src_set_rsr = src_set_rsr,
  1839. .src_set_sf = src_set_sf,
  1840. .src_set_wr = src_set_wr,
  1841. .src_set_pm = src_set_pm,
  1842. .src_set_rom = src_set_rom,
  1843. .src_set_vo = src_set_vo,
  1844. .src_set_st = src_set_st,
  1845. .src_set_ie = src_set_ie,
  1846. .src_set_ilsz = src_set_ilsz,
  1847. .src_set_bp = src_set_bp,
  1848. .src_set_cisz = src_set_cisz,
  1849. .src_set_ca = src_set_ca,
  1850. .src_set_sa = src_set_sa,
  1851. .src_set_la = src_set_la,
  1852. .src_set_pitch = src_set_pitch,
  1853. .src_set_dirty = src_set_dirty,
  1854. .src_set_clear_zbufs = src_set_clear_zbufs,
  1855. .src_set_dirty_all = src_set_dirty_all,
  1856. .src_commit_write = src_commit_write,
  1857. .src_get_ca = src_get_ca,
  1858. .src_get_dirty = src_get_dirty,
  1859. .src_dirty_conj_mask = src_dirty_conj_mask,
  1860. .src_mgr_enbs_src = src_mgr_enbs_src,
  1861. .src_mgr_enb_src = src_mgr_enb_src,
  1862. .src_mgr_dsb_src = src_mgr_dsb_src,
  1863. .src_mgr_commit_write = src_mgr_commit_write,
  1864. .srcimp_mgr_get_ctrl_blk = srcimp_mgr_get_ctrl_blk,
  1865. .srcimp_mgr_put_ctrl_blk = srcimp_mgr_put_ctrl_blk,
  1866. .srcimp_mgr_set_imaparc = srcimp_mgr_set_imaparc,
  1867. .srcimp_mgr_set_imapuser = srcimp_mgr_set_imapuser,
  1868. .srcimp_mgr_set_imapnxt = srcimp_mgr_set_imapnxt,
  1869. .srcimp_mgr_set_imapaddr = srcimp_mgr_set_imapaddr,
  1870. .srcimp_mgr_commit_write = srcimp_mgr_commit_write,
  1871. .amixer_rsc_get_ctrl_blk = amixer_rsc_get_ctrl_blk,
  1872. .amixer_rsc_put_ctrl_blk = amixer_rsc_put_ctrl_blk,
  1873. .amixer_mgr_get_ctrl_blk = amixer_mgr_get_ctrl_blk,
  1874. .amixer_mgr_put_ctrl_blk = amixer_mgr_put_ctrl_blk,
  1875. .amixer_set_mode = amixer_set_mode,
  1876. .amixer_set_iv = amixer_set_iv,
  1877. .amixer_set_x = amixer_set_x,
  1878. .amixer_set_y = amixer_set_y,
  1879. .amixer_set_sadr = amixer_set_sadr,
  1880. .amixer_set_se = amixer_set_se,
  1881. .amixer_set_dirty = amixer_set_dirty,
  1882. .amixer_set_dirty_all = amixer_set_dirty_all,
  1883. .amixer_commit_write = amixer_commit_write,
  1884. .amixer_get_y = amixer_get_y,
  1885. .amixer_get_dirty = amixer_get_dirty,
  1886. .dai_get_ctrl_blk = dai_get_ctrl_blk,
  1887. .dai_put_ctrl_blk = dai_put_ctrl_blk,
  1888. .dai_srt_set_srco = dai_srt_set_srco,
  1889. .dai_srt_set_srcm = dai_srt_set_srcm,
  1890. .dai_srt_set_rsr = dai_srt_set_rsr,
  1891. .dai_srt_set_drat = dai_srt_set_drat,
  1892. .dai_srt_set_ec = dai_srt_set_ec,
  1893. .dai_srt_set_et = dai_srt_set_et,
  1894. .dai_commit_write = dai_commit_write,
  1895. .dao_get_ctrl_blk = dao_get_ctrl_blk,
  1896. .dao_put_ctrl_blk = dao_put_ctrl_blk,
  1897. .dao_set_spos = dao_set_spos,
  1898. .dao_commit_write = dao_commit_write,
  1899. .dao_get_spos = dao_get_spos,
  1900. .daio_mgr_get_ctrl_blk = daio_mgr_get_ctrl_blk,
  1901. .daio_mgr_put_ctrl_blk = daio_mgr_put_ctrl_blk,
  1902. .daio_mgr_enb_dai = daio_mgr_enb_dai,
  1903. .daio_mgr_dsb_dai = daio_mgr_dsb_dai,
  1904. .daio_mgr_enb_dao = daio_mgr_enb_dao,
  1905. .daio_mgr_dsb_dao = daio_mgr_dsb_dao,
  1906. .daio_mgr_dao_init = daio_mgr_dao_init,
  1907. .daio_mgr_set_imaparc = daio_mgr_set_imaparc,
  1908. .daio_mgr_set_imapnxt = daio_mgr_set_imapnxt,
  1909. .daio_mgr_set_imapaddr = daio_mgr_set_imapaddr,
  1910. .daio_mgr_commit_write = daio_mgr_commit_write,
  1911. .set_timer_irq = set_timer_irq,
  1912. .set_timer_tick = set_timer_tick,
  1913. .get_wc = get_wc,
  1914. };
  1915. int create_20k2_hw_obj(struct hw **rhw)
  1916. {
  1917. struct hw20k2 *hw20k2;
  1918. *rhw = NULL;
  1919. hw20k2 = kzalloc(sizeof(*hw20k2), GFP_KERNEL);
  1920. if (!hw20k2)
  1921. return -ENOMEM;
  1922. hw20k2->hw = ct20k2_preset;
  1923. *rhw = &hw20k2->hw;
  1924. return 0;
  1925. }
  1926. int destroy_20k2_hw_obj(struct hw *hw)
  1927. {
  1928. if (hw->io_base)
  1929. hw_card_shutdown(hw);
  1930. kfree(hw);
  1931. return 0;
  1932. }