cthw20k2.c 51 KB

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