wm8904.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  1. /*
  2. * wm8904.c -- WM8904 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/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. { 19, 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_reset(struct snd_soc_codec *codec)
  298. {
  299. return snd_soc_write(codec, WM8904_SW_RESET_AND_ID, 0);
  300. }
  301. static int wm8904_configure_clocking(struct snd_soc_codec *codec)
  302. {
  303. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  304. unsigned int clock0, clock2, rate;
  305. /* Gate the clock while we're updating to avoid misclocking */
  306. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  307. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  308. WM8904_SYSCLK_SRC, 0);
  309. /* This should be done on init() for bypass paths */
  310. switch (wm8904->sysclk_src) {
  311. case WM8904_CLK_MCLK:
  312. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
  313. clock2 &= ~WM8904_SYSCLK_SRC;
  314. rate = wm8904->mclk_rate;
  315. /* Ensure the FLL is stopped */
  316. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  317. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  318. break;
  319. case WM8904_CLK_FLL:
  320. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  321. wm8904->fll_fout);
  322. clock2 |= WM8904_SYSCLK_SRC;
  323. rate = wm8904->fll_fout;
  324. break;
  325. default:
  326. dev_err(codec->dev, "System clock not configured\n");
  327. return -EINVAL;
  328. }
  329. /* SYSCLK shouldn't be over 13.5MHz */
  330. if (rate > 13500000) {
  331. clock0 = WM8904_MCLK_DIV;
  332. wm8904->sysclk_rate = rate / 2;
  333. } else {
  334. clock0 = 0;
  335. wm8904->sysclk_rate = rate;
  336. }
  337. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
  338. clock0);
  339. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  340. WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
  341. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
  342. return 0;
  343. }
  344. static void wm8904_set_drc(struct snd_soc_codec *codec)
  345. {
  346. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  347. struct wm8904_pdata *pdata = wm8904->pdata;
  348. int save, i;
  349. /* Save any enables; the configuration should clear them. */
  350. save = snd_soc_read(codec, WM8904_DRC_0);
  351. for (i = 0; i < WM8904_DRC_REGS; i++)
  352. snd_soc_update_bits(codec, WM8904_DRC_0 + i, 0xffff,
  353. pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
  354. /* Reenable the DRC */
  355. snd_soc_update_bits(codec, WM8904_DRC_0,
  356. WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
  357. }
  358. static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
  359. struct snd_ctl_elem_value *ucontrol)
  360. {
  361. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  362. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  363. struct wm8904_pdata *pdata = wm8904->pdata;
  364. int value = ucontrol->value.integer.value[0];
  365. if (value >= pdata->num_drc_cfgs)
  366. return -EINVAL;
  367. wm8904->drc_cfg = value;
  368. wm8904_set_drc(codec);
  369. return 0;
  370. }
  371. static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
  372. struct snd_ctl_elem_value *ucontrol)
  373. {
  374. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  375. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  376. ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
  377. return 0;
  378. }
  379. static void wm8904_set_retune_mobile(struct snd_soc_codec *codec)
  380. {
  381. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  382. struct wm8904_pdata *pdata = wm8904->pdata;
  383. int best, best_val, save, i, cfg;
  384. if (!pdata || !wm8904->num_retune_mobile_texts)
  385. return;
  386. /* Find the version of the currently selected configuration
  387. * with the nearest sample rate. */
  388. cfg = wm8904->retune_mobile_cfg;
  389. best = 0;
  390. best_val = INT_MAX;
  391. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  392. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  393. wm8904->retune_mobile_texts[cfg]) == 0 &&
  394. abs(pdata->retune_mobile_cfgs[i].rate
  395. - wm8904->fs) < best_val) {
  396. best = i;
  397. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  398. - wm8904->fs);
  399. }
  400. }
  401. dev_dbg(codec->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
  402. pdata->retune_mobile_cfgs[best].name,
  403. pdata->retune_mobile_cfgs[best].rate,
  404. wm8904->fs);
  405. /* The EQ will be disabled while reconfiguring it, remember the
  406. * current configuration.
  407. */
  408. save = snd_soc_read(codec, WM8904_EQ1);
  409. for (i = 0; i < WM8904_EQ_REGS; i++)
  410. snd_soc_update_bits(codec, WM8904_EQ1 + i, 0xffff,
  411. pdata->retune_mobile_cfgs[best].regs[i]);
  412. snd_soc_update_bits(codec, WM8904_EQ1, WM8904_EQ_ENA, save);
  413. }
  414. static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  415. struct snd_ctl_elem_value *ucontrol)
  416. {
  417. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  418. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  419. struct wm8904_pdata *pdata = wm8904->pdata;
  420. int value = ucontrol->value.integer.value[0];
  421. if (value >= pdata->num_retune_mobile_cfgs)
  422. return -EINVAL;
  423. wm8904->retune_mobile_cfg = value;
  424. wm8904_set_retune_mobile(codec);
  425. return 0;
  426. }
  427. static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  428. struct snd_ctl_elem_value *ucontrol)
  429. {
  430. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  431. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  432. ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
  433. return 0;
  434. }
  435. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  436. static int wm8904_set_deemph(struct snd_soc_codec *codec)
  437. {
  438. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  439. int val, i, best;
  440. /* If we're using deemphasis select the nearest available sample
  441. * rate.
  442. */
  443. if (wm8904->deemph) {
  444. best = 1;
  445. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  446. if (abs(deemph_settings[i] - wm8904->fs) <
  447. abs(deemph_settings[best] - wm8904->fs))
  448. best = i;
  449. }
  450. val = best << WM8904_DEEMPH_SHIFT;
  451. } else {
  452. val = 0;
  453. }
  454. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  455. return snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  456. WM8904_DEEMPH_MASK, val);
  457. }
  458. static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
  459. struct snd_ctl_elem_value *ucontrol)
  460. {
  461. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  462. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  463. ucontrol->value.integer.value[0] = wm8904->deemph;
  464. return 0;
  465. }
  466. static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
  467. struct snd_ctl_elem_value *ucontrol)
  468. {
  469. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  470. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  471. int deemph = ucontrol->value.integer.value[0];
  472. if (deemph > 1)
  473. return -EINVAL;
  474. wm8904->deemph = deemph;
  475. return wm8904_set_deemph(codec);
  476. }
  477. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  478. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  479. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  480. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  481. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  482. static const char *input_mode_text[] = {
  483. "Single-Ended", "Differential Line", "Differential Mic"
  484. };
  485. static const struct soc_enum lin_mode =
  486. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  487. static const struct soc_enum rin_mode =
  488. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  489. static const char *hpf_mode_text[] = {
  490. "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
  491. };
  492. static const struct soc_enum hpf_mode =
  493. SOC_ENUM_SINGLE(WM8904_ADC_DIGITAL_0, 5, 4, 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_kcontrol_chip(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. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  525. .name = "ADC 128x OSR Switch",
  526. .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,
  527. .put = wm8904_adc_osr_put,
  528. .private_value = SOC_SINGLE_VALUE(WM8904_ANALOGUE_ADC_0, 0, 1, 0),
  529. },
  530. };
  531. static const char *drc_path_text[] = {
  532. "ADC", "DAC"
  533. };
  534. static const struct soc_enum drc_path =
  535. SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
  536. static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
  537. SOC_SINGLE_TLV("Digital Playback Boost Volume",
  538. WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
  539. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
  540. WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  541. SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
  542. WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
  543. SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
  544. WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
  545. SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
  546. WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
  547. SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
  548. WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
  549. SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
  550. WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
  551. SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
  552. WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
  553. SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
  554. SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
  555. SOC_ENUM("DRC Path", drc_path),
  556. SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
  557. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  558. wm8904_get_deemph, wm8904_put_deemph),
  559. };
  560. static const struct snd_kcontrol_new wm8904_snd_controls[] = {
  561. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
  562. sidetone_tlv),
  563. };
  564. static const struct snd_kcontrol_new wm8904_eq_controls[] = {
  565. SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
  566. SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
  567. SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
  568. SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
  569. SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
  570. };
  571. static int cp_event(struct snd_soc_dapm_widget *w,
  572. struct snd_kcontrol *kcontrol, int event)
  573. {
  574. BUG_ON(event != SND_SOC_DAPM_POST_PMU);
  575. /* Maximum startup time */
  576. udelay(500);
  577. return 0;
  578. }
  579. static int sysclk_event(struct snd_soc_dapm_widget *w,
  580. struct snd_kcontrol *kcontrol, int event)
  581. {
  582. struct snd_soc_codec *codec = w->codec;
  583. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  584. switch (event) {
  585. case SND_SOC_DAPM_PRE_PMU:
  586. /* If we're using the FLL then we only start it when
  587. * required; we assume that the configuration has been
  588. * done previously and all we need to do is kick it
  589. * off.
  590. */
  591. switch (wm8904->sysclk_src) {
  592. case WM8904_CLK_FLL:
  593. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  594. WM8904_FLL_OSC_ENA,
  595. WM8904_FLL_OSC_ENA);
  596. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  597. WM8904_FLL_ENA,
  598. WM8904_FLL_ENA);
  599. break;
  600. default:
  601. break;
  602. }
  603. break;
  604. case SND_SOC_DAPM_POST_PMD:
  605. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  606. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  607. break;
  608. }
  609. return 0;
  610. }
  611. static int out_pga_event(struct snd_soc_dapm_widget *w,
  612. struct snd_kcontrol *kcontrol, int event)
  613. {
  614. struct snd_soc_codec *codec = w->codec;
  615. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  616. int reg, val;
  617. int dcs_mask;
  618. int dcs_l, dcs_r;
  619. int dcs_l_reg, dcs_r_reg;
  620. int timeout;
  621. int pwr_reg;
  622. /* This code is shared between HP and LINEOUT; we do all our
  623. * power management in stereo pairs to avoid latency issues so
  624. * we reuse shift to identify which rather than strcmp() the
  625. * name. */
  626. reg = w->shift;
  627. switch (reg) {
  628. case WM8904_ANALOGUE_HP_0:
  629. pwr_reg = WM8904_POWER_MANAGEMENT_2;
  630. dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
  631. dcs_r_reg = WM8904_DC_SERVO_8;
  632. dcs_l_reg = WM8904_DC_SERVO_9;
  633. dcs_l = 0;
  634. dcs_r = 1;
  635. break;
  636. case WM8904_ANALOGUE_LINEOUT_0:
  637. pwr_reg = WM8904_POWER_MANAGEMENT_3;
  638. dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
  639. dcs_r_reg = WM8904_DC_SERVO_6;
  640. dcs_l_reg = WM8904_DC_SERVO_7;
  641. dcs_l = 2;
  642. dcs_r = 3;
  643. break;
  644. default:
  645. BUG();
  646. return -EINVAL;
  647. }
  648. switch (event) {
  649. case SND_SOC_DAPM_PRE_PMU:
  650. /* Power on the PGAs */
  651. snd_soc_update_bits(codec, pwr_reg,
  652. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  653. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
  654. /* Power on the amplifier */
  655. snd_soc_update_bits(codec, reg,
  656. WM8904_HPL_ENA | WM8904_HPR_ENA,
  657. WM8904_HPL_ENA | WM8904_HPR_ENA);
  658. /* Enable the first stage */
  659. snd_soc_update_bits(codec, reg,
  660. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
  661. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
  662. /* Power up the DC servo */
  663. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  664. dcs_mask, dcs_mask);
  665. /* Either calibrate the DC servo or restore cached state
  666. * if we have that.
  667. */
  668. if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
  669. dev_dbg(codec->dev, "Restoring DC servo state\n");
  670. snd_soc_write(codec, dcs_l_reg,
  671. wm8904->dcs_state[dcs_l]);
  672. snd_soc_write(codec, dcs_r_reg,
  673. wm8904->dcs_state[dcs_r]);
  674. snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
  675. timeout = 20;
  676. } else {
  677. dev_dbg(codec->dev, "Calibrating DC servo\n");
  678. snd_soc_write(codec, WM8904_DC_SERVO_1,
  679. dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
  680. timeout = 500;
  681. }
  682. /* Wait for DC servo to complete */
  683. dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
  684. do {
  685. val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
  686. if ((val & dcs_mask) == dcs_mask)
  687. break;
  688. msleep(1);
  689. } while (--timeout);
  690. if ((val & dcs_mask) != dcs_mask)
  691. dev_warn(codec->dev, "DC servo timed out\n");
  692. else
  693. dev_dbg(codec->dev, "DC servo ready\n");
  694. /* Enable the output stage */
  695. snd_soc_update_bits(codec, reg,
  696. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  697. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
  698. break;
  699. case SND_SOC_DAPM_POST_PMU:
  700. /* Unshort the output itself */
  701. snd_soc_update_bits(codec, reg,
  702. WM8904_HPL_RMV_SHORT |
  703. WM8904_HPR_RMV_SHORT,
  704. WM8904_HPL_RMV_SHORT |
  705. WM8904_HPR_RMV_SHORT);
  706. break;
  707. case SND_SOC_DAPM_PRE_PMD:
  708. /* Short the output */
  709. snd_soc_update_bits(codec, reg,
  710. WM8904_HPL_RMV_SHORT |
  711. WM8904_HPR_RMV_SHORT, 0);
  712. break;
  713. case SND_SOC_DAPM_POST_PMD:
  714. /* Cache the DC servo configuration; this will be
  715. * invalidated if we change the configuration. */
  716. wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
  717. wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
  718. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  719. dcs_mask, 0);
  720. /* Disable the amplifier input and output stages */
  721. snd_soc_update_bits(codec, reg,
  722. WM8904_HPL_ENA | WM8904_HPR_ENA |
  723. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
  724. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  725. 0);
  726. /* PGAs too */
  727. snd_soc_update_bits(codec, pwr_reg,
  728. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  729. 0);
  730. break;
  731. }
  732. return 0;
  733. }
  734. static const char *lin_text[] = {
  735. "IN1L", "IN2L", "IN3L"
  736. };
  737. static const struct soc_enum lin_enum =
  738. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
  739. static const struct snd_kcontrol_new lin_mux =
  740. SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
  741. static const struct soc_enum lin_inv_enum =
  742. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
  743. static const struct snd_kcontrol_new lin_inv_mux =
  744. SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
  745. static const char *rin_text[] = {
  746. "IN1R", "IN2R", "IN3R"
  747. };
  748. static const struct soc_enum rin_enum =
  749. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
  750. static const struct snd_kcontrol_new rin_mux =
  751. SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
  752. static const struct soc_enum rin_inv_enum =
  753. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
  754. static const struct snd_kcontrol_new rin_inv_mux =
  755. SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
  756. static const char *aif_text[] = {
  757. "Left", "Right"
  758. };
  759. static const struct soc_enum aifoutl_enum =
  760. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
  761. static const struct snd_kcontrol_new aifoutl_mux =
  762. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  763. static const struct soc_enum aifoutr_enum =
  764. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
  765. static const struct snd_kcontrol_new aifoutr_mux =
  766. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  767. static const struct soc_enum aifinl_enum =
  768. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
  769. static const struct snd_kcontrol_new aifinl_mux =
  770. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  771. static const struct soc_enum aifinr_enum =
  772. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
  773. static const struct snd_kcontrol_new aifinr_mux =
  774. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  775. static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
  776. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
  777. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  778. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
  779. SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
  780. };
  781. static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
  782. SND_SOC_DAPM_INPUT("IN1L"),
  783. SND_SOC_DAPM_INPUT("IN1R"),
  784. SND_SOC_DAPM_INPUT("IN2L"),
  785. SND_SOC_DAPM_INPUT("IN2R"),
  786. SND_SOC_DAPM_INPUT("IN3L"),
  787. SND_SOC_DAPM_INPUT("IN3R"),
  788. SND_SOC_DAPM_SUPPLY("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
  789. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
  790. SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  791. &lin_inv_mux),
  792. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
  793. SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  794. &rin_inv_mux),
  795. SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
  796. NULL, 0),
  797. SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
  798. NULL, 0),
  799. SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
  800. SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
  801. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  802. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  803. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  804. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  805. };
  806. static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
  807. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  808. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  809. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  810. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  811. SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
  812. SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
  813. SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
  814. SND_SOC_DAPM_POST_PMU),
  815. SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  816. SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  817. SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  818. SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  819. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_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_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
  824. 0, NULL, 0, out_pga_event,
  825. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  826. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  827. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  828. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  829. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  830. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  831. };
  832. static const char *out_mux_text[] = {
  833. "DAC", "Bypass"
  834. };
  835. static const struct soc_enum hpl_enum =
  836. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
  837. static const struct snd_kcontrol_new hpl_mux =
  838. SOC_DAPM_ENUM("HPL Mux", hpl_enum);
  839. static const struct soc_enum hpr_enum =
  840. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
  841. static const struct snd_kcontrol_new hpr_mux =
  842. SOC_DAPM_ENUM("HPR Mux", hpr_enum);
  843. static const struct soc_enum linel_enum =
  844. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
  845. static const struct snd_kcontrol_new linel_mux =
  846. SOC_DAPM_ENUM("LINEL Mux", linel_enum);
  847. static const struct soc_enum liner_enum =
  848. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
  849. static const struct snd_kcontrol_new liner_mux =
  850. SOC_DAPM_ENUM("LINEL Mux", liner_enum);
  851. static const char *sidetone_text[] = {
  852. "None", "Left", "Right"
  853. };
  854. static const struct soc_enum dacl_sidetone_enum =
  855. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
  856. static const struct snd_kcontrol_new dacl_sidetone_mux =
  857. SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
  858. static const struct soc_enum dacr_sidetone_enum =
  859. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
  860. static const struct snd_kcontrol_new dacr_sidetone_mux =
  861. SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
  862. static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
  863. SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
  864. SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  865. SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  866. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
  867. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
  868. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  869. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  870. SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
  871. SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
  872. };
  873. static const struct snd_soc_dapm_route core_intercon[] = {
  874. { "CLK_DSP", NULL, "SYSCLK" },
  875. { "TOCLK", NULL, "SYSCLK" },
  876. };
  877. static const struct snd_soc_dapm_route adc_intercon[] = {
  878. { "Left Capture Mux", "IN1L", "IN1L" },
  879. { "Left Capture Mux", "IN2L", "IN2L" },
  880. { "Left Capture Mux", "IN3L", "IN3L" },
  881. { "Left Capture Inverting Mux", "IN1L", "IN1L" },
  882. { "Left Capture Inverting Mux", "IN2L", "IN2L" },
  883. { "Left Capture Inverting Mux", "IN3L", "IN3L" },
  884. { "Right Capture Mux", "IN1R", "IN1R" },
  885. { "Right Capture Mux", "IN2R", "IN2R" },
  886. { "Right Capture Mux", "IN3R", "IN3R" },
  887. { "Right Capture Inverting Mux", "IN1R", "IN1R" },
  888. { "Right Capture Inverting Mux", "IN2R", "IN2R" },
  889. { "Right Capture Inverting Mux", "IN3R", "IN3R" },
  890. { "Left Capture PGA", NULL, "Left Capture Mux" },
  891. { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
  892. { "Right Capture PGA", NULL, "Right Capture Mux" },
  893. { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
  894. { "AIFOUTL", "Left", "ADCL" },
  895. { "AIFOUTL", "Right", "ADCR" },
  896. { "AIFOUTR", "Left", "ADCL" },
  897. { "AIFOUTR", "Right", "ADCR" },
  898. { "ADCL", NULL, "CLK_DSP" },
  899. { "ADCL", NULL, "Left Capture PGA" },
  900. { "ADCR", NULL, "CLK_DSP" },
  901. { "ADCR", NULL, "Right Capture PGA" },
  902. };
  903. static const struct snd_soc_dapm_route dac_intercon[] = {
  904. { "DACL", "Right", "AIFINR" },
  905. { "DACL", "Left", "AIFINL" },
  906. { "DACL", NULL, "CLK_DSP" },
  907. { "DACR", "Right", "AIFINR" },
  908. { "DACR", "Left", "AIFINL" },
  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 = &codec->dapm;
  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, core_intercon,
  977. ARRAY_SIZE(core_intercon));
  978. snd_soc_dapm_add_routes(dapm, adc_intercon,
  979. ARRAY_SIZE(adc_intercon));
  980. snd_soc_dapm_add_routes(dapm, dac_intercon,
  981. ARRAY_SIZE(dac_intercon));
  982. snd_soc_dapm_add_routes(dapm, wm8904_intercon,
  983. ARRAY_SIZE(wm8904_intercon));
  984. break;
  985. case WM8912:
  986. snd_soc_add_codec_controls(codec, wm8904_dac_snd_controls,
  987. ARRAY_SIZE(wm8904_dac_snd_controls));
  988. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  989. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  990. snd_soc_dapm_add_routes(dapm, dac_intercon,
  991. ARRAY_SIZE(dac_intercon));
  992. snd_soc_dapm_add_routes(dapm, wm8912_intercon,
  993. ARRAY_SIZE(wm8912_intercon));
  994. break;
  995. }
  996. snd_soc_dapm_new_widgets(dapm);
  997. return 0;
  998. }
  999. static struct {
  1000. int ratio;
  1001. unsigned int clk_sys_rate;
  1002. } clk_sys_rates[] = {
  1003. { 64, 0 },
  1004. { 128, 1 },
  1005. { 192, 2 },
  1006. { 256, 3 },
  1007. { 384, 4 },
  1008. { 512, 5 },
  1009. { 786, 6 },
  1010. { 1024, 7 },
  1011. { 1408, 8 },
  1012. { 1536, 9 },
  1013. };
  1014. static struct {
  1015. int rate;
  1016. int sample_rate;
  1017. } sample_rates[] = {
  1018. { 8000, 0 },
  1019. { 11025, 1 },
  1020. { 12000, 1 },
  1021. { 16000, 2 },
  1022. { 22050, 3 },
  1023. { 24000, 3 },
  1024. { 32000, 4 },
  1025. { 44100, 5 },
  1026. { 48000, 5 },
  1027. };
  1028. static struct {
  1029. int div; /* *10 due to .5s */
  1030. int bclk_div;
  1031. } bclk_divs[] = {
  1032. { 10, 0 },
  1033. { 15, 1 },
  1034. { 20, 2 },
  1035. { 30, 3 },
  1036. { 40, 4 },
  1037. { 50, 5 },
  1038. { 55, 6 },
  1039. { 60, 7 },
  1040. { 80, 8 },
  1041. { 100, 9 },
  1042. { 110, 10 },
  1043. { 120, 11 },
  1044. { 160, 12 },
  1045. { 200, 13 },
  1046. { 220, 14 },
  1047. { 240, 16 },
  1048. { 200, 17 },
  1049. { 320, 18 },
  1050. { 440, 19 },
  1051. { 480, 20 },
  1052. };
  1053. static int wm8904_hw_params(struct snd_pcm_substream *substream,
  1054. struct snd_pcm_hw_params *params,
  1055. struct snd_soc_dai *dai)
  1056. {
  1057. struct snd_soc_codec *codec = dai->codec;
  1058. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1059. int ret, i, best, best_val, cur_val;
  1060. unsigned int aif1 = 0;
  1061. unsigned int aif2 = 0;
  1062. unsigned int aif3 = 0;
  1063. unsigned int clock1 = 0;
  1064. unsigned int dac_digital1 = 0;
  1065. /* What BCLK do we need? */
  1066. wm8904->fs = params_rate(params);
  1067. if (wm8904->tdm_slots) {
  1068. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1069. wm8904->tdm_slots, wm8904->tdm_width);
  1070. wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
  1071. wm8904->tdm_width, 2,
  1072. wm8904->tdm_slots);
  1073. } else {
  1074. wm8904->bclk = snd_soc_params_to_bclk(params);
  1075. }
  1076. switch (params_format(params)) {
  1077. case SNDRV_PCM_FORMAT_S16_LE:
  1078. break;
  1079. case SNDRV_PCM_FORMAT_S20_3LE:
  1080. aif1 |= 0x40;
  1081. break;
  1082. case SNDRV_PCM_FORMAT_S24_LE:
  1083. aif1 |= 0x80;
  1084. break;
  1085. case SNDRV_PCM_FORMAT_S32_LE:
  1086. aif1 |= 0xc0;
  1087. break;
  1088. default:
  1089. return -EINVAL;
  1090. }
  1091. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
  1092. ret = wm8904_configure_clocking(codec);
  1093. if (ret != 0)
  1094. return ret;
  1095. /* Select nearest CLK_SYS_RATE */
  1096. best = 0;
  1097. best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
  1098. - wm8904->fs);
  1099. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1100. cur_val = abs((wm8904->sysclk_rate /
  1101. clk_sys_rates[i].ratio) - wm8904->fs);
  1102. if (cur_val < best_val) {
  1103. best = i;
  1104. best_val = cur_val;
  1105. }
  1106. }
  1107. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1108. clk_sys_rates[best].ratio);
  1109. clock1 |= (clk_sys_rates[best].clk_sys_rate
  1110. << WM8904_CLK_SYS_RATE_SHIFT);
  1111. /* SAMPLE_RATE */
  1112. best = 0;
  1113. best_val = abs(wm8904->fs - sample_rates[0].rate);
  1114. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1115. /* Closest match */
  1116. cur_val = abs(wm8904->fs - sample_rates[i].rate);
  1117. if (cur_val < best_val) {
  1118. best = i;
  1119. best_val = cur_val;
  1120. }
  1121. }
  1122. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1123. sample_rates[best].rate);
  1124. clock1 |= (sample_rates[best].sample_rate
  1125. << WM8904_SAMPLE_RATE_SHIFT);
  1126. /* Enable sloping stopband filter for low sample rates */
  1127. if (wm8904->fs <= 24000)
  1128. dac_digital1 |= WM8904_DAC_SB_FILT;
  1129. /* BCLK_DIV */
  1130. best = 0;
  1131. best_val = INT_MAX;
  1132. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1133. cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
  1134. - wm8904->bclk;
  1135. if (cur_val < 0) /* Table is sorted */
  1136. break;
  1137. if (cur_val < best_val) {
  1138. best = i;
  1139. best_val = cur_val;
  1140. }
  1141. }
  1142. wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
  1143. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1144. bclk_divs[best].div, wm8904->bclk);
  1145. aif2 |= bclk_divs[best].bclk_div;
  1146. /* LRCLK is a simple fraction of BCLK */
  1147. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
  1148. aif3 |= wm8904->bclk / wm8904->fs;
  1149. /* Apply the settings */
  1150. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  1151. WM8904_DAC_SB_FILT, dac_digital1);
  1152. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1153. WM8904_AIF_WL_MASK, aif1);
  1154. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
  1155. WM8904_BCLK_DIV_MASK, aif2);
  1156. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1157. WM8904_LRCLK_RATE_MASK, aif3);
  1158. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
  1159. WM8904_SAMPLE_RATE_MASK |
  1160. WM8904_CLK_SYS_RATE_MASK, clock1);
  1161. /* Update filters for the new settings */
  1162. wm8904_set_retune_mobile(codec);
  1163. wm8904_set_deemph(codec);
  1164. return 0;
  1165. }
  1166. static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1167. unsigned int freq, int dir)
  1168. {
  1169. struct snd_soc_codec *codec = dai->codec;
  1170. struct wm8904_priv *priv = snd_soc_codec_get_drvdata(codec);
  1171. switch (clk_id) {
  1172. case WM8904_CLK_MCLK:
  1173. priv->sysclk_src = clk_id;
  1174. priv->mclk_rate = freq;
  1175. break;
  1176. case WM8904_CLK_FLL:
  1177. priv->sysclk_src = clk_id;
  1178. break;
  1179. default:
  1180. return -EINVAL;
  1181. }
  1182. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  1183. wm8904_configure_clocking(codec);
  1184. return 0;
  1185. }
  1186. static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1187. {
  1188. struct snd_soc_codec *codec = dai->codec;
  1189. unsigned int aif1 = 0;
  1190. unsigned int aif3 = 0;
  1191. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1192. case SND_SOC_DAIFMT_CBS_CFS:
  1193. break;
  1194. case SND_SOC_DAIFMT_CBS_CFM:
  1195. aif3 |= WM8904_LRCLK_DIR;
  1196. break;
  1197. case SND_SOC_DAIFMT_CBM_CFS:
  1198. aif1 |= WM8904_BCLK_DIR;
  1199. break;
  1200. case SND_SOC_DAIFMT_CBM_CFM:
  1201. aif1 |= WM8904_BCLK_DIR;
  1202. aif3 |= WM8904_LRCLK_DIR;
  1203. break;
  1204. default:
  1205. return -EINVAL;
  1206. }
  1207. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1208. case SND_SOC_DAIFMT_DSP_B:
  1209. aif1 |= 0x3 | WM8904_AIF_LRCLK_INV;
  1210. case SND_SOC_DAIFMT_DSP_A:
  1211. aif1 |= 0x3;
  1212. break;
  1213. case SND_SOC_DAIFMT_I2S:
  1214. aif1 |= 0x2;
  1215. break;
  1216. case SND_SOC_DAIFMT_RIGHT_J:
  1217. break;
  1218. case SND_SOC_DAIFMT_LEFT_J:
  1219. aif1 |= 0x1;
  1220. break;
  1221. default:
  1222. return -EINVAL;
  1223. }
  1224. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1225. case SND_SOC_DAIFMT_DSP_A:
  1226. case SND_SOC_DAIFMT_DSP_B:
  1227. /* frame inversion not valid for DSP modes */
  1228. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1229. case SND_SOC_DAIFMT_NB_NF:
  1230. break;
  1231. case SND_SOC_DAIFMT_IB_NF:
  1232. aif1 |= WM8904_AIF_BCLK_INV;
  1233. break;
  1234. default:
  1235. return -EINVAL;
  1236. }
  1237. break;
  1238. case SND_SOC_DAIFMT_I2S:
  1239. case SND_SOC_DAIFMT_RIGHT_J:
  1240. case SND_SOC_DAIFMT_LEFT_J:
  1241. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1242. case SND_SOC_DAIFMT_NB_NF:
  1243. break;
  1244. case SND_SOC_DAIFMT_IB_IF:
  1245. aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
  1246. break;
  1247. case SND_SOC_DAIFMT_IB_NF:
  1248. aif1 |= WM8904_AIF_BCLK_INV;
  1249. break;
  1250. case SND_SOC_DAIFMT_NB_IF:
  1251. aif1 |= WM8904_AIF_LRCLK_INV;
  1252. break;
  1253. default:
  1254. return -EINVAL;
  1255. }
  1256. break;
  1257. default:
  1258. return -EINVAL;
  1259. }
  1260. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1261. WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
  1262. WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
  1263. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1264. WM8904_LRCLK_DIR, aif3);
  1265. return 0;
  1266. }
  1267. static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1268. unsigned int rx_mask, int slots, int slot_width)
  1269. {
  1270. struct snd_soc_codec *codec = dai->codec;
  1271. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1272. int aif1 = 0;
  1273. /* Don't need to validate anything if we're turning off TDM */
  1274. if (slots == 0)
  1275. goto out;
  1276. /* Note that we allow configurations we can't handle ourselves -
  1277. * for example, we can generate clocks for slots 2 and up even if
  1278. * we can't use those slots ourselves.
  1279. */
  1280. aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
  1281. switch (rx_mask) {
  1282. case 3:
  1283. break;
  1284. case 0xc:
  1285. aif1 |= WM8904_AIFADC_TDM_CHAN;
  1286. break;
  1287. default:
  1288. return -EINVAL;
  1289. }
  1290. switch (tx_mask) {
  1291. case 3:
  1292. break;
  1293. case 0xc:
  1294. aif1 |= WM8904_AIFDAC_TDM_CHAN;
  1295. break;
  1296. default:
  1297. return -EINVAL;
  1298. }
  1299. out:
  1300. wm8904->tdm_width = slot_width;
  1301. wm8904->tdm_slots = slots / 2;
  1302. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1303. WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
  1304. WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
  1305. return 0;
  1306. }
  1307. struct _fll_div {
  1308. u16 fll_fratio;
  1309. u16 fll_outdiv;
  1310. u16 fll_clk_ref_div;
  1311. u16 n;
  1312. u16 k;
  1313. };
  1314. /* The size in bits of the FLL divide multiplied by 10
  1315. * to allow rounding later */
  1316. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1317. static struct {
  1318. unsigned int min;
  1319. unsigned int max;
  1320. u16 fll_fratio;
  1321. int ratio;
  1322. } fll_fratios[] = {
  1323. { 0, 64000, 4, 16 },
  1324. { 64000, 128000, 3, 8 },
  1325. { 128000, 256000, 2, 4 },
  1326. { 256000, 1000000, 1, 2 },
  1327. { 1000000, 13500000, 0, 1 },
  1328. };
  1329. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1330. unsigned int Fout)
  1331. {
  1332. u64 Kpart;
  1333. unsigned int K, Ndiv, Nmod, target;
  1334. unsigned int div;
  1335. int i;
  1336. /* Fref must be <=13.5MHz */
  1337. div = 1;
  1338. fll_div->fll_clk_ref_div = 0;
  1339. while ((Fref / div) > 13500000) {
  1340. div *= 2;
  1341. fll_div->fll_clk_ref_div++;
  1342. if (div > 8) {
  1343. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1344. Fref);
  1345. return -EINVAL;
  1346. }
  1347. }
  1348. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  1349. /* Apply the division for our remaining calculations */
  1350. Fref /= div;
  1351. /* Fvco should be 90-100MHz; don't check the upper bound */
  1352. div = 4;
  1353. while (Fout * div < 90000000) {
  1354. div++;
  1355. if (div > 64) {
  1356. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1357. Fout);
  1358. return -EINVAL;
  1359. }
  1360. }
  1361. target = Fout * div;
  1362. fll_div->fll_outdiv = div - 1;
  1363. pr_debug("Fvco=%dHz\n", target);
  1364. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  1365. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1366. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1367. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1368. target /= fll_fratios[i].ratio;
  1369. break;
  1370. }
  1371. }
  1372. if (i == ARRAY_SIZE(fll_fratios)) {
  1373. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1374. return -EINVAL;
  1375. }
  1376. /* Now, calculate N.K */
  1377. Ndiv = target / Fref;
  1378. fll_div->n = Ndiv;
  1379. Nmod = target % Fref;
  1380. pr_debug("Nmod=%d\n", Nmod);
  1381. /* Calculate fractional part - scale up so we can round. */
  1382. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1383. do_div(Kpart, Fref);
  1384. K = Kpart & 0xFFFFFFFF;
  1385. if ((K % 10) >= 5)
  1386. K += 5;
  1387. /* Move down to proper range now rounding is done */
  1388. fll_div->k = K / 10;
  1389. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  1390. fll_div->n, fll_div->k,
  1391. fll_div->fll_fratio, fll_div->fll_outdiv,
  1392. fll_div->fll_clk_ref_div);
  1393. return 0;
  1394. }
  1395. static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  1396. unsigned int Fref, unsigned int Fout)
  1397. {
  1398. struct snd_soc_codec *codec = dai->codec;
  1399. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1400. struct _fll_div fll_div;
  1401. int ret, val;
  1402. int clock2, fll1;
  1403. /* Any change? */
  1404. if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
  1405. Fout == wm8904->fll_fout)
  1406. return 0;
  1407. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  1408. if (Fout == 0) {
  1409. dev_dbg(codec->dev, "FLL disabled\n");
  1410. wm8904->fll_fref = 0;
  1411. wm8904->fll_fout = 0;
  1412. /* Gate SYSCLK to avoid glitches */
  1413. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1414. WM8904_CLK_SYS_ENA, 0);
  1415. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1416. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1417. goto out;
  1418. }
  1419. /* Validate the FLL ID */
  1420. switch (source) {
  1421. case WM8904_FLL_MCLK:
  1422. case WM8904_FLL_LRCLK:
  1423. case WM8904_FLL_BCLK:
  1424. ret = fll_factors(&fll_div, Fref, Fout);
  1425. if (ret != 0)
  1426. return ret;
  1427. break;
  1428. case WM8904_FLL_FREE_RUNNING:
  1429. dev_dbg(codec->dev, "Using free running FLL\n");
  1430. /* Force 12MHz and output/4 for now */
  1431. Fout = 12000000;
  1432. Fref = 12000000;
  1433. memset(&fll_div, 0, sizeof(fll_div));
  1434. fll_div.fll_outdiv = 3;
  1435. break;
  1436. default:
  1437. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  1438. return -EINVAL;
  1439. }
  1440. /* Save current state then disable the FLL and SYSCLK to avoid
  1441. * misclocking */
  1442. fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
  1443. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1444. WM8904_CLK_SYS_ENA, 0);
  1445. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1446. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1447. /* Unlock forced oscilator control to switch it on/off */
  1448. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1449. WM8904_USER_KEY, WM8904_USER_KEY);
  1450. if (fll_id == WM8904_FLL_FREE_RUNNING) {
  1451. val = WM8904_FLL_FRC_NCO;
  1452. } else {
  1453. val = 0;
  1454. }
  1455. snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
  1456. val);
  1457. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1458. WM8904_USER_KEY, 0);
  1459. switch (fll_id) {
  1460. case WM8904_FLL_MCLK:
  1461. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1462. WM8904_FLL_CLK_REF_SRC_MASK, 0);
  1463. break;
  1464. case WM8904_FLL_LRCLK:
  1465. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1466. WM8904_FLL_CLK_REF_SRC_MASK, 1);
  1467. break;
  1468. case WM8904_FLL_BCLK:
  1469. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1470. WM8904_FLL_CLK_REF_SRC_MASK, 2);
  1471. break;
  1472. }
  1473. if (fll_div.k)
  1474. val = WM8904_FLL_FRACN_ENA;
  1475. else
  1476. val = 0;
  1477. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1478. WM8904_FLL_FRACN_ENA, val);
  1479. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
  1480. WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
  1481. (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
  1482. (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
  1483. snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
  1484. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
  1485. fll_div.n << WM8904_FLL_N_SHIFT);
  1486. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1487. WM8904_FLL_CLK_REF_DIV_MASK,
  1488. fll_div.fll_clk_ref_div
  1489. << WM8904_FLL_CLK_REF_DIV_SHIFT);
  1490. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  1491. wm8904->fll_fref = Fref;
  1492. wm8904->fll_fout = Fout;
  1493. wm8904->fll_src = source;
  1494. /* Enable the FLL if it was previously active */
  1495. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1496. WM8904_FLL_OSC_ENA, fll1);
  1497. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1498. WM8904_FLL_ENA, fll1);
  1499. out:
  1500. /* Reenable SYSCLK if it was previously active */
  1501. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1502. WM8904_CLK_SYS_ENA, clock2);
  1503. return 0;
  1504. }
  1505. static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1506. {
  1507. struct snd_soc_codec *codec = codec_dai->codec;
  1508. int val;
  1509. if (mute)
  1510. val = WM8904_DAC_MUTE;
  1511. else
  1512. val = 0;
  1513. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
  1514. return 0;
  1515. }
  1516. static int wm8904_set_bias_level(struct snd_soc_codec *codec,
  1517. enum snd_soc_bias_level level)
  1518. {
  1519. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1520. int ret;
  1521. switch (level) {
  1522. case SND_SOC_BIAS_ON:
  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 (codec->dapm.bias_level == 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_sync(wm8904->regmap);
  1544. /* Enable bias */
  1545. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1546. WM8904_BIAS_ENA, WM8904_BIAS_ENA);
  1547. /* Enable VMID, VMID buffering, 2*5k resistance */
  1548. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1549. WM8904_VMID_ENA |
  1550. WM8904_VMID_RES_MASK,
  1551. WM8904_VMID_ENA |
  1552. 0x3 << WM8904_VMID_RES_SHIFT);
  1553. /* Let VMID ramp */
  1554. msleep(1);
  1555. }
  1556. /* Maintain VMID with 2*250k */
  1557. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1558. WM8904_VMID_RES_MASK,
  1559. 0x2 << WM8904_VMID_RES_SHIFT);
  1560. /* Bias current *0.5 */
  1561. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1562. WM8904_ISEL_MASK, 0);
  1563. break;
  1564. case SND_SOC_BIAS_OFF:
  1565. /* Turn off VMID */
  1566. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1567. WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
  1568. /* Stop bias generation */
  1569. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1570. WM8904_BIAS_ENA, 0);
  1571. #ifdef CONFIG_REGULATOR
  1572. /* Post 2.6.34 we will be able to get a callback when
  1573. * the regulators are disabled which we can use but
  1574. * for now just assume that the power will be cut if
  1575. * the regulator API is in use.
  1576. */
  1577. codec->cache_sync = 1;
  1578. #endif
  1579. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
  1580. wm8904->supplies);
  1581. break;
  1582. }
  1583. codec->dapm.bias_level = level;
  1584. return 0;
  1585. }
  1586. #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
  1587. #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1588. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1589. static const struct snd_soc_dai_ops wm8904_dai_ops = {
  1590. .set_sysclk = wm8904_set_sysclk,
  1591. .set_fmt = wm8904_set_fmt,
  1592. .set_tdm_slot = wm8904_set_tdm_slot,
  1593. .set_pll = wm8904_set_fll,
  1594. .hw_params = wm8904_hw_params,
  1595. .digital_mute = wm8904_digital_mute,
  1596. };
  1597. static struct snd_soc_dai_driver wm8904_dai = {
  1598. .name = "wm8904-hifi",
  1599. .playback = {
  1600. .stream_name = "Playback",
  1601. .channels_min = 2,
  1602. .channels_max = 2,
  1603. .rates = WM8904_RATES,
  1604. .formats = WM8904_FORMATS,
  1605. },
  1606. .capture = {
  1607. .stream_name = "Capture",
  1608. .channels_min = 2,
  1609. .channels_max = 2,
  1610. .rates = WM8904_RATES,
  1611. .formats = WM8904_FORMATS,
  1612. },
  1613. .ops = &wm8904_dai_ops,
  1614. .symmetric_rates = 1,
  1615. };
  1616. #ifdef CONFIG_PM
  1617. static int wm8904_suspend(struct snd_soc_codec *codec)
  1618. {
  1619. wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1620. return 0;
  1621. }
  1622. static int wm8904_resume(struct snd_soc_codec *codec)
  1623. {
  1624. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1625. return 0;
  1626. }
  1627. #else
  1628. #define wm8904_suspend NULL
  1629. #define wm8904_resume NULL
  1630. #endif
  1631. static void wm8904_handle_retune_mobile_pdata(struct snd_soc_codec *codec)
  1632. {
  1633. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1634. struct wm8904_pdata *pdata = wm8904->pdata;
  1635. struct snd_kcontrol_new control =
  1636. SOC_ENUM_EXT("EQ Mode",
  1637. wm8904->retune_mobile_enum,
  1638. wm8904_get_retune_mobile_enum,
  1639. wm8904_put_retune_mobile_enum);
  1640. int ret, i, j;
  1641. const char **t;
  1642. /* We need an array of texts for the enum API but the number
  1643. * of texts is likely to be less than the number of
  1644. * configurations due to the sample rate dependency of the
  1645. * configurations. */
  1646. wm8904->num_retune_mobile_texts = 0;
  1647. wm8904->retune_mobile_texts = NULL;
  1648. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  1649. for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
  1650. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  1651. wm8904->retune_mobile_texts[j]) == 0)
  1652. break;
  1653. }
  1654. if (j != wm8904->num_retune_mobile_texts)
  1655. continue;
  1656. /* Expand the array... */
  1657. t = krealloc(wm8904->retune_mobile_texts,
  1658. sizeof(char *) *
  1659. (wm8904->num_retune_mobile_texts + 1),
  1660. GFP_KERNEL);
  1661. if (t == NULL)
  1662. continue;
  1663. /* ...store the new entry... */
  1664. t[wm8904->num_retune_mobile_texts] =
  1665. pdata->retune_mobile_cfgs[i].name;
  1666. /* ...and remember the new version. */
  1667. wm8904->num_retune_mobile_texts++;
  1668. wm8904->retune_mobile_texts = t;
  1669. }
  1670. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  1671. wm8904->num_retune_mobile_texts);
  1672. wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
  1673. wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
  1674. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1675. if (ret != 0)
  1676. dev_err(codec->dev,
  1677. "Failed to add ReTune Mobile control: %d\n", ret);
  1678. }
  1679. static void wm8904_handle_pdata(struct snd_soc_codec *codec)
  1680. {
  1681. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1682. struct wm8904_pdata *pdata = wm8904->pdata;
  1683. int ret, i;
  1684. if (!pdata) {
  1685. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1686. ARRAY_SIZE(wm8904_eq_controls));
  1687. return;
  1688. }
  1689. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  1690. if (pdata->num_drc_cfgs) {
  1691. struct snd_kcontrol_new control =
  1692. SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
  1693. wm8904_get_drc_enum, wm8904_put_drc_enum);
  1694. /* We need an array of texts for the enum API */
  1695. wm8904->drc_texts = kmalloc(sizeof(char *)
  1696. * pdata->num_drc_cfgs, GFP_KERNEL);
  1697. if (!wm8904->drc_texts) {
  1698. dev_err(codec->dev,
  1699. "Failed to allocate %d DRC config texts\n",
  1700. pdata->num_drc_cfgs);
  1701. return;
  1702. }
  1703. for (i = 0; i < pdata->num_drc_cfgs; i++)
  1704. wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
  1705. wm8904->drc_enum.max = pdata->num_drc_cfgs;
  1706. wm8904->drc_enum.texts = wm8904->drc_texts;
  1707. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1708. if (ret != 0)
  1709. dev_err(codec->dev,
  1710. "Failed to add DRC mode control: %d\n", ret);
  1711. wm8904_set_drc(codec);
  1712. }
  1713. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  1714. pdata->num_retune_mobile_cfgs);
  1715. if (pdata->num_retune_mobile_cfgs)
  1716. wm8904_handle_retune_mobile_pdata(codec);
  1717. else
  1718. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1719. ARRAY_SIZE(wm8904_eq_controls));
  1720. }
  1721. static int wm8904_probe(struct snd_soc_codec *codec)
  1722. {
  1723. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1724. struct wm8904_pdata *pdata = wm8904->pdata;
  1725. int ret, i;
  1726. codec->cache_sync = 1;
  1727. codec->control_data = wm8904->regmap;
  1728. switch (wm8904->devtype) {
  1729. case WM8904:
  1730. break;
  1731. case WM8912:
  1732. memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
  1733. break;
  1734. default:
  1735. dev_err(codec->dev, "Unknown device type %d\n",
  1736. wm8904->devtype);
  1737. return -EINVAL;
  1738. }
  1739. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  1740. if (ret != 0) {
  1741. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1742. return ret;
  1743. }
  1744. for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
  1745. wm8904->supplies[i].supply = wm8904_supply_names[i];
  1746. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8904->supplies),
  1747. wm8904->supplies);
  1748. if (ret != 0) {
  1749. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1750. return ret;
  1751. }
  1752. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1753. wm8904->supplies);
  1754. if (ret != 0) {
  1755. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1756. goto err_get;
  1757. }
  1758. ret = snd_soc_read(codec, WM8904_SW_RESET_AND_ID);
  1759. if (ret < 0) {
  1760. dev_err(codec->dev, "Failed to read ID register\n");
  1761. goto err_enable;
  1762. }
  1763. if (ret != 0x8904) {
  1764. dev_err(codec->dev, "Device is not a WM8904, ID is %x\n", ret);
  1765. ret = -EINVAL;
  1766. goto err_enable;
  1767. }
  1768. ret = snd_soc_read(codec, WM8904_REVISION);
  1769. if (ret < 0) {
  1770. dev_err(codec->dev, "Failed to read device revision: %d\n",
  1771. ret);
  1772. goto err_enable;
  1773. }
  1774. dev_info(codec->dev, "revision %c\n", ret + 'A');
  1775. ret = wm8904_reset(codec);
  1776. if (ret < 0) {
  1777. dev_err(codec->dev, "Failed to issue reset\n");
  1778. goto err_enable;
  1779. }
  1780. /* Change some default settings - latch VU and enable ZC */
  1781. snd_soc_update_bits(codec, WM8904_ADC_DIGITAL_VOLUME_LEFT,
  1782. WM8904_ADC_VU, WM8904_ADC_VU);
  1783. snd_soc_update_bits(codec, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
  1784. WM8904_ADC_VU, WM8904_ADC_VU);
  1785. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_VOLUME_LEFT,
  1786. WM8904_DAC_VU, WM8904_DAC_VU);
  1787. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
  1788. WM8904_DAC_VU, WM8904_DAC_VU);
  1789. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT1_LEFT,
  1790. WM8904_HPOUT_VU | WM8904_HPOUTLZC,
  1791. WM8904_HPOUT_VU | WM8904_HPOUTLZC);
  1792. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT1_RIGHT,
  1793. WM8904_HPOUT_VU | WM8904_HPOUTRZC,
  1794. WM8904_HPOUT_VU | WM8904_HPOUTRZC);
  1795. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT2_LEFT,
  1796. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
  1797. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
  1798. snd_soc_update_bits(codec, WM8904_ANALOGUE_OUT2_RIGHT,
  1799. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
  1800. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
  1801. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0,
  1802. WM8904_SR_MODE, 0);
  1803. /* Apply configuration from the platform data. */
  1804. if (wm8904->pdata) {
  1805. for (i = 0; i < WM8904_GPIO_REGS; i++) {
  1806. if (!pdata->gpio_cfg[i])
  1807. continue;
  1808. regmap_update_bits(wm8904->regmap,
  1809. WM8904_GPIO_CONTROL_1 + i,
  1810. 0xffff,
  1811. pdata->gpio_cfg[i]);
  1812. }
  1813. /* Zero is the default value for these anyway */
  1814. for (i = 0; i < WM8904_MIC_REGS; i++)
  1815. regmap_update_bits(wm8904->regmap,
  1816. WM8904_MIC_BIAS_CONTROL_0 + i,
  1817. 0xffff,
  1818. pdata->mic_cfg[i]);
  1819. }
  1820. /* Set Class W by default - this will be managed by the Class
  1821. * G widget at runtime where bypass paths are available.
  1822. */
  1823. snd_soc_update_bits(codec, WM8904_CLASS_W_0,
  1824. WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
  1825. /* Use normal bias source */
  1826. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1827. WM8904_POBCTRL, 0);
  1828. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1829. /* Bias level configuration will have done an extra enable */
  1830. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1831. wm8904_handle_pdata(codec);
  1832. wm8904_add_widgets(codec);
  1833. return 0;
  1834. err_enable:
  1835. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1836. err_get:
  1837. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1838. return ret;
  1839. }
  1840. static int wm8904_remove(struct snd_soc_codec *codec)
  1841. {
  1842. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1843. wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1844. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1845. kfree(wm8904->retune_mobile_texts);
  1846. kfree(wm8904->drc_texts);
  1847. return 0;
  1848. }
  1849. static struct snd_soc_codec_driver soc_codec_dev_wm8904 = {
  1850. .probe = wm8904_probe,
  1851. .remove = wm8904_remove,
  1852. .suspend = wm8904_suspend,
  1853. .resume = wm8904_resume,
  1854. .set_bias_level = wm8904_set_bias_level,
  1855. .idle_bias_off = true,
  1856. };
  1857. static const struct regmap_config wm8904_regmap = {
  1858. .reg_bits = 8,
  1859. .val_bits = 16,
  1860. .max_register = WM8904_MAX_REGISTER,
  1861. .volatile_reg = wm8904_volatile_register,
  1862. .readable_reg = wm8904_readable_register,
  1863. .cache_type = REGCACHE_RBTREE,
  1864. .reg_defaults = wm8904_reg_defaults,
  1865. .num_reg_defaults = ARRAY_SIZE(wm8904_reg_defaults),
  1866. };
  1867. static __devinit int wm8904_i2c_probe(struct i2c_client *i2c,
  1868. const struct i2c_device_id *id)
  1869. {
  1870. struct wm8904_priv *wm8904;
  1871. int ret;
  1872. wm8904 = devm_kzalloc(&i2c->dev, sizeof(struct wm8904_priv),
  1873. GFP_KERNEL);
  1874. if (wm8904 == NULL)
  1875. return -ENOMEM;
  1876. wm8904->regmap = regmap_init_i2c(i2c, &wm8904_regmap);
  1877. if (IS_ERR(wm8904->regmap)) {
  1878. ret = PTR_ERR(wm8904->regmap);
  1879. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1880. ret);
  1881. return ret;
  1882. }
  1883. wm8904->devtype = id->driver_data;
  1884. i2c_set_clientdata(i2c, wm8904);
  1885. wm8904->pdata = i2c->dev.platform_data;
  1886. ret = snd_soc_register_codec(&i2c->dev,
  1887. &soc_codec_dev_wm8904, &wm8904_dai, 1);
  1888. if (ret != 0)
  1889. goto err;
  1890. return 0;
  1891. err:
  1892. regmap_exit(wm8904->regmap);
  1893. return ret;
  1894. }
  1895. static __devexit int wm8904_i2c_remove(struct i2c_client *client)
  1896. {
  1897. struct wm8904_priv *wm8904 = i2c_get_clientdata(client);
  1898. snd_soc_unregister_codec(&client->dev);
  1899. regmap_exit(wm8904->regmap);
  1900. return 0;
  1901. }
  1902. static const struct i2c_device_id wm8904_i2c_id[] = {
  1903. { "wm8904", WM8904 },
  1904. { "wm8912", WM8912 },
  1905. { "wm8918", WM8904 }, /* Actually a subset, updates to follow */
  1906. { }
  1907. };
  1908. MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
  1909. static struct i2c_driver wm8904_i2c_driver = {
  1910. .driver = {
  1911. .name = "wm8904",
  1912. .owner = THIS_MODULE,
  1913. },
  1914. .probe = wm8904_i2c_probe,
  1915. .remove = __devexit_p(wm8904_i2c_remove),
  1916. .id_table = wm8904_i2c_id,
  1917. };
  1918. static int __init wm8904_modinit(void)
  1919. {
  1920. int ret = 0;
  1921. ret = i2c_add_driver(&wm8904_i2c_driver);
  1922. if (ret != 0) {
  1923. printk(KERN_ERR "Failed to register wm8904 I2C driver: %d\n",
  1924. ret);
  1925. }
  1926. return ret;
  1927. }
  1928. module_init(wm8904_modinit);
  1929. static void __exit wm8904_exit(void)
  1930. {
  1931. i2c_del_driver(&wm8904_i2c_driver);
  1932. }
  1933. module_exit(wm8904_exit);
  1934. MODULE_DESCRIPTION("ASoC WM8904 driver");
  1935. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1936. MODULE_LICENSE("GPL");