wm8904.c 62 KB

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