wm8904.c 72 KB

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