wm8996.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  1. /*
  2. * wm8996.c - WM8996 audio codec interface
  3. *
  4. * Copyright 2011 Wolfson Microelectronics PLC.
  5. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/completion.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/gcd.h>
  19. #include <linux/gpio.h>
  20. #include <linux/i2c.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <sound/core.h>
  26. #include <sound/jack.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <sound/initval.h>
  31. #include <sound/tlv.h>
  32. #include <trace/events/asoc.h>
  33. #include <sound/wm8996.h>
  34. #include "wm8996.h"
  35. #define WM8996_AIFS 2
  36. #define HPOUT1L 1
  37. #define HPOUT1R 2
  38. #define HPOUT2L 4
  39. #define HPOUT2R 8
  40. #define WM8996_NUM_SUPPLIES 3
  41. static const char *wm8996_supply_names[WM8996_NUM_SUPPLIES] = {
  42. "DBVDD",
  43. "AVDD1",
  44. "AVDD2",
  45. };
  46. struct wm8996_priv {
  47. struct device *dev;
  48. struct regmap *regmap;
  49. struct snd_soc_codec *codec;
  50. int ldo1ena;
  51. int sysclk;
  52. int sysclk_src;
  53. int fll_src;
  54. int fll_fref;
  55. int fll_fout;
  56. struct completion fll_lock;
  57. u16 dcs_pending;
  58. struct completion dcs_done;
  59. u16 hpout_ena;
  60. u16 hpout_pending;
  61. struct regulator_bulk_data supplies[WM8996_NUM_SUPPLIES];
  62. struct notifier_block disable_nb[WM8996_NUM_SUPPLIES];
  63. int bg_ena;
  64. struct wm8996_pdata pdata;
  65. int rx_rate[WM8996_AIFS];
  66. int bclk_rate[WM8996_AIFS];
  67. /* Platform dependant ReTune mobile configuration */
  68. int num_retune_mobile_texts;
  69. const char **retune_mobile_texts;
  70. int retune_mobile_cfg[2];
  71. struct soc_enum retune_mobile_enum;
  72. struct snd_soc_jack *jack;
  73. bool detecting;
  74. bool jack_mic;
  75. int jack_flips;
  76. wm8996_polarity_fn polarity_cb;
  77. #ifdef CONFIG_GPIOLIB
  78. struct gpio_chip gpio_chip;
  79. #endif
  80. };
  81. /* We can't use the same notifier block for more than one supply and
  82. * there's no way I can see to get from a callback to the caller
  83. * except container_of().
  84. */
  85. #define WM8996_REGULATOR_EVENT(n) \
  86. static int wm8996_regulator_event_##n(struct notifier_block *nb, \
  87. unsigned long event, void *data) \
  88. { \
  89. struct wm8996_priv *wm8996 = container_of(nb, struct wm8996_priv, \
  90. disable_nb[n]); \
  91. if (event & REGULATOR_EVENT_DISABLE) { \
  92. regcache_mark_dirty(wm8996->regmap); \
  93. } \
  94. return 0; \
  95. }
  96. WM8996_REGULATOR_EVENT(0)
  97. WM8996_REGULATOR_EVENT(1)
  98. WM8996_REGULATOR_EVENT(2)
  99. static struct reg_default wm8996_reg[] = {
  100. { WM8996_POWER_MANAGEMENT_1, 0x0 },
  101. { WM8996_POWER_MANAGEMENT_2, 0x0 },
  102. { WM8996_POWER_MANAGEMENT_3, 0x0 },
  103. { WM8996_POWER_MANAGEMENT_4, 0x0 },
  104. { WM8996_POWER_MANAGEMENT_5, 0x0 },
  105. { WM8996_POWER_MANAGEMENT_6, 0x0 },
  106. { WM8996_POWER_MANAGEMENT_7, 0x10 },
  107. { WM8996_POWER_MANAGEMENT_8, 0x0 },
  108. { WM8996_LEFT_LINE_INPUT_VOLUME, 0x0 },
  109. { WM8996_RIGHT_LINE_INPUT_VOLUME, 0x0 },
  110. { WM8996_LINE_INPUT_CONTROL, 0x0 },
  111. { WM8996_DAC1_HPOUT1_VOLUME, 0x88 },
  112. { WM8996_DAC2_HPOUT2_VOLUME, 0x88 },
  113. { WM8996_DAC1_LEFT_VOLUME, 0x2c0 },
  114. { WM8996_DAC1_RIGHT_VOLUME, 0x2c0 },
  115. { WM8996_DAC2_LEFT_VOLUME, 0x2c0 },
  116. { WM8996_DAC2_RIGHT_VOLUME, 0x2c0 },
  117. { WM8996_OUTPUT1_LEFT_VOLUME, 0x80 },
  118. { WM8996_OUTPUT1_RIGHT_VOLUME, 0x80 },
  119. { WM8996_OUTPUT2_LEFT_VOLUME, 0x80 },
  120. { WM8996_OUTPUT2_RIGHT_VOLUME, 0x80 },
  121. { WM8996_MICBIAS_1, 0x39 },
  122. { WM8996_MICBIAS_2, 0x39 },
  123. { WM8996_LDO_1, 0x3 },
  124. { WM8996_LDO_2, 0x13 },
  125. { WM8996_ACCESSORY_DETECT_MODE_1, 0x4 },
  126. { WM8996_ACCESSORY_DETECT_MODE_2, 0x0 },
  127. { WM8996_HEADPHONE_DETECT_1, 0x20 },
  128. { WM8996_HEADPHONE_DETECT_2, 0x0 },
  129. { WM8996_MIC_DETECT_1, 0x7600 },
  130. { WM8996_MIC_DETECT_2, 0xbf },
  131. { WM8996_CHARGE_PUMP_1, 0x1f25 },
  132. { WM8996_CHARGE_PUMP_2, 0xab19 },
  133. { WM8996_DC_SERVO_1, 0x0 },
  134. { WM8996_DC_SERVO_3, 0x0 },
  135. { WM8996_DC_SERVO_5, 0x2a2a },
  136. { WM8996_DC_SERVO_6, 0x0 },
  137. { WM8996_DC_SERVO_7, 0x0 },
  138. { WM8996_ANALOGUE_HP_1, 0x0 },
  139. { WM8996_ANALOGUE_HP_2, 0x0 },
  140. { WM8996_CONTROL_INTERFACE_1, 0x8004 },
  141. { WM8996_WRITE_SEQUENCER_CTRL_1, 0x0 },
  142. { WM8996_WRITE_SEQUENCER_CTRL_2, 0x0 },
  143. { WM8996_AIF_CLOCKING_1, 0x0 },
  144. { WM8996_AIF_CLOCKING_2, 0x0 },
  145. { WM8996_CLOCKING_1, 0x10 },
  146. { WM8996_CLOCKING_2, 0x0 },
  147. { WM8996_AIF_RATE, 0x83 },
  148. { WM8996_FLL_CONTROL_1, 0x0 },
  149. { WM8996_FLL_CONTROL_2, 0x0 },
  150. { WM8996_FLL_CONTROL_3, 0x0 },
  151. { WM8996_FLL_CONTROL_4, 0x5dc0 },
  152. { WM8996_FLL_CONTROL_5, 0xc84 },
  153. { WM8996_FLL_EFS_1, 0x0 },
  154. { WM8996_FLL_EFS_2, 0x2 },
  155. { WM8996_AIF1_CONTROL, 0x0 },
  156. { WM8996_AIF1_BCLK, 0x0 },
  157. { WM8996_AIF1_TX_LRCLK_1, 0x80 },
  158. { WM8996_AIF1_TX_LRCLK_2, 0x8 },
  159. { WM8996_AIF1_RX_LRCLK_1, 0x80 },
  160. { WM8996_AIF1_RX_LRCLK_2, 0x0 },
  161. { WM8996_AIF1TX_DATA_CONFIGURATION_1, 0x1818 },
  162. { WM8996_AIF1TX_DATA_CONFIGURATION_2, 0 },
  163. { WM8996_AIF1RX_DATA_CONFIGURATION, 0x1818 },
  164. { WM8996_AIF1TX_CHANNEL_0_CONFIGURATION, 0x0 },
  165. { WM8996_AIF1TX_CHANNEL_1_CONFIGURATION, 0x0 },
  166. { WM8996_AIF1TX_CHANNEL_2_CONFIGURATION, 0x0 },
  167. { WM8996_AIF1TX_CHANNEL_3_CONFIGURATION, 0x0 },
  168. { WM8996_AIF1TX_CHANNEL_4_CONFIGURATION, 0x0 },
  169. { WM8996_AIF1TX_CHANNEL_5_CONFIGURATION, 0x0 },
  170. { WM8996_AIF1RX_CHANNEL_0_CONFIGURATION, 0x0 },
  171. { WM8996_AIF1RX_CHANNEL_1_CONFIGURATION, 0x0 },
  172. { WM8996_AIF1RX_CHANNEL_2_CONFIGURATION, 0x0 },
  173. { WM8996_AIF1RX_CHANNEL_3_CONFIGURATION, 0x0 },
  174. { WM8996_AIF1RX_CHANNEL_4_CONFIGURATION, 0x0 },
  175. { WM8996_AIF1RX_CHANNEL_5_CONFIGURATION, 0x0 },
  176. { WM8996_AIF1RX_MONO_CONFIGURATION, 0x0 },
  177. { WM8996_AIF1TX_TEST, 0x7 },
  178. { WM8996_AIF2_CONTROL, 0x0 },
  179. { WM8996_AIF2_BCLK, 0x0 },
  180. { WM8996_AIF2_TX_LRCLK_1, 0x80 },
  181. { WM8996_AIF2_TX_LRCLK_2, 0x8 },
  182. { WM8996_AIF2_RX_LRCLK_1, 0x80 },
  183. { WM8996_AIF2_RX_LRCLK_2, 0x0 },
  184. { WM8996_AIF2TX_DATA_CONFIGURATION_1, 0x1818 },
  185. { WM8996_AIF2RX_DATA_CONFIGURATION, 0x1818 },
  186. { WM8996_AIF2RX_DATA_CONFIGURATION, 0x0 },
  187. { WM8996_AIF2TX_CHANNEL_0_CONFIGURATION, 0x0 },
  188. { WM8996_AIF2TX_CHANNEL_1_CONFIGURATION, 0x0 },
  189. { WM8996_AIF2RX_CHANNEL_0_CONFIGURATION, 0x0 },
  190. { WM8996_AIF2RX_CHANNEL_1_CONFIGURATION, 0x0 },
  191. { WM8996_AIF2RX_MONO_CONFIGURATION, 0x0 },
  192. { WM8996_AIF2TX_TEST, 0x1 },
  193. { WM8996_DSP1_TX_LEFT_VOLUME, 0xc0 },
  194. { WM8996_DSP1_TX_RIGHT_VOLUME, 0xc0 },
  195. { WM8996_DSP1_RX_LEFT_VOLUME, 0xc0 },
  196. { WM8996_DSP1_RX_RIGHT_VOLUME, 0xc0 },
  197. { WM8996_DSP1_TX_FILTERS, 0x2000 },
  198. { WM8996_DSP1_RX_FILTERS_1, 0x200 },
  199. { WM8996_DSP1_RX_FILTERS_2, 0x10 },
  200. { WM8996_DSP1_DRC_1, 0x98 },
  201. { WM8996_DSP1_DRC_2, 0x845 },
  202. { WM8996_DSP1_RX_EQ_GAINS_1, 0x6318 },
  203. { WM8996_DSP1_RX_EQ_GAINS_2, 0x6300 },
  204. { WM8996_DSP1_RX_EQ_BAND_1_A, 0xfca },
  205. { WM8996_DSP1_RX_EQ_BAND_1_B, 0x400 },
  206. { WM8996_DSP1_RX_EQ_BAND_1_PG, 0xd8 },
  207. { WM8996_DSP1_RX_EQ_BAND_2_A, 0x1eb5 },
  208. { WM8996_DSP1_RX_EQ_BAND_2_B, 0xf145 },
  209. { WM8996_DSP1_RX_EQ_BAND_2_C, 0xb75 },
  210. { WM8996_DSP1_RX_EQ_BAND_2_PG, 0x1c5 },
  211. { WM8996_DSP1_RX_EQ_BAND_3_A, 0x1c58 },
  212. { WM8996_DSP1_RX_EQ_BAND_3_B, 0xf373 },
  213. { WM8996_DSP1_RX_EQ_BAND_3_C, 0xa54 },
  214. { WM8996_DSP1_RX_EQ_BAND_3_PG, 0x558 },
  215. { WM8996_DSP1_RX_EQ_BAND_4_A, 0x168e },
  216. { WM8996_DSP1_RX_EQ_BAND_4_B, 0xf829 },
  217. { WM8996_DSP1_RX_EQ_BAND_4_C, 0x7ad },
  218. { WM8996_DSP1_RX_EQ_BAND_4_PG, 0x1103 },
  219. { WM8996_DSP1_RX_EQ_BAND_5_A, 0x564 },
  220. { WM8996_DSP1_RX_EQ_BAND_5_B, 0x559 },
  221. { WM8996_DSP1_RX_EQ_BAND_5_PG, 0x4000 },
  222. { WM8996_DSP2_TX_LEFT_VOLUME, 0xc0 },
  223. { WM8996_DSP2_TX_RIGHT_VOLUME, 0xc0 },
  224. { WM8996_DSP2_RX_LEFT_VOLUME, 0xc0 },
  225. { WM8996_DSP2_RX_RIGHT_VOLUME, 0xc0 },
  226. { WM8996_DSP2_TX_FILTERS, 0x2000 },
  227. { WM8996_DSP2_RX_FILTERS_1, 0x200 },
  228. { WM8996_DSP2_RX_FILTERS_2, 0x10 },
  229. { WM8996_DSP2_DRC_1, 0x98 },
  230. { WM8996_DSP2_DRC_2, 0x845 },
  231. { WM8996_DSP2_RX_EQ_GAINS_1, 0x6318 },
  232. { WM8996_DSP2_RX_EQ_GAINS_2, 0x6300 },
  233. { WM8996_DSP2_RX_EQ_BAND_1_A, 0xfca },
  234. { WM8996_DSP2_RX_EQ_BAND_1_B, 0x400 },
  235. { WM8996_DSP2_RX_EQ_BAND_1_PG, 0xd8 },
  236. { WM8996_DSP2_RX_EQ_BAND_2_A, 0x1eb5 },
  237. { WM8996_DSP2_RX_EQ_BAND_2_B, 0xf145 },
  238. { WM8996_DSP2_RX_EQ_BAND_2_C, 0xb75 },
  239. { WM8996_DSP2_RX_EQ_BAND_2_PG, 0x1c5 },
  240. { WM8996_DSP2_RX_EQ_BAND_3_A, 0x1c58 },
  241. { WM8996_DSP2_RX_EQ_BAND_3_B, 0xf373 },
  242. { WM8996_DSP2_RX_EQ_BAND_3_C, 0xa54 },
  243. { WM8996_DSP2_RX_EQ_BAND_3_PG, 0x558 },
  244. { WM8996_DSP2_RX_EQ_BAND_4_A, 0x168e },
  245. { WM8996_DSP2_RX_EQ_BAND_4_B, 0xf829 },
  246. { WM8996_DSP2_RX_EQ_BAND_4_C, 0x7ad },
  247. { WM8996_DSP2_RX_EQ_BAND_4_PG, 0x1103 },
  248. { WM8996_DSP2_RX_EQ_BAND_5_A, 0x564 },
  249. { WM8996_DSP2_RX_EQ_BAND_5_B, 0x559 },
  250. { WM8996_DSP2_RX_EQ_BAND_5_PG, 0x4000 },
  251. { WM8996_DAC1_MIXER_VOLUMES, 0x0 },
  252. { WM8996_DAC1_LEFT_MIXER_ROUTING, 0x0 },
  253. { WM8996_DAC1_RIGHT_MIXER_ROUTING, 0x0 },
  254. { WM8996_DAC2_MIXER_VOLUMES, 0x0 },
  255. { WM8996_DAC2_LEFT_MIXER_ROUTING, 0x0 },
  256. { WM8996_DAC2_RIGHT_MIXER_ROUTING, 0x0 },
  257. { WM8996_DSP1_TX_LEFT_MIXER_ROUTING, 0x0 },
  258. { WM8996_DSP1_TX_RIGHT_MIXER_ROUTING, 0x0 },
  259. { WM8996_DSP2_TX_LEFT_MIXER_ROUTING, 0x0 },
  260. { WM8996_DSP2_TX_RIGHT_MIXER_ROUTING, 0x0 },
  261. { WM8996_DSP_TX_MIXER_SELECT, 0x0 },
  262. { WM8996_DAC_SOFTMUTE, 0x0 },
  263. { WM8996_OVERSAMPLING, 0xd },
  264. { WM8996_SIDETONE, 0x1040 },
  265. { WM8996_GPIO_1, 0xa101 },
  266. { WM8996_GPIO_2, 0xa101 },
  267. { WM8996_GPIO_3, 0xa101 },
  268. { WM8996_GPIO_4, 0xa101 },
  269. { WM8996_GPIO_5, 0xa101 },
  270. { WM8996_PULL_CONTROL_1, 0x0 },
  271. { WM8996_PULL_CONTROL_2, 0x140 },
  272. { WM8996_INTERRUPT_STATUS_1_MASK, 0x1f },
  273. { WM8996_INTERRUPT_STATUS_2_MASK, 0x1ecf },
  274. { WM8996_LEFT_PDM_SPEAKER, 0x0 },
  275. { WM8996_RIGHT_PDM_SPEAKER, 0x1 },
  276. { WM8996_PDM_SPEAKER_MUTE_SEQUENCE, 0x69 },
  277. { WM8996_PDM_SPEAKER_VOLUME, 0x66 },
  278. { WM8996_WRITE_SEQUENCER_0, 0x1 },
  279. { WM8996_WRITE_SEQUENCER_1, 0x1 },
  280. { WM8996_WRITE_SEQUENCER_3, 0x6 },
  281. { WM8996_WRITE_SEQUENCER_4, 0x40 },
  282. { WM8996_WRITE_SEQUENCER_5, 0x1 },
  283. { WM8996_WRITE_SEQUENCER_6, 0xf },
  284. { WM8996_WRITE_SEQUENCER_7, 0x6 },
  285. { WM8996_WRITE_SEQUENCER_8, 0x1 },
  286. { WM8996_WRITE_SEQUENCER_9, 0x3 },
  287. { WM8996_WRITE_SEQUENCER_10, 0x104 },
  288. { WM8996_WRITE_SEQUENCER_12, 0x60 },
  289. { WM8996_WRITE_SEQUENCER_13, 0x11 },
  290. { WM8996_WRITE_SEQUENCER_14, 0x401 },
  291. { WM8996_WRITE_SEQUENCER_16, 0x50 },
  292. { WM8996_WRITE_SEQUENCER_17, 0x3 },
  293. { WM8996_WRITE_SEQUENCER_18, 0x100 },
  294. { WM8996_WRITE_SEQUENCER_20, 0x51 },
  295. { WM8996_WRITE_SEQUENCER_21, 0x3 },
  296. { WM8996_WRITE_SEQUENCER_22, 0x104 },
  297. { WM8996_WRITE_SEQUENCER_23, 0xa },
  298. { WM8996_WRITE_SEQUENCER_24, 0x60 },
  299. { WM8996_WRITE_SEQUENCER_25, 0x3b },
  300. { WM8996_WRITE_SEQUENCER_26, 0x502 },
  301. { WM8996_WRITE_SEQUENCER_27, 0x100 },
  302. { WM8996_WRITE_SEQUENCER_28, 0x2fff },
  303. { WM8996_WRITE_SEQUENCER_32, 0x2fff },
  304. { WM8996_WRITE_SEQUENCER_36, 0x2fff },
  305. { WM8996_WRITE_SEQUENCER_40, 0x2fff },
  306. { WM8996_WRITE_SEQUENCER_44, 0x2fff },
  307. { WM8996_WRITE_SEQUENCER_48, 0x2fff },
  308. { WM8996_WRITE_SEQUENCER_52, 0x2fff },
  309. { WM8996_WRITE_SEQUENCER_56, 0x2fff },
  310. { WM8996_WRITE_SEQUENCER_60, 0x2fff },
  311. { WM8996_WRITE_SEQUENCER_64, 0x1 },
  312. { WM8996_WRITE_SEQUENCER_65, 0x1 },
  313. { WM8996_WRITE_SEQUENCER_67, 0x6 },
  314. { WM8996_WRITE_SEQUENCER_68, 0x40 },
  315. { WM8996_WRITE_SEQUENCER_69, 0x1 },
  316. { WM8996_WRITE_SEQUENCER_70, 0xf },
  317. { WM8996_WRITE_SEQUENCER_71, 0x6 },
  318. { WM8996_WRITE_SEQUENCER_72, 0x1 },
  319. { WM8996_WRITE_SEQUENCER_73, 0x3 },
  320. { WM8996_WRITE_SEQUENCER_74, 0x104 },
  321. { WM8996_WRITE_SEQUENCER_76, 0x60 },
  322. { WM8996_WRITE_SEQUENCER_77, 0x11 },
  323. { WM8996_WRITE_SEQUENCER_78, 0x401 },
  324. { WM8996_WRITE_SEQUENCER_80, 0x50 },
  325. { WM8996_WRITE_SEQUENCER_81, 0x3 },
  326. { WM8996_WRITE_SEQUENCER_82, 0x100 },
  327. { WM8996_WRITE_SEQUENCER_84, 0x60 },
  328. { WM8996_WRITE_SEQUENCER_85, 0x3b },
  329. { WM8996_WRITE_SEQUENCER_86, 0x502 },
  330. { WM8996_WRITE_SEQUENCER_87, 0x100 },
  331. { WM8996_WRITE_SEQUENCER_88, 0x2fff },
  332. { WM8996_WRITE_SEQUENCER_92, 0x2fff },
  333. { WM8996_WRITE_SEQUENCER_96, 0x2fff },
  334. { WM8996_WRITE_SEQUENCER_100, 0x2fff },
  335. { WM8996_WRITE_SEQUENCER_104, 0x2fff },
  336. { WM8996_WRITE_SEQUENCER_108, 0x2fff },
  337. { WM8996_WRITE_SEQUENCER_112, 0x2fff },
  338. { WM8996_WRITE_SEQUENCER_116, 0x2fff },
  339. { WM8996_WRITE_SEQUENCER_120, 0x2fff },
  340. { WM8996_WRITE_SEQUENCER_124, 0x2fff },
  341. { WM8996_WRITE_SEQUENCER_128, 0x1 },
  342. { WM8996_WRITE_SEQUENCER_129, 0x1 },
  343. { WM8996_WRITE_SEQUENCER_131, 0x6 },
  344. { WM8996_WRITE_SEQUENCER_132, 0x40 },
  345. { WM8996_WRITE_SEQUENCER_133, 0x1 },
  346. { WM8996_WRITE_SEQUENCER_134, 0xf },
  347. { WM8996_WRITE_SEQUENCER_135, 0x6 },
  348. { WM8996_WRITE_SEQUENCER_136, 0x1 },
  349. { WM8996_WRITE_SEQUENCER_137, 0x3 },
  350. { WM8996_WRITE_SEQUENCER_138, 0x106 },
  351. { WM8996_WRITE_SEQUENCER_140, 0x61 },
  352. { WM8996_WRITE_SEQUENCER_141, 0x11 },
  353. { WM8996_WRITE_SEQUENCER_142, 0x401 },
  354. { WM8996_WRITE_SEQUENCER_144, 0x50 },
  355. { WM8996_WRITE_SEQUENCER_145, 0x3 },
  356. { WM8996_WRITE_SEQUENCER_146, 0x102 },
  357. { WM8996_WRITE_SEQUENCER_148, 0x51 },
  358. { WM8996_WRITE_SEQUENCER_149, 0x3 },
  359. { WM8996_WRITE_SEQUENCER_150, 0x106 },
  360. { WM8996_WRITE_SEQUENCER_151, 0xa },
  361. { WM8996_WRITE_SEQUENCER_152, 0x61 },
  362. { WM8996_WRITE_SEQUENCER_153, 0x3b },
  363. { WM8996_WRITE_SEQUENCER_154, 0x502 },
  364. { WM8996_WRITE_SEQUENCER_155, 0x100 },
  365. { WM8996_WRITE_SEQUENCER_156, 0x2fff },
  366. { WM8996_WRITE_SEQUENCER_160, 0x2fff },
  367. { WM8996_WRITE_SEQUENCER_164, 0x2fff },
  368. { WM8996_WRITE_SEQUENCER_168, 0x2fff },
  369. { WM8996_WRITE_SEQUENCER_172, 0x2fff },
  370. { WM8996_WRITE_SEQUENCER_176, 0x2fff },
  371. { WM8996_WRITE_SEQUENCER_180, 0x2fff },
  372. { WM8996_WRITE_SEQUENCER_184, 0x2fff },
  373. { WM8996_WRITE_SEQUENCER_188, 0x2fff },
  374. { WM8996_WRITE_SEQUENCER_192, 0x1 },
  375. { WM8996_WRITE_SEQUENCER_193, 0x1 },
  376. { WM8996_WRITE_SEQUENCER_195, 0x6 },
  377. { WM8996_WRITE_SEQUENCER_196, 0x40 },
  378. { WM8996_WRITE_SEQUENCER_197, 0x1 },
  379. { WM8996_WRITE_SEQUENCER_198, 0xf },
  380. { WM8996_WRITE_SEQUENCER_199, 0x6 },
  381. { WM8996_WRITE_SEQUENCER_200, 0x1 },
  382. { WM8996_WRITE_SEQUENCER_201, 0x3 },
  383. { WM8996_WRITE_SEQUENCER_202, 0x106 },
  384. { WM8996_WRITE_SEQUENCER_204, 0x61 },
  385. { WM8996_WRITE_SEQUENCER_205, 0x11 },
  386. { WM8996_WRITE_SEQUENCER_206, 0x401 },
  387. { WM8996_WRITE_SEQUENCER_208, 0x50 },
  388. { WM8996_WRITE_SEQUENCER_209, 0x3 },
  389. { WM8996_WRITE_SEQUENCER_210, 0x102 },
  390. { WM8996_WRITE_SEQUENCER_212, 0x61 },
  391. { WM8996_WRITE_SEQUENCER_213, 0x3b },
  392. { WM8996_WRITE_SEQUENCER_214, 0x502 },
  393. { WM8996_WRITE_SEQUENCER_215, 0x100 },
  394. { WM8996_WRITE_SEQUENCER_216, 0x2fff },
  395. { WM8996_WRITE_SEQUENCER_220, 0x2fff },
  396. { WM8996_WRITE_SEQUENCER_224, 0x2fff },
  397. { WM8996_WRITE_SEQUENCER_228, 0x2fff },
  398. { WM8996_WRITE_SEQUENCER_232, 0x2fff },
  399. { WM8996_WRITE_SEQUENCER_236, 0x2fff },
  400. { WM8996_WRITE_SEQUENCER_240, 0x2fff },
  401. { WM8996_WRITE_SEQUENCER_244, 0x2fff },
  402. { WM8996_WRITE_SEQUENCER_248, 0x2fff },
  403. { WM8996_WRITE_SEQUENCER_252, 0x2fff },
  404. { WM8996_WRITE_SEQUENCER_256, 0x60 },
  405. { WM8996_WRITE_SEQUENCER_258, 0x601 },
  406. { WM8996_WRITE_SEQUENCER_260, 0x50 },
  407. { WM8996_WRITE_SEQUENCER_262, 0x100 },
  408. { WM8996_WRITE_SEQUENCER_264, 0x1 },
  409. { WM8996_WRITE_SEQUENCER_266, 0x104 },
  410. { WM8996_WRITE_SEQUENCER_267, 0x100 },
  411. { WM8996_WRITE_SEQUENCER_268, 0x2fff },
  412. { WM8996_WRITE_SEQUENCER_272, 0x2fff },
  413. { WM8996_WRITE_SEQUENCER_276, 0x2fff },
  414. { WM8996_WRITE_SEQUENCER_280, 0x2fff },
  415. { WM8996_WRITE_SEQUENCER_284, 0x2fff },
  416. { WM8996_WRITE_SEQUENCER_288, 0x2fff },
  417. { WM8996_WRITE_SEQUENCER_292, 0x2fff },
  418. { WM8996_WRITE_SEQUENCER_296, 0x2fff },
  419. { WM8996_WRITE_SEQUENCER_300, 0x2fff },
  420. { WM8996_WRITE_SEQUENCER_304, 0x2fff },
  421. { WM8996_WRITE_SEQUENCER_308, 0x2fff },
  422. { WM8996_WRITE_SEQUENCER_312, 0x2fff },
  423. { WM8996_WRITE_SEQUENCER_316, 0x2fff },
  424. { WM8996_WRITE_SEQUENCER_320, 0x61 },
  425. { WM8996_WRITE_SEQUENCER_322, 0x601 },
  426. { WM8996_WRITE_SEQUENCER_324, 0x50 },
  427. { WM8996_WRITE_SEQUENCER_326, 0x102 },
  428. { WM8996_WRITE_SEQUENCER_328, 0x1 },
  429. { WM8996_WRITE_SEQUENCER_330, 0x106 },
  430. { WM8996_WRITE_SEQUENCER_331, 0x100 },
  431. { WM8996_WRITE_SEQUENCER_332, 0x2fff },
  432. { WM8996_WRITE_SEQUENCER_336, 0x2fff },
  433. { WM8996_WRITE_SEQUENCER_340, 0x2fff },
  434. { WM8996_WRITE_SEQUENCER_344, 0x2fff },
  435. { WM8996_WRITE_SEQUENCER_348, 0x2fff },
  436. { WM8996_WRITE_SEQUENCER_352, 0x2fff },
  437. { WM8996_WRITE_SEQUENCER_356, 0x2fff },
  438. { WM8996_WRITE_SEQUENCER_360, 0x2fff },
  439. { WM8996_WRITE_SEQUENCER_364, 0x2fff },
  440. { WM8996_WRITE_SEQUENCER_368, 0x2fff },
  441. { WM8996_WRITE_SEQUENCER_372, 0x2fff },
  442. { WM8996_WRITE_SEQUENCER_376, 0x2fff },
  443. { WM8996_WRITE_SEQUENCER_380, 0x2fff },
  444. { WM8996_WRITE_SEQUENCER_384, 0x60 },
  445. { WM8996_WRITE_SEQUENCER_386, 0x601 },
  446. { WM8996_WRITE_SEQUENCER_388, 0x61 },
  447. { WM8996_WRITE_SEQUENCER_390, 0x601 },
  448. { WM8996_WRITE_SEQUENCER_392, 0x50 },
  449. { WM8996_WRITE_SEQUENCER_394, 0x300 },
  450. { WM8996_WRITE_SEQUENCER_396, 0x1 },
  451. { WM8996_WRITE_SEQUENCER_398, 0x304 },
  452. { WM8996_WRITE_SEQUENCER_400, 0x40 },
  453. { WM8996_WRITE_SEQUENCER_402, 0xf },
  454. { WM8996_WRITE_SEQUENCER_404, 0x1 },
  455. { WM8996_WRITE_SEQUENCER_407, 0x100 },
  456. };
  457. static const DECLARE_TLV_DB_SCALE(inpga_tlv, 0, 100, 0);
  458. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 150, 0);
  459. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  460. static const DECLARE_TLV_DB_SCALE(out_digital_tlv, -1200, 150, 0);
  461. static const DECLARE_TLV_DB_SCALE(out_tlv, -900, 75, 0);
  462. static const DECLARE_TLV_DB_SCALE(spk_tlv, -900, 150, 0);
  463. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  464. static const DECLARE_TLV_DB_SCALE(threedstereo_tlv, -1600, 183, 1);
  465. static const char *sidetone_hpf_text[] = {
  466. "2.9kHz", "1.5kHz", "735Hz", "403Hz", "196Hz", "98Hz", "49Hz"
  467. };
  468. static const struct soc_enum sidetone_hpf =
  469. SOC_ENUM_SINGLE(WM8996_SIDETONE, 7, 7, sidetone_hpf_text);
  470. static const char *hpf_mode_text[] = {
  471. "HiFi", "Custom", "Voice"
  472. };
  473. static const struct soc_enum dsp1tx_hpf_mode =
  474. SOC_ENUM_SINGLE(WM8996_DSP1_TX_FILTERS, 3, 3, hpf_mode_text);
  475. static const struct soc_enum dsp2tx_hpf_mode =
  476. SOC_ENUM_SINGLE(WM8996_DSP2_TX_FILTERS, 3, 3, hpf_mode_text);
  477. static const char *hpf_cutoff_text[] = {
  478. "50Hz", "75Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  479. };
  480. static const struct soc_enum dsp1tx_hpf_cutoff =
  481. SOC_ENUM_SINGLE(WM8996_DSP1_TX_FILTERS, 0, 7, hpf_cutoff_text);
  482. static const struct soc_enum dsp2tx_hpf_cutoff =
  483. SOC_ENUM_SINGLE(WM8996_DSP2_TX_FILTERS, 0, 7, hpf_cutoff_text);
  484. static void wm8996_set_retune_mobile(struct snd_soc_codec *codec, int block)
  485. {
  486. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  487. struct wm8996_pdata *pdata = &wm8996->pdata;
  488. int base, best, best_val, save, i, cfg, iface;
  489. if (!wm8996->num_retune_mobile_texts)
  490. return;
  491. switch (block) {
  492. case 0:
  493. base = WM8996_DSP1_RX_EQ_GAINS_1;
  494. if (snd_soc_read(codec, WM8996_POWER_MANAGEMENT_8) &
  495. WM8996_DSP1RX_SRC)
  496. iface = 1;
  497. else
  498. iface = 0;
  499. break;
  500. case 1:
  501. base = WM8996_DSP1_RX_EQ_GAINS_2;
  502. if (snd_soc_read(codec, WM8996_POWER_MANAGEMENT_8) &
  503. WM8996_DSP2RX_SRC)
  504. iface = 1;
  505. else
  506. iface = 0;
  507. break;
  508. default:
  509. return;
  510. }
  511. /* Find the version of the currently selected configuration
  512. * with the nearest sample rate. */
  513. cfg = wm8996->retune_mobile_cfg[block];
  514. best = 0;
  515. best_val = INT_MAX;
  516. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  517. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  518. wm8996->retune_mobile_texts[cfg]) == 0 &&
  519. abs(pdata->retune_mobile_cfgs[i].rate
  520. - wm8996->rx_rate[iface]) < best_val) {
  521. best = i;
  522. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  523. - wm8996->rx_rate[iface]);
  524. }
  525. }
  526. dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
  527. block,
  528. pdata->retune_mobile_cfgs[best].name,
  529. pdata->retune_mobile_cfgs[best].rate,
  530. wm8996->rx_rate[iface]);
  531. /* The EQ will be disabled while reconfiguring it, remember the
  532. * current configuration.
  533. */
  534. save = snd_soc_read(codec, base);
  535. save &= WM8996_DSP1RX_EQ_ENA;
  536. for (i = 0; i < ARRAY_SIZE(pdata->retune_mobile_cfgs[best].regs); i++)
  537. snd_soc_update_bits(codec, base + i, 0xffff,
  538. pdata->retune_mobile_cfgs[best].regs[i]);
  539. snd_soc_update_bits(codec, base, WM8996_DSP1RX_EQ_ENA, save);
  540. }
  541. /* Icky as hell but saves code duplication */
  542. static int wm8996_get_retune_mobile_block(const char *name)
  543. {
  544. if (strcmp(name, "DSP1 EQ Mode") == 0)
  545. return 0;
  546. if (strcmp(name, "DSP2 EQ Mode") == 0)
  547. return 1;
  548. return -EINVAL;
  549. }
  550. static int wm8996_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  551. struct snd_ctl_elem_value *ucontrol)
  552. {
  553. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  554. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  555. struct wm8996_pdata *pdata = &wm8996->pdata;
  556. int block = wm8996_get_retune_mobile_block(kcontrol->id.name);
  557. int value = ucontrol->value.integer.value[0];
  558. if (block < 0)
  559. return block;
  560. if (value >= pdata->num_retune_mobile_cfgs)
  561. return -EINVAL;
  562. wm8996->retune_mobile_cfg[block] = value;
  563. wm8996_set_retune_mobile(codec, block);
  564. return 0;
  565. }
  566. static int wm8996_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  567. struct snd_ctl_elem_value *ucontrol)
  568. {
  569. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  570. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  571. int block = wm8996_get_retune_mobile_block(kcontrol->id.name);
  572. ucontrol->value.enumerated.item[0] = wm8996->retune_mobile_cfg[block];
  573. return 0;
  574. }
  575. static const struct snd_kcontrol_new wm8996_snd_controls[] = {
  576. SOC_DOUBLE_R_TLV("Capture Volume", WM8996_LEFT_LINE_INPUT_VOLUME,
  577. WM8996_RIGHT_LINE_INPUT_VOLUME, 0, 31, 0, inpga_tlv),
  578. SOC_DOUBLE_R("Capture ZC Switch", WM8996_LEFT_LINE_INPUT_VOLUME,
  579. WM8996_RIGHT_LINE_INPUT_VOLUME, 5, 1, 0),
  580. SOC_DOUBLE_TLV("DAC1 Sidetone Volume", WM8996_DAC1_MIXER_VOLUMES,
  581. 0, 5, 24, 0, sidetone_tlv),
  582. SOC_DOUBLE_TLV("DAC2 Sidetone Volume", WM8996_DAC2_MIXER_VOLUMES,
  583. 0, 5, 24, 0, sidetone_tlv),
  584. SOC_SINGLE("Sidetone LPF Switch", WM8996_SIDETONE, 12, 1, 0),
  585. SOC_ENUM("Sidetone HPF Cut-off", sidetone_hpf),
  586. SOC_SINGLE("Sidetone HPF Switch", WM8996_SIDETONE, 6, 1, 0),
  587. SOC_DOUBLE_R_TLV("DSP1 Capture Volume", WM8996_DSP1_TX_LEFT_VOLUME,
  588. WM8996_DSP1_TX_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  589. SOC_DOUBLE_R_TLV("DSP2 Capture Volume", WM8996_DSP2_TX_LEFT_VOLUME,
  590. WM8996_DSP2_TX_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  591. SOC_SINGLE("DSP1 Capture Notch Filter Switch", WM8996_DSP1_TX_FILTERS,
  592. 13, 1, 0),
  593. SOC_DOUBLE("DSP1 Capture HPF Switch", WM8996_DSP1_TX_FILTERS, 12, 11, 1, 0),
  594. SOC_ENUM("DSP1 Capture HPF Mode", dsp1tx_hpf_mode),
  595. SOC_ENUM("DSP1 Capture HPF Cutoff", dsp1tx_hpf_cutoff),
  596. SOC_SINGLE("DSP2 Capture Notch Filter Switch", WM8996_DSP2_TX_FILTERS,
  597. 13, 1, 0),
  598. SOC_DOUBLE("DSP2 Capture HPF Switch", WM8996_DSP2_TX_FILTERS, 12, 11, 1, 0),
  599. SOC_ENUM("DSP2 Capture HPF Mode", dsp2tx_hpf_mode),
  600. SOC_ENUM("DSP2 Capture HPF Cutoff", dsp2tx_hpf_cutoff),
  601. SOC_DOUBLE_R_TLV("DSP1 Playback Volume", WM8996_DSP1_RX_LEFT_VOLUME,
  602. WM8996_DSP1_RX_RIGHT_VOLUME, 1, 112, 0, digital_tlv),
  603. SOC_SINGLE("DSP1 Playback Switch", WM8996_DSP1_RX_FILTERS_1, 9, 1, 1),
  604. SOC_DOUBLE_R_TLV("DSP2 Playback Volume", WM8996_DSP2_RX_LEFT_VOLUME,
  605. WM8996_DSP2_RX_RIGHT_VOLUME, 1, 112, 0, digital_tlv),
  606. SOC_SINGLE("DSP2 Playback Switch", WM8996_DSP2_RX_FILTERS_1, 9, 1, 1),
  607. SOC_DOUBLE_R_TLV("DAC1 Volume", WM8996_DAC1_LEFT_VOLUME,
  608. WM8996_DAC1_RIGHT_VOLUME, 1, 112, 0, digital_tlv),
  609. SOC_DOUBLE_R("DAC1 Switch", WM8996_DAC1_LEFT_VOLUME,
  610. WM8996_DAC1_RIGHT_VOLUME, 9, 1, 1),
  611. SOC_DOUBLE_R_TLV("DAC2 Volume", WM8996_DAC2_LEFT_VOLUME,
  612. WM8996_DAC2_RIGHT_VOLUME, 1, 112, 0, digital_tlv),
  613. SOC_DOUBLE_R("DAC2 Switch", WM8996_DAC2_LEFT_VOLUME,
  614. WM8996_DAC2_RIGHT_VOLUME, 9, 1, 1),
  615. SOC_SINGLE("Speaker High Performance Switch", WM8996_OVERSAMPLING, 3, 1, 0),
  616. SOC_SINGLE("DMIC High Performance Switch", WM8996_OVERSAMPLING, 2, 1, 0),
  617. SOC_SINGLE("ADC High Performance Switch", WM8996_OVERSAMPLING, 1, 1, 0),
  618. SOC_SINGLE("DAC High Performance Switch", WM8996_OVERSAMPLING, 0, 1, 0),
  619. SOC_SINGLE("DAC Soft Mute Switch", WM8996_DAC_SOFTMUTE, 1, 1, 0),
  620. SOC_SINGLE("DAC Slow Soft Mute Switch", WM8996_DAC_SOFTMUTE, 0, 1, 0),
  621. SOC_SINGLE("DSP1 3D Stereo Switch", WM8996_DSP1_RX_FILTERS_2, 8, 1, 0),
  622. SOC_SINGLE("DSP2 3D Stereo Switch", WM8996_DSP2_RX_FILTERS_2, 8, 1, 0),
  623. SOC_SINGLE_TLV("DSP1 3D Stereo Volume", WM8996_DSP1_RX_FILTERS_2, 10, 15,
  624. 0, threedstereo_tlv),
  625. SOC_SINGLE_TLV("DSP2 3D Stereo Volume", WM8996_DSP2_RX_FILTERS_2, 10, 15,
  626. 0, threedstereo_tlv),
  627. SOC_DOUBLE_TLV("Digital Output 1 Volume", WM8996_DAC1_HPOUT1_VOLUME, 0, 4,
  628. 8, 0, out_digital_tlv),
  629. SOC_DOUBLE_TLV("Digital Output 2 Volume", WM8996_DAC2_HPOUT2_VOLUME, 0, 4,
  630. 8, 0, out_digital_tlv),
  631. SOC_DOUBLE_R_TLV("Output 1 Volume", WM8996_OUTPUT1_LEFT_VOLUME,
  632. WM8996_OUTPUT1_RIGHT_VOLUME, 0, 12, 0, out_tlv),
  633. SOC_DOUBLE_R("Output 1 ZC Switch", WM8996_OUTPUT1_LEFT_VOLUME,
  634. WM8996_OUTPUT1_RIGHT_VOLUME, 7, 1, 0),
  635. SOC_DOUBLE_R_TLV("Output 2 Volume", WM8996_OUTPUT2_LEFT_VOLUME,
  636. WM8996_OUTPUT2_RIGHT_VOLUME, 0, 12, 0, out_tlv),
  637. SOC_DOUBLE_R("Output 2 ZC Switch", WM8996_OUTPUT2_LEFT_VOLUME,
  638. WM8996_OUTPUT2_RIGHT_VOLUME, 7, 1, 0),
  639. SOC_DOUBLE_TLV("Speaker Volume", WM8996_PDM_SPEAKER_VOLUME, 0, 4, 8, 0,
  640. spk_tlv),
  641. SOC_DOUBLE_R("Speaker Switch", WM8996_LEFT_PDM_SPEAKER,
  642. WM8996_RIGHT_PDM_SPEAKER, 3, 1, 1),
  643. SOC_DOUBLE_R("Speaker ZC Switch", WM8996_LEFT_PDM_SPEAKER,
  644. WM8996_RIGHT_PDM_SPEAKER, 2, 1, 0),
  645. SOC_SINGLE("DSP1 EQ Switch", WM8996_DSP1_RX_EQ_GAINS_1, 0, 1, 0),
  646. SOC_SINGLE("DSP2 EQ Switch", WM8996_DSP2_RX_EQ_GAINS_1, 0, 1, 0),
  647. SOC_SINGLE("DSP1 DRC TXL Switch", WM8996_DSP1_DRC_1, 0, 1, 0),
  648. SOC_SINGLE("DSP1 DRC TXR Switch", WM8996_DSP1_DRC_1, 1, 1, 0),
  649. SOC_SINGLE("DSP1 DRC RX Switch", WM8996_DSP1_DRC_1, 2, 1, 0),
  650. SND_SOC_BYTES_MASK("DSP1 DRC", WM8996_DSP1_DRC_1, 5,
  651. WM8996_DSP1RX_DRC_ENA | WM8996_DSP1TXL_DRC_ENA |
  652. WM8996_DSP1TXR_DRC_ENA),
  653. SOC_SINGLE("DSP2 DRC TXL Switch", WM8996_DSP2_DRC_1, 0, 1, 0),
  654. SOC_SINGLE("DSP2 DRC TXR Switch", WM8996_DSP2_DRC_1, 1, 1, 0),
  655. SOC_SINGLE("DSP2 DRC RX Switch", WM8996_DSP2_DRC_1, 2, 1, 0),
  656. SND_SOC_BYTES_MASK("DSP2 DRC", WM8996_DSP2_DRC_1, 5,
  657. WM8996_DSP2RX_DRC_ENA | WM8996_DSP2TXL_DRC_ENA |
  658. WM8996_DSP2TXR_DRC_ENA),
  659. };
  660. static const struct snd_kcontrol_new wm8996_eq_controls[] = {
  661. SOC_SINGLE_TLV("DSP1 EQ B1 Volume", WM8996_DSP1_RX_EQ_GAINS_1, 11, 31, 0,
  662. eq_tlv),
  663. SOC_SINGLE_TLV("DSP1 EQ B2 Volume", WM8996_DSP1_RX_EQ_GAINS_1, 6, 31, 0,
  664. eq_tlv),
  665. SOC_SINGLE_TLV("DSP1 EQ B3 Volume", WM8996_DSP1_RX_EQ_GAINS_1, 1, 31, 0,
  666. eq_tlv),
  667. SOC_SINGLE_TLV("DSP1 EQ B4 Volume", WM8996_DSP1_RX_EQ_GAINS_2, 11, 31, 0,
  668. eq_tlv),
  669. SOC_SINGLE_TLV("DSP1 EQ B5 Volume", WM8996_DSP1_RX_EQ_GAINS_2, 6, 31, 0,
  670. eq_tlv),
  671. SOC_SINGLE_TLV("DSP2 EQ B1 Volume", WM8996_DSP2_RX_EQ_GAINS_1, 11, 31, 0,
  672. eq_tlv),
  673. SOC_SINGLE_TLV("DSP2 EQ B2 Volume", WM8996_DSP2_RX_EQ_GAINS_1, 6, 31, 0,
  674. eq_tlv),
  675. SOC_SINGLE_TLV("DSP2 EQ B3 Volume", WM8996_DSP2_RX_EQ_GAINS_1, 1, 31, 0,
  676. eq_tlv),
  677. SOC_SINGLE_TLV("DSP2 EQ B4 Volume", WM8996_DSP2_RX_EQ_GAINS_2, 11, 31, 0,
  678. eq_tlv),
  679. SOC_SINGLE_TLV("DSP2 EQ B5 Volume", WM8996_DSP2_RX_EQ_GAINS_2, 6, 31, 0,
  680. eq_tlv),
  681. };
  682. static void wm8996_bg_enable(struct snd_soc_codec *codec)
  683. {
  684. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  685. wm8996->bg_ena++;
  686. if (wm8996->bg_ena == 1) {
  687. snd_soc_update_bits(codec, WM8996_POWER_MANAGEMENT_1,
  688. WM8996_BG_ENA, WM8996_BG_ENA);
  689. msleep(2);
  690. }
  691. }
  692. static void wm8996_bg_disable(struct snd_soc_codec *codec)
  693. {
  694. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  695. wm8996->bg_ena--;
  696. if (!wm8996->bg_ena)
  697. snd_soc_update_bits(codec, WM8996_POWER_MANAGEMENT_1,
  698. WM8996_BG_ENA, 0);
  699. }
  700. static int bg_event(struct snd_soc_dapm_widget *w,
  701. struct snd_kcontrol *kcontrol, int event)
  702. {
  703. struct snd_soc_codec *codec = w->codec;
  704. int ret = 0;
  705. switch (event) {
  706. case SND_SOC_DAPM_PRE_PMU:
  707. wm8996_bg_enable(codec);
  708. break;
  709. case SND_SOC_DAPM_POST_PMD:
  710. wm8996_bg_disable(codec);
  711. break;
  712. default:
  713. BUG();
  714. ret = -EINVAL;
  715. }
  716. return ret;
  717. }
  718. static int cp_event(struct snd_soc_dapm_widget *w,
  719. struct snd_kcontrol *kcontrol, int event)
  720. {
  721. int ret = 0;
  722. switch (event) {
  723. case SND_SOC_DAPM_POST_PMU:
  724. msleep(5);
  725. break;
  726. default:
  727. BUG();
  728. ret = -EINVAL;
  729. }
  730. return 0;
  731. }
  732. static int rmv_short_event(struct snd_soc_dapm_widget *w,
  733. struct snd_kcontrol *kcontrol, int event)
  734. {
  735. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(w->codec);
  736. /* Record which outputs we enabled */
  737. switch (event) {
  738. case SND_SOC_DAPM_PRE_PMD:
  739. wm8996->hpout_pending &= ~w->shift;
  740. break;
  741. case SND_SOC_DAPM_PRE_PMU:
  742. wm8996->hpout_pending |= w->shift;
  743. break;
  744. default:
  745. BUG();
  746. return -EINVAL;
  747. }
  748. return 0;
  749. }
  750. static void wait_for_dc_servo(struct snd_soc_codec *codec, u16 mask)
  751. {
  752. struct i2c_client *i2c = to_i2c_client(codec->dev);
  753. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  754. int ret;
  755. unsigned long timeout = 200;
  756. snd_soc_write(codec, WM8996_DC_SERVO_2, mask);
  757. /* Use the interrupt if possible */
  758. do {
  759. if (i2c->irq) {
  760. timeout = wait_for_completion_timeout(&wm8996->dcs_done,
  761. msecs_to_jiffies(200));
  762. if (timeout == 0)
  763. dev_err(codec->dev, "DC servo timed out\n");
  764. } else {
  765. msleep(1);
  766. timeout--;
  767. }
  768. ret = snd_soc_read(codec, WM8996_DC_SERVO_2);
  769. dev_dbg(codec->dev, "DC servo state: %x\n", ret);
  770. } while (timeout && ret & mask);
  771. if (timeout == 0)
  772. dev_err(codec->dev, "DC servo timed out for %x\n", mask);
  773. else
  774. dev_dbg(codec->dev, "DC servo complete for %x\n", mask);
  775. }
  776. static void wm8996_seq_notifier(struct snd_soc_dapm_context *dapm,
  777. enum snd_soc_dapm_type event, int subseq)
  778. {
  779. struct snd_soc_codec *codec = container_of(dapm,
  780. struct snd_soc_codec, dapm);
  781. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  782. u16 val, mask;
  783. /* Complete any pending DC servo starts */
  784. if (wm8996->dcs_pending) {
  785. dev_dbg(codec->dev, "Starting DC servo for %x\n",
  786. wm8996->dcs_pending);
  787. /* Trigger a startup sequence */
  788. wait_for_dc_servo(codec, wm8996->dcs_pending
  789. << WM8996_DCS_TRIG_STARTUP_0_SHIFT);
  790. wm8996->dcs_pending = 0;
  791. }
  792. if (wm8996->hpout_pending != wm8996->hpout_ena) {
  793. dev_dbg(codec->dev, "Applying RMV_SHORTs %x->%x\n",
  794. wm8996->hpout_ena, wm8996->hpout_pending);
  795. val = 0;
  796. mask = 0;
  797. if (wm8996->hpout_pending & HPOUT1L) {
  798. val |= WM8996_HPOUT1L_RMV_SHORT | WM8996_HPOUT1L_OUTP;
  799. mask |= WM8996_HPOUT1L_RMV_SHORT | WM8996_HPOUT1L_OUTP;
  800. } else {
  801. mask |= WM8996_HPOUT1L_RMV_SHORT |
  802. WM8996_HPOUT1L_OUTP |
  803. WM8996_HPOUT1L_DLY;
  804. }
  805. if (wm8996->hpout_pending & HPOUT1R) {
  806. val |= WM8996_HPOUT1R_RMV_SHORT | WM8996_HPOUT1R_OUTP;
  807. mask |= WM8996_HPOUT1R_RMV_SHORT | WM8996_HPOUT1R_OUTP;
  808. } else {
  809. mask |= WM8996_HPOUT1R_RMV_SHORT |
  810. WM8996_HPOUT1R_OUTP |
  811. WM8996_HPOUT1R_DLY;
  812. }
  813. snd_soc_update_bits(codec, WM8996_ANALOGUE_HP_1, mask, val);
  814. val = 0;
  815. mask = 0;
  816. if (wm8996->hpout_pending & HPOUT2L) {
  817. val |= WM8996_HPOUT2L_RMV_SHORT | WM8996_HPOUT2L_OUTP;
  818. mask |= WM8996_HPOUT2L_RMV_SHORT | WM8996_HPOUT2L_OUTP;
  819. } else {
  820. mask |= WM8996_HPOUT2L_RMV_SHORT |
  821. WM8996_HPOUT2L_OUTP |
  822. WM8996_HPOUT2L_DLY;
  823. }
  824. if (wm8996->hpout_pending & HPOUT2R) {
  825. val |= WM8996_HPOUT2R_RMV_SHORT | WM8996_HPOUT2R_OUTP;
  826. mask |= WM8996_HPOUT2R_RMV_SHORT | WM8996_HPOUT2R_OUTP;
  827. } else {
  828. mask |= WM8996_HPOUT2R_RMV_SHORT |
  829. WM8996_HPOUT2R_OUTP |
  830. WM8996_HPOUT2R_DLY;
  831. }
  832. snd_soc_update_bits(codec, WM8996_ANALOGUE_HP_2, mask, val);
  833. wm8996->hpout_ena = wm8996->hpout_pending;
  834. }
  835. }
  836. static int dcs_start(struct snd_soc_dapm_widget *w,
  837. struct snd_kcontrol *kcontrol, int event)
  838. {
  839. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(w->codec);
  840. switch (event) {
  841. case SND_SOC_DAPM_POST_PMU:
  842. wm8996->dcs_pending |= 1 << w->shift;
  843. break;
  844. default:
  845. BUG();
  846. return -EINVAL;
  847. }
  848. return 0;
  849. }
  850. static const char *sidetone_text[] = {
  851. "IN1", "IN2",
  852. };
  853. static const struct soc_enum left_sidetone_enum =
  854. SOC_ENUM_SINGLE(WM8996_SIDETONE, 0, 2, sidetone_text);
  855. static const struct snd_kcontrol_new left_sidetone =
  856. SOC_DAPM_ENUM("Left Sidetone", left_sidetone_enum);
  857. static const struct soc_enum right_sidetone_enum =
  858. SOC_ENUM_SINGLE(WM8996_SIDETONE, 1, 2, sidetone_text);
  859. static const struct snd_kcontrol_new right_sidetone =
  860. SOC_DAPM_ENUM("Right Sidetone", right_sidetone_enum);
  861. static const char *spk_text[] = {
  862. "DAC1L", "DAC1R", "DAC2L", "DAC2R"
  863. };
  864. static const struct soc_enum spkl_enum =
  865. SOC_ENUM_SINGLE(WM8996_LEFT_PDM_SPEAKER, 0, 4, spk_text);
  866. static const struct snd_kcontrol_new spkl_mux =
  867. SOC_DAPM_ENUM("SPKL", spkl_enum);
  868. static const struct soc_enum spkr_enum =
  869. SOC_ENUM_SINGLE(WM8996_RIGHT_PDM_SPEAKER, 0, 4, spk_text);
  870. static const struct snd_kcontrol_new spkr_mux =
  871. SOC_DAPM_ENUM("SPKR", spkr_enum);
  872. static const char *dsp1rx_text[] = {
  873. "AIF1", "AIF2"
  874. };
  875. static const struct soc_enum dsp1rx_enum =
  876. SOC_ENUM_SINGLE(WM8996_POWER_MANAGEMENT_8, 0, 2, dsp1rx_text);
  877. static const struct snd_kcontrol_new dsp1rx =
  878. SOC_DAPM_ENUM("DSP1RX", dsp1rx_enum);
  879. static const char *dsp2rx_text[] = {
  880. "AIF2", "AIF1"
  881. };
  882. static const struct soc_enum dsp2rx_enum =
  883. SOC_ENUM_SINGLE(WM8996_POWER_MANAGEMENT_8, 4, 2, dsp2rx_text);
  884. static const struct snd_kcontrol_new dsp2rx =
  885. SOC_DAPM_ENUM("DSP2RX", dsp2rx_enum);
  886. static const char *aif2tx_text[] = {
  887. "DSP2", "DSP1", "AIF1"
  888. };
  889. static const struct soc_enum aif2tx_enum =
  890. SOC_ENUM_SINGLE(WM8996_POWER_MANAGEMENT_8, 6, 3, aif2tx_text);
  891. static const struct snd_kcontrol_new aif2tx =
  892. SOC_DAPM_ENUM("AIF2TX", aif2tx_enum);
  893. static const char *inmux_text[] = {
  894. "ADC", "DMIC1", "DMIC2"
  895. };
  896. static const struct soc_enum in1_enum =
  897. SOC_ENUM_SINGLE(WM8996_POWER_MANAGEMENT_7, 0, 3, inmux_text);
  898. static const struct snd_kcontrol_new in1_mux =
  899. SOC_DAPM_ENUM("IN1 Mux", in1_enum);
  900. static const struct soc_enum in2_enum =
  901. SOC_ENUM_SINGLE(WM8996_POWER_MANAGEMENT_7, 4, 3, inmux_text);
  902. static const struct snd_kcontrol_new in2_mux =
  903. SOC_DAPM_ENUM("IN2 Mux", in2_enum);
  904. static const struct snd_kcontrol_new dac2r_mix[] = {
  905. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8996_DAC2_RIGHT_MIXER_ROUTING,
  906. 5, 1, 0),
  907. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8996_DAC2_RIGHT_MIXER_ROUTING,
  908. 4, 1, 0),
  909. SOC_DAPM_SINGLE("DSP2 Switch", WM8996_DAC2_RIGHT_MIXER_ROUTING, 1, 1, 0),
  910. SOC_DAPM_SINGLE("DSP1 Switch", WM8996_DAC2_RIGHT_MIXER_ROUTING, 0, 1, 0),
  911. };
  912. static const struct snd_kcontrol_new dac2l_mix[] = {
  913. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8996_DAC2_LEFT_MIXER_ROUTING,
  914. 5, 1, 0),
  915. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8996_DAC2_LEFT_MIXER_ROUTING,
  916. 4, 1, 0),
  917. SOC_DAPM_SINGLE("DSP2 Switch", WM8996_DAC2_LEFT_MIXER_ROUTING, 1, 1, 0),
  918. SOC_DAPM_SINGLE("DSP1 Switch", WM8996_DAC2_LEFT_MIXER_ROUTING, 0, 1, 0),
  919. };
  920. static const struct snd_kcontrol_new dac1r_mix[] = {
  921. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8996_DAC1_RIGHT_MIXER_ROUTING,
  922. 5, 1, 0),
  923. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8996_DAC1_RIGHT_MIXER_ROUTING,
  924. 4, 1, 0),
  925. SOC_DAPM_SINGLE("DSP2 Switch", WM8996_DAC1_RIGHT_MIXER_ROUTING, 1, 1, 0),
  926. SOC_DAPM_SINGLE("DSP1 Switch", WM8996_DAC1_RIGHT_MIXER_ROUTING, 0, 1, 0),
  927. };
  928. static const struct snd_kcontrol_new dac1l_mix[] = {
  929. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8996_DAC1_LEFT_MIXER_ROUTING,
  930. 5, 1, 0),
  931. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8996_DAC1_LEFT_MIXER_ROUTING,
  932. 4, 1, 0),
  933. SOC_DAPM_SINGLE("DSP2 Switch", WM8996_DAC1_LEFT_MIXER_ROUTING, 1, 1, 0),
  934. SOC_DAPM_SINGLE("DSP1 Switch", WM8996_DAC1_LEFT_MIXER_ROUTING, 0, 1, 0),
  935. };
  936. static const struct snd_kcontrol_new dsp1txl[] = {
  937. SOC_DAPM_SINGLE("IN1 Switch", WM8996_DSP1_TX_LEFT_MIXER_ROUTING,
  938. 1, 1, 0),
  939. SOC_DAPM_SINGLE("DAC Switch", WM8996_DSP1_TX_LEFT_MIXER_ROUTING,
  940. 0, 1, 0),
  941. };
  942. static const struct snd_kcontrol_new dsp1txr[] = {
  943. SOC_DAPM_SINGLE("IN1 Switch", WM8996_DSP1_TX_RIGHT_MIXER_ROUTING,
  944. 1, 1, 0),
  945. SOC_DAPM_SINGLE("DAC Switch", WM8996_DSP1_TX_RIGHT_MIXER_ROUTING,
  946. 0, 1, 0),
  947. };
  948. static const struct snd_kcontrol_new dsp2txl[] = {
  949. SOC_DAPM_SINGLE("IN1 Switch", WM8996_DSP2_TX_LEFT_MIXER_ROUTING,
  950. 1, 1, 0),
  951. SOC_DAPM_SINGLE("DAC Switch", WM8996_DSP2_TX_LEFT_MIXER_ROUTING,
  952. 0, 1, 0),
  953. };
  954. static const struct snd_kcontrol_new dsp2txr[] = {
  955. SOC_DAPM_SINGLE("IN1 Switch", WM8996_DSP2_TX_RIGHT_MIXER_ROUTING,
  956. 1, 1, 0),
  957. SOC_DAPM_SINGLE("DAC Switch", WM8996_DSP2_TX_RIGHT_MIXER_ROUTING,
  958. 0, 1, 0),
  959. };
  960. static const struct snd_soc_dapm_widget wm8996_dapm_widgets[] = {
  961. SND_SOC_DAPM_INPUT("IN1LN"),
  962. SND_SOC_DAPM_INPUT("IN1LP"),
  963. SND_SOC_DAPM_INPUT("IN1RN"),
  964. SND_SOC_DAPM_INPUT("IN1RP"),
  965. SND_SOC_DAPM_INPUT("IN2LN"),
  966. SND_SOC_DAPM_INPUT("IN2LP"),
  967. SND_SOC_DAPM_INPUT("IN2RN"),
  968. SND_SOC_DAPM_INPUT("IN2RP"),
  969. SND_SOC_DAPM_INPUT("DMIC1DAT"),
  970. SND_SOC_DAPM_INPUT("DMIC2DAT"),
  971. SND_SOC_DAPM_REGULATOR_SUPPLY("CPVDD", 20),
  972. SND_SOC_DAPM_SUPPLY_S("SYSCLK", 1, WM8996_AIF_CLOCKING_1, 0, 0, NULL, 0),
  973. SND_SOC_DAPM_SUPPLY_S("SYSDSPCLK", 2, WM8996_CLOCKING_1, 1, 0, NULL, 0),
  974. SND_SOC_DAPM_SUPPLY_S("AIFCLK", 2, WM8996_CLOCKING_1, 2, 0, NULL, 0),
  975. SND_SOC_DAPM_SUPPLY_S("Charge Pump", 2, WM8996_CHARGE_PUMP_1, 15, 0, cp_event,
  976. SND_SOC_DAPM_POST_PMU),
  977. SND_SOC_DAPM_SUPPLY("Bandgap", SND_SOC_NOPM, 0, 0, bg_event,
  978. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  979. SND_SOC_DAPM_SUPPLY("LDO2", WM8996_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
  980. SND_SOC_DAPM_SUPPLY("MICB1 Audio", WM8996_MICBIAS_1, 4, 1, NULL, 0),
  981. SND_SOC_DAPM_SUPPLY("MICB2 Audio", WM8996_MICBIAS_2, 4, 1, NULL, 0),
  982. SND_SOC_DAPM_MICBIAS("MICB2", WM8996_POWER_MANAGEMENT_1, 9, 0),
  983. SND_SOC_DAPM_MICBIAS("MICB1", WM8996_POWER_MANAGEMENT_1, 8, 0),
  984. SND_SOC_DAPM_PGA("IN1L PGA", WM8996_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  985. SND_SOC_DAPM_PGA("IN1R PGA", WM8996_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  986. SND_SOC_DAPM_MUX("IN1L Mux", WM8996_POWER_MANAGEMENT_7, 2, 0, &in1_mux),
  987. SND_SOC_DAPM_MUX("IN1R Mux", WM8996_POWER_MANAGEMENT_7, 3, 0, &in1_mux),
  988. SND_SOC_DAPM_MUX("IN2L Mux", WM8996_POWER_MANAGEMENT_7, 6, 0, &in2_mux),
  989. SND_SOC_DAPM_MUX("IN2R Mux", WM8996_POWER_MANAGEMENT_7, 7, 0, &in2_mux),
  990. SND_SOC_DAPM_SUPPLY("DMIC2", WM8996_POWER_MANAGEMENT_7, 9, 0, NULL, 0),
  991. SND_SOC_DAPM_SUPPLY("DMIC1", WM8996_POWER_MANAGEMENT_7, 8, 0, NULL, 0),
  992. SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8996_POWER_MANAGEMENT_3, 5, 0),
  993. SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8996_POWER_MANAGEMENT_3, 4, 0),
  994. SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8996_POWER_MANAGEMENT_3, 3, 0),
  995. SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8996_POWER_MANAGEMENT_3, 2, 0),
  996. SND_SOC_DAPM_ADC("ADCL", NULL, WM8996_POWER_MANAGEMENT_3, 1, 0),
  997. SND_SOC_DAPM_ADC("ADCR", NULL, WM8996_POWER_MANAGEMENT_3, 0, 0),
  998. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &left_sidetone),
  999. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &right_sidetone),
  1000. SND_SOC_DAPM_AIF_IN("DSP2RXL", NULL, 0, WM8996_POWER_MANAGEMENT_3, 11, 0),
  1001. SND_SOC_DAPM_AIF_IN("DSP2RXR", NULL, 1, WM8996_POWER_MANAGEMENT_3, 10, 0),
  1002. SND_SOC_DAPM_AIF_IN("DSP1RXL", NULL, 0, WM8996_POWER_MANAGEMENT_3, 9, 0),
  1003. SND_SOC_DAPM_AIF_IN("DSP1RXR", NULL, 1, WM8996_POWER_MANAGEMENT_3, 8, 0),
  1004. SND_SOC_DAPM_MIXER("DSP2TXL", WM8996_POWER_MANAGEMENT_5, 11, 0,
  1005. dsp2txl, ARRAY_SIZE(dsp2txl)),
  1006. SND_SOC_DAPM_MIXER("DSP2TXR", WM8996_POWER_MANAGEMENT_5, 10, 0,
  1007. dsp2txr, ARRAY_SIZE(dsp2txr)),
  1008. SND_SOC_DAPM_MIXER("DSP1TXL", WM8996_POWER_MANAGEMENT_5, 9, 0,
  1009. dsp1txl, ARRAY_SIZE(dsp1txl)),
  1010. SND_SOC_DAPM_MIXER("DSP1TXR", WM8996_POWER_MANAGEMENT_5, 8, 0,
  1011. dsp1txr, ARRAY_SIZE(dsp1txr)),
  1012. SND_SOC_DAPM_MIXER("DAC2L Mixer", SND_SOC_NOPM, 0, 0,
  1013. dac2l_mix, ARRAY_SIZE(dac2l_mix)),
  1014. SND_SOC_DAPM_MIXER("DAC2R Mixer", SND_SOC_NOPM, 0, 0,
  1015. dac2r_mix, ARRAY_SIZE(dac2r_mix)),
  1016. SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
  1017. dac1l_mix, ARRAY_SIZE(dac1l_mix)),
  1018. SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
  1019. dac1r_mix, ARRAY_SIZE(dac1r_mix)),
  1020. SND_SOC_DAPM_DAC("DAC2L", NULL, WM8996_POWER_MANAGEMENT_5, 3, 0),
  1021. SND_SOC_DAPM_DAC("DAC2R", NULL, WM8996_POWER_MANAGEMENT_5, 2, 0),
  1022. SND_SOC_DAPM_DAC("DAC1L", NULL, WM8996_POWER_MANAGEMENT_5, 1, 0),
  1023. SND_SOC_DAPM_DAC("DAC1R", NULL, WM8996_POWER_MANAGEMENT_5, 0, 0),
  1024. SND_SOC_DAPM_AIF_IN("AIF2RX1", NULL, 0, WM8996_POWER_MANAGEMENT_4, 9, 0),
  1025. SND_SOC_DAPM_AIF_IN("AIF2RX0", NULL, 1, WM8996_POWER_MANAGEMENT_4, 8, 0),
  1026. SND_SOC_DAPM_AIF_OUT("AIF2TX1", NULL, 0, WM8996_POWER_MANAGEMENT_6, 9, 0),
  1027. SND_SOC_DAPM_AIF_OUT("AIF2TX0", NULL, 1, WM8996_POWER_MANAGEMENT_6, 8, 0),
  1028. SND_SOC_DAPM_AIF_IN("AIF1RX5", NULL, 5, WM8996_POWER_MANAGEMENT_4, 5, 0),
  1029. SND_SOC_DAPM_AIF_IN("AIF1RX4", NULL, 4, WM8996_POWER_MANAGEMENT_4, 4, 0),
  1030. SND_SOC_DAPM_AIF_IN("AIF1RX3", NULL, 3, WM8996_POWER_MANAGEMENT_4, 3, 0),
  1031. SND_SOC_DAPM_AIF_IN("AIF1RX2", NULL, 2, WM8996_POWER_MANAGEMENT_4, 2, 0),
  1032. SND_SOC_DAPM_AIF_IN("AIF1RX1", NULL, 1, WM8996_POWER_MANAGEMENT_4, 1, 0),
  1033. SND_SOC_DAPM_AIF_IN("AIF1RX0", NULL, 0, WM8996_POWER_MANAGEMENT_4, 0, 0),
  1034. SND_SOC_DAPM_AIF_OUT("AIF1TX5", NULL, 5, WM8996_POWER_MANAGEMENT_6, 5, 0),
  1035. SND_SOC_DAPM_AIF_OUT("AIF1TX4", NULL, 4, WM8996_POWER_MANAGEMENT_6, 4, 0),
  1036. SND_SOC_DAPM_AIF_OUT("AIF1TX3", NULL, 3, WM8996_POWER_MANAGEMENT_6, 3, 0),
  1037. SND_SOC_DAPM_AIF_OUT("AIF1TX2", NULL, 2, WM8996_POWER_MANAGEMENT_6, 2, 0),
  1038. SND_SOC_DAPM_AIF_OUT("AIF1TX1", NULL, 1, WM8996_POWER_MANAGEMENT_6, 1, 0),
  1039. SND_SOC_DAPM_AIF_OUT("AIF1TX0", NULL, 0, WM8996_POWER_MANAGEMENT_6, 0, 0),
  1040. /* We route as stereo pairs so define some dummy widgets to squash
  1041. * things down for now. RXA = 0,1, RXB = 2,3 and so on */
  1042. SND_SOC_DAPM_PGA("AIF1RXA", SND_SOC_NOPM, 0, 0, NULL, 0),
  1043. SND_SOC_DAPM_PGA("AIF1RXB", SND_SOC_NOPM, 0, 0, NULL, 0),
  1044. SND_SOC_DAPM_PGA("AIF1RXC", SND_SOC_NOPM, 0, 0, NULL, 0),
  1045. SND_SOC_DAPM_PGA("AIF2RX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1046. SND_SOC_DAPM_PGA("DSP2TX", SND_SOC_NOPM, 0, 0, NULL, 0),
  1047. SND_SOC_DAPM_MUX("DSP1RX", SND_SOC_NOPM, 0, 0, &dsp1rx),
  1048. SND_SOC_DAPM_MUX("DSP2RX", SND_SOC_NOPM, 0, 0, &dsp2rx),
  1049. SND_SOC_DAPM_MUX("AIF2TX", SND_SOC_NOPM, 0, 0, &aif2tx),
  1050. SND_SOC_DAPM_MUX("SPKL", SND_SOC_NOPM, 0, 0, &spkl_mux),
  1051. SND_SOC_DAPM_MUX("SPKR", SND_SOC_NOPM, 0, 0, &spkr_mux),
  1052. SND_SOC_DAPM_PGA("SPKL PGA", WM8996_LEFT_PDM_SPEAKER, 4, 0, NULL, 0),
  1053. SND_SOC_DAPM_PGA("SPKR PGA", WM8996_RIGHT_PDM_SPEAKER, 4, 0, NULL, 0),
  1054. SND_SOC_DAPM_PGA_S("HPOUT2L PGA", 0, WM8996_POWER_MANAGEMENT_1, 7, 0, NULL, 0),
  1055. SND_SOC_DAPM_PGA_S("HPOUT2L_DLY", 1, WM8996_ANALOGUE_HP_2, 5, 0, NULL, 0),
  1056. SND_SOC_DAPM_PGA_S("HPOUT2L_DCS", 2, WM8996_DC_SERVO_1, 2, 0, dcs_start,
  1057. SND_SOC_DAPM_POST_PMU),
  1058. SND_SOC_DAPM_PGA_S("HPOUT2L_RMV_SHORT", 3, SND_SOC_NOPM, HPOUT2L, 0,
  1059. rmv_short_event,
  1060. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  1061. SND_SOC_DAPM_PGA_S("HPOUT2R PGA", 0, WM8996_POWER_MANAGEMENT_1, 6, 0,NULL, 0),
  1062. SND_SOC_DAPM_PGA_S("HPOUT2R_DLY", 1, WM8996_ANALOGUE_HP_2, 1, 0, NULL, 0),
  1063. SND_SOC_DAPM_PGA_S("HPOUT2R_DCS", 2, WM8996_DC_SERVO_1, 3, 0, dcs_start,
  1064. SND_SOC_DAPM_POST_PMU),
  1065. SND_SOC_DAPM_PGA_S("HPOUT2R_RMV_SHORT", 3, SND_SOC_NOPM, HPOUT2R, 0,
  1066. rmv_short_event,
  1067. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  1068. SND_SOC_DAPM_PGA_S("HPOUT1L PGA", 0, WM8996_POWER_MANAGEMENT_1, 5, 0, NULL, 0),
  1069. SND_SOC_DAPM_PGA_S("HPOUT1L_DLY", 1, WM8996_ANALOGUE_HP_1, 5, 0, NULL, 0),
  1070. SND_SOC_DAPM_PGA_S("HPOUT1L_DCS", 2, WM8996_DC_SERVO_1, 0, 0, dcs_start,
  1071. SND_SOC_DAPM_POST_PMU),
  1072. SND_SOC_DAPM_PGA_S("HPOUT1L_RMV_SHORT", 3, SND_SOC_NOPM, HPOUT1L, 0,
  1073. rmv_short_event,
  1074. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  1075. SND_SOC_DAPM_PGA_S("HPOUT1R PGA", 0, WM8996_POWER_MANAGEMENT_1, 4, 0, NULL, 0),
  1076. SND_SOC_DAPM_PGA_S("HPOUT1R_DLY", 1, WM8996_ANALOGUE_HP_1, 1, 0, NULL, 0),
  1077. SND_SOC_DAPM_PGA_S("HPOUT1R_DCS", 2, WM8996_DC_SERVO_1, 1, 0, dcs_start,
  1078. SND_SOC_DAPM_POST_PMU),
  1079. SND_SOC_DAPM_PGA_S("HPOUT1R_RMV_SHORT", 3, SND_SOC_NOPM, HPOUT1R, 0,
  1080. rmv_short_event,
  1081. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  1082. SND_SOC_DAPM_OUTPUT("HPOUT1L"),
  1083. SND_SOC_DAPM_OUTPUT("HPOUT1R"),
  1084. SND_SOC_DAPM_OUTPUT("HPOUT2L"),
  1085. SND_SOC_DAPM_OUTPUT("HPOUT2R"),
  1086. SND_SOC_DAPM_OUTPUT("SPKDAT"),
  1087. };
  1088. static const struct snd_soc_dapm_route wm8996_dapm_routes[] = {
  1089. { "AIFCLK", NULL, "SYSCLK" },
  1090. { "SYSDSPCLK", NULL, "SYSCLK" },
  1091. { "Charge Pump", NULL, "SYSCLK" },
  1092. { "Charge Pump", NULL, "CPVDD" },
  1093. { "MICB1", NULL, "LDO2" },
  1094. { "MICB1", NULL, "MICB1 Audio" },
  1095. { "MICB1", NULL, "Bandgap" },
  1096. { "MICB2", NULL, "LDO2" },
  1097. { "MICB2", NULL, "MICB2 Audio" },
  1098. { "MICB2", NULL, "Bandgap" },
  1099. { "AIF1RX0", NULL, "AIF1 Playback" },
  1100. { "AIF1RX1", NULL, "AIF1 Playback" },
  1101. { "AIF1RX2", NULL, "AIF1 Playback" },
  1102. { "AIF1RX3", NULL, "AIF1 Playback" },
  1103. { "AIF1RX4", NULL, "AIF1 Playback" },
  1104. { "AIF1RX5", NULL, "AIF1 Playback" },
  1105. { "AIF2RX0", NULL, "AIF2 Playback" },
  1106. { "AIF2RX1", NULL, "AIF2 Playback" },
  1107. { "AIF1 Capture", NULL, "AIF1TX0" },
  1108. { "AIF1 Capture", NULL, "AIF1TX1" },
  1109. { "AIF1 Capture", NULL, "AIF1TX2" },
  1110. { "AIF1 Capture", NULL, "AIF1TX3" },
  1111. { "AIF1 Capture", NULL, "AIF1TX4" },
  1112. { "AIF1 Capture", NULL, "AIF1TX5" },
  1113. { "AIF2 Capture", NULL, "AIF2TX0" },
  1114. { "AIF2 Capture", NULL, "AIF2TX1" },
  1115. { "IN1L PGA", NULL, "IN2LN" },
  1116. { "IN1L PGA", NULL, "IN2LP" },
  1117. { "IN1L PGA", NULL, "IN1LN" },
  1118. { "IN1L PGA", NULL, "IN1LP" },
  1119. { "IN1L PGA", NULL, "Bandgap" },
  1120. { "IN1R PGA", NULL, "IN2RN" },
  1121. { "IN1R PGA", NULL, "IN2RP" },
  1122. { "IN1R PGA", NULL, "IN1RN" },
  1123. { "IN1R PGA", NULL, "IN1RP" },
  1124. { "IN1R PGA", NULL, "Bandgap" },
  1125. { "ADCL", NULL, "IN1L PGA" },
  1126. { "ADCR", NULL, "IN1R PGA" },
  1127. { "DMIC1L", NULL, "DMIC1DAT" },
  1128. { "DMIC1R", NULL, "DMIC1DAT" },
  1129. { "DMIC2L", NULL, "DMIC2DAT" },
  1130. { "DMIC2R", NULL, "DMIC2DAT" },
  1131. { "DMIC2L", NULL, "DMIC2" },
  1132. { "DMIC2R", NULL, "DMIC2" },
  1133. { "DMIC1L", NULL, "DMIC1" },
  1134. { "DMIC1R", NULL, "DMIC1" },
  1135. { "IN1L Mux", "ADC", "ADCL" },
  1136. { "IN1L Mux", "DMIC1", "DMIC1L" },
  1137. { "IN1L Mux", "DMIC2", "DMIC2L" },
  1138. { "IN1R Mux", "ADC", "ADCR" },
  1139. { "IN1R Mux", "DMIC1", "DMIC1R" },
  1140. { "IN1R Mux", "DMIC2", "DMIC2R" },
  1141. { "IN2L Mux", "ADC", "ADCL" },
  1142. { "IN2L Mux", "DMIC1", "DMIC1L" },
  1143. { "IN2L Mux", "DMIC2", "DMIC2L" },
  1144. { "IN2R Mux", "ADC", "ADCR" },
  1145. { "IN2R Mux", "DMIC1", "DMIC1R" },
  1146. { "IN2R Mux", "DMIC2", "DMIC2R" },
  1147. { "Left Sidetone", "IN1", "IN1L Mux" },
  1148. { "Left Sidetone", "IN2", "IN2L Mux" },
  1149. { "Right Sidetone", "IN1", "IN1R Mux" },
  1150. { "Right Sidetone", "IN2", "IN2R Mux" },
  1151. { "DSP1TXL", "IN1 Switch", "IN1L Mux" },
  1152. { "DSP1TXR", "IN1 Switch", "IN1R Mux" },
  1153. { "DSP2TXL", "IN1 Switch", "IN2L Mux" },
  1154. { "DSP2TXR", "IN1 Switch", "IN2R Mux" },
  1155. { "AIF1TX0", NULL, "DSP1TXL" },
  1156. { "AIF1TX1", NULL, "DSP1TXR" },
  1157. { "AIF1TX2", NULL, "DSP2TXL" },
  1158. { "AIF1TX3", NULL, "DSP2TXR" },
  1159. { "AIF1TX4", NULL, "AIF2RX0" },
  1160. { "AIF1TX5", NULL, "AIF2RX1" },
  1161. { "AIF1RX0", NULL, "AIFCLK" },
  1162. { "AIF1RX1", NULL, "AIFCLK" },
  1163. { "AIF1RX2", NULL, "AIFCLK" },
  1164. { "AIF1RX3", NULL, "AIFCLK" },
  1165. { "AIF1RX4", NULL, "AIFCLK" },
  1166. { "AIF1RX5", NULL, "AIFCLK" },
  1167. { "AIF2RX0", NULL, "AIFCLK" },
  1168. { "AIF2RX1", NULL, "AIFCLK" },
  1169. { "AIF1TX0", NULL, "AIFCLK" },
  1170. { "AIF1TX1", NULL, "AIFCLK" },
  1171. { "AIF1TX2", NULL, "AIFCLK" },
  1172. { "AIF1TX3", NULL, "AIFCLK" },
  1173. { "AIF1TX4", NULL, "AIFCLK" },
  1174. { "AIF1TX5", NULL, "AIFCLK" },
  1175. { "AIF2TX0", NULL, "AIFCLK" },
  1176. { "AIF2TX1", NULL, "AIFCLK" },
  1177. { "DSP1RXL", NULL, "SYSDSPCLK" },
  1178. { "DSP1RXR", NULL, "SYSDSPCLK" },
  1179. { "DSP2RXL", NULL, "SYSDSPCLK" },
  1180. { "DSP2RXR", NULL, "SYSDSPCLK" },
  1181. { "DSP1TXL", NULL, "SYSDSPCLK" },
  1182. { "DSP1TXR", NULL, "SYSDSPCLK" },
  1183. { "DSP2TXL", NULL, "SYSDSPCLK" },
  1184. { "DSP2TXR", NULL, "SYSDSPCLK" },
  1185. { "AIF1RXA", NULL, "AIF1RX0" },
  1186. { "AIF1RXA", NULL, "AIF1RX1" },
  1187. { "AIF1RXB", NULL, "AIF1RX2" },
  1188. { "AIF1RXB", NULL, "AIF1RX3" },
  1189. { "AIF1RXC", NULL, "AIF1RX4" },
  1190. { "AIF1RXC", NULL, "AIF1RX5" },
  1191. { "AIF2RX", NULL, "AIF2RX0" },
  1192. { "AIF2RX", NULL, "AIF2RX1" },
  1193. { "AIF2TX", "DSP2", "DSP2TX" },
  1194. { "AIF2TX", "DSP1", "DSP1RX" },
  1195. { "AIF2TX", "AIF1", "AIF1RXC" },
  1196. { "DSP1RXL", NULL, "DSP1RX" },
  1197. { "DSP1RXR", NULL, "DSP1RX" },
  1198. { "DSP2RXL", NULL, "DSP2RX" },
  1199. { "DSP2RXR", NULL, "DSP2RX" },
  1200. { "DSP2TX", NULL, "DSP2TXL" },
  1201. { "DSP2TX", NULL, "DSP2TXR" },
  1202. { "DSP1RX", "AIF1", "AIF1RXA" },
  1203. { "DSP1RX", "AIF2", "AIF2RX" },
  1204. { "DSP2RX", "AIF1", "AIF1RXB" },
  1205. { "DSP2RX", "AIF2", "AIF2RX" },
  1206. { "DAC2L Mixer", "DSP2 Switch", "DSP2RXL" },
  1207. { "DAC2L Mixer", "DSP1 Switch", "DSP1RXL" },
  1208. { "DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1209. { "DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1210. { "DAC2R Mixer", "DSP2 Switch", "DSP2RXR" },
  1211. { "DAC2R Mixer", "DSP1 Switch", "DSP1RXR" },
  1212. { "DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1213. { "DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1214. { "DAC1L Mixer", "DSP2 Switch", "DSP2RXL" },
  1215. { "DAC1L Mixer", "DSP1 Switch", "DSP1RXL" },
  1216. { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1217. { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1218. { "DAC1R Mixer", "DSP2 Switch", "DSP2RXR" },
  1219. { "DAC1R Mixer", "DSP1 Switch", "DSP1RXR" },
  1220. { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1221. { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1222. { "DAC1L", NULL, "DAC1L Mixer" },
  1223. { "DAC1R", NULL, "DAC1R Mixer" },
  1224. { "DAC2L", NULL, "DAC2L Mixer" },
  1225. { "DAC2R", NULL, "DAC2R Mixer" },
  1226. { "HPOUT2L PGA", NULL, "Charge Pump" },
  1227. { "HPOUT2L PGA", NULL, "Bandgap" },
  1228. { "HPOUT2L PGA", NULL, "DAC2L" },
  1229. { "HPOUT2L_DLY", NULL, "HPOUT2L PGA" },
  1230. { "HPOUT2L_DCS", NULL, "HPOUT2L_DLY" },
  1231. { "HPOUT2L_RMV_SHORT", NULL, "HPOUT2L_DCS" },
  1232. { "HPOUT2R PGA", NULL, "Charge Pump" },
  1233. { "HPOUT2R PGA", NULL, "Bandgap" },
  1234. { "HPOUT2R PGA", NULL, "DAC2R" },
  1235. { "HPOUT2R_DLY", NULL, "HPOUT2R PGA" },
  1236. { "HPOUT2R_DCS", NULL, "HPOUT2R_DLY" },
  1237. { "HPOUT2R_RMV_SHORT", NULL, "HPOUT2R_DCS" },
  1238. { "HPOUT1L PGA", NULL, "Charge Pump" },
  1239. { "HPOUT1L PGA", NULL, "Bandgap" },
  1240. { "HPOUT1L PGA", NULL, "DAC1L" },
  1241. { "HPOUT1L_DLY", NULL, "HPOUT1L PGA" },
  1242. { "HPOUT1L_DCS", NULL, "HPOUT1L_DLY" },
  1243. { "HPOUT1L_RMV_SHORT", NULL, "HPOUT1L_DCS" },
  1244. { "HPOUT1R PGA", NULL, "Charge Pump" },
  1245. { "HPOUT1R PGA", NULL, "Bandgap" },
  1246. { "HPOUT1R PGA", NULL, "DAC1R" },
  1247. { "HPOUT1R_DLY", NULL, "HPOUT1R PGA" },
  1248. { "HPOUT1R_DCS", NULL, "HPOUT1R_DLY" },
  1249. { "HPOUT1R_RMV_SHORT", NULL, "HPOUT1R_DCS" },
  1250. { "HPOUT2L", NULL, "HPOUT2L_RMV_SHORT" },
  1251. { "HPOUT2R", NULL, "HPOUT2R_RMV_SHORT" },
  1252. { "HPOUT1L", NULL, "HPOUT1L_RMV_SHORT" },
  1253. { "HPOUT1R", NULL, "HPOUT1R_RMV_SHORT" },
  1254. { "SPKL", "DAC1L", "DAC1L" },
  1255. { "SPKL", "DAC1R", "DAC1R" },
  1256. { "SPKL", "DAC2L", "DAC2L" },
  1257. { "SPKL", "DAC2R", "DAC2R" },
  1258. { "SPKR", "DAC1L", "DAC1L" },
  1259. { "SPKR", "DAC1R", "DAC1R" },
  1260. { "SPKR", "DAC2L", "DAC2L" },
  1261. { "SPKR", "DAC2R", "DAC2R" },
  1262. { "SPKL PGA", NULL, "SPKL" },
  1263. { "SPKR PGA", NULL, "SPKR" },
  1264. { "SPKDAT", NULL, "SPKL PGA" },
  1265. { "SPKDAT", NULL, "SPKR PGA" },
  1266. };
  1267. static bool wm8996_readable_register(struct device *dev, unsigned int reg)
  1268. {
  1269. /* Due to the sparseness of the register map the compiler
  1270. * output from an explicit switch statement ends up being much
  1271. * more efficient than a table.
  1272. */
  1273. switch (reg) {
  1274. case WM8996_SOFTWARE_RESET:
  1275. case WM8996_POWER_MANAGEMENT_1:
  1276. case WM8996_POWER_MANAGEMENT_2:
  1277. case WM8996_POWER_MANAGEMENT_3:
  1278. case WM8996_POWER_MANAGEMENT_4:
  1279. case WM8996_POWER_MANAGEMENT_5:
  1280. case WM8996_POWER_MANAGEMENT_6:
  1281. case WM8996_POWER_MANAGEMENT_7:
  1282. case WM8996_POWER_MANAGEMENT_8:
  1283. case WM8996_LEFT_LINE_INPUT_VOLUME:
  1284. case WM8996_RIGHT_LINE_INPUT_VOLUME:
  1285. case WM8996_LINE_INPUT_CONTROL:
  1286. case WM8996_DAC1_HPOUT1_VOLUME:
  1287. case WM8996_DAC2_HPOUT2_VOLUME:
  1288. case WM8996_DAC1_LEFT_VOLUME:
  1289. case WM8996_DAC1_RIGHT_VOLUME:
  1290. case WM8996_DAC2_LEFT_VOLUME:
  1291. case WM8996_DAC2_RIGHT_VOLUME:
  1292. case WM8996_OUTPUT1_LEFT_VOLUME:
  1293. case WM8996_OUTPUT1_RIGHT_VOLUME:
  1294. case WM8996_OUTPUT2_LEFT_VOLUME:
  1295. case WM8996_OUTPUT2_RIGHT_VOLUME:
  1296. case WM8996_MICBIAS_1:
  1297. case WM8996_MICBIAS_2:
  1298. case WM8996_LDO_1:
  1299. case WM8996_LDO_2:
  1300. case WM8996_ACCESSORY_DETECT_MODE_1:
  1301. case WM8996_ACCESSORY_DETECT_MODE_2:
  1302. case WM8996_HEADPHONE_DETECT_1:
  1303. case WM8996_HEADPHONE_DETECT_2:
  1304. case WM8996_MIC_DETECT_1:
  1305. case WM8996_MIC_DETECT_2:
  1306. case WM8996_MIC_DETECT_3:
  1307. case WM8996_CHARGE_PUMP_1:
  1308. case WM8996_CHARGE_PUMP_2:
  1309. case WM8996_DC_SERVO_1:
  1310. case WM8996_DC_SERVO_2:
  1311. case WM8996_DC_SERVO_3:
  1312. case WM8996_DC_SERVO_5:
  1313. case WM8996_DC_SERVO_6:
  1314. case WM8996_DC_SERVO_7:
  1315. case WM8996_DC_SERVO_READBACK_0:
  1316. case WM8996_ANALOGUE_HP_1:
  1317. case WM8996_ANALOGUE_HP_2:
  1318. case WM8996_CHIP_REVISION:
  1319. case WM8996_CONTROL_INTERFACE_1:
  1320. case WM8996_WRITE_SEQUENCER_CTRL_1:
  1321. case WM8996_WRITE_SEQUENCER_CTRL_2:
  1322. case WM8996_AIF_CLOCKING_1:
  1323. case WM8996_AIF_CLOCKING_2:
  1324. case WM8996_CLOCKING_1:
  1325. case WM8996_CLOCKING_2:
  1326. case WM8996_AIF_RATE:
  1327. case WM8996_FLL_CONTROL_1:
  1328. case WM8996_FLL_CONTROL_2:
  1329. case WM8996_FLL_CONTROL_3:
  1330. case WM8996_FLL_CONTROL_4:
  1331. case WM8996_FLL_CONTROL_5:
  1332. case WM8996_FLL_CONTROL_6:
  1333. case WM8996_FLL_EFS_1:
  1334. case WM8996_FLL_EFS_2:
  1335. case WM8996_AIF1_CONTROL:
  1336. case WM8996_AIF1_BCLK:
  1337. case WM8996_AIF1_TX_LRCLK_1:
  1338. case WM8996_AIF1_TX_LRCLK_2:
  1339. case WM8996_AIF1_RX_LRCLK_1:
  1340. case WM8996_AIF1_RX_LRCLK_2:
  1341. case WM8996_AIF1TX_DATA_CONFIGURATION_1:
  1342. case WM8996_AIF1TX_DATA_CONFIGURATION_2:
  1343. case WM8996_AIF1RX_DATA_CONFIGURATION:
  1344. case WM8996_AIF1TX_CHANNEL_0_CONFIGURATION:
  1345. case WM8996_AIF1TX_CHANNEL_1_CONFIGURATION:
  1346. case WM8996_AIF1TX_CHANNEL_2_CONFIGURATION:
  1347. case WM8996_AIF1TX_CHANNEL_3_CONFIGURATION:
  1348. case WM8996_AIF1TX_CHANNEL_4_CONFIGURATION:
  1349. case WM8996_AIF1TX_CHANNEL_5_CONFIGURATION:
  1350. case WM8996_AIF1RX_CHANNEL_0_CONFIGURATION:
  1351. case WM8996_AIF1RX_CHANNEL_1_CONFIGURATION:
  1352. case WM8996_AIF1RX_CHANNEL_2_CONFIGURATION:
  1353. case WM8996_AIF1RX_CHANNEL_3_CONFIGURATION:
  1354. case WM8996_AIF1RX_CHANNEL_4_CONFIGURATION:
  1355. case WM8996_AIF1RX_CHANNEL_5_CONFIGURATION:
  1356. case WM8996_AIF1RX_MONO_CONFIGURATION:
  1357. case WM8996_AIF1TX_TEST:
  1358. case WM8996_AIF2_CONTROL:
  1359. case WM8996_AIF2_BCLK:
  1360. case WM8996_AIF2_TX_LRCLK_1:
  1361. case WM8996_AIF2_TX_LRCLK_2:
  1362. case WM8996_AIF2_RX_LRCLK_1:
  1363. case WM8996_AIF2_RX_LRCLK_2:
  1364. case WM8996_AIF2TX_DATA_CONFIGURATION_1:
  1365. case WM8996_AIF2TX_DATA_CONFIGURATION_2:
  1366. case WM8996_AIF2RX_DATA_CONFIGURATION:
  1367. case WM8996_AIF2TX_CHANNEL_0_CONFIGURATION:
  1368. case WM8996_AIF2TX_CHANNEL_1_CONFIGURATION:
  1369. case WM8996_AIF2RX_CHANNEL_0_CONFIGURATION:
  1370. case WM8996_AIF2RX_CHANNEL_1_CONFIGURATION:
  1371. case WM8996_AIF2RX_MONO_CONFIGURATION:
  1372. case WM8996_AIF2TX_TEST:
  1373. case WM8996_DSP1_TX_LEFT_VOLUME:
  1374. case WM8996_DSP1_TX_RIGHT_VOLUME:
  1375. case WM8996_DSP1_RX_LEFT_VOLUME:
  1376. case WM8996_DSP1_RX_RIGHT_VOLUME:
  1377. case WM8996_DSP1_TX_FILTERS:
  1378. case WM8996_DSP1_RX_FILTERS_1:
  1379. case WM8996_DSP1_RX_FILTERS_2:
  1380. case WM8996_DSP1_DRC_1:
  1381. case WM8996_DSP1_DRC_2:
  1382. case WM8996_DSP1_DRC_3:
  1383. case WM8996_DSP1_DRC_4:
  1384. case WM8996_DSP1_DRC_5:
  1385. case WM8996_DSP1_RX_EQ_GAINS_1:
  1386. case WM8996_DSP1_RX_EQ_GAINS_2:
  1387. case WM8996_DSP1_RX_EQ_BAND_1_A:
  1388. case WM8996_DSP1_RX_EQ_BAND_1_B:
  1389. case WM8996_DSP1_RX_EQ_BAND_1_PG:
  1390. case WM8996_DSP1_RX_EQ_BAND_2_A:
  1391. case WM8996_DSP1_RX_EQ_BAND_2_B:
  1392. case WM8996_DSP1_RX_EQ_BAND_2_C:
  1393. case WM8996_DSP1_RX_EQ_BAND_2_PG:
  1394. case WM8996_DSP1_RX_EQ_BAND_3_A:
  1395. case WM8996_DSP1_RX_EQ_BAND_3_B:
  1396. case WM8996_DSP1_RX_EQ_BAND_3_C:
  1397. case WM8996_DSP1_RX_EQ_BAND_3_PG:
  1398. case WM8996_DSP1_RX_EQ_BAND_4_A:
  1399. case WM8996_DSP1_RX_EQ_BAND_4_B:
  1400. case WM8996_DSP1_RX_EQ_BAND_4_C:
  1401. case WM8996_DSP1_RX_EQ_BAND_4_PG:
  1402. case WM8996_DSP1_RX_EQ_BAND_5_A:
  1403. case WM8996_DSP1_RX_EQ_BAND_5_B:
  1404. case WM8996_DSP1_RX_EQ_BAND_5_PG:
  1405. case WM8996_DSP2_TX_LEFT_VOLUME:
  1406. case WM8996_DSP2_TX_RIGHT_VOLUME:
  1407. case WM8996_DSP2_RX_LEFT_VOLUME:
  1408. case WM8996_DSP2_RX_RIGHT_VOLUME:
  1409. case WM8996_DSP2_TX_FILTERS:
  1410. case WM8996_DSP2_RX_FILTERS_1:
  1411. case WM8996_DSP2_RX_FILTERS_2:
  1412. case WM8996_DSP2_DRC_1:
  1413. case WM8996_DSP2_DRC_2:
  1414. case WM8996_DSP2_DRC_3:
  1415. case WM8996_DSP2_DRC_4:
  1416. case WM8996_DSP2_DRC_5:
  1417. case WM8996_DSP2_RX_EQ_GAINS_1:
  1418. case WM8996_DSP2_RX_EQ_GAINS_2:
  1419. case WM8996_DSP2_RX_EQ_BAND_1_A:
  1420. case WM8996_DSP2_RX_EQ_BAND_1_B:
  1421. case WM8996_DSP2_RX_EQ_BAND_1_PG:
  1422. case WM8996_DSP2_RX_EQ_BAND_2_A:
  1423. case WM8996_DSP2_RX_EQ_BAND_2_B:
  1424. case WM8996_DSP2_RX_EQ_BAND_2_C:
  1425. case WM8996_DSP2_RX_EQ_BAND_2_PG:
  1426. case WM8996_DSP2_RX_EQ_BAND_3_A:
  1427. case WM8996_DSP2_RX_EQ_BAND_3_B:
  1428. case WM8996_DSP2_RX_EQ_BAND_3_C:
  1429. case WM8996_DSP2_RX_EQ_BAND_3_PG:
  1430. case WM8996_DSP2_RX_EQ_BAND_4_A:
  1431. case WM8996_DSP2_RX_EQ_BAND_4_B:
  1432. case WM8996_DSP2_RX_EQ_BAND_4_C:
  1433. case WM8996_DSP2_RX_EQ_BAND_4_PG:
  1434. case WM8996_DSP2_RX_EQ_BAND_5_A:
  1435. case WM8996_DSP2_RX_EQ_BAND_5_B:
  1436. case WM8996_DSP2_RX_EQ_BAND_5_PG:
  1437. case WM8996_DAC1_MIXER_VOLUMES:
  1438. case WM8996_DAC1_LEFT_MIXER_ROUTING:
  1439. case WM8996_DAC1_RIGHT_MIXER_ROUTING:
  1440. case WM8996_DAC2_MIXER_VOLUMES:
  1441. case WM8996_DAC2_LEFT_MIXER_ROUTING:
  1442. case WM8996_DAC2_RIGHT_MIXER_ROUTING:
  1443. case WM8996_DSP1_TX_LEFT_MIXER_ROUTING:
  1444. case WM8996_DSP1_TX_RIGHT_MIXER_ROUTING:
  1445. case WM8996_DSP2_TX_LEFT_MIXER_ROUTING:
  1446. case WM8996_DSP2_TX_RIGHT_MIXER_ROUTING:
  1447. case WM8996_DSP_TX_MIXER_SELECT:
  1448. case WM8996_DAC_SOFTMUTE:
  1449. case WM8996_OVERSAMPLING:
  1450. case WM8996_SIDETONE:
  1451. case WM8996_GPIO_1:
  1452. case WM8996_GPIO_2:
  1453. case WM8996_GPIO_3:
  1454. case WM8996_GPIO_4:
  1455. case WM8996_GPIO_5:
  1456. case WM8996_PULL_CONTROL_1:
  1457. case WM8996_PULL_CONTROL_2:
  1458. case WM8996_INTERRUPT_STATUS_1:
  1459. case WM8996_INTERRUPT_STATUS_2:
  1460. case WM8996_INTERRUPT_RAW_STATUS_2:
  1461. case WM8996_INTERRUPT_STATUS_1_MASK:
  1462. case WM8996_INTERRUPT_STATUS_2_MASK:
  1463. case WM8996_INTERRUPT_CONTROL:
  1464. case WM8996_LEFT_PDM_SPEAKER:
  1465. case WM8996_RIGHT_PDM_SPEAKER:
  1466. case WM8996_PDM_SPEAKER_MUTE_SEQUENCE:
  1467. case WM8996_PDM_SPEAKER_VOLUME:
  1468. return 1;
  1469. default:
  1470. return 0;
  1471. }
  1472. }
  1473. static bool wm8996_volatile_register(struct device *dev, unsigned int reg)
  1474. {
  1475. switch (reg) {
  1476. case WM8996_SOFTWARE_RESET:
  1477. case WM8996_CHIP_REVISION:
  1478. case WM8996_LDO_1:
  1479. case WM8996_LDO_2:
  1480. case WM8996_INTERRUPT_STATUS_1:
  1481. case WM8996_INTERRUPT_STATUS_2:
  1482. case WM8996_INTERRUPT_RAW_STATUS_2:
  1483. case WM8996_DC_SERVO_READBACK_0:
  1484. case WM8996_DC_SERVO_2:
  1485. case WM8996_DC_SERVO_6:
  1486. case WM8996_DC_SERVO_7:
  1487. case WM8996_FLL_CONTROL_6:
  1488. case WM8996_MIC_DETECT_3:
  1489. case WM8996_HEADPHONE_DETECT_1:
  1490. case WM8996_HEADPHONE_DETECT_2:
  1491. return 1;
  1492. default:
  1493. return 0;
  1494. }
  1495. }
  1496. static int wm8996_reset(struct wm8996_priv *wm8996)
  1497. {
  1498. if (wm8996->pdata.ldo_ena > 0) {
  1499. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 0);
  1500. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 1);
  1501. return 0;
  1502. } else {
  1503. return regmap_write(wm8996->regmap, WM8996_SOFTWARE_RESET,
  1504. 0x8915);
  1505. }
  1506. }
  1507. static const int bclk_divs[] = {
  1508. 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96
  1509. };
  1510. static void wm8996_update_bclk(struct snd_soc_codec *codec)
  1511. {
  1512. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  1513. int aif, best, cur_val, bclk_rate, bclk_reg, i;
  1514. /* Don't bother if we're in a low frequency idle mode that
  1515. * can't support audio.
  1516. */
  1517. if (wm8996->sysclk < 64000)
  1518. return;
  1519. for (aif = 0; aif < WM8996_AIFS; aif++) {
  1520. switch (aif) {
  1521. case 0:
  1522. bclk_reg = WM8996_AIF1_BCLK;
  1523. break;
  1524. case 1:
  1525. bclk_reg = WM8996_AIF2_BCLK;
  1526. break;
  1527. }
  1528. bclk_rate = wm8996->bclk_rate[aif];
  1529. /* Pick a divisor for BCLK as close as we can get to ideal */
  1530. best = 0;
  1531. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1532. cur_val = (wm8996->sysclk / bclk_divs[i]) - bclk_rate;
  1533. if (cur_val < 0) /* BCLK table is sorted */
  1534. break;
  1535. best = i;
  1536. }
  1537. bclk_rate = wm8996->sysclk / bclk_divs[best];
  1538. dev_dbg(codec->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
  1539. bclk_divs[best], bclk_rate);
  1540. snd_soc_update_bits(codec, bclk_reg,
  1541. WM8996_AIF1_BCLK_DIV_MASK, best);
  1542. }
  1543. }
  1544. static int wm8996_set_bias_level(struct snd_soc_codec *codec,
  1545. enum snd_soc_bias_level level)
  1546. {
  1547. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  1548. int ret;
  1549. switch (level) {
  1550. case SND_SOC_BIAS_ON:
  1551. case SND_SOC_BIAS_PREPARE:
  1552. break;
  1553. case SND_SOC_BIAS_STANDBY:
  1554. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1555. ret = regulator_bulk_enable(ARRAY_SIZE(wm8996->supplies),
  1556. wm8996->supplies);
  1557. if (ret != 0) {
  1558. dev_err(codec->dev,
  1559. "Failed to enable supplies: %d\n",
  1560. ret);
  1561. return ret;
  1562. }
  1563. if (wm8996->pdata.ldo_ena >= 0) {
  1564. gpio_set_value_cansleep(wm8996->pdata.ldo_ena,
  1565. 1);
  1566. msleep(5);
  1567. }
  1568. regcache_cache_only(codec->control_data, false);
  1569. regcache_sync(codec->control_data);
  1570. }
  1571. break;
  1572. case SND_SOC_BIAS_OFF:
  1573. regcache_cache_only(codec->control_data, true);
  1574. if (wm8996->pdata.ldo_ena >= 0)
  1575. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 0);
  1576. regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies),
  1577. wm8996->supplies);
  1578. break;
  1579. }
  1580. codec->dapm.bias_level = level;
  1581. return 0;
  1582. }
  1583. static int wm8996_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1584. {
  1585. struct snd_soc_codec *codec = dai->codec;
  1586. int aifctrl = 0;
  1587. int bclk = 0;
  1588. int lrclk_tx = 0;
  1589. int lrclk_rx = 0;
  1590. int aifctrl_reg, bclk_reg, lrclk_tx_reg, lrclk_rx_reg;
  1591. switch (dai->id) {
  1592. case 0:
  1593. aifctrl_reg = WM8996_AIF1_CONTROL;
  1594. bclk_reg = WM8996_AIF1_BCLK;
  1595. lrclk_tx_reg = WM8996_AIF1_TX_LRCLK_2;
  1596. lrclk_rx_reg = WM8996_AIF1_RX_LRCLK_2;
  1597. break;
  1598. case 1:
  1599. aifctrl_reg = WM8996_AIF2_CONTROL;
  1600. bclk_reg = WM8996_AIF2_BCLK;
  1601. lrclk_tx_reg = WM8996_AIF2_TX_LRCLK_2;
  1602. lrclk_rx_reg = WM8996_AIF2_RX_LRCLK_2;
  1603. break;
  1604. default:
  1605. BUG();
  1606. return -EINVAL;
  1607. }
  1608. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1609. case SND_SOC_DAIFMT_NB_NF:
  1610. break;
  1611. case SND_SOC_DAIFMT_IB_NF:
  1612. bclk |= WM8996_AIF1_BCLK_INV;
  1613. break;
  1614. case SND_SOC_DAIFMT_NB_IF:
  1615. lrclk_tx |= WM8996_AIF1TX_LRCLK_INV;
  1616. lrclk_rx |= WM8996_AIF1RX_LRCLK_INV;
  1617. break;
  1618. case SND_SOC_DAIFMT_IB_IF:
  1619. bclk |= WM8996_AIF1_BCLK_INV;
  1620. lrclk_tx |= WM8996_AIF1TX_LRCLK_INV;
  1621. lrclk_rx |= WM8996_AIF1RX_LRCLK_INV;
  1622. break;
  1623. }
  1624. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1625. case SND_SOC_DAIFMT_CBS_CFS:
  1626. break;
  1627. case SND_SOC_DAIFMT_CBS_CFM:
  1628. lrclk_tx |= WM8996_AIF1TX_LRCLK_MSTR;
  1629. lrclk_rx |= WM8996_AIF1RX_LRCLK_MSTR;
  1630. break;
  1631. case SND_SOC_DAIFMT_CBM_CFS:
  1632. bclk |= WM8996_AIF1_BCLK_MSTR;
  1633. break;
  1634. case SND_SOC_DAIFMT_CBM_CFM:
  1635. bclk |= WM8996_AIF1_BCLK_MSTR;
  1636. lrclk_tx |= WM8996_AIF1TX_LRCLK_MSTR;
  1637. lrclk_rx |= WM8996_AIF1RX_LRCLK_MSTR;
  1638. break;
  1639. default:
  1640. return -EINVAL;
  1641. }
  1642. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1643. case SND_SOC_DAIFMT_DSP_A:
  1644. break;
  1645. case SND_SOC_DAIFMT_DSP_B:
  1646. aifctrl |= 1;
  1647. break;
  1648. case SND_SOC_DAIFMT_I2S:
  1649. aifctrl |= 2;
  1650. break;
  1651. case SND_SOC_DAIFMT_LEFT_J:
  1652. aifctrl |= 3;
  1653. break;
  1654. default:
  1655. return -EINVAL;
  1656. }
  1657. snd_soc_update_bits(codec, aifctrl_reg, WM8996_AIF1_FMT_MASK, aifctrl);
  1658. snd_soc_update_bits(codec, bclk_reg,
  1659. WM8996_AIF1_BCLK_INV | WM8996_AIF1_BCLK_MSTR,
  1660. bclk);
  1661. snd_soc_update_bits(codec, lrclk_tx_reg,
  1662. WM8996_AIF1TX_LRCLK_INV |
  1663. WM8996_AIF1TX_LRCLK_MSTR,
  1664. lrclk_tx);
  1665. snd_soc_update_bits(codec, lrclk_rx_reg,
  1666. WM8996_AIF1RX_LRCLK_INV |
  1667. WM8996_AIF1RX_LRCLK_MSTR,
  1668. lrclk_rx);
  1669. return 0;
  1670. }
  1671. static const int dsp_divs[] = {
  1672. 48000, 32000, 16000, 8000
  1673. };
  1674. static int wm8996_hw_params(struct snd_pcm_substream *substream,
  1675. struct snd_pcm_hw_params *params,
  1676. struct snd_soc_dai *dai)
  1677. {
  1678. struct snd_soc_codec *codec = dai->codec;
  1679. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  1680. int bits, i, bclk_rate, best;
  1681. int aifdata = 0;
  1682. int lrclk = 0;
  1683. int dsp = 0;
  1684. int aifdata_reg, lrclk_reg, dsp_shift;
  1685. switch (dai->id) {
  1686. case 0:
  1687. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  1688. (snd_soc_read(codec, WM8996_GPIO_1)) & WM8996_GP1_FN_MASK) {
  1689. aifdata_reg = WM8996_AIF1RX_DATA_CONFIGURATION;
  1690. lrclk_reg = WM8996_AIF1_RX_LRCLK_1;
  1691. } else {
  1692. aifdata_reg = WM8996_AIF1TX_DATA_CONFIGURATION_1;
  1693. lrclk_reg = WM8996_AIF1_TX_LRCLK_1;
  1694. }
  1695. dsp_shift = 0;
  1696. break;
  1697. case 1:
  1698. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  1699. (snd_soc_read(codec, WM8996_GPIO_2)) & WM8996_GP2_FN_MASK) {
  1700. aifdata_reg = WM8996_AIF2RX_DATA_CONFIGURATION;
  1701. lrclk_reg = WM8996_AIF2_RX_LRCLK_1;
  1702. } else {
  1703. aifdata_reg = WM8996_AIF2TX_DATA_CONFIGURATION_1;
  1704. lrclk_reg = WM8996_AIF2_TX_LRCLK_1;
  1705. }
  1706. dsp_shift = WM8996_DSP2_DIV_SHIFT;
  1707. break;
  1708. default:
  1709. BUG();
  1710. return -EINVAL;
  1711. }
  1712. bclk_rate = snd_soc_params_to_bclk(params);
  1713. if (bclk_rate < 0) {
  1714. dev_err(codec->dev, "Unsupported BCLK rate: %d\n", bclk_rate);
  1715. return bclk_rate;
  1716. }
  1717. wm8996->bclk_rate[dai->id] = bclk_rate;
  1718. wm8996->rx_rate[dai->id] = params_rate(params);
  1719. /* Needs looking at for TDM */
  1720. bits = snd_pcm_format_width(params_format(params));
  1721. if (bits < 0)
  1722. return bits;
  1723. aifdata |= (bits << WM8996_AIF1TX_WL_SHIFT) | bits;
  1724. best = 0;
  1725. for (i = 0; i < ARRAY_SIZE(dsp_divs); i++) {
  1726. if (abs(dsp_divs[i] - params_rate(params)) <
  1727. abs(dsp_divs[best] - params_rate(params)))
  1728. best = i;
  1729. }
  1730. dsp |= i << dsp_shift;
  1731. wm8996_update_bclk(codec);
  1732. lrclk = bclk_rate / params_rate(params);
  1733. dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
  1734. lrclk, bclk_rate / lrclk);
  1735. snd_soc_update_bits(codec, aifdata_reg,
  1736. WM8996_AIF1TX_WL_MASK |
  1737. WM8996_AIF1TX_SLOT_LEN_MASK,
  1738. aifdata);
  1739. snd_soc_update_bits(codec, lrclk_reg, WM8996_AIF1RX_RATE_MASK,
  1740. lrclk);
  1741. snd_soc_update_bits(codec, WM8996_AIF_CLOCKING_2,
  1742. WM8996_DSP1_DIV_MASK << dsp_shift, dsp);
  1743. return 0;
  1744. }
  1745. static int wm8996_set_sysclk(struct snd_soc_dai *dai,
  1746. int clk_id, unsigned int freq, int dir)
  1747. {
  1748. struct snd_soc_codec *codec = dai->codec;
  1749. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  1750. int lfclk = 0;
  1751. int ratediv = 0;
  1752. int sync = WM8996_REG_SYNC;
  1753. int src;
  1754. int old;
  1755. if (freq == wm8996->sysclk && clk_id == wm8996->sysclk_src)
  1756. return 0;
  1757. /* Disable SYSCLK while we reconfigure */
  1758. old = snd_soc_read(codec, WM8996_AIF_CLOCKING_1) & WM8996_SYSCLK_ENA;
  1759. snd_soc_update_bits(codec, WM8996_AIF_CLOCKING_1,
  1760. WM8996_SYSCLK_ENA, 0);
  1761. switch (clk_id) {
  1762. case WM8996_SYSCLK_MCLK1:
  1763. wm8996->sysclk = freq;
  1764. src = 0;
  1765. break;
  1766. case WM8996_SYSCLK_MCLK2:
  1767. wm8996->sysclk = freq;
  1768. src = 1;
  1769. break;
  1770. case WM8996_SYSCLK_FLL:
  1771. wm8996->sysclk = freq;
  1772. src = 2;
  1773. break;
  1774. default:
  1775. dev_err(codec->dev, "Unsupported clock source %d\n", clk_id);
  1776. return -EINVAL;
  1777. }
  1778. switch (wm8996->sysclk) {
  1779. case 5644800:
  1780. case 6144000:
  1781. snd_soc_update_bits(codec, WM8996_AIF_RATE,
  1782. WM8996_SYSCLK_RATE, 0);
  1783. break;
  1784. case 22579200:
  1785. case 24576000:
  1786. ratediv = WM8996_SYSCLK_DIV;
  1787. wm8996->sysclk /= 2;
  1788. case 11289600:
  1789. case 12288000:
  1790. snd_soc_update_bits(codec, WM8996_AIF_RATE,
  1791. WM8996_SYSCLK_RATE, WM8996_SYSCLK_RATE);
  1792. break;
  1793. case 32000:
  1794. case 32768:
  1795. lfclk = WM8996_LFCLK_ENA;
  1796. sync = 0;
  1797. break;
  1798. default:
  1799. dev_warn(codec->dev, "Unsupported clock rate %dHz\n",
  1800. wm8996->sysclk);
  1801. return -EINVAL;
  1802. }
  1803. wm8996_update_bclk(codec);
  1804. snd_soc_update_bits(codec, WM8996_AIF_CLOCKING_1,
  1805. WM8996_SYSCLK_SRC_MASK | WM8996_SYSCLK_DIV_MASK,
  1806. src << WM8996_SYSCLK_SRC_SHIFT | ratediv);
  1807. snd_soc_update_bits(codec, WM8996_CLOCKING_1, WM8996_LFCLK_ENA, lfclk);
  1808. snd_soc_update_bits(codec, WM8996_CONTROL_INTERFACE_1,
  1809. WM8996_REG_SYNC, sync);
  1810. snd_soc_update_bits(codec, WM8996_AIF_CLOCKING_1,
  1811. WM8996_SYSCLK_ENA, old);
  1812. wm8996->sysclk_src = clk_id;
  1813. return 0;
  1814. }
  1815. struct _fll_div {
  1816. u16 fll_fratio;
  1817. u16 fll_outdiv;
  1818. u16 fll_refclk_div;
  1819. u16 fll_loop_gain;
  1820. u16 fll_ref_freq;
  1821. u16 n;
  1822. u16 theta;
  1823. u16 lambda;
  1824. };
  1825. static struct {
  1826. unsigned int min;
  1827. unsigned int max;
  1828. u16 fll_fratio;
  1829. int ratio;
  1830. } fll_fratios[] = {
  1831. { 0, 64000, 4, 16 },
  1832. { 64000, 128000, 3, 8 },
  1833. { 128000, 256000, 2, 4 },
  1834. { 256000, 1000000, 1, 2 },
  1835. { 1000000, 13500000, 0, 1 },
  1836. };
  1837. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1838. unsigned int Fout)
  1839. {
  1840. unsigned int target;
  1841. unsigned int div;
  1842. unsigned int fratio, gcd_fll;
  1843. int i;
  1844. /* Fref must be <=13.5MHz */
  1845. div = 1;
  1846. fll_div->fll_refclk_div = 0;
  1847. while ((Fref / div) > 13500000) {
  1848. div *= 2;
  1849. fll_div->fll_refclk_div++;
  1850. if (div > 8) {
  1851. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1852. Fref);
  1853. return -EINVAL;
  1854. }
  1855. }
  1856. pr_debug("FLL Fref=%u Fout=%u\n", Fref, Fout);
  1857. /* Apply the division for our remaining calculations */
  1858. Fref /= div;
  1859. if (Fref >= 3000000)
  1860. fll_div->fll_loop_gain = 5;
  1861. else
  1862. fll_div->fll_loop_gain = 0;
  1863. if (Fref >= 48000)
  1864. fll_div->fll_ref_freq = 0;
  1865. else
  1866. fll_div->fll_ref_freq = 1;
  1867. /* Fvco should be 90-100MHz; don't check the upper bound */
  1868. div = 2;
  1869. while (Fout * div < 90000000) {
  1870. div++;
  1871. if (div > 64) {
  1872. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1873. Fout);
  1874. return -EINVAL;
  1875. }
  1876. }
  1877. target = Fout * div;
  1878. fll_div->fll_outdiv = div - 1;
  1879. pr_debug("FLL Fvco=%dHz\n", target);
  1880. /* Find an appropraite FLL_FRATIO and factor it out of the target */
  1881. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1882. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1883. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1884. fratio = fll_fratios[i].ratio;
  1885. break;
  1886. }
  1887. }
  1888. if (i == ARRAY_SIZE(fll_fratios)) {
  1889. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1890. return -EINVAL;
  1891. }
  1892. fll_div->n = target / (fratio * Fref);
  1893. if (target % Fref == 0) {
  1894. fll_div->theta = 0;
  1895. fll_div->lambda = 0;
  1896. } else {
  1897. gcd_fll = gcd(target, fratio * Fref);
  1898. fll_div->theta = (target - (fll_div->n * fratio * Fref))
  1899. / gcd_fll;
  1900. fll_div->lambda = (fratio * Fref) / gcd_fll;
  1901. }
  1902. pr_debug("FLL N=%x THETA=%x LAMBDA=%x\n",
  1903. fll_div->n, fll_div->theta, fll_div->lambda);
  1904. pr_debug("FLL_FRATIO=%x FLL_OUTDIV=%x FLL_REFCLK_DIV=%x\n",
  1905. fll_div->fll_fratio, fll_div->fll_outdiv,
  1906. fll_div->fll_refclk_div);
  1907. return 0;
  1908. }
  1909. static int wm8996_set_fll(struct snd_soc_codec *codec, int fll_id, int source,
  1910. unsigned int Fref, unsigned int Fout)
  1911. {
  1912. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  1913. struct i2c_client *i2c = to_i2c_client(codec->dev);
  1914. struct _fll_div fll_div;
  1915. unsigned long timeout;
  1916. int ret, reg, retry;
  1917. /* Any change? */
  1918. if (source == wm8996->fll_src && Fref == wm8996->fll_fref &&
  1919. Fout == wm8996->fll_fout)
  1920. return 0;
  1921. if (Fout == 0) {
  1922. dev_dbg(codec->dev, "FLL disabled\n");
  1923. wm8996->fll_fref = 0;
  1924. wm8996->fll_fout = 0;
  1925. snd_soc_update_bits(codec, WM8996_FLL_CONTROL_1,
  1926. WM8996_FLL_ENA, 0);
  1927. wm8996_bg_disable(codec);
  1928. return 0;
  1929. }
  1930. ret = fll_factors(&fll_div, Fref, Fout);
  1931. if (ret != 0)
  1932. return ret;
  1933. switch (source) {
  1934. case WM8996_FLL_MCLK1:
  1935. reg = 0;
  1936. break;
  1937. case WM8996_FLL_MCLK2:
  1938. reg = 1;
  1939. break;
  1940. case WM8996_FLL_DACLRCLK1:
  1941. reg = 2;
  1942. break;
  1943. case WM8996_FLL_BCLK1:
  1944. reg = 3;
  1945. break;
  1946. default:
  1947. dev_err(codec->dev, "Unknown FLL source %d\n", ret);
  1948. return -EINVAL;
  1949. }
  1950. reg |= fll_div.fll_refclk_div << WM8996_FLL_REFCLK_DIV_SHIFT;
  1951. reg |= fll_div.fll_ref_freq << WM8996_FLL_REF_FREQ_SHIFT;
  1952. snd_soc_update_bits(codec, WM8996_FLL_CONTROL_5,
  1953. WM8996_FLL_REFCLK_DIV_MASK | WM8996_FLL_REF_FREQ |
  1954. WM8996_FLL_REFCLK_SRC_MASK, reg);
  1955. reg = 0;
  1956. if (fll_div.theta || fll_div.lambda)
  1957. reg |= WM8996_FLL_EFS_ENA | (3 << WM8996_FLL_LFSR_SEL_SHIFT);
  1958. else
  1959. reg |= 1 << WM8996_FLL_LFSR_SEL_SHIFT;
  1960. snd_soc_write(codec, WM8996_FLL_EFS_2, reg);
  1961. snd_soc_update_bits(codec, WM8996_FLL_CONTROL_2,
  1962. WM8996_FLL_OUTDIV_MASK |
  1963. WM8996_FLL_FRATIO_MASK,
  1964. (fll_div.fll_outdiv << WM8996_FLL_OUTDIV_SHIFT) |
  1965. (fll_div.fll_fratio));
  1966. snd_soc_write(codec, WM8996_FLL_CONTROL_3, fll_div.theta);
  1967. snd_soc_update_bits(codec, WM8996_FLL_CONTROL_4,
  1968. WM8996_FLL_N_MASK | WM8996_FLL_LOOP_GAIN_MASK,
  1969. (fll_div.n << WM8996_FLL_N_SHIFT) |
  1970. fll_div.fll_loop_gain);
  1971. snd_soc_write(codec, WM8996_FLL_EFS_1, fll_div.lambda);
  1972. /* Enable the bandgap if it's not already enabled */
  1973. ret = snd_soc_read(codec, WM8996_FLL_CONTROL_1);
  1974. if (!(ret & WM8996_FLL_ENA))
  1975. wm8996_bg_enable(codec);
  1976. /* Clear any pending completions (eg, from failed startups) */
  1977. try_wait_for_completion(&wm8996->fll_lock);
  1978. snd_soc_update_bits(codec, WM8996_FLL_CONTROL_1,
  1979. WM8996_FLL_ENA, WM8996_FLL_ENA);
  1980. /* The FLL supports live reconfiguration - kick that in case we were
  1981. * already enabled.
  1982. */
  1983. snd_soc_write(codec, WM8996_FLL_CONTROL_6, WM8996_FLL_SWITCH_CLK);
  1984. /* Wait for the FLL to lock, using the interrupt if possible */
  1985. if (Fref > 1000000)
  1986. timeout = usecs_to_jiffies(300);
  1987. else
  1988. timeout = msecs_to_jiffies(2);
  1989. /* Allow substantially longer if we've actually got the IRQ, poll
  1990. * at a slightly higher rate if we don't.
  1991. */
  1992. if (i2c->irq)
  1993. timeout *= 10;
  1994. else
  1995. timeout /= 2;
  1996. for (retry = 0; retry < 10; retry++) {
  1997. ret = wait_for_completion_timeout(&wm8996->fll_lock,
  1998. timeout);
  1999. if (ret != 0) {
  2000. WARN_ON(!i2c->irq);
  2001. break;
  2002. }
  2003. ret = snd_soc_read(codec, WM8996_INTERRUPT_RAW_STATUS_2);
  2004. if (ret & WM8996_FLL_LOCK_STS)
  2005. break;
  2006. }
  2007. if (retry == 10) {
  2008. dev_err(codec->dev, "Timed out waiting for FLL\n");
  2009. ret = -ETIMEDOUT;
  2010. }
  2011. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  2012. wm8996->fll_fref = Fref;
  2013. wm8996->fll_fout = Fout;
  2014. wm8996->fll_src = source;
  2015. return ret;
  2016. }
  2017. #ifdef CONFIG_GPIOLIB
  2018. static inline struct wm8996_priv *gpio_to_wm8996(struct gpio_chip *chip)
  2019. {
  2020. return container_of(chip, struct wm8996_priv, gpio_chip);
  2021. }
  2022. static void wm8996_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  2023. {
  2024. struct wm8996_priv *wm8996 = gpio_to_wm8996(chip);
  2025. regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
  2026. WM8996_GP1_LVL, !!value << WM8996_GP1_LVL_SHIFT);
  2027. }
  2028. static int wm8996_gpio_direction_out(struct gpio_chip *chip,
  2029. unsigned offset, int value)
  2030. {
  2031. struct wm8996_priv *wm8996 = gpio_to_wm8996(chip);
  2032. int val;
  2033. val = (1 << WM8996_GP1_FN_SHIFT) | (!!value << WM8996_GP1_LVL_SHIFT);
  2034. return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
  2035. WM8996_GP1_FN_MASK | WM8996_GP1_DIR |
  2036. WM8996_GP1_LVL, val);
  2037. }
  2038. static int wm8996_gpio_get(struct gpio_chip *chip, unsigned offset)
  2039. {
  2040. struct wm8996_priv *wm8996 = gpio_to_wm8996(chip);
  2041. unsigned int reg;
  2042. int ret;
  2043. ret = regmap_read(wm8996->regmap, WM8996_GPIO_1 + offset, &reg);
  2044. if (ret < 0)
  2045. return ret;
  2046. return (reg & WM8996_GP1_LVL) != 0;
  2047. }
  2048. static int wm8996_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  2049. {
  2050. struct wm8996_priv *wm8996 = gpio_to_wm8996(chip);
  2051. return regmap_update_bits(wm8996->regmap, WM8996_GPIO_1 + offset,
  2052. WM8996_GP1_FN_MASK | WM8996_GP1_DIR,
  2053. (1 << WM8996_GP1_FN_SHIFT) |
  2054. (1 << WM8996_GP1_DIR_SHIFT));
  2055. }
  2056. static struct gpio_chip wm8996_template_chip = {
  2057. .label = "wm8996",
  2058. .owner = THIS_MODULE,
  2059. .direction_output = wm8996_gpio_direction_out,
  2060. .set = wm8996_gpio_set,
  2061. .direction_input = wm8996_gpio_direction_in,
  2062. .get = wm8996_gpio_get,
  2063. .can_sleep = 1,
  2064. };
  2065. static void wm8996_init_gpio(struct wm8996_priv *wm8996)
  2066. {
  2067. int ret;
  2068. wm8996->gpio_chip = wm8996_template_chip;
  2069. wm8996->gpio_chip.ngpio = 5;
  2070. wm8996->gpio_chip.dev = wm8996->dev;
  2071. if (wm8996->pdata.gpio_base)
  2072. wm8996->gpio_chip.base = wm8996->pdata.gpio_base;
  2073. else
  2074. wm8996->gpio_chip.base = -1;
  2075. ret = gpiochip_add(&wm8996->gpio_chip);
  2076. if (ret != 0)
  2077. dev_err(wm8996->dev, "Failed to add GPIOs: %d\n", ret);
  2078. }
  2079. static void wm8996_free_gpio(struct wm8996_priv *wm8996)
  2080. {
  2081. int ret;
  2082. ret = gpiochip_remove(&wm8996->gpio_chip);
  2083. if (ret != 0)
  2084. dev_err(wm8996->dev, "Failed to remove GPIOs: %d\n", ret);
  2085. }
  2086. #else
  2087. static void wm8996_init_gpio(struct wm8996_priv *wm8996)
  2088. {
  2089. }
  2090. static void wm8996_free_gpio(struct wm8996_priv *wm8996)
  2091. {
  2092. }
  2093. #endif
  2094. /**
  2095. * wm8996_detect - Enable default WM8996 jack detection
  2096. *
  2097. * The WM8996 has advanced accessory detection support for headsets.
  2098. * This function provides a default implementation which integrates
  2099. * the majority of this functionality with minimal user configuration.
  2100. *
  2101. * This will detect headset, headphone and short circuit button and
  2102. * will also detect inverted microphone ground connections and update
  2103. * the polarity of the connections.
  2104. */
  2105. int wm8996_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
  2106. wm8996_polarity_fn polarity_cb)
  2107. {
  2108. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2109. wm8996->jack = jack;
  2110. wm8996->detecting = true;
  2111. wm8996->polarity_cb = polarity_cb;
  2112. wm8996->jack_flips = 0;
  2113. if (wm8996->polarity_cb)
  2114. wm8996->polarity_cb(codec, 0);
  2115. /* Clear discarge to avoid noise during detection */
  2116. snd_soc_update_bits(codec, WM8996_MICBIAS_1,
  2117. WM8996_MICB1_DISCH, 0);
  2118. snd_soc_update_bits(codec, WM8996_MICBIAS_2,
  2119. WM8996_MICB2_DISCH, 0);
  2120. /* LDO2 powers the microphones, SYSCLK clocks detection */
  2121. snd_soc_dapm_force_enable_pin(&codec->dapm, "LDO2");
  2122. snd_soc_dapm_force_enable_pin(&codec->dapm, "SYSCLK");
  2123. /* We start off just enabling microphone detection - even a
  2124. * plain headphone will trigger detection.
  2125. */
  2126. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1,
  2127. WM8996_MICD_ENA, WM8996_MICD_ENA);
  2128. /* Slowest detection rate, gives debounce for initial detection */
  2129. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1,
  2130. WM8996_MICD_RATE_MASK,
  2131. WM8996_MICD_RATE_MASK);
  2132. /* Enable interrupts and we're off */
  2133. snd_soc_update_bits(codec, WM8996_INTERRUPT_STATUS_2_MASK,
  2134. WM8996_IM_MICD_EINT | WM8996_HP_DONE_EINT, 0);
  2135. return 0;
  2136. }
  2137. EXPORT_SYMBOL_GPL(wm8996_detect);
  2138. static void wm8996_hpdet_irq(struct snd_soc_codec *codec)
  2139. {
  2140. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2141. int val, reg, report;
  2142. /* Assume headphone in error conditions; we need to report
  2143. * something or we stall our state machine.
  2144. */
  2145. report = SND_JACK_HEADPHONE;
  2146. reg = snd_soc_read(codec, WM8996_HEADPHONE_DETECT_2);
  2147. if (reg < 0) {
  2148. dev_err(codec->dev, "Failed to read HPDET status\n");
  2149. goto out;
  2150. }
  2151. if (!(reg & WM8996_HP_DONE)) {
  2152. dev_err(codec->dev, "Got HPDET IRQ but HPDET is busy\n");
  2153. goto out;
  2154. }
  2155. val = reg & WM8996_HP_LVL_MASK;
  2156. dev_dbg(codec->dev, "HPDET measured %d ohms\n", val);
  2157. /* If we've got high enough impedence then report as line,
  2158. * otherwise assume headphone.
  2159. */
  2160. if (val >= 126)
  2161. report = SND_JACK_LINEOUT;
  2162. else
  2163. report = SND_JACK_HEADPHONE;
  2164. out:
  2165. if (wm8996->jack_mic)
  2166. report |= SND_JACK_MICROPHONE;
  2167. snd_soc_jack_report(wm8996->jack, report,
  2168. SND_JACK_LINEOUT | SND_JACK_HEADSET);
  2169. wm8996->detecting = false;
  2170. /* If the output isn't running re-clamp it */
  2171. if (!(snd_soc_read(codec, WM8996_POWER_MANAGEMENT_1) &
  2172. (WM8996_HPOUT1L_ENA | WM8996_HPOUT1R_RMV_SHORT)))
  2173. snd_soc_update_bits(codec, WM8996_ANALOGUE_HP_1,
  2174. WM8996_HPOUT1L_RMV_SHORT |
  2175. WM8996_HPOUT1R_RMV_SHORT, 0);
  2176. /* Go back to looking at the microphone */
  2177. snd_soc_update_bits(codec, WM8996_ACCESSORY_DETECT_MODE_1,
  2178. WM8996_JD_MODE_MASK, 0);
  2179. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1, WM8996_MICD_ENA,
  2180. WM8996_MICD_ENA);
  2181. snd_soc_dapm_disable_pin(&codec->dapm, "Bandgap");
  2182. snd_soc_dapm_sync(&codec->dapm);
  2183. }
  2184. static void wm8996_hpdet_start(struct snd_soc_codec *codec)
  2185. {
  2186. /* Unclamp the output, we can't measure while we're shorting it */
  2187. snd_soc_update_bits(codec, WM8996_ANALOGUE_HP_1,
  2188. WM8996_HPOUT1L_RMV_SHORT |
  2189. WM8996_HPOUT1R_RMV_SHORT,
  2190. WM8996_HPOUT1L_RMV_SHORT |
  2191. WM8996_HPOUT1R_RMV_SHORT);
  2192. /* We need bandgap for HPDET */
  2193. snd_soc_dapm_force_enable_pin(&codec->dapm, "Bandgap");
  2194. snd_soc_dapm_sync(&codec->dapm);
  2195. /* Go into headphone detect left mode */
  2196. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1, WM8996_MICD_ENA, 0);
  2197. snd_soc_update_bits(codec, WM8996_ACCESSORY_DETECT_MODE_1,
  2198. WM8996_JD_MODE_MASK, 1);
  2199. /* Trigger a measurement */
  2200. snd_soc_update_bits(codec, WM8996_HEADPHONE_DETECT_1,
  2201. WM8996_HP_POLL, WM8996_HP_POLL);
  2202. }
  2203. static void wm8996_report_headphone(struct snd_soc_codec *codec)
  2204. {
  2205. dev_dbg(codec->dev, "Headphone detected\n");
  2206. wm8996_hpdet_start(codec);
  2207. /* Increase the detection rate a bit for responsiveness. */
  2208. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1,
  2209. WM8996_MICD_RATE_MASK |
  2210. WM8996_MICD_BIAS_STARTTIME_MASK,
  2211. 7 << WM8996_MICD_RATE_SHIFT |
  2212. 7 << WM8996_MICD_BIAS_STARTTIME_SHIFT);
  2213. }
  2214. static void wm8996_micd(struct snd_soc_codec *codec)
  2215. {
  2216. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2217. int val, reg;
  2218. val = snd_soc_read(codec, WM8996_MIC_DETECT_3);
  2219. dev_dbg(codec->dev, "Microphone event: %x\n", val);
  2220. if (!(val & WM8996_MICD_VALID)) {
  2221. dev_warn(codec->dev, "Microphone detection state invalid\n");
  2222. return;
  2223. }
  2224. /* No accessory, reset everything and report removal */
  2225. if (!(val & WM8996_MICD_STS)) {
  2226. dev_dbg(codec->dev, "Jack removal detected\n");
  2227. wm8996->jack_mic = false;
  2228. wm8996->detecting = true;
  2229. wm8996->jack_flips = 0;
  2230. snd_soc_jack_report(wm8996->jack, 0,
  2231. SND_JACK_LINEOUT | SND_JACK_HEADSET |
  2232. SND_JACK_BTN_0);
  2233. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1,
  2234. WM8996_MICD_RATE_MASK |
  2235. WM8996_MICD_BIAS_STARTTIME_MASK,
  2236. WM8996_MICD_RATE_MASK |
  2237. 9 << WM8996_MICD_BIAS_STARTTIME_SHIFT);
  2238. return;
  2239. }
  2240. /* If the measurement is very high we've got a microphone,
  2241. * either we just detected one or if we already reported then
  2242. * we've got a button release event.
  2243. */
  2244. if (val & 0x400) {
  2245. if (wm8996->detecting) {
  2246. dev_dbg(codec->dev, "Microphone detected\n");
  2247. wm8996->jack_mic = true;
  2248. wm8996_hpdet_start(codec);
  2249. /* Increase poll rate to give better responsiveness
  2250. * for buttons */
  2251. snd_soc_update_bits(codec, WM8996_MIC_DETECT_1,
  2252. WM8996_MICD_RATE_MASK |
  2253. WM8996_MICD_BIAS_STARTTIME_MASK,
  2254. 5 << WM8996_MICD_RATE_SHIFT |
  2255. 7 << WM8996_MICD_BIAS_STARTTIME_SHIFT);
  2256. } else {
  2257. dev_dbg(codec->dev, "Mic button up\n");
  2258. snd_soc_jack_report(wm8996->jack, 0, SND_JACK_BTN_0);
  2259. }
  2260. return;
  2261. }
  2262. /* If we detected a lower impedence during initial startup
  2263. * then we probably have the wrong polarity, flip it. Don't
  2264. * do this for the lowest impedences to speed up detection of
  2265. * plain headphones. If both polarities report a low
  2266. * impedence then give up and report headphones.
  2267. */
  2268. if (wm8996->detecting && (val & 0x3f0)) {
  2269. wm8996->jack_flips++;
  2270. if (wm8996->jack_flips > 1) {
  2271. wm8996_report_headphone(codec);
  2272. return;
  2273. }
  2274. reg = snd_soc_read(codec, WM8996_ACCESSORY_DETECT_MODE_2);
  2275. reg ^= WM8996_HPOUT1FB_SRC | WM8996_MICD_SRC |
  2276. WM8996_MICD_BIAS_SRC;
  2277. snd_soc_update_bits(codec, WM8996_ACCESSORY_DETECT_MODE_2,
  2278. WM8996_HPOUT1FB_SRC | WM8996_MICD_SRC |
  2279. WM8996_MICD_BIAS_SRC, reg);
  2280. if (wm8996->polarity_cb)
  2281. wm8996->polarity_cb(codec,
  2282. (reg & WM8996_MICD_SRC) != 0);
  2283. dev_dbg(codec->dev, "Set microphone polarity to %d\n",
  2284. (reg & WM8996_MICD_SRC) != 0);
  2285. return;
  2286. }
  2287. /* Don't distinguish between buttons, just report any low
  2288. * impedence as BTN_0.
  2289. */
  2290. if (val & 0x3fc) {
  2291. if (wm8996->jack_mic) {
  2292. dev_dbg(codec->dev, "Mic button detected\n");
  2293. snd_soc_jack_report(wm8996->jack, SND_JACK_BTN_0,
  2294. SND_JACK_BTN_0);
  2295. } else if (wm8996->detecting) {
  2296. wm8996_report_headphone(codec);
  2297. }
  2298. }
  2299. }
  2300. static irqreturn_t wm8996_irq(int irq, void *data)
  2301. {
  2302. struct snd_soc_codec *codec = data;
  2303. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2304. int irq_val;
  2305. irq_val = snd_soc_read(codec, WM8996_INTERRUPT_STATUS_2);
  2306. if (irq_val < 0) {
  2307. dev_err(codec->dev, "Failed to read IRQ status: %d\n",
  2308. irq_val);
  2309. return IRQ_NONE;
  2310. }
  2311. irq_val &= ~snd_soc_read(codec, WM8996_INTERRUPT_STATUS_2_MASK);
  2312. if (!irq_val)
  2313. return IRQ_NONE;
  2314. snd_soc_write(codec, WM8996_INTERRUPT_STATUS_2, irq_val);
  2315. if (irq_val & (WM8996_DCS_DONE_01_EINT | WM8996_DCS_DONE_23_EINT)) {
  2316. dev_dbg(codec->dev, "DC servo IRQ\n");
  2317. complete(&wm8996->dcs_done);
  2318. }
  2319. if (irq_val & WM8996_FIFOS_ERR_EINT)
  2320. dev_err(codec->dev, "Digital core FIFO error\n");
  2321. if (irq_val & WM8996_FLL_LOCK_EINT) {
  2322. dev_dbg(codec->dev, "FLL locked\n");
  2323. complete(&wm8996->fll_lock);
  2324. }
  2325. if (irq_val & WM8996_MICD_EINT)
  2326. wm8996_micd(codec);
  2327. if (irq_val & WM8996_HP_DONE_EINT)
  2328. wm8996_hpdet_irq(codec);
  2329. return IRQ_HANDLED;
  2330. }
  2331. static irqreturn_t wm8996_edge_irq(int irq, void *data)
  2332. {
  2333. irqreturn_t ret = IRQ_NONE;
  2334. irqreturn_t val;
  2335. do {
  2336. val = wm8996_irq(irq, data);
  2337. if (val != IRQ_NONE)
  2338. ret = val;
  2339. } while (val != IRQ_NONE);
  2340. return ret;
  2341. }
  2342. static void wm8996_retune_mobile_pdata(struct snd_soc_codec *codec)
  2343. {
  2344. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2345. struct wm8996_pdata *pdata = &wm8996->pdata;
  2346. struct snd_kcontrol_new controls[] = {
  2347. SOC_ENUM_EXT("DSP1 EQ Mode",
  2348. wm8996->retune_mobile_enum,
  2349. wm8996_get_retune_mobile_enum,
  2350. wm8996_put_retune_mobile_enum),
  2351. SOC_ENUM_EXT("DSP2 EQ Mode",
  2352. wm8996->retune_mobile_enum,
  2353. wm8996_get_retune_mobile_enum,
  2354. wm8996_put_retune_mobile_enum),
  2355. };
  2356. int ret, i, j;
  2357. const char **t;
  2358. /* We need an array of texts for the enum API but the number
  2359. * of texts is likely to be less than the number of
  2360. * configurations due to the sample rate dependency of the
  2361. * configurations. */
  2362. wm8996->num_retune_mobile_texts = 0;
  2363. wm8996->retune_mobile_texts = NULL;
  2364. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  2365. for (j = 0; j < wm8996->num_retune_mobile_texts; j++) {
  2366. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  2367. wm8996->retune_mobile_texts[j]) == 0)
  2368. break;
  2369. }
  2370. if (j != wm8996->num_retune_mobile_texts)
  2371. continue;
  2372. /* Expand the array... */
  2373. t = krealloc(wm8996->retune_mobile_texts,
  2374. sizeof(char *) *
  2375. (wm8996->num_retune_mobile_texts + 1),
  2376. GFP_KERNEL);
  2377. if (t == NULL)
  2378. continue;
  2379. /* ...store the new entry... */
  2380. t[wm8996->num_retune_mobile_texts] =
  2381. pdata->retune_mobile_cfgs[i].name;
  2382. /* ...and remember the new version. */
  2383. wm8996->num_retune_mobile_texts++;
  2384. wm8996->retune_mobile_texts = t;
  2385. }
  2386. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  2387. wm8996->num_retune_mobile_texts);
  2388. wm8996->retune_mobile_enum.max = wm8996->num_retune_mobile_texts;
  2389. wm8996->retune_mobile_enum.texts = wm8996->retune_mobile_texts;
  2390. ret = snd_soc_add_codec_controls(codec, controls, ARRAY_SIZE(controls));
  2391. if (ret != 0)
  2392. dev_err(codec->dev,
  2393. "Failed to add ReTune Mobile controls: %d\n", ret);
  2394. }
  2395. static const struct regmap_config wm8996_regmap = {
  2396. .reg_bits = 16,
  2397. .val_bits = 16,
  2398. .max_register = WM8996_MAX_REGISTER,
  2399. .reg_defaults = wm8996_reg,
  2400. .num_reg_defaults = ARRAY_SIZE(wm8996_reg),
  2401. .volatile_reg = wm8996_volatile_register,
  2402. .readable_reg = wm8996_readable_register,
  2403. .cache_type = REGCACHE_RBTREE,
  2404. };
  2405. static int wm8996_probe(struct snd_soc_codec *codec)
  2406. {
  2407. int ret;
  2408. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2409. struct i2c_client *i2c = to_i2c_client(codec->dev);
  2410. int i, irq_flags;
  2411. wm8996->codec = codec;
  2412. init_completion(&wm8996->dcs_done);
  2413. init_completion(&wm8996->fll_lock);
  2414. codec->control_data = wm8996->regmap;
  2415. ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_REGMAP);
  2416. if (ret != 0) {
  2417. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  2418. goto err;
  2419. }
  2420. wm8996->disable_nb[0].notifier_call = wm8996_regulator_event_0;
  2421. wm8996->disable_nb[1].notifier_call = wm8996_regulator_event_1;
  2422. wm8996->disable_nb[2].notifier_call = wm8996_regulator_event_2;
  2423. /* This should really be moved into the regulator core */
  2424. for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++) {
  2425. ret = regulator_register_notifier(wm8996->supplies[i].consumer,
  2426. &wm8996->disable_nb[i]);
  2427. if (ret != 0) {
  2428. dev_err(codec->dev,
  2429. "Failed to register regulator notifier: %d\n",
  2430. ret);
  2431. }
  2432. }
  2433. regcache_cache_only(codec->control_data, true);
  2434. /* Apply platform data settings */
  2435. snd_soc_update_bits(codec, WM8996_LINE_INPUT_CONTROL,
  2436. WM8996_INL_MODE_MASK | WM8996_INR_MODE_MASK,
  2437. wm8996->pdata.inl_mode << WM8996_INL_MODE_SHIFT |
  2438. wm8996->pdata.inr_mode);
  2439. for (i = 0; i < ARRAY_SIZE(wm8996->pdata.gpio_default); i++) {
  2440. if (!wm8996->pdata.gpio_default[i])
  2441. continue;
  2442. snd_soc_write(codec, WM8996_GPIO_1 + i,
  2443. wm8996->pdata.gpio_default[i] & 0xffff);
  2444. }
  2445. if (wm8996->pdata.spkmute_seq)
  2446. snd_soc_update_bits(codec, WM8996_PDM_SPEAKER_MUTE_SEQUENCE,
  2447. WM8996_SPK_MUTE_ENDIAN |
  2448. WM8996_SPK_MUTE_SEQ1_MASK,
  2449. wm8996->pdata.spkmute_seq);
  2450. snd_soc_update_bits(codec, WM8996_ACCESSORY_DETECT_MODE_2,
  2451. WM8996_MICD_BIAS_SRC | WM8996_HPOUT1FB_SRC |
  2452. WM8996_MICD_SRC, wm8996->pdata.micdet_def);
  2453. /* Latch volume update bits */
  2454. snd_soc_update_bits(codec, WM8996_LEFT_LINE_INPUT_VOLUME,
  2455. WM8996_IN1_VU, WM8996_IN1_VU);
  2456. snd_soc_update_bits(codec, WM8996_RIGHT_LINE_INPUT_VOLUME,
  2457. WM8996_IN1_VU, WM8996_IN1_VU);
  2458. snd_soc_update_bits(codec, WM8996_DAC1_LEFT_VOLUME,
  2459. WM8996_DAC1_VU, WM8996_DAC1_VU);
  2460. snd_soc_update_bits(codec, WM8996_DAC1_RIGHT_VOLUME,
  2461. WM8996_DAC1_VU, WM8996_DAC1_VU);
  2462. snd_soc_update_bits(codec, WM8996_DAC2_LEFT_VOLUME,
  2463. WM8996_DAC2_VU, WM8996_DAC2_VU);
  2464. snd_soc_update_bits(codec, WM8996_DAC2_RIGHT_VOLUME,
  2465. WM8996_DAC2_VU, WM8996_DAC2_VU);
  2466. snd_soc_update_bits(codec, WM8996_OUTPUT1_LEFT_VOLUME,
  2467. WM8996_DAC1_VU, WM8996_DAC1_VU);
  2468. snd_soc_update_bits(codec, WM8996_OUTPUT1_RIGHT_VOLUME,
  2469. WM8996_DAC1_VU, WM8996_DAC1_VU);
  2470. snd_soc_update_bits(codec, WM8996_OUTPUT2_LEFT_VOLUME,
  2471. WM8996_DAC2_VU, WM8996_DAC2_VU);
  2472. snd_soc_update_bits(codec, WM8996_OUTPUT2_RIGHT_VOLUME,
  2473. WM8996_DAC2_VU, WM8996_DAC2_VU);
  2474. snd_soc_update_bits(codec, WM8996_DSP1_TX_LEFT_VOLUME,
  2475. WM8996_DSP1TX_VU, WM8996_DSP1TX_VU);
  2476. snd_soc_update_bits(codec, WM8996_DSP1_TX_RIGHT_VOLUME,
  2477. WM8996_DSP1TX_VU, WM8996_DSP1TX_VU);
  2478. snd_soc_update_bits(codec, WM8996_DSP2_TX_LEFT_VOLUME,
  2479. WM8996_DSP2TX_VU, WM8996_DSP2TX_VU);
  2480. snd_soc_update_bits(codec, WM8996_DSP2_TX_RIGHT_VOLUME,
  2481. WM8996_DSP2TX_VU, WM8996_DSP2TX_VU);
  2482. snd_soc_update_bits(codec, WM8996_DSP1_RX_LEFT_VOLUME,
  2483. WM8996_DSP1RX_VU, WM8996_DSP1RX_VU);
  2484. snd_soc_update_bits(codec, WM8996_DSP1_RX_RIGHT_VOLUME,
  2485. WM8996_DSP1RX_VU, WM8996_DSP1RX_VU);
  2486. snd_soc_update_bits(codec, WM8996_DSP2_RX_LEFT_VOLUME,
  2487. WM8996_DSP2RX_VU, WM8996_DSP2RX_VU);
  2488. snd_soc_update_bits(codec, WM8996_DSP2_RX_RIGHT_VOLUME,
  2489. WM8996_DSP2RX_VU, WM8996_DSP2RX_VU);
  2490. /* No support currently for the underclocked TDM modes and
  2491. * pick a default TDM layout with each channel pair working with
  2492. * slots 0 and 1. */
  2493. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_0_CONFIGURATION,
  2494. WM8996_AIF1RX_CHAN0_SLOTS_MASK |
  2495. WM8996_AIF1RX_CHAN0_START_SLOT_MASK,
  2496. 1 << WM8996_AIF1RX_CHAN0_SLOTS_SHIFT | 0);
  2497. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_1_CONFIGURATION,
  2498. WM8996_AIF1RX_CHAN1_SLOTS_MASK |
  2499. WM8996_AIF1RX_CHAN1_START_SLOT_MASK,
  2500. 1 << WM8996_AIF1RX_CHAN1_SLOTS_SHIFT | 1);
  2501. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_2_CONFIGURATION,
  2502. WM8996_AIF1RX_CHAN2_SLOTS_MASK |
  2503. WM8996_AIF1RX_CHAN2_START_SLOT_MASK,
  2504. 1 << WM8996_AIF1RX_CHAN2_SLOTS_SHIFT | 0);
  2505. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_3_CONFIGURATION,
  2506. WM8996_AIF1RX_CHAN3_SLOTS_MASK |
  2507. WM8996_AIF1RX_CHAN0_START_SLOT_MASK,
  2508. 1 << WM8996_AIF1RX_CHAN3_SLOTS_SHIFT | 1);
  2509. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_4_CONFIGURATION,
  2510. WM8996_AIF1RX_CHAN4_SLOTS_MASK |
  2511. WM8996_AIF1RX_CHAN0_START_SLOT_MASK,
  2512. 1 << WM8996_AIF1RX_CHAN4_SLOTS_SHIFT | 0);
  2513. snd_soc_update_bits(codec, WM8996_AIF1RX_CHANNEL_5_CONFIGURATION,
  2514. WM8996_AIF1RX_CHAN5_SLOTS_MASK |
  2515. WM8996_AIF1RX_CHAN0_START_SLOT_MASK,
  2516. 1 << WM8996_AIF1RX_CHAN5_SLOTS_SHIFT | 1);
  2517. snd_soc_update_bits(codec, WM8996_AIF2RX_CHANNEL_0_CONFIGURATION,
  2518. WM8996_AIF2RX_CHAN0_SLOTS_MASK |
  2519. WM8996_AIF2RX_CHAN0_START_SLOT_MASK,
  2520. 1 << WM8996_AIF2RX_CHAN0_SLOTS_SHIFT | 0);
  2521. snd_soc_update_bits(codec, WM8996_AIF2RX_CHANNEL_1_CONFIGURATION,
  2522. WM8996_AIF2RX_CHAN1_SLOTS_MASK |
  2523. WM8996_AIF2RX_CHAN1_START_SLOT_MASK,
  2524. 1 << WM8996_AIF2RX_CHAN1_SLOTS_SHIFT | 1);
  2525. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_0_CONFIGURATION,
  2526. WM8996_AIF1TX_CHAN0_SLOTS_MASK |
  2527. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2528. 1 << WM8996_AIF1TX_CHAN0_SLOTS_SHIFT | 0);
  2529. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_1_CONFIGURATION,
  2530. WM8996_AIF1TX_CHAN1_SLOTS_MASK |
  2531. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2532. 1 << WM8996_AIF1TX_CHAN1_SLOTS_SHIFT | 1);
  2533. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_2_CONFIGURATION,
  2534. WM8996_AIF1TX_CHAN2_SLOTS_MASK |
  2535. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2536. 1 << WM8996_AIF1TX_CHAN2_SLOTS_SHIFT | 0);
  2537. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_3_CONFIGURATION,
  2538. WM8996_AIF1TX_CHAN3_SLOTS_MASK |
  2539. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2540. 1 << WM8996_AIF1TX_CHAN3_SLOTS_SHIFT | 1);
  2541. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_4_CONFIGURATION,
  2542. WM8996_AIF1TX_CHAN4_SLOTS_MASK |
  2543. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2544. 1 << WM8996_AIF1TX_CHAN4_SLOTS_SHIFT | 0);
  2545. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_5_CONFIGURATION,
  2546. WM8996_AIF1TX_CHAN5_SLOTS_MASK |
  2547. WM8996_AIF1TX_CHAN0_START_SLOT_MASK,
  2548. 1 << WM8996_AIF1TX_CHAN5_SLOTS_SHIFT | 1);
  2549. snd_soc_update_bits(codec, WM8996_AIF2TX_CHANNEL_0_CONFIGURATION,
  2550. WM8996_AIF2TX_CHAN0_SLOTS_MASK |
  2551. WM8996_AIF2TX_CHAN0_START_SLOT_MASK,
  2552. 1 << WM8996_AIF2TX_CHAN0_SLOTS_SHIFT | 0);
  2553. snd_soc_update_bits(codec, WM8996_AIF1TX_CHANNEL_1_CONFIGURATION,
  2554. WM8996_AIF2TX_CHAN1_SLOTS_MASK |
  2555. WM8996_AIF2TX_CHAN1_START_SLOT_MASK,
  2556. 1 << WM8996_AIF1TX_CHAN1_SLOTS_SHIFT | 1);
  2557. if (wm8996->pdata.num_retune_mobile_cfgs)
  2558. wm8996_retune_mobile_pdata(codec);
  2559. else
  2560. snd_soc_add_codec_controls(codec, wm8996_eq_controls,
  2561. ARRAY_SIZE(wm8996_eq_controls));
  2562. /* If the TX LRCLK pins are not in LRCLK mode configure the
  2563. * AIFs to source their clocks from the RX LRCLKs.
  2564. */
  2565. if ((snd_soc_read(codec, WM8996_GPIO_1)))
  2566. snd_soc_update_bits(codec, WM8996_AIF1_TX_LRCLK_2,
  2567. WM8996_AIF1TX_LRCLK_MODE,
  2568. WM8996_AIF1TX_LRCLK_MODE);
  2569. if ((snd_soc_read(codec, WM8996_GPIO_2)))
  2570. snd_soc_update_bits(codec, WM8996_AIF2_TX_LRCLK_2,
  2571. WM8996_AIF2TX_LRCLK_MODE,
  2572. WM8996_AIF2TX_LRCLK_MODE);
  2573. if (i2c->irq) {
  2574. if (wm8996->pdata.irq_flags)
  2575. irq_flags = wm8996->pdata.irq_flags;
  2576. else
  2577. irq_flags = IRQF_TRIGGER_LOW;
  2578. irq_flags |= IRQF_ONESHOT;
  2579. if (irq_flags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))
  2580. ret = request_threaded_irq(i2c->irq, NULL,
  2581. wm8996_edge_irq,
  2582. irq_flags, "wm8996", codec);
  2583. else
  2584. ret = request_threaded_irq(i2c->irq, NULL, wm8996_irq,
  2585. irq_flags, "wm8996", codec);
  2586. if (ret == 0) {
  2587. /* Unmask the interrupt */
  2588. snd_soc_update_bits(codec, WM8996_INTERRUPT_CONTROL,
  2589. WM8996_IM_IRQ, 0);
  2590. /* Enable error reporting and DC servo status */
  2591. snd_soc_update_bits(codec,
  2592. WM8996_INTERRUPT_STATUS_2_MASK,
  2593. WM8996_IM_DCS_DONE_23_EINT |
  2594. WM8996_IM_DCS_DONE_01_EINT |
  2595. WM8996_IM_FLL_LOCK_EINT |
  2596. WM8996_IM_FIFOS_ERR_EINT,
  2597. 0);
  2598. } else {
  2599. dev_err(codec->dev, "Failed to request IRQ: %d\n",
  2600. ret);
  2601. }
  2602. }
  2603. return 0;
  2604. err:
  2605. return ret;
  2606. }
  2607. static int wm8996_remove(struct snd_soc_codec *codec)
  2608. {
  2609. struct wm8996_priv *wm8996 = snd_soc_codec_get_drvdata(codec);
  2610. struct i2c_client *i2c = to_i2c_client(codec->dev);
  2611. int i;
  2612. snd_soc_update_bits(codec, WM8996_INTERRUPT_CONTROL,
  2613. WM8996_IM_IRQ, WM8996_IM_IRQ);
  2614. if (i2c->irq)
  2615. free_irq(i2c->irq, codec);
  2616. for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++)
  2617. regulator_unregister_notifier(wm8996->supplies[i].consumer,
  2618. &wm8996->disable_nb[i]);
  2619. regulator_bulk_free(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
  2620. return 0;
  2621. }
  2622. static struct snd_soc_codec_driver soc_codec_dev_wm8996 = {
  2623. .probe = wm8996_probe,
  2624. .remove = wm8996_remove,
  2625. .set_bias_level = wm8996_set_bias_level,
  2626. .idle_bias_off = true,
  2627. .seq_notifier = wm8996_seq_notifier,
  2628. .controls = wm8996_snd_controls,
  2629. .num_controls = ARRAY_SIZE(wm8996_snd_controls),
  2630. .dapm_widgets = wm8996_dapm_widgets,
  2631. .num_dapm_widgets = ARRAY_SIZE(wm8996_dapm_widgets),
  2632. .dapm_routes = wm8996_dapm_routes,
  2633. .num_dapm_routes = ARRAY_SIZE(wm8996_dapm_routes),
  2634. .set_pll = wm8996_set_fll,
  2635. };
  2636. #define WM8996_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  2637. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  2638. SNDRV_PCM_RATE_48000)
  2639. #define WM8996_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  2640. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
  2641. SNDRV_PCM_FMTBIT_S32_LE)
  2642. static const struct snd_soc_dai_ops wm8996_dai_ops = {
  2643. .set_fmt = wm8996_set_fmt,
  2644. .hw_params = wm8996_hw_params,
  2645. .set_sysclk = wm8996_set_sysclk,
  2646. };
  2647. static struct snd_soc_dai_driver wm8996_dai[] = {
  2648. {
  2649. .name = "wm8996-aif1",
  2650. .playback = {
  2651. .stream_name = "AIF1 Playback",
  2652. .channels_min = 1,
  2653. .channels_max = 6,
  2654. .rates = WM8996_RATES,
  2655. .formats = WM8996_FORMATS,
  2656. .sig_bits = 24,
  2657. },
  2658. .capture = {
  2659. .stream_name = "AIF1 Capture",
  2660. .channels_min = 1,
  2661. .channels_max = 6,
  2662. .rates = WM8996_RATES,
  2663. .formats = WM8996_FORMATS,
  2664. .sig_bits = 24,
  2665. },
  2666. .ops = &wm8996_dai_ops,
  2667. },
  2668. {
  2669. .name = "wm8996-aif2",
  2670. .playback = {
  2671. .stream_name = "AIF2 Playback",
  2672. .channels_min = 1,
  2673. .channels_max = 2,
  2674. .rates = WM8996_RATES,
  2675. .formats = WM8996_FORMATS,
  2676. .sig_bits = 24,
  2677. },
  2678. .capture = {
  2679. .stream_name = "AIF2 Capture",
  2680. .channels_min = 1,
  2681. .channels_max = 2,
  2682. .rates = WM8996_RATES,
  2683. .formats = WM8996_FORMATS,
  2684. .sig_bits = 24,
  2685. },
  2686. .ops = &wm8996_dai_ops,
  2687. },
  2688. };
  2689. static __devinit int wm8996_i2c_probe(struct i2c_client *i2c,
  2690. const struct i2c_device_id *id)
  2691. {
  2692. struct wm8996_priv *wm8996;
  2693. int ret, i;
  2694. unsigned int reg;
  2695. wm8996 = devm_kzalloc(&i2c->dev, sizeof(struct wm8996_priv),
  2696. GFP_KERNEL);
  2697. if (wm8996 == NULL)
  2698. return -ENOMEM;
  2699. i2c_set_clientdata(i2c, wm8996);
  2700. wm8996->dev = &i2c->dev;
  2701. if (dev_get_platdata(&i2c->dev))
  2702. memcpy(&wm8996->pdata, dev_get_platdata(&i2c->dev),
  2703. sizeof(wm8996->pdata));
  2704. if (wm8996->pdata.ldo_ena > 0) {
  2705. ret = gpio_request_one(wm8996->pdata.ldo_ena,
  2706. GPIOF_OUT_INIT_LOW, "WM8996 ENA");
  2707. if (ret < 0) {
  2708. dev_err(&i2c->dev, "Failed to request GPIO %d: %d\n",
  2709. wm8996->pdata.ldo_ena, ret);
  2710. goto err;
  2711. }
  2712. }
  2713. for (i = 0; i < ARRAY_SIZE(wm8996->supplies); i++)
  2714. wm8996->supplies[i].supply = wm8996_supply_names[i];
  2715. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8996->supplies),
  2716. wm8996->supplies);
  2717. if (ret != 0) {
  2718. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  2719. goto err_gpio;
  2720. }
  2721. ret = regulator_bulk_enable(ARRAY_SIZE(wm8996->supplies),
  2722. wm8996->supplies);
  2723. if (ret != 0) {
  2724. dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
  2725. goto err_gpio;
  2726. }
  2727. if (wm8996->pdata.ldo_ena > 0) {
  2728. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 1);
  2729. msleep(5);
  2730. }
  2731. wm8996->regmap = regmap_init_i2c(i2c, &wm8996_regmap);
  2732. if (IS_ERR(wm8996->regmap)) {
  2733. ret = PTR_ERR(wm8996->regmap);
  2734. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  2735. goto err_enable;
  2736. }
  2737. ret = regmap_read(wm8996->regmap, WM8996_SOFTWARE_RESET, &reg);
  2738. if (ret < 0) {
  2739. dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
  2740. goto err_regmap;
  2741. }
  2742. if (reg != 0x8915) {
  2743. dev_err(&i2c->dev, "Device is not a WM8996, ID %x\n", reg);
  2744. ret = -EINVAL;
  2745. goto err_regmap;
  2746. }
  2747. ret = regmap_read(wm8996->regmap, WM8996_CHIP_REVISION, &reg);
  2748. if (ret < 0) {
  2749. dev_err(&i2c->dev, "Failed to read device revision: %d\n",
  2750. ret);
  2751. goto err_regmap;
  2752. }
  2753. dev_info(&i2c->dev, "revision %c\n",
  2754. (reg & WM8996_CHIP_REV_MASK) + 'A');
  2755. regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
  2756. ret = wm8996_reset(wm8996);
  2757. if (ret < 0) {
  2758. dev_err(&i2c->dev, "Failed to issue reset\n");
  2759. goto err_regmap;
  2760. }
  2761. wm8996_init_gpio(wm8996);
  2762. ret = snd_soc_register_codec(&i2c->dev,
  2763. &soc_codec_dev_wm8996, wm8996_dai,
  2764. ARRAY_SIZE(wm8996_dai));
  2765. if (ret < 0)
  2766. goto err_gpiolib;
  2767. return ret;
  2768. err_gpiolib:
  2769. wm8996_free_gpio(wm8996);
  2770. err_regmap:
  2771. regmap_exit(wm8996->regmap);
  2772. err_enable:
  2773. if (wm8996->pdata.ldo_ena > 0)
  2774. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 0);
  2775. regulator_bulk_disable(ARRAY_SIZE(wm8996->supplies), wm8996->supplies);
  2776. err_gpio:
  2777. if (wm8996->pdata.ldo_ena > 0)
  2778. gpio_free(wm8996->pdata.ldo_ena);
  2779. err:
  2780. return ret;
  2781. }
  2782. static __devexit int wm8996_i2c_remove(struct i2c_client *client)
  2783. {
  2784. struct wm8996_priv *wm8996 = i2c_get_clientdata(client);
  2785. snd_soc_unregister_codec(&client->dev);
  2786. wm8996_free_gpio(wm8996);
  2787. regmap_exit(wm8996->regmap);
  2788. if (wm8996->pdata.ldo_ena > 0) {
  2789. gpio_set_value_cansleep(wm8996->pdata.ldo_ena, 0);
  2790. gpio_free(wm8996->pdata.ldo_ena);
  2791. }
  2792. return 0;
  2793. }
  2794. static const struct i2c_device_id wm8996_i2c_id[] = {
  2795. { "wm8996", 0 },
  2796. { }
  2797. };
  2798. MODULE_DEVICE_TABLE(i2c, wm8996_i2c_id);
  2799. static struct i2c_driver wm8996_i2c_driver = {
  2800. .driver = {
  2801. .name = "wm8996",
  2802. .owner = THIS_MODULE,
  2803. },
  2804. .probe = wm8996_i2c_probe,
  2805. .remove = __devexit_p(wm8996_i2c_remove),
  2806. .id_table = wm8996_i2c_id,
  2807. };
  2808. module_i2c_driver(wm8996_i2c_driver);
  2809. MODULE_DESCRIPTION("ASoC WM8996 driver");
  2810. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  2811. MODULE_LICENSE("GPL");