aqt1000.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/firmware.h>
  8. #include <linux/slab.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/device.h>
  11. #include <linux/printk.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/wait.h>
  15. #include <linux/bitops.h>
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/gpio.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/tlv.h>
  27. #include <sound/info.h>
  28. #include <asoc/wcdcal-hwdep.h>
  29. #include "aqt1000-registers.h"
  30. #include "aqt1000.h"
  31. #include "aqt1000-api.h"
  32. #include "aqt1000-mbhc.h"
  33. #include "aqt1000-routing.h"
  34. #include "aqt1000-internal.h"
  35. #define DRV_NAME "aqt_codec"
  36. #define AQT1000_TX_UNMUTE_DELAY_MS 40
  37. #define TX_HPF_CUT_OFF_FREQ_MASK 0x60
  38. #define CF_MIN_3DB_4HZ 0x0
  39. #define CF_MIN_3DB_75HZ 0x1
  40. #define CF_MIN_3DB_150HZ 0x2
  41. #define AQT_VERSION_ENTRY_SIZE 17
  42. #define AQT_VOUT_CTL_TO_MICB(x) (1000 + x *50)
  43. static struct interp_sample_rate sr_val_tbl[] = {
  44. {8000, 0x0}, {16000, 0x1}, {32000, 0x3}, {48000, 0x4}, {96000, 0x5},
  45. {192000, 0x6}, {384000, 0x7}, {44100, 0x9}, {88200, 0xA},
  46. {176400, 0xB}, {352800, 0xC},
  47. };
  48. static int tx_unmute_delay = AQT1000_TX_UNMUTE_DELAY_MS;
  49. module_param(tx_unmute_delay, int, 0664);
  50. MODULE_PARM_DESC(tx_unmute_delay, "delay to unmute the tx path");
  51. static void aqt_codec_set_tx_hold(struct snd_soc_component *, u16, bool);
  52. /* Cutoff frequency for high pass filter */
  53. static const char * const cf_text[] = {
  54. "CF_NEG_3DB_4HZ", "CF_NEG_3DB_75HZ", "CF_NEG_3DB_150HZ"
  55. };
  56. static const char * const rx_cf_text[] = {
  57. "CF_NEG_3DB_4HZ", "CF_NEG_3DB_75HZ", "CF_NEG_3DB_150HZ",
  58. "CF_NEG_3DB_0P48HZ"
  59. };
  60. struct aqt1000_anc_header {
  61. u32 reserved[3];
  62. u32 num_anc_slots;
  63. };
  64. static SOC_ENUM_SINGLE_DECL(cf_dec0_enum, AQT1000_CDC_TX0_TX_PATH_CFG0, 5,
  65. cf_text);
  66. static SOC_ENUM_SINGLE_DECL(cf_dec1_enum, AQT1000_CDC_TX1_TX_PATH_CFG0, 5,
  67. cf_text);
  68. static SOC_ENUM_SINGLE_DECL(cf_dec2_enum, AQT1000_CDC_TX2_TX_PATH_CFG0, 5,
  69. cf_text);
  70. static SOC_ENUM_SINGLE_DECL(cf_int1_1_enum, AQT1000_CDC_RX1_RX_PATH_CFG2, 0,
  71. rx_cf_text);
  72. static SOC_ENUM_SINGLE_DECL(cf_int1_2_enum, AQT1000_CDC_RX1_RX_PATH_MIX_CFG, 2,
  73. rx_cf_text);
  74. static SOC_ENUM_SINGLE_DECL(cf_int2_1_enum, AQT1000_CDC_RX2_RX_PATH_CFG2, 0,
  75. rx_cf_text);
  76. static SOC_ENUM_SINGLE_DECL(cf_int2_2_enum, AQT1000_CDC_RX2_RX_PATH_MIX_CFG, 2,
  77. rx_cf_text);
  78. static const DECLARE_TLV_DB_SCALE(hph_gain, -3000, 150, 0);
  79. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 150, 0);
  80. static const DECLARE_TLV_DB_SCALE(digital_gain, 0, 1, 0);
  81. static int aqt_get_anc_slot(struct snd_kcontrol *kcontrol,
  82. struct snd_ctl_elem_value *ucontrol)
  83. {
  84. struct snd_soc_component *component =
  85. snd_soc_kcontrol_component(kcontrol);
  86. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  87. ucontrol->value.integer.value[0] = aqt->anc_slot;
  88. return 0;
  89. }
  90. static int aqt_put_anc_slot(struct snd_kcontrol *kcontrol,
  91. struct snd_ctl_elem_value *ucontrol)
  92. {
  93. struct snd_soc_component *component =
  94. snd_soc_kcontrol_component(kcontrol);
  95. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  96. aqt->anc_slot = ucontrol->value.integer.value[0];
  97. return 0;
  98. }
  99. static int aqt_get_anc_func(struct snd_kcontrol *kcontrol,
  100. struct snd_ctl_elem_value *ucontrol)
  101. {
  102. struct snd_soc_component *component =
  103. snd_soc_kcontrol_component(kcontrol);
  104. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  105. ucontrol->value.integer.value[0] = (aqt->anc_func == true ? 1 : 0);
  106. return 0;
  107. }
  108. static int aqt_put_anc_func(struct snd_kcontrol *kcontrol,
  109. struct snd_ctl_elem_value *ucontrol)
  110. {
  111. struct snd_soc_component *component =
  112. snd_soc_kcontrol_component(kcontrol);
  113. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  114. struct snd_soc_dapm_context *dapm =
  115. snd_soc_component_get_dapm(component);
  116. mutex_lock(&aqt->codec_mutex);
  117. aqt->anc_func = (!ucontrol->value.integer.value[0] ? false : true);
  118. dev_dbg(component->dev, "%s: anc_func %x", __func__, aqt->anc_func);
  119. if (aqt->anc_func == true) {
  120. snd_soc_dapm_enable_pin(dapm, "ANC HPHL PA");
  121. snd_soc_dapm_enable_pin(dapm, "ANC HPHR PA");
  122. snd_soc_dapm_enable_pin(dapm, "ANC HPHL");
  123. snd_soc_dapm_enable_pin(dapm, "ANC HPHR");
  124. snd_soc_dapm_disable_pin(dapm, "HPHL PA");
  125. snd_soc_dapm_disable_pin(dapm, "HPHR PA");
  126. snd_soc_dapm_disable_pin(dapm, "HPHL");
  127. snd_soc_dapm_disable_pin(dapm, "HPHR");
  128. } else {
  129. snd_soc_dapm_disable_pin(dapm, "ANC HPHL PA");
  130. snd_soc_dapm_disable_pin(dapm, "ANC HPHR PA");
  131. snd_soc_dapm_disable_pin(dapm, "ANC HPHL");
  132. snd_soc_dapm_disable_pin(dapm, "ANC HPHR");
  133. snd_soc_dapm_enable_pin(dapm, "HPHL");
  134. snd_soc_dapm_enable_pin(dapm, "HPHR");
  135. snd_soc_dapm_enable_pin(dapm, "HPHL PA");
  136. snd_soc_dapm_enable_pin(dapm, "HPHR PA");
  137. }
  138. mutex_unlock(&aqt->codec_mutex);
  139. snd_soc_dapm_sync(dapm);
  140. return 0;
  141. }
  142. static const char *const aqt_anc_func_text[] = {"OFF", "ON"};
  143. static const struct soc_enum aqt_anc_func_enum =
  144. SOC_ENUM_SINGLE_EXT(2, aqt_anc_func_text);
  145. static int aqt_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  146. struct snd_ctl_elem_value *ucontrol)
  147. {
  148. struct snd_soc_component *component =
  149. snd_soc_kcontrol_component(kcontrol);
  150. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  151. ucontrol->value.integer.value[0] = aqt->hph_mode;
  152. return 0;
  153. }
  154. static int aqt_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. struct snd_soc_component *component =
  158. snd_soc_kcontrol_component(kcontrol);
  159. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  160. u32 mode_val;
  161. mode_val = ucontrol->value.enumerated.item[0];
  162. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  163. if (mode_val == 0) {
  164. dev_warn(component->dev, "%s:Invalid HPH Mode, default to Cls-H LOHiFi\n",
  165. __func__);
  166. mode_val = CLS_H_LOHIFI;
  167. }
  168. aqt->hph_mode = mode_val;
  169. return 0;
  170. }
  171. static const char * const rx_hph_mode_mux_text[] = {
  172. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  173. "CLS_H_ULP", "CLS_AB_HIFI",
  174. };
  175. static const struct soc_enum rx_hph_mode_mux_enum =
  176. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  177. rx_hph_mode_mux_text);
  178. static int aqt_iir_enable_audio_mixer_get(struct snd_kcontrol *kcontrol,
  179. struct snd_ctl_elem_value *ucontrol)
  180. {
  181. struct snd_soc_component *component =
  182. snd_soc_kcontrol_component(kcontrol);
  183. int band_idx = ((struct soc_multi_mixer_control *)
  184. kcontrol->private_value)->shift;
  185. ucontrol->value.integer.value[0] = (snd_soc_component_read32(component,
  186. AQT1000_CDC_SIDETONE_IIR0_IIR_CTL) &
  187. (1 << band_idx)) != 0;
  188. dev_dbg(component->dev, "%s: IIR0 band #%d enable %d\n", __func__,
  189. band_idx, (uint32_t)ucontrol->value.integer.value[0]);
  190. return 0;
  191. }
  192. static int aqt_iir_enable_audio_mixer_put(struct snd_kcontrol *kcontrol,
  193. struct snd_ctl_elem_value *ucontrol)
  194. {
  195. struct snd_soc_component *component =
  196. snd_soc_kcontrol_component(kcontrol);
  197. int band_idx = ((struct soc_multi_mixer_control *)
  198. kcontrol->private_value)->shift;
  199. bool iir_band_en_status;
  200. int value = ucontrol->value.integer.value[0];
  201. /* Mask first 5 bits, 6-8 are reserved */
  202. snd_soc_component_update_bits(component,
  203. AQT1000_CDC_SIDETONE_IIR0_IIR_CTL,
  204. (1 << band_idx), (value << band_idx));
  205. iir_band_en_status = ((snd_soc_component_read32(component,
  206. AQT1000_CDC_SIDETONE_IIR0_IIR_CTL) &
  207. (1 << band_idx)) != 0);
  208. dev_dbg(component->dev, "%s: IIR0 band #%d enable %d\n", __func__,
  209. band_idx, iir_band_en_status);
  210. return 0;
  211. }
  212. static uint32_t aqt_get_iir_band_coeff(struct snd_soc_component *component,
  213. int band_idx, int coeff_idx)
  214. {
  215. uint32_t value = 0;
  216. /* Address does not automatically update if reading */
  217. snd_soc_component_write(component,
  218. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B1_CTL,
  219. ((band_idx * BAND_MAX + coeff_idx)
  220. * sizeof(uint32_t)) & 0x7F);
  221. value |= snd_soc_component_read32(component,
  222. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL);
  223. snd_soc_component_write(component,
  224. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B1_CTL,
  225. ((band_idx * BAND_MAX + coeff_idx)
  226. * sizeof(uint32_t) + 1) & 0x7F);
  227. value |= (snd_soc_component_read32(component,
  228. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL) << 8);
  229. snd_soc_component_write(component,
  230. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B1_CTL,
  231. ((band_idx * BAND_MAX + coeff_idx)
  232. * sizeof(uint32_t) + 2) & 0x7F);
  233. value |= (snd_soc_component_read32(component,
  234. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL) << 16);
  235. snd_soc_component_write(component,
  236. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B1_CTL,
  237. ((band_idx * BAND_MAX + coeff_idx)
  238. * sizeof(uint32_t) + 3) & 0x7F);
  239. /* Mask bits top 2 bits since they are reserved */
  240. value |= ((snd_soc_component_read32(component,
  241. AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL)
  242. & 0x3F) << 24);
  243. return value;
  244. }
  245. static int aqt_iir_band_audio_mixer_get(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. struct snd_soc_component *component =
  249. snd_soc_kcontrol_component(kcontrol);
  250. int band_idx = ((struct soc_multi_mixer_control *)
  251. kcontrol->private_value)->shift;
  252. ucontrol->value.integer.value[0] =
  253. aqt_get_iir_band_coeff(component, band_idx, 0);
  254. ucontrol->value.integer.value[1] =
  255. aqt_get_iir_band_coeff(component, band_idx, 1);
  256. ucontrol->value.integer.value[2] =
  257. aqt_get_iir_band_coeff(component, band_idx, 2);
  258. ucontrol->value.integer.value[3] =
  259. aqt_get_iir_band_coeff(component, band_idx, 3);
  260. ucontrol->value.integer.value[4] =
  261. aqt_get_iir_band_coeff(component, band_idx, 4);
  262. dev_dbg(component->dev, "%s: IIR band #%d b0 = 0x%x\n"
  263. "%s: IIR band #%d b1 = 0x%x\n"
  264. "%s: IIR band #%d b2 = 0x%x\n"
  265. "%s: IIR band #%d a1 = 0x%x\n"
  266. "%s: IIR band #%d a2 = 0x%x\n",
  267. __func__, band_idx,
  268. (uint32_t)ucontrol->value.integer.value[0],
  269. __func__, band_idx,
  270. (uint32_t)ucontrol->value.integer.value[1],
  271. __func__, band_idx,
  272. (uint32_t)ucontrol->value.integer.value[2],
  273. __func__, band_idx,
  274. (uint32_t)ucontrol->value.integer.value[3],
  275. __func__, band_idx,
  276. (uint32_t)ucontrol->value.integer.value[4]);
  277. return 0;
  278. }
  279. static void aqt_set_iir_band_coeff(struct snd_soc_component *component,
  280. int band_idx, uint32_t value)
  281. {
  282. snd_soc_component_write(component,
  283. (AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL),
  284. (value & 0xFF));
  285. snd_soc_component_write(component,
  286. (AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL),
  287. (value >> 8) & 0xFF);
  288. snd_soc_component_write(component,
  289. (AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL),
  290. (value >> 16) & 0xFF);
  291. /* Mask top 2 bits, 7-8 are reserved */
  292. snd_soc_component_write(component,
  293. (AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B2_CTL),
  294. (value >> 24) & 0x3F);
  295. }
  296. static int aqt_iir_band_audio_mixer_put(struct snd_kcontrol *kcontrol,
  297. struct snd_ctl_elem_value *ucontrol)
  298. {
  299. struct snd_soc_component *component =
  300. snd_soc_kcontrol_component(kcontrol);
  301. int band_idx = ((struct soc_multi_mixer_control *)
  302. kcontrol->private_value)->shift;
  303. int coeff_idx;
  304. /*
  305. * Mask top bit it is reserved
  306. * Updates addr automatically for each B2 write
  307. */
  308. snd_soc_component_write(component,
  309. (AQT1000_CDC_SIDETONE_IIR0_IIR_COEF_B1_CTL),
  310. (band_idx * BAND_MAX * sizeof(uint32_t)) & 0x7F);
  311. for (coeff_idx = 0; coeff_idx < AQT1000_CDC_SIDETONE_IIR_COEFF_MAX;
  312. coeff_idx++) {
  313. aqt_set_iir_band_coeff(component, band_idx,
  314. ucontrol->value.integer.value[coeff_idx]);
  315. }
  316. dev_dbg(component->dev, "%s: IIR band #%d b0 = 0x%x\n"
  317. "%s: IIR band #%d b1 = 0x%x\n"
  318. "%s: IIR band #%d b2 = 0x%x\n"
  319. "%s: IIR band #%d a1 = 0x%x\n"
  320. "%s: IIR band #%d a2 = 0x%x\n",
  321. __func__, band_idx,
  322. aqt_get_iir_band_coeff(component, band_idx, 0),
  323. __func__, band_idx,
  324. aqt_get_iir_band_coeff(component, band_idx, 1),
  325. __func__, band_idx,
  326. aqt_get_iir_band_coeff(component, band_idx, 2),
  327. __func__, band_idx,
  328. aqt_get_iir_band_coeff(component, band_idx, 3),
  329. __func__, band_idx,
  330. aqt_get_iir_band_coeff(component, band_idx, 4));
  331. return 0;
  332. }
  333. static int aqt_compander_get(struct snd_kcontrol *kcontrol,
  334. struct snd_ctl_elem_value *ucontrol)
  335. {
  336. struct snd_soc_component *component =
  337. snd_soc_kcontrol_component(kcontrol);
  338. int comp = ((struct soc_multi_mixer_control *)
  339. kcontrol->private_value)->shift;
  340. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  341. ucontrol->value.integer.value[0] = aqt->comp_enabled[comp];
  342. return 0;
  343. }
  344. static int aqt_compander_put(struct snd_kcontrol *kcontrol,
  345. struct snd_ctl_elem_value *ucontrol)
  346. {
  347. struct snd_soc_component *component =
  348. snd_soc_kcontrol_component(kcontrol);
  349. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  350. int comp = ((struct soc_multi_mixer_control *)
  351. kcontrol->private_value)->shift;
  352. int value = ucontrol->value.integer.value[0];
  353. dev_dbg(component->dev, "%s: Compander %d enable current %d, new %d\n",
  354. __func__, comp + 1, aqt->comp_enabled[comp], value);
  355. aqt->comp_enabled[comp] = value;
  356. /* Any specific register configuration for compander */
  357. switch (comp) {
  358. case COMPANDER_1:
  359. /* Set Gain Source Select based on compander enable/disable */
  360. snd_soc_component_update_bits(component,
  361. AQT1000_HPH_L_EN, 0x20,
  362. (value ? 0x00 : 0x20));
  363. break;
  364. case COMPANDER_2:
  365. snd_soc_component_update_bits(component,
  366. AQT1000_HPH_R_EN, 0x20,
  367. (value ? 0x00 : 0x20));
  368. break;
  369. default:
  370. /*
  371. * if compander is not enabled for any interpolator,
  372. * it does not cause any audio failure, so do not
  373. * return error in this case, but just print a log
  374. */
  375. dev_warn(component->dev, "%s: unknown compander: %d\n",
  376. __func__, comp);
  377. };
  378. return 0;
  379. }
  380. static int aqt_hph_asrc_mode_put(struct snd_kcontrol *kcontrol,
  381. struct snd_ctl_elem_value *ucontrol)
  382. {
  383. struct snd_soc_component *component =
  384. snd_soc_kcontrol_component(kcontrol);
  385. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  386. int index = -EINVAL;
  387. if (!strcmp(kcontrol->id.name, "AQT ASRC0 Output Mode"))
  388. index = ASRC0;
  389. if (!strcmp(kcontrol->id.name, "AQT ASRC1 Output Mode"))
  390. index = ASRC1;
  391. if (aqt && (index >= 0) && (index < ASRC_MAX))
  392. aqt->asrc_output_mode[index] =
  393. ucontrol->value.integer.value[0];
  394. return 0;
  395. }
  396. static int aqt_hph_asrc_mode_get(struct snd_kcontrol *kcontrol,
  397. struct snd_ctl_elem_value *ucontrol)
  398. {
  399. struct snd_soc_component *component =
  400. snd_soc_kcontrol_component(kcontrol);
  401. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  402. int val = 0;
  403. int index = -EINVAL;
  404. if (!strcmp(kcontrol->id.name, "AQT ASRC0 Output Mode"))
  405. index = ASRC0;
  406. if (!strcmp(kcontrol->id.name, "AQT ASRC1 Output Mode"))
  407. index = ASRC1;
  408. if (aqt && (index >= 0) && (index < ASRC_MAX))
  409. val = aqt->asrc_output_mode[index];
  410. ucontrol->value.integer.value[0] = val;
  411. return 0;
  412. }
  413. static const char * const asrc_mode_text[] = {
  414. "INT", "FRAC"
  415. };
  416. static SOC_ENUM_SINGLE_EXT_DECL(asrc_mode_enum, asrc_mode_text);
  417. static int aqt_hph_idle_detect_get(struct snd_kcontrol *kcontrol,
  418. struct snd_ctl_elem_value *ucontrol)
  419. {
  420. struct snd_soc_component *component =
  421. snd_soc_kcontrol_component(kcontrol);
  422. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  423. int val = 0;
  424. if (aqt)
  425. val = aqt->idle_det_cfg.hph_idle_detect_en;
  426. ucontrol->value.integer.value[0] = val;
  427. return 0;
  428. }
  429. static int aqt_hph_idle_detect_put(struct snd_kcontrol *kcontrol,
  430. struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct snd_soc_component *component =
  433. snd_soc_kcontrol_component(kcontrol);
  434. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  435. if (aqt)
  436. aqt->idle_det_cfg.hph_idle_detect_en =
  437. ucontrol->value.integer.value[0];
  438. return 0;
  439. }
  440. static const char * const hph_idle_detect_text[] = {
  441. "OFF", "ON"
  442. };
  443. static SOC_ENUM_SINGLE_EXT_DECL(hph_idle_detect_enum, hph_idle_detect_text);
  444. static int aqt_amic_pwr_lvl_get(struct snd_kcontrol *kcontrol,
  445. struct snd_ctl_elem_value *ucontrol)
  446. {
  447. struct snd_soc_component *component =
  448. snd_soc_kcontrol_component(kcontrol);
  449. u16 amic_reg = 0;
  450. if (!strcmp(kcontrol->id.name, "AQT AMIC_1_2 PWR MODE"))
  451. amic_reg = AQT1000_ANA_AMIC1;
  452. if (!strcmp(kcontrol->id.name, "AQT AMIC_3 PWR MODE"))
  453. amic_reg = AQT1000_ANA_AMIC3;
  454. if (amic_reg)
  455. ucontrol->value.integer.value[0] =
  456. (snd_soc_component_read32(component, amic_reg) &
  457. AQT1000_AMIC_PWR_LVL_MASK) >>
  458. AQT1000_AMIC_PWR_LVL_SHIFT;
  459. return 0;
  460. }
  461. static int aqt_amic_pwr_lvl_put(struct snd_kcontrol *kcontrol,
  462. struct snd_ctl_elem_value *ucontrol)
  463. {
  464. struct snd_soc_component *component =
  465. snd_soc_kcontrol_component(kcontrol);
  466. u32 mode_val;
  467. u16 amic_reg = 0;
  468. mode_val = ucontrol->value.enumerated.item[0];
  469. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  470. if (!strcmp(kcontrol->id.name, "AQT AMIC_1_2 PWR MODE"))
  471. amic_reg = AQT1000_ANA_AMIC1;
  472. if (!strcmp(kcontrol->id.name, "AQT AMIC_3 PWR MODE"))
  473. amic_reg = AQT1000_ANA_AMIC3;
  474. if (amic_reg)
  475. snd_soc_component_update_bits(component, amic_reg,
  476. AQT1000_AMIC_PWR_LVL_MASK,
  477. mode_val << AQT1000_AMIC_PWR_LVL_SHIFT);
  478. return 0;
  479. }
  480. static const char * const amic_pwr_lvl_text[] = {
  481. "LOW_PWR", "DEFAULT", "HIGH_PERF", "HYBRID"
  482. };
  483. static SOC_ENUM_SINGLE_EXT_DECL(amic_pwr_lvl_enum, amic_pwr_lvl_text);
  484. static const struct snd_kcontrol_new aqt_snd_controls[] = {
  485. SOC_SINGLE_TLV("AQT HPHL Volume", AQT1000_HPH_L_EN, 0, 24, 1, hph_gain),
  486. SOC_SINGLE_TLV("AQT HPHR Volume", AQT1000_HPH_R_EN, 0, 24, 1, hph_gain),
  487. SOC_SINGLE_TLV("AQT ADC1 Volume", AQT1000_ANA_AMIC1, 0, 20, 0,
  488. analog_gain),
  489. SOC_SINGLE_TLV("AQT ADC2 Volume", AQT1000_ANA_AMIC2, 0, 20, 0,
  490. analog_gain),
  491. SOC_SINGLE_TLV("AQT ADC3 Volume", AQT1000_ANA_AMIC3, 0, 20, 0,
  492. analog_gain),
  493. SOC_SINGLE_SX_TLV("AQT RX1 Digital Volume", AQT1000_CDC_RX1_RX_VOL_CTL,
  494. 0, -84, 40, digital_gain),
  495. SOC_SINGLE_SX_TLV("AQT RX2 Digital Volume", AQT1000_CDC_RX2_RX_VOL_CTL,
  496. 0, -84, 40, digital_gain),
  497. SOC_SINGLE_SX_TLV("AQT DEC0 Volume", AQT1000_CDC_TX0_TX_VOL_CTL, 0,
  498. -84, 40, digital_gain),
  499. SOC_SINGLE_SX_TLV("AQT DEC1 Volume", AQT1000_CDC_TX1_TX_VOL_CTL, 0,
  500. -84, 40, digital_gain),
  501. SOC_SINGLE_SX_TLV("AQT DEC2 Volume", AQT1000_CDC_TX2_TX_VOL_CTL, 0,
  502. -84, 40, digital_gain),
  503. SOC_SINGLE_SX_TLV("AQT IIR0 INP0 Volume",
  504. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B1_CTL, 0, -84, 40,
  505. digital_gain),
  506. SOC_SINGLE_SX_TLV("AQT IIR0 INP1 Volume",
  507. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B2_CTL, 0, -84, 40,
  508. digital_gain),
  509. SOC_SINGLE_SX_TLV("AQT IIR0 INP2 Volume",
  510. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B3_CTL, 0, -84, 40,
  511. digital_gain),
  512. SOC_SINGLE_SX_TLV("AQT IIR0 INP3 Volume",
  513. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B4_CTL, 0, -84, 40,
  514. digital_gain),
  515. SOC_SINGLE_EXT("AQT ANC Slot", SND_SOC_NOPM, 0, 100, 0,
  516. aqt_get_anc_slot, aqt_put_anc_slot),
  517. SOC_ENUM_EXT("AQT ANC Function", aqt_anc_func_enum, aqt_get_anc_func,
  518. aqt_put_anc_func),
  519. SOC_ENUM("AQT TX0 HPF cut off", cf_dec0_enum),
  520. SOC_ENUM("AQT TX1 HPF cut off", cf_dec1_enum),
  521. SOC_ENUM("AQT TX2 HPF cut off", cf_dec2_enum),
  522. SOC_ENUM("AQT RX INT1_1 HPF cut off", cf_int1_1_enum),
  523. SOC_ENUM("AQT RX INT1_2 HPF cut off", cf_int1_2_enum),
  524. SOC_ENUM("AQT RX INT2_1 HPF cut off", cf_int2_1_enum),
  525. SOC_ENUM("AQT RX INT2_2 HPF cut off", cf_int2_2_enum),
  526. SOC_ENUM_EXT("AQT RX HPH Mode", rx_hph_mode_mux_enum,
  527. aqt_rx_hph_mode_get, aqt_rx_hph_mode_put),
  528. SOC_SINGLE_EXT("AQT IIR0 Enable Band1", IIR0, BAND1, 1, 0,
  529. aqt_iir_enable_audio_mixer_get,
  530. aqt_iir_enable_audio_mixer_put),
  531. SOC_SINGLE_EXT("AQT IIR0 Enable Band2", IIR0, BAND2, 1, 0,
  532. aqt_iir_enable_audio_mixer_get,
  533. aqt_iir_enable_audio_mixer_put),
  534. SOC_SINGLE_EXT("AQT IIR0 Enable Band3", IIR0, BAND3, 1, 0,
  535. aqt_iir_enable_audio_mixer_get,
  536. aqt_iir_enable_audio_mixer_put),
  537. SOC_SINGLE_EXT("AQT IIR0 Enable Band4", IIR0, BAND4, 1, 0,
  538. aqt_iir_enable_audio_mixer_get,
  539. aqt_iir_enable_audio_mixer_put),
  540. SOC_SINGLE_EXT("AQT IIR0 Enable Band5", IIR0, BAND5, 1, 0,
  541. aqt_iir_enable_audio_mixer_get,
  542. aqt_iir_enable_audio_mixer_put),
  543. SOC_SINGLE_MULTI_EXT("AQT IIR0 Band1", IIR0, BAND1, 255, 0, 5,
  544. aqt_iir_band_audio_mixer_get, aqt_iir_band_audio_mixer_put),
  545. SOC_SINGLE_MULTI_EXT("AQT IIR0 Band2", IIR0, BAND2, 255, 0, 5,
  546. aqt_iir_band_audio_mixer_get, aqt_iir_band_audio_mixer_put),
  547. SOC_SINGLE_MULTI_EXT("AQT IIR0 Band3", IIR0, BAND3, 255, 0, 5,
  548. aqt_iir_band_audio_mixer_get, aqt_iir_band_audio_mixer_put),
  549. SOC_SINGLE_MULTI_EXT("AQT IIR0 Band4", IIR0, BAND4, 255, 0, 5,
  550. aqt_iir_band_audio_mixer_get, aqt_iir_band_audio_mixer_put),
  551. SOC_SINGLE_MULTI_EXT("AQT IIR0 Band5", IIR0, BAND5, 255, 0, 5,
  552. aqt_iir_band_audio_mixer_get, aqt_iir_band_audio_mixer_put),
  553. SOC_SINGLE_EXT("AQT COMP1 Switch", SND_SOC_NOPM, COMPANDER_1, 1, 0,
  554. aqt_compander_get, aqt_compander_put),
  555. SOC_SINGLE_EXT("AQT COMP2 Switch", SND_SOC_NOPM, COMPANDER_2, 1, 0,
  556. aqt_compander_get, aqt_compander_put),
  557. SOC_ENUM_EXT("AQT ASRC0 Output Mode", asrc_mode_enum,
  558. aqt_hph_asrc_mode_get, aqt_hph_asrc_mode_put),
  559. SOC_ENUM_EXT("AQT ASRC1 Output Mode", asrc_mode_enum,
  560. aqt_hph_asrc_mode_get, aqt_hph_asrc_mode_put),
  561. SOC_ENUM_EXT("AQT HPH Idle Detect", hph_idle_detect_enum,
  562. aqt_hph_idle_detect_get, aqt_hph_idle_detect_put),
  563. SOC_ENUM_EXT("AQT AMIC_1_2 PWR MODE", amic_pwr_lvl_enum,
  564. aqt_amic_pwr_lvl_get, aqt_amic_pwr_lvl_put),
  565. SOC_ENUM_EXT("AQT AMIC_3 PWR MODE", amic_pwr_lvl_enum,
  566. aqt_amic_pwr_lvl_get, aqt_amic_pwr_lvl_put),
  567. };
  568. static int aqt_codec_enable_rx_bias(struct snd_soc_dapm_widget *w,
  569. struct snd_kcontrol *kcontrol, int event)
  570. {
  571. struct snd_soc_component *component =
  572. snd_soc_dapm_to_component(w->dapm);
  573. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  574. dev_dbg(component->dev, "%s %s %d\n", __func__, w->name, event);
  575. switch (event) {
  576. case SND_SOC_DAPM_PRE_PMU:
  577. aqt->rx_bias_count++;
  578. if (aqt->rx_bias_count == 1) {
  579. snd_soc_component_update_bits(component,
  580. AQT1000_ANA_RX_SUPPLIES,
  581. 0x01, 0x01);
  582. }
  583. break;
  584. case SND_SOC_DAPM_POST_PMD:
  585. aqt->rx_bias_count--;
  586. if (!aqt->rx_bias_count)
  587. snd_soc_component_update_bits(component,
  588. AQT1000_ANA_RX_SUPPLIES,
  589. 0x01, 0x00);
  590. break;
  591. };
  592. dev_dbg(component->dev, "%s: Current RX BIAS user count: %d\n",
  593. __func__, aqt->rx_bias_count);
  594. return 0;
  595. }
  596. /*
  597. * aqt_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  598. * @component: handle to snd_soc_component *
  599. * @req_volt: micbias voltage to be set
  600. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  601. *
  602. * return 0 if adjustment is success or error code in case of failure
  603. */
  604. int aqt_mbhc_micb_adjust_voltage(struct snd_soc_component *component,
  605. int req_volt, int micb_num)
  606. {
  607. struct aqt1000 *aqt;
  608. int cur_vout_ctl, req_vout_ctl;
  609. int micb_reg, micb_val, micb_en;
  610. int ret = 0;
  611. if (!component) {
  612. pr_err("%s: Invalid component pointer\n", __func__);
  613. return -EINVAL;
  614. }
  615. if (micb_num != MIC_BIAS_1)
  616. return -EINVAL;
  617. else
  618. micb_reg = AQT1000_ANA_MICB1;
  619. aqt = snd_soc_component_get_drvdata(component);
  620. mutex_lock(&aqt->micb_lock);
  621. /*
  622. * If requested micbias voltage is same as current micbias
  623. * voltage, then just return. Otherwise, adjust voltage as
  624. * per requested value. If micbias is already enabled, then
  625. * to avoid slow micbias ramp-up or down enable pull-up
  626. * momentarily, change the micbias value and then re-enable
  627. * micbias.
  628. */
  629. micb_val = snd_soc_component_read32(component, micb_reg);
  630. micb_en = (micb_val & 0xC0) >> 6;
  631. cur_vout_ctl = micb_val & 0x3F;
  632. req_vout_ctl = aqt_get_micb_vout_ctl_val(req_volt);
  633. if (req_vout_ctl < 0) {
  634. ret = -EINVAL;
  635. goto exit;
  636. }
  637. if (cur_vout_ctl == req_vout_ctl) {
  638. ret = 0;
  639. goto exit;
  640. }
  641. dev_dbg(component->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  642. __func__, micb_num, AQT_VOUT_CTL_TO_MICB(cur_vout_ctl),
  643. req_volt, micb_en);
  644. if (micb_en == 0x1)
  645. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x80);
  646. snd_soc_component_update_bits(component, micb_reg, 0x3F, req_vout_ctl);
  647. if (micb_en == 0x1) {
  648. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x40);
  649. /*
  650. * Add 2ms delay as per HW requirement after enabling
  651. * micbias
  652. */
  653. usleep_range(2000, 2100);
  654. }
  655. exit:
  656. mutex_unlock(&aqt->micb_lock);
  657. return ret;
  658. }
  659. EXPORT_SYMBOL(aqt_mbhc_micb_adjust_voltage);
  660. /*
  661. * aqt_micbias_control: enable/disable micbias
  662. * @component: handle to snd_soc_component *
  663. * @micb_num: micbias to be enabled/disabled, e.g. micbias1 or micbias2
  664. * @req: control requested, enable/disable or pullup enable/disable
  665. * @is_dapm: triggered by dapm or not
  666. *
  667. * return 0 if control is success or error code in case of failure
  668. */
  669. int aqt_micbias_control(struct snd_soc_component *component,
  670. int micb_num, int req, bool is_dapm)
  671. {
  672. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  673. u16 micb_reg;
  674. int pre_off_event = 0, post_off_event = 0;
  675. int post_on_event = 0, post_dapm_off = 0;
  676. int post_dapm_on = 0;
  677. int ret = 0;
  678. switch (micb_num) {
  679. case MIC_BIAS_1:
  680. micb_reg = AQT1000_ANA_MICB1;
  681. pre_off_event = AQT_EVENT_PRE_MICBIAS_1_OFF;
  682. post_off_event = AQT_EVENT_POST_MICBIAS_1_OFF;
  683. post_on_event = AQT_EVENT_POST_MICBIAS_1_ON;
  684. post_dapm_on = AQT_EVENT_POST_DAPM_MICBIAS_1_ON;
  685. post_dapm_off = AQT_EVENT_POST_DAPM_MICBIAS_1_OFF;
  686. break;
  687. default:
  688. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  689. __func__, micb_num);
  690. return -EINVAL;
  691. }
  692. mutex_lock(&aqt->micb_lock);
  693. switch (req) {
  694. case MICB_PULLUP_ENABLE:
  695. aqt->pullup_ref++;
  696. if ((aqt->pullup_ref == 1) &&
  697. (aqt->micb_ref == 0))
  698. snd_soc_component_update_bits(component, micb_reg,
  699. 0xC0, 0x80);
  700. break;
  701. case MICB_PULLUP_DISABLE:
  702. if (aqt->pullup_ref > 0)
  703. aqt->pullup_ref--;
  704. if ((aqt->pullup_ref == 0) &&
  705. (aqt->micb_ref == 0))
  706. snd_soc_component_update_bits(component, micb_reg,
  707. 0xC0, 0x00);
  708. break;
  709. case MICB_ENABLE:
  710. aqt->micb_ref++;
  711. if (aqt->micb_ref == 1) {
  712. snd_soc_component_update_bits(component, micb_reg,
  713. 0xC0, 0x40);
  714. if (post_on_event && aqt->mbhc)
  715. blocking_notifier_call_chain(
  716. &aqt->mbhc->notifier,
  717. post_on_event,
  718. &aqt->mbhc->wcd_mbhc);
  719. }
  720. if (is_dapm && post_dapm_on && aqt->mbhc)
  721. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  722. post_dapm_on, &aqt->mbhc->wcd_mbhc);
  723. break;
  724. case MICB_DISABLE:
  725. if (aqt->micb_ref > 0)
  726. aqt->micb_ref--;
  727. if ((aqt->micb_ref == 0) &&
  728. (aqt->pullup_ref > 0))
  729. snd_soc_component_update_bits(component, micb_reg,
  730. 0xC0, 0x80);
  731. else if ((aqt->micb_ref == 0) &&
  732. (aqt->pullup_ref == 0)) {
  733. if (pre_off_event && aqt->mbhc)
  734. blocking_notifier_call_chain(
  735. &aqt->mbhc->notifier,
  736. pre_off_event,
  737. &aqt->mbhc->wcd_mbhc);
  738. snd_soc_component_update_bits(component, micb_reg,
  739. 0xC0, 0x00);
  740. if (post_off_event && aqt->mbhc)
  741. blocking_notifier_call_chain(
  742. &aqt->mbhc->notifier,
  743. post_off_event,
  744. &aqt->mbhc->wcd_mbhc);
  745. }
  746. if (is_dapm && post_dapm_off && aqt->mbhc)
  747. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  748. post_dapm_off, &aqt->mbhc->wcd_mbhc);
  749. break;
  750. default:
  751. dev_err(component->dev, "%s: Invalid micbias request: %d\n",
  752. __func__, req);
  753. ret = -EINVAL;
  754. break;
  755. };
  756. if (!ret)
  757. dev_dbg(component->dev,
  758. "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  759. __func__, micb_num, aqt->micb_ref, aqt->pullup_ref);
  760. mutex_unlock(&aqt->micb_lock);
  761. return ret;
  762. }
  763. EXPORT_SYMBOL(aqt_micbias_control);
  764. static int __aqt_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  765. int event)
  766. {
  767. struct snd_soc_component *component =
  768. snd_soc_dapm_to_component(w->dapm);
  769. int micb_num;
  770. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  771. __func__, w->name, event);
  772. if (strnstr(w->name, "AQT MIC BIAS1", sizeof("AQT MIC BIAS1")))
  773. micb_num = MIC_BIAS_1;
  774. else
  775. return -EINVAL;
  776. switch (event) {
  777. case SND_SOC_DAPM_PRE_PMU:
  778. /*
  779. * MIC BIAS can also be requested by MBHC,
  780. * so use ref count to handle micbias pullup
  781. * and enable requests
  782. */
  783. aqt_micbias_control(component, micb_num, MICB_ENABLE, true);
  784. break;
  785. case SND_SOC_DAPM_POST_PMU:
  786. /* wait for cnp time */
  787. usleep_range(1000, 1100);
  788. break;
  789. case SND_SOC_DAPM_POST_PMD:
  790. aqt_micbias_control(component, micb_num, MICB_DISABLE, true);
  791. break;
  792. };
  793. return 0;
  794. }
  795. static int aqt_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  796. struct snd_kcontrol *kcontrol, int event)
  797. {
  798. return __aqt_codec_enable_micbias(w, event);
  799. }
  800. static int aqt_codec_enable_i2s_block(struct snd_soc_component *component)
  801. {
  802. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  803. mutex_lock(&aqt->i2s_lock);
  804. if (++aqt->i2s_users == 1)
  805. snd_soc_component_update_bits(component, AQT1000_I2S_I2S_0_CTL,
  806. 0x01, 0x01);
  807. mutex_unlock(&aqt->i2s_lock);
  808. return 0;
  809. }
  810. static int aqt_codec_disable_i2s_block(struct snd_soc_component *component)
  811. {
  812. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  813. mutex_lock(&aqt->i2s_lock);
  814. if (--aqt->i2s_users == 0)
  815. snd_soc_component_update_bits(component, AQT1000_I2S_I2S_0_CTL,
  816. 0x01, 0x00);
  817. if (aqt->i2s_users < 0)
  818. dev_warn(component->dev, "%s: i2s_users count (%d) < 0\n",
  819. __func__, aqt->i2s_users);
  820. mutex_unlock(&aqt->i2s_lock);
  821. return 0;
  822. }
  823. static int aqt_codec_enable_i2s_tx(struct snd_soc_dapm_widget *w,
  824. struct snd_kcontrol *kcontrol,
  825. int event)
  826. {
  827. struct snd_soc_component *component =
  828. snd_soc_dapm_to_component(w->dapm);
  829. switch (event) {
  830. case SND_SOC_DAPM_PRE_PMU:
  831. aqt_codec_enable_i2s_block(component);
  832. break;
  833. case SND_SOC_DAPM_POST_PMD:
  834. aqt_codec_disable_i2s_block(component);
  835. break;
  836. }
  837. dev_dbg(component->dev, "%s: event: %d\n", __func__, event);
  838. return 0;
  839. }
  840. static int aqt_codec_enable_i2s_rx(struct snd_soc_dapm_widget *w,
  841. struct snd_kcontrol *kcontrol,
  842. int event)
  843. {
  844. struct snd_soc_component *component =
  845. snd_soc_dapm_to_component(w->dapm);
  846. switch (event) {
  847. case SND_SOC_DAPM_PRE_PMU:
  848. aqt_codec_enable_i2s_block(component);
  849. break;
  850. case SND_SOC_DAPM_POST_PMD:
  851. aqt_codec_disable_i2s_block(component);
  852. break;
  853. }
  854. dev_dbg(component->dev, "%s: event: %d\n", __func__, event);
  855. return 0;
  856. }
  857. static const char * const tx_mux_text[] = {
  858. "ZERO", "DEC_L", "DEC_R", "DEC_V",
  859. };
  860. AQT_DAPM_ENUM(tx0, AQT1000_CDC_IF_ROUTER_TX_MUX_CFG0, 0, tx_mux_text);
  861. AQT_DAPM_ENUM(tx1, AQT1000_CDC_IF_ROUTER_TX_MUX_CFG0, 2, tx_mux_text);
  862. static const char * const tx_adc_mux_text[] = {
  863. "AMIC", "ANC_FB0", "ANC_FB1",
  864. };
  865. AQT_DAPM_ENUM(tx_adc0, AQT1000_CDC_TX_INP_MUX_ADC_MUX0_CFG1, 0,
  866. tx_adc_mux_text);
  867. AQT_DAPM_ENUM(tx_adc1, AQT1000_CDC_TX_INP_MUX_ADC_MUX1_CFG1, 0,
  868. tx_adc_mux_text);
  869. AQT_DAPM_ENUM(tx_adc2, AQT1000_CDC_TX_INP_MUX_ADC_MUX2_CFG1, 0,
  870. tx_adc_mux_text);
  871. static int aqt_find_amic_input(struct snd_soc_component *component,
  872. int adc_mux_n)
  873. {
  874. u8 mask;
  875. u16 adc_mux_in_reg = 0, amic_mux_sel_reg = 0;
  876. bool is_amic;
  877. if (adc_mux_n > 2)
  878. return 0;
  879. if (adc_mux_n < 3) {
  880. adc_mux_in_reg = AQT1000_CDC_TX_INP_MUX_ADC_MUX0_CFG1 +
  881. adc_mux_n;
  882. mask = 0x03;
  883. amic_mux_sel_reg = AQT1000_CDC_TX_INP_MUX_ADC_MUX0_CFG0 +
  884. 2 * adc_mux_n;
  885. }
  886. is_amic = (
  887. ((snd_soc_component_read32(component, adc_mux_in_reg)
  888. & mask)) == 0);
  889. if (!is_amic)
  890. return 0;
  891. return snd_soc_component_read32(component, amic_mux_sel_reg) & 0x07;
  892. }
  893. static u16 aqt_codec_get_amic_pwlvl_reg(
  894. struct snd_soc_component *component, int amic)
  895. {
  896. u16 pwr_level_reg = 0;
  897. switch (amic) {
  898. case 1:
  899. case 2:
  900. pwr_level_reg = AQT1000_ANA_AMIC1;
  901. break;
  902. case 3:
  903. pwr_level_reg = AQT1000_ANA_AMIC3;
  904. break;
  905. default:
  906. dev_dbg(component->dev, "%s: invalid amic: %d\n",
  907. __func__, amic);
  908. break;
  909. }
  910. return pwr_level_reg;
  911. }
  912. static void aqt_tx_hpf_corner_freq_callback(struct work_struct *work)
  913. {
  914. struct delayed_work *hpf_delayed_work;
  915. struct hpf_work *hpf_work;
  916. struct aqt1000 *aqt;
  917. struct snd_soc_component *component;
  918. u16 dec_cfg_reg, amic_reg, go_bit_reg;
  919. u8 hpf_cut_off_freq;
  920. int amic_n;
  921. hpf_delayed_work = to_delayed_work(work);
  922. hpf_work = container_of(hpf_delayed_work, struct hpf_work, dwork);
  923. aqt = hpf_work->aqt;
  924. component = aqt->component;
  925. hpf_cut_off_freq = hpf_work->hpf_cut_off_freq;
  926. dec_cfg_reg = AQT1000_CDC_TX0_TX_PATH_CFG0 + 16 * hpf_work->decimator;
  927. go_bit_reg = dec_cfg_reg + 7;
  928. dev_dbg(component->dev, "%s: decimator %u hpf_cut_of_freq 0x%x\n",
  929. __func__, hpf_work->decimator, hpf_cut_off_freq);
  930. amic_n = aqt_find_amic_input(component, hpf_work->decimator);
  931. if (amic_n) {
  932. amic_reg = AQT1000_ANA_AMIC1 + amic_n - 1;
  933. aqt_codec_set_tx_hold(component, amic_reg, false);
  934. }
  935. snd_soc_component_update_bits(component, dec_cfg_reg,
  936. TX_HPF_CUT_OFF_FREQ_MASK,
  937. hpf_cut_off_freq << 5);
  938. snd_soc_component_update_bits(component, go_bit_reg, 0x02, 0x02);
  939. /* Minimum 1 clk cycle delay is required as per HW spec */
  940. usleep_range(1000, 1010);
  941. snd_soc_component_update_bits(component, go_bit_reg, 0x02, 0x00);
  942. }
  943. static void aqt_tx_mute_update_callback(struct work_struct *work)
  944. {
  945. struct tx_mute_work *tx_mute_dwork;
  946. struct aqt1000 *aqt;
  947. struct delayed_work *delayed_work;
  948. struct snd_soc_component *component;
  949. u16 tx_vol_ctl_reg, hpf_gate_reg;
  950. delayed_work = to_delayed_work(work);
  951. tx_mute_dwork = container_of(delayed_work, struct tx_mute_work, dwork);
  952. aqt = tx_mute_dwork->aqt;
  953. component = aqt->component;
  954. tx_vol_ctl_reg = AQT1000_CDC_TX0_TX_PATH_CTL +
  955. 16 * tx_mute_dwork->decimator;
  956. hpf_gate_reg = AQT1000_CDC_TX0_TX_PATH_SEC2 +
  957. 16 * tx_mute_dwork->decimator;
  958. snd_soc_component_update_bits(component, tx_vol_ctl_reg, 0x10, 0x00);
  959. }
  960. static int aqt_codec_enable_dec(struct snd_soc_dapm_widget *w,
  961. struct snd_kcontrol *kcontrol, int event)
  962. {
  963. struct snd_soc_component *component =
  964. snd_soc_dapm_to_component(w->dapm);
  965. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  966. char *widget_name = NULL;
  967. char *dec = NULL;
  968. unsigned int decimator = 0;
  969. u8 amic_n = 0;
  970. u16 tx_vol_ctl_reg, pwr_level_reg = 0, dec_cfg_reg, hpf_gate_reg;
  971. u16 tx_gain_ctl_reg;
  972. int ret = 0;
  973. u8 hpf_cut_off_freq;
  974. dev_dbg(component->dev, "%s: event: %d\n", __func__, event);
  975. widget_name = kstrndup(w->name, 15, GFP_KERNEL);
  976. if (!widget_name)
  977. return -ENOMEM;
  978. dec = strpbrk(widget_name, "012");
  979. if (!dec) {
  980. dev_err(component->dev, "%s: decimator index not found\n",
  981. __func__);
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. ret = kstrtouint(dec, 10, &decimator);
  986. if (ret < 0) {
  987. dev_err(component->dev, "%s: Invalid decimator = %s\n",
  988. __func__, widget_name);
  989. ret = -EINVAL;
  990. goto out;
  991. }
  992. dev_dbg(component->dev, "%s(): widget = %s decimator = %u\n", __func__,
  993. w->name, decimator);
  994. tx_vol_ctl_reg = AQT1000_CDC_TX0_TX_PATH_CTL + 16 * decimator;
  995. hpf_gate_reg = AQT1000_CDC_TX0_TX_PATH_SEC2 + 16 * decimator;
  996. dec_cfg_reg = AQT1000_CDC_TX0_TX_PATH_CFG0 + 16 * decimator;
  997. tx_gain_ctl_reg = AQT1000_CDC_TX0_TX_VOL_CTL + 16 * decimator;
  998. amic_n = aqt_find_amic_input(component, decimator);
  999. switch (event) {
  1000. case SND_SOC_DAPM_PRE_PMU:
  1001. if (amic_n)
  1002. pwr_level_reg = aqt_codec_get_amic_pwlvl_reg(component,
  1003. amic_n);
  1004. if (pwr_level_reg) {
  1005. switch ((snd_soc_component_read32(
  1006. component, pwr_level_reg) &
  1007. AQT1000_AMIC_PWR_LVL_MASK) >>
  1008. AQT1000_AMIC_PWR_LVL_SHIFT) {
  1009. case AQT1000_AMIC_PWR_LEVEL_LP:
  1010. snd_soc_component_update_bits(
  1011. component, dec_cfg_reg,
  1012. AQT1000_DEC_PWR_LVL_MASK,
  1013. AQT1000_DEC_PWR_LVL_LP);
  1014. break;
  1015. case AQT1000_AMIC_PWR_LEVEL_HP:
  1016. snd_soc_component_update_bits(
  1017. component, dec_cfg_reg,
  1018. AQT1000_DEC_PWR_LVL_MASK,
  1019. AQT1000_DEC_PWR_LVL_HP);
  1020. break;
  1021. case AQT1000_AMIC_PWR_LEVEL_DEFAULT:
  1022. default:
  1023. snd_soc_component_update_bits(
  1024. component, dec_cfg_reg,
  1025. AQT1000_DEC_PWR_LVL_MASK,
  1026. AQT1000_DEC_PWR_LVL_DF);
  1027. break;
  1028. }
  1029. }
  1030. /* Enable TX PGA Mute */
  1031. snd_soc_component_update_bits(component, tx_vol_ctl_reg,
  1032. 0x10, 0x10);
  1033. break;
  1034. case SND_SOC_DAPM_POST_PMU:
  1035. hpf_cut_off_freq = (snd_soc_component_read32(
  1036. component, dec_cfg_reg) &
  1037. TX_HPF_CUT_OFF_FREQ_MASK) >> 5;
  1038. aqt->tx_hpf_work[decimator].hpf_cut_off_freq =
  1039. hpf_cut_off_freq;
  1040. if (hpf_cut_off_freq != CF_MIN_3DB_150HZ) {
  1041. snd_soc_component_update_bits(component, dec_cfg_reg,
  1042. TX_HPF_CUT_OFF_FREQ_MASK,
  1043. CF_MIN_3DB_150HZ << 5);
  1044. snd_soc_component_update_bits(component, hpf_gate_reg,
  1045. 0x02, 0x02);
  1046. /*
  1047. * Minimum 1 clk cycle delay is required as per
  1048. * HW spec.
  1049. */
  1050. usleep_range(1000, 1010);
  1051. snd_soc_component_update_bits(component, hpf_gate_reg,
  1052. 0x02, 0x00);
  1053. }
  1054. /* schedule work queue to Remove Mute */
  1055. schedule_delayed_work(&aqt->tx_mute_dwork[decimator].dwork,
  1056. msecs_to_jiffies(tx_unmute_delay));
  1057. if (aqt->tx_hpf_work[decimator].hpf_cut_off_freq !=
  1058. CF_MIN_3DB_150HZ)
  1059. schedule_delayed_work(
  1060. &aqt->tx_hpf_work[decimator].dwork,
  1061. msecs_to_jiffies(300));
  1062. /* apply gain after decimator is enabled */
  1063. snd_soc_component_write(component, tx_gain_ctl_reg,
  1064. snd_soc_component_read32(
  1065. component, tx_gain_ctl_reg));
  1066. break;
  1067. case SND_SOC_DAPM_PRE_PMD:
  1068. hpf_cut_off_freq =
  1069. aqt->tx_hpf_work[decimator].hpf_cut_off_freq;
  1070. snd_soc_component_update_bits(component, tx_vol_ctl_reg,
  1071. 0x10, 0x10);
  1072. if (cancel_delayed_work_sync(
  1073. &aqt->tx_hpf_work[decimator].dwork)) {
  1074. if (hpf_cut_off_freq != CF_MIN_3DB_150HZ) {
  1075. snd_soc_component_update_bits(
  1076. component, dec_cfg_reg,
  1077. TX_HPF_CUT_OFF_FREQ_MASK,
  1078. hpf_cut_off_freq << 5);
  1079. snd_soc_component_update_bits(
  1080. component, hpf_gate_reg,
  1081. 0x02, 0x02);
  1082. /*
  1083. * Minimum 1 clk cycle delay is required as per
  1084. * HW spec.
  1085. */
  1086. usleep_range(1000, 1010);
  1087. snd_soc_component_update_bits(
  1088. component, hpf_gate_reg,
  1089. 0x02, 0x00);
  1090. }
  1091. }
  1092. cancel_delayed_work_sync(
  1093. &aqt->tx_mute_dwork[decimator].dwork);
  1094. break;
  1095. case SND_SOC_DAPM_POST_PMD:
  1096. snd_soc_component_update_bits(component, tx_vol_ctl_reg,
  1097. 0x10, 0x00);
  1098. snd_soc_component_update_bits(component, dec_cfg_reg,
  1099. AQT1000_DEC_PWR_LVL_MASK,
  1100. AQT1000_DEC_PWR_LVL_DF);
  1101. break;
  1102. }
  1103. out:
  1104. kfree(widget_name);
  1105. return ret;
  1106. }
  1107. static const char * const tx_amic_text[] = {
  1108. "ZERO", "ADC_L", "ADC_R", "ADC_V",
  1109. };
  1110. AQT_DAPM_ENUM(tx_amic0, AQT1000_CDC_TX_INP_MUX_ADC_MUX0_CFG0, 0, tx_amic_text);
  1111. AQT_DAPM_ENUM(tx_amic1, AQT1000_CDC_TX_INP_MUX_ADC_MUX1_CFG0, 0, tx_amic_text);
  1112. AQT_DAPM_ENUM(tx_amic2, AQT1000_CDC_TX_INP_MUX_ADC_MUX2_CFG0, 0, tx_amic_text);
  1113. AQT_DAPM_ENUM(tx_amic10, AQT1000_CDC_TX_INP_MUX_ADC_MUX10_CFG0, 0,
  1114. tx_amic_text);
  1115. AQT_DAPM_ENUM(tx_amic11, AQT1000_CDC_TX_INP_MUX_ADC_MUX11_CFG0, 0,
  1116. tx_amic_text);
  1117. AQT_DAPM_ENUM(tx_amic12, AQT1000_CDC_TX_INP_MUX_ADC_MUX12_CFG0, 0,
  1118. tx_amic_text);
  1119. AQT_DAPM_ENUM(tx_amic13, AQT1000_CDC_TX_INP_MUX_ADC_MUX13_CFG0, 0,
  1120. tx_amic_text);
  1121. static int aqt_codec_enable_adc(struct snd_soc_dapm_widget *w,
  1122. struct snd_kcontrol *kcontrol, int event)
  1123. {
  1124. struct snd_soc_component *component =
  1125. snd_soc_dapm_to_component(w->dapm);
  1126. switch (event) {
  1127. case SND_SOC_DAPM_PRE_PMU:
  1128. aqt_codec_set_tx_hold(component, w->reg, true);
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. return 0;
  1134. }
  1135. static const struct snd_kcontrol_new anc_hphl_pa_switch =
  1136. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0);
  1137. static const struct snd_kcontrol_new anc_hphr_pa_switch =
  1138. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0);
  1139. static int aqt_config_compander(struct snd_soc_component *component,
  1140. int interp_n, int event)
  1141. {
  1142. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1143. int comp;
  1144. u16 comp_ctl0_reg, rx_path_cfg0_reg;
  1145. comp = interp_n;
  1146. dev_dbg(component->dev, "%s: event %d compander %d, enabled %d\n",
  1147. __func__, event, comp, aqt->comp_enabled[comp]);
  1148. if (!aqt->comp_enabled[comp])
  1149. return 0;
  1150. comp_ctl0_reg = AQT1000_CDC_COMPANDER1_CTL0 + (comp * 8);
  1151. rx_path_cfg0_reg = AQT1000_CDC_RX1_RX_PATH_CFG0 + (comp * 20);
  1152. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1153. /* Enable Compander Clock */
  1154. snd_soc_component_update_bits(
  1155. component, comp_ctl0_reg, 0x01, 0x01);
  1156. snd_soc_component_update_bits(
  1157. component, comp_ctl0_reg, 0x02, 0x02);
  1158. snd_soc_component_update_bits(
  1159. component, comp_ctl0_reg, 0x02, 0x00);
  1160. snd_soc_component_update_bits(
  1161. component, rx_path_cfg0_reg, 0x02, 0x02);
  1162. }
  1163. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  1164. snd_soc_component_update_bits(
  1165. component, rx_path_cfg0_reg, 0x02, 0x00);
  1166. snd_soc_component_update_bits(
  1167. component, comp_ctl0_reg, 0x04, 0x04);
  1168. snd_soc_component_update_bits(
  1169. component, comp_ctl0_reg, 0x02, 0x02);
  1170. snd_soc_component_update_bits(
  1171. component, comp_ctl0_reg, 0x02, 0x00);
  1172. snd_soc_component_update_bits(
  1173. component, comp_ctl0_reg, 0x01, 0x00);
  1174. snd_soc_component_update_bits(
  1175. component, comp_ctl0_reg, 0x04, 0x00);
  1176. }
  1177. return 0;
  1178. }
  1179. static void aqt_codec_idle_detect_control(struct snd_soc_component *component,
  1180. int interp, int event)
  1181. {
  1182. int reg = 0, mask, val;
  1183. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1184. if (!aqt->idle_det_cfg.hph_idle_detect_en)
  1185. return;
  1186. if (interp == INTERP_HPHL) {
  1187. reg = AQT1000_CDC_RX_IDLE_DET_PATH_CTL;
  1188. mask = 0x01;
  1189. val = 0x01;
  1190. }
  1191. if (interp == INTERP_HPHR) {
  1192. reg = AQT1000_CDC_RX_IDLE_DET_PATH_CTL;
  1193. mask = 0x02;
  1194. val = 0x02;
  1195. }
  1196. if (reg && SND_SOC_DAPM_EVENT_ON(event))
  1197. snd_soc_component_update_bits(component, reg, mask, val);
  1198. if (reg && SND_SOC_DAPM_EVENT_OFF(event)) {
  1199. snd_soc_component_update_bits(component, reg, mask, 0x00);
  1200. aqt->idle_det_cfg.hph_idle_thr = 0;
  1201. snd_soc_component_write(component,
  1202. AQT1000_CDC_RX_IDLE_DET_CFG3, 0x0);
  1203. }
  1204. }
  1205. static void aqt_codec_hphdelay_lutbypass(struct snd_soc_component *component,
  1206. u16 interp_idx, int event)
  1207. {
  1208. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1209. u8 hph_dly_mask;
  1210. u16 hph_lut_bypass_reg = 0;
  1211. u16 hph_comp_ctrl7 = 0;
  1212. switch (interp_idx) {
  1213. case INTERP_HPHL:
  1214. hph_dly_mask = 1;
  1215. hph_lut_bypass_reg = AQT1000_CDC_TOP_HPHL_COMP_LUT;
  1216. hph_comp_ctrl7 = AQT1000_CDC_COMPANDER1_CTL7;
  1217. break;
  1218. case INTERP_HPHR:
  1219. hph_dly_mask = 2;
  1220. hph_lut_bypass_reg = AQT1000_CDC_TOP_HPHR_COMP_LUT;
  1221. hph_comp_ctrl7 = AQT1000_CDC_COMPANDER2_CTL7;
  1222. break;
  1223. default:
  1224. break;
  1225. }
  1226. if (hph_lut_bypass_reg && SND_SOC_DAPM_EVENT_ON(event)) {
  1227. snd_soc_component_update_bits(component, AQT1000_CDC_CLSH_TEST0,
  1228. hph_dly_mask, 0x0);
  1229. snd_soc_component_update_bits(component, hph_lut_bypass_reg,
  1230. 0x80, 0x80);
  1231. if (aqt->hph_mode == CLS_H_ULP)
  1232. snd_soc_component_update_bits(component, hph_comp_ctrl7,
  1233. 0x20, 0x20);
  1234. }
  1235. if (hph_lut_bypass_reg && SND_SOC_DAPM_EVENT_OFF(event)) {
  1236. snd_soc_component_update_bits(component, AQT1000_CDC_CLSH_TEST0,
  1237. hph_dly_mask, hph_dly_mask);
  1238. snd_soc_component_update_bits(component, hph_lut_bypass_reg,
  1239. 0x80, 0x00);
  1240. snd_soc_component_update_bits(component, hph_comp_ctrl7,
  1241. 0x20, 0x0);
  1242. }
  1243. }
  1244. static int aqt_codec_enable_interp_clk(struct snd_soc_component *component,
  1245. int event, int interp_idx)
  1246. {
  1247. struct aqt1000 *aqt;
  1248. u16 main_reg, dsm_reg;
  1249. if (!component) {
  1250. pr_err("%s: component is NULL\n", __func__);
  1251. return -EINVAL;
  1252. }
  1253. aqt = snd_soc_component_get_drvdata(component);
  1254. main_reg = AQT1000_CDC_RX1_RX_PATH_CTL + (interp_idx * 20);
  1255. dsm_reg = AQT1000_CDC_RX1_RX_PATH_DSMDEM_CTL + (interp_idx * 20);
  1256. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1257. if (aqt->main_clk_users[interp_idx] == 0) {
  1258. /* Main path PGA mute enable */
  1259. snd_soc_component_update_bits(component, main_reg,
  1260. 0x10, 0x10);
  1261. /* Clk enable */
  1262. snd_soc_component_update_bits(component, dsm_reg,
  1263. 0x01, 0x01);
  1264. snd_soc_component_update_bits(component, main_reg,
  1265. 0x20, 0x20);
  1266. aqt_codec_idle_detect_control(component, interp_idx,
  1267. event);
  1268. aqt_codec_hphdelay_lutbypass(component, interp_idx,
  1269. event);
  1270. aqt_config_compander(component, interp_idx, event);
  1271. }
  1272. aqt->main_clk_users[interp_idx]++;
  1273. }
  1274. if (SND_SOC_DAPM_EVENT_OFF(event)) {
  1275. aqt->main_clk_users[interp_idx]--;
  1276. if (aqt->main_clk_users[interp_idx] <= 0) {
  1277. aqt->main_clk_users[interp_idx] = 0;
  1278. aqt_config_compander(component, interp_idx, event);
  1279. aqt_codec_hphdelay_lutbypass(component, interp_idx,
  1280. event);
  1281. aqt_codec_idle_detect_control(component, interp_idx,
  1282. event);
  1283. /* Clk Disable */
  1284. snd_soc_component_update_bits(component, main_reg,
  1285. 0x20, 0x00);
  1286. snd_soc_component_update_bits(component, dsm_reg,
  1287. 0x01, 0x00);
  1288. /* Reset enable and disable */
  1289. snd_soc_component_update_bits(component, main_reg,
  1290. 0x40, 0x40);
  1291. snd_soc_component_update_bits(component, main_reg,
  1292. 0x40, 0x00);
  1293. /* Reset rate to 48K*/
  1294. snd_soc_component_update_bits(component, main_reg,
  1295. 0x0F, 0x04);
  1296. }
  1297. }
  1298. dev_dbg(component->dev, "%s event %d main_clk_users %d\n",
  1299. __func__, event, aqt->main_clk_users[interp_idx]);
  1300. return aqt->main_clk_users[interp_idx];
  1301. }
  1302. static int aqt_anc_out_switch_cb(struct snd_soc_dapm_widget *w,
  1303. struct snd_kcontrol *kcontrol, int event)
  1304. {
  1305. struct snd_soc_component *component =
  1306. snd_soc_dapm_to_component(w->dapm);
  1307. aqt_codec_enable_interp_clk(component, event, w->shift);
  1308. return 0;
  1309. }
  1310. static const char * const anc0_fb_mux_text[] = {
  1311. "ZERO", "ANC_IN_HPHL",
  1312. };
  1313. static const char * const anc1_fb_mux_text[] = {
  1314. "ZERO", "ANC_IN_HPHR",
  1315. };
  1316. AQT_DAPM_ENUM(anc0_fb, AQT1000_CDC_RX_INP_MUX_ANC_CFG0, 0, anc0_fb_mux_text);
  1317. AQT_DAPM_ENUM(anc1_fb, AQT1000_CDC_RX_INP_MUX_ANC_CFG0, 3, anc1_fb_mux_text);
  1318. static const char *const rx_int1_1_mux_text[] = {
  1319. "ZERO", "MAIN_DMA_L", "I2S0_L", "I2S0_R", "DEC_L", "DEC_R", "DEC_V",
  1320. "SHADOW_I2S0_L", "MAIN_DMA_R"
  1321. };
  1322. static const char *const rx_int1_2_mux_text[] = {
  1323. "ZERO", "MIX_DMA_L", "I2S0_L", "I2S0_R", "DEC_L", "DEC_R", "DEC_V",
  1324. "IIR0", "MIX_DMA_R"
  1325. };
  1326. static const char *const rx_int2_1_mux_text[] = {
  1327. "ZERO", "MAIN_DMA_R", "I2S0_L", "I2S0_R", "DEC_L", "DEC_R", "DEC_V",
  1328. "SHADOW_I2S0_R", "MAIN_DMA_L"
  1329. };
  1330. static const char *const rx_int2_2_mux_text[] = {
  1331. "ZERO", "MIX_DMA_R", "I2S0_L", "I2S0_R", "DEC_L", "DEC_R", "DEC_V",
  1332. "IIR0", "MIX_DMA_L"
  1333. };
  1334. AQT_DAPM_ENUM(rx_int1_1, AQT1000_CDC_RX_INP_MUX_RX_INT1_CFG0, 0,
  1335. rx_int1_1_mux_text);
  1336. AQT_DAPM_ENUM(rx_int1_2, AQT1000_CDC_RX_INP_MUX_RX_INT1_CFG1, 0,
  1337. rx_int1_2_mux_text);
  1338. AQT_DAPM_ENUM(rx_int2_1, AQT1000_CDC_RX_INP_MUX_RX_INT2_CFG0, 0,
  1339. rx_int2_1_mux_text);
  1340. AQT_DAPM_ENUM(rx_int2_2, AQT1000_CDC_RX_INP_MUX_RX_INT2_CFG1, 0,
  1341. rx_int2_2_mux_text);
  1342. static int aqt_codec_set_idle_detect_thr(struct snd_soc_component *component,
  1343. int interp, int path_type)
  1344. {
  1345. int port_id[4] = { 0, 0, 0, 0 };
  1346. int *port_ptr, num_ports;
  1347. int bit_width = 0;
  1348. int mux_reg = 0, mux_reg_val = 0;
  1349. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1350. int idle_thr;
  1351. if ((interp != INTERP_HPHL) && (interp != INTERP_HPHR))
  1352. return 0;
  1353. if (!aqt->idle_det_cfg.hph_idle_detect_en)
  1354. return 0;
  1355. port_ptr = &port_id[0];
  1356. num_ports = 0;
  1357. if (path_type == INTERP_MIX_PATH) {
  1358. if (interp == INTERP_HPHL)
  1359. mux_reg = AQT1000_CDC_RX_INP_MUX_RX_INT1_CFG1;
  1360. else
  1361. mux_reg = AQT1000_CDC_RX_INP_MUX_RX_INT2_CFG1;
  1362. }
  1363. if (path_type == INTERP_MAIN_PATH) {
  1364. if (interp == INTERP_HPHL)
  1365. mux_reg = AQT1000_CDC_RX_INP_MUX_RX_INT1_CFG0;
  1366. else
  1367. mux_reg = AQT1000_CDC_RX_INP_MUX_RX_INT2_CFG0;
  1368. }
  1369. mux_reg_val = snd_soc_component_read32(component, mux_reg);
  1370. /* Read bit width from I2S reg if mux is set to I2S0_L or I2S0_R */
  1371. if (mux_reg_val == 0x02 || mux_reg_val == 0x03)
  1372. bit_width = ((snd_soc_component_read32(
  1373. component, AQT1000_I2S_I2S_0_CTL) &
  1374. 0x40) >> 6);
  1375. switch (bit_width) {
  1376. case 1: /* 16 bit */
  1377. idle_thr = 0xff; /* F16 */
  1378. break;
  1379. case 0: /* 32 bit */
  1380. default:
  1381. idle_thr = 0x03; /* F22 */
  1382. break;
  1383. }
  1384. dev_dbg(component->dev, "%s: (new) idle_thr: %d, (cur) idle_thr: %d\n",
  1385. __func__, idle_thr, aqt->idle_det_cfg.hph_idle_thr);
  1386. if ((aqt->idle_det_cfg.hph_idle_thr == 0) ||
  1387. (idle_thr < aqt->idle_det_cfg.hph_idle_thr)) {
  1388. snd_soc_component_write(component, AQT1000_CDC_RX_IDLE_DET_CFG3,
  1389. idle_thr);
  1390. aqt->idle_det_cfg.hph_idle_thr = idle_thr;
  1391. }
  1392. return 0;
  1393. }
  1394. static int aqt_codec_enable_main_path(struct snd_soc_dapm_widget *w,
  1395. struct snd_kcontrol *kcontrol,
  1396. int event)
  1397. {
  1398. struct snd_soc_component *component =
  1399. snd_soc_dapm_to_component(w->dapm);
  1400. u16 gain_reg = 0;
  1401. int val = 0;
  1402. dev_dbg(component->dev, "%s %d %s\n", __func__, event, w->name);
  1403. if (w->shift >= AQT1000_NUM_INTERPOLATORS) {
  1404. dev_err(component->dev, "%s: Invalid Interpolator value %d for name %s\n",
  1405. __func__, w->shift, w->name);
  1406. return -EINVAL;
  1407. };
  1408. gain_reg = AQT1000_CDC_RX1_RX_VOL_CTL + (w->shift *
  1409. AQT1000_RX_PATH_CTL_OFFSET);
  1410. switch (event) {
  1411. case SND_SOC_DAPM_PRE_PMU:
  1412. aqt_codec_enable_interp_clk(component, event, w->shift);
  1413. break;
  1414. case SND_SOC_DAPM_POST_PMU:
  1415. aqt_codec_set_idle_detect_thr(component, w->shift,
  1416. INTERP_MAIN_PATH);
  1417. /* apply gain after int clk is enabled */
  1418. val = snd_soc_component_read32(component, gain_reg);
  1419. snd_soc_component_write(component, gain_reg, val);
  1420. break;
  1421. case SND_SOC_DAPM_POST_PMD:
  1422. aqt_codec_enable_interp_clk(component, event, w->shift);
  1423. break;
  1424. };
  1425. return 0;
  1426. }
  1427. static int aqt_codec_enable_mix_path(struct snd_soc_dapm_widget *w,
  1428. struct snd_kcontrol *kcontrol,
  1429. int event)
  1430. {
  1431. struct snd_soc_component *component =
  1432. snd_soc_dapm_to_component(w->dapm);
  1433. u16 gain_reg = 0;
  1434. u16 mix_reg = 0;
  1435. if (w->shift >= AQT1000_NUM_INTERPOLATORS) {
  1436. dev_err(component->dev, "%s: Invalid Interpolator value %d for name %s\n",
  1437. __func__, w->shift, w->name);
  1438. return -EINVAL;
  1439. };
  1440. gain_reg = AQT1000_CDC_RX1_RX_VOL_MIX_CTL +
  1441. (w->shift * AQT1000_RX_PATH_CTL_OFFSET);
  1442. mix_reg = AQT1000_CDC_RX1_RX_PATH_MIX_CTL +
  1443. (w->shift * AQT1000_RX_PATH_CTL_OFFSET);
  1444. switch (event) {
  1445. case SND_SOC_DAPM_PRE_PMU:
  1446. aqt_codec_enable_interp_clk(component, event, w->shift);
  1447. /* Clk enable */
  1448. snd_soc_component_update_bits(component, mix_reg, 0x20, 0x20);
  1449. break;
  1450. case SND_SOC_DAPM_POST_PMU:
  1451. aqt_codec_set_idle_detect_thr(component, w->shift,
  1452. INTERP_MIX_PATH);
  1453. break;
  1454. case SND_SOC_DAPM_POST_PMD:
  1455. /* Clk Disable */
  1456. snd_soc_component_update_bits(component, mix_reg, 0x20, 0x00);
  1457. aqt_codec_enable_interp_clk(component, event, w->shift);
  1458. /* Reset enable and disable */
  1459. snd_soc_component_update_bits(component, mix_reg, 0x40, 0x40);
  1460. snd_soc_component_update_bits(component, mix_reg, 0x40, 0x00);
  1461. break;
  1462. };
  1463. dev_dbg(component->dev, "%s event %d name %s\n", __func__,
  1464. event, w->name);
  1465. return 0;
  1466. }
  1467. static const char * const rx_int1_1_interp_mux_text[] = {
  1468. "ZERO", "RX INT1_1 MUX",
  1469. };
  1470. static const char * const rx_int2_1_interp_mux_text[] = {
  1471. "ZERO", "RX INT2_1 MUX",
  1472. };
  1473. static const char * const rx_int1_2_interp_mux_text[] = {
  1474. "ZERO", "RX INT1_2 MUX",
  1475. };
  1476. static const char * const rx_int2_2_interp_mux_text[] = {
  1477. "ZERO", "RX INT2_2 MUX",
  1478. };
  1479. AQT_DAPM_ENUM(rx_int1_1_interp, SND_SOC_NOPM, 0, rx_int1_1_interp_mux_text);
  1480. AQT_DAPM_ENUM(rx_int2_1_interp, SND_SOC_NOPM, 0, rx_int2_1_interp_mux_text);
  1481. AQT_DAPM_ENUM(rx_int1_2_interp, SND_SOC_NOPM, 0, rx_int1_2_interp_mux_text);
  1482. AQT_DAPM_ENUM(rx_int2_2_interp, SND_SOC_NOPM, 0, rx_int2_2_interp_mux_text);
  1483. static const char * const asrc0_mux_text[] = {
  1484. "ZERO", "ASRC_IN_HPHL",
  1485. };
  1486. static const char * const asrc1_mux_text[] = {
  1487. "ZERO", "ASRC_IN_HPHR",
  1488. };
  1489. AQT_DAPM_ENUM(asrc0, AQT1000_CDC_RX_INP_MUX_SPLINE_ASRC_CFG0, 0,
  1490. asrc0_mux_text);
  1491. AQT_DAPM_ENUM(asrc1, AQT1000_CDC_RX_INP_MUX_SPLINE_ASRC_CFG0, 2,
  1492. asrc1_mux_text);
  1493. static int aqt_get_asrc_mode(struct aqt1000 *aqt, int asrc,
  1494. u8 main_sr, u8 mix_sr)
  1495. {
  1496. u8 asrc_output_mode;
  1497. int asrc_mode = CONV_88P2K_TO_384K;
  1498. if ((asrc < 0) || (asrc >= ASRC_MAX))
  1499. return 0;
  1500. asrc_output_mode = aqt->asrc_output_mode[asrc];
  1501. if (asrc_output_mode) {
  1502. /*
  1503. * If Mix sample rate is < 96KHz, use 96K to 352.8K
  1504. * conversion, or else use 384K to 352.8K conversion
  1505. */
  1506. if (mix_sr < 5)
  1507. asrc_mode = CONV_96K_TO_352P8K;
  1508. else
  1509. asrc_mode = CONV_384K_TO_352P8K;
  1510. } else {
  1511. /* Integer main and Fractional mix path */
  1512. if (main_sr < 8 && mix_sr > 9) {
  1513. asrc_mode = CONV_352P8K_TO_384K;
  1514. } else if (main_sr > 8 && mix_sr < 8) {
  1515. /* Fractional main and Integer mix path */
  1516. if (mix_sr < 5)
  1517. asrc_mode = CONV_96K_TO_352P8K;
  1518. else
  1519. asrc_mode = CONV_384K_TO_352P8K;
  1520. } else if (main_sr < 8 && mix_sr < 8) {
  1521. /* Integer main and Integer mix path */
  1522. asrc_mode = CONV_96K_TO_384K;
  1523. }
  1524. }
  1525. return asrc_mode;
  1526. }
  1527. static int aqt_codec_enable_asrc_resampler(struct snd_soc_dapm_widget *w,
  1528. struct snd_kcontrol *kcontrol,
  1529. int event)
  1530. {
  1531. struct snd_soc_component *component =
  1532. snd_soc_dapm_to_component(w->dapm);
  1533. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1534. int asrc = 0, ret = 0;
  1535. u8 cfg;
  1536. u16 cfg_reg = 0;
  1537. u16 ctl_reg = 0;
  1538. u16 clk_reg = 0;
  1539. u16 asrc_ctl = 0;
  1540. u16 mix_ctl_reg = 0;
  1541. u16 paired_reg = 0;
  1542. u8 main_sr, mix_sr, asrc_mode = 0;
  1543. cfg = snd_soc_component_read32(component,
  1544. AQT1000_CDC_RX_INP_MUX_SPLINE_ASRC_CFG0);
  1545. if (!(cfg & 0xFF)) {
  1546. dev_err(component->dev, "%s: ASRC%u input not selected\n",
  1547. __func__, w->shift);
  1548. return -EINVAL;
  1549. }
  1550. switch (w->shift) {
  1551. case ASRC0:
  1552. if ((cfg & 0x03) == 0x01) {
  1553. cfg_reg = AQT1000_CDC_RX1_RX_PATH_CFG0;
  1554. ctl_reg = AQT1000_CDC_RX1_RX_PATH_CTL;
  1555. clk_reg = AQT1000_MIXING_ASRC0_CLK_RST_CTL;
  1556. paired_reg = AQT1000_MIXING_ASRC1_CLK_RST_CTL;
  1557. asrc_ctl = AQT1000_MIXING_ASRC0_CTL1;
  1558. }
  1559. break;
  1560. case ASRC1:
  1561. if ((cfg & 0x0C) == 0x4) {
  1562. cfg_reg = AQT1000_CDC_RX2_RX_PATH_CFG0;
  1563. ctl_reg = AQT1000_CDC_RX2_RX_PATH_CTL;
  1564. clk_reg = AQT1000_MIXING_ASRC1_CLK_RST_CTL;
  1565. paired_reg = AQT1000_MIXING_ASRC0_CLK_RST_CTL;
  1566. asrc_ctl = AQT1000_MIXING_ASRC1_CTL1;
  1567. }
  1568. break;
  1569. default:
  1570. dev_err(component->dev, "%s: Invalid asrc:%u\n", __func__,
  1571. w->shift);
  1572. ret = -EINVAL;
  1573. break;
  1574. };
  1575. if ((cfg_reg == 0) || (ctl_reg == 0) || (clk_reg == 0) ||
  1576. (asrc_ctl == 0) || ret)
  1577. goto done;
  1578. switch (event) {
  1579. case SND_SOC_DAPM_PRE_PMU:
  1580. if ((snd_soc_component_read32(component, clk_reg) & 0x02) ||
  1581. (snd_soc_component_read32(component, paired_reg) & 0x02)) {
  1582. snd_soc_component_update_bits(component, clk_reg,
  1583. 0x02, 0x00);
  1584. snd_soc_component_update_bits(component, paired_reg,
  1585. 0x02, 0x00);
  1586. }
  1587. snd_soc_component_update_bits(component, cfg_reg, 0x80, 0x80);
  1588. snd_soc_component_update_bits(component, clk_reg, 0x01, 0x01);
  1589. main_sr = snd_soc_component_read32(component, ctl_reg) & 0x0F;
  1590. mix_ctl_reg = ctl_reg + 5;
  1591. mix_sr = snd_soc_component_read32(
  1592. component, mix_ctl_reg) & 0x0F;
  1593. asrc_mode = aqt_get_asrc_mode(aqt, asrc,
  1594. main_sr, mix_sr);
  1595. dev_dbg(component->dev, "%s: main_sr:%d mix_sr:%d asrc_mode %d\n",
  1596. __func__, main_sr, mix_sr, asrc_mode);
  1597. snd_soc_component_update_bits(
  1598. component, asrc_ctl, 0x07, asrc_mode);
  1599. break;
  1600. case SND_SOC_DAPM_POST_PMD:
  1601. snd_soc_component_update_bits(component, asrc_ctl, 0x07, 0x00);
  1602. snd_soc_component_update_bits(component, cfg_reg, 0x80, 0x00);
  1603. snd_soc_component_update_bits(component, clk_reg, 0x03, 0x02);
  1604. break;
  1605. };
  1606. done:
  1607. return ret;
  1608. }
  1609. static int aqt_codec_enable_anc(struct snd_soc_dapm_widget *w,
  1610. struct snd_kcontrol *kcontrol, int event)
  1611. {
  1612. struct snd_soc_component *component =
  1613. snd_soc_dapm_to_component(w->dapm);
  1614. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1615. const char *filename;
  1616. const struct firmware *fw;
  1617. int i;
  1618. int ret = 0;
  1619. int num_anc_slots;
  1620. struct aqt1000_anc_header *anc_head;
  1621. struct firmware_cal *hwdep_cal = NULL;
  1622. u32 anc_writes_size = 0;
  1623. u32 anc_cal_size = 0;
  1624. int anc_size_remaining;
  1625. u32 *anc_ptr;
  1626. u16 reg;
  1627. u8 mask, val;
  1628. size_t cal_size;
  1629. const void *data;
  1630. if (!aqt->anc_func)
  1631. return 0;
  1632. switch (event) {
  1633. case SND_SOC_DAPM_PRE_PMU:
  1634. hwdep_cal = wcdcal_get_fw_cal(aqt->fw_data, WCD9XXX_ANC_CAL);
  1635. if (hwdep_cal) {
  1636. data = hwdep_cal->data;
  1637. cal_size = hwdep_cal->size;
  1638. dev_dbg(component->dev, "%s: using hwdep calibration, cal_size %zd",
  1639. __func__, cal_size);
  1640. } else {
  1641. filename = "AQT1000/AQT1000_anc.bin";
  1642. ret = request_firmware(&fw, filename, component->dev);
  1643. if (ret < 0) {
  1644. dev_err(component->dev, "%s: Failed to acquire ANC data: %d\n",
  1645. __func__, ret);
  1646. return ret;
  1647. }
  1648. if (!fw) {
  1649. dev_err(component->dev, "%s: Failed to get anc fw\n",
  1650. __func__);
  1651. return -ENODEV;
  1652. }
  1653. data = fw->data;
  1654. cal_size = fw->size;
  1655. dev_dbg(component->dev, "%s: using request_firmware calibration\n",
  1656. __func__);
  1657. }
  1658. if (cal_size < sizeof(struct aqt1000_anc_header)) {
  1659. dev_err(component->dev, "%s: Invalid cal_size %zd\n",
  1660. __func__, cal_size);
  1661. ret = -EINVAL;
  1662. goto err;
  1663. }
  1664. /* First number is the number of register writes */
  1665. anc_head = (struct aqt1000_anc_header *)(data);
  1666. anc_ptr = (u32 *)(data + sizeof(struct aqt1000_anc_header));
  1667. anc_size_remaining = cal_size -
  1668. sizeof(struct aqt1000_anc_header);
  1669. num_anc_slots = anc_head->num_anc_slots;
  1670. if (aqt->anc_slot >= num_anc_slots) {
  1671. dev_err(component->dev, "%s: Invalid ANC slot selected\n",
  1672. __func__);
  1673. ret = -EINVAL;
  1674. goto err;
  1675. }
  1676. for (i = 0; i < num_anc_slots; i++) {
  1677. if (anc_size_remaining < AQT1000_PACKED_REG_SIZE) {
  1678. dev_err(component->dev, "%s: Invalid register format\n",
  1679. __func__);
  1680. ret = -EINVAL;
  1681. goto err;
  1682. }
  1683. anc_writes_size = (u32)(*anc_ptr);
  1684. anc_size_remaining -= sizeof(u32);
  1685. anc_ptr += 1;
  1686. if ((anc_writes_size * AQT1000_PACKED_REG_SIZE) >
  1687. anc_size_remaining) {
  1688. dev_err(component->dev, "%s: Invalid register format\n",
  1689. __func__);
  1690. ret = -EINVAL;
  1691. goto err;
  1692. }
  1693. if (aqt->anc_slot == i)
  1694. break;
  1695. anc_size_remaining -= (anc_writes_size *
  1696. AQT1000_PACKED_REG_SIZE);
  1697. anc_ptr += anc_writes_size;
  1698. }
  1699. if (i == num_anc_slots) {
  1700. dev_err(component->dev, "%s: Selected ANC slot not present\n",
  1701. __func__);
  1702. ret = -EINVAL;
  1703. goto err;
  1704. }
  1705. i = 0;
  1706. anc_cal_size = anc_writes_size;
  1707. /* Rate converter clk enable and set bypass mode */
  1708. if (!strcmp(w->name, "AQT RX INT1 DAC")) {
  1709. snd_soc_component_update_bits(component,
  1710. AQT1000_CDC_ANC0_RC_COMMON_CTL,
  1711. 0x05, 0x05);
  1712. snd_soc_component_update_bits(component,
  1713. AQT1000_CDC_ANC0_FIFO_COMMON_CTL,
  1714. 0x66, 0x66);
  1715. anc_writes_size = anc_cal_size / 2;
  1716. snd_soc_component_update_bits(component,
  1717. AQT1000_CDC_ANC0_CLK_RESET_CTL, 0x39, 0x39);
  1718. } else if (!strcmp(w->name, "AQT RX INT2 DAC")) {
  1719. snd_soc_component_update_bits(component,
  1720. AQT1000_CDC_ANC1_RC_COMMON_CTL,
  1721. 0x05, 0x05);
  1722. snd_soc_component_update_bits(component,
  1723. AQT1000_CDC_ANC1_FIFO_COMMON_CTL,
  1724. 0x66, 0x66);
  1725. i = anc_cal_size / 2;
  1726. snd_soc_component_update_bits(component,
  1727. AQT1000_CDC_ANC1_CLK_RESET_CTL, 0x39, 0x39);
  1728. }
  1729. for (; i < anc_writes_size; i++) {
  1730. AQT1000_CODEC_UNPACK_ENTRY(anc_ptr[i], reg, mask, val);
  1731. snd_soc_component_write(component, reg, (val & mask));
  1732. }
  1733. if (!strcmp(w->name, "AQT RX INT1 DAC"))
  1734. snd_soc_component_update_bits(component,
  1735. AQT1000_CDC_ANC0_CLK_RESET_CTL, 0x08, 0x08);
  1736. else if (!strcmp(w->name, "AQT RX INT2 DAC"))
  1737. snd_soc_component_update_bits(component,
  1738. AQT1000_CDC_ANC1_CLK_RESET_CTL, 0x08, 0x08);
  1739. if (!hwdep_cal)
  1740. release_firmware(fw);
  1741. break;
  1742. case SND_SOC_DAPM_POST_PMU:
  1743. /* Remove ANC Rx from reset */
  1744. snd_soc_component_update_bits(component,
  1745. AQT1000_CDC_ANC0_CLK_RESET_CTL,
  1746. 0x08, 0x00);
  1747. snd_soc_component_update_bits(component,
  1748. AQT1000_CDC_ANC1_CLK_RESET_CTL,
  1749. 0x08, 0x00);
  1750. break;
  1751. case SND_SOC_DAPM_POST_PMD:
  1752. snd_soc_component_update_bits(component,
  1753. AQT1000_CDC_ANC0_RC_COMMON_CTL,
  1754. 0x05, 0x00);
  1755. if (!strcmp(w->name, "AQT ANC HPHL PA")) {
  1756. snd_soc_component_update_bits(component,
  1757. AQT1000_CDC_ANC0_MODE_1_CTL,
  1758. 0x30, 0x00);
  1759. /* 50 msec sleep is needed to avoid click and pop as
  1760. * per HW requirement
  1761. */
  1762. msleep(50);
  1763. snd_soc_component_update_bits(component,
  1764. AQT1000_CDC_ANC0_MODE_1_CTL,
  1765. 0x01, 0x00);
  1766. snd_soc_component_update_bits(component,
  1767. AQT1000_CDC_ANC0_CLK_RESET_CTL,
  1768. 0x38, 0x38);
  1769. snd_soc_component_update_bits(component,
  1770. AQT1000_CDC_ANC0_CLK_RESET_CTL,
  1771. 0x07, 0x00);
  1772. snd_soc_component_update_bits(component,
  1773. AQT1000_CDC_ANC0_CLK_RESET_CTL,
  1774. 0x38, 0x00);
  1775. } else if (!strcmp(w->name, "AQT ANC HPHR PA")) {
  1776. snd_soc_component_update_bits(component,
  1777. AQT1000_CDC_ANC1_MODE_1_CTL,
  1778. 0x30, 0x00);
  1779. /* 50 msec sleep is needed to avoid click and pop as
  1780. * per HW requirement
  1781. */
  1782. msleep(50);
  1783. snd_soc_component_update_bits(component,
  1784. AQT1000_CDC_ANC1_MODE_1_CTL,
  1785. 0x01, 0x00);
  1786. snd_soc_component_update_bits(component,
  1787. AQT1000_CDC_ANC1_CLK_RESET_CTL,
  1788. 0x38, 0x38);
  1789. snd_soc_component_update_bits(component,
  1790. AQT1000_CDC_ANC1_CLK_RESET_CTL,
  1791. 0x07, 0x00);
  1792. snd_soc_component_update_bits(component,
  1793. AQT1000_CDC_ANC1_CLK_RESET_CTL,
  1794. 0x38, 0x00);
  1795. }
  1796. break;
  1797. }
  1798. return 0;
  1799. err:
  1800. if (!hwdep_cal)
  1801. release_firmware(fw);
  1802. return ret;
  1803. }
  1804. static void aqt_codec_override(struct snd_soc_component *component, int mode,
  1805. int event)
  1806. {
  1807. if (mode == CLS_AB || mode == CLS_AB_HIFI) {
  1808. switch (event) {
  1809. case SND_SOC_DAPM_PRE_PMU:
  1810. case SND_SOC_DAPM_POST_PMU:
  1811. snd_soc_component_update_bits(component,
  1812. AQT1000_ANA_RX_SUPPLIES, 0x02, 0x02);
  1813. break;
  1814. case SND_SOC_DAPM_POST_PMD:
  1815. snd_soc_component_update_bits(component,
  1816. AQT1000_ANA_RX_SUPPLIES, 0x02, 0x00);
  1817. break;
  1818. }
  1819. }
  1820. }
  1821. static void aqt_codec_set_tx_hold(struct snd_soc_component *component,
  1822. u16 amic_reg, bool set)
  1823. {
  1824. u8 mask = 0x20;
  1825. u8 val;
  1826. if (amic_reg == AQT1000_ANA_AMIC1 ||
  1827. amic_reg == AQT1000_ANA_AMIC3)
  1828. mask = 0x40;
  1829. val = set ? mask : 0x00;
  1830. switch (amic_reg) {
  1831. case AQT1000_ANA_AMIC1:
  1832. case AQT1000_ANA_AMIC2:
  1833. snd_soc_component_update_bits(component, AQT1000_ANA_AMIC2,
  1834. mask, val);
  1835. break;
  1836. case AQT1000_ANA_AMIC3:
  1837. snd_soc_component_update_bits(component, AQT1000_ANA_AMIC3_HPF,
  1838. mask, val);
  1839. break;
  1840. default:
  1841. dev_dbg(component->dev, "%s: invalid amic: %d\n",
  1842. __func__, amic_reg);
  1843. break;
  1844. }
  1845. }
  1846. static void aqt_codec_clear_anc_tx_hold(struct aqt1000 *aqt)
  1847. {
  1848. if (test_and_clear_bit(ANC_MIC_AMIC1, &aqt->status_mask))
  1849. aqt_codec_set_tx_hold(aqt->component, AQT1000_ANA_AMIC1, false);
  1850. if (test_and_clear_bit(ANC_MIC_AMIC2, &aqt->status_mask))
  1851. aqt_codec_set_tx_hold(aqt->component, AQT1000_ANA_AMIC2, false);
  1852. if (test_and_clear_bit(ANC_MIC_AMIC3, &aqt->status_mask))
  1853. aqt_codec_set_tx_hold(aqt->component, AQT1000_ANA_AMIC3, false);
  1854. }
  1855. static const char * const rx_int_dem_inp_mux_text[] = {
  1856. "NORMAL_DSM_OUT", "CLSH_DSM_OUT",
  1857. };
  1858. static int aqt_int_dem_inp_mux_put(struct snd_kcontrol *kcontrol,
  1859. struct snd_ctl_elem_value *ucontrol)
  1860. {
  1861. struct snd_soc_dapm_widget *widget =
  1862. snd_soc_dapm_kcontrol_widget(kcontrol);
  1863. struct snd_soc_component *component =
  1864. snd_soc_dapm_to_component(widget->dapm);
  1865. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1866. unsigned int val;
  1867. unsigned short look_ahead_dly_reg = AQT1000_CDC_RX1_RX_PATH_CFG0;
  1868. val = ucontrol->value.enumerated.item[0];
  1869. if (val >= e->items)
  1870. return -EINVAL;
  1871. dev_dbg(component->dev, "%s: wname: %s, val: 0x%x\n", __func__,
  1872. widget->name, val);
  1873. if (e->reg == AQT1000_CDC_RX1_RX_PATH_SEC0)
  1874. look_ahead_dly_reg = AQT1000_CDC_RX1_RX_PATH_CFG0;
  1875. else if (e->reg == AQT1000_CDC_RX2_RX_PATH_SEC0)
  1876. look_ahead_dly_reg = AQT1000_CDC_RX2_RX_PATH_CFG0;
  1877. /* Set Look Ahead Delay */
  1878. snd_soc_component_update_bits(component, look_ahead_dly_reg,
  1879. 0x08, (val ? 0x08 : 0x00));
  1880. /* Set DEM INP Select */
  1881. return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  1882. }
  1883. AQT_DAPM_ENUM_EXT(rx_int1_dem, AQT1000_CDC_RX1_RX_PATH_SEC0, 0,
  1884. rx_int_dem_inp_mux_text, snd_soc_dapm_get_enum_double,
  1885. aqt_int_dem_inp_mux_put);
  1886. AQT_DAPM_ENUM_EXT(rx_int2_dem, AQT1000_CDC_RX2_RX_PATH_SEC0, 0,
  1887. rx_int_dem_inp_mux_text, snd_soc_dapm_get_enum_double,
  1888. aqt_int_dem_inp_mux_put);
  1889. static int aqt_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  1890. struct snd_kcontrol *kcontrol,
  1891. int event)
  1892. {
  1893. struct snd_soc_component *component =
  1894. snd_soc_dapm_to_component(w->dapm);
  1895. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1896. int hph_mode = aqt->hph_mode;
  1897. u8 dem_inp;
  1898. int ret = 0;
  1899. uint32_t impedl = 0;
  1900. uint32_t impedr = 0;
  1901. dev_dbg(component->dev, "%s wname: %s event: %d hph_mode: %d\n",
  1902. __func__, w->name, event, hph_mode);
  1903. switch (event) {
  1904. case SND_SOC_DAPM_PRE_PMU:
  1905. if (aqt->anc_func) {
  1906. ret = aqt_codec_enable_anc(w, kcontrol, event);
  1907. /* 40 msec delay is needed to avoid click and pop */
  1908. msleep(40);
  1909. }
  1910. /* Read DEM INP Select */
  1911. dem_inp = snd_soc_component_read32(
  1912. component, AQT1000_CDC_RX1_RX_PATH_SEC0) &
  1913. 0x03;
  1914. if (((hph_mode == CLS_H_HIFI) || (hph_mode == CLS_H_LOHIFI) ||
  1915. (hph_mode == CLS_H_LP)) && (dem_inp != 0x01)) {
  1916. dev_err(component->dev, "%s: DEM Input not set correctly, hph_mode: %d\n",
  1917. __func__, hph_mode);
  1918. return -EINVAL;
  1919. }
  1920. /* Disable AutoChop timer during power up */
  1921. snd_soc_component_update_bits(component,
  1922. AQT1000_HPH_NEW_INT_HPH_TIMER1,
  1923. 0x02, 0x00);
  1924. aqt_clsh_fsm(component, &aqt->clsh_d,
  1925. AQT_CLSH_EVENT_PRE_DAC,
  1926. AQT_CLSH_STATE_HPHL,
  1927. hph_mode);
  1928. if (aqt->anc_func)
  1929. snd_soc_component_update_bits(component,
  1930. AQT1000_CDC_RX1_RX_PATH_CFG0,
  1931. 0x10, 0x10);
  1932. ret = aqt_mbhc_get_impedance(aqt->mbhc,
  1933. &impedl, &impedr);
  1934. if (!ret) {
  1935. aqt_clsh_imped_config(component, impedl, false);
  1936. set_bit(CLSH_Z_CONFIG, &aqt->status_mask);
  1937. } else {
  1938. dev_dbg(component->dev, "%s: Failed to get mbhc impedance %d\n",
  1939. __func__, ret);
  1940. ret = 0;
  1941. }
  1942. break;
  1943. case SND_SOC_DAPM_POST_PMD:
  1944. /* 1000us required as per HW requirement */
  1945. usleep_range(1000, 1100);
  1946. aqt_clsh_fsm(component, &aqt->clsh_d,
  1947. AQT_CLSH_EVENT_POST_PA,
  1948. AQT_CLSH_STATE_HPHL,
  1949. hph_mode);
  1950. if (test_bit(CLSH_Z_CONFIG, &aqt->status_mask)) {
  1951. aqt_clsh_imped_config(component, impedl, true);
  1952. clear_bit(CLSH_Z_CONFIG, &aqt->status_mask);
  1953. }
  1954. break;
  1955. default:
  1956. break;
  1957. };
  1958. return ret;
  1959. }
  1960. static int aqt_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  1961. struct snd_kcontrol *kcontrol,
  1962. int event)
  1963. {
  1964. struct snd_soc_component *component =
  1965. snd_soc_dapm_to_component(w->dapm);
  1966. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  1967. int hph_mode = aqt->hph_mode;
  1968. u8 dem_inp;
  1969. int ret = 0;
  1970. dev_dbg(component->dev, "%s wname: %s event: %d hph_mode: %d\n",
  1971. __func__, w->name, event, hph_mode);
  1972. switch (event) {
  1973. case SND_SOC_DAPM_PRE_PMU:
  1974. if (aqt->anc_func) {
  1975. ret = aqt_codec_enable_anc(w, kcontrol, event);
  1976. /* 40 msec delay is needed to avoid click and pop */
  1977. msleep(40);
  1978. }
  1979. /* Read DEM INP Select */
  1980. dem_inp = snd_soc_component_read32(
  1981. component, AQT1000_CDC_RX2_RX_PATH_SEC0) &
  1982. 0x03;
  1983. if (((hph_mode == CLS_H_HIFI) || (hph_mode == CLS_H_LOHIFI) ||
  1984. (hph_mode == CLS_H_LP)) && (dem_inp != 0x01)) {
  1985. dev_err(component->dev, "%s: DEM Input not set correctly, hph_mode: %d\n",
  1986. __func__, hph_mode);
  1987. return -EINVAL;
  1988. }
  1989. /* Disable AutoChop timer during power up */
  1990. snd_soc_component_update_bits(component,
  1991. AQT1000_HPH_NEW_INT_HPH_TIMER1,
  1992. 0x02, 0x00);
  1993. aqt_clsh_fsm(component, &aqt->clsh_d,
  1994. AQT_CLSH_EVENT_PRE_DAC,
  1995. AQT_CLSH_STATE_HPHR,
  1996. hph_mode);
  1997. if (aqt->anc_func)
  1998. snd_soc_component_update_bits(component,
  1999. AQT1000_CDC_RX2_RX_PATH_CFG0,
  2000. 0x10, 0x10);
  2001. break;
  2002. case SND_SOC_DAPM_POST_PMD:
  2003. /* 1000us required as per HW requirement */
  2004. usleep_range(1000, 1100);
  2005. aqt_clsh_fsm(component, &aqt->clsh_d,
  2006. AQT_CLSH_EVENT_POST_PA,
  2007. AQT_CLSH_STATE_HPHR,
  2008. hph_mode);
  2009. break;
  2010. default:
  2011. break;
  2012. };
  2013. return 0;
  2014. }
  2015. static int aqt_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  2016. struct snd_kcontrol *kcontrol,
  2017. int event)
  2018. {
  2019. struct snd_soc_component *component =
  2020. snd_soc_dapm_to_component(w->dapm);
  2021. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  2022. int ret = 0;
  2023. dev_dbg(component->dev, "%s %s %d\n", __func__, w->name, event);
  2024. switch (event) {
  2025. case SND_SOC_DAPM_PRE_PMU:
  2026. if ((!(strcmp(w->name, "AQT ANC HPHR PA"))) &&
  2027. (test_bit(HPH_PA_DELAY, &aqt->status_mask)))
  2028. snd_soc_component_update_bits(component,
  2029. AQT1000_ANA_HPH, 0xC0, 0xC0);
  2030. set_bit(HPH_PA_DELAY, &aqt->status_mask);
  2031. break;
  2032. case SND_SOC_DAPM_POST_PMU:
  2033. if ((!(strcmp(w->name, "AQT ANC HPHR PA")))) {
  2034. if ((snd_soc_component_read32(
  2035. component, AQT1000_ANA_HPH) & 0xC0)
  2036. != 0xC0)
  2037. /*
  2038. * If PA_EN is not set (potentially in ANC case)
  2039. * then do nothing for POST_PMU and let left
  2040. * channel handle everything.
  2041. */
  2042. break;
  2043. }
  2044. /*
  2045. * 7ms sleep is required after PA is enabled as per
  2046. * HW requirement. If compander is disabled, then
  2047. * 20ms delay is needed.
  2048. */
  2049. if (test_bit(HPH_PA_DELAY, &aqt->status_mask)) {
  2050. if (!aqt->comp_enabled[COMPANDER_2])
  2051. usleep_range(20000, 20100);
  2052. else
  2053. usleep_range(7000, 7100);
  2054. clear_bit(HPH_PA_DELAY, &aqt->status_mask);
  2055. }
  2056. if (aqt->anc_func) {
  2057. /* Clear Tx FE HOLD if both PAs are enabled */
  2058. if ((snd_soc_component_read32(
  2059. aqt->component, AQT1000_ANA_HPH) &
  2060. 0xC0) == 0xC0)
  2061. aqt_codec_clear_anc_tx_hold(aqt);
  2062. }
  2063. snd_soc_component_update_bits(
  2064. component, AQT1000_HPH_R_TEST, 0x01, 0x01);
  2065. /* Remove mute */
  2066. snd_soc_component_update_bits(
  2067. component, AQT1000_CDC_RX2_RX_PATH_CTL,
  2068. 0x10, 0x00);
  2069. /* Enable GM3 boost */
  2070. snd_soc_component_update_bits(
  2071. component, AQT1000_HPH_CNP_WG_CTL,
  2072. 0x80, 0x80);
  2073. /* Enable AutoChop timer at the end of power up */
  2074. snd_soc_component_update_bits(component,
  2075. AQT1000_HPH_NEW_INT_HPH_TIMER1,
  2076. 0x02, 0x02);
  2077. /* Remove mix path mute if it is enabled */
  2078. if ((snd_soc_component_read32(
  2079. component, AQT1000_CDC_RX2_RX_PATH_MIX_CTL)) &
  2080. 0x10)
  2081. snd_soc_component_update_bits(component,
  2082. AQT1000_CDC_RX2_RX_PATH_MIX_CTL,
  2083. 0x10, 0x00);
  2084. if (!(strcmp(w->name, "AQT ANC HPHR PA"))) {
  2085. dev_dbg(component->dev,
  2086. "%s:Do everything needed for left channel\n",
  2087. __func__);
  2088. /* Do everything needed for left channel */
  2089. snd_soc_component_update_bits(
  2090. component, AQT1000_HPH_L_TEST,
  2091. 0x01, 0x01);
  2092. /* Remove mute */
  2093. snd_soc_component_update_bits(component,
  2094. AQT1000_CDC_RX1_RX_PATH_CTL,
  2095. 0x10, 0x00);
  2096. /* Remove mix path mute if it is enabled */
  2097. if ((snd_soc_component_read32(component,
  2098. AQT1000_CDC_RX1_RX_PATH_MIX_CTL)) &
  2099. 0x10)
  2100. snd_soc_component_update_bits(component,
  2101. AQT1000_CDC_RX1_RX_PATH_MIX_CTL,
  2102. 0x10, 0x00);
  2103. /* Remove ANC Rx from reset */
  2104. ret = aqt_codec_enable_anc(w, kcontrol, event);
  2105. }
  2106. aqt_codec_override(component, aqt->hph_mode, event);
  2107. break;
  2108. case SND_SOC_DAPM_PRE_PMD:
  2109. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  2110. AQT_EVENT_PRE_HPHR_PA_OFF,
  2111. &aqt->mbhc->wcd_mbhc);
  2112. snd_soc_component_update_bits(component,
  2113. AQT1000_HPH_R_TEST, 0x01, 0x00);
  2114. snd_soc_component_update_bits(component,
  2115. AQT1000_CDC_RX2_RX_PATH_CTL,
  2116. 0x10, 0x10);
  2117. snd_soc_component_update_bits(component,
  2118. AQT1000_CDC_RX2_RX_PATH_MIX_CTL,
  2119. 0x10, 0x10);
  2120. if (!(strcmp(w->name, "AQT ANC HPHR PA")))
  2121. snd_soc_component_update_bits(component,
  2122. AQT1000_ANA_HPH, 0x40, 0x00);
  2123. break;
  2124. case SND_SOC_DAPM_POST_PMD:
  2125. /*
  2126. * 5ms sleep is required after PA disable. If compander is
  2127. * disabled, then 20ms delay is needed after PA disable.
  2128. */
  2129. if (!aqt->comp_enabled[COMPANDER_2])
  2130. usleep_range(20000, 20100);
  2131. else
  2132. usleep_range(5000, 5100);
  2133. aqt_codec_override(component, aqt->hph_mode, event);
  2134. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  2135. AQT_EVENT_POST_HPHR_PA_OFF,
  2136. &aqt->mbhc->wcd_mbhc);
  2137. if (!(strcmp(w->name, "AQT ANC HPHR PA"))) {
  2138. ret = aqt_codec_enable_anc(w, kcontrol, event);
  2139. snd_soc_component_update_bits(component,
  2140. AQT1000_CDC_RX2_RX_PATH_CFG0,
  2141. 0x10, 0x00);
  2142. }
  2143. break;
  2144. };
  2145. return ret;
  2146. }
  2147. static int aqt_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  2148. struct snd_kcontrol *kcontrol,
  2149. int event)
  2150. {
  2151. struct snd_soc_component *component =
  2152. snd_soc_dapm_to_component(w->dapm);
  2153. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  2154. int ret = 0;
  2155. dev_dbg(component->dev, "%s %s %d\n", __func__, w->name, event);
  2156. switch (event) {
  2157. case SND_SOC_DAPM_PRE_PMU:
  2158. if ((!(strcmp(w->name, "AQT ANC HPHL PA"))) &&
  2159. (test_bit(HPH_PA_DELAY, &aqt->status_mask)))
  2160. snd_soc_component_update_bits(component,
  2161. AQT1000_ANA_HPH,
  2162. 0xC0, 0xC0);
  2163. set_bit(HPH_PA_DELAY, &aqt->status_mask);
  2164. break;
  2165. case SND_SOC_DAPM_POST_PMU:
  2166. if (!(strcmp(w->name, "AQT ANC HPHL PA"))) {
  2167. if ((snd_soc_component_read32(
  2168. component, AQT1000_ANA_HPH) & 0xC0)
  2169. != 0xC0)
  2170. /*
  2171. * If PA_EN is not set (potentially in ANC
  2172. * case) then do nothing for POST_PMU and
  2173. * let right channel handle everything.
  2174. */
  2175. break;
  2176. }
  2177. /*
  2178. * 7ms sleep is required after PA is enabled as per
  2179. * HW requirement. If compander is disabled, then
  2180. * 20ms delay is needed.
  2181. */
  2182. if (test_bit(HPH_PA_DELAY, &aqt->status_mask)) {
  2183. if (!aqt->comp_enabled[COMPANDER_1])
  2184. usleep_range(20000, 20100);
  2185. else
  2186. usleep_range(7000, 7100);
  2187. clear_bit(HPH_PA_DELAY, &aqt->status_mask);
  2188. }
  2189. if (aqt->anc_func) {
  2190. /* Clear Tx FE HOLD if both PAs are enabled */
  2191. if ((snd_soc_component_read32(
  2192. aqt->component, AQT1000_ANA_HPH) &
  2193. 0xC0) == 0xC0)
  2194. aqt_codec_clear_anc_tx_hold(aqt);
  2195. }
  2196. snd_soc_component_update_bits(component,
  2197. AQT1000_HPH_L_TEST, 0x01, 0x01);
  2198. /* Remove Mute on primary path */
  2199. snd_soc_component_update_bits(component,
  2200. AQT1000_CDC_RX1_RX_PATH_CTL,
  2201. 0x10, 0x00);
  2202. /* Enable GM3 boost */
  2203. snd_soc_component_update_bits(component,
  2204. AQT1000_HPH_CNP_WG_CTL,
  2205. 0x80, 0x80);
  2206. /* Enable AutoChop timer at the end of power up */
  2207. snd_soc_component_update_bits(component,
  2208. AQT1000_HPH_NEW_INT_HPH_TIMER1,
  2209. 0x02, 0x02);
  2210. /* Remove mix path mute if it is enabled */
  2211. if ((snd_soc_component_read32(component,
  2212. AQT1000_CDC_RX1_RX_PATH_MIX_CTL)) &
  2213. 0x10)
  2214. snd_soc_component_update_bits(component,
  2215. AQT1000_CDC_RX1_RX_PATH_MIX_CTL,
  2216. 0x10, 0x00);
  2217. if (!(strcmp(w->name, "AQT ANC HPHL PA"))) {
  2218. dev_dbg(component->dev,
  2219. "%s:Do everything needed for right channel\n",
  2220. __func__);
  2221. /* Do everything needed for right channel */
  2222. snd_soc_component_update_bits(component,
  2223. AQT1000_HPH_R_TEST,
  2224. 0x01, 0x01);
  2225. /* Remove mute */
  2226. snd_soc_component_update_bits(component,
  2227. AQT1000_CDC_RX2_RX_PATH_CTL,
  2228. 0x10, 0x00);
  2229. /* Remove mix path mute if it is enabled */
  2230. if ((snd_soc_component_read32(component,
  2231. AQT1000_CDC_RX2_RX_PATH_MIX_CTL)) &
  2232. 0x10)
  2233. snd_soc_component_update_bits(component,
  2234. AQT1000_CDC_RX2_RX_PATH_MIX_CTL,
  2235. 0x10, 0x00);
  2236. /* Remove ANC Rx from reset */
  2237. ret = aqt_codec_enable_anc(w, kcontrol, event);
  2238. }
  2239. aqt_codec_override(component, aqt->hph_mode, event);
  2240. break;
  2241. case SND_SOC_DAPM_PRE_PMD:
  2242. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  2243. AQT_EVENT_PRE_HPHL_PA_OFF,
  2244. &aqt->mbhc->wcd_mbhc);
  2245. snd_soc_component_update_bits(component,
  2246. AQT1000_HPH_L_TEST, 0x01, 0x00);
  2247. snd_soc_component_update_bits(component,
  2248. AQT1000_CDC_RX1_RX_PATH_CTL, 0x10, 0x10);
  2249. snd_soc_component_update_bits(component,
  2250. AQT1000_CDC_RX1_RX_PATH_MIX_CTL, 0x10, 0x10);
  2251. if (!(strcmp(w->name, "AQT ANC HPHL PA")))
  2252. snd_soc_component_update_bits(component,
  2253. AQT1000_ANA_HPH, 0x80, 0x00);
  2254. break;
  2255. case SND_SOC_DAPM_POST_PMD:
  2256. /*
  2257. * 5ms sleep is required after PA disable. If compander is
  2258. * disabled, then 20ms delay is needed after PA disable.
  2259. */
  2260. if (!aqt->comp_enabled[COMPANDER_1])
  2261. usleep_range(20000, 20100);
  2262. else
  2263. usleep_range(5000, 5100);
  2264. aqt_codec_override(component, aqt->hph_mode, event);
  2265. blocking_notifier_call_chain(&aqt->mbhc->notifier,
  2266. AQT_EVENT_POST_HPHL_PA_OFF,
  2267. &aqt->mbhc->wcd_mbhc);
  2268. if (!(strcmp(w->name, "AQT ANC HPHL PA"))) {
  2269. ret = aqt_codec_enable_anc(w, kcontrol, event);
  2270. snd_soc_component_update_bits(component,
  2271. AQT1000_CDC_RX1_RX_PATH_CFG0, 0x10, 0x00);
  2272. }
  2273. break;
  2274. };
  2275. return ret;
  2276. }
  2277. static int aqt_codec_set_iir_gain(struct snd_soc_dapm_widget *w,
  2278. struct snd_kcontrol *kcontrol, int event)
  2279. {
  2280. struct snd_soc_component *component =
  2281. snd_soc_dapm_to_component(w->dapm);
  2282. dev_dbg(component->dev, "%s: event = %d\n", __func__, event);
  2283. switch (event) {
  2284. case SND_SOC_DAPM_POST_PMU: /* fall through */
  2285. case SND_SOC_DAPM_PRE_PMD:
  2286. if (strnstr(w->name, "AQT IIR0", sizeof("AQT IIR0"))) {
  2287. snd_soc_component_write(component,
  2288. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B1_CTL,
  2289. snd_soc_component_read32(component,
  2290. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B1_CTL));
  2291. snd_soc_component_write(component,
  2292. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B2_CTL,
  2293. snd_soc_component_read32(component,
  2294. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B2_CTL));
  2295. snd_soc_component_write(component,
  2296. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B3_CTL,
  2297. snd_soc_component_read32(component,
  2298. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B3_CTL));
  2299. snd_soc_component_write(component,
  2300. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B4_CTL,
  2301. snd_soc_component_read32(component,
  2302. AQT1000_CDC_SIDETONE_IIR0_IIR_GAIN_B4_CTL));
  2303. }
  2304. break;
  2305. }
  2306. return 0;
  2307. }
  2308. static int aqt_enable_native_supply(struct snd_soc_dapm_widget *w,
  2309. struct snd_kcontrol *kcontrol, int event)
  2310. {
  2311. struct snd_soc_component *component =
  2312. snd_soc_dapm_to_component(w->dapm);
  2313. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  2314. switch (event) {
  2315. case SND_SOC_DAPM_PRE_PMU:
  2316. if (++aqt->native_clk_users == 1) {
  2317. snd_soc_component_update_bits(component,
  2318. AQT1000_CLK_SYS_PLL_ENABLES,
  2319. 0x01, 0x01);
  2320. /* 100usec is needed as per HW requirement */
  2321. usleep_range(100, 120);
  2322. snd_soc_component_update_bits(component,
  2323. AQT1000_CDC_CLK_RST_CTRL_MCLK_CONTROL,
  2324. 0x02, 0x02);
  2325. snd_soc_component_update_bits(component,
  2326. AQT1000_CDC_CLK_RST_CTRL_FS_CNT_CONTROL,
  2327. 0x10, 0x10);
  2328. }
  2329. break;
  2330. case SND_SOC_DAPM_PRE_PMD:
  2331. if (aqt->native_clk_users &&
  2332. (--aqt->native_clk_users == 0)) {
  2333. snd_soc_component_update_bits(component,
  2334. AQT1000_CDC_CLK_RST_CTRL_FS_CNT_CONTROL,
  2335. 0x10, 0x00);
  2336. snd_soc_component_update_bits(component,
  2337. AQT1000_CDC_CLK_RST_CTRL_MCLK_CONTROL,
  2338. 0x02, 0x00);
  2339. snd_soc_component_update_bits(component,
  2340. AQT1000_CLK_SYS_PLL_ENABLES,
  2341. 0x01, 0x00);
  2342. }
  2343. break;
  2344. }
  2345. dev_dbg(component->dev, "%s: native_clk_users: %d, event: %d\n",
  2346. __func__, aqt->native_clk_users, event);
  2347. return 0;
  2348. }
  2349. static const char * const native_mux_text[] = {
  2350. "OFF", "ON",
  2351. };
  2352. AQT_DAPM_ENUM(int1_1_native, SND_SOC_NOPM, 0, native_mux_text);
  2353. AQT_DAPM_ENUM(int2_1_native, SND_SOC_NOPM, 0, native_mux_text);
  2354. static int aqt_mclk_event(struct snd_soc_dapm_widget *w,
  2355. struct snd_kcontrol *kcontrol, int event)
  2356. {
  2357. struct snd_soc_component *component =
  2358. snd_soc_dapm_to_component(w->dapm);
  2359. int ret = 0;
  2360. dev_dbg(component->dev, "%s: event = %d\n", __func__, event);
  2361. switch (event) {
  2362. case SND_SOC_DAPM_PRE_PMU:
  2363. ret = aqt_cdc_mclk_enable(component, true);
  2364. break;
  2365. case SND_SOC_DAPM_POST_PMD:
  2366. ret = aqt_cdc_mclk_enable(component, false);
  2367. break;
  2368. }
  2369. return ret;
  2370. }
  2371. static int aif_cap_mixer_get(struct snd_kcontrol *kcontrol,
  2372. struct snd_ctl_elem_value *ucontrol)
  2373. {
  2374. return 0;
  2375. }
  2376. static int aif_cap_mixer_put(struct snd_kcontrol *kcontrol,
  2377. struct snd_ctl_elem_value *ucontrol)
  2378. {
  2379. return 0;
  2380. }
  2381. static const struct snd_kcontrol_new aif1_cap_mixer[] = {
  2382. SOC_SINGLE_EXT("TX0", SND_SOC_NOPM, AQT_TX0, 1, 0,
  2383. aif_cap_mixer_get, aif_cap_mixer_put),
  2384. SOC_SINGLE_EXT("TX1", SND_SOC_NOPM, AQT_TX1, 1, 0,
  2385. aif_cap_mixer_get, aif_cap_mixer_put),
  2386. };
  2387. static const char * const rx_inp_st_mux_text[] = {
  2388. "ZERO", "SRC0",
  2389. };
  2390. AQT_DAPM_ENUM(rx_inp_st, AQT1000_CDC_RX_INP_MUX_SIDETONE_SRC_CFG0, 4,
  2391. rx_inp_st_mux_text);
  2392. static const struct snd_soc_dapm_widget aqt_dapm_widgets[] = {
  2393. SND_SOC_DAPM_SUPPLY("AQT MCLK", SND_SOC_NOPM, 0, 0, aqt_mclk_event,
  2394. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2395. SND_SOC_DAPM_AIF_OUT_E("AQT AIF1 CAP", "AQT AIF1 Capture", 0,
  2396. SND_SOC_NOPM, AIF1_CAP, 0, aqt_codec_enable_i2s_tx,
  2397. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2398. SND_SOC_DAPM_MIXER("AQT AIF1 CAP Mixer", SND_SOC_NOPM, AIF1_CAP, 0,
  2399. aif1_cap_mixer, ARRAY_SIZE(aif1_cap_mixer)),
  2400. AQT_DAPM_MUX("AQT TX0_MUX", 0, tx0),
  2401. AQT_DAPM_MUX("AQT TX1_MUX", 0, tx1),
  2402. SND_SOC_DAPM_MUX_E("AQT ADC0 MUX", AQT1000_CDC_TX0_TX_PATH_CTL, 5, 0,
  2403. &tx_adc0_mux, aqt_codec_enable_dec,
  2404. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2405. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2406. SND_SOC_DAPM_MUX_E("AQT ADC1 MUX", AQT1000_CDC_TX1_TX_PATH_CTL, 5, 0,
  2407. &tx_adc1_mux, aqt_codec_enable_dec,
  2408. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2409. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2410. SND_SOC_DAPM_MUX_E("AQT ADC2 MUX", AQT1000_CDC_TX2_TX_PATH_CTL, 5, 0,
  2411. &tx_adc2_mux, aqt_codec_enable_dec,
  2412. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2413. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2414. AQT_DAPM_MUX("AQT AMIC0_MUX", 0, tx_amic0),
  2415. AQT_DAPM_MUX("AQT AMIC1_MUX", 0, tx_amic1),
  2416. AQT_DAPM_MUX("AQT AMIC2_MUX", 0, tx_amic2),
  2417. SND_SOC_DAPM_ADC_E("AQT ADC_L", NULL, AQT1000_ANA_AMIC1, 7, 0,
  2418. aqt_codec_enable_adc, SND_SOC_DAPM_PRE_PMU),
  2419. SND_SOC_DAPM_ADC_E("AQT ADC_R", NULL, AQT1000_ANA_AMIC2, 7, 0,
  2420. aqt_codec_enable_adc, SND_SOC_DAPM_PRE_PMU),
  2421. SND_SOC_DAPM_ADC_E("AQT ADC_V", NULL, AQT1000_ANA_AMIC3, 7, 0,
  2422. aqt_codec_enable_adc, SND_SOC_DAPM_PRE_PMU),
  2423. AQT_DAPM_MUX("AQT AMIC10_MUX", 0, tx_amic10),
  2424. AQT_DAPM_MUX("AQT AMIC11_MUX", 0, tx_amic11),
  2425. AQT_DAPM_MUX("AQT AMIC12_MUX", 0, tx_amic12),
  2426. AQT_DAPM_MUX("AQT AMIC13_MUX", 0, tx_amic13),
  2427. SND_SOC_DAPM_SWITCH_E("AQT ANC OUT HPHL Enable", SND_SOC_NOPM,
  2428. INTERP_HPHL, 0, &anc_hphl_pa_switch, aqt_anc_out_switch_cb,
  2429. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  2430. SND_SOC_DAPM_SWITCH_E("AQT ANC OUT HPHR Enable", SND_SOC_NOPM,
  2431. INTERP_HPHR, 0, &anc_hphr_pa_switch, aqt_anc_out_switch_cb,
  2432. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  2433. SND_SOC_DAPM_MIXER("AQT RX INT1 MIX2", SND_SOC_NOPM, 0, 0, NULL, 0),
  2434. SND_SOC_DAPM_MIXER("AQT RX INT2 MIX2", SND_SOC_NOPM, 0, 0, NULL, 0),
  2435. AQT_DAPM_MUX("AQT ANC0 FB MUX", 0, anc0_fb),
  2436. AQT_DAPM_MUX("AQT ANC1 FB MUX", 0, anc1_fb),
  2437. SND_SOC_DAPM_INPUT("AQT AMIC1"),
  2438. SND_SOC_DAPM_INPUT("AQT AMIC2"),
  2439. SND_SOC_DAPM_INPUT("AQT AMIC3"),
  2440. SND_SOC_DAPM_MIXER("AQT I2S_L RX", SND_SOC_NOPM, 0, 0, NULL, 0),
  2441. SND_SOC_DAPM_MIXER("AQT I2S_R RX", SND_SOC_NOPM, 0, 0, NULL, 0),
  2442. SND_SOC_DAPM_AIF_IN_E("AQT AIF1 PB", "AQT AIF1 Playback", 0,
  2443. SND_SOC_NOPM, AIF1_PB, 0, aqt_codec_enable_i2s_rx,
  2444. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2445. SND_SOC_DAPM_MUX_E("AQT RX INT1_1 MUX", SND_SOC_NOPM, INTERP_HPHL, 0,
  2446. &rx_int1_1_mux, aqt_codec_enable_main_path,
  2447. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2448. SND_SOC_DAPM_POST_PMD),
  2449. SND_SOC_DAPM_MUX_E("AQT RX INT2_1 MUX", SND_SOC_NOPM, INTERP_HPHR, 0,
  2450. &rx_int2_1_mux, aqt_codec_enable_main_path,
  2451. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2452. SND_SOC_DAPM_POST_PMD),
  2453. SND_SOC_DAPM_MUX_E("AQT RX INT1_2 MUX", SND_SOC_NOPM, INTERP_HPHL, 0,
  2454. &rx_int1_2_mux, aqt_codec_enable_mix_path,
  2455. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2456. SND_SOC_DAPM_POST_PMD),
  2457. SND_SOC_DAPM_MUX_E("AQT RX INT2_2 MUX", SND_SOC_NOPM, INTERP_HPHR, 0,
  2458. &rx_int2_2_mux, aqt_codec_enable_mix_path,
  2459. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2460. SND_SOC_DAPM_POST_PMD),
  2461. AQT_DAPM_MUX("AQT RX INT1_1 INTERP", 0, rx_int1_1_interp),
  2462. AQT_DAPM_MUX("AQT RX INT1_2 INTERP", 0, rx_int1_2_interp),
  2463. AQT_DAPM_MUX("AQT RX INT2_1 INTERP", 0, rx_int2_1_interp),
  2464. AQT_DAPM_MUX("AQT RX INT2_2 INTERP", 0, rx_int2_2_interp),
  2465. SND_SOC_DAPM_MIXER("AQT RX INT1 MIX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  2466. SND_SOC_DAPM_MIXER("AQT RX INT2 MIX1", SND_SOC_NOPM, 0, 0, NULL, 0),
  2467. SND_SOC_DAPM_MUX_E("AQT ASRC0 MUX", SND_SOC_NOPM, ASRC0, 0,
  2468. &asrc0_mux, aqt_codec_enable_asrc_resampler,
  2469. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2470. SND_SOC_DAPM_MUX_E("AQT ASRC1 MUX", SND_SOC_NOPM, ASRC1, 0,
  2471. &asrc1_mux, aqt_codec_enable_asrc_resampler,
  2472. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2473. AQT_DAPM_MUX("AQT RX INT1 DEM MUX", 0, rx_int1_dem),
  2474. AQT_DAPM_MUX("AQT RX INT2 DEM MUX", 0, rx_int2_dem),
  2475. SND_SOC_DAPM_DAC_E("AQT RX INT1 DAC", NULL, AQT1000_ANA_HPH,
  2476. 5, 0, aqt_codec_hphl_dac_event,
  2477. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2478. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2479. SND_SOC_DAPM_DAC_E("AQT RX INT2 DAC", NULL, AQT1000_ANA_HPH,
  2480. 4, 0, aqt_codec_hphr_dac_event,
  2481. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2482. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2483. SND_SOC_DAPM_PGA_E("AQT HPHL PA", AQT1000_ANA_HPH, 7, 0, NULL, 0,
  2484. aqt_codec_enable_hphl_pa,
  2485. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2486. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2487. SND_SOC_DAPM_PGA_E("AQT HPHR PA", AQT1000_ANA_HPH, 6, 0, NULL, 0,
  2488. aqt_codec_enable_hphr_pa,
  2489. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2490. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2491. SND_SOC_DAPM_PGA_E("AQT ANC HPHL PA", SND_SOC_NOPM, 0, 0, NULL, 0,
  2492. aqt_codec_enable_hphl_pa,
  2493. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2494. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2495. SND_SOC_DAPM_PGA_E("AQT ANC HPHR PA", SND_SOC_NOPM, 0, 0, NULL, 0,
  2496. aqt_codec_enable_hphr_pa,
  2497. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2498. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2499. SND_SOC_DAPM_OUTPUT("AQT HPHL"),
  2500. SND_SOC_DAPM_OUTPUT("AQT HPHR"),
  2501. SND_SOC_DAPM_OUTPUT("AQT ANC HPHL"),
  2502. SND_SOC_DAPM_OUTPUT("AQT ANC HPHR"),
  2503. SND_SOC_DAPM_MIXER_E("AQT IIR0", AQT1000_CDC_SIDETONE_IIR0_IIR_PATH_CTL,
  2504. 4, 0, NULL, 0, aqt_codec_set_iir_gain,
  2505. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  2506. SND_SOC_DAPM_MIXER("AQT SRC0",
  2507. AQT1000_CDC_SIDETONE_SRC0_ST_SRC_PATH_CTL,
  2508. 4, 0, NULL, 0),
  2509. SND_SOC_DAPM_MICBIAS_E("AQT MIC BIAS1", SND_SOC_NOPM, 0, 0,
  2510. aqt_codec_enable_micbias, SND_SOC_DAPM_PRE_PMU |
  2511. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  2512. SND_SOC_DAPM_SUPPLY("AQT RX_BIAS", SND_SOC_NOPM, 0, 0,
  2513. aqt_codec_enable_rx_bias,
  2514. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2515. SND_SOC_DAPM_SUPPLY("AQT RX INT1 NATIVE SUPPLY", SND_SOC_NOPM,
  2516. INTERP_HPHL, 0, aqt_enable_native_supply,
  2517. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  2518. SND_SOC_DAPM_SUPPLY("AQT RX INT2 NATIVE SUPPLY", SND_SOC_NOPM,
  2519. INTERP_HPHR, 0, aqt_enable_native_supply,
  2520. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  2521. AQT_DAPM_MUX("AQT RX INT1_1 NATIVE MUX", 0, int1_1_native),
  2522. AQT_DAPM_MUX("AQT RX INT2_1 NATIVE MUX", 0, int2_1_native),
  2523. SND_SOC_DAPM_MUX("AQT RX ST MUX",
  2524. AQT1000_CDC_RX_INP_MUX_SIDETONE_SRC_CFG0, 2, 0,
  2525. &rx_inp_st_mux),
  2526. };
  2527. static int aqt_startup(struct snd_pcm_substream *substream,
  2528. struct snd_soc_dai *dai)
  2529. {
  2530. pr_debug("%s(): substream = %s stream = %d\n", __func__,
  2531. substream->name, substream->stream);
  2532. return 0;
  2533. }
  2534. static void aqt_shutdown(struct snd_pcm_substream *substream,
  2535. struct snd_soc_dai *dai)
  2536. {
  2537. pr_debug("%s(): substream = %s stream = %d\n", __func__,
  2538. substream->name, substream->stream);
  2539. }
  2540. static int aqt_set_decimator_rate(struct snd_soc_dai *dai,
  2541. u32 sample_rate)
  2542. {
  2543. struct snd_soc_component *component = dai->component;
  2544. u8 tx_fs_rate = 0;
  2545. u8 tx_mux_sel = 0, tx0_mux_sel = 0, tx1_mux_sel = 0;
  2546. u16 tx_path_ctl_reg = 0;
  2547. switch (sample_rate) {
  2548. case 8000:
  2549. tx_fs_rate = 0;
  2550. break;
  2551. case 16000:
  2552. tx_fs_rate = 1;
  2553. break;
  2554. case 32000:
  2555. tx_fs_rate = 3;
  2556. break;
  2557. case 48000:
  2558. tx_fs_rate = 4;
  2559. break;
  2560. case 96000:
  2561. tx_fs_rate = 5;
  2562. break;
  2563. case 192000:
  2564. tx_fs_rate = 6;
  2565. break;
  2566. default:
  2567. dev_err(component->dev, "%s: Invalid TX sample rate: %d\n",
  2568. __func__, sample_rate);
  2569. return -EINVAL;
  2570. };
  2571. /* Find which decimator path is enabled */
  2572. tx_mux_sel = snd_soc_component_read32(component,
  2573. AQT1000_CDC_IF_ROUTER_TX_MUX_CFG0);
  2574. tx0_mux_sel = (tx_mux_sel & 0x03);
  2575. tx1_mux_sel = (tx_mux_sel & 0xC0);
  2576. if (tx0_mux_sel) {
  2577. tx_path_ctl_reg = AQT1000_CDC_TX0_TX_PATH_CTL +
  2578. ((tx0_mux_sel - 1) * 16);
  2579. snd_soc_component_update_bits(component, tx_path_ctl_reg,
  2580. 0x0F, tx_fs_rate);
  2581. }
  2582. if (tx1_mux_sel) {
  2583. tx_path_ctl_reg = AQT1000_CDC_TX0_TX_PATH_CTL +
  2584. ((tx1_mux_sel - 1) * 16);
  2585. snd_soc_component_update_bits(component, tx_path_ctl_reg,
  2586. 0x0F, tx_fs_rate);
  2587. }
  2588. return 0;
  2589. }
  2590. static int aqt_set_interpolator_rate(struct snd_soc_dai *dai,
  2591. u32 sample_rate)
  2592. {
  2593. struct snd_soc_component *component = dai->component;
  2594. int rate_val = 0;
  2595. int i;
  2596. for (i = 0; i < ARRAY_SIZE(sr_val_tbl); i++) {
  2597. if (sample_rate == sr_val_tbl[i].sample_rate) {
  2598. rate_val = sr_val_tbl[i].rate_val;
  2599. break;
  2600. }
  2601. }
  2602. if ((i == ARRAY_SIZE(sr_val_tbl)) || (rate_val < 0)) {
  2603. dev_err(component->dev, "%s: Unsupported sample rate: %d\n",
  2604. __func__, sample_rate);
  2605. return -EINVAL;
  2606. }
  2607. /* TODO - Set the rate only to enabled path */
  2608. /* Set Primary interpolator rate */
  2609. snd_soc_component_update_bits(component, AQT1000_CDC_RX1_RX_PATH_CTL,
  2610. 0x0F, (u8)rate_val);
  2611. snd_soc_component_update_bits(component, AQT1000_CDC_RX2_RX_PATH_CTL,
  2612. 0x0F, (u8)rate_val);
  2613. /* Set mixing path interpolator rate */
  2614. snd_soc_component_update_bits(component,
  2615. AQT1000_CDC_RX1_RX_PATH_MIX_CTL,
  2616. 0x0F, (u8)rate_val);
  2617. snd_soc_component_update_bits(component,
  2618. AQT1000_CDC_RX2_RX_PATH_MIX_CTL,
  2619. 0x0F, (u8)rate_val);
  2620. return 0;
  2621. }
  2622. static int aqt_prepare(struct snd_pcm_substream *substream,
  2623. struct snd_soc_dai *dai)
  2624. {
  2625. pr_debug("%s(): substream = %s stream = %d\n", __func__,
  2626. substream->name, substream->stream);
  2627. return 0;
  2628. }
  2629. static int aqt_hw_params(struct snd_pcm_substream *substream,
  2630. struct snd_pcm_hw_params *params,
  2631. struct snd_soc_dai *dai)
  2632. {
  2633. struct aqt1000 *aqt = snd_soc_component_get_drvdata(dai->component);
  2634. int ret = 0;
  2635. dev_dbg(aqt->dev, "%s: dai_name = %s DAI-ID %x rate %d num_ch %d\n",
  2636. __func__, dai->name, dai->id, params_rate(params),
  2637. params_channels(params));
  2638. switch (substream->stream) {
  2639. case SNDRV_PCM_STREAM_PLAYBACK:
  2640. ret = aqt_set_interpolator_rate(dai, params_rate(params));
  2641. if (ret) {
  2642. dev_err(aqt->dev, "%s: cannot set sample rate: %u\n",
  2643. __func__, params_rate(params));
  2644. return ret;
  2645. }
  2646. switch (params_width(params)) {
  2647. case 16:
  2648. aqt->dai[dai->id].bit_width = 16;
  2649. break;
  2650. case 24:
  2651. aqt->dai[dai->id].bit_width = 24;
  2652. break;
  2653. case 32:
  2654. aqt->dai[dai->id].bit_width = 32;
  2655. break;
  2656. default:
  2657. return -EINVAL;
  2658. }
  2659. aqt->dai[dai->id].rate = params_rate(params);
  2660. break;
  2661. case SNDRV_PCM_STREAM_CAPTURE:
  2662. ret = aqt_set_decimator_rate(dai, params_rate(params));
  2663. if (ret) {
  2664. dev_err(aqt->dev,
  2665. "%s: cannot set TX Decimator rate: %d\n",
  2666. __func__, ret);
  2667. return ret;
  2668. }
  2669. switch (params_width(params)) {
  2670. case 16:
  2671. aqt->dai[dai->id].bit_width = 16;
  2672. break;
  2673. case 24:
  2674. aqt->dai[dai->id].bit_width = 24;
  2675. break;
  2676. default:
  2677. dev_err(aqt->dev, "%s: Invalid format 0x%x\n",
  2678. __func__, params_width(params));
  2679. return -EINVAL;
  2680. };
  2681. aqt->dai[dai->id].rate = params_rate(params);
  2682. break;
  2683. default:
  2684. dev_err(aqt->dev, "%s: Invalid stream type %d\n", __func__,
  2685. substream->stream);
  2686. return -EINVAL;
  2687. };
  2688. return 0;
  2689. }
  2690. static struct snd_soc_dai_ops aqt_dai_ops = {
  2691. .startup = aqt_startup,
  2692. .shutdown = aqt_shutdown,
  2693. .hw_params = aqt_hw_params,
  2694. .prepare = aqt_prepare,
  2695. };
  2696. struct snd_soc_dai_driver aqt_dai[] = {
  2697. {
  2698. .name = "aqt_rx1",
  2699. .id = AIF1_PB,
  2700. .playback = {
  2701. .stream_name = "AQT AIF1 Playback",
  2702. .rates = AQT1000_RATES_MASK | AQT1000_FRAC_RATES_MASK,
  2703. .formats = AQT1000_FORMATS_S16_S24_S32_LE,
  2704. .rate_min = 8000,
  2705. .rate_max = 384000,
  2706. .channels_min = 1,
  2707. .channels_max = 2,
  2708. },
  2709. .ops = &aqt_dai_ops,
  2710. },
  2711. {
  2712. .name = "aqt_tx1",
  2713. .id = AIF1_CAP,
  2714. .capture = {
  2715. .stream_name = "AQT AIF1 Capture",
  2716. .rates = AQT1000_RATES_MASK,
  2717. .formats = AQT1000_FORMATS_S16_S24_LE,
  2718. .rate_min = 8000,
  2719. .rate_max = 192000,
  2720. .channels_min = 1,
  2721. .channels_max = 2,
  2722. },
  2723. .ops = &aqt_dai_ops,
  2724. },
  2725. };
  2726. static int aqt_enable_mclk(struct aqt1000 *aqt)
  2727. {
  2728. struct snd_soc_component *component = aqt->component;
  2729. /* Enable mclk requires master bias to be enabled first */
  2730. if (aqt->master_bias_users <= 0) {
  2731. dev_err(aqt->dev,
  2732. "%s: Cannot turn on MCLK, BG is not enabled\n",
  2733. __func__);
  2734. return -EINVAL;
  2735. }
  2736. if (++aqt->mclk_users == 1) {
  2737. /* Set clock div 2 */
  2738. snd_soc_component_update_bits(component,
  2739. AQT1000_CLK_SYS_MCLK1_PRG, 0x0C, 0x04);
  2740. snd_soc_component_update_bits(component,
  2741. AQT1000_CLK_SYS_MCLK1_PRG, 0x10, 0x10);
  2742. snd_soc_component_update_bits(component,
  2743. AQT1000_CDC_CLK_RST_CTRL_FS_CNT_CONTROL,
  2744. 0x01, 0x01);
  2745. snd_soc_component_update_bits(component,
  2746. AQT1000_CDC_CLK_RST_CTRL_MCLK_CONTROL,
  2747. 0x01, 0x01);
  2748. /*
  2749. * 10us sleep is required after clock is enabled
  2750. * as per HW requirement
  2751. */
  2752. usleep_range(10, 15);
  2753. }
  2754. dev_dbg(aqt->dev, "%s: mclk_users: %d\n", __func__, aqt->mclk_users);
  2755. return 0;
  2756. }
  2757. static int aqt_disable_mclk(struct aqt1000 *aqt)
  2758. {
  2759. struct snd_soc_component *component = aqt->component;
  2760. if (aqt->mclk_users <= 0) {
  2761. dev_err(aqt->dev, "%s: No mclk users, cannot disable mclk\n",
  2762. __func__);
  2763. return -EINVAL;
  2764. }
  2765. if (--aqt->mclk_users == 0) {
  2766. snd_soc_component_update_bits(component,
  2767. AQT1000_CDC_CLK_RST_CTRL_MCLK_CONTROL,
  2768. 0x01, 0x00);
  2769. snd_soc_component_update_bits(component,
  2770. AQT1000_CDC_CLK_RST_CTRL_FS_CNT_CONTROL,
  2771. 0x01, 0x00);
  2772. snd_soc_component_update_bits(component,
  2773. AQT1000_CLK_SYS_MCLK1_PRG, 0x10, 0x00);
  2774. }
  2775. dev_dbg(component->dev, "%s: mclk_users: %d\n", __func__,
  2776. aqt->mclk_users);
  2777. return 0;
  2778. }
  2779. static int aqt_enable_master_bias(struct aqt1000 *aqt)
  2780. {
  2781. struct snd_soc_component *component = aqt->component;
  2782. mutex_lock(&aqt->master_bias_lock);
  2783. aqt->master_bias_users++;
  2784. if (aqt->master_bias_users == 1) {
  2785. snd_soc_component_update_bits(component, AQT1000_ANA_BIAS,
  2786. 0x80, 0x80);
  2787. snd_soc_component_update_bits(component, AQT1000_ANA_BIAS,
  2788. 0x40, 0x40);
  2789. /*
  2790. * 1ms delay is required after pre-charge is enabled
  2791. * as per HW requirement
  2792. */
  2793. usleep_range(1000, 1100);
  2794. snd_soc_component_update_bits(component, AQT1000_ANA_BIAS,
  2795. 0x40, 0x00);
  2796. }
  2797. mutex_unlock(&aqt->master_bias_lock);
  2798. return 0;
  2799. }
  2800. static int aqt_disable_master_bias(struct aqt1000 *aqt)
  2801. {
  2802. struct snd_soc_component *component = aqt->component;
  2803. mutex_lock(&aqt->master_bias_lock);
  2804. if (aqt->master_bias_users <= 0) {
  2805. mutex_unlock(&aqt->master_bias_lock);
  2806. return -EINVAL;
  2807. }
  2808. aqt->master_bias_users--;
  2809. if (aqt->master_bias_users == 0)
  2810. snd_soc_component_update_bits(component, AQT1000_ANA_BIAS,
  2811. 0x80, 0x00);
  2812. mutex_unlock(&aqt->master_bias_lock);
  2813. return 0;
  2814. }
  2815. static int aqt_cdc_req_mclk_enable(struct aqt1000 *aqt,
  2816. bool enable)
  2817. {
  2818. int ret = 0;
  2819. if (enable) {
  2820. ret = clk_prepare_enable(aqt->ext_clk);
  2821. if (ret) {
  2822. dev_err(aqt->dev, "%s: ext clk enable failed\n",
  2823. __func__);
  2824. goto done;
  2825. }
  2826. /* Get BG */
  2827. aqt_enable_master_bias(aqt);
  2828. /* Get MCLK */
  2829. aqt_enable_mclk(aqt);
  2830. } else {
  2831. /* put MCLK */
  2832. aqt_disable_mclk(aqt);
  2833. /* put BG */
  2834. if (aqt_disable_master_bias(aqt))
  2835. dev_err(aqt->dev, "%s: master bias disable failed\n",
  2836. __func__);
  2837. clk_disable_unprepare(aqt->ext_clk);
  2838. }
  2839. done:
  2840. return ret;
  2841. }
  2842. static int __aqt_cdc_mclk_enable_locked(struct aqt1000 *aqt,
  2843. bool enable)
  2844. {
  2845. int ret = 0;
  2846. dev_dbg(aqt->dev, "%s: mclk_enable = %u\n", __func__, enable);
  2847. if (enable)
  2848. ret = aqt_cdc_req_mclk_enable(aqt, true);
  2849. else
  2850. aqt_cdc_req_mclk_enable(aqt, false);
  2851. return ret;
  2852. }
  2853. static int __aqt_cdc_mclk_enable(struct aqt1000 *aqt,
  2854. bool enable)
  2855. {
  2856. int ret;
  2857. mutex_lock(&aqt->cdc_bg_clk_lock);
  2858. ret = __aqt_cdc_mclk_enable_locked(aqt, enable);
  2859. mutex_unlock(&aqt->cdc_bg_clk_lock);
  2860. return ret;
  2861. }
  2862. /**
  2863. * aqt_cdc_mclk_enable - Enable/disable codec mclk
  2864. *
  2865. * @component: codec component instance
  2866. * @enable: Indicates clk enable or disable
  2867. *
  2868. * Returns 0 on Success and error on failure
  2869. */
  2870. int aqt_cdc_mclk_enable(struct snd_soc_component *component, bool enable)
  2871. {
  2872. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  2873. return __aqt_cdc_mclk_enable(aqt, enable);
  2874. }
  2875. EXPORT_SYMBOL(aqt_cdc_mclk_enable);
  2876. /*
  2877. * aqt_get_micb_vout_ctl_val: converts micbias from volts to register value
  2878. * @micb_mv: micbias in mv
  2879. *
  2880. * return register value converted
  2881. */
  2882. int aqt_get_micb_vout_ctl_val(u32 micb_mv)
  2883. {
  2884. /* min micbias voltage is 1V and maximum is 2.85V */
  2885. if (micb_mv < 1000 || micb_mv > 2850) {
  2886. pr_err("%s: unsupported micbias voltage\n", __func__);
  2887. return -EINVAL;
  2888. }
  2889. return (micb_mv - 1000) / 50;
  2890. }
  2891. EXPORT_SYMBOL(aqt_get_micb_vout_ctl_val);
  2892. static int aqt_set_micbias(struct aqt1000 *aqt,
  2893. struct aqt1000_pdata *pdata)
  2894. {
  2895. struct snd_soc_component *component = aqt->component;
  2896. int vout_ctl_1;
  2897. if (!pdata) {
  2898. dev_err(component->dev, "%s: NULL pdata\n", __func__);
  2899. return -ENODEV;
  2900. }
  2901. /* set micbias voltage */
  2902. vout_ctl_1 = aqt_get_micb_vout_ctl_val(pdata->micbias.micb1_mv);
  2903. if (vout_ctl_1 < 0)
  2904. return -EINVAL;
  2905. snd_soc_component_update_bits(component, AQT1000_ANA_MICB1,
  2906. 0x3F, vout_ctl_1);
  2907. return 0;
  2908. }
  2909. static ssize_t aqt_codec_version_read(struct snd_info_entry *entry,
  2910. void *file_private_data,
  2911. struct file *file,
  2912. char __user *buf, size_t count,
  2913. loff_t pos)
  2914. {
  2915. char buffer[AQT_VERSION_ENTRY_SIZE];
  2916. int len = 0;
  2917. len = snprintf(buffer, sizeof(buffer), "AQT1000_1_0\n");
  2918. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2919. }
  2920. static struct snd_info_entry_ops aqt_codec_info_ops = {
  2921. .read = aqt_codec_version_read,
  2922. };
  2923. /*
  2924. * aqt_codec_info_create_codec_entry - creates aqt1000 module
  2925. * @codec_root: The parent directory
  2926. * @component: Codec component instance
  2927. *
  2928. * Creates aqt1000 module and version entry under the given
  2929. * parent directory.
  2930. *
  2931. * Return: 0 on success or negative error code on failure.
  2932. */
  2933. int aqt_codec_info_create_codec_entry(struct snd_info_entry *codec_root,
  2934. struct snd_soc_component *component)
  2935. {
  2936. struct snd_info_entry *version_entry;
  2937. struct aqt1000 *aqt;
  2938. struct snd_soc_card *card;
  2939. if (!codec_root || !component)
  2940. return -EINVAL;
  2941. aqt = snd_soc_component_get_drvdata(component);
  2942. if (!aqt) {
  2943. dev_dbg(component->dev, "%s: aqt is NULL\n", __func__);
  2944. return -EINVAL;
  2945. }
  2946. card = component->card;
  2947. aqt->entry = snd_info_create_subdir(codec_root->module,
  2948. "aqt1000", codec_root);
  2949. if (!aqt->entry) {
  2950. dev_dbg(component->dev, "%s: failed to create aqt1000 entry\n",
  2951. __func__);
  2952. return -ENOMEM;
  2953. }
  2954. version_entry = snd_info_create_card_entry(card->snd_card,
  2955. "version",
  2956. aqt->entry);
  2957. if (!version_entry) {
  2958. dev_dbg(component->dev, "%s: failed to create aqt1000 version entry\n",
  2959. __func__);
  2960. return -ENOMEM;
  2961. }
  2962. version_entry->private_data = aqt;
  2963. version_entry->size = AQT_VERSION_ENTRY_SIZE;
  2964. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  2965. version_entry->c.ops = &aqt_codec_info_ops;
  2966. if (snd_info_register(version_entry) < 0) {
  2967. snd_info_free_entry(version_entry);
  2968. return -ENOMEM;
  2969. }
  2970. aqt->version_entry = version_entry;
  2971. return 0;
  2972. }
  2973. EXPORT_SYMBOL(aqt_codec_info_create_codec_entry);
  2974. static const struct aqt_reg_mask_val aqt_codec_reg_init[] = {
  2975. {AQT1000_CHIP_CFG0_EFUSE_CTL, 0x01, 0x01},
  2976. };
  2977. static const struct aqt_reg_mask_val aqt_codec_reg_update[] = {
  2978. {AQT1000_LDOH_MODE, 0x1F, 0x0B},
  2979. {AQT1000_MICB1_TEST_CTL_2, 0x07, 0x01},
  2980. {AQT1000_MICB1_MISC_MICB1_INM_RES_BIAS, 0x03, 0x02},
  2981. {AQT1000_MICB1_MISC_MICB1_INM_RES_BIAS, 0x0C, 0x08},
  2982. {AQT1000_MICB1_MISC_MICB1_INM_RES_BIAS, 0x30, 0x20},
  2983. {AQT1000_CDC_TX0_TX_PATH_CFG1, 0x01, 0x00},
  2984. {AQT1000_CDC_TX1_TX_PATH_CFG1, 0x01, 0x00},
  2985. {AQT1000_CDC_TX2_TX_PATH_CFG1, 0x01, 0x00},
  2986. };
  2987. static void aqt_codec_init_reg(struct aqt1000 *priv)
  2988. {
  2989. struct snd_soc_component *component = priv->component;
  2990. u32 i;
  2991. for (i = 0; i < ARRAY_SIZE(aqt_codec_reg_init); i++)
  2992. snd_soc_component_update_bits(component,
  2993. aqt_codec_reg_init[i].reg,
  2994. aqt_codec_reg_init[i].mask,
  2995. aqt_codec_reg_init[i].val);
  2996. }
  2997. static void aqt_codec_update_reg(struct aqt1000 *priv)
  2998. {
  2999. struct snd_soc_component *component = priv->component;
  3000. u32 i;
  3001. for (i = 0; i < ARRAY_SIZE(aqt_codec_reg_update); i++)
  3002. snd_soc_component_update_bits(component,
  3003. aqt_codec_reg_update[i].reg,
  3004. aqt_codec_reg_update[i].mask,
  3005. aqt_codec_reg_update[i].val);
  3006. }
  3007. static int aqt_soc_codec_probe(struct snd_soc_component *component)
  3008. {
  3009. struct aqt1000 *aqt;
  3010. struct aqt1000_pdata *pdata;
  3011. struct snd_soc_dapm_context *dapm =
  3012. snd_soc_component_get_dapm(component);
  3013. int i, ret = 0;
  3014. dev_dbg(component->dev, "%s()\n", __func__);
  3015. aqt = snd_soc_component_get_drvdata(component);
  3016. snd_soc_component_init_regmap(component, aqt->regmap);
  3017. mutex_init(&aqt->codec_mutex);
  3018. mutex_init(&aqt->i2s_lock);
  3019. /* Class-H Init */
  3020. aqt_clsh_init(&aqt->clsh_d);
  3021. /* Default HPH Mode to Class-H Low HiFi */
  3022. aqt->hph_mode = CLS_H_LOHIFI;
  3023. aqt->fw_data = devm_kzalloc(component->dev, sizeof(*(aqt->fw_data)),
  3024. GFP_KERNEL);
  3025. if (!aqt->fw_data)
  3026. goto err;
  3027. set_bit(WCD9XXX_ANC_CAL, aqt->fw_data->cal_bit);
  3028. set_bit(WCD9XXX_MBHC_CAL, aqt->fw_data->cal_bit);
  3029. /* Register for Clock */
  3030. aqt->ext_clk = clk_get(aqt->dev, "aqt_clk");
  3031. if (IS_ERR(aqt->ext_clk)) {
  3032. dev_err(aqt->dev, "%s: clk get %s failed\n",
  3033. __func__, "aqt_ext_clk");
  3034. goto err_clk;
  3035. }
  3036. ret = wcd_cal_create_hwdep(aqt->fw_data,
  3037. AQT1000_CODEC_HWDEP_NODE, component);
  3038. if (ret < 0) {
  3039. dev_err(component->dev, "%s hwdep failed %d\n", __func__, ret);
  3040. goto err_hwdep;
  3041. }
  3042. /* Initialize MBHC module */
  3043. ret = aqt_mbhc_init(&aqt->mbhc, component, aqt->fw_data);
  3044. if (ret) {
  3045. pr_err("%s: mbhc initialization failed\n", __func__);
  3046. goto err_hwdep;
  3047. }
  3048. aqt->component = component;
  3049. for (i = 0; i < COMPANDER_MAX; i++)
  3050. aqt->comp_enabled[i] = 0;
  3051. aqt_cdc_mclk_enable(component, true);
  3052. aqt_codec_init_reg(aqt);
  3053. aqt_cdc_mclk_enable(component, false);
  3054. /* Add 100usec delay as per HW requirement */
  3055. usleep_range(100, 110);
  3056. aqt_codec_update_reg(aqt);
  3057. pdata = dev_get_platdata(component->dev);
  3058. /* If 1.8v is supplied externally, then disable internal 1.8v supply */
  3059. for (i = 0; i < pdata->num_supplies; i++) {
  3060. if (!strcmp(pdata->regulator->name, "aqt_vdd1p8")) {
  3061. snd_soc_component_update_bits(component,
  3062. AQT1000_BUCK_5V_EN_CTL,
  3063. 0x03, 0x00);
  3064. dev_dbg(component->dev, "%s: Disabled internal supply\n",
  3065. __func__);
  3066. break;
  3067. }
  3068. }
  3069. aqt_set_micbias(aqt, pdata);
  3070. snd_soc_dapm_add_routes(dapm, aqt_audio_map,
  3071. ARRAY_SIZE(aqt_audio_map));
  3072. for (i = 0; i < NUM_CODEC_DAIS; i++) {
  3073. INIT_LIST_HEAD(&aqt->dai[i].ch_list);
  3074. init_waitqueue_head(&aqt->dai[i].dai_wait);
  3075. }
  3076. for (i = 0; i < AQT1000_NUM_DECIMATORS; i++) {
  3077. aqt->tx_hpf_work[i].aqt = aqt;
  3078. aqt->tx_hpf_work[i].decimator = i;
  3079. INIT_DELAYED_WORK(&aqt->tx_hpf_work[i].dwork,
  3080. aqt_tx_hpf_corner_freq_callback);
  3081. aqt->tx_mute_dwork[i].aqt = aqt;
  3082. aqt->tx_mute_dwork[i].decimator = i;
  3083. INIT_DELAYED_WORK(&aqt->tx_mute_dwork[i].dwork,
  3084. aqt_tx_mute_update_callback);
  3085. }
  3086. mutex_lock(&aqt->codec_mutex);
  3087. snd_soc_dapm_disable_pin(dapm, "AQT ANC HPHL PA");
  3088. snd_soc_dapm_disable_pin(dapm, "AQT ANC HPHR PA");
  3089. snd_soc_dapm_disable_pin(dapm, "AQT ANC HPHL");
  3090. snd_soc_dapm_disable_pin(dapm, "AQT ANC HPHR");
  3091. mutex_unlock(&aqt->codec_mutex);
  3092. snd_soc_dapm_ignore_suspend(dapm, "AQT AIF1 Playback");
  3093. snd_soc_dapm_ignore_suspend(dapm, "AQT AIF1 Capture");
  3094. snd_soc_dapm_sync(dapm);
  3095. return ret;
  3096. err_hwdep:
  3097. clk_put(aqt->ext_clk);
  3098. err_clk:
  3099. devm_kfree(component->dev, aqt->fw_data);
  3100. aqt->fw_data = NULL;
  3101. err:
  3102. mutex_destroy(&aqt->i2s_lock);
  3103. mutex_destroy(&aqt->codec_mutex);
  3104. return ret;
  3105. }
  3106. static void aqt_soc_codec_remove(struct snd_soc_component *component)
  3107. {
  3108. struct aqt1000 *aqt = snd_soc_component_get_drvdata(component);
  3109. /* Deinitialize MBHC module */
  3110. aqt_mbhc_deinit(component);
  3111. aqt->mbhc = NULL;
  3112. mutex_destroy(&aqt->i2s_lock);
  3113. mutex_destroy(&aqt->codec_mutex);
  3114. clk_put(aqt->ext_clk);
  3115. return;
  3116. }
  3117. static const struct snd_soc_component_driver snd_cdc_dev_aqt = {
  3118. .name = DRV_NAME,
  3119. .probe = aqt_soc_codec_probe,
  3120. .remove = aqt_soc_codec_remove,
  3121. .controls = aqt_snd_controls,
  3122. .num_controls = ARRAY_SIZE(aqt_snd_controls),
  3123. .dapm_widgets = aqt_dapm_widgets,
  3124. .num_dapm_widgets = ARRAY_SIZE(aqt_dapm_widgets),
  3125. .dapm_routes = aqt_audio_map,
  3126. .num_dapm_routes = ARRAY_SIZE(aqt_audio_map),
  3127. };
  3128. /*
  3129. * aqt_register_codec: Register the device to ASoC
  3130. * @dev: device
  3131. *
  3132. * return 0 success or error code in case of failure
  3133. */
  3134. int aqt_register_codec(struct device *dev)
  3135. {
  3136. return snd_soc_register_component(dev, &snd_cdc_dev_aqt, aqt_dai,
  3137. ARRAY_SIZE(aqt_dai));
  3138. }
  3139. EXPORT_SYMBOL(aqt_register_codec);