aqt1000.c 98 KB

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