aqt1000.c 93 KB

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