wm8904.c 62 KB

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