wm8904.c 62 KB

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