wm8904.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601
  1. /*
  2. * wm8904.c -- WM8904 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <sound/wm8904.h>
  29. #include "wm8904.h"
  30. enum wm8904_type {
  31. WM8904,
  32. WM8912,
  33. };
  34. #define WM8904_NUM_DCS_CHANNELS 4
  35. #define WM8904_NUM_SUPPLIES 5
  36. static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
  37. "DCVDD",
  38. "DBVDD",
  39. "AVDD",
  40. "CPVDD",
  41. "MICVDD",
  42. };
  43. /* codec private data */
  44. struct wm8904_priv {
  45. enum wm8904_type devtype;
  46. struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
  47. struct wm8904_pdata *pdata;
  48. int deemph;
  49. /* Platform provided DRC configuration */
  50. const char **drc_texts;
  51. int drc_cfg;
  52. struct soc_enum drc_enum;
  53. /* Platform provided ReTune mobile configuration */
  54. int num_retune_mobile_texts;
  55. const char **retune_mobile_texts;
  56. int retune_mobile_cfg;
  57. struct soc_enum retune_mobile_enum;
  58. /* FLL setup */
  59. int fll_src;
  60. int fll_fref;
  61. int fll_fout;
  62. /* Clocking configuration */
  63. unsigned int mclk_rate;
  64. int sysclk_src;
  65. unsigned int sysclk_rate;
  66. int tdm_width;
  67. int tdm_slots;
  68. int bclk;
  69. int fs;
  70. /* DC servo configuration - cached offset values */
  71. int dcs_state[WM8904_NUM_DCS_CHANNELS];
  72. };
  73. static const u16 wm8904_reg[WM8904_MAX_REGISTER + 1] = {
  74. 0x8904, /* R0 - SW Reset and ID */
  75. 0x0000, /* R1 - Revision */
  76. 0x0000, /* R2 */
  77. 0x0000, /* R3 */
  78. 0x0018, /* R4 - Bias Control 0 */
  79. 0x0000, /* R5 - VMID Control 0 */
  80. 0x0000, /* R6 - Mic Bias Control 0 */
  81. 0x0000, /* R7 - Mic Bias Control 1 */
  82. 0x0001, /* R8 - Analogue DAC 0 */
  83. 0x9696, /* R9 - mic Filter Control */
  84. 0x0001, /* R10 - Analogue ADC 0 */
  85. 0x0000, /* R11 */
  86. 0x0000, /* R12 - Power Management 0 */
  87. 0x0000, /* R13 */
  88. 0x0000, /* R14 - Power Management 2 */
  89. 0x0000, /* R15 - Power Management 3 */
  90. 0x0000, /* R16 */
  91. 0x0000, /* R17 */
  92. 0x0000, /* R18 - Power Management 6 */
  93. 0x0000, /* R19 */
  94. 0x945E, /* R20 - Clock Rates 0 */
  95. 0x0C05, /* R21 - Clock Rates 1 */
  96. 0x0006, /* R22 - Clock Rates 2 */
  97. 0x0000, /* R23 */
  98. 0x0050, /* R24 - Audio Interface 0 */
  99. 0x000A, /* R25 - Audio Interface 1 */
  100. 0x00E4, /* R26 - Audio Interface 2 */
  101. 0x0040, /* R27 - Audio Interface 3 */
  102. 0x0000, /* R28 */
  103. 0x0000, /* R29 */
  104. 0x00C0, /* R30 - DAC Digital Volume Left */
  105. 0x00C0, /* R31 - DAC Digital Volume Right */
  106. 0x0000, /* R32 - DAC Digital 0 */
  107. 0x0008, /* R33 - DAC Digital 1 */
  108. 0x0000, /* R34 */
  109. 0x0000, /* R35 */
  110. 0x00C0, /* R36 - ADC Digital Volume Left */
  111. 0x00C0, /* R37 - ADC Digital Volume Right */
  112. 0x0010, /* R38 - ADC Digital 0 */
  113. 0x0000, /* R39 - Digital Microphone 0 */
  114. 0x01AF, /* R40 - DRC 0 */
  115. 0x3248, /* R41 - DRC 1 */
  116. 0x0000, /* R42 - DRC 2 */
  117. 0x0000, /* R43 - DRC 3 */
  118. 0x0085, /* R44 - Analogue Left Input 0 */
  119. 0x0085, /* R45 - Analogue Right Input 0 */
  120. 0x0044, /* R46 - Analogue Left Input 1 */
  121. 0x0044, /* R47 - Analogue Right Input 1 */
  122. 0x0000, /* R48 */
  123. 0x0000, /* R49 */
  124. 0x0000, /* R50 */
  125. 0x0000, /* R51 */
  126. 0x0000, /* R52 */
  127. 0x0000, /* R53 */
  128. 0x0000, /* R54 */
  129. 0x0000, /* R55 */
  130. 0x0000, /* R56 */
  131. 0x002D, /* R57 - Analogue OUT1 Left */
  132. 0x002D, /* R58 - Analogue OUT1 Right */
  133. 0x0039, /* R59 - Analogue OUT2 Left */
  134. 0x0039, /* R60 - Analogue OUT2 Right */
  135. 0x0000, /* R61 - Analogue OUT12 ZC */
  136. 0x0000, /* R62 */
  137. 0x0000, /* R63 */
  138. 0x0000, /* R64 */
  139. 0x0000, /* R65 */
  140. 0x0000, /* R66 */
  141. 0x0000, /* R67 - DC Servo 0 */
  142. 0x0000, /* R68 - DC Servo 1 */
  143. 0xAAAA, /* R69 - DC Servo 2 */
  144. 0x0000, /* R70 */
  145. 0xAAAA, /* R71 - DC Servo 4 */
  146. 0xAAAA, /* R72 - DC Servo 5 */
  147. 0x0000, /* R73 - DC Servo 6 */
  148. 0x0000, /* R74 - DC Servo 7 */
  149. 0x0000, /* R75 - DC Servo 8 */
  150. 0x0000, /* R76 - DC Servo 9 */
  151. 0x0000, /* R77 - DC Servo Readback 0 */
  152. 0x0000, /* R78 */
  153. 0x0000, /* R79 */
  154. 0x0000, /* R80 */
  155. 0x0000, /* R81 */
  156. 0x0000, /* R82 */
  157. 0x0000, /* R83 */
  158. 0x0000, /* R84 */
  159. 0x0000, /* R85 */
  160. 0x0000, /* R86 */
  161. 0x0000, /* R87 */
  162. 0x0000, /* R88 */
  163. 0x0000, /* R89 */
  164. 0x0000, /* R90 - Analogue HP 0 */
  165. 0x0000, /* R91 */
  166. 0x0000, /* R92 */
  167. 0x0000, /* R93 */
  168. 0x0000, /* R94 - Analogue Lineout 0 */
  169. 0x0000, /* R95 */
  170. 0x0000, /* R96 */
  171. 0x0000, /* R97 */
  172. 0x0000, /* R98 - Charge Pump 0 */
  173. 0x0000, /* R99 */
  174. 0x0000, /* R100 */
  175. 0x0000, /* R101 */
  176. 0x0000, /* R102 */
  177. 0x0000, /* R103 */
  178. 0x0004, /* R104 - Class W 0 */
  179. 0x0000, /* R105 */
  180. 0x0000, /* R106 */
  181. 0x0000, /* R107 */
  182. 0x0000, /* R108 - Write Sequencer 0 */
  183. 0x0000, /* R109 - Write Sequencer 1 */
  184. 0x0000, /* R110 - Write Sequencer 2 */
  185. 0x0000, /* R111 - Write Sequencer 3 */
  186. 0x0000, /* R112 - Write Sequencer 4 */
  187. 0x0000, /* R113 */
  188. 0x0000, /* R114 */
  189. 0x0000, /* R115 */
  190. 0x0000, /* R116 - FLL Control 1 */
  191. 0x0007, /* R117 - FLL Control 2 */
  192. 0x0000, /* R118 - FLL Control 3 */
  193. 0x2EE0, /* R119 - FLL Control 4 */
  194. 0x0004, /* R120 - FLL Control 5 */
  195. 0x0014, /* R121 - GPIO Control 1 */
  196. 0x0010, /* R122 - GPIO Control 2 */
  197. 0x0010, /* R123 - GPIO Control 3 */
  198. 0x0000, /* R124 - GPIO Control 4 */
  199. 0x0000, /* R125 */
  200. 0x0000, /* R126 - Digital Pulls */
  201. 0x0000, /* R127 - Interrupt Status */
  202. 0xFFFF, /* R128 - Interrupt Status Mask */
  203. 0x0000, /* R129 - Interrupt Polarity */
  204. 0x0000, /* R130 - Interrupt Debounce */
  205. 0x0000, /* R131 */
  206. 0x0000, /* R132 */
  207. 0x0000, /* R133 */
  208. 0x0000, /* R134 - EQ1 */
  209. 0x000C, /* R135 - EQ2 */
  210. 0x000C, /* R136 - EQ3 */
  211. 0x000C, /* R137 - EQ4 */
  212. 0x000C, /* R138 - EQ5 */
  213. 0x000C, /* R139 - EQ6 */
  214. 0x0FCA, /* R140 - EQ7 */
  215. 0x0400, /* R141 - EQ8 */
  216. 0x00D8, /* R142 - EQ9 */
  217. 0x1EB5, /* R143 - EQ10 */
  218. 0xF145, /* R144 - EQ11 */
  219. 0x0B75, /* R145 - EQ12 */
  220. 0x01C5, /* R146 - EQ13 */
  221. 0x1C58, /* R147 - EQ14 */
  222. 0xF373, /* R148 - EQ15 */
  223. 0x0A54, /* R149 - EQ16 */
  224. 0x0558, /* R150 - EQ17 */
  225. 0x168E, /* R151 - EQ18 */
  226. 0xF829, /* R152 - EQ19 */
  227. 0x07AD, /* R153 - EQ20 */
  228. 0x1103, /* R154 - EQ21 */
  229. 0x0564, /* R155 - EQ22 */
  230. 0x0559, /* R156 - EQ23 */
  231. 0x4000, /* R157 - EQ24 */
  232. 0x0000, /* R158 */
  233. 0x0000, /* R159 */
  234. 0x0000, /* R160 */
  235. 0x0000, /* R161 - Control Interface Test 1 */
  236. 0x0000, /* R162 */
  237. 0x0000, /* R163 */
  238. 0x0000, /* R164 */
  239. 0x0000, /* R165 */
  240. 0x0000, /* R166 */
  241. 0x0000, /* R167 */
  242. 0x0000, /* R168 */
  243. 0x0000, /* R169 */
  244. 0x0000, /* R170 */
  245. 0x0000, /* R171 */
  246. 0x0000, /* R172 */
  247. 0x0000, /* R173 */
  248. 0x0000, /* R174 */
  249. 0x0000, /* R175 */
  250. 0x0000, /* R176 */
  251. 0x0000, /* R177 */
  252. 0x0000, /* R178 */
  253. 0x0000, /* R179 */
  254. 0x0000, /* R180 */
  255. 0x0000, /* R181 */
  256. 0x0000, /* R182 */
  257. 0x0000, /* R183 */
  258. 0x0000, /* R184 */
  259. 0x0000, /* R185 */
  260. 0x0000, /* R186 */
  261. 0x0000, /* R187 */
  262. 0x0000, /* R188 */
  263. 0x0000, /* R189 */
  264. 0x0000, /* R190 */
  265. 0x0000, /* R191 */
  266. 0x0000, /* R192 */
  267. 0x0000, /* R193 */
  268. 0x0000, /* R194 */
  269. 0x0000, /* R195 */
  270. 0x0000, /* R196 */
  271. 0x0000, /* R197 */
  272. 0x0000, /* R198 */
  273. 0x0000, /* R199 */
  274. 0x0000, /* R200 */
  275. 0x0000, /* R201 */
  276. 0x0000, /* R202 */
  277. 0x0000, /* R203 */
  278. 0x0000, /* R204 - Analogue Output Bias 0 */
  279. 0x0000, /* R205 */
  280. 0x0000, /* R206 */
  281. 0x0000, /* R207 */
  282. 0x0000, /* R208 */
  283. 0x0000, /* R209 */
  284. 0x0000, /* R210 */
  285. 0x0000, /* R211 */
  286. 0x0000, /* R212 */
  287. 0x0000, /* R213 */
  288. 0x0000, /* R214 */
  289. 0x0000, /* R215 */
  290. 0x0000, /* R216 */
  291. 0x0000, /* R217 */
  292. 0x0000, /* R218 */
  293. 0x0000, /* R219 */
  294. 0x0000, /* R220 */
  295. 0x0000, /* R221 */
  296. 0x0000, /* R222 */
  297. 0x0000, /* R223 */
  298. 0x0000, /* R224 */
  299. 0x0000, /* R225 */
  300. 0x0000, /* R226 */
  301. 0x0000, /* R227 */
  302. 0x0000, /* R228 */
  303. 0x0000, /* R229 */
  304. 0x0000, /* R230 */
  305. 0x0000, /* R231 */
  306. 0x0000, /* R232 */
  307. 0x0000, /* R233 */
  308. 0x0000, /* R234 */
  309. 0x0000, /* R235 */
  310. 0x0000, /* R236 */
  311. 0x0000, /* R237 */
  312. 0x0000, /* R238 */
  313. 0x0000, /* R239 */
  314. 0x0000, /* R240 */
  315. 0x0000, /* R241 */
  316. 0x0000, /* R242 */
  317. 0x0000, /* R243 */
  318. 0x0000, /* R244 */
  319. 0x0000, /* R245 */
  320. 0x0000, /* R246 */
  321. 0x0000, /* R247 - FLL NCO Test 0 */
  322. 0x0019, /* R248 - FLL NCO Test 1 */
  323. };
  324. static struct {
  325. int readable;
  326. int writable;
  327. int vol;
  328. } wm8904_access[] = {
  329. { 0xFFFF, 0xFFFF, 1 }, /* R0 - SW Reset and ID */
  330. { 0x0000, 0x0000, 0 }, /* R1 - Revision */
  331. { 0x0000, 0x0000, 0 }, /* R2 */
  332. { 0x0000, 0x0000, 0 }, /* R3 */
  333. { 0x001F, 0x001F, 0 }, /* R4 - Bias Control 0 */
  334. { 0x0047, 0x0047, 0 }, /* R5 - VMID Control 0 */
  335. { 0x007F, 0x007F, 0 }, /* R6 - Mic Bias Control 0 */
  336. { 0xC007, 0xC007, 0 }, /* R7 - Mic Bias Control 1 */
  337. { 0x001E, 0x001E, 0 }, /* R8 - Analogue DAC 0 */
  338. { 0xFFFF, 0xFFFF, 0 }, /* R9 - mic Filter Control */
  339. { 0x0001, 0x0001, 0 }, /* R10 - Analogue ADC 0 */
  340. { 0x0000, 0x0000, 0 }, /* R11 */
  341. { 0x0003, 0x0003, 0 }, /* R12 - Power Management 0 */
  342. { 0x0000, 0x0000, 0 }, /* R13 */
  343. { 0x0003, 0x0003, 0 }, /* R14 - Power Management 2 */
  344. { 0x0003, 0x0003, 0 }, /* R15 - Power Management 3 */
  345. { 0x0000, 0x0000, 0 }, /* R16 */
  346. { 0x0000, 0x0000, 0 }, /* R17 */
  347. { 0x000F, 0x000F, 0 }, /* R18 - Power Management 6 */
  348. { 0x0000, 0x0000, 0 }, /* R19 */
  349. { 0x7001, 0x7001, 0 }, /* R20 - Clock Rates 0 */
  350. { 0x3C07, 0x3C07, 0 }, /* R21 - Clock Rates 1 */
  351. { 0xD00F, 0xD00F, 0 }, /* R22 - Clock Rates 2 */
  352. { 0x0000, 0x0000, 0 }, /* R23 */
  353. { 0x1FFF, 0x1FFF, 0 }, /* R24 - Audio Interface 0 */
  354. { 0x3DDF, 0x3DDF, 0 }, /* R25 - Audio Interface 1 */
  355. { 0x0F1F, 0x0F1F, 0 }, /* R26 - Audio Interface 2 */
  356. { 0x0FFF, 0x0FFF, 0 }, /* R27 - Audio Interface 3 */
  357. { 0x0000, 0x0000, 0 }, /* R28 */
  358. { 0x0000, 0x0000, 0 }, /* R29 */
  359. { 0x00FF, 0x01FF, 0 }, /* R30 - DAC Digital Volume Left */
  360. { 0x00FF, 0x01FF, 0 }, /* R31 - DAC Digital Volume Right */
  361. { 0x0FFF, 0x0FFF, 0 }, /* R32 - DAC Digital 0 */
  362. { 0x1E4E, 0x1E4E, 0 }, /* R33 - DAC Digital 1 */
  363. { 0x0000, 0x0000, 0 }, /* R34 */
  364. { 0x0000, 0x0000, 0 }, /* R35 */
  365. { 0x00FF, 0x01FF, 0 }, /* R36 - ADC Digital Volume Left */
  366. { 0x00FF, 0x01FF, 0 }, /* R37 - ADC Digital Volume Right */
  367. { 0x0073, 0x0073, 0 }, /* R38 - ADC Digital 0 */
  368. { 0x1800, 0x1800, 0 }, /* R39 - Digital Microphone 0 */
  369. { 0xDFEF, 0xDFEF, 0 }, /* R40 - DRC 0 */
  370. { 0xFFFF, 0xFFFF, 0 }, /* R41 - DRC 1 */
  371. { 0x003F, 0x003F, 0 }, /* R42 - DRC 2 */
  372. { 0x07FF, 0x07FF, 0 }, /* R43 - DRC 3 */
  373. { 0x009F, 0x009F, 0 }, /* R44 - Analogue Left Input 0 */
  374. { 0x009F, 0x009F, 0 }, /* R45 - Analogue Right Input 0 */
  375. { 0x007F, 0x007F, 0 }, /* R46 - Analogue Left Input 1 */
  376. { 0x007F, 0x007F, 0 }, /* R47 - Analogue Right Input 1 */
  377. { 0x0000, 0x0000, 0 }, /* R48 */
  378. { 0x0000, 0x0000, 0 }, /* R49 */
  379. { 0x0000, 0x0000, 0 }, /* R50 */
  380. { 0x0000, 0x0000, 0 }, /* R51 */
  381. { 0x0000, 0x0000, 0 }, /* R52 */
  382. { 0x0000, 0x0000, 0 }, /* R53 */
  383. { 0x0000, 0x0000, 0 }, /* R54 */
  384. { 0x0000, 0x0000, 0 }, /* R55 */
  385. { 0x0000, 0x0000, 0 }, /* R56 */
  386. { 0x017F, 0x01FF, 0 }, /* R57 - Analogue OUT1 Left */
  387. { 0x017F, 0x01FF, 0 }, /* R58 - Analogue OUT1 Right */
  388. { 0x017F, 0x01FF, 0 }, /* R59 - Analogue OUT2 Left */
  389. { 0x017F, 0x01FF, 0 }, /* R60 - Analogue OUT2 Right */
  390. { 0x000F, 0x000F, 0 }, /* R61 - Analogue OUT12 ZC */
  391. { 0x0000, 0x0000, 0 }, /* R62 */
  392. { 0x0000, 0x0000, 0 }, /* R63 */
  393. { 0x0000, 0x0000, 0 }, /* R64 */
  394. { 0x0000, 0x0000, 0 }, /* R65 */
  395. { 0x0000, 0x0000, 0 }, /* R66 */
  396. { 0x000F, 0x000F, 0 }, /* R67 - DC Servo 0 */
  397. { 0xFFFF, 0xFFFF, 1 }, /* R68 - DC Servo 1 */
  398. { 0x0F0F, 0x0F0F, 0 }, /* R69 - DC Servo 2 */
  399. { 0x0000, 0x0000, 0 }, /* R70 */
  400. { 0x007F, 0x007F, 0 }, /* R71 - DC Servo 4 */
  401. { 0x007F, 0x007F, 0 }, /* R72 - DC Servo 5 */
  402. { 0x00FF, 0x00FF, 1 }, /* R73 - DC Servo 6 */
  403. { 0x00FF, 0x00FF, 1 }, /* R74 - DC Servo 7 */
  404. { 0x00FF, 0x00FF, 1 }, /* R75 - DC Servo 8 */
  405. { 0x00FF, 0x00FF, 1 }, /* R76 - DC Servo 9 */
  406. { 0x0FFF, 0x0000, 1 }, /* R77 - DC Servo Readback 0 */
  407. { 0x0000, 0x0000, 0 }, /* R78 */
  408. { 0x0000, 0x0000, 0 }, /* R79 */
  409. { 0x0000, 0x0000, 0 }, /* R80 */
  410. { 0x0000, 0x0000, 0 }, /* R81 */
  411. { 0x0000, 0x0000, 0 }, /* R82 */
  412. { 0x0000, 0x0000, 0 }, /* R83 */
  413. { 0x0000, 0x0000, 0 }, /* R84 */
  414. { 0x0000, 0x0000, 0 }, /* R85 */
  415. { 0x0000, 0x0000, 0 }, /* R86 */
  416. { 0x0000, 0x0000, 0 }, /* R87 */
  417. { 0x0000, 0x0000, 0 }, /* R88 */
  418. { 0x0000, 0x0000, 0 }, /* R89 */
  419. { 0x00FF, 0x00FF, 0 }, /* R90 - Analogue HP 0 */
  420. { 0x0000, 0x0000, 0 }, /* R91 */
  421. { 0x0000, 0x0000, 0 }, /* R92 */
  422. { 0x0000, 0x0000, 0 }, /* R93 */
  423. { 0x00FF, 0x00FF, 0 }, /* R94 - Analogue Lineout 0 */
  424. { 0x0000, 0x0000, 0 }, /* R95 */
  425. { 0x0000, 0x0000, 0 }, /* R96 */
  426. { 0x0000, 0x0000, 0 }, /* R97 */
  427. { 0x0001, 0x0001, 0 }, /* R98 - Charge Pump 0 */
  428. { 0x0000, 0x0000, 0 }, /* R99 */
  429. { 0x0000, 0x0000, 0 }, /* R100 */
  430. { 0x0000, 0x0000, 0 }, /* R101 */
  431. { 0x0000, 0x0000, 0 }, /* R102 */
  432. { 0x0000, 0x0000, 0 }, /* R103 */
  433. { 0x0001, 0x0001, 0 }, /* R104 - Class W 0 */
  434. { 0x0000, 0x0000, 0 }, /* R105 */
  435. { 0x0000, 0x0000, 0 }, /* R106 */
  436. { 0x0000, 0x0000, 0 }, /* R107 */
  437. { 0x011F, 0x011F, 0 }, /* R108 - Write Sequencer 0 */
  438. { 0x7FFF, 0x7FFF, 0 }, /* R109 - Write Sequencer 1 */
  439. { 0x4FFF, 0x4FFF, 0 }, /* R110 - Write Sequencer 2 */
  440. { 0x003F, 0x033F, 0 }, /* R111 - Write Sequencer 3 */
  441. { 0x03F1, 0x0000, 0 }, /* R112 - Write Sequencer 4 */
  442. { 0x0000, 0x0000, 0 }, /* R113 */
  443. { 0x0000, 0x0000, 0 }, /* R114 */
  444. { 0x0000, 0x0000, 0 }, /* R115 */
  445. { 0x0007, 0x0007, 0 }, /* R116 - FLL Control 1 */
  446. { 0x3F77, 0x3F77, 0 }, /* R117 - FLL Control 2 */
  447. { 0xFFFF, 0xFFFF, 0 }, /* R118 - FLL Control 3 */
  448. { 0x7FEF, 0x7FEF, 0 }, /* R119 - FLL Control 4 */
  449. { 0x001B, 0x001B, 0 }, /* R120 - FLL Control 5 */
  450. { 0x003F, 0x003F, 0 }, /* R121 - GPIO Control 1 */
  451. { 0x003F, 0x003F, 0 }, /* R122 - GPIO Control 2 */
  452. { 0x003F, 0x003F, 0 }, /* R123 - GPIO Control 3 */
  453. { 0x038F, 0x038F, 0 }, /* R124 - GPIO Control 4 */
  454. { 0x0000, 0x0000, 0 }, /* R125 */
  455. { 0x00FF, 0x00FF, 0 }, /* R126 - Digital Pulls */
  456. { 0x07FF, 0x03FF, 1 }, /* R127 - Interrupt Status */
  457. { 0x03FF, 0x03FF, 0 }, /* R128 - Interrupt Status Mask */
  458. { 0x03FF, 0x03FF, 0 }, /* R129 - Interrupt Polarity */
  459. { 0x03FF, 0x03FF, 0 }, /* R130 - Interrupt Debounce */
  460. { 0x0000, 0x0000, 0 }, /* R131 */
  461. { 0x0000, 0x0000, 0 }, /* R132 */
  462. { 0x0000, 0x0000, 0 }, /* R133 */
  463. { 0x0001, 0x0001, 0 }, /* R134 - EQ1 */
  464. { 0x001F, 0x001F, 0 }, /* R135 - EQ2 */
  465. { 0x001F, 0x001F, 0 }, /* R136 - EQ3 */
  466. { 0x001F, 0x001F, 0 }, /* R137 - EQ4 */
  467. { 0x001F, 0x001F, 0 }, /* R138 - EQ5 */
  468. { 0x001F, 0x001F, 0 }, /* R139 - EQ6 */
  469. { 0xFFFF, 0xFFFF, 0 }, /* R140 - EQ7 */
  470. { 0xFFFF, 0xFFFF, 0 }, /* R141 - EQ8 */
  471. { 0xFFFF, 0xFFFF, 0 }, /* R142 - EQ9 */
  472. { 0xFFFF, 0xFFFF, 0 }, /* R143 - EQ10 */
  473. { 0xFFFF, 0xFFFF, 0 }, /* R144 - EQ11 */
  474. { 0xFFFF, 0xFFFF, 0 }, /* R145 - EQ12 */
  475. { 0xFFFF, 0xFFFF, 0 }, /* R146 - EQ13 */
  476. { 0xFFFF, 0xFFFF, 0 }, /* R147 - EQ14 */
  477. { 0xFFFF, 0xFFFF, 0 }, /* R148 - EQ15 */
  478. { 0xFFFF, 0xFFFF, 0 }, /* R149 - EQ16 */
  479. { 0xFFFF, 0xFFFF, 0 }, /* R150 - EQ17 */
  480. { 0xFFFF, 0xFFFF, 0 }, /* R151wm8523_dai - EQ18 */
  481. { 0xFFFF, 0xFFFF, 0 }, /* R152 - EQ19 */
  482. { 0xFFFF, 0xFFFF, 0 }, /* R153 - EQ20 */
  483. { 0xFFFF, 0xFFFF, 0 }, /* R154 - EQ21 */
  484. { 0xFFFF, 0xFFFF, 0 }, /* R155 - EQ22 */
  485. { 0xFFFF, 0xFFFF, 0 }, /* R156 - EQ23 */
  486. { 0xFFFF, 0xFFFF, 0 }, /* R157 - EQ24 */
  487. { 0x0000, 0x0000, 0 }, /* R158 */
  488. { 0x0000, 0x0000, 0 }, /* R159 */
  489. { 0x0000, 0x0000, 0 }, /* R160 */
  490. { 0x0002, 0x0002, 0 }, /* R161 - Control Interface Test 1 */
  491. { 0x0000, 0x0000, 0 }, /* R162 */
  492. { 0x0000, 0x0000, 0 }, /* R163 */
  493. { 0x0000, 0x0000, 0 }, /* R164 */
  494. { 0x0000, 0x0000, 0 }, /* R165 */
  495. { 0x0000, 0x0000, 0 }, /* R166 */
  496. { 0x0000, 0x0000, 0 }, /* R167 */
  497. { 0x0000, 0x0000, 0 }, /* R168 */
  498. { 0x0000, 0x0000, 0 }, /* R169 */
  499. { 0x0000, 0x0000, 0 }, /* R170 */
  500. { 0x0000, 0x0000, 0 }, /* R171 */
  501. { 0x0000, 0x0000, 0 }, /* R172 */
  502. { 0x0000, 0x0000, 0 }, /* R173 */
  503. { 0x0000, 0x0000, 0 }, /* R174 */
  504. { 0x0000, 0x0000, 0 }, /* R175 */
  505. { 0x0000, 0x0000, 0 }, /* R176 */
  506. { 0x0000, 0x0000, 0 }, /* R177 */
  507. { 0x0000, 0x0000, 0 }, /* R178 */
  508. { 0x0000, 0x0000, 0 }, /* R179 */
  509. { 0x0000, 0x0000, 0 }, /* R180 */
  510. { 0x0000, 0x0000, 0 }, /* R181 */
  511. { 0x0000, 0x0000, 0 }, /* R182 */
  512. { 0x0000, 0x0000, 0 }, /* R183 */
  513. { 0x0000, 0x0000, 0 }, /* R184 */
  514. { 0x0000, 0x0000, 0 }, /* R185 */
  515. { 0x0000, 0x0000, 0 }, /* R186 */
  516. { 0x0000, 0x0000, 0 }, /* R187 */
  517. { 0x0000, 0x0000, 0 }, /* R188 */
  518. { 0x0000, 0x0000, 0 }, /* R189 */
  519. { 0x0000, 0x0000, 0 }, /* R190 */
  520. { 0x0000, 0x0000, 0 }, /* R191 */
  521. { 0x0000, 0x0000, 0 }, /* R192 */
  522. { 0x0000, 0x0000, 0 }, /* R193 */
  523. { 0x0000, 0x0000, 0 }, /* R194 */
  524. { 0x0000, 0x0000, 0 }, /* R195 */
  525. { 0x0000, 0x0000, 0 }, /* R196 */
  526. { 0x0000, 0x0000, 0 }, /* R197 */
  527. { 0x0000, 0x0000, 0 }, /* R198 */
  528. { 0x0000, 0x0000, 0 }, /* R199 */
  529. { 0x0000, 0x0000, 0 }, /* R200 */
  530. { 0x0000, 0x0000, 0 }, /* R201 */
  531. { 0x0000, 0x0000, 0 }, /* R202 */
  532. { 0x0000, 0x0000, 0 }, /* R203 */
  533. { 0x0070, 0x0070, 0 }, /* R204 - Analogue Output Bias 0 */
  534. { 0x0000, 0x0000, 0 }, /* R205 */
  535. { 0x0000, 0x0000, 0 }, /* R206 */
  536. { 0x0000, 0x0000, 0 }, /* R207 */
  537. { 0x0000, 0x0000, 0 }, /* R208 */
  538. { 0x0000, 0x0000, 0 }, /* R209 */
  539. { 0x0000, 0x0000, 0 }, /* R210 */
  540. { 0x0000, 0x0000, 0 }, /* R211 */
  541. { 0x0000, 0x0000, 0 }, /* R212 */
  542. { 0x0000, 0x0000, 0 }, /* R213 */
  543. { 0x0000, 0x0000, 0 }, /* R214 */
  544. { 0x0000, 0x0000, 0 }, /* R215 */
  545. { 0x0000, 0x0000, 0 }, /* R216 */
  546. { 0x0000, 0x0000, 0 }, /* R217 */
  547. { 0x0000, 0x0000, 0 }, /* R218 */
  548. { 0x0000, 0x0000, 0 }, /* R219 */
  549. { 0x0000, 0x0000, 0 }, /* R220 */
  550. { 0x0000, 0x0000, 0 }, /* R221 */
  551. { 0x0000, 0x0000, 0 }, /* R222 */
  552. { 0x0000, 0x0000, 0 }, /* R223 */
  553. { 0x0000, 0x0000, 0 }, /* R224 */
  554. { 0x0000, 0x0000, 0 }, /* R225 */
  555. { 0x0000, 0x0000, 0 }, /* R226 */
  556. { 0x0000, 0x0000, 0 }, /* R227 */
  557. { 0x0000, 0x0000, 0 }, /* R228 */
  558. { 0x0000, 0x0000, 0 }, /* R229 */
  559. { 0x0000, 0x0000, 0 }, /* R230 */
  560. { 0x0000, 0x0000, 0 }, /* R231 */
  561. { 0x0000, 0x0000, 0 }, /* R232 */
  562. { 0x0000, 0x0000, 0 }, /* R233 */
  563. { 0x0000, 0x0000, 0 }, /* R234 */
  564. { 0x0000, 0x0000, 0 }, /* R235 */
  565. { 0x0000, 0x0000, 0 }, /* R236 */
  566. { 0x0000, 0x0000, 0 }, /* R237 */
  567. { 0x0000, 0x0000, 0 }, /* R238 */
  568. { 0x0000, 0x0000, 0 }, /* R239 */
  569. { 0x0000, 0x0000, 0 }, /* R240 */
  570. { 0x0000, 0x0000, 0 }, /* R241 */
  571. { 0x0000, 0x0000, 0 }, /* R242 */
  572. { 0x0000, 0x0000, 0 }, /* R243 */
  573. { 0x0000, 0x0000, 0 }, /* R244 */
  574. { 0x0000, 0x0000, 0 }, /* R245 */
  575. { 0x0000, 0x0000, 0 }, /* R246 */
  576. { 0x0001, 0x0001, 0 }, /* R247 - FLL NCO Test 0 */
  577. { 0x003F, 0x003F, 0 }, /* R248 - FLL NCO Test 1 */
  578. };
  579. static int wm8904_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
  580. {
  581. return wm8904_access[reg].vol;
  582. }
  583. static int wm8904_reset(struct snd_soc_codec *codec)
  584. {
  585. return snd_soc_write(codec, WM8904_SW_RESET_AND_ID, 0);
  586. }
  587. static int wm8904_configure_clocking(struct snd_soc_codec *codec)
  588. {
  589. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  590. unsigned int clock0, clock2, rate;
  591. /* Gate the clock while we're updating to avoid misclocking */
  592. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  593. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  594. WM8904_SYSCLK_SRC, 0);
  595. /* This should be done on init() for bypass paths */
  596. switch (wm8904->sysclk_src) {
  597. case WM8904_CLK_MCLK:
  598. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
  599. clock2 &= ~WM8904_SYSCLK_SRC;
  600. rate = wm8904->mclk_rate;
  601. /* Ensure the FLL is stopped */
  602. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  603. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  604. break;
  605. case WM8904_CLK_FLL:
  606. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  607. wm8904->fll_fout);
  608. clock2 |= WM8904_SYSCLK_SRC;
  609. rate = wm8904->fll_fout;
  610. break;
  611. default:
  612. dev_err(codec->dev, "System clock not configured\n");
  613. return -EINVAL;
  614. }
  615. /* SYSCLK shouldn't be over 13.5MHz */
  616. if (rate > 13500000) {
  617. clock0 = WM8904_MCLK_DIV;
  618. wm8904->sysclk_rate = rate / 2;
  619. } else {
  620. clock0 = 0;
  621. wm8904->sysclk_rate = rate;
  622. }
  623. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
  624. clock0);
  625. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  626. WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
  627. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
  628. return 0;
  629. }
  630. static void wm8904_set_drc(struct snd_soc_codec *codec)
  631. {
  632. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  633. struct wm8904_pdata *pdata = wm8904->pdata;
  634. int save, i;
  635. /* Save any enables; the configuration should clear them. */
  636. save = snd_soc_read(codec, WM8904_DRC_0);
  637. for (i = 0; i < WM8904_DRC_REGS; i++)
  638. snd_soc_update_bits(codec, WM8904_DRC_0 + i, 0xffff,
  639. pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
  640. /* Reenable the DRC */
  641. snd_soc_update_bits(codec, WM8904_DRC_0,
  642. WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
  643. }
  644. static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
  645. struct snd_ctl_elem_value *ucontrol)
  646. {
  647. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  648. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  649. struct wm8904_pdata *pdata = wm8904->pdata;
  650. int value = ucontrol->value.integer.value[0];
  651. if (value >= pdata->num_drc_cfgs)
  652. return -EINVAL;
  653. wm8904->drc_cfg = value;
  654. wm8904_set_drc(codec);
  655. return 0;
  656. }
  657. static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
  658. struct snd_ctl_elem_value *ucontrol)
  659. {
  660. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  661. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  662. ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
  663. return 0;
  664. }
  665. static void wm8904_set_retune_mobile(struct snd_soc_codec *codec)
  666. {
  667. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  668. struct wm8904_pdata *pdata = wm8904->pdata;
  669. int best, best_val, save, i, cfg;
  670. if (!pdata || !wm8904->num_retune_mobile_texts)
  671. return;
  672. /* Find the version of the currently selected configuration
  673. * with the nearest sample rate. */
  674. cfg = wm8904->retune_mobile_cfg;
  675. best = 0;
  676. best_val = INT_MAX;
  677. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  678. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  679. wm8904->retune_mobile_texts[cfg]) == 0 &&
  680. abs(pdata->retune_mobile_cfgs[i].rate
  681. - wm8904->fs) < best_val) {
  682. best = i;
  683. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  684. - wm8904->fs);
  685. }
  686. }
  687. dev_dbg(codec->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
  688. pdata->retune_mobile_cfgs[best].name,
  689. pdata->retune_mobile_cfgs[best].rate,
  690. wm8904->fs);
  691. /* The EQ will be disabled while reconfiguring it, remember the
  692. * current configuration.
  693. */
  694. save = snd_soc_read(codec, WM8904_EQ1);
  695. for (i = 0; i < WM8904_EQ_REGS; i++)
  696. snd_soc_update_bits(codec, WM8904_EQ1 + i, 0xffff,
  697. pdata->retune_mobile_cfgs[best].regs[i]);
  698. snd_soc_update_bits(codec, WM8904_EQ1, WM8904_EQ_ENA, save);
  699. }
  700. static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  701. struct snd_ctl_elem_value *ucontrol)
  702. {
  703. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  704. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  705. struct wm8904_pdata *pdata = wm8904->pdata;
  706. int value = ucontrol->value.integer.value[0];
  707. if (value >= pdata->num_retune_mobile_cfgs)
  708. return -EINVAL;
  709. wm8904->retune_mobile_cfg = value;
  710. wm8904_set_retune_mobile(codec);
  711. return 0;
  712. }
  713. static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  714. struct snd_ctl_elem_value *ucontrol)
  715. {
  716. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  717. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  718. ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
  719. return 0;
  720. }
  721. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  722. static int wm8904_set_deemph(struct snd_soc_codec *codec)
  723. {
  724. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  725. int val, i, best;
  726. /* If we're using deemphasis select the nearest available sample
  727. * rate.
  728. */
  729. if (wm8904->deemph) {
  730. best = 1;
  731. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  732. if (abs(deemph_settings[i] - wm8904->fs) <
  733. abs(deemph_settings[best] - wm8904->fs))
  734. best = i;
  735. }
  736. val = best << WM8904_DEEMPH_SHIFT;
  737. } else {
  738. val = 0;
  739. }
  740. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  741. return snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  742. WM8904_DEEMPH_MASK, val);
  743. }
  744. static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
  745. struct snd_ctl_elem_value *ucontrol)
  746. {
  747. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  748. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  749. ucontrol->value.enumerated.item[0] = wm8904->deemph;
  750. return 0;
  751. }
  752. static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
  753. struct snd_ctl_elem_value *ucontrol)
  754. {
  755. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  756. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  757. int deemph = ucontrol->value.enumerated.item[0];
  758. if (deemph > 1)
  759. return -EINVAL;
  760. wm8904->deemph = deemph;
  761. return wm8904_set_deemph(codec);
  762. }
  763. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  764. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  765. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  766. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  767. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  768. static const char *input_mode_text[] = {
  769. "Single-Ended", "Differential Line", "Differential Mic"
  770. };
  771. static const struct soc_enum lin_mode =
  772. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  773. static const struct soc_enum rin_mode =
  774. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  775. static const char *hpf_mode_text[] = {
  776. "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
  777. };
  778. static const struct soc_enum hpf_mode =
  779. SOC_ENUM_SINGLE(WM8904_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
  780. static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
  781. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
  782. WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
  783. SOC_ENUM("Left Caputure Mode", lin_mode),
  784. SOC_ENUM("Right Capture Mode", rin_mode),
  785. /* No TLV since it depends on mode */
  786. SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
  787. WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
  788. SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
  789. WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 1),
  790. SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
  791. SOC_ENUM("High Pass Filter Mode", hpf_mode),
  792. SOC_SINGLE("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0),
  793. };
  794. static const char *drc_path_text[] = {
  795. "ADC", "DAC"
  796. };
  797. static const struct soc_enum drc_path =
  798. SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
  799. static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
  800. SOC_SINGLE_TLV("Digital Playback Boost Volume",
  801. WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
  802. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
  803. WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  804. SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
  805. WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
  806. SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
  807. WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
  808. SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
  809. WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
  810. SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
  811. WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
  812. SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
  813. WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
  814. SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
  815. WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
  816. SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
  817. SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
  818. SOC_ENUM("DRC Path", drc_path),
  819. SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
  820. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  821. wm8904_get_deemph, wm8904_put_deemph),
  822. };
  823. static const struct snd_kcontrol_new wm8904_snd_controls[] = {
  824. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
  825. sidetone_tlv),
  826. };
  827. static const struct snd_kcontrol_new wm8904_eq_controls[] = {
  828. SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
  829. SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
  830. SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
  831. SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
  832. SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
  833. };
  834. static int cp_event(struct snd_soc_dapm_widget *w,
  835. struct snd_kcontrol *kcontrol, int event)
  836. {
  837. BUG_ON(event != SND_SOC_DAPM_POST_PMU);
  838. /* Maximum startup time */
  839. udelay(500);
  840. return 0;
  841. }
  842. static int sysclk_event(struct snd_soc_dapm_widget *w,
  843. struct snd_kcontrol *kcontrol, int event)
  844. {
  845. struct snd_soc_codec *codec = w->codec;
  846. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  847. switch (event) {
  848. case SND_SOC_DAPM_PRE_PMU:
  849. /* If we're using the FLL then we only start it when
  850. * required; we assume that the configuration has been
  851. * done previously and all we need to do is kick it
  852. * off.
  853. */
  854. switch (wm8904->sysclk_src) {
  855. case WM8904_CLK_FLL:
  856. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  857. WM8904_FLL_OSC_ENA,
  858. WM8904_FLL_OSC_ENA);
  859. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  860. WM8904_FLL_ENA,
  861. WM8904_FLL_ENA);
  862. break;
  863. default:
  864. break;
  865. }
  866. break;
  867. case SND_SOC_DAPM_POST_PMD:
  868. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  869. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  870. break;
  871. }
  872. return 0;
  873. }
  874. static int out_pga_event(struct snd_soc_dapm_widget *w,
  875. struct snd_kcontrol *kcontrol, int event)
  876. {
  877. struct snd_soc_codec *codec = w->codec;
  878. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  879. int reg, val;
  880. int dcs_mask;
  881. int dcs_l, dcs_r;
  882. int dcs_l_reg, dcs_r_reg;
  883. int timeout;
  884. int pwr_reg;
  885. /* This code is shared between HP and LINEOUT; we do all our
  886. * power management in stereo pairs to avoid latency issues so
  887. * we reuse shift to identify which rather than strcmp() the
  888. * name. */
  889. reg = w->shift;
  890. switch (reg) {
  891. case WM8904_ANALOGUE_HP_0:
  892. pwr_reg = WM8904_POWER_MANAGEMENT_2;
  893. dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
  894. dcs_r_reg = WM8904_DC_SERVO_8;
  895. dcs_l_reg = WM8904_DC_SERVO_9;
  896. dcs_l = 0;
  897. dcs_r = 1;
  898. break;
  899. case WM8904_ANALOGUE_LINEOUT_0:
  900. pwr_reg = WM8904_POWER_MANAGEMENT_3;
  901. dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
  902. dcs_r_reg = WM8904_DC_SERVO_6;
  903. dcs_l_reg = WM8904_DC_SERVO_7;
  904. dcs_l = 2;
  905. dcs_r = 3;
  906. break;
  907. default:
  908. BUG();
  909. return -EINVAL;
  910. }
  911. switch (event) {
  912. case SND_SOC_DAPM_PRE_PMU:
  913. /* Power on the PGAs */
  914. snd_soc_update_bits(codec, pwr_reg,
  915. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  916. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
  917. /* Power on the amplifier */
  918. snd_soc_update_bits(codec, reg,
  919. WM8904_HPL_ENA | WM8904_HPR_ENA,
  920. WM8904_HPL_ENA | WM8904_HPR_ENA);
  921. /* Enable the first stage */
  922. snd_soc_update_bits(codec, reg,
  923. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
  924. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
  925. /* Power up the DC servo */
  926. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  927. dcs_mask, dcs_mask);
  928. /* Either calibrate the DC servo or restore cached state
  929. * if we have that.
  930. */
  931. if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
  932. dev_dbg(codec->dev, "Restoring DC servo state\n");
  933. snd_soc_write(codec, dcs_l_reg,
  934. wm8904->dcs_state[dcs_l]);
  935. snd_soc_write(codec, dcs_r_reg,
  936. wm8904->dcs_state[dcs_r]);
  937. snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
  938. timeout = 20;
  939. } else {
  940. dev_dbg(codec->dev, "Calibrating DC servo\n");
  941. snd_soc_write(codec, WM8904_DC_SERVO_1,
  942. dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
  943. timeout = 500;
  944. }
  945. /* Wait for DC servo to complete */
  946. dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
  947. do {
  948. val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
  949. if ((val & dcs_mask) == dcs_mask)
  950. break;
  951. msleep(1);
  952. } while (--timeout);
  953. if ((val & dcs_mask) != dcs_mask)
  954. dev_warn(codec->dev, "DC servo timed out\n");
  955. else
  956. dev_dbg(codec->dev, "DC servo ready\n");
  957. /* Enable the output stage */
  958. snd_soc_update_bits(codec, reg,
  959. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  960. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
  961. break;
  962. case SND_SOC_DAPM_POST_PMU:
  963. /* Unshort the output itself */
  964. snd_soc_update_bits(codec, reg,
  965. WM8904_HPL_RMV_SHORT |
  966. WM8904_HPR_RMV_SHORT,
  967. WM8904_HPL_RMV_SHORT |
  968. WM8904_HPR_RMV_SHORT);
  969. break;
  970. case SND_SOC_DAPM_PRE_PMD:
  971. /* Short the output */
  972. snd_soc_update_bits(codec, reg,
  973. WM8904_HPL_RMV_SHORT |
  974. WM8904_HPR_RMV_SHORT, 0);
  975. break;
  976. case SND_SOC_DAPM_POST_PMD:
  977. /* Cache the DC servo configuration; this will be
  978. * invalidated if we change the configuration. */
  979. wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
  980. wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
  981. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  982. dcs_mask, 0);
  983. /* Disable the amplifier input and output stages */
  984. snd_soc_update_bits(codec, reg,
  985. WM8904_HPL_ENA | WM8904_HPR_ENA |
  986. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
  987. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  988. 0);
  989. /* PGAs too */
  990. snd_soc_update_bits(codec, pwr_reg,
  991. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  992. 0);
  993. break;
  994. }
  995. return 0;
  996. }
  997. static const char *lin_text[] = {
  998. "IN1L", "IN2L", "IN3L"
  999. };
  1000. static const struct soc_enum lin_enum =
  1001. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
  1002. static const struct snd_kcontrol_new lin_mux =
  1003. SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
  1004. static const struct soc_enum lin_inv_enum =
  1005. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
  1006. static const struct snd_kcontrol_new lin_inv_mux =
  1007. SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
  1008. static const char *rin_text[] = {
  1009. "IN1R", "IN2R", "IN3R"
  1010. };
  1011. static const struct soc_enum rin_enum =
  1012. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
  1013. static const struct snd_kcontrol_new rin_mux =
  1014. SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
  1015. static const struct soc_enum rin_inv_enum =
  1016. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
  1017. static const struct snd_kcontrol_new rin_inv_mux =
  1018. SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
  1019. static const char *aif_text[] = {
  1020. "Left", "Right"
  1021. };
  1022. static const struct soc_enum aifoutl_enum =
  1023. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
  1024. static const struct snd_kcontrol_new aifoutl_mux =
  1025. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  1026. static const struct soc_enum aifoutr_enum =
  1027. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
  1028. static const struct snd_kcontrol_new aifoutr_mux =
  1029. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  1030. static const struct soc_enum aifinl_enum =
  1031. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
  1032. static const struct snd_kcontrol_new aifinl_mux =
  1033. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  1034. static const struct soc_enum aifinr_enum =
  1035. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
  1036. static const struct snd_kcontrol_new aifinr_mux =
  1037. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  1038. static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
  1039. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
  1040. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1041. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
  1042. SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
  1043. };
  1044. static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
  1045. SND_SOC_DAPM_INPUT("IN1L"),
  1046. SND_SOC_DAPM_INPUT("IN1R"),
  1047. SND_SOC_DAPM_INPUT("IN2L"),
  1048. SND_SOC_DAPM_INPUT("IN2R"),
  1049. SND_SOC_DAPM_INPUT("IN3L"),
  1050. SND_SOC_DAPM_INPUT("IN3R"),
  1051. SND_SOC_DAPM_MICBIAS("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0),
  1052. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
  1053. SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  1054. &lin_inv_mux),
  1055. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
  1056. SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  1057. &rin_inv_mux),
  1058. SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
  1059. NULL, 0),
  1060. SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
  1061. NULL, 0),
  1062. SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
  1063. SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
  1064. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  1065. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  1066. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  1067. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  1068. };
  1069. static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
  1070. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  1071. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  1072. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  1073. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  1074. SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
  1075. SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
  1076. SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
  1077. SND_SOC_DAPM_POST_PMU),
  1078. SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  1079. SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  1080. SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  1081. SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  1082. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
  1083. 0, NULL, 0, out_pga_event,
  1084. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1085. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1086. SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
  1087. 0, NULL, 0, out_pga_event,
  1088. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1089. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1090. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  1091. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  1092. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  1093. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  1094. };
  1095. static const char *out_mux_text[] = {
  1096. "DAC", "Bypass"
  1097. };
  1098. static const struct soc_enum hpl_enum =
  1099. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
  1100. static const struct snd_kcontrol_new hpl_mux =
  1101. SOC_DAPM_ENUM("HPL Mux", hpl_enum);
  1102. static const struct soc_enum hpr_enum =
  1103. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
  1104. static const struct snd_kcontrol_new hpr_mux =
  1105. SOC_DAPM_ENUM("HPR Mux", hpr_enum);
  1106. static const struct soc_enum linel_enum =
  1107. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
  1108. static const struct snd_kcontrol_new linel_mux =
  1109. SOC_DAPM_ENUM("LINEL Mux", linel_enum);
  1110. static const struct soc_enum liner_enum =
  1111. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
  1112. static const struct snd_kcontrol_new liner_mux =
  1113. SOC_DAPM_ENUM("LINEL Mux", liner_enum);
  1114. static const char *sidetone_text[] = {
  1115. "None", "Left", "Right"
  1116. };
  1117. static const struct soc_enum dacl_sidetone_enum =
  1118. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
  1119. static const struct snd_kcontrol_new dacl_sidetone_mux =
  1120. SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
  1121. static const struct soc_enum dacr_sidetone_enum =
  1122. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
  1123. static const struct snd_kcontrol_new dacr_sidetone_mux =
  1124. SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
  1125. static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
  1126. SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
  1127. SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  1128. SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  1129. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
  1130. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
  1131. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  1132. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  1133. SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
  1134. SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
  1135. };
  1136. static const struct snd_soc_dapm_route core_intercon[] = {
  1137. { "CLK_DSP", NULL, "SYSCLK" },
  1138. { "TOCLK", NULL, "SYSCLK" },
  1139. };
  1140. static const struct snd_soc_dapm_route adc_intercon[] = {
  1141. { "Left Capture Mux", "IN1L", "IN1L" },
  1142. { "Left Capture Mux", "IN2L", "IN2L" },
  1143. { "Left Capture Mux", "IN3L", "IN3L" },
  1144. { "Left Capture Inverting Mux", "IN1L", "IN1L" },
  1145. { "Left Capture Inverting Mux", "IN2L", "IN2L" },
  1146. { "Left Capture Inverting Mux", "IN3L", "IN3L" },
  1147. { "Right Capture Mux", "IN1R", "IN1R" },
  1148. { "Right Capture Mux", "IN2R", "IN2R" },
  1149. { "Right Capture Mux", "IN3R", "IN3R" },
  1150. { "Right Capture Inverting Mux", "IN1R", "IN1R" },
  1151. { "Right Capture Inverting Mux", "IN2R", "IN2R" },
  1152. { "Right Capture Inverting Mux", "IN3R", "IN3R" },
  1153. { "Left Capture PGA", NULL, "Left Capture Mux" },
  1154. { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
  1155. { "Right Capture PGA", NULL, "Right Capture Mux" },
  1156. { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
  1157. { "AIFOUTL", "Left", "ADCL" },
  1158. { "AIFOUTL", "Right", "ADCR" },
  1159. { "AIFOUTR", "Left", "ADCL" },
  1160. { "AIFOUTR", "Right", "ADCR" },
  1161. { "ADCL", NULL, "CLK_DSP" },
  1162. { "ADCL", NULL, "Left Capture PGA" },
  1163. { "ADCR", NULL, "CLK_DSP" },
  1164. { "ADCR", NULL, "Right Capture PGA" },
  1165. };
  1166. static const struct snd_soc_dapm_route dac_intercon[] = {
  1167. { "DACL", "Right", "AIFINR" },
  1168. { "DACL", "Left", "AIFINL" },
  1169. { "DACL", NULL, "CLK_DSP" },
  1170. { "DACR", "Right", "AIFINR" },
  1171. { "DACR", "Left", "AIFINL" },
  1172. { "DACR", NULL, "CLK_DSP" },
  1173. { "Charge pump", NULL, "SYSCLK" },
  1174. { "Headphone Output", NULL, "HPL PGA" },
  1175. { "Headphone Output", NULL, "HPR PGA" },
  1176. { "Headphone Output", NULL, "Charge pump" },
  1177. { "Headphone Output", NULL, "TOCLK" },
  1178. { "Line Output", NULL, "LINEL PGA" },
  1179. { "Line Output", NULL, "LINER PGA" },
  1180. { "Line Output", NULL, "Charge pump" },
  1181. { "Line Output", NULL, "TOCLK" },
  1182. { "HPOUTL", NULL, "Headphone Output" },
  1183. { "HPOUTR", NULL, "Headphone Output" },
  1184. { "LINEOUTL", NULL, "Line Output" },
  1185. { "LINEOUTR", NULL, "Line Output" },
  1186. };
  1187. static const struct snd_soc_dapm_route wm8904_intercon[] = {
  1188. { "Left Sidetone", "Left", "ADCL" },
  1189. { "Left Sidetone", "Right", "ADCR" },
  1190. { "DACL", NULL, "Left Sidetone" },
  1191. { "Right Sidetone", "Left", "ADCL" },
  1192. { "Right Sidetone", "Right", "ADCR" },
  1193. { "DACR", NULL, "Right Sidetone" },
  1194. { "Left Bypass", NULL, "Class G" },
  1195. { "Left Bypass", NULL, "Left Capture PGA" },
  1196. { "Right Bypass", NULL, "Class G" },
  1197. { "Right Bypass", NULL, "Right Capture PGA" },
  1198. { "HPL Mux", "DAC", "DACL" },
  1199. { "HPL Mux", "Bypass", "Left Bypass" },
  1200. { "HPR Mux", "DAC", "DACR" },
  1201. { "HPR Mux", "Bypass", "Right Bypass" },
  1202. { "LINEL Mux", "DAC", "DACL" },
  1203. { "LINEL Mux", "Bypass", "Left Bypass" },
  1204. { "LINER Mux", "DAC", "DACR" },
  1205. { "LINER Mux", "Bypass", "Right Bypass" },
  1206. { "HPL PGA", NULL, "HPL Mux" },
  1207. { "HPR PGA", NULL, "HPR Mux" },
  1208. { "LINEL PGA", NULL, "LINEL Mux" },
  1209. { "LINER PGA", NULL, "LINER Mux" },
  1210. };
  1211. static const struct snd_soc_dapm_route wm8912_intercon[] = {
  1212. { "HPL PGA", NULL, "DACL" },
  1213. { "HPR PGA", NULL, "DACR" },
  1214. { "LINEL PGA", NULL, "DACL" },
  1215. { "LINER PGA", NULL, "DACR" },
  1216. };
  1217. static int wm8904_add_widgets(struct snd_soc_codec *codec)
  1218. {
  1219. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1220. struct snd_soc_dapm_context *dapm = &codec->dapm;
  1221. snd_soc_dapm_new_controls(dapm, wm8904_core_dapm_widgets,
  1222. ARRAY_SIZE(wm8904_core_dapm_widgets));
  1223. snd_soc_dapm_add_routes(dapm, core_intercon,
  1224. ARRAY_SIZE(core_intercon));
  1225. switch (wm8904->devtype) {
  1226. case WM8904:
  1227. snd_soc_add_controls(codec, wm8904_adc_snd_controls,
  1228. ARRAY_SIZE(wm8904_adc_snd_controls));
  1229. snd_soc_add_controls(codec, wm8904_dac_snd_controls,
  1230. ARRAY_SIZE(wm8904_dac_snd_controls));
  1231. snd_soc_add_controls(codec, wm8904_snd_controls,
  1232. ARRAY_SIZE(wm8904_snd_controls));
  1233. snd_soc_dapm_new_controls(dapm, wm8904_adc_dapm_widgets,
  1234. ARRAY_SIZE(wm8904_adc_dapm_widgets));
  1235. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  1236. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  1237. snd_soc_dapm_new_controls(dapm, wm8904_dapm_widgets,
  1238. ARRAY_SIZE(wm8904_dapm_widgets));
  1239. snd_soc_dapm_add_routes(dapm, core_intercon,
  1240. ARRAY_SIZE(core_intercon));
  1241. snd_soc_dapm_add_routes(dapm, adc_intercon,
  1242. ARRAY_SIZE(adc_intercon));
  1243. snd_soc_dapm_add_routes(dapm, dac_intercon,
  1244. ARRAY_SIZE(dac_intercon));
  1245. snd_soc_dapm_add_routes(dapm, wm8904_intercon,
  1246. ARRAY_SIZE(wm8904_intercon));
  1247. break;
  1248. case WM8912:
  1249. snd_soc_add_controls(codec, wm8904_dac_snd_controls,
  1250. ARRAY_SIZE(wm8904_dac_snd_controls));
  1251. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  1252. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  1253. snd_soc_dapm_add_routes(dapm, dac_intercon,
  1254. ARRAY_SIZE(dac_intercon));
  1255. snd_soc_dapm_add_routes(dapm, wm8912_intercon,
  1256. ARRAY_SIZE(wm8912_intercon));
  1257. break;
  1258. }
  1259. snd_soc_dapm_new_widgets(dapm);
  1260. return 0;
  1261. }
  1262. static struct {
  1263. int ratio;
  1264. unsigned int clk_sys_rate;
  1265. } clk_sys_rates[] = {
  1266. { 64, 0 },
  1267. { 128, 1 },
  1268. { 192, 2 },
  1269. { 256, 3 },
  1270. { 384, 4 },
  1271. { 512, 5 },
  1272. { 786, 6 },
  1273. { 1024, 7 },
  1274. { 1408, 8 },
  1275. { 1536, 9 },
  1276. };
  1277. static struct {
  1278. int rate;
  1279. int sample_rate;
  1280. } sample_rates[] = {
  1281. { 8000, 0 },
  1282. { 11025, 1 },
  1283. { 12000, 1 },
  1284. { 16000, 2 },
  1285. { 22050, 3 },
  1286. { 24000, 3 },
  1287. { 32000, 4 },
  1288. { 44100, 5 },
  1289. { 48000, 5 },
  1290. };
  1291. static struct {
  1292. int div; /* *10 due to .5s */
  1293. int bclk_div;
  1294. } bclk_divs[] = {
  1295. { 10, 0 },
  1296. { 15, 1 },
  1297. { 20, 2 },
  1298. { 30, 3 },
  1299. { 40, 4 },
  1300. { 50, 5 },
  1301. { 55, 6 },
  1302. { 60, 7 },
  1303. { 80, 8 },
  1304. { 100, 9 },
  1305. { 110, 10 },
  1306. { 120, 11 },
  1307. { 160, 12 },
  1308. { 200, 13 },
  1309. { 220, 14 },
  1310. { 240, 16 },
  1311. { 200, 17 },
  1312. { 320, 18 },
  1313. { 440, 19 },
  1314. { 480, 20 },
  1315. };
  1316. static int wm8904_hw_params(struct snd_pcm_substream *substream,
  1317. struct snd_pcm_hw_params *params,
  1318. struct snd_soc_dai *dai)
  1319. {
  1320. struct snd_soc_codec *codec = dai->codec;
  1321. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1322. int ret, i, best, best_val, cur_val;
  1323. unsigned int aif1 = 0;
  1324. unsigned int aif2 = 0;
  1325. unsigned int aif3 = 0;
  1326. unsigned int clock1 = 0;
  1327. unsigned int dac_digital1 = 0;
  1328. /* What BCLK do we need? */
  1329. wm8904->fs = params_rate(params);
  1330. if (wm8904->tdm_slots) {
  1331. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1332. wm8904->tdm_slots, wm8904->tdm_width);
  1333. wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
  1334. wm8904->tdm_width, 2,
  1335. wm8904->tdm_slots);
  1336. } else {
  1337. wm8904->bclk = snd_soc_params_to_bclk(params);
  1338. }
  1339. switch (params_format(params)) {
  1340. case SNDRV_PCM_FORMAT_S16_LE:
  1341. break;
  1342. case SNDRV_PCM_FORMAT_S20_3LE:
  1343. aif1 |= 0x40;
  1344. break;
  1345. case SNDRV_PCM_FORMAT_S24_LE:
  1346. aif1 |= 0x80;
  1347. break;
  1348. case SNDRV_PCM_FORMAT_S32_LE:
  1349. aif1 |= 0xc0;
  1350. break;
  1351. default:
  1352. return -EINVAL;
  1353. }
  1354. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
  1355. ret = wm8904_configure_clocking(codec);
  1356. if (ret != 0)
  1357. return ret;
  1358. /* Select nearest CLK_SYS_RATE */
  1359. best = 0;
  1360. best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
  1361. - wm8904->fs);
  1362. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1363. cur_val = abs((wm8904->sysclk_rate /
  1364. clk_sys_rates[i].ratio) - wm8904->fs);
  1365. if (cur_val < best_val) {
  1366. best = i;
  1367. best_val = cur_val;
  1368. }
  1369. }
  1370. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1371. clk_sys_rates[best].ratio);
  1372. clock1 |= (clk_sys_rates[best].clk_sys_rate
  1373. << WM8904_CLK_SYS_RATE_SHIFT);
  1374. /* SAMPLE_RATE */
  1375. best = 0;
  1376. best_val = abs(wm8904->fs - sample_rates[0].rate);
  1377. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1378. /* Closest match */
  1379. cur_val = abs(wm8904->fs - sample_rates[i].rate);
  1380. if (cur_val < best_val) {
  1381. best = i;
  1382. best_val = cur_val;
  1383. }
  1384. }
  1385. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1386. sample_rates[best].rate);
  1387. clock1 |= (sample_rates[best].sample_rate
  1388. << WM8904_SAMPLE_RATE_SHIFT);
  1389. /* Enable sloping stopband filter for low sample rates */
  1390. if (wm8904->fs <= 24000)
  1391. dac_digital1 |= WM8904_DAC_SB_FILT;
  1392. /* BCLK_DIV */
  1393. best = 0;
  1394. best_val = INT_MAX;
  1395. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1396. cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
  1397. - wm8904->bclk;
  1398. if (cur_val < 0) /* Table is sorted */
  1399. break;
  1400. if (cur_val < best_val) {
  1401. best = i;
  1402. best_val = cur_val;
  1403. }
  1404. }
  1405. wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
  1406. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1407. bclk_divs[best].div, wm8904->bclk);
  1408. aif2 |= bclk_divs[best].bclk_div;
  1409. /* LRCLK is a simple fraction of BCLK */
  1410. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
  1411. aif3 |= wm8904->bclk / wm8904->fs;
  1412. /* Apply the settings */
  1413. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  1414. WM8904_DAC_SB_FILT, dac_digital1);
  1415. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1416. WM8904_AIF_WL_MASK, aif1);
  1417. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
  1418. WM8904_BCLK_DIV_MASK, aif2);
  1419. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1420. WM8904_LRCLK_RATE_MASK, aif3);
  1421. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
  1422. WM8904_SAMPLE_RATE_MASK |
  1423. WM8904_CLK_SYS_RATE_MASK, clock1);
  1424. /* Update filters for the new settings */
  1425. wm8904_set_retune_mobile(codec);
  1426. wm8904_set_deemph(codec);
  1427. return 0;
  1428. }
  1429. static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1430. unsigned int freq, int dir)
  1431. {
  1432. struct snd_soc_codec *codec = dai->codec;
  1433. struct wm8904_priv *priv = snd_soc_codec_get_drvdata(codec);
  1434. switch (clk_id) {
  1435. case WM8904_CLK_MCLK:
  1436. priv->sysclk_src = clk_id;
  1437. priv->mclk_rate = freq;
  1438. break;
  1439. case WM8904_CLK_FLL:
  1440. priv->sysclk_src = clk_id;
  1441. break;
  1442. default:
  1443. return -EINVAL;
  1444. }
  1445. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  1446. wm8904_configure_clocking(codec);
  1447. return 0;
  1448. }
  1449. static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1450. {
  1451. struct snd_soc_codec *codec = dai->codec;
  1452. unsigned int aif1 = 0;
  1453. unsigned int aif3 = 0;
  1454. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1455. case SND_SOC_DAIFMT_CBS_CFS:
  1456. break;
  1457. case SND_SOC_DAIFMT_CBS_CFM:
  1458. aif3 |= WM8904_LRCLK_DIR;
  1459. break;
  1460. case SND_SOC_DAIFMT_CBM_CFS:
  1461. aif1 |= WM8904_BCLK_DIR;
  1462. break;
  1463. case SND_SOC_DAIFMT_CBM_CFM:
  1464. aif1 |= WM8904_BCLK_DIR;
  1465. aif3 |= WM8904_LRCLK_DIR;
  1466. break;
  1467. default:
  1468. return -EINVAL;
  1469. }
  1470. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1471. case SND_SOC_DAIFMT_DSP_B:
  1472. aif1 |= WM8904_AIF_LRCLK_INV;
  1473. case SND_SOC_DAIFMT_DSP_A:
  1474. aif1 |= 0x3;
  1475. break;
  1476. case SND_SOC_DAIFMT_I2S:
  1477. aif1 |= 0x2;
  1478. break;
  1479. case SND_SOC_DAIFMT_RIGHT_J:
  1480. break;
  1481. case SND_SOC_DAIFMT_LEFT_J:
  1482. aif1 |= 0x1;
  1483. break;
  1484. default:
  1485. return -EINVAL;
  1486. }
  1487. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1488. case SND_SOC_DAIFMT_DSP_A:
  1489. case SND_SOC_DAIFMT_DSP_B:
  1490. /* frame inversion not valid for DSP modes */
  1491. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1492. case SND_SOC_DAIFMT_NB_NF:
  1493. break;
  1494. case SND_SOC_DAIFMT_IB_NF:
  1495. aif1 |= WM8904_AIF_BCLK_INV;
  1496. break;
  1497. default:
  1498. return -EINVAL;
  1499. }
  1500. break;
  1501. case SND_SOC_DAIFMT_I2S:
  1502. case SND_SOC_DAIFMT_RIGHT_J:
  1503. case SND_SOC_DAIFMT_LEFT_J:
  1504. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1505. case SND_SOC_DAIFMT_NB_NF:
  1506. break;
  1507. case SND_SOC_DAIFMT_IB_IF:
  1508. aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
  1509. break;
  1510. case SND_SOC_DAIFMT_IB_NF:
  1511. aif1 |= WM8904_AIF_BCLK_INV;
  1512. break;
  1513. case SND_SOC_DAIFMT_NB_IF:
  1514. aif1 |= WM8904_AIF_LRCLK_INV;
  1515. break;
  1516. default:
  1517. return -EINVAL;
  1518. }
  1519. break;
  1520. default:
  1521. return -EINVAL;
  1522. }
  1523. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1524. WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
  1525. WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
  1526. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1527. WM8904_LRCLK_DIR, aif3);
  1528. return 0;
  1529. }
  1530. static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1531. unsigned int rx_mask, int slots, int slot_width)
  1532. {
  1533. struct snd_soc_codec *codec = dai->codec;
  1534. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1535. int aif1 = 0;
  1536. /* Don't need to validate anything if we're turning off TDM */
  1537. if (slots == 0)
  1538. goto out;
  1539. /* Note that we allow configurations we can't handle ourselves -
  1540. * for example, we can generate clocks for slots 2 and up even if
  1541. * we can't use those slots ourselves.
  1542. */
  1543. aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
  1544. switch (rx_mask) {
  1545. case 3:
  1546. break;
  1547. case 0xc:
  1548. aif1 |= WM8904_AIFADC_TDM_CHAN;
  1549. break;
  1550. default:
  1551. return -EINVAL;
  1552. }
  1553. switch (tx_mask) {
  1554. case 3:
  1555. break;
  1556. case 0xc:
  1557. aif1 |= WM8904_AIFDAC_TDM_CHAN;
  1558. break;
  1559. default:
  1560. return -EINVAL;
  1561. }
  1562. out:
  1563. wm8904->tdm_width = slot_width;
  1564. wm8904->tdm_slots = slots / 2;
  1565. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1566. WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
  1567. WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
  1568. return 0;
  1569. }
  1570. struct _fll_div {
  1571. u16 fll_fratio;
  1572. u16 fll_outdiv;
  1573. u16 fll_clk_ref_div;
  1574. u16 n;
  1575. u16 k;
  1576. };
  1577. /* The size in bits of the FLL divide multiplied by 10
  1578. * to allow rounding later */
  1579. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1580. static struct {
  1581. unsigned int min;
  1582. unsigned int max;
  1583. u16 fll_fratio;
  1584. int ratio;
  1585. } fll_fratios[] = {
  1586. { 0, 64000, 4, 16 },
  1587. { 64000, 128000, 3, 8 },
  1588. { 128000, 256000, 2, 4 },
  1589. { 256000, 1000000, 1, 2 },
  1590. { 1000000, 13500000, 0, 1 },
  1591. };
  1592. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1593. unsigned int Fout)
  1594. {
  1595. u64 Kpart;
  1596. unsigned int K, Ndiv, Nmod, target;
  1597. unsigned int div;
  1598. int i;
  1599. /* Fref must be <=13.5MHz */
  1600. div = 1;
  1601. fll_div->fll_clk_ref_div = 0;
  1602. while ((Fref / div) > 13500000) {
  1603. div *= 2;
  1604. fll_div->fll_clk_ref_div++;
  1605. if (div > 8) {
  1606. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1607. Fref);
  1608. return -EINVAL;
  1609. }
  1610. }
  1611. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  1612. /* Apply the division for our remaining calculations */
  1613. Fref /= div;
  1614. /* Fvco should be 90-100MHz; don't check the upper bound */
  1615. div = 4;
  1616. while (Fout * div < 90000000) {
  1617. div++;
  1618. if (div > 64) {
  1619. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1620. Fout);
  1621. return -EINVAL;
  1622. }
  1623. }
  1624. target = Fout * div;
  1625. fll_div->fll_outdiv = div - 1;
  1626. pr_debug("Fvco=%dHz\n", target);
  1627. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  1628. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1629. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1630. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1631. target /= fll_fratios[i].ratio;
  1632. break;
  1633. }
  1634. }
  1635. if (i == ARRAY_SIZE(fll_fratios)) {
  1636. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1637. return -EINVAL;
  1638. }
  1639. /* Now, calculate N.K */
  1640. Ndiv = target / Fref;
  1641. fll_div->n = Ndiv;
  1642. Nmod = target % Fref;
  1643. pr_debug("Nmod=%d\n", Nmod);
  1644. /* Calculate fractional part - scale up so we can round. */
  1645. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1646. do_div(Kpart, Fref);
  1647. K = Kpart & 0xFFFFFFFF;
  1648. if ((K % 10) >= 5)
  1649. K += 5;
  1650. /* Move down to proper range now rounding is done */
  1651. fll_div->k = K / 10;
  1652. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  1653. fll_div->n, fll_div->k,
  1654. fll_div->fll_fratio, fll_div->fll_outdiv,
  1655. fll_div->fll_clk_ref_div);
  1656. return 0;
  1657. }
  1658. static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  1659. unsigned int Fref, unsigned int Fout)
  1660. {
  1661. struct snd_soc_codec *codec = dai->codec;
  1662. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1663. struct _fll_div fll_div;
  1664. int ret, val;
  1665. int clock2, fll1;
  1666. /* Any change? */
  1667. if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
  1668. Fout == wm8904->fll_fout)
  1669. return 0;
  1670. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  1671. if (Fout == 0) {
  1672. dev_dbg(codec->dev, "FLL disabled\n");
  1673. wm8904->fll_fref = 0;
  1674. wm8904->fll_fout = 0;
  1675. /* Gate SYSCLK to avoid glitches */
  1676. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1677. WM8904_CLK_SYS_ENA, 0);
  1678. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1679. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1680. goto out;
  1681. }
  1682. /* Validate the FLL ID */
  1683. switch (source) {
  1684. case WM8904_FLL_MCLK:
  1685. case WM8904_FLL_LRCLK:
  1686. case WM8904_FLL_BCLK:
  1687. ret = fll_factors(&fll_div, Fref, Fout);
  1688. if (ret != 0)
  1689. return ret;
  1690. break;
  1691. case WM8904_FLL_FREE_RUNNING:
  1692. dev_dbg(codec->dev, "Using free running FLL\n");
  1693. /* Force 12MHz and output/4 for now */
  1694. Fout = 12000000;
  1695. Fref = 12000000;
  1696. memset(&fll_div, 0, sizeof(fll_div));
  1697. fll_div.fll_outdiv = 3;
  1698. break;
  1699. default:
  1700. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  1701. return -EINVAL;
  1702. }
  1703. /* Save current state then disable the FLL and SYSCLK to avoid
  1704. * misclocking */
  1705. fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
  1706. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1707. WM8904_CLK_SYS_ENA, 0);
  1708. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1709. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1710. /* Unlock forced oscilator control to switch it on/off */
  1711. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1712. WM8904_USER_KEY, WM8904_USER_KEY);
  1713. if (fll_id == WM8904_FLL_FREE_RUNNING) {
  1714. val = WM8904_FLL_FRC_NCO;
  1715. } else {
  1716. val = 0;
  1717. }
  1718. snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
  1719. val);
  1720. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1721. WM8904_USER_KEY, 0);
  1722. switch (fll_id) {
  1723. case WM8904_FLL_MCLK:
  1724. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1725. WM8904_FLL_CLK_REF_SRC_MASK, 0);
  1726. break;
  1727. case WM8904_FLL_LRCLK:
  1728. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1729. WM8904_FLL_CLK_REF_SRC_MASK, 1);
  1730. break;
  1731. case WM8904_FLL_BCLK:
  1732. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1733. WM8904_FLL_CLK_REF_SRC_MASK, 2);
  1734. break;
  1735. }
  1736. if (fll_div.k)
  1737. val = WM8904_FLL_FRACN_ENA;
  1738. else
  1739. val = 0;
  1740. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1741. WM8904_FLL_FRACN_ENA, val);
  1742. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
  1743. WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
  1744. (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
  1745. (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
  1746. snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
  1747. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
  1748. fll_div.n << WM8904_FLL_N_SHIFT);
  1749. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1750. WM8904_FLL_CLK_REF_DIV_MASK,
  1751. fll_div.fll_clk_ref_div
  1752. << WM8904_FLL_CLK_REF_DIV_SHIFT);
  1753. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  1754. wm8904->fll_fref = Fref;
  1755. wm8904->fll_fout = Fout;
  1756. wm8904->fll_src = source;
  1757. /* Enable the FLL if it was previously active */
  1758. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1759. WM8904_FLL_OSC_ENA, fll1);
  1760. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1761. WM8904_FLL_ENA, fll1);
  1762. out:
  1763. /* Reenable SYSCLK if it was previously active */
  1764. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1765. WM8904_CLK_SYS_ENA, clock2);
  1766. return 0;
  1767. }
  1768. static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1769. {
  1770. struct snd_soc_codec *codec = codec_dai->codec;
  1771. int val;
  1772. if (mute)
  1773. val = WM8904_DAC_MUTE;
  1774. else
  1775. val = 0;
  1776. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
  1777. return 0;
  1778. }
  1779. static void wm8904_sync_cache(struct snd_soc_codec *codec)
  1780. {
  1781. u16 *reg_cache = codec->reg_cache;
  1782. int i;
  1783. if (!codec->cache_sync)
  1784. return;
  1785. codec->cache_only = 0;
  1786. /* Sync back cached values if they're different from the
  1787. * hardware default.
  1788. */
  1789. for (i = 1; i < codec->driver->reg_cache_size; i++) {
  1790. if (!wm8904_access[i].writable)
  1791. continue;
  1792. if (reg_cache[i] == wm8904_reg[i])
  1793. continue;
  1794. snd_soc_write(codec, i, reg_cache[i]);
  1795. }
  1796. codec->cache_sync = 0;
  1797. }
  1798. static int wm8904_set_bias_level(struct snd_soc_codec *codec,
  1799. enum snd_soc_bias_level level)
  1800. {
  1801. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1802. int ret;
  1803. switch (level) {
  1804. case SND_SOC_BIAS_ON:
  1805. break;
  1806. case SND_SOC_BIAS_PREPARE:
  1807. /* VMID resistance 2*50k */
  1808. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1809. WM8904_VMID_RES_MASK,
  1810. 0x1 << WM8904_VMID_RES_SHIFT);
  1811. /* Normal bias current */
  1812. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1813. WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
  1814. break;
  1815. case SND_SOC_BIAS_STANDBY:
  1816. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1817. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1818. wm8904->supplies);
  1819. if (ret != 0) {
  1820. dev_err(codec->dev,
  1821. "Failed to enable supplies: %d\n",
  1822. ret);
  1823. return ret;
  1824. }
  1825. wm8904_sync_cache(codec);
  1826. /* Enable bias */
  1827. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1828. WM8904_BIAS_ENA, WM8904_BIAS_ENA);
  1829. /* Enable VMID, VMID buffering, 2*5k resistance */
  1830. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1831. WM8904_VMID_ENA |
  1832. WM8904_VMID_RES_MASK,
  1833. WM8904_VMID_ENA |
  1834. 0x3 << WM8904_VMID_RES_SHIFT);
  1835. /* Let VMID ramp */
  1836. msleep(1);
  1837. }
  1838. /* Maintain VMID with 2*250k */
  1839. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1840. WM8904_VMID_RES_MASK,
  1841. 0x2 << WM8904_VMID_RES_SHIFT);
  1842. /* Bias current *0.5 */
  1843. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1844. WM8904_ISEL_MASK, 0);
  1845. break;
  1846. case SND_SOC_BIAS_OFF:
  1847. /* Turn off VMID */
  1848. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1849. WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
  1850. /* Stop bias generation */
  1851. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1852. WM8904_BIAS_ENA, 0);
  1853. #ifdef CONFIG_REGULATOR
  1854. /* Post 2.6.34 we will be able to get a callback when
  1855. * the regulators are disabled which we can use but
  1856. * for now just assume that the power will be cut if
  1857. * the regulator API is in use.
  1858. */
  1859. codec->cache_sync = 1;
  1860. #endif
  1861. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
  1862. wm8904->supplies);
  1863. break;
  1864. }
  1865. codec->dapm.bias_level = level;
  1866. return 0;
  1867. }
  1868. #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
  1869. #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1870. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1871. static struct snd_soc_dai_ops wm8904_dai_ops = {
  1872. .set_sysclk = wm8904_set_sysclk,
  1873. .set_fmt = wm8904_set_fmt,
  1874. .set_tdm_slot = wm8904_set_tdm_slot,
  1875. .set_pll = wm8904_set_fll,
  1876. .hw_params = wm8904_hw_params,
  1877. .digital_mute = wm8904_digital_mute,
  1878. };
  1879. static struct snd_soc_dai_driver wm8904_dai = {
  1880. .name = "wm8904-hifi",
  1881. .playback = {
  1882. .stream_name = "Playback",
  1883. .channels_min = 2,
  1884. .channels_max = 2,
  1885. .rates = WM8904_RATES,
  1886. .formats = WM8904_FORMATS,
  1887. },
  1888. .capture = {
  1889. .stream_name = "Capture",
  1890. .channels_min = 2,
  1891. .channels_max = 2,
  1892. .rates = WM8904_RATES,
  1893. .formats = WM8904_FORMATS,
  1894. },
  1895. .ops = &wm8904_dai_ops,
  1896. .symmetric_rates = 1,
  1897. };
  1898. #ifdef CONFIG_PM
  1899. static int wm8904_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1900. {
  1901. wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1902. return 0;
  1903. }
  1904. static int wm8904_resume(struct snd_soc_codec *codec)
  1905. {
  1906. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1907. return 0;
  1908. }
  1909. #else
  1910. #define wm8904_suspend NULL
  1911. #define wm8904_resume NULL
  1912. #endif
  1913. static void wm8904_handle_retune_mobile_pdata(struct snd_soc_codec *codec)
  1914. {
  1915. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1916. struct wm8904_pdata *pdata = wm8904->pdata;
  1917. struct snd_kcontrol_new control =
  1918. SOC_ENUM_EXT("EQ Mode",
  1919. wm8904->retune_mobile_enum,
  1920. wm8904_get_retune_mobile_enum,
  1921. wm8904_put_retune_mobile_enum);
  1922. int ret, i, j;
  1923. const char **t;
  1924. /* We need an array of texts for the enum API but the number
  1925. * of texts is likely to be less than the number of
  1926. * configurations due to the sample rate dependency of the
  1927. * configurations. */
  1928. wm8904->num_retune_mobile_texts = 0;
  1929. wm8904->retune_mobile_texts = NULL;
  1930. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  1931. for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
  1932. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  1933. wm8904->retune_mobile_texts[j]) == 0)
  1934. break;
  1935. }
  1936. if (j != wm8904->num_retune_mobile_texts)
  1937. continue;
  1938. /* Expand the array... */
  1939. t = krealloc(wm8904->retune_mobile_texts,
  1940. sizeof(char *) *
  1941. (wm8904->num_retune_mobile_texts + 1),
  1942. GFP_KERNEL);
  1943. if (t == NULL)
  1944. continue;
  1945. /* ...store the new entry... */
  1946. t[wm8904->num_retune_mobile_texts] =
  1947. pdata->retune_mobile_cfgs[i].name;
  1948. /* ...and remember the new version. */
  1949. wm8904->num_retune_mobile_texts++;
  1950. wm8904->retune_mobile_texts = t;
  1951. }
  1952. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  1953. wm8904->num_retune_mobile_texts);
  1954. wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
  1955. wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
  1956. ret = snd_soc_add_controls(codec, &control, 1);
  1957. if (ret != 0)
  1958. dev_err(codec->dev,
  1959. "Failed to add ReTune Mobile control: %d\n", ret);
  1960. }
  1961. static void wm8904_handle_pdata(struct snd_soc_codec *codec)
  1962. {
  1963. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1964. struct wm8904_pdata *pdata = wm8904->pdata;
  1965. int ret, i;
  1966. if (!pdata) {
  1967. snd_soc_add_controls(codec, wm8904_eq_controls,
  1968. ARRAY_SIZE(wm8904_eq_controls));
  1969. return;
  1970. }
  1971. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  1972. if (pdata->num_drc_cfgs) {
  1973. struct snd_kcontrol_new control =
  1974. SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
  1975. wm8904_get_drc_enum, wm8904_put_drc_enum);
  1976. /* We need an array of texts for the enum API */
  1977. wm8904->drc_texts = kmalloc(sizeof(char *)
  1978. * pdata->num_drc_cfgs, GFP_KERNEL);
  1979. if (!wm8904->drc_texts) {
  1980. dev_err(codec->dev,
  1981. "Failed to allocate %d DRC config texts\n",
  1982. pdata->num_drc_cfgs);
  1983. return;
  1984. }
  1985. for (i = 0; i < pdata->num_drc_cfgs; i++)
  1986. wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
  1987. wm8904->drc_enum.max = pdata->num_drc_cfgs;
  1988. wm8904->drc_enum.texts = wm8904->drc_texts;
  1989. ret = snd_soc_add_controls(codec, &control, 1);
  1990. if (ret != 0)
  1991. dev_err(codec->dev,
  1992. "Failed to add DRC mode control: %d\n", ret);
  1993. wm8904_set_drc(codec);
  1994. }
  1995. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  1996. pdata->num_retune_mobile_cfgs);
  1997. if (pdata->num_retune_mobile_cfgs)
  1998. wm8904_handle_retune_mobile_pdata(codec);
  1999. else
  2000. snd_soc_add_controls(codec, wm8904_eq_controls,
  2001. ARRAY_SIZE(wm8904_eq_controls));
  2002. }
  2003. static int wm8904_probe(struct snd_soc_codec *codec)
  2004. {
  2005. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  2006. struct wm8904_pdata *pdata = wm8904->pdata;
  2007. u16 *reg_cache = codec->reg_cache;
  2008. int ret, i;
  2009. codec->cache_sync = 1;
  2010. codec->dapm.idle_bias_off = 1;
  2011. switch (wm8904->devtype) {
  2012. case WM8904:
  2013. break;
  2014. case WM8912:
  2015. memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
  2016. break;
  2017. default:
  2018. dev_err(codec->dev, "Unknown device type %d\n",
  2019. wm8904->devtype);
  2020. return -EINVAL;
  2021. }
  2022. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  2023. if (ret != 0) {
  2024. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  2025. return ret;
  2026. }
  2027. for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
  2028. wm8904->supplies[i].supply = wm8904_supply_names[i];
  2029. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8904->supplies),
  2030. wm8904->supplies);
  2031. if (ret != 0) {
  2032. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  2033. return ret;
  2034. }
  2035. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  2036. wm8904->supplies);
  2037. if (ret != 0) {
  2038. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  2039. goto err_get;
  2040. }
  2041. ret = snd_soc_read(codec, WM8904_SW_RESET_AND_ID);
  2042. if (ret < 0) {
  2043. dev_err(codec->dev, "Failed to read ID register\n");
  2044. goto err_enable;
  2045. }
  2046. if (ret != wm8904_reg[WM8904_SW_RESET_AND_ID]) {
  2047. dev_err(codec->dev, "Device is not a WM8904, ID is %x\n", ret);
  2048. ret = -EINVAL;
  2049. goto err_enable;
  2050. }
  2051. ret = snd_soc_read(codec, WM8904_REVISION);
  2052. if (ret < 0) {
  2053. dev_err(codec->dev, "Failed to read device revision: %d\n",
  2054. ret);
  2055. goto err_enable;
  2056. }
  2057. dev_info(codec->dev, "revision %c\n", ret + 'A');
  2058. ret = wm8904_reset(codec);
  2059. if (ret < 0) {
  2060. dev_err(codec->dev, "Failed to issue reset\n");
  2061. goto err_enable;
  2062. }
  2063. /* Change some default settings - latch VU and enable ZC */
  2064. snd_soc_update_bits(codec, WM8904_ADC_DIGITAL_VOLUME_LEFT,
  2065. WM8904_ADC_VU, WM8904_ADC_VU);
  2066. snd_soc_update_bits(codec, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
  2067. WM8904_ADC_VU, WM8904_ADC_VU);
  2068. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_VOLUME_LEFT,
  2069. WM8904_DAC_VU, WM8904_DAC_VU);
  2070. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
  2071. WM8904_DAC_VU, WM8904_DAC_VU);
  2072. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT1_LEFT,
  2073. WM8904_HPOUT_VU | WM8904_HPOUTLZC,
  2074. WM8904_HPOUT_VU | WM8904_HPOUTLZC);
  2075. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT1_RIGHT,
  2076. WM8904_HPOUT_VU | WM8904_HPOUTRZC,
  2077. WM8904_HPOUT_VU | WM8904_HPOUTRZC);
  2078. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT2_LEFT,
  2079. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
  2080. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
  2081. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT2_RIGHT,
  2082. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
  2083. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
  2084. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0,
  2085. WM8904_SR_MODE, 0);
  2086. /* Apply configuration from the platform data. */
  2087. if (wm8904->pdata) {
  2088. for (i = 0; i < WM8904_GPIO_REGS; i++) {
  2089. if (!pdata->gpio_cfg[i])
  2090. continue;
  2091. reg_cache[WM8904_GPIO_CONTROL_1 + i]
  2092. = pdata->gpio_cfg[i] & 0xffff;
  2093. }
  2094. /* Zero is the default value for these anyway */
  2095. for (i = 0; i < WM8904_MIC_REGS; i++)
  2096. reg_cache[WM8904_MIC_BIAS_CONTROL_0 + i]
  2097. = pdata->mic_cfg[i];
  2098. }
  2099. /* Set Class W by default - this will be managed by the Class
  2100. * G widget at runtime where bypass paths are available.
  2101. */
  2102. snd_soc_update_bits(codec, WM8904_CLASS_W_0,
  2103. WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
  2104. /* Use normal bias source */
  2105. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  2106. WM8904_POBCTRL, 0);
  2107. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  2108. /* Bias level configuration will have done an extra enable */
  2109. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2110. wm8904_handle_pdata(codec);
  2111. wm8904_add_widgets(codec);
  2112. return 0;
  2113. err_enable:
  2114. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2115. err_get:
  2116. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2117. return ret;
  2118. }
  2119. static int wm8904_remove(struct snd_soc_codec *codec)
  2120. {
  2121. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  2122. wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
  2123. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2124. kfree(wm8904->retune_mobile_texts);
  2125. kfree(wm8904->drc_texts);
  2126. return 0;
  2127. }
  2128. static struct snd_soc_codec_driver soc_codec_dev_wm8904 = {
  2129. .probe = wm8904_probe,
  2130. .remove = wm8904_remove,
  2131. .suspend = wm8904_suspend,
  2132. .resume = wm8904_resume,
  2133. .set_bias_level = wm8904_set_bias_level,
  2134. .reg_cache_size = ARRAY_SIZE(wm8904_reg),
  2135. .reg_word_size = sizeof(u16),
  2136. .reg_cache_default = wm8904_reg,
  2137. .volatile_register = wm8904_volatile_register,
  2138. };
  2139. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2140. static __devinit int wm8904_i2c_probe(struct i2c_client *i2c,
  2141. const struct i2c_device_id *id)
  2142. {
  2143. struct wm8904_priv *wm8904;
  2144. int ret;
  2145. wm8904 = kzalloc(sizeof(struct wm8904_priv), GFP_KERNEL);
  2146. if (wm8904 == NULL)
  2147. return -ENOMEM;
  2148. wm8904->devtype = id->driver_data;
  2149. i2c_set_clientdata(i2c, wm8904);
  2150. wm8904->pdata = i2c->dev.platform_data;
  2151. ret = snd_soc_register_codec(&i2c->dev,
  2152. &soc_codec_dev_wm8904, &wm8904_dai, 1);
  2153. if (ret < 0)
  2154. kfree(wm8904);
  2155. return ret;
  2156. }
  2157. static __devexit int wm8904_i2c_remove(struct i2c_client *client)
  2158. {
  2159. snd_soc_unregister_codec(&client->dev);
  2160. kfree(i2c_get_clientdata(client));
  2161. return 0;
  2162. }
  2163. static const struct i2c_device_id wm8904_i2c_id[] = {
  2164. { "wm8904", WM8904 },
  2165. { "wm8912", WM8912 },
  2166. { "wm8918", WM8904 }, /* Actually a subset, updates to follow */
  2167. { }
  2168. };
  2169. MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
  2170. static struct i2c_driver wm8904_i2c_driver = {
  2171. .driver = {
  2172. .name = "wm8904-codec",
  2173. .owner = THIS_MODULE,
  2174. },
  2175. .probe = wm8904_i2c_probe,
  2176. .remove = __devexit_p(wm8904_i2c_remove),
  2177. .id_table = wm8904_i2c_id,
  2178. };
  2179. #endif
  2180. static int __init wm8904_modinit(void)
  2181. {
  2182. int ret = 0;
  2183. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2184. ret = i2c_add_driver(&wm8904_i2c_driver);
  2185. if (ret != 0) {
  2186. printk(KERN_ERR "Failed to register wm8904 I2C driver: %d\n",
  2187. ret);
  2188. }
  2189. #endif
  2190. return ret;
  2191. }
  2192. module_init(wm8904_modinit);
  2193. static void __exit wm8904_exit(void)
  2194. {
  2195. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2196. i2c_del_driver(&wm8904_i2c_driver);
  2197. #endif
  2198. }
  2199. module_exit(wm8904_exit);
  2200. MODULE_DESCRIPTION("ASoC WM8904 driver");
  2201. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  2202. MODULE_LICENSE("GPL");